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 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
77 ListInit(&list->list);
78 return list;
79 }
80
DestroySoftBusList(SoftBusList * list)81 void DestroySoftBusList(SoftBusList *list)
82 {
83 ListDelInit(&list->list);
84 SoftBusMutexDestroy(&list->lock);
85 SoftBusFree(list);
86 return;
87 }
88
RegisterTimeoutCallback(int32_t timerFunId,TimerFunCallback callback)89 int32_t RegisterTimeoutCallback(int32_t timerFunId, TimerFunCallback callback)
90 {
91 if (callback == NULL || timerFunId >= SOFTBUS_MAX_TIMER_FUN_NUM ||
92 timerFunId < SOFTBUS_CONN_TIMER_FUN) {
93 return SOFTBUS_ERR;
94 }
95
96 if (g_timerFunList[timerFunId] != NULL) {
97 return SOFTBUS_OK;
98 }
99
100 g_timerFunList[timerFunId] = callback;
101 return SOFTBUS_OK;
102 }
103
HandleTimeoutFun(void)104 static void HandleTimeoutFun(void)
105 {
106 int32_t i;
107 for (i = 0; i < SOFTBUS_MAX_TIMER_FUN_NUM; i++) {
108 if (g_timerFunList[i] != NULL) {
109 g_timerFunList[i]();
110 }
111 }
112 }
113
SoftBusTimerInit(void)114 int32_t SoftBusTimerInit(void)
115 {
116 if (g_timerId != NULL) {
117 return SOFTBUS_OK;
118 }
119 SetTimerFunc(HandleTimeoutFun);
120 g_timerId = SoftBusCreateTimer(&g_timerId, TIMER_TYPE_PERIOD);
121 if (SoftBusStartTimer(g_timerId, TIMER_TIMEOUT) != SOFTBUS_OK) {
122 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "start timer failed.");
123 (void)SoftBusDeleteTimer(g_timerId);
124 g_timerId = NULL;
125 return SOFTBUS_ERR;
126 }
127 return SOFTBUS_OK;
128 }
129
SoftBusTimerDeInit(void)130 void SoftBusTimerDeInit(void)
131 {
132 if (g_timerId != NULL) {
133 (void)SoftBusDeleteTimer(g_timerId);
134 g_timerId = NULL;
135 }
136 }
137
ConvertHexStringToBytes(unsigned char * outBuf,uint32_t outBufLen,const char * inBuf,uint32_t inLen)138 int32_t ConvertHexStringToBytes(unsigned char *outBuf, uint32_t outBufLen, const char *inBuf, uint32_t inLen)
139 {
140 (void)outBufLen;
141
142 if ((outBuf == NULL) || (inBuf == NULL) || (inLen % HEXIFY_UNIT_LEN != 0)) {
143 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
144 return SOFTBUS_ERR;
145 }
146
147 uint32_t outLen = UN_HEXIFY_LEN(inLen);
148 uint32_t i = 0;
149 while (i < outLen) {
150 unsigned char c = *inBuf++;
151 if ((c >= '0') && (c <= '9')) {
152 c -= '0';
153 } else if ((c >= 'a') && (c <= 'f')) {
154 c -= 'a' - DEC_MAX_NUM;
155 } else if ((c >= 'A') && (c <= 'F')) {
156 c -= 'A' - DEC_MAX_NUM;
157 } else {
158 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "HexToString Error! %c", c);
159 return SOFTBUS_ERR;
160 }
161
162 unsigned char c2 = *inBuf++;
163 if ((c2 >= '0') && (c2 <= '9')) {
164 c2 -= '0';
165 } else if ((c2 >= 'a') && (c2 <= 'f')) {
166 c2 -= 'a' - DEC_MAX_NUM;
167 } else if ((c2 >= 'A') && (c2 <= 'F')) {
168 c2 -= 'A' - DEC_MAX_NUM;
169 } else {
170 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "HexToString Error! %c2", c2);
171 return SOFTBUS_ERR;
172 }
173
174 *outBuf++ = (c << HEX_MAX_BIT_NUM) | c2;
175 i++;
176 }
177 return SOFTBUS_OK;
178 }
179
ConvertBytesToHexString(char * outBuf,uint32_t outBufLen,const unsigned char * inBuf,uint32_t inLen)180 int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, const unsigned char *inBuf, uint32_t inLen)
181 {
182 if ((outBuf == NULL) || (inBuf == NULL) || (outBufLen < HEXIFY_LEN(inLen))) {
183 return SOFTBUS_ERR;
184 }
185
186 while (inLen > 0) {
187 unsigned char h = *inBuf / HEX_MAX_NUM;
188 unsigned char l = *inBuf % HEX_MAX_NUM;
189
190 if (h < DEC_MAX_NUM) {
191 *outBuf++ = '0' + h;
192 } else {
193 *outBuf++ = 'a' + h - DEC_MAX_NUM;
194 }
195
196 if (l < DEC_MAX_NUM) {
197 *outBuf++ = '0' + l;
198 } else {
199 *outBuf++ = 'a' + l - DEC_MAX_NUM;
200 }
201
202 ++inBuf;
203 inLen--;
204 }
205 return SOFTBUS_OK;
206 }
207
GenerateRandomStr(char * str,uint32_t len)208 int32_t GenerateRandomStr(char *str, uint32_t len)
209 {
210 if ((str == NULL) || (len < HEXIFY_UNIT_LEN)) {
211 return SOFTBUS_INVALID_PARAM;
212 }
213
214 uint32_t hexLen = len / HEXIFY_UNIT_LEN;
215 unsigned char *hexAuthId = (unsigned char *)SoftBusMalloc(hexLen);
216 if (hexAuthId == NULL) {
217 return SOFTBUS_MEM_ERR;
218 }
219 (void)memset_s(hexAuthId, hexLen, 0, hexLen);
220
221 if (SoftBusGenerateRandomArray(hexAuthId, hexLen) != SOFTBUS_OK) {
222 SoftBusFree(hexAuthId);
223 return SOFTBUS_ERR;
224 }
225
226 if (ConvertBytesToHexString(str, len, hexAuthId, hexLen) != SOFTBUS_OK) {
227 SoftBusFree(hexAuthId);
228 return SOFTBUS_ERR;
229 }
230
231 SoftBusFree(hexAuthId);
232 return SOFTBUS_OK;
233 }
234
IsValidString(const char * input,uint32_t maxLen)235 bool IsValidString(const char *input, uint32_t maxLen)
236 {
237 if (input == NULL) {
238 return false;
239 }
240
241 uint32_t len = strlen(input);
242 if (len >= maxLen) {
243 return false;
244 }
245
246 return true;
247 }
248
ConvertBtMacToBinary(const char * strMac,uint32_t strMacLen,uint8_t * binMac,uint32_t binMacLen)249 int32_t ConvertBtMacToBinary(const char *strMac, uint32_t strMacLen, uint8_t *binMac, uint32_t binMacLen)
250 {
251 if (strMac == NULL || strMacLen < BT_MAC_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
252 return SOFTBUS_INVALID_PARAM;
253 }
254 char *tmpMac = (char *)SoftBusMalloc(strMacLen * sizeof(char));
255 if (tmpMac == NULL) {
256 return SOFTBUS_MALLOC_ERR;
257 }
258 if (memcpy_s(tmpMac, strMacLen, strMac, strMacLen) != EOK) {
259 SoftBusFree(tmpMac);
260 return SOFTBUS_MEM_ERR;
261 }
262 char *nextTokenPtr = NULL;
263 char *token = strtok_r((char *)tmpMac, BT_ADDR_DELIMITER, &nextTokenPtr);
264 char *endptr = NULL;
265 for (int i = 0; i < BT_ADDR_LEN; i++) {
266 if (token == NULL) {
267 SoftBusFree(tmpMac);
268 return SOFTBUS_ERR;
269 }
270 binMac[i] = strtoul(token, &endptr, BT_ADDR_BASE);
271 token = strtok_r(NULL, BT_ADDR_DELIMITER, &nextTokenPtr);
272 }
273 SoftBusFree(tmpMac);
274 return SOFTBUS_OK;
275 }
276
ConvertBtMacToStr(char * strMac,uint32_t strMacLen,const uint8_t * binMac,uint32_t binMacLen)277 int32_t ConvertBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac, uint32_t binMacLen)
278 {
279 int32_t ret;
280
281 if (strMac == NULL || strMacLen < BT_MAC_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
282 return SOFTBUS_INVALID_PARAM;
283 }
284 ret = snprintf_s(strMac, strMacLen, strMacLen - 1, "%02x:%02x:%02x:%02x:%02x:%02x",
285 binMac[MAC_BIT_ZERO], binMac[MAC_BIT_ONE], binMac[MAC_BIT_TWO],
286 binMac[MAC_BIT_THREE], binMac[MAC_BIT_FOUR], binMac[MAC_BIT_FIVE]);
287 if (ret < 0) {
288 return SOFTBUS_ERR;
289 }
290 return SOFTBUS_OK;
291 }
292
ToUpperCase(char ch)293 static char ToUpperCase(char ch)
294 {
295 if (ch >= 'a' && ch <= 'z') {
296 return ch - 'a' + 'A';
297 }
298 return ch;
299 }
300
StrCmpIgnoreCase(const char * str1,const char * str2)301 int32_t StrCmpIgnoreCase(const char *str1, const char *str2)
302 {
303 if (str1 == NULL || str2 == NULL) {
304 return SOFTBUS_ERR;
305 }
306 int32_t i;
307 for (i = 0; str1[i] != '\0' && str2[i] != '\0'; i++) {
308 if (ToUpperCase(str1[i]) != ToUpperCase(str2[i])) {
309 return SOFTBUS_ERR;
310 }
311 }
312 if (str1[i] != '\0' || str2[i] != '\0') {
313 return SOFTBUS_ERR;
314 }
315 return SOFTBUS_OK;
316 }
317
SetSignalingMsgSwitchOn(void)318 void SetSignalingMsgSwitchOn(void)
319 {
320 g_signalingMsgSwitch = true;
321 }
322
SetSignalingMsgSwitchOff(void)323 void SetSignalingMsgSwitchOff(void)
324 {
325 g_signalingMsgSwitch = false;
326 }
327
GetSignalingMsgSwitch(void)328 bool GetSignalingMsgSwitch(void)
329 {
330 return g_signalingMsgSwitch;
331 }
332
SignalingMsgPrint(const char * distinguish,unsigned char * data,unsigned char dataLen,uint32_t module)333 void SignalingMsgPrint(const char *distinguish, unsigned char *data, unsigned char dataLen, uint32_t module)
334 {
335 int ret = 0;
336 char signalingMsgBuf[BUF_HEX_LEN] = {0};
337
338 if (!GetSignalingMsgSwitch()) {
339 return;
340 }
341
342 if (dataLen >= BUF_BYTE_LEN) {
343 ret = ConvertBytesToHexString(signalingMsgBuf, BUF_HEX_LEN + OFFSET, data, BUF_BYTE_LEN);
344 } else {
345 ret = ConvertBytesToHexString(signalingMsgBuf, BUF_HEX_LEN + OFFSET, data, dataLen);
346 }
347
348 if (ret != SOFTBUS_OK) {
349 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "intercept signaling msg faile");
350 return;
351 }
352
353 if (module == SOFTBUS_LOG_DISC) {
354 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "[signaling]:%s, len:%d, data:%s",
355 distinguish, dataLen, signalingMsgBuf);
356 } else if (module == SOFTBUS_LOG_CONN) {
357 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "[signaling]:%s, len:%d, data:%s",
358 distinguish, dataLen, signalingMsgBuf);
359 }
360 }
361
MacInstead(char * data,uint32_t length,char delimiter)362 void MacInstead(char *data, uint32_t length, char delimiter)
363 {
364 if (length > MAX_MAC_LEN) {
365 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "MacInstead len is invalid");
366 return;
367 }
368 int delimiterCnt = 0;
369
370 for (uint32_t i = 0; i < length; i++) {
371 if (delimiterCnt == MAC_DELIMITER_FOURTH) {
372 break;
373 }
374 if (data[i] == delimiter) {
375 delimiterCnt++;
376 }
377 if (delimiterCnt >= MAC_DELIMITER_SECOND && data[i] != delimiter) {
378 data[i] = '*';
379 }
380 }
381 }
382
IpInstead(char * data,uint32_t length,char delimiter)383 void IpInstead(char *data, uint32_t length, char delimiter)
384 {
385 if (length > MAX_IP_LEN) {
386 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "IpInstead len is invalid");
387 return;
388 }
389 int delimiterCnt = 0;
390 for (uint32_t i = 0; i < length; i++) {
391 if (delimiterCnt == IP_DELIMITER_THIRD) {
392 break;
393 }
394 if (data[i] == delimiter) {
395 delimiterCnt++;
396 }
397 if (delimiterCnt >= IP_DELIMITER_FIRST && data[i] != delimiter) {
398 data[i] = '*';
399 }
400 }
401 }
402
IdInstead(char * data,uint32_t length)403 void IdInstead(char *data, uint32_t length)
404 {
405 if (length > MAX_ID_LEN) {
406 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "IdInstead len is invalid");
407 return;
408 }
409 uint32_t halfLen = length / GET_ID_HALF_LEN;
410 for (uint32_t i = 0; i < length - 1; i++) {
411 if (i > halfLen) {
412 data[i] = '*';
413 }
414 }
415 }
416
DataMasking(const char * data,uint32_t length,char delimiter,char * container)417 void DataMasking(const char *data, uint32_t length, char delimiter, char *container)
418 {
419 if (data == NULL) {
420 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
421 return;
422 }
423 if (memcpy_s(container, length, data, length) != EOK) {
424 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "container memcpy_s failed");
425 return;
426 }
427 switch (delimiter) {
428 case MAC_DELIMITER:
429 MacInstead(container, length, delimiter);
430 break;
431 case IP_DELIMITER:
432 IpInstead(container, length, delimiter);
433 break;
434 case ID_DELIMITER:
435 IdInstead(container, length);
436 break;
437 default:
438 break;
439 }
440 }
441
GenerateStrHashAndConvertToHexString(const unsigned char * str,uint32_t len,unsigned char * hashStr,uint32_t hashStrLen)442 int32_t GenerateStrHashAndConvertToHexString(const unsigned char *str, uint32_t len, unsigned char *hashStr,
443 uint32_t hashStrLen)
444 {
445 int32_t ret;
446 unsigned char hashResult[SHA_256_HASH_LEN] = {0};
447 if (hashStrLen < HEXIFY_LEN(len / HEXIFY_UNIT_LEN)) {
448 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "generate str hash invalid hashStrLen");
449 return SOFTBUS_INVALID_PARAM;
450 }
451 if (str == NULL) {
452 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "generate str hash invalid param");
453 return SOFTBUS_INVALID_PARAM;
454 }
455 ret = SoftBusGenerateStrHash(str, strlen((char *)str) + 1, hashResult);
456 if (ret != SOFTBUS_OK) {
457 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "generate str hash fail, ret=%d", ret);
458 return ret;
459 }
460 ret = ConvertBytesToHexString((char *)hashStr, hashStrLen, (const unsigned char *)hashResult,
461 len / HEXIFY_UNIT_LEN);
462 if (ret != SOFTBUS_OK) {
463 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "convert bytes to str hash fail, ret=%d", ret);
464 return ret;
465 }
466 return SOFTBUS_OK;
467 }