rename bond_data to bond_keys
This commit is contained in:
1 parent
751195fc17
commit
35372d8fab
5 files changed
+62
-52
No files matched your search
@@ -159,13 +159,13 @@ bool BLEGap::requestPairing(uint16_t conn_hdl)
|
||||
if ( peer->role == BLE_GAP_ROLE_CENTRAL )
|
||||
{
|
||||
// Check to see if we did bonded with current prph previously
|
||||
bond_data_t bdata;
|
||||
bond_keys_t bkeys;
|
||||
|
||||
if ( bond_find_cntr(&peer->addr, &bdata) )
|
||||
if ( bond_find_cntr(&peer->addr, &bkeys) )
|
||||
{
|
||||
cntr_ediv = bdata.peer_enc.master_id.ediv;
|
||||
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, &bdata.peer_enc.master_id, &bdata.peer_enc.enc_info), false);
|
||||
VERIFY_STATUS( sd_ble_gap_encrypt(conn_hdl, &bkeys.peer_enc.master_id, &bkeys.peer_enc.enc_info), false);
|
||||
|
||||
}else
|
||||
{
|
||||
@@ -284,11 +284,11 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
{
|
||||
// Pairing in progress, Peer asking for our info
|
||||
peer->bond_data = (bond_data_t*) rtos_malloc( sizeof(bond_data_t));
|
||||
VERIFY(peer->bond_data, );
|
||||
peer->bond_keys = (bond_keys_t*) rtos_malloc( sizeof(bond_keys_t));
|
||||
VERIFY(peer->bond_keys, );
|
||||
|
||||
bond_data_t* bdata = peer->bond_data;
|
||||
memclr(bdata, sizeof(bond_data_t));
|
||||
bond_keys_t* bkeys = peer->bond_keys;
|
||||
memclr(bkeys, sizeof(bond_keys_t));
|
||||
|
||||
peer->ediv = 0xFFFF; // invalid value for ediv
|
||||
|
||||
@@ -309,15 +309,15 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
ble_gap_sec_keyset_t keyset =
|
||||
{
|
||||
.keys_own = {
|
||||
.p_enc_key = &bdata->own_enc,
|
||||
.p_enc_key = &bkeys->own_enc,
|
||||
.p_id_key = NULL,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
},
|
||||
|
||||
.keys_peer = {
|
||||
.p_enc_key = &bdata->peer_enc,
|
||||
.p_id_key = &bdata->peer_id,
|
||||
.p_enc_key = &bkeys->peer_enc,
|
||||
.p_id_key = &bkeys->peer_id,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
}
|
||||
@@ -340,16 +340,16 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
|
||||
{
|
||||
peer->paired = true;
|
||||
peer->ediv = peer->bond_data->own_enc.master_id.ediv;
|
||||
peer->ediv = peer->bond_keys->own_enc.master_id.ediv;
|
||||
|
||||
bond_save_keys(peer->role, conn_hdl, peer->bond_data);
|
||||
bond_save_keys(peer->role, conn_hdl, peer->bond_keys);
|
||||
}else
|
||||
{
|
||||
PRINT_HEX(status->auth_status);
|
||||
}
|
||||
|
||||
rtos_free(peer->bond_data);
|
||||
peer->bond_data = NULL;
|
||||
rtos_free(peer->bond_keys);
|
||||
peer->bond_keys = NULL;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -360,14 +360,14 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
// - 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_data_t bdata;
|
||||
varclr(&bdata);
|
||||
bond_keys_t bkeys;
|
||||
varclr(&bkeys);
|
||||
|
||||
if ( bond_load_keys(peer->role, sec_req->master_id.ediv, &bdata) )
|
||||
if ( bond_load_keys(peer->role, sec_req->master_id.ediv, &bkeys) )
|
||||
{
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &bdata.own_enc.enc_info, &bdata.peer_id.id_info, NULL);
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &bkeys.own_enc.enc_info, &bkeys.peer_id.id_info, NULL);
|
||||
|
||||
peer->ediv = bdata.own_enc.master_id.ediv;
|
||||
peer->ediv = bkeys.own_enc.master_id.ediv;
|
||||
} else
|
||||
{
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
|
||||
|
||||
@@ -100,7 +100,7 @@ class BLEGap
|
||||
ble_gap_addr_t addr;
|
||||
|
||||
uint16_t ediv;
|
||||
bond_data_t* bond_data; // Shared keys with bonded device, size ~ 80 bytes
|
||||
bond_keys_t* bond_keys; // Shared keys with bonded device, size ~ 80 bytes
|
||||
|
||||
SemaphoreHandle_t hvn_tx_sem;
|
||||
SemaphoreHandle_t wrcmd_tx_sem;
|
||||
|
||||
@@ -139,13 +139,13 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt
|
||||
|
||||
if ( Bluefruit.Gap.paired(conn_hdl) )
|
||||
{
|
||||
bond_data_t bdata;
|
||||
bond_keys_t bkeys;
|
||||
|
||||
if ( bond_load_keys( BLE_GAP_ROLE_PERIPH, Bluefruit.Gap._get_peer(conn_hdl)->ediv, &bdata ) )
|
||||
if ( bond_load_keys( BLE_GAP_ROLE_PERIPH, Bluefruit.Gap._get_peer(conn_hdl)->ediv, &bkeys ) )
|
||||
{
|
||||
peer_data->addr = bdata.peer_id.id_addr_info;
|
||||
peer_data->irk = bdata.peer_id.id_info;
|
||||
peer_data->enc_key = bdata.own_enc;
|
||||
peer_data->addr = bkeys.peer_id.id_addr_info;
|
||||
peer_data->irk = bkeys.peer_id.id_info;
|
||||
peer_data->enc_key = bkeys.own_enc;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,15 @@ static void get_fname (char* fname, uint8_t role, uint16_t ediv)
|
||||
sprintf(fname, (role == BLE_GAP_ROLE_PERIPH) ? BOND_FNAME_PRPH : BOND_FNAME_CNTR, ediv);
|
||||
}
|
||||
|
||||
static bool skip_data_field(File* f)
|
||||
{
|
||||
int len = f->read();
|
||||
VERIFY(len > 0);
|
||||
|
||||
f->seek(len + f->position());
|
||||
return true;
|
||||
}
|
||||
|
||||
void bond_init(void)
|
||||
{
|
||||
InternalFS.begin();
|
||||
@@ -75,9 +84,9 @@ void bond_init(void)
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Keys
|
||||
*------------------------------------------------------------------*/
|
||||
static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_data_t* bdata)
|
||||
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) ? bdata->own_enc.master_id.ediv : bdata->peer_enc.master_id.ediv;
|
||||
uint16_t const ediv = (role == BLE_GAP_ROLE_PERIPH) ? bkeys->own_enc.master_id.ediv : bkeys->peer_enc.master_id.ediv;
|
||||
|
||||
char filename[BOND_FNAME_LEN];
|
||||
get_fname(filename, role, ediv);
|
||||
@@ -89,8 +98,8 @@ static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_data_t* bd
|
||||
VERIFY(file,);
|
||||
|
||||
//------------- save keys -------------//
|
||||
file.write(sizeof(bond_data_t));
|
||||
file.write((uint8_t*) bdata, sizeof(bond_data_t));
|
||||
file.write(sizeof(bond_keys_t));
|
||||
file.write((uint8_t*) bkeys, sizeof(bond_keys_t));
|
||||
|
||||
//------------- save device name -------------//
|
||||
char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
|
||||
@@ -99,7 +108,7 @@ static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_data_t* bd
|
||||
// If couldn't get devname then use peer mac address
|
||||
if ( !devname[0] )
|
||||
{
|
||||
uint8_t* mac = bdata->peer_id.id_addr_info.addr;
|
||||
uint8_t* mac = bkeys->peer_id.id_addr_info.addr;
|
||||
sprintf(devname, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
|
||||
}
|
||||
|
||||
@@ -112,12 +121,12 @@ static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_data_t* bd
|
||||
file.close();
|
||||
}
|
||||
|
||||
bool bond_save_keys (uint8_t role, uint16_t conn_hdl, bond_data_t* bdata)
|
||||
bool bond_save_keys (uint8_t role, uint16_t conn_hdl, bond_keys_t* bkeys)
|
||||
{
|
||||
uint8_t* buf = (uint8_t*) rtos_malloc( sizeof(bond_data_t) );
|
||||
uint8_t* buf = (uint8_t*) rtos_malloc( sizeof(bond_keys_t) );
|
||||
VERIFY(buf);
|
||||
|
||||
memcpy(buf, bdata, sizeof(bond_data_t));
|
||||
memcpy(buf, bkeys, sizeof(bond_keys_t));
|
||||
|
||||
// queue to execute in Ada Callback thread
|
||||
ada_callback(buf, bond_save_keys_dfr, role, conn_hdl, buf);
|
||||
@@ -125,7 +134,7 @@ bool bond_save_keys (uint8_t role, uint16_t conn_hdl, bond_data_t* bdata)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata)
|
||||
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_keys_t* bkeys)
|
||||
{
|
||||
char filename[BOND_FNAME_LEN];
|
||||
get_fname(filename, role, ediv);
|
||||
@@ -134,9 +143,9 @@ bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata)
|
||||
VERIFY(file);
|
||||
|
||||
int keylen = file.read();
|
||||
VERIFY(keylen == sizeof(bond_data_t));
|
||||
VERIFY(keylen == sizeof(bond_keys_t));
|
||||
|
||||
file.read(bdata, keylen);
|
||||
file.read(bkeys, keylen);
|
||||
file.close();
|
||||
|
||||
BOND_LOG("Loaded keys from file %s", filename);
|
||||
@@ -240,15 +249,16 @@ void bond_print_list(uint8_t role)
|
||||
|
||||
while ( (file = dir.openNextFile(FILE_READ)) )
|
||||
{
|
||||
file.seek(file.read() + file.position()); // skip key
|
||||
|
||||
int len = file.read();
|
||||
if ( len > 0 )
|
||||
if ( skip_data_field(&file) ) // skip key
|
||||
{
|
||||
char devname[len];
|
||||
file.read(devname, len);
|
||||
int len = file.read();
|
||||
if ( len > 0 )
|
||||
{
|
||||
char devname[len];
|
||||
file.read(devname, len);
|
||||
|
||||
cprintf(" %s : %s (%d bytes)\n", file.name(), devname, file.size());
|
||||
cprintf(" %s : %s (%d bytes)\n", file.name(), devname, file.size());
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
@@ -260,7 +270,7 @@ void bond_print_list(uint8_t role)
|
||||
}
|
||||
|
||||
|
||||
bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata)
|
||||
bool bond_find_cntr(ble_gap_addr_t* addr, bond_keys_t* bkeys)
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
@@ -271,12 +281,12 @@ bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata)
|
||||
{
|
||||
// Read bond data of each stored file
|
||||
int keylen = file.read();
|
||||
if ( keylen == sizeof(bond_data_t) )
|
||||
if ( keylen == sizeof(bond_keys_t) )
|
||||
{
|
||||
file.read((uint8_t*) bdata, keylen);
|
||||
file.read((uint8_t*) bkeys, keylen);
|
||||
|
||||
// Compare static address
|
||||
if ( !memcmp(addr->addr, bdata->peer_id.id_addr_info.addr, 6) )
|
||||
if ( !memcmp(addr->addr, bkeys->peer_id.id_addr_info.addr, 6) )
|
||||
{
|
||||
found = true;
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ typedef struct
|
||||
ble_gap_enc_key_t own_enc;
|
||||
ble_gap_enc_key_t peer_enc;
|
||||
ble_gap_id_key_t peer_id;
|
||||
} bond_data_t;
|
||||
} bond_keys_t;
|
||||
|
||||
void bond_init(void);
|
||||
void bond_clear_prph(void);
|
||||
@@ -59,15 +59,15 @@ void bond_clear_all(void);
|
||||
|
||||
void bond_remove_key(uint8_t role, uint16_t ediv);
|
||||
|
||||
bool bond_save_keys (uint8_t role, uint16_t conn_hdl, bond_data_t* bdata);
|
||||
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata);
|
||||
bool bond_save_keys (uint8_t role, uint16_t conn_hdl, bond_keys_t* bkeys);
|
||||
bool bond_load_keys(uint8_t role, uint16_t ediv, 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);
|
||||
|
||||
void bond_print_list(uint8_t role);
|
||||
|
||||
bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata);
|
||||
bool bond_find_cntr(ble_gap_addr_t* addr, bond_keys_t* bkeys);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user