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 "auth_connection.h"
17
18 #include <securec.h>
19 #include "auth_common.h"
20 #include "auth_p2p.h"
21 #include "bus_center_manager.h"
22 #include "device_auth.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_conn_interface.h"
25 #include "softbus_errcode.h"
26 #include "softbus_json_utils.h"
27 #include "softbus_log.h"
28
AuthSocketSendData(AuthManager * auth,const AuthDataHead * head,const uint8_t * data,uint32_t len)29 int32_t __attribute__ ((weak)) AuthSocketSendData(AuthManager *auth,
30 const AuthDataHead *head, const uint8_t *data, uint32_t len)
31 {
32 (void)auth;
33 (void)head;
34 (void)data;
35 (void)len;
36 return SOFTBUS_ERR;
37 }
38
39 typedef struct {
40 AuthSideFlag side;
41 int64_t seq;
42 uint32_t connectionId;
43 int32_t connModule;
44 } PostDataInfo;
45
PostDataByConn(const PostDataInfo * info,char * buf,uint32_t postDataLen)46 static int32_t PostDataByConn(const PostDataInfo *info, char *buf, uint32_t postDataLen)
47 {
48 int64_t seq = 0;
49 if (info->connModule == MODULE_DEVICE_AUTH ||
50 info->connModule == MODULE_TRUST_ENGINE || info->connModule == MODULE_AUTH_SDK) {
51 seq = info->seq;
52 } else if (info->connModule == MODULE_AUTH_CONNECTION) {
53 seq = GetSeq(info->side);
54 } else {
55 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "invalid conn module.");
56 SoftBusFree(buf);
57 return SOFTBUS_INVALID_PARAM;
58 }
59
60 ConnPostData postParam;
61 postParam.module = info->connModule;
62 postParam.seq = seq;
63 postParam.flag = CONN_HIGH;
64 postParam.pid = 0;
65 postParam.buf = buf;
66 postParam.len = postDataLen + ConnGetHeadSize();
67
68 return ConnPostBytes(info->connectionId, &postParam);
69 }
70
SetBufData(char * buf,const AuthManager * auth,const AuthDataHead * head,const uint8_t * data,uint32_t len)71 static int32_t SetBufData(char *buf, const AuthManager *auth, const AuthDataHead *head,
72 const uint8_t *data, uint32_t len)
73 {
74 buf += ConnGetHeadSize();
75 if (!IsWiFiLink(auth)) {
76 *(int32_t *)buf = head->dataType;
77 buf += sizeof(int32_t);
78 *(int32_t *)buf = head->module;
79 buf += sizeof(int32_t);
80 *(int64_t *)buf = head->seq;
81 buf += sizeof(int64_t);
82 *(int32_t *)buf = head->flag;
83 buf += sizeof(int32_t);
84 *(int32_t *)buf = len;
85 buf += sizeof(int32_t);
86 }
87 if (memcpy_s(buf, len, data, len) != EOK) {
88 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "memcpy_s failed");
89 return SOFTBUS_ERR;
90 }
91 return SOFTBUS_OK;
92 }
93
HandlePeerSyncDeviceInfo(AuthManager * auth,const AuthDataHead * head)94 static void HandlePeerSyncDeviceInfo(AuthManager *auth, const AuthDataHead *head)
95 {
96 if (head->dataType == DATA_TYPE_SYNC) {
97 auth->status = SYNC_FINISH;
98 }
99 if (head->dataType == DATA_TYPE_SYNC && auth->encryptDevData != NULL) {
100 AuthHandlePeerSyncDeviceInfo(auth, auth->encryptDevData, auth->encryptLen);
101 SoftBusFree(auth->encryptDevData);
102 auth->encryptDevData = NULL;
103 }
104 }
105
AuthPostData(const AuthDataHead * head,const uint8_t * data,uint32_t len)106 int32_t AuthPostData(const AuthDataHead *head, const uint8_t *data, uint32_t len)
107 {
108 if (head == NULL || data == NULL) {
109 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "invalid parameter");
110 return SOFTBUS_INVALID_PARAM;
111 }
112 AuthManager *auth = NULL;
113 auth = AuthGetManagerByAuthId(head->authId);
114 if (auth == NULL) {
115 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "no match auth found(%llu), AuthPostData failed",
116 head->authId);
117 return SOFTBUS_ERR;
118 }
119
120 if (IsWiFiLink(auth)) {
121 if (AuthSocketSendData(auth, head, data, len) != SOFTBUS_OK) {
122 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "AuthSocketSendData failed");
123 return SOFTBUS_ERR;
124 }
125 } else {
126 PostDataInfo info;
127 uint32_t postDataLen;
128 info.side = auth->side;
129 info.connectionId = auth->connectionId;
130 postDataLen = sizeof(AuthDataInfo) + len;
131 info.connModule = MODULE_DEVICE_AUTH;
132 info.seq = GetSeq(auth->side);
133 char *connPostData = NULL;
134 char *buf = (char *)SoftBusMalloc(ConnGetHeadSize() + postDataLen);
135 if (buf == NULL) {
136 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "SoftBusMalloc failed");
137 return SOFTBUS_ERR;
138 }
139 connPostData = buf;
140 if (SetBufData(buf, auth, head, data, len) != SOFTBUS_OK) {
141 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "SetBufData failed");
142 SoftBusFree(connPostData);
143 return SOFTBUS_ERR;
144 }
145 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO,
146 "auth start post data, authId is %lld, connectionId is %u, moduleId is %d, seq is %lld",
147 auth->authId, info.connectionId, info.connModule, info.seq);
148 if (PostDataByConn(&info, connPostData, postDataLen) != SOFTBUS_OK) {
149 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "PostDataByConn failed");
150 return SOFTBUS_ERR;
151 }
152 }
153 HandlePeerSyncDeviceInfo(auth, head);
154 return SOFTBUS_OK;
155 }
156
AuthPackDeviceInfo(const AuthManager * auth)157 static cJSON *AuthPackDeviceInfo(const AuthManager *auth)
158 {
159 if (auth == NULL) {
160 return NULL;
161 }
162 cJSON *msg = cJSON_CreateObject();
163 if (msg == NULL) {
164 return NULL;
165 }
166 char uuid[UUID_BUF_LEN] = {0};
167 if (LnnGetLocalStrInfo(STRING_KEY_UUID, uuid, UUID_BUF_LEN) != SOFTBUS_OK) {
168 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth get uuid failed!");
169 cJSON_Delete(msg);
170 return NULL;
171 }
172 char udid[UDID_BUF_LEN] = {0};
173 if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, udid, UDID_BUF_LEN) != SOFTBUS_OK) {
174 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth get device udid failed!");
175 cJSON_Delete(msg);
176 return NULL;
177 }
178 if (IsWiFiLink(auth) && auth->side == CLIENT_SIDE_FLAG) {
179 if (AddStringToJsonObject(msg, CMD_TAG, CMD_GET_AUTH_INFO) == false) {
180 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "AddStringToJsonObject failed!");
181 cJSON_Delete(msg);
182 return NULL;
183 }
184 } else {
185 if (AddStringToJsonObject(msg, CMD_TAG, CMD_RET_AUTH_INFO) == false) {
186 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "AddStringToJsonObject failed!");
187 cJSON_Delete(msg);
188 return NULL;
189 }
190 }
191 if (!AddStringToJsonObject(msg, DATA_TAG, uuid) ||
192 !AddStringToJsonObject(msg, TE_DEVICE_ID_TAG, udid) ||
193 !AddNumberToJsonObject(msg, DATA_BUF_SIZE_TAG, PACKET_SIZE) ||
194 !AddNumberToJsonObject(msg, SOFTBUS_VERSION_INFO, auth->softbusVersion)) {
195 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "AddStringToJsonObject Fail.");
196 cJSON_Delete(msg);
197 return NULL;
198 }
199 return msg;
200 }
201
AuthSyncDeviceUuid(AuthManager * auth)202 int32_t AuthSyncDeviceUuid(AuthManager *auth)
203 {
204 if (auth == NULL) {
205 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "invalid parameter");
206 return SOFTBUS_INVALID_PARAM;
207 }
208 AuthDataHead head;
209 (void)memset_s(&head, sizeof(head), 0, sizeof(head));
210 cJSON *obj = AuthPackDeviceInfo(auth);
211 if (obj == NULL) {
212 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "AuthPackDeviceInfo failed");
213 return SOFTBUS_ERR;
214 }
215 char *msgStr = cJSON_PrintUnformatted(obj);
216 if (msgStr == NULL) {
217 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "cJSON_PrintUnformatted failed");
218 cJSON_Delete(obj);
219 return SOFTBUS_ERR;
220 }
221 auth->status = IN_AUTH_PROGRESS;
222 if (IsWiFiLink(auth)) {
223 /* WIFI_WLAN or ETH */
224 head.module = MODULE_TRUST_ENGINE;
225 } else {
226 /* WIFI_P2P or BR or BLE */
227 head.dataType = DATA_TYPE_DEVICE_ID;
228 head.module = NONE;
229 }
230 head.authId = auth->authId;
231 head.seq = auth->authId;
232 head.flag = auth->side;
233 if (AuthPostData(&head, (uint8_t *)msgStr, strlen(msgStr) + 1) != SOFTBUS_OK) {
234 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "AuthPostData failed");
235 cJSON_free(msgStr);
236 cJSON_Delete(obj);
237 return SOFTBUS_ERR;
238 }
239 cJSON_free(msgStr);
240 cJSON_Delete(obj);
241 return SOFTBUS_OK;
242 }
243
UnpackDeviceId(cJSON * msg,AuthManager * auth)244 static int32_t UnpackDeviceId(cJSON *msg, AuthManager *auth)
245 {
246 if (!GetJsonObjectStringItem(msg, DATA_TAG, auth->peerUuid, UUID_BUF_LEN)) {
247 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth get peer uuid failed");
248 return SOFTBUS_ERR;
249 }
250 if (!GetJsonObjectStringItem(msg, TE_DEVICE_ID_TAG, auth->peerUdid, UDID_BUF_LEN)) {
251 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth get peer udid failed");
252 return SOFTBUS_ERR;
253 }
254 return SOFTBUS_OK;
255 }
256
AuthUnpackDeviceInfo(AuthManager * auth,uint8_t * data)257 int32_t AuthUnpackDeviceInfo(AuthManager *auth, uint8_t *data)
258 {
259 if (auth == NULL || data == NULL) {
260 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "invalid parameter");
261 return SOFTBUS_INVALID_PARAM;
262 }
263 cJSON *msg = cJSON_Parse((char*)data);
264 if (msg == NULL) {
265 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "json parse failed.");
266 return SOFTBUS_ERR;
267 }
268 char cmd[CMD_TAG_LEN] = {0};
269 if (!GetJsonObjectStringItem(msg, CMD_TAG, cmd, CMD_TAG_LEN)) {
270 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth get cmd tag failed");
271 cJSON_Delete(msg);
272 return SOFTBUS_ERR;
273 }
274 if (IsWiFiLink(auth) && auth->side == SERVER_SIDE_FLAG) {
275 if (strncmp(cmd, CMD_GET_AUTH_INFO, strlen(CMD_GET_AUTH_INFO)) != 0) {
276 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth cmd tag error");
277 cJSON_Delete(msg);
278 return SOFTBUS_ERR;
279 }
280 } else {
281 if (strncmp(cmd, CMD_RET_AUTH_INFO, strlen(CMD_RET_AUTH_INFO)) != 0) {
282 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth cmd tag error");
283 cJSON_Delete(msg);
284 return SOFTBUS_ERR;
285 }
286 }
287 if (UnpackDeviceId(msg, auth) != SOFTBUS_OK) {
288 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "UnpackDeviceId failed");
289 cJSON_Delete(msg);
290 return SOFTBUS_ERR;
291 }
292 int32_t packetSize;
293 if (!GetJsonObjectNumberItem(msg, DATA_BUF_SIZE_TAG, &packetSize)) {
294 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth get packet size failed");
295 cJSON_Delete(msg);
296 return SOFTBUS_ERR;
297 }
298
299 int32_t peerVersion;
300 if (!GetJsonObjectNumberItem(msg, SOFTBUS_VERSION_INFO, &peerVersion)) {
301 auth->peerVersion = SOFT_BUS_OLD_V2;
302 } else {
303 auth->peerVersion = (SoftBusVersion)peerVersion;
304 }
305 cJSON_Delete(msg);
306 return SOFTBUS_OK;
307 }
308
AuthGenDeviceLevelParam(const AuthManager * auth,bool isClient)309 char *AuthGenDeviceLevelParam(const AuthManager *auth, bool isClient)
310 {
311 if (auth == NULL) {
312 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "invalid parameter");
313 return NULL;
314 }
315 cJSON *msg = cJSON_CreateObject();
316 if (msg == NULL) {
317 return NULL;
318 }
319 if (!AddStringToJsonObject(msg, FIELD_PEER_CONN_DEVICE_ID, auth->peerUdid) ||
320 !AddStringToJsonObject(msg, FIELD_SERVICE_PKG_NAME, AUTH_APPID) ||
321 cJSON_AddBoolToObject(msg, FIELD_IS_CLIENT, isClient) == NULL ||
322 !AddNumberToJsonObject(msg, FIELD_KEY_LENGTH, SESSION_KEY_LENGTH)) {
323 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "AddStringToJsonObject Fail.");
324 cJSON_Delete(msg);
325 return NULL;
326 }
327 #ifdef AUTH_ACCOUNT
328 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "in account auth mode");
329 if (!AddStringToJsonObject(msg, FIELD_UID_HASH, auth->peerUid)) {
330 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "AddStringToJsonObject Fail.");
331 cJSON_Delete(msg);
332 return NULL;
333 }
334 #endif
335 char *data = cJSON_PrintUnformatted(msg);
336 if (data == NULL) {
337 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "cJSON_PrintUnformatted failed");
338 }
339 cJSON_Delete(msg);
340 return data;
341 }
342
AuthSendCloseAck(uint32_t connectionId,AuthSideFlag side,int64_t authId)343 void AuthSendCloseAck(uint32_t connectionId, AuthSideFlag side, int64_t authId)
344 {
345 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "auth finished, send close ack");
346 const char *closeData = "close ack";
347 uint32_t closeDataLen = strlen(closeData) + 1;
348
349 PostDataInfo info;
350 uint32_t postDataLen = sizeof(AuthDataInfo) + closeDataLen;
351 char *connPostData = NULL;
352 char *buf = (char *)SoftBusMalloc(ConnGetHeadSize() + postDataLen);
353 if (buf == NULL) {
354 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "SoftBusMalloc failed");
355 return;
356 }
357 connPostData = buf;
358 buf += ConnGetHeadSize();
359 *(int32_t *)buf = DATA_TYPE_CLOSE_ACK;
360 buf += sizeof(int32_t);
361 *(int32_t *)buf = NONE;
362 buf += sizeof(int32_t);
363 *(int64_t *)buf = authId;
364 buf += sizeof(int64_t);
365 *(int32_t *)buf = 0;
366 buf += sizeof(int32_t);
367 *(int32_t *)buf = closeDataLen;
368 buf += sizeof(int32_t);
369 if (memcpy_s(buf, closeDataLen, closeData, closeDataLen) != EOK) {
370 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "memcpy_s failed");
371 SoftBusFree(connPostData);
372 return;
373 }
374 info.side = side;
375 info.seq = GetSeq(side);
376 info.connectionId = connectionId;
377 info.connModule = MODULE_DEVICE_AUTH;
378 if (PostDataByConn(&info, connPostData, postDataLen) != SOFTBUS_OK) {
379 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "PostDataByConn failed");
380 return;
381 }
382 }
383
AuthTryCloseConnection(uint32_t connectionId)384 void AuthTryCloseConnection(uint32_t connectionId)
385 {
386 (void)ConnDisconnectDevice(connectionId);
387 }
388
AuthOnTransmit(int64_t authId,const uint8_t * data,uint32_t len)389 bool AuthOnTransmit(int64_t authId, const uint8_t *data, uint32_t len)
390 {
391 AuthManager *auth = NULL;
392 AuthDataHead head;
393
394 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "AuthOnTransmit authId=%lld, len=%u", authId, len);
395 (void)memset_s(&head, sizeof(head), 0, sizeof(head));
396 auth = AuthGetManagerByAuthId(authId);
397 if (auth == NULL) {
398 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "no match auth(%llu) found", authId);
399 return false;
400 }
401 head.dataType = DATA_TYPE_AUTH;
402 head.module = AUTH_SDK;
403 head.authId = auth->authId;
404 head.flag = auth->side;
405 head.seq = auth->authId;
406 return AuthPostData(&head, data, len) == SOFTBUS_OK;
407 }
408