/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "BundleChecker" #include "bundle_checker.h" #include #include "accesstoken_kit.h" #include "hap_token_info.h" #include "log_print.h" #include "utils/crypto.h" namespace OHOS { namespace DistributedData { using namespace Security::AccessToken; __attribute__((used)) BundleChecker BundleChecker::instance_; BundleChecker::BundleChecker() noexcept { CheckerManager::GetInstance().RegisterPlugin( "BundleChecker", [this]() -> auto { return this; }); } BundleChecker::~BundleChecker() { } void BundleChecker::Initialize() { } bool BundleChecker::SetTrustInfo(const CheckerManager::Trust &trust) { trusts_[trust.bundleName] = trust.appId; return true; } bool BundleChecker::SetDistrustInfo(const CheckerManager::Distrust &distrust) { distrusts_[distrust.bundleName] = distrust.appId; return true; } bool BundleChecker::SetSwitchesInfo(const CheckerManager::Switches &switches) { switches_[switches.bundleName] = switches.appId; return true; } std::string BundleChecker::GetAppId(const CheckerManager::StoreInfo &info) { if (AccessTokenKit::GetTokenTypeFlag(info.tokenId) != TOKEN_HAP) { return ""; } HapTokenInfo tokenInfo; auto result = AccessTokenKit::GetHapTokenInfo(info.tokenId, tokenInfo); if (result != RET_SUCCESS) { ZLOGE("token:0x%{public}x, result:%{public}d", info.tokenId, result); return ""; } if (!info.bundleName.empty() && tokenInfo.bundleName != info.bundleName) { ZLOGE("bundlename:%{public}s <-> %{public}s", info.bundleName.c_str(), tokenInfo.bundleName.c_str()); return ""; } auto it = trusts_.find(info.bundleName); if (it != trusts_.end() && (it->second == tokenInfo.appID)) { return info.bundleName; } ZLOGD("bundleName:%{public}s, appId:%{public}s", info.bundleName.c_str(), tokenInfo.appID.c_str()); return Crypto::Sha256(tokenInfo.appID); } bool BundleChecker::IsValid(const CheckerManager::StoreInfo &info) { if (AccessTokenKit::GetTokenTypeFlag(info.tokenId) != TOKEN_HAP) { return false; } HapTokenInfo tokenInfo; if (AccessTokenKit::GetHapTokenInfo(info.tokenId, tokenInfo) != RET_SUCCESS) { return false; } return tokenInfo.bundleName == info.bundleName; } bool BundleChecker::IsDistrust(const CheckerManager::StoreInfo &info) { if (AccessTokenKit::GetTokenTypeFlag(info.tokenId) != TOKEN_HAP) { return false; } HapTokenInfo tokenInfo; auto result = AccessTokenKit::GetHapTokenInfo(info.tokenId, tokenInfo); if (result != RET_SUCCESS) { ZLOGE("token:0x%{public}x, result:%{public}d", info.tokenId, result); return false; } if (!info.bundleName.empty() && tokenInfo.bundleName != info.bundleName) { ZLOGE("bundlename:%{public}s <-> %{public}s", info.bundleName.c_str(), tokenInfo.bundleName.c_str()); return false; } auto it = distrusts_.find(info.bundleName); if (it != distrusts_.end() && (it->second == tokenInfo.appID)) { return true; } return false; } bool BundleChecker::IsSwitches(const CheckerManager::StoreInfo &info) { return false; } std::vector BundleChecker::GetDynamicStores() { return dynamicStores_; } std::vector BundleChecker::GetStaticStores() { return staticStores_; } bool BundleChecker::IsDynamic(const CheckerManager::StoreInfo &info) { for (const auto &store : dynamicStores_) { if (info.bundleName == store.bundleName && info.storeId == store.storeId) { return true; } } return false; } bool BundleChecker::IsStatic(const CheckerManager::StoreInfo &info) { for (const auto &store : staticStores_) { if (info.bundleName == store.bundleName && info.storeId == store.storeId) { return true; } } return false; } bool BundleChecker::AddDynamicStore(const CheckerManager::StoreInfo &storeInfo) { dynamicStores_.push_back(storeInfo); return true; } bool BundleChecker::AddStaticStore(const CheckerManager::StoreInfo &storeInfo) { staticStores_.push_back(storeInfo); return true; } } // namespace DistributedData } // namespace OHOS