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