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 "device_auth.h"
17
18 #include "account_auth_plugin_proxy.h"
19 #include "alg_loader.h"
20 #include "callback_manager.h"
21 #include "channel_manager.h"
22 #include "common_defs.h"
23 #include "cred_manager.h"
24 #include "data_manager.h"
25 #include "dev_auth_module_manager.h"
26 #include "dev_session_mgr.h"
27 #include "group_manager.h"
28 #include "group_operation_common.h"
29 #include "hc_dev_info.h"
30 #include "hc_init_protection.h"
31 #include "hc_log.h"
32 #include "hc_time.h"
33 #include "hisysevent_adapter.h"
34 #include "hitrace_adapter.h"
35 #include "json_utils.h"
36 #include "key_manager.h"
37 #include "os_account_adapter.h"
38 #include "plugin_adapter.h"
39 #include "pseudonym_manager.h"
40 #include "task_manager.h"
41 #include "performance_dumper.h"
42 #include "identity_manager.h"
43 #include "group_auth_manager.h"
44
45 static GroupAuthManager *g_groupAuthManager = NULL;
46 static DeviceGroupManager *g_groupManagerInstance = NULL;
47
48 typedef struct {
49 HcTaskBase base;
50 int64_t sessionId;
51 } StartSessionTask;
52
53 typedef struct {
54 HcTaskBase base;
55 int64_t sessionId;
56 CJson *receivedMsg;
57 } ProcSessionTask;
58
59 typedef struct {
60 HcTaskBase base;
61 int64_t requestId;
62 } SoftBusTask;
63
IsDeviceIdHashMatch(const char * udid,const char * subUdidHash)64 static int32_t IsDeviceIdHashMatch(const char *udid, const char *subUdidHash)
65 {
66 Uint8Buff udidBuf = { (uint8_t *)udid, (uint32_t)HcStrlen(udid) };
67 uint8_t udidHashByte[SHA256_LEN] = { 0 };
68 Uint8Buff udidHashBuf = { udidHashByte, sizeof(udidHashByte) };
69 int32_t ret = GetLoaderInstance()->sha256(&udidBuf, &udidHashBuf);
70 if (ret != HC_SUCCESS) {
71 LOGE("sha256 failed, ret:%d", ret);
72 return ret;
73 }
74 uint32_t udidHashLen = SHA256_LEN * BYTE_TO_HEX_OPER_LENGTH + 1;
75 char *udidHash = (char *)HcMalloc(udidHashLen, 0);
76 if (udidHash == NULL) {
77 LOGE("malloc udidHash string failed");
78 return HC_ERR_ALLOC_MEMORY;
79 }
80 ret = ByteToHexString(udidHashByte, SHA256_LEN, udidHash, udidHashLen);
81 if (ret != HC_SUCCESS) {
82 LOGE("Byte to hexString failed, ret:%d", ret);
83 HcFree(udidHash);
84 return ret;
85 }
86 char *subUdidHashUpper = NULL;
87 ret = ToUpperCase(subUdidHash, &subUdidHashUpper);
88 if (ret != HC_SUCCESS) {
89 LOGE("Failed to convert the input sub udid hash to upper case!");
90 HcFree(udidHash);
91 return ret;
92 }
93 if (strstr((const char *)udidHash, subUdidHashUpper) != NULL) {
94 LOGI("udid hash is match!");
95 HcFree(udidHash);
96 HcFree(subUdidHashUpper);
97 return HC_SUCCESS;
98 }
99 HcFree(udidHash);
100 HcFree(subUdidHashUpper);
101 return HC_ERROR;
102 }
103
GetUdidByGroup(int32_t osAccountId,const char * groupId,const char * deviceIdHash)104 static const char *GetUdidByGroup(int32_t osAccountId, const char *groupId, const char *deviceIdHash)
105 {
106 uint32_t index;
107 TrustedDeviceEntry **deviceEntry = NULL;
108 DeviceEntryVec deviceEntryVec = CREATE_HC_VECTOR(DeviceEntryVec);
109 QueryDeviceParams params = InitQueryDeviceParams();
110 params.groupId = groupId;
111 if (QueryDevices(osAccountId, ¶ms, &deviceEntryVec) != HC_SUCCESS) {
112 LOGE("query trusted devices failed!");
113 ClearDeviceEntryVec(&deviceEntryVec);
114 return NULL;
115 }
116 FOR_EACH_HC_VECTOR(deviceEntryVec, index, deviceEntry) {
117 const char *udid = StringGet(&(*deviceEntry)->udid);
118 if (IsDeviceIdHashMatch(udid, deviceIdHash) == HC_SUCCESS) {
119 ClearDeviceEntryVec(&deviceEntryVec);
120 return udid;
121 }
122 continue;
123 }
124 ClearDeviceEntryVec(&deviceEntryVec);
125 return NULL;
126 }
127
GetDeviceIdByUdidHash(int32_t osAccountId,const char * deviceIdHash)128 static const char *GetDeviceIdByUdidHash(int32_t osAccountId, const char *deviceIdHash)
129 {
130 if (deviceIdHash == NULL) {
131 LOGE("deviceIdHash is null");
132 return NULL;
133 }
134 QueryGroupParams queryParams = InitQueryGroupParams();
135 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
136 int32_t ret = QueryGroups(osAccountId, &queryParams, &groupEntryVec);
137 if (ret != HC_SUCCESS) {
138 LOGE("Failed to query groups!");
139 ClearGroupEntryVec(&groupEntryVec);
140 return NULL;
141 }
142 uint32_t index;
143 TrustedGroupEntry **ptr = NULL;
144 FOR_EACH_HC_VECTOR(groupEntryVec, index, ptr) {
145 const TrustedGroupEntry *groupEntry = (const TrustedGroupEntry *)(*ptr);
146 const char *groupId = StringGet(&(groupEntry->id));
147 if (groupId == NULL) {
148 continue;
149 }
150 const char *udid = GetUdidByGroup(osAccountId, groupId, deviceIdHash);
151 if (udid != NULL) {
152 ClearGroupEntryVec(&groupEntryVec);
153 return udid;
154 }
155 }
156 ClearGroupEntryVec(&groupEntryVec);
157 return NULL;
158 }
159
GetPeerUdidFromJson(int32_t osAccountId,const CJson * in)160 static const char *GetPeerUdidFromJson(int32_t osAccountId, const CJson *in)
161 {
162 const char *peerConnDeviceId = GetStringFromJson(in, FIELD_PEER_CONN_DEVICE_ID);
163 if (peerConnDeviceId == NULL) {
164 LOGI("get peerConnDeviceId from json fail.");
165 return NULL;
166 }
167 bool isUdidHash = false;
168 (void)GetBoolFromJson(in, FIELD_IS_UDID_HASH, &isUdidHash);
169 if (isUdidHash) {
170 const char *deviceId = GetDeviceIdByUdidHash(osAccountId, peerConnDeviceId);
171 return (deviceId == NULL ? peerConnDeviceId : deviceId);
172 }
173 return peerConnDeviceId;
174 }
175
AddGroupInfoToContextByInput(const CJson * receivedMsg,CJson * context)176 static int32_t AddGroupInfoToContextByInput(const CJson *receivedMsg, CJson *context)
177 {
178 const char *groupId = GetStringFromJson(receivedMsg, FIELD_GROUP_ID);
179 if (groupId == NULL) {
180 LOGE("get groupId from json fail.");
181 return HC_ERR_JSON_GET;
182 }
183 const char *groupName = GetStringFromJson(receivedMsg, FIELD_GROUP_NAME);
184 if (groupName == NULL) {
185 LOGE("Failed to get groupName from jsonParams!");
186 return HC_ERR_JSON_GET;
187 }
188 if (AddStringToJson(context, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
189 LOGE("Failed to add groupId to json!");
190 return HC_ERR_JSON_FAIL;
191 }
192 if (AddIntToJson(context, FIELD_GROUP_TYPE, PEER_TO_PEER_GROUP) != HC_SUCCESS) {
193 LOGE("Failed to add groupType to json!");
194 return HC_ERR_JSON_FAIL;
195 }
196 if (AddStringToJson(context, FIELD_GROUP_NAME, groupName) != HC_SUCCESS) {
197 LOGE("Failed to add groupName to json!");
198 return HC_ERR_JSON_FAIL;
199 }
200 return HC_SUCCESS;
201 }
202
AddDevInfoToContextByInput(CJson * context)203 static int32_t AddDevInfoToContextByInput(CJson *context)
204 {
205 int32_t userType = DEVICE_TYPE_ACCESSORY;
206 (void)GetIntFromJson(context, FIELD_USER_TYPE, &userType);
207 const char *authId = GetStringFromJson(context, FIELD_DEVICE_ID);
208 char udid[INPUT_UDID_LEN] = { 0 };
209 if (authId == NULL) {
210 LOGD("No authId is found. The default value is udid!");
211 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
212 if (res != HC_SUCCESS) {
213 LOGE("Failed to get local udid! res: %d", res);
214 return HC_ERR_DB;
215 }
216 authId = udid;
217 }
218 if (AddStringToJson(context, FIELD_AUTH_ID, authId) != HC_SUCCESS) {
219 LOGE("Failed to add authId to params!");
220 return HC_ERR_JSON_FAIL;
221 }
222 if (AddIntToJson(context, FIELD_USER_TYPE, userType) != HC_SUCCESS) {
223 LOGE("Failed to add userType to params!");
224 return HC_ERR_JSON_FAIL;
225 }
226 return HC_SUCCESS;
227 }
228
AddGroupInfoToContextByDb(const char * groupId,CJson * context)229 static int32_t AddGroupInfoToContextByDb(const char *groupId, CJson *context)
230 {
231 int32_t osAccountId;
232 if (GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId) != HC_SUCCESS) {
233 LOGE("get osAccountId from json fail.");
234 return HC_ERR_JSON_GET;
235 }
236 TrustedGroupEntry *entry = GetGroupEntryById(osAccountId, groupId);
237 if (entry == NULL) {
238 LOGE("Failed to get groupEntry from db!");
239 return HC_ERR_DB;
240 }
241 if (AddStringToJson(context, FIELD_GROUP_ID, StringGet(&entry->id)) != HC_SUCCESS) {
242 LOGE("Failed to add groupId to json!");
243 DestroyGroupEntry(entry);
244 return HC_ERR_JSON_FAIL;
245 }
246 if (AddIntToJson(context, FIELD_GROUP_TYPE, entry->type) != HC_SUCCESS) {
247 LOGE("Failed to add groupType to json!");
248 DestroyGroupEntry(entry);
249 return HC_ERR_JSON_FAIL;
250 }
251 if (AddStringToJson(context, FIELD_GROUP_NAME, StringGet(&entry->name)) != HC_SUCCESS) {
252 LOGE("Failed to add groupName to json!");
253 DestroyGroupEntry(entry);
254 return HC_ERR_JSON_FAIL;
255 }
256 DestroyGroupEntry(entry);
257 return HC_SUCCESS;
258 }
259
AddDevInfoToContextByDb(const char * groupId,CJson * context)260 static int32_t AddDevInfoToContextByDb(const char *groupId, CJson *context)
261 {
262 int32_t osAccountId;
263 if (GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId) != HC_SUCCESS) {
264 LOGE("get osAccountId from json fail.");
265 return HC_ERR_JSON_GET;
266 }
267 char udid[INPUT_UDID_LEN] = { 0 };
268 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
269 if (res != HC_SUCCESS) {
270 LOGE("Failed to get local udid! res: %d", res);
271 return HC_ERR_DB;
272 }
273 TrustedDeviceEntry *devAuthParams = CreateDeviceEntry();
274 if (devAuthParams == NULL) {
275 LOGE("Failed to allocate devEntry memory!");
276 return HC_ERR_ALLOC_MEMORY;
277 }
278 if (GetTrustedDevInfoById(osAccountId, udid, true, groupId, devAuthParams) != HC_SUCCESS) {
279 LOGE("Failed to obtain the local device information from the database!");
280 DestroyDeviceEntry(devAuthParams);
281 return HC_ERR_DB;
282 }
283 if (AddStringToJson(context, FIELD_AUTH_ID, StringGet(&devAuthParams->authId)) != HC_SUCCESS) {
284 LOGE("Failed to add authId to params!");
285 DestroyDeviceEntry(devAuthParams);
286 return HC_ERR_JSON_FAIL;
287 }
288 if (AddIntToJson(context, FIELD_USER_TYPE, devAuthParams->devType) != HC_SUCCESS) {
289 LOGE("Failed to add userType to params!");
290 DestroyDeviceEntry(devAuthParams);
291 return HC_ERR_JSON_FAIL;
292 }
293 DestroyDeviceEntry(devAuthParams);
294 return HC_SUCCESS;
295 }
296
GetOpCodeFromContext(const CJson * context)297 static int32_t GetOpCodeFromContext(const CJson *context)
298 {
299 bool isAdmin = true;
300 (void)GetBoolFromJson(context, FIELD_IS_ADMIN, &isAdmin);
301 return isAdmin ? MEMBER_INVITE : MEMBER_JOIN;
302 }
303
AddClientReqInfoToContext(int32_t osAccountId,int64_t requestId,const char * appId,CJson * context)304 static int32_t AddClientReqInfoToContext(int32_t osAccountId, int64_t requestId, const char *appId, CJson *context)
305 {
306 const char *groupId = GetStringFromJson(context, FIELD_GROUP_ID);
307 if (groupId == NULL) {
308 LOGE("get groupId from json fail.");
309 return HC_ERR_JSON_GET;
310 }
311 if (AddBoolToJson(context, FIELD_IS_BIND, true) != HC_SUCCESS) {
312 LOGE("add isBind to context fail.");
313 return HC_ERR_JSON_ADD;
314 }
315 if (AddBoolToJson(context, FIELD_IS_CLIENT, true) != HC_SUCCESS) {
316 LOGE("add isClient to context fail.");
317 return HC_ERR_JSON_ADD;
318 }
319 if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
320 LOGE("add osAccountId to context fail.");
321 return HC_ERR_JSON_ADD;
322 }
323 if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
324 LOGE("add requestId to context fail.");
325 return HC_ERR_JSON_ADD;
326 }
327 if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
328 LOGE("add appId to context fail.");
329 return HC_ERR_JSON_ADD;
330 }
331 int32_t opCode = GetOpCodeFromContext(context);
332 if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
333 LOGE("add operationCode to context fail.");
334 return HC_ERR_JSON_ADD;
335 }
336 if (opCode == MEMBER_JOIN) {
337 return AddDevInfoToContextByInput(context);
338 }
339 int32_t res = AddDevInfoToContextByDb(groupId, context);
340 if (res != HC_SUCCESS) {
341 return res;
342 }
343 return AddGroupInfoToContextByDb(groupId, context);
344 }
345
AddChannelInfoToContext(int32_t channelType,int64_t channelId,CJson * context)346 static int32_t AddChannelInfoToContext(int32_t channelType, int64_t channelId, CJson *context)
347 {
348 if (AddIntToJson(context, FIELD_CHANNEL_TYPE, channelType) != HC_SUCCESS) {
349 LOGE("add channelType to context fail.");
350 return HC_ERR_JSON_ADD;
351 }
352 if (AddByteToJson(context, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) {
353 LOGE("add channelId to context fail.");
354 return HC_ERR_JSON_ADD;
355 }
356 return HC_SUCCESS;
357 }
358
BuildClientBindContext(int32_t osAccountId,int64_t requestId,const char * appId,const DeviceAuthCallback * callback,CJson * context)359 static int32_t BuildClientBindContext(int32_t osAccountId, int64_t requestId, const char *appId,
360 const DeviceAuthCallback *callback, CJson *context)
361 {
362 int32_t res = AddClientReqInfoToContext(osAccountId, requestId, appId, context);
363 if (res != HC_SUCCESS) {
364 return res;
365 }
366 ChannelType channelType = GetChannelType(callback, context);
367 int64_t channelId;
368 res = OpenChannel(channelType, context, requestId, &channelId);
369 if (res != HC_SUCCESS) {
370 LOGE("open channel fail.");
371 return res;
372 }
373 return AddChannelInfoToContext(channelType, channelId, context);
374 }
375
DoStartSession(HcTaskBase * task)376 static void DoStartSession(HcTaskBase *task)
377 {
378 LOGI("start session task begin.");
379 if (task == NULL) {
380 LOGE("The input task is NULL, can't start session!");
381 return;
382 }
383 StartSessionTask *realTask = (StartSessionTask *)task;
384 SET_LOG_MODE(TRACE_MODE);
385 SET_TRACE_ID(realTask->sessionId);
386 int32_t res = StartDevSession(realTask->sessionId);
387 if (res != HC_SUCCESS) {
388 LOGE("start session fail.[Res]: %d", res);
389 CloseDevSession(realTask->sessionId);
390 }
391 }
392
DoProcSession(HcTaskBase * task)393 static void DoProcSession(HcTaskBase *task)
394 {
395 LOGI("proc session task begin.");
396 if (task == NULL) {
397 LOGE("The input task is NULL, can't start session!");
398 return;
399 }
400 ProcSessionTask *realTask = (ProcSessionTask *)task;
401 SET_LOG_MODE(TRACE_MODE);
402 SET_TRACE_ID(realTask->sessionId);
403 bool isFinish = false;
404 int32_t res = ProcessDevSession(realTask->sessionId, realTask->receivedMsg, &isFinish);
405 if (res != HC_SUCCESS) {
406 LOGE("ProcessDevSession fail. [Res]: %d", res);
407 CloseDevSession(realTask->sessionId);
408 return;
409 }
410 LOGI("ProcessDevSession success. [State]: %s", isFinish ? "FINISH" : "CONTINUE");
411 if (isFinish) {
412 CloseDevSession(realTask->sessionId);
413 }
414 }
415
InitStartSessionTask(StartSessionTask * task,int64_t sessionId)416 static void InitStartSessionTask(StartSessionTask *task, int64_t sessionId)
417 {
418 task->base.doAction = DoStartSession;
419 task->base.destroy = NULL;
420 task->sessionId = sessionId;
421 }
422
DestroyProcSessionTask(HcTaskBase * task)423 static void DestroyProcSessionTask(HcTaskBase *task)
424 {
425 ProcSessionTask *realTask = (ProcSessionTask *)task;
426 FreeJson(realTask->receivedMsg);
427 }
428
InitProcSessionTask(ProcSessionTask * task,int64_t sessionId,CJson * receivedMsg)429 static void InitProcSessionTask(ProcSessionTask *task, int64_t sessionId, CJson *receivedMsg)
430 {
431 task->base.doAction = DoProcSession;
432 task->base.destroy = DestroyProcSessionTask;
433 task->sessionId = sessionId;
434 task->receivedMsg = receivedMsg;
435 }
436
PushStartSessionTask(int64_t sessionId)437 static int32_t PushStartSessionTask(int64_t sessionId)
438 {
439 StartSessionTask *task = (StartSessionTask *)HcMalloc(sizeof(StartSessionTask), 0);
440 if (task == NULL) {
441 LOGE("Failed to allocate memory for task!");
442 return HC_ERR_ALLOC_MEMORY;
443 }
444 InitStartSessionTask(task, sessionId);
445 if (PushTask((HcTaskBase*)task) != HC_SUCCESS) {
446 LOGE("push start session task fail.");
447 HcFree(task);
448 return HC_ERR_INIT_TASK_FAIL;
449 }
450 LOGI("push start session task success.");
451 return HC_SUCCESS;
452 }
453
AddOriginDataForPlugin(CJson * receivedMsg,const uint8_t * data)454 static int32_t AddOriginDataForPlugin(CJson *receivedMsg, const uint8_t *data)
455 {
456 if ((receivedMsg == NULL) || (data == NULL)) {
457 LOGE("Invalid params");
458 return HC_ERR_INVALID_PARAMS;
459 }
460 return AddStringToJson(receivedMsg, FIELD_PLUGIN_EXT_DATA, (const char *)data);
461 }
462
PushProcSessionTask(int64_t sessionId,CJson * receivedMsg)463 static int32_t PushProcSessionTask(int64_t sessionId, CJson *receivedMsg)
464 {
465 ProcSessionTask *task = (ProcSessionTask *)HcMalloc(sizeof(ProcSessionTask), 0);
466 if (task == NULL) {
467 LOGE("Failed to allocate memory for task!");
468 return HC_ERR_ALLOC_MEMORY;
469 }
470 InitProcSessionTask(task, sessionId, receivedMsg);
471 if (PushTask((HcTaskBase*)task) != HC_SUCCESS) {
472 LOGE("push start session task fail.");
473 HcFree(task);
474 return HC_ERR_INIT_TASK_FAIL;
475 }
476 LOGI("push start session task success.");
477 return HC_SUCCESS;
478 }
479
480 #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
481 // If bind with iso short pin, groupVisibility must be private
CheckGroupVisibility(const CJson * context)482 static int32_t CheckGroupVisibility(const CJson *context)
483 {
484 int32_t osAccountId = INVALID_OS_ACCOUNT;
485 if (GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId) != HC_SUCCESS) {
486 LOGE("Failed to get osAccountId!");
487 return HC_ERR_JSON_GET;
488 }
489 const char *groupId = GetStringFromJson(context, FIELD_GROUP_ID);
490 if (groupId == NULL) {
491 LOGE("Failed to get groupId!");
492 return HC_ERR_JSON_GET;
493 }
494 const char *appId = GetStringFromJson(context, FIELD_APP_ID);
495 if (appId == NULL) {
496 LOGE("Failed to get appId!");
497 return HC_ERR_JSON_GET;
498 }
499 TrustedGroupEntry *entry = GetGroupEntryById(osAccountId, groupId);
500 if (entry == NULL) {
501 LOGE("Failed to get group entry!");
502 return HC_ERR_GROUP_NOT_EXIST;
503 }
504 int32_t res = CheckUpgradeIdentity(entry->upgradeFlag, appId, NULL);
505 if (res == HC_SUCCESS) {
506 LOGI("Group is from upgrade, no need to check visibility.");
507 DestroyGroupEntry(entry);
508 return HC_SUCCESS;
509 }
510 if (entry->visibility != GROUP_VISIBILITY_PRIVATE) {
511 LOGE("Group is not private, can not bind old version wearable device!");
512 DestroyGroupEntry(entry);
513 return HC_ERR_INVALID_PARAMS;
514 }
515 DestroyGroupEntry(entry);
516 return HC_SUCCESS;
517 }
518 #endif
519
520 #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
CheckBindParams(const CJson * context,bool isClient)521 static int32_t CheckBindParams(const CJson *context, bool isClient)
522 {
523 int32_t opCode;
524 if (GetIntFromJson(context, FIELD_OPERATION_CODE, &opCode) != HC_SUCCESS) {
525 LOGE("Failed to get operation code!");
526 return HC_ERR_JSON_GET;
527 }
528 if ((isClient && opCode == MEMBER_INVITE) || (!isClient && opCode == MEMBER_JOIN)) {
529 int32_t protocolExpandVal = INVALID_PROTOCOL_EXPAND_VALUE;
530 (void)GetIntFromJson(context, FIELD_PROTOCOL_EXPAND, &protocolExpandVal);
531 if (protocolExpandVal == LITE_PROTOCOL_COMPATIBILITY_MODE) {
532 return CheckGroupVisibility(context);
533 }
534 }
535 return HC_SUCCESS;
536 }
537 #endif
538
StartClientBindSession(int32_t osAccountId,int64_t requestId,const char * appId,const char * contextParams,const DeviceAuthCallback * callback)539 static int32_t StartClientBindSession(int32_t osAccountId, int64_t requestId, const char *appId,
540 const char *contextParams, const DeviceAuthCallback *callback)
541 {
542 CJson *context = CreateJsonFromString(contextParams);
543 if (context == NULL) {
544 LOGE("Failed to create json from string!");
545 return HC_ERR_JSON_FAIL;
546 }
547 int32_t res = BuildClientBindContext(osAccountId, requestId, appId, callback, context);
548 if (res != HC_SUCCESS) {
549 FreeJson(context);
550 return res;
551 }
552 #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
553 res = CheckBindParams(context, true);
554 if (res != HC_SUCCESS) {
555 FreeJson(context);
556 return res;
557 }
558 #endif
559 ChannelType channelType = GetChannelType(callback, context);
560 SessionInitParams params = { context, *callback };
561 res = OpenDevSession(requestId, appId, ¶ms);
562 FreeJson(context);
563 if (res != HC_SUCCESS) {
564 LOGE("OpenDevSession fail. [Res]: %d", res);
565 return res;
566 }
567 if (channelType == SERVICE_CHANNEL) {
568 res = PushStartSessionTask(requestId);
569 if (res != HC_SUCCESS) {
570 return res;
571 }
572 }
573 return HC_SUCCESS;
574 }
575
576 #ifdef DEV_AUTH_HIVIEW_ENABLE
GetAddMemberCallEventFuncName(const char * addParams)577 static const char *GetAddMemberCallEventFuncName(const char *addParams)
578 {
579 if (addParams == NULL) {
580 LOGE("add params is null!");
581 return ADD_MEMBER_EVENT;
582 }
583 CJson *in = CreateJsonFromString(addParams);
584 if (in == NULL) {
585 LOGE("Failed to create json param!");
586 return ADD_MEMBER_EVENT;
587 }
588 int32_t protocolExpandVal = INVALID_PROTOCOL_EXPAND_VALUE;
589 (void)GetIntFromJson(in, FIELD_PROTOCOL_EXPAND, &protocolExpandVal);
590 FreeJson(in);
591 if (protocolExpandVal == LITE_PROTOCOL_STANDARD_MODE) {
592 return ADD_MEMBER_WITH_LITE_STANDARD;
593 } else if (protocolExpandVal == LITE_PROTOCOL_COMPATIBILITY_MODE) {
594 return ADD_MEMBER_WITH_LITE_COMPATIBILITY;
595 } else {
596 return ADD_MEMBER_EVENT;
597 }
598 }
599 #endif
600
AddMemberToGroupInner(int32_t osAccountId,int64_t requestId,const char * appId,const char * addParams)601 static int32_t AddMemberToGroupInner(int32_t osAccountId, int64_t requestId, const char *appId, const char *addParams)
602 {
603 SET_LOG_MODE(TRACE_MODE);
604 SET_TRACE_ID(requestId);
605 ADD_PERFORM_DATA(requestId, true, true, HcGetCurTimeInMillis());
606 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
607 if ((appId == NULL) || (addParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
608 LOGE("Invalid input parameters!");
609 return HC_ERR_INVALID_PARAMS;
610 }
611 if (!IsOsAccountUnlocked(osAccountId)) {
612 LOGE("Os account is not unlocked!");
613 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
614 }
615 LOGI("Start to add member to group. [ReqId]: %" PRId64 ", [OsAccountId]: %d, [AppId]: %s",
616 requestId, osAccountId, appId);
617 const DeviceAuthCallback *callback = GetGMCallbackByAppId(appId);
618 if (callback == NULL) {
619 LOGE("Failed to find callback by appId! [AppId]: %s", appId);
620 return HC_ERR_CALLBACK_NOT_FOUND;
621 }
622 return StartClientBindSession(osAccountId, requestId, appId, addParams, callback);
623 }
624
AddMemberToGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * addParams)625 static int32_t AddMemberToGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *addParams)
626 {
627 int32_t res = AddMemberToGroupInner(osAccountId, requestId, appId, addParams);
628 #ifdef DEV_AUTH_HIVIEW_ENABLE
629 const char *callEventFuncName = GetAddMemberCallEventFuncName(addParams);
630 DEV_AUTH_REPORT_UE_CALL_EVENT_BY_PARAMS(osAccountId, addParams, appId, callEventFuncName);
631 #endif
632 return res;
633 }
634
AddServerReqInfoToContext(int64_t requestId,const char * appId,int32_t opCode,const CJson * receivedMsg,CJson * context)635 static int32_t AddServerReqInfoToContext(int64_t requestId, const char *appId, int32_t opCode,
636 const CJson *receivedMsg, CJson *context)
637 {
638 const char *groupId = GetStringFromJson(receivedMsg, FIELD_GROUP_ID);
639 if (groupId == NULL) {
640 LOGE("get groupId from json fail.");
641 return HC_ERR_JSON_GET;
642 }
643 if (AddBoolToJson(context, FIELD_IS_SINGLE_CRED, true) != HC_SUCCESS) {
644 LOGE("add isSingleCred to context fail.");
645 return HC_ERR_JSON_ADD;
646 }
647 if (AddBoolToJson(context, FIELD_IS_BIND, true) != HC_SUCCESS) {
648 LOGE("add isBind to context fail.");
649 return HC_ERR_JSON_ADD;
650 }
651 if (AddBoolToJson(context, FIELD_IS_CLIENT, false) != HC_SUCCESS) {
652 LOGE("add isClient to context fail.");
653 return HC_ERR_JSON_ADD;
654 }
655 if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
656 LOGE("add requestId to context fail.");
657 return HC_ERR_JSON_ADD;
658 }
659 if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
660 LOGE("add appId to context fail.");
661 return HC_ERR_JSON_ADD;
662 }
663 if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
664 LOGE("add opCode to context fail.");
665 return HC_ERR_JSON_ADD;
666 }
667 int32_t res;
668 if (opCode == MEMBER_INVITE) {
669 res = AddGroupInfoToContextByInput(receivedMsg, context);
670 if (res != HC_SUCCESS) {
671 return res;
672 }
673 return AddDevInfoToContextByInput(context);
674 }
675 res = AddGroupInfoToContextByDb(groupId, context);
676 if (res != HC_SUCCESS) {
677 return res;
678 }
679 return AddDevInfoToContextByDb(groupId, context);
680 }
681
CheckConfirmationExist(const CJson * context)682 static int32_t CheckConfirmationExist(const CJson *context)
683 {
684 uint32_t confirmation = REQUEST_REJECTED;
685 if (GetUnsignedIntFromJson(context, FIELD_CONFIRMATION, &confirmation) != HC_SUCCESS) {
686 LOGE("Failed to get confimation from json!");
687 return HC_ERR_JSON_GET;
688 }
689 if (confirmation == REQUEST_ACCEPTED) {
690 LOGI("The service accepts this request!");
691 } else {
692 LOGW("The service rejects this request!");
693 }
694 return HC_SUCCESS;
695 }
696
AddOsAccountIdToContextIfValid(CJson * context)697 static int32_t AddOsAccountIdToContextIfValid(CJson *context)
698 {
699 int32_t osAccountId = ANY_OS_ACCOUNT;
700 (void)GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId);
701 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
702 LOGI("[OsAccountId]: %d", osAccountId);
703 if (osAccountId == INVALID_OS_ACCOUNT) {
704 return HC_ERR_INVALID_PARAMS;
705 }
706 if (!IsOsAccountUnlocked(osAccountId)) {
707 LOGE("Os account is not unlocked!");
708 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
709 }
710 if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
711 LOGE("add operationCode to context fail.");
712 return HC_ERR_JSON_ADD;
713 }
714 return HC_SUCCESS;
715 }
716
BuildServerBindContext(int64_t requestId,const char * appId,int32_t opCode,const CJson * receivedMsg,CJson * context)717 static int32_t BuildServerBindContext(int64_t requestId, const char *appId, int32_t opCode,
718 const CJson *receivedMsg, CJson *context)
719 {
720 int32_t res = CheckConfirmationExist(context);
721 if (res != HC_SUCCESS) {
722 return res;
723 }
724 res = AddOsAccountIdToContextIfValid(context);
725 if (res != HC_SUCCESS) {
726 return res;
727 }
728 res = AddServerReqInfoToContext(requestId, appId, opCode, receivedMsg, context);
729 if (res != HC_SUCCESS) {
730 return res;
731 }
732 int32_t channelType;
733 int64_t channelId = DEFAULT_CHANNEL_ID;
734 if (GetByteFromJson(receivedMsg, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) == HC_SUCCESS) {
735 channelType = SOFT_BUS;
736 } else {
737 channelType = SERVICE_CHANNEL;
738 }
739 return AddChannelInfoToContext(channelType, channelId, context);
740 }
741
GetAppIdFromReceivedMsg(const CJson * receivedMsg)742 static const char *GetAppIdFromReceivedMsg(const CJson *receivedMsg)
743 {
744 const char *appId = GetStringFromJson(receivedMsg, FIELD_APP_ID);
745 if (appId == NULL) {
746 LOGW("use default device manager appId.");
747 appId = DM_APP_ID;
748 }
749 return appId;
750 }
751
OpenServerBindSession(int64_t requestId,const CJson * receivedMsg)752 static int32_t OpenServerBindSession(int64_t requestId, const CJson *receivedMsg)
753 {
754 const char *appId = GetAppIdFromReceivedMsg(receivedMsg);
755 const DeviceAuthCallback *callback = GetGMCallbackByAppId(appId);
756 if (callback == NULL) {
757 LOGE("Failed to find callback by appId! [AppId]: %s", appId);
758 return HC_ERR_CALLBACK_NOT_FOUND;
759 }
760 int32_t opCode;
761 if (GetIntFromJson(receivedMsg, FIELD_GROUP_OP, &opCode) != HC_SUCCESS) {
762 if (GetIntFromJson(receivedMsg, FIELD_OP_CODE, &opCode) != HC_SUCCESS) {
763 opCode = MEMBER_JOIN;
764 LOGW("use default opCode.");
765 }
766 }
767 char *returnDataStr = ProcessRequestCallback(requestId, opCode, NULL, callback);
768 if (returnDataStr == NULL) {
769 LOGE("The OnRequest callback is fail!");
770 return HC_ERR_REQ_REJECTED;
771 }
772 CJson *context = CreateJsonFromString(returnDataStr);
773 FreeJsonString(returnDataStr);
774 if (context == NULL) {
775 LOGE("Failed to create context from string!");
776 return HC_ERR_JSON_FAIL;
777 }
778 int32_t res = BuildServerBindContext(requestId, appId, opCode, receivedMsg, context);
779 if (res != HC_SUCCESS) {
780 FreeJson(context);
781 return res;
782 }
783 #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
784 res = CheckBindParams(context, false);
785 if (res != HC_SUCCESS) {
786 FreeJson(context);
787 return res;
788 }
789 #endif
790 SessionInitParams params = { context, *callback };
791 res = OpenDevSession(requestId, appId, ¶ms);
792 FreeJson(context);
793 return res;
794 }
795
ProcessBindData(int64_t requestId,const uint8_t * data,uint32_t dataLen)796 static int32_t ProcessBindData(int64_t requestId, const uint8_t *data, uint32_t dataLen)
797 {
798 SET_LOG_MODE(TRACE_MODE);
799 SET_TRACE_ID(requestId);
800 if (!IsSessionExist(requestId)) {
801 ADD_PERFORM_DATA(requestId, true, false, HcGetCurTimeInMillis());
802 } else {
803 UPDATE_PERFORM_DATA_BY_SELF_INDEX(requestId, HcGetCurTimeInMillis());
804 }
805 if ((data == NULL) || (dataLen == 0) || (dataLen > MAX_DATA_BUFFER_SIZE)) {
806 LOGE("The input data is invalid!");
807 return HC_ERR_INVALID_PARAMS;
808 }
809 LOGI("[Start]: RequestProcessBindData! [ReqId]: %" PRId64, requestId);
810 CJson *receivedMsg = CreateJsonFromString((const char *)data);
811 if (receivedMsg == NULL) {
812 LOGE("Failed to create json from string!");
813 return HC_ERR_JSON_FAIL;
814 }
815 int32_t res;
816 if (!IsSessionExist(requestId)) {
817 res = OpenServerBindSession(requestId, receivedMsg);
818 if (res != HC_SUCCESS) {
819 FreeJson(receivedMsg);
820 return res;
821 }
822 }
823 res = PushProcSessionTask(requestId, receivedMsg);
824 if (res != HC_SUCCESS) {
825 FreeJson(receivedMsg);
826 return res;
827 }
828 return HC_SUCCESS;
829 }
830
BuildClientAuthContext(int32_t osAccountId,int64_t requestId,const char * appId,CJson * context)831 static int32_t BuildClientAuthContext(int32_t osAccountId, int64_t requestId, const char *appId, CJson *context)
832 {
833 const char *peerUdid = GetPeerUdidFromJson(osAccountId, context);
834 if (peerUdid != NULL) {
835 char *deviceId = NULL;
836 if (DeepCopyString(peerUdid, &deviceId) != HC_SUCCESS) {
837 LOGE("Failed to copy peerUdid!");
838 return HC_ERR_ALLOC_MEMORY;
839 }
840 if (AddStringToJson(context, FIELD_PEER_UDID, deviceId) != HC_SUCCESS) {
841 LOGE("add peerUdid to context fail.");
842 HcFree(deviceId);
843 return HC_ERR_JSON_ADD;
844 }
845 if (AddStringToJson(context, FIELD_PEER_CONN_DEVICE_ID, deviceId) != HC_SUCCESS) {
846 LOGE("add peerConnDeviceId to context fail.");
847 HcFree(deviceId);
848 return HC_ERR_JSON_ADD;
849 }
850 PRINT_SENSITIVE_DATA("PeerUdid", deviceId);
851 HcFree(deviceId);
852 }
853 if (AddBoolToJson(context, FIELD_IS_BIND, false) != HC_SUCCESS) {
854 LOGE("add isBind to context fail.");
855 return HC_ERR_JSON_ADD;
856 }
857 if (AddBoolToJson(context, FIELD_IS_CLIENT, true) != HC_SUCCESS) {
858 LOGE("add isClient to context fail.");
859 return HC_ERR_JSON_ADD;
860 }
861 if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
862 LOGE("add osAccountId to context fail.");
863 return HC_ERR_JSON_ADD;
864 }
865 if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
866 LOGE("add requestId to context fail.");
867 return HC_ERR_JSON_ADD;
868 }
869 if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
870 LOGE("add appId to context fail.");
871 return HC_ERR_JSON_ADD;
872 }
873 if (AddIntToJson(context, FIELD_OPERATION_CODE, AUTH_FORM_ACCOUNT_UNRELATED) != HC_SUCCESS) {
874 LOGE("add opCode to context fail.");
875 return HC_ERR_JSON_ADD;
876 }
877 return AddChannelInfoToContext(SERVICE_CHANNEL, DEFAULT_CHANNEL_ID, context);
878 }
879
BuildP2PBindContext(CJson * context)880 static int32_t BuildP2PBindContext(CJson *context)
881 {
882 int32_t acquireType = -1;
883 if (GetIntFromJson(context, FIELD_ACQURIED_TYPE, &acquireType) != HC_SUCCESS) {
884 LOGE("Failed to get acquireType from reqJsonStr!");
885 return HC_ERR_JSON_FAIL;
886 }
887 if ((acquireType == P2P_BIND) && AddBoolToJson(context, FIELD_IS_DIRECT_AUTH, true) != HC_SUCCESS) {
888 LOGE("add isDirectAuth to context fail.");
889 return HC_ERR_JSON_ADD;
890 }
891 if (AddIntToJson(context, FIELD_OPERATION_CODE, acquireType) != HC_SUCCESS) {
892 LOGE("add opCode to context fail.");
893 return HC_ERR_JSON_ADD;
894 }
895 const char *serviceType = GetStringFromJson(context, FIELD_SERVICE_TYPE);
896 if (serviceType == NULL) {
897 if ((acquireType == P2P_BIND) &&
898 AddStringToJson(context, FIELD_SERVICE_TYPE, DEFAULT_SERVICE_TYPE) != HC_SUCCESS) {
899 LOGE("add serviceType to context fail.");
900 return HC_ERR_JSON_ADD;
901 }
902 }
903 return HC_SUCCESS;
904 }
905
AuthDevice(int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * gaCallback)906 static int32_t AuthDevice(int32_t osAccountId, int64_t authReqId, const char *authParams,
907 const DeviceAuthCallback *gaCallback)
908 {
909 SET_LOG_MODE(TRACE_MODE);
910 SET_TRACE_ID(authReqId);
911 ADD_PERFORM_DATA(authReqId, false, true, HcGetCurTimeInMillis());
912 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
913 LOGI("Begin AuthDevice. [ReqId]: %" PRId64 ", [OsAccountId]: %d", authReqId, osAccountId);
914 if ((authParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT) || (gaCallback == NULL)) {
915 LOGE("The input auth params is invalid!");
916 return HC_ERR_INVALID_PARAMS;
917 }
918 if (!IsOsAccountUnlocked(osAccountId)) {
919 LOGE("Os account is not unlocked!");
920 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
921 }
922 CJson *context = CreateJsonFromString(authParams);
923 if (context == NULL) {
924 LOGE("Failed to create json from string!");
925 return HC_ERR_JSON_FAIL;
926 }
927 const char *appId = GetStringFromJson(context, FIELD_SERVICE_PKG_NAME);
928 if (appId == NULL) {
929 LOGE("get servicePkgName from json fail.");
930 FreeJson(context);
931 return HC_ERR_JSON_GET;
932 }
933 int32_t res = BuildClientAuthContext(osAccountId, authReqId, appId, context);
934 if (res != HC_SUCCESS) {
935 FreeJson(context);
936 return res;
937 }
938 DEV_AUTH_REPORT_UE_CALL_EVENT_BY_PARAMS(osAccountId, authParams, appId, AUTH_DEV_EVENT);
939 SessionInitParams params = { context, *gaCallback };
940 res = OpenDevSession(authReqId, appId, ¶ms);
941 FreeJson(context);
942 if (res != HC_SUCCESS) {
943 LOGE("OpenDevSession fail. [Res]: %d", res);
944 return res;
945 }
946 return PushStartSessionTask(authReqId);
947 }
948
AddDeviceIdToJson(CJson * context,const char * peerUdid)949 static int32_t AddDeviceIdToJson(CJson *context, const char *peerUdid)
950 {
951 char *deviceId = NULL;
952 if (DeepCopyString(peerUdid, &deviceId) != HC_SUCCESS) {
953 LOGE("Failed to copy peerUdid!");
954 return HC_ERR_ALLOC_MEMORY;
955 }
956 if (AddStringToJson(context, FIELD_PEER_UDID, deviceId) != HC_SUCCESS) {
957 LOGE("add peerUdid to context fail.");
958 HcFree(deviceId);
959 return HC_ERR_JSON_ADD;
960 }
961 if (AddStringToJson(context, FIELD_PEER_CONN_DEVICE_ID, deviceId) != HC_SUCCESS) {
962 LOGE("add peerConnDeviceId to context fail.");
963 HcFree(deviceId);
964 return HC_ERR_JSON_ADD;
965 }
966 HcFree(deviceId);
967 return HC_SUCCESS;
968 }
969
BuildServerAuthContext(int64_t requestId,int32_t opCode,const char * appId,CJson * context)970 static int32_t BuildServerAuthContext(int64_t requestId, int32_t opCode, const char *appId, CJson *context)
971 {
972 int32_t res = CheckConfirmationExist(context);
973 if (res != HC_SUCCESS) {
974 return res;
975 }
976 res = AddOsAccountIdToContextIfValid(context);
977 if (res != HC_SUCCESS) {
978 return res;
979 }
980 int32_t osAccountId = ANY_OS_ACCOUNT;
981 (void)GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId);
982 const char *peerUdid = GetPeerUdidFromJson(osAccountId, context);
983 if (peerUdid == NULL) {
984 return HC_ERR_JSON_GET;
985 }
986 PRINT_SENSITIVE_DATA("PeerUdid", peerUdid);
987 if (AddDeviceIdToJson(context, peerUdid) != HC_SUCCESS) {
988 LOGE("add deviceId to context fail.");
989 return HC_ERR_JSON_ADD;
990 }
991 if (AddBoolToJson(context, FIELD_IS_BIND, false) != HC_SUCCESS) {
992 LOGE("add isBind to context fail.");
993 return HC_ERR_JSON_ADD;
994 }
995 if (AddBoolToJson(context, FIELD_IS_CLIENT, false) != HC_SUCCESS) {
996 LOGE("add isClient to context fail.");
997 return HC_ERR_JSON_ADD;
998 }
999 if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
1000 LOGE("add requestId to context fail.");
1001 return HC_ERR_JSON_ADD;
1002 }
1003 if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
1004 LOGE("add appId to context fail.");
1005 return HC_ERR_JSON_ADD;
1006 }
1007 if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
1008 LOGE("add opCode to context fail.");
1009 return HC_ERR_JSON_ADD;
1010 }
1011 return AddChannelInfoToContext(SERVICE_CHANNEL, DEFAULT_CHANNEL_ID, context);
1012 }
1013
BuildServerP2PAuthContext(int64_t requestId,int32_t opCode,const char * appId,CJson * context)1014 static int32_t BuildServerP2PAuthContext(int64_t requestId, int32_t opCode, const char *appId, CJson *context)
1015 {
1016 int32_t res = CheckConfirmationExist(context);
1017 if (res != HC_SUCCESS) {
1018 return res;
1019 }
1020 res = AddOsAccountIdToContextIfValid(context);
1021 if (res != HC_SUCCESS) {
1022 return res;
1023 }
1024 const char *peerUdid = GetStringFromJson(context, FIELD_PEER_CONN_DEVICE_ID);
1025 const char *pinCode = GetStringFromJson(context, FIELD_PIN_CODE);
1026 if (peerUdid == NULL && pinCode == NULL) {
1027 LOGE("need peerConnDeviceId or pinCode!");
1028 return HC_ERR_JSON_GET;
1029 }
1030 if (peerUdid != NULL) {
1031 PRINT_SENSITIVE_DATA("PeerUdid", peerUdid);
1032 if (AddDeviceIdToJson(context, peerUdid) != HC_SUCCESS) {
1033 LOGE("add deviceId to context fail.");
1034 return HC_ERR_JSON_ADD;
1035 }
1036 }
1037 if (AddBoolToJson(context, FIELD_IS_SINGLE_CRED, true) != HC_SUCCESS) {
1038 LOGE("add isSingleCred to context fail.");
1039 return HC_ERR_JSON_ADD;
1040 }
1041 if (AddBoolToJson(context, FIELD_IS_BIND, false) != HC_SUCCESS) {
1042 LOGE("add isBind to context fail.");
1043 return HC_ERR_JSON_ADD;
1044 }
1045 if (AddBoolToJson(context, FIELD_IS_CLIENT, false) != HC_SUCCESS) {
1046 LOGE("add isClient to context fail.");
1047 return HC_ERR_JSON_ADD;
1048 }
1049 if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
1050 LOGE("add requestId to context fail.");
1051 return HC_ERR_JSON_ADD;
1052 }
1053 if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
1054 LOGE("add appId to context fail.");
1055 return HC_ERR_JSON_ADD;
1056 }
1057 if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
1058 LOGE("add opCode to context fail.");
1059 return HC_ERR_JSON_ADD;
1060 }
1061 return AddChannelInfoToContext(SERVICE_CHANNEL, DEFAULT_CHANNEL_ID, context);
1062 }
1063
OpenServerAuthSession(int64_t requestId,const CJson * receivedMsg,const DeviceAuthCallback * callback)1064 static int32_t OpenServerAuthSession(int64_t requestId, const CJson *receivedMsg, const DeviceAuthCallback *callback)
1065 {
1066 int32_t opCode = AUTH_FORM_ACCOUNT_UNRELATED;
1067 if (GetIntFromJson(receivedMsg, FIELD_AUTH_FORM, &opCode) != HC_SUCCESS) {
1068 if (GetIntFromJson(receivedMsg, FIELD_OP_CODE, &opCode) != HC_SUCCESS) {
1069 opCode = AUTH_FORM_INVALID_TYPE;
1070 LOGW("use default opCode.");
1071 }
1072 }
1073 char *returnDataStr = ProcessRequestCallback(requestId, opCode, NULL, callback);
1074 if (returnDataStr == NULL) {
1075 LOGE("The OnRequest callback is fail!");
1076 return HC_ERR_REQ_REJECTED;
1077 }
1078 CJson *context = CreateJsonFromString(returnDataStr);
1079 FreeJsonString(returnDataStr);
1080 if (context == NULL) {
1081 LOGE("Failed to create context from string!");
1082 return HC_ERR_JSON_FAIL;
1083 }
1084 const char *appId = GetStringFromJson(context, FIELD_SERVICE_PKG_NAME);
1085 if (appId == NULL) {
1086 LOGE("get appId from json fail.");
1087 FreeJson(context);
1088 return HC_ERR_JSON_GET;
1089 }
1090 int32_t res = BuildServerAuthContext(requestId, opCode, appId, context);
1091 if (res != HC_SUCCESS) {
1092 FreeJson(context);
1093 return res;
1094 }
1095 SessionInitParams params = { context, *callback };
1096 res = OpenDevSession(requestId, appId, ¶ms);
1097 FreeJson(context);
1098 return res;
1099 }
1100
OpenServerAuthSessionForP2P(int64_t requestId,const CJson * receivedMsg,const DeviceAuthCallback * callback)1101 static int32_t OpenServerAuthSessionForP2P(
1102 int64_t requestId, const CJson *receivedMsg, const DeviceAuthCallback *callback)
1103 {
1104 int32_t opCode = P2P_BIND;
1105 if (GetIntFromJson(receivedMsg, FIELD_OP_CODE, &opCode) != HC_SUCCESS) {
1106 opCode = P2P_BIND;
1107 LOGW("use default opCode.");
1108 }
1109 char *returnDataStr = ProcessRequestCallback(requestId, opCode, NULL, callback);
1110 if (returnDataStr == NULL) {
1111 LOGE("The OnRequest callback is fail!");
1112 return HC_ERR_REQ_REJECTED;
1113 }
1114 CJson *context = CreateJsonFromString(returnDataStr);
1115 FreeJsonString(returnDataStr);
1116 if (context == NULL) {
1117 LOGE("Failed to create context from string!");
1118 return HC_ERR_JSON_FAIL;
1119 }
1120 if (AddBoolToJson(context, FIELD_IS_DIRECT_AUTH, true) != HC_SUCCESS) {
1121 LOGE("Failed to add isDirectAuth to context!");
1122 FreeJson(context);
1123 return HC_ERR_JSON_ADD;
1124 }
1125 const char *pkgName = GetStringFromJson(context, FIELD_SERVICE_PKG_NAME);
1126 if (pkgName == NULL && AddStringToJson(context, FIELD_SERVICE_PKG_NAME, DEFAULT_PACKAGE_NAME) != HC_SUCCESS) {
1127 LOGE("Failed to add default package name to context!");
1128 FreeJson(context);
1129 return HC_ERR_JSON_ADD;
1130 }
1131 const char *serviceType = GetStringFromJson(context, FIELD_SERVICE_TYPE);
1132 if (serviceType == NULL && AddStringToJson(context, FIELD_SERVICE_TYPE, DEFAULT_SERVICE_TYPE) != HC_SUCCESS) {
1133 LOGE("Failed to add default package name to context!");
1134 FreeJson(context);
1135 return HC_ERR_JSON_ADD;
1136 }
1137 const char *appId = pkgName != NULL ? pkgName : DEFAULT_PACKAGE_NAME;
1138 int32_t res = BuildServerP2PAuthContext(requestId, opCode, appId, context);
1139 if (res != HC_SUCCESS) {
1140 FreeJson(context);
1141 return res;
1142 }
1143 SessionInitParams params = { context, *callback };
1144 res = OpenDevSession(requestId, appId, ¶ms);
1145 FreeJson(context);
1146 return res;
1147 }
1148
ProcessData(int64_t authReqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * gaCallback)1149 static int32_t ProcessData(int64_t authReqId, const uint8_t *data, uint32_t dataLen,
1150 const DeviceAuthCallback *gaCallback)
1151 {
1152 SET_LOG_MODE(TRACE_MODE);
1153 SET_TRACE_ID(authReqId);
1154 if (!IsSessionExist(authReqId)) {
1155 ADD_PERFORM_DATA(authReqId, false, false, HcGetCurTimeInMillis());
1156 } else {
1157 UPDATE_PERFORM_DATA_BY_SELF_INDEX(authReqId, HcGetCurTimeInMillis());
1158 }
1159 LOGI("[GA] Begin ProcessData. [DataLen]: %u, [ReqId]: %" PRId64, dataLen, authReqId);
1160 if ((data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) {
1161 LOGE("Invalid input for ProcessData!");
1162 return HC_ERR_INVALID_PARAMS;
1163 }
1164 CJson *receivedMsg = CreateJsonFromString((const char *)data);
1165 if (receivedMsg == NULL) {
1166 LOGE("Failed to create json from string!");
1167 return HC_ERR_JSON_FAIL;
1168 }
1169 int32_t res;
1170 if (!IsSessionExist(authReqId)) {
1171 res = OpenServerAuthSession(authReqId, receivedMsg, gaCallback);
1172 if (res != HC_SUCCESS) {
1173 FreeJson(receivedMsg);
1174 return res;
1175 }
1176 }
1177 if (HasAccountAuthPlugin() == HC_SUCCESS) {
1178 res = AddOriginDataForPlugin(receivedMsg, data);
1179 if (res != HC_SUCCESS) {
1180 FreeJson(receivedMsg);
1181 return res;
1182 }
1183 }
1184 res = PushProcSessionTask(authReqId, receivedMsg);
1185 if (res != HC_SUCCESS) {
1186 FreeJson(receivedMsg);
1187 return res;
1188 }
1189 return HC_SUCCESS;
1190 }
1191
CancelRequest(int64_t requestId,const char * appId)1192 static void CancelRequest(int64_t requestId, const char *appId)
1193 {
1194 SET_LOG_MODE(TRACE_MODE);
1195 SET_TRACE_ID(requestId);
1196 DEV_AUTH_REPORT_UE_CALL_EVENT_BY_PARAMS(DEFAULT_OS_ACCOUNT, NULL, appId, CANCEL_REQUEST_EVENT);
1197 if (appId == NULL) {
1198 LOGE("Invalid app id!");
1199 return;
1200 }
1201 LOGI("cancel request. [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
1202 CancelDevSession(requestId, appId);
1203 }
1204
GetRealInfo(int32_t osAccountId,const char * pseudonymId,char ** realInfo)1205 static int32_t GetRealInfo(int32_t osAccountId, const char *pseudonymId, char **realInfo)
1206 {
1207 if (pseudonymId == NULL || realInfo == NULL) {
1208 LOGE("Invalid params!");
1209 return HC_ERR_INVALID_PARAMS;
1210 }
1211 PseudonymManager *pseudonymInstance = GetPseudonymInstance();
1212 if (pseudonymInstance == NULL) {
1213 LOGE("not support privacy enhancement!");
1214 return HC_ERR_NOT_SUPPORT;
1215 }
1216 return pseudonymInstance->getRealInfo(osAccountId, pseudonymId, realInfo);
1217 }
1218
GetPseudonymId(int32_t osAccountId,const char * indexKey,char ** pseudonymId)1219 static int32_t GetPseudonymId(int32_t osAccountId, const char *indexKey, char **pseudonymId)
1220 {
1221 if (indexKey == NULL || pseudonymId == NULL) {
1222 LOGE("Invalid params!");
1223 return HC_ERR_INVALID_PARAMS;
1224 }
1225 PseudonymManager *pseudonymInstance = GetPseudonymInstance();
1226 if (pseudonymInstance == NULL) {
1227 LOGE("not support privacy enhancement!");
1228 return HC_ERR_NOT_SUPPORT;
1229 }
1230 return pseudonymInstance->getPseudonymId(osAccountId, indexKey, pseudonymId);
1231 }
1232
ProcessCredential(int32_t operationCode,const char * reqJsonStr,char ** returnData)1233 DEVICE_AUTH_API_PUBLIC int32_t ProcessCredential(int32_t operationCode, const char *reqJsonStr, char **returnData)
1234 {
1235 if (reqJsonStr == NULL || returnData == NULL) {
1236 LOGE("Invalid params!");
1237 return HC_ERR_INVALID_PARAMS;
1238 }
1239
1240 const CredentialOperator *credOperator = GetCredentialOperator();
1241 if (credOperator == NULL) {
1242 LOGE("credOperator is null!");
1243 return HC_ERR_NOT_SUPPORT;
1244 }
1245
1246 int32_t res = HC_ERR_UNSUPPORTED_OPCODE;
1247 switch (operationCode) {
1248 case CRED_OP_QUERY:
1249 res = credOperator->queryCredential(reqJsonStr, returnData);
1250 break;
1251 case CRED_OP_CREATE:
1252 res = credOperator->genarateCredential(reqJsonStr, returnData);
1253 break;
1254 case CRED_OP_IMPORT:
1255 res = credOperator->importCredential(reqJsonStr, returnData);
1256 break;
1257 case CRED_OP_DELETE:
1258 res = credOperator->deleteCredential(reqJsonStr, returnData);
1259 break;
1260 default:
1261 LOGE("invalid opCode: %d", operationCode);
1262 break;
1263 }
1264
1265 return res;
1266 }
1267
ProcessAuthDevice(int64_t authReqId,const char * authParams,const DeviceAuthCallback * callback)1268 DEVICE_AUTH_API_PUBLIC int32_t ProcessAuthDevice(
1269 int64_t authReqId, const char *authParams, const DeviceAuthCallback *callback)
1270 {
1271 SET_LOG_MODE(TRACE_MODE);
1272 SET_TRACE_ID(authReqId);
1273 LOGI("[DA] Begin ProcessAuthDevice [ReqId]: %" PRId64, authReqId);
1274 if (authParams == NULL || HcStrlen(authParams) > MAX_DATA_BUFFER_SIZE) {
1275 LOGE("Invalid input for ProcessData!");
1276 return HC_ERR_INVALID_PARAMS;
1277 }
1278 CJson *json = CreateJsonFromString(authParams);
1279 if (json == NULL) {
1280 LOGE("Failed to create json from string!");
1281 return HC_ERR_JSON_FAIL;
1282 }
1283 const char *data = GetStringFromJson(json, "data");
1284 if (data == NULL) {
1285 LOGE("Failed to get received data from parameter!");
1286 FreeJson(json);
1287 return HC_ERR_INVALID_PARAMS;
1288 }
1289 CJson *receivedMsg = CreateJsonFromString(data);
1290 FreeJson(json);
1291 if (receivedMsg == NULL) {
1292 LOGE("Failed to create json from string!");
1293 return HC_ERR_JSON_FAIL;
1294 }
1295 int32_t res;
1296 if (!IsSessionExist(authReqId)) {
1297 res = OpenServerAuthSessionForP2P(authReqId, receivedMsg, callback);
1298 if (res != HC_SUCCESS) {
1299 FreeJson(receivedMsg);
1300 return res;
1301 }
1302 }
1303 res = PushProcSessionTask(authReqId, receivedMsg);
1304 if (res != HC_SUCCESS) {
1305 FreeJson(receivedMsg);
1306 return res;
1307 }
1308 return HC_SUCCESS;
1309 }
1310
StartAuthDevice(int64_t authReqId,const char * authParams,const DeviceAuthCallback * callback)1311 DEVICE_AUTH_API_PUBLIC int32_t StartAuthDevice(
1312 int64_t authReqId, const char *authParams, const DeviceAuthCallback *callback)
1313 {
1314 SET_LOG_MODE(TRACE_MODE);
1315 SET_TRACE_ID(authReqId);
1316 LOGI("StartAuthDevice. [ReqId]:%" PRId64, authReqId);
1317
1318 if ((authParams == NULL) || (callback == NULL) || HcStrlen(authParams) > MAX_DATA_BUFFER_SIZE) {
1319 LOGE("The input auth params is invalid!");
1320 return HC_ERR_INVALID_PARAMS;
1321 }
1322 CJson *context = CreateJsonFromString(authParams);
1323 if (context == NULL) {
1324 LOGE("Failed to create json from string!");
1325 return HC_ERR_JSON_FAIL;
1326 }
1327 int32_t osAccountId = INVALID_OS_ACCOUNT;
1328 if (GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId) != HC_SUCCESS) {
1329 LOGE("Failed to get osAccountId from json!");
1330 FreeJson(context);
1331 return HC_ERR_JSON_FAIL;
1332 }
1333
1334 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
1335 if (osAccountId == INVALID_OS_ACCOUNT) {
1336 FreeJson(context);
1337 return HC_ERR_INVALID_PARAMS;
1338 }
1339 int32_t res = BuildClientAuthContext(osAccountId, authReqId, DEFAULT_PACKAGE_NAME, context);
1340 if (res != HC_SUCCESS) {
1341 FreeJson(context);
1342 return res;
1343 }
1344 res = BuildP2PBindContext(context);
1345 if (res != HC_SUCCESS) {
1346 FreeJson(context);
1347 return res;
1348 }
1349 SessionInitParams params = { context, *callback };
1350 res = OpenDevSession(authReqId, DEFAULT_PACKAGE_NAME, ¶ms);
1351 FreeJson(context);
1352 if (res != HC_SUCCESS) {
1353 LOGE("OpenDevSession fail. [Res]: %d", res);
1354 return res;
1355 }
1356 return PushStartSessionTask(authReqId);
1357 }
1358
CancelAuthRequest(int64_t requestId,const char * authParams)1359 DEVICE_AUTH_API_PUBLIC int32_t CancelAuthRequest(int64_t requestId, const char *authParams)
1360 {
1361 SET_LOG_MODE(TRACE_MODE);
1362 SET_TRACE_ID(requestId);
1363 if (authParams == NULL || HcStrlen(authParams) > MAX_DATA_BUFFER_SIZE) {
1364 LOGE("Invalid authParams!");
1365 return HC_ERR_INVALID_PARAMS;
1366 }
1367 LOGI("cancel request. [ReqId]: %" PRId64, requestId);
1368 CancelDevSession(requestId, DEFAULT_PACKAGE_NAME);
1369 return HC_SUCCESS;
1370 }
1371
AllocGmAndGa(void)1372 static int32_t AllocGmAndGa(void)
1373 {
1374 if (g_groupManagerInstance == NULL) {
1375 g_groupManagerInstance = (DeviceGroupManager *)HcMalloc(sizeof(DeviceGroupManager), 0);
1376 if (g_groupManagerInstance == NULL) {
1377 LOGE("Failed to allocate groupManager Instance memory!");
1378 return HC_ERR_ALLOC_MEMORY;
1379 }
1380 }
1381 if (g_groupAuthManager == NULL) {
1382 g_groupAuthManager = (GroupAuthManager *)HcMalloc(sizeof(GroupAuthManager), 0);
1383 if (g_groupAuthManager == NULL) {
1384 LOGE("Failed to allocate groupAuth Instance memory!");
1385 HcFree(g_groupManagerInstance);
1386 g_groupManagerInstance = NULL;
1387 return HC_ERR_ALLOC_MEMORY;
1388 }
1389 }
1390 return HC_SUCCESS;
1391 }
1392
DestroyGmAndGa(void)1393 static void DestroyGmAndGa(void)
1394 {
1395 if (g_groupAuthManager != NULL) {
1396 HcFree(g_groupAuthManager);
1397 g_groupAuthManager = NULL;
1398 }
1399 if (g_groupManagerInstance != NULL) {
1400 HcFree(g_groupManagerInstance);
1401 g_groupManagerInstance = NULL;
1402 }
1403 }
1404
InitAllModules(void)1405 static int32_t InitAllModules(void)
1406 {
1407 int32_t res = GetLoaderInstance()->initAlg();
1408 if (res != HC_SUCCESS) {
1409 LOGE("[End]: [Service]: Failed to init algorithm module!");
1410 return res;
1411 }
1412 res = InitCredMgr();
1413 if (res != HC_SUCCESS) {
1414 LOGE("[End]: [Service]: Failed to init cred mgr!");
1415 return res;
1416 }
1417 res = InitModules();
1418 if (res != HC_SUCCESS) {
1419 LOGE("[End]: [Service]: Failed to init all authenticator modules!");
1420 goto CLEAN_CRED;
1421 }
1422 res = InitCallbackManager();
1423 if (res != HC_SUCCESS) {
1424 LOGE("[End]: [Service]: Failed to init callback manage module!");
1425 goto CLEAN_MODULE;
1426 }
1427 res = InitGroupManager();
1428 if (res != HC_SUCCESS) {
1429 goto CLEAN_CALLBACK;
1430 }
1431 res = InitDevSessionManager();
1432 if (res != HC_SUCCESS) {
1433 goto CLEAN_GROUP_MANAGER;
1434 }
1435 (void)InitGroupAuthManager();
1436 res = InitTaskManager();
1437 if (res != HC_SUCCESS) {
1438 LOGE("[End]: [Service]: Failed to init worker thread!");
1439 goto CLEAN_ALL;
1440 }
1441 return res;
1442 CLEAN_ALL:
1443 DestroyDevSessionManager();
1444 CLEAN_GROUP_MANAGER:
1445 DestroyGroupManager();
1446 CLEAN_CALLBACK:
1447 DestroyCallbackManager();
1448 CLEAN_MODULE:
1449 DestroyModules();
1450 CLEAN_CRED:
1451 DestroyCredMgr();
1452 return res;
1453 }
1454
InitPseudonymModule(void)1455 static void InitPseudonymModule(void)
1456 {
1457 PseudonymManager *manager = GetPseudonymInstance();
1458 if (manager == NULL) {
1459 LOGE("Pseudonym manager is null!");
1460 return;
1461 }
1462 manager->loadPseudonymData();
1463 }
1464
DoOnChannelOpened(HcTaskBase * baseTask)1465 static void DoOnChannelOpened(HcTaskBase *baseTask)
1466 {
1467 if (baseTask == NULL) {
1468 LOGE("The input task is NULL!");
1469 return;
1470 }
1471 SoftBusTask *task = (SoftBusTask *)baseTask;
1472 SET_LOG_MODE(TRACE_MODE);
1473 SET_TRACE_ID(task->requestId);
1474 LOGI("[Start]: DoOnChannelOpened!");
1475 int32_t res = StartDevSession(task->requestId);
1476 if (res != HC_SUCCESS) {
1477 LOGE("start session fail.[Res]: %d", res);
1478 CloseDevSession(task->requestId);
1479 }
1480 }
1481
InitSoftBusTask(SoftBusTask * task,int64_t requestId)1482 static void InitSoftBusTask(SoftBusTask *task, int64_t requestId)
1483 {
1484 task->base.doAction = DoOnChannelOpened;
1485 task->base.destroy = NULL;
1486 task->requestId = requestId;
1487 }
1488
OnChannelOpenedCb(int64_t requestId,int result)1489 static int OnChannelOpenedCb(int64_t requestId, int result)
1490 {
1491 if (result != HC_SUCCESS) {
1492 LOGE("[SoftBus][Out]: Failed to open channel! res: %d", result);
1493 CloseDevSession(requestId);
1494 return HC_ERR_SOFT_BUS;
1495 }
1496 LOGI("[Start]: OnChannelOpened! [ReqId]: %" PRId64, requestId);
1497 SoftBusTask *task = (SoftBusTask *)HcMalloc(sizeof(SoftBusTask), 0);
1498 if (task == NULL) {
1499 LOGE("Failed to allocate task memory!");
1500 CloseDevSession(requestId);
1501 return HC_ERR_ALLOC_MEMORY;
1502 }
1503 InitSoftBusTask(task, requestId);
1504 if (PushTask((HcTaskBase *)task) != HC_SUCCESS) {
1505 HcFree(task);
1506 CloseDevSession(requestId);
1507 return HC_ERR_INIT_TASK_FAIL;
1508 }
1509 LOGI("[End]: OnChannelOpened!");
1510 return HC_SUCCESS;
1511 }
1512
OnChannelClosedCb(void)1513 static void OnChannelClosedCb(void)
1514 {
1515 return;
1516 }
1517
OnBytesReceivedCb(int64_t requestId,uint8_t * data,uint32_t dataLen)1518 static void OnBytesReceivedCb(int64_t requestId, uint8_t *data, uint32_t dataLen)
1519 {
1520 if ((data == NULL) || (dataLen == 0) || (dataLen > MAX_DATA_BUFFER_SIZE)) {
1521 LOGE("Invalid input params!");
1522 return;
1523 }
1524 (void)ProcessBindData(requestId, data, dataLen);
1525 }
1526
RegCallback(const char * appId,const DeviceAuthCallback * callback)1527 static int32_t RegCallback(const char *appId, const DeviceAuthCallback *callback)
1528 {
1529 SET_LOG_MODE(NORMAL_MODE);
1530 if ((appId == NULL) || (callback == NULL)) {
1531 LOGE("The input parameters contains NULL value!");
1532 return HC_ERR_INVALID_PARAMS;
1533 }
1534 ChannelProxy proxy = {
1535 .onChannelOpened = OnChannelOpenedCb,
1536 .onChannelClosed = OnChannelClosedCb,
1537 .onBytesReceived = OnBytesReceivedCb
1538 };
1539 int32_t res = InitChannelManager(&proxy);
1540 if (res != HC_SUCCESS) {
1541 LOGE("[End]: [Service]: Failed to init channel manage module!");
1542 return res;
1543 }
1544 return RegGroupManagerCallback(appId, callback);
1545 }
1546
InitDeviceAuthService(void)1547 DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService(void)
1548 {
1549 LOGI("[Service]: Start to init device auth service!");
1550 if (CheckInit() == FINISH_INIT) {
1551 LOGI("[End]: [Service]: Device auth service is running!");
1552 return HC_SUCCESS;
1553 }
1554 int32_t res = AllocGmAndGa();
1555 if (res != HC_SUCCESS) {
1556 return res;
1557 }
1558 InitOsAccountAdapter();
1559 res = InitAllModules();
1560 if (res != HC_SUCCESS) {
1561 DestroyGmAndGa();
1562 return res;
1563 }
1564 INIT_PERFORMANCE_DUMPER();
1565 InitPseudonymModule();
1566 DEV_AUTH_LOAD_PLUGIN();
1567 SetInitStatus();
1568 LOGI("[End]: [Service]: Init device auth service successfully!");
1569 return HC_SUCCESS;
1570 }
1571
DestroyDeviceAuthService(void)1572 DEVICE_AUTH_API_PUBLIC void DestroyDeviceAuthService(void)
1573 {
1574 LOGI("[Service]: Start to destroy device auth service!");
1575 if (CheckDestroy() == FINISH_DESTROY) {
1576 LOGI("[End]: [Service]: The service has not been initialized!");
1577 return;
1578 }
1579 DestroyTaskManager();
1580 DestroyDevSessionManager();
1581 DestroyGroupManager();
1582 DestroyGmAndGa();
1583 DEV_AUTH_UNLOAD_PLUGIN();
1584 DestroyModules();
1585 DestroyCredMgr();
1586 DestroyChannelManager();
1587 DestroyCallbackManager();
1588 DESTROY_PERFORMANCE_DUMPER();
1589 DestroyPseudonymManager();
1590 DestroyOsAccountAdapter();
1591 SetDeInitStatus();
1592 LOGI("[End]: [Service]: Destroy device auth service successfully!");
1593 }
1594
GetGmInstance(void)1595 DEVICE_AUTH_API_PUBLIC const DeviceGroupManager *GetGmInstance(void)
1596 {
1597 if (g_groupManagerInstance == NULL) {
1598 LOGE("Service not init.");
1599 return NULL;
1600 }
1601
1602 g_groupManagerInstance->regCallback = RegCallback;
1603 g_groupManagerInstance->unRegCallback = UnRegGroupManagerCallback;
1604 g_groupManagerInstance->regDataChangeListener = RegListenerImpl;
1605 g_groupManagerInstance->unRegDataChangeListener = UnRegListenerImpl;
1606 g_groupManagerInstance->createGroup = CreateGroupImpl;
1607 g_groupManagerInstance->deleteGroup = DeleteGroupImpl;
1608 g_groupManagerInstance->addMemberToGroup = AddMemberToGroup;
1609 g_groupManagerInstance->deleteMemberFromGroup = DeleteMemberFromGroupImpl;
1610 g_groupManagerInstance->addMultiMembersToGroup = AddMultiMembersToGroupImpl;
1611 g_groupManagerInstance->delMultiMembersFromGroup = DelMultiMembersFromGroupImpl;
1612 g_groupManagerInstance->processData = ProcessBindData;
1613 g_groupManagerInstance->getRegisterInfo = GetRegisterInfoImpl;
1614 g_groupManagerInstance->checkAccessToGroup = CheckAccessToGroupImpl;
1615 g_groupManagerInstance->getPkInfoList = GetPkInfoListImpl;
1616 g_groupManagerInstance->getGroupInfoById = GetGroupInfoByIdImpl;
1617 g_groupManagerInstance->getGroupInfo = GetGroupInfoImpl;
1618 g_groupManagerInstance->getJoinedGroups = GetJoinedGroupsImpl;
1619 g_groupManagerInstance->getRelatedGroups = GetRelatedGroupsImpl;
1620 g_groupManagerInstance->getDeviceInfoById = GetDeviceInfoByIdImpl;
1621 g_groupManagerInstance->getTrustedDevices = GetTrustedDevicesImpl;
1622 g_groupManagerInstance->isDeviceInGroup = IsDeviceInGroupImpl;
1623 g_groupManagerInstance->cancelRequest = CancelRequest;
1624 g_groupManagerInstance->destroyInfo = DestroyInfoImpl;
1625 return g_groupManagerInstance;
1626 }
1627
GetGaInstance(void)1628 DEVICE_AUTH_API_PUBLIC const GroupAuthManager *GetGaInstance(void)
1629 {
1630 if (g_groupAuthManager == NULL) {
1631 LOGE("Service not init.");
1632 return NULL;
1633 }
1634
1635 g_groupAuthManager->processData = ProcessData;
1636 g_groupAuthManager->authDevice = AuthDevice;
1637 g_groupAuthManager->cancelRequest = CancelRequest;
1638 g_groupAuthManager->getRealInfo = GetRealInfo;
1639 g_groupAuthManager->getPseudonymId = GetPseudonymId;
1640 return g_groupAuthManager;
1641 }
1642