-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
prov_node.c
1644 lines (1330 loc) · 46.6 KB
/
prov_node.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
/* Bluetooth Mesh */
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <string.h>
#include "crypto.h"
#include "adv.h"
#include "mesh.h"
#include "access.h"
#include "foundation.h"
#include "mesh/common.h"
#include "mesh/proxy.h"
#include "proxy_common.h"
#include "proxy_server.h"
#include "proxy_client.h"
#include "prov_common.h"
#include "prov_node.h"
#include "mesh_v1.1/utils.h"
#if CONFIG_BLE_MESH_NODE
#if CONFIG_BLE_MESH_PB_ADV && !CONFIG_BLE_MESH_PB_GATT
NET_BUF_SIMPLE_DEFINE_STATIC(rx_buf, PROV_RX_BUF_SIZE);
#endif
static struct bt_mesh_prov_link prov_link;
#if CONFIG_BLE_MESH_PB_ADV
static void reset_adv_link(struct bt_mesh_prov_link *link, uint8_t reason);
extern uint8_t node_next_xact_id(struct bt_mesh_prov_link *link);
#endif
#if CONFIG_BLE_MESH_PB_GATT
static int prov_send_gatt(struct bt_mesh_prov_link *link, struct net_buf_simple *msg);
#endif
static void prov_send_fail_msg(uint8_t err);
struct bt_mesh_prov_link *bt_mesh_prov_node_get_link(void)
{
return &prov_link;
}
static void close_link(uint8_t reason)
{
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
if (prov_link.pb_remote_close) {
prov_link.pb_remote_close(&prov_link, reason);
}
return;
}
prov_send_fail_msg(reason);
}
void bt_mesh_prov_node_close_link(uint8_t reason)
{
close_link(reason);
}
static void reset_state(void)
{
k_delayed_work_cancel(&prov_link.prot_timer);
/* Disable Attention Timer if it was set */
if (prov_link.conf_inputs[0]) {
bt_mesh_attention(NULL, 0);
}
#if CONFIG_BLE_MESH_PB_GATT
if (prov_link.conn) {
bt_mesh_conn_unref(prov_link.conn);
}
#endif /* CONFIG_BLE_MESH_PB_GATT */
#if CONFIG_BLE_MESH_PB_ADV
/* Clear everything except the retransmit and protocol timer
* delayed work objects.
*/
(void)memset(&prov_link, 0, offsetof(struct bt_mesh_prov_link, tx.retransmit));
prov_link.pending_ack = PROV_XACT_NVAL;
prov_link.rx.prev_id = PROV_XACT_NVAL;
#if CONFIG_BLE_MESH_PB_GATT
prov_link.rx.buf = bt_mesh_proxy_server_get_buf();
#else
net_buf_simple_reset(&rx_buf);
prov_link.rx.buf = &rx_buf;
#endif /* CONFIG_BLE_MESH_PB_GATT */
prov_link.next_xact_id = node_next_xact_id;
prov_link.reset_adv_link = reset_adv_link;
prov_link.retrans_timeout = reset_adv_link;
prov_link.invite_recv = false;
#if CONFIG_BLE_MESH_FAST_PROV
prov_link.last_tx_pdu = PROV_COMPLETE;
#endif /* CONFIG_BLE_MESH_FAST_PROV */
#else /* !CONFIG_BLE_MESH_PB_ADV */
/* Clear everything except the protocol timer (k_delayed_work) */
(void)memset(&prov_link, 0, offsetof(struct bt_mesh_prov_link, prot_timer));
#endif /* CONFIG_BLE_MESH_PB_ADV */
#if CONFIG_BLE_MESH_PB_GATT
prov_link.pb_gatt_send = prov_send_gatt;
#endif /* CONFIG_BLE_MESH_PB_GATT */
}
#if CONFIG_BLE_MESH_PB_ADV
static void reset_adv_link(struct bt_mesh_prov_link *link, uint8_t reason)
{
ARG_UNUSED(link);
bt_mesh_prov_clear_tx(&prov_link, true);
if (bt_mesh_prov_get()->link_close) {
bt_mesh_prov_get()->link_close(BLE_MESH_PROV_ADV, reason);
}
#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
/* Remove the link id from exceptional list */
bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID,
&prov_link.link_id);
#endif
reset_state();
}
#endif /* CONFIG_BLE_MESH_PB_ADV */
#if CONFIG_BLE_MESH_PB_GATT
static int prov_send_gatt(struct bt_mesh_prov_link *link, struct net_buf_simple *msg)
{
int err = 0;
ARG_UNUSED(link);
if (!prov_link.conn) {
BT_ERR("PB-GATT send, not connected");
return -ENOTCONN;
}
/* Changed by Espressif, add provisioning timeout timer operations.
* When sending a provisioning PDU successfully, restart the 60s timer.
*/
#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT && CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) {
err = bt_mesh_proxy_client_send(link->conn, BLE_MESH_PROXY_PROV, msg);
/* TODO: do we need to check the err here? */
bt_mesh_rpr_srv_send_outbound_report(link->pb_remote_uuid, true);
} else
#endif
{
err = bt_mesh_proxy_server_send(prov_link.conn, BLE_MESH_PROXY_PROV, msg);
}
if (err) {
BT_ERR("Failed to send provisioning PDU");
return err;
}
k_delayed_work_submit(&prov_link.prot_timer, PROTOCOL_TIMEOUT);
return 0;
}
#endif /* CONFIG_BLE_MESH_PB_GATT */
static void prov_send_fail_msg(uint8_t err)
{
PROV_BUF(buf, 2);
bt_mesh_prov_buf_init(&buf, PROV_FAILED);
net_buf_simple_add_u8(&buf, err);
if (bt_mesh_prov_send(&prov_link, &buf)) {
BT_ERR("Failed to send Provisioning Failed message");
}
bt_mesh_atomic_set_bit(prov_link.flags, LINK_INVALID);
}
static void prov_invite(const uint8_t *data)
{
PROV_BUF(buf, 12);
BT_DBG("Attention Duration: %u seconds", data[0]);
if (data[0]) {
bt_mesh_attention(NULL, data[0]);
}
#if CONFIG_BLE_MESH_CERT_BASED_PROV
/* Indicate prov_invite is received */
prov_link.invite_recv = true;
#endif
prov_link.conf_inputs[0] = data[0];
bt_mesh_prov_buf_init(&buf, PROV_CAPABILITIES);
/* Number of Elements supported */
net_buf_simple_add_u8(&buf, bt_mesh_elem_count());
/* NOTE: If bit 1 of the OOB Type field is set to 1, bit 0 of
* the Algorithms field shall be set to 0.
*/
/* Supported algorithms - FIPS P-256 Elliptic Curve */
#if CONFIG_BLE_MESH_PROV_EPA
if (bt_mesh_prov_get()->oob_type & BIT(PROV_ONLY_OOB_AUTH_SUPPORT)) {
net_buf_simple_add_be16(&buf, BIT(PROV_ALG_P256_HMAC_SHA256));
} else {
net_buf_simple_add_be16(&buf, (BIT(PROV_ALG_P256_CMAC_AES128) |
BIT(PROV_ALG_P256_HMAC_SHA256)));
}
#else
net_buf_simple_add_be16(&buf, BIT(PROV_ALG_P256_CMAC_AES128));
#endif
/* Public Key Type */
net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->oob_pub_key);
/* OOB Type */
net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->oob_type);
/* Output OOB Size */
net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->output_size);
/* Output OOB Action */
net_buf_simple_add_be16(&buf, bt_mesh_prov_get()->output_actions);
/* Input OOB Size */
net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->input_size);
/* Input OOB Action */
net_buf_simple_add_be16(&buf, bt_mesh_prov_get()->input_actions);
memcpy(&prov_link.conf_inputs[1], &buf.data[1], 11);
if (bt_mesh_prov_send(&prov_link, &buf)) {
BT_ERR("Failed to send capabilities");
return;
}
prov_link.expect = PROV_START;
}
static void prov_capabilities(const uint8_t *data)
{}
static int prov_auth(uint8_t method, uint8_t action, uint8_t size)
{
bt_mesh_output_action_t output = 0U;
bt_mesh_input_action_t input = 0U;
uint8_t auth_size = 0;
auth_size = PROV_AUTH_SIZE(&prov_link);
switch (method) {
case AUTH_METHOD_NO_OOB:
if (action || size) {
return -EINVAL;
}
(void)memset(prov_link.auth, 0, sizeof(prov_link.auth));
return 0;
case AUTH_METHOD_STATIC:
if (action || size) {
return -EINVAL;
}
if (bt_mesh_prov_get()->static_val_len > auth_size) {
memcpy(prov_link.auth, bt_mesh_prov_get()->static_val, auth_size);
} else {
memcpy(prov_link.auth + auth_size - bt_mesh_prov_get()->static_val_len,
bt_mesh_prov_get()->static_val, bt_mesh_prov_get()->static_val_len);
(void)memset(prov_link.auth, 0,
auth_size - bt_mesh_prov_get()->static_val_len);
}
return 0;
case AUTH_METHOD_OUTPUT:
output = bt_mesh_prov_output_action(action);
if (!output) {
return -EINVAL;
}
if (!(bt_mesh_prov_get()->output_actions & output)) {
return -EINVAL;
}
if (size > bt_mesh_prov_get()->output_size) {
return -EINVAL;
}
if (output == BLE_MESH_DISPLAY_STRING) {
unsigned char str[9] = {'\0'};
uint8_t i = 0U;
bt_mesh_rand(str, size);
/* Normalize to '0' .. '9' & 'A' .. 'Z' */
for (i = 0U; i < size; i++) {
str[i] %= 36;
if (str[i] < 10) {
str[i] += '0';
} else {
str[i] += 'A' - 10;
}
}
str[size] = '\0';
memcpy(prov_link.auth, str, size);
(void)memset(prov_link.auth + size, 0,
sizeof(prov_link.auth) - size);
return bt_mesh_prov_get()->output_string((char *)str);
} else {
uint32_t div[8] = { 10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000
};
uint32_t num = 0U;
bt_mesh_rand(&num, sizeof(num));
if (output == BLE_MESH_BLINK ||
output == BLE_MESH_BEEP ||
output == BLE_MESH_VIBRATE) {
/** NOTE: According to the Bluetooth Mesh Profile Specification
* Section 5.4.2.4, blink, beep and vibrate should be a random
* integer between 0 and 10^size.
*/
num = (num % (div[size - 1] - 1)) + 1;
} else {
num %= div[size - 1];
}
sys_put_be32(num, &prov_link.auth[auth_size - 4]);
(void)memset(prov_link.auth, 0, auth_size - 4);
return bt_mesh_prov_get()->output_number(output, num);
}
case AUTH_METHOD_INPUT:
input = bt_mesh_prov_input_action(action);
if (!input) {
return -EINVAL;
}
if (!(bt_mesh_prov_get()->input_actions & input)) {
return -EINVAL;
}
if (size > bt_mesh_prov_get()->input_size) {
return -EINVAL;
}
if (input == BLE_MESH_ENTER_STRING) {
bt_mesh_atomic_set_bit(prov_link.flags, WAIT_STRING);
} else {
bt_mesh_atomic_set_bit(prov_link.flags, WAIT_NUMBER);
}
return bt_mesh_prov_get()->input(input, size);
default:
return -EINVAL;
}
}
static void prov_start(const uint8_t *data)
{
BT_INFO("Algorithm: 0x%02x", data[0]);
BT_INFO("Public Key: 0x%02x", data[1]);
BT_INFO("Auth Method: 0x%02x", data[2]);
BT_INFO("Auth Action: 0x%02x", data[3]);
BT_INFO("Auth Size: 0x%02x", data[4]);
prov_link.algorithm = data[0];
if (data[0] >= PROV_ALG_METHOD_MAX_NUM) {
BT_ERR("Unknown algorithm 0x%02x", data[0]);
close_link(PROV_ERR_NVAL_FMT);
return;
}
/* If bit 1 of the OOB Type field of the Provisioning Capabilities PDU
* is set to 1 (Only OOB authenticated provisioning supported) and any
* of the following conditions is met:
* 1. the Algorithm field of the Provisioning Start PDU is not set to
* BTM_ECDH_P256_CMAC_AES128_AES_CCM and the Provisioning Start PDU
* has the Authentication Method field set to 0x00 (Authentication
* with No OOB).
* 2. the Algorithm field is set to BTM_ECDH_P256_CMAC_AES128_AES_CCM.
* the provisioning protocol shall fail and the message shall be treated
* by the Provisionee as an error in the provisioning protocol.
*/
#if CONFIG_BLE_MESH_PROV_EPA
if ((bt_mesh_prov_get()->oob_type & BIT(PROV_ONLY_OOB_AUTH_SUPPORT)) &&
((data[0] == PROV_ALG_P256_HMAC_SHA256 && data[2] == AUTH_METHOD_NO_OOB) ||
data[0] == PROV_ALG_P256_CMAC_AES128)) {
close_link(PROV_ERR_NVAL_FMT);
return;
}
#endif
if (data[1] != bt_mesh_prov_get()->oob_pub_key) {
BT_ERR("Invalid public key type: 0x%02x", data[1]);
close_link(PROV_ERR_NVAL_FMT);
return;
}
prov_link.public_key = data[1];
memcpy(&prov_link.conf_inputs[12], data, 5);
prov_link.expect = PROV_PUB_KEY;
/* If Provisioning Start PDU indicates that provisioner chooses
* OOB public key, then callback to the application layer to let
* users input public & private key pair.
*/
if (prov_link.public_key == PROV_OOB_PUB_KEY) {
bt_mesh_prov_get()->oob_pub_key_cb();
}
if (prov_auth(data[2], data[3], data[4])) {
BT_ERR("Invalid authentication method: 0x%02x; "
"action: 0x%02x; size: 0x%02x",
data[2], data[3], data[4]);
close_link(PROV_ERR_NVAL_FMT);
}
}
static void send_confirm(void)
{
uint8_t *local_conf = NULL;
uint8_t conf_val_size = 0;
uint8_t rand_val_size = 0;
uint8_t conf_salt_size = 0;
uint8_t conf_key_size = 0;
conf_val_size = PROV_CONF_SIZE(&prov_link);
rand_val_size = PROV_RAND_SIZE(&prov_link);
conf_salt_size = PROV_CONF_SALT_SIZE(&prov_link);
conf_key_size = PROV_CONF_KEY_SIZE(&prov_link);
ARG_UNUSED(conf_salt_size);
ARG_UNUSED(conf_key_size);
PROV_BUF(cfm, (conf_val_size + 1));
BT_DBG("ConfInputs[0] %s", bt_hex(prov_link.conf_inputs, 64));
BT_DBG("ConfInputs[64] %s", bt_hex(&prov_link.conf_inputs[64], 64));
BT_DBG("ConfInputs[128] %s", bt_hex(&prov_link.conf_inputs[128], 17));
if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_conf_salt(prov_link.conf_inputs, prov_link.conf_salt)) {
BT_ERR("Unable to generate confirmation salt");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
if (bt_mesh_prov_conf_key(prov_link.dhkey, prov_link.conf_salt,
prov_link.conf_key)) {
BT_ERR("Unable to generate confirmation key");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_conf_salt_epa(prov_link.conf_inputs, prov_link.conf_salt)) {
BT_ERR("Unable to generate confirmation salt(epa)");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
if (bt_mesh_prov_conf_key_epa(prov_link.dhkey, prov_link.auth,
prov_link.conf_salt, prov_link.conf_key)) {
BT_ERR("Unable to generate confirmation key(epa)");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
}
#endif
if (bt_mesh_rand(prov_link.rand, rand_val_size)) {
BT_ERR("Unable to generate random number");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("ConfirmationSalt: %s", bt_hex(prov_link.conf_salt, conf_salt_size));
BT_DBG("ConfirmationKey: %s", bt_hex(prov_link.conf_key, conf_key_size));
BT_DBG("LocalRandom: %s", bt_hex(prov_link.rand, rand_val_size));
bt_mesh_prov_buf_init(&cfm, PROV_CONFIRM);
local_conf = net_buf_simple_add(&cfm, conf_val_size);
if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_conf(prov_link.conf_key, prov_link.rand,
prov_link.auth, local_conf)) {
BT_ERR("Unable to generate confirmation value");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_conf_epa(prov_link.conf_key, prov_link.rand,
local_conf)) {
BT_ERR("Unable to generate confirmation value(epa)");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
}
#endif
if (!memcmp(prov_link.conf, local_conf, conf_val_size)) {
BT_ERR("Confirmation value is identical to ours, rejecting.");
prov_send_fail_msg(PROV_ERR_NVAL_FMT);
return;
}
if (bt_mesh_prov_send(&prov_link, &cfm)) {
BT_ERR("Unable to send Provisioning Confirm");
return;
}
prov_link.expect = PROV_RANDOM;
}
static void send_input_complete(void)
{
PROV_BUF(buf, 1);
bt_mesh_prov_buf_init(&buf, PROV_INPUT_COMPLETE);
if (bt_mesh_prov_send(&prov_link, &buf)) {
BT_ERR("Failed to send Provisioning Input Complete");
}
}
int bt_mesh_input_number(uint32_t num)
{
uint8_t auth_size = 0;
auth_size = PROV_AUTH_SIZE(&prov_link);
BT_INFO("%u", num);
if (!bt_mesh_atomic_test_and_clear_bit(prov_link.flags, WAIT_NUMBER)) {
return -EINVAL;
}
sys_put_be32(num, &prov_link.auth[auth_size - 4]);
send_input_complete();
if (!bt_mesh_atomic_test_bit(prov_link.flags, HAVE_DHKEY)) {
return 0;
}
if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, SEND_CONFIRM)) {
send_confirm();
}
return 0;
}
int bt_mesh_input_string(const char *str)
{
BT_INFO("%s", str);
if (!bt_mesh_atomic_test_and_clear_bit(prov_link.flags, WAIT_STRING)) {
return -EINVAL;
}
(void)memcpy(prov_link.auth, str, bt_mesh_prov_get()->input_size);
send_input_complete();
if (!bt_mesh_atomic_test_bit(prov_link.flags, HAVE_DHKEY)) {
return 0;
}
if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, SEND_CONFIRM)) {
send_confirm();
}
return 0;
}
static void send_pub_key(void)
{
const uint8_t *key = NULL;
uint8_t dhkey[32] = {0};
PROV_BUF(buf, 65);
/* Copy remote key in little-endian for generating DHKey.
* X and Y halves are swapped independently. Use response
* buffer as a temporary storage location. The validating
* of the remote public key is finished when it is received.
*/
sys_memcpy_swap(buf.data, &prov_link.conf_inputs[17], 32);
sys_memcpy_swap(&buf.data[32], &prov_link.conf_inputs[49], 32);
if (bt_mesh_dh_key_gen(buf.data, dhkey)) {
BT_ERR("Unable to generate DHKey");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
sys_memcpy_swap(prov_link.dhkey, dhkey, 32);
BT_DBG("DHkey: %s", bt_hex(prov_link.dhkey, 32));
bt_mesh_atomic_set_bit(prov_link.flags, HAVE_DHKEY);
key = bt_mesh_pub_key_get();
if (!key) {
BT_ERR("No public key available");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("Local Public Key: %s", bt_hex(key, 64));
bt_mesh_prov_buf_init(&buf, PROV_PUB_KEY);
/* Swap X and Y halves independently to big-endian */
sys_memcpy_swap(net_buf_simple_add(&buf, 32), key, 32);
sys_memcpy_swap(net_buf_simple_add(&buf, 32), &key[32], 32);
memcpy(&prov_link.conf_inputs[81], &buf.data[1], 64);
if (bt_mesh_prov_send(&prov_link, &buf)) {
BT_ERR("Failed to send Public Key");
return;
}
prov_link.expect = PROV_CONFIRM;
}
static int bt_mesh_calc_dh_key(void)
{
uint8_t pub_key[64] = {0};
uint8_t dhkey[32] = {0};
/* Copy remote key in little-endian for generating DHKey.
* X and Y halves are swapped independently.
*/
sys_memcpy_swap(&pub_key[0], &prov_link.conf_inputs[17], 32);
sys_memcpy_swap(&pub_key[32], &prov_link.conf_inputs[49], 32);
if (bt_mesh_dh_key_gen(pub_key, dhkey)) {
BT_ERR("Unable to generate DHKey");
close_link(PROV_ERR_UNEXP_ERR);
return -EIO;
}
sys_memcpy_swap(prov_link.dhkey, dhkey, 32);
BT_DBG("DHkey: %s", bt_hex(prov_link.dhkey, 32));
bt_mesh_atomic_set_bit(prov_link.flags, HAVE_DHKEY);
if (bt_mesh_atomic_test_bit(prov_link.flags, WAIT_NUMBER) ||
bt_mesh_atomic_test_bit(prov_link.flags, WAIT_STRING)) {
return 0;
}
if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, SEND_CONFIRM)) {
send_confirm();
}
return 0;
}
int bt_mesh_set_oob_pub_key(const uint8_t pub_key_x[32],
const uint8_t pub_key_y[32],
const uint8_t pri_key[32])
{
if (!pub_key_x || !pub_key_y || !pri_key) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
/* Copy OOB public key in big-endian to Provisioning ConfirmationInputs,
* X and Y halves are swapped independently.
* And set input private key to mesh_bearer_adapt.c
*/
sys_memcpy_swap(&prov_link.conf_inputs[81], pub_key_x, 32);
sys_memcpy_swap(&prov_link.conf_inputs[81] + 32, pub_key_y, 32);
bt_mesh_set_private_key(pri_key);
bt_mesh_atomic_set_bit(prov_link.flags, OOB_PUB_KEY);
/* If remote public key is not got, just return */
if (!bt_mesh_atomic_test_bit(prov_link.flags, REMOTE_PUB_KEY)) {
return 0;
}
return bt_mesh_calc_dh_key();
}
static void prov_pub_key(const uint8_t *data)
{
BT_DBG("Remote Public Key: %s", bt_hex(data, 64));
/* BLE Mesh BQB test case MESH/NODE/PROV/UPD/BI-13-C needs to
* check the public key using the following rules:
* (1) X > 0, Y > 0
* (2) X > 0, Y = 0
* (3) X = 0, Y = 0
*/
if (!bt_mesh_check_public_key(data)) {
BT_ERR("Invalid public key");
close_link(PROV_ERR_UNEXP_PDU);
return;
}
memcpy(&prov_link.conf_inputs[17], data, 64);
bt_mesh_atomic_set_bit(prov_link.flags, REMOTE_PUB_KEY);
if (prov_link.public_key == PROV_NO_OOB_PUB_KEY) {
send_pub_key();
} else {
prov_link.expect = PROV_CONFIRM;
}
}
static void prov_input_complete(const uint8_t *data)
{}
static void prov_confirm(const uint8_t *data)
{
uint8_t conf_val_size = 0;
conf_val_size = PROV_CONF_SIZE(&prov_link);
BT_DBG("Remote Confirm: %s", bt_hex(data, conf_val_size));
memcpy(prov_link.conf, data, conf_val_size);
if (!bt_mesh_atomic_test_bit(prov_link.flags, HAVE_DHKEY)) {
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_prov_clear_tx(&prov_link, true);
#endif
bt_mesh_atomic_set_bit(prov_link.flags, SEND_CONFIRM);
/* If using OOB public key and it has already got, calculates dhkey */
if (prov_link.public_key == PROV_OOB_PUB_KEY &&
bt_mesh_atomic_test_bit(prov_link.flags, OOB_PUB_KEY)) {
bt_mesh_calc_dh_key();
}
} else {
send_confirm();
}
}
static void prov_random(const uint8_t *data)
{
uint8_t conf_verify[32] = {0};
uint8_t rand_val_size = 0;
uint8_t conf_val_size = 0;
rand_val_size = PROV_RAND_SIZE(&prov_link);
conf_val_size = PROV_CONF_SIZE(&prov_link);
PROV_BUF(rnd, rand_val_size);
BT_INFO("Remote Random: %s", bt_hex(data, rand_val_size));
if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_conf(prov_link.conf_key, data, prov_link.auth, conf_verify)) {
BT_ERR("Unable to calculate confirmation verification");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_conf_epa(prov_link.conf_key, data, conf_verify)) {
BT_ERR("Unable to calculate confirmation verification");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
}
#endif
if (memcmp(conf_verify, prov_link.conf, conf_val_size)) {
BT_ERR("Invalid confirmation value");
BT_ERR("Received: %s", bt_hex(prov_link.conf, conf_val_size));
BT_ERR("Calculated: %s", bt_hex(conf_verify, conf_val_size));
close_link(PROV_ERR_CFM_FAILED);
return;
}
bt_mesh_prov_buf_init(&rnd, PROV_RANDOM);
net_buf_simple_add_mem(&rnd, prov_link.rand, rand_val_size);
if (bt_mesh_prov_send(&prov_link, &rnd)) {
BT_ERR("Failed to send Provisioning Random");
return;
}
if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_salt(prov_link.conf_salt, data, prov_link.rand,
prov_link.prov_salt)) {
BT_ERR("Failed to generate provisioning salt");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_salt_epa(prov_link.conf_salt, data, prov_link.rand,
prov_link.prov_salt)) {
BT_ERR("Failed to generate provisioning salt");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
}
#endif
BT_DBG("ProvisioningSalt: %s", bt_hex(prov_link.prov_salt, 16));
prov_link.expect = PROV_DATA;
}
static inline bool is_pb_gatt(void)
{
#if CONFIG_BLE_MESH_PB_GATT
return !!prov_link.conn;
#else
return false;
#endif
}
static void prov_data(const uint8_t *data)
{
uint8_t session_key[16] = {0};
bool identity_enable = false;
uint8_t dev_key[16] = {0};
uint8_t nonce[13] = {0};
uint32_t iv_index = 0U;
uint16_t net_idx = 0U;
uint8_t pdu[25] = {0};
uint16_t addr = 0U;
uint8_t flags = 0U;
PROV_BUF(msg, 1);
int err = 0;
err = bt_mesh_session_key(prov_link.dhkey, prov_link.prov_salt, session_key);
if (err) {
BT_ERR("Unable to generate session key");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("SessionKey: %s", bt_hex(session_key, 16));
err = bt_mesh_prov_nonce(prov_link.dhkey, prov_link.prov_salt, nonce);
if (err) {
BT_ERR("Unable to generate session nonce");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("Nonce: %s", bt_hex(nonce, 13));
err = bt_mesh_prov_decrypt(session_key, nonce, data, pdu);
if (err) {
BT_ERR("Unable to decrypt provisioning data");
close_link(PROV_ERR_DECRYPT);
return;
}
net_idx = sys_get_be16(&pdu[16]);
flags = pdu[18];
iv_index = sys_get_be32(&pdu[19]);
addr = sys_get_be16(&pdu[23]);
BT_DBG("net_idx %u iv_index 0x%08x, addr 0x%04x",
net_idx, iv_index, addr);
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) {
uint8_t reason = 0;
if (bt_mesh_rpr_srv_nppi_check(prov_link.pb_remote_nppi, pdu, net_idx,
iv_index, addr, &reason) == false) {
close_link(reason);
return;
}
}
#endif /* CONFIG_BLE_MESH_RPR_SRV */
err = bt_mesh_dev_key(prov_link.dhkey, prov_link.prov_salt, dev_key);
if (err) {
BT_ERR("Unable to generate device key");
close_link(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("DevKey: %s", bt_hex(dev_key, 16));
#if CONFIG_BLE_MESH_RPR_SRV
/* Store NPPI data */
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) {
err = bt_mesh_rpr_srv_store_nppi_data(prov_link.pb_remote_uuid,
pdu, net_idx, flags,
iv_index, addr, dev_key);
if (err) {
close_link(PROV_ERR_UNEXP_ERR);
return;
}
}
#endif /* CONFIG_BLE_MESH_RPR_SRV */
bt_mesh_prov_buf_init(&msg, PROV_COMPLETE);
if (bt_mesh_prov_send(&prov_link, &msg)) {
BT_ERR("Failed to send Provisioning Complete");
return;
}
/* Ignore any further PDUs on this link */
prov_link.expect = 0U;
#if CONFIG_BLE_MESH_RPR_SRV
/* For NPPI, no need to perform the following actions */
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) {
return;
}
#endif /* CONFIG_BLE_MESH_RPR_SRV */
/* Store info, since bt_mesh_provision() will end up clearing it */
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
identity_enable = is_pb_gatt();
} else {
identity_enable = false;
}
err = bt_mesh_provision(pdu, net_idx, flags, iv_index, addr, dev_key);
if (err) {
BT_ERR("Failed to provision (err %d)", err);
return;
}
/* After PB-GATT provisioning we should start advertising
* using Node Identity.
*/
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && identity_enable) {
bt_mesh_proxy_identity_enable();
}
}
static void prov_complete(const uint8_t *data)
{}
static void prov_failed(const uint8_t *data)
{
BT_WARN("Error: 0x%02x", data[0]);
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
prov_link.pb_remote_cbd = true;
/* In this case, no need to send Link Close */
prov_link.pb_remote_reset = true;
close_link(data[0]);
return;
}
#endif /* CONFIG_BLE_MESH_RPR_SRV */
}
static const struct {
void (*func)(const uint8_t *data);
bool pb_remote; /* Indicate if the PDU could be received when PB-Remote is used */
} prov_handlers[] = {
{ prov_invite, false },
{ prov_capabilities, true },
{ prov_start, false },
{ prov_pub_key, true },
{ prov_input_complete, true },
{ prov_confirm, true },
{ prov_random, true },
{ prov_data, false },
{ prov_complete, true },
{ prov_failed, true },