Skip to content

Commit fd2a96e

Browse files
author
Cruz Monrreal
authored
Merge pull request #9339 from costanic/fix_oob
Fix Out-Of-Band (OOB) data generation for BLE OOB pairing
2 parents 74f5723 + 7795e30 commit fd2a96e

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -653,14 +653,20 @@ ble_error_t GenericSecurityManager::generateOOB(
653653
/* Secure connections. Avoid generating if we're already waiting for it.
654654
* If a local random is set to 0 it means we're already calculating. */
655655
if (!is_all_zeros(_oob_local_random)) {
656-
status = _pal.generate_secure_connections_oob();
656+
/* save the current values in case the call to
657+
* generate_secure_connections_oob fails */
658+
address_t orig_local_address = _oob_local_address;
659+
oob_lesc_value_t orig_local_random = _oob_local_random;
660+
661+
_oob_local_address = *address;
662+
/* this will be updated when calculation completes,
663+
* a value of all zeros is an invalid random value */
664+
set_all_zeros(_oob_local_random);
657665

658-
if (status == BLE_ERROR_NONE) {
659-
_oob_local_address = *address;
660-
/* this will be updated when calculation completes,
661-
* a value of all zeros is an invalid random value */
662-
set_all_zeros(_oob_local_random);
663-
} else if (status != BLE_ERROR_NOT_IMPLEMENTED) {
666+
status = _pal.generate_secure_connections_oob();
667+
if (status != BLE_ERROR_NONE && status != BLE_ERROR_NOT_IMPLEMENTED) {
668+
_oob_local_address = orig_local_address;
669+
_oob_local_random = orig_local_random;
664670
return status;
665671
}
666672
} else {

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NORDIC_SOFTDEVICE/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include <stdint.h>
18+
#include "platform/mbed_assert.h"
1819
#include "nRF5xPalSecurityManager.h"
1920
#include "nRF5xn.h"
2021
#include "ble/Gap.h"
@@ -80,11 +81,6 @@ struct nRF5xSecurityManager::pairing_control_block_t {
8081
ble_gap_id_key_t peer_id_key;
8182
ble_gap_sign_info_t peer_sign_key;
8283
ble_gap_lesc_p256_pk_t peer_pk;
83-
84-
// flag required to help DHKey computation/process; should be removed with
85-
// later versions of the softdevice
86-
uint8_t own_oob:1;
87-
uint8_t peer_oob:1;
8884
};
8985

9086
nRF5xSecurityManager::nRF5xSecurityManager()
@@ -662,26 +658,37 @@ ble_error_t nRF5xSecurityManager::secure_connections_oob_request_reply(
662658
const oob_lesc_value_t &peer_random,
663659
const oob_confirm_t &peer_confirm
664660
) {
661+
bool have_oob_own;
662+
bool have_oob_peer;
663+
const oob_lesc_value_t zerokey;
664+
ble_gap_lesc_oob_data_t oob_own;
665+
ble_gap_lesc_oob_data_t oob_peer;
666+
665667
pairing_control_block_t* pairing_cb = get_pairing_cb(connection);
666668
if (!pairing_cb) {
667669
return BLE_ERROR_INVALID_STATE;
668670
}
669671

670-
ble_gap_lesc_oob_data_t oob_own;
671-
ble_gap_lesc_oob_data_t oob_peer;
672-
673-
// is own address important ?
674-
memcpy(oob_own.r, local_random.data(), local_random.size());
675-
// FIXME: What to do with local confirm ???
672+
have_oob_own = false;
673+
if (local_random != zerokey) {
674+
have_oob_own = true;
675+
// is own address important ?
676+
memcpy(oob_own.r, local_random.data(), local_random.size());
677+
// FIXME: What to do with local confirm ???
678+
}
676679

677-
// is peer address important ?
678-
memcpy(oob_peer.r, peer_random.data(), peer_random.size());
679-
memcpy(oob_peer.c, peer_confirm.data(), peer_confirm.size());
680+
have_oob_peer = false;
681+
if (peer_random != zerokey && peer_confirm != zerokey) {
682+
have_oob_peer = true;
683+
// is peer address important ?
684+
memcpy(oob_peer.r, peer_random.data(), peer_random.size());
685+
memcpy(oob_peer.c, peer_confirm.data(), peer_confirm.size());
686+
}
680687

681688
uint32_t err = sd_ble_gap_lesc_oob_data_set(
682689
connection,
683-
pairing_cb->own_oob ? &oob_own : NULL,
684-
pairing_cb->peer_oob ? &oob_peer : NULL
690+
have_oob_own ? &oob_own : NULL,
691+
have_oob_peer ? &oob_peer : NULL
685692
);
686693

687694
return convert_sd_error(err);
@@ -734,7 +741,9 @@ ble_error_t nRF5xSecurityManager::generate_secure_connections_oob()
734741
ble_gap_lesc_p256_pk_t own_secret;
735742
ble_gap_lesc_oob_data_t oob_data;
736743

737-
memcpy(own_secret.pk, secret.data(), secret.size());
744+
MBED_ASSERT(sizeof(own_secret.pk) >= X.size() + Y.size());
745+
memcpy(own_secret.pk, X.data(), X.size());
746+
memcpy(own_secret.pk + X.size(), Y.data(), Y.size());
738747

739748
uint32_t err = sd_ble_gap_lesc_oob_data_get(
740749
BLE_CONN_HANDLE_INVALID,

0 commit comments

Comments
 (0)