NimBLE-Arduino icon indicating copy to clipboard operation
NimBLE-Arduino copied to clipboard

Cannot establish secure connection on ESP32

Open ohdarling opened this issue 2 years ago • 6 comments

Hi, I'm using NimBLE to connect ESP32 to a BLE server, and it needs secure connection, but when I perform a secureConnection() on client, it is failed with reason code 13.

I'm testing with the example https://github.com/h2zero/NimBLE-Arduino/blob/release/1.4/examples/NimBLE_Secure_Client/NimBLE_Secure_Client.ino, and just change UUID to expected UUID.

See logs below:

D NimBLEClient: Got Client event BLE_GAP_EVENT_CONNECT 
I NimBLEClient: Connected event
D NimBLEClient: Got Client event BLE_GAP_EVENT_MTU
I NimBLEClient: mtu update event; conn_handle=0 mtu=255
I NimBLEClient: Connection established
D NimBLEClient: >> deleteServices
D NimBLEClient: << deleteServices
D NimBLEClientCallbacks: onConnect: default
D NimBLEClient: << connect()
D NimBLEClient: >> secureConnection()
D NimBLEClient: Got Client event BLE_GAP_EVENT_ENC_CHANGE
E NimBLEClient: secureConnection: failed rc=13
D NimBLEClient: >> getService: uuid: 8000dd00-dd00-ffff-ffff-ffffffffffff
D NimBLEClient: >> retrieveServices
D NimBLEClient: Service Discovered >> status: 7 handle: -1
E NimBLEClient: serviceDiscoveredCB() rc=7 No open connection with the specified handle.
D NimBLEClient: << Service Discovered
E NimBLEClient: Could not retrieve services
D NimBLEClient: Got Client event BLE_GAP_EVENT_DISCONNECT
D NimBLEClient: << getService: not found
I NimBLEClient: disconnect; reason=531, Remote User Terminated Connection
D NimBLEClient: >> deleteServices
D NimBLEClient: << deleteServices
D
 NimBLEClientCallbacks: onDisconnect: default

When I use the ESP32 BLE stack, I should use setAuth method of Characteristic with param ESP_GATT_AUTH_REQ_NO_MITM, is there any equivalent code to do that in NimBLE?

Thanks.

Platform Packages version:

PLATFORM: Espressif 32 (6.0.0) > ESP32-WROOM-32E (16MB) HARDWARE: ESP32 240MHz, 320KB RAM, 16MB Flash DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa) PACKAGES:

  • framework-arduinoespressif32 @ 3.20006.221224 (2.0.6)
  • tool-esptoolpy @ 1.40400.0 (4.4.0)
  • tool-mkfatfs @ 2.0.1
  • tool-mklittlefs @ 1.203.210628 (2.3)
  • tool-mkspiffs @ 2.230.0 (2.30)
  • toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5

ohdarling avatar Jan 22 '23 17:01 ohdarling

Also I saw the comment in the header of the example, and performed the erase_flash on ESP32.

ohdarling avatar Jan 22 '23 17:01 ohdarling

The error is because the connection times out, which causes the secure connection call to fail and all the subsequent calls will obviously fail also. From the log rc=13 is BLE_HS_ETIMEOUT Operation timed out.

h2zero avatar Feb 04 '23 17:02 h2zero

Yes, I think the timeout is caused by the BLE server does not confirmed the connection. When I use internal BLE library, after setting ESP_GATT_AUTH_REQ_NO_MITM to characteristic, the BLE serve prompts user to confirm the connection, as in the code below:

pLocReadChar->setAuth(ESP_GATT_AUTH_REQ_NO_MITM);
pLocReadChar->readValue();

After readValue, the BLE server displays a confirmation dialog.

I also tried setSecurityIOCap of NimBLE, but it didn't work:

BLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_YESNO);
BLEDevice::setSecurityAuth(true, true, true);

Is there any equivalent method to do ESP_GATT_AUTH_REQ_NO_MITM in NimBLE?

Thanks.

ohdarling avatar Feb 05 '23 05:02 ohdarling

Is there any equivalent method to do ESP_GATT_AUTH_REQ_NO_MITM in NimBLE?

This should do the same thing: NimBLEDevice::setSecurityAuth(true, false, true);

h2zero avatar Feb 20 '23 16:02 h2zero

@ohdarling how did you manage to get these NimBle logs ?

mjamry avatar Apr 13 '23 13:04 mjamry

@ohdarling how did you manage to get these NimBle logs ?

Add build flags in platformio.ini:

build_flags =
    -DCONFIG_NIMBLE_CPP_LOG_LEVEL=5
    -DCONFIG_BT_NIMBLE_LOG_LEVEL=0

ohdarling avatar Apr 13 '23 13:04 ohdarling

closing as stale.

h2zero avatar Jun 04 '24 22:06 h2zero