• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 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 "account_module.h"
17 #include "account_module_defines.h"
18 #include "account_multi_task_manager.h"
19 #include "account_version_util.h"
20 #include "alg_loader.h"
21 #include "common_defs.h"
22 #include "device_auth_defines.h"
23 #include "hc_log.h"
24 #include "json_utils.h"
25 #include "pake_v2_auth_client_task.h"
26 #include "pake_v2_auth_server_task.h"
27 
28 #define ACCOUNT_CLIENT_FIRST_MESSAGE 0x0000
29 #define ACCOUNT_CLIENT_STEP_MASK 0x000F
30 
31 typedef struct {
32     AuthModuleBase base;
33 } AccountModule;
34 
IsAccountMsgNeedIgnore(const CJson * in)35 static bool IsAccountMsgNeedIgnore(const CJson *in)
36 {
37     int32_t opCode;
38     if (GetIntFromJson(in, FIELD_OPERATION_CODE, &opCode) != HC_SUCCESS) {
39         LOGE("Get opCode failed.");
40         return true;
41     }
42     const char *key = NULL;
43     if (opCode == OP_BIND) {
44         key = FIELD_MESSAGE;
45     } else if (opCode == AUTHENTICATE) {
46         key = FIELD_STEP;
47     } else {
48         LOGE("Invalid opCode: %d.", opCode);
49         return true;
50     }
51     uint32_t message;
52     if (GetIntFromJson(in, key, (int32_t *)&message) == HC_SUCCESS) {
53         if ((message & ACCOUNT_CLIENT_STEP_MASK) != ACCOUNT_CLIENT_FIRST_MESSAGE) {
54             LOGI("The message is repeated, ignore it, code: %u", message);
55             return true;
56         }
57     }
58     return false;
59 }
60 
CreateAccountTask(int32_t * taskId,const CJson * in,CJson * out)61 static int32_t CreateAccountTask(int32_t *taskId, const CJson *in, CJson *out)
62 {
63     if (taskId == NULL || in == NULL || out == NULL) {
64         LOGE("Params is null in account task.");
65         return HC_ERR_NULL_PTR;
66     }
67     if (IsAccountMsgNeedIgnore(in)) {
68         return HC_ERR_IGNORE_MSG;
69     }
70     AccountMultiTaskManager *authManager = GetAccountMultiTaskManager();
71     if (authManager == NULL) {
72         LOGE("Get multi auth manager instance failed.");
73         return HC_ERROR;
74     }
75     if (authManager->isTaskNumUpToMax() == true) {
76         LOGE("Account auth task is full.");
77         return HC_ERR_ACCOUNT_TASK_IS_FULL;
78     }
79     AccountTask *newTask = CreateAccountTaskT(taskId, in, out);
80     if (newTask == NULL) {
81         LOGE("Create account related task failed.");
82         return HC_ERR_ALLOC_MEMORY;
83     }
84     int32_t res = authManager->addTaskToManager(newTask);
85     if (res != HC_SUCCESS) {
86         LOGE("Add new task into task manager failed, res: %d.", res);
87         newTask->destroyTask(newTask);
88     }
89     return res;
90 }
91 
ProcessAccountTask(int32_t taskId,const CJson * in,CJson * out,int32_t * status)92 static int32_t ProcessAccountTask(int32_t taskId, const CJson *in, CJson *out, int32_t *status)
93 {
94     AccountMultiTaskManager *authManager = GetAccountMultiTaskManager();
95     if (authManager == NULL) {
96         LOGE("Get multi auth manager instance failed.");
97         return HC_ERROR;
98     }
99     AccountTask *currentTask = authManager->getTaskFromManager(taskId);
100     if (currentTask == NULL) {
101         LOGE("Get task from manager failed, taskId: %d.", taskId);
102         return HC_ERR_TASK_ID_IS_NOT_MATCH;
103     }
104     LOGD("Begin process account related task, taskId: %d.", taskId);
105     return currentTask->processTask(currentTask, in, out, status);
106 }
107 
DestroyAccountTask(int32_t taskId)108 static void DestroyAccountTask(int32_t taskId)
109 {
110     AccountMultiTaskManager *authManager = GetAccountMultiTaskManager();
111     if (authManager == NULL) {
112         LOGE("Get multi auth manager instance failed.");
113         return;
114     }
115     LOGI("Delete taskId:%d from task manager.", taskId);
116     authManager->deleteTaskFromManager(taskId);
117 }
118 
InitAccountModule(void)119 static int32_t InitAccountModule(void)
120 {
121     InitVersionInfos();
122     InitAccountMultiTaskManager();
123     return HC_SUCCESS;
124 }
125 
DestroyAccountModule(void)126 static void DestroyAccountModule(void)
127 {
128     DestroyAccountMultiTaskManager();
129     DestroyVersionInfos();
130 }
131 
132 static AccountModule g_module = {
133     .base.moduleType = ACCOUNT_MODULE,
134     .base.init = InitAccountModule,
135     .base.destroy = DestroyAccountModule,
136     .base.isMsgNeedIgnore = IsAccountMsgNeedIgnore,
137     .base.createTask = CreateAccountTask,
138     .base.processTask = ProcessAccountTask,
139     .base.destroyTask = DestroyAccountTask,
140 };
141 
GetAccountModule(void)142 const AuthModuleBase *GetAccountModule(void)
143 {
144     return (const AuthModuleBase *)&g_module;
145 }
146