Skip to content

Commit dd432a3

Browse files
authored
Merge pull request #13942 from paul-szczepanek-arm/securitydb-fix
ble: Fix persistence in SecurityDB
2 parents 66ca70a + e71f452 commit dd432a3

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

connectivity/FEATURE_BLE/source/generic/KVStoreSecurityDb.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ namespace ble {
2626
#error "BLE_SECURITY_DATABASE_MAX_ENTRIES must be only one digit long"
2727
#endif
2828

29-
#define ENTRY_INVALID (0xFF)
30-
3129
constexpr uint8_t KVStoreSecurityDb::KVSTORESECURITYDB_VERSION;
3230
constexpr size_t KVStoreSecurityDb::DB_PREFIX_SIZE;
3331
constexpr size_t KVStoreSecurityDb::DB_KEY_SIZE;
@@ -54,9 +52,6 @@ typedef SecurityDb::entry_handle_t entry_handle_t;
5452
KVStoreSecurityDb::KVStoreSecurityDb()
5553
: SecurityDb() {
5654
memset(_entries, 0, sizeof(_entries));
57-
for (size_t i = 0; i < get_entry_count(); i++) {
58-
_entries[i].index = ENTRY_INVALID;
59-
}
6055
}
6156

6257
KVStoreSecurityDb::~KVStoreSecurityDb()
@@ -95,7 +90,7 @@ bool KVStoreSecurityDb::erase_db()
9590

9691
/* we zero the database and make sure we can fit all our keys */
9792

98-
db_write(zero.entries, DB_ENTRIES);
93+
db_write(&zero.entries, DB_ENTRIES);
9994
db_write((SecurityEntryIdentity_t*)zero.buffer, DB_LOCAL_IDENTITY);
10095
db_write((csrk_t*)zero.buffer, DB_LOCAL_CSRK);
10196
db_write((sign_count_t*)zero.buffer, DB_LOCAL_SIGN_COUNT);
@@ -144,7 +139,7 @@ void KVStoreSecurityDb::set_entry_local_ltk(
144139
SecurityEntryKeys_t* current_entry = read_in_entry_local_keys(db_handle);
145140
current_entry->ltk = ltk;
146141

147-
db_write_entry(current_entry, DB_ENTRY_LOCAL_KEYS, entry->index);
142+
db_write_entry(current_entry, DB_ENTRY_LOCAL_KEYS, get_index(entry));
148143
}
149144

150145
void KVStoreSecurityDb::set_entry_local_ediv_rand(
@@ -162,7 +157,7 @@ void KVStoreSecurityDb::set_entry_local_ediv_rand(
162157
current_entry->ediv = ediv;
163158
current_entry->rand = rand;
164159

165-
db_write_entry(current_entry, DB_ENTRY_LOCAL_KEYS, entry->index);
160+
db_write_entry(current_entry, DB_ENTRY_LOCAL_KEYS, get_index(entry));
166161
}
167162

168163
/* peer's keys */
@@ -184,7 +179,7 @@ void KVStoreSecurityDb::set_entry_peer_ltk(
184179
SecurityEntryKeys_t* current_entry = read_in_entry_peer_keys(db_handle);
185180
current_entry->ltk = ltk;
186181

187-
db_write_entry(current_entry, DB_ENTRY_PEER_KEYS, entry->index);
182+
db_write_entry(current_entry, DB_ENTRY_PEER_KEYS, get_index(entry));
188183
}
189184

190185
void KVStoreSecurityDb::set_entry_peer_ediv_rand(
@@ -202,7 +197,7 @@ void KVStoreSecurityDb::set_entry_peer_ediv_rand(
202197
current_entry->ediv = ediv;
203198
current_entry->rand = rand;
204199

205-
db_write_entry(current_entry, DB_ENTRY_PEER_KEYS, entry->index);
200+
db_write_entry(current_entry, DB_ENTRY_PEER_KEYS, get_index(entry));
206201
}
207202

208203
void KVStoreSecurityDb::set_entry_peer_irk(
@@ -220,7 +215,7 @@ void KVStoreSecurityDb::set_entry_peer_irk(
220215
SecurityEntryIdentity_t* current_entry = read_in_entry_peer_identity(db_handle);
221216
current_entry->irk = irk;
222217

223-
db_write_entry(current_entry, DB_ENTRY_PEER_IDENTITY, entry->index);
218+
db_write_entry(current_entry, DB_ENTRY_PEER_IDENTITY, get_index(entry));
224219
}
225220

226221
void KVStoreSecurityDb::set_entry_peer_bdaddr(
@@ -238,7 +233,7 @@ void KVStoreSecurityDb::set_entry_peer_bdaddr(
238233
current_entry->identity_address = peer_address;
239234
current_entry->identity_address_is_public = address_is_public;
240235

241-
db_write_entry(current_entry, DB_ENTRY_PEER_IDENTITY, entry->index);
236+
db_write_entry(current_entry, DB_ENTRY_PEER_IDENTITY, get_index(entry));
242237
}
243238

244239
void KVStoreSecurityDb::set_entry_peer_csrk(
@@ -256,7 +251,7 @@ void KVStoreSecurityDb::set_entry_peer_csrk(
256251
SecurityEntrySigning_t* current_entry = read_in_entry_peer_signing(db_handle);
257252
current_entry->csrk = csrk;
258253

259-
db_write_entry(current_entry, DB_ENTRY_PEER_SIGNING, entry->index);
254+
db_write_entry(current_entry, DB_ENTRY_PEER_SIGNING, get_index(entry));
260255
}
261256

262257
void KVStoreSecurityDb::set_entry_peer_sign_counter(
@@ -349,16 +344,12 @@ void KVStoreSecurityDb::reset_entry(entry_handle_t db_handle)
349344
return;
350345
}
351346

352-
if (entry->index != ENTRY_INVALID) {
353-
uint8_t zero_buffer[sizeof(SecurityEntryKeys_t)] = {0};
347+
uint8_t zero_buffer[sizeof(SecurityEntryKeys_t)] = {0};
354348

355-
db_write_entry((SecurityEntryKeys_t*)zero_buffer, DB_ENTRY_LOCAL_KEYS, entry->index);
356-
db_write_entry((SecurityEntryIdentity_t*)zero_buffer, DB_ENTRY_PEER_IDENTITY, entry->index);
357-
db_write_entry((SecurityEntryKeys_t*)zero_buffer, DB_ENTRY_PEER_KEYS, entry->index);
358-
db_write_entry((SecurityEntrySigning_t*)zero_buffer, DB_ENTRY_PEER_SIGNING, entry->index);
359-
360-
entry->index = ENTRY_INVALID;
361-
}
349+
db_write_entry((SecurityEntryKeys_t*)zero_buffer, DB_ENTRY_LOCAL_KEYS, get_index(entry));
350+
db_write_entry((SecurityEntryIdentity_t*)zero_buffer, DB_ENTRY_PEER_IDENTITY, get_index(entry));
351+
db_write_entry((SecurityEntryKeys_t*)zero_buffer, DB_ENTRY_PEER_KEYS, get_index(entry));
352+
db_write_entry((SecurityEntrySigning_t*)zero_buffer, DB_ENTRY_PEER_SIGNING, get_index(entry));
362353

363354
entry->flags = SecurityDistributionFlags_t();
364355
entry->peer_sign_counter = 0;
@@ -372,7 +363,7 @@ SecurityEntryIdentity_t* KVStoreSecurityDb::read_in_entry_peer_identity(entry_ha
372363
}
373364

374365
SecurityEntryIdentity_t* identity = reinterpret_cast<SecurityEntryIdentity_t*>(_buffer);
375-
db_read_entry(identity, DB_ENTRY_PEER_IDENTITY, entry->index);
366+
db_read_entry(identity, DB_ENTRY_PEER_IDENTITY, get_index(entry));
376367

377368
return identity;
378369
};
@@ -385,7 +376,7 @@ SecurityEntryKeys_t* KVStoreSecurityDb::read_in_entry_peer_keys(entry_handle_t d
385376
}
386377

387378
SecurityEntryKeys_t* keys = reinterpret_cast<SecurityEntryKeys_t*>(_buffer);
388-
db_read_entry(keys, DB_ENTRY_PEER_KEYS, entry->index);
379+
db_read_entry(keys, DB_ENTRY_PEER_KEYS, get_index(entry));
389380

390381
return keys;
391382
};
@@ -398,7 +389,7 @@ SecurityEntryKeys_t* KVStoreSecurityDb::read_in_entry_local_keys(entry_handle_t
398389
}
399390

400391
SecurityEntryKeys_t* keys = reinterpret_cast<SecurityEntryKeys_t*>(_buffer);
401-
db_read_entry(keys, DB_ENTRY_LOCAL_KEYS, entry->index);
392+
db_read_entry(keys, DB_ENTRY_LOCAL_KEYS, get_index(entry));
402393

403394
return keys;
404395
};
@@ -412,7 +403,7 @@ SecurityEntrySigning_t* KVStoreSecurityDb::read_in_entry_peer_signing(entry_hand
412403

413404
/* only read in the csrk */
414405
csrk_t* csrk = reinterpret_cast<csrk_t*>(_buffer);
415-
db_read_entry(csrk, DB_ENTRY_PEER_SIGNING, entry->index);
406+
db_read_entry(csrk, DB_ENTRY_PEER_SIGNING,get_index(entry));
416407

417408

418409
/* use the counter held in memory */

connectivity/FEATURE_BLE/source/generic/KVStoreSecurityDb.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class KVStoreSecurityDb : public SecurityDb {
3838
struct entry_t {
3939
SecurityDistributionFlags_t flags;
4040
sign_count_t peer_sign_counter;
41-
uint8_t index;
4241
};
4342

4443
static constexpr uint8_t KVSTORESECURITYDB_VERSION = 1;
@@ -228,6 +227,11 @@ class KVStoreSecurityDb : public SecurityDb {
228227
private:
229228
entry_t _entries[BLE_SECURITY_DATABASE_MAX_ENTRIES];
230229
uint8_t _buffer[sizeof(SecurityEntryKeys_t)];
230+
231+
uint8_t get_index(entry_t *entry)
232+
{
233+
return entry - _entries;
234+
}
231235
};
232236

233237
} /* namespace ble */

connectivity/FEATURE_BLE/source/generic/SecurityDb.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,21 @@ class SecurityDb {
181181
entry_handle_t correct_handle = find_entry_by_peer_ediv_rand(ediv, rand);
182182
if (!correct_handle) {
183183
cb(*db_handle, NULL);
184+
return;
184185
}
185186
// Note: keys should never be null as a matching entry has been retrieved
186187
SecurityEntryKeys_t* keys = read_in_entry_local_keys(correct_handle);
187188
MBED_ASSERT(keys);
189+
190+
/* set flags connected */
191+
SecurityDistributionFlags_t* flags = get_distribution_flags(correct_handle);
192+
flags->connected = true;
193+
194+
/* update peer address */
195+
SecurityDistributionFlags_t* old_flags = get_distribution_flags(*db_handle);
196+
flags->peer_address = old_flags->peer_address;
197+
flags->peer_address_is_public = old_flags->peer_address_is_public;
198+
188199
close_entry(*db_handle, false);
189200
*db_handle = correct_handle;
190201
cb(*db_handle, keys);
@@ -494,6 +505,7 @@ class SecurityDb {
494505
) {
495506
entry_handle_t db_handle = find_entry_by_peer_address(peer_address_type, peer_address);
496507
if (db_handle) {
508+
((SecurityDistributionFlags_t*)db_handle)->connected = true;
497509
return db_handle;
498510
}
499511

@@ -507,6 +519,7 @@ class SecurityDb {
507519
* by identity address */
508520
flags->peer_address = peer_address;
509521
flags->peer_address_is_public = peer_address_public;
522+
flags->connected = true;
510523
return flags;
511524
}
512525

0 commit comments

Comments
 (0)