1 /*
2 * Copyright (c) 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 "lnn_network_id.h"
17
18 #include <stdbool.h>
19 #include <stdlib.h>
20
21 #include <securec.h>
22
23 #include "anonymizer.h"
24 #include "lnn_file_utils.h"
25 #include "lnn_log.h"
26 #include "lnn_node_info.h"
27 #include "softbus_adapter_crypto.h"
28 #include "softbus_adapter_file.h"
29 #include "softbus_bus_center.h"
30 #include "softbus_def.h"
31 #include "softbus_error_code.h"
32 #include "softbus_utils.h"
33
GetUuidFromFile(char * id,uint32_t len,bool isUpdate)34 static int32_t GetUuidFromFile(char *id, uint32_t len, bool isUpdate)
35 {
36 int32_t rc;
37 char uuidFilePath[SOFTBUS_MAX_PATH_LEN] = {0};
38
39 rc = LnnGetFullStoragePath(LNN_FILE_ID_UUID, uuidFilePath, SOFTBUS_MAX_PATH_LEN);
40 if (rc != SOFTBUS_OK) {
41 LNN_LOGE(LNN_STATE, "get uuid save path fail");
42 return rc;
43 }
44 if (isUpdate || SoftBusReadFullFile(uuidFilePath, id, len) != SOFTBUS_OK) {
45 rc = GenerateRandomStr(id, len);
46 if (rc != SOFTBUS_OK) {
47 LNN_LOGE(LNN_STATE, "generate uuid id fail");
48 return rc;
49 }
50 rc = SoftBusWriteFile(uuidFilePath, id, len);
51 if (rc != SOFTBUS_OK) {
52 LNN_LOGE(LNN_STATE, "write uuid to file failed");
53 return rc;
54 }
55 }
56 if (id[len - 1] != '\0' || strlen(id) != (len - 1)) {
57 LNN_LOGE(LNN_STATE, "uuid is invalid format");
58 return SOFTBUS_NETWORK_GET_UUID_FROM_FILE_FAILED;
59 }
60 return SOFTBUS_OK;
61 }
62
LnnGenLocalNetworkId(char * networkId,uint32_t len)63 int32_t LnnGenLocalNetworkId(char *networkId, uint32_t len)
64 {
65 if (networkId == NULL || len < NETWORK_ID_BUF_LEN) {
66 LNN_LOGE(LNN_STATE, "invalid prama");
67 return SOFTBUS_INVALID_PARAM;
68 }
69
70 int32_t rc = GenerateRandomStr(networkId, NETWORK_ID_BUF_LEN);
71 if (rc != SOFTBUS_OK) {
72 LNN_LOGE(LNN_STATE, "generate network id fail");
73 return rc;
74 }
75 networkId[len - 1] = '\0';
76 return SOFTBUS_OK;
77 }
78
LnnGenLocalUuid(char * uuid,uint32_t len,bool isUpdate)79 int32_t LnnGenLocalUuid(char *uuid, uint32_t len, bool isUpdate)
80 {
81 static bool isGenerated = false;
82 static char localUuid[UUID_BUF_LEN] = {0};
83
84 if (uuid == NULL || len < UUID_BUF_LEN) {
85 LNN_LOGE(LNN_STATE, "invalid param");
86 return SOFTBUS_INVALID_PARAM;
87 }
88
89 if (isUpdate || isGenerated == false) {
90 if (GetUuidFromFile(localUuid, UUID_BUF_LEN, isUpdate) != SOFTBUS_OK) {
91 LNN_LOGE(LNN_STATE, "get uuid from file failed");
92 return SOFTBUS_NETWORK_GET_UUID_FROM_FILE_FAILED;
93 }
94 isGenerated = true;
95 }
96 if (strncpy_s(uuid, len, localUuid, UUID_BUF_LEN) != EOK) {
97 LNN_LOGE(LNN_STATE, "copy uuid id fail");
98 return SOFTBUS_STRCPY_ERR;
99 }
100 return SOFTBUS_OK;
101 }
102
GetIrkFromFile(unsigned char * irk,uint32_t len,bool isUpdate)103 static int32_t GetIrkFromFile(unsigned char *irk, uint32_t len, bool isUpdate)
104 {
105 int32_t rc;
106 char irkFilePath[SOFTBUS_MAX_PATH_LEN] = {0};
107
108 rc = LnnGetFullStoragePath(LNN_FILE_ID_IRK_KEY, irkFilePath, SOFTBUS_MAX_PATH_LEN);
109 if (rc != SOFTBUS_OK) {
110 LNN_LOGE(LNN_STATE, "get irk save path fail");
111 return rc;
112 }
113 if (isUpdate || SoftBusReadFullFile(irkFilePath, (char *)irk, len) != SOFTBUS_OK) {
114 rc = SoftBusGenerateRandomArray(irk, len);
115 if (rc != SOFTBUS_OK) {
116 LNN_LOGE(LNN_STATE, "generate irk id fail");
117 return rc;
118 }
119 rc = SoftBusWriteFile(irkFilePath, (char *)irk, len);
120 if (rc != SOFTBUS_OK) {
121 LNN_LOGE(LNN_STATE, "write irk to file failed");
122 return rc;
123 }
124 }
125 return SOFTBUS_OK;
126 }
127
LnnGenLocalIrk(unsigned char * irk,uint32_t len,bool isUpdate)128 int32_t LnnGenLocalIrk(unsigned char *irk, uint32_t len, bool isUpdate)
129 {
130 static bool isIrkGenerated = false;
131 static char locaIrk[LFINDER_IRK_LEN] = {0};
132
133 if (irk == NULL || len < LFINDER_IRK_LEN) {
134 return SOFTBUS_INVALID_PARAM;
135 }
136
137 if (isUpdate || !isIrkGenerated) {
138 if (GetIrkFromFile((unsigned char *)locaIrk, LFINDER_IRK_LEN, isUpdate) != SOFTBUS_OK) {
139 LNN_LOGE(LNN_STATE, "get irk from file failed");
140 return SOFTBUS_GET_IRK_FAIL;
141 }
142 isIrkGenerated = true;
143 }
144 if (memcpy_s(irk, len, locaIrk, LFINDER_IRK_LEN) != EOK) {
145 LNN_LOGE(LNN_STATE, "copy irk id fail");
146 isIrkGenerated = false;
147 (void)memset_s(locaIrk, LFINDER_IRK_LEN, 0, LFINDER_IRK_LEN);
148 return SOFTBUS_MEM_ERR;
149 }
150 char irkStr[LFINDER_IRK_STR_LEN] = {0};
151 if (ConvertBytesToHexString(irkStr, LFINDER_IRK_STR_LEN, irk, LFINDER_IRK_LEN) != SOFTBUS_OK) {
152 LNN_LOGW(LNN_STATE, "convert irk to string fail, just is dump, ignore this warning");
153 return SOFTBUS_OK;
154 }
155 char *anonyIrk = NULL;
156 Anonymize(irkStr, &anonyIrk);
157 LNN_LOGI(LNN_STATE, "get irk success:irk=%{public}s", AnonymizeWrapper(anonyIrk));
158 AnonymizeFree(anonyIrk);
159 (void)memset_s(irkStr, LFINDER_IRK_STR_LEN, 0, LFINDER_IRK_STR_LEN);
160 return SOFTBUS_OK;
161 }