Update wrt-installer_0.0.69
[platform/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     SetCertiInfo(SIGNATURE_AUTHOR);
53     SetCertiInfo(SIGNATURE_DISTRIBUTOR);
54 }
55
56 void TaskCertificates::SetCertiInfo(CertificateSource source)
57 {
58     LogDebug("Set CertiInfo to pkgmgr : " << source);
59     CertificateChainList certificateChainList;
60     m_context.wacSecurity.getCertificateChainList(certificateChainList, source);
61
62     pkgmgr_instcertinfo_h handle;
63
64     FOREACH(it, certificateChainList)
65     {
66         LogDebug("Insert certinfo to pkgmgr db");
67
68         ValidationCore::CertificateCollection chain;
69         if (false == chain.load(*it)) {
70             LogError("Chain is broken");
71         }
72
73         chain.sort();
74
75         ValidationCore::CertificateList list = chain.getCertificateList();
76
77         FOREACH(certIt, list) 
78         {
79             ValidationCore::Crypto::Hash::SHA1 sha1;
80             sha1.Append((*certIt)->getDER());
81             sha1.Finish();
82             std::string sha1String = sha1.ToBase64String();
83
84             if (pkgmgr_installer_create_certinfo_set_handle(&handle)< 0) {
85                 LogError("pkgmgrInstallerCreateCertinfoSetHandle fail");
86                 ThrowMsg(Exceptions::SetCertificateInfoFailed,
87                         "Failed to create certificate handle");
88             }
89
90             if (source == SIGNATURE_DISTRIBUTOR) {
91                 if((pkgmgr_installer_set_cert_value(
92                                 handle,
93                                 PM_SET_DISTRIBUTOR_SIGNER_CERT,
94                                 const_cast<char*>(sha1String.c_str()))) < 0) {
95                     LogError("pkgmgrInstallerSetCertValue fail");
96                     ThrowMsg(Exceptions::SetCertificateInfoFailed,
97                             "Failed to Set CertValue");
98                 }
99             }
100             else {
101                 if((pkgmgr_installer_set_cert_value(
102                                 handle,
103                                 PM_SET_AUTHOR_SIGNER_CERT,
104                                 const_cast<char*>(sha1String.c_str()))) < 0) {
105                     LogError("pkgmgrInstallerSetCertValue fail");
106                     ThrowMsg(Exceptions::SetCertificateInfoFailed,
107                             "Failed to Installer Set CertValue");
108                 }
109             }
110
111             if ((pkgmgr_installer_save_certinfo(
112                             const_cast<char*>(DPL::ToUTF8String(
113                                     *m_context.widgetConfig.pkgname).c_str()),
114                             handle)) < 0) {
115                 LogError("pkgmgrInstallerSaveCertinfo fail");
116                 ThrowMsg(Exceptions::SetCertificateInfoFailed,
117                         "Failed to Installer Save Certinfo");
118             } else {
119                 LogDebug("Succeed to save Certinfo");
120             }
121
122             if (pkgmgr_installer_destroy_certinfo_set_handle(handle) < 0) {
123                 LogError("pkgmgrInstallerDestroyCertinfoSetHandle fail");
124             }
125         }
126     }
127 }
128
129 void TaskCertificates::StepAbortCertiInfo()
130 {
131     pkgmgr_instcertinfo_h handle;
132
133     if ((pkgmgr_installer_delete_certinfo(
134             const_cast<char*>(DPL::ToUTF8String(
135                 *m_context.widgetConfig.pkgname).c_str()))) < 0) {
136         LogError("pkgmgr_installer_delete_certinfo fail");
137     }
138 }
139 }
140 }