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