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