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_session_message.h"
17
18 #include <math.h>
19 #include <securec.h>
20
21 #include "anonymizer.h"
22 #include "auth_attest_interface.h"
23 #include "auth_common.h"
24 #include "auth_request.h"
25 #include "auth_connection.h"
26 #include "auth_device_common_key.h"
27 #include "auth_hichain_adapter.h"
28 #include "auth_log.h"
29 #include "auth_manager.h"
30 #include "auth_session_json.h"
31 #include "auth_meta_manager.h"
32 #include "bus_center_manager.h"
33 #include "lnn_common_utils.h"
34 #include "lnn_event.h"
35 #include "lnn_extdata_config.h"
36 #include "lnn_local_net_ledger.h"
37 #include "lnn_feature_capability.h"
38 #include "lnn_network_manager.h"
39 #include "lnn_node_info.h"
40 #include "softbus_adapter_json.h"
41 #include "softbus_adapter_mem.h"
42 #include "softbus_adapter_socket.h"
43 #include "softbus_def.h"
44 #include "softbus_error_code.h"
45 #include "softbus_json_utils.h"
46 #include "lnn_compress.h"
47 #include "softbus_adapter_timer.h"
48 #include "softbus_socket.h"
49
50 #define FLAG_COMPRESS_DEVICE_INFO 1
51 #define FLAG_UNCOMPRESS_DEVICE_INFO 0
52 #define FLAG_RELAY_DEVICE_INFO 1
53 #define DEVICE_ID_STR_LEN 64 // for bt v1
54
UnPackBtDeviceIdV1(AuthSessionInfo * info,const uint8_t * data,uint32_t len)55 static int32_t UnPackBtDeviceIdV1(AuthSessionInfo *info, const uint8_t *data, uint32_t len)
56 {
57 if (!info->isServer) {
58 AUTH_LOGE(AUTH_FSM, "is not server");
59 return SOFTBUS_INVALID_PARAM;
60 }
61 if (memcpy_s(info->udid, UDID_BUF_LEN, data, len) != EOK) { // data:StandardCharsets.UTF_8
62 AUTH_LOGE(AUTH_FSM, "memcpy fail");
63 return SOFTBUS_MEM_ERR;
64 }
65 return SOFTBUS_OK;
66 }
67
PostDeviceIdData(int64_t authSeq,const AuthSessionInfo * info,uint8_t * data,uint32_t len)68 static int32_t PostDeviceIdData(int64_t authSeq, const AuthSessionInfo *info, uint8_t *data, uint32_t len)
69 {
70 AuthDataHead head = {
71 .dataType = DATA_TYPE_DEVICE_ID,
72 .module = MODULE_TRUST_ENGINE,
73 .seq = authSeq,
74 .flag = info->isConnectServer ? SERVER_SIDE_FLAG : CLIENT_SIDE_FLAG,
75 .len = len,
76 };
77 if (PostAuthData(info->connId, !info->isServer, &head, data) != SOFTBUS_OK) {
78 AUTH_LOGE(AUTH_FSM, "post device id fail");
79 return SOFTBUS_AUTH_SEND_FAIL;
80 }
81 return SOFTBUS_OK;
82 }
83
PostBtV1DevId(int64_t authSeq,const AuthSessionInfo * info)84 static int32_t PostBtV1DevId(int64_t authSeq, const AuthSessionInfo *info)
85 {
86 if (!info->isServer) {
87 AUTH_LOGE(AUTH_FSM, "client don't send Bt-v1 devId");
88 return SOFTBUS_ERR;
89 }
90 char uuid[UUID_BUF_LEN] = {0};
91 if (LnnGetLocalStrInfo(STRING_KEY_UUID, uuid, UUID_BUF_LEN) != SOFTBUS_OK) {
92 AUTH_LOGE(AUTH_FSM, "get uuid fail");
93 return SOFTBUS_ERR;
94 }
95 return PostDeviceIdData(authSeq, info, (uint8_t *)uuid, strlen(uuid));
96 }
97
PostWifiV1DevId(int64_t authSeq,const AuthSessionInfo * info)98 static int32_t PostWifiV1DevId(int64_t authSeq, const AuthSessionInfo *info)
99 {
100 if (!info->isServer) {
101 AUTH_LOGE(AUTH_FSM, "client don't send wifi-v1 devId");
102 return SOFTBUS_ERR;
103 }
104 char *msg = PackDeviceIdJson(info);
105 if (msg == NULL) {
106 AUTH_LOGE(AUTH_FSM, "pack devId fail");
107 return SOFTBUS_ERR;
108 }
109 if (PostDeviceIdData(authSeq, info, (uint8_t *)msg, strlen(msg) + 1) != SOFTBUS_OK) {
110 JSON_Free(msg);
111 return SOFTBUS_ERR;
112 }
113 JSON_Free(msg);
114 return SOFTBUS_OK;
115 }
116
PostDeviceIdV1(int64_t authSeq,const AuthSessionInfo * info)117 static int32_t PostDeviceIdV1(int64_t authSeq, const AuthSessionInfo *info)
118 {
119 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI) {
120 return PostWifiV1DevId(authSeq, info);
121 } else {
122 AUTH_LOGI(AUTH_FSM, "process v1 bt deviceIdSync");
123 return PostBtV1DevId(authSeq, info);
124 }
125 }
126
PostDeviceIdNew(int64_t authSeq,const AuthSessionInfo * info)127 static int32_t PostDeviceIdNew(int64_t authSeq, const AuthSessionInfo *info)
128 {
129 char *msg = PackDeviceIdJson(info);
130 if (msg == NULL) {
131 AUTH_LOGE(AUTH_FSM, "pack devId fail");
132 return SOFTBUS_ERR;
133 }
134 if (PostDeviceIdData(authSeq, info, (uint8_t *)msg, strlen(msg) + 1) != SOFTBUS_OK) {
135 JSON_Free(msg);
136 return SOFTBUS_AUTH_SEND_FAIL;
137 }
138 JSON_Free(msg);
139 return SOFTBUS_OK;
140 }
141
DfxRecordLnnPostDeviceIdStart(int64_t authSeq,const AuthSessionInfo * info)142 static void DfxRecordLnnPostDeviceIdStart(int64_t authSeq, const AuthSessionInfo *info)
143 {
144 LnnEventExtra extra = { 0 };
145 LnnEventExtraInit(&extra);
146 extra.authId = (int32_t)authSeq;
147 if (info != NULL) {
148 extra.authRequestId = (int32_t)info->requestId;
149 }
150 LNN_EVENT(EVENT_SCENE_JOIN_LNN, EVENT_STAGE_AUTH_DEVICE_ID_POST, extra);
151 }
152
PostDeviceIdMessage(int64_t authSeq,const AuthSessionInfo * info)153 int32_t PostDeviceIdMessage(int64_t authSeq, const AuthSessionInfo *info)
154 {
155 DfxRecordLnnPostDeviceIdStart(authSeq, info);
156 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
157 if (info->version == SOFTBUS_OLD_V1) {
158 return PostDeviceIdV1(authSeq, info);
159 } else {
160 return PostDeviceIdNew(authSeq, info);
161 }
162 }
163
ProcessDeviceIdMessage(AuthSessionInfo * info,const uint8_t * data,uint32_t len)164 int32_t ProcessDeviceIdMessage(AuthSessionInfo *info, const uint8_t *data, uint32_t len)
165 {
166 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
167 CHECK_NULL_PTR_RETURN_VALUE(data, SOFTBUS_INVALID_PARAM);
168 if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) && (len == DEVICE_ID_STR_LEN) && (info->isServer)) {
169 info->version = SOFTBUS_OLD_V1;
170 return UnPackBtDeviceIdV1(info, data, len);
171 }
172 return UnpackDeviceIdJson((const char *)data, len, info);
173 }
174
GetDumpSessionKeyList(int64_t authSeq,const AuthSessionInfo * info,SessionKeyList * list)175 static void GetDumpSessionKeyList(int64_t authSeq, const AuthSessionInfo *info, SessionKeyList *list)
176 {
177 ListInit(list);
178 SessionKey sessionKey;
179 int64_t index = authSeq;
180 if (info->normalizedType == NORMALIZED_SUPPORT) {
181 index = info->normalizedIndex;
182 }
183 if (AuthManagerGetSessionKey(index, info, &sessionKey) != SOFTBUS_OK) {
184 AUTH_LOGE(AUTH_FSM, "get session key fail");
185 return;
186 }
187 if (AddSessionKey(list, TO_INT32(index), &sessionKey, info->connInfo.type, false) != SOFTBUS_OK) {
188 AUTH_LOGE(AUTH_FSM, "add session key fail");
189 (void)memset_s(&sessionKey, sizeof(SessionKey), 0, sizeof(SessionKey));
190 return;
191 }
192 (void)memset_s(&sessionKey, sizeof(SessionKey), 0, sizeof(SessionKey));
193 if (SetSessionKeyAvailable(list, TO_INT32(index)) != SOFTBUS_OK) {
194 AUTH_LOGE(AUTH_FSM, "set session key available fail");
195 }
196 }
197
DfxRecordLnnPostDeviceInfoStart(int64_t authSeq,const AuthSessionInfo * info)198 static void DfxRecordLnnPostDeviceInfoStart(int64_t authSeq, const AuthSessionInfo *info)
199 {
200 LnnEventExtra extra = { 0 };
201 LnnEventExtraInit(&extra);
202 extra.authId = (int32_t)authSeq;
203 if (info != NULL) {
204 extra.authRequestId = (int32_t)info->requestId;
205 }
206 LNN_EVENT(EVENT_SCENE_JOIN_LNN, EVENT_STAGE_AUTH_DEVICE_INFO_POST, extra);
207 }
208
SetCompressFlagByAuthInfo(const AuthSessionInfo * info,char * msg,int32_t * compressFlag,uint8_t ** compressData,uint32_t * compressLen)209 static void SetCompressFlagByAuthInfo(const AuthSessionInfo *info, char *msg, int32_t *compressFlag,
210 uint8_t **compressData, uint32_t *compressLen)
211 {
212 if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) && info->isSupportCompress) {
213 AUTH_LOGI(AUTH_FSM, "before compress, datalen=%{public}zu", strlen(msg) + 1);
214 if (DataCompress((uint8_t *)msg, strlen(msg) + 1, compressData, compressLen) != SOFTBUS_OK) {
215 *compressFlag = FLAG_UNCOMPRESS_DEVICE_INFO;
216 } else {
217 *compressFlag = FLAG_COMPRESS_DEVICE_INFO;
218 AUTH_LOGI(AUTH_FSM, "deviceInfo compress finish");
219 }
220 AUTH_LOGI(AUTH_FSM, "after compress, datalen=%{public}u", *compressLen);
221 }
222 }
223
SetIndataInfo(InDataInfo * inDataInfo,uint8_t * compressData,uint32_t compressLen,char * msg)224 static void SetIndataInfo(InDataInfo *inDataInfo, uint8_t *compressData, uint32_t compressLen, char *msg)
225 {
226 if ((compressData != NULL) && (compressLen != 0)) {
227 inDataInfo->inData = compressData;
228 inDataInfo->inLen = compressLen;
229 } else {
230 inDataInfo->inData = (uint8_t *)msg;
231 inDataInfo->inLen = strlen(msg) + 1;
232 }
233 }
234
PostDeviceInfoMessage(int64_t authSeq,const AuthSessionInfo * info)235 int32_t PostDeviceInfoMessage(int64_t authSeq, const AuthSessionInfo *info)
236 {
237 DfxRecordLnnPostDeviceInfoStart(authSeq, info);
238 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
239 char *msg = PackDeviceInfoMessage(&(info->connInfo), info->version, false, info->uuid, info);
240 if (msg == NULL) {
241 AUTH_LOGE(AUTH_FSM, "pack device info fail");
242 return SOFTBUS_ERR;
243 }
244 int32_t compressFlag = FLAG_UNCOMPRESS_DEVICE_INFO;
245 uint8_t *compressData = NULL;
246 uint32_t compressLen = 0;
247 SetCompressFlagByAuthInfo(info, msg, &compressFlag, &compressData, &compressLen);
248 InDataInfo inDataInfo = { 0 };
249 uint8_t *data = NULL;
250 uint32_t dataLen = 0;
251 SetIndataInfo(&inDataInfo, compressData, compressLen, msg);
252 SessionKeyList sessionKeyList;
253 GetDumpSessionKeyList(authSeq, info, &sessionKeyList);
254 if (EncryptInner(&sessionKeyList, info->connInfo.type, &inDataInfo, &data, &dataLen) != SOFTBUS_OK) {
255 AUTH_LOGE(AUTH_FSM, "encrypt device info fail");
256 JSON_Free(msg);
257 SoftBusFree(compressData);
258 return SOFTBUS_ENCRYPT_ERR;
259 }
260 JSON_Free(msg);
261 SoftBusFree(compressData);
262 DestroySessionKeyList(&sessionKeyList);
263 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI && info->isServer) {
264 compressFlag = FLAG_RELAY_DEVICE_INFO;
265 authSeq = 0;
266 }
267 AuthDataHead head = {
268 .dataType = DATA_TYPE_DEVICE_INFO,
269 .module = MODULE_AUTH_CONNECTION,
270 .seq = authSeq,
271 .flag = compressFlag,
272 .len = dataLen,
273 };
274 if (PostAuthData(info->connId, !info->isServer, &head, data) != SOFTBUS_OK) {
275 AUTH_LOGE(AUTH_FSM, "post device info fail");
276 SoftBusFree(data);
277 return SOFTBUS_AUTH_SEND_FAIL;
278 }
279 SoftBusFree(data);
280 return SOFTBUS_OK;
281 }
282
ProcessDeviceInfoMessage(int64_t authSeq,AuthSessionInfo * info,const uint8_t * data,uint32_t len)283 int32_t ProcessDeviceInfoMessage(int64_t authSeq, AuthSessionInfo *info, const uint8_t *data, uint32_t len)
284 {
285 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
286 CHECK_NULL_PTR_RETURN_VALUE(data, SOFTBUS_INVALID_PARAM);
287 uint8_t *msg = NULL;
288 uint32_t msgSize = 0;
289 SessionKeyList sessionKeyList;
290 GetDumpSessionKeyList(authSeq, info, &sessionKeyList);
291 InDataInfo inDataInfo = { .inData = data, .inLen = len };
292 if (DecryptInner(&sessionKeyList, info->connInfo.type, &inDataInfo, &msg, &msgSize) != SOFTBUS_OK) {
293 AUTH_LOGE(AUTH_FSM, "decrypt device info fail");
294 return SOFTBUS_DECRYPT_ERR;
295 }
296 DestroySessionKeyList(&sessionKeyList);
297 uint8_t *decompressData = NULL;
298 uint32_t decompressLen = 0;
299 if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) && info->isSupportCompress) {
300 AUTH_LOGI(AUTH_FSM, "before decompress, msgSize=%{public}u", msgSize);
301 if (DataDecompress((uint8_t *)msg, msgSize, &decompressData, &decompressLen) != SOFTBUS_OK) {
302 AUTH_LOGE(AUTH_FSM, "data decompress fail");
303 SoftBusFree(msg);
304 return SOFTBUS_ERR;
305 } else {
306 AUTH_LOGI(AUTH_FSM, "deviceInfo deCompress finish, decompress=%{public}d", decompressLen);
307 }
308 AUTH_LOGI(AUTH_FSM, "after decompress, datalen=%{public}d", decompressLen);
309 }
310 DevInfoData devInfo = {NULL, 0, info->connInfo.type, info->version};
311 if ((decompressData != NULL) && (decompressLen != 0)) {
312 devInfo.msg = (const char *)decompressData;
313 devInfo.len = decompressLen;
314 } else {
315 devInfo.msg = (const char *)msg;
316 devInfo.len = msgSize;
317 }
318 if (UnpackDeviceInfoMessage(&devInfo, &info->nodeInfo, false, info) != SOFTBUS_OK) {
319 AUTH_LOGE(AUTH_FSM, "unpack device info fail");
320 SoftBusFree(msg);
321 SoftBusFree(decompressData);
322 return SOFTBUS_AUTH_UNPACK_DEVINFO_FAIL;
323 }
324 SoftBusFree(msg);
325 SoftBusFree(decompressData);
326 return SOFTBUS_OK;
327 }
328
PostCloseAckMessage(int64_t authSeq,const AuthSessionInfo * info)329 int32_t PostCloseAckMessage(int64_t authSeq, const AuthSessionInfo *info)
330 {
331 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
332 const char *msg = "";
333 AuthDataHead head = {
334 .dataType = DATA_TYPE_CLOSE_ACK,
335 .module = 0,
336 .seq = authSeq,
337 .flag = 0,
338 .len = strlen(msg) + 1,
339 };
340 if (PostAuthData(info->connId, !info->isServer, &head, (uint8_t *)msg) != SOFTBUS_OK) {
341 AUTH_LOGE(AUTH_FSM, "post close ack fail");
342 return SOFTBUS_AUTH_SEND_FAIL;
343 }
344 return SOFTBUS_OK;
345 }
346
PostHichainAuthMessage(int64_t authSeq,const AuthSessionInfo * info,const uint8_t * data,uint32_t len)347 int32_t PostHichainAuthMessage(int64_t authSeq, const AuthSessionInfo *info, const uint8_t *data, uint32_t len)
348 {
349 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
350 CHECK_NULL_PTR_RETURN_VALUE(data, SOFTBUS_INVALID_PARAM);
351 AuthDataHead head = {
352 .dataType = DATA_TYPE_AUTH,
353 .module = MODULE_AUTH_SDK,
354 .seq = authSeq,
355 .flag = 0,
356 .len = len,
357 };
358 if (PostAuthData(info->connId, !info->isServer, &head, data) != SOFTBUS_OK) {
359 AUTH_LOGE(AUTH_FSM, "post hichain data fail");
360 return SOFTBUS_AUTH_SEND_FAIL;
361 }
362 return SOFTBUS_OK;
363 }
364
PackVerifyDeviceMessage(const char * uuid)365 static char *PackVerifyDeviceMessage(const char *uuid)
366 {
367 JsonObj *obj = JSON_CreateObject();
368 if (obj == NULL) {
369 AUTH_LOGE(AUTH_FSM, "create json fail");
370 return NULL;
371 }
372 if (!JSON_AddInt32ToObject(obj, CODE, CODE_VERIFY_DEVICE) || !JSON_AddStringToObject(obj, DEVICE_ID, uuid)) {
373 AUTH_LOGE(AUTH_FSM, "add uuid fail");
374 JSON_Delete(obj);
375 return NULL;
376 }
377 char *msg = JSON_PrintUnformatted(obj);
378 JSON_Delete(obj);
379 return msg;
380 }
381
PackKeepaliveMessage(const char * uuid,ModeCycle cycle)382 static char *PackKeepaliveMessage(const char *uuid, ModeCycle cycle)
383 {
384 JsonObj *obj = JSON_CreateObject();
385 if (obj == NULL) {
386 AUTH_LOGE(AUTH_FSM, "create json fail");
387 return NULL;
388 }
389 if (!JSON_AddInt32ToObject(obj, CODE, CODE_TCP_KEEPALIVE) || !JSON_AddStringToObject(obj, DEVICE_ID, uuid) ||
390 !JSON_AddInt32ToObject(obj, TIME, cycle)) {
391 AUTH_LOGE(AUTH_FSM, "add uuid or cycle fail");
392 JSON_Delete(obj);
393 return NULL;
394 }
395 char *msg = JSON_PrintUnformatted(obj);
396 JSON_Delete(obj);
397 return msg;
398 }
399
IsDeviceMessagePacket(const AuthConnInfo * connInfo,const AuthDataHead * head,const uint8_t * data,bool isServer,DeviceMessageParse * messageParse)400 bool IsDeviceMessagePacket(const AuthConnInfo *connInfo, const AuthDataHead *head, const uint8_t *data, bool isServer,
401 DeviceMessageParse *messageParse)
402 {
403 if (connInfo->type != AUTH_LINK_TYPE_WIFI) {
404 return false;
405 }
406 int64_t authId = AuthDeviceGetIdByConnInfo(connInfo, isServer);
407 if (authId == AUTH_INVALID_ID) {
408 AUTH_LOGE(AUTH_FSM, "is flush device packet not find authId");
409 return false;
410 }
411 uint32_t decDataLen = AuthGetDecryptSize(head->len);
412 uint8_t *decData = (uint8_t *)SoftBusCalloc(decDataLen);
413 if (decData == NULL) {
414 return false;
415 }
416 AuthHandle authHandle = { .authId = authId, .type = connInfo->type };
417 if (AuthDeviceDecrypt(&authHandle, data, head->len, decData, &decDataLen) != SOFTBUS_OK) {
418 SoftBusFree(decData);
419 AUTH_LOGE(AUTH_FSM, "parse device info decrypt fail");
420 return false;
421 }
422 JsonObj *json = JSON_Parse((char *)decData, decDataLen);
423 if (json == NULL) {
424 AUTH_LOGE(AUTH_FSM, "parse json fail");
425 SoftBusFree(decData);
426 return false;
427 }
428 bool result = false;
429 if (!JSON_GetInt32FromOject(json, CODE, &messageParse->messageType)) {
430 AUTH_LOGE(AUTH_FSM, "parse messageType fail");
431 }
432 AUTH_LOGI(AUTH_FSM, "messageType=%{public}d", messageParse->messageType);
433 if (messageParse->messageType == CODE_VERIFY_DEVICE) {
434 result = true;
435 }
436 if (messageParse->messageType == CODE_TCP_KEEPALIVE) {
437 if (JSON_GetInt32FromOject(json, TIME, (int32_t *)&messageParse->cycle)) {
438 AUTH_LOGI(AUTH_FSM, "parse keepalive cycle success, cycle=%{public}d", messageParse->cycle);
439 result = true;
440 }
441 }
442 JSON_Delete(json);
443 SoftBusFree(decData);
444 return result;
445 }
446
IsEmptyShortHashStr(char * udidHash)447 static bool IsEmptyShortHashStr(char *udidHash)
448 {
449 if (strlen(udidHash) == 0) {
450 AUTH_LOGE(AUTH_FSM, "udidHash len is 0");
451 return true;
452 }
453 uint8_t emptyHash[SHORT_HASH_LEN] = {0};
454 char emptyHashStr[UDID_SHORT_HASH_HEX_STR + 1] = {0};
455 if (ConvertBytesToHexString(emptyHashStr, UDID_SHORT_HASH_HEX_STR + 1, emptyHash, SHORT_HASH_LEN) != SOFTBUS_OK) {
456 AUTH_LOGE(AUTH_FSM, "convert bytes to string fail");
457 return false;
458 }
459 if (strncmp(emptyHashStr, udidHash, strlen(emptyHashStr)) == EOK) {
460 AUTH_LOGE(AUTH_FSM, "udidHash is null");
461 return true;
462 }
463 return false;
464 }
465
GetLocalUdidHash(char * udid,char * udidHash,uint32_t len)466 static int32_t GetLocalUdidHash(char *udid, char *udidHash, uint32_t len)
467 {
468 if (udid == NULL || udidHash == NULL || len < UDID_HASH_LEN) {
469 AUTH_LOGE(AUTH_FSM, "invalid param");
470 return SOFTBUS_INVALID_PARAM;
471 }
472 uint8_t hash[UDID_HASH_LEN] = {0};
473 if (SoftBusGenerateStrHash((unsigned char *)udid, strlen(udid), (unsigned char *)hash) != SOFTBUS_OK) {
474 AUTH_LOGE(AUTH_FSM, "restore manager fail because generate strhash");
475 return SOFTBUS_ERR;
476 }
477 if (ConvertBytesToHexString(udidHash, len, hash, UDID_SHORT_HASH_LEN_TEMP) != SOFTBUS_OK) {
478 AUTH_LOGE(AUTH_FSM, "convert bytes to string fail");
479 return SOFTBUS_ERR;
480 }
481 return SOFTBUS_OK;
482 }
483
UpdateLocalAuthState(int64_t authSeq,AuthSessionInfo * info)484 int32_t UpdateLocalAuthState(int64_t authSeq, AuthSessionInfo *info)
485 {
486 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
487 if (info->isServer && strlen(info->udid) == 0) {
488 info->localState = AUTH_STATE_UNKNOW;
489 AUTH_LOGI(AUTH_FSM, "authSeq=%{public}" PRId64 ", udid is null update local auth state=%{public}d",
490 authSeq, info->localState);
491 return SOFTBUS_OK;
492 }
493 if (info->peerState == AUTH_STATE_COMPATIBLE) {
494 info->localState = AUTH_STATE_COMPATIBLE;
495 AUTH_LOGI(AUTH_FSM, "authSeq=%{public}" PRId64 " local auth state=%{public}d", authSeq, info->localState);
496 return SOFTBUS_OK;
497 }
498 if (info->peerState == AUTH_STATE_ACK || info->peerState == AUTH_STATE_START) {
499 info->localState = AUTH_STATE_ACK;
500 AUTH_LOGI(AUTH_FSM, "authSeq=%{public}" PRId64 " local auth state=%{public}d", authSeq, info->localState);
501 return SOFTBUS_OK;
502 }
503 char udid[UDID_BUF_LEN] = {0};
504 if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, udid, UDID_BUF_LEN) != SOFTBUS_OK) {
505 AUTH_LOGE(AUTH_FSM, "get local udid fail");
506 return SOFTBUS_NETWORK_GET_LOCAL_NODE_INFO_ERR;
507 }
508 char localUdidHash[SHA_256_HEX_HASH_LEN] = { 0 };
509 char udidHash[SHA_256_HEX_HASH_LEN] = { 0 };
510 if (GetLocalUdidHash(udid, localUdidHash, SHA_256_HEX_HASH_LEN) != SOFTBUS_OK) {
511 AUTH_LOGE(AUTH_FSM, "get local udid hash fail");
512 return SOFTBUS_ERR;
513 }
514 if (!GetUdidShortHash(info, udidHash, SHA_256_HEX_HASH_LEN) ||
515 IsEmptyShortHashStr(udidHash)) {
516 AUTH_LOGI(AUTH_FSM, "unknow peer udidHash");
517 info->localState = AUTH_STATE_UNKNOW;
518 } else if (memcmp(localUdidHash, udidHash, SHORT_HASH_LEN) < 0) {
519 info->localState = AUTH_STATE_WAIT;
520 } else if (memcmp(localUdidHash, udidHash, SHORT_HASH_LEN) > 0) {
521 info->localState = AUTH_STATE_START;
522 } else {
523 AUTH_LOGE(AUTH_FSM, "peer udidHash = local udidHash!");
524 info->localState = AUTH_STATE_START;
525 }
526 if (strlen(udidHash) != 0 && strcpy_s(info->udidHash, SHA_256_HEX_HASH_LEN, udidHash) != EOK) {
527 AUTH_LOGE(AUTH_FSM, "memcpy udidHash fail");
528 return SOFTBUS_MEM_ERR;
529 }
530 AUTH_LOGI(AUTH_FSM, "authSeq=%{public}" PRId64 " local auth state=%{public}d", authSeq, info->localState);
531 return SOFTBUS_OK;
532 }
533
PostDeviceMessage(const AuthManager * auth,int32_t flagRelay,AuthLinkType type,const DeviceMessageParse * messageParse)534 int32_t PostDeviceMessage(
535 const AuthManager *auth, int32_t flagRelay, AuthLinkType type, const DeviceMessageParse *messageParse)
536 {
537 CHECK_NULL_PTR_RETURN_VALUE(auth, SOFTBUS_INVALID_PARAM);
538 if (messageParse == NULL) {
539 AUTH_LOGE(AUTH_FSM, "invalid param");
540 return SOFTBUS_INVALID_PARAM;
541 }
542 if (type < AUTH_LINK_TYPE_WIFI || type >= AUTH_LINK_TYPE_MAX) {
543 AUTH_LOGE(AUTH_FSM, "type error, type=%{public}d", type);
544 return SOFTBUS_ERR;
545 }
546 char *msg = NULL;
547 if (messageParse->messageType == CODE_VERIFY_DEVICE) {
548 msg = PackVerifyDeviceMessage(auth->uuid);
549 } else if (messageParse->messageType == CODE_TCP_KEEPALIVE) {
550 msg = PackKeepaliveMessage(auth->uuid, messageParse->cycle);
551 }
552 if (msg == NULL) {
553 AUTH_LOGE(AUTH_FSM, "pack verify device msg fail");
554 return SOFTBUS_ERR;
555 }
556
557 uint8_t *data = NULL;
558 uint32_t dataLen = 0;
559 InDataInfo inDataInfo = { .inData = (uint8_t *)msg, .inLen = strlen(msg) + 1 };
560 if (EncryptInner(&auth->sessionKeyList, type, &inDataInfo, &data, &dataLen) != SOFTBUS_OK) {
561 AUTH_LOGE(AUTH_FSM, "encrypt device info fail");
562 JSON_Free(msg);
563 return SOFTBUS_ENCRYPT_ERR;
564 }
565 JSON_Free(msg);
566
567 AuthDataHead head = {
568 .dataType = DATA_TYPE_DEVICE_INFO,
569 .module = MODULE_AUTH_CONNECTION,
570 .seq = auth->authId,
571 .flag = flagRelay,
572 .len = dataLen,
573 };
574 if (PostAuthData(auth->connId[type], !auth->isServer, &head, data) != SOFTBUS_OK) {
575 AUTH_LOGE(AUTH_FSM, "post verify device msg fail");
576 SoftBusFree(data);
577 return SOFTBUS_AUTH_SEND_FAIL;
578 }
579 SoftBusFree(data);
580 return SOFTBUS_OK;
581 }
582