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 | ||
533e35d4 JH |
38 | enum { |
39 | SMP_FLAG_TK_VALID, | |
40 | SMP_FLAG_CFM_PENDING, | |
41 | SMP_FLAG_MITM_AUTH, | |
42 | SMP_FLAG_COMPLETE, | |
43 | SMP_FLAG_INITIATOR, | |
44 | }; | |
4bc58f51 JH |
45 | |
46 | struct smp_chan { | |
47 | struct l2cap_conn *conn; | |
48 | u8 preq[7]; /* SMP Pairing Request */ | |
49 | u8 prsp[7]; /* SMP Pairing Response */ | |
50 | u8 prnd[16]; /* SMP Pairing Random (local) */ | |
51 | u8 rrnd[16]; /* SMP Pairing Random (remote) */ | |
52 | u8 pcnf[16]; /* SMP Pairing Confirm */ | |
53 | u8 tk[16]; /* SMP Temporary Key */ | |
54 | u8 enc_key_size; | |
55 | u8 remote_key_dist; | |
56 | bdaddr_t id_addr; | |
57 | u8 id_addr_type; | |
58 | u8 irk[16]; | |
59 | struct smp_csrk *csrk; | |
60 | struct smp_csrk *slave_csrk; | |
61 | struct smp_ltk *ltk; | |
62 | struct smp_ltk *slave_ltk; | |
63 | struct smp_irk *remote_irk; | |
4a74d658 | 64 | unsigned long flags; |
4bc58f51 JH |
65 | }; |
66 | ||
66bed1a2 | 67 | static inline void swap128(const u8 src[16], u8 dst[16]) |
d22ef0bc AB |
68 | { |
69 | int i; | |
70 | for (i = 0; i < 16; i++) | |
71 | dst[15 - i] = src[i]; | |
72 | } | |
73 | ||
66bed1a2 | 74 | static inline void swap56(const u8 src[7], u8 dst[7]) |
d22ef0bc AB |
75 | { |
76 | int i; | |
77 | for (i = 0; i < 7; i++) | |
78 | dst[6 - i] = src[i]; | |
79 | } | |
80 | ||
81 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) | |
82 | { | |
83 | struct blkcipher_desc desc; | |
84 | struct scatterlist sg; | |
943a732a | 85 | uint8_t tmp[16], data[16]; |
201a5929 | 86 | int err; |
d22ef0bc AB |
87 | |
88 | if (tfm == NULL) { | |
89 | BT_ERR("tfm %p", tfm); | |
90 | return -EINVAL; | |
91 | } | |
92 | ||
93 | desc.tfm = tfm; | |
94 | desc.flags = 0; | |
95 | ||
943a732a JH |
96 | /* The most significant octet of key corresponds to k[0] */ |
97 | swap128(k, tmp); | |
98 | ||
99 | err = crypto_blkcipher_setkey(tfm, tmp, 16); | |
d22ef0bc AB |
100 | if (err) { |
101 | BT_ERR("cipher setkey failed: %d", err); | |
102 | return err; | |
103 | } | |
104 | ||
943a732a JH |
105 | /* Most significant octet of plaintextData corresponds to data[0] */ |
106 | swap128(r, data); | |
107 | ||
108 | sg_init_one(&sg, data, 16); | |
d22ef0bc | 109 | |
d22ef0bc AB |
110 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); |
111 | if (err) | |
112 | BT_ERR("Encrypt data error %d", err); | |
113 | ||
943a732a JH |
114 | /* Most significant octet of encryptedData corresponds to data[0] */ |
115 | swap128(data, r); | |
116 | ||
d22ef0bc AB |
117 | return err; |
118 | } | |
119 | ||
60478054 JH |
120 | static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3]) |
121 | { | |
943a732a | 122 | u8 _res[16]; |
60478054 JH |
123 | int err; |
124 | ||
125 | /* r' = padding || r */ | |
943a732a JH |
126 | memcpy(_res, r, 3); |
127 | memset(_res + 3, 0, 13); | |
60478054 | 128 | |
943a732a | 129 | err = smp_e(tfm, irk, _res); |
60478054 JH |
130 | if (err) { |
131 | BT_ERR("Encrypt error"); | |
132 | return err; | |
133 | } | |
134 | ||
135 | /* The output of the random address function ah is: | |
136 | * ah(h, r) = e(k, r') mod 2^24 | |
137 | * The output of the security function e is then truncated to 24 bits | |
138 | * by taking the least significant 24 bits of the output of e as the | |
139 | * result of ah. | |
140 | */ | |
943a732a | 141 | memcpy(res, _res, 3); |
60478054 JH |
142 | |
143 | return 0; | |
144 | } | |
145 | ||
146 | bool smp_irk_matches(struct crypto_blkcipher *tfm, u8 irk[16], | |
147 | bdaddr_t *bdaddr) | |
148 | { | |
149 | u8 hash[3]; | |
150 | int err; | |
151 | ||
152 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); | |
153 | ||
154 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); | |
155 | if (err) | |
156 | return false; | |
157 | ||
158 | return !memcmp(bdaddr->b, hash, 3); | |
159 | } | |
160 | ||
b1e2b3ae JH |
161 | int smp_generate_rpa(struct crypto_blkcipher *tfm, u8 irk[16], bdaddr_t *rpa) |
162 | { | |
163 | int err; | |
164 | ||
165 | get_random_bytes(&rpa->b[3], 3); | |
166 | ||
167 | rpa->b[5] &= 0x3f; /* Clear two most significant bits */ | |
168 | rpa->b[5] |= 0x40; /* Set second most significant bit */ | |
169 | ||
170 | err = smp_ah(tfm, irk, &rpa->b[3], rpa->b); | |
171 | if (err < 0) | |
172 | return err; | |
173 | ||
174 | BT_DBG("RPA %pMR", rpa); | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
d22ef0bc | 179 | static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16], |
f1560463 MH |
180 | u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, |
181 | u8 _rat, bdaddr_t *ra, u8 res[16]) | |
d22ef0bc AB |
182 | { |
183 | u8 p1[16], p2[16]; | |
184 | int err; | |
185 | ||
186 | memset(p1, 0, 16); | |
187 | ||
188 | /* p1 = pres || preq || _rat || _iat */ | |
943a732a JH |
189 | p1[0] = _iat; |
190 | p1[1] = _rat; | |
191 | memcpy(p1 + 2, preq, 7); | |
192 | memcpy(p1 + 9, pres, 7); | |
d22ef0bc AB |
193 | |
194 | /* p2 = padding || ia || ra */ | |
943a732a JH |
195 | memcpy(p2, ra, 6); |
196 | memcpy(p2 + 6, ia, 6); | |
197 | memset(p2 + 12, 0, 4); | |
d22ef0bc AB |
198 | |
199 | /* res = r XOR p1 */ | |
200 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); | |
201 | ||
202 | /* res = e(k, res) */ | |
203 | err = smp_e(tfm, k, res); | |
204 | if (err) { | |
205 | BT_ERR("Encrypt data error"); | |
206 | return err; | |
207 | } | |
208 | ||
209 | /* res = res XOR p2 */ | |
210 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); | |
211 | ||
212 | /* res = e(k, res) */ | |
213 | err = smp_e(tfm, k, res); | |
214 | if (err) | |
215 | BT_ERR("Encrypt data error"); | |
216 | ||
217 | return err; | |
218 | } | |
219 | ||
f1560463 MH |
220 | static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], u8 r1[16], |
221 | u8 r2[16], u8 _r[16]) | |
d22ef0bc AB |
222 | { |
223 | int err; | |
224 | ||
225 | /* Just least significant octets from r1 and r2 are considered */ | |
943a732a JH |
226 | memcpy(_r, r2, 8); |
227 | memcpy(_r + 8, r1, 8); | |
d22ef0bc AB |
228 | |
229 | err = smp_e(tfm, k, _r); | |
230 | if (err) | |
231 | BT_ERR("Encrypt data error"); | |
232 | ||
233 | return err; | |
234 | } | |
235 | ||
eb492e01 | 236 | static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code, |
f1560463 | 237 | u16 dlen, void *data) |
eb492e01 AB |
238 | { |
239 | struct sk_buff *skb; | |
240 | struct l2cap_hdr *lh; | |
241 | int len; | |
242 | ||
243 | len = L2CAP_HDR_SIZE + sizeof(code) + dlen; | |
244 | ||
245 | if (len > conn->mtu) | |
246 | return NULL; | |
247 | ||
248 | skb = bt_skb_alloc(len, GFP_ATOMIC); | |
249 | if (!skb) | |
250 | return NULL; | |
251 | ||
252 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); | |
253 | lh->len = cpu_to_le16(sizeof(code) + dlen); | |
dcf4adbf | 254 | lh->cid = cpu_to_le16(L2CAP_CID_SMP); |
eb492e01 AB |
255 | |
256 | memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code)); | |
257 | ||
258 | memcpy(skb_put(skb, dlen), data, dlen); | |
259 | ||
260 | return skb; | |
261 | } | |
262 | ||
263 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) | |
264 | { | |
265 | struct sk_buff *skb = smp_build_cmd(conn, code, len, data); | |
266 | ||
267 | BT_DBG("code 0x%2.2x", code); | |
268 | ||
269 | if (!skb) | |
270 | return; | |
271 | ||
73d80deb LAD |
272 | skb->priority = HCI_PRIO_MAX; |
273 | hci_send_acl(conn->hchan, skb, 0); | |
e2dcd113 | 274 | |
6c9d42a1 | 275 | cancel_delayed_work_sync(&conn->security_timer); |
17b02e62 | 276 | schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT); |
eb492e01 AB |
277 | } |
278 | ||
2b64d153 BG |
279 | static __u8 authreq_to_seclevel(__u8 authreq) |
280 | { | |
281 | if (authreq & SMP_AUTH_MITM) | |
282 | return BT_SECURITY_HIGH; | |
283 | else | |
284 | return BT_SECURITY_MEDIUM; | |
285 | } | |
286 | ||
287 | static __u8 seclevel_to_authreq(__u8 sec_level) | |
288 | { | |
289 | switch (sec_level) { | |
290 | case BT_SECURITY_HIGH: | |
291 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; | |
292 | case BT_SECURITY_MEDIUM: | |
293 | return SMP_AUTH_BONDING; | |
294 | default: | |
295 | return SMP_AUTH_NONE; | |
296 | } | |
297 | } | |
298 | ||
b8e66eac | 299 | static void build_pairing_cmd(struct l2cap_conn *conn, |
f1560463 MH |
300 | struct smp_cmd_pairing *req, |
301 | struct smp_cmd_pairing *rsp, __u8 authreq) | |
b8e66eac | 302 | { |
fd349c02 JH |
303 | struct smp_chan *smp = conn->smp_chan; |
304 | struct hci_conn *hcon = conn->hcon; | |
305 | struct hci_dev *hdev = hcon->hdev; | |
306 | u8 local_dist = 0, remote_dist = 0; | |
54790f73 | 307 | |
a8b2d5c2 | 308 | if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) { |
7ee4ea36 MH |
309 | local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
310 | remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; | |
54790f73 | 311 | authreq |= SMP_AUTH_BONDING; |
2b64d153 BG |
312 | } else { |
313 | authreq &= ~SMP_AUTH_BONDING; | |
54790f73 VCG |
314 | } |
315 | ||
fd349c02 JH |
316 | if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) |
317 | remote_dist |= SMP_DIST_ID_KEY; | |
318 | ||
863efaf2 JH |
319 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
320 | local_dist |= SMP_DIST_ID_KEY; | |
321 | ||
54790f73 VCG |
322 | if (rsp == NULL) { |
323 | req->io_capability = conn->hcon->io_capability; | |
324 | req->oob_flag = SMP_OOB_NOT_PRESENT; | |
325 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
fd349c02 JH |
326 | req->init_key_dist = local_dist; |
327 | req->resp_key_dist = remote_dist; | |
065a13e2 | 328 | req->auth_req = (authreq & AUTH_REQ_MASK); |
fd349c02 JH |
329 | |
330 | smp->remote_key_dist = remote_dist; | |
54790f73 VCG |
331 | return; |
332 | } | |
333 | ||
334 | rsp->io_capability = conn->hcon->io_capability; | |
335 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; | |
336 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
fd349c02 JH |
337 | rsp->init_key_dist = req->init_key_dist & remote_dist; |
338 | rsp->resp_key_dist = req->resp_key_dist & local_dist; | |
065a13e2 | 339 | rsp->auth_req = (authreq & AUTH_REQ_MASK); |
fd349c02 JH |
340 | |
341 | smp->remote_key_dist = rsp->init_key_dist; | |
b8e66eac VCG |
342 | } |
343 | ||
3158c50c VCG |
344 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
345 | { | |
1c1def09 VCG |
346 | struct smp_chan *smp = conn->smp_chan; |
347 | ||
3158c50c | 348 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
f1560463 | 349 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
3158c50c VCG |
350 | return SMP_ENC_KEY_SIZE; |
351 | ||
f7aa611a | 352 | smp->enc_key_size = max_key_size; |
3158c50c VCG |
353 | |
354 | return 0; | |
355 | } | |
356 | ||
84794e11 | 357 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
4f957a76 | 358 | { |
bab73cb6 JH |
359 | struct hci_conn *hcon = conn->hcon; |
360 | ||
84794e11 | 361 | if (reason) |
4f957a76 | 362 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
f1560463 | 363 | &reason); |
4f957a76 | 364 | |
ce39fb4e MH |
365 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
366 | mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type, | |
367 | HCI_ERROR_AUTH_FAILURE); | |
f1c09c07 | 368 | |
61a0cfb0 AG |
369 | cancel_delayed_work_sync(&conn->security_timer); |
370 | ||
ce39fb4e | 371 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
f1c09c07 | 372 | smp_chan_destroy(conn); |
4f957a76 BG |
373 | } |
374 | ||
2b64d153 BG |
375 | #define JUST_WORKS 0x00 |
376 | #define JUST_CFM 0x01 | |
377 | #define REQ_PASSKEY 0x02 | |
378 | #define CFM_PASSKEY 0x03 | |
379 | #define REQ_OOB 0x04 | |
380 | #define OVERLAP 0xFF | |
381 | ||
382 | static const u8 gen_method[5][5] = { | |
383 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
384 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
385 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, | |
386 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, | |
387 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, | |
388 | }; | |
389 | ||
581370cc JH |
390 | static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io) |
391 | { | |
392 | /* If either side has unknown io_caps, use JUST WORKS */ | |
393 | if (local_io > SMP_IO_KEYBOARD_DISPLAY || | |
394 | remote_io > SMP_IO_KEYBOARD_DISPLAY) | |
395 | return JUST_WORKS; | |
396 | ||
397 | return gen_method[remote_io][local_io]; | |
398 | } | |
399 | ||
2b64d153 BG |
400 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, |
401 | u8 local_io, u8 remote_io) | |
402 | { | |
403 | struct hci_conn *hcon = conn->hcon; | |
404 | struct smp_chan *smp = conn->smp_chan; | |
405 | u8 method; | |
406 | u32 passkey = 0; | |
407 | int ret = 0; | |
408 | ||
409 | /* Initialize key for JUST WORKS */ | |
410 | memset(smp->tk, 0, sizeof(smp->tk)); | |
4a74d658 | 411 | clear_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
412 | |
413 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); | |
414 | ||
415 | /* If neither side wants MITM, use JUST WORKS */ | |
2b64d153 | 416 | /* Otherwise, look up method from the table */ |
581370cc | 417 | if (!(auth & SMP_AUTH_MITM)) |
2b64d153 BG |
418 | method = JUST_WORKS; |
419 | else | |
581370cc | 420 | method = get_auth_method(smp, local_io, remote_io); |
2b64d153 BG |
421 | |
422 | /* If not bonding, don't ask user to confirm a Zero TK */ | |
423 | if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM) | |
424 | method = JUST_WORKS; | |
425 | ||
a82505c7 | 426 | /* Don't confirm locally initiated pairing attempts */ |
4a74d658 | 427 | if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) |
a82505c7 JH |
428 | method = JUST_WORKS; |
429 | ||
2b64d153 BG |
430 | /* If Just Works, Continue with Zero TK */ |
431 | if (method == JUST_WORKS) { | |
4a74d658 | 432 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
433 | return 0; |
434 | } | |
435 | ||
436 | /* Not Just Works/Confirm results in MITM Authentication */ | |
437 | if (method != JUST_CFM) | |
4a74d658 | 438 | set_bit(SMP_FLAG_MITM_AUTH, &smp->flags); |
2b64d153 BG |
439 | |
440 | /* If both devices have Keyoard-Display I/O, the master | |
441 | * Confirms and the slave Enters the passkey. | |
442 | */ | |
443 | if (method == OVERLAP) { | |
444 | if (hcon->link_mode & HCI_LM_MASTER) | |
445 | method = CFM_PASSKEY; | |
446 | else | |
447 | method = REQ_PASSKEY; | |
448 | } | |
449 | ||
01ad34d2 | 450 | /* Generate random passkey. */ |
2b64d153 | 451 | if (method == CFM_PASSKEY) { |
943a732a | 452 | memset(smp->tk, 0, sizeof(smp->tk)); |
2b64d153 BG |
453 | get_random_bytes(&passkey, sizeof(passkey)); |
454 | passkey %= 1000000; | |
943a732a | 455 | put_unaligned_le32(passkey, smp->tk); |
2b64d153 | 456 | BT_DBG("PassKey: %d", passkey); |
4a74d658 | 457 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
458 | } |
459 | ||
460 | hci_dev_lock(hcon->hdev); | |
461 | ||
462 | if (method == REQ_PASSKEY) | |
ce39fb4e | 463 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
272d90df | 464 | hcon->type, hcon->dst_type); |
4eb65e66 JH |
465 | else if (method == JUST_CFM) |
466 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, | |
467 | hcon->type, hcon->dst_type, | |
468 | passkey, 1); | |
2b64d153 | 469 | else |
01ad34d2 | 470 | ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst, |
272d90df | 471 | hcon->type, hcon->dst_type, |
39adbffe | 472 | passkey, 0); |
2b64d153 BG |
473 | |
474 | hci_dev_unlock(hcon->hdev); | |
475 | ||
476 | return ret; | |
477 | } | |
478 | ||
1cc61144 | 479 | static u8 smp_confirm(struct smp_chan *smp) |
8aab4757 | 480 | { |
8aab4757 | 481 | struct l2cap_conn *conn = smp->conn; |
893ce8b1 JH |
482 | struct hci_dev *hdev = conn->hcon->hdev; |
483 | struct crypto_blkcipher *tfm = hdev->tfm_aes; | |
8aab4757 VCG |
484 | struct smp_cmd_pairing_confirm cp; |
485 | int ret; | |
8aab4757 VCG |
486 | |
487 | BT_DBG("conn %p", conn); | |
488 | ||
893ce8b1 JH |
489 | /* Prevent mutual access to hdev->tfm_aes */ |
490 | hci_dev_lock(hdev); | |
8aab4757 | 491 | |
b1cd5fd9 JH |
492 | ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, |
493 | conn->hcon->init_addr_type, &conn->hcon->init_addr, | |
943a732a JH |
494 | conn->hcon->resp_addr_type, &conn->hcon->resp_addr, |
495 | cp.confirm_val); | |
893ce8b1 JH |
496 | |
497 | hci_dev_unlock(hdev); | |
498 | ||
1cc61144 JH |
499 | if (ret) |
500 | return SMP_UNSPECIFIED; | |
8aab4757 | 501 | |
4a74d658 | 502 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
2b64d153 | 503 | |
8aab4757 VCG |
504 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); |
505 | ||
1cc61144 | 506 | return 0; |
8aab4757 VCG |
507 | } |
508 | ||
861580a9 | 509 | static u8 smp_random(struct smp_chan *smp) |
8aab4757 | 510 | { |
8aab4757 VCG |
511 | struct l2cap_conn *conn = smp->conn; |
512 | struct hci_conn *hcon = conn->hcon; | |
893ce8b1 JH |
513 | struct hci_dev *hdev = hcon->hdev; |
514 | struct crypto_blkcipher *tfm = hdev->tfm_aes; | |
861580a9 | 515 | u8 confirm[16]; |
8aab4757 VCG |
516 | int ret; |
517 | ||
861580a9 JH |
518 | if (IS_ERR_OR_NULL(tfm)) |
519 | return SMP_UNSPECIFIED; | |
8aab4757 VCG |
520 | |
521 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); | |
522 | ||
893ce8b1 JH |
523 | /* Prevent mutual access to hdev->tfm_aes */ |
524 | hci_dev_lock(hdev); | |
525 | ||
b1cd5fd9 JH |
526 | ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
527 | hcon->init_addr_type, &hcon->init_addr, | |
943a732a | 528 | hcon->resp_addr_type, &hcon->resp_addr, confirm); |
893ce8b1 JH |
529 | |
530 | hci_dev_unlock(hdev); | |
531 | ||
861580a9 JH |
532 | if (ret) |
533 | return SMP_UNSPECIFIED; | |
8aab4757 | 534 | |
8aab4757 VCG |
535 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { |
536 | BT_ERR("Pairing failed (confirmation values mismatch)"); | |
861580a9 | 537 | return SMP_CONFIRM_FAILED; |
8aab4757 VCG |
538 | } |
539 | ||
540 | if (hcon->out) { | |
fe39c7b2 MH |
541 | u8 stk[16]; |
542 | __le64 rand = 0; | |
543 | __le16 ediv = 0; | |
8aab4757 | 544 | |
943a732a | 545 | smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, stk); |
8aab4757 | 546 | |
f7aa611a | 547 | memset(stk + smp->enc_key_size, 0, |
04124681 | 548 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 549 | |
861580a9 JH |
550 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
551 | return SMP_UNSPECIFIED; | |
8aab4757 VCG |
552 | |
553 | hci_le_start_enc(hcon, ediv, rand, stk); | |
f7aa611a | 554 | hcon->enc_key_size = smp->enc_key_size; |
8aab4757 | 555 | } else { |
fff3490f | 556 | u8 stk[16], auth; |
fe39c7b2 MH |
557 | __le64 rand = 0; |
558 | __le16 ediv = 0; | |
8aab4757 | 559 | |
943a732a JH |
560 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
561 | smp->prnd); | |
8aab4757 | 562 | |
943a732a | 563 | smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, stk); |
8aab4757 | 564 | |
f7aa611a | 565 | memset(stk + smp->enc_key_size, 0, |
f1560463 | 566 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 567 | |
fff3490f JH |
568 | if (hcon->pending_sec_level == BT_SECURITY_HIGH) |
569 | auth = 1; | |
570 | else | |
571 | auth = 0; | |
572 | ||
7d5843b7 JH |
573 | /* Even though there's no _SLAVE suffix this is the |
574 | * slave STK we're adding for later lookup (the master | |
575 | * STK never needs to be stored). | |
576 | */ | |
ce39fb4e | 577 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
2ceba539 | 578 | SMP_STK, auth, stk, smp->enc_key_size, ediv, rand); |
8aab4757 VCG |
579 | } |
580 | ||
861580a9 | 581 | return 0; |
8aab4757 VCG |
582 | } |
583 | ||
584 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) | |
585 | { | |
586 | struct smp_chan *smp; | |
587 | ||
f1560463 | 588 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
8aab4757 VCG |
589 | if (!smp) |
590 | return NULL; | |
591 | ||
8aab4757 VCG |
592 | smp->conn = conn; |
593 | conn->smp_chan = smp; | |
2b64d153 | 594 | conn->hcon->smp_conn = conn; |
8aab4757 VCG |
595 | |
596 | hci_conn_hold(conn->hcon); | |
597 | ||
598 | return smp; | |
599 | } | |
600 | ||
601 | void smp_chan_destroy(struct l2cap_conn *conn) | |
602 | { | |
c8eb9690 | 603 | struct smp_chan *smp = conn->smp_chan; |
f4a407be | 604 | bool complete; |
c8eb9690 | 605 | |
f1c09c07 | 606 | BUG_ON(!smp); |
c8eb9690 | 607 | |
4a74d658 | 608 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); |
f4a407be JH |
609 | mgmt_smp_complete(conn->hcon, complete); |
610 | ||
7ee4ea36 MH |
611 | kfree(smp->csrk); |
612 | kfree(smp->slave_csrk); | |
613 | ||
759331d7 JH |
614 | /* If pairing failed clean up any keys we might have */ |
615 | if (!complete) { | |
616 | if (smp->ltk) { | |
617 | list_del(&smp->ltk->list); | |
618 | kfree(smp->ltk); | |
619 | } | |
620 | ||
621 | if (smp->slave_ltk) { | |
622 | list_del(&smp->slave_ltk->list); | |
623 | kfree(smp->slave_ltk); | |
624 | } | |
625 | ||
626 | if (smp->remote_irk) { | |
627 | list_del(&smp->remote_irk->list); | |
628 | kfree(smp->remote_irk); | |
629 | } | |
630 | } | |
631 | ||
c8eb9690 BG |
632 | kfree(smp); |
633 | conn->smp_chan = NULL; | |
2b64d153 | 634 | conn->hcon->smp_conn = NULL; |
76a68ba0 | 635 | hci_conn_drop(conn->hcon); |
8aab4757 VCG |
636 | } |
637 | ||
2b64d153 BG |
638 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
639 | { | |
640 | struct l2cap_conn *conn = hcon->smp_conn; | |
641 | struct smp_chan *smp; | |
642 | u32 value; | |
2b64d153 BG |
643 | |
644 | BT_DBG(""); | |
645 | ||
646 | if (!conn) | |
647 | return -ENOTCONN; | |
648 | ||
649 | smp = conn->smp_chan; | |
650 | ||
651 | switch (mgmt_op) { | |
652 | case MGMT_OP_USER_PASSKEY_REPLY: | |
653 | value = le32_to_cpu(passkey); | |
943a732a | 654 | memset(smp->tk, 0, sizeof(smp->tk)); |
2b64d153 | 655 | BT_DBG("PassKey: %d", value); |
943a732a | 656 | put_unaligned_le32(value, smp->tk); |
2b64d153 BG |
657 | /* Fall Through */ |
658 | case MGMT_OP_USER_CONFIRM_REPLY: | |
4a74d658 | 659 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
660 | break; |
661 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: | |
662 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: | |
84794e11 | 663 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
2b64d153 BG |
664 | return 0; |
665 | default: | |
84794e11 | 666 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
2b64d153 BG |
667 | return -EOPNOTSUPP; |
668 | } | |
669 | ||
670 | /* If it is our turn to send Pairing Confirm, do so now */ | |
1cc61144 JH |
671 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) { |
672 | u8 rsp = smp_confirm(smp); | |
673 | if (rsp) | |
674 | smp_failure(conn, rsp); | |
675 | } | |
2b64d153 BG |
676 | |
677 | return 0; | |
678 | } | |
679 | ||
da85e5e5 | 680 | static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 681 | { |
3158c50c | 682 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
8aab4757 | 683 | struct smp_chan *smp; |
c7262e71 | 684 | u8 key_size, auth, sec_level; |
8aab4757 | 685 | int ret; |
88ba43b6 AB |
686 | |
687 | BT_DBG("conn %p", conn); | |
688 | ||
c46b98be | 689 | if (skb->len < sizeof(*req)) |
38e4a915 | 690 | return SMP_INVALID_PARAMS; |
c46b98be | 691 | |
2b64d153 BG |
692 | if (conn->hcon->link_mode & HCI_LM_MASTER) |
693 | return SMP_CMD_NOTSUPP; | |
694 | ||
51a8efd7 | 695 | if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) |
8aab4757 | 696 | smp = smp_chan_create(conn); |
d08fd0e7 AE |
697 | else |
698 | smp = conn->smp_chan; | |
8aab4757 | 699 | |
d08fd0e7 AE |
700 | if (!smp) |
701 | return SMP_UNSPECIFIED; | |
d26a2345 | 702 | |
1c1def09 VCG |
703 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
704 | memcpy(&smp->preq[1], req, sizeof(*req)); | |
3158c50c | 705 | skb_pull(skb, sizeof(*req)); |
88ba43b6 | 706 | |
2b64d153 | 707 | /* We didn't start the pairing, so match remote */ |
1ef35827 | 708 | auth = req->auth_req; |
da85e5e5 | 709 | |
c7262e71 JH |
710 | sec_level = authreq_to_seclevel(auth); |
711 | if (sec_level > conn->hcon->pending_sec_level) | |
712 | conn->hcon->pending_sec_level = sec_level; | |
fdde0a26 | 713 | |
2ed8f65c JH |
714 | /* If we need MITM check that it can be acheived */ |
715 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { | |
716 | u8 method; | |
717 | ||
718 | method = get_auth_method(smp, conn->hcon->io_capability, | |
719 | req->io_capability); | |
720 | if (method == JUST_WORKS || method == JUST_CFM) | |
721 | return SMP_AUTH_REQUIREMENTS; | |
722 | } | |
723 | ||
2b64d153 | 724 | build_pairing_cmd(conn, req, &rsp, auth); |
3158c50c VCG |
725 | |
726 | key_size = min(req->max_key_size, rsp.max_key_size); | |
727 | if (check_enc_key_size(conn, key_size)) | |
728 | return SMP_ENC_KEY_SIZE; | |
88ba43b6 | 729 | |
e84a6b13 | 730 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
8aab4757 | 731 | |
1c1def09 VCG |
732 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
733 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); | |
f01ead31 | 734 | |
3158c50c | 735 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
da85e5e5 | 736 | |
2b64d153 BG |
737 | /* Request setup of TK */ |
738 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); | |
739 | if (ret) | |
740 | return SMP_UNSPECIFIED; | |
741 | ||
4a74d658 | 742 | clear_bit(SMP_FLAG_INITIATOR, &smp->flags); |
edca792c | 743 | |
da85e5e5 | 744 | return 0; |
88ba43b6 AB |
745 | } |
746 | ||
da85e5e5 | 747 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 748 | { |
3158c50c | 749 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
1c1def09 | 750 | struct smp_chan *smp = conn->smp_chan; |
2b64d153 | 751 | u8 key_size, auth = SMP_AUTH_NONE; |
7d24ddcc | 752 | int ret; |
88ba43b6 AB |
753 | |
754 | BT_DBG("conn %p", conn); | |
755 | ||
c46b98be | 756 | if (skb->len < sizeof(*rsp)) |
38e4a915 | 757 | return SMP_INVALID_PARAMS; |
c46b98be | 758 | |
2b64d153 BG |
759 | if (!(conn->hcon->link_mode & HCI_LM_MASTER)) |
760 | return SMP_CMD_NOTSUPP; | |
761 | ||
3158c50c VCG |
762 | skb_pull(skb, sizeof(*rsp)); |
763 | ||
1c1def09 | 764 | req = (void *) &smp->preq[1]; |
da85e5e5 | 765 | |
3158c50c VCG |
766 | key_size = min(req->max_key_size, rsp->max_key_size); |
767 | if (check_enc_key_size(conn, key_size)) | |
768 | return SMP_ENC_KEY_SIZE; | |
769 | ||
2ed8f65c JH |
770 | /* If we need MITM check that it can be acheived */ |
771 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { | |
772 | u8 method; | |
773 | ||
774 | method = get_auth_method(smp, req->io_capability, | |
775 | rsp->io_capability); | |
776 | if (method == JUST_WORKS || method == JUST_CFM) | |
777 | return SMP_AUTH_REQUIREMENTS; | |
778 | } | |
779 | ||
e84a6b13 | 780 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
7d24ddcc | 781 | |
8aab4757 VCG |
782 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
783 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); | |
7d24ddcc | 784 | |
fdcc4bec JH |
785 | /* Update remote key distribution in case the remote cleared |
786 | * some bits that we had enabled in our request. | |
787 | */ | |
788 | smp->remote_key_dist &= rsp->resp_key_dist; | |
789 | ||
2b64d153 | 790 | if ((req->auth_req & SMP_AUTH_BONDING) && |
f1560463 | 791 | (rsp->auth_req & SMP_AUTH_BONDING)) |
2b64d153 BG |
792 | auth = SMP_AUTH_BONDING; |
793 | ||
794 | auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM; | |
795 | ||
476585ec | 796 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
2b64d153 BG |
797 | if (ret) |
798 | return SMP_UNSPECIFIED; | |
799 | ||
4a74d658 | 800 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
2b64d153 BG |
801 | |
802 | /* Can't compose response until we have been confirmed */ | |
4a74d658 | 803 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
1cc61144 | 804 | return smp_confirm(smp); |
da85e5e5 VCG |
805 | |
806 | return 0; | |
88ba43b6 AB |
807 | } |
808 | ||
da85e5e5 | 809 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 810 | { |
1c1def09 | 811 | struct smp_chan *smp = conn->smp_chan; |
7d24ddcc | 812 | |
88ba43b6 AB |
813 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
814 | ||
c46b98be | 815 | if (skb->len < sizeof(smp->pcnf)) |
38e4a915 | 816 | return SMP_INVALID_PARAMS; |
c46b98be | 817 | |
1c1def09 VCG |
818 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
819 | skb_pull(skb, sizeof(smp->pcnf)); | |
88ba43b6 | 820 | |
943a732a JH |
821 | if (conn->hcon->out) |
822 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), | |
823 | smp->prnd); | |
4a74d658 | 824 | else if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
1cc61144 | 825 | return smp_confirm(smp); |
943a732a | 826 | else |
4a74d658 | 827 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
da85e5e5 VCG |
828 | |
829 | return 0; | |
88ba43b6 AB |
830 | } |
831 | ||
da85e5e5 | 832 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 833 | { |
1c1def09 | 834 | struct smp_chan *smp = conn->smp_chan; |
7d24ddcc | 835 | |
8aab4757 | 836 | BT_DBG("conn %p", conn); |
3158c50c | 837 | |
c46b98be | 838 | if (skb->len < sizeof(smp->rrnd)) |
38e4a915 | 839 | return SMP_INVALID_PARAMS; |
c46b98be | 840 | |
943a732a | 841 | memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd)); |
8aab4757 | 842 | skb_pull(skb, sizeof(smp->rrnd)); |
e7e62c85 | 843 | |
861580a9 | 844 | return smp_random(smp); |
88ba43b6 AB |
845 | } |
846 | ||
4dab7864 | 847 | static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
988c5997 | 848 | { |
c9839a11 | 849 | struct smp_ltk *key; |
988c5997 VCG |
850 | struct hci_conn *hcon = conn->hcon; |
851 | ||
98a0b845 JH |
852 | key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type, |
853 | hcon->out); | |
988c5997 VCG |
854 | if (!key) |
855 | return 0; | |
856 | ||
4dab7864 JH |
857 | if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated) |
858 | return 0; | |
859 | ||
51a8efd7 | 860 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
988c5997 VCG |
861 | return 1; |
862 | ||
c9839a11 VCG |
863 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
864 | hcon->enc_key_size = key->enc_size; | |
988c5997 VCG |
865 | |
866 | return 1; | |
988c5997 | 867 | } |
f1560463 | 868 | |
da85e5e5 | 869 | static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 AB |
870 | { |
871 | struct smp_cmd_security_req *rp = (void *) skb->data; | |
872 | struct smp_cmd_pairing cp; | |
f1cb9af5 | 873 | struct hci_conn *hcon = conn->hcon; |
8aab4757 | 874 | struct smp_chan *smp; |
c7262e71 | 875 | u8 sec_level; |
88ba43b6 AB |
876 | |
877 | BT_DBG("conn %p", conn); | |
878 | ||
c46b98be | 879 | if (skb->len < sizeof(*rp)) |
38e4a915 | 880 | return SMP_INVALID_PARAMS; |
c46b98be | 881 | |
86ca9eac JH |
882 | if (!(conn->hcon->link_mode & HCI_LM_MASTER)) |
883 | return SMP_CMD_NOTSUPP; | |
884 | ||
c7262e71 JH |
885 | sec_level = authreq_to_seclevel(rp->auth_req); |
886 | if (sec_level > hcon->pending_sec_level) | |
887 | hcon->pending_sec_level = sec_level; | |
feb45eb5 | 888 | |
4dab7864 | 889 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
988c5997 VCG |
890 | return 0; |
891 | ||
51a8efd7 | 892 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
da85e5e5 | 893 | return 0; |
f1cb9af5 | 894 | |
8aab4757 | 895 | smp = smp_chan_create(conn); |
c29d2444 JH |
896 | if (!smp) |
897 | return SMP_UNSPECIFIED; | |
d26a2345 | 898 | |
88ba43b6 | 899 | skb_pull(skb, sizeof(*rp)); |
88ba43b6 | 900 | |
da85e5e5 | 901 | memset(&cp, 0, sizeof(cp)); |
54790f73 | 902 | build_pairing_cmd(conn, &cp, NULL, rp->auth_req); |
88ba43b6 | 903 | |
1c1def09 VCG |
904 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
905 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 906 | |
88ba43b6 | 907 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
f1cb9af5 | 908 | |
4a74d658 | 909 | clear_bit(SMP_FLAG_INITIATOR, &smp->flags); |
edca792c | 910 | |
da85e5e5 | 911 | return 0; |
88ba43b6 AB |
912 | } |
913 | ||
ad32a2f5 JH |
914 | bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level) |
915 | { | |
916 | if (sec_level == BT_SECURITY_LOW) | |
917 | return true; | |
918 | ||
919 | if (hcon->sec_level >= sec_level) | |
920 | return true; | |
921 | ||
922 | return false; | |
923 | } | |
924 | ||
cc110922 | 925 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
eb492e01 | 926 | { |
cc110922 | 927 | struct l2cap_conn *conn = hcon->l2cap_data; |
0a66cf20 | 928 | struct smp_chan *smp; |
2b64d153 | 929 | __u8 authreq; |
eb492e01 | 930 | |
3a0259bb VCG |
931 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
932 | ||
0a66cf20 JH |
933 | /* This may be NULL if there's an unexpected disconnection */ |
934 | if (!conn) | |
935 | return 1; | |
936 | ||
757aee0f | 937 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
2e65c9d2 AG |
938 | return 1; |
939 | ||
ad32a2f5 | 940 | if (smp_sufficient_security(hcon, sec_level)) |
eb492e01 | 941 | return 1; |
f1cb9af5 | 942 | |
c7262e71 JH |
943 | if (sec_level > hcon->pending_sec_level) |
944 | hcon->pending_sec_level = sec_level; | |
945 | ||
988c5997 | 946 | if (hcon->link_mode & HCI_LM_MASTER) |
c7262e71 JH |
947 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
948 | return 0; | |
d26a2345 | 949 | |
51a8efd7 | 950 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
d26a2345 VCG |
951 | return 0; |
952 | ||
8aab4757 | 953 | smp = smp_chan_create(conn); |
2b64d153 BG |
954 | if (!smp) |
955 | return 1; | |
956 | ||
957 | authreq = seclevel_to_authreq(sec_level); | |
d26a2345 | 958 | |
79897d20 JH |
959 | /* Require MITM if IO Capability allows or the security level |
960 | * requires it. | |
2e233644 | 961 | */ |
79897d20 | 962 | if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT || |
c7262e71 | 963 | hcon->pending_sec_level > BT_SECURITY_MEDIUM) |
2e233644 JH |
964 | authreq |= SMP_AUTH_MITM; |
965 | ||
d26a2345 VCG |
966 | if (hcon->link_mode & HCI_LM_MASTER) { |
967 | struct smp_cmd_pairing cp; | |
f01ead31 | 968 | |
2b64d153 | 969 | build_pairing_cmd(conn, &cp, NULL, authreq); |
1c1def09 VCG |
970 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
971 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 972 | |
eb492e01 AB |
973 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
974 | } else { | |
975 | struct smp_cmd_security_req cp; | |
2b64d153 | 976 | cp.auth_req = authreq; |
eb492e01 AB |
977 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
978 | } | |
979 | ||
4a74d658 | 980 | set_bit(SMP_FLAG_INITIATOR, &smp->flags); |
edca792c | 981 | |
eb492e01 AB |
982 | return 0; |
983 | } | |
984 | ||
7034b911 VCG |
985 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
986 | { | |
16b90839 | 987 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
1c1def09 | 988 | struct smp_chan *smp = conn->smp_chan; |
16b90839 | 989 | |
c46b98be JH |
990 | BT_DBG("conn %p", conn); |
991 | ||
992 | if (skb->len < sizeof(*rp)) | |
38e4a915 | 993 | return SMP_INVALID_PARAMS; |
c46b98be | 994 | |
6131ddc8 JH |
995 | /* Ignore this PDU if it wasn't requested */ |
996 | if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY)) | |
997 | return 0; | |
998 | ||
16b90839 VCG |
999 | skb_pull(skb, sizeof(*rp)); |
1000 | ||
1c1def09 | 1001 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
16b90839 | 1002 | |
7034b911 VCG |
1003 | return 0; |
1004 | } | |
1005 | ||
1006 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) | |
1007 | { | |
16b90839 | 1008 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
1c1def09 | 1009 | struct smp_chan *smp = conn->smp_chan; |
c9839a11 VCG |
1010 | struct hci_dev *hdev = conn->hcon->hdev; |
1011 | struct hci_conn *hcon = conn->hcon; | |
23d0e128 | 1012 | struct smp_ltk *ltk; |
c9839a11 | 1013 | u8 authenticated; |
16b90839 | 1014 | |
c46b98be JH |
1015 | BT_DBG("conn %p", conn); |
1016 | ||
1017 | if (skb->len < sizeof(*rp)) | |
38e4a915 | 1018 | return SMP_INVALID_PARAMS; |
c46b98be | 1019 | |
6131ddc8 JH |
1020 | /* Ignore this PDU if it wasn't requested */ |
1021 | if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY)) | |
1022 | return 0; | |
1023 | ||
9747a9f3 JH |
1024 | /* Mark the information as received */ |
1025 | smp->remote_key_dist &= ~SMP_DIST_ENC_KEY; | |
1026 | ||
16b90839 | 1027 | skb_pull(skb, sizeof(*rp)); |
7034b911 | 1028 | |
c9839a11 | 1029 | hci_dev_lock(hdev); |
ce39fb4e | 1030 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
2ceba539 | 1031 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK, |
23d0e128 JH |
1032 | authenticated, smp->tk, smp->enc_key_size, |
1033 | rp->ediv, rp->rand); | |
1034 | smp->ltk = ltk; | |
fd349c02 | 1035 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) |
4bd6d38e | 1036 | smp_distribute_keys(conn); |
c9839a11 | 1037 | hci_dev_unlock(hdev); |
7034b911 VCG |
1038 | |
1039 | return 0; | |
1040 | } | |
1041 | ||
fd349c02 JH |
1042 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1043 | { | |
1044 | struct smp_cmd_ident_info *info = (void *) skb->data; | |
1045 | struct smp_chan *smp = conn->smp_chan; | |
1046 | ||
1047 | BT_DBG(""); | |
1048 | ||
1049 | if (skb->len < sizeof(*info)) | |
38e4a915 | 1050 | return SMP_INVALID_PARAMS; |
fd349c02 | 1051 | |
6131ddc8 JH |
1052 | /* Ignore this PDU if it wasn't requested */ |
1053 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) | |
1054 | return 0; | |
1055 | ||
fd349c02 JH |
1056 | skb_pull(skb, sizeof(*info)); |
1057 | ||
1058 | memcpy(smp->irk, info->irk, 16); | |
1059 | ||
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, | |
1064 | struct sk_buff *skb) | |
1065 | { | |
1066 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; | |
1067 | struct smp_chan *smp = conn->smp_chan; | |
1068 | struct hci_conn *hcon = conn->hcon; | |
1069 | bdaddr_t rpa; | |
1070 | ||
1071 | BT_DBG(""); | |
1072 | ||
1073 | if (skb->len < sizeof(*info)) | |
38e4a915 | 1074 | return SMP_INVALID_PARAMS; |
fd349c02 | 1075 | |
6131ddc8 JH |
1076 | /* Ignore this PDU if it wasn't requested */ |
1077 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) | |
1078 | return 0; | |
1079 | ||
9747a9f3 JH |
1080 | /* Mark the information as received */ |
1081 | smp->remote_key_dist &= ~SMP_DIST_ID_KEY; | |
1082 | ||
fd349c02 JH |
1083 | skb_pull(skb, sizeof(*info)); |
1084 | ||
a9a58f86 JH |
1085 | /* Strictly speaking the Core Specification (4.1) allows sending |
1086 | * an empty address which would force us to rely on just the IRK | |
1087 | * as "identity information". However, since such | |
1088 | * implementations are not known of and in order to not over | |
1089 | * complicate our implementation, simply pretend that we never | |
1090 | * received an IRK for such a device. | |
1091 | */ | |
1092 | if (!bacmp(&info->bdaddr, BDADDR_ANY)) { | |
1093 | BT_ERR("Ignoring IRK with no identity address"); | |
4bd6d38e | 1094 | smp_distribute_keys(conn); |
a9a58f86 JH |
1095 | return 0; |
1096 | } | |
1097 | ||
fd349c02 JH |
1098 | bacpy(&smp->id_addr, &info->bdaddr); |
1099 | smp->id_addr_type = info->addr_type; | |
1100 | ||
1101 | if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type)) | |
1102 | bacpy(&rpa, &hcon->dst); | |
1103 | else | |
1104 | bacpy(&rpa, BDADDR_ANY); | |
1105 | ||
23d0e128 JH |
1106 | smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, |
1107 | smp->id_addr_type, smp->irk, &rpa); | |
fd349c02 | 1108 | |
4bd6d38e | 1109 | smp_distribute_keys(conn); |
fd349c02 JH |
1110 | |
1111 | return 0; | |
1112 | } | |
1113 | ||
7ee4ea36 MH |
1114 | static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1115 | { | |
1116 | struct smp_cmd_sign_info *rp = (void *) skb->data; | |
1117 | struct smp_chan *smp = conn->smp_chan; | |
1118 | struct hci_dev *hdev = conn->hcon->hdev; | |
1119 | struct smp_csrk *csrk; | |
1120 | ||
1121 | BT_DBG("conn %p", conn); | |
1122 | ||
1123 | if (skb->len < sizeof(*rp)) | |
38e4a915 | 1124 | return SMP_INVALID_PARAMS; |
7ee4ea36 MH |
1125 | |
1126 | /* Ignore this PDU if it wasn't requested */ | |
1127 | if (!(smp->remote_key_dist & SMP_DIST_SIGN)) | |
1128 | return 0; | |
1129 | ||
1130 | /* Mark the information as received */ | |
1131 | smp->remote_key_dist &= ~SMP_DIST_SIGN; | |
1132 | ||
1133 | skb_pull(skb, sizeof(*rp)); | |
1134 | ||
1135 | hci_dev_lock(hdev); | |
1136 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); | |
1137 | if (csrk) { | |
1138 | csrk->master = 0x01; | |
1139 | memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); | |
1140 | } | |
1141 | smp->csrk = csrk; | |
1142 | if (!(smp->remote_key_dist & SMP_DIST_SIGN)) | |
1143 | smp_distribute_keys(conn); | |
1144 | hci_dev_unlock(hdev); | |
1145 | ||
1146 | return 0; | |
1147 | } | |
1148 | ||
eb492e01 AB |
1149 | int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) |
1150 | { | |
7b9899db | 1151 | struct hci_conn *hcon = conn->hcon; |
92381f5c | 1152 | __u8 code, reason; |
eb492e01 AB |
1153 | int err = 0; |
1154 | ||
7b9899db MH |
1155 | if (hcon->type != LE_LINK) { |
1156 | kfree_skb(skb); | |
3432711f | 1157 | return 0; |
7b9899db MH |
1158 | } |
1159 | ||
92381f5c MH |
1160 | if (skb->len < 1) { |
1161 | kfree_skb(skb); | |
1162 | return -EILSEQ; | |
1163 | } | |
1164 | ||
06ae3314 | 1165 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
2e65c9d2 AG |
1166 | err = -ENOTSUPP; |
1167 | reason = SMP_PAIRING_NOTSUPP; | |
1168 | goto done; | |
1169 | } | |
1170 | ||
92381f5c | 1171 | code = skb->data[0]; |
eb492e01 AB |
1172 | skb_pull(skb, sizeof(code)); |
1173 | ||
8cf9fa12 JH |
1174 | /* |
1175 | * The SMP context must be initialized for all other PDUs except | |
1176 | * pairing and security requests. If we get any other PDU when | |
1177 | * not initialized simply disconnect (done if this function | |
1178 | * returns an error). | |
1179 | */ | |
1180 | if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ && | |
1181 | !conn->smp_chan) { | |
1182 | BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code); | |
1183 | kfree_skb(skb); | |
1184 | return -ENOTSUPP; | |
1185 | } | |
1186 | ||
eb492e01 AB |
1187 | switch (code) { |
1188 | case SMP_CMD_PAIRING_REQ: | |
da85e5e5 | 1189 | reason = smp_cmd_pairing_req(conn, skb); |
eb492e01 AB |
1190 | break; |
1191 | ||
1192 | case SMP_CMD_PAIRING_FAIL: | |
84794e11 | 1193 | smp_failure(conn, 0); |
da85e5e5 VCG |
1194 | reason = 0; |
1195 | err = -EPERM; | |
eb492e01 AB |
1196 | break; |
1197 | ||
1198 | case SMP_CMD_PAIRING_RSP: | |
da85e5e5 | 1199 | reason = smp_cmd_pairing_rsp(conn, skb); |
88ba43b6 AB |
1200 | break; |
1201 | ||
1202 | case SMP_CMD_SECURITY_REQ: | |
da85e5e5 | 1203 | reason = smp_cmd_security_req(conn, skb); |
88ba43b6 AB |
1204 | break; |
1205 | ||
eb492e01 | 1206 | case SMP_CMD_PAIRING_CONFIRM: |
da85e5e5 | 1207 | reason = smp_cmd_pairing_confirm(conn, skb); |
88ba43b6 AB |
1208 | break; |
1209 | ||
eb492e01 | 1210 | case SMP_CMD_PAIRING_RANDOM: |
da85e5e5 | 1211 | reason = smp_cmd_pairing_random(conn, skb); |
88ba43b6 AB |
1212 | break; |
1213 | ||
eb492e01 | 1214 | case SMP_CMD_ENCRYPT_INFO: |
7034b911 VCG |
1215 | reason = smp_cmd_encrypt_info(conn, skb); |
1216 | break; | |
1217 | ||
eb492e01 | 1218 | case SMP_CMD_MASTER_IDENT: |
7034b911 VCG |
1219 | reason = smp_cmd_master_ident(conn, skb); |
1220 | break; | |
1221 | ||
eb492e01 | 1222 | case SMP_CMD_IDENT_INFO: |
fd349c02 JH |
1223 | reason = smp_cmd_ident_info(conn, skb); |
1224 | break; | |
1225 | ||
eb492e01 | 1226 | case SMP_CMD_IDENT_ADDR_INFO: |
fd349c02 JH |
1227 | reason = smp_cmd_ident_addr_info(conn, skb); |
1228 | break; | |
1229 | ||
eb492e01 | 1230 | case SMP_CMD_SIGN_INFO: |
7ee4ea36 | 1231 | reason = smp_cmd_sign_info(conn, skb); |
7034b911 VCG |
1232 | break; |
1233 | ||
eb492e01 AB |
1234 | default: |
1235 | BT_DBG("Unknown command code 0x%2.2x", code); | |
1236 | ||
1237 | reason = SMP_CMD_NOTSUPP; | |
eb492e01 | 1238 | err = -EOPNOTSUPP; |
3a0259bb | 1239 | goto done; |
eb492e01 AB |
1240 | } |
1241 | ||
3a0259bb VCG |
1242 | done: |
1243 | if (reason) | |
84794e11 | 1244 | smp_failure(conn, reason); |
3a0259bb | 1245 | |
eb492e01 AB |
1246 | kfree_skb(skb); |
1247 | return err; | |
1248 | } | |
7034b911 | 1249 | |
35d70271 JH |
1250 | static void smp_notify_keys(struct l2cap_conn *conn) |
1251 | { | |
1252 | struct smp_chan *smp = conn->smp_chan; | |
1253 | struct hci_conn *hcon = conn->hcon; | |
1254 | struct hci_dev *hdev = hcon->hdev; | |
53ac6ab6 MH |
1255 | struct smp_cmd_pairing *req = (void *) &smp->preq[1]; |
1256 | struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; | |
1257 | bool persistent; | |
35d70271 | 1258 | |
61b1a7fb | 1259 | if (smp->remote_irk) { |
95fbac8a | 1260 | mgmt_new_irk(hdev, smp->remote_irk); |
61b1a7fb JH |
1261 | /* Now that user space can be considered to know the |
1262 | * identity address track the connection based on it | |
1263 | * from now on. | |
1264 | */ | |
1265 | bacpy(&hcon->dst, &smp->remote_irk->bdaddr); | |
1266 | hcon->dst_type = smp->remote_irk->addr_type; | |
1267 | l2cap_conn_update_id_addr(hcon); | |
1268 | } | |
95fbac8a | 1269 | |
53ac6ab6 MH |
1270 | /* The LTKs and CSRKs should be persistent only if both sides |
1271 | * had the bonding bit set in their authentication requests. | |
1272 | */ | |
1273 | persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING); | |
1274 | ||
7ee4ea36 MH |
1275 | if (smp->csrk) { |
1276 | smp->csrk->bdaddr_type = hcon->dst_type; | |
1277 | bacpy(&smp->csrk->bdaddr, &hcon->dst); | |
53ac6ab6 | 1278 | mgmt_new_csrk(hdev, smp->csrk, persistent); |
7ee4ea36 MH |
1279 | } |
1280 | ||
1281 | if (smp->slave_csrk) { | |
1282 | smp->slave_csrk->bdaddr_type = hcon->dst_type; | |
1283 | bacpy(&smp->slave_csrk->bdaddr, &hcon->dst); | |
53ac6ab6 | 1284 | mgmt_new_csrk(hdev, smp->slave_csrk, persistent); |
7ee4ea36 MH |
1285 | } |
1286 | ||
35d70271 JH |
1287 | if (smp->ltk) { |
1288 | smp->ltk->bdaddr_type = hcon->dst_type; | |
1289 | bacpy(&smp->ltk->bdaddr, &hcon->dst); | |
53ac6ab6 | 1290 | mgmt_new_ltk(hdev, smp->ltk, persistent); |
35d70271 JH |
1291 | } |
1292 | ||
1293 | if (smp->slave_ltk) { | |
1294 | smp->slave_ltk->bdaddr_type = hcon->dst_type; | |
1295 | bacpy(&smp->slave_ltk->bdaddr, &hcon->dst); | |
53ac6ab6 | 1296 | mgmt_new_ltk(hdev, smp->slave_ltk, persistent); |
35d70271 JH |
1297 | } |
1298 | } | |
1299 | ||
4bd6d38e | 1300 | int smp_distribute_keys(struct l2cap_conn *conn) |
7034b911 VCG |
1301 | { |
1302 | struct smp_cmd_pairing *req, *rsp; | |
1c1def09 | 1303 | struct smp_chan *smp = conn->smp_chan; |
524237cb JH |
1304 | struct hci_conn *hcon = conn->hcon; |
1305 | struct hci_dev *hdev = hcon->hdev; | |
7034b911 VCG |
1306 | __u8 *keydist; |
1307 | ||
4bd6d38e | 1308 | BT_DBG("conn %p", conn); |
7034b911 | 1309 | |
524237cb | 1310 | if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
d26a2345 VCG |
1311 | return 0; |
1312 | ||
1c1def09 | 1313 | rsp = (void *) &smp->prsp[1]; |
7034b911 VCG |
1314 | |
1315 | /* The responder sends its keys first */ | |
efabba37 | 1316 | if (hcon->out && (smp->remote_key_dist & 0x07)) |
7034b911 VCG |
1317 | return 0; |
1318 | ||
1c1def09 | 1319 | req = (void *) &smp->preq[1]; |
7034b911 | 1320 | |
524237cb | 1321 | if (hcon->out) { |
7034b911 VCG |
1322 | keydist = &rsp->init_key_dist; |
1323 | *keydist &= req->init_key_dist; | |
1324 | } else { | |
1325 | keydist = &rsp->resp_key_dist; | |
1326 | *keydist &= req->resp_key_dist; | |
1327 | } | |
1328 | ||
7034b911 VCG |
1329 | BT_DBG("keydist 0x%x", *keydist); |
1330 | ||
1331 | if (*keydist & SMP_DIST_ENC_KEY) { | |
1332 | struct smp_cmd_encrypt_info enc; | |
1333 | struct smp_cmd_master_ident ident; | |
23d0e128 | 1334 | struct smp_ltk *ltk; |
c9839a11 | 1335 | u8 authenticated; |
7034b911 | 1336 | __le16 ediv; |
fe39c7b2 | 1337 | __le64 rand; |
7034b911 VCG |
1338 | |
1339 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); | |
1340 | get_random_bytes(&ediv, sizeof(ediv)); | |
fe39c7b2 | 1341 | get_random_bytes(&rand, sizeof(rand)); |
7034b911 VCG |
1342 | |
1343 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); | |
1344 | ||
c9839a11 | 1345 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; |
524237cb | 1346 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, |
2ceba539 | 1347 | SMP_LTK_SLAVE, authenticated, enc.ltk, |
fe39c7b2 | 1348 | smp->enc_key_size, ediv, rand); |
23d0e128 | 1349 | smp->slave_ltk = ltk; |
16b90839 | 1350 | |
58115373 | 1351 | ident.ediv = ediv; |
fe39c7b2 | 1352 | ident.rand = rand; |
7034b911 VCG |
1353 | |
1354 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); | |
1355 | ||
1356 | *keydist &= ~SMP_DIST_ENC_KEY; | |
1357 | } | |
1358 | ||
1359 | if (*keydist & SMP_DIST_ID_KEY) { | |
1360 | struct smp_cmd_ident_addr_info addrinfo; | |
1361 | struct smp_cmd_ident_info idinfo; | |
1362 | ||
863efaf2 | 1363 | memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); |
7034b911 VCG |
1364 | |
1365 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); | |
1366 | ||
82d4b359 JH |
1367 | /* The hci_conn contains the local identity address |
1368 | * after the connection has been established. | |
1369 | * | |
1370 | * This is true even when the connection has been | |
1371 | * established using a resolvable random address. | |
1372 | */ | |
524237cb | 1373 | bacpy(&addrinfo.bdaddr, &hcon->src); |
82d4b359 | 1374 | addrinfo.addr_type = hcon->src_type; |
7034b911 VCG |
1375 | |
1376 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), | |
f1560463 | 1377 | &addrinfo); |
7034b911 VCG |
1378 | |
1379 | *keydist &= ~SMP_DIST_ID_KEY; | |
1380 | } | |
1381 | ||
1382 | if (*keydist & SMP_DIST_SIGN) { | |
1383 | struct smp_cmd_sign_info sign; | |
7ee4ea36 | 1384 | struct smp_csrk *csrk; |
7034b911 | 1385 | |
7ee4ea36 | 1386 | /* Generate a new random key */ |
7034b911 VCG |
1387 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); |
1388 | ||
7ee4ea36 MH |
1389 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); |
1390 | if (csrk) { | |
1391 | csrk->master = 0x00; | |
1392 | memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); | |
1393 | } | |
1394 | smp->slave_csrk = csrk; | |
1395 | ||
7034b911 VCG |
1396 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); |
1397 | ||
1398 | *keydist &= ~SMP_DIST_SIGN; | |
1399 | } | |
1400 | ||
efabba37 JH |
1401 | /* If there are still keys to be received wait for them */ |
1402 | if ((smp->remote_key_dist & 0x07)) | |
1403 | return 0; | |
1404 | ||
1d98bf4f JH |
1405 | clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags); |
1406 | cancel_delayed_work_sync(&conn->security_timer); | |
4a74d658 | 1407 | set_bit(SMP_FLAG_COMPLETE, &smp->flags); |
1d98bf4f | 1408 | smp_notify_keys(conn); |
efabba37 | 1409 | |
1d98bf4f | 1410 | smp_chan_destroy(conn); |
d26a2345 | 1411 | |
7034b911 VCG |
1412 | return 0; |
1413 | } |