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