• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_helper.h"
17 #include "proto.h"
18 #include "ipc_skeleton.h"
19 #include "mmi_log.h"
20 
21 namespace OHOS {
22 namespace MMI {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "PermissionHelper"};
25 } // namespace
26 
PermissionHelper()27 PermissionHelper::PermissionHelper() {}
~PermissionHelper()28 PermissionHelper::~PermissionHelper() {}
29 
CheckPermission(uint32_t required)30 bool PermissionHelper::CheckPermission(uint32_t required)
31 {
32     CALL_DEBUG_ENTER;
33     auto tokenId = IPCSkeleton::GetCallingTokenID();
34     auto tokenType = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
35     if (tokenType == OHOS::Security::AccessToken::TOKEN_HAP) {
36         return CheckHapPermission(tokenId, required);
37     } else if (tokenType == OHOS::Security::AccessToken::TOKEN_NATIVE) {
38         MMI_HILOGI("Token type is native");
39         return true;
40     } else if (tokenType == OHOS::Security::AccessToken::TOKEN_SHELL) {
41         MMI_HILOGI("Token type is shell");
42         return true;
43     } else {
44         MMI_HILOGE("Unsupported token type:%{public}d", tokenType);
45         return false;
46     }
47 }
48 
CheckMonitor()49 bool PermissionHelper::CheckMonitor()
50 {
51     CALL_DEBUG_ENTER;
52     auto tokenId = IPCSkeleton::GetCallingTokenID();
53     auto tokenType = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
54     if ((tokenType == OHOS::Security::AccessToken::TOKEN_HAP) ||
55         (tokenType == OHOS::Security::AccessToken::TOKEN_NATIVE)) {
56         return CheckMonitorPermission(tokenId);
57     } else if (tokenType == OHOS::Security::AccessToken::TOKEN_SHELL) {
58         MMI_HILOGI("Token type is shell");
59         return true;
60     } else {
61         MMI_HILOGE("Unsupported token type:%{public}d", tokenType);
62         return false;
63     }
64 }
65 
CheckHapPermission(uint32_t tokenId,uint32_t required)66 bool PermissionHelper::CheckHapPermission(uint32_t tokenId, uint32_t required)
67 {
68     OHOS::Security::AccessToken::HapTokenInfo findInfo;
69     if (OHOS::Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, findInfo) != 0) {
70         MMI_HILOGE("GetHapTokenInfo failed");
71         return false;
72     }
73     if (!((1 << findInfo.apl) & required)) {
74         MMI_HILOGE("Check hap permission failed, name:%{public}s, apl:%{public}d, required:%{public}d",
75             findInfo.bundleName.c_str(), findInfo.apl, required);
76         return false;
77     }
78     MMI_HILOGI("Check hap permission success");
79     return true;
80 }
81 
CheckMonitorPermission(uint32_t tokenId)82 bool PermissionHelper::CheckMonitorPermission(uint32_t tokenId)
83 {
84     static const std::string inputMonitor = "ohos.permission.INPUT_MONITORING";
85     int32_t ret = OHOS::Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, inputMonitor);
86     if (ret != OHOS::Security::AccessToken::PERMISSION_GRANTED) {
87         MMI_HILOGE("Check monitor permission failed ret:%{public}d", ret);
88         return false;
89     }
90     MMI_HILOGI("Check monitor permission success");
91     return true;
92 }
93 
GetTokenType()94 int32_t PermissionHelper::GetTokenType()
95 {
96     CALL_DEBUG_ENTER;
97     auto tokenId = IPCSkeleton::GetCallingTokenID();
98     auto tokenType = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
99     if (tokenType == OHOS::Security::AccessToken::TOKEN_HAP) {
100         return TokenType::TOKEN_HAP;
101     } else if (tokenType == OHOS::Security::AccessToken::TOKEN_NATIVE) {
102         return TokenType::TOKEN_NATIVE;
103     }  else if (tokenType == OHOS::Security::AccessToken::TOKEN_SHELL) {
104         return TokenType::TOKEN_SHELL;
105     } else {
106         MMI_HILOGW("Unsupported token type:%{public}d", tokenType);
107         return TokenType::TOKEN_INVALID;
108     }
109 }
110 } // namespace MMI
111 } // namespace OHOS