2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file SignatureHandler.cpp
19 * @brief This is the implementation file for %SignatureHandler class.
22 #include "SignatureHandler.h"
23 #include "InstallerUtil.h"
25 using namespace Tizen::Base;
26 using namespace Tizen::Base::Collection;
28 SignatureHandler::SignatureHandler(void)
30 ,__pAuthorCertChain(null)
31 ,__pDistributorCertChain(null)
32 ,__isAuthorSignature(false)
33 ,__isDistributorSignature(false)
34 ,__pReferenceMap(null)
35 ,__isReferenceDetected(false)
39 SignatureHandler::~SignatureHandler(void)
41 if (__pAuthorCertChain)
43 __pAuthorCertChain->RemoveAll(true);
44 delete __pAuthorCertChain;
47 if (__pDistributorCertChain)
49 __pDistributorCertChain->RemoveAll(true);
50 delete __pDistributorCertChain;
55 __pReferenceMap->RemoveAll(true);
56 delete __pReferenceMap;
61 SignatureHandler::Construct(InstallationContext* pContext)
64 __pContext = pContext;
66 __pReferenceMap = new (std::nothrow) HashMap;
67 TryReturn(__pReferenceMap, false, "__pReferenceMap is null.");
69 r = __pReferenceMap->Construct();
70 TryReturn(!IsFailed(r), false, "__pReferenceMap->Construct() is failed.");
76 SignatureHandler::Parse(const char *pFilepath)
78 return ParseNormalizedDocument(pFilepath);
82 SignatureHandler::OnStartElement(const char *pName)
84 TryReturn(pName, true, "pName is null.");
88 if (strcasecmp(pName, "Signature") == 0)
90 AppLog("------------------------------------------");
91 AppLog("signature.xml");
92 AppLog("------------------------------------------");
93 AppLog("<%s>", pName);
94 status = OnSignatureStartElement();
96 else if (strcasecmp(pName, "Reference") == 0)
98 status = OnReferenceStartElement();
100 else if (strcasecmp(pName, "DigestMethod") == 0)
102 status = OnDigestMethodStartElement();
114 SignatureHandler::OnEndElement(const char *pName)
116 TryReturn(pName, true, "pName is null.");
118 if (strcasecmp(pName, "Signature") == 0)
120 __isDistributorSignature = false;
121 __isAuthorSignature = false;
122 AppLog("</%s>", pName);
124 else if (strcasecmp(pName, "Reference") == 0)
126 OnReferenceEndElement();
133 SignatureHandler::OnCharacters(const char *pCharacters)
138 pName = GetElementName();
139 TryReturn(pName, false, "pName is null.");
141 if (strcasecmp(pName, "X509Certificate") == 0)
143 status = OnCertificateValue(pCharacters);
145 else if (strcasecmp(pName, "DigestValue") == 0)
147 status = OnDigestValue(pCharacters);
159 SignatureHandler::OnSignatureStartElement(void)
161 XmlAttribute *pAttr = null;
164 pAttr = GetAttribute();
165 TryReturn(pAttr, true, "pAttr is null");
167 pId = pAttr->Find("Id");
170 AppLog("<Id = %s>", pId);
172 if (strcasecmp(pId, "AuthorSignature") == 0)
174 __isAuthorSignature = true;
176 else if (strcasecmp(pId, "DistributorSignature") == 0)
178 __isDistributorSignature = true;
186 SignatureHandler::OnReferenceStartElement(void)
188 static const char* pRefDir[] = {"bin/", "info/", null};
190 XmlAttribute *pAttr = null;
193 pAttr = GetAttribute();
194 TryReturn(pAttr, true, "pAttr is null");
196 pUri = pAttr->Find("URI");
197 TryReturn(pUri, true, "pUri is null");
201 String referenceUri = pUri;
203 for (int i = 0; pRefDir[i]; i++)
205 if (referenceUri.StartsWith(pRefDir[i], 0) == true)
207 __referenceUri = referenceUri;
208 AppLog("Reference URI=[%ls]", __referenceUri.GetPointer());
209 __isReferenceDetected = true;
218 SignatureHandler::OnDigestMethodStartElement(void)
224 SignatureHandler::OnCertificateValue(const char *pCharacters)
226 //AppLog("<X509Certificate>%s</X509Certificate>", pCharacters);
228 result r = E_SUCCESS;
230 ByteBuffer* pByteBuffer = null;
232 if (__isAuthorSignature == true)
234 if (__pAuthorCertChain == null)
236 __pAuthorCertChain = new (std::nothrow) ArrayList;
237 TryCatch(__pAuthorCertChain, res = false, "__pAuthorCertChain is null");
240 pByteBuffer = new (std::nothrow) ByteBuffer;
241 TryCatch(pByteBuffer, res = false, "pByteBuffer is null");
243 int length = strlen(pCharacters);
244 pByteBuffer->Construct(length);
246 r = pByteBuffer->SetArray((byte*)pCharacters, 0, length);
247 TryCatch(!IsFailed(r), res = false, "SetArray() is failed.");
251 __pAuthorCertChain->Add(*pByteBuffer);
254 else if (__isDistributorSignature == true)
256 if (__pDistributorCertChain == null)
258 __pDistributorCertChain = new (std::nothrow) ArrayList;
259 TryCatch(__pDistributorCertChain, res = false, "__pDistributorCertChain is null");
262 pByteBuffer = new (std::nothrow) ByteBuffer;
263 TryCatch(pByteBuffer, res = false, "pByteBuffer is null");
265 int length = strlen(pCharacters);
266 pByteBuffer->Construct(length);
268 r = pByteBuffer->SetArray((byte*)pCharacters, 0, length);
269 TryCatch(!IsFailed(r), res = false, "SetArray() is failed.");
273 __pDistributorCertChain->Add(*pByteBuffer);
283 SignatureHandler::OnDigestValue(const char *pCharacters)
285 if (__isReferenceDetected == true)
287 __digestValue = pCharacters;
288 AppLog("digest value=[%ls]", __digestValue.GetPointer());
295 SignatureHandler::OnReferenceEndElement(void)
297 TryReturn(__pReferenceMap, true, "__pReferenceMap is null.");
299 if (__isReferenceDetected == true)
301 __pReferenceMap->Add(new String(__referenceUri), new String(__digestValue));
302 __referenceUri.Clear();
303 __digestValue.Clear();
306 __isReferenceDetected = false;
312 SignatureHandler::GetAuthorCertChain(void)
314 return __pAuthorCertChain;
318 SignatureHandler::GetDistributorCertChain(void)
320 return __pDistributorCertChain;
324 SignatureHandler::GetReferenceMap()
326 return __pReferenceMap;