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),
44 AddStep(&TaskCertificates::StepSetCertiInfo);
45 AddAbortStep(&TaskCertificates::StepAbortCertiInfo);
48 void TaskCertificates::StepSetCertiInfo()
50 LogDebug("StepSetCertiInfo");
52 if (pkgmgr_installer_create_certinfo_set_handle(&m_pkgHandle) < 0) {
53 LogError("pkgmgrInstallerCreateCertinfoSetHandle fail");
54 ThrowMsg(Exceptions::SetCertificateInfoFailed,
55 "Failed to create certificate handle");
58 SetCertiInfo(SIGNATURE_AUTHOR);
59 SetCertiInfo(SIGNATURE_DISTRIBUTOR);
61 if ((pkgmgr_installer_save_certinfo(
62 const_cast<char*>(DPL::ToUTF8String(
63 m_context.widgetConfig.tzPkgid).c_str()),
66 LogError("pkgmgrInstallerSaveCertinfo fail");
67 ThrowMsg(Exceptions::SetCertificateInfoFailed,
68 "Failed to Installer Save Certinfo");
70 LogDebug("Succeed to save Certinfo");
73 if (pkgmgr_installer_destroy_certinfo_set_handle(m_pkgHandle) < 0) {
74 LogError("pkgmgrInstallerDestroyCertinfoSetHandle fail");
77 m_context.job->UpdateProgress(
78 InstallerContext::INSTALL_SET_CERTINFO,
79 "Save certinfo to pkgmgr");
82 void TaskCertificates::SetCertiInfo(CertificateSource source)
84 LogDebug("Set CertiInfo to pkgmgr : " << source);
85 CertificateChainList certificateChainList;
86 m_context.wacSecurity.getCertificateChainList(certificateChainList, source);
88 FOREACH(it, certificateChainList)
90 LogDebug("Insert certinfo to pkgmgr structure");
92 ValidationCore::CertificateCollection chain;
93 if (false == chain.load(*it)) {
94 LogError("Chain is broken");
95 ThrowMsg(Exceptions::SetCertificateInfoFailed,
96 "Failed to Installer Save Certinfo");
100 LogError("Chain failed at sorting");
103 ValidationCore::CertificateList list = chain.getCertificateList();
105 FOREACH(certIt, list)
107 pkgmgr_instcert_type instCertType;
109 if (source == SIGNATURE_DISTRIBUTOR) {
111 Name(DPL::ToUTF8String(*(*certIt)->getOrganizationName()));
112 size_t found = Name.find("Tizen");
114 if (found != std::string::npos) {
115 LogDebug("Set SIGNATURE_DISTRIBUTOR");
116 if ((*certIt)->isRootCert()) {
117 instCertType = PM_SET_DISTRIBUTOR_ROOT_CERT;
119 if ((*certIt)->isCA()) {
120 instCertType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
122 instCertType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
126 LogDebug("Set SIGNATURE_DISTRIBUTOR2");
127 if ((*certIt)->isRootCert()) {
128 instCertType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
130 if ((*certIt)->isCA()) {
132 PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
134 instCertType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
139 LogDebug("set SIGNATURE_AUTHOR");
140 if ((*certIt)->isRootCert()) {
141 instCertType = PM_SET_AUTHOR_ROOT_CERT;
143 if ((*certIt)->isCA()) {
144 instCertType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
146 instCertType = PM_SET_AUTHOR_SIGNER_CERT;
150 LogDebug("cert type : " << instCertType);
151 if ((pkgmgr_installer_set_cert_value(
154 const_cast<char*>(((*certIt)->getBase64()).c_str()))) < 0)
156 LogError("pkgmgrInstallerSetCertValue fail");
157 ThrowMsg(Exceptions::SetCertificateInfoFailed,
158 "Failed to Set CertValue");
164 void TaskCertificates::StepAbortCertiInfo()
166 if ((pkgmgr_installer_delete_certinfo(
167 const_cast<char*>(DPL::ToUTF8String(
168 m_context.widgetConfig.tzPkgid).c_str()))) <
171 LogError("pkgmgr_installer_delete_certinfo fail");
174 } //namespace WidgetInstall