1 /*
2  * Copyright (C) 2021-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 "ble_security.h"
17 
18 #include <iostream>
19 
20 #include "ble_adapter.h"
21 #include "ble_config.h"
22 #include "ble_properties.h"
23 #include "ble_utils.h"
24 
25 #include "btm.h"
26 #include "compat.h"
27 #include "log.h"
28 #include "securec.h"
29 
30 namespace OHOS {
31 namespace bluetooth {
32 struct BleSecurity::impl {
33 public:
34     uint8_t pairMethod_ = 0;
35     std::map<int, func> funcMap_ {};
36 };
BleSecurity( IAdapterBle &bleAdapter, utility::Dispatcher &dispatch, BaseObserverList<IAdapterBleObserver> &observer)37 BleSecurity::BleSecurity(
38     IAdapterBle &bleAdapter, utility::Dispatcher &dispatch, BaseObserverList<IAdapterBleObserver> &observer)
39     : bleAdapter_(&bleAdapter),
40       dispatcher_(&dispatch),
41       baseCallback_(&observer),
42       pimpl(std::make_unique<BleSecurity::impl>())
43 {
44     InitGapEventFuncTable();
45     int ret = RegisterCallbackToGap();
46     if (ret != BT_SUCCESS) {
47         LOG_ERROR("[BleSecurity] %{public}s", __func__);
48     }
49 }
50 
~BleSecurity()51 BleSecurity::~BleSecurity()
52 {
53     pimpl->funcMap_.clear();
54     int ret = DeregisterCallbackToGap();
55     if (ret != BT_SUCCESS) {
56         LOG_ERROR("[BleSecurity] %{public}s", __func__);
57     }
58 }
59 
InitGapEventFuncTable() const60 void BleSecurity::InitGapEventFuncTable() const
61 {
62     pimpl->funcMap_.clear();
63     pimpl->funcMap_.insert(std::make_pair(BLE_GAP_ENCRYPTION_COMPLETE_EVT, &BleSecurity::GapEncryptionComplete));
64     pimpl->funcMap_.insert(
65         std::make_pair(BLE_GAP_LE_LOCAL_ENCRYPTION_KEY_REQ_EVT, &BleSecurity::GapLeLocalEncryptionKeyReqEvent));
66     pimpl->funcMap_.insert(
67         std::make_pair(BLE_GAP_LE_REMOTE_ENCRYPTION_KEY_REQ_EVT, &BleSecurity::GapLeRemoteEncryptionKeyReqEvent));
68     pimpl->funcMap_.insert(
69         std::make_pair(BLE_GAP_LE_SIGN_COUNTER_CHANGE_NOTIF_EVT, &BleSecurity::GapLeSignCounterChangeNotification));
70     pimpl->funcMap_.insert(
71         std::make_pair(BLE_GAP_LE_REQ_SIGNING_ALGORITHM_INFO_EVT, &BleSecurity::GapRequestSigningAlgorithmInfoEvt));
72     pimpl->funcMap_.insert(std::make_pair(BLE_GAP_LE_PAIR_FEATURE_REQ_EVT, &BleSecurity::GapLePairFeatureReq));
73     pimpl->funcMap_.insert(std::make_pair(BLE_GAP_LE_PAIR_FEATURE_IND_EVT, &BleSecurity::GapLePairFeatureInd));
74     pimpl->funcMap_.insert(std::make_pair(BLE_GAP_LE_PAIR_METHOD_NOTI_EVT, &BleSecurity::GapLePairMethodNotify));
75     pimpl->funcMap_.insert(
76         std::make_pair(BLE_GAP_LE_PAIR_KEY_PRESS_NOTI_EVT, &BleSecurity::GapLePairKeyPressNotification));
77     pimpl->funcMap_.insert(std::make_pair(BLE_GAP_LE_PAIR_PASSKEY_REQ_EVT, &BleSecurity::GapLePairPassKeyReq));
78     pimpl->funcMap_.insert(
79         std::make_pair(BLE_GAP_LE_PAIR_PASSKEY_NOTI_EVT, &BleSecurity::GapLePairPassKeyNotification));
80     pimpl->funcMap_.insert(std::make_pair(BLE_GAP_LE_PAIR_OOB_REQ_EVT, &BleSecurity::GapLePairOobReq));
81     pimpl->funcMap_.insert(std::make_pair(BLE_GAP_LE_PAIR_SC_OOB_REQ_EVT, &BleSecurity::GapLePairScOobReq));
82     pimpl->funcMap_.insert(
83         std::make_pair(BLE_GAP_LE_PAIR_SC_USER_CONFIRM_REQ_EVT, &BleSecurity::GapLePairScUserConfirmReq));
84     pimpl->funcMap_.insert(std::make_pair(BLE_GAP_LE_PAIR_COMELETE_EVT, &BleSecurity::GapLePairComplete));
85     pimpl->funcMap_.insert(std::make_pair(BLE_GAP_LE_PAIR_KEY_NOTI_EVT, &BleSecurity::GapLePairKeyNotify));
86     pimpl->funcMap_.insert(
87         std::make_pair(BLE_GAP_LE_REQUEST_SECURITY_RESULT, &BleSecurity::GapLeRequestSecurityResultEvt));
88 }
89 
EncryptionComplete(uint8_t status, const BtAddr *peerAddr, void *context)90 void BleSecurity::EncryptionComplete(uint8_t status, const BtAddr *peerAddr, void *context)
91 {
92     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
93 
94     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
95     if (bleSecurity == nullptr) {
96         return;
97     }
98 
99     BleGapCallbackParam gapCallbackParam;
100     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
101     gapCallbackParam.encryptionComplete_.status = status;
102     (void)memcpy_s(&gapCallbackParam.encryptionComplete_.peerAddr, sizeof(BtAddr), peerAddr, sizeof(BtAddr));
103     bleSecurity->dispatcher_->PostTask(
104         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_ENCRYPTION_COMPLETE_EVT, gapCallbackParam));
105 }
106 
LeLocalEncryptionKeyReqEvent(const BtAddr *addr, uint64_t rand, uint16_t ediv, void *context)107 void BleSecurity::LeLocalEncryptionKeyReqEvent(const BtAddr *addr, uint64_t rand, uint16_t ediv, void *context)
108 {
109     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
110 
111     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
112     if (bleSecurity == nullptr) {
113         return;
114     }
115 
116     BleGapCallbackParam gapCallbackParam;
117     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
118     (void)memcpy_s(&gapCallbackParam.leLocalEncryptionKeyReqEvent_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
119     gapCallbackParam.leLocalEncryptionKeyReqEvent_.ediv = ediv;
120     gapCallbackParam.leLocalEncryptionKeyReqEvent_.rand = rand;
121     bleSecurity->dispatcher_->PostTask(std::bind(
122         &BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_LOCAL_ENCRYPTION_KEY_REQ_EVT, gapCallbackParam));
123 }
124 
LeRemoteEncryptionKeyReqEvent(const BtAddr *addr, void *context)125 void BleSecurity::LeRemoteEncryptionKeyReqEvent(const BtAddr *addr, void *context)
126 {
127     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
128 
129     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
130     if (bleSecurity == nullptr) {
131         return;
132     }
133 
134     BleGapCallbackParam gapCallbackParam;
135     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
136     (void)memcpy_s(&gapCallbackParam.leRemoteEncryptionKeyReqEvent_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
137     bleSecurity->dispatcher_->PostTask(std::bind(
138         &BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_REMOTE_ENCRYPTION_KEY_REQ_EVT, gapCallbackParam));
139 }
140 
LeSignCounterChangeNotification( const BtAddr *addr, GAP_SignCounterType type, uint32_t counter, void *context)141 void BleSecurity::LeSignCounterChangeNotification(
142     const BtAddr *addr, GAP_SignCounterType type, uint32_t counter, void *context)
143 {
144     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
145 
146     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
147     if (bleSecurity == nullptr) {
148         return;
149     }
150 
151     BleGapCallbackParam gapCallbackParam;
152     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
153     (void)memcpy_s(&gapCallbackParam.leSignCounterChangeNotification_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
154     gapCallbackParam.leSignCounterChangeNotification_.type = type;
155     gapCallbackParam.leSignCounterChangeNotification_.counter = counter;
156     bleSecurity->dispatcher_->PostTask(std::bind(
157         &BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_SIGN_COUNTER_CHANGE_NOTIF_EVT, gapCallbackParam));
158 }
159 
LePairFeatureReq(const BtAddr *peerAddr, bool localPair, void *context)160 void BleSecurity::LePairFeatureReq(const BtAddr *peerAddr, bool localPair, void *context)
161 {
162     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
163 
164     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
165     if (bleSecurity == nullptr) {
166         return;
167     }
168 
169     BleGapCallbackParam gapCallbackParam;
170     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
171     (void)memcpy_s(&gapCallbackParam.lePairFeatureReq_.peerAddr, sizeof(BtAddr), peerAddr, sizeof(BtAddr));
172     gapCallbackParam.lePairFeatureReq_.localPair = localPair;
173     bleSecurity->dispatcher_->PostTask(
174         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_FEATURE_REQ_EVT, gapCallbackParam));
175 }
176 
LePairFeatureInd(const BtAddr *addr, GapLePairFeature remoteFrature, void *context)177 void BleSecurity::LePairFeatureInd(const BtAddr *addr, GapLePairFeature remoteFrature, void *context)
178 {
179     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
180 
181     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
182     if (bleSecurity == nullptr) {
183         return;
184     }
185 
186     BleGapCallbackParam gapCallbackParam;
187     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
188     (void)memcpy_s(&gapCallbackParam.lePairFeatureInd_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
189     gapCallbackParam.lePairFeatureInd_.remoteFrature = remoteFrature;
190     bleSecurity->dispatcher_->PostTask(
191         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_FEATURE_IND_EVT, gapCallbackParam));
192 }
193 
LePairMethodNotify(const BtAddr *addr, uint8_t pairMethod, void *context)194 void BleSecurity::LePairMethodNotify(const BtAddr *addr, uint8_t pairMethod, void *context)
195 {
196     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
197 
198     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
199     if (bleSecurity == nullptr) {
200         return;
201     }
202 
203     BleGapCallbackParam gapCallbackParam;
204     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
205     (void)memcpy_s(&gapCallbackParam.lePairMethodNotify_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
206     gapCallbackParam.lePairMethodNotify_.pairMethod = pairMethod;
207     bleSecurity->dispatcher_->PostTask(
208         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_METHOD_NOTI_EVT, gapCallbackParam));
209 }
210 
LePairKeyPressNotification(const BtAddr *addr, uint8_t pressType, void *context)211 void BleSecurity::LePairKeyPressNotification(const BtAddr *addr, uint8_t pressType, void *context)
212 {
213     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
214 
215     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
216     if (bleSecurity == nullptr) {
217         return;
218     }
219 
220     BleGapCallbackParam gapCallbackParam;
221     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
222     (void)memcpy_s(&gapCallbackParam.lePairKeyPressNotification_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
223     gapCallbackParam.lePairKeyPressNotification_.pressType = pressType;
224     bleSecurity->dispatcher_->PostTask(
225         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_KEY_PRESS_NOTI_EVT, gapCallbackParam));
226 }
227 
LePairPassKeyReq(const BtAddr *addr, void *context)228 void BleSecurity::LePairPassKeyReq(const BtAddr *addr, void *context)
229 {
230     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
231 
232     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
233     if (bleSecurity == nullptr) {
234         return;
235     }
236 
237     BleGapCallbackParam gapCallbackParam;
238     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
239     (void)memcpy_s(&gapCallbackParam.lePairPassKeyReq_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
240     bleSecurity->dispatcher_->PostTask(
241         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_PASSKEY_REQ_EVT, gapCallbackParam));
242 }
243 
LePairOobReq(const BtAddr *addr, void *context)244 void BleSecurity::LePairOobReq(const BtAddr *addr, void *context)
245 {
246     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
247 
248     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
249     if (bleSecurity == nullptr) {
250         return;
251     }
252 
253     BleGapCallbackParam gapCallbackParam;
254     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
255     (void)memcpy_s(&gapCallbackParam.lePairOobReq_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
256     bleSecurity->dispatcher_->PostTask(
257         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_OOB_REQ_EVT, gapCallbackParam));
258 }
259 
LePairScOobReq(const BtAddr *addr, void *context)260 void BleSecurity::LePairScOobReq(const BtAddr *addr, void *context)
261 {
262     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
263 
264     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
265     if (bleSecurity == nullptr) {
266         return;
267     }
268 
269     BleGapCallbackParam gapCallbackParam;
270     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
271     (void)memcpy_s(&gapCallbackParam.lePairScOob_req_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
272     bleSecurity->dispatcher_->PostTask(
273         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_SC_OOB_REQ_EVT, gapCallbackParam));
274 }
275 
LePairScUserConfirmReq(const BtAddr *addr, uint32_t number, void *context)276 void BleSecurity::LePairScUserConfirmReq(const BtAddr *addr, uint32_t number, void *context)
277 {
278     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
279 
280     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
281     if (bleSecurity == nullptr) {
282         return;
283     }
284 
285     BleGapCallbackParam gapCallbackParam;
286     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
287     (void)memcpy_s(&gapCallbackParam.lePairScUserConfirmReq_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
288     gapCallbackParam.lePairScUserConfirmReq_.number = number;
289     bleSecurity->dispatcher_->PostTask(std::bind(
290         &BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_SC_USER_CONFIRM_REQ_EVT, gapCallbackParam));
291 }
292 
LePairComplete(const BtAddr *addr, uint8_t result, uint8_t keyType, void *context)293 void BleSecurity::LePairComplete(const BtAddr *addr, uint8_t result, uint8_t keyType, void *context)
294 {
295     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
296 
297     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
298     if (bleSecurity == nullptr) {
299         return;
300     }
301 
302     BleGapCallbackParam gapCallbackParam;
303     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
304     (void)memcpy_s(&gapCallbackParam.lePairComplete_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
305     gapCallbackParam.lePairComplete_.result = result;
306     gapCallbackParam.lePairComplete_.keyType = keyType;
307     bleSecurity->dispatcher_->PostTask(
308         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_COMELETE_EVT, gapCallbackParam));
309 }
310 
LePairKeyNotify(const BtAddr *addr, LePairedKeys leKeys, void *context)311 void BleSecurity::LePairKeyNotify(const BtAddr *addr, LePairedKeys leKeys, void *context)
312 {
313     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
314     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
315     if (bleSecurity == nullptr) {
316         return;
317     }
318     BleGapCallbackParam gapCallbackParam;
319     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
320     (void)memcpy_s(&gapCallbackParam.lePairKeyNotify_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
321     if (leKeys.remoteEncKey != nullptr) {
322         (void)memcpy_s(
323             &gapCallbackParam.lePairKeyNotify_.remoteEncKey, sizeof(LeEncKey), leKeys.remoteEncKey, sizeof(LeEncKey));
324         gapCallbackParam.lePairKeyNotify_.hasRemoteEncKey = true;
325     } else {
326         gapCallbackParam.lePairKeyNotify_.hasRemoteEncKey = false;
327     }
328     if (leKeys.remoteIdKey != nullptr) {
329         (void)memcpy_s(
330             &gapCallbackParam.lePairKeyNotify_.remoteIdKey, sizeof(LeIdKey), leKeys.remoteIdKey, sizeof(LeIdKey));
331         gapCallbackParam.lePairKeyNotify_.hasRemoteIdKey = true;
332     } else {
333         gapCallbackParam.lePairKeyNotify_.hasRemoteIdKey = false;
334     }
335     if (leKeys.remoteSignKey != nullptr) {
336         (void)memcpy_s(&gapCallbackParam.lePairKeyNotify_.remoteSignKey,
337             sizeof(LeSignKey),
338             leKeys.remoteSignKey,
339             sizeof(LeSignKey));
340         gapCallbackParam.lePairKeyNotify_.hasRemoteSignKey = true;
341     } else {
342         gapCallbackParam.lePairKeyNotify_.hasRemoteSignKey = false;
343     }
344     if (leKeys.localEncKey != nullptr) {
345         (void)memcpy_s(
346             &gapCallbackParam.lePairKeyNotify_.localEncKey, sizeof(LeEncKey), leKeys.localEncKey, sizeof(LeEncKey));
347         gapCallbackParam.lePairKeyNotify_.hasLocalEncKey = true;
348     } else {
349         gapCallbackParam.lePairKeyNotify_.hasLocalEncKey = false;
350     }
351     if (leKeys.localSignKey != nullptr) {
352         (void)memcpy_s(
353             &gapCallbackParam.lePairKeyNotify_.localSignKey, sizeof(LeSignKey), leKeys.localSignKey, sizeof(LeSignKey));
354         gapCallbackParam.lePairKeyNotify_.hasLocalSignKey = true;
355     } else {
356         gapCallbackParam.lePairKeyNotify_.hasLocalSignKey = false;
357     }
358 
359     bleSecurity->dispatcher_->PostTask(
360         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_KEY_NOTI_EVT, gapCallbackParam));
361 }
362 
GapRequestSigningAlgorithmInfo(const BtAddr *addr, void *context)363 void BleSecurity::GapRequestSigningAlgorithmInfo(const BtAddr *addr, void *context)
364 {
365     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
366 
367     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
368     if (bleSecurity == nullptr) {
369         return;
370     }
371 
372     BleGapCallbackParam gapCallbackParam;
373     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
374     (void)memcpy_s(&gapCallbackParam.gapRequestSigningAlgorithmInfo_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
375     bleSecurity->dispatcher_->PostTask(std::bind(
376         &BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_REQ_SIGNING_ALGORITHM_INFO_EVT, gapCallbackParam));
377 }
378 
LePairPassKeyNotification(const BtAddr *addr, uint32_t number, void *context)379 void BleSecurity::LePairPassKeyNotification(const BtAddr *addr, uint32_t number, void *context)
380 {
381     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
382 
383     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
384     if (bleSecurity == nullptr) {
385         return;
386     }
387 
388     BleGapCallbackParam gapCallbackParam;
389     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
390     (void)memcpy_s(&gapCallbackParam.lePairPassKeyNotification_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
391     gapCallbackParam.lePairPassKeyNotification_.number = number;
392     bleSecurity->dispatcher_->PostTask(
393         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_PAIR_PASSKEY_NOTI_EVT, gapCallbackParam));
394 }
395 
GapLeRequestSecurityResult( const BtAddr *addr, uint8_t result, GAP_LeSecurityStatus status, void *context)396 void BleSecurity::GapLeRequestSecurityResult(
397     const BtAddr *addr, uint8_t result, GAP_LeSecurityStatus status, void *context)
398 {
399     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
400 
401     BleSecurity *bleSecurity = static_cast<BleSecurity *>(context);
402     if (bleSecurity == nullptr) {
403         return;
404     }
405 
406     BleGapCallbackParam gapCallbackParam;
407     (void)memset_s(&gapCallbackParam, sizeof(gapCallbackParam), 0x00, sizeof(gapCallbackParam));
408     (void)memcpy_s(&gapCallbackParam.leRequestSecurityResult_.addr, sizeof(BtAddr), addr, sizeof(BtAddr));
409     gapCallbackParam.leRequestSecurityResult_.result = result;
410     gapCallbackParam.leRequestSecurityResult_.status = status;
411     bleSecurity->dispatcher_->PostTask(
412         std::bind(&BleSecurity::HandleGapEvent, bleSecurity, BLE_GAP_LE_REQUEST_SECURITY_RESULT, gapCallbackParam));
413 }
414 
SavePairKeyNotify(const BleGapCallbackParam &param) const415 bool BleSecurity::SavePairKeyNotify(const BleGapCallbackParam &param) const
416 {
417     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
418 
419     bool ret = false;
420     RawAddress addr = RawAddress::ConvertToString(param.lePairKeyNotify_.addr.addr);
421 
422     if (bleAdapter_ != nullptr) {
423         ret = (static_cast<BleAdapter *>(bleAdapter_))->IsRemovePairedDevice(addr);
424         if (ret) {
425             return ret;
426         }
427     }
428 
429     /// before saving key, delete the same identity addr device
430     std::vector<std::string> pairedAddrList = BleConfig::GetInstance().GetPairedAddrList();
431     for (auto address : pairedAddrList) {
432         RawAddress rawAddr(address);
433         std::string invalidMacAddress(INVALID_MAC_ADDRESS);
434         if ((!invalidMacAddress.compare(rawAddr.GetAddress())) || (rawAddr.GetAddress().empty())) {
435             continue;
436         }
437         std::string peerIdentityAddr = BleConfig::GetInstance().GetPeerIdentityAddr(rawAddr.GetAddress());
438         if (param.lePairKeyNotify_.hasRemoteIdKey) {
439             RawAddress peerAddr = RawAddress::ConvertToString(param.lePairKeyNotify_.remoteIdKey.identityAddr.addr);
440             if (peerIdentityAddr.compare(peerAddr.GetAddress()) == 0) {
441                 BleConfig::GetInstance().RemovePairedDevice(rawAddr.GetAddress());
442             }
443         }
444     }
445     ret = SaveLocalPairKey(addr, param);
446     ret &= SavePeerPairKey(addr, param);
447     return ret;
448 }
449 
SaveLocalPairKey(const RawAddress &addr, const BleGapCallbackParam &param)450 bool BleSecurity::SaveLocalPairKey(const RawAddress &addr, const BleGapCallbackParam &param)
451 {
452     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
453 
454     bool ret = true;
455     // local
456     if (param.lePairKeyNotify_.hasLocalEncKey) {
457         ret &= BleConfig::GetInstance().SetLocalEdivRand(addr.GetAddress(),
458             std::to_string(param.lePairKeyNotify_.localEncKey.ediv),
459             std::to_string(param.lePairKeyNotify_.localEncKey.rand));
460         std::vector<uint8_t> ltk(
461             param.lePairKeyNotify_.localEncKey.ltk, param.lePairKeyNotify_.localEncKey.ltk + GAP_LTK_SIZE);
462         ret &= BleConfig::GetInstance().SetLocalLtk(addr.GetAddress(), BleUtils::ConvertIntToHexString(ltk));
463         ret &= BleConfig::GetInstance().SetLocalKeySize(
464             addr.GetAddress(), std::to_string(param.lePairKeyNotify_.localEncKey.keySize));
465     }
466 
467     if (param.lePairKeyNotify_.hasLocalSignKey) {
468         std::vector<uint8_t> csrk(
469             param.lePairKeyNotify_.localSignKey.csrk, param.lePairKeyNotify_.localSignKey.csrk + GAP_LTK_SIZE);
470         ret &= BleConfig::GetInstance().SetLocalCsrk(addr.GetAddress(), BleUtils::ConvertIntToHexString(csrk));
471         ret &= BleConfig::GetInstance().SetLocalSignCounter(
472             addr.GetAddress(), param.lePairKeyNotify_.localSignKey.counter);
473     }
474     return ret;
475 }
476 
SavePeerPairKey(const RawAddress &addr, const BleGapCallbackParam &param)477 bool BleSecurity::SavePeerPairKey(const RawAddress &addr, const BleGapCallbackParam &param)
478 {
479     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
480 
481     bool ret = true;
482     // peer
483     if (param.lePairKeyNotify_.hasRemoteEncKey) {
484         ret &= BleConfig::GetInstance().SetPeerEdivRand(addr.GetAddress(),
485             std::to_string(param.lePairKeyNotify_.remoteEncKey.ediv),
486             std::to_string(param.lePairKeyNotify_.remoteEncKey.rand));
487         std::vector<uint8_t> ltk(
488             param.lePairKeyNotify_.remoteEncKey.ltk, param.lePairKeyNotify_.remoteEncKey.ltk + GAP_LTK_SIZE);
489         ret &= BleConfig::GetInstance().SetPeerLtk(addr.GetAddress(), BleUtils::ConvertIntToHexString(ltk));
490         ret &= BleConfig::GetInstance().SetPeerKeySize(
491             addr.GetAddress(), std::to_string(param.lePairKeyNotify_.remoteEncKey.keySize));
492     }
493 
494     if (param.lePairKeyNotify_.hasRemoteIdKey) {
495         RawAddress peerAddr = RawAddress::ConvertToString(param.lePairKeyNotify_.remoteIdKey.identityAddr.addr);
496         ret &= BleConfig::GetInstance().SetPeerIdentityAddr(
497             addr.GetAddress(), param.lePairKeyNotify_.remoteIdKey.identityAddr.type, peerAddr.GetAddress());
498         std::vector<uint8_t> irk(
499             param.lePairKeyNotify_.remoteIdKey.irk, param.lePairKeyNotify_.remoteIdKey.irk + GAP_LTK_SIZE);
500         ret &= BleConfig::GetInstance().SetPeerIrk(addr.GetAddress(), BleUtils::ConvertIntToHexString(irk));
501     }
502 
503     if (param.lePairKeyNotify_.hasRemoteSignKey) {
504         std::vector<uint8_t> csrk(
505             param.lePairKeyNotify_.remoteSignKey.csrk, param.lePairKeyNotify_.remoteSignKey.csrk + GAP_LTK_SIZE);
506         ret &= BleConfig::GetInstance().SetPeerCsrk(addr.GetAddress(), BleUtils::ConvertIntToHexString(csrk));
507         ret &= BleConfig::GetInstance().SetPeerSignCounter(
508             addr.GetAddress(), param.lePairKeyNotify_.remoteSignKey.counter);
509     }
510     return ret;
511 }
512 
GapEncryptionComplete(const BleGapCallbackParam &param) const513 bool BleSecurity::GapEncryptionComplete(const BleGapCallbackParam &param) const
514 {
515     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
516 
517     if (param.encryptionComplete_.status != BT_SUCCESS) {
518         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble encryption failed!");
519         return false;
520     } else {
521         RawAddress addr = RawAddress::ConvertToString(param.encryptionComplete_.peerAddr.addr);
522         if (bleAdapter_ != nullptr) {
523             (static_cast<BleAdapter *>(bleAdapter_))->EncryptionComplete(addr);
524         }
525         LOG_DEBUG("[BleSecurity] %{public}s:%{public}s", __func__, "Ble encryption success!");
526     }
527     return true;
528 }
529 
GapLeLocalEncryptionKeyReqEvent(const BleGapCallbackParam &param) const530 bool BleSecurity::GapLeLocalEncryptionKeyReqEvent(const BleGapCallbackParam &param) const
531 {
532     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
533 
534     RawAddress addr = RawAddress::ConvertToString(param.leLocalEncryptionKeyReqEvent_.addr.addr);
535     int accept = GAP_NOT_ACCEPT;
536     LeEncKey encKey;
537     (void)memset_s(&encKey, sizeof(encKey), 0x00, sizeof(encKey));
538     std::string ltk = BleConfig::GetInstance().GetLocalLtk(addr.GetAddress());
539     std::string rand = BleConfig::GetInstance().GetLocalRand(addr.GetAddress());
540     std::string ediv = BleConfig::GetInstance().GetLocalEdiv(addr.GetAddress());
541     if ((rand.compare(std::to_string(param.leLocalEncryptionKeyReqEvent_.rand)) == 0) &&
542         (ediv.compare(std::to_string(param.leLocalEncryptionKeyReqEvent_.ediv)) == 0) && (!ltk.empty())) {
543         accept = GAP_ACCEPT;
544         std::vector<uint8_t> vec;
545         BleUtils::ConvertHexStringToInt(ltk, vec);
546         (void)memcpy_s(encKey.ltk, GAP_CSRK_SIZE, &vec[0], vec.size());
547         encKey.rand = std::stoull(rand);
548         encKey.ediv = std::stoull(ediv);
549     }
550 
551     int ret = GAPIF_LeLocalEncryptionKeyRsp(&param.leLocalEncryptionKeyReqEvent_.addr, accept, encKey, 1);
552     if (ret != BT_SUCCESS) {
553         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble remote encryption key rsp failed!");
554     }
555     (void)memset_s(&encKey, sizeof(encKey), 0x00, sizeof(encKey));
556     return ret;
557 }
558 
GapLeRemoteEncryptionKeyReqEvent(const BleGapCallbackParam &param) const559 bool BleSecurity::GapLeRemoteEncryptionKeyReqEvent(const BleGapCallbackParam &param) const
560 {
561     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
562 
563     RawAddress addr = RawAddress::ConvertToString(param.leRemoteEncryptionKeyReqEvent_.addr.addr);
564     int accept = GAP_NOT_ACCEPT;
565     LeEncKey encKey;
566     (void)memset_s(&encKey, sizeof(encKey), 0x00, sizeof(encKey));
567     std::string ltk = BleConfig::GetInstance().GetPeerLtk(addr.GetAddress());
568     std::string rand = BleConfig::GetInstance().GetPeerRand(addr.GetAddress());
569     std::string ediv = BleConfig::GetInstance().GetPeerEdiv(addr.GetAddress());
570     if ((!rand.empty()) && (!ediv.empty()) && (!ltk.empty())) {
571         accept = GAP_ACCEPT;
572         std::vector<uint8_t> vec;
573         BleUtils::ConvertHexStringToInt(ltk, vec);
574         (void)memcpy_s(encKey.ltk, GAP_CSRK_SIZE, &vec[0], vec.size());
575         encKey.rand = std::stoull(rand);
576         encKey.ediv = std::stoull(ediv);
577     }
578 
579     int ret = GAPIF_LeRemoteEncryptionKeyRsp(&param.leRemoteEncryptionKeyReqEvent_.addr, accept, encKey, 1);
580     if (ret != BT_SUCCESS) {
581         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble remote encryption key rsp failed!");
582     }
583     (void)memset_s(&encKey, sizeof(encKey), 0x00, sizeof(encKey));
584     return ret;
585 }
586 
GapLeSignCounterChangeNotification(const BleGapCallbackParam &param) const587 bool BleSecurity::GapLeSignCounterChangeNotification(const BleGapCallbackParam &param) const
588 {
589     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
590 
591     RawAddress addr = RawAddress::ConvertToString(param.leSignCounterChangeNotification_.addr.addr);
592 
593     if (param.leSignCounterChangeNotification_.type == LOCAL_SIGN_COUNTER) {
594         BleConfig::GetInstance().SetLocalSignCounter(addr.GetAddress(), param.leSignCounterChangeNotification_.counter);
595     } else {
596         BleConfig::GetInstance().SetPeerSignCounter(addr.GetAddress(), param.leSignCounterChangeNotification_.counter);
597     }
598     return true;
599 }
600 
GapRequestSigningAlgorithmInfoEvt(const BleGapCallbackParam &param) const601 bool BleSecurity::GapRequestSigningAlgorithmInfoEvt(const BleGapCallbackParam &param) const
602 {
603     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
604 
605     RawAddress addr = RawAddress::ConvertToString(param.gapRequestSigningAlgorithmInfo_.addr.addr);
606     int accept = GAP_NOT_ACCEPT;
607     GapSigningAlgorithmInfo info;
608     (void)memset_s(&info, sizeof(info), 0x00, sizeof(info));
609     LeSignKey localKey;
610     (void)memset_s(&localKey, sizeof(localKey), 0x00, sizeof(localKey));
611 
612     if (!BleConfig::GetInstance().GetLocalCsrk(addr.GetAddress()).empty()) {
613         localKey.counter = BleConfig::GetInstance().GetLocalSignCounter(addr.GetAddress());
614         std::vector<uint8_t> vec;
615         BleUtils::ConvertHexStringToInt(BleConfig::GetInstance().GetLocalCsrk(addr.GetAddress()), vec);
616         (void)memcpy_s(localKey.csrk, GAP_CSRK_SIZE, &vec[0], vec.size());
617         info.localKey = &localKey;
618         accept = GAP_ACCEPT;
619     }
620 
621     LeSignKey remoteKey;
622     if (!BleConfig::GetInstance().GetPeerCsrk(addr.GetAddress()).empty()) {
623         remoteKey.counter = BleConfig::GetInstance().GetPeerSignCounter(addr.GetAddress());
624         std::vector<uint8_t> vec;
625         BleUtils::ConvertHexStringToInt(BleConfig::GetInstance().GetPeerCsrk(addr.GetAddress()), vec);
626         (void)memcpy_s(remoteKey.csrk, GAP_CSRK_SIZE, &vec[0], vec.size());
627         info.remoteKey = &remoteKey;
628         accept &= GAP_ACCEPT;
629     }
630 
631     int ret = GAPIF_RequestSigningAlgorithmInfoRsp(&param.gapRequestSigningAlgorithmInfo_.addr, accept, info);
632     if (ret != BT_SUCCESS) {
633         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble request signing algorithm info rsp failed!");
634     }
635     return ret;
636 }
637 
LePairFeatureReq(const BleGapCallbackParam &param)638 bool BleSecurity::LePairFeatureReq(const BleGapCallbackParam &param)
639 {
640     RawAddress addr = RawAddress::ConvertToString(param.lePairFeatureReq_.peerAddr.addr);
641     GapLePairFeature feature;
642     (void)memset_s(&feature, sizeof(feature), 0x00, sizeof(feature));
643     feature.ioCapability = BleConfig::GetInstance().GetIoCapability();
644     feature.maxEncKeySize = GAP_LINKKEY_SIZE;
645 
646     if (BleConfig::GetInstance().GetBleSecurity()) {
647         feature.authReq = GAP_LE_AUTH_REQ_BONDING | GAP_LE_AUTH_REQ_BIT_MITM | GAP_LE_AUTH_REQ_BIT_SC;
648     } else {
649         feature.authReq = GAP_LE_AUTH_REQ_BONDING | GAP_LE_AUTH_REQ_BIT_MITM;
650     }
651 
652     /// Compat check
653     GAP_LeSecMode1Level level1 = static_cast<GAP_LeSecMode1Level>(BleConfig::GetInstance().GetBleModel1Level());
654     if ((level1 != LE_MODE_1_LEVEL_4) &&
655         (Compat::CompatCheck(CompatType::COMPAT_DISABLE_BLE_SECURE_CONNECTIONS, addr.GetAddress()))) {
656         BtmLocalVersionInformation version;
657         (void)memset_s(&version, sizeof(version), 0x00, sizeof(version));
658         if ((BTM_GetLocalVersionInformation(&version) == BT_SUCCESS) &&
659             (version.hciVersion >= BLUETOOTH_CORE_SPECIFICATION_4_2)) {
660             feature.authReq &= ~GAP_LE_AUTH_REQ_BIT_SC;
661         }
662     }
663 
664     feature.respKeyDis = GAP_LE_KEY_DIST_ENC_KEY | GAP_LE_KEY_DIST_ID_KEY | GAP_LE_KEY_DIST_SIGN_KEY;
665     feature.initKeyDis = GAP_LE_KEY_DIST_ENC_KEY | GAP_LE_KEY_DIST_ID_KEY | GAP_LE_KEY_DIST_SIGN_KEY;
666 
667     int ret = GAPIF_LePairFeatureRsp(&param.lePairFeatureReq_.peerAddr, feature);
668     if (ret != BT_SUCCESS) {
669         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble pair feature rsp failed!");
670     }
671     return ret == BT_SUCCESS;
672 }
673 
PairRequestReply(const RawAddress &addr, int addrType, bool accept) const674 bool BleSecurity::PairRequestReply(const RawAddress &addr, int addrType, bool accept) const
675 {
676     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
677 
678     if (accept) {
679         BleGapCallbackParam param;
680         (void)memset_s(&param, sizeof(param), 0x00, sizeof(param));
681         addr.ConvertToUint8(param.lePairFeatureReq_.peerAddr.addr);
682         param.lePairFeatureReq_.peerAddr.type = addrType;
683         return LePairFeatureReq(param);
684     } else {
685         return CancelPairing(addr);
686     }
687 }
688 
GapLePairFeatureReq(const BleGapCallbackParam &param) const689 bool BleSecurity::GapLePairFeatureReq(const BleGapCallbackParam &param) const
690 {
691     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
692 
693     if (param.lePairFeatureReq_.localPair) {
694         return LePairFeatureReq(param);
695     } else {
696         if (baseCallback_ != nullptr) {
697             RawAddress addr = RawAddress::ConvertToString(param.lePairFeatureReq_.peerAddr.addr);
698             if (bleAdapter_ != nullptr) {
699                 (static_cast<BleAdapter *>(bleAdapter_))->LePairingStatus(addr);
700             }
701             baseCallback_->ForEach(
702                 [addr](IAdapterBleObserver &observer) { observer.OnPairRequested(BTTransport::ADAPTER_BLE, addr); });
703             return true;
704         }
705         return false;
706     }
707 }
708 
GapLePairFeatureInd(const BleGapCallbackParam &param) const709 bool BleSecurity::GapLePairFeatureInd(const BleGapCallbackParam &param) const
710 {
711     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
712 
713     RawAddress addr = RawAddress::ConvertToString(param.lePairFeatureInd_.addr.addr);
714     BleConfig::GetInstance().SetPeerDeviceIoCapability(
715         addr.GetAddress(), param.lePairFeatureInd_.remoteFrature.ioCapability);
716     return true;
717 }
718 
GapLePairMethodNotify(const BleGapCallbackParam &param) const719 bool BleSecurity::GapLePairMethodNotify(const BleGapCallbackParam &param) const
720 {
721     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
722 
723     pimpl->pairMethod_ = param.lePairMethodNotify_.pairMethod;
724     return true;
725 }
726 
GapLePairKeyPressNotification(const BleGapCallbackParam &param) const727 bool BleSecurity::GapLePairKeyPressNotification(const BleGapCallbackParam &param) const
728 {
729     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
730 
731     return true;
732 }
733 
GapLePairPassKeyReq(const BleGapCallbackParam &param) const734 bool BleSecurity::GapLePairPassKeyReq(const BleGapCallbackParam &param) const
735 {
736     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
737 
738     RawAddress addr = RawAddress::ConvertToString(param.lePairPassKeyReq_.addr.addr);
739     uint8_t pairMethod = pimpl->pairMethod_;
740     if (baseCallback_ != nullptr) {
741         baseCallback_->ForEach([addr, pairMethod](IAdapterBleObserver &observer) {
742             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, 0);
743         });
744     }
745     return true;
746 }
747 
GapLePairPassKeyNotification(const BleGapCallbackParam &param) const748 bool BleSecurity::GapLePairPassKeyNotification(const BleGapCallbackParam &param) const
749 {
750     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
751 
752     RawAddress addr = RawAddress::ConvertToString(param.lePairPassKeyNotification_.addr.addr);
753     uint8_t pairMethod = pimpl->pairMethod_;
754     uint32_t number = param.lePairPassKeyNotification_.number;
755     if (baseCallback_ != nullptr) {
756         baseCallback_->ForEach([addr, pairMethod, number](IAdapterBleObserver &observer) {
757             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, number);
758         });
759     }
760     return true;
761 }
762 
GapLePairOobReq(const BleGapCallbackParam &param) const763 bool BleSecurity::GapLePairOobReq(const BleGapCallbackParam &param) const
764 {
765     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
766 
767     RawAddress addr = RawAddress::ConvertToString(param.lePairOobReq_.addr.addr);
768     uint8_t pairMethod = pimpl->pairMethod_;
769     if (baseCallback_ != nullptr) {
770         baseCallback_->ForEach([addr, pairMethod](IAdapterBleObserver &observer) {
771             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, 0);
772         });
773     }
774     return true;
775 }
776 
GapLePairScOobReq(const BleGapCallbackParam &param) const777 bool BleSecurity::GapLePairScOobReq(const BleGapCallbackParam &param) const
778 {
779     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
780 
781     RawAddress addr = RawAddress::ConvertToString(param.lePairScOob_req_.addr.addr);
782     uint8_t pairMethod = pimpl->pairMethod_;
783     if (baseCallback_ != nullptr) {
784         baseCallback_->ForEach([addr, pairMethod](IAdapterBleObserver &observer) {
785             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, 0);
786         });
787     }
788     return true;
789 }
790 
GapLePairScUserConfirmReq(const BleGapCallbackParam &param) const791 bool BleSecurity::GapLePairScUserConfirmReq(const BleGapCallbackParam &param) const
792 {
793     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
794 
795     RawAddress addr = RawAddress::ConvertToString(param.lePairScUserConfirmReq_.addr.addr);
796     uint8_t pairMethod = pimpl->pairMethod_;
797     uint32_t number = param.lePairScUserConfirmReq_.number;
798     if (baseCallback_ != nullptr) {
799         baseCallback_->ForEach([addr, pairMethod, number](IAdapterBleObserver &observer) {
800             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, number);
801         });
802     }
803     return true;
804 }
805 
GapLePairComplete(const BleGapCallbackParam &param) const806 bool BleSecurity::GapLePairComplete(const BleGapCallbackParam &param) const
807 {
808     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
809 
810     RawAddress addr = RawAddress::ConvertToString(param.lePairComplete_.addr.addr);
811 
812     if (param.lePairComplete_.result == BT_SUCCESS) {
813         BleConfig::GetInstance().SetPeerKeyType(addr.GetAddress(), std::to_string(param.lePairComplete_.keyType));
814         BleConfig::GetInstance().Save();
815     }
816 
817     if (bleAdapter_ != nullptr) {
818         (static_cast<BleAdapter *>(bleAdapter_))->LePairComplete(addr, param.lePairComplete_.result);
819     }
820 
821     LOG_DEBUG("[BleSecurity] %{public}s:Ble pair comelete event result = %u", __func__, param.lePairComplete_.result);
822     return true;
823 }
824 
GapLePairKeyNotify(const BleGapCallbackParam &param) const825 bool BleSecurity::GapLePairKeyNotify(const BleGapCallbackParam &param) const
826 {
827     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
828 
829     return SavePairKeyNotify(param);
830 }
831 
GapLeRequestSecurityResultEvt(const BleGapCallbackParam &param) const832 bool BleSecurity::GapLeRequestSecurityResultEvt(const BleGapCallbackParam &param) const
833 {
834     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
835 
836     return true;
837 }
838 
GapLeRequestSecurity(uint16_t connectionHandle, const BtAddr &addr, uint8_t role)839 int BleSecurity::GapLeRequestSecurity(uint16_t connectionHandle, const BtAddr &addr, uint8_t role)
840 {
841     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
842 
843     int ret =
844         GAPIF_LeRequestSecurity(&addr, GAP_LE_AUTHENTICATED_ENCRYPTION, &BleSecurity::GapLeRequestSecurityResult, this);
845     return ret;
846 }
847 
HandleGapEvent(const BLE_GAP_CB_EVENT &event, const BleGapCallbackParam &param)848 void BleSecurity::HandleGapEvent(const BLE_GAP_CB_EVENT &event, const BleGapCallbackParam &param)
849 {
850     LOG_DEBUG("[BleSecurity] %{public}s:[event no: %{public}d].", __func__, static_cast<int>(event));
851     if (pimpl->funcMap_.find(event) == pimpl->funcMap_.end()) {
852         LOG_ERROR("[BleSecurity] %{public}s:[Not exist event no: %{public}d].", __func__, static_cast<int>(event));
853     } else {
854         (this->*pimpl->funcMap_[event])(param);
855     }
856 }
857 
RegisterCallbackToGap()858 int BleSecurity::RegisterCallbackToGap()
859 {
860     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
861     GapLeSecurityCallback gapCallbacks_ {};
862     GapLePairCallback gapPairCallbacks_ {};
863 
864     gapCallbacks_.encryptionComplete = &BleSecurity::EncryptionComplete;
865     gapCallbacks_.leLocalEncryptionKeyReqEvent = &BleSecurity::LeLocalEncryptionKeyReqEvent;
866     gapCallbacks_.leRemoteEncryptionKeyReqEvent = &BleSecurity::LeRemoteEncryptionKeyReqEvent;
867     gapCallbacks_.leSignCounterChangeNotification = &BleSecurity::LeSignCounterChangeNotification;
868     gapCallbacks_.GapRequestSigningAlgorithmInfo = &BleSecurity::GapRequestSigningAlgorithmInfo;
869     int ret = GAPIF_RegisterLeSecurityCallback(&gapCallbacks_, this);
870 
871     gapPairCallbacks_.lePairFeatureReq = &BleSecurity::LePairFeatureReq;
872     gapPairCallbacks_.lePairFeatureInd = &BleSecurity::LePairFeatureInd;
873     gapPairCallbacks_.lePairMethodNotify = &BleSecurity::LePairMethodNotify;
874     gapPairCallbacks_.lePairKeyPressNotification = &BleSecurity::LePairKeyPressNotification;
875     gapPairCallbacks_.lePairPassKeyReq = &BleSecurity::LePairPassKeyReq;
876     gapPairCallbacks_.lePairPassKeyNotification = &BleSecurity::LePairPassKeyNotification;
877     gapPairCallbacks_.lePairOobReq = &BleSecurity::LePairOobReq;
878     gapPairCallbacks_.lePairScOobReq = &BleSecurity::LePairScOobReq;
879     gapPairCallbacks_.lePairScUserConfirmReq = &BleSecurity::LePairScUserConfirmReq;
880     gapPairCallbacks_.lePairComplete = &BleSecurity::LePairComplete;
881     gapPairCallbacks_.lePairKeyNotify = &BleSecurity::LePairKeyNotify;
882     ret &= GAPIF_RegisterLePairCallback(&gapPairCallbacks_, this);
883     return ret;
884 }
885 
DeregisterCallbackToGap() const886 int BleSecurity::DeregisterCallbackToGap() const
887 {
888     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
889 
890     int ret = GAPIF_DeregisterLeSecurityCallback();
891     ret &= GAPIF_DeregisterLePairCallback();
892     return ret;
893 }
894 
StartPair(const RawAddress &device, uint8_t peerAddrType)895 bool BleSecurity::StartPair(const RawAddress &device, uint8_t peerAddrType)
896 {
897     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
898 
899     BtAddr addr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, peerAddrType};
900     device.ConvertToUint8(addr.addr);
901     int ret = GAPIF_LePair(&addr);
902     if (ret != BT_SUCCESS) {
903         LOG_ERROR("[BleSecurity] %{public}s:GAP_LePair failed!", __func__);
904     }
905     return (ret == BT_SUCCESS);
906 }
907 
SetDevicePasskey(const RawAddress &device, int passkey, int accept) const908 int BleSecurity::SetDevicePasskey(const RawAddress &device, int passkey, int accept) const
909 {
910     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
911 
912     if (bleAdapter_ == nullptr) {
913         return BT_BAD_PARAM;
914     }
915     uint8_t peerAddrType = (uint8_t)bleAdapter_->GetPeerDeviceAddrType(device);
916     BtAddr addr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, peerAddrType};
917     device.ConvertToUint8(addr.addr);
918     int ret = GAPIF_LePairPassKeyRsp(&addr, accept, passkey);
919     if (ret != BT_SUCCESS) {
920         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble pair passkey rsp failed!");
921     }
922     return ret;
923 }
924 
SetUserConfirm(const RawAddress &device, int accept) const925 int BleSecurity::SetUserConfirm(const RawAddress &device, int accept) const
926 {
927     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
928 
929     if (bleAdapter_ == nullptr) {
930         return BT_BAD_PARAM;
931     }
932     uint8_t peerAddrType = (uint8_t)bleAdapter_->GetPeerDeviceAddrType(device);
933     BtAddr addr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, peerAddrType};
934     device.ConvertToUint8(addr.addr);
935     int ret = GAPIF_LePairScUserConfirmRsp(&addr, accept);
936     if (ret != BT_SUCCESS) {
937         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble pair user confirm failed!");
938     }
939     return ret;
940 }
941 
CancelPairing(const RawAddress &device) const942 int BleSecurity::CancelPairing(const RawAddress &device) const
943 {
944     if (bleAdapter_ == nullptr) {
945         return BT_BAD_PARAM;
946     }
947     uint8_t peerAddrType = (uint8_t)bleAdapter_->GetPeerDeviceAddrType(device);
948     BtAddr addr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, peerAddrType};
949     device.ConvertToUint8(addr.addr);
950     return GAPIF_LeCancelPair(&addr);
951 }
952 }  // namespace bluetooth
953 }  // namespace OHOS