-
Notifications
You must be signed in to change notification settings - Fork 590
/
smp.c
5980 lines (4862 loc) · 150 KB
/
smp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file smp.c
* Security Manager Protocol implementation
*/
/*
* Copyright (c) 2017 Nordic Semiconductor ASA
* Copyright (c) 2015-2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/buf.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/debug/stack.h>
#include <zephyr/kernel.h>
#include <zephyr/net/buf.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/check.h>
#include <zephyr/sys/util.h>
#include "common/bt_str.h"
#include "crypto/bt_crypto.h"
#include "hci_core.h"
#include "ecc.h"
#include "keys.h"
#include "conn_internal.h"
#include "l2cap_internal.h"
#include "classic/l2cap_br_interface.h"
#include "smp.h"
#define LOG_LEVEL CONFIG_BT_SMP_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bt_smp);
#define SMP_TIMEOUT K_SECONDS(30)
#if defined(CONFIG_BT_SIGNING)
#define SIGN_DIST BT_SMP_DIST_SIGN
#else
#define SIGN_DIST 0
#endif
#if defined(CONFIG_BT_PRIVACY)
#define ID_DIST BT_SMP_DIST_ID_KEY
#else
#define ID_DIST 0
#endif
#if defined(CONFIG_BT_CLASSIC)
#define LINK_DIST BT_SMP_DIST_LINK_KEY
#else
#define LINK_DIST 0
#endif
#define RECV_KEYS (BT_SMP_DIST_ENC_KEY | BT_SMP_DIST_ID_KEY | SIGN_DIST |\
LINK_DIST)
#define SEND_KEYS (BT_SMP_DIST_ENC_KEY | ID_DIST | SIGN_DIST | LINK_DIST)
#define RECV_KEYS_SC (RECV_KEYS & ~(BT_SMP_DIST_ENC_KEY))
#define SEND_KEYS_SC (SEND_KEYS & ~(BT_SMP_DIST_ENC_KEY))
#define BR_RECV_KEYS_SC (RECV_KEYS & ~(LINK_DIST))
#define BR_SEND_KEYS_SC (SEND_KEYS & ~(LINK_DIST))
#define BT_SMP_AUTH_MASK 0x07
#if defined(CONFIG_BT_BONDABLE)
#define BT_SMP_AUTH_BONDING_FLAGS BT_SMP_AUTH_BONDING
#else
#define BT_SMP_AUTH_BONDING_FLAGS 0
#endif /* CONFIG_BT_BONDABLE */
#if defined(CONFIG_BT_CLASSIC)
#define BT_SMP_AUTH_MASK_SC 0x2f
#if defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
#define BT_SMP_AUTH_DEFAULT (BT_SMP_AUTH_BONDING_FLAGS | BT_SMP_AUTH_CT2)
#else
#define BT_SMP_AUTH_DEFAULT (BT_SMP_AUTH_BONDING_FLAGS | BT_SMP_AUTH_CT2 |\
BT_SMP_AUTH_SC)
#endif /* CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY */
#else
#define BT_SMP_AUTH_MASK_SC 0x0f
#if defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
#define BT_SMP_AUTH_DEFAULT (BT_SMP_AUTH_BONDING_FLAGS)
#else
#define BT_SMP_AUTH_DEFAULT (BT_SMP_AUTH_BONDING_FLAGS | BT_SMP_AUTH_SC)
#endif /* CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY */
#endif /* CONFIG_BT_CLASSIC */
enum pairing_method {
JUST_WORKS, /* JustWorks pairing */
PASSKEY_INPUT, /* Passkey Entry input */
PASSKEY_DISPLAY, /* Passkey Entry display */
PASSKEY_CONFIRM, /* Passkey confirm */
PASSKEY_ROLE, /* Passkey Entry depends on role */
LE_SC_OOB, /* LESC Out of Band */
LEGACY_OOB, /* Legacy Out of Band */
};
enum {
SMP_FLAG_CFM_DELAYED, /* if confirm should be send when TK is valid */
SMP_FLAG_ENC_PENDING, /* if waiting for an encryption change event */
SMP_FLAG_KEYS_DISTR, /* if keys distribution phase is in progress */
SMP_FLAG_PAIRING, /* if pairing is in progress */
SMP_FLAG_TIMEOUT, /* if SMP timeout occurred */
SMP_FLAG_SC, /* if LE Secure Connections is used */
SMP_FLAG_PKEY_SEND, /* if should send Public Key when available */
SMP_FLAG_DHKEY_PENDING, /* if waiting for local DHKey */
SMP_FLAG_DHKEY_GEN, /* if generating DHKey */
SMP_FLAG_DHKEY_SEND, /* if should generate and send DHKey Check */
SMP_FLAG_USER, /* if waiting for user input */
SMP_FLAG_DISPLAY, /* if display_passkey() callback was called */
SMP_FLAG_OOB_PENDING, /* if waiting for OOB data */
SMP_FLAG_BOND, /* if bonding */
SMP_FLAG_SC_DEBUG_KEY, /* if Secure Connection are using debug key */
SMP_FLAG_SEC_REQ, /* if Security Request was sent/received */
SMP_FLAG_DHCHECK_WAIT, /* if waiting for remote DHCheck (as periph) */
SMP_FLAG_DERIVE_LK, /* if Link Key should be derived */
SMP_FLAG_BR_CONNECTED, /* if BR/EDR channel is connected */
SMP_FLAG_BR_PAIR, /* if should start BR/EDR pairing */
SMP_FLAG_CT2, /* if should use H7 for keys derivation */
/* Total number of flags - must be at the end */
SMP_NUM_FLAGS,
};
/* SMP channel specific context */
struct bt_smp {
/* Commands that remote is allowed to send */
ATOMIC_DEFINE(allowed_cmds, BT_SMP_NUM_CMDS);
/* Flags for SMP state machine */
ATOMIC_DEFINE(flags, SMP_NUM_FLAGS);
/* Type of method used for pairing */
uint8_t method;
/* Pairing Request PDU */
uint8_t preq[7];
/* Pairing Response PDU */
uint8_t prsp[7];
/* Pairing Confirm PDU */
uint8_t pcnf[16];
/* Local random number */
uint8_t prnd[16];
/* Remote random number */
uint8_t rrnd[16];
/* Temporary key */
uint8_t tk[16];
/* Remote Public Key for LE SC */
uint8_t pkey[BT_PUB_KEY_LEN];
/* DHKey */
uint8_t dhkey[BT_DH_KEY_LEN];
/* Remote DHKey check */
uint8_t e[16];
/* MacKey */
uint8_t mackey[16];
/* LE SC passkey */
uint32_t passkey;
/* LE SC passkey round */
uint8_t passkey_round;
/* LE SC local OOB data */
const struct bt_le_oob_sc_data *oobd_local;
/* LE SC remote OOB data */
const struct bt_le_oob_sc_data *oobd_remote;
/* Local key distribution */
uint8_t local_dist;
/* Remote key distribution */
uint8_t remote_dist;
/* The channel this context is associated with.
* This marks the beginning of the part of the structure that will not
* be memset to zero in init.
*/
struct bt_l2cap_le_chan chan;
/* Delayed work for timeout handling */
struct k_work_delayable work;
/* Used Bluetooth authentication callbacks. */
atomic_ptr_t auth_cb;
/* Bondable flag */
atomic_t bondable;
};
static unsigned int fixed_passkey = BT_PASSKEY_INVALID;
#define DISPLAY_FIXED(smp) (IS_ENABLED(CONFIG_BT_FIXED_PASSKEY) && \
fixed_passkey != BT_PASSKEY_INVALID && \
(smp)->method == PASSKEY_DISPLAY)
#if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
/* based on table 2.8 Core Spec 2.3.5.1 Vol. 3 Part H */
static const uint8_t gen_method_legacy[5 /* remote */][5 /* local */] = {
{ JUST_WORKS, JUST_WORKS, PASSKEY_INPUT, JUST_WORKS, PASSKEY_INPUT },
{ JUST_WORKS, JUST_WORKS, PASSKEY_INPUT, JUST_WORKS, PASSKEY_INPUT },
{ PASSKEY_DISPLAY, PASSKEY_DISPLAY, PASSKEY_INPUT, JUST_WORKS,
PASSKEY_DISPLAY },
{ JUST_WORKS, JUST_WORKS, JUST_WORKS, JUST_WORKS, JUST_WORKS },
{ PASSKEY_DISPLAY, PASSKEY_DISPLAY, PASSKEY_INPUT, JUST_WORKS,
PASSKEY_ROLE },
};
#endif /* CONFIG_BT_SMP_SC_PAIR_ONLY */
#if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
/* based on table 2.8 Core Spec 2.3.5.1 Vol. 3 Part H */
static const uint8_t gen_method_sc[5 /* remote */][5 /* local */] = {
{ JUST_WORKS, JUST_WORKS, PASSKEY_INPUT, JUST_WORKS, PASSKEY_INPUT },
{ JUST_WORKS, PASSKEY_CONFIRM, PASSKEY_INPUT, JUST_WORKS,
PASSKEY_CONFIRM },
{ PASSKEY_DISPLAY, PASSKEY_DISPLAY, PASSKEY_INPUT, JUST_WORKS,
PASSKEY_DISPLAY },
{ JUST_WORKS, JUST_WORKS, JUST_WORKS, JUST_WORKS, JUST_WORKS },
{ PASSKEY_DISPLAY, PASSKEY_CONFIRM, PASSKEY_INPUT, JUST_WORKS,
PASSKEY_CONFIRM },
};
#endif /* !CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY */
#if defined(CONFIG_BT_CLASSIC)
/* SMP over BR/EDR channel specific context */
struct bt_smp_br {
/* Commands that remote is allowed to send */
ATOMIC_DEFINE(allowed_cmds, BT_SMP_NUM_CMDS);
/* Flags for SMP state machine */
ATOMIC_DEFINE(flags, SMP_NUM_FLAGS);
/* Local key distribution */
uint8_t local_dist;
/* Remote key distribution */
uint8_t remote_dist;
/* Encryption Key Size used for connection */
uint8_t enc_key_size;
/* The channel this context is associated with.
* This marks the beginning of the part of the structure that will not
* be memset to zero in init.
*/
struct bt_l2cap_br_chan chan;
/* Delayed work for timeout handling */
struct k_work_delayable work;
};
static struct bt_smp_br bt_smp_br_pool[CONFIG_BT_MAX_CONN];
#endif /* CONFIG_BT_CLASSIC */
static struct bt_smp bt_smp_pool[CONFIG_BT_MAX_CONN];
static bool bondable = IS_ENABLED(CONFIG_BT_BONDABLE);
static bool sc_oobd_present;
static bool legacy_oobd_present;
static bool sc_supported;
static const uint8_t *sc_public_key;
static K_SEM_DEFINE(sc_local_pkey_ready, 0, 1);
/* Pointer to internal data is used to mark that callbacks of given SMP channel are not initialized.
* Value of NULL represents no authenticaiton capabilities and cannot be used for that purpose.
*/
#define BT_SMP_AUTH_CB_UNINITIALIZED ((atomic_ptr_val_t)bt_smp_pool)
/* Value used to mark that per-connection bondable flag is not initialized.
* Value false/true represent if flag is cleared or set and cannot be used for that purpose.
*/
#define BT_SMP_BONDABLE_UNINITIALIZED ((atomic_val_t)-1)
static bool le_sc_supported(void)
{
/*
* If controller based ECC is to be used it must support
* "LE Read Local P-256 Public Key" and "LE Generate DH Key" commands.
* Otherwise LE SC are not supported.
*/
if (IS_ENABLED(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)) {
return false;
}
return BT_CMD_TEST(bt_dev.supported_commands, 34, 1) &&
BT_CMD_TEST(bt_dev.supported_commands, 34, 2);
}
static const struct bt_conn_auth_cb *latch_auth_cb(struct bt_smp *smp)
{
(void)atomic_ptr_cas(&smp->auth_cb, BT_SMP_AUTH_CB_UNINITIALIZED,
(atomic_ptr_val_t)bt_auth);
return atomic_ptr_get(&smp->auth_cb);
}
static bool latch_bondable(struct bt_smp *smp)
{
(void)atomic_cas(&smp->bondable, BT_SMP_BONDABLE_UNINITIALIZED, (atomic_val_t)bondable);
return atomic_get(&smp->bondable);
}
static uint8_t get_io_capa(struct bt_smp *smp)
{
const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp);
if (!smp_auth_cb) {
goto no_callbacks;
}
/* Passkey Confirmation is valid only for LE SC */
if (smp_auth_cb->passkey_display && smp_auth_cb->passkey_entry &&
(smp_auth_cb->passkey_confirm || !sc_supported)) {
return BT_SMP_IO_KEYBOARD_DISPLAY;
}
/* DisplayYesNo is useful only for LE SC */
if (sc_supported && smp_auth_cb->passkey_display &&
smp_auth_cb->passkey_confirm) {
return BT_SMP_IO_DISPLAY_YESNO;
}
if (smp_auth_cb->passkey_entry) {
if (IS_ENABLED(CONFIG_BT_FIXED_PASSKEY) &&
fixed_passkey != BT_PASSKEY_INVALID) {
return BT_SMP_IO_KEYBOARD_DISPLAY;
} else {
return BT_SMP_IO_KEYBOARD_ONLY;
}
}
if (smp_auth_cb->passkey_display) {
return BT_SMP_IO_DISPLAY_ONLY;
}
no_callbacks:
if (IS_ENABLED(CONFIG_BT_FIXED_PASSKEY) &&
fixed_passkey != BT_PASSKEY_INVALID) {
return BT_SMP_IO_DISPLAY_ONLY;
} else {
return BT_SMP_IO_NO_INPUT_OUTPUT;
}
}
#if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
static uint8_t legacy_get_pair_method(struct bt_smp *smp, uint8_t remote_io);
#endif
static bool smp_keys_check(struct bt_conn *conn)
{
if (atomic_test_bit(conn->flags, BT_CONN_FORCE_PAIR)) {
return false;
}
if (!conn->le.keys) {
conn->le.keys = bt_keys_find(BT_KEYS_LTK_P256,
conn->id, &conn->le.dst);
if (!conn->le.keys) {
conn->le.keys = bt_keys_find(BT_KEYS_LTK,
conn->id,
&conn->le.dst);
}
}
if (!conn->le.keys ||
!(conn->le.keys->keys & (BT_KEYS_LTK | BT_KEYS_LTK_P256))) {
return false;
}
if (conn->required_sec_level >= BT_SECURITY_L3 &&
!(conn->le.keys->flags & BT_KEYS_AUTHENTICATED)) {
return false;
}
if (conn->required_sec_level >= BT_SECURITY_L4 &&
!((conn->le.keys->flags & BT_KEYS_AUTHENTICATED) &&
(conn->le.keys->keys & BT_KEYS_LTK_P256) &&
(conn->le.keys->enc_size == BT_SMP_MAX_ENC_KEY_SIZE))) {
return false;
}
return true;
}
static uint8_t get_pair_method(struct bt_smp *smp, uint8_t remote_io)
{
#if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) {
return legacy_get_pair_method(smp, remote_io);
}
#endif
#if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
struct bt_smp_pairing *req, *rsp;
req = (struct bt_smp_pairing *)&smp->preq[1];
rsp = (struct bt_smp_pairing *)&smp->prsp[1];
if ((req->auth_req & rsp->auth_req) & BT_SMP_AUTH_SC) {
/* if one side has OOB data use OOB */
if ((req->oob_flag | rsp->oob_flag) & BT_SMP_OOB_DATA_MASK) {
return LE_SC_OOB;
}
}
if (remote_io > BT_SMP_IO_KEYBOARD_DISPLAY) {
return JUST_WORKS;
}
/* if none side requires MITM use JustWorks */
if (!((req->auth_req | rsp->auth_req) & BT_SMP_AUTH_MITM)) {
return JUST_WORKS;
}
return gen_method_sc[remote_io][get_io_capa(smp)];
#else
return JUST_WORKS;
#endif
}
static enum bt_security_err security_err_get(uint8_t smp_err)
{
switch (smp_err) {
case BT_SMP_ERR_PASSKEY_ENTRY_FAILED:
case BT_SMP_ERR_DHKEY_CHECK_FAILED:
case BT_SMP_ERR_NUMERIC_COMP_FAILED:
case BT_SMP_ERR_CONFIRM_FAILED:
return BT_SECURITY_ERR_AUTH_FAIL;
case BT_SMP_ERR_OOB_NOT_AVAIL:
return BT_SECURITY_ERR_OOB_NOT_AVAILABLE;
case BT_SMP_ERR_AUTH_REQUIREMENTS:
case BT_SMP_ERR_ENC_KEY_SIZE:
return BT_SECURITY_ERR_AUTH_REQUIREMENT;
case BT_SMP_ERR_PAIRING_NOTSUPP:
case BT_SMP_ERR_CMD_NOTSUPP:
return BT_SECURITY_ERR_PAIR_NOT_SUPPORTED;
case BT_SMP_ERR_REPEATED_ATTEMPTS:
case BT_SMP_ERR_BREDR_PAIRING_IN_PROGRESS:
case BT_SMP_ERR_CROSS_TRANSP_NOT_ALLOWED:
return BT_SECURITY_ERR_PAIR_NOT_ALLOWED;
case BT_SMP_ERR_INVALID_PARAMS:
return BT_SECURITY_ERR_INVALID_PARAM;
case BT_SMP_ERR_KEY_REJECTED:
return BT_SECURITY_ERR_KEY_REJECTED;
case BT_SMP_ERR_UNSPECIFIED:
default:
return BT_SECURITY_ERR_UNSPECIFIED;
}
}
static uint8_t smp_err_get(enum bt_security_err auth_err)
{
switch (auth_err) {
case BT_SECURITY_ERR_OOB_NOT_AVAILABLE:
return BT_SMP_ERR_OOB_NOT_AVAIL;
case BT_SECURITY_ERR_AUTH_FAIL:
case BT_SECURITY_ERR_AUTH_REQUIREMENT:
return BT_SMP_ERR_AUTH_REQUIREMENTS;
case BT_SECURITY_ERR_PAIR_NOT_SUPPORTED:
return BT_SMP_ERR_PAIRING_NOTSUPP;
case BT_SECURITY_ERR_INVALID_PARAM:
return BT_SMP_ERR_INVALID_PARAMS;
case BT_SECURITY_ERR_PIN_OR_KEY_MISSING:
case BT_SECURITY_ERR_PAIR_NOT_ALLOWED:
case BT_SECURITY_ERR_UNSPECIFIED:
return BT_SMP_ERR_UNSPECIFIED;
default:
return 0;
}
}
static struct net_buf *smp_create_pdu(struct bt_smp *smp, uint8_t op, size_t len)
{
struct bt_smp_hdr *hdr;
struct net_buf *buf;
k_timeout_t timeout;
/* Don't if session had already timed out */
if (atomic_test_bit(smp->flags, SMP_FLAG_TIMEOUT)) {
timeout = K_NO_WAIT;
} else {
timeout = SMP_TIMEOUT;
}
/* Use smaller timeout if returning an error since that could be
* caused by lack of buffers.
*/
buf = bt_l2cap_create_pdu_timeout(NULL, 0, timeout);
if (!buf) {
/* If it was not possible to allocate a buffer within the
* timeout marked it as timed out.
*/
atomic_set_bit(smp->flags, SMP_FLAG_TIMEOUT);
return NULL;
}
hdr = net_buf_add(buf, sizeof(*hdr));
hdr->code = op;
return buf;
}
static uint8_t get_encryption_key_size(struct bt_smp *smp)
{
struct bt_smp_pairing *req, *rsp;
req = (struct bt_smp_pairing *)&smp->preq[1];
rsp = (struct bt_smp_pairing *)&smp->prsp[1];
/*
* The smaller value of the initiating and responding devices maximum
* encryption key length parameters shall be used as the encryption key
* size.
*/
return MIN(req->max_key_size, rsp->max_key_size);
}
/* Check that if a new pairing procedure with an existing bond will not lower
* the established security level of the bond.
*/
static bool update_keys_check(struct bt_smp *smp, struct bt_keys *keys)
{
if (IS_ENABLED(CONFIG_BT_SMP_DISABLE_LEGACY_JW_PASSKEY) &&
!atomic_test_bit(smp->flags, SMP_FLAG_SC) &&
smp->method != LEGACY_OOB) {
return false;
}
if (IS_ENABLED(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) &&
smp->method != LEGACY_OOB) {
return false;
}
if (!keys ||
!(keys->keys & (BT_KEYS_LTK_P256 | BT_KEYS_LTK))) {
return true;
}
if (keys->enc_size > get_encryption_key_size(smp)) {
return false;
}
if ((keys->keys & BT_KEYS_LTK_P256) &&
!atomic_test_bit(smp->flags, SMP_FLAG_SC)) {
return false;
}
if ((keys->flags & BT_KEYS_AUTHENTICATED) &&
smp->method == JUST_WORKS) {
return false;
}
if (!IS_ENABLED(CONFIG_BT_SMP_ALLOW_UNAUTH_OVERWRITE) &&
(!(keys->flags & BT_KEYS_AUTHENTICATED)
&& smp->method == JUST_WORKS)) {
if (!IS_ENABLED(CONFIG_BT_ID_ALLOW_UNAUTH_OVERWRITE) ||
(keys->id == smp->chan.chan.conn->id)) {
return false;
}
}
return true;
}
static bool update_debug_keys_check(struct bt_smp *smp)
{
struct bt_conn *conn = smp->chan.chan.conn;
if (!conn->le.keys) {
conn->le.keys = bt_keys_get_addr(conn->id, &conn->le.dst);
}
if (!conn->le.keys ||
!(conn->le.keys->keys & (BT_KEYS_LTK_P256 | BT_KEYS_LTK))) {
return true;
}
if (conn->le.keys->flags & BT_KEYS_DEBUG) {
return true;
}
return false;
}
#if defined(CONFIG_BT_PRIVACY) || defined(CONFIG_BT_SIGNING) || \
!defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
/* For TX callbacks */
static void smp_pairing_complete(struct bt_smp *smp, uint8_t status);
#if defined(CONFIG_BT_CLASSIC)
static void smp_pairing_br_complete(struct bt_smp_br *smp, uint8_t status);
#endif
static void smp_check_complete(struct bt_conn *conn, uint8_t dist_complete)
{
struct bt_l2cap_chan *chan;
if (conn->type == BT_CONN_TYPE_LE) {
struct bt_smp *smp;
chan = bt_l2cap_le_lookup_tx_cid(conn, BT_L2CAP_CID_SMP);
__ASSERT(chan, "No SMP channel found");
smp = CONTAINER_OF(chan, struct bt_smp, chan.chan);
smp->local_dist &= ~dist_complete;
/* if all keys were distributed, pairing is done */
if (!smp->local_dist && !smp->remote_dist) {
smp_pairing_complete(smp, 0);
}
return;
}
#if defined(CONFIG_BT_CLASSIC)
if (conn->type == BT_CONN_TYPE_BR) {
struct bt_smp_br *smp;
chan = bt_l2cap_le_lookup_tx_cid(conn, BT_L2CAP_CID_BR_SMP);
__ASSERT(chan, "No SMP channel found");
smp = CONTAINER_OF(chan, struct bt_smp_br, chan.chan);
smp->local_dist &= ~dist_complete;
/* if all keys were distributed, pairing is done */
if (!smp->local_dist && !smp->remote_dist) {
smp_pairing_br_complete(smp, 0);
}
}
#endif
}
#endif
#if defined(CONFIG_BT_PRIVACY)
static void smp_id_sent(struct bt_conn *conn, void *user_data, int err)
{
if (!err) {
smp_check_complete(conn, BT_SMP_DIST_ID_KEY);
}
}
#endif /* CONFIG_BT_PRIVACY */
#if defined(CONFIG_BT_SIGNING)
static void smp_sign_info_sent(struct bt_conn *conn, void *user_data, int err)
{
if (!err) {
smp_check_complete(conn, BT_SMP_DIST_SIGN);
}
}
#endif /* CONFIG_BT_SIGNING */
#if defined(CONFIG_BT_CLASSIC)
static void sc_derive_link_key(struct bt_smp *smp)
{
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
static const uint8_t lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
struct bt_conn *conn = smp->chan.chan.conn;
struct bt_keys_link_key *link_key;
uint8_t ilk[16];
LOG_DBG("");
/* TODO handle errors? */
/*
* At this point remote device identity is known so we can use
* destination address here
*/
link_key = bt_keys_get_link_key(&conn->le.dst.a);
if (!link_key) {
return;
}
if (atomic_test_bit(smp->flags, SMP_FLAG_CT2)) {
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
static const uint8_t salt[16] = { 0x31, 0x70, 0x6d, 0x74,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 };
if (bt_crypto_h7(salt, conn->le.keys->ltk.val, ilk)) {
bt_keys_link_key_clear(link_key);
return;
}
} else {
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
static const uint8_t tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
if (bt_crypto_h6(conn->le.keys->ltk.val, tmp1, ilk)) {
bt_keys_link_key_clear(link_key);
return;
}
}
if (bt_crypto_h6(ilk, lebr, link_key->val)) {
bt_keys_link_key_clear(link_key);
}
link_key->flags |= BT_LINK_KEY_SC;
if (conn->le.keys->flags & BT_KEYS_AUTHENTICATED) {
link_key->flags |= BT_LINK_KEY_AUTHENTICATED;
} else {
link_key->flags &= ~BT_LINK_KEY_AUTHENTICATED;
}
}
static void smp_br_reset(struct bt_smp_br *smp)
{
/* Clear flags first in case canceling of timeout fails. The SMP context
* shall be marked as timed out in that case.
*/
atomic_set(smp->flags, 0);
/* If canceling fails the timeout handler will set the timeout flag and
* mark the it as timed out. No new pairing procedures shall be started
* on this connection if that happens.
*/
(void)k_work_cancel_delayable(&smp->work);
atomic_set(smp->allowed_cmds, 0);
atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_REQ);
}
static void smp_pairing_br_complete(struct bt_smp_br *smp, uint8_t status)
{
struct bt_conn *conn = smp->chan.chan.conn;
struct bt_keys *keys;
bt_addr_le_t addr;
LOG_DBG("status 0x%x", status);
/* For dualmode devices LE address is same as BR/EDR address
* and is of public type.
*/
bt_addr_copy(&addr.a, &conn->br.dst);
addr.type = BT_ADDR_LE_PUBLIC;
keys = bt_keys_find_addr(conn->id, &addr);
if (status) {
struct bt_conn_auth_info_cb *listener, *next;
if (keys) {
bt_keys_clear(keys);
}
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&bt_auth_info_cbs, listener,
next, node) {
if (listener->pairing_failed) {
listener->pairing_failed(smp->chan.chan.conn,
security_err_get(status));
}
}
} else {
bool bond_flag = atomic_test_bit(smp->flags, SMP_FLAG_BOND);
struct bt_conn_auth_info_cb *listener, *next;
if (bond_flag && keys) {
bt_keys_store(keys);
}
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&bt_auth_info_cbs, listener,
next, node) {
if (listener->pairing_complete) {
listener->pairing_complete(smp->chan.chan.conn,
bond_flag);
}
}
}
smp_br_reset(smp);
}
static void smp_br_timeout(struct k_work *work)
{
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
struct bt_smp_br *smp = CONTAINER_OF(dwork, struct bt_smp_br, work);
LOG_ERR("SMP Timeout");
smp_pairing_br_complete(smp, BT_SMP_ERR_UNSPECIFIED);
atomic_set_bit(smp->flags, SMP_FLAG_TIMEOUT);
}
static void smp_br_send(struct bt_smp_br *smp, struct net_buf *buf,
bt_conn_tx_cb_t cb)
{
int err = bt_l2cap_br_send_cb(smp->chan.chan.conn, BT_L2CAP_CID_BR_SMP, buf, cb, NULL);
if (err) {
if (err == -ENOBUFS) {
LOG_ERR("Ran out of TX buffers or contexts.");
}
net_buf_unref(buf);
return;
}
k_work_reschedule(&smp->work, SMP_TIMEOUT);
}
static void bt_smp_br_connected(struct bt_l2cap_chan *chan)
{
struct bt_smp_br *smp = CONTAINER_OF(chan, struct bt_smp_br, chan.chan);
LOG_DBG("chan %p cid 0x%04x", chan,
CONTAINER_OF(chan, struct bt_l2cap_br_chan, chan)->tx.cid);
atomic_set_bit(smp->flags, SMP_FLAG_BR_CONNECTED);
/*
* if this flag is set it means pairing was requested before channel
* was connected
*/
if (atomic_test_bit(smp->flags, SMP_FLAG_BR_PAIR)) {
bt_smp_br_send_pairing_req(chan->conn);
}
}
static void bt_smp_br_disconnected(struct bt_l2cap_chan *chan)
{
struct bt_smp_br *smp = CONTAINER_OF(chan, struct bt_smp_br, chan.chan);
LOG_DBG("chan %p cid 0x%04x", chan,
CONTAINER_OF(chan, struct bt_l2cap_br_chan, chan)->tx.cid);
/* Channel disconnected callback is always called from a work handler
* so canceling of the timeout work should always succeed.
*/
(void)k_work_cancel_delayable(&smp->work);
(void)memset(smp, 0, sizeof(*smp));
}
static void smp_br_init(struct bt_smp_br *smp)
{
/* Initialize SMP context exluding L2CAP channel context and anything
* else declared after.
*/
(void)memset(smp, 0, offsetof(struct bt_smp_br, chan));
atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_FAIL);
}
static void smp_br_derive_ltk(struct bt_smp_br *smp)
{
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.5 */
static const uint8_t brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
struct bt_conn *conn = smp->chan.chan.conn;
struct bt_keys_link_key *link_key = conn->br.link_key;
struct bt_keys *keys;
bt_addr_le_t addr;
uint8_t ilk[16];
LOG_DBG("");
if (!link_key) {
return;
}
if (IS_ENABLED(CONFIG_BT_SMP_FORCE_BREDR) && conn->encrypt != 0x02) {
LOG_WRN("Using P192 Link Key for P256 LTK derivation");
}
/*
* For dualmode devices LE address is same as BR/EDR address and is of
* public type.
*/
bt_addr_copy(&addr.a, &conn->br.dst);
addr.type = BT_ADDR_LE_PUBLIC;
keys = bt_keys_get_type(BT_KEYS_LTK_P256, conn->id, &addr);
if (!keys) {
LOG_ERR("Unable to get keys for %s", bt_addr_le_str(&addr));
return;
}
if (atomic_test_bit(smp->flags, SMP_FLAG_CT2)) {
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.5 */
static const uint8_t salt[16] = { 0x32, 0x70, 0x6d, 0x74,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 };
if (bt_crypto_h7(salt, link_key->val, ilk)) {
bt_keys_link_key_clear(link_key);
return;
}
} else {
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.5 */
static const uint8_t tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
if (bt_crypto_h6(link_key->val, tmp2, ilk)) {
bt_keys_clear(keys);
return;
}
}
if (bt_crypto_h6(ilk, brle, keys->ltk.val)) {
bt_keys_clear(keys);
return;
}
(void)memset(keys->ltk.ediv, 0, sizeof(keys->ltk.ediv));
(void)memset(keys->ltk.rand, 0, sizeof(keys->ltk.rand));
keys->enc_size = smp->enc_key_size;
if (link_key->flags & BT_LINK_KEY_AUTHENTICATED) {
keys->flags |= BT_KEYS_AUTHENTICATED;
} else {
keys->flags &= ~BT_KEYS_AUTHENTICATED;
}
LOG_DBG("LTK derived from LinkKey");
}
static struct net_buf *smp_br_create_pdu(struct bt_smp_br *smp, uint8_t op,
size_t len)
{
struct bt_smp_hdr *hdr;
struct net_buf *buf;
k_timeout_t timeout;
/* Don't if session had already timed out */
if (atomic_test_bit(smp->flags, SMP_FLAG_TIMEOUT)) {
timeout = K_NO_WAIT;
} else {
timeout = SMP_TIMEOUT;
}
/* Use smaller timeout if returning an error since that could be
* caused by lack of buffers.
*/
buf = bt_l2cap_create_pdu_timeout(NULL, 0, timeout);
if (!buf) {
/* If it was not possible to allocate a buffer within the
* timeout marked it as timed out.
*/
atomic_set_bit(smp->flags, SMP_FLAG_TIMEOUT);
return NULL;
}
hdr = net_buf_add(buf, sizeof(*hdr));
hdr->code = op;
return buf;
}
static void smp_br_distribute_keys(struct bt_smp_br *smp)
{
struct bt_conn *conn = smp->chan.chan.conn;
struct bt_keys *keys;
bt_addr_le_t addr;
/*
* For dualmode devices LE address is same as BR/EDR address and is of
* public type.
*/
bt_addr_copy(&addr.a, &conn->br.dst);
addr.type = BT_ADDR_LE_PUBLIC;
keys = bt_keys_get_addr(conn->id, &addr);
if (!keys) {
LOG_ERR("No keys space for %s", bt_addr_le_str(&addr));
return;
}
#if defined(CONFIG_BT_PRIVACY)
if (smp->local_dist & BT_SMP_DIST_ID_KEY) {