2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file SignatureStep.cpp
19 * @brief This is the implementation file for %SignatureStep class.
23 #include <FBase_StringConverter.h>
25 #include "InstallationContext.h"
26 #include "PrivilegeHandler.h"
27 #include "SignatureHandler.h"
28 #include "SignatureStep.h"
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Io;
35 SignatureStep::SignatureStep(void)
36 :__state(STATE_SIGNER_INIT)
38 ,__pSignatureManager(null)
42 SignatureStep::~SignatureStep(void)
44 delete __pSignatureManager;
48 SignatureStep::Run(InstallationContext* pContext)
50 InstallerError error = INSTALLER_ERROR_NONE;
51 AppLog(" SignatureStep - START");
53 __pContext = pContext;
55 if (__pContext->__isVerificationMode == false)
57 AppLog("no signature file[%ls]", __pContext->GetSignatureXmlPath().GetPointer());
58 return INSTALLER_ERROR_NONE;
65 case STATE_SIGNER_INIT:
66 AppLog("[STATE_SIGNER_INIT]");
67 error = OnStateSignerInit();
70 case STATE_SIGNER_CERT:
71 AppLog("[STATE_SIGNER_CERT]");
72 error = OnStateSignerCert();
75 case STATE_CERT_CHAIN:
76 AppLog("[STATE_CERT_CHAIN]");
77 error = OnStateCertChain();
81 AppLog("[STATE_ROOT_CERT]");
82 error = OnStateRootCert();
85 case STATE_CONTEXT_AUDIT:
86 AppLog("[STATE_CONTEXT_AUDIT]");
87 error = OnStateContextAudit();
91 AppLog("[STATE_DONE]");
92 error = OnStateDone();
99 if (error != INSTALLER_ERROR_NONE)
104 if (__state > STATE_DONE)
106 AppLog(" SignatureStep - END");
115 SignatureStep::GoNextState(void)
121 SignatureStep::OnStateSignerInit(void)
123 InstallerError error = INSTALLER_ERROR_NONE;
126 __pSignatureManager = new (std::nothrow) SignatureManager();
127 TryReturn(__pSignatureManager, INSTALLER_ERROR_OUT_OF_MEMORY, "__pSignatureManager is null.");
129 res = __pSignatureManager->Construct(__pContext);
130 TryReturn(res == true, INSTALLER_ERROR_INTERNAL_STATE, "Construct() failed");
132 res = __pSignatureManager->ValidateSignatures();
133 AppLog(" ## __pSignatureManager->ValidateSignatures() result = [%s]", res?"true":"false");
134 fprintf(stderr, " ## __pSignatureManager->ValidateSignatures() result = [%s]\n", res?"true":"false");
138 res = __pSignatureManager->ValidatePartialReferences();
139 AppLog(" ## __pSignatureManager->ValidatePartialReferences() result = [%s]", res?"true":"false");
140 fprintf(stderr, " ## __pSignatureManager->ValidatePartialReferences() result = [%s]\n", res?"true":"false");
144 __pContext->__rootCertType = ROOT_CERTIFICATE_NONE;
145 __pContext->__pAuthorCertList->RemoveAll();
146 __pContext->__pDistributorCertList->RemoveAll();
151 AppLog("_pSignatureManager->ValidateSignatures() is done.");
152 __state = STATE_ROOT_CERT;
156 AppLog("_pSignatureManager->ValidateSignatures() does not be passed, using another validator.");
165 SignatureStep::OnStateSignerCert(void)
167 InstallerError error = INSTALLER_ERROR_NONE;
170 ret = __pSignatureManager->SetSignature();
171 TryCatch(ret == true, error = INSTALLER_ERROR_SIGNATURE_INVALID, "SetSignature() failed");
179 SignatureStep::OnStateCertChain(void)
181 InstallerError error = INSTALLER_ERROR_NONE;
184 ret = __pSignatureManager->AddCert();
185 TryCatch(ret == true, error = INSTALLER_ERROR_CERTIFICATE_CHAIN_VERIFICATION_FAILED, "AddCert(DEVELOPER_ROOT_CERTIFICATE) failed");
187 ret = __pSignatureManager->VerifyChain();
188 if (__pContext->__isPreloaded == true)
190 fprintf(stderr, " ## VerifyChain() result = [%d]\n", ret);
194 TryCatch(ret == true, error = INSTALLER_ERROR_CERTIFICATE_CHAIN_VERIFICATION_FAILED, "VerifyChain() failed");
203 SignatureStep::OnStateRootCert(void)
205 InstallerError error = INSTALLER_ERROR_NONE;
206 result r = E_SUCCESS;
208 String hmacPrivileges;
209 ArrayList stringPrivilegeList;
210 stringPrivilegeList.Construct();
212 const ArrayList* pPrivilegeList = __pContext->GetPrivilegeList();
213 RootCertificateType certType = __pContext->__rootCertType;
214 PackageId packageId = __pContext->__packageId;
215 int apiVisibility = __pSignatureManager->GetApiVisibility(certType);
217 if (__pContext->__isPreloaded == true)
219 __pContext->__privilegeLevel = PRIVILEGE_LEVEL_PLATFORM;
223 __pContext->__privilegeLevel = __pSignatureManager->GetPrivilegeLevel(apiVisibility);
225 AppLog("PackageId = [%ls], CertType = [%d], ApiVisibility = [%d], preloaded = [%d], privilege level = [%d]",
226 packageId.GetPointer(), certType, apiVisibility, __pContext->__isPreloaded, __pContext->__privilegeLevel);
228 if (pPrivilegeList != null)
230 r = PrivilegeHandler::GenerateCipherPrivilege(packageId, *pPrivilegeList, apiVisibility, privileges, hmacPrivileges, stringPrivilegeList);
231 TryCatch(!IsFailed(r), error = INSTALLER_ERROR_PRIVILEGE_INVALID, "privMgr.GeneratePrivilegeString() failed");
234 __pContext->__privileges = privileges;
235 __pContext->__hmacPrivileges = hmacPrivileges;
236 __pContext->__certType = apiVisibility;
237 __pContext->__pStringPrivilegeList = new ArrayList;
238 __pContext->__pStringPrivilegeList->Construct(stringPrivilegeList);
246 SignatureStep::OnStateContextAudit(void)
248 InstallerError error = INSTALLER_ERROR_NONE;
250 TryCatch(__pContext->__pAppDataList, error = INSTALLER_ERROR_NONE, "__pContext->__pAppDataList is null.");
252 if (__pContext->__privilegeLevel == PRIVILEGE_LEVEL_PLATFORM)
254 TryCatch(__pContext->__privilegeLevel, error = INSTALLER_ERROR_NONE, "__pContext->__privilegeLevel is PRIVILEGE_LEVEL_PLATFORM.");
256 else if (__pContext->__privilegeLevel == PRIVILEGE_LEVEL_PARTNER)
258 TryCatch(__pContext->__privilegeLevel, error = INSTALLER_ERROR_NONE, "__pContext->__privilegeLevel is PRIVILEGE_LEVEL_PARTNER.");
260 else if (__pContext->__privilegeLevel == PRIVILEGE_LEVEL_PUBLIC)
262 int count = __pContext->__pAppDataList->GetCount();
263 result r = E_SUCCESS;
265 for (int i = 0; i < count; i++)
267 AppData* pAppData = null;
268 r = __pContext->__pAppDataList->GetAt(i, pAppData);
269 TryCatch(!IsFailed(r), error = INSTALLER_ERROR_NONE, "pAppDataList->GetAt() is failed.");
273 if ((pAppData->__type.Equals(L"UiApp", false) == true)
274 && (pAppData->__main.Equals(L"True", false) == true))
276 if (pAppData->__menuIconVisible == false)
278 AppLog("pAppData->__menuIconVisible[%d] does not be allowed in public privilege level", (int)pAppData->__menuIconVisible);
279 pAppData->__menuIconVisible = true;
282 if (pAppData->__launchingHistoryVisible.Equals(L"False", false) == true)
284 AppLog("pAppData->__launchingHistoryVisible(%ls) does not be allowed in public privilege level",
285 pAppData->__launchingHistoryVisible.GetPointer());
286 pAppData->__launchingHistoryVisible = L"True";
290 if (pAppData->__isSubMode == true)
292 AppLog("pAppData->__isSubMode(%d) does not be allowed in public privilege level",
293 pAppData->__isSubMode);
295 pAppData->__isSubModeAllowed = false;
298 if (pAppData->__pFeatureList)
300 String autoStart = L"AutoRestart";
301 String* pValue = static_cast<String *>(pAppData->__pFeatureList->GetValue(autoStart));
302 if (pValue && (pValue->Equals("True", false) == true))
304 AppLog("AutoRestart does not be allowed in public privilege level");
305 pAppData->__pFeatureList->Remove(autoStart);
308 String launchOnBoot = L"LaunchOnBoot";
309 pValue = static_cast<String *>(pAppData->__pFeatureList->GetValue(launchOnBoot));
310 if (pValue && (pValue->Equals("True", false) == true))
312 AppLog("LaunchOnBoot does not be allowed in public privilege level");
313 pAppData->__pFeatureList->Remove(launchOnBoot);
321 TryCatch(__pContext->__privilegeLevel, error = INSTALLER_ERROR_SIGNATURE_INVALID, "__pContext->__privilegeLevel is unknown.");
331 SignatureStep::OnStateDone(void)
333 InstallerError error = INSTALLER_ERROR_NONE;