2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file task_certificates.cpp
18 * @author Leerang Song(leerang.song@samsung.com)
20 * @brief Implementation file for installer task certificates
22 #include <widget_install/task_certificates.h>
23 #include <widget_install/widget_install_context.h>
24 #include <widget_install/widget_install_errors.h>
25 #include <widget_install/job_widget_install.h>
26 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
27 #include <pkgmgr_installer.h>
28 #include <vcore/CryptoHash.h>
30 #include <dpl/log/log.h>
33 #include <dpl/foreach.h>
34 #include <dpl/sstream.h>
36 using namespace WrtDB;
39 namespace WidgetInstall {
40 TaskCertificates::TaskCertificates(InstallerContext& context) :
41 DPL::TaskDecl<TaskCertificates>(this),
45 AddStep(&TaskCertificates::StepSetCertiInfo);
46 AddAbortStep(&TaskCertificates::StepAbortCertiInfo);
49 void TaskCertificates::StepSetCertiInfo()
51 LogDebug("StepSetCertiInfo");
53 if (pkgmgr_installer_create_certinfo_set_handle(&m_pkgHandle) < 0) {
54 LogError("pkgmgrInstallerCreateCertinfoSetHandle fail");
55 ThrowMsg(Exceptions::SetCertificateInfoFailed,
56 "Failed to create certificate handle");
59 SetCertiInfo(SIGNATURE_AUTHOR);
60 SetCertiInfo(SIGNATURE_DISTRIBUTOR);
62 if ((pkgmgr_installer_save_certinfo(
63 const_cast<char*>(DPL::ToUTF8String(
64 m_context.widgetConfig.tzPkgid).c_str()),
67 LogError("pkgmgrInstallerSaveCertinfo fail");
68 ThrowMsg(Exceptions::SetCertificateInfoFailed,
69 "Failed to Installer Save Certinfo");
71 LogDebug("Succeed to save Certinfo");
74 if (pkgmgr_installer_destroy_certinfo_set_handle(m_pkgHandle) < 0) {
75 LogError("pkgmgrInstallerDestroyCertinfoSetHandle fail");
78 m_context.job->UpdateProgress(
79 InstallerContext::INSTALL_SET_CERTINFO,
80 "Save certinfo to pkgmgr");
83 void TaskCertificates::SetCertiInfo(CertificateSource source)
85 LogDebug("Set CertiInfo to pkgmgr : " << source);
86 CertificateChainList certificateChainList;
87 m_context.wacSecurity.getCertificateChainList(certificateChainList, source);
89 FOREACH(it, certificateChainList)
91 LogDebug("Insert certinfo to pkgmgr structure");
93 ValidationCore::CertificateCollection chain;
94 if (false == chain.load(*it)) {
95 LogError("Chain is broken");
96 ThrowMsg(Exceptions::SetCertificateInfoFailed,
97 "Failed to Installer Save Certinfo");
101 LogError("Chain failed at sorting");
104 ValidationCore::CertificateList list = chain.getCertificateList();
106 FOREACH(certIt, list)
108 pkgmgr_instcert_type instCertType;
110 if (source == SIGNATURE_DISTRIBUTOR) {
112 Name(DPL::ToUTF8String(*(*certIt)->getOrganizationName()));
113 size_t found = Name.find("Tizen");
115 if (found != std::string::npos) {
116 LogDebug("Set SIGNATURE_DISTRIBUTOR");
117 if ((*certIt)->isRootCert()) {
118 instCertType = PM_SET_DISTRIBUTOR_ROOT_CERT;
120 if ((*certIt)->isCA()) {
121 instCertType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
123 instCertType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
127 LogDebug("Set SIGNATURE_DISTRIBUTOR2");
128 if ((*certIt)->isRootCert()) {
129 instCertType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
131 if ((*certIt)->isCA()) {
133 PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
135 instCertType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
140 LogDebug("set SIGNATURE_AUTHOR");
141 if ((*certIt)->isRootCert()) {
142 instCertType = PM_SET_AUTHOR_ROOT_CERT;
144 if ((*certIt)->isCA()) {
145 instCertType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
147 instCertType = PM_SET_AUTHOR_SIGNER_CERT;
151 LogDebug("cert type : " << instCertType);
152 if ((pkgmgr_installer_set_cert_value(
155 const_cast<char*>(((*certIt)->getBase64()).c_str()))) < 0)
157 LogError("pkgmgrInstallerSetCertValue fail");
158 ThrowMsg(Exceptions::SetCertificateInfoFailed,
159 "Failed to Set CertValue");
165 void TaskCertificates::StepAbortCertiInfo()
167 if ((pkgmgr_installer_delete_certinfo(
168 const_cast<char*>(DPL::ToUTF8String(
169 m_context.widgetConfig.tzPkgid).c_str()))) <
172 LogError("pkgmgr_installer_delete_certinfo fail");
175 } //namespace WidgetInstall