Bluetooth: New mgmt messages for SMP Keys
Subsystems (2)
v2 cover Bluetooth: New mgmt messages for SMP Keys
Hi,
Changes from the last version:
- Add support for removing the SMP Keys using mgmt Remove Key command;
- Disconnect the link if the found LTK isn't accepted to encrypt the link;
- We still have no support for any key generation method that would give us MITM protection, so we shouldn't say that we require it.
- Changed the defines of the MGMT events, there was a conflict with the blocked device events.
Cheers,
--
Vinicius Costa Gomes (13):
Bluetooth: Fix sending wrong authentication requirements
Bluetooth: Use the LTK after receiving a LE Security Request Revert "Bluetooth: Add support for communicating keys with userspace"
Bluetooth: Add structures for the new SMP messages
Bluetooth: Add support for cleaning the SMP key list
Bluetooth: Add handlers for the new mgmt messages
Bluetooth: Rename smp_key_size to enc_size
Bluetooth: Use the smp_keys list for accessing SMP keys
Bluetooth: Fix not setting a pending security level
Bluetooth: Fix setting the connection sec_level when encryption fails
Bluetooth: Remove support for other SMP keys than the LTK
Bluetooth: mgmt: Add support for removing SMP keys
Bluetooth: Disconnect the link if Encryption on LE links fails
include/net/bluetooth/hci.h | 1 + include/net/bluetooth/hci_core.h | 39 ++++++--- include/net/bluetooth/mgmt.h | 26 ++++++- net/bluetooth/hci_core.c | 104 ++++++++++++++++-------- net/bluetooth/hci_event.c | 13 ++- net/bluetooth/l2cap_core.c | 5 +- net/bluetooth/mgmt.c | 165 +++++++++++++++++++++++++++---------- net/bluetooth/smp.c | 97 +++++++++++----------- 8 files changed, 298 insertions(+), 152 deletions(-)
--
1.7.6
PATCH v2 1/13 Bluetooth: Fix sending wrong authentication requirements
From: Vinicius Costa Gomes <[email protected]>
Until we support any pairing method (Passkey Entry, OOB) that gives MITM protection we shouldn't send that we have MITM protection.
--- net/bluetooth/smp.c | 19 ++----------------- 1 files changed, 2 insertions(+), 17 deletions(-)
net/bluetooth/smp.c +2 -17
@@ -204,18 +204,6 @@ static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) msecs_to_jiffies(SMP_TIMEOUT)); } -static __u8 seclevel_to_authreq (__u8 level) -{ -switch (level) { -case BT_SECURITY_HIGH : -/* Right now we don't support bonding */ -return SMP_AUTH_MITM ; - -default : -return SMP_AUTH_NONE ; - } -} - static void build_pairing_cmd(struct l2cap_conn *conn, struct smp_cmd_pairing *req, struct smp_cmd_pairing *rsp,
@@ -556,7 +544,6 @@ int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level) {struct hci_conn *hcon = conn->hcon;struct smp_chan *smp = conn->smp_chan; -__u8 authreq;BT_DBG ("conn %p hcon %p level 0x%2.2x" , conn, hcon, sec_level);
@@ -594,19 +581,17 @@ int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level) smp =smp_chan_create (conn); - authreq =seclevel_to_authreq (sec_level); -if (hcon->link_mode &HCI_LM_MASTER ) {struct smp_cmd_pairing cp; -build_pairing_cmd (conn, &cp,NULL , authreq); +build_pairing_cmd (conn, &cp,NULL ,SMP_AUTH_NONE ); smp->preq[0 ] =SMP_CMD_PAIRING_REQ ;memcpy (&smp->preq[1 ], &cp,sizeof (cp));smp_send_cmd (conn,SMP_CMD_PAIRING_REQ ,sizeof (cp), &cp); }else {struct smp_cmd_security_req cp; - cp.auth_req = authreq; + cp.auth_req =SMP_AUTH_NONE ;smp_send_cmd (conn,SMP_CMD_SECURITY_REQ ,sizeof (cp), &cp); }
Signature · 1 line
1.7.6
PATCH v2 2/13 Bluetooth: Use the LTK after receiving a LE Security Request
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
When receiving a security request from the remote device we should find if there is already a LTK associated with the remote device, if found we should use it to encrypt the link.
--- net/bluetooth/smp.c | 46 ++++++++++++++++++++++++++++------------------ 1 files changed, 28 insertions(+), 18 deletions(-)
net/bluetooth/smp.c +28 -18
@@ -513,6 +513,29 @@ static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)return 0 ; } +static u8 smp_ltk_encrypt (struct l2cap_conn *conn) +{ +struct link_key *key; +struct key_master_id *master; +struct hci_conn *hcon = conn->hcon; + + key =hci_find_link_key_type (hcon->hdev, conn->dst, +HCI_LK_SMP_LTK ); +if (!key) +return 0 ; + +if (test_and_set_bit (HCI_CONN_ENCRYPT_PEND , + &hcon->pend)) +return 1 ; + + master = (void *) key->data; +hci_le_start_enc (hcon, master->ediv, master->rand, + key->val); + hcon->enc_key_size = key->pin_len; + +return 1 ; + +}static u8 smp_cmd_security_req (struct l2cap_conn *conn,struct sk_buff *skb) {struct smp_cmd_security_req *rp = (void *) skb->data;
@@ -522,6 +545,9 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)BT_DBG ("conn %p" , conn); +if (smp_ltk_encrypt (conn)) +return 0 ; +if (test_and_set_bit (HCI_CONN_LE_SMP_PEND , &hcon->pend))return 0 ;
@@ -556,25 +582,9 @@ int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)if (hcon->sec_level >= sec_level)return 1 ; -if (hcon->link_mode &HCI_LM_MASTER ) { -struct link_key *key; - - key =hci_find_link_key_type (hcon->hdev, conn->dst, -HCI_LK_SMP_LTK ); -if (key) { -struct key_master_id *master = (void *) key->data; - -if (test_and_set_bit (HCI_CONN_ENCRYPT_PEND , - &hcon->pend)) - goto done; - -hci_le_start_enc (hcon, master->ediv, master->rand, - key->val); - hcon->enc_key_size = key->pin_len; - +if (hcon->link_mode &HCI_LM_MASTER ) +if (smp_ltk_encrypt (conn)) goto done; - } - }if (test_and_set_bit (HCI_CONN_LE_SMP_PEND , &hcon->pend))return 0 ;
Signature · 1 line
1.7.6
PATCH v2 3/13 Revert "Bluetooth: Add support for communicating keys with userspace"
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
This reverts commit 5a0a8b49746771fba79866fb9185ffa051a6a183.
If we use separate messages and list for SMP specific keys we can simplify the code.
Conflicts:
net/bluetooth/mgmt.c
--- net/bluetooth/mgmt.c | 60 ++++++++++++------------------------------------- 1 files changed, 15 insertions(+), 45 deletions(-)
net/bluetooth/mgmt.c +15 -45
@@ -908,7 +908,7 @@ static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len)struct hci_dev *hdev;struct mgmt_cp_load_keys *cp;u16 key_count, expected_len; -int i, err; +int i; cp = (void *) data;
@@ -918,9 +918,9 @@ static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len) key_count =get_unaligned_le16 (&cp->key_count); expected_len =sizeof (*cp) + key_count *sizeof (struct mgmt_key_info ); -if (expected_len > len) { -BT_ERR ("load_keys: expected at least %u bytes, got %u bytes" , - expected_len, len); +if (expected_len != len) { +BT_ERR ("load_keys: expected %u bytes, got %u bytes" , + len, expected_len);return -EINVAL ; }
@@ -942,36 +942,17 @@ static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len)else clear_bit (HCI_DEBUG_KEYS , &hdev->flags); - len -=sizeof (*cp); - i =0 ; - -while (i < len) { -struct mgmt_key_info *key = (void *) cp->keys + i; - - i +=sizeof (*key) + key->dlen; - -if (key->type ==HCI_LK_SMP_LTK ) { -struct key_master_id *id = (void *) key->data; - -if (key->dlen !=sizeof (struct key_master_id )) -continue ; - -hci_add_ltk (hdev,0 , &key->bdaddr, key->pin_len, - id->ediv, id->rand, key->val); - -continue ; - } +for (i =0 ; i < key_count; i++) { +struct mgmt_key_info *key = &cp->keys[i];hci_add_link_key (hdev,NULL ,0 , &key->bdaddr, key->val, key->type, key->pin_len); } - err =cmd_complete (sk, index,MGMT_OP_LOAD_KEYS ,NULL ,0 ); -hci_dev_unlock_bh (hdev);hci_dev_put (hdev); -return err; +return 0 ; }static int remove_key (struct sock *sk,u16 index,unsigned char *data,u16 len)
@@ -2012,28 +1993,17 @@ int mgmt_connectable(u16 index, u8 connectable)int mgmt_new_key (u16 index,struct link_key *key,u8 persistent) { -struct mgmt_ev_new_key *ev; -int err, total; - - total =sizeof (struct mgmt_ev_new_key ) + key->dlen; - ev =kzalloc (total,GFP_ATOMIC ); -if (!ev) -return -ENOMEM ; - -bacpy (&ev->key.bdaddr, &key->bdaddr); - ev->key.type = key->type; -memcpy (ev->key.val, key->val,16 ); - ev->key.pin_len = key->pin_len; - ev->key.dlen = key->dlen; - ev->store_hint = persistent; - -memcpy (ev->key.data, key->data, key->dlen); +struct mgmt_ev_new_key ev; - err =mgmt_event (MGMT_EV_NEW_KEY , index, ev, total,NULL ); +memset (&ev,0 ,sizeof (ev)); -kfree (ev); + ev.store_hint = persistent; +bacpy (&ev.key.bdaddr, &key->bdaddr); + ev.key.type = key->type; +memcpy (ev.key.val, key->val,16 ); + ev.key.pin_len = key->pin_len; -return err; +return mgmt_event (MGMT_EV_NEW_KEY , index, &ev,sizeof (ev),NULL ); }int mgmt_connected (u16 index,bdaddr_t *bdaddr,u8 link_type)
Signature · 1 line
1.7.6
PATCH v2 4/13 Bluetooth: Add structures for the new SMP messages
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
--- include/net/bluetooth/hci_core.h | 24 ++++++++++++++++++++++++ include/net/bluetooth/mgmt.h | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 0 deletions(-)
include/net/bluetooth/hci_core.h +24 -0
@@ -89,6 +89,28 @@ struct link_key_data {u8 data[0 ]; } __packed; +struct smp_ltk_info { +u8 enc_size; +u16 ediv; +u8 rand[8 ]; +} __packed; + +struct smp_irsk_info { +u8 addr_type; +} __packed; + +struct smp_link_key { +struct list_head list; +bdaddr_t bdaddr; +u8 type; +u8 pin_len; +u8 val[16 ]; +union { +struct smp_ltk_info ltk; +struct smp_irsk_info irsk; + }; +} __packed; +struct link_key {struct list_head list;bdaddr_t bdaddr;
@@ -203,6 +225,8 @@ struct hci_dev {struct list_head link_keys; +struct list_head smp_keys; +struct list_head remote_oob_data;struct list_head adv_entries;
include/net/bluetooth/mgmt.h +24 -0
@@ -211,6 +211,24 @@ struct mgmt_cp_unblock_device {bdaddr_t bdaddr; } __packed; +struct mgmt_smp_key_info { +bdaddr_t bdaddr; +u8 type; +u8 pin_len; +u8 val[16 ]; +union { +struct smp_ltk_info ltk; +struct smp_irsk_info irsk; + }; +} __packed; + +#define MGMT_OP_LOAD_SMP_KEYS 0x001F +struct mgmt_cp_load_smp_keys { +__u8 debug_keys; +__u16 key_count; +struct mgmt_smp_key_info keys[0 ]; +} __packed; +#define MGMT_EV_CMD_COMPLETE 0x0001struct mgmt_ev_cmd_complete {__le16 opcode;
@@ -312,3 +330,9 @@ struct mgmt_ev_device_blocked {struct mgmt_ev_device_unblocked {bdaddr_t bdaddr; } __packed; + +#define MGMT_EV_NEW_SMP_KEY 0x0017 +struct mgmt_ev_new_smp_key { +__u8 store_hint; +struct mgmt_smp_key_info key; +} __packed;
Signature · 1 line
1.7.6
PATCH v2 5/13 Bluetooth: Add support for cleaning the SMP key list
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
--- include/net/bluetooth/hci_core.h | 1 + net/bluetooth/hci_core.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 0 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
@@ -593,6 +593,7 @@ int hci_link_keys_clear(struct hci_dev *hdev);struct link_key *hci_find_link_key (struct hci_dev *hdev,bdaddr_t *bdaddr);int hci_add_link_key (struct hci_dev *hdev,struct hci_conn *conn,int new_key,bdaddr_t *bdaddr,u8 *val,u8 type,u8 pin_len); +int hci_smp_keys_clear (struct hci_dev *hdev);struct link_key *hci_find_ltk (struct hci_dev *hdev,__le16 ediv,u8 rand[8 ]);struct link_key *hci_find_link_key_type (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 type);
net/bluetooth/hci_core.c +14 -0
@@ -1005,6 +1005,18 @@ int hci_link_keys_clear(struct hci_dev *hdev)return 0 ; } +int hci_smp_keys_clear (struct hci_dev *hdev) +{ +struct smp_link_key *k, *tmp; + +list_for_each_entry_safe (k, tmp, &hdev->smp_keys, list) { +list_del (&k->list); +kfree (k); + } + +return 0 ; +} +struct link_key *hci_find_link_key (struct hci_dev *hdev,bdaddr_t *bdaddr) {struct list_head *p;
@@ -1486,6 +1498,7 @@ int hci_register_dev(struct hci_dev *hdev)INIT_LIST_HEAD (&hdev->uuids);INIT_LIST_HEAD (&hdev->link_keys); +INIT_LIST_HEAD (&hdev->smp_keys);INIT_LIST_HEAD (&hdev->remote_oob_data);
@@ -1573,6 +1586,7 @@ int hci_unregister_dev(struct hci_dev *hdev)hci_blacklist_clear (hdev);hci_uuids_clear (hdev);hci_link_keys_clear (hdev); +hci_smp_keys_clear (hdev);hci_remote_oob_data_clear (hdev);hci_adv_entries_clear (hdev);hci_dev_unlock_bh (hdev);
Signature · 1 line
1.7.6
PATCH v2 6/13 Bluetooth: Add handlers for the new mgmt messages
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
--- include/net/bluetooth/hci.h | 1 + include/net/bluetooth/hci_core.h | 1 + net/bluetooth/mgmt.c | 97 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 0 deletions(-)
include/net/bluetooth/hci.h +1 -0
@@ -260,6 +260,7 @@ enum {#define HCI_LK_AUTH_COMBINATION 0x05#define HCI_LK_CHANGED_COMBINATION 0x06/* The spec doesn't define types for SMP keys */ +#define HCI_LK_SMP_STK 0x80#define HCI_LK_SMP_LTK 0x81#define HCI_LK_SMP_IRK 0x82#define HCI_LK_SMP_CSRK 0x83
include/net/bluetooth/hci_core.h +1 -0
@@ -900,6 +900,7 @@ int mgmt_remote_name(u16 index, bdaddr_t *bdaddr, u8 *name);int mgmt_discovering (u16 index,u8 discovering);int mgmt_device_blocked (u16 index,bdaddr_t *bdaddr);int mgmt_device_unblocked (u16 index,bdaddr_t *bdaddr); +int mgmt_new_smp_key (u16 index,struct smp_link_key *key,u8 persistent);/* HCI info for socket */ #define hci_pi (sk) ((struct hci_pinfo *) sk)
net/bluetooth/mgmt.c +97 -0
112 lines not on this page · raw message
Signature · 1 line
1.7.6
PATCH v2 7/13 Bluetooth: Rename smp_key_size to enc_size
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
This makes it more clear that the encryption size (effective size of the key used for encrypting the link) is a different concept than the pin code lenght.
--- net/bluetooth/smp.c | 20 ++++++++++---------- 1 files changed, 10 insertions(+), 10 deletions(-)
net/bluetooth/smp.c +10 -10
@@ -41,7 +41,7 @@ struct smp_chan {u8 rrnd[16 ];/* SMP Pairing Random (remote) */ u8 pcnf[16 ];/* SMP Pairing Confirm */ u8 tk[16 ];/* SMP Temporary Key */ -u8 smp_key_size; +u8 enc_size;struct crypto_blkcipher *tfm;struct work_struct confirm;struct work_struct random;
@@ -243,7 +243,7 @@ static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) (max_key_size < SMP_MIN_ENC_KEY_SIZE))return SMP_ENC_KEY_SIZE ; - smp->smp_key_size = max_key_size; + smp->enc_size = max_key_size;return 0 ; }
@@ -337,8 +337,8 @@ static void random_work(struct work_struct *work)smp_s1 (tfm, smp->tk, smp->rrnd, smp->prnd, key);swap128 (key, stk); -memset (stk + smp->smp_key_size,0 , -SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size); +memset (stk + smp->enc_size,0 , +SMP_MAX_ENC_KEY_SIZE - smp->enc_size);if (test_and_set_bit (HCI_CONN_ENCRYPT_PEND , &hcon->pend)) { reason =SMP_UNSPECIFIED ;
@@ -346,7 +346,7 @@ static void random_work(struct work_struct *work) }hci_le_start_enc (hcon, ediv, rand, stk); - hcon->enc_key_size = smp->smp_key_size; + hcon->enc_key_size = smp->enc_size; }else {u8 stk[16 ], r[16 ], rand[8 ];__le16 ediv;
@@ -360,10 +360,10 @@ static void random_work(struct work_struct *work)smp_s1 (tfm, smp->tk, smp->prnd, smp->rrnd, key);swap128 (key, stk); -memset (stk + smp->smp_key_size,0 , -SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size); +memset (stk + smp->enc_size,0 , +SMP_MAX_ENC_KEY_SIZE - smp->enc_size); -hci_add_ltk (hcon->hdev,0 , conn->dst, smp->smp_key_size, +hci_add_ltk (hcon->hdev,0 , conn->dst, smp->enc_size, ediv, rand, stk); }
@@ -630,7 +630,7 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)skb_pull (skb,sizeof (*rp)); -hci_add_ltk (conn->hcon->hdev,1 , conn->src, smp->smp_key_size, +hci_add_ltk (conn->hcon->hdev,1 , conn->src, smp->enc_size, rp->ediv, rp->rand, smp->tk);smp_distribute_keys (conn,1 );
@@ -754,7 +754,7 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)smp_send_cmd (conn,SMP_CMD_ENCRYPT_INFO ,sizeof (enc), &enc); -hci_add_ltk (conn->hcon->hdev,1 , conn->dst, smp->smp_key_size, +hci_add_ltk (conn->hcon->hdev,1 , conn->dst, smp->enc_size, ediv, ident.rand, enc.ltk); ident.ediv =cpu_to_le16 (ediv);
Signature · 1 line
1.7.6
PATCH v2 8/13 Bluetooth: Use the smp_keys list for accessing SMP keys
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
Now that we have a separate key list for SMP, we may use it.
For now, there is only support for the Short Term Key (which should be used only once) and LTK (which should be sent to userspace for storage).
--- include/net/bluetooth/hci_core.h | 26 +++---------- include/net/bluetooth/mgmt.h | 2 - net/bluetooth/hci_core.c | 73 +++++++++++++++++++------------------ net/bluetooth/hci_event.c | 13 ++++--- net/bluetooth/mgmt.c | 4 ++- net/bluetooth/smp.c | 26 +++++++------ 6 files changed, 68 insertions(+), 76 deletions(-)
include/net/bluetooth/hci_core.h +6 -20
@@ -75,20 +75,6 @@ struct bt_uuid {u8 svc_hint; }; -struct key_master_id { -__le16 ediv; -u8 rand[8 ]; -} __packed; - -struct link_key_data { -bdaddr_t bdaddr; -u8 type; -u8 val[16 ]; -u8 pin_len; -u8 dlen; -u8 data[0 ]; -} __packed; -struct smp_ltk_info {u8 enc_size;u16 ediv;
@@ -117,8 +103,6 @@ struct link_key {u8 type;u8 val[16 ];u8 pin_len; -u8 dlen; -u8 data[0 ]; };struct oob_data {
@@ -594,11 +578,13 @@ struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);int hci_add_link_key (struct hci_dev *hdev,struct hci_conn *conn,int new_key,bdaddr_t *bdaddr,u8 *val,u8 type,u8 pin_len);int hci_smp_keys_clear (struct hci_dev *hdev); -struct link_key *hci_find_ltk (struct hci_dev *hdev,__le16 ediv,u8 rand[8 ]); -struct link_key *hci_find_link_key_type (struct hci_dev *hdev, +struct smp_link_key *hci_find_smp_enc_key (struct hci_dev *hdev,__le16 ediv, +u8 rand[8 ]); +struct smp_link_key *hci_find_smp_key_type (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 type); -int hci_add_ltk (struct hci_dev *hdev,int new_key,bdaddr_t *bdaddr, -u8 key_size,__le16 ediv,u8 rand[8 ],u8 ltk[16 ]); +int hci_add_smp_enc_key (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 new_key, +u8 type,u8 pin_len,u8 tk[16 ], +u8 enc_size,u16 ediv,u8 rand[8 ]);int hci_remove_link_key (struct hci_dev *hdev,bdaddr_t *bdaddr);int hci_remote_oob_data_clear (struct hci_dev *hdev);
include/net/bluetooth/mgmt.h +0 -2
@@ -101,8 +101,6 @@ struct mgmt_key_info {u8 type;u8 val[16 ];u8 pin_len; -u8 dlen; -u8 data[0 ]; } __packed;#define MGMT_OP_LOAD_KEYS 0x000D
net/bluetooth/hci_core.c +37 -36
118 lines not on this page · raw message
net/bluetooth/hci_event.c +8 -5
@@ -2860,7 +2860,7 @@ static inline void hci_le_ltk_request_evt(struct hci_dev *hdev,struct hci_cp_le_ltk_reply cp;struct hci_cp_le_ltk_neg_reply neg;struct hci_conn *conn; -struct link_key *ltk; +struct smp_link_key *tk;BT_DBG ("%s handle %d" , hdev->name,cpu_to_le16 (ev->handle));
@@ -2870,16 +2870,19 @@ static inline void hci_le_ltk_request_evt(struct hci_dev *hdev,if (conn ==NULL ) goto not_found; - ltk =hci_find_ltk (hdev, ev->ediv, ev->random); -if (ltk ==NULL ) + tk =hci_find_smp_enc_key (hdev, ev->ediv, ev->random); +if (tk ==NULL ) goto not_found; -memcpy (cp.ltk, ltk->val,sizeof (ltk->val)); +memcpy (cp.ltk, tk->val,sizeof (tk->val)); cp.handle =cpu_to_le16 (conn->handle); - conn->pin_length = ltk->pin_len; + conn->pin_length = tk->pin_len;hci_send_cmd (hdev,HCI_OP_LE_LTK_REPLY ,sizeof (cp), &cp); +if (tk->type ==HCI_LK_SMP_STK ) +kfree (tk); +hci_dev_unlock (hdev);return ;
net/bluetooth/mgmt.c +3 -1
@@ -1817,7 +1817,9 @@ static int load_smp_keys(struct sock *sk, u16 index, unsigned char *data,case HCI_LK_SMP_LTK : ltk = &key->ltk; -/* Add key to the smp list */ +hci_add_smp_enc_key (hdev, &key->bdaddr,0 , key->type, + key->pin_len, key->val, ltk->enc_size, + ltk->ediv, ltk->rand);break ;default :
net/bluetooth/smp.c +14 -12
@@ -363,8 +363,8 @@ static void random_work(struct work_struct *work)memset (stk + smp->enc_size,0 ,SMP_MAX_ENC_KEY_SIZE - smp->enc_size); -hci_add_ltk (hcon->hdev,0 , conn->dst, smp->enc_size, - ediv, rand, stk); +hci_add_smp_enc_key (hcon->hdev, conn->dst,0 ,HCI_LK_SMP_STK , +0 , stk, smp->enc_size, ediv, rand); }return ;
@@ -515,11 +515,11 @@ static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)static u8 smp_ltk_encrypt (struct l2cap_conn *conn) { -struct link_key *key; -struct key_master_id *master; +struct smp_link_key *key; +struct smp_ltk_info *info;struct hci_conn *hcon = conn->hcon; - key =hci_find_link_key_type (hcon->hdev, conn->dst, + key =hci_find_smp_key_type (hcon->hdev, conn->dst,HCI_LK_SMP_LTK );if (!key)return 0 ;
@@ -528,10 +528,10 @@ static u8 smp_ltk_encrypt(struct l2cap_conn *conn) &hcon->pend))return 1 ; - master = (void *) key->data; -hci_le_start_enc (hcon, master->ediv, master->rand, + info = &key->ltk; +hci_le_start_enc (hcon, info->ediv, info->rand, key->val); - hcon->enc_key_size = key->pin_len; + hcon->enc_key_size = info->enc_size;return 1 ;
@@ -630,8 +630,9 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)skb_pull (skb,sizeof (*rp)); -hci_add_ltk (conn->hcon->hdev,1 , conn->src, smp->enc_size, - rp->ediv, rp->rand, smp->tk); +hci_add_smp_enc_key (conn->hcon->hdev, conn->dst,1 ,HCI_LK_SMP_LTK , + conn->hcon->pin_length, smp->tk, + smp->enc_size, rp->ediv, rp->rand);smp_distribute_keys (conn,1 );
@@ -754,8 +755,9 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)smp_send_cmd (conn,SMP_CMD_ENCRYPT_INFO ,sizeof (enc), &enc); -hci_add_ltk (conn->hcon->hdev,1 , conn->dst, smp->enc_size, - ediv, ident.rand, enc.ltk); +hci_add_smp_enc_key (conn->hcon->hdev, conn->src,1 , +HCI_LK_SMP_LTK , conn->hcon->pin_length, + enc.ltk, smp->enc_size, ediv, ident.rand); ident.ediv =cpu_to_le16 (ediv);
Signature · 1 line
1.7.6
PATCH v2 9/13 Bluetooth: Fix not setting a pending security level
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
For slave initiated security, we should set a default security level, for now BT_SECURITY_MEDIUM.
--- net/bluetooth/smp.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
net/bluetooth/smp.c +2 -0
@@ -545,6 +545,8 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)BT_DBG ("conn %p" , conn); + hcon->pending_sec_level =BT_SECURITY_MEDIUM ; +if (smp_ltk_encrypt (conn))return 0 ;
Signature · 1 line
1.7.6
PATCH v2 10/13 Bluetooth: Fix setting the connection sec_level when encryption fails
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
If the encryption changed event indicates that happened an error we should not set the security level of the connection.
--- include/net/bluetooth/hci_core.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
include/net/bluetooth/hci_core.h +1 -1
@@ -797,7 +797,7 @@ static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,if (conn->sec_level ==BT_SECURITY_SDP ) conn->sec_level =BT_SECURITY_LOW ; -if (conn->pending_sec_level > conn->sec_level) +if (!status && conn->pending_sec_level > conn->sec_level) conn->sec_level = conn->pending_sec_level;hci_proto_encrypt_cfm (conn, status, encrypt);
Signature · 1 line
1.7.6
PATCH v2 11/13 Bluetooth: Remove support for other SMP keys than the LTK
In reply to Vinicius Costa GomesFrom: Vinicius Costa Gomes <[email protected]>
For now, only the LTK is properly supported. We are able to receive and generate the other types of keys, but we are not able to use them. So it's better not request them to be distributed.
--- net/bluetooth/smp.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
net/bluetooth/smp.c +1 -1
@@ -213,7 +213,7 @@ static void build_pairing_cmd(struct l2cap_conn *conn, dist_keys =0 ;if (test_bit (HCI_PAIRABLE , &conn->hcon->hdev->flags)) { - dist_keys =SMP_DIST_ENC_KEY |SMP_DIST_ID_KEY |SMP_DIST_SIGN ; + dist_keys =SMP_DIST_ENC_KEY ; authreq |=SMP_AUTH_BONDING ; }
Signature · 1 line
1.7.6
PATCH v2 12/13 Bluetooth: mgmt: Add support for removing SMP keys
In reply to Vinicius Costa GomesThe mgmt command to remove keys is used for removing associations with a remote device, so it makes sense to also use the same command to remove SMP keys.
--- include/net/bluetooth/hci_core.h | 1 + net/bluetooth/hci_core.c | 17 +++++++++++++++++ net/bluetooth/mgmt.c | 6 ++++++ 3 files changed, 24 insertions(+), 0 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
@@ -586,6 +586,7 @@ int hci_add_smp_enc_key(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 new_key, u8 type, u8 pin_len, u8 tk[16], u8 enc_size, u16 ediv, u8 rand[8]);int hci_remove_link_key (struct hci_dev *hdev,bdaddr_t *bdaddr); +int hci_remove_smp_keys (struct hci_dev *hdev,bdaddr_t *bdaddr);int hci_remote_oob_data_clear (struct hci_dev *hdev); struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
net/bluetooth/hci_core.c +17 -0
@@ -1215,6 +1215,23 @@ int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)return 0 ; } +int hci_remove_smp_keys (struct hci_dev *hdev,bdaddr_t *bdaddr) +{ +struct smp_link_key *k, *tmp; + +list_for_each_entry_safe (k, tmp, &hdev->smp_keys, list) { +if (bacmp (bdaddr, &k->bdaddr)) +continue ; + +BT_DBG ("%s removing %s" , hdev->name,batostr (bdaddr)); + +list_del (&k->list); +kfree (k); + } + +return 0 ; +} +/* HCI command timer function */ static void hci_cmd_timer (unsigned long arg) {
net/bluetooth/mgmt.c +6 -0
@@ -973,6 +973,12 @@ static int remove_key(struct sock *sk, u16 index, unsigned char *data, u16 len)hci_dev_lock_bh (hdev); + err =hci_remove_smp_keys (hdev, &cp->bdaddr); +if (err <0 ) { + err =cmd_status (sk, index,MGMT_OP_REMOVE_KEY , -err); + goto unlock; + } + err =hci_remove_link_key (hdev, &cp->bdaddr);if (err <0 ) { err =cmd_status (sk, index,MGMT_OP_REMOVE_KEY , -err);
Signature · 1 line
1.7.6
PATCH v2 13/13 Bluetooth: Disconnect the link if Encryption on LE links fails
In reply to Vinicius Costa GomesWith the last commit this solves a security issue, in the case that a device spoofs the address of an already bonded device, if we try encryption, we will receive an error that there's no agreed key between those devices.
The solution is to disconnect the link as soon as the error is detected and report the error. So the user can remove the ofending key and start the pairing process from the begining.
--- net/bluetooth/l2cap_core.c | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
net/bluetooth/l2cap_core.c +3 -2
@@ -4034,10 +4034,11 @@ static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)BT_DBG ("conn %p" , conn); -if (hcon->type ==LE_LINK ) { +if (hcon->type ==LE_LINK && !status) {smp_distribute_keys (conn,0 );del_timer (&conn->security_timer); - } + }else if (hcon->type ==LE_LINK ) +l2cap_conn_del (hcon,bt_to_errno (status));read_lock (&conn->chan_lock);
Signature · 1 line
1.7.6
Re: [PATCH v2 10/13] Bluetooth: Fix setting the connection sec_level when encryption fails
In reply to Vinicius Costa GomesHi Vinicius,
Quoted 15 lines · Vinicius Costa Gomes
From: Vinicius Costa Gomes <[email protected]>
If the encryption changed event indicates that happened an error we should not set the security level of the connection.
Signed-off-by Vinicius Costa Gomes <[email protected]>--- include/net/bluetooth/hci_core.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)include/net/bluetooth/hci_core.h +0
-0
include/net/bluetooth/hci_core.h static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status, · line 801
Quoted 6 lines
include/net/bluetooth/hci_core.h +1
-1@@ -797,7 +797,7 @@ static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,if (conn->sec_level ==BT_SECURITY_SDP ) conn->sec_level =BT_SECURITY_LOW ; -if (conn->pending_sec_level > conn->sec_level) +if (!status && conn->pending_sec_level > conn->sec_level) conn->sec_level = conn->pending_sec_level;
I think this should be moved out of hci_encrypt_cfm and directly into hci_encrypt_change_evt. Currently, the only place this assignment is valid is on receipt of a successful Encryption Change Event (Although, where is the Encryption Key Refresh Complete Event handling? Or does the current SMP implementation not allow sec_level elevation?)
Also, I believe that this assignment should only occur on LE links (which should be specifically tested for). For example, what if an ACL link authenticates at BT_SECURITY_MEDIUM level successfully but then later a specific service attempts to authenticates at BT_SECURITY_HIGH level but fails. The pending_sec_level will still be set to BT_SECURITY_HIGH so SET_CONN_ENCRYPT just needs to be resent and the ACL link will be promoted to BT_SECURITY_HIGH.
Maybe instead of testing for an LE link, a new pend bit should be introduced to indicate that this link is specifically expecting to set the link sec_level as a result of sending the LE_START_ENCRYPTION cmd?
Regards,
Peter Hurley
Re: [PATCH v2 10/13] Bluetooth: Fix setting the connection sec_level when encryption fails
Hi Peter,
Quoted 21 lines · Peter Hurley
Hi Vinicius,
Quoted 18 lines · Vinicius Costa Gomes
From: Vinicius Costa Gomes <[email protected]>
If the encryption changed event indicates that happened an error we should not set the security level of the connection.
Signed-off-by Vinicius Costa Gomes <[email protected]>--- include/net/bluetooth/hci_core.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)include/net/bluetooth/hci_core.h +0
-0@@ -797,7 +797,7 @@ static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,if (conn->sec_level ==BT_SECURITY_SDP ) conn->sec_level =BT_SECURITY_LOW ;
Quoted 9 lines
include/net/bluetooth/hci_core.h static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status, · line 801
Quoted 3 lines
include/net/bluetooth/hci_core.h +1
-1-if (conn->pending_sec_level > conn->sec_level) +if (!status && conn->pending_sec_level > conn->sec_level) conn->sec_level = conn->pending_sec_level;I think this should be moved out of
hci_encrypt_cfmand directly intohci_encrypt_change_evt. Currently, the only place this assignment is valid is on receipt of a successful Encryption Change Event (Although, where is the Encryption Key Refresh Complete Event handling? Or does the current SMP implementation not allowsec_levelelevation?)
Security elevation in SMP isn't supported right now, as the only supported security level is MEDIUM, because we don't support anything that would offer MITM protection, that we are using as a condition for the HIGH security level.
But your point, that this isn't the best place for this assignment is good. See below.
Quoted 8 lines
Also, I believe that this assignment should only occur on LE links (which should be specifically tested for). For example, what if an ACL link authenticates at
BT_SECURITY_MEDIUMlevel successfully but then later a specific service attempts to authenticates atBT_SECURITY_HIGHlevel but fails. Thepending_sec_levelwill still be set toBT_SECURITY_HIGHsoSET_CONN_ENCRYPTjust needs to be resent and the ACL link will be promoted toBT_SECURITY_HIGH.
Again, good point. But that's an problem that already existed before my patch. What my patch tries to solve is a much simpler issue: in case of failure of a single security procedure, the security level of the link shouldn't be promoted.
Quoted 4 lines
Maybe instead of testing for an LE link, a new pend bit should be introduced to indicate that this link is specifically expecting to set the link
sec_levelas a result of sending theLE_START_ENCRYPTIONcmd?
I like it.
Quoted 3 lines
Regards,
Peter Hurley
Cheers,
Signature · 1 line
Vinicius
Re: [PATCH v2 01/13] Bluetooth: Fix sending wrong authentication requirements
In reply to Vinicius Costa GomesHi Vinicius,
Quoted 9 lines · Vinicius Costa Gomes
From: Vinicius Costa Gomes <[email protected]>
Until we support any pairing method (Passkey Entry, OOB) that gives MITM protection we shouldn't send that we have MITM protection.
Signed-off-by Vinicius Costa Gomes <[email protected]>--- net/bluetooth/smp.c | 19 ++----------------- 1 files changed, 2 insertions(+), 17 deletions(-)
Patch has been applied, thanks.
Gustavo
Gustavo Padovan
AppliedRe: [PATCH v2 02/13] Bluetooth: Use the LTK after receiving a LE Security Request
In reply to Vinicius Costa GomesQuoted 10 lines · Vinicius Costa Gomes
From: Vinicius Costa Gomes <[email protected]>
When receiving a security request from the remote device we should find if there is already a LTK associated with the remote device, if found we should use it to encrypt the link.
Signed-off-by Vinicius Costa Gomes <[email protected]>--- net/bluetooth/smp.c | 46 ++++++++++++++++++++++++++++------------------ 1 files changed, 28 insertions(+), 18 deletions(-)
Applied, thanks.
Gustavo
Gustavo Padovan
AppliedRe: [PATCH v2 03/13] Revert "Bluetooth: Add support for communicating keys with userspace"
In reply to Vinicius Costa GomesQuoted 7 lines · Vinicius Costa Gomes
From: Vinicius Costa Gomes <[email protected]>
This reverts commit
5a0a8b49746771fba79866fb9185ffa051a6a183.If we use separate messages and list for SMP specific keys we can simplify the code.
Quoted 8 lines
Conflicts:
net/bluetooth/mgmt.cSigned-off-by Vinicius Costa Gomes <[email protected]>--- net/bluetooth/mgmt.c | 60 ++++++++++++------------------------------------- 1 files changed, 15 insertions(+), 45 deletions(-)
Applied, thanks.
Gustavo
Gustavo Padovan
AppliedRe: [PATCH v2 05/13] Bluetooth: Add support for cleaning the SMP key list
In reply to Vinicius Costa GomesQuoted 7 lines · Vinicius Costa Gomes
From: Vinicius Costa Gomes <[email protected]>
Signed-off-by Vinicius Costa Gomes <[email protected]>--- include/net/bluetooth/hci_core.h | 1 + net/bluetooth/hci_core.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 0 deletions(-)
Applied, thanks.
Gustavo
Gustavo Padovan
AppliedRe: [PATCH v2 09/13] Bluetooth: Fix not setting a pending security level
In reply to Vinicius Costa GomesQuoted 9 lines · Vinicius Costa Gomes
From: Vinicius Costa Gomes <[email protected]>
For slave initiated security, we should set a default security level, for now
BT_SECURITY_MEDIUM.Signed-off-by Vinicius Costa Gomes <[email protected]>--- net/bluetooth/smp.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
Applied, thanks.
Gustavo
Gustavo Padovan
AppliedRe: [PATCH v2 11/13] Bluetooth: Remove support for other SMP keys than the LTK
In reply to Vinicius Costa GomesQuoted 10 lines · Vinicius Costa Gomes
From: Vinicius Costa Gomes <[email protected]>
For now, only the LTK is properly supported. We are able to receive and generate the other types of keys, but we are not able to use them. So it's better not request them to be distributed.
Signed-off-by Vinicius Costa Gomes <[email protected]>--- net/bluetooth/smp.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
Applied, thanks.
Gustavo