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