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 "dev_auth_module_manager.h"
17 #include "common_defs.h"
18 #include "das_module.h"
19 #include "hc_log.h"
20 #include "hc_types.h"
21 #include "hc_vector.h"
22 #include "account_module.h"
23 #include "das_version_util.h"
24 #include "hitrace_adapter.h"
25 #include "das_token_manager.h"
26
27 DECLARE_HC_VECTOR(AuthModuleVec, AuthModuleBase *);
28 IMPLEMENT_HC_VECTOR(AuthModuleVec, AuthModuleBase *, 2)
29
30 static AuthModuleVec g_authModuleVec;
31 static VersionStruct g_version;
32
GetModule(int moduleType)33 static AuthModuleBase *GetModule(int moduleType)
34 {
35 uint32_t index;
36 AuthModuleBase **module;
37 FOR_EACH_HC_VECTOR(g_authModuleVec, index, module) {
38 if (moduleType == ((*module)->moduleType)) {
39 return *module;
40 }
41 }
42 LOGE("There is no matched module, moduleType: %d", moduleType);
43 return NULL;
44 }
45
IsParamsForDasTokenManagerValid(const char * pkgName,const char * serviceType,Uint8Buff * authId,int userType,int moduleType)46 static bool IsParamsForDasTokenManagerValid(const char *pkgName, const char *serviceType, Uint8Buff *authId,
47 int userType, int moduleType)
48 {
49 if (moduleType != DAS_MODULE) {
50 LOGE("Unsupported method in the module, moduleType: %d", moduleType);
51 return false;
52 }
53 if (pkgName == NULL || serviceType == NULL || authId == NULL || authId->val == NULL) {
54 LOGE("Params is null.");
55 return false;
56 }
57
58 if (HcStrlen(pkgName) == 0 || HcStrlen(serviceType) == 0 || authId->length == 0) {
59 LOGE("The length of params is invalid!");
60 return false;
61 }
62 if (userType < DEVICE_TYPE_ACCESSORY || userType > DEVICE_TYPE_PROXY) {
63 LOGE("Invalid userType, userType: %d", userType);
64 return false;
65 }
66 return true;
67 }
68
RegisterLocalIdentity(const AuthModuleParams * moduleParams,int moduleType)69 int32_t RegisterLocalIdentity(const AuthModuleParams *moduleParams, int moduleType)
70 {
71 if (!IsParamsForDasTokenManagerValid(moduleParams->pkgName, moduleParams->serviceType, moduleParams->authId,
72 moduleParams->userType, moduleType)) {
73 LOGE("Params for RegisterLocalIdentity is invalid.");
74 return HC_ERR_INVALID_PARAMS;
75 }
76 AuthModuleBase *module = GetModule(moduleType);
77 if (module == NULL) {
78 LOGE("Failed to get module for das.");
79 return HC_ERR_MODULE_NOT_FOUNT;
80 }
81 DasAuthModule *dasModule = (DasAuthModule *)module;
82 TokenManagerParams params = {
83 .osAccountId = moduleParams->osAccountId,
84 .pkgName = { (uint8_t *)moduleParams->pkgName, HcStrlen(moduleParams->pkgName) },
85 .serviceType = { (uint8_t *)moduleParams->serviceType, HcStrlen(moduleParams->serviceType) },
86 .authId = { moduleParams->authId->val, moduleParams->authId->length },
87 .userType = moduleParams->userType
88 };
89 int32_t res = dasModule->registerLocalIdentity(¶ms);
90 if (res != HC_SUCCESS) {
91 LOGE("Register local identity failed, res: %x", res);
92 return res;
93 }
94 return HC_SUCCESS;
95 }
96
UnregisterLocalIdentity(const AuthModuleParams * moduleParams,int moduleType)97 int32_t UnregisterLocalIdentity(const AuthModuleParams *moduleParams, int moduleType)
98 {
99 if (!IsParamsForDasTokenManagerValid(moduleParams->pkgName, moduleParams->serviceType, moduleParams->authId,
100 moduleParams->userType, moduleType)) {
101 LOGE("Params for UnregisterLocalIdentity is invalid.");
102 return HC_ERR_INVALID_PARAMS;
103 }
104 AuthModuleBase *module = GetModule(moduleType);
105 if (module == NULL) {
106 LOGE("Failed to get module for das.");
107 return HC_ERR_MODULE_NOT_FOUNT;
108 }
109 DasAuthModule *dasModule = (DasAuthModule *)module;
110 TokenManagerParams params = {
111 .osAccountId = moduleParams->osAccountId,
112 .pkgName = { (uint8_t *)moduleParams->pkgName, HcStrlen(moduleParams->pkgName) },
113 .serviceType = { (uint8_t *)moduleParams->serviceType, HcStrlen(moduleParams->serviceType) },
114 .authId = { moduleParams->authId->val, moduleParams->authId->length },
115 .userType = moduleParams->userType
116 };
117 int32_t res = dasModule->unregisterLocalIdentity(¶ms);
118 if (res != HC_SUCCESS) {
119 LOGE("Unregister local identity failed, res: %x", res);
120 return res;
121 }
122 return HC_SUCCESS;
123 }
124
DeletePeerAuthInfo(const AuthModuleParams * moduleParams,int moduleType)125 int32_t DeletePeerAuthInfo(const AuthModuleParams *moduleParams, int moduleType)
126 {
127 if (!IsParamsForDasTokenManagerValid(moduleParams->pkgName, moduleParams->serviceType, moduleParams->authId,
128 moduleParams->userType, moduleType)) {
129 LOGE("Params for DeletePeerAuthInfo is invalid.");
130 return HC_ERR_INVALID_PARAMS;
131 }
132 AuthModuleBase *module = GetModule(moduleType);
133 if (module == NULL) {
134 LOGE("Failed to get module for das.");
135 return HC_ERR_MODULE_NOT_FOUNT;
136 }
137 DasAuthModule *dasModule = (DasAuthModule *)module;
138 TokenManagerParams params = {
139 .osAccountId = moduleParams->osAccountId,
140 .pkgName = { (uint8_t *)moduleParams->pkgName, HcStrlen(moduleParams->pkgName) },
141 .serviceType = { (uint8_t *)moduleParams->serviceType, HcStrlen(moduleParams->serviceType) },
142 .authId = { moduleParams->authId->val, moduleParams->authId->length },
143 .userType = moduleParams->userType
144 };
145 int32_t res = dasModule->deletePeerAuthInfo(¶ms);
146 if (res != HC_SUCCESS) {
147 LOGE("Delete peer authInfo failed, res: %x", res);
148 return res;
149 }
150 return HC_SUCCESS;
151 }
152
GetPublicKey(int moduleType,AuthModuleParams * moduleParams,Uint8Buff * returnPk)153 int32_t GetPublicKey(int moduleType, AuthModuleParams *moduleParams, Uint8Buff *returnPk)
154 {
155 if (moduleParams == NULL || returnPk == NULL ||
156 !IsParamsForDasTokenManagerValid(moduleParams->pkgName, moduleParams->serviceType,
157 moduleParams->authId, moduleParams->userType, moduleType)) {
158 LOGE("Params for GetPublicKey is invalid.");
159 return HC_ERR_INVALID_PARAMS;
160 }
161 AuthModuleBase *module = GetModule(moduleType);
162 if (module == NULL) {
163 LOGE("Failed to get module for das.");
164 return HC_ERR_MODULE_NOT_FOUNT;
165 }
166 DasAuthModule *dasModule = (DasAuthModule *)module;
167 TokenManagerParams params = {
168 .osAccountId = moduleParams->osAccountId,
169 .pkgName = { (uint8_t *)moduleParams->pkgName, HcStrlen(moduleParams->pkgName) },
170 .serviceType = { (uint8_t *)moduleParams->serviceType, HcStrlen(moduleParams->serviceType) },
171 .authId = { moduleParams->authId->val, moduleParams->authId->length },
172 .userType = moduleParams->userType
173 };
174 int32_t res = dasModule->getPublicKey(¶ms, returnPk);
175 if (res != HC_SUCCESS) {
176 LOGE("Get public key failed, res: %d", res);
177 return res;
178 }
179 return HC_SUCCESS;
180 }
181
CheckMsgRepeatability(const CJson * in,int moduleType)182 int32_t CheckMsgRepeatability(const CJson *in, int moduleType)
183 {
184 if (in == NULL) {
185 LOGE("Params is null.");
186 return HC_ERR_NULL_PTR;
187 }
188 AuthModuleBase *module = GetModule(moduleType);
189 if (module == NULL) {
190 LOGE("Failed to get module for das.");
191 return HC_ERR_MODULE_NOT_FOUNT;
192 }
193 return module->isMsgNeedIgnore(in) ? HC_ERR_IGNORE_MSG : HC_SUCCESS;
194 }
195
CreateTask(int32_t * taskId,const CJson * in,CJson * out,int moduleType)196 int32_t CreateTask(int32_t *taskId, const CJson *in, CJson *out, int moduleType)
197 {
198 if (in == NULL || out == NULL || taskId == NULL) {
199 LOGE("Params is null.");
200 return HC_ERR_NULL_PTR;
201 }
202 LOGI("Start to create task, moduleType: %d", moduleType);
203 AuthModuleBase *module = GetModule(moduleType);
204 if (module == NULL) {
205 LOGE("Failed to get module!");
206 return HC_ERR_MODULE_NOT_FOUNT;
207 }
208 int32_t res = module->createTask(taskId, in, out);
209 if (res != HC_SUCCESS) {
210 LOGE("Create task failed, taskId: %d, moduleType: %d, res: %d", *taskId, moduleType, res);
211 return res;
212 }
213 LOGI("Create task success, taskId: %d, moduleType: %d", *taskId, moduleType);
214 return HC_SUCCESS;
215 }
216
ProcessTask(int taskId,const CJson * in,CJson * out,int32_t * status,int moduleType)217 int32_t ProcessTask(int taskId, const CJson *in, CJson *out, int32_t *status, int moduleType)
218 {
219 if (in == NULL || out == NULL || status == NULL) {
220 LOGE("Params is null.");
221 return HC_ERR_NULL_PTR;
222 }
223 AuthModuleBase *module = GetModule(moduleType);
224 if (module == NULL) {
225 LOGE("Failed to get module!");
226 return HC_ERR_MODULE_NOT_FOUNT;
227 }
228 int32_t res = module->processTask(taskId, in, out, status);
229 if (res != HC_SUCCESS) {
230 LOGE("Process task failed, taskId: %d, moduleType: %d, res: %d.", taskId, moduleType, res);
231 return res;
232 }
233 res = AddSingleVersionToJson(out, &g_version);
234 if (res != HC_SUCCESS) {
235 LOGE("AddSingleVersionToJson failed, res: %x.", res);
236 return res;
237 }
238 LOGI("Process task success, taskId: %d, moduleType: %d.", taskId, moduleType);
239 return res;
240 }
241
DestroyTask(int taskId,int moduleType)242 void DestroyTask(int taskId, int moduleType)
243 {
244 AuthModuleBase *module = GetModule(moduleType);
245 if (module == NULL) {
246 return;
247 }
248 module->destroyTask(taskId);
249 }
250
InitModules(void)251 int32_t InitModules(void)
252 {
253 g_authModuleVec = CREATE_HC_VECTOR(AuthModuleVec);
254 InitGroupAndModuleVersion(&g_version);
255 int32_t res;
256 const AuthModuleBase *dasModule = GetDasModule();
257 if (dasModule != NULL) {
258 res = dasModule->init();
259 if (res != HC_SUCCESS) {
260 LOGE("[ModuleMgr]: Init das module fail. [Res]: %d", res);
261 DestroyModules();
262 return res;
263 }
264 (void)g_authModuleVec.pushBack(&g_authModuleVec, &dasModule);
265 g_version.third |= dasModule->moduleType;
266 }
267 const AuthModuleBase *accountModule = GetAccountModule();
268 if (accountModule != NULL) {
269 res = accountModule->init();
270 if (res != HC_SUCCESS) {
271 LOGE("[ModuleMgr]: Init account module fail. [Res]: %d", res);
272 DestroyModules();
273 return res;
274 }
275 (void)g_authModuleVec.pushBack(&g_authModuleVec, &accountModule);
276 g_version.third |= accountModule->moduleType;
277 }
278 LOGI("Init modules success!");
279 return HC_SUCCESS;
280 }
281
DestroyModules(void)282 void DestroyModules(void)
283 {
284 uint32_t index;
285 AuthModuleBase **module;
286 FOR_EACH_HC_VECTOR(g_authModuleVec, index, module) {
287 (*module)->destroy();
288 }
289 DESTROY_HC_VECTOR(AuthModuleVec, &g_authModuleVec);
290 (void)memset_s(&g_version, sizeof(VersionStruct), 0, sizeof(VersionStruct));
291 }
292
AddAuthModulePlugin(const AuthModuleBase * plugin)293 int32_t AddAuthModulePlugin(const AuthModuleBase *plugin)
294 {
295 if (plugin == NULL || plugin->init == NULL || plugin->destroy == NULL ||
296 plugin->createTask == NULL || plugin->processTask == NULL || plugin->destroyTask == NULL) {
297 LOGE("The plugin is invalid.");
298 return HC_ERR_INVALID_PARAMS;
299 }
300 int32_t res = plugin->init();
301 if (res != HC_SUCCESS) {
302 LOGE("[ModuleMgr]: Init module plugin fail. [Res]: %d", res);
303 return HC_ERR_INIT_FAILED;
304 }
305 bool isNeedReplace = false;
306 uint32_t index;
307 AuthModuleBase **pluginPtr;
308 FOR_EACH_HC_VECTOR(g_authModuleVec, index, pluginPtr) {
309 if ((*pluginPtr)->moduleType == plugin->moduleType) {
310 isNeedReplace = true;
311 break;
312 }
313 }
314 if (g_authModuleVec.pushBack(&g_authModuleVec, &plugin) == NULL) {
315 LOGE("[ModuleMgr]: Push module plugin to vector fail.");
316 plugin->destroy();
317 return HC_ERR_ALLOC_MEMORY;
318 }
319 if (isNeedReplace) {
320 LOGI("[ModuleMgr]: Replace module plugin. [Name]: %d", plugin->moduleType);
321 HC_VECTOR_POPELEMENT(&g_authModuleVec, pluginPtr, index);
322 } else {
323 LOGI("[ModuleMgr]: Add new module plugin. [Name]: %d", plugin->moduleType);
324 }
325 return HC_SUCCESS;
326 }
327
DelAuthModulePlugin(int32_t moduleType)328 void DelAuthModulePlugin(int32_t moduleType)
329 {
330 uint32_t index;
331 AuthModuleBase **pluginPtr;
332 FOR_EACH_HC_VECTOR(g_authModuleVec, index, pluginPtr) {
333 if ((*pluginPtr)->moduleType == moduleType) {
334 LOGI("[ModuleMgr]: Delete module plugin success. [Name]: %d", moduleType);
335 (*pluginPtr)->destroy();
336 HC_VECTOR_POPELEMENT(&g_authModuleVec, pluginPtr, index);
337 break;
338 }
339 }
340 }
341