1 /*
2  * Copyright (c) 2024 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 "edm_access_token_manager_impl.h"
17 
18 #include "ipc_skeleton.h"
19 #include "parameters.h"
20 #include "tokenid_kit.h"
21 
22 #include "edm_log.h"
23 
24 namespace OHOS {
25 namespace EDM {
26 const std::string DEVELOP_MODE_STATE = "const.security.developermode.state";
27 
IsDebug()28 bool EdmAccessTokenManagerImpl::IsDebug()
29 {
30     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
31     Security::AccessToken::ATokenTypeEnum tokenType =
32         Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
33     if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL &&
34         system::GetBoolParameter(DEVELOP_MODE_STATE, false)) {
35         EDMLOGI("EdmAccessTokenManagerImpl::caller tokenType is shell and check developer mode success.");
36         return true;
37     }
38     return false;
39 }
40 
IsSystemAppOrNative()41 bool EdmAccessTokenManagerImpl::IsSystemAppOrNative()
42 {
43     bool isSystemApp =
44         Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID());
45     Security::AccessToken::ATokenTypeEnum tokenType =
46         Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(IPCSkeleton::GetCallingTokenID());
47     if (isSystemApp || tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
48         tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
49         EDMLOGE("EdmAccessTokenManagerImpl::not system app or native process");
50         return true;
51     }
52     return false;
53 }
54 
VerifyCallingPermission(Security::AccessToken::AccessTokenID tokenId,const std::string & permissionName)55 bool EdmAccessTokenManagerImpl::VerifyCallingPermission(Security::AccessToken::AccessTokenID tokenId,
56     const std::string &permissionName)
57 {
58     EDMLOGD("EdmAccessTokenManagerImpl::VerifyCallingPermission permission %{public}s", permissionName.c_str());
59     int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, permissionName);
60     if (ret == Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
61         EDMLOGI("EdmAccessTokenManagerImpl::permission %{public}s: PERMISSION_GRANTED", permissionName.c_str());
62         return true;
63     }
64     EDMLOGW("EdmAccessTokenManagerImpl::verify AccessToken failed");
65     return false;
66 }
67 } // namespace EDM
68 } // namespace OHOS