diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.cpp b/libraries/Bluefruit52Lib/src/BLEConnection.cpp index 3c987880..9d8f7bbb 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.cpp +++ b/libraries/Bluefruit52Lib/src/BLEConnection.cpp @@ -227,14 +227,36 @@ bool BLEConnection::getWriteCmdPacket (void) return xSemaphoreTake(_wrcmd_sem, ms2tick(BLE_GENERIC_TIMEOUT)); } -bool BLEConnection::storeCccd(void) +bool BLEConnection::saveCccd(void) { - return bond_save_cccd( _role, _conn_hdl, _ediv); + return bond_save_cccd(_role, _conn_hdl, &_bond_id_addr); +} + +bool BLEConnection::loadCccd(void) +{ + return bond_load_cccd(_role, _conn_hdl, &_bond_id_addr); +} + +bool BLEConnection::_saveLongTermKey(bond_keys_t const* ltkey) +{ + bond_save_keys(_role, _conn_hdl, ltkey); + _bond_id_addr = ltkey->peer_id.id_addr_info; + _paired = true; // paired with new device + return true; +} + +bool BLEConnection::_loadLongTermKey(bond_keys_t* ltkey) +{ + VERIFY( bond_load_keys(_role, &_peer_addr, ltkey) ); + _bond_id_addr = ltkey->peer_id.id_addr_info; + return true; } bool BLEConnection::loadKeys(bond_keys_t* bkeys) { - return bond_load_keys(_role, _ediv, bkeys); + return false; + // FIXME dfu key sharing later + //return bond_load_keys(_role, _ediv, bkeys); } bool BLEConnection::requestPairing(void) @@ -276,6 +298,7 @@ bool BLEConnection::requestPairing(void) // Prph delete bonds while we did not --> let's remove the obsolete keyfile and retry if ( !_paired && (cntr_ediv != 0xffff) ) { + // FIXME central remove key bond_remove_key(BLE_GAP_ROLE_CENTRAL, cntr_ediv); // Re-try with a fresh session @@ -314,34 +337,6 @@ void BLEConnection::_eventHandler(ble_evt_t* evt) _connected = false; break; - //--------------------------------------------------------------------+ - /* First-time Pairing - * - * Connect -> SEC_PARAMS_REQUEST -> PASSKEY_DISPLAY -> BLE_GAP_EVT_LESC_DHKEY_REQUEST -> - * CONN_SEC_UPDATE -> AUTH_STATUS - * 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 PassKey exchange - * 4. Connection is secured BLE_GAP_EVT_CONN_SEC_UPDATE - * 5. Long term Keys exchanged BLE_GAP_EVT_AUTH_STATUS - * - * Reconnect using bonded key - * Connect -> SEC_INFO_REQUEST -> CONN_SEC_UPDATE - * 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_AUTH_STATUS: - { - ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status; - - // Pairing succeeded - if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) _paired = true; - } - 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; diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.h b/libraries/Bluefruit52Lib/src/BLEConnection.h index 5a6495a1..56b0fada 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.h +++ b/libraries/Bluefruit52Lib/src/BLEConnection.h @@ -57,7 +57,8 @@ class BLEConnection bool _paired; bool _hvc_received; - ble_gap_addr_t _peer_addr; + ble_gap_addr_t _peer_addr; // resolvable connect address + ble_gap_addr_t _bond_id_addr; // address stored as bonded SemaphoreHandle_t _hvn_sem; SemaphoreHandle_t _wrcmd_sem; @@ -105,15 +106,21 @@ class BLEConnection bool getWriteCmdPacket(void); bool waitForIndicateConfirm(void); - bool storeCccd(void); + bool saveCccd(void); + bool loadCccd(void); + bool loadKeys(bond_keys_t* bkeys); + /*------------------------------------------------------------------*/ /* INTERNAL USAGE ONLY * Although declare as public, it is meant to be invoked by internal code. *------------------------------------------------------------------*/ void _eventHandler(ble_evt_t* evt); + bool _saveLongTermKey(bond_keys_t const* bkeys); + bool _loadLongTermKey(bond_keys_t* bkeys); + friend class BLEPairing; }; diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index 48b67f59..04bce2ce 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -142,7 +142,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) // Save CCCD if paired if ( conn->paired() && (evt_id == BLE_GATTS_EVT_WRITE) && (req_handle == chr->handles().cccd_handle) ) { - conn->storeCccd(); + conn->saveCccd(); } } } diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.cpp b/libraries/Bluefruit52Lib/src/BLEPairing.cpp index bf7a5a6c..afdcdb63 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.cpp +++ b/libraries/Bluefruit52Lib/src/BLEPairing.cpp @@ -128,7 +128,6 @@ bool BLEPairing::resolveAddress(ble_gap_addr_t const * p_addr, ble_gap_irk_t con swap_endian(ecb_data.cleartext, SOC_ECB_CLEARTEXT_LENGTH); // compute using HW AES peripherals - // TODO use CC310 to compute AES (void) sd_ecb_block_encrypt(&ecb_data); // Swap output endian @@ -351,13 +350,7 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) LOG_LV2("PAIR", "Ediv = 0x%02X", _ediv); LOG_LV2_BUFFER("Rand", _bond_keys->own_enc.master_id.rand, 8); - //PRINT_INT(_bond_keys->peer_id.id_addr_info.addr_type); - //Serial.printBufferReverse(_bond_keys->peer_id.id_addr_info.addr, 6, ':'); - //Serial.println(); - - bond_save_keys(conn->getRole(), conn_hdl, _bond_keys); - -// _paired = true; + conn->_saveLongTermKey(_bond_keys); } rtos_free(_bond_keys); @@ -375,23 +368,13 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) // - Else return NULL --> Initiate key exchange ble_gap_evt_sec_info_request_t* sec_info = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request; - LOG_LV2("PAIR", "Addr ID = %d, Addr Type = 0x%02X, ediv = 0x%02X", - sec_info->peer_addr.addr_id_peer, sec_info->peer_addr.addr_type, sec_info->master_id.ediv); + LOG_LV2("PAIR", "Addr ID = %d, Addr Type = 0x%02X", sec_info->peer_addr.addr_id_peer, sec_info->peer_addr.addr_type); LOG_LV2_BUFFER("Address", sec_info->peer_addr.addr, 6); - LOG_LV2_BUFFER("Rand", sec_info->master_id.rand, 8); bond_keys_t bkeys; - varclr(&bkeys); - // TODO Random Static - - // Resolvable - - - if ( bond_load_keys(conn->getRole(), sec_info->master_id.ediv, &bkeys) ) + if ( conn->_loadLongTermKey(&bkeys) ) { -// PRINT_INT(resolveAddress(&sec_info->peer_addr, &bkeys.peer_id.id_info)); - 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; @@ -412,7 +395,7 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) { // 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, _ediv) ) + if ( !conn->loadCccd() ) { sd_ble_gatts_sys_attr_set(conn_hdl, NULL, 0, 0); } diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.cpp b/libraries/Bluefruit52Lib/src/utility/bonding.cpp index 5d402ff0..653f633e 100644 --- a/libraries/Bluefruit52Lib/src/utility/bonding.cpp +++ b/libraries/Bluefruit52Lib/src/utility/bonding.cpp @@ -60,11 +60,12 @@ using namespace Adafruit_LittleFS_Namespace; * Each field has an 1-byte preceding length *------------------------------------------------------------------*/ #define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS) -#define BOND_FNAME_LEN (max(sizeof(BOND_FNAME_PRPH), sizeof(BOND_FNAME_CNTR)) + 8)// TODO refactor later +#define BOND_FNAME_LEN max(sizeof(BOND_FNAME_PRPH), sizeof(BOND_FNAME_CNTR)) -static void get_fname (char* fname, uint8_t role, uint16_t ediv) +static void get_fname (char* fname, uint8_t role, uint8_t const mac[6]) { - sprintf(fname, (role == BLE_GAP_ROLE_PERIPH) ? BOND_FNAME_PRPH : BOND_FNAME_CNTR, ediv); + sprintf(fname, (role == BLE_GAP_ROLE_PERIPH) ? BOND_FNAME_PRPH : BOND_FNAME_CNTR, + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } static bool bdata_skip_field(File* file) @@ -94,18 +95,14 @@ void bond_init(void) /*------------------------------------------------------------------*/ /* Keys *------------------------------------------------------------------*/ -static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bool lesc, bond_keys_t* bkeys) +static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_keys_t* bkeys) { uint16_t const ediv = (role == BLE_GAP_ROLE_PERIPH) ? bkeys->own_enc.master_id.ediv : bkeys->peer_enc.master_id.ediv; + uint8_t const * mac = bkeys->peer_id.id_addr_info.addr; // Bond store keys using peer mac address e.g 1a2b3c4d5e6f - char filename[BOND_FNAME_LEN]; -// get_fname(filename, role, ediv); - - uint8_t const * mac = bkeys->peer_id.id_addr_info.addr; - sprintf(filename, BOND_DIR_PRPH "/%02X%02X%02X%02X%02X%02X", - mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + get_fname(filename, role, mac); // delete if file already exists if ( InternalFS.exists(filename) ) InternalFS.remove(filename); @@ -152,19 +149,26 @@ bool bond_load_keys(uint8_t role, ble_gap_addr_t* addr, bond_keys_t* bkeys) // Address is used as identity char filename[BOND_FNAME_LEN]; - // get_fname(filename, role, ediv); - sprintf(filename, BOND_DIR_PRPH "/%02X%02X%02X%02X%02X%02X", - addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3], addr->addr[4], addr->addr[5]); + get_fname(filename, role, addr->addr); File file(InternalFS); - VERIFY( file.open(filename, FILE_O_READ) ); - BOND_LOG("Loaded keys from file %s", filename); + if( file.open(filename, FILE_O_READ) ) + { + int keylen = file.read(); + file.read((uint8_t*) bkeys, keylen); + + ret = true; + BOND_LOG("Loaded keys from file %s", filename); + } + + file.close(); } break; case BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE: { // Resolvable address, we have to go through the whole list to perform IRK Address matching + char const * dpath = (role == BLE_GAP_ROLE_PERIPH ? BOND_DIR_PRPH : BOND_DIR_CNTR); File dir(dpath, FILE_O_READ, InternalFS); File file(InternalFS); @@ -177,8 +181,8 @@ bool bond_load_keys(uint8_t role, ble_gap_addr_t* addr, bond_keys_t* bkeys) file.read((uint8_t*) bkeys, keylen); if ( Bluefruit.Pairing.resolveAddress(addr, &bkeys->peer_id.id_info) ) { - BOND_LOG("Loaded keys from file %s/%s", dpath, file.name()); ret = true; + BOND_LOG("Loaded keys from file %s/%s", dpath, file.name()); } } @@ -199,7 +203,7 @@ bool bond_load_keys(uint8_t role, ble_gap_addr_t* addr, bond_keys_t* bkeys) /*------------------------------------------------------------------*/ /* CCCD *------------------------------------------------------------------*/ -static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv) +static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, ble_gap_addr_t const* id_addr) { uint16_t len=0; sd_ble_gatts_sys_attr_get(conn_hdl, NULL, &len, SVC_CONTEXT_FLAG); @@ -209,7 +213,7 @@ static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv) VERIFY_STATUS(sd_ble_gatts_sys_attr_get(conn_hdl, sys_attr, &len, SVC_CONTEXT_FLAG),); char filename[BOND_FNAME_LEN]; - get_fname(filename, role, ediv); + get_fname(filename, role, id_addr->addr); File file(filename, FILE_O_WRITE, InternalFS); VERIFY(file,); @@ -225,53 +229,42 @@ static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv) file.close(); } -bool bond_save_cccd (uint8_t role, uint16_t conn_hdl, uint16_t ediv) +bool bond_save_cccd (uint8_t role, uint16_t conn_hdl, ble_gap_addr_t const* id_addr) { - VERIFY(ediv != 0xFFFF); - // queue to execute in Ada Callback thread - return ada_callback(NULL, 0, bond_save_cccd_dfr, role, conn_hdl, ediv); + return ada_callback(id_addr, sizeof(ble_gap_addr_t), bond_save_cccd_dfr, role, conn_hdl, id_addr); } -bool bond_load_cccd(uint8_t role, uint16_t conn_hdl, uint16_t ediv) +bool bond_load_cccd(uint8_t role, uint16_t conn_hdl, ble_gap_addr_t const* id_addr) { bool loaded = false; - if ( ediv != 0xFFFF ) + char filename[BOND_FNAME_LEN]; + get_fname(filename, role, id_addr->addr); + + File file(filename, FILE_O_READ, InternalFS); + if ( file ) { - char filename[BOND_FNAME_LEN]; - get_fname(filename, role, ediv); + bdata_skip_field(&file); // skip key + bdata_skip_field(&file); // skip name - File file(filename, FILE_O_READ, InternalFS); - - if ( file ) + int len = file.read(); + if ( len > 0 ) { - bdata_skip_field(&file); // skip key - bdata_skip_field(&file); // skip name + uint8_t sys_attr[len]; + file.read(sys_attr, len); - int len = file.read(); - if ( len > 0 ) + if ( ERROR_NONE == sd_ble_gatts_sys_attr_set(conn_hdl, sys_attr, len, SVC_CONTEXT_FLAG) ) { - uint8_t sys_attr[len]; - - file.read(sys_attr, len); - - if ( ERROR_NONE == sd_ble_gatts_sys_attr_set(conn_hdl, sys_attr, len, SVC_CONTEXT_FLAG) ) - { - loaded = true; - BOND_LOG("Loaded CCCD from file %s ( offset = %ld, len = %d bytes )", filename, file.size() - (len + 1), len); - } + loaded = true; + BOND_LOG("Loaded CCCD from file %s ( offset = %ld, len = %d bytes )", filename, file.size() - (len + 1), len); } } - - file.close(); } - if ( !loaded ) - { - LOG_LV1("BOND", "CCCD setting not found"); - } + file.close(); + if ( !loaded ) LOG_LV1("BOND", "CCCD setting not found"); return loaded; } @@ -378,8 +371,8 @@ void bond_clear_all(void) void bond_remove_key(uint8_t role, uint16_t ediv) { - char filename[BOND_FNAME_LEN]; - get_fname(filename, role, ediv); - - InternalFS.remove(filename); +// char filename[BOND_FNAME_LEN]; +// get_fname(filename, role, ediv); +// +// InternalFS.remove(filename); } diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.h b/libraries/Bluefruit52Lib/src/utility/bonding.h index 396b9797..926a4520 100644 --- a/libraries/Bluefruit52Lib/src/utility/bonding.h +++ b/libraries/Bluefruit52Lib/src/utility/bonding.h @@ -41,8 +41,8 @@ #define BOND_DIR_PRPH "/adafruit/bond_prph" #define BOND_DIR_CNTR "/adafruit/bond_cntr" -#define BOND_FNAME_PRPH BOND_DIR_PRPH "/%04x" -#define BOND_FNAME_CNTR BOND_DIR_CNTR "/%04x" +#define BOND_FNAME_PRPH BOND_DIR_PRPH "/%02X%02X%02X%02X%02X%02X" +#define BOND_FNAME_CNTR BOND_DIR_CNTR "/%02X%02X%02X%02X%02X%02X" // Shared keys with bonded device, size = 80 bytes typedef struct @@ -57,13 +57,13 @@ void bond_clear_prph(void); void bond_clear_cntr(void); void bond_clear_all(void); -void bond_remove_key(uint8_t role, uint16_t ediv); +void bond_remove_key(uint8_t role, uint16_t ediv) TU_ATTR_DEPRECATED("FIXME remove key"); bool bond_save_keys (uint8_t role, uint16_t conn_hdl, bond_keys_t const* bkeys); bool bond_load_keys(uint8_t role, ble_gap_addr_t*, bond_keys_t* bkeys); -bool bond_save_cccd (uint8_t role, uint16_t conn_hdl, uint16_t ediv); -bool bond_load_cccd (uint8_t role, uint16_t conn_hdl, uint16_t ediv); +bool bond_save_cccd (uint8_t role, uint16_t conn_hdl, ble_gap_addr_t const* id_addr); +bool bond_load_cccd (uint8_t role, uint16_t conn_hdl, ble_gap_addr_t const* id_addr); void bond_print_list(uint8_t role);