1 /*
2 * Copyright (c) 2023 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 "bms/bundle_info_query.h"
17 #include "bundle_mgr_client.h"
18 #include "bundle_info.h"
19 #include "os_account_manager.h"
20 #include "app_domain_verify_hilog.h"
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "bundlemgr/bundle_mgr_proxy.h"
24
25 namespace OHOS {
26 namespace AppDomainVerify {
GetBundleInfo(const std::string & bundleName,std::string & appIdentifier,std::string & fingerprint)27 bool BundleInfoQuery::GetBundleInfo(const std::string &bundleName, std::string &appIdentifier, std::string &fingerprint)
28 {
29 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "called");
30 sptr<AppExecFwk::IBundleMgr> bundleMgrProxy = GetBundleMgrProxy();
31
32 if (bundleMgrProxy == nullptr) {
33 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "bundleMgrProxy is nullptr.");
34 return false;
35 }
36 int32_t userId = GetCurrentAccountId();
37 if (userId < 0) {
38 return false;
39 }
40 OHOS::AppExecFwk::BundleInfo bundleInfo;
41 auto ret = bundleMgrProxy->GetBundleInfoV9(bundleName,
42 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO), bundleInfo, userId);
43 if (ret != ERR_OK) {
44 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "GetBundleInfo failed, ret: %{public}d.", ret);
45 return false;
46 }
47 appIdentifier = bundleInfo.signatureInfo.appIdentifier;
48 fingerprint = bundleInfo.signatureInfo.fingerprint;
49 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "call end");
50 return true;
51 }
52
GetBundleMgrProxy()53 sptr<AppExecFwk::IBundleMgr> BundleInfoQuery::GetBundleMgrProxy()
54 {
55 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "called");
56 sptr<ISystemAbilityManager> systemAbilityManager =
57 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58 if (!systemAbilityManager) {
59 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE,
60 "GetBundleMgrProxy, systemAbilityManager is null");
61 return nullptr;
62 }
63 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
64 if (!remoteObject) {
65 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "GetBundleMgrProxy, remoteObject is null");
66 return nullptr;
67 }
68 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "call end");
69 return iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
70 }
71
GetCurrentAccountId()72 int32_t BundleInfoQuery::GetCurrentAccountId()
73 {
74 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "called");
75 std::vector<int32_t> osAccountIds;
76 ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
77 if (ret != ERR_OK) {
78 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "QueryActiveOsAccountIds failed.");
79 return -1;
80 }
81
82 if (osAccountIds.empty()) {
83 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "osAccountInfos is empty, no accounts.");
84 return -1;
85 }
86
87 auto iter = std::find_if(osAccountIds.cbegin(), osAccountIds.cend(),
88 [](const int32_t &accountId) { return accountId >= 0; });
89 if (iter != osAccountIds.end()) {
90 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "call end");
91 return *iter;
92 }
93 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE,
94 "GetCurrentAccountId failed, no osAccountIds now.");
95 return -1;
96 }
97 }
98 }