2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
23 #include <linux/crypto.h>
24 #include <linux/scatterlist.h>
25 #include <crypto/algapi.h>
26 #include <crypto/b128ops.h>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
30 #include <net/bluetooth/l2cap.h>
31 #include <net/bluetooth/mgmt.h>
35 #define SMP_TIMEOUT msecs_to_jiffies(30000)
37 #define AUTH_REQ_MASK 0x07
39 #define SMP_FLAG_TK_VALID 1
40 #define SMP_FLAG_CFM_PENDING 2
41 #define SMP_FLAG_MITM_AUTH 3
42 #define SMP_FLAG_COMPLETE 4
43 #define SMP_FLAG_INITIATOR 5
46 struct l2cap_conn
*conn
;
47 u8 preq
[7]; /* SMP Pairing Request */
48 u8 prsp
[7]; /* SMP Pairing Response */
49 u8 prnd
[16]; /* SMP Pairing Random (local) */
50 u8 rrnd
[16]; /* SMP Pairing Random (remote) */
51 u8 pcnf
[16]; /* SMP Pairing Confirm */
52 u8 tk
[16]; /* SMP Temporary Key */
58 struct smp_csrk
*csrk
;
59 struct smp_csrk
*slave_csrk
;
61 struct smp_ltk
*slave_ltk
;
62 struct smp_irk
*remote_irk
;
66 static inline void swap128(const u8 src
[16], u8 dst
[16])
69 for (i
= 0; i
< 16; i
++)
73 static inline void swap56(const u8 src
[7], u8 dst
[7])
76 for (i
= 0; i
< 7; i
++)
80 static int smp_e(struct crypto_blkcipher
*tfm
, const u8
*k
, u8
*r
)
82 struct blkcipher_desc desc
;
83 struct scatterlist sg
;
84 uint8_t tmp
[16], data
[16];
88 BT_ERR("tfm %p", tfm
);
95 /* The most significant octet of key corresponds to k[0] */
98 err
= crypto_blkcipher_setkey(tfm
, tmp
, 16);
100 BT_ERR("cipher setkey failed: %d", err
);
104 /* Most significant octet of plaintextData corresponds to data[0] */
107 sg_init_one(&sg
, data
, 16);
109 err
= crypto_blkcipher_encrypt(&desc
, &sg
, &sg
, 16);
111 BT_ERR("Encrypt data error %d", err
);
113 /* Most significant octet of encryptedData corresponds to data[0] */
119 static int smp_ah(struct crypto_blkcipher
*tfm
, u8 irk
[16], u8 r
[3], u8 res
[3])
124 /* r' = padding || r */
126 memset(_res
+ 3, 0, 13);
128 err
= smp_e(tfm
, irk
, _res
);
130 BT_ERR("Encrypt error");
134 /* The output of the random address function ah is:
135 * ah(h, r) = e(k, r') mod 2^24
136 * The output of the security function e is then truncated to 24 bits
137 * by taking the least significant 24 bits of the output of e as the
140 memcpy(res
, _res
, 3);
145 bool smp_irk_matches(struct crypto_blkcipher
*tfm
, u8 irk
[16],
151 BT_DBG("RPA %pMR IRK %*phN", bdaddr
, 16, irk
);
153 err
= smp_ah(tfm
, irk
, &bdaddr
->b
[3], hash
);
157 return !crypto_memneq(bdaddr
->b
, hash
, 3);
160 int smp_generate_rpa(struct crypto_blkcipher
*tfm
, u8 irk
[16], bdaddr_t
*rpa
)
164 get_random_bytes(&rpa
->b
[3], 3);
166 rpa
->b
[5] &= 0x3f; /* Clear two most significant bits */
167 rpa
->b
[5] |= 0x40; /* Set second most significant bit */
169 err
= smp_ah(tfm
, irk
, &rpa
->b
[3], rpa
->b
);
173 BT_DBG("RPA %pMR", rpa
);
178 static int smp_c1(struct crypto_blkcipher
*tfm
, u8 k
[16], u8 r
[16],
179 u8 preq
[7], u8 pres
[7], u8 _iat
, bdaddr_t
*ia
,
180 u8 _rat
, bdaddr_t
*ra
, u8 res
[16])
187 /* p1 = pres || preq || _rat || _iat */
190 memcpy(p1
+ 2, preq
, 7);
191 memcpy(p1
+ 9, pres
, 7);
193 /* p2 = padding || ia || ra */
195 memcpy(p2
+ 6, ia
, 6);
196 memset(p2
+ 12, 0, 4);
199 u128_xor((u128
*) res
, (u128
*) r
, (u128
*) p1
);
201 /* res = e(k, res) */
202 err
= smp_e(tfm
, k
, res
);
204 BT_ERR("Encrypt data error");
208 /* res = res XOR p2 */
209 u128_xor((u128
*) res
, (u128
*) res
, (u128
*) p2
);
211 /* res = e(k, res) */
212 err
= smp_e(tfm
, k
, res
);
214 BT_ERR("Encrypt data error");
219 static int smp_s1(struct crypto_blkcipher
*tfm
, u8 k
[16], u8 r1
[16],
220 u8 r2
[16], u8 _r
[16])
224 /* Just least significant octets from r1 and r2 are considered */
226 memcpy(_r
+ 8, r1
, 8);
228 err
= smp_e(tfm
, k
, _r
);
230 BT_ERR("Encrypt data error");
235 static struct sk_buff
*smp_build_cmd(struct l2cap_conn
*conn
, u8 code
,
236 u16 dlen
, void *data
)
239 struct l2cap_hdr
*lh
;
242 len
= L2CAP_HDR_SIZE
+ sizeof(code
) + dlen
;
247 skb
= bt_skb_alloc(len
, GFP_ATOMIC
);
251 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
252 lh
->len
= cpu_to_le16(sizeof(code
) + dlen
);
253 lh
->cid
= cpu_to_le16(L2CAP_CID_SMP
);
255 memcpy(skb_put(skb
, sizeof(code
)), &code
, sizeof(code
));
257 memcpy(skb_put(skb
, dlen
), data
, dlen
);
262 static void smp_send_cmd(struct l2cap_conn
*conn
, u8 code
, u16 len
, void *data
)
264 struct sk_buff
*skb
= smp_build_cmd(conn
, code
, len
, data
);
266 BT_DBG("code 0x%2.2x", code
);
271 skb
->priority
= HCI_PRIO_MAX
;
272 hci_send_acl(conn
->hchan
, skb
, 0);
274 cancel_delayed_work_sync(&conn
->security_timer
);
275 schedule_delayed_work(&conn
->security_timer
, SMP_TIMEOUT
);
278 static __u8
authreq_to_seclevel(__u8 authreq
)
280 if (authreq
& SMP_AUTH_MITM
)
281 return BT_SECURITY_HIGH
;
283 return BT_SECURITY_MEDIUM
;
286 static __u8
seclevel_to_authreq(__u8 sec_level
)
289 case BT_SECURITY_HIGH
:
290 return SMP_AUTH_MITM
| SMP_AUTH_BONDING
;
291 case BT_SECURITY_MEDIUM
:
292 return SMP_AUTH_BONDING
;
294 return SMP_AUTH_NONE
;
298 static void build_pairing_cmd(struct l2cap_conn
*conn
,
299 struct smp_cmd_pairing
*req
,
300 struct smp_cmd_pairing
*rsp
, __u8 authreq
)
302 struct smp_chan
*smp
= conn
->smp_chan
;
303 struct hci_conn
*hcon
= conn
->hcon
;
304 struct hci_dev
*hdev
= hcon
->hdev
;
305 u8 local_dist
= 0, remote_dist
= 0;
307 if (test_bit(HCI_PAIRABLE
, &conn
->hcon
->hdev
->dev_flags
)) {
308 local_dist
= SMP_DIST_ENC_KEY
| SMP_DIST_SIGN
;
309 remote_dist
= SMP_DIST_ENC_KEY
| SMP_DIST_SIGN
;
310 authreq
|= SMP_AUTH_BONDING
;
312 authreq
&= ~SMP_AUTH_BONDING
;
315 if (test_bit(HCI_RPA_RESOLVING
, &hdev
->dev_flags
))
316 remote_dist
|= SMP_DIST_ID_KEY
;
318 if (test_bit(HCI_PRIVACY
, &hdev
->dev_flags
))
319 local_dist
|= SMP_DIST_ID_KEY
;
322 req
->io_capability
= conn
->hcon
->io_capability
;
323 req
->oob_flag
= SMP_OOB_NOT_PRESENT
;
324 req
->max_key_size
= SMP_MAX_ENC_KEY_SIZE
;
325 req
->init_key_dist
= local_dist
;
326 req
->resp_key_dist
= remote_dist
;
327 req
->auth_req
= (authreq
& AUTH_REQ_MASK
);
329 smp
->remote_key_dist
= remote_dist
;
333 rsp
->io_capability
= conn
->hcon
->io_capability
;
334 rsp
->oob_flag
= SMP_OOB_NOT_PRESENT
;
335 rsp
->max_key_size
= SMP_MAX_ENC_KEY_SIZE
;
336 rsp
->init_key_dist
= req
->init_key_dist
& remote_dist
;
337 rsp
->resp_key_dist
= req
->resp_key_dist
& local_dist
;
338 rsp
->auth_req
= (authreq
& AUTH_REQ_MASK
);
340 smp
->remote_key_dist
= rsp
->init_key_dist
;
343 static u8
check_enc_key_size(struct l2cap_conn
*conn
, __u8 max_key_size
)
345 struct smp_chan
*smp
= conn
->smp_chan
;
347 if ((max_key_size
> SMP_MAX_ENC_KEY_SIZE
) ||
348 (max_key_size
< SMP_MIN_ENC_KEY_SIZE
))
349 return SMP_ENC_KEY_SIZE
;
351 smp
->enc_key_size
= max_key_size
;
356 static void smp_failure(struct l2cap_conn
*conn
, u8 reason
)
358 struct hci_conn
*hcon
= conn
->hcon
;
361 smp_send_cmd(conn
, SMP_CMD_PAIRING_FAIL
, sizeof(reason
),
364 clear_bit(HCI_CONN_ENCRYPT_PEND
, &hcon
->flags
);
365 mgmt_auth_failed(hcon
->hdev
, &hcon
->dst
, hcon
->type
, hcon
->dst_type
,
366 HCI_ERROR_AUTH_FAILURE
);
368 cancel_delayed_work_sync(&conn
->security_timer
);
370 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND
, &hcon
->flags
))
371 smp_chan_destroy(conn
);
374 #define JUST_WORKS 0x00
375 #define JUST_CFM 0x01
376 #define REQ_PASSKEY 0x02
377 #define CFM_PASSKEY 0x03
381 static const u8 gen_method
[5][5] = {
382 { JUST_WORKS
, JUST_CFM
, REQ_PASSKEY
, JUST_WORKS
, REQ_PASSKEY
},
383 { JUST_WORKS
, JUST_CFM
, REQ_PASSKEY
, JUST_WORKS
, REQ_PASSKEY
},
384 { CFM_PASSKEY
, CFM_PASSKEY
, REQ_PASSKEY
, JUST_WORKS
, CFM_PASSKEY
},
385 { JUST_WORKS
, JUST_CFM
, JUST_WORKS
, JUST_WORKS
, JUST_CFM
},
386 { CFM_PASSKEY
, CFM_PASSKEY
, REQ_PASSKEY
, JUST_WORKS
, OVERLAP
},
389 static u8
get_auth_method(struct smp_chan
*smp
, u8 local_io
, u8 remote_io
)
391 /* If either side has unknown io_caps, use JUST WORKS */
392 if (local_io
> SMP_IO_KEYBOARD_DISPLAY
||
393 remote_io
> SMP_IO_KEYBOARD_DISPLAY
)
396 return gen_method
[remote_io
][local_io
];
399 static int tk_request(struct l2cap_conn
*conn
, u8 remote_oob
, u8 auth
,
400 u8 local_io
, u8 remote_io
)
402 struct hci_conn
*hcon
= conn
->hcon
;
403 struct smp_chan
*smp
= conn
->smp_chan
;
408 /* Initialize key for JUST WORKS */
409 memset(smp
->tk
, 0, sizeof(smp
->tk
));
410 clear_bit(SMP_FLAG_TK_VALID
, &smp
->flags
);
412 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth
, local_io
, remote_io
);
414 /* If neither side wants MITM, use JUST WORKS */
415 /* Otherwise, look up method from the table */
416 if (!(auth
& SMP_AUTH_MITM
))
419 method
= get_auth_method(smp
, local_io
, remote_io
);
421 /* If not bonding, don't ask user to confirm a Zero TK */
422 if (!(auth
& SMP_AUTH_BONDING
) && method
== JUST_CFM
)
425 /* Don't confirm locally initiated pairing attempts */
426 if (method
== JUST_CFM
&& test_bit(SMP_FLAG_INITIATOR
, &smp
->flags
))
429 /* If Just Works, Continue with Zero TK */
430 if (method
== JUST_WORKS
) {
431 set_bit(SMP_FLAG_TK_VALID
, &smp
->flags
);
435 /* Not Just Works/Confirm results in MITM Authentication */
436 if (method
!= JUST_CFM
) {
437 set_bit(SMP_FLAG_MITM_AUTH
, &smp
->flags
);
438 if (hcon
->pending_sec_level
< BT_SECURITY_HIGH
)
439 hcon
->pending_sec_level
= BT_SECURITY_HIGH
;
442 /* If both devices have Keyoard-Display I/O, the master
443 * Confirms and the slave Enters the passkey.
445 if (method
== OVERLAP
) {
446 if (hcon
->link_mode
& HCI_LM_MASTER
)
447 method
= CFM_PASSKEY
;
449 method
= REQ_PASSKEY
;
452 /* Generate random passkey. */
453 if (method
== CFM_PASSKEY
) {
454 memset(smp
->tk
, 0, sizeof(smp
->tk
));
455 get_random_bytes(&passkey
, sizeof(passkey
));
457 put_unaligned_le32(passkey
, smp
->tk
);
458 BT_DBG("PassKey: %d", passkey
);
459 set_bit(SMP_FLAG_TK_VALID
, &smp
->flags
);
462 hci_dev_lock(hcon
->hdev
);
464 if (method
== REQ_PASSKEY
)
465 ret
= mgmt_user_passkey_request(hcon
->hdev
, &hcon
->dst
,
466 hcon
->type
, hcon
->dst_type
);
467 else if (method
== JUST_CFM
)
468 ret
= mgmt_user_confirm_request(hcon
->hdev
, &hcon
->dst
,
469 hcon
->type
, hcon
->dst_type
,
472 ret
= mgmt_user_passkey_notify(hcon
->hdev
, &hcon
->dst
,
473 hcon
->type
, hcon
->dst_type
,
476 hci_dev_unlock(hcon
->hdev
);
481 static u8
smp_confirm(struct smp_chan
*smp
)
483 struct l2cap_conn
*conn
= smp
->conn
;
484 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
485 struct crypto_blkcipher
*tfm
= hdev
->tfm_aes
;
486 struct smp_cmd_pairing_confirm cp
;
489 BT_DBG("conn %p", conn
);
491 /* Prevent mutual access to hdev->tfm_aes */
494 ret
= smp_c1(tfm
, smp
->tk
, smp
->prnd
, smp
->preq
, smp
->prsp
,
495 conn
->hcon
->init_addr_type
, &conn
->hcon
->init_addr
,
496 conn
->hcon
->resp_addr_type
, &conn
->hcon
->resp_addr
,
499 hci_dev_unlock(hdev
);
502 return SMP_UNSPECIFIED
;
504 clear_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
);
506 smp_send_cmd(smp
->conn
, SMP_CMD_PAIRING_CONFIRM
, sizeof(cp
), &cp
);
511 static u8
smp_random(struct smp_chan
*smp
)
513 struct l2cap_conn
*conn
= smp
->conn
;
514 struct hci_conn
*hcon
= conn
->hcon
;
515 struct hci_dev
*hdev
= hcon
->hdev
;
516 struct crypto_blkcipher
*tfm
= hdev
->tfm_aes
;
520 if (IS_ERR_OR_NULL(tfm
))
521 return SMP_UNSPECIFIED
;
523 BT_DBG("conn %p %s", conn
, conn
->hcon
->out
? "master" : "slave");
525 /* Prevent mutual access to hdev->tfm_aes */
528 ret
= smp_c1(tfm
, smp
->tk
, smp
->rrnd
, smp
->preq
, smp
->prsp
,
529 hcon
->init_addr_type
, &hcon
->init_addr
,
530 hcon
->resp_addr_type
, &hcon
->resp_addr
, confirm
);
532 hci_dev_unlock(hdev
);
535 return SMP_UNSPECIFIED
;
537 if (crypto_memneq(smp
->pcnf
, confirm
, sizeof(smp
->pcnf
))) {
538 BT_ERR("Pairing failed (confirmation values mismatch)");
539 return SMP_CONFIRM_FAILED
;
547 smp_s1(tfm
, smp
->tk
, smp
->rrnd
, smp
->prnd
, stk
);
549 memset(stk
+ smp
->enc_key_size
, 0,
550 SMP_MAX_ENC_KEY_SIZE
- smp
->enc_key_size
);
552 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND
, &hcon
->flags
))
553 return SMP_UNSPECIFIED
;
555 hci_le_start_enc(hcon
, ediv
, rand
, stk
);
556 hcon
->enc_key_size
= smp
->enc_key_size
;
562 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
, sizeof(smp
->prnd
),
565 smp_s1(tfm
, smp
->tk
, smp
->prnd
, smp
->rrnd
, stk
);
567 memset(stk
+ smp
->enc_key_size
, 0,
568 SMP_MAX_ENC_KEY_SIZE
- smp
->enc_key_size
);
570 if (hcon
->pending_sec_level
== BT_SECURITY_HIGH
)
575 hci_add_ltk(hcon
->hdev
, &hcon
->dst
, hcon
->dst_type
,
576 HCI_SMP_STK_SLAVE
, auth
, stk
, smp
->enc_key_size
,
583 static struct smp_chan
*smp_chan_create(struct l2cap_conn
*conn
)
585 struct smp_chan
*smp
;
587 smp
= kzalloc(sizeof(*smp
), GFP_ATOMIC
);
592 conn
->smp_chan
= smp
;
593 conn
->hcon
->smp_conn
= conn
;
595 hci_conn_hold(conn
->hcon
);
600 void smp_chan_destroy(struct l2cap_conn
*conn
)
602 struct smp_chan
*smp
= conn
->smp_chan
;
607 complete
= test_bit(SMP_FLAG_COMPLETE
, &smp
->flags
);
608 mgmt_smp_complete(conn
->hcon
, complete
);
611 kfree(smp
->slave_csrk
);
613 /* If pairing failed clean up any keys we might have */
616 list_del(&smp
->ltk
->list
);
620 if (smp
->slave_ltk
) {
621 list_del(&smp
->slave_ltk
->list
);
622 kfree(smp
->slave_ltk
);
625 if (smp
->remote_irk
) {
626 list_del(&smp
->remote_irk
->list
);
627 kfree(smp
->remote_irk
);
632 conn
->smp_chan
= NULL
;
633 conn
->hcon
->smp_conn
= NULL
;
634 hci_conn_drop(conn
->hcon
);
637 int smp_user_confirm_reply(struct hci_conn
*hcon
, u16 mgmt_op
, __le32 passkey
)
639 struct l2cap_conn
*conn
= hcon
->smp_conn
;
640 struct smp_chan
*smp
;
648 smp
= conn
->smp_chan
;
651 case MGMT_OP_USER_PASSKEY_REPLY
:
652 value
= le32_to_cpu(passkey
);
653 memset(smp
->tk
, 0, sizeof(smp
->tk
));
654 BT_DBG("PassKey: %d", value
);
655 put_unaligned_le32(value
, smp
->tk
);
657 case MGMT_OP_USER_CONFIRM_REPLY
:
658 set_bit(SMP_FLAG_TK_VALID
, &smp
->flags
);
660 case MGMT_OP_USER_PASSKEY_NEG_REPLY
:
661 case MGMT_OP_USER_CONFIRM_NEG_REPLY
:
662 smp_failure(conn
, SMP_PASSKEY_ENTRY_FAILED
);
665 smp_failure(conn
, SMP_PASSKEY_ENTRY_FAILED
);
669 /* If it is our turn to send Pairing Confirm, do so now */
670 if (test_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
)) {
671 u8 rsp
= smp_confirm(smp
);
673 smp_failure(conn
, rsp
);
679 static u8
smp_cmd_pairing_req(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
681 struct smp_cmd_pairing rsp
, *req
= (void *) skb
->data
;
682 struct smp_chan
*smp
;
683 u8 key_size
, auth
, sec_level
;
686 BT_DBG("conn %p", conn
);
688 if (skb
->len
< sizeof(*req
))
689 return SMP_INVALID_PARAMS
;
691 if (conn
->hcon
->link_mode
& HCI_LM_MASTER
)
692 return SMP_CMD_NOTSUPP
;
694 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND
, &conn
->hcon
->flags
))
695 smp
= smp_chan_create(conn
);
697 smp
= conn
->smp_chan
;
700 return SMP_UNSPECIFIED
;
702 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
703 memcpy(&smp
->preq
[1], req
, sizeof(*req
));
704 skb_pull(skb
, sizeof(*req
));
706 /* We didn't start the pairing, so match remote */
707 auth
= req
->auth_req
;
709 sec_level
= authreq_to_seclevel(auth
);
710 if (sec_level
> conn
->hcon
->pending_sec_level
)
711 conn
->hcon
->pending_sec_level
= sec_level
;
713 /* If we need MITM check that it can be acheived */
714 if (conn
->hcon
->pending_sec_level
>= BT_SECURITY_HIGH
) {
717 method
= get_auth_method(smp
, conn
->hcon
->io_capability
,
719 if (method
== JUST_WORKS
|| method
== JUST_CFM
)
720 return SMP_AUTH_REQUIREMENTS
;
723 build_pairing_cmd(conn
, req
, &rsp
, auth
);
725 key_size
= min(req
->max_key_size
, rsp
.max_key_size
);
726 if (check_enc_key_size(conn
, key_size
))
727 return SMP_ENC_KEY_SIZE
;
729 get_random_bytes(smp
->prnd
, sizeof(smp
->prnd
));
731 smp
->prsp
[0] = SMP_CMD_PAIRING_RSP
;
732 memcpy(&smp
->prsp
[1], &rsp
, sizeof(rsp
));
734 smp_send_cmd(conn
, SMP_CMD_PAIRING_RSP
, sizeof(rsp
), &rsp
);
736 /* Request setup of TK */
737 ret
= tk_request(conn
, 0, auth
, rsp
.io_capability
, req
->io_capability
);
739 return SMP_UNSPECIFIED
;
741 clear_bit(SMP_FLAG_INITIATOR
, &smp
->flags
);
746 static u8
smp_cmd_pairing_rsp(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
748 struct smp_cmd_pairing
*req
, *rsp
= (void *) skb
->data
;
749 struct smp_chan
*smp
= conn
->smp_chan
;
750 u8 key_size
, auth
= SMP_AUTH_NONE
;
753 BT_DBG("conn %p", conn
);
755 if (skb
->len
< sizeof(*rsp
))
756 return SMP_INVALID_PARAMS
;
758 if (!(conn
->hcon
->link_mode
& HCI_LM_MASTER
))
759 return SMP_CMD_NOTSUPP
;
761 skb_pull(skb
, sizeof(*rsp
));
763 req
= (void *) &smp
->preq
[1];
765 key_size
= min(req
->max_key_size
, rsp
->max_key_size
);
766 if (check_enc_key_size(conn
, key_size
))
767 return SMP_ENC_KEY_SIZE
;
769 /* If we need MITM check that it can be acheived */
770 if (conn
->hcon
->pending_sec_level
>= BT_SECURITY_HIGH
) {
773 method
= get_auth_method(smp
, req
->io_capability
,
775 if (method
== JUST_WORKS
|| method
== JUST_CFM
)
776 return SMP_AUTH_REQUIREMENTS
;
779 get_random_bytes(smp
->prnd
, sizeof(smp
->prnd
));
781 smp
->prsp
[0] = SMP_CMD_PAIRING_RSP
;
782 memcpy(&smp
->prsp
[1], rsp
, sizeof(*rsp
));
784 /* Update remote key distribution in case the remote cleared
785 * some bits that we had enabled in our request.
787 smp
->remote_key_dist
&= rsp
->resp_key_dist
;
789 if ((req
->auth_req
& SMP_AUTH_BONDING
) &&
790 (rsp
->auth_req
& SMP_AUTH_BONDING
))
791 auth
= SMP_AUTH_BONDING
;
793 auth
|= (req
->auth_req
| rsp
->auth_req
) & SMP_AUTH_MITM
;
795 ret
= tk_request(conn
, 0, auth
, req
->io_capability
, rsp
->io_capability
);
797 return SMP_UNSPECIFIED
;
799 set_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
);
801 /* Can't compose response until we have been confirmed */
802 if (test_bit(SMP_FLAG_TK_VALID
, &smp
->flags
))
803 return smp_confirm(smp
);
808 static u8
smp_cmd_pairing_confirm(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
810 struct smp_chan
*smp
= conn
->smp_chan
;
812 BT_DBG("conn %p %s", conn
, conn
->hcon
->out
? "master" : "slave");
814 if (skb
->len
< sizeof(smp
->pcnf
))
815 return SMP_INVALID_PARAMS
;
817 memcpy(smp
->pcnf
, skb
->data
, sizeof(smp
->pcnf
));
818 skb_pull(skb
, sizeof(smp
->pcnf
));
821 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
, sizeof(smp
->prnd
),
823 else if (test_bit(SMP_FLAG_TK_VALID
, &smp
->flags
))
824 return smp_confirm(smp
);
826 set_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
);
831 static u8
smp_cmd_pairing_random(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
833 struct smp_chan
*smp
= conn
->smp_chan
;
835 BT_DBG("conn %p", conn
);
837 if (skb
->len
< sizeof(smp
->rrnd
))
838 return SMP_INVALID_PARAMS
;
840 memcpy(smp
->rrnd
, skb
->data
, sizeof(smp
->rrnd
));
841 skb_pull(skb
, sizeof(smp
->rrnd
));
843 return smp_random(smp
);
846 static u8
smp_ltk_encrypt(struct l2cap_conn
*conn
, u8 sec_level
)
849 struct hci_conn
*hcon
= conn
->hcon
;
851 key
= hci_find_ltk_by_addr(hcon
->hdev
, &hcon
->dst
, hcon
->dst_type
,
856 if (sec_level
> BT_SECURITY_MEDIUM
&& !key
->authenticated
)
859 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND
, &hcon
->flags
))
862 hci_le_start_enc(hcon
, key
->ediv
, key
->rand
, key
->val
);
863 hcon
->enc_key_size
= key
->enc_size
;
868 static u8
smp_cmd_security_req(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
870 struct smp_cmd_security_req
*rp
= (void *) skb
->data
;
871 struct smp_cmd_pairing cp
;
872 struct hci_conn
*hcon
= conn
->hcon
;
873 struct smp_chan
*smp
;
876 BT_DBG("conn %p", conn
);
878 if (skb
->len
< sizeof(*rp
))
879 return SMP_INVALID_PARAMS
;
881 if (!(conn
->hcon
->link_mode
& HCI_LM_MASTER
))
882 return SMP_CMD_NOTSUPP
;
884 sec_level
= authreq_to_seclevel(rp
->auth_req
);
885 if (sec_level
> hcon
->pending_sec_level
)
886 hcon
->pending_sec_level
= sec_level
;
888 if (smp_ltk_encrypt(conn
, hcon
->pending_sec_level
))
891 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND
, &hcon
->flags
))
894 smp
= smp_chan_create(conn
);
896 skb_pull(skb
, sizeof(*rp
));
898 memset(&cp
, 0, sizeof(cp
));
899 build_pairing_cmd(conn
, &cp
, NULL
, rp
->auth_req
);
901 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
902 memcpy(&smp
->preq
[1], &cp
, sizeof(cp
));
904 smp_send_cmd(conn
, SMP_CMD_PAIRING_REQ
, sizeof(cp
), &cp
);
906 clear_bit(SMP_FLAG_INITIATOR
, &smp
->flags
);
911 bool smp_sufficient_security(struct hci_conn
*hcon
, u8 sec_level
)
913 if (sec_level
== BT_SECURITY_LOW
)
916 if (hcon
->sec_level
>= sec_level
)
922 int smp_conn_security(struct hci_conn
*hcon
, __u8 sec_level
)
924 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
925 struct smp_chan
*smp
;
928 BT_DBG("conn %p hcon %p level 0x%2.2x", conn
, hcon
, sec_level
);
930 /* This may be NULL if there's an unexpected disconnection */
934 if (!test_bit(HCI_LE_ENABLED
, &hcon
->hdev
->dev_flags
))
937 if (smp_sufficient_security(hcon
, sec_level
))
940 if (sec_level
> hcon
->pending_sec_level
)
941 hcon
->pending_sec_level
= sec_level
;
943 if (hcon
->link_mode
& HCI_LM_MASTER
)
944 if (smp_ltk_encrypt(conn
, hcon
->pending_sec_level
))
947 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND
, &hcon
->flags
))
950 smp
= smp_chan_create(conn
);
954 authreq
= seclevel_to_authreq(sec_level
);
956 /* Require MITM if IO Capability allows or the security level
959 if (hcon
->io_capability
!= HCI_IO_NO_INPUT_OUTPUT
||
960 hcon
->pending_sec_level
> BT_SECURITY_MEDIUM
)
961 authreq
|= SMP_AUTH_MITM
;
963 if (hcon
->link_mode
& HCI_LM_MASTER
) {
964 struct smp_cmd_pairing cp
;
966 build_pairing_cmd(conn
, &cp
, NULL
, authreq
);
967 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
968 memcpy(&smp
->preq
[1], &cp
, sizeof(cp
));
970 smp_send_cmd(conn
, SMP_CMD_PAIRING_REQ
, sizeof(cp
), &cp
);
972 struct smp_cmd_security_req cp
;
973 cp
.auth_req
= authreq
;
974 smp_send_cmd(conn
, SMP_CMD_SECURITY_REQ
, sizeof(cp
), &cp
);
977 set_bit(SMP_FLAG_INITIATOR
, &smp
->flags
);
982 static int smp_cmd_encrypt_info(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
984 struct smp_cmd_encrypt_info
*rp
= (void *) skb
->data
;
985 struct smp_chan
*smp
= conn
->smp_chan
;
987 BT_DBG("conn %p", conn
);
989 if (skb
->len
< sizeof(*rp
))
990 return SMP_INVALID_PARAMS
;
992 /* Ignore this PDU if it wasn't requested */
993 if (!(smp
->remote_key_dist
& SMP_DIST_ENC_KEY
))
996 skb_pull(skb
, sizeof(*rp
));
998 memcpy(smp
->tk
, rp
->ltk
, sizeof(smp
->tk
));
1003 static int smp_cmd_master_ident(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
1005 struct smp_cmd_master_ident
*rp
= (void *) skb
->data
;
1006 struct smp_chan
*smp
= conn
->smp_chan
;
1007 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
1008 struct hci_conn
*hcon
= conn
->hcon
;
1009 struct smp_ltk
*ltk
;
1012 BT_DBG("conn %p", conn
);
1014 if (skb
->len
< sizeof(*rp
))
1015 return SMP_INVALID_PARAMS
;
1017 /* Ignore this PDU if it wasn't requested */
1018 if (!(smp
->remote_key_dist
& SMP_DIST_ENC_KEY
))
1021 /* Mark the information as received */
1022 smp
->remote_key_dist
&= ~SMP_DIST_ENC_KEY
;
1024 skb_pull(skb
, sizeof(*rp
));
1027 authenticated
= (hcon
->sec_level
== BT_SECURITY_HIGH
);
1028 ltk
= hci_add_ltk(hdev
, &hcon
->dst
, hcon
->dst_type
, HCI_SMP_LTK
,
1029 authenticated
, smp
->tk
, smp
->enc_key_size
,
1030 rp
->ediv
, rp
->rand
);
1032 if (!(smp
->remote_key_dist
& SMP_DIST_ID_KEY
))
1033 smp_distribute_keys(conn
);
1034 hci_dev_unlock(hdev
);
1039 static int smp_cmd_ident_info(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
1041 struct smp_cmd_ident_info
*info
= (void *) skb
->data
;
1042 struct smp_chan
*smp
= conn
->smp_chan
;
1046 if (skb
->len
< sizeof(*info
))
1047 return SMP_INVALID_PARAMS
;
1049 /* Ignore this PDU if it wasn't requested */
1050 if (!(smp
->remote_key_dist
& SMP_DIST_ID_KEY
))
1053 skb_pull(skb
, sizeof(*info
));
1055 memcpy(smp
->irk
, info
->irk
, 16);
1060 static int smp_cmd_ident_addr_info(struct l2cap_conn
*conn
,
1061 struct sk_buff
*skb
)
1063 struct smp_cmd_ident_addr_info
*info
= (void *) skb
->data
;
1064 struct smp_chan
*smp
= conn
->smp_chan
;
1065 struct hci_conn
*hcon
= conn
->hcon
;
1070 if (skb
->len
< sizeof(*info
))
1071 return SMP_INVALID_PARAMS
;
1073 /* Ignore this PDU if it wasn't requested */
1074 if (!(smp
->remote_key_dist
& SMP_DIST_ID_KEY
))
1077 /* Mark the information as received */
1078 smp
->remote_key_dist
&= ~SMP_DIST_ID_KEY
;
1080 skb_pull(skb
, sizeof(*info
));
1082 /* Strictly speaking the Core Specification (4.1) allows sending
1083 * an empty address which would force us to rely on just the IRK
1084 * as "identity information". However, since such
1085 * implementations are not known of and in order to not over
1086 * complicate our implementation, simply pretend that we never
1087 * received an IRK for such a device.
1089 if (!bacmp(&info
->bdaddr
, BDADDR_ANY
)) {
1090 BT_ERR("Ignoring IRK with no identity address");
1091 smp_distribute_keys(conn
);
1095 bacpy(&smp
->id_addr
, &info
->bdaddr
);
1096 smp
->id_addr_type
= info
->addr_type
;
1098 if (hci_bdaddr_is_rpa(&hcon
->dst
, hcon
->dst_type
))
1099 bacpy(&rpa
, &hcon
->dst
);
1101 bacpy(&rpa
, BDADDR_ANY
);
1103 smp
->remote_irk
= hci_add_irk(conn
->hcon
->hdev
, &smp
->id_addr
,
1104 smp
->id_addr_type
, smp
->irk
, &rpa
);
1106 smp_distribute_keys(conn
);
1111 static int smp_cmd_sign_info(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
1113 struct smp_cmd_sign_info
*rp
= (void *) skb
->data
;
1114 struct smp_chan
*smp
= conn
->smp_chan
;
1115 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
1116 struct smp_csrk
*csrk
;
1118 BT_DBG("conn %p", conn
);
1120 if (skb
->len
< sizeof(*rp
))
1121 return SMP_INVALID_PARAMS
;
1123 /* Ignore this PDU if it wasn't requested */
1124 if (!(smp
->remote_key_dist
& SMP_DIST_SIGN
))
1127 /* Mark the information as received */
1128 smp
->remote_key_dist
&= ~SMP_DIST_SIGN
;
1130 skb_pull(skb
, sizeof(*rp
));
1133 csrk
= kzalloc(sizeof(*csrk
), GFP_KERNEL
);
1135 csrk
->master
= 0x01;
1136 memcpy(csrk
->val
, rp
->csrk
, sizeof(csrk
->val
));
1139 if (!(smp
->remote_key_dist
& SMP_DIST_SIGN
))
1140 smp_distribute_keys(conn
);
1141 hci_dev_unlock(hdev
);
1146 int smp_sig_channel(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
1148 struct hci_conn
*hcon
= conn
->hcon
;
1152 if (hcon
->type
!= LE_LINK
) {
1162 if (!test_bit(HCI_LE_ENABLED
, &hcon
->hdev
->dev_flags
)) {
1164 reason
= SMP_PAIRING_NOTSUPP
;
1168 code
= skb
->data
[0];
1169 skb_pull(skb
, sizeof(code
));
1172 * The SMP context must be initialized for all other PDUs except
1173 * pairing and security requests. If we get any other PDU when
1174 * not initialized simply disconnect (done if this function
1175 * returns an error).
1177 if (code
!= SMP_CMD_PAIRING_REQ
&& code
!= SMP_CMD_SECURITY_REQ
&&
1179 BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code
);
1185 case SMP_CMD_PAIRING_REQ
:
1186 reason
= smp_cmd_pairing_req(conn
, skb
);
1189 case SMP_CMD_PAIRING_FAIL
:
1190 smp_failure(conn
, 0);
1195 case SMP_CMD_PAIRING_RSP
:
1196 reason
= smp_cmd_pairing_rsp(conn
, skb
);
1199 case SMP_CMD_SECURITY_REQ
:
1200 reason
= smp_cmd_security_req(conn
, skb
);
1203 case SMP_CMD_PAIRING_CONFIRM
:
1204 reason
= smp_cmd_pairing_confirm(conn
, skb
);
1207 case SMP_CMD_PAIRING_RANDOM
:
1208 reason
= smp_cmd_pairing_random(conn
, skb
);
1211 case SMP_CMD_ENCRYPT_INFO
:
1212 reason
= smp_cmd_encrypt_info(conn
, skb
);
1215 case SMP_CMD_MASTER_IDENT
:
1216 reason
= smp_cmd_master_ident(conn
, skb
);
1219 case SMP_CMD_IDENT_INFO
:
1220 reason
= smp_cmd_ident_info(conn
, skb
);
1223 case SMP_CMD_IDENT_ADDR_INFO
:
1224 reason
= smp_cmd_ident_addr_info(conn
, skb
);
1227 case SMP_CMD_SIGN_INFO
:
1228 reason
= smp_cmd_sign_info(conn
, skb
);
1232 BT_DBG("Unknown command code 0x%2.2x", code
);
1234 reason
= SMP_CMD_NOTSUPP
;
1241 smp_failure(conn
, reason
);
1247 static void smp_notify_keys(struct l2cap_conn
*conn
)
1249 struct smp_chan
*smp
= conn
->smp_chan
;
1250 struct hci_conn
*hcon
= conn
->hcon
;
1251 struct hci_dev
*hdev
= hcon
->hdev
;
1252 struct smp_cmd_pairing
*req
= (void *) &smp
->preq
[1];
1253 struct smp_cmd_pairing
*rsp
= (void *) &smp
->prsp
[1];
1256 if (smp
->remote_irk
) {
1257 mgmt_new_irk(hdev
, smp
->remote_irk
);
1258 /* Now that user space can be considered to know the
1259 * identity address track the connection based on it
1262 bacpy(&hcon
->dst
, &smp
->remote_irk
->bdaddr
);
1263 hcon
->dst_type
= smp
->remote_irk
->addr_type
;
1264 l2cap_conn_update_id_addr(hcon
);
1267 /* The LTKs and CSRKs should be persistent only if both sides
1268 * had the bonding bit set in their authentication requests.
1270 persistent
= !!((req
->auth_req
& rsp
->auth_req
) & SMP_AUTH_BONDING
);
1273 smp
->csrk
->bdaddr_type
= hcon
->dst_type
;
1274 bacpy(&smp
->csrk
->bdaddr
, &hcon
->dst
);
1275 mgmt_new_csrk(hdev
, smp
->csrk
, persistent
);
1278 if (smp
->slave_csrk
) {
1279 smp
->slave_csrk
->bdaddr_type
= hcon
->dst_type
;
1280 bacpy(&smp
->slave_csrk
->bdaddr
, &hcon
->dst
);
1281 mgmt_new_csrk(hdev
, smp
->slave_csrk
, persistent
);
1285 smp
->ltk
->bdaddr_type
= hcon
->dst_type
;
1286 bacpy(&smp
->ltk
->bdaddr
, &hcon
->dst
);
1287 mgmt_new_ltk(hdev
, smp
->ltk
, persistent
);
1290 if (smp
->slave_ltk
) {
1291 smp
->slave_ltk
->bdaddr_type
= hcon
->dst_type
;
1292 bacpy(&smp
->slave_ltk
->bdaddr
, &hcon
->dst
);
1293 mgmt_new_ltk(hdev
, smp
->slave_ltk
, persistent
);
1297 int smp_distribute_keys(struct l2cap_conn
*conn
)
1299 struct smp_cmd_pairing
*req
, *rsp
;
1300 struct smp_chan
*smp
= conn
->smp_chan
;
1301 struct hci_conn
*hcon
= conn
->hcon
;
1302 struct hci_dev
*hdev
= hcon
->hdev
;
1305 BT_DBG("conn %p", conn
);
1307 if (!test_bit(HCI_CONN_LE_SMP_PEND
, &hcon
->flags
))
1310 rsp
= (void *) &smp
->prsp
[1];
1312 /* The responder sends its keys first */
1313 if (hcon
->out
&& (smp
->remote_key_dist
& 0x07))
1316 req
= (void *) &smp
->preq
[1];
1319 keydist
= &rsp
->init_key_dist
;
1320 *keydist
&= req
->init_key_dist
;
1322 keydist
= &rsp
->resp_key_dist
;
1323 *keydist
&= req
->resp_key_dist
;
1326 BT_DBG("keydist 0x%x", *keydist
);
1328 if (*keydist
& SMP_DIST_ENC_KEY
) {
1329 struct smp_cmd_encrypt_info enc
;
1330 struct smp_cmd_master_ident ident
;
1331 struct smp_ltk
*ltk
;
1336 get_random_bytes(enc
.ltk
, sizeof(enc
.ltk
));
1337 get_random_bytes(&ediv
, sizeof(ediv
));
1338 get_random_bytes(&rand
, sizeof(rand
));
1340 smp_send_cmd(conn
, SMP_CMD_ENCRYPT_INFO
, sizeof(enc
), &enc
);
1342 authenticated
= hcon
->sec_level
== BT_SECURITY_HIGH
;
1343 ltk
= hci_add_ltk(hdev
, &hcon
->dst
, hcon
->dst_type
,
1344 HCI_SMP_LTK_SLAVE
, authenticated
, enc
.ltk
,
1345 smp
->enc_key_size
, ediv
, rand
);
1346 smp
->slave_ltk
= ltk
;
1351 smp_send_cmd(conn
, SMP_CMD_MASTER_IDENT
, sizeof(ident
), &ident
);
1353 *keydist
&= ~SMP_DIST_ENC_KEY
;
1356 if (*keydist
& SMP_DIST_ID_KEY
) {
1357 struct smp_cmd_ident_addr_info addrinfo
;
1358 struct smp_cmd_ident_info idinfo
;
1360 memcpy(idinfo
.irk
, hdev
->irk
, sizeof(idinfo
.irk
));
1362 smp_send_cmd(conn
, SMP_CMD_IDENT_INFO
, sizeof(idinfo
), &idinfo
);
1364 /* The hci_conn contains the local identity address
1365 * after the connection has been established.
1367 * This is true even when the connection has been
1368 * established using a resolvable random address.
1370 bacpy(&addrinfo
.bdaddr
, &hcon
->src
);
1371 addrinfo
.addr_type
= hcon
->src_type
;
1373 smp_send_cmd(conn
, SMP_CMD_IDENT_ADDR_INFO
, sizeof(addrinfo
),
1376 *keydist
&= ~SMP_DIST_ID_KEY
;
1379 if (*keydist
& SMP_DIST_SIGN
) {
1380 struct smp_cmd_sign_info sign
;
1381 struct smp_csrk
*csrk
;
1383 /* Generate a new random key */
1384 get_random_bytes(sign
.csrk
, sizeof(sign
.csrk
));
1386 csrk
= kzalloc(sizeof(*csrk
), GFP_KERNEL
);
1388 csrk
->master
= 0x00;
1389 memcpy(csrk
->val
, sign
.csrk
, sizeof(csrk
->val
));
1391 smp
->slave_csrk
= csrk
;
1393 smp_send_cmd(conn
, SMP_CMD_SIGN_INFO
, sizeof(sign
), &sign
);
1395 *keydist
&= ~SMP_DIST_SIGN
;
1398 /* If there are still keys to be received wait for them */
1399 if ((smp
->remote_key_dist
& 0x07))
1402 clear_bit(HCI_CONN_LE_SMP_PEND
, &hcon
->flags
);
1403 cancel_delayed_work_sync(&conn
->security_timer
);
1404 set_bit(SMP_FLAG_COMPLETE
, &smp
->flags
);
1405 smp_notify_keys(conn
);
1407 smp_chan_destroy(conn
);