• 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 "device_auth.h"
17 
18 #include "alg_loader.h"
19 #include "callback_manager.h"
20 #include "channel_manager.h"
21 #include "common_defs.h"
22 #include "cred_manager.h"
23 #include "data_manager.h"
24 #include "dev_auth_module_manager.h"
25 #include "dev_session_mgr.h"
26 #include "group_manager.h"
27 #include "group_operation_common.h"
28 #include "hc_dev_info.h"
29 #include "hc_init_protection.h"
30 #include "hc_log.h"
31 #include "hisysevent_adapter.h"
32 #include "hitrace_adapter.h"
33 #include "json_utils.h"
34 #include "key_manager.h"
35 #include "os_account_adapter.h"
36 #include "plugin_adapter.h"
37 #include "pseudonym_manager.h"
38 #include "task_manager.h"
39 
40 static GroupAuthManager *g_groupAuthManager =  NULL;
41 static DeviceGroupManager *g_groupManagerInstance = NULL;
42 
43 typedef struct {
44     HcTaskBase base;
45     int64_t sessionId;
46 } StartSessionTask;
47 
48 typedef struct {
49     HcTaskBase base;
50     int64_t sessionId;
51     CJson *receivedMsg;
52 } ProcSessionTask;
53 
IsDeviceIdHashMatch(const char * udid,const char * subUdidHash)54 static int32_t IsDeviceIdHashMatch(const char *udid, const char *subUdidHash)
55 {
56     Uint8Buff udidBuf = { (uint8_t *)udid, (uint32_t)HcStrlen(udid) };
57     uint8_t udidHashByte[SHA256_LEN] = { 0 };
58     Uint8Buff udidHashBuf = { udidHashByte, sizeof(udidHashByte) };
59     int32_t ret = GetLoaderInstance()->sha256(&udidBuf, &udidHashBuf);
60     if (ret != HC_SUCCESS) {
61         LOGE("sha256 failed, ret:%d", ret);
62         return ret;
63     }
64     uint32_t udidHashLen = SHA256_LEN * BYTE_TO_HEX_OPER_LENGTH + 1;
65     char *udidHash = (char *)HcMalloc(udidHashLen, 0);
66     if (udidHash == NULL) {
67         LOGE("malloc udidHash string failed");
68         return HC_ERR_ALLOC_MEMORY;
69     }
70     ret = ByteToHexString(udidHashByte, SHA256_LEN, udidHash, udidHashLen);
71     if (ret != HC_SUCCESS) {
72         LOGE("Byte to hexString failed, ret:%d", ret);
73         HcFree(udidHash);
74         return ret;
75     }
76     char *subUdidHashUpper = NULL;
77     ret = ToUpperCase(subUdidHash, &subUdidHashUpper);
78     if (ret != HC_SUCCESS) {
79         LOGE("Failed to convert the input sub udid hash to upper case!");
80         HcFree(udidHash);
81         return ret;
82     }
83     if (strstr((const char *)udidHash, subUdidHashUpper) != NULL) {
84         LOGI("udid hash is match!");
85         HcFree(udidHash);
86         HcFree(subUdidHashUpper);
87         return HC_SUCCESS;
88     }
89     HcFree(udidHash);
90     HcFree(subUdidHashUpper);
91     return HC_ERROR;
92 }
93 
GetUdidByGroup(int32_t osAccountId,const char * groupId,const char * deviceIdHash)94 static const char *GetUdidByGroup(int32_t osAccountId, const char *groupId, const char *deviceIdHash)
95 {
96     uint32_t index;
97     TrustedDeviceEntry **deviceEntry = NULL;
98     DeviceEntryVec deviceEntryVec = CREATE_HC_VECTOR(DeviceEntryVec);
99     QueryDeviceParams params = InitQueryDeviceParams();
100     params.groupId = groupId;
101     if (QueryDevices(osAccountId, &params, &deviceEntryVec) != HC_SUCCESS) {
102         LOGE("query trusted devices failed!");
103         ClearDeviceEntryVec(&deviceEntryVec);
104         return NULL;
105     }
106     FOR_EACH_HC_VECTOR(deviceEntryVec, index, deviceEntry) {
107         const char *udid = StringGet(&(*deviceEntry)->udid);
108         if (IsDeviceIdHashMatch(udid, deviceIdHash) == HC_SUCCESS) {
109             ClearDeviceEntryVec(&deviceEntryVec);
110             return udid;
111         }
112         continue;
113     }
114     ClearDeviceEntryVec(&deviceEntryVec);
115     return NULL;
116 }
117 
GetDeviceIdByUdidHash(int32_t osAccountId,const char * deviceIdHash)118 static const char *GetDeviceIdByUdidHash(int32_t osAccountId, const char *deviceIdHash)
119 {
120     if (deviceIdHash == NULL) {
121         LOGE("deviceIdHash is null");
122         return NULL;
123     }
124     QueryGroupParams queryParams = InitQueryGroupParams();
125     GroupEntryVec groupEntryVec = CreateGroupEntryVec();
126     int32_t ret = QueryGroups(osAccountId, &queryParams, &groupEntryVec);
127     if (ret != HC_SUCCESS) {
128         LOGE("Failed to query groups!");
129         ClearGroupEntryVec(&groupEntryVec);
130         return NULL;
131     }
132     uint32_t index;
133     TrustedGroupEntry **ptr = NULL;
134     FOR_EACH_HC_VECTOR(groupEntryVec, index, ptr) {
135         const TrustedGroupEntry *groupEntry = (const TrustedGroupEntry *)(*ptr);
136         const char *groupId = StringGet(&(groupEntry->id));
137         if (groupId == NULL) {
138             continue;
139         }
140         const char *udid = GetUdidByGroup(osAccountId, groupId, deviceIdHash);
141         if (udid != NULL) {
142             ClearGroupEntryVec(&groupEntryVec);
143             return udid;
144         }
145     }
146     ClearGroupEntryVec(&groupEntryVec);
147     return NULL;
148 }
149 
GetPeerUdidFromJson(int32_t osAccountId,const CJson * in)150 static const char *GetPeerUdidFromJson(int32_t osAccountId, const CJson *in)
151 {
152     const char *peerConnDeviceId = GetStringFromJson(in, FIELD_PEER_CONN_DEVICE_ID);
153     if (peerConnDeviceId == NULL) {
154         LOGE("get peerConnDeviceId from json fail.");
155         return NULL;
156     }
157     bool isUdidHash = false;
158     (void)GetBoolFromJson(in, FIELD_IS_UDID_HASH, &isUdidHash);
159     if (isUdidHash) {
160         const char *deviceId = GetDeviceIdByUdidHash(osAccountId, peerConnDeviceId);
161         return (deviceId == NULL ? peerConnDeviceId : deviceId);
162     }
163     return peerConnDeviceId;
164 }
165 
AddGroupInfoToContextByInput(const CJson * receivedMsg,CJson * context)166 static int32_t AddGroupInfoToContextByInput(const CJson *receivedMsg, CJson *context)
167 {
168     const char *groupId = GetStringFromJson(receivedMsg, FIELD_GROUP_ID);
169     if (groupId == NULL) {
170         LOGE("get groupId from json fail.");
171         return HC_ERR_JSON_GET;
172     }
173     const char *groupName = GetStringFromJson(receivedMsg, FIELD_GROUP_NAME);
174     if (groupName == NULL) {
175         LOGE("Failed to get groupName from jsonParams!");
176         return HC_ERR_JSON_GET;
177     }
178     if (AddStringToJson(context, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
179         LOGE("Failed to add groupId to json!");
180         return HC_ERR_JSON_FAIL;
181     }
182     if (AddIntToJson(context, FIELD_GROUP_TYPE, PEER_TO_PEER_GROUP) != HC_SUCCESS) {
183         LOGE("Failed to add groupType to json!");
184         return HC_ERR_JSON_FAIL;
185     }
186     if (AddStringToJson(context, FIELD_GROUP_NAME, groupName) != HC_SUCCESS) {
187         LOGE("Failed to add groupName to json!");
188         return HC_ERR_JSON_FAIL;
189     }
190     return HC_SUCCESS;
191 }
192 
AddDevInfoToContextByInput(CJson * context)193 static int32_t AddDevInfoToContextByInput(CJson *context)
194 {
195     int32_t userType = DEVICE_TYPE_ACCESSORY;
196     (void)GetIntFromJson(context, FIELD_USER_TYPE, &userType);
197     const char *authId = GetStringFromJson(context, FIELD_DEVICE_ID);
198     char udid[INPUT_UDID_LEN] = { 0 };
199     if (authId == NULL) {
200         LOGD("No authId is found. The default value is udid!");
201         int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
202         if (res != HC_SUCCESS) {
203             LOGE("Failed to get local udid! res: %d", res);
204             return HC_ERR_DB;
205         }
206         authId = udid;
207     }
208     if (AddStringToJson(context, FIELD_AUTH_ID, authId) != HC_SUCCESS) {
209         LOGE("Failed to add authId to params!");
210         return HC_ERR_JSON_FAIL;
211     }
212     if (AddIntToJson(context, FIELD_USER_TYPE, userType) != HC_SUCCESS) {
213         LOGE("Failed to add userType to params!");
214         return HC_ERR_JSON_FAIL;
215     }
216     return HC_SUCCESS;
217 }
218 
AddGroupInfoToContextByDb(const char * groupId,CJson * context)219 static int32_t AddGroupInfoToContextByDb(const char *groupId, CJson *context)
220 {
221     int32_t osAccountId;
222     if (GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId) != HC_SUCCESS) {
223         LOGE("get osAccountId from json fail.");
224         return HC_ERR_JSON_GET;
225     }
226     TrustedGroupEntry *entry = GetGroupEntryById(osAccountId, groupId);
227     if (entry == NULL) {
228         LOGE("Failed to get groupEntry from db!");
229         return HC_ERR_DB;
230     }
231     if (AddStringToJson(context, FIELD_GROUP_ID, StringGet(&entry->id)) != HC_SUCCESS) {
232         LOGE("Failed to add groupId to json!");
233         DestroyGroupEntry(entry);
234         return HC_ERR_JSON_FAIL;
235     }
236     if (AddIntToJson(context, FIELD_GROUP_TYPE, entry->type) != HC_SUCCESS) {
237         LOGE("Failed to add groupType to json!");
238         DestroyGroupEntry(entry);
239         return HC_ERR_JSON_FAIL;
240     }
241     if (AddStringToJson(context, FIELD_GROUP_NAME, StringGet(&entry->name)) != HC_SUCCESS) {
242         LOGE("Failed to add groupName to json!");
243         DestroyGroupEntry(entry);
244         return HC_ERR_JSON_FAIL;
245     }
246     DestroyGroupEntry(entry);
247     return HC_SUCCESS;
248 }
249 
AddDevInfoToContextByDb(const char * groupId,CJson * context)250 static int32_t AddDevInfoToContextByDb(const char *groupId, CJson *context)
251 {
252     int32_t osAccountId;
253     if (GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId) != HC_SUCCESS) {
254         LOGE("get osAccountId from json fail.");
255         return HC_ERR_JSON_GET;
256     }
257     char udid[INPUT_UDID_LEN] = { 0 };
258     int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
259     if (res != HC_SUCCESS) {
260         LOGE("Failed to get local udid! res: %d", res);
261         return HC_ERR_DB;
262     }
263     TrustedDeviceEntry *devAuthParams = CreateDeviceEntry();
264     if (devAuthParams == NULL) {
265         LOGE("Failed to allocate devEntry memory!");
266         return HC_ERR_ALLOC_MEMORY;
267     }
268     if (GetTrustedDevInfoById(osAccountId, udid, true, groupId, devAuthParams) != HC_SUCCESS) {
269         LOGE("Failed to obtain the local device information from the database!");
270         DestroyDeviceEntry(devAuthParams);
271         return HC_ERR_DB;
272     }
273     if (AddStringToJson(context, FIELD_AUTH_ID, StringGet(&devAuthParams->authId)) != HC_SUCCESS) {
274         LOGE("Failed to add authId to params!");
275         DestroyDeviceEntry(devAuthParams);
276         return HC_ERR_JSON_FAIL;
277     }
278     if (AddIntToJson(context, FIELD_USER_TYPE, devAuthParams->devType) != HC_SUCCESS) {
279         LOGE("Failed to add userType to params!");
280         DestroyDeviceEntry(devAuthParams);
281         return HC_ERR_JSON_FAIL;
282     }
283     DestroyDeviceEntry(devAuthParams);
284     return HC_SUCCESS;
285 }
286 
GetOpCodeFromContext(const CJson * context)287 static int32_t GetOpCodeFromContext(const CJson *context)
288 {
289     bool isAdmin = true;
290     (void)GetBoolFromJson(context, FIELD_IS_ADMIN, &isAdmin);
291     return isAdmin ? MEMBER_INVITE : MEMBER_JOIN;
292 }
293 
AddClientReqInfoToContext(int32_t osAccountId,int64_t requestId,const char * appId,CJson * context)294 static int32_t AddClientReqInfoToContext(int32_t osAccountId, int64_t requestId, const char *appId, CJson *context)
295 {
296     const char *groupId = GetStringFromJson(context, FIELD_GROUP_ID);
297     if (groupId == NULL) {
298         LOGE("get groupId from json fail.");
299         return HC_ERR_JSON_GET;
300     }
301     if (AddBoolToJson(context, FIELD_IS_BIND, true) != HC_SUCCESS) {
302         LOGE("add isBind to context fail.");
303         return HC_ERR_JSON_ADD;
304     }
305     if (AddBoolToJson(context, FIELD_IS_CLIENT, true) != HC_SUCCESS) {
306         LOGE("add isClient to context fail.");
307         return HC_ERR_JSON_ADD;
308     }
309     if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
310         LOGE("add osAccountId to context fail.");
311         return HC_ERR_JSON_ADD;
312     }
313     if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
314         LOGE("add requestId to context fail.");
315         return HC_ERR_JSON_ADD;
316     }
317     if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
318         LOGE("add appId to context fail.");
319         return HC_ERR_JSON_ADD;
320     }
321     int32_t opCode = GetOpCodeFromContext(context);
322     if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
323         LOGE("add operationCode to context fail.");
324         return HC_ERR_JSON_ADD;
325     }
326     if (opCode == MEMBER_JOIN) {
327         return AddDevInfoToContextByInput(context);
328     }
329     int32_t res = AddDevInfoToContextByDb(groupId, context);
330     if (res != HC_SUCCESS) {
331         return res;
332     }
333     return AddGroupInfoToContextByDb(groupId, context);
334 }
335 
AddChannelInfoToContext(int32_t channelType,int64_t channelId,CJson * context)336 static int32_t AddChannelInfoToContext(int32_t channelType, int64_t channelId, CJson *context)
337 {
338     if (AddIntToJson(context, FIELD_CHANNEL_TYPE, channelType) != HC_SUCCESS) {
339         LOGE("add channelType to context fail.");
340         return HC_ERR_JSON_ADD;
341     }
342     if (AddByteToJson(context, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) {
343         LOGE("add channelId to context fail.");
344         return HC_ERR_JSON_ADD;
345     }
346     return HC_SUCCESS;
347 }
348 
BuildClientBindContext(int32_t osAccountId,int64_t requestId,const char * appId,const DeviceAuthCallback * callback,CJson * context)349 static int32_t BuildClientBindContext(int32_t osAccountId, int64_t requestId, const char *appId,
350     const DeviceAuthCallback *callback, CJson *context)
351 {
352     int32_t res = AddClientReqInfoToContext(osAccountId, requestId, appId, context);
353     if (res != HC_SUCCESS) {
354         return res;
355     }
356     ChannelType channelType = GetChannelType(callback, context);
357     int64_t channelId;
358     res = OpenChannel(channelType, context, requestId, &channelId);
359     if (res != HC_SUCCESS) {
360         LOGE("open channel fail.");
361         return res;
362     }
363     return AddChannelInfoToContext(channelType, channelId, context);
364 }
365 
DoStartSession(HcTaskBase * task)366 static void DoStartSession(HcTaskBase *task)
367 {
368     LOGI("start session task begin.");
369     if (task == NULL) {
370         LOGE("The input task is NULL, can't start session!");
371         return;
372     }
373     StartSessionTask *realTask = (StartSessionTask *)task;
374     SET_LOG_MODE(TRACE_MODE);
375     SET_TRACE_ID(realTask->sessionId);
376     int32_t res = StartDevSession(realTask->sessionId);
377     if (res != HC_SUCCESS) {
378         LOGE("start session fail.[Res]: %d", res);
379         CloseDevSession(realTask->sessionId);
380     }
381 }
382 
DoProcSession(HcTaskBase * task)383 static void DoProcSession(HcTaskBase *task)
384 {
385     LOGI("proc session task begin.");
386     if (task == NULL) {
387         LOGE("The input task is NULL, can't start session!");
388         return;
389     }
390     ProcSessionTask *realTask = (ProcSessionTask *)task;
391     SET_LOG_MODE(TRACE_MODE);
392     SET_TRACE_ID(realTask->sessionId);
393     bool isFinish = false;
394     int32_t res = ProcessDevSession(realTask->sessionId, realTask->receivedMsg, &isFinish);
395     if (res != HC_SUCCESS) {
396         LOGE("ProcessDevSession fail. [Res]: %d", res);
397         CloseDevSession(realTask->sessionId);
398         return;
399     }
400     LOGI("ProcessDevSession success. [State]: %s", isFinish ? "FINISH" : "CONTINUE");
401     if (isFinish) {
402         CloseDevSession(realTask->sessionId);
403     }
404 }
405 
InitStartSessionTask(StartSessionTask * task,int64_t sessionId)406 static void InitStartSessionTask(StartSessionTask *task, int64_t sessionId)
407 {
408     task->base.doAction = DoStartSession;
409     task->base.destroy = NULL;
410     task->sessionId = sessionId;
411 }
412 
DestroyProcSessionTask(HcTaskBase * task)413 static void DestroyProcSessionTask(HcTaskBase *task)
414 {
415     ProcSessionTask *realTask = (ProcSessionTask *)task;
416     FreeJson(realTask->receivedMsg);
417 }
418 
InitProcSessionTask(ProcSessionTask * task,int64_t sessionId,CJson * receivedMsg)419 static void InitProcSessionTask(ProcSessionTask *task, int64_t sessionId, CJson *receivedMsg)
420 {
421     task->base.doAction = DoProcSession;
422     task->base.destroy = DestroyProcSessionTask;
423     task->sessionId = sessionId;
424     task->receivedMsg = receivedMsg;
425 }
426 
PushStartSessionTask(int64_t sessionId)427 static int32_t PushStartSessionTask(int64_t sessionId)
428 {
429     StartSessionTask *task = (StartSessionTask *)HcMalloc(sizeof(StartSessionTask), 0);
430     if (task == NULL) {
431         LOGE("Failed to allocate memory for task!");
432         return HC_ERR_ALLOC_MEMORY;
433     }
434     InitStartSessionTask(task, sessionId);
435     if (PushTask((HcTaskBase*)task) != HC_SUCCESS) {
436         LOGE("push start session task fail.");
437         HcFree(task);
438         return HC_ERR_INIT_TASK_FAIL;
439     }
440     LOGI("push start session task success.");
441     return HC_SUCCESS;
442 }
443 
PushProcSessionTask(int64_t sessionId,CJson * receivedMsg)444 static int32_t PushProcSessionTask(int64_t sessionId, CJson *receivedMsg)
445 {
446     ProcSessionTask *task = (ProcSessionTask *)HcMalloc(sizeof(ProcSessionTask), 0);
447     if (task == NULL) {
448         LOGE("Failed to allocate memory for task!");
449         return HC_ERR_ALLOC_MEMORY;
450     }
451     InitProcSessionTask(task, sessionId, receivedMsg);
452     if (PushTask((HcTaskBase*)task) != HC_SUCCESS) {
453         LOGE("push start session task fail.");
454         HcFree(task);
455         return HC_ERR_INIT_TASK_FAIL;
456     }
457     LOGI("push start session task success.");
458     return HC_SUCCESS;
459 }
460 
StartClientBindSession(int32_t osAccountId,int64_t requestId,const char * appId,const char * contextParams,const DeviceAuthCallback * callback)461 static int32_t StartClientBindSession(int32_t osAccountId, int64_t requestId, const char *appId,
462     const char *contextParams, const DeviceAuthCallback *callback)
463 {
464     CJson *context = CreateJsonFromString(contextParams);
465     if (context == NULL) {
466         LOGE("Failed to create json from string!");
467         return HC_ERR_JSON_FAIL;
468     }
469     int32_t res = BuildClientBindContext(osAccountId, requestId, appId, callback, context);
470     if (res != HC_SUCCESS) {
471         FreeJson(context);
472         return res;
473     }
474     ChannelType channelType = GetChannelType(callback, context);
475     SessionInitParams params = { context, *callback };
476     res = OpenDevSession(requestId, appId, &params);
477     FreeJson(context);
478     if (res != HC_SUCCESS) {
479         LOGE("OpenDevSession fail. [Res]: %d", res);
480         return res;
481     }
482     if (channelType == SERVICE_CHANNEL) {
483         res = PushStartSessionTask(requestId);
484         if (res != HC_SUCCESS) {
485             return res;
486         }
487     }
488     return HC_SUCCESS;
489 }
490 
AddMemberToGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * addParams)491 static int32_t AddMemberToGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *addParams)
492 {
493     SET_LOG_MODE(TRACE_MODE);
494     SET_TRACE_ID(requestId);
495     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
496     if ((appId == NULL) || (addParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
497         LOGE("Invalid input parameters!");
498         return HC_ERR_INVALID_PARAMS;
499     }
500     LOGI("[Start]: AddMemberToGroup! [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
501     DEV_AUTH_REPORT_CALL_EVENT(ADD_MEMBER_EVENT, osAccountId, requestId, appId);
502     const DeviceAuthCallback *callback = GetGMCallbackByAppId(appId);
503     if (callback == NULL) {
504         LOGE("Failed to find callback by appId! [AppId]: %s", appId);
505         return HC_ERR_CALLBACK_NOT_FOUND;
506     }
507     return StartClientBindSession(osAccountId, requestId, appId, addParams, callback);
508 }
509 
AddServerReqInfoToContext(int64_t requestId,const char * appId,int32_t opCode,const CJson * receivedMsg,CJson * context)510 static int32_t AddServerReqInfoToContext(int64_t requestId, const char *appId, int32_t opCode,
511     const CJson *receivedMsg, CJson *context)
512 {
513     const char *groupId = GetStringFromJson(receivedMsg, FIELD_GROUP_ID);
514     if (groupId == NULL) {
515         LOGE("get groupId from json fail.");
516         return HC_ERR_JSON_GET;
517     }
518     if (AddBoolToJson(context, FIELD_IS_BIND, true) != HC_SUCCESS) {
519         LOGE("add isBind to context fail.");
520         return HC_ERR_JSON_ADD;
521     }
522     if (AddBoolToJson(context, FIELD_IS_CLIENT, false) != HC_SUCCESS) {
523         LOGE("add isClient to context fail.");
524         return HC_ERR_JSON_ADD;
525     }
526     int32_t osAccountId = ANY_OS_ACCOUNT;
527     (void)GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId);
528     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
529     if (osAccountId == INVALID_OS_ACCOUNT) {
530         return HC_ERR_INVALID_PARAMS;
531     }
532     if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
533         LOGE("add osAccountId to context fail.");
534         return HC_ERR_JSON_ADD;
535     }
536     if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
537         LOGE("add requestId to context fail.");
538         return HC_ERR_JSON_ADD;
539     }
540     if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
541         LOGE("add appId to context fail.");
542         return HC_ERR_JSON_ADD;
543     }
544     if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
545         LOGE("add opCode to context fail.");
546         return HC_ERR_JSON_ADD;
547     }
548     int32_t res;
549     if (opCode == MEMBER_INVITE) {
550         res = AddGroupInfoToContextByInput(receivedMsg, context);
551         if (res != HC_SUCCESS) {
552             return res;
553         }
554         return AddDevInfoToContextByInput(context);
555     }
556     res = AddGroupInfoToContextByDb(groupId, context);
557     if (res != HC_SUCCESS) {
558         return res;
559     }
560     return AddDevInfoToContextByDb(groupId, context);
561 }
562 
BuildServerBindContext(int64_t requestId,const char * appId,int32_t opCode,const CJson * receivedMsg,CJson * context)563 static int32_t BuildServerBindContext(int64_t requestId, const char *appId, int32_t opCode,
564     const CJson *receivedMsg, CJson *context)
565 {
566     int32_t res = AddServerReqInfoToContext(requestId, appId, opCode, receivedMsg, context);
567     if (res != HC_SUCCESS) {
568         return res;
569     }
570     int32_t channelType;
571     int64_t channelId = DEFAULT_CHANNEL_ID;
572     if (GetByteFromJson(receivedMsg, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) == HC_SUCCESS) {
573         channelType = SOFT_BUS;
574     } else {
575         channelType = SERVICE_CHANNEL;
576     }
577     return AddChannelInfoToContext(channelType, channelId, context);
578 }
579 
CheckAcceptRequest(const CJson * context)580 static int32_t CheckAcceptRequest(const CJson *context)
581 {
582     uint32_t confirmation = REQUEST_REJECTED;
583     if (GetUnsignedIntFromJson(context, FIELD_CONFIRMATION, &confirmation) != HC_SUCCESS) {
584         LOGE("Failed to get confimation from json!");
585         return HC_ERR_JSON_GET;
586     }
587     if (confirmation == REQUEST_ACCEPTED) {
588         LOGI("The service accepts this request!");
589     } else {
590         LOGE("The service rejects this request!");
591     }
592     return HC_SUCCESS;
593 }
594 
OpenServerBindSession(int64_t requestId,const CJson * receivedMsg)595 static int32_t OpenServerBindSession(int64_t requestId, const CJson *receivedMsg)
596 {
597     const char *appId = GetStringFromJson(receivedMsg, FIELD_APP_ID);
598     if (appId == NULL) {
599         appId = DM_APP_ID;
600         LOGW("use default device manager appId.");
601     }
602     const DeviceAuthCallback *callback = GetGMCallbackByAppId(appId);
603     if (callback == NULL) {
604         LOGE("Failed to find callback by appId! [AppId]: %s", appId);
605         return HC_ERR_CALLBACK_NOT_FOUND;
606     }
607     int32_t opCode;
608     if (GetIntFromJson(receivedMsg, FIELD_GROUP_OP, &opCode) != HC_SUCCESS) {
609         if (GetIntFromJson(receivedMsg, FIELD_OP_CODE, &opCode) != HC_SUCCESS) {
610             opCode = MEMBER_JOIN;
611             LOGW("use default opCode.");
612         }
613     }
614     char *returnDataStr = ProcessRequestCallback(requestId, opCode, NULL, callback);
615     if (returnDataStr == NULL) {
616         LOGE("The OnRequest callback is fail!");
617         return HC_ERR_REQ_REJECTED;
618     }
619     CJson *context = CreateJsonFromString(returnDataStr);
620     FreeJsonString(returnDataStr);
621     if (context == NULL) {
622         LOGE("Failed to create context from string!");
623         return HC_ERR_JSON_FAIL;
624     }
625     int32_t res = CheckAcceptRequest(context);
626     if (res != HC_SUCCESS) {
627         FreeJson(context);
628         return res;
629     }
630     res = BuildServerBindContext(requestId, appId, opCode, receivedMsg, context);
631     if (res != HC_SUCCESS) {
632         FreeJson(context);
633         return res;
634     }
635     SessionInitParams params = { context, *callback };
636     res = OpenDevSession(requestId, appId, &params);
637     FreeJson(context);
638     return res;
639 }
640 
ProcessBindData(int64_t requestId,const uint8_t * data,uint32_t dataLen)641 static int32_t ProcessBindData(int64_t requestId, const uint8_t *data, uint32_t dataLen)
642 {
643     SET_LOG_MODE(TRACE_MODE);
644     SET_TRACE_ID(requestId);
645     if ((data == NULL) || (dataLen == 0) || (dataLen > MAX_DATA_BUFFER_SIZE)) {
646         LOGE("The input data is invalid!");
647         return HC_ERR_INVALID_PARAMS;
648     }
649     LOGI("[Start]: RequestProcessBindData! [ReqId]: %" PRId64, requestId);
650     CJson *receivedMsg = CreateJsonFromString((const char *)data);
651     if (receivedMsg == NULL) {
652         LOGE("Failed to create json from string!");
653         return HC_ERR_JSON_FAIL;
654     }
655     int32_t res;
656     if (!IsSessionExist(requestId)) {
657         res = OpenServerBindSession(requestId, receivedMsg);
658         if (res != HC_SUCCESS) {
659             FreeJson(receivedMsg);
660             return res;
661         }
662     }
663     res = PushProcSessionTask(requestId, receivedMsg);
664     if (res != HC_SUCCESS) {
665         FreeJson(receivedMsg);
666         return res;
667     }
668     return HC_SUCCESS;
669 }
670 
BuildClientAuthContext(int32_t osAccountId,int64_t requestId,const char * appId,CJson * context)671 static int32_t BuildClientAuthContext(int32_t osAccountId, int64_t requestId, const char *appId, CJson *context)
672 {
673     const char *peerUdid = GetPeerUdidFromJson(osAccountId, context);
674     if (peerUdid != NULL) {
675         char *deviceId = NULL;
676         if (DeepCopyString(peerUdid, &deviceId) != HC_SUCCESS) {
677             LOGE("Failed to copy peerUdid!");
678             return HC_ERR_ALLOC_MEMORY;
679         }
680         if (AddStringToJson(context, FIELD_PEER_UDID, deviceId) != HC_SUCCESS) {
681             LOGE("add peerUdid to context fail.");
682             HcFree(deviceId);
683             return HC_ERR_JSON_ADD;
684         }
685         if (AddStringToJson(context, FIELD_PEER_CONN_DEVICE_ID, deviceId) != HC_SUCCESS) {
686             LOGE("add peerConnDeviceId to context fail.");
687             HcFree(deviceId);
688             return HC_ERR_JSON_ADD;
689         }
690         PRINT_SENSITIVE_DATA("PeerUdid", deviceId);
691         HcFree(deviceId);
692     }
693     if (AddBoolToJson(context, FIELD_IS_BIND, false) != HC_SUCCESS) {
694         LOGE("add isBind to context fail.");
695         return HC_ERR_JSON_ADD;
696     }
697     if (AddBoolToJson(context, FIELD_IS_CLIENT, true) != HC_SUCCESS) {
698         LOGE("add isClient to context fail.");
699         return HC_ERR_JSON_ADD;
700     }
701     if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
702         LOGE("add osAccountId to context fail.");
703         return HC_ERR_JSON_ADD;
704     }
705     if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
706         LOGE("add requestId to context fail.");
707         return HC_ERR_JSON_ADD;
708     }
709     if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
710         LOGE("add appId to context fail.");
711         return HC_ERR_JSON_ADD;
712     }
713     if (AddIntToJson(context, FIELD_OPERATION_CODE, AUTH_FORM_ACCOUNT_UNRELATED) != HC_SUCCESS) {
714         LOGE("add opCode to context fail.");
715         return HC_ERR_JSON_ADD;
716     }
717     return AddChannelInfoToContext(SERVICE_CHANNEL, DEFAULT_CHANNEL_ID, context);
718 }
719 
AuthDevice(int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * gaCallback)720 static int32_t AuthDevice(int32_t osAccountId, int64_t authReqId, const char *authParams,
721     const DeviceAuthCallback *gaCallback)
722 {
723     SET_LOG_MODE(TRACE_MODE);
724     SET_TRACE_ID(authReqId);
725     LOGI("Begin AuthDevice. [ReqId]:%" PRId64, authReqId);
726     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
727     if ((authParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT) || (gaCallback == NULL)) {
728         LOGE("The input auth params is invalid!");
729         return HC_ERR_INVALID_PARAMS;
730     }
731     CJson *context = CreateJsonFromString(authParams);
732     if (context == NULL) {
733         LOGE("Failed to create json from string!");
734         return HC_ERR_JSON_FAIL;
735     }
736     const char *appId = GetStringFromJson(context, FIELD_SERVICE_PKG_NAME);
737     if (appId == NULL) {
738         LOGE("get servicePkgName from json fail.");
739         FreeJson(context);
740         return HC_ERR_JSON_GET;
741     }
742     int32_t res = BuildClientAuthContext(osAccountId, authReqId, appId, context);
743     if (res != HC_SUCCESS) {
744         FreeJson(context);
745         return res;
746     }
747     SessionInitParams params = { context, *gaCallback };
748     res = OpenDevSession(authReqId, appId, &params);
749     FreeJson(context);
750     if (res != HC_SUCCESS) {
751         LOGE("OpenDevSession fail. [Res]: %d", res);
752         return res;
753     }
754     return PushStartSessionTask(authReqId);
755 }
756 
AddDeviceIdToJson(CJson * context,const char * peerUdid)757 static int32_t AddDeviceIdToJson(CJson *context, const char *peerUdid)
758 {
759     char *deviceId = NULL;
760     if (DeepCopyString(peerUdid, &deviceId) != HC_SUCCESS) {
761         LOGE("Failed to copy peerUdid!");
762         return HC_ERR_ALLOC_MEMORY;
763     }
764     if (AddStringToJson(context, FIELD_PEER_UDID, deviceId) != HC_SUCCESS) {
765         LOGE("add peerUdid to context fail.");
766         HcFree(deviceId);
767         return HC_ERR_JSON_ADD;
768     }
769     if (AddStringToJson(context, FIELD_PEER_CONN_DEVICE_ID, deviceId) != HC_SUCCESS) {
770         LOGE("add peerConnDeviceId to context fail.");
771         HcFree(deviceId);
772         return HC_ERR_JSON_ADD;
773     }
774     HcFree(deviceId);
775     return HC_SUCCESS;
776 }
777 
BuildServerAuthContext(int64_t requestId,int32_t opCode,const char * appId,CJson * context)778 static int32_t BuildServerAuthContext(int64_t requestId, int32_t opCode, const char *appId, CJson *context)
779 {
780     int32_t osAccountId = ANY_OS_ACCOUNT;
781     (void)GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId);
782     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
783     if (osAccountId == INVALID_OS_ACCOUNT) {
784         return HC_ERR_INVALID_PARAMS;
785     }
786     const char *peerUdid = GetPeerUdidFromJson(osAccountId, context);
787     if (peerUdid == NULL) {
788         LOGE("get peerUdid from json fail.");
789         return HC_ERR_JSON_GET;
790     }
791     PRINT_SENSITIVE_DATA("PeerUdid", peerUdid);
792     if (AddDeviceIdToJson(context, peerUdid) != HC_SUCCESS) {
793         LOGE("add deviceId to context fail.");
794         return HC_ERR_JSON_ADD;
795     }
796     if (AddBoolToJson(context, FIELD_IS_BIND, false) != HC_SUCCESS) {
797         LOGE("add isBind to context fail.");
798         return HC_ERR_JSON_ADD;
799     }
800     if (AddBoolToJson(context, FIELD_IS_CLIENT, false) != HC_SUCCESS) {
801         LOGE("add isClient to context fail.");
802         return HC_ERR_JSON_ADD;
803     }
804     if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
805         LOGE("add operationCode to context fail.");
806         return HC_ERR_JSON_ADD;
807     }
808     if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
809         LOGE("add requestId to context fail.");
810         return HC_ERR_JSON_ADD;
811     }
812     if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
813         LOGE("add appId to context fail.");
814         return HC_ERR_JSON_ADD;
815     }
816     if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
817         LOGE("add opCode to context fail.");
818         return HC_ERR_JSON_ADD;
819     }
820     return AddChannelInfoToContext(SERVICE_CHANNEL, DEFAULT_CHANNEL_ID, context);
821 }
822 
OpenServerAuthSession(int64_t requestId,const CJson * receivedMsg,const DeviceAuthCallback * callback)823 static int32_t OpenServerAuthSession(int64_t requestId, const CJson *receivedMsg, const DeviceAuthCallback *callback)
824 {
825     int32_t opCode = AUTH_FORM_ACCOUNT_UNRELATED;
826     if (GetIntFromJson(receivedMsg, FIELD_AUTH_FORM, &opCode) != HC_SUCCESS) {
827         if (GetIntFromJson(receivedMsg, FIELD_OP_CODE, &opCode) != HC_SUCCESS) {
828             opCode = AUTH_FORM_INVALID_TYPE;
829             LOGW("use default opCode.");
830         }
831     }
832     char *returnDataStr = ProcessRequestCallback(requestId, opCode, NULL, callback);
833     if (returnDataStr == NULL) {
834         LOGE("The OnRequest callback is fail!");
835         return HC_ERR_REQ_REJECTED;
836     }
837     CJson *context = CreateJsonFromString(returnDataStr);
838     FreeJsonString(returnDataStr);
839     if (context == NULL) {
840         LOGE("Failed to create context from string!");
841         return HC_ERR_JSON_FAIL;
842     }
843     const char *appId = GetStringFromJson(context, FIELD_SERVICE_PKG_NAME);
844     if (appId == NULL) {
845         LOGE("get appId from json fail.");
846         FreeJson(context);
847         return HC_ERR_JSON_GET;
848     }
849     int32_t res = CheckAcceptRequest(context);
850     if (res != HC_SUCCESS) {
851         FreeJson(context);
852         return res;
853     }
854     res = BuildServerAuthContext(requestId, opCode, appId, context);
855     if (res != HC_SUCCESS) {
856         FreeJson(context);
857         return res;
858     }
859     SessionInitParams params = { context, *callback };
860     res = OpenDevSession(requestId, appId, &params);
861     FreeJson(context);
862     return res;
863 }
864 
ProcessData(int64_t authReqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * gaCallback)865 static int32_t ProcessData(int64_t authReqId, const uint8_t *data, uint32_t dataLen,
866     const DeviceAuthCallback *gaCallback)
867 {
868     SET_LOG_MODE(TRACE_MODE);
869     SET_TRACE_ID(authReqId);
870     LOGI("[GA] Begin ProcessData. [DataLen]: %u, [ReqId]: %" PRId64, dataLen, authReqId);
871     if ((data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) {
872         LOGE("Invalid input for ProcessData!");
873         return HC_ERR_INVALID_PARAMS;
874     }
875     CJson *receivedMsg = CreateJsonFromString((const char *)data);
876     if (receivedMsg == NULL) {
877         LOGE("Failed to create json from string!");
878         return HC_ERR_JSON_FAIL;
879     }
880     int32_t res;
881     if (!IsSessionExist(authReqId)) {
882         res = OpenServerAuthSession(authReqId, receivedMsg, gaCallback);
883         if (res != HC_SUCCESS) {
884             FreeJson(receivedMsg);
885             return res;
886         }
887     }
888     res = PushProcSessionTask(authReqId, receivedMsg);
889     if (res != HC_SUCCESS) {
890         FreeJson(receivedMsg);
891         return res;
892     }
893     return HC_SUCCESS;
894 }
895 
CancelRequest(int64_t requestId,const char * appId)896 static void CancelRequest(int64_t requestId, const char *appId)
897 {
898     SET_LOG_MODE(TRACE_MODE);
899     SET_TRACE_ID(requestId);
900     if (appId == NULL) {
901         LOGE("Invalid app id!");
902         return;
903     }
904     LOGI("cancel request. [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
905     CancelDevSession(requestId, appId);
906 }
907 
GetRealInfo(int32_t osAccountId,const char * pseudonymId,char ** realInfo)908 static int32_t GetRealInfo(int32_t osAccountId, const char *pseudonymId, char **realInfo)
909 {
910     if (pseudonymId == NULL || realInfo == NULL) {
911         LOGE("Invalid params!");
912         return HC_ERR_INVALID_PARAMS;
913     }
914     PseudonymManager *pseudonymInstance = GetPseudonymInstance();
915     if (pseudonymInstance == NULL) {
916         LOGE("not support privacy enhancement!");
917         return HC_ERR_NOT_SUPPORT;
918     }
919     return pseudonymInstance->getRealInfo(osAccountId, pseudonymId, realInfo);
920 }
921 
GetPseudonymId(int32_t osAccountId,const char * indexKey,char ** pseudonymId)922 static int32_t GetPseudonymId(int32_t osAccountId, const char *indexKey, char **pseudonymId)
923 {
924     if (indexKey == NULL || pseudonymId == NULL) {
925         LOGE("Invalid params!");
926         return HC_ERR_INVALID_PARAMS;
927     }
928     PseudonymManager *pseudonymInstance = GetPseudonymInstance();
929     if (pseudonymInstance == NULL) {
930         LOGE("not support privacy enhancement!");
931         return HC_ERR_NOT_SUPPORT;
932     }
933     return pseudonymInstance->getPseudonymId(osAccountId, indexKey, pseudonymId);
934 }
935 
AllocGmAndGa(void)936 static int32_t AllocGmAndGa(void)
937 {
938     if (g_groupManagerInstance == NULL) {
939         g_groupManagerInstance = (DeviceGroupManager *)HcMalloc(sizeof(DeviceGroupManager), 0);
940         if (g_groupManagerInstance == NULL) {
941             LOGE("Failed to allocate groupManager Instance memory!");
942             return HC_ERR_ALLOC_MEMORY;
943         }
944     }
945     if (g_groupAuthManager == NULL) {
946         g_groupAuthManager = (GroupAuthManager *)HcMalloc(sizeof(GroupAuthManager), 0);
947         if (g_groupAuthManager == NULL) {
948             LOGE("Failed to allocate groupAuth Instance memory!");
949             HcFree(g_groupManagerInstance);
950             g_groupManagerInstance = NULL;
951             return HC_ERR_ALLOC_MEMORY;
952         }
953     }
954     return HC_SUCCESS;
955 }
956 
DestroyGmAndGa(void)957 static void DestroyGmAndGa(void)
958 {
959     if (g_groupAuthManager != NULL) {
960         HcFree(g_groupAuthManager);
961         g_groupAuthManager = NULL;
962     }
963     if (g_groupManagerInstance != NULL) {
964         HcFree(g_groupManagerInstance);
965         g_groupManagerInstance = NULL;
966     }
967 }
968 
InitAllModules(void)969 static int32_t InitAllModules(void)
970 {
971     int32_t res = GetLoaderInstance()->initAlg();
972     if (res != HC_SUCCESS) {
973         LOGE("[End]: [Service]: Failed to init algorithm module!");
974         return res;
975     }
976     res = InitCredMgr();
977     if (res != HC_SUCCESS) {
978         LOGE("[End]: [Service]: Failed to init cred mgr!");
979         return res;
980     }
981     res = InitModules();
982     if (res != HC_SUCCESS) {
983         LOGE("[End]: [Service]: Failed to init all authenticator modules!");
984         goto CLEAN_CRED;
985     }
986     res = InitCallbackManager();
987     if (res != HC_SUCCESS) {
988         LOGE("[End]: [Service]: Failed to init callback manage module!");
989         goto CLEAN_MODULE;
990     }
991     res = InitGroupManager();
992     if (res != HC_SUCCESS) {
993         goto CLEAN_CALLBACK;
994     }
995     res = InitDevSessionManager();
996     if (res != HC_SUCCESS) {
997         goto CLEAN_GROUP_MANAGER;
998     }
999     res = InitTaskManager();
1000     if (res != HC_SUCCESS) {
1001         LOGE("[End]: [Service]: Failed to init worker thread!");
1002         goto CLEAN_ALL;
1003     }
1004     return res;
1005 CLEAN_ALL:
1006     DestroyDevSessionManager();
1007 CLEAN_GROUP_MANAGER:
1008     DestroyGroupManager();
1009 CLEAN_CALLBACK:
1010     DestroyCallbackManager();
1011 CLEAN_MODULE:
1012     DestroyModules();
1013 CLEAN_CRED:
1014     DestroyCredMgr();
1015     return res;
1016 }
1017 
InitPseudonymModule(void)1018 static void InitPseudonymModule(void)
1019 {
1020     PseudonymManager *manager = GetPseudonymInstance();
1021     if (manager == NULL) {
1022         LOGE("Pseudonym manager is null!");
1023         return;
1024     }
1025     manager->loadPseudonymData();
1026 }
1027 
InitDeviceAuthService(void)1028 DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService(void)
1029 {
1030     LOGI("[Service]: Start to init device auth service!");
1031     if (CheckInit() == FINISH_INIT) {
1032         LOGI("[End]: [Service]: Device auth service is running!");
1033         return HC_SUCCESS;
1034     }
1035     int32_t res = AllocGmAndGa();
1036     if (res != HC_SUCCESS) {
1037         return res;
1038     }
1039     res = InitAllModules();
1040     if (res != HC_SUCCESS) {
1041         DestroyGmAndGa();
1042         return res;
1043     }
1044     (void)GenerateDeviceKeyPair();
1045     InitPseudonymModule();
1046     DEV_AUTH_LOAD_PLUGIN();
1047     SetInitStatus();
1048     LOGI("[End]: [Service]: Init device auth service successfully!");
1049     return HC_SUCCESS;
1050 }
1051 
DestroyDeviceAuthService(void)1052 DEVICE_AUTH_API_PUBLIC void DestroyDeviceAuthService(void)
1053 {
1054     LOGI("[Service]: Start to destroy device auth service!");
1055     if (CheckDestroy() == FINISH_DESTROY) {
1056         LOGI("[End]: [Service]: The service has not been initialized!");
1057         return;
1058     }
1059     DestroyTaskManager();
1060     DestroyDevSessionManager();
1061     DestroyGroupManager();
1062     DestroyGmAndGa();
1063     DEV_AUTH_UNLOAD_PLUGIN();
1064     DestroyModules();
1065     DestroyCredMgr();
1066     DestroyChannelManager();
1067     DestroyCallbackManager();
1068     DestroyPseudonymManager();
1069     SetDeInitStatus();
1070     LOGI("[End]: [Service]: Destroy device auth service successfully!");
1071 }
1072 
GetGmInstance(void)1073 DEVICE_AUTH_API_PUBLIC const DeviceGroupManager *GetGmInstance(void)
1074 {
1075     if (g_groupManagerInstance == NULL) {
1076         LOGE("Service not init.");
1077         return NULL;
1078     }
1079 
1080     g_groupManagerInstance->regCallback = RegGroupManagerCallback;
1081     g_groupManagerInstance->unRegCallback = UnRegGroupManagerCallback;
1082     g_groupManagerInstance->regDataChangeListener = RegListenerImpl;
1083     g_groupManagerInstance->unRegDataChangeListener = UnRegListenerImpl;
1084     g_groupManagerInstance->createGroup = CreateGroupImpl;
1085     g_groupManagerInstance->deleteGroup = DeleteGroupImpl;
1086     g_groupManagerInstance->addMemberToGroup = AddMemberToGroup;
1087     g_groupManagerInstance->deleteMemberFromGroup = DeleteMemberFromGroupImpl;
1088     g_groupManagerInstance->addMultiMembersToGroup = AddMultiMembersToGroupImpl;
1089     g_groupManagerInstance->delMultiMembersFromGroup = DelMultiMembersFromGroupImpl;
1090     g_groupManagerInstance->processData = ProcessBindData;
1091     g_groupManagerInstance->getRegisterInfo = GetRegisterInfoImpl;
1092     g_groupManagerInstance->checkAccessToGroup = CheckAccessToGroupImpl;
1093     g_groupManagerInstance->getPkInfoList = GetPkInfoListImpl;
1094     g_groupManagerInstance->getGroupInfoById = GetGroupInfoByIdImpl;
1095     g_groupManagerInstance->getGroupInfo = GetGroupInfoImpl;
1096     g_groupManagerInstance->getJoinedGroups = GetJoinedGroupsImpl;
1097     g_groupManagerInstance->getRelatedGroups = GetRelatedGroupsImpl;
1098     g_groupManagerInstance->getDeviceInfoById = GetDeviceInfoByIdImpl;
1099     g_groupManagerInstance->getTrustedDevices = GetTrustedDevicesImpl;
1100     g_groupManagerInstance->isDeviceInGroup = IsDeviceInGroupImpl;
1101     g_groupManagerInstance->cancelRequest = CancelRequest;
1102     g_groupManagerInstance->destroyInfo = DestroyInfoImpl;
1103     return g_groupManagerInstance;
1104 }
1105 
GetGaInstance(void)1106 DEVICE_AUTH_API_PUBLIC const GroupAuthManager *GetGaInstance(void)
1107 {
1108     if (g_groupAuthManager == NULL) {
1109         LOGE("Service not init.");
1110         return NULL;
1111     }
1112 
1113     g_groupAuthManager->processData = ProcessData;
1114     g_groupAuthManager->authDevice = AuthDevice;
1115     g_groupAuthManager->cancelRequest = CancelRequest;
1116     g_groupAuthManager->getRealInfo = GetRealInfo;
1117     g_groupAuthManager->getPseudonymId = GetPseudonymId;
1118     return g_groupAuthManager;
1119 }
1120