1 /* 2 * Copyright (C) 2014 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__ "mesh_upper_transport.c" 39 40 #include "mesh/mesh_upper_transport.h" 41 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 46 #include "btstack_util.h" 47 #include "btstack_memory.h" 48 #include "btstack_debug.h" 49 50 #include "mesh/beacon.h" 51 #include "mesh/mesh_iv_index_seq_number.h" 52 #include "mesh/mesh_keys.h" 53 #include "mesh/mesh_lower_transport.h" 54 #include "mesh/mesh_peer.h" 55 #include "mesh/mesh_virtual_addresses.h" 56 57 // TODO: extract mesh_pdu functions into lower transport or network 58 #include "mesh/mesh_access.h" 59 60 // combined key x address iterator for upper transport decryption 61 62 typedef struct { 63 // state 64 mesh_transport_key_iterator_t key_it; 65 mesh_virtual_address_iterator_t address_it; 66 // elements 67 const mesh_transport_key_t * key; 68 const mesh_virtual_address_t * address; 69 // address - might be virtual 70 uint16_t dst; 71 // key info 72 } mesh_transport_key_and_virtual_address_iterator_t; 73 74 static void (*higher_layer_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu); 75 76 static void mesh_upper_transport_validate_unsegmented_message(void); 77 static void mesh_upper_transport_validate_segmented_message(void); 78 static void mesh_upper_transport_run(void); 79 80 static int crypto_active; 81 82 static mesh_unsegmented_incoming_pdu_t * incoming_unsegmented_pdu_raw; 83 static mesh_network_pdu_t * incoming_network_pdu_decoded; 84 85 static mesh_message_pdu_t incoming_message_pdu_singleton; 86 87 static mesh_access_pdu_t * incoming_access_pdu_encrypted; 88 static mesh_access_pdu_t * incoming_access_pdu_decrypted; 89 90 static mesh_access_pdu_t incoming_access_pdu_encrypted_singleton; 91 static mesh_access_pdu_t incoming_access_pdu_decrypted_singleton; 92 93 static mesh_message_pdu_t outgoing_segmented_message_singleton; 94 95 static mesh_transport_pdu_t * outgoing_segmented_pdu; 96 97 static uint8_t application_nonce[13]; 98 static btstack_crypto_ccm_t ccm; 99 static mesh_transport_key_and_virtual_address_iterator_t mesh_transport_key_it; 100 101 // upper transport callbacks - in access layer 102 static void (*mesh_access_message_handler)(mesh_pdu_t * pdu); 103 static void (*mesh_control_message_handler)(mesh_pdu_t * pdu); 104 105 // incoming unsegmented (network) and segmented (transport) control and access messages 106 static btstack_linked_list_t upper_transport_incoming; 107 108 // outgoing unsegmented (network) and segmented (uppert_transport_outgoing) control and access messages 109 static btstack_linked_list_t upper_transport_outgoing; 110 111 112 // TODO: higher layer define used for assert 113 #define MESH_ACCESS_OPCODE_NOT_SET 0xFFFFFFFEu 114 115 void mesh_upper_transport_send_access_pdu(mesh_pdu_t *pdu){ 116 btstack_assert(pdu->pdu_type != MESH_PDU_TYPE_NETWORK); 117 118 btstack_linked_list_add_tail(&upper_transport_outgoing, (btstack_linked_item_t*) pdu); 119 mesh_upper_transport_run(); 120 } 121 122 void mesh_upper_transport_send_control_pdu(mesh_pdu_t * pdu){ 123 if (pdu->pdu_type == MESH_PDU_TYPE_NETWORK){ 124 btstack_assert( ((mesh_network_pdu_t *) pdu)->len >= 9); 125 } 126 127 btstack_linked_list_add_tail(&upper_transport_outgoing, (btstack_linked_item_t*) pdu); 128 mesh_upper_transport_run(); 129 } 130 131 static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len){ 132 printf("%-20s ", name); 133 printf_hexdump(data, len); 134 } 135 // static void mesh_print_x(const char * name, uint32_t value){ 136 // printf("%20s: 0x%x", name, (int) value); 137 // } 138 139 static void mesh_transport_key_and_virtual_address_iterator_init(mesh_transport_key_and_virtual_address_iterator_t *it, 140 uint16_t dst, uint16_t netkey_index, uint8_t akf, 141 uint8_t aid) { 142 printf("KEY_INIT: dst %04x, akf %x, aid %x\n", dst, akf, aid); 143 // config 144 it->dst = dst; 145 // init elements 146 it->key = NULL; 147 it->address = NULL; 148 // init element iterators 149 mesh_transport_key_aid_iterator_init(&it->key_it, netkey_index, akf, aid); 150 // init address iterator 151 if (mesh_network_address_virtual(it->dst)){ 152 mesh_virtual_address_iterator_init(&it->address_it, dst); 153 // get first key 154 if (mesh_transport_key_aid_iterator_has_more(&it->key_it)) { 155 it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it); 156 } 157 } 158 } 159 160 // cartesian product: keys x addressses 161 static int mesh_transport_key_and_virtual_address_iterator_has_more(mesh_transport_key_and_virtual_address_iterator_t * it){ 162 if (mesh_network_address_virtual(it->dst)) { 163 // find next valid entry 164 while (true){ 165 if (mesh_virtual_address_iterator_has_more(&it->address_it)) return 1; 166 if (!mesh_transport_key_aid_iterator_has_more(&it->key_it)) return 0; 167 // get next key 168 it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it); 169 mesh_virtual_address_iterator_init(&it->address_it, it->dst); 170 } 171 } else { 172 return mesh_transport_key_aid_iterator_has_more(&it->key_it); 173 } 174 } 175 176 static void mesh_transport_key_and_virtual_address_iterator_next(mesh_transport_key_and_virtual_address_iterator_t * it){ 177 if (mesh_network_address_virtual(it->dst)) { 178 it->address = mesh_virtual_address_iterator_get_next(&it->address_it); 179 } else { 180 it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it); 181 } 182 } 183 184 // UPPER TRANSPORT 185 186 static uint16_t mesh_access_dst(mesh_access_pdu_t * access_pdu){ 187 return big_endian_read_16(access_pdu->network_header, 7); 188 } 189 190 uint16_t mesh_access_ctl(mesh_access_pdu_t * access_pdu){ 191 return access_pdu->network_header[1] >> 7; 192 } 193 194 195 // stub lower transport 196 197 static void mesh_upper_transport_dump_pdus(const char *name, btstack_linked_list_t *list){ 198 printf("List: %s:\n", name); 199 btstack_linked_list_iterator_t it; 200 btstack_linked_list_iterator_init(&it, list); 201 while (btstack_linked_list_iterator_has_next(&it)){ 202 mesh_pdu_t * pdu = (mesh_pdu_t*) btstack_linked_list_iterator_next(&it); 203 printf("- %p\n", pdu); 204 // printf_hexdump( mesh_pdu_data(pdu), mesh_pdu_len(pdu)); 205 } 206 } 207 208 static void mesh_upper_transport_reset_pdus(btstack_linked_list_t *list){ 209 while (!btstack_linked_list_empty(list)){ 210 mesh_upper_transport_pdu_free((mesh_pdu_t *) btstack_linked_list_pop(list)); 211 } 212 } 213 214 void mesh_upper_transport_dump(void){ 215 printf("incoming_unsegmented_pdu_raw: %p\n", incoming_unsegmented_pdu_raw); 216 printf("incoming_network_pdu_decoded: %p\n", incoming_network_pdu_decoded); 217 mesh_upper_transport_dump_pdus("upper_transport_incoming", &upper_transport_incoming); 218 } 219 220 void mesh_upper_transport_reset(void){ 221 crypto_active = 0; 222 if (incoming_unsegmented_pdu_raw){ 223 mesh_network_pdu_t * network_pdu = incoming_unsegmented_pdu_raw->segment; 224 btstack_assert(network_pdu != NULL); 225 incoming_unsegmented_pdu_raw->segment = NULL; 226 mesh_network_pdu_free(network_pdu); 227 incoming_unsegmented_pdu_raw = NULL; 228 } 229 mesh_upper_transport_reset_pdus(&upper_transport_incoming); 230 } 231 232 static uint32_t iv_index_for_ivi_nid(uint8_t ivi_nid){ 233 // get IV Index and IVI 234 uint32_t iv_index = mesh_get_iv_index(); 235 int ivi = ivi_nid >> 7; 236 237 // if least significant bit differs, use previous IV Index 238 if ((iv_index & 1 ) ^ ivi){ 239 iv_index--; 240 } 241 return iv_index; 242 } 243 244 static void transport_unsegmented_setup_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){ 245 nonce[1] = 0x00; // SZMIC if a Segmented Access message or 0 for all other message formats 246 (void)memcpy(&nonce[2], &network_pdu->data[2], 7); 247 big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(network_pdu->data[0])); 248 } 249 250 static void transport_segmented_setup_nonce(uint8_t * nonce, const mesh_pdu_t * pdu){ 251 mesh_transport_pdu_t * transport_pdu; 252 mesh_access_pdu_t * access_pdu; 253 switch (pdu->pdu_type){ 254 case MESH_PDU_TYPE_TRANSPORT: 255 transport_pdu = (mesh_transport_pdu_t *) pdu; 256 nonce[1] = transport_pdu->transmic_len == 8 ? 0x80 : 0x00; 257 (void)memcpy(&nonce[2], &transport_pdu->network_header[2], 7); 258 big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(transport_pdu->network_header[0])); 259 break; 260 case MESH_PDU_TYPE_ACCESS: 261 access_pdu = (mesh_access_pdu_t *) pdu; 262 nonce[1] = access_pdu->transmic_len == 8 ? 0x80 : 0x00; 263 (void)memcpy(&nonce[2], &access_pdu->network_header[2], 7); 264 big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(access_pdu->network_header[0])); 265 break; 266 default: 267 btstack_assert(0); 268 break; 269 } 270 } 271 272 static void transport_unsegmented_setup_application_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){ 273 nonce[0] = 0x01; 274 transport_unsegmented_setup_nonce(nonce, network_pdu); 275 mesh_print_hex("AppNonce", nonce, 13); 276 } 277 278 static void transport_unsegmented_setup_device_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){ 279 nonce[0] = 0x02; 280 transport_unsegmented_setup_nonce(nonce, network_pdu); 281 mesh_print_hex("DeviceNonce", nonce, 13); 282 } 283 284 static void transport_segmented_setup_application_nonce(uint8_t * nonce, const mesh_pdu_t * pdu){ 285 nonce[0] = 0x01; 286 transport_segmented_setup_nonce(nonce, pdu); 287 mesh_print_hex("AppNonce", nonce, 13); 288 } 289 290 static void transport_segmented_setup_device_nonce(uint8_t * nonce, const mesh_pdu_t * pdu){ 291 nonce[0] = 0x02; 292 transport_segmented_setup_nonce(nonce, pdu); 293 mesh_print_hex("DeviceNonce", nonce, 13); 294 } 295 296 static void mesh_upper_unsegmented_control_message_received(mesh_unsegmented_incoming_pdu_t * unsegmented_incoming_pdu){ 297 if (mesh_control_message_handler){ 298 mesh_control_message_handler((mesh_pdu_t*) unsegmented_incoming_pdu); 299 } else { 300 mesh_network_pdu_t * network_pdu =unsegmented_incoming_pdu->segment; 301 uint8_t * lower_transport_pdu = mesh_network_pdu_data(network_pdu); 302 uint8_t opcode = lower_transport_pdu[0]; 303 printf("[!] Unhandled Control message with opcode %02x\n", opcode); 304 // done 305 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t*) unsegmented_incoming_pdu); 306 } 307 } 308 309 static void mesh_upper_transport_process_message_done(mesh_message_pdu_t *message_pdu){ 310 crypto_active = 0; 311 btstack_assert(message_pdu == &incoming_message_pdu_singleton); 312 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&incoming_message_pdu_singleton.segments); 313 if (mesh_network_control(network_pdu)) { 314 btstack_assert(0); 315 } else { 316 btstack_assert(network_pdu != NULL); 317 mesh_network_pdu_free(network_pdu); 318 mesh_pdu_t * pdu = (mesh_pdu_t *) incoming_unsegmented_pdu_raw; 319 incoming_unsegmented_pdu_raw = NULL; 320 mesh_lower_transport_message_processed_by_higher_layer(pdu); 321 } 322 mesh_upper_transport_run(); 323 } 324 325 static void mesh_upper_transport_process_unsegmented_message_done(mesh_pdu_t * pdu){ 326 btstack_assert(pdu != NULL); 327 btstack_assert(pdu->pdu_type == MESH_PDU_TYPE_UNSEGMENTED_INCOMING); 328 329 mesh_unsegmented_incoming_pdu_t * unsegmented_incoming_pdu = (mesh_unsegmented_incoming_pdu_t *) pdu; 330 btstack_assert(unsegmented_incoming_pdu == incoming_unsegmented_pdu_raw); 331 332 crypto_active = 0; 333 incoming_unsegmented_pdu_raw = NULL; 334 mesh_network_pdu_t * network_pdu = unsegmented_incoming_pdu->segment; 335 if (!mesh_network_control(network_pdu)) { 336 mesh_network_pdu_free(network_pdu); 337 } 338 339 mesh_lower_transport_message_processed_by_higher_layer(pdu); 340 mesh_upper_transport_run(); 341 } 342 343 static void mesh_upper_transport_process_segmented_access_message_done(mesh_access_pdu_t *access_pdu){ 344 crypto_active = 0; 345 btstack_assert(mesh_access_ctl(access_pdu) == 0); 346 incoming_access_pdu_encrypted = NULL; 347 mesh_upper_transport_run(); 348 } 349 350 static void mesh_upper_transport_validate_unsegmented_message_ccm(void * arg){ 351 UNUSED(arg); 352 353 uint8_t * lower_transport_pdu = mesh_network_pdu_data(incoming_network_pdu_decoded); 354 uint8_t trans_mic_len = 4; 355 356 // store TransMIC 357 uint8_t trans_mic[8]; 358 btstack_crypto_ccm_get_authentication_value(&ccm, trans_mic); 359 mesh_print_hex("TransMIC", trans_mic, trans_mic_len); 360 361 uint8_t * upper_transport_pdu = mesh_network_pdu_data(incoming_network_pdu_decoded) + 1; 362 uint8_t upper_transport_pdu_len = mesh_network_pdu_len(incoming_network_pdu_decoded) - 1; 363 364 mesh_print_hex("Decryted PDU", upper_transport_pdu, upper_transport_pdu_len - trans_mic_len); 365 366 if (memcmp(trans_mic, &upper_transport_pdu[upper_transport_pdu_len - trans_mic_len], trans_mic_len) == 0){ 367 printf("TransMIC matches\n"); 368 369 // remove TransMIC from payload 370 incoming_network_pdu_decoded->len -= trans_mic_len; 371 372 // if virtual address, update dst to pseudo_dst 373 if (mesh_network_address_virtual(mesh_network_dst(incoming_network_pdu_decoded))){ 374 big_endian_store_16(incoming_network_pdu_decoded->data, 7, mesh_transport_key_it.address->pseudo_dst); 375 } 376 377 // pass to upper layer 378 if (mesh_access_message_handler){ 379 mesh_pdu_t * pdu = (mesh_pdu_t*) incoming_network_pdu_decoded; 380 incoming_network_pdu_decoded = NULL; 381 mesh_access_message_handler((mesh_pdu_t*) &incoming_message_pdu_singleton); 382 } else { 383 printf("[!] Unhandled Unsegmented Access message\n"); 384 // done 385 mesh_upper_transport_process_unsegmented_message_done((mesh_pdu_t*) incoming_unsegmented_pdu_raw); 386 } 387 388 printf("\n"); 389 } else { 390 uint8_t afk = lower_transport_pdu[0] & 0x40; 391 if (afk){ 392 printf("TransMIC does not match, try next key\n"); 393 mesh_upper_transport_validate_unsegmented_message(); 394 } else { 395 printf("TransMIC does not match device key, done\n"); 396 // done 397 mesh_upper_transport_process_unsegmented_message_done((mesh_pdu_t*) incoming_unsegmented_pdu_raw); 398 } 399 } 400 } 401 402 static void mesh_upper_transport_validate_segmented_message_ccm(void * arg){ 403 UNUSED(arg); 404 405 uint8_t * upper_transport_pdu = incoming_access_pdu_decrypted->data; 406 uint8_t upper_transport_pdu_len = incoming_access_pdu_decrypted->len - incoming_access_pdu_decrypted->transmic_len; 407 408 mesh_print_hex("Decrypted PDU", upper_transport_pdu, upper_transport_pdu_len); 409 410 // store TransMIC 411 uint8_t trans_mic[8]; 412 btstack_crypto_ccm_get_authentication_value(&ccm, trans_mic); 413 mesh_print_hex("TransMIC", trans_mic, incoming_access_pdu_decrypted->transmic_len); 414 415 if (memcmp(trans_mic, &upper_transport_pdu[upper_transport_pdu_len], incoming_access_pdu_decrypted->transmic_len) == 0){ 416 printf("TransMIC matches\n"); 417 418 // remove TransMIC from payload 419 incoming_access_pdu_decrypted->len -= incoming_access_pdu_decrypted->transmic_len; 420 421 // if virtual address, update dst to pseudo_dst 422 if (mesh_network_address_virtual(mesh_access_dst(incoming_access_pdu_decrypted))){ 423 big_endian_store_16(incoming_access_pdu_decrypted->network_header, 7, mesh_transport_key_it.address->pseudo_dst); 424 } 425 426 // pass to upper layer 427 btstack_assert(mesh_access_message_handler != NULL); 428 mesh_pdu_t * pdu = (mesh_pdu_t*) incoming_access_pdu_decrypted; 429 incoming_network_pdu_decoded = NULL; 430 mesh_access_message_handler(pdu); 431 432 printf("\n"); 433 434 } else { 435 uint8_t akf = incoming_access_pdu_decrypted->akf_aid_control & 0x40; 436 if (akf){ 437 printf("TransMIC does not match, try next key\n"); 438 mesh_upper_transport_validate_segmented_message(); 439 } else { 440 printf("TransMIC does not match device key, done\n"); 441 // done 442 mesh_upper_transport_process_segmented_access_message_done(incoming_access_pdu_decrypted); 443 } 444 } 445 } 446 447 static void mesh_upper_transport_validate_segmented_message_digest(void * arg){ 448 UNUSED(arg); 449 uint8_t upper_transport_pdu_len = incoming_access_pdu_encrypted->len - incoming_access_pdu_encrypted->transmic_len; 450 uint8_t * upper_transport_pdu_data_in = incoming_access_pdu_encrypted->data; 451 uint8_t * upper_transport_pdu_data_out = incoming_access_pdu_decrypted->data; 452 btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu_data_in, upper_transport_pdu_data_out, &mesh_upper_transport_validate_segmented_message_ccm, NULL); 453 } 454 455 static void mesh_upper_transport_validate_unsegmented_message_digest(void * arg){ 456 UNUSED(arg); 457 mesh_network_pdu_t * network_pdu = incoming_unsegmented_pdu_raw->segment; 458 uint8_t trans_mic_len = 4; 459 uint8_t lower_transport_pdu_len = network_pdu->len - 9; 460 uint8_t * upper_transport_pdu_data_in = &network_pdu->data[10]; 461 uint8_t * upper_transport_pdu_data_out = &incoming_network_pdu_decoded->data[10]; 462 uint8_t upper_transport_pdu_len = lower_transport_pdu_len - 1 - trans_mic_len; 463 btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu_data_in, upper_transport_pdu_data_out, &mesh_upper_transport_validate_unsegmented_message_ccm, NULL); 464 } 465 466 static void mesh_upper_transport_validate_unsegmented_message(void){ 467 468 if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){ 469 printf("No valid transport key found\n"); 470 mesh_upper_transport_process_unsegmented_message_done((mesh_pdu_t*) incoming_unsegmented_pdu_raw); 471 return; 472 } 473 mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it); 474 const mesh_transport_key_t * message_key = mesh_transport_key_it.key; 475 476 mesh_network_pdu_t * network_pdu = incoming_unsegmented_pdu_raw->segment; 477 if (message_key->akf){ 478 transport_unsegmented_setup_application_nonce(application_nonce, network_pdu); 479 } else { 480 transport_unsegmented_setup_device_nonce(application_nonce, network_pdu); 481 } 482 483 // store application / device key index 484 mesh_print_hex("AppOrDevKey", message_key->key, 16); 485 incoming_message_pdu_singleton.appkey_index = message_key->appkey_index; 486 487 // unsegmented message have TransMIC of 32 bit 488 uint8_t trans_mic_len = 4; 489 printf("Unsegmented Access message with TransMIC len 4\n"); 490 491 uint8_t lower_transport_pdu_len = network_pdu->len - 9; 492 uint8_t * upper_transport_pdu_data = &network_pdu->data[10]; 493 uint8_t upper_transport_pdu_len = lower_transport_pdu_len - 1 - trans_mic_len; 494 495 mesh_print_hex("EncAccessPayload", upper_transport_pdu_data, upper_transport_pdu_len); 496 497 // decrypt ccm 498 crypto_active = 1; 499 uint16_t aad_len = 0; 500 if (mesh_network_address_virtual(mesh_network_dst(incoming_network_pdu_decoded))){ 501 aad_len = 16; 502 } 503 btstack_crypto_ccm_init(&ccm, message_key->key, application_nonce, upper_transport_pdu_len, aad_len, trans_mic_len); 504 if (aad_len){ 505 btstack_crypto_ccm_digest(&ccm, (uint8_t*) mesh_transport_key_it.address->label_uuid, aad_len, &mesh_upper_transport_validate_unsegmented_message_digest, NULL); 506 } else { 507 mesh_upper_transport_validate_unsegmented_message_digest(incoming_network_pdu_decoded); 508 } 509 } 510 511 static void mesh_upper_transport_validate_segmented_message(void){ 512 uint8_t * upper_transport_pdu_data = incoming_access_pdu_decrypted->data; 513 uint8_t upper_transport_pdu_len = incoming_access_pdu_decrypted->len - incoming_access_pdu_decrypted->transmic_len; 514 515 if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){ 516 printf("No valid transport key found\n"); 517 mesh_upper_transport_process_segmented_access_message_done(incoming_access_pdu_decrypted); 518 return; 519 } 520 mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it); 521 const mesh_transport_key_t * message_key = mesh_transport_key_it.key; 522 523 if (message_key->akf){ 524 transport_segmented_setup_application_nonce(application_nonce, (mesh_pdu_t *) incoming_access_pdu_encrypted); 525 } else { 526 transport_segmented_setup_device_nonce(application_nonce, (mesh_pdu_t *) incoming_access_pdu_encrypted); 527 } 528 529 // store application / device key index 530 mesh_print_hex("AppOrDevKey", message_key->key, 16); 531 incoming_access_pdu_decrypted->appkey_index = message_key->appkey_index; 532 533 mesh_print_hex("EncAccessPayload", upper_transport_pdu_data, upper_transport_pdu_len); 534 535 // decrypt ccm 536 crypto_active = 1; 537 uint16_t aad_len = 0; 538 if (mesh_network_address_virtual(mesh_access_dst(incoming_access_pdu_decrypted))){ 539 aad_len = 16; 540 } 541 btstack_crypto_ccm_init(&ccm, message_key->key, application_nonce, upper_transport_pdu_len, aad_len, incoming_access_pdu_decrypted->transmic_len); 542 543 if (aad_len){ 544 btstack_crypto_ccm_digest(&ccm, (uint8_t *) mesh_transport_key_it.address->label_uuid, aad_len, &mesh_upper_transport_validate_segmented_message_digest, NULL); 545 } else { 546 mesh_upper_transport_validate_segmented_message_digest(NULL); 547 } 548 } 549 550 static void mesh_upper_transport_process_unsegmented_access_message(void){ 551 // copy original pdu 552 mesh_network_pdu_t * raw_network_pdu = incoming_unsegmented_pdu_raw->segment; 553 incoming_network_pdu_decoded->len = raw_network_pdu->len; 554 (void)memcpy(incoming_network_pdu_decoded->data, 555 raw_network_pdu->data, 556 incoming_network_pdu_decoded->len); 557 558 // 559 uint8_t * lower_transport_pdu = &raw_network_pdu->data[9]; 560 uint8_t lower_transport_pdu_len = raw_network_pdu->len - 9; 561 562 mesh_print_hex("Lower Transport network pdu", &raw_network_pdu->data[9], lower_transport_pdu_len); 563 564 uint8_t aid = lower_transport_pdu[0] & 0x3f; 565 uint8_t akf = (lower_transport_pdu[0] & 0x40) >> 6; 566 printf("AKF: %u\n", akf); 567 printf("AID: %02x\n", aid); 568 569 mesh_transport_key_and_virtual_address_iterator_init(&mesh_transport_key_it, mesh_network_dst(incoming_network_pdu_decoded), 570 incoming_network_pdu_decoded->netkey_index, akf, aid); 571 mesh_upper_transport_validate_unsegmented_message(); 572 } 573 574 static void mesh_upper_transport_process_message(void){ 575 // copy original pdu 576 (void)memcpy(incoming_access_pdu_decrypted, incoming_access_pdu_encrypted, 577 sizeof(mesh_transport_pdu_t)); 578 579 // 580 uint8_t * upper_transport_pdu = incoming_access_pdu_decrypted->data; 581 uint8_t upper_transport_pdu_len = incoming_access_pdu_decrypted->len - incoming_access_pdu_decrypted->transmic_len; 582 mesh_print_hex("Upper Transport pdu", upper_transport_pdu, upper_transport_pdu_len); 583 584 uint8_t aid = incoming_access_pdu_decrypted->akf_aid_control & 0x3f; 585 uint8_t akf = (incoming_access_pdu_decrypted->akf_aid_control & 0x40) >> 6; 586 587 printf("AKF: %u\n", akf); 588 printf("AID: %02x\n", aid); 589 590 mesh_transport_key_and_virtual_address_iterator_init(&mesh_transport_key_it, mesh_access_dst(incoming_access_pdu_decrypted), 591 incoming_access_pdu_decrypted->netkey_index, akf, aid); 592 mesh_upper_transport_validate_segmented_message(); 593 } 594 595 static void mesh_upper_transport_message_received(mesh_pdu_t * pdu){ 596 btstack_linked_list_add_tail(&upper_transport_incoming, (btstack_linked_item_t*) pdu); 597 mesh_upper_transport_run(); 598 } 599 600 601 602 static void mesh_upper_transport_pdu_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu){ 603 mesh_transport_pdu_t * transport_pdu; 604 switch (callback_type){ 605 case MESH_TRANSPORT_PDU_RECEIVED: 606 mesh_upper_transport_message_received(pdu); 607 break; 608 case MESH_TRANSPORT_PDU_SENT: 609 // free chunks 610 while (!btstack_linked_list_empty(&outgoing_segmented_message_singleton.segments)){ 611 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&outgoing_segmented_message_singleton.segments); 612 mesh_network_pdu_free(network_pdu); 613 } 614 // notify upper layer but use transport pdu 615 transport_pdu = outgoing_segmented_pdu; 616 outgoing_segmented_pdu = NULL; 617 if (higher_layer_handler){ 618 higher_layer_handler(callback_type, status, (mesh_pdu_t*) transport_pdu); 619 } else { 620 mesh_transport_pdu_free(transport_pdu); 621 } 622 mesh_upper_transport_run(); 623 break; 624 default: 625 break; 626 } 627 } 628 static void mesh_upper_transport_send_unsegmented_access_pdu_ccm(void * arg){ 629 crypto_active = 0; 630 631 mesh_message_pdu_t * message_pdu = (mesh_message_pdu_t *) arg; 632 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&message_pdu->segments); 633 634 uint8_t * upper_transport_pdu = mesh_network_pdu_data(network_pdu) + 1; 635 uint8_t upper_transport_pdu_len = mesh_network_pdu_len(network_pdu) - 1; 636 mesh_print_hex("EncAccessPayload", upper_transport_pdu, upper_transport_pdu_len); 637 // store TransMIC 638 btstack_crypto_ccm_get_authentication_value(&ccm, &upper_transport_pdu[upper_transport_pdu_len]); 639 mesh_print_hex("TransMIC", &upper_transport_pdu[upper_transport_pdu_len], 4); 640 network_pdu->len += 4; 641 upper_transport_pdu_len += 4; 642 mesh_print_hex("UpperTransportPDU", upper_transport_pdu, upper_transport_pdu_len); 643 // send network pdu 644 mesh_lower_transport_send_pdu((mesh_pdu_t*) message_pdu); 645 } 646 647 static void mesh_upper_transport_send_segmented_pdu(mesh_transport_pdu_t * transport_pdu){ 648 outgoing_segmented_pdu = transport_pdu; 649 mesh_message_pdu_t * message_pdu = &outgoing_segmented_message_singleton; 650 message_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_MESSAGE; 651 message_pdu->segmented = true; 652 653 // convert mesh_transport_pdu_t into mesh_message_pdu_t 654 uint16_t message_offset = 0; 655 uint16_t bytes_current_segment = 0; 656 mesh_network_pdu_t * network_pdu = NULL; 657 while (message_offset < transport_pdu->len){ 658 if (bytes_current_segment == 0){ 659 network_pdu = mesh_network_pdu_get(); 660 btstack_assert(network_pdu != NULL); 661 btstack_linked_list_add_tail(&message_pdu->segments, (btstack_linked_item_t *) network_pdu); 662 bytes_current_segment = MESH_NETWORK_PAYLOAD_MAX; 663 } 664 uint16_t bytes_to_copy = btstack_max(bytes_current_segment, transport_pdu->len - message_offset); 665 (void) memcpy(&network_pdu->data[network_pdu->len], &transport_pdu->data[message_offset], bytes_to_copy); 666 bytes_current_segment -= bytes_to_copy; 667 network_pdu->len += bytes_to_copy; 668 message_offset += bytes_to_copy; 669 } 670 // copy meta 671 message_pdu->len = transport_pdu->len; 672 message_pdu->netkey_index = transport_pdu->netkey_index; 673 message_pdu->transmic_len = transport_pdu->transmic_len; 674 message_pdu->akf_aid_control = transport_pdu->akf_aid_control; 675 message_pdu->flags = transport_pdu->flags; 676 (void)memcpy(message_pdu->network_header, transport_pdu->network_header, 9); 677 678 mesh_lower_transport_send_pdu((mesh_pdu_t*) message_pdu); 679 } 680 681 static void mesh_upper_transport_send_segmented_access_pdu_ccm(void * arg){ 682 crypto_active = 0; 683 684 mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg; 685 mesh_print_hex("EncAccessPayload", transport_pdu->data, transport_pdu->len); 686 // store TransMIC 687 btstack_crypto_ccm_get_authentication_value(&ccm, &transport_pdu->data[transport_pdu->len]); 688 mesh_print_hex("TransMIC", &transport_pdu->data[transport_pdu->len], transport_pdu->transmic_len); 689 transport_pdu->len += transport_pdu->transmic_len; 690 mesh_print_hex("UpperTransportPDU", transport_pdu->data, transport_pdu->len); 691 mesh_upper_transport_send_segmented_pdu(transport_pdu); 692 } 693 694 static uint8_t mesh_upper_transport_setup_unsegmented_control_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, 695 const uint8_t * control_pdu_data, uint16_t control_pdu_len){ 696 697 if (control_pdu_len > 11) return 1; 698 699 const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 700 if (!network_key) return 1; 701 702 uint8_t transport_pdu_data[12]; 703 transport_pdu_data[0] = opcode; 704 (void)memcpy(&transport_pdu_data[1], control_pdu_data, control_pdu_len); 705 uint16_t transport_pdu_len = control_pdu_len + 1; 706 707 // setup network_pdu 708 mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, 1, ttl, 0, src, dest, transport_pdu_data, transport_pdu_len); 709 710 return 0; 711 } 712 713 static uint8_t mesh_upper_transport_setup_segmented_control_pdu(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, 714 const uint8_t * control_pdu_data, uint16_t control_pdu_len){ 715 716 if (control_pdu_len > 256) return 1; 717 718 const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 719 if (!network_key) return 1; 720 721 (void)memcpy(transport_pdu->data, control_pdu_data, control_pdu_len); 722 transport_pdu->len = control_pdu_len; 723 transport_pdu->netkey_index = netkey_index; 724 transport_pdu->akf_aid_control = opcode; 725 transport_pdu->transmic_len = 0; // no TransMIC for control 726 mesh_transport_set_nid_ivi(transport_pdu, network_key->nid); 727 mesh_transport_set_src(transport_pdu, src); 728 mesh_transport_set_dest(transport_pdu, dest); 729 mesh_transport_set_ctl_ttl(transport_pdu, 0x80 | ttl); 730 731 return 0; 732 } 733 734 uint8_t mesh_upper_transport_setup_control_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, 735 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, const uint8_t * control_pdu_data, uint16_t control_pdu_len){ 736 switch (pdu->pdu_type){ 737 case MESH_PDU_TYPE_NETWORK: 738 return mesh_upper_transport_setup_unsegmented_control_pdu((mesh_network_pdu_t *) pdu, netkey_index, ttl, src, dest, opcode, control_pdu_data, control_pdu_len); 739 case MESH_PDU_TYPE_TRANSPORT: 740 return mesh_upper_transport_setup_segmented_control_pdu((mesh_transport_pdu_t *) pdu, netkey_index, ttl, src, dest, opcode, control_pdu_data, control_pdu_len); 741 case MESH_PDU_TYPE_MESSAGE: 742 btstack_assert(0); 743 break; 744 default: 745 return 1; 746 } 747 } 748 749 static uint8_t mesh_upper_transport_setup_unsegmented_access_pdu_header(mesh_message_pdu_t * message_pdu, uint16_t netkey_index, 750 uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest){ 751 752 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&message_pdu->segments); 753 754 // get app or device key 755 const mesh_transport_key_t * appkey; 756 appkey = mesh_transport_key_get(appkey_index); 757 if (appkey == NULL){ 758 printf("appkey_index %x unknown\n", appkey_index); 759 return 1; 760 } 761 uint8_t akf_aid = (appkey->akf << 6) | appkey->aid; 762 763 // lookup network by netkey_index 764 const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 765 if (!network_key) return 1; 766 767 message_pdu->appkey_index = appkey_index; 768 769 network_pdu->data[9] = akf_aid; 770 // setup network_pdu 771 mesh_network_setup_pdu_header(network_pdu, netkey_index, network_key->nid, 0, ttl, 0, src, dest); 772 return 0; 773 } 774 775 static uint8_t mesh_upper_transport_setup_unsegmented_access_pdu(mesh_message_pdu_t * message_pdu, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, 776 const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 777 778 int status = mesh_upper_transport_setup_unsegmented_access_pdu_header(message_pdu, netkey_index, appkey_index, ttl, src, dest); 779 if (status) return status; 780 781 // store in transport pdu 782 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&message_pdu->segments); 783 (void)memcpy(&network_pdu->data[10], access_pdu_data, access_pdu_len); 784 network_pdu->len = 10 + access_pdu_len; 785 return 0; 786 } 787 788 static uint8_t mesh_upper_transport_setup_segmented_access_pdu_header(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index, 789 uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){ 790 791 // get app or device key 792 const mesh_transport_key_t *appkey; 793 appkey = mesh_transport_key_get(appkey_index); 794 if (appkey == NULL) { 795 printf("[!] Upper transport, setup segmented Access PDU - appkey_index %x unknown\n", appkey_index); 796 return 1; 797 } 798 uint8_t akf_aid = (appkey->akf << 6) | appkey->aid; 799 800 // lookup network by netkey_index 801 const mesh_network_key_t *network_key = mesh_network_key_list_get(netkey_index); 802 if (!network_key) return 1; 803 if (network_key == NULL) { 804 printf("[!] Upper transport, setup segmented Access PDU - netkey_index %x unknown\n", appkey_index); 805 return 1; 806 } 807 808 const uint8_t trans_mic_len = szmic ? 8 : 4; 809 810 // store in transport pdu 811 transport_pdu->transmic_len = trans_mic_len; 812 transport_pdu->netkey_index = netkey_index; 813 transport_pdu->appkey_index = appkey_index; 814 transport_pdu->akf_aid_control = akf_aid; 815 mesh_transport_set_nid_ivi(transport_pdu, network_key->nid | ((mesh_get_iv_index_for_tx() & 1) << 7)); 816 mesh_transport_set_src(transport_pdu, src); 817 mesh_transport_set_dest(transport_pdu, dest); 818 mesh_transport_set_ctl_ttl(transport_pdu, ttl); 819 return 0; 820 } 821 822 823 static uint8_t mesh_upper_transport_setup_segmented_access_pdu(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, 824 uint8_t szmic, const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 825 int status = mesh_upper_transport_setup_segmented_access_pdu_header(transport_pdu, netkey_index, appkey_index, ttl, src, dest, szmic); 826 if (status) return status; 827 828 // store in transport pdu 829 (void)memcpy(transport_pdu->data, access_pdu_data, access_pdu_len); 830 transport_pdu->len = access_pdu_len; 831 return 0; 832 } 833 uint8_t mesh_upper_transport_setup_access_pdu_header(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index, 834 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){ 835 switch (pdu->pdu_type){ 836 case MESH_PDU_TYPE_NETWORK: 837 btstack_assert(0); 838 case MESH_PDU_TYPE_TRANSPORT: 839 return mesh_upper_transport_setup_segmented_access_pdu_header((mesh_transport_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic); 840 case MESH_PDU_TYPE_MESSAGE: 841 return mesh_upper_transport_setup_unsegmented_access_pdu_header((mesh_message_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest); 842 default: 843 return 1; 844 } 845 } 846 847 uint8_t mesh_upper_transport_setup_access_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index, 848 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic, 849 const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 850 switch (pdu->pdu_type){ 851 case MESH_PDU_TYPE_NETWORK: 852 btstack_assert(0); 853 break; 854 case MESH_PDU_TYPE_MESSAGE: 855 return mesh_upper_transport_setup_unsegmented_access_pdu((mesh_message_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, access_pdu_data, access_pdu_len); 856 case MESH_PDU_TYPE_TRANSPORT: 857 return mesh_upper_transport_setup_segmented_access_pdu((mesh_transport_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic, access_pdu_data, access_pdu_len); 858 default: 859 return 1; 860 } 861 } 862 863 static void mesh_upper_transport_send_unsegmented_access_pdu_digest(void * arg){ 864 mesh_message_pdu_t * message_pdu = (mesh_message_pdu_t *) arg; 865 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&message_pdu->segments); 866 uint8_t * access_pdu_data = mesh_network_pdu_data(network_pdu) + 1; 867 uint16_t access_pdu_len = mesh_network_pdu_len(network_pdu) - 1; 868 btstack_crypto_ccm_encrypt_block(&ccm, access_pdu_len, access_pdu_data, access_pdu_data, &mesh_upper_transport_send_unsegmented_access_pdu_ccm, message_pdu); 869 } 870 871 static mesh_transport_key_t * mesh_upper_transport_get_outgoing_appkey(uint16_t netkey_index, uint16_t appkey_index){ 872 // Device Key is fixed 873 if (appkey_index == MESH_DEVICE_KEY_INDEX) { 874 return mesh_transport_key_get(appkey_index); 875 } 876 877 // Get key refresh state from subnet 878 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 879 if (subnet == NULL) return NULL; 880 881 // identify old and new app keys for given appkey_index 882 mesh_transport_key_t * old_key = NULL; 883 mesh_transport_key_t * new_key = NULL; 884 mesh_transport_key_iterator_t it; 885 mesh_transport_key_iterator_init(&it, netkey_index); 886 while (mesh_transport_key_iterator_has_more(&it)){ 887 mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it); 888 if (transport_key->appkey_index != appkey_index) continue; 889 if (transport_key->old_key == 0) { 890 new_key = transport_key; 891 } else { 892 old_key = transport_key; 893 } 894 } 895 896 // if no key is marked as old, just use the current one 897 if (old_key == NULL) return new_key; 898 899 // use new key if it exists in phase two 900 if ((subnet->key_refresh == MESH_KEY_REFRESH_SECOND_PHASE) && (new_key != NULL)){ 901 return new_key; 902 } else { 903 return old_key; 904 } 905 } 906 907 static void mesh_upper_transport_send_unsegmented_access_pdu(mesh_message_pdu_t * message_pdu){ 908 909 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&message_pdu->segments); 910 911 // if dst is virtual address, lookup label uuid and hash 912 uint16_t aad_len = 0; 913 mesh_virtual_address_t * virtual_address = NULL; 914 uint16_t dst = mesh_network_dst(network_pdu); 915 if (mesh_network_address_virtual(dst)){ 916 virtual_address = mesh_virtual_address_for_pseudo_dst(dst); 917 if (!virtual_address){ 918 printf("No virtual address register for pseudo dst %4x\n", dst); 919 btstack_memory_mesh_network_pdu_free(network_pdu); 920 return; 921 } 922 aad_len = 16; 923 big_endian_store_16(network_pdu->data, 7, virtual_address->hash); 924 } 925 926 // reserve slot 927 mesh_lower_transport_reserve_slot(); 928 929 // Nonce for Access Payload based on Network Sequence number: needs to be fixed now and lower layers need to send packet in right order 930 uint32_t seq = mesh_sequence_number_next(); 931 mesh_network_pdu_set_seq(network_pdu, seq); 932 933 // Dump PDU 934 printf("[+] Upper transport, send unsegmented Access PDU - dest %04x, seq %06x\n", dst, mesh_network_seq(network_pdu)); 935 mesh_print_hex("Access Payload", &network_pdu->data[10], network_pdu->len - 10); 936 937 // setup nonce 938 uint16_t appkey_index = message_pdu->appkey_index; 939 if (appkey_index == MESH_DEVICE_KEY_INDEX){ 940 transport_unsegmented_setup_device_nonce(application_nonce, network_pdu); 941 } else { 942 transport_unsegmented_setup_application_nonce(application_nonce, network_pdu); 943 } 944 945 // get app or device key 946 const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(network_pdu->netkey_index, appkey_index); 947 mesh_print_hex("AppOrDevKey", appkey->key, 16); 948 949 // encrypt ccm 950 uint8_t trans_mic_len = 4; 951 uint16_t access_pdu_len = mesh_network_pdu_len(network_pdu) - 1; 952 crypto_active = 1; 953 954 btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, trans_mic_len); 955 if (virtual_address){ 956 mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16); 957 btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_unsegmented_access_pdu_digest, message_pdu); 958 } else { 959 mesh_upper_transport_send_unsegmented_access_pdu_digest(message_pdu); 960 } 961 } 962 963 static void mesh_upper_transport_send_segmented_access_pdu_digest(void *arg){ 964 mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg; 965 uint16_t access_pdu_len = transport_pdu->len; 966 uint8_t * access_pdu_data = transport_pdu->data; 967 btstack_crypto_ccm_encrypt_block(&ccm, access_pdu_len,access_pdu_data, access_pdu_data, &mesh_upper_transport_send_segmented_access_pdu_ccm, transport_pdu); 968 } 969 970 static void mesh_upper_transport_send_segmented_access_pdu(mesh_transport_pdu_t * transport_pdu){ 971 972 // if dst is virtual address, lookup label uuid and hash 973 uint16_t aad_len = 0; 974 mesh_virtual_address_t * virtual_address = NULL; 975 uint16_t dst = mesh_transport_dst(transport_pdu); 976 if (mesh_network_address_virtual(dst)){ 977 virtual_address = mesh_virtual_address_for_pseudo_dst(dst); 978 if (!virtual_address){ 979 printf("No virtual address register for pseudo dst %4x\n", dst); 980 btstack_memory_mesh_transport_pdu_free(transport_pdu); 981 return; 982 } 983 // printf("Using hash %4x with LabelUUID: ", virtual_address->hash); 984 // printf_hexdump(virtual_address->label_uuid, 16); 985 aad_len = 16; 986 big_endian_store_16(transport_pdu->network_header, 7, virtual_address->hash); 987 } 988 989 // get app or device key 990 uint16_t appkey_index = transport_pdu->appkey_index; 991 const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(transport_pdu->netkey_index, appkey_index); 992 if (appkey == NULL){ 993 printf("AppKey %04x not found, drop message\n", appkey_index); 994 btstack_memory_mesh_transport_pdu_free(transport_pdu); 995 return; 996 } 997 998 // reserve slot 999 mesh_lower_transport_reserve_slot(); 1000 1001 // reserve one sequence number, which is also used to encrypt access payload 1002 uint32_t seq = mesh_sequence_number_next(); 1003 transport_pdu->flags |= MESH_TRANSPORT_FLAG_SEQ_RESERVED; 1004 mesh_transport_set_seq(transport_pdu, seq); 1005 1006 // Dump PDU 1007 printf("[+] Upper transport, send segmented Access PDU - dest %04x, seq %06x\n", dst, mesh_transport_seq(transport_pdu)); 1008 mesh_print_hex("Access Payload", transport_pdu->data, transport_pdu->len); 1009 1010 // setup nonce - uses dst, so after pseudo address translation 1011 if (appkey_index == MESH_DEVICE_KEY_INDEX){ 1012 transport_segmented_setup_device_nonce(application_nonce, (mesh_pdu_t *) transport_pdu); 1013 } else { 1014 transport_segmented_setup_application_nonce(application_nonce, (mesh_pdu_t *) transport_pdu); 1015 } 1016 1017 // Dump key 1018 mesh_print_hex("AppOrDevKey", appkey->key, 16); 1019 1020 // encrypt ccm 1021 uint8_t transmic_len = transport_pdu->transmic_len; 1022 uint16_t access_pdu_len = transport_pdu->len; 1023 crypto_active = 1; 1024 btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, transmic_len); 1025 if (virtual_address){ 1026 mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16); 1027 btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_segmented_access_pdu_digest, transport_pdu); 1028 } else { 1029 mesh_upper_transport_send_segmented_access_pdu_digest(transport_pdu); 1030 } 1031 } 1032 1033 static void mesh_upper_transport_send_unsegmented_control_pdu(mesh_network_pdu_t * network_pdu){ 1034 // reserve slot 1035 mesh_lower_transport_reserve_slot(); 1036 // reserve sequence number 1037 uint32_t seq = mesh_sequence_number_next(); 1038 mesh_network_pdu_set_seq(network_pdu, seq); 1039 // Dump PDU 1040 uint8_t opcode = network_pdu->data[9]; 1041 printf("[+] Upper transport, send unsegmented Control PDU %p - seq %06x opcode %02x\n", network_pdu, seq, opcode); 1042 mesh_print_hex("Access Payload", &network_pdu->data[10], network_pdu->len - 10); 1043 // send 1044 mesh_lower_transport_send_pdu((mesh_pdu_t *) network_pdu); 1045 } 1046 1047 static void mesh_upper_transport_send_segmented_control_pdu(mesh_transport_pdu_t * transport_pdu){ 1048 // reserve slot 1049 mesh_lower_transport_reserve_slot(); 1050 // reserve sequence number 1051 uint32_t seq = mesh_sequence_number_next(); 1052 transport_pdu->flags |= MESH_TRANSPORT_FLAG_SEQ_RESERVED; 1053 mesh_transport_set_seq(transport_pdu, seq); 1054 // Dump PDU 1055 uint8_t opcode = transport_pdu->data[0]; 1056 printf("[+] Upper transport, send segmented Control PDU %p - seq %06x opcode %02x\n", transport_pdu, seq, opcode); 1057 mesh_print_hex("Access Payload", &transport_pdu->data[1], transport_pdu->len - 1); 1058 // send 1059 mesh_upper_transport_send_segmented_pdu(transport_pdu); 1060 } 1061 1062 static void mesh_upper_transport_run(void){ 1063 1064 while(!btstack_linked_list_empty(&upper_transport_incoming)){ 1065 1066 if (crypto_active) return; 1067 1068 // peek at next message 1069 mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_get_first_item(&upper_transport_incoming); 1070 mesh_network_pdu_t * network_pdu; 1071 mesh_transport_pdu_t * transport_pdu; 1072 mesh_message_pdu_t * message_pdu; 1073 mesh_unsegmented_incoming_pdu_t * unsegmented_pdu; 1074 switch (pdu->pdu_type){ 1075 case MESH_PDU_TYPE_UNSEGMENTED_INCOMING: 1076 unsegmented_pdu = (mesh_unsegmented_incoming_pdu_t *) pdu; 1077 network_pdu = unsegmented_pdu->segment; 1078 btstack_assert(network_pdu != NULL); 1079 incoming_unsegmented_pdu_raw = unsegmented_pdu; 1080 // control? 1081 if (mesh_network_control(network_pdu)) { 1082 (void) btstack_linked_list_pop(&upper_transport_incoming); 1083 mesh_upper_unsegmented_control_message_received(unsegmented_pdu); 1084 break; 1085 } else { 1086 incoming_network_pdu_decoded = mesh_network_pdu_get(); 1087 if (!incoming_network_pdu_decoded) return; 1088 1089 (void) btstack_linked_list_pop(&upper_transport_incoming); 1090 1091 // use pre-allocated message pdu 1092 incoming_message_pdu_singleton.pdu_header.pdu_type = MESH_PDU_TYPE_MESSAGE; 1093 btstack_linked_list_add(&incoming_message_pdu_singleton.segments, (btstack_linked_item_t *) incoming_network_pdu_decoded); 1094 mesh_upper_transport_process_unsegmented_access_message(); 1095 } 1096 break; 1097 case MESH_PDU_TYPE_MESSAGE: 1098 message_pdu = (mesh_message_pdu_t *) pdu; 1099 uint8_t ctl = mesh_message_ctl(message_pdu); 1100 if (ctl){ 1101 printf("Ignoring Segmented Control Message\n"); 1102 (void) btstack_linked_list_pop(&upper_transport_incoming); 1103 mesh_lower_transport_message_processed_by_higher_layer(pdu); 1104 } else { 1105 1106 incoming_access_pdu_encrypted = &incoming_access_pdu_encrypted_singleton; 1107 incoming_access_pdu_encrypted->pdu_header.pdu_type = MESH_PDU_TYPE_ACCESS; 1108 1109 // flatten segmented message into mesh_transport_pdu_t 1110 1111 // assemble payload 1112 while (message_pdu->segments){ 1113 mesh_network_pdu_t * segment = (mesh_network_pdu_t *) btstack_linked_list_pop(&message_pdu->segments); 1114 // get segment n 1115 uint8_t * lower_transport_pdu = mesh_network_pdu_data(segment); 1116 uint8_t seg_o = ( big_endian_read_16(lower_transport_pdu, 2) >> 5) & 0x001f; 1117 uint8_t * segment_data = &lower_transport_pdu[4]; 1118 (void)memcpy(&incoming_access_pdu_encrypted->data[seg_o * 12], segment_data, 12); 1119 } 1120 1121 // copy meta data into encrypted pdu buffer 1122 incoming_access_pdu_encrypted->len = message_pdu->len; 1123 incoming_access_pdu_encrypted->netkey_index = message_pdu->netkey_index; 1124 incoming_access_pdu_encrypted->transmic_len = message_pdu->transmic_len; 1125 incoming_access_pdu_encrypted->akf_aid_control = message_pdu->akf_aid_control; 1126 (void)memcpy(incoming_access_pdu_encrypted->network_header, message_pdu->network_header, 9); 1127 1128 mesh_print_hex("Assembled payload", incoming_access_pdu_encrypted->data, incoming_access_pdu_encrypted->len); 1129 1130 // copy meta data into decrypted pdu buffer 1131 incoming_access_pdu_decrypted = &incoming_access_pdu_decrypted_singleton; 1132 incoming_access_pdu_decrypted->pdu_header.pdu_type = MESH_PDU_TYPE_ACCESS; 1133 incoming_access_pdu_decrypted->len = message_pdu->len; 1134 incoming_access_pdu_decrypted->netkey_index = message_pdu->netkey_index; 1135 incoming_access_pdu_decrypted->transmic_len = message_pdu->transmic_len; 1136 incoming_access_pdu_decrypted->akf_aid_control = message_pdu->akf_aid_control; 1137 (void)memcpy(incoming_access_pdu_decrypted->network_header, message_pdu->network_header, 9); 1138 1139 // free mesh message 1140 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *)message_pdu); 1141 1142 // get encoded transport pdu and start processing 1143 (void) btstack_linked_list_pop(&upper_transport_incoming); 1144 mesh_upper_transport_process_message(); 1145 } 1146 break; 1147 default: 1148 btstack_assert(0); 1149 break; 1150 } 1151 } 1152 1153 while (!btstack_linked_list_empty(&upper_transport_outgoing)){ 1154 1155 if (crypto_active) break; 1156 1157 if (outgoing_segmented_pdu != NULL) break; 1158 1159 mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_get_first_item(&upper_transport_outgoing); 1160 if (mesh_lower_transport_can_send_to_dest(mesh_pdu_dst(pdu)) == 0) break; 1161 1162 (void) btstack_linked_list_pop(&upper_transport_outgoing); 1163 1164 if (mesh_pdu_ctl(pdu)){ 1165 switch (pdu->pdu_type){ 1166 case MESH_PDU_TYPE_NETWORK: 1167 mesh_upper_transport_send_unsegmented_control_pdu((mesh_network_pdu_t *) pdu); 1168 break; 1169 case MESH_PDU_TYPE_MESSAGE: 1170 btstack_assert(0); 1171 break; 1172 case MESH_PDU_TYPE_TRANSPORT: 1173 mesh_upper_transport_send_segmented_control_pdu((mesh_transport_pdu_t *) pdu); 1174 break; 1175 default: 1176 break; 1177 } 1178 } else { 1179 switch (pdu->pdu_type){ 1180 case MESH_PDU_TYPE_NETWORK: 1181 btstack_assert(0); 1182 break; 1183 case MESH_PDU_TYPE_MESSAGE: 1184 mesh_upper_transport_send_unsegmented_access_pdu((mesh_message_pdu_t *) pdu); 1185 break; 1186 case MESH_PDU_TYPE_TRANSPORT: 1187 mesh_upper_transport_send_segmented_access_pdu((mesh_transport_pdu_t *) pdu); 1188 break; 1189 default: 1190 break; 1191 } 1192 } 1193 } 1194 } 1195 1196 void mesh_upper_transport_pdu_free(mesh_pdu_t * pdu){ 1197 mesh_network_pdu_t * network_pdu; 1198 mesh_transport_pdu_t * transport_pdu; 1199 mesh_message_pdu_t * message_pdu; 1200 switch (pdu->pdu_type) { 1201 case MESH_PDU_TYPE_NETWORK: 1202 network_pdu = (mesh_network_pdu_t *) pdu; 1203 mesh_network_pdu_free(network_pdu); 1204 break; 1205 case MESH_PDU_TYPE_TRANSPORT: 1206 transport_pdu = (mesh_transport_pdu_t *) pdu; 1207 mesh_transport_pdu_free(transport_pdu); 1208 break; 1209 case MESH_PDU_TYPE_MESSAGE: 1210 message_pdu = (mesh_message_pdu_t *) pdu; 1211 mesh_message_pdu_free(message_pdu); 1212 default: 1213 break; 1214 } 1215 } 1216 1217 void mesh_upper_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){ 1218 crypto_active = 0; 1219 switch (pdu->pdu_type){ 1220 case MESH_PDU_TYPE_ACCESS: 1221 mesh_upper_transport_process_segmented_access_message_done((mesh_access_pdu_t *) pdu); 1222 break; 1223 case MESH_PDU_TYPE_MESSAGE: 1224 mesh_upper_transport_process_message_done((mesh_message_pdu_t *) pdu); 1225 break; 1226 case MESH_PDU_TYPE_UNSEGMENTED_INCOMING: 1227 mesh_upper_transport_process_unsegmented_message_done(pdu); 1228 break; 1229 default: 1230 btstack_assert(0); 1231 break; 1232 } 1233 } 1234 1235 void mesh_upper_transport_register_access_message_handler(void (*callback)(mesh_pdu_t *pdu)){ 1236 mesh_access_message_handler = callback; 1237 } 1238 1239 void mesh_upper_transport_register_control_message_handler(void (*callback)(mesh_pdu_t *pdu)){ 1240 mesh_control_message_handler = callback; 1241 } 1242 1243 void mesh_upper_transport_set_higher_layer_handler(void (*pdu_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu)){ 1244 higher_layer_handler = pdu_handler; 1245 } 1246 1247 void mesh_upper_transport_init(){ 1248 mesh_lower_transport_set_higher_layer_handler(&mesh_upper_transport_pdu_handler); 1249 } 1250