1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BLE_SECURITY_H 17 #define BLE_SECURITY_H 18 19 #include <map> 20 21 #include "base_observer_list.h" 22 #include "ble_defs.h" 23 #include "dispatcher.h" 24 #include "gap_le_if.h" 25 #include "interface_adapter_ble.h" 26 27 /* 28 * @brief The bluetooth system. 29 */ 30 namespace bluetooth { 31 /* 32 * @brief BLE filter. 33 */ 34 class BleSecurity { 35 public: 36 /** 37 * @brief Constructor. 38 */ 39 explicit BleSecurity( 40 IAdapterBle &bleAdapter, utility::Dispatcher &dispatch, BaseObserverList<IAdapterBleObserver> &observer); 41 42 /** 43 * @brief Destructor. 44 */ 45 virtual ~BleSecurity(); 46 47 static bool StartPair(const RawAddress &device, uint8_t peerAddrType = BT_PUBLIC_DEVICE_ADDRESS); 48 int SetDevicePasskey(const RawAddress &device, int passkey, int accept) const; 49 int SetUserConfirm(const RawAddress &device, int accept) const; 50 int GapLeRequestSecurity(uint16_t connectionHandle, const BtAddr &addr, uint8_t role); 51 int CancelPairing(const RawAddress &device) const; 52 bool PairRequestReply(const RawAddress &addr, int addrType, bool accept) const; 53 54 /** 55 * @brief Register avertising callback to gap 56 * 57 * @return @c status. 58 */ 59 int RegisterCallbackToGap(); 60 61 /** 62 * @brief Deregister avertising callback to gap 63 * 64 * @return @c status. 65 */ 66 int DeregisterCallbackToGap() const; 67 68 private: 69 // gap callback 70 static void EncryptionComplete(uint8_t status, const BtAddr *peerAddr, void *context); 71 static void LeLocalEncryptionKeyReqEvent(const BtAddr *addr, uint64_t rand, uint16_t ediv, void *context); 72 static void LeRemoteEncryptionKeyReqEvent(const BtAddr *addr, void *context); 73 static void LeSignCounterChangeNotification( 74 const BtAddr *addr, GAP_SignCounterType type, uint32_t counter, void *context); 75 static void GapRequestSigningAlgorithmInfo(const BtAddr *addr, void *context); 76 77 static void LePairFeatureReq(const BtAddr *peerAddr, bool localPair, void *context); 78 static void LePairFeatureInd(const BtAddr *addr, GapLePairFeature remoteFrature, void *context); 79 static void LePairMethodNotify(const BtAddr *addr, uint8_t pairMethod, void *context); 80 static void LePairKeyPressNotification(const BtAddr *addr, uint8_t pressType, void *context); 81 static void LePairPassKeyReq(const BtAddr *addr, void *context); 82 static void LePairPassKeyNotification(const BtAddr *addr, uint32_t number, void *context); 83 static void LePairOobReq(const BtAddr *addr, void *context); 84 static void LePairScOobReq(const BtAddr *addr, void *context); 85 static void LePairScUserConfirmReq(const BtAddr *addr, uint32_t number, void *context); 86 static void LePairComplete(const BtAddr *addr, uint8_t result, uint8_t keyType, void *context); 87 static void LePairKeyNotify(const BtAddr *addr, LePairedKeys leKeys, void *context); 88 static void GapLeRequestSecurityResult( 89 const BtAddr *addr, uint8_t result, GAP_LeSecurityStatus status, void *context); 90 /** 91 * @brief Internal status 92 * 93 * @param [in] event gap event. 94 * @param [in] status gap callback status. 95 */ 96 void HandleGapEvent(const BLE_GAP_CB_EVENT &event, const BleGapCallbackParam ¶m); 97 bool SavePairKeyNotify(const BleGapCallbackParam ¶m) const; 98 static bool SaveLocalPairKey(const RawAddress &addr, const BleGapCallbackParam ¶m); 99 static bool SavePeerPairKey(const RawAddress &addr, const BleGapCallbackParam ¶m); 100 bool GapEncryptionComplete(const BleGapCallbackParam ¶m) const; 101 bool GapLeLocalEncryptionKeyReqEvent(const BleGapCallbackParam ¶m) const; 102 bool GapLeRemoteEncryptionKeyReqEvent(const BleGapCallbackParam ¶m) const; 103 bool GapLeSignCounterChangeNotification(const BleGapCallbackParam ¶m) const; 104 bool GapRequestSigningAlgorithmInfoEvt(const BleGapCallbackParam ¶m) const; 105 bool GapLePairFeatureReq(const BleGapCallbackParam ¶m) const; 106 bool GapLePairFeatureInd(const BleGapCallbackParam ¶m) const; 107 bool GapLePairMethodNotify(const BleGapCallbackParam ¶m) const; 108 bool GapLePairKeyPressNotification(const BleGapCallbackParam ¶m) const; 109 bool GapLePairPassKeyReq(const BleGapCallbackParam ¶m) const; 110 bool GapLePairPassKeyNotification(const BleGapCallbackParam ¶m) const; 111 bool GapLePairOobReq(const BleGapCallbackParam ¶m) const; 112 bool GapLePairScOobReq(const BleGapCallbackParam ¶m) const; 113 bool GapLePairScUserConfirmReq(const BleGapCallbackParam ¶m) const; 114 bool GapLePairComplete(const BleGapCallbackParam ¶m) const; 115 bool GapLePairKeyNotify(const BleGapCallbackParam ¶m) const; 116 bool GapLeRequestSecurityResultEvt(const BleGapCallbackParam ¶m) const; 117 static bool LePairFeatureReq(const BleGapCallbackParam ¶m); 118 119 void InitGapEventFuncTable() const; 120 121 typedef bool (BleSecurity::*func)(const BleGapCallbackParam ¶m) const; 122 123 IAdapterBle *bleAdapter_ = nullptr; 124 /// The dispatcher that is used to switch to the thread. 125 utility::Dispatcher *dispatcher_ = nullptr; 126 BaseObserverList<IAdapterBleObserver> *baseCallback_ = nullptr; 127 128 DISALLOW_COPY_AND_ASSIGN(BleSecurity); 129 DECLARE_IMPL(); 130 }; 131 } // namespace bluetooth 132 133 #endif // BLE_SECURITY_H