Skip to content

Commit 9da64e5

Browse files
refactor into separate functions for readability and correctness of pal matching db
1 parent 439d002 commit 9da64e5

File tree

3 files changed

+82
-32
lines changed

3 files changed

+82
-32
lines changed

features/FEATURE_BLE/ble/SecurityManager.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,17 @@ class SecurityManager {
464464
}
465465

466466
/**
467-
* Change the file used for the security datagse. If path is invalid or a NULL is passed
467+
* Change the file used for the security database. If path is invalid or a NULL is passed
468468
* keys will only be stored in memory.
469469
*
470+
* @note This operation is only allowed with no active connections.
471+
*
470472
* @param[in] dbPath Path to the file used to store keys in the filesystem,
471473
* if NULL keys will be only stored in memory
472474
*
473475
* @return BLE_ERROR_NONE on success.
474476
*/
475-
virtual ble_error_t setDatabaseFile(const char *dbFilepath = NULL) {
477+
virtual ble_error_t setDatabaseFilepath(const char *dbFilepath = NULL) {
476478
(void)dbFilepath;
477479
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */
478480
}

features/FEATURE_BLE/ble/generic/GenericSecurityManager.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class GenericSecurityManager : public SecurityManager,
5353
const char* db_path = NULL
5454
);
5555

56-
virtual ble_error_t setDatabaseFile(const char *db_path = NULL);
56+
virtual ble_error_t setDatabaseFilepath(const char *db_path = NULL);
5757

5858
virtual ble_error_t reset();
5959

@@ -265,6 +265,22 @@ class GenericSecurityManager : public SecurityManager,
265265
//
266266

267267
private:
268+
269+
/**
270+
* Initialise the database, if database already exists it will close it and open the new one.
271+
*
272+
* @param db_path path to file to store secure db
273+
* @return BLE_ERROR_NONE or appropriate error code indicating the failure reason.
274+
*/
275+
ble_error_t init_database(const char *db_path = NULL);
276+
277+
/**
278+
* Generate identity list based on the database of IRK and apply it to the resolving list.
279+
*
280+
* @return BLE_ERROR_NONE or appropriate error code indicating the failure reason.
281+
*/
282+
ble_error_t init_resolving_list();
283+
268284
/**
269285
* Generate the CSRK if needed.
270286
*

features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ble_error_t GenericSecurityManager::init(
4848
return result;
4949
}
5050

51-
result = setDatabaseFile(db_path);
51+
result = init_database(db_path);
5252
if (result != BLE_ERROR_NONE) {
5353
return result;
5454
}
@@ -79,49 +79,38 @@ ble_error_t GenericSecurityManager::init(
7979
init_signing();
8080
}
8181

82+
init_resolving_list();
83+
8284
_connection_monitor.set_connection_event_handler(this);
8385
_signing_monitor.set_signing_event_handler(this);
8486
_pal.set_event_handler(this);
8587

86-
uint8_t resolving_list_capacity = _pal.read_resolving_list_capacity();
87-
SecurityEntryIdentity_t* identity_list_p =
88-
new (std::nothrow) SecurityEntryIdentity_t[resolving_list_capacity];
89-
90-
if (identity_list_p) {
91-
ArrayView<SecurityEntryIdentity_t> identity_list(
92-
identity_list_p,
93-
resolving_list_capacity
94-
);
95-
96-
_db->get_identity_list(
97-
mbed::callback(this, &GenericSecurityManager::on_identity_list_retrieved),
98-
identity_list
99-
);
100-
}
101-
10288
return BLE_ERROR_NONE;
10389
}
10490

105-
ble_error_t GenericSecurityManager::setDatabaseFile(
91+
ble_error_t GenericSecurityManager::setDatabaseFilepath(
10692
const char *db_path
10793
) {
108-
if (_db) {
109-
delete _db;
110-
}
94+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
11195

112-
FILE* db_file = FileSecurityDb::open_db_file(db_path);
96+
/* operation only allowed with no connections active */
97+
for (size_t i = 0; i < MAX_CONTROL_BLOCKS; i++) {
98+
if (_control_blocks[i].connected) {
99+
return BLE_ERROR_OPERATION_NOT_PERMITTED;
100+
}
101+
}
113102

114-
if (db_file) {
115-
_db = new (std::nothrow) FileSecurityDb(db_file);
116-
} else {
117-
_db = new (std::nothrow) MemorySecurityDb();
103+
ble_error_t result = init_database(db_path);
104+
if (result != BLE_ERROR_NONE) {
105+
return result;
118106
}
119107

120-
if (!_db) {
121-
return BLE_ERROR_NO_MEM;
108+
result = init_database(db_path);
109+
if (result != BLE_ERROR_NONE) {
110+
return result;
122111
}
123112

124-
_db->restore();
113+
init_resolving_list();
125114

126115
return BLE_ERROR_NONE;
127116
}
@@ -779,6 +768,49 @@ ble_error_t GenericSecurityManager::oobReceived(
779768
// Helper functions
780769
//
781770

771+
ble_error_t GenericSecurityManager::init_database(
772+
const char *db_path
773+
) {
774+
if (_db) {
775+
delete _db;
776+
}
777+
778+
FILE* db_file = FileSecurityDb::open_db_file(db_path);
779+
780+
if (db_file) {
781+
_db = new (std::nothrow) FileSecurityDb(db_file);
782+
} else {
783+
_db = new (std::nothrow) MemorySecurityDb();
784+
}
785+
786+
if (!_db) {
787+
return BLE_ERROR_NO_MEM;
788+
}
789+
790+
_db->restore();
791+
792+
return BLE_ERROR_NONE;
793+
}
794+
795+
ble_error_t GenericSecurityManager::init_resolving_list() {
796+
/* match the resolving list to the currently stored set of IRKs */
797+
uint8_t resolving_list_capacity = _pal.read_resolving_list_capacity();
798+
SecurityEntryIdentity_t* identity_list_p =
799+
new (std::nothrow) SecurityEntryIdentity_t[resolving_list_capacity];
800+
801+
if (identity_list_p) {
802+
ArrayView<SecurityEntryIdentity_t> identity_list(
803+
identity_list_p,
804+
resolving_list_capacity
805+
);
806+
807+
_db->get_identity_list(
808+
mbed::callback(this, &GenericSecurityManager::on_identity_list_retrieved),
809+
identity_list
810+
);
811+
}
812+
}
813+
782814
ble_error_t GenericSecurityManager::init_signing() {
783815
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
784816
const csrk_t *pcsrk = _db->get_local_csrk();

0 commit comments

Comments
 (0)