blob: 45e527d3c741851a9e7d4c90afc1c837c1364082 [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 Hedbergb28b4942014-09-05 22:19:55 +030035#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030036
Johan Hedberg3b191462014-06-06 10:50:15 +030037/* Keys which are not distributed with Secure Connections */
38#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
39
Marcel Holtmann17b02e62012-03-01 14:32:37 -080040#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030041
Johan Hedberg0edb14d2014-05-26 13:29:28 +030042#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
43 0x1f : 0x07)
44#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020045
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030046/* Maximum message length that can be passed to aes_cmac */
47#define CMAC_MSG_MAX 80
48
Johan Hedberg533e35d2014-06-16 19:25:18 +030049enum {
50 SMP_FLAG_TK_VALID,
51 SMP_FLAG_CFM_PENDING,
52 SMP_FLAG_MITM_AUTH,
53 SMP_FLAG_COMPLETE,
54 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030055 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030056 SMP_FLAG_REMOTE_PK,
Johan Hedberg533e35d2014-06-16 19:25:18 +030057};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030058
59struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030060 struct l2cap_conn *conn;
61 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030062 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030063
Johan Hedberg4bc58f52014-05-20 09:45:47 +030064 u8 preq[7]; /* SMP Pairing Request */
65 u8 prsp[7]; /* SMP Pairing Response */
66 u8 prnd[16]; /* SMP Pairing Random (local) */
67 u8 rrnd[16]; /* SMP Pairing Random (remote) */
68 u8 pcnf[16]; /* SMP Pairing Confirm */
69 u8 tk[16]; /* SMP Temporary Key */
70 u8 enc_key_size;
71 u8 remote_key_dist;
72 bdaddr_t id_addr;
73 u8 id_addr_type;
74 u8 irk[16];
75 struct smp_csrk *csrk;
76 struct smp_csrk *slave_csrk;
77 struct smp_ltk *ltk;
78 struct smp_ltk *slave_ltk;
79 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +030080 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +030081 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +030082 u8 method;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030083
Johan Hedberg3b191462014-06-06 10:50:15 +030084 /* Secure Connections variables */
85 u8 local_pk[64];
86 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030087 u8 remote_pk[64];
88 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +030089 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +030090
Johan Hedberg6a7bd102014-06-27 14:23:03 +030091 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +030092 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030093};
94
Johan Hedberg8a2936f2014-06-16 19:25:19 +030095static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030096{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030097 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030098
Johan Hedberg8a2936f2014-06-16 19:25:19 +030099 for (i = 0; i < len; i++)
100 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300101}
102
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300103static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
104 size_t len, u8 mac[16])
105{
106 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
107 struct hash_desc desc;
108 struct scatterlist sg;
109 int err;
110
111 if (len > CMAC_MSG_MAX)
112 return -EFBIG;
113
114 if (!tfm) {
115 BT_ERR("tfm %p", tfm);
116 return -EINVAL;
117 }
118
119 desc.tfm = tfm;
120 desc.flags = 0;
121
122 crypto_hash_init(&desc);
123
124 /* Swap key and message from LSB to MSB */
125 swap_buf(k, tmp, 16);
126 swap_buf(m, msg_msb, len);
127
128 BT_DBG("msg (len %zu) %*phN", len, (int) len, m);
129 BT_DBG("key %16phN", k);
130
131 err = crypto_hash_setkey(tfm, tmp, 16);
132 if (err) {
133 BT_ERR("cipher setkey failed: %d", err);
134 return err;
135 }
136
137 sg_init_one(&sg, msg_msb, len);
138
139 err = crypto_hash_update(&desc, &sg, len);
140 if (err) {
141 BT_ERR("Hash update error %d", err);
142 return err;
143 }
144
145 err = crypto_hash_final(&desc, mac_msb);
146 if (err) {
147 BT_ERR("Hash final error %d", err);
148 return err;
149 }
150
151 swap_buf(mac_msb, mac, 16);
152
153 BT_DBG("mac %16phN", mac);
154
155 return 0;
156}
157
158static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
159 const u8 x[16], u8 z, u8 res[16])
160{
161 u8 m[65];
162 int err;
163
164 BT_DBG("u %32phN", u);
165 BT_DBG("v %32phN", v);
166 BT_DBG("x %16phN z %02x", x, z);
167
168 m[0] = z;
169 memcpy(m + 1, v, 32);
170 memcpy(m + 33, u, 32);
171
172 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
173 if (err)
174 return err;
175
176 BT_DBG("res %16phN", res);
177
178 return err;
179}
180
Johan Hedberg760b0182014-06-06 11:44:05 +0300181static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
182 u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
183{
184 /* The btle, salt and length "magic" values are as defined in
185 * the SMP section of the Bluetooth core specification. In ASCII
186 * the btle value ends up being 'btle'. The salt is just a
187 * random number whereas length is the value 256 in little
188 * endian format.
189 */
190 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
191 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
192 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
193 const u8 length[2] = { 0x00, 0x01 };
194 u8 m[53], t[16];
195 int err;
196
197 BT_DBG("w %32phN", w);
198 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
199 BT_DBG("a1 %7phN a2 %7phN", a1, a2);
200
201 err = aes_cmac(tfm_cmac, salt, w, 32, t);
202 if (err)
203 return err;
204
205 BT_DBG("t %16phN", t);
206
207 memcpy(m, length, 2);
208 memcpy(m + 2, a2, 7);
209 memcpy(m + 9, a1, 7);
210 memcpy(m + 16, n2, 16);
211 memcpy(m + 32, n1, 16);
212 memcpy(m + 48, btle, 4);
213
214 m[52] = 0; /* Counter */
215
216 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
217 if (err)
218 return err;
219
220 BT_DBG("mackey %16phN", mackey);
221
222 m[52] = 1; /* Counter */
223
224 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
225 if (err)
226 return err;
227
228 BT_DBG("ltk %16phN", ltk);
229
230 return 0;
231}
232
233static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
234 const u8 n1[16], u8 n2[16], const u8 r[16],
235 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
236 u8 res[16])
237{
238 u8 m[65];
239 int err;
240
241 BT_DBG("w %16phN", w);
242 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
243 BT_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
244
245 memcpy(m, a2, 7);
246 memcpy(m + 7, a1, 7);
247 memcpy(m + 14, io_cap, 3);
248 memcpy(m + 17, r, 16);
249 memcpy(m + 33, n2, 16);
250 memcpy(m + 49, n1, 16);
251
252 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
253 if (err)
254 return err;
255
256 BT_DBG("res %16phN", res);
257
258 return err;
259}
260
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300261static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
262 const u8 x[16], const u8 y[16], u32 *val)
263{
264 u8 m[80], tmp[16];
265 int err;
266
267 BT_DBG("u %32phN", u);
268 BT_DBG("v %32phN", v);
269 BT_DBG("x %16phN y %16phN", x, y);
270
271 memcpy(m, y, 16);
272 memcpy(m + 16, v, 32);
273 memcpy(m + 48, u, 32);
274
275 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
276 if (err)
277 return err;
278
279 *val = get_unaligned_le32(tmp);
280 *val %= 1000000;
281
282 BT_DBG("val %06u", *val);
283
284 return 0;
285}
286
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300287static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
288{
289 struct blkcipher_desc desc;
290 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200291 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200292 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300293
294 if (tfm == NULL) {
295 BT_ERR("tfm %p", tfm);
296 return -EINVAL;
297 }
298
299 desc.tfm = tfm;
300 desc.flags = 0;
301
Johan Hedberg943a7322014-03-18 12:58:24 +0200302 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300303 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200304
305 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300306 if (err) {
307 BT_ERR("cipher setkey failed: %d", err);
308 return err;
309 }
310
Johan Hedberg943a7322014-03-18 12:58:24 +0200311 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300312 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200313
314 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300315
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300316 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
317 if (err)
318 BT_ERR("Encrypt data error %d", err);
319
Johan Hedberg943a7322014-03-18 12:58:24 +0200320 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300321 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200322
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300323 return err;
324}
325
Johan Hedberg6a770832014-06-06 11:54:04 +0300326static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
327 const u8 key_id[4], u8 res[16])
328{
329 int err;
330
331 BT_DBG("w %16phN key_id %4phN", w, key_id);
332
333 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
334 if (err)
335 return err;
336
337 BT_DBG("res %16phN", res);
338
339 return err;
340}
341
Johan Hedberg60478052014-02-18 10:19:31 +0200342static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
343{
Johan Hedberg943a7322014-03-18 12:58:24 +0200344 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200345 int err;
346
347 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200348 memcpy(_res, r, 3);
349 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200350
Johan Hedberg943a7322014-03-18 12:58:24 +0200351 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200352 if (err) {
353 BT_ERR("Encrypt error");
354 return err;
355 }
356
357 /* The output of the random address function ah is:
358 * ah(h, r) = e(k, r') mod 2^24
359 * The output of the security function e is then truncated to 24 bits
360 * by taking the least significant 24 bits of the output of e as the
361 * result of ah.
362 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200363 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200364
365 return 0;
366}
367
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300368bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200369{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300370 struct l2cap_chan *chan = hdev->smp_data;
371 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200372 u8 hash[3];
373 int err;
374
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300375 if (!chan || !chan->data)
376 return false;
377
378 tfm = chan->data;
379
Johan Hedberg60478052014-02-18 10:19:31 +0200380 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
381
382 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
383 if (err)
384 return false;
385
386 return !memcmp(bdaddr->b, hash, 3);
387}
388
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300389int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200390{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300391 struct l2cap_chan *chan = hdev->smp_data;
392 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200393 int err;
394
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300395 if (!chan || !chan->data)
396 return -EOPNOTSUPP;
397
398 tfm = chan->data;
399
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200400 get_random_bytes(&rpa->b[3], 3);
401
402 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
403 rpa->b[5] |= 0x40; /* Set second most significant bit */
404
405 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
406 if (err < 0)
407 return err;
408
409 BT_DBG("RPA %pMR", rpa);
410
411 return 0;
412}
413
Johan Hedberge491eaf2014-10-25 21:15:37 +0200414static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
415 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
416 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300417{
418 u8 p1[16], p2[16];
419 int err;
420
421 memset(p1, 0, 16);
422
423 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200424 p1[0] = _iat;
425 p1[1] = _rat;
426 memcpy(p1 + 2, preq, 7);
427 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300428
429 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200430 memcpy(p2, ra, 6);
431 memcpy(p2 + 6, ia, 6);
432 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300433
434 /* res = r XOR p1 */
435 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
436
437 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200438 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300439 if (err) {
440 BT_ERR("Encrypt data error");
441 return err;
442 }
443
444 /* res = res XOR p2 */
445 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
446
447 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200448 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300449 if (err)
450 BT_ERR("Encrypt data error");
451
452 return err;
453}
454
Johan Hedberge491eaf2014-10-25 21:15:37 +0200455static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
456 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300457{
458 int err;
459
460 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200461 memcpy(_r, r2, 8);
462 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300463
Johan Hedberge491eaf2014-10-25 21:15:37 +0200464 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300465 if (err)
466 BT_ERR("Encrypt data error");
467
468 return err;
469}
470
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300471static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
472{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300473 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300474 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300475 struct kvec iv[2];
476 struct msghdr msg;
477
478 if (!chan)
479 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300480
481 BT_DBG("code 0x%2.2x", code);
482
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300483 iv[0].iov_base = &code;
484 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300485
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300486 iv[1].iov_base = data;
487 iv[1].iov_len = len;
488
489 memset(&msg, 0, sizeof(msg));
490
491 msg.msg_iov = (struct iovec *) &iv;
492 msg.msg_iovlen = 2;
493
494 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300495
Johan Hedbergb68fda62014-08-11 22:06:40 +0300496 if (!chan->data)
497 return;
498
499 smp = chan->data;
500
501 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300502 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300503}
504
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300505static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800506{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300507 if (authreq & SMP_AUTH_MITM) {
508 if (authreq & SMP_AUTH_SC)
509 return BT_SECURITY_FIPS;
510 else
511 return BT_SECURITY_HIGH;
512 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800513 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300514 }
Brian Gix2b64d152011-12-21 16:12:12 -0800515}
516
517static __u8 seclevel_to_authreq(__u8 sec_level)
518{
519 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300520 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800521 case BT_SECURITY_HIGH:
522 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
523 case BT_SECURITY_MEDIUM:
524 return SMP_AUTH_BONDING;
525 default:
526 return SMP_AUTH_NONE;
527 }
528}
529
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300530static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700531 struct smp_cmd_pairing *req,
532 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300533{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300534 struct l2cap_chan *chan = conn->smp;
535 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200536 struct hci_conn *hcon = conn->hcon;
537 struct hci_dev *hdev = hcon->hdev;
538 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300539
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300540 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700541 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
542 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300543 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800544 } else {
545 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300546 }
547
Johan Hedbergfd349c02014-02-18 10:19:36 +0200548 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
549 remote_dist |= SMP_DIST_ID_KEY;
550
Johan Hedberg863efaf2014-02-22 19:06:32 +0200551 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
552 local_dist |= SMP_DIST_ID_KEY;
553
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300554 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
555 if ((authreq & SMP_AUTH_SC) &&
556 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
557 local_dist |= SMP_DIST_LINK_KEY;
558 remote_dist |= SMP_DIST_LINK_KEY;
559 }
560 } else {
561 authreq &= ~SMP_AUTH_SC;
562 }
563
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300564 if (rsp == NULL) {
565 req->io_capability = conn->hcon->io_capability;
566 req->oob_flag = SMP_OOB_NOT_PRESENT;
567 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200568 req->init_key_dist = local_dist;
569 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300570 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200571
572 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300573 return;
574 }
575
576 rsp->io_capability = conn->hcon->io_capability;
577 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
578 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200579 rsp->init_key_dist = req->init_key_dist & remote_dist;
580 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300581 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200582
583 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300584}
585
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300586static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
587{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300588 struct l2cap_chan *chan = conn->smp;
589 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300590
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300591 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700592 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300593 return SMP_ENC_KEY_SIZE;
594
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300595 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300596
597 return 0;
598}
599
Johan Hedberg6f48e262014-08-11 22:06:44 +0300600static void smp_chan_destroy(struct l2cap_conn *conn)
601{
602 struct l2cap_chan *chan = conn->smp;
603 struct smp_chan *smp = chan->data;
604 bool complete;
605
606 BUG_ON(!smp);
607
608 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300609
Johan Hedberg6f48e262014-08-11 22:06:44 +0300610 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
611 mgmt_smp_complete(conn->hcon, complete);
612
613 kfree(smp->csrk);
614 kfree(smp->slave_csrk);
Johan Hedberg6a770832014-06-06 11:54:04 +0300615 kfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300616
617 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300618 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300619
620 /* If pairing failed clean up any keys we might have */
621 if (!complete) {
622 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200623 list_del_rcu(&smp->ltk->list);
624 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300625 }
626
627 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200628 list_del_rcu(&smp->slave_ltk->list);
629 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300630 }
631
632 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200633 list_del_rcu(&smp->remote_irk->list);
634 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300635 }
636 }
637
638 chan->data = NULL;
639 kfree(smp);
640 hci_conn_drop(conn->hcon);
641}
642
Johan Hedberg84794e12013-11-06 11:24:57 +0200643static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800644{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200645 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300646 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200647
Johan Hedberg84794e12013-11-06 11:24:57 +0200648 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800649 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700650 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800651
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700652 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700653 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300654
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300655 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300656 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800657}
658
Brian Gix2b64d152011-12-21 16:12:12 -0800659#define JUST_WORKS 0x00
660#define JUST_CFM 0x01
661#define REQ_PASSKEY 0x02
662#define CFM_PASSKEY 0x03
663#define REQ_OOB 0x04
664#define OVERLAP 0xFF
665
666static const u8 gen_method[5][5] = {
667 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
668 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
669 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
670 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
671 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
672};
673
Johan Hedberg581370c2014-06-17 13:07:38 +0300674static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
675{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300676 /* If either side has unknown io_caps, use JUST_CFM (which gets
677 * converted later to JUST_WORKS if we're initiators.
678 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300679 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
680 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300681 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300682
683 return gen_method[remote_io][local_io];
684}
685
Brian Gix2b64d152011-12-21 16:12:12 -0800686static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
687 u8 local_io, u8 remote_io)
688{
689 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300690 struct l2cap_chan *chan = conn->smp;
691 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800692 u32 passkey = 0;
693 int ret = 0;
694
695 /* Initialize key for JUST WORKS */
696 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300697 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800698
699 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
700
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300701 /* If neither side wants MITM, either "just" confirm an incoming
702 * request or use just-works for outgoing ones. The JUST_CFM
703 * will be converted to JUST_WORKS if necessary later in this
704 * function. If either side has MITM look up the method from the
705 * table.
706 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300707 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300708 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800709 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300710 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800711
Johan Hedberga82505c2014-03-24 14:39:07 +0200712 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300713 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
714 &smp->flags))
715 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200716
Johan Hedberg02f3e252014-07-16 15:09:13 +0300717 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300718 if (smp->method == JUST_CFM &&
719 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
720 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300721
Brian Gix2b64d152011-12-21 16:12:12 -0800722 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300723 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300724 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800725 return 0;
726 }
727
728 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300729 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300730 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300731 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
732 hcon->pending_sec_level = BT_SECURITY_HIGH;
733 }
Brian Gix2b64d152011-12-21 16:12:12 -0800734
735 /* If both devices have Keyoard-Display I/O, the master
736 * Confirms and the slave Enters the passkey.
737 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300738 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300739 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300740 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800741 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300742 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800743 }
744
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200745 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300746 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200747 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800748 get_random_bytes(&passkey, sizeof(passkey));
749 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200750 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800751 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300752 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800753 }
754
Johan Hedberg783e0572014-05-31 18:48:26 +0300755 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700756 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200757 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300758 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200759 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
760 hcon->type, hcon->dst_type,
761 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800762 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200763 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200764 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200765 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800766
Brian Gix2b64d152011-12-21 16:12:12 -0800767 return ret;
768}
769
Johan Hedberg1cc61142014-05-20 09:45:52 +0300770static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300771{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300772 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300773 struct smp_cmd_pairing_confirm cp;
774 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300775
776 BT_DBG("conn %p", conn);
777
Johan Hedberge491eaf2014-10-25 21:15:37 +0200778 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200779 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200780 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
781 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300782 if (ret)
783 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300784
Johan Hedberg4a74d652014-05-20 09:45:50 +0300785 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800786
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300787 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
788
Johan Hedbergb28b4942014-09-05 22:19:55 +0300789 if (conn->hcon->out)
790 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
791 else
792 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
793
Johan Hedberg1cc61142014-05-20 09:45:52 +0300794 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300795}
796
Johan Hedberg861580a2014-05-20 09:45:51 +0300797static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300798{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300799 struct l2cap_conn *conn = smp->conn;
800 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300801 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300802 int ret;
803
Johan Hedbergec70f362014-06-27 14:23:04 +0300804 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300805 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300806
807 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
808
Johan Hedberge491eaf2014-10-25 21:15:37 +0200809 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200810 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200811 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300812 if (ret)
813 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300814
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300815 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
816 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300817 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300818 }
819
820 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800821 u8 stk[16];
822 __le64 rand = 0;
823 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300824
Johan Hedberge491eaf2014-10-25 21:15:37 +0200825 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300826
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300827 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300828 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300829
Johan Hedberg861580a2014-05-20 09:45:51 +0300830 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
831 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300832
833 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300834 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300835 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300836 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300837 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800838 __le64 rand = 0;
839 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300840
Johan Hedberg943a7322014-03-18 12:58:24 +0200841 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
842 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300843
Johan Hedberge491eaf2014-10-25 21:15:37 +0200844 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300845
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300846 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700847 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300848
Johan Hedbergfff34902014-06-10 15:19:50 +0300849 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
850 auth = 1;
851 else
852 auth = 0;
853
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300854 /* Even though there's no _SLAVE suffix this is the
855 * slave STK we're adding for later lookup (the master
856 * STK never needs to be stored).
857 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700858 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300859 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300860 }
861
Johan Hedberg861580a2014-05-20 09:45:51 +0300862 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300863}
864
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300865static void smp_notify_keys(struct l2cap_conn *conn)
866{
867 struct l2cap_chan *chan = conn->smp;
868 struct smp_chan *smp = chan->data;
869 struct hci_conn *hcon = conn->hcon;
870 struct hci_dev *hdev = hcon->hdev;
871 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
872 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
873 bool persistent;
874
875 if (smp->remote_irk) {
876 mgmt_new_irk(hdev, smp->remote_irk);
877 /* Now that user space can be considered to know the
878 * identity address track the connection based on it
879 * from now on.
880 */
881 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
882 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300883 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300884
885 /* When receiving an indentity resolving key for
886 * a remote device that does not use a resolvable
887 * private address, just remove the key so that
888 * it is possible to use the controller white
889 * list for scanning.
890 *
891 * Userspace will have been told to not store
892 * this key at this point. So it is safe to
893 * just remove it.
894 */
895 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200896 list_del_rcu(&smp->remote_irk->list);
897 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300898 smp->remote_irk = NULL;
899 }
900 }
901
902 /* The LTKs and CSRKs should be persistent only if both sides
903 * had the bonding bit set in their authentication requests.
904 */
905 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
906
907 if (smp->csrk) {
908 smp->csrk->bdaddr_type = hcon->dst_type;
909 bacpy(&smp->csrk->bdaddr, &hcon->dst);
910 mgmt_new_csrk(hdev, smp->csrk, persistent);
911 }
912
913 if (smp->slave_csrk) {
914 smp->slave_csrk->bdaddr_type = hcon->dst_type;
915 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
916 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
917 }
918
919 if (smp->ltk) {
920 smp->ltk->bdaddr_type = hcon->dst_type;
921 bacpy(&smp->ltk->bdaddr, &hcon->dst);
922 mgmt_new_ltk(hdev, smp->ltk, persistent);
923 }
924
925 if (smp->slave_ltk) {
926 smp->slave_ltk->bdaddr_type = hcon->dst_type;
927 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
928 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
929 }
Johan Hedberg6a770832014-06-06 11:54:04 +0300930
931 if (smp->link_key) {
932 hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
933 smp->link_key, HCI_LK_AUTH_COMBINATION_P256,
934 0, NULL);
935 }
936}
937
938static void sc_generate_link_key(struct smp_chan *smp)
939{
940 /* These constants are as specified in the core specification.
941 * In ASCII they spell out to 'tmp1' and 'lebr'.
942 */
943 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
944 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
945
946 smp->link_key = kzalloc(16, GFP_KERNEL);
947 if (!smp->link_key)
948 return;
949
950 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
951 kfree(smp->link_key);
952 smp->link_key = NULL;
953 return;
954 }
955
956 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
957 kfree(smp->link_key);
958 smp->link_key = NULL;
959 return;
960 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300961}
962
Johan Hedbergb28b4942014-09-05 22:19:55 +0300963static void smp_allow_key_dist(struct smp_chan *smp)
964{
965 /* Allow the first expected phase 3 PDU. The rest of the PDUs
966 * will be allowed in each PDU handler to ensure we receive
967 * them in the correct order.
968 */
969 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
970 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
971 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
972 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
973 else if (smp->remote_key_dist & SMP_DIST_SIGN)
974 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
975}
976
Johan Hedbergd6268e82014-09-05 22:19:51 +0300977static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300978{
979 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300980 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300981 struct hci_conn *hcon = conn->hcon;
982 struct hci_dev *hdev = hcon->hdev;
983 __u8 *keydist;
984
985 BT_DBG("conn %p", conn);
986
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300987 rsp = (void *) &smp->prsp[1];
988
989 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300990 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
991 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300992 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300993 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300994
995 req = (void *) &smp->preq[1];
996
997 if (hcon->out) {
998 keydist = &rsp->init_key_dist;
999 *keydist &= req->init_key_dist;
1000 } else {
1001 keydist = &rsp->resp_key_dist;
1002 *keydist &= req->resp_key_dist;
1003 }
1004
Johan Hedberg6a770832014-06-06 11:54:04 +03001005 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1006 if (*keydist & SMP_DIST_LINK_KEY)
1007 sc_generate_link_key(smp);
1008
1009 /* Clear the keys which are generated but not distributed */
1010 *keydist &= ~SMP_SC_NO_DIST;
1011 }
1012
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001013 BT_DBG("keydist 0x%x", *keydist);
1014
1015 if (*keydist & SMP_DIST_ENC_KEY) {
1016 struct smp_cmd_encrypt_info enc;
1017 struct smp_cmd_master_ident ident;
1018 struct smp_ltk *ltk;
1019 u8 authenticated;
1020 __le16 ediv;
1021 __le64 rand;
1022
1023 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1024 get_random_bytes(&ediv, sizeof(ediv));
1025 get_random_bytes(&rand, sizeof(rand));
1026
1027 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1028
1029 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1030 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1031 SMP_LTK_SLAVE, authenticated, enc.ltk,
1032 smp->enc_key_size, ediv, rand);
1033 smp->slave_ltk = ltk;
1034
1035 ident.ediv = ediv;
1036 ident.rand = rand;
1037
1038 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1039
1040 *keydist &= ~SMP_DIST_ENC_KEY;
1041 }
1042
1043 if (*keydist & SMP_DIST_ID_KEY) {
1044 struct smp_cmd_ident_addr_info addrinfo;
1045 struct smp_cmd_ident_info idinfo;
1046
1047 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1048
1049 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1050
1051 /* The hci_conn contains the local identity address
1052 * after the connection has been established.
1053 *
1054 * This is true even when the connection has been
1055 * established using a resolvable random address.
1056 */
1057 bacpy(&addrinfo.bdaddr, &hcon->src);
1058 addrinfo.addr_type = hcon->src_type;
1059
1060 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1061 &addrinfo);
1062
1063 *keydist &= ~SMP_DIST_ID_KEY;
1064 }
1065
1066 if (*keydist & SMP_DIST_SIGN) {
1067 struct smp_cmd_sign_info sign;
1068 struct smp_csrk *csrk;
1069
1070 /* Generate a new random key */
1071 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1072
1073 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1074 if (csrk) {
1075 csrk->master = 0x00;
1076 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1077 }
1078 smp->slave_csrk = csrk;
1079
1080 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1081
1082 *keydist &= ~SMP_DIST_SIGN;
1083 }
1084
1085 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001086 if (smp->remote_key_dist & KEY_DIST_MASK) {
1087 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001088 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001089 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001090
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001091 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1092 smp_notify_keys(conn);
1093
1094 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001095}
1096
Johan Hedbergb68fda62014-08-11 22:06:40 +03001097static void smp_timeout(struct work_struct *work)
1098{
1099 struct smp_chan *smp = container_of(work, struct smp_chan,
1100 security_timer.work);
1101 struct l2cap_conn *conn = smp->conn;
1102
1103 BT_DBG("conn %p", conn);
1104
Johan Hedberg1e91c292014-08-18 20:33:29 +03001105 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001106}
1107
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001108static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1109{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001110 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001111 struct smp_chan *smp;
1112
Marcel Holtmannf1560462013-10-13 05:43:25 -07001113 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001114 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001115 return NULL;
1116
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001117 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1118 if (IS_ERR(smp->tfm_aes)) {
1119 BT_ERR("Unable to create ECB crypto context");
1120 kfree(smp);
1121 return NULL;
1122 }
1123
Johan Hedberg407cecf2014-05-02 14:19:47 +03001124 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1125 if (IS_ERR(smp->tfm_cmac)) {
1126 BT_ERR("Unable to create CMAC crypto context");
1127 crypto_free_blkcipher(smp->tfm_aes);
1128 kfree(smp);
1129 return NULL;
1130 }
1131
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001132 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001133 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001134
Johan Hedbergb28b4942014-09-05 22:19:55 +03001135 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1136
Johan Hedbergb68fda62014-08-11 22:06:40 +03001137 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1138
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001139 hci_conn_hold(conn->hcon);
1140
1141 return smp;
1142}
1143
Johan Hedberg760b0182014-06-06 11:44:05 +03001144static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1145{
1146 struct hci_conn *hcon = smp->conn->hcon;
1147 u8 *na, *nb, a[7], b[7];
1148
1149 if (hcon->out) {
1150 na = smp->prnd;
1151 nb = smp->rrnd;
1152 } else {
1153 na = smp->rrnd;
1154 nb = smp->prnd;
1155 }
1156
1157 memcpy(a, &hcon->init_addr, 6);
1158 memcpy(b, &hcon->resp_addr, 6);
1159 a[6] = hcon->init_addr_type;
1160 b[6] = hcon->resp_addr_type;
1161
1162 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1163}
1164
1165static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1166{
1167 struct hci_conn *hcon = smp->conn->hcon;
1168 struct smp_cmd_dhkey_check check;
1169 u8 a[7], b[7], *local_addr, *remote_addr;
1170 u8 io_cap[3], r[16];
1171
1172 switch (mgmt_op) {
1173 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1174 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1175 return 0;
1176 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1177 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1178 return 0;
1179 }
1180
1181 memcpy(a, &hcon->init_addr, 6);
1182 memcpy(b, &hcon->resp_addr, 6);
1183 a[6] = hcon->init_addr_type;
1184 b[6] = hcon->resp_addr_type;
1185
1186 if (hcon->out) {
1187 local_addr = a;
1188 remote_addr = b;
1189 memcpy(io_cap, &smp->preq[1], 3);
1190 } else {
1191 local_addr = b;
1192 remote_addr = a;
1193 memcpy(io_cap, &smp->prsp[1], 3);
1194 }
1195
1196 memcpy(r, &passkey, sizeof(passkey));
1197 memset(r + sizeof(passkey), 0, sizeof(r) - sizeof(passkey));
1198
1199 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1200 local_addr, remote_addr, check.e);
1201
1202 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
1203
1204 return 0;
1205}
1206
Brian Gix2b64d152011-12-21 16:12:12 -08001207int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1208{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001209 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001210 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001211 struct smp_chan *smp;
1212 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001213 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001214
1215 BT_DBG("");
1216
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001217 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001218 return -ENOTCONN;
1219
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001220 chan = conn->smp;
1221 if (!chan)
1222 return -ENOTCONN;
1223
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001224 l2cap_chan_lock(chan);
1225 if (!chan->data) {
1226 err = -ENOTCONN;
1227 goto unlock;
1228 }
1229
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001230 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001231
Johan Hedberg760b0182014-06-06 11:44:05 +03001232 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1233 err = sc_user_reply(smp, mgmt_op, passkey);
1234 goto unlock;
1235 }
1236
Brian Gix2b64d152011-12-21 16:12:12 -08001237 switch (mgmt_op) {
1238 case MGMT_OP_USER_PASSKEY_REPLY:
1239 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001240 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001241 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001242 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001243 /* Fall Through */
1244 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001245 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001246 break;
1247 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1248 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001249 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001250 err = 0;
1251 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001252 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001253 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001254 err = -EOPNOTSUPP;
1255 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001256 }
1257
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001258 err = 0;
1259
Brian Gix2b64d152011-12-21 16:12:12 -08001260 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001261 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1262 u8 rsp = smp_confirm(smp);
1263 if (rsp)
1264 smp_failure(conn, rsp);
1265 }
Brian Gix2b64d152011-12-21 16:12:12 -08001266
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001267unlock:
1268 l2cap_chan_unlock(chan);
1269 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001270}
1271
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001272static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001273{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001274 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001275 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001276 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001277 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001278 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001279 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001280
1281 BT_DBG("conn %p", conn);
1282
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001283 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001284 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001285
Johan Hedberg40bef302014-07-16 11:42:27 +03001286 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001287 return SMP_CMD_NOTSUPP;
1288
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001289 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001290 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001291 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001292 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001293
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001294 if (!smp)
1295 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001296
Johan Hedbergc05b9332014-09-10 17:37:42 -07001297 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001298 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001299
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001300 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001301 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001302 return SMP_PAIRING_NOTSUPP;
1303
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001304 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1305 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001306 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001307
Johan Hedberg5be5e272014-09-10 17:58:54 -07001308 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001309 sec_level = BT_SECURITY_MEDIUM;
1310 else
1311 sec_level = authreq_to_seclevel(auth);
1312
Johan Hedbergc7262e72014-06-17 13:07:37 +03001313 if (sec_level > conn->hcon->pending_sec_level)
1314 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001315
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001316 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001317 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1318 u8 method;
1319
1320 method = get_auth_method(smp, conn->hcon->io_capability,
1321 req->io_capability);
1322 if (method == JUST_WORKS || method == JUST_CFM)
1323 return SMP_AUTH_REQUIREMENTS;
1324 }
1325
Brian Gix2b64d152011-12-21 16:12:12 -08001326 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001327
Johan Hedberg65668772014-05-16 11:03:34 +03001328 if (rsp.auth_req & SMP_AUTH_SC)
1329 set_bit(SMP_FLAG_SC, &smp->flags);
1330
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001331 key_size = min(req->max_key_size, rsp.max_key_size);
1332 if (check_enc_key_size(conn, key_size))
1333 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001334
Johan Hedberge84a6b12013-12-02 10:49:03 +02001335 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001336
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001337 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1338 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001339
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001340 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001341
1342 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1343
1344 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1345 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1346 /* Clear bits which are generated but not distributed */
1347 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1348 /* Wait for Public Key from Initiating Device */
1349 return 0;
1350 } else {
1351 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1352 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001353
Brian Gix2b64d152011-12-21 16:12:12 -08001354 /* Request setup of TK */
1355 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1356 if (ret)
1357 return SMP_UNSPECIFIED;
1358
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001359 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001360}
1361
Johan Hedberg3b191462014-06-06 10:50:15 +03001362static u8 sc_send_public_key(struct smp_chan *smp)
1363{
1364 BT_DBG("");
1365
1366 /* Generate local key pair for Secure Connections */
1367 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1368 return SMP_UNSPECIFIED;
1369
1370 BT_DBG("Local Public Key X: %32phN", smp->local_pk);
1371 BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1372 BT_DBG("Local Private Key: %32phN", smp->local_sk);
1373
1374 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1375
1376 return 0;
1377}
1378
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001379static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001380{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001381 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001382 struct l2cap_chan *chan = conn->smp;
1383 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001384 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001385 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001386 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001387
1388 BT_DBG("conn %p", conn);
1389
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001390 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001391 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001392
Johan Hedberg40bef302014-07-16 11:42:27 +03001393 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001394 return SMP_CMD_NOTSUPP;
1395
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001396 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001397
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001398 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001399
1400 key_size = min(req->max_key_size, rsp->max_key_size);
1401 if (check_enc_key_size(conn, key_size))
1402 return SMP_ENC_KEY_SIZE;
1403
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001404 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001405
Johan Hedberg65668772014-05-16 11:03:34 +03001406 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1407 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001408 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1409 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001410
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001411 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001412 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1413 u8 method;
1414
1415 method = get_auth_method(smp, req->io_capability,
1416 rsp->io_capability);
1417 if (method == JUST_WORKS || method == JUST_CFM)
1418 return SMP_AUTH_REQUIREMENTS;
1419 }
1420
Johan Hedberge84a6b12013-12-02 10:49:03 +02001421 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001422
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001423 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1424 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001425
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001426 /* Update remote key distribution in case the remote cleared
1427 * some bits that we had enabled in our request.
1428 */
1429 smp->remote_key_dist &= rsp->resp_key_dist;
1430
Johan Hedberg3b191462014-06-06 10:50:15 +03001431 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1432 /* Clear bits which are generated but not distributed */
1433 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1434 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1435 return sc_send_public_key(smp);
1436 }
1437
Johan Hedbergc05b9332014-09-10 17:37:42 -07001438 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001439
Johan Hedberg476585e2012-06-06 18:54:15 +08001440 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001441 if (ret)
1442 return SMP_UNSPECIFIED;
1443
Johan Hedberg4a74d652014-05-20 09:45:50 +03001444 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001445
1446 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001447 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001448 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001449
1450 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001451}
1452
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001453static u8 sc_check_confirm(struct smp_chan *smp)
1454{
1455 struct l2cap_conn *conn = smp->conn;
1456
1457 BT_DBG("");
1458
1459 /* Public Key exchange must happen before any other steps */
1460 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1461 return SMP_UNSPECIFIED;
1462
1463 if (conn->hcon->out) {
1464 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1465 smp->prnd);
1466 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1467 }
1468
1469 return 0;
1470}
1471
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001472static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001473{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001474 struct l2cap_chan *chan = conn->smp;
1475 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001476
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001477 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1478
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001479 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001480 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001481
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001482 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1483 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001484
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001485 if (test_bit(SMP_FLAG_SC, &smp->flags))
1486 return sc_check_confirm(smp);
1487
Johan Hedbergb28b4942014-09-05 22:19:55 +03001488 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001489 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1490 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001491 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1492 return 0;
1493 }
1494
1495 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001496 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001497 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001498 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001499
1500 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001501}
1502
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001503static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001504{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001505 struct l2cap_chan *chan = conn->smp;
1506 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001507 struct hci_conn *hcon = conn->hcon;
1508 u8 *pkax, *pkbx, *na, *nb;
1509 u32 passkey;
1510 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001511
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001512 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001513
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001514 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001515 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001516
Johan Hedberg943a7322014-03-18 12:58:24 +02001517 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001518 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001519
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001520 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1521 return smp_random(smp);
1522
1523 if (hcon->out) {
1524 u8 cfm[16];
1525
1526 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1527 smp->rrnd, 0, cfm);
1528 if (err)
1529 return SMP_UNSPECIFIED;
1530
1531 if (memcmp(smp->pcnf, cfm, 16))
1532 return SMP_CONFIRM_FAILED;
1533
1534 pkax = smp->local_pk;
1535 pkbx = smp->remote_pk;
1536 na = smp->prnd;
1537 nb = smp->rrnd;
1538 } else {
1539 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1540 smp->prnd);
1541 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1542
1543 pkax = smp->remote_pk;
1544 pkbx = smp->local_pk;
1545 na = smp->rrnd;
1546 nb = smp->prnd;
1547 }
1548
Johan Hedberg760b0182014-06-06 11:44:05 +03001549 /* Generate MacKey and LTK */
1550 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1551 if (err)
1552 return SMP_UNSPECIFIED;
1553
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001554 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
1555 if (err)
1556 return SMP_UNSPECIFIED;
1557
1558 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
1559 hcon->type, hcon->dst_type,
1560 passkey, 0);
1561 if (err)
1562 return SMP_UNSPECIFIED;
1563
1564 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001565}
1566
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001567static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001568{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001569 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001570 struct hci_conn *hcon = conn->hcon;
1571
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001572 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001573 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001574 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001575
Johan Hedberga6f78332014-09-10 17:37:45 -07001576 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001577 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001578
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001579 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001580 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001581
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001582 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1583 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001584
Johan Hedbergfe59a052014-07-01 19:14:12 +03001585 /* We never store STKs for master role, so clear this flag */
1586 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1587
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001588 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001589}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001590
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001591bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1592 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001593{
1594 if (sec_level == BT_SECURITY_LOW)
1595 return true;
1596
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001597 /* If we're encrypted with an STK but the caller prefers using
1598 * LTK claim insufficient security. This way we allow the
1599 * connection to be re-encrypted with an LTK, even if the LTK
1600 * provides the same level of security. Only exception is if we
1601 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001602 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001603 if (key_pref == SMP_USE_LTK &&
1604 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001605 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001606 return false;
1607
Johan Hedberg854f4722014-07-01 18:40:20 +03001608 if (hcon->sec_level >= sec_level)
1609 return true;
1610
1611 return false;
1612}
1613
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001614static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001615{
1616 struct smp_cmd_security_req *rp = (void *) skb->data;
1617 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001618 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001619 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001620 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001621 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001622
1623 BT_DBG("conn %p", conn);
1624
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001625 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001626 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001627
Johan Hedberg40bef302014-07-16 11:42:27 +03001628 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001629 return SMP_CMD_NOTSUPP;
1630
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001631 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001632
Johan Hedberg5be5e272014-09-10 17:58:54 -07001633 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001634 sec_level = BT_SECURITY_MEDIUM;
1635 else
1636 sec_level = authreq_to_seclevel(auth);
1637
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001638 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03001639 return 0;
1640
Johan Hedbergc7262e72014-06-17 13:07:37 +03001641 if (sec_level > hcon->pending_sec_level)
1642 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001643
Johan Hedberg4dab7862012-06-07 14:58:37 +08001644 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001645 return 0;
1646
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001647 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001648 if (!smp)
1649 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001650
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001651 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001652 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001653 return SMP_PAIRING_NOTSUPP;
1654
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001655 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001656
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001657 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001658 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001659
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001660 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1661 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001662
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001663 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001664 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001665
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001666 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001667}
1668
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001669int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001670{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001671 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001672 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001673 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001674 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001675 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001676
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001677 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1678
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001679 /* This may be NULL if there's an unexpected disconnection */
1680 if (!conn)
1681 return 1;
1682
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001683 chan = conn->smp;
1684
Johan Hedberg757aee02013-04-24 13:05:32 +03001685 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001686 return 1;
1687
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001688 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001689 return 1;
1690
Johan Hedbergc7262e72014-06-17 13:07:37 +03001691 if (sec_level > hcon->pending_sec_level)
1692 hcon->pending_sec_level = sec_level;
1693
Johan Hedberg40bef302014-07-16 11:42:27 +03001694 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001695 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1696 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001697
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001698 l2cap_chan_lock(chan);
1699
1700 /* If SMP is already in progress ignore this request */
1701 if (chan->data) {
1702 ret = 0;
1703 goto unlock;
1704 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001705
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001706 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001707 if (!smp) {
1708 ret = 1;
1709 goto unlock;
1710 }
Brian Gix2b64d152011-12-21 16:12:12 -08001711
1712 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001713
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001714 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1715 authreq |= SMP_AUTH_SC;
1716
Johan Hedberg79897d22014-06-01 09:45:24 +03001717 /* Require MITM if IO Capability allows or the security level
1718 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001719 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001720 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001721 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001722 authreq |= SMP_AUTH_MITM;
1723
Johan Hedberg40bef302014-07-16 11:42:27 +03001724 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001725 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001726
Brian Gix2b64d152011-12-21 16:12:12 -08001727 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001728 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1729 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001730
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001731 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001732 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001733 } else {
1734 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001735 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001736 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001737 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001738 }
1739
Johan Hedberg4a74d652014-05-20 09:45:50 +03001740 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001741 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001742
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001743unlock:
1744 l2cap_chan_unlock(chan);
1745 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001746}
1747
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001748static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1749{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001750 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001751 struct l2cap_chan *chan = conn->smp;
1752 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001753
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001754 BT_DBG("conn %p", conn);
1755
1756 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001757 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001758
Johan Hedbergb28b4942014-09-05 22:19:55 +03001759 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001760
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001761 skb_pull(skb, sizeof(*rp));
1762
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001763 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001764
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001765 return 0;
1766}
1767
1768static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1769{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001770 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001771 struct l2cap_chan *chan = conn->smp;
1772 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001773 struct hci_dev *hdev = conn->hcon->hdev;
1774 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001775 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001776 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001777
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001778 BT_DBG("conn %p", conn);
1779
1780 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001781 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001782
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001783 /* Mark the information as received */
1784 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1785
Johan Hedbergb28b4942014-09-05 22:19:55 +03001786 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1787 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001788 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1789 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001790
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001791 skb_pull(skb, sizeof(*rp));
1792
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001793 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001794 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001795 authenticated, smp->tk, smp->enc_key_size,
1796 rp->ediv, rp->rand);
1797 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001798 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001799 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001800
1801 return 0;
1802}
1803
Johan Hedbergfd349c02014-02-18 10:19:36 +02001804static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1805{
1806 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001807 struct l2cap_chan *chan = conn->smp;
1808 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001809
1810 BT_DBG("");
1811
1812 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001813 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001814
Johan Hedbergb28b4942014-09-05 22:19:55 +03001815 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001816
Johan Hedbergfd349c02014-02-18 10:19:36 +02001817 skb_pull(skb, sizeof(*info));
1818
1819 memcpy(smp->irk, info->irk, 16);
1820
1821 return 0;
1822}
1823
1824static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1825 struct sk_buff *skb)
1826{
1827 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001828 struct l2cap_chan *chan = conn->smp;
1829 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001830 struct hci_conn *hcon = conn->hcon;
1831 bdaddr_t rpa;
1832
1833 BT_DBG("");
1834
1835 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001836 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001837
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001838 /* Mark the information as received */
1839 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1840
Johan Hedbergb28b4942014-09-05 22:19:55 +03001841 if (smp->remote_key_dist & SMP_DIST_SIGN)
1842 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1843
Johan Hedbergfd349c02014-02-18 10:19:36 +02001844 skb_pull(skb, sizeof(*info));
1845
Johan Hedberga9a58f82014-02-25 22:24:37 +02001846 /* Strictly speaking the Core Specification (4.1) allows sending
1847 * an empty address which would force us to rely on just the IRK
1848 * as "identity information". However, since such
1849 * implementations are not known of and in order to not over
1850 * complicate our implementation, simply pretend that we never
1851 * received an IRK for such a device.
1852 */
1853 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1854 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001855 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001856 }
1857
Johan Hedbergfd349c02014-02-18 10:19:36 +02001858 bacpy(&smp->id_addr, &info->bdaddr);
1859 smp->id_addr_type = info->addr_type;
1860
1861 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1862 bacpy(&rpa, &hcon->dst);
1863 else
1864 bacpy(&rpa, BDADDR_ANY);
1865
Johan Hedberg23d0e122014-02-19 14:57:46 +02001866 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1867 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001868
Johan Hedberg31dd6242014-06-27 14:23:02 +03001869distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001870 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1871 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001872
1873 return 0;
1874}
1875
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001876static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1877{
1878 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001879 struct l2cap_chan *chan = conn->smp;
1880 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001881 struct smp_csrk *csrk;
1882
1883 BT_DBG("conn %p", conn);
1884
1885 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001886 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001887
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001888 /* Mark the information as received */
1889 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1890
1891 skb_pull(skb, sizeof(*rp));
1892
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001893 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1894 if (csrk) {
1895 csrk->master = 0x01;
1896 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1897 }
1898 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001899 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001900
1901 return 0;
1902}
1903
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001904static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
1905{
1906 struct smp_cmd_public_key *key = (void *) skb->data;
1907 struct hci_conn *hcon = conn->hcon;
1908 struct l2cap_chan *chan = conn->smp;
1909 struct smp_chan *smp = chan->data;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03001910 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001911 int err;
1912
1913 BT_DBG("conn %p", conn);
1914
1915 if (skb->len < sizeof(*key))
1916 return SMP_INVALID_PARAMS;
1917
1918 memcpy(smp->remote_pk, key, 64);
1919
1920 /* Non-initiating device sends its public key after receiving
1921 * the key from the initiating device.
1922 */
1923 if (!hcon->out) {
1924 err = sc_send_public_key(smp);
1925 if (err)
1926 return err;
1927 }
1928
1929 BT_DBG("Remote Public Key X: %32phN", smp->remote_pk);
1930 BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
1931
1932 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
1933 return SMP_UNSPECIFIED;
1934
1935 BT_DBG("DHKey %32phN", smp->dhkey);
1936
1937 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
1938
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03001939 /* The Initiating device waits for the non-initiating device to
1940 * send the confirm value.
1941 */
1942 if (conn->hcon->out)
1943 return 0;
1944
1945 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
1946 0, cfm.confirm_val);
1947 if (err)
1948 return SMP_UNSPECIFIED;
1949
1950 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1951 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1952
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001953 return 0;
1954}
1955
Johan Hedberg6433a9a2014-06-06 11:47:30 +03001956static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
1957{
1958 struct smp_cmd_dhkey_check *check = (void *) skb->data;
1959 struct l2cap_chan *chan = conn->smp;
1960 struct hci_conn *hcon = conn->hcon;
1961 struct smp_chan *smp = chan->data;
1962 u8 a[7], b[7], *local_addr, *remote_addr;
1963 u8 io_cap[3], r[16], e[16];
1964 int err;
1965
1966 BT_DBG("conn %p", conn);
1967
1968 if (skb->len < sizeof(*check))
1969 return SMP_INVALID_PARAMS;
1970
1971 memcpy(a, &hcon->init_addr, 6);
1972 memcpy(b, &hcon->resp_addr, 6);
1973 a[6] = hcon->init_addr_type;
1974 b[6] = hcon->resp_addr_type;
1975
1976 if (hcon->out) {
1977 local_addr = a;
1978 remote_addr = b;
1979 memcpy(io_cap, &smp->prsp[1], 3);
1980 } else {
1981 local_addr = b;
1982 remote_addr = a;
1983 memcpy(io_cap, &smp->preq[1], 3);
1984 }
1985
1986 memset(r, 0, sizeof(r));
1987
1988 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
1989 io_cap, remote_addr, local_addr, e);
1990 if (err)
1991 return SMP_UNSPECIFIED;
1992
1993 if (memcmp(check->e, e, 16))
1994 return SMP_DHKEY_CHECK_FAILED;
1995
1996 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1997 SMP_LTK_P256, 0, smp->tk, smp->enc_key_size,
1998 0, 0);
1999
2000 if (hcon->out) {
2001 hci_le_start_enc(hcon, 0, 0, smp->tk);
2002 hcon->enc_key_size = smp->enc_key_size;
2003 }
2004
2005 return 0;
2006}
2007
Johan Hedberg4befb862014-08-11 22:06:38 +03002008static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002009{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002010 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002011 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002012 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002013 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002014 int err = 0;
2015
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002016 if (hcon->type != LE_LINK) {
2017 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03002018 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002019 }
2020
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002021 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002022 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002023
Marcel Holtmann06ae3312013-10-18 03:43:00 -07002024 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002025 reason = SMP_PAIRING_NOTSUPP;
2026 goto done;
2027 }
2028
Marcel Holtmann92381f52013-10-03 01:23:08 -07002029 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002030 skb_pull(skb, sizeof(code));
2031
Johan Hedbergb28b4942014-09-05 22:19:55 +03002032 smp = chan->data;
2033
2034 if (code > SMP_CMD_MAX)
2035 goto drop;
2036
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002037 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002038 goto drop;
2039
2040 /* If we don't have a context the only allowed commands are
2041 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002042 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002043 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2044 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002045
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002046 switch (code) {
2047 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002048 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002049 break;
2050
2051 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002052 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002053 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002054 break;
2055
2056 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002057 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002058 break;
2059
2060 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002061 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002062 break;
2063
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002064 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002065 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002066 break;
2067
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002068 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002069 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002070 break;
2071
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002072 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002073 reason = smp_cmd_encrypt_info(conn, skb);
2074 break;
2075
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002076 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002077 reason = smp_cmd_master_ident(conn, skb);
2078 break;
2079
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002080 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002081 reason = smp_cmd_ident_info(conn, skb);
2082 break;
2083
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002084 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002085 reason = smp_cmd_ident_addr_info(conn, skb);
2086 break;
2087
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002088 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002089 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002090 break;
2091
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002092 case SMP_CMD_PUBLIC_KEY:
2093 reason = smp_cmd_public_key(conn, skb);
2094 break;
2095
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002096 case SMP_CMD_DHKEY_CHECK:
2097 reason = smp_cmd_dhkey_check(conn, skb);
2098 break;
2099
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002100 default:
2101 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002102 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002103 goto done;
2104 }
2105
2106done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002107 if (!err) {
2108 if (reason)
2109 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002110 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002111 }
2112
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002113 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002114
2115drop:
2116 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2117 code, &hcon->dst);
2118 kfree_skb(skb);
2119 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002120}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002121
Johan Hedberg70db83c2014-08-08 09:37:16 +03002122static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2123{
2124 struct l2cap_conn *conn = chan->conn;
2125
2126 BT_DBG("chan %p", chan);
2127
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002128 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002129 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002130
Johan Hedberg70db83c2014-08-08 09:37:16 +03002131 conn->smp = NULL;
2132 l2cap_chan_put(chan);
2133}
2134
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002135static void smp_resume_cb(struct l2cap_chan *chan)
2136{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002137 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002138 struct l2cap_conn *conn = chan->conn;
2139 struct hci_conn *hcon = conn->hcon;
2140
2141 BT_DBG("chan %p", chan);
2142
Johan Hedberg86d14072014-08-11 22:06:43 +03002143 if (!smp)
2144 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002145
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002146 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2147 return;
2148
Johan Hedberg86d14072014-08-11 22:06:43 +03002149 cancel_delayed_work(&smp->security_timer);
2150
Johan Hedbergd6268e82014-09-05 22:19:51 +03002151 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002152}
2153
Johan Hedberg70db83c2014-08-08 09:37:16 +03002154static void smp_ready_cb(struct l2cap_chan *chan)
2155{
2156 struct l2cap_conn *conn = chan->conn;
2157
2158 BT_DBG("chan %p", chan);
2159
2160 conn->smp = chan;
2161 l2cap_chan_hold(chan);
2162}
2163
Johan Hedberg4befb862014-08-11 22:06:38 +03002164static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2165{
2166 int err;
2167
2168 BT_DBG("chan %p", chan);
2169
2170 err = smp_sig_channel(chan, skb);
2171 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002172 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002173
Johan Hedbergb68fda62014-08-11 22:06:40 +03002174 if (smp)
2175 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002176
Johan Hedberg1e91c292014-08-18 20:33:29 +03002177 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002178 }
2179
2180 return err;
2181}
2182
Johan Hedberg70db83c2014-08-08 09:37:16 +03002183static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2184 unsigned long hdr_len,
2185 unsigned long len, int nb)
2186{
2187 struct sk_buff *skb;
2188
2189 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2190 if (!skb)
2191 return ERR_PTR(-ENOMEM);
2192
2193 skb->priority = HCI_PRIO_MAX;
2194 bt_cb(skb)->chan = chan;
2195
2196 return skb;
2197}
2198
2199static const struct l2cap_ops smp_chan_ops = {
2200 .name = "Security Manager",
2201 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002202 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002203 .alloc_skb = smp_alloc_skb_cb,
2204 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002205 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002206
2207 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002208 .state_change = l2cap_chan_no_state_change,
2209 .close = l2cap_chan_no_close,
2210 .defer = l2cap_chan_no_defer,
2211 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002212 .set_shutdown = l2cap_chan_no_set_shutdown,
2213 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2214 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2215};
2216
2217static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2218{
2219 struct l2cap_chan *chan;
2220
2221 BT_DBG("pchan %p", pchan);
2222
2223 chan = l2cap_chan_create();
2224 if (!chan)
2225 return NULL;
2226
2227 chan->chan_type = pchan->chan_type;
2228 chan->ops = &smp_chan_ops;
2229 chan->scid = pchan->scid;
2230 chan->dcid = chan->scid;
2231 chan->imtu = pchan->imtu;
2232 chan->omtu = pchan->omtu;
2233 chan->mode = pchan->mode;
2234
Johan Hedbergabe84902014-11-12 22:22:21 +02002235 /* Other L2CAP channels may request SMP routines in order to
2236 * change the security level. This means that the SMP channel
2237 * lock must be considered in its own category to avoid lockdep
2238 * warnings.
2239 */
2240 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2241
Johan Hedberg70db83c2014-08-08 09:37:16 +03002242 BT_DBG("created chan %p", chan);
2243
2244 return chan;
2245}
2246
2247static const struct l2cap_ops smp_root_chan_ops = {
2248 .name = "Security Manager Root",
2249 .new_connection = smp_new_conn_cb,
2250
2251 /* None of these are implemented for the root channel */
2252 .close = l2cap_chan_no_close,
2253 .alloc_skb = l2cap_chan_no_alloc_skb,
2254 .recv = l2cap_chan_no_recv,
2255 .state_change = l2cap_chan_no_state_change,
2256 .teardown = l2cap_chan_no_teardown,
2257 .ready = l2cap_chan_no_ready,
2258 .defer = l2cap_chan_no_defer,
2259 .suspend = l2cap_chan_no_suspend,
2260 .resume = l2cap_chan_no_resume,
2261 .set_shutdown = l2cap_chan_no_set_shutdown,
2262 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2263 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2264};
2265
Johan Hedberg711eafe2014-08-08 09:32:52 +03002266int smp_register(struct hci_dev *hdev)
2267{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002268 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002269 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002270
Johan Hedberg711eafe2014-08-08 09:32:52 +03002271 BT_DBG("%s", hdev->name);
2272
Johan Hedbergadae20c2014-11-13 14:37:48 +02002273 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002274 if (IS_ERR(tfm_aes)) {
2275 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002276 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03002277 return err;
2278 }
2279
Johan Hedberg70db83c2014-08-08 09:37:16 +03002280 chan = l2cap_chan_create();
2281 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002282 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002283 return -ENOMEM;
2284 }
2285
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002286 chan->data = tfm_aes;
2287
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002288 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002289
2290 l2cap_chan_set_defaults(chan);
2291
2292 bacpy(&chan->src, &hdev->bdaddr);
2293 chan->src_type = BDADDR_LE_PUBLIC;
2294 chan->state = BT_LISTEN;
2295 chan->mode = L2CAP_MODE_BASIC;
2296 chan->imtu = L2CAP_DEFAULT_MTU;
2297 chan->ops = &smp_root_chan_ops;
2298
Johan Hedbergabe84902014-11-12 22:22:21 +02002299 /* Set correct nesting level for a parent/listening channel */
2300 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2301
Johan Hedberg70db83c2014-08-08 09:37:16 +03002302 hdev->smp_data = chan;
2303
Johan Hedberg711eafe2014-08-08 09:32:52 +03002304 return 0;
2305}
2306
2307void smp_unregister(struct hci_dev *hdev)
2308{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002309 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002310 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002311
2312 if (!chan)
2313 return;
2314
2315 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002316
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002317 tfm_aes = chan->data;
2318 if (tfm_aes) {
2319 chan->data = NULL;
2320 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002321 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03002322
2323 hdev->smp_data = NULL;
2324 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002325}