• 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 
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(&params);
90     if (res != HC_SUCCESS) {
91         LOGE("Register local identity failed, res: %" LOG_PUB "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(&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     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(&params);
146     if (res != HC_SUCCESS) {
147         LOGE("Delete peer authInfo failed, res: %" LOG_PUB "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(&params, returnPk);
175     if (res != HC_SUCCESS) {
176         LOGE("Get public key failed, res: %" LOG_PUB "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: %" LOG_PUB "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: %" LOG_PUB "d, moduleType: %" LOG_PUB "d, res: %" LOG_PUB "d", *taskId,
211             moduleType, res);
212         return res;
213     }
214     LOGI("Create task success, taskId: %" LOG_PUB "d, moduleType: %" LOG_PUB "d", *taskId, moduleType);
215     return HC_SUCCESS;
216 }
217 
ProcessTask(int taskId,const CJson * in,CJson * out,int32_t * status,int moduleType)218 int32_t ProcessTask(int taskId, const CJson *in, CJson *out, int32_t *status, int moduleType)
219 {
220     if (in == NULL || out == NULL || status == NULL) {
221         LOGE("Params is null.");
222         return HC_ERR_NULL_PTR;
223     }
224     AuthModuleBase *module = GetModule(moduleType);
225     if (module == NULL) {
226         LOGE("Failed to get module!");
227         return HC_ERR_MODULE_NOT_FOUNT;
228     }
229     int32_t res = module->processTask(taskId, in, out, status);
230     if (res != HC_SUCCESS) {
231         LOGE("Process task failed, taskId: %" LOG_PUB "d, moduleType: %" LOG_PUB "d, res: %" LOG_PUB "d.",
232             taskId, moduleType, res);
233         return res;
234     }
235     res = AddSingleVersionToJson(out, &g_version);
236     if (res != HC_SUCCESS) {
237         LOGE("AddSingleVersionToJson failed, res: %" LOG_PUB "x.", res);
238         return res;
239     }
240     LOGI("Process task success, taskId: %" LOG_PUB "d, moduleType: %" LOG_PUB "d.", taskId, moduleType);
241     return res;
242 }
243 
DestroyTask(int taskId,int moduleType)244 void DestroyTask(int taskId, int moduleType)
245 {
246     AuthModuleBase *module = GetModule(moduleType);
247     if (module == NULL) {
248         return;
249     }
250     module->destroyTask(taskId);
251 }
252 
InitModules(void)253 int32_t InitModules(void)
254 {
255     g_authModuleVec = CREATE_HC_VECTOR(AuthModuleVec);
256     InitGroupAndModuleVersion(&g_version);
257     int32_t res;
258     const AuthModuleBase *dasModule = GetDasModule();
259     if (dasModule != NULL) {
260         res = dasModule->init();
261         if (res != HC_SUCCESS) {
262             LOGE("[ModuleMgr]: Init das module fail. [Res]: %" LOG_PUB "d", res);
263             DestroyModules();
264             return res;
265         }
266         if (g_authModuleVec.pushBack(&g_authModuleVec, &dasModule) == NULL) {
267             LOGE("[ModuleMgr]: Failed to push dasModule!");
268             DestroyModules();
269             return HC_ERR_ALLOC_MEMORY;
270         }
271         g_version.third |= dasModule->moduleType;
272     }
273     const AuthModuleBase *accountModule = GetAccountModule();
274     if (accountModule != NULL) {
275         res = accountModule->init();
276         if (res != HC_SUCCESS) {
277             LOGE("[ModuleMgr]: Init account module fail. [Res]: %" LOG_PUB "d", res);
278             DestroyModules();
279             return res;
280         }
281         if (g_authModuleVec.pushBack(&g_authModuleVec, &accountModule) == NULL) {
282             LOGE("[ModuleMgr]: Failed to push dasModule!");
283             DestroyModules();
284             return HC_ERR_ALLOC_MEMORY;
285         }
286         g_version.third |= accountModule->moduleType;
287     }
288     LOGI("Init modules success!");
289     return HC_SUCCESS;
290 }
291 
DestroyModules(void)292 void DestroyModules(void)
293 {
294     uint32_t index;
295     AuthModuleBase **module;
296     FOR_EACH_HC_VECTOR(g_authModuleVec, index, module) {
297         (*module)->destroy();
298     }
299     DESTROY_HC_VECTOR(AuthModuleVec, &g_authModuleVec);
300     (void)memset_s(&g_version, sizeof(VersionStruct), 0, sizeof(VersionStruct));
301 }
302 
AddAuthModulePlugin(const AuthModuleBase * plugin)303 int32_t AddAuthModulePlugin(const AuthModuleBase *plugin)
304 {
305     if (plugin == NULL || plugin->init == NULL || plugin->destroy == NULL ||
306         plugin->createTask == NULL || plugin->processTask == NULL || plugin->destroyTask == NULL) {
307         LOGE("The plugin is invalid.");
308         return HC_ERR_INVALID_PARAMS;
309     }
310     int32_t res = plugin->init();
311     if (res != HC_SUCCESS) {
312         LOGE("[ModuleMgr]: Init module plugin fail. [Res]: %" LOG_PUB "d", res);
313         return HC_ERR_INIT_FAILED;
314     }
315     bool isNeedReplace = false;
316     uint32_t index;
317     AuthModuleBase **pluginPtr;
318     FOR_EACH_HC_VECTOR(g_authModuleVec, index, pluginPtr) {
319         if ((*pluginPtr)->moduleType == plugin->moduleType) {
320             isNeedReplace = true;
321             break;
322         }
323     }
324     if (g_authModuleVec.pushBack(&g_authModuleVec, &plugin) == NULL) {
325         LOGE("[ModuleMgr]: Push module plugin to vector fail.");
326         plugin->destroy();
327         return HC_ERR_ALLOC_MEMORY;
328     }
329     if (isNeedReplace) {
330         LOGI("[ModuleMgr]: Replace module plugin. [Name]: %" LOG_PUB "d", plugin->moduleType);
331         HC_VECTOR_POPELEMENT(&g_authModuleVec, pluginPtr, index);
332     } else {
333         LOGI("[ModuleMgr]: Add new module plugin. [Name]: %" LOG_PUB "d", plugin->moduleType);
334     }
335     return HC_SUCCESS;
336 }
337 
DelAuthModulePlugin(int32_t moduleType)338 void DelAuthModulePlugin(int32_t moduleType)
339 {
340     uint32_t index;
341     AuthModuleBase **pluginPtr;
342     FOR_EACH_HC_VECTOR(g_authModuleVec, index, pluginPtr) {
343         if ((*pluginPtr)->moduleType == moduleType) {
344             LOGI("[ModuleMgr]: Delete module plugin success. [Name]: %" LOG_PUB "d", moduleType);
345             (*pluginPtr)->destroy();
346             HC_VECTOR_POPELEMENT(&g_authModuleVec, pluginPtr, index);
347             break;
348         }
349     }
350 }
351