Skip to content

Commit ebb50a6

Browse files
Merge pull request #32 from pan-/sm-privacy-nordic
Sm privacy nordic
2 parents a2f206f + 9a0a086 commit ebb50a6

File tree

14 files changed

+792
-166
lines changed

14 files changed

+792
-166
lines changed

features/FEATURE_BLE/ble/BLETypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,10 @@ typedef uint32_t sign_count_t;
426426
*/
427427
struct address_t : public byte_array_t<6> {
428428
/**
429-
* Create an invalid mac address, equal to FF:FF:FF:FF:FF:FF
429+
* Create an invalid mac address, equal to 00:00:00:00:00:00
430430
*/
431431
address_t() {
432-
memset(_value, 0xFF, sizeof(_value));
432+
memset(_value, 0x00, sizeof(_value));
433433
}
434434

435435
/**

features/FEATURE_BLE/ble/Gap.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,12 @@ class Gap {
887887
*/
888888
static const uint16_t UNIT_1_25_MS = 1250;
889889

890+
static const PeripheralPrivacyConfiguration_t
891+
default_peripheral_privacy_configuration;
892+
893+
static const CentralPrivacyConfiguration_t
894+
default_central_privacy_configuration;
895+
890896
/**
891897
* Convert milliseconds into 1.25ms units.
892898
*
@@ -1062,7 +1068,7 @@ class Gap {
10621068
* the address in input was not identifiable as a random address.
10631069
*/
10641070
static ble_error_t getRandomAddressType(
1065-
BLEProtocol::AddressBytes_t address,
1071+
const BLEProtocol::AddressBytes_t address,
10661072
RandomAddressType_t* addressType
10671073
);
10681074

features/FEATURE_BLE/ble/generic/GenericSecurityManager.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "ble/pal/SigningEventMonitor.h"
2626
#include "ble/generic/GenericGap.h"
2727
#include "ble/pal/PalSecurityManager.h"
28+
#include "ble/ArrayView.h"
2829

2930
namespace ble {
3031
namespace generic {
@@ -400,21 +401,28 @@ class GenericSecurityManager : public SecurityManager,
400401
);
401402

402403
/**
403-
* Inform the security manager of a new connection.
404-
*
405-
* @param[in] params information about the new connection.
404+
* Callback invoked by the secure DB when an identity entry has been
405+
* retrieved.
406+
* @param entry Handle of the entry.
407+
* @param identity The identity associated with the entry; may be NULL.
406408
*/
407-
void connection_callback(
408-
const Gap::ConnectionCallbackParams_t* params
409+
void on_security_entry_retrieved(
410+
pal::SecurityDb::entry_handle_t entry,
411+
const pal::SecurityEntryIdentity_t* identity
409412
);
410413

411414
/**
412-
* Inform the security manager that a connection ended.
415+
* Callback invoked by the secure DB when the identity list has been
416+
* retrieved.
417+
*
418+
* @param identity View to the array passed to the secure DB. It contains
419+
* identity entries retrieved.
413420
*
414-
* @param[in] params handle and reason of the disconnection.
421+
* @param count Number of identities entries retrieved.
415422
*/
416-
void disconnection_callback(
417-
const Gap::DisconnectionCallbackParams_t* params
423+
void on_identity_list_retrieved(
424+
ble::ArrayView<pal::SecurityEntryIdentity_t*>& identity_list,
425+
size_t count
418426
);
419427

420428
private:

features/FEATURE_BLE/ble/pal/MemorySecurityDb.h

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#ifndef PAL_MEMORY_SECURITY_DB_H_
1818
#define PAL_MEMORY_SECURITY_DB_H_
1919

20-
#include "SecurityDB.h"
20+
#include "ble/Gap.h"
21+
#include "SecurityDb.h"
2122

2223
namespace ble {
2324
namespace pal {
@@ -49,8 +50,9 @@ class MemorySecurityDb : public SecurityDb {
4950
}
5051

5152
public:
52-
MemorySecurityDb() { };
53-
virtual ~MemorySecurityDb() { };
53+
MemorySecurityDb() : _local_sign_counter(0) { }
54+
55+
virtual ~MemorySecurityDb() { }
5456

5557
virtual const SecurityDistributionFlags_t* get_distribution_flags(
5658
entry_handle_t entry_handle
@@ -173,6 +175,35 @@ class MemorySecurityDb : public SecurityDb {
173175
cb(entry_handle, key);
174176
}
175177

178+
virtual void get_entry_identity(
179+
SecurityEntryIdentityDbCb_t cb,
180+
entry_handle_t entry_handle
181+
) {
182+
entry_t *entry = as_entry(entry_handle);
183+
if (entry && entry->flags.irk_stored) {
184+
cb(entry_handle, &entry->peer_identity);
185+
} else {
186+
cb(entry_handle, NULL);
187+
}
188+
}
189+
190+
virtual void get_identity_list(
191+
IdentitylistDbCb_t cb,
192+
ArrayView<SecurityEntryIdentity_t*>& entries
193+
) {
194+
size_t count = 0;
195+
for (size_t i = 0; i < MAX_ENTRIES && count < entries.size(); ++i) {
196+
entry_t& e = _entries[i];
197+
198+
if (e.state == ENTRY_WRITTEN && e.flags.irk_stored) {
199+
entries[count] = &e.peer_identity;
200+
++count;
201+
}
202+
}
203+
204+
cb(entries, count);
205+
}
206+
176207
/* set */
177208

178209
virtual void set_entry_peer_ltk(
@@ -207,6 +238,7 @@ class MemorySecurityDb : public SecurityDb {
207238
if (entry) {
208239
entry->state = ENTRY_WRITTEN;
209240
entry->peer_identity.irk = irk;
241+
entry->flags.irk_stored = true;
210242
}
211243
}
212244

@@ -219,6 +251,7 @@ class MemorySecurityDb : public SecurityDb {
219251
if (entry) {
220252
entry->state = ENTRY_WRITTEN;
221253
entry->peer_identity.identity_address = peer_address;
254+
entry->peer_identity.identity_address_is_public = address_is_public;
222255
}
223256
}
224257

@@ -271,23 +304,61 @@ class MemorySecurityDb : public SecurityDb {
271304
const address_t &peer_address
272305
) {
273306
const bool peer_address_public =
274-
(peer_address_type == BLEProtocol::AddressType::PUBLIC);
307+
(peer_address_type == BLEProtocol::AddressType::PUBLIC) ||
308+
(peer_address_type == BLEProtocol::AddressType::PUBLIC_IDENTITY);
275309

276310
for (size_t i = 0; i < MAX_ENTRIES; i++) {
277-
if (_entries[i].state == ENTRY_FREE) {
311+
entry_t& e = _entries[i];
312+
313+
if (e.state == ENTRY_FREE) {
278314
continue;
279-
} else if (peer_address == _entries[i].peer_identity.identity_address
280-
&& _entries[i].flags.peer_address_is_public == peer_address_public) {
281-
return &_entries[i];
315+
} else {
316+
if (peer_address_type == BLEProtocol::AddressType::PUBLIC_IDENTITY &&
317+
e.flags.irk_stored == false
318+
) {
319+
continue;
320+
}
321+
322+
// lookup for the identity address then the connection address.
323+
if (e.flags.irk_stored &&
324+
e.peer_identity.identity_address == peer_address &&
325+
e.peer_identity.identity_address_is_public == peer_address_public
326+
) {
327+
return &e;
328+
// lookup for connection address used during bonding
329+
} else if (e.flags.peer_address == peer_address &&
330+
e.flags.peer_address_is_public == peer_address_public
331+
) {
332+
return &e;
333+
}
334+
}
335+
}
336+
337+
// determine if the address in input is private or not.
338+
bool is_private_address = false;
339+
if (peer_address_type == BLEProtocol::AddressType::RANDOM) {
340+
::Gap::RandomAddressType_t random_type(::Gap::RandomAddressType_t::STATIC);
341+
ble_error_t err = ::Gap::getRandomAddressType(peer_address.data(), &random_type);
342+
if (err) {
343+
return NULL;
344+
}
345+
if (random_type != ::Gap::RandomAddressType_t::STATIC) {
346+
is_private_address = true;
282347
}
283348
}
284349

285350
/* if we din't find one grab the first disconnected slot*/
286351
for (size_t i = 0; i < MAX_ENTRIES; i++) {
287352
if (_entries[i].state == ENTRY_FREE) {
288353
_entries[i] = entry_t();
289-
_entries[i].flags.peer_address = peer_address;
290-
_entries[i].flags.peer_address_is_public = peer_address_public;
354+
// do not store private addresses in the flags; just store public
355+
// or random static address so it can be reused latter.
356+
if (is_private_address == false) {
357+
_entries[i].flags.peer_address = peer_address;
358+
_entries[i].flags.peer_address_is_public = peer_address_public;
359+
} else {
360+
_entries[i].flags.peer_address = address_t();
361+
}
291362
_entries[i].state = ENTRY_RESERVED;
292363
return &_entries[i];
293364
}

features/FEATURE_BLE/ble/pal/SecurityDb.h

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ struct SecurityDistributionFlags_t {
3535
peer_address(),
3636
encryption_key_size(0),
3737
peer_address_is_public(false),
38-
local_address_is_public(false),
3938
csrk_stored(false),
4039
csrk_mitm_protected(false),
4140
ltk_stored(false),
4241
ltk_mitm_protected(false),
43-
secure_connections_paired(false) {
42+
secure_connections_paired(false),
43+
irk_stored(false) {
4444
}
4545

4646
/** peer address */
@@ -50,8 +50,6 @@ struct SecurityDistributionFlags_t {
5050
uint8_t encryption_key_size;
5151
/** true if peer address is public, false if it's static random */
5252
uint8_t peer_address_is_public:1;
53-
/** true if local address is public, false if it's static random */
54-
uint8_t local_address_is_public:1;
5553

5654
/** CSRK (Connection Signature Resolving Key) has been distributed and stored */
5755
uint8_t csrk_stored:1;
@@ -63,6 +61,8 @@ struct SecurityDistributionFlags_t {
6361
uint8_t ltk_mitm_protected:1;
6462
/** the current pairing was done using Secure Connections */
6563
uint8_t secure_connections_paired:1;
64+
/** the security entry has been distributed and stored */
65+
uint8_t irk_stored:1;
6666
};
6767

6868
/** Long Term Key and data used to identify it */
@@ -81,6 +81,8 @@ struct SecurityEntryIdentity_t {
8181
address_t identity_address;
8282
/** Identity Resolving Key */
8383
irk_t irk;
84+
/** true if peer identity address is public, false if it's static random */
85+
uint8_t identity_address_is_public:1;
8486
};
8587

8688
/**
@@ -103,6 +105,10 @@ class SecurityDb {
103105
SecurityEntryKeysDbCb_t;
104106
typedef mbed::Callback<void(entry_handle_t, const csrk_t*, uint32_t sign_counter)>
105107
SecurityEntryCsrkDbCb_t;
108+
typedef mbed::Callback<void(entry_handle_t, const SecurityEntryIdentity_t*)>
109+
SecurityEntryIdentityDbCb_t;
110+
typedef mbed::Callback<void(ArrayView<SecurityEntryIdentity_t*>&, size_t count)>
111+
IdentitylistDbCb_t;
106112
typedef mbed::Callback<void(::Gap::Whitelist_t*)>
107113
WhitelistDbCb_t;
108114

@@ -259,6 +265,31 @@ class SecurityDb {
259265
const address_t &peer_address
260266
) = 0;
261267

268+
/**
269+
* Retrieve stored identity address and IRK.
270+
*
271+
* @param[in] cb callback that will receive the SecurityEntryIdentity_t struct
272+
* @param[in] db_entry handle of the entry being queried.
273+
*/
274+
virtual void get_entry_identity(
275+
SecurityEntryIdentityDbCb_t cb,
276+
entry_handle_t db_entry
277+
) = 0;
278+
279+
/**
280+
* Asynchronously return the identity list stored in NVM through a callback.
281+
* Function takes ownership of the memory. The identity list and the
282+
* ownership will be returned in the callback.
283+
*
284+
* @param[in] cb callback that will receive the whitelist
285+
* @param[in] identity_list preallocated identity_list that will be filled
286+
* in.
287+
*/
288+
virtual void get_identity_list(
289+
IdentitylistDbCb_t cb,
290+
ArrayView<SecurityEntryIdentity_t*>& identity_list
291+
) = 0;
292+
262293
/**
263294
* Update peer signing key.
264295
*

features/FEATURE_BLE/source/Gap.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "ble/Gap.h"
1818

1919
ble_error_t Gap::getRandomAddressType(
20-
BLEProtocol::AddressBytes_t address,
20+
const BLEProtocol::AddressBytes_t address,
2121
RandomAddressType_t* type
2222
) {
2323
// see section Device address in Bluetooth Link Layer specification
@@ -36,3 +36,13 @@ ble_error_t Gap::getRandomAddressType(
3636
return BLE_ERROR_INVALID_PARAM;
3737
}
3838
}
39+
40+
const Gap::PeripheralPrivacyConfiguration_t Gap::default_peripheral_privacy_configuration = {
41+
/* use_non_resolvable_random_address */ false,
42+
/* resolution_strategy */ PeripheralPrivacyConfiguration_t::PERFORM_PAIRING_PROCEDURE
43+
};
44+
45+
const Gap::CentralPrivacyConfiguration_t Gap::default_central_privacy_configuration = {
46+
/* use_non_resolvable_random_address */ false,
47+
/* resolution_strategy */ CentralPrivacyConfiguration_t::DO_NOT_RESOLVE
48+
};

0 commit comments

Comments
 (0)