• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     returnEntry->upgradeFlag = entry->upgradeFlag;
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 
GetGroupEntryById(int32_t osAccountId,const char * groupId,TrustedGroupEntry * returnEntry)93 static int32_t GetGroupEntryById(int32_t osAccountId, const char *groupId, TrustedGroupEntry *returnEntry)
94 {
95     uint32_t groupIndex;
96     TrustedGroupEntry **entry = NULL;
97     GroupEntryVec groupEntryVec = CreateGroupEntryVec();
98     QueryGroupParams groupParams = InitQueryGroupParams();
99     groupParams.groupId = groupId;
100     if (QueryGroups(osAccountId, &groupParams, &groupEntryVec) != HC_SUCCESS) {
101         LOGE("query groups failed!");
102         ClearGroupEntryVec(&groupEntryVec);
103         return HC_ERR_GROUP_NOT_EXIST;
104     }
105     FOR_EACH_HC_VECTOR(groupEntryVec, groupIndex, entry) {
106         if (entry == NULL) {
107             LOGE("groupEntry is null!");
108             ClearGroupEntryVec(&groupEntryVec);
109             return HC_ERR_GROUP_NOT_EXIST;
110         }
111         if (!GaDeepCopyGroupEntry(*entry, returnEntry)) {
112             ClearGroupEntryVec(&groupEntryVec);
113             return HC_ERR_GROUP_NOT_EXIST;
114         }
115         ClearGroupEntryVec(&groupEntryVec);
116         return HC_SUCCESS;
117     }
118     ClearGroupEntryVec(&groupEntryVec);
119     return HC_ERR_GROUP_NOT_EXIST;
120 }
121 
GaIsGroupAccessible(int32_t osAccountId,const char * groupId,const char * appId)122 bool GaIsGroupAccessible(int32_t osAccountId, const char *groupId, const char *appId)
123 {
124     if ((groupId == NULL) || (appId == NULL)) {
125         LOGE("The input groupId or appId is NULL!");
126         return false;
127     }
128     TrustedGroupEntry *entry = CreateGroupEntry();
129     if (entry == NULL) {
130         LOGE("Failed to create group entry!");
131         return false;
132     }
133     int32_t res = GetGroupEntryById(osAccountId, groupId, entry);
134     if (res != HC_SUCCESS) {
135         LOGE("Failed to get group entry by groupId!");
136         DestroyGroupEntry(entry);
137         return false;
138     }
139     DestroyGroupEntry(entry);
140     return true;
141 }
142 
GaGetTrustedDeviceEntryById(int32_t osAccountId,const char * deviceId,bool isUdid,const char * groupId,TrustedDeviceEntry * returnDeviceEntry)143 int32_t GaGetTrustedDeviceEntryById(int32_t osAccountId, const char *deviceId,
144     bool isUdid, const char *groupId, TrustedDeviceEntry *returnDeviceEntry)
145 {
146     if (returnDeviceEntry == NULL) {
147         LOGE("The input returnEntry is NULL!");
148         return HC_ERR_INVALID_PARAMS;
149     }
150     uint32_t index;
151     TrustedDeviceEntry **deviceEntry = NULL;
152     DeviceEntryVec deviceEntryVec = CREATE_HC_VECTOR(DeviceEntryVec);
153     QueryDeviceParams params = InitQueryDeviceParams();
154     params.groupId = groupId;
155     if (isUdid) {
156         params.udid = deviceId;
157     } else {
158         params.authId = deviceId;
159     }
160     if (QueryDevices(osAccountId, &params, &deviceEntryVec) != HC_SUCCESS) {
161         LOGE("query trusted devices failed!");
162         ClearDeviceEntryVec(&deviceEntryVec);
163         return HC_ERR_DEVICE_NOT_EXIST;
164     }
165     FOR_EACH_HC_VECTOR(deviceEntryVec, index, deviceEntry) {
166         if (deviceEntry == NULL) {
167             LOGE("deviceEntry is null!");
168             ClearDeviceEntryVec(&deviceEntryVec);
169             return HC_ERR_DEVICE_NOT_EXIST;
170         }
171         if (!GaDeepCopyDeviceEntry(*deviceEntry, returnDeviceEntry)) {
172             ClearDeviceEntryVec(&deviceEntryVec);
173             return HC_ERR_DEVICE_NOT_EXIST;
174         }
175         ClearDeviceEntryVec(&deviceEntryVec);
176         return HC_SUCCESS;
177     }
178     ClearDeviceEntryVec(&deviceEntryVec);
179     return HC_ERR_DEVICE_NOT_EXIST;
180 }
181 
GaIsDeviceInGroup(int32_t groupType,int32_t osAccountId,const char * peerUdid,const char * peerAuthId,const char * groupId)182 bool GaIsDeviceInGroup(int32_t groupType, int32_t osAccountId, const char *peerUdid, const char *peerAuthId,
183     const char *groupId)
184 {
185     int32_t res;
186     int32_t authForm = GroupTypeToAuthForm(groupType);
187     if ((authForm == AUTH_FORM_ACROSS_ACCOUNT) || (authForm == AUTH_FORM_IDENTICAL_ACCOUNT)) {
188         LOGD("Auth for account related type.");
189         return true; /* Do not check  whether account related devices is in account. */
190     }
191     TrustedDeviceEntry *deviceEntry = CreateDeviceEntry();
192     if (deviceEntry == NULL) {
193         LOGE("Failed to allocate memory for deviceEntry!");
194         return false;
195     }
196     if (peerUdid != NULL) {
197         res = GaGetTrustedDeviceEntryById(osAccountId, peerUdid, true, groupId, deviceEntry);
198     } else if (peerAuthId != NULL) {
199         res = GaGetTrustedDeviceEntryById(osAccountId, peerAuthId, false, groupId, deviceEntry);
200     } else {
201         LOGE("Both the input udid and authId is null!");
202         res = HC_ERROR;
203     }
204     DestroyDeviceEntry(deviceEntry);
205     if (res != HC_SUCCESS) {
206         return false;
207     }
208     return true;
209 }
210 
GaGetLocalDeviceInfo(int32_t osAccountId,const char * groupId,TrustedDeviceEntry * localAuthInfo)211 int32_t GaGetLocalDeviceInfo(int32_t osAccountId, const char *groupId, TrustedDeviceEntry *localAuthInfo)
212 {
213     char *localUdid = (char *)HcMalloc(INPUT_UDID_LEN, 0);
214     if (localUdid == NULL) {
215         LOGE("Failed to malloc for local udid!");
216         return HC_ERR_ALLOC_MEMORY;
217     }
218     int32_t res = HcGetUdid((uint8_t *)localUdid, INPUT_UDID_LEN);
219     if (res != HC_SUCCESS) {
220         LOGE("Failed to get local udid!");
221         HcFree(localUdid);
222         return res;
223     }
224     PRINT_SENSITIVE_DATA("SelfUdid", localUdid);
225     res = GaGetTrustedDeviceEntryById(osAccountId, localUdid, true, groupId, localAuthInfo);
226     HcFree(localUdid);
227     if (res != HC_SUCCESS) {
228         LOGE("Failed to get local device info from database!");
229     }
230     return res;
231 }
232 
AuthFormToGroupType(int32_t authForm)233 int32_t AuthFormToGroupType(int32_t authForm)
234 {
235     int32_t groupType;
236     switch (authForm) {
237         case AUTH_FORM_ACCOUNT_UNRELATED:
238             groupType = PEER_TO_PEER_GROUP;
239             break;
240         case AUTH_FORM_IDENTICAL_ACCOUNT:
241             groupType = IDENTICAL_ACCOUNT_GROUP;
242             break;
243         case AUTH_FORM_ACROSS_ACCOUNT:
244             groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
245             break;
246         default:
247             LOGE("Invalid auth form!");
248             groupType = GROUP_TYPE_INVALID;
249             break;
250     }
251     return groupType;
252 }
253 
GroupTypeToAuthForm(int32_t groupType)254 int32_t GroupTypeToAuthForm(int32_t groupType)
255 {
256     int32_t authForm;
257     switch (groupType) {
258         case PEER_TO_PEER_GROUP:
259             authForm = AUTH_FORM_ACCOUNT_UNRELATED;
260             break;
261         case IDENTICAL_ACCOUNT_GROUP:
262             authForm = AUTH_FORM_IDENTICAL_ACCOUNT;
263             break;
264         case ACROSS_ACCOUNT_AUTHORIZE_GROUP:
265             authForm = AUTH_FORM_ACROSS_ACCOUNT;
266             break;
267         default:
268             LOGE("Invalid group type!");
269             authForm = AUTH_FORM_INVALID_TYPE;
270             break;
271     }
272     return authForm;
273 }
274