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();
86 AppLog("[STATE_DONE]");
87 error = OnStateDone();
94 if (error != INSTALLER_ERROR_NONE)
99 if (__state > STATE_DONE)
101 AppLog(" SignatureStep - END");
110 SignatureStep::GoNextState(void)
116 SignatureStep::OnStateSignerInit(void)
118 InstallerError error = INSTALLER_ERROR_NONE;
121 __pSignatureManager = new (std::nothrow) SignatureManager();
122 TryReturn(__pSignatureManager, error = INSTALLER_ERROR_OUT_OF_MEMORY, "__pSignatureManager is null.");
124 ret = __pSignatureManager->Construct(__pContext);
125 TryReturn(ret == true, error = INSTALLER_ERROR_INTERNAL_STATE, "Construct() failed");
127 ret = __pSignatureManager->ValidateSignatures();
128 fprintf(stderr, " ## __pSignatureManager->ValidateSignatures() result = [%d]\n", ret);
132 AppLog("_pSignatureManager->ValidateSignatures() is ok.");
133 __state = STATE_ROOT_CERT;
137 AppLog("_pSignatureManager->ValidateSignatures() does not be passed, using another validator.");
139 __pContext->__rootCertType = ROOT_CERTIFICATE_NONE;
140 __pContext->__pAuthorCertList->RemoveAll();
141 __pContext->__pDistributorCertList->RemoveAll();
151 SignatureStep::OnStateSignerCert(void)
153 InstallerError error = INSTALLER_ERROR_NONE;
156 ret = __pSignatureManager->SetSignature();
157 TryCatch(ret == true, error = INSTALLER_ERROR_SIGNATURE_INVALID, "SetSignature() failed");
165 SignatureStep::OnStateCertChain(void)
167 InstallerError error = INSTALLER_ERROR_NONE;
170 ret = __pSignatureManager->AddCert();
171 TryCatch(ret == true, error = INSTALLER_ERROR_CERTIFICATE_CHAIN_VERIFICATION_FAILED, "AddCert(DEVELOPER_ROOT_CERTIFICATE) failed");
173 ret = __pSignatureManager->VerifyChain();
174 if (__pContext->__isPreloaded == true)
176 fprintf(stderr, " ## VerifyChain() result = [%d]\n", ret);
180 TryCatch(ret == true, error = INSTALLER_ERROR_CERTIFICATE_CHAIN_VERIFICATION_FAILED, "VerifyChain() failed");
189 SignatureStep::OnStateRootCert(void)
191 InstallerError error = INSTALLER_ERROR_NONE;
192 result r = E_SUCCESS;
194 String hmacPrivileges;
195 ArrayList stringPrivilegeList;
196 stringPrivilegeList.Construct();
198 const ArrayList* pPrivilegeList = __pContext->GetPrivilegeList();
199 RootCertificateType certType = __pContext->__rootCertType;
200 PackageId packageId = __pContext->__packageId;
201 int apiVisibility = __pSignatureManager->GetApiVisibility(certType);
203 AppLog("PackageId = [%ls], CertType = [%d], ApiVisibility = [%d]", packageId.GetPointer(), certType, apiVisibility);
205 if (pPrivilegeList != null)
207 r = PrivilegeHandler::GenerateCipherPrivilege(packageId, *pPrivilegeList, apiVisibility, privileges, hmacPrivileges, stringPrivilegeList);
208 TryCatch(!IsFailed(r), error = INSTALLER_ERROR_PRIVILEGE_INVALID, "privMgr.GeneratePrivilegeString() failed");
211 __pContext->__privileges = privileges;
212 __pContext->__hmacPrivileges = hmacPrivileges;
213 __pContext->__certType = apiVisibility;
214 __pContext->__pStringPrivilegeList = new ArrayList;
215 __pContext->__pStringPrivilegeList->Construct(stringPrivilegeList);
223 SignatureStep::OnStateDone(void)
225 InstallerError error = INSTALLER_ERROR_NONE;