• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 "permission_manager.h"
17 
18 #include <cstdint>
19 #include <string>
20 
21 #include "access_token.h"
22 #include "accesstoken_kit.h"
23 #include "bundlemgr/bundle_mgr_proxy.h"
24 #include "common/log.h"
25 #include "hap_token_info.h"
26 #include "ipc_skeleton.h"
27 #include "iservice_registry.h"
28 #include "native_token_info.h"
29 #include "system_ability_definition.h"
30 #include "tokenid_kit.h"
31 
32 #include "hitrace_meter.h"
33 
34 namespace OHOS {
35 namespace bluetooth {
36 
GetCallingName()37 std::string PermissionManager::GetCallingName()
38 {
39     uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
40     return GetCallingName(tokenId);
41 }
42 
GetCallingName(const uint32_t & tokenId)43 std::string PermissionManager::GetCallingName(const uint32_t& tokenId)
44 {
45     Security::AccessToken::ATokenTypeEnum callingType =
46         Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
47     switch (callingType) {
48         case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: {
49             Security::AccessToken::HapTokenInfo hapTokenInfo;
50             if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapTokenInfo) ==
51                 Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
52                 return hapTokenInfo.bundleName;
53             }
54             HILOGE("permission callingtype(%{public}d), getHapTokenInfoFail", callingType);
55             return "";
56         }
57         case OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL:
58         case OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE: {
59             Security::AccessToken::NativeTokenInfo naitiveTokenInfo;
60             if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, naitiveTokenInfo) ==
61                 Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
62                 return naitiveTokenInfo.processName;
63             }
64             HILOGE("permission callingtype(%{public}d), getNativeTokenInfoFail", callingType);
65             return "";
66         }
67         default:
68             HILOGE("permission callingtype(%{public}d) is invalid.", callingType);
69             return "";
70     }
71 }
72 
IsSystemHap()73 bool PermissionManager::IsSystemHap()
74 {
75     uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
76     return IsSystemHap(fullTokenId);
77 }
78 
IsSystemHap(const uint64_t & fullTokenId)79 bool PermissionManager::IsSystemHap(const uint64_t& fullTokenId)
80 {
81     bool isSystemApp = Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
82     Security::AccessToken::ATokenTypeEnum callingType =
83         Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(static_cast<uint32_t>(fullTokenId));
84     if (callingType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP && !isSystemApp) {
85         return false;
86     }
87     return true;
88 }
89 } // namespace bluetooth
90 } // namespace OHOS
91