@@ -121,16 +121,34 @@ ble_error_t GenericSecurityManager::requestPairing(connection_handle_t connectio
121
121
AuthenticationMask link_authentication (_default_authentication);
122
122
link_authentication.set_mitm (cb->mitm_requested );
123
123
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
+ }
127
145
128
146
return _pal.send_pairing_request (
129
147
connection,
130
148
cb->oob_present ,
131
149
link_authentication,
132
- link_key_distribution ,
133
- link_key_distribution
150
+ initiator_distribution ,
151
+ responder_distribution
134
152
);
135
153
}
136
154
@@ -145,15 +163,36 @@ ble_error_t GenericSecurityManager::acceptPairingRequest(connection_handle_t con
145
163
AuthenticationMask link_authentication (_default_authentication);
146
164
link_authentication.set_mitm (cb->mitm_requested );
147
165
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
+ );
150
189
151
190
return _pal.send_pairing_response (
152
191
connection,
153
192
cb->oob_present ,
154
193
link_authentication,
155
- link_key_distribution ,
156
- link_key_distribution
194
+ responder_dist ,
195
+ responder_dist
157
196
);
158
197
}
159
198
@@ -257,6 +296,7 @@ ble_error_t GenericSecurityManager::enableSigning(
257
296
}
258
297
259
298
cb->signing_requested = enabled;
299
+ cb->signing_override_default = false ;
260
300
261
301
if (cb->encrypted ) {
262
302
return BLE_ERROR_INVALID_STATE;
@@ -799,6 +839,14 @@ void GenericSecurityManager::on_pairing_request(
799
839
cancelPairingRequest (connection);
800
840
}
801
841
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
+
802
850
set_mitm_performed (connection, false );
803
851
804
852
if (_pairing_authorisation_required) {
@@ -1157,6 +1205,7 @@ GenericSecurityManager::ControlBlock_t::ControlBlock_t() :
1157
1205
encryption_failed (false ),
1158
1206
encrypted (false ),
1159
1207
signing_requested (false ),
1208
+ signing_override_default (false ),
1160
1209
mitm_requested (false ),
1161
1210
mitm_performed (false ),
1162
1211
attempt_oob (false ),
0 commit comments