From 4eb6a97f4cfa643a7e4f46cc21b0aa366a8887a1 Mon Sep 17 00:00:00 2001 From: Daniel Adam Date: Thu, 22 Jun 2023 16:32:13 +0200 Subject: [PATCH] Handle oc_message_t buffer size Depending on compilation options the buffer of oc_message_t might be static or dynamic, and of various sizes. Functions that work with the buffer must receive correct size of the buffer. --- .github/workflows/cmake-linux.yml | 6 +- CMakeLists.txt | 16 ++ api/oc_buffer.c | 10 +- api/oc_client_api.c | 8 +- api/oc_server_api.c | 6 +- messaging/coap/coap.c | 211 +++++++++++---------- messaging/coap/coap.h | 20 +- messaging/coap/coap_signal.c | 6 +- messaging/coap/engine.c | 7 +- messaging/coap/observe.c | 9 +- messaging/coap/oscore.c | 15 +- messaging/coap/oscore.h | 6 +- messaging/coap/separate.c | 3 +- messaging/coap/unittest/coapsignaltest.cpp | 15 +- port/unittest/connectivitytest.cpp | 7 +- security/oc_oscore_engine.c | 25 ++- security/oc_tls.c | 2 +- security/unittest/oscore_test.cpp | 105 +++++----- 18 files changed, 275 insertions(+), 202 deletions(-) diff --git a/.github/workflows/cmake-linux.yml b/.github/workflows/cmake-linux.yml index 73f7e94ada..21247335f4 100644 --- a/.github/workflows/cmake-linux.yml +++ b/.github/workflows/cmake-linux.yml @@ -52,8 +52,10 @@ jobs: - args: "-DOC_IPV4_ENABLED=ON -DOC_TCP_ENABLED=ON -DOC_DYNAMIC_ALLOCATION_ENABLED=OFF" # ipv4 on, tcp on, pki off - args: "-DOC_IPV4_ENABLED=ON -DOC_TCP_ENABLED=ON -DOC_PKI_ENABLED=OFF" - # cloud on (ipv4+tcp on), collections create on, maintenance resource on + # cloud on (ipv4+tcp on), collections create on - args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON" + # cloud on (ipv4+tcp on), collections create on, custom message buffer size + - args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_INOUT_BUFFER_SIZE=1024" # debug on - args: "-DOC_DEBUG_ENABLED=ON" # debug on, cloud on (ipv4+tcp on) @@ -66,7 +68,7 @@ jobs: - args: "-DOC_SECURITY_ENABLED=OFF -DOC_TCP_ENABLED=ON -DOC_IPV4_ENABLED=ON" # /oic/res observable on, rep realloc on - args: "-DOC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED=ON -DOC_REPRESENTATION_REALLOC_ENCODING_ENABLED=ON" - # everything off (dynamic allocation off, secure off, pki off, idd off, oscore off, well-known core resource off, software update off, push notifications off, plgd-time off, introspection off) + # everything off (dynamic allocation off, secure off, pki off, idd off, oscore off, well-known core resource off, software update off, , maintenance resource off, push notifications off, plgd-time off, introspection off) - args: "-DOC_DYNAMIC_ALLOCATION_ENABLED=OFF -DOC_SECURITY_ENABLED=OFF -DOC_PKI_ENABLED=OFF -DOC_IDD_API_ENABLED=OFF -DOC_OSCORE_ENABLED=OFF -DOC_WKCORE_ENABLED=OFF -DOC_SOFTWARE_UPDATE_ENABLED=OFF -DOC_MNT_ENABLED=OFF -DOC_PUSH_ENABLED=OFF -DPLGD_DEV_TIME_ENABLED=OFF -DOC_INTROSPECTION_ENABLED=OFF" uses: ./.github/workflows/unit-test-with-cfg.yml with: diff --git a/CMakeLists.txt b/CMakeLists.txt index 04567bff76..38a3e58497 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,8 @@ if (OC_DEBUG_ENABLED) else() set(OC_LOG_MAXIMUM_LOG_LEVEL "DISABLED" CACHE STRING "Maximum supported log level in compile time.") endif() +set(OC_INOUT_BUFFER_SIZE "" CACHE STRING "Custom buffer size for network messages.") +set(OC_INOUT_BUFFER_POOL_SIZE "" CACHE STRING "Custom pool size of network messages.") set(PLGD_DEV_TIME_ENABLED OFF CACHE BOOL "Enable plgd time feature.") set(CMAKE_POSITION_INDEPENDENT_CODE ON) @@ -363,6 +365,20 @@ if(OC_MEMORY_TRACE_ENABLED) list(APPEND TEST_COMPILE_DEFINITIONS "OC_MEMORY_TRACE") endif() +if (NOT("${OC_INOUT_BUFFER_SIZE}" STREQUAL "")) + if(NOT OC_DYNAMIC_ALLOCATION_ENABLED) + message(FATAL_ERROR "Cannot set custom buffer size for network messages without dynamic allocation") + endif() + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_SIZE=(${OC_INOUT_BUFFER_SIZE})") + list(APPEND MBEDTLS_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_SIZE=(${OC_INOUT_BUFFER_SIZE})") +endif() +if (NOT("${OC_INOUT_BUFFER_POOL_SIZE}" STREQUAL "")) + if(NOT OC_DYNAMIC_ALLOCATION_ENABLED) + message(FATAL_ERROR "Cannot set custom pool size for network messages without dynamic allocation") + endif() + list(APPEND PRIVATE_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_POOL_SIZE=(${OC_INOUT_BUFFER_POOL_SIZE})") +endif() + if(PLGD_DEV_TIME_ENABLED) list(APPEND PUBLIC_COMPILE_DEFINITIONS "PLGD_DEV_TIME") if(BUILD_MBEDTLS) diff --git a/api/oc_buffer.c b/api/oc_buffer.c index bc15c1964f..d18093b9e3 100644 --- a/api/oc_buffer.c +++ b/api/oc_buffer.c @@ -41,13 +41,13 @@ #endif /* OC_DYNAMIC_ALLOCATION */ OC_PROCESS(oc_message_buffer_handler, "OC Message Buffer Handler"); -#ifdef OC_INOUT_BUFFER_POOL -OC_MEMB_STATIC(oc_incoming_buffers, oc_message_t, OC_INOUT_BUFFER_POOL); -OC_MEMB_STATIC(oc_outgoing_buffers, oc_message_t, OC_INOUT_BUFFER_POOL); -#else /* OC_INOUT_BUFFER_POOL */ +#ifdef OC_INOUT_BUFFER_POOL_SIZE +OC_MEMB_STATIC(oc_incoming_buffers, oc_message_t, OC_INOUT_BUFFER_POOL_SIZE); +OC_MEMB_STATIC(oc_outgoing_buffers, oc_message_t, OC_INOUT_BUFFER_POOL_SIZE); +#else /* OC_INOUT_BUFFER_POOL_SIZE */ OC_MEMB(oc_incoming_buffers, oc_message_t, OC_MAX_NUM_CONCURRENT_REQUESTS); OC_MEMB(oc_outgoing_buffers, oc_message_t, OC_MAX_NUM_CONCURRENT_REQUESTS); -#endif /* !OC_INOUT_BUFFER_POOL */ +#endif /* !OC_INOUT_BUFFER_POOL_SIZE */ static void message_deallocate(oc_message_t *message, struct oc_memb *pool) diff --git a/api/oc_client_api.c b/api/oc_client_api.c index 65b3c688e8..311a759feb 100644 --- a/api/oc_client_api.c +++ b/api/oc_client_api.c @@ -108,8 +108,8 @@ dispatch_coap_request(void) } bool success = false; - g_dispatch.transaction->message->length = - coap_serialize_message(g_request, g_dispatch.transaction->message->data); + g_dispatch.transaction->message->length = coap_serialize_message( + g_request, g_dispatch.transaction->message->data, oc_message_buffer_size()); if (g_dispatch.transaction->message->length > 0) { coap_send_transaction(g_dispatch.transaction); @@ -265,8 +265,8 @@ oc_do_multicast_update(void) coap_set_header_content_format(g_request, APPLICATION_VND_OCF_CBOR); } - g_multicast_update->length = - coap_serialize_message(g_request, g_multicast_update->data); + g_multicast_update->length = coap_serialize_message( + g_request, g_multicast_update->data, oc_message_buffer_size()); if (g_multicast_update->length > 0) { oc_send_message(g_multicast_update); } else { diff --git a/api/oc_server_api.c b/api/oc_server_api.c index 32902d77f6..f00e29bc49 100644 --- a/api/oc_server_api.c +++ b/api/oc_server_api.c @@ -16,7 +16,7 @@ * ****************************************************************************/ -#include "oc_server_api_internal.h" +#include "api/oc_buffer_internal.h" #include "api/oc_ri_internal.h" #include "messaging/coap/engine.h" #include "messaging/coap/oc_coap.h" @@ -25,6 +25,7 @@ #include "oc_api.h" #include "oc_core_res.h" #include "oc_core_res_internal.h" +#include "oc_server_api_internal.h" #include "port/oc_log_internal.h" #include "util/oc_features.h" #include "util/oc_macros_internal.h" @@ -697,7 +698,8 @@ handle_separate_response_transaction(coap_transaction_t *t, uint8_t response_code) { coap_set_status_code(response, response_code); - t->message->length = coap_serialize_message(response, t->message->data); + t->message->length = coap_serialize_message(response, t->message->data, + oc_message_buffer_size()); if (t->message->length <= 0) { coap_clear_transaction(t); return; diff --git a/messaging/coap/coap.c b/messaging/coap/coap.c index 0674e87e99..d3e8fdc806 100644 --- a/messaging/coap/coap.c +++ b/messaging/coap/coap.c @@ -1131,111 +1131,119 @@ coap_tcp_parse_message_length(const uint8_t *data, size_t *message_length, *message_length, *num_extended_length_bytes); } #endif /* OC_TCP */ + /*---------------------------------------------------------------------------*/ /*- Internal API ------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ + void coap_init_connection(void) { /* initialize transaction ID */ current_mid = (uint16_t)oc_random_value(); } -/*---------------------------------------------------------------------------*/ + uint16_t coap_get_mid(void) { return ++current_mid; } -/*---------------------------------------------------------------------------*/ + void -coap_udp_init_message(void *packet, coap_message_type_t type, uint8_t code, - uint16_t mid) +coap_udp_init_message(coap_packet_t *packet, coap_message_type_t type, + uint8_t code, uint16_t mid) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - /* Important thing */ - memset(coap_pkt, 0, sizeof(coap_packet_t)); - - coap_pkt->transport_type = COAP_TRANSPORT_UDP; - coap_pkt->type = type; - coap_pkt->code = code; - coap_pkt->mid = mid; + memset(packet, 0, sizeof(coap_packet_t)); + packet->transport_type = COAP_TRANSPORT_UDP; + packet->type = type; + packet->code = code; + packet->mid = mid; } -/*---------------------------------------------------------------------------*/ + #ifdef OC_TCP void -coap_tcp_init_message(void *packet, uint8_t code) +coap_tcp_init_message(coap_packet_t *packet, uint8_t code) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - /* Important thing */ - memset(coap_pkt, 0, sizeof(coap_packet_t)); - - coap_pkt->transport_type = COAP_TRANSPORT_TCP; - coap_pkt->type = COAP_TYPE_NON; - coap_pkt->code = code; - coap_pkt->mid = 0; + memset(packet, 0, sizeof(coap_packet_t)); + packet->transport_type = COAP_TRANSPORT_TCP; + packet->type = COAP_TYPE_NON; + packet->code = code; + packet->mid = 0; } #endif /* OC_TCP */ -/*---------------------------------------------------------------------------*/ + static void -coap_udp_set_header_fields(void *packet) +coap_udp_set_header_fields(coap_packet_t *packet) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; + packet->buffer[0] = 0x00; + packet->buffer[0] |= COAP_HEADER_VERSION_MASK & + (packet->version) << COAP_HEADER_VERSION_POSITION; + packet->buffer[0] |= COAP_HEADER_TYPE_MASK & (packet->type) + << COAP_HEADER_TYPE_POSITION; + packet->buffer[0] |= COAP_HEADER_TOKEN_LEN_MASK & + (packet->token_len) << COAP_HEADER_TOKEN_LEN_POSITION; + packet->buffer[1] = packet->code; + packet->buffer[2] = (uint8_t)((packet->mid) >> 8); + packet->buffer[3] = (uint8_t)(packet->mid); +} - coap_pkt->buffer[0] = 0x00; - coap_pkt->buffer[0] |= COAP_HEADER_VERSION_MASK & - (coap_pkt->version) << COAP_HEADER_VERSION_POSITION; - coap_pkt->buffer[0] |= COAP_HEADER_TYPE_MASK & (coap_pkt->type) - << COAP_HEADER_TYPE_POSITION; - coap_pkt->buffer[0] |= - COAP_HEADER_TOKEN_LEN_MASK & (coap_pkt->token_len) - << COAP_HEADER_TOKEN_LEN_POSITION; - coap_pkt->buffer[1] = coap_pkt->code; - coap_pkt->buffer[2] = (uint8_t)((coap_pkt->mid) >> 8); - coap_pkt->buffer[3] = (uint8_t)(coap_pkt->mid); +static bool +coap_oscore_check_packet_header_size(size_t header_size, size_t buffer_size) +{ + if (header_size > (size_t)COAP_MAX_HEADER_SIZE) { + OC_ERR("Serialized header length %zu exceeds COAP_MAX_HEADER_SIZE %zu", + header_size, (size_t)COAP_MAX_HEADER_SIZE); + return false; + } + if (header_size > buffer_size) { + OC_ERR("Serialized header length %zu exceeds buffer size %zu", header_size, + buffer_size); + return false; + } + return true; } -/*---------------------------------------------------------------------------*/ + size_t -coap_oscore_serialize_message(void *packet, uint8_t *buffer, bool inner, - bool outer, bool oscore) +coap_oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer, + size_t buffer_size, bool inner, bool outer, + bool oscore) { - if (!packet || !buffer) { - OC_ERR("packet: %p or buffer: %p is NULL", packet, (void *)buffer); + if (packet == NULL || buffer == NULL) { + OC_ERR("packet or buffer is NULL"); return 0; } - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; uint8_t *option; unsigned int current_number = 0; uint8_t token_location = 0; - size_t option_length = 0, option_length_calculation = 0, - header_length_calculation = 0; + size_t option_length = 0; + size_t option_length_calculation = 0; + size_t header_length_calculation = 0; /* Initialize */ - coap_pkt->buffer = buffer; - coap_pkt->version = 1; + packet->buffer = buffer; + packet->version = 1; /* coap header option serialize first to know total length about options */ option_length_calculation = - coap_serialize_options(coap_pkt, NULL, inner, outer, oscore); + coap_serialize_options(packet, NULL, inner, outer, oscore); header_length_calculation += option_length_calculation; /* accoridng to spec COAP_PAYLOAD_MARKER_LEN should be included if payload exists */ - if (coap_pkt->payload_len > 0) { + if (packet->payload_len > 0) { header_length_calculation += COAP_PAYLOAD_MARKER_LEN; } if (outer) { - header_length_calculation += coap_pkt->token_len; + header_length_calculation += packet->token_len; #ifdef OC_TCP - if (coap_pkt->transport_type == COAP_TRANSPORT_TCP) { + if (packet->transport_type == COAP_TRANSPORT_TCP) { uint8_t num_extended_length_bytes = 0, len = 0; size_t extended_len = 0; - coap_tcp_compute_message_length(coap_pkt, option_length_calculation, + coap_tcp_compute_message_length(packet, option_length_calculation, &num_extended_length_bytes, &len, &extended_len); @@ -1243,14 +1251,13 @@ coap_oscore_serialize_message(void *packet, uint8_t *buffer, bool inner, header_length_calculation += token_location; /* an error occurred: caller must check for !=0 */ - if (header_length_calculation > COAP_MAX_HEADER_SIZE) { - OC_ERR( - "Serialized header length %u exceeds COAP_MAX_HEADER_SIZE %u-TCP", - (unsigned int)(header_length_calculation), COAP_MAX_HEADER_SIZE); + if (!coap_oscore_check_packet_header_size(header_length_calculation, + buffer_size)) { + OC_ERR("cannot serialize TCP packet"); goto exit; } /* set header fields */ - coap_tcp_set_header_fields(coap_pkt, &num_extended_length_bytes, &len, + coap_tcp_set_header_fields(packet, &num_extended_length_bytes, &len, &extended_len); } else #endif /* OC_TCP */ @@ -1258,45 +1265,43 @@ coap_oscore_serialize_message(void *packet, uint8_t *buffer, bool inner, /* set header fields */ token_location = COAP_HEADER_LEN; header_length_calculation += token_location; - - if (header_length_calculation > COAP_MAX_HEADER_SIZE) { - OC_ERR( - "Serialized header length %u exceeds COAP_MAX_HEADER_SIZE %u-UDP", - (unsigned int)(header_length_calculation), COAP_MAX_HEADER_SIZE); + if (!coap_oscore_check_packet_header_size(header_length_calculation, + buffer_size)) { + OC_ERR("cannot serialize UDP packet"); goto exit; } - OC_DBG("-Serializing MID %u to %p", coap_pkt->mid, - (void *)coap_pkt->buffer); - coap_udp_set_header_fields(coap_pkt); + OC_DBG("-Serializing MID %u to %p", packet->mid, (void *)packet->buffer); + coap_udp_set_header_fields(packet); } } /* empty packet, dont need to do more stuff */ - if (outer && !coap_pkt->code) { - OC_DBG("Done serializing empty message at %p-", (void *)coap_pkt->buffer); + if (outer && !packet->code) { + OC_DBG("Done serializing empty message at %p-", (void *)packet->buffer); return token_location; - } else if (outer) { + } + if (outer) { if (oscore) { - OC_DBG("Outer CoAP code: %d", coap_pkt->code); + OC_DBG("Outer CoAP code: %d", packet->code); } /* set Token */ - OC_DBG("Token (len %u)", coap_pkt->token_len); - OC_LOGbytes(coap_pkt->token, coap_pkt->token_len); - option = coap_pkt->buffer + token_location; - for (current_number = 0; current_number < coap_pkt->token_len; + OC_DBG("Token (len %u)", packet->token_len); + OC_LOGbytes(packet->token, packet->token_len); + option = packet->buffer + token_location; + for (current_number = 0; current_number < packet->token_len; ++current_number) { - *option = coap_pkt->token[current_number]; + *option = packet->token[current_number]; ++option; } } else { - OC_DBG("Inner CoAP code: %d", coap_pkt->code); - coap_pkt->buffer[0] = coap_pkt->code; - option = coap_pkt->buffer + 1; + OC_DBG("Inner CoAP code: %d", packet->code); + packet->buffer[0] = packet->code; + option = packet->buffer + 1; ++header_length_calculation; - if (header_length_calculation > COAP_MAX_HEADER_SIZE) { - OC_ERR("Serialized header length %u exceeds COAP_MAX_HEADER_SIZE %u-UDP", - (unsigned int)(header_length_calculation), COAP_MAX_HEADER_SIZE); + if (!coap_oscore_check_packet_header_size(header_length_calculation, + buffer_size)) { + OC_ERR("cannot serialize inner packet"); goto exit; } } @@ -1305,33 +1310,31 @@ coap_oscore_serialize_message(void *packet, uint8_t *buffer, bool inner, option += option_length; /* Pack payload */ - if ((option - coap_pkt->buffer) <= COAP_MAX_HEADER_SIZE) { - /* Payload marker */ - if (coap_pkt->payload_len > 0) { - *option = 0xFF; - ++option; - memmove(option, coap_pkt->payload, coap_pkt->payload_len); - } - OC_DBG("Serialized payload:"); - OC_LOGbytes(option, coap_pkt->payload_len); - } else { - /* an error occurred: caller must check for !=0 */ - OC_WRN("Serialized header length %u exceeds COAP_MAX_HEADER_SIZE %u", - (unsigned int)(option - coap_pkt->buffer), COAP_MAX_HEADER_SIZE); + size_t header_len = (size_t)(option - packet->buffer); + if (!coap_oscore_check_packet_header_size(header_len, buffer_size)) { + OC_ERR("cannot serialize packet"); goto exit; } - OC_DBG("-Done %u B (header len %u, payload len %u)-", - (unsigned int)(coap_pkt->payload_len + option - buffer), - (unsigned int)(option - buffer), (unsigned int)coap_pkt->payload_len); + /* Payload marker */ + if (packet->payload_len > 0) { + *option = 0xFF; + ++option; + memmove(option, packet->payload, packet->payload_len); + } + OC_DBG("Serialized payload:"); + OC_LOGbytes(option, packet->payload_len); - OC_DBG("Dump"); - OC_LOGbytes(coap_pkt->buffer, (coap_pkt->payload_len + option - buffer)); + OC_DBG("-Done %zu B (header len %zu, payload len %u)-", + (size_t)(packet->payload_len + option - buffer), + (size_t)(option - buffer), (unsigned)packet->payload_len); - return (option - buffer) + coap_pkt->payload_len; /* packet length */ + OC_DBG("Dump"); + OC_LOGbytes(packet->buffer, (packet->payload_len + option - buffer)); + return (size_t)((option - buffer) + packet->payload_len); exit: - coap_pkt->buffer = NULL; + packet->buffer = NULL; return 0; } /*---------------------------------------------------------------------------*/ @@ -1354,9 +1357,11 @@ coap_send_message(oc_message_t *message) } size_t -coap_serialize_message(void *packet, uint8_t *buffer) +coap_serialize_message(coap_packet_t *packet, uint8_t *buffer, + size_t buffer_size) { - return coap_oscore_serialize_message(packet, buffer, true, true, false); + return coap_oscore_serialize_message(packet, buffer, buffer_size, true, true, + false); } /*---------------------------------------------------------------------------*/ coap_status_t diff --git a/messaging/coap/coap.h b/messaging/coap/coap.h index 2d336b83ff..e084cfe9ac 100644 --- a/messaging/coap/coap.h +++ b/messaging/coap/coap.h @@ -58,6 +58,7 @@ #include "port/oc_connectivity.h" #include "port/oc_log_internal.h" #include "port/oc_random.h" +#include "util/oc_compiler.h" #ifdef OC_OSCORE #include "oscore_constants.h" @@ -176,12 +177,17 @@ typedef enum { void coap_init_connection(void); uint16_t coap_get_mid(void); -void coap_udp_init_message(void *packet, coap_message_type_t type, uint8_t code, - uint16_t mid); -size_t coap_serialize_message(void *packet, uint8_t *buffer); -size_t coap_oscore_serialize_message(void *packet, uint8_t *buffer, bool inner, - bool outer, bool oscore); -void coap_send_message(oc_message_t *message); +void coap_udp_init_message(coap_packet_t *packet, coap_message_type_t type, + uint8_t code, uint16_t mid) OC_NONNULL(); + +size_t coap_serialize_message(coap_packet_t *packet, uint8_t *buffer, + size_t buffer_size); + +size_t coap_oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer, + size_t buffer_size, bool inner, bool outer, + bool oscore); + +void coap_send_message(oc_message_t *message) OC_NONNULL(); /** * @brief Parse CoAP message options @@ -293,7 +299,7 @@ size_t coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer); #ifdef OC_TCP -void coap_tcp_init_message(void *packet, uint8_t code); +void coap_tcp_init_message(coap_packet_t *packet, uint8_t code) OC_NONNULL(); size_t coap_tcp_get_packet_size(const uint8_t *data); diff --git a/messaging/coap/coap_signal.c b/messaging/coap/coap_signal.c index 503d23c783..38e3a63dfa 100644 --- a/messaging/coap/coap_signal.c +++ b/messaging/coap/coap_signal.c @@ -42,7 +42,8 @@ coap_send_signal_message(const oc_endpoint_t *endpoint, coap_packet_t *packet) memcpy(&message->endpoint, endpoint, sizeof(oc_endpoint_t)); - message->length = coap_serialize_message(packet, message->data); + message->length = + coap_serialize_message(packet, message->data, oc_message_buffer_size()); oc_send_message(message); return 1; @@ -105,7 +106,8 @@ coap_send_ping_message(const oc_endpoint_t *endpoint, uint8_t custody_option, if (!t) { return 0; } - t->message->length = coap_serialize_message(ping_pkt, t->message->data); + t->message->length = coap_serialize_message(ping_pkt, t->message->data, + oc_message_buffer_size()); OC_DBG("send ping signal message."); coap_send_transaction(t); diff --git a/messaging/coap/engine.c b/messaging/coap/engine.c index 844d4d50af..172f2d8158 100644 --- a/messaging/coap/engine.c +++ b/messaging/coap/engine.c @@ -122,7 +122,8 @@ coap_send_empty_response(coap_message_type_t type, uint16_t mid, if (token && token_len > 0) { coap_set_token(msg, token, token_len); } - size_t len = coap_serialize_message(msg, message->data); + size_t len = + coap_serialize_message(msg, message->data, oc_message_buffer_size()); if (len > 0) { message->length = len; coap_send_message(message); @@ -882,8 +883,8 @@ coap_receive(oc_message_t *msg) memcpy(transaction->token, response->token, response->token_len); transaction->token_len = response->token_len; } - transaction->message->length = - coap_serialize_message(response, transaction->message->data); + transaction->message->length = coap_serialize_message( + response, transaction->message->data, oc_message_buffer_size()); if (transaction->message->length > 0) { coap_send_transaction(transaction); } else { diff --git a/messaging/coap/observe.c b/messaging/coap/observe.c index 30a27422d9..759753dd2d 100644 --- a/messaging/coap/observe.c +++ b/messaging/coap/observe.c @@ -51,6 +51,7 @@ #ifdef OC_SERVER +#include "api/oc_buffer_internal.h" #include "oc_api.h" #include "observe.h" #include "separate.h" @@ -394,8 +395,8 @@ send_cancellation_notification(coap_observer_t *obs, uint8_t code) coap_get_mid(), obs->token, obs->token_len, &obs->endpoint); if (transaction) { notification->mid = transaction->mid; - transaction->message->length = - coap_serialize_message(notification, transaction->message->data); + transaction->message->length = coap_serialize_message( + notification, transaction->message->data, oc_message_buffer_size()); if (transaction->message->length > 0) { coap_send_transaction(transaction); } else { @@ -587,8 +588,8 @@ send_notification(coap_observer_t *obs, oc_response_t *response, if (transaction) { obs->last_mid = transaction->mid; notification->mid = transaction->mid; - transaction->message->length = - coap_serialize_message(notification, transaction->message->data); + transaction->message->length = coap_serialize_message( + notification, transaction->message->data, oc_message_buffer_size()); if (transaction->message->length > 0) { coap_send_transaction(transaction); } else { diff --git a/messaging/coap/oscore.c b/messaging/coap/oscore.c index 438b29e0ea..b772c59828 100644 --- a/messaging/coap/oscore.c +++ b/messaging/coap/oscore.c @@ -53,7 +53,8 @@ oscore_send_error(const coap_packet_t *packet, uint8_t code, coap_set_token(&msg, packet->token, packet->token_len); } coap_set_header_max_age(&msg, 0); - size_t len = coap_serialize_message(&msg, message->data); + size_t len = + coap_serialize_message(&msg, message->data, oc_message_buffer_size()); if (len > 0) { message->length = len; coap_send_message(message); @@ -374,15 +375,19 @@ coap_serialize_oscore_option(unsigned int *current_number, } size_t -oscore_serialize_plaintext(coap_packet_t *packet, uint8_t *buffer) +oscore_serialize_plaintext(coap_packet_t *packet, uint8_t *buffer, + size_t buffer_size) { - return coap_oscore_serialize_message(packet, buffer, true, false, true); + return coap_oscore_serialize_message(packet, buffer, buffer_size, true, false, + true); } size_t -oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer) +oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer, + size_t buffer_size) { - return coap_oscore_serialize_message(packet, buffer, false, true, true); + return coap_oscore_serialize_message(packet, buffer, buffer_size, false, true, + true); } coap_status_t diff --git a/messaging/coap/oscore.h b/messaging/coap/oscore.h index a983b2e487..e93e831200 100644 --- a/messaging/coap/oscore.h +++ b/messaging/coap/oscore.h @@ -50,8 +50,10 @@ coap_status_t oscore_parse_inner_message(uint8_t *data, size_t data_len, coap_packet_t *packet); coap_status_t oscore_parse_outer_message(oc_message_t *msg, coap_packet_t *packet); -size_t oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer); -size_t oscore_serialize_plaintext(coap_packet_t *packet, uint8_t *buffer); +size_t oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer, + size_t buffer_size); +size_t oscore_serialize_plaintext(coap_packet_t *packet, uint8_t *buffer, + size_t buffer_size); #ifdef __cplusplus } diff --git a/messaging/coap/separate.c b/messaging/coap/separate.c index 47ad869ff6..df912183d3 100644 --- a/messaging/coap/separate.c +++ b/messaging/coap/separate.c @@ -150,7 +150,8 @@ coap_separate_accept(const coap_packet_t *request, oc_message_t *message = oc_message_allocate_outgoing(); if (message != NULL) { memcpy(&message->endpoint, endpoint, sizeof(oc_endpoint_t)); - message->length = coap_serialize_message(ack, message->data); + message->length = + coap_serialize_message(ack, message->data, oc_message_buffer_size()); bool success = false; if (message->length > 0) { coap_send_message(message); diff --git a/messaging/coap/unittest/coapsignaltest.cpp b/messaging/coap/unittest/coapsignaltest.cpp index 389c33a895..1975715a73 100644 --- a/messaging/coap/unittest/coapsignaltest.cpp +++ b/messaging/coap/unittest/coapsignaltest.cpp @@ -618,7 +618,8 @@ TEST_F(TestCoapSignal, SignalSerializeParseTest_CSM) std::vector buffer; buffer.reserve(OC_PDU_SIZE); - size_t buffer_len = coap_serialize_message(&packet, buffer.data()); + size_t buffer_len = + coap_serialize_message(&packet, buffer.data(), buffer.capacity()); coap_packet_t parse_packet{}; coap_status_t ret = @@ -638,7 +639,8 @@ TEST_F(TestCoapSignal, SignalSerializeParseTest_PING) std::vector buffer; buffer.reserve(OC_PDU_SIZE); - size_t buffer_len = coap_serialize_message(&packet, buffer.data()); + size_t buffer_len = + coap_serialize_message(&packet, buffer.data(), buffer.capacity()); coap_packet_t parse_packet{}; coap_status_t ret = @@ -657,7 +659,8 @@ TEST_F(TestCoapSignal, SignalSerializeParseTest_PONG) std::vector buffer; buffer.reserve(OC_PDU_SIZE); - size_t buffer_len = coap_serialize_message(&packet, buffer.data()); + size_t buffer_len = + coap_serialize_message(&packet, buffer.data(), buffer.capacity()); coap_packet_t parse_packet{}; coap_status_t ret = @@ -680,7 +683,8 @@ TEST_F(TestCoapSignal, SignalSerializeParseTest_RELEASE) std::vector buffer; buffer.reserve(OC_PDU_SIZE); - size_t buffer_len = coap_serialize_message(&packet, buffer.data()); + size_t buffer_len = + coap_serialize_message(&packet, buffer.data(), buffer.capacity()); coap_packet_t parse_packet{}; coap_status_t ret = @@ -707,7 +711,8 @@ TEST_F(TestCoapSignal, SignalSerializeParseTest_ABORT) std::vector buffer; buffer.reserve(OC_PDU_SIZE); - size_t buffer_len = coap_serialize_message(&packet, buffer.data()); + size_t buffer_len = + coap_serialize_message(&packet, buffer.data(), buffer.capacity()); coap_packet_t parse_packet{}; coap_status_t ret = diff --git a/port/unittest/connectivitytest.cpp b/port/unittest/connectivitytest.cpp index 4568121a6f..4567ecef12 100644 --- a/port/unittest/connectivitytest.cpp +++ b/port/unittest/connectivitytest.cpp @@ -16,6 +16,7 @@ * ******************************************************************/ +#include "api/oc_buffer_internal.h" #include "api/oc_tcp_internal.h" #include "api/oc_session_events_internal.h" #include "messaging/coap/coap.h" @@ -445,7 +446,8 @@ TEST_F(TestConnectivityWithServer, oc_tcp_update_csm_state_P) std::array payload{ "connect" }; packet.payload = payload.data(); packet.payload_len = payload.size(); - msg->length = coap_serialize_message(&packet, msg->data); + msg->length = + coap_serialize_message(&packet, msg->data, oc_message_buffer_size()); oc_send_buffer(msg); oc_message_unref(msg); @@ -576,7 +578,8 @@ TEST_F(TestConnectivityWithServer, oc_tcp_send_buffer2) oc_message_t *msg = oc_allocate_message(); memcpy(&msg->endpoint, ep, sizeof(oc_endpoint_t)); - msg->length = coap_serialize_message(&packet, msg->data); + msg->length = + coap_serialize_message(&packet, msg->data, oc_message_buffer_size()); EXPECT_EQ(msg->length, oc_send_buffer2(msg, false)); oc_message_unref(msg); diff --git a/security/oc_oscore_engine.c b/security/oc_oscore_engine.c index dc77958e2a..d0b84dec43 100644 --- a/security/oc_oscore_engine.c +++ b/security/oc_oscore_engine.c @@ -279,7 +279,8 @@ oscore_parse_message(oc_message_t *message) OC_DBG("### serializing CoAP message ###"); /* Serialize fully decrypted CoAP packet to message->data buffer */ - message->length = coap_serialize_message(&coap_pkt, message->data); + message->length = + coap_serialize_message(&coap_pkt, message->data, oc_message_buffer_size()); OC_DBG("### serialized decrypted CoAP message to dispatch to the CoAP " "layer ###"); @@ -443,15 +444,17 @@ oc_oscore_send_multicast_message(oc_message_t *message) /* Serialize OSCORE plaintext at offset COAP_MAX_HEADER_SIZE (code, inner options, payload) */ - size_t plaintext_size = oscore_serialize_plaintext( - coap_pkt, message->data + COAP_MAX_HEADER_SIZE); + uint8_t *buffer = message->data + COAP_MAX_HEADER_SIZE; + size_t buffer_size = oc_message_buffer_size() - COAP_MAX_HEADER_SIZE; + size_t plaintext_size = + oscore_serialize_plaintext(coap_pkt, buffer, buffer_size); OC_DBG("### serialized OSCORE plaintext: %zd bytes ###", plaintext_size); /* Set the OSCORE packet payload to point to location of the serialized inner message. */ - coap_pkt->payload = message->data + COAP_MAX_HEADER_SIZE; + coap_pkt->payload = buffer; coap_pkt->payload_len = plaintext_size; /* Encrypt OSCORE plaintext */ @@ -480,7 +483,8 @@ oc_oscore_send_multicast_message(oc_message_t *message) /* Serialize OSCORE message to oc_message_t */ OC_DBG("### serializing OSCORE message ###"); - message->length = oscore_serialize_message(coap_pkt, message->data); + message->length = oscore_serialize_message(coap_pkt, message->data, + oc_message_buffer_size()); OC_DBG("### serialized OSCORE message ###"); } else { OC_ERR("*** could not find group OSCORE context ***"); @@ -737,15 +741,17 @@ oc_oscore_send_message(oc_message_t *msg) /* Serialize OSCORE plaintext at offset COAP_MAX_HEADER_SIZE (code, inner options, payload) */ - size_t plaintext_size = oscore_serialize_plaintext( - coap_pkt, message->data + COAP_MAX_HEADER_SIZE); + uint8_t *buffer = message->data + COAP_MAX_HEADER_SIZE; + size_t buffer_size = oc_message_buffer_size() - COAP_MAX_HEADER_SIZE; + size_t plaintext_size = + oscore_serialize_plaintext(coap_pkt, buffer, buffer_size); OC_DBG("### serialized OSCORE plaintext: %zd bytes ###", plaintext_size); /* Set the OSCORE packet payload to point to location of the serialized inner message. */ - coap_pkt->payload = message->data + COAP_MAX_HEADER_SIZE; + coap_pkt->payload = buffer; coap_pkt->payload_len = plaintext_size; /* Encrypt OSCORE plaintext */ @@ -789,7 +795,8 @@ oc_oscore_send_message(oc_message_t *msg) /* Serialize OSCORE message to oc_message_t */ OC_DBG("### serializing OSCORE message ###"); - message->length = oscore_serialize_message(coap_pkt, message->data); + message->length = oscore_serialize_message(coap_pkt, message->data, + oc_message_buffer_size()); OC_DBG("### serialized OSCORE message ###"); oc_free_string(&proxy_uri); } diff --git a/security/oc_tls.c b/security/oc_tls.c index 18075a65ef..7ea76288db 100644 --- a/security/oc_tls.c +++ b/security/oc_tls.c @@ -762,7 +762,7 @@ get_psk_cb(void *data, mbedtls_ssl_context *ssl, const unsigned char *identity, OC_CREDUSAGE_NULL, peer->endpoint.device); if (cred != NULL) { OC_DBG("oc_tls: Found peer credential"); - memcpy(peer->uuid.id, identity, 16); + memcpy(peer->uuid.id, identity, OC_UUID_ID_SIZE); OC_DBG("oc_tls: Setting the key:"); OC_LOGbytes(oc_string(cred->privatedata.data), oc_string_len(cred->privatedata.data)); diff --git a/security/unittest/oscore_test.cpp b/security/unittest/oscore_test.cpp index 79b2de0f02..341e820a82 100644 --- a/security/unittest/oscore_test.cpp +++ b/security/unittest/oscore_test.cpp @@ -625,11 +625,14 @@ TEST_F(TestOSCORE, ClientRequest1_P) EXPECT_STREQ(testvec, "8368456e63727970743040488501810a40411440"); /* Verify plaintext: 0x01b3747631 (5 bytes) */ - size_t plaintext_len = oscore_serialize_plaintext(coap_pkt, buffer + 256); + uint8_t *payload = buffer + 256; + size_t payload_size = sizeof(buffer) - 256; + size_t plaintext_len = + oscore_serialize_plaintext(coap_pkt, payload, payload_size); testvec_len = 512; - EXPECT_EQ(oc_conv_byte_array_to_hex_string(buffer + 256, plaintext_len, - testvec, &testvec_len), + EXPECT_EQ(oc_conv_byte_array_to_hex_string(payload, plaintext_len, testvec, + &testvec_len), 0); EXPECT_STREQ(testvec, "01b3747631"); @@ -644,18 +647,18 @@ TEST_F(TestOSCORE, ClientRequest1_P) EXPECT_STREQ(testvec, "4622d4dd6d944168eefb549868"); /* Verify ciphertext: 0x612f1092f1776f1c1668b3825e (13 bytes) */ - EXPECT_EQ(oc_oscore_encrypt(buffer + 256, plaintext_len, OSCORE_AEAD_TAG_LEN, - skey, skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, - AAD_len, buffer + 256), + EXPECT_EQ(oc_oscore_encrypt(payload, plaintext_len, OSCORE_AEAD_TAG_LEN, skey, + skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, + AAD_len, payload), 0); testvec_len = 512; EXPECT_EQ( oc_conv_byte_array_to_hex_string( - buffer + 256, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), + payload, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), 0); EXPECT_STREQ(testvec, "612f1092f1776f1c1668b3825e"); - coap_pkt->payload = buffer + 256; + coap_pkt->payload = payload; coap_pkt->payload_len = plaintext_len + OSCORE_AEAD_TAG_LEN; /* Set the Outer code for the OSCORE packet (POST/FETCH:2.04/2.05) */ @@ -665,7 +668,7 @@ TEST_F(TestOSCORE, ClientRequest1_P) coap_set_header_oscore(coap_pkt, piv, piv_len, nullptr, 0, nullptr, 0); /* Serialize OSCORE message to oc_message_t */ - buffer_len = oscore_serialize_message(coap_pkt, buffer); + buffer_len = oscore_serialize_message(coap_pkt, buffer, sizeof(buffer)); /* Verify protected CoAP request (OSCORE message): 0x44025d1f00003974396c6f6 3616c686f7374620914ff612f1092f1776f1c1668b3825e (35 bytes) @@ -741,11 +744,14 @@ TEST_F(TestOSCORE, ClientRequest2_P) EXPECT_STREQ(testvec, "8368456e63727970743040498501810a4100411440"); /* Verify plaintext: 0x01b3747631 (5 bytes) */ - size_t plaintext_len = oscore_serialize_plaintext(coap_pkt, buffer + 256); + uint8_t *payload = buffer + 256; + size_t payload_size = sizeof(buffer) - 256; + size_t plaintext_len = + oscore_serialize_plaintext(coap_pkt, payload, payload_size); testvec_len = 512; - EXPECT_EQ(oc_conv_byte_array_to_hex_string(buffer + 256, plaintext_len, - testvec, &testvec_len), + EXPECT_EQ(oc_conv_byte_array_to_hex_string(payload, plaintext_len, testvec, + &testvec_len), 0); EXPECT_STREQ(testvec, "01b3747631"); @@ -759,18 +765,18 @@ TEST_F(TestOSCORE, ClientRequest2_P) EXPECT_STREQ(testvec, "bf35ae297d2dace910c52e99ed"); /* Verify ciphertext: 0x4ed339a5a379b0b8bc731fffb0 (13 bytes) */ - EXPECT_EQ(oc_oscore_encrypt(buffer + 256, plaintext_len, OSCORE_AEAD_TAG_LEN, - skey, skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, - AAD_len, buffer + 256), + EXPECT_EQ(oc_oscore_encrypt(payload, plaintext_len, OSCORE_AEAD_TAG_LEN, skey, + skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, + AAD_len, payload), 0); testvec_len = 512; EXPECT_EQ( oc_conv_byte_array_to_hex_string( - buffer + 256, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), + payload, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), 0); EXPECT_STREQ(testvec, "4ed339a5a379b0b8bc731fffb0"); - coap_pkt->payload = buffer + 256; + coap_pkt->payload = payload; coap_pkt->payload_len = plaintext_len + OSCORE_AEAD_TAG_LEN; /* Set the Outer code for the OSCORE packet (POST/FETCH:2.04/2.05) */ @@ -780,7 +786,7 @@ TEST_F(TestOSCORE, ClientRequest2_P) coap_set_header_oscore(coap_pkt, piv, piv_len, sid, 1, nullptr, 0); /* Serialize OSCORE message to oc_message_t */ - buffer_len = oscore_serialize_message(coap_pkt, buffer); + buffer_len = oscore_serialize_message(coap_pkt, buffer, sizeof(buffer)); /* Protected CoAP request (OSCORE message): 0x440271c30000b932396c6f6 3616c686f737463091400ff4ed339a5a379b0b8bc731fffb0 (36 bytes) @@ -862,11 +868,14 @@ TEST_F(TestOSCORE, ClientRequest3_P) EXPECT_STREQ(testvec, "8368456e63727970743040488501810a40411440"); /* Verify plaintext: 0x01b3747631 (5 bytes) */ - size_t plaintext_len = oscore_serialize_plaintext(coap_pkt, buffer + 256); + uint8_t *payload = buffer + 256; + size_t payload_size = sizeof(buffer) - 256; + size_t plaintext_len = + oscore_serialize_plaintext(coap_pkt, payload, payload_size); testvec_len = 512; - EXPECT_EQ(oc_conv_byte_array_to_hex_string(buffer + 256, plaintext_len, - testvec, &testvec_len), + EXPECT_EQ(oc_conv_byte_array_to_hex_string(payload, plaintext_len, testvec, + &testvec_len), 0); EXPECT_STREQ(testvec, "01b3747631"); @@ -881,18 +890,18 @@ TEST_F(TestOSCORE, ClientRequest3_P) EXPECT_STREQ(testvec, "2ca58fb85ff1b81c0b7181b84a"); /* Verify ciphertext: 0x72cd7273fd331ac45cffbe55c3 (13 bytes) */ - EXPECT_EQ(oc_oscore_encrypt(buffer + 256, plaintext_len, OSCORE_AEAD_TAG_LEN, - skey, skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, - AAD_len, buffer + 256), + EXPECT_EQ(oc_oscore_encrypt(payload, plaintext_len, OSCORE_AEAD_TAG_LEN, skey, + skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, + AAD_len, payload), 0); testvec_len = 512; EXPECT_EQ( oc_conv_byte_array_to_hex_string( - buffer + 256, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), + payload, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), 0); EXPECT_STREQ(testvec, "72cd7273fd331ac45cffbe55c3"); - coap_pkt->payload = buffer + 256; + coap_pkt->payload = payload; coap_pkt->payload_len = plaintext_len + OSCORE_AEAD_TAG_LEN; /* Set the Outer code for the OSCORE packet (POST/FETCH:2.04/2.05) */ @@ -902,7 +911,7 @@ TEST_F(TestOSCORE, ClientRequest3_P) coap_set_header_oscore(coap_pkt, piv, piv_len, nullptr, 0, idctx, idctx_len); /* Serialize OSCORE message to oc_message_t */ - buffer_len = oscore_serialize_message(coap_pkt, buffer); + buffer_len = oscore_serialize_message(coap_pkt, buffer, sizeof(buffer)); /* Protected CoAP request (OSCORE message): 0x44022f8eef9bbf7a396c6f63616c686f73746b19140837cbf3210017a2d3ff @@ -976,11 +985,14 @@ TEST_F(TestOSCORE, ServerResponse1_P) EXPECT_STREQ(testvec, "8368456e63727970743040488501810a40411440"); /* Verify plaintext: 0x45ff48656c6c6f20576f726c6421 (14 bytes) */ - size_t plaintext_len = oscore_serialize_plaintext(coap_pkt, buffer + 256); + uint8_t *payload = buffer + 256; + size_t payload_size = sizeof(buffer) - 256; + size_t plaintext_len = + oscore_serialize_plaintext(coap_pkt, payload, payload_size); testvec_len = 512; - EXPECT_EQ(oc_conv_byte_array_to_hex_string(buffer + 256, plaintext_len, - testvec, &testvec_len), + EXPECT_EQ(oc_conv_byte_array_to_hex_string(payload, plaintext_len, testvec, + &testvec_len), 0); EXPECT_STREQ(testvec, "45ff48656c6c6f20576f726c6421"); @@ -997,18 +1009,18 @@ TEST_F(TestOSCORE, ServerResponse1_P) /* Verify ciphertext: 0xdbaad1e9a7e7b2a813d3c31524378303cdafae119106 (22 bytes) */ - EXPECT_EQ(oc_oscore_encrypt(buffer + 256, plaintext_len, OSCORE_AEAD_TAG_LEN, - skey, skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, - AAD_len, buffer + 256), + EXPECT_EQ(oc_oscore_encrypt(payload, plaintext_len, OSCORE_AEAD_TAG_LEN, skey, + skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, + AAD_len, payload), 0); testvec_len = 512; EXPECT_EQ( oc_conv_byte_array_to_hex_string( - buffer + 256, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), + payload, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), 0); EXPECT_STREQ(testvec, "dbaad1e9a7e7b2a813d3c31524378303cdafae119106"); - coap_pkt->payload = buffer + 256; + coap_pkt->payload = payload; coap_pkt->payload_len = plaintext_len + OSCORE_AEAD_TAG_LEN; /* Set the Outer code for the OSCORE packet (POST/FETCH:2.04/2.05) */ @@ -1018,7 +1030,7 @@ TEST_F(TestOSCORE, ServerResponse1_P) coap_set_header_oscore(coap_pkt, nullptr, 0, nullptr, 0, nullptr, 0); /* Serialize OSCORE message to oc_message_t */ - buffer_len = oscore_serialize_message(coap_pkt, buffer); + buffer_len = oscore_serialize_message(coap_pkt, buffer, sizeof(buffer)); /* Protected CoAP response (OSCORE message): @@ -1090,11 +1102,14 @@ TEST_F(TestOSCORE, ServerResponse2_P) EXPECT_STREQ(testvec, "8368456e63727970743040488501810a40411440"); /* Verify plaintext: 0x45ff48656c6c6f20576f726c6421 (14 bytes) */ - size_t plaintext_len = oscore_serialize_plaintext(coap_pkt, buffer + 256); + uint8_t *payload = buffer + 256; + size_t payload_size = sizeof(buffer) - 256; + size_t plaintext_len = + oscore_serialize_plaintext(coap_pkt, payload, payload_size); testvec_len = 512; - EXPECT_EQ(oc_conv_byte_array_to_hex_string(buffer + 256, plaintext_len, - testvec, &testvec_len), + EXPECT_EQ(oc_conv_byte_array_to_hex_string(payload, plaintext_len, testvec, + &testvec_len), 0); EXPECT_STREQ(testvec, "45ff48656c6c6f20576f726c6421"); @@ -1115,18 +1130,18 @@ TEST_F(TestOSCORE, ServerResponse2_P) /* Verify ciphertext: 0x4d4c13669384b67354b2b6175ff4b8658c666a6cf88e (22 bytes) */ - EXPECT_EQ(oc_oscore_encrypt(buffer + 256, plaintext_len, OSCORE_AEAD_TAG_LEN, - skey, skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, - AAD_len, buffer + 256), + EXPECT_EQ(oc_oscore_encrypt(payload, plaintext_len, OSCORE_AEAD_TAG_LEN, skey, + skey_len, nonce, OSCORE_AEAD_NONCE_LEN, AAD, + AAD_len, payload), 0); testvec_len = 512; EXPECT_EQ( oc_conv_byte_array_to_hex_string( - buffer + 256, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), + payload, plaintext_len + OSCORE_AEAD_TAG_LEN, testvec, &testvec_len), 0); EXPECT_STREQ(testvec, "4d4c13669384b67354b2b6175ff4b8658c666a6cf88e"); - coap_pkt->payload = buffer + 256; + coap_pkt->payload = payload; coap_pkt->payload_len = plaintext_len + OSCORE_AEAD_TAG_LEN; /* Set the Outer code for the OSCORE packet (POST/FETCH:2.04/2.05) */ @@ -1136,7 +1151,7 @@ TEST_F(TestOSCORE, ServerResponse2_P) coap_set_header_oscore(coap_pkt, piv, 1, nullptr, 0, nullptr, 0); /* Serialize OSCORE message to oc_message_t */ - buffer_len = oscore_serialize_message(coap_pkt, buffer); + buffer_len = oscore_serialize_message(coap_pkt, buffer, sizeof(buffer)); /* Protected CoAP response (OSCORE message): 0x64445d1f00003974920100