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