[Release] wrt-installer_0.0.90
[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
41 TaskCertificates::TaskCertificates(InstallerContext& context) :
42     DPL::TaskDecl<TaskCertificates>(this),
43     m_context(context)
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.pkgName).c_str()),
65                     m_pkgHandle)) < 0) {
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
78 void TaskCertificates::SetCertiInfo(CertificateSource source)
79 {
80     LogDebug("Set CertiInfo to pkgmgr : " << source);
81     CertificateChainList certificateChainList;
82     m_context.wacSecurity.getCertificateChainList(certificateChainList, source);
83
84     FOREACH(it, certificateChainList)
85     {
86         LogDebug("Insert certinfo to pkgmgr structure");
87
88         ValidationCore::CertificateCollection chain;
89         if (false == chain.load(*it)) {
90             LogError("Chain is broken");
91             ThrowMsg(Exceptions::SetCertificateInfoFailed,
92                     "Failed to Installer Save Certinfo");
93         }
94
95         if (!chain.sort()){
96             LogError("Chain failed at sorting");
97         }
98
99         ValidationCore::CertificateList list = chain.getCertificateList();
100
101         FOREACH(certIt, list) 
102         {
103             pkgmgr_instcert_type instCertType;
104
105             if (source == SIGNATURE_DISTRIBUTOR) {
106                 std::string
107                     Name(DPL::ToUTF8String(*(*certIt)->getOrganizationName()));
108                 size_t found = Name.find("Tizen");
109
110                 if (found != std::string::npos) {
111                     LogDebug("Set SIGNATURE_DISTRIBUTOR");
112                     if ((*certIt)->isRootCert()) {
113                         instCertType = PM_SET_DISTRIBUTOR_ROOT_CERT;
114                     } else {
115                         if ((*certIt)->isCA()) {
116                             instCertType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
117                         } else {
118                             instCertType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
119                         }
120                     }
121                 } else {
122                     LogDebug("Set SIGNATURE_DISTRIBUTOR2");
123                     if ((*certIt)->isRootCert()) {
124                         instCertType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
125                     } else {
126                         if ((*certIt)->isCA()) {
127                             instCertType = PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
128                         } else {
129                             instCertType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
130                         }
131                     }
132                 }
133             } else {
134                 LogDebug("set SIGNATURE_AUTHOR");
135                 if ((*certIt)->isRootCert()) {
136                     instCertType = PM_SET_AUTHOR_ROOT_CERT;
137                 } else {
138                     if ((*certIt)->isCA()) {
139                         instCertType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
140                     } else {
141                         instCertType = PM_SET_AUTHOR_SIGNER_CERT;
142                     }
143                 }
144             }
145             LogDebug("cert type : " << instCertType);
146             if((pkgmgr_installer_set_cert_value(
147                             m_pkgHandle,
148                             instCertType,
149                             const_cast<char*>(((*certIt)->getBase64()).c_str()))) < 0) {
150                 LogError("pkgmgrInstallerSetCertValue fail");
151                 ThrowMsg(Exceptions::SetCertificateInfoFailed,
152                         "Failed to Set CertValue");
153             }
154         }
155     }
156 }
157
158 void TaskCertificates::StepAbortCertiInfo()
159 {
160     if ((pkgmgr_installer_delete_certinfo(
161                     const_cast<char*>(DPL::ToUTF8String(
162                             m_context.widgetConfig.pkgName).c_str()))) < 0) {
163         LogError("pkgmgr_installer_delete_certinfo fail");
164     }
165 }
166
167 } //namespace WidgetInstall
168 } //namespace Jobs