Mandatorily check partial signature verification for RDS
[platform/framework/native/installer.git] / src / Manager / SignatureManager.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        SignatureManager.cpp
19  * @brief       This is the implementation file for %SignatureManager class.
20  */
21
22 #include <pkgmgr_parser.h>
23 #include <pkgmgr_installer.h>
24 #include <package-manager.h>
25 #include <pkgmgr-info.h>
26
27 #include <FBase_StringConverter.h>
28 #include <FBaseInternalTypes.h>
29
30 #include "SignatureManager.h"
31
32 #include <vcore/SignatureFinder.h>
33 #include <vcore/SignatureData.h>
34 #include <vcore/SignatureReader.h>
35 #include <vcore/SignatureValidator.h>
36 #include <vcore/WrtSignatureValidator.h>
37 #include <vcore/VCore.h>
38
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Base::Utility;
42
43 SignatureManager::SignatureManager(void)
44 :__pContext(null)
45 {
46 }
47
48 SignatureManager::~SignatureManager(void)
49 {
50 }
51
52 bool
53 SignatureManager::Construct(InstallationContext* pContext)
54 {
55         __pContext = pContext;
56
57         return true;
58 }
59
60 bool
61 SignatureManager::ValidateSignatures()
62 {
63         TryReturn(__pContext, false, "__pContext is null.");
64
65         AppLog("ValidateSignatures start >>");
66
67         bool res = false;
68         res = ValidationCore::VCoreInit(
69                         std::string("/usr/share/wrt-engine/fingerprint_list.xml"),
70                         std::string("/usr/share/wrt-engine/fingerprint_list.xsd"),
71                         std::string("/opt/dbspace/.cert_svc_vcore.db"));
72         TryReturn(res, false, "ValidationCore::VCoreInit() is failed.");
73         TryReturn(__pContext->__rootPath.IsEmpty() == false, false, "__pContext->__rootPath is empty.");
74         AppLog("ValidationCore::VCoreInit() is done");
75
76         std::unique_ptr<char[]> pRootPath(_StringConverter::CopyToCharArrayN(__pContext->__rootPath));
77         TryReturn(pRootPath, false, "__pRootPath is null.");
78
79         std::string rootPath = pRootPath.get();
80         rootPath += "/";
81         AppLog("rootPath=[%s]", rootPath.c_str());
82
83         ValidationCore::SignatureFinder::Result findRes = ValidationCore::SignatureFinder::NO_ERROR;
84         ValidationCore::SignatureFinder signatureFinder(rootPath);
85         ValidationCore::SignatureFileInfoSet signatureFiles;
86
87         findRes = signatureFinder.find(signatureFiles);
88         TryReturn(findRes == ValidationCore::SignatureFinder::NO_ERROR , false, "signatureFinder.find() is failed, findRes=[%d]", (int)findRes);
89         AppLog("signatureFinder.find() is done");
90
91         ValidationCore::SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
92         for (; iter != signatureFiles.rend(); ++iter)
93         {
94                 int sigFileNumber = iter->getFileNumber();
95                 AppLog("SignatureFiles: file=[%s]", iter->getFileName().c_str());
96                 AppLog("SignatureFiles: number=[%d]", sigFileNumber);
97
98                 if (sigFileNumber > SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
99                 {
100                         AppLog("SignatureFiles: skip!");
101                         continue;
102                 }
103
104                 ValidationCore::SignatureData data(rootPath + iter->getFileName(), iter->getFileNumber());
105
106                 try {
107                         ValidationCore::SignatureReader xml;
108                         xml.initialize(data, "/usr/share/wrt-engine/schema.xsd");
109                         xml.read(data);
110                         AppLog("ValidationCore::SignatureReader() is done");
111
112                         ValidationCore::CertificateList certList = data.getCertList();
113                         ValidationCore::CertificateList::iterator it = certList.begin();
114                         ValidationCore::CertificateList::iterator it_end = certList.end();
115                         for (; it != it_end; it++)
116                         {
117                                 std::string value = (*it)->getBase64();
118
119                                 if (data.isAuthorSignature() == true)
120                                 {
121                                         __pContext->__pAuthorCertList->Add(new String(value.c_str()));
122                                         AppLog("Author cert value=[%s]", value.c_str());
123                                 }
124                                 else
125                                 {
126                                         if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR)
127                                         {
128                                                 __pContext->__pDistributorCertList->Add(new String(value.c_str()));
129                                                 AppLog("Default distributor cert value=[%s]", value.c_str());
130                                         }
131                                         else if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
132                                         {
133                                                 AppLog("Default distributor2 cert value=[%s]", value.c_str());
134                                         }
135                                 }
136                         }
137
138                         ValidationCore::SignatureValidator::Result valRes = ValidationCore::SignatureValidator::SIGNATURE_VALID;
139                         ValidationCore::SignatureValidator::AppType appType = ValidationCore::SignatureValidator::TIZEN;
140                         ValidationCore::SignatureValidator validator(appType, false, false, false);
141
142                         AppLog("validator.check() start >>");
143                         valRes = validator.check(data, rootPath);
144                         if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
145                         {
146                                 if (valRes == ValidationCore::SignatureValidator::SIGNATURE_VERIFIED)
147                                 {
148                                         AppLog("Signature validator.check success, file=[%s], number=[%d]", iter->getFileName().c_str(), iter->getFileNumber());
149                                 }
150                                 else
151                                 {
152                                         AppLog("Signature validator.check failed, but it's ok, file=[%s], number=[%d]", iter->getFileName().c_str(), iter->getFileNumber());
153                                         __pContext->__pDistributorCertList2->RemoveAll();
154                                 }
155                         }
156                         else
157                         {
158                                 TryReturn(valRes == ValidationCore::SignatureValidator::SIGNATURE_VERIFIED, false, "validator.check() is failed, valRes=[%d]", (int)valRes);
159                                 AppLog("Signature validator.check success, file=[%s], number=[%d]", iter->getFileName().c_str(), iter->getFileNumber());
160                         }
161
162                         ValidationCore::CertificatePtr pRootCert = data.getRootCaCertificatePtr();
163                         TryReturn(pRootCert, false, "__pRootPath is null.");
164                         std::string value = pRootCert.Get()->getBase64();
165                         if (data.isAuthorSignature() == true)
166                         {
167                                 __pContext->__pAuthorCertList->Add(new String(value.c_str()));
168                                 AppLog("Author root cert value=[%s]", value.c_str());
169                         }
170                         else
171                         {
172                                 if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR)
173                                 {
174                                         __pContext->__pDistributorCertList->Add(new String(value.c_str()));
175                                         AppLog("Default root distributor cert value=[%s]", value.c_str());
176
177                                         ValidationCore::CertStoreId::Type certStoreIdType = data.getVisibilityLevel();
178                                         AppLog("CertStoreIdType = [%d]", (int)certStoreIdType);
179                                         __pContext->__rootCertType = (RootCertificateType)certStoreIdType;
180                                 }
181                                 else if ((sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2) && (valRes == ValidationCore::SignatureValidator::SIGNATURE_VERIFIED))
182                                 {
183                                         __pContext->__pDistributorCertList2->Add(new String(value.c_str()));
184                                         AppLog("Default root distributor2 cert value=[%s]", value.c_str());
185                                 }
186                         }
187                 }
188                 catch (ValidationCore::ParserSchemaException::Base)
189                 {
190                         if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
191                         {
192                                 AppLog("ValidationCore::ParserSchemaException::Base occurred, but it's ok for DISTRIBUTOR2.");
193                                 continue;
194                         }
195                         TryReturn(0, false, "ParserSchemaException::Base exception occurred.");
196                 }
197                 catch (DPL::Exception)
198                 {
199                         if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
200                         {
201                                 AppLog("DPL::Exception occurred, but it's ok for DISTRIBUTOR2.");
202                                 continue;
203                         }
204                         TryReturn(0, false, "DPL::Exception exception occurred.");
205                 }
206         }
207
208         AppLog("ValidateSignatures done successfully <<");
209
210         if (__pContext->__isUpdated == true)
211         {
212                 res = ValidateUpdate();
213                 TryReturn(res, false, "ValidateUpdate() is failed.");
214         }
215
216         return true;
217 }
218
219 bool
220 SignatureManager::ValidateUpdate()
221 {
222         TryReturn(__pContext, false, "__pContext is null.");
223
224         if (InstallerUtil::IsAuthorSignatureVerificationEnabled() == false)
225         {
226                 AppLog("ValidateUpdate() skip.");
227                 return true;
228         }
229
230         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
231         TryReturn(pPackageId, false, "pPackageId is null.");
232
233         result r = E_SUCCESS;
234         String oldCertificate;
235         String* pNewCertificate = null;
236         const char* pValue = null;
237         pkgmgr_certinfo_h handle = null;
238
239         int res = pkgmgr_pkginfo_create_certinfo(&handle);
240         if (res == PMINFO_R_OK)
241         {
242                 res = pkgmgr_pkginfo_load_certinfo(pPackageId.get(), handle);
243                 if (res == PMINFO_R_OK)
244                 {
245                         pkgmgr_pkginfo_get_cert_value(handle, PM_AUTHOR_SIGNER_CERT, &pValue);
246                         oldCertificate = pValue;
247                 }
248
249                 pkgmgr_pkginfo_destroy_certinfo(handle);
250         }
251
252         if (oldCertificate.IsEmpty() == true)
253         {
254                 AppLog("oldCert is empty.");
255                 return true;
256         }
257         else
258         {
259                 r = __pContext->__pAuthorCertList->GetAt(0, pNewCertificate);
260                 TryReturn(r == E_SUCCESS, false, "GetAt() failed.");
261
262                 AppLog("------------------------------------------");
263                 AppLog("oldCert = [%ls]", oldCertificate.GetPointer());
264                 AppLog("newCert = [%ls]", pNewCertificate->GetPointer());
265                 AppLog("------------------------------------------");
266
267                 if (oldCertificate.Equals(*pNewCertificate, true) == false)
268                 {
269                         AppLog("oldCert, newCert is different.");
270                         return false;
271                 }
272         }
273
274         AppLog("oldCert, newCert is the same.");
275         return true;
276 }
277
278 bool
279 SignatureManager::ValidatePartialReferences(const String& path, IList* pAddedList, IList* pModifiedList)
280 {
281         TryReturn(__pContext, false, "__pContext is null.");
282         TryReturn(path.IsEmpty() == false, false, "rootPath is empty.");
283
284         AppLog("ValidatePartialReferences start >>");
285
286         std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(path));
287         TryReturn(pPath, false, "pPath is null.");
288
289         std::string rootPath = pPath.get();
290         rootPath += "/";
291         AppLog("rootPath = [%s]", rootPath.c_str());
292
293         bool res = ValidationCore::VCoreInit(
294                         std::string("/usr/share/wrt-engine/fingerprint_list.xml"),
295                         std::string("/usr/share/wrt-engine/fingerprint_list.xsd"),
296                         std::string("/opt/dbspace/.cert_svc_vcore.db"));
297         TryReturn(res, false, "ValidationCore::VCoreInit() failed.");
298
299         ValidationCore::SignatureFinder::Result findRes = ValidationCore::SignatureFinder::NO_ERROR;
300         ValidationCore::SignatureFinder signatureFinder(rootPath);
301         ValidationCore::SignatureFileInfoSet signatureFiles;
302
303         findRes = signatureFinder.find(signatureFiles);
304         TryReturn(findRes == ValidationCore::SignatureFinder::NO_ERROR , false, "signatureFinder.find() failed, findRes = [%d]", (int)findRes);
305
306         ValidationCore::SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
307         for (; iter != signatureFiles.rend(); ++iter)
308         {
309                 int sigFileNumber = iter->getFileNumber();
310                 AppLog("SignatureFiles: file = [%s]", iter->getFileName().c_str());
311                 AppLog("SignatureFiles: number = [%d]", sigFileNumber);
312
313                 if (sigFileNumber > SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
314                 {
315                         AppLog("SignatureFiles: skip!");
316                         continue;
317                 }
318
319                 ValidationCore::SignatureData data(rootPath + iter->getFileName(), iter->getFileNumber());
320
321                 try
322                 {
323                         ValidationCore::SignatureReader xml;
324                         xml.initialize(data, "/usr/share/wrt-engine/schema.xsd");
325                         xml.read(data);
326
327                         ValidationCore::CertificateList certList = data.getCertList();
328                         ValidationCore::CertificateList::iterator it = certList.begin();
329                         ValidationCore::CertificateList::iterator it_end = certList.end();
330                         for (; it != it_end; it++)
331                         {
332                                 std::string value = (*it)->getBase64();
333
334                                 if (data.isAuthorSignature() == true)
335                                 {
336                                         __pContext->__pAuthorCertList->Add(new String(value.c_str()));
337                                         AppLog("Author cert value = [%s]", value.c_str());
338                                 }
339                                 else
340                                 {
341                                         if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR)
342                                         {
343                                                 __pContext->__pDistributorCertList->Add(new String(value.c_str()));
344                                                 AppLog("Default distributor cert value = [%s]", value.c_str());
345                                         }
346                                 }
347                         }
348
349                         ValidationCore::SignatureValidator::Result valRes = ValidationCore::SignatureValidator::SIGNATURE_VALID;
350                         ValidationCore::SignatureValidator::AppType appType = ValidationCore::SignatureValidator::TIZEN;
351                         ValidationCore::SignatureValidator validator(appType, false, false, false);
352
353                         std::list<std::string> uriList;
354                         if (pAddedList)
355                         {
356                                 for (int idx = 0; idx < pAddedList->GetCount(); idx++)
357                                 {
358                                         String* pFilePath = static_cast< String* >(pAddedList->GetAt(idx));
359                                         TryReturn(pFilePath, false, "pFilePath is null.");
360
361                                         std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(*pFilePath));
362                                         TryReturn(pPath, false, "pPath is null.");
363
364                                         uriList.push_back(pPath.get());
365                                         AppLog("added = [%s]", pPath.get());
366                                 }
367                         }
368                         if (pModifiedList)
369                         {
370                                 for (int idx = 0; idx < pModifiedList->GetCount(); idx++)
371                                 {
372                                         String* pFilePath = static_cast< String* >(pModifiedList->GetAt(idx));
373                                         TryReturn(pFilePath, false, "pFilePath is null.");
374
375                                         std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(*pFilePath));
376                                         TryReturn(pPath, false, "pPath is null.");
377
378                                         if ((strcasecmp(pPath.get(), "author-signature.xml") != 0) &&
379                                                         (strcasecmp(pPath.get(), "signature1.xml") != 0))
380                                         {
381                                                 uriList.push_back(pPath.get());
382                                                 AppLog("modified = [%s]", pPath.get());
383                                         }
384                                 }
385                         }
386                         TryReturn(uriList.size() != 0, false, "uriList.size is 0.");
387                         AppLog("uriList.size = [%d]", uriList.size());
388
389                         valRes = validator.checkList(data, rootPath, uriList);
390                         TryReturn(valRes == ValidationCore::SignatureValidator::SIGNATURE_VERIFIED, false, "validator.checkList() is failed, valRes=[%d]", (int)valRes);
391                         AppLog("Signature validator.checkList() success, file = [%s], number = [%d]", iter->getFileName().c_str(), iter->getFileNumber());
392                 }
393                 catch (ValidationCore::ParserSchemaException::Base)
394                 {
395                         if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
396                         {
397                                 AppLog("ValidationCore::ParserSchemaException::Base occurred, but it's ok for DISTRIBUTOR2.");
398                                 continue;
399                         }
400                         TryReturn(0, false, "ParserSchemaException::Base exception occurred.");
401                 }
402                 catch (DPL::Exception)
403                 {
404                         if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
405                         {
406                                 AppLog("DPL::Exception occurred, but it's ok for DISTRIBUTOR2.");
407                                 continue;
408                         }
409                         TryReturn(0, false, "DPL::Exception exception occurred.");
410                 }
411         }
412
413         AppLog("ValidateSignatures done successfully <<");
414
415         res = ValidateUpdate();
416         TryReturn(res, false, "ValidateUpdate() is failed.");
417
418         return true;
419 }
420
421 bool
422 SignatureManager::RegisterCertInfo() const
423 {
424         TryReturn(__pContext, false, "__pContext is null");
425
426         if (__pContext->__isHybridService == true)
427         {
428                 AppLog("Skip - HybridService");
429                 return true;
430         }
431
432         AppLog("START");
433
434         int error = 0;
435         bool res = false;
436         pkgmgr_instcertinfo_h handle = null;
437
438         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
439         TryReturn(pPackageId, false, "pPackageId is null");
440
441         error = pkgmgr_installer_create_certinfo_set_handle(&handle);
442         TryReturn(error == 0, false, "pkgmgr_installer_create_certinfo_set_handle() failed, error = [%d].", error);
443
444         if (__pContext->__pAuthorCertList)
445         {
446                 AppLog("[AuthorCert]");
447                 res = SetAuthorCertValue(handle, __pContext->__pAuthorCertList);
448                 TryCatch(res == true, , "SetAuthorCertHashValue() is failed.");
449         }
450
451         if (__pContext->__pDistributorCertList)
452         {
453                 AppLog("[DistributorCert]");
454                 res = SetDistributorCertValue(handle, __pContext->__pDistributorCertList, 1);
455                 TryCatch(res == true, , "SetDistributorCertValue() is failed.");
456         }
457
458         if (__pContext->__pDistributorCertList2)
459         {
460                 AppLog("[DistributorCert2]");
461                 res = SetDistributorCertValue(handle, __pContext->__pDistributorCertList2, 2);
462                 TryCatch(res == true, , "SetDistributorCertValue() is failed.");
463         }
464
465         error = pkgmgr_installer_save_certinfo(pPackageId.get(), handle);
466         TryCatch(error == 0, res = false, "pkgmgr_installer_save_certinfo(%s) failed, error = [%d]", pPackageId.get(), error);
467
468         AppLog("RegisterCertInfo - END");
469         AppLog("------------------------------------------");
470
471         res = true;
472
473 CATCH:
474         if (handle)
475         {
476                 pkgmgr_installer_destroy_certinfo_set_handle(handle);
477         }
478
479         return res;
480 }
481
482 bool
483 SignatureManager::UnregisterCertInfo() const
484 {
485         TryReturn(__pContext, false, "__pContext is null");
486
487         if (__pContext->__isHybridService == true)
488         {
489                 AppLog("Skip - HybridService");
490                 return true;
491         }
492
493         AppLog("START");
494         int error = 0;
495         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
496         TryReturn(pPackageId, false, "pPackageId is null");
497
498         error = pkgmgr_installer_delete_certinfo(pPackageId.get());
499         TryReturn(error == 0, false, "pkgmgr_installer_delete_certinfo(%s) failed, error = [%d]", pPackageId.get(), error);
500
501         AppLog("UnregisterCertInfo - END");
502         AppLog("------------------------------------------");
503
504         return true;
505 }
506
507 int
508 SignatureManager::GetApiVisibility(RootCertificateType certType)
509 {
510         int apiVisibility = _API_VISIBILITY_NONE;
511
512         if (certType == ROOT_CERTIFICATE_PUBLIC)
513         {
514                 apiVisibility = _API_VISIBILITY_PUBLIC;
515         }
516         else if (certType == ROOT_CERTIFICATE_PARTNER)
517         {
518                 apiVisibility = _API_VISIBILITY_PARTNER;
519         }
520         else if (certType == ROOT_CERTIFICATE_PARTNER_OPERATOR)
521         {
522                 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
523         }
524         else if (certType == ROOT_CERTIFICATE_PARTNER_MANUFACTURER)
525         {
526                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
527         }
528         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PUBLIC)
529         {
530                 apiVisibility = _API_VISIBILITY_PUBLIC;
531         }
532         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER)
533         {
534                 apiVisibility = _API_VISIBILITY_PARTNER;
535         }
536         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER_OPERATOR)
537         {
538                 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
539         }
540         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER_MANUFACTURER)
541         {
542                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
543         }
544         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PLATFORM)
545         {
546                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
547         }
548
549         return apiVisibility;
550 }
551
552 int
553 SignatureManager::GetPrivilegeLevel(int apiVisibility)
554 {
555         if (apiVisibility == _API_VISIBILITY_PARTNER_MANUFACTURER || apiVisibility == _API_VISIBILITY_PARTNER_OPERATOR)
556         {
557                 return PRIVILEGE_LEVEL_PLATFORM;
558         }
559         else if (apiVisibility == _API_VISIBILITY_PARTNER)
560         {
561                 return PRIVILEGE_LEVEL_PARTNER;
562         }
563
564         return PRIVILEGE_LEVEL_PUBLIC;
565 }
566
567 bool
568 SignatureManager::SetAuthorCertValue(void* pHandle, IListT<String *>* pCertList) const
569 {
570         TryReturn(pHandle, false, "pHandle is null.");
571         TryReturn(pCertList, false, "pCertPath is null.");
572
573         int res = 0;
574         result r = E_SUCCESS;
575         int certType = 0;
576
577         for (int i = 0; i < pCertList->GetCount(); i++)
578         {
579                 String* pCert = null;
580                 r = pCertList->GetAt(i, pCert);
581                 TryReturn(!IsFailed(r), false, "pCertList->GetAt() is failed, [%d]", i);
582                 TryReturn(pCert, false, "pCertList->GetAt() is failed, [%d]", i);
583
584                 std::unique_ptr<char[]> pCertValue(_StringConverter::CopyToCharArrayN(*pCert));
585
586                 if (i == 0)
587                 {
588                         certType = PM_SET_AUTHOR_SIGNER_CERT;
589                 }
590                 else if (i == 1)
591                 {
592                         certType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
593                 }
594                 else if (i == 2)
595                 {
596                         certType = PM_SET_AUTHOR_ROOT_CERT;
597                 }
598                 else
599                 {
600                         AppLog("Invalid certificate type[%d]", i);
601                         break;
602                 }
603
604                 AppLog("------------------------------------------");
605                 AppLog("Certificate type=[%d]", certType);
606                 AppLog("[%s]", pCertValue.get());
607
608                 res = pkgmgr_installer_set_cert_value(pHandle, (pkgmgr_instcert_type)certType, pCertValue.get());
609                 TryReturn(res == 0, false, "pkgmgr_installer_set_cert_value(%d) failed.[%d]", i, res);
610         }
611
612         return true;
613 }
614
615 bool
616 SignatureManager::SetDistributorCertValue(void* pHandle, IListT<String *>* pCertList, int sigFileNumber) const
617 {
618         TryReturn(pHandle, false, "pHandle is null.");
619         TryReturn(pCertList, false, "pCertPath is null.");
620         TryReturn((sigFileNumber == 1) || (sigFileNumber == 2), false, "sigFileNumber[%d] is invalid.", sigFileNumber);
621
622         int res = 0;
623         result r = E_SUCCESS;
624         int certType = 0;
625
626         for (int i = 0; i < pCertList->GetCount(); i++)
627         {
628                 String* pCert = null;
629                 r = pCertList->GetAt(i, pCert);
630                 TryReturn(!IsFailed(r), false, "pCertList->GetAt() is failed, [%d]", i);
631                 TryReturn(pCert, false, "pCertList->GetAt() is failed, [%d]", i);
632
633                 std::unique_ptr<char[]> pCertValue(_StringConverter::CopyToCharArrayN(*pCert));
634
635                 if (sigFileNumber == 1)
636                 {
637                         if (i == 0)
638                         {
639                                 certType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
640                         }
641                         else if (i == 1)
642                         {
643                                 certType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
644                         }
645                         else if (i == 2)
646                         {
647                                 certType = PM_SET_DISTRIBUTOR_ROOT_CERT;
648                         }
649                         else
650                         {
651                                 AppLog("Invalid certificate type[%d]", i);
652                                 break;
653                         }
654                 }
655                 else if (sigFileNumber == 2)
656                 {
657                         if (pCertList->GetCount() == 1)
658                         {
659                                 certType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
660                         }
661                         else
662                         {
663                                 if (i == 0)
664                                 {
665                                         certType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
666                                 }
667                                 else if (i == 1)
668                                 {
669                                         certType = PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
670                                 }
671                                 else if (i == 2)
672                                 {
673                                         certType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
674                                 }
675                                 else
676                                 {
677                                         AppLog("Invalid certificate type[%d]", i);
678                                         break;
679                                 }
680                         }
681                 }
682
683                 AppLog("------------------------------------------");
684                 AppLog("Certificate type=[%d]", certType);
685                 AppLog("[%s]", pCertValue.get());
686
687                 res = pkgmgr_installer_set_cert_value(pHandle, (pkgmgr_instcert_type)certType, pCertValue.get());
688                 TryReturn(res == 0, false, "pkgmgr_installer_set_cert_value(%d) failed.[%d]", i, res);
689         }
690
691         return true;
692 }
693
694 bool
695 SignatureManager::CompareReferences(HashMap* pDistributorRefMap, HashMap* pAuthorRefMap)
696 {
697         TryReturn(__pContext, false, "__pContext is null.");
698         bool res = false;
699
700         std::unique_ptr< IMapEnumerator > pEnum(pAuthorRefMap->GetMapEnumeratorN());
701         TryReturn(pEnum, false, "pEnum is null.");
702
703         while (pEnum->MoveNext() == E_SUCCESS)
704         {
705                 String* pAuthorKey = static_cast< String* > (pEnum->GetKey());
706                 TryReturn(pAuthorKey, false, "pAuthorKey is null.");
707
708                 String* pAuthorValue = static_cast< String* > (pEnum->GetValue());
709                 TryReturn(pAuthorValue, false, "pAuthorValue is failed.");
710
711                 String* pDistValue = static_cast< String* >(pDistributorRefMap->GetValue(*pAuthorKey));
712                 TryReturn(pDistValue, false, "pDistValue is null, failed to compare.");
713
714                 AppLog("pAuthorValue=[%ls]", pAuthorValue->GetPointer());
715                 AppLog("pDistValue=[%ls]", pDistValue->GetPointer());
716                 if (*pAuthorValue != *pDistValue)
717                 {
718                         TryReturn(0, false, "Digest value does not be matched.");
719                 }
720
721                 String filePath = __pContext->__rootPath + L"/" + *pAuthorKey;
722                 String fileDigest;
723
724                 AppLog("filePath=[%ls]", filePath.GetPointer());
725                 res = InstallerUtil::GetFileDigest(filePath, fileDigest);
726                 TryReturn(res == true, false, "GetFileDigest() is failed.");
727
728                 AppLog("fileDigest=[%ls]", fileDigest.GetPointer());
729                 if (*pAuthorValue != fileDigest)
730                 {
731                         TryReturn(0, false, "Digest value does not be matched.");
732                 }
733         }
734
735         return true;
736 }
737
738 bool
739 SignatureManager::PrintCertValue(const String& certValue) const
740 {
741         const int certLength = 30;
742
743         String startString;
744         String endString;
745         certValue.SubString(0, certLength, startString);
746         certValue.SubString(certValue.GetLength() - certLength, certLength, endString);
747         AppLog("cert value, start with [%ls]", startString.GetPointer());
748         AppLog("cert value, end with [%ls]", endString.GetPointer());
749
750         return true;
751 }