blob: f59f0510e0b0d57e6db8be64d7238bf96884cce4 [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 Hedberg4a74d652014-05-20 09:45:50 +030080 unsigned long flags;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030081
Johan Hedberg3b191462014-06-06 10:50:15 +030082 /* Secure Connections variables */
83 u8 local_pk[64];
84 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030085 u8 remote_pk[64];
86 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +030087 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +030088
Johan Hedberg6a7bd102014-06-27 14:23:03 +030089 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +030090 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030091};
92
Johan Hedberg8a2936f2014-06-16 19:25:19 +030093static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030094{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030095 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030096
Johan Hedberg8a2936f2014-06-16 19:25:19 +030097 for (i = 0; i < len; i++)
98 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030099}
100
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300101static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
102 size_t len, u8 mac[16])
103{
104 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
105 struct hash_desc desc;
106 struct scatterlist sg;
107 int err;
108
109 if (len > CMAC_MSG_MAX)
110 return -EFBIG;
111
112 if (!tfm) {
113 BT_ERR("tfm %p", tfm);
114 return -EINVAL;
115 }
116
117 desc.tfm = tfm;
118 desc.flags = 0;
119
120 crypto_hash_init(&desc);
121
122 /* Swap key and message from LSB to MSB */
123 swap_buf(k, tmp, 16);
124 swap_buf(m, msg_msb, len);
125
126 BT_DBG("msg (len %zu) %*phN", len, (int) len, m);
127 BT_DBG("key %16phN", k);
128
129 err = crypto_hash_setkey(tfm, tmp, 16);
130 if (err) {
131 BT_ERR("cipher setkey failed: %d", err);
132 return err;
133 }
134
135 sg_init_one(&sg, msg_msb, len);
136
137 err = crypto_hash_update(&desc, &sg, len);
138 if (err) {
139 BT_ERR("Hash update error %d", err);
140 return err;
141 }
142
143 err = crypto_hash_final(&desc, mac_msb);
144 if (err) {
145 BT_ERR("Hash final error %d", err);
146 return err;
147 }
148
149 swap_buf(mac_msb, mac, 16);
150
151 BT_DBG("mac %16phN", mac);
152
153 return 0;
154}
155
156static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
157 const u8 x[16], u8 z, u8 res[16])
158{
159 u8 m[65];
160 int err;
161
162 BT_DBG("u %32phN", u);
163 BT_DBG("v %32phN", v);
164 BT_DBG("x %16phN z %02x", x, z);
165
166 m[0] = z;
167 memcpy(m + 1, v, 32);
168 memcpy(m + 33, u, 32);
169
170 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
171 if (err)
172 return err;
173
174 BT_DBG("res %16phN", res);
175
176 return err;
177}
178
Johan Hedberg760b0182014-06-06 11:44:05 +0300179static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
180 u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
181{
182 /* The btle, salt and length "magic" values are as defined in
183 * the SMP section of the Bluetooth core specification. In ASCII
184 * the btle value ends up being 'btle'. The salt is just a
185 * random number whereas length is the value 256 in little
186 * endian format.
187 */
188 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
189 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
190 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
191 const u8 length[2] = { 0x00, 0x01 };
192 u8 m[53], t[16];
193 int err;
194
195 BT_DBG("w %32phN", w);
196 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
197 BT_DBG("a1 %7phN a2 %7phN", a1, a2);
198
199 err = aes_cmac(tfm_cmac, salt, w, 32, t);
200 if (err)
201 return err;
202
203 BT_DBG("t %16phN", t);
204
205 memcpy(m, length, 2);
206 memcpy(m + 2, a2, 7);
207 memcpy(m + 9, a1, 7);
208 memcpy(m + 16, n2, 16);
209 memcpy(m + 32, n1, 16);
210 memcpy(m + 48, btle, 4);
211
212 m[52] = 0; /* Counter */
213
214 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
215 if (err)
216 return err;
217
218 BT_DBG("mackey %16phN", mackey);
219
220 m[52] = 1; /* Counter */
221
222 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
223 if (err)
224 return err;
225
226 BT_DBG("ltk %16phN", ltk);
227
228 return 0;
229}
230
231static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
232 const u8 n1[16], u8 n2[16], const u8 r[16],
233 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
234 u8 res[16])
235{
236 u8 m[65];
237 int err;
238
239 BT_DBG("w %16phN", w);
240 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
241 BT_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
242
243 memcpy(m, a2, 7);
244 memcpy(m + 7, a1, 7);
245 memcpy(m + 14, io_cap, 3);
246 memcpy(m + 17, r, 16);
247 memcpy(m + 33, n2, 16);
248 memcpy(m + 49, n1, 16);
249
250 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
251 if (err)
252 return err;
253
254 BT_DBG("res %16phN", res);
255
256 return err;
257}
258
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300259static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
260 const u8 x[16], const u8 y[16], u32 *val)
261{
262 u8 m[80], tmp[16];
263 int err;
264
265 BT_DBG("u %32phN", u);
266 BT_DBG("v %32phN", v);
267 BT_DBG("x %16phN y %16phN", x, y);
268
269 memcpy(m, y, 16);
270 memcpy(m + 16, v, 32);
271 memcpy(m + 48, u, 32);
272
273 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
274 if (err)
275 return err;
276
277 *val = get_unaligned_le32(tmp);
278 *val %= 1000000;
279
280 BT_DBG("val %06u", *val);
281
282 return 0;
283}
284
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300285static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
286{
287 struct blkcipher_desc desc;
288 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200289 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200290 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300291
292 if (tfm == NULL) {
293 BT_ERR("tfm %p", tfm);
294 return -EINVAL;
295 }
296
297 desc.tfm = tfm;
298 desc.flags = 0;
299
Johan Hedberg943a7322014-03-18 12:58:24 +0200300 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300301 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200302
303 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300304 if (err) {
305 BT_ERR("cipher setkey failed: %d", err);
306 return err;
307 }
308
Johan Hedberg943a7322014-03-18 12:58:24 +0200309 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300310 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200311
312 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300313
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300314 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
315 if (err)
316 BT_ERR("Encrypt data error %d", err);
317
Johan Hedberg943a7322014-03-18 12:58:24 +0200318 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300319 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200320
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300321 return err;
322}
323
Johan Hedberg60478052014-02-18 10:19:31 +0200324static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
325{
Johan Hedberg943a7322014-03-18 12:58:24 +0200326 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200327 int err;
328
329 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200330 memcpy(_res, r, 3);
331 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200332
Johan Hedberg943a7322014-03-18 12:58:24 +0200333 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200334 if (err) {
335 BT_ERR("Encrypt error");
336 return err;
337 }
338
339 /* The output of the random address function ah is:
340 * ah(h, r) = e(k, r') mod 2^24
341 * The output of the security function e is then truncated to 24 bits
342 * by taking the least significant 24 bits of the output of e as the
343 * result of ah.
344 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200345 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200346
347 return 0;
348}
349
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300350bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200351{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300352 struct l2cap_chan *chan = hdev->smp_data;
353 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200354 u8 hash[3];
355 int err;
356
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300357 if (!chan || !chan->data)
358 return false;
359
360 tfm = chan->data;
361
Johan Hedberg60478052014-02-18 10:19:31 +0200362 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
363
364 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
365 if (err)
366 return false;
367
368 return !memcmp(bdaddr->b, hash, 3);
369}
370
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300371int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200372{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300373 struct l2cap_chan *chan = hdev->smp_data;
374 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200375 int err;
376
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300377 if (!chan || !chan->data)
378 return -EOPNOTSUPP;
379
380 tfm = chan->data;
381
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200382 get_random_bytes(&rpa->b[3], 3);
383
384 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
385 rpa->b[5] |= 0x40; /* Set second most significant bit */
386
387 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
388 if (err < 0)
389 return err;
390
391 BT_DBG("RPA %pMR", rpa);
392
393 return 0;
394}
395
Johan Hedberge491eaf2014-10-25 21:15:37 +0200396static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
397 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
398 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300399{
400 u8 p1[16], p2[16];
401 int err;
402
403 memset(p1, 0, 16);
404
405 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200406 p1[0] = _iat;
407 p1[1] = _rat;
408 memcpy(p1 + 2, preq, 7);
409 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300410
411 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200412 memcpy(p2, ra, 6);
413 memcpy(p2 + 6, ia, 6);
414 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300415
416 /* res = r XOR p1 */
417 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
418
419 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200420 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300421 if (err) {
422 BT_ERR("Encrypt data error");
423 return err;
424 }
425
426 /* res = res XOR p2 */
427 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
428
429 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200430 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300431 if (err)
432 BT_ERR("Encrypt data error");
433
434 return err;
435}
436
Johan Hedberge491eaf2014-10-25 21:15:37 +0200437static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
438 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300439{
440 int err;
441
442 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200443 memcpy(_r, r2, 8);
444 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300445
Johan Hedberge491eaf2014-10-25 21:15:37 +0200446 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300447 if (err)
448 BT_ERR("Encrypt data error");
449
450 return err;
451}
452
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300453static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
454{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300455 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300456 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300457 struct kvec iv[2];
458 struct msghdr msg;
459
460 if (!chan)
461 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300462
463 BT_DBG("code 0x%2.2x", code);
464
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300465 iv[0].iov_base = &code;
466 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300467
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300468 iv[1].iov_base = data;
469 iv[1].iov_len = len;
470
471 memset(&msg, 0, sizeof(msg));
472
473 msg.msg_iov = (struct iovec *) &iv;
474 msg.msg_iovlen = 2;
475
476 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300477
Johan Hedbergb68fda62014-08-11 22:06:40 +0300478 if (!chan->data)
479 return;
480
481 smp = chan->data;
482
483 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300484 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300485}
486
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300487static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800488{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300489 if (authreq & SMP_AUTH_MITM) {
490 if (authreq & SMP_AUTH_SC)
491 return BT_SECURITY_FIPS;
492 else
493 return BT_SECURITY_HIGH;
494 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800495 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300496 }
Brian Gix2b64d152011-12-21 16:12:12 -0800497}
498
499static __u8 seclevel_to_authreq(__u8 sec_level)
500{
501 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300502 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800503 case BT_SECURITY_HIGH:
504 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
505 case BT_SECURITY_MEDIUM:
506 return SMP_AUTH_BONDING;
507 default:
508 return SMP_AUTH_NONE;
509 }
510}
511
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300512static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700513 struct smp_cmd_pairing *req,
514 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300515{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300516 struct l2cap_chan *chan = conn->smp;
517 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200518 struct hci_conn *hcon = conn->hcon;
519 struct hci_dev *hdev = hcon->hdev;
520 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300521
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300522 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700523 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
524 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300525 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800526 } else {
527 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300528 }
529
Johan Hedbergfd349c02014-02-18 10:19:36 +0200530 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
531 remote_dist |= SMP_DIST_ID_KEY;
532
Johan Hedberg863efaf2014-02-22 19:06:32 +0200533 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
534 local_dist |= SMP_DIST_ID_KEY;
535
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300536 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
537 if ((authreq & SMP_AUTH_SC) &&
538 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
539 local_dist |= SMP_DIST_LINK_KEY;
540 remote_dist |= SMP_DIST_LINK_KEY;
541 }
542 } else {
543 authreq &= ~SMP_AUTH_SC;
544 }
545
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300546 if (rsp == NULL) {
547 req->io_capability = conn->hcon->io_capability;
548 req->oob_flag = SMP_OOB_NOT_PRESENT;
549 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200550 req->init_key_dist = local_dist;
551 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300552 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200553
554 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300555 return;
556 }
557
558 rsp->io_capability = conn->hcon->io_capability;
559 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
560 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200561 rsp->init_key_dist = req->init_key_dist & remote_dist;
562 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300563 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200564
565 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300566}
567
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300568static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
569{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300570 struct l2cap_chan *chan = conn->smp;
571 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300572
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300573 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700574 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300575 return SMP_ENC_KEY_SIZE;
576
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300577 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300578
579 return 0;
580}
581
Johan Hedberg6f48e262014-08-11 22:06:44 +0300582static void smp_chan_destroy(struct l2cap_conn *conn)
583{
584 struct l2cap_chan *chan = conn->smp;
585 struct smp_chan *smp = chan->data;
586 bool complete;
587
588 BUG_ON(!smp);
589
590 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300591
Johan Hedberg6f48e262014-08-11 22:06:44 +0300592 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
593 mgmt_smp_complete(conn->hcon, complete);
594
595 kfree(smp->csrk);
596 kfree(smp->slave_csrk);
597
598 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300599 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300600
601 /* If pairing failed clean up any keys we might have */
602 if (!complete) {
603 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200604 list_del_rcu(&smp->ltk->list);
605 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300606 }
607
608 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200609 list_del_rcu(&smp->slave_ltk->list);
610 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300611 }
612
613 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200614 list_del_rcu(&smp->remote_irk->list);
615 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300616 }
617 }
618
619 chan->data = NULL;
620 kfree(smp);
621 hci_conn_drop(conn->hcon);
622}
623
Johan Hedberg84794e12013-11-06 11:24:57 +0200624static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800625{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200626 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300627 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200628
Johan Hedberg84794e12013-11-06 11:24:57 +0200629 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800630 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700631 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800632
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700633 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700634 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300635
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300636 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300637 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800638}
639
Brian Gix2b64d152011-12-21 16:12:12 -0800640#define JUST_WORKS 0x00
641#define JUST_CFM 0x01
642#define REQ_PASSKEY 0x02
643#define CFM_PASSKEY 0x03
644#define REQ_OOB 0x04
645#define OVERLAP 0xFF
646
647static const u8 gen_method[5][5] = {
648 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
649 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
650 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
651 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
652 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
653};
654
Johan Hedberg581370c2014-06-17 13:07:38 +0300655static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
656{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300657 /* If either side has unknown io_caps, use JUST_CFM (which gets
658 * converted later to JUST_WORKS if we're initiators.
659 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300660 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
661 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300662 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300663
664 return gen_method[remote_io][local_io];
665}
666
Brian Gix2b64d152011-12-21 16:12:12 -0800667static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
668 u8 local_io, u8 remote_io)
669{
670 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300671 struct l2cap_chan *chan = conn->smp;
672 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800673 u8 method;
674 u32 passkey = 0;
675 int ret = 0;
676
677 /* Initialize key for JUST WORKS */
678 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300679 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800680
681 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
682
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300683 /* If neither side wants MITM, either "just" confirm an incoming
684 * request or use just-works for outgoing ones. The JUST_CFM
685 * will be converted to JUST_WORKS if necessary later in this
686 * function. If either side has MITM look up the method from the
687 * table.
688 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300689 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300690 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800691 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300692 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800693
Johan Hedberga82505c2014-03-24 14:39:07 +0200694 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300695 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200696 method = JUST_WORKS;
697
Johan Hedberg02f3e252014-07-16 15:09:13 +0300698 /* Don't bother user space with no IO capabilities */
699 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
700 method = JUST_WORKS;
701
Brian Gix2b64d152011-12-21 16:12:12 -0800702 /* If Just Works, Continue with Zero TK */
703 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300704 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800705 return 0;
706 }
707
708 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300709 if (method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300710 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300711 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
712 hcon->pending_sec_level = BT_SECURITY_HIGH;
713 }
Brian Gix2b64d152011-12-21 16:12:12 -0800714
715 /* If both devices have Keyoard-Display I/O, the master
716 * Confirms and the slave Enters the passkey.
717 */
718 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300719 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800720 method = CFM_PASSKEY;
721 else
722 method = REQ_PASSKEY;
723 }
724
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200725 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800726 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200727 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800728 get_random_bytes(&passkey, sizeof(passkey));
729 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200730 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800731 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300732 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800733 }
734
Brian Gix2b64d152011-12-21 16:12:12 -0800735 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700736 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200737 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200738 else if (method == JUST_CFM)
739 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
740 hcon->type, hcon->dst_type,
741 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800742 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200743 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200744 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200745 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800746
Brian Gix2b64d152011-12-21 16:12:12 -0800747 return ret;
748}
749
Johan Hedberg1cc61142014-05-20 09:45:52 +0300750static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300751{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300752 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300753 struct smp_cmd_pairing_confirm cp;
754 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300755
756 BT_DBG("conn %p", conn);
757
Johan Hedberge491eaf2014-10-25 21:15:37 +0200758 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200759 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200760 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
761 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300762 if (ret)
763 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300764
Johan Hedberg4a74d652014-05-20 09:45:50 +0300765 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800766
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300767 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
768
Johan Hedbergb28b4942014-09-05 22:19:55 +0300769 if (conn->hcon->out)
770 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
771 else
772 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
773
Johan Hedberg1cc61142014-05-20 09:45:52 +0300774 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300775}
776
Johan Hedberg861580a2014-05-20 09:45:51 +0300777static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300778{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300779 struct l2cap_conn *conn = smp->conn;
780 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300781 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300782 int ret;
783
Johan Hedbergec70f362014-06-27 14:23:04 +0300784 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300785 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300786
787 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
788
Johan Hedberge491eaf2014-10-25 21:15:37 +0200789 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200790 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200791 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300792 if (ret)
793 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300794
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300795 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
796 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300797 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300798 }
799
800 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800801 u8 stk[16];
802 __le64 rand = 0;
803 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300804
Johan Hedberge491eaf2014-10-25 21:15:37 +0200805 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300806
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300807 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300808 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300809
Johan Hedberg861580a2014-05-20 09:45:51 +0300810 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
811 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300812
813 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300814 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300815 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300816 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300817 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800818 __le64 rand = 0;
819 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300820
Johan Hedberg943a7322014-03-18 12:58:24 +0200821 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
822 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300823
Johan Hedberge491eaf2014-10-25 21:15:37 +0200824 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300825
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300826 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700827 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300828
Johan Hedbergfff34902014-06-10 15:19:50 +0300829 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
830 auth = 1;
831 else
832 auth = 0;
833
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300834 /* Even though there's no _SLAVE suffix this is the
835 * slave STK we're adding for later lookup (the master
836 * STK never needs to be stored).
837 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700838 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300839 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300840 }
841
Johan Hedberg861580a2014-05-20 09:45:51 +0300842 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300843}
844
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300845static void smp_notify_keys(struct l2cap_conn *conn)
846{
847 struct l2cap_chan *chan = conn->smp;
848 struct smp_chan *smp = chan->data;
849 struct hci_conn *hcon = conn->hcon;
850 struct hci_dev *hdev = hcon->hdev;
851 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
852 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
853 bool persistent;
854
855 if (smp->remote_irk) {
856 mgmt_new_irk(hdev, smp->remote_irk);
857 /* Now that user space can be considered to know the
858 * identity address track the connection based on it
859 * from now on.
860 */
861 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
862 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300863 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300864
865 /* When receiving an indentity resolving key for
866 * a remote device that does not use a resolvable
867 * private address, just remove the key so that
868 * it is possible to use the controller white
869 * list for scanning.
870 *
871 * Userspace will have been told to not store
872 * this key at this point. So it is safe to
873 * just remove it.
874 */
875 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200876 list_del_rcu(&smp->remote_irk->list);
877 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300878 smp->remote_irk = NULL;
879 }
880 }
881
882 /* The LTKs and CSRKs should be persistent only if both sides
883 * had the bonding bit set in their authentication requests.
884 */
885 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
886
887 if (smp->csrk) {
888 smp->csrk->bdaddr_type = hcon->dst_type;
889 bacpy(&smp->csrk->bdaddr, &hcon->dst);
890 mgmt_new_csrk(hdev, smp->csrk, persistent);
891 }
892
893 if (smp->slave_csrk) {
894 smp->slave_csrk->bdaddr_type = hcon->dst_type;
895 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
896 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
897 }
898
899 if (smp->ltk) {
900 smp->ltk->bdaddr_type = hcon->dst_type;
901 bacpy(&smp->ltk->bdaddr, &hcon->dst);
902 mgmt_new_ltk(hdev, smp->ltk, persistent);
903 }
904
905 if (smp->slave_ltk) {
906 smp->slave_ltk->bdaddr_type = hcon->dst_type;
907 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
908 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
909 }
910}
911
Johan Hedbergb28b4942014-09-05 22:19:55 +0300912static void smp_allow_key_dist(struct smp_chan *smp)
913{
914 /* Allow the first expected phase 3 PDU. The rest of the PDUs
915 * will be allowed in each PDU handler to ensure we receive
916 * them in the correct order.
917 */
918 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
919 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
920 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
921 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
922 else if (smp->remote_key_dist & SMP_DIST_SIGN)
923 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
924}
925
Johan Hedbergd6268e82014-09-05 22:19:51 +0300926static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300927{
928 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300929 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300930 struct hci_conn *hcon = conn->hcon;
931 struct hci_dev *hdev = hcon->hdev;
932 __u8 *keydist;
933
934 BT_DBG("conn %p", conn);
935
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300936 rsp = (void *) &smp->prsp[1];
937
938 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300939 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
940 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300941 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300942 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300943
944 req = (void *) &smp->preq[1];
945
946 if (hcon->out) {
947 keydist = &rsp->init_key_dist;
948 *keydist &= req->init_key_dist;
949 } else {
950 keydist = &rsp->resp_key_dist;
951 *keydist &= req->resp_key_dist;
952 }
953
954 BT_DBG("keydist 0x%x", *keydist);
955
956 if (*keydist & SMP_DIST_ENC_KEY) {
957 struct smp_cmd_encrypt_info enc;
958 struct smp_cmd_master_ident ident;
959 struct smp_ltk *ltk;
960 u8 authenticated;
961 __le16 ediv;
962 __le64 rand;
963
964 get_random_bytes(enc.ltk, sizeof(enc.ltk));
965 get_random_bytes(&ediv, sizeof(ediv));
966 get_random_bytes(&rand, sizeof(rand));
967
968 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
969
970 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
971 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
972 SMP_LTK_SLAVE, authenticated, enc.ltk,
973 smp->enc_key_size, ediv, rand);
974 smp->slave_ltk = ltk;
975
976 ident.ediv = ediv;
977 ident.rand = rand;
978
979 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
980
981 *keydist &= ~SMP_DIST_ENC_KEY;
982 }
983
984 if (*keydist & SMP_DIST_ID_KEY) {
985 struct smp_cmd_ident_addr_info addrinfo;
986 struct smp_cmd_ident_info idinfo;
987
988 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
989
990 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
991
992 /* The hci_conn contains the local identity address
993 * after the connection has been established.
994 *
995 * This is true even when the connection has been
996 * established using a resolvable random address.
997 */
998 bacpy(&addrinfo.bdaddr, &hcon->src);
999 addrinfo.addr_type = hcon->src_type;
1000
1001 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1002 &addrinfo);
1003
1004 *keydist &= ~SMP_DIST_ID_KEY;
1005 }
1006
1007 if (*keydist & SMP_DIST_SIGN) {
1008 struct smp_cmd_sign_info sign;
1009 struct smp_csrk *csrk;
1010
1011 /* Generate a new random key */
1012 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1013
1014 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1015 if (csrk) {
1016 csrk->master = 0x00;
1017 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1018 }
1019 smp->slave_csrk = csrk;
1020
1021 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1022
1023 *keydist &= ~SMP_DIST_SIGN;
1024 }
1025
1026 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001027 if (smp->remote_key_dist & KEY_DIST_MASK) {
1028 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001029 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001030 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001031
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001032 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1033 smp_notify_keys(conn);
1034
1035 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001036}
1037
Johan Hedbergb68fda62014-08-11 22:06:40 +03001038static void smp_timeout(struct work_struct *work)
1039{
1040 struct smp_chan *smp = container_of(work, struct smp_chan,
1041 security_timer.work);
1042 struct l2cap_conn *conn = smp->conn;
1043
1044 BT_DBG("conn %p", conn);
1045
Johan Hedberg1e91c292014-08-18 20:33:29 +03001046 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001047}
1048
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001049static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1050{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001051 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001052 struct smp_chan *smp;
1053
Marcel Holtmannf1560462013-10-13 05:43:25 -07001054 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001055 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001056 return NULL;
1057
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001058 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1059 if (IS_ERR(smp->tfm_aes)) {
1060 BT_ERR("Unable to create ECB crypto context");
1061 kfree(smp);
1062 return NULL;
1063 }
1064
Johan Hedberg407cecf2014-05-02 14:19:47 +03001065 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1066 if (IS_ERR(smp->tfm_cmac)) {
1067 BT_ERR("Unable to create CMAC crypto context");
1068 crypto_free_blkcipher(smp->tfm_aes);
1069 kfree(smp);
1070 return NULL;
1071 }
1072
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001073 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001074 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001075
Johan Hedbergb28b4942014-09-05 22:19:55 +03001076 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1077
Johan Hedbergb68fda62014-08-11 22:06:40 +03001078 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1079
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001080 hci_conn_hold(conn->hcon);
1081
1082 return smp;
1083}
1084
Johan Hedberg760b0182014-06-06 11:44:05 +03001085static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1086{
1087 struct hci_conn *hcon = smp->conn->hcon;
1088 u8 *na, *nb, a[7], b[7];
1089
1090 if (hcon->out) {
1091 na = smp->prnd;
1092 nb = smp->rrnd;
1093 } else {
1094 na = smp->rrnd;
1095 nb = smp->prnd;
1096 }
1097
1098 memcpy(a, &hcon->init_addr, 6);
1099 memcpy(b, &hcon->resp_addr, 6);
1100 a[6] = hcon->init_addr_type;
1101 b[6] = hcon->resp_addr_type;
1102
1103 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1104}
1105
1106static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1107{
1108 struct hci_conn *hcon = smp->conn->hcon;
1109 struct smp_cmd_dhkey_check check;
1110 u8 a[7], b[7], *local_addr, *remote_addr;
1111 u8 io_cap[3], r[16];
1112
1113 switch (mgmt_op) {
1114 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1115 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1116 return 0;
1117 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1118 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1119 return 0;
1120 }
1121
1122 memcpy(a, &hcon->init_addr, 6);
1123 memcpy(b, &hcon->resp_addr, 6);
1124 a[6] = hcon->init_addr_type;
1125 b[6] = hcon->resp_addr_type;
1126
1127 if (hcon->out) {
1128 local_addr = a;
1129 remote_addr = b;
1130 memcpy(io_cap, &smp->preq[1], 3);
1131 } else {
1132 local_addr = b;
1133 remote_addr = a;
1134 memcpy(io_cap, &smp->prsp[1], 3);
1135 }
1136
1137 memcpy(r, &passkey, sizeof(passkey));
1138 memset(r + sizeof(passkey), 0, sizeof(r) - sizeof(passkey));
1139
1140 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1141 local_addr, remote_addr, check.e);
1142
1143 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
1144
1145 return 0;
1146}
1147
Brian Gix2b64d152011-12-21 16:12:12 -08001148int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1149{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001150 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001151 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001152 struct smp_chan *smp;
1153 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001154 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001155
1156 BT_DBG("");
1157
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001158 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001159 return -ENOTCONN;
1160
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001161 chan = conn->smp;
1162 if (!chan)
1163 return -ENOTCONN;
1164
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001165 l2cap_chan_lock(chan);
1166 if (!chan->data) {
1167 err = -ENOTCONN;
1168 goto unlock;
1169 }
1170
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001171 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001172
Johan Hedberg760b0182014-06-06 11:44:05 +03001173 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1174 err = sc_user_reply(smp, mgmt_op, passkey);
1175 goto unlock;
1176 }
1177
Brian Gix2b64d152011-12-21 16:12:12 -08001178 switch (mgmt_op) {
1179 case MGMT_OP_USER_PASSKEY_REPLY:
1180 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001181 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001182 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001183 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001184 /* Fall Through */
1185 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001186 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001187 break;
1188 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1189 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001190 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001191 err = 0;
1192 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001193 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001194 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001195 err = -EOPNOTSUPP;
1196 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001197 }
1198
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001199 err = 0;
1200
Brian Gix2b64d152011-12-21 16:12:12 -08001201 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001202 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1203 u8 rsp = smp_confirm(smp);
1204 if (rsp)
1205 smp_failure(conn, rsp);
1206 }
Brian Gix2b64d152011-12-21 16:12:12 -08001207
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001208unlock:
1209 l2cap_chan_unlock(chan);
1210 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001211}
1212
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001213static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001214{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001215 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001216 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001217 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001218 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001219 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001220 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001221
1222 BT_DBG("conn %p", conn);
1223
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001224 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001225 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001226
Johan Hedberg40bef302014-07-16 11:42:27 +03001227 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001228 return SMP_CMD_NOTSUPP;
1229
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001230 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001231 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001232 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001233 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001234
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001235 if (!smp)
1236 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001237
Johan Hedbergc05b9332014-09-10 17:37:42 -07001238 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001239 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001240
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001241 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001242 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001243 return SMP_PAIRING_NOTSUPP;
1244
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001245 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1246 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001247 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001248
Johan Hedberg5be5e272014-09-10 17:58:54 -07001249 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001250 sec_level = BT_SECURITY_MEDIUM;
1251 else
1252 sec_level = authreq_to_seclevel(auth);
1253
Johan Hedbergc7262e72014-06-17 13:07:37 +03001254 if (sec_level > conn->hcon->pending_sec_level)
1255 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001256
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001257 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001258 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1259 u8 method;
1260
1261 method = get_auth_method(smp, conn->hcon->io_capability,
1262 req->io_capability);
1263 if (method == JUST_WORKS || method == JUST_CFM)
1264 return SMP_AUTH_REQUIREMENTS;
1265 }
1266
Brian Gix2b64d152011-12-21 16:12:12 -08001267 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001268
Johan Hedberg65668772014-05-16 11:03:34 +03001269 if (rsp.auth_req & SMP_AUTH_SC)
1270 set_bit(SMP_FLAG_SC, &smp->flags);
1271
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001272 key_size = min(req->max_key_size, rsp.max_key_size);
1273 if (check_enc_key_size(conn, key_size))
1274 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001275
Johan Hedberge84a6b12013-12-02 10:49:03 +02001276 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001277
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001278 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1279 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001280
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001281 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001282
1283 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1284
1285 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1286 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1287 /* Clear bits which are generated but not distributed */
1288 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1289 /* Wait for Public Key from Initiating Device */
1290 return 0;
1291 } else {
1292 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1293 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001294
Brian Gix2b64d152011-12-21 16:12:12 -08001295 /* Request setup of TK */
1296 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1297 if (ret)
1298 return SMP_UNSPECIFIED;
1299
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001300 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001301}
1302
Johan Hedberg3b191462014-06-06 10:50:15 +03001303static u8 sc_send_public_key(struct smp_chan *smp)
1304{
1305 BT_DBG("");
1306
1307 /* Generate local key pair for Secure Connections */
1308 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1309 return SMP_UNSPECIFIED;
1310
1311 BT_DBG("Local Public Key X: %32phN", smp->local_pk);
1312 BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1313 BT_DBG("Local Private Key: %32phN", smp->local_sk);
1314
1315 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1316
1317 return 0;
1318}
1319
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001320static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001321{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001322 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001323 struct l2cap_chan *chan = conn->smp;
1324 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001325 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001326 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001327 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001328
1329 BT_DBG("conn %p", conn);
1330
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001331 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001332 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001333
Johan Hedberg40bef302014-07-16 11:42:27 +03001334 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001335 return SMP_CMD_NOTSUPP;
1336
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001337 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001338
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001339 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001340
1341 key_size = min(req->max_key_size, rsp->max_key_size);
1342 if (check_enc_key_size(conn, key_size))
1343 return SMP_ENC_KEY_SIZE;
1344
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001345 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001346
Johan Hedberg65668772014-05-16 11:03:34 +03001347 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1348 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001349 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1350 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001351
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001352 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001353 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1354 u8 method;
1355
1356 method = get_auth_method(smp, req->io_capability,
1357 rsp->io_capability);
1358 if (method == JUST_WORKS || method == JUST_CFM)
1359 return SMP_AUTH_REQUIREMENTS;
1360 }
1361
Johan Hedberge84a6b12013-12-02 10:49:03 +02001362 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001363
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001364 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1365 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001366
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001367 /* Update remote key distribution in case the remote cleared
1368 * some bits that we had enabled in our request.
1369 */
1370 smp->remote_key_dist &= rsp->resp_key_dist;
1371
Johan Hedberg3b191462014-06-06 10:50:15 +03001372 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1373 /* Clear bits which are generated but not distributed */
1374 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1375 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1376 return sc_send_public_key(smp);
1377 }
1378
Johan Hedbergc05b9332014-09-10 17:37:42 -07001379 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001380
Johan Hedberg476585e2012-06-06 18:54:15 +08001381 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001382 if (ret)
1383 return SMP_UNSPECIFIED;
1384
Johan Hedberg4a74d652014-05-20 09:45:50 +03001385 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001386
1387 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001388 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001389 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001390
1391 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001392}
1393
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001394static u8 sc_check_confirm(struct smp_chan *smp)
1395{
1396 struct l2cap_conn *conn = smp->conn;
1397
1398 BT_DBG("");
1399
1400 /* Public Key exchange must happen before any other steps */
1401 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1402 return SMP_UNSPECIFIED;
1403
1404 if (conn->hcon->out) {
1405 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1406 smp->prnd);
1407 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1408 }
1409
1410 return 0;
1411}
1412
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001413static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001414{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001415 struct l2cap_chan *chan = conn->smp;
1416 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001417
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001418 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1419
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001420 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001421 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001422
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001423 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1424 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001425
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001426 if (test_bit(SMP_FLAG_SC, &smp->flags))
1427 return sc_check_confirm(smp);
1428
Johan Hedbergb28b4942014-09-05 22:19:55 +03001429 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001430 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1431 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001432 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1433 return 0;
1434 }
1435
1436 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001437 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001438 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001439 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001440
1441 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001442}
1443
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001444static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001445{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001446 struct l2cap_chan *chan = conn->smp;
1447 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001448 struct hci_conn *hcon = conn->hcon;
1449 u8 *pkax, *pkbx, *na, *nb;
1450 u32 passkey;
1451 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001452
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001453 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001454
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001455 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001456 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001457
Johan Hedberg943a7322014-03-18 12:58:24 +02001458 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001459 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001460
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001461 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1462 return smp_random(smp);
1463
1464 if (hcon->out) {
1465 u8 cfm[16];
1466
1467 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1468 smp->rrnd, 0, cfm);
1469 if (err)
1470 return SMP_UNSPECIFIED;
1471
1472 if (memcmp(smp->pcnf, cfm, 16))
1473 return SMP_CONFIRM_FAILED;
1474
1475 pkax = smp->local_pk;
1476 pkbx = smp->remote_pk;
1477 na = smp->prnd;
1478 nb = smp->rrnd;
1479 } else {
1480 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1481 smp->prnd);
1482 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1483
1484 pkax = smp->remote_pk;
1485 pkbx = smp->local_pk;
1486 na = smp->rrnd;
1487 nb = smp->prnd;
1488 }
1489
Johan Hedberg760b0182014-06-06 11:44:05 +03001490 /* Generate MacKey and LTK */
1491 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1492 if (err)
1493 return SMP_UNSPECIFIED;
1494
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001495 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
1496 if (err)
1497 return SMP_UNSPECIFIED;
1498
1499 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
1500 hcon->type, hcon->dst_type,
1501 passkey, 0);
1502 if (err)
1503 return SMP_UNSPECIFIED;
1504
1505 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001506}
1507
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001508static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001509{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001510 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001511 struct hci_conn *hcon = conn->hcon;
1512
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001513 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001514 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001515 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001516
Johan Hedberga6f78332014-09-10 17:37:45 -07001517 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001518 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001519
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001520 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001521 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001522
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001523 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1524 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001525
Johan Hedbergfe59a052014-07-01 19:14:12 +03001526 /* We never store STKs for master role, so clear this flag */
1527 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1528
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001529 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001530}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001531
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001532bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1533 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001534{
1535 if (sec_level == BT_SECURITY_LOW)
1536 return true;
1537
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001538 /* If we're encrypted with an STK but the caller prefers using
1539 * LTK claim insufficient security. This way we allow the
1540 * connection to be re-encrypted with an LTK, even if the LTK
1541 * provides the same level of security. Only exception is if we
1542 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001543 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001544 if (key_pref == SMP_USE_LTK &&
1545 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001546 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001547 return false;
1548
Johan Hedberg854f4722014-07-01 18:40:20 +03001549 if (hcon->sec_level >= sec_level)
1550 return true;
1551
1552 return false;
1553}
1554
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001555static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001556{
1557 struct smp_cmd_security_req *rp = (void *) skb->data;
1558 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001559 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001560 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001561 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001562 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001563
1564 BT_DBG("conn %p", conn);
1565
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001566 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001567 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001568
Johan Hedberg40bef302014-07-16 11:42:27 +03001569 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001570 return SMP_CMD_NOTSUPP;
1571
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001572 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001573
Johan Hedberg5be5e272014-09-10 17:58:54 -07001574 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001575 sec_level = BT_SECURITY_MEDIUM;
1576 else
1577 sec_level = authreq_to_seclevel(auth);
1578
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001579 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03001580 return 0;
1581
Johan Hedbergc7262e72014-06-17 13:07:37 +03001582 if (sec_level > hcon->pending_sec_level)
1583 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001584
Johan Hedberg4dab7862012-06-07 14:58:37 +08001585 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001586 return 0;
1587
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001588 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001589 if (!smp)
1590 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001591
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001592 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001593 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001594 return SMP_PAIRING_NOTSUPP;
1595
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001596 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001597
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001598 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001599 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001600
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001601 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1602 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001603
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001604 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001605 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001606
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001607 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001608}
1609
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001610int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001611{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001612 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001613 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001614 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001615 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001616 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001617
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001618 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1619
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001620 /* This may be NULL if there's an unexpected disconnection */
1621 if (!conn)
1622 return 1;
1623
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001624 chan = conn->smp;
1625
Johan Hedberg757aee02013-04-24 13:05:32 +03001626 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001627 return 1;
1628
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001629 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001630 return 1;
1631
Johan Hedbergc7262e72014-06-17 13:07:37 +03001632 if (sec_level > hcon->pending_sec_level)
1633 hcon->pending_sec_level = sec_level;
1634
Johan Hedberg40bef302014-07-16 11:42:27 +03001635 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001636 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1637 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001638
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001639 l2cap_chan_lock(chan);
1640
1641 /* If SMP is already in progress ignore this request */
1642 if (chan->data) {
1643 ret = 0;
1644 goto unlock;
1645 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001646
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001647 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001648 if (!smp) {
1649 ret = 1;
1650 goto unlock;
1651 }
Brian Gix2b64d152011-12-21 16:12:12 -08001652
1653 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001654
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001655 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1656 authreq |= SMP_AUTH_SC;
1657
Johan Hedberg79897d22014-06-01 09:45:24 +03001658 /* Require MITM if IO Capability allows or the security level
1659 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001660 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001661 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001662 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001663 authreq |= SMP_AUTH_MITM;
1664
Johan Hedberg40bef302014-07-16 11:42:27 +03001665 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001666 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001667
Brian Gix2b64d152011-12-21 16:12:12 -08001668 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001669 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1670 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001671
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001672 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001673 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001674 } else {
1675 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001676 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001677 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001678 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001679 }
1680
Johan Hedberg4a74d652014-05-20 09:45:50 +03001681 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001682 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001683
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001684unlock:
1685 l2cap_chan_unlock(chan);
1686 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001687}
1688
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001689static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1690{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001691 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001692 struct l2cap_chan *chan = conn->smp;
1693 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001694
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001695 BT_DBG("conn %p", conn);
1696
1697 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001698 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001699
Johan Hedbergb28b4942014-09-05 22:19:55 +03001700 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001701
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001702 skb_pull(skb, sizeof(*rp));
1703
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001704 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001705
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001706 return 0;
1707}
1708
1709static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1710{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001711 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001712 struct l2cap_chan *chan = conn->smp;
1713 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001714 struct hci_dev *hdev = conn->hcon->hdev;
1715 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001716 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001717 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001718
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001719 BT_DBG("conn %p", conn);
1720
1721 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001722 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001723
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001724 /* Mark the information as received */
1725 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1726
Johan Hedbergb28b4942014-09-05 22:19:55 +03001727 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1728 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001729 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1730 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001731
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001732 skb_pull(skb, sizeof(*rp));
1733
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001734 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001735 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001736 authenticated, smp->tk, smp->enc_key_size,
1737 rp->ediv, rp->rand);
1738 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001739 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001740 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001741
1742 return 0;
1743}
1744
Johan Hedbergfd349c02014-02-18 10:19:36 +02001745static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1746{
1747 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001748 struct l2cap_chan *chan = conn->smp;
1749 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001750
1751 BT_DBG("");
1752
1753 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001754 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001755
Johan Hedbergb28b4942014-09-05 22:19:55 +03001756 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001757
Johan Hedbergfd349c02014-02-18 10:19:36 +02001758 skb_pull(skb, sizeof(*info));
1759
1760 memcpy(smp->irk, info->irk, 16);
1761
1762 return 0;
1763}
1764
1765static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1766 struct sk_buff *skb)
1767{
1768 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001769 struct l2cap_chan *chan = conn->smp;
1770 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001771 struct hci_conn *hcon = conn->hcon;
1772 bdaddr_t rpa;
1773
1774 BT_DBG("");
1775
1776 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001777 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001778
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001779 /* Mark the information as received */
1780 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1781
Johan Hedbergb28b4942014-09-05 22:19:55 +03001782 if (smp->remote_key_dist & SMP_DIST_SIGN)
1783 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1784
Johan Hedbergfd349c02014-02-18 10:19:36 +02001785 skb_pull(skb, sizeof(*info));
1786
Johan Hedberga9a58f82014-02-25 22:24:37 +02001787 /* Strictly speaking the Core Specification (4.1) allows sending
1788 * an empty address which would force us to rely on just the IRK
1789 * as "identity information". However, since such
1790 * implementations are not known of and in order to not over
1791 * complicate our implementation, simply pretend that we never
1792 * received an IRK for such a device.
1793 */
1794 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1795 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001796 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001797 }
1798
Johan Hedbergfd349c02014-02-18 10:19:36 +02001799 bacpy(&smp->id_addr, &info->bdaddr);
1800 smp->id_addr_type = info->addr_type;
1801
1802 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1803 bacpy(&rpa, &hcon->dst);
1804 else
1805 bacpy(&rpa, BDADDR_ANY);
1806
Johan Hedberg23d0e122014-02-19 14:57:46 +02001807 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1808 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001809
Johan Hedberg31dd6242014-06-27 14:23:02 +03001810distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001811 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1812 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001813
1814 return 0;
1815}
1816
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001817static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1818{
1819 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001820 struct l2cap_chan *chan = conn->smp;
1821 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001822 struct smp_csrk *csrk;
1823
1824 BT_DBG("conn %p", conn);
1825
1826 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001827 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001828
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001829 /* Mark the information as received */
1830 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1831
1832 skb_pull(skb, sizeof(*rp));
1833
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001834 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1835 if (csrk) {
1836 csrk->master = 0x01;
1837 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1838 }
1839 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001840 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001841
1842 return 0;
1843}
1844
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001845static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
1846{
1847 struct smp_cmd_public_key *key = (void *) skb->data;
1848 struct hci_conn *hcon = conn->hcon;
1849 struct l2cap_chan *chan = conn->smp;
1850 struct smp_chan *smp = chan->data;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03001851 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001852 int err;
1853
1854 BT_DBG("conn %p", conn);
1855
1856 if (skb->len < sizeof(*key))
1857 return SMP_INVALID_PARAMS;
1858
1859 memcpy(smp->remote_pk, key, 64);
1860
1861 /* Non-initiating device sends its public key after receiving
1862 * the key from the initiating device.
1863 */
1864 if (!hcon->out) {
1865 err = sc_send_public_key(smp);
1866 if (err)
1867 return err;
1868 }
1869
1870 BT_DBG("Remote Public Key X: %32phN", smp->remote_pk);
1871 BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
1872
1873 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
1874 return SMP_UNSPECIFIED;
1875
1876 BT_DBG("DHKey %32phN", smp->dhkey);
1877
1878 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
1879
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03001880 /* The Initiating device waits for the non-initiating device to
1881 * send the confirm value.
1882 */
1883 if (conn->hcon->out)
1884 return 0;
1885
1886 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
1887 0, cfm.confirm_val);
1888 if (err)
1889 return SMP_UNSPECIFIED;
1890
1891 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1892 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1893
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001894 return 0;
1895}
1896
Johan Hedberg6433a9a2014-06-06 11:47:30 +03001897static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
1898{
1899 struct smp_cmd_dhkey_check *check = (void *) skb->data;
1900 struct l2cap_chan *chan = conn->smp;
1901 struct hci_conn *hcon = conn->hcon;
1902 struct smp_chan *smp = chan->data;
1903 u8 a[7], b[7], *local_addr, *remote_addr;
1904 u8 io_cap[3], r[16], e[16];
1905 int err;
1906
1907 BT_DBG("conn %p", conn);
1908
1909 if (skb->len < sizeof(*check))
1910 return SMP_INVALID_PARAMS;
1911
1912 memcpy(a, &hcon->init_addr, 6);
1913 memcpy(b, &hcon->resp_addr, 6);
1914 a[6] = hcon->init_addr_type;
1915 b[6] = hcon->resp_addr_type;
1916
1917 if (hcon->out) {
1918 local_addr = a;
1919 remote_addr = b;
1920 memcpy(io_cap, &smp->prsp[1], 3);
1921 } else {
1922 local_addr = b;
1923 remote_addr = a;
1924 memcpy(io_cap, &smp->preq[1], 3);
1925 }
1926
1927 memset(r, 0, sizeof(r));
1928
1929 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
1930 io_cap, remote_addr, local_addr, e);
1931 if (err)
1932 return SMP_UNSPECIFIED;
1933
1934 if (memcmp(check->e, e, 16))
1935 return SMP_DHKEY_CHECK_FAILED;
1936
1937 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1938 SMP_LTK_P256, 0, smp->tk, smp->enc_key_size,
1939 0, 0);
1940
1941 if (hcon->out) {
1942 hci_le_start_enc(hcon, 0, 0, smp->tk);
1943 hcon->enc_key_size = smp->enc_key_size;
1944 }
1945
1946 return 0;
1947}
1948
Johan Hedberg4befb862014-08-11 22:06:38 +03001949static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001950{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001951 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001952 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001953 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001954 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001955 int err = 0;
1956
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001957 if (hcon->type != LE_LINK) {
1958 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001959 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001960 }
1961
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001962 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001963 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001964
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001965 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001966 reason = SMP_PAIRING_NOTSUPP;
1967 goto done;
1968 }
1969
Marcel Holtmann92381f52013-10-03 01:23:08 -07001970 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001971 skb_pull(skb, sizeof(code));
1972
Johan Hedbergb28b4942014-09-05 22:19:55 +03001973 smp = chan->data;
1974
1975 if (code > SMP_CMD_MAX)
1976 goto drop;
1977
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07001978 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03001979 goto drop;
1980
1981 /* If we don't have a context the only allowed commands are
1982 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001983 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001984 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1985 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001986
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001987 switch (code) {
1988 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001989 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001990 break;
1991
1992 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001993 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001994 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001995 break;
1996
1997 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001998 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001999 break;
2000
2001 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002002 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002003 break;
2004
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002005 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002006 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002007 break;
2008
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002009 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002010 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002011 break;
2012
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002013 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002014 reason = smp_cmd_encrypt_info(conn, skb);
2015 break;
2016
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002017 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002018 reason = smp_cmd_master_ident(conn, skb);
2019 break;
2020
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002021 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002022 reason = smp_cmd_ident_info(conn, skb);
2023 break;
2024
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002025 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002026 reason = smp_cmd_ident_addr_info(conn, skb);
2027 break;
2028
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002029 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002030 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002031 break;
2032
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002033 case SMP_CMD_PUBLIC_KEY:
2034 reason = smp_cmd_public_key(conn, skb);
2035 break;
2036
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002037 case SMP_CMD_DHKEY_CHECK:
2038 reason = smp_cmd_dhkey_check(conn, skb);
2039 break;
2040
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002041 default:
2042 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002043 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002044 goto done;
2045 }
2046
2047done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002048 if (!err) {
2049 if (reason)
2050 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002051 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002052 }
2053
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002054 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002055
2056drop:
2057 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2058 code, &hcon->dst);
2059 kfree_skb(skb);
2060 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002061}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002062
Johan Hedberg70db83c2014-08-08 09:37:16 +03002063static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2064{
2065 struct l2cap_conn *conn = chan->conn;
2066
2067 BT_DBG("chan %p", chan);
2068
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002069 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002070 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002071
Johan Hedberg70db83c2014-08-08 09:37:16 +03002072 conn->smp = NULL;
2073 l2cap_chan_put(chan);
2074}
2075
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002076static void smp_resume_cb(struct l2cap_chan *chan)
2077{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002078 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002079 struct l2cap_conn *conn = chan->conn;
2080 struct hci_conn *hcon = conn->hcon;
2081
2082 BT_DBG("chan %p", chan);
2083
Johan Hedberg86d14072014-08-11 22:06:43 +03002084 if (!smp)
2085 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002086
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002087 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2088 return;
2089
Johan Hedberg86d14072014-08-11 22:06:43 +03002090 cancel_delayed_work(&smp->security_timer);
2091
Johan Hedbergd6268e82014-09-05 22:19:51 +03002092 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002093}
2094
Johan Hedberg70db83c2014-08-08 09:37:16 +03002095static void smp_ready_cb(struct l2cap_chan *chan)
2096{
2097 struct l2cap_conn *conn = chan->conn;
2098
2099 BT_DBG("chan %p", chan);
2100
2101 conn->smp = chan;
2102 l2cap_chan_hold(chan);
2103}
2104
Johan Hedberg4befb862014-08-11 22:06:38 +03002105static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2106{
2107 int err;
2108
2109 BT_DBG("chan %p", chan);
2110
2111 err = smp_sig_channel(chan, skb);
2112 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002113 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002114
Johan Hedbergb68fda62014-08-11 22:06:40 +03002115 if (smp)
2116 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002117
Johan Hedberg1e91c292014-08-18 20:33:29 +03002118 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002119 }
2120
2121 return err;
2122}
2123
Johan Hedberg70db83c2014-08-08 09:37:16 +03002124static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2125 unsigned long hdr_len,
2126 unsigned long len, int nb)
2127{
2128 struct sk_buff *skb;
2129
2130 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2131 if (!skb)
2132 return ERR_PTR(-ENOMEM);
2133
2134 skb->priority = HCI_PRIO_MAX;
2135 bt_cb(skb)->chan = chan;
2136
2137 return skb;
2138}
2139
2140static const struct l2cap_ops smp_chan_ops = {
2141 .name = "Security Manager",
2142 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002143 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002144 .alloc_skb = smp_alloc_skb_cb,
2145 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002146 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002147
2148 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002149 .state_change = l2cap_chan_no_state_change,
2150 .close = l2cap_chan_no_close,
2151 .defer = l2cap_chan_no_defer,
2152 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002153 .set_shutdown = l2cap_chan_no_set_shutdown,
2154 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2155 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2156};
2157
2158static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2159{
2160 struct l2cap_chan *chan;
2161
2162 BT_DBG("pchan %p", pchan);
2163
2164 chan = l2cap_chan_create();
2165 if (!chan)
2166 return NULL;
2167
2168 chan->chan_type = pchan->chan_type;
2169 chan->ops = &smp_chan_ops;
2170 chan->scid = pchan->scid;
2171 chan->dcid = chan->scid;
2172 chan->imtu = pchan->imtu;
2173 chan->omtu = pchan->omtu;
2174 chan->mode = pchan->mode;
2175
Johan Hedbergabe84902014-11-12 22:22:21 +02002176 /* Other L2CAP channels may request SMP routines in order to
2177 * change the security level. This means that the SMP channel
2178 * lock must be considered in its own category to avoid lockdep
2179 * warnings.
2180 */
2181 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2182
Johan Hedberg70db83c2014-08-08 09:37:16 +03002183 BT_DBG("created chan %p", chan);
2184
2185 return chan;
2186}
2187
2188static const struct l2cap_ops smp_root_chan_ops = {
2189 .name = "Security Manager Root",
2190 .new_connection = smp_new_conn_cb,
2191
2192 /* None of these are implemented for the root channel */
2193 .close = l2cap_chan_no_close,
2194 .alloc_skb = l2cap_chan_no_alloc_skb,
2195 .recv = l2cap_chan_no_recv,
2196 .state_change = l2cap_chan_no_state_change,
2197 .teardown = l2cap_chan_no_teardown,
2198 .ready = l2cap_chan_no_ready,
2199 .defer = l2cap_chan_no_defer,
2200 .suspend = l2cap_chan_no_suspend,
2201 .resume = l2cap_chan_no_resume,
2202 .set_shutdown = l2cap_chan_no_set_shutdown,
2203 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2204 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2205};
2206
Johan Hedberg711eafe2014-08-08 09:32:52 +03002207int smp_register(struct hci_dev *hdev)
2208{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002209 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002210 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002211
Johan Hedberg711eafe2014-08-08 09:32:52 +03002212 BT_DBG("%s", hdev->name);
2213
Johan Hedbergadae20c2014-11-13 14:37:48 +02002214 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002215 if (IS_ERR(tfm_aes)) {
2216 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002217 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03002218 return err;
2219 }
2220
Johan Hedberg70db83c2014-08-08 09:37:16 +03002221 chan = l2cap_chan_create();
2222 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002223 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002224 return -ENOMEM;
2225 }
2226
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002227 chan->data = tfm_aes;
2228
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002229 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002230
2231 l2cap_chan_set_defaults(chan);
2232
2233 bacpy(&chan->src, &hdev->bdaddr);
2234 chan->src_type = BDADDR_LE_PUBLIC;
2235 chan->state = BT_LISTEN;
2236 chan->mode = L2CAP_MODE_BASIC;
2237 chan->imtu = L2CAP_DEFAULT_MTU;
2238 chan->ops = &smp_root_chan_ops;
2239
Johan Hedbergabe84902014-11-12 22:22:21 +02002240 /* Set correct nesting level for a parent/listening channel */
2241 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2242
Johan Hedberg70db83c2014-08-08 09:37:16 +03002243 hdev->smp_data = chan;
2244
Johan Hedberg711eafe2014-08-08 09:32:52 +03002245 return 0;
2246}
2247
2248void smp_unregister(struct hci_dev *hdev)
2249{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002250 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002251 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002252
2253 if (!chan)
2254 return;
2255
2256 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002257
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002258 tfm_aes = chan->data;
2259 if (tfm_aes) {
2260 chan->data = NULL;
2261 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002262 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03002263
2264 hdev->smp_data = NULL;
2265 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002266}