1 /*
2 * Copyright (C) 2022-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_auth_data_operation.h"
17 #include "common_defs.h"
18 #include "device_auth.h"
19 #include "device_auth_defines.h"
20 #include "hc_dev_info.h"
21 #include "hc_log.h"
22 #include "hc_string.h"
23 #include "hc_types.h"
24 #include "hc_vector.h"
25
GaDeepCopyDeviceEntry(const TrustedDeviceEntry * entry,TrustedDeviceEntry * returnEntry)26 static bool GaDeepCopyDeviceEntry(const TrustedDeviceEntry *entry, TrustedDeviceEntry *returnEntry)
27 {
28 returnEntry->groupEntry = NULL;
29 if (!StringSet(&returnEntry->groupId, entry->groupId)) {
30 LOGE("[GA]: Failed to copy udid!");
31 return false;
32 }
33 if (!StringSet(&returnEntry->udid, entry->udid)) {
34 LOGE("[GA]: Failed to copy udid!");
35 return false;
36 }
37 if (!StringSet(&returnEntry->authId, entry->authId)) {
38 LOGE("[GA]: Failed to copy authId!");
39 return false;
40 }
41 if (!StringSet(&returnEntry->serviceType, entry->serviceType)) {
42 LOGE("[GA]: Failed to copy serviceType!");
43 return false;
44 }
45 if (!StringSet(&returnEntry->userId, entry->userId)) {
46 LOGE("[GA]: Failed to copy userId!");
47 return false;
48 }
49 returnEntry->credential = entry->credential;
50 returnEntry->devType = entry->devType;
51 returnEntry->lastTm = entry->lastTm;
52 returnEntry->source = entry->source;
53 return true;
54 }
55
GaDeepCopyGroupEntry(const TrustedGroupEntry * entry,TrustedGroupEntry * returnEntry)56 static bool GaDeepCopyGroupEntry(const TrustedGroupEntry *entry, TrustedGroupEntry *returnEntry)
57 {
58 if (HC_VECTOR_SIZE(&entry->managers) <= 0) {
59 LOGE("[GA]: The group owner is lost!");
60 return false;
61 }
62 HcString entryOwner = HC_VECTOR_GET(&entry->managers, 0);
63 if (!StringSet(&returnEntry->name, entry->name)) {
64 LOGE("[GA]: Failed to copy groupName!");
65 return false;
66 }
67 if (!StringSet(&returnEntry->id, entry->id)) {
68 LOGE("[GA]: Failed to copy groupId!");
69 return false;
70 }
71 if (!StringSet(&returnEntry->userId, entry->userId)) {
72 LOGE("[GA]: Failed to copy userId!");
73 return false;
74 }
75 returnEntry->type = entry->type;
76 returnEntry->visibility = entry->visibility;
77 returnEntry->expireTime = entry->expireTime;
78 HcString ownerName = CreateString();
79 if (!StringSet(&ownerName, entryOwner)) {
80 LOGE("[GA]: Failed to copy groupOwner!");
81 DeleteString(&ownerName);
82 return false;
83 }
84 if (returnEntry->managers.pushBack(&returnEntry->managers, &ownerName) == NULL) {
85 LOGE("[GA]: Failed to push groupOwner to managers!");
86 DeleteString(&ownerName);
87 return false;
88 }
89 return true;
90 }
91
GaIsGroupManager(const char * appId,const TrustedGroupEntry * entry)92 static bool GaIsGroupManager(const char *appId, const TrustedGroupEntry *entry)
93 {
94 uint32_t index;
95 HcString *manager = NULL;
96 const char *managerStr = NULL;
97 FOR_EACH_HC_VECTOR(entry->managers, index, manager) {
98 managerStr = StringGet(manager);
99 if ((managerStr != NULL) && (strcmp(managerStr, appId) == 0)) {
100 return true;
101 }
102 }
103 return false;
104 }
105
GaIsGroupFriend(const char * appId,const TrustedGroupEntry * entry)106 static bool GaIsGroupFriend(const char *appId, const TrustedGroupEntry *entry)
107 {
108 uint32_t index;
109 HcString *trustedFriend = NULL;
110 const char *friendStr = NULL;
111 FOR_EACH_HC_VECTOR(entry->friends, index, trustedFriend) {
112 friendStr = StringGet(trustedFriend);
113 if ((friendStr != NULL) && (strcmp(friendStr, appId) == 0)) {
114 return true;
115 }
116 }
117 return false;
118 }
119
GetGroupEntryById(int32_t osAccountId,const char * groupId,TrustedGroupEntry * returnEntry)120 static int32_t GetGroupEntryById(int32_t osAccountId, const char *groupId, TrustedGroupEntry *returnEntry)
121 {
122 if (returnEntry == NULL) {
123 LOGE("The input returnEntry is NULL!");
124 return HC_ERR_INVALID_PARAMS;
125 }
126 uint32_t groupIndex;
127 TrustedGroupEntry **entry = NULL;
128 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
129 QueryGroupParams groupParams = InitQueryGroupParams();
130 groupParams.groupId = groupId;
131 if (QueryGroups(osAccountId, &groupParams, &groupEntryVec) != HC_SUCCESS) {
132 LOGE("query groups failed!");
133 ClearGroupEntryVec(&groupEntryVec);
134 return HC_ERR_GROUP_NOT_EXIST;
135 }
136 FOR_EACH_HC_VECTOR(groupEntryVec, groupIndex, entry) {
137 if (!GaDeepCopyGroupEntry(*entry, returnEntry)) {
138 ClearGroupEntryVec(&groupEntryVec);
139 return HC_ERR_GROUP_NOT_EXIST;
140 }
141 ClearGroupEntryVec(&groupEntryVec);
142 return HC_SUCCESS;
143 }
144 ClearGroupEntryVec(&groupEntryVec);
145 return HC_ERR_GROUP_NOT_EXIST;
146 }
147
GaIsGroupAccessible(int32_t osAccountId,const char * groupId,const char * appId)148 bool GaIsGroupAccessible(int32_t osAccountId, const char *groupId, const char *appId)
149 {
150 if ((groupId == NULL) || (appId == NULL)) {
151 LOGE("The input groupId or appId is NULL!");
152 return false;
153 }
154 TrustedGroupEntry *entry = CreateGroupEntry();
155 if (entry == NULL) {
156 LOGE("Failed to create group entry!");
157 return false;
158 }
159 int32_t res = GetGroupEntryById(osAccountId, groupId, entry);
160 if (res != HC_SUCCESS) {
161 LOGE("Failed to get group entry by groupId!");
162 DestroyGroupEntry(entry);
163 return false;
164 }
165 if ((entry->visibility == GROUP_VISIBILITY_PUBLIC) ||
166 (GaIsGroupManager(appId, entry)) ||
167 (GaIsGroupFriend(appId, entry))) {
168 DestroyGroupEntry(entry);
169 return true;
170 }
171 DestroyGroupEntry(entry);
172 return false;
173 }
174
GaGetTrustedDeviceEntryById(int32_t osAccountId,const char * deviceId,bool isUdid,const char * groupId,TrustedDeviceEntry * returnDeviceEntry)175 int32_t GaGetTrustedDeviceEntryById(int32_t osAccountId, const char *deviceId,
176 bool isUdid, const char *groupId, TrustedDeviceEntry *returnDeviceEntry)
177 {
178 if (returnDeviceEntry == NULL) {
179 LOGE("The input returnEntry is NULL!");
180 return HC_ERR_INVALID_PARAMS;
181 }
182 uint32_t index;
183 TrustedDeviceEntry **deviceEntry = NULL;
184 DeviceEntryVec deviceEntryVec = CREATE_HC_VECTOR(DeviceEntryVec);
185 QueryDeviceParams params = InitQueryDeviceParams();
186 params.groupId = groupId;
187 if (isUdid) {
188 params.udid = deviceId;
189 } else {
190 params.authId = deviceId;
191 }
192 if (QueryDevices(osAccountId, ¶ms, &deviceEntryVec) != HC_SUCCESS) {
193 LOGE("query trusted devices failed!");
194 ClearDeviceEntryVec(&deviceEntryVec);
195 return HC_ERR_DEVICE_NOT_EXIST;
196 }
197 FOR_EACH_HC_VECTOR(deviceEntryVec, index, deviceEntry) {
198 if (!GaDeepCopyDeviceEntry(*deviceEntry, returnDeviceEntry)) {
199 ClearDeviceEntryVec(&deviceEntryVec);
200 return HC_ERR_GROUP_NOT_EXIST;
201 }
202 ClearDeviceEntryVec(&deviceEntryVec);
203 return HC_SUCCESS;
204 }
205 ClearDeviceEntryVec(&deviceEntryVec);
206 return HC_ERR_DEVICE_NOT_EXIST;
207 }
208
GaIsDeviceInGroup(int32_t groupType,int32_t osAccountId,const char * peerUdid,const char * peerAuthId,const char * groupId)209 bool GaIsDeviceInGroup(int32_t groupType, int32_t osAccountId, const char *peerUdid, const char *peerAuthId,
210 const char *groupId)
211 {
212 int32_t res;
213 int32_t authForm = GroupTypeToAuthForm(groupType);
214 if ((authForm == AUTH_FORM_ACROSS_ACCOUNT) || (authForm == AUTH_FORM_IDENTICAL_ACCOUNT)) {
215 LOGD("Auth for account related type.");
216 return true; /* Do not check whether account related devices is in account. */
217 }
218 TrustedDeviceEntry *deviceEntry = CreateDeviceEntry();
219 if (deviceEntry == NULL) {
220 LOGE("Failed to allocate memory for deviceEntry!");
221 return false;
222 }
223 if (peerUdid != NULL) {
224 res = GaGetTrustedDeviceEntryById(osAccountId, peerUdid, true, groupId, deviceEntry);
225 } else if (peerAuthId != NULL) {
226 res = GaGetTrustedDeviceEntryById(osAccountId, peerAuthId, false, groupId, deviceEntry);
227 } else {
228 LOGE("Both the input udid and authId is null!");
229 res = HC_ERROR;
230 }
231 DestroyDeviceEntry(deviceEntry);
232 if (res != HC_SUCCESS) {
233 return false;
234 }
235 return true;
236 }
237
GaGetLocalDeviceInfo(int32_t osAccountId,const char * groupId,TrustedDeviceEntry * localAuthInfo)238 int32_t GaGetLocalDeviceInfo(int32_t osAccountId, const char *groupId, TrustedDeviceEntry *localAuthInfo)
239 {
240 char *localUdid = (char *)HcMalloc(INPUT_UDID_LEN, 0);
241 if (localUdid == NULL) {
242 LOGE("Failed to malloc for local udid!");
243 return HC_ERR_ALLOC_MEMORY;
244 }
245 int32_t res = HcGetUdid((uint8_t *)localUdid, INPUT_UDID_LEN);
246 if (res != HC_SUCCESS) {
247 LOGE("Failed to get local udid!");
248 HcFree(localUdid);
249 return res;
250 }
251 PRINT_SENSITIVE_DATA("SelfUdid", localUdid);
252 res = GaGetTrustedDeviceEntryById(osAccountId, localUdid, true, groupId, localAuthInfo);
253 HcFree(localUdid);
254 if (res != HC_SUCCESS) {
255 LOGE("Failed to get local device info from database!");
256 }
257 return res;
258 }
259
AuthFormToGroupType(int32_t authForm)260 int32_t AuthFormToGroupType(int32_t authForm)
261 {
262 int32_t groupType;
263 switch (authForm) {
264 case AUTH_FORM_ACCOUNT_UNRELATED:
265 groupType = PEER_TO_PEER_GROUP;
266 break;
267 case AUTH_FORM_IDENTICAL_ACCOUNT:
268 groupType = IDENTICAL_ACCOUNT_GROUP;
269 break;
270 case AUTH_FORM_ACROSS_ACCOUNT:
271 groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
272 break;
273 default:
274 LOGE("Invalid auth form!");
275 groupType = GROUP_TYPE_INVALID;
276 break;
277 }
278 return groupType;
279 }
280
GroupTypeToAuthForm(int32_t groupType)281 int32_t GroupTypeToAuthForm(int32_t groupType)
282 {
283 int32_t authForm;
284 switch (groupType) {
285 case PEER_TO_PEER_GROUP:
286 authForm = AUTH_FORM_ACCOUNT_UNRELATED;
287 break;
288 case IDENTICAL_ACCOUNT_GROUP:
289 authForm = AUTH_FORM_IDENTICAL_ACCOUNT;
290 break;
291 case ACROSS_ACCOUNT_AUTHORIZE_GROUP:
292 authForm = AUTH_FORM_ACROSS_ACCOUNT;
293 break;
294 default:
295 LOGE("Invalid group type!");
296 authForm = AUTH_FORM_INVALID_TYPE;
297 break;
298 }
299 return authForm;
300 }
301