Commit | Line | Data |
---|---|---|
eb492e01 AB |
1 | /* |
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 | ||
8c520a59 GP |
23 | #include <linux/crypto.h> |
24 | #include <linux/scatterlist.h> | |
25 | #include <crypto/b128ops.h> | |
26 | ||
eb492e01 AB |
27 | #include <net/bluetooth/bluetooth.h> |
28 | #include <net/bluetooth/hci_core.h> | |
29 | #include <net/bluetooth/l2cap.h> | |
2b64d153 | 30 | #include <net/bluetooth/mgmt.h> |
ac4b7236 MH |
31 | |
32 | #include "smp.h" | |
d22ef0bc | 33 | |
b28b4943 JH |
34 | #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd) |
35 | #define SMP_DISALLOW_CMD(smp, code) clear_bit(code, &smp->allow_cmd) | |
36 | ||
17b02e62 | 37 | #define SMP_TIMEOUT msecs_to_jiffies(30000) |
5d3de7df | 38 | |
065a13e2 | 39 | #define AUTH_REQ_MASK 0x07 |
88d3a8ac | 40 | #define KEY_DIST_MASK 0x07 |
065a13e2 | 41 | |
533e35d4 JH |
42 | enum { |
43 | SMP_FLAG_TK_VALID, | |
44 | SMP_FLAG_CFM_PENDING, | |
45 | SMP_FLAG_MITM_AUTH, | |
46 | SMP_FLAG_COMPLETE, | |
47 | SMP_FLAG_INITIATOR, | |
48 | }; | |
4bc58f51 JH |
49 | |
50 | struct smp_chan { | |
b68fda68 JH |
51 | struct l2cap_conn *conn; |
52 | struct delayed_work security_timer; | |
b28b4943 | 53 | unsigned long allow_cmd; /* Bitmask of allowed commands */ |
b68fda68 | 54 | |
4bc58f51 JH |
55 | u8 preq[7]; /* SMP Pairing Request */ |
56 | u8 prsp[7]; /* SMP Pairing Response */ | |
57 | u8 prnd[16]; /* SMP Pairing Random (local) */ | |
58 | u8 rrnd[16]; /* SMP Pairing Random (remote) */ | |
59 | u8 pcnf[16]; /* SMP Pairing Confirm */ | |
60 | u8 tk[16]; /* SMP Temporary Key */ | |
61 | u8 enc_key_size; | |
62 | u8 remote_key_dist; | |
63 | bdaddr_t id_addr; | |
64 | u8 id_addr_type; | |
65 | u8 irk[16]; | |
66 | struct smp_csrk *csrk; | |
67 | struct smp_csrk *slave_csrk; | |
68 | struct smp_ltk *ltk; | |
69 | struct smp_ltk *slave_ltk; | |
70 | struct smp_irk *remote_irk; | |
4a74d658 | 71 | unsigned long flags; |
6a7bd103 JH |
72 | |
73 | struct crypto_blkcipher *tfm_aes; | |
4bc58f51 JH |
74 | }; |
75 | ||
8a2936f4 | 76 | static inline void swap_buf(const u8 *src, u8 *dst, size_t len) |
d22ef0bc | 77 | { |
8a2936f4 | 78 | size_t i; |
d22ef0bc | 79 | |
8a2936f4 JH |
80 | for (i = 0; i < len; i++) |
81 | dst[len - 1 - i] = src[i]; | |
d22ef0bc AB |
82 | } |
83 | ||
84 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) | |
85 | { | |
86 | struct blkcipher_desc desc; | |
87 | struct scatterlist sg; | |
943a732a | 88 | uint8_t tmp[16], data[16]; |
201a5929 | 89 | int err; |
d22ef0bc AB |
90 | |
91 | if (tfm == NULL) { | |
92 | BT_ERR("tfm %p", tfm); | |
93 | return -EINVAL; | |
94 | } | |
95 | ||
96 | desc.tfm = tfm; | |
97 | desc.flags = 0; | |
98 | ||
943a732a | 99 | /* The most significant octet of key corresponds to k[0] */ |
8a2936f4 | 100 | swap_buf(k, tmp, 16); |
943a732a JH |
101 | |
102 | err = crypto_blkcipher_setkey(tfm, tmp, 16); | |
d22ef0bc AB |
103 | if (err) { |
104 | BT_ERR("cipher setkey failed: %d", err); | |
105 | return err; | |
106 | } | |
107 | ||
943a732a | 108 | /* Most significant octet of plaintextData corresponds to data[0] */ |
8a2936f4 | 109 | swap_buf(r, data, 16); |
943a732a JH |
110 | |
111 | sg_init_one(&sg, data, 16); | |
d22ef0bc | 112 | |
d22ef0bc AB |
113 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); |
114 | if (err) | |
115 | BT_ERR("Encrypt data error %d", err); | |
116 | ||
943a732a | 117 | /* Most significant octet of encryptedData corresponds to data[0] */ |
8a2936f4 | 118 | swap_buf(data, r, 16); |
943a732a | 119 | |
d22ef0bc AB |
120 | return err; |
121 | } | |
122 | ||
60478054 JH |
123 | static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3]) |
124 | { | |
943a732a | 125 | u8 _res[16]; |
60478054 JH |
126 | int err; |
127 | ||
128 | /* r' = padding || r */ | |
943a732a JH |
129 | memcpy(_res, r, 3); |
130 | memset(_res + 3, 0, 13); | |
60478054 | 131 | |
943a732a | 132 | err = smp_e(tfm, irk, _res); |
60478054 JH |
133 | if (err) { |
134 | BT_ERR("Encrypt error"); | |
135 | return err; | |
136 | } | |
137 | ||
138 | /* The output of the random address function ah is: | |
139 | * ah(h, r) = e(k, r') mod 2^24 | |
140 | * The output of the security function e is then truncated to 24 bits | |
141 | * by taking the least significant 24 bits of the output of e as the | |
142 | * result of ah. | |
143 | */ | |
943a732a | 144 | memcpy(res, _res, 3); |
60478054 JH |
145 | |
146 | return 0; | |
147 | } | |
148 | ||
defce9e8 | 149 | bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr) |
60478054 | 150 | { |
defce9e8 JH |
151 | struct l2cap_chan *chan = hdev->smp_data; |
152 | struct crypto_blkcipher *tfm; | |
60478054 JH |
153 | u8 hash[3]; |
154 | int err; | |
155 | ||
defce9e8 JH |
156 | if (!chan || !chan->data) |
157 | return false; | |
158 | ||
159 | tfm = chan->data; | |
160 | ||
60478054 JH |
161 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); |
162 | ||
163 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); | |
164 | if (err) | |
165 | return false; | |
166 | ||
167 | return !memcmp(bdaddr->b, hash, 3); | |
168 | } | |
169 | ||
defce9e8 | 170 | int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa) |
b1e2b3ae | 171 | { |
defce9e8 JH |
172 | struct l2cap_chan *chan = hdev->smp_data; |
173 | struct crypto_blkcipher *tfm; | |
b1e2b3ae JH |
174 | int err; |
175 | ||
defce9e8 JH |
176 | if (!chan || !chan->data) |
177 | return -EOPNOTSUPP; | |
178 | ||
179 | tfm = chan->data; | |
180 | ||
b1e2b3ae JH |
181 | get_random_bytes(&rpa->b[3], 3); |
182 | ||
183 | rpa->b[5] &= 0x3f; /* Clear two most significant bits */ | |
184 | rpa->b[5] |= 0x40; /* Set second most significant bit */ | |
185 | ||
186 | err = smp_ah(tfm, irk, &rpa->b[3], rpa->b); | |
187 | if (err < 0) | |
188 | return err; | |
189 | ||
190 | BT_DBG("RPA %pMR", rpa); | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
ec70f36f JH |
195 | static int smp_c1(struct smp_chan *smp, u8 k[16], u8 r[16], u8 preq[7], |
196 | u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, bdaddr_t *ra, | |
197 | u8 res[16]) | |
d22ef0bc | 198 | { |
ec70f36f | 199 | struct hci_dev *hdev = smp->conn->hcon->hdev; |
d22ef0bc AB |
200 | u8 p1[16], p2[16]; |
201 | int err; | |
202 | ||
ec70f36f JH |
203 | BT_DBG("%s", hdev->name); |
204 | ||
d22ef0bc AB |
205 | memset(p1, 0, 16); |
206 | ||
207 | /* p1 = pres || preq || _rat || _iat */ | |
943a732a JH |
208 | p1[0] = _iat; |
209 | p1[1] = _rat; | |
210 | memcpy(p1 + 2, preq, 7); | |
211 | memcpy(p1 + 9, pres, 7); | |
d22ef0bc AB |
212 | |
213 | /* p2 = padding || ia || ra */ | |
943a732a JH |
214 | memcpy(p2, ra, 6); |
215 | memcpy(p2 + 6, ia, 6); | |
216 | memset(p2 + 12, 0, 4); | |
d22ef0bc AB |
217 | |
218 | /* res = r XOR p1 */ | |
219 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); | |
220 | ||
221 | /* res = e(k, res) */ | |
ec70f36f | 222 | err = smp_e(smp->tfm_aes, k, res); |
d22ef0bc AB |
223 | if (err) { |
224 | BT_ERR("Encrypt data error"); | |
225 | return err; | |
226 | } | |
227 | ||
228 | /* res = res XOR p2 */ | |
229 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); | |
230 | ||
231 | /* res = e(k, res) */ | |
ec70f36f | 232 | err = smp_e(smp->tfm_aes, k, res); |
d22ef0bc AB |
233 | if (err) |
234 | BT_ERR("Encrypt data error"); | |
235 | ||
236 | return err; | |
237 | } | |
238 | ||
ec70f36f JH |
239 | static int smp_s1(struct smp_chan *smp, u8 k[16], u8 r1[16], u8 r2[16], |
240 | u8 _r[16]) | |
d22ef0bc | 241 | { |
ec70f36f | 242 | struct hci_dev *hdev = smp->conn->hcon->hdev; |
d22ef0bc AB |
243 | int err; |
244 | ||
ec70f36f JH |
245 | BT_DBG("%s", hdev->name); |
246 | ||
d22ef0bc | 247 | /* Just least significant octets from r1 and r2 are considered */ |
943a732a JH |
248 | memcpy(_r, r2, 8); |
249 | memcpy(_r + 8, r1, 8); | |
d22ef0bc | 250 | |
ec70f36f | 251 | err = smp_e(smp->tfm_aes, k, _r); |
d22ef0bc AB |
252 | if (err) |
253 | BT_ERR("Encrypt data error"); | |
254 | ||
255 | return err; | |
256 | } | |
257 | ||
5d88cc73 | 258 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) |
eb492e01 | 259 | { |
5d88cc73 | 260 | struct l2cap_chan *chan = conn->smp; |
b68fda68 | 261 | struct smp_chan *smp; |
5d88cc73 JH |
262 | struct kvec iv[2]; |
263 | struct msghdr msg; | |
eb492e01 | 264 | |
5d88cc73 JH |
265 | if (!chan) |
266 | return; | |
eb492e01 | 267 | |
5d88cc73 | 268 | BT_DBG("code 0x%2.2x", code); |
eb492e01 | 269 | |
5d88cc73 JH |
270 | iv[0].iov_base = &code; |
271 | iv[0].iov_len = 1; | |
eb492e01 | 272 | |
5d88cc73 JH |
273 | iv[1].iov_base = data; |
274 | iv[1].iov_len = len; | |
eb492e01 | 275 | |
5d88cc73 | 276 | memset(&msg, 0, sizeof(msg)); |
eb492e01 | 277 | |
5d88cc73 JH |
278 | msg.msg_iov = (struct iovec *) &iv; |
279 | msg.msg_iovlen = 2; | |
eb492e01 | 280 | |
5d88cc73 | 281 | l2cap_chan_send(chan, &msg, 1 + len); |
e2dcd113 | 282 | |
b68fda68 JH |
283 | if (!chan->data) |
284 | return; | |
285 | ||
286 | smp = chan->data; | |
287 | ||
288 | cancel_delayed_work_sync(&smp->security_timer); | |
1b0921d6 | 289 | schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT); |
eb492e01 AB |
290 | } |
291 | ||
2b64d153 BG |
292 | static __u8 authreq_to_seclevel(__u8 authreq) |
293 | { | |
294 | if (authreq & SMP_AUTH_MITM) | |
295 | return BT_SECURITY_HIGH; | |
296 | else | |
297 | return BT_SECURITY_MEDIUM; | |
298 | } | |
299 | ||
300 | static __u8 seclevel_to_authreq(__u8 sec_level) | |
301 | { | |
302 | switch (sec_level) { | |
303 | case BT_SECURITY_HIGH: | |
304 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; | |
305 | case BT_SECURITY_MEDIUM: | |
306 | return SMP_AUTH_BONDING; | |
307 | default: | |
308 | return SMP_AUTH_NONE; | |
309 | } | |
310 | } | |
311 | ||
b8e66eac | 312 | static void build_pairing_cmd(struct l2cap_conn *conn, |
f1560463 MH |
313 | struct smp_cmd_pairing *req, |
314 | struct smp_cmd_pairing *rsp, __u8 authreq) | |
b8e66eac | 315 | { |
5d88cc73 JH |
316 | struct l2cap_chan *chan = conn->smp; |
317 | struct smp_chan *smp = chan->data; | |
fd349c02 JH |
318 | struct hci_conn *hcon = conn->hcon; |
319 | struct hci_dev *hdev = hcon->hdev; | |
320 | u8 local_dist = 0, remote_dist = 0; | |
54790f73 | 321 | |
b6ae8457 | 322 | if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) { |
7ee4ea36 MH |
323 | local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
324 | remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; | |
54790f73 | 325 | authreq |= SMP_AUTH_BONDING; |
2b64d153 BG |
326 | } else { |
327 | authreq &= ~SMP_AUTH_BONDING; | |
54790f73 VCG |
328 | } |
329 | ||
fd349c02 JH |
330 | if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) |
331 | remote_dist |= SMP_DIST_ID_KEY; | |
332 | ||
863efaf2 JH |
333 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
334 | local_dist |= SMP_DIST_ID_KEY; | |
335 | ||
54790f73 VCG |
336 | if (rsp == NULL) { |
337 | req->io_capability = conn->hcon->io_capability; | |
338 | req->oob_flag = SMP_OOB_NOT_PRESENT; | |
339 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
fd349c02 JH |
340 | req->init_key_dist = local_dist; |
341 | req->resp_key_dist = remote_dist; | |
065a13e2 | 342 | req->auth_req = (authreq & AUTH_REQ_MASK); |
fd349c02 JH |
343 | |
344 | smp->remote_key_dist = remote_dist; | |
54790f73 VCG |
345 | return; |
346 | } | |
347 | ||
348 | rsp->io_capability = conn->hcon->io_capability; | |
349 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; | |
350 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
fd349c02 JH |
351 | rsp->init_key_dist = req->init_key_dist & remote_dist; |
352 | rsp->resp_key_dist = req->resp_key_dist & local_dist; | |
065a13e2 | 353 | rsp->auth_req = (authreq & AUTH_REQ_MASK); |
fd349c02 JH |
354 | |
355 | smp->remote_key_dist = rsp->init_key_dist; | |
b8e66eac VCG |
356 | } |
357 | ||
3158c50c VCG |
358 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
359 | { | |
5d88cc73 JH |
360 | struct l2cap_chan *chan = conn->smp; |
361 | struct smp_chan *smp = chan->data; | |
1c1def09 | 362 | |
3158c50c | 363 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
f1560463 | 364 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
3158c50c VCG |
365 | return SMP_ENC_KEY_SIZE; |
366 | ||
f7aa611a | 367 | smp->enc_key_size = max_key_size; |
3158c50c VCG |
368 | |
369 | return 0; | |
370 | } | |
371 | ||
6f48e260 JH |
372 | static void smp_chan_destroy(struct l2cap_conn *conn) |
373 | { | |
374 | struct l2cap_chan *chan = conn->smp; | |
375 | struct smp_chan *smp = chan->data; | |
376 | bool complete; | |
377 | ||
378 | BUG_ON(!smp); | |
379 | ||
380 | cancel_delayed_work_sync(&smp->security_timer); | |
6f48e260 | 381 | |
6f48e260 JH |
382 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); |
383 | mgmt_smp_complete(conn->hcon, complete); | |
384 | ||
385 | kfree(smp->csrk); | |
386 | kfree(smp->slave_csrk); | |
387 | ||
388 | crypto_free_blkcipher(smp->tfm_aes); | |
389 | ||
390 | /* If pairing failed clean up any keys we might have */ | |
391 | if (!complete) { | |
392 | if (smp->ltk) { | |
393 | list_del(&smp->ltk->list); | |
394 | kfree(smp->ltk); | |
395 | } | |
396 | ||
397 | if (smp->slave_ltk) { | |
398 | list_del(&smp->slave_ltk->list); | |
399 | kfree(smp->slave_ltk); | |
400 | } | |
401 | ||
402 | if (smp->remote_irk) { | |
403 | list_del(&smp->remote_irk->list); | |
404 | kfree(smp->remote_irk); | |
405 | } | |
406 | } | |
407 | ||
408 | chan->data = NULL; | |
409 | kfree(smp); | |
410 | hci_conn_drop(conn->hcon); | |
411 | } | |
412 | ||
84794e11 | 413 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
4f957a76 | 414 | { |
bab73cb6 | 415 | struct hci_conn *hcon = conn->hcon; |
b68fda68 | 416 | struct l2cap_chan *chan = conn->smp; |
bab73cb6 | 417 | |
84794e11 | 418 | if (reason) |
4f957a76 | 419 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
f1560463 | 420 | &reason); |
4f957a76 | 421 | |
ce39fb4e MH |
422 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
423 | mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type, | |
424 | HCI_ERROR_AUTH_FAILURE); | |
f1c09c07 | 425 | |
fc75cc86 | 426 | if (chan->data) |
f1c09c07 | 427 | smp_chan_destroy(conn); |
4f957a76 BG |
428 | } |
429 | ||
2b64d153 BG |
430 | #define JUST_WORKS 0x00 |
431 | #define JUST_CFM 0x01 | |
432 | #define REQ_PASSKEY 0x02 | |
433 | #define CFM_PASSKEY 0x03 | |
434 | #define REQ_OOB 0x04 | |
435 | #define OVERLAP 0xFF | |
436 | ||
437 | static const u8 gen_method[5][5] = { | |
438 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
439 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
440 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, | |
441 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, | |
442 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, | |
443 | }; | |
444 | ||
581370cc JH |
445 | static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io) |
446 | { | |
2bcd4003 JH |
447 | /* If either side has unknown io_caps, use JUST_CFM (which gets |
448 | * converted later to JUST_WORKS if we're initiators. | |
449 | */ | |
581370cc JH |
450 | if (local_io > SMP_IO_KEYBOARD_DISPLAY || |
451 | remote_io > SMP_IO_KEYBOARD_DISPLAY) | |
2bcd4003 | 452 | return JUST_CFM; |
581370cc JH |
453 | |
454 | return gen_method[remote_io][local_io]; | |
455 | } | |
456 | ||
2b64d153 BG |
457 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, |
458 | u8 local_io, u8 remote_io) | |
459 | { | |
460 | struct hci_conn *hcon = conn->hcon; | |
5d88cc73 JH |
461 | struct l2cap_chan *chan = conn->smp; |
462 | struct smp_chan *smp = chan->data; | |
2b64d153 BG |
463 | u8 method; |
464 | u32 passkey = 0; | |
465 | int ret = 0; | |
466 | ||
467 | /* Initialize key for JUST WORKS */ | |
468 | memset(smp->tk, 0, sizeof(smp->tk)); | |
4a74d658 | 469 | clear_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
470 | |
471 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); | |
472 | ||
2bcd4003 JH |
473 | /* If neither side wants MITM, either "just" confirm an incoming |
474 | * request or use just-works for outgoing ones. The JUST_CFM | |
475 | * will be converted to JUST_WORKS if necessary later in this | |
476 | * function. If either side has MITM look up the method from the | |
477 | * table. | |
478 | */ | |
581370cc | 479 | if (!(auth & SMP_AUTH_MITM)) |
2bcd4003 | 480 | method = JUST_CFM; |
2b64d153 | 481 | else |
581370cc | 482 | method = get_auth_method(smp, local_io, remote_io); |
2b64d153 | 483 | |
a82505c7 | 484 | /* Don't confirm locally initiated pairing attempts */ |
4a74d658 | 485 | if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) |
a82505c7 JH |
486 | method = JUST_WORKS; |
487 | ||
02f3e254 JH |
488 | /* Don't bother user space with no IO capabilities */ |
489 | if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) | |
490 | method = JUST_WORKS; | |
491 | ||
2b64d153 BG |
492 | /* If Just Works, Continue with Zero TK */ |
493 | if (method == JUST_WORKS) { | |
4a74d658 | 494 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
495 | return 0; |
496 | } | |
497 | ||
498 | /* Not Just Works/Confirm results in MITM Authentication */ | |
499 | if (method != JUST_CFM) | |
4a74d658 | 500 | set_bit(SMP_FLAG_MITM_AUTH, &smp->flags); |
2b64d153 BG |
501 | |
502 | /* If both devices have Keyoard-Display I/O, the master | |
503 | * Confirms and the slave Enters the passkey. | |
504 | */ | |
505 | if (method == OVERLAP) { | |
40bef302 | 506 | if (hcon->role == HCI_ROLE_MASTER) |
2b64d153 BG |
507 | method = CFM_PASSKEY; |
508 | else | |
509 | method = REQ_PASSKEY; | |
510 | } | |
511 | ||
01ad34d2 | 512 | /* Generate random passkey. */ |
2b64d153 | 513 | if (method == CFM_PASSKEY) { |
943a732a | 514 | memset(smp->tk, 0, sizeof(smp->tk)); |
2b64d153 BG |
515 | get_random_bytes(&passkey, sizeof(passkey)); |
516 | passkey %= 1000000; | |
943a732a | 517 | put_unaligned_le32(passkey, smp->tk); |
2b64d153 | 518 | BT_DBG("PassKey: %d", passkey); |
4a74d658 | 519 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
520 | } |
521 | ||
522 | hci_dev_lock(hcon->hdev); | |
523 | ||
524 | if (method == REQ_PASSKEY) | |
ce39fb4e | 525 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
272d90df | 526 | hcon->type, hcon->dst_type); |
4eb65e66 JH |
527 | else if (method == JUST_CFM) |
528 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, | |
529 | hcon->type, hcon->dst_type, | |
530 | passkey, 1); | |
2b64d153 | 531 | else |
01ad34d2 | 532 | ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst, |
272d90df | 533 | hcon->type, hcon->dst_type, |
39adbffe | 534 | passkey, 0); |
2b64d153 BG |
535 | |
536 | hci_dev_unlock(hcon->hdev); | |
537 | ||
538 | return ret; | |
539 | } | |
540 | ||
1cc61144 | 541 | static u8 smp_confirm(struct smp_chan *smp) |
8aab4757 | 542 | { |
8aab4757 | 543 | struct l2cap_conn *conn = smp->conn; |
8aab4757 VCG |
544 | struct smp_cmd_pairing_confirm cp; |
545 | int ret; | |
8aab4757 VCG |
546 | |
547 | BT_DBG("conn %p", conn); | |
548 | ||
ec70f36f | 549 | ret = smp_c1(smp, smp->tk, smp->prnd, smp->preq, smp->prsp, |
b1cd5fd9 | 550 | conn->hcon->init_addr_type, &conn->hcon->init_addr, |
943a732a JH |
551 | conn->hcon->resp_addr_type, &conn->hcon->resp_addr, |
552 | cp.confirm_val); | |
1cc61144 JH |
553 | if (ret) |
554 | return SMP_UNSPECIFIED; | |
8aab4757 | 555 | |
4a74d658 | 556 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
2b64d153 | 557 | |
8aab4757 VCG |
558 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); |
559 | ||
b28b4943 JH |
560 | if (conn->hcon->out) |
561 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); | |
562 | else | |
563 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); | |
564 | ||
1cc61144 | 565 | return 0; |
8aab4757 VCG |
566 | } |
567 | ||
861580a9 | 568 | static u8 smp_random(struct smp_chan *smp) |
8aab4757 | 569 | { |
8aab4757 VCG |
570 | struct l2cap_conn *conn = smp->conn; |
571 | struct hci_conn *hcon = conn->hcon; | |
861580a9 | 572 | u8 confirm[16]; |
8aab4757 VCG |
573 | int ret; |
574 | ||
ec70f36f | 575 | if (IS_ERR_OR_NULL(smp->tfm_aes)) |
861580a9 | 576 | return SMP_UNSPECIFIED; |
8aab4757 VCG |
577 | |
578 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); | |
579 | ||
ec70f36f | 580 | ret = smp_c1(smp, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
b1cd5fd9 | 581 | hcon->init_addr_type, &hcon->init_addr, |
943a732a | 582 | hcon->resp_addr_type, &hcon->resp_addr, confirm); |
861580a9 JH |
583 | if (ret) |
584 | return SMP_UNSPECIFIED; | |
8aab4757 | 585 | |
8aab4757 VCG |
586 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { |
587 | BT_ERR("Pairing failed (confirmation values mismatch)"); | |
861580a9 | 588 | return SMP_CONFIRM_FAILED; |
8aab4757 VCG |
589 | } |
590 | ||
591 | if (hcon->out) { | |
fe39c7b2 MH |
592 | u8 stk[16]; |
593 | __le64 rand = 0; | |
594 | __le16 ediv = 0; | |
8aab4757 | 595 | |
ec70f36f | 596 | smp_s1(smp, smp->tk, smp->rrnd, smp->prnd, stk); |
8aab4757 | 597 | |
f7aa611a | 598 | memset(stk + smp->enc_key_size, 0, |
04124681 | 599 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 600 | |
861580a9 JH |
601 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
602 | return SMP_UNSPECIFIED; | |
8aab4757 VCG |
603 | |
604 | hci_le_start_enc(hcon, ediv, rand, stk); | |
f7aa611a | 605 | hcon->enc_key_size = smp->enc_key_size; |
fe59a05f | 606 | set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); |
8aab4757 | 607 | } else { |
fff3490f | 608 | u8 stk[16], auth; |
fe39c7b2 MH |
609 | __le64 rand = 0; |
610 | __le16 ediv = 0; | |
8aab4757 | 611 | |
943a732a JH |
612 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
613 | smp->prnd); | |
8aab4757 | 614 | |
ec70f36f | 615 | smp_s1(smp, smp->tk, smp->prnd, smp->rrnd, stk); |
8aab4757 | 616 | |
f7aa611a | 617 | memset(stk + smp->enc_key_size, 0, |
f1560463 | 618 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 619 | |
fff3490f JH |
620 | if (hcon->pending_sec_level == BT_SECURITY_HIGH) |
621 | auth = 1; | |
622 | else | |
623 | auth = 0; | |
624 | ||
7d5843b7 JH |
625 | /* Even though there's no _SLAVE suffix this is the |
626 | * slave STK we're adding for later lookup (the master | |
627 | * STK never needs to be stored). | |
628 | */ | |
ce39fb4e | 629 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
2ceba539 | 630 | SMP_STK, auth, stk, smp->enc_key_size, ediv, rand); |
8aab4757 VCG |
631 | } |
632 | ||
861580a9 | 633 | return 0; |
8aab4757 VCG |
634 | } |
635 | ||
44f1a7ab JH |
636 | static void smp_notify_keys(struct l2cap_conn *conn) |
637 | { | |
638 | struct l2cap_chan *chan = conn->smp; | |
639 | struct smp_chan *smp = chan->data; | |
640 | struct hci_conn *hcon = conn->hcon; | |
641 | struct hci_dev *hdev = hcon->hdev; | |
642 | struct smp_cmd_pairing *req = (void *) &smp->preq[1]; | |
643 | struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; | |
644 | bool persistent; | |
645 | ||
646 | if (smp->remote_irk) { | |
647 | mgmt_new_irk(hdev, smp->remote_irk); | |
648 | /* Now that user space can be considered to know the | |
649 | * identity address track the connection based on it | |
650 | * from now on. | |
651 | */ | |
652 | bacpy(&hcon->dst, &smp->remote_irk->bdaddr); | |
653 | hcon->dst_type = smp->remote_irk->addr_type; | |
f3d82d0c | 654 | queue_work(hdev->workqueue, &conn->id_addr_update_work); |
44f1a7ab JH |
655 | |
656 | /* When receiving an indentity resolving key for | |
657 | * a remote device that does not use a resolvable | |
658 | * private address, just remove the key so that | |
659 | * it is possible to use the controller white | |
660 | * list for scanning. | |
661 | * | |
662 | * Userspace will have been told to not store | |
663 | * this key at this point. So it is safe to | |
664 | * just remove it. | |
665 | */ | |
666 | if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) { | |
667 | list_del(&smp->remote_irk->list); | |
668 | kfree(smp->remote_irk); | |
669 | smp->remote_irk = NULL; | |
670 | } | |
671 | } | |
672 | ||
673 | /* The LTKs and CSRKs should be persistent only if both sides | |
674 | * had the bonding bit set in their authentication requests. | |
675 | */ | |
676 | persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING); | |
677 | ||
678 | if (smp->csrk) { | |
679 | smp->csrk->bdaddr_type = hcon->dst_type; | |
680 | bacpy(&smp->csrk->bdaddr, &hcon->dst); | |
681 | mgmt_new_csrk(hdev, smp->csrk, persistent); | |
682 | } | |
683 | ||
684 | if (smp->slave_csrk) { | |
685 | smp->slave_csrk->bdaddr_type = hcon->dst_type; | |
686 | bacpy(&smp->slave_csrk->bdaddr, &hcon->dst); | |
687 | mgmt_new_csrk(hdev, smp->slave_csrk, persistent); | |
688 | } | |
689 | ||
690 | if (smp->ltk) { | |
691 | smp->ltk->bdaddr_type = hcon->dst_type; | |
692 | bacpy(&smp->ltk->bdaddr, &hcon->dst); | |
693 | mgmt_new_ltk(hdev, smp->ltk, persistent); | |
694 | } | |
695 | ||
696 | if (smp->slave_ltk) { | |
697 | smp->slave_ltk->bdaddr_type = hcon->dst_type; | |
698 | bacpy(&smp->slave_ltk->bdaddr, &hcon->dst); | |
699 | mgmt_new_ltk(hdev, smp->slave_ltk, persistent); | |
700 | } | |
701 | } | |
702 | ||
b28b4943 JH |
703 | static void smp_allow_key_dist(struct smp_chan *smp) |
704 | { | |
705 | /* Allow the first expected phase 3 PDU. The rest of the PDUs | |
706 | * will be allowed in each PDU handler to ensure we receive | |
707 | * them in the correct order. | |
708 | */ | |
709 | if (smp->remote_key_dist & SMP_DIST_ENC_KEY) | |
710 | SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO); | |
711 | else if (smp->remote_key_dist & SMP_DIST_ID_KEY) | |
712 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); | |
713 | else if (smp->remote_key_dist & SMP_DIST_SIGN) | |
714 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); | |
715 | } | |
716 | ||
d6268e86 | 717 | static void smp_distribute_keys(struct smp_chan *smp) |
44f1a7ab JH |
718 | { |
719 | struct smp_cmd_pairing *req, *rsp; | |
86d1407c | 720 | struct l2cap_conn *conn = smp->conn; |
44f1a7ab JH |
721 | struct hci_conn *hcon = conn->hcon; |
722 | struct hci_dev *hdev = hcon->hdev; | |
723 | __u8 *keydist; | |
724 | ||
725 | BT_DBG("conn %p", conn); | |
726 | ||
44f1a7ab JH |
727 | rsp = (void *) &smp->prsp[1]; |
728 | ||
729 | /* The responder sends its keys first */ | |
b28b4943 JH |
730 | if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) { |
731 | smp_allow_key_dist(smp); | |
86d1407c | 732 | return; |
b28b4943 | 733 | } |
44f1a7ab JH |
734 | |
735 | req = (void *) &smp->preq[1]; | |
736 | ||
737 | if (hcon->out) { | |
738 | keydist = &rsp->init_key_dist; | |
739 | *keydist &= req->init_key_dist; | |
740 | } else { | |
741 | keydist = &rsp->resp_key_dist; | |
742 | *keydist &= req->resp_key_dist; | |
743 | } | |
744 | ||
745 | BT_DBG("keydist 0x%x", *keydist); | |
746 | ||
747 | if (*keydist & SMP_DIST_ENC_KEY) { | |
748 | struct smp_cmd_encrypt_info enc; | |
749 | struct smp_cmd_master_ident ident; | |
750 | struct smp_ltk *ltk; | |
751 | u8 authenticated; | |
752 | __le16 ediv; | |
753 | __le64 rand; | |
754 | ||
755 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); | |
756 | get_random_bytes(&ediv, sizeof(ediv)); | |
757 | get_random_bytes(&rand, sizeof(rand)); | |
758 | ||
759 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); | |
760 | ||
761 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; | |
762 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, | |
763 | SMP_LTK_SLAVE, authenticated, enc.ltk, | |
764 | smp->enc_key_size, ediv, rand); | |
765 | smp->slave_ltk = ltk; | |
766 | ||
767 | ident.ediv = ediv; | |
768 | ident.rand = rand; | |
769 | ||
770 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); | |
771 | ||
772 | *keydist &= ~SMP_DIST_ENC_KEY; | |
773 | } | |
774 | ||
775 | if (*keydist & SMP_DIST_ID_KEY) { | |
776 | struct smp_cmd_ident_addr_info addrinfo; | |
777 | struct smp_cmd_ident_info idinfo; | |
778 | ||
779 | memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); | |
780 | ||
781 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); | |
782 | ||
783 | /* The hci_conn contains the local identity address | |
784 | * after the connection has been established. | |
785 | * | |
786 | * This is true even when the connection has been | |
787 | * established using a resolvable random address. | |
788 | */ | |
789 | bacpy(&addrinfo.bdaddr, &hcon->src); | |
790 | addrinfo.addr_type = hcon->src_type; | |
791 | ||
792 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), | |
793 | &addrinfo); | |
794 | ||
795 | *keydist &= ~SMP_DIST_ID_KEY; | |
796 | } | |
797 | ||
798 | if (*keydist & SMP_DIST_SIGN) { | |
799 | struct smp_cmd_sign_info sign; | |
800 | struct smp_csrk *csrk; | |
801 | ||
802 | /* Generate a new random key */ | |
803 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); | |
804 | ||
805 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); | |
806 | if (csrk) { | |
807 | csrk->master = 0x00; | |
808 | memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); | |
809 | } | |
810 | smp->slave_csrk = csrk; | |
811 | ||
812 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); | |
813 | ||
814 | *keydist &= ~SMP_DIST_SIGN; | |
815 | } | |
816 | ||
817 | /* If there are still keys to be received wait for them */ | |
b28b4943 JH |
818 | if (smp->remote_key_dist & KEY_DIST_MASK) { |
819 | smp_allow_key_dist(smp); | |
86d1407c | 820 | return; |
b28b4943 | 821 | } |
44f1a7ab | 822 | |
44f1a7ab JH |
823 | set_bit(SMP_FLAG_COMPLETE, &smp->flags); |
824 | smp_notify_keys(conn); | |
825 | ||
826 | smp_chan_destroy(conn); | |
44f1a7ab JH |
827 | } |
828 | ||
b68fda68 JH |
829 | static void smp_timeout(struct work_struct *work) |
830 | { | |
831 | struct smp_chan *smp = container_of(work, struct smp_chan, | |
832 | security_timer.work); | |
833 | struct l2cap_conn *conn = smp->conn; | |
834 | ||
835 | BT_DBG("conn %p", conn); | |
836 | ||
1e91c29e | 837 | hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM); |
b68fda68 JH |
838 | } |
839 | ||
8aab4757 VCG |
840 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) |
841 | { | |
5d88cc73 | 842 | struct l2cap_chan *chan = conn->smp; |
8aab4757 VCG |
843 | struct smp_chan *smp; |
844 | ||
f1560463 | 845 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
fc75cc86 | 846 | if (!smp) |
8aab4757 VCG |
847 | return NULL; |
848 | ||
6a7bd103 JH |
849 | smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); |
850 | if (IS_ERR(smp->tfm_aes)) { | |
851 | BT_ERR("Unable to create ECB crypto context"); | |
852 | kfree(smp); | |
853 | return NULL; | |
854 | } | |
855 | ||
8aab4757 | 856 | smp->conn = conn; |
5d88cc73 | 857 | chan->data = smp; |
8aab4757 | 858 | |
b28b4943 JH |
859 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL); |
860 | ||
b68fda68 JH |
861 | INIT_DELAYED_WORK(&smp->security_timer, smp_timeout); |
862 | ||
8aab4757 VCG |
863 | hci_conn_hold(conn->hcon); |
864 | ||
865 | return smp; | |
866 | } | |
867 | ||
2b64d153 BG |
868 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
869 | { | |
b10e8017 | 870 | struct l2cap_conn *conn = hcon->l2cap_data; |
5d88cc73 | 871 | struct l2cap_chan *chan; |
2b64d153 BG |
872 | struct smp_chan *smp; |
873 | u32 value; | |
fc75cc86 | 874 | int err; |
2b64d153 BG |
875 | |
876 | BT_DBG(""); | |
877 | ||
fc75cc86 | 878 | if (!conn) |
2b64d153 BG |
879 | return -ENOTCONN; |
880 | ||
5d88cc73 JH |
881 | chan = conn->smp; |
882 | if (!chan) | |
883 | return -ENOTCONN; | |
884 | ||
fc75cc86 JH |
885 | l2cap_chan_lock(chan); |
886 | if (!chan->data) { | |
887 | err = -ENOTCONN; | |
888 | goto unlock; | |
889 | } | |
890 | ||
5d88cc73 | 891 | smp = chan->data; |
2b64d153 BG |
892 | |
893 | switch (mgmt_op) { | |
894 | case MGMT_OP_USER_PASSKEY_REPLY: | |
895 | value = le32_to_cpu(passkey); | |
943a732a | 896 | memset(smp->tk, 0, sizeof(smp->tk)); |
2b64d153 | 897 | BT_DBG("PassKey: %d", value); |
943a732a | 898 | put_unaligned_le32(value, smp->tk); |
2b64d153 BG |
899 | /* Fall Through */ |
900 | case MGMT_OP_USER_CONFIRM_REPLY: | |
4a74d658 | 901 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
902 | break; |
903 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: | |
904 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: | |
84794e11 | 905 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
fc75cc86 JH |
906 | err = 0; |
907 | goto unlock; | |
2b64d153 | 908 | default: |
84794e11 | 909 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
fc75cc86 JH |
910 | err = -EOPNOTSUPP; |
911 | goto unlock; | |
2b64d153 BG |
912 | } |
913 | ||
fc75cc86 JH |
914 | err = 0; |
915 | ||
2b64d153 | 916 | /* If it is our turn to send Pairing Confirm, do so now */ |
1cc61144 JH |
917 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) { |
918 | u8 rsp = smp_confirm(smp); | |
919 | if (rsp) | |
920 | smp_failure(conn, rsp); | |
921 | } | |
2b64d153 | 922 | |
fc75cc86 JH |
923 | unlock: |
924 | l2cap_chan_unlock(chan); | |
925 | return err; | |
2b64d153 BG |
926 | } |
927 | ||
da85e5e5 | 928 | static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 929 | { |
3158c50c | 930 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
fc75cc86 | 931 | struct l2cap_chan *chan = conn->smp; |
b3c6410b | 932 | struct hci_dev *hdev = conn->hcon->hdev; |
8aab4757 | 933 | struct smp_chan *smp; |
c7262e71 | 934 | u8 key_size, auth, sec_level; |
8aab4757 | 935 | int ret; |
88ba43b6 AB |
936 | |
937 | BT_DBG("conn %p", conn); | |
938 | ||
c46b98be | 939 | if (skb->len < sizeof(*req)) |
38e4a915 | 940 | return SMP_INVALID_PARAMS; |
c46b98be | 941 | |
40bef302 | 942 | if (conn->hcon->role != HCI_ROLE_SLAVE) |
2b64d153 BG |
943 | return SMP_CMD_NOTSUPP; |
944 | ||
fc75cc86 | 945 | if (!chan->data) |
8aab4757 | 946 | smp = smp_chan_create(conn); |
fc75cc86 | 947 | else |
5d88cc73 | 948 | smp = chan->data; |
8aab4757 | 949 | |
d08fd0e7 AE |
950 | if (!smp) |
951 | return SMP_UNSPECIFIED; | |
d26a2345 | 952 | |
b6ae8457 | 953 | if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) && |
b3c6410b JH |
954 | (req->auth_req & SMP_AUTH_BONDING)) |
955 | return SMP_PAIRING_NOTSUPP; | |
956 | ||
b28b4943 JH |
957 | SMP_DISALLOW_CMD(smp, SMP_CMD_PAIRING_REQ); |
958 | ||
1c1def09 VCG |
959 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
960 | memcpy(&smp->preq[1], req, sizeof(*req)); | |
3158c50c | 961 | skb_pull(skb, sizeof(*req)); |
88ba43b6 | 962 | |
2b64d153 | 963 | /* We didn't start the pairing, so match remote */ |
1ef35827 | 964 | auth = req->auth_req; |
da85e5e5 | 965 | |
c7262e71 JH |
966 | sec_level = authreq_to_seclevel(auth); |
967 | if (sec_level > conn->hcon->pending_sec_level) | |
968 | conn->hcon->pending_sec_level = sec_level; | |
fdde0a26 | 969 | |
2ed8f65c JH |
970 | /* If we need MITM check that it can be acheived */ |
971 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { | |
972 | u8 method; | |
973 | ||
974 | method = get_auth_method(smp, conn->hcon->io_capability, | |
975 | req->io_capability); | |
976 | if (method == JUST_WORKS || method == JUST_CFM) | |
977 | return SMP_AUTH_REQUIREMENTS; | |
978 | } | |
979 | ||
2b64d153 | 980 | build_pairing_cmd(conn, req, &rsp, auth); |
3158c50c VCG |
981 | |
982 | key_size = min(req->max_key_size, rsp.max_key_size); | |
983 | if (check_enc_key_size(conn, key_size)) | |
984 | return SMP_ENC_KEY_SIZE; | |
88ba43b6 | 985 | |
e84a6b13 | 986 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
8aab4757 | 987 | |
1c1def09 VCG |
988 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
989 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); | |
f01ead31 | 990 | |
3158c50c | 991 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
b28b4943 | 992 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
da85e5e5 | 993 | |
2b64d153 BG |
994 | /* Request setup of TK */ |
995 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); | |
996 | if (ret) | |
997 | return SMP_UNSPECIFIED; | |
998 | ||
da85e5e5 | 999 | return 0; |
88ba43b6 AB |
1000 | } |
1001 | ||
da85e5e5 | 1002 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 1003 | { |
3158c50c | 1004 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
5d88cc73 JH |
1005 | struct l2cap_chan *chan = conn->smp; |
1006 | struct smp_chan *smp = chan->data; | |
2b64d153 | 1007 | u8 key_size, auth = SMP_AUTH_NONE; |
7d24ddcc | 1008 | int ret; |
88ba43b6 AB |
1009 | |
1010 | BT_DBG("conn %p", conn); | |
1011 | ||
c46b98be | 1012 | if (skb->len < sizeof(*rsp)) |
38e4a915 | 1013 | return SMP_INVALID_PARAMS; |
c46b98be | 1014 | |
40bef302 | 1015 | if (conn->hcon->role != HCI_ROLE_MASTER) |
2b64d153 BG |
1016 | return SMP_CMD_NOTSUPP; |
1017 | ||
b28b4943 JH |
1018 | SMP_DISALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
1019 | ||
3158c50c VCG |
1020 | skb_pull(skb, sizeof(*rsp)); |
1021 | ||
1c1def09 | 1022 | req = (void *) &smp->preq[1]; |
da85e5e5 | 1023 | |
3158c50c VCG |
1024 | key_size = min(req->max_key_size, rsp->max_key_size); |
1025 | if (check_enc_key_size(conn, key_size)) | |
1026 | return SMP_ENC_KEY_SIZE; | |
1027 | ||
2ed8f65c JH |
1028 | /* If we need MITM check that it can be acheived */ |
1029 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { | |
1030 | u8 method; | |
1031 | ||
1032 | method = get_auth_method(smp, req->io_capability, | |
1033 | rsp->io_capability); | |
1034 | if (method == JUST_WORKS || method == JUST_CFM) | |
1035 | return SMP_AUTH_REQUIREMENTS; | |
1036 | } | |
1037 | ||
e84a6b13 | 1038 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
7d24ddcc | 1039 | |
8aab4757 VCG |
1040 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
1041 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); | |
7d24ddcc | 1042 | |
fdcc4bec JH |
1043 | /* Update remote key distribution in case the remote cleared |
1044 | * some bits that we had enabled in our request. | |
1045 | */ | |
1046 | smp->remote_key_dist &= rsp->resp_key_dist; | |
1047 | ||
2b64d153 | 1048 | if ((req->auth_req & SMP_AUTH_BONDING) && |
f1560463 | 1049 | (rsp->auth_req & SMP_AUTH_BONDING)) |
2b64d153 BG |
1050 | auth = SMP_AUTH_BONDING; |
1051 | ||
1052 | auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM; | |
1053 | ||
476585ec | 1054 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
2b64d153 BG |
1055 | if (ret) |
1056 | return SMP_UNSPECIFIED; | |
1057 | ||
4a74d658 | 1058 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
2b64d153 BG |
1059 | |
1060 | /* Can't compose response until we have been confirmed */ | |
4a74d658 | 1061 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
1cc61144 | 1062 | return smp_confirm(smp); |
da85e5e5 VCG |
1063 | |
1064 | return 0; | |
88ba43b6 AB |
1065 | } |
1066 | ||
da85e5e5 | 1067 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 1068 | { |
5d88cc73 JH |
1069 | struct l2cap_chan *chan = conn->smp; |
1070 | struct smp_chan *smp = chan->data; | |
7d24ddcc | 1071 | |
88ba43b6 AB |
1072 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
1073 | ||
c46b98be | 1074 | if (skb->len < sizeof(smp->pcnf)) |
38e4a915 | 1075 | return SMP_INVALID_PARAMS; |
c46b98be | 1076 | |
b28b4943 JH |
1077 | SMP_DISALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
1078 | ||
1c1def09 VCG |
1079 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
1080 | skb_pull(skb, sizeof(smp->pcnf)); | |
88ba43b6 | 1081 | |
b28b4943 | 1082 | if (conn->hcon->out) { |
943a732a JH |
1083 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
1084 | smp->prnd); | |
b28b4943 JH |
1085 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
1086 | return 0; | |
1087 | } | |
1088 | ||
1089 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) | |
1cc61144 | 1090 | return smp_confirm(smp); |
943a732a | 1091 | else |
4a74d658 | 1092 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
da85e5e5 VCG |
1093 | |
1094 | return 0; | |
88ba43b6 AB |
1095 | } |
1096 | ||
da85e5e5 | 1097 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 1098 | { |
5d88cc73 JH |
1099 | struct l2cap_chan *chan = conn->smp; |
1100 | struct smp_chan *smp = chan->data; | |
7d24ddcc | 1101 | |
8aab4757 | 1102 | BT_DBG("conn %p", conn); |
3158c50c | 1103 | |
c46b98be | 1104 | if (skb->len < sizeof(smp->rrnd)) |
38e4a915 | 1105 | return SMP_INVALID_PARAMS; |
c46b98be | 1106 | |
b28b4943 JH |
1107 | SMP_DISALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
1108 | ||
943a732a | 1109 | memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd)); |
8aab4757 | 1110 | skb_pull(skb, sizeof(smp->rrnd)); |
e7e62c85 | 1111 | |
861580a9 | 1112 | return smp_random(smp); |
88ba43b6 AB |
1113 | } |
1114 | ||
f81cd823 | 1115 | static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
988c5997 | 1116 | { |
c9839a11 | 1117 | struct smp_ltk *key; |
988c5997 VCG |
1118 | struct hci_conn *hcon = conn->hcon; |
1119 | ||
98a0b845 | 1120 | key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type, |
e804d25d | 1121 | hcon->role); |
988c5997 | 1122 | if (!key) |
f81cd823 | 1123 | return false; |
988c5997 | 1124 | |
4dab7864 | 1125 | if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated) |
f81cd823 | 1126 | return false; |
4dab7864 | 1127 | |
51a8efd7 | 1128 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
f81cd823 | 1129 | return true; |
988c5997 | 1130 | |
c9839a11 VCG |
1131 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
1132 | hcon->enc_key_size = key->enc_size; | |
988c5997 | 1133 | |
fe59a05f JH |
1134 | /* We never store STKs for master role, so clear this flag */ |
1135 | clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); | |
1136 | ||
f81cd823 | 1137 | return true; |
988c5997 | 1138 | } |
f1560463 | 1139 | |
854f4727 JH |
1140 | bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level) |
1141 | { | |
1142 | if (sec_level == BT_SECURITY_LOW) | |
1143 | return true; | |
1144 | ||
9ab65d60 JH |
1145 | /* If we're encrypted with an STK always claim insufficient |
1146 | * security. This way we allow the connection to be re-encrypted | |
1147 | * with an LTK, even if the LTK provides the same level of | |
b2d5e254 JH |
1148 | * security. Only exception is if we don't have an LTK (e.g. |
1149 | * because of key distribution bits). | |
9ab65d60 | 1150 | */ |
b2d5e254 JH |
1151 | if (test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) && |
1152 | hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type, | |
e804d25d | 1153 | hcon->role)) |
9ab65d60 JH |
1154 | return false; |
1155 | ||
854f4727 JH |
1156 | if (hcon->sec_level >= sec_level) |
1157 | return true; | |
1158 | ||
1159 | return false; | |
1160 | } | |
1161 | ||
da85e5e5 | 1162 | static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 AB |
1163 | { |
1164 | struct smp_cmd_security_req *rp = (void *) skb->data; | |
1165 | struct smp_cmd_pairing cp; | |
f1cb9af5 | 1166 | struct hci_conn *hcon = conn->hcon; |
8aab4757 | 1167 | struct smp_chan *smp; |
c7262e71 | 1168 | u8 sec_level; |
88ba43b6 AB |
1169 | |
1170 | BT_DBG("conn %p", conn); | |
1171 | ||
c46b98be | 1172 | if (skb->len < sizeof(*rp)) |
38e4a915 | 1173 | return SMP_INVALID_PARAMS; |
c46b98be | 1174 | |
40bef302 | 1175 | if (hcon->role != HCI_ROLE_MASTER) |
86ca9eac JH |
1176 | return SMP_CMD_NOTSUPP; |
1177 | ||
c7262e71 | 1178 | sec_level = authreq_to_seclevel(rp->auth_req); |
854f4727 JH |
1179 | if (smp_sufficient_security(hcon, sec_level)) |
1180 | return 0; | |
1181 | ||
c7262e71 JH |
1182 | if (sec_level > hcon->pending_sec_level) |
1183 | hcon->pending_sec_level = sec_level; | |
feb45eb5 | 1184 | |
4dab7864 | 1185 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
988c5997 VCG |
1186 | return 0; |
1187 | ||
8aab4757 | 1188 | smp = smp_chan_create(conn); |
c29d2444 JH |
1189 | if (!smp) |
1190 | return SMP_UNSPECIFIED; | |
d26a2345 | 1191 | |
b6ae8457 | 1192 | if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) && |
616d55be JH |
1193 | (rp->auth_req & SMP_AUTH_BONDING)) |
1194 | return SMP_PAIRING_NOTSUPP; | |
1195 | ||
88ba43b6 | 1196 | skb_pull(skb, sizeof(*rp)); |
88ba43b6 | 1197 | |
da85e5e5 | 1198 | memset(&cp, 0, sizeof(cp)); |
54790f73 | 1199 | build_pairing_cmd(conn, &cp, NULL, rp->auth_req); |
88ba43b6 | 1200 | |
1c1def09 VCG |
1201 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
1202 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 1203 | |
88ba43b6 | 1204 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
b28b4943 | 1205 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
f1cb9af5 | 1206 | |
da85e5e5 | 1207 | return 0; |
88ba43b6 AB |
1208 | } |
1209 | ||
cc110922 | 1210 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
eb492e01 | 1211 | { |
cc110922 | 1212 | struct l2cap_conn *conn = hcon->l2cap_data; |
c68b7f12 | 1213 | struct l2cap_chan *chan; |
0a66cf20 | 1214 | struct smp_chan *smp; |
2b64d153 | 1215 | __u8 authreq; |
fc75cc86 | 1216 | int ret; |
eb492e01 | 1217 | |
3a0259bb VCG |
1218 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
1219 | ||
0a66cf20 JH |
1220 | /* This may be NULL if there's an unexpected disconnection */ |
1221 | if (!conn) | |
1222 | return 1; | |
1223 | ||
c68b7f12 JH |
1224 | chan = conn->smp; |
1225 | ||
757aee0f | 1226 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
2e65c9d2 AG |
1227 | return 1; |
1228 | ||
ad32a2f5 | 1229 | if (smp_sufficient_security(hcon, sec_level)) |
eb492e01 | 1230 | return 1; |
f1cb9af5 | 1231 | |
c7262e71 JH |
1232 | if (sec_level > hcon->pending_sec_level) |
1233 | hcon->pending_sec_level = sec_level; | |
1234 | ||
40bef302 | 1235 | if (hcon->role == HCI_ROLE_MASTER) |
c7262e71 JH |
1236 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
1237 | return 0; | |
d26a2345 | 1238 | |
fc75cc86 JH |
1239 | l2cap_chan_lock(chan); |
1240 | ||
1241 | /* If SMP is already in progress ignore this request */ | |
1242 | if (chan->data) { | |
1243 | ret = 0; | |
1244 | goto unlock; | |
1245 | } | |
d26a2345 | 1246 | |
8aab4757 | 1247 | smp = smp_chan_create(conn); |
fc75cc86 JH |
1248 | if (!smp) { |
1249 | ret = 1; | |
1250 | goto unlock; | |
1251 | } | |
2b64d153 BG |
1252 | |
1253 | authreq = seclevel_to_authreq(sec_level); | |
d26a2345 | 1254 | |
79897d20 JH |
1255 | /* Require MITM if IO Capability allows or the security level |
1256 | * requires it. | |
2e233644 | 1257 | */ |
79897d20 | 1258 | if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT || |
c7262e71 | 1259 | hcon->pending_sec_level > BT_SECURITY_MEDIUM) |
2e233644 JH |
1260 | authreq |= SMP_AUTH_MITM; |
1261 | ||
40bef302 | 1262 | if (hcon->role == HCI_ROLE_MASTER) { |
d26a2345 | 1263 | struct smp_cmd_pairing cp; |
f01ead31 | 1264 | |
2b64d153 | 1265 | build_pairing_cmd(conn, &cp, NULL, authreq); |
1c1def09 VCG |
1266 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
1267 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 1268 | |
eb492e01 | 1269 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
b28b4943 | 1270 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
eb492e01 AB |
1271 | } else { |
1272 | struct smp_cmd_security_req cp; | |
2b64d153 | 1273 | cp.auth_req = authreq; |
eb492e01 | 1274 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
b28b4943 | 1275 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ); |
eb492e01 AB |
1276 | } |
1277 | ||
4a74d658 | 1278 | set_bit(SMP_FLAG_INITIATOR, &smp->flags); |
fc75cc86 | 1279 | ret = 0; |
edca792c | 1280 | |
fc75cc86 JH |
1281 | unlock: |
1282 | l2cap_chan_unlock(chan); | |
1283 | return ret; | |
eb492e01 AB |
1284 | } |
1285 | ||
7034b911 VCG |
1286 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1287 | { | |
16b90839 | 1288 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
5d88cc73 JH |
1289 | struct l2cap_chan *chan = conn->smp; |
1290 | struct smp_chan *smp = chan->data; | |
16b90839 | 1291 | |
c46b98be JH |
1292 | BT_DBG("conn %p", conn); |
1293 | ||
1294 | if (skb->len < sizeof(*rp)) | |
38e4a915 | 1295 | return SMP_INVALID_PARAMS; |
c46b98be | 1296 | |
b28b4943 JH |
1297 | SMP_DISALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO); |
1298 | SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT); | |
6131ddc8 | 1299 | |
16b90839 VCG |
1300 | skb_pull(skb, sizeof(*rp)); |
1301 | ||
1c1def09 | 1302 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
16b90839 | 1303 | |
7034b911 VCG |
1304 | return 0; |
1305 | } | |
1306 | ||
1307 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) | |
1308 | { | |
16b90839 | 1309 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
5d88cc73 JH |
1310 | struct l2cap_chan *chan = conn->smp; |
1311 | struct smp_chan *smp = chan->data; | |
c9839a11 VCG |
1312 | struct hci_dev *hdev = conn->hcon->hdev; |
1313 | struct hci_conn *hcon = conn->hcon; | |
23d0e128 | 1314 | struct smp_ltk *ltk; |
c9839a11 | 1315 | u8 authenticated; |
16b90839 | 1316 | |
c46b98be JH |
1317 | BT_DBG("conn %p", conn); |
1318 | ||
1319 | if (skb->len < sizeof(*rp)) | |
38e4a915 | 1320 | return SMP_INVALID_PARAMS; |
c46b98be | 1321 | |
9747a9f3 JH |
1322 | /* Mark the information as received */ |
1323 | smp->remote_key_dist &= ~SMP_DIST_ENC_KEY; | |
1324 | ||
b28b4943 JH |
1325 | SMP_DISALLOW_CMD(smp, SMP_CMD_MASTER_IDENT); |
1326 | if (smp->remote_key_dist & SMP_DIST_ID_KEY) | |
1327 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); | |
1328 | ||
16b90839 | 1329 | skb_pull(skb, sizeof(*rp)); |
7034b911 | 1330 | |
c9839a11 | 1331 | hci_dev_lock(hdev); |
ce39fb4e | 1332 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
2ceba539 | 1333 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK, |
23d0e128 JH |
1334 | authenticated, smp->tk, smp->enc_key_size, |
1335 | rp->ediv, rp->rand); | |
1336 | smp->ltk = ltk; | |
c6e81e9a | 1337 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
d6268e86 | 1338 | smp_distribute_keys(smp); |
c9839a11 | 1339 | hci_dev_unlock(hdev); |
7034b911 VCG |
1340 | |
1341 | return 0; | |
1342 | } | |
1343 | ||
fd349c02 JH |
1344 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1345 | { | |
1346 | struct smp_cmd_ident_info *info = (void *) skb->data; | |
5d88cc73 JH |
1347 | struct l2cap_chan *chan = conn->smp; |
1348 | struct smp_chan *smp = chan->data; | |
fd349c02 JH |
1349 | |
1350 | BT_DBG(""); | |
1351 | ||
1352 | if (skb->len < sizeof(*info)) | |
38e4a915 | 1353 | return SMP_INVALID_PARAMS; |
fd349c02 | 1354 | |
b28b4943 JH |
1355 | SMP_DISALLOW_CMD(smp, SMP_CMD_IDENT_INFO); |
1356 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO); | |
6131ddc8 | 1357 | |
fd349c02 JH |
1358 | skb_pull(skb, sizeof(*info)); |
1359 | ||
1360 | memcpy(smp->irk, info->irk, 16); | |
1361 | ||
1362 | return 0; | |
1363 | } | |
1364 | ||
1365 | static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, | |
1366 | struct sk_buff *skb) | |
1367 | { | |
1368 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; | |
5d88cc73 JH |
1369 | struct l2cap_chan *chan = conn->smp; |
1370 | struct smp_chan *smp = chan->data; | |
fd349c02 JH |
1371 | struct hci_conn *hcon = conn->hcon; |
1372 | bdaddr_t rpa; | |
1373 | ||
1374 | BT_DBG(""); | |
1375 | ||
1376 | if (skb->len < sizeof(*info)) | |
38e4a915 | 1377 | return SMP_INVALID_PARAMS; |
fd349c02 | 1378 | |
9747a9f3 JH |
1379 | /* Mark the information as received */ |
1380 | smp->remote_key_dist &= ~SMP_DIST_ID_KEY; | |
1381 | ||
b28b4943 JH |
1382 | SMP_DISALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO); |
1383 | if (smp->remote_key_dist & SMP_DIST_SIGN) | |
1384 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); | |
1385 | ||
fd349c02 JH |
1386 | skb_pull(skb, sizeof(*info)); |
1387 | ||
31dd624e JH |
1388 | hci_dev_lock(hcon->hdev); |
1389 | ||
a9a58f86 JH |
1390 | /* Strictly speaking the Core Specification (4.1) allows sending |
1391 | * an empty address which would force us to rely on just the IRK | |
1392 | * as "identity information". However, since such | |
1393 | * implementations are not known of and in order to not over | |
1394 | * complicate our implementation, simply pretend that we never | |
1395 | * received an IRK for such a device. | |
1396 | */ | |
1397 | if (!bacmp(&info->bdaddr, BDADDR_ANY)) { | |
1398 | BT_ERR("Ignoring IRK with no identity address"); | |
31dd624e | 1399 | goto distribute; |
a9a58f86 JH |
1400 | } |
1401 | ||
fd349c02 JH |
1402 | bacpy(&smp->id_addr, &info->bdaddr); |
1403 | smp->id_addr_type = info->addr_type; | |
1404 | ||
1405 | if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type)) | |
1406 | bacpy(&rpa, &hcon->dst); | |
1407 | else | |
1408 | bacpy(&rpa, BDADDR_ANY); | |
1409 | ||
23d0e128 JH |
1410 | smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, |
1411 | smp->id_addr_type, smp->irk, &rpa); | |
fd349c02 | 1412 | |
31dd624e | 1413 | distribute: |
c6e81e9a JH |
1414 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
1415 | smp_distribute_keys(smp); | |
fd349c02 | 1416 | |
31dd624e JH |
1417 | hci_dev_unlock(hcon->hdev); |
1418 | ||
fd349c02 JH |
1419 | return 0; |
1420 | } | |
1421 | ||
7ee4ea36 MH |
1422 | static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1423 | { | |
1424 | struct smp_cmd_sign_info *rp = (void *) skb->data; | |
5d88cc73 JH |
1425 | struct l2cap_chan *chan = conn->smp; |
1426 | struct smp_chan *smp = chan->data; | |
7ee4ea36 MH |
1427 | struct hci_dev *hdev = conn->hcon->hdev; |
1428 | struct smp_csrk *csrk; | |
1429 | ||
1430 | BT_DBG("conn %p", conn); | |
1431 | ||
1432 | if (skb->len < sizeof(*rp)) | |
38e4a915 | 1433 | return SMP_INVALID_PARAMS; |
7ee4ea36 | 1434 | |
7ee4ea36 MH |
1435 | /* Mark the information as received */ |
1436 | smp->remote_key_dist &= ~SMP_DIST_SIGN; | |
1437 | ||
b28b4943 JH |
1438 | SMP_DISALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
1439 | ||
7ee4ea36 MH |
1440 | skb_pull(skb, sizeof(*rp)); |
1441 | ||
1442 | hci_dev_lock(hdev); | |
1443 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); | |
1444 | if (csrk) { | |
1445 | csrk->master = 0x01; | |
1446 | memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); | |
1447 | } | |
1448 | smp->csrk = csrk; | |
d6268e86 | 1449 | smp_distribute_keys(smp); |
7ee4ea36 MH |
1450 | hci_dev_unlock(hdev); |
1451 | ||
1452 | return 0; | |
1453 | } | |
1454 | ||
4befb867 | 1455 | static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb) |
eb492e01 | 1456 | { |
5d88cc73 | 1457 | struct l2cap_conn *conn = chan->conn; |
7b9899db | 1458 | struct hci_conn *hcon = conn->hcon; |
b28b4943 | 1459 | struct smp_chan *smp; |
92381f5c | 1460 | __u8 code, reason; |
eb492e01 AB |
1461 | int err = 0; |
1462 | ||
7b9899db MH |
1463 | if (hcon->type != LE_LINK) { |
1464 | kfree_skb(skb); | |
3432711f | 1465 | return 0; |
7b9899db MH |
1466 | } |
1467 | ||
8ae9b984 | 1468 | if (skb->len < 1) |
92381f5c | 1469 | return -EILSEQ; |
92381f5c | 1470 | |
06ae3314 | 1471 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
2e65c9d2 AG |
1472 | reason = SMP_PAIRING_NOTSUPP; |
1473 | goto done; | |
1474 | } | |
1475 | ||
92381f5c | 1476 | code = skb->data[0]; |
eb492e01 AB |
1477 | skb_pull(skb, sizeof(code)); |
1478 | ||
b28b4943 JH |
1479 | smp = chan->data; |
1480 | ||
1481 | if (code > SMP_CMD_MAX) | |
1482 | goto drop; | |
1483 | ||
1484 | if (smp && !test_bit(code, &smp->allow_cmd)) | |
1485 | goto drop; | |
1486 | ||
1487 | /* If we don't have a context the only allowed commands are | |
1488 | * pairing request and security request. | |
8cf9fa12 | 1489 | */ |
b28b4943 JH |
1490 | if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ) |
1491 | goto drop; | |
8cf9fa12 | 1492 | |
eb492e01 AB |
1493 | switch (code) { |
1494 | case SMP_CMD_PAIRING_REQ: | |
da85e5e5 | 1495 | reason = smp_cmd_pairing_req(conn, skb); |
eb492e01 AB |
1496 | break; |
1497 | ||
1498 | case SMP_CMD_PAIRING_FAIL: | |
84794e11 | 1499 | smp_failure(conn, 0); |
da85e5e5 | 1500 | err = -EPERM; |
eb492e01 AB |
1501 | break; |
1502 | ||
1503 | case SMP_CMD_PAIRING_RSP: | |
da85e5e5 | 1504 | reason = smp_cmd_pairing_rsp(conn, skb); |
88ba43b6 AB |
1505 | break; |
1506 | ||
1507 | case SMP_CMD_SECURITY_REQ: | |
da85e5e5 | 1508 | reason = smp_cmd_security_req(conn, skb); |
88ba43b6 AB |
1509 | break; |
1510 | ||
eb492e01 | 1511 | case SMP_CMD_PAIRING_CONFIRM: |
da85e5e5 | 1512 | reason = smp_cmd_pairing_confirm(conn, skb); |
88ba43b6 AB |
1513 | break; |
1514 | ||
eb492e01 | 1515 | case SMP_CMD_PAIRING_RANDOM: |
da85e5e5 | 1516 | reason = smp_cmd_pairing_random(conn, skb); |
88ba43b6 AB |
1517 | break; |
1518 | ||
eb492e01 | 1519 | case SMP_CMD_ENCRYPT_INFO: |
7034b911 VCG |
1520 | reason = smp_cmd_encrypt_info(conn, skb); |
1521 | break; | |
1522 | ||
eb492e01 | 1523 | case SMP_CMD_MASTER_IDENT: |
7034b911 VCG |
1524 | reason = smp_cmd_master_ident(conn, skb); |
1525 | break; | |
1526 | ||
eb492e01 | 1527 | case SMP_CMD_IDENT_INFO: |
fd349c02 JH |
1528 | reason = smp_cmd_ident_info(conn, skb); |
1529 | break; | |
1530 | ||
eb492e01 | 1531 | case SMP_CMD_IDENT_ADDR_INFO: |
fd349c02 JH |
1532 | reason = smp_cmd_ident_addr_info(conn, skb); |
1533 | break; | |
1534 | ||
eb492e01 | 1535 | case SMP_CMD_SIGN_INFO: |
7ee4ea36 | 1536 | reason = smp_cmd_sign_info(conn, skb); |
7034b911 VCG |
1537 | break; |
1538 | ||
eb492e01 AB |
1539 | default: |
1540 | BT_DBG("Unknown command code 0x%2.2x", code); | |
eb492e01 | 1541 | reason = SMP_CMD_NOTSUPP; |
3a0259bb | 1542 | goto done; |
eb492e01 AB |
1543 | } |
1544 | ||
3a0259bb | 1545 | done: |
9b7b18ef JH |
1546 | if (!err) { |
1547 | if (reason) | |
1548 | smp_failure(conn, reason); | |
8ae9b984 | 1549 | kfree_skb(skb); |
9b7b18ef JH |
1550 | } |
1551 | ||
eb492e01 | 1552 | return err; |
b28b4943 JH |
1553 | |
1554 | drop: | |
1555 | BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name, | |
1556 | code, &hcon->dst); | |
1557 | kfree_skb(skb); | |
1558 | return 0; | |
eb492e01 | 1559 | } |
7034b911 | 1560 | |
70db83c4 JH |
1561 | static void smp_teardown_cb(struct l2cap_chan *chan, int err) |
1562 | { | |
1563 | struct l2cap_conn *conn = chan->conn; | |
1564 | ||
1565 | BT_DBG("chan %p", chan); | |
1566 | ||
fc75cc86 | 1567 | if (chan->data) |
5d88cc73 | 1568 | smp_chan_destroy(conn); |
5d88cc73 | 1569 | |
70db83c4 JH |
1570 | conn->smp = NULL; |
1571 | l2cap_chan_put(chan); | |
1572 | } | |
1573 | ||
44f1a7ab JH |
1574 | static void smp_resume_cb(struct l2cap_chan *chan) |
1575 | { | |
b68fda68 | 1576 | struct smp_chan *smp = chan->data; |
44f1a7ab JH |
1577 | struct l2cap_conn *conn = chan->conn; |
1578 | struct hci_conn *hcon = conn->hcon; | |
1579 | ||
1580 | BT_DBG("chan %p", chan); | |
1581 | ||
86d1407c JH |
1582 | if (!smp) |
1583 | return; | |
b68fda68 | 1584 | |
84bc0db5 JH |
1585 | if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags)) |
1586 | return; | |
1587 | ||
86d1407c JH |
1588 | cancel_delayed_work(&smp->security_timer); |
1589 | ||
d6268e86 | 1590 | smp_distribute_keys(smp); |
44f1a7ab JH |
1591 | } |
1592 | ||
70db83c4 JH |
1593 | static void smp_ready_cb(struct l2cap_chan *chan) |
1594 | { | |
1595 | struct l2cap_conn *conn = chan->conn; | |
1596 | ||
1597 | BT_DBG("chan %p", chan); | |
1598 | ||
1599 | conn->smp = chan; | |
1600 | l2cap_chan_hold(chan); | |
1601 | } | |
1602 | ||
4befb867 JH |
1603 | static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
1604 | { | |
1605 | int err; | |
1606 | ||
1607 | BT_DBG("chan %p", chan); | |
1608 | ||
1609 | err = smp_sig_channel(chan, skb); | |
1610 | if (err) { | |
b68fda68 | 1611 | struct smp_chan *smp = chan->data; |
4befb867 | 1612 | |
b68fda68 JH |
1613 | if (smp) |
1614 | cancel_delayed_work_sync(&smp->security_timer); | |
4befb867 | 1615 | |
1e91c29e | 1616 | hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE); |
4befb867 JH |
1617 | } |
1618 | ||
1619 | return err; | |
1620 | } | |
1621 | ||
70db83c4 JH |
1622 | static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan, |
1623 | unsigned long hdr_len, | |
1624 | unsigned long len, int nb) | |
1625 | { | |
1626 | struct sk_buff *skb; | |
1627 | ||
1628 | skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL); | |
1629 | if (!skb) | |
1630 | return ERR_PTR(-ENOMEM); | |
1631 | ||
1632 | skb->priority = HCI_PRIO_MAX; | |
1633 | bt_cb(skb)->chan = chan; | |
1634 | ||
1635 | return skb; | |
1636 | } | |
1637 | ||
1638 | static const struct l2cap_ops smp_chan_ops = { | |
1639 | .name = "Security Manager", | |
1640 | .ready = smp_ready_cb, | |
5d88cc73 | 1641 | .recv = smp_recv_cb, |
70db83c4 JH |
1642 | .alloc_skb = smp_alloc_skb_cb, |
1643 | .teardown = smp_teardown_cb, | |
44f1a7ab | 1644 | .resume = smp_resume_cb, |
70db83c4 JH |
1645 | |
1646 | .new_connection = l2cap_chan_no_new_connection, | |
70db83c4 JH |
1647 | .state_change = l2cap_chan_no_state_change, |
1648 | .close = l2cap_chan_no_close, | |
1649 | .defer = l2cap_chan_no_defer, | |
1650 | .suspend = l2cap_chan_no_suspend, | |
70db83c4 JH |
1651 | .set_shutdown = l2cap_chan_no_set_shutdown, |
1652 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, | |
1653 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, | |
1654 | }; | |
1655 | ||
1656 | static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan) | |
1657 | { | |
1658 | struct l2cap_chan *chan; | |
1659 | ||
1660 | BT_DBG("pchan %p", pchan); | |
1661 | ||
1662 | chan = l2cap_chan_create(); | |
1663 | if (!chan) | |
1664 | return NULL; | |
1665 | ||
1666 | chan->chan_type = pchan->chan_type; | |
1667 | chan->ops = &smp_chan_ops; | |
1668 | chan->scid = pchan->scid; | |
1669 | chan->dcid = chan->scid; | |
1670 | chan->imtu = pchan->imtu; | |
1671 | chan->omtu = pchan->omtu; | |
1672 | chan->mode = pchan->mode; | |
1673 | ||
1674 | BT_DBG("created chan %p", chan); | |
1675 | ||
1676 | return chan; | |
1677 | } | |
1678 | ||
1679 | static const struct l2cap_ops smp_root_chan_ops = { | |
1680 | .name = "Security Manager Root", | |
1681 | .new_connection = smp_new_conn_cb, | |
1682 | ||
1683 | /* None of these are implemented for the root channel */ | |
1684 | .close = l2cap_chan_no_close, | |
1685 | .alloc_skb = l2cap_chan_no_alloc_skb, | |
1686 | .recv = l2cap_chan_no_recv, | |
1687 | .state_change = l2cap_chan_no_state_change, | |
1688 | .teardown = l2cap_chan_no_teardown, | |
1689 | .ready = l2cap_chan_no_ready, | |
1690 | .defer = l2cap_chan_no_defer, | |
1691 | .suspend = l2cap_chan_no_suspend, | |
1692 | .resume = l2cap_chan_no_resume, | |
1693 | .set_shutdown = l2cap_chan_no_set_shutdown, | |
1694 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, | |
1695 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, | |
1696 | }; | |
1697 | ||
711eafe3 JH |
1698 | int smp_register(struct hci_dev *hdev) |
1699 | { | |
70db83c4 | 1700 | struct l2cap_chan *chan; |
defce9e8 | 1701 | struct crypto_blkcipher *tfm_aes; |
70db83c4 | 1702 | |
711eafe3 JH |
1703 | BT_DBG("%s", hdev->name); |
1704 | ||
defce9e8 JH |
1705 | tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); |
1706 | if (IS_ERR(tfm_aes)) { | |
1707 | int err = PTR_ERR(tfm_aes); | |
711eafe3 | 1708 | BT_ERR("Unable to create crypto context"); |
711eafe3 JH |
1709 | return err; |
1710 | } | |
1711 | ||
70db83c4 JH |
1712 | chan = l2cap_chan_create(); |
1713 | if (!chan) { | |
defce9e8 | 1714 | crypto_free_blkcipher(tfm_aes); |
70db83c4 JH |
1715 | return -ENOMEM; |
1716 | } | |
1717 | ||
defce9e8 JH |
1718 | chan->data = tfm_aes; |
1719 | ||
5d88cc73 | 1720 | l2cap_add_scid(chan, L2CAP_CID_SMP); |
70db83c4 JH |
1721 | |
1722 | l2cap_chan_set_defaults(chan); | |
1723 | ||
1724 | bacpy(&chan->src, &hdev->bdaddr); | |
1725 | chan->src_type = BDADDR_LE_PUBLIC; | |
1726 | chan->state = BT_LISTEN; | |
1727 | chan->mode = L2CAP_MODE_BASIC; | |
1728 | chan->imtu = L2CAP_DEFAULT_MTU; | |
1729 | chan->ops = &smp_root_chan_ops; | |
1730 | ||
1731 | hdev->smp_data = chan; | |
1732 | ||
711eafe3 JH |
1733 | return 0; |
1734 | } | |
1735 | ||
1736 | void smp_unregister(struct hci_dev *hdev) | |
1737 | { | |
70db83c4 | 1738 | struct l2cap_chan *chan = hdev->smp_data; |
defce9e8 | 1739 | struct crypto_blkcipher *tfm_aes; |
70db83c4 JH |
1740 | |
1741 | if (!chan) | |
1742 | return; | |
1743 | ||
1744 | BT_DBG("%s chan %p", hdev->name, chan); | |
711eafe3 | 1745 | |
defce9e8 JH |
1746 | tfm_aes = chan->data; |
1747 | if (tfm_aes) { | |
1748 | chan->data = NULL; | |
1749 | crypto_free_blkcipher(tfm_aes); | |
711eafe3 | 1750 | } |
70db83c4 JH |
1751 | |
1752 | hdev->smp_data = NULL; | |
1753 | l2cap_chan_put(chan); | |
711eafe3 | 1754 | } |