move whole pairing process into bleconnection

This commit is contained in:
hathach committed 2019-01-19 01:32:08 +07:00
1 parent 2a3f6ee375
commit 5ca4309ec2
9 files changed
+219 -188

No files matched your search

+199 -5
View File
@@ -34,13 +34,12 @@
*/
/**************************************************************************/
#include "BLEConnection.h"
#include "bluefruit.h"
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+
BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* evt_connected, uint8_t hvn_qsize, uint8_t wrcmd_qsize)
{
_conn_hdl = conn_hdl;
@@ -55,9 +54,9 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e
_paired = false;
_hvc_sem = NULL;
_hvc_received = false;
pair_sem = NULL;
ediv = 0xFFFF; // invalid ediv value
bond_keys = NULL;
_pair_sem = NULL;
_ediv = 0xFFFF; // invalid ediv value
_bond_keys = NULL;
}
BLEConnection::~BLEConnection()
@@ -114,6 +113,60 @@ bool BLEConnection::getWriteCmdPacket (void)
return xSemaphoreTake(_wrcmd_sem, ms2tick(BLE_GENERIC_TIMEOUT));
}
bool BLEConnection::requestPairing(void)
{
// skip if already paired
if ( _paired ) return true;
ble_gap_sec_params_t sec_param = Bluefruit.Gap.getSecureParam();
// on-the-fly semaphore
_pair_sem = xSemaphoreCreateBinary();
if ( _role == BLE_GAP_ROLE_PERIPH )
{
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &sec_param ), false);
xSemaphoreTake(_pair_sem, portMAX_DELAY);
}
else
{
uint16_t cntr_ediv = 0xFFFF;
bond_keys_t bkeys;
// Check to see if we did bonded with current prph previously
// TODO currently only matches key using fixed address
if ( bond_find_cntr(&_addr, &bkeys) )
{
cntr_ediv = bkeys.peer_enc.master_id.ediv;
LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, cntr_ediv);
VERIFY_STATUS( sd_ble_gap_encrypt(_conn_hdl, &bkeys.peer_enc.master_id, &bkeys.peer_enc.enc_info), false);
}else
{
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &sec_param ), false);
}
xSemaphoreTake(_pair_sem, portMAX_DELAY);
// Failed to pair using central stored keys, this happens when
// Prph delete bonds while we did not --> let's remove the obsolete keyfile and retry
if ( !_paired && (cntr_ediv != 0xffff) )
{
bond_remove_key(BLE_GAP_ROLE_CENTRAL, cntr_ediv);
// Re-try with a fresh session
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &sec_param ), false);
xSemaphoreTake(_pair_sem, portMAX_DELAY);
}
}
vSemaphoreDelete(_pair_sem);
_pair_sem = NULL;
return _paired;
}
bool BLEConnection::waitForIndicateConfirm(void)
{
// on the fly semaphore
@@ -132,6 +185,147 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
{
switch(evt->header.evt_id)
{
//--------------------------------------------------------------------+
/* First-time Pairing
* 1. Either we or peer initiate the process
* 2. Peer ask for Secure Parameter ( I/O Caps ) BLE_GAP_EVT_SEC_PARAMS_REQUEST
* 3. Pair Key exchange ( PIN code)
* 4. Connection is secured BLE_GAP_EVT_CONN_SEC_UPDATE
* 5. Long term Keys exchanged BLE_GAP_EVT_AUTH_STATUS
*
* Reconnect using bonded key
* 1. Either we or peer initiate the process
* 2. Peer ask for Secure Info ( bond keys ) BLE_GAP_EVT_SEC_INFO_REQUEST
* 3. Connection is secured BLE_GAP_EVT_CONN_SEC_UPDATE
*/
//--------------------------------------------------------------------+
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
{
// Pairing in progress, Peer asking for our info
_bond_keys = (bond_keys_t*) rtos_malloc( sizeof(bond_keys_t));
VERIFY(_bond_keys, );
memclr(_bond_keys, sizeof(bond_keys_t));
_ediv = 0xFFFF; // invalid value for ediv
/* Step 1: Pairing/Bonding
* - Central supplies its parameters
* - We replies with our security parameters
*/
// ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
COMMENT_OUT(
// Change security parameter according to authentication type
if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
{
sec_para.mitm = 1;
sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
}
)
ble_gap_sec_keyset_t keyset =
{
.keys_own = {
.p_enc_key = &_bond_keys->own_enc,
.p_id_key = NULL,
.p_sign_key = NULL,
.p_pk = NULL
},
.keys_peer = {
.p_enc_key = &_bond_keys->peer_enc,
.p_id_key = &_bond_keys->peer_id,
.p_sign_key = NULL,
.p_pk = NULL
}
};
ble_gap_sec_params_t sec_param = Bluefruit.Gap.getSecureParam();
VERIFY_STATUS(sd_ble_gap_sec_params_reply(_conn_hdl,
BLE_GAP_SEC_STATUS_SUCCESS,
_role == BLE_GAP_ROLE_PERIPH ? &sec_param : NULL,
&keyset),
);
}
break;
case BLE_GAP_EVT_AUTH_STATUS:
{
// Pairing process completed
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
// Pairing succeeded --> save encryption keys ( Bonding )
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
{
_paired = true;
_ediv = _bond_keys->own_enc.master_id.ediv;
bond_save_keys(_role, _conn_hdl, _bond_keys);
}else
{
PRINT_HEX(status->auth_status);
}
rtos_free(_bond_keys);
_bond_keys = NULL;
}
break;
case BLE_GAP_EVT_SEC_INFO_REQUEST:
{
// Peer asks for the stored keys.
// - load key and return if bonded previously.
// - Else return NULL --> Initiate key exchange
ble_gap_evt_sec_info_request_t* sec_req = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
bond_keys_t bkeys;
varclr(&bkeys);
if ( bond_load_keys(_role, sec_req->master_id.ediv, &bkeys) )
{
sd_ble_gap_sec_info_reply(_conn_hdl, &bkeys.own_enc.enc_info, &bkeys.peer_id.id_info, NULL);
_ediv = bkeys.own_enc.master_id.ediv;
} else
{
sd_ble_gap_sec_info_reply(_conn_hdl, NULL, NULL, NULL);
}
}
break;
case BLE_GAP_EVT_PASSKEY_DISPLAY:
{
// ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
// PRINT_INT(passkey_display->match_request);
// PRINT_BUFFER(passkey_display->passkey, 6);
// sd_ble_gap_auth_key_reply
}
break;
case BLE_GAP_EVT_CONN_SEC_UPDATE:
{
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
// Connection is secured (paired) if encryption level > 1
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
{
// Previously bonded --> secure by re-connection process --> Load & Set SysAttr (Apply Service Context)
// Else Init SysAttr (first bonded)
if ( !bond_load_cccd(_role, _conn_hdl, _ediv) )
{
sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
}
_paired = true;
}
if (_pair_sem) xSemaphoreGive(_pair_sem);
}
break;
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
for(uint8_t i=0; i<evt->evt.gatts_evt.params.hvn_tx_complete.count; i++) xSemaphoreGive(_hvn_sem);
break;
+9 -6
View File
@@ -52,16 +52,18 @@ class BLEConnection
SemaphoreHandle_t _hvn_sem;
SemaphoreHandle_t _wrcmd_sem;
// On-demand semaphore that are created on the fly
bool _paired;
bool _hvc_received;
// On-demand semaphore/data that are created on the fly
SemaphoreHandle_t _hvc_sem;
bool _hvc_received;
SemaphoreHandle_t _pair_sem;
bond_keys_t* _bond_keys; // Shared keys with bonded device, size ~ 80 bytes
public:
SemaphoreHandle_t pair_sem;
bool _paired;
uint16_t ediv;
bond_keys_t* bond_keys; // Shared keys with bonded device, size ~ 80 bytes
uint16_t _ediv;
BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const * evt_connected, uint8_t hvn_qsize, uint8_t wrcmd_qsize);
virtual ~BLEConnection();
@@ -80,6 +82,7 @@ class BLEConnection
bool getHvnPacket(void);
bool getWriteCmdPacket(void);
bool requestPairing(void);
bool waitForIndicateConfirm(void);
/*------------------------------------------------------------------*/
+1 -173
View File
@@ -152,53 +152,7 @@ bool BLEGap::requestPairing(uint16_t conn_hdl)
BLEConnection* conn = _connection[conn_hdl];
VERIFY(conn);
// skip if already paired
if ( conn->paired() ) return true;
uint16_t cntr_ediv = 0xFFFF;
if ( conn->getRole() == BLE_GAP_ROLE_CENTRAL )
{
// Check to see if we did bonded with current prph previously
bond_keys_t bkeys;
ble_gap_addr_t peer_addr = conn->getPeerAddr();
if ( bond_find_cntr(&peer_addr, &bkeys) )
{
cntr_ediv = bkeys.peer_enc.master_id.ediv;
LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, cntr_ediv);
VERIFY_STATUS( sd_ble_gap_encrypt(conn_hdl, &bkeys.peer_enc.master_id, &bkeys.peer_enc.enc_info), false);
}else
{
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
}
}else
{
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
}
// Wait for pairing process using on-the-fly semaphore
conn->pair_sem = xSemaphoreCreateBinary();
xSemaphoreTake(conn->pair_sem, portMAX_DELAY);
// Failed to pair using central stored keys, this happens when
// Prph delete bonds while we did not --> let's remove the obsolete keyfile and move on
if ( !conn->paired() && (cntr_ediv != 0xffff) )
{
bond_remove_key(BLE_GAP_ROLE_CENTRAL, cntr_ediv);
// Re-try with a fresh session
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
xSemaphoreTake(conn->pair_sem, portMAX_DELAY);
}
vSemaphoreDelete(conn->pair_sem);
conn->pair_sem = NULL;
return conn->paired();
return conn->requestPairing();
}
uint8_t BLEGap::getRole(uint16_t conn_hdl)
@@ -306,132 +260,6 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
}
break;
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
{
// Pairing in progress, Peer asking for our info
conn->bond_keys = (bond_keys_t*) rtos_malloc( sizeof(bond_keys_t));
VERIFY(conn->bond_keys, );
bond_keys_t* bkeys = conn->bond_keys;
memclr(bkeys, sizeof(bond_keys_t));
conn->ediv = 0xFFFF; // invalid value for ediv
/* Step 1: Pairing/Bonding
* - Central supplies its parameters
* - We replies with our security parameters
*/
// ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
COMMENT_OUT(
// Change security parameter according to authentication type
if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
{
sec_para.mitm = 1;
sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
}
)
ble_gap_sec_keyset_t keyset =
{
.keys_own = {
.p_enc_key = &bkeys->own_enc,
.p_id_key = NULL,
.p_sign_key = NULL,
.p_pk = NULL
},
.keys_peer = {
.p_enc_key = &bkeys->peer_enc,
.p_id_key = &bkeys->peer_id,
.p_sign_key = NULL,
.p_pk = NULL
}
};
VERIFY_STATUS(sd_ble_gap_sec_params_reply(conn_hdl,
BLE_GAP_SEC_STATUS_SUCCESS,
conn->getRole() == BLE_GAP_ROLE_PERIPH ? &_sec_param : NULL,
&keyset),
);
}
break;
case BLE_GAP_EVT_AUTH_STATUS:
{
// Pairing process completed
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
// Pairing succeeded --> save encryption keys ( Bonding )
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
{
conn->_paired = true;
conn->ediv = conn->bond_keys->own_enc.master_id.ediv;
bond_save_keys(conn->getRole(), conn_hdl, conn->bond_keys);
}else
{
PRINT_HEX(status->auth_status);
}
rtos_free(conn->bond_keys);
conn->bond_keys = NULL;
}
break;
case BLE_GAP_EVT_SEC_INFO_REQUEST:
{
// Central ask for the stored keys.
// - load key and return if bonded previously.
// - Else return NULL --> Initiate key exchange
ble_gap_evt_sec_info_request_t* sec_req = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
bond_keys_t bkeys;
varclr(&bkeys);
if ( bond_load_keys(conn->getRole(), sec_req->master_id.ediv, &bkeys) )
{
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &bkeys.own_enc.enc_info, &bkeys.peer_id.id_info, NULL);
conn->ediv = bkeys.own_enc.master_id.ediv;
} else
{
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
}
}
break;
case BLE_GAP_EVT_CONN_SEC_UPDATE:
{
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
// Connection is secured (paired)
// Occurs if bonded + reconnection, or we initiate the pairing process
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
{
// Previously bonded --> secure by re-connection process --> Load & Set SysAttr (Apply Service Context)
// Else Init SysAttr (first bonded)
if ( !bond_load_cccd(conn->getRole(), conn_hdl, conn->ediv) )
{
sd_ble_gatts_sys_attr_set(conn_hdl, NULL, 0, 0);
}
conn->_paired = true;
}
if (conn->pair_sem) xSemaphoreGive(conn->pair_sem);
}
break;
case BLE_GAP_EVT_PASSKEY_DISPLAY:
{
// ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
// PRINT_INT(passkey_display->match_request);
// PRINT_BUFFER(passkey_display->passkey, 6);
// sd_ble_gap_auth_key_reply
}
break;
case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
{
ble_gap_data_length_params_t* param = &evt->evt.gap_evt.params.data_length_update_request.peer_params;
+5
View File
@@ -61,6 +61,11 @@ class BLEGap
bool setAddr (uint8_t mac[6], uint8_t type);
// bool setPrivacy (); sd_ble_gap_privacy_set()
ble_gap_sec_params_t getSecureParam(void)
{
return _sec_param;
}
BLEConnection* getConnection(uint16_t conn_hdl)
{
return ( conn_hdl != BLE_CONN_HANDLE_INVALID ) ?_connection[conn_hdl] : NULL;
+1 -1
View File
@@ -139,7 +139,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
// Save CCCD if paired
if ( Bluefruit.connPaired() && (evt_id == BLE_GATTS_EVT_WRITE) && (req_handle == chr->handles().cccd_handle) )
{
bond_save_cccd( Bluefruit.Gap.getRole(evt_conn_hdl), evt_conn_hdl, conn->ediv);
bond_save_cccd( Bluefruit.Gap.getRole(evt_conn_hdl), evt_conn_hdl, conn->_ediv);
}
}
}
+1
View File
@@ -62,6 +62,7 @@
#include "BLEClientCharacteristic.h"
#include "BLEClientService.h"
#include "BLEDiscovery.h"
#include "BLEConnection.h"
#include "BLEGap.h"
#include "BLEGatt.h"
@@ -143,7 +143,7 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt
{
bond_keys_t bkeys;
if ( bond_load_keys( BLE_GAP_ROLE_PERIPH, Bluefruit.Gap.getConnection(conn_hdl)->ediv, &bkeys ) )
if ( bond_load_keys( BLE_GAP_ROLE_PERIPH, Bluefruit.Gap.getConnection(conn_hdl)->_ediv, &bkeys ) )
{
peer_data->addr = bkeys.peer_id.id_addr_info;
peer_data->irk = bkeys.peer_id.id_info;
@@ -269,7 +269,7 @@ void bond_print_list(uint8_t role)
}
bool bond_find_cntr(ble_gap_addr_t* addr, bond_keys_t* bkeys)
bool bond_find_cntr(ble_gap_addr_t const * addr, bond_keys_t* bkeys)
{
bool found = false;
@@ -67,7 +67,7 @@ bool bond_load_cccd (uint8_t role, uint16_t conn_hdl, uint16_t ediv);
void bond_print_list(uint8_t role);
bool bond_find_cntr(ble_gap_addr_t* addr, bond_keys_t* bkeys);
bool bond_find_cntr(ble_gap_addr_t const * addr, bond_keys_t* bkeys);