1 /*
2 * Copyright (c) 2025 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 #include "load_mode_client_util.h"
16
17 #include "accesstoken_kit.h"
18 #include "ipc_skeleton.h"
19 #include "parameter.h"
20
21 #include "iam_check.h"
22 #include "iam_common_defines.h"
23 #include "iam_logger.h"
24
25 #define LOG_TAG "USER_AUTH_SDK"
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 #ifdef ENABLE_DYNAMIC_LOAD
CheckSelfPermission(const std::string & permission)31 bool CheckSelfPermission(const std::string &permission)
32 {
33 using namespace Security::AccessToken;
34 uint32_t tokenId = static_cast<uint32_t>(IPCSkeleton::GetSelfTokenID());
35 if (AccessTokenKit::VerifyAccessToken(tokenId, permission) != RET_SUCCESS) {
36 IAM_LOGE("failed to check permission %{public}s", permission.c_str());
37 return false;
38 }
39 return true;
40 }
41
IsUserIamDeamonProcess()42 bool IsUserIamDeamonProcess()
43 {
44 constexpr uint32_t MAX_VALUE_LEN = 128;
45 const char *IS_PIN_ENROLLED_KEY = "persist.useriam.isPinEnrolled";
46 char valueBuffer[MAX_VALUE_LEN] = { 0 };
47 int32_t ret = GetParameter(IS_PIN_ENROLLED_KEY, "false", valueBuffer, MAX_VALUE_LEN);
48 if (ret < 0) {
49 IAM_LOGE("get parameter failed, ret:%{public}d", ret);
50 return false;
51 }
52 return std::string(valueBuffer) == "true";
53 }
54
HasAnyPermission(const std::vector<std::string> & permissions)55 bool HasAnyPermission(const std::vector<std::string> &permissions)
56 {
57 for (const auto &permission : permissions) {
58 if (CheckSelfPermission(permission)) {
59 return true;
60 }
61 }
62
63 return false;
64 }
65
GetProxyNullResultCode(const char * funcName,const std::vector<std::string> & permissions)66 int32_t LoadModeUtil::GetProxyNullResultCode(const char *funcName, const std::vector<std::string> &permissions)
67 {
68 IF_FALSE_LOGE_AND_RETURN_VAL(funcName != nullptr, GENERAL_ERROR);
69 if (IsUserIamDeamonProcess()) {
70 IAM_LOGE("%{public}s, user iam is deamon process, proxy should not be null", funcName);
71 return GENERAL_ERROR;
72 }
73
74 if (!HasAnyPermission(permissions)) {
75 IAM_LOGE("%{public}s, check permission failed", funcName);
76 return CHECK_PERMISSION_FAILED;
77 }
78
79 IAM_LOGE("%{public}s, useriam is not deamon process and pin is not enrolled", funcName);
80 return NOT_ENROLLED;
81 }
82 #else
83 int32_t LoadModeUtil::GetProxyNullResultCode(const char *funcName, const std::vector<std::string> &permissions)
84 {
85 static_cast<void>(funcName);
86 static_cast<void>(permissions);
87 IAM_LOGE("proxy is nullptr");
88 return GENERAL_ERROR;
89 }
90 #endif
91 } // namespace UserAuth
92 } // namespace UserIam
93 } // namespace OHOS