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 "trans_auth_manager.h"
17
18 #include "auth_channel.h"
19 #include "bus_center_manager.h"
20 #include "common_list.h"
21 #include "lnn_connection_addr_utils.h"
22 #include "lnn_net_builder.h"
23 #include "securec.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_adapter_thread.h"
26 #include "softbus_def.h"
27 #include "softbus_errcode.h"
28 #include "softbus_feature_config.h"
29 #include "softbus_log.h"
30 #include "softbus_utils.h"
31 #include "trans_auth_message.h"
32 #include "trans_channel_limit.h"
33 #include "trans_session_manager.h"
34
35 #define AUTH_CHANNEL_REQ 0
36 #define AUTH_CHANNEL_REPLY 1
37
38 #define AUTH_GROUP_ID "auth group id"
39 #define AUTH_SESSION_KEY "auth session key"
40
41 typedef struct {
42 ListNode node;
43 AppInfo appInfo;
44 int32_t authId;
45 ConnectOption connOpt;
46 bool isClient;
47 } AuthChannelInfo;
48
49 typedef struct {
50 int32_t channelType;
51 int32_t businessType;
52 ConfigType configType;
53 } ConfigTypeMap;
54
55 static SoftBusList *g_authChannelList = NULL;
56 static int32_t g_channelId = 0;
57 static IServerChannelCallBack *g_cb = NULL;
58
59 static void TransPostAuthChannelErrMsg(int32_t authId, int32_t errcode, const char *errMsg);
60 static int32_t TransPostAuthChannelMsg(const AppInfo *appInfo, int32_t authId, int32_t flag);
61 static AuthChannelInfo *CreateAuthChannelInfo(const char *sessionName, bool isClient);
62 static int32_t AddAuthChannelInfo(AuthChannelInfo *info);
63 static void DelAuthChannelInfoByChanId(int32_t channelId);
64 static void DelAuthChannelInfoByAuthId(int32_t authId);
65
GenerateAuthChannelId(void)66 static int32_t GenerateAuthChannelId(void)
67 {
68 g_channelId++;
69 return g_channelId;
70 }
71
GetAuthChannelInfoByChanId(int32_t channelId,AuthChannelInfo * dstInfo)72 static int32_t GetAuthChannelInfoByChanId(int32_t channelId, AuthChannelInfo *dstInfo)
73 {
74 if (dstInfo == NULL || g_authChannelList == NULL) {
75 return SOFTBUS_INVALID_PARAM;
76 }
77
78 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
79 return SOFTBUS_LOCK_ERR;
80 }
81 AuthChannelInfo *info = NULL;
82 LIST_FOR_EACH_ENTRY(info, &g_authChannelList->list, AuthChannelInfo, node) {
83 if (info->appInfo.myData.channelId == channelId) {
84 if (memcpy_s(dstInfo, sizeof(AuthChannelInfo), info, sizeof(AuthChannelInfo)) != EOK) {
85 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
86 return SOFTBUS_MEM_ERR;
87 }
88 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
89 return SOFTBUS_OK;
90 }
91 }
92 SoftBusMutexUnlock(&g_authChannelList->lock);
93 return SOFTBUS_ERR;
94 }
95
GetAuthIdByChannelId(int32_t channelId)96 static int32_t GetAuthIdByChannelId(int32_t channelId)
97 {
98 if (g_authChannelList == NULL) {
99 return SOFTBUS_ERR;
100 }
101
102 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
103 return SOFTBUS_LOCK_ERR;
104 }
105 int32_t authId = -1;
106 AuthChannelInfo *info = NULL;
107 LIST_FOR_EACH_ENTRY(info, &g_authChannelList->list, AuthChannelInfo, node) {
108 if (info->appInfo.myData.channelId == channelId) {
109 authId = info->authId;
110 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
111 return authId;
112 }
113 }
114 SoftBusMutexUnlock(&g_authChannelList->lock);
115 return authId;
116 }
117
GetChannelInfoByAuthId(int32_t authId,AuthChannelInfo * dstInfo)118 static int32_t GetChannelInfoByAuthId(int32_t authId, AuthChannelInfo *dstInfo)
119 {
120 if (dstInfo == NULL || g_authChannelList == NULL) {
121 return SOFTBUS_INVALID_PARAM;
122 }
123
124 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
125 return SOFTBUS_LOCK_ERR;
126 }
127 AuthChannelInfo *info = NULL;
128 LIST_FOR_EACH_ENTRY(info, &g_authChannelList->list, AuthChannelInfo, node) {
129 if (info->authId == authId) {
130 if (memcpy_s(dstInfo, sizeof(AuthChannelInfo), info, sizeof(AuthChannelInfo)) != EOK) {
131 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
132 return SOFTBUS_MEM_ERR;
133 }
134 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
135 return SOFTBUS_OK;
136 }
137 }
138 SoftBusMutexUnlock(&g_authChannelList->lock);
139 return SOFTBUS_ERR;
140 }
141
NotifyOpenAuthChannelSuccess(const AppInfo * appInfo,bool isServer)142 NO_SANITIZE("cfi") static int32_t NotifyOpenAuthChannelSuccess(const AppInfo *appInfo, bool isServer)
143 {
144 ChannelInfo channelInfo = {0};
145 channelInfo.channelType = CHANNEL_TYPE_AUTH;
146 channelInfo.isServer = isServer;
147 channelInfo.isEnabled = true;
148 channelInfo.channelId = appInfo->myData.channelId;
149 channelInfo.peerDeviceId = (char *)appInfo->peerData.deviceId;
150 channelInfo.peerSessionName = (char *)appInfo->peerData.sessionName;
151 channelInfo.businessType = BUSINESS_TYPE_NOT_CARE;
152 channelInfo.groupId = (char *)AUTH_GROUP_ID;
153 channelInfo.isEncrypt = false;
154 channelInfo.sessionKey = (char *)AUTH_SESSION_KEY;
155 channelInfo.keyLen = strlen(channelInfo.sessionKey) + 1;
156 channelInfo.autoCloseTime = appInfo->autoCloseTime;
157 channelInfo.reqId = (char*)appInfo->reqId;
158 channelInfo.dataConfig = appInfo->myData.dataConfig;
159 return g_cb->OnChannelOpened(appInfo->myData.pkgName, appInfo->myData.pid,
160 appInfo->myData.sessionName, &channelInfo);
161 }
162
NotifyOpenAuthChannelFailed(const char * pkgName,int32_t pid,int32_t channelId,int32_t errCode)163 NO_SANITIZE("cfi") static int32_t NotifyOpenAuthChannelFailed(const char *pkgName, int32_t pid, int32_t channelId,
164 int32_t errCode)
165 {
166 return g_cb->OnChannelOpenFailed(pkgName, pid, channelId, CHANNEL_TYPE_AUTH, errCode);
167 }
168
NofifyCloseAuthChannel(const char * pkgName,int32_t pid,int32_t channelId)169 NO_SANITIZE("cfi") static int32_t NofifyCloseAuthChannel(const char *pkgName, int32_t pid, int32_t channelId)
170 {
171 return g_cb->OnChannelClosed(pkgName, pid, channelId, CHANNEL_TYPE_AUTH);
172 }
173
AuthGetUidAndPidBySessionName(const char * sessionName,int32_t * uid,int32_t * pid)174 NO_SANITIZE("cfi") static int32_t AuthGetUidAndPidBySessionName(const char *sessionName, int32_t *uid, int32_t *pid)
175 {
176 return g_cb->GetUidAndPidBySessionName(sessionName, uid, pid);
177 }
178
NotifyOnDataReceived(int32_t authId,const void * data,uint32_t len)179 NO_SANITIZE("cfi") static int32_t NotifyOnDataReceived(int32_t authId, const void *data, uint32_t len)
180 {
181 AuthChannelInfo channel;
182 if (GetChannelInfoByAuthId(authId, &channel) != SOFTBUS_OK) {
183 return SOFTBUS_ERR;
184 }
185 TransReceiveData receiveData;
186 receiveData.data = (void*)data;
187 receiveData.dataLen = len;
188 receiveData.dataType = TRANS_SESSION_BYTES;
189
190 return g_cb->OnDataReceived(channel.appInfo.myData.pkgName, channel.appInfo.myData.pid,
191 channel.appInfo.myData.channelId, CHANNEL_TYPE_AUTH, &receiveData);
192 }
193
CopyPeerAppInfo(AppInfo * recvAppInfo,AppInfo * channelAppInfo)194 static int32_t CopyPeerAppInfo(AppInfo *recvAppInfo, AppInfo *channelAppInfo)
195 {
196 if (recvAppInfo == NULL || channelAppInfo == NULL) {
197 return SOFTBUS_INVALID_PARAM;
198 }
199 if (memcpy_s(channelAppInfo->peerData.deviceId, DEVICE_ID_SIZE_MAX,
200 recvAppInfo->peerData.deviceId, DEVICE_ID_SIZE_MAX) != EOK ||
201 memcpy_s(recvAppInfo->myData.deviceId, DEVICE_ID_SIZE_MAX,
202 channelAppInfo->myData.deviceId, DEVICE_ID_SIZE_MAX) != EOK ||
203 memcpy_s(channelAppInfo->peerData.pkgName, PKG_NAME_SIZE_MAX,
204 recvAppInfo->peerData.pkgName, PKG_NAME_SIZE_MAX) != EOK ||
205 memcpy_s(recvAppInfo->myData.pkgName, PKG_NAME_SIZE_MAX,
206 channelAppInfo->myData.pkgName, PKG_NAME_SIZE_MAX) != EOK ||
207 memcpy_s(channelAppInfo->peerData.sessionName, SESSION_NAME_SIZE_MAX,
208 recvAppInfo->peerData.sessionName, SESSION_NAME_SIZE_MAX) != EOK) {
209 return SOFTBUS_MEM_ERR;
210 }
211 return SOFTBUS_OK;
212 }
213
OnRequsetUpdateAuthChannel(int32_t authId,AppInfo * appInfo)214 static int32_t OnRequsetUpdateAuthChannel(int32_t authId, AppInfo *appInfo)
215 {
216 if (appInfo == NULL) {
217 return SOFTBUS_INVALID_PARAM;
218 }
219 AuthChannelInfo *item = NULL;
220 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
221 return SOFTBUS_LOCK_ERR;
222 }
223 bool exists = false;
224 LIST_FOR_EACH_ENTRY(item, &g_authChannelList->list, AuthChannelInfo, node) {
225 if (item->authId == authId) {
226 exists = true;
227 break;
228 }
229 }
230 if (!exists) {
231 item = CreateAuthChannelInfo(appInfo->myData.sessionName, false);
232 if (item == NULL) {
233 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CreateAuthChannelInfo failed");
234 SoftBusMutexUnlock(&g_authChannelList->lock);
235 return SOFTBUS_ERR;
236 }
237 item->authId = authId;
238 appInfo->myData.channelId = item->appInfo.myData.channelId;
239 appInfo->myData.dataConfig = item->appInfo.myData.dataConfig;
240 if (AddAuthChannelInfo(item) != SOFTBUS_OK) {
241 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "AddAuthChannelInfo failed");
242 SoftBusFree(item);
243 SoftBusMutexUnlock(&g_authChannelList->lock);
244 return SOFTBUS_ERR;
245 }
246 }
247 if (CopyPeerAppInfo(appInfo, &item->appInfo) != SOFTBUS_OK) {
248 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CopyPeerAppInfo failed");
249 SoftBusFree(item);
250 SoftBusMutexUnlock(&g_authChannelList->lock);
251 return SOFTBUS_MEM_ERR;
252 }
253 SoftBusMutexUnlock(&g_authChannelList->lock);
254 return SOFTBUS_OK;
255 }
256
257 static const ConfigTypeMap g_configTypeMap[] = {
258 {CHANNEL_TYPE_AUTH, BUSINESS_TYPE_BYTE, SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH},
259 {CHANNEL_TYPE_AUTH, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH},
260 };
261
FindConfigType(int32_t channelType,int32_t businessType)262 static int32_t FindConfigType(int32_t channelType, int32_t businessType)
263 {
264 for (uint32_t i = 0; i < sizeof(g_configTypeMap) / sizeof(ConfigTypeMap); i++) {
265 if ((g_configTypeMap[i].channelType == channelType) && (g_configTypeMap[i].businessType == businessType)) {
266 return g_configTypeMap[i].configType;
267 }
268 }
269 return SOFTBUS_CONFIG_TYPE_MAX;
270 }
271
TransGetLocalConfig(int32_t channelType,int32_t businessType,uint32_t * len)272 static int TransGetLocalConfig(int32_t channelType, int32_t businessType, uint32_t *len)
273 {
274 ConfigType configType = (ConfigType)FindConfigType(channelType, businessType);
275 if (configType == SOFTBUS_CONFIG_TYPE_MAX) {
276 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Invalid channelType[%d] businessType[%d]",
277 channelType, businessType);
278 return SOFTBUS_INVALID_PARAM;
279 }
280 uint32_t maxLen;
281 if (SoftbusGetConfig(configType, (unsigned char *)&maxLen, sizeof(maxLen)) != SOFTBUS_OK) {
282 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get fail configType[%d]", configType);
283 return SOFTBUS_GET_CONFIG_VAL_ERR;
284 }
285 *len = maxLen;
286 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "get appinfo local config[%d]", *len);
287 return SOFTBUS_OK;
288 }
289
TransAuthFillDataConfig(AppInfo * appInfo)290 static int32_t TransAuthFillDataConfig(AppInfo *appInfo)
291 {
292 if (appInfo == NULL) {
293 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "appInfo is null");
294 return SOFTBUS_ERR;
295 }
296 appInfo->businessType = BUSINESS_TYPE_BYTE;
297 if (appInfo->peerData.dataConfig != 0) {
298 uint32_t localDataConfig = 0;
299 if (TransGetLocalConfig(CHANNEL_TYPE_AUTH, appInfo->businessType, &localDataConfig) != SOFTBUS_OK) {
300 return SOFTBUS_ERR;
301 }
302 appInfo->myData.dataConfig = MIN(localDataConfig, appInfo->peerData.dataConfig);
303 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "fill dataConfig[%u] succ", appInfo->myData.dataConfig);
304 return SOFTBUS_OK;
305 }
306 ConfigType configType = appInfo->businessType == BUSINESS_TYPE_BYTE ?
307 SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH : SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH;
308 if (SoftbusGetConfig(configType, (unsigned char *)&appInfo->myData.dataConfig,
309 sizeof(appInfo->myData.dataConfig)) != SOFTBUS_OK) {
310 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get config failed, configType[%d]", configType);
311 return SOFTBUS_ERR;
312 }
313 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "fill data config value[%d]", appInfo->myData.dataConfig);
314 return SOFTBUS_OK;
315 }
316
OnRecvAuthChannelRequest(int32_t authId,const char * data,int32_t len)317 static void OnRecvAuthChannelRequest(int32_t authId, const char *data, int32_t len)
318 {
319 if (data == NULL || len <= 0) {
320 return;
321 }
322
323 AppInfo appInfo;
324 int32_t ret = TransAuthChannelMsgUnpack(data, &appInfo, len);
325 if (ret != SOFTBUS_OK) {
326 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "unpackRequest failed");
327 TransPostAuthChannelErrMsg(authId, ret, "unpackRequest");
328 goto EXIT_ERR;
329 }
330 if (!CheckSessionNameValidOnAuthChannel(appInfo.myData.sessionName)) {
331 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "check auth channel pkginfo invalid.");
332 TransPostAuthChannelErrMsg(authId, ret, "check msginfo failed");
333 goto EXIT_ERR;
334 }
335 ret = AuthGetUidAndPidBySessionName(appInfo.myData.sessionName, &appInfo.myData.uid, &appInfo.myData.pid);
336 if (ret != SOFTBUS_OK) {
337 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "AuthGetUidAndPidBySessionName failed");
338 goto EXIT_ERR;
339 }
340 ret = TransAuthFillDataConfig(&appInfo);
341 if (ret != SOFTBUS_OK) {
342 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransAuthFillDataConfig failed");
343 goto EXIT_ERR;
344 }
345 ret = OnRequsetUpdateAuthChannel(authId, &appInfo);
346 if (ret != SOFTBUS_OK) {
347 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "update auth channel failed");
348 TransPostAuthChannelErrMsg(authId, ret, "unpackRequest");
349 goto EXIT_ERR;
350 }
351 ret = NotifyOpenAuthChannelSuccess(&appInfo, true);
352 if (ret != SOFTBUS_OK) {
353 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "NotifyOpenAuthChannelSuccess failed");
354 TransPostAuthChannelErrMsg(authId, ret, "NotifyOpenAuthChannelSuccess failed");
355 goto EXIT_ERR;
356 }
357 ret = TransPostAuthChannelMsg(&appInfo, authId, AUTH_CHANNEL_REPLY);
358 if (ret != SOFTBUS_OK) {
359 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "send reply failed");
360 TransPostAuthChannelErrMsg(authId, ret, "send reply failed");
361 goto EXIT_ERR;
362 }
363 return;
364 EXIT_ERR:
365 DelAuthChannelInfoByAuthId(authId);
366 AuthCloseChannel(authId);
367 }
368
TransAuthProcessDataConfig(AppInfo * appInfo)369 static int32_t TransAuthProcessDataConfig(AppInfo *appInfo)
370 {
371 if (appInfo == NULL) {
372 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "appInfo is null");
373 return SOFTBUS_ERR;
374 }
375 if (appInfo->businessType != BUSINESS_TYPE_MESSAGE && appInfo->businessType != BUSINESS_TYPE_BYTE) {
376 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "invalid businessType[%d]", appInfo->businessType);
377 return SOFTBUS_OK;
378 }
379 if (appInfo->peerData.dataConfig != 0) {
380 appInfo->myData.dataConfig = MIN(appInfo->myData.dataConfig, appInfo->peerData.dataConfig);
381 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "process dataConfig[%u] succ", appInfo->myData.dataConfig);
382 return SOFTBUS_OK;
383 }
384 ConfigType configType = appInfo->businessType == BUSINESS_TYPE_BYTE ?
385 SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH : SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH;
386 if (SoftbusGetConfig(configType, (unsigned char *)&appInfo->myData.dataConfig,
387 sizeof(appInfo->myData.dataConfig)) != SOFTBUS_OK) {
388 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get config failed, configType[%d]", configType);
389 return SOFTBUS_ERR;
390 }
391 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "process data config value[%d]", appInfo->myData.dataConfig);
392 return SOFTBUS_OK;
393 }
394
OnRecvAuthChannelReply(int32_t authId,const char * data,int32_t len)395 static void OnRecvAuthChannelReply(int32_t authId, const char *data, int32_t len)
396 {
397 if (data == NULL || len <= 0) {
398 return;
399 }
400 AuthChannelInfo info;
401 if (GetChannelInfoByAuthId(authId, &info) != SOFTBUS_OK) {
402 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "can not find channel info by auth id");
403 return;
404 }
405 int32_t ret = TransAuthChannelMsgUnpack(data, &info.appInfo, len);
406 if (ret != SOFTBUS_OK) {
407 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "unpackReply failed");
408 goto EXIT_ERR;
409 }
410 ret = TransAuthProcessDataConfig(&info.appInfo);
411 if (ret != SOFTBUS_OK) {
412 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ProcessDataConfig failed");
413 goto EXIT_ERR;
414 }
415 ret = NotifyOpenAuthChannelSuccess(&info.appInfo, false);
416 if (ret != SOFTBUS_OK) {
417 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "NotifyOpenAuthChannelSuccess failed");
418 goto EXIT_ERR;
419 }
420 return;
421 EXIT_ERR:
422 AuthCloseChannel(authId);
423 DelAuthChannelInfoByChanId(info.appInfo.myData.channelId);
424 (void)NotifyOpenAuthChannelFailed(info.appInfo.myData.pkgName, info.appInfo.myData.pid,
425 info.appInfo.myData.channelId, ret);
426 }
427
OnAuthChannelDataRecv(int32_t authId,const AuthChannelData * data)428 static void OnAuthChannelDataRecv(int32_t authId, const AuthChannelData *data)
429 {
430 if (data == NULL || data->data == NULL || data->len < 1) {
431 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param.");
432 return;
433 }
434
435 if (data->flag == AUTH_CHANNEL_REQ) {
436 OnRecvAuthChannelRequest(authId, (const char *)data->data, data->len);
437 } else if (data->flag == AUTH_CHANNEL_REPLY) {
438 OnRecvAuthChannelReply(authId, (const char *)data->data, data->len);
439 } else {
440 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "auth channel flags err, authId=%d", authId);
441 }
442 }
443
OnAuthMsgDataRecv(int32_t authId,const AuthChannelData * data)444 static void OnAuthMsgDataRecv(int32_t authId, const AuthChannelData *data)
445 {
446 if (data == NULL || data->data == NULL) {
447 return;
448 }
449 if (NotifyOnDataReceived(authId, data->data, data->len) != SOFTBUS_OK) {
450 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "auth=%d recv MODULE_AUTH_MSG err", authId);
451 }
452 }
453
OnDisconnect(int32_t authId)454 static void OnDisconnect(int32_t authId)
455 {
456 AuthChannelInfo dstInfo;
457 if (GetChannelInfoByAuthId(authId, &dstInfo) != EOK) {
458 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "auth=%d channel already removed", authId);
459 return;
460 }
461 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "recv auth=%d channel disconnect event.", authId);
462 DelAuthChannelInfoByChanId(dstInfo.appInfo.myData.channelId);
463 (void)NofifyCloseAuthChannel(dstInfo.appInfo.myData.pkgName, dstInfo.appInfo.myData.pid,
464 dstInfo.appInfo.myData.channelId);
465 }
466
GetAppInfo(const char * sessionName,int32_t channelId,AppInfo * appInfo,bool isClient)467 static int32_t GetAppInfo(const char *sessionName, int32_t channelId, AppInfo *appInfo, bool isClient)
468 {
469 if (sessionName == NULL || appInfo == NULL) {
470 return SOFTBUS_INVALID_PARAM;
471 }
472 appInfo->appType = APP_TYPE_NOT_CARE;
473 appInfo->businessType = BUSINESS_TYPE_BYTE;
474 appInfo->channelType = CHANNEL_TYPE_AUTH;
475 appInfo->myData.channelId = channelId;
476 appInfo->myData.apiVersion = API_V2;
477 appInfo->peerData.apiVersion = API_V2;
478 appInfo->autoCloseTime = 0;
479 if ((!IsNoPkgNameSession(sessionName)) || (isClient)) {
480 if (TransGetUidAndPid(sessionName, &appInfo->myData.uid, &appInfo->myData.pid) != SOFTBUS_OK) {
481 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransGetUidAndPid failed");
482 return SOFTBUS_ERR;
483 }
484 if (TransGetPkgNameBySessionName(sessionName, appInfo->myData.pkgName, PKG_NAME_SIZE_MAX) != SOFTBUS_OK) {
485 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransGetPkgNameBySessionName failed");
486 return SOFTBUS_ERR;
487 }
488 }
489 if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, appInfo->myData.deviceId,
490 sizeof(appInfo->myData.deviceId)) != SOFTBUS_OK) {
491 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "LnnGetLocalStrInfo failed");
492 return SOFTBUS_ERR;
493 }
494 if (strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), sessionName) != EOK) {
495 return SOFTBUS_ERR;
496 }
497 appInfo->peerData.apiVersion = API_V2;
498 if (strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), sessionName) != 0) {
499 return SOFTBUS_ERR;
500 }
501 if (TransGetLocalConfig(appInfo->channelType, appInfo->businessType, &appInfo->myData.dataConfig) != SOFTBUS_OK) {
502 return SOFTBUS_ERR;
503 }
504 return SOFTBUS_OK;
505 }
506
AddAuthChannelInfo(AuthChannelInfo * info)507 static int32_t AddAuthChannelInfo(AuthChannelInfo *info)
508 {
509 if (g_authChannelList == NULL || info == NULL) {
510 return SOFTBUS_INVALID_PARAM;
511 }
512 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
513 return SOFTBUS_LOCK_ERR;
514 }
515 AuthChannelInfo *item = NULL;
516 LIST_FOR_EACH_ENTRY(item, &g_authChannelList->list, AuthChannelInfo, node) {
517 if (item->appInfo.myData.channelId == info->appInfo.myData.channelId) {
518 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
519 return SOFTBUS_ERR;
520 }
521 }
522 ListAdd(&g_authChannelList->list, &info->node);
523 g_authChannelList->cnt++;
524 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
525 return SOFTBUS_OK;
526 }
527
DelAuthChannelInfoByChanId(int32_t channelId)528 static void DelAuthChannelInfoByChanId(int32_t channelId)
529 {
530 if (g_authChannelList == NULL) {
531 return;
532 }
533 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
534 return;
535 }
536 AuthChannelInfo *item = NULL;
537 AuthChannelInfo *tmp = NULL;
538 LIST_FOR_EACH_ENTRY_SAFE(item, tmp, &g_authChannelList->list, AuthChannelInfo, node) {
539 if (item->appInfo.myData.channelId == channelId) {
540 ListDelete(&item->node);
541 SoftBusFree(item);
542 g_authChannelList->cnt--;
543 break;
544 }
545 }
546 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
547 }
548
DelAuthChannelInfoByAuthId(int32_t authId)549 static void DelAuthChannelInfoByAuthId(int32_t authId)
550 {
551 if (g_authChannelList == NULL) {
552 return;
553 }
554 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
555 return;
556 }
557 AuthChannelInfo *item = NULL;
558 AuthChannelInfo *tmp = NULL;
559 LIST_FOR_EACH_ENTRY_SAFE(item, tmp, &g_authChannelList->list, AuthChannelInfo, node) {
560 if (item->authId == authId) {
561 ListDelete(&item->node);
562 SoftBusFree(item);
563 g_authChannelList->cnt--;
564 break;
565 }
566 }
567 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
568 }
569
TransAuthGetNameByChanId(int32_t chanId,char * pkgName,char * sessionName,uint16_t pkgLen,uint16_t sessionLen)570 NO_SANITIZE("cfi") int32_t TransAuthGetNameByChanId(int32_t chanId, char *pkgName, char *sessionName,
571 uint16_t pkgLen, uint16_t sessionLen)
572 {
573 if (pkgName == NULL || sessionName == NULL) {
574 return SOFTBUS_INVALID_PARAM;
575 }
576
577 AuthChannelInfo info;
578 if (GetAuthChannelInfoByChanId(chanId, &info) != SOFTBUS_OK) {
579 return SOFTBUS_ERR;
580 }
581
582 if (memcpy_s(pkgName, pkgLen, info.appInfo.myData.pkgName, PKG_NAME_SIZE_MAX) != EOK ||
583 memcpy_s(sessionName, sessionLen, info.appInfo.myData.sessionName, SESSION_NAME_SIZE_MAX) != EOK) {
584 return SOFTBUS_MEM_ERR;
585 }
586 return SOFTBUS_OK;
587 }
588
TransAuthInit(IServerChannelCallBack * cb)589 NO_SANITIZE("cfi") int32_t TransAuthInit(IServerChannelCallBack *cb)
590 {
591 if (cb == NULL) {
592 return SOFTBUS_INVALID_PARAM;
593 }
594 AuthChannelListener channelListener = {
595 .onDataReceived = OnAuthChannelDataRecv,
596 .onDisconnected = OnDisconnect,
597 };
598 AuthChannelListener msgListener = {
599 .onDataReceived = OnAuthMsgDataRecv,
600 .onDisconnected = OnDisconnect,
601 };
602 if (RegAuthChannelListener(MODULE_AUTH_CHANNEL, &channelListener) != SOFTBUS_OK ||
603 RegAuthChannelListener(MODULE_AUTH_MSG, &msgListener) != SOFTBUS_OK) {
604 UnregAuthChannelListener(MODULE_AUTH_CHANNEL);
605 UnregAuthChannelListener(MODULE_AUTH_MSG);
606 return SOFTBUS_ERR;
607 }
608 if (g_authChannelList == NULL) {
609 g_authChannelList = CreateSoftBusList();
610 }
611 if (g_authChannelList == NULL) {
612 return SOFTBUS_INVALID_PARAM;
613 }
614 if (g_cb == NULL) {
615 g_cb = cb;
616 }
617 return SOFTBUS_OK;
618 }
619
TransAuthDeinit(void)620 NO_SANITIZE("cfi") void TransAuthDeinit(void)
621 {
622 UnregAuthChannelListener(MODULE_AUTH_CHANNEL);
623 UnregAuthChannelListener(MODULE_AUTH_MSG);
624 g_channelId = 1;
625 g_cb = NULL;
626 }
627
TransPostAuthChannelMsg(const AppInfo * appInfo,int32_t authId,int32_t flag)628 static int32_t TransPostAuthChannelMsg(const AppInfo *appInfo, int32_t authId, int32_t flag)
629 {
630 if (appInfo == NULL) {
631 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransPostAuthChannelMsg invalid param");
632 return SOFTBUS_INVALID_PARAM;
633 }
634 cJSON *msg = cJSON_CreateObject();
635 if (msg == NULL) {
636 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "json failed");
637 return SOFTBUS_MALLOC_ERR;
638 }
639 if (TransAuthChannelMsgPack(msg, appInfo) != SOFTBUS_OK) {
640 cJSON_Delete(msg);
641 return SOFTBUS_ERR;
642 }
643 char *data = cJSON_PrintUnformatted(msg);
644 if (data == NULL) {
645 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "json failed");
646 cJSON_Delete(msg);
647 return SOFTBUS_PARSE_JSON_ERR;
648 }
649 cJSON_Delete(msg);
650
651 AuthChannelData channelData = {
652 .module = MODULE_AUTH_CHANNEL,
653 .flag = flag,
654 .seq = 0,
655 .len = strlen(data) + 1,
656 .data = (const uint8_t *)data,
657 };
658 if (AuthPostChannelData(authId, &channelData) != SOFTBUS_OK) {
659 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "auth post channel data fail");
660 cJSON_free(data);
661 return SOFTBUS_ERR;
662 }
663 cJSON_free(data);
664 return SOFTBUS_OK;
665 }
666
TransPostAuthChannelErrMsg(int32_t authId,int32_t errcode,const char * errMsg)667 static void TransPostAuthChannelErrMsg(int32_t authId, int32_t errcode, const char *errMsg)
668 {
669 if (errMsg == NULL) {
670 return;
671 }
672 char cJsonStr[ERR_MSG_MAX_LEN] = {0};
673 int32_t ret = TransAuthChannelErrorPack(errcode, errMsg, cJsonStr, ERR_MSG_MAX_LEN);
674 if (ret != SOFTBUS_OK) {
675 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransAuthChannelErrorPack failed");
676 return;
677 }
678 AuthChannelData channelData = {
679 .module = MODULE_AUTH_CHANNEL,
680 .flag = AUTH_CHANNEL_REPLY,
681 .seq = 0,
682 .len = strlen(cJsonStr) + 1,
683 .data = (const uint8_t *)cJsonStr,
684 };
685 if (AuthPostChannelData(authId, &channelData) != SOFTBUS_OK) {
686 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "auth post channel data fail");
687 }
688 }
689
CreateAuthChannelInfo(const char * sessionName,bool isClient)690 static AuthChannelInfo *CreateAuthChannelInfo(const char *sessionName, bool isClient)
691 {
692 AuthChannelInfo *info = (AuthChannelInfo *)SoftBusCalloc(sizeof(AuthChannelInfo));
693 if (info == NULL) {
694 return NULL;
695 }
696 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
697 goto EXIT_ERR;
698 }
699 info->appInfo.myData.channelId = GenerateAuthChannelId();
700 SoftBusMutexUnlock(&g_authChannelList->lock);
701 if (GetAppInfo(sessionName, info->appInfo.myData.channelId, &info->appInfo, isClient) != SOFTBUS_OK) {
702 goto EXIT_ERR;
703 }
704 info->isClient = isClient;
705 return info;
706 EXIT_ERR:
707 SoftBusFree(info);
708 return NULL;
709 }
710
TransOpenAuthMsgChannel(const char * sessionName,const ConnectOption * connOpt,int32_t * channelId,const char * reqId)711 NO_SANITIZE("cfi") int32_t TransOpenAuthMsgChannel(const char *sessionName, const ConnectOption *connOpt,
712 int32_t *channelId, const char *reqId)
713 {
714 if (connOpt == NULL || channelId == NULL || connOpt->type != CONNECT_TCP || g_authChannelList == NULL) {
715 return SOFTBUS_INVALID_PARAM;
716 }
717 AuthChannelInfo *channel = CreateAuthChannelInfo(sessionName, true);
718 if (channel == NULL) {
719 return SOFTBUS_ERR;
720 }
721 if (strcpy_s(channel->appInfo.reqId, REQ_ID_SIZE_MAX, reqId) != EOK) {
722 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransOpenAuthMsgChannel strcpy_s reqId failed");
723 return SOFTBUS_ERR;
724 }
725 if (memcpy_s(&channel->connOpt, sizeof(ConnectOption), connOpt, sizeof(ConnectOption)) != EOK) {
726 SoftBusFree(channel);
727 return SOFTBUS_MEM_ERR;
728 }
729 *channelId = channel->appInfo.myData.channelId;
730 int32_t authId = AuthOpenChannel(connOpt->socketOption.addr, connOpt->socketOption.port);
731 if (authId < 0) {
732 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "AuthOpenChannel failed");
733 SoftBusFree(channel);
734 return SOFTBUS_ERR;
735 }
736 channel->authId = authId;
737 if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
738 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SoftBusMutexLock failed");
739 AuthCloseChannel(channel->authId);
740 SoftBusFree(channel);
741 return SOFTBUS_LOCK_ERR;
742 }
743 if (AddAuthChannelInfo(channel) != SOFTBUS_OK) {
744 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "AddAuthChannelInfo failed");
745 AuthCloseChannel(channel->authId);
746 SoftBusFree(channel);
747 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
748 return SOFTBUS_ERR;
749 }
750 if (TransPostAuthChannelMsg(&channel->appInfo, authId, AUTH_CHANNEL_REQ) != SOFTBUS_OK) {
751 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransPostAuthRequest failed");
752 AuthCloseChannel(channel->authId);
753 DelAuthChannelInfoByChanId(*channelId);
754 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
755 return SOFTBUS_ERR;
756 }
757 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
758 return SOFTBUS_OK;
759 }
760
TransCloseAuthChannel(int32_t channelId)761 NO_SANITIZE("cfi") int32_t TransCloseAuthChannel(int32_t channelId)
762 {
763 AuthChannelInfo *channel = NULL;
764 AuthChannelInfo *tmp = NULL;
765 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
766 return SOFTBUS_LOCK_ERR;
767 }
768 LIST_FOR_EACH_ENTRY_SAFE(channel, tmp, &g_authChannelList->list, AuthChannelInfo, node) {
769 if (channel->appInfo.myData.channelId != channelId) {
770 continue;
771 }
772 ListDelete(&channel->node);
773 g_authChannelList->cnt--;
774 AuthCloseChannel(channel->authId);
775 NofifyCloseAuthChannel(channel->appInfo.myData.pkgName, channel->appInfo.myData.pid, channelId);
776 SoftBusFree(channel);
777 SoftBusMutexUnlock(&g_authChannelList->lock);
778 return SOFTBUS_OK;
779 }
780 SoftBusMutexUnlock(&g_authChannelList->lock);
781 return SOFTBUS_ERR;
782 }
783
TransSendAuthMsg(int32_t channelId,const char * msg,int32_t len)784 NO_SANITIZE("cfi") int32_t TransSendAuthMsg(int32_t channelId, const char *msg, int32_t len)
785 {
786 if (msg == NULL || len <= 0) {
787 return SOFTBUS_INVALID_PARAM;
788 }
789
790 int32_t authId = GetAuthIdByChannelId(channelId);
791 if (authId < 0) {
792 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Get AuthId failed");
793 return SOFTBUS_TRANS_AUTH_CHANNEL_NOT_FOUND;
794 }
795
796 AuthChannelData channelData = {
797 .module = MODULE_AUTH_MSG,
798 .flag = 0,
799 .seq = 0,
800 .len = (uint32_t)len,
801 .data = (const uint8_t *)msg,
802 };
803 if (AuthPostChannelData(authId, &channelData) != SOFTBUS_OK) {
804 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "auth post channel data fail");
805 return SOFTBUS_ERR;
806 }
807 return SOFTBUS_OK;
808 }
809
TransAuthGetConnOptionByChanId(int32_t channelId,ConnectOption * connOpt)810 NO_SANITIZE("cfi") int32_t TransAuthGetConnOptionByChanId(int32_t channelId, ConnectOption *connOpt)
811 {
812 AuthChannelInfo chanInfo;
813 if (GetAuthChannelInfoByChanId(channelId, &chanInfo) != SOFTBUS_OK) {
814 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get auth channel info by chanid=%d fail", channelId);
815 return SOFTBUS_ERR;
816 }
817
818 if (!chanInfo.isClient) {
819 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "auth channel of conn opt invalid");
820 return SOFTBUS_ERR;
821 }
822
823 if (memcpy_s(connOpt, sizeof(ConnectOption), &(chanInfo.connOpt), sizeof(ConnectOption)) != EOK) {
824 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "auth channel connopt memcpy fail");
825 return SOFTBUS_ERR;
826 }
827 return SOFTBUS_OK;
828 }
829
TransNotifyAuthDataSuccess(int32_t channelId,const ConnectOption * connOpt)830 NO_SANITIZE("cfi") int32_t TransNotifyAuthDataSuccess(int32_t channelId, const ConnectOption *connOpt)
831 {
832 if (connOpt == NULL) {
833 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "%s invalid param.", __func__);
834 return SOFTBUS_ERR;
835 }
836 ConnectionAddr addr;
837 (void)memset_s(&addr, sizeof(ConnectionAddr), 0, sizeof(ConnectionAddr));
838 if (!LnnConvertOptionToAddr(&addr, connOpt, CONNECTION_ADDR_WLAN)) {
839 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "channel=%d convert addr fail.", channelId);
840 return SOFTBUS_ERR;
841 }
842 return LnnNotifyDiscoveryDevice(&addr, true);
843 }
844
TransAuthGetAppInfoByChanId(int32_t channelId,AppInfo * appInfo)845 NO_SANITIZE("cfi") int32_t TransAuthGetAppInfoByChanId(int32_t channelId, AppInfo *appInfo)
846 {
847 if (appInfo == NULL || g_authChannelList == NULL) {
848 return SOFTBUS_INVALID_PARAM;
849 }
850
851 if (SoftBusMutexLock(&g_authChannelList->lock) != 0) {
852 return SOFTBUS_LOCK_ERR;
853 }
854 AuthChannelInfo *info = NULL;
855 LIST_FOR_EACH_ENTRY(info, &g_authChannelList->list, AuthChannelInfo, node) {
856 if (info->appInfo.myData.channelId == channelId) {
857 if (memcpy_s(appInfo, sizeof(AppInfo), &info->appInfo, sizeof(AppInfo)) != EOK) {
858 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
859 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "auth channel appinfo memcpy fail");
860 return SOFTBUS_ERR;
861 }
862 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
863 return SOFTBUS_OK;
864 }
865 }
866 SoftBusMutexUnlock(&g_authChannelList->lock);
867 return SOFTBUS_ERR;
868 }
869
870