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 "user_auth_funcs.h"
17
18 #include "securec.h"
19
20 #include "adaptor_algorithm.h"
21 #include "adaptor_log.h"
22 #include "adaptor_time.h"
23 #include "context_manager.h"
24 #include "executor_message.h"
25 #include "idm_database.h"
26 #include "user_sign_centre.h"
27
28 #ifdef IAM_TEST_ENABLE
29 #define IAM_STATIC
30 #else
31 #define IAM_STATIC static
32 #endif
33
GenerateSolutionFunc(AuthSolutionHal param,LinkedList ** schedules)34 ResultCode GenerateSolutionFunc(AuthSolutionHal param, LinkedList **schedules)
35 {
36 if (schedules == NULL) {
37 LOG_ERROR("schedules is null");
38 return RESULT_BAD_PARAM;
39 }
40 UserAuthContext *authContext = NULL;
41 ResultCode result = GenerateAuthContext(param, &authContext);
42 if (result != RESULT_SUCCESS) {
43 LOG_ERROR("GenerateAuthContext fail %{public}d", result);
44 return result;
45 }
46 if (authContext == NULL) {
47 LOG_ERROR("authContext is null");
48 return RESULT_GENERAL_ERROR;
49 }
50 ResultCode ret = CopySchedules(authContext, schedules);
51 if (ret != RESULT_SUCCESS) {
52 DestoryContext(authContext);
53 return ret;
54 }
55 return ret;
56 }
57
SetAuthResult(uint32_t authType,const ExecutorResultInfo * info,AuthResult * result)58 IAM_STATIC ResultCode SetAuthResult(uint32_t authType, const ExecutorResultInfo *info, AuthResult *result)
59 {
60 result->authType = authType;
61 result->freezingTime = info->freezingTime;
62 result->remainTimes = info->remainTimes;
63 result->result = info->result;
64 if (result->result == RESULT_SUCCESS && authType == PIN_AUTH) {
65 result->rootSecret = CopyBuffer(info->rootSecret);
66 if (!IsBufferValid(result->rootSecret)) {
67 LOG_ERROR("rootSecret is invalid");
68 return RESULT_NO_MEMORY;
69 }
70 }
71 return RESULT_SUCCESS;
72 }
73
RequestAuthResultFunc(uint64_t contextId,const Buffer * scheduleResult,UserAuthTokenHal * authToken,AuthResult * result)74 ResultCode RequestAuthResultFunc(uint64_t contextId, const Buffer *scheduleResult, UserAuthTokenHal *authToken,
75 AuthResult *result)
76 {
77 if (!IsBufferValid(scheduleResult) || authToken == NULL || result == NULL || result->rootSecret != NULL) {
78 LOG_ERROR("param is invalid");
79 DestoryContextbyId(contextId);
80 return RESULT_BAD_PARAM;
81 }
82
83 UserAuthContext *userAuthContext = GetContext(contextId);
84 if (userAuthContext == NULL) {
85 LOG_ERROR("context is not found");
86 return RESULT_GENERAL_ERROR;
87 }
88
89 ExecutorResultInfo *executorResultInfo = CreateExecutorResultInfo(scheduleResult);
90 if (executorResultInfo == NULL) {
91 LOG_ERROR("CreateExecutorResultInfo fail");
92 DestoryContext(userAuthContext);
93 return RESULT_GENERAL_ERROR;
94 }
95
96 ResultCode ret = RESULT_GENERAL_ERROR;
97 if (executorResultInfo->result != RESULT_SUCCESS) {
98 LOG_ERROR("executor result is not success, result:%{public}d", executorResultInfo->result);
99 goto EXIT;
100 }
101
102 uint64_t credentialId;
103 ret = FillInContext(userAuthContext, &credentialId, executorResultInfo, SCHEDULE_MODE_AUTH);
104 if (ret != RESULT_SUCCESS) {
105 LOG_ERROR("FillInContext fail");
106 goto EXIT;
107 }
108
109 ret = GetTokenDataAndSign(userAuthContext, credentialId, SCHEDULE_MODE_AUTH, authToken);
110 if (ret != RESULT_SUCCESS) {
111 LOG_ERROR("sign token failed");
112 goto EXIT;
113 }
114
115 EXIT:
116 ret = SetAuthResult(userAuthContext->authType, executorResultInfo, result);
117 if (ret != RESULT_SUCCESS) {
118 LOG_ERROR("set result failed");
119 }
120
121 DestoryExecutorResultInfo(executorResultInfo);
122 DestoryContext(userAuthContext);
123 return ret;
124 }