From 0755ef429ec205790c12c9bcb10ebacb4c4d4633 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 11 Apr 2020 13:10:31 +0100 Subject: [PATCH] DTLS: Sessions unnecessarily being re-handshaked Found with GnuTLS, believed not to be an issue with OpenSSL and likely (but not reproduced) to be an issue with MbedTLS. In coap_io_prepare_io() the DTLS layer is checked to see if there should be a (handshake) packet re-transmitted. The logic checks to see if there is a timeout and if so causes it to be re-transmitted. Unfortunately this check was also being done when not in the DTLS handshake phase and GnuTLS was returning 'timed out' based on the time since the last handshake transmission. As there was nothing being sent or received, when doing the retransmission, this caused a re-handshake. The fix is to only check for timeouts / re-transmits when in the handshake phase. This change does rigorous checking of state. src/coap_io.c: Only check timeouts / re-transmits when in Handshake phase. [There is also a cpu savings as most sessions will not be in the Handshake phase.] src/coap_gnutls.c: src/coap_mbedtls.c: src/coap_openssl.c: coap_dtls_get_timeout() assert if not handshaking coap_dtls_handle_timeout() assert if not handshaking Note: It is much easier to observe time when checking for this issue if #441 changes are included. --- src/coap_gnutls.c | 10 +++++++--- src/coap_io.c | 6 ++++-- src/coap_mbedtls.c | 6 +++--- src/coap_openssl.c | 7 +++---- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index a4a248b5..17446fee 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -34,6 +34,10 @@ * * TLS 1.3 is properly supported from 3.6.5 onwards * (but is not enabled by default in 3.6.4) + * + * Starting with 3.6.3, fixed in 3.6.13, Client Hellos may fail with some + * server implementations (e.g. Californium) as random value is all zeros + * - CVE-2020-11501 - a security weakness. */ #include "coap_internal.h" @@ -1815,6 +1819,7 @@ coap_tick_t coap_dtls_get_context_timeout(void *dtls_context UNUSED) { coap_tick_t coap_dtls_get_timeout(coap_session_t *c_session, coap_tick_t now) { coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; + assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE); if (g_env && g_env->g_session) { unsigned int rem_ms = gnutls_dtls_get_timeout(g_env->g_session); @@ -1839,10 +1844,9 @@ coap_tick_t coap_dtls_get_timeout(coap_session_t *c_session, coap_tick_t now) { void coap_dtls_handle_timeout(coap_session_t *c_session) { coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; - assert(g_env != NULL); + assert(g_env != NULL && c_session->state == COAP_SESSION_STATE_HANDSHAKE); g_env->doing_dtls_timeout = 1; - if (((c_session->state == COAP_SESSION_STATE_HANDSHAKE) && - (++c_session->dtls_timeout_count > c_session->max_retransmit)) || + if ((++c_session->dtls_timeout_count > c_session->max_retransmit) || (do_gnutls_handshake(c_session, g_env) < 0)) { /* Too many retries */ g_env->doing_dtls_timeout = 0; diff --git a/src/coap_io.c b/src/coap_io.c index c1ca5dd6..fe12f86f 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -1125,7 +1125,8 @@ coap_io_prepare_io(coap_context_t *ctx, LL_FOREACH(ctx->endpoint, ep) { if (ep->proto == COAP_PROTO_DTLS) { SESSIONS_ITER(ep->sessions, s, rtmp) { - if (s->proto == COAP_PROTO_DTLS && s->tls) { + if (s->state == COAP_SESSION_STATE_HANDSHAKE && + s->proto == COAP_PROTO_DTLS && s->tls) { coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now); while (tls_timeout > 0 && tls_timeout <= now) { coap_log(LOG_DEBUG, "** %s: DTLS retransmit timeout\n", @@ -1148,7 +1149,8 @@ coap_io_prepare_io(coap_context_t *ctx, } } SESSIONS_ITER(ctx->sessions, s, rtmp) { - if (s->proto == COAP_PROTO_DTLS && s->tls) { + if (s->state == COAP_SESSION_STATE_HANDSHAKE && + s->proto == COAP_PROTO_DTLS && s->tls) { coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now); while (tls_timeout > 0 && tls_timeout <= now) { coap_log(LOG_DEBUG, "** %s: DTLS retransmit timeout\n", coap_session_str(s)); diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 89625785..6d552b4a 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -1536,6 +1536,7 @@ coap_tick_t coap_dtls_get_timeout(coap_session_t *c_session, coap_tick_t now) int ret = mbedtls_timing_get_delay(&m_env->timer); unsigned int scalar = 1 << m_env->retry_scalar; + assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE); switch (ret) { case 0: /* int_ms has not timed out */ @@ -1572,10 +1573,9 @@ void coap_dtls_handle_timeout(coap_session_t *c_session) { coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls; - assert(m_env != NULL); + assert(m_env != NULL && c_session->state == COAP_SESSION_STATE_HANDSHAKE); m_env->retry_scalar++; - if (((c_session->state == COAP_SESSION_STATE_HANDSHAKE) && - (++c_session->dtls_timeout_count > c_session->max_retransmit)) || + if ((++c_session->dtls_timeout_count > c_session->max_retransmit) || (do_mbedtls_handshake(c_session, m_env) < 0)) { /* Too many retries */ coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); diff --git a/src/coap_openssl.c b/src/coap_openssl.c index a44f81b9..2e195aa1 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -2530,7 +2530,7 @@ coap_tick_t coap_dtls_get_timeout(coap_session_t *session, coap_tick_t now UNUSE SSL *ssl = (SSL *)session->tls; coap_ssl_data *ssl_data; - assert(ssl != NULL); + assert(ssl != NULL && session->state == COAP_SESSION_STATE_HANDSHAKE); ssl_data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(ssl)); return ssl_data->timeout; } @@ -2538,9 +2538,8 @@ coap_tick_t coap_dtls_get_timeout(coap_session_t *session, coap_tick_t now UNUSE void coap_dtls_handle_timeout(coap_session_t *session) { SSL *ssl = (SSL *)session->tls; - assert(ssl != NULL); - if (((session->state == COAP_SESSION_STATE_HANDSHAKE) && - (++session->dtls_timeout_count > session->max_retransmit)) || + assert(ssl != NULL && session->state == COAP_SESSION_STATE_HANDSHAKE); + if ((++session->dtls_timeout_count > session->max_retransmit) || (DTLSv1_handle_timeout(ssl) < 0)) { /* Too many retries */ coap_session_disconnected(session, COAP_NACK_TLS_FAILED);