From 5d01cf313910d42d155fbc3f129120314fbca860 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 23 Oct 2019 15:30:55 +0100 Subject: [PATCH] OpenSSL: Update ciphers src/coap_openssl.c: Remove NULL as a valid cipher from the cipher list. Add in TLSv1.3 ciphers if OpenSSL 1.1.1 (they are already included in TLSv1.2 definition, but this is to document this is happening). Remove the TLSv1.0 deprecated ciphers. Cipher strings can now be overridden during compilation by using -DCOAP_OPENSSL_CIPHERS=xxx compiler option where xxx is the cipher string as defined in https://www.openssl.org/docs/man1.1.0/man1/ciphers.html Explicit PSK cipher strings can now be overridden during compilation by using -DCOAP_OPENSSL_PSK_CIPHERS=xxx compiler option where xxx is the cipher string. Log the client ciphers presented, and the cipher chosen. include/coap2/coap_debug.h: src/coap_debug.c: Add in COAP_LOG_CIPHERS as a debug level (value 9) which matches getting the cipher information reported in GnuTLS, and is now used for OpenSSL. man/coap-client.txt.in: man/coap-rd.txt.in: man/coap-server.txt.in: Include OpenSSL for verbosity > 7. --- include/coap2/coap_debug.h | 11 ++++++++-- man/coap-client.txt.in | 2 +- man/coap-rd.txt.in | 2 +- man/coap-server.txt.in | 2 +- src/coap_debug.c | 4 ++-- src/coap_openssl.c | 45 ++++++++++++++++++++++++++++++-------- 6 files changed, 50 insertions(+), 16 deletions(-) diff --git a/include/coap2/coap_debug.h b/include/coap2/coap_debug.h index e4631b71..cfd0b8fc 100644 --- a/include/coap2/coap_debug.h +++ b/include/coap2/coap_debug.h @@ -36,8 +36,14 @@ * Logging type. One of LOG_* from @b syslog. */ typedef short coap_log_t; +/* + LOG_DEBUG+2 gives ciphers in GnuTLS + Use COAP_LOG_CIPHERS to output Cipher Info in OpenSSL etc. + */ +#define COAP_LOG_CIPHERS (LOG_DEBUG+2) #else -/** Pre-defined log levels akin to what is used in \b syslog. */ +/** Pre-defined log levels akin to what is used in \b syslog + with LOG_CIPHERS added. */ typedef enum { LOG_EMERG=0, /**< Emergency */ LOG_ALERT, /**< Alert */ @@ -46,7 +52,8 @@ typedef enum { LOG_WARNING, /**< Warning */ LOG_NOTICE, /**< Notice */ LOG_INFO, /**< Information */ - LOG_DEBUG /**< Debug */ + LOG_DEBUG, /**< Debug */ + COAP_LOG_CIPHERS=LOG_DEBUG+2 /**< CipherInfo */ } coap_log_t; #endif diff --git a/man/coap-client.txt.in b/man/coap-client.txt.in index f7dcd54c..44f494bc 100644 --- a/man/coap-client.txt.in +++ b/man/coap-client.txt.in @@ -98,7 +98,7 @@ OPTIONS - General *-v* num:: The verbosity level to use (default 3, maximum is 9). Above 7, there is - increased verbosity in GnuTLS logging. + increased verbosity in GnuTLS and OpenSSL logging. *-A* type:: Accepted media type. 'type' must be either a numeric value reflecting a diff --git a/man/coap-rd.txt.in b/man/coap-rd.txt.in index b7e84d71..6b8aa894 100644 --- a/man/coap-rd.txt.in +++ b/man/coap-rd.txt.in @@ -35,7 +35,7 @@ OPTIONS *-v* num:: The verbosity level to use (default: 3, maximum is 9). Above 7, there is - increased verbosity in GnuTLS logging. + increased verbosity in GnuTLS and OpenSSL logging. EXAMPLES -------- diff --git a/man/coap-server.txt.in b/man/coap-server.txt.in index 8fc30d1a..ac50997f 100644 --- a/man/coap-server.txt.in +++ b/man/coap-server.txt.in @@ -50,7 +50,7 @@ OPTIONS - General *-v* num:: The verbosity level to use (default 3, maximum is 9). Above 7, there is - increased verbosity in GnuTLS logging. + increased verbosity in GnuTLS and OpenSSL logging. *-A* addr:: The local address of the interface which the server has to listen. diff --git a/src/coap_debug.c b/src/coap_debug.c index 2dda55d9..7dbba491 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -69,7 +69,7 @@ coap_set_log_level(coap_log_t level) { /* this array has the same order as the type log_t */ static const char *loglevels[] = { - "EMRG", "ALRT", "CRIT", "ERR ", "WARN", "NOTE", "INFO", "DEBG" + "EMRG", "ALRT", "CRIT", "ERR ", "WARN", "NOTE", "INFO", "DEBG", "????", "CIPH" }; #ifdef HAVE_TIME_H @@ -791,7 +791,7 @@ coap_log_impl(coap_log_t level, const char *format, ...) { if (print_timestamp(timebuf,sizeof(timebuf), now)) fprintf(log_fd, "%s ", timebuf); - if (level <= LOG_DEBUG) + if (level <= COAP_LOG_CIPHERS) fprintf(log_fd, "%s ", loglevels[level]); va_start(ap, format); diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 70b826d0..9d53960f 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -68,6 +68,18 @@ #define TLSEXT_TYPE_server_certificate_type 20 #endif +#ifndef COAP_OPENSSL_CIPHERS +#if OPENSSL_VERSION_NUMBER >= 0x10101000L +#define COAP_OPENSSL_CIPHERS "TLSv1.3:TLSv1.2:!NULL" +#else /* OPENSSL_VERSION_NUMBER < 0x10101000L */ +#define COAP_OPENSSL_CIPHERS "TLSv1.2:!NULL" +#endif /* OPENSSL_VERSION_NUMBER < 0x10101000L */ +#endif /*COAP_OPENSSL_CIPHERS */ + +#ifndef COAP_OPENSSL_PSK_CIPHERS +#define COAP_OPENSSL_PSK_CIPHERS "PSK:!NULL" +#endif /*COAP_OPENSSL_PSK_CIPHERS */ + /* This structure encapsulates the OpenSSL context object. */ typedef struct coap_dtls_context_t { SSL_CTX *ctx; @@ -507,7 +519,7 @@ void *coap_dtls_new_context(struct coap_context_t *coap_context) { SSL_CTX_set_min_proto_version(context->dtls.ctx, DTLS1_2_VERSION); SSL_CTX_set_app_data(context->dtls.ctx, &context->dtls); SSL_CTX_set_read_ahead(context->dtls.ctx, 1); - SSL_CTX_set_cipher_list(context->dtls.ctx, "TLSv1.2:TLSv1.0"); + SSL_CTX_set_cipher_list(context->dtls.ctx, COAP_OPENSSL_CIPHERS); memset(cookie_secret, 0, sizeof(cookie_secret)); if (!RAND_bytes(cookie_secret, (int)sizeof(cookie_secret))) { if (dtls_log_level >= LOG_WARNING) @@ -541,7 +553,7 @@ void *coap_dtls_new_context(struct coap_context_t *coap_context) { goto error; SSL_CTX_set_app_data(context->tls.ctx, &context->tls); SSL_CTX_set_min_proto_version(context->tls.ctx, TLS1_VERSION); - SSL_CTX_set_cipher_list(context->tls.ctx, "TLSv1.2:TLSv1.0"); + SSL_CTX_set_cipher_list(context->tls.ctx, COAP_OPENSSL_CIPHERS); SSL_CTX_set_info_callback(context->tls.ctx, coap_dtls_info_callback); context->tls.meth = BIO_meth_new(BIO_TYPE_SOCKET, "coapsock"); if (!context->tls.meth) @@ -1123,7 +1135,7 @@ tls_verify_call_back(int preverify_ok, X509_STORE_CTX *ctx) { /* * During the SSL/TLS initial negotiations, tls_secret_call_back() is called so * it is possible to determine whether this is a PKI or PSK incoming - * request and adjust the Ciphers if necessary + * request and adjust the ciphers if necessary * * Set up by SSL_set_session_secret_cb() in tls_server_name_call_back() */ @@ -1145,6 +1157,8 @@ tls_secret_call_back(SSL *ssl, for (ii = 0; ii < sk_SSL_CIPHER_num (peer_ciphers); ii++) { const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii); + coap_log(COAP_LOG_CIPHERS, "Client cipher: %s\n", + SSL_CIPHER_get_name(peer_cipher)); if (strstr (SSL_CIPHER_get_name (peer_cipher), "PSK")) { psk_requested = 1; break; @@ -1207,7 +1221,7 @@ tls_secret_call_back(SSL *ssl, /* * Force a PSK algorithm to be used, so we do PSK */ - SSL_set_cipher_list (ssl, "PSK:!NULL"); + SSL_set_cipher_list (ssl, COAP_OPENSSL_PSK_CIPHERS); SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback); } if (setup_data->additional_tls_setup_call_back) { @@ -1221,7 +1235,7 @@ tls_secret_call_back(SSL *ssl, /* * During the SSL/TLS initial negotiations, tls_server_name_call_back() is called * so it is possible to set up an extra callback to determine whether this is - * a PKI or PSK incoming request and adjust the Ciphers if necessary + * a PKI or PSK incoming request and adjust the ciphers if necessary * * Set up by SSL_CTX_set_tlsext_servername_callback() in coap_dtls_context_set_pki() */ @@ -1269,7 +1283,7 @@ tls_server_name_call_back(SSL *ssl, SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION); SSL_CTX_set_app_data(ctx, &context->dtls); SSL_CTX_set_read_ahead(ctx, 1); - SSL_CTX_set_cipher_list(ctx, "TLSv1.2:TLSv1.0"); + SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS); SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie); SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie); SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback); @@ -1282,7 +1296,7 @@ tls_server_name_call_back(SSL *ssl, goto error; SSL_CTX_set_app_data(ctx, &context->tls); SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); - SSL_CTX_set_cipher_list(ctx, "TLSv1.2:TLSv1.0"); + SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS); SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback); SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL); } @@ -1317,7 +1331,7 @@ error: /* * During the SSL/TLS initial negotiations, tls_client_hello_call_back() is * called early in the Client Hello processing so it is possible to determine - * whether this is a PKI or PSK incoming request and adjust the Ciphers if + * whether this is a PKI or PSK incoming request and adjust the ciphers if * necessary. * * Set up by SSL_CTX_set_client_hello_cb(). @@ -1360,6 +1374,9 @@ tls_client_hello_call_back(SSL *ssl, for (ii = 0; ii < sk_SSL_CIPHER_num (peer_ciphers); ii++) { const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii); + coap_log(COAP_LOG_CIPHERS, "Client cipher: %s (%04x)\n", + SSL_CIPHER_get_name(peer_cipher), + SSL_CIPHER_get_protocol_id(peer_cipher)); if (strstr (SSL_CIPHER_get_name (peer_cipher), "PSK")) { psk_requested = 1; break; @@ -1744,7 +1761,7 @@ setup_client_ssl_session(coap_session_t *session, SSL *ssl if (context->psk_pki_enabled & IS_PSK) { SSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback); SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback); - SSL_set_cipher_list(ssl, "PSK:!NULL"); + SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS); } if (context->psk_pki_enabled & IS_PKI) { coap_dtls_pki_t *setup_data = &context->setup_data; @@ -1964,6 +1981,8 @@ int coap_dtls_receive(coap_session_t *session, int err = SSL_get_error(ssl, r); if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { if (in_init && SSL_is_init_finished(ssl)) { + coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_connected(session); } @@ -2189,6 +2208,8 @@ ssize_t coap_tls_write(coap_session_t *session, int err = SSL_get_error(ssl, r); if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { if (in_init && SSL_is_init_finished(ssl)) { + coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_send_csm(session); } @@ -2215,6 +2236,8 @@ ssize_t coap_tls_write(coap_session_t *session, r = -1; } } else if (in_init && SSL_is_init_finished(ssl)) { + coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_send_csm(session); } @@ -2250,6 +2273,8 @@ ssize_t coap_tls_read(coap_session_t *session, int err = SSL_get_error(ssl, r); if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { if (in_init && SSL_is_init_finished(ssl)) { + coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_send_csm(session); } @@ -2274,6 +2299,8 @@ ssize_t coap_tls_read(coap_session_t *session, r = -1; } } else if (in_init && SSL_is_init_finished(ssl)) { + coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_send_csm(session); }