]>
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 | |
17b02e62 | 34 | #define SMP_TIMEOUT msecs_to_jiffies(30000) |
5d3de7df | 35 | |
065a13e2 JH |
36 | #define AUTH_REQ_MASK 0x07 |
37 | ||
d22ef0bc AB |
38 | static inline void swap128(u8 src[16], u8 dst[16]) |
39 | { | |
40 | int i; | |
41 | for (i = 0; i < 16; i++) | |
42 | dst[15 - i] = src[i]; | |
43 | } | |
44 | ||
45 | static inline void swap56(u8 src[7], u8 dst[7]) | |
46 | { | |
47 | int i; | |
48 | for (i = 0; i < 7; i++) | |
49 | dst[6 - i] = src[i]; | |
50 | } | |
51 | ||
52 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) | |
53 | { | |
54 | struct blkcipher_desc desc; | |
55 | struct scatterlist sg; | |
56 | int err, iv_len; | |
57 | unsigned char iv[128]; | |
58 | ||
59 | if (tfm == NULL) { | |
60 | BT_ERR("tfm %p", tfm); | |
61 | return -EINVAL; | |
62 | } | |
63 | ||
64 | desc.tfm = tfm; | |
65 | desc.flags = 0; | |
66 | ||
67 | err = crypto_blkcipher_setkey(tfm, k, 16); | |
68 | if (err) { | |
69 | BT_ERR("cipher setkey failed: %d", err); | |
70 | return err; | |
71 | } | |
72 | ||
73 | sg_init_one(&sg, r, 16); | |
74 | ||
75 | iv_len = crypto_blkcipher_ivsize(tfm); | |
76 | if (iv_len) { | |
77 | memset(&iv, 0xff, iv_len); | |
78 | crypto_blkcipher_set_iv(tfm, iv, iv_len); | |
79 | } | |
80 | ||
81 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); | |
82 | if (err) | |
83 | BT_ERR("Encrypt data error %d", err); | |
84 | ||
85 | return err; | |
86 | } | |
87 | ||
88 | static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16], | |
89 | u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, | |
90 | u8 _rat, bdaddr_t *ra, u8 res[16]) | |
91 | { | |
92 | u8 p1[16], p2[16]; | |
93 | int err; | |
94 | ||
95 | memset(p1, 0, 16); | |
96 | ||
97 | /* p1 = pres || preq || _rat || _iat */ | |
98 | swap56(pres, p1); | |
99 | swap56(preq, p1 + 7); | |
100 | p1[14] = _rat; | |
101 | p1[15] = _iat; | |
102 | ||
103 | memset(p2, 0, 16); | |
104 | ||
105 | /* p2 = padding || ia || ra */ | |
106 | baswap((bdaddr_t *) (p2 + 4), ia); | |
107 | baswap((bdaddr_t *) (p2 + 10), ra); | |
108 | ||
109 | /* res = r XOR p1 */ | |
110 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); | |
111 | ||
112 | /* res = e(k, res) */ | |
113 | err = smp_e(tfm, k, res); | |
114 | if (err) { | |
115 | BT_ERR("Encrypt data error"); | |
116 | return err; | |
117 | } | |
118 | ||
119 | /* res = res XOR p2 */ | |
120 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); | |
121 | ||
122 | /* res = e(k, res) */ | |
123 | err = smp_e(tfm, k, res); | |
124 | if (err) | |
125 | BT_ERR("Encrypt data error"); | |
126 | ||
127 | return err; | |
128 | } | |
129 | ||
130 | static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], | |
131 | u8 r1[16], u8 r2[16], u8 _r[16]) | |
132 | { | |
133 | int err; | |
134 | ||
135 | /* Just least significant octets from r1 and r2 are considered */ | |
136 | memcpy(_r, r1 + 8, 8); | |
137 | memcpy(_r + 8, r2 + 8, 8); | |
138 | ||
139 | err = smp_e(tfm, k, _r); | |
140 | if (err) | |
141 | BT_ERR("Encrypt data error"); | |
142 | ||
143 | return err; | |
144 | } | |
145 | ||
146 | static int smp_rand(u8 *buf) | |
147 | { | |
148 | get_random_bytes(buf, 16); | |
149 | ||
150 | return 0; | |
151 | } | |
eb492e01 AB |
152 | |
153 | static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code, | |
154 | u16 dlen, void *data) | |
155 | { | |
156 | struct sk_buff *skb; | |
157 | struct l2cap_hdr *lh; | |
158 | int len; | |
159 | ||
160 | len = L2CAP_HDR_SIZE + sizeof(code) + dlen; | |
161 | ||
162 | if (len > conn->mtu) | |
163 | return NULL; | |
164 | ||
165 | skb = bt_skb_alloc(len, GFP_ATOMIC); | |
166 | if (!skb) | |
167 | return NULL; | |
168 | ||
169 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); | |
170 | lh->len = cpu_to_le16(sizeof(code) + dlen); | |
d8aece2a | 171 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_SMP); |
eb492e01 AB |
172 | |
173 | memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code)); | |
174 | ||
175 | memcpy(skb_put(skb, dlen), data, dlen); | |
176 | ||
177 | return skb; | |
178 | } | |
179 | ||
180 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) | |
181 | { | |
182 | struct sk_buff *skb = smp_build_cmd(conn, code, len, data); | |
183 | ||
184 | BT_DBG("code 0x%2.2x", code); | |
185 | ||
186 | if (!skb) | |
187 | return; | |
188 | ||
73d80deb LAD |
189 | skb->priority = HCI_PRIO_MAX; |
190 | hci_send_acl(conn->hchan, skb, 0); | |
e2dcd113 | 191 | |
6c9d42a1 | 192 | cancel_delayed_work_sync(&conn->security_timer); |
17b02e62 | 193 | schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT); |
eb492e01 AB |
194 | } |
195 | ||
2b64d153 BG |
196 | static __u8 authreq_to_seclevel(__u8 authreq) |
197 | { | |
198 | if (authreq & SMP_AUTH_MITM) | |
199 | return BT_SECURITY_HIGH; | |
200 | else | |
201 | return BT_SECURITY_MEDIUM; | |
202 | } | |
203 | ||
204 | static __u8 seclevel_to_authreq(__u8 sec_level) | |
205 | { | |
206 | switch (sec_level) { | |
207 | case BT_SECURITY_HIGH: | |
208 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; | |
209 | case BT_SECURITY_MEDIUM: | |
210 | return SMP_AUTH_BONDING; | |
211 | default: | |
212 | return SMP_AUTH_NONE; | |
213 | } | |
214 | } | |
215 | ||
b8e66eac | 216 | static void build_pairing_cmd(struct l2cap_conn *conn, |
54790f73 VCG |
217 | struct smp_cmd_pairing *req, |
218 | struct smp_cmd_pairing *rsp, | |
219 | __u8 authreq) | |
b8e66eac | 220 | { |
2b64d153 | 221 | u8 dist_keys = 0; |
54790f73 | 222 | |
a8b2d5c2 | 223 | if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) { |
ca10b5ee | 224 | dist_keys = SMP_DIST_ENC_KEY; |
54790f73 | 225 | authreq |= SMP_AUTH_BONDING; |
2b64d153 BG |
226 | } else { |
227 | authreq &= ~SMP_AUTH_BONDING; | |
54790f73 VCG |
228 | } |
229 | ||
230 | if (rsp == NULL) { | |
231 | req->io_capability = conn->hcon->io_capability; | |
232 | req->oob_flag = SMP_OOB_NOT_PRESENT; | |
233 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
2b64d153 | 234 | req->init_key_dist = 0; |
54790f73 | 235 | req->resp_key_dist = dist_keys; |
065a13e2 | 236 | req->auth_req = (authreq & AUTH_REQ_MASK); |
54790f73 VCG |
237 | return; |
238 | } | |
239 | ||
240 | rsp->io_capability = conn->hcon->io_capability; | |
241 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; | |
242 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
2b64d153 | 243 | rsp->init_key_dist = 0; |
54790f73 | 244 | rsp->resp_key_dist = req->resp_key_dist & dist_keys; |
065a13e2 | 245 | rsp->auth_req = (authreq & AUTH_REQ_MASK); |
b8e66eac VCG |
246 | } |
247 | ||
3158c50c VCG |
248 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
249 | { | |
1c1def09 VCG |
250 | struct smp_chan *smp = conn->smp_chan; |
251 | ||
3158c50c VCG |
252 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
253 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) | |
254 | return SMP_ENC_KEY_SIZE; | |
255 | ||
f7aa611a | 256 | smp->enc_key_size = max_key_size; |
3158c50c VCG |
257 | |
258 | return 0; | |
259 | } | |
260 | ||
4f957a76 BG |
261 | static void smp_failure(struct l2cap_conn *conn, u8 reason, u8 send) |
262 | { | |
bab73cb6 JH |
263 | struct hci_conn *hcon = conn->hcon; |
264 | ||
4f957a76 BG |
265 | if (send) |
266 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), | |
267 | &reason); | |
268 | ||
ce39fb4e MH |
269 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
270 | mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type, | |
271 | HCI_ERROR_AUTH_FAILURE); | |
f1c09c07 | 272 | |
61a0cfb0 AG |
273 | cancel_delayed_work_sync(&conn->security_timer); |
274 | ||
ce39fb4e | 275 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
f1c09c07 | 276 | smp_chan_destroy(conn); |
4f957a76 BG |
277 | } |
278 | ||
2b64d153 BG |
279 | #define JUST_WORKS 0x00 |
280 | #define JUST_CFM 0x01 | |
281 | #define REQ_PASSKEY 0x02 | |
282 | #define CFM_PASSKEY 0x03 | |
283 | #define REQ_OOB 0x04 | |
284 | #define OVERLAP 0xFF | |
285 | ||
286 | static const u8 gen_method[5][5] = { | |
287 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
288 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
289 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, | |
290 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, | |
291 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, | |
292 | }; | |
293 | ||
294 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, | |
295 | u8 local_io, u8 remote_io) | |
296 | { | |
297 | struct hci_conn *hcon = conn->hcon; | |
298 | struct smp_chan *smp = conn->smp_chan; | |
299 | u8 method; | |
300 | u32 passkey = 0; | |
301 | int ret = 0; | |
302 | ||
303 | /* Initialize key for JUST WORKS */ | |
304 | memset(smp->tk, 0, sizeof(smp->tk)); | |
305 | clear_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); | |
306 | ||
307 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); | |
308 | ||
309 | /* If neither side wants MITM, use JUST WORKS */ | |
310 | /* If either side has unknown io_caps, use JUST WORKS */ | |
311 | /* Otherwise, look up method from the table */ | |
312 | if (!(auth & SMP_AUTH_MITM) || | |
313 | local_io > SMP_IO_KEYBOARD_DISPLAY || | |
314 | remote_io > SMP_IO_KEYBOARD_DISPLAY) | |
315 | method = JUST_WORKS; | |
316 | else | |
b3ff53ff | 317 | method = gen_method[remote_io][local_io]; |
2b64d153 BG |
318 | |
319 | /* If not bonding, don't ask user to confirm a Zero TK */ | |
320 | if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM) | |
321 | method = JUST_WORKS; | |
322 | ||
323 | /* If Just Works, Continue with Zero TK */ | |
324 | if (method == JUST_WORKS) { | |
325 | set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); | |
326 | return 0; | |
327 | } | |
328 | ||
329 | /* Not Just Works/Confirm results in MITM Authentication */ | |
330 | if (method != JUST_CFM) | |
331 | set_bit(SMP_FLAG_MITM_AUTH, &smp->smp_flags); | |
332 | ||
333 | /* If both devices have Keyoard-Display I/O, the master | |
334 | * Confirms and the slave Enters the passkey. | |
335 | */ | |
336 | if (method == OVERLAP) { | |
337 | if (hcon->link_mode & HCI_LM_MASTER) | |
338 | method = CFM_PASSKEY; | |
339 | else | |
340 | method = REQ_PASSKEY; | |
341 | } | |
342 | ||
343 | /* Generate random passkey. Not valid until confirmed. */ | |
344 | if (method == CFM_PASSKEY) { | |
345 | u8 key[16]; | |
346 | ||
347 | memset(key, 0, sizeof(key)); | |
348 | get_random_bytes(&passkey, sizeof(passkey)); | |
349 | passkey %= 1000000; | |
350 | put_unaligned_le32(passkey, key); | |
351 | swap128(key, smp->tk); | |
352 | BT_DBG("PassKey: %d", passkey); | |
353 | } | |
354 | ||
355 | hci_dev_lock(hcon->hdev); | |
356 | ||
357 | if (method == REQ_PASSKEY) | |
ce39fb4e | 358 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
272d90df | 359 | hcon->type, hcon->dst_type); |
2b64d153 | 360 | else |
ce39fb4e | 361 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, |
272d90df | 362 | hcon->type, hcon->dst_type, |
2b64d153 BG |
363 | cpu_to_le32(passkey), 0); |
364 | ||
365 | hci_dev_unlock(hcon->hdev); | |
366 | ||
367 | return ret; | |
368 | } | |
369 | ||
8aab4757 VCG |
370 | static void confirm_work(struct work_struct *work) |
371 | { | |
372 | struct smp_chan *smp = container_of(work, struct smp_chan, confirm); | |
373 | struct l2cap_conn *conn = smp->conn; | |
374 | struct crypto_blkcipher *tfm; | |
375 | struct smp_cmd_pairing_confirm cp; | |
376 | int ret; | |
377 | u8 res[16], reason; | |
378 | ||
379 | BT_DBG("conn %p", conn); | |
380 | ||
381 | tfm = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); | |
382 | if (IS_ERR(tfm)) { | |
383 | reason = SMP_UNSPECIFIED; | |
384 | goto error; | |
385 | } | |
386 | ||
387 | smp->tfm = tfm; | |
388 | ||
389 | if (conn->hcon->out) | |
390 | ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 0, | |
ce39fb4e MH |
391 | &conn->hcon->hdev->bdaddr, conn->hcon->dst_type, |
392 | &conn->hcon->dst, res); | |
8aab4757 VCG |
393 | else |
394 | ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, | |
ce39fb4e MH |
395 | conn->hcon->dst_type, &conn->hcon->dst, 0, |
396 | &conn->hcon->hdev->bdaddr, res); | |
8aab4757 VCG |
397 | if (ret) { |
398 | reason = SMP_UNSPECIFIED; | |
399 | goto error; | |
400 | } | |
401 | ||
2b64d153 BG |
402 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); |
403 | ||
8aab4757 VCG |
404 | swap128(res, cp.confirm_val); |
405 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); | |
406 | ||
407 | return; | |
408 | ||
409 | error: | |
4f957a76 | 410 | smp_failure(conn, reason, 1); |
8aab4757 VCG |
411 | } |
412 | ||
413 | static void random_work(struct work_struct *work) | |
414 | { | |
415 | struct smp_chan *smp = container_of(work, struct smp_chan, random); | |
416 | struct l2cap_conn *conn = smp->conn; | |
417 | struct hci_conn *hcon = conn->hcon; | |
418 | struct crypto_blkcipher *tfm = smp->tfm; | |
419 | u8 reason, confirm[16], res[16], key[16]; | |
420 | int ret; | |
421 | ||
422 | if (IS_ERR_OR_NULL(tfm)) { | |
423 | reason = SMP_UNSPECIFIED; | |
424 | goto error; | |
425 | } | |
426 | ||
427 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); | |
428 | ||
429 | if (hcon->out) | |
430 | ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, 0, | |
ce39fb4e MH |
431 | &hcon->hdev->bdaddr, hcon->dst_type, &hcon->dst, |
432 | res); | |
8aab4757 VCG |
433 | else |
434 | ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, | |
ce39fb4e MH |
435 | hcon->dst_type, &hcon->dst, 0, &hcon->hdev->bdaddr, |
436 | res); | |
8aab4757 VCG |
437 | if (ret) { |
438 | reason = SMP_UNSPECIFIED; | |
439 | goto error; | |
440 | } | |
441 | ||
442 | swap128(res, confirm); | |
443 | ||
444 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { | |
445 | BT_ERR("Pairing failed (confirmation values mismatch)"); | |
446 | reason = SMP_CONFIRM_FAILED; | |
447 | goto error; | |
448 | } | |
449 | ||
450 | if (hcon->out) { | |
451 | u8 stk[16], rand[8]; | |
452 | __le16 ediv; | |
453 | ||
454 | memset(rand, 0, sizeof(rand)); | |
455 | ediv = 0; | |
456 | ||
457 | smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key); | |
458 | swap128(key, stk); | |
459 | ||
f7aa611a | 460 | memset(stk + smp->enc_key_size, 0, |
04124681 | 461 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 462 | |
51a8efd7 | 463 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) { |
8aab4757 VCG |
464 | reason = SMP_UNSPECIFIED; |
465 | goto error; | |
466 | } | |
467 | ||
468 | hci_le_start_enc(hcon, ediv, rand, stk); | |
f7aa611a | 469 | hcon->enc_key_size = smp->enc_key_size; |
8aab4757 VCG |
470 | } else { |
471 | u8 stk[16], r[16], rand[8]; | |
472 | __le16 ediv; | |
473 | ||
474 | memset(rand, 0, sizeof(rand)); | |
475 | ediv = 0; | |
476 | ||
477 | swap128(smp->prnd, r); | |
478 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r); | |
479 | ||
480 | smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, key); | |
481 | swap128(key, stk); | |
482 | ||
f7aa611a VCG |
483 | memset(stk + smp->enc_key_size, 0, |
484 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); | |
8aab4757 | 485 | |
ce39fb4e | 486 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
04124681 GP |
487 | HCI_SMP_STK_SLAVE, 0, 0, stk, smp->enc_key_size, |
488 | ediv, rand); | |
8aab4757 VCG |
489 | } |
490 | ||
491 | return; | |
492 | ||
493 | error: | |
4f957a76 | 494 | smp_failure(conn, reason, 1); |
8aab4757 VCG |
495 | } |
496 | ||
497 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) | |
498 | { | |
499 | struct smp_chan *smp; | |
500 | ||
501 | smp = kzalloc(sizeof(struct smp_chan), GFP_ATOMIC); | |
502 | if (!smp) | |
503 | return NULL; | |
504 | ||
505 | INIT_WORK(&smp->confirm, confirm_work); | |
506 | INIT_WORK(&smp->random, random_work); | |
507 | ||
508 | smp->conn = conn; | |
509 | conn->smp_chan = smp; | |
2b64d153 | 510 | conn->hcon->smp_conn = conn; |
8aab4757 VCG |
511 | |
512 | hci_conn_hold(conn->hcon); | |
513 | ||
514 | return smp; | |
515 | } | |
516 | ||
517 | void smp_chan_destroy(struct l2cap_conn *conn) | |
518 | { | |
c8eb9690 BG |
519 | struct smp_chan *smp = conn->smp_chan; |
520 | ||
f1c09c07 | 521 | BUG_ON(!smp); |
c8eb9690 BG |
522 | |
523 | if (smp->tfm) | |
524 | crypto_free_blkcipher(smp->tfm); | |
525 | ||
526 | kfree(smp); | |
527 | conn->smp_chan = NULL; | |
2b64d153 | 528 | conn->hcon->smp_conn = NULL; |
76a68ba0 | 529 | hci_conn_drop(conn->hcon); |
8aab4757 VCG |
530 | } |
531 | ||
2b64d153 BG |
532 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
533 | { | |
534 | struct l2cap_conn *conn = hcon->smp_conn; | |
535 | struct smp_chan *smp; | |
536 | u32 value; | |
537 | u8 key[16]; | |
538 | ||
539 | BT_DBG(""); | |
540 | ||
541 | if (!conn) | |
542 | return -ENOTCONN; | |
543 | ||
544 | smp = conn->smp_chan; | |
545 | ||
546 | switch (mgmt_op) { | |
547 | case MGMT_OP_USER_PASSKEY_REPLY: | |
548 | value = le32_to_cpu(passkey); | |
549 | memset(key, 0, sizeof(key)); | |
550 | BT_DBG("PassKey: %d", value); | |
551 | put_unaligned_le32(value, key); | |
552 | swap128(key, smp->tk); | |
553 | /* Fall Through */ | |
554 | case MGMT_OP_USER_CONFIRM_REPLY: | |
555 | set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); | |
556 | break; | |
557 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: | |
558 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: | |
559 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED, 1); | |
560 | return 0; | |
561 | default: | |
562 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED, 1); | |
563 | return -EOPNOTSUPP; | |
564 | } | |
565 | ||
566 | /* If it is our turn to send Pairing Confirm, do so now */ | |
567 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags)) | |
568 | queue_work(hcon->hdev->workqueue, &smp->confirm); | |
569 | ||
570 | return 0; | |
571 | } | |
572 | ||
da85e5e5 | 573 | static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 574 | { |
3158c50c | 575 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
8aab4757 | 576 | struct smp_chan *smp; |
3158c50c | 577 | u8 key_size; |
2b64d153 | 578 | u8 auth = SMP_AUTH_NONE; |
8aab4757 | 579 | int ret; |
88ba43b6 AB |
580 | |
581 | BT_DBG("conn %p", conn); | |
582 | ||
2b64d153 BG |
583 | if (conn->hcon->link_mode & HCI_LM_MASTER) |
584 | return SMP_CMD_NOTSUPP; | |
585 | ||
51a8efd7 | 586 | if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) |
8aab4757 | 587 | smp = smp_chan_create(conn); |
d08fd0e7 AE |
588 | else |
589 | smp = conn->smp_chan; | |
8aab4757 | 590 | |
d08fd0e7 AE |
591 | if (!smp) |
592 | return SMP_UNSPECIFIED; | |
d26a2345 | 593 | |
1c1def09 VCG |
594 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
595 | memcpy(&smp->preq[1], req, sizeof(*req)); | |
3158c50c | 596 | skb_pull(skb, sizeof(*req)); |
88ba43b6 | 597 | |
2b64d153 BG |
598 | /* We didn't start the pairing, so match remote */ |
599 | if (req->auth_req & SMP_AUTH_BONDING) | |
600 | auth = req->auth_req; | |
da85e5e5 | 601 | |
fdde0a26 IY |
602 | conn->hcon->pending_sec_level = authreq_to_seclevel(auth); |
603 | ||
2b64d153 | 604 | build_pairing_cmd(conn, req, &rsp, auth); |
3158c50c VCG |
605 | |
606 | key_size = min(req->max_key_size, rsp.max_key_size); | |
607 | if (check_enc_key_size(conn, key_size)) | |
608 | return SMP_ENC_KEY_SIZE; | |
88ba43b6 | 609 | |
8aab4757 VCG |
610 | ret = smp_rand(smp->prnd); |
611 | if (ret) | |
612 | return SMP_UNSPECIFIED; | |
613 | ||
1c1def09 VCG |
614 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
615 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); | |
f01ead31 | 616 | |
3158c50c | 617 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
da85e5e5 | 618 | |
2b64d153 BG |
619 | /* Request setup of TK */ |
620 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); | |
621 | if (ret) | |
622 | return SMP_UNSPECIFIED; | |
623 | ||
da85e5e5 | 624 | return 0; |
88ba43b6 AB |
625 | } |
626 | ||
da85e5e5 | 627 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 628 | { |
3158c50c | 629 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
1c1def09 | 630 | struct smp_chan *smp = conn->smp_chan; |
8aab4757 | 631 | struct hci_dev *hdev = conn->hcon->hdev; |
2b64d153 | 632 | u8 key_size, auth = SMP_AUTH_NONE; |
7d24ddcc | 633 | int ret; |
88ba43b6 AB |
634 | |
635 | BT_DBG("conn %p", conn); | |
636 | ||
2b64d153 BG |
637 | if (!(conn->hcon->link_mode & HCI_LM_MASTER)) |
638 | return SMP_CMD_NOTSUPP; | |
639 | ||
3158c50c VCG |
640 | skb_pull(skb, sizeof(*rsp)); |
641 | ||
1c1def09 | 642 | req = (void *) &smp->preq[1]; |
da85e5e5 | 643 | |
3158c50c VCG |
644 | key_size = min(req->max_key_size, rsp->max_key_size); |
645 | if (check_enc_key_size(conn, key_size)) | |
646 | return SMP_ENC_KEY_SIZE; | |
647 | ||
1c1def09 | 648 | ret = smp_rand(smp->prnd); |
7d24ddcc | 649 | if (ret) |
da85e5e5 | 650 | return SMP_UNSPECIFIED; |
7d24ddcc | 651 | |
8aab4757 VCG |
652 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
653 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); | |
7d24ddcc | 654 | |
2b64d153 BG |
655 | if ((req->auth_req & SMP_AUTH_BONDING) && |
656 | (rsp->auth_req & SMP_AUTH_BONDING)) | |
657 | auth = SMP_AUTH_BONDING; | |
658 | ||
659 | auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM; | |
660 | ||
476585ec | 661 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
2b64d153 BG |
662 | if (ret) |
663 | return SMP_UNSPECIFIED; | |
664 | ||
665 | set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); | |
666 | ||
667 | /* Can't compose response until we have been confirmed */ | |
668 | if (!test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) | |
669 | return 0; | |
670 | ||
8aab4757 | 671 | queue_work(hdev->workqueue, &smp->confirm); |
da85e5e5 VCG |
672 | |
673 | return 0; | |
88ba43b6 AB |
674 | } |
675 | ||
da85e5e5 | 676 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 677 | { |
1c1def09 | 678 | struct smp_chan *smp = conn->smp_chan; |
8aab4757 | 679 | struct hci_dev *hdev = conn->hcon->hdev; |
7d24ddcc | 680 | |
88ba43b6 AB |
681 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
682 | ||
1c1def09 VCG |
683 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
684 | skb_pull(skb, sizeof(smp->pcnf)); | |
88ba43b6 | 685 | |
7d24ddcc AB |
686 | if (conn->hcon->out) { |
687 | u8 random[16]; | |
88ba43b6 | 688 | |
1c1def09 | 689 | swap128(smp->prnd, random); |
88ba43b6 | 690 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random), |
7d24ddcc | 691 | random); |
2b64d153 | 692 | } else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) { |
8aab4757 | 693 | queue_work(hdev->workqueue, &smp->confirm); |
2b64d153 BG |
694 | } else { |
695 | set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); | |
88ba43b6 | 696 | } |
da85e5e5 VCG |
697 | |
698 | return 0; | |
88ba43b6 AB |
699 | } |
700 | ||
da85e5e5 | 701 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 702 | { |
1c1def09 | 703 | struct smp_chan *smp = conn->smp_chan; |
8aab4757 | 704 | struct hci_dev *hdev = conn->hcon->hdev; |
7d24ddcc | 705 | |
8aab4757 | 706 | BT_DBG("conn %p", conn); |
3158c50c | 707 | |
8aab4757 VCG |
708 | swap128(skb->data, smp->rrnd); |
709 | skb_pull(skb, sizeof(smp->rrnd)); | |
e7e62c85 | 710 | |
8aab4757 | 711 | queue_work(hdev->workqueue, &smp->random); |
da85e5e5 VCG |
712 | |
713 | return 0; | |
88ba43b6 AB |
714 | } |
715 | ||
4dab7864 | 716 | static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
988c5997 | 717 | { |
c9839a11 | 718 | struct smp_ltk *key; |
988c5997 VCG |
719 | struct hci_conn *hcon = conn->hcon; |
720 | ||
ce39fb4e | 721 | key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type); |
988c5997 VCG |
722 | if (!key) |
723 | return 0; | |
724 | ||
4dab7864 JH |
725 | if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated) |
726 | return 0; | |
727 | ||
51a8efd7 | 728 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
988c5997 VCG |
729 | return 1; |
730 | ||
c9839a11 VCG |
731 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
732 | hcon->enc_key_size = key->enc_size; | |
988c5997 VCG |
733 | |
734 | return 1; | |
735 | ||
736 | } | |
da85e5e5 | 737 | static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 AB |
738 | { |
739 | struct smp_cmd_security_req *rp = (void *) skb->data; | |
740 | struct smp_cmd_pairing cp; | |
f1cb9af5 | 741 | struct hci_conn *hcon = conn->hcon; |
8aab4757 | 742 | struct smp_chan *smp; |
88ba43b6 AB |
743 | |
744 | BT_DBG("conn %p", conn); | |
745 | ||
2b64d153 | 746 | hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req); |
feb45eb5 | 747 | |
4dab7864 | 748 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
988c5997 VCG |
749 | return 0; |
750 | ||
51a8efd7 | 751 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
da85e5e5 | 752 | return 0; |
f1cb9af5 | 753 | |
8aab4757 | 754 | smp = smp_chan_create(conn); |
d26a2345 | 755 | |
88ba43b6 | 756 | skb_pull(skb, sizeof(*rp)); |
88ba43b6 | 757 | |
da85e5e5 | 758 | memset(&cp, 0, sizeof(cp)); |
54790f73 | 759 | build_pairing_cmd(conn, &cp, NULL, rp->auth_req); |
88ba43b6 | 760 | |
1c1def09 VCG |
761 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
762 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 763 | |
88ba43b6 | 764 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
f1cb9af5 | 765 | |
da85e5e5 | 766 | return 0; |
88ba43b6 AB |
767 | } |
768 | ||
cc110922 | 769 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
eb492e01 | 770 | { |
cc110922 | 771 | struct l2cap_conn *conn = hcon->l2cap_data; |
1c1def09 | 772 | struct smp_chan *smp = conn->smp_chan; |
2b64d153 | 773 | __u8 authreq; |
eb492e01 | 774 | |
3a0259bb VCG |
775 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
776 | ||
757aee0f | 777 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
2e65c9d2 AG |
778 | return 1; |
779 | ||
f1cb9af5 VCG |
780 | if (sec_level == BT_SECURITY_LOW) |
781 | return 1; | |
eb492e01 | 782 | |
f1cb9af5 | 783 | if (hcon->sec_level >= sec_level) |
eb492e01 | 784 | return 1; |
f1cb9af5 | 785 | |
988c5997 | 786 | if (hcon->link_mode & HCI_LM_MASTER) |
4dab7864 | 787 | if (smp_ltk_encrypt(conn, sec_level)) |
02bc7455 | 788 | goto done; |
d26a2345 | 789 | |
51a8efd7 | 790 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
d26a2345 VCG |
791 | return 0; |
792 | ||
8aab4757 | 793 | smp = smp_chan_create(conn); |
2b64d153 BG |
794 | if (!smp) |
795 | return 1; | |
796 | ||
797 | authreq = seclevel_to_authreq(sec_level); | |
d26a2345 | 798 | |
d26a2345 VCG |
799 | if (hcon->link_mode & HCI_LM_MASTER) { |
800 | struct smp_cmd_pairing cp; | |
f01ead31 | 801 | |
2b64d153 | 802 | build_pairing_cmd(conn, &cp, NULL, authreq); |
1c1def09 VCG |
803 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
804 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 805 | |
eb492e01 AB |
806 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
807 | } else { | |
808 | struct smp_cmd_security_req cp; | |
2b64d153 | 809 | cp.auth_req = authreq; |
eb492e01 AB |
810 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
811 | } | |
812 | ||
02bc7455 | 813 | done: |
f1cb9af5 | 814 | hcon->pending_sec_level = sec_level; |
f1cb9af5 | 815 | |
eb492e01 AB |
816 | return 0; |
817 | } | |
818 | ||
7034b911 VCG |
819 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
820 | { | |
16b90839 | 821 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
1c1def09 | 822 | struct smp_chan *smp = conn->smp_chan; |
16b90839 VCG |
823 | |
824 | skb_pull(skb, sizeof(*rp)); | |
825 | ||
1c1def09 | 826 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
16b90839 | 827 | |
7034b911 VCG |
828 | return 0; |
829 | } | |
830 | ||
831 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) | |
832 | { | |
16b90839 | 833 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
1c1def09 | 834 | struct smp_chan *smp = conn->smp_chan; |
c9839a11 VCG |
835 | struct hci_dev *hdev = conn->hcon->hdev; |
836 | struct hci_conn *hcon = conn->hcon; | |
837 | u8 authenticated; | |
16b90839 VCG |
838 | |
839 | skb_pull(skb, sizeof(*rp)); | |
7034b911 | 840 | |
c9839a11 | 841 | hci_dev_lock(hdev); |
ce39fb4e MH |
842 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
843 | hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, HCI_SMP_LTK, 1, | |
844 | authenticated, smp->tk, smp->enc_key_size, | |
04124681 | 845 | rp->ediv, rp->rand); |
7034b911 | 846 | smp_distribute_keys(conn, 1); |
c9839a11 | 847 | hci_dev_unlock(hdev); |
7034b911 VCG |
848 | |
849 | return 0; | |
850 | } | |
851 | ||
eb492e01 AB |
852 | int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) |
853 | { | |
7b9899db | 854 | struct hci_conn *hcon = conn->hcon; |
92381f5c | 855 | __u8 code, reason; |
eb492e01 AB |
856 | int err = 0; |
857 | ||
7b9899db MH |
858 | if (hcon->type != LE_LINK) { |
859 | kfree_skb(skb); | |
860 | return -ENOTSUPP; | |
861 | } | |
862 | ||
92381f5c MH |
863 | if (skb->len < 1) { |
864 | kfree_skb(skb); | |
865 | return -EILSEQ; | |
866 | } | |
867 | ||
757aee0f | 868 | if (!test_bit(HCI_LE_ENABLED, &conn->hcon->hdev->dev_flags)) { |
2e65c9d2 AG |
869 | err = -ENOTSUPP; |
870 | reason = SMP_PAIRING_NOTSUPP; | |
871 | goto done; | |
872 | } | |
873 | ||
92381f5c | 874 | code = skb->data[0]; |
eb492e01 AB |
875 | skb_pull(skb, sizeof(code)); |
876 | ||
8cf9fa12 JH |
877 | /* |
878 | * The SMP context must be initialized for all other PDUs except | |
879 | * pairing and security requests. If we get any other PDU when | |
880 | * not initialized simply disconnect (done if this function | |
881 | * returns an error). | |
882 | */ | |
883 | if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ && | |
884 | !conn->smp_chan) { | |
885 | BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code); | |
886 | kfree_skb(skb); | |
887 | return -ENOTSUPP; | |
888 | } | |
889 | ||
eb492e01 AB |
890 | switch (code) { |
891 | case SMP_CMD_PAIRING_REQ: | |
da85e5e5 | 892 | reason = smp_cmd_pairing_req(conn, skb); |
eb492e01 AB |
893 | break; |
894 | ||
895 | case SMP_CMD_PAIRING_FAIL: | |
4f957a76 | 896 | smp_failure(conn, skb->data[0], 0); |
da85e5e5 VCG |
897 | reason = 0; |
898 | err = -EPERM; | |
eb492e01 AB |
899 | break; |
900 | ||
901 | case SMP_CMD_PAIRING_RSP: | |
da85e5e5 | 902 | reason = smp_cmd_pairing_rsp(conn, skb); |
88ba43b6 AB |
903 | break; |
904 | ||
905 | case SMP_CMD_SECURITY_REQ: | |
da85e5e5 | 906 | reason = smp_cmd_security_req(conn, skb); |
88ba43b6 AB |
907 | break; |
908 | ||
eb492e01 | 909 | case SMP_CMD_PAIRING_CONFIRM: |
da85e5e5 | 910 | reason = smp_cmd_pairing_confirm(conn, skb); |
88ba43b6 AB |
911 | break; |
912 | ||
eb492e01 | 913 | case SMP_CMD_PAIRING_RANDOM: |
da85e5e5 | 914 | reason = smp_cmd_pairing_random(conn, skb); |
88ba43b6 AB |
915 | break; |
916 | ||
eb492e01 | 917 | case SMP_CMD_ENCRYPT_INFO: |
7034b911 VCG |
918 | reason = smp_cmd_encrypt_info(conn, skb); |
919 | break; | |
920 | ||
eb492e01 | 921 | case SMP_CMD_MASTER_IDENT: |
7034b911 VCG |
922 | reason = smp_cmd_master_ident(conn, skb); |
923 | break; | |
924 | ||
eb492e01 AB |
925 | case SMP_CMD_IDENT_INFO: |
926 | case SMP_CMD_IDENT_ADDR_INFO: | |
927 | case SMP_CMD_SIGN_INFO: | |
7034b911 VCG |
928 | /* Just ignored */ |
929 | reason = 0; | |
930 | break; | |
931 | ||
eb492e01 AB |
932 | default: |
933 | BT_DBG("Unknown command code 0x%2.2x", code); | |
934 | ||
935 | reason = SMP_CMD_NOTSUPP; | |
eb492e01 | 936 | err = -EOPNOTSUPP; |
3a0259bb | 937 | goto done; |
eb492e01 AB |
938 | } |
939 | ||
3a0259bb VCG |
940 | done: |
941 | if (reason) | |
4f957a76 | 942 | smp_failure(conn, reason, 1); |
3a0259bb | 943 | |
eb492e01 AB |
944 | kfree_skb(skb); |
945 | return err; | |
946 | } | |
7034b911 VCG |
947 | |
948 | int smp_distribute_keys(struct l2cap_conn *conn, __u8 force) | |
949 | { | |
950 | struct smp_cmd_pairing *req, *rsp; | |
1c1def09 | 951 | struct smp_chan *smp = conn->smp_chan; |
7034b911 VCG |
952 | __u8 *keydist; |
953 | ||
954 | BT_DBG("conn %p force %d", conn, force); | |
955 | ||
51a8efd7 | 956 | if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) |
d26a2345 VCG |
957 | return 0; |
958 | ||
1c1def09 | 959 | rsp = (void *) &smp->prsp[1]; |
7034b911 VCG |
960 | |
961 | /* The responder sends its keys first */ | |
962 | if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07)) | |
963 | return 0; | |
964 | ||
1c1def09 | 965 | req = (void *) &smp->preq[1]; |
7034b911 VCG |
966 | |
967 | if (conn->hcon->out) { | |
968 | keydist = &rsp->init_key_dist; | |
969 | *keydist &= req->init_key_dist; | |
970 | } else { | |
971 | keydist = &rsp->resp_key_dist; | |
972 | *keydist &= req->resp_key_dist; | |
973 | } | |
974 | ||
975 | ||
976 | BT_DBG("keydist 0x%x", *keydist); | |
977 | ||
978 | if (*keydist & SMP_DIST_ENC_KEY) { | |
979 | struct smp_cmd_encrypt_info enc; | |
980 | struct smp_cmd_master_ident ident; | |
c9839a11 VCG |
981 | struct hci_conn *hcon = conn->hcon; |
982 | u8 authenticated; | |
7034b911 VCG |
983 | __le16 ediv; |
984 | ||
985 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); | |
986 | get_random_bytes(&ediv, sizeof(ediv)); | |
987 | get_random_bytes(ident.rand, sizeof(ident.rand)); | |
988 | ||
989 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); | |
990 | ||
c9839a11 | 991 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; |
ce39fb4e | 992 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
04124681 GP |
993 | HCI_SMP_LTK_SLAVE, 1, authenticated, |
994 | enc.ltk, smp->enc_key_size, ediv, ident.rand); | |
16b90839 | 995 | |
58115373 | 996 | ident.ediv = ediv; |
7034b911 VCG |
997 | |
998 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); | |
999 | ||
1000 | *keydist &= ~SMP_DIST_ENC_KEY; | |
1001 | } | |
1002 | ||
1003 | if (*keydist & SMP_DIST_ID_KEY) { | |
1004 | struct smp_cmd_ident_addr_info addrinfo; | |
1005 | struct smp_cmd_ident_info idinfo; | |
1006 | ||
1007 | /* Send a dummy key */ | |
1008 | get_random_bytes(idinfo.irk, sizeof(idinfo.irk)); | |
1009 | ||
1010 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); | |
1011 | ||
1012 | /* Just public address */ | |
1013 | memset(&addrinfo, 0, sizeof(addrinfo)); | |
ce39fb4e | 1014 | bacpy(&addrinfo.bdaddr, &conn->hcon->hdev->bdaddr); |
7034b911 VCG |
1015 | |
1016 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), | |
1017 | &addrinfo); | |
1018 | ||
1019 | *keydist &= ~SMP_DIST_ID_KEY; | |
1020 | } | |
1021 | ||
1022 | if (*keydist & SMP_DIST_SIGN) { | |
1023 | struct smp_cmd_sign_info sign; | |
1024 | ||
1025 | /* Send a dummy key */ | |
1026 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); | |
1027 | ||
1028 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); | |
1029 | ||
1030 | *keydist &= ~SMP_DIST_SIGN; | |
1031 | } | |
1032 | ||
d26a2345 | 1033 | if (conn->hcon->out || force) { |
51a8efd7 | 1034 | clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags); |
6c9d42a1 | 1035 | cancel_delayed_work_sync(&conn->security_timer); |
8aab4757 | 1036 | smp_chan_destroy(conn); |
d26a2345 VCG |
1037 | } |
1038 | ||
7034b911 VCG |
1039 | return 0; |
1040 | } |