c6e7d7a5cf678da0f95ef5983c9e461d50f6eb2c
[framework/osp/installer.git] / src / Step / SignatureStep.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        SignatureStep.cpp
19  * @brief       This is the implementation file for %SignatureStep class.
20  */
21
22 #include <FIoFile.h>
23 #include <FApp_PackageInfoImpl.h>
24 #include <FBase_StringConverter.h>
25
26 #include "InstallationContext.h"
27 #include "PrivilegeHandler.h"
28 #include "SignatureHandler.h"
29 #include "SignatureStep.h"
30
31 using namespace Osp::App;
32 using namespace Osp::Base;
33 using namespace Osp::Base::Collection;
34 using namespace Osp::Io;
35
36 SignatureStep::SignatureStep(void)
37 :__state(STATE_SIGNER_CERT)
38 ,__pContext(null)
39 ,__pSignatureManager(null)
40 {
41 }
42
43 SignatureStep::~SignatureStep(void)
44 {
45         delete __pSignatureManager;
46 }
47
48 InstallerError
49 SignatureStep::Run(InstallationContext* pContext)
50 {
51         InstallerError error = INSTALLER_ERROR_NONE;
52         AppLogTag(OSP_INSTALLER, "------------------------------------------");
53         AppLogTag(OSP_INSTALLER, " SignatureStep - START");
54         AppLogTag(OSP_INSTALLER, "------------------------------------------");
55
56         __pContext = pContext;
57
58         // signature.xml for beta
59         FileAttributes attr;
60         result r = E_SUCCESS;
61
62         r = File::GetAttributes(__pContext->GetSignatureXmlPath(), attr);
63         if (r != E_SUCCESS)
64         {
65                 AppLogTag(OSP_INSTALLER, "Signature file not found. path = [%ls]\n", __pContext->GetSignatureXmlPath().GetPointer());
66                 return INSTALLER_ERROR_NONE;
67         }
68
69         while (true)
70         {
71                 switch (__state)
72                 {
73                 case STATE_SIGNER_CERT:
74                         AppLogTag(OSP_INSTALLER, "[STATE_SIGNER_CERT]");
75                         error = OnStateSignerCert();
76                         break;
77
78                 case STATE_CERT_CHAIN:
79                         AppLogTag(OSP_INSTALLER, "[STATE_CERT_CHAIN]");
80                         error = OnStateCertChain();
81                         break;
82
83                 case STATE_ROOT_CERT:
84                         AppLogTag(OSP_INSTALLER, "[STATE_ROOT_CERT]");
85                         error = OnStateRootCert();
86                         break;
87
88                 case STATE_DONE:
89                         AppLogTag(OSP_INSTALLER, "[STATE_DONE]");
90                         error = OnStateDone();
91                         break;
92
93                 default:
94                         break;
95                 }
96
97                 if (error != INSTALLER_ERROR_NONE)
98                 {
99                         AppLogTag(OSP_INSTALLER, "SignatureStep::Run - ErrorType [%d]\n", error);
100                         //fprintf(stderr, "SignatureStep::Run - ErrorType [%d]\n", error);
101                         break;
102                 }
103
104                 if (__state > STATE_DONE)
105                 {
106                         AppLogTag(OSP_INSTALLER, "------------------------------------------");
107                         AppLogTag(OSP_INSTALLER, " SignatureStep - END");
108                         AppLogTag(OSP_INSTALLER, "------------------------------------------");
109                         break;
110                 }
111         }
112
113         return error;
114 }
115
116 void
117 SignatureStep::GoNextState(void)
118 {
119         __state++;
120 }
121
122 InstallerError
123 SignatureStep::OnStateSignerCert(void)
124 {
125         InstallerError error = INSTALLER_ERROR_NONE;
126         bool ret = true;
127
128         __pSignatureManager = new SignatureManager();
129         TryCatch(__pSignatureManager, error = INSTALLER_ERROR_MEMORY, "[osp-installer] __pSignatureManager is null.");
130
131         ret = __pSignatureManager->Construct(__pContext);
132         TryCatch(ret == true, error = INSTALLER_ERROR_INTERNAL_STATE, "[osp-installer] Construct() failed");
133
134         ret = __pSignatureManager->SetSignature();
135         TryCatch(ret == true, error = INSTALLER_ERROR_INVALID_SIGNATURE, "[osp-installer] SetSignature() failed");
136
137 CATCH:
138         GoNextState();
139         return error;
140 }
141
142 InstallerError
143 SignatureStep::OnStateCertChain(void)
144 {
145         InstallerError error = INSTALLER_ERROR_NONE;
146         bool ret = true;
147         int rootCert = ROOT_CERTIFICATE_PUBLIC;
148
149         ret = __pSignatureManager->AddCert(ROOT_CERTIFICATE_DEVELOPER);
150         TryCatch(ret == true, error = INSTALLER_ERROR_INVALID_SIGNATURE, "[osp-installer] AddCert(DEVELOPER_ROOT_CERTIFICATE) failed");
151
152         ret = __pSignatureManager->VerifyChain(ROOT_CERTIFICATE_DEVELOPER);
153         TryCatch(ret == true, error = INSTALLER_ERROR_INVALID_SIGNATURE, "[osp-installer] VerifyChain(DEVELOPER_ROOT_CERTIFICATE) failed");
154
155         for(rootCert = ROOT_CERTIFICATE_PUBLIC; rootCert <= ROOT_CERTIFICATE_PRIVATE; rootCert++)
156         {
157                 ret = __pSignatureManager->AddCert((RootCertificateType)rootCert);
158                 TryCatch(ret == true, error = INSTALLER_ERROR_INVALID_SIGNATURE, "[osp-installer] AddCert() failed");
159
160                 ret = __pSignatureManager->VerifyChain((RootCertificateType)rootCert);
161                 if (ret == true)
162                 {
163                         AppLogTag(OSP_INSTALLER, "VerifyChain() success(Cert = [%d])", rootCert);
164                         __pContext->SetRootCertType((RootCertificateType)rootCert);
165                         error = INSTALLER_ERROR_NONE;
166                         break;
167                 }
168                 else
169                 {
170                         AppLogTag(OSP_INSTALLER, "VerifyChain() fail(Cert = [%d])", rootCert);
171                         error = INSTALLER_ERROR_INVALID_SIGNATURE;
172                 }
173         }
174
175 CATCH:
176         GoNextState();
177         return error;
178 }
179
180 InstallerError
181 SignatureStep::OnStateRootCert(void)
182 {
183         InstallerError error = INSTALLER_ERROR_NONE;
184         result r = E_SUCCESS;
185         String privileges;
186         String hmacPrivileges;
187
188         const ArrayList* pPrivilegeList = __pContext->GetPrivilegeList();
189         RootCertificateType certType = __pContext->GetRootCertType();
190         _PackageInfoImpl* pPackageInfoImpl = __pContext->GetPackageInfoImpl();
191         String appId = pPackageInfoImpl->GetAppId();
192         int apiVisibility = GetApiVisibility(certType);
193
194         AppLogTag(OSP_INSTALLER, "AppId = [%ls], CertType = [%d], ApiVisibility = [%d]", appId.GetPointer(), certType, apiVisibility);
195
196         r = PrivilegeHandler::GenerateCipherPrivilege(appId, *pPrivilegeList, apiVisibility, privileges, hmacPrivileges);
197         if (IsFailed(r))
198         {
199                 fprintf(stderr, "PrivilegeHandler::GenerateCipherPrivilege is failded. [%ls][%ls][%ls]\n", appId.GetPointer(), privileges.GetPointer(), hmacPrivileges.GetPointer());
200         }
201         TryCatch(!IsFailed(r), error = INSTALLER_ERROR_PRIVILEGE, "[osp-installer] privMgr.GeneratePrivilegeString() failed");
202
203         pPackageInfoImpl->SetPrivilegesValue(privileges, hmacPrivileges);
204         pPackageInfoImpl->SetCertType(apiVisibility);
205
206 CATCH:
207         GoNextState();
208         return error;
209 }
210
211 InstallerError
212 SignatureStep::OnStateDone(void)
213 {
214         InstallerError error = INSTALLER_ERROR_NONE;
215
216         GoNextState();
217         return error;
218 }
219
220 int
221 SignatureStep::GetApiVisibility(RootCertificateType certType)
222 {
223         int apiVisibility = _API_VISIBILITY_NONE;
224
225         if (certType == ROOT_CERTIFICATE_PUBLIC)
226         {
227                 apiVisibility = _API_VISIBILITY_PUBLIC;
228         }
229         else if (certType == ROOT_CERTIFICATE_PARTNER)
230         {
231                 apiVisibility = _API_VISIBILITY_PARTNER;
232         }
233         else if (certType == ROOT_CERTIFICATE_PARTNER_OPERATOR)
234         {
235                 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
236         }
237         else if (certType == ROOT_CERTIFICATE_PARTNER_MANUFACTURER)
238         {
239                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
240         }
241         else if (certType == ROOT_CERTIFICATE_PRIVATE)
242         {
243                 apiVisibility = _API_VISIBILITY_PRIVATE;
244         }
245
246         return apiVisibility;
247 }