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     return AuthCenter::GetInstance().VerifyAccessBluetoothPermission(
71         IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
72 }
73 
VerifyGetBluetoothLocalMacPermission()74 int PermissionUtils::VerifyGetBluetoothLocalMacPermission()
75 {
76     return AuthCenter::GetInstance().VerifyGetBluetoothLocalMacPermission(
77         IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
78 }
79 
VerifyUseBluetoothPermission(const std::uint32_t & tokenID)80 int PermissionUtils::VerifyUseBluetoothPermission(const std::uint32_t  &tokenID)
81 {
82     return AuthCenter::GetInstance().VerifyUseBluetoothPermission(tokenID);
83 }
84 
VerifyDiscoverBluetoothPermission(const std::uint32_t & tokenID)85 int PermissionUtils::VerifyDiscoverBluetoothPermission(const std::uint32_t  &tokenID)
86 {
87     return AuthCenter::GetInstance().VerifyDiscoverBluetoothPermission(tokenID);
88 }
89 
CheckSystemHapApp()90 bool PermissionUtils::CheckSystemHapApp()
91 {
92     uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
93     ATokenTypeEnum callingType = AccessTokenKit::GetTokenTypeFlag(tokenId);
94     uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
95     bool isSystemApp = TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
96     HILOGI("tokenId:0x%{public}x, callingType:0x%{public}x, fullTokenId:0x%{public}llx, isSystemApp:%{public}d",
97         tokenId, callingType, static_cast<unsigned long long>(fullTokenId), isSystemApp);
98     // Only the system app can invoke the system interface.
99     if (callingType == TOKEN_HAP && !isSystemApp) {
100         HILOGE("The caller is not a system app.");
101         return false;
102     }
103     return true;
104 }
105 
GetApiVersion()106 int PermissionUtils::GetApiVersion()
107 {
108     uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
109     ATokenTypeEnum callingType = AccessTokenKit::GetTokenTypeFlag(tokenId);
110     if (callingType != ATokenTypeEnum::TOKEN_HAP) {
111         return API_VERSION_INVALID;
112     }
113     HapTokenInfo hapTokenInfo;
114     if (AccessTokenKit::GetHapTokenInfo(tokenId, hapTokenInfo) != AccessTokenKitRet::RET_SUCCESS) {
115         return API_VERSION_INVALID;
116     }
117     return hapTokenInfo.apiVersion;
118 }
119 }  // namespace bluetooth
120 }  // namespace OHOS