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