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_transport.c" 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include "mesh/beacon.h" 44 #include "mesh/mesh_lower_transport.h" 45 #include "mesh/mesh_upper_transport.h" 46 #include "btstack_util.h" 47 #include "btstack_memory.h" 48 #include "mesh_peer.h" 49 #include "mesh_keys.h" 50 #include "mesh_virtual_addresses.h" 51 #include "mesh_iv_index_seq_number.h" 52 53 static void (*higher_layer_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu); 54 55 static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len){ 56 printf("%-20s ", name); 57 printf_hexdump(data, len); 58 } 59 // static void mesh_print_x(const char * name, uint32_t value){ 60 // printf("%20s: 0x%x", name, (int) value); 61 // } 62 63 64 // combined key x address iterator for upper transport decryption 65 66 typedef struct { 67 // state 68 mesh_transport_key_iterator_t key_it; 69 mesh_virtual_address_iterator_t address_it; 70 // elements 71 const mesh_transport_key_t * key; 72 const mesh_virtual_address_t * address; 73 // address - might be virtual 74 uint16_t dst; 75 // key info 76 } mesh_transport_key_and_virtual_address_iterator_t; 77 78 static void mesh_transport_key_and_virtual_address_iterator_init(mesh_transport_key_and_virtual_address_iterator_t *it, 79 uint16_t dst, uint16_t netkey_index, uint8_t akf, 80 uint8_t aid) { 81 printf("KEY_INIT: dst %04x, akf %x, aid %x\n", dst, akf, aid); 82 // config 83 it->dst = dst; 84 // init elements 85 it->key = NULL; 86 it->address = NULL; 87 // init element iterators 88 mesh_transport_key_aid_iterator_init(&it->key_it, netkey_index, akf, aid); 89 // init address iterator 90 if (mesh_network_address_virtual(it->dst)){ 91 mesh_virtual_address_iterator_init(&it->address_it, dst); 92 // get first key 93 if (mesh_transport_key_aid_iterator_has_more(&it->key_it)) { 94 it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it); 95 } 96 } 97 } 98 99 // cartesian product: keys x addressses 100 static int mesh_transport_key_and_virtual_address_iterator_has_more(mesh_transport_key_and_virtual_address_iterator_t * it){ 101 if (mesh_network_address_virtual(it->dst)) { 102 // find next valid entry 103 while (1){ 104 if (mesh_virtual_address_iterator_has_more(&it->address_it)) return 1; 105 if (!mesh_transport_key_aid_iterator_has_more(&it->key_it)) return 0; 106 // get next key 107 it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it); 108 mesh_virtual_address_iterator_init(&it->address_it, it->dst); 109 } 110 } else { 111 return mesh_transport_key_aid_iterator_has_more(&it->key_it); 112 } 113 } 114 115 static void mesh_transport_key_and_virtual_address_iterator_next(mesh_transport_key_and_virtual_address_iterator_t * it){ 116 if (mesh_network_address_virtual(it->dst)) { 117 it->address = mesh_virtual_address_iterator_get_next(&it->address_it); 118 } else { 119 it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it); 120 } 121 } 122 123 // UPPER TRANSPORT 124 125 // stub lower transport 126 127 static void mesh_upper_transport_validate_unsegmented_message(mesh_network_pdu_t * network_pdu); 128 static void mesh_upper_transport_validate_segmented_message(mesh_transport_pdu_t * transport_pdu); 129 130 static void mesh_transport_run(void); 131 132 static int crypto_active; 133 static mesh_network_pdu_t * network_pdu_in_validation; 134 static mesh_transport_pdu_t * transport_pdu_in_validation; 135 static uint8_t application_nonce[13]; 136 static btstack_crypto_ccm_t ccm; 137 static mesh_transport_key_and_virtual_address_iterator_t mesh_transport_key_it; 138 139 // upper transport callbacks - in access layer 140 static void (*mesh_access_message_handler)(mesh_pdu_t * pdu); 141 static void (*mesh_control_message_handler)(mesh_pdu_t * pdu); 142 143 // unsegmented (network) and segmented (transport) control and access messages 144 static btstack_linked_list_t upper_transport_incoming; 145 146 147 void mesh_upper_unsegmented_control_message_received(mesh_network_pdu_t * network_pdu){ 148 uint8_t * lower_transport_pdu = mesh_network_pdu_data(network_pdu); 149 uint8_t opcode = lower_transport_pdu[0]; 150 if (mesh_control_message_handler){ 151 mesh_control_message_handler((mesh_pdu_t*) network_pdu); 152 } else { 153 printf("[!] Unhandled Control message with opcode %02x\n", opcode); 154 // done 155 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) network_pdu); 156 } 157 } 158 159 static void mesh_upper_transport_process_unsegmented_message_done(mesh_network_pdu_t *network_pdu){ 160 crypto_active = 0; 161 if (mesh_network_control(network_pdu)) { 162 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) network_pdu); 163 } else { 164 mesh_network_pdu_free(network_pdu); 165 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) network_pdu_in_validation); 166 network_pdu_in_validation = NULL; 167 } 168 mesh_transport_run(); 169 } 170 171 static void mesh_upper_transport_process_segmented_message_done(mesh_transport_pdu_t *transport_pdu){ 172 crypto_active = 0; 173 if (mesh_transport_ctl(transport_pdu)) { 174 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *)transport_pdu); 175 } else { 176 mesh_transport_pdu_free(transport_pdu); 177 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *)transport_pdu_in_validation); 178 transport_pdu_in_validation = NULL; 179 } 180 mesh_transport_run(); 181 } 182 183 static uint32_t iv_index_for_ivi_nid(uint8_t ivi_nid){ 184 // get IV Index and IVI 185 uint32_t iv_index = mesh_get_iv_index(); 186 int ivi = ivi_nid >> 7; 187 188 // if least significant bit differs, use previous IV Index 189 if ((iv_index & 1 ) ^ ivi){ 190 iv_index--; 191 } 192 return iv_index; 193 } 194 195 static void transport_unsegmented_setup_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){ 196 nonce[1] = 0x00; // SZMIC if a Segmented Access message or 0 for all other message formats 197 memcpy(&nonce[2], &network_pdu->data[2], 7); 198 big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(network_pdu->data[0])); 199 } 200 201 static void transport_segmented_setup_nonce(uint8_t * nonce, const mesh_transport_pdu_t * transport_pdu){ 202 nonce[1] = transport_pdu->transmic_len == 8 ? 0x80 : 0x00; 203 memcpy(&nonce[2], &transport_pdu->network_header[2], 7); 204 big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(transport_pdu->network_header[0])); 205 } 206 207 static void transport_unsegmented_setup_application_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){ 208 nonce[0] = 0x01; 209 transport_unsegmented_setup_nonce(nonce, network_pdu); 210 mesh_print_hex("AppNonce", nonce, 13); 211 } 212 213 static void transport_unsegmented_setup_device_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){ 214 nonce[0] = 0x02; 215 transport_unsegmented_setup_nonce(nonce, network_pdu); 216 mesh_print_hex("DeviceNonce", nonce, 13); 217 } 218 219 static void transport_segmented_setup_application_nonce(uint8_t * nonce, const mesh_transport_pdu_t * transport_pdu){ 220 nonce[0] = 0x01; 221 transport_segmented_setup_nonce(nonce, transport_pdu); 222 mesh_print_hex("AppNonce", nonce, 13); 223 } 224 225 static void transport_segmented_setup_device_nonce(uint8_t * nonce, const mesh_transport_pdu_t * transport_pdu){ 226 nonce[0] = 0x02; 227 transport_segmented_setup_nonce(nonce, transport_pdu); 228 mesh_print_hex("DeviceNonce", nonce, 13); 229 } 230 231 static void mesh_upper_transport_validate_unsegmented_message_ccm(void * arg){ 232 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg; 233 234 uint8_t * lower_transport_pdu = mesh_network_pdu_data(network_pdu); 235 uint8_t trans_mic_len = 4; 236 237 // store TransMIC 238 uint8_t trans_mic[8]; 239 btstack_crypto_ccm_get_authentication_value(&ccm, trans_mic); 240 mesh_print_hex("TransMIC", trans_mic, trans_mic_len); 241 242 uint8_t * upper_transport_pdu = mesh_network_pdu_data(network_pdu) + 1; 243 uint8_t upper_transport_pdu_len = mesh_network_pdu_len(network_pdu) - 1; 244 245 mesh_print_hex("Decryted PDU", upper_transport_pdu, upper_transport_pdu_len - trans_mic_len); 246 247 if (memcmp(trans_mic, &upper_transport_pdu[upper_transport_pdu_len - trans_mic_len], trans_mic_len) == 0){ 248 printf("TransMIC matches\n"); 249 250 // remove TransMIC from payload 251 network_pdu->len -= trans_mic_len; 252 253 // if virtual address, update dst to pseudo_dst 254 if (mesh_network_address_virtual(mesh_network_dst(network_pdu))){ 255 big_endian_store_16(network_pdu->data, 7, mesh_transport_key_it.address->pseudo_dst); 256 } 257 258 // pass to upper layer 259 if (mesh_access_message_handler){ 260 mesh_access_message_handler((mesh_pdu_t*) network_pdu); 261 } else { 262 printf("[!] Unhandled Unsegmented Access message\n"); 263 // done 264 mesh_upper_transport_process_unsegmented_message_done(network_pdu); 265 } 266 267 printf("\n"); 268 } else { 269 uint8_t afk = lower_transport_pdu[0] & 0x40; 270 if (afk){ 271 printf("TransMIC does not match, try next key\n"); 272 mesh_upper_transport_validate_unsegmented_message(network_pdu); 273 } else { 274 printf("TransMIC does not match device key, done\n"); 275 // done 276 mesh_upper_transport_process_unsegmented_message_done(network_pdu); 277 } 278 } 279 } 280 281 static void mesh_upper_transport_validate_segmented_message_ccm(void * arg){ 282 mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg; 283 284 uint8_t * upper_transport_pdu = transport_pdu->data; 285 uint8_t upper_transport_pdu_len = transport_pdu->len - transport_pdu->transmic_len; 286 287 mesh_print_hex("Decrypted PDU", upper_transport_pdu, upper_transport_pdu_len); 288 289 // store TransMIC 290 uint8_t trans_mic[8]; 291 btstack_crypto_ccm_get_authentication_value(&ccm, trans_mic); 292 mesh_print_hex("TransMIC", trans_mic, transport_pdu->transmic_len); 293 294 if (memcmp(trans_mic, &upper_transport_pdu[upper_transport_pdu_len], transport_pdu->transmic_len) == 0){ 295 printf("TransMIC matches\n"); 296 297 // remove TransMIC from payload 298 transport_pdu->len -= transport_pdu->transmic_len; 299 300 // if virtual address, update dst to pseudo_dst 301 if (mesh_network_address_virtual(mesh_transport_dst(transport_pdu))){ 302 big_endian_store_16(transport_pdu->network_header, 7, mesh_transport_key_it.address->pseudo_dst); 303 } 304 305 // pass to upper layer 306 if (mesh_access_message_handler){ 307 mesh_access_message_handler((mesh_pdu_t*) transport_pdu); 308 } else { 309 printf("[!] Unhandled Segmented Access/Control message\n"); 310 // done 311 mesh_upper_transport_process_segmented_message_done(transport_pdu); 312 } 313 314 printf("\n"); 315 316 } else { 317 uint8_t akf = transport_pdu->akf_aid & 0x40; 318 if (akf){ 319 printf("TransMIC does not match, try next key\n"); 320 mesh_upper_transport_validate_segmented_message(transport_pdu); 321 } else { 322 printf("TransMIC does not match device key, done\n"); 323 // done 324 mesh_upper_transport_process_segmented_message_done(transport_pdu); 325 } 326 } 327 } 328 329 void mesh_upper_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){ 330 crypto_active = 0; 331 switch (pdu->pdu_type){ 332 case MESH_PDU_TYPE_NETWORK: 333 mesh_upper_transport_process_unsegmented_message_done((mesh_network_pdu_t *) pdu); 334 break; 335 case MESH_PDU_TYPE_TRANSPORT: 336 mesh_upper_transport_process_segmented_message_done((mesh_transport_pdu_t *) pdu); 337 break; 338 default: 339 break; 340 } 341 } 342 343 static void mesh_upper_transport_validate_segmented_message_digest(void * arg){ 344 mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t*) arg; 345 uint8_t upper_transport_pdu_len = transport_pdu_in_validation->len - transport_pdu_in_validation->transmic_len; 346 uint8_t * upper_transport_pdu_data_in = transport_pdu_in_validation->data; 347 uint8_t * upper_transport_pdu_data_out = transport_pdu->data; 348 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, transport_pdu); 349 } 350 351 static void mesh_upper_transport_validate_unsegmented_message_digest(void * arg){ 352 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg; 353 uint8_t trans_mic_len = 4; 354 uint8_t lower_transport_pdu_len = network_pdu_in_validation->len - 9; 355 uint8_t * upper_transport_pdu_data_in = &network_pdu_in_validation->data[10]; 356 uint8_t * upper_transport_pdu_data_out = &network_pdu->data[10]; 357 uint8_t upper_transport_pdu_len = lower_transport_pdu_len - 1 - trans_mic_len; 358 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, network_pdu); 359 } 360 361 static void mesh_upper_transport_validate_unsegmented_message(mesh_network_pdu_t * network_pdu){ 362 363 if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){ 364 printf("No valid transport key found\n"); 365 mesh_upper_transport_process_unsegmented_message_done(network_pdu); 366 return; 367 } 368 mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it); 369 const mesh_transport_key_t * message_key = mesh_transport_key_it.key; 370 371 if (message_key->akf){ 372 transport_unsegmented_setup_application_nonce(application_nonce, network_pdu_in_validation); 373 } else { 374 transport_unsegmented_setup_device_nonce(application_nonce, network_pdu_in_validation); 375 } 376 377 // store application / device key index 378 mesh_print_hex("AppOrDevKey", message_key->key, 16); 379 network_pdu->appkey_index = message_key->appkey_index; 380 381 // unsegmented message have TransMIC of 32 bit 382 uint8_t trans_mic_len = 4; 383 printf("Unsegmented Access message with TransMIC len 4\n"); 384 385 uint8_t lower_transport_pdu_len = network_pdu_in_validation->len - 9; 386 uint8_t * upper_transport_pdu_data = &network_pdu_in_validation->data[10]; 387 uint8_t upper_transport_pdu_len = lower_transport_pdu_len - 1 - trans_mic_len; 388 389 mesh_print_hex("EncAccessPayload", upper_transport_pdu_data, upper_transport_pdu_len); 390 391 // decrypt ccm 392 crypto_active = 1; 393 uint16_t aad_len = 0; 394 if (mesh_network_address_virtual(mesh_network_dst(network_pdu))){ 395 aad_len = 16; 396 } 397 btstack_crypto_ccm_init(&ccm, message_key->key, application_nonce, upper_transport_pdu_len, aad_len, trans_mic_len); 398 if (aad_len){ 399 btstack_crypto_ccm_digest(&ccm, (uint8_t*) mesh_transport_key_it.address->label_uuid, aad_len, &mesh_upper_transport_validate_unsegmented_message_digest, network_pdu); 400 } else { 401 mesh_upper_transport_validate_unsegmented_message_digest(network_pdu); 402 } 403 } 404 405 static void mesh_upper_transport_validate_segmented_message(mesh_transport_pdu_t * transport_pdu){ 406 uint8_t * upper_transport_pdu_data = transport_pdu->data; 407 uint8_t upper_transport_pdu_len = transport_pdu->len - transport_pdu->transmic_len; 408 409 if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){ 410 printf("No valid transport key found\n"); 411 mesh_upper_transport_process_segmented_message_done(transport_pdu); 412 return; 413 } 414 mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it); 415 const mesh_transport_key_t * message_key = mesh_transport_key_it.key; 416 417 if (message_key->akf){ 418 transport_segmented_setup_application_nonce(application_nonce, transport_pdu_in_validation); 419 } else { 420 transport_segmented_setup_device_nonce(application_nonce, transport_pdu_in_validation); 421 } 422 423 // store application / device key index 424 mesh_print_hex("AppOrDevKey", message_key->key, 16); 425 transport_pdu->appkey_index = message_key->appkey_index; 426 427 mesh_print_hex("EncAccessPayload", upper_transport_pdu_data, upper_transport_pdu_len); 428 429 // decrypt ccm 430 crypto_active = 1; 431 uint16_t aad_len = 0; 432 if (mesh_network_address_virtual(mesh_transport_dst(transport_pdu))){ 433 aad_len = 16; 434 } 435 btstack_crypto_ccm_init(&ccm, message_key->key, application_nonce, upper_transport_pdu_len, aad_len, transport_pdu->transmic_len); 436 437 if (aad_len){ 438 btstack_crypto_ccm_digest(&ccm, (uint8_t *) mesh_transport_key_it.address->label_uuid, aad_len, &mesh_upper_transport_validate_segmented_message_digest, transport_pdu); 439 } else { 440 mesh_upper_transport_validate_segmented_message_digest(transport_pdu); 441 } 442 } 443 444 static void mesh_upper_transport_process_unsegmented_access_message(mesh_network_pdu_t *network_pdu){ 445 // copy original pdu 446 network_pdu->len = network_pdu_in_validation->len; 447 memcpy(network_pdu->data, network_pdu_in_validation->data, network_pdu->len); 448 449 // 450 uint8_t * lower_transport_pdu = &network_pdu_in_validation->data[9]; 451 uint8_t lower_transport_pdu_len = network_pdu_in_validation->len - 9; 452 453 mesh_print_hex("Lower Transport network pdu", &network_pdu_in_validation->data[9], lower_transport_pdu_len); 454 455 uint8_t aid = lower_transport_pdu[0] & 0x3f; 456 uint8_t akf = (lower_transport_pdu[0] & 0x40) >> 6; 457 printf("AKF: %u\n", akf); 458 printf("AID: %02x\n", aid); 459 460 mesh_transport_key_and_virtual_address_iterator_init(&mesh_transport_key_it, mesh_network_dst(network_pdu), 461 network_pdu->netkey_index, akf, aid); 462 mesh_upper_transport_validate_unsegmented_message(network_pdu); 463 } 464 465 static void mesh_upper_transport_process_message(mesh_transport_pdu_t * transport_pdu){ 466 // copy original pdu 467 transport_pdu->len = transport_pdu_in_validation->len; 468 memcpy(transport_pdu, transport_pdu_in_validation, sizeof(mesh_transport_pdu_t)); 469 470 // 471 uint8_t * upper_transport_pdu = transport_pdu->data; 472 uint8_t upper_transport_pdu_len = transport_pdu->len - transport_pdu->transmic_len; 473 mesh_print_hex("Upper Transport pdu", upper_transport_pdu, upper_transport_pdu_len); 474 475 uint8_t aid = transport_pdu->akf_aid & 0x3f; 476 uint8_t akf = (transport_pdu->akf_aid & 0x40) >> 6; 477 478 printf("AKF: %u\n", akf); 479 printf("AID: %02x\n", aid); 480 481 mesh_transport_key_and_virtual_address_iterator_init(&mesh_transport_key_it, mesh_transport_dst(transport_pdu), 482 transport_pdu->netkey_index, akf, aid); 483 mesh_upper_transport_validate_segmented_message(transport_pdu); 484 } 485 486 void mesh_upper_transport_message_received(mesh_pdu_t * pdu){ 487 btstack_linked_list_add_tail(&upper_transport_incoming, (btstack_linked_item_t*) pdu); 488 mesh_transport_run(); 489 } 490 491 void mesh_upper_transport_pdu_free(mesh_pdu_t * pdu){ 492 mesh_network_pdu_t * network_pdu; 493 mesh_transport_pdu_t * transport_pdu; 494 switch (pdu->pdu_type) { 495 case MESH_PDU_TYPE_NETWORK: 496 network_pdu = (mesh_network_pdu_t *) pdu; 497 mesh_network_pdu_free(network_pdu); 498 break; 499 case MESH_PDU_TYPE_TRANSPORT: 500 transport_pdu = (mesh_transport_pdu_t *) pdu; 501 mesh_transport_pdu_free(transport_pdu); 502 break; 503 default: 504 break; 505 } 506 } 507 508 void mesh_upper_transport_pdu_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu){ 509 switch (callback_type){ 510 case MESH_TRANSPORT_PDU_RECEIVED: 511 mesh_upper_transport_message_received(pdu); 512 break; 513 case MESH_TRANSPORT_PDU_SENT: 514 // notify upper layer (or just free pdu) 515 if (higher_layer_handler){ 516 higher_layer_handler(callback_type, status, pdu); 517 } else { 518 mesh_upper_transport_pdu_free(pdu); 519 } 520 break; 521 default: 522 break; 523 } 524 } 525 static void mesh_upper_transport_send_unsegmented_access_pdu_ccm(void * arg){ 526 crypto_active = 0; 527 528 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg; 529 uint8_t * upper_transport_pdu = mesh_network_pdu_data(network_pdu) + 1; 530 uint8_t upper_transport_pdu_len = mesh_network_pdu_len(network_pdu) - 1; 531 mesh_print_hex("EncAccessPayload", upper_transport_pdu, upper_transport_pdu_len); 532 // store TransMIC 533 btstack_crypto_ccm_get_authentication_value(&ccm, &upper_transport_pdu[upper_transport_pdu_len]); 534 mesh_print_hex("TransMIC", &upper_transport_pdu[upper_transport_pdu_len], 4); 535 network_pdu->len += 4; 536 // send network pdu 537 mesh_lower_transport_send_pdu((mesh_pdu_t*) network_pdu); 538 } 539 540 static void mesh_upper_transport_send_segmented_access_pdu_ccm(void * arg){ 541 crypto_active = 0; 542 543 mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg; 544 mesh_print_hex("EncAccessPayload", transport_pdu->data, transport_pdu->len); 545 // store TransMIC 546 btstack_crypto_ccm_get_authentication_value(&ccm, &transport_pdu->data[transport_pdu->len]); 547 mesh_print_hex("TransMIC", &transport_pdu->data[transport_pdu->len], transport_pdu->transmic_len); 548 transport_pdu->len += transport_pdu->transmic_len; 549 mesh_lower_transport_send_pdu((mesh_pdu_t*) transport_pdu); 550 } 551 552 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, 553 const uint8_t * control_pdu_data, uint16_t control_pdu_len){ 554 555 printf("[+] Upper transport, setup unsegmented Control PDU (opcode %02x): \n", opcode); 556 printf_hexdump(control_pdu_data, control_pdu_len); 557 558 if (control_pdu_len > 11) return 1; 559 560 const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 561 if (!network_key) return 1; 562 563 uint8_t transport_pdu_data[12]; 564 transport_pdu_data[0] = opcode; 565 memcpy(&transport_pdu_data[1], control_pdu_data, control_pdu_len); 566 uint16_t transport_pdu_len = control_pdu_len + 1; 567 568 mesh_print_hex("LowerTransportPDU", transport_pdu_data, transport_pdu_len); 569 // setup network_pdu 570 mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, 1, ttl, mesh_sequence_number_next(), src, dest, transport_pdu_data, transport_pdu_len); 571 572 return 0; 573 } 574 575 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, 576 const uint8_t * control_pdu_data, uint16_t control_pdu_len){ 577 578 printf("[+] Upper transport, setup segmented Control PDU (opcode %02x): \n", opcode); 579 printf_hexdump(control_pdu_data, control_pdu_len); 580 581 if (control_pdu_len > 256) return 1; 582 583 const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 584 if (!network_key) return 1; 585 586 uint32_t seq = mesh_sequence_number_peek(); 587 588 memcpy(transport_pdu->data, control_pdu_data, control_pdu_len); 589 transport_pdu->len = control_pdu_len; 590 transport_pdu->netkey_index = netkey_index; 591 transport_pdu->akf_aid = opcode; 592 transport_pdu->transmic_len = 0; // no TransMIC for control 593 mesh_transport_set_nid_ivi(transport_pdu, network_key->nid); 594 mesh_transport_set_seq(transport_pdu, seq); 595 mesh_transport_set_src(transport_pdu, src); 596 mesh_transport_set_dest(transport_pdu, dest); 597 mesh_transport_set_ctl_ttl(transport_pdu, 0x80 | ttl); 598 599 return 0; 600 } 601 602 uint8_t mesh_upper_transport_setup_control_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, 603 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, const uint8_t * control_pdu_data, uint16_t control_pdu_len){ 604 switch (pdu->pdu_type){ 605 case MESH_PDU_TYPE_NETWORK: 606 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); 607 case MESH_PDU_TYPE_TRANSPORT: 608 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); 609 default: 610 return 1; 611 } 612 } 613 614 static uint8_t mesh_upper_transport_setup_unsegmented_access_pdu_header(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, 615 uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest){ 616 617 // get app or device key 618 const mesh_transport_key_t * appkey; 619 appkey = mesh_transport_key_get(appkey_index); 620 if (appkey == NULL){ 621 printf("appkey_index %x unknown\n", appkey_index); 622 return 1; 623 } 624 uint8_t akf_aid = (appkey->akf << 6) | appkey->aid; 625 626 // lookup network by netkey_index 627 const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 628 if (!network_key) return 1; 629 630 network_pdu->data[9] = akf_aid; 631 // setup network_pdu 632 mesh_network_setup_pdu_header(network_pdu, netkey_index, network_key->nid, 0, ttl, mesh_sequence_number_next(), src, dest); 633 network_pdu->appkey_index = appkey_index; 634 return 0; 635 } 636 637 static uint8_t mesh_upper_transport_setup_unsegmented_access_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, 638 const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 639 640 int status = mesh_upper_transport_setup_unsegmented_access_pdu_header(network_pdu, netkey_index, appkey_index, ttl, src, dest); 641 if (status) return status; 642 643 printf("[+] Upper transport, setup unsegmented Access PDU - seq %06x\n", mesh_network_seq(network_pdu)); 644 mesh_print_hex("Access Payload", access_pdu_data, access_pdu_len); 645 646 // store in transport pdu 647 memcpy(&network_pdu->data[10], access_pdu_data, access_pdu_len); 648 network_pdu->len = 10 + access_pdu_len; 649 return 0; 650 } 651 652 static uint8_t mesh_upper_transport_setup_segmented_access_pdu_header(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, 653 uint8_t szmic){ 654 uint32_t seq = mesh_sequence_number_peek(); 655 656 printf("[+] Upper transport, setup segmented Access PDU - seq %06x, szmic %u, iv_index %08x\n", seq, szmic, 657 mesh_get_iv_index_for_tx()); 658 mesh_print_hex("Access Payload", transport_pdu->data, transport_pdu->len); 659 660 // get app or device key 661 const mesh_transport_key_t *appkey; 662 appkey = mesh_transport_key_get(appkey_index); 663 if (appkey == NULL) { 664 printf("appkey_index %x unknown\n", appkey_index); 665 return 1; 666 } 667 uint8_t akf_aid = (appkey->akf << 6) | appkey->aid; 668 669 // lookup network by netkey_index 670 const mesh_network_key_t *network_key = mesh_network_key_list_get(netkey_index); 671 if (!network_key) return 1; 672 673 const uint8_t trans_mic_len = szmic ? 8 : 4; 674 675 // store in transport pdu 676 transport_pdu->transmic_len = trans_mic_len; 677 transport_pdu->netkey_index = netkey_index; 678 transport_pdu->appkey_index = appkey_index; 679 transport_pdu->akf_aid = akf_aid; 680 mesh_transport_set_nid_ivi(transport_pdu, network_key->nid | ((mesh_get_iv_index_for_tx() & 1) << 7)); 681 mesh_transport_set_seq(transport_pdu, seq); 682 mesh_transport_set_src(transport_pdu, src); 683 mesh_transport_set_dest(transport_pdu, dest); 684 mesh_transport_set_ctl_ttl(transport_pdu, ttl); 685 return 0; 686 } 687 688 689 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, 690 uint8_t szmic, const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 691 int status = mesh_upper_transport_setup_segmented_access_pdu_header(transport_pdu, netkey_index, appkey_index, ttl, src, dest, szmic); 692 if (status) return status; 693 694 // store in transport pdu 695 memcpy(transport_pdu->data, access_pdu_data, access_pdu_len); 696 transport_pdu->len = access_pdu_len; 697 return 0; 698 } 699 uint8_t mesh_upper_transport_setup_access_pdu_header(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index, 700 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){ 701 switch (pdu->pdu_type){ 702 case MESH_PDU_TYPE_NETWORK: 703 return mesh_upper_transport_setup_unsegmented_access_pdu_header((mesh_network_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest); 704 case MESH_PDU_TYPE_TRANSPORT: 705 return mesh_upper_transport_setup_segmented_access_pdu_header((mesh_transport_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic); 706 default: 707 return 1; 708 } 709 } 710 711 uint8_t mesh_upper_transport_setup_access_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index, 712 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic, 713 const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 714 switch (pdu->pdu_type){ 715 case MESH_PDU_TYPE_NETWORK: 716 return mesh_upper_transport_setup_unsegmented_access_pdu((mesh_network_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, access_pdu_data, access_pdu_len); 717 case MESH_PDU_TYPE_TRANSPORT: 718 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); 719 default: 720 return 1; 721 } 722 } 723 724 void mesh_upper_transport_send_control_pdu(mesh_pdu_t * pdu){ 725 mesh_lower_transport_send_pdu((mesh_pdu_t*) pdu); 726 } 727 728 static void mesh_upper_transport_send_unsegmented_access_pdu_digest(void * arg){ 729 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg; 730 uint8_t * access_pdu_data = mesh_network_pdu_data(network_pdu) + 1; 731 uint16_t access_pdu_len = mesh_network_pdu_len(network_pdu) - 1; 732 btstack_crypto_ccm_encrypt_block(&ccm, access_pdu_len, access_pdu_data, access_pdu_data, &mesh_upper_transport_send_unsegmented_access_pdu_ccm, network_pdu); 733 } 734 735 static mesh_transport_key_t * mesh_upper_transport_get_outgoing_appkey(uint16_t netkey_index, uint16_t appkey_index){ 736 // Device Key is fixed 737 if (appkey_index == MESH_DEVICE_KEY_INDEX) { 738 return mesh_transport_key_get(appkey_index); 739 } 740 741 // Get key refresh state from subnet 742 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 743 if (subnet == NULL) return NULL; 744 745 // identify old and new app keys for given appkey_index 746 mesh_transport_key_t * old_key = NULL; 747 mesh_transport_key_t * new_key = NULL; 748 mesh_transport_key_iterator_t it; 749 mesh_transport_key_iterator_init(&it, netkey_index); 750 while (mesh_transport_key_iterator_has_more(&it)){ 751 mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it); 752 if (transport_key->appkey_index != appkey_index) continue; 753 if (transport_key->old_key == 0) { 754 new_key = transport_key; 755 } else { 756 old_key = transport_key; 757 } 758 } 759 760 // if no key is marked as old, just use the current one 761 if (old_key == NULL) return new_key; 762 763 // use new key if it exists in phase two 764 if ((subnet->key_refresh == MESH_KEY_REFRESH_SECOND_PHASE) && (new_key != NULL)){ 765 return new_key; 766 } else { 767 return old_key; 768 } 769 } 770 771 static void mesh_upper_transport_send_unsegmented_access_pdu(mesh_network_pdu_t * network_pdu){ 772 773 // if dst is virtual address, lookup label uuid and hash 774 uint16_t aad_len = 0; 775 mesh_virtual_address_t * virtual_address = NULL; 776 uint16_t dst = mesh_network_dst(network_pdu); 777 if (mesh_network_address_virtual(dst)){ 778 virtual_address = mesh_virtual_address_for_pseudo_dst(dst); 779 if (!virtual_address){ 780 printf("No virtual address register for pseudo dst %4x\n", dst); 781 btstack_memory_mesh_network_pdu_free(network_pdu); 782 return; 783 } 784 aad_len = 16; 785 big_endian_store_16(network_pdu->data, 7, virtual_address->hash); 786 } 787 788 // setup nonce 789 uint16_t appkey_index = network_pdu->appkey_index; 790 if (appkey_index == MESH_DEVICE_KEY_INDEX){ 791 transport_unsegmented_setup_device_nonce(application_nonce, network_pdu); 792 } else { 793 transport_unsegmented_setup_application_nonce(application_nonce, network_pdu); 794 } 795 796 // get app or device key 797 const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(network_pdu->netkey_index, appkey_index); 798 mesh_print_hex("AppOrDevKey", appkey->key, 16); 799 800 // encrypt ccm 801 uint8_t trans_mic_len = 4; 802 uint16_t access_pdu_len = mesh_network_pdu_len(network_pdu) - 1; 803 crypto_active = 1; 804 805 btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, trans_mic_len); 806 if (virtual_address){ 807 mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16); 808 btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_unsegmented_access_pdu_digest, network_pdu); 809 } else { 810 mesh_upper_transport_send_unsegmented_access_pdu_digest(network_pdu); 811 } 812 } 813 814 static void mesh_upper_transport_send_segmented_access_pdu_digest(void *arg){ 815 mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg; 816 uint16_t access_pdu_len = transport_pdu->len; 817 uint8_t * access_pdu_data = transport_pdu->data; 818 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); 819 } 820 821 static void mesh_upper_transport_send_segmented_access_pdu(mesh_transport_pdu_t * transport_pdu){ 822 823 // if dst is virtual address, lookup label uuid and hash 824 uint16_t aad_len = 0; 825 mesh_virtual_address_t * virtual_address = NULL; 826 uint16_t dst = mesh_transport_dst(transport_pdu); 827 if (mesh_network_address_virtual(dst)){ 828 virtual_address = mesh_virtual_address_for_pseudo_dst(dst); 829 if (!virtual_address){ 830 printf("No virtual address register for pseudo dst %4x\n", dst); 831 btstack_memory_mesh_transport_pdu_free(transport_pdu); 832 return; 833 } 834 // printf("Using hash %4x with LabelUUID: ", virtual_address->hash); 835 // printf_hexdump(virtual_address->label_uuid, 16); 836 aad_len = 16; 837 big_endian_store_16(transport_pdu->network_header, 7, virtual_address->hash); 838 } 839 840 // setup nonce - uses dst, so after pseudo address translation 841 uint16_t appkey_index = transport_pdu->appkey_index; 842 if (appkey_index == MESH_DEVICE_KEY_INDEX){ 843 transport_segmented_setup_device_nonce(application_nonce, transport_pdu); 844 } else { 845 transport_segmented_setup_application_nonce(application_nonce, transport_pdu); 846 } 847 848 // get app or device key 849 const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(transport_pdu->netkey_index, appkey_index); 850 mesh_print_hex("AppOrDevKey", appkey->key, 16); 851 852 // encrypt ccm 853 uint8_t transmic_len = transport_pdu->transmic_len; 854 uint16_t access_pdu_len = transport_pdu->len; 855 crypto_active = 1; 856 btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, transmic_len); 857 if (virtual_address){ 858 mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16); 859 btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_segmented_access_pdu_digest, transport_pdu); 860 } else { 861 mesh_upper_transport_send_segmented_access_pdu_digest(transport_pdu); 862 } 863 } 864 865 void mesh_upper_transport_send_access_pdu(mesh_pdu_t * pdu){ 866 switch (pdu->pdu_type){ 867 case MESH_PDU_TYPE_NETWORK: 868 mesh_upper_transport_send_unsegmented_access_pdu((mesh_network_pdu_t *) pdu); 869 break; 870 case MESH_PDU_TYPE_TRANSPORT: 871 mesh_upper_transport_send_segmented_access_pdu((mesh_transport_pdu_t *) pdu); 872 break; 873 default: 874 break; 875 } 876 } 877 878 void mesh_upper_transport_register_access_message_handler(void (*callback)(mesh_pdu_t *pdu)){ 879 mesh_access_message_handler = callback; 880 } 881 882 void mesh_upper_transport_register_control_message_handler(void (*callback)(mesh_pdu_t *pdu)){ 883 mesh_control_message_handler = callback; 884 } 885 886 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)){ 887 higher_layer_handler = pdu_handler; 888 } 889 890 void mesh_upper_transport_init(){ 891 mesh_lower_transport_init(); 892 mesh_lower_transport_set_higher_layer_handler(&mesh_upper_transport_pdu_handler); 893 } 894 895 static void mesh_transport_run(void){ 896 while(!btstack_linked_list_empty(&upper_transport_incoming)){ 897 898 if (crypto_active) return; 899 900 // peek at next message 901 mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_get_first_item(&upper_transport_incoming); 902 mesh_transport_pdu_t * transport_pdu; 903 mesh_network_pdu_t * network_pdu; 904 switch (pdu->pdu_type){ 905 case MESH_PDU_TYPE_NETWORK: 906 network_pdu = (mesh_network_pdu_t *) pdu; 907 // control? 908 if (mesh_network_control(network_pdu)) { 909 (void) btstack_linked_list_pop(&upper_transport_incoming); 910 mesh_upper_unsegmented_control_message_received(network_pdu); 911 } else { 912 mesh_network_pdu_t * decode_pdu = mesh_network_pdu_get(); 913 if (!decode_pdu) return; 914 // get encoded network pdu and start processing 915 network_pdu_in_validation = network_pdu; 916 (void) btstack_linked_list_pop(&upper_transport_incoming); 917 mesh_upper_transport_process_unsegmented_access_message(decode_pdu); 918 } 919 break; 920 case MESH_PDU_TYPE_TRANSPORT: 921 transport_pdu = (mesh_transport_pdu_t *) pdu; 922 uint8_t ctl = mesh_transport_ctl(transport_pdu); 923 if (ctl){ 924 printf("Ignoring Segmented Control Message\n"); 925 (void) btstack_linked_list_pop(&upper_transport_incoming); 926 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) transport_pdu); 927 } else { 928 mesh_transport_pdu_t * decode_pdu = mesh_transport_pdu_get(); 929 if (!decode_pdu) return; 930 // get encoded transport pdu and start processing 931 transport_pdu_in_validation = transport_pdu; 932 (void) btstack_linked_list_pop(&upper_transport_incoming); 933 mesh_upper_transport_process_message(decode_pdu); 934 } 935 break; 936 default: 937 break; 938 } 939 } 940 } 941 942 // buffer pool 943 mesh_transport_pdu_t * mesh_transport_pdu_get(void){ 944 mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get(); 945 if (transport_pdu) { 946 memset(transport_pdu, 0, sizeof(mesh_transport_pdu_t)); 947 transport_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_TRANSPORT; 948 } 949 return transport_pdu; 950 } 951 952 void mesh_transport_pdu_free(mesh_transport_pdu_t * transport_pdu){ 953 btstack_memory_mesh_transport_pdu_free(transport_pdu); 954 } 955