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