86e16fed91fabbe7f560effb424b2cf748cf5bbe
[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         fprintf(stderr, "  ## sValidationCore::VCoreInit() is done\n");
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
97         fprintf(stderr, "  ## signatureFinder.find() is done\n");
98         ValidationCore::SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
99         for (; iter != signatureFiles.rend(); ++iter)
100         {
101                 int sigFileNumber = iter->getFileNumber();
102                 AppLog("SignatureFiles: name=[%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() done");
111                         fprintf(stderr, "  ## ValidationCore::SignatureReader() is done\n");
112
113                         ValidationCore::CertificateList certList = data.getCertList();
114                         ValidationCore::CertificateList::iterator it = certList.begin();
115                         ValidationCore::CertificateList::iterator it_end = certList.end();
116                         for (; it != it_end; it++)
117                         {
118                                 std::string value = (*it)->getBase64();
119
120                                 if (data.isAuthorSignature() == true)
121                                 {
122                                         __pContext->__pAuthorCertList->Add(new String(value.c_str()));
123                                         AppLog("Author cert value=[%s]", value.c_str());
124                                 }
125                                 else
126                                 {
127                                         if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR)
128                                         {
129                                                 __pContext->__pDistributorCertList->Add(new String(value.c_str()));
130                                                 AppLog("Default distributor cert value=[%s]", value.c_str());
131                                         }
132                                 }
133                         }
134
135                         ValidationCore::SignatureValidator::Result valRes = ValidationCore::SignatureValidator::SIGNATURE_VALID;
136                         ValidationCore::SignatureValidator::AppType appType = ValidationCore::SignatureValidator::TIZEN;
137                         ValidationCore::SignatureValidator validator(appType, false, false, false);
138
139                         AppLog("validator.check() start >>");
140                         fprintf(stderr, "  ## validator.check() start\n");
141
142                         valRes = validator.check(data, rootPath);
143                         TryReturn(valRes == ValidationCore::SignatureValidator::SIGNATURE_VERIFIED, false, "validator.check() is failed, valRes=[%d]", (int)valRes);
144                         fprintf(stderr, "  ## validator.check() done\n");
145                         AppLog("validator.check() done <<");
146                         fprintf(stderr, "  ## Signature validator.check success, name=[%s], number=[%d]\n", 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                         }
168                 }
169                 catch (ValidationCore::ParserSchemaException::Base)
170                 {
171                         TryReturn(0, false, "ParserSchemaException::Base exception occurred.");
172                 }
173                 catch (DPL::Exception)
174                 {
175                         TryReturn(0, false, "DPL::Exception exception occurred.");
176                 }
177         }
178
179         AppLog("ValidateSignatures done successfully <<");
180         return true;
181 }
182
183 bool
184 SignatureManager::RegisterCertInfo() const
185 {
186         TryReturn(__pContext, false, "__pContext is null");
187
188         if (__pContext->__isHybridService == true)
189         {
190                 AppLog("Skip - HybridService");
191                 return true;
192         }
193
194         AppLog("START");
195
196         int error = 0;
197         bool res = false;
198         pkgmgr_instcertinfo_h handle = null;
199
200         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
201         TryReturn(pPackageId, false, "pPackageId is null");
202
203         error = pkgmgr_installer_create_certinfo_set_handle(&handle);
204         TryReturn(error == 0, false, "pkgmgr_installer_create_certinfo_set_handle() failed, error = [%d].", error);
205
206         if (__pContext->__pAuthorCertList)
207         {
208                 AppLog("[AuthorCert]");
209                 res = SetAuthorCertValue(handle, __pContext->__pAuthorCertList);
210                 TryCatch(res == true, , "SetAuthorCertHashValue() is failed.");
211         }
212
213         if (__pContext->__pDistributorCertList)
214         {
215                 AppLog("[DistributorCert]");
216                 res = SetDistributorCertValue(handle, __pContext->__pDistributorCertList);
217                 TryCatch(res == true, , "RegisterCertHashValue() is failed.");
218         }
219
220         error = pkgmgr_installer_save_certinfo(pPackageId.get(), handle);
221         TryCatch(error == 0, res = false, "pkgmgr_installer_save_certinfo(%s) failed, error = [%d]", pPackageId.get(), error);
222
223         AppLog("RegisterCertInfo - END");
224         AppLog("------------------------------------------");
225
226         res = true;
227
228 CATCH:
229         if (handle)
230         {
231                 pkgmgr_installer_destroy_certinfo_set_handle(handle);
232         }
233
234         return res;
235 }
236
237 bool
238 SignatureManager::UnregisterCertInfo() const
239 {
240         TryReturn(__pContext, false, "__pContext is null");
241
242         if (__pContext->__isHybridService == true)
243         {
244                 AppLog("Skip - HybridService");
245                 return true;
246         }
247
248         AppLog("START");
249         int error = 0;
250         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
251         TryReturn(pPackageId, false, "pPackageId is null");
252
253         error = pkgmgr_installer_delete_certinfo(pPackageId.get());
254         TryReturn(error == 0, false, "pkgmgr_installer_delete_certinfo(%s) failed, error = [%d]", pPackageId.get(), error);
255
256         AppLog("UnregisterCertInfo - END");
257         AppLog("------------------------------------------");
258
259         return true;
260 }
261
262 int
263 SignatureManager::GetApiVisibility(RootCertificateType certType)
264 {
265         int apiVisibility = _API_VISIBILITY_NONE;
266
267         if (certType == ROOT_CERTIFICATE_PUBLIC)
268         {
269                 apiVisibility = _API_VISIBILITY_PUBLIC;
270         }
271         else if (certType == ROOT_CERTIFICATE_PARTNER)
272         {
273                 apiVisibility = _API_VISIBILITY_PARTNER;
274         }
275         else if (certType == ROOT_CERTIFICATE_PARTNER_OPERATOR)
276         {
277                 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
278         }
279         else if (certType == ROOT_CERTIFICATE_PARTNER_MANUFACTURER)
280         {
281                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
282         }
283         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PUBLIC)
284         {
285                 apiVisibility = _API_VISIBILITY_PUBLIC;
286         }
287         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER)
288         {
289                 apiVisibility = _API_VISIBILITY_PARTNER;
290         }
291         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER_OPERATOR)
292         {
293                 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
294         }
295         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER_MANUFACTURER)
296         {
297                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
298         }
299         else if (certType == ROOT_CERTIFICATE_CERT_SVC_PLATFORM)
300         {
301                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
302         }
303
304         return apiVisibility;
305 }
306
307 bool
308 SignatureManager::SetSignature()
309 {
310         TryReturn(__pContext, false, "__pContext is null.");
311
312         bool ret = true;
313         char* pSignaturePath = _StringConverter::CopyToCharArrayN(__pContext->GetSignatureXmlPath());
314         char* pAuthorSignaturePath = _StringConverter::CopyToCharArrayN(__pContext->GetAuthorSignatureXmlPath());
315
316         __pDistributorSignature = new (std::nothrow) SignatureHandler;
317         TryCatch(__pDistributorSignature, ret = false, "__pDistributorSignature is null");
318
319         ret = __pDistributorSignature->Construct(__pContext);
320         TryCatch(ret == true, ret = false, "__pDistributorSignature->Construct is failed.");
321
322         ret = __pDistributorSignature->Parse(pSignaturePath);
323         TryCatch(ret == true, ret = false, "__pDistributorSignature->Parse is failed.");
324
325         __pAuthorSignature = new (std::nothrow) SignatureHandler;
326         TryCatch(__pAuthorSignature, ret = false, "__pAuthorSignature is null");
327
328         ret = __pAuthorSignature->Construct(__pContext);
329         TryCatch(ret == true, ret = false, "__pAuthorSignature->Construct is failed.");
330
331         ret = __pAuthorSignature->Parse(pAuthorSignaturePath);
332         TryCatch(ret == true, ret = false, "__pAuthorSignature->Parse is failed.");
333
334 CATCH:
335         delete[] pSignaturePath;
336         delete[] pAuthorSignaturePath;
337         return ret;
338 }
339
340 bool
341 SignatureManager::AddCert()
342 {
343         TryReturn(__pAuthorSignature, false, "__pAuthorSignature is null.");
344         TryReturn(__pDistributorSignature, false, "__pDistributorSignature is null.");
345
346         bool ret = true;
347         IList* pAuthorCertChain = __pAuthorSignature->GetAuthorCertChain();
348         IList* pDistributorCertChain = __pDistributorSignature->GetDistributorCertChain();
349
350         if (pAuthorCertChain)
351         {
352                 AppLog("AddCertificate - AuthorCertChain");
353
354                 __pAuthorCertPath = new (std::nothrow) X509CertificatePath();
355                 TryCatch(__pAuthorCertPath, ret = false, "__pAuthorCertPath is null.");
356
357                 ret = AddCertificate(__pAuthorCertPath, pAuthorCertChain);
358                 TryCatch(ret == true, ret = false, "AddCertificate(AuthorCert) is failed.");
359
360                 ret = AddAuthorRootCert(__pAuthorCertPath);
361                 TryCatch(ret == true, ret = false, "AddAuthorRootCert(AuthorCertPath) is failed.");
362         }
363
364         if (pDistributorCertChain)
365         {
366                 AppLog("AddCertificate - DistributorCert");
367
368                 __pDistributorCertPath = new (std::nothrow) X509CertificatePath();
369                 TryCatch(__pDistributorCertPath, ret = false, "__pDistributorCertPath is null.");
370
371                 ret = AddCertificate(__pDistributorCertPath, pDistributorCertChain);
372                 TryCatch(ret == true, ret = false, "AddCertificate(DistributorCert) is failed.");
373
374                 ret = AddDistributorRootCert(__pDistributorCertPath);
375                 TryCatch(ret == true, ret = false, "AddDistributorRootCert(DistributorCert) is failed.");
376         }
377
378 CATCH:
379         return ret;
380 }
381
382 bool
383 SignatureManager::VerifyChain()
384 {
385         TryReturn(__pAuthorCertPath, false, "__pAuthorCertPath is null.");
386         TryReturn(__pDistributorCertPath, false, "__pDistributorCertPath is null.");
387
388         bool ret = true;
389
390         AppLog("AuthorCert Validate - START");
391         ret = Validate(__pAuthorCertPath);
392         AppLog("AuthorCert Validate - END");
393         TryCatch(ret == true, ret = false, "Validate(AuthorCert) is failed.");
394
395         SetCertificatePath(SIGNATURE_FILE_TYPE_AUTHOR, __pAuthorCertPath);
396         __pAuthorCertPath = null;
397
398         AppLog("DistributorCert Validate - START");
399         ret = Validate(__pDistributorCertPath);
400         AppLog("DistributorCert Validate - END");
401         TryCatch(ret == true, ret = false, "Validate(DistributorCert) is failed.");
402
403         SetCertificatePath(SIGNATURE_FILE_TYPE_DISTRIBUTOR, __pDistributorCertPath);
404         __pDistributorCertPath = null;
405
406 CATCH:
407         delete __pAuthorCertPath;
408         __pAuthorCertPath = null;
409         delete __pDistributorCertPath;
410         __pDistributorCertPath = null;
411
412         return ret;
413 }
414
415 bool
416 SignatureManager::SetAuthorCertValue(void* pHandle, IListT<String *>* pCertList) const
417 {
418         TryReturn(pHandle, false, "pHandle is null.");
419         TryReturn(pCertList, false, "pCertPath is null.");
420
421         int res = 0;
422         result r = E_SUCCESS;
423         int certType = 0;
424
425         for (int i = 0; i < pCertList->GetCount(); i++)
426         {
427                 String* pCert = null;
428                 r = pCertList->GetAt(i, pCert);
429                 TryReturn(!IsFailed(r), false, "pCertList->GetAt() is failed, [%d]", i);
430                 TryReturn(pCert, false, "pCertList->GetAt() is failed, [%d]", i);
431
432                 std::unique_ptr<char[]> pCertValue(_StringConverter::CopyToCharArrayN(*pCert));
433
434                 if (i == 0)
435                 {
436                         certType = PM_SET_AUTHOR_SIGNER_CERT;
437                 }
438                 else if (i == 1)
439                 {
440                         certType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
441                 }
442                 else if (i == 2)
443                 {
444                         certType = PM_SET_AUTHOR_ROOT_CERT;
445                 }
446                 else
447                 {
448                         AppLog("Invalid certificate type[%d]", i);
449                         break;
450                 }
451
452                 AppLog("------------------------------------------");
453                 AppLog("Certificate type=[%d]", certType);
454                 AppLog("[%s]", pCertValue.get());
455
456                 res = pkgmgr_installer_set_cert_value(pHandle, (pkgmgr_instcert_type)certType, pCertValue.get());
457                 TryReturn(res == 0, false, "pkgmgr_installer_set_cert_value(%d) failed.[%d]", i, res);
458         }
459
460         return true;
461 }
462
463 bool
464 SignatureManager::SetDistributorCertValue(void* pHandle, IListT<String *>* pCertList) const
465 {
466         TryReturn(pHandle, false, "pHandle is null.");
467         TryReturn(pCertList, false, "pCertPath is null.");
468
469         int res = 0;
470         result r = E_SUCCESS;
471         int certType = 0;
472
473         for (int i = 0; i < pCertList->GetCount(); i++)
474         {
475                 String* pCert = null;
476                 r = pCertList->GetAt(i, pCert);
477                 TryReturn(!IsFailed(r), false, "pCertList->GetAt() is failed, [%d]", i);
478                 TryReturn(pCert, false, "pCertList->GetAt() is failed, [%d]", i);
479
480                 std::unique_ptr<char[]> pCertValue(_StringConverter::CopyToCharArrayN(*pCert));
481
482                 if (i == 0)
483                 {
484                         certType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
485                 }
486                 else if (i == 1)
487                 {
488                         certType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
489                 }
490                 else if (i == 2)
491                 {
492                         certType = PM_SET_DISTRIBUTOR_ROOT_CERT;
493                 }
494                 else
495                 {
496                         AppLog("Invalid certificate type[%d]", i);
497                         break;
498                 }
499
500                 AppLog("------------------------------------------");
501                 AppLog("Certificate type=[%d]", certType);
502                 AppLog("[%s]", pCertValue.get());
503
504                 res = pkgmgr_installer_set_cert_value(pHandle, (pkgmgr_instcert_type)certType, pCertValue.get());
505                 TryReturn(res == 0, false, "pkgmgr_installer_set_cert_value(%d) failed.[%d]", i, res);
506         }
507
508         return true;
509 }
510
511 bool
512 SignatureManager::SetCertificatePath(SignatureFileType type, X509CertificatePath* pCertPath)
513 {
514         TryReturn(pCertPath, false, "pCertPath is null.");
515         TryReturn(__pContext, false, "__pContext is null.");
516
517         IListT<String *>* pList = null;
518
519         if (type == SIGNATURE_FILE_TYPE_AUTHOR)
520         {
521                 pList = __pContext->__pAuthorCertList;
522         }
523         else if (type == SIGNATURE_FILE_TYPE_DISTRIBUTOR)
524         {
525                 pList = __pContext->__pDistributorCertList;
526         }
527         else
528         {
529                 TryReturn(0, false, "SignatureFileType is unknown, type = [%d]", (int)type);
530         }
531
532         for (int i = 0; i < pCertPath->GetLength(); i++)
533         {
534                 std::unique_ptr<ICertificate> pCert(pCertPath->GetCertificateN(i));
535                 TryReturn(pCert, false, "pCert is null, [%i].", i);
536
537                 bool res = false;
538                 String certValue;
539                 res = GetCertValue(pCert.get(), certValue);
540                 TryReturn(res == true, false, "GetCertValue() is failed, [%d].", i);
541
542                 result r = pList->Add(new String(certValue));
543                 TryReturn(!IsFailed(r), false, "pList->Add() is failed, [%d]", i);
544
545                 AppLog("------------------------------------------");
546                 AppLog("CertValue[%d], certType[%d]", i, (int)type);
547                 AppLog("[%ls]", certValue.GetPointer());
548         }
549
550         return true;
551 }
552
553 bool
554 SignatureManager::GetCertValue(ICertificate* pCert, String& value) const
555 {
556         result r = E_SUCCESS;
557
558         std::unique_ptr<ByteBuffer> pEncodedData(pCert->GetEncodedDataN());
559         TryReturn(pEncodedData, false, "pEncodedData is null.");
560
561         r = StringUtil::EncodeToBase64String(*pEncodedData.get(), value);
562         TryReturn(!IsFailed(r), false, "StringUtil::EncodeToBase64String() is failed.");
563
564         return true;
565 }
566
567 bool
568 SignatureManager::Validate(X509CertificatePath* pCertPath)
569 {
570         TryReturn(pCertPath, false, "pCertPath is null.");
571
572         AppLog("------------------------------------------");
573         AppLog("# signature.xml");
574         ValidationResult valResult = VALIDATION_SUCCESS;
575         valResult = pCertPath->Validate();
576
577         if (valResult != VALIDATION_SUCCESS)
578         {
579                 AppLog("Validate() fail! - ValidationResult = [%d]", valResult);
580                 AppLog("------------------------------------------");
581                 return false;
582         }
583         else
584         {
585                 int depth = pCertPath->GetLength();
586                 if (depth == 0)
587                 {
588                         AppLog("depth = 0");
589                         return false;
590                 }
591
592                 AppLog("Validate() success!");
593                 AppLog("------------------------------------------");
594         }
595
596         return true;
597 }
598
599 bool
600 SignatureManager::AddCertificate(X509CertificatePath* pCertPath, IList* pCertChain)
601 {
602         TryReturn(pCertChain, false, "pCertChain is null.");
603
604         bool ret = true;
605         result r = E_SUCCESS;
606         X509Certificate* pCertificate = null;
607
608         for (int i = 0; i < pCertChain->GetCount(); i++)
609         {
610                 Tizen::Base::ByteBuffer* pByteBuffer = dynamic_cast <ByteBuffer*>(pCertChain->GetAt(i));
611
612                 if (pByteBuffer)
613                 {
614                         AppLog("[cert][%d]", i);
615
616                         pCertificate = new (std::nothrow) X509Certificate;
617                         TryCatch(pCertificate, ret = false, "pCertificate is null.");
618
619                         r = pCertificate->Construct(*pByteBuffer);
620                         TryCatch(!IsFailed(r), ret = false, "pCertificate->Construct() is failed.");
621
622                         r = pCertPath->AddCertificate(*pCertificate);
623                         TryCatch(!IsFailed(r), ret = false, "AddCertificate is failed.");
624
625                         delete pCertificate;
626                         pCertificate = null;
627                 }
628         }
629
630 CATCH:
631         delete pCertificate;
632         return ret;
633 }
634
635 bool
636 SignatureManager::AddAuthorRootCert(X509CertificatePath* pCertPath)
637 {
638         TryReturn(pCertPath, false, "pCertPath is null.");
639
640         result r = E_SUCCESS;
641         bool ret = true;
642         ByteBuffer byteBuffer;
643         X509Certificate rootCert;
644         int length = 0;
645         const char* pAuthorRootCert = "MIICnzCCAggCCQCn+GGT4zh+BjANBgkqhkiG9w0BAQUFADCBkzELMAkGA1UEBhMC"
646                                                                                                                                 "S1IxDjAMBgNVBAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UECgwNVGl6"
647                                                                                                                                 "ZW4gVGVzdCBDQTElMCMGA1UECwwcVGl6ZW4gVGVzdCBEZXZlbG9wZXIgUm9vdCBD"
648                                                                                                                                 "QTElMCMGA1UEAwwcVGl6ZW4gVGVzdCBEZXZlbG9wZXIgUm9vdCBDQTAeFw0xMjEw"
649                                                                                                                                 "MjYwOTUwMTNaFw0yMjEwMjQwOTUwMTNaMIGTMQswCQYDVQQGEwJLUjEOMAwGA1UE"
650                                                                                                                                 "CAwFU3V3b24xDjAMBgNVBAcMBVN1d29uMRYwFAYDVQQKDA1UaXplbiBUZXN0IENB"
651                                                                                                                                 "MSUwIwYDVQQLDBxUaXplbiBUZXN0IERldmVsb3BlciBSb290IENBMSUwIwYDVQQD"
652                                                                                                                                 "DBxUaXplbiBUZXN0IERldmVsb3BlciBSb290IENBMIGfMA0GCSqGSIb3DQEBAQUA"
653                                                                                                                                 "A4GNADCBiQKBgQDWT6ZH5JyGadTUK1QmNwU8j+py4WtuElJE+4/wPFP8/KBmvvmI"
654                                                                                                                                 "rGVjhUbKXToKIo8N6C/0SLxGEWuRAIoZHhg5JVbw1Ay7smgJJHizDUAqMTmV6LI9"
655                                                                                                                                 "yTFbBV+OlO2Dir4LVdQ/XDBiqqslr7pqXgsg1V2g7x+tOI/f3dn2kWoVZQIDAQAB"
656                                                                                                                                 "MA0GCSqGSIb3DQEBBQUAA4GBADGJYMtzUBDK+KKLZQ6zYmrKb+OWLlmEr/t/c2af"
657                                                                                                                                 "KjTKUtommcz8VeTPqrDBOwxlVPdxlbhisCYzzvwnWeZk1aeptxxU3kdW9N3/wocN"
658                                                                                                                                 "5nBzgqkkHJnj/ptqjrH2v/m0Z3hBuI4/akHIIfCBF8mUHwqcxYsRdcCIrkgp2Aiv"
659                                                                                                                                 "bSaM";
660
661         length = strlen(pAuthorRootCert);
662         byteBuffer.Construct(length);
663
664         r = byteBuffer.SetArray((byte*)pAuthorRootCert, 0, length);
665         TryCatch(!IsFailed(r), ret = false, "SetArray() is failed.");
666
667         byteBuffer.Flip();
668
669         r = rootCert.Construct(byteBuffer);
670         TryCatch(!IsFailed(r), ret = false, "rootCert.Construct() is failed.");
671
672         r = pCertPath->AddCertificate(rootCert);
673         TryCatch(!IsFailed(r), ret = false, "AddCertificate(AuthorRootCert) is failed.");
674
675 CATCH:
676         return ret;
677 }
678
679 bool
680 SignatureManager::AddDistributorRootCert(X509CertificatePath* pCertPath)
681 {
682         TryReturn(pCertPath, false, "pCertPath is null.");
683
684         result r = E_SUCCESS;
685         bool ret = true;
686         ICertificate* pIntermediateCA = null;
687         String issuer;
688
689         pIntermediateCA = pCertPath->GetCertificateN(1);
690         TryCatch(pIntermediateCA, ret = false, "pIntermediateCA is null.");
691
692         issuer = pIntermediateCA->GetIssuer();
693
694         for(int certType = ROOT_CERTIFICATE_PUBLIC; certType <= ROOT_CERTIFICATE_PARTNER_MANUFACTURER; certType++)
695         {
696                 const char* pRootCert = null;
697                 ByteBuffer byteBuffer;
698                 X509Certificate rootCert;
699                 int length = 0;
700
701                 if (certType == ROOT_CERTIFICATE_PUBLIC)
702                 {
703                         pRootCert = "MIICozCCAgwCCQD9XW6kNg4bbjANBgkqhkiG9w0BAQUFADCBlTELMAkGA1UEBhMC"
704                                                 "S1IxDjAMBgNVBAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UECgwNVGl6"
705                                                 "ZW4gVGVzdCBDQTEjMCEGA1UECwwaVFRpemVuIERpc3RyaWJ1dG9yIFRlc3QgQ0Ex"
706                                                 "KTAnBgNVBAMMIFRpemVuIFB1YmxpYyBEaXN0cmlidXRvciBSb290IENBMB4XDTEy"
707                                                 "MTAyNjA4MDAyN1oXDTIyMTAyNDA4MDAyN1owgZUxCzAJBgNVBAYTAktSMQ4wDAYD"
708                                                 "VQQIDAVTdXdvbjEOMAwGA1UEBwwFU3V3b24xFjAUBgNVBAoMDVRpemVuIFRlc3Qg"
709                                                 "Q0ExIzAhBgNVBAsMGlRUaXplbiBEaXN0cmlidXRvciBUZXN0IENBMSkwJwYDVQQD"
710                                                 "DCBUaXplbiBQdWJsaWMgRGlzdHJpYnV0b3IgUm9vdCBDQTCBnzANBgkqhkiG9w0B"
711                                                 "AQEFAAOBjQAwgYkCgYEA8o0kPY1U9El1BbBUF1k4jCq6mH8a6MmDJdjgsz+hILAY"
712                                                 "sPWimRTXUcW8GAUWhZWgm1Fbb49xWcasA8b4bIJabC/6hLb8uWiozzpRXyQJbe7k"
713                                                 "//RocskRqDmFOky8ANFsCCww72/Xbq8BFK1sxlGdmOWQiGwDWBDlS2Lw1XOMqb0C"
714                                                 "AwEAATANBgkqhkiG9w0BAQUFAAOBgQBUotZqTNFr+SNyqeZqhOToRsg3ojN1VJUa"
715                                                 "07qdlVo5I1UObSE+UTJPJ0NtSj7OyTY7fF3E4xzUv/w8aUoabQP1erEmztY/AVD+"
716                                                 "phHaPytkZ/Dx+zDZ1u5e9bKm5zfY4dQs/A53zDQta5a/NkZOEF97Dj3+bzAh2bP7"
717                                                 "KOszlocFYw==";
718                 }
719                 else if (certType == ROOT_CERTIFICATE_PARTNER)
720                 {
721                         pRootCert = "MIICozCCAgwCCQD9IBoOxzq2hjANBgkqhkiG9w0BAQUFADCBlTELMAkGA1UEBhMC"
722                                                 "S1IxDjAMBgNVBAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UECgwNVGl6"
723                                                 "ZW4gVGVzdCBDQTEiMCAGA1UECwwZVGl6ZW4gRGlzdHJpYnV0b3IgVGVzdCBDQTEq"
724                                                 "MCgGA1UEAwwhVGl6ZW4gUGFydG5lciBEaXN0cmlidXRvciBSb290IENBMB4XDTEy"
725                                                 "MTAyNjA4MTIzMVoXDTIyMTAyNDA4MTIzMVowgZUxCzAJBgNVBAYTAktSMQ4wDAYD"
726                                                 "VQQIDAVTdXdvbjEOMAwGA1UEBwwFU3V3b24xFjAUBgNVBAoMDVRpemVuIFRlc3Qg"
727                                                 "Q0ExIjAgBgNVBAsMGVRpemVuIERpc3RyaWJ1dG9yIFRlc3QgQ0ExKjAoBgNVBAMM"
728                                                 "IVRpemVuIFBhcnRuZXIgRGlzdHJpYnV0b3IgUm9vdCBDQTCBnzANBgkqhkiG9w0B"
729                                                 "AQEFAAOBjQAwgYkCgYEAnIBA2qQEaMzGalP0kzvwUxdCC6ybSC/fb+M9iGvt8QXp"
730                                                 "ic2yARQB+bIhfbEu1XHwE1jCAGxKd6uT91b4FWr04YwnBPoRX4rBGIYlqo/dg+pS"
731                                                 "rGyFjy7vfr0BOdWp2+WPlTe7SOS6bVauncrSoHxX0spiLaU5LU686BKr7YaABV0C"
732                                                 "AwEAATANBgkqhkiG9w0BAQUFAAOBgQAX0Tcfmxcs1TUPBdr1U1dx/W/6Y4PcAF7n"
733                                                 "DnMrR0ZNRPgeSCiVLax1bkHxcvW74WchdKIb24ZtAsFwyrsmUCRV842YHdfddjo6"
734                                                 "xgUu7B8n7hQeV3EADh6ft/lE8nalzAl9tALTxAmLtYvEYA7thvDoKi1k7bN48izL"
735                                                 "gS9G4WEAUg==";
736                 }
737                 else if (certType == ROOT_CERTIFICATE_PARTNER_OPERATOR)
738                 {
739                         pRootCert = "MIICzDCCAjWgAwIBAgIJAJrv22F9wyp/MA0GCSqGSIb3DQEBBQUAMIGeMQswCQYD"
740                                                                         "VQQGEwJLUjEOMAwGA1UECAwFU3V3b24xDjAMBgNVBAcMBVN1d29uMRYwFAYDVQQK"
741                                                                         "DA1UaXplbiBUZXN0IENBMSIwIAYDVQQLDBlUaXplbiBEaXN0cmlidXRvciBUZXN0"
742                                                                         "IENBMTMwMQYDVQQDDCpUaXplbiBQYXJ0bmVyLU9wZXJhdG9yIERpc3RyaWJ1dG9y"
743                                                                         "IFJvb3QgQ0EwHhcNMTIxMjEzMDUzOTMyWhcNMjIxMjExMDUzOTMyWjCBnjELMAkG"
744                                                                         "A1UEBhMCS1IxDjAMBgNVBAgMBVN1d29uMQ4wDAYDVQQHDAVTdXdvbjEWMBQGA1UE"
745                                                                         "CgwNVGl6ZW4gVGVzdCBDQTEiMCAGA1UECwwZVGl6ZW4gRGlzdHJpYnV0b3IgVGVz"
746                                                                         "dCBDQTEzMDEGA1UEAwwqVGl6ZW4gUGFydG5lci1PcGVyYXRvciBEaXN0cmlidXRv"
747                                                                         "ciBSb290IENBMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9X0Hw0EfAuagg"
748                                                                         "De9h6Jtvh8Df4fyVbvLm9VNea/iVP3/qTbG8tNqoQ32lu0SwzAZBnjpvpbxzsWs9"
749                                                                         "pSYo7Ys1fymHlu+gf+kmTGTVscBrAHWkr4O0m33x2FYfy/wmu+IImnRDYDud83rN"
750                                                                         "tjQmMO6BihN9Lb6kLiEtVIa8ITwdQwIDAQABoxAwDjAMBgNVHRMEBTADAQH/MA0G"
751                                                                         "CSqGSIb3DQEBBQUAA4GBAHS2M2UnfEsZf80/sT84xTcfASXgpFL/1M5HiAVpR+1O"
752                                                                         "UwLpLyqHiGQaASuADDeGEfcIqEf8gP1SzvnAZqLx9GchbOrOKRleooVFH7PRxFBS"
753                                                                         "VWJ5Fq46dJ1mCgTWSkrL6dN5j9hWCzzGfv0Wco+NAf61n9kVbCv7AScIJwQNltOy";
754                 }
755                 else // ROOT_CERTIFICATE_PARTNER_MANUFACTURER
756                 {
757                         pRootCert = "MIIC1DCCAj2gAwIBAgIJAJZH47dCtgPdMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD"
758                                                                         "VQQGEwJLUjEOMAwGA1UECAwFU3V3b24xDjAMBgNVBAcMBVN1d29uMRYwFAYDVQQK"
759                                                                         "DA1UaXplbiBUZXN0IENBMSIwIAYDVQQLDBlUaXplbiBEaXN0cmlidXRvciBUZXN0"
760                                                                         "IENBMTcwNQYDVQQDDC5UaXplbiBQYXJ0bmVyLU1hbnVmYWN0dXJlciBEaXN0cmli"
761                                                                         "dXRvciBSb290IENBMB4XDTEyMTIxMzA1NDQxN1oXDTIyMTIxMTA1NDQxN1owgaIx"
762                                                                         "CzAJBgNVBAYTAktSMQ4wDAYDVQQIDAVTdXdvbjEOMAwGA1UEBwwFU3V3b24xFjAU"
763                                                                         "BgNVBAoMDVRpemVuIFRlc3QgQ0ExIjAgBgNVBAsMGVRpemVuIERpc3RyaWJ1dG9y"
764                                                                         "IFRlc3QgQ0ExNzA1BgNVBAMMLlRpemVuIFBhcnRuZXItTWFudWZhY3R1cmVyIERp"
765                                                                         "c3RyaWJ1dG9yIFJvb3QgQ0EwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMJG"
766                                                                         "0gq3XrDn7W7cIH58w7hSDMVENnXLmXm4Jl5teXXrgL/BgpORracGMgm0Fmxxq/Nq"
767                                                                         "8OEI2RfmtrlN5nWGiphs3XqLHtO+BAPY1BbZS6YVZjrVXrGWdzk12zQxd6sXJMiV"
768                                                                         "B08ECQiQ0qgKFbTDSEbH/p4eyKCMG9lnrBLPHTpJAgMBAAGjEDAOMAwGA1UdEwQF"
769                                                                         "MAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAJTJYCr9GhZ1xXwvxsWLGd9XV9wixo1zk"
770                                                                         "FV2+eJZbCc4xJyOSb0cZds8fYUuzw32oyElLvPhYfAHVTHu/WlkwSshZlKdI2hCT"
771                                                                         "Iy03/Up+JNfuom8JLgF7qc3YtbuJHzoVu1jJ/akXU6y52D/J5CkYy2JSsV0KZuh2"
772                                                                         "ZeRWlV2f1Uo=";
773                 }
774
775                 length = strlen(pRootCert);
776                 byteBuffer.Construct(length);
777
778                 r = byteBuffer.SetArray((byte*)pRootCert, 0, length);
779                 TryCatch(!IsFailed(r), ret = false, "SetArray() is failed.");
780
781                 byteBuffer.Flip();
782
783                 r = rootCert.Construct(byteBuffer);
784                 TryCatch(!IsFailed(r), ret = false, "rootCert.Construct() is failed.");
785
786                 String subject = rootCert.GetSubject();
787                 AppLog("------------------------------------------");
788                 AppLog("Issuer  = [%ls]", issuer.GetPointer());
789                 AppLog("Subject = [%ls]", subject.GetPointer());
790                 AppLog("------------------------------------------");
791
792                 if (subject == issuer)
793                 {
794                         AppLog("subject, issuer is matched.");
795
796                         r = pCertPath->AddCertificate(rootCert);
797                         TryCatch(!IsFailed(r), ret = false, "AddCertificate(DistributorRootCert) is failed.");
798
799                         AppLog("AddCertificate() RootCert = [%d]", certType);
800                         __pContext->__rootCertType = (RootCertificateType)certType;
801
802                         ret = true;
803
804                         break;
805                 }
806                 else
807                 {
808                         AppLog("subject, issuer is not matched.");
809                         ret = false;
810                 }
811         }
812
813 CATCH:
814         delete pIntermediateCA;
815         return ret;
816 }
817