Statickey authentication problem with pc-ble-driver-py

Hello everyone, I'm not able to perform the authentication process with statickey and bonding using the pc-ble-driver-py.

My setup is the following:

  • Central : Devkit nRF52832 controlled with pc-ble-driver-py.
  • Peripheral : Proteus III with base firmware that requires authentication with a statickey and bonding in order to maintain the connection. LESC is disabled.

The authentication and bonding process works fine if I use the android nrf Connect app or the Windows nrf connect app with the same devkit mentioned above.
The problem comes when I try to automate the task with pc-ble-driver-py. For this, I have regarded the tests available on github and some code snippets from the DevZone, obtaining :

    def start(self, connect_with, conn_params):

        req_phys = [driver.BLE_GAP_PHY_1MBPS, driver.BLE_GAP_PHY_1MBPS]

        self.connect_with = connect_with
        logger.info(f"scan_start, trying to find {self.connect_with}")
        scan_duration = 20
        self.adapter.driver.ble_gap_scan_start()

        self.conn_handle = self.conn_q.get(timeout=scan_duration)

        if self.conn_handle is None:
            return False

        kdist_own = BLEGapSecKDist(True, True, False, False)
        kdist_peer = BLEGapSecKDist(True, True, False, False)
        sec_params = BLEGapSecParams(bond=True,
                                     mitm=False,
                                     lesc=False,
                                     oob=False,
                                     keypress=True,
                                     io_caps=BLEGapIOCaps.display_only, #BLEGapIOCaps.display_only keyboard_display
                                     min_key_size=7,
                                     max_key_size=16,
                                     kdist_own=kdist_own,
                                     kdist_peer=kdist_peer)

        self.adapter.driver.ble_gap_authenticate(self.conn_handle, sec_params)

        # Wait for gap_evt_sec_params_request
        result = self.adapter.evt_sync[self.conn_handle].wait(BLEEvtID.gap_evt_sec_params_request)

        # LE Secure Connections used only if both sides support it.
        if not sec_params.lesc or not result["peer_params"].lesc:
            # sd_ble_gap_sec_params_reply ... In the central role, sec_params must be set to NULL,
            # as the parameters have already been provided during a previous call to
            # sd_ble_gap_authenticate
            sec_params = (
                None
            )
            sec_keyset = driver.ble_gap_sec_keyset_t()
            self.adapter.driver.ble_gap_sec_params_reply(
                self.conn_handle, BLEGapSecStatus.success, sec_params=sec_params, keyset=None
            )
        else:
            self.adapter.evt_sync[self.conn_handle].wait(evt=BLEEvtID.gap_evt_lesc_dhkey_request, timeout=5)

        result = self.adapter.evt_sync[self.conn_handle].wait(evt=BLEEvtID.gap_evt_auth_key_request)

        # Wait for gap_evt_auth_status
        result = self.adapter.evt_sync[self.conn_handle].wait(BLEEvtID.gap_evt_auth_status)

        self.adapter.data_length_update(self.conn_handle, DLE_UPDATE)
        self.adapter.conn_param_update(self.conn_handle, conn_params)
        self.adapter.service_discovery(self.conn_handle)

        self.enable_notification(BLEUUID(UUID_CUSTOM_CHAR_TX, CUSTOM_BASE))

Comparing with the following diagram:

I have detected that for some reason the event "BLEEvtID.gap_evt_auth_key_request" is not captured, the callback is not executed and therefore the Central does not send the PassKey to the peripheric. This means that any action like the activation of the notifications or the writing of a Characteristic, generates this error : "BLEGattStatusCode.insuf_authentication".

I attach also the log of pc-ble-driver-py:

2023-01-11 12:59:37,627 [20096/EventThread] Trying to connect to peripheral advertising as XYZ, address: 0x00FFFFFFFFF
2023-01-11 12:59:37,629 [20096/EventThread] evt> adv_report conn(65535)
 peer_addr(0x00FFFFFFFFF)
 rssi(-30)
 adv_type(None)
2023-01-11 12:59:37,673 [20096/EventThread] (Central) New connection: 0.
2023-01-11 12:59:37,677 [20096/EventThread] evt> on_gatts_evt_exchange_mtu_request conn(0) client_mtu(247)
2023-01-11 12:59:37,735 [20096/EventThread] Central on_gap_evt_sec_params_request. peer_params.lesc=0
2023-01-11 12:59:38,093 [20096/EventThread] evt> conn_sec_update conn(0)
 conn_sec(sec_mode(sm(1) lv(2))) encr_key_size(16))
