diff --git a/CMakeLists.txt b/CMakeLists.txt index 82eeca2a..0fb37e27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -492,6 +492,7 @@ target_sources( ${CMAKE_CURRENT_LIST_DIR}/src/coap_tcp.c ${CMAKE_CURRENT_LIST_DIR}/src/coap_time.c ${CMAKE_CURRENT_LIST_DIR}/src/coap_uri.c + ${CMAKE_CURRENT_LIST_DIR}/src/coap_ws.c ${CMAKE_CURRENT_LIST_DIR}/src/block.c ${CMAKE_CURRENT_LIST_DIR}/src/net.c ${CMAKE_CURRENT_LIST_DIR}/src/pdu.c @@ -529,7 +530,8 @@ target_sources( ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_str.h ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_subscribe.h ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_time.h - ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_uri.h) + ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_uri.h + ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_ws.h) target_include_directories( ${COAP_LIBRARY_NAME} PUBLIC # config headers are generated during configuration time diff --git a/Makefile.am b/Makefile.am index 65f0b26b..5b46344a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -85,6 +85,7 @@ EXTRA_DIST = \ include/coap$(LIBCOAP_API_VERSION)/coap_uri_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_uthash_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_utlist_internal.h \ + include/coap$(LIBCOAP_API_VERSION)/coap_ws_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap.h.in \ include/coap$(LIBCOAP_API_VERSION)/coap.h.windows \ include/coap$(LIBCOAP_API_VERSION)/coap.h.windows.in \ @@ -176,6 +177,7 @@ libcoap_@LIBCOAP_NAME_SUFFIX@_la_SOURCES = \ src/coap_time.c \ src/coap_tinydtls.c \ src/coap_uri.c \ + src/coap_ws.c \ src/net.c \ src/pdu.c \ src/resource.c @@ -220,7 +222,8 @@ libcoap_include_HEADERS = \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_str.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_subscribe.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_time.h \ - $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_uri.h + $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_uri.h \ + $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_ws.h ## Instruct libtool to include API version information in the generated shared ## library file (.so). The library ABI version will later defined in configure.ac, diff --git a/examples/coap-client.c b/examples/coap-client.c index 7383b1c1..008de79e 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -351,13 +351,12 @@ nack_handler(coap_session_t *session COAP_UNUSED, case COAP_NACK_NOT_DELIVERABLE: case COAP_NACK_RST: case COAP_NACK_TLS_FAILED: + case COAP_NACK_TLS_LAYER_FAILED: coap_log_err("cannot send CoAP pdu\n"); quit = 1; break; case COAP_NACK_ICMP_ISSUE: case COAP_NACK_BAD_RESPONSE: - case COAP_NACK_TLS_LAYER_FAILED: - case COAP_NACK_SESSION_LAYER_FAILED: default: ; } diff --git a/examples/coap-server.c b/examples/coap-server.c index 96e5a8b5..92e284d0 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -863,6 +863,7 @@ get_ongoing_proxy_session(coap_session_t *session, coap_address_t dst; coap_uri_scheme_t scheme; + coap_proto_t proto; static char client_sni[256]; coap_str_const_t server; uint16_t port; @@ -888,6 +889,27 @@ get_ongoing_proxy_session(coap_session_t *session, scheme = uri->scheme; } + switch(scheme) { + case COAP_URI_SCHEME_COAP: + proto = COAP_PROTO_UDP; + break; + case COAP_URI_SCHEME_COAPS: + proto = COAP_PROTO_DTLS; + break; + case COAP_URI_SCHEME_COAP_TCP: + proto = COAP_PROTO_TCP; + break; + case COAP_URI_SCHEME_COAPS_TCP: + proto = COAP_PROTO_TLS; + break; + case COAP_URI_SCHEME_LAST: + default: + case COAP_URI_SCHEME_HTTP: + case COAP_URI_SCHEME_HTTPS: + coap_log_err("http:// or https:// not supported\n"); + return NULL; + } + /* resolve destination address where data should be sent */ info_list = coap_resolve_address_info(&server, port, port, 0, @@ -905,9 +927,7 @@ get_ongoing_proxy_session(coap_session_t *session, case COAP_URI_SCHEME_COAP: case COAP_URI_SCHEME_COAP_TCP: new_proxy_list->ongoing = - coap_new_client_session(context, NULL, &dst, - scheme == COAP_URI_SCHEME_COAP ? - COAP_PROTO_UDP : COAP_PROTO_TCP); + coap_new_client_session(context, NULL, &dst, proto); break; case COAP_URI_SCHEME_COAPS: case COAP_URI_SCHEME_COAPS_TCP: @@ -923,19 +943,13 @@ get_ongoing_proxy_session(coap_session_t *session, coap_dtls_pki_t *dtls_pki = setup_pki(context, COAP_DTLS_ROLE_CLIENT, client_sni); new_proxy_list->ongoing = - coap_new_client_session_pki(context, NULL, &dst, - scheme == COAP_URI_SCHEME_COAPS ? - COAP_PROTO_DTLS : COAP_PROTO_TLS, - dtls_pki); + coap_new_client_session_pki(context, NULL, &dst, proto, dtls_pki); } else { /* Use our defined PSK */ coap_dtls_cpsk_t *dtls_cpsk = setup_cpsk(client_sni); new_proxy_list->ongoing = - coap_new_client_session_psk2(context, NULL, &dst, - scheme == COAP_URI_SCHEME_COAPS ? - COAP_PROTO_DTLS : COAP_PROTO_TLS, - dtls_cpsk); + coap_new_client_session_psk2(context, NULL, &dst, proto, dtls_cpsk); } break; case COAP_URI_SCHEME_HTTP: @@ -1702,13 +1716,12 @@ proxy_nack_handler(coap_session_t *session, case COAP_NACK_NOT_DELIVERABLE: case COAP_NACK_RST: case COAP_NACK_TLS_FAILED: + case COAP_NACK_TLS_LAYER_FAILED: /* Need to remove any proxy associations */ remove_proxy_association(session, 1); break; case COAP_NACK_ICMP_ISSUE: case COAP_NACK_BAD_RESPONSE: - case COAP_NACK_TLS_LAYER_FAILED: - case COAP_NACK_SESSION_LAYER_FAILED: default: break; } @@ -2274,6 +2287,7 @@ get_context(const char *node, const char *port) { int have_ep = 0; uint16_t u_s_port = 5683; uint16_t s_port = 5684; + coap_uri_scheme_t scheme; int scheme_hint_bits = COAP_URI_SCHEME_ALL_COAP_BITS; ctx = coap_new_context(NULL); @@ -2293,16 +2307,41 @@ get_context(const char *node, const char *port) { u_s_port = atoi(port); s_port = u_s_port + 1; } - - if (cert_file == NULL && key_defined == 0) - scheme_hint_bits = COAP_URI_SCHEME_COAP_BIT | COAP_URI_SCHEME_COAP_TCP_BIT; + for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) { + switch (scheme) { + case COAP_URI_SCHEME_COAP: + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_COAPS: + if (!coap_dtls_is_supported() || (cert_file == NULL && key_defined == 0)) + continue; + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_COAP_TCP: + if (!coap_tcp_is_supported()) + continue; + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_COAPS_TCP: + if (!coap_tls_is_supported() || (cert_file == NULL && key_defined == 0)) + continue; + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_HTTP: + case COAP_URI_SCHEME_HTTPS: + case COAP_URI_SCHEME_LAST: + default: + continue; + } + } switch (use_unix_proto) { + /* For AF_UNIX, can only listen on a single endpoint */ case COAP_PROTO_UDP: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP; break; case COAP_PROTO_TCP: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_TCP; break; case COAP_PROTO_DTLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS; break; case COAP_PROTO_TLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_TCP; break; - case COAP_PROTO_NONE: + case COAP_PROTO_NONE: /* If -U was not defined */ case COAP_PROTO_LAST: default: break; @@ -2696,7 +2735,7 @@ main(int argc, char **argv) { char *group_if = NULL; coap_tick_t now; char addr_str[NI_MAXHOST] = "::"; - char port_str[NI_MAXSERV] = "5683"; + char *port_str = NULL; int opt; int mcast_per_resource = 0; coap_log_t log_level = COAP_LOG_WARN; @@ -2802,8 +2841,7 @@ main(int argc, char **argv) { resource_flags = COAP_RESOURCE_FLAGS_NOTIFY_NON; break; case 'p' : - strncpy(port_str, optarg, NI_MAXSERV-1); - port_str[NI_MAXSERV - 1] = '\0'; + port_str = optarg; break; case 'P': #if SERVER_CAN_PROXY diff --git a/examples/lwip/Makefile b/examples/lwip/Makefile index 03c53f93..6bae4b18 100644 --- a/examples/lwip/Makefile +++ b/examples/lwip/Makefile @@ -155,7 +155,8 @@ COAP_SRC = coap_address.c \ coap_tinydtls.c \ coap_str.c \ coap_tcp.c \ - coap_uri.c + coap_uri.c \ + coap_ws.c CN_COAP_OBJ =$(patsubst %.c,lib-client/%.o,$(COAP_SRC)) SN_COAP_OBJ =$(patsubst %.c,lib-server/%.o,$(COAP_SRC)) diff --git a/include/coap3/coap.h.in b/include/coap3/coap.h.in index 8ffb5f37..6e7f81ee 100644 --- a/include/coap3/coap.h.in +++ b/include/coap3/coap.h.in @@ -63,6 +63,7 @@ extern "C" { #include "coap@LIBCOAP_API_VERSION@/coap_subscribe.h" #include "coap@LIBCOAP_API_VERSION@/coap_time.h" #include "coap@LIBCOAP_API_VERSION@/coap_uri.h" +#include "coap@LIBCOAP_API_VERSION@/coap_ws.h" #ifdef __cplusplus } diff --git a/include/coap3/coap.h.windows b/include/coap3/coap.h.windows index 4f0528ed..c9664190 100644 --- a/include/coap3/coap.h.windows +++ b/include/coap3/coap.h.windows @@ -58,6 +58,7 @@ extern "C" { #include "coap3/coap_time.h" #include "coap3/coap_str.h" #include "coap3/coap_uri.h" +#include "coap3/coap_ws.h" #ifdef __cplusplus } diff --git a/include/coap3/coap.h.windows.in b/include/coap3/coap.h.windows.in index 2d6b8b63..520038fc 100644 --- a/include/coap3/coap.h.windows.in +++ b/include/coap3/coap.h.windows.in @@ -58,6 +58,7 @@ extern "C" { #include "coap@LIBCOAP_API_VERSION@/coap_time.h" #include "coap@LIBCOAP_API_VERSION@/coap_str.h" #include "coap@LIBCOAP_API_VERSION@/coap_uri.h" +#include "coap@LIBCOAP_API_VERSION@/coap_ws.h" #ifdef __cplusplus } diff --git a/include/coap3/coap_crypto_internal.h b/include/coap3/coap_crypto_internal.h index 9107710f..95a6a475 100644 --- a/include/coap3/coap_crypto_internal.h +++ b/include/coap3/coap_crypto_internal.h @@ -137,6 +137,21 @@ int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *data, coap_bin_const_t **hmac); +/** + * Create a hash of the provided data. + * + * @param alg The hash algorithm. + * @param data The data to hash. + * @param hash Where to put the hash result if successful. + * + * @return @c 1 if the data was successfully hashed, else @c 0. + * It is the responsibility of the caller to release the + * created hash. + */ +int coap_crypto_hash(cose_alg_t alg, + const coap_bin_const_t *data, + coap_bin_const_t **hash); + /** @} */ #endif /* COAP_CRYPTO_INTERNAL_H_ */ diff --git a/include/coap3/coap_internal.h b/include/coap3/coap_internal.h index 187c28f3..69c94ef4 100644 --- a/include/coap3/coap_internal.h +++ b/include/coap3/coap_internal.h @@ -112,5 +112,6 @@ typedef struct oscore_ctx_t oscore_ctx_t; #include "coap_uri_internal.h" #include "coap_utlist_internal.h" #include "coap_uthash_internal.h" +#include "coap_ws_internal.h" #endif /* COAP_INTERNAL_H_ */ diff --git a/include/coap3/coap_io.h b/include/coap3/coap_io.h index fc4dd3fd..79da4399 100644 --- a/include/coap3/coap_io.h +++ b/include/coap3/coap_io.h @@ -74,7 +74,6 @@ typedef enum { COAP_NACK_ICMP_ISSUE, COAP_NACK_BAD_RESPONSE, COAP_NACK_TLS_LAYER_FAILED, - COAP_NACK_SESSION_LAYER_FAILED, } coap_nack_reason_t; #endif /* COAP_IO_H_ */ diff --git a/include/coap3/coap_pdu_internal.h b/include/coap3/coap_pdu_internal.h index 0fecdf5d..1c3c7e79 100644 --- a/include/coap3/coap_pdu_internal.h +++ b/include/coap3/coap_pdu_internal.h @@ -200,7 +200,7 @@ size_t coap_pdu_parse_header_size(coap_proto_t proto, * @param data The raw data to parse as CoAP PDU. * @param length The actual size of @p data. * - * @return A value greater than zero on success or @c 0 on error. + * @return PDU size including token on success or @c 0 on error. */ size_t coap_pdu_parse_size(coap_proto_t proto, const uint8_t *data, diff --git a/include/coap3/coap_session_internal.h b/include/coap3/coap_session_internal.h index 37eb4c97..2d944a3e 100644 --- a/include/coap3/coap_session_internal.h +++ b/include/coap3/coap_session_internal.h @@ -20,6 +20,7 @@ #include "coap_internal.h" #include "coap_io_internal.h" +#include "coap_ws_internal.h" #define COAP_DEFAULT_SESSION_TIMEOUT 300 #define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND) diff --git a/include/coap3/coap_ws.h b/include/coap3/coap_ws.h new file mode 100644 index 00000000..64a7441a --- /dev/null +++ b/include/coap3/coap_ws.h @@ -0,0 +1,51 @@ +/* + * coap_ws.h -- WebSockets Transport Layer Support for libcoap + * + * Copyright (C) 2023 Olaf Bergmann + * Copyright (C) 2023 Jon Shallow + * + * SPDX-License-Identifier: BSD-2-Clause + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +/** + * @file coap_ws.h + * @brief CoAP WebSockets support + */ + +#ifndef COAP_WS_H_ +#define COAP_WS_H_ + +/** + * @ingroup application_api + * @defgroup ws WebSockets Support + * API for interfacing with WebSockets + * @{ + */ + +/** + * Check whether Websockets is available. + * + * @return @c 1 if support for WebSockets is available, or @c 0 otherwise. + */ +int coap_ws_is_supported(void); + +/** + * Check whether Secure WebSockets is available. + * + * @return @c 1 if support for Secure WebSockets is available, or @c 0 otherwise. + */ +int coap_wss_is_supported(void); + +/** + * Set the host for the HTTP Host: Header in the WebSockets Request. + * + * @return @c 1 if successful, else @c 0 if failure of some sort. + */ +int coap_ws_set_host_request(coap_session_t *session, coap_str_const_t *ws_host); + +/** @} */ + +#endif /* COAP_WS_H */ diff --git a/include/coap3/coap_ws_internal.h b/include/coap3/coap_ws_internal.h new file mode 100644 index 00000000..20b97d6a --- /dev/null +++ b/include/coap3/coap_ws_internal.h @@ -0,0 +1,32 @@ +/* + * coap_ws_internal.h -- WebSockets Transport Layer Support for libcoap + * + * Copyright (C) 2023 Olaf Bergmann + * Copyright (C) 2023 Jon Shallow + * + * SPDX-License-Identifier: BSD-2-Clause + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +/** + * @file coap_ws_internal.h + * @brief Internal CoAP WebSockets support + */ + +#ifndef COAP_WS_INTERNAL_H_ +#define COAP_WS_INTERNAL_H_ + +#include "coap_internal.h" + +/** + * @ingroup internal_api + * @defgroup ws_internal WebSockets Support + * Internal API for WebSockets Support + * @{ + */ + +/** @} */ + +#endif /* COAP_WS_INTERNAL_H */ diff --git a/libcoap-3.map b/libcoap-3.map index cb79319a..4450a1ae 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -267,6 +267,9 @@ global: coap_uri_into_options; coap_write_block_b_opt; coap_write_block_opt; + coap_ws_is_supported; + coap_ws_set_host_request; + coap_wss_is_supported; local: *; }; diff --git a/libcoap-3.sym b/libcoap-3.sym index bed7ee6c..c7be8511 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -265,3 +265,6 @@ coap_tls_is_supported coap_uri_into_options coap_write_block_b_opt coap_write_block_opt +coap_ws_is_supported +coap_ws_set_host_request +coap_wss_is_supported diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index e6ae85a0..a8b85d44 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -3000,8 +3000,74 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, } #endif /* COAP_SERVER_SUPPORT */ -#if HAVE_OSCORE +#if COAP_WS_SUPPORT +/* + * The struct hash_algs and the function get_hash_alg() are used to + * determine which hash type to use for creating the required hash object. + */ +static struct hash_algs { + cose_alg_t alg; + gnutls_digest_algorithm_t dig_type; + size_t dig_size; +} hashs[] = { + {COSE_ALGORITHM_SHA_1, GNUTLS_DIG_SHA1, 20}, + {COSE_ALGORITHM_SHA_256_256, GNUTLS_DIG_SHA256, 32}, + {COSE_ALGORITHM_SHA_512, GNUTLS_DIG_SHA512, 64}, +}; +static gnutls_digest_algorithm_t +get_hash_alg(cose_alg_t alg, size_t *hash_len) { + size_t idx; + + for (idx = 0; idx < sizeof(hashs) / sizeof(struct hash_algs); idx++) { + if (hashs[idx].alg == alg) { + *hash_len = hashs[idx].dig_size; + return hashs[idx].dig_type; + } + } + coap_log_debug("get_hash_alg: COSE hash %d not supported\n", alg); + return GNUTLS_DIG_UNKNOWN; +} + +int +coap_crypto_hash(cose_alg_t alg, + const coap_bin_const_t *data, + coap_bin_const_t **hash) { + size_t hash_length; + gnutls_digest_algorithm_t dig_type = get_hash_alg(alg, &hash_length); + gnutls_hash_hd_t digest_ctx; + coap_binary_t *dummy = NULL; + int ret; + + if (dig_type == GNUTLS_DIG_UNKNOWN) { + coap_log_debug("coap_crypto_hash: algorithm %d not supported\n", alg); + return 0; + } + + if (gnutls_hash_init(&digest_ctx, dig_type)) { + return 0; + } + ret = gnutls_hash(digest_ctx, data->s, data->length); + if (ret != 0) + goto error; + + dummy = coap_new_binary(hash_length); + if (!dummy) + goto error; + gnutls_hash_output(digest_ctx, dummy->s); + + *hash = (coap_bin_const_t *)(dummy); + gnutls_hash_deinit(digest_ctx, NULL); + return 1; + +error: + coap_delete_binary(dummy); + gnutls_hash_deinit(digest_ctx, NULL); + return 0; +} +#endif /* COAP_WS_SUPPORT */ + +#if HAVE_OSCORE int coap_oscore_is_supported(void) { return 1; diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index f9965bce..f7a4cbb9 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -2599,13 +2599,6 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, } #endif /* COAP_SERVER_SUPPORT */ -#if HAVE_OSCORE - -int -coap_oscore_is_supported(void) { - return 1; -} - #include #include @@ -2617,6 +2610,104 @@ coap_oscore_is_supported(void) { #include #endif /* MBEDTLS_ERROR_C */ +#ifdef MBEDTLS_ERROR_C +#define C(Func) \ + do { \ + int c_tmp = (int)(Func); \ + if (c_tmp != 0) { \ + char error_buf[64]; \ + mbedtls_strerror(c_tmp, error_buf, sizeof(error_buf)); \ + coap_log_err("mbedtls: -0x%04x: %s\n", -c_tmp, error_buf); \ + goto error; \ + } \ + } while (0); +#else /* !MBEDTLS_ERROR_C */ +#define C(Func) \ + do { \ + int c_tmp = (int)(Func); \ + if (c_tmp != 0) { \ + coap_log_err("mbedtls: %d\n", tmp); \ + goto error; \ + } \ + } while (0); +#endif /* !MBEDTLS_ERROR_C */ + +#if COAP_WS_SUPPORT +/* + * The struct hash_algs and the function get_hash_alg() are used to + * determine which hash type to use for creating the required hash object. + */ +static struct hash_algs { + cose_alg_t alg; + mbedtls_md_type_t hash_type; + size_t hash_size; +} hashs[] = { + {COSE_ALGORITHM_SHA_1, MBEDTLS_MD_SHA1, 20}, + {COSE_ALGORITHM_SHA_256_256, MBEDTLS_MD_SHA256, 32}, + {COSE_ALGORITHM_SHA_512, MBEDTLS_MD_SHA512, 64}, +}; + +static mbedtls_md_type_t +get_hash_alg(cose_alg_t alg, size_t *hash_len) { + size_t idx; + + for (idx = 0; idx < sizeof(hashs) / sizeof(struct hash_algs); idx++) { + if (hashs[idx].alg == alg) { + *hash_len = hashs[idx].hash_size; + return hashs[idx].hash_type; + } + } + coap_log_debug("get_hash_alg: COSE hash %d not supported\n", alg); + return MBEDTLS_MD_NONE; +} + +int +coap_crypto_hash(cose_alg_t alg, + const coap_bin_const_t *data, + coap_bin_const_t **hash) { + mbedtls_md_context_t ctx; + int ret = 0; + const mbedtls_md_info_t *md_info; + unsigned int len; + coap_binary_t *dummy = NULL; + size_t hash_length; + mbedtls_md_type_t dig_type = get_hash_alg(alg, &hash_length); + + if (dig_type == MBEDTLS_MD_NONE) { + coap_log_debug("coap_crypto_hash: algorithm %d not supported\n", alg); + return 0; + } + md_info = mbedtls_md_info_from_type(dig_type); + + len = mbedtls_md_get_size(md_info); + if (len == 0) { + return 0; + } + + mbedtls_md_init(&ctx); + C(mbedtls_md_setup(&ctx, md_info, 0)); + + C(mbedtls_md_starts(&ctx)); + C(mbedtls_md_update(&ctx, (const unsigned char *)data->s, data->length)); + dummy = coap_new_binary(len); + if (dummy == NULL) + goto error; + C(mbedtls_md_finish(&ctx, dummy->s)); + + *hash = (coap_bin_const_t *)dummy; + ret = 1; +error: + mbedtls_md_free(&ctx); + return ret; +} +#endif /* COAP_WS_SUPPORT */ + +#if HAVE_OSCORE +int +coap_oscore_is_supported(void) { + return 1; +} + /* * The struct cipher_algs and the function get_cipher_alg() are used to * determine which cipher type to use for creating the required cipher @@ -2680,28 +2771,6 @@ coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg) { return get_hmac_alg(hmac_alg) != 0; } -#ifdef MBEDTLS_ERROR_C -#define C(Func) \ - do { \ - int c_tmp = (int)(Func); \ - if (c_tmp != 0) { \ - char error_buf[64]; \ - mbedtls_strerror(c_tmp, error_buf, sizeof(error_buf)); \ - coap_log_err("mbedtls: -0x%04x: %s\n", -c_tmp, error_buf); \ - goto error; \ - } \ - } while (0); -#else /* !MBEDTLS_ERROR_C */ -#define C(Func) \ - do { \ - int c_tmp = (int)(Func); \ - if (c_tmp != 0) { \ - coap_log_err("mbedtls: %d\n", tmp); \ - goto error; \ - } \ - } while (0); -#endif /* !MBEDTLS_ERROR_C */ - /** * Initializes the cipher context @p ctx. On success, this function * returns true and @p ctx must be released by the caller using diff --git a/src/coap_notls.c b/src/coap_notls.c index 495f37f7..3867a461 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -310,6 +310,18 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, } #endif /* COAP_SERVER_SUPPORT */ +#if COAP_WS_SUPPORT +int +coap_crypto_hash(cose_alg_t alg, + const coap_bin_const_t *data, + coap_bin_const_t **hash) { + (void)alg; + (void)data; + (void)hash; + return 0; +} +#endif /* COAP_WS_SUPPORT */ + #if HAVE_OSCORE int diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 98360636..1482b779 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -3646,8 +3646,81 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, } #endif /* COAP_SERVER_SUPPORT */ -#if HAVE_OSCORE +#if COAP_WS_SUPPORT +/* + * The struct hash_algs and the function get_hash_alg() are used to + * determine which hash type to use for creating the required hash object. + */ +static struct hash_algs { + cose_alg_t alg; + const EVP_MD *(*get_hash)(void); + size_t length; /* in bytes */ +} hashs[] = { + {COSE_ALGORITHM_SHA_1, EVP_sha1, 20}, + {COSE_ALGORITHM_SHA_256_64, EVP_sha256, 8}, + {COSE_ALGORITHM_SHA_256_256, EVP_sha256, 32}, + {COSE_ALGORITHM_SHA_512, EVP_sha512, 64}, +}; +static const EVP_MD * +get_hash_alg(cose_alg_t alg, size_t *length) { + size_t idx; + + for (idx = 0; idx < sizeof(hashs) / sizeof(struct hash_algs); idx++) { + if (hashs[idx].alg == alg) { + *length = hashs[idx].length; + return hashs[idx].get_hash(); + } + } + coap_log_debug("get_hash_alg: COSE hash %d not supported\n", alg); + return NULL; +} + +int +coap_crypto_hash(cose_alg_t alg, + const coap_bin_const_t *data, + coap_bin_const_t **hash) { + unsigned int length; + const EVP_MD *evp_md; + EVP_MD_CTX *evp_ctx = NULL; + coap_binary_t *dummy = NULL; + size_t hash_length; + + if ((evp_md = get_hash_alg(alg, &hash_length)) == NULL) { + coap_log_debug("coap_crypto_hash: algorithm %d not supported\n", alg); + return 0; + } + evp_ctx = EVP_MD_CTX_new(); + if (evp_ctx == NULL) + goto error; + if (EVP_DigestInit_ex(evp_ctx, evp_md, NULL) == 0) + goto error; + ; + if (EVP_DigestUpdate(evp_ctx, data->s, data->length) == 0) + goto error; + ; + dummy = coap_new_binary(EVP_MAX_MD_SIZE); + if (dummy == NULL) + goto error; + if (EVP_DigestFinal_ex(evp_ctx, dummy->s, &length) == 0) + goto error; + dummy->length = length; + if (hash_length < dummy->length) + dummy->length = hash_length; + *hash = (coap_bin_const_t *)(dummy); + EVP_MD_CTX_free(evp_ctx); + return 1; + +error: + coap_crypto_output_errors("coap_crypto_hash"); + coap_delete_binary(dummy); + if (evp_ctx) + EVP_MD_CTX_free(evp_ctx); + return 0; +} +#endif /* COAP_WS_SUPPORT */ + +#if HAVE_OSCORE int coap_oscore_is_supported(void) { return 1; diff --git a/src/coap_session.c b/src/coap_session.c index 96cb295f..6e7b48b0 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -310,6 +310,8 @@ void coap_session_free(coap_session_t *session) { assert(session->ref == 0); if (session->ref) return; + /* Make sure nothing gets deleted under our feet */ + coap_session_reference(session); coap_session_mfree(session); #if COAP_SERVER_SUPPORT if (session->endpoint) { @@ -327,6 +329,7 @@ void coap_session_free(coap_session_t *session) { coap_log_debug("***%s: session %p: closed\n", coap_session_str(session), (void *)session); + assert(session->ref == 1); coap_free_type(COAP_SESSION, session); } @@ -586,8 +589,6 @@ coap_nack_name(coap_nack_reason_t reason) { return "COAP_NACK_BAD_RESPONSE"; case COAP_NACK_TLS_LAYER_FAILED: return "COAP_NACK_TLS_LAYER_FAILED"; - case COAP_NACK_SESSION_LAYER_FAILED: - return "COAP_NACK_SESSION_LAYER_FAILED"; default: return "???"; } diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index 49700b5e..028b9b9d 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -1460,6 +1460,18 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, } #endif /* COAP_SERVER_SUPPORT */ +#if COAP_WS_SUPPORT +int +coap_crypto_hash(cose_alg_t alg, + const coap_bin_const_t *data, + coap_bin_const_t **hash) { + (void)alg; + (void)data; + (void)hash; + return 0; +} +#endif /* COAP_WS_SUPPORT */ + #if HAVE_OSCORE int diff --git a/src/coap_ws.c b/src/coap_ws.c new file mode 100644 index 00000000..29873e05 --- /dev/null +++ b/src/coap_ws.c @@ -0,0 +1,41 @@ +/* + * coap_ws.c -- WebSockets functions for libcoap + * + * Copyright (C) 2023 Olaf Bergmann + * Copyright (C) 2023 Jon Shallow + * + * SPDX-License-Identifier: BSD-2-Clause + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +/** + * @file coap_ws.c + * @brief CoAP WebSocket handling functions + */ + +#include "coap3/coap_internal.h" + +#if COAP_WS_SUPPORT + +#else /* !COAP_WS_SUPPORT */ + +int +coap_ws_is_supported(void) { + return 0; +} + +int +coap_wss_is_supported(void) { + return 0; +} + +int +coap_ws_set_host_request(coap_session_t *session, coap_str_const_t *ws_host) { + (void)session; + (void)ws_host; + return 0; +} + +#endif /* !COAP_WS_SUPPORT */ diff --git a/src/net.c b/src/net.c index 48029e1a..e62eaa67 100644 --- a/src/net.c +++ b/src/net.c @@ -1681,7 +1681,7 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) } } else if (session->partial_read > 0) { size_t hdr_size = coap_pdu_parse_header_size(session->proto, - session->read_header); + session->read_header); size_t tkl = session->read_header[0] & 0x0f; size_t tok_ext_bytes = tkl == COAP_TOKEN_EXT_1B_TKL ? 1 : tkl == COAP_TOKEN_EXT_2B_TKL ? 2 : 0; @@ -3503,7 +3503,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, if (!coap_is_mcast(&session->addr_info.local)) { if (COAP_PDU_IS_EMPTY(pdu)) { - if (session->proto != COAP_PROTO_TCP && session->proto != COAP_PROTO_TLS) { + if (COAP_PROTO_NOT_RELIABLE(session->proto)) { coap_tick_t now; coap_ticks(&now); if (session->last_tx_rst + COAP_TICKS_PER_SECOND/4 < now) { diff --git a/win32/libcoap.vcxproj b/win32/libcoap.vcxproj index 8752cb4e..6f3b646b 100644 --- a/win32/libcoap.vcxproj +++ b/win32/libcoap.vcxproj @@ -65,6 +65,7 @@ + @@ -115,6 +116,8 @@ + + diff --git a/win32/libcoap.vcxproj.filters b/win32/libcoap.vcxproj.filters index 04eadfde..782443ed 100644 --- a/win32/libcoap.vcxproj.filters +++ b/win32/libcoap.vcxproj.filters @@ -89,6 +89,9 @@ Source Files + + Source Files + Source Files @@ -235,6 +238,12 @@ Header Files + + Header Files + + + Header Files + Header Files