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