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