19
19
* Common includes *
20
20
***************************************************************************/
21
21
22
+ #include " Arduino.h"
22
23
#include < esp_bt.h>
23
24
#include " BLEClient.h"
24
25
#include " BLEUtils.h"
29
30
#include < unordered_set>
30
31
#include " BLEDevice.h"
31
32
#include " esp32-hal-log.h"
33
+ #include " BLESecurity.h"
32
34
33
35
/* **************************************************************************
34
36
* Bluedroid includes *
@@ -598,11 +600,11 @@ void BLEClient::gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t
598
600
if (errRc != ESP_OK) {
599
601
log_e (" esp_ble_gattc_send_mtu_req: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
600
602
}
601
- #ifdef CONFIG_BLE_SMP_ENABLE // Check that BLE SMP (security) is configured in make menuconfig
602
- if (BLEDevice::m_securityLevel) {
603
- esp_ble_set_encryption (evtParam->connect .remote_bda , BLEDevice::m_securityLevel);
603
+ // Set encryption on connect for BlueDroid when security is enabled
604
+ // This ensures security is established before any secure operations
605
+ if (BLESecurity::m_securityEnabled && BLESecurity::m_forceSecurity) {
606
+ BLESecurity::startSecurity (evtParam->connect .remote_bda );
604
607
}
605
- #endif // CONFIG_BLE_SMP_ENABLE
606
608
break ;
607
609
} // ESP_GATTC_CONNECT_EVT
608
610
@@ -1006,6 +1008,10 @@ int BLEClient::handleGAPEvent(struct ble_gap_event *event, void *arg) {
1006
1008
break ;
1007
1009
}
1008
1010
1011
+ if (BLESecurity::m_securityEnabled) {
1012
+ BLESecurity::startSecurity (client->m_conn_id );
1013
+ }
1014
+
1009
1015
// In the case of a multiconnecting device we ignore this device when
1010
1016
// scanning since we are already connected to it
1011
1017
// BLEDevice::addIgnored(client->m_peerAddress);
@@ -1136,8 +1142,6 @@ int BLEClient::handleGAPEvent(struct ble_gap_event *event, void *arg) {
1136
1142
ble_store_util_delete_peer (&desc.peer_id_addr );
1137
1143
} else if (BLEDevice::m_securityCallbacks != nullptr ) {
1138
1144
BLEDevice::m_securityCallbacks->onAuthenticationComplete (&desc);
1139
- } else {
1140
- client->m_pClientCallbacks ->onAuthenticationComplete (&desc);
1141
1145
}
1142
1146
}
1143
1147
@@ -1164,52 +1168,88 @@ int BLEClient::handleGAPEvent(struct ble_gap_event *event, void *arg) {
1164
1168
}
1165
1169
1166
1170
if (event->passkey .params .action == BLE_SM_IOACT_DISP) {
1171
+ // Display the passkey on this device
1172
+ log_d (" BLE_SM_IOACT_DISP" );
1173
+
1167
1174
pkey.action = event->passkey .params .action ;
1168
- pkey.passkey = BLESecurity::m_passkey; // This is the passkey to be entered on peer
1175
+ pkey.passkey = BLESecurity::getPassKey (); // This is the passkey to be entered on peer
1176
+
1177
+ if (!BLESecurity::m_passkeySet) {
1178
+ log_w (" No passkey set" );
1179
+ }
1180
+
1181
+ if (BLESecurity::m_staticPasskey && pkey.passkey == BLE_SM_DEFAULT_PASSKEY) {
1182
+ log_w (" *ATTENTION* Using default passkey: %06d" , BLE_SM_DEFAULT_PASSKEY);
1183
+ log_w (" *ATTENTION* Please use a random passkey or set a different static passkey" );
1184
+ } else {
1185
+ log_i (" Passkey: %d" , pkey.passkey );
1186
+ }
1187
+
1188
+ if (BLEDevice::m_securityCallbacks != nullptr ) {
1189
+ BLEDevice::m_securityCallbacks->onPassKeyNotify (pkey.passkey );
1190
+ }
1191
+
1169
1192
rc = ble_sm_inject_io (event->passkey .conn_handle , &pkey);
1170
1193
log_d (" ble_sm_inject_io result: %d" , rc);
1171
1194
1172
1195
} else if (event->passkey .params .action == BLE_SM_IOACT_NUMCMP) {
1196
+ // Check if the passkey on the peer device is correct
1197
+ log_d (" BLE_SM_IOACT_NUMCMP" );
1198
+
1173
1199
log_d (" Passkey on device's display: %d" , event->passkey .params .numcmp );
1174
1200
pkey.action = event->passkey .params .action ;
1175
- // Compatibility only - Do not use, should be removed the in future
1201
+
1176
1202
if (BLEDevice::m_securityCallbacks != nullptr ) {
1177
1203
pkey.numcmp_accept = BLEDevice::m_securityCallbacks->onConfirmPIN (event->passkey .params .numcmp );
1178
- // //////////////////////////////////////////////////
1179
1204
} else {
1180
- pkey.numcmp_accept = client->m_pClientCallbacks ->onConfirmPIN (event->passkey .params .numcmp );
1205
+ log_e (" onConfirmPIN not implemented. Rejecting connection" );
1206
+ pkey.numcmp_accept = 0 ;
1181
1207
}
1182
1208
1183
1209
rc = ble_sm_inject_io (event->passkey .conn_handle , &pkey);
1184
1210
log_d (" ble_sm_inject_io result: %d" , rc);
1185
1211
1186
- // TODO: Handle out of band pairing
1187
1212
} else if (event->passkey .params .action == BLE_SM_IOACT_OOB) {
1213
+ // Out of band pairing
1214
+ // TODO: Handle out of band pairing
1215
+ log_w (" BLE_SM_IOACT_OOB: Not implemented" );
1216
+
1188
1217
static uint8_t tem_oob[16 ] = {0 };
1189
1218
pkey.action = event->passkey .params .action ;
1190
1219
for (int i = 0 ; i < 16 ; i++) {
1191
1220
pkey.oob [i] = tem_oob[i];
1192
1221
}
1193
1222
rc = ble_sm_inject_io (event->passkey .conn_handle , &pkey);
1194
1223
log_d (" ble_sm_inject_io result: %d" , rc);
1195
- // //////
1196
1224
} else if (event->passkey .params .action == BLE_SM_IOACT_INPUT) {
1197
- log_d (" Enter the passkey" );
1225
+ // Input passkey from peer device
1226
+ log_d (" BLE_SM_IOACT_INPUT" );
1227
+
1198
1228
pkey.action = event->passkey .params .action ;
1229
+ pkey.passkey = BLESecurity::getPassKey ();
1230
+
1231
+ if (!BLESecurity::m_passkeySet) {
1232
+ if (BLEDevice::m_securityCallbacks != nullptr ) {
1233
+ log_i (" No passkey set, getting passkey from onPassKeyRequest" );
1234
+ pkey.passkey = BLEDevice::m_securityCallbacks->onPassKeyRequest ();
1235
+ } else {
1236
+ log_w (" *ATTENTION* onPassKeyRequest not implemented and no static passkey set." );
1237
+ }
1238
+ }
1199
1239
1200
- // Compatibility only - Do not use, should be removed the in future
1201
- if (BLEDevice::m_securityCallbacks != nullptr ) {
1202
- pkey.passkey = BLEDevice::m_securityCallbacks->onPassKeyRequest ();
1203
- // ///////////////////////////////////////////
1240
+ if (BLESecurity::m_staticPasskey && pkey.passkey == BLE_SM_DEFAULT_PASSKEY) {
1241
+ log_w (" *ATTENTION* Using default passkey: %06d" , BLE_SM_DEFAULT_PASSKEY);
1242
+ log_w (" *ATTENTION* Please use a random passkey or set a different static passkey" );
1204
1243
} else {
1205
- pkey.passkey = client-> m_pClientCallbacks -> onPassKeyRequest ( );
1244
+ log_i ( " Passkey: %d " , pkey.passkey );
1206
1245
}
1207
1246
1208
1247
rc = ble_sm_inject_io (event->passkey .conn_handle , &pkey);
1209
1248
log_d (" ble_sm_inject_io result: %d" , rc);
1210
1249
1211
1250
} else if (event->passkey .params .action == BLE_SM_IOACT_NONE) {
1212
- log_d (" No passkey action required" );
1251
+ log_d (" BLE_SM_IOACT_NONE" );
1252
+ log_i (" No passkey action required" );
1213
1253
}
1214
1254
1215
1255
return 0 ;
@@ -1255,20 +1295,6 @@ bool BLEClientCallbacks::onConnParamsUpdateRequest(BLEClient *pClient, const ble
1255
1295
return true ;
1256
1296
}
1257
1297
1258
- uint32_t BLEClientCallbacks::onPassKeyRequest () {
1259
- log_d (" onPassKeyRequest: default: 123456" );
1260
- return 123456 ;
1261
- }
1262
-
1263
- void BLEClientCallbacks::onAuthenticationComplete (ble_gap_conn_desc *desc) {
1264
- log_d (" onAuthenticationComplete: default" );
1265
- }
1266
-
1267
- bool BLEClientCallbacks::onConfirmPIN (uint32_t pin) {
1268
- log_d (" onConfirmPIN: default: true" );
1269
- return true ;
1270
- }
1271
-
1272
1298
#endif // CONFIG_NIMBLE_ENABLED
1273
1299
1274
1300
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
0 commit comments