Bluetooth: Add support for sending LE SC Confirm value
[deliverable/linux.git] / net / bluetooth / smp.c
CommitLineData
eb492e01
AB
1/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
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;
8
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.
17
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.
21*/
22
8c520a59
GP
23#include <linux/crypto.h>
24#include <linux/scatterlist.h>
25#include <crypto/b128ops.h>
26
eb492e01
AB
27#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/l2cap.h>
2b64d153 30#include <net/bluetooth/mgmt.h>
ac4b7236 31
3b19146d 32#include "ecc.h"
ac4b7236 33#include "smp.h"
d22ef0bc 34
b28b4943 35#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
b28b4943 36
3b19146d
JH
37/* Keys which are not distributed with Secure Connections */
38#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
39
17b02e62 40#define SMP_TIMEOUT msecs_to_jiffies(30000)
5d3de7df 41
0edb14de
JH
42#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
43 0x1f : 0x07)
44#define KEY_DIST_MASK 0x07
065a13e2 45
cbbbe3e2
JH
46/* Maximum message length that can be passed to aes_cmac */
47#define CMAC_MSG_MAX 80
48
533e35d4
JH
49enum {
50 SMP_FLAG_TK_VALID,
51 SMP_FLAG_CFM_PENDING,
52 SMP_FLAG_MITM_AUTH,
53 SMP_FLAG_COMPLETE,
54 SMP_FLAG_INITIATOR,
65668776 55 SMP_FLAG_SC,
d8f8edbe 56 SMP_FLAG_REMOTE_PK,
533e35d4 57};
4bc58f51
JH
58
59struct smp_chan {
b68fda68
JH
60 struct l2cap_conn *conn;
61 struct delayed_work security_timer;
b28b4943 62 unsigned long allow_cmd; /* Bitmask of allowed commands */
b68fda68 63
4bc58f51
JH
64 u8 preq[7]; /* SMP Pairing Request */
65 u8 prsp[7]; /* SMP Pairing Response */
66 u8 prnd[16]; /* SMP Pairing Random (local) */
67 u8 rrnd[16]; /* SMP Pairing Random (remote) */
68 u8 pcnf[16]; /* SMP Pairing Confirm */
69 u8 tk[16]; /* SMP Temporary Key */
70 u8 enc_key_size;
71 u8 remote_key_dist;
72 bdaddr_t id_addr;
73 u8 id_addr_type;
74 u8 irk[16];
75 struct smp_csrk *csrk;
76 struct smp_csrk *slave_csrk;
77 struct smp_ltk *ltk;
78 struct smp_ltk *slave_ltk;
79 struct smp_irk *remote_irk;
4a74d658 80 unsigned long flags;
6a7bd103 81
3b19146d
JH
82 /* Secure Connections variables */
83 u8 local_pk[64];
84 u8 local_sk[32];
d8f8edbe
JH
85 u8 remote_pk[64];
86 u8 dhkey[32];
3b19146d 87
6a7bd103 88 struct crypto_blkcipher *tfm_aes;
407cecf6 89 struct crypto_hash *tfm_cmac;
4bc58f51
JH
90};
91
8a2936f4 92static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
d22ef0bc 93{
8a2936f4 94 size_t i;
d22ef0bc 95
8a2936f4
JH
96 for (i = 0; i < len; i++)
97 dst[len - 1 - i] = src[i];
d22ef0bc
AB
98}
99
cbbbe3e2
JH
100static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
101 size_t len, u8 mac[16])
102{
103 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
104 struct hash_desc desc;
105 struct scatterlist sg;
106 int err;
107
108 if (len > CMAC_MSG_MAX)
109 return -EFBIG;
110
111 if (!tfm) {
112 BT_ERR("tfm %p", tfm);
113 return -EINVAL;
114 }
115
116 desc.tfm = tfm;
117 desc.flags = 0;
118
119 crypto_hash_init(&desc);
120
121 /* Swap key and message from LSB to MSB */
122 swap_buf(k, tmp, 16);
123 swap_buf(m, msg_msb, len);
124
125 BT_DBG("msg (len %zu) %*phN", len, (int) len, m);
126 BT_DBG("key %16phN", k);
127
128 err = crypto_hash_setkey(tfm, tmp, 16);
129 if (err) {
130 BT_ERR("cipher setkey failed: %d", err);
131 return err;
132 }
133
134 sg_init_one(&sg, msg_msb, len);
135
136 err = crypto_hash_update(&desc, &sg, len);
137 if (err) {
138 BT_ERR("Hash update error %d", err);
139 return err;
140 }
141
142 err = crypto_hash_final(&desc, mac_msb);
143 if (err) {
144 BT_ERR("Hash final error %d", err);
145 return err;
146 }
147
148 swap_buf(mac_msb, mac, 16);
149
150 BT_DBG("mac %16phN", mac);
151
152 return 0;
153}
154
155static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
156 const u8 x[16], u8 z, u8 res[16])
157{
158 u8 m[65];
159 int err;
160
161 BT_DBG("u %32phN", u);
162 BT_DBG("v %32phN", v);
163 BT_DBG("x %16phN z %02x", x, z);
164
165 m[0] = z;
166 memcpy(m + 1, v, 32);
167 memcpy(m + 33, u, 32);
168
169 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
170 if (err)
171 return err;
172
173 BT_DBG("res %16phN", res);
174
175 return err;
176}
177
d22ef0bc
AB
178static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
179{
180 struct blkcipher_desc desc;
181 struct scatterlist sg;
943a732a 182 uint8_t tmp[16], data[16];
201a5929 183 int err;
d22ef0bc
AB
184
185 if (tfm == NULL) {
186 BT_ERR("tfm %p", tfm);
187 return -EINVAL;
188 }
189
190 desc.tfm = tfm;
191 desc.flags = 0;
192
943a732a 193 /* The most significant octet of key corresponds to k[0] */
8a2936f4 194 swap_buf(k, tmp, 16);
943a732a
JH
195
196 err = crypto_blkcipher_setkey(tfm, tmp, 16);
d22ef0bc
AB
197 if (err) {
198 BT_ERR("cipher setkey failed: %d", err);
199 return err;
200 }
201
943a732a 202 /* Most significant octet of plaintextData corresponds to data[0] */
8a2936f4 203 swap_buf(r, data, 16);
943a732a
JH
204
205 sg_init_one(&sg, data, 16);
d22ef0bc 206
d22ef0bc
AB
207 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
208 if (err)
209 BT_ERR("Encrypt data error %d", err);
210
943a732a 211 /* Most significant octet of encryptedData corresponds to data[0] */
8a2936f4 212 swap_buf(data, r, 16);
943a732a 213
d22ef0bc
AB
214 return err;
215}
216
60478054
JH
217static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
218{
943a732a 219 u8 _res[16];
60478054
JH
220 int err;
221
222 /* r' = padding || r */
943a732a
JH
223 memcpy(_res, r, 3);
224 memset(_res + 3, 0, 13);
60478054 225
943a732a 226 err = smp_e(tfm, irk, _res);
60478054
JH
227 if (err) {
228 BT_ERR("Encrypt error");
229 return err;
230 }
231
232 /* The output of the random address function ah is:
233 * ah(h, r) = e(k, r') mod 2^24
234 * The output of the security function e is then truncated to 24 bits
235 * by taking the least significant 24 bits of the output of e as the
236 * result of ah.
237 */
943a732a 238 memcpy(res, _res, 3);
60478054
JH
239
240 return 0;
241}
242
defce9e8 243bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
60478054 244{
defce9e8
JH
245 struct l2cap_chan *chan = hdev->smp_data;
246 struct crypto_blkcipher *tfm;
60478054
JH
247 u8 hash[3];
248 int err;
249
defce9e8
JH
250 if (!chan || !chan->data)
251 return false;
252
253 tfm = chan->data;
254
60478054
JH
255 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
256
257 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
258 if (err)
259 return false;
260
261 return !memcmp(bdaddr->b, hash, 3);
262}
263
defce9e8 264int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
b1e2b3ae 265{
defce9e8
JH
266 struct l2cap_chan *chan = hdev->smp_data;
267 struct crypto_blkcipher *tfm;
b1e2b3ae
JH
268 int err;
269
defce9e8
JH
270 if (!chan || !chan->data)
271 return -EOPNOTSUPP;
272
273 tfm = chan->data;
274
b1e2b3ae
JH
275 get_random_bytes(&rpa->b[3], 3);
276
277 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
278 rpa->b[5] |= 0x40; /* Set second most significant bit */
279
280 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
281 if (err < 0)
282 return err;
283
284 BT_DBG("RPA %pMR", rpa);
285
286 return 0;
287}
288
e491eaf3
JH
289static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
290 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
291 bdaddr_t *ra, u8 res[16])
d22ef0bc
AB
292{
293 u8 p1[16], p2[16];
294 int err;
295
296 memset(p1, 0, 16);
297
298 /* p1 = pres || preq || _rat || _iat */
943a732a
JH
299 p1[0] = _iat;
300 p1[1] = _rat;
301 memcpy(p1 + 2, preq, 7);
302 memcpy(p1 + 9, pres, 7);
d22ef0bc
AB
303
304 /* p2 = padding || ia || ra */
943a732a
JH
305 memcpy(p2, ra, 6);
306 memcpy(p2 + 6, ia, 6);
307 memset(p2 + 12, 0, 4);
d22ef0bc
AB
308
309 /* res = r XOR p1 */
310 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
311
312 /* res = e(k, res) */
e491eaf3 313 err = smp_e(tfm_aes, k, res);
d22ef0bc
AB
314 if (err) {
315 BT_ERR("Encrypt data error");
316 return err;
317 }
318
319 /* res = res XOR p2 */
320 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
321
322 /* res = e(k, res) */
e491eaf3 323 err = smp_e(tfm_aes, k, res);
d22ef0bc
AB
324 if (err)
325 BT_ERR("Encrypt data error");
326
327 return err;
328}
329
e491eaf3
JH
330static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
331 u8 r2[16], u8 _r[16])
d22ef0bc
AB
332{
333 int err;
334
335 /* Just least significant octets from r1 and r2 are considered */
943a732a
JH
336 memcpy(_r, r2, 8);
337 memcpy(_r + 8, r1, 8);
d22ef0bc 338
e491eaf3 339 err = smp_e(tfm_aes, k, _r);
d22ef0bc
AB
340 if (err)
341 BT_ERR("Encrypt data error");
342
343 return err;
344}
345
5d88cc73 346static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
eb492e01 347{
5d88cc73 348 struct l2cap_chan *chan = conn->smp;
b68fda68 349 struct smp_chan *smp;
5d88cc73
JH
350 struct kvec iv[2];
351 struct msghdr msg;
eb492e01 352
5d88cc73
JH
353 if (!chan)
354 return;
eb492e01 355
5d88cc73 356 BT_DBG("code 0x%2.2x", code);
eb492e01 357
5d88cc73
JH
358 iv[0].iov_base = &code;
359 iv[0].iov_len = 1;
eb492e01 360
5d88cc73
JH
361 iv[1].iov_base = data;
362 iv[1].iov_len = len;
eb492e01 363
5d88cc73 364 memset(&msg, 0, sizeof(msg));
eb492e01 365
5d88cc73
JH
366 msg.msg_iov = (struct iovec *) &iv;
367 msg.msg_iovlen = 2;
eb492e01 368
5d88cc73 369 l2cap_chan_send(chan, &msg, 1 + len);
e2dcd113 370
b68fda68
JH
371 if (!chan->data)
372 return;
373
374 smp = chan->data;
375
376 cancel_delayed_work_sync(&smp->security_timer);
1b0921d6 377 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
eb492e01
AB
378}
379
d2eb9e10 380static u8 authreq_to_seclevel(u8 authreq)
2b64d153 381{
d2eb9e10
JH
382 if (authreq & SMP_AUTH_MITM) {
383 if (authreq & SMP_AUTH_SC)
384 return BT_SECURITY_FIPS;
385 else
386 return BT_SECURITY_HIGH;
387 } else {
2b64d153 388 return BT_SECURITY_MEDIUM;
d2eb9e10 389 }
2b64d153
BG
390}
391
392static __u8 seclevel_to_authreq(__u8 sec_level)
393{
394 switch (sec_level) {
d2eb9e10 395 case BT_SECURITY_FIPS:
2b64d153
BG
396 case BT_SECURITY_HIGH:
397 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
398 case BT_SECURITY_MEDIUM:
399 return SMP_AUTH_BONDING;
400 default:
401 return SMP_AUTH_NONE;
402 }
403}
404
b8e66eac 405static void build_pairing_cmd(struct l2cap_conn *conn,
f1560463
MH
406 struct smp_cmd_pairing *req,
407 struct smp_cmd_pairing *rsp, __u8 authreq)
b8e66eac 408{
5d88cc73
JH
409 struct l2cap_chan *chan = conn->smp;
410 struct smp_chan *smp = chan->data;
fd349c02
JH
411 struct hci_conn *hcon = conn->hcon;
412 struct hci_dev *hdev = hcon->hdev;
413 u8 local_dist = 0, remote_dist = 0;
54790f73 414
b6ae8457 415 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
7ee4ea36
MH
416 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
417 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
54790f73 418 authreq |= SMP_AUTH_BONDING;
2b64d153
BG
419 } else {
420 authreq &= ~SMP_AUTH_BONDING;
54790f73
VCG
421 }
422
fd349c02
JH
423 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
424 remote_dist |= SMP_DIST_ID_KEY;
425
863efaf2
JH
426 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
427 local_dist |= SMP_DIST_ID_KEY;
428
df8e1a4c
JH
429 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
430 if ((authreq & SMP_AUTH_SC) &&
431 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
432 local_dist |= SMP_DIST_LINK_KEY;
433 remote_dist |= SMP_DIST_LINK_KEY;
434 }
435 } else {
436 authreq &= ~SMP_AUTH_SC;
437 }
438
54790f73
VCG
439 if (rsp == NULL) {
440 req->io_capability = conn->hcon->io_capability;
441 req->oob_flag = SMP_OOB_NOT_PRESENT;
442 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
fd349c02
JH
443 req->init_key_dist = local_dist;
444 req->resp_key_dist = remote_dist;
0edb14de 445 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
fd349c02
JH
446
447 smp->remote_key_dist = remote_dist;
54790f73
VCG
448 return;
449 }
450
451 rsp->io_capability = conn->hcon->io_capability;
452 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
453 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
fd349c02
JH
454 rsp->init_key_dist = req->init_key_dist & remote_dist;
455 rsp->resp_key_dist = req->resp_key_dist & local_dist;
0edb14de 456 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
fd349c02
JH
457
458 smp->remote_key_dist = rsp->init_key_dist;
b8e66eac
VCG
459}
460
3158c50c
VCG
461static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
462{
5d88cc73
JH
463 struct l2cap_chan *chan = conn->smp;
464 struct smp_chan *smp = chan->data;
1c1def09 465
3158c50c 466 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
f1560463 467 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
3158c50c
VCG
468 return SMP_ENC_KEY_SIZE;
469
f7aa611a 470 smp->enc_key_size = max_key_size;
3158c50c
VCG
471
472 return 0;
473}
474
6f48e260
JH
475static void smp_chan_destroy(struct l2cap_conn *conn)
476{
477 struct l2cap_chan *chan = conn->smp;
478 struct smp_chan *smp = chan->data;
479 bool complete;
480
481 BUG_ON(!smp);
482
483 cancel_delayed_work_sync(&smp->security_timer);
6f48e260 484
6f48e260
JH
485 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
486 mgmt_smp_complete(conn->hcon, complete);
487
488 kfree(smp->csrk);
489 kfree(smp->slave_csrk);
490
491 crypto_free_blkcipher(smp->tfm_aes);
407cecf6 492 crypto_free_hash(smp->tfm_cmac);
6f48e260
JH
493
494 /* If pairing failed clean up any keys we might have */
495 if (!complete) {
496 if (smp->ltk) {
970d0f1b
JH
497 list_del_rcu(&smp->ltk->list);
498 kfree_rcu(smp->ltk, rcu);
6f48e260
JH
499 }
500
501 if (smp->slave_ltk) {
970d0f1b
JH
502 list_del_rcu(&smp->slave_ltk->list);
503 kfree_rcu(smp->slave_ltk, rcu);
6f48e260
JH
504 }
505
506 if (smp->remote_irk) {
adae20cb
JH
507 list_del_rcu(&smp->remote_irk->list);
508 kfree_rcu(smp->remote_irk, rcu);
6f48e260
JH
509 }
510 }
511
512 chan->data = NULL;
513 kfree(smp);
514 hci_conn_drop(conn->hcon);
515}
516
84794e11 517static void smp_failure(struct l2cap_conn *conn, u8 reason)
4f957a76 518{
bab73cb6 519 struct hci_conn *hcon = conn->hcon;
b68fda68 520 struct l2cap_chan *chan = conn->smp;
bab73cb6 521
84794e11 522 if (reason)
4f957a76 523 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
f1560463 524 &reason);
4f957a76 525
ce39fb4e 526 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
e1e930f5 527 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
f1c09c07 528
fc75cc86 529 if (chan->data)
f1c09c07 530 smp_chan_destroy(conn);
4f957a76
BG
531}
532
2b64d153
BG
533#define JUST_WORKS 0x00
534#define JUST_CFM 0x01
535#define REQ_PASSKEY 0x02
536#define CFM_PASSKEY 0x03
537#define REQ_OOB 0x04
538#define OVERLAP 0xFF
539
540static const u8 gen_method[5][5] = {
541 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
542 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
543 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
544 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
545 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
546};
547
581370cc
JH
548static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
549{
2bcd4003
JH
550 /* If either side has unknown io_caps, use JUST_CFM (which gets
551 * converted later to JUST_WORKS if we're initiators.
552 */
581370cc
JH
553 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
554 remote_io > SMP_IO_KEYBOARD_DISPLAY)
2bcd4003 555 return JUST_CFM;
581370cc
JH
556
557 return gen_method[remote_io][local_io];
558}
559
2b64d153
BG
560static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
561 u8 local_io, u8 remote_io)
562{
563 struct hci_conn *hcon = conn->hcon;
5d88cc73
JH
564 struct l2cap_chan *chan = conn->smp;
565 struct smp_chan *smp = chan->data;
2b64d153
BG
566 u8 method;
567 u32 passkey = 0;
568 int ret = 0;
569
570 /* Initialize key for JUST WORKS */
571 memset(smp->tk, 0, sizeof(smp->tk));
4a74d658 572 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
573
574 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
575
2bcd4003
JH
576 /* If neither side wants MITM, either "just" confirm an incoming
577 * request or use just-works for outgoing ones. The JUST_CFM
578 * will be converted to JUST_WORKS if necessary later in this
579 * function. If either side has MITM look up the method from the
580 * table.
581 */
581370cc 582 if (!(auth & SMP_AUTH_MITM))
2bcd4003 583 method = JUST_CFM;
2b64d153 584 else
581370cc 585 method = get_auth_method(smp, local_io, remote_io);
2b64d153 586
a82505c7 587 /* Don't confirm locally initiated pairing attempts */
4a74d658 588 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
a82505c7
JH
589 method = JUST_WORKS;
590
02f3e254
JH
591 /* Don't bother user space with no IO capabilities */
592 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
593 method = JUST_WORKS;
594
2b64d153
BG
595 /* If Just Works, Continue with Zero TK */
596 if (method == JUST_WORKS) {
4a74d658 597 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
598 return 0;
599 }
600
601 /* Not Just Works/Confirm results in MITM Authentication */
5eb596f5 602 if (method != JUST_CFM) {
4a74d658 603 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
5eb596f5
JH
604 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
605 hcon->pending_sec_level = BT_SECURITY_HIGH;
606 }
2b64d153
BG
607
608 /* If both devices have Keyoard-Display I/O, the master
609 * Confirms and the slave Enters the passkey.
610 */
611 if (method == OVERLAP) {
40bef302 612 if (hcon->role == HCI_ROLE_MASTER)
2b64d153
BG
613 method = CFM_PASSKEY;
614 else
615 method = REQ_PASSKEY;
616 }
617
01ad34d2 618 /* Generate random passkey. */
2b64d153 619 if (method == CFM_PASSKEY) {
943a732a 620 memset(smp->tk, 0, sizeof(smp->tk));
2b64d153
BG
621 get_random_bytes(&passkey, sizeof(passkey));
622 passkey %= 1000000;
943a732a 623 put_unaligned_le32(passkey, smp->tk);
2b64d153 624 BT_DBG("PassKey: %d", passkey);
4a74d658 625 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
626 }
627
2b64d153 628 if (method == REQ_PASSKEY)
ce39fb4e 629 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
272d90df 630 hcon->type, hcon->dst_type);
4eb65e66
JH
631 else if (method == JUST_CFM)
632 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
633 hcon->type, hcon->dst_type,
634 passkey, 1);
2b64d153 635 else
01ad34d2 636 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
272d90df 637 hcon->type, hcon->dst_type,
39adbffe 638 passkey, 0);
2b64d153 639
2b64d153
BG
640 return ret;
641}
642
1cc61144 643static u8 smp_confirm(struct smp_chan *smp)
8aab4757 644{
8aab4757 645 struct l2cap_conn *conn = smp->conn;
8aab4757
VCG
646 struct smp_cmd_pairing_confirm cp;
647 int ret;
8aab4757
VCG
648
649 BT_DBG("conn %p", conn);
650
e491eaf3 651 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
b1cd5fd9 652 conn->hcon->init_addr_type, &conn->hcon->init_addr,
943a732a
JH
653 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
654 cp.confirm_val);
1cc61144
JH
655 if (ret)
656 return SMP_UNSPECIFIED;
8aab4757 657
4a74d658 658 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
2b64d153 659
8aab4757
VCG
660 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
661
b28b4943
JH
662 if (conn->hcon->out)
663 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
664 else
665 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
666
1cc61144 667 return 0;
8aab4757
VCG
668}
669
861580a9 670static u8 smp_random(struct smp_chan *smp)
8aab4757 671{
8aab4757
VCG
672 struct l2cap_conn *conn = smp->conn;
673 struct hci_conn *hcon = conn->hcon;
861580a9 674 u8 confirm[16];
8aab4757
VCG
675 int ret;
676
ec70f36f 677 if (IS_ERR_OR_NULL(smp->tfm_aes))
861580a9 678 return SMP_UNSPECIFIED;
8aab4757
VCG
679
680 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
681
e491eaf3 682 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
b1cd5fd9 683 hcon->init_addr_type, &hcon->init_addr,
943a732a 684 hcon->resp_addr_type, &hcon->resp_addr, confirm);
861580a9
JH
685 if (ret)
686 return SMP_UNSPECIFIED;
8aab4757 687
8aab4757
VCG
688 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
689 BT_ERR("Pairing failed (confirmation values mismatch)");
861580a9 690 return SMP_CONFIRM_FAILED;
8aab4757
VCG
691 }
692
693 if (hcon->out) {
fe39c7b2
MH
694 u8 stk[16];
695 __le64 rand = 0;
696 __le16 ediv = 0;
8aab4757 697
e491eaf3 698 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
8aab4757 699
f7aa611a 700 memset(stk + smp->enc_key_size, 0,
04124681 701 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
8aab4757 702
861580a9
JH
703 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
704 return SMP_UNSPECIFIED;
8aab4757
VCG
705
706 hci_le_start_enc(hcon, ediv, rand, stk);
f7aa611a 707 hcon->enc_key_size = smp->enc_key_size;
fe59a05f 708 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
8aab4757 709 } else {
fff3490f 710 u8 stk[16], auth;
fe39c7b2
MH
711 __le64 rand = 0;
712 __le16 ediv = 0;
8aab4757 713
943a732a
JH
714 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
715 smp->prnd);
8aab4757 716
e491eaf3 717 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
8aab4757 718
f7aa611a 719 memset(stk + smp->enc_key_size, 0,
f1560463 720 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
8aab4757 721
fff3490f
JH
722 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
723 auth = 1;
724 else
725 auth = 0;
726
7d5843b7
JH
727 /* Even though there's no _SLAVE suffix this is the
728 * slave STK we're adding for later lookup (the master
729 * STK never needs to be stored).
730 */
ce39fb4e 731 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
2ceba539 732 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
8aab4757
VCG
733 }
734
861580a9 735 return 0;
8aab4757
VCG
736}
737
44f1a7ab
JH
738static void smp_notify_keys(struct l2cap_conn *conn)
739{
740 struct l2cap_chan *chan = conn->smp;
741 struct smp_chan *smp = chan->data;
742 struct hci_conn *hcon = conn->hcon;
743 struct hci_dev *hdev = hcon->hdev;
744 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
745 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
746 bool persistent;
747
748 if (smp->remote_irk) {
749 mgmt_new_irk(hdev, smp->remote_irk);
750 /* Now that user space can be considered to know the
751 * identity address track the connection based on it
752 * from now on.
753 */
754 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
755 hcon->dst_type = smp->remote_irk->addr_type;
f3d82d0c 756 queue_work(hdev->workqueue, &conn->id_addr_update_work);
44f1a7ab
JH
757
758 /* When receiving an indentity resolving key for
759 * a remote device that does not use a resolvable
760 * private address, just remove the key so that
761 * it is possible to use the controller white
762 * list for scanning.
763 *
764 * Userspace will have been told to not store
765 * this key at this point. So it is safe to
766 * just remove it.
767 */
768 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
adae20cb
JH
769 list_del_rcu(&smp->remote_irk->list);
770 kfree_rcu(smp->remote_irk, rcu);
44f1a7ab
JH
771 smp->remote_irk = NULL;
772 }
773 }
774
775 /* The LTKs and CSRKs should be persistent only if both sides
776 * had the bonding bit set in their authentication requests.
777 */
778 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
779
780 if (smp->csrk) {
781 smp->csrk->bdaddr_type = hcon->dst_type;
782 bacpy(&smp->csrk->bdaddr, &hcon->dst);
783 mgmt_new_csrk(hdev, smp->csrk, persistent);
784 }
785
786 if (smp->slave_csrk) {
787 smp->slave_csrk->bdaddr_type = hcon->dst_type;
788 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
789 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
790 }
791
792 if (smp->ltk) {
793 smp->ltk->bdaddr_type = hcon->dst_type;
794 bacpy(&smp->ltk->bdaddr, &hcon->dst);
795 mgmt_new_ltk(hdev, smp->ltk, persistent);
796 }
797
798 if (smp->slave_ltk) {
799 smp->slave_ltk->bdaddr_type = hcon->dst_type;
800 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
801 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
802 }
803}
804
b28b4943
JH
805static void smp_allow_key_dist(struct smp_chan *smp)
806{
807 /* Allow the first expected phase 3 PDU. The rest of the PDUs
808 * will be allowed in each PDU handler to ensure we receive
809 * them in the correct order.
810 */
811 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
812 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
813 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
814 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
815 else if (smp->remote_key_dist & SMP_DIST_SIGN)
816 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
817}
818
d6268e86 819static void smp_distribute_keys(struct smp_chan *smp)
44f1a7ab
JH
820{
821 struct smp_cmd_pairing *req, *rsp;
86d1407c 822 struct l2cap_conn *conn = smp->conn;
44f1a7ab
JH
823 struct hci_conn *hcon = conn->hcon;
824 struct hci_dev *hdev = hcon->hdev;
825 __u8 *keydist;
826
827 BT_DBG("conn %p", conn);
828
44f1a7ab
JH
829 rsp = (void *) &smp->prsp[1];
830
831 /* The responder sends its keys first */
b28b4943
JH
832 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
833 smp_allow_key_dist(smp);
86d1407c 834 return;
b28b4943 835 }
44f1a7ab
JH
836
837 req = (void *) &smp->preq[1];
838
839 if (hcon->out) {
840 keydist = &rsp->init_key_dist;
841 *keydist &= req->init_key_dist;
842 } else {
843 keydist = &rsp->resp_key_dist;
844 *keydist &= req->resp_key_dist;
845 }
846
847 BT_DBG("keydist 0x%x", *keydist);
848
849 if (*keydist & SMP_DIST_ENC_KEY) {
850 struct smp_cmd_encrypt_info enc;
851 struct smp_cmd_master_ident ident;
852 struct smp_ltk *ltk;
853 u8 authenticated;
854 __le16 ediv;
855 __le64 rand;
856
857 get_random_bytes(enc.ltk, sizeof(enc.ltk));
858 get_random_bytes(&ediv, sizeof(ediv));
859 get_random_bytes(&rand, sizeof(rand));
860
861 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
862
863 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
864 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
865 SMP_LTK_SLAVE, authenticated, enc.ltk,
866 smp->enc_key_size, ediv, rand);
867 smp->slave_ltk = ltk;
868
869 ident.ediv = ediv;
870 ident.rand = rand;
871
872 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
873
874 *keydist &= ~SMP_DIST_ENC_KEY;
875 }
876
877 if (*keydist & SMP_DIST_ID_KEY) {
878 struct smp_cmd_ident_addr_info addrinfo;
879 struct smp_cmd_ident_info idinfo;
880
881 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
882
883 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
884
885 /* The hci_conn contains the local identity address
886 * after the connection has been established.
887 *
888 * This is true even when the connection has been
889 * established using a resolvable random address.
890 */
891 bacpy(&addrinfo.bdaddr, &hcon->src);
892 addrinfo.addr_type = hcon->src_type;
893
894 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
895 &addrinfo);
896
897 *keydist &= ~SMP_DIST_ID_KEY;
898 }
899
900 if (*keydist & SMP_DIST_SIGN) {
901 struct smp_cmd_sign_info sign;
902 struct smp_csrk *csrk;
903
904 /* Generate a new random key */
905 get_random_bytes(sign.csrk, sizeof(sign.csrk));
906
907 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
908 if (csrk) {
909 csrk->master = 0x00;
910 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
911 }
912 smp->slave_csrk = csrk;
913
914 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
915
916 *keydist &= ~SMP_DIST_SIGN;
917 }
918
919 /* If there are still keys to be received wait for them */
b28b4943
JH
920 if (smp->remote_key_dist & KEY_DIST_MASK) {
921 smp_allow_key_dist(smp);
86d1407c 922 return;
b28b4943 923 }
44f1a7ab 924
44f1a7ab
JH
925 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
926 smp_notify_keys(conn);
927
928 smp_chan_destroy(conn);
44f1a7ab
JH
929}
930
b68fda68
JH
931static void smp_timeout(struct work_struct *work)
932{
933 struct smp_chan *smp = container_of(work, struct smp_chan,
934 security_timer.work);
935 struct l2cap_conn *conn = smp->conn;
936
937 BT_DBG("conn %p", conn);
938
1e91c29e 939 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
b68fda68
JH
940}
941
8aab4757
VCG
942static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
943{
5d88cc73 944 struct l2cap_chan *chan = conn->smp;
8aab4757
VCG
945 struct smp_chan *smp;
946
f1560463 947 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
fc75cc86 948 if (!smp)
8aab4757
VCG
949 return NULL;
950
6a7bd103
JH
951 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
952 if (IS_ERR(smp->tfm_aes)) {
953 BT_ERR("Unable to create ECB crypto context");
954 kfree(smp);
955 return NULL;
956 }
957
407cecf6
JH
958 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
959 if (IS_ERR(smp->tfm_cmac)) {
960 BT_ERR("Unable to create CMAC crypto context");
961 crypto_free_blkcipher(smp->tfm_aes);
962 kfree(smp);
963 return NULL;
964 }
965
8aab4757 966 smp->conn = conn;
5d88cc73 967 chan->data = smp;
8aab4757 968
b28b4943
JH
969 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
970
b68fda68
JH
971 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
972
8aab4757
VCG
973 hci_conn_hold(conn->hcon);
974
975 return smp;
976}
977
2b64d153
BG
978int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
979{
b10e8017 980 struct l2cap_conn *conn = hcon->l2cap_data;
5d88cc73 981 struct l2cap_chan *chan;
2b64d153
BG
982 struct smp_chan *smp;
983 u32 value;
fc75cc86 984 int err;
2b64d153
BG
985
986 BT_DBG("");
987
fc75cc86 988 if (!conn)
2b64d153
BG
989 return -ENOTCONN;
990
5d88cc73
JH
991 chan = conn->smp;
992 if (!chan)
993 return -ENOTCONN;
994
fc75cc86
JH
995 l2cap_chan_lock(chan);
996 if (!chan->data) {
997 err = -ENOTCONN;
998 goto unlock;
999 }
1000
5d88cc73 1001 smp = chan->data;
2b64d153
BG
1002
1003 switch (mgmt_op) {
1004 case MGMT_OP_USER_PASSKEY_REPLY:
1005 value = le32_to_cpu(passkey);
943a732a 1006 memset(smp->tk, 0, sizeof(smp->tk));
2b64d153 1007 BT_DBG("PassKey: %d", value);
943a732a 1008 put_unaligned_le32(value, smp->tk);
2b64d153
BG
1009 /* Fall Through */
1010 case MGMT_OP_USER_CONFIRM_REPLY:
4a74d658 1011 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
1012 break;
1013 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1014 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
84794e11 1015 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
fc75cc86
JH
1016 err = 0;
1017 goto unlock;
2b64d153 1018 default:
84794e11 1019 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
fc75cc86
JH
1020 err = -EOPNOTSUPP;
1021 goto unlock;
2b64d153
BG
1022 }
1023
fc75cc86
JH
1024 err = 0;
1025
2b64d153 1026 /* If it is our turn to send Pairing Confirm, do so now */
1cc61144
JH
1027 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1028 u8 rsp = smp_confirm(smp);
1029 if (rsp)
1030 smp_failure(conn, rsp);
1031 }
2b64d153 1032
fc75cc86
JH
1033unlock:
1034 l2cap_chan_unlock(chan);
1035 return err;
2b64d153
BG
1036}
1037
da85e5e5 1038static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1039{
3158c50c 1040 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
fc75cc86 1041 struct l2cap_chan *chan = conn->smp;
b3c6410b 1042 struct hci_dev *hdev = conn->hcon->hdev;
8aab4757 1043 struct smp_chan *smp;
c7262e71 1044 u8 key_size, auth, sec_level;
8aab4757 1045 int ret;
88ba43b6
AB
1046
1047 BT_DBG("conn %p", conn);
1048
c46b98be 1049 if (skb->len < sizeof(*req))
38e4a915 1050 return SMP_INVALID_PARAMS;
c46b98be 1051
40bef302 1052 if (conn->hcon->role != HCI_ROLE_SLAVE)
2b64d153
BG
1053 return SMP_CMD_NOTSUPP;
1054
fc75cc86 1055 if (!chan->data)
8aab4757 1056 smp = smp_chan_create(conn);
fc75cc86 1057 else
5d88cc73 1058 smp = chan->data;
8aab4757 1059
d08fd0e7
AE
1060 if (!smp)
1061 return SMP_UNSPECIFIED;
d26a2345 1062
c05b9339 1063 /* We didn't start the pairing, so match remote */
0edb14de 1064 auth = req->auth_req & AUTH_REQ_MASK(hdev);
c05b9339 1065
b6ae8457 1066 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
c05b9339 1067 (auth & SMP_AUTH_BONDING))
b3c6410b
JH
1068 return SMP_PAIRING_NOTSUPP;
1069
1c1def09
VCG
1070 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1071 memcpy(&smp->preq[1], req, sizeof(*req));
3158c50c 1072 skb_pull(skb, sizeof(*req));
88ba43b6 1073
5be5e275 1074 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1afc2a1a
JH
1075 sec_level = BT_SECURITY_MEDIUM;
1076 else
1077 sec_level = authreq_to_seclevel(auth);
1078
c7262e71
JH
1079 if (sec_level > conn->hcon->pending_sec_level)
1080 conn->hcon->pending_sec_level = sec_level;
fdde0a26 1081
49c922bb 1082 /* If we need MITM check that it can be achieved */
2ed8f65c
JH
1083 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1084 u8 method;
1085
1086 method = get_auth_method(smp, conn->hcon->io_capability,
1087 req->io_capability);
1088 if (method == JUST_WORKS || method == JUST_CFM)
1089 return SMP_AUTH_REQUIREMENTS;
1090 }
1091
2b64d153 1092 build_pairing_cmd(conn, req, &rsp, auth);
3158c50c 1093
65668776
JH
1094 if (rsp.auth_req & SMP_AUTH_SC)
1095 set_bit(SMP_FLAG_SC, &smp->flags);
1096
3158c50c
VCG
1097 key_size = min(req->max_key_size, rsp.max_key_size);
1098 if (check_enc_key_size(conn, key_size))
1099 return SMP_ENC_KEY_SIZE;
88ba43b6 1100
e84a6b13 1101 get_random_bytes(smp->prnd, sizeof(smp->prnd));
8aab4757 1102
1c1def09
VCG
1103 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1104 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
f01ead31 1105
3158c50c 1106 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
3b19146d
JH
1107
1108 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1109
1110 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1111 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1112 /* Clear bits which are generated but not distributed */
1113 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1114 /* Wait for Public Key from Initiating Device */
1115 return 0;
1116 } else {
1117 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1118 }
da85e5e5 1119
2b64d153
BG
1120 /* Request setup of TK */
1121 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1122 if (ret)
1123 return SMP_UNSPECIFIED;
1124
da85e5e5 1125 return 0;
88ba43b6
AB
1126}
1127
3b19146d
JH
1128static u8 sc_send_public_key(struct smp_chan *smp)
1129{
1130 BT_DBG("");
1131
1132 /* Generate local key pair for Secure Connections */
1133 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1134 return SMP_UNSPECIFIED;
1135
1136 BT_DBG("Local Public Key X: %32phN", smp->local_pk);
1137 BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1138 BT_DBG("Local Private Key: %32phN", smp->local_sk);
1139
1140 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1141
1142 return 0;
1143}
1144
da85e5e5 1145static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1146{
3158c50c 1147 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
5d88cc73
JH
1148 struct l2cap_chan *chan = conn->smp;
1149 struct smp_chan *smp = chan->data;
0edb14de 1150 struct hci_dev *hdev = conn->hcon->hdev;
3a7dbfb8 1151 u8 key_size, auth;
7d24ddcc 1152 int ret;
88ba43b6
AB
1153
1154 BT_DBG("conn %p", conn);
1155
c46b98be 1156 if (skb->len < sizeof(*rsp))
38e4a915 1157 return SMP_INVALID_PARAMS;
c46b98be 1158
40bef302 1159 if (conn->hcon->role != HCI_ROLE_MASTER)
2b64d153
BG
1160 return SMP_CMD_NOTSUPP;
1161
3158c50c
VCG
1162 skb_pull(skb, sizeof(*rsp));
1163
1c1def09 1164 req = (void *) &smp->preq[1];
da85e5e5 1165
3158c50c
VCG
1166 key_size = min(req->max_key_size, rsp->max_key_size);
1167 if (check_enc_key_size(conn, key_size))
1168 return SMP_ENC_KEY_SIZE;
1169
0edb14de 1170 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
c05b9339 1171
65668776
JH
1172 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1173 set_bit(SMP_FLAG_SC, &smp->flags);
d2eb9e10
JH
1174 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1175 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
65668776 1176
49c922bb 1177 /* If we need MITM check that it can be achieved */
2ed8f65c
JH
1178 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1179 u8 method;
1180
1181 method = get_auth_method(smp, req->io_capability,
1182 rsp->io_capability);
1183 if (method == JUST_WORKS || method == JUST_CFM)
1184 return SMP_AUTH_REQUIREMENTS;
1185 }
1186
e84a6b13 1187 get_random_bytes(smp->prnd, sizeof(smp->prnd));
7d24ddcc 1188
8aab4757
VCG
1189 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1190 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
7d24ddcc 1191
fdcc4bec
JH
1192 /* Update remote key distribution in case the remote cleared
1193 * some bits that we had enabled in our request.
1194 */
1195 smp->remote_key_dist &= rsp->resp_key_dist;
1196
3b19146d
JH
1197 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1198 /* Clear bits which are generated but not distributed */
1199 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1200 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1201 return sc_send_public_key(smp);
1202 }
1203
c05b9339 1204 auth |= req->auth_req;
2b64d153 1205
476585ec 1206 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
2b64d153
BG
1207 if (ret)
1208 return SMP_UNSPECIFIED;
1209
4a74d658 1210 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
2b64d153
BG
1211
1212 /* Can't compose response until we have been confirmed */
4a74d658 1213 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1cc61144 1214 return smp_confirm(smp);
da85e5e5
VCG
1215
1216 return 0;
88ba43b6
AB
1217}
1218
da85e5e5 1219static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1220{
5d88cc73
JH
1221 struct l2cap_chan *chan = conn->smp;
1222 struct smp_chan *smp = chan->data;
7d24ddcc 1223
88ba43b6
AB
1224 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1225
c46b98be 1226 if (skb->len < sizeof(smp->pcnf))
38e4a915 1227 return SMP_INVALID_PARAMS;
c46b98be 1228
1c1def09
VCG
1229 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1230 skb_pull(skb, sizeof(smp->pcnf));
88ba43b6 1231
b28b4943 1232 if (conn->hcon->out) {
943a732a
JH
1233 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1234 smp->prnd);
b28b4943
JH
1235 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1236 return 0;
1237 }
1238
1239 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1cc61144 1240 return smp_confirm(smp);
943a732a 1241 else
4a74d658 1242 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
da85e5e5
VCG
1243
1244 return 0;
88ba43b6
AB
1245}
1246
da85e5e5 1247static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1248{
5d88cc73
JH
1249 struct l2cap_chan *chan = conn->smp;
1250 struct smp_chan *smp = chan->data;
7d24ddcc 1251
8aab4757 1252 BT_DBG("conn %p", conn);
3158c50c 1253
c46b98be 1254 if (skb->len < sizeof(smp->rrnd))
38e4a915 1255 return SMP_INVALID_PARAMS;
c46b98be 1256
943a732a 1257 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
8aab4757 1258 skb_pull(skb, sizeof(smp->rrnd));
e7e62c85 1259
861580a9 1260 return smp_random(smp);
88ba43b6
AB
1261}
1262
f81cd823 1263static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
988c5997 1264{
c9839a11 1265 struct smp_ltk *key;
988c5997
VCG
1266 struct hci_conn *hcon = conn->hcon;
1267
f3a73d97 1268 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
988c5997 1269 if (!key)
f81cd823 1270 return false;
988c5997 1271
a6f7833c 1272 if (smp_ltk_sec_level(key) < sec_level)
f81cd823 1273 return false;
4dab7864 1274
51a8efd7 1275 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
f81cd823 1276 return true;
988c5997 1277
c9839a11
VCG
1278 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1279 hcon->enc_key_size = key->enc_size;
988c5997 1280
fe59a05f
JH
1281 /* We never store STKs for master role, so clear this flag */
1282 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1283
f81cd823 1284 return true;
988c5997 1285}
f1560463 1286
35dc6f83
JH
1287bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1288 enum smp_key_pref key_pref)
854f4727
JH
1289{
1290 if (sec_level == BT_SECURITY_LOW)
1291 return true;
1292
35dc6f83
JH
1293 /* If we're encrypted with an STK but the caller prefers using
1294 * LTK claim insufficient security. This way we allow the
1295 * connection to be re-encrypted with an LTK, even if the LTK
1296 * provides the same level of security. Only exception is if we
1297 * don't have an LTK (e.g. because of key distribution bits).
9ab65d60 1298 */
35dc6f83
JH
1299 if (key_pref == SMP_USE_LTK &&
1300 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
f3a73d97 1301 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
9ab65d60
JH
1302 return false;
1303
854f4727
JH
1304 if (hcon->sec_level >= sec_level)
1305 return true;
1306
1307 return false;
1308}
1309
da85e5e5 1310static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6
AB
1311{
1312 struct smp_cmd_security_req *rp = (void *) skb->data;
1313 struct smp_cmd_pairing cp;
f1cb9af5 1314 struct hci_conn *hcon = conn->hcon;
0edb14de 1315 struct hci_dev *hdev = hcon->hdev;
8aab4757 1316 struct smp_chan *smp;
c05b9339 1317 u8 sec_level, auth;
88ba43b6
AB
1318
1319 BT_DBG("conn %p", conn);
1320
c46b98be 1321 if (skb->len < sizeof(*rp))
38e4a915 1322 return SMP_INVALID_PARAMS;
c46b98be 1323
40bef302 1324 if (hcon->role != HCI_ROLE_MASTER)
86ca9eac
JH
1325 return SMP_CMD_NOTSUPP;
1326
0edb14de 1327 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
c05b9339 1328
5be5e275 1329 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1afc2a1a
JH
1330 sec_level = BT_SECURITY_MEDIUM;
1331 else
1332 sec_level = authreq_to_seclevel(auth);
1333
35dc6f83 1334 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
854f4727
JH
1335 return 0;
1336
c7262e71
JH
1337 if (sec_level > hcon->pending_sec_level)
1338 hcon->pending_sec_level = sec_level;
feb45eb5 1339
4dab7864 1340 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
988c5997
VCG
1341 return 0;
1342
8aab4757 1343 smp = smp_chan_create(conn);
c29d2444
JH
1344 if (!smp)
1345 return SMP_UNSPECIFIED;
d26a2345 1346
b6ae8457 1347 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
c05b9339 1348 (auth & SMP_AUTH_BONDING))
616d55be
JH
1349 return SMP_PAIRING_NOTSUPP;
1350
88ba43b6 1351 skb_pull(skb, sizeof(*rp));
88ba43b6 1352
da85e5e5 1353 memset(&cp, 0, sizeof(cp));
c05b9339 1354 build_pairing_cmd(conn, &cp, NULL, auth);
88ba43b6 1355
1c1def09
VCG
1356 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1357 memcpy(&smp->preq[1], &cp, sizeof(cp));
f01ead31 1358
88ba43b6 1359 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
b28b4943 1360 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
f1cb9af5 1361
da85e5e5 1362 return 0;
88ba43b6
AB
1363}
1364
cc110922 1365int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
eb492e01 1366{
cc110922 1367 struct l2cap_conn *conn = hcon->l2cap_data;
c68b7f12 1368 struct l2cap_chan *chan;
0a66cf20 1369 struct smp_chan *smp;
2b64d153 1370 __u8 authreq;
fc75cc86 1371 int ret;
eb492e01 1372
3a0259bb
VCG
1373 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1374
0a66cf20
JH
1375 /* This may be NULL if there's an unexpected disconnection */
1376 if (!conn)
1377 return 1;
1378
c68b7f12
JH
1379 chan = conn->smp;
1380
757aee0f 1381 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
2e65c9d2
AG
1382 return 1;
1383
35dc6f83 1384 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
eb492e01 1385 return 1;
f1cb9af5 1386
c7262e71
JH
1387 if (sec_level > hcon->pending_sec_level)
1388 hcon->pending_sec_level = sec_level;
1389
40bef302 1390 if (hcon->role == HCI_ROLE_MASTER)
c7262e71
JH
1391 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1392 return 0;
d26a2345 1393
fc75cc86
JH
1394 l2cap_chan_lock(chan);
1395
1396 /* If SMP is already in progress ignore this request */
1397 if (chan->data) {
1398 ret = 0;
1399 goto unlock;
1400 }
d26a2345 1401
8aab4757 1402 smp = smp_chan_create(conn);
fc75cc86
JH
1403 if (!smp) {
1404 ret = 1;
1405 goto unlock;
1406 }
2b64d153
BG
1407
1408 authreq = seclevel_to_authreq(sec_level);
d26a2345 1409
d2eb9e10
JH
1410 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1411 authreq |= SMP_AUTH_SC;
1412
79897d20
JH
1413 /* Require MITM if IO Capability allows or the security level
1414 * requires it.
2e233644 1415 */
79897d20 1416 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
c7262e71 1417 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
2e233644
JH
1418 authreq |= SMP_AUTH_MITM;
1419
40bef302 1420 if (hcon->role == HCI_ROLE_MASTER) {
d26a2345 1421 struct smp_cmd_pairing cp;
f01ead31 1422
2b64d153 1423 build_pairing_cmd(conn, &cp, NULL, authreq);
1c1def09
VCG
1424 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1425 memcpy(&smp->preq[1], &cp, sizeof(cp));
f01ead31 1426
eb492e01 1427 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
b28b4943 1428 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
eb492e01
AB
1429 } else {
1430 struct smp_cmd_security_req cp;
2b64d153 1431 cp.auth_req = authreq;
eb492e01 1432 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
b28b4943 1433 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
eb492e01
AB
1434 }
1435
4a74d658 1436 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
fc75cc86 1437 ret = 0;
edca792c 1438
fc75cc86
JH
1439unlock:
1440 l2cap_chan_unlock(chan);
1441 return ret;
eb492e01
AB
1442}
1443
7034b911
VCG
1444static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1445{
16b90839 1446 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
5d88cc73
JH
1447 struct l2cap_chan *chan = conn->smp;
1448 struct smp_chan *smp = chan->data;
16b90839 1449
c46b98be
JH
1450 BT_DBG("conn %p", conn);
1451
1452 if (skb->len < sizeof(*rp))
38e4a915 1453 return SMP_INVALID_PARAMS;
c46b98be 1454
b28b4943 1455 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
6131ddc8 1456
16b90839
VCG
1457 skb_pull(skb, sizeof(*rp));
1458
1c1def09 1459 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
16b90839 1460
7034b911
VCG
1461 return 0;
1462}
1463
1464static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1465{
16b90839 1466 struct smp_cmd_master_ident *rp = (void *) skb->data;
5d88cc73
JH
1467 struct l2cap_chan *chan = conn->smp;
1468 struct smp_chan *smp = chan->data;
c9839a11
VCG
1469 struct hci_dev *hdev = conn->hcon->hdev;
1470 struct hci_conn *hcon = conn->hcon;
23d0e128 1471 struct smp_ltk *ltk;
c9839a11 1472 u8 authenticated;
16b90839 1473
c46b98be
JH
1474 BT_DBG("conn %p", conn);
1475
1476 if (skb->len < sizeof(*rp))
38e4a915 1477 return SMP_INVALID_PARAMS;
c46b98be 1478
9747a9f3
JH
1479 /* Mark the information as received */
1480 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1481
b28b4943
JH
1482 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1483 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
196332f5
JH
1484 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1485 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
b28b4943 1486
16b90839 1487 skb_pull(skb, sizeof(*rp));
7034b911 1488
ce39fb4e 1489 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
2ceba539 1490 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
23d0e128
JH
1491 authenticated, smp->tk, smp->enc_key_size,
1492 rp->ediv, rp->rand);
1493 smp->ltk = ltk;
c6e81e9a 1494 if (!(smp->remote_key_dist & KEY_DIST_MASK))
d6268e86 1495 smp_distribute_keys(smp);
7034b911
VCG
1496
1497 return 0;
1498}
1499
fd349c02
JH
1500static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1501{
1502 struct smp_cmd_ident_info *info = (void *) skb->data;
5d88cc73
JH
1503 struct l2cap_chan *chan = conn->smp;
1504 struct smp_chan *smp = chan->data;
fd349c02
JH
1505
1506 BT_DBG("");
1507
1508 if (skb->len < sizeof(*info))
38e4a915 1509 return SMP_INVALID_PARAMS;
fd349c02 1510
b28b4943 1511 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
6131ddc8 1512
fd349c02
JH
1513 skb_pull(skb, sizeof(*info));
1514
1515 memcpy(smp->irk, info->irk, 16);
1516
1517 return 0;
1518}
1519
1520static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1521 struct sk_buff *skb)
1522{
1523 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
5d88cc73
JH
1524 struct l2cap_chan *chan = conn->smp;
1525 struct smp_chan *smp = chan->data;
fd349c02
JH
1526 struct hci_conn *hcon = conn->hcon;
1527 bdaddr_t rpa;
1528
1529 BT_DBG("");
1530
1531 if (skb->len < sizeof(*info))
38e4a915 1532 return SMP_INVALID_PARAMS;
fd349c02 1533
9747a9f3
JH
1534 /* Mark the information as received */
1535 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1536
b28b4943
JH
1537 if (smp->remote_key_dist & SMP_DIST_SIGN)
1538 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1539
fd349c02
JH
1540 skb_pull(skb, sizeof(*info));
1541
a9a58f86
JH
1542 /* Strictly speaking the Core Specification (4.1) allows sending
1543 * an empty address which would force us to rely on just the IRK
1544 * as "identity information". However, since such
1545 * implementations are not known of and in order to not over
1546 * complicate our implementation, simply pretend that we never
1547 * received an IRK for such a device.
1548 */
1549 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1550 BT_ERR("Ignoring IRK with no identity address");
31dd624e 1551 goto distribute;
a9a58f86
JH
1552 }
1553
fd349c02
JH
1554 bacpy(&smp->id_addr, &info->bdaddr);
1555 smp->id_addr_type = info->addr_type;
1556
1557 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1558 bacpy(&rpa, &hcon->dst);
1559 else
1560 bacpy(&rpa, BDADDR_ANY);
1561
23d0e128
JH
1562 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1563 smp->id_addr_type, smp->irk, &rpa);
fd349c02 1564
31dd624e 1565distribute:
c6e81e9a
JH
1566 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1567 smp_distribute_keys(smp);
fd349c02
JH
1568
1569 return 0;
1570}
1571
7ee4ea36
MH
1572static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1573{
1574 struct smp_cmd_sign_info *rp = (void *) skb->data;
5d88cc73
JH
1575 struct l2cap_chan *chan = conn->smp;
1576 struct smp_chan *smp = chan->data;
7ee4ea36
MH
1577 struct smp_csrk *csrk;
1578
1579 BT_DBG("conn %p", conn);
1580
1581 if (skb->len < sizeof(*rp))
38e4a915 1582 return SMP_INVALID_PARAMS;
7ee4ea36 1583
7ee4ea36
MH
1584 /* Mark the information as received */
1585 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1586
1587 skb_pull(skb, sizeof(*rp));
1588
7ee4ea36
MH
1589 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1590 if (csrk) {
1591 csrk->master = 0x01;
1592 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1593 }
1594 smp->csrk = csrk;
d6268e86 1595 smp_distribute_keys(smp);
7ee4ea36
MH
1596
1597 return 0;
1598}
1599
d8f8edbe
JH
1600static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
1601{
1602 struct smp_cmd_public_key *key = (void *) skb->data;
1603 struct hci_conn *hcon = conn->hcon;
1604 struct l2cap_chan *chan = conn->smp;
1605 struct smp_chan *smp = chan->data;
cbbbe3e2 1606 struct smp_cmd_pairing_confirm cfm;
d8f8edbe
JH
1607 int err;
1608
1609 BT_DBG("conn %p", conn);
1610
1611 if (skb->len < sizeof(*key))
1612 return SMP_INVALID_PARAMS;
1613
1614 memcpy(smp->remote_pk, key, 64);
1615
1616 /* Non-initiating device sends its public key after receiving
1617 * the key from the initiating device.
1618 */
1619 if (!hcon->out) {
1620 err = sc_send_public_key(smp);
1621 if (err)
1622 return err;
1623 }
1624
1625 BT_DBG("Remote Public Key X: %32phN", smp->remote_pk);
1626 BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
1627
1628 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
1629 return SMP_UNSPECIFIED;
1630
1631 BT_DBG("DHKey %32phN", smp->dhkey);
1632
1633 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
1634
cbbbe3e2
JH
1635 /* The Initiating device waits for the non-initiating device to
1636 * send the confirm value.
1637 */
1638 if (conn->hcon->out)
1639 return 0;
1640
1641 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
1642 0, cfm.confirm_val);
1643 if (err)
1644 return SMP_UNSPECIFIED;
1645
1646 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1647 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1648
d8f8edbe
JH
1649 return 0;
1650}
1651
4befb867 1652static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
eb492e01 1653{
5d88cc73 1654 struct l2cap_conn *conn = chan->conn;
7b9899db 1655 struct hci_conn *hcon = conn->hcon;
b28b4943 1656 struct smp_chan *smp;
92381f5c 1657 __u8 code, reason;
eb492e01
AB
1658 int err = 0;
1659
7b9899db
MH
1660 if (hcon->type != LE_LINK) {
1661 kfree_skb(skb);
3432711f 1662 return 0;
7b9899db
MH
1663 }
1664
8ae9b984 1665 if (skb->len < 1)
92381f5c 1666 return -EILSEQ;
92381f5c 1667
06ae3314 1668 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
2e65c9d2
AG
1669 reason = SMP_PAIRING_NOTSUPP;
1670 goto done;
1671 }
1672
92381f5c 1673 code = skb->data[0];
eb492e01
AB
1674 skb_pull(skb, sizeof(code));
1675
b28b4943
JH
1676 smp = chan->data;
1677
1678 if (code > SMP_CMD_MAX)
1679 goto drop;
1680
24bd0bd9 1681 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
b28b4943
JH
1682 goto drop;
1683
1684 /* If we don't have a context the only allowed commands are
1685 * pairing request and security request.
8cf9fa12 1686 */
b28b4943
JH
1687 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1688 goto drop;
8cf9fa12 1689
eb492e01
AB
1690 switch (code) {
1691 case SMP_CMD_PAIRING_REQ:
da85e5e5 1692 reason = smp_cmd_pairing_req(conn, skb);
eb492e01
AB
1693 break;
1694
1695 case SMP_CMD_PAIRING_FAIL:
84794e11 1696 smp_failure(conn, 0);
da85e5e5 1697 err = -EPERM;
eb492e01
AB
1698 break;
1699
1700 case SMP_CMD_PAIRING_RSP:
da85e5e5 1701 reason = smp_cmd_pairing_rsp(conn, skb);
88ba43b6
AB
1702 break;
1703
1704 case SMP_CMD_SECURITY_REQ:
da85e5e5 1705 reason = smp_cmd_security_req(conn, skb);
88ba43b6
AB
1706 break;
1707
eb492e01 1708 case SMP_CMD_PAIRING_CONFIRM:
da85e5e5 1709 reason = smp_cmd_pairing_confirm(conn, skb);
88ba43b6
AB
1710 break;
1711
eb492e01 1712 case SMP_CMD_PAIRING_RANDOM:
da85e5e5 1713 reason = smp_cmd_pairing_random(conn, skb);
88ba43b6
AB
1714 break;
1715
eb492e01 1716 case SMP_CMD_ENCRYPT_INFO:
7034b911
VCG
1717 reason = smp_cmd_encrypt_info(conn, skb);
1718 break;
1719
eb492e01 1720 case SMP_CMD_MASTER_IDENT:
7034b911
VCG
1721 reason = smp_cmd_master_ident(conn, skb);
1722 break;
1723
eb492e01 1724 case SMP_CMD_IDENT_INFO:
fd349c02
JH
1725 reason = smp_cmd_ident_info(conn, skb);
1726 break;
1727
eb492e01 1728 case SMP_CMD_IDENT_ADDR_INFO:
fd349c02
JH
1729 reason = smp_cmd_ident_addr_info(conn, skb);
1730 break;
1731
eb492e01 1732 case SMP_CMD_SIGN_INFO:
7ee4ea36 1733 reason = smp_cmd_sign_info(conn, skb);
7034b911
VCG
1734 break;
1735
d8f8edbe
JH
1736 case SMP_CMD_PUBLIC_KEY:
1737 reason = smp_cmd_public_key(conn, skb);
1738 break;
1739
eb492e01
AB
1740 default:
1741 BT_DBG("Unknown command code 0x%2.2x", code);
eb492e01 1742 reason = SMP_CMD_NOTSUPP;
3a0259bb 1743 goto done;
eb492e01
AB
1744 }
1745
3a0259bb 1746done:
9b7b18ef
JH
1747 if (!err) {
1748 if (reason)
1749 smp_failure(conn, reason);
8ae9b984 1750 kfree_skb(skb);
9b7b18ef
JH
1751 }
1752
eb492e01 1753 return err;
b28b4943
JH
1754
1755drop:
1756 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1757 code, &hcon->dst);
1758 kfree_skb(skb);
1759 return 0;
eb492e01 1760}
7034b911 1761
70db83c4
JH
1762static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1763{
1764 struct l2cap_conn *conn = chan->conn;
1765
1766 BT_DBG("chan %p", chan);
1767
fc75cc86 1768 if (chan->data)
5d88cc73 1769 smp_chan_destroy(conn);
5d88cc73 1770
70db83c4
JH
1771 conn->smp = NULL;
1772 l2cap_chan_put(chan);
1773}
1774
44f1a7ab
JH
1775static void smp_resume_cb(struct l2cap_chan *chan)
1776{
b68fda68 1777 struct smp_chan *smp = chan->data;
44f1a7ab
JH
1778 struct l2cap_conn *conn = chan->conn;
1779 struct hci_conn *hcon = conn->hcon;
1780
1781 BT_DBG("chan %p", chan);
1782
86d1407c
JH
1783 if (!smp)
1784 return;
b68fda68 1785
84bc0db5
JH
1786 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1787 return;
1788
86d1407c
JH
1789 cancel_delayed_work(&smp->security_timer);
1790
d6268e86 1791 smp_distribute_keys(smp);
44f1a7ab
JH
1792}
1793
70db83c4
JH
1794static void smp_ready_cb(struct l2cap_chan *chan)
1795{
1796 struct l2cap_conn *conn = chan->conn;
1797
1798 BT_DBG("chan %p", chan);
1799
1800 conn->smp = chan;
1801 l2cap_chan_hold(chan);
1802}
1803
4befb867
JH
1804static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1805{
1806 int err;
1807
1808 BT_DBG("chan %p", chan);
1809
1810 err = smp_sig_channel(chan, skb);
1811 if (err) {
b68fda68 1812 struct smp_chan *smp = chan->data;
4befb867 1813
b68fda68
JH
1814 if (smp)
1815 cancel_delayed_work_sync(&smp->security_timer);
4befb867 1816
1e91c29e 1817 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
4befb867
JH
1818 }
1819
1820 return err;
1821}
1822
70db83c4
JH
1823static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1824 unsigned long hdr_len,
1825 unsigned long len, int nb)
1826{
1827 struct sk_buff *skb;
1828
1829 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1830 if (!skb)
1831 return ERR_PTR(-ENOMEM);
1832
1833 skb->priority = HCI_PRIO_MAX;
1834 bt_cb(skb)->chan = chan;
1835
1836 return skb;
1837}
1838
1839static const struct l2cap_ops smp_chan_ops = {
1840 .name = "Security Manager",
1841 .ready = smp_ready_cb,
5d88cc73 1842 .recv = smp_recv_cb,
70db83c4
JH
1843 .alloc_skb = smp_alloc_skb_cb,
1844 .teardown = smp_teardown_cb,
44f1a7ab 1845 .resume = smp_resume_cb,
70db83c4
JH
1846
1847 .new_connection = l2cap_chan_no_new_connection,
70db83c4
JH
1848 .state_change = l2cap_chan_no_state_change,
1849 .close = l2cap_chan_no_close,
1850 .defer = l2cap_chan_no_defer,
1851 .suspend = l2cap_chan_no_suspend,
70db83c4
JH
1852 .set_shutdown = l2cap_chan_no_set_shutdown,
1853 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1854 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1855};
1856
1857static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1858{
1859 struct l2cap_chan *chan;
1860
1861 BT_DBG("pchan %p", pchan);
1862
1863 chan = l2cap_chan_create();
1864 if (!chan)
1865 return NULL;
1866
1867 chan->chan_type = pchan->chan_type;
1868 chan->ops = &smp_chan_ops;
1869 chan->scid = pchan->scid;
1870 chan->dcid = chan->scid;
1871 chan->imtu = pchan->imtu;
1872 chan->omtu = pchan->omtu;
1873 chan->mode = pchan->mode;
1874
abe84903
JH
1875 /* Other L2CAP channels may request SMP routines in order to
1876 * change the security level. This means that the SMP channel
1877 * lock must be considered in its own category to avoid lockdep
1878 * warnings.
1879 */
1880 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
1881
70db83c4
JH
1882 BT_DBG("created chan %p", chan);
1883
1884 return chan;
1885}
1886
1887static const struct l2cap_ops smp_root_chan_ops = {
1888 .name = "Security Manager Root",
1889 .new_connection = smp_new_conn_cb,
1890
1891 /* None of these are implemented for the root channel */
1892 .close = l2cap_chan_no_close,
1893 .alloc_skb = l2cap_chan_no_alloc_skb,
1894 .recv = l2cap_chan_no_recv,
1895 .state_change = l2cap_chan_no_state_change,
1896 .teardown = l2cap_chan_no_teardown,
1897 .ready = l2cap_chan_no_ready,
1898 .defer = l2cap_chan_no_defer,
1899 .suspend = l2cap_chan_no_suspend,
1900 .resume = l2cap_chan_no_resume,
1901 .set_shutdown = l2cap_chan_no_set_shutdown,
1902 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1903 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1904};
1905
711eafe3
JH
1906int smp_register(struct hci_dev *hdev)
1907{
70db83c4 1908 struct l2cap_chan *chan;
defce9e8 1909 struct crypto_blkcipher *tfm_aes;
70db83c4 1910
711eafe3
JH
1911 BT_DBG("%s", hdev->name);
1912
adae20cb 1913 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
defce9e8
JH
1914 if (IS_ERR(tfm_aes)) {
1915 int err = PTR_ERR(tfm_aes);
711eafe3 1916 BT_ERR("Unable to create crypto context");
711eafe3
JH
1917 return err;
1918 }
1919
70db83c4
JH
1920 chan = l2cap_chan_create();
1921 if (!chan) {
defce9e8 1922 crypto_free_blkcipher(tfm_aes);
70db83c4
JH
1923 return -ENOMEM;
1924 }
1925
defce9e8
JH
1926 chan->data = tfm_aes;
1927
5d88cc73 1928 l2cap_add_scid(chan, L2CAP_CID_SMP);
70db83c4
JH
1929
1930 l2cap_chan_set_defaults(chan);
1931
1932 bacpy(&chan->src, &hdev->bdaddr);
1933 chan->src_type = BDADDR_LE_PUBLIC;
1934 chan->state = BT_LISTEN;
1935 chan->mode = L2CAP_MODE_BASIC;
1936 chan->imtu = L2CAP_DEFAULT_MTU;
1937 chan->ops = &smp_root_chan_ops;
1938
abe84903
JH
1939 /* Set correct nesting level for a parent/listening channel */
1940 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
1941
70db83c4
JH
1942 hdev->smp_data = chan;
1943
711eafe3
JH
1944 return 0;
1945}
1946
1947void smp_unregister(struct hci_dev *hdev)
1948{
70db83c4 1949 struct l2cap_chan *chan = hdev->smp_data;
defce9e8 1950 struct crypto_blkcipher *tfm_aes;
70db83c4
JH
1951
1952 if (!chan)
1953 return;
1954
1955 BT_DBG("%s chan %p", hdev->name, chan);
711eafe3 1956
defce9e8
JH
1957 tfm_aes = chan->data;
1958 if (tfm_aes) {
1959 chan->data = NULL;
1960 crypto_free_blkcipher(tfm_aes);
711eafe3 1961 }
70db83c4
JH
1962
1963 hdev->smp_data = NULL;
1964 l2cap_chan_put(chan);
711eafe3 1965}
This page took 0.407121 seconds and 5 git commands to generate.