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         if ((!INVALID_MAC_ADDRESS.compare(rawAddr.GetAddress())) || (rawAddr.GetAddress().empty())) {
434             continue;
435         }
436         std::string peerIdentityAddr = BleConfig::GetInstance().GetPeerIdentityAddr(rawAddr.GetAddress());
437         if (param.lePairKeyNotify_.hasRemoteIdKey) {
438             RawAddress peerAddr = RawAddress::ConvertToString(param.lePairKeyNotify_.remoteIdKey.identityAddr.addr);
439             if (peerIdentityAddr.compare(peerAddr.GetAddress()) == 0) {
440                 BleConfig::GetInstance().RemovePairedDevice(rawAddr.GetAddress());
441             }
442         }
443     }
444     ret = SaveLocalPairKey(addr, param);
445     ret &= SavePeerPairKey(addr, param);
446     return ret;
447 }
448 
SaveLocalPairKey(const RawAddress & addr,const BleGapCallbackParam & param)449 bool BleSecurity::SaveLocalPairKey(const RawAddress &addr, const BleGapCallbackParam &param)
450 {
451     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
452 
453     bool ret = true;
454     // local
455     if (param.lePairKeyNotify_.hasLocalEncKey) {
456         ret &= BleConfig::GetInstance().SetLocalEdivRand(addr.GetAddress(),
457             std::to_string(param.lePairKeyNotify_.localEncKey.ediv),
458             std::to_string(param.lePairKeyNotify_.localEncKey.rand));
459         std::vector<uint8_t> ltk(
460             param.lePairKeyNotify_.localEncKey.ltk, param.lePairKeyNotify_.localEncKey.ltk + GAP_LTK_SIZE);
461         ret &= BleConfig::GetInstance().SetLocalLtk(addr.GetAddress(), BleUtils::ConvertIntToHexString(ltk));
462         ret &= BleConfig::GetInstance().SetLocalKeySize(
463             addr.GetAddress(), std::to_string(param.lePairKeyNotify_.localEncKey.keySize));
464     }
465 
466     if (param.lePairKeyNotify_.hasLocalSignKey) {
467         std::vector<uint8_t> csrk(
468             param.lePairKeyNotify_.localSignKey.csrk, param.lePairKeyNotify_.localSignKey.csrk + GAP_LTK_SIZE);
469         ret &= BleConfig::GetInstance().SetLocalCsrk(addr.GetAddress(), BleUtils::ConvertIntToHexString(csrk));
470         ret &= BleConfig::GetInstance().SetLocalSignCounter(
471             addr.GetAddress(), param.lePairKeyNotify_.localSignKey.counter);
472     }
473     return ret;
474 }
475 
SavePeerPairKey(const RawAddress & addr,const BleGapCallbackParam & param)476 bool BleSecurity::SavePeerPairKey(const RawAddress &addr, const BleGapCallbackParam &param)
477 {
478     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
479 
480     bool ret = true;
481     // peer
482     if (param.lePairKeyNotify_.hasRemoteEncKey) {
483         ret &= BleConfig::GetInstance().SetPeerEdivRand(addr.GetAddress(),
484             std::to_string(param.lePairKeyNotify_.remoteEncKey.ediv),
485             std::to_string(param.lePairKeyNotify_.remoteEncKey.rand));
486         std::vector<uint8_t> ltk(
487             param.lePairKeyNotify_.remoteEncKey.ltk, param.lePairKeyNotify_.remoteEncKey.ltk + GAP_LTK_SIZE);
488         ret &= BleConfig::GetInstance().SetPeerLtk(addr.GetAddress(), BleUtils::ConvertIntToHexString(ltk));
489         ret &= BleConfig::GetInstance().SetPeerKeySize(
490             addr.GetAddress(), std::to_string(param.lePairKeyNotify_.remoteEncKey.keySize));
491     }
492 
493     if (param.lePairKeyNotify_.hasRemoteIdKey) {
494         RawAddress peerAddr = RawAddress::ConvertToString(param.lePairKeyNotify_.remoteIdKey.identityAddr.addr);
495         ret &= BleConfig::GetInstance().SetPeerIdentityAddr(
496             addr.GetAddress(), param.lePairKeyNotify_.remoteIdKey.identityAddr.type, peerAddr.GetAddress());
497         std::vector<uint8_t> irk(
498             param.lePairKeyNotify_.remoteIdKey.irk, param.lePairKeyNotify_.remoteIdKey.irk + GAP_LTK_SIZE);
499         ret &= BleConfig::GetInstance().SetPeerIrk(addr.GetAddress(), BleUtils::ConvertIntToHexString(irk));
500     }
501 
502     if (param.lePairKeyNotify_.hasRemoteSignKey) {
503         std::vector<uint8_t> csrk(
504             param.lePairKeyNotify_.remoteSignKey.csrk, param.lePairKeyNotify_.remoteSignKey.csrk + GAP_LTK_SIZE);
505         ret &= BleConfig::GetInstance().SetPeerCsrk(addr.GetAddress(), BleUtils::ConvertIntToHexString(csrk));
506         ret &= BleConfig::GetInstance().SetPeerSignCounter(
507             addr.GetAddress(), param.lePairKeyNotify_.remoteSignKey.counter);
508     }
509     return ret;
510 }
511 
GapEncryptionComplete(const BleGapCallbackParam & param) const512 bool BleSecurity::GapEncryptionComplete(const BleGapCallbackParam &param) const
513 {
514     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
515 
516     if (param.encryptionComplete_.status != BT_SUCCESS) {
517         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble encryption failed!");
518         return false;
519     } else {
520         RawAddress addr = RawAddress::ConvertToString(param.encryptionComplete_.peerAddr.addr);
521         if (bleAdapter_ != nullptr) {
522             (static_cast<BleAdapter *>(bleAdapter_))->EncryptionComplete(addr);
523         }
524         LOG_DEBUG("[BleSecurity] %{public}s:%{public}s", __func__, "Ble encryption success!");
525     }
526     return true;
527 }
528 
GapLeLocalEncryptionKeyReqEvent(const BleGapCallbackParam & param) const529 bool BleSecurity::GapLeLocalEncryptionKeyReqEvent(const BleGapCallbackParam &param) const
530 {
531     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
532 
533     RawAddress addr = RawAddress::ConvertToString(param.leLocalEncryptionKeyReqEvent_.addr.addr);
534     int accept = GAP_NOT_ACCEPT;
535     LeEncKey encKey;
536     (void)memset_s(&encKey, sizeof(encKey), 0x00, sizeof(encKey));
537     std::string ltk = BleConfig::GetInstance().GetLocalLtk(addr.GetAddress());
538     std::string rand = BleConfig::GetInstance().GetLocalRand(addr.GetAddress());
539     std::string ediv = BleConfig::GetInstance().GetLocalEdiv(addr.GetAddress());
540     if ((rand.compare(std::to_string(param.leLocalEncryptionKeyReqEvent_.rand)) == 0) &&
541         (ediv.compare(std::to_string(param.leLocalEncryptionKeyReqEvent_.ediv)) == 0) && (!ltk.empty())) {
542         accept = GAP_ACCEPT;
543         std::vector<uint8_t> vec;
544         BleUtils::ConvertHexStringToInt(ltk, vec);
545         (void)memcpy_s(encKey.ltk, GAP_CSRK_SIZE, &vec[0], vec.size());
546         encKey.rand = std::stoull(rand);
547         encKey.ediv = std::stoull(ediv);
548     }
549 
550     int ret = GAPIF_LeLocalEncryptionKeyRsp(&param.leLocalEncryptionKeyReqEvent_.addr, accept, encKey, 1);
551     if (ret != BT_SUCCESS) {
552         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble remote encryption key rsp failed!");
553     }
554     (void)memset_s(&encKey, sizeof(encKey), 0x00, sizeof(encKey));
555     return ret;
556 }
557 
GapLeRemoteEncryptionKeyReqEvent(const BleGapCallbackParam & param) const558 bool BleSecurity::GapLeRemoteEncryptionKeyReqEvent(const BleGapCallbackParam &param) const
559 {
560     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
561 
562     RawAddress addr = RawAddress::ConvertToString(param.leRemoteEncryptionKeyReqEvent_.addr.addr);
563     int accept = GAP_NOT_ACCEPT;
564     LeEncKey encKey;
565     (void)memset_s(&encKey, sizeof(encKey), 0x00, sizeof(encKey));
566     std::string ltk = BleConfig::GetInstance().GetPeerLtk(addr.GetAddress());
567     std::string rand = BleConfig::GetInstance().GetPeerRand(addr.GetAddress());
568     std::string ediv = BleConfig::GetInstance().GetPeerEdiv(addr.GetAddress());
569     if ((!rand.empty()) && (!ediv.empty()) && (!ltk.empty())) {
570         accept = GAP_ACCEPT;
571         std::vector<uint8_t> vec;
572         BleUtils::ConvertHexStringToInt(ltk, vec);
573         (void)memcpy_s(encKey.ltk, GAP_CSRK_SIZE, &vec[0], vec.size());
574         encKey.rand = std::stoull(rand);
575         encKey.ediv = std::stoull(ediv);
576     }
577 
578     int ret = GAPIF_LeRemoteEncryptionKeyRsp(&param.leRemoteEncryptionKeyReqEvent_.addr, accept, encKey, 1);
579     if (ret != BT_SUCCESS) {
580         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble remote encryption key rsp failed!");
581     }
582     (void)memset_s(&encKey, sizeof(encKey), 0x00, sizeof(encKey));
583     return ret;
584 }
585 
GapLeSignCounterChangeNotification(const BleGapCallbackParam & param) const586 bool BleSecurity::GapLeSignCounterChangeNotification(const BleGapCallbackParam &param) const
587 {
588     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
589 
590     RawAddress addr = RawAddress::ConvertToString(param.leSignCounterChangeNotification_.addr.addr);
591 
592     if (param.leSignCounterChangeNotification_.type == LOCAL_SIGN_COUNTER) {
593         BleConfig::GetInstance().SetLocalSignCounter(addr.GetAddress(), param.leSignCounterChangeNotification_.counter);
594     } else {
595         BleConfig::GetInstance().SetPeerSignCounter(addr.GetAddress(), param.leSignCounterChangeNotification_.counter);
596     }
597     return true;
598 }
599 
GapRequestSigningAlgorithmInfoEvt(const BleGapCallbackParam & param) const600 bool BleSecurity::GapRequestSigningAlgorithmInfoEvt(const BleGapCallbackParam &param) const
601 {
602     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
603 
604     RawAddress addr = RawAddress::ConvertToString(param.gapRequestSigningAlgorithmInfo_.addr.addr);
605     int accept = GAP_NOT_ACCEPT;
606     GapSigningAlgorithmInfo info;
607     (void)memset_s(&info, sizeof(info), 0x00, sizeof(info));
608     LeSignKey localKey;
609     (void)memset_s(&localKey, sizeof(localKey), 0x00, sizeof(localKey));
610 
611     if (!BleConfig::GetInstance().GetLocalCsrk(addr.GetAddress()).empty()) {
612         localKey.counter = BleConfig::GetInstance().GetLocalSignCounter(addr.GetAddress());
613         std::vector<uint8_t> vec;
614         BleUtils::ConvertHexStringToInt(BleConfig::GetInstance().GetLocalCsrk(addr.GetAddress()), vec);
615         (void)memcpy_s(localKey.csrk, GAP_CSRK_SIZE, &vec[0], vec.size());
616         info.localKey = &localKey;
617         accept = GAP_ACCEPT;
618     }
619 
620     LeSignKey remoteKey;
621     if (!BleConfig::GetInstance().GetPeerCsrk(addr.GetAddress()).empty()) {
622         remoteKey.counter = BleConfig::GetInstance().GetPeerSignCounter(addr.GetAddress());
623         std::vector<uint8_t> vec;
624         BleUtils::ConvertHexStringToInt(BleConfig::GetInstance().GetPeerCsrk(addr.GetAddress()), vec);
625         (void)memcpy_s(remoteKey.csrk, GAP_CSRK_SIZE, &vec[0], vec.size());
626         info.remoteKey = &remoteKey;
627         accept &= GAP_ACCEPT;
628     }
629 
630     int ret = GAPIF_RequestSigningAlgorithmInfoRsp(&param.gapRequestSigningAlgorithmInfo_.addr, accept, info);
631     if (ret != BT_SUCCESS) {
632         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble request signing algorithm info rsp failed!");
633     }
634     return ret;
635 }
636 
LePairFeatureReq(const BleGapCallbackParam & param)637 bool BleSecurity::LePairFeatureReq(const BleGapCallbackParam &param)
638 {
639     RawAddress addr = RawAddress::ConvertToString(param.lePairFeatureReq_.peerAddr.addr);
640     GapLePairFeature feature;
641     (void)memset_s(&feature, sizeof(feature), 0x00, sizeof(feature));
642     feature.ioCapability = BleConfig::GetInstance().GetIoCapability();
643     feature.maxEncKeySize = GAP_LINKKEY_SIZE;
644 
645     if (BleConfig::GetInstance().GetBleSecurity()) {
646         feature.authReq = GAP_LE_AUTH_REQ_BONDING | GAP_LE_AUTH_REQ_BIT_MITM | GAP_LE_AUTH_REQ_BIT_SC;
647     } else {
648         feature.authReq = GAP_LE_AUTH_REQ_BONDING | GAP_LE_AUTH_REQ_BIT_MITM;
649     }
650 
651     /// Compat check
652     GAP_LeSecMode1Level level1 = static_cast<GAP_LeSecMode1Level>(BleConfig::GetInstance().GetBleModel1Level());
653     if ((level1 != LE_MODE_1_LEVEL_4) &&
654         (Compat::CompatCheck(CompatType::COMPAT_DISABLE_BLE_SECURE_CONNECTIONS, addr.GetAddress()))) {
655         BtmLocalVersionInformation version;
656         (void)memset_s(&version, sizeof(version), 0x00, sizeof(version));
657         if ((BTM_GetLocalVersionInformation(&version) == BT_SUCCESS) &&
658             (version.hciVersion >= BLUETOOTH_CORE_SPECIFICATION_4_2)) {
659             feature.authReq &= ~GAP_LE_AUTH_REQ_BIT_SC;
660         }
661     }
662 
663     feature.respKeyDis = GAP_LE_KEY_DIST_ENC_KEY | GAP_LE_KEY_DIST_ID_KEY | GAP_LE_KEY_DIST_SIGN_KEY;
664     feature.initKeyDis = GAP_LE_KEY_DIST_ENC_KEY | GAP_LE_KEY_DIST_ID_KEY | GAP_LE_KEY_DIST_SIGN_KEY;
665 
666     int ret = GAPIF_LePairFeatureRsp(&param.lePairFeatureReq_.peerAddr, feature);
667     if (ret != BT_SUCCESS) {
668         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble pair feature rsp failed!");
669     }
670     return ret == BT_SUCCESS;
671 }
672 
PairRequestReply(const RawAddress & addr,int addrType,bool accept) const673 bool BleSecurity::PairRequestReply(const RawAddress &addr, int addrType, bool accept) const
674 {
675     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
676 
677     if (accept) {
678         BleGapCallbackParam param;
679         (void)memset_s(&param, sizeof(param), 0x00, sizeof(param));
680         addr.ConvertToUint8(param.lePairFeatureReq_.peerAddr.addr);
681         param.lePairFeatureReq_.peerAddr.type = addrType;
682         return LePairFeatureReq(param);
683     } else {
684         return CancelPairing(addr);
685     }
686 }
687 
GapLePairFeatureReq(const BleGapCallbackParam & param) const688 bool BleSecurity::GapLePairFeatureReq(const BleGapCallbackParam &param) const
689 {
690     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
691 
692     if (param.lePairFeatureReq_.localPair) {
693         return LePairFeatureReq(param);
694     } else {
695         if (baseCallback_ != nullptr) {
696             RawAddress addr = RawAddress::ConvertToString(param.lePairFeatureReq_.peerAddr.addr);
697             if (bleAdapter_ != nullptr) {
698                 (static_cast<BleAdapter *>(bleAdapter_))->LePairingStatus(addr);
699             }
700             baseCallback_->ForEach(
701                 [addr](IAdapterBleObserver &observer) { observer.OnPairRequested(BTTransport::ADAPTER_BLE, addr); });
702             return true;
703         }
704         return false;
705     }
706 }
707 
GapLePairFeatureInd(const BleGapCallbackParam & param) const708 bool BleSecurity::GapLePairFeatureInd(const BleGapCallbackParam &param) const
709 {
710     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
711 
712     RawAddress addr = RawAddress::ConvertToString(param.lePairFeatureInd_.addr.addr);
713     BleConfig::GetInstance().SetPeerDeviceIoCapability(
714         addr.GetAddress(), param.lePairFeatureInd_.remoteFrature.ioCapability);
715     return true;
716 }
717 
GapLePairMethodNotify(const BleGapCallbackParam & param) const718 bool BleSecurity::GapLePairMethodNotify(const BleGapCallbackParam &param) const
719 {
720     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
721 
722     pimpl->pairMethod_ = param.lePairMethodNotify_.pairMethod;
723     return true;
724 }
725 
GapLePairKeyPressNotification(const BleGapCallbackParam & param) const726 bool BleSecurity::GapLePairKeyPressNotification(const BleGapCallbackParam &param) const
727 {
728     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
729 
730     return true;
731 }
732 
GapLePairPassKeyReq(const BleGapCallbackParam & param) const733 bool BleSecurity::GapLePairPassKeyReq(const BleGapCallbackParam &param) const
734 {
735     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
736 
737     RawAddress addr = RawAddress::ConvertToString(param.lePairPassKeyReq_.addr.addr);
738     uint8_t pairMethod = pimpl->pairMethod_;
739     if (baseCallback_ != nullptr) {
740         baseCallback_->ForEach([addr, pairMethod](IAdapterBleObserver &observer) {
741             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, 0);
742         });
743     }
744     return true;
745 }
746 
GapLePairPassKeyNotification(const BleGapCallbackParam & param) const747 bool BleSecurity::GapLePairPassKeyNotification(const BleGapCallbackParam &param) const
748 {
749     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
750 
751     RawAddress addr = RawAddress::ConvertToString(param.lePairPassKeyNotification_.addr.addr);
752     uint8_t pairMethod = pimpl->pairMethod_;
753     uint32_t number = param.lePairPassKeyNotification_.number;
754     if (baseCallback_ != nullptr) {
755         baseCallback_->ForEach([addr, pairMethod, number](IAdapterBleObserver &observer) {
756             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, number);
757         });
758     }
759     return true;
760 }
761 
GapLePairOobReq(const BleGapCallbackParam & param) const762 bool BleSecurity::GapLePairOobReq(const BleGapCallbackParam &param) const
763 {
764     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
765 
766     RawAddress addr = RawAddress::ConvertToString(param.lePairOobReq_.addr.addr);
767     uint8_t pairMethod = pimpl->pairMethod_;
768     if (baseCallback_ != nullptr) {
769         baseCallback_->ForEach([addr, pairMethod](IAdapterBleObserver &observer) {
770             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, 0);
771         });
772     }
773     return true;
774 }
775 
GapLePairScOobReq(const BleGapCallbackParam & param) const776 bool BleSecurity::GapLePairScOobReq(const BleGapCallbackParam &param) const
777 {
778     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
779 
780     RawAddress addr = RawAddress::ConvertToString(param.lePairScOob_req_.addr.addr);
781     uint8_t pairMethod = pimpl->pairMethod_;
782     if (baseCallback_ != nullptr) {
783         baseCallback_->ForEach([addr, pairMethod](IAdapterBleObserver &observer) {
784             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, 0);
785         });
786     }
787     return true;
788 }
789 
GapLePairScUserConfirmReq(const BleGapCallbackParam & param) const790 bool BleSecurity::GapLePairScUserConfirmReq(const BleGapCallbackParam &param) const
791 {
792     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
793 
794     RawAddress addr = RawAddress::ConvertToString(param.lePairScUserConfirmReq_.addr.addr);
795     uint8_t pairMethod = pimpl->pairMethod_;
796     uint32_t number = param.lePairScUserConfirmReq_.number;
797     if (baseCallback_ != nullptr) {
798         baseCallback_->ForEach([addr, pairMethod, number](IAdapterBleObserver &observer) {
799             observer.OnPairConfirmed(BTTransport::ADAPTER_BLE, addr, pairMethod, number);
800         });
801     }
802     return true;
803 }
804 
GapLePairComplete(const BleGapCallbackParam & param) const805 bool BleSecurity::GapLePairComplete(const BleGapCallbackParam &param) const
806 {
807     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
808 
809     RawAddress addr = RawAddress::ConvertToString(param.lePairComplete_.addr.addr);
810 
811     if (param.lePairComplete_.result == BT_SUCCESS) {
812         BleConfig::GetInstance().SetPeerKeyType(addr.GetAddress(), std::to_string(param.lePairComplete_.keyType));
813         BleConfig::GetInstance().Save();
814     }
815 
816     if (bleAdapter_ != nullptr) {
817         (static_cast<BleAdapter *>(bleAdapter_))->LePairComplete(addr, param.lePairComplete_.result);
818     }
819 
820     LOG_DEBUG("[BleSecurity] %{public}s:Ble pair comelete event result = %u", __func__, param.lePairComplete_.result);
821     return true;
822 }
823 
GapLePairKeyNotify(const BleGapCallbackParam & param) const824 bool BleSecurity::GapLePairKeyNotify(const BleGapCallbackParam &param) const
825 {
826     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
827 
828     return SavePairKeyNotify(param);
829 }
830 
GapLeRequestSecurityResultEvt(const BleGapCallbackParam & param) const831 bool BleSecurity::GapLeRequestSecurityResultEvt(const BleGapCallbackParam &param) const
832 {
833     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
834 
835     return true;
836 }
837 
GapLeRequestSecurity(uint16_t connectionHandle,const BtAddr & addr,uint8_t role)838 int BleSecurity::GapLeRequestSecurity(uint16_t connectionHandle, const BtAddr &addr, uint8_t role)
839 {
840     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
841 
842     int ret =
843         GAPIF_LeRequestSecurity(&addr, GAP_LE_AUTHENTICATED_ENCRYPTION, &BleSecurity::GapLeRequestSecurityResult, this);
844     return ret;
845 }
846 
HandleGapEvent(const BLE_GAP_CB_EVENT & event,const BleGapCallbackParam & param)847 void BleSecurity::HandleGapEvent(const BLE_GAP_CB_EVENT &event, const BleGapCallbackParam &param)
848 {
849     LOG_DEBUG("[BleSecurity] %{public}s:[event no: %{public}d].", __func__, static_cast<int>(event));
850     if (pimpl->funcMap_.find(event) == pimpl->funcMap_.end()) {
851         LOG_ERROR("[BleSecurity] %{public}s:[Not exist event no: %{public}d].", __func__, static_cast<int>(event));
852     } else {
853         (this->*pimpl->funcMap_[event])(param);
854     }
855 }
856 
RegisterCallbackToGap()857 int BleSecurity::RegisterCallbackToGap()
858 {
859     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
860     GapLeSecurityCallback gapCallbacks_ {};
861     GapLePairCallback gapPairCallbacks_ {};
862 
863     gapCallbacks_.encryptionComplete = &BleSecurity::EncryptionComplete;
864     gapCallbacks_.leLocalEncryptionKeyReqEvent = &BleSecurity::LeLocalEncryptionKeyReqEvent;
865     gapCallbacks_.leRemoteEncryptionKeyReqEvent = &BleSecurity::LeRemoteEncryptionKeyReqEvent;
866     gapCallbacks_.leSignCounterChangeNotification = &BleSecurity::LeSignCounterChangeNotification;
867     gapCallbacks_.GapRequestSigningAlgorithmInfo = &BleSecurity::GapRequestSigningAlgorithmInfo;
868     int ret = GAPIF_RegisterLeSecurityCallback(&gapCallbacks_, this);
869 
870     gapPairCallbacks_.lePairFeatureReq = &BleSecurity::LePairFeatureReq;
871     gapPairCallbacks_.lePairFeatureInd = &BleSecurity::LePairFeatureInd;
872     gapPairCallbacks_.lePairMethodNotify = &BleSecurity::LePairMethodNotify;
873     gapPairCallbacks_.lePairKeyPressNotification = &BleSecurity::LePairKeyPressNotification;
874     gapPairCallbacks_.lePairPassKeyReq = &BleSecurity::LePairPassKeyReq;
875     gapPairCallbacks_.lePairPassKeyNotification = &BleSecurity::LePairPassKeyNotification;
876     gapPairCallbacks_.lePairOobReq = &BleSecurity::LePairOobReq;
877     gapPairCallbacks_.lePairScOobReq = &BleSecurity::LePairScOobReq;
878     gapPairCallbacks_.lePairScUserConfirmReq = &BleSecurity::LePairScUserConfirmReq;
879     gapPairCallbacks_.lePairComplete = &BleSecurity::LePairComplete;
880     gapPairCallbacks_.lePairKeyNotify = &BleSecurity::LePairKeyNotify;
881     ret &= GAPIF_RegisterLePairCallback(&gapPairCallbacks_, this);
882     return ret;
883 }
884 
DeregisterCallbackToGap() const885 int BleSecurity::DeregisterCallbackToGap() const
886 {
887     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
888 
889     int ret = GAPIF_DeregisterLeSecurityCallback();
890     ret &= GAPIF_DeregisterLePairCallback();
891     return ret;
892 }
893 
StartPair(const RawAddress & device,uint8_t peerAddrType)894 bool BleSecurity::StartPair(const RawAddress &device, uint8_t peerAddrType)
895 {
896     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
897 
898     BtAddr addr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, peerAddrType};
899     device.ConvertToUint8(addr.addr);
900     int ret = GAPIF_LePair(&addr);
901     if (ret != BT_SUCCESS) {
902         LOG_ERROR("[BleSecurity] %{public}s:GAP_LePair failed!", __func__);
903     }
904     return (ret == BT_SUCCESS);
905 }
906 
SetDevicePasskey(const RawAddress & device,int passkey,int accept) const907 int BleSecurity::SetDevicePasskey(const RawAddress &device, int passkey, int accept) const
908 {
909     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
910 
911     if (bleAdapter_ == nullptr) {
912         return BT_BAD_PARAM;
913     }
914     uint8_t peerAddrType = (uint8_t)bleAdapter_->GetPeerDeviceAddrType(device);
915     BtAddr addr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, peerAddrType};
916     device.ConvertToUint8(addr.addr);
917     int ret = GAPIF_LePairPassKeyRsp(&addr, accept, passkey);
918     if (ret != BT_SUCCESS) {
919         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble pair passkey rsp failed!");
920     }
921     return ret;
922 }
923 
SetUserConfirm(const RawAddress & device,int accept) const924 int BleSecurity::SetUserConfirm(const RawAddress &device, int accept) const
925 {
926     LOG_DEBUG("[BleSecurity] %{public}s", __func__);
927 
928     if (bleAdapter_ == nullptr) {
929         return BT_BAD_PARAM;
930     }
931     uint8_t peerAddrType = (uint8_t)bleAdapter_->GetPeerDeviceAddrType(device);
932     BtAddr addr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, peerAddrType};
933     device.ConvertToUint8(addr.addr);
934     int ret = GAPIF_LePairScUserConfirmRsp(&addr, accept);
935     if (ret != BT_SUCCESS) {
936         LOG_ERROR("[BleSecurity] %{public}s:%{public}s", __func__, "Ble pair user confirm failed!");
937     }
938     return ret;
939 }
940 
CancelPairing(const RawAddress & device) const941 int BleSecurity::CancelPairing(const RawAddress &device) const
942 {
943     if (bleAdapter_ == nullptr) {
944         return BT_BAD_PARAM;
945     }
946     uint8_t peerAddrType = (uint8_t)bleAdapter_->GetPeerDeviceAddrType(device);
947     BtAddr addr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, peerAddrType};
948     device.ConvertToUint8(addr.addr);
949     return GAPIF_LeCancelPair(&addr);
950 }
951 }  // namespace bluetooth
952 }  // namespace OHOS