esp-nimble-cpp / NimBLE-Arduino  1.3.1
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 #include "sdkconfig.h"
18 #if defined(CONFIG_BT_ENABLED)
19 
20 #include "host/ble_gap.h"
21 /**** FIX COMPILATION ****/
22 #undef min
23 #undef max
24 /**************************/
25 
26 #include <stdint.h>
27 
28 #define ESP_LE_AUTH_NO_BOND 0x00 /* relate to BTM_LE_AUTH_NO_BOND in stack/btm_api.h */
29 #define ESP_LE_AUTH_BOND 0x01 /* relate to BTM_LE_AUTH_BOND in stack/btm_api.h */
30 #define ESP_LE_AUTH_REQ_MITM (1 << 2) /* relate to BTM_LE_AUTH_REQ_MITM in stack/btm_api.h */
31 #define ESP_LE_AUTH_REQ_BOND_MITM (ESP_LE_AUTH_BOND | ESP_LE_AUTH_REQ_MITM)
32 #define ESP_LE_AUTH_REQ_SC_ONLY (1 << 3) /* relate to BTM_LE_AUTH_REQ_SC_ONLY in stack/btm_api.h */
33 #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 */
34 #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 */
35 #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 */
36 
37 #define ESP_IO_CAP_OUT 0 /* relate to BTM_IO_CAP_OUT in stack/btm_api.h */
38 #define ESP_IO_CAP_IO 1 /* relate to BTM_IO_CAP_IO in stack/btm_api.h */
39 #define ESP_IO_CAP_IN 2 /* relate to BTM_IO_CAP_IN in stack/btm_api.h */
40 #define ESP_IO_CAP_NONE 3 /* relate to BTM_IO_CAP_NONE in stack/btm_api.h */
41 #define ESP_IO_CAP_KBDISP 4 /* relate to BTM_IO_CAP_KBDISP in stack/btm_api.h */
42 
44 #define ESP_BLE_ENC_KEY_MASK (1 << 0) /* relate to BTM_BLE_ENC_KEY_MASK in stack/btm_api.h */
46 #define ESP_BLE_ID_KEY_MASK (1 << 1) /* relate to BTM_BLE_ID_KEY_MASK in stack/btm_api.h */
48 #define ESP_BLE_CSR_KEY_MASK (1 << 2) /* relate to BTM_BLE_CSR_KEY_MASK in stack/btm_api.h */
50 #define ESP_BLE_LINK_KEY_MASK (1 << 3) /* relate to BTM_BLE_LINK_KEY_MASK in stack/btm_api.h */
51 
52 typedef uint8_t esp_ble_auth_req_t;
53 typedef uint8_t esp_ble_io_cap_t;
62 public:
64  virtual ~NimBLESecurity();
65  void setAuthenticationMode(esp_ble_auth_req_t auth_req);
66  void setCapability(esp_ble_io_cap_t iocap);
67  void setInitEncryptionKey(uint8_t init_key);
68  void setRespEncryptionKey(uint8_t resp_key);
69  void setKeySize(uint8_t key_size = 16);
70  void setStaticPIN(uint32_t pin);
71  //static char* esp_key_type_to_str(esp_ble_key_type_t key_type);
72 /*
73 private:
74  esp_ble_auth_req_t m_authReq;
75  esp_ble_io_cap_t m_iocap;
76  uint8_t m_initKey;
77  uint8_t m_respKey;
78  uint8_t m_keySize;
79 */
80 }; // BLESecurity
81 
82 
89 public:
90  virtual ~NimBLESecurityCallbacks() {};
91 
97  virtual uint32_t onPassKeyRequest() = 0;
98 
104  virtual void onPassKeyNotify(uint32_t pass_key) = 0;
105 
110  virtual bool onSecurityRequest() = 0 ;
114  virtual void onAuthenticationComplete(ble_gap_conn_desc*) = 0;
115 
121  virtual bool onConfirmPIN(uint32_t pin) = 0;
122 }; // BLESecurityCallbacks
123 
124 #endif // CONFIG_BT_ENABLED
125 #endif // COMPONENTS_NIMBLESECURITY_H_
Callbacks to handle GAP events related to authorization. Deprecated - provided for backward compatibi...
Definition: NimBLESecurity.h:88
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:61
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