From 524ef5ee1dd506145bacecc7da889e90c479748c Mon Sep 17 00:00:00 2001 From: Arkadiusz Lichwa Date: Mon, 4 Jan 2016 14:27:14 +0100 Subject: [PATCH] Bluetooth: Refactor type of keys as flags Converts bt_keys type to atomic_t flags to enable consistent and compact key type management. Change-Id: Ie384168da6d5d0d1b305a33988ce7689ba3a4c6a Signed-off-by: Arkadiusz Lichwa --- net/bluetooth/conn.c | 6 ++++-- net/bluetooth/hci_core.c | 3 ++- net/bluetooth/keys.h | 3 +-- net/bluetooth/smp.c | 31 +++++++++++++++---------------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/net/bluetooth/conn.c b/net/bluetooth/conn.c index 630e49c5c87..a2ce4af53e7 100644 --- a/net/bluetooth/conn.c +++ b/net/bluetooth/conn.c @@ -179,12 +179,14 @@ static int start_security(struct bt_conn *conn) } if (conn->required_sec_level > BT_SECURITY_MEDIUM && - conn->keys->type != BT_KEYS_AUTHENTICATED) { + !atomic_test_bit(&conn->keys->flags, + BT_KEYS_AUTHENTICATED)) { return bt_smp_send_pairing_req(conn); } if (conn->required_sec_level > BT_SECURITY_HIGH && - conn->keys->type != BT_KEYS_AUTHENTICATED && + !atomic_test_bit(&conn->keys->flags, + BT_KEYS_AUTHENTICATED) && !(conn->keys->keys & BT_KEYS_LTK_P256)) { return bt_smp_send_pairing_req(conn); } diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 9fec6073f5e..50e549ca945 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -817,7 +817,8 @@ static void update_sec_level(struct bt_conn *conn) return; } - if (conn->keys && conn->keys->type == BT_KEYS_AUTHENTICATED) { + if (conn->keys && atomic_test_bit(&conn->keys->flags, + BT_KEYS_AUTHENTICATED)) { if (conn->keys->keys & BT_KEYS_LTK_P256) { conn->sec_level = BT_SECURITY_FIPS; } else { diff --git a/net/bluetooth/keys.h b/net/bluetooth/keys.h index 6d57a683f4d..6ef3949e658 100644 --- a/net/bluetooth/keys.h +++ b/net/bluetooth/keys.h @@ -31,7 +31,6 @@ enum { }; enum { - BT_KEYS_UNAUTHENTICATED, BT_KEYS_AUTHENTICATED, }; @@ -54,7 +53,7 @@ struct bt_csrk { struct bt_keys { bt_addr_le_t addr; int keys; - uint8_t type; + atomic_t flags; uint8_t enc_size; #if !defined(CONFIG_BLUETOOTH_SMP_SC_ONLY) diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index 5ff4ec93fc0..157d0c5642b 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -1100,7 +1100,7 @@ static uint8_t legacy_request_tk(struct bt_smp *smp) * keys with unauthenticated ones. */ keys = bt_keys_find_addr(&conn->le.dst); - if (keys && keys->type == BT_KEYS_AUTHENTICATED && + if (keys && atomic_test_bit(&keys->flags, BT_KEYS_AUTHENTICATED) && smp->method == JUST_WORKS) { BT_ERR("JustWorks failed, authenticated keys present"); return BT_SMP_ERR_UNSPECIFIED; @@ -1752,19 +1752,6 @@ static uint8_t smp_pairing_confirm(struct bt_smp *smp, struct net_buf *buf) return 0; } -static uint8_t get_keys_type(uint8_t method) -{ - switch (method) { - case PASSKEY_DISPLAY: - case PASSKEY_INPUT: - case PASSKEY_CONFIRM: - return BT_KEYS_AUTHENTICATED; - case JUST_WORKS: - default: - return BT_KEYS_UNAUTHENTICATED; - } -} - static uint8_t sc_smp_send_dhkey_check(struct bt_smp *smp, const uint8_t *e) { struct bt_smp_dhkey_check *req; @@ -2271,7 +2258,7 @@ static uint8_t smp_security_request(struct bt_smp *smp, struct net_buf *buf) /* if MITM required key must be authenticated */ if ((auth & BT_SMP_AUTH_MITM) && - conn->keys->type != BT_KEYS_AUTHENTICATED) { + !atomic_test_bit(&conn->keys->flags, BT_KEYS_AUTHENTICATED)) { if (get_io_capa() != BT_SMP_IO_NO_INPUT_OUTPUT) { BT_INFO("New auth requirements: 0x%x, repairing", auth); @@ -3318,7 +3305,19 @@ void bt_smp_update_keys(struct bt_conn *conn) * it is important to store it since type is used to determine * security level upon encryption */ - conn->keys->type = get_keys_type(smp->method); + switch (smp->method) { + case PASSKEY_DISPLAY: + case PASSKEY_INPUT: + case PASSKEY_CONFIRM: + atomic_set_bit(&conn->keys->flags, BT_KEYS_AUTHENTICATED); + break; + case JUST_WORKS: + default: + /* unauthenticated key, clear it */ + atomic_clear_bit(&conn->keys->flags, BT_KEYS_AUTHENTICATED); + break; + } + conn->keys->enc_size = get_encryption_key_size(smp); /*