1 /*
2 * Copyright (C) 2021-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 "das_module.h"
17 #include "common_defs.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "hc_vector.h"
21 #include "das_task_main.h"
22 #include "das_token_manager.h"
23
24 #define DAS_CLIENT_STEP_MASK 0xF00F
25 #define DAS_CLIENT_FIRST_MESSAGE 0x0001
26
27 DECLARE_HC_VECTOR(TaskInModuleVec, void *);
28 IMPLEMENT_HC_VECTOR(TaskInModuleVec, void *, 1)
29
30 static TaskInModuleVec g_taskInModuleVec;
31
RegisterDasLocalIdentity(const TokenManagerParams * params)32 static int32_t RegisterDasLocalIdentity(const TokenManagerParams *params)
33 {
34 return RegisterLocalIdentityInTask(params);
35 }
36
UnregisterDasLocalIdentity(const TokenManagerParams * params)37 static int32_t UnregisterDasLocalIdentity(const TokenManagerParams *params)
38 {
39 return UnregisterLocalIdentityInTask(params);
40 }
41
DeleteDasPeerAuthInfo(const TokenManagerParams * params)42 static int32_t DeleteDasPeerAuthInfo(const TokenManagerParams *params)
43 {
44 return DeletePeerAuthInfoInTask(params);
45 }
46
GetDasPublicKey(const TokenManagerParams * params,Uint8Buff * returnPk)47 static int32_t GetDasPublicKey(const TokenManagerParams *params, Uint8Buff *returnPk)
48 {
49 return GetPublicKeyInTask(params, returnPk);
50 }
51
IsDasMsgNeedIgnore(const CJson * in)52 static bool IsDasMsgNeedIgnore(const CJson *in)
53 {
54 int32_t message = 0;
55 if (GetIntFromJson(in, FIELD_MESSAGE, &message) != HC_SUCCESS) {
56 LOGD("There is no message code."); // There is no message code in the client's createTask request params
57 return false;
58 }
59 if (((uint32_t)message & DAS_CLIENT_STEP_MASK) == DAS_CLIENT_FIRST_MESSAGE) {
60 return false;
61 }
62
63 LOGI("The message needs to ignore, message: %" LOG_PUB "d.", message);
64 return true;
65 }
66
CreateDasTask(int32_t * taskId,const CJson * in,CJson * out)67 static int CreateDasTask(int32_t *taskId, const CJson *in, CJson *out)
68 {
69 if (taskId == NULL || in == NULL || out == NULL) {
70 LOGE("Params is null.");
71 return HC_ERR_NULL_PTR;
72 }
73 if (IsDasMsgNeedIgnore(in)) {
74 return HC_ERR_IGNORE_MSG;
75 }
76 Task *task = CreateTaskT(taskId, in, out);
77 if (task == NULL) {
78 LOGE("Create das task failed.");
79 return HC_ERR_ALLOC_MEMORY;
80 }
81
82 if (g_taskInModuleVec.pushBackT(&g_taskInModuleVec, (void *)task) == NULL) {
83 LOGE("Failed to push task!");
84 task->destroyTask(task);
85 return HC_ERR_ALLOC_MEMORY;
86 }
87 return HC_SUCCESS;
88 }
89
ProcessDasTask(int32_t taskId,const CJson * in,CJson * out,int32_t * status)90 static int ProcessDasTask(int32_t taskId, const CJson* in, CJson* out, int32_t *status)
91 {
92 if (status == NULL || in == NULL || out == NULL) {
93 LOGE("Params is null.");
94 return HC_ERR_NULL_PTR;
95 }
96 uint32_t index;
97 void **ptr = NULL;
98 FOR_EACH_HC_VECTOR(g_taskInModuleVec, index, ptr) {
99 Task *temp = (Task *)*ptr;
100 if (taskId == temp->taskId) {
101 return temp->processTask(temp, in, out, status);
102 }
103 }
104
105 LOGE("Task doesn't exist, taskId: %" LOG_PUB "d.", taskId);
106 return HC_ERR_TASK_ID_IS_NOT_MATCH;
107 }
108
DestroyDasTask(int taskId)109 static void DestroyDasTask(int taskId)
110 {
111 uint32_t index;
112 void **ptr = NULL;
113 FOR_EACH_HC_VECTOR(g_taskInModuleVec, index, ptr) {
114 Task *temp = (Task *)(*ptr);
115 if (taskId == temp->taskId) {
116 temp->destroyTask(temp);
117 void *tempPtr = NULL;
118 HC_VECTOR_POPELEMENT(&g_taskInModuleVec, &tempPtr, index);
119 return;
120 }
121 }
122 }
123
InitDasModule(void)124 static int32_t InitDasModule(void)
125 {
126 int32_t res = InitDasProtocolEntities();
127 if (res != HC_SUCCESS) {
128 LOGE("Init das protocol entities failed.");
129 return res;
130 }
131 g_taskInModuleVec = CREATE_HC_VECTOR(TaskInModuleVec);
132 return HC_SUCCESS;
133 }
134
DestroyDasModule(void)135 static void DestroyDasModule(void)
136 {
137 uint32_t index;
138 void **ptr = NULL;
139 FOR_EACH_HC_VECTOR(g_taskInModuleVec, index, ptr) {
140 ((Task *)(*ptr))->destroyTask((Task *)(*ptr));
141 }
142 DESTROY_HC_VECTOR(TaskInModuleVec, &g_taskInModuleVec);
143 DestroyDasProtocolEntities();
144 }
145
146 static DasAuthModule g_dasModule = {
147 .base.moduleType = DAS_MODULE,
148 .base.init = InitDasModule,
149 .base.destroy = DestroyDasModule,
150 .base.isMsgNeedIgnore = IsDasMsgNeedIgnore,
151 .base.createTask = CreateDasTask,
152 .base.processTask = ProcessDasTask,
153 .base.destroyTask = DestroyDasTask,
154 .registerLocalIdentity = RegisterDasLocalIdentity,
155 .unregisterLocalIdentity = UnregisterDasLocalIdentity,
156 .deletePeerAuthInfo = DeleteDasPeerAuthInfo,
157 .getPublicKey = GetDasPublicKey
158 };
159
GetDasModule(void)160 const AuthModuleBase *GetDasModule(void)
161 {
162 return (const AuthModuleBase *)&g_dasModule;
163 }
164