Skip to content

Commit 2b48d48

Browse files
committed
BLE: SecurityManager PIMPL
1 parent e3cf59e commit 2b48d48

File tree

4 files changed

+333
-60
lines changed

4 files changed

+333
-60
lines changed

connectivity/FEATURE_BLE/include/ble/SecurityManager.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,11 @@ namespace ble {
187187
*
188188
*/
189189
#if !defined(DOXYGEN_ONLY)
190-
namespace interface {
190+
namespace impl {
191+
class SecurityManager;
192+
}
191193
#endif // !defined(DOXYGEN_ONLY)
194+
192195
class SecurityManager
193196
{
194197
public:
@@ -895,16 +898,13 @@ class SecurityManager
895898
* @deprecated use the enum in ble namespace */
896899
typedef ble::Keypress_t Keypress_t;
897900
#endif // !defined(DOXYGEN_ONLY)
901+
902+
private:
903+
impl::SecurityManager *impl;
898904
};
899905

900-
#if !defined(DOXYGEN_ONLY)
901-
} // namespace interface
902-
#endif // !defined(DOXYGEN_ONLY)
903-
} // ble
904906

905-
/* This includes the concrete class implementation, to provide a an alternative API implementation
906-
* disable ble-api-implementation and place your header in a path with the same structure */
907-
#include "ble/internal/SecurityManagerImpl.h"
907+
} // ble
908908

909909
/** @deprecated Use the namespaced ble::SecurityManager instead of the global SecurityManager. */
910910
using ble::SecurityManager;

connectivity/FEATURE_BLE/libraries/ble-api-implementation/include/ble/internal/SecurityManagerImpl.h

Lines changed: 72 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,41 @@
3636
#include "ble/SecurityManager.h"
3737

