blob: e03cc284161c4a0df641954ee7fe9818148eb726 [file] [log] [blame]
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
8
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
21*/
22
Marcel Holtmann300acfde2014-12-31 14:43:16 -080023#include <linux/debugfs.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030024#include <linux/scatterlist.h>
Andy Lutomirskia4770e12016-06-26 14:55:23 -070025#include <linux/crypto.h>
Ard Biesheuvel28a220a2019-07-02 21:41:41 +020026#include <crypto/aes.h>
Jason A. Donenfeld329d8232017-06-10 04:59:11 +020027#include <crypto/algapi.h>
Herbert Xu71af2f62016-01-24 21:18:30 +080028#include <crypto/hash.h>
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +030029#include <crypto/kpp.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030030
Anderson Brigliaeb492e02011-06-09 18:50:40 -030031#include <net/bluetooth/bluetooth.h>
32#include <net/bluetooth/hci_core.h>
33#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080034#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070035
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010036#include "ecdh_helper.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070037#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030038
Johan Hedberg2fd36552015-06-11 13:52:26 +030039#define SMP_DEV(hdev) \
40 ((struct smp_dev *)((struct l2cap_chan *)((hdev)->smp_data))->data)
41
Johan Hedbergc7a3d572014-12-01 22:03:16 +020042/* Low-level debug macros to be used for stuff that we don't want
43 * accidentially in dmesg, i.e. the values of the various crypto keys
44 * and the inputs & outputs of crypto functions.
45 */
46#ifdef DEBUG
47#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
48 ##__VA_ARGS__)
49#else
50#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
51 ##__VA_ARGS__)
52#endif
53
Johan Hedbergb28b4942014-09-05 22:19:55 +030054#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030055
Johan Hedberg3b191462014-06-06 10:50:15 +030056/* Keys which are not distributed with Secure Connections */
57#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
58
Marcel Holtmann17b02e62012-03-01 14:32:37 -080059#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030060
Marcel Holtmannd7a5a112015-03-13 02:11:00 -070061#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
Johan Hedberga62da6f2016-12-08 08:32:54 +020062 0x3f : 0x07)
Johan Hedberg0edb14d2014-05-26 13:29:28 +030063#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020064
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030065/* Maximum message length that can be passed to aes_cmac */
66#define CMAC_MSG_MAX 80
67
Johan Hedberg533e35d2014-06-16 19:25:18 +030068enum {
69 SMP_FLAG_TK_VALID,
70 SMP_FLAG_CFM_PENDING,
71 SMP_FLAG_MITM_AUTH,
72 SMP_FLAG_COMPLETE,
73 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030074 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030075 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030076 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030077 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030078 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg1a8bab42015-03-16 11:45:44 +020079 SMP_FLAG_REMOTE_OOB,
80 SMP_FLAG_LOCAL_OOB,
Johan Hedberga62da6f2016-12-08 08:32:54 +020081 SMP_FLAG_CT2,
Johan Hedberg533e35d2014-06-16 19:25:18 +030082};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030083
Marcel Holtmann88a479d2015-03-16 01:10:19 -070084struct smp_dev {
Marcel Holtmann60a27d62015-03-16 01:10:22 -070085 /* Secure Connections OOB data */
Johan Hedberg94f14e42018-09-11 14:10:12 +030086 bool local_oob;
Marcel Holtmann60a27d62015-03-16 01:10:22 -070087 u8 local_pk[64];
Marcel Holtmannfb334fe2015-03-16 12:34:57 -070088 u8 local_rand[16];
Marcel Holtmann60a27d62015-03-16 01:10:22 -070089 bool debug_key;
90
Herbert Xu71af2f62016-01-24 21:18:30 +080091 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +030092 struct crypto_kpp *tfm_ecdh;
Marcel Holtmann88a479d2015-03-16 01:10:19 -070093};
94
Johan Hedberg4bc58f52014-05-20 09:45:47 +030095struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030096 struct l2cap_conn *conn;
97 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030098 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030099
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300100 u8 preq[7]; /* SMP Pairing Request */
101 u8 prsp[7]; /* SMP Pairing Response */
102 u8 prnd[16]; /* SMP Pairing Random (local) */
103 u8 rrnd[16]; /* SMP Pairing Random (remote) */
104 u8 pcnf[16]; /* SMP Pairing Confirm */
105 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberg882fafa2015-03-16 11:45:43 +0200106 u8 rr[16]; /* Remote OOB ra/rb value */
107 u8 lr[16]; /* Local OOB ra/rb value */
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300108 u8 enc_key_size;
109 u8 remote_key_dist;
110 bdaddr_t id_addr;
111 u8 id_addr_type;
112 u8 irk[16];
113 struct smp_csrk *csrk;
114 struct smp_csrk *slave_csrk;
115 struct smp_ltk *ltk;
116 struct smp_ltk *slave_ltk;
117 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +0300118 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300119 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300120 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300121 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300122
Johan Hedberg3b191462014-06-06 10:50:15 +0300123 /* Secure Connections variables */
124 u8 local_pk[64];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300125 u8 remote_pk[64];
126 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300127 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300128
Herbert Xu71af2f62016-01-24 21:18:30 +0800129 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +0300130 struct crypto_kpp *tfm_ecdh;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300131};
132
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300133/* These debug key values are defined in the SMP section of the core
134 * specification. debug_pk is the public debug key and debug_sk the
135 * private debug key.
136 */
137static const u8 debug_pk[64] = {
138 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
139 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
140 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
141 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
142
143 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
144 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
145 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
146 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
147};
148
149static const u8 debug_sk[32] = {
150 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
151 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
152 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
153 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
154};
155
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300156static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300157{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300158 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300159
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300160 for (i = 0; i < len; i++)
161 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300162}
163
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200164/* The following functions map to the LE SC SMP crypto functions
165 * AES-CMAC, f4, f5, f6, g2 and h6.
166 */
167
Herbert Xu71af2f62016-01-24 21:18:30 +0800168static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m,
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300169 size_t len, u8 mac[16])
170{
171 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300172 int err;
173
174 if (len > CMAC_MSG_MAX)
175 return -EFBIG;
176
177 if (!tfm) {
178 BT_ERR("tfm %p", tfm);
179 return -EINVAL;
180 }
181
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300182 /* Swap key and message from LSB to MSB */
183 swap_buf(k, tmp, 16);
184 swap_buf(m, msg_msb, len);
185
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200186 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
187 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300188
Herbert Xu71af2f62016-01-24 21:18:30 +0800189 err = crypto_shash_setkey(tfm, tmp, 16);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300190 if (err) {
191 BT_ERR("cipher setkey failed: %d", err);
192 return err;
193 }
194
Eric Biggersec0bf6e2020-05-01 22:31:19 -0700195 err = crypto_shash_tfm_digest(tfm, msg_msb, len, mac_msb);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300196 if (err) {
Herbert Xu71af2f62016-01-24 21:18:30 +0800197 BT_ERR("Hash computation error %d", err);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300198 return err;
199 }
200
201 swap_buf(mac_msb, mac, 16);
202
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200203 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300204
205 return 0;
206}
207
Herbert Xu71af2f62016-01-24 21:18:30 +0800208static int smp_f4(struct crypto_shash *tfm_cmac, const u8 u[32],
209 const u8 v[32], const u8 x[16], u8 z, u8 res[16])
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300210{
211 u8 m[65];
212 int err;
213
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200214 SMP_DBG("u %32phN", u);
215 SMP_DBG("v %32phN", v);
216 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300217
218 m[0] = z;
219 memcpy(m + 1, v, 32);
220 memcpy(m + 33, u, 32);
221
222 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
223 if (err)
224 return err;
225
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200226 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300227
228 return err;
229}
230
Herbert Xu71af2f62016-01-24 21:18:30 +0800231static int smp_f5(struct crypto_shash *tfm_cmac, const u8 w[32],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200232 const u8 n1[16], const u8 n2[16], const u8 a1[7],
233 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300234{
235 /* The btle, salt and length "magic" values are as defined in
236 * the SMP section of the Bluetooth core specification. In ASCII
237 * the btle value ends up being 'btle'. The salt is just a
238 * random number whereas length is the value 256 in little
239 * endian format.
240 */
241 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
242 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
243 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
244 const u8 length[2] = { 0x00, 0x01 };
245 u8 m[53], t[16];
246 int err;
247
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200248 SMP_DBG("w %32phN", w);
249 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
250 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300251
252 err = aes_cmac(tfm_cmac, salt, w, 32, t);
253 if (err)
254 return err;
255
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200256 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300257
258 memcpy(m, length, 2);
259 memcpy(m + 2, a2, 7);
260 memcpy(m + 9, a1, 7);
261 memcpy(m + 16, n2, 16);
262 memcpy(m + 32, n1, 16);
263 memcpy(m + 48, btle, 4);
264
265 m[52] = 0; /* Counter */
266
267 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
268 if (err)
269 return err;
270
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200271 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300272
273 m[52] = 1; /* Counter */
274
275 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
276 if (err)
277 return err;
278
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200279 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300280
281 return 0;
282}
283
Herbert Xu71af2f62016-01-24 21:18:30 +0800284static int smp_f6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200285 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300286 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
287 u8 res[16])
288{
289 u8 m[65];
290 int err;
291
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200292 SMP_DBG("w %16phN", w);
293 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
294 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300295
296 memcpy(m, a2, 7);
297 memcpy(m + 7, a1, 7);
298 memcpy(m + 14, io_cap, 3);
299 memcpy(m + 17, r, 16);
300 memcpy(m + 33, n2, 16);
301 memcpy(m + 49, n1, 16);
302
303 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
304 if (err)
305 return err;
306
Marcel Holtmann203de212014-12-31 20:01:22 -0800307 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300308
309 return err;
310}
311
Herbert Xu71af2f62016-01-24 21:18:30 +0800312static int smp_g2(struct crypto_shash *tfm_cmac, const u8 u[32], const u8 v[32],
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300313 const u8 x[16], const u8 y[16], u32 *val)
314{
315 u8 m[80], tmp[16];
316 int err;
317
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200318 SMP_DBG("u %32phN", u);
319 SMP_DBG("v %32phN", v);
320 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300321
322 memcpy(m, y, 16);
323 memcpy(m + 16, v, 32);
324 memcpy(m + 48, u, 32);
325
326 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
327 if (err)
328 return err;
329
330 *val = get_unaligned_le32(tmp);
331 *val %= 1000000;
332
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200333 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300334
335 return 0;
336}
337
Herbert Xu71af2f62016-01-24 21:18:30 +0800338static int smp_h6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200339 const u8 key_id[4], u8 res[16])
340{
341 int err;
342
343 SMP_DBG("w %16phN key_id %4phN", w, key_id);
344
345 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
346 if (err)
347 return err;
348
349 SMP_DBG("res %16phN", res);
350
351 return err;
352}
353
Johan Hedberga62da6f2016-12-08 08:32:54 +0200354static int smp_h7(struct crypto_shash *tfm_cmac, const u8 w[16],
355 const u8 salt[16], u8 res[16])
356{
357 int err;
358
359 SMP_DBG("w %16phN salt %16phN", w, salt);
360
361 err = aes_cmac(tfm_cmac, salt, w, 16, res);
362 if (err)
363 return err;
364
365 SMP_DBG("res %16phN", res);
366
367 return err;
368}
369
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200370/* The following functions map to the legacy SMP crypto functions e, c1,
371 * s1 and ah.
372 */
373
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200374static int smp_e(const u8 *k, u8 *r)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300375{
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200376 struct crypto_aes_ctx ctx;
Johan Hedberg943a7322014-03-18 12:58:24 +0200377 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200378 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300379
Johan Hedberg011c3912015-05-19 21:06:04 +0300380 SMP_DBG("k %16phN r %16phN", k, r);
381
Johan Hedberg943a7322014-03-18 12:58:24 +0200382 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300383 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200384
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200385 err = aes_expandkey(&ctx, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300386 if (err) {
387 BT_ERR("cipher setkey failed: %d", err);
388 return err;
389 }
390
Johan Hedberg943a7322014-03-18 12:58:24 +0200391 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300392 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200393
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200394 aes_encrypt(&ctx, data, data);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300395
Johan Hedberg943a7322014-03-18 12:58:24 +0200396 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300397 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200398
Johan Hedberg011c3912015-05-19 21:06:04 +0300399 SMP_DBG("r %16phN", r);
400
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200401 memzero_explicit(&ctx, sizeof (ctx));
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300402 return err;
403}
404
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200405static int smp_c1(const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200406 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
407 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
408{
409 u8 p1[16], p2[16];
410 int err;
411
Johan Hedberg011c3912015-05-19 21:06:04 +0300412 SMP_DBG("k %16phN r %16phN", k, r);
413 SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat, ia, _rat, ra);
414 SMP_DBG("preq %7phN pres %7phN", preq, pres);
415
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200416 memset(p1, 0, 16);
417
418 /* p1 = pres || preq || _rat || _iat */
419 p1[0] = _iat;
420 p1[1] = _rat;
421 memcpy(p1 + 2, preq, 7);
422 memcpy(p1 + 9, pres, 7);
423
Johan Hedberg011c3912015-05-19 21:06:04 +0300424 SMP_DBG("p1 %16phN", p1);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200425
426 /* res = r XOR p1 */
Ard Biesheuvelef0bb5a2021-01-05 17:10:53 +0100427 crypto_xor_cpy(res, r, p1, sizeof(p1));
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200428
429 /* res = e(k, res) */
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200430 err = smp_e(k, res);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200431 if (err) {
432 BT_ERR("Encrypt data error");
433 return err;
434 }
435
Johan Hedberg011c3912015-05-19 21:06:04 +0300436 /* p2 = padding || ia || ra */
437 memcpy(p2, ra, 6);
438 memcpy(p2 + 6, ia, 6);
439 memset(p2 + 12, 0, 4);
440
441 SMP_DBG("p2 %16phN", p2);
442
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200443 /* res = res XOR p2 */
Ard Biesheuvelef0bb5a2021-01-05 17:10:53 +0100444 crypto_xor(res, p2, sizeof(p2));
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200445
446 /* res = e(k, res) */
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200447 err = smp_e(k, res);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200448 if (err)
449 BT_ERR("Encrypt data error");
450
451 return err;
452}
453
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200454static int smp_s1(const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200455 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300456{
457 int err;
458
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200459 /* Just least significant octets from r1 and r2 are considered */
460 memcpy(_r, r2, 8);
461 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300462
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200463 err = smp_e(k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300464 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200465 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300466
467 return err;
468}
469
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200470static int smp_ah(const u8 irk[16], const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200471{
Johan Hedberg943a7322014-03-18 12:58:24 +0200472 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200473 int err;
474
475 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200476 memcpy(_res, r, 3);
477 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200478
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200479 err = smp_e(irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200480 if (err) {
481 BT_ERR("Encrypt error");
482 return err;
483 }
484
485 /* The output of the random address function ah is:
Marcel Holtmannc5080d42015-09-04 17:08:18 +0200486 * ah(k, r) = e(k, r') mod 2^24
Johan Hedberg60478052014-02-18 10:19:31 +0200487 * The output of the security function e is then truncated to 24 bits
488 * by taking the least significant 24 bits of the output of e as the
489 * result of ah.
490 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200491 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200492
493 return 0;
494}
495
Johan Hedbergcd082792014-12-02 13:37:41 +0200496bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
497 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200498{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300499 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedberg60478052014-02-18 10:19:31 +0200500 u8 hash[3];
501 int err;
502
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300503 if (!chan || !chan->data)
504 return false;
505
Marcel Holtmann56860242020-05-06 09:57:50 +0200506 bt_dev_dbg(hdev, "RPA %pMR IRK %*phN", bdaddr, 16, irk);
Johan Hedberg60478052014-02-18 10:19:31 +0200507
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200508 err = smp_ah(irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200509 if (err)
510 return false;
511
Jason A. Donenfeld329d8232017-06-10 04:59:11 +0200512 return !crypto_memneq(bdaddr->b, hash, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200513}
514
Johan Hedbergcd082792014-12-02 13:37:41 +0200515int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200516{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300517 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200518 int err;
519
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300520 if (!chan || !chan->data)
521 return -EOPNOTSUPP;
522
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200523 get_random_bytes(&rpa->b[3], 3);
524
525 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
526 rpa->b[5] |= 0x40; /* Set second most significant bit */
527
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200528 err = smp_ah(irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200529 if (err < 0)
530 return err;
531
Marcel Holtmann56860242020-05-06 09:57:50 +0200532 bt_dev_dbg(hdev, "RPA %pMR", rpa);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200533
534 return 0;
535}
536
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700537int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
538{
539 struct l2cap_chan *chan = hdev->smp_data;
540 struct smp_dev *smp;
541 int err;
542
543 if (!chan || !chan->data)
544 return -EOPNOTSUPP;
545
546 smp = chan->data;
547
548 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Marcel Holtmann56860242020-05-06 09:57:50 +0200549 bt_dev_dbg(hdev, "Using debug keys");
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300550 err = set_ecdh_privkey(smp->tfm_ecdh, debug_sk);
551 if (err)
552 return err;
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700553 memcpy(smp->local_pk, debug_pk, 64);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700554 smp->debug_key = true;
555 } else {
556 while (true) {
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300557 /* Generate key pair for Secure Connections */
558 err = generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk);
Tudor Ambarusa2976412017-09-28 17:14:52 +0300559 if (err)
560 return err;
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700561
562 /* This is unlikely, but we need to check that
563 * we didn't accidentially generate a debug key.
564 */
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300565 if (crypto_memneq(smp->local_pk, debug_pk, 64))
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700566 break;
567 }
568 smp->debug_key = false;
569 }
570
571 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
572 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700573
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700574 get_random_bytes(smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700575
576 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700577 smp->local_rand, 0, hash);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700578 if (err < 0)
579 return err;
580
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700581 memcpy(rand, smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700582
Johan Hedberg94f14e42018-09-11 14:10:12 +0300583 smp->local_oob = true;
584
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700585 return 0;
586}
587
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300588static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
589{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300590 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300591 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300592 struct kvec iv[2];
593 struct msghdr msg;
594
595 if (!chan)
596 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300597
598 BT_DBG("code 0x%2.2x", code);
599
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300600 iv[0].iov_base = &code;
601 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300602
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300603 iv[1].iov_base = data;
604 iv[1].iov_len = len;
605
606 memset(&msg, 0, sizeof(msg));
607
David Howellsaa563d72018-10-20 00:57:56 +0100608 iov_iter_kvec(&msg.msg_iter, WRITE, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300609
610 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300611
Johan Hedbergb68fda62014-08-11 22:06:40 +0300612 if (!chan->data)
613 return;
614
615 smp = chan->data;
616
617 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300618 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300619}
620
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300621static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800622{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300623 if (authreq & SMP_AUTH_MITM) {
624 if (authreq & SMP_AUTH_SC)
625 return BT_SECURITY_FIPS;
626 else
627 return BT_SECURITY_HIGH;
628 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800629 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300630 }
Brian Gix2b64d152011-12-21 16:12:12 -0800631}
632
633static __u8 seclevel_to_authreq(__u8 sec_level)
634{
635 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300636 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800637 case BT_SECURITY_HIGH:
638 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
639 case BT_SECURITY_MEDIUM:
640 return SMP_AUTH_BONDING;
641 default:
642 return SMP_AUTH_NONE;
643 }
644}
645
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300646static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700647 struct smp_cmd_pairing *req,
648 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300649{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300650 struct l2cap_chan *chan = conn->smp;
651 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200652 struct hci_conn *hcon = conn->hcon;
653 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100654 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300655
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700656 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700657 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
658 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300659 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800660 } else {
661 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300662 }
663
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700664 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200665 remote_dist |= SMP_DIST_ID_KEY;
666
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700667 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200668 local_dist |= SMP_DIST_ID_KEY;
669
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700670 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100671 (authreq & SMP_AUTH_SC)) {
672 struct oob_data *oob_data;
673 u8 bdaddr_type;
674
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700675 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300676 local_dist |= SMP_DIST_LINK_KEY;
677 remote_dist |= SMP_DIST_LINK_KEY;
678 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100679
680 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
681 bdaddr_type = BDADDR_LE_PUBLIC;
682 else
683 bdaddr_type = BDADDR_LE_RANDOM;
684
685 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
686 bdaddr_type);
Marcel Holtmann4775a4e2015-01-31 00:15:52 -0800687 if (oob_data && oob_data->present) {
Johan Hedberg1a8bab42015-03-16 11:45:44 +0200688 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100689 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100690 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100691 memcpy(smp->pcnf, oob_data->hash256, 16);
Marcel Holtmannbc07cd62015-03-16 12:34:56 -0700692 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
693 SMP_DBG("OOB Remote Random: %16phN", smp->rr);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100694 }
695
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300696 } else {
697 authreq &= ~SMP_AUTH_SC;
698 }
699
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300700 if (rsp == NULL) {
701 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100702 req->oob_flag = oob_flag;
Matias Karhumaa30d65e02018-09-28 21:54:30 +0300703 req->max_key_size = hdev->le_max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200704 req->init_key_dist = local_dist;
705 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300706 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200707
708 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300709 return;
710 }
711
712 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100713 rsp->oob_flag = oob_flag;
Matias Karhumaa30d65e02018-09-28 21:54:30 +0300714 rsp->max_key_size = hdev->le_max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200715 rsp->init_key_dist = req->init_key_dist & remote_dist;
716 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300717 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200718
719 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300720}
721
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300722static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
723{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300724 struct l2cap_chan *chan = conn->smp;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300725 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300726 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300727
Łukasz Rymanowski49c06c92020-05-13 10:18:53 +0200728 if (conn->hcon->pending_sec_level == BT_SECURITY_FIPS &&
729 max_key_size != SMP_MAX_ENC_KEY_SIZE)
730 return SMP_ENC_KEY_SIZE;
731
Matias Karhumaa30d65e02018-09-28 21:54:30 +0300732 if (max_key_size > hdev->le_max_key_size ||
Johan Hedberg2fd36552015-06-11 13:52:26 +0300733 max_key_size < SMP_MIN_ENC_KEY_SIZE)
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300734 return SMP_ENC_KEY_SIZE;
735
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300736 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300737
738 return 0;
739}
740
Johan Hedberg6f48e262014-08-11 22:06:44 +0300741static void smp_chan_destroy(struct l2cap_conn *conn)
742{
743 struct l2cap_chan *chan = conn->smp;
744 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200745 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300746 bool complete;
747
748 BUG_ON(!smp);
749
750 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300751
Johan Hedberg6f48e262014-08-11 22:06:44 +0300752 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200753 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300754
Waiman Long453431a2020-08-06 23:18:13 -0700755 kfree_sensitive(smp->csrk);
756 kfree_sensitive(smp->slave_csrk);
757 kfree_sensitive(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300758
Herbert Xu71af2f62016-01-24 21:18:30 +0800759 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +0300760 crypto_free_kpp(smp->tfm_ecdh);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300761
Johan Hedberg923e2412014-12-03 12:43:39 +0200762 /* Ensure that we don't leave any debug key around if debug key
763 * support hasn't been explicitly enabled.
764 */
765 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700766 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200767 list_del_rcu(&smp->ltk->list);
768 kfree_rcu(smp->ltk, rcu);
769 smp->ltk = NULL;
770 }
771
Johan Hedberg6f48e262014-08-11 22:06:44 +0300772 /* If pairing failed clean up any keys we might have */
773 if (!complete) {
774 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200775 list_del_rcu(&smp->ltk->list);
776 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300777 }
778
779 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200780 list_del_rcu(&smp->slave_ltk->list);
781 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300782 }
783
784 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200785 list_del_rcu(&smp->remote_irk->list);
786 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300787 }
788 }
789
790 chan->data = NULL;
Waiman Long453431a2020-08-06 23:18:13 -0700791 kfree_sensitive(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200792 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300793}
794
Johan Hedberg84794e12013-11-06 11:24:57 +0200795static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800796{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200797 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300798 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200799
Johan Hedberg84794e12013-11-06 11:24:57 +0200800 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800801 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700802 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800803
Johan Hedberge1e930f2014-09-08 17:09:49 -0700804 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300805
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300806 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300807 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800808}
809
Brian Gix2b64d152011-12-21 16:12:12 -0800810#define JUST_WORKS 0x00
811#define JUST_CFM 0x01
812#define REQ_PASSKEY 0x02
813#define CFM_PASSKEY 0x03
814#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300815#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800816#define OVERLAP 0xFF
817
818static const u8 gen_method[5][5] = {
819 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
820 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
821 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
822 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
823 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
824};
825
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300826static const u8 sc_method[5][5] = {
827 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
828 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
829 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
830 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
831 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
832};
833
Johan Hedberg581370c2014-06-17 13:07:38 +0300834static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
835{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300836 /* If either side has unknown io_caps, use JUST_CFM (which gets
837 * converted later to JUST_WORKS if we're initiators.
838 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300839 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
840 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300841 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300842
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300843 if (test_bit(SMP_FLAG_SC, &smp->flags))
844 return sc_method[remote_io][local_io];
845
Johan Hedberg581370c2014-06-17 13:07:38 +0300846 return gen_method[remote_io][local_io];
847}
848
Brian Gix2b64d152011-12-21 16:12:12 -0800849static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
850 u8 local_io, u8 remote_io)
851{
852 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300853 struct l2cap_chan *chan = conn->smp;
854 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800855 u32 passkey = 0;
Guenter Roeckd1d900f2020-04-06 11:54:38 -0700856 int ret;
Brian Gix2b64d152011-12-21 16:12:12 -0800857
858 /* Initialize key for JUST WORKS */
859 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300860 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800861
862 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
863
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300864 /* If neither side wants MITM, either "just" confirm an incoming
865 * request or use just-works for outgoing ones. The JUST_CFM
866 * will be converted to JUST_WORKS if necessary later in this
867 * function. If either side has MITM look up the method from the
868 * table.
869 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300870 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300871 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800872 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300873 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800874
Johan Hedberga82505c2014-03-24 14:39:07 +0200875 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300876 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
877 &smp->flags))
878 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200879
Johan Hedberg02f3e252014-07-16 15:09:13 +0300880 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300881 if (smp->method == JUST_CFM &&
882 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
883 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300884
Sonny Sasaka92516cd2020-03-27 17:34:23 -0700885 /* If Just Works, Continue with Zero TK and ask user-space for
886 * confirmation */
Johan Hedberg783e0572014-05-31 18:48:26 +0300887 if (smp->method == JUST_WORKS) {
Guenter Roeckd1d900f2020-04-06 11:54:38 -0700888 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
Sonny Sasaka92516cd2020-03-27 17:34:23 -0700889 hcon->type,
890 hcon->dst_type,
891 passkey, 1);
Guenter Roeckd1d900f2020-04-06 11:54:38 -0700892 if (ret)
893 return ret;
Sonny Sasaka92516cd2020-03-27 17:34:23 -0700894 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800895 return 0;
896 }
897
Johan Hedberg19c5ce92015-03-15 19:34:04 +0200898 /* If this function is used for SC -> legacy fallback we
899 * can only recover the just-works case.
900 */
901 if (test_bit(SMP_FLAG_SC, &smp->flags))
902 return -EINVAL;
903
Brian Gix2b64d152011-12-21 16:12:12 -0800904 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300905 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300906 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300907 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
908 hcon->pending_sec_level = BT_SECURITY_HIGH;
909 }
Brian Gix2b64d152011-12-21 16:12:12 -0800910
911 /* If both devices have Keyoard-Display I/O, the master
912 * Confirms and the slave Enters the passkey.
913 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300914 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300915 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300916 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800917 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300918 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800919 }
920
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200921 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300922 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200923 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800924 get_random_bytes(&passkey, sizeof(passkey));
925 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200926 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800927 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300928 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800929 }
930
Johan Hedberg783e0572014-05-31 18:48:26 +0300931 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700932 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200933 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300934 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200935 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
936 hcon->type, hcon->dst_type,
937 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800938 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200939 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200940 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200941 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800942
Brian Gix2b64d152011-12-21 16:12:12 -0800943 return ret;
944}
945
Johan Hedberg1cc61142014-05-20 09:45:52 +0300946static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300947{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300948 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300949 struct smp_cmd_pairing_confirm cp;
950 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300951
952 BT_DBG("conn %p", conn);
953
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200954 ret = smp_c1(smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200955 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200956 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
957 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300958 if (ret)
959 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300960
Johan Hedberg4a74d652014-05-20 09:45:50 +0300961 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800962
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300963 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
964
Johan Hedbergb28b4942014-09-05 22:19:55 +0300965 if (conn->hcon->out)
966 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
967 else
968 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
969
Johan Hedberg1cc61142014-05-20 09:45:52 +0300970 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300971}
972
Johan Hedberg861580a2014-05-20 09:45:51 +0300973static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300974{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300975 struct l2cap_conn *conn = smp->conn;
976 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300977 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300978 int ret;
979
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300980 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
981
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200982 ret = smp_c1(smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200983 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200984 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300985 if (ret)
986 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300987
Jason A. Donenfeld329d8232017-06-10 04:59:11 +0200988 if (crypto_memneq(smp->pcnf, confirm, sizeof(smp->pcnf))) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100989 bt_dev_err(hcon->hdev, "pairing failed "
990 "(confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300991 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300992 }
993
994 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800995 u8 stk[16];
996 __le64 rand = 0;
997 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300998
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200999 smp_s1(smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001000
Johan Hedberg861580a2014-05-20 09:45:51 +03001001 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1002 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001003
Johan Hedberg8b76ce32015-06-08 18:14:39 +03001004 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -03001005 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +03001006 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001007 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +03001008 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001009 __le64 rand = 0;
1010 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001011
Johan Hedberg943a7322014-03-18 12:58:24 +02001012 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1013 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001014
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02001015 smp_s1(smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001016
Johan Hedbergfff34902014-06-10 15:19:50 +03001017 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1018 auth = 1;
1019 else
1020 auth = 0;
1021
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001022 /* Even though there's no _SLAVE suffix this is the
1023 * slave STK we're adding for later lookup (the master
1024 * STK never needs to be stored).
1025 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001026 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001027 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001028 }
1029
Johan Hedberg861580a2014-05-20 09:45:51 +03001030 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001031}
1032
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001033static void smp_notify_keys(struct l2cap_conn *conn)
1034{
1035 struct l2cap_chan *chan = conn->smp;
1036 struct smp_chan *smp = chan->data;
1037 struct hci_conn *hcon = conn->hcon;
1038 struct hci_dev *hdev = hcon->hdev;
1039 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1040 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1041 bool persistent;
1042
Johan Hedbergcad20c22015-10-12 13:36:19 +02001043 if (hcon->type == ACL_LINK) {
1044 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1045 persistent = false;
1046 else
1047 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1048 &hcon->flags);
1049 } else {
1050 /* The LTKs, IRKs and CSRKs should be persistent only if
1051 * both sides had the bonding bit set in their
1052 * authentication requests.
1053 */
1054 persistent = !!((req->auth_req & rsp->auth_req) &
1055 SMP_AUTH_BONDING);
1056 }
1057
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001058 if (smp->remote_irk) {
Johan Hedbergcad20c22015-10-12 13:36:19 +02001059 mgmt_new_irk(hdev, smp->remote_irk, persistent);
1060
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001061 /* Now that user space can be considered to know the
1062 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001063 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001064 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001065 if (hcon->type == LE_LINK) {
1066 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1067 hcon->dst_type = smp->remote_irk->addr_type;
1068 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1069 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001070 }
1071
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001072 if (smp->csrk) {
1073 smp->csrk->bdaddr_type = hcon->dst_type;
1074 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1075 mgmt_new_csrk(hdev, smp->csrk, persistent);
1076 }
1077
1078 if (smp->slave_csrk) {
1079 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1080 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1081 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1082 }
1083
1084 if (smp->ltk) {
1085 smp->ltk->bdaddr_type = hcon->dst_type;
1086 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1087 mgmt_new_ltk(hdev, smp->ltk, persistent);
1088 }
1089
1090 if (smp->slave_ltk) {
1091 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1092 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1093 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1094 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001095
1096 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001097 struct link_key *key;
1098 u8 type;
1099
1100 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1101 type = HCI_LK_DEBUG_COMBINATION;
1102 else if (hcon->sec_level == BT_SECURITY_FIPS)
1103 type = HCI_LK_AUTH_COMBINATION_P256;
1104 else
1105 type = HCI_LK_UNAUTH_COMBINATION_P256;
1106
1107 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1108 smp->link_key, type, 0, &persistent);
1109 if (key) {
1110 mgmt_new_link_key(hdev, key, persistent);
1111
1112 /* Don't keep debug keys around if the relevant
1113 * flag is not set.
1114 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001115 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001116 key->type == HCI_LK_DEBUG_COMBINATION) {
1117 list_del_rcu(&key->list);
1118 kfree_rcu(key, rcu);
1119 }
1120 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001121 }
1122}
1123
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001124static void sc_add_ltk(struct smp_chan *smp)
1125{
1126 struct hci_conn *hcon = smp->conn->hcon;
1127 u8 key_type, auth;
1128
1129 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1130 key_type = SMP_LTK_P256_DEBUG;
1131 else
1132 key_type = SMP_LTK_P256;
1133
1134 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1135 auth = 1;
1136 else
1137 auth = 0;
1138
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001139 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1140 key_type, auth, smp->tk, smp->enc_key_size,
1141 0, 0);
1142}
1143
Johan Hedberg6a770832014-06-06 11:54:04 +03001144static void sc_generate_link_key(struct smp_chan *smp)
1145{
Johan Hedberga62da6f2016-12-08 08:32:54 +02001146 /* From core spec. Spells out in ASCII as 'lebr'. */
Johan Hedberg6a770832014-06-06 11:54:04 +03001147 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1148
1149 smp->link_key = kzalloc(16, GFP_KERNEL);
1150 if (!smp->link_key)
1151 return;
1152
Johan Hedberga62da6f2016-12-08 08:32:54 +02001153 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
Christophe JAILLET151129d2020-01-27 23:36:09 +01001154 /* SALT = 0x000000000000000000000000746D7031 */
Johan Hedberga62da6f2016-12-08 08:32:54 +02001155 const u8 salt[16] = { 0x31, 0x70, 0x6d, 0x74 };
1156
1157 if (smp_h7(smp->tfm_cmac, smp->tk, salt, smp->link_key)) {
Waiman Long453431a2020-08-06 23:18:13 -07001158 kfree_sensitive(smp->link_key);
Johan Hedberga62da6f2016-12-08 08:32:54 +02001159 smp->link_key = NULL;
1160 return;
1161 }
1162 } else {
1163 /* From core spec. Spells out in ASCII as 'tmp1'. */
1164 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1165
1166 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
Waiman Long453431a2020-08-06 23:18:13 -07001167 kfree_sensitive(smp->link_key);
Johan Hedberga62da6f2016-12-08 08:32:54 +02001168 smp->link_key = NULL;
1169 return;
1170 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001171 }
1172
1173 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Waiman Long453431a2020-08-06 23:18:13 -07001174 kfree_sensitive(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001175 smp->link_key = NULL;
1176 return;
1177 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001178}
1179
Johan Hedbergb28b4942014-09-05 22:19:55 +03001180static void smp_allow_key_dist(struct smp_chan *smp)
1181{
1182 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1183 * will be allowed in each PDU handler to ensure we receive
1184 * them in the correct order.
1185 */
1186 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1187 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1188 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1189 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1190 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1191 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1192}
1193
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001194static void sc_generate_ltk(struct smp_chan *smp)
1195{
Johan Hedberga62da6f2016-12-08 08:32:54 +02001196 /* From core spec. Spells out in ASCII as 'brle'. */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001197 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1198 struct hci_conn *hcon = smp->conn->hcon;
1199 struct hci_dev *hdev = hcon->hdev;
1200 struct link_key *key;
1201
1202 key = hci_find_link_key(hdev, &hcon->dst);
1203 if (!key) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001204 bt_dev_err(hdev, "no Link Key found to generate LTK");
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001205 return;
1206 }
1207
1208 if (key->type == HCI_LK_DEBUG_COMBINATION)
1209 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1210
Johan Hedberga62da6f2016-12-08 08:32:54 +02001211 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
Christophe JAILLET151129d2020-01-27 23:36:09 +01001212 /* SALT = 0x000000000000000000000000746D7032 */
Johan Hedberga62da6f2016-12-08 08:32:54 +02001213 const u8 salt[16] = { 0x32, 0x70, 0x6d, 0x74 };
1214
1215 if (smp_h7(smp->tfm_cmac, key->val, salt, smp->tk))
1216 return;
1217 } else {
1218 /* From core spec. Spells out in ASCII as 'tmp2'. */
1219 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1220
1221 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1222 return;
1223 }
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001224
1225 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1226 return;
1227
1228 sc_add_ltk(smp);
1229}
1230
Johan Hedbergd6268e82014-09-05 22:19:51 +03001231static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001232{
1233 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001234 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001235 struct hci_conn *hcon = conn->hcon;
1236 struct hci_dev *hdev = hcon->hdev;
1237 __u8 *keydist;
1238
1239 BT_DBG("conn %p", conn);
1240
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001241 rsp = (void *) &smp->prsp[1];
1242
1243 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001244 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1245 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001246 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001247 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001248
1249 req = (void *) &smp->preq[1];
1250
1251 if (hcon->out) {
1252 keydist = &rsp->init_key_dist;
1253 *keydist &= req->init_key_dist;
1254 } else {
1255 keydist = &rsp->resp_key_dist;
1256 *keydist &= req->resp_key_dist;
1257 }
1258
Johan Hedberg6a770832014-06-06 11:54:04 +03001259 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001260 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001261 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001262 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1263 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001264
1265 /* Clear the keys which are generated but not distributed */
1266 *keydist &= ~SMP_SC_NO_DIST;
1267 }
1268
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001269 BT_DBG("keydist 0x%x", *keydist);
1270
1271 if (*keydist & SMP_DIST_ENC_KEY) {
1272 struct smp_cmd_encrypt_info enc;
1273 struct smp_cmd_master_ident ident;
1274 struct smp_ltk *ltk;
1275 u8 authenticated;
1276 __le16 ediv;
1277 __le64 rand;
1278
Johan Hedberg1fc62c52015-06-10 11:11:20 +03001279 /* Make sure we generate only the significant amount of
1280 * bytes based on the encryption key size, and set the rest
1281 * of the value to zeroes.
1282 */
1283 get_random_bytes(enc.ltk, smp->enc_key_size);
1284 memset(enc.ltk + smp->enc_key_size, 0,
1285 sizeof(enc.ltk) - smp->enc_key_size);
1286
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001287 get_random_bytes(&ediv, sizeof(ediv));
1288 get_random_bytes(&rand, sizeof(rand));
1289
1290 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1291
1292 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1293 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1294 SMP_LTK_SLAVE, authenticated, enc.ltk,
1295 smp->enc_key_size, ediv, rand);
1296 smp->slave_ltk = ltk;
1297
1298 ident.ediv = ediv;
1299 ident.rand = rand;
1300
1301 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1302
1303 *keydist &= ~SMP_DIST_ENC_KEY;
1304 }
1305
1306 if (*keydist & SMP_DIST_ID_KEY) {
1307 struct smp_cmd_ident_addr_info addrinfo;
1308 struct smp_cmd_ident_info idinfo;
1309
1310 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1311
1312 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1313
1314 /* The hci_conn contains the local identity address
1315 * after the connection has been established.
1316 *
1317 * This is true even when the connection has been
1318 * established using a resolvable random address.
1319 */
1320 bacpy(&addrinfo.bdaddr, &hcon->src);
1321 addrinfo.addr_type = hcon->src_type;
1322
1323 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1324 &addrinfo);
1325
1326 *keydist &= ~SMP_DIST_ID_KEY;
1327 }
1328
1329 if (*keydist & SMP_DIST_SIGN) {
1330 struct smp_cmd_sign_info sign;
1331 struct smp_csrk *csrk;
1332
1333 /* Generate a new random key */
1334 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1335
1336 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1337 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001338 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1339 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1340 else
1341 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001342 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1343 }
1344 smp->slave_csrk = csrk;
1345
1346 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1347
1348 *keydist &= ~SMP_DIST_SIGN;
1349 }
1350
1351 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001352 if (smp->remote_key_dist & KEY_DIST_MASK) {
1353 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001354 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001355 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001356
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001357 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1358 smp_notify_keys(conn);
1359
1360 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001361}
1362
Johan Hedbergb68fda62014-08-11 22:06:40 +03001363static void smp_timeout(struct work_struct *work)
1364{
1365 struct smp_chan *smp = container_of(work, struct smp_chan,
1366 security_timer.work);
1367 struct l2cap_conn *conn = smp->conn;
1368
1369 BT_DBG("conn %p", conn);
1370
Johan Hedberg1e91c292014-08-18 20:33:29 +03001371 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001372}
1373
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001374static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1375{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001376 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001377 struct smp_chan *smp;
1378
Marcel Holtmannf1560462013-10-13 05:43:25 -07001379 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001380 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001381 return NULL;
1382
Herbert Xu71af2f62016-01-24 21:18:30 +08001383 smp->tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001384 if (IS_ERR(smp->tfm_cmac)) {
1385 BT_ERR("Unable to create CMAC crypto context");
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02001386 goto zfree_smp;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001387 }
1388
Herbert Xu075f7732020-07-31 17:41:58 +10001389 smp->tfm_ecdh = crypto_alloc_kpp("ecdh", 0, 0);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001390 if (IS_ERR(smp->tfm_ecdh)) {
1391 BT_ERR("Unable to create ECDH crypto context");
1392 goto free_shash;
Johan Hedberg407cecf2014-05-02 14:19:47 +03001393 }
1394
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001395 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001396 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001397
Johan Hedbergb28b4942014-09-05 22:19:55 +03001398 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1399
Johan Hedbergb68fda62014-08-11 22:06:40 +03001400 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1401
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001402 hci_conn_hold(conn->hcon);
1403
1404 return smp;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001405
1406free_shash:
1407 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001408zfree_smp:
Waiman Long453431a2020-08-06 23:18:13 -07001409 kfree_sensitive(smp);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001410 return NULL;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001411}
1412
Johan Hedberg760b0182014-06-06 11:44:05 +03001413static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1414{
1415 struct hci_conn *hcon = smp->conn->hcon;
1416 u8 *na, *nb, a[7], b[7];
1417
1418 if (hcon->out) {
1419 na = smp->prnd;
1420 nb = smp->rrnd;
1421 } else {
1422 na = smp->rrnd;
1423 nb = smp->prnd;
1424 }
1425
1426 memcpy(a, &hcon->init_addr, 6);
1427 memcpy(b, &hcon->resp_addr, 6);
1428 a[6] = hcon->init_addr_type;
1429 b[6] = hcon->resp_addr_type;
1430
1431 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1432}
1433
Johan Hedberg38606f12014-06-25 11:10:28 +03001434static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001435{
1436 struct hci_conn *hcon = smp->conn->hcon;
1437 struct smp_cmd_dhkey_check check;
1438 u8 a[7], b[7], *local_addr, *remote_addr;
1439 u8 io_cap[3], r[16];
1440
Johan Hedberg760b0182014-06-06 11:44:05 +03001441 memcpy(a, &hcon->init_addr, 6);
1442 memcpy(b, &hcon->resp_addr, 6);
1443 a[6] = hcon->init_addr_type;
1444 b[6] = hcon->resp_addr_type;
1445
1446 if (hcon->out) {
1447 local_addr = a;
1448 remote_addr = b;
1449 memcpy(io_cap, &smp->preq[1], 3);
1450 } else {
1451 local_addr = b;
1452 remote_addr = a;
1453 memcpy(io_cap, &smp->prsp[1], 3);
1454 }
1455
Johan Hedbergdddd3052014-06-01 15:38:09 +03001456 memset(r, 0, sizeof(r));
1457
1458 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001459 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001460
Johan Hedberga29b0732014-10-28 15:17:05 +01001461 if (smp->method == REQ_OOB)
1462 memcpy(r, smp->rr, 16);
1463
Johan Hedberg760b0182014-06-06 11:44:05 +03001464 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1465 local_addr, remote_addr, check.e);
1466
1467 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001468}
1469
Johan Hedberg38606f12014-06-25 11:10:28 +03001470static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1471{
1472 struct l2cap_conn *conn = smp->conn;
1473 struct hci_conn *hcon = conn->hcon;
1474 struct smp_cmd_pairing_confirm cfm;
1475 u8 r;
1476
1477 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1478 r |= 0x80;
1479
1480 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1481
1482 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1483 cfm.confirm_val))
1484 return SMP_UNSPECIFIED;
1485
1486 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1487
1488 return 0;
1489}
1490
1491static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1492{
1493 struct l2cap_conn *conn = smp->conn;
1494 struct hci_conn *hcon = conn->hcon;
1495 struct hci_dev *hdev = hcon->hdev;
1496 u8 cfm[16], r;
1497
1498 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1499 if (smp->passkey_round >= 20)
1500 return 0;
1501
1502 switch (smp_op) {
1503 case SMP_CMD_PAIRING_RANDOM:
1504 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1505 r |= 0x80;
1506
1507 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1508 smp->rrnd, r, cfm))
1509 return SMP_UNSPECIFIED;
1510
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02001511 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg38606f12014-06-25 11:10:28 +03001512 return SMP_CONFIRM_FAILED;
1513
1514 smp->passkey_round++;
1515
1516 if (smp->passkey_round == 20) {
1517 /* Generate MacKey and LTK */
1518 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1519 return SMP_UNSPECIFIED;
1520 }
1521
1522 /* The round is only complete when the initiator
1523 * receives pairing random.
1524 */
1525 if (!hcon->out) {
1526 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1527 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001528 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001529 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001530 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001531 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001532 return 0;
1533 }
1534
1535 /* Start the next round */
1536 if (smp->passkey_round != 20)
1537 return sc_passkey_round(smp, 0);
1538
1539 /* Passkey rounds are complete - start DHKey Check */
1540 sc_dhkey_check(smp);
1541 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1542
1543 break;
1544
1545 case SMP_CMD_PAIRING_CONFIRM:
1546 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1547 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1548 return 0;
1549 }
1550
1551 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1552
1553 if (hcon->out) {
1554 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1555 sizeof(smp->prnd), smp->prnd);
1556 return 0;
1557 }
1558
1559 return sc_passkey_send_confirm(smp);
1560
1561 case SMP_CMD_PUBLIC_KEY:
1562 default:
1563 /* Initiating device starts the round */
1564 if (!hcon->out)
1565 return 0;
1566
1567 BT_DBG("%s Starting passkey round %u", hdev->name,
1568 smp->passkey_round + 1);
1569
1570 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1571
1572 return sc_passkey_send_confirm(smp);
1573 }
1574
1575 return 0;
1576}
1577
Johan Hedbergdddd3052014-06-01 15:38:09 +03001578static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1579{
Johan Hedberg38606f12014-06-25 11:10:28 +03001580 struct l2cap_conn *conn = smp->conn;
1581 struct hci_conn *hcon = conn->hcon;
1582 u8 smp_op;
1583
1584 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1585
Johan Hedbergdddd3052014-06-01 15:38:09 +03001586 switch (mgmt_op) {
1587 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1588 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1589 return 0;
1590 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1591 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1592 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001593 case MGMT_OP_USER_PASSKEY_REPLY:
1594 hcon->passkey_notify = le32_to_cpu(passkey);
1595 smp->passkey_round = 0;
1596
1597 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1598 smp_op = SMP_CMD_PAIRING_CONFIRM;
1599 else
1600 smp_op = 0;
1601
1602 if (sc_passkey_round(smp, smp_op))
1603 return -EIO;
1604
1605 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001606 }
1607
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001608 /* Initiator sends DHKey check first */
1609 if (hcon->out) {
1610 sc_dhkey_check(smp);
1611 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1612 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1613 sc_dhkey_check(smp);
1614 sc_add_ltk(smp);
1615 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001616
1617 return 0;
1618}
1619
Brian Gix2b64d152011-12-21 16:12:12 -08001620int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1621{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001622 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001623 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001624 struct smp_chan *smp;
1625 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001626 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001627
1628 BT_DBG("");
1629
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001630 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001631 return -ENOTCONN;
1632
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001633 chan = conn->smp;
1634 if (!chan)
1635 return -ENOTCONN;
1636
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001637 l2cap_chan_lock(chan);
1638 if (!chan->data) {
1639 err = -ENOTCONN;
1640 goto unlock;
1641 }
1642
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001643 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001644
Johan Hedberg760b0182014-06-06 11:44:05 +03001645 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1646 err = sc_user_reply(smp, mgmt_op, passkey);
1647 goto unlock;
1648 }
1649
Brian Gix2b64d152011-12-21 16:12:12 -08001650 switch (mgmt_op) {
1651 case MGMT_OP_USER_PASSKEY_REPLY:
1652 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001653 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001654 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001655 put_unaligned_le32(value, smp->tk);
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05001656 fallthrough;
Brian Gix2b64d152011-12-21 16:12:12 -08001657 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001658 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001659 break;
1660 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1661 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001662 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001663 err = 0;
1664 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001665 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001666 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001667 err = -EOPNOTSUPP;
1668 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001669 }
1670
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001671 err = 0;
1672
Brian Gix2b64d152011-12-21 16:12:12 -08001673 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001674 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1675 u8 rsp = smp_confirm(smp);
1676 if (rsp)
1677 smp_failure(conn, rsp);
1678 }
Brian Gix2b64d152011-12-21 16:12:12 -08001679
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001680unlock:
1681 l2cap_chan_unlock(chan);
1682 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001683}
1684
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001685static void build_bredr_pairing_cmd(struct smp_chan *smp,
1686 struct smp_cmd_pairing *req,
1687 struct smp_cmd_pairing *rsp)
1688{
1689 struct l2cap_conn *conn = smp->conn;
1690 struct hci_dev *hdev = conn->hcon->hdev;
1691 u8 local_dist = 0, remote_dist = 0;
1692
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001693 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001694 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1695 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1696 }
1697
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001698 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001699 remote_dist |= SMP_DIST_ID_KEY;
1700
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001701 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001702 local_dist |= SMP_DIST_ID_KEY;
1703
1704 if (!rsp) {
1705 memset(req, 0, sizeof(*req));
1706
Johan Hedberga62da6f2016-12-08 08:32:54 +02001707 req->auth_req = SMP_AUTH_CT2;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001708 req->init_key_dist = local_dist;
1709 req->resp_key_dist = remote_dist;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001710 req->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001711
1712 smp->remote_key_dist = remote_dist;
1713
1714 return;
1715 }
1716
1717 memset(rsp, 0, sizeof(*rsp));
1718
Johan Hedberga62da6f2016-12-08 08:32:54 +02001719 rsp->auth_req = SMP_AUTH_CT2;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001720 rsp->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001721 rsp->init_key_dist = req->init_key_dist & remote_dist;
1722 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1723
1724 smp->remote_key_dist = rsp->init_key_dist;
1725}
1726
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001727static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001728{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001729 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001730 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001731 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001732 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001733 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001734 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001735
1736 BT_DBG("conn %p", conn);
1737
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001738 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001739 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001740
Johan Hedberg40bef302014-07-16 11:42:27 +03001741 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001742 return SMP_CMD_NOTSUPP;
1743
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001744 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001745 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001746 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001747 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001748
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001749 if (!smp)
1750 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001751
Johan Hedbergc05b9332014-09-10 17:37:42 -07001752 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001753 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001754
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001755 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001756 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001757 return SMP_PAIRING_NOTSUPP;
1758
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001759 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001760 return SMP_AUTH_REQUIREMENTS;
1761
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001762 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1763 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001764 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001765
Johan Hedbergcb06d362015-03-16 21:12:34 +02001766 /* If the remote side's OOB flag is set it means it has
1767 * successfully received our local OOB data - therefore set the
1768 * flag to indicate that local OOB is in use.
1769 */
Johan Hedberg94f14e42018-09-11 14:10:12 +03001770 if (req->oob_flag == SMP_OOB_PRESENT && SMP_DEV(hdev)->local_oob)
Johan Hedberg58428562015-03-16 11:45:45 +02001771 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1772
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001773 /* SMP over BR/EDR requires special treatment */
1774 if (conn->hcon->type == ACL_LINK) {
1775 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001776 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001777 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001778 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1779
1780 set_bit(SMP_FLAG_SC, &smp->flags);
1781
1782 build_bredr_pairing_cmd(smp, req, &rsp);
1783
Johan Hedberga62da6f2016-12-08 08:32:54 +02001784 if (req->auth_req & SMP_AUTH_CT2)
1785 set_bit(SMP_FLAG_CT2, &smp->flags);
1786
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001787 key_size = min(req->max_key_size, rsp.max_key_size);
1788 if (check_enc_key_size(conn, key_size))
1789 return SMP_ENC_KEY_SIZE;
1790
1791 /* Clear bits which are generated but not distributed */
1792 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1793
1794 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1795 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1796 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1797
1798 smp_distribute_keys(smp);
1799 return 0;
1800 }
1801
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001802 build_pairing_cmd(conn, req, &rsp, auth);
1803
Johan Hedberga62da6f2016-12-08 08:32:54 +02001804 if (rsp.auth_req & SMP_AUTH_SC) {
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001805 set_bit(SMP_FLAG_SC, &smp->flags);
1806
Johan Hedberga62da6f2016-12-08 08:32:54 +02001807 if (rsp.auth_req & SMP_AUTH_CT2)
1808 set_bit(SMP_FLAG_CT2, &smp->flags);
1809 }
1810
Johan Hedberg5be5e272014-09-10 17:58:54 -07001811 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001812 sec_level = BT_SECURITY_MEDIUM;
1813 else
1814 sec_level = authreq_to_seclevel(auth);
1815
Johan Hedbergc7262e72014-06-17 13:07:37 +03001816 if (sec_level > conn->hcon->pending_sec_level)
1817 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001818
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001819 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001820 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1821 u8 method;
1822
1823 method = get_auth_method(smp, conn->hcon->io_capability,
1824 req->io_capability);
1825 if (method == JUST_WORKS || method == JUST_CFM)
1826 return SMP_AUTH_REQUIREMENTS;
1827 }
1828
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001829 key_size = min(req->max_key_size, rsp.max_key_size);
1830 if (check_enc_key_size(conn, key_size))
1831 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001832
Johan Hedberge84a6b12013-12-02 10:49:03 +02001833 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001834
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001835 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1836 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001837
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001838 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001839
1840 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1841
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001842 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1843 * SC case, however some implementations incorrectly copy RFU auth
1844 * req bits from our security request, which may create a false
1845 * positive SC enablement.
1846 */
1847 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1848
Johan Hedberg3b191462014-06-06 10:50:15 +03001849 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1850 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1851 /* Clear bits which are generated but not distributed */
1852 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1853 /* Wait for Public Key from Initiating Device */
1854 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001855 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001856
Brian Gix2b64d152011-12-21 16:12:12 -08001857 /* Request setup of TK */
1858 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1859 if (ret)
1860 return SMP_UNSPECIFIED;
1861
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001862 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001863}
1864
Johan Hedberg3b191462014-06-06 10:50:15 +03001865static u8 sc_send_public_key(struct smp_chan *smp)
1866{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001867 struct hci_dev *hdev = smp->conn->hcon->hdev;
1868
Marcel Holtmann56860242020-05-06 09:57:50 +02001869 bt_dev_dbg(hdev, "");
Johan Hedberg3b191462014-06-06 10:50:15 +03001870
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001871 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001872 struct l2cap_chan *chan = hdev->smp_data;
1873 struct smp_dev *smp_dev;
1874
1875 if (!chan || !chan->data)
1876 return SMP_UNSPECIFIED;
1877
1878 smp_dev = chan->data;
1879
1880 memcpy(smp->local_pk, smp_dev->local_pk, 64);
Marcel Holtmannfb334fe2015-03-16 12:34:57 -07001881 memcpy(smp->lr, smp_dev->local_rand, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001882
1883 if (smp_dev->debug_key)
1884 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1885
1886 goto done;
1887 }
1888
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001889 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001890 BT_DBG("Using debug keys");
Tudor Ambarusc0153b02017-09-28 17:14:55 +03001891 if (set_ecdh_privkey(smp->tfm_ecdh, debug_sk))
1892 return SMP_UNSPECIFIED;
Johan Hedberg70157ef2014-06-24 15:22:59 +03001893 memcpy(smp->local_pk, debug_pk, 64);
Johan Hedberg70157ef2014-06-24 15:22:59 +03001894 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1895 } else {
1896 while (true) {
Tudor Ambarusc0153b02017-09-28 17:14:55 +03001897 /* Generate key pair for Secure Connections */
1898 if (generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk))
Johan Hedberg70157ef2014-06-24 15:22:59 +03001899 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001900
Johan Hedberg70157ef2014-06-24 15:22:59 +03001901 /* This is unlikely, but we need to check that
1902 * we didn't accidentially generate a debug key.
1903 */
Tudor Ambarusc0153b02017-09-28 17:14:55 +03001904 if (crypto_memneq(smp->local_pk, debug_pk, 64))
Johan Hedberg70157ef2014-06-24 15:22:59 +03001905 break;
1906 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001907 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001908
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001909done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001910 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001911 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedberg3b191462014-06-06 10:50:15 +03001912
1913 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1914
1915 return 0;
1916}
1917
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001918static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001919{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001920 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001921 struct l2cap_chan *chan = conn->smp;
1922 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001923 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001924 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001925 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001926
1927 BT_DBG("conn %p", conn);
1928
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001929 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001930 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001931
Johan Hedberg40bef302014-07-16 11:42:27 +03001932 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001933 return SMP_CMD_NOTSUPP;
1934
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001935 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001936
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001937 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001938
1939 key_size = min(req->max_key_size, rsp->max_key_size);
1940 if (check_enc_key_size(conn, key_size))
1941 return SMP_ENC_KEY_SIZE;
1942
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001943 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001944
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001945 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001946 return SMP_AUTH_REQUIREMENTS;
1947
Johan Hedbergcb06d362015-03-16 21:12:34 +02001948 /* If the remote side's OOB flag is set it means it has
1949 * successfully received our local OOB data - therefore set the
1950 * flag to indicate that local OOB is in use.
1951 */
Johan Hedberg94f14e42018-09-11 14:10:12 +03001952 if (rsp->oob_flag == SMP_OOB_PRESENT && SMP_DEV(hdev)->local_oob)
Johan Hedberg58428562015-03-16 11:45:45 +02001953 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1954
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001955 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1956 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1957
1958 /* Update remote key distribution in case the remote cleared
1959 * some bits that we had enabled in our request.
1960 */
1961 smp->remote_key_dist &= rsp->resp_key_dist;
1962
Johan Hedberga62da6f2016-12-08 08:32:54 +02001963 if ((req->auth_req & SMP_AUTH_CT2) && (auth & SMP_AUTH_CT2))
1964 set_bit(SMP_FLAG_CT2, &smp->flags);
1965
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001966 /* For BR/EDR this means we're done and can start phase 3 */
1967 if (conn->hcon->type == ACL_LINK) {
1968 /* Clear bits which are generated but not distributed */
1969 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1970 smp_distribute_keys(smp);
1971 return 0;
1972 }
1973
Johan Hedberg65668772014-05-16 11:03:34 +03001974 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1975 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001976 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1977 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001978
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001979 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001980 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1981 u8 method;
1982
1983 method = get_auth_method(smp, req->io_capability,
1984 rsp->io_capability);
1985 if (method == JUST_WORKS || method == JUST_CFM)
1986 return SMP_AUTH_REQUIREMENTS;
1987 }
1988
Johan Hedberge84a6b12013-12-02 10:49:03 +02001989 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001990
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001991 /* Update remote key distribution in case the remote cleared
1992 * some bits that we had enabled in our request.
1993 */
1994 smp->remote_key_dist &= rsp->resp_key_dist;
1995
Johan Hedberg3b191462014-06-06 10:50:15 +03001996 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1997 /* Clear bits which are generated but not distributed */
1998 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1999 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
2000 return sc_send_public_key(smp);
2001 }
2002
Johan Hedbergc05b9332014-09-10 17:37:42 -07002003 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08002004
Johan Hedberg476585e2012-06-06 18:54:15 +08002005 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08002006 if (ret)
2007 return SMP_UNSPECIFIED;
2008
Johan Hedberg4a74d652014-05-20 09:45:50 +03002009 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08002010
2011 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03002012 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002013 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002014
2015 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002016}
2017
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002018static u8 sc_check_confirm(struct smp_chan *smp)
2019{
2020 struct l2cap_conn *conn = smp->conn;
2021
2022 BT_DBG("");
2023
Johan Hedberg38606f12014-06-25 11:10:28 +03002024 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2025 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
2026
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002027 if (conn->hcon->out) {
2028 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2029 smp->prnd);
2030 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2031 }
2032
2033 return 0;
2034}
2035
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002036/* Work-around for some implementations that incorrectly copy RFU bits
2037 * from our security request and thereby create the impression that
2038 * we're doing SC when in fact the remote doesn't support it.
2039 */
2040static int fixup_sc_false_positive(struct smp_chan *smp)
2041{
2042 struct l2cap_conn *conn = smp->conn;
2043 struct hci_conn *hcon = conn->hcon;
2044 struct hci_dev *hdev = hcon->hdev;
2045 struct smp_cmd_pairing *req, *rsp;
2046 u8 auth;
2047
2048 /* The issue is only observed when we're in slave role */
2049 if (hcon->out)
2050 return SMP_UNSPECIFIED;
2051
2052 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002053 bt_dev_err(hdev, "refusing legacy fallback in SC-only mode");
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002054 return SMP_UNSPECIFIED;
2055 }
2056
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002057 bt_dev_err(hdev, "trying to fall back to legacy SMP");
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002058
2059 req = (void *) &smp->preq[1];
2060 rsp = (void *) &smp->prsp[1];
2061
2062 /* Rebuild key dist flags which may have been cleared for SC */
2063 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2064
2065 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2066
2067 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002068 bt_dev_err(hdev, "failed to fall back to legacy SMP");
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002069 return SMP_UNSPECIFIED;
2070 }
2071
2072 clear_bit(SMP_FLAG_SC, &smp->flags);
2073
2074 return 0;
2075}
2076
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002077static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002078{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002079 struct l2cap_chan *chan = conn->smp;
2080 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002081
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002082 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2083
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002084 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002085 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002086
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002087 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2088 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002089
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002090 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2091 int ret;
2092
2093 /* Public Key exchange must happen before any other steps */
2094 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2095 return sc_check_confirm(smp);
2096
2097 BT_ERR("Unexpected SMP Pairing Confirm");
2098
2099 ret = fixup_sc_false_positive(smp);
2100 if (ret)
2101 return ret;
2102 }
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002103
Johan Hedbergb28b4942014-09-05 22:19:55 +03002104 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002105 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2106 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002107 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2108 return 0;
2109 }
2110
2111 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002112 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002113
2114 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002115
2116 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002117}
2118
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002119static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002120{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002121 struct l2cap_chan *chan = conn->smp;
2122 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002123 struct hci_conn *hcon = conn->hcon;
Howard Chungeed467b2020-02-20 11:17:29 +08002124 u8 *pkax, *pkbx, *na, *nb, confirm_hint;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002125 u32 passkey;
2126 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002127
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002128 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002129
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002130 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002131 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002132
Johan Hedberg943a7322014-03-18 12:58:24 +02002133 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002134 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002135
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002136 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2137 return smp_random(smp);
2138
Johan Hedberg580039e2014-12-03 16:26:37 +02002139 if (hcon->out) {
2140 pkax = smp->local_pk;
2141 pkbx = smp->remote_pk;
2142 na = smp->prnd;
2143 nb = smp->rrnd;
2144 } else {
2145 pkax = smp->remote_pk;
2146 pkbx = smp->local_pk;
2147 na = smp->rrnd;
2148 nb = smp->prnd;
2149 }
2150
Johan Hedberga29b0732014-10-28 15:17:05 +01002151 if (smp->method == REQ_OOB) {
2152 if (!hcon->out)
2153 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2154 sizeof(smp->prnd), smp->prnd);
2155 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2156 goto mackey_and_ltk;
2157 }
2158
Johan Hedberg38606f12014-06-25 11:10:28 +03002159 /* Passkey entry has special treatment */
2160 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2161 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2162
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002163 if (hcon->out) {
2164 u8 cfm[16];
2165
2166 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2167 smp->rrnd, 0, cfm);
2168 if (err)
2169 return SMP_UNSPECIFIED;
2170
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002171 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002172 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002173 } else {
2174 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2175 smp->prnd);
2176 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Howard Chungcee5f202020-02-14 19:16:41 +08002177
2178 /* Only Just-Works pairing requires extra checks */
2179 if (smp->method != JUST_WORKS)
2180 goto mackey_and_ltk;
2181
2182 /* If there already exists long term key in local host, leave
2183 * the decision to user space since the remote device could
2184 * be legitimate or malicious.
2185 */
2186 if (hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
2187 hcon->role)) {
Howard Chungeed467b2020-02-20 11:17:29 +08002188 /* Set passkey to 0. The value can be any number since
2189 * it'll be ignored anyway.
2190 */
2191 passkey = 0;
2192 confirm_hint = 1;
2193 goto confirm;
Howard Chungcee5f202020-02-14 19:16:41 +08002194 }
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002195 }
2196
Johan Hedberga29b0732014-10-28 15:17:05 +01002197mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002198 /* Generate MacKey and LTK */
2199 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2200 if (err)
2201 return SMP_UNSPECIFIED;
2202
Sonny Sasakaffee2022020-04-06 11:04:02 -07002203 if (smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002204 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002205 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002206 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2207 }
2208 return 0;
2209 }
2210
Johan Hedberg38606f12014-06-25 11:10:28 +03002211 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002212 if (err)
2213 return SMP_UNSPECIFIED;
2214
Howard Chungeed467b2020-02-20 11:17:29 +08002215 confirm_hint = 0;
2216
2217confirm:
Sonny Sasakaffee2022020-04-06 11:04:02 -07002218 if (smp->method == JUST_WORKS)
2219 confirm_hint = 1;
2220
Johan Hedberg38606f12014-06-25 11:10:28 +03002221 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
Howard Chungeed467b2020-02-20 11:17:29 +08002222 hcon->dst_type, passkey, confirm_hint);
Johan Hedberg38606f12014-06-25 11:10:28 +03002223 if (err)
2224 return SMP_UNSPECIFIED;
2225
2226 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2227
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002228 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002229}
2230
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002231static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002232{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002233 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002234 struct hci_conn *hcon = conn->hcon;
2235
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002236 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002237 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002238 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002239
Johan Hedberga6f78332014-09-10 17:37:45 -07002240 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002241 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002242
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002243 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002244 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002245
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002246 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002247 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002248
Johan Hedbergfe59a052014-07-01 19:14:12 +03002249 /* We never store STKs for master role, so clear this flag */
2250 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2251
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002252 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002253}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002254
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002255bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2256 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002257{
2258 if (sec_level == BT_SECURITY_LOW)
2259 return true;
2260
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002261 /* If we're encrypted with an STK but the caller prefers using
2262 * LTK claim insufficient security. This way we allow the
2263 * connection to be re-encrypted with an LTK, even if the LTK
2264 * provides the same level of security. Only exception is if we
2265 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002266 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002267 if (key_pref == SMP_USE_LTK &&
2268 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002269 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002270 return false;
2271
Johan Hedberg854f4722014-07-01 18:40:20 +03002272 if (hcon->sec_level >= sec_level)
2273 return true;
2274
2275 return false;
2276}
2277
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002278static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002279{
2280 struct smp_cmd_security_req *rp = (void *) skb->data;
2281 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002282 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002283 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002284 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002285 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002286
2287 BT_DBG("conn %p", conn);
2288
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002289 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002290 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002291
Johan Hedberg40bef302014-07-16 11:42:27 +03002292 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002293 return SMP_CMD_NOTSUPP;
2294
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002295 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002296
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002297 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002298 return SMP_AUTH_REQUIREMENTS;
2299
Johan Hedberg5be5e272014-09-10 17:58:54 -07002300 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002301 sec_level = BT_SECURITY_MEDIUM;
2302 else
2303 sec_level = authreq_to_seclevel(auth);
2304
Szymon Janc64e759f2018-02-26 15:41:53 +01002305 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) {
2306 /* If link is already encrypted with sufficient security we
2307 * still need refresh encryption as per Core Spec 5.0 Vol 3,
2308 * Part H 2.4.6
2309 */
2310 smp_ltk_encrypt(conn, hcon->sec_level);
Johan Hedberg854f4722014-07-01 18:40:20 +03002311 return 0;
Szymon Janc64e759f2018-02-26 15:41:53 +01002312 }
Johan Hedberg854f4722014-07-01 18:40:20 +03002313
Johan Hedbergc7262e72014-06-17 13:07:37 +03002314 if (sec_level > hcon->pending_sec_level)
2315 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002316
Johan Hedberg4dab7862012-06-07 14:58:37 +08002317 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002318 return 0;
2319
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002320 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002321 if (!smp)
2322 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002323
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002324 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002325 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002326 return SMP_PAIRING_NOTSUPP;
2327
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002328 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002329
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002330 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002331 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002332
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002333 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2334 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002335
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002336 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002337 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002338
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002339 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002340}
2341
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002342int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002343{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002344 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002345 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002346 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002347 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002348 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002349
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002350 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2351
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002352 /* This may be NULL if there's an unexpected disconnection */
2353 if (!conn)
2354 return 1;
2355
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002356 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002357 return 1;
2358
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002359 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002360 return 1;
2361
Johan Hedbergc7262e72014-06-17 13:07:37 +03002362 if (sec_level > hcon->pending_sec_level)
2363 hcon->pending_sec_level = sec_level;
2364
Johan Hedberg40bef302014-07-16 11:42:27 +03002365 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002366 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2367 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002368
Johan Hedbergd8949aa2015-09-04 12:22:46 +03002369 chan = conn->smp;
2370 if (!chan) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002371 bt_dev_err(hcon->hdev, "security requested but not available");
Johan Hedbergd8949aa2015-09-04 12:22:46 +03002372 return 1;
2373 }
2374
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002375 l2cap_chan_lock(chan);
2376
2377 /* If SMP is already in progress ignore this request */
2378 if (chan->data) {
2379 ret = 0;
2380 goto unlock;
2381 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002382
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002383 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002384 if (!smp) {
2385 ret = 1;
2386 goto unlock;
2387 }
Brian Gix2b64d152011-12-21 16:12:12 -08002388
2389 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002390
Johan Hedberga62da6f2016-12-08 08:32:54 +02002391 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED)) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002392 authreq |= SMP_AUTH_SC;
Johan Hedberga62da6f2016-12-08 08:32:54 +02002393 if (hci_dev_test_flag(hcon->hdev, HCI_SSP_ENABLED))
2394 authreq |= SMP_AUTH_CT2;
2395 }
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002396
Archie Pusakac2aa30d2020-04-07 12:26:27 +08002397 /* Don't attempt to set MITM if setting is overridden by debugfs
2398 * Needed to pass certification test SM/MAS/PKE/BV-01-C
Johan Hedberg2e233642014-03-18 15:42:30 +02002399 */
Archie Pusakac2aa30d2020-04-07 12:26:27 +08002400 if (!hci_dev_test_flag(hcon->hdev, HCI_FORCE_NO_MITM)) {
2401 /* Require MITM if IO Capability allows or the security level
2402 * requires it.
2403 */
2404 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
2405 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
2406 authreq |= SMP_AUTH_MITM;
2407 }
Johan Hedberg2e233642014-03-18 15:42:30 +02002408
Johan Hedberg40bef302014-07-16 11:42:27 +03002409 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002410 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002411
Brian Gix2b64d152011-12-21 16:12:12 -08002412 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002413 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2414 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002415
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002416 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002417 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002418 } else {
2419 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002420 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002421 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002422 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002423 }
2424
Johan Hedberg4a74d652014-05-20 09:45:50 +03002425 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002426 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002427
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002428unlock:
2429 l2cap_chan_unlock(chan);
2430 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002431}
2432
Matias Karhumaacb28c302018-09-26 09:13:46 +03002433int smp_cancel_and_remove_pairing(struct hci_dev *hdev, bdaddr_t *bdaddr,
2434 u8 addr_type)
Johan Hedbergc81d5552015-10-22 09:38:35 +03002435{
Matias Karhumaacb28c302018-09-26 09:13:46 +03002436 struct hci_conn *hcon;
2437 struct l2cap_conn *conn;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002438 struct l2cap_chan *chan;
2439 struct smp_chan *smp;
Matias Karhumaacb28c302018-09-26 09:13:46 +03002440 int err;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002441
Matias Karhumaacb28c302018-09-26 09:13:46 +03002442 err = hci_remove_ltk(hdev, bdaddr, addr_type);
2443 hci_remove_irk(hdev, bdaddr, addr_type);
2444
2445 hcon = hci_conn_hash_lookup_le(hdev, bdaddr, addr_type);
2446 if (!hcon)
2447 goto done;
2448
2449 conn = hcon->l2cap_data;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002450 if (!conn)
Matias Karhumaacb28c302018-09-26 09:13:46 +03002451 goto done;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002452
2453 chan = conn->smp;
2454 if (!chan)
Matias Karhumaacb28c302018-09-26 09:13:46 +03002455 goto done;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002456
2457 l2cap_chan_lock(chan);
2458
2459 smp = chan->data;
2460 if (smp) {
Matias Karhumaacb28c302018-09-26 09:13:46 +03002461 /* Set keys to NULL to make sure smp_failure() does not try to
2462 * remove and free already invalidated rcu list entries. */
2463 smp->ltk = NULL;
2464 smp->slave_ltk = NULL;
2465 smp->remote_irk = NULL;
2466
Johan Hedbergc81d5552015-10-22 09:38:35 +03002467 if (test_bit(SMP_FLAG_COMPLETE, &smp->flags))
2468 smp_failure(conn, 0);
2469 else
2470 smp_failure(conn, SMP_UNSPECIFIED);
Matias Karhumaacb28c302018-09-26 09:13:46 +03002471 err = 0;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002472 }
2473
2474 l2cap_chan_unlock(chan);
Matias Karhumaacb28c302018-09-26 09:13:46 +03002475
2476done:
2477 return err;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002478}
2479
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002480static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2481{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002482 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002483 struct l2cap_chan *chan = conn->smp;
2484 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002485
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002486 BT_DBG("conn %p", conn);
2487
2488 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002489 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002490
Alain Michaud600a8742020-01-07 00:43:17 +00002491 /* Pairing is aborted if any blocked keys are distributed */
2492 if (hci_is_blocked_key(conn->hcon->hdev, HCI_BLOCKED_KEY_TYPE_LTK,
2493 rp->ltk)) {
2494 bt_dev_warn_ratelimited(conn->hcon->hdev,
2495 "LTK blocked for %pMR",
2496 &conn->hcon->dst);
2497 return SMP_INVALID_PARAMS;
2498 }
2499
Johan Hedbergb28b4942014-09-05 22:19:55 +03002500 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002501
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002502 skb_pull(skb, sizeof(*rp));
2503
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002504 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002505
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002506 return 0;
2507}
2508
2509static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2510{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002511 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002512 struct l2cap_chan *chan = conn->smp;
2513 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002514 struct hci_dev *hdev = conn->hcon->hdev;
2515 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002516 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002517 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002518
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002519 BT_DBG("conn %p", conn);
2520
2521 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002522 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002523
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002524 /* Mark the information as received */
2525 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2526
Johan Hedbergb28b4942014-09-05 22:19:55 +03002527 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2528 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002529 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2530 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002531
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002532 skb_pull(skb, sizeof(*rp));
2533
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002534 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002535 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002536 authenticated, smp->tk, smp->enc_key_size,
2537 rp->ediv, rp->rand);
2538 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002539 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002540 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002541
2542 return 0;
2543}
2544
Johan Hedbergfd349c02014-02-18 10:19:36 +02002545static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2546{
2547 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002548 struct l2cap_chan *chan = conn->smp;
2549 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002550
2551 BT_DBG("");
2552
2553 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002554 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002555
Alain Michaud600a8742020-01-07 00:43:17 +00002556 /* Pairing is aborted if any blocked keys are distributed */
2557 if (hci_is_blocked_key(conn->hcon->hdev, HCI_BLOCKED_KEY_TYPE_IRK,
2558 info->irk)) {
2559 bt_dev_warn_ratelimited(conn->hcon->hdev,
2560 "Identity key blocked for %pMR",
2561 &conn->hcon->dst);
2562 return SMP_INVALID_PARAMS;
2563 }
2564
Johan Hedbergb28b4942014-09-05 22:19:55 +03002565 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002566
Johan Hedbergfd349c02014-02-18 10:19:36 +02002567 skb_pull(skb, sizeof(*info));
2568
2569 memcpy(smp->irk, info->irk, 16);
2570
2571 return 0;
2572}
2573
2574static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2575 struct sk_buff *skb)
2576{
2577 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002578 struct l2cap_chan *chan = conn->smp;
2579 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002580 struct hci_conn *hcon = conn->hcon;
2581 bdaddr_t rpa;
2582
2583 BT_DBG("");
2584
2585 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002586 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002587
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002588 /* Mark the information as received */
2589 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2590
Johan Hedbergb28b4942014-09-05 22:19:55 +03002591 if (smp->remote_key_dist & SMP_DIST_SIGN)
2592 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2593
Johan Hedbergfd349c02014-02-18 10:19:36 +02002594 skb_pull(skb, sizeof(*info));
2595
Johan Hedberga9a58f82014-02-25 22:24:37 +02002596 /* Strictly speaking the Core Specification (4.1) allows sending
2597 * an empty address which would force us to rely on just the IRK
2598 * as "identity information". However, since such
2599 * implementations are not known of and in order to not over
2600 * complicate our implementation, simply pretend that we never
2601 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002602 *
2603 * The Identity Address must also be a Static Random or Public
2604 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002605 */
Johan Hedberge12af482015-01-14 20:51:37 +02002606 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2607 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002608 bt_dev_err(hcon->hdev, "ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002609 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002610 }
2611
Szymon Janc1d87b882019-06-19 00:47:47 +02002612 /* Drop IRK if peer is using identity address during pairing but is
2613 * providing different address as identity information.
2614 *
2615 * Microsoft Surface Precision Mouse is known to have this bug.
2616 */
2617 if (hci_is_identity_address(&hcon->dst, hcon->dst_type) &&
2618 (bacmp(&info->bdaddr, &hcon->dst) ||
2619 info->addr_type != hcon->dst_type)) {
2620 bt_dev_err(hcon->hdev,
2621 "ignoring IRK with invalid identity address");
2622 goto distribute;
2623 }
2624
Johan Hedbergfd349c02014-02-18 10:19:36 +02002625 bacpy(&smp->id_addr, &info->bdaddr);
2626 smp->id_addr_type = info->addr_type;
2627
2628 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2629 bacpy(&rpa, &hcon->dst);
2630 else
2631 bacpy(&rpa, BDADDR_ANY);
2632
Johan Hedberg23d0e122014-02-19 14:57:46 +02002633 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2634 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002635
Johan Hedberg31dd6242014-06-27 14:23:02 +03002636distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002637 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2638 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002639
2640 return 0;
2641}
2642
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002643static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2644{
2645 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002646 struct l2cap_chan *chan = conn->smp;
2647 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002648 struct smp_csrk *csrk;
2649
2650 BT_DBG("conn %p", conn);
2651
2652 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002653 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002654
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002655 /* Mark the information as received */
2656 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2657
2658 skb_pull(skb, sizeof(*rp));
2659
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002660 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2661 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002662 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2663 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2664 else
2665 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002666 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2667 }
2668 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002669 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002670
2671 return 0;
2672}
2673
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002674static u8 sc_select_method(struct smp_chan *smp)
2675{
2676 struct l2cap_conn *conn = smp->conn;
2677 struct hci_conn *hcon = conn->hcon;
2678 struct smp_cmd_pairing *local, *remote;
2679 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2680
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002681 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2682 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002683 return REQ_OOB;
2684
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002685 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2686 * which are needed as inputs to some crypto functions. To get
2687 * the "struct smp_cmd_pairing" from them we need to skip the
2688 * first byte which contains the opcode.
2689 */
2690 if (hcon->out) {
2691 local = (void *) &smp->preq[1];
2692 remote = (void *) &smp->prsp[1];
2693 } else {
2694 local = (void *) &smp->prsp[1];
2695 remote = (void *) &smp->preq[1];
2696 }
2697
2698 local_io = local->io_capability;
2699 remote_io = remote->io_capability;
2700
2701 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2702 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2703
2704 /* If either side wants MITM, look up the method from the table,
2705 * otherwise use JUST WORKS.
2706 */
2707 if (local_mitm || remote_mitm)
2708 method = get_auth_method(smp, local_io, remote_io);
2709 else
2710 method = JUST_WORKS;
2711
2712 /* Don't confirm locally initiated pairing attempts */
2713 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2714 method = JUST_WORKS;
2715
2716 return method;
2717}
2718
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002719static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2720{
2721 struct smp_cmd_public_key *key = (void *) skb->data;
2722 struct hci_conn *hcon = conn->hcon;
2723 struct l2cap_chan *chan = conn->smp;
2724 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002725 struct hci_dev *hdev = hcon->hdev;
Tudor Ambarusc0153b02017-09-28 17:14:55 +03002726 struct crypto_kpp *tfm_ecdh;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002727 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002728 int err;
2729
2730 BT_DBG("conn %p", conn);
2731
2732 if (skb->len < sizeof(*key))
2733 return SMP_INVALID_PARAMS;
2734
Luiz Augusto von Dentz6d19628f2021-03-10 14:13:08 -08002735 /* Check if remote and local public keys are the same and debug key is
2736 * not in use.
2737 */
2738 if (!test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags) &&
2739 !crypto_memneq(key, smp->local_pk, 64)) {
2740 bt_dev_err(hdev, "Remote and local public keys are identical");
2741 return SMP_UNSPECIFIED;
2742 }
2743
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002744 memcpy(smp->remote_pk, key, 64);
2745
Johan Hedberga8ca6172015-03-16 18:12:57 +02002746 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2747 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2748 smp->rr, 0, cfm.confirm_val);
2749 if (err)
2750 return SMP_UNSPECIFIED;
2751
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002752 if (crypto_memneq(cfm.confirm_val, smp->pcnf, 16))
Johan Hedberga8ca6172015-03-16 18:12:57 +02002753 return SMP_CONFIRM_FAILED;
2754 }
2755
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002756 /* Non-initiating device sends its public key after receiving
2757 * the key from the initiating device.
2758 */
2759 if (!hcon->out) {
2760 err = sc_send_public_key(smp);
2761 if (err)
2762 return err;
2763 }
2764
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002765 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
Marcel Holtmanne0915262015-03-16 12:34:55 -07002766 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002767
Tudor Ambarusc0153b02017-09-28 17:14:55 +03002768 /* Compute the shared secret on the same crypto tfm on which the private
2769 * key was set/generated.
2770 */
2771 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Matias Karhumaa4ba51752018-09-11 14:10:13 +03002772 struct l2cap_chan *hchan = hdev->smp_data;
2773 struct smp_dev *smp_dev;
2774
2775 if (!hchan || !hchan->data)
2776 return SMP_UNSPECIFIED;
2777
2778 smp_dev = hchan->data;
Tudor Ambarusc0153b02017-09-28 17:14:55 +03002779
2780 tfm_ecdh = smp_dev->tfm_ecdh;
2781 } else {
2782 tfm_ecdh = smp->tfm_ecdh;
2783 }
2784
2785 if (compute_ecdh_secret(tfm_ecdh, smp->remote_pk, smp->dhkey))
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002786 return SMP_UNSPECIFIED;
2787
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002788 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002789
2790 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2791
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002792 smp->method = sc_select_method(smp);
2793
2794 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2795
2796 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2797 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2798 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2799 else
2800 hcon->pending_sec_level = BT_SECURITY_FIPS;
2801
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002802 if (!crypto_memneq(debug_pk, smp->remote_pk, 64))
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002803 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2804
Johan Hedberg38606f12014-06-25 11:10:28 +03002805 if (smp->method == DSP_PASSKEY) {
2806 get_random_bytes(&hcon->passkey_notify,
2807 sizeof(hcon->passkey_notify));
2808 hcon->passkey_notify %= 1000000;
2809 hcon->passkey_entered = 0;
2810 smp->passkey_round = 0;
2811 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2812 hcon->dst_type,
2813 hcon->passkey_notify,
2814 hcon->passkey_entered))
2815 return SMP_UNSPECIFIED;
2816 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2817 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2818 }
2819
Johan Hedberg94ea7252015-03-16 11:45:46 +02002820 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002821 if (hcon->out)
2822 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2823 sizeof(smp->prnd), smp->prnd);
2824
2825 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2826
2827 return 0;
2828 }
2829
Johan Hedberg38606f12014-06-25 11:10:28 +03002830 if (hcon->out)
2831 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2832
2833 if (smp->method == REQ_PASSKEY) {
2834 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2835 hcon->dst_type))
2836 return SMP_UNSPECIFIED;
2837 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2838 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2839 return 0;
2840 }
2841
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002842 /* The Initiating device waits for the non-initiating device to
2843 * send the confirm value.
2844 */
2845 if (conn->hcon->out)
2846 return 0;
2847
2848 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2849 0, cfm.confirm_val);
2850 if (err)
2851 return SMP_UNSPECIFIED;
2852
2853 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2854 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2855
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002856 return 0;
2857}
2858
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002859static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2860{
2861 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2862 struct l2cap_chan *chan = conn->smp;
2863 struct hci_conn *hcon = conn->hcon;
2864 struct smp_chan *smp = chan->data;
2865 u8 a[7], b[7], *local_addr, *remote_addr;
2866 u8 io_cap[3], r[16], e[16];
2867 int err;
2868
2869 BT_DBG("conn %p", conn);
2870
2871 if (skb->len < sizeof(*check))
2872 return SMP_INVALID_PARAMS;
2873
2874 memcpy(a, &hcon->init_addr, 6);
2875 memcpy(b, &hcon->resp_addr, 6);
2876 a[6] = hcon->init_addr_type;
2877 b[6] = hcon->resp_addr_type;
2878
2879 if (hcon->out) {
2880 local_addr = a;
2881 remote_addr = b;
2882 memcpy(io_cap, &smp->prsp[1], 3);
2883 } else {
2884 local_addr = b;
2885 remote_addr = a;
2886 memcpy(io_cap, &smp->preq[1], 3);
2887 }
2888
2889 memset(r, 0, sizeof(r));
2890
Johan Hedberg38606f12014-06-25 11:10:28 +03002891 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2892 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002893 else if (smp->method == REQ_OOB)
2894 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002895
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002896 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2897 io_cap, remote_addr, local_addr, e);
2898 if (err)
2899 return SMP_UNSPECIFIED;
2900
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002901 if (crypto_memneq(check->e, e, 16))
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002902 return SMP_DHKEY_CHECK_FAILED;
2903
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002904 if (!hcon->out) {
2905 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2906 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2907 return 0;
2908 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002909
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002910 /* Slave sends DHKey check as response to master */
2911 sc_dhkey_check(smp);
2912 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002913
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002914 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002915
2916 if (hcon->out) {
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002917 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002918 hcon->enc_key_size = smp->enc_key_size;
2919 }
2920
2921 return 0;
2922}
2923
Johan Hedberg1408bb62014-06-04 22:45:57 +03002924static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2925 struct sk_buff *skb)
2926{
2927 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2928
2929 BT_DBG("value 0x%02x", kp->value);
2930
2931 return 0;
2932}
2933
Johan Hedberg4befb862014-08-11 22:06:38 +03002934static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002935{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002936 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002937 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002938 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002939 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002940 int err = 0;
2941
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002942 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002943 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002944
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002945 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002946 reason = SMP_PAIRING_NOTSUPP;
2947 goto done;
2948 }
2949
Marcel Holtmann92381f52013-10-03 01:23:08 -07002950 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002951 skb_pull(skb, sizeof(code));
2952
Johan Hedbergb28b4942014-09-05 22:19:55 +03002953 smp = chan->data;
2954
2955 if (code > SMP_CMD_MAX)
2956 goto drop;
2957
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002958 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002959 goto drop;
2960
2961 /* If we don't have a context the only allowed commands are
2962 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002963 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002964 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2965 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002966
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002967 switch (code) {
2968 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002969 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002970 break;
2971
2972 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002973 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002974 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002975 break;
2976
2977 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002978 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002979 break;
2980
2981 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002982 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002983 break;
2984
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002985 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002986 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002987 break;
2988
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002989 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002990 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002991 break;
2992
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002993 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002994 reason = smp_cmd_encrypt_info(conn, skb);
2995 break;
2996
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002997 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002998 reason = smp_cmd_master_ident(conn, skb);
2999 break;
3000
Anderson Brigliaeb492e02011-06-09 18:50:40 -03003001 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02003002 reason = smp_cmd_ident_info(conn, skb);
3003 break;
3004
Anderson Brigliaeb492e02011-06-09 18:50:40 -03003005 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02003006 reason = smp_cmd_ident_addr_info(conn, skb);
3007 break;
3008
Anderson Brigliaeb492e02011-06-09 18:50:40 -03003009 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07003010 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03003011 break;
3012
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03003013 case SMP_CMD_PUBLIC_KEY:
3014 reason = smp_cmd_public_key(conn, skb);
3015 break;
3016
Johan Hedberg6433a9a2014-06-06 11:47:30 +03003017 case SMP_CMD_DHKEY_CHECK:
3018 reason = smp_cmd_dhkey_check(conn, skb);
3019 break;
3020
Johan Hedberg1408bb62014-06-04 22:45:57 +03003021 case SMP_CMD_KEYPRESS_NOTIFY:
3022 reason = smp_cmd_keypress_notify(conn, skb);
3023 break;
3024
Anderson Brigliaeb492e02011-06-09 18:50:40 -03003025 default:
3026 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03003027 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03003028 goto done;
3029 }
3030
3031done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03003032 if (!err) {
3033 if (reason)
3034 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03003035 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03003036 }
3037
Anderson Brigliaeb492e02011-06-09 18:50:40 -03003038 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03003039
3040drop:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003041 bt_dev_err(hcon->hdev, "unexpected SMP command 0x%02x from %pMR",
3042 code, &hcon->dst);
Johan Hedbergb28b4942014-09-05 22:19:55 +03003043 kfree_skb(skb);
3044 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03003045}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03003046
Johan Hedberg70db83c2014-08-08 09:37:16 +03003047static void smp_teardown_cb(struct l2cap_chan *chan, int err)
3048{
3049 struct l2cap_conn *conn = chan->conn;
3050
3051 BT_DBG("chan %p", chan);
3052
Johan Hedbergfc75cc82014-09-05 22:19:52 +03003053 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003054 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003055
Johan Hedberg70db83c2014-08-08 09:37:16 +03003056 conn->smp = NULL;
3057 l2cap_chan_put(chan);
3058}
3059
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003060static void bredr_pairing(struct l2cap_chan *chan)
3061{
3062 struct l2cap_conn *conn = chan->conn;
3063 struct hci_conn *hcon = conn->hcon;
3064 struct hci_dev *hdev = hcon->hdev;
3065 struct smp_cmd_pairing req;
3066 struct smp_chan *smp;
3067
3068 BT_DBG("chan %p", chan);
3069
3070 /* Only new pairings are interesting */
3071 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
3072 return;
3073
3074 /* Don't bother if we're not encrypted */
3075 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3076 return;
3077
3078 /* Only master may initiate SMP over BR/EDR */
3079 if (hcon->role != HCI_ROLE_MASTER)
3080 return;
3081
3082 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003083 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003084 return;
3085
3086 /* BR/EDR must use Secure Connections for SMP */
3087 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003088 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003089 return;
3090
3091 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003092 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003093 return;
3094
3095 /* Don't bother if remote LE support is not enabled */
3096 if (!lmp_host_le_capable(hcon))
3097 return;
3098
3099 /* Remote must support SMP fixed chan for BR/EDR */
3100 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
3101 return;
3102
3103 /* Don't bother if SMP is already ongoing */
3104 if (chan->data)
3105 return;
3106
3107 smp = smp_chan_create(conn);
3108 if (!smp) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003109 bt_dev_err(hdev, "unable to create SMP context for BR/EDR");
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003110 return;
3111 }
3112
3113 set_bit(SMP_FLAG_SC, &smp->flags);
3114
3115 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
3116
3117 /* Prepare and send the BR/EDR SMP Pairing Request */
3118 build_bredr_pairing_cmd(smp, &req, NULL);
3119
3120 smp->preq[0] = SMP_CMD_PAIRING_REQ;
3121 memcpy(&smp->preq[1], &req, sizeof(req));
3122
3123 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
3124 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
3125}
3126
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003127static void smp_resume_cb(struct l2cap_chan *chan)
3128{
Johan Hedbergb68fda62014-08-11 22:06:40 +03003129 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003130 struct l2cap_conn *conn = chan->conn;
3131 struct hci_conn *hcon = conn->hcon;
3132
3133 BT_DBG("chan %p", chan);
3134
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003135 if (hcon->type == ACL_LINK) {
3136 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003137 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003138 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003139
Johan Hedberg86d14072014-08-11 22:06:43 +03003140 if (!smp)
3141 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03003142
Johan Hedberg84bc0db2014-09-05 22:19:49 +03003143 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3144 return;
3145
Johan Hedberg86d14072014-08-11 22:06:43 +03003146 cancel_delayed_work(&smp->security_timer);
3147
Johan Hedbergd6268e82014-09-05 22:19:51 +03003148 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003149}
3150
Johan Hedberg70db83c2014-08-08 09:37:16 +03003151static void smp_ready_cb(struct l2cap_chan *chan)
3152{
3153 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003154 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003155
3156 BT_DBG("chan %p", chan);
3157
Johan Hedberg78837462015-11-11 21:47:12 +02003158 /* No need to call l2cap_chan_hold() here since we already own
3159 * the reference taken in smp_new_conn_cb(). This is just the
3160 * first time that we tie it to a specific pointer. The code in
3161 * l2cap_core.c ensures that there's no risk this function wont
3162 * get called if smp_new_conn_cb was previously called.
3163 */
Johan Hedberg70db83c2014-08-08 09:37:16 +03003164 conn->smp = chan;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003165
3166 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3167 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003168}
3169
Johan Hedberg4befb862014-08-11 22:06:38 +03003170static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
3171{
3172 int err;
3173
3174 BT_DBG("chan %p", chan);
3175
3176 err = smp_sig_channel(chan, skb);
3177 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03003178 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03003179
Johan Hedbergb68fda62014-08-11 22:06:40 +03003180 if (smp)
3181 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03003182
Johan Hedberg1e91c292014-08-18 20:33:29 +03003183 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03003184 }
3185
3186 return err;
3187}
3188
Johan Hedberg70db83c2014-08-08 09:37:16 +03003189static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
3190 unsigned long hdr_len,
3191 unsigned long len, int nb)
3192{
3193 struct sk_buff *skb;
3194
3195 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
3196 if (!skb)
3197 return ERR_PTR(-ENOMEM);
3198
3199 skb->priority = HCI_PRIO_MAX;
Johan Hedberga4368ff2015-03-30 23:21:01 +03003200 bt_cb(skb)->l2cap.chan = chan;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003201
3202 return skb;
3203}
3204
3205static const struct l2cap_ops smp_chan_ops = {
3206 .name = "Security Manager",
3207 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003208 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003209 .alloc_skb = smp_alloc_skb_cb,
3210 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003211 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003212
3213 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003214 .state_change = l2cap_chan_no_state_change,
3215 .close = l2cap_chan_no_close,
3216 .defer = l2cap_chan_no_defer,
3217 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003218 .set_shutdown = l2cap_chan_no_set_shutdown,
3219 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003220};
3221
3222static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
3223{
3224 struct l2cap_chan *chan;
3225
3226 BT_DBG("pchan %p", pchan);
3227
3228 chan = l2cap_chan_create();
3229 if (!chan)
3230 return NULL;
3231
3232 chan->chan_type = pchan->chan_type;
3233 chan->ops = &smp_chan_ops;
3234 chan->scid = pchan->scid;
3235 chan->dcid = chan->scid;
3236 chan->imtu = pchan->imtu;
3237 chan->omtu = pchan->omtu;
3238 chan->mode = pchan->mode;
3239
Johan Hedbergabe84902014-11-12 22:22:21 +02003240 /* Other L2CAP channels may request SMP routines in order to
3241 * change the security level. This means that the SMP channel
3242 * lock must be considered in its own category to avoid lockdep
3243 * warnings.
3244 */
3245 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3246
Johan Hedberg70db83c2014-08-08 09:37:16 +03003247 BT_DBG("created chan %p", chan);
3248
3249 return chan;
3250}
3251
3252static const struct l2cap_ops smp_root_chan_ops = {
3253 .name = "Security Manager Root",
3254 .new_connection = smp_new_conn_cb,
3255
3256 /* None of these are implemented for the root channel */
3257 .close = l2cap_chan_no_close,
3258 .alloc_skb = l2cap_chan_no_alloc_skb,
3259 .recv = l2cap_chan_no_recv,
3260 .state_change = l2cap_chan_no_state_change,
3261 .teardown = l2cap_chan_no_teardown,
3262 .ready = l2cap_chan_no_ready,
3263 .defer = l2cap_chan_no_defer,
3264 .suspend = l2cap_chan_no_suspend,
3265 .resume = l2cap_chan_no_resume,
3266 .set_shutdown = l2cap_chan_no_set_shutdown,
3267 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003268};
3269
Johan Hedbergef8efe42014-08-13 15:12:32 +03003270static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003271{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003272 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003273 struct smp_dev *smp;
Herbert Xu71af2f62016-01-24 21:18:30 +08003274 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003275 struct crypto_kpp *tfm_ecdh;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003276
Johan Hedbergef8efe42014-08-13 15:12:32 +03003277 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003278 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003279 goto create_chan;
3280 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003281
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003282 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3283 if (!smp)
3284 return ERR_PTR(-ENOMEM);
3285
Herbert Xu71af2f62016-01-24 21:18:30 +08003286 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003287 if (IS_ERR(tfm_cmac)) {
3288 BT_ERR("Unable to create CMAC crypto context");
Waiman Long453431a2020-08-06 23:18:13 -07003289 kfree_sensitive(smp);
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003290 return ERR_CAST(tfm_cmac);
3291 }
3292
Herbert Xu075f7732020-07-31 17:41:58 +10003293 tfm_ecdh = crypto_alloc_kpp("ecdh", 0, 0);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003294 if (IS_ERR(tfm_ecdh)) {
3295 BT_ERR("Unable to create ECDH crypto context");
3296 crypto_free_shash(tfm_cmac);
Waiman Long453431a2020-08-06 23:18:13 -07003297 kfree_sensitive(smp);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003298 return ERR_CAST(tfm_ecdh);
3299 }
3300
Johan Hedberg94f14e42018-09-11 14:10:12 +03003301 smp->local_oob = false;
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003302 smp->tfm_cmac = tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003303 smp->tfm_ecdh = tfm_ecdh;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003304
Johan Hedbergef8efe42014-08-13 15:12:32 +03003305create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003306 chan = l2cap_chan_create();
3307 if (!chan) {
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003308 if (smp) {
Herbert Xu71af2f62016-01-24 21:18:30 +08003309 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003310 crypto_free_kpp(smp->tfm_ecdh);
Waiman Long453431a2020-08-06 23:18:13 -07003311 kfree_sensitive(smp);
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003312 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003313 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003314 }
3315
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003316 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003317
Johan Hedbergef8efe42014-08-13 15:12:32 +03003318 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003319
3320 l2cap_chan_set_defaults(chan);
3321
Marcel Holtmann157029b2015-01-14 15:43:09 -08003322 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003323 u8 bdaddr_type;
3324
3325 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3326
3327 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003328 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003329 else
3330 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003331 } else {
3332 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003333 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003334 }
3335
Johan Hedberg70db83c2014-08-08 09:37:16 +03003336 chan->state = BT_LISTEN;
3337 chan->mode = L2CAP_MODE_BASIC;
3338 chan->imtu = L2CAP_DEFAULT_MTU;
3339 chan->ops = &smp_root_chan_ops;
3340
Johan Hedbergabe84902014-11-12 22:22:21 +02003341 /* Set correct nesting level for a parent/listening channel */
3342 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3343
Johan Hedbergef8efe42014-08-13 15:12:32 +03003344 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003345}
3346
Johan Hedbergef8efe42014-08-13 15:12:32 +03003347static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003348{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003349 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003350
Johan Hedbergef8efe42014-08-13 15:12:32 +03003351 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003352
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003353 smp = chan->data;
3354 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003355 chan->data = NULL;
Herbert Xu71af2f62016-01-24 21:18:30 +08003356 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003357 crypto_free_kpp(smp->tfm_ecdh);
Waiman Long453431a2020-08-06 23:18:13 -07003358 kfree_sensitive(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003359 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003360
Johan Hedberg70db83c2014-08-08 09:37:16 +03003361 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003362}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003363
Claire Chang82493312020-09-29 16:03:24 +08003364int smp_force_bredr(struct hci_dev *hdev, bool enable)
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003365{
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003366 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003367 return -EALREADY;
3368
3369 if (enable) {
3370 struct l2cap_chan *chan;
3371
3372 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3373 if (IS_ERR(chan))
3374 return PTR_ERR(chan);
3375
3376 hdev->smp_bredr_data = chan;
3377 } else {
3378 struct l2cap_chan *chan;
3379
3380 chan = hdev->smp_bredr_data;
3381 hdev->smp_bredr_data = NULL;
3382 smp_del_chan(chan);
3383 }
3384
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003385 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003386
Claire Chang82493312020-09-29 16:03:24 +08003387 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003388}
3389
Johan Hedbergef8efe42014-08-13 15:12:32 +03003390int smp_register(struct hci_dev *hdev)
3391{
3392 struct l2cap_chan *chan;
3393
3394 BT_DBG("%s", hdev->name);
3395
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003396 /* If the controller does not support Low Energy operation, then
3397 * there is also no need to register any SMP channel.
3398 */
3399 if (!lmp_le_capable(hdev))
3400 return 0;
3401
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003402 if (WARN_ON(hdev->smp_data)) {
3403 chan = hdev->smp_data;
3404 hdev->smp_data = NULL;
3405 smp_del_chan(chan);
3406 }
3407
Johan Hedbergef8efe42014-08-13 15:12:32 +03003408 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3409 if (IS_ERR(chan))
3410 return PTR_ERR(chan);
3411
3412 hdev->smp_data = chan;
3413
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003414 if (!lmp_sc_capable(hdev)) {
Szymon Janc83ebb9e2016-09-09 20:24:40 +02003415 /* Flag can be already set here (due to power toggle) */
3416 if (!hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
3417 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003418 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003419
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003420 if (WARN_ON(hdev->smp_bredr_data)) {
3421 chan = hdev->smp_bredr_data;
3422 hdev->smp_bredr_data = NULL;
3423 smp_del_chan(chan);
3424 }
3425
Johan Hedbergef8efe42014-08-13 15:12:32 +03003426 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3427 if (IS_ERR(chan)) {
3428 int err = PTR_ERR(chan);
3429 chan = hdev->smp_data;
3430 hdev->smp_data = NULL;
3431 smp_del_chan(chan);
3432 return err;
3433 }
3434
3435 hdev->smp_bredr_data = chan;
3436
3437 return 0;
3438}
3439
3440void smp_unregister(struct hci_dev *hdev)
3441{
3442 struct l2cap_chan *chan;
3443
3444 if (hdev->smp_bredr_data) {
3445 chan = hdev->smp_bredr_data;
3446 hdev->smp_bredr_data = NULL;
3447 smp_del_chan(chan);
3448 }
3449
3450 if (hdev->smp_data) {
3451 chan = hdev->smp_data;
3452 hdev->smp_data = NULL;
3453 smp_del_chan(chan);
3454 }
3455}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003456
3457#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3458
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003459static int __init test_debug_key(struct crypto_kpp *tfm_ecdh)
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003460{
Tudor Ambarusc0153b02017-09-28 17:14:55 +03003461 u8 pk[64];
Tudor Ambarusa2976412017-09-28 17:14:52 +03003462 int err;
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003463
Tudor Ambarusc0153b02017-09-28 17:14:55 +03003464 err = set_ecdh_privkey(tfm_ecdh, debug_sk);
Tudor Ambarusa2976412017-09-28 17:14:52 +03003465 if (err)
3466 return err;
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003467
Tudor Ambarusc0153b02017-09-28 17:14:55 +03003468 err = generate_ecdh_public_key(tfm_ecdh, pk);
3469 if (err)
3470 return err;
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003471
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003472 if (crypto_memneq(pk, debug_pk, 64))
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003473 return -EINVAL;
3474
3475 return 0;
3476}
3477
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003478static int __init test_ah(void)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003479{
3480 const u8 irk[16] = {
3481 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3482 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3483 const u8 r[3] = { 0x94, 0x81, 0x70 };
3484 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3485 u8 res[3];
3486 int err;
3487
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003488 err = smp_ah(irk, r, res);
Johan Hedbergcfc41982014-12-30 09:50:40 +02003489 if (err)
3490 return err;
3491
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003492 if (crypto_memneq(res, exp, 3))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003493 return -EINVAL;
3494
3495 return 0;
3496}
3497
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003498static int __init test_c1(void)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003499{
3500 const u8 k[16] = {
3501 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3502 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3503 const u8 r[16] = {
3504 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3505 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3506 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3507 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3508 const u8 _iat = 0x01;
3509 const u8 _rat = 0x00;
3510 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3511 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3512 const u8 exp[16] = {
3513 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3514 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3515 u8 res[16];
3516 int err;
3517
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003518 err = smp_c1(k, r, preq, pres, _iat, &ia, _rat, &ra, res);
Johan Hedbergcfc41982014-12-30 09:50:40 +02003519 if (err)
3520 return err;
3521
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003522 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003523 return -EINVAL;
3524
3525 return 0;
3526}
3527
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003528static int __init test_s1(void)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003529{
3530 const u8 k[16] = {
3531 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3532 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3533 const u8 r1[16] = {
3534 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3535 const u8 r2[16] = {
3536 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3537 const u8 exp[16] = {
3538 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3539 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3540 u8 res[16];
3541 int err;
3542
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003543 err = smp_s1(k, r1, r2, res);
Johan Hedbergcfc41982014-12-30 09:50:40 +02003544 if (err)
3545 return err;
3546
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003547 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003548 return -EINVAL;
3549
3550 return 0;
3551}
3552
Herbert Xu71af2f62016-01-24 21:18:30 +08003553static int __init test_f4(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003554{
3555 const u8 u[32] = {
3556 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3557 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3558 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3559 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3560 const u8 v[32] = {
3561 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3562 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3563 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3564 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3565 const u8 x[16] = {
3566 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3567 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3568 const u8 z = 0x00;
3569 const u8 exp[16] = {
3570 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3571 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3572 u8 res[16];
3573 int err;
3574
3575 err = smp_f4(tfm_cmac, u, v, x, z, res);
3576 if (err)
3577 return err;
3578
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003579 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003580 return -EINVAL;
3581
3582 return 0;
3583}
3584
Herbert Xu71af2f62016-01-24 21:18:30 +08003585static int __init test_f5(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003586{
3587 const u8 w[32] = {
3588 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3589 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3590 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3591 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3592 const u8 n1[16] = {
3593 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3594 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3595 const u8 n2[16] = {
3596 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3597 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3598 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3599 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3600 const u8 exp_ltk[16] = {
3601 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3602 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3603 const u8 exp_mackey[16] = {
3604 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3605 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3606 u8 mackey[16], ltk[16];
3607 int err;
3608
3609 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3610 if (err)
3611 return err;
3612
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003613 if (crypto_memneq(mackey, exp_mackey, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003614 return -EINVAL;
3615
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003616 if (crypto_memneq(ltk, exp_ltk, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003617 return -EINVAL;
3618
3619 return 0;
3620}
3621
Herbert Xu71af2f62016-01-24 21:18:30 +08003622static int __init test_f6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003623{
3624 const u8 w[16] = {
3625 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3626 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3627 const u8 n1[16] = {
3628 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3629 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3630 const u8 n2[16] = {
3631 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3632 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3633 const u8 r[16] = {
3634 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3635 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3636 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3637 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3638 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3639 const u8 exp[16] = {
3640 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3641 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3642 u8 res[16];
3643 int err;
3644
3645 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3646 if (err)
3647 return err;
3648
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003649 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003650 return -EINVAL;
3651
3652 return 0;
3653}
3654
Herbert Xu71af2f62016-01-24 21:18:30 +08003655static int __init test_g2(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003656{
3657 const u8 u[32] = {
3658 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3659 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3660 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3661 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3662 const u8 v[32] = {
3663 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3664 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3665 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3666 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3667 const u8 x[16] = {
3668 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3669 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3670 const u8 y[16] = {
3671 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3672 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3673 const u32 exp_val = 0x2f9ed5ba % 1000000;
3674 u32 val;
3675 int err;
3676
3677 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3678 if (err)
3679 return err;
3680
3681 if (val != exp_val)
3682 return -EINVAL;
3683
3684 return 0;
3685}
3686
Herbert Xu71af2f62016-01-24 21:18:30 +08003687static int __init test_h6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003688{
3689 const u8 w[16] = {
3690 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3691 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3692 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3693 const u8 exp[16] = {
3694 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3695 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3696 u8 res[16];
3697 int err;
3698
3699 err = smp_h6(tfm_cmac, w, key_id, res);
3700 if (err)
3701 return err;
3702
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003703 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003704 return -EINVAL;
3705
3706 return 0;
3707}
3708
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003709static char test_smp_buffer[32];
3710
3711static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3712 size_t count, loff_t *ppos)
3713{
3714 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3715 strlen(test_smp_buffer));
3716}
3717
3718static const struct file_operations test_smp_fops = {
3719 .open = simple_open,
3720 .read = test_smp_read,
3721 .llseek = default_llseek,
3722};
3723
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003724static int __init run_selftests(struct crypto_shash *tfm_cmac,
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003725 struct crypto_kpp *tfm_ecdh)
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003726{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003727 ktime_t calltime, delta, rettime;
3728 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003729 int err;
3730
Marcel Holtmann255047b2014-12-30 00:11:20 -08003731 calltime = ktime_get();
3732
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003733 err = test_debug_key(tfm_ecdh);
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003734 if (err) {
3735 BT_ERR("debug_key test failed");
3736 goto done;
3737 }
3738
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003739 err = test_ah();
Johan Hedbergcfc41982014-12-30 09:50:40 +02003740 if (err) {
3741 BT_ERR("smp_ah test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003742 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003743 }
3744
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003745 err = test_c1();
Johan Hedbergcfc41982014-12-30 09:50:40 +02003746 if (err) {
3747 BT_ERR("smp_c1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003748 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003749 }
3750
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003751 err = test_s1();
Johan Hedbergcfc41982014-12-30 09:50:40 +02003752 if (err) {
3753 BT_ERR("smp_s1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003754 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003755 }
3756
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003757 err = test_f4(tfm_cmac);
3758 if (err) {
3759 BT_ERR("smp_f4 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003760 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003761 }
3762
3763 err = test_f5(tfm_cmac);
3764 if (err) {
3765 BT_ERR("smp_f5 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003766 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003767 }
3768
3769 err = test_f6(tfm_cmac);
3770 if (err) {
3771 BT_ERR("smp_f6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003772 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003773 }
3774
3775 err = test_g2(tfm_cmac);
3776 if (err) {
3777 BT_ERR("smp_g2 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003778 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003779 }
3780
3781 err = test_h6(tfm_cmac);
3782 if (err) {
3783 BT_ERR("smp_h6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003784 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003785 }
3786
Marcel Holtmann255047b2014-12-30 00:11:20 -08003787 rettime = ktime_get();
3788 delta = ktime_sub(rettime, calltime);
3789 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3790
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003791 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003792
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003793done:
3794 if (!err)
3795 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3796 "PASS (%llu usecs)\n", duration);
3797 else
3798 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3799
3800 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3801 &test_smp_fops);
3802
3803 return err;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003804}
3805
3806int __init bt_selftest_smp(void)
3807{
Herbert Xu71af2f62016-01-24 21:18:30 +08003808 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003809 struct crypto_kpp *tfm_ecdh;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003810 int err;
3811
Eric Biggers3d234b32018-11-14 12:21:11 -08003812 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003813 if (IS_ERR(tfm_cmac)) {
3814 BT_ERR("Unable to create CMAC crypto context");
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003815 return PTR_ERR(tfm_cmac);
3816 }
3817
Herbert Xu075f7732020-07-31 17:41:58 +10003818 tfm_ecdh = crypto_alloc_kpp("ecdh", 0, 0);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003819 if (IS_ERR(tfm_ecdh)) {
3820 BT_ERR("Unable to create ECDH crypto context");
3821 crypto_free_shash(tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003822 return PTR_ERR(tfm_ecdh);
3823 }
3824
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003825 err = run_selftests(tfm_cmac, tfm_ecdh);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003826
Herbert Xu71af2f62016-01-24 21:18:30 +08003827 crypto_free_shash(tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003828 crypto_free_kpp(tfm_ecdh);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003829
3830 return err;
3831}
3832
3833#endif