Skip to content

Commit 110b190

Browse files
fix privacy initialisation and give access to local identity
Initialises identity addres when privacy is enabled. Stores the identity. Retrieves local identity if previously stored.
1 parent 73b4beb commit 110b190

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

features/FEATURE_BLE/ble/SecurityManager.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,22 @@ class SecurityManager {
285285
(void)result;
286286
}
287287

288+
/**
289+
* Indicate that a peer address has been saved by the security manager or if we are
290+
* bonded to the peer the identity has been retrieved from the database on connection.
291+
*
292+
* @param[in] connectionHandle Connection handle.
293+
* @param[in] peer_address Peer address that has been saved by the security database, NULL it not found.
294+
* @param[in] address_is_public Address type, true if public. Invalid if peer_address NULL.
295+
*/
296+
virtual void peerIdentity(ble::connection_handle_t connectionHandle,
297+
const address_t *peer_address,
298+
bool address_is_public) {
299+
(void)connectionHandle;
300+
(void)peer_address;
301+
(void)address_is_public;
302+
}
303+
288304
////////////////////////////////////////////////////////////////////////////
289305
// Security
290306
//
@@ -561,6 +577,14 @@ class SecurityManager {
561577
*/
562578
ble_error_t setPairingRequestAuthorisation(bool required = true);
563579

580+
/**
581+
* Retrieve identity address for the peer on the given connection.
582+
*
583+
* @param[in] connectionHandle Handle to identify the connection.
584+
* @return BLE_ERROR_NONE or appropriate error code indicating the failure reason.
585+
*/
586+
ble_error_t getPeerIdentity(ble::connection_handle_t connectionHandle);
587+
564588
////////////////////////////////////////////////////////////////////////////
565589
// Feature support
566590
//
@@ -899,6 +923,10 @@ class SecurityManager {
899923
ble::connection_handle_t connectionHandle
900924
);
901925

926+
ble_error_t getPeerIdentity_(
927+
ble::connection_handle_t connectionHandle
928+
);
929+
902930
ble_error_t setPairingRequestAuthorisation_(
903931
bool required
904932
);

features/FEATURE_BLE/ble/generic/GenericSecurityManager.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class GenericSecurityManager :
124124
bool required = true
125125
);
126126

127+
ble_error_t getPeerIdentity_(
128+
connection_handle_t connection
129+
);
130+
127131
////////////////////////////////////////////////////////////////////////////
128132
// Feature support
129133
//
@@ -321,6 +325,13 @@ class GenericSecurityManager :
321325
*/
322326
ble_error_t init_signing();
323327

328+
/**
329+
* Generate the IRK if needed.
330+
*
331+
* @return BLE_ERROR_NONE or appropriate error code indicating the failure reason.
332+
*/
333+
ble_error_t init_identity();
334+
324335
/**
325336
* Fills the buffer with the specified number of bytes of random data
326337
* produced by the link controller

features/FEATURE_BLE/source/generic/GenericSecurityManager.tpp

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,19 @@ ble_error_t GenericSecurityManager<TPalSecurityManager, SigningMonitor>::init_(
102102
result = init_resolving_list();
103103
#endif
104104

105+
#if BLE_FEATURE_PRIVACY
106+
// set the local identity address and irk
107+
if (result != BLE_ERROR_NONE) {
108+
result = init_identity();
109+
}
110+
#endif // BLE_FEATURE_PRIVACY
111+
105112
if (result != BLE_ERROR_NONE) {
106113
delete _db;
107114
_db = NULL;
108-
return result;
109115
}
110116

111-
return BLE_ERROR_NONE;
117+
return result;
112118
}
113119

114120
template<template<class> class TPalSecurityManager, template<class> class SigningMonitor>
@@ -309,6 +315,33 @@ ble_error_t GenericSecurityManager<TPalSecurityManager, SigningMonitor>::setPair
309315
return BLE_ERROR_NONE;
310316
}
311317

318+
template<template<class> class TPalSecurityManager, template<class> class SigningMonitor>
319+
ble_error_t GenericSecurityManager<TPalSecurityManager, SigningMonitor>::getPeerIdentity_(connection_handle_t connection) {
320+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
321+
if (eventHandler) {
322+
ControlBlock_t *cb = get_control_block(connection);
323+
if (!cb) {
324+
return BLE_ERROR_INVALID_PARAM;
325+
}
326+
327+
_db->get_entry_identity(
328+
[connection,this](SecurityDb::entry_handle_t handle, const SecurityEntryIdentity_t* identity) {
329+
if (eventHandler) {
330+
eventHandler->peerIdentity(
331+
connection,
332+
identity ? &identity->identity_address : nullptr,
333+
identity ? identity->identity_address_is_public : false
334+
);
335+
}
336+
},
337+
cb->db_entry
338+
);
339+
return BLE_ERROR_NONE;
340+
} else {
341+
return BLE_ERROR_INVALID_STATE;
342+
}
343+
}
344+
312345
////////////////////////////////////////////////////////////////////////////
313346
// Feature support
314347
//
@@ -901,6 +934,33 @@ ble_error_t GenericSecurityManager<TPalSecurityManager, SigningMonitor>::init_si
901934
return _pal.set_csrk(*pcsrk, local_sign_counter);
902935
}
903936

937+
template<template<class> class TPalSecurityManager, template<class> class SigningMonitor>
938+
ble_error_t GenericSecurityManager<TPalSecurityManager, SigningMonitor>::init_identity() {
939+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
940+
const irk_t *pirk = nullptr;
941+
942+
irk_t irk = _db->get_local_irk();
943+
if (irk != irk_t()) {
944+
pirk = &irk;
945+
} else {
946+
ble_error_t ret = get_random_data(irk.data(), irk.size());
947+
if (ret != BLE_ERROR_NONE) {
948+
return ret;
949+
}
950+
951+
pirk = &irk;
952+
address_t identity_address;
953+
bool public_address;
954+
ret = _pal.get_identity_address(identity_address, public_address);
955+
if (ret != BLE_ERROR_NONE) {
956+
return ret;
957+
}
958+
_db->set_local_identity(irk, identity_address, public_address);
959+
}
960+
961+
return _pal.set_irk(*pirk);
962+
}
963+
904964
template<template<class> class TPalSecurityManager, template<class> class SigningMonitor>
905965
ble_error_t GenericSecurityManager<TPalSecurityManager, SigningMonitor>::get_random_data(uint8_t *buffer, size_t size) {
906966
byte_array_t<8> random_data;

0 commit comments

Comments
 (0)