Skip to content

Commit 4e5639f

Browse files
committed
BLE: Support encryption with secure connection key.
1 parent f79eeb0 commit 4e5639f

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

features/FEATURE_BLE/ble/SecurityManager.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -967,15 +967,31 @@ class SecurityManager {
967967
*/
968968
ble_error_t getLinkSecurity(ble::connection_handle_t connectionHandle, LinkSecurityStatus_t *securityStatus) {
969969
ble::link_encryption_t encryption(ble::link_encryption_t::NOT_ENCRYPTED);
970-
ble_error_t status = getLinkEncryption(connectionHandle, &encryption);
971-
/* legacy support limits the return values */
972-
if (encryption.value() == ble::link_encryption_t::ENCRYPTED_WITH_MITM) {
973-
*securityStatus = ENCRYPTED;
974-
} else {
975-
*securityStatus = (LinkSecurityStatus_t)encryption.value();
970+
ble_error_t err = getLinkEncryption(connectionHandle, &encryption);
971+
if (err) {
972+
return err;
973+
}
974+
975+
switch (encryption.value()) {
976+
case ble::link_encryption_t::NOT_ENCRYPTED:
977+
*securityStatus = NOT_ENCRYPTED;
978+
break;
979+
case ble::link_encryption_t::ENCRYPTION_IN_PROGRESS:
980+
*securityStatus = ENCRYPTION_IN_PROGRESS;
981+
break;
982+
case ble::link_encryption_t::ENCRYPTED:
983+
case ble::link_encryption_t::ENCRYPTED_WITH_MITM:
984+
case ble::link_encryption_t::ENCRYPTED_WITH_SC_AND_MITM:
985+
*securityStatus = ENCRYPTED;
986+
break;
987+
default:
988+
// should never happen
989+
MBED_ASSERT(false);
990+
*securityStatus = NOT_ENCRYPTED;
991+
break;
976992
}
977993

978-
return status;
994+
return BLE_ERROR_NONE;
979995
}
980996

981997
/**
@@ -1079,7 +1095,10 @@ class SecurityManager {
10791095
SecurityManager::SecurityMode_t securityMode;
10801096
if (result == ble::link_encryption_t::ENCRYPTED) {
10811097
securityMode = SECURITY_MODE_ENCRYPTION_NO_MITM;
1082-
} else if (result == ble::link_encryption_t::ENCRYPTED_WITH_MITM) {
1098+
} else if (
1099+
result == ble::link_encryption_t::ENCRYPTED_WITH_MITM ||
1100+
result == ble::link_encryption_t::ENCRYPTED_WITH_SC_AND_MITM
1101+
) {
10831102
securityMode = SECURITY_MODE_ENCRYPTION_WITH_MITM;
10841103
} else {
10851104
securityMode = SECURITY_MODE_ENCRYPTION_OPEN_LINK;

features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,11 @@ ble_error_t GenericSecurityManager::getLinkEncryption(
369369

370370
if (cb->encrypted) {
371371
if (cb->ltk_mitm_protected || cb->mitm_performed) {
372-
*encryption = link_encryption_t::ENCRYPTED_WITH_MITM;
372+
if (cb->secure_connections_paired) {
373+
*encryption = link_encryption_t::ENCRYPTED_WITH_SC_AND_MITM;
374+
} else {
375+
*encryption = link_encryption_t::ENCRYPTED_WITH_MITM;
376+
}
373377
} else {
374378
*encryption = link_encryption_t::ENCRYPTED;
375379
}
@@ -408,7 +412,9 @@ ble_error_t GenericSecurityManager::setLinkEncryption(
408412
} else if (encryption == link_encryption_t::ENCRYPTED) {
409413

410414
/* only change if we're not already encrypted with mitm */
411-
if (current_encryption != link_encryption_t::ENCRYPTED_WITH_MITM) {
415+
if (current_encryption != link_encryption_t::ENCRYPTED_WITH_MITM ||
416+
current_encryption != link_encryption_t::ENCRYPTED_WITH_SC_AND_MITM
417+
) {
412418
cb->encryption_requested = true;
413419
return enable_encryption(connection);
414420
}
@@ -423,6 +429,19 @@ ble_error_t GenericSecurityManager::setLinkEncryption(
423429
return requestAuthentication(connection);
424430
}
425431

432+
} else if (encryption == link_encryption_t::ENCRYPTED_WITH_SC_AND_MITM) {
433+
434+
if (cb->ltk_mitm_protected &&
435+
cb->secure_connections_paired && !
436+
cb->encrypted
437+
) {
438+
cb->encryption_requested = true;
439+
return enable_encryption(connection);
440+
} else {
441+
cb->encryption_requested = true;
442+
return requestAuthentication(connection);
443+
}
444+
426445
} else {
427446
return BLE_ERROR_INVALID_PARAM;
428447
}
@@ -1059,7 +1078,10 @@ void GenericSecurityManager::on_link_encryption_result(
10591078
cb->encryption_failed = false;
10601079
cb->encrypted = true;
10611080

1062-
} else if (result == link_encryption_t::ENCRYPTED_WITH_MITM) {
1081+
} else if (
1082+
result == link_encryption_t::ENCRYPTED_WITH_MITM ||
1083+
result == link_encryption_t::ENCRYPTED_WITH_SC_AND_MITM
1084+
) {
10631085

10641086
cb->encryption_requested = false;
10651087
cb->encryption_failed = false;

0 commit comments

Comments
 (0)