-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
prov.c
1872 lines (1478 loc) · 47.4 KB
/
prov.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 */
/*
* Copyright (c) 2017 Intel Corporation
* Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE 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_server.h"
#include "prov.h"
#if CONFIG_BLE_MESH_NODE
/* 3 transmissions, 20ms interval */
#define PROV_XMIT BLE_MESH_TRANSMIT(2, 20)
#define AUTH_METHOD_NO_OOB 0x00
#define AUTH_METHOD_STATIC 0x01
#define AUTH_METHOD_OUTPUT 0x02
#define AUTH_METHOD_INPUT 0x03
#define OUTPUT_OOB_BLINK 0x00
#define OUTPUT_OOB_BEEP 0x01
#define OUTPUT_OOB_VIBRATE 0x02
#define OUTPUT_OOB_NUMBER 0x03
#define OUTPUT_OOB_STRING 0x04
#define INPUT_OOB_PUSH 0x00
#define INPUT_OOB_TWIST 0x01
#define INPUT_OOB_NUMBER 0x02
#define INPUT_OOB_STRING 0x03
#define PUB_KEY_NO_OOB 0x00
#define PUB_KEY_OOB 0x01
#define PROV_ERR_NONE 0x00
#define PROV_ERR_NVAL_PDU 0x01
#define PROV_ERR_NVAL_FMT 0x02
#define PROV_ERR_UNEXP_PDU 0x03
#define PROV_ERR_CFM_FAILED 0x04
#define PROV_ERR_RESOURCES 0x05
#define PROV_ERR_DECRYPT 0x06
#define PROV_ERR_UNEXP_ERR 0x07
#define PROV_ERR_ADDR 0x08
#define PROV_INVITE 0x00
#define PROV_CAPABILITIES 0x01
#define PROV_START 0x02
#define PROV_PUB_KEY 0x03
#define PROV_INPUT_COMPLETE 0x04
#define PROV_CONFIRM 0x05
#define PROV_RANDOM 0x06
#define PROV_DATA 0x07
#define PROV_COMPLETE 0x08
#define PROV_FAILED 0x09
#define PROV_ALG_P256 0x00
#define GPCF(gpc) (gpc & 0x03)
#define GPC_START(last_seg) (((last_seg) << 2) | 0x00)
#define GPC_ACK 0x01
#define GPC_CONT(seg_id) (((seg_id) << 2) | 0x02)
#define GPC_CTL(op) (((op) << 2) | 0x03)
#define START_PAYLOAD_MAX 20
#define CONT_PAYLOAD_MAX 23
#define START_LAST_SEG_MAX 2
#define START_LAST_SEG(gpc) (gpc >> 2)
#define CONT_SEG_INDEX(gpc) (gpc >> 2)
#define BEARER_CTL(gpc) (gpc >> 2)
#define LINK_OPEN 0x00
#define LINK_ACK 0x01
#define LINK_CLOSE 0x02
#define CLOSE_REASON_SUCCESS 0x00
#define CLOSE_REASON_TIMEOUT 0x01
#define CLOSE_REASON_FAILED 0x02
#define XACT_SEG_DATA(_seg) (&link.rx.buf->data[20 + ((_seg - 1) * 23)])
#define XACT_SEG_RECV(_seg) (link.rx.seg &= ~(1 << (_seg)))
#define XACT_NVAL 0xff
enum {
REMOTE_PUB_KEY, /* Remote key has been received */
OOB_PUB_KEY, /* OOB public key is available */
LINK_ACTIVE, /* Link has been opened */
HAVE_DHKEY, /* DHKey has been calculated */
SEND_CONFIRM, /* Waiting to send Confirm value */
WAIT_NUMBER, /* Waiting for number input from user */
WAIT_STRING, /* Waiting for string input from user */
LINK_INVALID, /* Error occurred during provisioning */
NUM_FLAGS,
};
struct prov_link {
BLE_MESH_ATOMIC_DEFINE(flags, NUM_FLAGS);
#if defined(CONFIG_BLE_MESH_PB_GATT)
struct bt_mesh_conn *conn; /* GATT connection */
#endif
uint8_t dhkey[32]; /* Calculated DHKey */
uint8_t expect; /* Next expected PDU */
bool oob_pk_flag; /* Flag indicates whether using OOB public key */
uint8_t oob_method;
uint8_t oob_action;
uint8_t oob_size;
uint8_t conf[16]; /* Remote Confirmation */
uint8_t rand[16]; /* Local Random */
uint8_t auth[16]; /* Authentication Value */
uint8_t conf_salt[16]; /* ConfirmationSalt */
uint8_t conf_key[16]; /* ConfirmationKey */
uint8_t conf_inputs[145]; /* ConfirmationInputs */
uint8_t prov_salt[16]; /* Provisioning Salt */
#if defined(CONFIG_BLE_MESH_PB_ADV)
uint32_t id; /* Link ID */
uint8_t tx_pdu_type; /* The previously transmitted Provisioning PDU type */
struct {
uint8_t id; /* Transaction ID */
uint8_t prev_id; /* Previous Transaction ID */
uint8_t seg; /* Bit-field of unreceived segments */
uint8_t last_seg; /* Last segment (to check length) */
uint8_t fcs; /* Expected FCS value */
struct net_buf_simple *buf;
} rx;
struct {
/* Start timestamp of the transaction */
int64_t start;
/* Transaction id*/
uint8_t id;
/* Pending outgoing buffer(s) */
struct net_buf *buf[3];
/* Retransmit timer */
struct k_delayed_work retransmit;
} tx;
#endif
struct k_delayed_work prot_timer;
};
struct prov_rx {
uint32_t link_id;
uint8_t xact_id;
uint8_t gpc;
};
#define BUF_TIMEOUT K_MSEC(400)
#if defined(CONFIG_BLE_MESH_FAST_PROV)
#define RETRANSMIT_TIMEOUT K_MSEC(360)
#define TRANSACTION_TIMEOUT K_SECONDS(3)
#define PROTOCOL_TIMEOUT K_SECONDS(6)
#else
#define RETRANSMIT_TIMEOUT K_MSEC(500)
#define TRANSACTION_TIMEOUT K_SECONDS(30)
#define PROTOCOL_TIMEOUT K_SECONDS(60)
#endif /* CONFIG_BLE_MESH_FAST_PROV */
#if defined(CONFIG_BLE_MESH_PB_GATT)
#define PROV_BUF_HEADROOM 5
#else
#define PROV_BUF_HEADROOM 0
NET_BUF_SIMPLE_DEFINE_STATIC(rx_buf, 65);
#endif
#define PROV_BUF(name, len) \
NET_BUF_SIMPLE_DEFINE(name, PROV_BUF_HEADROOM + len)
static struct prov_link link;
static const struct bt_mesh_prov *prov;
#if defined(CONFIG_BLE_MESH_PB_ADV)
static bt_mesh_mutex_t pb_buf_lock;
static inline void bt_mesh_pb_buf_mutex_new(void)
{
if (!pb_buf_lock.mutex) {
bt_mesh_mutex_create(&pb_buf_lock);
}
}
#if CONFIG_BLE_MESH_DEINIT
static inline void bt_mesh_pb_buf_mutex_free(void)
{
bt_mesh_mutex_free(&pb_buf_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
static inline void bt_mesh_pb_buf_lock(void)
{
bt_mesh_mutex_lock(&pb_buf_lock);
}
static inline void bt_mesh_pb_buf_unlock(void)
{
bt_mesh_mutex_unlock(&pb_buf_lock);
}
#endif /* CONFIG_BLE_MESH_PB_ADV */
static void reset_state(void)
{
k_delayed_work_cancel(&link.prot_timer);
/* Disable Attention Timer if it was set */
if (link.conf_inputs[0]) {
bt_mesh_attention(NULL, 0);
}
#if defined(CONFIG_BLE_MESH_PB_GATT)
if (link.conn) {
bt_mesh_conn_unref(link.conn);
}
#endif
#if defined(CONFIG_BLE_MESH_PB_ADV)
/* Clear everything except the retransmit and protocol timer
* delayed work objects.
*/
(void)memset(&link, 0, offsetof(struct prov_link, tx.retransmit));
link.rx.prev_id = XACT_NVAL;
#if defined(CONFIG_BLE_MESH_PB_GATT)
link.rx.buf = bt_mesh_proxy_server_get_buf();
#else
net_buf_simple_reset(&rx_buf);
link.rx.buf = &rx_buf;
#endif /* PB_GATT */
#else /* !PB_ADV */
/* Clear everything except the protocol timer (k_delayed_work) */
(void)memset(&link, 0, offsetof(struct prov_link, prot_timer));
#endif /* PB_ADV */
}
#if defined(CONFIG_BLE_MESH_PB_ADV)
static void buf_sent(int err, void *user_data)
{
if (!link.tx.buf[0]) {
return;
}
k_delayed_work_submit(&link.tx.retransmit, RETRANSMIT_TIMEOUT);
}
static struct bt_mesh_send_cb buf_sent_cb = {
.end = buf_sent,
};
static void free_segments(void)
{
int i;
bt_mesh_pb_buf_lock();
for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) {
struct net_buf *buf = link.tx.buf[i];
if (!buf) {
break;
}
link.tx.buf[i] = NULL;
bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL);
/* Mark as canceled */
BLE_MESH_ADV(buf)->busy = 0U;
net_buf_unref(buf);
}
bt_mesh_pb_buf_unlock();
}
static void prov_clear_tx(void)
{
BT_DBG("%s", __func__);
k_delayed_work_cancel(&link.tx.retransmit);
free_segments();
}
static void reset_adv_link(void)
{
prov_clear_tx();
if (prov->link_close) {
prov->link_close(BLE_MESH_PROV_ADV);
}
#if defined(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, &link.id);
#endif
reset_state();
}
static struct net_buf *adv_buf_create(void)
{
struct net_buf *buf = NULL;
buf = bt_mesh_adv_create(BLE_MESH_ADV_PROV, PROV_XMIT, BUF_TIMEOUT);
if (!buf) {
BT_ERR("Out of provisioning buffers");
return NULL;
}
return buf;
}
static uint8_t pending_ack = XACT_NVAL;
static void ack_complete(uint16_t duration, int err, void *user_data)
{
BT_DBG("xact %u complete", (uint8_t)pending_ack);
pending_ack = XACT_NVAL;
}
static void gen_prov_ack_send(uint8_t xact_id)
{
static const struct bt_mesh_send_cb cb = {
.start = ack_complete,
};
const struct bt_mesh_send_cb *complete = NULL;
struct net_buf *buf = NULL;
BT_DBG("xact_id %u", xact_id);
if (pending_ack == xact_id) {
BT_DBG("Not sending duplicate ack");
return;
}
buf = adv_buf_create();
if (!buf) {
return;
}
if (pending_ack == XACT_NVAL) {
pending_ack = xact_id;
complete = &cb;
} else {
complete = NULL;
}
net_buf_add_be32(buf, link.id);
net_buf_add_u8(buf, xact_id);
net_buf_add_u8(buf, GPC_ACK);
bt_mesh_adv_send(buf, complete, NULL);
net_buf_unref(buf);
}
static void send_reliable(void)
{
int i;
link.tx.start = k_uptime_get();
for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) {
struct net_buf *buf = link.tx.buf[i];
if (!buf) {
break;
}
if (i + 1 < ARRAY_SIZE(link.tx.buf) && link.tx.buf[i + 1]) {
bt_mesh_adv_send(buf, NULL, NULL);
} else {
bt_mesh_adv_send(buf, &buf_sent_cb, NULL);
}
}
}
static int bearer_ctl_send(uint8_t op, void *data, uint8_t data_len)
{
struct net_buf *buf = NULL;
BT_DBG("op 0x%02x data_len %u", op, data_len);
prov_clear_tx();
buf = adv_buf_create();
if (!buf) {
return -ENOBUFS;
}
net_buf_add_be32(buf, link.id);
/* Transaction ID, always 0 for Bearer messages */
net_buf_add_u8(buf, 0x00);
net_buf_add_u8(buf, GPC_CTL(op));
net_buf_add_mem(buf, data, data_len);
link.tx.buf[0] = buf;
send_reliable();
return 0;
}
static uint8_t last_seg(uint8_t len)
{
if (len <= START_PAYLOAD_MAX) {
return 0;
}
len -= START_PAYLOAD_MAX;
return 1 + (len / CONT_PAYLOAD_MAX);
}
static inline uint8_t next_transaction_id(void)
{
if (link.tx.id != 0U && link.tx.id != 0xFF) {
return ++link.tx.id;
}
link.tx.id = 0x80;
return link.tx.id;
}
static int prov_send_adv(struct net_buf_simple *msg)
{
struct net_buf *start = NULL, *buf = NULL;
uint8_t seg_len = 0U, seg_id = 0U;
uint8_t xact_id = 0U;
int32_t timeout = PROTOCOL_TIMEOUT;
BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len));
prov_clear_tx();
start = adv_buf_create();
if (!start) {
return -ENOBUFS;
}
xact_id = next_transaction_id();
net_buf_add_be32(start, link.id);
net_buf_add_u8(start, xact_id);
net_buf_add_u8(start, GPC_START(last_seg(msg->len)));
net_buf_add_be16(start, msg->len);
net_buf_add_u8(start, bt_mesh_fcs_calc(msg->data, msg->len));
link.tx.buf[0] = start;
/* Changed by Espressif, get message type */
link.tx_pdu_type = msg->data[0];
seg_len = MIN(msg->len, START_PAYLOAD_MAX);
BT_DBG("seg 0 len %u: %s", seg_len, bt_hex(msg->data, seg_len));
net_buf_add_mem(start, msg->data, seg_len);
net_buf_simple_pull(msg, seg_len);
buf = start;
for (seg_id = 1U; msg->len > 0; seg_id++) {
if (seg_id >= ARRAY_SIZE(link.tx.buf)) {
BT_ERR("Too big message (seg_id %d)", seg_id);
free_segments();
return -E2BIG;
}
buf = adv_buf_create();
if (!buf) {
free_segments();
return -ENOBUFS;
}
link.tx.buf[seg_id] = buf;
seg_len = MIN(msg->len, CONT_PAYLOAD_MAX);
BT_DBG("seg_id %u len %u: %s", seg_id, seg_len,
bt_hex(msg->data, seg_len));
net_buf_add_be32(buf, link.id);
net_buf_add_u8(buf, xact_id);
net_buf_add_u8(buf, GPC_CONT(seg_id));
net_buf_add_mem(buf, msg->data, seg_len);
net_buf_simple_pull(msg, seg_len);
}
send_reliable();
/* Changed by Espressif, add provisioning timeout timer operations.
* When sending a provisioning PDU successfully, restart the 60s timer.
*/
#if defined(CONFIG_BLE_MESH_FAST_PROV)
if (link.tx_pdu_type >= PROV_COMPLETE) {
timeout = K_SECONDS(60);
}
#endif
k_delayed_work_submit(&link.prot_timer, timeout);
return 0;
}
#endif /* CONFIG_BLE_MESH_PB_ADV */
#if defined(CONFIG_BLE_MESH_PB_GATT)
static int prov_send_gatt(struct net_buf_simple *msg)
{
int err = 0;
if (!link.conn) {
return -ENOTCONN;
}
/* Changed by Espressif, add provisioning timeout timer operations.
* When sending a provisioning PDU successfully, restart the 60s timer.
*/
err = bt_mesh_proxy_server_send(link.conn, BLE_MESH_PROXY_PROV, msg);
if (err) {
BT_ERR("Failed to send provisioning PDU");
return err;
}
k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT);
return 0;
}
#endif /* CONFIG_BLE_MESH_PB_GATT */
static inline int prov_send(struct net_buf_simple *buf)
{
#if defined(CONFIG_BLE_MESH_PB_GATT)
if (link.conn) {
return prov_send_gatt(buf);
}
#endif
#if defined(CONFIG_BLE_MESH_PB_ADV)
return prov_send_adv(buf);
#else
return 0;
#endif
}
static void prov_buf_init(struct net_buf_simple *buf, uint8_t type)
{
net_buf_simple_reserve(buf, PROV_BUF_HEADROOM);
net_buf_simple_add_u8(buf, type);
}
static void prov_send_fail_msg(uint8_t err)
{
PROV_BUF(buf, 2);
prov_buf_init(&buf, PROV_FAILED);
net_buf_simple_add_u8(&buf, err);
if (prov_send(&buf)) {
BT_ERR("Failed to send Provisioning Failed message");
}
bt_mesh_atomic_set_bit(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]);
}
link.conf_inputs[0] = data[0];
prov_buf_init(&buf, PROV_CAPABILITIES);
/* Number of Elements supported */
net_buf_simple_add_u8(&buf, bt_mesh_elem_count());
/* Supported algorithms - FIPS P-256 Eliptic Curve */
net_buf_simple_add_be16(&buf, BIT(PROV_ALG_P256));
/* Public Key Type */
net_buf_simple_add_u8(&buf, prov->oob_pub_key);
/* Static OOB Type */
net_buf_simple_add_u8(&buf, prov->static_val ? BIT(0) : 0x00);
/* Output OOB Size */
net_buf_simple_add_u8(&buf, prov->output_size);
/* Output OOB Action */
net_buf_simple_add_be16(&buf, prov->output_actions);
/* Input OOB Size */
net_buf_simple_add_u8(&buf, prov->input_size);
/* Input OOB Action */
net_buf_simple_add_be16(&buf, prov->input_actions);
memcpy(&link.conf_inputs[1], &buf.data[1], 11);
if (prov_send(&buf)) {
BT_ERR("Failed to send capabilities");
return;
}
link.expect = PROV_START;
}
static void prov_capabilities(const uint8_t *data)
{
uint16_t algorithms = 0U, output_action = 0U, input_action = 0U;
BT_DBG("Elements: %u", data[0]);
algorithms = sys_get_be16(&data[1]);
BT_DBG("Algorithms: %u", algorithms);
BT_DBG("Public Key Type: 0x%02x", data[3]);
BT_DBG("Static OOB Type: 0x%02x", data[4]);
BT_DBG("Output OOB Size: %u", data[5]);
output_action = sys_get_be16(&data[6]);
BT_DBG("Output OOB Action: 0x%04x", output_action);
BT_DBG("Input OOB Size: %u", data[8]);
input_action = sys_get_be16(&data[9]);
BT_DBG("Input OOB Action: 0x%04x", input_action);
((void) algorithms);
((void) output_action);
((void) input_action);
}
static bt_mesh_output_action_t output_action(uint8_t action)
{
switch (action) {
case OUTPUT_OOB_BLINK:
return BLE_MESH_BLINK;
case OUTPUT_OOB_BEEP:
return BLE_MESH_BEEP;
case OUTPUT_OOB_VIBRATE:
return BLE_MESH_VIBRATE;
case OUTPUT_OOB_NUMBER:
return BLE_MESH_DISPLAY_NUMBER;
case OUTPUT_OOB_STRING:
return BLE_MESH_DISPLAY_STRING;
default:
return BLE_MESH_NO_OUTPUT;
}
}
static bt_mesh_input_action_t input_action(uint8_t action)
{
switch (action) {
case INPUT_OOB_PUSH:
return BLE_MESH_PUSH;
case INPUT_OOB_TWIST:
return BLE_MESH_TWIST;
case INPUT_OOB_NUMBER:
return BLE_MESH_ENTER_NUMBER;
case INPUT_OOB_STRING:
return BLE_MESH_ENTER_STRING;
default:
return BLE_MESH_NO_INPUT;
}
}
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;
switch (method) {
case AUTH_METHOD_NO_OOB:
if (action || size) {
return -EINVAL;
}
(void)memset(link.auth, 0, sizeof(link.auth));
return 0;
case AUTH_METHOD_STATIC:
if (action || size) {
return -EINVAL;
}
memcpy(link.auth + 16 - prov->static_val_len,
prov->static_val, prov->static_val_len);
(void)memset(link.auth, 0,
sizeof(link.auth) - prov->static_val_len);
return 0;
case AUTH_METHOD_OUTPUT:
output = output_action(action);
if (!output) {
return -EINVAL;
}
if (!(prov->output_actions & output)) {
return -EINVAL;
}
if (size > prov->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(link.auth, str, size);
(void)memset(link.auth + size, 0,
sizeof(link.auth) - size);
return prov->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, &link.auth[12]);
(void)memset(link.auth, 0, 12);
return prov->output_number(output, num);
}
case AUTH_METHOD_INPUT:
input = input_action(action);
if (!input) {
return -EINVAL;
}
if (!(prov->input_actions & input)) {
return -EINVAL;
}
if (size > prov->input_size) {
return -EINVAL;
}
if (input == BLE_MESH_ENTER_STRING) {
bt_mesh_atomic_set_bit(link.flags, WAIT_STRING);
} else {
bt_mesh_atomic_set_bit(link.flags, WAIT_NUMBER);
}
return prov->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]);
if (data[0] != PROV_ALG_P256) {
BT_ERR("Unknown algorithm 0x%02x", data[0]);
prov_send_fail_msg(PROV_ERR_NVAL_FMT);
return;
}
if (data[1] != prov->oob_pub_key) {
BT_ERR("Invalid public key type: 0x%02x", data[1]);
prov_send_fail_msg(PROV_ERR_NVAL_FMT);
return;
}
memcpy(&link.conf_inputs[12], data, 5);
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.
*/
link.oob_pk_flag = data[1] ? true : false;
if (link.oob_pk_flag) {
prov->oob_pub_key_cb();
}
if (prov_auth(data[2], data[3], data[4]) < 0) {
BT_ERR("Invalid authentication method: 0x%02x; "
"action: 0x%02x; size: 0x%02x",
data[2], data[3], data[4]);
prov_send_fail_msg(PROV_ERR_NVAL_FMT);
}
}
static void send_confirm(void)
{
uint8_t *local_conf = NULL;
PROV_BUF(cfm, 17);
BT_DBG("ConfInputs[0] %s", bt_hex(link.conf_inputs, 64));
BT_DBG("ConfInputs[64] %s", bt_hex(&link.conf_inputs[64], 64));
BT_DBG("ConfInputs[128] %s", bt_hex(&link.conf_inputs[128], 17));
if (bt_mesh_prov_conf_salt(link.conf_inputs, link.conf_salt)) {
BT_ERR("Unable to generate confirmation salt");
prov_send_fail_msg(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("ConfirmationSalt: %s", bt_hex(link.conf_salt, 16));
if (bt_mesh_prov_conf_key(link.dhkey, link.conf_salt, link.conf_key)) {
BT_ERR("Unable to generate confirmation key");
prov_send_fail_msg(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("ConfirmationKey: %s", bt_hex(link.conf_key, 16));
if (bt_mesh_rand(link.rand, 16)) {
BT_ERR("Unable to generate random number");
prov_send_fail_msg(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("LocalRandom: %s", bt_hex(link.rand, 16));
prov_buf_init(&cfm, PROV_CONFIRM);
local_conf = net_buf_simple_add(&cfm, 16);
if (bt_mesh_prov_conf(link.conf_key, link.rand, link.auth,
local_conf)) {
BT_ERR("Unable to generate confirmation value");
prov_send_fail_msg(PROV_ERR_UNEXP_ERR);
return;
}
if (!memcmp(link.conf, local_conf, 16)) {
BT_ERR("Confirmation value is identical to ours, rejecting.");
prov_send_fail_msg(PROV_ERR_NVAL_FMT);
return;
}
if (prov_send(&cfm)) {
BT_ERR("Unable to send Provisioning Confirm");
return;
}
link.expect = PROV_RANDOM;
}
static void send_input_complete(void)
{
PROV_BUF(buf, 1);
prov_buf_init(&buf, PROV_INPUT_COMPLETE);
if (prov_send(&buf)) {
BT_ERR("Failed to send Provisioning Input Complete");
}
}
int bt_mesh_input_number(uint32_t num)
{
BT_INFO("%u", num);
if (!bt_mesh_atomic_test_and_clear_bit(link.flags, WAIT_NUMBER)) {
return -EINVAL;
}
sys_put_be32(num, &link.auth[12]);
send_input_complete();
if (!bt_mesh_atomic_test_bit(link.flags, HAVE_DHKEY)) {
return 0;
}
if (bt_mesh_atomic_test_and_clear_bit(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(link.flags, WAIT_STRING)) {
return -EINVAL;
}
(void)memcpy(link.auth, str, prov->input_size);
send_input_complete();
if (!bt_mesh_atomic_test_bit(link.flags, HAVE_DHKEY)) {
return 0;
}
if (bt_mesh_atomic_test_and_clear_bit(link.flags, SEND_CONFIRM)) {
send_confirm();
}
return 0;
}
static void prov_dh_key_cb(const uint8_t key[32], const uint8_t idx)
{
BT_DBG("%p", key);
if (!key) {
BT_ERR("DHKey generation failed");
prov_send_fail_msg(PROV_ERR_UNEXP_ERR);
return;
}
sys_memcpy_swap(link.dhkey, key, 32);
BT_DBG("DHkey: %s", bt_hex(link.dhkey, 32));
bt_mesh_atomic_set_bit(link.flags, HAVE_DHKEY);
if (bt_mesh_atomic_test_bit(link.flags, WAIT_NUMBER) ||
bt_mesh_atomic_test_bit(link.flags, WAIT_STRING)) {
return;
}
if (bt_mesh_atomic_test_and_clear_bit(link.flags, SEND_CONFIRM)) {
send_confirm();
}
}
static void send_pub_key(void)
{
PROV_BUF(buf, 65);
const uint8_t *key = NULL;
/* Copy remote key in little-endian for bt_mesh_dh_key_gen().
* 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, &link.conf_inputs[17], 32);
sys_memcpy_swap(&buf.data[32], &link.conf_inputs[49], 32);
if (bt_mesh_dh_key_gen(buf.data, prov_dh_key_cb, 0)) {
BT_ERR("Unable to generate DHKey");
prov_send_fail_msg(PROV_ERR_UNEXP_ERR);
return;
}