2023-01-11 12:59:38,188 [20096/EventThread] evt> auth_status conn(0)
 error_src(0)
 bonded(1)
 sm1_levels(<pc_ble_driver_py.lib.nrf_ble_driver_sd_api_v5.ble_gap_sec_levels_t; proxy of <Swig Object of type 'ble_gap_sec_levels_t *' at 0x00000132A1B2D8C0> >)
 sm2_levels(<pc_ble_driver_py.lib.nrf_ble_driver_sd_api_v5.ble_gap_sec_levels_t; proxy of <Swig Object of type 'ble_gap_sec_levels_t *' at 0x00000132A1B2D770> >)
 kdist_own(enc(1) id(1) sign(0) link(0))
 kdist_peer(enc(1) id(1) sign(0) link(0))
 auth_status(BLEGapSecStatus.success)
2023-01-11 12:59:47,873 [19672/MainThread] New Data Length is 251
2023-01-11 12:59:48,121 [20096/EventThread] evt> on_gattc_evt_prim_srvc_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Service uuid(0x1800) start handle(1) end handle(9)
 Service uuid(0x1801) start handle(10) end handle(10)
2023-01-11 12:59:48,136 [20096/EventThread] evt> on_gattc_evt_prim_srvc_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Service uuid(0x0001) start handle(11) end handle(65535)
2023-01-11 12:59:48,152 [20096/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Characteristic uuid(0x2A00) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(2) value handle(3)
 Characteristic uuid(0x2A01) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(4) value handle(5)
 Characteristic uuid(0x2A04) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(6) value handle(7)
2023-01-11 12:59:48,166 [20096/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Characteristic uuid(0x2AA6) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(8) value handle(9)
2023-01-11 12:59:48,181 [20096/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(0)
 
2023-01-11 12:59:48,196 [20096/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Descriptor uuid(0x2A00) handle(3)
2023-01-11 12:59:48,211 [20096/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Descriptor uuid(0x2A01) handle(5)
2023-01-11 12:59:48,226 [20096/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Descriptor uuid(0x2A04) handle(7)
2023-01-11 12:59:48,241 [20096/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Descriptor uuid(0x2AA6) handle(9)
2023-01-11 12:59:48,256 [20096/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(0)
 
2023-01-11 12:59:48,271 [20096/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Characteristic uuid(0x0002) properties(BLECharProperties(broadcast=0, read=0, write_wo_resp=1, write=1, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(12) value handle(13)
2023-01-11 12:59:48,286 [20096/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Characteristic uuid(0x0003) properties(BLECharProperties(broadcast=0, read=0, write_wo_resp=0, write=0, notify=1, indicate=0, auth_signed_wr=0)) declaration handle(14) value handle(15)
2023-01-11 12:59:48,301 [20096/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(0)
 
2023-01-11 12:59:48,316 [20096/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Descriptor uuid(0x0002) handle(13)
2023-01-11 12:59:48,331 [20096/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Descriptor uuid(0x0003) handle(15)
2023-01-11 12:59:48,346 [20096/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
 Descriptor uuid(0x2902 (Standard.cccd)) handle(16)
2023-01-11 12:59:48,361 [20096/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(0)
 
2023-01-11 12:59:48,376 [20096/EventThread] evt> on_gattc_evt_write_rsp conn(0)
 status(BLEGattStatusCode.insuf_authentication)
 error_handle(16)
 attr_handle(16)
 write_op(BLEGattWriteOperation.write_req)
 offset(0)
 data([])
Traceback (most recent call last):
    self.adapter.enable_notification(self.conn_handle, uuid)
  File "...\venv\lib\site-packages\pc_ble_driver_py\ble_driver.py", line 106, in wrapper
    raise NordicSemiException(
pc_ble_driver_py.exceptions.NordicSemiException: Failed to enable_notification. Error code: BLEGattStatusCode.insuf_authentication

any idea of what could be the problem ?
thanks for your help,

Jakub.

Parents Reply Children
  • Thank you very much for your answer, it helped me to find the solution!

    In the end my problem was a combination of two problems:

    • The "BLEGapIOCaps" has to be configured as "keyboard_display", for the Central in my case"(I discarded it at first because I did not get a bonded flag correctly).
    • The type of the KEY has to be a bytestring and not an integer.

    Thanks again for your help.


    best regards,

    Jakub

Related