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
  • I have changed the flag to SMP_FLAG_SC for pairing_info->flags = 5 and also copied EDIV and RAND values to the structures now, but still same issue.

    You were previously testing with LESC bonding. As mentioned earlier, you should not set the SC bit in NCS if you use legacy pairing. I also believe you need to store the EDIV and Rand.

    Should I enable CONFIG_BT_SMP_SC_PAIR_ONLY and remove bt_conn_set_security(conn, BT_SECURITY_L2) for my connection?

     

Children
  • Should I enable CONFIG_BT_SMP_SC_PAIR_ONLY and remove bt_conn_set_security(conn, BT_SECURITY_L2) for my connection?

    I have specifically disabled this config CONFIG_BT_SMP_SC_PAIR_ONLY=n but still stuck in the same state of "Peer removed info" error.

    I have tested with pairing_info->flags = 3 also but still same result.

  • From the description of the CONFIG_BT_SMP_SC_PAIR_ONLY symbol you can see that it forces sec. level 4 and is disabled by default.

    Gaurav said:
    I have tested with pairing_info->flags = 3 also but still same result.

    pairing_info->flags should be '0' for legacy pairing. Also, I mistook the SMP_FLAG_SC for BT_KEYS_SC. They have the same value, but the latter should be the correct one.

    params_info->keys is also different for legacy bonds. It looks like it shoul be 'BT_KEYS_LTK'.

  • params_info->keys is also different for legacy bonds. It looks like it shoul be 'BT_KEYS_LTK'.

    I didn`t really get here, what all should be changed in my application for this to work?

  • pairing_info->flags should be '0' for legacy pairing. Also, I mistook the SMP_FLAG_SC for BT_KEYS_SC. They have the same value, but the latter should be the correct one.

    I have made pairing_info->flags=0 and pairing_info->keys = BT_KEYS_LTK, but still in stuck state

  • Seeing this log with  changes pairing_info->flags=0 and pairing_info->keys =BT_KEYS_IRK | BT_KEYS_LTK_P256

    00> [00:01:01.855,712] <dbg> bt_keys: bt_keys_find_irk: 76:B0:CA:9C:DB:FE (random)
    00> [00:01:01.856,048] <dbg> bt_keys: bt_keys_find_irk: cached RPA 76:B0:CA:9C:DB:FE for F4:E8:C7:5E:82:43 (public)
    00> [00:01:01.856,109] <dbg> bt_smp: bt_smp_accept: conn 0x20003e58 handle 1
    00> [00:01:01.856,140] <dbg> bt_smp: bt_smp_connected: chan 0x200042b0 cid 0x0006
    00> [00:01:01.856,384] <dbg> bt_keys: bt_keys_find_addr: F4:E8:C7:5E:82:43 (public)
    00> Watchdog is getting fed here : adv 0 and conn 1
    00> Connected
    00> [00:01:01.857,025] <dbg> bt_smp: smp_send_security_req: 
    00> [00:01:01.857,208] <dbg> bt_keys: bt_keys_get_addr: F4:E8:C7:5E:82:43 (public)
    00> [00:01:01.857,391] <dbg> bt_smp: smp_init: prnd 7ca7b088296fa458824ee3c559362555
    00> [00:01:02.029,693] <dbg> bt_smp: bt_smp_encrypt_change: chan 0x200042b0 conn 0x20003e58 handle 1 encrypt 0x00 hci status 0x06
    00> Security failed: F4:E8:C7:5E:82:43 (public) level 1 err 2
    00> [00:01:02.119,689] <dbg> bt_smp: bt_smp_disconnected: chan 0x200042b0 cid 0x0006
    00> MTU exchange failed (err 14)[00:01:02.120,086] <dbg> bt_keys: bt_keys_find_addr: F4:E8:C7:5E:82:43 (public)
    00> [00:01:02.120,269] <dbg> bt_keys: bt_keys_find_addr: F4:E8:C7:5E:82:43 (public)
    00> Disconnected (reason 0x13)

Related