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
16 #include "user_auth_helper.h"
17
18 #include <cinttypes>
19
20 #include "iam_logger.h"
21
22 #define LOG_TAG "USER_AUTH_COMMON"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27
GetResultCodeV10(int32_t result)28 int32_t UserAuthHelper::GetResultCodeV10(int32_t result)
29 {
30 if (result == CHECK_PERMISSION_FAILED) {
31 return static_cast<int32_t>(UserAuthResultCode::OHOS_CHECK_PERMISSION_FAILED);
32 }
33 if (result == INVALID_PARAMETERS) {
34 return static_cast<int32_t>(UserAuthResultCode::OHOS_INVALID_PARAM);
35 }
36 if (result == CHECK_SYSTEM_APP_FAILED) {
37 return static_cast<int32_t>(UserAuthResultCode::OHOS_CHECK_SYSTEM_APP_FAILED);
38 }
39 if (result == HARDWARE_NOT_SUPPORTED) {
40 return static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR);
41 }
42 if (result > (INT32_MAX - static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V10_MIN))) {
43 return static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR);
44 }
45 int32_t resultCodeV10 = result + static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V10_MIN);
46 if (resultCodeV10 >= static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V10_MIN) &&
47 resultCodeV10 <= static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V10_MAX)) {
48 IAM_LOGI("version GetResultCodeV10 resultCodeV10 result: %{public}d", resultCodeV10);
49 return resultCodeV10;
50 }
51 IAM_LOGE("version GetResultCodeV10 resultCodeV10 error");
52 return static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR);
53 }
54
GetResultCodeV20(int32_t result)55 int32_t UserAuthHelper::GetResultCodeV20(int32_t result)
56 {
57 if (result == INVALID_PARAMETERS) {
58 return static_cast<int32_t>(UserAuthResultCode::PARAM_VERIFIED_FAILED);
59 }
60 if (result == REUSE_AUTH_RESULT_FAILED) {
61 return static_cast<int32_t>(UserAuthResultCode::REUSE_AUTH_RESULT_FAILED);
62 }
63 return GetResultCodeV10(result);
64 }
65
CheckUserAuthType(int32_t authType)66 bool UserAuthHelper::CheckUserAuthType(int32_t authType)
67 {
68 if (authType != AuthType::PIN && authType != AuthType::FACE &&
69 authType != AuthType::FINGERPRINT && authType != AuthType::PRIVATE_PIN) {
70 IAM_LOGE("authType check fail:%{public}d", authType);
71 return false;
72 }
73 return true;
74 }
75
CheckAuthTrustLevel(uint32_t authTrustLevel)76 bool UserAuthHelper::CheckAuthTrustLevel(uint32_t authTrustLevel)
77 {
78 if (authTrustLevel != AuthTrustLevel::ATL1 && authTrustLevel != AuthTrustLevel::ATL2 &&
79 authTrustLevel != AuthTrustLevel::ATL3 && authTrustLevel != AuthTrustLevel::ATL4) {
80 IAM_LOGE("authTrustLevel check fail:%{public}d", authTrustLevel);
81 return false;
82 }
83 return true;
84 }
85
CheckReuseUnlockResult(ReuseUnlockResult reuseUnlockResult)86 bool UserAuthHelper::CheckReuseUnlockResult(ReuseUnlockResult reuseUnlockResult)
87 {
88 if (reuseUnlockResult.reuseMode != ReuseMode::AUTH_TYPE_RELEVANT &&
89 reuseUnlockResult.reuseMode != ReuseMode::AUTH_TYPE_IRRELEVANT &&
90 reuseUnlockResult.reuseMode != ReuseMode::CALLER_IRRELEVANT_AUTH_TYPE_RELEVANT &&
91 reuseUnlockResult.reuseMode != ReuseMode::CALLER_IRRELEVANT_AUTH_TYPE_IRRELEVANT) {
92 IAM_LOGE("reuseMode check fail:%{public}u", reuseUnlockResult.reuseMode);
93 return false;
94 }
95 if (reuseUnlockResult.reuseDuration <= 0 || reuseUnlockResult.reuseDuration > MAX_ALLOWABLE_REUSE_DURATION) {
96 IAM_LOGE("reuseDuration check fail:%{public}" PRIu64, reuseUnlockResult.reuseDuration);
97 return false;
98 }
99 return true;
100 }
101 } // namespace UserAuth
102 } // namespace UserIam
103 } // namespace OHOS
104