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