xref: /btstack/src/mesh/provisioning_device.c (revision 046b44372d25c7bd6fe78e5cf6e5176a1979f437)
1 /*
2  * Copyright (C) 2017 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "provisioning_device.c"
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "btstack.h"
46 #include "btstack_memory.h"
47 
48 #include "mesh/mesh_crypto.h"
49 #ifdef ENABLE_MESH_ADV_BEARER
50 #include "mesh/pb_adv.h"
51 #endif
52 #ifdef ENABLE_MESH_GATT_BEARER
53 #include "mesh/pb_gatt.h"
54 #endif
55 #include "mesh/provisioning.h"
56 
57 static void prov_key_generated(void * arg);
58 
59 // remote ecc
60 static uint8_t remote_ec_q[64];
61 static uint8_t dhkey[32];
62 
63 static btstack_packet_handler_t prov_packet_handler;
64 
65 static uint8_t  prov_buffer_out[MESH_PROV_MAX_PROXY_PDU];
66 // ConfirmationInputs = ProvisioningInvitePDUValue || ProvisioningCapabilitiesPDUValue || ProvisioningStartPDUValue || PublicKeyProvisioner || PublicKeyDevice
67 static uint8_t  prov_confirmation_inputs[1 + 11 + 5 + 64 + 64];
68 static uint8_t  prov_authentication_method;
69 static uint8_t  prov_public_key_oob_used;
70 static uint8_t  prov_emit_public_key_oob_active;
71 static uint8_t  prov_emit_output_oob_active;
72 static uint8_t  prov_ec_q[64];
73 
74 static const uint8_t * prov_public_key_oob_q;
75 static const uint8_t * prov_public_key_oob_d;
76 
77 // num elements
78 static uint8_t  prov_num_elements = 1;
79 
80 // capabilites
81 static const uint8_t * prov_static_oob_data;
82 
83 static uint16_t  prov_static_oob_len;
84 static uint16_t  prov_output_oob_actions;
85 static uint16_t  prov_input_oob_actions;
86 static uint8_t   prov_public_key_oob_available;
87 static uint8_t   prov_static_oob_available;
88 static uint8_t   prov_output_oob_size;
89 static uint8_t   prov_input_oob_size;
90 static uint8_t   prov_error_code;
91 static uint8_t   prov_waiting_for_outgoing_complete;
92 
93 static uint8_t                      prov_attention_timer_timeout;
94 
95 static btstack_timer_source_t       prov_protocol_timer;
96 
97 static btstack_crypto_aes128_cmac_t prov_cmac_request;
98 static btstack_crypto_random_t      prov_random_request;
99 static btstack_crypto_ecc_p256_t    prov_ecc_p256_request;
100 static btstack_crypto_ccm_t         prov_ccm_request;
101 
102 // ConfirmationDevice
103 static uint8_t confirmation_device[16];
104 // ConfirmationSalt
105 static uint8_t confirmation_salt[16];
106 // ConfirmationKey
107 static uint8_t confirmation_key[16];
108 // RandomDevice
109 static uint8_t random_device[16];
110 // ProvisioningSalt
111 static uint8_t provisioning_salt[16];
112 // AuthValue
113 static uint8_t auth_value[16];
114 // SessionKey
115 static uint8_t session_key[16];
116 // SessionNonce
117 static uint8_t session_nonce[16];
118 // EncProvisioningData
119 static uint8_t enc_provisioning_data[25];
120 // ProvisioningData
121 static uint8_t provisioning_data[25];
122 
123 // received network_key
124 static mesh_network_key_t * network_key;
125 
126 // DeviceKey
127 static uint8_t device_key[16];
128 
129 static uint8_t  flags;
130 
131 static uint32_t iv_index;
132 static uint16_t unicast_address;
133 
134 typedef enum {
135     DEVICE_W4_INVITE,
136     DEVICE_SEND_CAPABILITIES,
137     DEVICE_W4_START,
138     DEVICE_W4_INPUT_OOK,
139     DEVICE_SEND_INPUT_COMPLETE,
140     DEVICE_W4_PUB_KEY,
141     DEVICE_SEND_PUB_KEY,
142     DEVICE_W4_CONFIRM,
143     DEVICE_SEND_CONFIRM,
144     DEVICE_W4_RANDOM,
145     DEVICE_SEND_RANDOM,
146     DEVICE_W4_DATA,
147     DEVICE_SEND_COMPLETE,
148     DEVICE_SEND_ERROR,
149 } device_state_t;
150 
151 static device_state_t device_state;
152 static uint16_t pb_transport_cid;
153 static mesh_pb_type_t pb_type;
154 
155 static void pb_send_pdu(uint16_t transport_cid, const uint8_t * buffer, uint16_t buffer_size){
156     switch (pb_type){
157 #ifdef ENABLE_MESH_ADV_BEARER
158         case MESH_PB_TYPE_ADV:
159             pb_adv_send_pdu(transport_cid, buffer, buffer_size);
160             break;
161 #endif
162 #ifdef ENABLE_MESH_GATT_BEARER
163         case MESH_PB_TYPE_GATT:
164             pb_gatt_send_pdu(transport_cid, buffer, buffer_size);
165             break;
166 #endif
167         default:
168             break;
169      }
170 }
171 
172 static void pb_close_link(uint16_t transport_cid, uint8_t reason){
173     switch (pb_type){
174 #ifdef ENABLE_MESH_ADV_BEARER
175         case MESH_PB_TYPE_ADV:
176             pb_adv_close_link(transport_cid, reason);
177             break;
178 #endif
179 #ifdef ENABLE_MESH_GATT_BEARER
180         case MESH_PB_TYPE_GATT:
181             pb_gatt_close_link(transport_cid, reason);
182             break;
183 #endif
184         default:
185             break;
186     }
187 }
188 
189 static void provisioning_emit_event(uint16_t pb_adv_cid, uint8_t mesh_subevent){
190     if (!prov_packet_handler) return;
191     uint8_t event[5] = { HCI_EVENT_MESH_META, 3, mesh_subevent};
192     little_endian_store_16(event, 3, pb_adv_cid);
193     prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
194 }
195 
196 static void provisioning_emit_output_oob_event(uint16_t pb_adv_cid, uint32_t number){
197     if (!prov_packet_handler) return;
198     uint8_t event[9] = { HCI_EVENT_MESH_META, 7, MESH_SUBEVENT_PB_PROV_START_EMIT_OUTPUT_OOB};
199     little_endian_store_16(event, 3, pb_adv_cid);
200     little_endian_store_32(event, 5, number);
201     prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
202 }
203 
204 static void provisioning_emit_attention_timer_event(uint16_t pb_adv_cid, uint8_t timer_s){
205     if (!prov_packet_handler) return;
206     uint8_t event[6] = { HCI_EVENT_MESH_META, 4, MESH_SUBEVENT_PB_PROV_ATTENTION_TIMER};
207     little_endian_store_16(event, 3, pb_adv_cid);
208     event[5] = timer_s;
209     prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
210 }
211 
212 static void provisiong_timer_handler(btstack_timer_source_t * ts){
213     UNUSED(ts);
214     printf("Provisioning Protocol Timeout -> Close Link!\n");
215     pb_close_link(1, 1);
216 }
217 
218 // The provisioning protocol shall have a minimum timeout of 60 seconds that is reset
219 // each time a provisioning protocol PDU is sent or received
220 static void provisioning_timer_start(void){
221     btstack_run_loop_remove_timer(&prov_protocol_timer);
222     btstack_run_loop_set_timer_handler(&prov_protocol_timer, &provisiong_timer_handler);
223     btstack_run_loop_set_timer(&prov_protocol_timer, PROVISIONING_PROTOCOL_TIMEOUT_MS);
224     btstack_run_loop_add_timer(&prov_protocol_timer);
225 }
226 
227 static void provisioning_timer_stop(void){
228     btstack_run_loop_remove_timer(&prov_protocol_timer);
229 }
230 
231 
232 // Outgoing Provisioning PDUs
233 static void provisioning_send_provisioning_error(void){
234     // setup response
235     prov_buffer_out[0] = MESH_PROV_FAILED;
236     prov_buffer_out[1] = prov_error_code;
237     pb_send_pdu(pb_transport_cid, prov_buffer_out, 2);
238 }
239 
240 static void provisioning_send_capabilites(void){
241     // setup response
242     prov_buffer_out[0] = MESH_PROV_CAPABILITIES;
243 
244     /* Number of Elements supported */
245     prov_buffer_out[1] = prov_num_elements;
246 
247     /* Supported algorithms - FIPS P-256 Eliptic Curve */
248     big_endian_store_16(prov_buffer_out, 2, 1);
249 
250     /* Public Key Type - Public Key OOB information available */
251     prov_buffer_out[4] = prov_public_key_oob_available;
252 
253     /* Static OOB Type - Static OOB information available */
254     prov_buffer_out[5] = prov_static_oob_available;
255 
256     /* Output OOB Size - max of 8 */
257     prov_buffer_out[6] = prov_output_oob_size;
258 
259     /* Output OOB Action */
260     big_endian_store_16(prov_buffer_out, 7, prov_output_oob_actions);
261 
262     /* Input OOB Size - max of 8*/
263     prov_buffer_out[9] = prov_input_oob_size;
264 
265     /* Input OOB Action */
266     big_endian_store_16(prov_buffer_out, 10, prov_input_oob_actions);
267 
268     // store for confirmation inputs: len 11
269     memcpy(&prov_confirmation_inputs[1], &prov_buffer_out[1], 11);
270 
271     // send
272 
273     pb_send_pdu(pb_transport_cid, prov_buffer_out, 12);
274 }
275 
276 static void provisioning_send_public_key(void){
277     // setup response
278     prov_buffer_out[0] = MESH_PROV_PUB_KEY;
279     memcpy(&prov_buffer_out[1], prov_ec_q, 64);
280 
281     // store for confirmation inputs: len 64
282     memcpy(&prov_confirmation_inputs[81], &prov_buffer_out[1], 64);
283 
284     // send
285     pb_send_pdu(pb_transport_cid, prov_buffer_out, 65);
286 }
287 
288 static void provisioning_send_input_complete(void){
289     // setup response
290     prov_buffer_out[0] = MESH_PROV_INPUT_COMPLETE;
291 
292     // send
293     pb_send_pdu(pb_transport_cid, prov_buffer_out, 17);
294 }
295 static void provisioning_send_confirm(void){
296     // setup response
297     prov_buffer_out[0] = MESH_PROV_CONFIRM;
298     memcpy(&prov_buffer_out[1], confirmation_device, 16);
299 
300     // send
301     pb_send_pdu(pb_transport_cid, prov_buffer_out, 17);
302 }
303 
304 static void provisioning_send_random(void){
305     // setup response
306     prov_buffer_out[0] = MESH_PROV_RANDOM;
307     memcpy(&prov_buffer_out[1],  random_device, 16);
308 
309     // send pdu
310     pb_send_pdu(pb_transport_cid, prov_buffer_out, 17);
311 }
312 
313 static void provisioning_send_complete(void){
314     // setup response
315     prov_buffer_out[0] = MESH_PROV_COMPLETE;
316 
317     // send pdu
318     pb_send_pdu(pb_transport_cid, prov_buffer_out, 1);
319 }
320 
321 static void provisioning_done(void){
322     if (prov_emit_public_key_oob_active){
323         prov_emit_public_key_oob_active = 0;
324         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_STOP_EMIT_PUBLIC_KEY_OOB);
325     }
326     if (prov_emit_output_oob_active){
327         prov_emit_output_oob_active = 0;
328         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_STOP_EMIT_OUTPUT_OOB);
329     }
330     if (prov_attention_timer_timeout){
331         prov_attention_timer_timeout = 0;
332         provisioning_emit_attention_timer_event(1, 0);
333     }
334     device_state = DEVICE_W4_INVITE;
335 
336     // generate new public key
337     printf("Generate new public key\n");
338     btstack_crypto_ecc_p256_generate_key(&prov_ecc_p256_request, prov_ec_q, &prov_key_generated, NULL);
339 }
340 
341 static void provisioning_handle_auth_value_output_oob(void * arg){
342     UNUSED(arg);
343     // limit auth value to single digit
344     auth_value[15] = auth_value[15] % 9 + 1;
345 
346     printf("Output OOB: %u\n", auth_value[15]);
347 
348     // emit output oob value
349     provisioning_emit_output_oob_event(1, auth_value[15]);
350     prov_emit_output_oob_active = 1;
351 }
352 
353 static void provisioning_public_key_exchange_complete(void){
354 
355     // reset auth_value
356     memset(auth_value, 0, sizeof(auth_value));
357 
358     // handle authentication method
359     switch (prov_authentication_method){
360         case 0x00:
361             device_state = DEVICE_W4_CONFIRM;
362             break;
363         case 0x01:
364             memcpy(&auth_value[16-prov_static_oob_len], prov_static_oob_data, prov_static_oob_len);
365             device_state = DEVICE_W4_CONFIRM;
366             break;
367         case 0x02:
368             device_state = DEVICE_W4_CONFIRM;
369             printf("Generate random for auth_value\n");
370             // generate single byte of random data to use for authentication
371             btstack_crypto_random_generate(&prov_random_request, &auth_value[15], 1, &provisioning_handle_auth_value_output_oob, NULL);
372             break;
373         case 0x03:
374             // Input OOB
375             printf("Input OOB requested\n");
376             provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_INPUT_OOB_REQUEST);
377             device_state = DEVICE_W4_INPUT_OOK;
378             break;
379         default:
380             break;
381     }
382 }
383 
384 static void provisioning_run(void){
385     printf("provisioning_run: state %x, wait for outgoing complete %u\n", device_state, prov_waiting_for_outgoing_complete);
386     if (prov_waiting_for_outgoing_complete) return;
387     int start_timer = 1;
388     switch (device_state){
389         case DEVICE_SEND_ERROR:
390             start_timer = 0;    // game over
391             prov_waiting_for_outgoing_complete = 1;
392             provisioning_send_provisioning_error();
393             provisioning_done();
394             break;
395         case DEVICE_SEND_CAPABILITIES:
396             device_state = DEVICE_W4_START;
397             prov_waiting_for_outgoing_complete = 1;
398             provisioning_send_capabilites();
399             break;
400         case DEVICE_SEND_INPUT_COMPLETE:
401             device_state = DEVICE_W4_CONFIRM;
402             prov_waiting_for_outgoing_complete = 1;
403             provisioning_send_input_complete();
404             break;
405         case DEVICE_SEND_PUB_KEY:
406             prov_waiting_for_outgoing_complete = 1;
407             provisioning_send_public_key();
408             provisioning_public_key_exchange_complete();
409             break;
410         case DEVICE_SEND_CONFIRM:
411             device_state = DEVICE_W4_RANDOM;
412             prov_waiting_for_outgoing_complete = 1;
413             provisioning_send_confirm();
414             break;
415         case DEVICE_SEND_RANDOM:
416             device_state = DEVICE_W4_DATA;
417             prov_waiting_for_outgoing_complete = 1;
418             provisioning_send_random();
419             break;
420         case DEVICE_SEND_COMPLETE:
421             start_timer = 0;    // last message
422             prov_waiting_for_outgoing_complete = 1;
423             provisioning_send_complete();
424             provisioning_done();
425             break;
426         default:
427             return;
428     }
429     if (start_timer){
430         provisioning_timer_start();
431     }
432 }
433 
434 static void provisioning_handle_provisioning_error(uint8_t error_code){
435     printf("PROVISIONING ERROR\n");
436     provisioning_timer_stop();
437     prov_error_code = error_code;
438     device_state = DEVICE_SEND_ERROR;
439     provisioning_run();
440 }
441 
442 static void provisioning_handle_invite(uint8_t *packet, uint16_t size){
443 
444     if (size != 1) return;
445 
446     // store for confirmation inputs: len 1
447     memcpy(&prov_confirmation_inputs[0], packet, 1);
448 
449     // handle invite message
450     prov_attention_timer_timeout = packet[0];
451     if (prov_attention_timer_timeout){
452         provisioning_emit_attention_timer_event(pb_transport_cid, prov_attention_timer_timeout);
453     }
454 
455     device_state = DEVICE_SEND_CAPABILITIES;
456     provisioning_run();
457 }
458 
459 static void provisioning_handle_start(uint8_t * packet, uint16_t size){
460 
461     if (size != 5) return;
462 
463     // validate Algorithm
464     int ok = 1;
465     if (packet[0] > 0x00){
466         ok = 0;
467     }
468     // validate Publik Key
469     if (packet[1] > 0x01){
470         ok = 0;
471     }
472     // validate Authentication Method
473     switch (packet[2]){
474         case 0:
475         case 1:
476             if (packet[3] != 0 || packet[4] != 0){
477                 ok = 0;
478                 break;
479             }
480             break;
481         case 2:
482             if (packet[3] > 0x04 || packet[4] == 0 || packet[4] > 0x08){
483                 ok = 0;
484                 break;
485             }
486             break;
487         case 3:
488             if (packet[3] > 0x03 || packet[4] == 0 || packet[4] > 0x08){
489                 ok = 0;
490                 break;
491             }
492             break;
493     }
494     if (!ok){
495         printf("PROV_START arguments incorrect\n");
496         provisioning_handle_provisioning_error(0x02);
497         return;
498     }
499 
500     // store for confirmation inputs: len 5
501     memcpy(&prov_confirmation_inputs[12], packet, 5);
502 
503     // public key oob
504     prov_public_key_oob_used = packet[1];
505 
506     // authentication method
507     prov_authentication_method = packet[2];
508 
509     // start emit public OOK if specified
510     if (prov_public_key_oob_available && prov_public_key_oob_used){
511         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_START_EMIT_PUBLIC_KEY_OOB);
512     }
513 
514     printf("PublicKey:  %02x\n", prov_public_key_oob_used);
515     printf("AuthMethod: %02x\n", prov_authentication_method);
516 
517     device_state = DEVICE_W4_PUB_KEY;
518     provisioning_run();
519 }
520 
521 static void provisioning_handle_public_key_dhkey(void * arg){
522     UNUSED(arg);
523 
524     printf("DHKEY: ");
525     printf_hexdump(dhkey, sizeof(dhkey));
526 
527     // skip sending own public key when public key oob is used
528     if (prov_public_key_oob_available && prov_public_key_oob_used){
529         // just copy key for confirmation inputs
530         memcpy(&prov_confirmation_inputs[81], prov_ec_q, 64);
531         provisioning_public_key_exchange_complete();
532     } else {
533         // queue public key pdu
534         printf("DEVICE_SEND_PUB_KEY\n");
535         device_state = DEVICE_SEND_PUB_KEY;
536     }
537     provisioning_run();
538 }
539 
540 static void provisioning_handle_public_key(uint8_t *packet, uint16_t size){
541 
542     // validate public key
543     if (size != sizeof(remote_ec_q) || btstack_crypto_ecc_p256_validate_public_key(packet) != 0){
544         printf("Public Key invalid, abort provisioning\n");
545         provisioning_handle_provisioning_error(0x07);   // Unexpected Error
546         return;
547     }
548 
549     // stop emit public OOK if specified and send to crypto module
550     if (prov_public_key_oob_available && prov_public_key_oob_used){
551         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_STOP_EMIT_PUBLIC_KEY_OOB);
552 
553         printf("Replace generated ECC with Public Key OOB:");
554         memcpy(prov_ec_q, prov_public_key_oob_q, 64);
555         printf_hexdump(prov_ec_q, sizeof(prov_ec_q));
556         btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d);
557     }
558 
559     // store for confirmation inputs: len 64
560     memcpy(&prov_confirmation_inputs[17], packet, 64);
561 
562     // store remote q
563     memcpy(remote_ec_q, packet, sizeof(remote_ec_q));
564 
565     // calculate DHKey
566     btstack_crypto_ecc_p256_calculate_dhkey(&prov_ecc_p256_request, remote_ec_q, dhkey, provisioning_handle_public_key_dhkey, NULL);
567 }
568 
569 static void provisioning_handle_confirmation_device_calculated(void * arg){
570     UNUSED(arg);
571 
572     printf("ConfirmationDevice: ");
573     printf_hexdump(confirmation_device, sizeof(confirmation_device));
574 
575     device_state = DEVICE_SEND_CONFIRM;
576     provisioning_run();
577 }
578 
579 static void provisioning_handle_confirmation_random_device(void * arg){
580     UNUSED(arg);
581 
582     // re-use prov_confirmation_inputs buffer
583     memcpy(&prov_confirmation_inputs[0],  random_device, 16);
584     memcpy(&prov_confirmation_inputs[16], auth_value, 16);
585 
586     // calc confirmation device
587     btstack_crypto_aes128_cmac_message(&prov_cmac_request, confirmation_key, 32, prov_confirmation_inputs, confirmation_device, &provisioning_handle_confirmation_device_calculated, NULL);
588 }
589 
590 static void provisioning_handle_confirmation_k1_calculated(void * arg){
591     UNUSED(arg);
592 
593     printf("ConfirmationKey:   ");
594     printf_hexdump(confirmation_key, sizeof(confirmation_key));
595 
596     printf("AuthValue: ");
597     printf_hexdump(auth_value, 16);
598 
599     // generate random_device
600     btstack_crypto_random_generate(&prov_random_request,random_device, 16, &provisioning_handle_confirmation_random_device, NULL);
601 }
602 
603 static void provisioning_handle_confirmation_s1_calculated(void * arg){
604     UNUSED(arg);
605 
606     // ConfirmationSalt
607     printf("ConfirmationSalt:   ");
608     printf_hexdump(confirmation_salt, sizeof(confirmation_salt));
609 
610     // ConfirmationKey
611     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), confirmation_salt, (const uint8_t*) "prck", 4, confirmation_key, &provisioning_handle_confirmation_k1_calculated, NULL);
612 }
613 
614 static void provisioning_handle_confirmation(uint8_t *packet, uint16_t size){
615     UNUSED(size);
616     UNUSED(packet);
617 
618     //
619     if (prov_emit_output_oob_active){
620         prov_emit_output_oob_active = 0;
621         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_STOP_EMIT_OUTPUT_OOB);
622     }
623 
624     // CalculationInputs
625     printf("ConfirmationInputs: ");
626     printf_hexdump(prov_confirmation_inputs, sizeof(prov_confirmation_inputs));
627 
628     // calculate s1
629     btstack_crypto_aes128_cmac_zero(&prov_cmac_request, sizeof(prov_confirmation_inputs), prov_confirmation_inputs, confirmation_salt, &provisioning_handle_confirmation_s1_calculated, NULL);
630 }
631 
632 // PROV_RANDOM
633 static void provisioning_handle_random_session_nonce_calculated(void * arg){
634     UNUSED(arg);
635 
636     // The nonce shall be the 13 least significant octets == zero most significant octets
637     uint8_t temp[13];
638     memcpy(temp, &session_nonce[3], 13);
639     memcpy(session_nonce, temp, 13);
640 
641     // SessionNonce
642     printf("SessionNonce:   ");
643     printf_hexdump(session_nonce, 13);
644 
645     device_state = DEVICE_SEND_RANDOM;
646     provisioning_run();
647 }
648 
649 static void provisioning_handle_random_session_key_calculated(void * arg){
650     UNUSED(arg);
651 
652     // SessionKey
653     printf("SessionKey:   ");
654     printf_hexdump(session_key, sizeof(session_key));
655 
656     // SessionNonce
657     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prsn", 4, session_nonce, &provisioning_handle_random_session_nonce_calculated, NULL);
658 }
659 
660 static void provisioning_handle_random_s1_calculated(void * arg){
661 
662     UNUSED(arg);
663 
664     // ProvisioningSalt
665     printf("ProvisioningSalt:   ");
666     printf_hexdump(provisioning_salt, sizeof(provisioning_salt));
667 
668     // SessionKey
669     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prsk", 4, session_key, &provisioning_handle_random_session_key_calculated, NULL);
670 }
671 
672 static void provisioning_handle_random(uint8_t *packet, uint16_t size){
673 
674     UNUSED(size);
675     UNUSED(packet);
676 
677     // TODO: validate Confirmation
678 
679     // calc ProvisioningSalt = s1(ConfirmationSalt || RandomProvisioner || RandomDevice)
680     memcpy(&prov_confirmation_inputs[0], confirmation_salt, 16);
681     memcpy(&prov_confirmation_inputs[16], packet, 16);
682     memcpy(&prov_confirmation_inputs[32], random_device, 16);
683     btstack_crypto_aes128_cmac_zero(&prov_cmac_request, 48, prov_confirmation_inputs, provisioning_salt, &provisioning_handle_random_s1_calculated, NULL);
684 }
685 
686 // PROV_DATA
687 static void provisioning_handle_network_dervived(void * arg){
688     UNUSED(arg);
689 
690     provisioning_timer_stop();
691 
692     // notify client
693     provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_COMPLETE);
694 
695     device_state = DEVICE_SEND_COMPLETE;
696     provisioning_run();
697 
698 }
699 
700 static void provisioning_handle_data_device_key(void * arg){
701     UNUSED(arg);
702 
703     // derive full network key
704     mesh_network_key_derive(&prov_cmac_request, network_key, &provisioning_handle_network_dervived, NULL);
705 }
706 
707 static void provisioning_handle_data_ccm(void * arg){
708 
709     UNUSED(arg);
710 
711     // TODO: validate MIC?
712     uint8_t mic[8];
713     btstack_crypto_ccm_get_authentication_value(&prov_ccm_request, mic);
714     printf("MIC: ");
715     printf_hexdump(mic, 8);
716 
717     // allocate network key
718     network_key = btstack_memory_mesh_network_key_get();
719 
720     // sort provisoning data
721     memcpy(network_key->net_key, provisioning_data, 16);
722     network_key->netkey_index = big_endian_read_16(provisioning_data, 16);
723     // assume free index available for very first network key
724     network_key->internal_index = mesh_network_key_get_free_index();
725     flags = provisioning_data[18];
726     iv_index = big_endian_read_32(provisioning_data, 19);
727     unicast_address = big_endian_read_16(provisioning_data, 23);
728 
729     // DeviceKey
730     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prdk", 4, device_key, &provisioning_handle_data_device_key, NULL);
731 }
732 
733 static void provisioning_handle_data(uint8_t *packet, uint16_t size){
734 
735     UNUSED(size);
736 
737     memcpy(enc_provisioning_data, packet, 25);
738 
739     // decode response
740     btstack_crypto_ccm_init(&prov_ccm_request, session_key, session_nonce, 25, 0, 8);
741     btstack_crypto_ccm_decrypt_block(&prov_ccm_request, 25, enc_provisioning_data, provisioning_data, &provisioning_handle_data_ccm, NULL);
742 }
743 
744 static void provisioning_handle_unexpected_pdu(uint8_t *packet, uint16_t size){
745     UNUSED(size);
746     printf("Unexpected PDU #%u in state #%u\n", packet[0], (int) device_state);
747     provisioning_handle_provisioning_error(0x03);
748 }
749 
750 static void provisioning_handle_pdu(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
751     UNUSED(channel);
752 
753     if (size < 1) return;
754 
755     switch (packet_type){
756         case HCI_EVENT_PACKET:
757             if (packet[0] != HCI_EVENT_MESH_META)  break;
758             switch (packet[2]){
759                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
760                     pb_transport_cid = mesh_subevent_pb_transport_link_open_get_pb_transport_cid(packet);
761                     pb_type = mesh_subevent_pb_transport_link_open_get_pb_type(packet);
762                     printf("Link opened, reset state, transport cid 0x%02x, PB type %d\n", pb_transport_cid, pb_type);
763                     provisioning_done();
764                     break;
765                 case MESH_SUBEVENT_PB_TRANSPORT_PDU_SENT:
766                     printf("Outgoing packet acked\n");
767                     prov_waiting_for_outgoing_complete = 0;
768                     break;
769                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
770                     printf("Link close, reset state\n");
771                     pb_transport_cid = MESH_PB_TRANSPORT_INVALID_CID;
772                     provisioning_done();
773                     break;
774             }
775             break;
776         case PROVISIONING_DATA_PACKET:
777             // check state
778             switch (device_state){
779                 case DEVICE_W4_INVITE:
780                     if (packet[0] != MESH_PROV_INVITE) provisioning_handle_unexpected_pdu(packet, size);
781                     printf("MESH_PROV_INVITE: ");
782                     printf_hexdump(&packet[1], size-1);
783                     provisioning_handle_invite(&packet[1], size-1);
784                     break;
785                 case DEVICE_W4_START:
786                     if (packet[0] != MESH_PROV_START) provisioning_handle_unexpected_pdu(packet, size);
787                     printf("MESH_PROV_START:  ");
788                     printf_hexdump(&packet[1], size-1);
789                     provisioning_handle_start(&packet[1], size-1);
790                     break;
791                 case DEVICE_W4_PUB_KEY:
792                     if (packet[0] != MESH_PROV_PUB_KEY) provisioning_handle_unexpected_pdu(packet, size);
793                     printf("MESH_PROV_PUB_KEY: ");
794                     printf_hexdump(&packet[1], size-1);
795                     provisioning_handle_public_key(&packet[1], size-1);
796                     break;
797                 case DEVICE_W4_CONFIRM:
798                     if (packet[0] != MESH_PROV_CONFIRM) provisioning_handle_unexpected_pdu(packet, size);
799                     printf("MESH_PROV_CONFIRM: ");
800                     printf_hexdump(&packet[1], size-1);
801                     provisioning_handle_confirmation(&packet[1], size-1);
802                     break;
803                 case DEVICE_W4_RANDOM:
804                     if (packet[0] != MESH_PROV_RANDOM) provisioning_handle_unexpected_pdu(packet, size);
805                     printf("MESH_PROV_RANDOM:  ");
806                     printf_hexdump(&packet[1], size-1);
807                     provisioning_handle_random(&packet[1], size-1);
808                     break;
809                 case DEVICE_W4_DATA:
810                     if (packet[0] != MESH_PROV_DATA) provisioning_handle_unexpected_pdu(packet, size);
811                     printf("MESH_PROV_DATA:  ");
812                     provisioning_handle_data(&packet[1], size-1);
813                     break;
814                 default:
815                     break;
816             }
817             break;
818         default:
819             break;
820     }
821     provisioning_run();
822 }
823 
824 static void prov_key_generated(void * arg){
825     UNUSED(arg);
826     printf("ECC-P256: ");
827     printf_hexdump(prov_ec_q, sizeof(prov_ec_q));
828     // allow override
829     if (prov_public_key_oob_available){
830         printf("Replace generated ECC with Public Key OOB:");
831         memcpy(prov_ec_q, prov_public_key_oob_q, 64);
832         printf_hexdump(prov_ec_q, sizeof(prov_ec_q));
833         btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d);
834     }
835 }
836 
837 void provisioning_device_init(void){
838 #ifdef ENABLE_MESH_ADV_BEARER
839     // setup PB ADV
840     pb_adv_init();
841     pb_adv_register_packet_handler(&provisioning_handle_pdu);
842 #endif
843 #ifdef ENABLE_MESH_GATT_BEARER
844     // setup PB GATT
845     pb_gatt_init();
846     pb_gatt_register_packet_handler(&provisioning_handle_pdu);
847 #endif
848 
849     pb_transport_cid = MESH_PB_TRANSPORT_INVALID_CID;
850 
851     // init provisioning state
852     provisioning_done();
853 
854     // generate public key
855     btstack_crypto_ecc_p256_generate_key(&prov_ecc_p256_request, prov_ec_q, &prov_key_generated, NULL);
856 }
857 
858 void provisioning_device_register_packet_handler(btstack_packet_handler_t packet_handler){
859     prov_packet_handler = packet_handler;
860 }
861 
862 void provisioning_device_set_public_key_oob(const uint8_t * public_key, const uint8_t * private_key){
863     prov_public_key_oob_q = public_key;
864     prov_public_key_oob_d = private_key;
865     prov_public_key_oob_available = 1;
866     btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d);
867 }
868 
869 void provisioning_device_set_static_oob(uint16_t static_oob_len, const uint8_t * static_oob_data){
870     prov_static_oob_available = 1;
871     prov_static_oob_data = static_oob_data;
872     prov_static_oob_len  = btstack_min(static_oob_len, 16);
873 }
874 
875 void provisioning_device_set_output_oob_actions(uint16_t supported_output_oob_action_types, uint8_t max_oob_output_size){
876     prov_output_oob_actions = supported_output_oob_action_types;
877     prov_output_oob_size    = max_oob_output_size;
878 }
879 
880 void provisioning_device_set_input_oob_actions(uint16_t supported_input_oob_action_types, uint8_t max_oob_input_size){
881     prov_input_oob_actions = supported_input_oob_action_types;
882     prov_input_oob_size    = max_oob_input_size;
883 }
884 
885 void provisioning_device_input_oob_complete_numeric(uint16_t pb_adv_cid, uint32_t input_oob){
886     UNUSED(pb_adv_cid);
887     if (device_state != DEVICE_W4_INPUT_OOK) return;
888 
889     // store input_oob as auth value
890     big_endian_store_32(auth_value, 12, input_oob);
891     device_state = DEVICE_SEND_INPUT_COMPLETE;
892     provisioning_run();
893 }
894 
895 void provisioning_device_input_oob_complete_alphanumeric(uint16_t pb_adv_cid, const uint8_t * input_oob_data, uint16_t input_oob_len){
896     UNUSED(pb_adv_cid);
897     if (device_state != DEVICE_W4_INPUT_OOK) return;
898 
899     // store input_oob and fillup with zeros
900     input_oob_len = btstack_min(input_oob_len, 16);
901     memset(auth_value, 0, 16);
902     memcpy(auth_value, input_oob_data, input_oob_len);
903     device_state = DEVICE_SEND_INPUT_COMPLETE;
904     provisioning_run();
905 }
906 
907 void provisioning_device_data_get(mesh_provisioning_data_t * the_provisioning_data){
908     the_provisioning_data->unicast_address = unicast_address;
909     the_provisioning_data->iv_index = iv_index;
910     the_provisioning_data->flags = flags;
911     memcpy(the_provisioning_data->device_key, device_key, 16);
912     the_provisioning_data->network_key = network_key;
913 }
914