From 439d002f7df62e8f597ca50e9c0a52b7761a52eb Mon Sep 17 00:00:00 2001 From: paul-szczepanek-arm <33840200+paul-szczepanek-arm@users.noreply.github.com> Date: Fri, 18 May 2018 12:34:52 +0100 Subject: [PATCH] new API call to change db at runtime --- features/FEATURE_BLE/ble/SecurityManager.h | 20 ++++++-- .../ble/generic/GenericSecurityManager.h | 2 + .../source/generic/GenericSecurityManager.cpp | 49 ++++++++++++------- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/features/FEATURE_BLE/ble/SecurityManager.h b/features/FEATURE_BLE/ble/SecurityManager.h index c99ebd702a..548805c191 100644 --- a/features/FEATURE_BLE/ble/SecurityManager.h +++ b/features/FEATURE_BLE/ble/SecurityManager.h @@ -441,7 +441,7 @@ public: * support out-of-band exchanges of security data. * @param[in] passkey To specify a static passkey. * @param[in] signing Generate and distribute signing key during pairing - * @param[in] dbPath Path to the folder used to store keys in the filesystem, + * @param[in] dbPath Path to the file used to store keys in the filesystem, * if NULL keys will be only stored in memory * * @@ -452,17 +452,31 @@ public: SecurityIOCapabilities_t iocaps = IO_CAPS_NONE, const Passkey_t passkey = NULL, bool signing = true, - const char *dbPath = NULL) { + const char *dbFilepath = NULL) { /* Avoid compiler warnings about unused variables. */ (void)enableBonding; (void)requireMITM; (void)iocaps; (void)passkey; - (void)dbPath; + (void)dbFilepath; return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */ } + /** + * Change the file used for the security datagse. If path is invalid or a NULL is passed + * keys will only be stored in memory. + * + * @param[in] dbPath Path to the file used to store keys in the filesystem, + * if NULL keys will be only stored in memory + * + * @return BLE_ERROR_NONE on success. + */ + virtual ble_error_t setDatabaseFile(const char *dbFilepath = NULL) { + (void)dbFilepath; + return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */ + } + /** * Notify all registered onShutdown callbacks that the SecurityManager is * about to be shutdown and clear all SecurityManager state of the diff --git a/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h b/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h index f5f060a5e6..4dca909fc7 100644 --- a/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h +++ b/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h @@ -53,6 +53,8 @@ public: const char* db_path = NULL ); + virtual ble_error_t setDatabaseFile(const char *db_path = NULL); + virtual ble_error_t reset(); virtual ble_error_t preserveBondingStateOnReset( diff --git a/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp b/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp index 9f2f527b0e..588e69ceaa 100644 --- a/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp +++ b/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp @@ -43,29 +43,16 @@ ble_error_t GenericSecurityManager::init( const char* db_path ) { - ble_error_t err = _pal.initialize(); - if (err) { - return err; + ble_error_t result = _pal.initialize(); + if (result != BLE_ERROR_NONE) { + return result; } - if (_db) { - delete _db; + result = setDatabaseFile(db_path); + if (result != BLE_ERROR_NONE) { + return result; } - FILE* db_file = FileSecurityDb::open_db_file(db_path); - - if (db_file) { - _db = new (std::nothrow) FileSecurityDb(db_file); - } else { - _db = new (std::nothrow) MemorySecurityDb(); - } - - if (!_db) { - return BLE_ERROR_NO_MEM; - } - - _db->restore(); - _pal.set_io_capability((io_capability_t::type) iocaps); if (passkey) { @@ -115,6 +102,30 @@ ble_error_t GenericSecurityManager::init( return BLE_ERROR_NONE; } +ble_error_t GenericSecurityManager::setDatabaseFile( + const char *db_path +) { + if (_db) { + delete _db; + } + + FILE* db_file = FileSecurityDb::open_db_file(db_path); + + if (db_file) { + _db = new (std::nothrow) FileSecurityDb(db_file); + } else { + _db = new (std::nothrow) MemorySecurityDb(); + } + + if (!_db) { + return BLE_ERROR_NO_MEM; + } + + _db->restore(); + + return BLE_ERROR_NONE; +} + ble_error_t GenericSecurityManager::reset(void) { _pal.reset(); SecurityManager::reset();