From f02c684c6ca21150fb45a4fc8d1ab68f060b8b06 Mon Sep 17 00:00:00 2001 From: Milanka Ringwald Date: Mon, 10 Dec 2018 22:59:51 +0100 Subject: [PATCH] mesh: proxy service. send secure network beacon --- .../gatt-service/mesh_proxy_service_server.c | 10 ++- src/ble/mesh/gatt_bearer.c | 29 ++----- test/mesh/Makefile | 4 +- test/mesh/mesh_proxy_server.c | 77 ++++++++++++++++--- 4 files changed, 85 insertions(+), 35 deletions(-) diff --git a/src/ble/gatt-service/mesh_proxy_service_server.c b/src/ble/gatt-service/mesh_proxy_service_server.c index 8005a0c96..b168a0e04 100644 --- a/src/ble/gatt-service/mesh_proxy_service_server.c +++ b/src/ble/gatt-service/mesh_proxy_service_server.c @@ -106,7 +106,13 @@ static uint16_t mesh_proxy_service_read_callback(hci_con_handle_t con_handle, ui log_error("mesh_proxy_service_read_callback: instance is null"); return 0; } - // log_info("mesh_proxy_service_read_callback: not handled read on handle 0x%02x", attribute_handle); + if (attribute_handle == instance->data_out_client_configuration_descriptor_handle){ + if (buffer && buffer_size >= 2){ + little_endian_store_16(buffer, 0, instance->data_out_client_configuration_descriptor_value); + } + return 2; + } + log_info("mesh_proxy_service_read_callback: not handled read on handle 0x%02x", attribute_handle); return 0; } @@ -140,7 +146,7 @@ static int mesh_proxy_service_write_callback(hci_con_handle_t con_handle, uint16 } return 0; } - // log_info("mesh_proxy_service_write_callback: not handled write on handle 0x%02x, buffer size %d", attribute_handle, buffer_size); + log_info("mesh_proxy_service_write_callback: not handled write on handle 0x%02x, buffer size %d", attribute_handle, buffer_size); return 0; } diff --git a/src/ble/mesh/gatt_bearer.c b/src/ble/mesh/gatt_bearer.c index ee301b075..bbfcf8303 100644 --- a/src/ble/mesh/gatt_bearer.c +++ b/src/ble/mesh/gatt_bearer.c @@ -94,7 +94,6 @@ static void gatt_bearer_emit_can_send_now(void){ if (request_can_send_now[last_sender]){ request_can_send_now[last_sender] = 0; // emit can send now - log_info("can send now"); uint8_t event[3]; event[0] = HCI_EVENT_MESH_META; event[1] = 1; @@ -106,7 +105,6 @@ static void gatt_bearer_emit_can_send_now(void){ } static void gatt_bearer_request(message_type_id_t type_id){ - log_info("request to send message type %u", (int) type_id); request_can_send_now[type_id] = 1; mesh_proxy_service_server_request_can_send_now(gatt_bearer_con_handle); } @@ -162,7 +160,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe if (!client_callbacks[msg_type]) return; break; default: - printf("gatt bearer: message type %d not supported yet\n", msg_type); + log_info("gatt bearer: message type %d not supported yet", msg_type); return; } pdu_segment_len = size - pos; @@ -207,7 +205,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe gatt_bearer_send_mesh_beacon(sar_buffer.reassembly_buffer, reassembly_offset); break; default: - printf("gatt bearer: message type %d not supported yet\n", msg_type); + log_info("gatt bearer: message type %d not supported yet", msg_type); return; } } @@ -219,10 +217,11 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe switch (hci_event_mesh_meta_get_subevent_code(packet)){ case MESH_PB_TRANSPORT_LINK_OPEN: case MESH_PB_TRANSPORT_LINK_CLOSED: - // Forward link open/close gatt_bearer_mtu = ATT_DEFAULT_MTU; gatt_bearer_con_handle = mesh_pb_transport_link_open_event_get_pb_transport_cid(packet); - // gatt_bearer_packet_handler(HCI_EVENT_PACKET, 0, packet, size); + if ((*client_callbacks[MESH_MESSAGE_ID])){ + (*client_callbacks[MESH_MESSAGE_ID])(HCI_EVENT_PACKET, 0, packet, size); + } break; case MESH_SUBEVENT_CAN_SEND_NOW: con_handle = little_endian_read_16(packet, 3); @@ -282,23 +281,11 @@ static void gatt_bearer_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint1 } void gatt_bearer_send_mesh_message(const uint8_t * data, uint16_t data_len){ + msg_type = MESH_MSG_TYPE_NETWORK_PDU; gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len); } + void gatt_bearer_send_mesh_beacon(const uint8_t * data, uint16_t data_len){ + msg_type = MESH_MSG_TYPE_BEACON; gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len); } - -#if 0 -void gatt_bearer_register_for_pb_adv(btstack_packet_handler_t packet_handler){ - client_callbacks[PB_ADV_ID] = packet_handler; -} - -void gatt_bearer_request_can_send_now_for_pb_adv(void){ - gatt_bearer_request(PB_ADV_ID); -} - -void gatt_bearer_send_pb_adv(const uint8_t * data, uint16_t data_len){ - // gatt_bearer_start_advertising(data, data_len, BLUETOOTH_DATA_TYPE_PB_ADV); -} -#endif - diff --git a/test/mesh/Makefile b/test/mesh/Makefile index 913ec7f01..98ebbd8c6 100644 --- a/test/mesh/Makefile +++ b/test/mesh/Makefile @@ -1,4 +1,4 @@ -# Makefile for libusb based examples + # Makefile for libusb based examples BTSTACK_ROOT = ../.. CORE += \ @@ -73,7 +73,7 @@ mesh_provisioning_device: mesh_provisioning_device.h ${CORE_OBJ} ${COMMON_OBJ} $ mesh_proxy_server.h: mesh_proxy_server.gatt python ${BTSTACK_ROOT}/tool/compile_gatt.py $< $@ -mesh_proxy_server: mesh_proxy_server.h ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_SERVER_OBJ} ${SM_OBJ} mesh_crypto.o gatt_bearer.c mesh_proxy_service_server.o mesh_proxy_server.o +mesh_proxy_server: mesh_proxy_server.h ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_SERVER_OBJ} ${SM_OBJ} mesh_network.o mesh_crypto.o gatt_bearer.c mesh_proxy_service_server.o mesh_proxy_server.o ${CC} $(filter-out mesh_proxy_server.h,$^) ${CFLAGS} ${LDFLAGS} -o $@ EXAMPLES = mesh provisioner mesh_message_test sniffer provisioning_device_test provisioning_provisioner_test mesh_provisioning_device mesh_proxy_server diff --git a/test/mesh/mesh_proxy_server.c b/test/mesh/mesh_proxy_server.c index 7dc7ab514..277e022a9 100644 --- a/test/mesh/mesh_proxy_server.c +++ b/test/mesh/mesh_proxy_server.c @@ -45,12 +45,15 @@ #include "ble/mesh/gatt_bearer.h" #include "ble/gatt-service/mesh_proxy_service_server.h" #include "mesh_proxy_server.h" +#include "ble/mesh/mesh_network.h" #include "btstack_config.h" #include "btstack.h" #include "provisioning.h" #include "provisioning_device.h" #include "btstack_tlv.h" +#define BEACON_TYPE_SECURE_NETWORK 1 + static mesh_provisioning_data_t provisioning_data; static const btstack_tlv_t * btstack_tlv_singleton_impl; static void * btstack_tlv_singleton_context; @@ -64,6 +67,14 @@ static uint8_t plaintext[16]; static uint8_t hash[8]; static uint8_t random_value[8]; +static btstack_crypto_aes128_cmac_t mesh_cmac_request; +static uint8_t mesh_secure_network_beacon[22]; +static uint8_t mesh_secure_network_beacon_auth_value[16]; +static uint8_t mesh_flags; +static uint8_t network_id[8]; +static uint8_t beacon_key[16]; + + static btstack_packet_callback_registration_t hci_event_callback_registration; static uint8_t adv_data_with_network_id[] = { @@ -124,11 +135,14 @@ static void stdin_process(char cmd){ static void setup_advertising_with_network_id(mesh_provisioning_data_t * prov_data){ // dynamically store network ID into adv data - // skip flipping for now ... (check if provisioner or BlueNRG-MESH has a bug) - // uint8_t netid_flipped[8]; - // reverse_64(provisioning_data.network_id, netid_flipped); - // memcpy(&adv_data_with_network_id[12], netid_flipped, sizeof(netid_flipped)); memcpy(&adv_data_with_network_id[12], prov_data->network_id, sizeof(prov_data->network_id)); + // copy beacon key and network id + memcpy(beacon_key, prov_data->beacon_key, 16); + memcpy(network_id, prov_data->network_id, 8); + + printf_hexdump(prov_data->network_id, 8); + mesh_flags = prov_data->flags; + // setup advertisements bd_addr_t null_addr; memset(null_addr, 0, 6); @@ -181,12 +195,33 @@ static void mesh_proxy_handle_get_salt_nhbk(void * arg){ printf_hexdump(mesh_salt_nhbk, sizeof(mesh_salt_nhbk)); } +static void mesh_provisioning_dump(const mesh_provisioning_data_t * data){ + printf("UnicastAddr: 0x%02x\n", data->unicast_address); + printf("NID: 0x%02x\n", data->nid); + printf("IV Index: 0x%08x\n", data->iv_index); + printf("NetworkID: "); printf_hexdump(data->network_id, 8); + printf("BeaconKey: "); printf_hexdump(data->beacon_key, 16); + printf("EncryptionKey: "); printf_hexdump(data->encryption_key, 16); + printf("PrivacyKey: "); printf_hexdump(data->privacy_key, 16); + printf("DevKey: "); printf_hexdump(data->device_key, 16); +} + +static void mesh_secure_network_beacon_auth_value_calculated(void * arg){ + UNUSED(arg); + memcpy(&mesh_secure_network_beacon[14], mesh_secure_network_beacon_auth_value, 8); + printf("Secure Network Beacon\n"); + printf("- "); + printf_hexdump(mesh_secure_network_beacon, sizeof(mesh_secure_network_beacon)); + printf("Secure Network Beacon done"); + gatt_bearer_request_can_send_now_for_mesh_beacon(); +} + static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ UNUSED(channel); UNUSED(size); int prov_len; if (packet_type != HCI_EVENT_PACKET) return; - + switch (hci_event_packet_get_type(packet)){ case BTSTACK_EVENT_STATE:{ if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; @@ -195,11 +230,11 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe // load provisioning data prov_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, 'PROV', (uint8_t *) &provisioning_data, sizeof(mesh_provisioning_data_t)); printf("Provisioning data available: %u\n", prov_len ? 1 : 0); - if (!prov_len) break; - // setup_advertising_with_network_id(&provisioning_data); - btstack_crypto_random_generate(&crypto_request_random, random_value, sizeof(random_value), mesh_proxy_handle_get_random, NULL); - // btstack_crypto_aes128_cmac_zero(&crypto_aes128_cmac_request, 4, (const uint8_t *)salt, mesh_salt_nhbk, mesh_proxy_handle_get_salt_nhbk, NULL); + + mesh_provisioning_dump(&provisioning_data); + setup_advertising_with_network_id(&provisioning_data); + // btstack_crypto_random_generate(&crypto_request_random, random_value, sizeof(random_value), mesh_proxy_handle_get_random, NULL); break; } case HCI_EVENT_LE_META: @@ -212,6 +247,25 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe break; } break; + case HCI_EVENT_MESH_META: + printf("packet_handler packet_type %02x, type 0%02x\n", packet_type, hci_event_packet_get_type(packet)); + switch (hci_event_mesh_meta_get_subevent_code(packet)){ + case MESH_PB_TRANSPORT_LINK_OPEN: + printf("mesh_proxy_server: MESH_PB_TRANSPORT_LINK_OPEN\n"); + printf("+ Setup Secure Network Beacon\n"); + mesh_secure_network_beacon[0] = BEACON_TYPE_SECURE_NETWORK; + mesh_secure_network_beacon[1] = mesh_flags; + memcpy(&mesh_secure_network_beacon[2], network_id, 8); + big_endian_store_32(mesh_secure_network_beacon, 10, mesh_get_iv_index()); + btstack_crypto_aes128_cmac_message(&mesh_cmac_request, beacon_key, 13, + &mesh_secure_network_beacon[1], mesh_secure_network_beacon_auth_value, &mesh_secure_network_beacon_auth_value_calculated, NULL); + break; + case MESH_SUBEVENT_CAN_SEND_NOW: + gatt_bearer_send_mesh_beacon(mesh_secure_network_beacon, sizeof(mesh_secure_network_beacon)); + default: + break; + } + break; default: break; } @@ -236,9 +290,12 @@ int btstack_main(void){ // setup ATT server att_server_init(profile_data, NULL, NULL); - + // mesh_proxy_service_server_init(); + // mesh_proxy_service_server_register_packet_handler(packet_handler); // Setup GATT bearere gatt_bearer_init(); + gatt_bearer_register_for_mesh_message(packet_handler); + gatt_bearer_register_for_mesh_beacon(packet_handler); #ifdef HAVE_BTSTACK_STDIN btstack_stdin_setup(stdin_process); #endif