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 #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 OHOS {
31 namespace bluetooth {
32 /*
33  * @brief BLE filter.
34  */
35 class BleSecurity {
36 public:
37     /**
38      * @brief Constructor.
39      */
40     explicit BleSecurity(
41         IAdapterBle &bleAdapter, utility::Dispatcher &dispatch, BaseObserverList<IAdapterBleObserver> &observer);
42 
43     /**
44      * @brief Destructor.
45      */
46     virtual ~BleSecurity();
47 
48     static bool StartPair(const RawAddress &device, uint8_t peerAddrType = BT_PUBLIC_DEVICE_ADDRESS);
49     int SetDevicePasskey(const RawAddress &device, int passkey, int accept) const;
50     int SetUserConfirm(const RawAddress &device, int accept) const;
51     int GapLeRequestSecurity(uint16_t connectionHandle, const BtAddr &addr, uint8_t role);
52     int CancelPairing(const RawAddress &device) const;
53     bool PairRequestReply(const RawAddress &addr, int addrType, bool accept) const;
54 
55     /**
56      * @brief Register avertising callback to gap
57      *
58      * @return @c status.
59      */
60     int RegisterCallbackToGap();
61 
62     /**
63      * @brief Deregister avertising callback to gap
64      *
65      * @return @c status.
66      */
67     int DeregisterCallbackToGap() const;
68 
69 private:
70     // gap callback
71     static void EncryptionComplete(uint8_t status, const BtAddr *peerAddr, void *context);
72     static void LeLocalEncryptionKeyReqEvent(const BtAddr *addr, uint64_t rand, uint16_t ediv, void *context);
73     static void LeRemoteEncryptionKeyReqEvent(const BtAddr *addr, void *context);
74     static void LeSignCounterChangeNotification(
75         const BtAddr *addr, GAP_SignCounterType type, uint32_t counter, void *context);
76     static void GapRequestSigningAlgorithmInfo(const BtAddr *addr, void *context);
77 
78     static void LePairFeatureReq(const BtAddr *peerAddr, bool localPair, void *context);
79     static void LePairFeatureInd(const BtAddr *addr, GapLePairFeature remoteFrature, void *context);
80     static void LePairMethodNotify(const BtAddr *addr, uint8_t pairMethod, void *context);
81     static void LePairKeyPressNotification(const BtAddr *addr, uint8_t pressType, void *context);
82     static void LePairPassKeyReq(const BtAddr *addr, void *context);
83     static void LePairPassKeyNotification(const BtAddr *addr, uint32_t number, void *context);
84     static void LePairOobReq(const BtAddr *addr, void *context);
85     static void LePairScOobReq(const BtAddr *addr, void *context);
86     static void LePairScUserConfirmReq(const BtAddr *addr, uint32_t number, void *context);
87     static void LePairComplete(const BtAddr *addr, uint8_t result, uint8_t keyType, void *context);
88     static void LePairKeyNotify(const BtAddr *addr, LePairedKeys leKeys, void *context);
89     static void GapLeRequestSecurityResult(
90         const BtAddr *addr, uint8_t result, GAP_LeSecurityStatus status, void *context);
91     /**
92      *  @brief Internal status
93      *
94      *  @param [in] event gap event.
95      *  @param [in] status gap callback status.
96      */
97     void HandleGapEvent(const BLE_GAP_CB_EVENT &event, const BleGapCallbackParam &param);
98     bool SavePairKeyNotify(const BleGapCallbackParam &param) const;
99     static bool SaveLocalPairKey(const RawAddress &addr, const BleGapCallbackParam &param);
100     static bool SavePeerPairKey(const RawAddress &addr, const BleGapCallbackParam &param);
101     bool GapEncryptionComplete(const BleGapCallbackParam &param) const;
102     bool GapLeLocalEncryptionKeyReqEvent(const BleGapCallbackParam &param) const;
103     bool GapLeRemoteEncryptionKeyReqEvent(const BleGapCallbackParam &param) const;
104     bool GapLeSignCounterChangeNotification(const BleGapCallbackParam &param) const;
105     bool GapRequestSigningAlgorithmInfoEvt(const BleGapCallbackParam &param) const;
106     bool GapLePairFeatureReq(const BleGapCallbackParam &param) const;
107     bool GapLePairFeatureInd(const BleGapCallbackParam &param) const;
108     bool GapLePairMethodNotify(const BleGapCallbackParam &param) const;
109     bool GapLePairKeyPressNotification(const BleGapCallbackParam &param) const;
110     bool GapLePairPassKeyReq(const BleGapCallbackParam &param) const;
111     bool GapLePairPassKeyNotification(const BleGapCallbackParam &param) const;
112     bool GapLePairOobReq(const BleGapCallbackParam &param) const;
113     bool GapLePairScOobReq(const BleGapCallbackParam &param) const;
114     bool GapLePairScUserConfirmReq(const BleGapCallbackParam &param) const;
115     bool GapLePairComplete(const BleGapCallbackParam &param) const;
116     bool GapLePairKeyNotify(const BleGapCallbackParam &param) const;
117     bool GapLeRequestSecurityResultEvt(const BleGapCallbackParam &param) const;
118     static bool LePairFeatureReq(const BleGapCallbackParam &param);
119 
120     void InitGapEventFuncTable() const;
121 
122     typedef bool (BleSecurity::*func)(const BleGapCallbackParam &param) const;
123 
124     IAdapterBle *bleAdapter_ = nullptr;
125     /// The dispatcher that is used to switch to the thread.
126     utility::Dispatcher *dispatcher_ = nullptr;
127     BaseObserverList<IAdapterBleObserver> *baseCallback_ = nullptr;
128 
129     BT_DISALLOW_COPY_AND_ASSIGN(BleSecurity);
130     DECLARE_IMPL();
131 };
132 }  // namespace bluetooth
133 }  // namespace OHOS
134 
135 #endif  // BLE_SECURITY_H