• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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_device.h"
17 
18 #include <securec.h>
19 #include "anonymizer.h"
20 #include "auth_connection.h"
21 #include "auth_deviceprofile.h"
22 #include "auth_hichain.h"
23 #include "auth_log.h"
24 #include "auth_request.h"
25 #include "auth_session_message.h"
26 #include "bus_center_manager.h"
27 #include "device_profile_listener.h"
28 #include "g_enhance_lnn_func.h"
29 #include "g_enhance_lnn_func_pack.h"
30 #include "lnn_app_bind_interface.h"
31 #include "lnn_decision_db.h"
32 #include "lnn_heartbeat_ctrl.h"
33 #include "lnn_local_net_ledger.h"
34 #include "lnn_ohos_account_adapter.h"
35 #include "lnn_map.h"
36 #include "lnn_net_builder.h"
37 #include "lnn_log.h"
38 #include "legacy/softbus_adapter_hitrace.h"
39 #include "softbus_adapter_mem.h"
40 #include "softbus_init_common.h"
41 
42 #define DELAY_AUTH_TIME                    (8 * 1000L)
43 
44 static AuthVerifyListener g_verifyListener = { 0 };
45 static GroupChangeListener g_groupChangeListener = { 0 };
46 static Map g_authLimitMap;
47 static SoftBusMutex g_authLimitMutex;
48 static bool g_isInit = false;
49 static bool g_regDataChangeListener = false;
50 
AuthMapInit(void)51 static bool AuthMapInit(void)
52 {
53     if (SoftBusMutexInit(&g_authLimitMutex, NULL) != SOFTBUS_OK) {
54         AUTH_LOGE(AUTH_FSM, "g_authLimit mutex init fail");
55         return false;
56     }
57     LnnMapInit(&g_authLimitMap);
58     g_isInit = true;
59     AUTH_LOGI(AUTH_FSM, "authLimit map init success");
60     return true;
61 }
62 
InsertToAuthLimitMap(const char * udidHash,uint64_t currentTime)63 static void InsertToAuthLimitMap(const char *udidHash, uint64_t currentTime)
64 {
65     if (SoftBusMutexLock(&g_authLimitMutex) != SOFTBUS_OK) {
66         AUTH_LOGE(AUTH_FSM, "SoftBusMutexLock fail");
67         return;
68     }
69     if (LnnMapSet(&g_authLimitMap, udidHash, (const void *)&currentTime, sizeof(uint64_t)) != SOFTBUS_OK) {
70         AUTH_LOGE(AUTH_FSM, "LnnMapSet fail");
71         (void)SoftBusMutexUnlock(&g_authLimitMutex);
72         return;
73     }
74     (void)SoftBusMutexUnlock(&g_authLimitMutex);
75 }
76 
GetNodeFromAuthLimitMap(const char * udidHash,uint64_t * time)77 static int32_t GetNodeFromAuthLimitMap(const char *udidHash, uint64_t *time)
78 {
79     if (!g_isInit) {
80         return SOFTBUS_OK;
81     }
82     if (SoftBusMutexLock(&g_authLimitMutex) != SOFTBUS_OK) {
83         AUTH_LOGE(AUTH_FSM, "SoftBusMutexLock fail");
84         return SOFTBUS_LOCK_ERR;
85     }
86     uint64_t *ptr = (uint64_t *)LnnMapGet(&g_authLimitMap, udidHash);
87     if (ptr == NULL) {
88         AUTH_LOGE(AUTH_FSM, "LnnMapGet fail");
89         (void)SoftBusMutexUnlock(&g_authLimitMutex);
90         return SOFTBUS_INVALID_PARAM;
91     }
92     *time = *ptr;
93     (void)SoftBusMutexUnlock(&g_authLimitMutex);
94     return SOFTBUS_OK;
95 }
96 
IsNeedAuthLimit(const char * udidHash)97 bool IsNeedAuthLimit(const char *udidHash)
98 {
99     if (udidHash == NULL) {
100         AUTH_LOGE(AUTH_FSM, "invalid param");
101         return false;
102     }
103     uint64_t time = 0;
104     uint64_t currentTime = 0;
105     if (GetNodeFromAuthLimitMap(udidHash, &time) != SOFTBUS_OK) {
106         return false;
107     }
108     if (time == 0) {
109         AUTH_LOGI(AUTH_FSM, "no need delay authentication");
110         return false;
111     }
112     currentTime = GetCurrentTimeMs();
113     AUTH_LOGI(AUTH_FSM, "currentTime=%{public}" PRIu64 ", time=%{public}" PRIu64 "", currentTime, time);
114     if (currentTime - time < DELAY_AUTH_TIME) {
115         AUTH_LOGI(AUTH_FSM, "lastest retcode authentication time less than 8s");
116         return true;
117     }
118     return false;
119 }
120 
AuthDeleteLimitMap(const char * udidHash)121 void AuthDeleteLimitMap(const char *udidHash)
122 {
123     if (!g_isInit || udidHash == NULL) {
124         return;
125     }
126     if (SoftBusMutexLock(&g_authLimitMutex) != SOFTBUS_OK) {
127         AUTH_LOGE(AUTH_FSM, "SoftBusMutexLock fail");
128         return;
129     }
130     int32_t ret = LnnMapErase(&g_authLimitMap, udidHash);
131     if (ret != SOFTBUS_OK) {
132         AUTH_LOGE(AUTH_FSM, "delete item fail, ret=%{public}d", ret);
133         (void)SoftBusMutexUnlock(&g_authLimitMutex);
134         return;
135     }
136     (void)SoftBusMutexUnlock(&g_authLimitMutex);
137 }
138 
ClearAuthLimitMap(void)139 void ClearAuthLimitMap(void)
140 {
141     if (!g_isInit) {
142         return;
143     }
144     if (SoftBusMutexLock(&g_authLimitMutex) != SOFTBUS_OK) {
145         AUTH_LOGE(AUTH_FSM, "SoftBusMutexLock fail");
146         return;
147     }
148     LnnMapDelete(&g_authLimitMap);
149     AUTH_LOGI(AUTH_FSM, "ClearAuthLimitMap success");
150     (void)SoftBusMutexUnlock(&g_authLimitMutex);
151 }
152 
AuthAddNodeToLimitMap(const char * udid,int32_t reason)153 void AuthAddNodeToLimitMap(const char *udid, int32_t reason)
154 {
155     AUTH_CHECK_AND_RETURN_LOGE(udid != NULL, AUTH_FSM, "udid is null");
156 
157     if (reason == SOFTBUS_AUTH_HICHAIN_LOCAL_IDENTITY_NOT_EXIST || reason == SOFTBUS_AUTH_HICHAIN_GROUP_NOT_EXIST ||
158         reason == SOFTBUS_AUTH_HICHAIN_NO_CANDIDATE_GROUP) {
159         uint64_t currentTime = GetCurrentTimeMs();
160         AUTH_LOGI(AUTH_FSM, "reason=%{public}d, currentTime=%{public}" PRIu64 "", reason, currentTime);
161 
162         uint8_t hash[SHA_256_HASH_LEN] = { 0 };
163         char udidHash[SHORT_UDID_HASH_HEX_LEN + 1] = { 0 };
164         if (SoftBusGenerateStrHash((uint8_t *)udid, strlen(udid), hash) != SOFTBUS_OK) {
165             AUTH_LOGE(AUTH_FSM, "GenerateUdidShortHash fail.");
166             return;
167         }
168         if (ConvertBytesToUpperCaseHexString(udidHash, SHORT_UDID_HASH_HEX_LEN + 1, hash, UDID_SHORT_HASH_LEN_TEMP) !=
169             SOFTBUS_OK) {
170             AUTH_LOGE(AUTH_FSM, "convert bytes to string fail");
171             return;
172         }
173         if (!g_isInit && !AuthMapInit()) {
174             return;
175         }
176         InsertToAuthLimitMap(udidHash, currentTime);
177     }
178 }
179 
AuthDevicePostTransData(AuthHandle authHandle,const AuthTransData * dataInfo)180 int32_t AuthDevicePostTransData(AuthHandle authHandle, const AuthTransData *dataInfo)
181 {
182     if (dataInfo == NULL) {
183         AUTH_LOGE(AUTH_CONN, "dataInfo is null");
184         return SOFTBUS_INVALID_PARAM;
185     }
186     if (authHandle.type < AUTH_LINK_TYPE_WIFI || authHandle.type >= AUTH_LINK_TYPE_MAX) {
187         AUTH_LOGE(AUTH_CONN, "authHandle type error");
188         return SOFTBUS_INVALID_PARAM;
189     }
190     AuthManager *auth = GetAuthManagerByAuthId(authHandle.authId);
191     if (auth == NULL) {
192         return SOFTBUS_AUTH_NOT_FOUND;
193     }
194     AuthDataHead head;
195     head.dataType = DATA_TYPE_CONNECTION;
196     head.module = dataInfo->module;
197     head.seq = dataInfo->seq;
198     head.flag = dataInfo->flag;
199     head.len = 0;
200     uint8_t *encData = NULL;
201     InDataInfo inDataInfo = { .inData = dataInfo->data, .inLen = dataInfo->len };
202     if (EncryptInner(&auth->sessionKeyList, (AuthLinkType)authHandle.type, &inDataInfo, &encData,
203         &head.len) != SOFTBUS_OK) {
204         AUTH_LOGE(AUTH_KEY, "encrypt trans data fail");
205         DelDupAuthManager(auth);
206         return SOFTBUS_ENCRYPT_ERR;
207     }
208     if (PostAuthData(auth->connId[authHandle.type], !auth->isServer, &head, encData) != SOFTBUS_OK) {
209         AUTH_LOGE(AUTH_CONN, "post trans data fail");
210         SoftBusFree(encData);
211         DelDupAuthManager(auth);
212         return SOFTBUS_AUTH_SEND_FAIL;
213     }
214     SoftBusFree(encData);
215     DelDupAuthManager(auth);
216     return SOFTBUS_OK;
217 }
218 
AuthDeviceEncrypt(AuthHandle * authHandle,const uint8_t * inData,uint32_t inLen,uint8_t * outData,uint32_t * outLen)219 int32_t AuthDeviceEncrypt(AuthHandle *authHandle, const uint8_t *inData, uint32_t inLen, uint8_t *outData,
220     uint32_t *outLen)
221 {
222     if (authHandle == NULL || inData == NULL || inLen == 0 || outData == NULL || outLen == NULL ||
223         *outLen < (inLen + ENCRYPT_OVER_HEAD_LEN)) {
224         AUTH_LOGE(AUTH_KEY, "invalid param");
225         return SOFTBUS_INVALID_PARAM;
226     }
227     AuthManager *auth = GetAuthManagerByAuthId(authHandle->authId);
228     if (auth == NULL) {
229         return SOFTBUS_AUTH_NOT_FOUND;
230     }
231     InDataInfo inDataInfo = { .inData = inData, .inLen = inLen };
232     if (EncryptData(&auth->sessionKeyList, (AuthLinkType)authHandle->type, &inDataInfo, outData,
233         outLen) != SOFTBUS_OK) {
234         AUTH_LOGE(AUTH_KEY, "auth encrypt fail");
235         DelDupAuthManager(auth);
236         return SOFTBUS_ENCRYPT_ERR;
237     }
238     DelDupAuthManager(auth);
239     return SOFTBUS_OK;
240 }
241 
AuthDeviceDecrypt(AuthHandle * authHandle,const uint8_t * inData,uint32_t inLen,uint8_t * outData,uint32_t * outLen)242 int32_t AuthDeviceDecrypt(AuthHandle *authHandle, const uint8_t *inData, uint32_t inLen, uint8_t *outData,
243     uint32_t *outLen)
244 {
245     if (authHandle == NULL || inData == NULL || inLen == 0 || outData == NULL || outLen == NULL ||
246         *outLen < AuthGetDecryptSize(inLen)) {
247         AUTH_LOGE(AUTH_KEY, "invalid param");
248         return SOFTBUS_INVALID_PARAM;
249     }
250     AuthManager *auth = GetAuthManagerByAuthId(authHandle->authId);
251     if (auth == NULL) {
252         return SOFTBUS_AUTH_NOT_FOUND;
253     }
254     InDataInfo inDataInfo = { .inData = inData, .inLen = inLen };
255     if (DecryptData(&auth->sessionKeyList, (AuthLinkType)authHandle->type, &inDataInfo, outData,
256         outLen) != SOFTBUS_OK) {
257         AUTH_LOGE(AUTH_KEY, "auth decrypt fail, authId=%{public}" PRId64, authHandle->authId);
258         DelDupAuthManager(auth);
259         return SOFTBUS_ENCRYPT_ERR;
260     }
261     DelDupAuthManager(auth);
262     return SOFTBUS_OK;
263 }
264 
AuthDeviceGetConnInfo(AuthHandle authHandle,AuthConnInfo * connInfo)265 int32_t AuthDeviceGetConnInfo(AuthHandle authHandle, AuthConnInfo *connInfo)
266 {
267     if (connInfo == NULL) {
268         AUTH_LOGE(AUTH_CONN, "connInfo is null");
269         return SOFTBUS_INVALID_PARAM;
270     }
271     if (authHandle.type < AUTH_LINK_TYPE_WIFI || authHandle.type >= AUTH_LINK_TYPE_MAX) {
272         AUTH_LOGE(AUTH_CONN, "authHandle type error");
273         return SOFTBUS_INVALID_PARAM;
274     }
275     AuthManager *auth = GetAuthManagerByAuthId(authHandle.authId);
276     if (auth == NULL) {
277         return SOFTBUS_AUTH_NOT_FOUND;
278     }
279     *connInfo = auth->connInfo[authHandle.type];
280     DelDupAuthManager(auth);
281     return SOFTBUS_OK;
282 }
283 
AuthDeviceGetServerSide(int64_t authId,bool * isServer)284 int32_t AuthDeviceGetServerSide(int64_t authId, bool *isServer)
285 {
286     if (isServer == NULL) {
287         AUTH_LOGE(AUTH_CONN, "isServer is null");
288         return SOFTBUS_INVALID_PARAM;
289     }
290     AuthManager *auth = GetAuthManagerByAuthId(authId);
291     if (auth == NULL) {
292         return SOFTBUS_AUTH_NOT_FOUND;
293     }
294     *isServer = auth->isServer;
295     DelDupAuthManager(auth);
296     return SOFTBUS_OK;
297 }
298 
AuthDeviceGetDeviceUuid(int64_t authId,char * uuid,uint16_t size)299 int32_t AuthDeviceGetDeviceUuid(int64_t authId, char *uuid, uint16_t size)
300 {
301     if (uuid == NULL) {
302         AUTH_LOGE(AUTH_CONN, "uuid is empty");
303         return SOFTBUS_INVALID_PARAM;
304     }
305     AuthManager *auth = GetAuthManagerByAuthId(authId);
306     if (auth == NULL) {
307         return SOFTBUS_AUTH_NOT_FOUND;
308     }
309     if (strcpy_s(uuid, size, auth->uuid) != EOK) {
310         AUTH_LOGI(AUTH_CONN, "copy uuid fail, size=%{public}u", size);
311         DelDupAuthManager(auth);
312         return SOFTBUS_STRCPY_ERR;
313     }
314     DelDupAuthManager(auth);
315     return SOFTBUS_OK;
316 }
317 
AuthDeviceGetVersion(int64_t authId,SoftBusVersion * version)318 int32_t AuthDeviceGetVersion(int64_t authId, SoftBusVersion *version)
319 {
320     if (version == NULL) {
321         AUTH_LOGE(AUTH_CONN, "version is null");
322         return SOFTBUS_INVALID_PARAM;
323     }
324     AuthManager *auth = GetAuthManagerByAuthId(authId);
325     if (auth == NULL) {
326         return SOFTBUS_AUTH_NOT_FOUND;
327     }
328     *version = auth->version;
329     DelDupAuthManager(auth);
330     return SOFTBUS_OK;
331 }
332 
AuthDeviceNotTrust(const char * peerUdid)333 void AuthDeviceNotTrust(const char *peerUdid)
334 {
335     if (peerUdid == NULL || strlen(peerUdid) == 0) {
336         AUTH_LOGE(AUTH_HICHAIN, "invalid param");
337         return;
338     }
339     char networkId[NETWORK_ID_BUF_LEN] = {0};
340     if (LnnGetNetworkIdByUdid(peerUdid, networkId, sizeof(networkId)) != SOFTBUS_OK) {
341         AUTH_LOGE(AUTH_HICHAIN, "get networkId by udid fail");
342         return;
343     }
344     RemoveNotPassedAuthManagerByUdid(peerUdid);
345     AuthSessionHandleDeviceNotTrusted(peerUdid);
346     LnnDeleteSpecificTrustedDevInfo(peerUdid, GetActiveOsAccountIds());
347     LnnHbOnTrustedRelationReduced();
348     AuthRemoveDeviceKeyByUdidPacked(peerUdid);
349     if (LnnRequestLeaveSpecific(networkId, CONNECTION_ADDR_MAX) != SOFTBUS_OK) {
350         AUTH_LOGE(AUTH_HICHAIN, "request leave specific fail");
351     } else {
352         AUTH_LOGI(AUTH_HICHAIN, "request leave specific successful");
353     }
354 }
355 
AuthNotifyDeviceVerifyPassed(AuthHandle authHandle,const NodeInfo * nodeInfo)356 void AuthNotifyDeviceVerifyPassed(AuthHandle authHandle, const NodeInfo *nodeInfo)
357 {
358     CHECK_NULL_PTR_RETURN_VOID(nodeInfo);
359     AuthManager *auth = GetAuthManagerByAuthId(authHandle.authId);
360     if (auth == NULL) {
361         AUTH_LOGE(AUTH_FSM, "get auth manager failed");
362         return;
363     }
364     if (auth->connInfo[authHandle.type].type == AUTH_LINK_TYPE_P2P ||
365         auth->connInfo[authHandle.type].type == AUTH_LINK_TYPE_SLE) {
366         /* P2P auth and sle auth no need notify to LNN. */
367         DelDupAuthManager(auth);
368         return;
369     }
370     DelDupAuthManager(auth);
371 
372     /* notify LNN device verify pass. */
373     if (g_verifyListener.onDeviceVerifyPass == NULL) {
374         AUTH_LOGW(AUTH_FSM, "onDeviceVerifyPass not set");
375         return;
376     }
377     g_verifyListener.onDeviceVerifyPass(authHandle, nodeInfo);
378 }
379 
AuthNotifyDeviceDisconnect(AuthHandle authHandle)380 void AuthNotifyDeviceDisconnect(AuthHandle authHandle)
381 {
382     if (g_verifyListener.onDeviceDisconnect == NULL) {
383         AUTH_LOGW(AUTH_FSM, "onDeviceDisconnect not set");
384         return;
385     }
386     g_verifyListener.onDeviceDisconnect(authHandle);
387 }
388 
OnDeviceNotTrusted(const char * peerUdid,int32_t localUserId)389 static void OnDeviceNotTrusted(const char *peerUdid, int32_t localUserId)
390 {
391     RemoveNotPassedAuthManagerByUdid(peerUdid);
392     AuthSessionHandleDeviceNotTrusted(peerUdid);
393     if (!DpHasAccessControlProfile(peerUdid, false, localUserId)) {
394         LnnDeleteLinkFinderInfo(peerUdid);
395     }
396     if (!DpHasAccessControlProfile(peerUdid, true, localUserId)) {
397         LnnDeleteSpecificTrustedDevInfo(peerUdid, localUserId);
398     }
399     if (g_verifyListener.onDeviceNotTrusted == NULL) {
400         AUTH_LOGW(AUTH_HICHAIN, "onDeviceNotTrusted not set");
401         return;
402     }
403     g_verifyListener.onDeviceNotTrusted(peerUdid);
404     LnnHbOnTrustedRelationReduced();
405     AuthRemoveDeviceKeyByUdidPacked(peerUdid);
406 }
407 
RegAuthVerifyListener(const AuthVerifyListener * listener)408 int32_t RegAuthVerifyListener(const AuthVerifyListener *listener)
409 {
410     if (listener == NULL) {
411         AUTH_LOGE(AUTH_CONN, "invalid listener");
412         return SOFTBUS_INVALID_PARAM;
413     }
414     g_verifyListener = *listener;
415     return SOFTBUS_OK;
416 }
417 
UnregAuthVerifyListener(void)418 void UnregAuthVerifyListener(void)
419 {
420     (void)memset_s(&g_verifyListener, sizeof(AuthVerifyListener), 0, sizeof(AuthVerifyListener));
421 }
422 
OnGroupCreated(const char * groupId,int32_t groupType)423 static void OnGroupCreated(const char *groupId, int32_t groupType)
424 {
425     if (g_groupChangeListener.onGroupCreated != NULL) {
426         g_groupChangeListener.onGroupCreated(groupId, groupType);
427     }
428 }
429 
OnGroupDeleted(const char * groupId,int32_t groupType)430 static void OnGroupDeleted(const char *groupId, int32_t groupType)
431 {
432     if (g_groupChangeListener.onGroupDeleted != NULL) {
433         g_groupChangeListener.onGroupDeleted(groupId, groupType);
434     }
435 }
436 
OnDeviceBound(const char * udid,const char * groupInfo)437 static void OnDeviceBound(const char *udid, const char *groupInfo)
438 {
439     LnnInsertSpecificTrustedDevInfo(udid);
440     if (g_groupChangeListener.onDeviceBound != NULL) {
441         g_groupChangeListener.onDeviceBound(udid, groupInfo);
442     }
443 }
444 
RetryRegTrustDataChangeListener(void)445 static int32_t RetryRegTrustDataChangeListener(void)
446 {
447     TrustDataChangeListener trustListener = {
448         .onGroupCreated = OnGroupCreated,
449         .onGroupDeleted = OnGroupDeleted,
450         .onDeviceNotTrusted = OnDeviceNotTrusted,
451         .onDeviceBound = OnDeviceBound,
452     };
453     for (int32_t i = 1; i <= RETRY_REGDATA_TIMES; i++) {
454         int32_t ret = RegTrustDataChangeListener(&trustListener);
455         if (ret == SOFTBUS_OK) {
456             AUTH_LOGI(AUTH_HICHAIN, "regDataChangeListener success, times=%{public}d", i);
457             return SOFTBUS_OK;
458         }
459         AUTH_LOGW(AUTH_HICHAIN, "retry regDataChangeListener, current retry times=%{public}d, err=%{public}d", i, ret);
460         (void)SoftBusSleepMs(RETRY_REGDATA_MILLSECONDS);
461     }
462     return SOFTBUS_AUTH_REG_DATA_FAIL;
463 }
464 
RegTrustListenerOnHichainSaStart(void)465 int32_t RegTrustListenerOnHichainSaStart(void)
466 {
467     TrustDataChangeListener trustListener = {
468         .onGroupCreated = OnGroupCreated,
469         .onGroupDeleted = OnGroupDeleted,
470         .onDeviceNotTrusted = OnDeviceNotTrusted,
471         .onDeviceBound = OnDeviceBound,
472     };
473     if (RegTrustDataChangeListener(&trustListener) != SOFTBUS_OK) {
474         AUTH_LOGE(AUTH_INIT, "RegTrustDataChangeListener fail");
475         g_regDataChangeListener = false;
476         return SOFTBUS_AUTH_INIT_FAIL;
477     }
478     g_regDataChangeListener = true;
479     AUTH_LOGE(AUTH_INIT, "OnHichainSaStart add listener succ");
480     return SOFTBUS_OK;
481 }
482 
RegGroupChangeListener(const GroupChangeListener * listener)483 int32_t RegGroupChangeListener(const GroupChangeListener *listener)
484 {
485     if (listener == NULL) {
486         AUTH_LOGE(AUTH_CONN, "listener is null");
487         return SOFTBUS_INVALID_PARAM;
488     }
489     g_groupChangeListener.onGroupCreated = listener->onGroupCreated;
490     g_groupChangeListener.onGroupDeleted = listener->onGroupDeleted;
491     g_groupChangeListener.onDeviceBound = listener->onDeviceBound;
492     return SOFTBUS_OK;
493 }
494 
UnregGroupChangeListener(void)495 void UnregGroupChangeListener(void)
496 {
497     g_groupChangeListener.onGroupCreated = NULL;
498     g_groupChangeListener.onGroupDeleted = NULL;
499     g_groupChangeListener.onDeviceBound = NULL;
500 }
501 
AuthRegisterToDpDelay(void)502 int32_t AuthRegisterToDpDelay(void)
503 {
504     DeviceProfileChangeListener deviceProfileChangeListener = {
505         .onDeviceProfileAdd = OnDeviceBound,
506         .onDeviceProfileDeleted = OnDeviceNotTrusted,
507     };
508     int32_t ret = RegisterToDp(&deviceProfileChangeListener);
509     if (ret != SOFTBUS_OK) {
510         AUTH_LOGE(AUTH_FSM, "register dp error");
511         return ret;
512     }
513     ret = InitDbListDelay();
514     if (ret != SOFTBUS_OK) {
515         AUTH_LOGE(AUTH_FSM, "init trusted list error");
516     }
517     return ret;
518 }
519 
AuthDirectOnlineCreateAuthManager(int64_t authSeq,const AuthSessionInfo * info)520 int32_t AuthDirectOnlineCreateAuthManager(int64_t authSeq, const AuthSessionInfo *info)
521 {
522     AUTH_CHECK_AND_RETURN_RET_LOGE(info, SOFTBUS_INVALID_PARAM, AUTH_FSM, "info is null");
523     AUTH_CHECK_AND_RETURN_RET_LOGE(CheckAuthConnInfoType(&info->connInfo), SOFTBUS_INVALID_PARAM,
524         AUTH_FSM, "connInfo type error");
525     AUTH_LOGI(AUTH_FSM, "direct online authSeq=%{public}" PRId64 ", side=%{public}s, requestId=%{public}u",
526         authSeq, GetAuthSideStr(info->isServer), info->requestId);
527     if (!RequireAuthLock()) {
528         return SOFTBUS_LOCK_ERR;
529     }
530     if (info->connInfo.type != AUTH_LINK_TYPE_BLE) {
531         AUTH_LOGE(AUTH_FSM, "sessionkey online only support ble");
532         ReleaseAuthLock();
533         return SOFTBUS_AUTH_UNEXPECTED_CONN_TYPE;
534     }
535 
536     bool isNewCreated = false;
537     AuthManager *auth = GetDeviceAuthManager(authSeq, info, &isNewCreated, authSeq);
538     if (auth == NULL) {
539         AUTH_LOGE(AUTH_FSM, "auth manager does not exist.");
540         ReleaseAuthLock();
541         return SOFTBUS_AUTH_NOT_FOUND;
542     }
543     auth->hasAuthPassed[info->connInfo.type] = true;
544     AUTH_LOGI(AUTH_FSM,
545         "auth manager without sessionkey. isNewCreated=%{public}d, authId=%{public}" PRId64 ", authSeq=%{public}" PRId64
546         ", lastVerifyTime=%{public}" PRId64,
547         isNewCreated, auth->authId, authSeq, auth->lastVerifyTime);
548     ReleaseAuthLock();
549     return SOFTBUS_OK;
550 }
551 
VerifyDevice(AuthRequest * request)552 static int32_t VerifyDevice(AuthRequest *request)
553 {
554     int64_t traceId = GenSeq(false);
555     request->traceId = traceId;
556     SoftbusHitraceStart(SOFTBUS_HITRACE_ID_VALID, (uint64_t)traceId);
557     AUTH_LOGI(AUTH_CONN, "start verify device: requestId=%{public}u", request->requestId);
558     if (!g_regDataChangeListener) {
559         if (RetryRegTrustDataChangeListener() != SOFTBUS_OK) {
560             AUTH_LOGE(AUTH_HICHAIN, "hichain regDataChangeListener failed");
561             SoftbusHitraceStop();
562             return SOFTBUS_AUTH_INIT_FAIL;
563         }
564         g_regDataChangeListener = true;
565     }
566     uint32_t waitNum = AddAuthRequest(request);
567     if (waitNum == 0) {
568         AUTH_LOGE(AUTH_CONN, "add verify request to list fail, requestId=%{public}u", request->requestId);
569         SoftbusHitraceStop();
570         return SOFTBUS_AUTH_INNER_ERR;
571     }
572     if (waitNum > 1) {
573         AUTH_LOGI(
574             AUTH_CONN, "wait last verify request complete, waitNum=%{public}u, requestId=%{public}u",
575             waitNum, request->requestId);
576         SoftbusHitraceStop();
577         return SOFTBUS_OK;
578     }
579     if (ConnectAuthDevice(request->requestId, &request->connInfo, CONN_SIDE_ANY) != SOFTBUS_OK) {
580         AUTH_LOGE(AUTH_CONN, "connect auth device failed: requestId=%{public}u", request->requestId);
581         FindAndDelAuthRequestByConnInfo(request->requestId, &request->connInfo);
582         SoftbusHitraceStop();
583         return SOFTBUS_AUTH_CONN_FAIL;
584     }
585     SoftbusHitraceStop();
586     AUTH_LOGI(AUTH_CONN, "verify device succ. requestId=%{public}u", request->requestId);
587     return SOFTBUS_OK;
588 }
589 
StartConnVerifyDevice(uint32_t requestId,const AuthConnInfo * connInfo,const AuthConnCallback * connCb,AuthVerifyModule module,bool isFastAuth)590 static int32_t StartConnVerifyDevice(uint32_t requestId, const AuthConnInfo *connInfo, const AuthConnCallback *connCb,
591     AuthVerifyModule module, bool isFastAuth)
592 {
593     if (connInfo == NULL || connCb == NULL) {
594         AUTH_LOGE(AUTH_CONN, "invalid param");
595         return SOFTBUS_INVALID_PARAM;
596     }
597     AuthRequest request;
598     (void)memset_s(&request, sizeof(AuthRequest), 0, sizeof(AuthRequest));
599     request.connCb = *connCb;
600     request.module = module;
601     request.requestId = requestId;
602     request.connInfo = *connInfo;
603     request.authId = AUTH_INVALID_ID;
604     request.type = REQUEST_TYPE_VERIFY;
605     request.isFastAuth = isFastAuth;
606     return VerifyDevice(&request);
607 }
608 
StartVerifyDevice(const AuthVerifyParam * authVerifyParam,const AuthConnInfo * connInfo,const AuthVerifyCallback * verifyCb)609 static int32_t StartVerifyDevice(const AuthVerifyParam *authVerifyParam, const AuthConnInfo *connInfo,
610     const AuthVerifyCallback *verifyCb)
611 {
612     if (connInfo == NULL || verifyCb == NULL || authVerifyParam == NULL) {
613         AUTH_LOGE(AUTH_CONN, "invalid param");
614         return SOFTBUS_INVALID_PARAM;
615     }
616     AuthRequest request;
617     (void)memset_s(&request, sizeof(AuthRequest), 0, sizeof(AuthRequest));
618     request.verifyCb = *verifyCb;
619     request.module = authVerifyParam->module;
620     request.requestId = authVerifyParam->requestId;
621     request.connInfo = *connInfo;
622     request.authId = AUTH_INVALID_ID;
623     request.type = REQUEST_TYPE_VERIFY;
624     request.isFastAuth = authVerifyParam->isFastAuth;
625     request.deviceKeyId = authVerifyParam->deviceKeyId;
626     request.forceJoinInfo = authVerifyParam->forceJoinInfo;
627     request.deviceTypeId = authVerifyParam->deviceTypeId;
628     return VerifyDevice(&request);
629 }
630 
AuthStartReconnectDevice(AuthHandle authHandle,const AuthConnInfo * connInfo,uint32_t requestId,const AuthConnCallback * connCb)631 int32_t AuthStartReconnectDevice(
632     AuthHandle authHandle, const AuthConnInfo *connInfo, uint32_t requestId, const AuthConnCallback *connCb)
633 {
634     AUTH_CHECK_AND_RETURN_RET_LOGE(connInfo != NULL, SOFTBUS_INVALID_PARAM, AUTH_CONN, "connInfo is NULL");
635     AUTH_CHECK_AND_RETURN_RET_LOGE(connCb != NULL, SOFTBUS_INVALID_PARAM, AUTH_CONN, "connCb is NULL");
636     if (connInfo->type < AUTH_LINK_TYPE_WIFI || connInfo->type >= AUTH_LINK_TYPE_MAX) {
637         AUTH_LOGE(AUTH_CONN, "connInfo type error");
638         return SOFTBUS_INVALID_PARAM;
639     }
640     AUTH_LOGI(AUTH_CONN, "start reconnect device. requestId=%{public}u, authId=%{public}" PRId64,
641         requestId, authHandle.authId);
642     AuthManager *auth = GetAuthManagerByAuthId(authHandle.authId);
643     if (auth == NULL) {
644         return SOFTBUS_AUTH_NOT_FOUND;
645     }
646     ConnSideType sideType = GetConnSideType(auth->connId[connInfo->type]);
647     uint64_t connId = auth->connId[AUTH_LINK_TYPE_BR];
648     DelDupAuthManager(auth);
649 
650     AuthRequest request;
651     (void)memset_s(&request, sizeof(AuthRequest), 0, sizeof(AuthRequest));
652     request.authId = authHandle.authId;
653     request.connCb = *connCb;
654     request.connInfo = *connInfo;
655     request.requestId = requestId;
656     request.type = REQUEST_TYPE_RECONNECT;
657     request.isFastAuth = true;
658     if (connInfo->type == AUTH_LINK_TYPE_BR) {
659         request.connInfo.info.brInfo.connectionId = GetConnId(connId);
660     }
661     if (AddAuthRequest(&request) == 0) {
662         AUTH_LOGE(AUTH_CONN, "add reconnect request fail, requestId=%{public}u", requestId);
663         return SOFTBUS_AUTH_ADD_REQUEST_FAIL;
664     }
665     if (ConnectAuthDevice(requestId, &request.connInfo, sideType) != SOFTBUS_OK) {
666         DelAuthRequest(requestId);
667         return SOFTBUS_AUTH_CONN_FAIL;
668     }
669     return SOFTBUS_OK;
670 }
671 
AuthCheckSessionKey(AuthHandle * authHandle)672 static bool AuthCheckSessionKey(AuthHandle *authHandle)
673 {
674     AuthManager *auth = GetAuthManagerByAuthId(authHandle->authId);
675     if (auth == NULL) {
676         AUTH_LOGE(AUTH_CONN, "not found auth manager, authId=%{public}" PRId64, authHandle->authId);
677         return false;
678     }
679     bool res = CheckSessionKeyListExistType(&auth->sessionKeyList, (AuthLinkType)authHandle->type);
680     DelDupAuthManager(auth);
681     return res;
682 }
683 
AuthDeviceOpenConn(const AuthConnInfo * info,uint32_t requestId,const AuthConnCallback * callback)684 int32_t AuthDeviceOpenConn(const AuthConnInfo *info, uint32_t requestId, const AuthConnCallback *callback)
685 {
686     if (info == NULL || !CheckAuthConnCallback(callback)) {
687         AUTH_LOGE(AUTH_CONN, "invalid param");
688         return SOFTBUS_INVALID_PARAM;
689     }
690     AUTH_CHECK_AND_RETURN_RET_LOGE(CheckAuthConnInfoType(info), SOFTBUS_INVALID_PARAM,
691         AUTH_FSM, "connInfo type error");
692     AUTH_LOGI(AUTH_CONN, "open auth conn: connType=%{public}d, requestId=%{public}u", info->type, requestId);
693     AuthHandle authHandle = { .authId = AUTH_INVALID_ID, .type = info->type };
694     bool judgeTimeOut = false;
695     switch (info->type) {
696         case AUTH_LINK_TYPE_WIFI:
697         case AUTH_LINK_TYPE_SESSION_KEY:
698         case AUTH_LINK_TYPE_USB:
699             authHandle.authId = GetLatestIdByConnInfo(info);
700             if (authHandle.authId == AUTH_INVALID_ID) {
701                 return SOFTBUS_AUTH_NOT_FOUND;
702             }
703             callback->onConnOpened(requestId, authHandle);
704             break;
705         case AUTH_LINK_TYPE_SLE:
706             /* fall-through */
707         case AUTH_LINK_TYPE_BR:
708             /* fall-through */
709         case AUTH_LINK_TYPE_BLE:
710             judgeTimeOut = true;
711             authHandle.authId = GetActiveAuthIdByConnInfo(info, judgeTimeOut);
712             if (authHandle.authId != AUTH_INVALID_ID && AuthCheckSessionKey(&authHandle)) {
713                 return AuthStartReconnectDevice(authHandle, info, requestId, callback);
714             }
715             return StartConnVerifyDevice(requestId, info, callback, AUTH_MODULE_LNN, true);
716         case AUTH_LINK_TYPE_P2P:
717         case AUTH_LINK_TYPE_ENHANCED_P2P:
718             authHandle.authId = GetActiveAuthIdByConnInfo(info, judgeTimeOut);
719             if (authHandle.authId != AUTH_INVALID_ID) {
720                 AUTH_LOGI(AUTH_CONN, "reuse type=%{public}d, authId=%{public}" PRId64, info->type, authHandle.authId);
721                 callback->onConnOpened(requestId, authHandle);
722                 break;
723             }
724             return StartConnVerifyDevice(requestId, info, callback, AUTH_MODULE_LNN, true);
725         default:
726             AUTH_LOGE(AUTH_CONN, "unknown connType. type=%{public}d", info->type);
727             return SOFTBUS_INVALID_PARAM;
728     }
729     return SOFTBUS_OK;
730 }
731 
AuthDeviceCloseConn(AuthHandle authHandle)732 void AuthDeviceCloseConn(AuthHandle authHandle)
733 {
734     AUTH_LOGI(AUTH_CONN, "close auth conn: authId=%{public}" PRId64, authHandle.authId);
735     if (authHandle.type < AUTH_LINK_TYPE_WIFI || authHandle.type >= AUTH_LINK_TYPE_MAX) {
736         AUTH_LOGE(AUTH_CONN, "authHandle type error");
737         return;
738     }
739     AuthManager *auth = GetAuthManagerByAuthId(authHandle.authId);
740     if (auth == NULL) {
741         return;
742     }
743     switch (auth->connInfo[authHandle.type].type) {
744         case AUTH_LINK_TYPE_WIFI:
745         case AUTH_LINK_TYPE_P2P:
746         case AUTH_LINK_TYPE_ENHANCED_P2P:
747         case AUTH_LINK_TYPE_SESSION_KEY:
748         case AUTH_LINK_TYPE_USB:
749             /* Do nothing. */
750             break;
751         case AUTH_LINK_TYPE_BR:
752         case AUTH_LINK_TYPE_BLE:
753         case AUTH_LINK_TYPE_SLE:
754             DisconnectAuthDevice(&auth->connId[authHandle.type]);
755             break;
756         default:
757             break;
758     }
759     DelDupAuthManager(auth);
760     return;
761 }
762 
AuthStartVerify(const AuthConnInfo * connInfo,const AuthVerifyParam * authVerifyParam,const AuthVerifyCallback * callback)763 int32_t AuthStartVerify(const AuthConnInfo *connInfo, const AuthVerifyParam *authVerifyParam,
764     const AuthVerifyCallback *callback)
765 {
766     if (connInfo == NULL || authVerifyParam == NULL || !CheckVerifyCallback(callback)) {
767         AUTH_LOGE(AUTH_CONN, "invalid param");
768         return SOFTBUS_INVALID_PARAM;
769     }
770     AUTH_CHECK_AND_RETURN_RET_LOGE(CheckAuthConnInfoType(connInfo), SOFTBUS_INVALID_PARAM,
771         AUTH_FSM, "connInfo type error");
772     return StartVerifyDevice(authVerifyParam, connInfo, callback);
773 }
774 
AuthStartConnVerify(const AuthConnInfo * connInfo,uint32_t requestId,const AuthConnCallback * connCallback,AuthVerifyModule module,bool isFastAuth)775 int32_t AuthStartConnVerify(const AuthConnInfo *connInfo, uint32_t requestId, const AuthConnCallback *connCallback,
776     AuthVerifyModule module, bool isFastAuth)
777 {
778     if (connInfo == NULL || !CheckAuthConnCallback(connCallback)) {
779         AUTH_LOGE(AUTH_CONN, "invalid param");
780         return SOFTBUS_INVALID_PARAM;
781     }
782     AUTH_CHECK_AND_RETURN_RET_LOGE(CheckAuthConnInfoType(connInfo), SOFTBUS_INVALID_PARAM,
783         AUTH_FSM, "connInfo type error");
784     return StartConnVerifyDevice(requestId, connInfo, connCallback, module, isFastAuth);
785 }