• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2022 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 #define LOG_TAG "SystemChecker"
16 #include "system_checker.h"
17 #include "accesstoken_kit.h"
18 #include "log_print.h"
19 namespace OHOS {
20 namespace DistributedData {
21 using namespace Security::AccessToken;
22 __attribute__((used)) SystemChecker SystemChecker::instance_;
SystemChecker()23 SystemChecker::SystemChecker() noexcept
24 {
25     CheckerManager::GetInstance().RegisterPlugin(
26         "SystemChecker", [this]() -> auto { return this; });
27 }
28 
~SystemChecker()29 SystemChecker::~SystemChecker()
30 {
31 }
32 
Initialize()33 void SystemChecker::Initialize()
34 {
35 }
36 
SetTrustInfo(const CheckerManager::Trust & trust)37 bool SystemChecker::SetTrustInfo(const CheckerManager::Trust &trust)
38 {
39     trusts_[trust.bundleName] = trust.appId;
40     return true;
41 }
42 
SetDistrustInfo(const CheckerManager::Distrust & distrust)43 bool SystemChecker::SetDistrustInfo(const CheckerManager::Distrust &distrust)
44 {
45     distrusts_[distrust.bundleName] = distrust.appId;
46     return true;
47 }
48 
GetAppId(const CheckerManager::StoreInfo & info)49 std::string SystemChecker::GetAppId(const CheckerManager::StoreInfo &info)
50 {
51     if (!IsValid(info)) {
52         return "";
53     }
54     std::string appId = (trusts_.find(info.bundleName) != trusts_.end()) ? trusts_[info.bundleName] : info.bundleName;
55     ZLOGD("bundleName:%{public}s, appId:%{public}s", info.bundleName.c_str(), appId.c_str());
56     return appId;
57 }
58 
IsValid(const CheckerManager::StoreInfo & info)59 bool SystemChecker::IsValid(const CheckerManager::StoreInfo &info)
60 {
61     auto type = AccessTokenKit::GetTokenTypeFlag(info.tokenId);
62     return (type == TOKEN_NATIVE || type == TOKEN_SHELL);
63 }
64 
IsDistrust(const CheckerManager::StoreInfo & info)65 bool SystemChecker::IsDistrust(const CheckerManager::StoreInfo &info)
66 {
67     if (!IsValid(info)) {
68         return false;
69     }
70     auto it = distrusts_.find(info.bundleName);
71     if (it != distrusts_.end()) {
72         return true;
73     }
74     return false;
75 }
76 } // namespace DistributedData
77 } // namespace OHOS