blob: 1bd281060de25578b4e7db9b7db6af9cc3715606 [file] [log] [blame]
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001/*
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 Holtmann300acfde2014-12-31 14:43:16 -080023#include <linux/debugfs.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030024#include <linux/crypto.h>
25#include <linux/scatterlist.h>
26#include <crypto/b128ops.h>
27
Anderson Brigliaeb492e02011-06-09 18:50:40 -030028#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080031#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070032
Johan Hedberg3b191462014-06-06 10:50:15 +030033#include "ecc.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070034#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030035
Johan Hedbergc7a3d572014-12-01 22:03:16 +020036/* 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 Hedbergb28b4942014-09-05 22:19:55 +030048#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030049
Johan Hedberg3b191462014-06-06 10:50:15 +030050/* Keys which are not distributed with Secure Connections */
51#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
52
Marcel Holtmann17b02e62012-03-01 14:32:37 -080053#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030054
Marcel Holtmannd7a5a112015-03-13 02:11:00 -070055#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
Johan Hedberg0edb14d2014-05-26 13:29:28 +030056 0x1f : 0x07)
57#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020058
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030059/* Maximum message length that can be passed to aes_cmac */
60#define CMAC_MSG_MAX 80
61
Johan Hedberg533e35d2014-06-16 19:25:18 +030062enum {
63 SMP_FLAG_TK_VALID,
64 SMP_FLAG_CFM_PENDING,
65 SMP_FLAG_MITM_AUTH,
66 SMP_FLAG_COMPLETE,
67 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030068 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030069 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030070 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030071 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030072 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg1a8bab42015-03-16 11:45:44 +020073 SMP_FLAG_REMOTE_OOB,
74 SMP_FLAG_LOCAL_OOB,
Johan Hedberg533e35d2014-06-16 19:25:18 +030075};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030076
Marcel Holtmann88a479d2015-03-16 01:10:19 -070077struct smp_dev {
Marcel Holtmann60a27d62015-03-16 01:10:22 -070078 /* 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 Holtmann88a479d2015-03-16 01:10:19 -070084 struct crypto_blkcipher *tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -070085 struct crypto_hash *tfm_cmac;
Marcel Holtmann88a479d2015-03-16 01:10:19 -070086};
87
Johan Hedberg4bc58f52014-05-20 09:45:47 +030088struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030089 struct l2cap_conn *conn;
90 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030091 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030092
Johan Hedberg4bc58f52014-05-20 09:45:47 +030093 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 Hedberg882fafa2015-03-16 11:45:43 +020099 u8 rr[16]; /* Remote OOB ra/rb value */
100 u8 lr[16]; /* Local OOB ra/rb value */
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300101 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 Hedberg6a770832014-06-06 11:54:04 +0300111 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300112 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300113 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300114 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300115
Johan Hedberg3b191462014-06-06 10:50:15 +0300116 /* Secure Connections variables */
117 u8 local_pk[64];
118 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300119 u8 remote_pk[64];
120 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300121 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300122
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300123 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +0300124 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300125};
126
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300127/* 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 */
131static 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
143static 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 Hedberg8a2936f2014-06-16 19:25:19 +0300150static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300151{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300152 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300153
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300154 for (i = 0; i < len; i++)
155 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300156}
157
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200158/* The following functions map to the LE SC SMP crypto functions
159 * AES-CMAC, f4, f5, f6, g2 and h6.
160 */
161
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300162static 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 Hedbergc7a3d572014-12-01 22:03:16 +0200187 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
188 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300189
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 Hedbergc7a3d572014-12-01 22:03:16 +0200212 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300213
214 return 0;
215}
216
217static 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 Hedbergc7a3d572014-12-01 22:03:16 +0200223 SMP_DBG("u %32phN", u);
224 SMP_DBG("v %32phN", v);
225 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300226
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 Hedbergc7a3d572014-12-01 22:03:16 +0200235 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300236
237 return err;
238}
239
Johan Hedberg4da50de2014-12-29 12:04:10 +0200240static 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 Hedberg760b0182014-06-06 11:44:05 +0300243{
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 Hedbergc7a3d572014-12-01 22:03:16 +0200257 SMP_DBG("w %32phN", w);
258 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
259 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300260
261 err = aes_cmac(tfm_cmac, salt, w, 32, t);
262 if (err)
263 return err;
264
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200265 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300266
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 Hedbergc7a3d572014-12-01 22:03:16 +0200280 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300281
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 Hedbergc7a3d572014-12-01 22:03:16 +0200288 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300289
290 return 0;
291}
292
293static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200294 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300295 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 Hedbergc7a3d572014-12-01 22:03:16 +0200301 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 Hedberg760b0182014-06-06 11:44:05 +0300304
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 Holtmann203de212014-12-31 20:01:22 -0800316 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300317
318 return err;
319}
320
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300321static 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 Hedbergc7a3d572014-12-01 22:03:16 +0200327 SMP_DBG("u %32phN", u);
328 SMP_DBG("v %32phN", v);
329 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300330
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 Hedbergc7a3d572014-12-01 22:03:16 +0200342 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300343
344 return 0;
345}
346
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200347static 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 Brigliad22ef0b2011-06-09 18:50:44 -0300367static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
368{
369 struct blkcipher_desc desc;
370 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200371 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200372 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300373
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200374 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300375 BT_ERR("tfm %p", tfm);
376 return -EINVAL;
377 }
378
379 desc.tfm = tfm;
380 desc.flags = 0;
381
Johan Hedberg943a7322014-03-18 12:58:24 +0200382 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300383 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200384
385 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300386 if (err) {
387 BT_ERR("cipher setkey failed: %d", err);
388 return err;
389 }
390
Johan Hedberg943a7322014-03-18 12:58:24 +0200391 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300392 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200393
394 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300395
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300396 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
397 if (err)
398 BT_ERR("Encrypt data error %d", err);
399
Johan Hedberg943a7322014-03-18 12:58:24 +0200400 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300401 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200402
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300403 return err;
404}
405
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200406static 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
447static 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 Hedberg6a770832014-06-06 11:54:04 +0300449{
450 int err;
451
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200452 /* Just least significant octets from r1 and r2 are considered */
453 memcpy(_r, r2, 8);
454 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300455
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200456 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300457 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200458 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300459
460 return err;
461}
462
Johan Hedbergcd082792014-12-02 13:37:41 +0200463static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
464 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200465{
Johan Hedberg943a7322014-03-18 12:58:24 +0200466 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200467 int err;
468
469 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200470 memcpy(_res, r, 3);
471 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200472
Johan Hedberg943a7322014-03-18 12:58:24 +0200473 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200474 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 Hedberg943a7322014-03-18 12:58:24 +0200485 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200486
487 return 0;
488}
489
Johan Hedbergcd082792014-12-02 13:37:41 +0200490bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
491 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200492{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300493 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700494 struct smp_dev *smp;
Johan Hedberg60478052014-02-18 10:19:31 +0200495 u8 hash[3];
496 int err;
497
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300498 if (!chan || !chan->data)
499 return false;
500
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700501 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300502
Johan Hedberg60478052014-02-18 10:19:31 +0200503 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
504
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700505 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200506 if (err)
507 return false;
508
509 return !memcmp(bdaddr->b, hash, 3);
510}
511
Johan Hedbergcd082792014-12-02 13:37:41 +0200512int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200513{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300514 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700515 struct smp_dev *smp;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200516 int err;
517
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300518 if (!chan || !chan->data)
519 return -EOPNOTSUPP;
520
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700521 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300522
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200523 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 Holtmann88a479d2015-03-16 01:10:19 -0700528 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200529 if (err < 0)
530 return err;
531
532 BT_DBG("RPA %pMR", rpa);
533
534 return 0;
535}
536
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700537int 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 Brigliaeb492e02011-06-09 18:50:40 -0300584static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
585{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300586 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300587 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300588 struct kvec iv[2];
589 struct msghdr msg;
590
591 if (!chan)
592 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300593
594 BT_DBG("code 0x%2.2x", code);
595
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300596 iv[0].iov_base = &code;
597 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300598
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300599 iv[1].iov_base = data;
600 iv[1].iov_len = len;
601
602 memset(&msg, 0, sizeof(msg));
603
Al Viro17836392014-11-24 17:07:38 -0500604 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300605
606 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300607
Johan Hedbergb68fda62014-08-11 22:06:40 +0300608 if (!chan->data)
609 return;
610
611 smp = chan->data;
612
613 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300614 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300615}
616
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300617static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800618{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300619 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 Gix2b64d152011-12-21 16:12:12 -0800625 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300626 }
Brian Gix2b64d152011-12-21 16:12:12 -0800627}
628
629static __u8 seclevel_to_authreq(__u8 sec_level)
630{
631 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300632 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800633 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 Gomesb8e66ea2011-06-09 18:50:52 -0300642static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700643 struct smp_cmd_pairing *req,
644 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300645{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300646 struct l2cap_chan *chan = conn->smp;
647 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200648 struct hci_conn *hcon = conn->hcon;
649 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100650 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300651
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700652 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700653 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
654 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300655 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800656 } else {
657 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300658 }
659
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700660 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200661 remote_dist |= SMP_DIST_ID_KEY;
662
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700663 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200664 local_dist |= SMP_DIST_ID_KEY;
665
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700666 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100667 (authreq & SMP_AUTH_SC)) {
668 struct oob_data *oob_data;
669 u8 bdaddr_type;
670
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700671 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300672 local_dist |= SMP_DIST_LINK_KEY;
673 remote_dist |= SMP_DIST_LINK_KEY;
674 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100675
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 Holtmann4775a4e2015-01-31 00:15:52 -0800683 if (oob_data && oob_data->present) {
Johan Hedberg1a8bab42015-03-16 11:45:44 +0200684 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100685 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100686 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100687 memcpy(smp->pcnf, oob_data->hash256, 16);
688 }
689
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300690 } else {
691 authreq &= ~SMP_AUTH_SC;
692 }
693
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300694 if (rsp == NULL) {
695 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100696 req->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300697 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200698 req->init_key_dist = local_dist;
699 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300700 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200701
702 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300703 return;
704 }
705
706 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100707 rsp->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300708 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200709 rsp->init_key_dist = req->init_key_dist & remote_dist;
710 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300711 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200712
713 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300714}
715
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300716static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
717{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300718 struct l2cap_chan *chan = conn->smp;
719 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300720
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300721 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700722 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300723 return SMP_ENC_KEY_SIZE;
724
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300725 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300726
727 return 0;
728}
729
Johan Hedberg6f48e262014-08-11 22:06:44 +0300730static void smp_chan_destroy(struct l2cap_conn *conn)
731{
732 struct l2cap_chan *chan = conn->smp;
733 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200734 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300735 bool complete;
736
737 BUG_ON(!smp);
738
739 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300740
Johan Hedberg6f48e262014-08-11 22:06:44 +0300741 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200742 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300743
Marcel Holtmann276812e2015-03-16 01:10:18 -0700744 kzfree(smp->csrk);
745 kzfree(smp->slave_csrk);
746 kzfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300747
748 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300749 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300750
Johan Hedberg923e2412014-12-03 12:43:39 +0200751 /* 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 Holtmannd7a5a112015-03-13 02:11:00 -0700755 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200756 list_del_rcu(&smp->ltk->list);
757 kfree_rcu(smp->ltk, rcu);
758 smp->ltk = NULL;
759 }
760
Johan Hedberg6f48e262014-08-11 22:06:44 +0300761 /* If pairing failed clean up any keys we might have */
762 if (!complete) {
763 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200764 list_del_rcu(&smp->ltk->list);
765 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300766 }
767
768 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200769 list_del_rcu(&smp->slave_ltk->list);
770 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300771 }
772
773 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200774 list_del_rcu(&smp->remote_irk->list);
775 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300776 }
777 }
778
779 chan->data = NULL;
Marcel Holtmann276812e2015-03-16 01:10:18 -0700780 kzfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200781 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300782}
783
Johan Hedberg84794e12013-11-06 11:24:57 +0200784static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800785{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200786 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300787 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200788
Johan Hedberg84794e12013-11-06 11:24:57 +0200789 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800790 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700791 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800792
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700793 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700794 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300795
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300796 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300797 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800798}
799
Brian Gix2b64d152011-12-21 16:12:12 -0800800#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 Hedberg5e3d3d92014-05-31 18:51:02 +0300805#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800806#define OVERLAP 0xFF
807
808static 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 Hedberg5e3d3d92014-05-31 18:51:02 +0300816static 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 Hedberg581370c2014-06-17 13:07:38 +0300824static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
825{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300826 /* If either side has unknown io_caps, use JUST_CFM (which gets
827 * converted later to JUST_WORKS if we're initiators.
828 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300829 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
830 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300831 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300832
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300833 if (test_bit(SMP_FLAG_SC, &smp->flags))
834 return sc_method[remote_io][local_io];
835
Johan Hedberg581370c2014-06-17 13:07:38 +0300836 return gen_method[remote_io][local_io];
837}
838
Brian Gix2b64d152011-12-21 16:12:12 -0800839static 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 Hedberg5d88cc72014-08-08 09:37:18 +0300843 struct l2cap_chan *chan = conn->smp;
844 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800845 u32 passkey = 0;
846 int ret = 0;
847
848 /* Initialize key for JUST WORKS */
849 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300850 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800851
852 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
853
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300854 /* 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 Hedberg581370c2014-06-17 13:07:38 +0300860 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300861 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800862 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300863 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800864
Johan Hedberga82505c2014-03-24 14:39:07 +0200865 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300866 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
867 &smp->flags))
868 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200869
Johan Hedberg02f3e252014-07-16 15:09:13 +0300870 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300871 if (smp->method == JUST_CFM &&
872 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
873 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300874
Brian Gix2b64d152011-12-21 16:12:12 -0800875 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300876 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300877 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800878 return 0;
879 }
880
881 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300882 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300883 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300884 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
885 hcon->pending_sec_level = BT_SECURITY_HIGH;
886 }
Brian Gix2b64d152011-12-21 16:12:12 -0800887
888 /* If both devices have Keyoard-Display I/O, the master
889 * Confirms and the slave Enters the passkey.
890 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300891 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300892 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300893 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800894 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300895 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800896 }
897
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200898 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300899 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200900 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800901 get_random_bytes(&passkey, sizeof(passkey));
902 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200903 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800904 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300905 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800906 }
907
Johan Hedberg783e0572014-05-31 18:48:26 +0300908 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700909 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200910 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300911 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200912 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
913 hcon->type, hcon->dst_type,
914 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800915 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200916 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200917 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200918 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800919
Brian Gix2b64d152011-12-21 16:12:12 -0800920 return ret;
921}
922
Johan Hedberg1cc61142014-05-20 09:45:52 +0300923static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300924{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300925 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300926 struct smp_cmd_pairing_confirm cp;
927 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300928
929 BT_DBG("conn %p", conn);
930
Johan Hedberge491eaf2014-10-25 21:15:37 +0200931 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200932 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200933 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
934 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300935 if (ret)
936 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300937
Johan Hedberg4a74d652014-05-20 09:45:50 +0300938 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800939
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300940 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
941
Johan Hedbergb28b4942014-09-05 22:19:55 +0300942 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 Hedberg1cc61142014-05-20 09:45:52 +0300947 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300948}
949
Johan Hedberg861580a2014-05-20 09:45:51 +0300950static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300951{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300952 struct l2cap_conn *conn = smp->conn;
953 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300954 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300955 int ret;
956
Johan Hedbergec70f362014-06-27 14:23:04 +0300957 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300958 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300959
960 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
961
Johan Hedberge491eaf2014-10-25 21:15:37 +0200962 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200963 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200964 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300965 if (ret)
966 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300967
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300968 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
969 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300970 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300971 }
972
973 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800974 u8 stk[16];
975 __le64 rand = 0;
976 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300977
Johan Hedberge491eaf2014-10-25 21:15:37 +0200978 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300979
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300980 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300981 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300982
Johan Hedberg861580a2014-05-20 09:45:51 +0300983 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
984 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300985
986 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300987 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300988 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300989 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300990 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800991 __le64 rand = 0;
992 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300993
Johan Hedberg943a7322014-03-18 12:58:24 +0200994 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
995 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300996
Johan Hedberge491eaf2014-10-25 21:15:37 +0200997 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300998
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300999 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -07001000 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001001
Johan Hedbergfff34902014-06-10 15:19:50 +03001002 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1003 auth = 1;
1004 else
1005 auth = 0;
1006
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001007 /* 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 Holtmannce39fb42013-10-13 02:23:39 -07001011 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001012 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001013 }
1014
Johan Hedberg861580a2014-05-20 09:45:51 +03001015 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001016}
1017
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001018static 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 Hedbergb5ae3442014-08-14 12:34:26 +03001032 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001033 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001034 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 Hedberg44f1a7a2014-08-11 22:06:36 +03001039
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 Hedbergadae20c2014-11-13 14:37:48 +02001051 list_del_rcu(&smp->remote_irk->list);
1052 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001053 smp->remote_irk = NULL;
1054 }
1055 }
1056
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001057 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 Hedberg44f1a7a2014-08-11 22:06:36 +03001071
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 Hedberg6a770832014-06-06 11:54:04 +03001095
1096 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001097 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 Holtmannd7a5a112015-03-13 02:11:00 -07001115 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001116 key->type == HCI_LK_DEBUG_COMBINATION) {
1117 list_del_rcu(&key->list);
1118 kfree_rcu(key, rcu);
1119 }
1120 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001121 }
1122}
1123
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001124static 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 Hedberg6a770832014-06-06 11:54:04 +03001147static 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 Holtmann276812e2015-03-16 01:10:18 -07001160 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001161 smp->link_key = NULL;
1162 return;
1163 }
1164
1165 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001166 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001167 smp->link_key = NULL;
1168 return;
1169 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001170}
1171
Johan Hedbergb28b4942014-09-05 22:19:55 +03001172static 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 Hedbergb5ae3442014-08-14 12:34:26 +03001186static 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 Hedbergd6268e82014-09-05 22:19:51 +03001215static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001216{
1217 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001218 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001219 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 Hedberg44f1a7a2014-08-11 22:06:36 +03001225 rsp = (void *) &smp->prsp[1];
1226
1227 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001228 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1229 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001230 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001231 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001232
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 Hedberg6a770832014-06-06 11:54:04 +03001243 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001244 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001245 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001246 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1247 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001248
1249 /* Clear the keys which are generated but not distributed */
1250 *keydist &= ~SMP_SC_NO_DIST;
1251 }
1252
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001253 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 Hedberg4cd39282015-02-27 10:11:13 +02001315 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1316 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1317 else
1318 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001319 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 Hedbergb28b4942014-09-05 22:19:55 +03001329 if (smp->remote_key_dist & KEY_DIST_MASK) {
1330 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001331 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001332 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001333
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001334 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1335 smp_notify_keys(conn);
1336
1337 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001338}
1339
Johan Hedbergb68fda62014-08-11 22:06:40 +03001340static 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 Hedberg1e91c292014-08-18 20:33:29 +03001348 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001349}
1350
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001351static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1352{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001353 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001354 struct smp_chan *smp;
1355
Marcel Holtmannf1560462013-10-13 05:43:25 -07001356 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001357 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001358 return NULL;
1359
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001360 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 Holtmann276812e2015-03-16 01:10:18 -07001363 kzfree(smp);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001364 return NULL;
1365 }
1366
Johan Hedberg407cecf2014-05-02 14:19:47 +03001367 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 Holtmann276812e2015-03-16 01:10:18 -07001371 kzfree(smp);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001372 return NULL;
1373 }
1374
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001375 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001376 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001377
Johan Hedbergb28b4942014-09-05 22:19:55 +03001378 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1379
Johan Hedbergb68fda62014-08-11 22:06:40 +03001380 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1381
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001382 hci_conn_hold(conn->hcon);
1383
1384 return smp;
1385}
1386
Johan Hedberg760b0182014-06-06 11:44:05 +03001387static 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 Hedberg38606f12014-06-25 11:10:28 +03001408static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001409{
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 Hedberg760b0182014-06-06 11:44:05 +03001415 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 Hedbergdddd3052014-06-01 15:38:09 +03001430 memset(r, 0, sizeof(r));
1431
1432 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001433 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001434
Johan Hedberga29b0732014-10-28 15:17:05 +01001435 if (smp->method == REQ_OOB)
1436 memcpy(r, smp->rr, 16);
1437
Johan Hedberg760b0182014-06-06 11:44:05 +03001438 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 Hedbergdddd3052014-06-01 15:38:09 +03001442}
1443
Johan Hedberg38606f12014-06-25 11:10:28 +03001444static 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
1465static 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 Hedbergd3e54a82014-06-04 11:07:40 +03001502 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001503 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001504 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001505 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001506 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 Hedbergdddd3052014-06-01 15:38:09 +03001552static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1553{
Johan Hedberg38606f12014-06-25 11:10:28 +03001554 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 Hedbergdddd3052014-06-01 15:38:09 +03001560 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 Hedberg38606f12014-06-25 11:10:28 +03001567 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 Hedbergdddd3052014-06-01 15:38:09 +03001580 }
1581
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001582 /* 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 Hedberg760b0182014-06-06 11:44:05 +03001590
1591 return 0;
1592}
1593
Brian Gix2b64d152011-12-21 16:12:12 -08001594int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1595{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001596 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001597 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001598 struct smp_chan *smp;
1599 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001600 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001601
1602 BT_DBG("");
1603
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001604 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001605 return -ENOTCONN;
1606
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001607 chan = conn->smp;
1608 if (!chan)
1609 return -ENOTCONN;
1610
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001611 l2cap_chan_lock(chan);
1612 if (!chan->data) {
1613 err = -ENOTCONN;
1614 goto unlock;
1615 }
1616
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001617 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001618
Johan Hedberg760b0182014-06-06 11:44:05 +03001619 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1620 err = sc_user_reply(smp, mgmt_op, passkey);
1621 goto unlock;
1622 }
1623
Brian Gix2b64d152011-12-21 16:12:12 -08001624 switch (mgmt_op) {
1625 case MGMT_OP_USER_PASSKEY_REPLY:
1626 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001627 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001628 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001629 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001630 /* Fall Through */
1631 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001632 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001633 break;
1634 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1635 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001636 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001637 err = 0;
1638 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001639 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001640 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001641 err = -EOPNOTSUPP;
1642 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001643 }
1644
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001645 err = 0;
1646
Brian Gix2b64d152011-12-21 16:12:12 -08001647 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001648 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 Gix2b64d152011-12-21 16:12:12 -08001653
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001654unlock:
1655 l2cap_chan_unlock(chan);
1656 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001657}
1658
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001659static 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 Holtmannd7a5a112015-03-13 02:11:00 -07001667 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001668 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1669 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1670 }
1671
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001672 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001673 remote_dist |= SMP_DIST_ID_KEY;
1674
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001675 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001676 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 Gomesda85e5e2011-06-09 18:50:53 -03001699static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001700{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001701 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001702 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001703 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001704 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001705 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001706 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001707
1708 BT_DBG("conn %p", conn);
1709
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001710 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001711 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001712
Johan Hedberg40bef302014-07-16 11:42:27 +03001713 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001714 return SMP_CMD_NOTSUPP;
1715
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001716 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001717 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001718 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001719 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001720
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001721 if (!smp)
1722 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001723
Johan Hedbergc05b9332014-09-10 17:37:42 -07001724 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001725 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001726
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001727 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001728 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001729 return SMP_PAIRING_NOTSUPP;
1730
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001731 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001732 return SMP_AUTH_REQUIREMENTS;
1733
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001734 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1735 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001736 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001737
Johan Hedbergcb06d362015-03-16 21:12:34 +02001738 /* If the remote side's OOB flag is set it means it has
1739 * successfully received our local OOB data - therefore set the
1740 * flag to indicate that local OOB is in use.
1741 */
Johan Hedberg58428562015-03-16 11:45:45 +02001742 if (req->oob_flag == SMP_OOB_PRESENT)
1743 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1744
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001745 /* SMP over BR/EDR requires special treatment */
1746 if (conn->hcon->type == ACL_LINK) {
1747 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001748 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001749 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001750 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1751
1752 set_bit(SMP_FLAG_SC, &smp->flags);
1753
1754 build_bredr_pairing_cmd(smp, req, &rsp);
1755
1756 key_size = min(req->max_key_size, rsp.max_key_size);
1757 if (check_enc_key_size(conn, key_size))
1758 return SMP_ENC_KEY_SIZE;
1759
1760 /* Clear bits which are generated but not distributed */
1761 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1762
1763 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1764 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1765 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1766
1767 smp_distribute_keys(smp);
1768 return 0;
1769 }
1770
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001771 build_pairing_cmd(conn, req, &rsp, auth);
1772
1773 if (rsp.auth_req & SMP_AUTH_SC)
1774 set_bit(SMP_FLAG_SC, &smp->flags);
1775
Johan Hedberg5be5e272014-09-10 17:58:54 -07001776 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001777 sec_level = BT_SECURITY_MEDIUM;
1778 else
1779 sec_level = authreq_to_seclevel(auth);
1780
Johan Hedbergc7262e72014-06-17 13:07:37 +03001781 if (sec_level > conn->hcon->pending_sec_level)
1782 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001783
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001784 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001785 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1786 u8 method;
1787
1788 method = get_auth_method(smp, conn->hcon->io_capability,
1789 req->io_capability);
1790 if (method == JUST_WORKS || method == JUST_CFM)
1791 return SMP_AUTH_REQUIREMENTS;
1792 }
1793
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001794 key_size = min(req->max_key_size, rsp.max_key_size);
1795 if (check_enc_key_size(conn, key_size))
1796 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001797
Johan Hedberge84a6b12013-12-02 10:49:03 +02001798 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001799
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001800 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1801 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001802
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001803 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001804
1805 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1806
1807 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1808 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1809 /* Clear bits which are generated but not distributed */
1810 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1811 /* Wait for Public Key from Initiating Device */
1812 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001813 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001814
Marcel Holtmann983f9812015-03-11 17:47:40 -07001815 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1816
Brian Gix2b64d152011-12-21 16:12:12 -08001817 /* Request setup of TK */
1818 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1819 if (ret)
1820 return SMP_UNSPECIFIED;
1821
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001822 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001823}
1824
Johan Hedberg3b191462014-06-06 10:50:15 +03001825static u8 sc_send_public_key(struct smp_chan *smp)
1826{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001827 struct hci_dev *hdev = smp->conn->hcon->hdev;
1828
Johan Hedberg3b191462014-06-06 10:50:15 +03001829 BT_DBG("");
1830
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001831 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001832 struct l2cap_chan *chan = hdev->smp_data;
1833 struct smp_dev *smp_dev;
1834
1835 if (!chan || !chan->data)
1836 return SMP_UNSPECIFIED;
1837
1838 smp_dev = chan->data;
1839
1840 memcpy(smp->local_pk, smp_dev->local_pk, 64);
1841 memcpy(smp->local_sk, smp_dev->local_sk, 32);
Johan Hedberg882fafa2015-03-16 11:45:43 +02001842 memcpy(smp->lr, smp_dev->local_rr, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001843
1844 if (smp_dev->debug_key)
1845 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1846
1847 goto done;
1848 }
1849
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001850 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001851 BT_DBG("Using debug keys");
1852 memcpy(smp->local_pk, debug_pk, 64);
1853 memcpy(smp->local_sk, debug_sk, 32);
1854 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1855 } else {
1856 while (true) {
1857 /* Generate local key pair for Secure Connections */
1858 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1859 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001860
Johan Hedberg70157ef2014-06-24 15:22:59 +03001861 /* This is unlikely, but we need to check that
1862 * we didn't accidentially generate a debug key.
1863 */
1864 if (memcmp(smp->local_sk, debug_sk, 32))
1865 break;
1866 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001867 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001868
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001869done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001870 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001871 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001872 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001873
1874 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1875
1876 return 0;
1877}
1878
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001879static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001880{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001881 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001882 struct l2cap_chan *chan = conn->smp;
1883 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001884 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001885 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001886 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001887
1888 BT_DBG("conn %p", conn);
1889
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001890 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001891 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001892
Johan Hedberg40bef302014-07-16 11:42:27 +03001893 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001894 return SMP_CMD_NOTSUPP;
1895
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001896 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001897
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001898 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001899
1900 key_size = min(req->max_key_size, rsp->max_key_size);
1901 if (check_enc_key_size(conn, key_size))
1902 return SMP_ENC_KEY_SIZE;
1903
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001904 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001905
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001906 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001907 return SMP_AUTH_REQUIREMENTS;
1908
Johan Hedbergcb06d362015-03-16 21:12:34 +02001909 /* If the remote side's OOB flag is set it means it has
1910 * successfully received our local OOB data - therefore set the
1911 * flag to indicate that local OOB is in use.
1912 */
Johan Hedberg58428562015-03-16 11:45:45 +02001913 if (rsp->oob_flag == SMP_OOB_PRESENT)
1914 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1915
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001916 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1917 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1918
1919 /* Update remote key distribution in case the remote cleared
1920 * some bits that we had enabled in our request.
1921 */
1922 smp->remote_key_dist &= rsp->resp_key_dist;
1923
1924 /* For BR/EDR this means we're done and can start phase 3 */
1925 if (conn->hcon->type == ACL_LINK) {
1926 /* Clear bits which are generated but not distributed */
1927 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1928 smp_distribute_keys(smp);
1929 return 0;
1930 }
1931
Johan Hedberg65668772014-05-16 11:03:34 +03001932 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1933 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001934 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1935 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001936
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001937 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001938 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1939 u8 method;
1940
1941 method = get_auth_method(smp, req->io_capability,
1942 rsp->io_capability);
1943 if (method == JUST_WORKS || method == JUST_CFM)
1944 return SMP_AUTH_REQUIREMENTS;
1945 }
1946
Johan Hedberge84a6b12013-12-02 10:49:03 +02001947 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001948
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001949 /* Update remote key distribution in case the remote cleared
1950 * some bits that we had enabled in our request.
1951 */
1952 smp->remote_key_dist &= rsp->resp_key_dist;
1953
Johan Hedberg3b191462014-06-06 10:50:15 +03001954 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1955 /* Clear bits which are generated but not distributed */
1956 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1957 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1958 return sc_send_public_key(smp);
1959 }
1960
Johan Hedbergc05b9332014-09-10 17:37:42 -07001961 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001962
Johan Hedberg476585e2012-06-06 18:54:15 +08001963 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001964 if (ret)
1965 return SMP_UNSPECIFIED;
1966
Johan Hedberg4a74d652014-05-20 09:45:50 +03001967 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001968
1969 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001970 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001971 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001972
1973 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001974}
1975
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001976static u8 sc_check_confirm(struct smp_chan *smp)
1977{
1978 struct l2cap_conn *conn = smp->conn;
1979
1980 BT_DBG("");
1981
1982 /* Public Key exchange must happen before any other steps */
1983 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1984 return SMP_UNSPECIFIED;
1985
Johan Hedberg38606f12014-06-25 11:10:28 +03001986 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1987 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1988
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001989 if (conn->hcon->out) {
1990 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1991 smp->prnd);
1992 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1993 }
1994
1995 return 0;
1996}
1997
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001998static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001999{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002000 struct l2cap_chan *chan = conn->smp;
2001 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002002
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002003 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2004
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002005 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002006 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002007
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002008 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2009 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002010
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002011 if (test_bit(SMP_FLAG_SC, &smp->flags))
2012 return sc_check_confirm(smp);
2013
Johan Hedbergb28b4942014-09-05 22:19:55 +03002014 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002015 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2016 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002017 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2018 return 0;
2019 }
2020
2021 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002022 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002023
2024 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002025
2026 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002027}
2028
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002029static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002030{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002031 struct l2cap_chan *chan = conn->smp;
2032 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002033 struct hci_conn *hcon = conn->hcon;
2034 u8 *pkax, *pkbx, *na, *nb;
2035 u32 passkey;
2036 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002037
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002038 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002039
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002040 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002041 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002042
Johan Hedberg943a7322014-03-18 12:58:24 +02002043 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002044 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002045
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002046 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2047 return smp_random(smp);
2048
Johan Hedberg580039e2014-12-03 16:26:37 +02002049 if (hcon->out) {
2050 pkax = smp->local_pk;
2051 pkbx = smp->remote_pk;
2052 na = smp->prnd;
2053 nb = smp->rrnd;
2054 } else {
2055 pkax = smp->remote_pk;
2056 pkbx = smp->local_pk;
2057 na = smp->rrnd;
2058 nb = smp->prnd;
2059 }
2060
Johan Hedberga29b0732014-10-28 15:17:05 +01002061 if (smp->method == REQ_OOB) {
2062 if (!hcon->out)
2063 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2064 sizeof(smp->prnd), smp->prnd);
2065 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2066 goto mackey_and_ltk;
2067 }
2068
Johan Hedberg38606f12014-06-25 11:10:28 +03002069 /* Passkey entry has special treatment */
2070 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2071 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2072
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002073 if (hcon->out) {
2074 u8 cfm[16];
2075
2076 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2077 smp->rrnd, 0, cfm);
2078 if (err)
2079 return SMP_UNSPECIFIED;
2080
2081 if (memcmp(smp->pcnf, cfm, 16))
2082 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002083 } else {
2084 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2085 smp->prnd);
2086 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002087 }
2088
Johan Hedberga29b0732014-10-28 15:17:05 +01002089mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002090 /* Generate MacKey and LTK */
2091 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2092 if (err)
2093 return SMP_UNSPECIFIED;
2094
Johan Hedberga29b0732014-10-28 15:17:05 +01002095 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002096 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002097 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002098 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2099 }
2100 return 0;
2101 }
2102
Johan Hedberg38606f12014-06-25 11:10:28 +03002103 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002104 if (err)
2105 return SMP_UNSPECIFIED;
2106
Johan Hedberg38606f12014-06-25 11:10:28 +03002107 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2108 hcon->dst_type, passkey, 0);
2109 if (err)
2110 return SMP_UNSPECIFIED;
2111
2112 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2113
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002114 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002115}
2116
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002117static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002118{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002119 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002120 struct hci_conn *hcon = conn->hcon;
2121
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002122 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002123 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002124 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002125
Johan Hedberga6f78332014-09-10 17:37:45 -07002126 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002127 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002128
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002129 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002130 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002131
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002132 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
2133 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002134
Johan Hedbergfe59a052014-07-01 19:14:12 +03002135 /* We never store STKs for master role, so clear this flag */
2136 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2137
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002138 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002139}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002140
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002141bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2142 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002143{
2144 if (sec_level == BT_SECURITY_LOW)
2145 return true;
2146
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002147 /* If we're encrypted with an STK but the caller prefers using
2148 * LTK claim insufficient security. This way we allow the
2149 * connection to be re-encrypted with an LTK, even if the LTK
2150 * provides the same level of security. Only exception is if we
2151 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002152 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002153 if (key_pref == SMP_USE_LTK &&
2154 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002155 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002156 return false;
2157
Johan Hedberg854f4722014-07-01 18:40:20 +03002158 if (hcon->sec_level >= sec_level)
2159 return true;
2160
2161 return false;
2162}
2163
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002164static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002165{
2166 struct smp_cmd_security_req *rp = (void *) skb->data;
2167 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002168 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002169 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002170 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002171 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002172
2173 BT_DBG("conn %p", conn);
2174
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002175 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002176 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002177
Johan Hedberg40bef302014-07-16 11:42:27 +03002178 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002179 return SMP_CMD_NOTSUPP;
2180
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002181 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002182
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002183 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002184 return SMP_AUTH_REQUIREMENTS;
2185
Johan Hedberg5be5e272014-09-10 17:58:54 -07002186 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002187 sec_level = BT_SECURITY_MEDIUM;
2188 else
2189 sec_level = authreq_to_seclevel(auth);
2190
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002191 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002192 return 0;
2193
Johan Hedbergc7262e72014-06-17 13:07:37 +03002194 if (sec_level > hcon->pending_sec_level)
2195 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002196
Johan Hedberg4dab7862012-06-07 14:58:37 +08002197 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002198 return 0;
2199
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002200 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002201 if (!smp)
2202 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002203
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002204 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002205 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002206 return SMP_PAIRING_NOTSUPP;
2207
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002208 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002209
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002210 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002211 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002212
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002213 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2214 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002215
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002216 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002217 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002218
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002219 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002220}
2221
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002222int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002223{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002224 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002225 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002226 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002227 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002228 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002229
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002230 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2231
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002232 /* This may be NULL if there's an unexpected disconnection */
2233 if (!conn)
2234 return 1;
2235
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002236 chan = conn->smp;
2237
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002238 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002239 return 1;
2240
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002241 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002242 return 1;
2243
Johan Hedbergc7262e72014-06-17 13:07:37 +03002244 if (sec_level > hcon->pending_sec_level)
2245 hcon->pending_sec_level = sec_level;
2246
Johan Hedberg40bef302014-07-16 11:42:27 +03002247 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002248 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2249 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002250
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002251 l2cap_chan_lock(chan);
2252
2253 /* If SMP is already in progress ignore this request */
2254 if (chan->data) {
2255 ret = 0;
2256 goto unlock;
2257 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002258
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002259 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002260 if (!smp) {
2261 ret = 1;
2262 goto unlock;
2263 }
Brian Gix2b64d152011-12-21 16:12:12 -08002264
2265 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002266
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002267 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002268 authreq |= SMP_AUTH_SC;
2269
Johan Hedberg79897d22014-06-01 09:45:24 +03002270 /* Require MITM if IO Capability allows or the security level
2271 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002272 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002273 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002274 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002275 authreq |= SMP_AUTH_MITM;
2276
Johan Hedberg40bef302014-07-16 11:42:27 +03002277 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002278 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002279
Brian Gix2b64d152011-12-21 16:12:12 -08002280 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002281 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2282 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002283
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002284 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002285 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002286 } else {
2287 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002288 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002289 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002290 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002291 }
2292
Johan Hedberg4a74d652014-05-20 09:45:50 +03002293 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002294 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002295
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002296unlock:
2297 l2cap_chan_unlock(chan);
2298 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002299}
2300
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002301static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2302{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002303 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002304 struct l2cap_chan *chan = conn->smp;
2305 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002306
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002307 BT_DBG("conn %p", conn);
2308
2309 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002310 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002311
Johan Hedbergb28b4942014-09-05 22:19:55 +03002312 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002313
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002314 skb_pull(skb, sizeof(*rp));
2315
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002316 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002317
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002318 return 0;
2319}
2320
2321static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2322{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002323 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002324 struct l2cap_chan *chan = conn->smp;
2325 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002326 struct hci_dev *hdev = conn->hcon->hdev;
2327 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002328 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002329 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002330
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002331 BT_DBG("conn %p", conn);
2332
2333 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002334 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002335
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002336 /* Mark the information as received */
2337 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2338
Johan Hedbergb28b4942014-09-05 22:19:55 +03002339 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2340 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002341 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2342 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002343
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002344 skb_pull(skb, sizeof(*rp));
2345
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002346 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002347 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002348 authenticated, smp->tk, smp->enc_key_size,
2349 rp->ediv, rp->rand);
2350 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002351 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002352 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002353
2354 return 0;
2355}
2356
Johan Hedbergfd349c02014-02-18 10:19:36 +02002357static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2358{
2359 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002360 struct l2cap_chan *chan = conn->smp;
2361 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002362
2363 BT_DBG("");
2364
2365 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002366 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002367
Johan Hedbergb28b4942014-09-05 22:19:55 +03002368 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002369
Johan Hedbergfd349c02014-02-18 10:19:36 +02002370 skb_pull(skb, sizeof(*info));
2371
2372 memcpy(smp->irk, info->irk, 16);
2373
2374 return 0;
2375}
2376
2377static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2378 struct sk_buff *skb)
2379{
2380 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002381 struct l2cap_chan *chan = conn->smp;
2382 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002383 struct hci_conn *hcon = conn->hcon;
2384 bdaddr_t rpa;
2385
2386 BT_DBG("");
2387
2388 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002389 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002390
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002391 /* Mark the information as received */
2392 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2393
Johan Hedbergb28b4942014-09-05 22:19:55 +03002394 if (smp->remote_key_dist & SMP_DIST_SIGN)
2395 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2396
Johan Hedbergfd349c02014-02-18 10:19:36 +02002397 skb_pull(skb, sizeof(*info));
2398
Johan Hedberga9a58f82014-02-25 22:24:37 +02002399 /* Strictly speaking the Core Specification (4.1) allows sending
2400 * an empty address which would force us to rely on just the IRK
2401 * as "identity information". However, since such
2402 * implementations are not known of and in order to not over
2403 * complicate our implementation, simply pretend that we never
2404 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002405 *
2406 * The Identity Address must also be a Static Random or Public
2407 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002408 */
Johan Hedberge12af482015-01-14 20:51:37 +02002409 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2410 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Johan Hedberga9a58f82014-02-25 22:24:37 +02002411 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002412 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002413 }
2414
Johan Hedbergfd349c02014-02-18 10:19:36 +02002415 bacpy(&smp->id_addr, &info->bdaddr);
2416 smp->id_addr_type = info->addr_type;
2417
2418 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2419 bacpy(&rpa, &hcon->dst);
2420 else
2421 bacpy(&rpa, BDADDR_ANY);
2422
Johan Hedberg23d0e122014-02-19 14:57:46 +02002423 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2424 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002425
Johan Hedberg31dd6242014-06-27 14:23:02 +03002426distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002427 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2428 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002429
2430 return 0;
2431}
2432
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002433static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2434{
2435 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002436 struct l2cap_chan *chan = conn->smp;
2437 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002438 struct smp_csrk *csrk;
2439
2440 BT_DBG("conn %p", conn);
2441
2442 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002443 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002444
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002445 /* Mark the information as received */
2446 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2447
2448 skb_pull(skb, sizeof(*rp));
2449
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002450 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2451 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002452 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2453 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2454 else
2455 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002456 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2457 }
2458 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002459 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002460
2461 return 0;
2462}
2463
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002464static u8 sc_select_method(struct smp_chan *smp)
2465{
2466 struct l2cap_conn *conn = smp->conn;
2467 struct hci_conn *hcon = conn->hcon;
2468 struct smp_cmd_pairing *local, *remote;
2469 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2470
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002471 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2472 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002473 return REQ_OOB;
2474
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002475 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2476 * which are needed as inputs to some crypto functions. To get
2477 * the "struct smp_cmd_pairing" from them we need to skip the
2478 * first byte which contains the opcode.
2479 */
2480 if (hcon->out) {
2481 local = (void *) &smp->preq[1];
2482 remote = (void *) &smp->prsp[1];
2483 } else {
2484 local = (void *) &smp->prsp[1];
2485 remote = (void *) &smp->preq[1];
2486 }
2487
2488 local_io = local->io_capability;
2489 remote_io = remote->io_capability;
2490
2491 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2492 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2493
2494 /* If either side wants MITM, look up the method from the table,
2495 * otherwise use JUST WORKS.
2496 */
2497 if (local_mitm || remote_mitm)
2498 method = get_auth_method(smp, local_io, remote_io);
2499 else
2500 method = JUST_WORKS;
2501
2502 /* Don't confirm locally initiated pairing attempts */
2503 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2504 method = JUST_WORKS;
2505
2506 return method;
2507}
2508
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002509static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2510{
2511 struct smp_cmd_public_key *key = (void *) skb->data;
2512 struct hci_conn *hcon = conn->hcon;
2513 struct l2cap_chan *chan = conn->smp;
2514 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002515 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002516 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002517 int err;
2518
2519 BT_DBG("conn %p", conn);
2520
2521 if (skb->len < sizeof(*key))
2522 return SMP_INVALID_PARAMS;
2523
2524 memcpy(smp->remote_pk, key, 64);
2525
Johan Hedberga8ca6172015-03-16 18:12:57 +02002526 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2527 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2528 smp->rr, 0, cfm.confirm_val);
2529 if (err)
2530 return SMP_UNSPECIFIED;
2531
2532 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2533 return SMP_CONFIRM_FAILED;
2534 }
2535
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002536 /* Non-initiating device sends its public key after receiving
2537 * the key from the initiating device.
2538 */
2539 if (!hcon->out) {
2540 err = sc_send_public_key(smp);
2541 if (err)
2542 return err;
2543 }
2544
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002545 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2546 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002547
2548 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2549 return SMP_UNSPECIFIED;
2550
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002551 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002552
2553 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2554
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002555 smp->method = sc_select_method(smp);
2556
2557 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2558
2559 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2560 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2561 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2562 else
2563 hcon->pending_sec_level = BT_SECURITY_FIPS;
2564
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002565 if (!memcmp(debug_pk, smp->remote_pk, 64))
2566 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2567
Johan Hedberg38606f12014-06-25 11:10:28 +03002568 if (smp->method == DSP_PASSKEY) {
2569 get_random_bytes(&hcon->passkey_notify,
2570 sizeof(hcon->passkey_notify));
2571 hcon->passkey_notify %= 1000000;
2572 hcon->passkey_entered = 0;
2573 smp->passkey_round = 0;
2574 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2575 hcon->dst_type,
2576 hcon->passkey_notify,
2577 hcon->passkey_entered))
2578 return SMP_UNSPECIFIED;
2579 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2580 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2581 }
2582
Johan Hedberg94ea7252015-03-16 11:45:46 +02002583 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002584 if (hcon->out)
2585 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2586 sizeof(smp->prnd), smp->prnd);
2587
2588 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2589
2590 return 0;
2591 }
2592
Johan Hedberg38606f12014-06-25 11:10:28 +03002593 if (hcon->out)
2594 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2595
2596 if (smp->method == REQ_PASSKEY) {
2597 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2598 hcon->dst_type))
2599 return SMP_UNSPECIFIED;
2600 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2601 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2602 return 0;
2603 }
2604
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002605 /* The Initiating device waits for the non-initiating device to
2606 * send the confirm value.
2607 */
2608 if (conn->hcon->out)
2609 return 0;
2610
2611 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2612 0, cfm.confirm_val);
2613 if (err)
2614 return SMP_UNSPECIFIED;
2615
2616 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2617 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2618
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002619 return 0;
2620}
2621
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002622static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2623{
2624 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2625 struct l2cap_chan *chan = conn->smp;
2626 struct hci_conn *hcon = conn->hcon;
2627 struct smp_chan *smp = chan->data;
2628 u8 a[7], b[7], *local_addr, *remote_addr;
2629 u8 io_cap[3], r[16], e[16];
2630 int err;
2631
2632 BT_DBG("conn %p", conn);
2633
2634 if (skb->len < sizeof(*check))
2635 return SMP_INVALID_PARAMS;
2636
2637 memcpy(a, &hcon->init_addr, 6);
2638 memcpy(b, &hcon->resp_addr, 6);
2639 a[6] = hcon->init_addr_type;
2640 b[6] = hcon->resp_addr_type;
2641
2642 if (hcon->out) {
2643 local_addr = a;
2644 remote_addr = b;
2645 memcpy(io_cap, &smp->prsp[1], 3);
2646 } else {
2647 local_addr = b;
2648 remote_addr = a;
2649 memcpy(io_cap, &smp->preq[1], 3);
2650 }
2651
2652 memset(r, 0, sizeof(r));
2653
Johan Hedberg38606f12014-06-25 11:10:28 +03002654 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2655 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002656 else if (smp->method == REQ_OOB)
2657 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002658
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002659 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2660 io_cap, remote_addr, local_addr, e);
2661 if (err)
2662 return SMP_UNSPECIFIED;
2663
2664 if (memcmp(check->e, e, 16))
2665 return SMP_DHKEY_CHECK_FAILED;
2666
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002667 if (!hcon->out) {
2668 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2669 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2670 return 0;
2671 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002672
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002673 /* Slave sends DHKey check as response to master */
2674 sc_dhkey_check(smp);
2675 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002676
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002677 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002678
2679 if (hcon->out) {
2680 hci_le_start_enc(hcon, 0, 0, smp->tk);
2681 hcon->enc_key_size = smp->enc_key_size;
2682 }
2683
2684 return 0;
2685}
2686
Johan Hedberg1408bb62014-06-04 22:45:57 +03002687static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2688 struct sk_buff *skb)
2689{
2690 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2691
2692 BT_DBG("value 0x%02x", kp->value);
2693
2694 return 0;
2695}
2696
Johan Hedberg4befb862014-08-11 22:06:38 +03002697static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002698{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002699 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002700 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002701 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002702 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002703 int err = 0;
2704
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002705 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002706 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002707
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002708 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002709 reason = SMP_PAIRING_NOTSUPP;
2710 goto done;
2711 }
2712
Marcel Holtmann92381f52013-10-03 01:23:08 -07002713 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002714 skb_pull(skb, sizeof(code));
2715
Johan Hedbergb28b4942014-09-05 22:19:55 +03002716 smp = chan->data;
2717
2718 if (code > SMP_CMD_MAX)
2719 goto drop;
2720
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002721 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002722 goto drop;
2723
2724 /* If we don't have a context the only allowed commands are
2725 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002726 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002727 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2728 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002729
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002730 switch (code) {
2731 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002732 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002733 break;
2734
2735 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002736 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002737 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002738 break;
2739
2740 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002741 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002742 break;
2743
2744 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002745 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002746 break;
2747
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002748 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002749 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002750 break;
2751
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002752 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002753 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002754 break;
2755
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002756 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002757 reason = smp_cmd_encrypt_info(conn, skb);
2758 break;
2759
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002760 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002761 reason = smp_cmd_master_ident(conn, skb);
2762 break;
2763
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002764 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002765 reason = smp_cmd_ident_info(conn, skb);
2766 break;
2767
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002768 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002769 reason = smp_cmd_ident_addr_info(conn, skb);
2770 break;
2771
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002772 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002773 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002774 break;
2775
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002776 case SMP_CMD_PUBLIC_KEY:
2777 reason = smp_cmd_public_key(conn, skb);
2778 break;
2779
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002780 case SMP_CMD_DHKEY_CHECK:
2781 reason = smp_cmd_dhkey_check(conn, skb);
2782 break;
2783
Johan Hedberg1408bb62014-06-04 22:45:57 +03002784 case SMP_CMD_KEYPRESS_NOTIFY:
2785 reason = smp_cmd_keypress_notify(conn, skb);
2786 break;
2787
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002788 default:
2789 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002790 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002791 goto done;
2792 }
2793
2794done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002795 if (!err) {
2796 if (reason)
2797 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002798 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002799 }
2800
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002801 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002802
2803drop:
2804 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2805 code, &hcon->dst);
2806 kfree_skb(skb);
2807 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002808}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002809
Johan Hedberg70db83c2014-08-08 09:37:16 +03002810static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2811{
2812 struct l2cap_conn *conn = chan->conn;
2813
2814 BT_DBG("chan %p", chan);
2815
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002816 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002817 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002818
Johan Hedberg70db83c2014-08-08 09:37:16 +03002819 conn->smp = NULL;
2820 l2cap_chan_put(chan);
2821}
2822
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002823static void bredr_pairing(struct l2cap_chan *chan)
2824{
2825 struct l2cap_conn *conn = chan->conn;
2826 struct hci_conn *hcon = conn->hcon;
2827 struct hci_dev *hdev = hcon->hdev;
2828 struct smp_cmd_pairing req;
2829 struct smp_chan *smp;
2830
2831 BT_DBG("chan %p", chan);
2832
2833 /* Only new pairings are interesting */
2834 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2835 return;
2836
2837 /* Don't bother if we're not encrypted */
2838 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2839 return;
2840
2841 /* Only master may initiate SMP over BR/EDR */
2842 if (hcon->role != HCI_ROLE_MASTER)
2843 return;
2844
2845 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002846 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002847 return;
2848
2849 /* BR/EDR must use Secure Connections for SMP */
2850 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07002851 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002852 return;
2853
2854 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002855 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002856 return;
2857
2858 /* Don't bother if remote LE support is not enabled */
2859 if (!lmp_host_le_capable(hcon))
2860 return;
2861
2862 /* Remote must support SMP fixed chan for BR/EDR */
2863 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2864 return;
2865
2866 /* Don't bother if SMP is already ongoing */
2867 if (chan->data)
2868 return;
2869
2870 smp = smp_chan_create(conn);
2871 if (!smp) {
2872 BT_ERR("%s unable to create SMP context for BR/EDR",
2873 hdev->name);
2874 return;
2875 }
2876
2877 set_bit(SMP_FLAG_SC, &smp->flags);
2878
2879 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2880
2881 /* Prepare and send the BR/EDR SMP Pairing Request */
2882 build_bredr_pairing_cmd(smp, &req, NULL);
2883
2884 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2885 memcpy(&smp->preq[1], &req, sizeof(req));
2886
2887 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2888 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2889}
2890
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002891static void smp_resume_cb(struct l2cap_chan *chan)
2892{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002893 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002894 struct l2cap_conn *conn = chan->conn;
2895 struct hci_conn *hcon = conn->hcon;
2896
2897 BT_DBG("chan %p", chan);
2898
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002899 if (hcon->type == ACL_LINK) {
2900 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002901 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002902 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002903
Johan Hedberg86d14072014-08-11 22:06:43 +03002904 if (!smp)
2905 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002906
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002907 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2908 return;
2909
Johan Hedberg86d14072014-08-11 22:06:43 +03002910 cancel_delayed_work(&smp->security_timer);
2911
Johan Hedbergd6268e82014-09-05 22:19:51 +03002912 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002913}
2914
Johan Hedberg70db83c2014-08-08 09:37:16 +03002915static void smp_ready_cb(struct l2cap_chan *chan)
2916{
2917 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002918 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002919
2920 BT_DBG("chan %p", chan);
2921
2922 conn->smp = chan;
2923 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002924
2925 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2926 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002927}
2928
Johan Hedberg4befb862014-08-11 22:06:38 +03002929static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2930{
2931 int err;
2932
2933 BT_DBG("chan %p", chan);
2934
2935 err = smp_sig_channel(chan, skb);
2936 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002937 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002938
Johan Hedbergb68fda62014-08-11 22:06:40 +03002939 if (smp)
2940 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002941
Johan Hedberg1e91c292014-08-18 20:33:29 +03002942 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002943 }
2944
2945 return err;
2946}
2947
Johan Hedberg70db83c2014-08-08 09:37:16 +03002948static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2949 unsigned long hdr_len,
2950 unsigned long len, int nb)
2951{
2952 struct sk_buff *skb;
2953
2954 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2955 if (!skb)
2956 return ERR_PTR(-ENOMEM);
2957
2958 skb->priority = HCI_PRIO_MAX;
2959 bt_cb(skb)->chan = chan;
2960
2961 return skb;
2962}
2963
2964static const struct l2cap_ops smp_chan_ops = {
2965 .name = "Security Manager",
2966 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002967 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002968 .alloc_skb = smp_alloc_skb_cb,
2969 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002970 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002971
2972 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002973 .state_change = l2cap_chan_no_state_change,
2974 .close = l2cap_chan_no_close,
2975 .defer = l2cap_chan_no_defer,
2976 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002977 .set_shutdown = l2cap_chan_no_set_shutdown,
2978 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002979};
2980
2981static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2982{
2983 struct l2cap_chan *chan;
2984
2985 BT_DBG("pchan %p", pchan);
2986
2987 chan = l2cap_chan_create();
2988 if (!chan)
2989 return NULL;
2990
2991 chan->chan_type = pchan->chan_type;
2992 chan->ops = &smp_chan_ops;
2993 chan->scid = pchan->scid;
2994 chan->dcid = chan->scid;
2995 chan->imtu = pchan->imtu;
2996 chan->omtu = pchan->omtu;
2997 chan->mode = pchan->mode;
2998
Johan Hedbergabe84902014-11-12 22:22:21 +02002999 /* Other L2CAP channels may request SMP routines in order to
3000 * change the security level. This means that the SMP channel
3001 * lock must be considered in its own category to avoid lockdep
3002 * warnings.
3003 */
3004 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3005
Johan Hedberg70db83c2014-08-08 09:37:16 +03003006 BT_DBG("created chan %p", chan);
3007
3008 return chan;
3009}
3010
3011static const struct l2cap_ops smp_root_chan_ops = {
3012 .name = "Security Manager Root",
3013 .new_connection = smp_new_conn_cb,
3014
3015 /* None of these are implemented for the root channel */
3016 .close = l2cap_chan_no_close,
3017 .alloc_skb = l2cap_chan_no_alloc_skb,
3018 .recv = l2cap_chan_no_recv,
3019 .state_change = l2cap_chan_no_state_change,
3020 .teardown = l2cap_chan_no_teardown,
3021 .ready = l2cap_chan_no_ready,
3022 .defer = l2cap_chan_no_defer,
3023 .suspend = l2cap_chan_no_suspend,
3024 .resume = l2cap_chan_no_resume,
3025 .set_shutdown = l2cap_chan_no_set_shutdown,
3026 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003027};
3028
Johan Hedbergef8efe42014-08-13 15:12:32 +03003029static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003030{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003031 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003032 struct smp_dev *smp;
3033 struct crypto_blkcipher *tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003034 struct crypto_hash *tfm_cmac;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003035
Johan Hedbergef8efe42014-08-13 15:12:32 +03003036 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003037 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003038 goto create_chan;
3039 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003040
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003041 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3042 if (!smp)
3043 return ERR_PTR(-ENOMEM);
3044
3045 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003046 if (IS_ERR(tfm_aes)) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003047 BT_ERR("Unable to create ECB crypto context");
3048 kzfree(smp);
Fengguang Wufe700772014-12-08 03:04:38 +08003049 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003050 }
3051
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003052 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3053 if (IS_ERR(tfm_cmac)) {
3054 BT_ERR("Unable to create CMAC crypto context");
3055 crypto_free_blkcipher(tfm_aes);
3056 kzfree(smp);
3057 return ERR_CAST(tfm_cmac);
3058 }
3059
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003060 smp->tfm_aes = tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003061 smp->tfm_cmac = tfm_cmac;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003062
Johan Hedbergef8efe42014-08-13 15:12:32 +03003063create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003064 chan = l2cap_chan_create();
3065 if (!chan) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003066 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003067 crypto_free_hash(smp->tfm_cmac);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003068 kzfree(smp);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003069 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003070 }
3071
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003072 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003073
Johan Hedbergef8efe42014-08-13 15:12:32 +03003074 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003075
3076 l2cap_chan_set_defaults(chan);
3077
Marcel Holtmann157029b2015-01-14 15:43:09 -08003078 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003079 u8 bdaddr_type;
3080
3081 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3082
3083 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003084 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003085 else
3086 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003087 } else {
3088 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003089 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003090 }
3091
Johan Hedberg70db83c2014-08-08 09:37:16 +03003092 chan->state = BT_LISTEN;
3093 chan->mode = L2CAP_MODE_BASIC;
3094 chan->imtu = L2CAP_DEFAULT_MTU;
3095 chan->ops = &smp_root_chan_ops;
3096
Johan Hedbergabe84902014-11-12 22:22:21 +02003097 /* Set correct nesting level for a parent/listening channel */
3098 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3099
Johan Hedbergef8efe42014-08-13 15:12:32 +03003100 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003101}
3102
Johan Hedbergef8efe42014-08-13 15:12:32 +03003103static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003104{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003105 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003106
Johan Hedbergef8efe42014-08-13 15:12:32 +03003107 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003108
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003109 smp = chan->data;
3110 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003111 chan->data = NULL;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003112 if (smp->tfm_aes)
3113 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003114 if (smp->tfm_cmac)
3115 crypto_free_hash(smp->tfm_cmac);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003116 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003117 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003118
Johan Hedberg70db83c2014-08-08 09:37:16 +03003119 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003120}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003121
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003122static ssize_t force_bredr_smp_read(struct file *file,
3123 char __user *user_buf,
3124 size_t count, loff_t *ppos)
3125{
3126 struct hci_dev *hdev = file->private_data;
3127 char buf[3];
3128
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003129 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003130 buf[1] = '\n';
3131 buf[2] = '\0';
3132 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3133}
3134
3135static ssize_t force_bredr_smp_write(struct file *file,
3136 const char __user *user_buf,
3137 size_t count, loff_t *ppos)
3138{
3139 struct hci_dev *hdev = file->private_data;
3140 char buf[32];
3141 size_t buf_size = min(count, (sizeof(buf)-1));
3142 bool enable;
3143
3144 if (copy_from_user(buf, user_buf, buf_size))
3145 return -EFAULT;
3146
3147 buf[buf_size] = '\0';
3148 if (strtobool(buf, &enable))
3149 return -EINVAL;
3150
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003151 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003152 return -EALREADY;
3153
3154 if (enable) {
3155 struct l2cap_chan *chan;
3156
3157 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3158 if (IS_ERR(chan))
3159 return PTR_ERR(chan);
3160
3161 hdev->smp_bredr_data = chan;
3162 } else {
3163 struct l2cap_chan *chan;
3164
3165 chan = hdev->smp_bredr_data;
3166 hdev->smp_bredr_data = NULL;
3167 smp_del_chan(chan);
3168 }
3169
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003170 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003171
3172 return count;
3173}
3174
3175static const struct file_operations force_bredr_smp_fops = {
3176 .open = simple_open,
3177 .read = force_bredr_smp_read,
3178 .write = force_bredr_smp_write,
3179 .llseek = default_llseek,
3180};
3181
Johan Hedbergef8efe42014-08-13 15:12:32 +03003182int smp_register(struct hci_dev *hdev)
3183{
3184 struct l2cap_chan *chan;
3185
3186 BT_DBG("%s", hdev->name);
3187
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003188 /* If the controller does not support Low Energy operation, then
3189 * there is also no need to register any SMP channel.
3190 */
3191 if (!lmp_le_capable(hdev))
3192 return 0;
3193
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003194 if (WARN_ON(hdev->smp_data)) {
3195 chan = hdev->smp_data;
3196 hdev->smp_data = NULL;
3197 smp_del_chan(chan);
3198 }
3199
Johan Hedbergef8efe42014-08-13 15:12:32 +03003200 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3201 if (IS_ERR(chan))
3202 return PTR_ERR(chan);
3203
3204 hdev->smp_data = chan;
3205
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003206 /* If the controller does not support BR/EDR Secure Connections
3207 * feature, then the BR/EDR SMP channel shall not be present.
3208 *
3209 * To test this with Bluetooth 4.0 controllers, create a debugfs
3210 * switch that allows forcing BR/EDR SMP support and accepting
3211 * cross-transport pairing on non-AES encrypted connections.
3212 */
3213 if (!lmp_sc_capable(hdev)) {
3214 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3215 hdev, &force_bredr_smp_fops);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003216 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003217 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003218
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003219 if (WARN_ON(hdev->smp_bredr_data)) {
3220 chan = hdev->smp_bredr_data;
3221 hdev->smp_bredr_data = NULL;
3222 smp_del_chan(chan);
3223 }
3224
Johan Hedbergef8efe42014-08-13 15:12:32 +03003225 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3226 if (IS_ERR(chan)) {
3227 int err = PTR_ERR(chan);
3228 chan = hdev->smp_data;
3229 hdev->smp_data = NULL;
3230 smp_del_chan(chan);
3231 return err;
3232 }
3233
3234 hdev->smp_bredr_data = chan;
3235
3236 return 0;
3237}
3238
3239void smp_unregister(struct hci_dev *hdev)
3240{
3241 struct l2cap_chan *chan;
3242
3243 if (hdev->smp_bredr_data) {
3244 chan = hdev->smp_bredr_data;
3245 hdev->smp_bredr_data = NULL;
3246 smp_del_chan(chan);
3247 }
3248
3249 if (hdev->smp_data) {
3250 chan = hdev->smp_data;
3251 hdev->smp_data = NULL;
3252 smp_del_chan(chan);
3253 }
3254}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003255
3256#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3257
Johan Hedbergcfc41982014-12-30 09:50:40 +02003258static int __init test_ah(struct crypto_blkcipher *tfm_aes)
3259{
3260 const u8 irk[16] = {
3261 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3262 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3263 const u8 r[3] = { 0x94, 0x81, 0x70 };
3264 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3265 u8 res[3];
3266 int err;
3267
3268 err = smp_ah(tfm_aes, irk, r, res);
3269 if (err)
3270 return err;
3271
3272 if (memcmp(res, exp, 3))
3273 return -EINVAL;
3274
3275 return 0;
3276}
3277
3278static int __init test_c1(struct crypto_blkcipher *tfm_aes)
3279{
3280 const u8 k[16] = {
3281 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3282 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3283 const u8 r[16] = {
3284 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3285 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3286 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3287 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3288 const u8 _iat = 0x01;
3289 const u8 _rat = 0x00;
3290 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3291 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3292 const u8 exp[16] = {
3293 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3294 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3295 u8 res[16];
3296 int err;
3297
3298 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3299 if (err)
3300 return err;
3301
3302 if (memcmp(res, exp, 16))
3303 return -EINVAL;
3304
3305 return 0;
3306}
3307
3308static int __init test_s1(struct crypto_blkcipher *tfm_aes)
3309{
3310 const u8 k[16] = {
3311 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3312 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3313 const u8 r1[16] = {
3314 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3315 const u8 r2[16] = {
3316 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3317 const u8 exp[16] = {
3318 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3319 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3320 u8 res[16];
3321 int err;
3322
3323 err = smp_s1(tfm_aes, k, r1, r2, res);
3324 if (err)
3325 return err;
3326
3327 if (memcmp(res, exp, 16))
3328 return -EINVAL;
3329
3330 return 0;
3331}
3332
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003333static int __init test_f4(struct crypto_hash *tfm_cmac)
3334{
3335 const u8 u[32] = {
3336 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3337 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3338 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3339 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3340 const u8 v[32] = {
3341 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3342 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3343 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3344 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3345 const u8 x[16] = {
3346 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3347 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3348 const u8 z = 0x00;
3349 const u8 exp[16] = {
3350 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3351 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3352 u8 res[16];
3353 int err;
3354
3355 err = smp_f4(tfm_cmac, u, v, x, z, res);
3356 if (err)
3357 return err;
3358
3359 if (memcmp(res, exp, 16))
3360 return -EINVAL;
3361
3362 return 0;
3363}
3364
3365static int __init test_f5(struct crypto_hash *tfm_cmac)
3366{
3367 const u8 w[32] = {
3368 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3369 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3370 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3371 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3372 const u8 n1[16] = {
3373 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3374 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3375 const u8 n2[16] = {
3376 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3377 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3378 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3379 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3380 const u8 exp_ltk[16] = {
3381 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3382 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3383 const u8 exp_mackey[16] = {
3384 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3385 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3386 u8 mackey[16], ltk[16];
3387 int err;
3388
3389 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3390 if (err)
3391 return err;
3392
3393 if (memcmp(mackey, exp_mackey, 16))
3394 return -EINVAL;
3395
3396 if (memcmp(ltk, exp_ltk, 16))
3397 return -EINVAL;
3398
3399 return 0;
3400}
3401
3402static int __init test_f6(struct crypto_hash *tfm_cmac)
3403{
3404 const u8 w[16] = {
3405 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3406 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3407 const u8 n1[16] = {
3408 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3409 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3410 const u8 n2[16] = {
3411 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3412 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3413 const u8 r[16] = {
3414 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3415 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3416 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3417 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3418 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3419 const u8 exp[16] = {
3420 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3421 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3422 u8 res[16];
3423 int err;
3424
3425 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3426 if (err)
3427 return err;
3428
3429 if (memcmp(res, exp, 16))
3430 return -EINVAL;
3431
3432 return 0;
3433}
3434
3435static int __init test_g2(struct crypto_hash *tfm_cmac)
3436{
3437 const u8 u[32] = {
3438 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3439 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3440 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3441 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3442 const u8 v[32] = {
3443 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3444 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3445 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3446 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3447 const u8 x[16] = {
3448 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3449 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3450 const u8 y[16] = {
3451 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3452 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3453 const u32 exp_val = 0x2f9ed5ba % 1000000;
3454 u32 val;
3455 int err;
3456
3457 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3458 if (err)
3459 return err;
3460
3461 if (val != exp_val)
3462 return -EINVAL;
3463
3464 return 0;
3465}
3466
3467static int __init test_h6(struct crypto_hash *tfm_cmac)
3468{
3469 const u8 w[16] = {
3470 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3471 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3472 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3473 const u8 exp[16] = {
3474 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3475 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3476 u8 res[16];
3477 int err;
3478
3479 err = smp_h6(tfm_cmac, w, key_id, res);
3480 if (err)
3481 return err;
3482
3483 if (memcmp(res, exp, 16))
3484 return -EINVAL;
3485
3486 return 0;
3487}
3488
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003489static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3490 struct crypto_hash *tfm_cmac)
3491{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003492 ktime_t calltime, delta, rettime;
3493 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003494 int err;
3495
Marcel Holtmann255047b2014-12-30 00:11:20 -08003496 calltime = ktime_get();
3497
Johan Hedbergcfc41982014-12-30 09:50:40 +02003498 err = test_ah(tfm_aes);
3499 if (err) {
3500 BT_ERR("smp_ah test failed");
3501 return err;
3502 }
3503
3504 err = test_c1(tfm_aes);
3505 if (err) {
3506 BT_ERR("smp_c1 test failed");
3507 return err;
3508 }
3509
3510 err = test_s1(tfm_aes);
3511 if (err) {
3512 BT_ERR("smp_s1 test failed");
3513 return err;
3514 }
3515
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003516 err = test_f4(tfm_cmac);
3517 if (err) {
3518 BT_ERR("smp_f4 test failed");
3519 return err;
3520 }
3521
3522 err = test_f5(tfm_cmac);
3523 if (err) {
3524 BT_ERR("smp_f5 test failed");
3525 return err;
3526 }
3527
3528 err = test_f6(tfm_cmac);
3529 if (err) {
3530 BT_ERR("smp_f6 test failed");
3531 return err;
3532 }
3533
3534 err = test_g2(tfm_cmac);
3535 if (err) {
3536 BT_ERR("smp_g2 test failed");
3537 return err;
3538 }
3539
3540 err = test_h6(tfm_cmac);
3541 if (err) {
3542 BT_ERR("smp_h6 test failed");
3543 return err;
3544 }
3545
Marcel Holtmann255047b2014-12-30 00:11:20 -08003546 rettime = ktime_get();
3547 delta = ktime_sub(rettime, calltime);
3548 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3549
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003550 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003551
3552 return 0;
3553}
3554
3555int __init bt_selftest_smp(void)
3556{
3557 struct crypto_blkcipher *tfm_aes;
3558 struct crypto_hash *tfm_cmac;
3559 int err;
3560
3561 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
3562 if (IS_ERR(tfm_aes)) {
3563 BT_ERR("Unable to create ECB crypto context");
3564 return PTR_ERR(tfm_aes);
3565 }
3566
3567 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3568 if (IS_ERR(tfm_cmac)) {
3569 BT_ERR("Unable to create CMAC crypto context");
3570 crypto_free_blkcipher(tfm_aes);
3571 return PTR_ERR(tfm_cmac);
3572 }
3573
3574 err = run_selftests(tfm_aes, tfm_cmac);
3575
3576 crypto_free_hash(tfm_cmac);
3577 crypto_free_blkcipher(tfm_aes);
3578
3579 return err;
3580}
3581
3582#endif