3333#include " source/pal/PalSecurityManager.h"
3434
3535
36+ // Cordio defines the random address used by connection to be the global one
37+ #define CORDIO_GLOBAL_RANDOM_ADDRESS_FOR_CONNECTION 1
38+
3639using namespace std ::chrono;
3740
3841MBED_STATIC_ASSERT (BLE_GAP_MAX_ADVERTISING_SETS < 0xFF , " BLE_GAP_MAX_ADVERTISING_SETS must be less than 255" );
@@ -1859,6 +1862,9 @@ ble_error_t Gap::startAdvertising(
18591862 if (is_extended_advertising_available ()) {
18601863 // Addresses can be updated if the set is not advertising
18611864 if (!_active_sets.get (handle)) {
1865+ #if CORDIO_GLOBAL_RANDOM_ADDRESS_FOR_CONNECTION
1866+ _pal_gap.set_random_address (*random_address);
1867+ #endif
18621868 _pal_gap.set_advertising_set_random_address (handle, *random_address);
18631869 }
18641870
@@ -2229,7 +2235,7 @@ void Gap::signal_connection_complete(
22292235 address_resolved = true ;
22302236 }
22312237 }
2232- #endif BLE_ROLE_CENTRAL
2238+ #endif
22332239
22342240#if BLE_ROLE_PERIPHERAL
22352241 if (event.getOwnRole () == connection_role_t ::PERIPHERAL) {
@@ -2264,6 +2270,9 @@ void Gap::signal_connection_complete(
22642270
22652271 /* if successful then proceed to call the handler immediately same as for when privacy is disabled */
22662272 if (address_resolved) {
2273+ if (!apply_peripheral_privacy_connection_policy (event)) {
2274+ return ;
2275+ }
22672276 report_internal_connection_complete (event);
22682277 _event_handler->onConnectionComplete (event);
22692278 } else {
@@ -2297,15 +2306,64 @@ void Gap::signal_connection_complete(
22972306}
22982307
22992308#if BLE_FEATURE_PRIVACY
2309+
2310+ bool Gap::apply_peripheral_privacy_connection_policy (
2311+ const ConnectionCompleteEvent &event
2312+ )
2313+ {
2314+ #if BLE_ROLE_PERIPHERAL
2315+ if (event.getOwnRole () != connection_role_t ::PERIPHERAL) {
2316+ return true ;
2317+ }
2318+
2319+ if (event.getPeerAddressType () != peer_address_type_t ::RANDOM) {
2320+ return true ;
2321+ }
2322+
2323+ if (!is_random_private_resolvable_address (event.getPeerAddress ())) {
2324+ return true ;
2325+ }
2326+
2327+ auto connection_handle = event.getConnectionHandle ();
2328+
2329+ switch (_peripheral_privacy_configuration.resolution_strategy ) {
2330+ case peripheral_privacy_configuration_t ::REJECT_NON_RESOLVED_ADDRESS:
2331+ _pal_gap.disconnect (
2332+ connection_handle,
2333+ local_disconnection_reason_t ::AUTHENTICATION_FAILURE
2334+ );
2335+ return false ;
2336+
2337+ case peripheral_privacy_configuration_t ::PERFORM_PAIRING_PROCEDURE:
2338+ _event_queue.post ([connection_handle] {
2339+ BLE::Instance ().securityManager ().requestAuthentication (connection_handle);
2340+ });
2341+ return true ;
2342+
2343+ case peripheral_privacy_configuration_t ::PERFORM_AUTHENTICATION_PROCEDURE:
2344+ _event_queue.post ([connection_handle] {
2345+ BLE::Instance ().securityManager ().setLinkSecurity (
2346+ connection_handle,
2347+ ble::SecurityManager::SecurityMode_t::SECURITY_MODE_ENCRYPTION_WITH_MITM
2348+ );
2349+ });
2350+ return true ;
2351+
2352+ default :
2353+ return true ;
2354+ }
2355+ #else
2356+ return true ;
2357+ #endif
2358+ }
2359+
2360+
23002361void Gap::conclude_signal_connection_complete_after_address_resolution (
23012362 ConnectionCompleteEvent &event,
23022363 target_peer_address_type_t identity_address_type,
23032364 const address_t *identity_address
23042365)
23052366{
2306- #if BLE_ROLE_PERIPHERAL
2307- bool resolvable_address_not_known = false ;
2308- #endif // BLE_ROLE_PERIPHERAL
23092367 /* fix the event addresses */
23102368 if (identity_address) {
23112369 /* move old address to resolvable address */
@@ -2317,42 +2375,13 @@ void Gap::conclude_signal_connection_complete_after_address_resolution(
23172375 peer_address_type_t ::RANDOM_STATIC_IDENTITY
23182376 : peer_address_type_t ::PUBLIC_IDENTITY);
23192377 }
2320- #if BLE_ROLE_PERIPHERAL
2321- if (!identity_address) {
2322- if (_peripheral_privacy_configuration.resolution_strategy ==
2323- peripheral_privacy_configuration_t ::REJECT_NON_RESOLVED_ADDRESS) {
2324- // Reject connection request - the user will get notified through a callback
2325- _pal_gap.disconnect (
2326- event.getConnectionHandle (),
2327- local_disconnection_reason_t ::AUTHENTICATION_FAILURE
2328- );
2329- return ;
2330- }
2331- resolvable_address_not_known = true ;
2378+
2379+ if (!apply_peripheral_privacy_connection_policy (event)) {
2380+ return ;
23322381 }
2333- #endif // BLE_ROLE_PERIPHERAL
23342382
23352383 report_internal_connection_complete (event);
23362384 _event_handler->onConnectionComplete (event);
2337- #if BLE_ROLE_PERIPHERAL
2338- #if BLE_FEATURE_SECURITY
2339- if (resolvable_address_not_known) {
2340- ble::SecurityManager &sm = BLE::Instance ().securityManager ();
2341- if (_peripheral_privacy_configuration.resolution_strategy ==
2342- peripheral_privacy_configuration_t ::PERFORM_PAIRING_PROCEDURE) {
2343-
2344- // Request authentication to start pairing procedure
2345- sm.requestAuthentication (event.getConnectionHandle ());
2346- } else if (_peripheral_privacy_configuration.resolution_strategy ==
2347- peripheral_privacy_configuration_t ::PERFORM_AUTHENTICATION_PROCEDURE) {
2348- sm.setLinkSecurity (
2349- event.getConnectionHandle (),
2350- ble::SecurityManager::SecurityMode_t::SECURITY_MODE_ENCRYPTION_WITH_MITM
2351- );
2352- }
2353- }
2354- #endif // BLE_FEATURE_SECURITY
2355- #endif // BLE_ROLE_PERIPHERAL
23562385}
23572386#endif // BLE_FEATURE_PRIVACY
23582387#endif // BLE_FEATURE_CONNECTABLE
@@ -3075,7 +3104,7 @@ void Gap::on_address_resolution_completed(
30753104
30763105 delete event;
30773106 }
3078- #endif BLE_ROLE_OBSERVER
3107+ #endif // BLE_ROLE_OBSERVER
30793108#endif // BLE_FEATURE_PRIVACY
30803109}
30813110
@@ -3132,6 +3161,7 @@ const address_t *Gap::get_random_address(controller_operation_t operation, size_
31323161 bool advertising_use_main_address = true ;
31333162 // Extended advertising is a special case as the address isn't shared with
31343163 // the main address.
3164+ #if !CORDIO_GLOBAL_RANDOM_ADDRESS_FOR_CONNECTION
31353165#if BLE_FEATURE_EXTENDED_ADVERTISING
31363166 if (is_extended_advertising_available ()) {
31373167 if (operation == controller_operation_t ::advertising) {
@@ -3145,6 +3175,8 @@ const address_t *Gap::get_random_address(controller_operation_t operation, size_
31453175 }
31463176 }
31473177#endif
3178+ #endif
3179+
31483180
31493181 // For other cases we first compute the address being used and then compares
31503182 // it to the address to use to determine if the address is correct or not.
0 commit comments