1 /*
2 * Copyright (C) 2021-2023 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.h"
17
18 #include "alg_defs.h"
19 #include "broadcast_manager.h"
20 #include "callback_manager.h"
21 #include "common_defs.h"
22 #include "cred_manager.h"
23 #include "data_manager.h"
24 #include "dev_auth_module_manager.h"
25 #include "device_auth_defines.h"
26 #include "group_manager_common.h"
27 #include "group_operation_common.h"
28 #include "hc_dev_info.h"
29 #include "hc_log.h"
30 #include "hisysevent_adapter.h"
31 #include "hitrace_adapter.h"
32 #include "os_account_adapter.h"
33 #include "task_manager.h"
34
35 #include "across_account_group.h"
36 #include "identical_account_group.h"
37 #include "peer_to_peer_group.h"
38
IsGroupTypeSupported(int groupType)39 static bool IsGroupTypeSupported(int groupType)
40 {
41 if (((groupType == PEER_TO_PEER_GROUP) && (IsPeerToPeerGroupSupported())) ||
42 ((groupType == IDENTICAL_ACCOUNT_GROUP) && (IsIdenticalAccountGroupSupported())) ||
43 ((groupType == ACROSS_ACCOUNT_AUTHORIZE_GROUP) && (IsAcrossAccountGroupSupported()))) {
44 return true;
45 }
46 LOGE("The group type is not supported! [GroupType]: %d", groupType);
47 return false;
48 }
49
RemoveNoPermissionGroup(int32_t osAccountId,GroupEntryVec * groupEntryVec,const char * appId)50 static void RemoveNoPermissionGroup(int32_t osAccountId, GroupEntryVec *groupEntryVec, const char *appId)
51 {
52 uint32_t index = 0;
53 TrustedGroupEntry **groupEntryPtr = NULL;
54 while (index < groupEntryVec->size(groupEntryVec)) {
55 groupEntryPtr = groupEntryVec->getp(groupEntryVec, index);
56 if (CheckGroupAccessible(osAccountId, StringGet(&(*groupEntryPtr)->id), appId) == HC_SUCCESS) {
57 index++;
58 continue;
59 }
60 LOGI("Remove a group without permission!");
61 TrustedGroupEntry *tempEntry = NULL;
62 HC_VECTOR_POPELEMENT(groupEntryVec, &tempEntry, index);
63 DestroyGroupEntry(tempEntry);
64 }
65 }
66
GenerateReturnEmptyArrayStr(char ** returnVec)67 static int32_t GenerateReturnEmptyArrayStr(char **returnVec)
68 {
69 CJson *json = CreateJsonArray();
70 if (json == NULL) {
71 LOGE("Failed to allocate json memory!");
72 return HC_ERR_JSON_FAIL;
73 }
74 *returnVec = PackJsonToString(json);
75 FreeJson(json);
76 if (*returnVec == NULL) {
77 LOGE("Failed to convert json to string!");
78 return HC_ERR_JSON_FAIL;
79 }
80 return HC_SUCCESS;
81 }
82
GenerateReturnGroupVec(GroupEntryVec * groupInfoVec,char ** returnGroupVec,uint32_t * groupNum)83 static int32_t GenerateReturnGroupVec(GroupEntryVec *groupInfoVec, char **returnGroupVec, uint32_t *groupNum)
84 {
85 if (HC_VECTOR_SIZE(groupInfoVec) == 0) {
86 *groupNum = 0;
87 return GenerateReturnEmptyArrayStr(returnGroupVec);
88 }
89
90 CJson *json = CreateJsonArray();
91 if (json == NULL) {
92 LOGE("Failed to allocate json memory!");
93 return HC_ERR_JSON_FAIL;
94 }
95 uint32_t groupCount = 0;
96 uint32_t index;
97 TrustedGroupEntry **groupInfoPtr = NULL;
98 FOR_EACH_HC_VECTOR(*groupInfoVec, index, groupInfoPtr) {
99 TrustedGroupEntry *groupInfo = *groupInfoPtr;
100 CJson *groupInfoJson = CreateJson();
101 if (groupInfoJson == NULL) {
102 LOGE("Failed to allocate groupInfoJson memory!");
103 FreeJson(json);
104 return HC_ERR_ALLOC_MEMORY;
105 }
106 int32_t result = GenerateReturnGroupInfo(groupInfo, groupInfoJson);
107 if (result != HC_SUCCESS) {
108 FreeJson(groupInfoJson);
109 FreeJson(json);
110 return result;
111 }
112 if (AddObjToArray(json, groupInfoJson) != HC_SUCCESS) {
113 LOGE("Failed to add groupInfoStr to returnGroupVec!");
114 FreeJson(groupInfoJson);
115 FreeJson(json);
116 return HC_ERR_JSON_FAIL;
117 }
118 ++groupCount;
119 }
120 *returnGroupVec = PackJsonToString(json);
121 FreeJson(json);
122 if ((*returnGroupVec) == NULL) {
123 LOGE("Failed to convert json to string!");
124 return HC_ERR_JSON_FAIL;
125 }
126 *groupNum = groupCount;
127 return HC_SUCCESS;
128 }
129
GenerateReturnDeviceVec(DeviceEntryVec * devInfoVec,char ** returnDevInfoVec,uint32_t * deviceNum)130 static int32_t GenerateReturnDeviceVec(DeviceEntryVec *devInfoVec, char **returnDevInfoVec, uint32_t *deviceNum)
131 {
132 CJson *json = CreateJsonArray();
133 if (json == NULL) {
134 LOGE("Failed to allocate json memory!");
135 return HC_ERR_JSON_FAIL;
136 }
137 uint32_t devCount = 0;
138 uint32_t index;
139 TrustedDeviceEntry **devInfoPtr = NULL;
140 FOR_EACH_HC_VECTOR(*devInfoVec, index, devInfoPtr) {
141 TrustedDeviceEntry *devInfo = (TrustedDeviceEntry*)(*devInfoPtr);
142 CJson *devInfoJson = CreateJson();
143 if (devInfoJson == NULL) {
144 LOGE("Failed to allocate devInfoJson memory!");
145 FreeJson(json);
146 return HC_ERR_ALLOC_MEMORY;
147 }
148 int32_t result = GenerateReturnDevInfo(devInfo, devInfoJson);
149 if (result != HC_SUCCESS) {
150 FreeJson(devInfoJson);
151 FreeJson(json);
152 return result;
153 }
154 if (AddObjToArray(json, devInfoJson) != HC_SUCCESS) {
155 LOGE("Failed to add devInfoStr to returnGroupVec!");
156 FreeJson(devInfoJson);
157 FreeJson(json);
158 return HC_ERR_JSON_FAIL;
159 }
160 ++devCount;
161 }
162 *returnDevInfoVec = PackJsonToString(json);
163 FreeJson(json);
164 if ((*returnDevInfoVec) == NULL) {
165 LOGE("Failed to convert json to string!");
166 return HC_ERR_JSON_FAIL;
167 }
168 *deviceNum = devCount;
169 return HC_SUCCESS;
170 }
171
IsQueryParamsValid(int groupType,const char * groupId,const char * groupName,const char * groupOwner)172 static bool IsQueryParamsValid(int groupType, const char *groupId, const char *groupName, const char *groupOwner)
173 {
174 if ((groupType == ALL_GROUP) && (groupId == NULL) && (groupName == NULL) && (groupOwner == NULL)) {
175 return false;
176 } else {
177 return true;
178 }
179 }
180
QueryRelatedGroupsForGetPk(int32_t osAccountId,const char * udid,GroupEntryVec * returnGroupEntryVec)181 static int32_t QueryRelatedGroupsForGetPk(int32_t osAccountId, const char *udid, GroupEntryVec *returnGroupEntryVec)
182 {
183 DeviceEntryVec deviceEntryVec = CreateDeviceEntryVec();
184 QueryDeviceParams params = InitQueryDeviceParams();
185 params.udid = udid;
186 int32_t result = QueryDevices(osAccountId, ¶ms, &deviceEntryVec);
187 if (result != HC_SUCCESS) {
188 LOGE("Failed to query trusted devices!");
189 ClearDeviceEntryVec(&deviceEntryVec);
190 return result;
191 }
192 uint32_t index;
193 TrustedDeviceEntry **entry = NULL;
194 FOR_EACH_HC_VECTOR(deviceEntryVec, index, entry) {
195 /* In order to improve availability, even if there is an error, it does not terminate. */
196 TrustedGroupEntry *groupEntry = GetGroupEntryById(osAccountId, StringGet(&(*entry)->groupId));
197 if (groupEntry == NULL) {
198 LOGW("An exception occurred! Device found, but group not found. There may be dirty data.");
199 continue;
200 }
201 if (groupEntry->visibility != GROUP_VISIBILITY_PUBLIC) {
202 DestroyGroupEntry(groupEntry);
203 continue;
204 }
205 if (returnGroupEntryVec->pushBackT(returnGroupEntryVec, groupEntry) == NULL) {
206 LOGW("An exception occurred! Failed to push groupEntry to returnGroupEntryVec!");
207 DestroyGroupEntry(groupEntry);
208 }
209 }
210 ClearDeviceEntryVec(&deviceEntryVec);
211 return HC_SUCCESS;
212 }
213
GetPkByParams(const char * groupId,const TrustedDeviceEntry * deviceEntry,char * returnPkHexStr,int32_t returnPkHexStrLen)214 static int32_t GetPkByParams(const char *groupId, const TrustedDeviceEntry *deviceEntry,
215 char *returnPkHexStr, int32_t returnPkHexStrLen)
216 {
217 /* Use the DeviceGroupManager package name. */
218 const char *appId = GROUP_MANAGER_PACKAGE_NAME;
219 int userType = deviceEntry->devType;
220 const char *authId = StringGet(&deviceEntry->authId);
221 Uint8Buff authIdBuff = { 0, 0 };
222 authIdBuff.length = HcStrlen(authId);
223 authIdBuff.val = (uint8_t *)HcMalloc(authIdBuff.length, 0);
224 if (authIdBuff.val == NULL) {
225 LOGE("Failed to allocate authIdBuff memory!");
226 return HC_ERR_ALLOC_MEMORY;
227 }
228 if (memcpy_s(authIdBuff.val, authIdBuff.length, authId, authIdBuff.length) != HC_SUCCESS) {
229 LOGE("Failed to copy authId!");
230 HcFree(authIdBuff.val);
231 return HC_ERR_MEMORY_COPY;
232 }
233 uint8_t returnPkBytes[PUBLIC_KEY_MAX_LENGTH] = { 0 };
234 Uint8Buff returnPkBuff = { 0, 0 };
235 returnPkBuff.length = PUBLIC_KEY_MAX_LENGTH;
236 returnPkBuff.val = returnPkBytes;
237 AuthModuleParams authParams = {
238 .pkgName = appId,
239 .serviceType = groupId,
240 .authId = &authIdBuff,
241 .userType = userType
242 };
243 int32_t res = GetPublicKey(DAS_MODULE, &authParams, &returnPkBuff);
244 HcFree(authIdBuff.val);
245 if (res != HC_SUCCESS) {
246 return res;
247 }
248 res = GetHashResult(returnPkBuff.val, returnPkBuff.length, returnPkHexStr, returnPkHexStrLen);
249 if (res != HC_SUCCESS) {
250 LOGE("Failed to get hash for pk!");
251 return HC_ERR_HASH_FAIL;
252 }
253 return HC_SUCCESS;
254 }
255
GeneratePkInfo(int32_t osAccountId,const char * queryUdid,const char * groupId,CJson * pkInfo)256 static int32_t GeneratePkInfo(int32_t osAccountId, const char *queryUdid, const char *groupId, CJson *pkInfo)
257 {
258 TrustedDeviceEntry *deviceEntry = GetTrustedDeviceEntryById(osAccountId, queryUdid, true, groupId);
259 if (deviceEntry == NULL) {
260 LOGE("The trusted device is not found!");
261 return HC_ERR_DEVICE_NOT_EXIST;
262 }
263 char returnPkHexStr[SHA256_LEN * BYTE_TO_HEX_OPER_LENGTH + 1] = { 0 };
264 int32_t result = GetPkByParams(groupId, deviceEntry, returnPkHexStr, sizeof(returnPkHexStr));
265 DestroyDeviceEntry(deviceEntry);
266 if (result != HC_SUCCESS) {
267 return result;
268 }
269 if (AddStringToJson(pkInfo, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
270 LOGE("Failed to add groupId to pkInfo!");
271 return HC_ERR_JSON_ADD;
272 }
273 if (AddStringToJson(pkInfo, FIELD_PUBLIC_KEY, returnPkHexStr) != HC_SUCCESS) {
274 LOGE("Failed to add publicKey to pkInfo!");
275 return HC_ERR_JSON_ADD;
276 }
277 return HC_SUCCESS;
278 }
279
AddAllPkInfoToList(int32_t osAccountId,const char * queryUdid,const GroupEntryVec * groupEntryVec,CJson * pkInfoList)280 static void AddAllPkInfoToList(int32_t osAccountId, const char *queryUdid, const GroupEntryVec *groupEntryVec,
281 CJson *pkInfoList)
282 {
283 uint32_t index;
284 TrustedGroupEntry **entry = NULL;
285 FOR_EACH_HC_VECTOR(*groupEntryVec, index, entry) {
286 /* Account related group cannot export public key. */
287 if (IsAccountRelatedGroup((*entry)->type)) {
288 continue;
289 }
290 const char *groupId = StringGet(&((*entry)->id));
291 CJson *pkInfo = CreateJson();
292 if (pkInfo == NULL) {
293 LOGE("Failed to create json!");
294 continue;
295 }
296 int32_t res = GeneratePkInfo(osAccountId, queryUdid, groupId, pkInfo);
297 if (res != HC_SUCCESS) {
298 FreeJson(pkInfo);
299 continue;
300 }
301 if (AddObjToArray(pkInfoList, pkInfo) != HC_SUCCESS) {
302 LOGE("Failed to add pkInfo to pkInfoList!");
303 FreeJson(pkInfo);
304 }
305 }
306 }
307
IsOnlyAccountRelatedGroups(const GroupEntryVec * groupEntryVec)308 static bool IsOnlyAccountRelatedGroups(const GroupEntryVec *groupEntryVec)
309 {
310 if (groupEntryVec->size(groupEntryVec) == 0) {
311 LOGW("No groups available.");
312 return false;
313 }
314 uint32_t index;
315 TrustedGroupEntry **entry = NULL;
316 FOR_EACH_HC_VECTOR(*groupEntryVec, index, entry) {
317 if (!IsAccountRelatedGroup((*entry)->type)) {
318 return false;
319 }
320 }
321 return true;
322 }
323
GeneratePkInfoList(int32_t osAccountId,const CJson * params,CJson * pkInfoList)324 static int32_t GeneratePkInfoList(int32_t osAccountId, const CJson *params, CJson *pkInfoList)
325 {
326 const char *udid = GetStringFromJson(params, FIELD_UDID);
327 if (udid == NULL) {
328 LOGE("Failed to get udid from params!");
329 return HC_ERR_JSON_GET;
330 }
331 bool isSelfPk = false;
332 if (GetBoolFromJson(params, FIELD_IS_SELF_PK, &isSelfPk) != HC_SUCCESS) {
333 LOGE("Failed to get isSelfPk from json!");
334 return HC_ERR_JSON_GET;
335 }
336 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
337 int32_t res = QueryRelatedGroupsForGetPk(osAccountId, udid, &groupEntryVec);
338 if (res != HC_SUCCESS) {
339 ClearGroupEntryVec(&groupEntryVec);
340 return res;
341 }
342 /**
343 * Specification requirements:
344 * when there are only account related groups in the group list of public key information to be queried,
345 * a special error code needs to be returned.
346 */
347 if (IsOnlyAccountRelatedGroups(&groupEntryVec)) {
348 LOGE("There are only account related groups in the group list.");
349 ClearGroupEntryVec(&groupEntryVec);
350 return HC_ERR_ONLY_ACCOUNT_RELATED;
351 }
352 const char *queryUdid = NULL;
353 char selfUdid[INPUT_UDID_LEN] = { 0 };
354 if (isSelfPk) {
355 res = HcGetUdid((uint8_t *)selfUdid, INPUT_UDID_LEN);
356 if (res != HC_SUCCESS) {
357 LOGE("Failed to get local udid! res: %d", res);
358 ClearGroupEntryVec(&groupEntryVec);
359 return HC_ERR_DB;
360 }
361 queryUdid = selfUdid;
362 } else {
363 queryUdid = udid;
364 }
365 AddAllPkInfoToList(osAccountId, queryUdid, &groupEntryVec, pkInfoList);
366 ClearGroupEntryVec(&groupEntryVec);
367 return HC_SUCCESS;
368 }
369
GetGroupInstance(int32_t groupType)370 static BaseGroup *GetGroupInstance(int32_t groupType)
371 {
372 if (!IsGroupTypeSupported(groupType)) {
373 return NULL;
374 }
375 BaseGroup *instance = NULL;
376 if (groupType == PEER_TO_PEER_GROUP) {
377 instance = GetPeerToPeerGroupInstance();
378 } else if (groupType == IDENTICAL_ACCOUNT_GROUP) {
379 instance = GetIdenticalAccountGroupInstance();
380 } else if (groupType == ACROSS_ACCOUNT_AUTHORIZE_GROUP) {
381 instance = GetAcrossAccountGroupInstance();
382 }
383 return instance;
384 }
385
CreateGroup(int32_t osAccountId,CJson * jsonParams,char ** returnJsonStr)386 static int32_t CreateGroup(int32_t osAccountId, CJson *jsonParams, char **returnJsonStr)
387 {
388 int32_t groupType = PEER_TO_PEER_GROUP;
389 if (GetIntFromJson(jsonParams, FIELD_GROUP_TYPE, &groupType) != HC_SUCCESS) {
390 LOGE("Failed to get groupType from jsonParams!");
391 return HC_ERR_JSON_GET;
392 }
393 BaseGroup *instance = GetGroupInstance(groupType);
394 if (instance == NULL) {
395 LOGE("The group instance is NULL or its function ptr is NULL!");
396 return HC_ERR_NULL_PTR;
397 }
398 return instance->createGroup(osAccountId, jsonParams, returnJsonStr);
399 }
400
DeleteGroup(int32_t osAccountId,CJson * jsonParams,char ** returnJsonStr)401 static int32_t DeleteGroup(int32_t osAccountId, CJson *jsonParams, char **returnJsonStr)
402 {
403 int32_t result;
404 const char *groupId = NULL;
405 const char *appId = NULL;
406 int32_t groupType = PEER_TO_PEER_GROUP;
407 if (((result = GetGroupIdFromJson(jsonParams, &groupId)) != HC_SUCCESS) ||
408 ((result = GetAppIdFromJson(jsonParams, &appId)) != HC_SUCCESS) ||
409 ((result = CheckGroupExist(osAccountId, groupId)) != HC_SUCCESS) ||
410 ((result = GetGroupTypeFromDb(osAccountId, groupId, &groupType)) != HC_SUCCESS) ||
411 ((result = CheckPermForGroup(osAccountId, GROUP_DISBAND, appId, groupId)) != HC_SUCCESS)) {
412 return result;
413 }
414 BaseGroup *instance = GetGroupInstance(groupType);
415 return instance->deleteGroup(osAccountId, jsonParams, returnJsonStr);
416 }
417
DeleteMemberFromPeerToPeerGroup(int32_t osAccountId,int64_t requestId,CJson * jsonParams,const DeviceAuthCallback * callback)418 static int32_t DeleteMemberFromPeerToPeerGroup(int32_t osAccountId, int64_t requestId, CJson *jsonParams,
419 const DeviceAuthCallback *callback)
420 {
421 if (!IsPeerToPeerGroupSupported()) {
422 LOGE("Peer to peer group is not supported!");
423 return HC_ERR_NOT_SUPPORT;
424 }
425 PeerToPeerGroup *instance = (PeerToPeerGroup *)GetPeerToPeerGroupInstance();
426 return instance->deleteMember(osAccountId, requestId, jsonParams, callback);
427 }
428
DoCreateGroup(HcTaskBase * baseTask)429 static void DoCreateGroup(HcTaskBase *baseTask)
430 {
431 GroupManagerTask *task = (GroupManagerTask *)baseTask;
432 SET_LOG_MODE(TRACE_MODE);
433 SET_TRACE_ID(task->reqId);
434 LOGI("[Start]: DoCreateGroup! [ReqId]: %" PRId64, task->reqId);
435 char *returnJsonStr = NULL;
436 int32_t result = CreateGroup(task->osAccountId, task->params, &returnJsonStr);
437 if (result != HC_SUCCESS) {
438 ProcessErrorCallback(task->reqId, GROUP_CREATE, result, NULL, task->cb);
439 } else {
440 ProcessFinishCallback(task->reqId, GROUP_CREATE, returnJsonStr, task->cb);
441 FreeJsonString(returnJsonStr);
442 }
443 }
444
DoDeleteGroup(HcTaskBase * baseTask)445 static void DoDeleteGroup(HcTaskBase *baseTask)
446 {
447 GroupManagerTask *task = (GroupManagerTask *)baseTask;
448 SET_LOG_MODE(TRACE_MODE);
449 SET_TRACE_ID(task->reqId);
450 LOGI("[Start]: DoDeleteGroup! [ReqId]: %" PRId64, task->reqId);
451 char *returnJsonStr = NULL;
452 int32_t result = DeleteGroup(task->osAccountId, task->params, &returnJsonStr);
453 if (result != HC_SUCCESS) {
454 ProcessErrorCallback(task->reqId, GROUP_DISBAND, result, NULL, task->cb);
455 } else {
456 ProcessFinishCallback(task->reqId, GROUP_DISBAND, returnJsonStr, task->cb);
457 FreeJsonString(returnJsonStr);
458 }
459 }
460
DoDeleteMember(HcTaskBase * baseTask)461 static void DoDeleteMember(HcTaskBase *baseTask)
462 {
463 GroupManagerTask *task = (GroupManagerTask *)baseTask;
464 SET_LOG_MODE(TRACE_MODE);
465 SET_TRACE_ID(task->reqId);
466 LOGI("[Start]: DoDeleteMember! [ReqId]: %" PRId64, task->reqId);
467 (void)DeleteMemberFromPeerToPeerGroup(task->osAccountId, task->reqId, task->params, task->cb);
468 }
469
CreateGroupInner(int32_t osAccountId,int64_t requestId,const char * appId,const char * createParams)470 static int32_t CreateGroupInner(int32_t osAccountId, int64_t requestId, const char *appId, const char *createParams)
471 {
472 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
473 if ((appId == NULL) || (createParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
474 LOGE("Invalid input parameters!");
475 return HC_ERR_INVALID_PARAMS;
476 }
477 if (!IsOsAccountUnlocked(osAccountId)) {
478 LOGE("Os account is not unlocked!");
479 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
480 }
481 LOGI("[Start]: [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
482 CJson *params = CreateJsonFromString(createParams);
483 if (params == NULL) {
484 LOGE("Failed to create json from string!");
485 return HC_ERR_JSON_FAIL;
486 }
487 int32_t result = AddBindParamsToJson(GROUP_CREATE, requestId, appId, params);
488 if (result != HC_SUCCESS) {
489 FreeJson(params);
490 return result;
491 }
492 if (InitAndPushGMTask(osAccountId, GROUP_CREATE, requestId, params, DoCreateGroup) != HC_SUCCESS) {
493 FreeJson(params);
494 return HC_ERR_INIT_TASK_FAIL;
495 }
496 LOGI("[End]: create group successfully!");
497 return HC_SUCCESS;
498 }
499
RequestCreateGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * createParams)500 static int32_t RequestCreateGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *createParams)
501 {
502 int32_t res = CreateGroupInner(osAccountId, requestId, appId, createParams);
503 DEV_AUTH_REPORT_CALL_EVENT(requestId, CREATE_GROUP_EVENT, appId, osAccountId, res);
504 return res;
505 }
506
DeleteGroupInner(int32_t osAccountId,int64_t requestId,const char * appId,const char * disbandParams)507 static int32_t DeleteGroupInner(int32_t osAccountId, int64_t requestId, const char *appId, const char *disbandParams)
508 {
509 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
510 if ((appId == NULL) || (disbandParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
511 LOGE("Invalid input parameters!");
512 return HC_ERR_INVALID_PARAMS;
513 }
514 if (!IsOsAccountUnlocked(osAccountId)) {
515 LOGE("Os account is not unlocked!");
516 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
517 }
518 LOGI("[Start]: [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
519 CJson *params = CreateJsonFromString(disbandParams);
520 if (params == NULL) {
521 LOGE("Failed to create json from string!");
522 return HC_ERR_JSON_FAIL;
523 }
524 int32_t result = AddBindParamsToJson(GROUP_DISBAND, requestId, appId, params);
525 if (result != HC_SUCCESS) {
526 FreeJson(params);
527 return result;
528 }
529 if (InitAndPushGMTask(osAccountId, GROUP_DISBAND, requestId, params, DoDeleteGroup) != HC_SUCCESS) {
530 FreeJson(params);
531 return HC_ERR_INIT_TASK_FAIL;
532 }
533 LOGI("[End]: delete group successfully!");
534 return HC_SUCCESS;
535 }
536
RequestDeleteGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * disbandParams)537 static int32_t RequestDeleteGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *disbandParams)
538 {
539 int32_t res = DeleteGroupInner(osAccountId, requestId, appId, disbandParams);
540 DEV_AUTH_REPORT_CALL_EVENT(requestId, DELETE_GROUP_EVENT, appId, osAccountId, res);
541 return res;
542 }
543
DeleteMemberFromGroupInner(int32_t osAccountId,int64_t requestId,const char * appId,const char * deleteParams)544 static int32_t DeleteMemberFromGroupInner(int32_t osAccountId, int64_t requestId, const char *appId,
545 const char *deleteParams)
546 {
547 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
548 if ((appId == NULL) || (deleteParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
549 LOGE("Invalid input parameters!");
550 return HC_ERR_INVALID_PARAMS;
551 }
552 if (!IsOsAccountUnlocked(osAccountId)) {
553 LOGE("Os account is not unlocked!");
554 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
555 }
556 LOGI("[Start]: [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
557 CJson *params = CreateJsonFromString(deleteParams);
558 if (params == NULL) {
559 LOGE("Failed to create json from string!");
560 return HC_ERR_JSON_FAIL;
561 }
562 int32_t result = AddBindParamsToJson(MEMBER_DELETE, requestId, appId, params);
563 if (result != HC_SUCCESS) {
564 FreeJson(params);
565 return result;
566 }
567 if (InitAndPushGMTask(osAccountId, MEMBER_DELETE, requestId, params, DoDeleteMember) != HC_SUCCESS) {
568 FreeJson(params);
569 return HC_ERR_INIT_TASK_FAIL;
570 }
571 LOGI("[End]: delete member from group successully!");
572 return HC_SUCCESS;
573 }
574
RequestDeleteMemberFromGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * deleteParams)575 static int32_t RequestDeleteMemberFromGroup(int32_t osAccountId, int64_t requestId, const char *appId,
576 const char *deleteParams)
577 {
578 int32_t res = DeleteMemberFromGroupInner(osAccountId, requestId, appId, deleteParams);
579 DEV_AUTH_REPORT_CALL_EVENT(requestId, DEL_MEMBER_EVENT, appId, osAccountId, res);
580 return res;
581 }
582
AddMultiMembersToGroupInner(int32_t osAccountId,const char * appId,const char * addParams)583 static int32_t AddMultiMembersToGroupInner(int32_t osAccountId, const char *appId, const char *addParams)
584 {
585 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
586 if ((appId == NULL) || (addParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
587 LOGE("Invalid input parameters!");
588 return HC_ERR_INVALID_PARAMS;
589 }
590 if (!IsOsAccountUnlocked(osAccountId)) {
591 LOGE("Os account is not unlocked!");
592 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
593 }
594 LOGI("[Start]: [AppId]: %s", appId);
595 CJson *params = CreateJsonFromString(addParams);
596 if (params == NULL) {
597 LOGE("Failed to create json from string!");
598 return HC_ERR_JSON_CREATE;
599 }
600 int32_t groupType = GROUP_TYPE_INVALID;
601 if (GetIntFromJson(params, FIELD_GROUP_TYPE, &groupType) != HC_SUCCESS) {
602 LOGE("Failed to get groupType from json!");
603 FreeJson(params);
604 return HC_ERR_JSON_GET;
605 }
606 if (!IsGroupTypeSupported(groupType)) {
607 FreeJson(params);
608 return HC_ERR_NOT_SUPPORT;
609 }
610 int32_t res;
611 if (groupType == IDENTICAL_ACCOUNT_GROUP) {
612 IdenticalAccountGroup *instance = (IdenticalAccountGroup *)GetIdenticalAccountGroupInstance();
613 res = instance->addMultiMembersToGroup(osAccountId, appId, params);
614 } else if (groupType == ACROSS_ACCOUNT_AUTHORIZE_GROUP) {
615 AcrossAccountGroup *instance = (AcrossAccountGroup *)GetAcrossAccountGroupInstance();
616 res = instance->addMultiMembersToGroup(osAccountId, appId, params);
617 } else {
618 LOGE("The input groupType is invalid! [GroupType]: %d", groupType);
619 res = HC_ERR_INVALID_PARAMS;
620 }
621 FreeJson(params);
622 LOGI("[End]: [Res]: %d!", res);
623 return res;
624 }
625
RequestAddMultiMembersToGroup(int32_t osAccountId,const char * appId,const char * addParams)626 static int32_t RequestAddMultiMembersToGroup(int32_t osAccountId, const char *appId, const char *addParams)
627 {
628 int32_t res = AddMultiMembersToGroupInner(osAccountId, appId, addParams);
629 DEV_AUTH_REPORT_CALL_EVENT(DEFAULT_REQUEST_ID, ADD_MULTI_MEMBER_EVENT, appId, osAccountId, res);
630 return res;
631 }
632
DelMultiMembersFromGroupInner(int32_t osAccountId,const char * appId,const char * deleteParams)633 static int32_t DelMultiMembersFromGroupInner(int32_t osAccountId, const char *appId, const char *deleteParams)
634 {
635 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
636 if ((appId == NULL) || (deleteParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
637 LOGE("Invalid input parameters!");
638 return HC_ERR_INVALID_PARAMS;
639 }
640 if (!IsOsAccountUnlocked(osAccountId)) {
641 LOGE("Os account is not unlocked!");
642 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
643 }
644 LOGI("[Start]: [AppId]: %s", appId);
645 CJson *params = CreateJsonFromString(deleteParams);
646 if (params == NULL) {
647 LOGE("Failed to create json from string!");
648 return HC_ERR_JSON_CREATE;
649 }
650 int32_t groupType = GROUP_TYPE_INVALID;
651 if (GetIntFromJson(params, FIELD_GROUP_TYPE, &groupType) != HC_SUCCESS) {
652 LOGE("Failed to get groupType from json!");
653 FreeJson(params);
654 return HC_ERR_JSON_GET;
655 }
656 if (!IsGroupTypeSupported(groupType)) {
657 FreeJson(params);
658 return HC_ERR_NOT_SUPPORT;
659 }
660 int32_t res;
661 if (groupType == IDENTICAL_ACCOUNT_GROUP) {
662 IdenticalAccountGroup *instance = (IdenticalAccountGroup *)GetIdenticalAccountGroupInstance();
663 res = instance->delMultiMembersFromGroup(osAccountId, appId, params);
664 } else if (groupType == ACROSS_ACCOUNT_AUTHORIZE_GROUP) {
665 AcrossAccountGroup *instance = (AcrossAccountGroup *)GetAcrossAccountGroupInstance();
666 res = instance->delMultiMembersFromGroup(osAccountId, appId, params);
667 } else {
668 LOGE("The input groupType is invalid! [GroupType]: %d", groupType);
669 res = HC_ERR_INVALID_PARAMS;
670 }
671 FreeJson(params);
672 LOGI("[End]: [Res]: %d!", res);
673 return res;
674 }
675
RequestDelMultiMembersFromGroup(int32_t osAccountId,const char * appId,const char * deleteParams)676 static int32_t RequestDelMultiMembersFromGroup(int32_t osAccountId, const char *appId, const char *deleteParams)
677 {
678 int32_t res = DelMultiMembersFromGroupInner(osAccountId, appId, deleteParams);
679 DEV_AUTH_REPORT_CALL_EVENT(DEFAULT_REQUEST_ID, DEL_MULTI_MEMBER_EVENT, appId, osAccountId, res);
680 return res;
681 }
682
RegListener(const char * appId,const DataChangeListener * listener)683 static int32_t RegListener(const char *appId, const DataChangeListener *listener)
684 {
685 if ((appId == NULL) || (listener == NULL)) {
686 LOGE("The input parameter contains NULL value!");
687 return HC_ERR_INVALID_PARAMS;
688 }
689 if (!IsBroadcastSupported()) {
690 LOGE("Broadcast is not supported!");
691 return HC_ERR_NOT_SUPPORT;
692 }
693 return AddListener(appId, listener);
694 }
695
UnRegListener(const char * appId)696 static int32_t UnRegListener(const char *appId)
697 {
698 if (appId == NULL) {
699 LOGE("The input appId is NULL!");
700 return HC_ERR_INVALID_PARAMS;
701 }
702 if (!IsBroadcastSupported()) {
703 LOGE("Broadcast is not supported!");
704 return HC_ERR_NOT_SUPPORT;
705 }
706 return RemoveListener(appId);
707 }
708
GetRegisterInfo(const char * reqJsonStr,char ** returnRegisterInfo)709 static int32_t GetRegisterInfo(const char *reqJsonStr, char **returnRegisterInfo)
710 {
711 if ((reqJsonStr == NULL) || (returnRegisterInfo == NULL)) {
712 LOGE("The input param is NULL!");
713 return HC_ERR_INVALID_PARAMS;
714 }
715 CJson *requestJson = CreateJsonFromString(reqJsonStr);
716 if (requestJson == NULL) {
717 LOGE("Failed to create request json!");
718 return HC_ERR_JSON_CREATE;
719 }
720 if (AddIntToJson(requestJson, FIELD_CREDENTIAL_TYPE, ASYMMETRIC_CRED) != HC_SUCCESS) {
721 LOGE("Failed to add credentialType to input json!");
722 FreeJson(requestJson);
723 return HC_ERR_JSON_GET;
724 }
725 CJson *registerInfo = CreateJson();
726 if (registerInfo == NULL) {
727 LOGE("Failed to allocate registerInfo memory!");
728 FreeJson(requestJson);
729 return HC_ERR_JSON_CREATE;
730 }
731 int32_t result = ProcCred(ACCOUNT_RELATED_PLUGIN, 0, REQUEST_SIGNATURE, requestJson, registerInfo);
732 FreeJson(requestJson);
733 if (result != HC_SUCCESS) {
734 LOGE("Failed to get register info!");
735 FreeJson(registerInfo);
736 return result;
737 }
738 *returnRegisterInfo = PackJsonToString(registerInfo);
739 FreeJson(registerInfo);
740 if (*returnRegisterInfo == NULL) {
741 LOGE("Failed to convert json to string!");
742 return HC_ERR_PACKAGE_JSON_TO_STRING_FAIL;
743 }
744 return HC_SUCCESS;
745 }
746
CheckAccessToGroup(int32_t osAccountId,const char * appId,const char * groupId)747 static int32_t CheckAccessToGroup(int32_t osAccountId, const char *appId, const char *groupId)
748 {
749 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
750 if ((appId == NULL) || (groupId == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
751 LOGE("Invalid input parameters!");
752 return HC_ERR_INVALID_PARAMS;
753 }
754 if (!IsOsAccountUnlocked(osAccountId)) {
755 LOGE("Os account is not unlocked!");
756 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
757 }
758 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
759 LOGE("You do not have the permission to query the group information!");
760 return HC_ERR_ACCESS_DENIED;
761 }
762 return HC_SUCCESS;
763 }
764
GetAccessibleGroupInfoById(int32_t osAccountId,const char * appId,const char * groupId,char ** returnGroupInfo)765 static int32_t GetAccessibleGroupInfoById(int32_t osAccountId, const char *appId, const char *groupId,
766 char **returnGroupInfo)
767 {
768 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
769 if ((appId == NULL) || (groupId == NULL) || (returnGroupInfo == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
770 LOGE("Invalid input parameters!");
771 return HC_ERR_INVALID_PARAMS;
772 }
773 if (!IsOsAccountUnlocked(osAccountId)) {
774 LOGE("Os account is not unlocked!");
775 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
776 }
777 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
778 LOGE("No group is found based on the query parameters!");
779 return HC_ERR_GROUP_NOT_EXIST;
780 }
781 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
782 LOGE("You do not have the permission to query the group information!");
783 return HC_ERR_ACCESS_DENIED;
784 }
785 TrustedGroupEntry *groupEntry = GetGroupEntryById(osAccountId, groupId);
786 if (groupEntry == NULL) {
787 LOGE("Failed to get groupEntry from db!");
788 return HC_ERR_DB;
789 }
790 CJson *groupInfoJson = CreateJson();
791 if (groupInfoJson == NULL) {
792 LOGE("Failed to allocate groupInfoJson memory!");
793 DestroyGroupEntry(groupEntry);
794 return HC_ERR_JSON_FAIL;
795 }
796 int32_t result = GenerateReturnGroupInfo(groupEntry, groupInfoJson);
797 DestroyGroupEntry(groupEntry);
798 if (result != HC_SUCCESS) {
799 FreeJson(groupInfoJson);
800 return result;
801 }
802 *returnGroupInfo = PackJsonToString(groupInfoJson);
803 FreeJson(groupInfoJson);
804 if (*returnGroupInfo == NULL) {
805 LOGE("Failed to convert json to string!");
806 return HC_ERR_JSON_FAIL;
807 }
808 return HC_SUCCESS;
809 }
810
GetAccessibleGroupInfo(int32_t osAccountId,const char * appId,const char * queryParams,char ** returnGroupVec,uint32_t * groupNum)811 static int32_t GetAccessibleGroupInfo(int32_t osAccountId, const char *appId, const char *queryParams,
812 char **returnGroupVec, uint32_t *groupNum)
813 {
814 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
815 if ((appId == NULL) || (queryParams == NULL) || (returnGroupVec == NULL) || (groupNum == NULL) ||
816 (osAccountId == INVALID_OS_ACCOUNT)) {
817 LOGE("Invalid input parameters!");
818 return HC_ERR_INVALID_PARAMS;
819 }
820 if (!IsOsAccountUnlocked(osAccountId)) {
821 LOGE("Os account is not unlocked!");
822 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
823 }
824 CJson *queryParamsJson = CreateJsonFromString(queryParams);
825 if (queryParamsJson == NULL) {
826 LOGE("Failed to create queryParamsJson from string!");
827 return HC_ERR_JSON_FAIL;
828 }
829 int32_t groupType = ALL_GROUP;
830 (void)GetIntFromJson(queryParamsJson, FIELD_GROUP_TYPE, &groupType);
831 if ((groupType != ALL_GROUP) && (!IsGroupTypeSupported(groupType))) {
832 LOGE("Invalid group type!");
833 FreeJson(queryParamsJson);
834 return HC_ERR_INVALID_PARAMS;
835 }
836 const char *groupId = GetStringFromJson(queryParamsJson, FIELD_GROUP_ID);
837 const char *groupName = GetStringFromJson(queryParamsJson, FIELD_GROUP_NAME);
838 const char *groupOwner = GetStringFromJson(queryParamsJson, FIELD_GROUP_OWNER);
839 if (!IsQueryParamsValid(groupType, groupId, groupName, groupOwner)) {
840 LOGE("The query parameters cannot be all null!");
841 FreeJson(queryParamsJson);
842 return HC_ERR_INVALID_PARAMS;
843 }
844 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
845 QueryGroupParams params = InitQueryGroupParams();
846 params.groupId = groupId;
847 params.groupName = groupName;
848 params.ownerName = groupOwner;
849 params.groupType = groupType;
850 int32_t result = GetGroupInfo(osAccountId, ¶ms, &groupEntryVec);
851 FreeJson(queryParamsJson);
852 if (result != HC_SUCCESS) {
853 ClearGroupEntryVec(&groupEntryVec);
854 return result;
855 }
856 RemoveNoPermissionGroup(osAccountId, &groupEntryVec, appId);
857 result = GenerateReturnGroupVec(&groupEntryVec, returnGroupVec, groupNum);
858 ClearGroupEntryVec(&groupEntryVec);
859 return result;
860 }
861
GetAccessibleJoinedGroups(int32_t osAccountId,const char * appId,int groupType,char ** returnGroupVec,uint32_t * groupNum)862 static int32_t GetAccessibleJoinedGroups(int32_t osAccountId, const char *appId, int groupType,
863 char **returnGroupVec, uint32_t *groupNum)
864 {
865 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
866 if ((appId == NULL) || (returnGroupVec == NULL) || (groupNum == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
867 LOGE("Invalid input parameters!");
868 return HC_ERR_INVALID_PARAMS;
869 }
870 if (!IsOsAccountUnlocked(osAccountId)) {
871 LOGE("Os account is not unlocked!");
872 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
873 }
874 if (!IsGroupTypeSupported(groupType)) {
875 LOGE("Invalid group type!");
876 return HC_ERR_INVALID_PARAMS;
877 }
878 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
879 int32_t result = GetJoinedGroups(osAccountId, groupType, &groupEntryVec);
880 if (result != HC_SUCCESS) {
881 ClearGroupEntryVec(&groupEntryVec);
882 return result;
883 }
884 RemoveNoPermissionGroup(osAccountId, &groupEntryVec, appId);
885 result = GenerateReturnGroupVec(&groupEntryVec, returnGroupVec, groupNum);
886 ClearGroupEntryVec(&groupEntryVec);
887 return result;
888 }
889
GetAccessibleRelatedGroups(int32_t osAccountId,const char * appId,const char * peerDeviceId,char ** returnGroupVec,uint32_t * groupNum)890 static int32_t GetAccessibleRelatedGroups(int32_t osAccountId, const char *appId, const char *peerDeviceId,
891 char **returnGroupVec, uint32_t *groupNum)
892 {
893 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
894 if ((appId == NULL) || (peerDeviceId == NULL) || (returnGroupVec == NULL) || (groupNum == NULL) ||
895 (osAccountId == INVALID_OS_ACCOUNT)) {
896 LOGE("Invalid input parameters!");
897 return HC_ERR_INVALID_PARAMS;
898 }
899 if (!IsOsAccountUnlocked(osAccountId)) {
900 LOGE("Os account is not unlocked!");
901 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
902 }
903 PRINT_SENSITIVE_DATA("PeerUdid", peerDeviceId);
904 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
905 int32_t result = GetRelatedGroups(osAccountId, peerDeviceId, true, &groupEntryVec);
906 if (result != HC_SUCCESS) {
907 LOGE("Failed to get related groups by udid!");
908 ClearGroupEntryVec(&groupEntryVec);
909 return result;
910 }
911 if (groupEntryVec.size(&groupEntryVec) == 0) {
912 LOGI("Group entry not found by udid, try to get by authId!");
913 result = GetRelatedGroups(osAccountId, peerDeviceId, false, &groupEntryVec);
914 if (result != HC_SUCCESS) {
915 LOGE("Failed to get related groups by authId!");
916 ClearGroupEntryVec(&groupEntryVec);
917 return result;
918 }
919 }
920 RemoveNoPermissionGroup(osAccountId, &groupEntryVec, appId);
921 result = GenerateReturnGroupVec(&groupEntryVec, returnGroupVec, groupNum);
922 ClearGroupEntryVec(&groupEntryVec);
923 return result;
924 }
925
GetAccessibleDeviceInfoById(int32_t osAccountId,const char * appId,const DeviceQueryParams * devQueryParams,const char * groupId,char ** returnDeviceInfo)926 static int32_t GetAccessibleDeviceInfoById(int32_t osAccountId, const char *appId,
927 const DeviceQueryParams *devQueryParams, const char *groupId, char **returnDeviceInfo)
928 {
929 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
930 if ((appId == NULL) || (devQueryParams == NULL) || (devQueryParams->deviceId == NULL) ||
931 (groupId == NULL) || (returnDeviceInfo == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
932 LOGE("Invalid input parameters!");
933 return HC_ERR_INVALID_PARAMS;
934 }
935 if (!IsOsAccountUnlocked(osAccountId)) {
936 LOGE("Os account is not unlocked!");
937 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
938 }
939 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
940 LOGE("No group is found based on the query parameters!");
941 return HC_ERR_GROUP_NOT_EXIST;
942 }
943 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
944 LOGE("You do not have the permission to query the group information!");
945 return HC_ERR_ACCESS_DENIED;
946 }
947 TrustedDeviceEntry *deviceEntry = CreateDeviceEntry();
948 if (deviceEntry == NULL) {
949 LOGE("Failed to allocate deviceEntry memory!");
950 return HC_ERR_ALLOC_MEMORY;
951 }
952 if (GetTrustedDevInfoById(osAccountId, devQueryParams->deviceId,
953 devQueryParams->isUdid, groupId, deviceEntry) != HC_SUCCESS) {
954 LOGE("No device is found based on the query parameters!");
955 DestroyDeviceEntry(deviceEntry);
956 return HC_ERR_DEVICE_NOT_EXIST;
957 }
958 CJson *devInfoJson = CreateJson();
959 if (devInfoJson == NULL) {
960 LOGE("Failed to allocate devInfoJson memory!");
961 DestroyDeviceEntry(deviceEntry);
962 return HC_ERR_JSON_FAIL;
963 }
964 int32_t result = GenerateReturnDevInfo(deviceEntry, devInfoJson);
965 DestroyDeviceEntry(deviceEntry);
966 if (result != HC_SUCCESS) {
967 FreeJson(devInfoJson);
968 return result;
969 }
970 *returnDeviceInfo = PackJsonToString(devInfoJson);
971 FreeJson(devInfoJson);
972 if (*returnDeviceInfo == NULL) {
973 LOGE("Failed to convert json to string!");
974 return HC_ERR_JSON_FAIL;
975 }
976 return HC_SUCCESS;
977 }
978
GetAccessibleTrustedDevices(int32_t osAccountId,const char * appId,const char * groupId,char ** returnDevInfoVec,uint32_t * deviceNum)979 static int32_t GetAccessibleTrustedDevices(int32_t osAccountId, const char *appId, const char *groupId,
980 char **returnDevInfoVec, uint32_t *deviceNum)
981 {
982 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
983 if ((appId == NULL) || (groupId == NULL) || (returnDevInfoVec == NULL) || (deviceNum == NULL) ||
984 (osAccountId == INVALID_OS_ACCOUNT)) {
985 LOGE("Invalid input parameters!");
986 return HC_ERR_INVALID_PARAMS;
987 }
988 if (!IsOsAccountUnlocked(osAccountId)) {
989 LOGE("Os account is not unlocked!");
990 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
991 }
992 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
993 LOGE("No group is found based on the query parameters!");
994 return HC_ERR_GROUP_NOT_EXIST;
995 }
996 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
997 LOGE("You do not have the permission to query the group information!");
998 return HC_ERR_ACCESS_DENIED;
999 }
1000 DeviceEntryVec deviceEntryVec = CreateDeviceEntryVec();
1001 int32_t result = GetTrustedDevices(osAccountId, groupId, &deviceEntryVec);
1002 if (result != HC_SUCCESS) {
1003 ClearDeviceEntryVec(&deviceEntryVec);
1004 return result;
1005 }
1006 result = GenerateReturnDeviceVec(&deviceEntryVec, returnDevInfoVec, deviceNum);
1007 ClearDeviceEntryVec(&deviceEntryVec);
1008 return result;
1009 }
1010
IsDeviceInAccessibleGroup(int32_t osAccountId,const char * appId,const char * groupId,const char * deviceId,bool isUdid)1011 static bool IsDeviceInAccessibleGroup(int32_t osAccountId, const char *appId, const char *groupId,
1012 const char *deviceId, bool isUdid)
1013 {
1014 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
1015 if ((appId == NULL) || (groupId == NULL) || (deviceId == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
1016 LOGE("Invalid input parameters!");
1017 return false;
1018 }
1019 if (!IsOsAccountUnlocked(osAccountId)) {
1020 LOGE("Os account is not unlocked!");
1021 return false;
1022 }
1023 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
1024 LOGE("No group is found based on the query parameters!");
1025 return false;
1026 }
1027 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
1028 LOGE("You do not have the permission to query the group information!");
1029 return false;
1030 }
1031 return IsTrustedDeviceInGroup(osAccountId, groupId, deviceId, isUdid);
1032 }
1033
GetPkInfoList(int32_t osAccountId,const char * appId,const char * queryParams,char ** returnInfoList,uint32_t * returnInfoNum)1034 static int32_t GetPkInfoList(int32_t osAccountId, const char *appId, const char *queryParams,
1035 char **returnInfoList, uint32_t *returnInfoNum)
1036 {
1037 LOGI("[Start]: start to get pk list!");
1038 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
1039 if ((appId == NULL) || (queryParams == NULL) || (returnInfoList == NULL) ||
1040 (returnInfoNum == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
1041 LOGE("Invalid input parameters!");
1042 return HC_ERR_INVALID_PARAMS;
1043 }
1044 if (!IsOsAccountUnlocked(osAccountId)) {
1045 LOGE("Os account is not unlocked!");
1046 return HC_ERR_OS_ACCOUNT_NOT_UNLOCKED;
1047 }
1048 CJson *params = CreateJsonFromString(queryParams);
1049 if (params == NULL) {
1050 LOGE("Failed to create json from string!");
1051 return HC_ERR_JSON_CREATE;
1052 }
1053 CJson *pkInfoList = CreateJsonArray();
1054 if (pkInfoList == NULL) {
1055 LOGE("Failed to create json array!");
1056 FreeJson(params);
1057 return HC_ERR_JSON_CREATE;
1058 }
1059 int32_t res = GeneratePkInfoList(osAccountId, params, pkInfoList);
1060 FreeJson(params);
1061 if (res != HC_SUCCESS) {
1062 FreeJson(pkInfoList);
1063 return res;
1064 }
1065 int32_t pkInfoNum = GetItemNum(pkInfoList);
1066 char *pkInfoListStr = PackJsonToString(pkInfoList);
1067 FreeJson(pkInfoList);
1068 if (pkInfoListStr == NULL) {
1069 LOGE("Failed to convert json to string!");
1070 return HC_ERR_PACKAGE_JSON_TO_STRING_FAIL;
1071 }
1072 *returnInfoList = pkInfoListStr;
1073 *returnInfoNum = pkInfoNum;
1074 LOGI("[End]: Get pk list successfully! [PkInfoNum]: %" PRId32, pkInfoNum);
1075 return HC_SUCCESS;
1076 }
1077
DestroyInfo(char ** returnInfo)1078 static void DestroyInfo(char **returnInfo)
1079 {
1080 if ((returnInfo == NULL) || (*returnInfo == NULL)) {
1081 return;
1082 }
1083 FreeJsonString(*returnInfo);
1084 *returnInfo = NULL;
1085 }
1086
1087 static const GroupImpl GROUP_IMPL_INSTANCE = {
1088 .createGroup = RequestCreateGroup,
1089 .deleteGroup = RequestDeleteGroup,
1090 .deleteMember = RequestDeleteMemberFromGroup,
1091 .addMultiMembers = RequestAddMultiMembersToGroup,
1092 .delMultiMembers = RequestDelMultiMembersFromGroup,
1093 .regListener = RegListener,
1094 .unRegListener = UnRegListener,
1095 .getRegisterInfo = GetRegisterInfo,
1096 .checkAccessToGroup = CheckAccessToGroup,
1097 .getAccessibleGroupInfoById = GetAccessibleGroupInfoById,
1098 .getAccessibleGroupInfo = GetAccessibleGroupInfo,
1099 .getAccessibleJoinedGroups = GetAccessibleJoinedGroups,
1100 .getAccessibleRelatedGroups = GetAccessibleRelatedGroups,
1101 .getAccessibleDeviceInfoById = GetAccessibleDeviceInfoById,
1102 .getAccessibleTrustedDevices = GetAccessibleTrustedDevices,
1103 .isDeviceInAccessibleGroup = IsDeviceInAccessibleGroup,
1104 .getPkInfoList = GetPkInfoList,
1105 .destroyInfo = DestroyInfo
1106 };
1107
InitGroupRelatedModule(void)1108 int32_t InitGroupRelatedModule(void)
1109 {
1110 if (IsBroadcastSupported()) {
1111 if (InitBroadcastManager() != HC_SUCCESS) {
1112 LOGE("[End]: [Service]: Failed to init broadcast manage module!");
1113 return HC_ERR_SERVICE_NEED_RESTART;
1114 }
1115 }
1116 return HC_SUCCESS;
1117 }
1118
DestroyGroupRelatedModule(void)1119 void DestroyGroupRelatedModule(void)
1120 {
1121 DestroyBroadcastManager();
1122 }
1123
GetGroupImplInstance(void)1124 const GroupImpl *GetGroupImplInstance(void)
1125 {
1126 return &GROUP_IMPL_INSTANCE;
1127 }
1128
IsGroupSupport(void)1129 bool IsGroupSupport(void)
1130 {
1131 return true;
1132 }