2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file SignatureManager.cpp
19 * @brief This is the implementation file for %SignatureManager class.
22 #include <pkgmgr_parser.h>
23 #include <pkgmgr_installer.h>
24 #include <package-manager.h>
25 #include <pkgmgr-info.h>
27 #include <FBase_StringConverter.h>
28 #include <FBaseInternalTypes.h>
30 #include "SignatureManager.h"
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>
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Base::Utility;
43 SignatureManager::SignatureManager(void)
48 SignatureManager::~SignatureManager(void)
53 SignatureManager::Construct(InstallationContext* pContext)
55 __pContext = pContext;
61 SignatureManager::ValidateSignatures()
63 TryReturn(__pContext, false, "__pContext is null.");
65 AppLog("ValidateSignatures start >>");
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");
76 std::unique_ptr<char[]> pRootPath(_StringConverter::CopyToCharArrayN(__pContext->__rootPath));
77 TryReturn(pRootPath, false, "__pRootPath is null.");
79 std::string rootPath = pRootPath.get();
81 AppLog("rootPath=[%s]", rootPath.c_str());
83 ValidationCore::SignatureFinder::Result findRes = ValidationCore::SignatureFinder::NO_ERROR;
84 ValidationCore::SignatureFinder signatureFinder(rootPath);
85 ValidationCore::SignatureFileInfoSet signatureFiles;
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");
91 ValidationCore::SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
92 for (; iter != signatureFiles.rend(); ++iter)
94 int sigFileNumber = iter->getFileNumber();
95 AppLog("SignatureFiles: file=[%s]", iter->getFileName().c_str());
96 AppLog("SignatureFiles: number=[%d]", sigFileNumber);
98 if (sigFileNumber > SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
100 AppLog("SignatureFiles: skip!");
104 ValidationCore::SignatureData data(rootPath + iter->getFileName(), iter->getFileNumber());
107 ValidationCore::SignatureReader xml;
108 xml.initialize(data, "/usr/share/wrt-engine/schema.xsd");
110 AppLog("ValidationCore::SignatureReader() is done");
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++)
117 std::string value = (*it)->getBase64();
119 if (data.isAuthorSignature() == true)
121 __pContext->__pAuthorCertList->Add(new String(value.c_str()));
122 AppLog("Author cert value=[%s]", value.c_str());
126 if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR)
128 __pContext->__pDistributorCertList->Add(new String(value.c_str()));
129 AppLog("Default distributor cert value=[%s]", value.c_str());
131 else if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
133 AppLog("Default distributor2 cert value=[%s]", value.c_str());
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);
142 AppLog("validator.check() start >>");
143 valRes = validator.check(data, rootPath);
144 if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
146 if (valRes == ValidationCore::SignatureValidator::SIGNATURE_VERIFIED)
148 AppLog("Signature validator.check success, file=[%s], number=[%d]", iter->getFileName().c_str(), iter->getFileNumber());
152 AppLog("Signature validator.check failed, but it's ok, file=[%s], number=[%d]", iter->getFileName().c_str(), iter->getFileNumber());
153 __pContext->__pDistributorCertList2->RemoveAll();
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());
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)
167 __pContext->__pAuthorCertList->Add(new String(value.c_str()));
168 AppLog("Author root cert value=[%s]", value.c_str());
172 if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR)
174 __pContext->__pDistributorCertList->Add(new String(value.c_str()));
175 AppLog("Default root distributor cert value=[%s]", value.c_str());
177 ValidationCore::CertStoreId::Type certStoreIdType = data.getVisibilityLevel();
178 AppLog("CertStoreIdType = [%d]", (int)certStoreIdType);
179 __pContext->__rootCertType = (RootCertificateType)certStoreIdType;
181 else if ((sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2) && (valRes == ValidationCore::SignatureValidator::SIGNATURE_VERIFIED))
183 __pContext->__pDistributorCertList2->Add(new String(value.c_str()));
184 AppLog("Default root distributor2 cert value=[%s]", value.c_str());
188 catch (ValidationCore::ParserSchemaException::Base)
190 if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
192 AppLog("ValidationCore::ParserSchemaException::Base occurred, but it's ok for DISTRIBUTOR2.");
195 TryReturn(0, false, "ParserSchemaException::Base exception occurred.");
197 catch (DPL::Exception)
199 if (sigFileNumber == SIGNATURE_FILE_NUMBER_DISTRIBUTOR2)
201 AppLog("DPL::Exception occurred, but it's ok for DISTRIBUTOR2.");
204 TryReturn(0, false, "DPL::Exception exception occurred.");
208 AppLog("ValidateSignatures done successfully <<");
210 if (__pContext->__isUpdated == true)
212 res = ValidateUpdate();
213 TryReturn(res, false, "ValidateUpdate() is failed.");
220 SignatureManager::ValidateUpdate()
222 TryReturn(__pContext, false, "__pContext is null.");
223 TryReturn(__pContext->__isUpdated, false, "It's not update.");
225 if (InstallerUtil::IsAuthorSignatureVerificationEnabled() == false)
227 AppLog("ValidateUpdate() skip.");
231 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
232 TryReturn(pPackageId, false, "pPackageId is null.");
234 result r = E_SUCCESS;
235 String oldCertificate;
236 String* pNewCertificate = null;
237 const char* pValue = null;
238 pkgmgr_certinfo_h handle = null;
240 int res = pkgmgr_pkginfo_create_certinfo(&handle);
241 if (res == PMINFO_R_OK)
243 res = pkgmgr_pkginfo_load_certinfo(pPackageId.get(), handle);
244 if (res == PMINFO_R_OK)
246 pkgmgr_pkginfo_get_cert_value(handle, PM_AUTHOR_SIGNER_CERT, &pValue);
247 oldCertificate = pValue;
250 pkgmgr_pkginfo_destroy_certinfo(handle);
253 if (oldCertificate.IsEmpty() == true)
255 AppLog("oldCert is empty.");
260 r = __pContext->__pAuthorCertList->GetAt(0, pNewCertificate);
261 TryReturn(r == E_SUCCESS, false, "GetAt() failed.");
263 AppLog("------------------------------------------");
264 AppLog("oldCert = [%ls]", oldCertificate.GetPointer());
265 AppLog("newCert = [%ls]", pNewCertificate->GetPointer());
266 AppLog("------------------------------------------");
268 if (oldCertificate.Equals(*pNewCertificate, true) == false)
270 AppLog("oldCert, newCert is different.");
275 AppLog("oldCert, newCert is the same.");
280 SignatureManager::ValidatePartialReferences()
282 AppLog("ValidatePartialReferences start >>");
285 std::unique_ptr<char[]> pSignaturePath(_StringConverter::CopyToCharArrayN(__pContext->GetSignatureXmlPath()));
286 TryReturn(pSignaturePath, false, "pSignaturePath is null.");
288 std::unique_ptr<char[]> pAuthorSignaturePath(_StringConverter::CopyToCharArrayN(__pContext->GetAuthorSignatureXmlPath()));
289 TryReturn(pAuthorSignaturePath, false, "pAuthorSignaturePath is null.");
291 std::unique_ptr<SignatureHandler> pDistributorSignature(new (std::nothrow) SignatureHandler());
292 TryReturn(pDistributorSignature, false, "pDistributorSignature is null.");
294 res = pDistributorSignature->Construct(__pContext);
295 TryReturn(res == true, false, "pDistributorSignature->Construct() is failed.");
297 res = pDistributorSignature->Parse(pSignaturePath.get());
298 TryReturn(res == true, false, "pDistributorSignature->Parse() is failed.");
300 HashMap* pDistributorRefMap = pDistributorSignature->GetReferenceMap();
301 TryReturn(pDistributorRefMap, false, "pDistributorRefMap is null.");
303 std::unique_ptr<SignatureHandler> pAuthorSignature(new (std::nothrow) SignatureHandler());
304 TryReturn(pAuthorSignature, false, "pAuthorSignature is null.");
306 res = pAuthorSignature->Construct(__pContext);
307 TryReturn(res == true, false, "pAuthorSignature.Construct() is failed.");
309 res = pAuthorSignature->Parse(pAuthorSignaturePath.get());
310 TryReturn(res == true, false, "pAuthorSignature->Parse() is failed.");
312 HashMap* pAuthorRefMap = pAuthorSignature->GetReferenceMap();
313 TryReturn(pAuthorRefMap, false, "pAuthorRefMap is null.");
315 res = CompareReferences(pDistributorRefMap, pAuthorRefMap);
316 TryReturn(res == true, false, "CompareReferences() is failed.");
322 SignatureManager::RegisterCertInfo() const
324 TryReturn(__pContext, false, "__pContext is null");
326 if (__pContext->__isHybridService == true)
328 AppLog("Skip - HybridService");
336 pkgmgr_instcertinfo_h handle = null;
338 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
339 TryReturn(pPackageId, false, "pPackageId is null");
341 error = pkgmgr_installer_create_certinfo_set_handle(&handle);
342 TryReturn(error == 0, false, "pkgmgr_installer_create_certinfo_set_handle() failed, error = [%d].", error);
344 if (__pContext->__pAuthorCertList)
346 AppLog("[AuthorCert]");
347 res = SetAuthorCertValue(handle, __pContext->__pAuthorCertList);
348 TryCatch(res == true, , "SetAuthorCertHashValue() is failed.");
351 if (__pContext->__pDistributorCertList)
353 AppLog("[DistributorCert]");
354 res = SetDistributorCertValue(handle, __pContext->__pDistributorCertList, 1);
355 TryCatch(res == true, , "SetDistributorCertValue() is failed.");
358 if (__pContext->__pDistributorCertList2)
360 AppLog("[DistributorCert2]");
361 res = SetDistributorCertValue(handle, __pContext->__pDistributorCertList2, 2);
362 TryCatch(res == true, , "SetDistributorCertValue() is failed.");
365 error = pkgmgr_installer_save_certinfo(pPackageId.get(), handle);
366 TryCatch(error == 0, res = false, "pkgmgr_installer_save_certinfo(%s) failed, error = [%d]", pPackageId.get(), error);
368 AppLog("RegisterCertInfo - END");
369 AppLog("------------------------------------------");
376 pkgmgr_installer_destroy_certinfo_set_handle(handle);
383 SignatureManager::UnregisterCertInfo() const
385 TryReturn(__pContext, false, "__pContext is null");
387 if (__pContext->__isHybridService == true)
389 AppLog("Skip - HybridService");
395 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
396 TryReturn(pPackageId, false, "pPackageId is null");
398 error = pkgmgr_installer_delete_certinfo(pPackageId.get());
399 TryReturn(error == 0, false, "pkgmgr_installer_delete_certinfo(%s) failed, error = [%d]", pPackageId.get(), error);
401 AppLog("UnregisterCertInfo - END");
402 AppLog("------------------------------------------");
408 SignatureManager::GetApiVisibility(RootCertificateType certType)
410 int apiVisibility = _API_VISIBILITY_NONE;
412 if (certType == ROOT_CERTIFICATE_PUBLIC)
414 apiVisibility = _API_VISIBILITY_PUBLIC;
416 else if (certType == ROOT_CERTIFICATE_PARTNER)
418 apiVisibility = _API_VISIBILITY_PARTNER;
420 else if (certType == ROOT_CERTIFICATE_PARTNER_OPERATOR)
422 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
424 else if (certType == ROOT_CERTIFICATE_PARTNER_MANUFACTURER)
426 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
428 else if (certType == ROOT_CERTIFICATE_CERT_SVC_PUBLIC)
430 apiVisibility = _API_VISIBILITY_PUBLIC;
432 else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER)
434 apiVisibility = _API_VISIBILITY_PARTNER;
436 else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER_OPERATOR)
438 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
440 else if (certType == ROOT_CERTIFICATE_CERT_SVC_PARTNER_MANUFACTURER)
442 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
444 else if (certType == ROOT_CERTIFICATE_CERT_SVC_PLATFORM)
446 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
449 return apiVisibility;
453 SignatureManager::GetPrivilegeLevel(int apiVisibility)
455 if (apiVisibility == _API_VISIBILITY_PARTNER_MANUFACTURER || apiVisibility == _API_VISIBILITY_PARTNER_OPERATOR)
457 return PRIVILEGE_LEVEL_PLATFORM;
459 else if (apiVisibility == _API_VISIBILITY_PARTNER)
461 return PRIVILEGE_LEVEL_PARTNER;
464 return PRIVILEGE_LEVEL_PUBLIC;
468 SignatureManager::SetAuthorCertValue(void* pHandle, IListT<String *>* pCertList) const
470 TryReturn(pHandle, false, "pHandle is null.");
471 TryReturn(pCertList, false, "pCertPath is null.");
474 result r = E_SUCCESS;
477 for (int i = 0; i < pCertList->GetCount(); i++)
479 String* pCert = null;
480 r = pCertList->GetAt(i, pCert);
481 TryReturn(!IsFailed(r), false, "pCertList->GetAt() is failed, [%d]", i);
482 TryReturn(pCert, false, "pCertList->GetAt() is failed, [%d]", i);
484 std::unique_ptr<char[]> pCertValue(_StringConverter::CopyToCharArrayN(*pCert));
488 certType = PM_SET_AUTHOR_SIGNER_CERT;
492 certType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
496 certType = PM_SET_AUTHOR_ROOT_CERT;
500 AppLog("Invalid certificate type[%d]", i);
504 AppLog("------------------------------------------");
505 AppLog("Certificate type=[%d]", certType);
506 AppLog("[%s]", pCertValue.get());
508 res = pkgmgr_installer_set_cert_value(pHandle, (pkgmgr_instcert_type)certType, pCertValue.get());
509 TryReturn(res == 0, false, "pkgmgr_installer_set_cert_value(%d) failed.[%d]", i, res);
516 SignatureManager::SetDistributorCertValue(void* pHandle, IListT<String *>* pCertList, int sigFileNumber) const
518 TryReturn(pHandle, false, "pHandle is null.");
519 TryReturn(pCertList, false, "pCertPath is null.");
520 TryReturn((sigFileNumber == 1) || (sigFileNumber == 2), false, "sigFileNumber[%d] is invalid.", sigFileNumber);
523 result r = E_SUCCESS;
526 for (int i = 0; i < pCertList->GetCount(); i++)
528 String* pCert = null;
529 r = pCertList->GetAt(i, pCert);
530 TryReturn(!IsFailed(r), false, "pCertList->GetAt() is failed, [%d]", i);
531 TryReturn(pCert, false, "pCertList->GetAt() is failed, [%d]", i);
533 std::unique_ptr<char[]> pCertValue(_StringConverter::CopyToCharArrayN(*pCert));
535 if (sigFileNumber == 1)
539 certType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
543 certType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
547 certType = PM_SET_DISTRIBUTOR_ROOT_CERT;
551 AppLog("Invalid certificate type[%d]", i);
555 else if (sigFileNumber == 2)
557 if (pCertList->GetCount() == 1)
559 certType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
565 certType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
569 certType = PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
573 certType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
577 AppLog("Invalid certificate type[%d]", i);
583 AppLog("------------------------------------------");
584 AppLog("Certificate type=[%d]", certType);
585 AppLog("[%s]", pCertValue.get());
587 res = pkgmgr_installer_set_cert_value(pHandle, (pkgmgr_instcert_type)certType, pCertValue.get());
588 TryReturn(res == 0, false, "pkgmgr_installer_set_cert_value(%d) failed.[%d]", i, res);
595 SignatureManager::CompareReferences(HashMap* pDistributorRefMap, HashMap* pAuthorRefMap)
597 TryReturn(__pContext, false, "__pContext is null.");
600 std::unique_ptr< IMapEnumerator > pEnum(pAuthorRefMap->GetMapEnumeratorN());
601 TryReturn(pEnum, false, "pEnum is null.");
603 while (pEnum->MoveNext() == E_SUCCESS)
605 String* pAuthorKey = static_cast< String* > (pEnum->GetKey());
606 TryReturn(pAuthorKey, false, "pAuthorKey is null.");
608 String* pAuthorValue = static_cast< String* > (pEnum->GetValue());
609 TryReturn(pAuthorValue, false, "pAuthorValue is failed.");
611 String* pDistValue = static_cast< String* >(pDistributorRefMap->GetValue(*pAuthorKey));
612 TryReturn(pDistValue, false, "pDistValue is null, failed to compare.");
614 AppLog("pAuthorValue=[%ls]", pAuthorValue->GetPointer());
615 AppLog("pDistValue=[%ls]", pDistValue->GetPointer());
616 if (*pAuthorValue != *pDistValue)
618 TryReturn(0, false, "Digest value does not be matched.");
621 String filePath = __pContext->__rootPath + L"/" + *pAuthorKey;
624 AppLog("filePath=[%ls]", filePath.GetPointer());
625 res = InstallerUtil::GetFileDigest(filePath, fileDigest);
626 TryReturn(res == true, false, "GetFileDigest() is failed.");
628 AppLog("fileDigest=[%ls]", fileDigest.GetPointer());
629 if (*pAuthorValue != fileDigest)
631 TryReturn(0, false, "Digest value does not be matched.");
639 SignatureManager::PrintCertValue(const String& certValue) const
641 const int certLength = 30;
645 certValue.SubString(0, certLength, startString);
646 certValue.SubString(certValue.GetLength() - certLength, certLength, endString);
647 AppLog("cert value, start with [%ls]", startString.GetPointer());
648 AppLog("cert value, end with [%ls]", endString.GetPointer());