• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "group_operation_common.h"
17 
18 #include "alg_loader.h"
19 #include "string_util.h"
20 #include "data_manager.h"
21 #include "dev_auth_module_manager.h"
22 #include "device_auth_defines.h"
23 #include "group_operation.h"
24 #include "hal_error.h"
25 #include "hc_dev_info.h"
26 #include "hc_log.h"
27 
IsGroupManager(const char * appId,const TrustedGroupEntry * entry)28 static bool IsGroupManager(const char *appId, const TrustedGroupEntry *entry)
29 {
30     uint32_t index;
31     HcString *manager = NULL;
32     FOR_EACH_HC_VECTOR(entry->managers, index, manager) {
33         if (strcmp(StringGet(manager), appId) == 0) {
34             return true;
35         }
36     }
37     return false;
38 }
39 
IsGroupFriend(const char * appId,const TrustedGroupEntry * entry)40 static bool IsGroupFriend(const char *appId, const TrustedGroupEntry *entry)
41 {
42     uint32_t index;
43     HcString *trustedFriend = NULL;
44     FOR_EACH_HC_VECTOR(entry->friends, index, trustedFriend) {
45         if (strcmp(StringGet(trustedFriend), appId) == 0) {
46             return true;
47         }
48     }
49     return false;
50 }
51 
GetGroupNumByOwner(int32_t osAccountId,const char * ownerName)52 static int32_t GetGroupNumByOwner(int32_t osAccountId, const char *ownerName)
53 {
54     if (ownerName == NULL) {
55         LOGE("The input ownerName is NULL!");
56         return 0;
57     }
58     int count = 0;
59     QueryGroupParams queryParams = InitQueryGroupParams();
60     queryParams.ownerName = ownerName;
61     GroupEntryVec groupEntryVec = CreateGroupEntryVec();
62     int32_t result = QueryGroups(osAccountId, &queryParams, &groupEntryVec);
63     if (result != HC_SUCCESS) {
64         LOGE("Failed to query groups!");
65         ClearGroupEntryVec(&groupEntryVec);
66         return count;
67     }
68     count = HC_VECTOR_SIZE(&groupEntryVec);
69     ClearGroupEntryVec(&groupEntryVec);
70     return count;
71 }
72 
GetTrustedDeviceEntryById(int32_t osAccountId,const char * deviceId,bool isUdid,const char * groupId)73 TrustedDeviceEntry *GetTrustedDeviceEntryById(int32_t osAccountId, const char *deviceId, bool isUdid,
74     const char *groupId)
75 {
76     DeviceEntryVec deviceEntryVec = CreateDeviceEntryVec();
77     QueryDeviceParams params = InitQueryDeviceParams();
78     params.groupId = groupId;
79     if (isUdid) {
80         params.udid = deviceId;
81     } else {
82         params.authId = deviceId;
83     }
84     if (QueryDevices(osAccountId, &params, &deviceEntryVec) != HC_SUCCESS) {
85         LOGE("Failed to query trusted devices!");
86         ClearDeviceEntryVec(&deviceEntryVec);
87         return NULL;
88     }
89     uint32_t index;
90     TrustedDeviceEntry **deviceEntry;
91     FOR_EACH_HC_VECTOR(deviceEntryVec, index, deviceEntry) {
92         TrustedDeviceEntry *returnEntry = DeepCopyDeviceEntry(*deviceEntry);
93         ClearDeviceEntryVec(&deviceEntryVec);
94         return returnEntry;
95     }
96     ClearDeviceEntryVec(&deviceEntryVec);
97     return NULL;
98 }
99 
GetGroupEntryById(int32_t osAccountId,const char * groupId)100 TrustedGroupEntry *GetGroupEntryById(int32_t osAccountId, const char *groupId)
101 {
102     if (groupId == NULL) {
103         LOGE("The input groupId is NULL!");
104         return NULL;
105     }
106     uint32_t index;
107     TrustedGroupEntry **entry = NULL;
108     GroupEntryVec groupEntryVec = CreateGroupEntryVec();
109     QueryGroupParams params = InitQueryGroupParams();
110     params.groupId = groupId;
111     if (QueryGroups(osAccountId, &params, &groupEntryVec) != HC_SUCCESS) {
112         LOGE("Failed to query groups!");
113         ClearGroupEntryVec(&groupEntryVec);
114         return NULL;
115     }
116     FOR_EACH_HC_VECTOR(groupEntryVec, index, entry) {
117         TrustedGroupEntry *returnEntry = DeepCopyGroupEntry(*entry);
118         ClearGroupEntryVec(&groupEntryVec);
119         return returnEntry;
120     }
121     ClearGroupEntryVec(&groupEntryVec);
122     return NULL;
123 }
124 
IsTrustedDeviceInGroup(int32_t osAccountId,const char * groupId,const char * deviceId,bool isUdid)125 bool IsTrustedDeviceInGroup(int32_t osAccountId, const char *groupId, const char *deviceId, bool isUdid)
126 {
127     if ((groupId == NULL) || (deviceId == NULL)) {
128         LOGE("The input groupId or deviceId is NULL!");
129         return false;
130     }
131     TrustedDeviceEntry *entry = GetTrustedDeviceEntryById(osAccountId, deviceId, isUdid, groupId);
132     if (entry == NULL) {
133         return false;
134     }
135     DestroyDeviceEntry(entry);
136     return true;
137 }
138 
CheckGroupNumLimit(int32_t osAccountId,int32_t groupType,const char * appId)139 int32_t CheckGroupNumLimit(int32_t osAccountId, int32_t groupType, const char *appId)
140 {
141     /* Currently, only peer to peer group is supported. */
142     (void)groupType;
143     if (GetGroupNumByOwner(osAccountId, appId) >= HC_TRUST_GROUP_ENTRY_MAX_NUM) {
144         LOGE("The number of groups created by the service exceeds the maximum!");
145         return HC_ERR_BEYOND_LIMIT;
146     }
147     return HC_SUCCESS;
148 }
149 
IsLocalDevice(const char * udid)150 bool IsLocalDevice(const char *udid)
151 {
152     char localUdid[INPUT_UDID_LEN] = { 0 };
153     int32_t res = HcGetUdid((uint8_t *)localUdid, INPUT_UDID_LEN);
154     if (res != HC_SUCCESS) {
155         LOGE("Failed to get local udid! res: %d", res);
156         return HC_ERR_DB;
157     }
158     return (strcmp(localUdid, udid) == 0);
159 }
160 
IsGroupOwner(int32_t osAccountId,const char * groupId,const char * appId)161 bool IsGroupOwner(int32_t osAccountId, const char *groupId, const char *appId)
162 {
163     if ((groupId == NULL) || (appId == NULL)) {
164         LOGE("The input groupId or appId is NULL!");
165         return false;
166     }
167     TrustedGroupEntry *entry = GetGroupEntryById(osAccountId, groupId);
168     if (entry == NULL) {
169         LOGE("The group cannot be found!");
170         return false;
171     }
172     HcString entryManager = HC_VECTOR_GET(&entry->managers, 0);
173     const char *groupOwner = StringGet(&entryManager);
174     if (strcmp(groupOwner, appId) == 0) {
175         DestroyGroupEntry(entry);
176         return true;
177     }
178     DestroyGroupEntry(entry);
179     return false;
180 }
181 
IsGroupExistByGroupId(int32_t osAccountId,const char * groupId)182 bool IsGroupExistByGroupId(int32_t osAccountId, const char *groupId)
183 {
184     if (groupId == NULL) {
185         LOGE("The input groupId is NULL!");
186         return false;
187     }
188     TrustedGroupEntry *entry = GetGroupEntryById(osAccountId, groupId);
189     if (entry == NULL) {
190         return false;
191     }
192     DestroyGroupEntry(entry);
193     return true;
194 }
195 
CheckGroupAccessible(int32_t osAccountId,const char * groupId,const char * appId)196 int32_t CheckGroupAccessible(int32_t osAccountId, const char *groupId, const char *appId)
197 {
198     if ((groupId == NULL) || (appId == NULL)) {
199         LOGE("The input groupId or appId is NULL!");
200         return HC_ERR_NULL_PTR;
201     }
202     TrustedGroupEntry *entry = GetGroupEntryById(osAccountId, groupId);
203     if (entry == NULL) {
204         LOGE("The group cannot be found!");
205         return HC_ERR_GROUP_NOT_EXIST;
206     }
207     if ((entry->visibility != GROUP_VISIBILITY_PUBLIC) &&
208         (!IsGroupManager(appId, entry)) &&
209         (!IsGroupFriend(appId, entry))) {
210         DestroyGroupEntry(entry);
211         return HC_ERR_ACCESS_DENIED;
212     }
213     DestroyGroupEntry(entry);
214     return HC_SUCCESS;
215 }
216 
CheckGroupEditAllowed(int32_t osAccountId,const char * groupId,const char * appId)217 int32_t CheckGroupEditAllowed(int32_t osAccountId, const char *groupId, const char *appId)
218 {
219     if ((groupId == NULL) || (appId == NULL)) {
220         LOGE("The input groupId or appId is NULL!");
221         return HC_ERR_NULL_PTR;
222     }
223     TrustedGroupEntry *entry = GetGroupEntryById(osAccountId, groupId);
224     if (entry == NULL) {
225         LOGE("The group cannot be found!");
226         return HC_ERR_GROUP_NOT_EXIST;
227     }
228     if (!IsGroupManager(appId, entry)) {
229         DestroyGroupEntry(entry);
230         return HC_ERR_ACCESS_DENIED;
231     }
232     DestroyGroupEntry(entry);
233     return HC_SUCCESS;
234 }
235 
GetGroupInfo(int32_t osAccountId,const QueryGroupParams * params,GroupEntryVec * returnGroupEntryVec)236 int32_t GetGroupInfo(int32_t osAccountId, const QueryGroupParams *params, GroupEntryVec *returnGroupEntryVec)
237 {
238     /* Fuzzy query interfaces, so some parameters can be NULL. */
239     if (returnGroupEntryVec == NULL) {
240         LOGE("The input returnGroupEntryVec is NULL!");
241         return HC_ERR_INVALID_PARAMS;
242     }
243     return QueryGroups(osAccountId, params, returnGroupEntryVec);
244 }
245 
GetJoinedGroups(int32_t osAccountId,int groupType,GroupEntryVec * returnGroupEntryVec)246 int32_t GetJoinedGroups(int32_t osAccountId, int groupType, GroupEntryVec *returnGroupEntryVec)
247 {
248     QueryGroupParams params = InitQueryGroupParams();
249     params.groupType = groupType;
250     return QueryGroups(osAccountId, &params, returnGroupEntryVec);
251 }
252 
GetRelatedGroups(int32_t osAccountId,const char * peerDeviceId,bool isUdid,GroupEntryVec * returnGroupEntryVec)253 int32_t GetRelatedGroups(int32_t osAccountId, const char *peerDeviceId, bool isUdid, GroupEntryVec *returnGroupEntryVec)
254 {
255     uint32_t index;
256     TrustedDeviceEntry **entry = NULL;
257     DeviceEntryVec deviceEntryVec = CreateDeviceEntryVec();
258     QueryDeviceParams params = InitQueryDeviceParams();
259     params.groupId = NULL;
260     if (isUdid) {
261         params.udid = peerDeviceId;
262     } else {
263         params.authId = peerDeviceId;
264     }
265     int32_t result = QueryDevices(osAccountId, &params, &deviceEntryVec);
266     if (result != HC_SUCCESS) {
267         LOGE("Failed to query trusted devices!");
268         ClearDeviceEntryVec(&deviceEntryVec);
269         return result;
270     }
271     FOR_EACH_HC_VECTOR(deviceEntryVec, index, entry) {
272         TrustedGroupEntry *groupEntry = GetGroupEntryById(osAccountId, StringGet(&(*entry)->groupId));
273         if (groupEntry == NULL) {
274             LOGE("Failed to get group entry by id!");
275             ClearDeviceEntryVec(&deviceEntryVec);
276             return HC_ERR_GROUP_NOT_EXIST;
277         }
278         if (returnGroupEntryVec->pushBackT(returnGroupEntryVec, groupEntry) == NULL) {
279             DestroyGroupEntry(groupEntry);
280             ClearDeviceEntryVec(&deviceEntryVec);
281             return HC_ERR_MEMORY_COPY;
282         }
283     }
284     ClearDeviceEntryVec(&deviceEntryVec);
285     return HC_SUCCESS;
286 }
287 
GetTrustedDevInfoById(int32_t osAccountId,const char * deviceId,bool isUdid,const char * groupId,TrustedDeviceEntry * returnDeviceEntry)288 int32_t GetTrustedDevInfoById(int32_t osAccountId, const char *deviceId, bool isUdid, const char *groupId,
289     TrustedDeviceEntry *returnDeviceEntry)
290 {
291     if ((deviceId == NULL) || (groupId == NULL) || (returnDeviceEntry == NULL)) {
292         LOGE("The input parameters contain NULL value!");
293         return HC_ERR_INVALID_PARAMS;
294     }
295     TrustedDeviceEntry *deviceEntry = GetTrustedDeviceEntryById(osAccountId, deviceId, isUdid, groupId);
296     if (deviceEntry == NULL) {
297         LOGE("The trusted device is not found!");
298         return HC_ERR_DEVICE_NOT_EXIST;
299     }
300     int32_t result = GenerateDeviceEntryFromEntry(deviceEntry, returnDeviceEntry) ? HC_SUCCESS : HC_ERR_MEMORY_COPY;
301     DestroyDeviceEntry(deviceEntry);
302     return result;
303 }
304 
GetTrustedDevices(int32_t osAccountId,const char * groupId,DeviceEntryVec * returnDeviceEntryVec)305 int32_t GetTrustedDevices(int32_t osAccountId, const char *groupId, DeviceEntryVec *returnDeviceEntryVec)
306 {
307     QueryDeviceParams params = InitQueryDeviceParams();
308     params.groupId = groupId;
309     return QueryDevices(osAccountId, &params, returnDeviceEntryVec);
310 }
311 
AddGroupNameToReturn(const TrustedGroupEntry * groupInfo,CJson * json)312 static int32_t AddGroupNameToReturn(const TrustedGroupEntry *groupInfo, CJson *json)
313 {
314     const char *groupName = StringGet(&groupInfo->name);
315     if (groupName == NULL) {
316         LOGE("Failed to get groupName from groupInfo!");
317         return HC_ERR_NULL_PTR;
318     }
319     if (AddStringToJson(json, FIELD_GROUP_NAME, groupName) != HC_SUCCESS) {
320         LOGE("Failed to add groupName to json!");
321         return HC_ERR_JSON_FAIL;
322     }
323     return HC_SUCCESS;
324 }
325 
AddGroupIdToReturn(const TrustedGroupEntry * groupInfo,CJson * json)326 static int32_t AddGroupIdToReturn(const TrustedGroupEntry *groupInfo, CJson *json)
327 {
328     const char *groupId = StringGet(&groupInfo->id);
329     if (groupId == NULL) {
330         LOGE("Failed to get groupId from groupInfo!");
331         return HC_ERR_NULL_PTR;
332     }
333     if (AddStringToJson(json, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
334         LOGE("Failed to add groupId to json!");
335         return HC_ERR_JSON_FAIL;
336     }
337     return HC_SUCCESS;
338 }
339 
AddGroupOwnerToReturn(const TrustedGroupEntry * groupInfo,CJson * json)340 static int32_t AddGroupOwnerToReturn(const TrustedGroupEntry *groupInfo, CJson *json)
341 {
342     HcString entryManager = HC_VECTOR_GET(&groupInfo->managers, 0);
343     const char *groupOwner = StringGet(&entryManager);
344     if (groupOwner == NULL) {
345         LOGE("Failed to get groupOwner from groupInfo!");
346         return HC_ERR_NULL_PTR;
347     }
348     if (AddStringToJson(json, FIELD_GROUP_OWNER, groupOwner) != HC_SUCCESS) {
349         LOGE("Failed to add groupOwner to json!");
350         return HC_ERR_JSON_FAIL;
351     }
352     return HC_SUCCESS;
353 }
354 
AddGroupTypeToReturn(const TrustedGroupEntry * groupInfo,CJson * json)355 static int32_t AddGroupTypeToReturn(const TrustedGroupEntry *groupInfo, CJson *json)
356 {
357     int32_t groupType = groupInfo->type;
358     if (AddIntToJson(json, FIELD_GROUP_TYPE, groupType) != HC_SUCCESS) {
359         LOGE("Failed to add groupType to json!");
360         return HC_ERR_JSON_FAIL;
361     }
362     return HC_SUCCESS;
363 }
364 
AddGroupVisibilityToReturn(const TrustedGroupEntry * groupInfo,CJson * json)365 static int32_t AddGroupVisibilityToReturn(const TrustedGroupEntry *groupInfo, CJson *json)
366 {
367     int groupVisibility = groupInfo->visibility;
368     if (AddIntToJson(json, FIELD_GROUP_VISIBILITY, groupVisibility) != HC_SUCCESS) {
369         LOGE("Failed to add groupType to json!");
370         return HC_ERR_JSON_FAIL;
371     }
372     return HC_SUCCESS;
373 }
374 
AddUserIdToReturnIfAccountGroup(const TrustedGroupEntry * groupInfo,CJson * json)375 static int32_t AddUserIdToReturnIfAccountGroup(const TrustedGroupEntry *groupInfo, CJson *json)
376 {
377     if ((groupInfo->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) && (groupInfo->type != IDENTICAL_ACCOUNT_GROUP)) {
378         return HC_SUCCESS;
379     }
380     const char *userId = StringGet(&groupInfo->userId);
381     if (userId == NULL) {
382         LOGE("Failed to get userId from groupInfo!");
383         return HC_ERR_NULL_PTR;
384     }
385     if (AddStringToJson(json, FIELD_USER_ID, userId) != HC_SUCCESS) {
386         LOGE("Failed to add userId to json!");
387         return HC_ERR_JSON_FAIL;
388     }
389     return HC_SUCCESS;
390 }
391 
AddSharedUserIdToReturnIfAcrossAccountGroup(const TrustedGroupEntry * groupInfo,CJson * json)392 static int32_t AddSharedUserIdToReturnIfAcrossAccountGroup(const TrustedGroupEntry *groupInfo, CJson *json)
393 {
394     if (groupInfo->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) {
395         return HC_SUCCESS;
396     }
397     const char *sharedUserId = StringGet(&groupInfo->sharedUserId);
398     if (sharedUserId == NULL) {
399         LOGE("Failed to get sharedUserId from groupInfo!");
400         return HC_ERR_NULL_PTR;
401     }
402     if (AddStringToJson(json, FIELD_SHARED_USER_ID, sharedUserId) != HC_SUCCESS) {
403         LOGE("Failed to add sharedUserId to json!");
404         return HC_ERR_JSON_FAIL;
405     }
406     return HC_SUCCESS;
407 }
408 
AddAuthIdToReturn(const TrustedDeviceEntry * deviceInfo,CJson * json)409 static int32_t AddAuthIdToReturn(const TrustedDeviceEntry *deviceInfo, CJson *json)
410 {
411     const char *authId = StringGet(&deviceInfo->authId);
412     if (authId == NULL) {
413         LOGE("Failed to get authId from deviceInfo!");
414         return HC_ERR_NULL_PTR;
415     }
416     if (AddStringToJson(json, FIELD_AUTH_ID, authId) != HC_SUCCESS) {
417         LOGE("Failed to add authId to json!");
418         return HC_ERR_JSON_FAIL;
419     }
420     return HC_SUCCESS;
421 }
422 
AddCredentialTypeToReturn(const TrustedDeviceEntry * deviceInfo,CJson * json)423 static int32_t AddCredentialTypeToReturn(const TrustedDeviceEntry *deviceInfo, CJson *json)
424 {
425     int credentialType = deviceInfo->credential;
426     if (AddIntToJson(json, FIELD_CREDENTIAL_TYPE, credentialType) != HC_SUCCESS) {
427         LOGE("Failed to add credentialType to json!");
428         return HC_ERR_JSON_FAIL;
429     }
430     return HC_SUCCESS;
431 }
432 
AddUserTypeToReturn(const TrustedDeviceEntry * deviceInfo,CJson * json)433 static int32_t AddUserTypeToReturn(const TrustedDeviceEntry *deviceInfo, CJson *json)
434 {
435     int userType = deviceInfo->devType;
436     if (AddIntToJson(json, FIELD_USER_TYPE, userType) != HC_SUCCESS) {
437         LOGE("Failed to add userType to json!");
438         return HC_ERR_JSON_FAIL;
439     }
440     return HC_SUCCESS;
441 }
442 
IsAccountRelatedGroup(int groupType)443 bool IsAccountRelatedGroup(int groupType)
444 {
445     return ((groupType == IDENTICAL_ACCOUNT_GROUP) || (groupType == ACROSS_ACCOUNT_AUTHORIZE_GROUP));
446 }
447 
GenerateReturnGroupInfo(const TrustedGroupEntry * groupEntry,CJson * returnJson)448 int32_t GenerateReturnGroupInfo(const TrustedGroupEntry *groupEntry, CJson *returnJson)
449 {
450     int32_t result;
451     if (((result = AddGroupNameToReturn(groupEntry, returnJson)) != HC_SUCCESS) ||
452         ((result = AddGroupIdToReturn(groupEntry, returnJson)) != HC_SUCCESS) ||
453         ((result = AddGroupOwnerToReturn(groupEntry, returnJson)) != HC_SUCCESS) ||
454         ((result = AddGroupTypeToReturn(groupEntry, returnJson)) != HC_SUCCESS) ||
455         ((result = AddGroupVisibilityToReturn(groupEntry, returnJson)) != HC_SUCCESS) ||
456         ((result = AddUserIdToReturnIfAccountGroup(groupEntry, returnJson)) != HC_SUCCESS) ||
457         ((result = AddSharedUserIdToReturnIfAcrossAccountGroup(groupEntry, returnJson)) != HC_SUCCESS)) {
458         return result;
459     }
460     return HC_SUCCESS;
461 }
462 
GenerateReturnDevInfo(const TrustedDeviceEntry * deviceEntry,CJson * returnJson)463 int32_t GenerateReturnDevInfo(const TrustedDeviceEntry *deviceEntry, CJson *returnJson)
464 {
465     int32_t result;
466     if (((result = AddAuthIdToReturn(deviceEntry, returnJson)) != HC_SUCCESS) ||
467         ((result = AddCredentialTypeToReturn(deviceEntry, returnJson)) != HC_SUCCESS) ||
468         ((result = AddUserTypeToReturn(deviceEntry, returnJson)) != HC_SUCCESS)) {
469         return result;
470     }
471     return HC_SUCCESS;
472 }
473 
GetHashMessage(const Uint8Buff * first,const Uint8Buff * second,uint8_t ** hashMessage,uint32_t * messageSize)474 int32_t GetHashMessage(const Uint8Buff *first, const Uint8Buff *second, uint8_t **hashMessage, uint32_t *messageSize)
475 {
476     if ((first == NULL) || (second == NULL) || (hashMessage == NULL) || (messageSize == NULL)) {
477         LOGE("The input parameters contains NULL value!");
478         return HC_ERR_NULL_PTR;
479     }
480     const char *separator = "|";
481     uint32_t firstSize = first->length;
482     uint32_t secondSize = second->length;
483     uint32_t separatorSize = HcStrlen(separator);
484     uint32_t totalSize = firstSize + secondSize + separatorSize;
485     *hashMessage = (uint8_t *)HcMalloc(totalSize, 0);
486     if (*hashMessage == NULL) {
487         LOGE("Failed to allocate hashMessage memory!");
488         return HC_ERR_ALLOC_MEMORY;
489     }
490     int32_t result = HC_SUCCESS;
491     do {
492         if (memcpy_s((*hashMessage), totalSize, first->val, firstSize) != HC_SUCCESS) {
493             LOGE("Failed to copy first!");
494             result = HC_ERR_MEMORY_COPY;
495             break;
496         }
497         if (memcpy_s((*hashMessage) + firstSize, totalSize - firstSize, separator, separatorSize) != HC_SUCCESS) {
498             LOGE("Failed to copy separator!");
499             result = HC_ERR_MEMORY_COPY;
500             break;
501         }
502         if (memcpy_s((*hashMessage) + firstSize + separatorSize, secondSize, second->val, secondSize) != HC_SUCCESS) {
503             LOGE("Failed to copy second!");
504             result = HC_ERR_MEMORY_COPY;
505         }
506     } while (0);
507     if (result != HC_SUCCESS) {
508         HcFree(*hashMessage);
509         *hashMessage = NULL;
510         return result;
511     }
512     *messageSize = totalSize;
513     return HC_SUCCESS;
514 }
515 
GetCurDeviceNumByGroupId(int32_t osAccountId,const char * groupId)516 int32_t GetCurDeviceNumByGroupId(int32_t osAccountId, const char *groupId)
517 {
518     if (groupId == NULL) {
519         LOGE("The input groupId is NULL!");
520         return 0;
521     }
522     int count = 0;
523     QueryDeviceParams queryDeviceParams = InitQueryDeviceParams();
524     queryDeviceParams.groupId = groupId;
525     DeviceEntryVec deviceEntryVec = CreateDeviceEntryVec();
526     int32_t result = QueryDevices(osAccountId, &queryDeviceParams, &deviceEntryVec);
527     if (result != HC_SUCCESS) {
528         LOGE("Failed to query trusted devices!");
529         ClearDeviceEntryVec(&deviceEntryVec);
530         return result;
531     }
532     count = HC_VECTOR_SIZE(&deviceEntryVec);
533     ClearDeviceEntryVec(&deviceEntryVec);
534     return count;
535 }
536 
CheckDeviceNumLimit(int32_t osAccountId,const char * groupId,const char * peerUdid)537 int32_t CheckDeviceNumLimit(int32_t osAccountId, const char *groupId, const char *peerUdid)
538 {
539     /*
540      * If the peer device does not exist in the group and needs to be added,
541      * check whether the number of trusted devices exceeds the upper limit.
542      */
543 
544     if ((peerUdid != NULL) && (IsTrustedDeviceInGroup(osAccountId, groupId, peerUdid, true))) {
545         return HC_SUCCESS;
546     }
547     if (GetCurDeviceNumByGroupId(osAccountId, groupId) >= HC_TRUST_DEV_ENTRY_MAX_NUM) {
548         LOGE("The number of devices in the group has reached the upper limit!");
549         return HC_ERR_BEYOND_LIMIT;
550     }
551     return HC_SUCCESS;
552 }
553 
IsUserTypeValid(int userType)554 bool IsUserTypeValid(int userType)
555 {
556     if ((userType == DEVICE_TYPE_ACCESSORY) ||
557         (userType == DEVICE_TYPE_CONTROLLER) ||
558         (userType == DEVICE_TYPE_PROXY)) {
559         return true;
560     }
561     return false;
562 }
563 
IsExpireTimeValid(int expireTime)564 bool IsExpireTimeValid(int expireTime)
565 {
566     if ((expireTime < -1) || (expireTime == 0) || (expireTime > MAX_EXPIRE_TIME)) {
567         return false;
568     }
569     return true;
570 }
571 
IsGroupVisibilityValid(int groupVisibility)572 bool IsGroupVisibilityValid(int groupVisibility)
573 {
574     /* Currently, only the public group and private group can be created. */
575     if ((groupVisibility == GROUP_VISIBILITY_PUBLIC) ||
576         ((groupVisibility == GROUP_VISIBILITY_PRIVATE))) {
577         return true;
578     }
579     return false;
580 }
581 
CheckUserTypeIfExist(const CJson * jsonParams)582 int32_t CheckUserTypeIfExist(const CJson *jsonParams)
583 {
584     int32_t userType = DEVICE_TYPE_ACCESSORY;
585     (void)GetIntFromJson(jsonParams, FIELD_USER_TYPE, &userType);
586     if (!IsUserTypeValid(userType)) {
587         LOGE("The input userType is invalid! [UserType]: %d", userType);
588         return HC_ERR_INVALID_PARAMS;
589     }
590     return HC_SUCCESS;
591 }
592 
CheckGroupVisibilityIfExist(const CJson * jsonParams)593 int32_t CheckGroupVisibilityIfExist(const CJson *jsonParams)
594 {
595     int32_t groupVisibility = GROUP_VISIBILITY_PUBLIC;
596     (void)GetIntFromJson(jsonParams, FIELD_GROUP_VISIBILITY, &groupVisibility);
597     if (!IsGroupVisibilityValid(groupVisibility)) {
598         LOGE("The input groupVisibility is invalid! [GroupVisibility]: %d", groupVisibility);
599         return HC_ERR_INVALID_PARAMS;
600     }
601     return HC_SUCCESS;
602 }
603 
CheckExpireTimeIfExist(const CJson * jsonParams)604 int32_t CheckExpireTimeIfExist(const CJson *jsonParams)
605 {
606     int32_t expireTime = DEFAULT_EXPIRE_TIME;
607     (void)GetIntFromJson(jsonParams, FIELD_EXPIRE_TIME, &expireTime);
608     if (!IsExpireTimeValid(expireTime)) {
609         LOGE("Invalid group expire time! [ExpireTime]: %d", expireTime);
610         return HC_ERR_INVALID_PARAMS;
611     }
612     return HC_SUCCESS;
613 }
614 
AddGroupNameToParams(const char * groupName,TrustedGroupEntry * groupParams)615 int32_t AddGroupNameToParams(const char *groupName, TrustedGroupEntry *groupParams)
616 {
617     if (!StringSetPointer(&groupParams->name, groupName)) {
618         LOGE("Failed to copy groupName!");
619         return HC_ERR_MEMORY_COPY;
620     }
621     return HC_SUCCESS;
622 }
623 
AddGroupIdToParams(const char * groupId,TrustedGroupEntry * groupParams)624 int32_t AddGroupIdToParams(const char *groupId, TrustedGroupEntry *groupParams)
625 {
626     if (!StringSetPointer(&groupParams->id, groupId)) {
627         LOGE("Failed to copy groupId!");
628         return HC_ERR_MEMORY_COPY;
629     }
630     return HC_SUCCESS;
631 }
632 
AddGroupOwnerToParams(const char * owner,TrustedGroupEntry * groupParams)633 int32_t AddGroupOwnerToParams(const char *owner, TrustedGroupEntry *groupParams)
634 {
635     HcString ownerName = CreateString();
636     if (!StringSetPointer(&ownerName, owner)) {
637         LOGE("Failed to copy groupOwner!");
638         DeleteString(&ownerName);
639         return HC_ERR_MEMORY_COPY;
640     }
641     if (groupParams->managers.pushBackT(&groupParams->managers, ownerName) == NULL) {
642         LOGE("Failed to push owner to vec!");
643         DeleteString(&ownerName);
644         return HC_ERR_MEMORY_COPY;
645     }
646     return HC_SUCCESS;
647 }
648 
AddGroupTypeToParams(int groupType,TrustedGroupEntry * groupParams)649 int32_t AddGroupTypeToParams(int groupType, TrustedGroupEntry *groupParams)
650 {
651     groupParams->type = groupType;
652     return HC_SUCCESS;
653 }
654 
AddGroupVisibilityOrDefault(const CJson * jsonParams,TrustedGroupEntry * groupParams)655 int32_t AddGroupVisibilityOrDefault(const CJson *jsonParams, TrustedGroupEntry *groupParams)
656 {
657     /* Currently, only the public group and private group can be created. */
658     int32_t groupVisibility = GROUP_VISIBILITY_PUBLIC;
659     (void)GetIntFromJson(jsonParams, FIELD_GROUP_VISIBILITY, &groupVisibility);
660     groupParams->visibility = groupVisibility;
661     return HC_SUCCESS;
662 }
663 
AddExpireTimeOrDefault(const CJson * jsonParams,TrustedGroupEntry * groupParams)664 int32_t AddExpireTimeOrDefault(const CJson *jsonParams, TrustedGroupEntry *groupParams)
665 {
666     int32_t expireTime = DEFAULT_EXPIRE_TIME;
667     (void)GetIntFromJson(jsonParams, FIELD_EXPIRE_TIME, &expireTime);
668     groupParams->expireTime = expireTime;
669     return HC_SUCCESS;
670 }
671 
AddUserIdToGroupParams(const CJson * jsonParams,TrustedGroupEntry * groupParams)672 int32_t AddUserIdToGroupParams(const CJson *jsonParams, TrustedGroupEntry *groupParams)
673 {
674     char *userId = NULL;
675     int32_t result = GetUserIdFromJson(jsonParams, &userId);
676     if (result != HC_SUCCESS) {
677         return result;
678     }
679     if (!StringSetPointer(&groupParams->userId, userId)) {
680         LOGE("Failed to copy userId!");
681         HcFree(userId);
682         return HC_ERR_MEMORY_COPY;
683     }
684     HcFree(userId);
685     return HC_SUCCESS;
686 }
687 
AddSharedUserIdToGroupParams(const CJson * jsonParams,TrustedGroupEntry * groupParams)688 int32_t AddSharedUserIdToGroupParams(const CJson *jsonParams, TrustedGroupEntry *groupParams)
689 {
690     char *sharedUserId = NULL;
691     int32_t result = GetSharedUserIdFromJson(jsonParams, &sharedUserId);
692     if (result != HC_SUCCESS) {
693         return result;
694     }
695     if (!StringSetPointer(&groupParams->sharedUserId, sharedUserId)) {
696         LOGE("Failed to copy sharedUserId!");
697         HcFree(sharedUserId);
698         return HC_ERR_MEMORY_COPY;
699     }
700     HcFree(sharedUserId);
701     return HC_SUCCESS;
702 }
703 
AddSelfUdidToParams(TrustedDeviceEntry * devParams)704 int32_t AddSelfUdidToParams(TrustedDeviceEntry *devParams)
705 {
706     char udid[INPUT_UDID_LEN] = { 0 };
707     int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
708     if (res != HC_SUCCESS) {
709         LOGE("Failed to get local udid! res: %d", res);
710         return HC_ERR_DB;
711     }
712     if (!StringSetPointer(&devParams->udid, udid)) {
713         LOGE("Failed to copy udid!");
714         return HC_ERR_MEMORY_COPY;
715     }
716     return HC_SUCCESS;
717 }
718 
AddUdidToParams(const CJson * jsonParams,TrustedDeviceEntry * devParams)719 int32_t AddUdidToParams(const CJson *jsonParams, TrustedDeviceEntry *devParams)
720 {
721     const char *udid = GetStringFromJson(jsonParams, FIELD_UDID);
722     if (udid == NULL) {
723         LOGE("Failed to get udid from json!");
724         return HC_ERR_JSON_GET;
725     }
726     if (!StringSetPointer(&devParams->udid, udid)) {
727         LOGE("Failed to copy udid!");
728         return HC_ERR_MEMORY_COPY;
729     }
730     return HC_SUCCESS;
731 }
732 
AddAuthIdToParamsOrDefault(const CJson * jsonParams,TrustedDeviceEntry * devParams)733 int32_t AddAuthIdToParamsOrDefault(const CJson *jsonParams, TrustedDeviceEntry *devParams)
734 {
735     const char *authId = GetStringFromJson(jsonParams, FIELD_DEVICE_ID);
736     char udid[INPUT_UDID_LEN] = { 0 };
737     if (authId == NULL) {
738         LOGD("No authId is found. The default value is udid!");
739         int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
740         if (res != HC_SUCCESS) {
741             LOGE("Failed to get local udid! res: %d", res);
742             return HC_ERR_DB;
743         }
744         authId = udid;
745     }
746     if (!StringSetPointer(&devParams->authId, authId)) {
747         LOGE("Failed to copy authId!");
748         return HC_ERR_MEMORY_COPY;
749     }
750     return HC_SUCCESS;
751 }
752 
AddAuthIdToParams(const CJson * jsonParams,TrustedDeviceEntry * devParams)753 int32_t AddAuthIdToParams(const CJson *jsonParams, TrustedDeviceEntry *devParams)
754 {
755     const char *authId = GetStringFromJson(jsonParams, FIELD_DEVICE_ID);
756     if (authId == NULL) {
757         LOGE("Failed to get authId from json!");
758         return HC_ERR_JSON_GET;
759     }
760     if (!StringSetPointer(&devParams->authId, authId)) {
761         LOGE("Failed to copy authId!");
762         return HC_ERR_MEMORY_COPY;
763     }
764     return HC_SUCCESS;
765 }
766 
AddSourceToParams(RelationShipSource source,TrustedDeviceEntry * devParams)767 int32_t AddSourceToParams(RelationShipSource source, TrustedDeviceEntry *devParams)
768 {
769     devParams->source = source;
770     return HC_SUCCESS;
771 }
772 
AddCredTypeToParams(const CJson * jsonParams,TrustedDeviceEntry * devParams)773 int32_t AddCredTypeToParams(const CJson *jsonParams, TrustedDeviceEntry *devParams)
774 {
775     int32_t credType = INVALID_CRED;
776     if (GetIntFromJson(jsonParams, FIELD_CREDENTIAL_TYPE, &credType) != HC_SUCCESS) {
777         LOGE("Failed to get credentialType from json!");
778         return HC_ERR_JSON_GET;
779     }
780     devParams->credential = (uint8_t)credType;
781     return HC_SUCCESS;
782 }
783 
AddUserTypeToParamsOrDefault(const CJson * jsonParams,TrustedDeviceEntry * devParams)784 int32_t AddUserTypeToParamsOrDefault(const CJson *jsonParams, TrustedDeviceEntry *devParams)
785 {
786     int32_t userType = DEVICE_TYPE_ACCESSORY;
787     (void)GetIntFromJson(jsonParams, FIELD_USER_TYPE, &userType);
788     devParams->devType = userType;
789     return HC_SUCCESS;
790 }
791 
AddServiceTypeToParams(const char * groupId,TrustedDeviceEntry * devParams)792 int32_t AddServiceTypeToParams(const char *groupId, TrustedDeviceEntry *devParams)
793 {
794     if (!StringSetPointer(&devParams->serviceType, groupId)) {
795         LOGE("Failed to copy serviceType!");
796         return HC_ERR_MEMORY_COPY;
797     }
798     return HC_SUCCESS;
799 }
800 
AddGroupIdToDevParams(const char * groupId,TrustedDeviceEntry * devParams)801 int32_t AddGroupIdToDevParams(const char *groupId, TrustedDeviceEntry *devParams)
802 {
803     if (!StringSetPointer(&devParams->groupId, groupId)) {
804         LOGE("Failed to copy groupId!");
805         return HC_ERR_MEMORY_COPY;
806     }
807     return HC_SUCCESS;
808 }
809 
AddUserIdToDevParams(const CJson * jsonParams,TrustedDeviceEntry * devParams)810 int32_t AddUserIdToDevParams(const CJson *jsonParams, TrustedDeviceEntry *devParams)
811 {
812     char *userId = NULL;
813     int32_t result = GetUserIdFromJson(jsonParams, &userId);
814     if (result != HC_SUCCESS) {
815         return result;
816     }
817     if (!StringSetPointer(&devParams->userId, userId)) {
818         LOGE("Failed to copy userId!");
819         HcFree(userId);
820         return HC_ERR_MEMORY_COPY;
821     }
822     HcFree(userId);
823     return HC_SUCCESS;
824 }
825 
AssertUserIdExist(const CJson * jsonParams)826 int32_t AssertUserIdExist(const CJson *jsonParams)
827 {
828     const char *userId = GetStringFromJson(jsonParams, FIELD_USER_ID);
829     if (userId == NULL) {
830         LOGE("Failed to get userId from jsonParams!");
831         return HC_ERR_JSON_GET;
832     }
833     return HC_SUCCESS;
834 }
835 
AssertSameGroupNotExist(int32_t osAccountId,const char * groupId)836 int32_t AssertSameGroupNotExist(int32_t osAccountId, const char *groupId)
837 {
838     if (IsGroupExistByGroupId(osAccountId, groupId)) {
839         LOGE("The group has been created!");
840         return HC_ERR_GROUP_DUPLICATE;
841     }
842     return HC_SUCCESS;
843 }
844 
AssertPeerDeviceNotSelf(const char * peerUdid)845 int32_t AssertPeerDeviceNotSelf(const char *peerUdid)
846 {
847     if (peerUdid == NULL) {
848         LOGE("The input peerUdid is NULL!");
849         return HC_ERR_NULL_PTR;
850     }
851     char udid[INPUT_UDID_LEN] = { 0 };
852     int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
853     if (res != HC_SUCCESS) {
854         LOGE("Failed to get local udid! res: %d", res);
855         return HC_ERR_DB;
856     }
857     if (strcmp(peerUdid, udid) == 0) {
858         LOGE("You are not allowed to delete yourself!");
859         return HC_ERR_INVALID_PARAMS;
860     }
861     return HC_SUCCESS;
862 }
863 
CheckGroupExist(int32_t osAccountId,const char * groupId)864 int32_t CheckGroupExist(int32_t osAccountId, const char *groupId)
865 {
866     if (groupId == NULL) {
867         LOGE("The input groupId is NULL!");
868         return HC_ERR_NULL_PTR;
869     }
870     if (!IsGroupExistByGroupId(osAccountId, groupId)) {
871         LOGE("The group does not exist!");
872         return HC_ERR_GROUP_NOT_EXIST;
873     }
874     return HC_SUCCESS;
875 }
876 
AddGroupToDatabaseByJson(int32_t osAccountId,int32_t (* generateGroupParams)(const CJson *,const char *,TrustedGroupEntry *),const CJson * jsonParams,const char * groupId)877 int32_t AddGroupToDatabaseByJson(int32_t osAccountId, int32_t (*generateGroupParams)(const CJson*, const char *,
878     TrustedGroupEntry*), const CJson *jsonParams, const char *groupId)
879 {
880     if ((generateGroupParams == NULL) || (jsonParams == NULL) || (groupId == NULL)) {
881         LOGE("The input parameters contains NULL value!");
882         return HC_ERR_INVALID_PARAMS;
883     }
884     TrustedGroupEntry *groupParams = CreateGroupEntry();
885     if (groupParams == NULL) {
886         LOGE("Failed to allocate groupParams memory!");
887         return HC_ERR_ALLOC_MEMORY;
888     }
889 
890     int32_t result = (*generateGroupParams)(jsonParams, groupId, groupParams);
891     if (result != HC_SUCCESS) {
892         DestroyGroupEntry(groupParams);
893         return result;
894     }
895 
896     result = AddGroup(osAccountId, groupParams);
897     DestroyGroupEntry(groupParams);
898     if (result != HC_SUCCESS) {
899         LOGE("Failed to add the group to the database!");
900     }
901     return result;
902 }
903 
AddDeviceToDatabaseByJson(int32_t osAccountId,int32_t (* generateDevParams)(const CJson *,const char *,TrustedDeviceEntry *),const CJson * jsonParams,const char * groupId)904 int32_t AddDeviceToDatabaseByJson(int32_t osAccountId, int32_t (*generateDevParams)(const CJson*, const char*,
905     TrustedDeviceEntry*), const CJson *jsonParams, const char *groupId)
906 {
907     if ((generateDevParams == NULL) || (jsonParams == NULL) || (groupId == NULL)) {
908         LOGE("The input parameters contains NULL value!");
909         return HC_ERR_INVALID_PARAMS;
910     }
911     TrustedDeviceEntry *devParams = CreateDeviceEntry();
912     if (devParams == NULL) {
913         LOGE("Failed to allocate devParams memory!");
914         return HC_ERR_ALLOC_MEMORY;
915     }
916 
917     int32_t result = (*generateDevParams)(jsonParams, groupId, devParams);
918     if (result != HC_SUCCESS) {
919         DestroyDeviceEntry(devParams);
920         return result;
921     }
922 
923     result = AddTrustedDevice(osAccountId, devParams);
924     DestroyDeviceEntry(devParams);
925     if (result != HC_SUCCESS) {
926         LOGE("Failed to add the trust device to the database!");
927     }
928     return result;
929 }
930 
DelGroupFromDb(int32_t osAccountId,const char * groupId)931 int32_t DelGroupFromDb(int32_t osAccountId, const char *groupId)
932 {
933     if (groupId == NULL) {
934         LOGE("The input groupId is NULL!");
935         return HC_ERR_NULL_PTR;
936     }
937     QueryGroupParams queryGroupParams = InitQueryGroupParams();
938     queryGroupParams.groupId = groupId;
939     QueryDeviceParams queryDeviceParams = InitQueryDeviceParams();
940     queryDeviceParams.groupId = groupId;
941     int32_t result = HC_SUCCESS;
942     if (DelTrustedDevice(osAccountId, &queryDeviceParams) != HC_SUCCESS) {
943         result = HC_ERR_DEL_GROUP;
944     }
945     if (DelGroup(osAccountId, &queryGroupParams) != HC_SUCCESS) {
946         result = HC_ERR_DEL_GROUP;
947     }
948     if (SaveOsAccountDb(osAccountId) != HC_SUCCESS) {
949         result = HC_ERR_DEL_GROUP;
950     }
951     return result;
952 }
953 
ConvertGroupIdToJsonStr(const char * groupId,char ** returnJsonStr)954 int32_t ConvertGroupIdToJsonStr(const char *groupId, char **returnJsonStr)
955 {
956     if ((groupId == NULL) || (returnJsonStr == NULL)) {
957         LOGE("The input parameters contains NULL value!");
958         return HC_ERR_INVALID_PARAMS;
959     }
960     CJson *json = CreateJson();
961     if (json == NULL) {
962         LOGE("Failed to allocate json memory!");
963         return HC_ERR_ALLOC_MEMORY;
964     }
965     if (AddStringToJson(json, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
966         LOGE("Failed to add groupId to json!");
967         FreeJson(json);
968         return HC_ERR_JSON_FAIL;
969     }
970     *returnJsonStr = PackJsonToString(json);
971     FreeJson(json);
972     if (*returnJsonStr == NULL) {
973         LOGE("Failed to convert json to string!");
974         return HC_ERR_JSON_FAIL;
975     }
976     return HC_SUCCESS;
977 }
978 
GenerateBindSuccessData(const char * peerAuthId,const char * groupId,char ** returnDataStr)979 int32_t GenerateBindSuccessData(const char *peerAuthId, const char *groupId, char **returnDataStr)
980 {
981     if ((peerAuthId == NULL) || (groupId == NULL) || (returnDataStr == NULL)) {
982         LOGE("The input params contains NULL value!");
983         return HC_ERR_NULL_PTR;
984     }
985     LOGI("Bind successfully! [GroupId]: %c%c%c%c****", groupId[DEV_AUTH_ZERO], groupId[DEV_AUTH_ONE],
986         groupId[DEV_AUTH_TWO], groupId[DEV_AUTH_THREE]);
987     if (HcStrlen(peerAuthId) >= DESENSITIZATION_LEN) {
988         LOGI("Bind successfully! [PeerAuthId]: %c%c%c%c****", peerAuthId[DEV_AUTH_ZERO], peerAuthId[DEV_AUTH_ONE],
989             peerAuthId[DEV_AUTH_TWO], peerAuthId[DEV_AUTH_THREE]);
990     } else {
991         LOGI("Bind successfully! [PeerAuthId]: too short");
992     }
993     CJson *jsonData = CreateJson();
994     if (jsonData == NULL) {
995         LOGE("Failed to allocate jsonData memory!");
996         return HC_ERR_JSON_FAIL;
997     }
998     if (AddStringToJson(jsonData, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
999         LOGE("Failed to add groupId to jsonData!");
1000         FreeJson(jsonData);
1001         return HC_ERR_JSON_FAIL;
1002     }
1003     if (AddStringToJson(jsonData, FIELD_ADD_ID, peerAuthId) != HC_SUCCESS) {
1004         LOGE("Failed to add addId to jsonData!");
1005         FreeJson(jsonData);
1006         return HC_ERR_JSON_FAIL;
1007     }
1008     char *jsonDataStr = PackJsonToString(jsonData);
1009     FreeJson(jsonData);
1010     if (jsonDataStr == NULL) {
1011         LOGE("An error occurred when converting JSON data to String data!");
1012         return HC_ERR_JSON_FAIL;
1013     }
1014     *returnDataStr = jsonDataStr;
1015     return HC_SUCCESS;
1016 }
1017 
GenerateUnbindSuccessData(const char * peerAuthId,const char * groupId,char ** returnDataStr)1018 int32_t GenerateUnbindSuccessData(const char *peerAuthId, const char *groupId, char **returnDataStr)
1019 {
1020     if ((peerAuthId == NULL) || (groupId == NULL) || (returnDataStr == NULL)) {
1021         LOGE("The input params contains NULL value!");
1022         return HC_ERR_NULL_PTR;
1023     }
1024     LOGI("Unbind successfully! [GroupId]: %c%c%c%c****", groupId[DEV_AUTH_ZERO], groupId[DEV_AUTH_ONE],
1025         groupId[DEV_AUTH_TWO], groupId[DEV_AUTH_THREE]);
1026     if (HcStrlen(peerAuthId) >= DESENSITIZATION_LEN) {
1027         LOGI("Unbind successfully! [PeerAuthId]: %c%c%c%c****", peerAuthId[DEV_AUTH_ZERO], peerAuthId[DEV_AUTH_ONE],
1028             peerAuthId[DEV_AUTH_TWO], peerAuthId[DEV_AUTH_THREE]);
1029     } else {
1030         LOGI("Unbind successfully! [PeerAuthId]: too short");
1031     }
1032     CJson *jsonData = CreateJson();
1033     if (jsonData == NULL) {
1034         LOGE("Failed to allocate jsonData memory!");
1035         return HC_ERR_JSON_FAIL;
1036     }
1037     if (AddStringToJson(jsonData, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
1038         LOGE("Failed to add groupId to jsonData!");
1039         FreeJson(jsonData);
1040         return HC_ERR_JSON_FAIL;
1041     }
1042     if (AddStringToJson(jsonData, FIELD_DELETE_ID, peerAuthId) != HC_SUCCESS) {
1043         LOGE("Failed to add deleteId to jsonData!");
1044         FreeJson(jsonData);
1045         return HC_ERR_JSON_FAIL;
1046     }
1047     char *jsonDataStr = PackJsonToString(jsonData);
1048     FreeJson(jsonData);
1049     if (jsonDataStr == NULL) {
1050         LOGE("An error occurred when converting JSON data to String data!");
1051         return HC_ERR_JSON_FAIL;
1052     }
1053     *returnDataStr = jsonDataStr;
1054     return HC_SUCCESS;
1055 }
1056 
ProcessKeyPair(int action,const CJson * jsonParams,const char * groupId)1057 int32_t ProcessKeyPair(int action, const CJson *jsonParams, const char *groupId)
1058 {
1059     if ((jsonParams == NULL) || (groupId == NULL)) {
1060         LOGE("The input parameters contains NULL value!");
1061         return HC_ERR_INVALID_PARAMS;
1062     }
1063     /* Use the DeviceGroupManager package name. */
1064     const char *appId = GROUP_MANAGER_PACKAGE_NAME;
1065     const char *authId = GetStringFromJson(jsonParams, FIELD_DEVICE_ID);
1066     char udid[INPUT_UDID_LEN] = { 0 };
1067     if (authId == NULL) {
1068         LOGD("No authId is found. The default value is udid!");
1069         int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
1070         if (res != HC_SUCCESS) {
1071             LOGE("Failed to get local udid! res: %d", res);
1072             return HC_ERR_DB;
1073         }
1074         authId = udid;
1075     }
1076     int32_t userType = DEVICE_TYPE_ACCESSORY;
1077     (void)GetIntFromJson(jsonParams, FIELD_USER_TYPE, &userType);
1078     Uint8Buff authIdBuff = { 0, 0 };
1079     authIdBuff.length = HcStrlen(authId);
1080     if (authIdBuff.length > MAX_DATA_BUFFER_SIZE) {
1081         LOGE("The length of authId is too long!");
1082         return HC_ERR_INVALID_PARAMS;
1083     }
1084     authIdBuff.val = (uint8_t *)HcMalloc(authIdBuff.length, 0);
1085     if (authIdBuff.val == NULL) {
1086         LOGE("Failed to allocate authIdBuff memory!");
1087         return HC_ERR_ALLOC_MEMORY;
1088     }
1089     if (memcpy_s(authIdBuff.val, authIdBuff.length, authId, authIdBuff.length) != EOK) {
1090         LOGE("Failed to copy authId!");
1091         HcFree(authIdBuff.val);
1092         return HC_ERR_MEMORY_COPY;
1093     }
1094     int32_t result;
1095     if (action == CREATE_KEY_PAIR) {
1096         result = RegisterLocalIdentity(appId, groupId, &authIdBuff, userType, DAS_MODULE);
1097     } else {
1098         result = UnregisterLocalIdentity(appId, groupId, &authIdBuff, userType, DAS_MODULE);
1099     }
1100     HcFree(authIdBuff.val);
1101     return result;
1102 }
1103 
GetGroupTypeFromDb(int32_t osAccountId,const char * groupId,int32_t * returnGroupType)1104 int32_t GetGroupTypeFromDb(int32_t osAccountId, const char *groupId, int32_t *returnGroupType)
1105 {
1106     if ((groupId == NULL) || (returnGroupType == NULL)) {
1107         LOGE("The input parameters contains NULL value!");
1108         return HC_ERR_INVALID_PARAMS;
1109     }
1110     TrustedGroupEntry *groupEntry = GetGroupEntryById(osAccountId, groupId);
1111     if (groupEntry == NULL) {
1112         LOGE("Failed to get groupEntry from db!");
1113         return HC_ERR_DB;
1114     }
1115     *returnGroupType = groupEntry->type;
1116     DestroyGroupEntry(groupEntry);
1117     return HC_SUCCESS;
1118 }
1119 
GetUserIdFromJson(const CJson * jsonParams,char ** userId)1120 int32_t GetUserIdFromJson(const CJson *jsonParams, char **userId)
1121 {
1122     if ((jsonParams == NULL) || (userId == NULL)) {
1123         LOGE("The input parameters contains NULL value!");
1124         return HC_ERR_INVALID_PARAMS;
1125     }
1126     const char *oriUserId = GetStringFromJson(jsonParams, FIELD_USER_ID);
1127     if (oriUserId == NULL) {
1128         LOGE("Failed to get userId from jsonParams!");
1129         return HC_ERR_JSON_GET;
1130     }
1131     return ToUpperCase(oriUserId, userId);
1132 }
1133 
GetSharedUserIdFromJson(const CJson * jsonParams,char ** sharedUserId)1134 int32_t GetSharedUserIdFromJson(const CJson *jsonParams, char **sharedUserId)
1135 {
1136     if ((jsonParams == NULL) || (sharedUserId == NULL)) {
1137         LOGE("The input parameters contains NULL value!");
1138         return HC_ERR_INVALID_PARAMS;
1139     }
1140     const char *oriUserId = GetStringFromJson(jsonParams, FIELD_PEER_USER_ID);
1141     if (oriUserId == NULL) {
1142         LOGE("Failed to get sharedUserId from jsonParams!");
1143         return HC_ERR_JSON_GET;
1144     }
1145     return ToUpperCase(oriUserId, sharedUserId);
1146 }
1147 
GetGroupIdFromJson(const CJson * jsonParams,const char ** groupId)1148 int32_t GetGroupIdFromJson(const CJson *jsonParams, const char **groupId)
1149 {
1150     if ((jsonParams == NULL) || (groupId == NULL)) {
1151         LOGE("The input parameters contains NULL value!");
1152         return HC_ERR_INVALID_PARAMS;
1153     }
1154     *groupId = GetStringFromJson(jsonParams, FIELD_GROUP_ID);
1155     if (*groupId == NULL) {
1156         LOGE("Failed to get groupId from jsonParams!");
1157         return HC_ERR_JSON_GET;
1158     }
1159     return HC_SUCCESS;
1160 }
1161 
GetAppIdFromJson(const CJson * jsonParams,const char ** appId)1162 int32_t GetAppIdFromJson(const CJson *jsonParams, const char **appId)
1163 {
1164     if ((jsonParams == NULL) || (appId == NULL)) {
1165         LOGE("The input parameters contains NULL value!");
1166         return HC_ERR_INVALID_PARAMS;
1167     }
1168     *appId = GetStringFromJson(jsonParams, FIELD_APP_ID);
1169     if (*appId == NULL) {
1170         LOGE("Failed to get appId from jsonParams!");
1171         return HC_ERR_JSON_GET;
1172     }
1173     return HC_SUCCESS;
1174 }
1175 
AssertGroupTypeMatch(int32_t inputType,int32_t targetType)1176 int32_t AssertGroupTypeMatch(int32_t inputType, int32_t targetType)
1177 {
1178     if (inputType != targetType) {
1179         LOGE("Invalid group type! [InputType]: %d, [TargetType]: %d", inputType, targetType);
1180         return HC_ERR_INVALID_PARAMS;
1181     }
1182     return HC_SUCCESS;
1183 }
1184 
CheckPermForGroup(int32_t osAccountId,int actionType,const char * callerPkgName,const char * groupId)1185 int32_t CheckPermForGroup(int32_t osAccountId, int actionType, const char *callerPkgName, const char *groupId)
1186 {
1187     if (((actionType == GROUP_DISBAND) && (IsGroupOwner(osAccountId, groupId, callerPkgName))) ||
1188         ((actionType == MEMBER_INVITE) && (CheckGroupEditAllowed(osAccountId, groupId, callerPkgName) == HC_SUCCESS)) ||
1189         ((actionType == MEMBER_DELETE) && (CheckGroupEditAllowed(osAccountId, groupId, callerPkgName) == HC_SUCCESS))) {
1190         return HC_SUCCESS;
1191     }
1192     LOGE("You do not have the right to execute the command!");
1193     return HC_ERR_ACCESS_DENIED;
1194 }
1195 
GetHashResult(const uint8_t * info,uint32_t infoLen,char * hash,uint32_t hashLen)1196 int32_t GetHashResult(const uint8_t *info, uint32_t infoLen, char *hash, uint32_t hashLen)
1197 {
1198     if ((info == NULL) || (hash == NULL)) {
1199         LOGE("The input parameters contains NULL value!");
1200         return HAL_ERR_NULL_PTR;
1201     }
1202     Uint8Buff infoHash = { NULL, SHA256_LEN };
1203     Uint8Buff message = { NULL, infoLen };
1204     infoHash.val = (uint8_t *)HcMalloc(SHA256_LEN, 0);
1205     if (infoHash.val == NULL) {
1206         LOGE("Failed to allocate infoHash.val memory!");
1207         return HAL_ERR_BAD_ALLOC;
1208     }
1209     message.val = (uint8_t *)HcMalloc(infoLen, 0);
1210     if (message.val == NULL) {
1211         LOGE("Failed to allocate message.val memory!");
1212         HcFree(infoHash.val);
1213         return HAL_ERR_BAD_ALLOC;
1214     }
1215     if (memcpy_s(message.val, infoLen, info, infoLen) != EOK) {
1216         LOGE("Failed to copy info!");
1217         HcFree(infoHash.val);
1218         HcFree(message.val);
1219         return HAL_ERR_MEMORY_COPY;
1220     }
1221     int32_t result = GetLoaderInstance()->sha256(&message, &infoHash);
1222     if (result == HAL_SUCCESS) {
1223         if (ByteToHexString(infoHash.val, infoHash.length, hash, hashLen) != HAL_SUCCESS) {
1224             LOGE("Failed to convert bytes to string!");
1225             result = HAL_ERR_BUILD_PARAM_SET_FAILED;
1226         }
1227     }
1228     HcFree(infoHash.val);
1229     HcFree(message.val);
1230     return result;
1231 }