xref: /btstack/src/mesh/provisioning_provisioner.c (revision a5a7b6da93a734de273f68a4770fffd1d7ae3888)
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_provisioner.c"
39 
40 #include "mesh/provisioning_provisioner.h"
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "btstack.h"
47 
48 #include "mesh/mesh_crypto.h"
49 #include "mesh/pb_adv.h"
50 #include "mesh/provisioning.h"
51 
52 static void provisioning_public_key_ready(void);
53 
54 // global
55 static uint8_t         prov_ec_q[64];
56 static const uint8_t * prov_public_key_oob_q;
57 static const uint8_t * prov_public_key_oob_d;
58 static uint8_t  prov_public_key_oob_available;
59 
60 static btstack_packet_handler_t prov_packet_handler;
61 
62 // NetKey
63 static uint8_t  net_key[16];
64 // NetKeyIndex
65 static uint16_t net_key_index;
66 // Flags
67 static uint8_t  flags;
68 // IV Index
69 static uint32_t iv_index;
70 
71 // either used once or per session
72 static btstack_crypto_aes128_cmac_t prov_cmac_request;
73 static btstack_crypto_random_t      prov_random_request;
74 static btstack_crypto_ecc_p256_t    prov_ecc_p256_request;
75 static btstack_crypto_ccm_t         prov_ccm_request;
76 
77 // data per provisioning session
78 static btstack_timer_source_t       prov_protocol_timer;
79 
80 static uint16_t pb_adv_cid;
81 static uint8_t  prov_attention_timer;
82 static uint8_t  prov_buffer_out[100];   // TODO: how large are prov messages?
83 static uint8_t  prov_waiting_for_outgoing_complete;
84 static uint8_t  prov_error_code;
85 static uint8_t  prov_start_algorithm;
86 static uint8_t  prov_start_public_key_used;
87 static uint8_t  prov_start_authentication_method;
88 static uint8_t  prov_start_authentication_action;
89 static uint8_t  prov_start_authentication_size;
90 static uint8_t  prov_authentication_string;
91 static uint8_t  prov_confirmation_inputs[1 + 11 + 5 + 64 + 64];
92 static uint8_t  confirmation_provisioner[16];
93 static uint8_t  random_provisioner[16];
94 static uint8_t  auth_value[16];
95 static uint8_t  remote_ec_q[64];
96 static uint8_t  dhkey[32];
97 static uint8_t  confirmation_salt[16];
98 static uint8_t  confirmation_key[16];
99 // ConfirmationInputs = ProvisioningInvitePDUValue || ProvisioningCapabilitiesPDUValue || ProvisioningStartPDUValue || PublicKeyProvisioner || PublicKeyDevice
100 static uint8_t  prov_confirmation_inputs[1 + 11 + 5 + 64 + 64];
101 static uint8_t provisioning_salt[16];
102 static uint8_t session_key[16];
103 static uint8_t session_nonce[16];
104 static uint16_t unicast_address;
105 static uint8_t provisioning_data[25];
106 static uint8_t enc_provisioning_data[25];
107 static uint8_t provisioning_data_mic[8];
108 static uint8_t  prov_emit_output_oob_active;
109 
110 static const uint8_t * prov_static_oob_data;
111 static uint16_t  prov_static_oob_len;
112 
113 #if 0
114 static uint8_t  prov_public_key_oob_used;
115 static uint8_t  prov_emit_public_key_oob_active;
116 
117 // capabilites
118 
119 static uint16_t  prov_output_oob_actions;
120 static uint16_t  prov_input_oob_actions;
121 static uint8_t   prov_output_oob_size;
122 static uint8_t   prov_input_oob_size;
123 
124 // derived
125 static uint8_t network_id[8];
126 static uint8_t beacon_key[16];
127 
128 static void provisioning_attention_timer_timeout(btstack_timer_source_t * ts){
129     UNUSED(ts);
130     if (prov_attention_timer_timeout == 0) return;
131     prov_attention_timer_timeout--;
132     provisioning_attention_timer_set();
133 }
134 
135 static void provisioning_attention_timer_set(void){
136     provisioning_emit_attention_timer_event(1, prov_attention_timer_timeout);
137     if (prov_attention_timer_timeout){
138         btstack_run_loop_set_timer_handler(&prov_attention_timer, &provisioning_attention_timer_timeout);
139         btstack_run_loop_set_timer(&prov_attention_timer, 1000);
140         btstack_run_loop_add_timer(&prov_attention_timer);
141     }
142 }
143 #endif
144 
145 static void provisioning_emit_output_oob_event(uint16_t pb_adv_cid, uint32_t number){
146     if (!prov_packet_handler) return;
147     uint8_t event[9] = { HCI_EVENT_MESH_META, 7, MESH_SUBEVENT_PB_PROV_START_EMIT_OUTPUT_OOB};
148     little_endian_store_16(event, 3, pb_adv_cid);
149     little_endian_store_16(event, 5, number);
150     prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
151 }
152 
153 static void provisioning_emit_event(uint8_t mesh_subevent, uint16_t pb_adv_cid){
154     if (!prov_packet_handler) return;
155     uint8_t event[5] = { HCI_EVENT_MESH_META, 3, mesh_subevent};
156     little_endian_store_16(event, 3, pb_adv_cid);
157     prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
158 }
159 
160 static void provisiong_timer_handler(btstack_timer_source_t * ts){
161     UNUSED(ts);
162     printf("Provisioning Protocol Timeout -> Close Link!\n");
163     // TODO: use actual pb_adv_cid
164     pb_adv_close_link(1, 1);
165 }
166 
167 // The provisioning protocol shall have a minimum timeout of 60 seconds that is reset
168 // each time a provisioning protocol PDU is sent or received
169 static void provisioning_timer_start(void){
170     btstack_run_loop_remove_timer(&prov_protocol_timer);
171     btstack_run_loop_set_timer_handler(&prov_protocol_timer, &provisiong_timer_handler);
172     btstack_run_loop_set_timer(&prov_protocol_timer, PROVISIONING_PROTOCOL_TIMEOUT_MS);
173     btstack_run_loop_add_timer(&prov_protocol_timer);
174 }
175 
176 static void provisioning_timer_stop(void){
177     btstack_run_loop_remove_timer(&prov_protocol_timer);
178 }
179 
180 // Outgoing Provisioning PDUs
181 
182 static void provisioning_send_invite(uint16_t pb_adv_cid){
183     prov_buffer_out[0] = MESH_PROV_INVITE;
184     prov_buffer_out[1] = prov_attention_timer;
185     pb_adv_send_pdu(pb_adv_cid, prov_buffer_out, 2);
186     // collect confirmation_inputs
187     memcpy(&prov_confirmation_inputs[0], &prov_buffer_out[1], 1);
188 }
189 
190 static void provisioning_send_start(uint16_t pb_adv_cid){
191     prov_buffer_out[0] = MESH_PROV_START;
192     prov_buffer_out[1] = prov_start_algorithm;
193     prov_buffer_out[2] = prov_start_public_key_used;
194     prov_buffer_out[3] = prov_start_authentication_method;
195     prov_buffer_out[4] = prov_start_authentication_action;
196     prov_buffer_out[5] = prov_start_authentication_size;
197     pb_adv_send_pdu(pb_adv_cid, prov_buffer_out, 6);
198     // store for confirmation inputs: len 5
199     memcpy(&prov_confirmation_inputs[12], &prov_buffer_out[1], 5);
200 }
201 
202 static void provisioning_send_provisioning_error(void){
203     prov_buffer_out[0] = MESH_PROV_FAILED;
204     prov_buffer_out[1] = prov_error_code;
205     pb_adv_send_pdu(pb_adv_cid, prov_buffer_out, 2);
206 }
207 
208 static void provisioning_send_public_key(void){
209     prov_buffer_out[0] = MESH_PROV_PUB_KEY;
210     memcpy(&prov_buffer_out[1], prov_ec_q, 64);
211     pb_adv_send_pdu(pb_adv_cid, prov_buffer_out, 65);
212     // store for confirmation inputs: len 64
213     memcpy(&prov_confirmation_inputs[17], &prov_buffer_out[1], 64);
214 }
215 
216 static void provisioning_send_confirm(void){
217     prov_buffer_out[0] = MESH_PROV_CONFIRM;
218     memcpy(&prov_buffer_out[1], confirmation_provisioner, 16);
219     pb_adv_send_pdu(pb_adv_cid, prov_buffer_out, 17);
220 }
221 
222 static void provisioning_send_random(void){
223     prov_buffer_out[0] = MESH_PROV_RANDOM;
224     memcpy(&prov_buffer_out[1], random_provisioner, 16);
225     pb_adv_send_pdu(pb_adv_cid, prov_buffer_out, 17);
226 }
227 
228 static void provisioning_send_data(void){
229     prov_buffer_out[0] = MESH_PROV_DATA;
230     memcpy(&prov_buffer_out[1], enc_provisioning_data, 25);
231     memcpy(&prov_buffer_out[26], provisioning_data_mic, 8);
232     pb_adv_send_pdu(pb_adv_cid, prov_buffer_out, 34);
233 }
234 
235 typedef enum {
236     PROVISIONER_IDLE,
237     PROVISIONER_SEND_INVITE,
238     PROVISIONER_W4_CAPABILITIES,
239     PROVISIONER_W4_AUTH_CONFIGURATION,
240     PROVISIONER_SEND_START,
241     PROVISIONED_W2_EMIT_READ_PUB_KEY_OOB,
242     PROVISIONER_SEND_PUB_KEY,
243     PROVISIONER_W4_PUB_KEY,
244     PROVISIONER_W4_PUB_KEY_OOB,
245     PROVISIONER_W4_INPUT_OOK,
246     PROVISIONER_W4_INPUT_COMPLETE,
247     PROVISIONER_SEND_CONFIRM,
248     PROVISIONER_W4_CONFIRM,
249     PROVISIONER_SEND_RANDOM,
250     PROVISIONER_W4_RANDOM,
251     PROVISIONER_SEND_DATA,
252     PROVISIONER_W4_COMPLETE,
253     PROVISIONER_SEND_ERROR,
254 } provisioner_state_t;
255 
256 static provisioner_state_t provisioner_state;
257 
258 static void provisioning_run(void){
259     if (prov_waiting_for_outgoing_complete) return;
260     int start_timer = 1;
261     switch (provisioner_state){
262         case PROVISIONER_SEND_ERROR:
263             start_timer = 0;    // game over
264             provisioning_send_provisioning_error();
265             break;
266         case PROVISIONER_SEND_INVITE:
267             provisioning_send_invite(pb_adv_cid);
268             provisioner_state = PROVISIONER_W4_CAPABILITIES;
269             break;
270         case PROVISIONER_SEND_START:
271             provisioning_send_start(pb_adv_cid);
272             if (prov_start_public_key_used){
273                 provisioner_state = PROVISIONED_W2_EMIT_READ_PUB_KEY_OOB;
274             } else {
275                 provisioner_state = PROVISIONER_SEND_PUB_KEY;
276             }
277             break;
278         case PROVISIONED_W2_EMIT_READ_PUB_KEY_OOB:
279             printf("Public OOB: please read OOB from remote device\n");
280             provisioner_state = PROVISIONER_W4_PUB_KEY_OOB;
281             provisioning_emit_event(MESH_SUBEVENT_PB_PROV_START_RECEIVE_PUBLIC_KEY_OOB, 1);
282             break;
283         case PROVISIONER_SEND_PUB_KEY:
284             provisioning_send_public_key();
285             if (prov_start_public_key_used){
286                 provisioning_public_key_ready();
287             } else {
288                 provisioner_state = PROVISIONER_W4_PUB_KEY;
289             }
290             break;
291         case PROVISIONER_SEND_CONFIRM:
292             provisioning_send_confirm();
293             provisioner_state = PROVISIONER_W4_CONFIRM;
294             break;
295         case PROVISIONER_SEND_RANDOM:
296             provisioning_send_random();
297             provisioner_state = PROVISIONER_W4_RANDOM;
298             break;
299         case PROVISIONER_SEND_DATA:
300             provisioning_send_data();
301             provisioner_state = PROVISIONER_W4_COMPLETE;
302             break;
303         default:
304             return;
305     }
306     if (start_timer){
307         provisioning_timer_start();
308     }
309     prov_waiting_for_outgoing_complete = 1;
310 }
311 
312 // End of outgoing PDUs
313 
314 static void provisioning_done(void){
315     // if (prov_emit_public_key_oob_active){
316     //     prov_emit_public_key_oob_active = 0;
317     //     provisioning_emit_event(MESH_PB_PROV_STOP_EMIT_PUBLIC_KEY_OOB, 1);
318     // }
319     if (prov_emit_output_oob_active){
320         prov_emit_output_oob_active = 0;
321         provisioning_emit_event(MESH_SUBEVENT_PB_PROV_STOP_EMIT_OUTPUT_OOB, 1);
322     }
323     provisioner_state = PROVISIONER_IDLE;
324 }
325 
326 
327 static void provisioning_handle_provisioning_error(uint8_t error_code){
328     provisioning_timer_stop();
329     prov_error_code = error_code;
330     provisioner_state = PROVISIONER_SEND_ERROR;
331     provisioning_run();
332 }
333 
334 static void provisioning_handle_link_opened(uint16_t pb_adv_cid){
335     provisioner_state = PROVISIONER_SEND_INVITE;
336 }
337 
338 static void provisioning_handle_capabilities(uint16_t pb_adv_cid, const uint8_t * packet_data, uint16_t packet_len){
339 
340     if (packet_len != 11) return;
341 
342     // collect confirmation_inputs
343     memcpy(&prov_confirmation_inputs[1], packet_data, packet_len);
344 
345     provisioner_state = PROVISIONER_W4_AUTH_CONFIGURATION;
346 
347     // notify client and wait for auth method selection
348     uint8_t event[16] = { HCI_EVENT_MESH_META, 3, MESH_SUBEVENT_PB_PROV_CAPABILITIES};
349     little_endian_store_16(event, 3, pb_adv_cid);
350     event[5] = packet_data[0];
351     little_endian_store_16(event, 6, big_endian_read_16(packet_data, 1));
352     event[8] = packet_data[3];
353     event[9] = packet_data[4];
354     event[10] = packet_data[5];
355     little_endian_store_16(event, 11, big_endian_read_16(packet_data, 6));
356     event[13] = packet_data[8];
357     little_endian_store_16(event, 14, big_endian_read_16(packet_data, 9));
358     prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
359 }
360 
361 static void provisioning_handle_confirmation_provisioner_calculated(void * arg){
362     UNUSED(arg);
363 
364     printf("ConfirmationProvisioner: ");
365     printf_hexdump(confirmation_provisioner, sizeof(confirmation_provisioner));
366 
367     provisioner_state = PROVISIONER_SEND_CONFIRM;
368     provisioning_run();
369 }
370 
371 static void provisioning_handle_random_provisioner(void * arg){
372     printf("RandomProvisioner:   ");
373     printf_hexdump(random_provisioner, sizeof(random_provisioner));
374 
375     // re-use prov_confirmation_inputs buffer
376     memcpy(&prov_confirmation_inputs[0],  random_provisioner, 16);
377     memcpy(&prov_confirmation_inputs[16], auth_value, 16);
378 
379     // calc confirmation device
380     btstack_crypto_aes128_cmac_message(&prov_cmac_request, confirmation_key, 32, prov_confirmation_inputs, confirmation_provisioner, &provisioning_handle_confirmation_provisioner_calculated, NULL);
381 }
382 
383 static void provisioning_handle_confirmation_k1_calculated(void * arg){
384     printf("ConfirmationKey:   ");
385     printf_hexdump(confirmation_key, sizeof(confirmation_key));
386 
387     // generate random_device
388     btstack_crypto_random_generate(&prov_random_request,random_provisioner, 16, &provisioning_handle_random_provisioner, NULL);
389 }
390 
391 static void provisioning_handle_confirmation_salt(void * arg){
392     UNUSED(arg);
393 
394     // dump
395     printf("ConfirmationSalt:   ");
396     printf_hexdump(confirmation_salt, sizeof(confirmation_salt));
397 
398     // ConfirmationKey
399     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), confirmation_salt, (const uint8_t*) "prck", 4, confirmation_key, &provisioning_handle_confirmation_k1_calculated, NULL);
400 }
401 
402 static void provisioning_handle_auth_value_ready(void){
403     // CalculationInputs
404     printf("ConfirmationInputs: ");
405     printf_hexdump(prov_confirmation_inputs, sizeof(prov_confirmation_inputs));
406 
407     // calculate s1
408     btstack_crypto_aes128_cmac_zero(&prov_cmac_request, sizeof(prov_confirmation_inputs), prov_confirmation_inputs, confirmation_salt, &provisioning_handle_confirmation_salt, NULL);
409 }
410 
411 static void provisioning_handle_auth_value_input_oob(void * arg){
412 
413     // limit auth value to single digit
414     auth_value[15] = auth_value[15] % 9 + 1;
415     printf("Input OOB: %u\n", auth_value[15]);
416 
417     if (prov_authentication_string){
418         // strings start at 0 while numbers are stored as 16-byte big endian
419         auth_value[0] = auth_value[15] + '0';
420         auth_value[15] = 0;
421     }
422 
423     printf("AuthValue: ");
424     printf_hexdump(auth_value, sizeof(auth_value));
425 
426     // emit output oob value
427     provisioning_emit_output_oob_event(1, auth_value[15]);
428     prov_emit_output_oob_active = 1;
429 
430     provisioner_state = PROVISIONER_W4_INPUT_COMPLETE;
431 }
432 
433 static void provisioning_handle_input_complete(uint16_t pb_adv_cid){
434     provisioning_handle_auth_value_ready();
435 }
436 
437 static void provisioning_public_key_exchange_complete(void){
438     // reset auth_value
439     memset(auth_value, 0, sizeof(auth_value));
440 
441     // handle authentication method
442     switch (prov_start_authentication_method){
443         case 0x00:
444             provisioning_handle_auth_value_ready();
445             break;
446         case 0x01:
447             memcpy(&auth_value[16-prov_static_oob_len], prov_static_oob_data, prov_static_oob_len);
448             provisioning_handle_auth_value_ready();
449             break;
450         case 0x02:
451             // Output OOB
452             prov_authentication_string = prov_start_authentication_action == 0x04;
453             printf("Output OOB requested (and we're in Provisioniner role), string %u\n", prov_authentication_string);
454             provisioner_state = PROVISIONER_W4_INPUT_OOK;
455             provisioning_emit_event(MESH_SUBEVENT_PB_PROV_OUTPUT_OOB_REQUEST, 1);
456             break;
457         case 0x03:
458             // Input OOB
459             prov_authentication_string = prov_start_authentication_action == 0x03;
460             printf("Input OOB requested, string %u\n", prov_authentication_string);
461             printf("Generate random for auth_value\n");
462             // generate single byte of random data to use for authentication
463             btstack_crypto_random_generate(&prov_random_request, &auth_value[15], 1, &provisioning_handle_auth_value_input_oob, NULL);
464             provisioning_emit_event(MESH_SUBEVENT_PB_PROV_START_EMIT_INPUT_OOB, 1);
465             break;
466         default:
467             break;
468     }
469 }
470 
471 static void provisioning_handle_public_key_dhkey(void * arg){
472     UNUSED(arg);
473 
474     printf("DHKEY: ");
475     printf_hexdump(dhkey, sizeof(dhkey));
476 
477 #if 0
478     // skip sending own public key when public key oob is used
479     if (prov_public_key_oob_available && prov_public_key_oob_used){
480         // just copy key for confirmation inputs
481         memcpy(&prov_confirmation_inputs[81], prov_ec_q, 64);
482     } else {
483         // queue public key pdu
484         provisioning_queue_pdu(MESH_PROV_PUB_KEY);
485     }
486 #endif
487 
488     provisioning_public_key_exchange_complete();
489 }
490 
491 static void provisioning_public_key_ready(void){
492     // calculate DHKey
493     btstack_crypto_ecc_p256_calculate_dhkey(&prov_ecc_p256_request, remote_ec_q, dhkey, provisioning_handle_public_key_dhkey, NULL);
494 }
495 
496 static void provisioning_handle_public_key(uint16_t pb_adv_cid, const uint8_t *packet_data, uint16_t packet_len){
497 
498     // validate public key
499     if (packet_len != sizeof(remote_ec_q) || btstack_crypto_ecc_p256_validate_public_key(packet_data) != 0){
500         printf("Public Key invalid, abort provisioning\n");
501 
502         // disconnect provisioning link
503         pb_adv_close_link(pb_adv_cid, 0x02);    // reason: fail
504         provisioning_timer_stop();
505         return;
506     }
507 
508 #if 0
509     // stop emit public OOK if specified and send to crypto module
510     if (prov_public_key_oob_available && prov_public_key_oob_used){
511         provisioning_emit_event(MESH_PB_PROV_STOP_EMIT_PUBLIC_KEY_OOB, 1);
512 
513         printf("Replace generated ECC with Public Key OOB:");
514         memcpy(prov_ec_q, prov_public_key_oob_q, 64);
515         printf_hexdump(prov_ec_q, sizeof(prov_ec_q));
516         btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d);
517     }
518 #endif
519 
520     // store for confirmation inputs: len 64
521     memcpy(&prov_confirmation_inputs[81], packet_data, 64);
522 
523     // store remote q
524     memcpy(remote_ec_q, packet_data, sizeof(remote_ec_q));
525 
526     provisioning_public_key_ready();
527 }
528 
529 static void provisioning_handle_confirmation(uint16_t pb_adv_cid, const uint8_t *packet_data, uint16_t packet_len){
530 
531     UNUSED(packet_data);
532     UNUSED(packet_len);
533 
534     //
535     if (prov_emit_output_oob_active){
536         prov_emit_output_oob_active = 0;
537         provisioning_emit_event(MESH_SUBEVENT_PB_PROV_STOP_EMIT_OUTPUT_OOB, 1);
538     }
539 
540 #if 0
541     // CalculationInputs
542     printf("ConfirmationInputs: ");
543     printf_hexdump(prov_confirmation_inputs, sizeof(prov_confirmation_inputs));
544 
545     // calculate s1
546     btstack_crypto_aes128_cmac_zero(&prov_cmac_request, sizeof(prov_confirmation_inputs), prov_confirmation_inputs, confirmation_salt, &provisioning_handle_confirmation_s1_calculated, NULL);
547 #endif
548     provisioner_state = PROVISIONER_SEND_RANDOM;
549 }
550 
551 static void provisioning_handle_data_encrypted(void * arg){
552     UNUSED(arg);
553 
554     // enc_provisioning_data
555     printf("EncProvisioningData:   ");
556     printf_hexdump(enc_provisioning_data, sizeof(enc_provisioning_data));
557 
558     btstack_crypto_ccm_get_authentication_value(&prov_ccm_request, provisioning_data_mic);
559     printf("MIC:   ");
560     printf_hexdump(provisioning_data_mic, sizeof(provisioning_data_mic));
561 
562     // send
563     provisioner_state = PROVISIONER_SEND_DATA;
564     provisioning_run();
565 }
566 
567 static void provisioning_handle_session_nonce_calculated(void * arg){
568     UNUSED(arg);
569 
570     // The nonce shall be the 13 least significant octets == zero most significant octets
571     uint8_t temp[13];
572     memcpy(temp, &session_nonce[3], 13);
573     memcpy(session_nonce, temp, 13);
574 
575     // SessionNonce
576     printf("SessionNonce:   ");
577     printf_hexdump(session_nonce, 13);
578 
579     // setup provisioning data
580     memcpy(&provisioning_data[0], net_key, 16);
581     big_endian_store_16(provisioning_data, 16, net_key_index);
582     provisioning_data[18] = flags;
583     big_endian_store_32(provisioning_data, 19, iv_index);
584     big_endian_store_16(provisioning_data, 23, unicast_address);
585 
586     btstack_crypto_ccm_init(&prov_ccm_request, session_key, session_nonce, 25, 0, 8);
587     btstack_crypto_ccm_encrypt_block(&prov_ccm_request, 25, provisioning_data, enc_provisioning_data, &provisioning_handle_data_encrypted, NULL);
588 }
589 
590 static void provisioning_handle_session_key_calculated(void * arg){
591     UNUSED(arg);
592 
593     // SessionKey
594     printf("SessionKey:   ");
595     printf_hexdump(session_key, sizeof(session_key));
596 
597     // SessionNonce
598     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prsn", 4, session_nonce, &provisioning_handle_session_nonce_calculated, NULL);
599 }
600 
601 
602 static void provisioning_handle_provisioning_salt_calculated(void * arg){
603     UNUSED(arg);
604 
605     // ProvisioningSalt
606     printf("ProvisioningSalt:   ");
607     printf_hexdump(provisioning_salt, sizeof(provisioning_salt));
608 
609     // SessionKey
610     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prsk", 4, session_key, &provisioning_handle_session_key_calculated, NULL);
611 }
612 
613 static void provisioning_handle_random(uint16_t pb_adv_cid, const uint8_t *packet_data, uint16_t packet_len){
614 
615     UNUSED(packet_data);
616     UNUSED(packet_len);
617 
618     // TODO: validate Confirmation
619 
620     // calc ProvisioningSalt = s1(ConfirmationSalt || RandomProvisioner || RandomDevice)
621     memcpy(&prov_confirmation_inputs[0], confirmation_salt, 16);
622     memcpy(&prov_confirmation_inputs[16], random_provisioner, 16);
623     memcpy(&prov_confirmation_inputs[32], packet_data, 16);
624     btstack_crypto_aes128_cmac_zero(&prov_cmac_request, 48, prov_confirmation_inputs, provisioning_salt, &provisioning_handle_provisioning_salt_calculated, NULL);
625 }
626 
627 static void provisioning_handle_complete(uint16_t pb_adv_cid){
628 }
629 
630 static void provisioning_handle_pdu(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
631 
632     if (size < 1) return;
633 
634     switch (packet_type){
635         case HCI_EVENT_PACKET:
636             if (packet[0] != HCI_EVENT_MESH_META)  break;
637             switch (packet[2]){
638                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
639                     printf("Link opened, sending Invite\n");
640                     provisioning_handle_link_opened(pb_adv_cid);
641                     break;
642                 case MESH_SUBEVENT_PB_TRANSPORT_PDU_SENT:
643                     printf("Outgoing packet acked\n");
644                     prov_waiting_for_outgoing_complete = 0;
645                     break;
646                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
647                     printf("Link close, reset state\n");
648                     provisioning_done();
649                     break;
650             }
651             break;
652         case PROVISIONING_DATA_PACKET:
653             // check state
654             switch (provisioner_state){
655                 case PROVISIONER_W4_CAPABILITIES:
656                     if (packet[0] != MESH_PROV_CAPABILITIES) provisioning_handle_provisioning_error(0x03);
657                     printf("MESH_PROV_CAPABILITIES: ");
658                     printf_hexdump(&packet[1], size-1);
659                     provisioning_handle_capabilities(pb_adv_cid, &packet[1], size-1);
660                     break;
661                 case PROVISIONER_W4_PUB_KEY:
662                     if (packet[0] != MESH_PROV_PUB_KEY) provisioning_handle_provisioning_error(0x03);
663                     printf("MESH_PROV_PUB_KEY: ");
664                     printf_hexdump(&packet[1], size-1);
665                     provisioning_handle_public_key(pb_adv_cid, &packet[1], size-1);
666                     break;
667                 case PROVISIONER_W4_INPUT_COMPLETE:
668                     if (packet[0] != MESH_PROV_INPUT_COMPLETE) provisioning_handle_provisioning_error(0x03);
669                     printf("MESH_PROV_INPUT_COMPLETE: ");
670                     printf_hexdump(&packet[1], size-1);
671                     provisioning_handle_input_complete(pb_adv_cid);
672                     break;
673                 case PROVISIONER_W4_CONFIRM:
674                     if (packet[0] != MESH_PROV_CONFIRM) provisioning_handle_provisioning_error(0x03);
675                     printf("MESH_PROV_CONFIRM: ");
676                     printf_hexdump(&packet[1], size-1);
677                     provisioning_handle_confirmation(pb_adv_cid, &packet[1], size-1);
678                     break;
679                 case PROVISIONER_W4_RANDOM:
680                     if (packet[0] != MESH_PROV_RANDOM) provisioning_handle_provisioning_error(0x03);
681                     printf("MESH_PROV_RANDOM:  ");
682                     printf_hexdump(&packet[1], size-1);
683                     provisioning_handle_random(pb_adv_cid, &packet[1], size-1);
684                     break;
685                 case PROVISIONER_W4_COMPLETE:
686                     if (packet[0] != MESH_PROV_COMPLETE) provisioning_handle_provisioning_error(0x03);
687                     printf("MESH_PROV_COMPLETE:  ");
688                     provisioning_handle_complete(pb_adv_cid);
689                     break;
690                 default:
691                     printf("TODO: handle provisioning state %x\n", provisioner_state);
692                     break;
693             }
694             break;
695         default:
696             break;
697     }
698     provisioning_run();
699 }
700 
701 static void prov_key_generated(void * arg){
702     UNUSED(arg);
703     printf("ECC-P256: ");
704     printf_hexdump(prov_ec_q, sizeof(prov_ec_q));
705     // allow override
706     if (prov_public_key_oob_available){
707         printf("Replace generated ECC with Public Key OOB:");
708         memcpy(prov_ec_q, prov_public_key_oob_q, 64);
709         printf_hexdump(prov_ec_q, sizeof(prov_ec_q));
710         btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d);
711     }
712 }
713 
714 void provisioning_provisioner_init(void){
715     pb_adv_cid = MESH_PB_TRANSPORT_INVALID_CID;
716     pb_adv_init();
717     pb_adv_register_packet_handler(&provisioning_handle_pdu);
718 }
719 
720 void provisioning_provisioner_register_packet_handler(btstack_packet_handler_t packet_handler){
721     prov_packet_handler = packet_handler;
722 }
723 
724 uint16_t provisioning_provisioner_start_provisioning(const uint8_t * device_uuid){
725     // generate new public key
726     btstack_crypto_ecc_p256_generate_key(&prov_ecc_p256_request, prov_ec_q, &prov_key_generated, NULL);
727 
728     if (pb_adv_cid == MESH_PB_TRANSPORT_INVALID_CID) {
729         pb_adv_cid = pb_adv_create_link(device_uuid);
730     }
731     return pb_adv_cid;
732 }
733 
734 void provisioning_provisioner_set_static_oob(uint16_t pb_adv_cid, uint16_t static_oob_len, const uint8_t * static_oob_data){
735     UNUSED(pb_adv_cid);
736     prov_static_oob_data = static_oob_data;
737     prov_static_oob_len  = btstack_min(static_oob_len, 16);
738 }
739 
740 uint8_t provisioning_provisioner_select_authentication_method(uint16_t pb_adv_cid, uint8_t algorithm, uint8_t public_key_used, uint8_t authentication_method, uint8_t authentication_action, uint8_t authentication_size){
741 
742     if (provisioner_state != PROVISIONER_W4_AUTH_CONFIGURATION) return ERROR_CODE_COMMAND_DISALLOWED;
743 
744     prov_start_algorithm = algorithm;
745     prov_start_public_key_used = public_key_used;
746     prov_start_authentication_method = authentication_method;
747     prov_start_authentication_action = authentication_action;
748     prov_start_authentication_size   = authentication_size;
749     provisioner_state = PROVISIONER_SEND_START;
750 
751     return ERROR_CODE_SUCCESS;
752 }
753 
754 uint8_t provisioning_provisioner_public_key_oob_received(uint16_t pb_adv_cid, const uint8_t * public_key){
755 
756     if (provisioner_state != PROVISIONER_W4_PUB_KEY_OOB) return ERROR_CODE_COMMAND_DISALLOWED;
757 
758     // store for confirmation inputs: len 64
759     memcpy(&prov_confirmation_inputs[81], public_key, 64);
760 
761     // store remote q
762     memcpy(remote_ec_q, public_key, sizeof(remote_ec_q));
763 
764     // continue procedure
765     provisioner_state = PROVISIONER_SEND_PUB_KEY;
766     provisioning_run();
767 
768     return ERROR_CODE_SUCCESS;
769 }
770 
771 void provisioning_provisioner_input_oob_complete_numeric(uint16_t pb_adv_cid, uint32_t input_oob){
772     UNUSED(pb_adv_cid);
773     if (provisioner_state != PROVISIONER_W4_INPUT_OOK) return;
774 
775     // store input_oob as auth value
776     big_endian_store_32(auth_value, 12, input_oob);
777     provisioning_handle_auth_value_ready();
778 }
779 
780 void provisioning_provisioner_input_oob_complete_alphanumeric(uint16_t pb_adv_cid, const uint8_t * input_oob_data, uint16_t input_oob_len){
781     UNUSED(pb_adv_cid);
782     if (provisioner_state != PROVISIONER_W4_INPUT_OOK) return;
783 
784     // store input_oob and fillup with zeros
785     input_oob_len = btstack_min(input_oob_len, 16);
786     memset(auth_value, 0, 16);
787     memcpy(auth_value, input_oob_data, input_oob_len);
788     provisioning_handle_auth_value_ready();
789 }
790 
791