b4a67f7fa8406d1b1745dd54439c3020cf53d96d
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_certificates.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file    task_certificates.cpp
18  * @author  Leerang Song(leerang.song@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task certificates
21  */
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>
29
30 #include <dpl/log/log.h>
31 #include <dlog.h>
32 #include <sstream>
33 #include <dpl/foreach.h>
34 #include <dpl/sstream.h>
35
36 using namespace WrtDB;
37
38 namespace Jobs {
39 namespace WidgetInstall {
40 TaskCertificates::TaskCertificates(InstallerContext& context) :
41     DPL::TaskDecl<TaskCertificates>(this),
42     m_context(context)
43 {
44     AddStep(&TaskCertificates::StepSetCertiInfo);
45     AddAbortStep(&TaskCertificates::StepAbortCertiInfo);
46 }
47
48 void TaskCertificates::StepSetCertiInfo()
49 {
50     LogDebug("StepSetCertiInfo");
51
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");
56     }
57
58     SetCertiInfo(SIGNATURE_AUTHOR);
59     SetCertiInfo(SIGNATURE_DISTRIBUTOR);
60
61     if ((pkgmgr_installer_save_certinfo(
62              const_cast<char*>(DPL::ToUTF8String(
63                                    m_context.widgetConfig.tzPkgid).c_str()),
64              m_pkgHandle)) < 0)
65     {
66         LogError("pkgmgrInstallerSaveCertinfo fail");
67         ThrowMsg(Exceptions::SetCertificateInfoFailed,
68                  "Failed to Installer Save Certinfo");
69     } else {
70         LogDebug("Succeed to save Certinfo");
71     }
72
73     if (pkgmgr_installer_destroy_certinfo_set_handle(m_pkgHandle) < 0) {
74         LogError("pkgmgrInstallerDestroyCertinfoSetHandle fail");
75     }
76
77     m_context.job->UpdateProgress(
78             InstallerContext::INSTALL_SET_CERTINFO,
79             "Save certinfo to pkgmgr");
80 }
81
82 void TaskCertificates::SetCertiInfo(CertificateSource source)
83 {
84     LogDebug("Set CertiInfo to pkgmgr : " << source);
85     CertificateChainList certificateChainList;
86     m_context.wacSecurity.getCertificateChainList(certificateChainList, source);
87
88     FOREACH(it, certificateChainList)
89     {
90         LogDebug("Insert certinfo to pkgmgr structure");
91
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");
97         }
98
99         if (!chain.sort()) {
100             LogError("Chain failed at sorting");
101         }
102
103         ValidationCore::CertificateList list = chain.getCertificateList();
104
105         FOREACH(certIt, list)
106         {
107             pkgmgr_instcert_type instCertType;
108
109             if (source == SIGNATURE_DISTRIBUTOR) {
110                 std::string
111                 Name(DPL::ToUTF8String(*(*certIt)->getOrganizationName()));
112                 size_t found = Name.find("Tizen");
113
114                 if (found != std::string::npos) {
115                     LogDebug("Set SIGNATURE_DISTRIBUTOR");
116                     if ((*certIt)->isRootCert()) {
117                         instCertType = PM_SET_DISTRIBUTOR_ROOT_CERT;
118                     } else {
119                         if ((*certIt)->isCA()) {
120                             instCertType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
121                         } else {
122                             instCertType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
123                         }
124                     }
125                 } else {
126                     LogDebug("Set SIGNATURE_DISTRIBUTOR2");
127                     if ((*certIt)->isRootCert()) {
128                         instCertType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
129                     } else {
130                         if ((*certIt)->isCA()) {
131                             instCertType =
132                                 PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
133                         } else {
134                             instCertType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
135                         }
136                     }
137                 }
138             } else {
139                 LogDebug("set SIGNATURE_AUTHOR");
140                 if ((*certIt)->isRootCert()) {
141                     instCertType = PM_SET_AUTHOR_ROOT_CERT;
142                 } else {
143                     if ((*certIt)->isCA()) {
144                         instCertType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
145                     } else {
146                         instCertType = PM_SET_AUTHOR_SIGNER_CERT;
147                     }
148                 }
149             }
150             LogDebug("cert type : " << instCertType);
151             if ((pkgmgr_installer_set_cert_value(
152                      m_pkgHandle,
153                      instCertType,
154                      const_cast<char*>(((*certIt)->getBase64()).c_str()))) < 0)
155             {
156                 LogError("pkgmgrInstallerSetCertValue fail");
157                 ThrowMsg(Exceptions::SetCertificateInfoFailed,
158                          "Failed to Set CertValue");
159             }
160         }
161     }
162 }
163
164 void TaskCertificates::StepAbortCertiInfo()
165 {
166     if ((pkgmgr_installer_delete_certinfo(
167              const_cast<char*>(DPL::ToUTF8String(
168                                    m_context.widgetConfig.tzPkgid).c_str()))) <
169         0)
170     {
171         LogError("pkgmgr_installer_delete_certinfo fail");
172     }
173 }
174 } //namespace WidgetInstall
175 } //namespace Jobs