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::INTERNALTESTING, Constants::APP_DISTRIBUTION_TYPE_INTERNALTESTING },
33 { Security::Verify::AppDistType::OS_INTEGRATION, Constants::APP_DISTRIBUTION_TYPE_OS_INTEGRATION },
34 { Security::Verify::AppDistType::CROWDTESTING, Constants::APP_DISTRIBUTION_TYPE_CROWDTESTING },
35 };
36
37 namespace {
38 const int32_t HAP_VERIFY_ERR_MAP_KEY[] = {
39 HapVerifyResultCode::VERIFY_SUCCESS, HapVerifyResultCode::FILE_PATH_INVALID, HapVerifyResultCode::OPEN_FILE_ERROR,
40 HapVerifyResultCode::SIGNATURE_NOT_FOUND, HapVerifyResultCode::VERIFY_APP_PKCS7_FAIL,
41 HapVerifyResultCode::PROFILE_PARSE_FAIL, HapVerifyResultCode::APP_SOURCE_NOT_TRUSTED,
42 HapVerifyResultCode::GET_DIGEST_FAIL, HapVerifyResultCode::VERIFY_INTEGRITY_FAIL,
43 HapVerifyResultCode::FILE_SIZE_TOO_LARGE, HapVerifyResultCode::GET_PUBLICKEY_FAIL,
44 HapVerifyResultCode::GET_SIGNATURE_FAIL, HapVerifyResultCode::NO_PROFILE_BLOCK_FAIL,
45 HapVerifyResultCode::VERIFY_SIGNATURE_FAIL, HapVerifyResultCode::VERIFY_SOURCE_INIT_FAIL,
46 HapVerifyResultCode::DEVICE_UNAUTHORIZED
47 };
48 const ErrCode HAP_VERIFY_ERR_MAP_VALUE[] = {
49 ERR_OK, ERR_APPEXECFWK_INSTALL_FAILED_INVALID_SIGNATURE_FILE_PATH,
50 ERR_APPEXECFWK_INSTALL_FAILED_BAD_BUNDLE_SIGNATURE_FILE, ERR_APPEXECFWK_INSTALL_FAILED_NO_BUNDLE_SIGNATURE,
51 ERR_APPEXECFWK_INSTALL_FAILED_VERIFY_APP_PKCS7_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_PROFILE_PARSE_FAIL,
52 ERR_APPEXECFWK_INSTALL_FAILED_APP_SOURCE_NOT_TRUESTED, ERR_APPEXECFWK_INSTALL_FAILED_BAD_DIGEST,
53 ERR_APPEXECFWK_INSTALL_FAILED_BUNDLE_INTEGRITY_VERIFICATION_FAILURE,
54 ERR_APPEXECFWK_INSTALL_FAILED_FILE_SIZE_TOO_LARGE, ERR_APPEXECFWK_INSTALL_FAILED_BAD_PUBLICKEY,
55 ERR_APPEXECFWK_INSTALL_FAILED_BAD_BUNDLE_SIGNATURE, ERR_APPEXECFWK_INSTALL_FAILED_NO_PROFILE_BLOCK_FAIL,
56 ERR_APPEXECFWK_INSTALL_FAILED_BUNDLE_SIGNATURE_VERIFICATION_FAILURE,
57 ERR_APPEXECFWK_INSTALL_FAILED_VERIFY_SOURCE_INIT_FAIL,
58 ERR_APPEXECFWK_INSTALL_FAILED_DEVICE_UNAUTHORIZED
59 };
60 } // namespace
61
HapVerify(const std::string & filePath,HapVerifyResult & hapVerifyResult)62 ErrCode BundleVerifyMgr::HapVerify(const std::string &filePath, HapVerifyResult &hapVerifyResult)
63 {
64 BmsExtensionDataMgr bmsExtensionDataMgr;
65 ErrCode res = bmsExtensionDataMgr.HapVerify(filePath, hapVerifyResult);
66 if (res == ERR_BUNDLEMANAGER_INSTALL_FAILED_SIGNATURE_EXTENSION_NOT_EXISTED) {
67 auto ret = Security::Verify::HapVerify(filePath, hapVerifyResult);
68 APP_LOGI("HapVerify result %{public}d", ret);
69 size_t len = sizeof(HAP_VERIFY_ERR_MAP_KEY) / sizeof(HAP_VERIFY_ERR_MAP_KEY[0]);
70 for (size_t i = 0; i < len; i++) {
71 if (ret == HAP_VERIFY_ERR_MAP_KEY[i]) {
72 return HAP_VERIFY_ERR_MAP_VALUE[i];
73 }
74 }
75 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
76 }
77 return res;
78 }
79
80 bool BundleVerifyMgr::isDebug_ = false;
81
EnableDebug()82 void BundleVerifyMgr::EnableDebug()
83 {
84 if (isDebug_) {
85 APP_LOGD("verify mode is already debug mode");
86 return;
87 }
88 if (!Security::Verify::EnableDebugMode()) {
89 APP_LOGE("start debug mode failed");
90 return;
91 }
92 isDebug_ = true;
93 }
94
DisableDebug()95 void BundleVerifyMgr::DisableDebug()
96 {
97 if (!isDebug_) {
98 APP_LOGD("verify mode is already signature mode");
99 return;
100 }
101 Security::Verify::DisableDebugMode();
102 isDebug_ = false;
103 }
104
ParseHapProfile(const std::string & filePath,HapVerifyResult & hapVerifyResult,bool readFile)105 ErrCode BundleVerifyMgr::ParseHapProfile(const std::string &filePath, HapVerifyResult &hapVerifyResult,
106 bool readFile)
107 {
108 auto ret = Security::Verify::ParseHapProfile(filePath, hapVerifyResult,
109 readFile);
110 APP_LOGI("ParseHapProfile result %{public}d", ret);
111 size_t len = sizeof(HAP_VERIFY_ERR_MAP_KEY) / sizeof(HAP_VERIFY_ERR_MAP_KEY[0]);
112 for (size_t i = 0; i < len; i++) {
113 if (ret == HAP_VERIFY_ERR_MAP_KEY[i]) {
114 return HAP_VERIFY_ERR_MAP_VALUE[i];
115 }
116 }
117 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
118 }
119 } // namespace AppExecFwk
120 } // namespace OHOS