Installer SignatureValidator implementation
[platform/framework/native/installer.git] / src / XmlHandler / SignatureHandler.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        SignatureHandler.cpp
19  * @brief       This is the implementation file for %SignatureHandler class.
20  */
21
22 #include "SignatureHandler.h"
23 #include "InstallerUtil.h"
24
25 using namespace Tizen::Base;
26 using namespace Tizen::Base::Collection;
27
28 SignatureHandler::SignatureHandler(void)
29 :__pContext(null)
30 ,__pAuthorCertChain(null)
31 ,__pDistributorCertChain(null)
32 ,__isAuthorSignature(false)
33 ,__isDistributorSignature(false)
34 ,__pReferenceMap(null)
35 ,__isReferenceDetected(false)
36 {
37 }
38
39 SignatureHandler::~SignatureHandler(void)
40 {
41         if (__pAuthorCertChain)
42         {
43                 __pAuthorCertChain->RemoveAll(true);
44                 delete __pAuthorCertChain;
45         }
46
47         if (__pDistributorCertChain)
48         {
49                 __pDistributorCertChain->RemoveAll(true);
50                 delete __pDistributorCertChain;
51         }
52
53         if (__pReferenceMap)
54         {
55                 __pReferenceMap->RemoveAll(true);
56                 delete __pReferenceMap;
57         }
58 }
59
60 bool
61 SignatureHandler::Construct(InstallationContext* pContext)
62 {
63         result r = E_SUCCESS;
64         __pContext = pContext;
65
66         __pReferenceMap = new (std::nothrow) HashMap;
67         TryReturn(__pReferenceMap, false, "__pReferenceMap is null.");
68
69         r = __pReferenceMap->Construct();
70         TryReturn(!IsFailed(r), false, "__pReferenceMap->Construct() is failed.");
71
72         return true;
73 }
74
75 bool
76 SignatureHandler::Parse(const char *pFilepath)
77 {
78         return ParseNormalizedDocument(pFilepath);
79 }
80
81 bool
82 SignatureHandler::OnStartElement(const char *pName)
83 {
84         TryReturn(pName, true, "pName is null.");
85
86         bool status = true;
87
88         if (strcasecmp(pName, "Signature") == 0)
89         {
90                 AppLog("------------------------------------------");
91                 AppLog("signature.xml");
92                 AppLog("------------------------------------------");
93                 AppLog("<%s>", pName);
94                 status = OnSignatureStartElement();
95         }
96         else if (strcasecmp(pName, "Reference") == 0)
97         {
98                 status = OnReferenceStartElement();
99         }
100         else if (strcasecmp(pName, "DigestMethod") == 0)
101         {
102                 status = OnDigestMethodStartElement();
103         }
104
105         if (!status)
106         {
107                 return false;
108         }
109
110         return true;
111 }
112
113 bool
114 SignatureHandler::OnEndElement(const char *pName)
115 {
116         TryReturn(pName, true, "pName is null.");
117
118         if (strcasecmp(pName, "Signature") == 0)
119         {
120                 __isDistributorSignature = false;
121                 __isAuthorSignature = false;
122                 AppLog("</%s>", pName);
123         }
124         else if (strcasecmp(pName, "Reference") == 0)
125         {
126                 OnReferenceEndElement();
127         }
128
129         return true;
130 }
131
132 bool
133 SignatureHandler::OnCharacters(const char *pCharacters)
134 {
135         bool status = true;
136         char *pName = 0;
137
138         pName = GetElementName();
139         TryReturn(pName, false, "pName is null.");
140
141         if (strcasecmp(pName, "X509Certificate") == 0)
142         {
143                 status = OnCertificateValue(pCharacters);
144         }
145         else if (strcasecmp(pName, "DigestValue") == 0)
146         {
147                 status = OnDigestValue(pCharacters);
148         }
149
150         if (!status)
151         {
152                 return false;
153         }
154
155         return true;
156 }
157
158 bool
159 SignatureHandler::OnSignatureStartElement(void)
160 {
161         XmlAttribute *pAttr = null;
162         char *pId = null;
163
164         pAttr = GetAttribute();
165         TryReturn(pAttr, true, "pAttr is null");
166
167         pId = pAttr->Find("Id");
168         if (pId)
169         {
170                 AppLog("<Id = %s>", pId);
171
172                 if (strcasecmp(pId, "AuthorSignature") == 0)
173                 {
174                         __isAuthorSignature = true;
175                 }
176                 else if (strcasecmp(pId, "DistributorSignature") == 0)
177                 {
178                         __isDistributorSignature = true;
179                 }
180         }
181
182         return true;
183 }
184
185 bool
186 SignatureHandler::OnReferenceStartElement(void)
187 {
188         static const char* pRefDir[] = {"bin/", "info/", null};
189
190         XmlAttribute *pAttr = null;
191         char *pUri = null;
192
193         pAttr = GetAttribute();
194         TryReturn(pAttr, true, "pAttr is null");
195
196         pUri = pAttr->Find("URI");
197         TryReturn(pUri, true, "pUri is null");
198
199         if (pUri)
200         {
201                 String referenceUri = pUri;
202
203                 for (int i = 0; pRefDir[i]; i++)
204                 {
205                         if (referenceUri.StartsWith(pRefDir[i], 0) == true)
206                         {
207                                 __referenceUri = referenceUri;
208                                 AppLog("Reference URI=[%ls]", __referenceUri.GetPointer());
209                                 __isReferenceDetected = true;
210                         }
211                 }
212         }
213
214         return true;
215 }
216
217 bool
218 SignatureHandler::OnDigestMethodStartElement(void)
219 {
220         return true;
221 }
222
223 bool
224 SignatureHandler::OnCertificateValue(const char *pCharacters)
225 {
226         //AppLog("<X509Certificate>%s</X509Certificate>", pCharacters);
227
228         result r = E_SUCCESS;
229         bool res = true;
230         ByteBuffer* pByteBuffer = null;
231
232         if (__isAuthorSignature == true)
233         {
234                 if (__pAuthorCertChain == null)
235                 {
236                         __pAuthorCertChain = new (std::nothrow) ArrayList;
237                         TryCatch(__pAuthorCertChain, res = false, "__pAuthorCertChain is null");
238                 }
239
240                 pByteBuffer = new (std::nothrow) ByteBuffer;
241                 TryCatch(pByteBuffer, res = false, "pByteBuffer is null");
242
243                 int length = strlen(pCharacters);
244                 pByteBuffer->Construct(length);
245
246                 r = pByteBuffer->SetArray((byte*)pCharacters, 0, length);
247                 TryCatch(!IsFailed(r), res = false, "SetArray() is failed.");
248
249                 pByteBuffer->Flip();
250
251                 __pAuthorCertChain->Add(*pByteBuffer);
252                 pByteBuffer = null;
253         }
254         else if (__isDistributorSignature == true)
255         {
256                 if (__pDistributorCertChain == null)
257                 {
258                         __pDistributorCertChain = new (std::nothrow) ArrayList;
259                         TryCatch(__pDistributorCertChain, res = false, "__pDistributorCertChain is null");
260                 }
261
262                 pByteBuffer = new (std::nothrow) ByteBuffer;
263                 TryCatch(pByteBuffer, res = false, "pByteBuffer is null");
264
265                 int length = strlen(pCharacters);
266                 pByteBuffer->Construct(length);
267
268                 r = pByteBuffer->SetArray((byte*)pCharacters, 0, length);
269                 TryCatch(!IsFailed(r), res = false, "SetArray() is failed.");
270
271                 pByteBuffer->Flip();
272
273                 __pDistributorCertChain->Add(*pByteBuffer);
274                 pByteBuffer = null;
275         }
276
277 CATCH:
278         delete pByteBuffer;
279         return res;
280 }
281
282 bool
283 SignatureHandler::OnDigestValue(const char *pCharacters)
284 {
285         if (__isReferenceDetected == true)
286         {
287                 __digestValue = pCharacters;
288                 AppLog("digest value=[%ls]", __digestValue.GetPointer());
289         }
290
291         return true;
292 }
293
294 bool
295 SignatureHandler::OnReferenceEndElement(void)
296 {
297         TryReturn(__pReferenceMap, true, "__pReferenceMap is null.");
298
299         if (__isReferenceDetected == true)
300         {
301                 __pReferenceMap->Add(new String(__referenceUri), new String(__digestValue));
302                 __referenceUri.Clear();
303                 __digestValue.Clear();
304         }
305
306         __isReferenceDetected = false;
307
308         return true;
309 }
310
311 ArrayList*
312 SignatureHandler::GetAuthorCertChain(void)
313 {
314         return __pAuthorCertChain;
315 }
316
317 ArrayList*
318 SignatureHandler::GetDistributorCertChain(void)
319 {
320         return __pDistributorCertChain;
321 }
322
323 HashMap*
324 SignatureHandler::GetReferenceMap()
325 {
326         return __pReferenceMap;
327 }