Fixed crash during saving certificate.
[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     m_pkgHandle(NULL)
44 {
45     AddStep(&TaskCertificates::StepSetCertiInfo);
46     AddAbortStep(&TaskCertificates::StepAbortCertiInfo);
47 }
48
49 void TaskCertificates::StepSetCertiInfo()
50 {
51     LogDebug("StepSetCertiInfo");
52
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");
57     }
58
59     SetCertiInfo(SIGNATURE_AUTHOR);
60     SetCertiInfo(SIGNATURE_DISTRIBUTOR);
61
62     if ((pkgmgr_installer_save_certinfo(
63              const_cast<char*>(DPL::ToUTF8String(
64                                    m_context.widgetConfig.tzPkgid).c_str()),
65              m_pkgHandle)) < 0)
66     {
67         LogError("pkgmgrInstallerSaveCertinfo fail");
68         ThrowMsg(Exceptions::SetCertificateInfoFailed,
69                  "Failed to Installer Save Certinfo");
70     } else {
71         LogDebug("Succeed to save Certinfo");
72     }
73
74     if (pkgmgr_installer_destroy_certinfo_set_handle(m_pkgHandle) < 0) {
75         LogError("pkgmgrInstallerDestroyCertinfoSetHandle fail");
76     }
77
78     m_context.job->UpdateProgress(
79             InstallerContext::INSTALL_SET_CERTINFO,
80             "Save certinfo to pkgmgr");
81 }
82
83 void TaskCertificates::SetCertiInfo(CertificateSource source)
84 {
85     LogDebug("Set CertiInfo to pkgmgr : " << source);
86     CertificateChainList certificateChainList;
87     m_context.wacSecurity.getCertificateChainList(certificateChainList, source);
88
89     FOREACH(it, certificateChainList)
90     {
91         LogDebug("Insert certinfo to pkgmgr structure");
92
93         ValidationCore::CertificateCollection chain;
94
95         if (false == chain.load(*it)) {
96             LogError("Chain is broken");
97             ThrowMsg(Exceptions::SetCertificateInfoFailed,
98                      "Failed to Installer Save Certinfo");
99         }
100
101         if (!chain.sort()) {
102             LogError("Chain failed at sorting");
103         }
104
105         ValidationCore::CertificateList list = chain.getCertificateList();
106
107         FOREACH(certIt, list)
108         {
109             pkgmgr_instcert_type instCertType;
110
111             if (source == SIGNATURE_DISTRIBUTOR) {
112                 bool distributor1 = false;
113                 if (!(*certIt)->getCommonName().IsNull()) {
114                     std::string
115                         Name(DPL::ToUTF8String(*(*certIt)->getCommonName()));
116                     std::string tizenStr("Tizen");
117                     if (0 == Name.compare(0, tizenStr.length(), tizenStr)) {
118                         distributor1 = true;
119                     }
120                 }
121
122                 if (distributor1) {
123                     LogDebug("Set SIGNATURE_DISTRIBUTOR");
124                     if ((*certIt)->isRootCert()) {
125                         instCertType = PM_SET_DISTRIBUTOR_ROOT_CERT;
126                     } else {
127                         if ((*certIt)->isCA()) {
128                             instCertType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
129                         } else {
130                             instCertType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
131                         }
132                     }
133                 } else {
134                     LogDebug("Set SIGNATURE_DISTRIBUTOR2");
135                     if ((*certIt)->isRootCert()) {
136                         instCertType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
137                     } else {
138                         if ((*certIt)->isCA()) {
139                             instCertType =
140                                 PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
141                         } else {
142                             instCertType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
143                         }
144                     }
145                 }
146             } else {
147                 LogDebug("set SIGNATURE_AUTHOR");
148                 if ((*certIt)->isRootCert()) {
149                     instCertType = PM_SET_AUTHOR_ROOT_CERT;
150                 } else {
151                     if ((*certIt)->isCA()) {
152                         instCertType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
153                     } else {
154                         instCertType = PM_SET_AUTHOR_SIGNER_CERT;
155                     }
156                 }
157             }
158             LogDebug("cert type : " << instCertType);
159             if ((pkgmgr_installer_set_cert_value(
160                      m_pkgHandle,
161                      instCertType,
162                      const_cast<char*>(((*certIt)->getBase64()).c_str()))) < 0)
163             {
164                 LogError("pkgmgrInstallerSetCertValue fail");
165                 ThrowMsg(Exceptions::SetCertificateInfoFailed,
166                          "Failed to Set CertValue");
167             }
168         }
169     }
170 }
171
172 void TaskCertificates::StepAbortCertiInfo()
173 {
174     if ((pkgmgr_installer_delete_certinfo(
175              const_cast<char*>(DPL::ToUTF8String(
176                                    m_context.widgetConfig.tzPkgid).c_str()))) <
177         0)
178     {
179         LogError("pkgmgr_installer_delete_certinfo fail");
180     }
181 }
182 } //namespace WidgetInstall
183 } //namespace Jobs