diff --git a/subsys/bluetooth/host/classic/br.c b/subsys/bluetooth/host/classic/br.c index 23a43a01959b3..4e9902747381a 100644 --- a/subsys/bluetooth/host/classic/br.c +++ b/subsys/bluetooth/host/classic/br.c @@ -143,6 +143,10 @@ static bool br_sufficient_key_size(struct bt_conn *conn) key_size = rp->key_size; net_buf_unref(rsp); + if (conn->br.link_key) { + conn->br.link_key->enc_key_size = key_size; + } + LOG_DBG("Encryption key size is %u", key_size); if (conn->sec_level == BT_SECURITY_L4) { diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index 6702073ce69f1..cd58e5ba8a7f6 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -2493,35 +2493,11 @@ uint8_t bt_conn_enc_key_size(const struct bt_conn *conn) return 0; } - if (IS_ENABLED(CONFIG_BT_CLASSIC) && - conn->type == BT_CONN_TYPE_BR) { - struct bt_hci_cp_read_encryption_key_size *cp; - struct bt_hci_rp_read_encryption_key_size *rp; - struct net_buf *buf; - struct net_buf *rsp; - uint8_t key_size; - - buf = bt_hci_cmd_alloc(K_FOREVER); - if (!buf) { - return 0; - } - - cp = net_buf_add(buf, sizeof(*cp)); - cp->handle = sys_cpu_to_le16(conn->handle); - - if (bt_hci_cmd_send_sync(BT_HCI_OP_READ_ENCRYPTION_KEY_SIZE, - buf, &rsp)) { - return 0; - } - - rp = (void *)rsp->data; - - key_size = rp->status ? 0 : rp->key_size; - - net_buf_unref(rsp); - - return key_size; +#if defined(CONFIG_BT_CLASSIC) + if (conn->type == BT_CONN_TYPE_BR) { + return conn->br.link_key ? conn->br.link_key->enc_key_size : 0; } +#endif /* CONFIG_BT_CLASSIC */ if (IS_ENABLED(CONFIG_BT_SMP)) { return conn->le.keys ? conn->le.keys->enc_size : 0; diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 274c202d800a3..dac35e7bc99e6 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -1077,9 +1077,16 @@ static void hci_disconn_complete(struct net_buf *buf) * If only for one connection session bond was set, clear keys * database row for this connection. */ - if (conn->type == BT_CONN_TYPE_BR && conn->br.link_key != NULL && - atomic_test_and_clear_bit(conn->flags, BT_CONN_BR_NOBOND)) { - bt_keys_link_key_clear(conn->br.link_key); + if (conn->type == BT_CONN_TYPE_BR && conn->br.link_key != NULL) { + /* + * If the connection link is paired but not bond, remove + * the link key upon disconnection. + */ + if (atomic_test_and_clear_bit(conn->flags, BT_CONN_BR_NOBOND)) { + bt_keys_link_key_clear(conn->br.link_key); + } + + conn->br.link_key->enc_key_size = 0; } #endif bt_conn_unref(conn); diff --git a/subsys/bluetooth/host/keys.h b/subsys/bluetooth/host/keys.h index b53635ce2c566..ab83aff8ebf07 100644 --- a/subsys/bluetooth/host/keys.h +++ b/subsys/bluetooth/host/keys.h @@ -217,6 +217,7 @@ enum { struct bt_keys_link_key { bt_addr_t addr; + uint8_t enc_key_size; uint8_t storage_start[0] __aligned(sizeof(void *)); uint8_t flags; uint8_t val[16];