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
GenerateSolutionFunc(AuthSolutionHal param,LinkedList ** schedules)28 int32_t GenerateSolutionFunc(AuthSolutionHal param, LinkedList **schedules)
29 {
30 if (schedules == NULL) {
31 LOG_ERROR("schedules is null");
32 return RESULT_BAD_PARAM;
33 }
34 UserAuthContext *authContext = NULL;
35 ResultCode result = GenerateAuthContext(param, &authContext);
36 if (result != RESULT_SUCCESS) {
37 LOG_ERROR("GenerateAuthContext fail %{public}d", result);
38 return result;
39 }
40 if (authContext == NULL) {
41 LOG_ERROR("authContext is null");
42 return RESULT_GENERAL_ERROR;
43 }
44 int32_t ret = CopySchedules(authContext, schedules);
45 if (ret != RESULT_SUCCESS) {
46 DestoryContext(authContext);
47 return ret;
48 }
49 return ret;
50 }
51
SetAuthResult(uint32_t authType,const ExecutorResultInfo * info,AuthResult * result)52 static int32_t SetAuthResult(uint32_t authType, const ExecutorResultInfo *info, AuthResult *result)
53 {
54 result->freezingTime = info->freezingTime;
55 result->remainTimes = info->remainTimes;
56 result->result = info->result;
57 if (result->result == RESULT_SUCCESS && authType == PIN_AUTH) {
58 result->rootSecret = CopyBuffer(info->rootSecret);
59 if (!IsBufferValid(result->rootSecret)) {
60 LOG_ERROR("rootSecret is invalid");
61 return RESULT_NO_MEMORY;
62 }
63 }
64 return RESULT_SUCCESS;
65 }
66
RequestAuthResultFunc(uint64_t contextId,const Buffer * scheduleResult,UserAuthTokenHal * authToken,AuthResult * result)67 int32_t RequestAuthResultFunc(uint64_t contextId, const Buffer *scheduleResult, UserAuthTokenHal *authToken,
68 AuthResult *result)
69 {
70 if (!IsBufferValid(scheduleResult) || authToken == NULL || result == NULL || result->rootSecret != NULL) {
71 LOG_ERROR("param is invalid");
72 DestoryContextbyId(contextId);
73 return RESULT_BAD_PARAM;
74 }
75 ExecutorResultInfo *executorResultInfo = CreateExecutorResultInfo(scheduleResult);
76 if (executorResultInfo == NULL) {
77 LOG_ERROR("executorResultInfo is null");
78 DestoryContextbyId(contextId);
79 return RESULT_UNKNOWN;
80 }
81
82 UserAuthContext *userAuthContext = GetContext(contextId);
83 if (userAuthContext == NULL) {
84 LOG_ERROR("userAuthContext is null");
85 DestoryExecutorResultInfo(executorResultInfo);
86 return RESULT_UNKNOWN;
87 }
88 uint64_t credentialId;
89 int32_t ret = FillInContext(userAuthContext, &credentialId, executorResultInfo);
90 if (ret != RESULT_SUCCESS) {
91 LOG_ERROR("get info failed");
92 goto EXIT;
93 }
94 ret = ScheduleOnceFinish(userAuthContext, executorResultInfo->scheduleId);
95 if (ret != RESULT_SUCCESS) {
96 LOG_ERROR("failed to finish schedule");
97 goto EXIT;
98 }
99 if (executorResultInfo->result == RESULT_SUCCESS) {
100 ret = GetTokenDataAndSign(userAuthContext, credentialId, SCHEDULE_MODE_AUTH, authToken);
101 if (ret != RESULT_SUCCESS) {
102 LOG_ERROR("sign token failed");
103 (void)memset_s(authToken, sizeof(UserAuthTokenHal), 0, sizeof(UserAuthTokenHal));
104 goto EXIT;
105 }
106 } else {
107 (void)memset_s(authToken, sizeof(UserAuthTokenHal), 0, sizeof(UserAuthTokenHal));
108 }
109 ret = SetAuthResult(userAuthContext->authType, executorResultInfo, result);
110 if (ret != RESULT_SUCCESS) {
111 LOG_ERROR("set result failed");
112 (void)memset_s(authToken, sizeof(UserAuthTokenHal), 0, sizeof(UserAuthTokenHal));
113 }
114
115 EXIT:
116 DestoryExecutorResultInfo(executorResultInfo);
117 DestoryContext(userAuthContext);
118 return ret;
119 }