Connection using old pairing information saved

I am running an experiment to use a static keys for pairing information without breaking the existing flow of pairing/bonding.
What I am doing is I am storing the pairing info of my central device and storing it into a structure and then erasing the flash to remove those information, and after reset I am retrieving those bonding information and connecting again using the same central devices but I am getting "Peer removed bonding info" response on my phone.

void store_ltk_in_zephyr(void)
{
    int id, err;
    struct bt_keys pairing_info = {
    .addr = {
        .type = BT_ADDR_LE_PUBLIC,
        .a.val = {0x43,0x82,0x5E,0xC7,0xE8,0xF4,0xFD}
    },
    .irk = {0x8A, 0x27, 0x1E, 0xA7, 0x92, 0x2A, 0xF0, 0x15, 0x41, 0x69, 0x48, 0xDD, 0xC0, 0x7E, 0xDD, 0xF7},
    .ltk = {
        .val = {0xc0, 0xe6, 0x9a, 0x0a, 0xf7, 0x4b, 0xdc, 0xb7, 0x7d, 0x23, 0xf4, 0xb4, 0x89, 0x8d, 0x96, 0x02},
        .ediv = {0x00,0x00},
        .rand = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
      }
    };

    // Store the keys using the settings API
    err = bt_keys_store(&pairing_info);
    if (err) {
        printk("Failed to store keys (err %d)\n", err);
    } else {
        printk("Keys stored successfully\n");
    }
}


In main, I am calling like this to load the info onto the flash- 

  settings_subsys_init();
  //smp_bt_register();
	bt_conn_auth_cb_register(&auth_cb_display);
  bt_conn_auth_info_cb_register(&conn_auth_info_callbacks);
	err = bt_enable(NULL);
	if (err) {
		printk("Bluetooth init failed (err %d)\n", err);
    k_sleep(K_MSEC(100));
		err = bt_enable(NULL);                                      //Trying one more time after this system reset
    if(err) NVIC_SystemReset();
	}
  store_ltk_in_zephyr();
  if (IS_ENABLED(CONFIG_SETTINGS)) {
    settings_load();
  }


And on connect I am using like this-
  bt_conn_set_security(conn, BT_SECURITY_L1|BT_SECURITY_FORCE_PAIR);


Do let me know, how can I connect with the same bonding info which I have made a copy of from the same bonding structure.

