mirror of
https://github.com/obgm/libcoap.git
synced 2026-12-05 20:02:19 +03:00
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.
This commit is contained in:
1 parent
357a0f6b7a
commit
0755ef429e
4 files changed
+17
-12
No files matched your search
+7
-3
@@ -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;
|
||||
|
||||
+4
-2
@@ -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));
|
||||
|
||||
+3
-3
@@ -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);
|
||||
|
||||
+3
-4
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user