esp-nimble-cpp  1.3.2
NimBLESecurity.h
1 /*
2  * NimBLESecurity.h
3  *
4  * Created: on Feb 22 2020
5  * Author H2zero
6  *
7  * Originally:
8  *
9  * BLESecurity.h
10  *
11  * Created on: Dec 17, 2017
12  * Author: chegewara
13  */
14 
15 #ifndef COMPONENTS_NIMBLESECURITY_H_
16 #define COMPONENTS_NIMBLESECURITY_H_
17 
18 #include "nimconfig.h"
19 #if defined(CONFIG_BT_ENABLED)
20 
21 #if defined(CONFIG_NIMBLE_CPP_IDF)
22 #include "host/ble_gap.h"
23 #else
24 #include "nimble/nimble/host/include/host/ble_gap.h"
25 #endif
26 
27 /**** FIX COMPILATION ****/
28 #undef min
29 #undef max
30 /**************************/
31 
32 #include <stdint.h>
33 
34 #define ESP_LE_AUTH_NO_BOND 0x00 /* relate to BTM_LE_AUTH_NO_BOND in stack/btm_api.h */
35 #define ESP_LE_AUTH_BOND 0x01 /* relate to BTM_LE_AUTH_BOND in stack/btm_api.h */
36 #define ESP_LE_AUTH_REQ_MITM (1 << 2) /* relate to BTM_LE_AUTH_REQ_MITM in stack/btm_api.h */
37 #define ESP_LE_AUTH_REQ_BOND_MITM (ESP_LE_AUTH_BOND | ESP_LE_AUTH_REQ_MITM)
38 #define ESP_LE_AUTH_REQ_SC_ONLY (1 << 3) /* relate to BTM_LE_AUTH_REQ_SC_ONLY in stack/btm_api.h */
39 #define ESP_LE_AUTH_REQ_SC_BOND (ESP_LE_AUTH_BOND | ESP_LE_AUTH_REQ_SC_ONLY) /* relate to BTM_LE_AUTH_REQ_SC_BOND in stack/btm_api.h */
40 #define ESP_LE_AUTH_REQ_SC_MITM (ESP_LE_AUTH_REQ_MITM | ESP_LE_AUTH_REQ_SC_ONLY) /* relate to BTM_LE_AUTH_REQ_SC_MITM in stack/btm_api.h */
41 #define ESP_LE_AUTH_REQ_SC_MITM_BOND (ESP_LE_AUTH_REQ_MITM | ESP_LE_AUTH_REQ_SC_ONLY | ESP_LE_AUTH_BOND) /* relate to BTM_LE_AUTH_REQ_SC_MITM_BOND in stack/btm_api.h */
42 
43 #define ESP_IO_CAP_OUT 0 /* relate to BTM_IO_CAP_OUT in stack/btm_api.h */
44 #define ESP_IO_CAP_IO 1 /* relate to BTM_IO_CAP_IO in stack/btm_api.h */
45 #define ESP_IO_CAP_IN 2 /* relate to BTM_IO_CAP_IN in stack/btm_api.h */
46 #define ESP_IO_CAP_NONE 3 /* relate to BTM_IO_CAP_NONE in stack/btm_api.h */
47 #define ESP_IO_CAP_KBDISP 4 /* relate to BTM_IO_CAP_KBDISP in stack/btm_api.h */
48 
50 #define ESP_BLE_ENC_KEY_MASK (1 << 0) /* relate to BTM_BLE_ENC_KEY_MASK in stack/btm_api.h */
52 #define ESP_BLE_ID_KEY_MASK (1 << 1) /* relate to BTM_BLE_ID_KEY_MASK in stack/btm_api.h */
54 #define ESP_BLE_CSR_KEY_MASK (1 << 2) /* relate to BTM_BLE_CSR_KEY_MASK in stack/btm_api.h */
56 #define ESP_BLE_LINK_KEY_MASK (1 << 3) /* relate to BTM_BLE_LINK_KEY_MASK in stack/btm_api.h */
57 
58 typedef uint8_t esp_ble_auth_req_t;
59 typedef uint8_t esp_ble_io_cap_t;
68 public:
70  virtual ~NimBLESecurity();
71  void setAuthenticationMode(esp_ble_auth_req_t auth_req);
72  void setCapability(esp_ble_io_cap_t iocap);
73  void setInitEncryptionKey(uint8_t init_key);
74  void setRespEncryptionKey(uint8_t resp_key);
75  void setKeySize(uint8_t key_size = 16);
76  void setStaticPIN(uint32_t pin);
77  //static char* esp_key_type_to_str(esp_ble_key_type_t key_type);
78 /*
79 private:
80  esp_ble_auth_req_t m_authReq;
81  esp_ble_io_cap_t m_iocap;
82  uint8_t m_initKey;
83  uint8_t m_respKey;
84  uint8_t m_keySize;
85 */
86 }; // BLESecurity
87 
88 
95 public:
96  virtual ~NimBLESecurityCallbacks() {};
97 
103  virtual uint32_t onPassKeyRequest() = 0;
104 
110  virtual void onPassKeyNotify(uint32_t pass_key) = 0;
111 
116  virtual bool onSecurityRequest() = 0 ;
120  virtual void onAuthenticationComplete(ble_gap_conn_desc*) = 0;
121 
127  virtual bool onConfirmPIN(uint32_t pin) = 0;
128 }; // BLESecurityCallbacks
129 
130 #endif // CONFIG_BT_ENABLED
131 #endif // COMPONENTS_NIMBLESECURITY_H_
Callbacks to handle GAP events related to authorization. Deprecated - provided for backward compatibi...
Definition: NimBLESecurity.h:94
virtual uint32_t onPassKeyRequest()=0
Its request from peer device to input authentication pin code displayed on peer device....
virtual void onAuthenticationComplete(ble_gap_conn_desc *)=0
Provides us information when authentication process is completed.
virtual bool onConfirmPIN(uint32_t pin)=0
Called when using numeric comparison for authentication.
virtual bool onSecurityRequest()=0
Here we can make decision if we want to let negotiate authorization with peer device or not.
virtual void onPassKeyNotify(uint32_t pass_key)=0
Provide us 6-digits code to perform authentication. It requires that our device is capable to display...
A class to handle BLE security operations. Deprecated - provided for backward compatibility only.
Definition: NimBLESecurity.h:67
void setStaticPIN(uint32_t pin)
Sets a static PIN used to authenticate/encrypt the connection.
Definition: NimBLESecurity.cpp:105
void setCapability(esp_ble_io_cap_t iocap)
Set our device IO capability to let end user perform authorization either by displaying or entering g...
Definition: NimBLESecurity.cpp:58
void setAuthenticationMode(esp_ble_auth_req_t auth_req)
Set requested authentication mode.
Definition: NimBLESecurity.cpp:40
void setRespEncryptionKey(uint8_t resp_key)
Sets the keys we will accept during encryption.
Definition: NimBLESecurity.cpp:86
void setKeySize(uint8_t key_size=16)
Definition: NimBLESecurity.cpp:94
void setInitEncryptionKey(uint8_t init_key)
Sets the keys we will distibute during encryption.
Definition: NimBLESecurity.cpp:72