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 the_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, the_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 the_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, the_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 the_pb_adv_cid){ 183 prov_buffer_out[0] = MESH_PROV_INVITE; 184 prov_buffer_out[1] = prov_attention_timer; 185 pb_adv_send_pdu(the_pb_adv_cid, prov_buffer_out, 2); 186 // collect confirmation_inputs 187 (void)memcpy(&prov_confirmation_inputs[0], &prov_buffer_out[1], 1); 188 } 189 190 static void provisioning_send_start(uint16_t the_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(the_pb_adv_cid, prov_buffer_out, 6); 198 // store for confirmation inputs: len 5 199 (void)memcpy(&prov_confirmation_inputs[12], &prov_buffer_out[1], 5); 200 } 201 202 static void provisioning_send_provisioning_error(uint16_t the_pb_adv_cid){ 203 prov_buffer_out[0] = MESH_PROV_FAILED; 204 prov_buffer_out[1] = prov_error_code; 205 pb_adv_send_pdu(the_pb_adv_cid, prov_buffer_out, 2); 206 } 207 208 static void provisioning_send_public_key(uint16_t the_pb_adv_cid){ 209 prov_buffer_out[0] = MESH_PROV_PUB_KEY; 210 (void)memcpy(&prov_buffer_out[1], prov_ec_q, 64); 211 pb_adv_send_pdu(the_pb_adv_cid, prov_buffer_out, 65); 212 // store for confirmation inputs: len 64 213 (void)memcpy(&prov_confirmation_inputs[17], &prov_buffer_out[1], 64); 214 } 215 216 static void provisioning_send_confirm(uint16_t the_pb_adv_cid){ 217 prov_buffer_out[0] = MESH_PROV_CONFIRM; 218 (void)memcpy(&prov_buffer_out[1], confirmation_provisioner, 16); 219 pb_adv_send_pdu(the_pb_adv_cid, prov_buffer_out, 17); 220 } 221 222 static void provisioning_send_random(uint16_t the_pb_adv_cid){ 223 prov_buffer_out[0] = MESH_PROV_RANDOM; 224 (void)memcpy(&prov_buffer_out[1], random_provisioner, 16); 225 pb_adv_send_pdu(the_pb_adv_cid, prov_buffer_out, 17); 226 } 227 228 static void provisioning_send_data(uint16_t the_pb_adv_cid){ 229 prov_buffer_out[0] = MESH_PROV_DATA; 230 (void)memcpy(&prov_buffer_out[1], enc_provisioning_data, 25); 231 (void)memcpy(&prov_buffer_out[26], provisioning_data_mic, 8); 232 pb_adv_send_pdu(the_pb_adv_cid, prov_buffer_out, 34); 233 } 234 235 typedef enum { 236 PROVISIONER_IDLE, 237 PROVISIONER_W4_LINK_OPENED, 238 PROVISIONER_SEND_INVITE, 239 PROVISIONER_W4_CAPABILITIES, 240 PROVISIONER_W4_AUTH_CONFIGURATION, 241 PROVISIONER_SEND_START, 242 PROVISIONED_W2_EMIT_READ_PUB_KEY_OOB, 243 PROVISIONER_SEND_PUB_KEY, 244 PROVISIONER_W4_PUB_KEY, 245 PROVISIONER_W4_PUB_KEY_OOB, 246 PROVISIONER_W4_INPUT_OOK, 247 PROVISIONER_W4_INPUT_COMPLETE, 248 PROVISIONER_SEND_CONFIRM, 249 PROVISIONER_W4_CONFIRM, 250 PROVISIONER_SEND_RANDOM, 251 PROVISIONER_W4_RANDOM, 252 PROVISIONER_SEND_DATA, 253 PROVISIONER_W4_COMPLETE, 254 PROVISIONER_SEND_ERROR, 255 } provisioner_state_t; 256 257 static provisioner_state_t provisioner_state; 258 259 static void provisioning_run(void){ 260 if (prov_waiting_for_outgoing_complete) return; 261 int start_timer = 1; 262 switch (provisioner_state){ 263 case PROVISIONER_SEND_ERROR: 264 start_timer = 0; // game over 265 provisioning_send_provisioning_error(pb_adv_cid); 266 break; 267 case PROVISIONER_SEND_INVITE: 268 provisioning_send_invite(pb_adv_cid); 269 provisioner_state = PROVISIONER_W4_CAPABILITIES; 270 break; 271 case PROVISIONER_SEND_START: 272 provisioning_send_start(pb_adv_cid); 273 if (prov_start_public_key_used){ 274 provisioner_state = PROVISIONED_W2_EMIT_READ_PUB_KEY_OOB; 275 } else { 276 provisioner_state = PROVISIONER_SEND_PUB_KEY; 277 } 278 break; 279 case PROVISIONED_W2_EMIT_READ_PUB_KEY_OOB: 280 printf("Public OOB: please read OOB from remote device\n"); 281 provisioner_state = PROVISIONER_W4_PUB_KEY_OOB; 282 provisioning_emit_event(MESH_SUBEVENT_PB_PROV_START_RECEIVE_PUBLIC_KEY_OOB, 1); 283 break; 284 case PROVISIONER_SEND_PUB_KEY: 285 provisioning_send_public_key(pb_adv_cid); 286 if (prov_start_public_key_used){ 287 provisioning_public_key_ready(); 288 } else { 289 provisioner_state = PROVISIONER_W4_PUB_KEY; 290 } 291 break; 292 case PROVISIONER_SEND_CONFIRM: 293 provisioning_send_confirm(pb_adv_cid); 294 provisioner_state = PROVISIONER_W4_CONFIRM; 295 break; 296 case PROVISIONER_SEND_RANDOM: 297 provisioning_send_random(pb_adv_cid); 298 provisioner_state = PROVISIONER_W4_RANDOM; 299 break; 300 case PROVISIONER_SEND_DATA: 301 provisioning_send_data(pb_adv_cid); 302 provisioner_state = PROVISIONER_W4_COMPLETE; 303 break; 304 default: 305 return; 306 } 307 if (start_timer){ 308 provisioning_timer_start(); 309 } 310 prov_waiting_for_outgoing_complete = 1; 311 } 312 313 // End of outgoing PDUs 314 315 static void provisioning_done(void){ 316 // if (prov_emit_public_key_oob_active){ 317 // prov_emit_public_key_oob_active = 0; 318 // provisioning_emit_event(MESH_PB_PROV_STOP_EMIT_PUBLIC_KEY_OOB, 1); 319 // } 320 if (prov_emit_output_oob_active){ 321 prov_emit_output_oob_active = 0; 322 provisioning_emit_event(MESH_SUBEVENT_PB_PROV_STOP_EMIT_OUTPUT_OOB, 1); 323 } 324 provisioner_state = PROVISIONER_IDLE; 325 } 326 327 328 static void provisioning_handle_provisioning_error(uint8_t error_code){ 329 provisioning_timer_stop(); 330 prov_error_code = error_code; 331 provisioner_state = PROVISIONER_SEND_ERROR; 332 provisioning_run(); 333 } 334 335 static void provisioning_handle_link_opened(uint16_t the_pb_adv_cid){ 336 UNUSED(the_pb_adv_cid); 337 provisioner_state = PROVISIONER_SEND_INVITE; 338 } 339 340 static void provisioning_handle_capabilities(uint16_t the_pb_adv_cid, const uint8_t * packet_data, uint16_t packet_len){ 341 342 if (packet_len != 11) return; 343 344 // collect confirmation_inputs 345 (void)memcpy(&prov_confirmation_inputs[1], packet_data, packet_len); 346 347 provisioner_state = PROVISIONER_W4_AUTH_CONFIGURATION; 348 349 // notify client and wait for auth method selection 350 uint8_t event[16] = { HCI_EVENT_MESH_META, 3, MESH_SUBEVENT_PB_PROV_CAPABILITIES}; 351 little_endian_store_16(event, 3, the_pb_adv_cid); 352 event[5] = packet_data[0]; 353 little_endian_store_16(event, 6, big_endian_read_16(packet_data, 1)); 354 event[8] = packet_data[3]; 355 event[9] = packet_data[4]; 356 event[10] = packet_data[5]; 357 little_endian_store_16(event, 11, big_endian_read_16(packet_data, 6)); 358 event[13] = packet_data[8]; 359 little_endian_store_16(event, 14, big_endian_read_16(packet_data, 9)); 360 prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 361 } 362 363 static void provisioning_handle_confirmation_provisioner_calculated(void * arg){ 364 UNUSED(arg); 365 366 printf("ConfirmationProvisioner: "); 367 printf_hexdump(confirmation_provisioner, sizeof(confirmation_provisioner)); 368 369 provisioner_state = PROVISIONER_SEND_CONFIRM; 370 provisioning_run(); 371 } 372 373 static void provisioning_handle_random_provisioner(void * arg){ 374 UNUSED(arg); 375 376 printf("RandomProvisioner: "); 377 printf_hexdump(random_provisioner, sizeof(random_provisioner)); 378 379 // re-use prov_confirmation_inputs buffer 380 (void)memcpy(&prov_confirmation_inputs[0], random_provisioner, 16); 381 (void)memcpy(&prov_confirmation_inputs[16], auth_value, 16); 382 383 // calc confirmation device 384 btstack_crypto_aes128_cmac_message(&prov_cmac_request, confirmation_key, 32, prov_confirmation_inputs, confirmation_provisioner, &provisioning_handle_confirmation_provisioner_calculated, NULL); 385 } 386 387 static void provisioning_handle_confirmation_k1_calculated(void * arg){ 388 UNUSED(arg); 389 390 printf("ConfirmationKey: "); 391 printf_hexdump(confirmation_key, sizeof(confirmation_key)); 392 393 // generate random_device 394 btstack_crypto_random_generate(&prov_random_request,random_provisioner, 16, &provisioning_handle_random_provisioner, NULL); 395 } 396 397 static void provisioning_handle_confirmation_salt(void * arg){ 398 UNUSED(arg); 399 400 // dump 401 printf("ConfirmationSalt: "); 402 printf_hexdump(confirmation_salt, sizeof(confirmation_salt)); 403 404 // ConfirmationKey 405 mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), confirmation_salt, (const uint8_t*) "prck", 4, confirmation_key, &provisioning_handle_confirmation_k1_calculated, NULL); 406 } 407 408 static void provisioning_handle_auth_value_ready(void){ 409 // CalculationInputs 410 printf("ConfirmationInputs: "); 411 printf_hexdump(prov_confirmation_inputs, sizeof(prov_confirmation_inputs)); 412 413 // calculate s1 414 btstack_crypto_aes128_cmac_zero(&prov_cmac_request, sizeof(prov_confirmation_inputs), prov_confirmation_inputs, confirmation_salt, &provisioning_handle_confirmation_salt, NULL); 415 } 416 417 static void provisioning_handle_auth_value_input_oob(void * arg){ 418 UNUSED(arg); 419 420 // limit auth value to single digit 421 auth_value[15] = auth_value[15] % 9 + 1; 422 printf("Input OOB: %u\n", auth_value[15]); 423 424 if (prov_authentication_string){ 425 // strings start at 0 while numbers are stored as 16-byte big endian 426 auth_value[0] = auth_value[15] + '0'; 427 auth_value[15] = 0; 428 } 429 430 printf("AuthValue: "); 431 printf_hexdump(auth_value, sizeof(auth_value)); 432 433 // emit output oob value 434 provisioning_emit_output_oob_event(1, auth_value[15]); 435 prov_emit_output_oob_active = 1; 436 437 provisioner_state = PROVISIONER_W4_INPUT_COMPLETE; 438 } 439 440 static void provisioning_handle_input_complete(uint16_t the_pb_adv_cid){ 441 UNUSED(the_pb_adv_cid); 442 provisioning_handle_auth_value_ready(); 443 } 444 445 static void provisioning_public_key_exchange_complete(void){ 446 // reset auth_value 447 memset(auth_value, 0, sizeof(auth_value)); 448 449 // handle authentication method 450 switch (prov_start_authentication_method){ 451 case 0x00: 452 provisioning_handle_auth_value_ready(); 453 break; 454 case 0x01: 455 (void)memcpy(&auth_value[16 - prov_static_oob_len], 456 prov_static_oob_data, prov_static_oob_len); 457 provisioning_handle_auth_value_ready(); 458 break; 459 case 0x02: 460 // Output OOB 461 prov_authentication_string = prov_start_authentication_action == 0x04; 462 printf("Output OOB requested (and we're in Provisioniner role), string %u\n", prov_authentication_string); 463 provisioner_state = PROVISIONER_W4_INPUT_OOK; 464 provisioning_emit_event(MESH_SUBEVENT_PB_PROV_OUTPUT_OOB_REQUEST, 1); 465 break; 466 case 0x03: 467 // Input OOB 468 prov_authentication_string = prov_start_authentication_action == 0x03; 469 printf("Input OOB requested, string %u\n", prov_authentication_string); 470 printf("Generate random for auth_value\n"); 471 // generate single byte of random data to use for authentication 472 btstack_crypto_random_generate(&prov_random_request, &auth_value[15], 1, &provisioning_handle_auth_value_input_oob, NULL); 473 provisioning_emit_event(MESH_SUBEVENT_PB_PROV_START_EMIT_INPUT_OOB, 1); 474 break; 475 default: 476 break; 477 } 478 } 479 480 static void provisioning_handle_public_key_dhkey(void * arg){ 481 UNUSED(arg); 482 483 printf("DHKEY: "); 484 printf_hexdump(dhkey, sizeof(dhkey)); 485 486 #if 0 487 // skip sending own public key when public key oob is used 488 if (prov_public_key_oob_available && prov_public_key_oob_used){ 489 // just copy key for confirmation inputs 490 memcpy(&prov_confirmation_inputs[81], prov_ec_q, 64); 491 } else { 492 // queue public key pdu 493 provisioning_queue_pdu(MESH_PROV_PUB_KEY); 494 } 495 #endif 496 497 provisioning_public_key_exchange_complete(); 498 } 499 500 static void provisioning_public_key_ready(void){ 501 // calculate DHKey 502 btstack_crypto_ecc_p256_calculate_dhkey(&prov_ecc_p256_request, remote_ec_q, dhkey, provisioning_handle_public_key_dhkey, NULL); 503 } 504 505 static void provisioning_handle_public_key(uint16_t the_pb_adv_cid, const uint8_t *packet_data, uint16_t packet_len){ 506 // validate public key 507 if (packet_len != sizeof(remote_ec_q) || btstack_crypto_ecc_p256_validate_public_key(packet_data) != 0){ 508 printf("Public Key invalid, abort provisioning\n"); 509 510 // disconnect provisioning link 511 pb_adv_close_link(the_pb_adv_cid, 0x02); // reason: fail 512 provisioning_timer_stop(); 513 return; 514 } 515 516 #if 0 517 // stop emit public OOK if specified and send to crypto module 518 if (prov_public_key_oob_available && prov_public_key_oob_used){ 519 provisioning_emit_event(MESH_PB_PROV_STOP_EMIT_PUBLIC_KEY_OOB, 1); 520 521 printf("Replace generated ECC with Public Key OOB:"); 522 memcpy(prov_ec_q, prov_public_key_oob_q, 64); 523 printf_hexdump(prov_ec_q, sizeof(prov_ec_q)); 524 btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d); 525 } 526 #endif 527 528 // store for confirmation inputs: len 64 529 (void)memcpy(&prov_confirmation_inputs[81], packet_data, 64); 530 531 // store remote q 532 (void)memcpy(remote_ec_q, packet_data, sizeof(remote_ec_q)); 533 534 provisioning_public_key_ready(); 535 } 536 537 static void provisioning_handle_confirmation(uint16_t the_pb_adv_cid, const uint8_t *packet_data, uint16_t packet_len){ 538 539 UNUSED(the_pb_adv_cid); 540 UNUSED(packet_data); 541 UNUSED(packet_len); 542 543 // 544 if (prov_emit_output_oob_active){ 545 prov_emit_output_oob_active = 0; 546 provisioning_emit_event(MESH_SUBEVENT_PB_PROV_STOP_EMIT_OUTPUT_OOB, 1); 547 } 548 549 #if 0 550 // CalculationInputs 551 printf("ConfirmationInputs: "); 552 printf_hexdump(prov_confirmation_inputs, sizeof(prov_confirmation_inputs)); 553 554 // calculate s1 555 btstack_crypto_aes128_cmac_zero(&prov_cmac_request, sizeof(prov_confirmation_inputs), prov_confirmation_inputs, confirmation_salt, &provisioning_handle_confirmation_s1_calculated, NULL); 556 #endif 557 provisioner_state = PROVISIONER_SEND_RANDOM; 558 } 559 560 static void provisioning_handle_data_encrypted(void * arg){ 561 UNUSED(arg); 562 563 // enc_provisioning_data 564 printf("EncProvisioningData: "); 565 printf_hexdump(enc_provisioning_data, sizeof(enc_provisioning_data)); 566 567 btstack_crypto_ccm_get_authentication_value(&prov_ccm_request, provisioning_data_mic); 568 printf("MIC: "); 569 printf_hexdump(provisioning_data_mic, sizeof(provisioning_data_mic)); 570 571 // send 572 provisioner_state = PROVISIONER_SEND_DATA; 573 provisioning_run(); 574 } 575 576 static void provisioning_handle_session_nonce_calculated(void * arg){ 577 UNUSED(arg); 578 579 // The nonce shall be the 13 least significant octets == zero most significant octets 580 uint8_t temp[13]; 581 (void)memcpy(temp, &session_nonce[3], 13); 582 (void)memcpy(session_nonce, temp, 13); 583 584 // SessionNonce 585 printf("SessionNonce: "); 586 printf_hexdump(session_nonce, 13); 587 588 // setup provisioning data 589 (void)memcpy(&provisioning_data[0], net_key, 16); 590 big_endian_store_16(provisioning_data, 16, net_key_index); 591 provisioning_data[18] = flags; 592 big_endian_store_32(provisioning_data, 19, iv_index); 593 big_endian_store_16(provisioning_data, 23, unicast_address); 594 595 btstack_crypto_ccm_init(&prov_ccm_request, session_key, session_nonce, 25, 0, 8); 596 btstack_crypto_ccm_encrypt_block(&prov_ccm_request, 25, provisioning_data, enc_provisioning_data, &provisioning_handle_data_encrypted, NULL); 597 } 598 599 static void provisioning_handle_session_key_calculated(void * arg){ 600 UNUSED(arg); 601 602 // SessionKey 603 printf("SessionKey: "); 604 printf_hexdump(session_key, sizeof(session_key)); 605 606 // SessionNonce 607 mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prsn", 4, session_nonce, &provisioning_handle_session_nonce_calculated, NULL); 608 } 609 610 611 static void provisioning_handle_provisioning_salt_calculated(void * arg){ 612 UNUSED(arg); 613 614 // ProvisioningSalt 615 printf("ProvisioningSalt: "); 616 printf_hexdump(provisioning_salt, sizeof(provisioning_salt)); 617 618 // SessionKey 619 mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prsk", 4, session_key, &provisioning_handle_session_key_calculated, NULL); 620 } 621 622 static void provisioning_handle_random(uint16_t the_pb_adv_cid, const uint8_t *packet_data, uint16_t packet_len){ 623 624 UNUSED(the_pb_adv_cid); 625 UNUSED(packet_len); 626 627 // TODO: validate Confirmation 628 629 // calc ProvisioningSalt = s1(ConfirmationSalt || RandomProvisioner || RandomDevice) 630 (void)memcpy(&prov_confirmation_inputs[0], confirmation_salt, 16); 631 (void)memcpy(&prov_confirmation_inputs[16], random_provisioner, 16); 632 (void)memcpy(&prov_confirmation_inputs[32], packet_data, 16); 633 btstack_crypto_aes128_cmac_zero(&prov_cmac_request, 48, prov_confirmation_inputs, provisioning_salt, &provisioning_handle_provisioning_salt_calculated, NULL); 634 } 635 636 static void provisioning_handle_complete(uint16_t the_pb_adv_cid){ 637 UNUSED(the_pb_adv_cid); 638 } 639 640 static void provisioning_handle_pdu(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 641 UNUSED(channel); 642 643 if (size < 1) return; 644 645 switch (packet_type){ 646 case HCI_EVENT_PACKET: 647 if (hci_event_packet_get_type(packet) != HCI_EVENT_MESH_META) break; 648 649 switch (hci_event_mesh_meta_get_subevent_code(packet)){ 650 case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN: 651 if (provisioner_state != PROVISIONER_W4_LINK_OPENED) break; 652 switch (mesh_subevent_pb_transport_link_open_get_status(packet)) { 653 case ERROR_CODE_SUCCESS: 654 printf("Link opened, sending Invite\n"); 655 provisioning_handle_link_opened(pb_adv_cid); 656 break; 657 default: 658 printf("Link open failed, abort\n"); 659 provisioning_done(); 660 break; 661 } 662 break; 663 case MESH_SUBEVENT_PB_TRANSPORT_PDU_SENT: 664 printf("Outgoing packet acked\n"); 665 prov_waiting_for_outgoing_complete = 0; 666 break; 667 case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED: 668 printf("Link close, reset state\n"); 669 provisioning_done(); 670 break; 671 default: 672 break; 673 } 674 break; 675 case PROVISIONING_DATA_PACKET: 676 // check state 677 switch (provisioner_state){ 678 case PROVISIONER_W4_CAPABILITIES: 679 if (packet[0] != MESH_PROV_CAPABILITIES) provisioning_handle_provisioning_error(0x03); 680 printf("MESH_PROV_CAPABILITIES: "); 681 printf_hexdump(&packet[1], size-1); 682 provisioning_handle_capabilities(pb_adv_cid, &packet[1], size-1); 683 break; 684 case PROVISIONER_W4_PUB_KEY: 685 if (packet[0] != MESH_PROV_PUB_KEY) provisioning_handle_provisioning_error(0x03); 686 printf("MESH_PROV_PUB_KEY: "); 687 printf_hexdump(&packet[1], size-1); 688 provisioning_handle_public_key(pb_adv_cid, &packet[1], size-1); 689 break; 690 case PROVISIONER_W4_INPUT_COMPLETE: 691 if (packet[0] != MESH_PROV_INPUT_COMPLETE) provisioning_handle_provisioning_error(0x03); 692 printf("MESH_PROV_INPUT_COMPLETE: "); 693 printf_hexdump(&packet[1], size-1); 694 provisioning_handle_input_complete(pb_adv_cid); 695 break; 696 case PROVISIONER_W4_CONFIRM: 697 if (packet[0] != MESH_PROV_CONFIRM) provisioning_handle_provisioning_error(0x03); 698 printf("MESH_PROV_CONFIRM: "); 699 printf_hexdump(&packet[1], size-1); 700 provisioning_handle_confirmation(pb_adv_cid, &packet[1], size-1); 701 break; 702 case PROVISIONER_W4_RANDOM: 703 if (packet[0] != MESH_PROV_RANDOM) provisioning_handle_provisioning_error(0x03); 704 printf("MESH_PROV_RANDOM: "); 705 printf_hexdump(&packet[1], size-1); 706 provisioning_handle_random(pb_adv_cid, &packet[1], size-1); 707 break; 708 case PROVISIONER_W4_COMPLETE: 709 if (packet[0] != MESH_PROV_COMPLETE) provisioning_handle_provisioning_error(0x03); 710 printf("MESH_PROV_COMPLETE: "); 711 provisioning_handle_complete(pb_adv_cid); 712 break; 713 default: 714 printf("TODO: handle provisioning state %x\n", provisioner_state); 715 break; 716 } 717 break; 718 default: 719 break; 720 } 721 provisioning_run(); 722 } 723 724 static void prov_key_generated(void * arg){ 725 UNUSED(arg); 726 printf("ECC-P256: "); 727 printf_hexdump(prov_ec_q, sizeof(prov_ec_q)); 728 // allow override 729 if (prov_public_key_oob_available){ 730 printf("Replace generated ECC with Public Key OOB:"); 731 (void)memcpy(prov_ec_q, prov_public_key_oob_q, 64); 732 printf_hexdump(prov_ec_q, sizeof(prov_ec_q)); 733 btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d); 734 } 735 } 736 737 void provisioning_provisioner_init(void){ 738 pb_adv_cid = MESH_PB_TRANSPORT_INVALID_CID; 739 pb_adv_init(); 740 pb_adv_register_provisioner_packet_handler(&provisioning_handle_pdu); 741 } 742 743 void provisioning_provisioner_register_packet_handler(btstack_packet_handler_t packet_handler){ 744 prov_packet_handler = packet_handler; 745 } 746 747 uint16_t provisioning_provisioner_start_provisioning(const uint8_t * device_uuid){ 748 // generate new public key 749 btstack_crypto_ecc_p256_generate_key(&prov_ecc_p256_request, prov_ec_q, &prov_key_generated, NULL); 750 751 if (pb_adv_cid == MESH_PB_TRANSPORT_INVALID_CID) { 752 provisioner_state = PROVISIONER_W4_LINK_OPENED; 753 pb_adv_cid = pb_adv_create_link(device_uuid); 754 } 755 return pb_adv_cid; 756 } 757 758 void provisioning_provisioner_set_static_oob(uint16_t the_pb_adv_cid, uint16_t static_oob_len, const uint8_t * static_oob_data){ 759 UNUSED(the_pb_adv_cid); 760 prov_static_oob_data = static_oob_data; 761 prov_static_oob_len = btstack_min(static_oob_len, 16); 762 } 763 764 uint8_t provisioning_provisioner_select_authentication_method(uint16_t the_pb_adv_cid, uint8_t algorithm, uint8_t public_key_used, uint8_t authentication_method, uint8_t authentication_action, uint8_t authentication_size){ 765 UNUSED(the_pb_adv_cid); 766 767 if (provisioner_state != PROVISIONER_W4_AUTH_CONFIGURATION) return ERROR_CODE_COMMAND_DISALLOWED; 768 769 prov_start_algorithm = algorithm; 770 prov_start_public_key_used = public_key_used; 771 prov_start_authentication_method = authentication_method; 772 prov_start_authentication_action = authentication_action; 773 prov_start_authentication_size = authentication_size; 774 provisioner_state = PROVISIONER_SEND_START; 775 776 return ERROR_CODE_SUCCESS; 777 } 778 779 uint8_t provisioning_provisioner_public_key_oob_received(uint16_t the_pb_adv_cid, const uint8_t * public_key){ 780 UNUSED(the_pb_adv_cid); 781 782 if (provisioner_state != PROVISIONER_W4_PUB_KEY_OOB) return ERROR_CODE_COMMAND_DISALLOWED; 783 784 // store for confirmation inputs: len 64 785 (void)memcpy(&prov_confirmation_inputs[81], public_key, 64); 786 787 // store remote q 788 (void)memcpy(remote_ec_q, public_key, sizeof(remote_ec_q)); 789 790 // continue procedure 791 provisioner_state = PROVISIONER_SEND_PUB_KEY; 792 provisioning_run(); 793 794 return ERROR_CODE_SUCCESS; 795 } 796 797 void provisioning_provisioner_input_oob_complete_numeric(uint16_t the_pb_adv_cid, uint32_t input_oob){ 798 UNUSED(the_pb_adv_cid); 799 if (provisioner_state != PROVISIONER_W4_INPUT_OOK) return; 800 801 // store input_oob as auth value 802 big_endian_store_32(auth_value, 12, input_oob); 803 provisioning_handle_auth_value_ready(); 804 } 805 806 void provisioning_provisioner_input_oob_complete_alphanumeric(uint16_t the_pb_adv_cid, const uint8_t * input_oob_data, uint16_t input_oob_len){ 807 UNUSED(the_pb_adv_cid); 808 if (provisioner_state != PROVISIONER_W4_INPUT_OOK) return; 809 810 // store input_oob and fillup with zeros 811 input_oob_len = btstack_min(input_oob_len, 16); 812 memset(auth_value, 0, 16); 813 (void)memcpy(auth_value, input_oob_data, input_oob_len); 814 provisioning_handle_auth_value_ready(); 815 } 816 817