Skip to content

Commit f4f3a3c

Browse files
store ltk and csrk but not irk sent status
and missing asserts
1 parent a7f8d54 commit f4f3a3c

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

features/FEATURE_BLE/ble/generic/SecurityDb.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ struct SecurityDistributionFlags_t {
4040
ltk_stored(false),
4141
ltk_sent(false),
4242
irk_stored(false),
43-
irk_sent(false),
4443
csrk_mitm_protected(false),
4544
ltk_mitm_protected(false),
4645
secure_connections_paired(false),
@@ -63,7 +62,6 @@ struct SecurityDistributionFlags_t {
6362
uint8_t ltk_sent:1;
6463
/** the security entry has been distributed and stored */
6564
uint8_t irk_stored:1;
66-
uint8_t irk_sent:1;
6765

6866
/** CSRK that is stored has MITM protection */
6967
uint8_t csrk_mitm_protected:1;
@@ -677,11 +675,9 @@ class SecurityDb {
677675
/* we settle for any disconnected if we don't find an empty one */
678676
match = flags;
679677
if (!flags->csrk_stored
680-
&& !flags->csrk_sent
681678
&& !flags->ltk_stored
682679
&& !flags->ltk_sent
683-
&& !flags->irk_stored
684-
&& !flags->irk_sent) {
680+
&& !flags->irk_stored) {
685681
/* empty one found, stop looking*/
686682
break;
687683
}

features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ ble_error_t GenericSecurityManager::enableSigning(
354354

355355
if (enabled && !cb->signing_requested && !_default_key_distribution.get_signing()) {
356356
cb->signing_requested = true;
357-
if (flags->csrk_stored) {
357+
if (flags->csrk_stored && flags->csrk_sent) {
358358
/* used the stored ones when available */
359359
_db->get_entry_peer_csrk(
360360
mbed::callback(this, &GenericSecurityManager::set_peer_csrk_cb),
@@ -389,7 +389,7 @@ ble_error_t GenericSecurityManager::getLinkEncryption(
389389
connection_handle_t connection,
390390
link_encryption_t *encryption
391391
) {
392-
392+
MBED_ASSERT(_db);
393393
ControlBlock_t *cb = get_control_block(connection);
394394
if (!cb) {
395395
return BLE_ERROR_INVALID_PARAM;
@@ -419,6 +419,7 @@ ble_error_t GenericSecurityManager::setLinkEncryption(
419419
connection_handle_t connection,
420420
link_encryption_t encryption
421421
) {
422+
MBED_ASSERT(_db);
422423
ControlBlock_t *cb = get_control_block(connection);
423424
if (!cb) {
424425
return BLE_ERROR_INVALID_PARAM;
@@ -475,6 +476,7 @@ ble_error_t GenericSecurityManager::getEncryptionKeySize(
475476
connection_handle_t connection,
476477
uint8_t *size
477478
) {
479+
MBED_ASSERT(_db);
478480
ControlBlock_t *cb = get_control_block(connection);
479481
if (!cb) {
480482
return BLE_ERROR_INVALID_PARAM;
@@ -547,6 +549,7 @@ ble_error_t GenericSecurityManager::setPrivateAddressTimeout(uint16_t timeout_in
547549
//
548550

549551
ble_error_t GenericSecurityManager::requestAuthentication(connection_handle_t connection) {
552+
MBED_ASSERT(_db);
550553
ControlBlock_t *cb = get_control_block(connection);
551554
if (!cb) {
552555
return BLE_ERROR_INVALID_PARAM;
@@ -663,6 +666,7 @@ ble_error_t GenericSecurityManager::legacyPairingOobReceived(
663666
const address_t *address,
664667
const oob_tk_t *tk
665668
) {
669+
MBED_ASSERT(_db);
666670
if (address && tk) {
667671
ControlBlock_t *cb = get_control_block(*address);
668672
if (!cb) {
@@ -792,6 +796,7 @@ void GenericSecurityManager::enable_encryption_cb(
792796
SecurityDb::entry_handle_t db_entry,
793797
const SecurityEntryKeys_t* entryKeys
794798
) {
799+
MBED_ASSERT(_db);
795800
ControlBlock_t *cb = get_control_block(db_entry);
796801
if (!cb) {
797802
return;
@@ -815,6 +820,7 @@ void GenericSecurityManager::set_ltk_cb(
815820
SecurityDb::entry_handle_t db_entry,
816821
const SecurityEntryKeys_t* entryKeys
817822
) {
823+
MBED_ASSERT(_db);
818824
ControlBlock_t *cb = get_control_block(db_entry);
819825
if (!cb) {
820826
return;
@@ -858,6 +864,7 @@ void GenericSecurityManager::return_csrk_cb(
858864
SecurityDb::entry_handle_t db_entry,
859865
const SecurityEntrySigning_t *signing
860866
) {
867+
MBED_ASSERT(_db);
861868
ControlBlock_t *cb = get_control_block(db_entry);
862869
if (!cb || !signing) {
863870
return;
@@ -876,6 +883,7 @@ void GenericSecurityManager::return_csrk_cb(
876883
}
877884

878885
void GenericSecurityManager::update_oob_presence(connection_handle_t connection) {
886+
MBED_ASSERT(_db);
879887
ControlBlock_t *cb = get_control_block(connection);
880888
if (!cb) {
881889
return;
@@ -1142,6 +1150,7 @@ void GenericSecurityManager::on_slave_security_request(
11421150
connection_handle_t connection,
11431151
AuthenticationMask authentication
11441152
) {
1153+
MBED_ASSERT(_db);
11451154
ControlBlock_t *cb = get_control_block(connection);
11461155
if (!cb) {
11471156
return;
@@ -1276,6 +1285,7 @@ void GenericSecurityManager::on_secure_connections_oob_request(connection_handle
12761285
}
12771286

12781287
void GenericSecurityManager::on_legacy_pairing_oob_request(connection_handle_t connection) {
1288+
MBED_ASSERT(_db);
12791289
ControlBlock_t *cb = get_control_block(connection);
12801290
if (!cb) {
12811291
return;
@@ -1334,6 +1344,7 @@ void GenericSecurityManager::on_secure_connections_ltk_generated(
13341344

13351345
flags->ltk_mitm_protected = cb->mitm_performed;
13361346
flags->secure_connections_paired = true;
1347+
flags->ltk_stored = true;
13371348

13381349
_db->set_entry_peer_ltk(cb->db_entry, ltk);
13391350
}
@@ -1354,6 +1365,7 @@ void GenericSecurityManager::on_keys_distributed_ltk(
13541365
}
13551366

13561367
flags->ltk_mitm_protected = cb->mitm_performed;
1368+
flags->ltk_stored = true;
13571369
_db->set_entry_peer_ltk(cb->db_entry, ltk);
13581370
}
13591371

@@ -1381,6 +1393,12 @@ void GenericSecurityManager::on_keys_distributed_local_ltk(
13811393
return;
13821394
}
13831395

1396+
SecurityDistributionFlags_t* flags = _db->get_distribution_flags(cb->db_entry);
1397+
if (!flags) {
1398+
return;
1399+
}
1400+
1401+
flags->ltk_sent = true;
13841402
_db->set_entry_local_ltk(cb->db_entry, ltk);
13851403
}
13861404

@@ -1408,6 +1426,12 @@ void GenericSecurityManager::on_keys_distributed_irk(
14081426
return;
14091427
}
14101428

1429+
SecurityDistributionFlags_t* flags = _db->get_distribution_flags(cb->db_entry);
1430+
if (!flags) {
1431+
return;
1432+
}
1433+
1434+
flags->irk_stored = true;
14111435
_db->set_entry_peer_irk(cb->db_entry, irk);
14121436
}
14131437

@@ -1445,6 +1469,7 @@ void GenericSecurityManager::on_keys_distributed_csrk(
14451469
}
14461470

14471471
flags->csrk_mitm_protected = cb->mitm_performed;
1472+
flags->csrk_stored = true;
14481473

14491474
_db->set_entry_peer_csrk(cb->db_entry, csrk);
14501475

@@ -1466,12 +1491,19 @@ void GenericSecurityManager::on_ltk_request(
14661491
return;
14671492
}
14681493

1469-
_db->get_entry_local_keys(
1470-
mbed::callback(this, &GenericSecurityManager::set_ltk_cb),
1471-
cb->db_entry,
1472-
ediv,
1473-
rand
1474-
);
1494+
SecurityDistributionFlags_t* flags = _db->get_distribution_flags(cb->db_entry);
1495+
if (!flags) {
1496+
return;
1497+
}
1498+
1499+
if (flags->ltk_stored) {
1500+
_db->get_entry_local_keys(
1501+
mbed::callback(this, &GenericSecurityManager::set_ltk_cb),
1502+
cb->db_entry,
1503+
ediv,
1504+
rand
1505+
);
1506+
}
14751507
}
14761508

14771509
/* control blocks list management */
@@ -1542,6 +1574,7 @@ GenericSecurityManager::ControlBlock_t* GenericSecurityManager::get_control_bloc
15421574
GenericSecurityManager::ControlBlock_t* GenericSecurityManager::get_control_block(
15431575
const address_t &peer_address
15441576
) {
1577+
MBED_ASSERT(_db);
15451578
for (size_t i = 0; i < MAX_CONTROL_BLOCKS; i++) {
15461579
ControlBlock_t *cb = &_control_blocks[i];
15471580
if (cb->connected) {

0 commit comments

Comments
 (0)