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