blob: f845dbf2e677732c139c80ec6cb554d26ab3f433 [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 Hedberg191dc7f2014-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 Hedberg191dc7f2014-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 Hedberg191dc7f2014-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 Hedbergcd082792014-12-02 13:37:41 +0200383static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
384 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200385{
Johan Hedberg943a7322014-03-18 12:58:24 +0200386 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200387 int err;
388
389 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200390 memcpy(_res, r, 3);
391 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200392
Johan Hedberg943a7322014-03-18 12:58:24 +0200393 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200394 if (err) {
395 BT_ERR("Encrypt error");
396 return err;
397 }
398
399 /* The output of the random address function ah is:
400 * ah(h, r) = e(k, r') mod 2^24
401 * The output of the security function e is then truncated to 24 bits
402 * by taking the least significant 24 bits of the output of e as the
403 * result of ah.
404 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200405 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200406
407 return 0;
408}
409
Johan Hedbergcd082792014-12-02 13:37:41 +0200410bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
411 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200412{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300413 struct l2cap_chan *chan = hdev->smp_data;
414 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200415 u8 hash[3];
416 int err;
417
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300418 if (!chan || !chan->data)
419 return false;
420
421 tfm = chan->data;
422
Johan Hedberg60478052014-02-18 10:19:31 +0200423 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
424
425 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
426 if (err)
427 return false;
428
429 return !memcmp(bdaddr->b, hash, 3);
430}
431
Johan Hedbergcd082792014-12-02 13:37:41 +0200432int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200433{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300434 struct l2cap_chan *chan = hdev->smp_data;
435 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200436 int err;
437
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300438 if (!chan || !chan->data)
439 return -EOPNOTSUPP;
440
441 tfm = chan->data;
442
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200443 get_random_bytes(&rpa->b[3], 3);
444
445 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
446 rpa->b[5] |= 0x40; /* Set second most significant bit */
447
448 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
449 if (err < 0)
450 return err;
451
452 BT_DBG("RPA %pMR", rpa);
453
454 return 0;
455}
456
Johan Hedbergcd082792014-12-02 13:37:41 +0200457static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
458 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
459 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300460{
461 u8 p1[16], p2[16];
462 int err;
463
464 memset(p1, 0, 16);
465
466 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200467 p1[0] = _iat;
468 p1[1] = _rat;
469 memcpy(p1 + 2, preq, 7);
470 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300471
472 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200473 memcpy(p2, ra, 6);
474 memcpy(p2 + 6, ia, 6);
475 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300476
477 /* res = r XOR p1 */
478 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
479
480 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200481 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300482 if (err) {
483 BT_ERR("Encrypt data error");
484 return err;
485 }
486
487 /* res = res XOR p2 */
488 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
489
490 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200491 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300492 if (err)
493 BT_ERR("Encrypt data error");
494
495 return err;
496}
497
Johan Hedbergcd082792014-12-02 13:37:41 +0200498static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
499 const u8 r1[16], const u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300500{
501 int err;
502
503 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200504 memcpy(_r, r2, 8);
505 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300506
Johan Hedberge491eaf2014-10-25 21:15:37 +0200507 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300508 if (err)
509 BT_ERR("Encrypt data error");
510
511 return err;
512}
513
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300514static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
515{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300516 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300517 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300518 struct kvec iv[2];
519 struct msghdr msg;
520
521 if (!chan)
522 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300523
524 BT_DBG("code 0x%2.2x", code);
525
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300526 iv[0].iov_base = &code;
527 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300528
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300529 iv[1].iov_base = data;
530 iv[1].iov_len = len;
531
532 memset(&msg, 0, sizeof(msg));
533
534 msg.msg_iov = (struct iovec *) &iv;
535 msg.msg_iovlen = 2;
536
537 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300538
Johan Hedbergb68fda62014-08-11 22:06:40 +0300539 if (!chan->data)
540 return;
541
542 smp = chan->data;
543
544 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300545 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300546}
547
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300548static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800549{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300550 if (authreq & SMP_AUTH_MITM) {
551 if (authreq & SMP_AUTH_SC)
552 return BT_SECURITY_FIPS;
553 else
554 return BT_SECURITY_HIGH;
555 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800556 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300557 }
Brian Gix2b64d152011-12-21 16:12:12 -0800558}
559
560static __u8 seclevel_to_authreq(__u8 sec_level)
561{
562 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300563 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800564 case BT_SECURITY_HIGH:
565 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
566 case BT_SECURITY_MEDIUM:
567 return SMP_AUTH_BONDING;
568 default:
569 return SMP_AUTH_NONE;
570 }
571}
572
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300573static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700574 struct smp_cmd_pairing *req,
575 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300576{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300577 struct l2cap_chan *chan = conn->smp;
578 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200579 struct hci_conn *hcon = conn->hcon;
580 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100581 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300582
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300583 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700584 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
585 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300586 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800587 } else {
588 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300589 }
590
Johan Hedbergfd349c02014-02-18 10:19:36 +0200591 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
592 remote_dist |= SMP_DIST_ID_KEY;
593
Johan Hedberg863efaf2014-02-22 19:06:32 +0200594 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
595 local_dist |= SMP_DIST_ID_KEY;
596
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100597 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
598 (authreq & SMP_AUTH_SC)) {
599 struct oob_data *oob_data;
600 u8 bdaddr_type;
601
602 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300603 local_dist |= SMP_DIST_LINK_KEY;
604 remote_dist |= SMP_DIST_LINK_KEY;
605 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100606
607 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
608 bdaddr_type = BDADDR_LE_PUBLIC;
609 else
610 bdaddr_type = BDADDR_LE_RANDOM;
611
612 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
613 bdaddr_type);
614 if (oob_data) {
615 set_bit(SMP_FLAG_OOB, &smp->flags);
616 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100617 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100618 memcpy(smp->pcnf, oob_data->hash256, 16);
619 }
620
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300621 } else {
622 authreq &= ~SMP_AUTH_SC;
623 }
624
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300625 if (rsp == NULL) {
626 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100627 req->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300628 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200629 req->init_key_dist = local_dist;
630 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300631 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200632
633 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300634 return;
635 }
636
637 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100638 rsp->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300639 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200640 rsp->init_key_dist = req->init_key_dist & remote_dist;
641 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300642 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200643
644 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300645}
646
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300647static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
648{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300649 struct l2cap_chan *chan = conn->smp;
650 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300651
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300652 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700653 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300654 return SMP_ENC_KEY_SIZE;
655
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300656 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300657
658 return 0;
659}
660
Johan Hedberg6f48e262014-08-11 22:06:44 +0300661static void smp_chan_destroy(struct l2cap_conn *conn)
662{
663 struct l2cap_chan *chan = conn->smp;
664 struct smp_chan *smp = chan->data;
665 bool complete;
666
667 BUG_ON(!smp);
668
669 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300670
Johan Hedberg6f48e262014-08-11 22:06:44 +0300671 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
672 mgmt_smp_complete(conn->hcon, complete);
673
674 kfree(smp->csrk);
675 kfree(smp->slave_csrk);
Johan Hedberg6a770832014-06-06 11:54:04 +0300676 kfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300677
678 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300679 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300680
681 /* If pairing failed clean up any keys we might have */
682 if (!complete) {
683 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200684 list_del_rcu(&smp->ltk->list);
685 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300686 }
687
688 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200689 list_del_rcu(&smp->slave_ltk->list);
690 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300691 }
692
693 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200694 list_del_rcu(&smp->remote_irk->list);
695 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300696 }
697 }
698
699 chan->data = NULL;
700 kfree(smp);
701 hci_conn_drop(conn->hcon);
702}
703
Johan Hedberg84794e12013-11-06 11:24:57 +0200704static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800705{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200706 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300707 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200708
Johan Hedberg84794e12013-11-06 11:24:57 +0200709 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800710 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700711 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800712
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700713 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700714 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300715
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300716 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300717 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800718}
719
Brian Gix2b64d152011-12-21 16:12:12 -0800720#define JUST_WORKS 0x00
721#define JUST_CFM 0x01
722#define REQ_PASSKEY 0x02
723#define CFM_PASSKEY 0x03
724#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300725#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800726#define OVERLAP 0xFF
727
728static const u8 gen_method[5][5] = {
729 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
730 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
731 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
732 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
733 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
734};
735
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300736static const u8 sc_method[5][5] = {
737 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
738 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
739 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
740 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
741 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
742};
743
Johan Hedberg581370c2014-06-17 13:07:38 +0300744static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
745{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300746 /* If either side has unknown io_caps, use JUST_CFM (which gets
747 * converted later to JUST_WORKS if we're initiators.
748 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300749 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
750 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300751 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300752
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300753 if (test_bit(SMP_FLAG_SC, &smp->flags))
754 return sc_method[remote_io][local_io];
755
Johan Hedberg581370c2014-06-17 13:07:38 +0300756 return gen_method[remote_io][local_io];
757}
758
Brian Gix2b64d152011-12-21 16:12:12 -0800759static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
760 u8 local_io, u8 remote_io)
761{
762 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300763 struct l2cap_chan *chan = conn->smp;
764 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800765 u32 passkey = 0;
766 int ret = 0;
767
768 /* Initialize key for JUST WORKS */
769 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300770 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800771
772 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
773
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300774 /* If neither side wants MITM, either "just" confirm an incoming
775 * request or use just-works for outgoing ones. The JUST_CFM
776 * will be converted to JUST_WORKS if necessary later in this
777 * function. If either side has MITM look up the method from the
778 * table.
779 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300780 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300781 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800782 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300783 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800784
Johan Hedberga82505c2014-03-24 14:39:07 +0200785 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300786 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
787 &smp->flags))
788 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200789
Johan Hedberg02f3e252014-07-16 15:09:13 +0300790 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300791 if (smp->method == JUST_CFM &&
792 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
793 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300794
Brian Gix2b64d152011-12-21 16:12:12 -0800795 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300796 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300797 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800798 return 0;
799 }
800
801 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300802 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300803 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300804 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
805 hcon->pending_sec_level = BT_SECURITY_HIGH;
806 }
Brian Gix2b64d152011-12-21 16:12:12 -0800807
808 /* If both devices have Keyoard-Display I/O, the master
809 * Confirms and the slave Enters the passkey.
810 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300811 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300812 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300813 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800814 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300815 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800816 }
817
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200818 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300819 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200820 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800821 get_random_bytes(&passkey, sizeof(passkey));
822 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200823 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800824 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300825 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800826 }
827
Johan Hedberg783e0572014-05-31 18:48:26 +0300828 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700829 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200830 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300831 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200832 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
833 hcon->type, hcon->dst_type,
834 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800835 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200836 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200837 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200838 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800839
Brian Gix2b64d152011-12-21 16:12:12 -0800840 return ret;
841}
842
Johan Hedberg1cc61142014-05-20 09:45:52 +0300843static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300844{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300845 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300846 struct smp_cmd_pairing_confirm cp;
847 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300848
849 BT_DBG("conn %p", conn);
850
Johan Hedberge491eaf2014-10-25 21:15:37 +0200851 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200852 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200853 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
854 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300855 if (ret)
856 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300857
Johan Hedberg4a74d652014-05-20 09:45:50 +0300858 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800859
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300860 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
861
Johan Hedbergb28b4942014-09-05 22:19:55 +0300862 if (conn->hcon->out)
863 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
864 else
865 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
866
Johan Hedberg1cc61142014-05-20 09:45:52 +0300867 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300868}
869
Johan Hedberg861580a2014-05-20 09:45:51 +0300870static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300871{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300872 struct l2cap_conn *conn = smp->conn;
873 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300874 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300875 int ret;
876
Johan Hedbergec70f362014-06-27 14:23:04 +0300877 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300878 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300879
880 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
881
Johan Hedberge491eaf2014-10-25 21:15:37 +0200882 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200883 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200884 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300885 if (ret)
886 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300887
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300888 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
889 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300890 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300891 }
892
893 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800894 u8 stk[16];
895 __le64 rand = 0;
896 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300897
Johan Hedberge491eaf2014-10-25 21:15:37 +0200898 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300899
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300900 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300901 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300902
Johan Hedberg861580a2014-05-20 09:45:51 +0300903 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
904 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300905
906 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300907 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300908 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300909 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300910 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800911 __le64 rand = 0;
912 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300913
Johan Hedberg943a7322014-03-18 12:58:24 +0200914 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
915 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300916
Johan Hedberge491eaf2014-10-25 21:15:37 +0200917 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300918
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300919 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700920 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300921
Johan Hedbergfff34902014-06-10 15:19:50 +0300922 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
923 auth = 1;
924 else
925 auth = 0;
926
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300927 /* Even though there's no _SLAVE suffix this is the
928 * slave STK we're adding for later lookup (the master
929 * STK never needs to be stored).
930 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700931 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300932 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300933 }
934
Johan Hedberg861580a2014-05-20 09:45:51 +0300935 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300936}
937
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300938static void smp_notify_keys(struct l2cap_conn *conn)
939{
940 struct l2cap_chan *chan = conn->smp;
941 struct smp_chan *smp = chan->data;
942 struct hci_conn *hcon = conn->hcon;
943 struct hci_dev *hdev = hcon->hdev;
944 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
945 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
946 bool persistent;
947
948 if (smp->remote_irk) {
949 mgmt_new_irk(hdev, smp->remote_irk);
950 /* Now that user space can be considered to know the
951 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300952 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300953 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300954 if (hcon->type == LE_LINK) {
955 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
956 hcon->dst_type = smp->remote_irk->addr_type;
957 queue_work(hdev->workqueue, &conn->id_addr_update_work);
958 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300959
960 /* When receiving an indentity resolving key for
961 * a remote device that does not use a resolvable
962 * private address, just remove the key so that
963 * it is possible to use the controller white
964 * list for scanning.
965 *
966 * Userspace will have been told to not store
967 * this key at this point. So it is safe to
968 * just remove it.
969 */
970 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200971 list_del_rcu(&smp->remote_irk->list);
972 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300973 smp->remote_irk = NULL;
974 }
975 }
976
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300977 if (hcon->type == ACL_LINK) {
978 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
979 persistent = false;
980 else
981 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
982 &hcon->flags);
983 } else {
984 /* The LTKs and CSRKs should be persistent only if both sides
985 * had the bonding bit set in their authentication requests.
986 */
987 persistent = !!((req->auth_req & rsp->auth_req) &
988 SMP_AUTH_BONDING);
989 }
990
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300991
992 if (smp->csrk) {
993 smp->csrk->bdaddr_type = hcon->dst_type;
994 bacpy(&smp->csrk->bdaddr, &hcon->dst);
995 mgmt_new_csrk(hdev, smp->csrk, persistent);
996 }
997
998 if (smp->slave_csrk) {
999 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1000 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1001 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1002 }
1003
1004 if (smp->ltk) {
1005 smp->ltk->bdaddr_type = hcon->dst_type;
1006 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1007 mgmt_new_ltk(hdev, smp->ltk, persistent);
1008 }
1009
1010 if (smp->slave_ltk) {
1011 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1012 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1013 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1014 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001015
1016 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001017 struct link_key *key;
1018 u8 type;
1019
1020 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1021 type = HCI_LK_DEBUG_COMBINATION;
1022 else if (hcon->sec_level == BT_SECURITY_FIPS)
1023 type = HCI_LK_AUTH_COMBINATION_P256;
1024 else
1025 type = HCI_LK_UNAUTH_COMBINATION_P256;
1026
1027 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1028 smp->link_key, type, 0, &persistent);
1029 if (key) {
1030 mgmt_new_link_key(hdev, key, persistent);
1031
1032 /* Don't keep debug keys around if the relevant
1033 * flag is not set.
1034 */
1035 if (!test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags) &&
1036 key->type == HCI_LK_DEBUG_COMBINATION) {
1037 list_del_rcu(&key->list);
1038 kfree_rcu(key, rcu);
1039 }
1040 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001041 }
1042}
1043
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001044static void sc_add_ltk(struct smp_chan *smp)
1045{
1046 struct hci_conn *hcon = smp->conn->hcon;
1047 u8 key_type, auth;
1048
1049 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1050 key_type = SMP_LTK_P256_DEBUG;
1051 else
1052 key_type = SMP_LTK_P256;
1053
1054 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1055 auth = 1;
1056 else
1057 auth = 0;
1058
1059 memset(smp->tk + smp->enc_key_size, 0,
1060 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
1061
1062 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1063 key_type, auth, smp->tk, smp->enc_key_size,
1064 0, 0);
1065}
1066
Johan Hedberg6a770832014-06-06 11:54:04 +03001067static void sc_generate_link_key(struct smp_chan *smp)
1068{
1069 /* These constants are as specified in the core specification.
1070 * In ASCII they spell out to 'tmp1' and 'lebr'.
1071 */
1072 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1073 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1074
1075 smp->link_key = kzalloc(16, GFP_KERNEL);
1076 if (!smp->link_key)
1077 return;
1078
1079 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1080 kfree(smp->link_key);
1081 smp->link_key = NULL;
1082 return;
1083 }
1084
1085 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
1086 kfree(smp->link_key);
1087 smp->link_key = NULL;
1088 return;
1089 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001090}
1091
Johan Hedbergb28b4942014-09-05 22:19:55 +03001092static void smp_allow_key_dist(struct smp_chan *smp)
1093{
1094 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1095 * will be allowed in each PDU handler to ensure we receive
1096 * them in the correct order.
1097 */
1098 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1099 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1100 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1101 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1102 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1103 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1104}
1105
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001106static void sc_generate_ltk(struct smp_chan *smp)
1107{
1108 /* These constants are as specified in the core specification.
1109 * In ASCII they spell out to 'tmp2' and 'brle'.
1110 */
1111 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1112 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1113 struct hci_conn *hcon = smp->conn->hcon;
1114 struct hci_dev *hdev = hcon->hdev;
1115 struct link_key *key;
1116
1117 key = hci_find_link_key(hdev, &hcon->dst);
1118 if (!key) {
1119 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1120 return;
1121 }
1122
1123 if (key->type == HCI_LK_DEBUG_COMBINATION)
1124 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1125
1126 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1127 return;
1128
1129 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1130 return;
1131
1132 sc_add_ltk(smp);
1133}
1134
Johan Hedbergd6268e82014-09-05 22:19:51 +03001135static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001136{
1137 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001138 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001139 struct hci_conn *hcon = conn->hcon;
1140 struct hci_dev *hdev = hcon->hdev;
1141 __u8 *keydist;
1142
1143 BT_DBG("conn %p", conn);
1144
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001145 rsp = (void *) &smp->prsp[1];
1146
1147 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001148 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1149 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001150 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001151 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001152
1153 req = (void *) &smp->preq[1];
1154
1155 if (hcon->out) {
1156 keydist = &rsp->init_key_dist;
1157 *keydist &= req->init_key_dist;
1158 } else {
1159 keydist = &rsp->resp_key_dist;
1160 *keydist &= req->resp_key_dist;
1161 }
1162
Johan Hedberg6a770832014-06-06 11:54:04 +03001163 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001164 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001165 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001166 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1167 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001168
1169 /* Clear the keys which are generated but not distributed */
1170 *keydist &= ~SMP_SC_NO_DIST;
1171 }
1172
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001173 BT_DBG("keydist 0x%x", *keydist);
1174
1175 if (*keydist & SMP_DIST_ENC_KEY) {
1176 struct smp_cmd_encrypt_info enc;
1177 struct smp_cmd_master_ident ident;
1178 struct smp_ltk *ltk;
1179 u8 authenticated;
1180 __le16 ediv;
1181 __le64 rand;
1182
1183 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1184 get_random_bytes(&ediv, sizeof(ediv));
1185 get_random_bytes(&rand, sizeof(rand));
1186
1187 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1188
1189 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1190 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1191 SMP_LTK_SLAVE, authenticated, enc.ltk,
1192 smp->enc_key_size, ediv, rand);
1193 smp->slave_ltk = ltk;
1194
1195 ident.ediv = ediv;
1196 ident.rand = rand;
1197
1198 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1199
1200 *keydist &= ~SMP_DIST_ENC_KEY;
1201 }
1202
1203 if (*keydist & SMP_DIST_ID_KEY) {
1204 struct smp_cmd_ident_addr_info addrinfo;
1205 struct smp_cmd_ident_info idinfo;
1206
1207 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1208
1209 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1210
1211 /* The hci_conn contains the local identity address
1212 * after the connection has been established.
1213 *
1214 * This is true even when the connection has been
1215 * established using a resolvable random address.
1216 */
1217 bacpy(&addrinfo.bdaddr, &hcon->src);
1218 addrinfo.addr_type = hcon->src_type;
1219
1220 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1221 &addrinfo);
1222
1223 *keydist &= ~SMP_DIST_ID_KEY;
1224 }
1225
1226 if (*keydist & SMP_DIST_SIGN) {
1227 struct smp_cmd_sign_info sign;
1228 struct smp_csrk *csrk;
1229
1230 /* Generate a new random key */
1231 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1232
1233 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1234 if (csrk) {
1235 csrk->master = 0x00;
1236 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1237 }
1238 smp->slave_csrk = csrk;
1239
1240 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1241
1242 *keydist &= ~SMP_DIST_SIGN;
1243 }
1244
1245 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001246 if (smp->remote_key_dist & KEY_DIST_MASK) {
1247 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001248 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001249 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001250
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001251 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1252 smp_notify_keys(conn);
1253
1254 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001255}
1256
Johan Hedbergb68fda62014-08-11 22:06:40 +03001257static void smp_timeout(struct work_struct *work)
1258{
1259 struct smp_chan *smp = container_of(work, struct smp_chan,
1260 security_timer.work);
1261 struct l2cap_conn *conn = smp->conn;
1262
1263 BT_DBG("conn %p", conn);
1264
Johan Hedberg1e91c292014-08-18 20:33:29 +03001265 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001266}
1267
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001268static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1269{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001270 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001271 struct smp_chan *smp;
1272
Marcel Holtmannf1560462013-10-13 05:43:25 -07001273 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001274 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001275 return NULL;
1276
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001277 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1278 if (IS_ERR(smp->tfm_aes)) {
1279 BT_ERR("Unable to create ECB crypto context");
1280 kfree(smp);
1281 return NULL;
1282 }
1283
Johan Hedberg407cecf2014-05-02 14:19:47 +03001284 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1285 if (IS_ERR(smp->tfm_cmac)) {
1286 BT_ERR("Unable to create CMAC crypto context");
1287 crypto_free_blkcipher(smp->tfm_aes);
1288 kfree(smp);
1289 return NULL;
1290 }
1291
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001292 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001293 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001294
Johan Hedbergb28b4942014-09-05 22:19:55 +03001295 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1296
Johan Hedbergb68fda62014-08-11 22:06:40 +03001297 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1298
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001299 hci_conn_hold(conn->hcon);
1300
1301 return smp;
1302}
1303
Johan Hedberg760b0182014-06-06 11:44:05 +03001304static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1305{
1306 struct hci_conn *hcon = smp->conn->hcon;
1307 u8 *na, *nb, a[7], b[7];
1308
1309 if (hcon->out) {
1310 na = smp->prnd;
1311 nb = smp->rrnd;
1312 } else {
1313 na = smp->rrnd;
1314 nb = smp->prnd;
1315 }
1316
1317 memcpy(a, &hcon->init_addr, 6);
1318 memcpy(b, &hcon->resp_addr, 6);
1319 a[6] = hcon->init_addr_type;
1320 b[6] = hcon->resp_addr_type;
1321
1322 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1323}
1324
Johan Hedberg38606f12014-06-25 11:10:28 +03001325static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001326{
1327 struct hci_conn *hcon = smp->conn->hcon;
1328 struct smp_cmd_dhkey_check check;
1329 u8 a[7], b[7], *local_addr, *remote_addr;
1330 u8 io_cap[3], r[16];
1331
Johan Hedberg760b0182014-06-06 11:44:05 +03001332 memcpy(a, &hcon->init_addr, 6);
1333 memcpy(b, &hcon->resp_addr, 6);
1334 a[6] = hcon->init_addr_type;
1335 b[6] = hcon->resp_addr_type;
1336
1337 if (hcon->out) {
1338 local_addr = a;
1339 remote_addr = b;
1340 memcpy(io_cap, &smp->preq[1], 3);
1341 } else {
1342 local_addr = b;
1343 remote_addr = a;
1344 memcpy(io_cap, &smp->prsp[1], 3);
1345 }
1346
Johan Hedbergdddd3052014-06-01 15:38:09 +03001347 memset(r, 0, sizeof(r));
1348
1349 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001350 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001351
Johan Hedberga29b0732014-10-28 15:17:05 +01001352 if (smp->method == REQ_OOB)
1353 memcpy(r, smp->rr, 16);
1354
Johan Hedberg760b0182014-06-06 11:44:05 +03001355 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1356 local_addr, remote_addr, check.e);
1357
1358 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001359}
1360
Johan Hedberg38606f12014-06-25 11:10:28 +03001361static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1362{
1363 struct l2cap_conn *conn = smp->conn;
1364 struct hci_conn *hcon = conn->hcon;
1365 struct smp_cmd_pairing_confirm cfm;
1366 u8 r;
1367
1368 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1369 r |= 0x80;
1370
1371 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1372
1373 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1374 cfm.confirm_val))
1375 return SMP_UNSPECIFIED;
1376
1377 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1378
1379 return 0;
1380}
1381
1382static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1383{
1384 struct l2cap_conn *conn = smp->conn;
1385 struct hci_conn *hcon = conn->hcon;
1386 struct hci_dev *hdev = hcon->hdev;
1387 u8 cfm[16], r;
1388
1389 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1390 if (smp->passkey_round >= 20)
1391 return 0;
1392
1393 switch (smp_op) {
1394 case SMP_CMD_PAIRING_RANDOM:
1395 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1396 r |= 0x80;
1397
1398 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1399 smp->rrnd, r, cfm))
1400 return SMP_UNSPECIFIED;
1401
1402 if (memcmp(smp->pcnf, cfm, 16))
1403 return SMP_CONFIRM_FAILED;
1404
1405 smp->passkey_round++;
1406
1407 if (smp->passkey_round == 20) {
1408 /* Generate MacKey and LTK */
1409 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1410 return SMP_UNSPECIFIED;
1411 }
1412
1413 /* The round is only complete when the initiator
1414 * receives pairing random.
1415 */
1416 if (!hcon->out) {
1417 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1418 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001419 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001420 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001421 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001422 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001423 return 0;
1424 }
1425
1426 /* Start the next round */
1427 if (smp->passkey_round != 20)
1428 return sc_passkey_round(smp, 0);
1429
1430 /* Passkey rounds are complete - start DHKey Check */
1431 sc_dhkey_check(smp);
1432 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1433
1434 break;
1435
1436 case SMP_CMD_PAIRING_CONFIRM:
1437 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1438 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1439 return 0;
1440 }
1441
1442 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1443
1444 if (hcon->out) {
1445 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1446 sizeof(smp->prnd), smp->prnd);
1447 return 0;
1448 }
1449
1450 return sc_passkey_send_confirm(smp);
1451
1452 case SMP_CMD_PUBLIC_KEY:
1453 default:
1454 /* Initiating device starts the round */
1455 if (!hcon->out)
1456 return 0;
1457
1458 BT_DBG("%s Starting passkey round %u", hdev->name,
1459 smp->passkey_round + 1);
1460
1461 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1462
1463 return sc_passkey_send_confirm(smp);
1464 }
1465
1466 return 0;
1467}
1468
Johan Hedbergdddd3052014-06-01 15:38:09 +03001469static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1470{
Johan Hedberg38606f12014-06-25 11:10:28 +03001471 struct l2cap_conn *conn = smp->conn;
1472 struct hci_conn *hcon = conn->hcon;
1473 u8 smp_op;
1474
1475 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1476
Johan Hedbergdddd3052014-06-01 15:38:09 +03001477 switch (mgmt_op) {
1478 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1479 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1480 return 0;
1481 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1482 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1483 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001484 case MGMT_OP_USER_PASSKEY_REPLY:
1485 hcon->passkey_notify = le32_to_cpu(passkey);
1486 smp->passkey_round = 0;
1487
1488 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1489 smp_op = SMP_CMD_PAIRING_CONFIRM;
1490 else
1491 smp_op = 0;
1492
1493 if (sc_passkey_round(smp, smp_op))
1494 return -EIO;
1495
1496 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001497 }
1498
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001499 /* Initiator sends DHKey check first */
1500 if (hcon->out) {
1501 sc_dhkey_check(smp);
1502 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1503 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1504 sc_dhkey_check(smp);
1505 sc_add_ltk(smp);
1506 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001507
1508 return 0;
1509}
1510
Brian Gix2b64d152011-12-21 16:12:12 -08001511int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1512{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001513 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001514 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001515 struct smp_chan *smp;
1516 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001517 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001518
1519 BT_DBG("");
1520
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001521 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001522 return -ENOTCONN;
1523
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001524 chan = conn->smp;
1525 if (!chan)
1526 return -ENOTCONN;
1527
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001528 l2cap_chan_lock(chan);
1529 if (!chan->data) {
1530 err = -ENOTCONN;
1531 goto unlock;
1532 }
1533
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001534 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001535
Johan Hedberg760b0182014-06-06 11:44:05 +03001536 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1537 err = sc_user_reply(smp, mgmt_op, passkey);
1538 goto unlock;
1539 }
1540
Brian Gix2b64d152011-12-21 16:12:12 -08001541 switch (mgmt_op) {
1542 case MGMT_OP_USER_PASSKEY_REPLY:
1543 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001544 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001545 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001546 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001547 /* Fall Through */
1548 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001549 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001550 break;
1551 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1552 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001553 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001554 err = 0;
1555 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001556 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001557 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001558 err = -EOPNOTSUPP;
1559 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001560 }
1561
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001562 err = 0;
1563
Brian Gix2b64d152011-12-21 16:12:12 -08001564 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001565 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1566 u8 rsp = smp_confirm(smp);
1567 if (rsp)
1568 smp_failure(conn, rsp);
1569 }
Brian Gix2b64d152011-12-21 16:12:12 -08001570
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001571unlock:
1572 l2cap_chan_unlock(chan);
1573 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001574}
1575
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001576static void build_bredr_pairing_cmd(struct smp_chan *smp,
1577 struct smp_cmd_pairing *req,
1578 struct smp_cmd_pairing *rsp)
1579{
1580 struct l2cap_conn *conn = smp->conn;
1581 struct hci_dev *hdev = conn->hcon->hdev;
1582 u8 local_dist = 0, remote_dist = 0;
1583
1584 if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) {
1585 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1586 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1587 }
1588
1589 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
1590 remote_dist |= SMP_DIST_ID_KEY;
1591
1592 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
1593 local_dist |= SMP_DIST_ID_KEY;
1594
1595 if (!rsp) {
1596 memset(req, 0, sizeof(*req));
1597
1598 req->init_key_dist = local_dist;
1599 req->resp_key_dist = remote_dist;
1600 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1601
1602 smp->remote_key_dist = remote_dist;
1603
1604 return;
1605 }
1606
1607 memset(rsp, 0, sizeof(*rsp));
1608
1609 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1610 rsp->init_key_dist = req->init_key_dist & remote_dist;
1611 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1612
1613 smp->remote_key_dist = rsp->init_key_dist;
1614}
1615
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001616static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001617{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001618 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001619 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001620 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001621 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001622 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001623 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001624
1625 BT_DBG("conn %p", conn);
1626
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001627 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001628 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001629
Johan Hedberg40bef302014-07-16 11:42:27 +03001630 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001631 return SMP_CMD_NOTSUPP;
1632
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001633 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001634 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001635 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001636 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001637
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001638 if (!smp)
1639 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001640
Johan Hedbergc05b9332014-09-10 17:37:42 -07001641 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001642 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001643
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001644 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001645 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001646 return SMP_PAIRING_NOTSUPP;
1647
Johan Hedberg903b71c2014-09-08 16:59:18 -07001648 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1649 return SMP_AUTH_REQUIREMENTS;
1650
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001651 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1652 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001653 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001654
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001655 /* SMP over BR/EDR requires special treatment */
1656 if (conn->hcon->type == ACL_LINK) {
1657 /* We must have a BR/EDR SC link */
1658 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags))
1659 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1660
1661 set_bit(SMP_FLAG_SC, &smp->flags);
1662
1663 build_bredr_pairing_cmd(smp, req, &rsp);
1664
1665 key_size = min(req->max_key_size, rsp.max_key_size);
1666 if (check_enc_key_size(conn, key_size))
1667 return SMP_ENC_KEY_SIZE;
1668
1669 /* Clear bits which are generated but not distributed */
1670 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1671
1672 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1673 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1674 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1675
1676 smp_distribute_keys(smp);
1677 return 0;
1678 }
1679
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001680 build_pairing_cmd(conn, req, &rsp, auth);
1681
1682 if (rsp.auth_req & SMP_AUTH_SC)
1683 set_bit(SMP_FLAG_SC, &smp->flags);
1684
Johan Hedberg5be5e272014-09-10 17:58:54 -07001685 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001686 sec_level = BT_SECURITY_MEDIUM;
1687 else
1688 sec_level = authreq_to_seclevel(auth);
1689
Johan Hedbergc7262e72014-06-17 13:07:37 +03001690 if (sec_level > conn->hcon->pending_sec_level)
1691 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001692
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001693 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001694 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1695 u8 method;
1696
1697 method = get_auth_method(smp, conn->hcon->io_capability,
1698 req->io_capability);
1699 if (method == JUST_WORKS || method == JUST_CFM)
1700 return SMP_AUTH_REQUIREMENTS;
1701 }
1702
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001703 key_size = min(req->max_key_size, rsp.max_key_size);
1704 if (check_enc_key_size(conn, key_size))
1705 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001706
Johan Hedberge84a6b12013-12-02 10:49:03 +02001707 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001708
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001709 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1710 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001711
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001712 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001713
1714 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1715
1716 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1717 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1718 /* Clear bits which are generated but not distributed */
1719 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1720 /* Wait for Public Key from Initiating Device */
1721 return 0;
1722 } else {
1723 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1724 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001725
Brian Gix2b64d152011-12-21 16:12:12 -08001726 /* Request setup of TK */
1727 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1728 if (ret)
1729 return SMP_UNSPECIFIED;
1730
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001731 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001732}
1733
Johan Hedberg3b191462014-06-06 10:50:15 +03001734static u8 sc_send_public_key(struct smp_chan *smp)
1735{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001736 struct hci_dev *hdev = smp->conn->hcon->hdev;
1737
Johan Hedberg3b191462014-06-06 10:50:15 +03001738 BT_DBG("");
1739
Johan Hedberg70157ef2014-06-24 15:22:59 +03001740 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) {
1741 BT_DBG("Using debug keys");
1742 memcpy(smp->local_pk, debug_pk, 64);
1743 memcpy(smp->local_sk, debug_sk, 32);
1744 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1745 } else {
1746 while (true) {
1747 /* Generate local key pair for Secure Connections */
1748 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1749 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001750
Johan Hedberg70157ef2014-06-24 15:22:59 +03001751 /* This is unlikely, but we need to check that
1752 * we didn't accidentially generate a debug key.
1753 */
1754 if (memcmp(smp->local_sk, debug_sk, 32))
1755 break;
1756 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001757 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001758
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001759 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
1760 SMP_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1761 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001762
1763 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1764
1765 return 0;
1766}
1767
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001768static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001769{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001770 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001771 struct l2cap_chan *chan = conn->smp;
1772 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001773 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001774 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001775 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001776
1777 BT_DBG("conn %p", conn);
1778
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001779 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001780 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001781
Johan Hedberg40bef302014-07-16 11:42:27 +03001782 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001783 return SMP_CMD_NOTSUPP;
1784
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001785 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001786
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001787 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001788
1789 key_size = min(req->max_key_size, rsp->max_key_size);
1790 if (check_enc_key_size(conn, key_size))
1791 return SMP_ENC_KEY_SIZE;
1792
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001793 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001794
Johan Hedberg903b71c2014-09-08 16:59:18 -07001795 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1796 return SMP_AUTH_REQUIREMENTS;
1797
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001798 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1799 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1800
1801 /* Update remote key distribution in case the remote cleared
1802 * some bits that we had enabled in our request.
1803 */
1804 smp->remote_key_dist &= rsp->resp_key_dist;
1805
1806 /* For BR/EDR this means we're done and can start phase 3 */
1807 if (conn->hcon->type == ACL_LINK) {
1808 /* Clear bits which are generated but not distributed */
1809 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1810 smp_distribute_keys(smp);
1811 return 0;
1812 }
1813
Johan Hedberg65668772014-05-16 11:03:34 +03001814 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1815 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001816 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1817 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001818
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001819 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001820 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1821 u8 method;
1822
1823 method = get_auth_method(smp, req->io_capability,
1824 rsp->io_capability);
1825 if (method == JUST_WORKS || method == JUST_CFM)
1826 return SMP_AUTH_REQUIREMENTS;
1827 }
1828
Johan Hedberge84a6b12013-12-02 10:49:03 +02001829 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001830
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001831 /* Update remote key distribution in case the remote cleared
1832 * some bits that we had enabled in our request.
1833 */
1834 smp->remote_key_dist &= rsp->resp_key_dist;
1835
Johan Hedberg3b191462014-06-06 10:50:15 +03001836 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1837 /* Clear bits which are generated but not distributed */
1838 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1839 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1840 return sc_send_public_key(smp);
1841 }
1842
Johan Hedbergc05b9332014-09-10 17:37:42 -07001843 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001844
Johan Hedberg476585e2012-06-06 18:54:15 +08001845 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001846 if (ret)
1847 return SMP_UNSPECIFIED;
1848
Johan Hedberg4a74d652014-05-20 09:45:50 +03001849 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001850
1851 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001852 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001853 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001854
1855 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001856}
1857
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001858static u8 sc_check_confirm(struct smp_chan *smp)
1859{
1860 struct l2cap_conn *conn = smp->conn;
1861
1862 BT_DBG("");
1863
1864 /* Public Key exchange must happen before any other steps */
1865 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1866 return SMP_UNSPECIFIED;
1867
Johan Hedberg38606f12014-06-25 11:10:28 +03001868 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1869 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1870
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001871 if (conn->hcon->out) {
1872 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1873 smp->prnd);
1874 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1875 }
1876
1877 return 0;
1878}
1879
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001880static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001881{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001882 struct l2cap_chan *chan = conn->smp;
1883 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001884
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001885 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1886
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001887 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001888 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001889
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001890 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1891 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001892
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001893 if (test_bit(SMP_FLAG_SC, &smp->flags))
1894 return sc_check_confirm(smp);
1895
Johan Hedbergb28b4942014-09-05 22:19:55 +03001896 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001897 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1898 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001899 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1900 return 0;
1901 }
1902
1903 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001904 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001905 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001906 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001907
1908 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001909}
1910
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001911static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001912{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001913 struct l2cap_chan *chan = conn->smp;
1914 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001915 struct hci_conn *hcon = conn->hcon;
1916 u8 *pkax, *pkbx, *na, *nb;
1917 u32 passkey;
1918 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001919
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001920 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001921
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001922 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001923 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001924
Johan Hedberg943a7322014-03-18 12:58:24 +02001925 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001926 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001927
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001928 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1929 return smp_random(smp);
1930
Johan Hedberga29b0732014-10-28 15:17:05 +01001931 if (smp->method == REQ_OOB) {
1932 if (!hcon->out)
1933 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1934 sizeof(smp->prnd), smp->prnd);
1935 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1936 goto mackey_and_ltk;
1937 }
1938
Johan Hedberg38606f12014-06-25 11:10:28 +03001939 /* Passkey entry has special treatment */
1940 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1941 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
1942
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001943 if (hcon->out) {
1944 u8 cfm[16];
1945
1946 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1947 smp->rrnd, 0, cfm);
1948 if (err)
1949 return SMP_UNSPECIFIED;
1950
1951 if (memcmp(smp->pcnf, cfm, 16))
1952 return SMP_CONFIRM_FAILED;
1953
1954 pkax = smp->local_pk;
1955 pkbx = smp->remote_pk;
1956 na = smp->prnd;
1957 nb = smp->rrnd;
1958 } else {
1959 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1960 smp->prnd);
1961 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1962
1963 pkax = smp->remote_pk;
1964 pkbx = smp->local_pk;
1965 na = smp->rrnd;
1966 nb = smp->prnd;
1967 }
1968
Johan Hedberga29b0732014-10-28 15:17:05 +01001969mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03001970 /* Generate MacKey and LTK */
1971 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1972 if (err)
1973 return SMP_UNSPECIFIED;
1974
Johan Hedberga29b0732014-10-28 15:17:05 +01001975 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03001976 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03001977 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001978 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1979 }
1980 return 0;
1981 }
1982
Johan Hedberg38606f12014-06-25 11:10:28 +03001983 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001984 if (err)
1985 return SMP_UNSPECIFIED;
1986
Johan Hedberg38606f12014-06-25 11:10:28 +03001987 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
1988 hcon->dst_type, passkey, 0);
1989 if (err)
1990 return SMP_UNSPECIFIED;
1991
1992 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1993
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001994 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001995}
1996
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001997static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001998{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001999 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002000 struct hci_conn *hcon = conn->hcon;
2001
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002002 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002003 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002004 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002005
Johan Hedberga6f78332014-09-10 17:37:45 -07002006 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002007 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002008
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002009 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002010 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002011
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002012 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
2013 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002014
Johan Hedbergfe59a052014-07-01 19:14:12 +03002015 /* We never store STKs for master role, so clear this flag */
2016 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2017
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002018 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002019}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002020
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002021bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2022 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002023{
2024 if (sec_level == BT_SECURITY_LOW)
2025 return true;
2026
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002027 /* If we're encrypted with an STK but the caller prefers using
2028 * LTK claim insufficient security. This way we allow the
2029 * connection to be re-encrypted with an LTK, even if the LTK
2030 * provides the same level of security. Only exception is if we
2031 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002032 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002033 if (key_pref == SMP_USE_LTK &&
2034 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002035 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002036 return false;
2037
Johan Hedberg854f4722014-07-01 18:40:20 +03002038 if (hcon->sec_level >= sec_level)
2039 return true;
2040
2041 return false;
2042}
2043
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002044static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002045{
2046 struct smp_cmd_security_req *rp = (void *) skb->data;
2047 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002048 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002049 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002050 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002051 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002052
2053 BT_DBG("conn %p", conn);
2054
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002055 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002056 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002057
Johan Hedberg40bef302014-07-16 11:42:27 +03002058 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002059 return SMP_CMD_NOTSUPP;
2060
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002061 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002062
Johan Hedberg903b71c2014-09-08 16:59:18 -07002063 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
2064 return SMP_AUTH_REQUIREMENTS;
2065
Johan Hedberg5be5e272014-09-10 17:58:54 -07002066 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002067 sec_level = BT_SECURITY_MEDIUM;
2068 else
2069 sec_level = authreq_to_seclevel(auth);
2070
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002071 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002072 return 0;
2073
Johan Hedbergc7262e72014-06-17 13:07:37 +03002074 if (sec_level > hcon->pending_sec_level)
2075 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002076
Johan Hedberg4dab7862012-06-07 14:58:37 +08002077 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002078 return 0;
2079
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002080 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002081 if (!smp)
2082 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002083
Johan Hedbergb6ae8452014-07-30 09:22:22 +03002084 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002085 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002086 return SMP_PAIRING_NOTSUPP;
2087
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002088 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002089
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002090 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002091 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002092
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002093 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2094 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002095
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002096 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002097 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002098
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002099 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002100}
2101
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002102int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002103{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002104 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002105 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002106 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002107 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002108 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002109
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002110 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2111
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002112 /* This may be NULL if there's an unexpected disconnection */
2113 if (!conn)
2114 return 1;
2115
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002116 chan = conn->smp;
2117
Johan Hedberg757aee02013-04-24 13:05:32 +03002118 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002119 return 1;
2120
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002121 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002122 return 1;
2123
Johan Hedbergc7262e72014-06-17 13:07:37 +03002124 if (sec_level > hcon->pending_sec_level)
2125 hcon->pending_sec_level = sec_level;
2126
Johan Hedberg40bef302014-07-16 11:42:27 +03002127 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002128 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2129 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002130
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002131 l2cap_chan_lock(chan);
2132
2133 /* If SMP is already in progress ignore this request */
2134 if (chan->data) {
2135 ret = 0;
2136 goto unlock;
2137 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002138
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002139 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002140 if (!smp) {
2141 ret = 1;
2142 goto unlock;
2143 }
Brian Gix2b64d152011-12-21 16:12:12 -08002144
2145 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002146
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002147 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
2148 authreq |= SMP_AUTH_SC;
2149
Johan Hedberg79897d22014-06-01 09:45:24 +03002150 /* Require MITM if IO Capability allows or the security level
2151 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002152 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002153 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002154 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002155 authreq |= SMP_AUTH_MITM;
2156
Johan Hedberg40bef302014-07-16 11:42:27 +03002157 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002158 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002159
Brian Gix2b64d152011-12-21 16:12:12 -08002160 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002161 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2162 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002163
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002164 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002165 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002166 } else {
2167 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002168 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002169 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002170 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002171 }
2172
Johan Hedberg4a74d652014-05-20 09:45:50 +03002173 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002174 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002175
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002176unlock:
2177 l2cap_chan_unlock(chan);
2178 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002179}
2180
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002181static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2182{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002183 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002184 struct l2cap_chan *chan = conn->smp;
2185 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002186
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002187 BT_DBG("conn %p", conn);
2188
2189 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002190 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002191
Johan Hedbergb28b4942014-09-05 22:19:55 +03002192 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002193
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002194 skb_pull(skb, sizeof(*rp));
2195
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002196 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002197
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002198 return 0;
2199}
2200
2201static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2202{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002203 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002204 struct l2cap_chan *chan = conn->smp;
2205 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002206 struct hci_dev *hdev = conn->hcon->hdev;
2207 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002208 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002209 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002210
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002211 BT_DBG("conn %p", conn);
2212
2213 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002214 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002215
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002216 /* Mark the information as received */
2217 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2218
Johan Hedbergb28b4942014-09-05 22:19:55 +03002219 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2220 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002221 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2222 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002223
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002224 skb_pull(skb, sizeof(*rp));
2225
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002226 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002227 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002228 authenticated, smp->tk, smp->enc_key_size,
2229 rp->ediv, rp->rand);
2230 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002231 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002232 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002233
2234 return 0;
2235}
2236
Johan Hedbergfd349c02014-02-18 10:19:36 +02002237static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2238{
2239 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002240 struct l2cap_chan *chan = conn->smp;
2241 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002242
2243 BT_DBG("");
2244
2245 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002246 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002247
Johan Hedbergb28b4942014-09-05 22:19:55 +03002248 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002249
Johan Hedbergfd349c02014-02-18 10:19:36 +02002250 skb_pull(skb, sizeof(*info));
2251
2252 memcpy(smp->irk, info->irk, 16);
2253
2254 return 0;
2255}
2256
2257static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2258 struct sk_buff *skb)
2259{
2260 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002261 struct l2cap_chan *chan = conn->smp;
2262 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002263 struct hci_conn *hcon = conn->hcon;
2264 bdaddr_t rpa;
2265
2266 BT_DBG("");
2267
2268 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002269 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002270
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002271 /* Mark the information as received */
2272 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2273
Johan Hedbergb28b4942014-09-05 22:19:55 +03002274 if (smp->remote_key_dist & SMP_DIST_SIGN)
2275 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2276
Johan Hedbergfd349c02014-02-18 10:19:36 +02002277 skb_pull(skb, sizeof(*info));
2278
Johan Hedberga9a58f82014-02-25 22:24:37 +02002279 /* Strictly speaking the Core Specification (4.1) allows sending
2280 * an empty address which would force us to rely on just the IRK
2281 * as "identity information". However, since such
2282 * implementations are not known of and in order to not over
2283 * complicate our implementation, simply pretend that we never
2284 * received an IRK for such a device.
2285 */
2286 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
2287 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002288 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002289 }
2290
Johan Hedbergfd349c02014-02-18 10:19:36 +02002291 bacpy(&smp->id_addr, &info->bdaddr);
2292 smp->id_addr_type = info->addr_type;
2293
2294 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2295 bacpy(&rpa, &hcon->dst);
2296 else
2297 bacpy(&rpa, BDADDR_ANY);
2298
Johan Hedberg23d0e122014-02-19 14:57:46 +02002299 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2300 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002301
Johan Hedberg31dd6242014-06-27 14:23:02 +03002302distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002303 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2304 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002305
2306 return 0;
2307}
2308
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002309static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2310{
2311 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002312 struct l2cap_chan *chan = conn->smp;
2313 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002314 struct smp_csrk *csrk;
2315
2316 BT_DBG("conn %p", conn);
2317
2318 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002319 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002320
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002321 /* Mark the information as received */
2322 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2323
2324 skb_pull(skb, sizeof(*rp));
2325
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002326 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2327 if (csrk) {
2328 csrk->master = 0x01;
2329 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2330 }
2331 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002332 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002333
2334 return 0;
2335}
2336
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002337static u8 sc_select_method(struct smp_chan *smp)
2338{
2339 struct l2cap_conn *conn = smp->conn;
2340 struct hci_conn *hcon = conn->hcon;
2341 struct smp_cmd_pairing *local, *remote;
2342 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2343
Johan Hedberga29b0732014-10-28 15:17:05 +01002344 if (test_bit(SMP_FLAG_OOB, &smp->flags))
2345 return REQ_OOB;
2346
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002347 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2348 * which are needed as inputs to some crypto functions. To get
2349 * the "struct smp_cmd_pairing" from them we need to skip the
2350 * first byte which contains the opcode.
2351 */
2352 if (hcon->out) {
2353 local = (void *) &smp->preq[1];
2354 remote = (void *) &smp->prsp[1];
2355 } else {
2356 local = (void *) &smp->prsp[1];
2357 remote = (void *) &smp->preq[1];
2358 }
2359
2360 local_io = local->io_capability;
2361 remote_io = remote->io_capability;
2362
2363 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2364 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2365
2366 /* If either side wants MITM, look up the method from the table,
2367 * otherwise use JUST WORKS.
2368 */
2369 if (local_mitm || remote_mitm)
2370 method = get_auth_method(smp, local_io, remote_io);
2371 else
2372 method = JUST_WORKS;
2373
2374 /* Don't confirm locally initiated pairing attempts */
2375 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2376 method = JUST_WORKS;
2377
2378 return method;
2379}
2380
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002381static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2382{
2383 struct smp_cmd_public_key *key = (void *) skb->data;
2384 struct hci_conn *hcon = conn->hcon;
2385 struct l2cap_chan *chan = conn->smp;
2386 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002387 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002388 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002389 int err;
2390
2391 BT_DBG("conn %p", conn);
2392
2393 if (skb->len < sizeof(*key))
2394 return SMP_INVALID_PARAMS;
2395
2396 memcpy(smp->remote_pk, key, 64);
2397
2398 /* Non-initiating device sends its public key after receiving
2399 * the key from the initiating device.
2400 */
2401 if (!hcon->out) {
2402 err = sc_send_public_key(smp);
2403 if (err)
2404 return err;
2405 }
2406
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002407 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2408 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002409
2410 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2411 return SMP_UNSPECIFIED;
2412
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002413 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002414
2415 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2416
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002417 smp->method = sc_select_method(smp);
2418
2419 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2420
2421 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2422 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2423 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2424 else
2425 hcon->pending_sec_level = BT_SECURITY_FIPS;
2426
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002427 if (!memcmp(debug_pk, smp->remote_pk, 64))
2428 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2429
Johan Hedberg38606f12014-06-25 11:10:28 +03002430 if (smp->method == DSP_PASSKEY) {
2431 get_random_bytes(&hcon->passkey_notify,
2432 sizeof(hcon->passkey_notify));
2433 hcon->passkey_notify %= 1000000;
2434 hcon->passkey_entered = 0;
2435 smp->passkey_round = 0;
2436 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2437 hcon->dst_type,
2438 hcon->passkey_notify,
2439 hcon->passkey_entered))
2440 return SMP_UNSPECIFIED;
2441 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2442 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2443 }
2444
Johan Hedberga29b0732014-10-28 15:17:05 +01002445 if (smp->method == REQ_OOB) {
2446 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2447 smp->rr, 0, cfm.confirm_val);
2448 if (err)
2449 return SMP_UNSPECIFIED;
2450
2451 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2452 return SMP_CONFIRM_FAILED;
2453
2454 if (hcon->out)
2455 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2456 sizeof(smp->prnd), smp->prnd);
2457
2458 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2459
2460 return 0;
2461 }
2462
Johan Hedberg38606f12014-06-25 11:10:28 +03002463 if (hcon->out)
2464 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2465
2466 if (smp->method == REQ_PASSKEY) {
2467 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2468 hcon->dst_type))
2469 return SMP_UNSPECIFIED;
2470 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2471 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2472 return 0;
2473 }
2474
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002475 /* The Initiating device waits for the non-initiating device to
2476 * send the confirm value.
2477 */
2478 if (conn->hcon->out)
2479 return 0;
2480
2481 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2482 0, cfm.confirm_val);
2483 if (err)
2484 return SMP_UNSPECIFIED;
2485
2486 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2487 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2488
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002489 return 0;
2490}
2491
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002492static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2493{
2494 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2495 struct l2cap_chan *chan = conn->smp;
2496 struct hci_conn *hcon = conn->hcon;
2497 struct smp_chan *smp = chan->data;
2498 u8 a[7], b[7], *local_addr, *remote_addr;
2499 u8 io_cap[3], r[16], e[16];
2500 int err;
2501
2502 BT_DBG("conn %p", conn);
2503
2504 if (skb->len < sizeof(*check))
2505 return SMP_INVALID_PARAMS;
2506
2507 memcpy(a, &hcon->init_addr, 6);
2508 memcpy(b, &hcon->resp_addr, 6);
2509 a[6] = hcon->init_addr_type;
2510 b[6] = hcon->resp_addr_type;
2511
2512 if (hcon->out) {
2513 local_addr = a;
2514 remote_addr = b;
2515 memcpy(io_cap, &smp->prsp[1], 3);
2516 } else {
2517 local_addr = b;
2518 remote_addr = a;
2519 memcpy(io_cap, &smp->preq[1], 3);
2520 }
2521
2522 memset(r, 0, sizeof(r));
2523
Johan Hedberg38606f12014-06-25 11:10:28 +03002524 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2525 put_unaligned_le32(hcon->passkey_notify, r);
2526
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002527 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2528 io_cap, remote_addr, local_addr, e);
2529 if (err)
2530 return SMP_UNSPECIFIED;
2531
2532 if (memcmp(check->e, e, 16))
2533 return SMP_DHKEY_CHECK_FAILED;
2534
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002535 if (!hcon->out) {
2536 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2537 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2538 return 0;
2539 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002540
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002541 /* Slave sends DHKey check as response to master */
2542 sc_dhkey_check(smp);
2543 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002544
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002545 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002546
2547 if (hcon->out) {
2548 hci_le_start_enc(hcon, 0, 0, smp->tk);
2549 hcon->enc_key_size = smp->enc_key_size;
2550 }
2551
2552 return 0;
2553}
2554
Johan Hedberg1408bb62014-06-04 22:45:57 +03002555static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2556 struct sk_buff *skb)
2557{
2558 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2559
2560 BT_DBG("value 0x%02x", kp->value);
2561
2562 return 0;
2563}
2564
Johan Hedberg4befb862014-08-11 22:06:38 +03002565static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002566{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002567 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002568 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002569 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002570 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002571 int err = 0;
2572
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002573 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002574 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002575
Marcel Holtmann06ae3312013-10-18 03:43:00 -07002576 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002577 reason = SMP_PAIRING_NOTSUPP;
2578 goto done;
2579 }
2580
Marcel Holtmann92381f52013-10-03 01:23:08 -07002581 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002582 skb_pull(skb, sizeof(code));
2583
Johan Hedbergb28b4942014-09-05 22:19:55 +03002584 smp = chan->data;
2585
2586 if (code > SMP_CMD_MAX)
2587 goto drop;
2588
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002589 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002590 goto drop;
2591
2592 /* If we don't have a context the only allowed commands are
2593 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002594 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002595 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2596 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002597
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002598 switch (code) {
2599 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002600 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002601 break;
2602
2603 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002604 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002605 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002606 break;
2607
2608 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002609 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002610 break;
2611
2612 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002613 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002614 break;
2615
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002616 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002617 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002618 break;
2619
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002620 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002621 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002622 break;
2623
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002624 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002625 reason = smp_cmd_encrypt_info(conn, skb);
2626 break;
2627
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002628 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002629 reason = smp_cmd_master_ident(conn, skb);
2630 break;
2631
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002632 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002633 reason = smp_cmd_ident_info(conn, skb);
2634 break;
2635
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002636 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002637 reason = smp_cmd_ident_addr_info(conn, skb);
2638 break;
2639
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002640 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002641 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002642 break;
2643
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002644 case SMP_CMD_PUBLIC_KEY:
2645 reason = smp_cmd_public_key(conn, skb);
2646 break;
2647
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002648 case SMP_CMD_DHKEY_CHECK:
2649 reason = smp_cmd_dhkey_check(conn, skb);
2650 break;
2651
Johan Hedberg1408bb62014-06-04 22:45:57 +03002652 case SMP_CMD_KEYPRESS_NOTIFY:
2653 reason = smp_cmd_keypress_notify(conn, skb);
2654 break;
2655
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002656 default:
2657 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002658 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002659 goto done;
2660 }
2661
2662done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002663 if (!err) {
2664 if (reason)
2665 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002666 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002667 }
2668
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002669 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002670
2671drop:
2672 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2673 code, &hcon->dst);
2674 kfree_skb(skb);
2675 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002676}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002677
Johan Hedberg70db83c2014-08-08 09:37:16 +03002678static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2679{
2680 struct l2cap_conn *conn = chan->conn;
2681
2682 BT_DBG("chan %p", chan);
2683
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002684 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002685 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002686
Johan Hedberg70db83c2014-08-08 09:37:16 +03002687 conn->smp = NULL;
2688 l2cap_chan_put(chan);
2689}
2690
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002691static void bredr_pairing(struct l2cap_chan *chan)
2692{
2693 struct l2cap_conn *conn = chan->conn;
2694 struct hci_conn *hcon = conn->hcon;
2695 struct hci_dev *hdev = hcon->hdev;
2696 struct smp_cmd_pairing req;
2697 struct smp_chan *smp;
2698
2699 BT_DBG("chan %p", chan);
2700
2701 /* Only new pairings are interesting */
2702 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2703 return;
2704
2705 /* Don't bother if we're not encrypted */
2706 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2707 return;
2708
2709 /* Only master may initiate SMP over BR/EDR */
2710 if (hcon->role != HCI_ROLE_MASTER)
2711 return;
2712
2713 /* Secure Connections support must be enabled */
2714 if (!test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
2715 return;
2716
2717 /* BR/EDR must use Secure Connections for SMP */
2718 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
2719 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2720 return;
2721
2722 /* If our LE support is not enabled don't do anything */
2723 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
2724 return;
2725
2726 /* Don't bother if remote LE support is not enabled */
2727 if (!lmp_host_le_capable(hcon))
2728 return;
2729
2730 /* Remote must support SMP fixed chan for BR/EDR */
2731 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2732 return;
2733
2734 /* Don't bother if SMP is already ongoing */
2735 if (chan->data)
2736 return;
2737
2738 smp = smp_chan_create(conn);
2739 if (!smp) {
2740 BT_ERR("%s unable to create SMP context for BR/EDR",
2741 hdev->name);
2742 return;
2743 }
2744
2745 set_bit(SMP_FLAG_SC, &smp->flags);
2746
2747 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2748
2749 /* Prepare and send the BR/EDR SMP Pairing Request */
2750 build_bredr_pairing_cmd(smp, &req, NULL);
2751
2752 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2753 memcpy(&smp->preq[1], &req, sizeof(req));
2754
2755 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2756 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2757}
2758
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002759static void smp_resume_cb(struct l2cap_chan *chan)
2760{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002761 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002762 struct l2cap_conn *conn = chan->conn;
2763 struct hci_conn *hcon = conn->hcon;
2764
2765 BT_DBG("chan %p", chan);
2766
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002767 if (hcon->type == ACL_LINK) {
2768 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002769 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002770 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002771
Johan Hedberg86d14072014-08-11 22:06:43 +03002772 if (!smp)
2773 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002774
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002775 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2776 return;
2777
Johan Hedberg86d14072014-08-11 22:06:43 +03002778 cancel_delayed_work(&smp->security_timer);
2779
Johan Hedbergd6268e82014-09-05 22:19:51 +03002780 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002781}
2782
Johan Hedberg70db83c2014-08-08 09:37:16 +03002783static void smp_ready_cb(struct l2cap_chan *chan)
2784{
2785 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002786 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002787
2788 BT_DBG("chan %p", chan);
2789
2790 conn->smp = chan;
2791 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002792
2793 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2794 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002795}
2796
Johan Hedberg4befb862014-08-11 22:06:38 +03002797static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2798{
2799 int err;
2800
2801 BT_DBG("chan %p", chan);
2802
2803 err = smp_sig_channel(chan, skb);
2804 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002805 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002806
Johan Hedbergb68fda62014-08-11 22:06:40 +03002807 if (smp)
2808 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002809
Johan Hedberg1e91c292014-08-18 20:33:29 +03002810 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002811 }
2812
2813 return err;
2814}
2815
Johan Hedberg70db83c2014-08-08 09:37:16 +03002816static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2817 unsigned long hdr_len,
2818 unsigned long len, int nb)
2819{
2820 struct sk_buff *skb;
2821
2822 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2823 if (!skb)
2824 return ERR_PTR(-ENOMEM);
2825
2826 skb->priority = HCI_PRIO_MAX;
2827 bt_cb(skb)->chan = chan;
2828
2829 return skb;
2830}
2831
2832static const struct l2cap_ops smp_chan_ops = {
2833 .name = "Security Manager",
2834 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002835 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002836 .alloc_skb = smp_alloc_skb_cb,
2837 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002838 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002839
2840 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002841 .state_change = l2cap_chan_no_state_change,
2842 .close = l2cap_chan_no_close,
2843 .defer = l2cap_chan_no_defer,
2844 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002845 .set_shutdown = l2cap_chan_no_set_shutdown,
2846 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2847 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2848};
2849
2850static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2851{
2852 struct l2cap_chan *chan;
2853
2854 BT_DBG("pchan %p", pchan);
2855
2856 chan = l2cap_chan_create();
2857 if (!chan)
2858 return NULL;
2859
2860 chan->chan_type = pchan->chan_type;
2861 chan->ops = &smp_chan_ops;
2862 chan->scid = pchan->scid;
2863 chan->dcid = chan->scid;
2864 chan->imtu = pchan->imtu;
2865 chan->omtu = pchan->omtu;
2866 chan->mode = pchan->mode;
2867
Johan Hedbergabe84902014-11-12 22:22:21 +02002868 /* Other L2CAP channels may request SMP routines in order to
2869 * change the security level. This means that the SMP channel
2870 * lock must be considered in its own category to avoid lockdep
2871 * warnings.
2872 */
2873 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2874
Johan Hedberg70db83c2014-08-08 09:37:16 +03002875 BT_DBG("created chan %p", chan);
2876
2877 return chan;
2878}
2879
2880static const struct l2cap_ops smp_root_chan_ops = {
2881 .name = "Security Manager Root",
2882 .new_connection = smp_new_conn_cb,
2883
2884 /* None of these are implemented for the root channel */
2885 .close = l2cap_chan_no_close,
2886 .alloc_skb = l2cap_chan_no_alloc_skb,
2887 .recv = l2cap_chan_no_recv,
2888 .state_change = l2cap_chan_no_state_change,
2889 .teardown = l2cap_chan_no_teardown,
2890 .ready = l2cap_chan_no_ready,
2891 .defer = l2cap_chan_no_defer,
2892 .suspend = l2cap_chan_no_suspend,
2893 .resume = l2cap_chan_no_resume,
2894 .set_shutdown = l2cap_chan_no_set_shutdown,
2895 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2896 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2897};
2898
Johan Hedbergef8efe42014-08-13 15:12:32 +03002899static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002900{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002901 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002902 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002903
Johan Hedbergef8efe42014-08-13 15:12:32 +03002904 if (cid == L2CAP_CID_SMP_BREDR) {
2905 tfm_aes = NULL;
2906 goto create_chan;
2907 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03002908
Johan Hedbergadae20c2014-11-13 14:37:48 +02002909 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002910 if (IS_ERR(tfm_aes)) {
Johan Hedberg711eafe2014-08-08 09:32:52 +03002911 BT_ERR("Unable to create crypto context");
Johan Hedbergef8efe42014-08-13 15:12:32 +03002912 return ERR_PTR(PTR_ERR(tfm_aes));
Johan Hedberg711eafe2014-08-08 09:32:52 +03002913 }
2914
Johan Hedbergef8efe42014-08-13 15:12:32 +03002915create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03002916 chan = l2cap_chan_create();
2917 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002918 crypto_free_blkcipher(tfm_aes);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002919 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002920 }
2921
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002922 chan->data = tfm_aes;
2923
Johan Hedbergef8efe42014-08-13 15:12:32 +03002924 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002925
2926 l2cap_chan_set_defaults(chan);
2927
2928 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002929 if (cid == L2CAP_CID_SMP)
2930 chan->src_type = BDADDR_LE_PUBLIC;
2931 else
2932 chan->src_type = BDADDR_BREDR;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002933 chan->state = BT_LISTEN;
2934 chan->mode = L2CAP_MODE_BASIC;
2935 chan->imtu = L2CAP_DEFAULT_MTU;
2936 chan->ops = &smp_root_chan_ops;
2937
Johan Hedbergabe84902014-11-12 22:22:21 +02002938 /* Set correct nesting level for a parent/listening channel */
2939 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2940
Johan Hedbergef8efe42014-08-13 15:12:32 +03002941 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03002942}
2943
Johan Hedbergef8efe42014-08-13 15:12:32 +03002944static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002945{
Johan Hedbergef8efe42014-08-13 15:12:32 +03002946 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002947
Johan Hedbergef8efe42014-08-13 15:12:32 +03002948 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002949
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002950 tfm_aes = chan->data;
2951 if (tfm_aes) {
2952 chan->data = NULL;
2953 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002954 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03002955
Johan Hedberg70db83c2014-08-08 09:37:16 +03002956 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002957}
Johan Hedbergef8efe42014-08-13 15:12:32 +03002958
2959int smp_register(struct hci_dev *hdev)
2960{
2961 struct l2cap_chan *chan;
2962
2963 BT_DBG("%s", hdev->name);
2964
2965 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
2966 if (IS_ERR(chan))
2967 return PTR_ERR(chan);
2968
2969 hdev->smp_data = chan;
2970
2971 if (!lmp_sc_capable(hdev) &&
2972 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2973 return 0;
2974
2975 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
2976 if (IS_ERR(chan)) {
2977 int err = PTR_ERR(chan);
2978 chan = hdev->smp_data;
2979 hdev->smp_data = NULL;
2980 smp_del_chan(chan);
2981 return err;
2982 }
2983
2984 hdev->smp_bredr_data = chan;
2985
2986 return 0;
2987}
2988
2989void smp_unregister(struct hci_dev *hdev)
2990{
2991 struct l2cap_chan *chan;
2992
2993 if (hdev->smp_bredr_data) {
2994 chan = hdev->smp_bredr_data;
2995 hdev->smp_bredr_data = NULL;
2996 smp_del_chan(chan);
2997 }
2998
2999 if (hdev->smp_data) {
3000 chan = hdev->smp_data;
3001 hdev->smp_data = NULL;
3002 smp_del_chan(chan);
3003 }
3004}