• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(int32_t userId,uint32_t authType,const ExecutorResultInfo * info,AuthResult * result)58 IAM_STATIC ResultCode SetAuthResult(
59     int32_t userId, uint32_t authType, const ExecutorResultInfo *info, AuthResult *result)
60 {
61     result->userId = userId;
62     result->authType = authType;
63     result->freezingTime = info->freezingTime;
64     result->remainTimes = info->remainTimes;
65     result->result = info->result;
66     if (result->result == RESULT_SUCCESS && authType == PIN_AUTH) {
67         result->rootSecret = CopyBuffer(info->rootSecret);
68         if (!IsBufferValid(result->rootSecret)) {
69             LOG_ERROR("rootSecret is invalid");
70             return RESULT_NO_MEMORY;
71         }
72     }
73     return RESULT_SUCCESS;
74 }
75 
RequestAuthResultFunc(uint64_t contextId,const Buffer * scheduleResult,UserAuthTokenHal * authToken,AuthResult * result)76 ResultCode RequestAuthResultFunc(uint64_t contextId, const Buffer *scheduleResult, UserAuthTokenHal *authToken,
77     AuthResult *result)
78 {
79     if (!IsBufferValid(scheduleResult) || authToken == NULL || result == NULL || result->rootSecret != NULL) {
80         LOG_ERROR("param is invalid");
81         DestoryContextbyId(contextId);
82         return RESULT_BAD_PARAM;
83     }
84 
85     UserAuthContext *userAuthContext = GetContext(contextId);
86     if (userAuthContext == NULL) {
87         LOG_ERROR("context is not found");
88         return RESULT_GENERAL_ERROR;
89     }
90 
91     ExecutorResultInfo *executorResultInfo = CreateExecutorResultInfo(scheduleResult);
92     if (executorResultInfo == NULL) {
93         LOG_ERROR("CreateExecutorResultInfo fail");
94         DestoryContext(userAuthContext);
95         return RESULT_GENERAL_ERROR;
96     }
97 
98     ResultCode ret = RESULT_GENERAL_ERROR;
99     if (executorResultInfo->result != RESULT_SUCCESS) {
100         LOG_ERROR("executor result is not success, result:%{public}d", executorResultInfo->result);
101         goto EXIT;
102     }
103 
104     uint64_t credentialId;
105     ret = FillInContext(userAuthContext, &credentialId, executorResultInfo, SCHEDULE_MODE_AUTH);
106     if (ret != RESULT_SUCCESS) {
107         LOG_ERROR("FillInContext fail");
108         goto EXIT;
109     }
110 
111     ret = GetTokenDataAndSign(userAuthContext, credentialId, SCHEDULE_MODE_AUTH, authToken);
112     if (ret != RESULT_SUCCESS) {
113         LOG_ERROR("sign token failed");
114         goto EXIT;
115     }
116 
117 EXIT:
118     ret = SetAuthResult(userAuthContext->userId, userAuthContext->authType, executorResultInfo, result);
119     if (ret != RESULT_SUCCESS) {
120         LOG_ERROR("set result failed");
121     }
122 
123     DestoryExecutorResultInfo(executorResultInfo);
124     DestoryContext(userAuthContext);
125     return ret;
126 }