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