Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License version 2 as |
| 7 | published by the Free Software Foundation; |
| 8 | |
| 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 10 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 12 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 13 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | |
| 18 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 19 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 20 | SOFTWARE IS DISCLAIMED. |
| 21 | */ |
| 22 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 23 | #include <linux/crypto.h> |
| 24 | #include <linux/scatterlist.h> |
| 25 | #include <crypto/b128ops.h> |
| 26 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 27 | #include <net/bluetooth/bluetooth.h> |
| 28 | #include <net/bluetooth/hci_core.h> |
| 29 | #include <net/bluetooth/l2cap.h> |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 30 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 31 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 32 | #include "ecc.h" |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 33 | #include "smp.h" |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 34 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 35 | #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd) |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 36 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 37 | /* Keys which are not distributed with Secure Connections */ |
| 38 | #define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY); |
| 39 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 40 | #define SMP_TIMEOUT msecs_to_jiffies(30000) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 41 | |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 42 | #define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \ |
| 43 | 0x1f : 0x07) |
| 44 | #define KEY_DIST_MASK 0x07 |
Johan Hedberg | 065a13e | 2012-10-11 16:26:06 +0200 | [diff] [blame] | 45 | |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 46 | /* Maximum message length that can be passed to aes_cmac */ |
| 47 | #define CMAC_MSG_MAX 80 |
| 48 | |
Johan Hedberg | 533e35d | 2014-06-16 19:25:18 +0300 | [diff] [blame] | 49 | enum { |
| 50 | SMP_FLAG_TK_VALID, |
| 51 | SMP_FLAG_CFM_PENDING, |
| 52 | SMP_FLAG_MITM_AUTH, |
| 53 | SMP_FLAG_COMPLETE, |
| 54 | SMP_FLAG_INITIATOR, |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 55 | SMP_FLAG_SC, |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 56 | SMP_FLAG_REMOTE_PK, |
Johan Hedberg | 533e35d | 2014-06-16 19:25:18 +0300 | [diff] [blame] | 57 | }; |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 58 | |
| 59 | struct smp_chan { |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 60 | struct l2cap_conn *conn; |
| 61 | struct delayed_work security_timer; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 62 | unsigned long allow_cmd; /* Bitmask of allowed commands */ |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 63 | |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 64 | u8 preq[7]; /* SMP Pairing Request */ |
| 65 | u8 prsp[7]; /* SMP Pairing Response */ |
| 66 | u8 prnd[16]; /* SMP Pairing Random (local) */ |
| 67 | u8 rrnd[16]; /* SMP Pairing Random (remote) */ |
| 68 | u8 pcnf[16]; /* SMP Pairing Confirm */ |
| 69 | u8 tk[16]; /* SMP Temporary Key */ |
| 70 | u8 enc_key_size; |
| 71 | u8 remote_key_dist; |
| 72 | bdaddr_t id_addr; |
| 73 | u8 id_addr_type; |
| 74 | u8 irk[16]; |
| 75 | struct smp_csrk *csrk; |
| 76 | struct smp_csrk *slave_csrk; |
| 77 | struct smp_ltk *ltk; |
| 78 | struct smp_ltk *slave_ltk; |
| 79 | struct smp_irk *remote_irk; |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 80 | unsigned long flags; |
Johan Hedberg | 6a7bd10 | 2014-06-27 14:23:03 +0300 | [diff] [blame] | 81 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 82 | /* Secure Connections variables */ |
| 83 | u8 local_pk[64]; |
| 84 | u8 local_sk[32]; |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 85 | u8 remote_pk[64]; |
| 86 | u8 dhkey[32]; |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 87 | u8 mackey[16]; |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 88 | |
Johan Hedberg | 6a7bd10 | 2014-06-27 14:23:03 +0300 | [diff] [blame] | 89 | struct crypto_blkcipher *tfm_aes; |
Johan Hedberg | 407cecf | 2014-05-02 14:19:47 +0300 | [diff] [blame] | 90 | struct crypto_hash *tfm_cmac; |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 91 | }; |
| 92 | |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 93 | static inline void swap_buf(const u8 *src, u8 *dst, size_t len) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 94 | { |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 95 | size_t i; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 96 | |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 97 | for (i = 0; i < len; i++) |
| 98 | dst[len - 1 - i] = src[i]; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 99 | } |
| 100 | |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 101 | static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m, |
| 102 | size_t len, u8 mac[16]) |
| 103 | { |
| 104 | uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX]; |
| 105 | struct hash_desc desc; |
| 106 | struct scatterlist sg; |
| 107 | int err; |
| 108 | |
| 109 | if (len > CMAC_MSG_MAX) |
| 110 | return -EFBIG; |
| 111 | |
| 112 | if (!tfm) { |
| 113 | BT_ERR("tfm %p", tfm); |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | desc.tfm = tfm; |
| 118 | desc.flags = 0; |
| 119 | |
| 120 | crypto_hash_init(&desc); |
| 121 | |
| 122 | /* Swap key and message from LSB to MSB */ |
| 123 | swap_buf(k, tmp, 16); |
| 124 | swap_buf(m, msg_msb, len); |
| 125 | |
| 126 | BT_DBG("msg (len %zu) %*phN", len, (int) len, m); |
| 127 | BT_DBG("key %16phN", k); |
| 128 | |
| 129 | err = crypto_hash_setkey(tfm, tmp, 16); |
| 130 | if (err) { |
| 131 | BT_ERR("cipher setkey failed: %d", err); |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | sg_init_one(&sg, msg_msb, len); |
| 136 | |
| 137 | err = crypto_hash_update(&desc, &sg, len); |
| 138 | if (err) { |
| 139 | BT_ERR("Hash update error %d", err); |
| 140 | return err; |
| 141 | } |
| 142 | |
| 143 | err = crypto_hash_final(&desc, mac_msb); |
| 144 | if (err) { |
| 145 | BT_ERR("Hash final error %d", err); |
| 146 | return err; |
| 147 | } |
| 148 | |
| 149 | swap_buf(mac_msb, mac, 16); |
| 150 | |
| 151 | BT_DBG("mac %16phN", mac); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32], |
| 157 | const u8 x[16], u8 z, u8 res[16]) |
| 158 | { |
| 159 | u8 m[65]; |
| 160 | int err; |
| 161 | |
| 162 | BT_DBG("u %32phN", u); |
| 163 | BT_DBG("v %32phN", v); |
| 164 | BT_DBG("x %16phN z %02x", x, z); |
| 165 | |
| 166 | m[0] = z; |
| 167 | memcpy(m + 1, v, 32); |
| 168 | memcpy(m + 33, u, 32); |
| 169 | |
| 170 | err = aes_cmac(tfm_cmac, x, m, sizeof(m), res); |
| 171 | if (err) |
| 172 | return err; |
| 173 | |
| 174 | BT_DBG("res %16phN", res); |
| 175 | |
| 176 | return err; |
| 177 | } |
| 178 | |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 179 | static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16], |
| 180 | u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16]) |
| 181 | { |
| 182 | /* The btle, salt and length "magic" values are as defined in |
| 183 | * the SMP section of the Bluetooth core specification. In ASCII |
| 184 | * the btle value ends up being 'btle'. The salt is just a |
| 185 | * random number whereas length is the value 256 in little |
| 186 | * endian format. |
| 187 | */ |
| 188 | const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 }; |
| 189 | const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60, |
| 190 | 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c }; |
| 191 | const u8 length[2] = { 0x00, 0x01 }; |
| 192 | u8 m[53], t[16]; |
| 193 | int err; |
| 194 | |
| 195 | BT_DBG("w %32phN", w); |
| 196 | BT_DBG("n1 %16phN n2 %16phN", n1, n2); |
| 197 | BT_DBG("a1 %7phN a2 %7phN", a1, a2); |
| 198 | |
| 199 | err = aes_cmac(tfm_cmac, salt, w, 32, t); |
| 200 | if (err) |
| 201 | return err; |
| 202 | |
| 203 | BT_DBG("t %16phN", t); |
| 204 | |
| 205 | memcpy(m, length, 2); |
| 206 | memcpy(m + 2, a2, 7); |
| 207 | memcpy(m + 9, a1, 7); |
| 208 | memcpy(m + 16, n2, 16); |
| 209 | memcpy(m + 32, n1, 16); |
| 210 | memcpy(m + 48, btle, 4); |
| 211 | |
| 212 | m[52] = 0; /* Counter */ |
| 213 | |
| 214 | err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey); |
| 215 | if (err) |
| 216 | return err; |
| 217 | |
| 218 | BT_DBG("mackey %16phN", mackey); |
| 219 | |
| 220 | m[52] = 1; /* Counter */ |
| 221 | |
| 222 | err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk); |
| 223 | if (err) |
| 224 | return err; |
| 225 | |
| 226 | BT_DBG("ltk %16phN", ltk); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16], |
| 232 | const u8 n1[16], u8 n2[16], const u8 r[16], |
| 233 | const u8 io_cap[3], const u8 a1[7], const u8 a2[7], |
| 234 | u8 res[16]) |
| 235 | { |
| 236 | u8 m[65]; |
| 237 | int err; |
| 238 | |
| 239 | BT_DBG("w %16phN", w); |
| 240 | BT_DBG("n1 %16phN n2 %16phN", n1, n2); |
| 241 | BT_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2); |
| 242 | |
| 243 | memcpy(m, a2, 7); |
| 244 | memcpy(m + 7, a1, 7); |
| 245 | memcpy(m + 14, io_cap, 3); |
| 246 | memcpy(m + 17, r, 16); |
| 247 | memcpy(m + 33, n2, 16); |
| 248 | memcpy(m + 49, n1, 16); |
| 249 | |
| 250 | err = aes_cmac(tfm_cmac, w, m, sizeof(m), res); |
| 251 | if (err) |
| 252 | return err; |
| 253 | |
| 254 | BT_DBG("res %16phN", res); |
| 255 | |
| 256 | return err; |
| 257 | } |
| 258 | |
Johan Hedberg | 191dc7f | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 259 | static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32], |
| 260 | const u8 x[16], const u8 y[16], u32 *val) |
| 261 | { |
| 262 | u8 m[80], tmp[16]; |
| 263 | int err; |
| 264 | |
| 265 | BT_DBG("u %32phN", u); |
| 266 | BT_DBG("v %32phN", v); |
| 267 | BT_DBG("x %16phN y %16phN", x, y); |
| 268 | |
| 269 | memcpy(m, y, 16); |
| 270 | memcpy(m + 16, v, 32); |
| 271 | memcpy(m + 48, u, 32); |
| 272 | |
| 273 | err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp); |
| 274 | if (err) |
| 275 | return err; |
| 276 | |
| 277 | *val = get_unaligned_le32(tmp); |
| 278 | *val %= 1000000; |
| 279 | |
| 280 | BT_DBG("val %06u", *val); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 285 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) |
| 286 | { |
| 287 | struct blkcipher_desc desc; |
| 288 | struct scatterlist sg; |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 289 | uint8_t tmp[16], data[16]; |
Johan Hedberg | 201a592 | 2013-12-02 10:49:04 +0200 | [diff] [blame] | 290 | int err; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 291 | |
| 292 | if (tfm == NULL) { |
| 293 | BT_ERR("tfm %p", tfm); |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | |
| 297 | desc.tfm = tfm; |
| 298 | desc.flags = 0; |
| 299 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 300 | /* The most significant octet of key corresponds to k[0] */ |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 301 | swap_buf(k, tmp, 16); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 302 | |
| 303 | err = crypto_blkcipher_setkey(tfm, tmp, 16); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 304 | if (err) { |
| 305 | BT_ERR("cipher setkey failed: %d", err); |
| 306 | return err; |
| 307 | } |
| 308 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 309 | /* Most significant octet of plaintextData corresponds to data[0] */ |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 310 | swap_buf(r, data, 16); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 311 | |
| 312 | sg_init_one(&sg, data, 16); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 313 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 314 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); |
| 315 | if (err) |
| 316 | BT_ERR("Encrypt data error %d", err); |
| 317 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 318 | /* Most significant octet of encryptedData corresponds to data[0] */ |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 319 | swap_buf(data, r, 16); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 320 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 321 | return err; |
| 322 | } |
| 323 | |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 324 | static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3]) |
| 325 | { |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 326 | u8 _res[16]; |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 327 | int err; |
| 328 | |
| 329 | /* r' = padding || r */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 330 | memcpy(_res, r, 3); |
| 331 | memset(_res + 3, 0, 13); |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 332 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 333 | err = smp_e(tfm, irk, _res); |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 334 | if (err) { |
| 335 | BT_ERR("Encrypt error"); |
| 336 | return err; |
| 337 | } |
| 338 | |
| 339 | /* The output of the random address function ah is: |
| 340 | * ah(h, r) = e(k, r') mod 2^24 |
| 341 | * The output of the security function e is then truncated to 24 bits |
| 342 | * by taking the least significant 24 bits of the output of e as the |
| 343 | * result of ah. |
| 344 | */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 345 | memcpy(res, _res, 3); |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 350 | bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr) |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 351 | { |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 352 | struct l2cap_chan *chan = hdev->smp_data; |
| 353 | struct crypto_blkcipher *tfm; |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 354 | u8 hash[3]; |
| 355 | int err; |
| 356 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 357 | if (!chan || !chan->data) |
| 358 | return false; |
| 359 | |
| 360 | tfm = chan->data; |
| 361 | |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 362 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); |
| 363 | |
| 364 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); |
| 365 | if (err) |
| 366 | return false; |
| 367 | |
| 368 | return !memcmp(bdaddr->b, hash, 3); |
| 369 | } |
| 370 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 371 | int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa) |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 372 | { |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 373 | struct l2cap_chan *chan = hdev->smp_data; |
| 374 | struct crypto_blkcipher *tfm; |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 375 | int err; |
| 376 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 377 | if (!chan || !chan->data) |
| 378 | return -EOPNOTSUPP; |
| 379 | |
| 380 | tfm = chan->data; |
| 381 | |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 382 | get_random_bytes(&rpa->b[3], 3); |
| 383 | |
| 384 | rpa->b[5] &= 0x3f; /* Clear two most significant bits */ |
| 385 | rpa->b[5] |= 0x40; /* Set second most significant bit */ |
| 386 | |
| 387 | err = smp_ah(tfm, irk, &rpa->b[3], rpa->b); |
| 388 | if (err < 0) |
| 389 | return err; |
| 390 | |
| 391 | BT_DBG("RPA %pMR", rpa); |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 396 | static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16], |
| 397 | u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, |
| 398 | bdaddr_t *ra, u8 res[16]) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 399 | { |
| 400 | u8 p1[16], p2[16]; |
| 401 | int err; |
| 402 | |
| 403 | memset(p1, 0, 16); |
| 404 | |
| 405 | /* p1 = pres || preq || _rat || _iat */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 406 | p1[0] = _iat; |
| 407 | p1[1] = _rat; |
| 408 | memcpy(p1 + 2, preq, 7); |
| 409 | memcpy(p1 + 9, pres, 7); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 410 | |
| 411 | /* p2 = padding || ia || ra */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 412 | memcpy(p2, ra, 6); |
| 413 | memcpy(p2 + 6, ia, 6); |
| 414 | memset(p2 + 12, 0, 4); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 415 | |
| 416 | /* res = r XOR p1 */ |
| 417 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); |
| 418 | |
| 419 | /* res = e(k, res) */ |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 420 | err = smp_e(tfm_aes, k, res); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 421 | if (err) { |
| 422 | BT_ERR("Encrypt data error"); |
| 423 | return err; |
| 424 | } |
| 425 | |
| 426 | /* res = res XOR p2 */ |
| 427 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); |
| 428 | |
| 429 | /* res = e(k, res) */ |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 430 | err = smp_e(tfm_aes, k, res); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 431 | if (err) |
| 432 | BT_ERR("Encrypt data error"); |
| 433 | |
| 434 | return err; |
| 435 | } |
| 436 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 437 | static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16], |
| 438 | u8 r2[16], u8 _r[16]) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 439 | { |
| 440 | int err; |
| 441 | |
| 442 | /* Just least significant octets from r1 and r2 are considered */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 443 | memcpy(_r, r2, 8); |
| 444 | memcpy(_r + 8, r1, 8); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 445 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 446 | err = smp_e(tfm_aes, k, _r); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 447 | if (err) |
| 448 | BT_ERR("Encrypt data error"); |
| 449 | |
| 450 | return err; |
| 451 | } |
| 452 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 453 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) |
| 454 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 455 | struct l2cap_chan *chan = conn->smp; |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 456 | struct smp_chan *smp; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 457 | struct kvec iv[2]; |
| 458 | struct msghdr msg; |
| 459 | |
| 460 | if (!chan) |
| 461 | return; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 462 | |
| 463 | BT_DBG("code 0x%2.2x", code); |
| 464 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 465 | iv[0].iov_base = &code; |
| 466 | iv[0].iov_len = 1; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 467 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 468 | iv[1].iov_base = data; |
| 469 | iv[1].iov_len = len; |
| 470 | |
| 471 | memset(&msg, 0, sizeof(msg)); |
| 472 | |
| 473 | msg.msg_iov = (struct iovec *) &iv; |
| 474 | msg.msg_iovlen = 2; |
| 475 | |
| 476 | l2cap_chan_send(chan, &msg, 1 + len); |
Vinicius Costa Gomes | e2dcd11 | 2011-08-19 21:06:50 -0300 | [diff] [blame] | 477 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 478 | if (!chan->data) |
| 479 | return; |
| 480 | |
| 481 | smp = chan->data; |
| 482 | |
| 483 | cancel_delayed_work_sync(&smp->security_timer); |
Johan Hedberg | 1b0921d | 2014-09-05 22:19:48 +0300 | [diff] [blame] | 484 | schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 485 | } |
| 486 | |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 487 | static u8 authreq_to_seclevel(u8 authreq) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 488 | { |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 489 | if (authreq & SMP_AUTH_MITM) { |
| 490 | if (authreq & SMP_AUTH_SC) |
| 491 | return BT_SECURITY_FIPS; |
| 492 | else |
| 493 | return BT_SECURITY_HIGH; |
| 494 | } else { |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 495 | return BT_SECURITY_MEDIUM; |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 496 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static __u8 seclevel_to_authreq(__u8 sec_level) |
| 500 | { |
| 501 | switch (sec_level) { |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 502 | case BT_SECURITY_FIPS: |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 503 | case BT_SECURITY_HIGH: |
| 504 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; |
| 505 | case BT_SECURITY_MEDIUM: |
| 506 | return SMP_AUTH_BONDING; |
| 507 | default: |
| 508 | return SMP_AUTH_NONE; |
| 509 | } |
| 510 | } |
| 511 | |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 512 | static void build_pairing_cmd(struct l2cap_conn *conn, |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 513 | struct smp_cmd_pairing *req, |
| 514 | struct smp_cmd_pairing *rsp, __u8 authreq) |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 515 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 516 | struct l2cap_chan *chan = conn->smp; |
| 517 | struct smp_chan *smp = chan->data; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 518 | struct hci_conn *hcon = conn->hcon; |
| 519 | struct hci_dev *hdev = hcon->hdev; |
| 520 | u8 local_dist = 0, remote_dist = 0; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 521 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 522 | if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) { |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 523 | local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
| 524 | remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 525 | authreq |= SMP_AUTH_BONDING; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 526 | } else { |
| 527 | authreq &= ~SMP_AUTH_BONDING; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 528 | } |
| 529 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 530 | if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) |
| 531 | remote_dist |= SMP_DIST_ID_KEY; |
| 532 | |
Johan Hedberg | 863efaf | 2014-02-22 19:06:32 +0200 | [diff] [blame] | 533 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 534 | local_dist |= SMP_DIST_ID_KEY; |
| 535 | |
Johan Hedberg | df8e1a4 | 2014-06-06 10:39:56 +0300 | [diff] [blame] | 536 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) { |
| 537 | if ((authreq & SMP_AUTH_SC) && |
| 538 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 539 | local_dist |= SMP_DIST_LINK_KEY; |
| 540 | remote_dist |= SMP_DIST_LINK_KEY; |
| 541 | } |
| 542 | } else { |
| 543 | authreq &= ~SMP_AUTH_SC; |
| 544 | } |
| 545 | |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 546 | if (rsp == NULL) { |
| 547 | req->io_capability = conn->hcon->io_capability; |
| 548 | req->oob_flag = SMP_OOB_NOT_PRESENT; |
| 549 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 550 | req->init_key_dist = local_dist; |
| 551 | req->resp_key_dist = remote_dist; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 552 | req->auth_req = (authreq & AUTH_REQ_MASK(hdev)); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 553 | |
| 554 | smp->remote_key_dist = remote_dist; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | |
| 558 | rsp->io_capability = conn->hcon->io_capability; |
| 559 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; |
| 560 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 561 | rsp->init_key_dist = req->init_key_dist & remote_dist; |
| 562 | rsp->resp_key_dist = req->resp_key_dist & local_dist; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 563 | rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev)); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 564 | |
| 565 | smp->remote_key_dist = rsp->init_key_dist; |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 566 | } |
| 567 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 568 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
| 569 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 570 | struct l2cap_chan *chan = conn->smp; |
| 571 | struct smp_chan *smp = chan->data; |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 572 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 573 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 574 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 575 | return SMP_ENC_KEY_SIZE; |
| 576 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 577 | smp->enc_key_size = max_key_size; |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 582 | static void smp_chan_destroy(struct l2cap_conn *conn) |
| 583 | { |
| 584 | struct l2cap_chan *chan = conn->smp; |
| 585 | struct smp_chan *smp = chan->data; |
| 586 | bool complete; |
| 587 | |
| 588 | BUG_ON(!smp); |
| 589 | |
| 590 | cancel_delayed_work_sync(&smp->security_timer); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 591 | |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 592 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); |
| 593 | mgmt_smp_complete(conn->hcon, complete); |
| 594 | |
| 595 | kfree(smp->csrk); |
| 596 | kfree(smp->slave_csrk); |
| 597 | |
| 598 | crypto_free_blkcipher(smp->tfm_aes); |
Johan Hedberg | 407cecf | 2014-05-02 14:19:47 +0300 | [diff] [blame] | 599 | crypto_free_hash(smp->tfm_cmac); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 600 | |
| 601 | /* If pairing failed clean up any keys we might have */ |
| 602 | if (!complete) { |
| 603 | if (smp->ltk) { |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 604 | list_del_rcu(&smp->ltk->list); |
| 605 | kfree_rcu(smp->ltk, rcu); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | if (smp->slave_ltk) { |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 609 | list_del_rcu(&smp->slave_ltk->list); |
| 610 | kfree_rcu(smp->slave_ltk, rcu); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (smp->remote_irk) { |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 614 | list_del_rcu(&smp->remote_irk->list); |
| 615 | kfree_rcu(smp->remote_irk, rcu); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | chan->data = NULL; |
| 620 | kfree(smp); |
| 621 | hci_conn_drop(conn->hcon); |
| 622 | } |
| 623 | |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 624 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 625 | { |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 626 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 627 | struct l2cap_chan *chan = conn->smp; |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 628 | |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 629 | if (reason) |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 630 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 631 | &reason); |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 632 | |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 633 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 634 | mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); |
Vinicius Costa Gomes | f1c09c0 | 2012-02-01 18:27:56 -0300 | [diff] [blame] | 635 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 636 | if (chan->data) |
Vinicius Costa Gomes | f1c09c0 | 2012-02-01 18:27:56 -0300 | [diff] [blame] | 637 | smp_chan_destroy(conn); |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 640 | #define JUST_WORKS 0x00 |
| 641 | #define JUST_CFM 0x01 |
| 642 | #define REQ_PASSKEY 0x02 |
| 643 | #define CFM_PASSKEY 0x03 |
| 644 | #define REQ_OOB 0x04 |
| 645 | #define OVERLAP 0xFF |
| 646 | |
| 647 | static const u8 gen_method[5][5] = { |
| 648 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, |
| 649 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, |
| 650 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, |
| 651 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, |
| 652 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, |
| 653 | }; |
| 654 | |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 655 | static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io) |
| 656 | { |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 657 | /* If either side has unknown io_caps, use JUST_CFM (which gets |
| 658 | * converted later to JUST_WORKS if we're initiators. |
| 659 | */ |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 660 | if (local_io > SMP_IO_KEYBOARD_DISPLAY || |
| 661 | remote_io > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 662 | return JUST_CFM; |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 663 | |
| 664 | return gen_method[remote_io][local_io]; |
| 665 | } |
| 666 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 667 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, |
| 668 | u8 local_io, u8 remote_io) |
| 669 | { |
| 670 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 671 | struct l2cap_chan *chan = conn->smp; |
| 672 | struct smp_chan *smp = chan->data; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 673 | u8 method; |
| 674 | u32 passkey = 0; |
| 675 | int ret = 0; |
| 676 | |
| 677 | /* Initialize key for JUST WORKS */ |
| 678 | memset(smp->tk, 0, sizeof(smp->tk)); |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 679 | clear_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 680 | |
| 681 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); |
| 682 | |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 683 | /* If neither side wants MITM, either "just" confirm an incoming |
| 684 | * request or use just-works for outgoing ones. The JUST_CFM |
| 685 | * will be converted to JUST_WORKS if necessary later in this |
| 686 | * function. If either side has MITM look up the method from the |
| 687 | * table. |
| 688 | */ |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 689 | if (!(auth & SMP_AUTH_MITM)) |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 690 | method = JUST_CFM; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 691 | else |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 692 | method = get_auth_method(smp, local_io, remote_io); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 693 | |
Johan Hedberg | a82505c | 2014-03-24 14:39:07 +0200 | [diff] [blame] | 694 | /* Don't confirm locally initiated pairing attempts */ |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 695 | if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) |
Johan Hedberg | a82505c | 2014-03-24 14:39:07 +0200 | [diff] [blame] | 696 | method = JUST_WORKS; |
| 697 | |
Johan Hedberg | 02f3e25 | 2014-07-16 15:09:13 +0300 | [diff] [blame] | 698 | /* Don't bother user space with no IO capabilities */ |
| 699 | if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
| 700 | method = JUST_WORKS; |
| 701 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 702 | /* If Just Works, Continue with Zero TK */ |
| 703 | if (method == JUST_WORKS) { |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 704 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | /* Not Just Works/Confirm results in MITM Authentication */ |
Johan Hedberg | 5eb596f | 2014-09-18 11:26:32 +0300 | [diff] [blame] | 709 | if (method != JUST_CFM) { |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 710 | set_bit(SMP_FLAG_MITM_AUTH, &smp->flags); |
Johan Hedberg | 5eb596f | 2014-09-18 11:26:32 +0300 | [diff] [blame] | 711 | if (hcon->pending_sec_level < BT_SECURITY_HIGH) |
| 712 | hcon->pending_sec_level = BT_SECURITY_HIGH; |
| 713 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 714 | |
| 715 | /* If both devices have Keyoard-Display I/O, the master |
| 716 | * Confirms and the slave Enters the passkey. |
| 717 | */ |
| 718 | if (method == OVERLAP) { |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 719 | if (hcon->role == HCI_ROLE_MASTER) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 720 | method = CFM_PASSKEY; |
| 721 | else |
| 722 | method = REQ_PASSKEY; |
| 723 | } |
| 724 | |
Johan Hedberg | 01ad34d | 2014-03-19 14:14:53 +0200 | [diff] [blame] | 725 | /* Generate random passkey. */ |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 726 | if (method == CFM_PASSKEY) { |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 727 | memset(smp->tk, 0, sizeof(smp->tk)); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 728 | get_random_bytes(&passkey, sizeof(passkey)); |
| 729 | passkey %= 1000000; |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 730 | put_unaligned_le32(passkey, smp->tk); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 731 | BT_DBG("PassKey: %d", passkey); |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 732 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 733 | } |
| 734 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 735 | if (method == REQ_PASSKEY) |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 736 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 737 | hcon->type, hcon->dst_type); |
Johan Hedberg | 4eb65e6 | 2014-03-24 14:39:05 +0200 | [diff] [blame] | 738 | else if (method == JUST_CFM) |
| 739 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, |
| 740 | hcon->type, hcon->dst_type, |
| 741 | passkey, 1); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 742 | else |
Johan Hedberg | 01ad34d | 2014-03-19 14:14:53 +0200 | [diff] [blame] | 743 | ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 744 | hcon->type, hcon->dst_type, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 745 | passkey, 0); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 746 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 747 | return ret; |
| 748 | } |
| 749 | |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 750 | static u8 smp_confirm(struct smp_chan *smp) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 751 | { |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 752 | struct l2cap_conn *conn = smp->conn; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 753 | struct smp_cmd_pairing_confirm cp; |
| 754 | int ret; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 755 | |
| 756 | BT_DBG("conn %p", conn); |
| 757 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 758 | ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp, |
Johan Hedberg | b1cd5fd | 2014-02-28 12:54:17 +0200 | [diff] [blame] | 759 | conn->hcon->init_addr_type, &conn->hcon->init_addr, |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 760 | conn->hcon->resp_addr_type, &conn->hcon->resp_addr, |
| 761 | cp.confirm_val); |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 762 | if (ret) |
| 763 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 764 | |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 765 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 766 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 767 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); |
| 768 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 769 | if (conn->hcon->out) |
| 770 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 771 | else |
| 772 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 773 | |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 774 | return 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 775 | } |
| 776 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 777 | static u8 smp_random(struct smp_chan *smp) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 778 | { |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 779 | struct l2cap_conn *conn = smp->conn; |
| 780 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 781 | u8 confirm[16]; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 782 | int ret; |
| 783 | |
Johan Hedberg | ec70f36 | 2014-06-27 14:23:04 +0300 | [diff] [blame] | 784 | if (IS_ERR_OR_NULL(smp->tfm_aes)) |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 785 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 786 | |
| 787 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 788 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 789 | ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
Johan Hedberg | b1cd5fd | 2014-02-28 12:54:17 +0200 | [diff] [blame] | 790 | hcon->init_addr_type, &hcon->init_addr, |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 791 | hcon->resp_addr_type, &hcon->resp_addr, confirm); |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 792 | if (ret) |
| 793 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 794 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 795 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { |
| 796 | BT_ERR("Pairing failed (confirmation values mismatch)"); |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 797 | return SMP_CONFIRM_FAILED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | if (hcon->out) { |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 801 | u8 stk[16]; |
| 802 | __le64 rand = 0; |
| 803 | __le16 ediv = 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 804 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 805 | smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 806 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 807 | memset(stk + smp->enc_key_size, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 808 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 809 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 810 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
| 811 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 812 | |
| 813 | hci_le_start_enc(hcon, ediv, rand, stk); |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 814 | hcon->enc_key_size = smp->enc_key_size; |
Johan Hedberg | fe59a05 | 2014-07-01 19:14:12 +0300 | [diff] [blame] | 815 | set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 816 | } else { |
Johan Hedberg | fff3490 | 2014-06-10 15:19:50 +0300 | [diff] [blame] | 817 | u8 stk[16], auth; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 818 | __le64 rand = 0; |
| 819 | __le16 ediv = 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 820 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 821 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 822 | smp->prnd); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 823 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 824 | smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 825 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 826 | memset(stk + smp->enc_key_size, 0, |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 827 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 828 | |
Johan Hedberg | fff3490 | 2014-06-10 15:19:50 +0300 | [diff] [blame] | 829 | if (hcon->pending_sec_level == BT_SECURITY_HIGH) |
| 830 | auth = 1; |
| 831 | else |
| 832 | auth = 0; |
| 833 | |
Johan Hedberg | 7d5843b | 2014-06-16 19:25:15 +0300 | [diff] [blame] | 834 | /* Even though there's no _SLAVE suffix this is the |
| 835 | * slave STK we're adding for later lookup (the master |
| 836 | * STK never needs to be stored). |
| 837 | */ |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 838 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 839 | SMP_STK, auth, stk, smp->enc_key_size, ediv, rand); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 840 | } |
| 841 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 842 | return 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 843 | } |
| 844 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 845 | static void smp_notify_keys(struct l2cap_conn *conn) |
| 846 | { |
| 847 | struct l2cap_chan *chan = conn->smp; |
| 848 | struct smp_chan *smp = chan->data; |
| 849 | struct hci_conn *hcon = conn->hcon; |
| 850 | struct hci_dev *hdev = hcon->hdev; |
| 851 | struct smp_cmd_pairing *req = (void *) &smp->preq[1]; |
| 852 | struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; |
| 853 | bool persistent; |
| 854 | |
| 855 | if (smp->remote_irk) { |
| 856 | mgmt_new_irk(hdev, smp->remote_irk); |
| 857 | /* Now that user space can be considered to know the |
| 858 | * identity address track the connection based on it |
| 859 | * from now on. |
| 860 | */ |
| 861 | bacpy(&hcon->dst, &smp->remote_irk->bdaddr); |
| 862 | hcon->dst_type = smp->remote_irk->addr_type; |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 863 | queue_work(hdev->workqueue, &conn->id_addr_update_work); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 864 | |
| 865 | /* When receiving an indentity resolving key for |
| 866 | * a remote device that does not use a resolvable |
| 867 | * private address, just remove the key so that |
| 868 | * it is possible to use the controller white |
| 869 | * list for scanning. |
| 870 | * |
| 871 | * Userspace will have been told to not store |
| 872 | * this key at this point. So it is safe to |
| 873 | * just remove it. |
| 874 | */ |
| 875 | if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) { |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 876 | list_del_rcu(&smp->remote_irk->list); |
| 877 | kfree_rcu(smp->remote_irk, rcu); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 878 | smp->remote_irk = NULL; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | /* The LTKs and CSRKs should be persistent only if both sides |
| 883 | * had the bonding bit set in their authentication requests. |
| 884 | */ |
| 885 | persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING); |
| 886 | |
| 887 | if (smp->csrk) { |
| 888 | smp->csrk->bdaddr_type = hcon->dst_type; |
| 889 | bacpy(&smp->csrk->bdaddr, &hcon->dst); |
| 890 | mgmt_new_csrk(hdev, smp->csrk, persistent); |
| 891 | } |
| 892 | |
| 893 | if (smp->slave_csrk) { |
| 894 | smp->slave_csrk->bdaddr_type = hcon->dst_type; |
| 895 | bacpy(&smp->slave_csrk->bdaddr, &hcon->dst); |
| 896 | mgmt_new_csrk(hdev, smp->slave_csrk, persistent); |
| 897 | } |
| 898 | |
| 899 | if (smp->ltk) { |
| 900 | smp->ltk->bdaddr_type = hcon->dst_type; |
| 901 | bacpy(&smp->ltk->bdaddr, &hcon->dst); |
| 902 | mgmt_new_ltk(hdev, smp->ltk, persistent); |
| 903 | } |
| 904 | |
| 905 | if (smp->slave_ltk) { |
| 906 | smp->slave_ltk->bdaddr_type = hcon->dst_type; |
| 907 | bacpy(&smp->slave_ltk->bdaddr, &hcon->dst); |
| 908 | mgmt_new_ltk(hdev, smp->slave_ltk, persistent); |
| 909 | } |
| 910 | } |
| 911 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 912 | static void smp_allow_key_dist(struct smp_chan *smp) |
| 913 | { |
| 914 | /* Allow the first expected phase 3 PDU. The rest of the PDUs |
| 915 | * will be allowed in each PDU handler to ensure we receive |
| 916 | * them in the correct order. |
| 917 | */ |
| 918 | if (smp->remote_key_dist & SMP_DIST_ENC_KEY) |
| 919 | SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO); |
| 920 | else if (smp->remote_key_dist & SMP_DIST_ID_KEY) |
| 921 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); |
| 922 | else if (smp->remote_key_dist & SMP_DIST_SIGN) |
| 923 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
| 924 | } |
| 925 | |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 926 | static void smp_distribute_keys(struct smp_chan *smp) |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 927 | { |
| 928 | struct smp_cmd_pairing *req, *rsp; |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 929 | struct l2cap_conn *conn = smp->conn; |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 930 | struct hci_conn *hcon = conn->hcon; |
| 931 | struct hci_dev *hdev = hcon->hdev; |
| 932 | __u8 *keydist; |
| 933 | |
| 934 | BT_DBG("conn %p", conn); |
| 935 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 936 | rsp = (void *) &smp->prsp[1]; |
| 937 | |
| 938 | /* The responder sends its keys first */ |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 939 | if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) { |
| 940 | smp_allow_key_dist(smp); |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 941 | return; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 942 | } |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 943 | |
| 944 | req = (void *) &smp->preq[1]; |
| 945 | |
| 946 | if (hcon->out) { |
| 947 | keydist = &rsp->init_key_dist; |
| 948 | *keydist &= req->init_key_dist; |
| 949 | } else { |
| 950 | keydist = &rsp->resp_key_dist; |
| 951 | *keydist &= req->resp_key_dist; |
| 952 | } |
| 953 | |
| 954 | BT_DBG("keydist 0x%x", *keydist); |
| 955 | |
| 956 | if (*keydist & SMP_DIST_ENC_KEY) { |
| 957 | struct smp_cmd_encrypt_info enc; |
| 958 | struct smp_cmd_master_ident ident; |
| 959 | struct smp_ltk *ltk; |
| 960 | u8 authenticated; |
| 961 | __le16 ediv; |
| 962 | __le64 rand; |
| 963 | |
| 964 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); |
| 965 | get_random_bytes(&ediv, sizeof(ediv)); |
| 966 | get_random_bytes(&rand, sizeof(rand)); |
| 967 | |
| 968 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); |
| 969 | |
| 970 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; |
| 971 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, |
| 972 | SMP_LTK_SLAVE, authenticated, enc.ltk, |
| 973 | smp->enc_key_size, ediv, rand); |
| 974 | smp->slave_ltk = ltk; |
| 975 | |
| 976 | ident.ediv = ediv; |
| 977 | ident.rand = rand; |
| 978 | |
| 979 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); |
| 980 | |
| 981 | *keydist &= ~SMP_DIST_ENC_KEY; |
| 982 | } |
| 983 | |
| 984 | if (*keydist & SMP_DIST_ID_KEY) { |
| 985 | struct smp_cmd_ident_addr_info addrinfo; |
| 986 | struct smp_cmd_ident_info idinfo; |
| 987 | |
| 988 | memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); |
| 989 | |
| 990 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); |
| 991 | |
| 992 | /* The hci_conn contains the local identity address |
| 993 | * after the connection has been established. |
| 994 | * |
| 995 | * This is true even when the connection has been |
| 996 | * established using a resolvable random address. |
| 997 | */ |
| 998 | bacpy(&addrinfo.bdaddr, &hcon->src); |
| 999 | addrinfo.addr_type = hcon->src_type; |
| 1000 | |
| 1001 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), |
| 1002 | &addrinfo); |
| 1003 | |
| 1004 | *keydist &= ~SMP_DIST_ID_KEY; |
| 1005 | } |
| 1006 | |
| 1007 | if (*keydist & SMP_DIST_SIGN) { |
| 1008 | struct smp_cmd_sign_info sign; |
| 1009 | struct smp_csrk *csrk; |
| 1010 | |
| 1011 | /* Generate a new random key */ |
| 1012 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); |
| 1013 | |
| 1014 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); |
| 1015 | if (csrk) { |
| 1016 | csrk->master = 0x00; |
| 1017 | memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); |
| 1018 | } |
| 1019 | smp->slave_csrk = csrk; |
| 1020 | |
| 1021 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); |
| 1022 | |
| 1023 | *keydist &= ~SMP_DIST_SIGN; |
| 1024 | } |
| 1025 | |
| 1026 | /* If there are still keys to be received wait for them */ |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1027 | if (smp->remote_key_dist & KEY_DIST_MASK) { |
| 1028 | smp_allow_key_dist(smp); |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 1029 | return; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1030 | } |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1031 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1032 | set_bit(SMP_FLAG_COMPLETE, &smp->flags); |
| 1033 | smp_notify_keys(conn); |
| 1034 | |
| 1035 | smp_chan_destroy(conn); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1036 | } |
| 1037 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1038 | static void smp_timeout(struct work_struct *work) |
| 1039 | { |
| 1040 | struct smp_chan *smp = container_of(work, struct smp_chan, |
| 1041 | security_timer.work); |
| 1042 | struct l2cap_conn *conn = smp->conn; |
| 1043 | |
| 1044 | BT_DBG("conn %p", conn); |
| 1045 | |
Johan Hedberg | 1e91c29 | 2014-08-18 20:33:29 +0300 | [diff] [blame] | 1046 | hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1047 | } |
| 1048 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1049 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) |
| 1050 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1051 | struct l2cap_chan *chan = conn->smp; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1052 | struct smp_chan *smp; |
| 1053 | |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 1054 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1055 | if (!smp) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1056 | return NULL; |
| 1057 | |
Johan Hedberg | 6a7bd10 | 2014-06-27 14:23:03 +0300 | [diff] [blame] | 1058 | smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); |
| 1059 | if (IS_ERR(smp->tfm_aes)) { |
| 1060 | BT_ERR("Unable to create ECB crypto context"); |
| 1061 | kfree(smp); |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | |
Johan Hedberg | 407cecf | 2014-05-02 14:19:47 +0300 | [diff] [blame] | 1065 | smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC); |
| 1066 | if (IS_ERR(smp->tfm_cmac)) { |
| 1067 | BT_ERR("Unable to create CMAC crypto context"); |
| 1068 | crypto_free_blkcipher(smp->tfm_aes); |
| 1069 | kfree(smp); |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1073 | smp->conn = conn; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1074 | chan->data = smp; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1075 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1076 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL); |
| 1077 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1078 | INIT_DELAYED_WORK(&smp->security_timer, smp_timeout); |
| 1079 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1080 | hci_conn_hold(conn->hcon); |
| 1081 | |
| 1082 | return smp; |
| 1083 | } |
| 1084 | |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1085 | static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16]) |
| 1086 | { |
| 1087 | struct hci_conn *hcon = smp->conn->hcon; |
| 1088 | u8 *na, *nb, a[7], b[7]; |
| 1089 | |
| 1090 | if (hcon->out) { |
| 1091 | na = smp->prnd; |
| 1092 | nb = smp->rrnd; |
| 1093 | } else { |
| 1094 | na = smp->rrnd; |
| 1095 | nb = smp->prnd; |
| 1096 | } |
| 1097 | |
| 1098 | memcpy(a, &hcon->init_addr, 6); |
| 1099 | memcpy(b, &hcon->resp_addr, 6); |
| 1100 | a[6] = hcon->init_addr_type; |
| 1101 | b[6] = hcon->resp_addr_type; |
| 1102 | |
| 1103 | return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk); |
| 1104 | } |
| 1105 | |
| 1106 | static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey) |
| 1107 | { |
| 1108 | struct hci_conn *hcon = smp->conn->hcon; |
| 1109 | struct smp_cmd_dhkey_check check; |
| 1110 | u8 a[7], b[7], *local_addr, *remote_addr; |
| 1111 | u8 io_cap[3], r[16]; |
| 1112 | |
| 1113 | switch (mgmt_op) { |
| 1114 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: |
| 1115 | smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED); |
| 1116 | return 0; |
| 1117 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
| 1118 | smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED); |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | memcpy(a, &hcon->init_addr, 6); |
| 1123 | memcpy(b, &hcon->resp_addr, 6); |
| 1124 | a[6] = hcon->init_addr_type; |
| 1125 | b[6] = hcon->resp_addr_type; |
| 1126 | |
| 1127 | if (hcon->out) { |
| 1128 | local_addr = a; |
| 1129 | remote_addr = b; |
| 1130 | memcpy(io_cap, &smp->preq[1], 3); |
| 1131 | } else { |
| 1132 | local_addr = b; |
| 1133 | remote_addr = a; |
| 1134 | memcpy(io_cap, &smp->prsp[1], 3); |
| 1135 | } |
| 1136 | |
| 1137 | memcpy(r, &passkey, sizeof(passkey)); |
| 1138 | memset(r + sizeof(passkey), 0, sizeof(r) - sizeof(passkey)); |
| 1139 | |
| 1140 | smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap, |
| 1141 | local_addr, remote_addr, check.e); |
| 1142 | |
| 1143 | smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check); |
| 1144 | |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1148 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
| 1149 | { |
Johan Hedberg | b10e801 | 2014-06-27 14:23:07 +0300 | [diff] [blame] | 1150 | struct l2cap_conn *conn = hcon->l2cap_data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1151 | struct l2cap_chan *chan; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1152 | struct smp_chan *smp; |
| 1153 | u32 value; |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1154 | int err; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1155 | |
| 1156 | BT_DBG(""); |
| 1157 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1158 | if (!conn) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1159 | return -ENOTCONN; |
| 1160 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1161 | chan = conn->smp; |
| 1162 | if (!chan) |
| 1163 | return -ENOTCONN; |
| 1164 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1165 | l2cap_chan_lock(chan); |
| 1166 | if (!chan->data) { |
| 1167 | err = -ENOTCONN; |
| 1168 | goto unlock; |
| 1169 | } |
| 1170 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1171 | smp = chan->data; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1172 | |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1173 | if (test_bit(SMP_FLAG_SC, &smp->flags)) { |
| 1174 | err = sc_user_reply(smp, mgmt_op, passkey); |
| 1175 | goto unlock; |
| 1176 | } |
| 1177 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1178 | switch (mgmt_op) { |
| 1179 | case MGMT_OP_USER_PASSKEY_REPLY: |
| 1180 | value = le32_to_cpu(passkey); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1181 | memset(smp->tk, 0, sizeof(smp->tk)); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1182 | BT_DBG("PassKey: %d", value); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1183 | put_unaligned_le32(value, smp->tk); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1184 | /* Fall Through */ |
| 1185 | case MGMT_OP_USER_CONFIRM_REPLY: |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1186 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1187 | break; |
| 1188 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: |
| 1189 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1190 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1191 | err = 0; |
| 1192 | goto unlock; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1193 | default: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1194 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1195 | err = -EOPNOTSUPP; |
| 1196 | goto unlock; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1197 | } |
| 1198 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1199 | err = 0; |
| 1200 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1201 | /* If it is our turn to send Pairing Confirm, do so now */ |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 1202 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) { |
| 1203 | u8 rsp = smp_confirm(smp); |
| 1204 | if (rsp) |
| 1205 | smp_failure(conn, rsp); |
| 1206 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1207 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1208 | unlock: |
| 1209 | l2cap_chan_unlock(chan); |
| 1210 | return err; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1213 | static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1214 | { |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1215 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1216 | struct l2cap_chan *chan = conn->smp; |
Johan Hedberg | b3c6410 | 2014-07-10 11:02:07 +0300 | [diff] [blame] | 1217 | struct hci_dev *hdev = conn->hcon->hdev; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1218 | struct smp_chan *smp; |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1219 | u8 key_size, auth, sec_level; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1220 | int ret; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1221 | |
| 1222 | BT_DBG("conn %p", conn); |
| 1223 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1224 | if (skb->len < sizeof(*req)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1225 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1226 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1227 | if (conn->hcon->role != HCI_ROLE_SLAVE) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1228 | return SMP_CMD_NOTSUPP; |
| 1229 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1230 | if (!chan->data) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1231 | smp = smp_chan_create(conn); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1232 | else |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1233 | smp = chan->data; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1234 | |
Andrei Emeltchenko | d08fd0e | 2012-07-19 17:03:43 +0300 | [diff] [blame] | 1235 | if (!smp) |
| 1236 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1237 | |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1238 | /* We didn't start the pairing, so match remote */ |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1239 | auth = req->auth_req & AUTH_REQ_MASK(hdev); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1240 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1241 | if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) && |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1242 | (auth & SMP_AUTH_BONDING)) |
Johan Hedberg | b3c6410 | 2014-07-10 11:02:07 +0300 | [diff] [blame] | 1243 | return SMP_PAIRING_NOTSUPP; |
| 1244 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1245 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 1246 | memcpy(&smp->preq[1], req, sizeof(*req)); |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1247 | skb_pull(skb, sizeof(*req)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1248 | |
Johan Hedberg | 5be5e27 | 2014-09-10 17:58:54 -0700 | [diff] [blame] | 1249 | if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
Johan Hedberg | 1afc2a1 | 2014-09-10 17:37:44 -0700 | [diff] [blame] | 1250 | sec_level = BT_SECURITY_MEDIUM; |
| 1251 | else |
| 1252 | sec_level = authreq_to_seclevel(auth); |
| 1253 | |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1254 | if (sec_level > conn->hcon->pending_sec_level) |
| 1255 | conn->hcon->pending_sec_level = sec_level; |
Ido Yariv | fdde0a2 | 2012-03-05 20:09:38 +0200 | [diff] [blame] | 1256 | |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 1257 | /* If we need MITM check that it can be achieved */ |
Johan Hedberg | 2ed8f65 | 2014-06-17 13:07:39 +0300 | [diff] [blame] | 1258 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { |
| 1259 | u8 method; |
| 1260 | |
| 1261 | method = get_auth_method(smp, conn->hcon->io_capability, |
| 1262 | req->io_capability); |
| 1263 | if (method == JUST_WORKS || method == JUST_CFM) |
| 1264 | return SMP_AUTH_REQUIREMENTS; |
| 1265 | } |
| 1266 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1267 | build_pairing_cmd(conn, req, &rsp, auth); |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1268 | |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 1269 | if (rsp.auth_req & SMP_AUTH_SC) |
| 1270 | set_bit(SMP_FLAG_SC, &smp->flags); |
| 1271 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1272 | key_size = min(req->max_key_size, rsp.max_key_size); |
| 1273 | if (check_enc_key_size(conn, key_size)) |
| 1274 | return SMP_ENC_KEY_SIZE; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1275 | |
Johan Hedberg | e84a6b1 | 2013-12-02 10:49:03 +0200 | [diff] [blame] | 1276 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1277 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1278 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 1279 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 1280 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1281 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1282 | |
| 1283 | clear_bit(SMP_FLAG_INITIATOR, &smp->flags); |
| 1284 | |
| 1285 | if (test_bit(SMP_FLAG_SC, &smp->flags)) { |
| 1286 | SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); |
| 1287 | /* Clear bits which are generated but not distributed */ |
| 1288 | smp->remote_key_dist &= ~SMP_SC_NO_DIST; |
| 1289 | /* Wait for Public Key from Initiating Device */ |
| 1290 | return 0; |
| 1291 | } else { |
| 1292 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 1293 | } |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1294 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1295 | /* Request setup of TK */ |
| 1296 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); |
| 1297 | if (ret) |
| 1298 | return SMP_UNSPECIFIED; |
| 1299 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1300 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1301 | } |
| 1302 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1303 | static u8 sc_send_public_key(struct smp_chan *smp) |
| 1304 | { |
| 1305 | BT_DBG(""); |
| 1306 | |
| 1307 | /* Generate local key pair for Secure Connections */ |
| 1308 | if (!ecc_make_key(smp->local_pk, smp->local_sk)) |
| 1309 | return SMP_UNSPECIFIED; |
| 1310 | |
| 1311 | BT_DBG("Local Public Key X: %32phN", smp->local_pk); |
| 1312 | BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]); |
| 1313 | BT_DBG("Local Private Key: %32phN", smp->local_sk); |
| 1314 | |
| 1315 | smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk); |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1320 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1321 | { |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1322 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1323 | struct l2cap_chan *chan = conn->smp; |
| 1324 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1325 | struct hci_dev *hdev = conn->hcon->hdev; |
Johan Hedberg | 3a7dbfb | 2014-09-10 17:37:41 -0700 | [diff] [blame] | 1326 | u8 key_size, auth; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1327 | int ret; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1328 | |
| 1329 | BT_DBG("conn %p", conn); |
| 1330 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1331 | if (skb->len < sizeof(*rsp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1332 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1333 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1334 | if (conn->hcon->role != HCI_ROLE_MASTER) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1335 | return SMP_CMD_NOTSUPP; |
| 1336 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1337 | skb_pull(skb, sizeof(*rsp)); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1338 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1339 | req = (void *) &smp->preq[1]; |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1340 | |
| 1341 | key_size = min(req->max_key_size, rsp->max_key_size); |
| 1342 | if (check_enc_key_size(conn, key_size)) |
| 1343 | return SMP_ENC_KEY_SIZE; |
| 1344 | |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1345 | auth = rsp->auth_req & AUTH_REQ_MASK(hdev); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1346 | |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 1347 | if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC)) |
| 1348 | set_bit(SMP_FLAG_SC, &smp->flags); |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 1349 | else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH) |
| 1350 | conn->hcon->pending_sec_level = BT_SECURITY_HIGH; |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 1351 | |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 1352 | /* If we need MITM check that it can be achieved */ |
Johan Hedberg | 2ed8f65 | 2014-06-17 13:07:39 +0300 | [diff] [blame] | 1353 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { |
| 1354 | u8 method; |
| 1355 | |
| 1356 | method = get_auth_method(smp, req->io_capability, |
| 1357 | rsp->io_capability); |
| 1358 | if (method == JUST_WORKS || method == JUST_CFM) |
| 1359 | return SMP_AUTH_REQUIREMENTS; |
| 1360 | } |
| 1361 | |
Johan Hedberg | e84a6b1 | 2013-12-02 10:49:03 +0200 | [diff] [blame] | 1362 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1363 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1364 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 1365 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1366 | |
Johan Hedberg | fdcc4be | 2014-03-14 10:53:50 +0200 | [diff] [blame] | 1367 | /* Update remote key distribution in case the remote cleared |
| 1368 | * some bits that we had enabled in our request. |
| 1369 | */ |
| 1370 | smp->remote_key_dist &= rsp->resp_key_dist; |
| 1371 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1372 | if (test_bit(SMP_FLAG_SC, &smp->flags)) { |
| 1373 | /* Clear bits which are generated but not distributed */ |
| 1374 | smp->remote_key_dist &= ~SMP_SC_NO_DIST; |
| 1375 | SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); |
| 1376 | return sc_send_public_key(smp); |
| 1377 | } |
| 1378 | |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1379 | auth |= req->auth_req; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1380 | |
Johan Hedberg | 476585e | 2012-06-06 18:54:15 +0800 | [diff] [blame] | 1381 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1382 | if (ret) |
| 1383 | return SMP_UNSPECIFIED; |
| 1384 | |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1385 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1386 | |
| 1387 | /* Can't compose response until we have been confirmed */ |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1388 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 1389 | return smp_confirm(smp); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1390 | |
| 1391 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1392 | } |
| 1393 | |
Johan Hedberg | dcee2b3 | 2014-06-06 11:36:38 +0300 | [diff] [blame] | 1394 | static u8 sc_check_confirm(struct smp_chan *smp) |
| 1395 | { |
| 1396 | struct l2cap_conn *conn = smp->conn; |
| 1397 | |
| 1398 | BT_DBG(""); |
| 1399 | |
| 1400 | /* Public Key exchange must happen before any other steps */ |
| 1401 | if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags)) |
| 1402 | return SMP_UNSPECIFIED; |
| 1403 | |
| 1404 | if (conn->hcon->out) { |
| 1405 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 1406 | smp->prnd); |
| 1407 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 1408 | } |
| 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1413 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1414 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1415 | struct l2cap_chan *chan = conn->smp; |
| 1416 | struct smp_chan *smp = chan->data; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1417 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1418 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 1419 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1420 | if (skb->len < sizeof(smp->pcnf)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1421 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1422 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1423 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
| 1424 | skb_pull(skb, sizeof(smp->pcnf)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1425 | |
Johan Hedberg | dcee2b3 | 2014-06-06 11:36:38 +0300 | [diff] [blame] | 1426 | if (test_bit(SMP_FLAG_SC, &smp->flags)) |
| 1427 | return sc_check_confirm(smp); |
| 1428 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1429 | if (conn->hcon->out) { |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1430 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 1431 | smp->prnd); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1432 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
| 1436 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 1437 | return smp_confirm(smp); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1438 | else |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1439 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1440 | |
| 1441 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1442 | } |
| 1443 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1444 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1445 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1446 | struct l2cap_chan *chan = conn->smp; |
| 1447 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 191dc7f | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 1448 | struct hci_conn *hcon = conn->hcon; |
| 1449 | u8 *pkax, *pkbx, *na, *nb; |
| 1450 | u32 passkey; |
| 1451 | int err; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1452 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1453 | BT_DBG("conn %p", conn); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1454 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1455 | if (skb->len < sizeof(smp->rrnd)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1456 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1457 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1458 | memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd)); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1459 | skb_pull(skb, sizeof(smp->rrnd)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1460 | |
Johan Hedberg | 191dc7f | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 1461 | if (!test_bit(SMP_FLAG_SC, &smp->flags)) |
| 1462 | return smp_random(smp); |
| 1463 | |
| 1464 | if (hcon->out) { |
| 1465 | u8 cfm[16]; |
| 1466 | |
| 1467 | err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk, |
| 1468 | smp->rrnd, 0, cfm); |
| 1469 | if (err) |
| 1470 | return SMP_UNSPECIFIED; |
| 1471 | |
| 1472 | if (memcmp(smp->pcnf, cfm, 16)) |
| 1473 | return SMP_CONFIRM_FAILED; |
| 1474 | |
| 1475 | pkax = smp->local_pk; |
| 1476 | pkbx = smp->remote_pk; |
| 1477 | na = smp->prnd; |
| 1478 | nb = smp->rrnd; |
| 1479 | } else { |
| 1480 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 1481 | smp->prnd); |
| 1482 | SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); |
| 1483 | |
| 1484 | pkax = smp->remote_pk; |
| 1485 | pkbx = smp->local_pk; |
| 1486 | na = smp->rrnd; |
| 1487 | nb = smp->prnd; |
| 1488 | } |
| 1489 | |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1490 | /* Generate MacKey and LTK */ |
| 1491 | err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk); |
| 1492 | if (err) |
| 1493 | return SMP_UNSPECIFIED; |
| 1494 | |
Johan Hedberg | 191dc7f | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 1495 | err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey); |
| 1496 | if (err) |
| 1497 | return SMP_UNSPECIFIED; |
| 1498 | |
| 1499 | err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, |
| 1500 | hcon->type, hcon->dst_type, |
| 1501 | passkey, 0); |
| 1502 | if (err) |
| 1503 | return SMP_UNSPECIFIED; |
| 1504 | |
| 1505 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1506 | } |
| 1507 | |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1508 | static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1509 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1510 | struct smp_ltk *key; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1511 | struct hci_conn *hcon = conn->hcon; |
| 1512 | |
Johan Hedberg | f3a73d9 | 2014-05-29 15:02:59 +0300 | [diff] [blame] | 1513 | key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role); |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1514 | if (!key) |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1515 | return false; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1516 | |
Johan Hedberg | a6f7833 | 2014-09-10 17:37:45 -0700 | [diff] [blame] | 1517 | if (smp_ltk_sec_level(key) < sec_level) |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1518 | return false; |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 1519 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1520 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1521 | return true; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1522 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1523 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
| 1524 | hcon->enc_key_size = key->enc_size; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1525 | |
Johan Hedberg | fe59a05 | 2014-07-01 19:14:12 +0300 | [diff] [blame] | 1526 | /* We never store STKs for master role, so clear this flag */ |
| 1527 | clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); |
| 1528 | |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1529 | return true; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1530 | } |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 1531 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1532 | bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level, |
| 1533 | enum smp_key_pref key_pref) |
Johan Hedberg | 854f472 | 2014-07-01 18:40:20 +0300 | [diff] [blame] | 1534 | { |
| 1535 | if (sec_level == BT_SECURITY_LOW) |
| 1536 | return true; |
| 1537 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1538 | /* If we're encrypted with an STK but the caller prefers using |
| 1539 | * LTK claim insufficient security. This way we allow the |
| 1540 | * connection to be re-encrypted with an LTK, even if the LTK |
| 1541 | * provides the same level of security. Only exception is if we |
| 1542 | * don't have an LTK (e.g. because of key distribution bits). |
Johan Hedberg | 9ab65d60 | 2014-07-01 19:14:13 +0300 | [diff] [blame] | 1543 | */ |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1544 | if (key_pref == SMP_USE_LTK && |
| 1545 | test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) && |
Johan Hedberg | f3a73d9 | 2014-05-29 15:02:59 +0300 | [diff] [blame] | 1546 | hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role)) |
Johan Hedberg | 9ab65d60 | 2014-07-01 19:14:13 +0300 | [diff] [blame] | 1547 | return false; |
| 1548 | |
Johan Hedberg | 854f472 | 2014-07-01 18:40:20 +0300 | [diff] [blame] | 1549 | if (hcon->sec_level >= sec_level) |
| 1550 | return true; |
| 1551 | |
| 1552 | return false; |
| 1553 | } |
| 1554 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1555 | static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1556 | { |
| 1557 | struct smp_cmd_security_req *rp = (void *) skb->data; |
| 1558 | struct smp_cmd_pairing cp; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 1559 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1560 | struct hci_dev *hdev = hcon->hdev; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1561 | struct smp_chan *smp; |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1562 | u8 sec_level, auth; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1563 | |
| 1564 | BT_DBG("conn %p", conn); |
| 1565 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1566 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1567 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1568 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1569 | if (hcon->role != HCI_ROLE_MASTER) |
Johan Hedberg | 86ca9ea | 2013-11-05 11:30:39 +0200 | [diff] [blame] | 1570 | return SMP_CMD_NOTSUPP; |
| 1571 | |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1572 | auth = rp->auth_req & AUTH_REQ_MASK(hdev); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1573 | |
Johan Hedberg | 5be5e27 | 2014-09-10 17:58:54 -0700 | [diff] [blame] | 1574 | if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
Johan Hedberg | 1afc2a1 | 2014-09-10 17:37:44 -0700 | [diff] [blame] | 1575 | sec_level = BT_SECURITY_MEDIUM; |
| 1576 | else |
| 1577 | sec_level = authreq_to_seclevel(auth); |
| 1578 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1579 | if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) |
Johan Hedberg | 854f472 | 2014-07-01 18:40:20 +0300 | [diff] [blame] | 1580 | return 0; |
| 1581 | |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1582 | if (sec_level > hcon->pending_sec_level) |
| 1583 | hcon->pending_sec_level = sec_level; |
Vinicius Costa Gomes | feb45eb | 2011-08-25 20:02:35 -0300 | [diff] [blame] | 1584 | |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 1585 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1586 | return 0; |
| 1587 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1588 | smp = smp_chan_create(conn); |
Johan Hedberg | c29d244 | 2014-06-16 19:25:14 +0300 | [diff] [blame] | 1589 | if (!smp) |
| 1590 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1591 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1592 | if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) && |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1593 | (auth & SMP_AUTH_BONDING)) |
Johan Hedberg | 616d55b | 2014-07-29 14:18:48 +0300 | [diff] [blame] | 1594 | return SMP_PAIRING_NOTSUPP; |
| 1595 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1596 | skb_pull(skb, sizeof(*rp)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1597 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1598 | memset(&cp, 0, sizeof(cp)); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1599 | build_pairing_cmd(conn, &cp, NULL, auth); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1600 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1601 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 1602 | memcpy(&smp->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 1603 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1604 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1605 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 1606 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1607 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1608 | } |
| 1609 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1610 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1611 | { |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1612 | struct l2cap_conn *conn = hcon->l2cap_data; |
Johan Hedberg | c68b7f1 | 2014-09-06 06:59:10 +0300 | [diff] [blame] | 1613 | struct l2cap_chan *chan; |
Johan Hedberg | 0a66cf2 | 2014-03-24 14:39:03 +0200 | [diff] [blame] | 1614 | struct smp_chan *smp; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1615 | __u8 authreq; |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1616 | int ret; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1617 | |
Vinicius Costa Gomes | 3a0259b | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 1618 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
| 1619 | |
Johan Hedberg | 0a66cf2 | 2014-03-24 14:39:03 +0200 | [diff] [blame] | 1620 | /* This may be NULL if there's an unexpected disconnection */ |
| 1621 | if (!conn) |
| 1622 | return 1; |
| 1623 | |
Johan Hedberg | c68b7f1 | 2014-09-06 06:59:10 +0300 | [diff] [blame] | 1624 | chan = conn->smp; |
| 1625 | |
Johan Hedberg | 757aee0 | 2013-04-24 13:05:32 +0300 | [diff] [blame] | 1626 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
Andre Guedes | 2e65c9d | 2011-06-30 19:20:56 -0300 | [diff] [blame] | 1627 | return 1; |
| 1628 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1629 | if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 1630 | return 1; |
| 1631 | |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1632 | if (sec_level > hcon->pending_sec_level) |
| 1633 | hcon->pending_sec_level = sec_level; |
| 1634 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1635 | if (hcon->role == HCI_ROLE_MASTER) |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1636 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
| 1637 | return 0; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1638 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1639 | l2cap_chan_lock(chan); |
| 1640 | |
| 1641 | /* If SMP is already in progress ignore this request */ |
| 1642 | if (chan->data) { |
| 1643 | ret = 0; |
| 1644 | goto unlock; |
| 1645 | } |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1646 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1647 | smp = smp_chan_create(conn); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1648 | if (!smp) { |
| 1649 | ret = 1; |
| 1650 | goto unlock; |
| 1651 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1652 | |
| 1653 | authreq = seclevel_to_authreq(sec_level); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1654 | |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 1655 | if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags)) |
| 1656 | authreq |= SMP_AUTH_SC; |
| 1657 | |
Johan Hedberg | 79897d2 | 2014-06-01 09:45:24 +0300 | [diff] [blame] | 1658 | /* Require MITM if IO Capability allows or the security level |
| 1659 | * requires it. |
Johan Hedberg | 2e23364 | 2014-03-18 15:42:30 +0200 | [diff] [blame] | 1660 | */ |
Johan Hedberg | 79897d2 | 2014-06-01 09:45:24 +0300 | [diff] [blame] | 1661 | if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT || |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1662 | hcon->pending_sec_level > BT_SECURITY_MEDIUM) |
Johan Hedberg | 2e23364 | 2014-03-18 15:42:30 +0200 | [diff] [blame] | 1663 | authreq |= SMP_AUTH_MITM; |
| 1664 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1665 | if (hcon->role == HCI_ROLE_MASTER) { |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1666 | struct smp_cmd_pairing cp; |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 1667 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1668 | build_pairing_cmd(conn, &cp, NULL, authreq); |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1669 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 1670 | memcpy(&smp->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 1671 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1672 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1673 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1674 | } else { |
| 1675 | struct smp_cmd_security_req cp; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1676 | cp.auth_req = authreq; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1677 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1678 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1679 | } |
| 1680 | |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1681 | set_bit(SMP_FLAG_INITIATOR, &smp->flags); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1682 | ret = 0; |
Johan Hedberg | edca792 | 2014-03-24 15:54:11 +0200 | [diff] [blame] | 1683 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1684 | unlock: |
| 1685 | l2cap_chan_unlock(chan); |
| 1686 | return ret; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1687 | } |
| 1688 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1689 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1690 | { |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1691 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1692 | struct l2cap_chan *chan = conn->smp; |
| 1693 | struct smp_chan *smp = chan->data; |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1694 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1695 | BT_DBG("conn %p", conn); |
| 1696 | |
| 1697 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1698 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1699 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1700 | SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT); |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 1701 | |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1702 | skb_pull(skb, sizeof(*rp)); |
| 1703 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1704 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1705 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1706 | return 0; |
| 1707 | } |
| 1708 | |
| 1709 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1710 | { |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1711 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1712 | struct l2cap_chan *chan = conn->smp; |
| 1713 | struct smp_chan *smp = chan->data; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1714 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1715 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 1716 | struct smp_ltk *ltk; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1717 | u8 authenticated; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1718 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1719 | BT_DBG("conn %p", conn); |
| 1720 | |
| 1721 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1722 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1723 | |
Johan Hedberg | 9747a9f | 2014-02-26 23:33:43 +0200 | [diff] [blame] | 1724 | /* Mark the information as received */ |
| 1725 | smp->remote_key_dist &= ~SMP_DIST_ENC_KEY; |
| 1726 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1727 | if (smp->remote_key_dist & SMP_DIST_ID_KEY) |
| 1728 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); |
Johan Hedberg | 196332f | 2014-09-09 16:21:46 -0700 | [diff] [blame] | 1729 | else if (smp->remote_key_dist & SMP_DIST_SIGN) |
| 1730 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1731 | |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1732 | skb_pull(skb, sizeof(*rp)); |
| 1733 | |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 1734 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 1735 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK, |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 1736 | authenticated, smp->tk, smp->enc_key_size, |
| 1737 | rp->ediv, rp->rand); |
| 1738 | smp->ltk = ltk; |
Johan Hedberg | c6e81e9 | 2014-09-05 22:19:54 +0300 | [diff] [blame] | 1739 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 1740 | smp_distribute_keys(smp); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1741 | |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1745 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1746 | { |
| 1747 | struct smp_cmd_ident_info *info = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1748 | struct l2cap_chan *chan = conn->smp; |
| 1749 | struct smp_chan *smp = chan->data; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1750 | |
| 1751 | BT_DBG(""); |
| 1752 | |
| 1753 | if (skb->len < sizeof(*info)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1754 | return SMP_INVALID_PARAMS; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1755 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1756 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO); |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 1757 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1758 | skb_pull(skb, sizeof(*info)); |
| 1759 | |
| 1760 | memcpy(smp->irk, info->irk, 16); |
| 1761 | |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, |
| 1766 | struct sk_buff *skb) |
| 1767 | { |
| 1768 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1769 | struct l2cap_chan *chan = conn->smp; |
| 1770 | struct smp_chan *smp = chan->data; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1771 | struct hci_conn *hcon = conn->hcon; |
| 1772 | bdaddr_t rpa; |
| 1773 | |
| 1774 | BT_DBG(""); |
| 1775 | |
| 1776 | if (skb->len < sizeof(*info)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1777 | return SMP_INVALID_PARAMS; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1778 | |
Johan Hedberg | 9747a9f | 2014-02-26 23:33:43 +0200 | [diff] [blame] | 1779 | /* Mark the information as received */ |
| 1780 | smp->remote_key_dist &= ~SMP_DIST_ID_KEY; |
| 1781 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1782 | if (smp->remote_key_dist & SMP_DIST_SIGN) |
| 1783 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
| 1784 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1785 | skb_pull(skb, sizeof(*info)); |
| 1786 | |
Johan Hedberg | a9a58f8 | 2014-02-25 22:24:37 +0200 | [diff] [blame] | 1787 | /* Strictly speaking the Core Specification (4.1) allows sending |
| 1788 | * an empty address which would force us to rely on just the IRK |
| 1789 | * as "identity information". However, since such |
| 1790 | * implementations are not known of and in order to not over |
| 1791 | * complicate our implementation, simply pretend that we never |
| 1792 | * received an IRK for such a device. |
| 1793 | */ |
| 1794 | if (!bacmp(&info->bdaddr, BDADDR_ANY)) { |
| 1795 | BT_ERR("Ignoring IRK with no identity address"); |
Johan Hedberg | 31dd624 | 2014-06-27 14:23:02 +0300 | [diff] [blame] | 1796 | goto distribute; |
Johan Hedberg | a9a58f8 | 2014-02-25 22:24:37 +0200 | [diff] [blame] | 1797 | } |
| 1798 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1799 | bacpy(&smp->id_addr, &info->bdaddr); |
| 1800 | smp->id_addr_type = info->addr_type; |
| 1801 | |
| 1802 | if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type)) |
| 1803 | bacpy(&rpa, &hcon->dst); |
| 1804 | else |
| 1805 | bacpy(&rpa, BDADDR_ANY); |
| 1806 | |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 1807 | smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, |
| 1808 | smp->id_addr_type, smp->irk, &rpa); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1809 | |
Johan Hedberg | 31dd624 | 2014-06-27 14:23:02 +0300 | [diff] [blame] | 1810 | distribute: |
Johan Hedberg | c6e81e9 | 2014-09-05 22:19:54 +0300 | [diff] [blame] | 1811 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
| 1812 | smp_distribute_keys(smp); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1813 | |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1817 | static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1818 | { |
| 1819 | struct smp_cmd_sign_info *rp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1820 | struct l2cap_chan *chan = conn->smp; |
| 1821 | struct smp_chan *smp = chan->data; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1822 | struct smp_csrk *csrk; |
| 1823 | |
| 1824 | BT_DBG("conn %p", conn); |
| 1825 | |
| 1826 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1827 | return SMP_INVALID_PARAMS; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1828 | |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1829 | /* Mark the information as received */ |
| 1830 | smp->remote_key_dist &= ~SMP_DIST_SIGN; |
| 1831 | |
| 1832 | skb_pull(skb, sizeof(*rp)); |
| 1833 | |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1834 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); |
| 1835 | if (csrk) { |
| 1836 | csrk->master = 0x01; |
| 1837 | memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); |
| 1838 | } |
| 1839 | smp->csrk = csrk; |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 1840 | smp_distribute_keys(smp); |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1841 | |
| 1842 | return 0; |
| 1843 | } |
| 1844 | |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 1845 | static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1846 | { |
| 1847 | struct smp_cmd_public_key *key = (void *) skb->data; |
| 1848 | struct hci_conn *hcon = conn->hcon; |
| 1849 | struct l2cap_chan *chan = conn->smp; |
| 1850 | struct smp_chan *smp = chan->data; |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 1851 | struct smp_cmd_pairing_confirm cfm; |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 1852 | int err; |
| 1853 | |
| 1854 | BT_DBG("conn %p", conn); |
| 1855 | |
| 1856 | if (skb->len < sizeof(*key)) |
| 1857 | return SMP_INVALID_PARAMS; |
| 1858 | |
| 1859 | memcpy(smp->remote_pk, key, 64); |
| 1860 | |
| 1861 | /* Non-initiating device sends its public key after receiving |
| 1862 | * the key from the initiating device. |
| 1863 | */ |
| 1864 | if (!hcon->out) { |
| 1865 | err = sc_send_public_key(smp); |
| 1866 | if (err) |
| 1867 | return err; |
| 1868 | } |
| 1869 | |
| 1870 | BT_DBG("Remote Public Key X: %32phN", smp->remote_pk); |
| 1871 | BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]); |
| 1872 | |
| 1873 | if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey)) |
| 1874 | return SMP_UNSPECIFIED; |
| 1875 | |
| 1876 | BT_DBG("DHKey %32phN", smp->dhkey); |
| 1877 | |
| 1878 | set_bit(SMP_FLAG_REMOTE_PK, &smp->flags); |
| 1879 | |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 1880 | /* The Initiating device waits for the non-initiating device to |
| 1881 | * send the confirm value. |
| 1882 | */ |
| 1883 | if (conn->hcon->out) |
| 1884 | return 0; |
| 1885 | |
| 1886 | err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, |
| 1887 | 0, cfm.confirm_val); |
| 1888 | if (err) |
| 1889 | return SMP_UNSPECIFIED; |
| 1890 | |
| 1891 | smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm); |
| 1892 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 1893 | |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 1894 | return 0; |
| 1895 | } |
| 1896 | |
Johan Hedberg | 6433a9a | 2014-06-06 11:47:30 +0300 | [diff] [blame^] | 1897 | static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1898 | { |
| 1899 | struct smp_cmd_dhkey_check *check = (void *) skb->data; |
| 1900 | struct l2cap_chan *chan = conn->smp; |
| 1901 | struct hci_conn *hcon = conn->hcon; |
| 1902 | struct smp_chan *smp = chan->data; |
| 1903 | u8 a[7], b[7], *local_addr, *remote_addr; |
| 1904 | u8 io_cap[3], r[16], e[16]; |
| 1905 | int err; |
| 1906 | |
| 1907 | BT_DBG("conn %p", conn); |
| 1908 | |
| 1909 | if (skb->len < sizeof(*check)) |
| 1910 | return SMP_INVALID_PARAMS; |
| 1911 | |
| 1912 | memcpy(a, &hcon->init_addr, 6); |
| 1913 | memcpy(b, &hcon->resp_addr, 6); |
| 1914 | a[6] = hcon->init_addr_type; |
| 1915 | b[6] = hcon->resp_addr_type; |
| 1916 | |
| 1917 | if (hcon->out) { |
| 1918 | local_addr = a; |
| 1919 | remote_addr = b; |
| 1920 | memcpy(io_cap, &smp->prsp[1], 3); |
| 1921 | } else { |
| 1922 | local_addr = b; |
| 1923 | remote_addr = a; |
| 1924 | memcpy(io_cap, &smp->preq[1], 3); |
| 1925 | } |
| 1926 | |
| 1927 | memset(r, 0, sizeof(r)); |
| 1928 | |
| 1929 | err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r, |
| 1930 | io_cap, remote_addr, local_addr, e); |
| 1931 | if (err) |
| 1932 | return SMP_UNSPECIFIED; |
| 1933 | |
| 1934 | if (memcmp(check->e, e, 16)) |
| 1935 | return SMP_DHKEY_CHECK_FAILED; |
| 1936 | |
| 1937 | smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
| 1938 | SMP_LTK_P256, 0, smp->tk, smp->enc_key_size, |
| 1939 | 0, 0); |
| 1940 | |
| 1941 | if (hcon->out) { |
| 1942 | hci_le_start_enc(hcon, 0, 0, smp->tk); |
| 1943 | hcon->enc_key_size = smp->enc_key_size; |
| 1944 | } |
| 1945 | |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 1949 | static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb) |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1950 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1951 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 1952 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1953 | struct smp_chan *smp; |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1954 | __u8 code, reason; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1955 | int err = 0; |
| 1956 | |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 1957 | if (hcon->type != LE_LINK) { |
| 1958 | kfree_skb(skb); |
Johan Hedberg | 3432711 | 2013-10-16 11:37:01 +0300 | [diff] [blame] | 1959 | return 0; |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Johan Hedberg | 8ae9b98 | 2014-08-11 22:06:39 +0300 | [diff] [blame] | 1962 | if (skb->len < 1) |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1963 | return -EILSEQ; |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1964 | |
Marcel Holtmann | 06ae331 | 2013-10-18 03:43:00 -0700 | [diff] [blame] | 1965 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
Andre Guedes | 2e65c9d | 2011-06-30 19:20:56 -0300 | [diff] [blame] | 1966 | reason = SMP_PAIRING_NOTSUPP; |
| 1967 | goto done; |
| 1968 | } |
| 1969 | |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1970 | code = skb->data[0]; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1971 | skb_pull(skb, sizeof(code)); |
| 1972 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1973 | smp = chan->data; |
| 1974 | |
| 1975 | if (code > SMP_CMD_MAX) |
| 1976 | goto drop; |
| 1977 | |
Johan Hedberg | 24bd0bd | 2014-09-10 17:37:43 -0700 | [diff] [blame] | 1978 | if (smp && !test_and_clear_bit(code, &smp->allow_cmd)) |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1979 | goto drop; |
| 1980 | |
| 1981 | /* If we don't have a context the only allowed commands are |
| 1982 | * pairing request and security request. |
Johan Hedberg | 8cf9fa1 | 2013-01-29 10:44:23 -0600 | [diff] [blame] | 1983 | */ |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1984 | if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ) |
| 1985 | goto drop; |
Johan Hedberg | 8cf9fa1 | 2013-01-29 10:44:23 -0600 | [diff] [blame] | 1986 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1987 | switch (code) { |
| 1988 | case SMP_CMD_PAIRING_REQ: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1989 | reason = smp_cmd_pairing_req(conn, skb); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1990 | break; |
| 1991 | |
| 1992 | case SMP_CMD_PAIRING_FAIL: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1993 | smp_failure(conn, 0); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1994 | err = -EPERM; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1995 | break; |
| 1996 | |
| 1997 | case SMP_CMD_PAIRING_RSP: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1998 | reason = smp_cmd_pairing_rsp(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1999 | break; |
| 2000 | |
| 2001 | case SMP_CMD_SECURITY_REQ: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2002 | reason = smp_cmd_security_req(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2003 | break; |
| 2004 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2005 | case SMP_CMD_PAIRING_CONFIRM: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2006 | reason = smp_cmd_pairing_confirm(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2007 | break; |
| 2008 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2009 | case SMP_CMD_PAIRING_RANDOM: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2010 | reason = smp_cmd_pairing_random(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2011 | break; |
| 2012 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2013 | case SMP_CMD_ENCRYPT_INFO: |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2014 | reason = smp_cmd_encrypt_info(conn, skb); |
| 2015 | break; |
| 2016 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2017 | case SMP_CMD_MASTER_IDENT: |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2018 | reason = smp_cmd_master_ident(conn, skb); |
| 2019 | break; |
| 2020 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2021 | case SMP_CMD_IDENT_INFO: |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2022 | reason = smp_cmd_ident_info(conn, skb); |
| 2023 | break; |
| 2024 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2025 | case SMP_CMD_IDENT_ADDR_INFO: |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2026 | reason = smp_cmd_ident_addr_info(conn, skb); |
| 2027 | break; |
| 2028 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2029 | case SMP_CMD_SIGN_INFO: |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 2030 | reason = smp_cmd_sign_info(conn, skb); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2031 | break; |
| 2032 | |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 2033 | case SMP_CMD_PUBLIC_KEY: |
| 2034 | reason = smp_cmd_public_key(conn, skb); |
| 2035 | break; |
| 2036 | |
Johan Hedberg | 6433a9a | 2014-06-06 11:47:30 +0300 | [diff] [blame^] | 2037 | case SMP_CMD_DHKEY_CHECK: |
| 2038 | reason = smp_cmd_dhkey_check(conn, skb); |
| 2039 | break; |
| 2040 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2041 | default: |
| 2042 | BT_DBG("Unknown command code 0x%2.2x", code); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2043 | reason = SMP_CMD_NOTSUPP; |
Vinicius Costa Gomes | 3a0259b | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 2044 | goto done; |
| 2045 | } |
| 2046 | |
| 2047 | done: |
Johan Hedberg | 9b7b18e | 2014-08-18 20:33:31 +0300 | [diff] [blame] | 2048 | if (!err) { |
| 2049 | if (reason) |
| 2050 | smp_failure(conn, reason); |
Johan Hedberg | 8ae9b98 | 2014-08-11 22:06:39 +0300 | [diff] [blame] | 2051 | kfree_skb(skb); |
Johan Hedberg | 9b7b18e | 2014-08-18 20:33:31 +0300 | [diff] [blame] | 2052 | } |
| 2053 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2054 | return err; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2055 | |
| 2056 | drop: |
| 2057 | BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name, |
| 2058 | code, &hcon->dst); |
| 2059 | kfree_skb(skb); |
| 2060 | return 0; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2061 | } |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2062 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2063 | static void smp_teardown_cb(struct l2cap_chan *chan, int err) |
| 2064 | { |
| 2065 | struct l2cap_conn *conn = chan->conn; |
| 2066 | |
| 2067 | BT_DBG("chan %p", chan); |
| 2068 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 2069 | if (chan->data) |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2070 | smp_chan_destroy(conn); |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2071 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2072 | conn->smp = NULL; |
| 2073 | l2cap_chan_put(chan); |
| 2074 | } |
| 2075 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 2076 | static void smp_resume_cb(struct l2cap_chan *chan) |
| 2077 | { |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 2078 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 2079 | struct l2cap_conn *conn = chan->conn; |
| 2080 | struct hci_conn *hcon = conn->hcon; |
| 2081 | |
| 2082 | BT_DBG("chan %p", chan); |
| 2083 | |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 2084 | if (!smp) |
| 2085 | return; |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 2086 | |
Johan Hedberg | 84bc0db | 2014-09-05 22:19:49 +0300 | [diff] [blame] | 2087 | if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags)) |
| 2088 | return; |
| 2089 | |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 2090 | cancel_delayed_work(&smp->security_timer); |
| 2091 | |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 2092 | smp_distribute_keys(smp); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 2093 | } |
| 2094 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2095 | static void smp_ready_cb(struct l2cap_chan *chan) |
| 2096 | { |
| 2097 | struct l2cap_conn *conn = chan->conn; |
| 2098 | |
| 2099 | BT_DBG("chan %p", chan); |
| 2100 | |
| 2101 | conn->smp = chan; |
| 2102 | l2cap_chan_hold(chan); |
| 2103 | } |
| 2104 | |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 2105 | static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
| 2106 | { |
| 2107 | int err; |
| 2108 | |
| 2109 | BT_DBG("chan %p", chan); |
| 2110 | |
| 2111 | err = smp_sig_channel(chan, skb); |
| 2112 | if (err) { |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 2113 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 2114 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 2115 | if (smp) |
| 2116 | cancel_delayed_work_sync(&smp->security_timer); |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 2117 | |
Johan Hedberg | 1e91c29 | 2014-08-18 20:33:29 +0300 | [diff] [blame] | 2118 | hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE); |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | return err; |
| 2122 | } |
| 2123 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2124 | static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan, |
| 2125 | unsigned long hdr_len, |
| 2126 | unsigned long len, int nb) |
| 2127 | { |
| 2128 | struct sk_buff *skb; |
| 2129 | |
| 2130 | skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL); |
| 2131 | if (!skb) |
| 2132 | return ERR_PTR(-ENOMEM); |
| 2133 | |
| 2134 | skb->priority = HCI_PRIO_MAX; |
| 2135 | bt_cb(skb)->chan = chan; |
| 2136 | |
| 2137 | return skb; |
| 2138 | } |
| 2139 | |
| 2140 | static const struct l2cap_ops smp_chan_ops = { |
| 2141 | .name = "Security Manager", |
| 2142 | .ready = smp_ready_cb, |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2143 | .recv = smp_recv_cb, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2144 | .alloc_skb = smp_alloc_skb_cb, |
| 2145 | .teardown = smp_teardown_cb, |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 2146 | .resume = smp_resume_cb, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2147 | |
| 2148 | .new_connection = l2cap_chan_no_new_connection, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2149 | .state_change = l2cap_chan_no_state_change, |
| 2150 | .close = l2cap_chan_no_close, |
| 2151 | .defer = l2cap_chan_no_defer, |
| 2152 | .suspend = l2cap_chan_no_suspend, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2153 | .set_shutdown = l2cap_chan_no_set_shutdown, |
| 2154 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, |
| 2155 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, |
| 2156 | }; |
| 2157 | |
| 2158 | static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan) |
| 2159 | { |
| 2160 | struct l2cap_chan *chan; |
| 2161 | |
| 2162 | BT_DBG("pchan %p", pchan); |
| 2163 | |
| 2164 | chan = l2cap_chan_create(); |
| 2165 | if (!chan) |
| 2166 | return NULL; |
| 2167 | |
| 2168 | chan->chan_type = pchan->chan_type; |
| 2169 | chan->ops = &smp_chan_ops; |
| 2170 | chan->scid = pchan->scid; |
| 2171 | chan->dcid = chan->scid; |
| 2172 | chan->imtu = pchan->imtu; |
| 2173 | chan->omtu = pchan->omtu; |
| 2174 | chan->mode = pchan->mode; |
| 2175 | |
Johan Hedberg | abe8490 | 2014-11-12 22:22:21 +0200 | [diff] [blame] | 2176 | /* Other L2CAP channels may request SMP routines in order to |
| 2177 | * change the security level. This means that the SMP channel |
| 2178 | * lock must be considered in its own category to avoid lockdep |
| 2179 | * warnings. |
| 2180 | */ |
| 2181 | atomic_set(&chan->nesting, L2CAP_NESTING_SMP); |
| 2182 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2183 | BT_DBG("created chan %p", chan); |
| 2184 | |
| 2185 | return chan; |
| 2186 | } |
| 2187 | |
| 2188 | static const struct l2cap_ops smp_root_chan_ops = { |
| 2189 | .name = "Security Manager Root", |
| 2190 | .new_connection = smp_new_conn_cb, |
| 2191 | |
| 2192 | /* None of these are implemented for the root channel */ |
| 2193 | .close = l2cap_chan_no_close, |
| 2194 | .alloc_skb = l2cap_chan_no_alloc_skb, |
| 2195 | .recv = l2cap_chan_no_recv, |
| 2196 | .state_change = l2cap_chan_no_state_change, |
| 2197 | .teardown = l2cap_chan_no_teardown, |
| 2198 | .ready = l2cap_chan_no_ready, |
| 2199 | .defer = l2cap_chan_no_defer, |
| 2200 | .suspend = l2cap_chan_no_suspend, |
| 2201 | .resume = l2cap_chan_no_resume, |
| 2202 | .set_shutdown = l2cap_chan_no_set_shutdown, |
| 2203 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, |
| 2204 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, |
| 2205 | }; |
| 2206 | |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2207 | int smp_register(struct hci_dev *hdev) |
| 2208 | { |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2209 | struct l2cap_chan *chan; |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2210 | struct crypto_blkcipher *tfm_aes; |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2211 | |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2212 | BT_DBG("%s", hdev->name); |
| 2213 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2214 | tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0); |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2215 | if (IS_ERR(tfm_aes)) { |
| 2216 | int err = PTR_ERR(tfm_aes); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2217 | BT_ERR("Unable to create crypto context"); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2218 | return err; |
| 2219 | } |
| 2220 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2221 | chan = l2cap_chan_create(); |
| 2222 | if (!chan) { |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2223 | crypto_free_blkcipher(tfm_aes); |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2224 | return -ENOMEM; |
| 2225 | } |
| 2226 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2227 | chan->data = tfm_aes; |
| 2228 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2229 | l2cap_add_scid(chan, L2CAP_CID_SMP); |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2230 | |
| 2231 | l2cap_chan_set_defaults(chan); |
| 2232 | |
| 2233 | bacpy(&chan->src, &hdev->bdaddr); |
| 2234 | chan->src_type = BDADDR_LE_PUBLIC; |
| 2235 | chan->state = BT_LISTEN; |
| 2236 | chan->mode = L2CAP_MODE_BASIC; |
| 2237 | chan->imtu = L2CAP_DEFAULT_MTU; |
| 2238 | chan->ops = &smp_root_chan_ops; |
| 2239 | |
Johan Hedberg | abe8490 | 2014-11-12 22:22:21 +0200 | [diff] [blame] | 2240 | /* Set correct nesting level for a parent/listening channel */ |
| 2241 | atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); |
| 2242 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2243 | hdev->smp_data = chan; |
| 2244 | |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2245 | return 0; |
| 2246 | } |
| 2247 | |
| 2248 | void smp_unregister(struct hci_dev *hdev) |
| 2249 | { |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2250 | struct l2cap_chan *chan = hdev->smp_data; |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2251 | struct crypto_blkcipher *tfm_aes; |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2252 | |
| 2253 | if (!chan) |
| 2254 | return; |
| 2255 | |
| 2256 | BT_DBG("%s chan %p", hdev->name, chan); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2257 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2258 | tfm_aes = chan->data; |
| 2259 | if (tfm_aes) { |
| 2260 | chan->data = NULL; |
| 2261 | crypto_free_blkcipher(tfm_aes); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2262 | } |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2263 | |
| 2264 | hdev->smp_data = NULL; |
| 2265 | l2cap_chan_put(chan); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2266 | } |