Skip to content

Commit 76f89f6

Browse files
committed
BLE: Factorize peripheral privacy applied when connected
1 parent e4b317c commit 76f89f6

File tree

2 files changed

+68
-35
lines changed

2 files changed

+68
-35
lines changed

connectivity/FEATURE_BLE/source/generic/GapImpl.cpp

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,9 @@ void Gap::signal_connection_complete(
23652365

23662366
/* if successful then proceed to call the handler immediately same as for when privacy is disabled */
23672367
if (address_resolved) {
2368+
if (!apply_peripheral_privacy_connection_policy(event)) {
2369+
return;
2370+
}
23682371
report_internal_connection_complete(event);
23692372
_event_handler->onConnectionComplete(event);
23702373
} else {
@@ -2398,15 +2401,64 @@ void Gap::signal_connection_complete(
23982401
}
23992402

24002403
#if BLE_FEATURE_PRIVACY
2404+
2405+
bool Gap::apply_peripheral_privacy_connection_policy(
2406+
const ConnectionCompleteEvent &event
2407+
)
2408+
{
2409+
#if BLE_ROLE_PERIPHERAL
2410+
if (event.getOwnRole() != connection_role_t::PERIPHERAL) {
2411+
return true;
2412+
}
2413+
2414+
if (event.getPeerAddressType() != peer_address_type_t::RANDOM) {
2415+
return true;
2416+
}
2417+
2418+
if (!is_random_private_resolvable_address(event.getPeerAddress())) {
2419+
return true;
2420+
}
2421+
2422+
auto connection_handle = event.getConnectionHandle();
2423+
2424+
switch (_peripheral_privacy_configuration.resolution_strategy) {
2425+
case peripheral_privacy_configuration_t::REJECT_NON_RESOLVED_ADDRESS:
2426+
_pal_gap.disconnect(
2427+
connection_handle,
2428+
local_disconnection_reason_t::AUTHENTICATION_FAILURE
2429+
);
2430+
return false;
2431+
2432+
case peripheral_privacy_configuration_t::PERFORM_PAIRING_PROCEDURE:
2433+
_event_queue.post([connection_handle] {
2434+
BLE::Instance().securityManager().requestAuthentication(connection_handle);
2435+
});
2436+
return true;
2437+
2438+
case peripheral_privacy_configuration_t::PERFORM_AUTHENTICATION_PROCEDURE:
2439+
_event_queue.post([connection_handle] {
2440+
BLE::Instance().securityManager().setLinkSecurity(
2441+
connection_handle,
2442+
ble::SecurityManager::SecurityMode_t::SECURITY_MODE_ENCRYPTION_WITH_MITM
2443+
);
2444+
});
2445+
return true;
2446+
2447+
default:
2448+
return true;
2449+
}
2450+
#else
2451+
return true;
2452+
#endif
2453+
}
2454+
2455+
24012456
void Gap::conclude_signal_connection_complete_after_address_resolution(
24022457
ConnectionCompleteEvent &event,
24032458
target_peer_address_type_t identity_address_type,
24042459
const address_t *identity_address
24052460
)
24062461
{
2407-
#if BLE_ROLE_PERIPHERAL
2408-
bool resolvable_address_not_known = false;
2409-
#endif // BLE_ROLE_PERIPHERAL
24102462
/* fix the event addresses */
24112463
if (identity_address) {
24122464
/* move old address to resolvable address */
@@ -2418,42 +2470,13 @@ void Gap::conclude_signal_connection_complete_after_address_resolution(
24182470
peer_address_type_t::RANDOM_STATIC_IDENTITY
24192471
: peer_address_type_t::PUBLIC_IDENTITY);
24202472
}
2421-
#if BLE_ROLE_PERIPHERAL
2422-
if (!identity_address) {
2423-
if (_peripheral_privacy_configuration.resolution_strategy ==
2424-
peripheral_privacy_configuration_t::REJECT_NON_RESOLVED_ADDRESS) {
2425-
// Reject connection request - the user will get notified through a callback
2426-
_pal_gap.disconnect(
2427-
event.getConnectionHandle(),
2428-
local_disconnection_reason_t::AUTHENTICATION_FAILURE
2429-
);
2430-
return;
2431-
}
2432-
resolvable_address_not_known = true;
2473+
2474+
if (!apply_peripheral_privacy_connection_policy(event)) {
2475+
return;
24332476
}
2434-
#endif // BLE_ROLE_PERIPHERAL
24352477

24362478
report_internal_connection_complete(event);
24372479
_event_handler->onConnectionComplete(event);
2438-
#if BLE_ROLE_PERIPHERAL
2439-
#if BLE_FEATURE_SECURITY
2440-
if (resolvable_address_not_known) {
2441-
ble::SecurityManager &sm = BLE::Instance().securityManager();
2442-
if (_peripheral_privacy_configuration.resolution_strategy ==
2443-
peripheral_privacy_configuration_t::PERFORM_PAIRING_PROCEDURE) {
2444-
2445-
// Request authentication to start pairing procedure
2446-
sm.requestAuthentication(event.getConnectionHandle());
2447-
} else if (_peripheral_privacy_configuration.resolution_strategy ==
2448-
peripheral_privacy_configuration_t::PERFORM_AUTHENTICATION_PROCEDURE) {
2449-
sm.setLinkSecurity(
2450-
event.getConnectionHandle(),
2451-
ble::SecurityManager::SecurityMode_t::SECURITY_MODE_ENCRYPTION_WITH_MITM
2452-
);
2453-
}
2454-
}
2455-
#endif // BLE_FEATURE_SECURITY
2456-
#endif // BLE_ROLE_PERIPHERAL
24572480
}
24582481
#endif // BLE_FEATURE_PRIVACY
24592482
#endif // BLE_FEATURE_CONNECTABLE

connectivity/FEATURE_BLE/source/generic/GapImpl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,16 @@ class Gap :
611611
void signal_connection_complete(ConnectionCompleteEvent& report);
612612

613613
#if BLE_FEATURE_PRIVACY
614+
/**
615+
* Apply the privacy policies when the local peripheral is connected.
616+
* @param event The connection event
617+
* @return true if the policy process has been successful and false if the
618+
* it fails meaning the process connection shouldn't continue.
619+
*/
620+
bool apply_peripheral_privacy_connection_policy(
621+
const ConnectionCompleteEvent &event
622+
);
623+
614624
/** Pass the connection complete event to the application after privacy resolution completed.
615625
*
616626
* @param event Event to be passed to the user application.

0 commit comments

Comments
 (0)