https://toitlang.org/ logo
BLE Just Works Encrypted Pairing and Bonding
# help
z
Do you know if ble central.connect can handle the encryption bits from Just Works bonding? https://technotes.kynetics.com/2018/BLE_Pairing_and_bonding/#:~:text=in%20BLE%20legacy-,Pairing%20methods%3A,nonce%20to%20the%20initiating%20device I have this information from the user manual of my peripheral of interest: 6.2.2 Pairing/Bonding After connection, the collector should initiate pairing with the PERIPHERAL BLE. The PERIPHERAL BLE uses the Just Works pairing method with bonding support. Upon successful pairing, the collector should initiate bonding, and the PERIPHERAL BLE will save the long term bonding key associated with the collector. The PERIPHERAL BLE requires the collector to be bonded in order to encrypt the connection. When bonding is complete, the collector should encrypt the connection. If the PERIPHERAL BLE detects that the connection remains unencrypted after four seconds, it will send a slave security request to the collector. The collector should accept the request which will trigger the pairing or encryption sequence. If the connection remains unencrypted four seconds after the PERIPHERAL BLE sends the slave security request, the connection will be considered insecure and the PERIPHERAL BLE will close the connection. After the collector is bonded with the PERIPHERAL BLE, the collector should encrypt subsequent connections immediately. If the encryption process fails, it is likely that either the collector or PERIPHERAL BLE has deleted its bond key. When this occurs, the PERIPHERAL BLE will delete its bond key if present. It is recommended that the collector also delete its bond key if present. The subsequent connection will trigger the pairing process, a new bond will be created, and the collector will be able to encrypt the connection. Note: The PERIPHERAL BLE’s default security mode places further restrictions on bonding. See 6.3.1.3.2 for more information.
f
I will have a look at it tomorrow. I'm working on BLE right now anyway.
z
thanks, Florian. you're very helpful in all this!
thanks for the PR for handles
Current behavior is that when I do
Copy code
adapter := ble.Adapter
  central := adapter.central

  address := find_by_name central DEV_NAME
  remote_device := central.connect address
then it just hangs on
connect
for a very long time, minutes at least
FWIW the same behavior when I attempt to connect using nRFConnect iOS app connecting to the device
f
I have in my to-do list to add a timeout to connecting. Even for non-encrypted devices it sometimes fails without any event from the lower layer.
z
yeah I've been using
with_timeout
and catching to implement that
f
I will look at the code tomorrow, but what happens if you use
connect --secure
?
Maybe that already uses "just works"
z
same hang. it immediately finds a device matching DEV_NAME and then when it moves on to connect, hangs
f
Ok. I will look tomorrow what it does.
z
https://github.com/h2zero/NimBLE-Arduino/issues/588 Hello, the only thing you need to do for this is to enable bonding: NimBLEDevice::setSecurityAuth(true, false, true);
^^ breadcrumb from NimBLE
f
Hehe. I sent the same link to my work email a few minutes ago.
I think that's what the
--secure
flag does, though.
z
that might be when the ESP32 is acting as the server, not the client
f
Hmm. Didn't find any reference to
setSecurityAuth
, but that might be the Arduino wrapper.
The way it's currently implemented: when we get the MTU event and we want a secure connection we
ble_gap_security_exchange
z
okay, I think I've got it. The
--secure
flag is what was needed. Additionally, my device has a 2-minute window exactly after inserting the batteries that it will attempt new connections
f
Nice.
So that sounds like
--secure
is indeed doing a "Just works" security. I will add documentation
z
ok, maybe not. I get /
EXCEPTION error. Encryption is insufficient.
f
It is using some encryption between the esp and the android app.
z
👌🏻
f
I don't know (yet) if their approach is standard or just something that works with their app, but hopefully worth a look.
3 Views