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
VerifyUseBluetoothPermission()26 int PermissionUtils::VerifyUseBluetoothPermission()
27 {
28 return AuthCenter::GetInstance().VerifyUseBluetoothPermission(
29 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
30 }
31
VerifyDiscoverBluetoothPermission()32 int PermissionUtils::VerifyDiscoverBluetoothPermission()
33 {
34 return AuthCenter::GetInstance().VerifyDiscoverBluetoothPermission(
35 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
36 }
37
VerifyManageBluetoothPermission()38 int PermissionUtils::VerifyManageBluetoothPermission()
39 {
40 return AuthCenter::GetInstance().VerifyManageBluetoothPermission(
41 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
42 }
43
VerifyLocationPermission()44 int PermissionUtils::VerifyLocationPermission()
45 {
46 return AuthCenter::GetInstance().VerifyLocationPermission(
47 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
48 }
49
VerifyApproximatelyPermission()50 int PermissionUtils::VerifyApproximatelyPermission()
51 {
52 return AuthCenter::GetInstance().VerifyApproximatelyPermission(
53 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
54 }
55
VerifyAccessBluetoothPermission()56 int PermissionUtils::VerifyAccessBluetoothPermission()
57 {
58 return AuthCenter::GetInstance().VerifyAccessBluetoothPermission(
59 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
60 }
61
VerifyGetBluetoothLocalMacPermission()62 int PermissionUtils::VerifyGetBluetoothLocalMacPermission()
63 {
64 return AuthCenter::GetInstance().VerifyGetBluetoothLocalMacPermission(
65 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
66 }
67
VerifyUseBluetoothPermission(const std::uint32_t & tokenID)68 int PermissionUtils::VerifyUseBluetoothPermission(const std::uint32_t &tokenID)
69 {
70 return AuthCenter::GetInstance().VerifyUseBluetoothPermission(tokenID);
71 }
72
VerifyDiscoverBluetoothPermission(const std::uint32_t & tokenID)73 int PermissionUtils::VerifyDiscoverBluetoothPermission(const std::uint32_t &tokenID)
74 {
75 return AuthCenter::GetInstance().VerifyDiscoverBluetoothPermission(tokenID);
76 }
77
CheckSystemHapApp()78 bool PermissionUtils::CheckSystemHapApp()
79 {
80 uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
81 ATokenTypeEnum callingType = AccessTokenKit::GetTokenTypeFlag(tokenId);
82 uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
83 bool isSystemApp = TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
84 HILOGI("tokenId:0x%{public}x, callingType:0x%{public}x, fullTokenId:0x%{public}llx, isSystemApp:%{public}d",
85 tokenId, callingType, static_cast<unsigned long long>(fullTokenId), isSystemApp);
86 // Only the system app can invoke the system interface.
87 if (callingType == TOKEN_HAP && !isSystemApp) {
88 HILOGE("The caller is not a system app.");
89 return false;
90 }
91 return true;
92 }
93 } // namespace bluetooth
94 } // namespace OHOS