• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundle_checker.h"
17 
18 #include "accesstoken_kit.h"
19 #include "global.h"
20 
21 namespace OHOS {
22 namespace MiscServices {
23 using namespace Security::AccessToken;
IsCurrentIme(uint32_t tokenID,const std::string & currentIme)24 bool BundleChecker::IsCurrentIme(uint32_t tokenID, const std::string &currentIme)
25 {
26     std::string bundleName = GetBundleNameByToken(tokenID);
27     if (bundleName.empty()) {
28         return false;
29     }
30     if (currentIme.empty() || bundleName.empty()) {
31         IMSA_HILOGE("empty string");
32         return false;
33     }
34     if (bundleName != currentIme) {
35         IMSA_HILOGE(
36             "not current ime, caller: %{public}s, current ime: %{public}s", bundleName.c_str(), currentIme.c_str());
37         return false;
38     }
39     IMSA_HILOGD("checked ime successfully");
40     return true;
41 }
42 
CheckPermission(uint32_t tokenID,const std::string & permission)43 bool BundleChecker::CheckPermission(uint32_t tokenID, const std::string &permission)
44 {
45     if (AccessTokenKit::VerifyAccessToken(tokenID, permission) != PERMISSION_GRANTED) {
46         IMSA_HILOGE("Permission [%{public}s] not granted", permission.c_str());
47         return false;
48     }
49     IMSA_HILOGD("verify AccessToken success");
50     return true;
51 }
52 
GetBundleNameByToken(uint32_t tokenID)53 std::string BundleChecker::GetBundleNameByToken(uint32_t tokenID)
54 {
55     auto tokenType = AccessTokenKit::GetTokenTypeFlag(tokenID);
56     if (tokenType != TOKEN_HAP) {
57         IMSA_HILOGE("invalid token");
58         return "";
59     }
60     HapTokenInfo info;
61     int ret = AccessTokenKit::GetHapTokenInfo(tokenID, info);
62     if (ret != ErrorCode::NO_ERROR) {
63         IMSA_HILOGE("failed to get hap info, ret: %{public}d", ret);
64         return "";
65     }
66     return info.bundleName;
67 }
68 } // namespace MiscServices
69 } // namespace OHOS
70