[Release] wrt-installer_0.1.28
[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         if (false == chain.load(*it)) {
95             LogError("Chain is broken");
96             ThrowMsg(Exceptions::SetCertificateInfoFailed,
97                      "Failed to Installer Save Certinfo");
98         }
99
100         if (!chain.sort()) {
101             LogError("Chain failed at sorting");
102         }
103
104         ValidationCore::CertificateList list = chain.getCertificateList();
105
106         FOREACH(certIt, list)
107         {
108             pkgmgr_instcert_type instCertType;
109
110             if (source == SIGNATURE_DISTRIBUTOR) {
111                 std::string
112                 Name(DPL::ToUTF8String(*(*certIt)->getOrganizationName()));
113                 size_t found = Name.find("Tizen");
114
115                 if (found != std::string::npos) {
116                     LogDebug("Set SIGNATURE_DISTRIBUTOR");
117                     if ((*certIt)->isRootCert()) {
118                         instCertType = PM_SET_DISTRIBUTOR_ROOT_CERT;
119                     } else {
120                         if ((*certIt)->isCA()) {
121                             instCertType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
122                         } else {
123                             instCertType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
124                         }
125                     }
126                 } else {
127                     LogDebug("Set SIGNATURE_DISTRIBUTOR2");
128                     if ((*certIt)->isRootCert()) {
129                         instCertType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
130                     } else {
131                         if ((*certIt)->isCA()) {
132                             instCertType =
133                                 PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
134                         } else {
135                             instCertType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
136                         }
137                     }
138                 }
139             } else {
140                 LogDebug("set SIGNATURE_AUTHOR");
141                 if ((*certIt)->isRootCert()) {
142                     instCertType = PM_SET_AUTHOR_ROOT_CERT;
143                 } else {
144                     if ((*certIt)->isCA()) {
145                         instCertType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
146                     } else {
147                         instCertType = PM_SET_AUTHOR_SIGNER_CERT;
148                     }
149                 }
150             }
151             LogDebug("cert type : " << instCertType);
152             if ((pkgmgr_installer_set_cert_value(
153                      m_pkgHandle,
154                      instCertType,
155                      const_cast<char*>(((*certIt)->getBase64()).c_str()))) < 0)
156             {
157                 LogError("pkgmgrInstallerSetCertValue fail");
158                 ThrowMsg(Exceptions::SetCertificateInfoFailed,
159                          "Failed to Set CertValue");
160             }
161         }
162     }
163 }
164
165 void TaskCertificates::StepAbortCertiInfo()
166 {
167     if ((pkgmgr_installer_delete_certinfo(
168              const_cast<char*>(DPL::ToUTF8String(
169                                    m_context.widgetConfig.tzPkgid).c_str()))) <
170         0)
171     {
172         LogError("pkgmgr_installer_delete_certinfo fail");
173     }
174 }
175 } //namespace WidgetInstall
176 } //namespace Jobs