1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 BlueZ - Bluetooth protocol stack for Linux 4 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). 5 6 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 7 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 8 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 9 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 10 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 11 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 15 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 16 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 17 SOFTWARE IS DISCLAIMED. 18 */ 19 20 #include <linux/debugfs.h> 21 #include <linux/scatterlist.h> 22 #include <crypto/aes-cbc-macs.h> 23 #include <crypto/aes.h> 24 #include <crypto/kpp.h> 25 #include <crypto/utils.h> 26 27 #include <net/bluetooth/bluetooth.h> 28 #include <net/bluetooth/hci_core.h> 29 #include <net/bluetooth/l2cap.h> 30 #include <net/bluetooth/mgmt.h> 31 32 #include "ecdh_helper.h" 33 #include "smp.h" 34 35 #define SMP_DEV(hdev) \ 36 ((struct smp_dev *)((struct l2cap_chan *)((hdev)->smp_data))->data) 37 38 /* Low-level debug macros to be used for stuff that we don't want 39 * accidentally in dmesg, i.e. the values of the various crypto keys 40 * and the inputs & outputs of crypto functions. 41 */ 42 #ifdef DEBUG 43 #define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \ 44 ##__VA_ARGS__) 45 #else 46 #define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \ 47 ##__VA_ARGS__) 48 #endif 49 50 #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd) 51 52 /* Keys which are not distributed with Secure Connections */ 53 #define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY) 54 55 #define SMP_TIMEOUT secs_to_jiffies(30) 56 57 #define ID_ADDR_TIMEOUT msecs_to_jiffies(200) 58 59 #define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \ 60 0x3f : 0x07) 61 #define KEY_DIST_MASK 0x07 62 63 /* Maximum message length that can be passed to smp_aes_cmac */ 64 #define CMAC_MSG_MAX 80 65 66 enum { 67 SMP_FLAG_TK_VALID, 68 SMP_FLAG_CFM_PENDING, 69 SMP_FLAG_MITM_AUTH, 70 SMP_FLAG_COMPLETE, 71 SMP_FLAG_INITIATOR, 72 SMP_FLAG_SC, 73 SMP_FLAG_REMOTE_PK, 74 SMP_FLAG_DEBUG_KEY, 75 SMP_FLAG_WAIT_USER, 76 SMP_FLAG_DHKEY_PENDING, 77 SMP_FLAG_REMOTE_OOB, 78 SMP_FLAG_LOCAL_OOB, 79 SMP_FLAG_CT2, 80 }; 81 82 struct smp_dev { 83 /* Secure Connections OOB data */ 84 bool local_oob; 85 u8 local_pk[64]; 86 u8 local_rand[16]; 87 bool debug_key; 88 89 struct crypto_kpp *tfm_ecdh; 90 }; 91 92 struct smp_chan { 93 struct l2cap_conn *conn; 94 struct delayed_work security_timer; 95 unsigned long allow_cmd; /* Bitmask of allowed commands */ 96 97 u8 preq[7]; /* SMP Pairing Request */ 98 u8 prsp[7]; /* SMP Pairing Response */ 99 u8 prnd[16]; /* SMP Pairing Random (local) */ 100 u8 rrnd[16]; /* SMP Pairing Random (remote) */ 101 u8 pcnf[16]; /* SMP Pairing Confirm */ 102 u8 tk[16]; /* SMP Temporary Key */ 103 u8 rr[16]; /* Remote OOB ra/rb value */ 104 u8 lr[16]; /* Local OOB ra/rb value */ 105 u8 enc_key_size; 106 u8 remote_key_dist; 107 bdaddr_t id_addr; 108 u8 id_addr_type; 109 u8 irk[16]; 110 struct smp_csrk *csrk; 111 struct smp_csrk *responder_csrk; 112 struct smp_ltk *ltk; 113 struct smp_ltk *responder_ltk; 114 struct smp_irk *remote_irk; 115 u8 *link_key; 116 unsigned long flags; 117 u8 method; 118 u8 passkey_round; 119 120 /* Secure Connections variables */ 121 u8 local_pk[64]; 122 u8 remote_pk[64]; 123 u8 dhkey[32]; 124 u8 mackey[16]; 125 126 struct crypto_kpp *tfm_ecdh; 127 }; 128 129 /* These debug key values are defined in the SMP section of the core 130 * specification. debug_pk is the public debug key and debug_sk the 131 * private debug key. 132 */ 133 static const u8 debug_pk[64] = { 134 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc, 135 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef, 136 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e, 137 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20, 138 139 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74, 140 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76, 141 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63, 142 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc, 143 }; 144 145 static const u8 debug_sk[32] = { 146 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58, 147 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a, 148 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74, 149 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f, 150 }; 151 152 static inline void swap_buf(const u8 *src, u8 *dst, size_t len) 153 { 154 size_t i; 155 156 for (i = 0; i < len; i++) 157 dst[len - 1 - i] = src[i]; 158 } 159 160 /* The following functions map to the LE SC SMP crypto functions 161 * AES-CMAC, f4, f5, f6, g2 and h6. 162 */ 163 164 static int smp_aes_cmac(const u8 k[16], const u8 *m, size_t len, u8 mac[16]) 165 { 166 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX]; 167 struct aes_cmac_key key __cleanup(aes_cmac_zeroize_key); 168 int err; 169 170 if (len > CMAC_MSG_MAX) 171 return -EFBIG; 172 173 /* Swap key and message from LSB to MSB */ 174 swap_buf(k, tmp, 16); 175 swap_buf(m, msg_msb, len); 176 177 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m); 178 SMP_DBG("key %16phN", k); 179 180 err = aes_cmac_preparekey(&key, tmp, 16); 181 memzero_explicit(tmp, sizeof(tmp)); 182 if (WARN_ON_ONCE(err)) /* Should never happen, as 16 is valid keylen */ 183 return err; 184 aes_cmac(&key, msg_msb, len, mac_msb); 185 186 swap_buf(mac_msb, mac, 16); 187 188 SMP_DBG("mac %16phN", mac); 189 190 return 0; 191 } 192 193 static int smp_f4(const u8 u[32], const u8 v[32], const u8 x[16], u8 z, 194 u8 res[16]) 195 { 196 u8 m[65]; 197 int err; 198 199 SMP_DBG("u %32phN", u); 200 SMP_DBG("v %32phN", v); 201 SMP_DBG("x %16phN z %02x", x, z); 202 203 m[0] = z; 204 memcpy(m + 1, v, 32); 205 memcpy(m + 33, u, 32); 206 207 err = smp_aes_cmac(x, m, sizeof(m), res); 208 if (err) 209 return err; 210 211 SMP_DBG("res %16phN", res); 212 213 return err; 214 } 215 216 static int smp_f5(const u8 w[32], const u8 n1[16], const u8 n2[16], 217 const u8 a1[7], const u8 a2[7], u8 mackey[16], u8 ltk[16]) 218 { 219 /* The btle, salt and length "magic" values are as defined in 220 * the SMP section of the Bluetooth core specification. In ASCII 221 * the btle value ends up being 'btle'. The salt is just a 222 * random number whereas length is the value 256 in little 223 * endian format. 224 */ 225 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 }; 226 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60, 227 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c }; 228 const u8 length[2] = { 0x00, 0x01 }; 229 u8 m[53], t[16]; 230 int err; 231 232 SMP_DBG("w %32phN", w); 233 SMP_DBG("n1 %16phN n2 %16phN", n1, n2); 234 SMP_DBG("a1 %7phN a2 %7phN", a1, a2); 235 236 err = smp_aes_cmac(salt, w, 32, t); 237 if (err) 238 return err; 239 240 SMP_DBG("t %16phN", t); 241 242 memcpy(m, length, 2); 243 memcpy(m + 2, a2, 7); 244 memcpy(m + 9, a1, 7); 245 memcpy(m + 16, n2, 16); 246 memcpy(m + 32, n1, 16); 247 memcpy(m + 48, btle, 4); 248 249 m[52] = 0; /* Counter */ 250 251 err = smp_aes_cmac(t, m, sizeof(m), mackey); 252 if (err) 253 return err; 254 255 SMP_DBG("mackey %16phN", mackey); 256 257 m[52] = 1; /* Counter */ 258 259 err = smp_aes_cmac(t, m, sizeof(m), ltk); 260 if (err) 261 return err; 262 263 SMP_DBG("ltk %16phN", ltk); 264 265 return 0; 266 } 267 268 static int smp_f6(const u8 w[16], const u8 n1[16], const u8 n2[16], 269 const u8 r[16], const u8 io_cap[3], const u8 a1[7], 270 const u8 a2[7], u8 res[16]) 271 { 272 u8 m[65]; 273 int err; 274 275 SMP_DBG("w %16phN", w); 276 SMP_DBG("n1 %16phN n2 %16phN", n1, n2); 277 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2); 278 279 memcpy(m, a2, 7); 280 memcpy(m + 7, a1, 7); 281 memcpy(m + 14, io_cap, 3); 282 memcpy(m + 17, r, 16); 283 memcpy(m + 33, n2, 16); 284 memcpy(m + 49, n1, 16); 285 286 err = smp_aes_cmac(w, m, sizeof(m), res); 287 if (err) 288 return err; 289 290 SMP_DBG("res %16phN", res); 291 292 return err; 293 } 294 295 static int smp_g2(const u8 u[32], const u8 v[32], const u8 x[16], 296 const u8 y[16], u32 *val) 297 { 298 u8 m[80], tmp[16]; 299 int err; 300 301 SMP_DBG("u %32phN", u); 302 SMP_DBG("v %32phN", v); 303 SMP_DBG("x %16phN y %16phN", x, y); 304 305 memcpy(m, y, 16); 306 memcpy(m + 16, v, 32); 307 memcpy(m + 48, u, 32); 308 309 err = smp_aes_cmac(x, m, sizeof(m), tmp); 310 if (err) 311 return err; 312 313 *val = get_unaligned_le32(tmp); 314 *val %= 1000000; 315 316 SMP_DBG("val %06u", *val); 317 318 return 0; 319 } 320 321 static int smp_h6(const u8 w[16], const u8 key_id[4], u8 res[16]) 322 { 323 int err; 324 325 SMP_DBG("w %16phN key_id %4phN", w, key_id); 326 327 err = smp_aes_cmac(w, key_id, 4, res); 328 if (err) 329 return err; 330 331 SMP_DBG("res %16phN", res); 332 333 return err; 334 } 335 336 static int smp_h7(const u8 w[16], const u8 salt[16], u8 res[16]) 337 { 338 int err; 339 340 SMP_DBG("w %16phN salt %16phN", w, salt); 341 342 err = smp_aes_cmac(salt, w, 16, res); 343 if (err) 344 return err; 345 346 SMP_DBG("res %16phN", res); 347 348 return err; 349 } 350 351 /* The following functions map to the legacy SMP crypto functions e, c1, 352 * s1 and ah. 353 */ 354 355 static int smp_e(const u8 *k, u8 *r) 356 { 357 struct aes_enckey aes; 358 uint8_t tmp[16], data[16]; 359 int err; 360 361 SMP_DBG("k %16phN r %16phN", k, r); 362 363 /* The most significant octet of key corresponds to k[0] */ 364 swap_buf(k, tmp, 16); 365 366 err = aes_prepareenckey(&aes, tmp, 16); 367 if (err) { 368 BT_ERR("cipher setkey failed: %d", err); 369 goto out_clear; 370 } 371 372 /* Most significant octet of plaintextData corresponds to data[0] */ 373 swap_buf(r, data, 16); 374 375 aes_encrypt(&aes, data, data); 376 377 /* Most significant octet of encryptedData corresponds to data[0] */ 378 swap_buf(data, r, 16); 379 380 SMP_DBG("r %16phN", r); 381 382 out_clear: 383 memzero_explicit(tmp, sizeof(tmp)); 384 memzero_explicit(&aes, sizeof(aes)); 385 return err; 386 } 387 388 static int smp_c1(const u8 k[16], 389 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat, 390 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16]) 391 { 392 u8 p1[16], p2[16]; 393 int err; 394 395 SMP_DBG("k %16phN r %16phN", k, r); 396 SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat, ia, _rat, ra); 397 SMP_DBG("preq %7phN pres %7phN", preq, pres); 398 399 memset(p1, 0, 16); 400 401 /* p1 = pres || preq || _rat || _iat */ 402 p1[0] = _iat; 403 p1[1] = _rat; 404 memcpy(p1 + 2, preq, 7); 405 memcpy(p1 + 9, pres, 7); 406 407 SMP_DBG("p1 %16phN", p1); 408 409 /* res = r XOR p1 */ 410 crypto_xor_cpy(res, r, p1, sizeof(p1)); 411 412 /* res = e(k, res) */ 413 err = smp_e(k, res); 414 if (err) { 415 BT_ERR("Encrypt data error"); 416 return err; 417 } 418 419 /* p2 = padding || ia || ra */ 420 memcpy(p2, ra, 6); 421 memcpy(p2 + 6, ia, 6); 422 memset(p2 + 12, 0, 4); 423 424 SMP_DBG("p2 %16phN", p2); 425 426 /* res = res XOR p2 */ 427 crypto_xor(res, p2, sizeof(p2)); 428 429 /* res = e(k, res) */ 430 err = smp_e(k, res); 431 if (err) 432 BT_ERR("Encrypt data error"); 433 434 return err; 435 } 436 437 static int smp_s1(const u8 k[16], 438 const u8 r1[16], const u8 r2[16], u8 _r[16]) 439 { 440 int err; 441 442 /* Just least significant octets from r1 and r2 are considered */ 443 memcpy(_r, r2, 8); 444 memcpy(_r + 8, r1, 8); 445 446 err = smp_e(k, _r); 447 if (err) 448 BT_ERR("Encrypt data error"); 449 450 return err; 451 } 452 453 static int smp_ah(const u8 irk[16], const u8 r[3], u8 res[3]) 454 { 455 u8 _res[16]; 456 int err; 457 458 /* r' = padding || r */ 459 memcpy(_res, r, 3); 460 memset(_res + 3, 0, 13); 461 462 err = smp_e(irk, _res); 463 if (err) { 464 BT_ERR("Encrypt error"); 465 return err; 466 } 467 468 /* The output of the random address function ah is: 469 * ah(k, r) = e(k, r') mod 2^24 470 * The output of the security function e is then truncated to 24 bits 471 * by taking the least significant 24 bits of the output of e as the 472 * result of ah. 473 */ 474 memcpy(res, _res, 3); 475 476 return 0; 477 } 478 479 bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16], 480 const bdaddr_t *bdaddr) 481 { 482 struct l2cap_chan *chan = hdev->smp_data; 483 u8 hash[3]; 484 int err; 485 486 if (!chan || !chan->data) 487 return false; 488 489 bt_dev_dbg(hdev, "RPA %pMR IRK %*phN", bdaddr, 16, irk); 490 491 err = smp_ah(irk, &bdaddr->b[3], hash); 492 if (err) 493 return false; 494 495 return !crypto_memneq(bdaddr->b, hash, 3); 496 } 497 498 int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa) 499 { 500 struct l2cap_chan *chan = hdev->smp_data; 501 int err; 502 503 if (!chan || !chan->data) 504 return -EOPNOTSUPP; 505 506 get_random_bytes(&rpa->b[3], 3); 507 508 rpa->b[5] &= 0x3f; /* Clear two most significant bits */ 509 rpa->b[5] |= 0x40; /* Set second most significant bit */ 510 511 err = smp_ah(irk, &rpa->b[3], rpa->b); 512 if (err < 0) 513 return err; 514 515 bt_dev_dbg(hdev, "RPA %pMR", rpa); 516 517 return 0; 518 } 519 520 int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16]) 521 { 522 struct l2cap_chan *chan = hdev->smp_data; 523 struct smp_dev *smp; 524 int err; 525 526 if (!chan || !chan->data) 527 return -EOPNOTSUPP; 528 529 smp = chan->data; 530 531 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) { 532 bt_dev_dbg(hdev, "Using debug keys"); 533 err = set_ecdh_privkey(smp->tfm_ecdh, debug_sk); 534 if (err) 535 return err; 536 memcpy(smp->local_pk, debug_pk, 64); 537 smp->debug_key = true; 538 } else { 539 while (true) { 540 /* Generate key pair for Secure Connections */ 541 err = generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk); 542 if (err) 543 return err; 544 545 /* This is unlikely, but we need to check that 546 * we didn't accidentally generate a debug key. 547 */ 548 if (crypto_memneq(smp->local_pk, debug_pk, 64)) 549 break; 550 } 551 smp->debug_key = false; 552 } 553 554 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk); 555 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32); 556 557 get_random_bytes(smp->local_rand, 16); 558 559 err = smp_f4(smp->local_pk, smp->local_pk, smp->local_rand, 0, hash); 560 if (err < 0) 561 return err; 562 563 memcpy(rand, smp->local_rand, 16); 564 565 smp->local_oob = true; 566 567 return 0; 568 } 569 570 static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) 571 { 572 struct l2cap_chan *chan = conn->smp; 573 struct smp_chan *smp; 574 struct kvec iv[2]; 575 struct msghdr msg; 576 577 if (!chan) 578 return; 579 580 bt_dev_dbg(conn->hcon->hdev, "code 0x%2.2x", code); 581 582 iv[0].iov_base = &code; 583 iv[0].iov_len = 1; 584 585 iv[1].iov_base = data; 586 iv[1].iov_len = len; 587 588 memset(&msg, 0, sizeof(msg)); 589 590 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iv, 2, 1 + len); 591 592 l2cap_chan_send(chan, &msg, 1 + len, NULL); 593 594 if (!chan->data) 595 return; 596 597 smp = chan->data; 598 599 cancel_delayed_work_sync(&smp->security_timer); 600 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT); 601 } 602 603 static u8 authreq_to_seclevel(u8 authreq) 604 { 605 if (authreq & SMP_AUTH_MITM) { 606 if (authreq & SMP_AUTH_SC) 607 return BT_SECURITY_FIPS; 608 else 609 return BT_SECURITY_HIGH; 610 } else { 611 return BT_SECURITY_MEDIUM; 612 } 613 } 614 615 static __u8 seclevel_to_authreq(__u8 sec_level) 616 { 617 switch (sec_level) { 618 case BT_SECURITY_FIPS: 619 case BT_SECURITY_HIGH: 620 return SMP_AUTH_MITM | SMP_AUTH_BONDING; 621 case BT_SECURITY_MEDIUM: 622 return SMP_AUTH_BONDING; 623 default: 624 return SMP_AUTH_NONE; 625 } 626 } 627 628 static void build_pairing_cmd(struct l2cap_conn *conn, 629 struct smp_cmd_pairing *req, 630 struct smp_cmd_pairing *rsp, __u8 authreq) 631 { 632 struct l2cap_chan *chan = conn->smp; 633 struct smp_chan *smp = chan->data; 634 struct hci_conn *hcon = conn->hcon; 635 struct hci_dev *hdev = hcon->hdev; 636 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT; 637 638 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) { 639 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 640 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 641 authreq |= SMP_AUTH_BONDING; 642 } else { 643 authreq &= ~SMP_AUTH_BONDING; 644 } 645 646 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING)) 647 remote_dist |= SMP_DIST_ID_KEY; 648 649 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) 650 local_dist |= SMP_DIST_ID_KEY; 651 652 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && 653 (authreq & SMP_AUTH_SC)) { 654 struct oob_data *oob_data; 655 u8 bdaddr_type; 656 657 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { 658 local_dist |= SMP_DIST_LINK_KEY; 659 remote_dist |= SMP_DIST_LINK_KEY; 660 } 661 662 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC) 663 bdaddr_type = BDADDR_LE_PUBLIC; 664 else 665 bdaddr_type = BDADDR_LE_RANDOM; 666 667 mutex_lock(&hdev->remote_oob_lock); 668 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst, 669 bdaddr_type); 670 if (oob_data && oob_data->present) { 671 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags); 672 oob_flag = SMP_OOB_PRESENT; 673 memcpy(smp->rr, oob_data->rand256, 16); 674 memcpy(smp->pcnf, oob_data->hash256, 16); 675 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf); 676 SMP_DBG("OOB Remote Random: %16phN", smp->rr); 677 } 678 mutex_unlock(&hdev->remote_oob_lock); 679 680 } else { 681 authreq &= ~SMP_AUTH_SC; 682 } 683 684 if (rsp == NULL) { 685 req->io_capability = conn->hcon->io_capability; 686 req->oob_flag = oob_flag; 687 req->max_key_size = hdev->le_max_key_size; 688 req->init_key_dist = local_dist; 689 req->resp_key_dist = remote_dist; 690 req->auth_req = (authreq & AUTH_REQ_MASK(hdev)); 691 692 smp->remote_key_dist = remote_dist; 693 return; 694 } 695 696 rsp->io_capability = conn->hcon->io_capability; 697 rsp->oob_flag = oob_flag; 698 rsp->max_key_size = hdev->le_max_key_size; 699 rsp->init_key_dist = req->init_key_dist & remote_dist; 700 rsp->resp_key_dist = req->resp_key_dist & local_dist; 701 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev)); 702 703 smp->remote_key_dist = rsp->init_key_dist; 704 } 705 706 static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) 707 { 708 struct l2cap_chan *chan = conn->smp; 709 struct hci_dev *hdev = conn->hcon->hdev; 710 struct smp_chan *smp = chan->data; 711 712 if (conn->hcon->pending_sec_level == BT_SECURITY_FIPS && 713 max_key_size != SMP_MAX_ENC_KEY_SIZE) 714 return SMP_ENC_KEY_SIZE; 715 716 if (max_key_size > hdev->le_max_key_size || 717 max_key_size < SMP_MIN_ENC_KEY_SIZE) 718 return SMP_ENC_KEY_SIZE; 719 720 smp->enc_key_size = max_key_size; 721 722 return 0; 723 } 724 725 static void smp_chan_destroy(struct l2cap_conn *conn) 726 { 727 struct l2cap_chan *chan = conn->smp; 728 struct smp_chan *smp = chan->data; 729 struct hci_conn *hcon = conn->hcon; 730 bool complete; 731 732 BUG_ON(!smp); 733 734 cancel_delayed_work_sync(&smp->security_timer); 735 736 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); 737 mgmt_smp_complete(hcon, complete); 738 739 kfree_sensitive(smp->csrk); 740 kfree_sensitive(smp->responder_csrk); 741 kfree_sensitive(smp->link_key); 742 743 crypto_free_kpp(smp->tfm_ecdh); 744 745 /* Ensure that we don't leave any debug key around if debug key 746 * support hasn't been explicitly enabled. 747 */ 748 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG && 749 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) { 750 list_del_rcu(&smp->ltk->list); 751 kfree_rcu(smp->ltk, rcu); 752 smp->ltk = NULL; 753 } 754 755 /* If pairing failed clean up any keys we might have */ 756 if (!complete) { 757 if (smp->ltk) { 758 list_del_rcu(&smp->ltk->list); 759 kfree_rcu(smp->ltk, rcu); 760 } 761 762 if (smp->responder_ltk) { 763 list_del_rcu(&smp->responder_ltk->list); 764 kfree_rcu(smp->responder_ltk, rcu); 765 } 766 767 if (smp->remote_irk) { 768 list_del_rcu(&smp->remote_irk->list); 769 kfree_rcu(smp->remote_irk, rcu); 770 } 771 } 772 773 chan->data = NULL; 774 kfree_sensitive(smp); 775 hci_conn_drop(hcon); 776 } 777 778 static void smp_failure(struct l2cap_conn *conn, u8 reason) 779 { 780 struct hci_conn *hcon = conn->hcon; 781 struct l2cap_chan *chan = conn->smp; 782 783 if (reason) 784 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), 785 &reason); 786 787 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); 788 789 if (chan->data) 790 smp_chan_destroy(conn); 791 } 792 793 #define JUST_WORKS 0x00 794 #define JUST_CFM 0x01 795 #define REQ_PASSKEY 0x02 796 #define CFM_PASSKEY 0x03 797 #define REQ_OOB 0x04 798 #define DSP_PASSKEY 0x05 799 #define OVERLAP 0xFF 800 801 static const u8 gen_method[5][5] = { 802 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, 803 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, 804 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, 805 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, 806 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, 807 }; 808 809 static const u8 sc_method[5][5] = { 810 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, 811 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, 812 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY }, 813 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, 814 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, 815 }; 816 817 static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io) 818 { 819 /* If either side has unknown io_caps, use JUST_CFM (which gets 820 * converted later to JUST_WORKS if we're initiators. 821 */ 822 if (local_io > SMP_IO_KEYBOARD_DISPLAY || 823 remote_io > SMP_IO_KEYBOARD_DISPLAY) 824 return JUST_CFM; 825 826 if (test_bit(SMP_FLAG_SC, &smp->flags)) 827 return sc_method[remote_io][local_io]; 828 829 return gen_method[remote_io][local_io]; 830 } 831 832 static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, 833 u8 local_io, u8 remote_io) 834 { 835 struct hci_conn *hcon = conn->hcon; 836 struct l2cap_chan *chan = conn->smp; 837 struct smp_chan *smp = chan->data; 838 u32 passkey = 0; 839 int ret; 840 841 /* Initialize key for JUST WORKS */ 842 memset(smp->tk, 0, sizeof(smp->tk)); 843 clear_bit(SMP_FLAG_TK_VALID, &smp->flags); 844 845 bt_dev_dbg(hcon->hdev, "auth:%u lcl:%u rem:%u", auth, local_io, 846 remote_io); 847 848 /* If neither side wants MITM, either "just" confirm an incoming 849 * request or use just-works for outgoing ones. The JUST_CFM 850 * will be converted to JUST_WORKS if necessary later in this 851 * function. If either side has MITM look up the method from the 852 * table. 853 */ 854 if (!(auth & SMP_AUTH_MITM)) 855 smp->method = JUST_CFM; 856 else 857 smp->method = get_auth_method(smp, local_io, remote_io); 858 859 /* Don't confirm locally initiated pairing attempts */ 860 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, 861 &smp->flags)) 862 smp->method = JUST_WORKS; 863 864 /* Don't bother user space with no IO capabilities */ 865 if (smp->method == JUST_CFM && 866 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) 867 smp->method = JUST_WORKS; 868 869 /* If Just Works, Continue with Zero TK and ask user-space for 870 * confirmation */ 871 if (smp->method == JUST_WORKS) { 872 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, 873 hcon->type, 874 hcon->dst_type, 875 passkey, 1); 876 if (ret) 877 return ret; 878 set_bit(SMP_FLAG_WAIT_USER, &smp->flags); 879 return 0; 880 } 881 882 /* If this function is used for SC -> legacy fallback we 883 * can only recover the just-works case. 884 */ 885 if (test_bit(SMP_FLAG_SC, &smp->flags)) 886 return -EINVAL; 887 888 /* Not Just Works/Confirm results in MITM Authentication */ 889 if (smp->method != JUST_CFM) { 890 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags); 891 if (hcon->pending_sec_level < BT_SECURITY_HIGH) 892 hcon->pending_sec_level = BT_SECURITY_HIGH; 893 } 894 895 /* If both devices have Keyboard-Display I/O, the initiator 896 * Confirms and the responder Enters the passkey. 897 */ 898 if (smp->method == OVERLAP) { 899 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 900 smp->method = CFM_PASSKEY; 901 else 902 smp->method = REQ_PASSKEY; 903 } 904 905 /* Generate random passkey. */ 906 if (smp->method == CFM_PASSKEY) { 907 memset(smp->tk, 0, sizeof(smp->tk)); 908 get_random_bytes(&passkey, sizeof(passkey)); 909 passkey %= 1000000; 910 put_unaligned_le32(passkey, smp->tk); 911 bt_dev_dbg(hcon->hdev, "PassKey: %u", passkey); 912 set_bit(SMP_FLAG_TK_VALID, &smp->flags); 913 } 914 915 if (smp->method == REQ_PASSKEY) 916 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, 917 hcon->type, hcon->dst_type); 918 else if (smp->method == JUST_CFM) 919 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, 920 hcon->type, hcon->dst_type, 921 passkey, 1); 922 else 923 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst, 924 hcon->type, hcon->dst_type, 925 passkey, 0); 926 927 return ret; 928 } 929 930 static u8 smp_confirm(struct smp_chan *smp) 931 { 932 struct l2cap_conn *conn = smp->conn; 933 struct smp_cmd_pairing_confirm cp; 934 int ret; 935 936 bt_dev_dbg(conn->hcon->hdev, "conn %p", conn); 937 938 ret = smp_c1(smp->tk, smp->prnd, smp->preq, smp->prsp, 939 conn->hcon->init_addr_type, &conn->hcon->init_addr, 940 conn->hcon->resp_addr_type, &conn->hcon->resp_addr, 941 cp.confirm_val); 942 if (ret) 943 return SMP_UNSPECIFIED; 944 945 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); 946 947 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); 948 949 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 950 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); 951 else 952 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); 953 954 return 0; 955 } 956 957 static u8 smp_random(struct smp_chan *smp) 958 { 959 struct l2cap_conn *conn = smp->conn; 960 struct hci_conn *hcon = conn->hcon; 961 u8 confirm[16]; 962 int ret; 963 964 bt_dev_dbg(conn->hcon->hdev, "conn %p %s", conn, 965 test_bit(SMP_FLAG_INITIATOR, &smp->flags) ? "initiator" : 966 "responder"); 967 968 ret = smp_c1(smp->tk, smp->rrnd, smp->preq, smp->prsp, 969 hcon->init_addr_type, &hcon->init_addr, 970 hcon->resp_addr_type, &hcon->resp_addr, confirm); 971 if (ret) 972 return SMP_UNSPECIFIED; 973 974 if (crypto_memneq(smp->pcnf, confirm, sizeof(smp->pcnf))) { 975 bt_dev_err(hcon->hdev, "pairing failed " 976 "(confirmation values mismatch)"); 977 return SMP_CONFIRM_FAILED; 978 } 979 980 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 981 u8 stk[16]; 982 __le64 rand = 0; 983 __le16 ediv = 0; 984 985 smp_s1(smp->tk, smp->rrnd, smp->prnd, stk); 986 987 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) 988 return SMP_UNSPECIFIED; 989 990 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size); 991 hcon->enc_key_size = smp->enc_key_size; 992 set_bit(HCI_CONN_STK_ENCRYPT, hcon->flags); 993 } else { 994 u8 stk[16], auth; 995 __le64 rand = 0; 996 __le16 ediv = 0; 997 998 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), 999 smp->prnd); 1000 1001 smp_s1(smp->tk, smp->prnd, smp->rrnd, stk); 1002 1003 auth = test_bit(SMP_FLAG_MITM_AUTH, &smp->flags) ? 1 : 0; 1004 1005 /* Even though there's no _RESPONDER suffix this is the 1006 * responder STK we're adding for later lookup (the initiator 1007 * STK never needs to be stored). 1008 */ 1009 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, 1010 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand); 1011 } 1012 1013 return 0; 1014 } 1015 1016 static void smp_notify_keys(struct l2cap_conn *conn) 1017 { 1018 struct l2cap_chan *chan = conn->smp; 1019 struct smp_chan *smp = chan->data; 1020 struct hci_conn *hcon = conn->hcon; 1021 struct hci_dev *hdev = hcon->hdev; 1022 struct smp_cmd_pairing *req = (void *) &smp->preq[1]; 1023 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; 1024 bool persistent; 1025 1026 if (hcon->type == ACL_LINK) { 1027 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION) 1028 persistent = false; 1029 else 1030 persistent = !test_bit(HCI_CONN_FLUSH_KEY, 1031 hcon->flags); 1032 } else { 1033 /* The LTKs, IRKs and CSRKs should be persistent only if 1034 * both sides had the bonding bit set in their 1035 * authentication requests. 1036 */ 1037 persistent = !!((req->auth_req & rsp->auth_req) & 1038 SMP_AUTH_BONDING); 1039 } 1040 1041 if (smp->remote_irk) { 1042 mgmt_new_irk(hdev, smp->remote_irk, persistent); 1043 1044 /* Now that user space can be considered to know the 1045 * identity address track the connection based on it 1046 * from now on (assuming this is an LE link). 1047 */ 1048 if (hcon->type == LE_LINK) { 1049 bacpy(&hcon->dst, &smp->remote_irk->bdaddr); 1050 hcon->dst_type = smp->remote_irk->addr_type; 1051 /* Use a short delay to make sure the new address is 1052 * propagated _before_ the channels. 1053 */ 1054 queue_delayed_work(hdev->workqueue, 1055 &conn->id_addr_timer, 1056 ID_ADDR_TIMEOUT); 1057 } 1058 } 1059 1060 if (smp->csrk) { 1061 smp->csrk->bdaddr_type = hcon->dst_type; 1062 bacpy(&smp->csrk->bdaddr, &hcon->dst); 1063 mgmt_new_csrk(hdev, smp->csrk, persistent); 1064 } 1065 1066 if (smp->responder_csrk) { 1067 smp->responder_csrk->bdaddr_type = hcon->dst_type; 1068 bacpy(&smp->responder_csrk->bdaddr, &hcon->dst); 1069 mgmt_new_csrk(hdev, smp->responder_csrk, persistent); 1070 } 1071 1072 if (smp->ltk) { 1073 smp->ltk->bdaddr_type = hcon->dst_type; 1074 bacpy(&smp->ltk->bdaddr, &hcon->dst); 1075 mgmt_new_ltk(hdev, smp->ltk, persistent); 1076 } 1077 1078 if (smp->responder_ltk) { 1079 smp->responder_ltk->bdaddr_type = hcon->dst_type; 1080 bacpy(&smp->responder_ltk->bdaddr, &hcon->dst); 1081 mgmt_new_ltk(hdev, smp->responder_ltk, persistent); 1082 } 1083 1084 if (smp->link_key) { 1085 struct link_key *key; 1086 u8 type; 1087 1088 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags)) 1089 type = HCI_LK_DEBUG_COMBINATION; 1090 else if (hcon->sec_level == BT_SECURITY_FIPS) 1091 type = HCI_LK_AUTH_COMBINATION_P256; 1092 else 1093 type = HCI_LK_UNAUTH_COMBINATION_P256; 1094 1095 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst, 1096 smp->link_key, type, 0, &persistent); 1097 if (key) { 1098 mgmt_new_link_key(hdev, key, persistent); 1099 1100 /* Don't keep debug keys around if the relevant 1101 * flag is not set. 1102 */ 1103 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) && 1104 key->type == HCI_LK_DEBUG_COMBINATION) { 1105 list_del_rcu(&key->list); 1106 kfree_rcu(key, rcu); 1107 } 1108 } 1109 } 1110 } 1111 1112 static void sc_add_ltk(struct smp_chan *smp) 1113 { 1114 struct hci_conn *hcon = smp->conn->hcon; 1115 u8 key_type, auth; 1116 1117 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags)) 1118 key_type = SMP_LTK_P256_DEBUG; 1119 else 1120 key_type = SMP_LTK_P256; 1121 1122 if (hcon->pending_sec_level == BT_SECURITY_FIPS) 1123 auth = 1; 1124 else 1125 auth = 0; 1126 1127 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, 1128 key_type, auth, smp->tk, smp->enc_key_size, 1129 0, 0); 1130 } 1131 1132 static void sc_generate_link_key(struct smp_chan *smp) 1133 { 1134 /* From core spec. Spells out in ASCII as 'lebr'. */ 1135 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c }; 1136 1137 smp->link_key = kzalloc(16, GFP_KERNEL); 1138 if (!smp->link_key) 1139 return; 1140 1141 if (test_bit(SMP_FLAG_CT2, &smp->flags)) { 1142 /* SALT = 0x000000000000000000000000746D7031 */ 1143 const u8 salt[16] = { 0x31, 0x70, 0x6d, 0x74 }; 1144 1145 if (smp_h7(smp->tk, salt, smp->link_key)) { 1146 kfree_sensitive(smp->link_key); 1147 smp->link_key = NULL; 1148 return; 1149 } 1150 } else { 1151 /* From core spec. Spells out in ASCII as 'tmp1'. */ 1152 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 }; 1153 1154 if (smp_h6(smp->tk, tmp1, smp->link_key)) { 1155 kfree_sensitive(smp->link_key); 1156 smp->link_key = NULL; 1157 return; 1158 } 1159 } 1160 1161 if (smp_h6(smp->link_key, lebr, smp->link_key)) { 1162 kfree_sensitive(smp->link_key); 1163 smp->link_key = NULL; 1164 return; 1165 } 1166 } 1167 1168 static void smp_allow_key_dist(struct smp_chan *smp) 1169 { 1170 /* Allow the first expected phase 3 PDU. The rest of the PDUs 1171 * will be allowed in each PDU handler to ensure we receive 1172 * them in the correct order. 1173 */ 1174 if (smp->remote_key_dist & SMP_DIST_ENC_KEY) 1175 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO); 1176 else if (smp->remote_key_dist & SMP_DIST_ID_KEY) 1177 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); 1178 else if (smp->remote_key_dist & SMP_DIST_SIGN) 1179 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); 1180 } 1181 1182 static void sc_generate_ltk(struct smp_chan *smp) 1183 { 1184 /* From core spec. Spells out in ASCII as 'brle'. */ 1185 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 }; 1186 struct hci_conn *hcon = smp->conn->hcon; 1187 struct hci_dev *hdev = hcon->hdev; 1188 struct link_key *key; 1189 int err; 1190 1191 rcu_read_lock(); 1192 1193 key = hci_find_link_key(hdev, &hcon->dst); 1194 if (!key) { 1195 rcu_read_unlock(); 1196 bt_dev_err(hdev, "no Link Key found to generate LTK"); 1197 return; 1198 } 1199 1200 if (key->type == HCI_LK_DEBUG_COMBINATION) 1201 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); 1202 1203 if (test_bit(SMP_FLAG_CT2, &smp->flags)) { 1204 /* SALT = 0x000000000000000000000000746D7032 */ 1205 const u8 salt[16] = { 0x32, 0x70, 0x6d, 0x74 }; 1206 1207 err = smp_h7(key->val, salt, smp->tk); 1208 } else { 1209 /* From core spec. Spells out in ASCII as 'tmp2'. */ 1210 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 }; 1211 1212 err = smp_h6(key->val, tmp2, smp->tk); 1213 } 1214 1215 rcu_read_unlock(); 1216 1217 if (err) 1218 return; 1219 1220 if (smp_h6(smp->tk, brle, smp->tk)) 1221 return; 1222 1223 sc_add_ltk(smp); 1224 } 1225 1226 static void smp_distribute_keys(struct smp_chan *smp) 1227 { 1228 struct smp_cmd_pairing *req, *rsp; 1229 struct l2cap_conn *conn = smp->conn; 1230 struct hci_conn *hcon = conn->hcon; 1231 struct hci_dev *hdev = hcon->hdev; 1232 __u8 *keydist; 1233 1234 bt_dev_dbg(hdev, "conn %p", conn); 1235 1236 rsp = (void *) &smp->prsp[1]; 1237 1238 /* The responder sends its keys first */ 1239 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags) && 1240 (smp->remote_key_dist & KEY_DIST_MASK)) { 1241 smp_allow_key_dist(smp); 1242 return; 1243 } 1244 1245 req = (void *) &smp->preq[1]; 1246 1247 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 1248 keydist = &rsp->init_key_dist; 1249 *keydist &= req->init_key_dist; 1250 } else { 1251 keydist = &rsp->resp_key_dist; 1252 *keydist &= req->resp_key_dist; 1253 } 1254 1255 if (test_bit(SMP_FLAG_SC, &smp->flags)) { 1256 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY)) 1257 sc_generate_link_key(smp); 1258 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY)) 1259 sc_generate_ltk(smp); 1260 1261 /* Clear the keys which are generated but not distributed */ 1262 *keydist &= ~SMP_SC_NO_DIST; 1263 } 1264 1265 bt_dev_dbg(hdev, "keydist 0x%x", *keydist); 1266 1267 if (*keydist & SMP_DIST_ENC_KEY) { 1268 struct smp_cmd_encrypt_info enc; 1269 struct smp_cmd_initiator_ident ident; 1270 struct smp_ltk *ltk; 1271 u8 authenticated; 1272 __le16 ediv; 1273 __le64 rand; 1274 1275 /* Make sure we generate only the significant amount of 1276 * bytes based on the encryption key size, and set the rest 1277 * of the value to zeroes. 1278 */ 1279 get_random_bytes(enc.ltk, smp->enc_key_size); 1280 memset(enc.ltk + smp->enc_key_size, 0, 1281 sizeof(enc.ltk) - smp->enc_key_size); 1282 1283 get_random_bytes(&ediv, sizeof(ediv)); 1284 get_random_bytes(&rand, sizeof(rand)); 1285 1286 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); 1287 1288 authenticated = hcon->sec_level == BT_SECURITY_HIGH; 1289 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, 1290 SMP_LTK_RESPONDER, authenticated, enc.ltk, 1291 smp->enc_key_size, ediv, rand); 1292 smp->responder_ltk = ltk; 1293 1294 ident.ediv = ediv; 1295 ident.rand = rand; 1296 1297 smp_send_cmd(conn, SMP_CMD_INITIATOR_IDENT, sizeof(ident), 1298 &ident); 1299 1300 *keydist &= ~SMP_DIST_ENC_KEY; 1301 } 1302 1303 if (*keydist & SMP_DIST_ID_KEY) { 1304 struct smp_cmd_ident_addr_info addrinfo; 1305 struct smp_cmd_ident_info idinfo; 1306 1307 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); 1308 1309 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); 1310 1311 /* The hci_conn contains the local identity address 1312 * after the connection has been established. 1313 * 1314 * This is true even when the connection has been 1315 * established using a resolvable random address. 1316 */ 1317 bacpy(&addrinfo.bdaddr, &hcon->src); 1318 addrinfo.addr_type = hcon->src_type; 1319 1320 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), 1321 &addrinfo); 1322 1323 *keydist &= ~SMP_DIST_ID_KEY; 1324 } 1325 1326 if (*keydist & SMP_DIST_SIGN) { 1327 struct smp_cmd_sign_info sign; 1328 struct smp_csrk *csrk; 1329 1330 /* Generate a new random key */ 1331 get_random_bytes(sign.csrk, sizeof(sign.csrk)); 1332 1333 csrk = kzalloc_obj(*csrk); 1334 if (csrk) { 1335 if (hcon->sec_level > BT_SECURITY_MEDIUM) 1336 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED; 1337 else 1338 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED; 1339 memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); 1340 } 1341 smp->responder_csrk = csrk; 1342 1343 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); 1344 1345 *keydist &= ~SMP_DIST_SIGN; 1346 } 1347 1348 /* If there are still keys to be received wait for them */ 1349 if (smp->remote_key_dist & KEY_DIST_MASK) { 1350 smp_allow_key_dist(smp); 1351 return; 1352 } 1353 1354 set_bit(SMP_FLAG_COMPLETE, &smp->flags); 1355 smp_notify_keys(conn); 1356 1357 smp_chan_destroy(conn); 1358 } 1359 1360 static void smp_timeout(struct work_struct *work) 1361 { 1362 struct smp_chan *smp = container_of(work, struct smp_chan, 1363 security_timer.work); 1364 struct l2cap_conn *conn = smp->conn; 1365 1366 bt_dev_dbg(conn->hcon->hdev, "conn %p", conn); 1367 1368 hci_disconnect(conn->hcon, HCI_ERROR_AUTH_FAILURE); 1369 } 1370 1371 static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) 1372 { 1373 struct hci_conn *hcon = conn->hcon; 1374 struct l2cap_chan *chan = conn->smp; 1375 struct smp_chan *smp; 1376 1377 smp = kzalloc_obj(*smp, GFP_ATOMIC); 1378 if (!smp) 1379 return NULL; 1380 1381 smp->tfm_ecdh = crypto_alloc_kpp("ecdh-nist-p256", 0, 0); 1382 if (IS_ERR(smp->tfm_ecdh)) { 1383 bt_dev_err(hcon->hdev, "Unable to create ECDH crypto context"); 1384 goto zfree_smp; 1385 } 1386 1387 smp->conn = conn; 1388 chan->data = smp; 1389 1390 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL); 1391 1392 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout); 1393 1394 hci_conn_hold(hcon); 1395 1396 return smp; 1397 1398 zfree_smp: 1399 kfree_sensitive(smp); 1400 return NULL; 1401 } 1402 1403 static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16]) 1404 { 1405 struct hci_conn *hcon = smp->conn->hcon; 1406 u8 *na, *nb, a[7], b[7]; 1407 1408 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 1409 na = smp->prnd; 1410 nb = smp->rrnd; 1411 } else { 1412 na = smp->rrnd; 1413 nb = smp->prnd; 1414 } 1415 1416 memcpy(a, &hcon->init_addr, 6); 1417 memcpy(b, &hcon->resp_addr, 6); 1418 a[6] = hcon->init_addr_type; 1419 b[6] = hcon->resp_addr_type; 1420 1421 return smp_f5(smp->dhkey, na, nb, a, b, mackey, ltk); 1422 } 1423 1424 static void sc_dhkey_check(struct smp_chan *smp) 1425 { 1426 struct hci_conn *hcon = smp->conn->hcon; 1427 struct smp_cmd_dhkey_check check; 1428 u8 a[7], b[7], *local_addr, *remote_addr; 1429 u8 io_cap[3], r[16]; 1430 1431 memcpy(a, &hcon->init_addr, 6); 1432 memcpy(b, &hcon->resp_addr, 6); 1433 a[6] = hcon->init_addr_type; 1434 b[6] = hcon->resp_addr_type; 1435 1436 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 1437 local_addr = a; 1438 remote_addr = b; 1439 memcpy(io_cap, &smp->preq[1], 3); 1440 } else { 1441 local_addr = b; 1442 remote_addr = a; 1443 memcpy(io_cap, &smp->prsp[1], 3); 1444 } 1445 1446 memset(r, 0, sizeof(r)); 1447 1448 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) 1449 put_unaligned_le32(hcon->passkey_notify, r); 1450 1451 if (smp->method == REQ_OOB) 1452 memcpy(r, smp->rr, 16); 1453 1454 smp_f6(smp->mackey, smp->prnd, smp->rrnd, r, io_cap, local_addr, 1455 remote_addr, check.e); 1456 1457 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check); 1458 } 1459 1460 static u8 sc_passkey_send_confirm(struct smp_chan *smp) 1461 { 1462 struct l2cap_conn *conn = smp->conn; 1463 struct hci_conn *hcon = conn->hcon; 1464 struct smp_cmd_pairing_confirm cfm; 1465 u8 r; 1466 1467 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01); 1468 r |= 0x80; 1469 1470 get_random_bytes(smp->prnd, sizeof(smp->prnd)); 1471 1472 if (smp_f4(smp->local_pk, smp->remote_pk, smp->prnd, r, 1473 cfm.confirm_val)) 1474 return SMP_UNSPECIFIED; 1475 1476 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm); 1477 1478 return 0; 1479 } 1480 1481 static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op) 1482 { 1483 struct l2cap_conn *conn = smp->conn; 1484 struct hci_conn *hcon = conn->hcon; 1485 struct hci_dev *hdev = hcon->hdev; 1486 u8 cfm[16], r; 1487 1488 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */ 1489 if (smp->passkey_round >= 20) 1490 return 0; 1491 1492 switch (smp_op) { 1493 case SMP_CMD_PAIRING_RANDOM: 1494 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01); 1495 r |= 0x80; 1496 1497 if (smp_f4(smp->remote_pk, smp->local_pk, smp->rrnd, r, cfm)) 1498 return SMP_UNSPECIFIED; 1499 1500 if (crypto_memneq(smp->pcnf, cfm, 16)) 1501 return SMP_CONFIRM_FAILED; 1502 1503 smp->passkey_round++; 1504 1505 if (smp->passkey_round == 20) { 1506 /* Generate MacKey and LTK */ 1507 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk)) 1508 return SMP_UNSPECIFIED; 1509 } 1510 1511 /* The round is only complete when the initiator 1512 * receives pairing random. 1513 */ 1514 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 1515 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, 1516 sizeof(smp->prnd), smp->prnd); 1517 if (smp->passkey_round == 20) 1518 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); 1519 else 1520 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); 1521 return 0; 1522 } 1523 1524 /* Start the next round */ 1525 if (smp->passkey_round != 20) 1526 return sc_passkey_round(smp, 0); 1527 1528 /* Passkey rounds are complete - start DHKey Check */ 1529 sc_dhkey_check(smp); 1530 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); 1531 1532 break; 1533 1534 case SMP_CMD_PAIRING_CONFIRM: 1535 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) { 1536 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); 1537 return 0; 1538 } 1539 1540 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); 1541 1542 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 1543 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, 1544 sizeof(smp->prnd), smp->prnd); 1545 return 0; 1546 } 1547 1548 return sc_passkey_send_confirm(smp); 1549 1550 case SMP_CMD_PUBLIC_KEY: 1551 default: 1552 /* Initiating device starts the round */ 1553 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 1554 return 0; 1555 1556 bt_dev_dbg(hdev, "Starting passkey round %u", 1557 smp->passkey_round + 1); 1558 1559 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); 1560 1561 return sc_passkey_send_confirm(smp); 1562 } 1563 1564 return 0; 1565 } 1566 1567 static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey) 1568 { 1569 struct l2cap_conn *conn = smp->conn; 1570 struct hci_conn *hcon = conn->hcon; 1571 u8 smp_op; 1572 1573 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags); 1574 1575 switch (mgmt_op) { 1576 case MGMT_OP_USER_PASSKEY_NEG_REPLY: 1577 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED); 1578 return 0; 1579 case MGMT_OP_USER_CONFIRM_NEG_REPLY: 1580 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED); 1581 return 0; 1582 case MGMT_OP_USER_PASSKEY_REPLY: 1583 hcon->passkey_notify = le32_to_cpu(passkey); 1584 smp->passkey_round = 0; 1585 1586 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) 1587 smp_op = SMP_CMD_PAIRING_CONFIRM; 1588 else 1589 smp_op = 0; 1590 1591 if (sc_passkey_round(smp, smp_op)) 1592 return -EIO; 1593 1594 return 0; 1595 } 1596 1597 /* Initiator sends DHKey check first */ 1598 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 1599 sc_dhkey_check(smp); 1600 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); 1601 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) { 1602 sc_dhkey_check(smp); 1603 sc_add_ltk(smp); 1604 } 1605 1606 return 0; 1607 } 1608 1609 int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) 1610 { 1611 struct l2cap_conn *conn = hcon->l2cap_data; 1612 struct l2cap_chan *chan; 1613 struct smp_chan *smp; 1614 u32 value; 1615 int err; 1616 1617 if (!conn) 1618 return -ENOTCONN; 1619 1620 bt_dev_dbg(conn->hcon->hdev, ""); 1621 1622 chan = conn->smp; 1623 if (!chan) 1624 return -ENOTCONN; 1625 1626 l2cap_chan_lock(chan); 1627 if (!chan->data) { 1628 err = -ENOTCONN; 1629 goto unlock; 1630 } 1631 1632 smp = chan->data; 1633 1634 if (test_bit(SMP_FLAG_SC, &smp->flags)) { 1635 err = sc_user_reply(smp, mgmt_op, passkey); 1636 goto unlock; 1637 } 1638 1639 switch (mgmt_op) { 1640 case MGMT_OP_USER_PASSKEY_REPLY: 1641 value = le32_to_cpu(passkey); 1642 memset(smp->tk, 0, sizeof(smp->tk)); 1643 bt_dev_dbg(conn->hcon->hdev, "PassKey: %u", value); 1644 put_unaligned_le32(value, smp->tk); 1645 fallthrough; 1646 case MGMT_OP_USER_CONFIRM_REPLY: 1647 set_bit(SMP_FLAG_TK_VALID, &smp->flags); 1648 break; 1649 case MGMT_OP_USER_PASSKEY_NEG_REPLY: 1650 case MGMT_OP_USER_CONFIRM_NEG_REPLY: 1651 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); 1652 err = 0; 1653 goto unlock; 1654 default: 1655 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); 1656 err = -EOPNOTSUPP; 1657 goto unlock; 1658 } 1659 1660 err = 0; 1661 1662 /* If it is our turn to send Pairing Confirm, do so now */ 1663 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) { 1664 u8 rsp = smp_confirm(smp); 1665 if (rsp) 1666 smp_failure(conn, rsp); 1667 } 1668 1669 unlock: 1670 l2cap_chan_unlock(chan); 1671 return err; 1672 } 1673 1674 static void build_bredr_pairing_cmd(struct smp_chan *smp, 1675 struct smp_cmd_pairing *req, 1676 struct smp_cmd_pairing *rsp) 1677 { 1678 struct l2cap_conn *conn = smp->conn; 1679 struct hci_dev *hdev = conn->hcon->hdev; 1680 u8 local_dist = 0, remote_dist = 0; 1681 1682 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) { 1683 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 1684 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 1685 } 1686 1687 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING)) 1688 remote_dist |= SMP_DIST_ID_KEY; 1689 1690 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) 1691 local_dist |= SMP_DIST_ID_KEY; 1692 1693 if (!rsp) { 1694 memset(req, 0, sizeof(*req)); 1695 1696 req->auth_req = SMP_AUTH_CT2; 1697 req->init_key_dist = local_dist; 1698 req->resp_key_dist = remote_dist; 1699 req->max_key_size = conn->hcon->enc_key_size; 1700 1701 smp->remote_key_dist = remote_dist; 1702 1703 return; 1704 } 1705 1706 memset(rsp, 0, sizeof(*rsp)); 1707 1708 rsp->auth_req = SMP_AUTH_CT2; 1709 rsp->max_key_size = conn->hcon->enc_key_size; 1710 rsp->init_key_dist = req->init_key_dist & remote_dist; 1711 rsp->resp_key_dist = req->resp_key_dist & local_dist; 1712 1713 smp->remote_key_dist = rsp->init_key_dist; 1714 } 1715 1716 static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) 1717 { 1718 struct smp_cmd_pairing rsp, *req = (void *) skb->data; 1719 struct l2cap_chan *chan = conn->smp; 1720 struct hci_dev *hdev = conn->hcon->hdev; 1721 struct smp_chan *smp = chan->data; 1722 u8 key_size, auth, sec_level; 1723 int ret; 1724 1725 bt_dev_dbg(hdev, "conn %p", conn); 1726 1727 if (skb->len < sizeof(*req)) 1728 return SMP_INVALID_PARAMS; 1729 1730 if (smp && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 1731 return SMP_CMD_NOTSUPP; 1732 1733 if (!smp) { 1734 smp = smp_chan_create(conn); 1735 if (!smp) 1736 return SMP_UNSPECIFIED; 1737 } 1738 1739 /* We didn't start the pairing, so match remote */ 1740 auth = req->auth_req & AUTH_REQ_MASK(hdev); 1741 1742 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) && 1743 (auth & SMP_AUTH_BONDING)) 1744 return SMP_PAIRING_NOTSUPP; 1745 1746 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC)) 1747 return SMP_AUTH_REQUIREMENTS; 1748 1749 smp->preq[0] = SMP_CMD_PAIRING_REQ; 1750 memcpy(&smp->preq[1], req, sizeof(*req)); 1751 skb_pull(skb, sizeof(*req)); 1752 1753 /* If the remote side's OOB flag is set it means it has 1754 * successfully received our local OOB data - therefore set the 1755 * flag to indicate that local OOB is in use. 1756 */ 1757 if (req->oob_flag == SMP_OOB_PRESENT && SMP_DEV(hdev)->local_oob) 1758 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags); 1759 1760 /* SMP over BR/EDR requires special treatment */ 1761 if (conn->hcon->type == ACL_LINK) { 1762 /* We must have a BR/EDR SC link */ 1763 if (!test_bit(HCI_CONN_AES_CCM, conn->hcon->flags) && 1764 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP)) 1765 return SMP_CROSS_TRANSP_NOT_ALLOWED; 1766 1767 set_bit(SMP_FLAG_SC, &smp->flags); 1768 1769 build_bredr_pairing_cmd(smp, req, &rsp); 1770 1771 if (req->auth_req & SMP_AUTH_CT2) 1772 set_bit(SMP_FLAG_CT2, &smp->flags); 1773 1774 key_size = min(req->max_key_size, rsp.max_key_size); 1775 if (check_enc_key_size(conn, key_size)) 1776 return SMP_ENC_KEY_SIZE; 1777 1778 /* Clear bits which are generated but not distributed */ 1779 smp->remote_key_dist &= ~SMP_SC_NO_DIST; 1780 1781 smp->prsp[0] = SMP_CMD_PAIRING_RSP; 1782 memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); 1783 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); 1784 1785 smp_distribute_keys(smp); 1786 return 0; 1787 } 1788 1789 build_pairing_cmd(conn, req, &rsp, auth); 1790 1791 if (rsp.auth_req & SMP_AUTH_SC) { 1792 set_bit(SMP_FLAG_SC, &smp->flags); 1793 1794 if (rsp.auth_req & SMP_AUTH_CT2) 1795 set_bit(SMP_FLAG_CT2, &smp->flags); 1796 } 1797 1798 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) 1799 sec_level = BT_SECURITY_MEDIUM; 1800 else 1801 sec_level = authreq_to_seclevel(auth); 1802 1803 if (sec_level > conn->hcon->pending_sec_level) 1804 conn->hcon->pending_sec_level = sec_level; 1805 1806 /* If we need MITM check that it can be achieved. */ 1807 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { 1808 u8 method; 1809 1810 method = get_auth_method(smp, conn->hcon->io_capability, 1811 req->io_capability); 1812 if (method == JUST_WORKS || method == JUST_CFM) 1813 return SMP_AUTH_REQUIREMENTS; 1814 1815 /* Force MITM bit if it isn't set by the initiator. */ 1816 auth |= SMP_AUTH_MITM; 1817 rsp.auth_req |= SMP_AUTH_MITM; 1818 } 1819 1820 key_size = min(req->max_key_size, rsp.max_key_size); 1821 if (check_enc_key_size(conn, key_size)) 1822 return SMP_ENC_KEY_SIZE; 1823 1824 get_random_bytes(smp->prnd, sizeof(smp->prnd)); 1825 1826 smp->prsp[0] = SMP_CMD_PAIRING_RSP; 1827 memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); 1828 1829 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); 1830 1831 clear_bit(SMP_FLAG_INITIATOR, &smp->flags); 1832 1833 /* Strictly speaking we shouldn't allow Pairing Confirm for the 1834 * SC case, however some implementations incorrectly copy RFU auth 1835 * req bits from our security request, which may create a false 1836 * positive SC enablement. 1837 */ 1838 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); 1839 1840 if (test_bit(SMP_FLAG_SC, &smp->flags)) { 1841 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); 1842 /* Clear bits which are generated but not distributed */ 1843 smp->remote_key_dist &= ~SMP_SC_NO_DIST; 1844 /* Wait for Public Key from Initiating Device */ 1845 return 0; 1846 } 1847 1848 /* Request setup of TK */ 1849 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); 1850 if (ret) 1851 return SMP_UNSPECIFIED; 1852 1853 return 0; 1854 } 1855 1856 static u8 sc_send_public_key(struct smp_chan *smp) 1857 { 1858 struct hci_dev *hdev = smp->conn->hcon->hdev; 1859 1860 bt_dev_dbg(hdev, ""); 1861 1862 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) { 1863 struct l2cap_chan *chan = hdev->smp_data; 1864 struct smp_dev *smp_dev; 1865 1866 if (!chan || !chan->data) 1867 return SMP_UNSPECIFIED; 1868 1869 smp_dev = chan->data; 1870 1871 memcpy(smp->local_pk, smp_dev->local_pk, 64); 1872 memcpy(smp->lr, smp_dev->local_rand, 16); 1873 1874 if (smp_dev->debug_key) 1875 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); 1876 1877 goto done; 1878 } 1879 1880 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) { 1881 bt_dev_dbg(hdev, "Using debug keys"); 1882 if (set_ecdh_privkey(smp->tfm_ecdh, debug_sk)) 1883 return SMP_UNSPECIFIED; 1884 memcpy(smp->local_pk, debug_pk, 64); 1885 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); 1886 } else { 1887 while (true) { 1888 /* Generate key pair for Secure Connections */ 1889 if (generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk)) 1890 return SMP_UNSPECIFIED; 1891 1892 /* This is unlikely, but we need to check that 1893 * we didn't accidentally generate a debug key. 1894 */ 1895 if (crypto_memneq(smp->local_pk, debug_pk, 64)) 1896 break; 1897 } 1898 } 1899 1900 done: 1901 SMP_DBG("Local Public Key X: %32phN", smp->local_pk); 1902 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32); 1903 1904 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk); 1905 1906 return 0; 1907 } 1908 1909 static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) 1910 { 1911 struct smp_cmd_pairing *req, *rsp = (void *) skb->data; 1912 struct l2cap_chan *chan = conn->smp; 1913 struct smp_chan *smp = chan->data; 1914 struct hci_dev *hdev = conn->hcon->hdev; 1915 u8 key_size, auth; 1916 int ret; 1917 1918 bt_dev_dbg(hdev, "conn %p", conn); 1919 1920 if (skb->len < sizeof(*rsp)) 1921 return SMP_INVALID_PARAMS; 1922 1923 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 1924 return SMP_CMD_NOTSUPP; 1925 1926 skb_pull(skb, sizeof(*rsp)); 1927 1928 req = (void *) &smp->preq[1]; 1929 1930 key_size = min(req->max_key_size, rsp->max_key_size); 1931 if (check_enc_key_size(conn, key_size)) 1932 return SMP_ENC_KEY_SIZE; 1933 1934 auth = rsp->auth_req & AUTH_REQ_MASK(hdev); 1935 1936 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC)) 1937 return SMP_AUTH_REQUIREMENTS; 1938 1939 /* If the remote side's OOB flag is set it means it has 1940 * successfully received our local OOB data - therefore set the 1941 * flag to indicate that local OOB is in use. 1942 */ 1943 if (rsp->oob_flag == SMP_OOB_PRESENT && SMP_DEV(hdev)->local_oob) 1944 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags); 1945 1946 smp->prsp[0] = SMP_CMD_PAIRING_RSP; 1947 memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); 1948 1949 /* Update remote key distribution in case the remote cleared 1950 * some bits that we had enabled in our request. 1951 */ 1952 smp->remote_key_dist &= rsp->resp_key_dist; 1953 1954 if ((req->auth_req & SMP_AUTH_CT2) && (auth & SMP_AUTH_CT2)) 1955 set_bit(SMP_FLAG_CT2, &smp->flags); 1956 1957 /* For BR/EDR this means we're done and can start phase 3 */ 1958 if (conn->hcon->type == ACL_LINK) { 1959 /* Clear bits which are generated but not distributed */ 1960 smp->remote_key_dist &= ~SMP_SC_NO_DIST; 1961 smp_distribute_keys(smp); 1962 return 0; 1963 } 1964 1965 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC)) 1966 set_bit(SMP_FLAG_SC, &smp->flags); 1967 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH) 1968 conn->hcon->pending_sec_level = BT_SECURITY_HIGH; 1969 1970 /* If we need MITM check that it can be achieved */ 1971 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { 1972 u8 method; 1973 1974 method = get_auth_method(smp, req->io_capability, 1975 rsp->io_capability); 1976 if (method == JUST_WORKS || method == JUST_CFM) 1977 return SMP_AUTH_REQUIREMENTS; 1978 } 1979 1980 get_random_bytes(smp->prnd, sizeof(smp->prnd)); 1981 1982 /* Update remote key distribution in case the remote cleared 1983 * some bits that we had enabled in our request. 1984 */ 1985 smp->remote_key_dist &= rsp->resp_key_dist; 1986 1987 if (test_bit(SMP_FLAG_SC, &smp->flags)) { 1988 /* Clear bits which are generated but not distributed */ 1989 smp->remote_key_dist &= ~SMP_SC_NO_DIST; 1990 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); 1991 return sc_send_public_key(smp); 1992 } 1993 1994 auth |= req->auth_req; 1995 1996 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); 1997 if (ret) 1998 return SMP_UNSPECIFIED; 1999 2000 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); 2001 2002 /* Can't compose response until we have been confirmed */ 2003 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) 2004 return smp_confirm(smp); 2005 2006 return 0; 2007 } 2008 2009 static u8 sc_check_confirm(struct smp_chan *smp) 2010 { 2011 struct l2cap_conn *conn = smp->conn; 2012 2013 bt_dev_dbg(conn->hcon->hdev, ""); 2014 2015 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) 2016 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM); 2017 2018 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2019 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), 2020 smp->prnd); 2021 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); 2022 } 2023 2024 return 0; 2025 } 2026 2027 /* Work-around for some implementations that incorrectly copy RFU bits 2028 * from our security request and thereby create the impression that 2029 * we're doing SC when in fact the remote doesn't support it. 2030 */ 2031 static int fixup_sc_false_positive(struct smp_chan *smp) 2032 { 2033 struct l2cap_conn *conn = smp->conn; 2034 struct hci_conn *hcon = conn->hcon; 2035 struct hci_dev *hdev = hcon->hdev; 2036 struct smp_cmd_pairing *req, *rsp; 2037 u8 auth; 2038 2039 /* The issue is only observed when we're in responder role */ 2040 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 2041 return SMP_UNSPECIFIED; 2042 2043 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { 2044 bt_dev_err(hdev, "refusing legacy fallback in SC-only mode"); 2045 return SMP_UNSPECIFIED; 2046 } 2047 2048 bt_dev_err(hdev, "trying to fall back to legacy SMP"); 2049 2050 req = (void *) &smp->preq[1]; 2051 rsp = (void *) &smp->prsp[1]; 2052 2053 /* Rebuild key dist flags which may have been cleared for SC */ 2054 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist); 2055 2056 auth = req->auth_req & AUTH_REQ_MASK(hdev); 2057 2058 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) { 2059 bt_dev_err(hdev, "failed to fall back to legacy SMP"); 2060 return SMP_UNSPECIFIED; 2061 } 2062 2063 clear_bit(SMP_FLAG_SC, &smp->flags); 2064 2065 return 0; 2066 } 2067 2068 static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) 2069 { 2070 struct l2cap_chan *chan = conn->smp; 2071 struct smp_chan *smp = chan->data; 2072 struct hci_conn *hcon = conn->hcon; 2073 struct hci_dev *hdev = hcon->hdev; 2074 2075 bt_dev_dbg(hdev, "conn %p %s", conn, 2076 test_bit(SMP_FLAG_INITIATOR, &smp->flags) ? "initiator" : 2077 "responder"); 2078 2079 if (skb->len < sizeof(smp->pcnf)) 2080 return SMP_INVALID_PARAMS; 2081 2082 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); 2083 skb_pull(skb, sizeof(smp->pcnf)); 2084 2085 if (test_bit(SMP_FLAG_SC, &smp->flags)) { 2086 int ret; 2087 2088 /* Public Key exchange must happen before any other steps */ 2089 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags)) 2090 return sc_check_confirm(smp); 2091 2092 bt_dev_err(hdev, "Unexpected SMP Pairing Confirm"); 2093 2094 ret = fixup_sc_false_positive(smp); 2095 if (ret) 2096 return ret; 2097 } 2098 2099 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2100 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), 2101 smp->prnd); 2102 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); 2103 return 0; 2104 } 2105 2106 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) 2107 return smp_confirm(smp); 2108 2109 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); 2110 2111 return 0; 2112 } 2113 2114 static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) 2115 { 2116 struct l2cap_chan *chan = conn->smp; 2117 struct smp_chan *smp = chan->data; 2118 struct hci_conn *hcon = conn->hcon; 2119 u8 *pkax, *pkbx, *na, *nb, confirm_hint; 2120 u32 passkey = 0; 2121 int err; 2122 2123 bt_dev_dbg(hcon->hdev, "conn %p", conn); 2124 2125 if (skb->len < sizeof(smp->rrnd)) 2126 return SMP_INVALID_PARAMS; 2127 2128 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd)); 2129 skb_pull(skb, sizeof(smp->rrnd)); 2130 2131 if (!test_bit(SMP_FLAG_SC, &smp->flags)) 2132 return smp_random(smp); 2133 2134 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2135 pkax = smp->local_pk; 2136 pkbx = smp->remote_pk; 2137 na = smp->prnd; 2138 nb = smp->rrnd; 2139 } else { 2140 pkax = smp->remote_pk; 2141 pkbx = smp->local_pk; 2142 na = smp->rrnd; 2143 nb = smp->prnd; 2144 } 2145 2146 if (smp->method == REQ_OOB) { 2147 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 2148 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, 2149 sizeof(smp->prnd), smp->prnd); 2150 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); 2151 goto mackey_and_ltk; 2152 } 2153 2154 /* Passkey entry has special treatment */ 2155 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) 2156 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM); 2157 2158 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2159 u8 cfm[16]; 2160 2161 err = smp_f4(smp->remote_pk, smp->local_pk, smp->rrnd, 0, cfm); 2162 if (err) 2163 return SMP_UNSPECIFIED; 2164 2165 if (crypto_memneq(smp->pcnf, cfm, 16)) 2166 return SMP_CONFIRM_FAILED; 2167 } else { 2168 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), 2169 smp->prnd); 2170 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); 2171 } 2172 2173 mackey_and_ltk: 2174 /* Generate MacKey and LTK */ 2175 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk); 2176 if (err) 2177 return SMP_UNSPECIFIED; 2178 2179 if (smp->method == REQ_OOB) { 2180 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2181 sc_dhkey_check(smp); 2182 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); 2183 } 2184 return 0; 2185 } 2186 2187 err = smp_g2(pkax, pkbx, na, nb, &passkey); 2188 if (err) 2189 return SMP_UNSPECIFIED; 2190 2191 /* Always require user confirmation for Just-Works pairing to prevent 2192 * impersonation attacks, or in case of a legitimate device that is 2193 * repairing use the confirmation as acknowledgment to proceed with the 2194 * creation of new keys. 2195 */ 2196 confirm_hint = smp->method == JUST_WORKS ? 1 : 0; 2197 2198 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type, 2199 hcon->dst_type, passkey, confirm_hint); 2200 if (err) 2201 return SMP_UNSPECIFIED; 2202 2203 set_bit(SMP_FLAG_WAIT_USER, &smp->flags); 2204 2205 return 0; 2206 } 2207 2208 static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) 2209 { 2210 struct smp_ltk *key; 2211 struct hci_conn *hcon = conn->hcon; 2212 bool ret = false; 2213 2214 rcu_read_lock(); 2215 2216 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role); 2217 if (!key) 2218 goto unlock; 2219 2220 if (smp_ltk_sec_level(key) < sec_level) 2221 goto unlock; 2222 2223 ret = true; 2224 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) 2225 goto unlock; 2226 2227 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size); 2228 hcon->enc_key_size = key->enc_size; 2229 2230 /* We never store STKs for initiator role, so clear this flag */ 2231 clear_bit(HCI_CONN_STK_ENCRYPT, hcon->flags); 2232 2233 unlock: 2234 rcu_read_unlock(); 2235 return ret; 2236 } 2237 2238 bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level, 2239 enum smp_key_pref key_pref) 2240 { 2241 if (sec_level == BT_SECURITY_LOW) 2242 return true; 2243 2244 /* If we're encrypted with an STK but the caller prefers using 2245 * LTK claim insufficient security. This way we allow the 2246 * connection to be re-encrypted with an LTK, even if the LTK 2247 * provides the same level of security. Only exception is if we 2248 * don't have an LTK (e.g. because of key distribution bits). 2249 */ 2250 if (key_pref == SMP_USE_LTK && 2251 test_bit(HCI_CONN_STK_ENCRYPT, hcon->flags) && 2252 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role)) 2253 return false; 2254 2255 if (hcon->sec_level >= sec_level) 2256 return true; 2257 2258 return false; 2259 } 2260 2261 static void smp_send_pairing_req(struct smp_chan *smp, __u8 auth) 2262 { 2263 struct smp_cmd_pairing cp; 2264 2265 if (smp->conn->hcon->type == ACL_LINK) 2266 build_bredr_pairing_cmd(smp, &cp, NULL); 2267 else 2268 build_pairing_cmd(smp->conn, &cp, NULL, auth); 2269 2270 smp->preq[0] = SMP_CMD_PAIRING_REQ; 2271 memcpy(&smp->preq[1], &cp, sizeof(cp)); 2272 2273 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); 2274 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); 2275 2276 set_bit(SMP_FLAG_INITIATOR, &smp->flags); 2277 } 2278 2279 static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) 2280 { 2281 struct smp_cmd_security_req *rp = (void *) skb->data; 2282 struct hci_conn *hcon = conn->hcon; 2283 struct hci_dev *hdev = hcon->hdev; 2284 struct smp_chan *smp; 2285 u8 sec_level, auth; 2286 2287 bt_dev_dbg(hdev, "conn %p", conn); 2288 2289 /* SMP over BR/EDR only covers cross-transport key derivation; the 2290 * Security Request procedure has no BR/EDR counterpart. Reject it 2291 * here, otherwise smp_ltk_encrypt() finds the peer's LE LTK 2292 * (ADDR_LE_DEV_PUBLIC and BDADDR_BREDR are both 0) and issues 2293 * HCI_OP_LE_START_ENC on the ACL handle, which the controller 2294 * rejects and hci_cs_le_start_enc() turns into a disconnect. Reply 2295 * without smp_failure(): this is not an authentication failure, and 2296 * MGMT_EV_AUTH_FAILED would make bluetoothd drop the device. 2297 */ 2298 if (hcon->type != LE_LINK) { 2299 u8 reason = SMP_CMD_NOTSUPP; 2300 2301 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), 2302 &reason); 2303 return 0; 2304 } 2305 2306 if (skb->len < sizeof(*rp)) 2307 return SMP_INVALID_PARAMS; 2308 2309 if (hcon->role != HCI_ROLE_MASTER) 2310 return SMP_CMD_NOTSUPP; 2311 2312 auth = rp->auth_req & AUTH_REQ_MASK(hdev); 2313 2314 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC)) 2315 return SMP_AUTH_REQUIREMENTS; 2316 2317 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) 2318 sec_level = BT_SECURITY_MEDIUM; 2319 else 2320 sec_level = authreq_to_seclevel(auth); 2321 2322 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) { 2323 /* If link is already encrypted with sufficient security we 2324 * still need refresh encryption as per Core Spec 5.0 Vol 3, 2325 * Part H 2.4.6 2326 */ 2327 smp_ltk_encrypt(conn, hcon->sec_level); 2328 return 0; 2329 } 2330 2331 if (sec_level > hcon->pending_sec_level) 2332 hcon->pending_sec_level = sec_level; 2333 2334 if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) 2335 return 0; 2336 2337 smp = smp_chan_create(conn); 2338 if (!smp) 2339 return SMP_UNSPECIFIED; 2340 2341 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) && 2342 (auth & SMP_AUTH_BONDING)) 2343 return SMP_PAIRING_NOTSUPP; 2344 2345 skb_pull(skb, sizeof(*rp)); 2346 2347 smp_send_pairing_req(smp, auth); 2348 2349 return 0; 2350 } 2351 2352 static void smp_send_security_req(struct smp_chan *smp, __u8 auth) 2353 { 2354 struct smp_cmd_security_req cp; 2355 2356 cp.auth_req = auth; 2357 smp_send_cmd(smp->conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); 2358 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ); 2359 2360 clear_bit(SMP_FLAG_INITIATOR, &smp->flags); 2361 } 2362 2363 int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) 2364 { 2365 struct l2cap_conn *conn; 2366 struct l2cap_chan *chan; 2367 struct smp_chan *smp; 2368 __u8 authreq; 2369 int ret; 2370 2371 /* Caller shall ensure there can be no race with l2cap_conn_del() */ 2372 conn = context_unsafe(hcon->l2cap_data); 2373 2374 bt_dev_dbg(hcon->hdev, "conn %p hcon %p level 0x%2.2x", conn, hcon, 2375 sec_level); 2376 2377 /* This may be NULL if there's an unexpected disconnection */ 2378 if (!conn) 2379 return 1; 2380 2381 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) 2382 return 1; 2383 2384 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) 2385 return 1; 2386 2387 if (sec_level > hcon->pending_sec_level) 2388 hcon->pending_sec_level = sec_level; 2389 2390 if (hcon->role == HCI_ROLE_MASTER) 2391 if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) 2392 return 0; 2393 2394 chan = conn->smp; 2395 if (!chan) { 2396 bt_dev_err(hcon->hdev, "security requested but not available"); 2397 return 1; 2398 } 2399 2400 l2cap_chan_lock(chan); 2401 2402 /* If SMP is already in progress ignore this request */ 2403 if (chan->data) { 2404 ret = 0; 2405 goto unlock; 2406 } 2407 2408 smp = smp_chan_create(conn); 2409 if (!smp) { 2410 ret = 1; 2411 goto unlock; 2412 } 2413 2414 authreq = seclevel_to_authreq(sec_level); 2415 2416 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED)) { 2417 authreq |= SMP_AUTH_SC; 2418 if (hci_dev_test_flag(hcon->hdev, HCI_SSP_ENABLED)) 2419 authreq |= SMP_AUTH_CT2; 2420 } 2421 2422 /* Don't attempt to set MITM if setting is overridden by debugfs 2423 * Needed to pass certification test SM/MAS/PKE/BV-01-C 2424 */ 2425 if (!hci_dev_test_flag(hcon->hdev, HCI_FORCE_NO_MITM)) { 2426 /* Require MITM if IO Capability allows or the security level 2427 * requires it. 2428 */ 2429 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT || 2430 hcon->pending_sec_level > BT_SECURITY_MEDIUM) 2431 authreq |= SMP_AUTH_MITM; 2432 } 2433 2434 if (hcon->role == HCI_ROLE_MASTER) 2435 smp_send_pairing_req(smp, authreq); 2436 else 2437 smp_send_security_req(smp, authreq); 2438 2439 ret = 0; 2440 2441 unlock: 2442 l2cap_chan_unlock(chan); 2443 return ret; 2444 } 2445 2446 int smp_cancel_and_remove_pairing(struct hci_dev *hdev, bdaddr_t *bdaddr, 2447 u8 addr_type) 2448 { 2449 struct hci_conn *hcon; 2450 struct l2cap_conn *conn; 2451 struct l2cap_chan *chan; 2452 struct smp_chan *smp; 2453 int err; 2454 2455 err = hci_remove_ltk(hdev, bdaddr, addr_type); 2456 hci_remove_irk(hdev, bdaddr, addr_type); 2457 2458 hcon = hci_conn_hash_lookup_le(hdev, bdaddr, addr_type); 2459 if (!hcon) 2460 goto done; 2461 2462 lockdep_assert_held(&hcon->hdev->lock); 2463 2464 conn = hcon->l2cap_data; 2465 if (!conn) 2466 goto done; 2467 2468 chan = conn->smp; 2469 if (!chan) 2470 goto done; 2471 2472 l2cap_chan_lock(chan); 2473 2474 smp = chan->data; 2475 if (smp) { 2476 /* Set keys to NULL to make sure smp_failure() does not try to 2477 * remove and free already invalidated rcu list entries. */ 2478 smp->ltk = NULL; 2479 smp->responder_ltk = NULL; 2480 smp->remote_irk = NULL; 2481 2482 if (test_bit(SMP_FLAG_COMPLETE, &smp->flags)) 2483 smp_failure(conn, 0); 2484 else 2485 smp_failure(conn, SMP_UNSPECIFIED); 2486 err = 0; 2487 } 2488 2489 l2cap_chan_unlock(chan); 2490 2491 done: 2492 return err; 2493 } 2494 2495 static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) 2496 { 2497 struct smp_cmd_encrypt_info *rp = (void *) skb->data; 2498 struct l2cap_chan *chan = conn->smp; 2499 struct smp_chan *smp = chan->data; 2500 2501 bt_dev_dbg(conn->hcon->hdev, "conn %p", conn); 2502 2503 if (skb->len < sizeof(*rp)) 2504 return SMP_INVALID_PARAMS; 2505 2506 /* Pairing is aborted if any blocked keys are distributed */ 2507 if (hci_is_blocked_key(conn->hcon->hdev, HCI_BLOCKED_KEY_TYPE_LTK, 2508 rp->ltk)) { 2509 bt_dev_warn_ratelimited(conn->hcon->hdev, 2510 "LTK blocked for %pMR", 2511 &conn->hcon->dst); 2512 return SMP_INVALID_PARAMS; 2513 } 2514 2515 SMP_ALLOW_CMD(smp, SMP_CMD_INITIATOR_IDENT); 2516 2517 skb_pull(skb, sizeof(*rp)); 2518 2519 memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); 2520 2521 return 0; 2522 } 2523 2524 static int smp_cmd_initiator_ident(struct l2cap_conn *conn, struct sk_buff *skb) 2525 { 2526 struct smp_cmd_initiator_ident *rp = (void *)skb->data; 2527 struct l2cap_chan *chan = conn->smp; 2528 struct smp_chan *smp = chan->data; 2529 struct hci_dev *hdev = conn->hcon->hdev; 2530 struct hci_conn *hcon = conn->hcon; 2531 struct smp_ltk *ltk; 2532 u8 authenticated; 2533 2534 bt_dev_dbg(hdev, "conn %p", conn); 2535 2536 if (skb->len < sizeof(*rp)) 2537 return SMP_INVALID_PARAMS; 2538 2539 /* Mark the information as received */ 2540 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY; 2541 2542 if (smp->remote_key_dist & SMP_DIST_ID_KEY) 2543 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); 2544 else if (smp->remote_key_dist & SMP_DIST_SIGN) 2545 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); 2546 2547 skb_pull(skb, sizeof(*rp)); 2548 2549 authenticated = (hcon->sec_level == BT_SECURITY_HIGH); 2550 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK, 2551 authenticated, smp->tk, smp->enc_key_size, 2552 rp->ediv, rp->rand); 2553 smp->ltk = ltk; 2554 if (!(smp->remote_key_dist & KEY_DIST_MASK)) 2555 smp_distribute_keys(smp); 2556 2557 return 0; 2558 } 2559 2560 static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) 2561 { 2562 struct smp_cmd_ident_info *info = (void *) skb->data; 2563 struct l2cap_chan *chan = conn->smp; 2564 struct smp_chan *smp = chan->data; 2565 2566 bt_dev_dbg(conn->hcon->hdev, ""); 2567 2568 if (skb->len < sizeof(*info)) 2569 return SMP_INVALID_PARAMS; 2570 2571 /* Pairing is aborted if any blocked keys are distributed */ 2572 if (hci_is_blocked_key(conn->hcon->hdev, HCI_BLOCKED_KEY_TYPE_IRK, 2573 info->irk)) { 2574 bt_dev_warn_ratelimited(conn->hcon->hdev, 2575 "Identity key blocked for %pMR", 2576 &conn->hcon->dst); 2577 return SMP_INVALID_PARAMS; 2578 } 2579 2580 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO); 2581 2582 skb_pull(skb, sizeof(*info)); 2583 2584 memcpy(smp->irk, info->irk, 16); 2585 2586 return 0; 2587 } 2588 2589 static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, 2590 struct sk_buff *skb) 2591 { 2592 struct smp_cmd_ident_addr_info *info = (void *) skb->data; 2593 struct l2cap_chan *chan = conn->smp; 2594 struct smp_chan *smp = chan->data; 2595 struct hci_conn *hcon = conn->hcon; 2596 bdaddr_t rpa; 2597 2598 bt_dev_dbg(hcon->hdev, ""); 2599 2600 if (skb->len < sizeof(*info)) 2601 return SMP_INVALID_PARAMS; 2602 2603 /* Mark the information as received */ 2604 smp->remote_key_dist &= ~SMP_DIST_ID_KEY; 2605 2606 if (smp->remote_key_dist & SMP_DIST_SIGN) 2607 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); 2608 2609 skb_pull(skb, sizeof(*info)); 2610 2611 /* Strictly speaking the Core Specification (4.1) allows sending 2612 * an empty address which would force us to rely on just the IRK 2613 * as "identity information". However, since such 2614 * implementations are not known of and in order to not over 2615 * complicate our implementation, simply pretend that we never 2616 * received an IRK for such a device. 2617 * 2618 * The Identity Address must also be a Static Random or Public 2619 * Address, which hci_is_identity_address() checks for. 2620 */ 2621 if (!bacmp(&info->bdaddr, BDADDR_ANY) || 2622 !hci_is_identity_address(&info->bdaddr, info->addr_type)) { 2623 bt_dev_err(hcon->hdev, "ignoring IRK with no identity address"); 2624 goto distribute; 2625 } 2626 2627 /* Drop IRK if peer is using identity address during pairing but is 2628 * providing different address as identity information. 2629 * 2630 * Microsoft Surface Precision Mouse is known to have this bug. 2631 */ 2632 if (hci_is_identity_address(&hcon->dst, hcon->dst_type) && 2633 (bacmp(&info->bdaddr, &hcon->dst) || 2634 info->addr_type != hcon->dst_type)) { 2635 bt_dev_err(hcon->hdev, 2636 "ignoring IRK with invalid identity address"); 2637 goto distribute; 2638 } 2639 2640 bacpy(&smp->id_addr, &info->bdaddr); 2641 smp->id_addr_type = info->addr_type; 2642 2643 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type)) 2644 bacpy(&rpa, &hcon->dst); 2645 else 2646 bacpy(&rpa, BDADDR_ANY); 2647 2648 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, 2649 smp->id_addr_type, smp->irk, &rpa); 2650 2651 distribute: 2652 if (!(smp->remote_key_dist & KEY_DIST_MASK)) 2653 smp_distribute_keys(smp); 2654 2655 return 0; 2656 } 2657 2658 static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) 2659 { 2660 struct smp_cmd_sign_info *rp = (void *) skb->data; 2661 struct l2cap_chan *chan = conn->smp; 2662 struct smp_chan *smp = chan->data; 2663 struct smp_csrk *csrk; 2664 2665 bt_dev_dbg(conn->hcon->hdev, "conn %p", conn); 2666 2667 if (skb->len < sizeof(*rp)) 2668 return SMP_INVALID_PARAMS; 2669 2670 /* Mark the information as received */ 2671 smp->remote_key_dist &= ~SMP_DIST_SIGN; 2672 2673 skb_pull(skb, sizeof(*rp)); 2674 2675 csrk = kzalloc_obj(*csrk); 2676 if (csrk) { 2677 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM) 2678 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED; 2679 else 2680 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED; 2681 memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); 2682 } 2683 smp->csrk = csrk; 2684 smp_distribute_keys(smp); 2685 2686 return 0; 2687 } 2688 2689 static u8 sc_select_method(struct smp_chan *smp) 2690 { 2691 struct smp_cmd_pairing *local, *remote; 2692 u8 local_mitm, remote_mitm, local_io, remote_io, method; 2693 2694 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) || 2695 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) 2696 return REQ_OOB; 2697 2698 /* The preq/prsp contain the raw Pairing Request/Response PDUs 2699 * which are needed as inputs to some crypto functions. To get 2700 * the "struct smp_cmd_pairing" from them we need to skip the 2701 * first byte which contains the opcode. 2702 */ 2703 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2704 local = (void *) &smp->preq[1]; 2705 remote = (void *) &smp->prsp[1]; 2706 } else { 2707 local = (void *) &smp->prsp[1]; 2708 remote = (void *) &smp->preq[1]; 2709 } 2710 2711 local_io = local->io_capability; 2712 remote_io = remote->io_capability; 2713 2714 local_mitm = (local->auth_req & SMP_AUTH_MITM); 2715 remote_mitm = (remote->auth_req & SMP_AUTH_MITM); 2716 2717 /* If either side wants MITM, look up the method from the table, 2718 * otherwise use JUST WORKS. 2719 */ 2720 if (local_mitm || remote_mitm) 2721 method = get_auth_method(smp, local_io, remote_io); 2722 else 2723 method = JUST_WORKS; 2724 2725 /* Don't confirm locally initiated pairing attempts */ 2726 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 2727 method = JUST_WORKS; 2728 2729 return method; 2730 } 2731 2732 static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb) 2733 { 2734 struct smp_cmd_public_key *key = (void *) skb->data; 2735 struct hci_conn *hcon = conn->hcon; 2736 struct l2cap_chan *chan = conn->smp; 2737 struct smp_chan *smp = chan->data; 2738 struct hci_dev *hdev = hcon->hdev; 2739 struct crypto_kpp *tfm_ecdh; 2740 struct smp_cmd_pairing_confirm cfm; 2741 int err; 2742 2743 bt_dev_dbg(hdev, "conn %p", conn); 2744 2745 if (skb->len < sizeof(*key)) 2746 return SMP_INVALID_PARAMS; 2747 2748 /* Check if remote and local public keys are the same and debug key is 2749 * not in use. 2750 */ 2751 if (!test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags) && 2752 !crypto_memneq(key, smp->local_pk, 64)) { 2753 bt_dev_err(hdev, "Remote and local public keys are identical"); 2754 return SMP_DHKEY_CHECK_FAILED; 2755 } 2756 2757 memcpy(smp->remote_pk, key, 64); 2758 2759 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) { 2760 err = smp_f4(smp->remote_pk, smp->remote_pk, smp->rr, 0, 2761 cfm.confirm_val); 2762 if (err) 2763 return SMP_UNSPECIFIED; 2764 2765 if (crypto_memneq(cfm.confirm_val, smp->pcnf, 16)) 2766 return SMP_CONFIRM_FAILED; 2767 } 2768 2769 /* Non-initiating device sends its public key after receiving 2770 * the key from the initiating device. 2771 */ 2772 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2773 err = sc_send_public_key(smp); 2774 if (err) 2775 return err; 2776 } 2777 2778 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk); 2779 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32); 2780 2781 /* Compute the shared secret on the same crypto tfm on which the private 2782 * key was set/generated. 2783 */ 2784 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) { 2785 struct l2cap_chan *hchan = hdev->smp_data; 2786 struct smp_dev *smp_dev; 2787 2788 if (!hchan || !hchan->data) 2789 return SMP_UNSPECIFIED; 2790 2791 smp_dev = hchan->data; 2792 2793 tfm_ecdh = smp_dev->tfm_ecdh; 2794 } else { 2795 tfm_ecdh = smp->tfm_ecdh; 2796 } 2797 2798 if (compute_ecdh_secret(tfm_ecdh, smp->remote_pk, smp->dhkey)) 2799 return SMP_UNSPECIFIED; 2800 2801 SMP_DBG("DHKey %32phN", smp->dhkey); 2802 2803 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags); 2804 2805 smp->method = sc_select_method(smp); 2806 2807 bt_dev_dbg(hdev, "selected method 0x%02x", smp->method); 2808 2809 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */ 2810 if (smp->method == JUST_WORKS || smp->method == JUST_CFM) 2811 hcon->pending_sec_level = BT_SECURITY_MEDIUM; 2812 else 2813 hcon->pending_sec_level = BT_SECURITY_FIPS; 2814 2815 if (!crypto_memneq(debug_pk, smp->remote_pk, 64)) 2816 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); 2817 2818 if (smp->method == DSP_PASSKEY) { 2819 get_random_bytes(&hcon->passkey_notify, 2820 sizeof(hcon->passkey_notify)); 2821 hcon->passkey_notify %= 1000000; 2822 hcon->passkey_entered = 0; 2823 smp->passkey_round = 0; 2824 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type, 2825 hcon->dst_type, 2826 hcon->passkey_notify, 2827 hcon->passkey_entered)) 2828 return SMP_UNSPECIFIED; 2829 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); 2830 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY); 2831 } 2832 2833 if (smp->method == REQ_OOB) { 2834 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 2835 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, 2836 sizeof(smp->prnd), smp->prnd); 2837 2838 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); 2839 2840 return 0; 2841 } 2842 2843 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 2844 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); 2845 2846 if (smp->method == REQ_PASSKEY) { 2847 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type, 2848 hcon->dst_type)) 2849 return SMP_UNSPECIFIED; 2850 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); 2851 set_bit(SMP_FLAG_WAIT_USER, &smp->flags); 2852 return 0; 2853 } 2854 2855 /* The Initiating device waits for the non-initiating device to 2856 * send the confirm value. 2857 */ 2858 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 2859 return 0; 2860 2861 err = smp_f4(smp->local_pk, smp->remote_pk, smp->prnd, 0, 2862 cfm.confirm_val); 2863 if (err) 2864 return SMP_UNSPECIFIED; 2865 2866 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm); 2867 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); 2868 2869 return 0; 2870 } 2871 2872 static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb) 2873 { 2874 struct smp_cmd_dhkey_check *check = (void *) skb->data; 2875 struct l2cap_chan *chan = conn->smp; 2876 struct hci_conn *hcon = conn->hcon; 2877 struct smp_chan *smp = chan->data; 2878 u8 a[7], b[7], *local_addr, *remote_addr; 2879 u8 io_cap[3], r[16], e[16]; 2880 int err; 2881 2882 bt_dev_dbg(hcon->hdev, "conn %p", conn); 2883 2884 if (skb->len < sizeof(*check)) 2885 return SMP_INVALID_PARAMS; 2886 2887 memcpy(a, &hcon->init_addr, 6); 2888 memcpy(b, &hcon->resp_addr, 6); 2889 a[6] = hcon->init_addr_type; 2890 b[6] = hcon->resp_addr_type; 2891 2892 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2893 local_addr = a; 2894 remote_addr = b; 2895 memcpy(io_cap, &smp->prsp[1], 3); 2896 } else { 2897 local_addr = b; 2898 remote_addr = a; 2899 memcpy(io_cap, &smp->preq[1], 3); 2900 } 2901 2902 memset(r, 0, sizeof(r)); 2903 2904 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) 2905 put_unaligned_le32(hcon->passkey_notify, r); 2906 else if (smp->method == REQ_OOB) 2907 memcpy(r, smp->lr, 16); 2908 2909 err = smp_f6(smp->mackey, smp->rrnd, smp->prnd, r, io_cap, remote_addr, 2910 local_addr, e); 2911 if (err) 2912 return SMP_UNSPECIFIED; 2913 2914 if (crypto_memneq(check->e, e, 16)) 2915 return SMP_DHKEY_CHECK_FAILED; 2916 2917 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2918 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) { 2919 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags); 2920 return 0; 2921 } 2922 2923 /* Responder sends DHKey check as response to initiator */ 2924 sc_dhkey_check(smp); 2925 } 2926 2927 sc_add_ltk(smp); 2928 2929 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { 2930 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size); 2931 hcon->enc_key_size = smp->enc_key_size; 2932 } 2933 2934 return 0; 2935 } 2936 2937 static int smp_cmd_keypress_notify(struct l2cap_conn *conn, 2938 struct sk_buff *skb) 2939 { 2940 struct smp_cmd_keypress_notify *kp = (void *) skb->data; 2941 2942 bt_dev_dbg(conn->hcon->hdev, "value 0x%02x", kp->value); 2943 2944 return 0; 2945 } 2946 2947 static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb) 2948 { 2949 struct l2cap_conn *conn = chan->conn; 2950 struct hci_conn *hcon = conn->hcon; 2951 struct smp_chan *smp; 2952 __u8 code, reason; 2953 int err = 0; 2954 2955 if (skb->len < 1) 2956 return -EILSEQ; 2957 2958 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) { 2959 reason = SMP_PAIRING_NOTSUPP; 2960 goto done; 2961 } 2962 2963 code = skb->data[0]; 2964 skb_pull(skb, sizeof(code)); 2965 2966 smp = chan->data; 2967 2968 if (code > SMP_CMD_MAX) 2969 goto drop; 2970 2971 if (smp && !test_and_clear_bit(code, &smp->allow_cmd)) { 2972 /* If there is a context and the command is not allowed consider 2973 * it a failure so the session is cleanup properly. 2974 */ 2975 switch (code) { 2976 case SMP_CMD_IDENT_INFO: 2977 case SMP_CMD_IDENT_ADDR_INFO: 2978 case SMP_CMD_SIGN_INFO: 2979 /* 3.6.1. Key distribution and generation 2980 * 2981 * A device may reject a distributed key by sending the 2982 * Pairing Failed command with the reason set to 2983 * "Key Rejected". 2984 */ 2985 smp_failure(conn, SMP_KEY_REJECTED); 2986 break; 2987 } 2988 goto drop; 2989 } 2990 2991 /* If we don't have a context the only allowed commands are 2992 * pairing request and security request. 2993 */ 2994 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ) 2995 goto drop; 2996 2997 switch (code) { 2998 case SMP_CMD_PAIRING_REQ: 2999 reason = smp_cmd_pairing_req(conn, skb); 3000 break; 3001 3002 case SMP_CMD_PAIRING_FAIL: 3003 smp_failure(conn, 0); 3004 err = -EPERM; 3005 break; 3006 3007 case SMP_CMD_PAIRING_RSP: 3008 reason = smp_cmd_pairing_rsp(conn, skb); 3009 break; 3010 3011 case SMP_CMD_SECURITY_REQ: 3012 reason = smp_cmd_security_req(conn, skb); 3013 break; 3014 3015 case SMP_CMD_PAIRING_CONFIRM: 3016 reason = smp_cmd_pairing_confirm(conn, skb); 3017 break; 3018 3019 case SMP_CMD_PAIRING_RANDOM: 3020 reason = smp_cmd_pairing_random(conn, skb); 3021 break; 3022 3023 case SMP_CMD_ENCRYPT_INFO: 3024 reason = smp_cmd_encrypt_info(conn, skb); 3025 break; 3026 3027 case SMP_CMD_INITIATOR_IDENT: 3028 reason = smp_cmd_initiator_ident(conn, skb); 3029 break; 3030 3031 case SMP_CMD_IDENT_INFO: 3032 reason = smp_cmd_ident_info(conn, skb); 3033 break; 3034 3035 case SMP_CMD_IDENT_ADDR_INFO: 3036 reason = smp_cmd_ident_addr_info(conn, skb); 3037 break; 3038 3039 case SMP_CMD_SIGN_INFO: 3040 reason = smp_cmd_sign_info(conn, skb); 3041 break; 3042 3043 case SMP_CMD_PUBLIC_KEY: 3044 reason = smp_cmd_public_key(conn, skb); 3045 break; 3046 3047 case SMP_CMD_DHKEY_CHECK: 3048 reason = smp_cmd_dhkey_check(conn, skb); 3049 break; 3050 3051 case SMP_CMD_KEYPRESS_NOTIFY: 3052 reason = smp_cmd_keypress_notify(conn, skb); 3053 break; 3054 3055 default: 3056 bt_dev_dbg(hcon->hdev, "Unknown command code 0x%2.2x", code); 3057 reason = SMP_CMD_NOTSUPP; 3058 goto done; 3059 } 3060 3061 done: 3062 if (!err) { 3063 if (reason) 3064 smp_failure(conn, reason); 3065 kfree_skb(skb); 3066 } 3067 3068 return err; 3069 3070 drop: 3071 bt_dev_err(hcon->hdev, "unexpected SMP command 0x%02x from %pMR", 3072 code, &hcon->dst); 3073 kfree_skb(skb); 3074 return 0; 3075 } 3076 3077 static void smp_teardown_cb(struct l2cap_chan *chan, int err) 3078 { 3079 struct l2cap_conn *conn = chan->conn; 3080 3081 bt_dev_dbg(conn->hcon->hdev, "chan %p", chan); 3082 3083 if (chan->data) 3084 smp_chan_destroy(conn); 3085 3086 conn->smp = NULL; 3087 l2cap_chan_put(chan); 3088 } 3089 3090 static void bredr_pairing(struct l2cap_chan *chan) 3091 { 3092 struct l2cap_conn *conn = chan->conn; 3093 struct hci_conn *hcon = conn->hcon; 3094 struct hci_dev *hdev = hcon->hdev; 3095 struct smp_chan *smp; 3096 3097 bt_dev_dbg(hdev, "chan %p", chan); 3098 3099 /* Only new pairings are interesting */ 3100 if (!test_bit(HCI_CONN_NEW_LINK_KEY, hcon->flags)) 3101 return; 3102 3103 /* Don't bother if we're not encrypted */ 3104 if (!test_bit(HCI_CONN_ENCRYPT, hcon->flags)) 3105 return; 3106 3107 /* Only initiator may initiate SMP over BR/EDR */ 3108 if (hcon->role != HCI_ROLE_MASTER) 3109 return; 3110 3111 /* Secure Connections support must be enabled */ 3112 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED)) 3113 return; 3114 3115 /* BR/EDR must use Secure Connections for SMP */ 3116 if (!test_bit(HCI_CONN_AES_CCM, hcon->flags) && 3117 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP)) 3118 return; 3119 3120 /* If our LE support is not enabled don't do anything */ 3121 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 3122 return; 3123 3124 /* Don't bother if remote LE support is not enabled */ 3125 if (!lmp_host_le_capable(hcon)) 3126 return; 3127 3128 /* Remote must support SMP fixed chan for BR/EDR */ 3129 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR)) 3130 return; 3131 3132 /* Don't bother if SMP is already ongoing */ 3133 if (chan->data) 3134 return; 3135 3136 smp = smp_chan_create(conn); 3137 if (!smp) { 3138 bt_dev_err(hdev, "unable to create SMP context for BR/EDR"); 3139 return; 3140 } 3141 3142 set_bit(SMP_FLAG_SC, &smp->flags); 3143 3144 bt_dev_dbg(hdev, "starting SMP over BR/EDR"); 3145 3146 smp_send_pairing_req(smp, 0x00); 3147 } 3148 3149 static void smp_resume_cb(struct l2cap_chan *chan) 3150 { 3151 struct smp_chan *smp = chan->data; 3152 struct l2cap_conn *conn = chan->conn; 3153 struct hci_conn *hcon = conn->hcon; 3154 3155 bt_dev_dbg(hcon->hdev, "chan %p", chan); 3156 3157 if (hcon->type == ACL_LINK) { 3158 bredr_pairing(chan); 3159 return; 3160 } 3161 3162 if (!smp) 3163 return; 3164 3165 if (!test_bit(HCI_CONN_ENCRYPT, hcon->flags)) 3166 return; 3167 3168 cancel_delayed_work(&smp->security_timer); 3169 3170 smp_distribute_keys(smp); 3171 } 3172 3173 static void smp_ready_cb(struct l2cap_chan *chan) 3174 { 3175 struct l2cap_conn *conn = chan->conn; 3176 struct hci_conn *hcon = conn->hcon; 3177 3178 bt_dev_dbg(hcon->hdev, "chan %p", chan); 3179 3180 /* No need to call l2cap_chan_hold() here since we already own 3181 * the reference taken in smp_new_conn_cb(). This is just the 3182 * first time that we tie it to a specific pointer. The code in 3183 * l2cap_core.c ensures that there's no risk this function won't 3184 * get called if smp_new_conn_cb was previously called. 3185 */ 3186 conn->smp = chan; 3187 3188 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, hcon->flags)) 3189 bredr_pairing(chan); 3190 } 3191 3192 static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) 3193 { 3194 int err; 3195 3196 bt_dev_dbg(chan->conn->hcon->hdev, "chan %p", chan); 3197 3198 err = smp_sig_channel(chan, skb); 3199 if (err) { 3200 struct smp_chan *smp = chan->data; 3201 3202 if (smp) 3203 cancel_delayed_work_sync(&smp->security_timer); 3204 3205 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE); 3206 } 3207 3208 return err; 3209 } 3210 3211 static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan, 3212 unsigned long hdr_len, 3213 unsigned long len, int nb) 3214 { 3215 struct sk_buff *skb; 3216 3217 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL); 3218 if (!skb) 3219 return ERR_PTR(-ENOMEM); 3220 3221 skb->priority = HCI_PRIO_MAX; 3222 bt_cb(skb)->l2cap.chan = chan; 3223 3224 return skb; 3225 } 3226 3227 static const struct l2cap_ops smp_chan_ops = { 3228 .name = "Security Manager", 3229 .ready = smp_ready_cb, 3230 .recv = smp_recv_cb, 3231 .alloc_skb = smp_alloc_skb_cb, 3232 .teardown = smp_teardown_cb, 3233 .resume = smp_resume_cb, 3234 3235 .new_connection = l2cap_chan_no_new_connection, 3236 .state_change = l2cap_chan_no_state_change, 3237 .close = l2cap_chan_no_close, 3238 .defer = l2cap_chan_no_defer, 3239 .suspend = l2cap_chan_no_suspend, 3240 .set_shutdown = l2cap_chan_no_set_shutdown, 3241 .get_sndtimeo = l2cap_chan_no_get_sndtimeo, 3242 }; 3243 3244 static inline int smp_new_conn_cb(struct l2cap_chan *chan, 3245 struct l2cap_chan *new_chan) 3246 { 3247 new_chan->ops = &smp_chan_ops; 3248 3249 /* Other L2CAP channels may request SMP routines in order to 3250 * change the security level. This means that the SMP channel 3251 * lock must be considered in its own category to avoid lockdep 3252 * warnings. 3253 */ 3254 atomic_set(&new_chan->nesting, L2CAP_NESTING_SMP); 3255 3256 return 0; 3257 } 3258 3259 static const struct l2cap_ops smp_root_chan_ops = { 3260 .name = "Security Manager Root", 3261 .new_connection = smp_new_conn_cb, 3262 3263 /* None of these are implemented for the root channel */ 3264 .close = l2cap_chan_no_close, 3265 .alloc_skb = l2cap_chan_no_alloc_skb, 3266 .recv = l2cap_chan_no_recv, 3267 .state_change = l2cap_chan_no_state_change, 3268 .teardown = l2cap_chan_no_teardown, 3269 .ready = l2cap_chan_no_ready, 3270 .defer = l2cap_chan_no_defer, 3271 .suspend = l2cap_chan_no_suspend, 3272 .resume = l2cap_chan_no_resume, 3273 .set_shutdown = l2cap_chan_no_set_shutdown, 3274 .get_sndtimeo = l2cap_chan_no_get_sndtimeo, 3275 }; 3276 3277 static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid) 3278 { 3279 struct l2cap_chan *chan; 3280 struct smp_dev *smp; 3281 struct crypto_kpp *tfm_ecdh; 3282 3283 if (cid == L2CAP_CID_SMP_BREDR) { 3284 smp = NULL; 3285 goto create_chan; 3286 } 3287 3288 smp = kzalloc_obj(*smp); 3289 if (!smp) 3290 return ERR_PTR(-ENOMEM); 3291 3292 tfm_ecdh = crypto_alloc_kpp("ecdh-nist-p256", 0, 0); 3293 if (IS_ERR(tfm_ecdh)) { 3294 bt_dev_err(hdev, "Unable to create ECDH crypto context"); 3295 kfree_sensitive(smp); 3296 return ERR_CAST(tfm_ecdh); 3297 } 3298 3299 smp->local_oob = false; 3300 smp->tfm_ecdh = tfm_ecdh; 3301 3302 create_chan: 3303 chan = l2cap_chan_create(); 3304 if (!chan) { 3305 if (smp) { 3306 crypto_free_kpp(smp->tfm_ecdh); 3307 kfree_sensitive(smp); 3308 } 3309 return ERR_PTR(-ENOMEM); 3310 } 3311 3312 chan->data = smp; 3313 3314 l2cap_add_scid(chan, cid); 3315 3316 l2cap_chan_set_defaults(chan, NULL); 3317 3318 if (cid == L2CAP_CID_SMP) { 3319 u8 bdaddr_type; 3320 3321 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type); 3322 3323 if (bdaddr_type == ADDR_LE_DEV_PUBLIC) 3324 chan->src_type = BDADDR_LE_PUBLIC; 3325 else 3326 chan->src_type = BDADDR_LE_RANDOM; 3327 } else { 3328 bacpy(&chan->src, &hdev->bdaddr); 3329 chan->src_type = BDADDR_BREDR; 3330 } 3331 3332 chan->state = BT_LISTEN; 3333 chan->mode = L2CAP_MODE_BASIC; 3334 chan->imtu = L2CAP_DEFAULT_MTU; 3335 chan->ops = &smp_root_chan_ops; 3336 3337 /* Set correct nesting level for a parent/listening channel */ 3338 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); 3339 3340 return chan; 3341 } 3342 3343 static void smp_del_chan(struct l2cap_chan *chan) 3344 { 3345 struct smp_dev *smp; 3346 3347 BT_DBG("chan %p", chan); 3348 3349 smp = chan->data; 3350 if (smp) { 3351 chan->data = NULL; 3352 crypto_free_kpp(smp->tfm_ecdh); 3353 kfree_sensitive(smp); 3354 } 3355 3356 l2cap_chan_put(chan); 3357 } 3358 3359 int smp_force_bredr(struct hci_dev *hdev, bool enable) 3360 { 3361 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP)) 3362 return -EALREADY; 3363 3364 if (enable) { 3365 struct l2cap_chan *chan; 3366 3367 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR); 3368 if (IS_ERR(chan)) 3369 return PTR_ERR(chan); 3370 3371 hdev->smp_bredr_data = chan; 3372 } else { 3373 struct l2cap_chan *chan; 3374 3375 chan = hdev->smp_bredr_data; 3376 hdev->smp_bredr_data = NULL; 3377 smp_del_chan(chan); 3378 } 3379 3380 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP); 3381 3382 return 0; 3383 } 3384 3385 int smp_register(struct hci_dev *hdev) 3386 { 3387 struct l2cap_chan *chan; 3388 3389 bt_dev_dbg(hdev, ""); 3390 3391 /* If the controller does not support Low Energy operation, then 3392 * there is also no need to register any SMP channel. 3393 */ 3394 if (!lmp_le_capable(hdev)) 3395 return 0; 3396 3397 if (WARN_ON(hdev->smp_data)) { 3398 chan = hdev->smp_data; 3399 hdev->smp_data = NULL; 3400 smp_del_chan(chan); 3401 } 3402 3403 chan = smp_add_cid(hdev, L2CAP_CID_SMP); 3404 if (IS_ERR(chan)) 3405 return PTR_ERR(chan); 3406 3407 hdev->smp_data = chan; 3408 3409 if (!lmp_sc_capable(hdev)) { 3410 /* Flag can be already set here (due to power toggle) */ 3411 if (!hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP)) 3412 return 0; 3413 } 3414 3415 if (WARN_ON(hdev->smp_bredr_data)) { 3416 chan = hdev->smp_bredr_data; 3417 hdev->smp_bredr_data = NULL; 3418 smp_del_chan(chan); 3419 } 3420 3421 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR); 3422 if (IS_ERR(chan)) { 3423 int err = PTR_ERR(chan); 3424 chan = hdev->smp_data; 3425 hdev->smp_data = NULL; 3426 smp_del_chan(chan); 3427 return err; 3428 } 3429 3430 hdev->smp_bredr_data = chan; 3431 3432 return 0; 3433 } 3434 3435 void smp_unregister(struct hci_dev *hdev) 3436 { 3437 struct l2cap_chan *chan; 3438 3439 if (hdev->smp_bredr_data) { 3440 chan = hdev->smp_bredr_data; 3441 hdev->smp_bredr_data = NULL; 3442 smp_del_chan(chan); 3443 } 3444 3445 if (hdev->smp_data) { 3446 chan = hdev->smp_data; 3447 hdev->smp_data = NULL; 3448 smp_del_chan(chan); 3449 } 3450 } 3451 3452 #if IS_ENABLED(CONFIG_BT_SELFTEST_SMP) 3453 3454 static int __init test_debug_key(struct crypto_kpp *tfm_ecdh) 3455 { 3456 u8 pk[64]; 3457 int err; 3458 3459 err = set_ecdh_privkey(tfm_ecdh, debug_sk); 3460 if (err) 3461 return err; 3462 3463 err = generate_ecdh_public_key(tfm_ecdh, pk); 3464 if (err) 3465 return err; 3466 3467 if (crypto_memneq(pk, debug_pk, 64)) 3468 return -EINVAL; 3469 3470 return 0; 3471 } 3472 3473 static int __init test_ah(void) 3474 { 3475 const u8 irk[16] = { 3476 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34, 3477 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec }; 3478 const u8 r[3] = { 0x94, 0x81, 0x70 }; 3479 const u8 exp[3] = { 0xaa, 0xfb, 0x0d }; 3480 u8 res[3]; 3481 int err; 3482 3483 err = smp_ah(irk, r, res); 3484 if (err) 3485 return err; 3486 3487 if (crypto_memneq(res, exp, 3)) 3488 return -EINVAL; 3489 3490 return 0; 3491 } 3492 3493 static int __init test_c1(void) 3494 { 3495 const u8 k[16] = { 3496 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 3497 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 3498 const u8 r[16] = { 3499 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63, 3500 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 }; 3501 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 }; 3502 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 }; 3503 const u8 _iat = 0x01; 3504 const u8 _rat = 0x00; 3505 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } }; 3506 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } }; 3507 const u8 exp[16] = { 3508 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2, 3509 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e }; 3510 u8 res[16]; 3511 int err; 3512 3513 err = smp_c1(k, r, preq, pres, _iat, &ia, _rat, &ra, res); 3514 if (err) 3515 return err; 3516 3517 if (crypto_memneq(res, exp, 16)) 3518 return -EINVAL; 3519 3520 return 0; 3521 } 3522 3523 static int __init test_s1(void) 3524 { 3525 const u8 k[16] = { 3526 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 3527 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 3528 const u8 r1[16] = { 3529 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }; 3530 const u8 r2[16] = { 3531 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 }; 3532 const u8 exp[16] = { 3533 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b, 3534 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a }; 3535 u8 res[16]; 3536 int err; 3537 3538 err = smp_s1(k, r1, r2, res); 3539 if (err) 3540 return err; 3541 3542 if (crypto_memneq(res, exp, 16)) 3543 return -EINVAL; 3544 3545 return 0; 3546 } 3547 3548 static int __init test_f4(void) 3549 { 3550 const u8 u[32] = { 3551 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc, 3552 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef, 3553 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e, 3554 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 }; 3555 const u8 v[32] = { 3556 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b, 3557 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59, 3558 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90, 3559 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 }; 3560 const u8 x[16] = { 3561 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff, 3562 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 }; 3563 const u8 z = 0x00; 3564 const u8 exp[16] = { 3565 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1, 3566 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 }; 3567 u8 res[16]; 3568 int err; 3569 3570 err = smp_f4(u, v, x, z, res); 3571 if (err) 3572 return err; 3573 3574 if (crypto_memneq(res, exp, 16)) 3575 return -EINVAL; 3576 3577 return 0; 3578 } 3579 3580 static int __init test_f5(void) 3581 { 3582 const u8 w[32] = { 3583 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86, 3584 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99, 3585 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34, 3586 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec }; 3587 const u8 n1[16] = { 3588 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff, 3589 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 }; 3590 const u8 n2[16] = { 3591 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21, 3592 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 }; 3593 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 }; 3594 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 }; 3595 const u8 exp_ltk[16] = { 3596 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98, 3597 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 }; 3598 const u8 exp_mackey[16] = { 3599 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd, 3600 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 }; 3601 u8 mackey[16], ltk[16]; 3602 int err; 3603 3604 err = smp_f5(w, n1, n2, a1, a2, mackey, ltk); 3605 if (err) 3606 return err; 3607 3608 if (crypto_memneq(mackey, exp_mackey, 16)) 3609 return -EINVAL; 3610 3611 if (crypto_memneq(ltk, exp_ltk, 16)) 3612 return -EINVAL; 3613 3614 return 0; 3615 } 3616 3617 static int __init test_f6(void) 3618 { 3619 const u8 w[16] = { 3620 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd, 3621 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 }; 3622 const u8 n1[16] = { 3623 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff, 3624 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 }; 3625 const u8 n2[16] = { 3626 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21, 3627 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 }; 3628 const u8 r[16] = { 3629 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08, 3630 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 }; 3631 const u8 io_cap[3] = { 0x02, 0x01, 0x01 }; 3632 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 }; 3633 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 }; 3634 const u8 exp[16] = { 3635 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2, 3636 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 }; 3637 u8 res[16]; 3638 int err; 3639 3640 err = smp_f6(w, n1, n2, r, io_cap, a1, a2, res); 3641 if (err) 3642 return err; 3643 3644 if (crypto_memneq(res, exp, 16)) 3645 return -EINVAL; 3646 3647 return 0; 3648 } 3649 3650 static int __init test_g2(void) 3651 { 3652 const u8 u[32] = { 3653 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc, 3654 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef, 3655 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e, 3656 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 }; 3657 const u8 v[32] = { 3658 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b, 3659 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59, 3660 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90, 3661 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 }; 3662 const u8 x[16] = { 3663 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff, 3664 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 }; 3665 const u8 y[16] = { 3666 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21, 3667 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 }; 3668 const u32 exp_val = 0x2f9ed5ba % 1000000; 3669 u32 val; 3670 int err; 3671 3672 err = smp_g2(u, v, x, y, &val); 3673 if (err) 3674 return err; 3675 3676 if (val != exp_val) 3677 return -EINVAL; 3678 3679 return 0; 3680 } 3681 3682 static int __init test_h6(void) 3683 { 3684 const u8 w[16] = { 3685 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34, 3686 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec }; 3687 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c }; 3688 const u8 exp[16] = { 3689 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8, 3690 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d }; 3691 u8 res[16]; 3692 int err; 3693 3694 err = smp_h6(w, key_id, res); 3695 if (err) 3696 return err; 3697 3698 if (crypto_memneq(res, exp, 16)) 3699 return -EINVAL; 3700 3701 return 0; 3702 } 3703 3704 static char test_smp_buffer[32]; 3705 3706 static ssize_t test_smp_read(struct file *file, char __user *user_buf, 3707 size_t count, loff_t *ppos) 3708 { 3709 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer, 3710 strlen(test_smp_buffer)); 3711 } 3712 3713 static const struct file_operations test_smp_fops = { 3714 .owner = THIS_MODULE, 3715 .open = simple_open, 3716 .read = test_smp_read, 3717 .llseek = default_llseek, 3718 }; 3719 3720 static int __init run_selftests(struct crypto_kpp *tfm_ecdh) 3721 { 3722 ktime_t calltime, delta, rettime; 3723 unsigned long long duration; 3724 int err; 3725 3726 calltime = ktime_get(); 3727 3728 err = test_debug_key(tfm_ecdh); 3729 if (err) { 3730 BT_ERR("debug_key test failed"); 3731 goto done; 3732 } 3733 3734 err = test_ah(); 3735 if (err) { 3736 BT_ERR("smp_ah test failed"); 3737 goto done; 3738 } 3739 3740 err = test_c1(); 3741 if (err) { 3742 BT_ERR("smp_c1 test failed"); 3743 goto done; 3744 } 3745 3746 err = test_s1(); 3747 if (err) { 3748 BT_ERR("smp_s1 test failed"); 3749 goto done; 3750 } 3751 3752 err = test_f4(); 3753 if (err) { 3754 BT_ERR("smp_f4 test failed"); 3755 goto done; 3756 } 3757 3758 err = test_f5(); 3759 if (err) { 3760 BT_ERR("smp_f5 test failed"); 3761 goto done; 3762 } 3763 3764 err = test_f6(); 3765 if (err) { 3766 BT_ERR("smp_f6 test failed"); 3767 goto done; 3768 } 3769 3770 err = test_g2(); 3771 if (err) { 3772 BT_ERR("smp_g2 test failed"); 3773 goto done; 3774 } 3775 3776 err = test_h6(); 3777 if (err) { 3778 BT_ERR("smp_h6 test failed"); 3779 goto done; 3780 } 3781 3782 rettime = ktime_get(); 3783 delta = ktime_sub(rettime, calltime); 3784 duration = (unsigned long long) ktime_to_ns(delta) >> 10; 3785 3786 BT_INFO("SMP test passed in %llu usecs", duration); 3787 3788 done: 3789 if (!err) 3790 snprintf(test_smp_buffer, sizeof(test_smp_buffer), 3791 "PASS (%llu usecs)\n", duration); 3792 else 3793 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n"); 3794 3795 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL, 3796 &test_smp_fops); 3797 3798 return err; 3799 } 3800 3801 int __init bt_selftest_smp(void) 3802 { 3803 struct crypto_kpp *tfm_ecdh; 3804 int err; 3805 3806 tfm_ecdh = crypto_alloc_kpp("ecdh-nist-p256", 0, 0); 3807 if (IS_ERR(tfm_ecdh)) { 3808 BT_ERR("Unable to create ECDH crypto context"); 3809 return PTR_ERR(tfm_ecdh); 3810 } 3811 3812 err = run_selftests(tfm_ecdh); 3813 3814 crypto_free_kpp(tfm_ecdh); 3815 3816 return err; 3817 } 3818 3819 #endif 3820