• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "auth_hichain_adapter.h"
17 
18 #include <string.h>
19 
20 #include "auth_common.h"
21 #include "auth_hichain.h"
22 #include "auth_log.h"
23 #include "auth_session_fsm.h"
24 #include "bus_center_manager.h"
25 #include "device_auth.h"
26 #include "device_auth_defines.h"
27 #include "lnn_ohos_account_adapter.h"
28 #include "softbus_adapter_mem.h"
29 #include "softbus_errcode.h"
30 #include "softbus_json_utils.h"
31 
32 #define AUTH_APPID "softbus_auth"
33 #define GROUP_ID "groupId"
34 #define GROUP_TYPE "groupType"
35 #define AUTH_ID "authId"
36 #define RETRY_TIMES 16
37 #define RETRY_MILLSECONDS 500
38 #define SAME_ACCOUNT_GROUY_TYPE 1
39 static const GroupAuthManager *g_hichain = NULL;
40 #define CUST_UDID_LEN 16
41 
InitHichain(void)42 static const GroupAuthManager *InitHichain(void)
43 {
44     int32_t ret = InitDeviceAuthService();
45     if (ret != 0) {
46         AUTH_LOGE(AUTH_INIT, "hichain InitDeviceAuthService failed err=%{public}d", ret);
47         return NULL;
48     }
49     const GroupAuthManager *gaIns = GetGaInstance();
50     if (gaIns == NULL) {
51         AUTH_LOGE(AUTH_INIT, "hichain GetGaInstance failed");
52         DestroyDeviceAuthService();
53         return NULL;
54     }
55     AUTH_LOGI(AUTH_INIT, "hichain init succ");
56     return gaIns;
57 }
58 
RegChangeListener(const char * appId,DataChangeListener * listener)59 int32_t RegChangeListener(const char *appId, DataChangeListener *listener)
60 {
61     AUTH_CHECK_AND_RETURN_RET_LOGE(listener != NULL, SOFTBUS_ERR, AUTH_HICHAIN,
62         "listener is null");
63     if (g_hichain == NULL) {
64         g_hichain = InitHichain();
65     }
66     AUTH_CHECK_AND_RETURN_RET_LOGE(g_hichain != NULL, SOFTBUS_ERR, AUTH_HICHAIN,
67         "hichain not initialized");
68 
69     const DeviceGroupManager *gmInstance = GetGmInstance();
70     AUTH_CHECK_AND_RETURN_RET_LOGE(gmInstance != NULL, SOFTBUS_ERR, AUTH_HICHAIN,
71         "hichain GetGmInstance failed");
72 
73     int32_t ret = gmInstance->regDataChangeListener(appId, listener);
74     AUTH_CHECK_AND_RETURN_RET_LOGE(ret == 0, SOFTBUS_ERR, AUTH_HICHAIN,
75         "hichain regDataChangeListener failed=%{public}d", ret);
76 
77     return SOFTBUS_OK;
78 }
79 
UnregChangeListener(const char * appId)80 int32_t UnregChangeListener(const char *appId)
81 {
82     AUTH_CHECK_AND_RETURN_RET_LOGE(appId != NULL, SOFTBUS_INVALID_PARAM, AUTH_HICHAIN,
83         "appId is null");
84     const DeviceGroupManager *gmInstance = GetGmInstance();
85     AUTH_CHECK_AND_RETURN_RET_LOGE(gmInstance != NULL, SOFTBUS_ERR, AUTH_HICHAIN,
86         "hichain GetGmInstance failed");
87     int32_t ret = gmInstance->unRegDataChangeListener(appId);
88     AUTH_CHECK_AND_RETURN_RET_LOGE(ret == 0, SOFTBUS_ERR, AUTH_HICHAIN,
89         "hichain unRegDataChangeListener failed=%{public}d", ret);
90 
91     return SOFTBUS_OK;
92 }
93 
AuthDevice(int64_t authReqId,const char * authParams,const DeviceAuthCallback * cb)94 int32_t AuthDevice(int64_t authReqId, const char *authParams, const DeviceAuthCallback *cb)
95 {
96     AUTH_CHECK_AND_RETURN_RET_LOGE(authParams != NULL && cb != NULL, SOFTBUS_INVALID_PARAM,
97         AUTH_HICHAIN, "authParams or cb is null");
98     if (g_hichain == NULL) {
99         g_hichain = InitHichain();
100     }
101     AUTH_CHECK_AND_RETURN_RET_LOGE(g_hichain != NULL, SOFTBUS_ERR, AUTH_HICHAIN, "hichain not initialized");
102 
103     for (int32_t i = 1; i < RETRY_TIMES; i++) {
104         int32_t ret = g_hichain->authDevice(ANY_OS_ACCOUNT, authReqId, authParams, cb);
105         if (ret == HC_SUCCESS) {
106             AUTH_LOGI(AUTH_HICHAIN, "hichain call authDevice success, times=%{public}d", i);
107             return SOFTBUS_OK;
108         }
109         if (ret != HC_ERR_INVALID_PARAMS) {
110             AUTH_LOGE(AUTH_HICHAIN, "hichain call authDevice failed, err=%{public}d", ret);
111             return SOFTBUS_ERR;
112         }
113         AUTH_LOGW(AUTH_HICHAIN,
114             "hichain retry call authDevice, current retry times=%{public}d, err=%{public}d", i, ret);
115         (void)SoftBusSleepMs(RETRY_MILLSECONDS);
116     }
117     return SOFTBUS_ERR;
118 }
119 
ProcessAuthData(int64_t authSeq,const uint8_t * data,uint32_t len,DeviceAuthCallback * cb)120 int32_t ProcessAuthData(int64_t authSeq, const uint8_t *data, uint32_t len, DeviceAuthCallback *cb)
121 {
122     AUTH_CHECK_AND_RETURN_RET_LOGE(data != NULL && cb != NULL, SOFTBUS_INVALID_PARAM, AUTH_HICHAIN,
123         "data or cb is null");
124     if (g_hichain == NULL) {
125         g_hichain = InitHichain();
126     }
127     AUTH_CHECK_AND_RETURN_RET_LOGE(g_hichain != NULL, SOFTBUS_ERR, AUTH_HICHAIN, "hichain not initialized");
128 
129     int32_t ret = g_hichain->processData(authSeq, data, len, cb);
130     AUTH_CHECK_AND_RETURN_RET_LOGE(ret == 0, SOFTBUS_ERR, AUTH_HICHAIN,
131         "hichain processData failed. ret=%{public}d", ret);
132 
133     return SOFTBUS_OK;
134 }
135 
CheckDeviceInGroupByType(const char * udid,const char * uuid,HichainGroup groupType)136 bool CheckDeviceInGroupByType(const char *udid, const char *uuid, HichainGroup groupType)
137 {
138     (void)udid;
139     (void)uuid;
140     (void)groupType;
141     return true;
142 }
143 
DestroyDeviceAuth(void)144 void DestroyDeviceAuth(void)
145 {
146     DestroyDeviceAuthService();
147     g_hichain = NULL;
148     AUTH_LOGI(AUTH_HICHAIN, "hichain destroy succ");
149 }
150 
IsTrustedDeviceInAGroup(const DeviceGroupManager * gmInstance,int32_t accountId,const char * groupId,const char * deviceId)151 static bool IsTrustedDeviceInAGroup(const DeviceGroupManager *gmInstance, int32_t accountId,
152     const char *groupId, const char *deviceId)
153 {
154     uint32_t deviceNum = 0;
155     char *returnDevInfoVec = NULL;
156     if (gmInstance->getTrustedDevices(accountId, AUTH_APPID, groupId, &returnDevInfoVec, &deviceNum) != SOFTBUS_OK) {
157         gmInstance->destroyInfo(&returnDevInfoVec);
158         AUTH_LOGE(AUTH_HICHAIN, "GetTrustedDevices fail");
159         return false;
160     }
161     if (deviceNum == 0) {
162         gmInstance->destroyInfo(&returnDevInfoVec);
163         AUTH_LOGI(AUTH_HICHAIN, "GetTrustedDevices zero");
164         return false;
165     }
166     cJSON *devJson = cJSON_Parse(returnDevInfoVec);
167     if (devJson == NULL) {
168         gmInstance->destroyInfo(&returnDevInfoVec);
169         AUTH_LOGE(AUTH_HICHAIN, "parse json fail");
170         return false;
171     }
172     int32_t devArraySize = cJSON_GetArraySize(devJson);
173     for (int32_t j = 0; j < devArraySize; j++) {
174         cJSON *devItem = cJSON_GetArrayItem(devJson, j);
175         char authId[UDID_BUF_LEN] = {0};
176         if (!GetJsonObjectStringItem(devItem, AUTH_ID, authId, UDID_BUF_LEN)) {
177             AUTH_LOGE(AUTH_HICHAIN, "AUTH_ID not found");
178             continue;
179         }
180         uint8_t udidHash[SHA_256_HASH_LEN] = {0};
181         char hashStr[CUST_UDID_LEN + 1] = {0};
182         if (SoftBusGenerateStrHash((const unsigned char *)authId, strlen(authId), udidHash) != SOFTBUS_OK) {
183             continue;
184         }
185         if (ConvertBytesToHexString(hashStr, CUST_UDID_LEN + 1, udidHash,
186             CUST_UDID_LEN / HEXIFY_UNIT_LEN) != SOFTBUS_OK) {
187             continue;
188         }
189         if (strncmp(hashStr, deviceId, strlen(deviceId)) == 0) {
190             cJSON_Delete(devJson);
191             gmInstance->destroyInfo(&returnDevInfoVec);
192             return true;
193         }
194     }
195     cJSON_Delete(devJson);
196     gmInstance->destroyInfo(&returnDevInfoVec);
197     return false;
198 }
199 
HasTrustedRelationWithLocalDevice(const DeviceGroupManager * gmInstance,int32_t accountId,char * localUdid,const char * deviceId,bool isPointToPoint)200 static bool HasTrustedRelationWithLocalDevice(const DeviceGroupManager *gmInstance, int32_t accountId,
201     char *localUdid, const char *deviceId, bool isPointToPoint)
202 {
203     uint32_t groupNum = 0;
204     char *returnGroupVec = NULL;
205     if (gmInstance->getRelatedGroups(accountId, AUTH_APPID, localUdid, &returnGroupVec, &groupNum) != SOFTBUS_OK) {
206         AUTH_LOGE(AUTH_HICHAIN, "GetRelatedGroups fail");
207         gmInstance->destroyInfo(&returnGroupVec);
208         return false;
209     }
210     if (groupNum == 0) {
211         AUTH_LOGI(AUTH_HICHAIN, "GetRelatedGroups zero");
212         gmInstance->destroyInfo(&returnGroupVec);
213         return false;
214     }
215     cJSON *groupJson = cJSON_Parse(returnGroupVec);
216     if (groupJson == NULL) {
217         AUTH_LOGE(AUTH_HICHAIN, "parse json fail");
218         gmInstance->destroyInfo(&returnGroupVec);
219         return false;
220     }
221     int32_t groupArraySize = cJSON_GetArraySize(groupJson);
222     for (int32_t i = 0; i < groupArraySize; i++) {
223         cJSON *groupItem = cJSON_GetArrayItem(groupJson, i);
224         char groupId[UDID_BUF_LEN] = {0};
225         if (isPointToPoint) {
226             int groupType = 0;
227             if ((GetJsonObjectNumberItem(groupItem, GROUP_TYPE, &groupType) && groupType == SAME_ACCOUNT_GROUY_TYPE)) {
228                 AUTH_LOGI(AUTH_HICHAIN, "ignore same account group");
229                 continue;
230             }
231         }
232         if (!GetJsonObjectStringItem(groupItem, GROUP_ID, groupId, UDID_BUF_LEN)) {
233             AUTH_LOGE(AUTH_HICHAIN, "GROUP_ID not found");
234             continue;
235         }
236         if (IsTrustedDeviceInAGroup(gmInstance, accountId, groupId, deviceId)) {
237             cJSON_Delete(groupJson);
238             gmInstance->destroyInfo(&returnGroupVec);
239             return true;
240         }
241     }
242     cJSON_Delete(groupJson);
243     gmInstance->destroyInfo(&returnGroupVec);
244     return false;
245 }
246 
IsPotentialTrustedDevice(TrustedRelationIdType idType,const char * deviceId,bool isPrecise,bool isPointToPoint)247 bool IsPotentialTrustedDevice(TrustedRelationIdType idType, const char *deviceId, bool isPrecise, bool isPointToPoint)
248 {
249     (void)idType;
250     (void)isPrecise;
251     const DeviceGroupManager *gmInstance = GetGmInstance();
252     if (gmInstance == NULL) {
253         AUTH_LOGE(AUTH_HICHAIN, "hichain GetGmInstance failed");
254         return false;
255     }
256 
257     int32_t accountId = GetActiveOsAccountIds();
258     if (accountId <= 0) {
259         AUTH_LOGE(AUTH_HICHAIN, "accountId is invalid");
260         return false;
261     }
262 
263     char localUdid[UDID_BUF_LEN] = {0};
264     if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
265         AUTH_LOGE(AUTH_HICHAIN, "get udid fail");
266         return false;
267     }
268     return HasTrustedRelationWithLocalDevice(gmInstance, accountId, localUdid, deviceId, isPointToPoint);
269 }
270 
HichainGetJoinedGroups(int32_t groupType)271 uint32_t HichainGetJoinedGroups(int32_t groupType)
272 {
273     uint32_t groupCnt = 0;
274     char *accountGroups = NULL;
275 
276     const DeviceGroupManager *gmInstance = GetGmInstance();
277     AUTH_CHECK_AND_RETURN_RET_LOGE(gmInstance != NULL, groupCnt, AUTH_HICHAIN, "hichain GetGmInstance failed");
278 
279     if (gmInstance->getJoinedGroups(0, AUTH_APPID, (GroupType)groupType, &accountGroups, &groupCnt) != 0) {
280         AUTH_LOGE(AUTH_HICHAIN, "hichain getJoinedGroups groupCnt fail");
281         groupCnt = 0;
282     }
283     if (accountGroups != NULL) {
284         SoftBusFree(accountGroups);
285     }
286     return groupCnt;
287 }
288 
CancelRequest(int64_t authReqId,const char * appId)289 void CancelRequest(int64_t authReqId, const char *appId)
290 {
291     AUTH_CHECK_AND_RETURN_LOGE(appId != NULL, AUTH_HICHAIN, "appId is null");
292     if (g_hichain == NULL) {
293         g_hichain = InitHichain();
294     }
295     AUTH_CHECK_AND_RETURN_LOGE(g_hichain != NULL, AUTH_HICHAIN, "hichain not initialized");
296     g_hichain->cancelRequest(authReqId, appId);
297 }