Skip to content

Commit b43f27a

Browse files
authored
Merge pull request ARMmbed#43 from paul-szczepanek-arm/filedb
Filesystem security db
2 parents 687ecc1 + 1ae13bc commit b43f27a

File tree

15 files changed

+1809
-1064
lines changed

15 files changed

+1809
-1064
lines changed

features/FEATURE_BLE/ble/SecurityManager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,24 @@ class SecurityManager {
417417
* support out-of-band exchanges of security data.
418418
* @param[in] passkey To specify a static passkey.
419419
* @param[in] signing Generate and distribute signing key during pairing
420+
* @param[in] dbPath Path to the folder used to store keys in the filesystem,
421+
* if NULL keys will be only stored in memory
422+
*
420423
*
421424
* @return BLE_ERROR_NONE on success.
422425
*/
423426
virtual ble_error_t init(bool enableBonding = true,
424427
bool requireMITM = true,
425428
SecurityIOCapabilities_t iocaps = IO_CAPS_NONE,
426429
const Passkey_t passkey = NULL,
427-
bool signing = true) {
430+
bool signing = true,
431+
const char *dbPath = NULL) {
428432
/* Avoid compiler warnings about unused variables. */
429433
(void)enableBonding;
430434
(void)requireMITM;
431435
(void)iocaps;
432436
(void)passkey;
437+
(void)dbPath;
433438

434439
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */
435440
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef GENERIC_FILE_SECURITY_DB_H_
18+
#define GENERIC_FILE_SECURITY_DB_H_
19+
20+
#include "SecurityDb.h"
21+
22+
#include <stdio.h>
23+
24+
namespace ble {
25+
namespace generic {
26+
27+
/** Filesystem implementation */
28+
class FileSecurityDb : public SecurityDb {
29+
private:
30+
31+
struct entry_t {
32+
SecurityDistributionFlags_t flags;
33+
sign_count_t peer_sign_counter;
34+
size_t file_offset;
35+
};
36+
37+
static const size_t MAX_ENTRIES = 5;
38+
39+
static entry_t* as_entry(entry_handle_t db_handle) {
40+
return reinterpret_cast<entry_t*>(db_handle);
41+
}
42+
43+
template<class T>
44+
void db_read(T *value, long int offset) {
45+
fseek(_db_file, offset, SEEK_SET);
46+
fread(value, sizeof(T), 1, _db_file);
47+
}
48+
49+
template<class T>
50+
void db_write(T *value, long int offset) {
51+
fseek(_db_file, offset, SEEK_SET);
52+
fwrite(value, sizeof(T), 1, _db_file);
53+
}
54+
55+
public:
56+
FileSecurityDb(FILE *db_file);
57+
virtual ~FileSecurityDb();
58+
59+
/**
60+
* Validates or creates a file for the security database.
61+
* @param db_path path to the file
62+
* @return FILE handle open and ready for use by the database or NULL if unavailable
63+
*/
64+
static FILE* open_db_file(const char *db_path);
65+
66+
virtual SecurityDistributionFlags_t* get_distribution_flags(
67+
entry_handle_t db_handle
68+
);
69+
70+
71+
/* local keys */
72+
73+
/* set */
74+
virtual void set_entry_local_ltk(
75+
entry_handle_t db_handle,
76+
const ltk_t &ltk
77+
);
78+
79+
virtual void set_entry_local_ediv_rand(
80+
entry_handle_t db_handle,
81+
const ediv_t &ediv,
82+
const rand_t &rand
83+
);
84+
85+
/* peer's keys */
86+
87+
/* set */
88+
89+
virtual void set_entry_peer_ltk(
90+
entry_handle_t db_handle,
91+
const ltk_t &ltk
92+
);
93+
94+
virtual void set_entry_peer_ediv_rand(
95+
entry_handle_t db_handle,
96+
const ediv_t &ediv,
97+
const rand_t &rand
98+
);
99+
100+
virtual void set_entry_peer_irk(
101+
entry_handle_t db_handle,
102+
const irk_t &irk
103+
);
104+
105+
virtual void set_entry_peer_bdaddr(
106+
entry_handle_t db_handle,
107+
bool address_is_public,
108+
const address_t &peer_address
109+
);
110+
111+
virtual void set_entry_peer_csrk(
112+
entry_handle_t db_handle,
113+
const csrk_t &csrk
114+
);
115+
116+
virtual void set_entry_peer_sign_counter(
117+
entry_handle_t db_handle,
118+
sign_count_t sign_counter
119+
);
120+
121+
/* saving and loading from nvm */
122+
123+
virtual void restore();
124+
125+
virtual void sync(entry_handle_t db_handle);
126+
127+
virtual void set_restore(bool reload);
128+
129+
private:
130+
virtual uint8_t get_entry_count();
131+
132+
virtual SecurityDistributionFlags_t* get_entry_handle_by_index(uint8_t index);
133+
134+
virtual void reset_entry(entry_handle_t db_handle);
135+
136+
virtual SecurityEntryIdentity_t* read_in_entry_peer_identity(entry_handle_t db_handle);
137+
virtual SecurityEntryKeys_t* read_in_entry_peer_keys(entry_handle_t db_handle);
138+
virtual SecurityEntryKeys_t* read_in_entry_local_keys(entry_handle_t db_handle);
139+
virtual SecurityEntrySigning_t* read_in_entry_peer_signing(entry_handle_t db_handle);
140+
141+
/**
142+
* Zero the db file.
143+
* @param db_file filehandle for file to erase
144+
* @return filehandle when successful, otherwise NULL
145+
*/
146+
static FILE* erase_db_file(FILE* db_file);
147+
148+
private:
149+
entry_t _entries[MAX_ENTRIES];
150+
FILE *_db_file;
151+
uint8_t _buffer[sizeof(SecurityEntryKeys_t)];
152+
};
153+
154+
} /* namespace pal */
155+
} /* namespace ble */
156+
157+
#endif /*GENERIC_FILE_SECURITY_DB_H_*/

features/FEATURE_BLE/ble/generic/GenericSecurityManager.h

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "ble/pal/GapTypes.h"
2121
#include "ble/BLETypes.h"
22-
#include "ble/pal/SecurityDb.h"
22+
#include "ble/generic/SecurityDb.h"
2323
#include "platform/Callback.h"
2424
#include "ble/pal/ConnectionEventMonitor.h"
2525
#include "ble/pal/SigningEventMonitor.h"
@@ -37,8 +37,6 @@ class GenericSecurityManager : public SecurityManager,
3737
public pal::ConnectionEventMonitor::EventHandler,
3838
public pal::SigningEventMonitor::EventHandler {
3939
public:
40-
typedef ble::pal::SecurityDistributionFlags_t SecurityDistributionFlags_t;
41-
typedef ble::pal::SecurityEntryKeys_t SecurityEntryKeys_t;
4240

4341
/* implements SecurityManager */
4442

@@ -51,7 +49,8 @@ class GenericSecurityManager : public SecurityManager,
5149
bool mitm = true,
5250
SecurityIOCapabilities_t iocaps = IO_CAPS_NONE,
5351
const Passkey_t passkey = NULL,
54-
bool signing = true
52+
bool signing = true,
53+
const char* db_path = NULL
5554
);
5655

5756
virtual ble_error_t reset();
@@ -236,13 +235,12 @@ class GenericSecurityManager : public SecurityManager,
236235
public:
237236
GenericSecurityManager(
238237
pal::SecurityManager &palImpl,
239-
pal::SecurityDb &dbImpl,
240238
pal::ConnectionEventMonitor &connMonitorImpl,
241239
pal::SigningEventMonitor &signingMonitorImpl
242240
) : _pal(palImpl),
243-
_db(dbImpl),
244241
_connection_monitor(connMonitorImpl),
245242
_signing_monitor(signingMonitorImpl),
243+
_db(NULL),
246244
_default_authentication(0),
247245
_default_key_distribution(pal::KeyDistribution::KEY_DISTRIBUTION_ALL),
248246
_pairing_authorisation_required(false),
@@ -256,6 +254,10 @@ class GenericSecurityManager : public SecurityManager,
256254
_oob_local_random[0] = 1;
257255
}
258256

257+
~GenericSecurityManager() {
258+
delete _db;
259+
}
260+
259261
////////////////////////////////////////////////////////////////////////////
260262
// Helper functions
261263
//
@@ -308,7 +310,7 @@ class GenericSecurityManager : public SecurityManager,
308310
* @param[in] entryKeys security entry containing keys.
309311
*/
310312
void enable_encryption_cb(
311-
pal::SecurityDb::entry_handle_t entry,
313+
SecurityDb::entry_handle_t entry,
312314
const SecurityEntryKeys_t* entryKeys
313315
);
314316

@@ -319,32 +321,30 @@ class GenericSecurityManager : public SecurityManager,
319321
* @param[in] entryKeys security entry containing keys.
320322
*/
321323
void set_ltk_cb(
322-
pal::SecurityDb::entry_handle_t entry,
324+
SecurityDb::entry_handle_t entry,
323325
const SecurityEntryKeys_t* entryKeys
324326
);
325327

326328
/**
327329
* Returns the CSRK for the connection. Called by the security db.
328330
*
329331
* @param[in] connectionHandle Handle to identify the connection.
330-
* @param[in] csrk connection signature resolving key.
332+
* @param[in] signing connection signature resolving key and counter.
331333
*/
332334
void return_csrk_cb(
333-
pal::SecurityDb::entry_handle_t connection,
334-
const csrk_t *csrk,
335-
sign_count_t sign_counter
335+
SecurityDb::entry_handle_t connection,
336+
const SecurityEntrySigning_t *signing
336337
);
337338

338339
/**
339340
* Set the peer CSRK for the connection. Called by the security db.
340341
*
341342
* @param[in] connectionHandle Handle to identify the connection.
342-
* @param[in] csrk connection signature resolving key.
343+
* @param[in] signing connection signature resolving key and counter.
343344
*/
344345
void set_peer_csrk_cb(
345-
pal::SecurityDb::entry_handle_t connection,
346-
const csrk_t *csrk,
347-
sign_count_t sign_counter
346+
SecurityDb::entry_handle_t connection,
347+
const SecurityEntrySigning_t *signing
348348
);
349349

350350
/**
@@ -407,8 +407,8 @@ class GenericSecurityManager : public SecurityManager,
407407
* @param identity The identity associated with the entry; may be NULL.
408408
*/
409409
void on_security_entry_retrieved(
410-
pal::SecurityDb::entry_handle_t entry,
411-
const pal::SecurityEntryIdentity_t* identity
410+
SecurityDb::entry_handle_t entry,
411+
const SecurityEntryIdentity_t* identity
412412
);
413413

414414
/**
@@ -421,12 +421,12 @@ class GenericSecurityManager : public SecurityManager,
421421
* @param count Number of identities entries retrieved.
422422
*/
423423
void on_identity_list_retrieved(
424-
ble::ArrayView<pal::SecurityEntryIdentity_t*>& identity_list,
424+
ble::ArrayView<SecurityEntryIdentity_t>& identity_list,
425425
size_t count
426426
);
427427

428428
private:
429-
struct ControlBlock_t : public pal::SecurityDistributionFlags_t {
429+
struct ControlBlock_t {
430430
ControlBlock_t();
431431

432432
pal::KeyDistribution get_initiator_key_distribution() {
@@ -443,7 +443,7 @@ class GenericSecurityManager : public SecurityManager,
443443
};
444444

445445
connection_handle_t connection;
446-
pal::SecurityDb::entry_handle_t db_entry;
446+
SecurityDb::entry_handle_t db_entry;
447447

448448
address_t local_address; /**< address used for connection, possibly different from identity */
449449

@@ -473,10 +473,11 @@ class GenericSecurityManager : public SecurityManager,
473473
};
474474

475475
pal::SecurityManager &_pal;
476-
pal::SecurityDb &_db;
477476
pal::ConnectionEventMonitor &_connection_monitor;
478477
pal::SigningEventMonitor &_signing_monitor;
479478

479+
SecurityDb *_db;
480+
480481
/* OOB data */
481482
address_t _oob_local_address;
482483
address_t _oob_peer_address;
@@ -718,7 +719,7 @@ class GenericSecurityManager : public SecurityManager,
718719

719720
ControlBlock_t* get_control_block(const address_t &peer_address);
720721

721-
ControlBlock_t* get_control_block(pal::SecurityDb::entry_handle_t db_entry);
722+
ControlBlock_t* get_control_block(SecurityDb::entry_handle_t db_entry);
722723

723724
void release_control_block(ControlBlock_t* entry);
724725
};

0 commit comments

Comments
 (0)