Skip to content

Commit 8716298

Browse files
committed
BLE: Update DB entry if current entry doesn't match requested EDIV and RAND
1 parent 4c1afe8 commit 8716298

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

connectivity/FEATURE_BLE/source/generic/SecurityDb.h

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,26 @@ class SecurityDb {
168168
*/
169169
virtual void get_entry_local_keys(
170170
SecurityEntryKeysDbCb_t cb,
171-
entry_handle_t db_handle,
171+
entry_handle_t* db_handle,
172172
const ediv_t &ediv,
173173
const rand_t &rand
174174
) {
175-
SecurityEntryKeys_t* keys = read_in_entry_local_keys(db_handle);
175+
SecurityEntryKeys_t* keys = read_in_entry_local_keys(*db_handle);
176176
/* validate we have the correct key */
177177
if (keys && ediv == keys->ediv && rand == keys->rand) {
178-
cb(db_handle, keys);
178+
cb(*db_handle, keys);
179179
} else {
180-
cb(db_handle, NULL);
180+
// Maybe this isn't the correct entry, try to find one that matches
181+
entry_handle_t correct_handle = find_entry_by_peer_ediv_rand(ediv, rand);
182+
if (!correct_handle) {
183+
cb(*db_handle, NULL);
184+
}
185+
// Note: keys should never be null as a matching entry has been retrieved
186+
SecurityEntryKeys_t* keys = read_in_entry_local_keys(correct_handle);
187+
MBED_ASSERT(keys);
188+
close_entry(*db_handle, false);
189+
*db_handle = correct_handle;
190+
cb(*db_handle, keys);
181191
}
182192
}
183193

@@ -552,17 +562,53 @@ class SecurityDb {
552562
return nullptr;
553563
}
554564

565+
/**
566+
* Find a database entry based on ediv and rand.
567+
*
568+
* @param[in] ediv E diversifier
569+
* @param[in] rand random part
570+
*
571+
* @return A handle to the entry.
572+
*/
573+
virtual entry_handle_t find_entry_by_peer_ediv_rand(
574+
const ediv_t &ediv,
575+
const rand_t &rand
576+
) {
577+
for (size_t i = 0; i < get_entry_count(); i++) {
578+
entry_handle_t db_handle = get_entry_handle_by_index(i);
579+
SecurityDistributionFlags_t* flags = get_distribution_flags(db_handle);
580+
581+
if (!flags || flags->connected) {
582+
continue;
583+
}
584+
585+
SecurityEntryKeys_t* keys = read_in_entry_local_keys(db_handle);
586+
if (!keys) {
587+
continue;
588+
}
589+
590+
if (keys->ediv == ediv && keys->rand == rand) {
591+
return db_handle;
592+
}
593+
}
594+
595+
return nullptr;
596+
}
597+
598+
555599
/**
556600
* Close a connection entry.
557601
*
558602
* @param[in] db_handle this handle will be freed up from the security db.
559603
*/
560-
virtual void close_entry(entry_handle_t db_handle) {
604+
virtual void close_entry(entry_handle_t db_handle, bool require_sync = true) {
561605
SecurityDistributionFlags_t* flags = get_distribution_flags(db_handle);
562606
if (flags) {
563607
flags->connected = false;
564608
}
565-
sync(db_handle);
609+
if (require_sync) {
610+
sync(db_handle);
611+
}
566612
}
567613

568614
/**

connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ void SecurityManager::on_ltk_request(
19351935

19361936
_db->get_entry_local_keys(
19371937
mbed::callback(this, &SecurityManager::set_ltk_cb),
1938-
cb->db_entry,
1938+
&cb->db_entry,
19391939
ediv,
19401940
rand
19411941
);

0 commit comments

Comments
 (0)