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
VerifyUseBluetoothPermission(const std::uint32_t & tokenID)56 int PermissionUtils::VerifyUseBluetoothPermission(const std::uint32_t &tokenID)
57 {
58 return AuthCenter::GetInstance().VerifyUseBluetoothPermission(tokenID);
59 }
60
VerifyDiscoverBluetoothPermission(const std::uint32_t & tokenID)61 int PermissionUtils::VerifyDiscoverBluetoothPermission(const std::uint32_t &tokenID)
62 {
63 return AuthCenter::GetInstance().VerifyDiscoverBluetoothPermission(tokenID);
64 }
65
CheckSystemHapApp()66 bool PermissionUtils::CheckSystemHapApp()
67 {
68 uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
69 ATokenTypeEnum callingType = AccessTokenKit::GetTokenTypeFlag(tokenId);
70 uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
71 bool isSystemApp = TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
72 HILOGI("tokenId:0x%{public}x, callingType:0x%{public}x, fullTokenId:0x%{public}llx, isSystemApp:%{public}d",
73 tokenId, callingType, static_cast<unsigned long long>(fullTokenId), isSystemApp);
74 // Only the system app can invoke the system interface.
75 if (callingType == TOKEN_HAP && !isSystemApp) {
76 HILOGE("The caller is not a system app.");
77 return false;
78 }
79 return true;
80 }
81 } // namespace bluetooth
82 } // namespace OHOS