SignatureValidator and RDS feature added.
[platform/framework/native/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 <FBase_StringConverter.h>
24
25 #include "InstallationContext.h"
26 #include "PrivilegeHandler.h"
27 #include "SignatureHandler.h"
28 #include "SignatureStep.h"
29
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Io;
34
35 SignatureStep::SignatureStep(void)
36 :__state(STATE_SIGNER_CERT)
37 ,__pContext(null)
38 ,__pSignatureManager(null)
39 {
40 }
41
42 SignatureStep::~SignatureStep(void)
43 {
44         delete __pSignatureManager;
45 }
46
47 InstallerError
48 SignatureStep::Run(InstallationContext* pContext)
49 {
50         InstallerError error = INSTALLER_ERROR_NONE;
51         AppLog(" SignatureStep - START");
52
53         __pContext = pContext;
54
55         if (__pContext->__isVerificationMode == false)
56         {
57                 AppLog("no signature file[%ls]", __pContext->GetSignatureXmlPath().GetPointer());
58                 return INSTALLER_ERROR_NONE;
59         }
60
61         while (true)
62         {
63                 switch (__state)
64                 {
65                 case STATE_SIGNER_CERT:
66                         AppLog("[STATE_SIGNER_CERT]");
67                         error = OnStateSignerCert();
68                         break;
69
70                 case STATE_CERT_CHAIN:
71                         AppLog("[STATE_CERT_CHAIN]");
72                         error = OnStateCertChain();
73                         break;
74
75                 case STATE_ROOT_CERT:
76                         AppLog("[STATE_ROOT_CERT]");
77                         error = OnStateRootCert();
78                         break;
79
80                 case STATE_DONE:
81                         AppLog("[STATE_DONE]");
82                         error = OnStateDone();
83                         break;
84
85                 default:
86                         break;
87                 }
88
89                 if (error != INSTALLER_ERROR_NONE)
90                 {
91                         break;
92                 }
93
94                 if (__state > STATE_DONE)
95                 {
96                         AppLog(" SignatureStep - END");
97                         break;
98                 }
99         }
100
101         return error;
102 }
103
104 void
105 SignatureStep::GoNextState(void)
106 {
107         __state++;
108 }
109
110 InstallerError
111 SignatureStep::OnStateSignerCert(void)
112 {
113         InstallerError error = INSTALLER_ERROR_NONE;
114         bool ret = true;
115
116         __pSignatureManager = new (std::nothrow) SignatureManager();
117         TryCatch(__pSignatureManager, error = INSTALLER_ERROR_OUT_OF_MEMORY, "__pSignatureManager is null.");
118
119         ret = __pSignatureManager->Construct(__pContext);
120         TryCatch(ret == true, error = INSTALLER_ERROR_INTERNAL_STATE, "Construct() failed");
121
122         ret = __pSignatureManager->SetSignature();
123         TryCatch(ret == true, error = INSTALLER_ERROR_SIGNATURE_INVALID, "SetSignature() failed");
124
125         // test for signature validator
126         ret = __pSignatureManager->ValidateSignatures();
127         fprintf(stderr, "  ## __pSignatureManager->ValidateSignatures() result = [%d]\n", ret);
128
129 CATCH:
130         GoNextState();
131         return error;
132 }
133
134 InstallerError
135 SignatureStep::OnStateCertChain(void)
136 {
137         InstallerError error = INSTALLER_ERROR_NONE;
138         bool ret = true;
139
140         ret = __pSignatureManager->AddCert();
141         TryCatch(ret == true, error = INSTALLER_ERROR_CERTIFICATE_CHAIN_VERIFICATION_FAILED, "AddCert(DEVELOPER_ROOT_CERTIFICATE) failed");
142
143         ret = __pSignatureManager->VerifyChain();
144         if (__pContext->__isPreloaded == true)
145         {
146                 fprintf(stderr, "  ## VerifyChain() result = [%d]\n", ret);
147         }
148         else
149         {
150                 TryCatch(ret == true, error = INSTALLER_ERROR_CERTIFICATE_CHAIN_VERIFICATION_FAILED, "VerifyChain() failed");
151         }
152
153 CATCH:
154         GoNextState();
155         return error;
156 }
157
158 InstallerError
159 SignatureStep::OnStateRootCert(void)
160 {
161         InstallerError error = INSTALLER_ERROR_NONE;
162         result r = E_SUCCESS;
163         String privileges;
164         String hmacPrivileges;
165         ArrayList stringPrivilegeList;
166         stringPrivilegeList.Construct();
167
168         const ArrayList* pPrivilegeList = __pContext->GetPrivilegeList();
169         RootCertificateType certType = __pContext->__rootCertType;
170         PackageId packageId = __pContext->__packageId;
171         int apiVisibility = GetApiVisibility(certType);
172
173         AppLog("PackageId = [%ls], CertType = [%d], ApiVisibility = [%d]", packageId.GetPointer(), certType, apiVisibility);
174
175         if (pPrivilegeList != null)
176         {
177                 r = PrivilegeHandler::GenerateCipherPrivilege(packageId, *pPrivilegeList, apiVisibility, privileges, hmacPrivileges, stringPrivilegeList);
178                 TryCatch(!IsFailed(r), error = INSTALLER_ERROR_PRIVILEGE_INVALID, "privMgr.GeneratePrivilegeString() failed");
179         }
180
181         __pContext->__privileges = privileges;
182         __pContext->__hmacPrivileges = hmacPrivileges;
183         __pContext->__certType = apiVisibility;
184         __pContext->__pStringPrivilegeList = new ArrayList;
185         __pContext->__pStringPrivilegeList->Construct(stringPrivilegeList);
186
187 CATCH:
188         GoNextState();
189         return error;
190 }
191
192 InstallerError
193 SignatureStep::OnStateDone(void)
194 {
195         InstallerError error = INSTALLER_ERROR_NONE;
196
197         GoNextState();
198         return error;
199 }
200
201 int
202 SignatureStep::GetApiVisibility(RootCertificateType certType)
203 {
204         int apiVisibility = _API_VISIBILITY_NONE;
205
206         if (certType == ROOT_CERTIFICATE_PUBLIC)
207         {
208                 apiVisibility = _API_VISIBILITY_PUBLIC;
209         }
210         else if (certType == ROOT_CERTIFICATE_PARTNER)
211         {
212                 apiVisibility = _API_VISIBILITY_PARTNER;
213         }
214         else if (certType == ROOT_CERTIFICATE_PARTNER_OPERATOR)
215         {
216                 apiVisibility = _API_VISIBILITY_PARTNER_OPERATOR;
217         }
218         else if (certType == ROOT_CERTIFICATE_PARTNER_MANUFACTURER)
219         {
220                 apiVisibility = _API_VISIBILITY_PARTNER_MANUFACTURER;
221         }
222
223         return apiVisibility;
224 }