Parents Reply Children
  • ou can also enable CONFIG_BT_KEYS_LOG_LEVEL_DBG=y CONFIG_BT_SMP_LOG_LEVEL_DBG=y to enable more logging relevant for BLE security.

    Getting these logs from the suggestions-

    00> [00:00:00.020,874] <inf> fs_nvs: 2 Sectors of 4096 bytes
    00> [00:00:00.020,904] <inf> fs_nvs: alloc wra: 0, f98
    00> [00:00:00.020,904] <inf> fs_nvs: data wra: 0, b4
    00> [00:00:06.323,638] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
    00>                                             36 f0 e5 0e 87 68 48 fb  02 fd 9f 82 cc 32 e5 7b |6....hH. .....2.{
    00>                                             91 b1 5c ed                                      |..\.             
    00> [00:00:06.326,904] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
    00> [00:00:06.326,934] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
    00> [00:00:06.326,965] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 54.58864 Build 1214809870
    00> [00:00:06.327,331] <dbg> bt_smp: bt_smp_init: LE SC enabled
    00> [00:00:06.327,636] <inf> bt_hci_core: No ID address. App must call settings_load()
    00> BT is ready now 
    00> [00:00:06.328,247] <dbg> bt_keys: bt_keys_store: Stored keys for F4:E8:C7:5E:82:43 (public)
    00> Keys stored successfully
    00> [00:00:06.328,674] <dbg> bt_keys: keys_set: name f4e8c75e82430 val 0000000000000000000000000000c0e69a0af74bdcb77d23f4b4898d96028a271ea7922af015416948ddc07eddf7000000000000000000000000000000000000
    00> [00:00:06.328,887] <dbg> bt_keys: bt_keys_get_addr: F4:E8:C7:5E:82:43 (public)
    00> [00:00:06.329,101] <dbg> bt_keys: bt_keys_get_addr: created 0x2001265c for F4:E8:C7:5E:82:43 (public)
    00> [00:00:06.329,284] <dbg> bt_keys: keys_set: Successfully restored keys for F4:E8:C7:5E:82:43 (public)
    00> [00:00:06.329,925] <inf> bt_hci_core: Identity: D3:C4:88:DC:F4:8D (random)
    00> [00:00:06.329,956] <inf> bt_hci_core: HCI: version 5.4 (0x0d) revision 0x118f, manufacturer 0x0059
    00> [00:00:06.329,986] <inf> bt_hci_core: LMP: version 5.4 (0x0d) subver 0x118f
    00> Bluetooth initialized
    00> Advertising successfully started
    00> [00:00:06.336,639] <err> bt_gatt: Unable to register handle 0x002d
    00> [00:00:06.360,717] <dbg> bt_smp: bt_smp_pkey_ready: 

  • It should be printed when BLE is enabled. What logs are you getting? Is the Bluetooth ID being printed.

    For printing the logs that you have mentioned, bt_dev_show_info() has to be called which is called in the call stack -


    But in my call stack this loop exited before calling for bt_dev_show_info() because of the highlighted part.

  • The ID is correctly printed after settings_load(). Next you may want to verify that the key and peer andress have the correct endianess. If you use the same phone to bond with your zephyr application, do you see the same public address in your bond table?

    And on connect I am using like this-
    Fullscreen
    1
    bt_conn_set_security(conn, BT_SECURITY_L1|BT_SECURITY_FORCE_PAIR);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      bt_conn_set_security(conn, BT_SECURITY_L1|BT_SECURITY_FORCE_PAIR);
    The BT_SECURITY_FORCE_PAIR flag should not be set if you want the link to be encrypted with the existing key. Also the sec. level should be set to '2'.
  • The BT_SECURITY_FORCE_PAIR flag should not be set if you want the link to be encrypted with the existing key. Also the sec. level should be set to '2'.

    I have made these changes and tried to connect, but the issue remains same.

    I am seeing below logs when trying to connect-

    00> Connected
    00> [00:00:32.147,583] <dbg> bt_smp: smp_send_security_req: 
    00> [00:00:32.147,766] <dbg> bt_keys: bt_keys_get_addr: 5D:F9:D2:71:90:43 (random)
    00> [00:00:32.147,949] <dbg> bt_keys: bt_keys_get_addr: created 0x200126b8 for 5D:F9:D2:71:90:43 (random)
    00> [00:00:32.148,162] <dbg> bt_smp: smp_init: prnd 1b6108926f3a6d0aa5ca0235c29450af
    00> [00:00:32.303,466] <dbg> bt_smp: bt_smp_encrypt_change: chan 0x20004250 conn 0x20003e58 handle 0 encrypt 0x00 hci status 0x06
    00> Security failed: 5D:F9:D2:71:90:43 (random) level 1 err 2
    00> [00:00:32.303,680] <dbg> bt_smp: smp_send_security_req: 
    00> [00:00:32.303,863] <dbg> bt_smp: smp_init: prnd c58951f430b834164a58620adc842f37
    00> [00:00:32.393,463] <dbg> bt_smp: bt_smp_disconnected: chan 0x20004250 cid 0x0006
    00> [00:00:32.393,646] <dbg> bt_keys: bt_keys_clear: 5D:F9:D2:71:90:43 (random) (keys 0x0000)
    00> MTU exchange failed (err 14)[00:00:32.394,317] <dbg> bt_keys: bt_keys_find_addr: 5D:F9:D2:71:90:43 (random)
    00> [00:00:32.394,500] <dbg> bt_keys: bt_keys_find_addr: 5D:F9:D2:71:90:43 (random)
    00> Disconnected (reason 0x13)

  • Please verify that they keys and addresses are stored with the correct endianess.

Related