|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Class with methods used in encryption and decryption process. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <e32std.h> |
|
19 #include <utf.h> |
|
20 |
|
21 #include "xmlsecwerrors.h" |
|
22 #include "xmlsecwdefs.h" |
|
23 #include "xmlsecwtemplate.h" |
|
24 |
|
25 #include "xmlsec_templates.h" |
|
26 #include "xmlsec_xmlsec.h" |
|
27 #include "xmlsec_xmltree.h" |
|
28 |
|
29 #include <xmlengdocument.h> |
|
30 #include <xmlengdomparser.h> |
|
31 #include <xmlengutils.h> |
|
32 |
|
33 // --------------------------------------------------------------------------- |
|
34 // Set Template |
|
35 // --------------------------------------------------------------------------- |
|
36 // |
|
37 void XmlSecTemplate::SetTemplateL(RXmlEngDocument& aTemplate, const RXmlEngDocument& aInput) |
|
38 { |
|
39 if(aInput.IsNull()) |
|
40 { |
|
41 User::Leave(KErrTemplate); |
|
42 }; |
|
43 if(aTemplate.NotNull()) |
|
44 { |
|
45 aTemplate.Close(); |
|
46 } |
|
47 aTemplate = aInput.CloneDocumentL(); |
|
48 } |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // Set Template from file |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 void XmlSecTemplate::SetTemplateFromFileL(RXmlEngDocument& aTemplate, |
|
55 const TDesC8& aFile, |
|
56 RFs& aRFs) |
|
57 { |
|
58 if(aTemplate.NotNull()) |
|
59 { |
|
60 aTemplate.Close(); |
|
61 } |
|
62 // load template |
|
63 HBufC16* buf = CnvUtfConverter::ConvertToUnicodeFromUtf8L(aFile); |
|
64 CleanupStack::PushL(buf); |
|
65 RXmlEngDOMImplementation dom; |
|
66 dom.OpenL(); |
|
67 CleanupClosePushL(dom); |
|
68 RXmlEngDOMParser parser; |
|
69 User::LeaveIfError(parser.Open(dom)); |
|
70 CleanupClosePushL(parser); |
|
71 aTemplate = parser.ParseFileL(aRFs,buf->Des()); |
|
72 CleanupStack::PopAndDestroy(&parser); |
|
73 CleanupStack::PopAndDestroy(&dom); |
|
74 CleanupStack::PopAndDestroy(buf); |
|
75 } |
|
76 |
|
77 // --------------------------------------------------------------------------- |
|
78 // Set Template from file |
|
79 // --------------------------------------------------------------------------- |
|
80 // |
|
81 void XmlSecTemplate::SetTemplateFromFileL(RXmlEngDocument& aTemplate, const TDesC8& aFile) |
|
82 { |
|
83 RFs rfs; |
|
84 User::LeaveIfError(rfs.Connect()); |
|
85 CleanupClosePushL(rfs); |
|
86 XmlSecTemplate::SetTemplateFromFileL(aTemplate, aFile, rfs); |
|
87 CleanupStack::PopAndDestroy(&rfs); |
|
88 } |
|
89 |
|
90 // --------------------------------------------------------------------------- |
|
91 // Set Template from buffer |
|
92 // --------------------------------------------------------------------------- |
|
93 // |
|
94 void XmlSecTemplate::SetTemplateFromBufferL(RXmlEngDocument& aTemplate,const TDesC8& aBuffer) |
|
95 { |
|
96 if(aTemplate.NotNull()) |
|
97 { |
|
98 aTemplate.Close(); |
|
99 } |
|
100 // load template |
|
101 RXmlEngDOMImplementation dom; |
|
102 dom.OpenL(); |
|
103 CleanupClosePushL(dom); |
|
104 RXmlEngDOMParser parser; |
|
105 User::LeaveIfError(parser.Open(dom)); |
|
106 CleanupClosePushL(parser); |
|
107 aTemplate = parser.ParseL(aBuffer); |
|
108 CleanupStack::PopAndDestroy(&parser); |
|
109 CleanupStack::PopAndDestroy(&dom); |
|
110 } |
|
111 |
|
112 // --------------------------------------------------------------------------- |
|
113 // Destroy Template |
|
114 // --------------------------------------------------------------------------- |
|
115 // |
|
116 void XmlSecTemplate::DestroyTemplate(RXmlEngDocument& aTemplate) |
|
117 { |
|
118 if(aTemplate.NotNull()) |
|
119 { |
|
120 aTemplate.Close(); |
|
121 } |
|
122 } |
|
123 |
|
124 // --------------------------------------------------------------------------- |
|
125 // Add <KeyName> element to <KeyInfo> node and set the value of it. |
|
126 // --------------------------------------------------------------------------- |
|
127 // |
|
128 void XmlSecTemplate::SetKeyInfoL(RXmlEngDocument& aTemplate, const TDesC8& aKeyName) |
|
129 { |
|
130 if(!aKeyName.Compare(KNullDesC8)) |
|
131 { |
|
132 User::Leave(KErrWrongParameter); |
|
133 } |
|
134 if(aTemplate.IsNull()) |
|
135 { |
|
136 User::Leave(KErrTemplate); |
|
137 } |
|
138 xmlNodePtr keyInfo = NULL; |
|
139 TXmlEngNode tmpNode = aTemplate.DocumentElement(); |
|
140 xmlNodePtr root = INTERNAL_NODEPTR(tmpNode); |
|
141 keyInfo = xmlSecFindNode(root,xmlSecNodeKeyInfo, xmlSecDSigNs); |
|
142 if(!keyInfo) |
|
143 { |
|
144 User::Leave(KErrTemplate); |
|
145 } |
|
146 TXmlEngElement(keyInfo).RemoveChildren(); |
|
147 char* name = XmlEngXmlCharFromDes8L(aKeyName); |
|
148 root = xmlSecTmplKeyInfoAddKeyName(keyInfo,(unsigned char*)name); |
|
149 delete name; |
|
150 if(!root) |
|
151 { |
|
152 User::Leave(KErrNoMemory); |
|
153 } |
|
154 } |
|
155 |
|
156 // --------------------------------------------------------------------------- |
|
157 // Add element to <KeyInfo> node. |
|
158 // --------------------------------------------------------------------------- |
|
159 // |
|
160 void XmlSecTemplate::SetKeyInfoL(RXmlEngDocument& aTemplate, TXmlEngElement aKeyProp) |
|
161 { |
|
162 if(aKeyProp.IsNull()) |
|
163 { |
|
164 User::Leave(KErrWrongParameter); |
|
165 } |
|
166 if(aTemplate.IsNull()) |
|
167 { |
|
168 User::Leave(KErrTemplate); |
|
169 } |
|
170 xmlNodePtr keyInfo = NULL; |
|
171 TXmlEngNode tmpNode = aTemplate.DocumentElement(); |
|
172 xmlNodePtr root = INTERNAL_NODEPTR(tmpNode); |
|
173 keyInfo = xmlSecFindNode(root,xmlSecNodeKeyInfo, xmlSecDSigNs); |
|
174 if(!keyInfo) |
|
175 { |
|
176 User::Leave(KErrTemplate); |
|
177 } |
|
178 TXmlEngElement keyIn(keyInfo); |
|
179 keyIn.RemoveChildren(); |
|
180 keyIn.AppendChildL(aKeyProp); |
|
181 } |