guild icon
Toit
#BLE Just Works Encrypted Pairing and Bonding
Thread channel in help
z3ugma
z3ugma 05/01/2024 08:43 PM
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.
Learn about how Paring and Bonding with Bluetooth while also taking into account, security implications of these procedures.
floitsch
floitsch 05/01/2024 08:44 PM
I will have a look at it tomorrow.
I'm working on BLE right now anyway.
πŸ‘πŸ»1
z3ugma
z3ugma 05/01/2024 08:45 PM
thanks, Florian. you're very helpful in all this!
z3ugma
z3ugma 05/01/2024 08:45 PM
thanks for the PR for handles
πŸ‘1
z3ugma
z3ugma 05/01/2024 08:47 PM
Current behavior is that when I do 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
z3ugma
z3ugma 05/01/2024 08:48 PM
FWIW the same behavior when I attempt to connect using nRFConnect iOS app connecting to the device
floitsch
floitsch 05/01/2024 08:49 PM
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.
z3ugma
z3ugma 05/01/2024 08:50 PM
yeah I've been using with_timeout and catching to implement that
floitsch
floitsch 05/01/2024 08:51 PM
I will look at the code tomorrow, but what happens if you use connect --secure?
floitsch
floitsch 05/01/2024 08:51 PM
Maybe that already uses "just works"
z3ugma
z3ugma 05/01/2024 08:53 PM
same hang. it immediately finds a device matching DEV_NAME and then when it moves on to connect, hangs
floitsch
floitsch 05/01/2024 08:53 PM
Ok. I will look tomorrow what it does.
z3ugma
z3ugma 05/01/2024 09:10 PM
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);
Hello - I am excited to find this code available on Arduino. Thank you for the library. For my ESP32, I am attempting to set up a simple secure GATT BLE bonding with the "Just Works" pair...
z3ugma
z3ugma 05/01/2024 09:10 PM
^^ breadcrumb from NimBLE
floitsch
floitsch 05/01/2024 09:11 PM
Hehe. I sent the same link to my work email a few minutes ago.
floitsch
floitsch 05/01/2024 09:12 PM
I think that's what the --secure flag does, though.
z3ugma
z3ugma 05/01/2024 09:12 PM
that might be when the ESP32 is acting as the server, not the client
floitsch
floitsch 05/01/2024 09:14 PM
Hmm. Didn't find any reference to setSecurityAuth, but that might be the Arduino wrapper.
floitsch
floitsch 05/01/2024 09:19 PM
The way it's currently implemented: when we get the MTU event and we want a secure connection we ble_gap_security_exchange
z3ugma
z3ugma 05/01/2024 09:19 PM
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
floitsch
floitsch 05/01/2024 09:20 PM
Nice.
floitsch
floitsch 05/01/2024 09:20 PM
So that sounds like --secure is indeed doing a "Just works" security.
I will add documentation
z3ugma
z3ugma 05/01/2024 09:35 PM
ok, maybe not.

I get /
z3ugma
z3ugma 05/01/2024 09:35 PM
EXCEPTION error.
Encryption is insufficient.
floitsch
floitsch 05/01/2024 09:37 PM
Toit package for provisioning. Contribute to toitware/toit-provision development by creating an account on GitHub.
floitsch
floitsch 05/01/2024 09:37 PM
It is using some encryption between the esp and the android app.
z3ugma
z3ugma 05/01/2024 09:38 PM
:πŸ‘ŒπŸ»:
floitsch
floitsch 05/01/2024 09:38 PM
I don't know (yet) if their approach is standard or just something that works with their app, but hopefully worth a look.
31 messages in total