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