• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: %" LOG_PUB "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: %" LOG_PUB "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: %" LOG_PUB "d", userType);
64         return false;
65     }
66     return true;
67 }
68 
BuildTokenManagerParams(const AuthModuleParams * moduleParams)69 static TokenManagerParams BuildTokenManagerParams(const AuthModuleParams *moduleParams)
70 {
71     TokenManagerParams params = {
72         .osAccountId = moduleParams->osAccountId,
73         .pkgName = { (uint8_t *)moduleParams->pkgName, HcStrlen(moduleParams->pkgName) },
74         .serviceType = { (uint8_t *)moduleParams->serviceType, HcStrlen(moduleParams->serviceType) },
75         .authId = { moduleParams->authId->val, moduleParams->authId->length },
76         .userType = moduleParams->userType
77     };
78     return params;
79 }
80 
RegisterLocalIdentity(const AuthModuleParams * moduleParams,int moduleType)81 int32_t RegisterLocalIdentity(const AuthModuleParams *moduleParams, int moduleType)
82 {
83     if (!IsParamsForDasTokenManagerValid(moduleParams->pkgName, moduleParams->serviceType, moduleParams->authId,
84         moduleParams->userType, moduleType)) {
85         LOGE("Params for RegisterLocalIdentity is invalid.");
86         return HC_ERR_INVALID_PARAMS;
87     }
88     AuthModuleBase *module = GetModule(moduleType);
89     if (module == NULL) {
90         LOGE("Failed to get module for das with moduleType.");
91         return HC_ERR_MODULE_NOT_FOUNT;
92     }
93     TokenManagerParams params = BuildTokenManagerParams(moduleParams);
94     DasAuthModule *dasModule = (DasAuthModule *)module;
95     int32_t res = dasModule->registerLocalIdentity(&params);
96     if (res != HC_SUCCESS) {
97         LOGE("Register local identity failed, res: %" LOG_PUB "x", res);
98         return res;
99     }
100     return HC_SUCCESS;
101 }
102 
UnregisterLocalIdentity(const AuthModuleParams * moduleParams,int moduleType)103 int32_t UnregisterLocalIdentity(const AuthModuleParams *moduleParams, int moduleType)
104 {
105     if (!IsParamsForDasTokenManagerValid(moduleParams->pkgName, moduleParams->serviceType, moduleParams->authId,
106         moduleParams->userType, moduleType)) {
107         LOGE("Params for UnregisterLocalIdentity is invalid.");
108         return HC_ERR_INVALID_PARAMS;
109     }
110     AuthModuleBase *module = GetModule(moduleType);
111     if (module == NULL) {
112         LOGE("Failed to get module for das with moduleType.");
113         return HC_ERR_MODULE_NOT_FOUNT;
114     }
115     TokenManagerParams params = BuildTokenManagerParams(moduleParams);
116     DasAuthModule *dasModule = (DasAuthModule *)module;
117     int32_t res = dasModule->unregisterLocalIdentity(&params);
118     if (res != HC_SUCCESS) {
119         LOGE("Unregister local identity failed, res: %" LOG_PUB "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     TokenManagerParams params = BuildTokenManagerParams(moduleParams);
138     DasAuthModule *dasModule = (DasAuthModule *)module;
139     int32_t res = dasModule->deletePeerAuthInfo(&params);
140     if (res != HC_SUCCESS) {
141         LOGE("Delete peer authInfo failed, res: %" LOG_PUB "x", res);
142         return res;
143     }
144     return HC_SUCCESS;
145 }
146 
GetPublicKey(int moduleType,AuthModuleParams * moduleParams,Uint8Buff * returnPk)147 int32_t GetPublicKey(int moduleType, AuthModuleParams *moduleParams, Uint8Buff *returnPk)
148 {
149     if (moduleParams == NULL || returnPk == NULL ||
150         !IsParamsForDasTokenManagerValid(moduleParams->pkgName, moduleParams->serviceType,
151         moduleParams->authId, moduleParams->userType, moduleType)) {
152         LOGE("Params for GetPublicKey is invalid.");
153         return HC_ERR_INVALID_PARAMS;
154     }
155     AuthModuleBase *module = GetModule(moduleType);
156     if (module == NULL) {
157         LOGE("Failed to get module for das.");
158         return HC_ERR_MODULE_NOT_FOUNT;
159     }
160     TokenManagerParams params = BuildTokenManagerParams(moduleParams);
161     DasAuthModule *dasModule = (DasAuthModule *)module;
162     int32_t res = dasModule->getPublicKey(&params, returnPk);
163     if (res != HC_SUCCESS) {
164         LOGE("Get public key failed, res: %" LOG_PUB "d", res);
165         return res;
166     }
167     return HC_SUCCESS;
168 }
169 
CheckMsgRepeatability(const CJson * in,int moduleType)170 int32_t CheckMsgRepeatability(const CJson *in, int moduleType)
171 {
172     if (in == NULL) {
173         LOGE("Params is null.");
174         return HC_ERR_NULL_PTR;
175     }
176     AuthModuleBase *module = GetModule(moduleType);
177     if (module == NULL) {
178         LOGE("Failed to get module for das.");
179         return HC_ERR_MODULE_NOT_FOUNT;
180     }
181     return module->isMsgNeedIgnore(in) ? HC_ERR_IGNORE_MSG : HC_SUCCESS;
182 }
183 
CreateTask(int32_t * taskId,const CJson * in,CJson * out,int moduleType)184 int32_t CreateTask(int32_t *taskId, const CJson *in, CJson *out, int moduleType)
185 {
186     if (in == NULL || out == NULL || taskId == NULL) {
187         LOGE("Params is null.");
188         return HC_ERR_NULL_PTR;
189     }
190     LOGI("Start to create task, moduleType: %" LOG_PUB "d", moduleType);
191     AuthModuleBase *module = GetModule(moduleType);
192     if (module == NULL) {
193         LOGE("Failed to get module!");
194         return HC_ERR_MODULE_NOT_FOUNT;
195     }
196     int32_t res = module->createTask(taskId, in, out);
197     if (res != HC_SUCCESS) {
198         LOGE("Create task failed, taskId: %" LOG_PUB "d, moduleType: %" LOG_PUB "d, res: %" LOG_PUB "d", *taskId,
199             moduleType, res);
200         return res;
201     }
202     LOGI("Create task success, taskId: %" LOG_PUB "d, moduleType: %" LOG_PUB "d", *taskId, moduleType);
203     return HC_SUCCESS;
204 }
205 
ProcessTask(int taskId,const CJson * in,CJson * out,int32_t * status,int moduleType)206 int32_t ProcessTask(int taskId, const CJson *in, CJson *out, int32_t *status, int moduleType)
207 {
208     if (in == NULL || out == NULL || status == NULL) {
209         LOGE("Params is null.");
210         return HC_ERR_NULL_PTR;
211     }
212     AuthModuleBase *module = GetModule(moduleType);
213     if (module == NULL) {
214         LOGE("Failed to get module!");
215         return HC_ERR_MODULE_NOT_FOUNT;
216     }
217     int32_t res = module->processTask(taskId, in, out, status);
218     if (res != HC_SUCCESS) {
219         LOGE("Process task failed, taskId: %" LOG_PUB "d, moduleType: %" LOG_PUB "d, res: %" LOG_PUB "d.",
220             taskId, moduleType, res);
221         return res;
222     }
223     res = AddSingleVersionToJson(out, &g_version);
224     if (res != HC_SUCCESS) {
225         LOGE("AddSingleVersionToJson failed, res: %" LOG_PUB "x.", res);
226         return res;
227     }
228     LOGI("Process task success, taskId: %" LOG_PUB "d, moduleType: %" LOG_PUB "d.", taskId, moduleType);
229     return res;
230 }
231 
DestroyTask(int taskId,int moduleType)232 void DestroyTask(int taskId, int moduleType)
233 {
234     AuthModuleBase *module = GetModule(moduleType);
235     if (module == NULL) {
236         return;
237     }
238     module->destroyTask(taskId);
239 }
240 
InitModules(void)241 int32_t InitModules(void)
242 {
243     g_authModuleVec = CREATE_HC_VECTOR(AuthModuleVec);
244     InitGroupAndModuleVersion(&g_version);
245     int32_t res;
246     const AuthModuleBase *dasModule = GetDasModule();
247     if (dasModule != NULL) {
248         res = dasModule->init();
249         if (res != HC_SUCCESS) {
250             LOGE("[ModuleMgr]: Init das module fail. [Res]: %" LOG_PUB "d", res);
251             DestroyModules();
252             return res;
253         }
254         if (g_authModuleVec.pushBack(&g_authModuleVec, &dasModule) == NULL) {
255             LOGE("[ModuleMgr]: Failed to push dasModule!");
256             DestroyModules();
257             return HC_ERR_ALLOC_MEMORY;
258         }
259         g_version.third |= dasModule->moduleType;
260     }
261     const AuthModuleBase *accountModule = GetAccountModule();
262     if (accountModule != NULL) {
263         res = accountModule->init();
264         if (res != HC_SUCCESS) {
265             LOGE("[ModuleMgr]: Init account module fail. [Res]: %" LOG_PUB "d", res);
266             DestroyModules();
267             return res;
268         }
269         if (g_authModuleVec.pushBack(&g_authModuleVec, &accountModule) == NULL) {
270             LOGE("[ModuleMgr]: Failed to push dasModule!");
271             DestroyModules();
272             return HC_ERR_ALLOC_MEMORY;
273         }
274         g_version.third |= accountModule->moduleType;
275     }
276     LOGI("Init modules success!");
277     return HC_SUCCESS;
278 }
279 
DestroyModules(void)280 void DestroyModules(void)
281 {
282     uint32_t index;
283     AuthModuleBase **module;
284     FOR_EACH_HC_VECTOR(g_authModuleVec, index, module) {
285         (*module)->destroy();
286     }
287     DESTROY_HC_VECTOR(AuthModuleVec, &g_authModuleVec);
288     (void)memset_s(&g_version, sizeof(VersionStruct), 0, sizeof(VersionStruct));
289 }
290 
AddAuthModulePlugin(const AuthModuleBase * plugin)291 int32_t AddAuthModulePlugin(const AuthModuleBase *plugin)
292 {
293     if (plugin == NULL || plugin->init == NULL || plugin->destroy == NULL ||
294         plugin->createTask == NULL || plugin->processTask == NULL || plugin->destroyTask == NULL) {
295         LOGE("The plugin is invalid.");
296         return HC_ERR_INVALID_PARAMS;
297     }
298     int32_t res = plugin->init();
299     if (res != HC_SUCCESS) {
300         LOGE("[ModuleMgr]: Init module plugin fail. [Res]: %" LOG_PUB "d", res);
301         return HC_ERR_INIT_FAILED;
302     }
303     bool isNeedReplace = false;
304     uint32_t index;
305     AuthModuleBase **pluginPtr;
306     FOR_EACH_HC_VECTOR(g_authModuleVec, index, pluginPtr) {
307         if ((*pluginPtr)->moduleType == plugin->moduleType) {
308             isNeedReplace = true;
309             break;
310         }
311     }
312     if (g_authModuleVec.pushBack(&g_authModuleVec, &plugin) == NULL) {
313         LOGE("[ModuleMgr]: Push module plugin to vector fail.");
314         plugin->destroy();
315         return HC_ERR_ALLOC_MEMORY;
316     }
317     if (isNeedReplace) {
318         LOGI("[ModuleMgr]: Replace module plugin. [Name]: %" LOG_PUB "d", plugin->moduleType);
319         HC_VECTOR_POPELEMENT(&g_authModuleVec, pluginPtr, index);
320     } else {
321         LOGI("[ModuleMgr]: Add new module plugin. [Name]: %" LOG_PUB "d", plugin->moduleType);
322     }
323     return HC_SUCCESS;
324 }
325 
DelAuthModulePlugin(int32_t moduleType)326 void DelAuthModulePlugin(int32_t moduleType)
327 {
328     uint32_t index;
329     AuthModuleBase **pluginPtr;
330     FOR_EACH_HC_VECTOR(g_authModuleVec, index, pluginPtr) {
331         if ((*pluginPtr)->moduleType == moduleType) {
332             LOGI("[ModuleMgr]: Delete module plugin success. [Name]: %" LOG_PUB "d", moduleType);
333             (*pluginPtr)->destroy();
334             HC_VECTOR_POPELEMENT(&g_authModuleVec, pluginPtr, index);
335             break;
336         }
337     }
338 }
339