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