Skip to content

Commit 975544f

Browse files
link key distribution decision made in generic security manager
1 parent 96de1c2 commit 975544f

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

features/FEATURE_BLE/ble/generic/GenericSecurityManager.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,28 @@ class GenericSecurityManager : public SecurityManager,
432432
struct ControlBlock_t : public pal::SecurityDistributionFlags_t {
433433
ControlBlock_t();
434434

435+
KeyDistribution get_initiator_key_distribution() {
436+
return KeyDistribution(initiator_key_distribution);
437+
};
438+
KeyDistribution get_responder_key_distribution() {
439+
return KeyDistribution(responder_key_distribution);
440+
};
441+
void set_initiator_key_distribution(KeyDistribution mask) {
442+
initiator_key_distribution = mask.value();
443+
};
444+
void set_responder_key_distribution(KeyDistribution mask) {
445+
responder_key_distribution = mask.value();
446+
};
447+
435448
connection_handle_t connection;
436449
pal::SecurityDb::entry_handle_t db_entry;
437450

438451
address_t local_address; /**< address used for connection, possibly different from identity */
439452

453+
private:
454+
uint8_t initiator_key_distribution:4;
455+
uint8_t responder_key_distribution:4;
456+
public:
440457
uint8_t connected:1;
441458
uint8_t authenticated:1; /**< have we turned encryption on during this connection */
442459
uint8_t is_master:1;
@@ -445,6 +462,7 @@ class GenericSecurityManager : public SecurityManager,
445462
uint8_t encryption_failed:1;
446463
uint8_t encrypted:1;
447464
uint8_t signing_requested:1;
465+
uint8_t signing_override_default:1;
448466

449467
uint8_t mitm_requested:1;
450468
uint8_t mitm_performed:1; /**< keys exchange will have MITM protection */

features/FEATURE_BLE/ble/pal/PalSecurityManager.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,18 @@ class KeyDistribution {
104104
operator uint8_t() {
105105
return _value;
106106
}
107-
uint8_t value() {
107+
108+
KeyDistribution operator&(const KeyDistribution& other) const {
109+
KeyDistribution result(this->value() & other.value());
110+
return result;
111+
}
112+
113+
KeyDistribution& operator&=(const KeyDistribution& other) {
114+
this->_value = this->_value & other.value();
115+
return *this;
116+
}
117+
118+
uint8_t value() const {
108119
return _value;
109120
}
110121

@@ -182,7 +193,7 @@ class AuthenticationMask {
182193
operator uint8_t() {
183194
return _value;
184195
}
185-
uint8_t value() {
196+
uint8_t value() const {
186197
return _value;
187198
}
188199

features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,34 @@ ble_error_t GenericSecurityManager::requestPairing(connection_handle_t connectio
121121
AuthenticationMask link_authentication(_default_authentication);
122122
link_authentication.set_mitm(cb->mitm_requested);
123123

124-
KeyDistribution link_key_distribution(_default_key_distribution);
125-
link_key_distribution.set_signing(cb->signing_requested);
126-
link_key_distribution.set_encryption(_master_sends_keys);
124+
/* by default the initiator doesn't send any keys other then identity */
125+
KeyDistribution initiator_distribution(
126+
KeyDistribution::KEY_DISTRIBUTION_IDENTITY | KeyDistribution::KEY_DISTRIBUTION_LINK
127+
);
128+
129+
/* if requested the initiator may send all the default keys for later
130+
* use when roles are changed */
131+
if (_master_sends_keys) {
132+
initiator_distribution = _default_key_distribution;
133+
}
134+
135+
/* override default if requested */
136+
initiator_distribution.set_signing(
137+
cb->signing_override_default ? cb->signing_requested : _default_key_distribution.get_signing()
138+
);
139+
140+
KeyDistribution responder_distribution(_default_key_distribution);
141+
142+
if (cb->signing_override_default) {
143+
responder_distribution.set_signing(cb->signing_requested);
144+
}
127145

128146
return _pal.send_pairing_request(
129147
connection,
130148
cb->oob_present,
131149
link_authentication,
132-
link_key_distribution,
133-
link_key_distribution
150+
initiator_distribution,
151+
responder_distribution
134152
);
135153
}
136154

@@ -145,15 +163,36 @@ ble_error_t GenericSecurityManager::acceptPairingRequest(connection_handle_t con
145163
AuthenticationMask link_authentication(_default_authentication);
146164
link_authentication.set_mitm(cb->mitm_requested);
147165

148-
KeyDistribution link_key_distribution(_default_key_distribution);
149-
link_key_distribution.set_signing(cb->signing_requested);
166+
KeyDistribution initiator_dist = cb->get_initiator_key_distribution();
167+
168+
if (_master_sends_keys) {
169+
initiator_dist &= _default_key_distribution;
170+
} else {
171+
initiator_dist &= KeyDistribution(KeyDistribution::KEY_DISTRIBUTION_IDENTITY | KeyDistribution::KEY_DISTRIBUTION_LINK);
172+
}
173+
174+
/* signing has to be offered and enabled on the link */
175+
initiator_dist.set_signing(
176+
initiator_dist.get_signing()
177+
&& (
178+
cb->signing_override_default ? cb->signing_requested : _default_key_distribution.get_signing())
179+
);
180+
181+
KeyDistribution responder_dist = cb->get_responder_key_distribution();
182+
183+
responder_dist &= _default_key_distribution;
184+
/* signing has to be requested and enabled on the link */
185+
responder_dist.set_signing(
186+
responder_dist.get_signing()
187+
&& (cb->signing_override_default ? cb->signing_requested : _default_key_distribution.get_signing())
188+
);
150189

151190
return _pal.send_pairing_response(
152191
connection,
153192
cb->oob_present,
154193
link_authentication,
155-
link_key_distribution,
156-
link_key_distribution
194+
responder_dist,
195+
responder_dist
157196
);
158197
}
159198

@@ -257,6 +296,7 @@ ble_error_t GenericSecurityManager::enableSigning(
257296
}
258297

259298
cb->signing_requested = enabled;
299+
cb->signing_override_default = false;
260300

261301
if (cb->encrypted) {
262302
return BLE_ERROR_INVALID_STATE;
@@ -799,6 +839,14 @@ void GenericSecurityManager::on_pairing_request(
799839
cancelPairingRequest(connection);
800840
}
801841

842+
ControlBlock_t *cb = get_control_block(connection);
843+
if (!cb) {
844+
return;
845+
}
846+
847+
cb->set_initiator_key_distribution(initiator_dist);
848+
cb->set_responder_key_distribution(responder_dist);
849+
802850
set_mitm_performed(connection, false);
803851

804852
if (_pairing_authorisation_required) {
@@ -1157,6 +1205,7 @@ GenericSecurityManager::ControlBlock_t::ControlBlock_t() :
11571205
encryption_failed(false),
11581206
encrypted(false),
11591207
signing_requested(false),
1208+
signing_override_default(false),
11601209
mitm_requested(false),
11611210
mitm_performed(false),
11621211
attempt_oob(false),

0 commit comments

Comments
 (0)