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, &params, &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, &params);
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_BIND, true) != HC_SUCCESS) {
644         LOGE("add isBind to context fail.");
645         return HC_ERR_JSON_ADD;
646     }
647     if (AddBoolToJson(context, FIELD_IS_CLIENT, false) != HC_SUCCESS) {
648         LOGE("add isClient to context fail.");
649         return HC_ERR_JSON_ADD;
650     }
651     if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
652         LOGE("add requestId to context fail.");
653         return HC_ERR_JSON_ADD;
654     }
655     if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
656         LOGE("add appId to context fail.");
657         return HC_ERR_JSON_ADD;
658     }
659     if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
660         LOGE("add opCode to context fail.");
661         return HC_ERR_JSON_ADD;
662     }
663     int32_t res;
664     if (opCode == MEMBER_INVITE) {
665         res = AddGroupInfoToContextByInput(receivedMsg, context);
666         if (res != HC_SUCCESS) {
667             return res;
668         }
669         return AddDevInfoToContextByInput(context);
670     }
671     res = AddGroupInfoToContextByDb(groupId, context);
672     if (res != HC_SUCCESS) {
673         return res;
674     }
675     return AddDevInfoToContextByDb(groupId, context);
676 }
677 
CheckConfirmationExist(const CJson * context)678 static int32_t CheckConfirmationExist(const CJson *context)
679 {
680     uint32_t confirmation = REQUEST_REJECTED;
681     if (GetUnsignedIntFromJson(context, FIELD_CONFIRMATION, &confirmation) != HC_SUCCESS) {
682         LOGE("Failed to get confimation from json!");
683         return HC_ERR_JSON_GET;
684     }
685     if (confirmation == REQUEST_ACCEPTED) {
686         LOGI("The service accepts this request!");
687     } else {
688         LOGW("The service rejects this request!");
689     }
690     return HC_SUCCESS;
691 }
692 
AddOsAccountIdToContextIfValid(CJson * context)693 static int32_t AddOsAccountIdToContextIfValid(CJson *context)
694 {
695     int32_t osAccountId = ANY_OS_ACCOUNT;
696     (void)GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId);
697     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
698     LOGI("[OsAccountId]: %d", osAccountId);
699     if (osAccountId == INVALID_OS_ACCOUNT) {
700         return HC_ERR_INVALID_PARAMS;
701     }
702     if (!IsOsAccountUnlocked(osAccountId)) {
703         LOGE("Os account is not unlocked!");
704         return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
705     }
706     if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
707         LOGE("add operationCode to context fail.");
708         return HC_ERR_JSON_ADD;
709     }
710     return HC_SUCCESS;
711 }
712 
BuildServerBindContext(int64_t requestId,const char * appId,int32_t opCode,const CJson * receivedMsg,CJson * context)713 static int32_t BuildServerBindContext(int64_t requestId, const char *appId, int32_t opCode,
714     const CJson *receivedMsg, CJson *context)
715 {
716     int32_t res = CheckConfirmationExist(context);
717     if (res != HC_SUCCESS) {
718         return res;
719     }
720     res = AddOsAccountIdToContextIfValid(context);
721     if (res != HC_SUCCESS) {
722         return res;
723     }
724     res = AddServerReqInfoToContext(requestId, appId, opCode, receivedMsg, context);
725     if (res != HC_SUCCESS) {
726         return res;
727     }
728     int32_t channelType;
729     int64_t channelId = DEFAULT_CHANNEL_ID;
730     if (GetByteFromJson(receivedMsg, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) == HC_SUCCESS) {
731         channelType = SOFT_BUS;
732     } else {
733         channelType = SERVICE_CHANNEL;
734     }
735     return AddChannelInfoToContext(channelType, channelId, context);
736 }
737 
GetAppIdFromReceivedMsg(const CJson * receivedMsg)738 static const char *GetAppIdFromReceivedMsg(const CJson *receivedMsg)
739 {
740     const char *appId = GetStringFromJson(receivedMsg, FIELD_APP_ID);
741     if (appId == NULL) {
742         LOGW("use default device manager appId.");
743         appId = DM_APP_ID;
744     }
745     return appId;
746 }
747 
OpenServerBindSession(int64_t requestId,const CJson * receivedMsg)748 static int32_t OpenServerBindSession(int64_t requestId, const CJson *receivedMsg)
749 {
750     const char *appId = GetAppIdFromReceivedMsg(receivedMsg);
751     const DeviceAuthCallback *callback = GetGMCallbackByAppId(appId);
752     if (callback == NULL) {
753         LOGE("Failed to find callback by appId! [AppId]: %s", appId);
754         return HC_ERR_CALLBACK_NOT_FOUND;
755     }
756     int32_t opCode;
757     if (GetIntFromJson(receivedMsg, FIELD_GROUP_OP, &opCode) != HC_SUCCESS) {
758         if (GetIntFromJson(receivedMsg, FIELD_OP_CODE, &opCode) != HC_SUCCESS) {
759             opCode = MEMBER_JOIN;
760             LOGW("use default opCode.");
761         }
762     }
763     char *returnDataStr = ProcessRequestCallback(requestId, opCode, NULL, callback);
764     if (returnDataStr == NULL) {
765         LOGE("The OnRequest callback is fail!");
766         return HC_ERR_REQ_REJECTED;
767     }
768     CJson *context = CreateJsonFromString(returnDataStr);
769     FreeJsonString(returnDataStr);
770     if (context == NULL) {
771         LOGE("Failed to create context from string!");
772         return HC_ERR_JSON_FAIL;
773     }
774     int32_t res = BuildServerBindContext(requestId, appId, opCode, receivedMsg, context);
775     if (res != HC_SUCCESS) {
776         FreeJson(context);
777         return res;
778     }
779 #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
780     res = CheckBindParams(context, false);
781     if (res != HC_SUCCESS) {
782         FreeJson(context);
783         return res;
784     }
785 #endif
786     SessionInitParams params = { context, *callback };
787     res = OpenDevSession(requestId, appId, &params);
788     FreeJson(context);
789     return res;
790 }
791 
ProcessBindData(int64_t requestId,const uint8_t * data,uint32_t dataLen)792 static int32_t ProcessBindData(int64_t requestId, const uint8_t *data, uint32_t dataLen)
793 {
794     SET_LOG_MODE(TRACE_MODE);
795     SET_TRACE_ID(requestId);
796     if (!IsSessionExist(requestId)) {
797         ADD_PERFORM_DATA(requestId, true, false, HcGetCurTimeInMillis());
798     } else {
799         UPDATE_PERFORM_DATA_BY_SELF_INDEX(requestId, HcGetCurTimeInMillis());
800     }
801     if ((data == NULL) || (dataLen == 0) || (dataLen > MAX_DATA_BUFFER_SIZE)) {
802         LOGE("The input data is invalid!");
803         return HC_ERR_INVALID_PARAMS;
804     }
805     LOGI("[Start]: RequestProcessBindData! [ReqId]: %" PRId64, requestId);
806     CJson *receivedMsg = CreateJsonFromString((const char *)data);
807     if (receivedMsg == NULL) {
808         LOGE("Failed to create json from string!");
809         return HC_ERR_JSON_FAIL;
810     }
811     int32_t res;
812     if (!IsSessionExist(requestId)) {
813         res = OpenServerBindSession(requestId, receivedMsg);
814         if (res != HC_SUCCESS) {
815             FreeJson(receivedMsg);
816             return res;
817         }
818     }
819     res = PushProcSessionTask(requestId, receivedMsg);
820     if (res != HC_SUCCESS) {
821         FreeJson(receivedMsg);
822         return res;
823     }
824     return HC_SUCCESS;
825 }
826 
BuildClientAuthContext(int32_t osAccountId,int64_t requestId,const char * appId,CJson * context)827 static int32_t BuildClientAuthContext(int32_t osAccountId, int64_t requestId, const char *appId, CJson *context)
828 {
829     const char *peerUdid = GetPeerUdidFromJson(osAccountId, context);
830     if (peerUdid != NULL) {
831         char *deviceId = NULL;
832         if (DeepCopyString(peerUdid, &deviceId) != HC_SUCCESS) {
833             LOGE("Failed to copy peerUdid!");
834             return HC_ERR_ALLOC_MEMORY;
835         }
836         if (AddStringToJson(context, FIELD_PEER_UDID, deviceId) != HC_SUCCESS) {
837             LOGE("add peerUdid to context fail.");
838             HcFree(deviceId);
839             return HC_ERR_JSON_ADD;
840         }
841         if (AddStringToJson(context, FIELD_PEER_CONN_DEVICE_ID, deviceId) != HC_SUCCESS) {
842             LOGE("add peerConnDeviceId to context fail.");
843             HcFree(deviceId);
844             return HC_ERR_JSON_ADD;
845         }
846         PRINT_SENSITIVE_DATA("PeerUdid", deviceId);
847         HcFree(deviceId);
848     }
849     if (AddBoolToJson(context, FIELD_IS_BIND, false) != HC_SUCCESS) {
850         LOGE("add isBind to context fail.");
851         return HC_ERR_JSON_ADD;
852     }
853     if (AddBoolToJson(context, FIELD_IS_CLIENT, true) != HC_SUCCESS) {
854         LOGE("add isClient to context fail.");
855         return HC_ERR_JSON_ADD;
856     }
857     if (AddIntToJson(context, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
858         LOGE("add osAccountId to context fail.");
859         return HC_ERR_JSON_ADD;
860     }
861     if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
862         LOGE("add requestId to context fail.");
863         return HC_ERR_JSON_ADD;
864     }
865     if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
866         LOGE("add appId to context fail.");
867         return HC_ERR_JSON_ADD;
868     }
869     if (AddIntToJson(context, FIELD_OPERATION_CODE, AUTH_FORM_ACCOUNT_UNRELATED) != HC_SUCCESS) {
870         LOGE("add opCode to context fail.");
871         return HC_ERR_JSON_ADD;
872     }
873     return AddChannelInfoToContext(SERVICE_CHANNEL, DEFAULT_CHANNEL_ID, context);
874 }
875 
BuildP2PBindContext(CJson * context)876 static int32_t BuildP2PBindContext(CJson *context)
877 {
878     int32_t acquireType = -1;
879     if (GetIntFromJson(context, FIELD_ACQURIED_TYPE, &acquireType) != HC_SUCCESS) {
880         LOGE("Failed to get acquireType from reqJsonStr!");
881         return HC_ERR_JSON_FAIL;
882     }
883     if ((acquireType == P2P_BIND) && AddBoolToJson(context, FIELD_IS_DIRECT_AUTH, true) != HC_SUCCESS) {
884         LOGE("add isDirectAuth to context fail.");
885         return HC_ERR_JSON_ADD;
886     }
887     if (AddIntToJson(context, FIELD_OPERATION_CODE, acquireType) != HC_SUCCESS) {
888         LOGE("add opCode to context fail.");
889         return HC_ERR_JSON_ADD;
890     }
891     const char *serviceType = GetStringFromJson(context, FIELD_SERVICE_TYPE);
892     if (serviceType == NULL) {
893         if ((acquireType == P2P_BIND) &&
894             AddStringToJson(context, FIELD_SERVICE_TYPE, DEFAULT_SERVICE_TYPE) != HC_SUCCESS) {
895             LOGE("add serviceType to context fail.");
896             return HC_ERR_JSON_ADD;
897         }
898     }
899     return HC_SUCCESS;
900 }
901 
AuthDevice(int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * gaCallback)902 static int32_t AuthDevice(int32_t osAccountId, int64_t authReqId, const char *authParams,
903     const DeviceAuthCallback *gaCallback)
904 {
905     SET_LOG_MODE(TRACE_MODE);
906     SET_TRACE_ID(authReqId);
907     ADD_PERFORM_DATA(authReqId, false, true, HcGetCurTimeInMillis());
908     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
909     LOGI("Begin AuthDevice. [ReqId]: %" PRId64 ", [OsAccountId]: %d", authReqId, osAccountId);
910     if ((authParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT) || (gaCallback == NULL)) {
911         LOGE("The input auth params is invalid!");
912         return HC_ERR_INVALID_PARAMS;
913     }
914     if (!IsOsAccountUnlocked(osAccountId)) {
915         LOGE("Os account is not unlocked!");
916         return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
917     }
918     CJson *context = CreateJsonFromString(authParams);
919     if (context == NULL) {
920         LOGE("Failed to create json from string!");
921         return HC_ERR_JSON_FAIL;
922     }
923     const char *appId = GetStringFromJson(context, FIELD_SERVICE_PKG_NAME);
924     if (appId == NULL) {
925         LOGE("get servicePkgName from json fail.");
926         FreeJson(context);
927         return HC_ERR_JSON_GET;
928     }
929     int32_t res = BuildClientAuthContext(osAccountId, authReqId, appId, context);
930     if (res != HC_SUCCESS) {
931         FreeJson(context);
932         return res;
933     }
934     DEV_AUTH_REPORT_UE_CALL_EVENT_BY_PARAMS(osAccountId, authParams, appId, AUTH_DEV_EVENT);
935     SessionInitParams params = { context, *gaCallback };
936     res = OpenDevSession(authReqId, appId, &params);
937     FreeJson(context);
938     if (res != HC_SUCCESS) {
939         LOGE("OpenDevSession fail. [Res]: %d", res);
940         return res;
941     }
942     return PushStartSessionTask(authReqId);
943 }
944 
AddDeviceIdToJson(CJson * context,const char * peerUdid)945 static int32_t AddDeviceIdToJson(CJson *context, const char *peerUdid)
946 {
947     char *deviceId = NULL;
948     if (DeepCopyString(peerUdid, &deviceId) != HC_SUCCESS) {
949         LOGE("Failed to copy peerUdid!");
950         return HC_ERR_ALLOC_MEMORY;
951     }
952     if (AddStringToJson(context, FIELD_PEER_UDID, deviceId) != HC_SUCCESS) {
953         LOGE("add peerUdid to context fail.");
954         HcFree(deviceId);
955         return HC_ERR_JSON_ADD;
956     }
957     if (AddStringToJson(context, FIELD_PEER_CONN_DEVICE_ID, deviceId) != HC_SUCCESS) {
958         LOGE("add peerConnDeviceId to context fail.");
959         HcFree(deviceId);
960         return HC_ERR_JSON_ADD;
961     }
962     HcFree(deviceId);
963     return HC_SUCCESS;
964 }
965 
BuildServerAuthContext(int64_t requestId,int32_t opCode,const char * appId,CJson * context)966 static int32_t BuildServerAuthContext(int64_t requestId, int32_t opCode, const char *appId, CJson *context)
967 {
968     int32_t res = CheckConfirmationExist(context);
969     if (res != HC_SUCCESS) {
970         return res;
971     }
972     res = AddOsAccountIdToContextIfValid(context);
973     if (res != HC_SUCCESS) {
974         return res;
975     }
976     int32_t osAccountId = ANY_OS_ACCOUNT;
977     (void)GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId);
978     const char *peerUdid = GetPeerUdidFromJson(osAccountId, context);
979     if (peerUdid == NULL) {
980         return HC_ERR_JSON_GET;
981     }
982     PRINT_SENSITIVE_DATA("PeerUdid", peerUdid);
983     if (AddDeviceIdToJson(context, peerUdid) != HC_SUCCESS) {
984         LOGE("add deviceId to context fail.");
985         return HC_ERR_JSON_ADD;
986     }
987     if (AddBoolToJson(context, FIELD_IS_BIND, false) != HC_SUCCESS) {
988         LOGE("add isBind to context fail.");
989         return HC_ERR_JSON_ADD;
990     }
991     if (AddBoolToJson(context, FIELD_IS_CLIENT, false) != HC_SUCCESS) {
992         LOGE("add isClient to context fail.");
993         return HC_ERR_JSON_ADD;
994     }
995     if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
996         LOGE("add requestId to context fail.");
997         return HC_ERR_JSON_ADD;
998     }
999     if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
1000         LOGE("add appId to context fail.");
1001         return HC_ERR_JSON_ADD;
1002     }
1003     if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
1004         LOGE("add opCode to context fail.");
1005         return HC_ERR_JSON_ADD;
1006     }
1007     return AddChannelInfoToContext(SERVICE_CHANNEL, DEFAULT_CHANNEL_ID, context);
1008 }
1009 
BuildServerP2PAuthContext(int64_t requestId,int32_t opCode,const char * appId,CJson * context)1010 static int32_t BuildServerP2PAuthContext(int64_t requestId, int32_t opCode, const char *appId, CJson *context)
1011 {
1012     int32_t res = CheckConfirmationExist(context);
1013     if (res != HC_SUCCESS) {
1014         return res;
1015     }
1016     res = AddOsAccountIdToContextIfValid(context);
1017     if (res != HC_SUCCESS) {
1018         return res;
1019     }
1020     const char *peerUdid = GetStringFromJson(context, FIELD_PEER_CONN_DEVICE_ID);
1021     const char *pinCode = GetStringFromJson(context, FIELD_PIN_CODE);
1022     if (peerUdid == NULL && pinCode == NULL) {
1023         LOGE("need peerConnDeviceId or pinCode!");
1024         return HC_ERR_JSON_GET;
1025     }
1026     if (peerUdid != NULL) {
1027         PRINT_SENSITIVE_DATA("PeerUdid", peerUdid);
1028         if (AddDeviceIdToJson(context, peerUdid) != HC_SUCCESS) {
1029             LOGE("add deviceId to context fail.");
1030             return HC_ERR_JSON_ADD;
1031         }
1032     }
1033     if (AddBoolToJson(context, FIELD_IS_BIND, false) != HC_SUCCESS) {
1034         LOGE("add isBind to context fail.");
1035         return HC_ERR_JSON_ADD;
1036     }
1037     if (AddBoolToJson(context, FIELD_IS_CLIENT, false) != HC_SUCCESS) {
1038         LOGE("add isClient to context fail.");
1039         return HC_ERR_JSON_ADD;
1040     }
1041     if (AddInt64StringToJson(context, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
1042         LOGE("add requestId to context fail.");
1043         return HC_ERR_JSON_ADD;
1044     }
1045     if (AddStringToJson(context, FIELD_APP_ID, appId) != HC_SUCCESS) {
1046         LOGE("add appId to context fail.");
1047         return HC_ERR_JSON_ADD;
1048     }
1049     if (AddIntToJson(context, FIELD_OPERATION_CODE, opCode) != HC_SUCCESS) {
1050         LOGE("add opCode to context fail.");
1051         return HC_ERR_JSON_ADD;
1052     }
1053     return AddChannelInfoToContext(SERVICE_CHANNEL, DEFAULT_CHANNEL_ID, context);
1054 }
1055 
OpenServerAuthSession(int64_t requestId,const CJson * receivedMsg,const DeviceAuthCallback * callback)1056 static int32_t OpenServerAuthSession(int64_t requestId, const CJson *receivedMsg, const DeviceAuthCallback *callback)
1057 {
1058     int32_t opCode = AUTH_FORM_ACCOUNT_UNRELATED;
1059     if (GetIntFromJson(receivedMsg, FIELD_AUTH_FORM, &opCode) != HC_SUCCESS) {
1060         if (GetIntFromJson(receivedMsg, FIELD_OP_CODE, &opCode) != HC_SUCCESS) {
1061             opCode = AUTH_FORM_INVALID_TYPE;
1062             LOGW("use default opCode.");
1063         }
1064     }
1065     char *returnDataStr = ProcessRequestCallback(requestId, opCode, NULL, callback);
1066     if (returnDataStr == NULL) {
1067         LOGE("The OnRequest callback is fail!");
1068         return HC_ERR_REQ_REJECTED;
1069     }
1070     CJson *context = CreateJsonFromString(returnDataStr);
1071     FreeJsonString(returnDataStr);
1072     if (context == NULL) {
1073         LOGE("Failed to create context from string!");
1074         return HC_ERR_JSON_FAIL;
1075     }
1076     const char *appId = GetStringFromJson(context, FIELD_SERVICE_PKG_NAME);
1077     if (appId == NULL) {
1078         LOGE("get appId from json fail.");
1079         FreeJson(context);
1080         return HC_ERR_JSON_GET;
1081     }
1082     int32_t res = BuildServerAuthContext(requestId, opCode, appId, context);
1083     if (res != HC_SUCCESS) {
1084         FreeJson(context);
1085         return res;
1086     }
1087     SessionInitParams params = { context, *callback };
1088     res = OpenDevSession(requestId, appId, &params);
1089     FreeJson(context);
1090     return res;
1091 }
1092 
OpenServerAuthSessionForP2P(int64_t requestId,const CJson * receivedMsg,const DeviceAuthCallback * callback)1093 static int32_t OpenServerAuthSessionForP2P(
1094     int64_t requestId, const CJson *receivedMsg, const DeviceAuthCallback *callback)
1095 {
1096     int32_t opCode = P2P_BIND;
1097     if (GetIntFromJson(receivedMsg, FIELD_OP_CODE, &opCode) != HC_SUCCESS) {
1098         opCode = P2P_BIND;
1099         LOGW("use default opCode.");
1100     }
1101     char *returnDataStr = ProcessRequestCallback(requestId, opCode, NULL, callback);
1102     if (returnDataStr == NULL) {
1103         LOGE("The OnRequest callback is fail!");
1104         return HC_ERR_REQ_REJECTED;
1105     }
1106     CJson *context = CreateJsonFromString(returnDataStr);
1107     FreeJsonString(returnDataStr);
1108     if (context == NULL) {
1109         LOGE("Failed to create context from string!");
1110         return HC_ERR_JSON_FAIL;
1111     }
1112     if (AddBoolToJson(context, FIELD_IS_DIRECT_AUTH, true) != HC_SUCCESS) {
1113         LOGE("Failed to add isDirectAuth to context!");
1114         FreeJson(context);
1115         return HC_ERR_JSON_ADD;
1116     }
1117     const char *pkgName = GetStringFromJson(context, FIELD_SERVICE_PKG_NAME);
1118     if (pkgName == NULL && AddStringToJson(context, FIELD_SERVICE_PKG_NAME, DEFAULT_PACKAGE_NAME) != HC_SUCCESS) {
1119         LOGE("Failed to add default package name to context!");
1120         FreeJson(context);
1121         return HC_ERR_JSON_ADD;
1122     }
1123     const char *serviceType = GetStringFromJson(context, FIELD_SERVICE_TYPE);
1124     if (serviceType == NULL && AddStringToJson(context, FIELD_SERVICE_TYPE, DEFAULT_SERVICE_TYPE) != HC_SUCCESS) {
1125         LOGE("Failed to add default package name to context!");
1126         FreeJson(context);
1127         return HC_ERR_JSON_ADD;
1128     }
1129     const char *appId = pkgName != NULL ? pkgName : DEFAULT_PACKAGE_NAME;
1130     int32_t res = BuildServerP2PAuthContext(requestId, opCode, appId, context);
1131     if (res != HC_SUCCESS) {
1132         FreeJson(context);
1133         return res;
1134     }
1135     SessionInitParams params = { context, *callback };
1136     res = OpenDevSession(requestId, appId, &params);
1137     FreeJson(context);
1138     return res;
1139 }
1140 
ProcessData(int64_t authReqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * gaCallback)1141 static int32_t ProcessData(int64_t authReqId, const uint8_t *data, uint32_t dataLen,
1142     const DeviceAuthCallback *gaCallback)
1143 {
1144     SET_LOG_MODE(TRACE_MODE);
1145     SET_TRACE_ID(authReqId);
1146     if (!IsSessionExist(authReqId)) {
1147         ADD_PERFORM_DATA(authReqId, false, false, HcGetCurTimeInMillis());
1148     } else {
1149         UPDATE_PERFORM_DATA_BY_SELF_INDEX(authReqId, HcGetCurTimeInMillis());
1150     }
1151     LOGI("[GA] Begin ProcessData. [DataLen]: %u, [ReqId]: %" PRId64, dataLen, authReqId);
1152     if ((data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) {
1153         LOGE("Invalid input for ProcessData!");
1154         return HC_ERR_INVALID_PARAMS;
1155     }
1156     CJson *receivedMsg = CreateJsonFromString((const char *)data);
1157     if (receivedMsg == NULL) {
1158         LOGE("Failed to create json from string!");
1159         return HC_ERR_JSON_FAIL;
1160     }
1161     int32_t res;
1162     if (!IsSessionExist(authReqId)) {
1163         res = OpenServerAuthSession(authReqId, receivedMsg, gaCallback);
1164         if (res != HC_SUCCESS) {
1165             FreeJson(receivedMsg);
1166             return res;
1167         }
1168     }
1169     if (HasAccountAuthPlugin() == HC_SUCCESS) {
1170         res = AddOriginDataForPlugin(receivedMsg, data);
1171         if (res != HC_SUCCESS) {
1172             FreeJson(receivedMsg);
1173             return res;
1174         }
1175     }
1176     res = PushProcSessionTask(authReqId, receivedMsg);
1177     if (res != HC_SUCCESS) {
1178         FreeJson(receivedMsg);
1179         return res;
1180     }
1181     return HC_SUCCESS;
1182 }
1183 
CancelRequest(int64_t requestId,const char * appId)1184 static void CancelRequest(int64_t requestId, const char *appId)
1185 {
1186     SET_LOG_MODE(TRACE_MODE);
1187     SET_TRACE_ID(requestId);
1188     DEV_AUTH_REPORT_UE_CALL_EVENT_BY_PARAMS(DEFAULT_OS_ACCOUNT, NULL, appId, CANCEL_REQUEST_EVENT);
1189     if (appId == NULL) {
1190         LOGE("Invalid app id!");
1191         return;
1192     }
1193     LOGI("cancel request. [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
1194     CancelDevSession(requestId, appId);
1195 }
1196 
GetRealInfo(int32_t osAccountId,const char * pseudonymId,char ** realInfo)1197 static int32_t GetRealInfo(int32_t osAccountId, const char *pseudonymId, char **realInfo)
1198 {
1199     if (pseudonymId == NULL || realInfo == NULL) {
1200         LOGE("Invalid params!");
1201         return HC_ERR_INVALID_PARAMS;
1202     }
1203     PseudonymManager *pseudonymInstance = GetPseudonymInstance();
1204     if (pseudonymInstance == NULL) {
1205         LOGE("not support privacy enhancement!");
1206         return HC_ERR_NOT_SUPPORT;
1207     }
1208     return pseudonymInstance->getRealInfo(osAccountId, pseudonymId, realInfo);
1209 }
1210 
GetPseudonymId(int32_t osAccountId,const char * indexKey,char ** pseudonymId)1211 static int32_t GetPseudonymId(int32_t osAccountId, const char *indexKey, char **pseudonymId)
1212 {
1213     if (indexKey == NULL || pseudonymId == NULL) {
1214         LOGE("Invalid params!");
1215         return HC_ERR_INVALID_PARAMS;
1216     }
1217     PseudonymManager *pseudonymInstance = GetPseudonymInstance();
1218     if (pseudonymInstance == NULL) {
1219         LOGE("not support privacy enhancement!");
1220         return HC_ERR_NOT_SUPPORT;
1221     }
1222     return pseudonymInstance->getPseudonymId(osAccountId, indexKey, pseudonymId);
1223 }
1224 
ProcessCredential(int32_t operationCode,const char * reqJsonStr,char ** returnData)1225 DEVICE_AUTH_API_PUBLIC int32_t ProcessCredential(int32_t operationCode, const char *reqJsonStr, char **returnData)
1226 {
1227     if (reqJsonStr == NULL || returnData == NULL) {
1228         LOGE("Invalid params!");
1229         return HC_ERR_INVALID_PARAMS;
1230     }
1231 
1232     const CredentialOperator *credOperator = GetCredentialOperator();
1233     if (credOperator == NULL) {
1234         LOGE("credOperator is null!");
1235         return HC_ERR_NOT_SUPPORT;
1236     }
1237 
1238     int32_t res = HC_ERR_UNSUPPORTED_OPCODE;
1239     switch (operationCode) {
1240         case CRED_OP_QUERY:
1241             res = credOperator->queryCredential(reqJsonStr, returnData);
1242             break;
1243         case CRED_OP_CREATE:
1244             res = credOperator->genarateCredential(reqJsonStr, returnData);
1245             break;
1246         case CRED_OP_IMPORT:
1247             res = credOperator->importCredential(reqJsonStr, returnData);
1248             break;
1249         case CRED_OP_DELETE:
1250             res = credOperator->deleteCredential(reqJsonStr, returnData);
1251             break;
1252         default:
1253             LOGE("invalid opCode: %d", operationCode);
1254             break;
1255     }
1256 
1257     return res;
1258 }
1259 
ProcessAuthDevice(int64_t authReqId,const char * authParams,const DeviceAuthCallback * callback)1260 DEVICE_AUTH_API_PUBLIC int32_t ProcessAuthDevice(
1261     int64_t authReqId, const char *authParams, const DeviceAuthCallback *callback)
1262 {
1263     SET_LOG_MODE(TRACE_MODE);
1264     SET_TRACE_ID(authReqId);
1265     LOGI("[DA] Begin ProcessAuthDevice [ReqId]: %" PRId64, authReqId);
1266     if (authParams == NULL || HcStrlen(authParams) > MAX_DATA_BUFFER_SIZE) {
1267         LOGE("Invalid input for ProcessData!");
1268         return HC_ERR_INVALID_PARAMS;
1269     }
1270     CJson *json = CreateJsonFromString(authParams);
1271     if (json == NULL) {
1272         LOGE("Failed to create json from string!");
1273         return HC_ERR_JSON_FAIL;
1274     }
1275     const char *data = GetStringFromJson(json, "data");
1276     if (data == NULL) {
1277         LOGE("Failed to get received data from parameter!");
1278         FreeJson(json);
1279         return HC_ERR_INVALID_PARAMS;
1280     }
1281     CJson *receivedMsg = CreateJsonFromString(data);
1282     FreeJson(json);
1283     if (receivedMsg == NULL) {
1284         LOGE("Failed to create json from string!");
1285         return HC_ERR_JSON_FAIL;
1286     }
1287     int32_t res;
1288     if (!IsSessionExist(authReqId)) {
1289         res = OpenServerAuthSessionForP2P(authReqId, receivedMsg, callback);
1290         if (res != HC_SUCCESS) {
1291             FreeJson(receivedMsg);
1292             return res;
1293         }
1294     }
1295     res = PushProcSessionTask(authReqId, receivedMsg);
1296     if (res != HC_SUCCESS) {
1297         FreeJson(receivedMsg);
1298         return res;
1299     }
1300     return HC_SUCCESS;
1301 }
1302 
StartAuthDevice(int64_t authReqId,const char * authParams,const DeviceAuthCallback * callback)1303 DEVICE_AUTH_API_PUBLIC int32_t StartAuthDevice(
1304     int64_t authReqId, const char *authParams, const DeviceAuthCallback *callback)
1305 {
1306     SET_LOG_MODE(TRACE_MODE);
1307     SET_TRACE_ID(authReqId);
1308     LOGI("StartAuthDevice. [ReqId]:%" PRId64, authReqId);
1309 
1310     if ((authParams == NULL) || (callback == NULL) || HcStrlen(authParams) > MAX_DATA_BUFFER_SIZE) {
1311         LOGE("The input auth params is invalid!");
1312         return HC_ERR_INVALID_PARAMS;
1313     }
1314     CJson *context = CreateJsonFromString(authParams);
1315     if (context == NULL) {
1316         LOGE("Failed to create json from string!");
1317         return HC_ERR_JSON_FAIL;
1318     }
1319     int32_t osAccountId = INVALID_OS_ACCOUNT;
1320     if (GetIntFromJson(context, FIELD_OS_ACCOUNT_ID, &osAccountId) != HC_SUCCESS) {
1321         LOGE("Failed to get osAccountId from json!");
1322         FreeJson(context);
1323         return HC_ERR_JSON_FAIL;
1324     }
1325 
1326     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
1327     if (osAccountId == INVALID_OS_ACCOUNT) {
1328         FreeJson(context);
1329         return HC_ERR_INVALID_PARAMS;
1330     }
1331     int32_t res = BuildClientAuthContext(osAccountId, authReqId, DEFAULT_PACKAGE_NAME, context);
1332     if (res != HC_SUCCESS) {
1333         FreeJson(context);
1334         return res;
1335     }
1336     res = BuildP2PBindContext(context);
1337     if (res != HC_SUCCESS) {
1338         FreeJson(context);
1339         return res;
1340     }
1341     SessionInitParams params = { context, *callback };
1342     res = OpenDevSession(authReqId, DEFAULT_PACKAGE_NAME, &params);
1343     FreeJson(context);
1344     if (res != HC_SUCCESS) {
1345         LOGE("OpenDevSession fail. [Res]: %d", res);
1346         return res;
1347     }
1348     return PushStartSessionTask(authReqId);
1349 }
1350 
CancelAuthRequest(int64_t requestId,const char * authParams)1351 DEVICE_AUTH_API_PUBLIC int32_t CancelAuthRequest(int64_t requestId, const char *authParams)
1352 {
1353     SET_LOG_MODE(TRACE_MODE);
1354     SET_TRACE_ID(requestId);
1355     if (authParams == NULL || HcStrlen(authParams) > MAX_DATA_BUFFER_SIZE) {
1356         LOGE("Invalid authParams!");
1357         return HC_ERR_INVALID_PARAMS;
1358     }
1359     LOGI("cancel request. [ReqId]: %" PRId64, requestId);
1360     CancelDevSession(requestId, DEFAULT_PACKAGE_NAME);
1361     return HC_SUCCESS;
1362 }
1363 
AllocGmAndGa(void)1364 static int32_t AllocGmAndGa(void)
1365 {
1366     if (g_groupManagerInstance == NULL) {
1367         g_groupManagerInstance = (DeviceGroupManager *)HcMalloc(sizeof(DeviceGroupManager), 0);
1368         if (g_groupManagerInstance == NULL) {
1369             LOGE("Failed to allocate groupManager Instance memory!");
1370             return HC_ERR_ALLOC_MEMORY;
1371         }
1372     }
1373     if (g_groupAuthManager == NULL) {
1374         g_groupAuthManager = (GroupAuthManager *)HcMalloc(sizeof(GroupAuthManager), 0);
1375         if (g_groupAuthManager == NULL) {
1376             LOGE("Failed to allocate groupAuth Instance memory!");
1377             HcFree(g_groupManagerInstance);
1378             g_groupManagerInstance = NULL;
1379             return HC_ERR_ALLOC_MEMORY;
1380         }
1381     }
1382     return HC_SUCCESS;
1383 }
1384 
DestroyGmAndGa(void)1385 static void DestroyGmAndGa(void)
1386 {
1387     if (g_groupAuthManager != NULL) {
1388         HcFree(g_groupAuthManager);
1389         g_groupAuthManager = NULL;
1390     }
1391     if (g_groupManagerInstance != NULL) {
1392         HcFree(g_groupManagerInstance);
1393         g_groupManagerInstance = NULL;
1394     }
1395 }
1396 
InitAllModules(void)1397 static int32_t InitAllModules(void)
1398 {
1399     int32_t res = GetLoaderInstance()->initAlg();
1400     if (res != HC_SUCCESS) {
1401         LOGE("[End]: [Service]: Failed to init algorithm module!");
1402         return res;
1403     }
1404     res = InitCredMgr();
1405     if (res != HC_SUCCESS) {
1406         LOGE("[End]: [Service]: Failed to init cred mgr!");
1407         return res;
1408     }
1409     res = InitModules();
1410     if (res != HC_SUCCESS) {
1411         LOGE("[End]: [Service]: Failed to init all authenticator modules!");
1412         goto CLEAN_CRED;
1413     }
1414     res = InitCallbackManager();
1415     if (res != HC_SUCCESS) {
1416         LOGE("[End]: [Service]: Failed to init callback manage module!");
1417         goto CLEAN_MODULE;
1418     }
1419     res = InitGroupManager();
1420     if (res != HC_SUCCESS) {
1421         goto CLEAN_CALLBACK;
1422     }
1423     res = InitDevSessionManager();
1424     if (res != HC_SUCCESS) {
1425         goto CLEAN_GROUP_MANAGER;
1426     }
1427     (void)InitGroupAuthManager();
1428     res = InitTaskManager();
1429     if (res != HC_SUCCESS) {
1430         LOGE("[End]: [Service]: Failed to init worker thread!");
1431         goto CLEAN_ALL;
1432     }
1433     return res;
1434 CLEAN_ALL:
1435     DestroyDevSessionManager();
1436 CLEAN_GROUP_MANAGER:
1437     DestroyGroupManager();
1438 CLEAN_CALLBACK:
1439     DestroyCallbackManager();
1440 CLEAN_MODULE:
1441     DestroyModules();
1442 CLEAN_CRED:
1443     DestroyCredMgr();
1444     return res;
1445 }
1446 
InitPseudonymModule(void)1447 static void InitPseudonymModule(void)
1448 {
1449     PseudonymManager *manager = GetPseudonymInstance();
1450     if (manager == NULL) {
1451         LOGE("Pseudonym manager is null!");
1452         return;
1453     }
1454     manager->loadPseudonymData();
1455 }
1456 
DoOnChannelOpened(HcTaskBase * baseTask)1457 static void DoOnChannelOpened(HcTaskBase *baseTask)
1458 {
1459     if (baseTask == NULL) {
1460         LOGE("The input task is NULL!");
1461         return;
1462     }
1463     SoftBusTask *task = (SoftBusTask *)baseTask;
1464     SET_LOG_MODE(TRACE_MODE);
1465     SET_TRACE_ID(task->requestId);
1466     LOGI("[Start]: DoOnChannelOpened!");
1467     int32_t res = StartDevSession(task->requestId);
1468     if (res != HC_SUCCESS) {
1469         LOGE("start session fail.[Res]: %d", res);
1470         CloseDevSession(task->requestId);
1471     }
1472 }
1473 
InitSoftBusTask(SoftBusTask * task,int64_t requestId)1474 static void InitSoftBusTask(SoftBusTask *task, int64_t requestId)
1475 {
1476     task->base.doAction = DoOnChannelOpened;
1477     task->base.destroy = NULL;
1478     task->requestId = requestId;
1479 }
1480 
OnChannelOpenedCb(int64_t requestId,int result)1481 static int OnChannelOpenedCb(int64_t requestId, int result)
1482 {
1483     if (result != HC_SUCCESS) {
1484         LOGE("[SoftBus][Out]: Failed to open channel! res: %d", result);
1485         CloseDevSession(requestId);
1486         return HC_ERR_SOFT_BUS;
1487     }
1488     LOGI("[Start]: OnChannelOpened! [ReqId]: %" PRId64, requestId);
1489     SoftBusTask *task = (SoftBusTask *)HcMalloc(sizeof(SoftBusTask), 0);
1490     if (task == NULL) {
1491         LOGE("Failed to allocate task memory!");
1492         CloseDevSession(requestId);
1493         return HC_ERR_ALLOC_MEMORY;
1494     }
1495     InitSoftBusTask(task, requestId);
1496     if (PushTask((HcTaskBase *)task) != HC_SUCCESS) {
1497         HcFree(task);
1498         CloseDevSession(requestId);
1499         return HC_ERR_INIT_TASK_FAIL;
1500     }
1501     LOGI("[End]: OnChannelOpened!");
1502     return HC_SUCCESS;
1503 }
1504 
OnChannelClosedCb(void)1505 static void OnChannelClosedCb(void)
1506 {
1507     return;
1508 }
1509 
OnBytesReceivedCb(int64_t requestId,uint8_t * data,uint32_t dataLen)1510 static void OnBytesReceivedCb(int64_t requestId, uint8_t *data, uint32_t dataLen)
1511 {
1512     if ((data == NULL) || (dataLen == 0) || (dataLen > MAX_DATA_BUFFER_SIZE)) {
1513         LOGE("Invalid input params!");
1514         return;
1515     }
1516     (void)ProcessBindData(requestId, data, dataLen);
1517 }
1518 
RegCallback(const char * appId,const DeviceAuthCallback * callback)1519 static int32_t RegCallback(const char *appId, const DeviceAuthCallback *callback)
1520 {
1521     SET_LOG_MODE(NORMAL_MODE);
1522     if ((appId == NULL) || (callback == NULL)) {
1523         LOGE("The input parameters contains NULL value!");
1524         return HC_ERR_INVALID_PARAMS;
1525     }
1526     ChannelProxy proxy = {
1527         .onChannelOpened = OnChannelOpenedCb,
1528         .onChannelClosed = OnChannelClosedCb,
1529         .onBytesReceived = OnBytesReceivedCb
1530     };
1531     int32_t res = InitChannelManager(&proxy);
1532     if (res != HC_SUCCESS) {
1533         LOGE("[End]: [Service]: Failed to init channel manage module!");
1534         return res;
1535     }
1536     return RegGroupManagerCallback(appId, callback);
1537 }
1538 
InitDeviceAuthService(void)1539 DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService(void)
1540 {
1541     LOGI("[Service]: Start to init device auth service!");
1542     if (CheckInit() == FINISH_INIT) {
1543         LOGI("[End]: [Service]: Device auth service is running!");
1544         return HC_SUCCESS;
1545     }
1546     int32_t res = AllocGmAndGa();
1547     if (res != HC_SUCCESS) {
1548         return res;
1549     }
1550     InitOsAccountAdapter();
1551     res = InitAllModules();
1552     if (res != HC_SUCCESS) {
1553         DestroyGmAndGa();
1554         return res;
1555     }
1556     INIT_PERFORMANCE_DUMPER();
1557     InitPseudonymModule();
1558     DEV_AUTH_LOAD_PLUGIN();
1559     SetInitStatus();
1560     LOGI("[End]: [Service]: Init device auth service successfully!");
1561     return HC_SUCCESS;
1562 }
1563 
DestroyDeviceAuthService(void)1564 DEVICE_AUTH_API_PUBLIC void DestroyDeviceAuthService(void)
1565 {
1566     LOGI("[Service]: Start to destroy device auth service!");
1567     if (CheckDestroy() == FINISH_DESTROY) {
1568         LOGI("[End]: [Service]: The service has not been initialized!");
1569         return;
1570     }
1571     DestroyTaskManager();
1572     DestroyDevSessionManager();
1573     DestroyGroupManager();
1574     DestroyGmAndGa();
1575     DEV_AUTH_UNLOAD_PLUGIN();
1576     DestroyModules();
1577     DestroyCredMgr();
1578     DestroyChannelManager();
1579     DestroyCallbackManager();
1580     DESTROY_PERFORMANCE_DUMPER();
1581     DestroyPseudonymManager();
1582     DestroyOsAccountAdapter();
1583     SetDeInitStatus();
1584     LOGI("[End]: [Service]: Destroy device auth service successfully!");
1585 }
1586 
GetGmInstance(void)1587 DEVICE_AUTH_API_PUBLIC const DeviceGroupManager *GetGmInstance(void)
1588 {
1589     if (g_groupManagerInstance == NULL) {
1590         LOGE("Service not init.");
1591         return NULL;
1592     }
1593 
1594     g_groupManagerInstance->regCallback = RegCallback;
1595     g_groupManagerInstance->unRegCallback = UnRegGroupManagerCallback;
1596     g_groupManagerInstance->regDataChangeListener = RegListenerImpl;
1597     g_groupManagerInstance->unRegDataChangeListener = UnRegListenerImpl;
1598     g_groupManagerInstance->createGroup = CreateGroupImpl;
1599     g_groupManagerInstance->deleteGroup = DeleteGroupImpl;
1600     g_groupManagerInstance->addMemberToGroup = AddMemberToGroup;
1601     g_groupManagerInstance->deleteMemberFromGroup = DeleteMemberFromGroupImpl;
1602     g_groupManagerInstance->addMultiMembersToGroup = AddMultiMembersToGroupImpl;
1603     g_groupManagerInstance->delMultiMembersFromGroup = DelMultiMembersFromGroupImpl;
1604     g_groupManagerInstance->processData = ProcessBindData;
1605     g_groupManagerInstance->getRegisterInfo = GetRegisterInfoImpl;
1606     g_groupManagerInstance->checkAccessToGroup = CheckAccessToGroupImpl;
1607     g_groupManagerInstance->getPkInfoList = GetPkInfoListImpl;
1608     g_groupManagerInstance->getGroupInfoById = GetGroupInfoByIdImpl;
1609     g_groupManagerInstance->getGroupInfo = GetGroupInfoImpl;
1610     g_groupManagerInstance->getJoinedGroups = GetJoinedGroupsImpl;
1611     g_groupManagerInstance->getRelatedGroups = GetRelatedGroupsImpl;
1612     g_groupManagerInstance->getDeviceInfoById = GetDeviceInfoByIdImpl;
1613     g_groupManagerInstance->getTrustedDevices = GetTrustedDevicesImpl;
1614     g_groupManagerInstance->isDeviceInGroup = IsDeviceInGroupImpl;
1615     g_groupManagerInstance->cancelRequest = CancelRequest;
1616     g_groupManagerInstance->destroyInfo = DestroyInfoImpl;
1617     return g_groupManagerInstance;
1618 }
1619 
GetGaInstance(void)1620 DEVICE_AUTH_API_PUBLIC const GroupAuthManager *GetGaInstance(void)
1621 {
1622     if (g_groupAuthManager == NULL) {
1623         LOGE("Service not init.");
1624         return NULL;
1625     }
1626 
1627     g_groupAuthManager->processData = ProcessData;
1628     g_groupAuthManager->authDevice = AuthDevice;
1629     g_groupAuthManager->cancelRequest = CancelRequest;
1630     g_groupAuthManager->getRealInfo = GetRealInfo;
1631     g_groupAuthManager->getPseudonymId = GetPseudonymId;
1632     return g_groupAuthManager;
1633 }
1634