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();
147 __pContext->__pDistributorCertList2->RemoveAll();
152 AppLog("_pSignatureManager->ValidateSignatures() is done.");
153 __state = STATE_ROOT_CERT;
157 if (InstallerUtil::IsSignatureVerificationEnabled() == true)
159 AppLog("_pSignatureManager->ValidateSignatures() is failed.");
160 error = INSTALLER_ERROR_SIGNATURE_VERIFICATION_FAILED;
164 AppLog("_pSignatureManager->ValidateSignatures() does not be passed, using another validator.");
174 SignatureStep::OnStateSignerCert(void)
176 InstallerError error = INSTALLER_ERROR_NONE;
179 ret = __pSignatureManager->SetSignature();
180 TryCatch(ret == true, error = INSTALLER_ERROR_SIGNATURE_INVALID, "SetSignature() failed");
188 SignatureStep::OnStateCertChain(void)
190 InstallerError error = INSTALLER_ERROR_NONE;
193 ret = __pSignatureManager->AddCert();
194 TryCatch(ret == true, error = INSTALLER_ERROR_CERTIFICATE_CHAIN_VERIFICATION_FAILED, "AddCert(DEVELOPER_ROOT_CERTIFICATE) failed");
196 ret = __pSignatureManager->VerifyChain();
197 if (__pContext->__isPreloaded == true)
199 fprintf(stderr, " ## VerifyChain() result = [%d]\n", ret);
203 TryCatch(ret == true, error = INSTALLER_ERROR_CERTIFICATE_CHAIN_VERIFICATION_FAILED, "VerifyChain() failed");
212 SignatureStep::OnStateRootCert(void)
214 InstallerError error = INSTALLER_ERROR_NONE;
215 result r = E_SUCCESS;
217 String hmacPrivileges;
218 ArrayList stringPrivilegeList;
219 stringPrivilegeList.Construct();
221 const ArrayList* pPrivilegeList = __pContext->GetPrivilegeList();
222 RootCertificateType certType = __pContext->__rootCertType;
223 PackageId packageId = __pContext->__packageId;
224 int apiVisibility = __pSignatureManager->GetApiVisibility(certType);
226 if (__pContext->__isPreloaded == true)
228 __pContext->__privilegeLevel = PRIVILEGE_LEVEL_PLATFORM;
232 __pContext->__privilegeLevel = __pSignatureManager->GetPrivilegeLevel(apiVisibility);
234 AppLog("Package = [%ls], CertType = [%d], ApiVisibility = [%d], preloaded = [%d], privilege level = [%d]",
235 packageId.GetPointer(), certType, apiVisibility, __pContext->__isPreloaded, __pContext->__privilegeLevel);
237 if (pPrivilegeList != null)
239 r = PrivilegeHandler::GenerateCipherPrivilege(packageId, *pPrivilegeList, apiVisibility, privileges, hmacPrivileges, stringPrivilegeList);
240 // TryCatch(r != E_ILLEGAL_ACCESS, error = INSTALLER_ERROR_PRIVILEGE_LEVEL_INVALID, "privMgr.GeneratePrivilegeString() failed. [PRIVILEGE_LEVEL_VIOLATION]");
241 if (r == E_ILLEGAL_ACCESS)
243 __pContext->__additionalErrorString = L"Thrown when unauthorized privileges are detected. [partner] or [platform] signing is required.";
245 TryCatch(!IsFailed(r), error = INSTALLER_ERROR_PRIVILEGE_INVALID, "privMgr.GeneratePrivilegeString() failed. [INVALID_PRIVILEGE]");
249 __pContext->__privileges = privileges;
250 __pContext->__hmacPrivileges = hmacPrivileges;
251 __pContext->__certType = apiVisibility;
252 __pContext->__pStringPrivilegeList = new ArrayList;
253 __pContext->__pStringPrivilegeList->Construct(stringPrivilegeList);
261 SignatureStep::OnStateContextAudit(void)
263 InstallerError error = INSTALLER_ERROR_NONE;
264 TryCatch(__pContext->__pAppDataList, error = INSTALLER_ERROR_NONE, "__pContext->__pAppDataList is null.");
266 if (__pContext->__privilegeLevel == PRIVILEGE_LEVEL_PLATFORM)
268 TryCatch(__pContext->__privilegeLevel, error = INSTALLER_ERROR_NONE, "__pContext->__privilegeLevel is PRIVILEGE_LEVEL_PLATFORM.");
270 else if (__pContext->__privilegeLevel == PRIVILEGE_LEVEL_PARTNER)
272 TryCatch(__pContext->__privilegeLevel, error = INSTALLER_ERROR_NONE, "__pContext->__privilegeLevel is PRIVILEGE_LEVEL_PARTNER.");
274 else if (__pContext->__privilegeLevel == PRIVILEGE_LEVEL_PUBLIC)
276 int count = __pContext->__pAppDataList->GetCount();
277 result r = E_SUCCESS;
279 for (int i = 0; i < count; i++)
281 AppData* pAppData = null;
282 r = __pContext->__pAppDataList->GetAt(i, pAppData);
283 TryCatch(!IsFailed(r), error = INSTALLER_ERROR_NONE, "pAppDataList->GetAt() is failed.");
287 if ((pAppData->__type.Equals(L"UiApp", false) == true)
288 && (pAppData->__main.Equals(L"True", false) == true))
290 if (pAppData->__menuIconVisible == false)
292 AppLog("pAppData->__menuIconVisible[%d] does not be allowed in public privilege level", (int)pAppData->__menuIconVisible);
293 pAppData->__menuIconVisible = true;
296 if (pAppData->__launchingHistoryVisible.Equals(L"False", false) == true)
298 AppLog("pAppData->__launchingHistoryVisible(%ls) does not be allowed in public privilege level",
299 pAppData->__launchingHistoryVisible.GetPointer());
300 pAppData->__launchingHistoryVisible = L"True";
304 if (pAppData->__isSubMode == true)
306 AppLog("pAppData->__isSubMode(%d) does not be allowed in public privilege level",
307 pAppData->__isSubMode);
309 pAppData->__isSubModeAllowed = false;
312 if (pAppData->__pFeatureList)
314 String autoStart = L"AutoRestart";
315 String* pValue = static_cast<String *>(pAppData->__pFeatureList->GetValue(autoStart));
316 if (pValue && (pValue->Equals("True", false) == true))
318 AppLog("AutoRestart does not be allowed in public privilege level");
319 pAppData->__pFeatureList->Remove(autoStart);
322 String launchOnBoot = L"LaunchOnBoot";
323 pValue = static_cast<String *>(pAppData->__pFeatureList->GetValue(launchOnBoot));
324 if (pValue && (pValue->Equals("True", false) == true))
326 AppLog("LaunchOnBoot does not be allowed in public privilege level");
327 pAppData->__pFeatureList->Remove(launchOnBoot);
336 TryCatch(__pContext->__privilegeLevel, error = INSTALLER_ERROR_SIGNATURE_INVALID, "__pContext->__privilegeLevel is unknown.");
345 SignatureStep::OnStateDone(void)
347 InstallerError error = INSTALLER_ERROR_NONE;