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