@@ -168,16 +168,26 @@ class SecurityDb {
168
168
*/
169
169
virtual void get_entry_local_keys (
170
170
SecurityEntryKeysDbCb_t cb,
171
- entry_handle_t db_handle,
171
+ entry_handle_t * db_handle,
172
172
const ediv_t &ediv,
173
173
const rand_t &rand
174
174
) {
175
- SecurityEntryKeys_t* keys = read_in_entry_local_keys (db_handle);
175
+ SecurityEntryKeys_t* keys = read_in_entry_local_keys (* db_handle);
176
176
/* validate we have the correct key */
177
177
if (keys && ediv == keys->ediv && rand == keys->rand ) {
178
- cb (db_handle, keys);
178
+ cb (* db_handle, keys);
179
179
} 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);
181
191
}
182
192
}
183
193
@@ -552,17 +562,53 @@ class SecurityDb {
552
562
return nullptr ;
553
563
}
554
564
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
+
555
599
/* *
556
600
* Close a connection entry.
557
601
*
558
602
* @param[in] db_handle this handle will be freed up from the security db.
559
603
*/
560
- virtual void close_entry (entry_handle_t db_handle) {
604
+ virtual void close_entry (entry_handle_t db_handle, bool require_sync = true ) {
561
605
SecurityDistributionFlags_t* flags = get_distribution_flags (db_handle);
562
606
if (flags) {
563
607
flags->connected = false ;
564
608
}
565
- sync (db_handle);
609
+ if (require_sync) {
610
+ sync (db_handle);
611
+ }
566
612
}
567
613
568
614
/* *
0 commit comments