1 /*
2 * Copyright (c) 2021 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 "bundle_verify_mgr.h"
17
18 #include "app_log_wrapper.h"
19 #include "bms_extension_data_mgr.h"
20
21 using namespace OHOS::Security::Verify;
22
23 namespace OHOS {
24 namespace AppExecFwk {
25
26 const std::unordered_map<Security::Verify::AppDistType, std::string> APP_DISTRIBUTION_TYPE_MAPS = {
27 { Security::Verify::AppDistType::NONE_TYPE, Constants::APP_DISTRIBUTION_TYPE_NONE },
28 { Security::Verify::AppDistType::APP_GALLERY, Constants::APP_DISTRIBUTION_TYPE_APP_GALLERY },
29 { Security::Verify::AppDistType::ENTERPRISE, Constants::APP_DISTRIBUTION_TYPE_ENTERPRISE },
30 { Security::Verify::AppDistType::ENTERPRISE_NORMAL, Constants::APP_DISTRIBUTION_TYPE_ENTERPRISE_NORMAL },
31 { Security::Verify::AppDistType::ENTERPRISE_MDM, Constants::APP_DISTRIBUTION_TYPE_ENTERPRISE_MDM },
32 { Security::Verify::AppDistType::OS_INTEGRATION, Constants::APP_DISTRIBUTION_TYPE_OS_INTEGRATION },
33 { Security::Verify::AppDistType::CROWDTESTING, Constants::APP_DISTRIBUTION_TYPE_CROWDTESTING },
34 };
35
36 namespace {
37 const std::map<int32_t, ErrCode> HAP_VERIFY_ERR_MAP = {
38 {HapVerifyResultCode::VERIFY_SUCCESS, ERR_OK},
39 {HapVerifyResultCode::FILE_PATH_INVALID, ERR_APPEXECFWK_INSTALL_FAILED_INVALID_SIGNATURE_FILE_PATH},
40 {HapVerifyResultCode::OPEN_FILE_ERROR, ERR_APPEXECFWK_INSTALL_FAILED_BAD_BUNDLE_SIGNATURE_FILE},
41 {HapVerifyResultCode::SIGNATURE_NOT_FOUND, ERR_APPEXECFWK_INSTALL_FAILED_NO_BUNDLE_SIGNATURE},
42 {HapVerifyResultCode::VERIFY_APP_PKCS7_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_VERIFY_APP_PKCS7_FAIL},
43 {HapVerifyResultCode::PROFILE_PARSE_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_PROFILE_PARSE_FAIL},
44 {HapVerifyResultCode::APP_SOURCE_NOT_TRUSTED, ERR_APPEXECFWK_INSTALL_FAILED_APP_SOURCE_NOT_TRUESTED},
45 {HapVerifyResultCode::GET_DIGEST_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BAD_DIGEST},
46 {HapVerifyResultCode::VERIFY_INTEGRITY_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BUNDLE_INTEGRITY_VERIFICATION_FAILURE},
47 {HapVerifyResultCode::FILE_SIZE_TOO_LARGE, ERR_APPEXECFWK_INSTALL_FAILED_FILE_SIZE_TOO_LARGE},
48 {HapVerifyResultCode::GET_PUBLICKEY_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BAD_PUBLICKEY},
49 {HapVerifyResultCode::GET_SIGNATURE_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BAD_BUNDLE_SIGNATURE},
50 {HapVerifyResultCode::NO_PROFILE_BLOCK_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_NO_PROFILE_BLOCK_FAIL},
51 {HapVerifyResultCode::VERIFY_SIGNATURE_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BUNDLE_SIGNATURE_VERIFICATION_FAILURE},
52 {HapVerifyResultCode::VERIFY_SOURCE_INIT_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_VERIFY_SOURCE_INIT_FAIL}
53 };
54 } // namespace
55
HapVerify(const std::string & filePath,HapVerifyResult & hapVerifyResult)56 ErrCode BundleVerifyMgr::HapVerify(const std::string &filePath, HapVerifyResult &hapVerifyResult)
57 {
58 BmsExtensionDataMgr bmsExtensionDataMgr;
59 ErrCode res = bmsExtensionDataMgr.HapVerify(filePath, hapVerifyResult);
60 if (res == ERR_BUNDLEMANAGER_INSTALL_FAILED_SIGNATURE_EXTENSION_NOT_EXISTED) {
61 auto ret = Security::Verify::HapVerify(filePath, hapVerifyResult);
62 APP_LOGI("HapVerify result %{public}d", ret);
63 if (HAP_VERIFY_ERR_MAP.find(ret) == HAP_VERIFY_ERR_MAP.end()) {
64 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
65 }
66 return HAP_VERIFY_ERR_MAP.at(ret);
67 }
68 return res;
69 }
70
71 bool BundleVerifyMgr::isDebug_ = false;
72
EnableDebug()73 void BundleVerifyMgr::EnableDebug()
74 {
75 if (isDebug_) {
76 APP_LOGD("verify mode is already debug mode");
77 return;
78 }
79 if (!Security::Verify::EnableDebugMode()) {
80 APP_LOGE("start debug mode failed");
81 return;
82 }
83 isDebug_ = true;
84 }
85
DisableDebug()86 void BundleVerifyMgr::DisableDebug()
87 {
88 if (!isDebug_) {
89 APP_LOGD("verify mode is already signature mode");
90 return;
91 }
92 Security::Verify::DisableDebugMode();
93 isDebug_ = false;
94 }
95
ParseHapProfile(const std::string & filePath,HapVerifyResult & hapVerifyResult)96 ErrCode BundleVerifyMgr::ParseHapProfile(const std::string &filePath, HapVerifyResult &hapVerifyResult)
97 {
98 auto ret = Security::Verify::ParseHapProfile(filePath, hapVerifyResult);
99 APP_LOGI("ParseHapProfile result %{public}d", ret);
100 if (HAP_VERIFY_ERR_MAP.find(ret) == HAP_VERIFY_ERR_MAP.end()) {
101 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
102 }
103 return HAP_VERIFY_ERR_MAP.at(ret);
104 }
105 } // namespace AppExecFwk
106 } // namespace OHOS