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 "time_permission.h"
17
18 #include "accesstoken_kit.h"
19 #include "ipc_skeleton.h"
20 #include "tokenid_kit.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 const std::string TimePermission::SET_TIME = "ohos.permission.SET_TIME";
25 const std::string TimePermission::SET_TIME_ZONE = "ohos.permission.SET_TIME_ZONE";
CheckCallingPermission(const std::string & permissionName)26 bool TimePermission::CheckCallingPermission(const std::string &permissionName)
27 {
28 if (permissionName.empty()) {
29 TIME_HILOGE(TIME_MODULE_COMMON, "permission check failed, permission name is empty.");
30 return false;
31 }
32 auto callerToken = IPCSkeleton::GetCallingTokenID();
33 auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
34 int result = Security::AccessToken::PERMISSION_DENIED;
35 if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
36 tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
37 result = Security::AccessToken::PERMISSION_GRANTED;
38 } else if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
39 result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
40 } else {
41 TIME_HILOGE(TIME_MODULE_COMMON, "permission check failed, callerToken:%{public}u,tokenType:%{public}d",
42 callerToken, tokenType);
43 }
44
45 if (result != Security::AccessToken::PERMISSION_GRANTED) {
46 TIME_HILOGE(TIME_MODULE_COMMON, "permission check failed, permission:%{public}s, callerToken:%{public}u",
47 permissionName.c_str(), callerToken);
48 return false;
49 }
50 return true;
51 }
52
CheckProxyCallingPermission()53 bool TimePermission::CheckProxyCallingPermission()
54 {
55 auto callerToken = IPCSkeleton::GetCallingTokenID();
56 auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
57 return (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
58 tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL);
59 }
60
CheckSystemUidCallingPermission(uint64_t tokenId)61 bool TimePermission::CheckSystemUidCallingPermission(uint64_t tokenId)
62 {
63 auto callerToken = IPCSkeleton::GetCallingTokenID();
64 auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
65 if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
66 return true;
67 }
68 if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
69 return true;
70 }
71 return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId);
72 }
73 } // namespace MiscServices
74 } // namespace OHOS