1 /*
2  * Copyright (c) 2020 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "hap_sign_verify.h"
17 
18 #include "appexecfwk_errors.h"
19 #include "bundle_manager_service.h"
20 #include "bundle_log.h"
21 
22 namespace OHOS {
VerifySignature(const std::string & hapFilepath,SignatureInfo & signatureInfo)23 uint8_t HapSignVerify::VerifySignature(const std::string &hapFilepath, SignatureInfo &signatureInfo)
24 {
25     bool mode = ManagerService::GetInstance().IsDebugMode();
26     HILOG_INFO(HILOG_MODULE_APP, "current mode is %d!", mode);
27     VerifyResult verifyResult;
28     // verify signature
29     int32_t ret = APPVERI_AppVerify(hapFilepath.c_str(), &verifyResult);
30     uint8_t errorCode = SwitchErrorCode(ret);
31     if (errorCode != ERR_OK) {
32         return errorCode;
33     }
34 
35     signatureInfo.appId = verifyResult.profile.appid;
36     signatureInfo.provisionBundleName = verifyResult.profile.bundleInfo.bundleName;
37     int32_t restricNum = verifyResult.profile.permission.restricNum;
38     for (int32_t i = 0; i < restricNum; i++) {
39         signatureInfo.restrictedPermissions.emplace_back((verifyResult.profile.permission.restricPermission)[i]);
40     }
41     APPVERI_FreeVerifyRst(&verifyResult);
42     return ERR_OK;
43 }
44 
SwitchErrorCode(int32_t errorCode)45 uint8_t HapSignVerify::SwitchErrorCode(int32_t errorCode)
46 {
47     uint32_t errCode = static_cast<uint32_t>(errorCode);
48     if (errCode >= V_ERR_GET_CERT_INFO && errCode <= V_ERR_GET_CERT_TYPE) {
49         return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_APP_SIGNATURE_ERROR;
50     } else if (errCode >= V_ERR_GET_PROFILE_DATA && errCode <= V_ERR_INVALID_DEVID) {
51         return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_PROFILE_SIGNATURE_ERROR;
52     } else if (errCode >= V_ERR_FILE_OPEN && errCode <= V_ERR_FILE_LENGTH) {
53         return ERR_APPEXECFWK_INSTALL_FAILED_OPERATE_SIGNED_FILE_ERROR;
54     } else if ((errCode >= V_ERR_MEMSET && errCode <= V_ERR_MALLOC) || errCode == V_ERR) {
55         return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_SIGNATURE_ERROR;
56     } else {
57         return ERR_OK;
58     }
59 }
60 } // namespace OHOS