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
16 #include "permission_utils.h"
17 #include "auth_center.h"
18 #include "ipc_skeleton.h"
19 #include "log.h"
20 #include "tokenid_kit.h"
21
22 namespace OHOS {
23 namespace bluetooth {
24 using namespace OHOS;
25 const int API_VERSION_INVALID = -1;
VerifyUseBluetoothPermission()26 int PermissionUtils::VerifyUseBluetoothPermission()
27 {
28 if (GetApiVersion() >= 10) { // 10:api version
29 return VerifyAccessBluetoothPermission();
30 }
31 return AuthCenter::GetInstance().VerifyUseBluetoothPermission(
32 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
33 }
34
VerifyDiscoverBluetoothPermission()35 int PermissionUtils::VerifyDiscoverBluetoothPermission()
36 {
37 if (GetApiVersion() >= 10) { // 10:api version
38 return VerifyAccessBluetoothPermission();
39 }
40 return AuthCenter::GetInstance().VerifyDiscoverBluetoothPermission(
41 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
42 }
43
VerifyManageBluetoothPermission()44 int PermissionUtils::VerifyManageBluetoothPermission()
45 {
46 return AuthCenter::GetInstance().VerifyManageBluetoothPermission(
47 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
48 }
49
VerifyLocationPermission()50 int PermissionUtils::VerifyLocationPermission()
51 {
52 if (GetApiVersion() >= 10) { // 10:api version
53 return VerifyAccessBluetoothPermission();
54 }
55 return AuthCenter::GetInstance().VerifyLocationPermission(
56 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
57 }
58
VerifyApproximatelyPermission()59 int PermissionUtils::VerifyApproximatelyPermission()
60 {
61 if (GetApiVersion() >= 10) { // 10:api version
62 return VerifyAccessBluetoothPermission();
63 }
64 return AuthCenter::GetInstance().VerifyApproximatelyPermission(
65 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
66 }
67
VerifyAccessBluetoothPermission()68 int PermissionUtils::VerifyAccessBluetoothPermission()
69 {
70 if (GetApiVersion() >= 10) { // 10:api version
71 return AuthCenter::GetInstance().VerifyAccessBluetoothPermission(
72 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
73 } else {
74 return PERMISSION_GRANTED;
75 }
76 }
77
VerifyGetBluetoothLocalMacPermission()78 int PermissionUtils::VerifyGetBluetoothLocalMacPermission()
79 {
80 return AuthCenter::GetInstance().VerifyGetBluetoothLocalMacPermission(
81 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
82 }
83
VerifyUseBluetoothPermission(const std::uint32_t & tokenID)84 int PermissionUtils::VerifyUseBluetoothPermission(const std::uint32_t &tokenID)
85 {
86 return AuthCenter::GetInstance().VerifyUseBluetoothPermission(tokenID);
87 }
88
VerifyDiscoverBluetoothPermission(const std::uint32_t & tokenID)89 int PermissionUtils::VerifyDiscoverBluetoothPermission(const std::uint32_t &tokenID)
90 {
91 return AuthCenter::GetInstance().VerifyDiscoverBluetoothPermission(tokenID);
92 }
93
CheckSystemHapApp()94 bool PermissionUtils::CheckSystemHapApp()
95 {
96 uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
97 ATokenTypeEnum callingType = AccessTokenKit::GetTokenTypeFlag(tokenId);
98 uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
99 bool isSystemApp = TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
100 HILOGI("tokenId:0x%{public}x, callingType:0x%{public}x, fullTokenId:0x%{public}llx, isSystemApp:%{public}d",
101 tokenId, callingType, static_cast<unsigned long long>(fullTokenId), isSystemApp);
102 // Only the system app can invoke the system interface.
103 if (callingType == TOKEN_HAP && !isSystemApp) {
104 HILOGE("The caller is not a system app.");
105 return false;
106 }
107 return true;
108 }
109
GetApiVersion()110 int PermissionUtils::GetApiVersion()
111 {
112 uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
113 ATokenTypeEnum callingType = AccessTokenKit::GetTokenTypeFlag(tokenId);
114 if (callingType != ATokenTypeEnum::TOKEN_HAP) {
115 return API_VERSION_INVALID;
116 }
117 HapTokenInfo hapTokenInfo;
118 if (AccessTokenKit::GetHapTokenInfo(tokenId, hapTokenInfo) != AccessTokenKitRet::RET_SUCCESS) {
119 return API_VERSION_INVALID;
120 }
121 return hapTokenInfo.apiVersion;
122 }
123 } // namespace bluetooth
124 } // namespace OHOS