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