cc3cdd1dd68f07deafb49f274ce6976d9d2edaa5
[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
25 #include <FBase_StringConverter.h>
26 #include <FBaseInternalTypes.h>
27
28 #include "SignatureManager.h"
29
30 #include <vcore/SignatureFinder.h>
31 #include <vcore/SignatureData.h>
32 #include <vcore/SignatureReader.h>
33 #include <vcore/SignatureValidator.h>
34 #include <vcore/WrtSignatureValidator.h>
35 #include <vcore/VCore.h>
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Security::Cert;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Utility;
41
42 SignatureManager::SignatureManager(void)
43 :__pContext(null)
44 ,__pAuthorSignature(null)
45 ,__pDistributorSignature(null)
46 ,__pAuthorCertPath(null)
47 ,__pDistributorCertPath(null)
48 {
49 }
50
51 SignatureManager::~SignatureManager(void)
52 {
53         delete __pAuthorSignature;
54         delete __pDistributorSignature;
55         delete __pAuthorCertPath;
56         delete __pDistributorCertPath;
57 }
58
59 bool
60 SignatureManager::Construct(InstallationContext* pContext)
61 {
62         __pContext = pContext;
63
64         return true;
65 }
66
67 bool
68 SignatureManager::ValidateSignatures()
69 {
70         AppLog("ValidateSignatures start >>");
71
72         TryReturn(__pContext, false, "__pContext is null.");
73
74         bool res = false;
75         res = ValidationCore::VCoreInit(
76                         std::string("/usr/share/wrt-engine/fingerprint_list.xml"),
77                         std::string("/usr/share/wrt-engine/fingerprint_list.xsd"),
78                         std::string("/opt/dbspace/.cert_svc_vcore.db"));
79         TryReturn(res, false, "ValidationCore::VCoreInit() is failed.");
80         TryReturn(__pContext->__rootPath.IsEmpty() == false, false, "__pContext->__rootPath is empty.");
81         AppLog("ValidationCore::VCoreInit() is done");
82
83         std::unique_ptr<char[]> pRootPath(_StringConverter::CopyToCharArrayN(__pContext->__rootPath));
84         TryReturn(pRootPath, false, "__pRootPath is null.");
85
86         std::string rootPath = pRootPath.get();
87         rootPath += "/";
88         AppLog("rootPath=[%s]", rootPath.c_str());
89
90         ValidationCore::SignatureFinder::Result findRes = ValidationCore::SignatureFinder::NO_ERROR;
91         ValidationCore::SignatureFinder signatureFinder(rootPath);
92         ValidationCore::SignatureFileInfoSet signatureFiles;
93
94         findRes = signatureFinder.find(signatureFiles);
95         TryReturn(findRes == ValidationCore::SignatureFinder::NO_ERROR , false, "signatureFinder.find() is failed, findRes=[%d]", (int)findRes);
96         AppLog("signatureFinder.find() is done");
97
98         ValidationCore::SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
99         for (; iter != signatureFiles.rend(); ++iter)
100         {
101                 int sigFileNumber = iter->getFileNumber();
102                 AppLog("SignatureFiles: file=[%s]", iter->getFileName().c_str());
103                 AppLog("SignatureFiles: number=[%d]", sigFileNumber);
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                                                 __pContext->__pDistributorCertList2->Add(new String(value.c_str()));
134                                                 AppLog("Default distributor2 cert value=[%s]", value.c_str());
135                                         }
136                                 }
137                         }
138
139                         ValidationCore::SignatureValidator::Result valRes = ValidationCore::SignatureValidator::SIGNATURE_VALID;
140                         ValidationCore::SignatureValidator::AppType appType = ValidationCore::SignatureValidator::TIZEN;
141                         ValidationCore::SignatureValidator validator(appType, false, false, false);
142
143                         AppLog("validator.check() start >>");
144                         valRes = validator.check(data, rootPath);
145                         TryReturn(valRes == ValidationCore::SignatureValidator::SIGNATURE_VERIFIED, false, "validator.check() is failed, valRes=[%d]", (int)valRes);
146                         AppLog("Signature validator.check success, file=[%s], number=[%d]", iter->getFileName().c_str(), iter->getFileNumber());
147
148                         ValidationCore::CertificatePtr pRootCert = data.getRootCaCertificatePtr();
149                         TryReturn(pRootCert, false, "__pRootPath is null.");
150                         std::string value = pRootCert.Get()->getBase64();
151                         if (data.isAuthorSignature() == true)
152                         {
153                                 __pContext->__pAuthorCertList->Add(new String(value.c_str()));
154                                 AppLog("Author root cert value=[%s]", value.c_str());
155                         }
156                         else
157                         {
158                                 if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR)
159                                 {
160                                         __pContext->__pDistributorCertList->Add(new String(value.c_str()));
161                                         AppLog("Default root distributor cert value=[%s]", value.c_str());
162
163                                         ValidationCore::CertStoreId::Type certStoreIdType = data.getVisibilityLevel();
164                                         AppLog("CertStoreIdType = [%d]", (int)certStoreIdType);
165                                         __pContext->__rootCertType = (RootCertificateType)certStoreIdType;
166                                 }
167                                 else if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
168                                 {
169                                         __pContext->__pDistributorCertList2->Add(new String(value.c_str()));
170                                         AppLog("Default root distributor2 cert value=[%s]", value.c_str());
171                                 }
172                         }
173                 }
174                 catch (ValidationCore::ParserSchemaException::Base)
175                 {
176                         TryReturn(0, false, "ParserSchemaException::Base exception occurred.");
177                 }
178                 catch (DPL::Exception)
179                 {
180                         TryReturn(0, false, "DPL::Exception exception occurred.");
181                 }
182         }
183
184         AppLog("ValidateSignatures done successfully <<");
185         return true;
186 }
187
188 bool
189 SignatureManager::ValidatePartialReferences()
190 {
191         AppLog("ValidatePartialReferences start >>");
192
193         bool res = true;
194         std::unique_ptr<char[]> pSignaturePath(_StringConverter::CopyToCharArrayN(__pContext->GetSignatureXmlPath()));
195         TryReturn(pSignaturePath, false, "pSignaturePath is null.");
196
197         std::unique_ptr<char[]> pAuthorSignaturePath(_StringConverter::CopyToCharArrayN(__pContext->GetAuthorSignatureXmlPath()));
198         TryReturn(pAuthorSignaturePath, false, "pAuthorSignaturePath is null.");
199
200         std::unique_ptr<SignatureHandler> pDistributorSignature(new (std::nothrow) SignatureHandler());
201         TryReturn(pDistributorSignature, false, "pDistributorSignature is null.");
202
203         res = pDistributorSignature->Construct(__pContext);
204         TryReturn(res == true, false, "pDistributorSignature->Construct() is failed.");
205
206         res = pDistributorSignature->Parse(pSignaturePath.get());
207         TryReturn(res == true, false, "pDistributorSignature->Parse() is failed.");
208
209         HashMap* pDistributorRefMap = pDistributorSignature->GetReferenceMap();
210         TryReturn(pDistributorRefMap, false, "pDistributorRefMap is null.");
211
212         std::unique_ptr<SignatureHandler> pAuthorSignature(new (std::nothrow) SignatureHandler());
213         TryReturn(pAuthorSignature, false, "pAuthorSignature is null.");
214
215         res = pAuthorSignature->Construct(__pContext);
216         TryReturn(res == true, false, "pAuthorSignature.Construct() is failed.");
217
218         res = pAuthorSignature->Parse(pAuthorSignaturePath.get());
219         TryReturn(res == true, false, "pAuthorSignature->Parse() is failed.");
220
221         HashMap* pAuthorRefMap = pAuthorSignature->GetReferenceMap();
222         TryReturn(pAuthorRefMap, false, "pAuthorRefMap is null.");
223
224         res = CompareReferences(pDistributorRefMap, pAuthorRefMap);
225         TryReturn(res == true, false, "CompareReferences() is failed.");
226
227         return true;
228 }
229
230 bool
231 SignatureManager::RegisterCertInfo() const
232 {
233         TryReturn(__pContext, false, "__pContext is null");
234
235         if (__pContext->__isHybridService == true)
236         {
237                 AppLog("Skip - HybridService");
238                 return true;
239         }
240
241         AppLog("START");
242
243         int error = 0;
244         bool res = false;
245         pkgmgr_instcertinfo_h handle = null;
246
247         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
248         TryReturn(pPackageId, false, "pPackageId is null");
249
250         error = pkgmgr_installer_create_certinfo_set_handle(&handle);
251         TryReturn(error == 0, false, "pkgmgr_installer_create_certinfo_set_handle() failed, error = [%d].", error);
252
253         if (__pContext->__pAuthorCertList)
254         {
255                 AppLog("[AuthorCert]");
256                 res = SetAuthorCertValue(handle, __pContext->__pAuthorCertList);
257                 TryCatch(res == true, , "SetAuthorCertHashValue() is failed.");
258         }
259
260         if (__pContext->__pDistributorCertList)
261         {
262                 AppLog("[DistributorCert]");
263                 res = SetDistributorCertValue(handle, __pContext->__pDistributorCertList, 1);
264                 TryCatch(res == true, , "SetDistributorCertValue() is failed.");
265         }
266
267         if (__pContext->__pDistributorCertList2)
268         {
269                 AppLog("[DistributorCert2]");
270                 res = SetDistributorCertValue(handle, __pContext->__pDistributorCertList2, 2);
271                 TryCatch(res == true, , "SetDistributorCertValue() is failed.");
272         }
273
274         error = pkgmgr_installer_save_certinfo(pPackageId.get(), handle);
275         TryCatch(error == 0, res = false, "pkgmgr_installer_save_certinfo(%s) failed, error = [%d]", pPackageId.get(), error);
276
277         AppLog("RegisterCertInfo - END");
278         AppLog("------------------------------------------");
279
280         res = true;
281
282 CATCH:
283         if (handle)
284         {
285                 pkgmgr_installer_destroy_certinfo_set_handle(handle);
286         }
287
288         return res;
289 }
290
291 bool
292 SignatureManager::UnregisterCertInfo() const
293 {
294         TryReturn(__pContext, false, "__pContext is null");
295
296         if (__pContext->__isHybridService == true)
297         {
298                 AppLog("Skip - HybridService");
299                 return true;
300         }
301
302         AppLog("START");
303         int error = 0;
304         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
305         TryReturn(pPackageId, false, "pPackageId is null");
306
307         error = pkgmgr_installer_delete_certinfo(pPackageId.get());
308         TryReturn(error == 0, false, "pkgmgr_installer_delete_certinfo(%s) failed, error = [%d]", pPackageId.get(), error);
309
310         AppLog("UnregisterCertInfo - END");
311         AppLog("------------------------------------------");
312
313         return true;
314 }
315
316 int
317 SignatureManager::GetApiVisibility(RootCertificateType certType)
318 {
319         int apiVisibility = _API_VISIBILITY_NONE;
320
321         if (certType == ROOT_CERTIFICATE_PUBLIC)
322         {
323                 apiVisibility = _API_VISIBILITY_PUBLIC;
324         }
325         else if (certType == ROOT_CERTIFICATE_PARTNER)
326         {
327                 apiVisibility = _API_VISIBILITY_PARTNER;
328         }
329         else if (certType == ROOT_CERTIFICATE_PARTNER_OPERATOR)
330         {
331                 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
332         }
333         else if (certType == ROOT_CERTIFICATE_PARTNER_MANUFACTURER)
334         {
335                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
336         }
337         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PUBLIC)
338         {
339                 apiVisibility = _API_VISIBILITY_PUBLIC;
340         }
341         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER)
342         {
343                 apiVisibility = _API_VISIBILITY_PARTNER;
344         }
345         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER_OPERATOR)
346         {
347                 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
348         }
349         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER_MANUFACTURER)
350         {
351                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
352         }
353         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PLATFORM)
354         {
355                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
356         }
357
358         return apiVisibility;
359 }
360
361 int
362 SignatureManager::GetPrivilegeLevel(int apiVisibility)
363 {
364         if (apiVisibility == _API_VISIBILITY_PARTNER_MANUFACTURER || apiVisibility == _API_VISIBILITY_PARTNER_OPERATOR)
365         {
366                 return PRIVILEGE_LEVEL_PLATFORM;
367         }
368         else if (apiVisibility == _API_VISIBILITY_PARTNER)
369         {
370                 return PRIVILEGE_LEVEL_PARTNER;
371         }
372
373         return PRIVILEGE_LEVEL_PUBLIC;
374 }
375
376 bool
377 SignatureManager::SetSignature()
378 {
379         TryReturn(__pContext, false, "__pContext is null.");
380
381         bool ret = true;
382         char* pSignaturePath = _StringConverter::CopyToCharArrayN(__pContext->GetSignatureXmlPath());
383         char* pAuthorSignaturePath = _StringConverter::CopyToCharArrayN(__pContext->GetAuthorSignatureXmlPath());
384
385         __pDistributorSignature = new (std::nothrow) SignatureHandler;
386         TryCatch(__pDistributorSignature, ret = false, "__pDistributorSignature is null");
387
388         ret = __pDistributorSignature->Construct(__pContext);
389         TryCatch(ret == true, ret = false, "__pDistributorSignature->Construct is failed.");
390
391         ret = __pDistributorSignature->Parse(pSignaturePath);
392         TryCatch(ret == true, ret = false, "__pDistributorSignature->Parse is failed.");
393
394         __pAuthorSignature = new (std::nothrow) SignatureHandler;
395         TryCatch(__pAuthorSignature, ret = false, "__pAuthorSignature is null");
396
397         ret = __pAuthorSignature->Construct(__pContext);
398         TryCatch(ret == true, ret = false, "__pAuthorSignature->Construct is failed.");
399
400         ret = __pAuthorSignature->Parse(pAuthorSignaturePath);
401         TryCatch(ret == true, ret = false, "__pAuthorSignature->Parse is failed.");
402
403 CATCH:
404         delete[] pSignaturePath;
405         delete[] pAuthorSignaturePath;
406         return ret;
407 }
408
409 bool
410 SignatureManager::AddCert()
411 {
412         TryReturn(__pAuthorSignature, false, "__pAuthorSignature is null.");
413         TryReturn(__pDistributorSignature, false, "__pDistributorSignature is null.");
414
415         bool ret = true;
416         IList* pAuthorCertChain = __pAuthorSignature->GetAuthorCertChain();
417         IList* pDistributorCertChain = __pDistributorSignature->GetDistributorCertChain();
418
419         if (pAuthorCertChain)
420         {
421                 AppLog("AddCertificate - AuthorCertChain");
422
423                 __pAuthorCertPath = new (std::nothrow) X509CertificatePath();
424                 TryCatch(__pAuthorCertPath, ret = false, "__pAuthorCertPath is null.");
425
426                 ret = AddCertificate(__pAuthorCertPath, pAuthorCertChain);
427                 TryCatch(ret == true, ret = false, "AddCertificate(AuthorCert) is failed.");
428
429                 ret = AddAuthorRootCert(__pAuthorCertPath);
430                 TryCatch(ret == true, ret = false, "AddAuthorRootCert(AuthorCertPath) is failed.");
431         }
432
433         if (pDistributorCertChain)
434         {
435                 AppLog("AddCertificate - DistributorCert");
436
437                 __pDistributorCertPath = new (std::nothrow) X509CertificatePath();
438                 TryCatch(__pDistributorCertPath, ret = false, "__pDistributorCertPath is null.");
439
440                 ret = AddCertificate(__pDistributorCertPath, pDistributorCertChain);
441                 TryCatch(ret == true, ret = false, "AddCertificate(DistributorCert) is failed.");
442
443                 ret = AddDistributorRootCert(__pDistributorCertPath);
444                 TryCatch(ret == true, ret = false, "AddDistributorRootCert(DistributorCert) is failed.");
445         }
446
447 CATCH:
448         return ret;
449 }
450
451 bool
452 SignatureManager::VerifyChain()
453 {
454         TryReturn(__pAuthorCertPath, false, "__pAuthorCertPath is null.");
455         TryReturn(__pDistributorCertPath, false, "__pDistributorCertPath is null.");
456
457         bool ret = true;
458
459         AppLog("AuthorCert Validate - START");
460         ret = Validate(__pAuthorCertPath);
461         AppLog("AuthorCert Validate - END");
462         TryCatch(ret == true, ret = false, "Validate(AuthorCert) is failed.");
463
464         SetCertificatePath(SIGNATURE_FILE_TYPE_AUTHOR, __pAuthorCertPath);
465         __pAuthorCertPath = null;
466
467         AppLog("DistributorCert Validate - START");
468         ret = Validate(__pDistributorCertPath);
469         AppLog("DistributorCert Validate - END");
470         TryCatch(ret == true, ret = false, "Validate(DistributorCert) is failed.");
471
472         SetCertificatePath(SIGNATURE_FILE_TYPE_DISTRIBUTOR, __pDistributorCertPath);
473         __pDistributorCertPath = null;
474
475 CATCH:
476         delete __pAuthorCertPath;
477         __pAuthorCertPath = null;
478         delete __pDistributorCertPath;
479         __pDistributorCertPath = null;
480
481         return ret;
482 }
483
484 bool
485 SignatureManager::SetAuthorCertValue(void* pHandle, IListT<String *>* pCertList) const
486 {
487         TryReturn(pHandle, false, "pHandle is null.");
488         TryReturn(pCertList, false, "pCertPath is null.");
489
490         int res = 0;
491         result r = E_SUCCESS;
492         int certType = 0;
493
494         for (int i = 0; i < pCertList->GetCount(); i++)
495         {
496                 String* pCert = null;
497                 r = pCertList->GetAt(i, pCert);
498                 TryReturn(!IsFailed(r), false, "pCertList->GetAt() is failed, [%d]", i);
499                 TryReturn(pCert, false, "pCertList->GetAt() is failed, [%d]", i);
500
501                 std::unique_ptr<char[]> pCertValue(_StringConverter::CopyToCharArrayN(*pCert));
502
503                 if (i == 0)
504                 {
505                         certType = PM_SET_AUTHOR_SIGNER_CERT;
506                 }
507                 else if (i == 1)
508                 {
509                         certType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
510                 }
511                 else if (i == 2)
512                 {
513                         certType = PM_SET_AUTHOR_ROOT_CERT;
514                 }
515                 else
516                 {
517                         AppLog("Invalid certificate type[%d]", i);
518                         break;
519                 }
520
521                 AppLog("------------------------------------------");
522                 AppLog("Certificate type=[%d]", certType);
523                 AppLog("[%s]", pCertValue.get());
524
525                 res = pkgmgr_installer_set_cert_value(pHandle, (pkgmgr_instcert_type)certType, pCertValue.get());
526                 TryReturn(res == 0, false, "pkgmgr_installer_set_cert_value(%d) failed.[%d]", i, res);
527         }
528
529         return true;
530 }
531
532 bool
533 SignatureManager::SetDistributorCertValue(void* pHandle, IListT<String *>* pCertList, int sigFileNumber) const
534 {
535         TryReturn(pHandle, false, "pHandle is null.");
536         TryReturn(pCertList, false, "pCertPath is null.");
537         TryReturn((sigFileNumber == 1) || (sigFileNumber == 2), false, "sigFileNumber[%d] is invalid.", sigFileNumber);
538
539         int res = 0;
540         result r = E_SUCCESS;
541         int certType = 0;
542
543         for (int i = 0; i < pCertList->GetCount(); i++)
544         {
545                 String* pCert = null;
546                 r = pCertList->GetAt(i, pCert);
547                 TryReturn(!IsFailed(r), false, "pCertList->GetAt() is failed, [%d]", i);
548                 TryReturn(pCert, false, "pCertList->GetAt() is failed, [%d]", i);
549
550                 std::unique_ptr<char[]> pCertValue(_StringConverter::CopyToCharArrayN(*pCert));
551
552                 if (sigFileNumber == 1)
553                 {
554                         if (i == 0)
555                         {
556                                 certType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
557                         }
558                         else if (i == 1)
559                         {
560                                 certType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
561                         }
562                         else if (i == 2)
563                         {
564                                 certType = PM_SET_DISTRIBUTOR_ROOT_CERT;
565                         }
566                         else
567                         {
568                                 AppLog("Invalid certificate type[%d]", i);
569                                 break;
570                         }
571                 }
572                 else if (sigFileNumber == 2)
573                 {
574                         if (i == 0)
575                         {
576                                 certType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
577                         }
578                         else if (i == 1)
579                         {
580                                 certType = PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
581                         }
582                         else if (i == 2)
583                         {
584                                 certType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
585                         }
586                         else
587                         {
588                                 AppLog("Invalid certificate type[%d]", i);
589                                 break;
590                         }
591                 }
592
593                 AppLog("------------------------------------------");
594                 AppLog("Certificate type=[%d]", certType);
595                 AppLog("[%s]", pCertValue.get());
596
597                 res = pkgmgr_installer_set_cert_value(pHandle, (pkgmgr_instcert_type)certType, pCertValue.get());
598                 TryReturn(res == 0, false, "pkgmgr_installer_set_cert_value(%d) failed.[%d]", i, res);
599         }
600
601         return true;
602 }
603
604 bool
605 SignatureManager::SetCertificatePath(SignatureFileType type, X509CertificatePath* pCertPath)
606 {
607         TryReturn(pCertPath, false, "pCertPath is null.");
608         TryReturn(__pContext, false, "__pContext is null.");
609
610         IListT<String *>* pList = null;
611
612         if (type == SIGNATURE_FILE_TYPE_AUTHOR)
613         {
614                 pList = __pContext->__pAuthorCertList;
615         }
616         else if (type == SIGNATURE_FILE_TYPE_DISTRIBUTOR)
617         {
618                 pList = __pContext->__pDistributorCertList;
619         }
620         else if (type == SIGNATURE_FILE_TYPE_DISTRIBUTOR2)
621         {
622                 pList = __pContext->__pDistributorCertList2;
623         }
624         else
625         {
626                 TryReturn(0, false, "SignatureFileType is unknown, type = [%d]", (int)type);
627         }
628
629         for (int i = 0; i < pCertPath->GetLength(); i++)
630         {
631                 std::unique_ptr<ICertificate> pCert(pCertPath->GetCertificateN(i));
632                 TryReturn(pCert, false, "pCert is null, [%i].", i);
633
634                 bool res = false;
635                 String certValue;
636                 res = GetCertValue(pCert.get(), certValue);
637                 TryReturn(res == true, false, "GetCertValue() is failed, [%d].", i);
638
639                 result r = pList->Add(new String(certValue));
640                 TryReturn(!IsFailed(r), false, "pList->Add() is failed, [%d]", i);
641
642                 AppLog("------------------------------------------");
643                 AppLog("CertValue[%d], certType[%d]", i, (int)type);
644                 AppLog("[%ls]", certValue.GetPointer());
645         }
646
647         return true;
648 }
649
650 bool
651 SignatureManager::GetCertValue(ICertificate* pCert, String& value) const
652 {
653         result r = E_SUCCESS;
654
655         std::unique_ptr<ByteBuffer> pEncodedData(pCert->GetEncodedDataN());
656         TryReturn(pEncodedData, false, "pEncodedData is null.");
657
658         r = StringUtil::EncodeToBase64String(*pEncodedData.get(), value);
659         TryReturn(!IsFailed(r), false, "StringUtil::EncodeToBase64String() is failed.");
660
661         return true;
662 }
663
664 bool
665 SignatureManager::CompareReferences(HashMap* pDistributorRefMap, HashMap* pAuthorRefMap)
666 {
667         TryReturn(__pContext, false, "__pContext is null.");
668         bool res = false;
669
670         std::unique_ptr< IMapEnumerator > pEnum(pAuthorRefMap->GetMapEnumeratorN());
671         TryReturn(pEnum, false, "pEnum is null.");
672
673         while (pEnum->MoveNext() == E_SUCCESS)
674         {
675                 String* pAuthorKey = static_cast< String* > (pEnum->GetKey());
676                 TryReturn(pAuthorKey, false, "pAuthorKey is null.");
677
678                 String* pAuthorValue = static_cast< String* > (pEnum->GetValue());
679                 TryReturn(pAuthorValue, false, "pAuthorValue is failed.");
680
681                 String* pDistValue = static_cast< String* >(pDistributorRefMap->GetValue(*pAuthorKey));
682                 TryReturn(pDistValue, false, "pDistValue is null, failed to compare.");
683
684                 AppLog("pAuthorValue=[%ls]", pAuthorValue->GetPointer());
685                 AppLog("pDistValue=[%ls]", pDistValue->GetPointer());
686                 if (*pAuthorValue != *pDistValue)
687                 {
688                         TryReturn(0, false, "Digest value does not be matched.");
689                 }
690
691                 String filePath = __pContext->__rootPath + L"/" + *pAuthorKey;
692                 String fileDigest;
693
694                 AppLog("filePath=[%ls]", filePath.GetPointer());
695                 res = InstallerUtil::GetFileDigest(filePath, fileDigest);
696                 TryReturn(res == true, false, "GetFileDigest() is failed.");
697
698                 AppLog("fileDigest=[%ls]", fileDigest.GetPointer());
699                 if (*pAuthorValue != fileDigest)
700                 {
701                         TryReturn(0, false, "Digest value does not be matched.");
702                 }
703         }
704
705         return true;
706 }
707
708 bool
709 SignatureManager::PrintCertValue(const String& certValue) const
710 {
711         const int certLength = 30;
712
713         String startString;
714         String endString;
715         certValue.SubString(0, certLength, startString);
716         certValue.SubString(certValue.GetLength() - certLength, certLength, endString);
717         AppLog("cert value, start with [%ls]", startString.GetPointer());
718         AppLog("cert value, end with [%ls]", endString.GetPointer());
719
720         return true;
721 }
722
723 bool
724 SignatureManager::Validate(X509CertificatePath* pCertPath)
725 {
726         TryReturn(pCertPath, false, "pCertPath is null.");
727
728         AppLog("------------------------------------------");
729         AppLog("# signature.xml");
730         ValidationResult valResult = VALIDATION_SUCCESS;
731         valResult = pCertPath->Validate();
732
733         if (valResult != VALIDATION_SUCCESS)
734         {
735                 AppLog("Validate() fail! - ValidationResult = [%d]", valResult);
736                 AppLog("------------------------------------------");
737                 return false;
738         }
739         else
740         {
741                 int depth = pCertPath->GetLength();
742                 if (depth == 0)
743                 {
744                         AppLog("depth = 0");
745                         return false;
746                 }
747
748                 AppLog("Validate() success!");
749                 AppLog("------------------------------------------");
750         }
751
752         return true;
753 }
754
755 bool
756 SignatureManager::AddCertificate(X509CertificatePath* pCertPath, IList* pCertChain)
757 {
758         TryReturn(pCertChain, false, "pCertChain is null.");
759
760         bool ret = true;
761         result r = E_SUCCESS;
762         X509Certificate* pCertificate = null;
763
764         for (int i = 0; i < pCertChain->GetCount(); i++)
765         {
766                 Tizen::Base::ByteBuffer* pByteBuffer = dynamic_cast <ByteBuffer*>(pCertChain->GetAt(i));
767
768                 if (pByteBuffer)
769                 {
770                         AppLog("[cert][%d]", i);
771
772                         pCertificate = new (std::nothrow) X509Certificate;
773                         TryCatch(pCertificate, ret = false, "pCertificate is null.");
774
775                         r = pCertificate->Construct(*pByteBuffer);
776                         TryCatch(!IsFailed(r), ret = false, "pCertificate->Construct() is failed.");
777
778                         r = pCertPath->AddCertificate(*pCertificate);
779                         TryCatch(!IsFailed(r), ret = false, "AddCertificate is failed.");
780
781                         delete pCertificate;
782                         pCertificate = null;
783                 }
784         }
785
786 CATCH:
787         delete pCertificate;
788         return ret;
789 }
790
791 bool
792 SignatureManager::AddAuthorRootCert(X509CertificatePath* pCertPath)
793 {
794         TryReturn(pCertPath, false, "pCertPath is null.");
795
796         result r = E_SUCCESS;
797         bool ret = true;
798         ByteBuffer byteBuffer;
799         X509Certificate rootCert;
800         int length = 0;
801         const char* pAuthorRootCert = "MIICnzCCAggCCQCn+GGT4zh+BjANBgkqhkiG9w0BAQUFADCBkzELMAkGA1UEBhMC"
802                                                                                                                                 "S1IxDjAMBgNVBAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UECgwNVGl6"
803                                                                                                                                 "ZW4gVGVzdCBDQTElMCMGA1UECwwcVGl6ZW4gVGVzdCBEZXZlbG9wZXIgUm9vdCBD"
804                                                                                                                                 "QTElMCMGA1UEAwwcVGl6ZW4gVGVzdCBEZXZlbG9wZXIgUm9vdCBDQTAeFw0xMjEw"
805                                                                                                                                 "MjYwOTUwMTNaFw0yMjEwMjQwOTUwMTNaMIGTMQswCQYDVQQGEwJLUjEOMAwGA1UE"
806                                                                                                                                 "CAwFU3V3b24xDjAMBgNVBAcMBVN1d29uMRYwFAYDVQQKDA1UaXplbiBUZXN0IENB"
807                                                                                                                                 "MSUwIwYDVQQLDBxUaXplbiBUZXN0IERldmVsb3BlciBSb290IENBMSUwIwYDVQQD"
808                                                                                                                                 "DBxUaXplbiBUZXN0IERldmVsb3BlciBSb290IENBMIGfMA0GCSqGSIb3DQEBAQUA"
809                                                                                                                                 "A4GNADCBiQKBgQDWT6ZH5JyGadTUK1QmNwU8j+py4WtuElJE+4/wPFP8/KBmvvmI"
810                                                                                                                                 "rGVjhUbKXToKIo8N6C/0SLxGEWuRAIoZHhg5JVbw1Ay7smgJJHizDUAqMTmV6LI9"
811                                                                                                                                 "yTFbBV+OlO2Dir4LVdQ/XDBiqqslr7pqXgsg1V2g7x+tOI/f3dn2kWoVZQIDAQAB"
812                                                                                                                                 "MA0GCSqGSIb3DQEBBQUAA4GBADGJYMtzUBDK+KKLZQ6zYmrKb+OWLlmEr/t/c2af"
813                                                                                                                                 "KjTKUtommcz8VeTPqrDBOwxlVPdxlbhisCYzzvwnWeZk1aeptxxU3kdW9N3/wocN"
814                                                                                                                                 "5nBzgqkkHJnj/ptqjrH2v/m0Z3hBuI4/akHIIfCBF8mUHwqcxYsRdcCIrkgp2Aiv"
815                                                                                                                                 "bSaM";
816
817         length = strlen(pAuthorRootCert);
818         byteBuffer.Construct(length);
819
820         r = byteBuffer.SetArray((byte*)pAuthorRootCert, 0, length);
821         TryCatch(!IsFailed(r), ret = false, "SetArray() is failed.");
822
823         byteBuffer.Flip();
824
825         r = rootCert.Construct(byteBuffer);
826         TryCatch(!IsFailed(r), ret = false, "rootCert.Construct() is failed.");
827
828         r = pCertPath->AddCertificate(rootCert);
829         TryCatch(!IsFailed(r), ret = false, "AddCertificate(AuthorRootCert) is failed.");
830
831 CATCH:
832         return ret;
833 }
834
835 bool
836 SignatureManager::AddDistributorRootCert(X509CertificatePath* pCertPath)
837 {
838         TryReturn(pCertPath, false, "pCertPath is null.");
839
840         result r = E_SUCCESS;
841         bool ret = true;
842         ICertificate* pIntermediateCA = null;
843         String issuer;
844
845         pIntermediateCA = pCertPath->GetCertificateN(1);
846         TryCatch(pIntermediateCA, ret = false, "pIntermediateCA is null.");
847
848         issuer = pIntermediateCA->GetIssuer();
849
850         for(int certType = ROOT_CERTIFICATE_PUBLIC; certType <= ROOT_CERTIFICATE_PARTNER_MANUFACTURER; certType++)
851         {
852                 const char* pRootCert = null;
853                 ByteBuffer byteBuffer;
854                 X509Certificate rootCert;
855                 int length = 0;
856
857                 if (certType == ROOT_CERTIFICATE_PUBLIC)
858                 {
859                         pRootCert = "MIICozCCAgwCCQD9XW6kNg4bbjANBgkqhkiG9w0BAQUFADCBlTELMAkGA1UEBhMC"
860                                                 "S1IxDjAMBgNVBAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UECgwNVGl6"
861                                                 "ZW4gVGVzdCBDQTEjMCEGA1UECwwaVFRpemVuIERpc3RyaWJ1dG9yIFRlc3QgQ0Ex"
862                                                 "KTAnBgNVBAMMIFRpemVuIFB1YmxpYyBEaXN0cmlidXRvciBSb290IENBMB4XDTEy"
863                                                 "MTAyNjA4MDAyN1oXDTIyMTAyNDA4MDAyN1owgZUxCzAJBgNVBAYTAktSMQ4wDAYD"
864                                                 "VQQIDAVTdXdvbjEOMAwGA1UEBwwFU3V3b24xFjAUBgNVBAoMDVRpemVuIFRlc3Qg"
865                                                 "Q0ExIzAhBgNVBAsMGlRUaXplbiBEaXN0cmlidXRvciBUZXN0IENBMSkwJwYDVQQD"
866                                                 "DCBUaXplbiBQdWJsaWMgRGlzdHJpYnV0b3IgUm9vdCBDQTCBnzANBgkqhkiG9w0B"
867                                                 "AQEFAAOBjQAwgYkCgYEA8o0kPY1U9El1BbBUF1k4jCq6mH8a6MmDJdjgsz+hILAY"
868                                                 "sPWimRTXUcW8GAUWhZWgm1Fbb49xWcasA8b4bIJabC/6hLb8uWiozzpRXyQJbe7k"
869                                                 "//RocskRqDmFOky8ANFsCCww72/Xbq8BFK1sxlGdmOWQiGwDWBDlS2Lw1XOMqb0C"
870                                                 "AwEAATANBgkqhkiG9w0BAQUFAAOBgQBUotZqTNFr+SNyqeZqhOToRsg3ojN1VJUa"
871                                                 "07qdlVo5I1UObSE+UTJPJ0NtSj7OyTY7fF3E4xzUv/w8aUoabQP1erEmztY/AVD+"
872                                                 "phHaPytkZ/Dx+zDZ1u5e9bKm5zfY4dQs/A53zDQta5a/NkZOEF97Dj3+bzAh2bP7"
873                                                 "KOszlocFYw==";
874                 }
875                 else if (certType == ROOT_CERTIFICATE_PARTNER)
876                 {
877                         pRootCert = "MIICozCCAgwCCQD9IBoOxzq2hjANBgkqhkiG9w0BAQUFADCBlTELMAkGA1UEBhMC"
878                                                 "S1IxDjAMBgNVBAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UECgwNVGl6"
879                                                 "ZW4gVGVzdCBDQTEiMCAGA1UECwwZVGl6ZW4gRGlzdHJpYnV0b3IgVGVzdCBDQTEq"
880                                                 "MCgGA1UEAwwhVGl6ZW4gUGFydG5lciBEaXN0cmlidXRvciBSb290IENBMB4XDTEy"
881                                                 "MTAyNjA4MTIzMVoXDTIyMTAyNDA4MTIzMVowgZUxCzAJBgNVBAYTAktSMQ4wDAYD"
882                                                 "VQQIDAVTdXdvbjEOMAwGA1UEBwwFU3V3b24xFjAUBgNVBAoMDVRpemVuIFRlc3Qg"
883                                                 "Q0ExIjAgBgNVBAsMGVRpemVuIERpc3RyaWJ1dG9yIFRlc3QgQ0ExKjAoBgNVBAMM"
884                                                 "IVRpemVuIFBhcnRuZXIgRGlzdHJpYnV0b3IgUm9vdCBDQTCBnzANBgkqhkiG9w0B"
885                                                 "AQEFAAOBjQAwgYkCgYEAnIBA2qQEaMzGalP0kzvwUxdCC6ybSC/fb+M9iGvt8QXp"
886                                                 "ic2yARQB+bIhfbEu1XHwE1jCAGxKd6uT91b4FWr04YwnBPoRX4rBGIYlqo/dg+pS"
887                                                 "rGyFjy7vfr0BOdWp2+WPlTe7SOS6bVauncrSoHxX0spiLaU5LU686BKr7YaABV0C"
888                                                 "AwEAATANBgkqhkiG9w0BAQUFAAOBgQAX0Tcfmxcs1TUPBdr1U1dx/W/6Y4PcAF7n"
889                                                 "DnMrR0ZNRPgeSCiVLax1bkHxcvW74WchdKIb24ZtAsFwyrsmUCRV842YHdfddjo6"
890                                                 "xgUu7B8n7hQeV3EADh6ft/lE8nalzAl9tALTxAmLtYvEYA7thvDoKi1k7bN48izL"
891                                                 "gS9G4WEAUg==";
892                 }
893                 else if (certType == ROOT_CERTIFICATE_PARTNER_OPERATOR)
894                 {
895                         pRootCert = "MIICzDCCAjWgAwIBAgIJAJrv22F9wyp/MA0GCSqGSIb3DQEBBQUAMIGeMQswCQYD"
896                                                                         "VQQGEwJLUjEOMAwGA1UECAwFU3V3b24xDjAMBgNVBAcMBVN1d29uMRYwFAYDVQQK"
897                                                                         "DA1UaXplbiBUZXN0IENBMSIwIAYDVQQLDBlUaXplbiBEaXN0cmlidXRvciBUZXN0"
898                                                                         "IENBMTMwMQYDVQQDDCpUaXplbiBQYXJ0bmVyLU9wZXJhdG9yIERpc3RyaWJ1dG9y"
899                                                                         "IFJvb3QgQ0EwHhcNMTIxMjEzMDUzOTMyWhcNMjIxMjExMDUzOTMyWjCBnjELMAkG"
900                                                                         "A1UEBhMCS1IxDjAMBgNVBAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UE"
901                                                                         "CgwNVGl6ZW4gVGVzdCBDQTEiMCAGA1UECwwZVGl6ZW4gRGlzdHJpYnV0b3IgVGVz"
902                                                                         "dCBDQTEzMDEGA1UEAwwqVGl6ZW4gUGFydG5lci1PcGVyYXRvciBEaXN0cmlidXRv"
903                                                                         "ciBSb290IENBMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9X0Hw0EfAuagg"
904                                                                         "De9h6Jtvh8Df4fyVbvLm9VNea/iVP3/qTbG8tNqoQ32lu0SwzAZBnjpvpbxzsWs9"
905                                                                         "pSYo7Ys1fymHlu+gf+kmTGTVscBrAHWkr4O0m33x2FYfy/wmu+IImnRDYDud83rN"
906                                                                         "tjQmMO6BihN9Lb6kLiEtVIa8ITwdQwIDAQABoxAwDjAMBgNVHRMEBTADAQH/MA0G"
907                                                                         "CSqGSIb3DQEBBQUAA4GBAHS2M2UnfEsZf80/sT84xTcfASXgpFL/1M5HiAVpR+1O"
908                                                                         "UwLpLyqHiGQaASuADDeGEfcIqEf8gP1SzvnAZqLx9GchbOrOKRleooVFH7PRxFBS"
909                                                                         "VWJ5Fq46dJ1mCgTWSkrL6dN5j9hWCzzGfv0Wco+NAf61n9kVbCv7AScIJwQNltOy";
910                 }
911                 else // ROOT_CERTIFICATE_PARTNER_MANUFACTURER
912                 {
913                         pRootCert = "MIIC1DCCAj2gAwIBAgIJAJZH47dCtgPdMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD"
914                                                                         "VQQGEwJLUjEOMAwGA1UECAwFU3V3b24xDjAMBgNVBAcMBVN1d29uMRYwFAYDVQQK"
915                                                                         "DA1UaXplbiBUZXN0IENBMSIwIAYDVQQLDBlUaXplbiBEaXN0cmlidXRvciBUZXN0"
916                                                                         "IENBMTcwNQYDVQQDDC5UaXplbiBQYXJ0bmVyLU1hbnVmYWN0dXJlciBEaXN0cmli"
917                                                                         "dXRvciBSb290IENBMB4XDTEyMTIxMzA1NDQxN1oXDTIyMTIxMTA1NDQxN1owgaIx"
918                                                                         "CzAJBgNVBAYTAktSMQ4wDAYDVQQIDAVTdXdvbjEOMAwGA1UEBwwFU3V3b24xFjAU"
919                                                                         "BgNVBAoMDVRpemVuIFRlc3QgQ0ExIjAgBgNVBAsMGVRpemVuIERpc3RyaWJ1dG9y"
920                                                                         "IFRlc3QgQ0ExNzA1BgNVBAMMLlRpemVuIFBhcnRuZXItTWFudWZhY3R1cmVyIERp"
921                                                                         "c3RyaWJ1dG9yIFJvb3QgQ0EwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMJG"
922                                                                         "0gq3XrDn7W7cIH58w7hSDMVENnXLmXm4Jl5teXXrgL/BgpORracGMgm0Fmxxq/Nq"
923                                                                         "8OEI2RfmtrlN5nWGiphs3XqLHtO+BAPY1BbZS6YVZjrVXrGWdzk12zQxd6sXJMiV"
924                                                                         "B08ECQiQ0qgKFbTDSEbH/p4eyKCMG9lnrBLPHTpJAgMBAAGjEDAOMAwGA1UdEwQF"
925                                                                         "MAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAJTJYCr9GhZ1xXwvxsWLGd9XV9wixo1zk"
926                                                                         "FV2+eJZbCc4xJyOSb0cZds8fYUuzw32oyElLvPhYfAHVTHu/WlkwSshZlKdI2hCT"
927                                                                         "Iy03/Up+JNfuom8JLgF7qc3YtbuJHzoVu1jJ/akXU6y52D/J5CkYy2JSsV0KZuh2"
928                                                                         "ZeRWlV2f1Uo=";
929                 }
930
931                 length = strlen(pRootCert);
932                 byteBuffer.Construct(length);
933
934                 r = byteBuffer.SetArray((byte*)pRootCert, 0, length);
935                 TryCatch(!IsFailed(r), ret = false, "SetArray() is failed.");
936
937                 byteBuffer.Flip();
938
939                 r = rootCert.Construct(byteBuffer);
940                 TryCatch(!IsFailed(r), ret = false, "rootCert.Construct() is failed.");
941
942                 String subject = rootCert.GetSubject();
943                 AppLog("------------------------------------------");
944                 AppLog("Issuer  = [%ls]", issuer.GetPointer());
945                 AppLog("Subject = [%ls]", subject.GetPointer());
946                 AppLog("------------------------------------------");
947
948                 if (subject == issuer)
949                 {
950                         AppLog("subject, issuer is matched.");
951
952                         r = pCertPath->AddCertificate(rootCert);
953                         TryCatch(!IsFailed(r), ret = false, "AddCertificate(DistributorRootCert) is failed.");
954
955                         AppLog("AddCertificate() RootCert = [%d]", certType);
956                         __pContext->__rootCertType = (RootCertificateType)certType;
957
958                         ret = true;
959
960                         break;
961                 }
962                 else
963                 {
964                         AppLog("subject, issuer is not matched.");
965                         ret = false;
966                 }
967         }
968
969 CATCH:
970         delete pIntermediateCA;
971         return ret;
972 }
973