3838
namespace ble {
39+
namespace impl {
3940

40-
class BLEInstanceBase;
41+
//class BLEInstanceBase;
4142

4243
class SecurityManager :
43-
public ble::interface::SecurityManager,
4444
public ble::PalSecurityManagerEventHandler,
4545
public ble::PalConnectionMonitorEventHandler,
46-
public ble::PalSigningMonitorEventHandler
47-
{
46+
public ble::PalSigningMonitorEventHandler {
4847
friend class ble::PalConnectionMonitorEventHandler;
49-
friend BLEInstanceBase;
48+
49+
friend ::ble::BLEInstanceBase;
5050
friend PalGenericAccessService;
5151
friend PalSecurityManager;
5252

53+
using SecurityIOCapabilities_t = ble::SecurityManager::SecurityIOCapabilities_t;
54+
using SecurityMode_t = ble::SecurityManager::SecurityMode_t;
55+
using SecurityManagerShutdownCallback_t = ble::SecurityManager::SecurityManagerShutdownCallback_t;
56+
using SecurityManagerShutdownCallbackChain_t = ble::SecurityManager::SecurityManagerShutdownCallbackChain_t;
57+
using EventHandler = ble::SecurityManager::EventHandler;
58+
using Passkey_t = ble::SecurityManager::Passkey_t ;
59+
60+
static auto constexpr IO_CAPS_NONE = ble::SecurityManager::IO_CAPS_NONE;
61+
5362
public:
5463
////////////////////////////////////////////////////////////////////////////
5564
// SM lifecycle management
5665
//
5766

5867
ble_error_t init(
59-
bool enableBonding = true,
60-
bool requireMITM = true,
61-
SecurityIOCapabilities_t iocaps = IO_CAPS_NONE,
62-
const Passkey_t passkey = NULL,
63-
bool signing = true,
64-
const char *dbFilepath = NULL
68+
bool enableBonding = true,
69+
bool requireMITM = true,
70+
SecurityIOCapabilities_t iocaps = IO_CAPS_NONE,
71+
const Passkey_t passkey = NULL,
72+
bool signing = true,
73+
const char *dbFilepath = NULL
6574
);
6675

6776
ble_error_t setDatabaseFilepath(const char *dbFilepath = NULL);
@@ -115,6 +124,7 @@ class SecurityManager :
115124
#if BLE_FEATURE_SIGNING
116125

117126
ble_error_t enableSigning(ble::connection_handle_t connectionHandle, bool enabled = true);
127+
118128
#endif // BLE_FEATURE_SIGNING
119129

120130
ble_error_t setHintFutureRoleReversal(bool enable = true);
@@ -156,7 +166,11 @@ class SecurityManager :
156166

157167
ble_error_t legacyPairingOobReceived(const ble::address_t *address, const ble::oob_tk_t *tk);
158168

159-
ble_error_t oobReceived(const ble::address_t *address, const ble::oob_lesc_value_t *random, const ble::oob_confirm_t *confirm);
169+
ble_error_t oobReceived(
170+
const ble::address_t *address,
171+
const ble::oob_lesc_value_t *random,
172+
const ble::oob_confirm_t *confirm
173+
);
160174

161175
////////////////////////////////////////////////////////////////////////////
162176
// Keys
@@ -175,14 +189,14 @@ class SecurityManager :
175189
/* Event callback handlers. */
176190
public:
177191

178-
void onShutdown(const SecurityManagerShutdownCallback_t& callback);
192+
void onShutdown(const SecurityManagerShutdownCallback_t &callback);
179193

180-
template <typename T>
194+
template<typename T>
181195
void onShutdown(T *objPtr, void (T::*memberPtr)(const SecurityManager *));
182196

183-
SecurityManagerShutdownCallbackChain_t& onShutdown();
197+
SecurityManagerShutdownCallbackChain_t &onShutdown();
184198

185-
void setSecurityManagerEventHandler(EventHandler* handler);
199+
void setSecurityManagerEventHandler(EventHandler *handler);
186200

187201
/* ===================================================================== */
188202
/* private implementation follows */
@@ -433,7 +447,8 @@ class SecurityManager :
433447
private:
434448
/* Disallow copy and assignment. */
435449
SecurityManager(const SecurityManager &);
436-
SecurityManager& operator=(const SecurityManager &);
450+
451+
SecurityManager &operator=(const SecurityManager &);
437452

438453
SecurityManager(
439454
PalSecurityManager &palImpl,
@@ -493,12 +508,12 @@ class SecurityManager :
493508

494509
void enable_encryption_cb(
495510
ble::SecurityDb::entry_handle_t entry,
496-
const SecurityEntryKeys_t* entryKeys
511+
const SecurityEntryKeys_t *entryKeys
497512
);
498513

499514
void set_ltk_cb(
500515
SecurityDb::entry_handle_t entry,
501-
const SecurityEntryKeys_t* entryKeys
516+
const SecurityEntryKeys_t *entryKeys
502517
);
503518

504519
void return_csrk_cb(
@@ -536,28 +551,35 @@ class SecurityManager :
536551

537552
void on_security_entry_retrieved(
538553
SecurityDb::entry_handle_t entry,
539-
const SecurityEntryIdentity_t* identity
554+
const SecurityEntryIdentity_t *identity
540555
);
541556

542557
void on_identity_list_retrieved(
543-
Span<SecurityEntryIdentity_t>& identity_list,
558+
Span<SecurityEntryIdentity_t> &identity_list,
544559
size_t count
545560
);
546561

547562
private:
548563
struct ControlBlock_t {
549564
ControlBlock_t();
550565

551-
KeyDistribution get_initiator_key_distribution() {
566+
KeyDistribution get_initiator_key_distribution()
567+
{
552568
return KeyDistribution(initiator_key_distribution);
553569
};
554-
KeyDistribution get_responder_key_distribution() {
570+
571+
KeyDistribution get_responder_key_distribution()
572+
{
555573
return KeyDistribution(responder_key_distribution);
556574
};
557-
void set_initiator_key_distribution(KeyDistribution mask) {
575+
576+
void set_initiator_key_distribution(KeyDistribution mask)
577+
{
558578
initiator_key_distribution = mask.value();
559579
};
560-
void set_responder_key_distribution(KeyDistribution mask) {
580+
581+
void set_responder_key_distribution(KeyDistribution mask)
582+
{
561583
responder_key_distribution = mask.value();
562584
};
563585

@@ -567,46 +589,46 @@ class SecurityManager :
567589
address_t local_address; /**< address used for connection, possibly different from identity */
568590

569591
private:
570-
uint8_t initiator_key_distribution:4;
571-
uint8_t responder_key_distribution:4;
592+
uint8_t initiator_key_distribution: 4;
593+
uint8_t responder_key_distribution: 4;
572594
public:
573-
uint8_t connected:1;
574-
uint8_t authenticated:1; /**< have we turned encryption on during this connection */
575-
uint8_t is_master:1;
595+
uint8_t connected: 1;
596+
uint8_t authenticated: 1; /**< have we turned encryption on during this connection */
597+
uint8_t is_master: 1;
576598

577-
uint8_t encryption_requested:1;
578-
uint8_t encryption_failed:1;
579-
uint8_t encrypted:1;
580-
uint8_t signing_requested:1;
581-
uint8_t signing_override_default:1;
599+
uint8_t encryption_requested: 1;
600+
uint8_t encryption_failed: 1;
601+
uint8_t encrypted: 1;
602+
uint8_t signing_requested: 1;
603+
uint8_t signing_override_default: 1;
582604

583-
uint8_t mitm_requested:1;
584-
uint8_t mitm_performed:1; /**< keys exchange will have MITM protection */
605+
uint8_t mitm_requested: 1;
606+
uint8_t mitm_performed: 1; /**< keys exchange will have MITM protection */
585607

586-
uint8_t attempt_oob:1;
587-
uint8_t oob_mitm_protection:1;
588-
uint8_t oob_present:1;
589-
uint8_t legacy_pairing_oob_request_pending:1;
608+
uint8_t attempt_oob: 1;
609+
uint8_t oob_mitm_protection: 1;
610+
uint8_t oob_present: 1;
611+
uint8_t legacy_pairing_oob_request_pending: 1;
590612

591-
uint8_t csrk_failures:2;
613+
uint8_t csrk_failures: 2;
592614
};
593615

594616
/* list management */
595617

596-
ControlBlock_t* acquire_control_block(connection_handle_t connection);
618+
ControlBlock_t *acquire_control_block(connection_handle_t connection);
597619

598-
ControlBlock_t* get_control_block(connection_handle_t connection);
620+
ControlBlock_t *get_control_block(connection_handle_t connection);
599621

600-
ControlBlock_t* get_control_block(const address_t &peer_address);
622+
ControlBlock_t *get_control_block(const address_t &peer_address);
601623

602-
ControlBlock_t* get_control_block(SecurityDb::entry_handle_t db_entry);
624+
ControlBlock_t *get_control_block(SecurityDb::entry_handle_t db_entry);
603625

604-
void release_control_block(ControlBlock_t* entry);
626+
void release_control_block(ControlBlock_t *entry);
605627

606628
private:
607629
SecurityManagerShutdownCallbackChain_t shutdownCallChain;
608-
EventHandler* eventHandler;
609-
EventHandler defaultEventHandler;
630+
EventHandler *eventHandler;
631+
EventHandler defaultEventHandler;
610632

611633
PalSecurityManager &_pal;
612634
PalConnectionMonitor &_connection_monitor;
@@ -634,6 +656,7 @@ class SecurityManager :
634656
ControlBlock_t _control_blocks[MAX_CONTROL_BLOCKS];
635657
};
636658

659+
} // namespace impl
637660
} // ble
638661

639662
#endif /*IMPL_SECURITY_MANAGER_H_*/

connectivity/FEATURE_BLE/libraries/ble-api-implementation/source/SecurityManagerImpl.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "BLERoles.h"
2020

21-
#include "ble/SecurityManager.h"
21+
#include "ble/internal/SecurityManagerImpl.h"
2222
#include "ble/internal/PalSecurityManager.h"
2323
#include "ble/internal/MemorySecurityDb.h"
2424
#include "ble/internal/FileSecurityDb.h"
@@ -30,6 +30,33 @@ using ble::KeyDistribution;
3030
using ble::connection_peer_address_type_t;
3131

3232
namespace ble {
33+
namespace impl {
34+
35+
namespace {
36+
static constexpr auto SECURITY_MODE_ENCRYPTION_OPEN_LINK =
37+
ble::SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK;
38+
39+
static constexpr auto SECURITY_MODE_ENCRYPTION_NO_MITM =
40+
ble::SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
41+
42+
static constexpr auto SECURITY_MODE_ENCRYPTION_WITH_MITM =
43+
ble::SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM;
44+
45+
static constexpr auto SECURITY_MODE_SIGNED_NO_MITM =
46+
ble::SecurityManager::SECURITY_MODE_SIGNED_NO_MITM;
47+
48+
static constexpr auto SECURITY_MODE_SIGNED_WITH_MITM =
49+
ble::SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM;
50+
51+
using SecurityCompletionStatus_t = ble::SecurityManager::SecurityCompletionStatus_t;
52+
53+
static constexpr auto SEC_STATUS_TIMEOUT =
54+
ble::SecurityManager::SEC_STATUS_TIMEOUT;
55+
56+
static constexpr auto SEC_STATUS_SUCCESS =
57+
ble::SecurityManager::SEC_STATUS_SUCCESS;
58+
59+
}
3360

3461
/* Implements PalSecurityManager */
3562

@@ -95,7 +122,9 @@ ble_error_t SecurityManager::init(
95122
_connection_monitor.set_connection_event_handler(this);
96123
#endif
97124
#if BLE_FEATURE_SIGNING
98-
_signing_monitor.set_signing_event_handler(this);
125+
// TODO: FIXME
126+
// _signing_monitor.set_signing_event_handler(this);
127+
_signing_monitor.set_signing_event_handler(nullptr);
99128
#endif
100129
_pal.set_event_handler(this);
101130

@@ -152,7 +181,9 @@ ble_error_t SecurityManager::reset(void)
152181
_pal.reset();
153182

154183
/* Notify that the instance is about to shutdown */
155-
shutdownCallChain.call(this);
184+
// TODO: FIXME
185+
// shutdownCallChain.call(this);
186+
shutdownCallChain.call(nullptr);
156187
shutdownCallChain.clear();
157188
eventHandler = &defaultEventHandler;
158189

@@ -2026,4 +2057,5 @@ void SecurityManager::setSecurityManagerEventHandler(EventHandler* handler)
20262057
}
20272058
}
20282059

2060+
} /* namespace impl */
20292061
} /* namespace ble */

0 commit comments

Comments
 (0)