• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_p2p.h"
17 
18 #include <securec.h>
19 
20 #include "auth_common.h"
21 #include "lnn_exchange_device_info.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_errcode.h"
24 #include "softbus_log.h"
25 
AuthP2pOnKeyGenerated(int64_t authId,ConnectOption * option,SoftBusVersion peerVersion)26 static void AuthP2pOnKeyGenerated(int64_t authId, ConnectOption *option, SoftBusVersion peerVersion)
27 {
28     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "auth p2p onKeyGenerated, authId = %lld.", authId);
29     AuthManager *auth = AuthGetManagerByAuthId(authId);
30     if (auth == NULL) {
31         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth manager not found.");
32         return;
33     }
34 
35     int32_t side;
36     uint32_t bufSize = 0;
37     uint8_t *buf = LnnGetExchangeNodeInfo((int32_t)authId, AUTH_BT, SOFT_BUS_NEW_V1, &bufSize, &side);
38     if (buf == NULL) {
39         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "pack device info fail.");
40         return;
41     }
42 
43     AuthDataHead head;
44     head.flag = auth->side;
45     head.dataType = DATA_TYPE_SYNC;
46     head.module = HICHAIN_SYNC;
47     head.authId = authId;
48     head.seq = authId;
49     if (AuthPostData(&head, buf, bufSize) != SOFTBUS_OK) {
50         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "send device info fail.");
51         SoftBusFree(buf);
52         return;
53     }
54     SoftBusFree(buf);
55     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "send device info succ, authId = %lld.", authId);
56 }
57 
AuthP2pOnRecvSyncDeviceInfo(int64_t authId,AuthSideFlag side,const char * peerUuid,uint8_t * data,uint32_t len)58 static void AuthP2pOnRecvSyncDeviceInfo(int64_t authId, AuthSideFlag side, const char *peerUuid,
59     uint8_t *data, uint32_t len)
60 {
61     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "auth p2p onRecvSyncDeviceInfo, authId = %lld.", authId);
62     AuthManager *auth = AuthGetManagerByAuthId(authId);
63     if (auth == NULL) {
64         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth manager not found.");
65         return;
66     }
67 
68     ParseBuf parseBuf = {0};
69     parseBuf.buf = data;
70     parseBuf.len = len;
71     NodeInfo *nodeInfo = SoftBusCalloc(sizeof(NodeInfo));
72     if (nodeInfo == NULL) {
73         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "malloc node info fail");
74         return;
75     }
76     if (LnnParsePeerNodeInfo(&auth->option, AUTH_BT, nodeInfo, &parseBuf, side, auth->peerVersion) != SOFTBUS_OK) {
77         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "unpack peer device info fail");
78     }
79     /* device info not used... */
80     SoftBusFree(nodeInfo);
81 }
82 
AuthP2pOnDeviceVerifyPass(int64_t authId)83 static void AuthP2pOnDeviceVerifyPass(int64_t authId)
84 {
85     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "auth p2p onDeviceVerifyPass, authId = %lld.", authId);
86 }
87 
AuthP2pOnDeviceVerifyFail(int64_t authId,int32_t reason)88 static void AuthP2pOnDeviceVerifyFail(int64_t authId, int32_t reason)
89 {
90     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "auth p2p onDeviceVerifyFail, authId = %lld.", authId);
91 }
92 
AuthP2pOnDisconnect(int64_t authId)93 static void AuthP2pOnDisconnect(int64_t authId)
94 {
95     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "auth p2p onDisconnect, authId = %lld.", authId);
96     AuthManager *auth = AuthGetManagerByAuthId(authId);
97     if (auth == NULL) {
98         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "auth manager not found.");
99         return;
100     }
101     if (auth->status < AUTH_PASSED && auth->connCb.onConnOpenFailed != NULL) {
102         auth->connCb.onConnOpenFailed(auth->requestId, SOFTBUS_CONNECTION_ERR_CLOSED);
103         auth->connCb.onConnOpenFailed = NULL;
104     }
105 }
106 
AuthP2pOnDeviceNotTrusted(const char * peerUdid)107 static void AuthP2pOnDeviceNotTrusted(const char *peerUdid)
108 {
109     (void)peerUdid;
110     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "auth p2p onDeviceNotTrusted.");
111 }
112 
AuthP2pOnGroupCreated(const char * groupId)113 static void AuthP2pOnGroupCreated(const char *groupId)
114 {
115     (void)groupId;
116     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "auth p2p onGroupCreated.");
117 }
118 
AuthP2pOnGroupDeleted(const char * groupId)119 static void AuthP2pOnGroupDeleted(const char *groupId)
120 {
121     (void)groupId;
122     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "auth p2p onGroupDeleted.");
123 }
124 
AuthP2pInit(void)125 int32_t AuthP2pInit(void)
126 {
127     VerifyCallback p2pVerifyCb = {
128         .onKeyGenerated = AuthP2pOnKeyGenerated,
129         .onRecvSyncDeviceInfo = AuthP2pOnRecvSyncDeviceInfo,
130         .onDeviceVerifyPass = AuthP2pOnDeviceVerifyPass,
131         .onDeviceVerifyFail = AuthP2pOnDeviceVerifyFail,
132         .onDisconnect = AuthP2pOnDisconnect,
133         .onDeviceNotTrusted = AuthP2pOnDeviceNotTrusted,
134         .onGroupCreated = AuthP2pOnGroupCreated,
135         .onGroupDeleted = AuthP2pOnGroupDeleted
136     };
137     return AuthRegCallback(VERIFY_P2P_DEVICE, &p2pVerifyCb);
138 }
139 
AuthGetPreferConnInfo(const char * uuid,AuthConnInfo * connInfo)140 int32_t AuthGetPreferConnInfo(const char *uuid, AuthConnInfo *connInfo)
141 {
142     if (uuid == NULL || connInfo == NULL) {
143         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "invalid param.");
144         return SOFTBUS_INVALID_PARAM;
145     }
146 
147     if (GetActiveAuthConnInfo(uuid, CONNECT_TCP, connInfo) == SOFTBUS_OK) {
148         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "select wifi auth.");
149         return SOFTBUS_OK;
150     }
151     if (GetActiveAuthConnInfo(uuid, CONNECT_BR, connInfo) == SOFTBUS_OK) {
152         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "select br auth.");
153         return SOFTBUS_OK;
154     }
155     if (GetActiveAuthConnInfo(uuid, CONNECT_BLE, connInfo) == SOFTBUS_OK) {
156         ConnectOption option = {0};
157         if (ConvertAuthConnInfoToOption(connInfo, &option) == SOFTBUS_OK &&
158             CheckActiveConnection(&option)) {
159             SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "select ble auth.");
160             return SOFTBUS_OK;
161         }
162     }
163     SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "active auth not found by uuid.");
164     return SOFTBUS_ERR;
165 }
166 
IsWiFiLink(const AuthManager * auth)167 bool IsWiFiLink(const AuthManager *auth)
168 {
169     if (auth == NULL) {
170         return false;
171     }
172     if (auth->option.type == CONNECT_TCP && !auth->isAuthP2p) {
173         return true;
174     }
175     return false;
176 }
177 
IsP2PLink(const AuthManager * auth)178 bool IsP2PLink(const AuthManager *auth)
179 {
180     if (auth == NULL) {
181         return false;
182     }
183     if (auth->option.type == CONNECT_TCP && auth->isAuthP2p) {
184         return true;
185     }
186     return false;
187 }