blob: 13b40e8af95efb10cbd5112e1c06f47d7b6ce898 [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
Gustavo Padovan8c520a52012-05-23 04:04:22 -030023#include <linux/crypto.h>
24#include <linux/scatterlist.h>
25#include <crypto/b128ops.h>
26
Anderson Brigliaeb492e02011-06-09 18:50:40 -030027#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080030#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070031
Johan Hedberg3b191462014-06-06 10:50:15 +030032#include "ecc.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070033#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030034
Johan Hedbergc7a3d572014-12-01 22:03:16 +020035/* Low-level debug macros to be used for stuff that we don't want
36 * accidentially in dmesg, i.e. the values of the various crypto keys
37 * and the inputs & outputs of crypto functions.
38 */
39#ifdef DEBUG
40#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
41 ##__VA_ARGS__)
42#else
43#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
44 ##__VA_ARGS__)
45#endif
46
Johan Hedbergb28b4942014-09-05 22:19:55 +030047#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030048
Johan Hedberg3b191462014-06-06 10:50:15 +030049/* Keys which are not distributed with Secure Connections */
50#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
51
Marcel Holtmann17b02e62012-03-01 14:32:37 -080052#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030053
Johan Hedberg0edb14d2014-05-26 13:29:28 +030054#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
55 0x1f : 0x07)
56#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020057
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030058/* Maximum message length that can be passed to aes_cmac */
59#define CMAC_MSG_MAX 80
60
Johan Hedberg533e35d2014-06-16 19:25:18 +030061enum {
62 SMP_FLAG_TK_VALID,
63 SMP_FLAG_CFM_PENDING,
64 SMP_FLAG_MITM_AUTH,
65 SMP_FLAG_COMPLETE,
66 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030067 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030068 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030069 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030070 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030071 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg02b05bd2014-10-26 21:19:10 +010072 SMP_FLAG_OOB,
Johan Hedberg533e35d2014-06-16 19:25:18 +030073};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030074
75struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030076 struct l2cap_conn *conn;
77 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030078 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030079
Johan Hedberg4bc58f52014-05-20 09:45:47 +030080 u8 preq[7]; /* SMP Pairing Request */
81 u8 prsp[7]; /* SMP Pairing Response */
82 u8 prnd[16]; /* SMP Pairing Random (local) */
83 u8 rrnd[16]; /* SMP Pairing Random (remote) */
84 u8 pcnf[16]; /* SMP Pairing Confirm */
85 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberga29b0732014-10-28 15:17:05 +010086 u8 rr[16];
Johan Hedberg4bc58f52014-05-20 09:45:47 +030087 u8 enc_key_size;
88 u8 remote_key_dist;
89 bdaddr_t id_addr;
90 u8 id_addr_type;
91 u8 irk[16];
92 struct smp_csrk *csrk;
93 struct smp_csrk *slave_csrk;
94 struct smp_ltk *ltk;
95 struct smp_ltk *slave_ltk;
96 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +030097 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +030098 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +030099 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300100 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300101
Johan Hedberg3b191462014-06-06 10:50:15 +0300102 /* Secure Connections variables */
103 u8 local_pk[64];
104 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300105 u8 remote_pk[64];
106 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300107 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300108
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300109 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +0300110 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300111};
112
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300113/* These debug key values are defined in the SMP section of the core
114 * specification. debug_pk is the public debug key and debug_sk the
115 * private debug key.
116 */
117static const u8 debug_pk[64] = {
118 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
119 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
120 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
121 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
122
123 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
124 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
125 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
126 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
127};
128
129static const u8 debug_sk[32] = {
130 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
131 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
132 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
133 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
134};
135
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300136static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300137{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300138 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300139
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300140 for (i = 0; i < len; i++)
141 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300142}
143
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300144static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
145 size_t len, u8 mac[16])
146{
147 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
148 struct hash_desc desc;
149 struct scatterlist sg;
150 int err;
151
152 if (len > CMAC_MSG_MAX)
153 return -EFBIG;
154
155 if (!tfm) {
156 BT_ERR("tfm %p", tfm);
157 return -EINVAL;
158 }
159
160 desc.tfm = tfm;
161 desc.flags = 0;
162
163 crypto_hash_init(&desc);
164
165 /* Swap key and message from LSB to MSB */
166 swap_buf(k, tmp, 16);
167 swap_buf(m, msg_msb, len);
168
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200169 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
170 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300171
172 err = crypto_hash_setkey(tfm, tmp, 16);
173 if (err) {
174 BT_ERR("cipher setkey failed: %d", err);
175 return err;
176 }
177
178 sg_init_one(&sg, msg_msb, len);
179
180 err = crypto_hash_update(&desc, &sg, len);
181 if (err) {
182 BT_ERR("Hash update error %d", err);
183 return err;
184 }
185
186 err = crypto_hash_final(&desc, mac_msb);
187 if (err) {
188 BT_ERR("Hash final error %d", err);
189 return err;
190 }
191
192 swap_buf(mac_msb, mac, 16);
193
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200194 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300195
196 return 0;
197}
198
199static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
200 const u8 x[16], u8 z, u8 res[16])
201{
202 u8 m[65];
203 int err;
204
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200205 SMP_DBG("u %32phN", u);
206 SMP_DBG("v %32phN", v);
207 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300208
209 m[0] = z;
210 memcpy(m + 1, v, 32);
211 memcpy(m + 33, u, 32);
212
213 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
214 if (err)
215 return err;
216
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200217 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300218
219 return err;
220}
221
Johan Hedberg760b0182014-06-06 11:44:05 +0300222static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
223 u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
224{
225 /* The btle, salt and length "magic" values are as defined in
226 * the SMP section of the Bluetooth core specification. In ASCII
227 * the btle value ends up being 'btle'. The salt is just a
228 * random number whereas length is the value 256 in little
229 * endian format.
230 */
231 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
232 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
233 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
234 const u8 length[2] = { 0x00, 0x01 };
235 u8 m[53], t[16];
236 int err;
237
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200238 SMP_DBG("w %32phN", w);
239 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
240 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300241
242 err = aes_cmac(tfm_cmac, salt, w, 32, t);
243 if (err)
244 return err;
245
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200246 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300247
248 memcpy(m, length, 2);
249 memcpy(m + 2, a2, 7);
250 memcpy(m + 9, a1, 7);
251 memcpy(m + 16, n2, 16);
252 memcpy(m + 32, n1, 16);
253 memcpy(m + 48, btle, 4);
254
255 m[52] = 0; /* Counter */
256
257 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
258 if (err)
259 return err;
260
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200261 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300262
263 m[52] = 1; /* Counter */
264
265 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
266 if (err)
267 return err;
268
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200269 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300270
271 return 0;
272}
273
274static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
275 const u8 n1[16], u8 n2[16], const u8 r[16],
276 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
277 u8 res[16])
278{
279 u8 m[65];
280 int err;
281
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200282 SMP_DBG("w %16phN", w);
283 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
284 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300285
286 memcpy(m, a2, 7);
287 memcpy(m + 7, a1, 7);
288 memcpy(m + 14, io_cap, 3);
289 memcpy(m + 17, r, 16);
290 memcpy(m + 33, n2, 16);
291 memcpy(m + 49, n1, 16);
292
293 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
294 if (err)
295 return err;
296
297 BT_DBG("res %16phN", res);
298
299 return err;
300}
301
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300302static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
303 const u8 x[16], const u8 y[16], u32 *val)
304{
305 u8 m[80], tmp[16];
306 int err;
307
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200308 SMP_DBG("u %32phN", u);
309 SMP_DBG("v %32phN", v);
310 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300311
312 memcpy(m, y, 16);
313 memcpy(m + 16, v, 32);
314 memcpy(m + 48, u, 32);
315
316 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
317 if (err)
318 return err;
319
320 *val = get_unaligned_le32(tmp);
321 *val %= 1000000;
322
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200323 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300324
325 return 0;
326}
327
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300328static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
329{
330 struct blkcipher_desc desc;
331 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200332 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200333 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300334
335 if (tfm == NULL) {
336 BT_ERR("tfm %p", tfm);
337 return -EINVAL;
338 }
339
340 desc.tfm = tfm;
341 desc.flags = 0;
342
Johan Hedberg943a7322014-03-18 12:58:24 +0200343 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300344 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200345
346 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300347 if (err) {
348 BT_ERR("cipher setkey failed: %d", err);
349 return err;
350 }
351
Johan Hedberg943a7322014-03-18 12:58:24 +0200352 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300353 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200354
355 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300356
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300357 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
358 if (err)
359 BT_ERR("Encrypt data error %d", err);
360
Johan Hedberg943a7322014-03-18 12:58:24 +0200361 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300362 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200363
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300364 return err;
365}
366
Johan Hedberg6a770832014-06-06 11:54:04 +0300367static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
368 const u8 key_id[4], u8 res[16])
369{
370 int err;
371
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200372 SMP_DBG("w %16phN key_id %4phN", w, key_id);
Johan Hedberg6a770832014-06-06 11:54:04 +0300373
374 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
375 if (err)
376 return err;
377
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200378 SMP_DBG("res %16phN", res);
Johan Hedberg6a770832014-06-06 11:54:04 +0300379
380 return err;
381}
382
Johan Hedberg60478052014-02-18 10:19:31 +0200383static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
384{
Johan Hedberg943a7322014-03-18 12:58:24 +0200385 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200386 int err;
387
388 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200389 memcpy(_res, r, 3);
390 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200391
Johan Hedberg943a7322014-03-18 12:58:24 +0200392 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200393 if (err) {
394 BT_ERR("Encrypt error");
395 return err;
396 }
397
398 /* The output of the random address function ah is:
399 * ah(h, r) = e(k, r') mod 2^24
400 * The output of the security function e is then truncated to 24 bits
401 * by taking the least significant 24 bits of the output of e as the
402 * result of ah.
403 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200404 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200405
406 return 0;
407}
408
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300409bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200410{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300411 struct l2cap_chan *chan = hdev->smp_data;
412 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200413 u8 hash[3];
414 int err;
415
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300416 if (!chan || !chan->data)
417 return false;
418
419 tfm = chan->data;
420
Johan Hedberg60478052014-02-18 10:19:31 +0200421 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
422
423 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
424 if (err)
425 return false;
426
427 return !memcmp(bdaddr->b, hash, 3);
428}
429
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300430int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200431{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300432 struct l2cap_chan *chan = hdev->smp_data;
433 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200434 int err;
435
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300436 if (!chan || !chan->data)
437 return -EOPNOTSUPP;
438
439 tfm = chan->data;
440
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200441 get_random_bytes(&rpa->b[3], 3);
442
443 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
444 rpa->b[5] |= 0x40; /* Set second most significant bit */
445
446 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
447 if (err < 0)
448 return err;
449
450 BT_DBG("RPA %pMR", rpa);
451
452 return 0;
453}
454
Johan Hedberge491eaf2014-10-25 21:15:37 +0200455static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
456 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
457 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300458{
459 u8 p1[16], p2[16];
460 int err;
461
462 memset(p1, 0, 16);
463
464 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200465 p1[0] = _iat;
466 p1[1] = _rat;
467 memcpy(p1 + 2, preq, 7);
468 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300469
470 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200471 memcpy(p2, ra, 6);
472 memcpy(p2 + 6, ia, 6);
473 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300474
475 /* res = r XOR p1 */
476 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
477
478 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200479 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300480 if (err) {
481 BT_ERR("Encrypt data error");
482 return err;
483 }
484
485 /* res = res XOR p2 */
486 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
487
488 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200489 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300490 if (err)
491 BT_ERR("Encrypt data error");
492
493 return err;
494}
495
Johan Hedberge491eaf2014-10-25 21:15:37 +0200496static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
497 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300498{
499 int err;
500
501 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200502 memcpy(_r, r2, 8);
503 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300504
Johan Hedberge491eaf2014-10-25 21:15:37 +0200505 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300506 if (err)
507 BT_ERR("Encrypt data error");
508
509 return err;
510}
511
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300512static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
513{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300514 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300515 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300516 struct kvec iv[2];
517 struct msghdr msg;
518
519 if (!chan)
520 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300521
522 BT_DBG("code 0x%2.2x", code);
523
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300524 iv[0].iov_base = &code;
525 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300526
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300527 iv[1].iov_base = data;
528 iv[1].iov_len = len;
529
530 memset(&msg, 0, sizeof(msg));
531
532 msg.msg_iov = (struct iovec *) &iv;
533 msg.msg_iovlen = 2;
534
535 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300536
Johan Hedbergb68fda62014-08-11 22:06:40 +0300537 if (!chan->data)
538 return;
539
540 smp = chan->data;
541
542 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300543 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300544}
545
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300546static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800547{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300548 if (authreq & SMP_AUTH_MITM) {
549 if (authreq & SMP_AUTH_SC)
550 return BT_SECURITY_FIPS;
551 else
552 return BT_SECURITY_HIGH;
553 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800554 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300555 }
Brian Gix2b64d152011-12-21 16:12:12 -0800556}
557
558static __u8 seclevel_to_authreq(__u8 sec_level)
559{
560 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300561 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800562 case BT_SECURITY_HIGH:
563 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
564 case BT_SECURITY_MEDIUM:
565 return SMP_AUTH_BONDING;
566 default:
567 return SMP_AUTH_NONE;
568 }
569}
570
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300571static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700572 struct smp_cmd_pairing *req,
573 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300574{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300575 struct l2cap_chan *chan = conn->smp;
576 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200577 struct hci_conn *hcon = conn->hcon;
578 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100579 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300580
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300581 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700582 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
583 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300584 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800585 } else {
586 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300587 }
588
Johan Hedbergfd349c02014-02-18 10:19:36 +0200589 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
590 remote_dist |= SMP_DIST_ID_KEY;
591
Johan Hedberg863efaf2014-02-22 19:06:32 +0200592 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
593 local_dist |= SMP_DIST_ID_KEY;
594
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100595 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
596 (authreq & SMP_AUTH_SC)) {
597 struct oob_data *oob_data;
598 u8 bdaddr_type;
599
600 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300601 local_dist |= SMP_DIST_LINK_KEY;
602 remote_dist |= SMP_DIST_LINK_KEY;
603 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100604
605 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
606 bdaddr_type = BDADDR_LE_PUBLIC;
607 else
608 bdaddr_type = BDADDR_LE_RANDOM;
609
610 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
611 bdaddr_type);
612 if (oob_data) {
613 set_bit(SMP_FLAG_OOB, &smp->flags);
614 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100615 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100616 memcpy(smp->pcnf, oob_data->hash256, 16);
617 }
618
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300619 } else {
620 authreq &= ~SMP_AUTH_SC;
621 }
622
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300623 if (rsp == NULL) {
624 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100625 req->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300626 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200627 req->init_key_dist = local_dist;
628 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300629 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200630
631 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300632 return;
633 }
634
635 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100636 rsp->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300637 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200638 rsp->init_key_dist = req->init_key_dist & remote_dist;
639 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300640 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200641
642 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300643}
644
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300645static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
646{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300647 struct l2cap_chan *chan = conn->smp;
648 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300649
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300650 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700651 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300652 return SMP_ENC_KEY_SIZE;
653
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300654 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300655
656 return 0;
657}
658
Johan Hedberg6f48e262014-08-11 22:06:44 +0300659static void smp_chan_destroy(struct l2cap_conn *conn)
660{
661 struct l2cap_chan *chan = conn->smp;
662 struct smp_chan *smp = chan->data;
663 bool complete;
664
665 BUG_ON(!smp);
666
667 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300668
Johan Hedberg6f48e262014-08-11 22:06:44 +0300669 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
670 mgmt_smp_complete(conn->hcon, complete);
671
672 kfree(smp->csrk);
673 kfree(smp->slave_csrk);
Johan Hedberg6a770832014-06-06 11:54:04 +0300674 kfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300675
676 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300677 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300678
679 /* If pairing failed clean up any keys we might have */
680 if (!complete) {
681 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200682 list_del_rcu(&smp->ltk->list);
683 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300684 }
685
686 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200687 list_del_rcu(&smp->slave_ltk->list);
688 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300689 }
690
691 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200692 list_del_rcu(&smp->remote_irk->list);
693 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300694 }
695 }
696
697 chan->data = NULL;
698 kfree(smp);
699 hci_conn_drop(conn->hcon);
700}
701
Johan Hedberg84794e12013-11-06 11:24:57 +0200702static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800703{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200704 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300705 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200706
Johan Hedberg84794e12013-11-06 11:24:57 +0200707 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800708 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700709 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800710
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700711 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700712 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300713
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300714 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300715 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800716}
717
Brian Gix2b64d152011-12-21 16:12:12 -0800718#define JUST_WORKS 0x00
719#define JUST_CFM 0x01
720#define REQ_PASSKEY 0x02
721#define CFM_PASSKEY 0x03
722#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300723#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800724#define OVERLAP 0xFF
725
726static const u8 gen_method[5][5] = {
727 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
728 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
729 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
730 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
731 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
732};
733
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300734static const u8 sc_method[5][5] = {
735 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
736 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
737 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
738 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
739 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
740};
741
Johan Hedberg581370c2014-06-17 13:07:38 +0300742static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
743{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300744 /* If either side has unknown io_caps, use JUST_CFM (which gets
745 * converted later to JUST_WORKS if we're initiators.
746 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300747 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
748 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300749 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300750
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300751 if (test_bit(SMP_FLAG_SC, &smp->flags))
752 return sc_method[remote_io][local_io];
753
Johan Hedberg581370c2014-06-17 13:07:38 +0300754 return gen_method[remote_io][local_io];
755}
756
Brian Gix2b64d152011-12-21 16:12:12 -0800757static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
758 u8 local_io, u8 remote_io)
759{
760 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300761 struct l2cap_chan *chan = conn->smp;
762 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800763 u32 passkey = 0;
764 int ret = 0;
765
766 /* Initialize key for JUST WORKS */
767 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300768 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800769
770 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
771
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300772 /* If neither side wants MITM, either "just" confirm an incoming
773 * request or use just-works for outgoing ones. The JUST_CFM
774 * will be converted to JUST_WORKS if necessary later in this
775 * function. If either side has MITM look up the method from the
776 * table.
777 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300778 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300779 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800780 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300781 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800782
Johan Hedberga82505c2014-03-24 14:39:07 +0200783 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300784 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
785 &smp->flags))
786 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200787
Johan Hedberg02f3e252014-07-16 15:09:13 +0300788 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300789 if (smp->method == JUST_CFM &&
790 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
791 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300792
Brian Gix2b64d152011-12-21 16:12:12 -0800793 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300794 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300795 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800796 return 0;
797 }
798
799 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300800 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300801 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300802 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
803 hcon->pending_sec_level = BT_SECURITY_HIGH;
804 }
Brian Gix2b64d152011-12-21 16:12:12 -0800805
806 /* If both devices have Keyoard-Display I/O, the master
807 * Confirms and the slave Enters the passkey.
808 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300809 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300810 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300811 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800812 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300813 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800814 }
815
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200816 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300817 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200818 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800819 get_random_bytes(&passkey, sizeof(passkey));
820 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200821 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800822 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300823 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800824 }
825
Johan Hedberg783e0572014-05-31 18:48:26 +0300826 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700827 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200828 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300829 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200830 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
831 hcon->type, hcon->dst_type,
832 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800833 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200834 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200835 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200836 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800837
Brian Gix2b64d152011-12-21 16:12:12 -0800838 return ret;
839}
840
Johan Hedberg1cc61142014-05-20 09:45:52 +0300841static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300842{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300843 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300844 struct smp_cmd_pairing_confirm cp;
845 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300846
847 BT_DBG("conn %p", conn);
848
Johan Hedberge491eaf2014-10-25 21:15:37 +0200849 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200850 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200851 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
852 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300853 if (ret)
854 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300855
Johan Hedberg4a74d652014-05-20 09:45:50 +0300856 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800857
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300858 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
859
Johan Hedbergb28b4942014-09-05 22:19:55 +0300860 if (conn->hcon->out)
861 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
862 else
863 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
864
Johan Hedberg1cc61142014-05-20 09:45:52 +0300865 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300866}
867
Johan Hedberg861580a2014-05-20 09:45:51 +0300868static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300869{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300870 struct l2cap_conn *conn = smp->conn;
871 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300872 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300873 int ret;
874
Johan Hedbergec70f362014-06-27 14:23:04 +0300875 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300876 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300877
878 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
879
Johan Hedberge491eaf2014-10-25 21:15:37 +0200880 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200881 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200882 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300883 if (ret)
884 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300885
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300886 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
887 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300888 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300889 }
890
891 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800892 u8 stk[16];
893 __le64 rand = 0;
894 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300895
Johan Hedberge491eaf2014-10-25 21:15:37 +0200896 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300897
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300898 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300899 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300900
Johan Hedberg861580a2014-05-20 09:45:51 +0300901 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
902 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300903
904 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300905 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300906 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300907 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300908 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800909 __le64 rand = 0;
910 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300911
Johan Hedberg943a7322014-03-18 12:58:24 +0200912 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
913 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300914
Johan Hedberge491eaf2014-10-25 21:15:37 +0200915 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300916
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300917 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700918 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300919
Johan Hedbergfff34902014-06-10 15:19:50 +0300920 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
921 auth = 1;
922 else
923 auth = 0;
924
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300925 /* Even though there's no _SLAVE suffix this is the
926 * slave STK we're adding for later lookup (the master
927 * STK never needs to be stored).
928 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700929 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300930 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300931 }
932
Johan Hedberg861580a2014-05-20 09:45:51 +0300933 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300934}
935
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300936static void smp_notify_keys(struct l2cap_conn *conn)
937{
938 struct l2cap_chan *chan = conn->smp;
939 struct smp_chan *smp = chan->data;
940 struct hci_conn *hcon = conn->hcon;
941 struct hci_dev *hdev = hcon->hdev;
942 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
943 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
944 bool persistent;
945
946 if (smp->remote_irk) {
947 mgmt_new_irk(hdev, smp->remote_irk);
948 /* Now that user space can be considered to know the
949 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300950 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300951 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300952 if (hcon->type == LE_LINK) {
953 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
954 hcon->dst_type = smp->remote_irk->addr_type;
955 queue_work(hdev->workqueue, &conn->id_addr_update_work);
956 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300957
958 /* When receiving an indentity resolving key for
959 * a remote device that does not use a resolvable
960 * private address, just remove the key so that
961 * it is possible to use the controller white
962 * list for scanning.
963 *
964 * Userspace will have been told to not store
965 * this key at this point. So it is safe to
966 * just remove it.
967 */
968 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200969 list_del_rcu(&smp->remote_irk->list);
970 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300971 smp->remote_irk = NULL;
972 }
973 }
974
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300975 if (hcon->type == ACL_LINK) {
976 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
977 persistent = false;
978 else
979 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
980 &hcon->flags);
981 } else {
982 /* The LTKs and CSRKs should be persistent only if both sides
983 * had the bonding bit set in their authentication requests.
984 */
985 persistent = !!((req->auth_req & rsp->auth_req) &
986 SMP_AUTH_BONDING);
987 }
988
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300989
990 if (smp->csrk) {
991 smp->csrk->bdaddr_type = hcon->dst_type;
992 bacpy(&smp->csrk->bdaddr, &hcon->dst);
993 mgmt_new_csrk(hdev, smp->csrk, persistent);
994 }
995
996 if (smp->slave_csrk) {
997 smp->slave_csrk->bdaddr_type = hcon->dst_type;
998 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
999 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1000 }
1001
1002 if (smp->ltk) {
1003 smp->ltk->bdaddr_type = hcon->dst_type;
1004 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1005 mgmt_new_ltk(hdev, smp->ltk, persistent);
1006 }
1007
1008 if (smp->slave_ltk) {
1009 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1010 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1011 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1012 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001013
1014 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001015 struct link_key *key;
1016 u8 type;
1017
1018 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1019 type = HCI_LK_DEBUG_COMBINATION;
1020 else if (hcon->sec_level == BT_SECURITY_FIPS)
1021 type = HCI_LK_AUTH_COMBINATION_P256;
1022 else
1023 type = HCI_LK_UNAUTH_COMBINATION_P256;
1024
1025 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1026 smp->link_key, type, 0, &persistent);
1027 if (key) {
1028 mgmt_new_link_key(hdev, key, persistent);
1029
1030 /* Don't keep debug keys around if the relevant
1031 * flag is not set.
1032 */
1033 if (!test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags) &&
1034 key->type == HCI_LK_DEBUG_COMBINATION) {
1035 list_del_rcu(&key->list);
1036 kfree_rcu(key, rcu);
1037 }
1038 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001039 }
1040}
1041
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001042static void sc_add_ltk(struct smp_chan *smp)
1043{
1044 struct hci_conn *hcon = smp->conn->hcon;
1045 u8 key_type, auth;
1046
1047 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1048 key_type = SMP_LTK_P256_DEBUG;
1049 else
1050 key_type = SMP_LTK_P256;
1051
1052 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1053 auth = 1;
1054 else
1055 auth = 0;
1056
1057 memset(smp->tk + smp->enc_key_size, 0,
1058 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
1059
1060 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1061 key_type, auth, smp->tk, smp->enc_key_size,
1062 0, 0);
1063}
1064
Johan Hedberg6a770832014-06-06 11:54:04 +03001065static void sc_generate_link_key(struct smp_chan *smp)
1066{
1067 /* These constants are as specified in the core specification.
1068 * In ASCII they spell out to 'tmp1' and 'lebr'.
1069 */
1070 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1071 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1072
1073 smp->link_key = kzalloc(16, GFP_KERNEL);
1074 if (!smp->link_key)
1075 return;
1076
1077 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1078 kfree(smp->link_key);
1079 smp->link_key = NULL;
1080 return;
1081 }
1082
1083 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
1084 kfree(smp->link_key);
1085 smp->link_key = NULL;
1086 return;
1087 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001088}
1089
Johan Hedbergb28b4942014-09-05 22:19:55 +03001090static void smp_allow_key_dist(struct smp_chan *smp)
1091{
1092 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1093 * will be allowed in each PDU handler to ensure we receive
1094 * them in the correct order.
1095 */
1096 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1097 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1098 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1099 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1100 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1101 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1102}
1103
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001104static void sc_generate_ltk(struct smp_chan *smp)
1105{
1106 /* These constants are as specified in the core specification.
1107 * In ASCII they spell out to 'tmp2' and 'brle'.
1108 */
1109 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1110 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1111 struct hci_conn *hcon = smp->conn->hcon;
1112 struct hci_dev *hdev = hcon->hdev;
1113 struct link_key *key;
1114
1115 key = hci_find_link_key(hdev, &hcon->dst);
1116 if (!key) {
1117 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1118 return;
1119 }
1120
1121 if (key->type == HCI_LK_DEBUG_COMBINATION)
1122 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1123
1124 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1125 return;
1126
1127 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1128 return;
1129
1130 sc_add_ltk(smp);
1131}
1132
Johan Hedbergd6268e82014-09-05 22:19:51 +03001133static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001134{
1135 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001136 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001137 struct hci_conn *hcon = conn->hcon;
1138 struct hci_dev *hdev = hcon->hdev;
1139 __u8 *keydist;
1140
1141 BT_DBG("conn %p", conn);
1142
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001143 rsp = (void *) &smp->prsp[1];
1144
1145 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001146 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1147 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001148 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001149 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001150
1151 req = (void *) &smp->preq[1];
1152
1153 if (hcon->out) {
1154 keydist = &rsp->init_key_dist;
1155 *keydist &= req->init_key_dist;
1156 } else {
1157 keydist = &rsp->resp_key_dist;
1158 *keydist &= req->resp_key_dist;
1159 }
1160
Johan Hedberg6a770832014-06-06 11:54:04 +03001161 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001162 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001163 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001164 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1165 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001166
1167 /* Clear the keys which are generated but not distributed */
1168 *keydist &= ~SMP_SC_NO_DIST;
1169 }
1170
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001171 BT_DBG("keydist 0x%x", *keydist);
1172
1173 if (*keydist & SMP_DIST_ENC_KEY) {
1174 struct smp_cmd_encrypt_info enc;
1175 struct smp_cmd_master_ident ident;
1176 struct smp_ltk *ltk;
1177 u8 authenticated;
1178 __le16 ediv;
1179 __le64 rand;
1180
1181 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1182 get_random_bytes(&ediv, sizeof(ediv));
1183 get_random_bytes(&rand, sizeof(rand));
1184
1185 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1186
1187 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1188 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1189 SMP_LTK_SLAVE, authenticated, enc.ltk,
1190 smp->enc_key_size, ediv, rand);
1191 smp->slave_ltk = ltk;
1192
1193 ident.ediv = ediv;
1194 ident.rand = rand;
1195
1196 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1197
1198 *keydist &= ~SMP_DIST_ENC_KEY;
1199 }
1200
1201 if (*keydist & SMP_DIST_ID_KEY) {
1202 struct smp_cmd_ident_addr_info addrinfo;
1203 struct smp_cmd_ident_info idinfo;
1204
1205 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1206
1207 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1208
1209 /* The hci_conn contains the local identity address
1210 * after the connection has been established.
1211 *
1212 * This is true even when the connection has been
1213 * established using a resolvable random address.
1214 */
1215 bacpy(&addrinfo.bdaddr, &hcon->src);
1216 addrinfo.addr_type = hcon->src_type;
1217
1218 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1219 &addrinfo);
1220
1221 *keydist &= ~SMP_DIST_ID_KEY;
1222 }
1223
1224 if (*keydist & SMP_DIST_SIGN) {
1225 struct smp_cmd_sign_info sign;
1226 struct smp_csrk *csrk;
1227
1228 /* Generate a new random key */
1229 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1230
1231 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1232 if (csrk) {
1233 csrk->master = 0x00;
1234 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1235 }
1236 smp->slave_csrk = csrk;
1237
1238 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1239
1240 *keydist &= ~SMP_DIST_SIGN;
1241 }
1242
1243 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001244 if (smp->remote_key_dist & KEY_DIST_MASK) {
1245 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001246 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001247 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001248
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001249 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1250 smp_notify_keys(conn);
1251
1252 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001253}
1254
Johan Hedbergb68fda62014-08-11 22:06:40 +03001255static void smp_timeout(struct work_struct *work)
1256{
1257 struct smp_chan *smp = container_of(work, struct smp_chan,
1258 security_timer.work);
1259 struct l2cap_conn *conn = smp->conn;
1260
1261 BT_DBG("conn %p", conn);
1262
Johan Hedberg1e91c292014-08-18 20:33:29 +03001263 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001264}
1265
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001266static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1267{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001268 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001269 struct smp_chan *smp;
1270
Marcel Holtmannf1560462013-10-13 05:43:25 -07001271 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001272 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001273 return NULL;
1274
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001275 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1276 if (IS_ERR(smp->tfm_aes)) {
1277 BT_ERR("Unable to create ECB crypto context");
1278 kfree(smp);
1279 return NULL;
1280 }
1281
Johan Hedberg407cecf2014-05-02 14:19:47 +03001282 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1283 if (IS_ERR(smp->tfm_cmac)) {
1284 BT_ERR("Unable to create CMAC crypto context");
1285 crypto_free_blkcipher(smp->tfm_aes);
1286 kfree(smp);
1287 return NULL;
1288 }
1289
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001290 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001291 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001292
Johan Hedbergb28b4942014-09-05 22:19:55 +03001293 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1294
Johan Hedbergb68fda62014-08-11 22:06:40 +03001295 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1296
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001297 hci_conn_hold(conn->hcon);
1298
1299 return smp;
1300}
1301
Johan Hedberg760b0182014-06-06 11:44:05 +03001302static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1303{
1304 struct hci_conn *hcon = smp->conn->hcon;
1305 u8 *na, *nb, a[7], b[7];
1306
1307 if (hcon->out) {
1308 na = smp->prnd;
1309 nb = smp->rrnd;
1310 } else {
1311 na = smp->rrnd;
1312 nb = smp->prnd;
1313 }
1314
1315 memcpy(a, &hcon->init_addr, 6);
1316 memcpy(b, &hcon->resp_addr, 6);
1317 a[6] = hcon->init_addr_type;
1318 b[6] = hcon->resp_addr_type;
1319
1320 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1321}
1322
Johan Hedberg38606f12014-06-25 11:10:28 +03001323static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001324{
1325 struct hci_conn *hcon = smp->conn->hcon;
1326 struct smp_cmd_dhkey_check check;
1327 u8 a[7], b[7], *local_addr, *remote_addr;
1328 u8 io_cap[3], r[16];
1329
Johan Hedberg760b0182014-06-06 11:44:05 +03001330 memcpy(a, &hcon->init_addr, 6);
1331 memcpy(b, &hcon->resp_addr, 6);
1332 a[6] = hcon->init_addr_type;
1333 b[6] = hcon->resp_addr_type;
1334
1335 if (hcon->out) {
1336 local_addr = a;
1337 remote_addr = b;
1338 memcpy(io_cap, &smp->preq[1], 3);
1339 } else {
1340 local_addr = b;
1341 remote_addr = a;
1342 memcpy(io_cap, &smp->prsp[1], 3);
1343 }
1344
Johan Hedbergdddd3052014-06-01 15:38:09 +03001345 memset(r, 0, sizeof(r));
1346
1347 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001348 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001349
Johan Hedberga29b0732014-10-28 15:17:05 +01001350 if (smp->method == REQ_OOB)
1351 memcpy(r, smp->rr, 16);
1352
Johan Hedberg760b0182014-06-06 11:44:05 +03001353 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1354 local_addr, remote_addr, check.e);
1355
1356 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001357}
1358
Johan Hedberg38606f12014-06-25 11:10:28 +03001359static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1360{
1361 struct l2cap_conn *conn = smp->conn;
1362 struct hci_conn *hcon = conn->hcon;
1363 struct smp_cmd_pairing_confirm cfm;
1364 u8 r;
1365
1366 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1367 r |= 0x80;
1368
1369 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1370
1371 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1372 cfm.confirm_val))
1373 return SMP_UNSPECIFIED;
1374
1375 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1376
1377 return 0;
1378}
1379
1380static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1381{
1382 struct l2cap_conn *conn = smp->conn;
1383 struct hci_conn *hcon = conn->hcon;
1384 struct hci_dev *hdev = hcon->hdev;
1385 u8 cfm[16], r;
1386
1387 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1388 if (smp->passkey_round >= 20)
1389 return 0;
1390
1391 switch (smp_op) {
1392 case SMP_CMD_PAIRING_RANDOM:
1393 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1394 r |= 0x80;
1395
1396 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1397 smp->rrnd, r, cfm))
1398 return SMP_UNSPECIFIED;
1399
1400 if (memcmp(smp->pcnf, cfm, 16))
1401 return SMP_CONFIRM_FAILED;
1402
1403 smp->passkey_round++;
1404
1405 if (smp->passkey_round == 20) {
1406 /* Generate MacKey and LTK */
1407 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1408 return SMP_UNSPECIFIED;
1409 }
1410
1411 /* The round is only complete when the initiator
1412 * receives pairing random.
1413 */
1414 if (!hcon->out) {
1415 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1416 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001417 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001418 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001419 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001420 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001421 return 0;
1422 }
1423
1424 /* Start the next round */
1425 if (smp->passkey_round != 20)
1426 return sc_passkey_round(smp, 0);
1427
1428 /* Passkey rounds are complete - start DHKey Check */
1429 sc_dhkey_check(smp);
1430 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1431
1432 break;
1433
1434 case SMP_CMD_PAIRING_CONFIRM:
1435 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1436 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1437 return 0;
1438 }
1439
1440 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1441
1442 if (hcon->out) {
1443 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1444 sizeof(smp->prnd), smp->prnd);
1445 return 0;
1446 }
1447
1448 return sc_passkey_send_confirm(smp);
1449
1450 case SMP_CMD_PUBLIC_KEY:
1451 default:
1452 /* Initiating device starts the round */
1453 if (!hcon->out)
1454 return 0;
1455
1456 BT_DBG("%s Starting passkey round %u", hdev->name,
1457 smp->passkey_round + 1);
1458
1459 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1460
1461 return sc_passkey_send_confirm(smp);
1462 }
1463
1464 return 0;
1465}
1466
Johan Hedbergdddd3052014-06-01 15:38:09 +03001467static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1468{
Johan Hedberg38606f12014-06-25 11:10:28 +03001469 struct l2cap_conn *conn = smp->conn;
1470 struct hci_conn *hcon = conn->hcon;
1471 u8 smp_op;
1472
1473 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1474
Johan Hedbergdddd3052014-06-01 15:38:09 +03001475 switch (mgmt_op) {
1476 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1477 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1478 return 0;
1479 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1480 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1481 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001482 case MGMT_OP_USER_PASSKEY_REPLY:
1483 hcon->passkey_notify = le32_to_cpu(passkey);
1484 smp->passkey_round = 0;
1485
1486 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1487 smp_op = SMP_CMD_PAIRING_CONFIRM;
1488 else
1489 smp_op = 0;
1490
1491 if (sc_passkey_round(smp, smp_op))
1492 return -EIO;
1493
1494 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001495 }
1496
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001497 /* Initiator sends DHKey check first */
1498 if (hcon->out) {
1499 sc_dhkey_check(smp);
1500 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1501 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1502 sc_dhkey_check(smp);
1503 sc_add_ltk(smp);
1504 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001505
1506 return 0;
1507}
1508
Brian Gix2b64d152011-12-21 16:12:12 -08001509int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1510{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001511 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001512 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001513 struct smp_chan *smp;
1514 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001515 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001516
1517 BT_DBG("");
1518
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001519 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001520 return -ENOTCONN;
1521
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001522 chan = conn->smp;
1523 if (!chan)
1524 return -ENOTCONN;
1525
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001526 l2cap_chan_lock(chan);
1527 if (!chan->data) {
1528 err = -ENOTCONN;
1529 goto unlock;
1530 }
1531
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001532 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001533
Johan Hedberg760b0182014-06-06 11:44:05 +03001534 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1535 err = sc_user_reply(smp, mgmt_op, passkey);
1536 goto unlock;
1537 }
1538
Brian Gix2b64d152011-12-21 16:12:12 -08001539 switch (mgmt_op) {
1540 case MGMT_OP_USER_PASSKEY_REPLY:
1541 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001542 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001543 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001544 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001545 /* Fall Through */
1546 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001547 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001548 break;
1549 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1550 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001551 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001552 err = 0;
1553 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001554 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001555 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001556 err = -EOPNOTSUPP;
1557 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001558 }
1559
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001560 err = 0;
1561
Brian Gix2b64d152011-12-21 16:12:12 -08001562 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001563 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1564 u8 rsp = smp_confirm(smp);
1565 if (rsp)
1566 smp_failure(conn, rsp);
1567 }
Brian Gix2b64d152011-12-21 16:12:12 -08001568
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001569unlock:
1570 l2cap_chan_unlock(chan);
1571 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001572}
1573
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001574static void build_bredr_pairing_cmd(struct smp_chan *smp,
1575 struct smp_cmd_pairing *req,
1576 struct smp_cmd_pairing *rsp)
1577{
1578 struct l2cap_conn *conn = smp->conn;
1579 struct hci_dev *hdev = conn->hcon->hdev;
1580 u8 local_dist = 0, remote_dist = 0;
1581
1582 if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) {
1583 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1584 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1585 }
1586
1587 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
1588 remote_dist |= SMP_DIST_ID_KEY;
1589
1590 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
1591 local_dist |= SMP_DIST_ID_KEY;
1592
1593 if (!rsp) {
1594 memset(req, 0, sizeof(*req));
1595
1596 req->init_key_dist = local_dist;
1597 req->resp_key_dist = remote_dist;
1598 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1599
1600 smp->remote_key_dist = remote_dist;
1601
1602 return;
1603 }
1604
1605 memset(rsp, 0, sizeof(*rsp));
1606
1607 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1608 rsp->init_key_dist = req->init_key_dist & remote_dist;
1609 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1610
1611 smp->remote_key_dist = rsp->init_key_dist;
1612}
1613
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001614static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001615{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001616 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001617 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001618 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001619 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001620 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001621 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001622
1623 BT_DBG("conn %p", conn);
1624
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001625 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001626 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001627
Johan Hedberg40bef302014-07-16 11:42:27 +03001628 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001629 return SMP_CMD_NOTSUPP;
1630
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001631 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001632 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001633 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001634 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001635
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001636 if (!smp)
1637 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001638
Johan Hedbergc05b9332014-09-10 17:37:42 -07001639 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001640 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001641
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001642 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001643 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001644 return SMP_PAIRING_NOTSUPP;
1645
Johan Hedberg903b71c2014-09-08 16:59:18 -07001646 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1647 return SMP_AUTH_REQUIREMENTS;
1648
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001649 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1650 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001651 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001652
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001653 /* SMP over BR/EDR requires special treatment */
1654 if (conn->hcon->type == ACL_LINK) {
1655 /* We must have a BR/EDR SC link */
1656 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags))
1657 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1658
1659 set_bit(SMP_FLAG_SC, &smp->flags);
1660
1661 build_bredr_pairing_cmd(smp, req, &rsp);
1662
1663 key_size = min(req->max_key_size, rsp.max_key_size);
1664 if (check_enc_key_size(conn, key_size))
1665 return SMP_ENC_KEY_SIZE;
1666
1667 /* Clear bits which are generated but not distributed */
1668 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1669
1670 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1671 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1672 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1673
1674 smp_distribute_keys(smp);
1675 return 0;
1676 }
1677
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001678 build_pairing_cmd(conn, req, &rsp, auth);
1679
1680 if (rsp.auth_req & SMP_AUTH_SC)
1681 set_bit(SMP_FLAG_SC, &smp->flags);
1682
Johan Hedberg5be5e272014-09-10 17:58:54 -07001683 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001684 sec_level = BT_SECURITY_MEDIUM;
1685 else
1686 sec_level = authreq_to_seclevel(auth);
1687
Johan Hedbergc7262e72014-06-17 13:07:37 +03001688 if (sec_level > conn->hcon->pending_sec_level)
1689 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001690
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001691 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001692 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1693 u8 method;
1694
1695 method = get_auth_method(smp, conn->hcon->io_capability,
1696 req->io_capability);
1697 if (method == JUST_WORKS || method == JUST_CFM)
1698 return SMP_AUTH_REQUIREMENTS;
1699 }
1700
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001701 key_size = min(req->max_key_size, rsp.max_key_size);
1702 if (check_enc_key_size(conn, key_size))
1703 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001704
Johan Hedberge84a6b12013-12-02 10:49:03 +02001705 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001706
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001707 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1708 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001709
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001710 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001711
1712 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1713
1714 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1715 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1716 /* Clear bits which are generated but not distributed */
1717 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1718 /* Wait for Public Key from Initiating Device */
1719 return 0;
1720 } else {
1721 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1722 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001723
Brian Gix2b64d152011-12-21 16:12:12 -08001724 /* Request setup of TK */
1725 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1726 if (ret)
1727 return SMP_UNSPECIFIED;
1728
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001729 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001730}
1731
Johan Hedberg3b191462014-06-06 10:50:15 +03001732static u8 sc_send_public_key(struct smp_chan *smp)
1733{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001734 struct hci_dev *hdev = smp->conn->hcon->hdev;
1735
Johan Hedberg3b191462014-06-06 10:50:15 +03001736 BT_DBG("");
1737
Johan Hedberg70157ef2014-06-24 15:22:59 +03001738 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) {
1739 BT_DBG("Using debug keys");
1740 memcpy(smp->local_pk, debug_pk, 64);
1741 memcpy(smp->local_sk, debug_sk, 32);
1742 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1743 } else {
1744 while (true) {
1745 /* Generate local key pair for Secure Connections */
1746 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1747 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001748
Johan Hedberg70157ef2014-06-24 15:22:59 +03001749 /* This is unlikely, but we need to check that
1750 * we didn't accidentially generate a debug key.
1751 */
1752 if (memcmp(smp->local_sk, debug_sk, 32))
1753 break;
1754 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001755 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001756
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001757 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
1758 SMP_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1759 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001760
1761 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1762
1763 return 0;
1764}
1765
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001766static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001767{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001768 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001769 struct l2cap_chan *chan = conn->smp;
1770 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001771 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001772 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001773 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001774
1775 BT_DBG("conn %p", conn);
1776
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001777 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001778 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001779
Johan Hedberg40bef302014-07-16 11:42:27 +03001780 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001781 return SMP_CMD_NOTSUPP;
1782
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001783 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001784
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001785 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001786
1787 key_size = min(req->max_key_size, rsp->max_key_size);
1788 if (check_enc_key_size(conn, key_size))
1789 return SMP_ENC_KEY_SIZE;
1790
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001791 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001792
Johan Hedberg903b71c2014-09-08 16:59:18 -07001793 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1794 return SMP_AUTH_REQUIREMENTS;
1795
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001796 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1797 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1798
1799 /* Update remote key distribution in case the remote cleared
1800 * some bits that we had enabled in our request.
1801 */
1802 smp->remote_key_dist &= rsp->resp_key_dist;
1803
1804 /* For BR/EDR this means we're done and can start phase 3 */
1805 if (conn->hcon->type == ACL_LINK) {
1806 /* Clear bits which are generated but not distributed */
1807 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1808 smp_distribute_keys(smp);
1809 return 0;
1810 }
1811
Johan Hedberg65668772014-05-16 11:03:34 +03001812 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1813 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001814 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1815 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001816
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001817 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001818 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1819 u8 method;
1820
1821 method = get_auth_method(smp, req->io_capability,
1822 rsp->io_capability);
1823 if (method == JUST_WORKS || method == JUST_CFM)
1824 return SMP_AUTH_REQUIREMENTS;
1825 }
1826
Johan Hedberge84a6b12013-12-02 10:49:03 +02001827 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001828
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001829 /* Update remote key distribution in case the remote cleared
1830 * some bits that we had enabled in our request.
1831 */
1832 smp->remote_key_dist &= rsp->resp_key_dist;
1833
Johan Hedberg3b191462014-06-06 10:50:15 +03001834 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1835 /* Clear bits which are generated but not distributed */
1836 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1837 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1838 return sc_send_public_key(smp);
1839 }
1840
Johan Hedbergc05b9332014-09-10 17:37:42 -07001841 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001842
Johan Hedberg476585e2012-06-06 18:54:15 +08001843 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001844 if (ret)
1845 return SMP_UNSPECIFIED;
1846
Johan Hedberg4a74d652014-05-20 09:45:50 +03001847 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001848
1849 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001850 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001851 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001852
1853 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001854}
1855
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001856static u8 sc_check_confirm(struct smp_chan *smp)
1857{
1858 struct l2cap_conn *conn = smp->conn;
1859
1860 BT_DBG("");
1861
1862 /* Public Key exchange must happen before any other steps */
1863 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1864 return SMP_UNSPECIFIED;
1865
Johan Hedberg38606f12014-06-25 11:10:28 +03001866 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1867 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1868
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001869 if (conn->hcon->out) {
1870 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1871 smp->prnd);
1872 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1873 }
1874
1875 return 0;
1876}
1877
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001878static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001879{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001880 struct l2cap_chan *chan = conn->smp;
1881 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001882
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001883 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1884
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001885 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001886 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001887
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001888 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1889 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001890
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001891 if (test_bit(SMP_FLAG_SC, &smp->flags))
1892 return sc_check_confirm(smp);
1893
Johan Hedbergb28b4942014-09-05 22:19:55 +03001894 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001895 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1896 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001897 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1898 return 0;
1899 }
1900
1901 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001902 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001903 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001904 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001905
1906 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001907}
1908
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001909static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001910{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001911 struct l2cap_chan *chan = conn->smp;
1912 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001913 struct hci_conn *hcon = conn->hcon;
1914 u8 *pkax, *pkbx, *na, *nb;
1915 u32 passkey;
1916 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001917
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001918 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001919
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001920 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001921 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001922
Johan Hedberg943a7322014-03-18 12:58:24 +02001923 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001924 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001925
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001926 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1927 return smp_random(smp);
1928
Johan Hedberga29b0732014-10-28 15:17:05 +01001929 if (smp->method == REQ_OOB) {
1930 if (!hcon->out)
1931 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1932 sizeof(smp->prnd), smp->prnd);
1933 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1934 goto mackey_and_ltk;
1935 }
1936
Johan Hedberg38606f12014-06-25 11:10:28 +03001937 /* Passkey entry has special treatment */
1938 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1939 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
1940
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001941 if (hcon->out) {
1942 u8 cfm[16];
1943
1944 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1945 smp->rrnd, 0, cfm);
1946 if (err)
1947 return SMP_UNSPECIFIED;
1948
1949 if (memcmp(smp->pcnf, cfm, 16))
1950 return SMP_CONFIRM_FAILED;
1951
1952 pkax = smp->local_pk;
1953 pkbx = smp->remote_pk;
1954 na = smp->prnd;
1955 nb = smp->rrnd;
1956 } else {
1957 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1958 smp->prnd);
1959 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1960
1961 pkax = smp->remote_pk;
1962 pkbx = smp->local_pk;
1963 na = smp->rrnd;
1964 nb = smp->prnd;
1965 }
1966
Johan Hedberga29b0732014-10-28 15:17:05 +01001967mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03001968 /* Generate MacKey and LTK */
1969 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1970 if (err)
1971 return SMP_UNSPECIFIED;
1972
Johan Hedberga29b0732014-10-28 15:17:05 +01001973 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03001974 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03001975 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001976 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1977 }
1978 return 0;
1979 }
1980
Johan Hedberg38606f12014-06-25 11:10:28 +03001981 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001982 if (err)
1983 return SMP_UNSPECIFIED;
1984
Johan Hedberg38606f12014-06-25 11:10:28 +03001985 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
1986 hcon->dst_type, passkey, 0);
1987 if (err)
1988 return SMP_UNSPECIFIED;
1989
1990 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1991
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001992 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001993}
1994
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001995static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001996{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001997 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001998 struct hci_conn *hcon = conn->hcon;
1999
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002000 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002001 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002002 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002003
Johan Hedberga6f78332014-09-10 17:37:45 -07002004 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002005 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002006
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002007 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002008 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002009
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002010 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
2011 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002012
Johan Hedbergfe59a052014-07-01 19:14:12 +03002013 /* We never store STKs for master role, so clear this flag */
2014 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2015
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002016 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002017}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002018
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002019bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2020 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002021{
2022 if (sec_level == BT_SECURITY_LOW)
2023 return true;
2024
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002025 /* If we're encrypted with an STK but the caller prefers using
2026 * LTK claim insufficient security. This way we allow the
2027 * connection to be re-encrypted with an LTK, even if the LTK
2028 * provides the same level of security. Only exception is if we
2029 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002030 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002031 if (key_pref == SMP_USE_LTK &&
2032 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002033 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002034 return false;
2035
Johan Hedberg854f4722014-07-01 18:40:20 +03002036 if (hcon->sec_level >= sec_level)
2037 return true;
2038
2039 return false;
2040}
2041
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002042static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002043{
2044 struct smp_cmd_security_req *rp = (void *) skb->data;
2045 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002046 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002047 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002048 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002049 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002050
2051 BT_DBG("conn %p", conn);
2052
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002053 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002054 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002055
Johan Hedberg40bef302014-07-16 11:42:27 +03002056 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002057 return SMP_CMD_NOTSUPP;
2058
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002059 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002060
Johan Hedberg903b71c2014-09-08 16:59:18 -07002061 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
2062 return SMP_AUTH_REQUIREMENTS;
2063
Johan Hedberg5be5e272014-09-10 17:58:54 -07002064 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002065 sec_level = BT_SECURITY_MEDIUM;
2066 else
2067 sec_level = authreq_to_seclevel(auth);
2068
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002069 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002070 return 0;
2071
Johan Hedbergc7262e72014-06-17 13:07:37 +03002072 if (sec_level > hcon->pending_sec_level)
2073 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002074
Johan Hedberg4dab7862012-06-07 14:58:37 +08002075 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002076 return 0;
2077
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002078 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002079 if (!smp)
2080 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002081
Johan Hedbergb6ae8452014-07-30 09:22:22 +03002082 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002083 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002084 return SMP_PAIRING_NOTSUPP;
2085
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002086 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002087
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002088 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002089 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002090
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002091 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2092 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002093
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002094 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002095 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002096
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002097 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002098}
2099
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002100int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002101{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002102 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002103 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002104 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002105 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002106 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002107
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002108 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2109
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002110 /* This may be NULL if there's an unexpected disconnection */
2111 if (!conn)
2112 return 1;
2113
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002114 chan = conn->smp;
2115
Johan Hedberg757aee02013-04-24 13:05:32 +03002116 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002117 return 1;
2118
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002119 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002120 return 1;
2121
Johan Hedbergc7262e72014-06-17 13:07:37 +03002122 if (sec_level > hcon->pending_sec_level)
2123 hcon->pending_sec_level = sec_level;
2124
Johan Hedberg40bef302014-07-16 11:42:27 +03002125 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002126 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2127 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002128
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002129 l2cap_chan_lock(chan);
2130
2131 /* If SMP is already in progress ignore this request */
2132 if (chan->data) {
2133 ret = 0;
2134 goto unlock;
2135 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002136
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002137 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002138 if (!smp) {
2139 ret = 1;
2140 goto unlock;
2141 }
Brian Gix2b64d152011-12-21 16:12:12 -08002142
2143 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002144
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002145 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
2146 authreq |= SMP_AUTH_SC;
2147
Johan Hedberg79897d22014-06-01 09:45:24 +03002148 /* Require MITM if IO Capability allows or the security level
2149 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002150 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002151 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002152 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002153 authreq |= SMP_AUTH_MITM;
2154
Johan Hedberg40bef302014-07-16 11:42:27 +03002155 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002156 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002157
Brian Gix2b64d152011-12-21 16:12:12 -08002158 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002159 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2160 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002161
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002162 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002163 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002164 } else {
2165 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002166 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002167 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002168 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002169 }
2170
Johan Hedberg4a74d652014-05-20 09:45:50 +03002171 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002172 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002173
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002174unlock:
2175 l2cap_chan_unlock(chan);
2176 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002177}
2178
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002179static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2180{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002181 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002182 struct l2cap_chan *chan = conn->smp;
2183 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002184
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002185 BT_DBG("conn %p", conn);
2186
2187 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002188 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002189
Johan Hedbergb28b4942014-09-05 22:19:55 +03002190 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002191
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002192 skb_pull(skb, sizeof(*rp));
2193
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002194 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002195
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002196 return 0;
2197}
2198
2199static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2200{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002201 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002202 struct l2cap_chan *chan = conn->smp;
2203 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002204 struct hci_dev *hdev = conn->hcon->hdev;
2205 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002206 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002207 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002208
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002209 BT_DBG("conn %p", conn);
2210
2211 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002212 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002213
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002214 /* Mark the information as received */
2215 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2216
Johan Hedbergb28b4942014-09-05 22:19:55 +03002217 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2218 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002219 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2220 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002221
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002222 skb_pull(skb, sizeof(*rp));
2223
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002224 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002225 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002226 authenticated, smp->tk, smp->enc_key_size,
2227 rp->ediv, rp->rand);
2228 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002229 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002230 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002231
2232 return 0;
2233}
2234
Johan Hedbergfd349c02014-02-18 10:19:36 +02002235static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2236{
2237 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002238 struct l2cap_chan *chan = conn->smp;
2239 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002240
2241 BT_DBG("");
2242
2243 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002244 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002245
Johan Hedbergb28b4942014-09-05 22:19:55 +03002246 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002247
Johan Hedbergfd349c02014-02-18 10:19:36 +02002248 skb_pull(skb, sizeof(*info));
2249
2250 memcpy(smp->irk, info->irk, 16);
2251
2252 return 0;
2253}
2254
2255static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2256 struct sk_buff *skb)
2257{
2258 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002259 struct l2cap_chan *chan = conn->smp;
2260 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002261 struct hci_conn *hcon = conn->hcon;
2262 bdaddr_t rpa;
2263
2264 BT_DBG("");
2265
2266 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002267 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002268
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002269 /* Mark the information as received */
2270 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2271
Johan Hedbergb28b4942014-09-05 22:19:55 +03002272 if (smp->remote_key_dist & SMP_DIST_SIGN)
2273 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2274
Johan Hedbergfd349c02014-02-18 10:19:36 +02002275 skb_pull(skb, sizeof(*info));
2276
Johan Hedberga9a58f82014-02-25 22:24:37 +02002277 /* Strictly speaking the Core Specification (4.1) allows sending
2278 * an empty address which would force us to rely on just the IRK
2279 * as "identity information". However, since such
2280 * implementations are not known of and in order to not over
2281 * complicate our implementation, simply pretend that we never
2282 * received an IRK for such a device.
2283 */
2284 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
2285 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002286 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002287 }
2288
Johan Hedbergfd349c02014-02-18 10:19:36 +02002289 bacpy(&smp->id_addr, &info->bdaddr);
2290 smp->id_addr_type = info->addr_type;
2291
2292 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2293 bacpy(&rpa, &hcon->dst);
2294 else
2295 bacpy(&rpa, BDADDR_ANY);
2296
Johan Hedberg23d0e122014-02-19 14:57:46 +02002297 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2298 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002299
Johan Hedberg31dd6242014-06-27 14:23:02 +03002300distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002301 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2302 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002303
2304 return 0;
2305}
2306
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002307static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2308{
2309 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002310 struct l2cap_chan *chan = conn->smp;
2311 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002312 struct smp_csrk *csrk;
2313
2314 BT_DBG("conn %p", conn);
2315
2316 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002317 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002318
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002319 /* Mark the information as received */
2320 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2321
2322 skb_pull(skb, sizeof(*rp));
2323
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002324 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2325 if (csrk) {
2326 csrk->master = 0x01;
2327 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2328 }
2329 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002330 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002331
2332 return 0;
2333}
2334
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002335static u8 sc_select_method(struct smp_chan *smp)
2336{
2337 struct l2cap_conn *conn = smp->conn;
2338 struct hci_conn *hcon = conn->hcon;
2339 struct smp_cmd_pairing *local, *remote;
2340 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2341
Johan Hedberga29b0732014-10-28 15:17:05 +01002342 if (test_bit(SMP_FLAG_OOB, &smp->flags))
2343 return REQ_OOB;
2344
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002345 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2346 * which are needed as inputs to some crypto functions. To get
2347 * the "struct smp_cmd_pairing" from them we need to skip the
2348 * first byte which contains the opcode.
2349 */
2350 if (hcon->out) {
2351 local = (void *) &smp->preq[1];
2352 remote = (void *) &smp->prsp[1];
2353 } else {
2354 local = (void *) &smp->prsp[1];
2355 remote = (void *) &smp->preq[1];
2356 }
2357
2358 local_io = local->io_capability;
2359 remote_io = remote->io_capability;
2360
2361 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2362 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2363
2364 /* If either side wants MITM, look up the method from the table,
2365 * otherwise use JUST WORKS.
2366 */
2367 if (local_mitm || remote_mitm)
2368 method = get_auth_method(smp, local_io, remote_io);
2369 else
2370 method = JUST_WORKS;
2371
2372 /* Don't confirm locally initiated pairing attempts */
2373 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2374 method = JUST_WORKS;
2375
2376 return method;
2377}
2378
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002379static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2380{
2381 struct smp_cmd_public_key *key = (void *) skb->data;
2382 struct hci_conn *hcon = conn->hcon;
2383 struct l2cap_chan *chan = conn->smp;
2384 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002385 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002386 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002387 int err;
2388
2389 BT_DBG("conn %p", conn);
2390
2391 if (skb->len < sizeof(*key))
2392 return SMP_INVALID_PARAMS;
2393
2394 memcpy(smp->remote_pk, key, 64);
2395
2396 /* Non-initiating device sends its public key after receiving
2397 * the key from the initiating device.
2398 */
2399 if (!hcon->out) {
2400 err = sc_send_public_key(smp);
2401 if (err)
2402 return err;
2403 }
2404
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002405 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2406 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002407
2408 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2409 return SMP_UNSPECIFIED;
2410
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002411 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002412
2413 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2414
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002415 smp->method = sc_select_method(smp);
2416
2417 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2418
2419 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2420 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2421 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2422 else
2423 hcon->pending_sec_level = BT_SECURITY_FIPS;
2424
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002425 if (!memcmp(debug_pk, smp->remote_pk, 64))
2426 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2427
Johan Hedberg38606f12014-06-25 11:10:28 +03002428 if (smp->method == DSP_PASSKEY) {
2429 get_random_bytes(&hcon->passkey_notify,
2430 sizeof(hcon->passkey_notify));
2431 hcon->passkey_notify %= 1000000;
2432 hcon->passkey_entered = 0;
2433 smp->passkey_round = 0;
2434 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2435 hcon->dst_type,
2436 hcon->passkey_notify,
2437 hcon->passkey_entered))
2438 return SMP_UNSPECIFIED;
2439 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2440 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2441 }
2442
Johan Hedberga29b0732014-10-28 15:17:05 +01002443 if (smp->method == REQ_OOB) {
2444 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2445 smp->rr, 0, cfm.confirm_val);
2446 if (err)
2447 return SMP_UNSPECIFIED;
2448
2449 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2450 return SMP_CONFIRM_FAILED;
2451
2452 if (hcon->out)
2453 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2454 sizeof(smp->prnd), smp->prnd);
2455
2456 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2457
2458 return 0;
2459 }
2460
Johan Hedberg38606f12014-06-25 11:10:28 +03002461 if (hcon->out)
2462 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2463
2464 if (smp->method == REQ_PASSKEY) {
2465 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2466 hcon->dst_type))
2467 return SMP_UNSPECIFIED;
2468 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2469 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2470 return 0;
2471 }
2472
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002473 /* The Initiating device waits for the non-initiating device to
2474 * send the confirm value.
2475 */
2476 if (conn->hcon->out)
2477 return 0;
2478
2479 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2480 0, cfm.confirm_val);
2481 if (err)
2482 return SMP_UNSPECIFIED;
2483
2484 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2485 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2486
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002487 return 0;
2488}
2489
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002490static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2491{
2492 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2493 struct l2cap_chan *chan = conn->smp;
2494 struct hci_conn *hcon = conn->hcon;
2495 struct smp_chan *smp = chan->data;
2496 u8 a[7], b[7], *local_addr, *remote_addr;
2497 u8 io_cap[3], r[16], e[16];
2498 int err;
2499
2500 BT_DBG("conn %p", conn);
2501
2502 if (skb->len < sizeof(*check))
2503 return SMP_INVALID_PARAMS;
2504
2505 memcpy(a, &hcon->init_addr, 6);
2506 memcpy(b, &hcon->resp_addr, 6);
2507 a[6] = hcon->init_addr_type;
2508 b[6] = hcon->resp_addr_type;
2509
2510 if (hcon->out) {
2511 local_addr = a;
2512 remote_addr = b;
2513 memcpy(io_cap, &smp->prsp[1], 3);
2514 } else {
2515 local_addr = b;
2516 remote_addr = a;
2517 memcpy(io_cap, &smp->preq[1], 3);
2518 }
2519
2520 memset(r, 0, sizeof(r));
2521
Johan Hedberg38606f12014-06-25 11:10:28 +03002522 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2523 put_unaligned_le32(hcon->passkey_notify, r);
2524
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002525 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2526 io_cap, remote_addr, local_addr, e);
2527 if (err)
2528 return SMP_UNSPECIFIED;
2529
2530 if (memcmp(check->e, e, 16))
2531 return SMP_DHKEY_CHECK_FAILED;
2532
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002533 if (!hcon->out) {
2534 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2535 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2536 return 0;
2537 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002538
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002539 /* Slave sends DHKey check as response to master */
2540 sc_dhkey_check(smp);
2541 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002542
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002543 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002544
2545 if (hcon->out) {
2546 hci_le_start_enc(hcon, 0, 0, smp->tk);
2547 hcon->enc_key_size = smp->enc_key_size;
2548 }
2549
2550 return 0;
2551}
2552
Johan Hedberg1408bb62014-06-04 22:45:57 +03002553static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2554 struct sk_buff *skb)
2555{
2556 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2557
2558 BT_DBG("value 0x%02x", kp->value);
2559
2560 return 0;
2561}
2562
Johan Hedberg4befb862014-08-11 22:06:38 +03002563static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002564{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002565 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002566 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002567 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002568 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002569 int err = 0;
2570
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002571 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002572 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002573
Marcel Holtmann06ae3312013-10-18 03:43:00 -07002574 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002575 reason = SMP_PAIRING_NOTSUPP;
2576 goto done;
2577 }
2578
Marcel Holtmann92381f52013-10-03 01:23:08 -07002579 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002580 skb_pull(skb, sizeof(code));
2581
Johan Hedbergb28b4942014-09-05 22:19:55 +03002582 smp = chan->data;
2583
2584 if (code > SMP_CMD_MAX)
2585 goto drop;
2586
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002587 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002588 goto drop;
2589
2590 /* If we don't have a context the only allowed commands are
2591 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002592 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002593 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2594 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002595
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002596 switch (code) {
2597 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002598 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002599 break;
2600
2601 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002602 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002603 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002604 break;
2605
2606 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002607 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002608 break;
2609
2610 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002611 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002612 break;
2613
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002614 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002615 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002616 break;
2617
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002618 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002619 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002620 break;
2621
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002622 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002623 reason = smp_cmd_encrypt_info(conn, skb);
2624 break;
2625
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002626 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002627 reason = smp_cmd_master_ident(conn, skb);
2628 break;
2629
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002630 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002631 reason = smp_cmd_ident_info(conn, skb);
2632 break;
2633
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002634 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002635 reason = smp_cmd_ident_addr_info(conn, skb);
2636 break;
2637
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002638 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002639 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002640 break;
2641
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002642 case SMP_CMD_PUBLIC_KEY:
2643 reason = smp_cmd_public_key(conn, skb);
2644 break;
2645
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002646 case SMP_CMD_DHKEY_CHECK:
2647 reason = smp_cmd_dhkey_check(conn, skb);
2648 break;
2649
Johan Hedberg1408bb62014-06-04 22:45:57 +03002650 case SMP_CMD_KEYPRESS_NOTIFY:
2651 reason = smp_cmd_keypress_notify(conn, skb);
2652 break;
2653
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002654 default:
2655 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002656 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002657 goto done;
2658 }
2659
2660done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002661 if (!err) {
2662 if (reason)
2663 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002664 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002665 }
2666
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002667 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002668
2669drop:
2670 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2671 code, &hcon->dst);
2672 kfree_skb(skb);
2673 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002674}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002675
Johan Hedberg70db83c2014-08-08 09:37:16 +03002676static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2677{
2678 struct l2cap_conn *conn = chan->conn;
2679
2680 BT_DBG("chan %p", chan);
2681
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002682 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002683 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002684
Johan Hedberg70db83c2014-08-08 09:37:16 +03002685 conn->smp = NULL;
2686 l2cap_chan_put(chan);
2687}
2688
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002689static void bredr_pairing(struct l2cap_chan *chan)
2690{
2691 struct l2cap_conn *conn = chan->conn;
2692 struct hci_conn *hcon = conn->hcon;
2693 struct hci_dev *hdev = hcon->hdev;
2694 struct smp_cmd_pairing req;
2695 struct smp_chan *smp;
2696
2697 BT_DBG("chan %p", chan);
2698
2699 /* Only new pairings are interesting */
2700 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2701 return;
2702
2703 /* Don't bother if we're not encrypted */
2704 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2705 return;
2706
2707 /* Only master may initiate SMP over BR/EDR */
2708 if (hcon->role != HCI_ROLE_MASTER)
2709 return;
2710
2711 /* Secure Connections support must be enabled */
2712 if (!test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
2713 return;
2714
2715 /* BR/EDR must use Secure Connections for SMP */
2716 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
2717 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2718 return;
2719
2720 /* If our LE support is not enabled don't do anything */
2721 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
2722 return;
2723
2724 /* Don't bother if remote LE support is not enabled */
2725 if (!lmp_host_le_capable(hcon))
2726 return;
2727
2728 /* Remote must support SMP fixed chan for BR/EDR */
2729 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2730 return;
2731
2732 /* Don't bother if SMP is already ongoing */
2733 if (chan->data)
2734 return;
2735
2736 smp = smp_chan_create(conn);
2737 if (!smp) {
2738 BT_ERR("%s unable to create SMP context for BR/EDR",
2739 hdev->name);
2740 return;
2741 }
2742
2743 set_bit(SMP_FLAG_SC, &smp->flags);
2744
2745 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2746
2747 /* Prepare and send the BR/EDR SMP Pairing Request */
2748 build_bredr_pairing_cmd(smp, &req, NULL);
2749
2750 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2751 memcpy(&smp->preq[1], &req, sizeof(req));
2752
2753 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2754 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2755}
2756
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002757static void smp_resume_cb(struct l2cap_chan *chan)
2758{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002759 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002760 struct l2cap_conn *conn = chan->conn;
2761 struct hci_conn *hcon = conn->hcon;
2762
2763 BT_DBG("chan %p", chan);
2764
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002765 if (hcon->type == ACL_LINK) {
2766 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002767 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002768 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002769
Johan Hedberg86d14072014-08-11 22:06:43 +03002770 if (!smp)
2771 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002772
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002773 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2774 return;
2775
Johan Hedberg86d14072014-08-11 22:06:43 +03002776 cancel_delayed_work(&smp->security_timer);
2777
Johan Hedbergd6268e82014-09-05 22:19:51 +03002778 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002779}
2780
Johan Hedberg70db83c2014-08-08 09:37:16 +03002781static void smp_ready_cb(struct l2cap_chan *chan)
2782{
2783 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002784 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002785
2786 BT_DBG("chan %p", chan);
2787
2788 conn->smp = chan;
2789 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002790
2791 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2792 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002793}
2794
Johan Hedberg4befb862014-08-11 22:06:38 +03002795static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2796{
2797 int err;
2798
2799 BT_DBG("chan %p", chan);
2800
2801 err = smp_sig_channel(chan, skb);
2802 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002803 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002804
Johan Hedbergb68fda62014-08-11 22:06:40 +03002805 if (smp)
2806 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002807
Johan Hedberg1e91c292014-08-18 20:33:29 +03002808 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002809 }
2810
2811 return err;
2812}
2813
Johan Hedberg70db83c2014-08-08 09:37:16 +03002814static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2815 unsigned long hdr_len,
2816 unsigned long len, int nb)
2817{
2818 struct sk_buff *skb;
2819
2820 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2821 if (!skb)
2822 return ERR_PTR(-ENOMEM);
2823
2824 skb->priority = HCI_PRIO_MAX;
2825 bt_cb(skb)->chan = chan;
2826
2827 return skb;
2828}
2829
2830static const struct l2cap_ops smp_chan_ops = {
2831 .name = "Security Manager",
2832 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002833 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002834 .alloc_skb = smp_alloc_skb_cb,
2835 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002836 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002837
2838 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002839 .state_change = l2cap_chan_no_state_change,
2840 .close = l2cap_chan_no_close,
2841 .defer = l2cap_chan_no_defer,
2842 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002843 .set_shutdown = l2cap_chan_no_set_shutdown,
2844 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2845 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2846};
2847
2848static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2849{
2850 struct l2cap_chan *chan;
2851
2852 BT_DBG("pchan %p", pchan);
2853
2854 chan = l2cap_chan_create();
2855 if (!chan)
2856 return NULL;
2857
2858 chan->chan_type = pchan->chan_type;
2859 chan->ops = &smp_chan_ops;
2860 chan->scid = pchan->scid;
2861 chan->dcid = chan->scid;
2862 chan->imtu = pchan->imtu;
2863 chan->omtu = pchan->omtu;
2864 chan->mode = pchan->mode;
2865
Johan Hedbergabe84902014-11-12 22:22:21 +02002866 /* Other L2CAP channels may request SMP routines in order to
2867 * change the security level. This means that the SMP channel
2868 * lock must be considered in its own category to avoid lockdep
2869 * warnings.
2870 */
2871 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2872
Johan Hedberg70db83c2014-08-08 09:37:16 +03002873 BT_DBG("created chan %p", chan);
2874
2875 return chan;
2876}
2877
2878static const struct l2cap_ops smp_root_chan_ops = {
2879 .name = "Security Manager Root",
2880 .new_connection = smp_new_conn_cb,
2881
2882 /* None of these are implemented for the root channel */
2883 .close = l2cap_chan_no_close,
2884 .alloc_skb = l2cap_chan_no_alloc_skb,
2885 .recv = l2cap_chan_no_recv,
2886 .state_change = l2cap_chan_no_state_change,
2887 .teardown = l2cap_chan_no_teardown,
2888 .ready = l2cap_chan_no_ready,
2889 .defer = l2cap_chan_no_defer,
2890 .suspend = l2cap_chan_no_suspend,
2891 .resume = l2cap_chan_no_resume,
2892 .set_shutdown = l2cap_chan_no_set_shutdown,
2893 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2894 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2895};
2896
Johan Hedbergef8efe42014-08-13 15:12:32 +03002897static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002898{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002899 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002900 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002901
Johan Hedbergef8efe42014-08-13 15:12:32 +03002902 if (cid == L2CAP_CID_SMP_BREDR) {
2903 tfm_aes = NULL;
2904 goto create_chan;
2905 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03002906
Johan Hedbergadae20c2014-11-13 14:37:48 +02002907 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002908 if (IS_ERR(tfm_aes)) {
Johan Hedberg711eafe2014-08-08 09:32:52 +03002909 BT_ERR("Unable to create crypto context");
Johan Hedbergef8efe42014-08-13 15:12:32 +03002910 return ERR_PTR(PTR_ERR(tfm_aes));
Johan Hedberg711eafe2014-08-08 09:32:52 +03002911 }
2912
Johan Hedbergef8efe42014-08-13 15:12:32 +03002913create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03002914 chan = l2cap_chan_create();
2915 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002916 crypto_free_blkcipher(tfm_aes);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002917 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002918 }
2919
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002920 chan->data = tfm_aes;
2921
Johan Hedbergef8efe42014-08-13 15:12:32 +03002922 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002923
2924 l2cap_chan_set_defaults(chan);
2925
2926 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002927 if (cid == L2CAP_CID_SMP)
2928 chan->src_type = BDADDR_LE_PUBLIC;
2929 else
2930 chan->src_type = BDADDR_BREDR;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002931 chan->state = BT_LISTEN;
2932 chan->mode = L2CAP_MODE_BASIC;
2933 chan->imtu = L2CAP_DEFAULT_MTU;
2934 chan->ops = &smp_root_chan_ops;
2935
Johan Hedbergabe84902014-11-12 22:22:21 +02002936 /* Set correct nesting level for a parent/listening channel */
2937 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2938
Johan Hedbergef8efe42014-08-13 15:12:32 +03002939 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03002940}
2941
Johan Hedbergef8efe42014-08-13 15:12:32 +03002942static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002943{
Johan Hedbergef8efe42014-08-13 15:12:32 +03002944 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002945
Johan Hedbergef8efe42014-08-13 15:12:32 +03002946 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002947
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002948 tfm_aes = chan->data;
2949 if (tfm_aes) {
2950 chan->data = NULL;
2951 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002952 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03002953
Johan Hedberg70db83c2014-08-08 09:37:16 +03002954 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002955}
Johan Hedbergef8efe42014-08-13 15:12:32 +03002956
2957int smp_register(struct hci_dev *hdev)
2958{
2959 struct l2cap_chan *chan;
2960
2961 BT_DBG("%s", hdev->name);
2962
2963 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
2964 if (IS_ERR(chan))
2965 return PTR_ERR(chan);
2966
2967 hdev->smp_data = chan;
2968
2969 if (!lmp_sc_capable(hdev) &&
2970 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2971 return 0;
2972
2973 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
2974 if (IS_ERR(chan)) {
2975 int err = PTR_ERR(chan);
2976 chan = hdev->smp_data;
2977 hdev->smp_data = NULL;
2978 smp_del_chan(chan);
2979 return err;
2980 }
2981
2982 hdev->smp_bredr_data = chan;
2983
2984 return 0;
2985}
2986
2987void smp_unregister(struct hci_dev *hdev)
2988{
2989 struct l2cap_chan *chan;
2990
2991 if (hdev->smp_bredr_data) {
2992 chan = hdev->smp_bredr_data;
2993 hdev->smp_bredr_data = NULL;
2994 smp_del_chan(chan);
2995 }
2996
2997 if (hdev->smp_data) {
2998 chan = hdev->smp_data;
2999 hdev->smp_data = NULL;
3000 smp_del_chan(chan);
3001 }
3002}