diff --git a/examples/coap-server.c b/examples/coap-server.c index d184247f..894bbaa0 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -2083,7 +2083,7 @@ fill_keystore(coap_context_t *ctx) { if (cert_file == NULL && key_defined == 0) { if (coap_dtls_is_supported() || coap_tls_is_supported()) { coap_log(LOG_DEBUG, - "(D)TLS not enabled as neither -k or -c options specified\n"); + "(D)TLS not enabled as none of -k, -c or -M options specified\n"); } return; } @@ -2100,7 +2100,11 @@ fill_keystore(coap_context_t *ctx) { if (key_defined) { coap_dtls_spsk_t *dtls_spsk = setup_spsk(); - coap_context_set_psk2(ctx, dtls_spsk); + if (!coap_context_set_psk2(ctx, dtls_spsk)) { + coap_log(LOG_INFO, "Unable to set up PSK\n"); + /* So we do not set up DTLS */ + key_defined = 0; + } } } diff --git a/include/coap3/coap_dtls.h b/include/coap3/coap_dtls.h index 76a280d6..9f95018a 100644 --- a/include/coap3/coap_dtls.h +++ b/include/coap3/coap_dtls.h @@ -50,17 +50,45 @@ typedef enum coap_dtls_role_t { /** * Check whether DTLS is available. * - * @return @c 1 if support for DTLS is enabled, or @c 0 otherwise. + * @return @c 1 if support for DTLS is available, or @c 0 otherwise. */ int coap_dtls_is_supported(void); /** * Check whether TLS is available. * - * @return @c 1 if support for TLS is enabled, or @c 0 otherwise. + * @return @c 1 if support for TLS is available, or @c 0 otherwise. */ int coap_tls_is_supported(void); +/** + * Check whether (D)TLS PSK is available. + * + * @return @c 1 if support for (D)TLS PSK is available, or @c 0 otherwise. + */ +int coap_dtls_psk_is_supported(void); + +/** + * Check whether (D)TLS PKI is available. + * + * @return @c 1 if support for (D)TLS PKI is available, or @c 0 otherwise. + */ +int coap_dtls_pki_is_supported(void); + +/** + * Check whether (D)TLS PKCS11 is available. + * + * @return @c 1 if support for (D)TLS PKCS11 is available, or @c 0 otherwise. + */ +int coap_dtls_pkcs11_is_supported(void); + +/** + * Check whether (D)TLS RPK is available. + * + * @return @c 1 if support for (D)TLS RPK is available, or @c 0 otherwise. + */ +int coap_dtls_rpk_is_supported(void); + typedef enum coap_tls_library_t { COAP_TLS_LIBRARY_NOTLS = 0, /**< No DTLS library */ COAP_TLS_LIBRARY_TINYDTLS, /**< Using TinyDTLS library */ diff --git a/libcoap-3.map b/libcoap-3.map index c9323da1..21b588d0 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -71,6 +71,10 @@ global: coap_delete_uri; coap_dtls_get_log_level; coap_dtls_is_supported; + coap_dtls_pkcs11_is_supported; + coap_dtls_pki_is_supported; + coap_dtls_psk_is_supported; + coap_dtls_rpk_is_supported; coap_dtls_set_log_level; coap_encode_var_safe; coap_encode_var_safe8; diff --git a/libcoap-3.sym b/libcoap-3.sym index b8ed5412..49bb6470 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -69,6 +69,10 @@ coap_delete_string coap_delete_uri coap_dtls_get_log_level coap_dtls_is_supported +coap_dtls_pkcs11_is_supported +coap_dtls_pki_is_supported +coap_dtls_psk_is_supported +coap_dtls_rpk_is_supported coap_dtls_set_log_level coap_encode_var_safe coap_encode_var_safe8 diff --git a/man/Makefile.am b/man/Makefile.am index 411f81ee..ecfa5f40 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -148,6 +148,9 @@ install-man: install-man3 install-man5 install-man7 @echo ".so man3/coap_string.3" > coap_make_str_const.3 @echo ".so man3/coap_string.3" > coap_string_equal.3 @echo ".so man3/coap_string.3" > coap_binary_equal.3 + @echo ".so man3/coap_tls_library.3" > coap_string_tls_support.3 + @echo ".so man3/coap_tls_library.3" > coap_string_tls_version.3 + @echo ".so man3/coap_tls_library.3" > coap_show_tls_version.3 $(INSTALL_DATA) $(A2X_EXTRA_PAGES_3) "$(DESTDIR)$(man3dir)" $(INSTALL_DATA) $(A2X_EXTRA_PAGES_5) "$(DESTDIR)$(man5dir)" diff --git a/man/coap_tls_library.txt.in b/man/coap_tls_library.txt.in index ce3deeac..100457c4 100644 --- a/man/coap_tls_library.txt.in +++ b/man/coap_tls_library.txt.in @@ -13,6 +13,10 @@ NAME coap_tls_library, coap_dtls_is_supported, coap_tls_is_supported, +coap_dtls_psk_is_supported, +coap_dtls_pki_is_supported, +coap_dtls_pkcs11_is_supported, +coap_dtls_rpk_is_supported, coap_tcp_is_supported, coap_get_tls_library_version, coap_string_tls_support, @@ -28,6 +32,14 @@ SYNOPSIS *int coap_tls_is_supported(void);* +*int coap_dtls_psk_is_supported(void);* + +*int coap_dtls_pki_is_supported(void);* + +*int coap_dtls_pkcs11_is_supported(void);* + +*int coap_dtls_rpk_is_supported(void);* + *int coap_tcp_is_supported(void);* *coap_tls_version_t *coap_get_tls_library_version(void);* @@ -74,35 +86,49 @@ However, there is the flexibility of the Callbacks for imposing additional security checks etc. when PKI is being used. These callbacks need to need to match the TLS implementation type. +FUNCTIONS +--------- + +*Function: coap_dtls_is_supported()* + The *coap_dtls_is_supported*() function returns 1 if support for DTLS is -enabled, otherwise 0; +available, otherwise 0; + +*Function: coap_tls_is_supported()* The *coap_tls_is_supported*() function returns 1 if support for TLS is -enabled, otherwise 0; +available, otherwise 0; + +*Function: coap_dtls_psk_is_supported()* + +The *coap_dtls_psk_is_supported*() function returns 1 if support for (D)TLS PSK +is available, otherwise 0; + +*Function: coap_dtls_pki_is_supported()* + +The *coap_dtls_pki_is_supported*() function returns 1 if support for (D)TLS PKI +is available, otherwise 0; + +*Function: coap_dtls_pkcs11_is_supported()* + +The *coap_dtls_pkcs11_is_supported*() function returns 1 if support for (D)TLS +PKCS11 is available, otherwise 0; + +*Function: coap_dtls_rpk_is_supported()* + +The *coap_dtls_rpk_is_supported*() function returns 1 if support for (D)TLS RPK +is available, otherwise 0; + +*Function: coap_tcp_is_supported()* The *coap_tcp_is_supported*() function returns 1 if support for TCP is -enabled, otherwise 0. +available, otherwise 0. + +*Function: coap_get_tls_library_version()* The *coap_get_tls_library_version*() function returns the TLS implementation type and library version in a coap_tls_version_t* structure. -The *coap_string_tls_support*() function is used to update the provided buffer -with ascii readable information about what type of PSK, PKI etc. keys the -current (D)TLS library supports. -_buffer_ defines the buffer to provide the information and _bufsize_ is the -size of _buffer_. - -The *coap_string_tls_version*() function is used to update the provided buffer -with information about the current (D)TLS library that libcoap was built -against, as well as the current linked version of the (D)TLS library. -_buffer_ defines the buffer to provide the information and _bufsize_ is the -size of _buffer_. - -The *coap_show_tls_version*() function is used log information about the -current (D)TLS library that libcoap was built against, as well as the current -linked version of the (D)TLS library. _level_ defines the minimum logging level -for this information to be output using coap_log(). - [source, c] ---- typedef enum coap_tls_library_t { @@ -120,16 +146,41 @@ typedef struct coap_tls_version_t { } ---- +*Function: coap_string_tls_support()* + +The *coap_string_tls_support*() function is used to update the provided buffer +with ascii readable information about what type of PSK, PKI etc. keys the +current (D)TLS library supports. +_buffer_ defines the buffer to provide the information and _bufsize_ is the +size of _buffer_. + +*Function: coap_string_tls_version()* + +The *coap_string_tls_version*() function is used to update the provided buffer +with information about the current (D)TLS library that libcoap was built +against, as well as the current linked version of the (D)TLS library. +_buffer_ defines the buffer to provide the information and _bufsize_ is the +size of _buffer_. + +*Function: coap_show_tls_version()* + +The *coap_show_tls_version*() function is used log information about the +current (D)TLS library that libcoap was built against, as well as the current +linked version of the (D)TLS library. _level_ defines the minimum logging level +for this information to be output using coap_log(). + RETURN VALUES ------------- -*coap_dtls_is_supported*() and *coap_tls_is_supported*() functions +*coap_dtls_is_supported*(), *coap_tls_is_supported*(), +*coap_dtls_psk_is_supported*(), *coap_dtls_pki_is_supported*(), +*coap_dtls_pkcs11_is_supported*() and *coap_dtls_rpk_is_supported*() functions return 0 if there is no support, 1 if support is available. *coap_get_tls_library_version*() function returns the TLS implementation type and library version in a coap_tls_version_t* structure. -The *coap_tcp_is_supported*() function returns 1 if support for TCP is -enabled, otherwise 0. +*coap_tcp_is_supported*() function returns 1 if support for TCP is +available, otherwise 0. *coap_string_tls_version*() function returns a pointer to the provided buffer. @@ -139,8 +190,13 @@ SEE ALSO FURTHER INFORMATION ------------------- -See "RFC7252: The Constrained Application Protocol (CoAP)" for further -information. +See + +"https://tools.ietf.org/html/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" + +"https://tools.ietf.org/html/rfc8323[RFC8323: CoAP (Constrained Application Protocol) over TCP, TLS, and WebSockets]" + +for further information. BUGS ---- diff --git a/src/coap_debug.c b/src/coap_debug.c index 2d89613c..47633acd 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -936,52 +936,25 @@ char *coap_string_tls_version(char *buffer, size_t bufsize) char *coap_string_tls_support(char *buffer, size_t bufsize) { - coap_tls_version_t *tls_version = coap_get_tls_library_version(); const int have_tls = coap_tls_is_supported(); const int have_dtls = coap_dtls_is_supported(); + const int have_psk = coap_dtls_psk_is_supported(); + const int have_pki = coap_dtls_pki_is_supported(); + const int have_pkcs11 = coap_dtls_pkcs11_is_supported(); + const int have_rpk = coap_dtls_rpk_is_supported(); if (have_dtls == 0 && have_tls == 0) { snprintf(buffer, bufsize, "(No DTLS or TLS support)"); return buffer; } - switch (tls_version->type) { - case COAP_TLS_LIBRARY_NOTLS: - snprintf(buffer, bufsize, "(No DTLS or TLS support)"); - break; - case COAP_TLS_LIBRARY_TINYDTLS: - snprintf(buffer, bufsize, - "(%sDTLS and%s TLS support; PSK, no PKI, no PKCS11, and RPK support)", - have_dtls ? "" : " no", - have_tls ? "" : " no"); - break; - case COAP_TLS_LIBRARY_OPENSSL: - snprintf(buffer, bufsize, - "(%sDTLS and%s TLS support; PSK, PKI, PKCS11, and no RPK support)", - have_dtls ? "" : " no", - have_tls ? "" : " no"); - break; - case COAP_TLS_LIBRARY_GNUTLS: - if (tls_version->version >= 0x030606) - snprintf(buffer, bufsize, - "(%sDTLS and%s TLS support; PSK, PKI, PKCS11, and RPK support)", - have_dtls ? "" : " no", - have_tls ? "" : " no"); - else - snprintf(buffer, bufsize, - "(%sDTLS and%s TLS support; PSK, PKI, PKCS11, and no RPK support)", - have_dtls ? "" : " no", - have_tls ? "" : " no"); - break; - case COAP_TLS_LIBRARY_MBEDTLS: - snprintf(buffer, bufsize, - "(%sDTLS and%s TLS support; PSK, PKI, no PKCS11, and no RPK support)", - have_dtls ? "" : " no", - have_tls ? "" : " no"); - break; - default: - buffer[0] = '\000'; - break; - } + snprintf(buffer, bufsize, + "(%sDTLS and %sTLS support; %sPSK, %sPKI, %sPKCS11, and %sRPK support)", + have_dtls ? "" : "No ", + have_tls ? "" : "no ", + have_psk ? "" : "no ", + have_pki ? "" : "no ", + have_pkcs11 ? "" : "no ", + have_rpk ? "" : "no "); return buffer; } diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index a502a81d..ef7a4440 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -205,6 +205,46 @@ coap_tls_is_supported(void) { #endif /* COAP_DISABLE_TCP */ } +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_psk_is_supported(void) { + return 1; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pki_is_supported(void) { + return 1; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pkcs11_is_supported(void) { + return 1; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_rpk_is_supported(void) { +#if (GNUTLS_VERSION_NUMBER >= 0x030606) + return 1; +#else /* GNUTLS_VERSION_NUMBER < 0x030606 */ + return 0; +#endif /* GNUTLS_VERSION_NUMBER < 0x030606 */ +} + coap_tls_version_t * coap_get_tls_library_version(void) { static coap_tls_version_t version; diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 40fcdfdc..9819543f 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -1599,7 +1599,8 @@ fail: return NULL; } -int coap_dtls_is_supported(void) { +int +coap_dtls_is_supported(void) { #if defined(MBEDTLS_SSL_PROTO_DTLS) return 1; #else /* !MBEDTLS_SSL_PROTO_DTLS */ @@ -1614,8 +1615,8 @@ int coap_dtls_is_supported(void) { #endif /* !MBEDTLS_SSL_PROTO_DTLS */ } -int coap_tls_is_supported(void) -{ +int +coap_tls_is_supported(void) { #if !COAP_DISABLE_TCP return 1; #else /* COAP_DISABLE_TCP */ @@ -1623,6 +1624,42 @@ int coap_tls_is_supported(void) #endif /* COAP_DISABLE_TCP */ } +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_psk_is_supported(void) { + return 1; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pki_is_supported(void) { + return 1; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pkcs11_is_supported(void) { + return 0; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_rpk_is_supported(void) { + return 0; +} + void *coap_dtls_new_context(coap_context_t *c_context) { coap_mbedtls_context_t *m_context; diff --git a/src/coap_notls.c b/src/coap_notls.c index 0af47ba0..1a6cf0e7 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -29,6 +29,42 @@ coap_tls_is_supported(void) { return 0; } +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_psk_is_supported(void) { + return 0; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pki_is_supported(void) { + return 0; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pkcs11_is_supported(void) { + return 0; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_rpk_is_supported(void) { + return 0; +} + coap_tls_version_t * coap_get_tls_library_version(void) { static coap_tls_version_t version; diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 53ab5354..cd1ea4e8 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -165,7 +165,8 @@ static int psk_tls_client_hello_call_back(SSL *ssl, int *al, void *arg); #endif /* OPENSSL_VERSION_NUMBER >= 0x10101000L */ #endif /* COAP_SERVER_SUPPORT */ -int coap_dtls_is_supported(void) { +int +coap_dtls_is_supported(void) { if (SSLeay() < 0x10100000L) { coap_log(LOG_WARNING, "OpenSSL version 1.1.0 or later is required\n"); return 0; @@ -186,7 +187,8 @@ int coap_dtls_is_supported(void) { return 1; } -int coap_tls_is_supported(void) { +int +coap_tls_is_supported(void) { #if !COAP_DISABLE_TCP if (SSLeay() < 0x10100000L) { coap_log(LOG_WARNING, "OpenSSL version 1.1.0 or later is required\n"); @@ -204,6 +206,42 @@ int coap_tls_is_supported(void) { #endif /* COAP_DISABLE_TCP */ } +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_psk_is_supported(void) { + return 1; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pki_is_supported(void) { + return 1; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pkcs11_is_supported(void) { + return 1; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_rpk_is_supported(void) { + return 0; +} + coap_tls_version_t * coap_get_tls_library_version(void) { static coap_tls_version_t version; diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index ce944747..3c8de98d 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -41,6 +41,10 @@ typedef struct coap_tiny_context_t { #endif /* DTLS_ECC */ } coap_tiny_context_t; +#if ! defined(DTLS_PSK) && ! defined(DTLS_ECC) +#error Neither DTLS_PSK or DTLS_ECC defined +#endif /* ! DTLS_PSK && ! DTLS_ECC */ + static dtls_tick_t dtls_tick_0 = 0; static coap_tick_t coap_tick_0 = 0; @@ -49,6 +53,51 @@ coap_dtls_is_supported(void) { return 1; } +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_psk_is_supported(void) { +#ifdef DTLS_PSK + return 1; +#else /* ! DTLS_PSK */ + return 0; +#endif /* ! DTLS_PSK */ +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pki_is_supported(void) { + return 0; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_pkcs11_is_supported(void) { + return 0; +} + +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_rpk_is_supported(void) { +#ifdef DTLS_ECC + return 1; +#else /* ! DTLS_ECC */ + return 0; +#endif /* ! DTLS_ECC */ +} + + void coap_dtls_startup(void) { dtls_init(); dtls_ticks(&dtls_tick_0); @@ -196,6 +245,7 @@ dtls_event(struct dtls_context_t *dtls_context, return 0; } +#ifdef DTLS_PSK /* This function is the "key store" for tinyDTLS. It is called to * retrieve a key for the given identity within this particular * session. */ @@ -383,6 +433,7 @@ get_psk_info(struct dtls_context_t *dtls_context, error: return dtls_alert_fatal_create(fatal_error); } +#endif /* DTLS_PSK */ #ifdef DTLS_ECC static int @@ -462,7 +513,9 @@ static dtls_handler_t ec_cb = { .write = dtls_send_to_peer, .read = dtls_application_data, .event = dtls_event, +#ifdef DTLS_PSK .get_psk_info = NULL, +#endif /* DTLS_PSK */ .get_ecdsa_key = get_ecdsa_key, .verify_ecdsa_key = verify_ecdsa_key }; @@ -472,11 +525,13 @@ static dtls_handler_t psk_cb = { .write = dtls_send_to_peer, .read = dtls_application_data, .event = dtls_event, +#ifdef DTLS_PSK .get_psk_info = get_psk_info, +#endif /* DTLS_PSK */ #ifdef DTLS_ECC .get_ecdsa_key = NULL, .verify_ecdsa_key = NULL -#endif +#endif /* DTLS_ECC */ }; void * @@ -1027,8 +1082,7 @@ pem_decode_mem_asn1(const char *begstr, const uint8_t *str) int coap_dtls_context_set_pki(coap_context_t *ctx, const coap_dtls_pki_t* setup_data, - const coap_dtls_role_t role COAP_UNUSED -) { + const coap_dtls_role_t role COAP_UNUSED) { #ifdef DTLS_ECC coap_tiny_context_t *t_context; coap_binary_t *asn1_priv; @@ -1182,6 +1236,7 @@ coap_dtls_context_set_pki(coap_context_t *ctx, (void)ctx; (void)setup_data; #endif /* ! DTLS_ECC */ + coap_log(LOG_WARNING, "TinyDTLS not compiled with ECC support\n"); return 0; } @@ -1202,7 +1257,12 @@ coap_dtls_context_set_cpsk(coap_context_t *coap_context COAP_UNUSED, if (!setup_data) return 0; +#ifdef DTLS_PSK return 1; +#else /* ! DTLS_PSK */ + coap_log(LOG_WARNING, "TinyDTLS not compiled with PSK support\n"); + return 0; +#endif /* ! DTLS_PSK */ } #endif /* COAP_CLIENT_SUPPORT */ @@ -1214,12 +1274,17 @@ coap_dtls_context_set_spsk(coap_context_t *coap_context COAP_UNUSED, if (!setup_data) return 0; +#ifdef DTLS_PSK if (setup_data->validate_sni_call_back) { coap_log(LOG_WARNING, "CoAP Server with TinyDTLS does not support SNI selection\n"); } return 1; +#else /* ! DTLS_PSK */ + coap_log(LOG_WARNING, "TinyDTLS not compiled with PSK support\n"); + return 0; +#endif /* ! DTLS_PSK */ } #endif /* COAP_SERVER_SUPPORT */