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