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