1 /*
2 * Copyright (c) 2021 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 "softbus_utils.h"
17
18 #include <ctype.h>
19 #include <stdlib.h>
20
21 #include "securec.h"
22 #include "softbus_adapter_crypto.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_adapter_thread.h"
25 #include "softbus_adapter_timer.h"
26 #include "softbus_common.h"
27 #include "softbus_def.h"
28 #include "softbus_errcode.h"
29 #include "softbus_log.h"
30 #include "softbus_type_def.h"
31
32 #define MAC_BIT_ZERO 0
33 #define MAC_BIT_ONE 1
34 #define MAC_BIT_TWO 2
35 #define MAC_BIT_THREE 3
36 #define MAC_BIT_FOUR 4
37 #define MAC_BIT_FIVE 5
38
39 #define BT_ADDR_LEN 6
40 #define BT_ADDR_DELIMITER ":"
41 #define BT_ADDR_BASE 16
42
43 #define BUF_BYTE_LEN 64
44 #define BUF_HEX_LEN 128
45 #define OFFSET 1
46
47 #define MAC_DELIMITER_SECOND 2
48 #define MAC_DELIMITER_FOURTH 4
49 #define IP_DELIMITER_FIRST 1
50 #define IP_DELIMITER_THIRD 3
51 #define GET_ID_HALF_LEN 2
52 #define MAX_ID_LEN 65
53 #define MAX_IP_LEN 48
54 #define MAX_MAC_LEN 46
55
56 static void *g_timerId = NULL;
57 static TimerFunCallback g_timerFunList[SOFTBUS_MAX_TIMER_FUN_NUM] = {0};
58 static bool g_signalingMsgSwitch = false;
59
CreateSoftBusList(void)60 NO_SANITIZE("cfi") SoftBusList *CreateSoftBusList(void)
61 {
62 SoftBusList *list = (SoftBusList *)SoftBusMalloc(sizeof(SoftBusList));
63 if (list == NULL) {
64 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "malloc failed");
65 return NULL;
66 }
67 (void)memset_s(list, sizeof(SoftBusList), 0, sizeof(SoftBusList));
68
69 SoftBusMutexAttr mutexAttr;
70 mutexAttr.type = SOFTBUS_MUTEX_RECURSIVE;
71 if (SoftBusMutexInit(&list->lock, &mutexAttr) != SOFTBUS_OK) {
72 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "init lock failed");
73 SoftBusFree(list);
74 return NULL;
75 }
76 ListInit(&list->list);
77 return list;
78 }
79
DestroySoftBusList(SoftBusList * list)80 NO_SANITIZE("cfi") void DestroySoftBusList(SoftBusList *list)
81 {
82 ListDelInit(&list->list);
83 SoftBusMutexDestroy(&list->lock);
84 SoftBusFree(list);
85 return;
86 }
87
RegisterTimeoutCallback(int32_t timerFunId,TimerFunCallback callback)88 int32_t RegisterTimeoutCallback(int32_t timerFunId, TimerFunCallback callback)
89 {
90 if (callback == NULL || timerFunId >= SOFTBUS_MAX_TIMER_FUN_NUM ||
91 timerFunId < SOFTBUS_CONN_TIMER_FUN) {
92 return SOFTBUS_ERR;
93 }
94 if (g_timerFunList[timerFunId] != NULL) {
95 return SOFTBUS_OK;
96 }
97 g_timerFunList[timerFunId] = callback;
98 return SOFTBUS_OK;
99 }
100
HandleTimeoutFun(void)101 NO_SANITIZE("cfi") static void HandleTimeoutFun(void)
102 {
103 int32_t i;
104 for (i = 0; i < SOFTBUS_MAX_TIMER_FUN_NUM; i++) {
105 if (g_timerFunList[i] != NULL) {
106 g_timerFunList[i]();
107 }
108 }
109 }
110
SoftBusTimerInit(void)111 int32_t SoftBusTimerInit(void)
112 {
113 if (g_timerId != NULL) {
114 return SOFTBUS_OK;
115 }
116 SetTimerFunc(HandleTimeoutFun);
117 g_timerId = SoftBusCreateTimer(&g_timerId, TIMER_TYPE_PERIOD);
118 if (SoftBusStartTimer(g_timerId, TIMER_TIMEOUT) != SOFTBUS_OK) {
119 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "start timer failed.");
120 (void)SoftBusDeleteTimer(g_timerId);
121 g_timerId = NULL;
122 return SOFTBUS_ERR;
123 }
124 return SOFTBUS_OK;
125 }
126
SoftBusTimerDeInit(void)127 void SoftBusTimerDeInit(void)
128 {
129 if (g_timerId != NULL) {
130 (void)SoftBusDeleteTimer(g_timerId);
131 g_timerId = NULL;
132 }
133 }
134
ConvertBytesToUpperCaseHexString(char * outBuf,uint32_t outBufLen,const unsigned char * inBuf,uint32_t inLen)135 int32_t ConvertBytesToUpperCaseHexString(char *outBuf, uint32_t outBufLen, const unsigned char * inBuf,
136 uint32_t inLen)
137 {
138 if ((outBuf == NULL) || (inBuf == NULL) || (outBufLen < HEXIFY_LEN(inLen))) {
139 return SOFTBUS_ERR;
140 }
141
142 while (inLen > 0) {
143 unsigned char h = *inBuf / HEX_MAX_NUM;
144 unsigned char l = *inBuf % HEX_MAX_NUM;
145 if (h < DEC_MAX_NUM) {
146 *outBuf++ = '0' + h;
147 } else {
148 *outBuf++ = 'A' + h - DEC_MAX_NUM;
149 }
150 if (l < DEC_MAX_NUM) {
151 *outBuf++ = '0' + l;
152 } else {
153 *outBuf++ = 'A' + l - DEC_MAX_NUM;
154 }
155 ++inBuf;
156 inLen--;
157 }
158 return SOFTBUS_OK;
159 }
160
ConvertHexStringToBytes(unsigned char * outBuf,uint32_t outBufLen,const char * inBuf,uint32_t inLen)161 NO_SANITIZE("cfi") int32_t ConvertHexStringToBytes(unsigned char *outBuf, uint32_t outBufLen, const char *inBuf,
162 uint32_t inLen)
163 {
164 (void)outBufLen;
165 if ((outBuf == NULL) || (inBuf == NULL) || (inLen % HEXIFY_UNIT_LEN != 0)) {
166 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
167 return SOFTBUS_ERR;
168 }
169
170 uint32_t outLen = UN_HEXIFY_LEN(inLen);
171 uint32_t i = 0;
172 while (i < outLen) {
173 unsigned char c = *inBuf++;
174 if ((c >= '0') && (c <= '9')) {
175 c -= '0';
176 } else if ((c >= 'a') && (c <= 'f')) {
177 c -= 'a' - DEC_MAX_NUM;
178 } else if ((c >= 'A') && (c <= 'F')) {
179 c -= 'A' - DEC_MAX_NUM;
180 } else {
181 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "HexToString Error! %c", c);
182 return SOFTBUS_ERR;
183 }
184 unsigned char c2 = *inBuf++;
185 if ((c2 >= '0') && (c2 <= '9')) {
186 c2 -= '0';
187 } else if ((c2 >= 'a') && (c2 <= 'f')) {
188 c2 -= 'a' - DEC_MAX_NUM;
189 } else if ((c2 >= 'A') && (c2 <= 'F')) {
190 c2 -= 'A' - DEC_MAX_NUM;
191 } else {
192 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "HexToString Error! %c2", c2);
193 return SOFTBUS_ERR;
194 }
195 *outBuf++ = (c << HEX_MAX_BIT_NUM) | c2;
196 i++;
197 }
198 return SOFTBUS_OK;
199 }
200
ConvertBytesToHexString(char * outBuf,uint32_t outBufLen,const unsigned char * inBuf,uint32_t inLen)201 NO_SANITIZE("cfi") int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, const unsigned char *inBuf,
202 uint32_t inLen)
203 {
204 if ((outBuf == NULL) || (inBuf == NULL) || (outBufLen < HEXIFY_LEN(inLen))) {
205 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "outBufLen=%d inLen=%d", outBufLen, inLen);
206 return SOFTBUS_ERR;
207 }
208
209 while (inLen > 0) {
210 unsigned char h = *inBuf / HEX_MAX_NUM;
211 unsigned char l = *inBuf % HEX_MAX_NUM;
212 if (h < DEC_MAX_NUM) {
213 *outBuf++ = '0' + h;
214 } else {
215 *outBuf++ = 'a' + h - DEC_MAX_NUM;
216 }
217 if (l < DEC_MAX_NUM) {
218 *outBuf++ = '0' + l;
219 } else {
220 *outBuf++ = 'a' + l - DEC_MAX_NUM;
221 }
222 ++inBuf;
223 inLen--;
224 }
225 return SOFTBUS_OK;
226 }
227
GenerateRandomStr(char * str,uint32_t len)228 NO_SANITIZE("cfi") int32_t GenerateRandomStr(char *str, uint32_t len)
229 {
230 if ((str == NULL) || (len < HEXIFY_UNIT_LEN)) {
231 return SOFTBUS_INVALID_PARAM;
232 }
233
234 uint32_t hexLen = len / HEXIFY_UNIT_LEN;
235 unsigned char *hexAuthId = (unsigned char *)SoftBusMalloc(hexLen);
236 if (hexAuthId == NULL) {
237 return SOFTBUS_MEM_ERR;
238 }
239 (void)memset_s(hexAuthId, hexLen, 0, hexLen);
240 if (SoftBusGenerateRandomArray(hexAuthId, hexLen) != SOFTBUS_OK) {
241 SoftBusFree(hexAuthId);
242 return SOFTBUS_ERR;
243 }
244 if (ConvertBytesToHexString(str, len, hexAuthId, hexLen) != SOFTBUS_OK) {
245 SoftBusFree(hexAuthId);
246 return SOFTBUS_ERR;
247 }
248 SoftBusFree(hexAuthId);
249 return SOFTBUS_OK;
250 }
251
IsValidString(const char * input,uint32_t maxLen)252 bool IsValidString(const char *input, uint32_t maxLen)
253 {
254 if (input == NULL) {
255 return false;
256 }
257 uint32_t len = strlen(input);
258 if (len == 0 || len > maxLen) {
259 return false;
260 }
261 return true;
262 }
263
ConvertBtMacToBinary(const char * strMac,uint32_t strMacLen,uint8_t * binMac,uint32_t binMacLen)264 NO_SANITIZE("cfi") int32_t ConvertBtMacToBinary(const char *strMac, uint32_t strMacLen, uint8_t *binMac,
265 uint32_t binMacLen)
266 {
267 if (strMac == NULL || strMacLen < BT_MAC_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
268 return SOFTBUS_INVALID_PARAM;
269 }
270 char *tmpMac = (char *)SoftBusMalloc(strMacLen * sizeof(char));
271 if (tmpMac == NULL) {
272 return SOFTBUS_MALLOC_ERR;
273 }
274 if (memcpy_s(tmpMac, strMacLen, strMac, strMacLen) != EOK) {
275 SoftBusFree(tmpMac);
276 return SOFTBUS_MEM_ERR;
277 }
278 char *nextTokenPtr = NULL;
279 char *token = strtok_r((char *)tmpMac, BT_ADDR_DELIMITER, &nextTokenPtr);
280 char *endptr = NULL;
281 for (int i = 0; i < BT_ADDR_LEN; i++) {
282 if (token == NULL) {
283 SoftBusFree(tmpMac);
284 return SOFTBUS_ERR;
285 }
286 binMac[i] = strtoul(token, &endptr, BT_ADDR_BASE);
287 token = strtok_r(NULL, BT_ADDR_DELIMITER, &nextTokenPtr);
288 }
289 SoftBusFree(tmpMac);
290 return SOFTBUS_OK;
291 }
292
ConvertBtMacToStrNoColon(char * strMac,uint32_t strMacLen,const uint8_t * binMac,uint32_t binMacLen)293 NO_SANITIZE("cfi") int32_t ConvertBtMacToStrNoColon(char *strMac, uint32_t strMacLen, const uint8_t *binMac,
294 uint32_t binMacLen)
295 {
296 int32_t ret;
297
298 if (strMac == NULL || strMacLen < BT_MAC_NO_COLON_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
299 return SOFTBUS_INVALID_PARAM;
300 }
301 ret = snprintf_s(strMac, strMacLen, strMacLen - 1, "%02x%02x%02x%02x%02x%02x",
302 binMac[MAC_BIT_ZERO], binMac[MAC_BIT_ONE], binMac[MAC_BIT_TWO],
303 binMac[MAC_BIT_THREE], binMac[MAC_BIT_FOUR], binMac[MAC_BIT_FIVE]);
304 if (ret < 0) {
305 return SOFTBUS_ERR;
306 }
307 return SOFTBUS_OK;
308 }
309
ConvertBtMacToStr(char * strMac,uint32_t strMacLen,const uint8_t * binMac,uint32_t binMacLen)310 NO_SANITIZE("cfi") int32_t ConvertBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac,
311 uint32_t binMacLen)
312 {
313 int32_t ret;
314
315 if (strMac == NULL || strMacLen < BT_MAC_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
316 return SOFTBUS_INVALID_PARAM;
317 }
318 ret = snprintf_s(strMac, strMacLen, strMacLen - 1, "%02x:%02x:%02x:%02x:%02x:%02x",
319 binMac[MAC_BIT_ZERO], binMac[MAC_BIT_ONE], binMac[MAC_BIT_TWO],
320 binMac[MAC_BIT_THREE], binMac[MAC_BIT_FOUR], binMac[MAC_BIT_FIVE]);
321 if (ret < 0) {
322 return SOFTBUS_ERR;
323 }
324 return SOFTBUS_OK;
325 }
326
ToUpperCase(char ch)327 static char ToUpperCase(char ch)
328 {
329 if (ch >= 'a' && ch <= 'z') {
330 return ch - 'a' + 'A';
331 }
332 return ch;
333 }
334
ToLowerCase(char ch)335 static char ToLowerCase(char ch)
336 {
337 if (ch >= 'A' && ch <= 'Z') {
338 return ch - 'A' + 'a';
339 }
340 return ch;
341 }
342
StringToUpperCase(const char * str,char * buf,int32_t size)343 int32_t StringToUpperCase(const char *str, char *buf, int32_t size)
344 {
345 if (str == NULL || buf == NULL) {
346 return SOFTBUS_ERR;
347 }
348 memset_s(buf, size, 0, size);
349 int32_t i;
350 for (i = 0; str[i] != '\0'; i++) {
351 buf[i] = ToUpperCase(str[i]);
352 }
353 return SOFTBUS_OK;
354 }
355
StringToLowerCase(const char * str,char * buf,int32_t size)356 int32_t StringToLowerCase(const char *str, char *buf, int32_t size)
357 {
358 if (str == NULL || buf == NULL) {
359 return SOFTBUS_ERR;
360 }
361 memset_s(buf, size, 0, size);
362 int32_t i;
363 for (i = 0; str[i] != '\0'; i++) {
364 buf[i] = ToLowerCase(str[i]);
365 }
366 return SOFTBUS_OK;
367 }
368
StrCmpIgnoreCase(const char * str1,const char * str2)369 int32_t StrCmpIgnoreCase(const char *str1, const char *str2)
370 {
371 if (str1 == NULL || str2 == NULL) {
372 return SOFTBUS_ERR;
373 }
374 int32_t i;
375 for (i = 0; str1[i] != '\0' && str2[i] != '\0'; i++) {
376 if (ToUpperCase(str1[i]) != ToUpperCase(str2[i])) {
377 return SOFTBUS_ERR;
378 }
379 }
380 if (str1[i] != '\0' || str2[i] != '\0') {
381 return SOFTBUS_ERR;
382 }
383 return SOFTBUS_OK;
384 }
385
SetSignalingMsgSwitchOn(void)386 void SetSignalingMsgSwitchOn(void)
387 {
388 g_signalingMsgSwitch = true;
389 }
390
SetSignalingMsgSwitchOff(void)391 void SetSignalingMsgSwitchOff(void)
392 {
393 g_signalingMsgSwitch = false;
394 }
395
GetSignalingMsgSwitch(void)396 bool GetSignalingMsgSwitch(void)
397 {
398 return g_signalingMsgSwitch;
399 }
400
SignalingMsgPrint(const char * distinguish,unsigned char * data,unsigned char dataLen,uint32_t module)401 void SignalingMsgPrint(const char *distinguish, unsigned char *data, unsigned char dataLen, uint32_t module)
402 {
403 int ret = 0;
404 char signalingMsgBuf[BUF_HEX_LEN] = {0};
405
406 if (!GetSignalingMsgSwitch()) {
407 return;
408 }
409 if (dataLen >= BUF_BYTE_LEN) {
410 ret = ConvertBytesToHexString(signalingMsgBuf, BUF_HEX_LEN + OFFSET, data, BUF_BYTE_LEN);
411 } else {
412 ret = ConvertBytesToHexString(signalingMsgBuf, BUF_HEX_LEN + OFFSET, data, dataLen);
413 }
414 if (ret != SOFTBUS_OK) {
415 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "intercept signaling msg faile");
416 return;
417 }
418 if (module == SOFTBUS_LOG_DISC) {
419 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "[signaling]:%s, len:%d, data:%s",
420 distinguish, dataLen, signalingMsgBuf);
421 } else if (module == SOFTBUS_LOG_CONN) {
422 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "[signaling]:%s, len:%d, data:%s",
423 distinguish, dataLen, signalingMsgBuf);
424 }
425 }
426
MacInstead(char * data,uint32_t length,char delimiter)427 void MacInstead(char *data, uint32_t length, char delimiter)
428 {
429 if (length > MAX_MAC_LEN) {
430 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "MacInstead len is invalid");
431 return;
432 }
433 int delimiterCnt = 0;
434 for (uint32_t i = 0; i < length; i++) {
435 if (delimiterCnt == MAC_DELIMITER_FOURTH) {
436 break;
437 }
438 if (data[i] == delimiter) {
439 delimiterCnt++;
440 }
441 if (delimiterCnt >= MAC_DELIMITER_SECOND && data[i] != delimiter) {
442 data[i] = '*';
443 }
444 }
445 }
446
IpInstead(char * data,uint32_t length,char delimiter)447 void IpInstead(char *data, uint32_t length, char delimiter)
448 {
449 if (length > MAX_IP_LEN) {
450 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "IpInstead len is invalid");
451 return;
452 }
453 int delimiterCnt = 0;
454 for (uint32_t i = 0; i < length; i++) {
455 if (delimiterCnt == IP_DELIMITER_THIRD) {
456 break;
457 }
458 if (data[i] == delimiter) {
459 delimiterCnt++;
460 }
461 if (delimiterCnt >= IP_DELIMITER_FIRST && data[i] != delimiter) {
462 data[i] = '*';
463 }
464 }
465 }
466
IdInstead(char * data,uint32_t length)467 void IdInstead(char *data, uint32_t length)
468 {
469 if (length > MAX_ID_LEN) {
470 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "IdInstead len is invalid");
471 return;
472 }
473 uint32_t halfLen = length / GET_ID_HALF_LEN;
474 for (uint32_t i = 0; i < length - 1; i++) {
475 if (i > halfLen) {
476 data[i] = '*';
477 }
478 }
479 }
480
DataMasking(const char * data,uint32_t length,char delimiter,char * container)481 NO_SANITIZE("cfi") void DataMasking(const char *data, uint32_t length, char delimiter, char *container)
482 {
483 if (data == NULL) {
484 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
485 return;
486 }
487 if (memcpy_s(container, length, data, length) != EOK) {
488 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "container memcpy_s failed");
489 return;
490 }
491 switch (delimiter) {
492 case MAC_DELIMITER:
493 MacInstead(container, length, delimiter);
494 break;
495 case IP_DELIMITER:
496 IpInstead(container, length, delimiter);
497 break;
498 case ID_DELIMITER:
499 IdInstead(container, length);
500 break;
501 default:
502 break;
503 }
504 }
505
GenerateStrHashAndConvertToHexString(const unsigned char * str,uint32_t len,unsigned char * hashStr,uint32_t hashStrLen)506 NO_SANITIZE("cfi") int32_t GenerateStrHashAndConvertToHexString(const unsigned char *str, uint32_t len,
507 unsigned char *hashStr, uint32_t hashStrLen)
508 {
509 int32_t ret;
510 unsigned char hashResult[SHA_256_HASH_LEN] = {0};
511 if (hashStrLen < HEXIFY_LEN(len / HEXIFY_UNIT_LEN)) {
512 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "generate str hash invalid hashStrLen");
513 return SOFTBUS_INVALID_PARAM;
514 }
515 if (str == NULL) {
516 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "generate str hash invalid param");
517 return SOFTBUS_INVALID_PARAM;
518 }
519 ret = SoftBusGenerateStrHash(str, strlen((char *)str) + 1, hashResult);
520 if (ret != SOFTBUS_OK) {
521 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "generate str hash fail, ret=%d", ret);
522 return ret;
523 }
524 ret = ConvertBytesToHexString((char *)hashStr, hashStrLen, (const unsigned char *)hashResult,
525 len / HEXIFY_UNIT_LEN);
526 if (ret != SOFTBUS_OK) {
527 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "convert bytes to str hash fail, ret=%d", ret);
528 return ret;
529 }
530 return SOFTBUS_OK;
531 }