blob: 9317583751eb2d74d677e379a646d4c0a4e6c2e7 [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 Hedberg533e35d2014-06-16 19:25:18 +030046enum {
47 SMP_FLAG_TK_VALID,
48 SMP_FLAG_CFM_PENDING,
49 SMP_FLAG_MITM_AUTH,
50 SMP_FLAG_COMPLETE,
51 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030052 SMP_FLAG_SC,
Johan Hedberg533e35d2014-06-16 19:25:18 +030053};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030054
55struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030056 struct l2cap_conn *conn;
57 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030058 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030059
Johan Hedberg4bc58f52014-05-20 09:45:47 +030060 u8 preq[7]; /* SMP Pairing Request */
61 u8 prsp[7]; /* SMP Pairing Response */
62 u8 prnd[16]; /* SMP Pairing Random (local) */
63 u8 rrnd[16]; /* SMP Pairing Random (remote) */
64 u8 pcnf[16]; /* SMP Pairing Confirm */
65 u8 tk[16]; /* SMP Temporary Key */
66 u8 enc_key_size;
67 u8 remote_key_dist;
68 bdaddr_t id_addr;
69 u8 id_addr_type;
70 u8 irk[16];
71 struct smp_csrk *csrk;
72 struct smp_csrk *slave_csrk;
73 struct smp_ltk *ltk;
74 struct smp_ltk *slave_ltk;
75 struct smp_irk *remote_irk;
Johan Hedberg4a74d652014-05-20 09:45:50 +030076 unsigned long flags;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030077
Johan Hedberg3b191462014-06-06 10:50:15 +030078 /* Secure Connections variables */
79 u8 local_pk[64];
80 u8 local_sk[32];
81
Johan Hedberg6a7bd102014-06-27 14:23:03 +030082 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +030083 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030084};
85
Johan Hedberg8a2936f2014-06-16 19:25:19 +030086static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030087{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030088 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030089
Johan Hedberg8a2936f2014-06-16 19:25:19 +030090 for (i = 0; i < len; i++)
91 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030092}
93
94static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
95{
96 struct blkcipher_desc desc;
97 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +020098 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +020099 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300100
101 if (tfm == NULL) {
102 BT_ERR("tfm %p", tfm);
103 return -EINVAL;
104 }
105
106 desc.tfm = tfm;
107 desc.flags = 0;
108
Johan Hedberg943a7322014-03-18 12:58:24 +0200109 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300110 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200111
112 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300113 if (err) {
114 BT_ERR("cipher setkey failed: %d", err);
115 return err;
116 }
117
Johan Hedberg943a7322014-03-18 12:58:24 +0200118 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300119 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200120
121 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300122
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300123 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
124 if (err)
125 BT_ERR("Encrypt data error %d", err);
126
Johan Hedberg943a7322014-03-18 12:58:24 +0200127 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300128 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200129
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300130 return err;
131}
132
Johan Hedberg60478052014-02-18 10:19:31 +0200133static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
134{
Johan Hedberg943a7322014-03-18 12:58:24 +0200135 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200136 int err;
137
138 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200139 memcpy(_res, r, 3);
140 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200141
Johan Hedberg943a7322014-03-18 12:58:24 +0200142 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200143 if (err) {
144 BT_ERR("Encrypt error");
145 return err;
146 }
147
148 /* The output of the random address function ah is:
149 * ah(h, r) = e(k, r') mod 2^24
150 * The output of the security function e is then truncated to 24 bits
151 * by taking the least significant 24 bits of the output of e as the
152 * result of ah.
153 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200154 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200155
156 return 0;
157}
158
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300159bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200160{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300161 struct l2cap_chan *chan = hdev->smp_data;
162 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200163 u8 hash[3];
164 int err;
165
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300166 if (!chan || !chan->data)
167 return false;
168
169 tfm = chan->data;
170
Johan Hedberg60478052014-02-18 10:19:31 +0200171 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
172
173 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
174 if (err)
175 return false;
176
177 return !memcmp(bdaddr->b, hash, 3);
178}
179
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300180int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200181{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300182 struct l2cap_chan *chan = hdev->smp_data;
183 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200184 int err;
185
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300186 if (!chan || !chan->data)
187 return -EOPNOTSUPP;
188
189 tfm = chan->data;
190
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200191 get_random_bytes(&rpa->b[3], 3);
192
193 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
194 rpa->b[5] |= 0x40; /* Set second most significant bit */
195
196 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
197 if (err < 0)
198 return err;
199
200 BT_DBG("RPA %pMR", rpa);
201
202 return 0;
203}
204
Johan Hedberge491eaf2014-10-25 21:15:37 +0200205static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
206 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
207 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300208{
209 u8 p1[16], p2[16];
210 int err;
211
212 memset(p1, 0, 16);
213
214 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200215 p1[0] = _iat;
216 p1[1] = _rat;
217 memcpy(p1 + 2, preq, 7);
218 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300219
220 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200221 memcpy(p2, ra, 6);
222 memcpy(p2 + 6, ia, 6);
223 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300224
225 /* res = r XOR p1 */
226 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
227
228 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200229 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300230 if (err) {
231 BT_ERR("Encrypt data error");
232 return err;
233 }
234
235 /* res = res XOR p2 */
236 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
237
238 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200239 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300240 if (err)
241 BT_ERR("Encrypt data error");
242
243 return err;
244}
245
Johan Hedberge491eaf2014-10-25 21:15:37 +0200246static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
247 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300248{
249 int err;
250
251 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200252 memcpy(_r, r2, 8);
253 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300254
Johan Hedberge491eaf2014-10-25 21:15:37 +0200255 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300256 if (err)
257 BT_ERR("Encrypt data error");
258
259 return err;
260}
261
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300262static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
263{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300264 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300265 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300266 struct kvec iv[2];
267 struct msghdr msg;
268
269 if (!chan)
270 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300271
272 BT_DBG("code 0x%2.2x", code);
273
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300274 iv[0].iov_base = &code;
275 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300276
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300277 iv[1].iov_base = data;
278 iv[1].iov_len = len;
279
280 memset(&msg, 0, sizeof(msg));
281
282 msg.msg_iov = (struct iovec *) &iv;
283 msg.msg_iovlen = 2;
284
285 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300286
Johan Hedbergb68fda62014-08-11 22:06:40 +0300287 if (!chan->data)
288 return;
289
290 smp = chan->data;
291
292 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300293 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300294}
295
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300296static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800297{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300298 if (authreq & SMP_AUTH_MITM) {
299 if (authreq & SMP_AUTH_SC)
300 return BT_SECURITY_FIPS;
301 else
302 return BT_SECURITY_HIGH;
303 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800304 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300305 }
Brian Gix2b64d152011-12-21 16:12:12 -0800306}
307
308static __u8 seclevel_to_authreq(__u8 sec_level)
309{
310 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300311 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800312 case BT_SECURITY_HIGH:
313 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
314 case BT_SECURITY_MEDIUM:
315 return SMP_AUTH_BONDING;
316 default:
317 return SMP_AUTH_NONE;
318 }
319}
320
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300321static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700322 struct smp_cmd_pairing *req,
323 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300324{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300325 struct l2cap_chan *chan = conn->smp;
326 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200327 struct hci_conn *hcon = conn->hcon;
328 struct hci_dev *hdev = hcon->hdev;
329 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300330
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300331 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700332 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
333 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300334 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800335 } else {
336 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300337 }
338
Johan Hedbergfd349c02014-02-18 10:19:36 +0200339 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
340 remote_dist |= SMP_DIST_ID_KEY;
341
Johan Hedberg863efaf2014-02-22 19:06:32 +0200342 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
343 local_dist |= SMP_DIST_ID_KEY;
344
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300345 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
346 if ((authreq & SMP_AUTH_SC) &&
347 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
348 local_dist |= SMP_DIST_LINK_KEY;
349 remote_dist |= SMP_DIST_LINK_KEY;
350 }
351 } else {
352 authreq &= ~SMP_AUTH_SC;
353 }
354
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300355 if (rsp == NULL) {
356 req->io_capability = conn->hcon->io_capability;
357 req->oob_flag = SMP_OOB_NOT_PRESENT;
358 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200359 req->init_key_dist = local_dist;
360 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300361 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200362
363 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300364 return;
365 }
366
367 rsp->io_capability = conn->hcon->io_capability;
368 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
369 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200370 rsp->init_key_dist = req->init_key_dist & remote_dist;
371 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300372 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200373
374 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300375}
376
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300377static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
378{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300379 struct l2cap_chan *chan = conn->smp;
380 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300381
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300382 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700383 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300384 return SMP_ENC_KEY_SIZE;
385
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300386 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300387
388 return 0;
389}
390
Johan Hedberg6f48e262014-08-11 22:06:44 +0300391static void smp_chan_destroy(struct l2cap_conn *conn)
392{
393 struct l2cap_chan *chan = conn->smp;
394 struct smp_chan *smp = chan->data;
395 bool complete;
396
397 BUG_ON(!smp);
398
399 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300400
Johan Hedberg6f48e262014-08-11 22:06:44 +0300401 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
402 mgmt_smp_complete(conn->hcon, complete);
403
404 kfree(smp->csrk);
405 kfree(smp->slave_csrk);
406
407 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300408 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300409
410 /* If pairing failed clean up any keys we might have */
411 if (!complete) {
412 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200413 list_del_rcu(&smp->ltk->list);
414 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300415 }
416
417 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200418 list_del_rcu(&smp->slave_ltk->list);
419 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300420 }
421
422 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200423 list_del_rcu(&smp->remote_irk->list);
424 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300425 }
426 }
427
428 chan->data = NULL;
429 kfree(smp);
430 hci_conn_drop(conn->hcon);
431}
432
Johan Hedberg84794e12013-11-06 11:24:57 +0200433static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800434{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200435 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300436 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200437
Johan Hedberg84794e12013-11-06 11:24:57 +0200438 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800439 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700440 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800441
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700442 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700443 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300444
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300445 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300446 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800447}
448
Brian Gix2b64d152011-12-21 16:12:12 -0800449#define JUST_WORKS 0x00
450#define JUST_CFM 0x01
451#define REQ_PASSKEY 0x02
452#define CFM_PASSKEY 0x03
453#define REQ_OOB 0x04
454#define OVERLAP 0xFF
455
456static const u8 gen_method[5][5] = {
457 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
458 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
459 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
460 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
461 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
462};
463
Johan Hedberg581370c2014-06-17 13:07:38 +0300464static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
465{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300466 /* If either side has unknown io_caps, use JUST_CFM (which gets
467 * converted later to JUST_WORKS if we're initiators.
468 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300469 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
470 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300471 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300472
473 return gen_method[remote_io][local_io];
474}
475
Brian Gix2b64d152011-12-21 16:12:12 -0800476static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
477 u8 local_io, u8 remote_io)
478{
479 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300480 struct l2cap_chan *chan = conn->smp;
481 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800482 u8 method;
483 u32 passkey = 0;
484 int ret = 0;
485
486 /* Initialize key for JUST WORKS */
487 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300488 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800489
490 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
491
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300492 /* If neither side wants MITM, either "just" confirm an incoming
493 * request or use just-works for outgoing ones. The JUST_CFM
494 * will be converted to JUST_WORKS if necessary later in this
495 * function. If either side has MITM look up the method from the
496 * table.
497 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300498 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300499 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800500 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300501 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800502
Johan Hedberga82505c2014-03-24 14:39:07 +0200503 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300504 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200505 method = JUST_WORKS;
506
Johan Hedberg02f3e252014-07-16 15:09:13 +0300507 /* Don't bother user space with no IO capabilities */
508 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
509 method = JUST_WORKS;
510
Brian Gix2b64d152011-12-21 16:12:12 -0800511 /* If Just Works, Continue with Zero TK */
512 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300513 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800514 return 0;
515 }
516
517 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300518 if (method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300519 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300520 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
521 hcon->pending_sec_level = BT_SECURITY_HIGH;
522 }
Brian Gix2b64d152011-12-21 16:12:12 -0800523
524 /* If both devices have Keyoard-Display I/O, the master
525 * Confirms and the slave Enters the passkey.
526 */
527 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300528 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800529 method = CFM_PASSKEY;
530 else
531 method = REQ_PASSKEY;
532 }
533
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200534 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800535 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200536 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800537 get_random_bytes(&passkey, sizeof(passkey));
538 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200539 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800540 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300541 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800542 }
543
Brian Gix2b64d152011-12-21 16:12:12 -0800544 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700545 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200546 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200547 else if (method == JUST_CFM)
548 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
549 hcon->type, hcon->dst_type,
550 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800551 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200552 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200553 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200554 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800555
Brian Gix2b64d152011-12-21 16:12:12 -0800556 return ret;
557}
558
Johan Hedberg1cc61142014-05-20 09:45:52 +0300559static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300560{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300561 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300562 struct smp_cmd_pairing_confirm cp;
563 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300564
565 BT_DBG("conn %p", conn);
566
Johan Hedberge491eaf2014-10-25 21:15:37 +0200567 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200568 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200569 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
570 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300571 if (ret)
572 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300573
Johan Hedberg4a74d652014-05-20 09:45:50 +0300574 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800575
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300576 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
577
Johan Hedbergb28b4942014-09-05 22:19:55 +0300578 if (conn->hcon->out)
579 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
580 else
581 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
582
Johan Hedberg1cc61142014-05-20 09:45:52 +0300583 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300584}
585
Johan Hedberg861580a2014-05-20 09:45:51 +0300586static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300587{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300588 struct l2cap_conn *conn = smp->conn;
589 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300590 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300591 int ret;
592
Johan Hedbergec70f362014-06-27 14:23:04 +0300593 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300594 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300595
596 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
597
Johan Hedberge491eaf2014-10-25 21:15:37 +0200598 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200599 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200600 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300601 if (ret)
602 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300603
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300604 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
605 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300606 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300607 }
608
609 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800610 u8 stk[16];
611 __le64 rand = 0;
612 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300613
Johan Hedberge491eaf2014-10-25 21:15:37 +0200614 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300615
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300616 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300617 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300618
Johan Hedberg861580a2014-05-20 09:45:51 +0300619 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
620 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300621
622 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300623 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300624 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300625 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300626 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800627 __le64 rand = 0;
628 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300629
Johan Hedberg943a7322014-03-18 12:58:24 +0200630 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
631 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300632
Johan Hedberge491eaf2014-10-25 21:15:37 +0200633 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300634
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300635 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700636 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300637
Johan Hedbergfff34902014-06-10 15:19:50 +0300638 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
639 auth = 1;
640 else
641 auth = 0;
642
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300643 /* Even though there's no _SLAVE suffix this is the
644 * slave STK we're adding for later lookup (the master
645 * STK never needs to be stored).
646 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700647 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300648 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300649 }
650
Johan Hedberg861580a2014-05-20 09:45:51 +0300651 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300652}
653
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300654static void smp_notify_keys(struct l2cap_conn *conn)
655{
656 struct l2cap_chan *chan = conn->smp;
657 struct smp_chan *smp = chan->data;
658 struct hci_conn *hcon = conn->hcon;
659 struct hci_dev *hdev = hcon->hdev;
660 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
661 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
662 bool persistent;
663
664 if (smp->remote_irk) {
665 mgmt_new_irk(hdev, smp->remote_irk);
666 /* Now that user space can be considered to know the
667 * identity address track the connection based on it
668 * from now on.
669 */
670 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
671 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300672 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300673
674 /* When receiving an indentity resolving key for
675 * a remote device that does not use a resolvable
676 * private address, just remove the key so that
677 * it is possible to use the controller white
678 * list for scanning.
679 *
680 * Userspace will have been told to not store
681 * this key at this point. So it is safe to
682 * just remove it.
683 */
684 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200685 list_del_rcu(&smp->remote_irk->list);
686 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300687 smp->remote_irk = NULL;
688 }
689 }
690
691 /* The LTKs and CSRKs should be persistent only if both sides
692 * had the bonding bit set in their authentication requests.
693 */
694 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
695
696 if (smp->csrk) {
697 smp->csrk->bdaddr_type = hcon->dst_type;
698 bacpy(&smp->csrk->bdaddr, &hcon->dst);
699 mgmt_new_csrk(hdev, smp->csrk, persistent);
700 }
701
702 if (smp->slave_csrk) {
703 smp->slave_csrk->bdaddr_type = hcon->dst_type;
704 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
705 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
706 }
707
708 if (smp->ltk) {
709 smp->ltk->bdaddr_type = hcon->dst_type;
710 bacpy(&smp->ltk->bdaddr, &hcon->dst);
711 mgmt_new_ltk(hdev, smp->ltk, persistent);
712 }
713
714 if (smp->slave_ltk) {
715 smp->slave_ltk->bdaddr_type = hcon->dst_type;
716 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
717 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
718 }
719}
720
Johan Hedbergb28b4942014-09-05 22:19:55 +0300721static void smp_allow_key_dist(struct smp_chan *smp)
722{
723 /* Allow the first expected phase 3 PDU. The rest of the PDUs
724 * will be allowed in each PDU handler to ensure we receive
725 * them in the correct order.
726 */
727 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
728 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
729 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
730 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
731 else if (smp->remote_key_dist & SMP_DIST_SIGN)
732 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
733}
734
Johan Hedbergd6268e82014-09-05 22:19:51 +0300735static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300736{
737 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300738 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300739 struct hci_conn *hcon = conn->hcon;
740 struct hci_dev *hdev = hcon->hdev;
741 __u8 *keydist;
742
743 BT_DBG("conn %p", conn);
744
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300745 rsp = (void *) &smp->prsp[1];
746
747 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300748 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
749 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300750 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300751 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300752
753 req = (void *) &smp->preq[1];
754
755 if (hcon->out) {
756 keydist = &rsp->init_key_dist;
757 *keydist &= req->init_key_dist;
758 } else {
759 keydist = &rsp->resp_key_dist;
760 *keydist &= req->resp_key_dist;
761 }
762
763 BT_DBG("keydist 0x%x", *keydist);
764
765 if (*keydist & SMP_DIST_ENC_KEY) {
766 struct smp_cmd_encrypt_info enc;
767 struct smp_cmd_master_ident ident;
768 struct smp_ltk *ltk;
769 u8 authenticated;
770 __le16 ediv;
771 __le64 rand;
772
773 get_random_bytes(enc.ltk, sizeof(enc.ltk));
774 get_random_bytes(&ediv, sizeof(ediv));
775 get_random_bytes(&rand, sizeof(rand));
776
777 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
778
779 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
780 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
781 SMP_LTK_SLAVE, authenticated, enc.ltk,
782 smp->enc_key_size, ediv, rand);
783 smp->slave_ltk = ltk;
784
785 ident.ediv = ediv;
786 ident.rand = rand;
787
788 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
789
790 *keydist &= ~SMP_DIST_ENC_KEY;
791 }
792
793 if (*keydist & SMP_DIST_ID_KEY) {
794 struct smp_cmd_ident_addr_info addrinfo;
795 struct smp_cmd_ident_info idinfo;
796
797 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
798
799 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
800
801 /* The hci_conn contains the local identity address
802 * after the connection has been established.
803 *
804 * This is true even when the connection has been
805 * established using a resolvable random address.
806 */
807 bacpy(&addrinfo.bdaddr, &hcon->src);
808 addrinfo.addr_type = hcon->src_type;
809
810 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
811 &addrinfo);
812
813 *keydist &= ~SMP_DIST_ID_KEY;
814 }
815
816 if (*keydist & SMP_DIST_SIGN) {
817 struct smp_cmd_sign_info sign;
818 struct smp_csrk *csrk;
819
820 /* Generate a new random key */
821 get_random_bytes(sign.csrk, sizeof(sign.csrk));
822
823 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
824 if (csrk) {
825 csrk->master = 0x00;
826 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
827 }
828 smp->slave_csrk = csrk;
829
830 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
831
832 *keydist &= ~SMP_DIST_SIGN;
833 }
834
835 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300836 if (smp->remote_key_dist & KEY_DIST_MASK) {
837 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300838 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300839 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300840
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300841 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
842 smp_notify_keys(conn);
843
844 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300845}
846
Johan Hedbergb68fda62014-08-11 22:06:40 +0300847static void smp_timeout(struct work_struct *work)
848{
849 struct smp_chan *smp = container_of(work, struct smp_chan,
850 security_timer.work);
851 struct l2cap_conn *conn = smp->conn;
852
853 BT_DBG("conn %p", conn);
854
Johan Hedberg1e91c292014-08-18 20:33:29 +0300855 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300856}
857
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300858static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
859{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300860 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300861 struct smp_chan *smp;
862
Marcel Holtmannf1560462013-10-13 05:43:25 -0700863 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300864 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300865 return NULL;
866
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300867 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
868 if (IS_ERR(smp->tfm_aes)) {
869 BT_ERR("Unable to create ECB crypto context");
870 kfree(smp);
871 return NULL;
872 }
873
Johan Hedberg407cecf2014-05-02 14:19:47 +0300874 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
875 if (IS_ERR(smp->tfm_cmac)) {
876 BT_ERR("Unable to create CMAC crypto context");
877 crypto_free_blkcipher(smp->tfm_aes);
878 kfree(smp);
879 return NULL;
880 }
881
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300882 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300883 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300884
Johan Hedbergb28b4942014-09-05 22:19:55 +0300885 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
886
Johan Hedbergb68fda62014-08-11 22:06:40 +0300887 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
888
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300889 hci_conn_hold(conn->hcon);
890
891 return smp;
892}
893
Brian Gix2b64d152011-12-21 16:12:12 -0800894int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
895{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300896 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300897 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800898 struct smp_chan *smp;
899 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300900 int err;
Brian Gix2b64d152011-12-21 16:12:12 -0800901
902 BT_DBG("");
903
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300904 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -0800905 return -ENOTCONN;
906
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300907 chan = conn->smp;
908 if (!chan)
909 return -ENOTCONN;
910
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300911 l2cap_chan_lock(chan);
912 if (!chan->data) {
913 err = -ENOTCONN;
914 goto unlock;
915 }
916
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300917 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800918
919 switch (mgmt_op) {
920 case MGMT_OP_USER_PASSKEY_REPLY:
921 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200922 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800923 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200924 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800925 /* Fall Through */
926 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300927 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800928 break;
929 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
930 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200931 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300932 err = 0;
933 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800934 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200935 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300936 err = -EOPNOTSUPP;
937 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800938 }
939
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300940 err = 0;
941
Brian Gix2b64d152011-12-21 16:12:12 -0800942 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300943 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
944 u8 rsp = smp_confirm(smp);
945 if (rsp)
946 smp_failure(conn, rsp);
947 }
Brian Gix2b64d152011-12-21 16:12:12 -0800948
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300949unlock:
950 l2cap_chan_unlock(chan);
951 return err;
Brian Gix2b64d152011-12-21 16:12:12 -0800952}
953
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300954static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300955{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300956 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300957 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300958 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300959 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300960 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300961 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300962
963 BT_DBG("conn %p", conn);
964
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200965 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300966 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200967
Johan Hedberg40bef302014-07-16 11:42:27 +0300968 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800969 return SMP_CMD_NOTSUPP;
970
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300971 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300972 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300973 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300974 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300975
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300976 if (!smp)
977 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300978
Johan Hedbergc05b9332014-09-10 17:37:42 -0700979 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300980 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -0700981
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300982 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -0700983 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +0300984 return SMP_PAIRING_NOTSUPP;
985
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300986 smp->preq[0] = SMP_CMD_PAIRING_REQ;
987 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300988 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300989
Johan Hedberg5be5e272014-09-10 17:58:54 -0700990 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -0700991 sec_level = BT_SECURITY_MEDIUM;
992 else
993 sec_level = authreq_to_seclevel(auth);
994
Johan Hedbergc7262e72014-06-17 13:07:37 +0300995 if (sec_level > conn->hcon->pending_sec_level)
996 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200997
Stephen Hemminger49c922b2014-10-27 21:12:20 -0700998 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300999 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1000 u8 method;
1001
1002 method = get_auth_method(smp, conn->hcon->io_capability,
1003 req->io_capability);
1004 if (method == JUST_WORKS || method == JUST_CFM)
1005 return SMP_AUTH_REQUIREMENTS;
1006 }
1007
Brian Gix2b64d152011-12-21 16:12:12 -08001008 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001009
Johan Hedberg65668772014-05-16 11:03:34 +03001010 if (rsp.auth_req & SMP_AUTH_SC)
1011 set_bit(SMP_FLAG_SC, &smp->flags);
1012
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001013 key_size = min(req->max_key_size, rsp.max_key_size);
1014 if (check_enc_key_size(conn, key_size))
1015 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001016
Johan Hedberge84a6b12013-12-02 10:49:03 +02001017 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001018
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001019 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1020 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001021
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001022 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001023
1024 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1025
1026 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1027 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1028 /* Clear bits which are generated but not distributed */
1029 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1030 /* Wait for Public Key from Initiating Device */
1031 return 0;
1032 } else {
1033 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1034 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001035
Brian Gix2b64d152011-12-21 16:12:12 -08001036 /* Request setup of TK */
1037 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1038 if (ret)
1039 return SMP_UNSPECIFIED;
1040
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001041 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001042}
1043
Johan Hedberg3b191462014-06-06 10:50:15 +03001044static u8 sc_send_public_key(struct smp_chan *smp)
1045{
1046 BT_DBG("");
1047
1048 /* Generate local key pair for Secure Connections */
1049 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1050 return SMP_UNSPECIFIED;
1051
1052 BT_DBG("Local Public Key X: %32phN", smp->local_pk);
1053 BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1054 BT_DBG("Local Private Key: %32phN", smp->local_sk);
1055
1056 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1057
1058 return 0;
1059}
1060
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001061static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001062{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001063 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001064 struct l2cap_chan *chan = conn->smp;
1065 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001066 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001067 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001068 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001069
1070 BT_DBG("conn %p", conn);
1071
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001072 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001073 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001074
Johan Hedberg40bef302014-07-16 11:42:27 +03001075 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001076 return SMP_CMD_NOTSUPP;
1077
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001078 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001079
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001080 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001081
1082 key_size = min(req->max_key_size, rsp->max_key_size);
1083 if (check_enc_key_size(conn, key_size))
1084 return SMP_ENC_KEY_SIZE;
1085
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001086 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001087
Johan Hedberg65668772014-05-16 11:03:34 +03001088 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1089 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001090 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1091 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001092
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001093 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001094 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1095 u8 method;
1096
1097 method = get_auth_method(smp, req->io_capability,
1098 rsp->io_capability);
1099 if (method == JUST_WORKS || method == JUST_CFM)
1100 return SMP_AUTH_REQUIREMENTS;
1101 }
1102
Johan Hedberge84a6b12013-12-02 10:49:03 +02001103 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001104
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001105 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1106 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001107
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001108 /* Update remote key distribution in case the remote cleared
1109 * some bits that we had enabled in our request.
1110 */
1111 smp->remote_key_dist &= rsp->resp_key_dist;
1112
Johan Hedberg3b191462014-06-06 10:50:15 +03001113 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1114 /* Clear bits which are generated but not distributed */
1115 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1116 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1117 return sc_send_public_key(smp);
1118 }
1119
Johan Hedbergc05b9332014-09-10 17:37:42 -07001120 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001121
Johan Hedberg476585e2012-06-06 18:54:15 +08001122 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001123 if (ret)
1124 return SMP_UNSPECIFIED;
1125
Johan Hedberg4a74d652014-05-20 09:45:50 +03001126 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001127
1128 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001129 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001130 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001131
1132 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001133}
1134
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001135static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001136{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001137 struct l2cap_chan *chan = conn->smp;
1138 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001139
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001140 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1141
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001142 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001143 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001144
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001145 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1146 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001147
Johan Hedbergb28b4942014-09-05 22:19:55 +03001148 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001149 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1150 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001151 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1152 return 0;
1153 }
1154
1155 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001156 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001157 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001158 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001159
1160 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001161}
1162
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001163static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001164{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001165 struct l2cap_chan *chan = conn->smp;
1166 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001167
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001168 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001169
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001170 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001171 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001172
Johan Hedberg943a7322014-03-18 12:58:24 +02001173 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001174 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001175
Johan Hedberg861580a2014-05-20 09:45:51 +03001176 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001177}
1178
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001179static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001180{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001181 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001182 struct hci_conn *hcon = conn->hcon;
1183
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001184 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001185 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001186 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001187
Johan Hedberga6f78332014-09-10 17:37:45 -07001188 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001189 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001190
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001191 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001192 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001193
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001194 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1195 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001196
Johan Hedbergfe59a052014-07-01 19:14:12 +03001197 /* We never store STKs for master role, so clear this flag */
1198 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1199
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001200 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001201}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001202
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001203bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1204 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001205{
1206 if (sec_level == BT_SECURITY_LOW)
1207 return true;
1208
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001209 /* If we're encrypted with an STK but the caller prefers using
1210 * LTK claim insufficient security. This way we allow the
1211 * connection to be re-encrypted with an LTK, even if the LTK
1212 * provides the same level of security. Only exception is if we
1213 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001214 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001215 if (key_pref == SMP_USE_LTK &&
1216 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001217 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001218 return false;
1219
Johan Hedberg854f4722014-07-01 18:40:20 +03001220 if (hcon->sec_level >= sec_level)
1221 return true;
1222
1223 return false;
1224}
1225
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001226static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001227{
1228 struct smp_cmd_security_req *rp = (void *) skb->data;
1229 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001230 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001231 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001232 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001233 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001234
1235 BT_DBG("conn %p", conn);
1236
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001237 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001238 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001239
Johan Hedberg40bef302014-07-16 11:42:27 +03001240 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001241 return SMP_CMD_NOTSUPP;
1242
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001243 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001244
Johan Hedberg5be5e272014-09-10 17:58:54 -07001245 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001246 sec_level = BT_SECURITY_MEDIUM;
1247 else
1248 sec_level = authreq_to_seclevel(auth);
1249
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001250 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03001251 return 0;
1252
Johan Hedbergc7262e72014-06-17 13:07:37 +03001253 if (sec_level > hcon->pending_sec_level)
1254 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001255
Johan Hedberg4dab7862012-06-07 14:58:37 +08001256 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001257 return 0;
1258
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001259 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001260 if (!smp)
1261 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001262
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001263 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001264 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001265 return SMP_PAIRING_NOTSUPP;
1266
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001267 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001268
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001269 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001270 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001271
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001272 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1273 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001274
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001275 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001276 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001277
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001278 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001279}
1280
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001281int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001282{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001283 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001284 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001285 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001286 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001287 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001288
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001289 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1290
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001291 /* This may be NULL if there's an unexpected disconnection */
1292 if (!conn)
1293 return 1;
1294
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001295 chan = conn->smp;
1296
Johan Hedberg757aee02013-04-24 13:05:32 +03001297 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001298 return 1;
1299
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001300 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001301 return 1;
1302
Johan Hedbergc7262e72014-06-17 13:07:37 +03001303 if (sec_level > hcon->pending_sec_level)
1304 hcon->pending_sec_level = sec_level;
1305
Johan Hedberg40bef302014-07-16 11:42:27 +03001306 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001307 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1308 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001309
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001310 l2cap_chan_lock(chan);
1311
1312 /* If SMP is already in progress ignore this request */
1313 if (chan->data) {
1314 ret = 0;
1315 goto unlock;
1316 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001317
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001318 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001319 if (!smp) {
1320 ret = 1;
1321 goto unlock;
1322 }
Brian Gix2b64d152011-12-21 16:12:12 -08001323
1324 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001325
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001326 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1327 authreq |= SMP_AUTH_SC;
1328
Johan Hedberg79897d22014-06-01 09:45:24 +03001329 /* Require MITM if IO Capability allows or the security level
1330 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001331 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001332 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001333 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001334 authreq |= SMP_AUTH_MITM;
1335
Johan Hedberg40bef302014-07-16 11:42:27 +03001336 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001337 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001338
Brian Gix2b64d152011-12-21 16:12:12 -08001339 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001340 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1341 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001342
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001343 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001344 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001345 } else {
1346 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001347 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001348 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001349 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001350 }
1351
Johan Hedberg4a74d652014-05-20 09:45:50 +03001352 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001353 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001354
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001355unlock:
1356 l2cap_chan_unlock(chan);
1357 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001358}
1359
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001360static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1361{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001362 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001363 struct l2cap_chan *chan = conn->smp;
1364 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001365
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001366 BT_DBG("conn %p", conn);
1367
1368 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001369 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001370
Johan Hedbergb28b4942014-09-05 22:19:55 +03001371 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001372
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001373 skb_pull(skb, sizeof(*rp));
1374
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001375 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001376
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001377 return 0;
1378}
1379
1380static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1381{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001382 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001383 struct l2cap_chan *chan = conn->smp;
1384 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001385 struct hci_dev *hdev = conn->hcon->hdev;
1386 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001387 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001388 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001389
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001390 BT_DBG("conn %p", conn);
1391
1392 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001393 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001394
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001395 /* Mark the information as received */
1396 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1397
Johan Hedbergb28b4942014-09-05 22:19:55 +03001398 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1399 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001400 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1401 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001402
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001403 skb_pull(skb, sizeof(*rp));
1404
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001405 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001406 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001407 authenticated, smp->tk, smp->enc_key_size,
1408 rp->ediv, rp->rand);
1409 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001410 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001411 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001412
1413 return 0;
1414}
1415
Johan Hedbergfd349c02014-02-18 10:19:36 +02001416static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1417{
1418 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001419 struct l2cap_chan *chan = conn->smp;
1420 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001421
1422 BT_DBG("");
1423
1424 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001425 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001426
Johan Hedbergb28b4942014-09-05 22:19:55 +03001427 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001428
Johan Hedbergfd349c02014-02-18 10:19:36 +02001429 skb_pull(skb, sizeof(*info));
1430
1431 memcpy(smp->irk, info->irk, 16);
1432
1433 return 0;
1434}
1435
1436static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1437 struct sk_buff *skb)
1438{
1439 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001440 struct l2cap_chan *chan = conn->smp;
1441 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001442 struct hci_conn *hcon = conn->hcon;
1443 bdaddr_t rpa;
1444
1445 BT_DBG("");
1446
1447 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001448 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001449
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001450 /* Mark the information as received */
1451 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1452
Johan Hedbergb28b4942014-09-05 22:19:55 +03001453 if (smp->remote_key_dist & SMP_DIST_SIGN)
1454 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1455
Johan Hedbergfd349c02014-02-18 10:19:36 +02001456 skb_pull(skb, sizeof(*info));
1457
Johan Hedberga9a58f82014-02-25 22:24:37 +02001458 /* Strictly speaking the Core Specification (4.1) allows sending
1459 * an empty address which would force us to rely on just the IRK
1460 * as "identity information". However, since such
1461 * implementations are not known of and in order to not over
1462 * complicate our implementation, simply pretend that we never
1463 * received an IRK for such a device.
1464 */
1465 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1466 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001467 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001468 }
1469
Johan Hedbergfd349c02014-02-18 10:19:36 +02001470 bacpy(&smp->id_addr, &info->bdaddr);
1471 smp->id_addr_type = info->addr_type;
1472
1473 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1474 bacpy(&rpa, &hcon->dst);
1475 else
1476 bacpy(&rpa, BDADDR_ANY);
1477
Johan Hedberg23d0e122014-02-19 14:57:46 +02001478 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1479 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001480
Johan Hedberg31dd6242014-06-27 14:23:02 +03001481distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001482 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1483 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001484
1485 return 0;
1486}
1487
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001488static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1489{
1490 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001491 struct l2cap_chan *chan = conn->smp;
1492 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001493 struct smp_csrk *csrk;
1494
1495 BT_DBG("conn %p", conn);
1496
1497 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001498 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001499
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001500 /* Mark the information as received */
1501 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1502
1503 skb_pull(skb, sizeof(*rp));
1504
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001505 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1506 if (csrk) {
1507 csrk->master = 0x01;
1508 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1509 }
1510 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001511 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001512
1513 return 0;
1514}
1515
Johan Hedberg4befb862014-08-11 22:06:38 +03001516static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001517{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001518 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001519 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001520 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001521 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001522 int err = 0;
1523
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001524 if (hcon->type != LE_LINK) {
1525 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001526 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001527 }
1528
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001529 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001530 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001531
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001532 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001533 reason = SMP_PAIRING_NOTSUPP;
1534 goto done;
1535 }
1536
Marcel Holtmann92381f52013-10-03 01:23:08 -07001537 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001538 skb_pull(skb, sizeof(code));
1539
Johan Hedbergb28b4942014-09-05 22:19:55 +03001540 smp = chan->data;
1541
1542 if (code > SMP_CMD_MAX)
1543 goto drop;
1544
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07001545 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03001546 goto drop;
1547
1548 /* If we don't have a context the only allowed commands are
1549 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001550 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001551 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1552 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001553
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001554 switch (code) {
1555 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001556 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001557 break;
1558
1559 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001560 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001561 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001562 break;
1563
1564 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001565 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001566 break;
1567
1568 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001569 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001570 break;
1571
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001572 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001573 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001574 break;
1575
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001576 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001577 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001578 break;
1579
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001580 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001581 reason = smp_cmd_encrypt_info(conn, skb);
1582 break;
1583
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001584 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001585 reason = smp_cmd_master_ident(conn, skb);
1586 break;
1587
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001588 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001589 reason = smp_cmd_ident_info(conn, skb);
1590 break;
1591
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001592 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001593 reason = smp_cmd_ident_addr_info(conn, skb);
1594 break;
1595
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001596 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001597 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001598 break;
1599
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001600 default:
1601 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001602 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001603 goto done;
1604 }
1605
1606done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001607 if (!err) {
1608 if (reason)
1609 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001610 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001611 }
1612
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001613 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001614
1615drop:
1616 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1617 code, &hcon->dst);
1618 kfree_skb(skb);
1619 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001620}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001621
Johan Hedberg70db83c2014-08-08 09:37:16 +03001622static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1623{
1624 struct l2cap_conn *conn = chan->conn;
1625
1626 BT_DBG("chan %p", chan);
1627
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001628 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001629 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001630
Johan Hedberg70db83c2014-08-08 09:37:16 +03001631 conn->smp = NULL;
1632 l2cap_chan_put(chan);
1633}
1634
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001635static void smp_resume_cb(struct l2cap_chan *chan)
1636{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001637 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001638 struct l2cap_conn *conn = chan->conn;
1639 struct hci_conn *hcon = conn->hcon;
1640
1641 BT_DBG("chan %p", chan);
1642
Johan Hedberg86d14072014-08-11 22:06:43 +03001643 if (!smp)
1644 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001645
Johan Hedberg84bc0db2014-09-05 22:19:49 +03001646 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1647 return;
1648
Johan Hedberg86d14072014-08-11 22:06:43 +03001649 cancel_delayed_work(&smp->security_timer);
1650
Johan Hedbergd6268e82014-09-05 22:19:51 +03001651 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001652}
1653
Johan Hedberg70db83c2014-08-08 09:37:16 +03001654static void smp_ready_cb(struct l2cap_chan *chan)
1655{
1656 struct l2cap_conn *conn = chan->conn;
1657
1658 BT_DBG("chan %p", chan);
1659
1660 conn->smp = chan;
1661 l2cap_chan_hold(chan);
1662}
1663
Johan Hedberg4befb862014-08-11 22:06:38 +03001664static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1665{
1666 int err;
1667
1668 BT_DBG("chan %p", chan);
1669
1670 err = smp_sig_channel(chan, skb);
1671 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001672 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001673
Johan Hedbergb68fda62014-08-11 22:06:40 +03001674 if (smp)
1675 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001676
Johan Hedberg1e91c292014-08-18 20:33:29 +03001677 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03001678 }
1679
1680 return err;
1681}
1682
Johan Hedberg70db83c2014-08-08 09:37:16 +03001683static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1684 unsigned long hdr_len,
1685 unsigned long len, int nb)
1686{
1687 struct sk_buff *skb;
1688
1689 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1690 if (!skb)
1691 return ERR_PTR(-ENOMEM);
1692
1693 skb->priority = HCI_PRIO_MAX;
1694 bt_cb(skb)->chan = chan;
1695
1696 return skb;
1697}
1698
1699static const struct l2cap_ops smp_chan_ops = {
1700 .name = "Security Manager",
1701 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001702 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001703 .alloc_skb = smp_alloc_skb_cb,
1704 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001705 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001706
1707 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001708 .state_change = l2cap_chan_no_state_change,
1709 .close = l2cap_chan_no_close,
1710 .defer = l2cap_chan_no_defer,
1711 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001712 .set_shutdown = l2cap_chan_no_set_shutdown,
1713 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1714 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1715};
1716
1717static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1718{
1719 struct l2cap_chan *chan;
1720
1721 BT_DBG("pchan %p", pchan);
1722
1723 chan = l2cap_chan_create();
1724 if (!chan)
1725 return NULL;
1726
1727 chan->chan_type = pchan->chan_type;
1728 chan->ops = &smp_chan_ops;
1729 chan->scid = pchan->scid;
1730 chan->dcid = chan->scid;
1731 chan->imtu = pchan->imtu;
1732 chan->omtu = pchan->omtu;
1733 chan->mode = pchan->mode;
1734
Johan Hedbergabe84902014-11-12 22:22:21 +02001735 /* Other L2CAP channels may request SMP routines in order to
1736 * change the security level. This means that the SMP channel
1737 * lock must be considered in its own category to avoid lockdep
1738 * warnings.
1739 */
1740 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
1741
Johan Hedberg70db83c2014-08-08 09:37:16 +03001742 BT_DBG("created chan %p", chan);
1743
1744 return chan;
1745}
1746
1747static const struct l2cap_ops smp_root_chan_ops = {
1748 .name = "Security Manager Root",
1749 .new_connection = smp_new_conn_cb,
1750
1751 /* None of these are implemented for the root channel */
1752 .close = l2cap_chan_no_close,
1753 .alloc_skb = l2cap_chan_no_alloc_skb,
1754 .recv = l2cap_chan_no_recv,
1755 .state_change = l2cap_chan_no_state_change,
1756 .teardown = l2cap_chan_no_teardown,
1757 .ready = l2cap_chan_no_ready,
1758 .defer = l2cap_chan_no_defer,
1759 .suspend = l2cap_chan_no_suspend,
1760 .resume = l2cap_chan_no_resume,
1761 .set_shutdown = l2cap_chan_no_set_shutdown,
1762 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1763 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1764};
1765
Johan Hedberg711eafe2014-08-08 09:32:52 +03001766int smp_register(struct hci_dev *hdev)
1767{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001768 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001769 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001770
Johan Hedberg711eafe2014-08-08 09:32:52 +03001771 BT_DBG("%s", hdev->name);
1772
Johan Hedbergadae20c2014-11-13 14:37:48 +02001773 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001774 if (IS_ERR(tfm_aes)) {
1775 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001776 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001777 return err;
1778 }
1779
Johan Hedberg70db83c2014-08-08 09:37:16 +03001780 chan = l2cap_chan_create();
1781 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001782 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001783 return -ENOMEM;
1784 }
1785
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001786 chan->data = tfm_aes;
1787
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001788 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001789
1790 l2cap_chan_set_defaults(chan);
1791
1792 bacpy(&chan->src, &hdev->bdaddr);
1793 chan->src_type = BDADDR_LE_PUBLIC;
1794 chan->state = BT_LISTEN;
1795 chan->mode = L2CAP_MODE_BASIC;
1796 chan->imtu = L2CAP_DEFAULT_MTU;
1797 chan->ops = &smp_root_chan_ops;
1798
Johan Hedbergabe84902014-11-12 22:22:21 +02001799 /* Set correct nesting level for a parent/listening channel */
1800 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
1801
Johan Hedberg70db83c2014-08-08 09:37:16 +03001802 hdev->smp_data = chan;
1803
Johan Hedberg711eafe2014-08-08 09:32:52 +03001804 return 0;
1805}
1806
1807void smp_unregister(struct hci_dev *hdev)
1808{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001809 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001810 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001811
1812 if (!chan)
1813 return;
1814
1815 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001816
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001817 tfm_aes = chan->data;
1818 if (tfm_aes) {
1819 chan->data = NULL;
1820 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001821 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001822
1823 hdev->smp_data = NULL;
1824 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001825}