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 "auth_session_message.h"
17
18 #include <math.h>
19 #include <securec.h>
20
21 #include "auth_common.h"
22 #include "auth_request.h"
23 #include "auth_connection.h"
24 #include "auth_interface.h"
25 #include "auth_manager.h"
26 #include "bus_center_manager.h"
27 #include "lnn_cipherkey_manager.h"
28 #include "lnn_common_utils.h"
29 #include "lnn_extdata_config.h"
30 #include "lnn_local_net_ledger.h"
31 #include "lnn_feature_capability.h"
32 #include "lnn_network_manager.h"
33 #include "lnn_node_info.h"
34 #include "softbus_adapter_mem.h"
35 #include "softbus_adapter_socket.h"
36 #include "softbus_def.h"
37 #include "softbus_common.h"
38 #include "softbus_json_utils.h"
39 #include "lnn_compress.h"
40 #include "softbus_adapter_json.h"
41 #include "auth_device_common_key.h"
42 #include "auth_hichain_adapter.h"
43
44 /* DeviceId */
45 #define CMD_TAG "TECmd"
46 #define CMD_GET_AUTH_INFO "getAuthInfo"
47 #define CMD_RET_AUTH_INFO "retAuthInfo"
48 #define DATA_TAG "TEData"
49 #define DEVICE_ID_TAG "TEDeviceId"
50 #define DATA_BUF_SIZE_TAG "DataBufSize"
51 #define SOFTBUS_VERSION_TAG "softbusVersion"
52 #define SUPPORT_INFO_COMPRESS "supportInfoCompress"
53 #define EXCHANGE_ID_TYPE "exchangeIdType"
54 #define CMD_TAG_LEN 30
55 #define PACKET_SIZE (64 * 1024)
56
57 /* DeviceInfo-WiFi */
58 #define CODE_VERIFY_IP 1
59 #define BUS_MAX_VERSION "BUS_MAX_VERSION"
60 #define BUS_MIN_VERSION "BUS_MIN_VERSION"
61 #define AUTH_PORT "AUTH_PORT"
62 #define SESSION_PORT "SESSION_PORT"
63 #define PROXY_PORT "PROXY_PORT"
64 #define DEV_IP "DEV_IP"
65 #define BLE_OFFLINE_CODE "OFFLINE_CODE"
66 #define BUS_V1 1
67 #define BUS_V2 2
68
69 /* DeviceInfo-BT */
70 #define CODE_VERIFY_BT 5
71 #define DISCOVERY_TYPE "DISCOVERY_TYPE"
72 #define UUID "UUID"
73 #define DEVICE_VERSION_TYPE "DEVICE_VERSION_TYPE"
74 #define BR_MAC_ADDR "BR_MAC_ADDR"
75 #define CONNECT_INFO "CONNECT_INFO"
76
77 /* DeviceInfo-common */
78 #define CODE "CODE"
79 #define DEVICE_NAME "DEVICE_NAME"
80 #define DEVICE_TYPE "DEVICE_TYPE"
81 #define DEVICE_UDID "DEVICE_UDID"
82 #define DEVICE_UUID "DEVICE_UUID"
83 #define NETWORK_ID "NETWORK_ID"
84 #define NODE_ADDR "NODE_ADDR"
85 #define VERSION_TYPE "VERSION_TYPE"
86 #define BT_MAC "BT_MAC"
87 #define CONN_CAP "CONN_CAP"
88 #define SW_VERSION "SW_VERSION"
89 #define MASTER_UDID "MASTER_UDID"
90 #define MASTER_WEIGHT "MASTER_WEIGHT"
91 #define BLE_P2P "BLE_P2P"
92 #define STA_FREQUENCY "STA_FREQUENCY"
93 #define P2P_MAC_ADDR "P2P_MAC_ADDR"
94 #define P2P_ROLE "P2P_ROLE"
95 #define TRANSPORT_PROTOCOL "TRANSPORT_PROTOCOL"
96 #define DATA_CHANGE_FLAG "NODE_DATA_CHANGE_FLAG"
97 #define IS_CHARGING "IS_CHARGING"
98 #define BATTERY_LEAVEL "BATTERY_LEAVEL"
99 #define PKG_VERSION "PKG_VERSION"
100 #define WIFI_VERSION "WIFI_VERSION"
101 #define BLE_VERSION "BLE_VERSION"
102 #define HML_MAC "HML_MAC"
103 #define WIFI_CFG "WIFI_CFG"
104 #define CHAN_LIST_5G "CHAN_LIST_5G"
105 #define REMAIN_POWER "REMAIN_POWER"
106 #define IS_CHARGING "IS_CHARGING"
107 #define IS_SCREENON "IS_SCREENON"
108 #define IP_MAC "IP_MAC"
109 #define NODE_WEIGHT "NODE_WEIGHT"
110 #define ACCOUNT_ID "ACCOUNT_ID"
111 #define DISTRIBUTED_SWITCH "DISTRIBUTED_SWITCH"
112 #define TRANS_FLAGS "TRANS_FLAGS"
113 #define BLE_TIMESTAMP "BLE_TIMESTAMP"
114 #define WIFI_BUFF_SIZE "WIFI_BUFF_SIZE"
115 #define BR_BUFF_SIZE "BR_BUFF_SIZE"
116 #define FEATURE "FEATURE"
117 #define META_NODE_INFO_OF_EAR "MetaNodeInfoOfEar"
118 #define NEW_CONN_CAP "NEW_CONN_CAP"
119 #define EXTDATA "EXTDATA"
120 #define STATE_VERSION "STATE_VERSION"
121 #define BD_KEY "BD_KEY"
122 #define IV "IV"
123 #define UNIFIED_DISPLAY_DEVICE_NAME "UNIFIED_DISPLAY_DEVICE_NAME"
124 #define UNIFIED_DEFAULT_DEVICE_NAME "UNIFIED_DEFAULT_DEVICE_NAME"
125 #define UNIFIED_DEVICE_NAME "UNIFIED_DEVICE_NAME"
126
127 #define FLAG_COMPRESS_DEVICE_INFO 1
128 #define FLAG_UNCOMPRESS_DEVICE_INFO 0
129 #define FLAG_RELAY_DEVICE_INFO 1
130
131 #define HAS_CTRL_CHANNEL (0x1L)
132 #define HAS_CHANNEL_AUTH (0x2L)
133 #define HAS_P2P_AUTH_V2 (0x04L)
134 #define HAS_SUPPRESS_STRATEGY (0x08L)
135 #define HAS_WAIT_TCP_TX_DONE (0x10L)
136 #define LOCAL_FLAGS (HAS_CTRL_CHANNEL | HAS_P2P_AUTH_V2 | HAS_SUPPRESS_STRATEGY | HAS_WAIT_TCP_TX_DONE)
137 #define DEVICE_ID_STR_LEN 64 // for bt v1
138 #define DEFAULT_BATTERY_LEVEL 100
139 #define DEFAULT_NODE_WEIGHT 100
140 #define BASE64_OFFLINE_CODE_LEN ((OFFLINE_CODE_BYTE_SIZE / 3 + 1) * 4 + 1)
141 #define DEFAULT_WIFI_BUFF_SIZE 32768 // 32k
142 #define DEFAULT_BR_BUFF_SIZE 4096 // 4k
143 #define DEFAULT_BLE_TIMESTAMP (roundl(pow(2, 63)) - 1)
144 #define BT_DISC_TYPE_MAX_LEN 7 // br, ble,...
145 #define BT_MAC_LEN 18
146 #define DEFAULT_BT_DISC_TYPE_STR "NO"
147 #define PARSE_UNCOMPRESS_STRING_BUFF_LEN 6 // "true" or "false"
148 #define TRUE_STRING_TAG "true"
149 #define FALSE_STRING_TAG "false"
150
151 /* fast_auth */
152 #define ACCOUNT_HASH "accountHash"
153 #define COMMON_KEY_HASH "keyHash"
154 #define FAST_AUTH "fastauth"
155 #define SOFTBUS_FAST_AUTH "support_fast_auth"
156
157 /* VerifyDevice */
158 #define CODE_VERIFY_DEVICE 2
159 #define DEVICE_ID "DEVICE_ID"
160 #define ENCRYPTED_FAST_AUTH_MAX_LEN 512
161 #define UDID_SHORT_HASH_HEX_STR 16
162 #define UDID_SHORT_HASH_LEN_TEMP 8
163
OptString(const JsonObj * json,const char * const key,char * target,uint32_t targetLen,const char * defaultValue)164 static void OptString(const JsonObj *json, const char * const key,
165 char *target, uint32_t targetLen, const char *defaultValue)
166 {
167 if (JSON_GetStringFromOject(json, key, target, targetLen)) {
168 return;
169 }
170 if (strcpy_s(target, targetLen, defaultValue) != EOK) {
171 ALOGI("set default fail");
172 return;
173 }
174 ALOGI("(%s) prase fail, use default", key);
175 }
176
OptInt(const JsonObj * json,const char * const key,int * target,int defaultValue)177 static void OptInt(const JsonObj *json, const char * const key, int *target, int defaultValue)
178 {
179 if (JSON_GetInt32FromOject(json, key, target)) {
180 return;
181 }
182 ALOGI("(%s) prase fail, use default", key);
183 *target = defaultValue;
184 }
185
OptInt64(const JsonObj * json,const char * const key,int64_t * target,int64_t defaultValue)186 static void OptInt64(const JsonObj *json, const char * const key,
187 int64_t *target, int64_t defaultValue)
188 {
189 if (JSON_GetInt64FromOject(json, key, target)) {
190 return;
191 }
192 ALOGI("(%s) prase fail, use default", key);
193 *target = defaultValue;
194 }
195
OptBool(const JsonObj * json,const char * const key,bool * target,bool defaultValue)196 static void OptBool(const JsonObj *json, const char * const key, bool *target, bool defaultValue)
197 {
198 if (JSON_GetBoolFromOject(json, key, target)) {
199 return;
200 }
201 ALOGI("(%s) prase fail, use default", key);
202 *target = defaultValue;
203 }
204
PackFastAuthValue(JsonObj * obj,AuthDeviceKeyInfo * deviceCommKey)205 static int32_t PackFastAuthValue(JsonObj *obj, AuthDeviceKeyInfo *deviceCommKey)
206 {
207 uint32_t dataLen = 0;
208 uint8_t *data = NULL;
209 AesGcmInputParam aesParam = {0};
210 aesParam.data = (uint8_t *)SOFTBUS_FAST_AUTH;
211 aesParam.dataLen = strlen(SOFTBUS_FAST_AUTH);
212 aesParam.key = deviceCommKey->deviceKey;
213 aesParam.keyLen = deviceCommKey->keyLen;
214 int32_t ret = LnnEncryptAesGcm(&aesParam, (int32_t)deviceCommKey->keyIndex, &data, &dataLen);
215 if (ret != SOFTBUS_OK) {
216 ALOGE("SoftBusEncryptDataWithSeq fail(=%d).", ret);
217 return SOFTBUS_ERR;
218 }
219 if (data == NULL || dataLen == 0) {
220 ALOGE("encrypt data invalid");
221 return SOFTBUS_INVALID_PARAM;
222 }
223 char encryptFastAuth[ENCRYPTED_FAST_AUTH_MAX_LEN] = {0};
224 if (ConvertBytesToUpperCaseHexString(encryptFastAuth, ENCRYPTED_FAST_AUTH_MAX_LEN - 1,
225 data, dataLen) != SOFTBUS_OK) {
226 SoftBusFree(data);
227 return SOFTBUS_ERR;
228 }
229 ALOGD("pack fastAuthTag:%s", encryptFastAuth);
230 JSON_AddStringToObject(obj, FAST_AUTH, encryptFastAuth);
231 SoftBusFree(data);
232 return SOFTBUS_OK;
233 }
234
GetUdidOrShortHash(const AuthSessionInfo * info,char * udidBuf,uint32_t bufLen)235 static bool GetUdidOrShortHash(const AuthSessionInfo *info, char *udidBuf, uint32_t bufLen)
236 {
237 if (strlen(info->udid) != 0) {
238 ALOGI("use info->udid build fastAuthInfo");
239 uint8_t hash[SHA_256_HASH_LEN] = {0};
240 int ret = SoftBusGenerateStrHash((uint8_t *)info->udid, strlen(info->udid), hash);
241 if (ret != SOFTBUS_OK) {
242 ALOGE("generate udidHash fail");
243 return false;
244 }
245 if (ConvertBytesToUpperCaseHexString(udidBuf, bufLen, hash, UDID_SHORT_HASH_LEN_TEMP) != SOFTBUS_OK) {
246 ALOGE("convert bytes to string fail");
247 return false;
248 }
249 return true;
250 }
251 if (info->connInfo.type == AUTH_LINK_TYPE_BLE) {
252 ALOGI("use bleInfo deviceIdHash build fastAuthInfo");
253 AuthRequest request = {0};
254 if (GetAuthRequestNoLock(info->requestId, &request) != SOFTBUS_OK) {
255 LLOGE("GetAuthRequest fail");
256 return false;
257 }
258 return (memcpy_s(udidBuf, bufLen, request.connInfo.info.bleInfo.deviceIdHash,
259 UDID_SHORT_HASH_HEX_STR) == EOK);
260 }
261 return false;
262 }
263
PackFastAuth(JsonObj * obj,AuthSessionInfo * info,const NodeInfo * localNodeInfo)264 static void PackFastAuth(JsonObj *obj, AuthSessionInfo *info, const NodeInfo *localNodeInfo)
265 {
266 ALOGD("pack fastAuth, isServer:%d", info->isServer);
267 bool isNeedPack;
268 if (!info->isServer) {
269 isNeedPack = true;
270 } else if (info->isSupportFastAuth) {
271 isNeedPack = true;
272 } else {
273 ALOGI("unsupport fastAuth");
274 isNeedPack = false;
275 }
276 if (!isNeedPack) {
277 return;
278 }
279 char udidHashHexStr[SHA_256_HEX_HASH_LEN] = {0};
280 if (!GetUdidOrShortHash(info, udidHashHexStr, SHA_256_HEX_HASH_LEN)) {
281 ALOGE("get udid fail, bypass fastAuth");
282 info->isSupportFastAuth = false;
283 return;
284 }
285 ALOGD("udidHashHexStr:%s", udidHashHexStr);
286 if (!IsPotentialTrustedDevice(ID_TYPE_DEVID, (const char *)udidHashHexStr, false)) {
287 ALOGI("not potential trusted realtion, bypass fastAuthProc");
288 info->isSupportFastAuth = false;
289 return;
290 }
291 AuthDeviceKeyInfo deviceCommKey = {0};
292 if (AuthFindDeviceKey(udidHashHexStr, info->connInfo.type, &deviceCommKey) != SOFTBUS_OK) {
293 ALOGW("can't find common key, unsupport fastAuth");
294 info->isSupportFastAuth = false;
295 return;
296 }
297 if (PackFastAuthValue(obj, &deviceCommKey) != SOFTBUS_OK) {
298 info->isSupportFastAuth = false;
299 return;
300 }
301 (void)memset_s(&deviceCommKey, sizeof(AuthDeviceKeyInfo), 0, sizeof(AuthDeviceKeyInfo));
302 }
303
ParseFastAuthValue(AuthSessionInfo * info,const char * encryptedFastAuth,AuthDeviceKeyInfo * deviceKey)304 static void ParseFastAuthValue(AuthSessionInfo *info, const char *encryptedFastAuth,
305 AuthDeviceKeyInfo *deviceKey)
306 {
307 uint8_t fastAuthBytes[ENCRYPTED_FAST_AUTH_MAX_LEN] = {0};
308 if (ConvertHexStringToBytes(fastAuthBytes, ENCRYPTED_FAST_AUTH_MAX_LEN,
309 encryptedFastAuth, strlen(encryptedFastAuth)) != SOFTBUS_OK) {
310 ALOGE("fastAuth data String to bytes fail");
311 return;
312 }
313 uint32_t bytesLen = strlen(encryptedFastAuth) >> 1;
314 uint32_t dataLen = 0;
315 uint8_t *data = NULL;
316 AesGcmInputParam aesParam = {0};
317 aesParam.data = fastAuthBytes;
318 aesParam.dataLen = bytesLen;
319 aesParam.key = deviceKey->deviceKey;
320 aesParam.keyLen = deviceKey->keyLen;
321 int32_t ret = LnnDecryptAesGcm(&aesParam, &data, &dataLen);
322 if (ret != SOFTBUS_OK) {
323 ALOGE("LnnDecryptAesGcm fail(=%d), fastAuth not support", ret);
324 return;
325 }
326 if (data == NULL || dataLen == 0) {
327 ALOGE("decrypt data invalid, fastAuth not support");
328 return;
329 }
330 if (strncmp((char *)data, SOFTBUS_FAST_AUTH, strlen(SOFTBUS_FAST_AUTH)) != 0) {
331 ALOGE("fast auth info error");
332 SoftBusFree(data);
333 return;
334 }
335 ALOGD("parse fastAuth succ");
336 SoftBusFree(data);
337 info->isSupportFastAuth = true;
338 }
339
UnpackFastAuth(JsonObj * obj,AuthSessionInfo * info)340 static void UnpackFastAuth(JsonObj *obj, AuthSessionInfo *info)
341 {
342 info->isSupportFastAuth = false;
343 char encryptedFastAuth[ENCRYPTED_FAST_AUTH_MAX_LEN] = {0};
344 if (!JSON_GetStringFromOject(obj, FAST_AUTH, encryptedFastAuth, ENCRYPTED_FAST_AUTH_MAX_LEN)) {
345 ALOGI("old version or not support fastAuth");
346 return;
347 }
348 ALOGE("unpack fastAuthTag:%s", encryptedFastAuth);
349 uint8_t udidHash[SHA_256_HASH_LEN] = {0};
350 int ret = SoftBusGenerateStrHash((uint8_t *)info->udid, strlen(info->udid), udidHash);
351 if (ret != SOFTBUS_OK) {
352 ALOGE("generate udidHash fail");
353 return;
354 }
355 char udidShortHash[UDID_SHORT_HASH_HEX_STR + 1] = {0};
356 if (ConvertBytesToUpperCaseHexString(udidShortHash, UDID_SHORT_HASH_HEX_STR + 1,
357 udidHash, UDID_SHORT_HASH_LEN_TEMP) != SOFTBUS_OK) {
358 ALOGE("udid hash bytes to hexString fail");
359 return;
360 }
361 if (!IsPotentialTrustedDevice(ID_TYPE_DEVID, (const char *)udidShortHash, false)) {
362 ALOGI("not potential trusted realtion, fastAuth not support");
363 return;
364 }
365 AuthDeviceKeyInfo deviceKey = {0};
366 /* find comm key use udid or udidShortHash */
367 if (AuthFindDeviceKey(udidShortHash, info->connInfo.type, &deviceKey) != SOFTBUS_OK) {
368 ALOGW("can't find common key, fastAuth not support");
369 return;
370 }
371 ParseFastAuthValue(info, encryptedFastAuth, &deviceKey);
372 (void)memset_s(&deviceKey, sizeof(deviceKey), 0, sizeof(deviceKey));
373 }
374
PackCompressInfo(JsonObj * obj,const NodeInfo * info)375 static void PackCompressInfo(JsonObj *obj, const NodeInfo *info)
376 {
377 if (info != NULL) {
378 if (IsFeatureSupport(info->feature, BIT_INFO_COMPRESS)) {
379 JSON_AddStringToObject(obj, SUPPORT_INFO_COMPRESS, TRUE_STRING_TAG);
380 } else {
381 JSON_AddStringToObject(obj, SUPPORT_INFO_COMPRESS, FALSE_STRING_TAG);
382 }
383 }
384 }
385
PackDeviceIdJson(const AuthSessionInfo * info)386 static char *PackDeviceIdJson(const AuthSessionInfo *info)
387 {
388 ALOGI("PackDeviceId: connType = %d.", info->connInfo.type);
389 JsonObj *obj = JSON_CreateObject();
390 if (obj == NULL) {
391 return NULL;
392 }
393 char uuid[UUID_BUF_LEN] = {0};
394 char udid[UDID_BUF_LEN] = {0};
395 char networkId[NETWORK_ID_BUF_LEN] = {0};
396 if (LnnGetLocalStrInfo(STRING_KEY_UUID, uuid, UUID_BUF_LEN) != SOFTBUS_OK ||
397 LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, udid, UDID_BUF_LEN) != SOFTBUS_OK ||
398 LnnGetLocalStrInfo(STRING_KEY_NETWORKID, networkId, sizeof(networkId)) != SOFTBUS_OK) {
399 ALOGE("get uuid/udid/networkId fail.");
400 JSON_Delete(obj);
401 return NULL;
402 }
403 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI && !info->isServer) {
404 if (!JSON_AddStringToObject(obj, CMD_TAG, CMD_GET_AUTH_INFO)) {
405 ALOGE("add CMD_GET fail.");
406 JSON_Delete(obj);
407 return NULL;
408 }
409 } else {
410 if (!JSON_AddStringToObject(obj, CMD_TAG, CMD_RET_AUTH_INFO)) {
411 ALOGE("add CMD_RET fail.");
412 JSON_Delete(obj);
413 return NULL;
414 }
415 }
416 if (info->idType == EXCHANGE_NETWORKID) {
417 if (!JSON_AddStringToObject(obj, DEVICE_ID_TAG, networkId)) {
418 ALOGE("add msg body fail.");
419 JSON_Delete(obj);
420 return NULL;
421 }
422 ALOGI("exchangeIdType=[%d], networkid=[%s]", info->idType, AnonymizesNetworkID(networkId));
423 } else {
424 if (!JSON_AddStringToObject(obj, DEVICE_ID_TAG, udid)) {
425 ALOGE("add msg body fail.");
426 JSON_Delete(obj);
427 return NULL;
428 }
429 ALOGI("exchangeIdType=[%d], udid=[%s]", info->idType, AnonymizesUDID(udid));
430 }
431 if (!JSON_AddStringToObject(obj, DATA_TAG, uuid) ||
432 !JSON_AddInt32ToObject(obj, DATA_BUF_SIZE_TAG, PACKET_SIZE) ||
433 !JSON_AddInt32ToObject(obj, SOFTBUS_VERSION_TAG, info->version) ||
434 !JSON_AddInt32ToObject(obj, EXCHANGE_ID_TYPE, info->idType)) {
435 ALOGE("add msg body fail.");
436 JSON_Delete(obj);
437 return NULL;
438 }
439 const NodeInfo *nodeInfo = LnnGetLocalNodeInfo();
440 PackCompressInfo(obj, nodeInfo);
441 PackFastAuth(obj, (AuthSessionInfo *)info, nodeInfo);
442 char *msg = JSON_PrintUnformatted(obj);
443 JSON_Delete(obj);
444 return msg;
445 }
446
UnPackBtDeviceIdV1(AuthSessionInfo * info,const uint8_t * data,uint32_t len)447 static int32_t UnPackBtDeviceIdV1(AuthSessionInfo *info, const uint8_t *data, uint32_t len)
448 {
449 if (!info->isServer) {
450 ALOGE("invalid bt deviceId msg");
451 return SOFTBUS_ERR;
452 }
453 if (memcpy_s(info->udid, UDID_BUF_LEN, data, len) != EOK) { // data:StandardCharsets.UTF_8
454 ALOGE("memcpy fail");
455 return SOFTBUS_ERR;
456 }
457 return SOFTBUS_OK;
458 }
459
SetCompressFlag(const char * compressCapa,bool * sessionSupportFlag)460 static void SetCompressFlag(const char *compressCapa, bool *sessionSupportFlag)
461 {
462 const NodeInfo *node = LnnGetLocalNodeInfo();
463 if (node == NULL) {
464 return;
465 }
466 bool isRemoteSupportCompress = false;
467 if (strncmp(compressCapa, TRUE_STRING_TAG, strlen(compressCapa)) == 0) {
468 isRemoteSupportCompress = true;
469 } else {
470 isRemoteSupportCompress = false;
471 }
472 if (IsFeatureSupport(node->feature, BIT_INFO_COMPRESS) && isRemoteSupportCompress) {
473 *sessionSupportFlag = true;
474 ALOGI("local-remote all support deviceinfo compress");
475 } else {
476 *sessionSupportFlag = false;
477 }
478 }
479
SetExchangeIdTypeAndValve(JsonObj * obj,AuthSessionInfo * info)480 static int32_t SetExchangeIdTypeAndValve(JsonObj *obj, AuthSessionInfo *info)
481 {
482 int32_t idType = -1;
483 char peerUdid[UDID_BUF_LEN] = {0};
484 if (obj == NULL || info == NULL) {
485 ALOGE("param invalid");
486 return SOFTBUS_ERR;
487 }
488 if (!JSON_GetInt32FromOject(obj, EXCHANGE_ID_TYPE, &idType)) {
489 ALOGI("parse idType failed, ignore");
490 info->idType = EXCHANHE_UDID;
491 return SOFTBUS_OK;
492 }
493 ALOGI("old idType=[%d] exchangeIdType=[%d] deviceId=[%s]", info->idType, idType, AnonymizesUDID(info->udid));
494 if (idType == EXCHANHE_UDID) {
495 info->idType = EXCHANHE_UDID;
496 return SOFTBUS_OK;
497 }
498 if (info->isServer) {
499 if (idType == EXCHANGE_NETWORKID) {
500 if (GetPeerUdidByNetworkId(info->udid, peerUdid) != SOFTBUS_OK) {
501 info->idType = EXCHANGE_FAIL;
502 } else {
503 if (memcpy_s(info->udid, UDID_BUF_LEN, peerUdid, UDID_BUF_LEN) != EOK) {
504 ALOGE("copy peer udid fail");
505 info->idType = EXCHANGE_FAIL;
506 return SOFTBUS_ERR;
507 }
508 info->idType = EXCHANGE_NETWORKID;
509 }
510 }
511 return SOFTBUS_OK;
512 }
513 if (info->idType == EXCHANGE_NETWORKID) {
514 if (idType == EXCHANGE_FAIL) {
515 info->idType = EXCHANGE_FAIL;
516 }
517 if (idType == EXCHANGE_NETWORKID) {
518 if (GetPeerUdidByNetworkId(info->udid, peerUdid) != SOFTBUS_OK) {
519 ALOGE("get peer udid fail, peer networkId=[%s]", AnonymizesUDID(info->udid));
520 info->idType = EXCHANGE_FAIL;
521 } else {
522 if (memcpy_s(info->udid, UDID_BUF_LEN, peerUdid, UDID_BUF_LEN) != EOK) {
523 ALOGE("copy peer udid fail");
524 info->idType = EXCHANGE_FAIL;
525 return SOFTBUS_ERR;
526 }
527 ALOGE("get peer udid success, peer udid=[%s]", AnonymizesUDID(info->udid));
528 info->idType = EXCHANGE_NETWORKID;
529 }
530 }
531 }
532 return SOFTBUS_OK;
533 }
534
UnpackDeviceIdJson(const char * msg,uint32_t len,AuthSessionInfo * info)535 static int32_t UnpackDeviceIdJson(const char *msg, uint32_t len, AuthSessionInfo *info)
536 {
537 JsonObj *obj = JSON_Parse(msg, len);
538 if (obj == NULL) {
539 ALOGE("json parse fail.");
540 return SOFTBUS_ERR;
541 }
542 char cmd[CMD_TAG_LEN] = {0};
543 if (!JSON_GetStringFromOject(obj, CMD_TAG, cmd, CMD_TAG_LEN)) {
544 ALOGE("CMD_TAG not found");
545 JSON_Delete(obj);
546 return SOFTBUS_ERR;
547 }
548 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI && info->isServer) {
549 if (strncmp(cmd, CMD_GET_AUTH_INFO, strlen(CMD_GET_AUTH_INFO)) != 0) {
550 ALOGE("CMD_GET not match.");
551 JSON_Delete(obj);
552 return SOFTBUS_ERR;
553 }
554 } else {
555 if (strncmp(cmd, CMD_RET_AUTH_INFO, strlen(CMD_RET_AUTH_INFO)) != 0) {
556 ALOGE("CMD_RET not match.");
557 JSON_Delete(obj);
558 return SOFTBUS_ERR;
559 }
560 }
561 if (!JSON_GetStringFromOject(obj, DATA_TAG, info->uuid, UUID_BUF_LEN)) {
562 ALOGE("uuid not found");
563 JSON_Delete(obj);
564 return SOFTBUS_ERR;
565 }
566 int32_t maxBuffSize;
567 OptString(obj, DEVICE_ID_TAG, info->udid, UDID_BUF_LEN, "");
568 OptInt(obj, DATA_BUF_SIZE_TAG, &maxBuffSize, PACKET_SIZE);
569 if (strlen(info->udid) != 0) {
570 info->version = SOFTBUS_OLD_V2;
571 } else {
572 info->version = SOFTBUS_OLD_V1;
573 if (strcpy_s(info->udid, UDID_BUF_LEN, info->uuid) != EOK) {
574 ALOGE("strcpy udid fail, ignore");
575 }
576 }
577 if (!JSON_GetInt32FromOject(obj, SOFTBUS_VERSION_TAG, (int32_t *)&info->version)) {
578 }
579 if (SetExchangeIdTypeAndValve(obj, info) != SOFTBUS_OK) {
580 ALOGE("set exchange id type or valve fail.");
581 JSON_Delete(obj);
582 return SOFTBUS_ERR;
583 }
584 if (info->connInfo.type != AUTH_LINK_TYPE_WIFI) {
585 char compressParse[PARSE_UNCOMPRESS_STRING_BUFF_LEN] = {0};
586 OptString(obj, SUPPORT_INFO_COMPRESS, compressParse,
587 PARSE_UNCOMPRESS_STRING_BUFF_LEN, FALSE_STRING_TAG);
588 SetCompressFlag(compressParse, &info->isSupportCompress);
589 }
590 UnpackFastAuth(obj, info);
591 JSON_Delete(obj);
592 return SOFTBUS_OK;
593 }
594
PackCommonDevInfo(JsonObj * json,const NodeInfo * info,bool isMetaAuth)595 static int32_t PackCommonDevInfo(JsonObj *json, const NodeInfo *info, bool isMetaAuth)
596 {
597 (void)JSON_AddStringToObject(json, UNIFIED_DEVICE_NAME, info->deviceInfo.unifiedName);
598 (void)JSON_AddStringToObject(json, UNIFIED_DISPLAY_DEVICE_NAME, info->deviceInfo.displayName);
599 if (!JSON_AddStringToObject(json, NETWORK_ID, info->networkId) ||
600 !JSON_AddStringToObject(json, DEVICE_NAME, LnnGetDeviceName(&info->deviceInfo)) ||
601 !JSON_AddStringToObject(json, DEVICE_TYPE, LnnConvertIdToDeviceType(info->deviceInfo.deviceTypeId)) ||
602 !JSON_AddStringToObject(json, DEVICE_UDID, LnnGetDeviceUdid(info))) {
603 ALOGE("JSON_AddStringToObject fail.");
604 return SOFTBUS_ERR;
605 }
606 if (isMetaAuth && !JSON_AddStringToObject(json, DEVICE_UUID, info->uuid)) {
607 ALOGE("JSON_AddStringToObject fail.");
608 return SOFTBUS_ERR;
609 }
610 return SOFTBUS_OK;
611 }
612
PackCommonFastAuth(JsonObj * json,const NodeInfo * info)613 static void PackCommonFastAuth(JsonObj *json, const NodeInfo *info)
614 {
615 (void)JSON_AddInt32ToObject(json, STATE_VERSION, info->stateVersion);
616 char extData[EXTDATA_LEN] = {0};
617 int32_t ret = GetExtData(extData, EXTDATA_LEN);
618 if (ret != SOFTBUS_OK) {
619 ALOGE("GetExtData fail.");
620 } else {
621 ALOGI("GetExtData : %s", extData);
622 (void)JSON_AddStringToObject(json, EXTDATA, extData);
623 }
624 }
625
PackCommP2pInfo(JsonObj * json,const NodeInfo * info)626 static void PackCommP2pInfo(JsonObj *json, const NodeInfo *info)
627 {
628 (void)JSON_AddInt32ToObject(json, P2P_ROLE, LnnGetP2pRole(info));
629 (void)JSON_AddStringToObject(json, P2P_MAC_ADDR, LnnGetP2pMac(info));
630 (void)JSON_AddStringToObject(json, HML_MAC, info->wifiDirectAddr);
631
632 (void)JSON_AddStringToObject(json, WIFI_CFG, info->p2pInfo.wifiCfg);
633 (void)JSON_AddStringToObject(json, CHAN_LIST_5G, info->p2pInfo.chanList5g);
634 (void)JSON_AddInt32ToObject(json, STA_FREQUENCY, LnnGetStaFrequency(info));
635 }
636
PackCommon(JsonObj * json,const NodeInfo * info,SoftBusVersion version,bool isMetaAuth)637 static int32_t PackCommon(JsonObj *json, const NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
638 {
639 if (version >= SOFTBUS_NEW_V1) {
640 if (!JSON_AddStringToObject(json, MASTER_UDID, info->masterUdid) ||
641 !JSON_AddInt32ToObject(json, MASTER_WEIGHT, info->masterWeight)) {
642 ALOGE("add master node info fail.");
643 return SOFTBUS_ERR;
644 }
645 if (!JSON_AddStringToObject(json, NODE_ADDR, info->nodeAddress)) {
646 ALOGE("pack node address Fail.");
647 return SOFTBUS_ERR;
648 }
649 }
650 if (!JSON_AddStringToObject(json, SW_VERSION, info->softBusVersion)) {
651 ALOGE("add version info fail.");
652 return SOFTBUS_ERR;
653 }
654 if (PackCommonDevInfo(json, info, isMetaAuth) != SOFTBUS_OK) {
655 return SOFTBUS_ERR;
656 }
657 if (!JSON_AddStringToObject(json, VERSION_TYPE, info->versionType) ||
658 !JSON_AddInt32ToObject(json, CONN_CAP, info->netCapacity) ||
659 !JSON_AddInt16ToObject(json, DATA_CHANGE_FLAG, info->dataChangeFlag) ||
660 !JSON_AddBoolToObject(json, IS_CHARGING, info->batteryInfo.isCharging) ||
661 !JSON_AddBoolToObject(json, BLE_P2P, info->isBleP2p) ||
662 !JSON_AddInt64ToObject(json, TRANSPORT_PROTOCOL, (int64_t)LnnGetSupportedProtocols(info))) {
663 ALOGE("JSON_AddStringToObject fail.");
664 return SOFTBUS_ERR;
665 }
666 char btMacUpper[BT_MAC_LEN] = {0};
667 if (StringToUpperCase(LnnGetBtMac(info), btMacUpper, BT_MAC_LEN) != SOFTBUS_OK) {
668 LLOGE("btMac to upperCase fail.");
669 if (memcpy_s(btMacUpper, BT_MAC_LEN, LnnGetBtMac(info), BT_MAC_LEN) != EOK) {
670 LLOGE("btMac cpy fail.");
671 return SOFTBUS_ERR;
672 }
673 }
674 if (!JSON_AddStringToObject(json, PKG_VERSION, info->pkgVersion) ||
675 !JSON_AddInt64ToObject(json, WIFI_VERSION, info->wifiVersion) ||
676 !JSON_AddInt64ToObject(json, BLE_VERSION, info->bleVersion) ||
677 !JSON_AddStringToObject(json, BT_MAC, btMacUpper) ||
678 !JSON_AddInt32ToObject(json, REMAIN_POWER, info->batteryInfo.batteryLevel) ||
679 !JSON_AddBoolToObject(json, IS_CHARGING, info->batteryInfo.isCharging) ||
680 !JSON_AddBoolToObject(json, IS_SCREENON, info->isScreenOn) ||
681 !JSON_AddInt32ToObject(json, NODE_WEIGHT, info->masterWeight) ||
682 !JSON_AddInt64ToObject(json, ACCOUNT_ID, info->accountId) ||
683 !JSON_AddBoolToObject(json, DISTRIBUTED_SWITCH, true) ||
684 !JSON_AddInt64ToObject(json, BLE_TIMESTAMP, info->bleStartTimestamp) ||
685 !JSON_AddInt32ToObject(json, WIFI_BUFF_SIZE, info->wifiBuffSize) ||
686 !JSON_AddInt32ToObject(json, BR_BUFF_SIZE, info->brBuffSize) ||
687 !JSON_AddInt64ToObject(json, FEATURE, info->feature) ||
688 !JSON_AddInt64ToObject(json, NEW_CONN_CAP, info->netCapacity)) {
689 ALOGE("JSON_AddStringToObject fail.");
690 return SOFTBUS_ERR;
691 }
692 PackCommonFastAuth(json, info);
693 if (!PackCipherKeySyncMsg(json)) {
694 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "PackCipherKeySyncMsg fail.");
695 }
696 PackCommP2pInfo(json, info);
697 return SOFTBUS_OK;
698 }
699
UnpackCommon(const JsonObj * json,NodeInfo * info,SoftBusVersion version,bool isMetaAuth)700 static void UnpackCommon(const JsonObj *json, NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
701 {
702 if (version >= SOFTBUS_NEW_V1) {
703 if (!JSON_GetStringFromOject(json, MASTER_UDID, info->masterUdid, UDID_BUF_LEN) ||
704 !JSON_GetInt32FromOject(json, MASTER_WEIGHT, &info->masterWeight)) {
705 ALOGE("get master node info fail");
706 }
707 ALOGE("get master weight: %d", info->masterWeight);
708 if (!JSON_GetStringFromOject(json, NODE_ADDR, info->nodeAddress, sizeof(info->nodeAddress))) {
709 ALOGW("no node address packed. set to address %s", NODE_ADDR_LOOPBACK);
710 (void)strcpy_s(info->nodeAddress, sizeof(info->nodeAddress), NODE_ADDR_LOOPBACK);
711 }
712 }
713 (void)JSON_GetStringFromOject(json, SW_VERSION, info->softBusVersion, VERSION_MAX_LEN);
714 OptString(json, PKG_VERSION, info->pkgVersion, VERSION_MAX_LEN, "");
715 OptString(json, UNIFIED_DEVICE_NAME, info->deviceInfo.unifiedName, DEVICE_NAME_BUF_LEN, "");
716 OptString(json, UNIFIED_DEFAULT_DEVICE_NAME, info->deviceInfo.unifiedDefaultName, DEVICE_NAME_BUF_LEN, "");
717 OptString(json, UNIFIED_DISPLAY_DEVICE_NAME, info->deviceInfo.displayName, DEVICE_NAME_BUF_LEN, "");
718 OptInt64(json, WIFI_VERSION, &info->wifiVersion, 0);
719 OptInt64(json, BLE_VERSION, &info->bleVersion, 0);
720 OptString(json, BT_MAC, info->connectInfo.macAddr, MAC_LEN, "");
721 char deviceType[DEVICE_TYPE_BUF_LEN] = {0};
722 (void)JSON_GetStringFromOject(json, DEVICE_NAME, info->deviceInfo.deviceName, DEVICE_NAME_BUF_LEN);
723 if (JSON_GetStringFromOject(json, DEVICE_TYPE, deviceType, DEVICE_TYPE_BUF_LEN)) {
724 (void)LnnConvertDeviceTypeToId(deviceType, &(info->deviceInfo.deviceTypeId));
725 }
726 (void)JSON_GetStringFromOject(json, DEVICE_UDID, info->deviceInfo.deviceUdid, UDID_BUF_LEN);
727 if (isMetaAuth) {
728 (void)JSON_GetStringFromOject(json, DEVICE_UUID, info->uuid, UDID_BUF_LEN);
729 }
730 (void)JSON_GetStringFromOject(json, NETWORK_ID, info->networkId, NETWORK_ID_BUF_LEN);
731 (void)JSON_GetStringFromOject(json, VERSION_TYPE, info->versionType, VERSION_MAX_LEN);
732 (void)JSON_GetInt32FromOject(json, CONN_CAP, (int32_t *)&info->netCapacity);
733
734 info->isBleP2p = false;
735 (void)JSON_GetBoolFromOject(json, BLE_P2P, &info->isBleP2p);
736 (void)JSON_GetInt16FromOject(json, DATA_CHANGE_FLAG, (int16_t *)&info->dataChangeFlag);
737 (void)JSON_GetBoolFromOject(json, IS_CHARGING, &info->batteryInfo.isCharging);
738 (void)JSON_GetInt32FromOject(json, REMAIN_POWER, &info->batteryInfo.batteryLevel);
739 OptBool(json, IS_SCREENON, &info->isScreenOn, false);
740 OptInt64(json, ACCOUNT_ID, &info->accountId, 0);
741 OptInt(json, NODE_WEIGHT, &info->masterWeight, DEFAULT_NODE_WEIGHT);
742
743 //IS_SUPPORT_TCP_HEARTBEAT
744 OptInt64(json, NEW_CONN_CAP, (int64_t *)&info->netCapacity, -1);
745 if (info->netCapacity == (uint32_t)-1) {
746 (void)JSON_GetInt64FromOject(json, CONN_CAP, (int64_t *)&info->netCapacity);
747 }
748 OptInt(json, WIFI_BUFF_SIZE, &info->wifiBuffSize, DEFAULT_WIFI_BUFF_SIZE);
749 OptInt(json, BR_BUFF_SIZE, &info->wifiBuffSize, DEFAULT_BR_BUFF_SIZE);
750 OptInt64(json, FEATURE, (int64_t *)&info->feature, 0);
751 //MetaNodeInfoOfEar
752 OptString(json, EXTDATA, info->extData, EXTDATA_LEN, "");
753 if (version == SOFTBUS_OLD_V1) {
754 if (strcpy_s(info->networkId, NETWORK_ID_BUF_LEN, info->uuid) != EOK) {
755 ALOGE("v1 version strcpy networkid fail");
756 }
757 }
758 ProcessCipherKeySyncInfo(json, info->deviceInfo.deviceUdid);
759
760 // unpack p2p info
761 OptInt(json, P2P_ROLE, &info->p2pInfo.p2pRole, -1);
762 OptString(json, WIFI_CFG, info->p2pInfo.wifiCfg, WIFI_CFG_INFO_MAX_LEN, "");
763 OptString(json, CHAN_LIST_5G, info->p2pInfo.chanList5g, CHANNEL_LIST_STR_LEN, "");
764 OptInt(json, STA_FREQUENCY, &info->p2pInfo.staFrequency, -1);
765 OptString(json, P2P_MAC_ADDR, info->p2pInfo.p2pMac, MAC_LEN, "");
766 OptString(json, HML_MAC, info->wifiDirectAddr, MAC_LEN, "");
767 }
768
GetBtDiscTypeString(const NodeInfo * info,char * buf,uint32_t len)769 static int32_t GetBtDiscTypeString(const NodeInfo *info, char *buf, uint32_t len)
770 {
771 uint32_t i = 0;
772 if (LnnHasDiscoveryType(info, DISCOVERY_TYPE_BLE)) {
773 CHECK_EXPRESSION_RETURN_VALUE((i >= len), SOFTBUS_ERR);
774 buf[i++] = DISCOVERY_TYPE_BLE + '0';
775 }
776 if (LnnHasDiscoveryType(info, DISCOVERY_TYPE_BR)) {
777 if (i != 0) {
778 CHECK_EXPRESSION_RETURN_VALUE((i >= len), SOFTBUS_ERR);
779 buf[i++] = ',';
780 }
781 CHECK_EXPRESSION_RETURN_VALUE((i >= len), SOFTBUS_ERR);
782 buf[i++] = DISCOVERY_TYPE_BR + '0';
783 }
784 return SOFTBUS_OK;
785 }
786
AddDiscoveryType(JsonObj * json,const char * remoteUuid)787 static void AddDiscoveryType(JsonObj *json, const char *remoteUuid)
788 {
789 if (remoteUuid == NULL) {
790 return;
791 }
792 char networkId[NETWORK_ID_BUF_LEN] = {0};
793 if (LnnGetNetworkIdByUuid(remoteUuid, networkId, sizeof(networkId)) != SOFTBUS_OK) {
794 ALOGI("networkId not found by uuid, maybe first online!");
795 return;
796 }
797 uint32_t discoveryType = 0;
798 if (LnnGetRemoteNumInfo(networkId, NUM_KEY_DISCOVERY_TYPE, (int32_t *)&discoveryType) != SOFTBUS_OK) {
799 ALOGE("get discoveryType fail!");
800 return;
801 }
802 NodeInfo nodeInfo; // only for discType calc
803 (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
804 nodeInfo.discoveryType = discoveryType;
805 char discTypeStr[BT_DISC_TYPE_MAX_LEN] = {0};
806 if (GetBtDiscTypeString(&nodeInfo, discTypeStr, BT_DISC_TYPE_MAX_LEN) != SOFTBUS_OK) {
807 ALOGE("disc Type calc fail");
808 return;
809 }
810 ALOGD("pack discType is:%s", discTypeStr);
811 JSON_AddStringToObject(json, DISCOVERY_TYPE, discTypeStr);
812 }
813
PackBt(JsonObj * json,const NodeInfo * info,SoftBusVersion version,bool isMetaAuth,const char * remoteUuid)814 static int32_t PackBt(JsonObj *json, const NodeInfo *info, SoftBusVersion version,
815 bool isMetaAuth, const char *remoteUuid)
816 {
817 if (!JSON_AddInt32ToObject(json, CODE, CODE_VERIFY_BT)) {
818 ALOGE("add bt info fail");
819 return SOFTBUS_ERR;
820 }
821 AddDiscoveryType(json, remoteUuid);
822 if (PackCommon(json, info, version, isMetaAuth) != SOFTBUS_OK) {
823 ALOGE("PackCommon fail");
824 return SOFTBUS_ERR;
825 }
826 return SOFTBUS_OK;
827 }
828
SetDiscType(uint32_t * discType,const char * discStr)829 static int32_t SetDiscType(uint32_t *discType, const char *discStr)
830 {
831 if (strcmp(discStr, DEFAULT_BT_DISC_TYPE_STR) == 0) {
832 ALOGE("disc type can't parse");
833 return SOFTBUS_ERR;
834 }
835 return SOFTBUS_OK;
836 }
837
UnpackBt(const JsonObj * json,NodeInfo * info,SoftBusVersion version,bool isMetaAuth)838 static int32_t UnpackBt(const JsonObj *json, NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
839 {
840 char discTypeStr[BT_DISC_TYPE_MAX_LEN] = {0};
841 if (!JSON_GetInt64FromOject(json, TRANSPORT_PROTOCOL, (int64_t *)&info->supportedProtocols)) {
842 info->supportedProtocols = LNN_PROTOCOL_BR | LNN_PROTOCOL_BLE;
843 }
844 OptString(json, DISCOVERY_TYPE, discTypeStr, BT_DISC_TYPE_MAX_LEN, DEFAULT_BT_DISC_TYPE_STR);
845 (void)SetDiscType(&info->discoveryType, discTypeStr);
846 OptInt64(json, BLE_TIMESTAMP, &info->bleStartTimestamp, DEFAULT_BLE_TIMESTAMP);
847 OptInt(json, STATE_VERSION, &info->stateVersion, 0);
848 UnpackCommon(json, info, version, isMetaAuth);
849 return SOFTBUS_OK;
850 }
851
PackWiFi(JsonObj * json,const NodeInfo * info,SoftBusVersion version,bool isMetaAuth)852 static int32_t PackWiFi(JsonObj *json, const NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
853 {
854 ALOGD("devIp %d", strlen(info->connectInfo.deviceIp));
855 if (!JSON_AddInt32ToObject(json, CODE, CODE_VERIFY_IP) ||
856 !JSON_AddInt32ToObject(json, BUS_MAX_VERSION, BUS_V2) ||
857 !JSON_AddInt32ToObject(json, BUS_MIN_VERSION, BUS_V1) ||
858 !JSON_AddInt32ToObject(json, AUTH_PORT, LnnGetAuthPort(info)) ||
859 !JSON_AddInt32ToObject(json, SESSION_PORT, LnnGetSessionPort(info)) ||
860 !JSON_AddInt32ToObject(json, PROXY_PORT, LnnGetProxyPort(info)) ||
861 !JSON_AddStringToObject(json, DEV_IP, info->connectInfo.deviceIp)) {
862 ALOGE("add wifi info fail.");
863 return SOFTBUS_ERR;
864 }
865 char offlineCode[BASE64_OFFLINE_CODE_LEN] = {0};
866 size_t len = 0;
867 ALOGE("offlineCode %d, %d", strlen(offlineCode), sizeof(info->offlineCode));
868 int32_t ret = SoftBusBase64Encode((unsigned char*)offlineCode, BASE64_OFFLINE_CODE_LEN, &len,
869 (unsigned char*)info->offlineCode, sizeof(info->offlineCode));
870 if (ret != 0) {
871 ALOGE("mbedtls base64 encode failed.");
872 return SOFTBUS_ERR;
873 }
874 (void)JSON_AddStringToObject(json, BLE_OFFLINE_CODE, offlineCode);
875 if (PackCommon(json, info, version, isMetaAuth) != SOFTBUS_OK) {
876 ALOGE("PackCommon fail");
877 return SOFTBUS_ERR;
878 }
879 return SOFTBUS_OK;
880 }
881
CheckBusVersion(const JsonObj * json)882 static int32_t CheckBusVersion(const JsonObj *json)
883 {
884 int32_t maxVersion;
885 int32_t minVersion;
886 OptInt(json, BUS_MAX_VERSION, &maxVersion, -1);
887 OptInt(json, BUS_MIN_VERSION, &minVersion, -1);
888 if (maxVersion > BUS_V2) {
889 maxVersion = BUS_V2;
890 }
891 if (minVersion < BUS_V1) {
892 minVersion = BUS_V1;
893 }
894 if (maxVersion < 0 || maxVersion < minVersion) {
895 ALOGE("no common version");
896 return SOFTBUS_ERR;
897 }
898 return maxVersion;
899 }
900
UnpackWiFi(const JsonObj * json,NodeInfo * info,SoftBusVersion version,bool isMetaAuth)901 static int32_t UnpackWiFi(const JsonObj *json, NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
902 {
903 if (CheckBusVersion(json) < 0) {
904 return SOFTBUS_ERR;
905 }
906 (void)JSON_GetInt32FromOject(json, AUTH_PORT, &info->connectInfo.authPort);
907 (void)JSON_GetInt32FromOject(json, SESSION_PORT, &info->connectInfo.sessionPort);
908 (void)JSON_GetInt32FromOject(json, PROXY_PORT, &info->connectInfo.proxyPort);
909 if (!JSON_GetInt64FromOject(json, TRANSPORT_PROTOCOL, (int64_t *)&info->supportedProtocols)) {
910 info->supportedProtocols = LNN_PROTOCOL_IP;
911 }
912 char offlineCode[BASE64_OFFLINE_CODE_LEN] = {0};
913 OptString(json, DEV_IP, info->connectInfo.deviceIp, MAX_ADDR_LEN, ""); // check ip available
914 OptString(json, BLE_OFFLINE_CODE, offlineCode, BASE64_OFFLINE_CODE_LEN, "");
915 size_t len;
916 if (SoftBusBase64Decode(info->offlineCode, OFFLINE_CODE_BYTE_SIZE,
917 &len, (const unsigned char *)offlineCode, strlen(offlineCode)) != 0) {
918 ALOGE("base64Decode fail");
919 }
920 if (len != OFFLINE_CODE_BYTE_SIZE) {
921 ALOGE("base64Decode data err");
922 }
923 UnpackCommon(json, info, version, isMetaAuth);
924 return SOFTBUS_OK;
925 }
926
PackDeviceInfoBtV1(JsonObj * json,const NodeInfo * info,bool isMetaAuth)927 static int32_t PackDeviceInfoBtV1(JsonObj *json, const NodeInfo *info, bool isMetaAuth)
928 {
929 ALOGI("pack deviceInfo bt-v1");
930 if (!JSON_AddStringToObject(json, DEVICE_NAME, LnnGetDeviceName(&info->deviceInfo)) ||
931 !JSON_AddStringToObject(json, DEVICE_TYPE, LnnConvertIdToDeviceType(info->deviceInfo.deviceTypeId)) ||
932 !JSON_AddStringToObject(json, DEVICE_VERSION_TYPE, info->versionType) ||
933 !JSON_AddStringToObject(json, BR_MAC_ADDR, LnnGetBtMac(info)) ||
934 !JSON_AddStringToObject(json, P2P_MAC_ADDR, LnnGetP2pMac(info)) ||
935 !JSON_AddStringToObject(json, UUID, info->uuid) ||
936 !JSON_AddStringToObject(json, SW_VERSION, info->softBusVersion) ||
937 !JSON_AddStringToObject(json, DEVICE_UDID, info->deviceInfo.deviceUdid) ||
938 !JSON_AddInt64ToObject(json, WIFI_VERSION, info->wifiVersion) ||
939 !JSON_AddInt64ToObject(json, BLE_VERSION, info->bleVersion) ||
940 !JSON_AddInt64ToObject(json, CONN_CAP, info->netCapacity) ||
941 !JSON_AddInt64ToObject(json, NEW_CONN_CAP, info->netCapacity) ||
942 !JSON_AddStringToObject(json, BT_MAC, info->connectInfo.macAddr) ||
943 !JSON_AddStringToObject(json, HML_MAC, info->wifiDirectAddr) ||
944 !JSON_AddInt32ToObject(json, REMAIN_POWER, info->batteryInfo.batteryLevel) ||
945 !JSON_AddBoolToObject(json, IS_CHARGING, info->batteryInfo.isCharging) ||
946 !JSON_AddBoolToObject(json, IS_SCREENON, info->isScreenOn) ||
947 !JSON_AddInt32ToObject(json, P2P_ROLE, info->p2pInfo.p2pRole) ||
948 !JSON_AddInt64ToObject(json, ACCOUNT_ID, info->accountId) ||
949 !JSON_AddInt32ToObject(json, NODE_WEIGHT, info->masterWeight)) {
950 ALOGE("add wifi info fail");
951 return SOFTBUS_ERR;
952 }
953 return SOFTBUS_OK;
954 }
955
UnpackDeviceInfoBtV1(const JsonObj * json,NodeInfo * info)956 static int32_t UnpackDeviceInfoBtV1(const JsonObj *json, NodeInfo *info)
957 {
958 char deviceType[DEVICE_TYPE_BUF_LEN] = {0};
959 if (!JSON_GetStringFromOject(json, DEVICE_NAME, info->deviceInfo.deviceName, DEVICE_NAME_BUF_LEN) ||
960 !JSON_GetStringFromOject(json, DEVICE_TYPE, deviceType, DEVICE_TYPE_BUF_LEN) ||
961 !JSON_GetStringFromOject(json, DEVICE_UDID, info->deviceInfo.deviceUdid, UDID_BUF_LEN) ||
962 !JSON_GetStringFromOject(json, UUID, info->uuid, UUID_BUF_LEN) ||
963 !JSON_GetStringFromOject(json, BR_MAC_ADDR, info->connectInfo.macAddr, MAC_LEN)) {
964 ALOGE("prase devinfo fail, invalid msg");
965 return SOFTBUS_ERR;
966 }
967 (void)LnnConvertDeviceTypeToId(deviceType, &(info->deviceInfo.deviceTypeId));
968 OptString(json, HML_MAC, info->wifiDirectAddr, MAC_LEN, "");
969 OptString(json, P2P_MAC_ADDR, info->p2pInfo.p2pMac, MAC_LEN, "");
970 OptString(json, SW_VERSION, info->softBusVersion, VERSION_MAX_LEN, "");
971 OptString(json, DEVICE_VERSION_TYPE, info->versionType, VERSION_MAX_LEN, "");
972 OptInt64(json, WIFI_VERSION, &info->wifiVersion, 0);
973 OptInt64(json, BLE_VERSION, &info->bleVersion, 0);
974 OptInt(json, REMAIN_POWER, &info->batteryInfo.batteryLevel, DEFAULT_BATTERY_LEVEL);
975 OptBool(json, IS_CHARGING, &info->batteryInfo.isCharging, false);
976 OptBool(json, IS_SCREENON, &info->isScreenOn, false);
977 OptInt(json, P2P_ROLE, &info->p2pInfo.p2pRole, 0);
978 OptInt64(json, ACCOUNT_ID, &info->accountId, 0);
979 OptInt(json, NODE_WEIGHT, &info->masterWeight, DEFAULT_NODE_WEIGHT);
980 OptInt64(json, FEATURE, (int64_t *)&info->feature, 0);
981 OptInt64(json, NEW_CONN_CAP, (int64_t *)&info->netCapacity, -1);
982 if (info->netCapacity == (uint32_t)-1) {
983 OptInt64(json, CONN_CAP, (int64_t *)&info->netCapacity, 0);
984 }
985 if (strcpy_s(info->networkId, NETWORK_ID_BUF_LEN, info->uuid) != EOK) {
986 ALOGE("strcpy networkId fail");
987 }
988 return SOFTBUS_OK;
989 }
990
PackDeviceInfoMessage(int32_t linkType,SoftBusVersion version,bool isMetaAuth,const char * remoteUuid)991 char *PackDeviceInfoMessage(int32_t linkType, SoftBusVersion version, bool isMetaAuth, const char *remoteUuid)
992 {
993 ALOGI("PackDeviceInfo: connType = %d.", linkType);
994 const NodeInfo *info = LnnGetLocalNodeInfo();
995 if (info == NULL) {
996 ALOGE("local info is null.");
997 return NULL;
998 }
999 JsonObj *json = JSON_CreateObject();
1000 if (json == NULL) {
1001 ALOGE("create cjson fail.");
1002 return NULL;
1003 }
1004 int32_t ret;
1005 if (linkType == AUTH_LINK_TYPE_WIFI) {
1006 ret = PackWiFi(json, info, version, isMetaAuth);
1007 } else if (version == SOFTBUS_OLD_V1) {
1008 ret = PackDeviceInfoBtV1(json, info, isMetaAuth);
1009 } else {
1010 ret = PackBt(json, info, version, isMetaAuth, remoteUuid);
1011 }
1012 if (ret != SOFTBUS_OK) {
1013 JSON_Delete(json);
1014 return NULL;
1015 }
1016
1017 char *msg = JSON_PrintUnformatted(json);
1018 if (msg == NULL) {
1019 ALOGE("JSON_PrintUnformatted fail.");
1020 }
1021 JSON_Delete(json);
1022 return msg;
1023 }
1024
UnpackDeviceInfoMessage(const DevInfoData * devInfo,NodeInfo * nodeInfo,bool isMetaAuth)1025 int32_t UnpackDeviceInfoMessage(const DevInfoData *devInfo, NodeInfo *nodeInfo, bool isMetaAuth)
1026 {
1027 CHECK_NULL_PTR_RETURN_VALUE(devInfo, SOFTBUS_INVALID_PARAM);
1028 CHECK_NULL_PTR_RETURN_VALUE(nodeInfo, SOFTBUS_INVALID_PARAM);
1029 ALOGI("UnpackDeviceInfo: connType = %d.", devInfo->linkType);
1030 JsonObj *json = JSON_Parse(devInfo->msg, devInfo->len);
1031 if (json == NULL) {
1032 ALOGE("parse cjson fail.");
1033 return SOFTBUS_ERR;
1034 }
1035 int32_t ret;
1036 if (devInfo->linkType == AUTH_LINK_TYPE_WIFI) {
1037 ret = UnpackWiFi(json, nodeInfo, devInfo->version, isMetaAuth);
1038 } else if (devInfo->version == SOFTBUS_OLD_V1) {
1039 ret = UnpackDeviceInfoBtV1(json, nodeInfo);
1040 } else {
1041 ret = UnpackBt(json, nodeInfo, devInfo->version, isMetaAuth);
1042 }
1043 JSON_Delete(json);
1044 return ret;
1045 }
1046
PostDeviceIdData(int64_t authSeq,const AuthSessionInfo * info,uint8_t * data,uint32_t len)1047 static int32_t PostDeviceIdData(int64_t authSeq, const AuthSessionInfo *info, uint8_t *data, uint32_t len)
1048 {
1049 AuthDataHead head = {
1050 .dataType = DATA_TYPE_DEVICE_ID,
1051 .module = MODULE_TRUST_ENGINE,
1052 .seq = authSeq,
1053 .flag = info->isServer ? SERVER_SIDE_FLAG : CLIENT_SIDE_FLAG,
1054 .len = len,
1055 };
1056 if (PostAuthData(info->connId, !info->isServer, &head, data) != SOFTBUS_OK) {
1057 ALOGE("post device id fail");
1058 return SOFTBUS_ERR;
1059 }
1060 return SOFTBUS_OK;
1061 }
1062
PostBtV1DevId(int64_t authSeq,const AuthSessionInfo * info)1063 static int32_t PostBtV1DevId(int64_t authSeq, const AuthSessionInfo *info)
1064 {
1065 if (!info->isServer) {
1066 ALOGE("client don't send Bt-v1 devId");
1067 return SOFTBUS_ERR;
1068 }
1069 char uuid[UUID_BUF_LEN] = {0};
1070 if (LnnGetLocalStrInfo(STRING_KEY_UUID, uuid, UUID_BUF_LEN) != SOFTBUS_OK) {
1071 ALOGE("get uuid fail");
1072 return SOFTBUS_ERR;
1073 }
1074 return PostDeviceIdData(authSeq, info, (uint8_t *)uuid, strlen(uuid));
1075 }
1076
PostWifiV1DevId(int64_t authSeq,const AuthSessionInfo * info)1077 static int32_t PostWifiV1DevId(int64_t authSeq, const AuthSessionInfo *info)
1078 {
1079 if (!info->isServer) {
1080 ALOGE("client don't send wifi-v1 devId");
1081 return SOFTBUS_ERR;
1082 }
1083 char *msg = PackDeviceIdJson(info);
1084 if (msg == NULL) {
1085 ALOGE("pack devId fail");
1086 return SOFTBUS_ERR;
1087 }
1088 if (PostDeviceIdData(authSeq, info, (uint8_t *)msg, strlen(msg) + 1) != SOFTBUS_OK) {
1089 JSON_Free(msg);
1090 return SOFTBUS_ERR;
1091 }
1092 JSON_Free(msg);
1093 return SOFTBUS_OK;
1094 }
1095
PostDeviceIdV1(int64_t authSeq,const AuthSessionInfo * info)1096 static int32_t PostDeviceIdV1(int64_t authSeq, const AuthSessionInfo *info)
1097 {
1098 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI) {
1099 return PostWifiV1DevId(authSeq, info);
1100 } else {
1101 ALOGI("process v1 bt deviceIdSync");
1102 return PostBtV1DevId(authSeq, info);
1103 }
1104 }
1105
PostDeviceIdNew(int64_t authSeq,const AuthSessionInfo * info)1106 static int32_t PostDeviceIdNew(int64_t authSeq, const AuthSessionInfo *info)
1107 {
1108 char *msg = PackDeviceIdJson(info);
1109 if (msg == NULL) {
1110 ALOGE("pack devId fail");
1111 return SOFTBUS_ERR;
1112 }
1113 if (PostDeviceIdData(authSeq, info, (uint8_t *)msg, strlen(msg) + 1) != SOFTBUS_OK) {
1114 JSON_Free(msg);
1115 return SOFTBUS_ERR;
1116 }
1117 JSON_Free(msg);
1118 return SOFTBUS_OK;
1119 }
1120
PostDeviceIdMessage(int64_t authSeq,const AuthSessionInfo * info)1121 int32_t PostDeviceIdMessage(int64_t authSeq, const AuthSessionInfo *info)
1122 {
1123 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1124 if (info->version == SOFTBUS_OLD_V1) {
1125 return PostDeviceIdV1(authSeq, info);
1126 } else {
1127 return PostDeviceIdNew(authSeq, info);
1128 }
1129 }
1130
ProcessDeviceIdMessage(AuthSessionInfo * info,const uint8_t * data,uint32_t len)1131 int32_t ProcessDeviceIdMessage(AuthSessionInfo *info, const uint8_t *data, uint32_t len)
1132 {
1133 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1134 CHECK_NULL_PTR_RETURN_VALUE(data, SOFTBUS_INVALID_PARAM);
1135 if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) &&
1136 (len == DEVICE_ID_STR_LEN) &&
1137 (info->isServer)) {
1138 info->version = SOFTBUS_OLD_V1;
1139 return UnPackBtDeviceIdV1(info, data, len);
1140 }
1141 return UnpackDeviceIdJson((const char *)data, len, info);
1142 }
1143
GetSessionKeyList(int64_t authSeq,const AuthSessionInfo * info,SessionKeyList * list)1144 static void GetSessionKeyList(int64_t authSeq, const AuthSessionInfo *info, SessionKeyList *list)
1145 {
1146 ListInit(list);
1147 SessionKey sessionKey;
1148 if (AuthManagerGetSessionKey(authSeq, info, &sessionKey) != SOFTBUS_OK) {
1149 ALOGE("get session key fail.");
1150 return;
1151 }
1152 if (AddSessionKey(list, TO_INT32(authSeq), &sessionKey) != SOFTBUS_OK) {
1153 ALOGE("add session key fail.");
1154 return;
1155 }
1156 }
1157
PostDeviceInfoMessage(int64_t authSeq,const AuthSessionInfo * info)1158 int32_t PostDeviceInfoMessage(int64_t authSeq, const AuthSessionInfo *info)
1159 {
1160 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1161 char *msg = PackDeviceInfoMessage(info->connInfo.type, info->version, false, info->uuid);
1162 if (msg == NULL) {
1163 ALOGE("pack device info fail.");
1164 return SOFTBUS_ERR;
1165 }
1166 int32_t compressFlag = FLAG_UNCOMPRESS_DEVICE_INFO;
1167 uint8_t *compressData = NULL;
1168 uint32_t compressLen = 0;
1169 if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) && info->isSupportCompress) {
1170 ALOGI("before compress, datalen:%d", strlen(msg) + 1);
1171 if (DataCompress((uint8_t *)msg, strlen(msg) + 1, &compressData, &compressLen) != SOFTBUS_OK) {
1172 compressFlag = FLAG_UNCOMPRESS_DEVICE_INFO;
1173 } else {
1174 compressFlag = FLAG_COMPRESS_DEVICE_INFO;
1175 ALOGI("deviceInfo compress finish");
1176 }
1177 ALOGI("after compress, datalen:%d", compressLen);
1178 }
1179 uint8_t *inputData = NULL;
1180 uint32_t inputLen;
1181 uint8_t *data = NULL;
1182 uint32_t dataLen = 0;
1183 if ((compressData != NULL) && (compressLen != 0)) {
1184 inputData = compressData;
1185 inputLen = compressLen;
1186 } else {
1187 inputData = (uint8_t *)msg;
1188 inputLen = strlen(msg) + 1;
1189 }
1190 SessionKeyList sessionKeyList;
1191 GetSessionKeyList(authSeq, info, &sessionKeyList);
1192 if (EncryptInner(&sessionKeyList, inputData, inputLen, &data, &dataLen) != SOFTBUS_OK) {
1193 ALOGE("encrypt device info fail.");
1194 JSON_Free(msg);
1195 SoftBusFree(compressData);
1196 return SOFTBUS_ENCRYPT_ERR;
1197 }
1198 JSON_Free(msg);
1199 SoftBusFree(compressData);
1200 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI && info->isServer) {
1201 compressFlag = FLAG_RELAY_DEVICE_INFO;
1202 authSeq = 0;
1203 }
1204 AuthDataHead head = {
1205 .dataType = DATA_TYPE_DEVICE_INFO,
1206 .module = MODULE_AUTH_CONNECTION,
1207 .seq = authSeq,
1208 .flag = compressFlag,
1209 .len = dataLen,
1210 };
1211 if (PostAuthData(info->connId, !info->isServer, &head, data) != SOFTBUS_OK) {
1212 ALOGE("post device info fail.");
1213 SoftBusFree(data);
1214 return SOFTBUS_ERR;
1215 }
1216 SoftBusFree(data);
1217 return SOFTBUS_OK;
1218 }
1219
ProcessDeviceInfoMessage(int64_t authSeq,AuthSessionInfo * info,const uint8_t * data,uint32_t len)1220 int32_t ProcessDeviceInfoMessage(int64_t authSeq, AuthSessionInfo *info, const uint8_t *data, uint32_t len)
1221 {
1222 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1223 CHECK_NULL_PTR_RETURN_VALUE(data, SOFTBUS_INVALID_PARAM);
1224 uint8_t *msg = NULL;
1225 uint32_t msgSize = 0;
1226 SessionKeyList sessionKeyList;
1227 GetSessionKeyList(authSeq, info, &sessionKeyList);
1228 if (DecryptInner(&sessionKeyList, data, len, &msg, &msgSize) != SOFTBUS_OK) {
1229 ALOGE("decrypt device info fail");
1230 return SOFTBUS_DECRYPT_ERR;
1231 }
1232 uint8_t *decompressData = NULL;
1233 uint32_t decompressLen = 0;
1234 if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) && info->isSupportCompress) {
1235 ALOGI("before decompress, msgSize:%d", msgSize);
1236 if (DataDecompress((uint8_t *)msg, msgSize, &decompressData, &decompressLen) != SOFTBUS_OK) {
1237 ALOGE("data decompress fail");
1238 SoftBusFree(msg);
1239 return SOFTBUS_ERR;
1240 } else {
1241 ALOGI("deviceInfo deCompress finish, decompress:%d", decompressLen);
1242 }
1243 ALOGI("after decompress, datalen:%d", decompressLen);
1244 }
1245 DevInfoData devInfo = {NULL, 0, info->connInfo.type, info->version};
1246 if ((decompressData != NULL) && (decompressLen != 0)) {
1247 devInfo.msg = (const char *)decompressData;
1248 devInfo.len = decompressLen;
1249 } else {
1250 devInfo.msg = (const char *)msg;
1251 devInfo.len = msgSize;
1252 }
1253 if (UnpackDeviceInfoMessage(&devInfo, &info->nodeInfo, false) != SOFTBUS_OK) {
1254 ALOGE("unpack device info fail");
1255 SoftBusFree(msg);
1256 SoftBusFree(decompressData);
1257 return SOFTBUS_ERR;
1258 }
1259 SoftBusFree(msg);
1260 SoftBusFree(decompressData);
1261 return SOFTBUS_OK;
1262 }
1263
PostCloseAckMessage(int64_t authSeq,const AuthSessionInfo * info)1264 int32_t PostCloseAckMessage(int64_t authSeq, const AuthSessionInfo *info)
1265 {
1266 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1267 const char *msg = "";
1268 AuthDataHead head = {
1269 .dataType = DATA_TYPE_CLOSE_ACK,
1270 .module = 0,
1271 .seq = authSeq,
1272 .flag = 0,
1273 .len = strlen(msg) + 1,
1274 };
1275 if (PostAuthData(info->connId, !info->isServer, &head, (uint8_t *)msg) != SOFTBUS_OK) {
1276 ALOGE("post close ack fail.");
1277 return SOFTBUS_ERR;
1278 }
1279 return SOFTBUS_OK;
1280 }
1281
PostHichainAuthMessage(int64_t authSeq,const AuthSessionInfo * info,const uint8_t * data,uint32_t len)1282 int32_t PostHichainAuthMessage(int64_t authSeq, const AuthSessionInfo *info, const uint8_t *data, uint32_t len)
1283 {
1284 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1285 CHECK_NULL_PTR_RETURN_VALUE(data, SOFTBUS_INVALID_PARAM);
1286 AuthDataHead head = {
1287 .dataType = DATA_TYPE_AUTH,
1288 .module = MODULE_AUTH_SDK,
1289 .seq = authSeq,
1290 .flag = 0,
1291 .len = len,
1292 };
1293 if (PostAuthData(info->connId, !info->isServer, &head, data) != SOFTBUS_OK) {
1294 ALOGE("post hichain data fail.");
1295 return SOFTBUS_ERR;
1296 }
1297 return SOFTBUS_OK;
1298 }
1299
PackVerifyDeviceMessage(const char * uuid)1300 static char *PackVerifyDeviceMessage(const char *uuid)
1301 {
1302 JsonObj *obj = JSON_CreateObject();
1303 if (obj == NULL) {
1304 ALOGE("create json fail.");
1305 return NULL;
1306 }
1307 if (!JSON_AddInt32ToObject(obj, CODE, CODE_VERIFY_DEVICE) ||
1308 !JSON_AddStringToObject(obj, DEVICE_ID, uuid)) {
1309 ALOGE("add uuid fail.");
1310 JSON_Delete(obj);
1311 return NULL;
1312 }
1313 char *msg = JSON_PrintUnformatted(obj);
1314 JSON_Delete(obj);
1315 return msg;
1316 }
1317
IsFlushDevicePacket(const AuthConnInfo * connInfo,const AuthDataHead * head,const uint8_t * data,bool isServer)1318 bool IsFlushDevicePacket(const AuthConnInfo *connInfo, const AuthDataHead *head, const uint8_t *data, bool isServer)
1319 {
1320 if (connInfo->type != AUTH_LINK_TYPE_WIFI) {
1321 return false;
1322 }
1323 int64_t authId = AuthDeviceGetIdByConnInfo(connInfo, isServer);
1324 if (authId == AUTH_INVALID_ID) {
1325 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "is flush device packet not find authId");
1326 return false;
1327 }
1328 uint32_t decDataLen = AuthGetDecryptSize(head->len);
1329 uint8_t *decData = (uint8_t *)SoftBusCalloc(decDataLen);
1330 if (decData == NULL) {
1331 return false;
1332 }
1333 if (AuthDeviceDecrypt(authId, data, head->len, decData, &decDataLen) != SOFTBUS_OK) {
1334 SoftBusFree(decData);
1335 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "parse device info decrypt fail");
1336 return false;
1337 }
1338 JsonObj *json = JSON_Parse((char *)decData, decDataLen);
1339 if (json == NULL) {
1340 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "parse json fail.");
1341 SoftBusFree(decData);
1342 return false;
1343 }
1344 bool result = false;
1345 int32_t verifyDevice = 0;
1346 if (!JSON_GetInt32FromOject(json, CODE, &verifyDevice)) {
1347 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "parse device info fail");
1348 }
1349 if (verifyDevice == CODE_VERIFY_DEVICE) {
1350 result = true;
1351 }
1352 JSON_Delete(json);
1353 SoftBusFree(decData);
1354 return result;
1355 }
1356
PostVerifyDeviceMessage(const AuthManager * auth,int32_t flagRelay)1357 int32_t PostVerifyDeviceMessage(const AuthManager *auth, int32_t flagRelay)
1358 {
1359 CHECK_NULL_PTR_RETURN_VALUE(auth, SOFTBUS_INVALID_PARAM);
1360 char *msg = PackVerifyDeviceMessage(auth->uuid);
1361 if (msg == NULL) {
1362 ALOGE("pack verify device msg fail.");
1363 return SOFTBUS_ERR;
1364 }
1365
1366 uint8_t *data = NULL;
1367 uint32_t dataLen = 0;
1368 if (EncryptInner(&auth->sessionKeyList, (uint8_t *)msg, strlen(msg) + 1, &data, &dataLen) != SOFTBUS_OK) {
1369 ALOGE("encrypt device info fail.");
1370 JSON_Free(msg);
1371 return SOFTBUS_ENCRYPT_ERR;
1372 }
1373 JSON_Free(msg);
1374
1375 AuthDataHead head = {
1376 .dataType = DATA_TYPE_DEVICE_INFO,
1377 .module = MODULE_AUTH_CONNECTION,
1378 .seq = auth->authId,
1379 .flag = flagRelay,
1380 .len = dataLen,
1381 };
1382 if (PostAuthData(auth->connId, !auth->isServer, &head, data) != SOFTBUS_OK) {
1383 ALOGE("post verify device msg fail.");
1384 SoftBusFree(data);
1385 return SOFTBUS_ERR;
1386 }
1387 SoftBusFree(data);
1388 return SOFTBUS_OK;
1389 }
1390