[PATCH 11 of 11] SSL: automatic rotation of session ticket keys

Maxim Dounin mdounin at mdounin.ru
Sun Oct 9 04:59:01 UTC 2022


Hello!

On Sat, Oct 01, 2022 at 11:58:20AM +0300, Maxim Dounin wrote:

> On Thu, Sep 29, 2022 at 08:00:03PM +0400, Sergey Kandaurov wrote:
> 
> > > On 28 Sep 2022, at 22:37, Maxim Dounin <mdounin at mdounin.ru> wrote:
> > > 
> > > On Mon, Sep 26, 2022 at 02:17:18PM +0400, Sergey Kandaurov wrote:
> 
> [...]
> 
> > >> And by the way, while reviewing this patch, I noticed that
> > >> OpenSSL doesn't allow a client to gracefully renew TLSv1.2 session
> > >> when the client receives a new session ticket in resumed sessions.
> > >> In practice, it is visible when client resumes a not yet expired
> > >> session encrypted with not a fresh ticket key (after rotation),
> > >> which results in sending a new session ticket.
> > >> See ssl_update_cache() for the !s->hit condition.
> > >> In the opposite, BoringSSL always allows to renew TLSv1.2 sessions.
> > > 
> > > You mean on the client side?  Yes, it looks like 
> > > ngx_ssl_new_client_session() won't be called for such a new 
> > > session ticket, and updated ticket will be never saved.  This 
> > > might need to be worked around.
> > 
> > Yes, I mean the client side.
> > 
> > > 
> > > This should be safe with the key rotation logic introduced in this 
> > > patch though, given that the previous key is preserved till the 
> > > last ticket encrypted with it is expected to expire.
> > > 
> > > One of the possible solutions might be to avoid re-encryption of 
> > > tickets with the new key, as the old key is anyway expected to be 
> > > available till the session expires.
> > 
> > I don't think it's worth the effort.  If I got you right, and
> > as far as I understand, re-encrypting the ticket essentially
> > means sending a fresh session (renewal).
> 
> Well, not really.  Re-encryption of a ticket does not imply 
> session renewal.  Further, doing so implies security risk: if we 
> renew a session during re-encryption, this makes it possible to 
> create essentially infinite sessions.  And, for example, if a 
> session used a client certificate, this effectively means that 
> this certificate will never expire and cannot be revoked.
> 
> With TLSv1.2, OpenSSL follows this logic: session expiration time 
> is set when a session is created, and ticket re-encryption only 
> re-encrypts the session, but doesn't change session expiration.  
> As such, any certificate validation which happens during session 
> creation needs to be redone once session timeout expires - and 
> this makes it possible to implement certificate revocation.
> 
> On the other hand, as implemented for TLSv1.3 at least in OpenSSL 
> it seems not to be the case.  Every ticket sent to the client 
> actually creates a new session with _updated_ expiration time.  As 
> such, it is possible to create a session authenticated with a client 
> certificate, and use this session indefinitely, even after the 
> certificate will expire and/or will be revoked.
> 
> This seems to be a security issue in OpenSSL.
> 
> BoringSSL seems to behave similarly with TLSv1.3, that is, it 
> updates session expiration time, making it possible to use an 
> authenticated session for much longer than session timeout 
> configured.  But BoringSSL also has session's auth_timeout, which 
> prevents indefinite use of the session.  The auth_timeout value is 
> hardcoded to 7 days (SSL_DEFAULT_SESSION_AUTH_TIMEOUT), and does 
> not seem to be adjustable (only with SSL_SESSION_set_timeout(), 
> which is documented to be a function for writing tests).
> 
> I would rather say it is also a security issue in BoringSSL, 
> though it's slightly better than in OpenSSL due to the 7 days 
> limit.

For the record:

https://github.com/openssl/openssl/issues/19341

Note that with the automatic ticket key rotation this issue with 
TLSv1.3 sessions becomes slightly worse in a typical configuration 
(with ssl_session_cache in shared memory, but without 
ssl_session_ticket_key explicitly set and/or ssl_session_tickets 
switched off).  Notably, configuration reload is no longer enough 
to invalidate all tickets, since ticket keys are now preserved in 
shared memory.

For example, consider that a CRL file is updated with new 
revocations, and nginx configuration is reloaded.  New revocations 
will be loaded by nginx and will appear to work with new sessions, 
but can be easily bypassed by maintaining a previously established 
TLSv1.3 session.  Previously, it was possible to bypass 
revocations in such scenario only if ticket keys were explicitly 
set or if session tickets were switched off and sessions were 
cached in shared memory.

Given that we do not enable TLSv1.3 by default, we probably can 
ignore this and wait for appropriate fixes from the affected 
libraries.  On the other hand, it might be a good idea to 
introduce a workaround, especially if we want to enable TLSv1.3 by 
default in the foreseeable future.

The following patch seems to be simple enough and forces session 
timeouts for TLSv1.3 sessions to be as configured, for both 
OpenSSL and BoringSSL:

# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1665286021 -10800
#      Sun Oct 09 06:27:01 2022 +0300
# Node ID c0ec4df7ccbb95b7f2c2842f40012082991bed52
# Parent  37a4ac7ba1c5a003ab85f73d77767058af4eae30
SSL: workaround for session timeout handling with TLSv1.3.

OpenSSL with TLSv1.3 updates the session creation time on session
resumption and keeps the session timeout unmodified, making it possible
to maintain the session forever, bypassing client certificate expiration
and revocation.  To make sure session timeouts are actually used, we
now update the session creation time and reduce the session timeout
accordingly.

BoringSSL with TLSv1.3 ignores configured session timeouts and uses a
hardcoded timeout instead, 7 days.  So we update session timeout to
the configured value as soon as a session is created.

diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -1086,6 +1086,53 @@ ngx_ssl_info_callback(const ngx_ssl_conn
 
 #endif
 
+#ifdef TLS1_3_VERSION
+
+    if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP
+        && SSL_version(ssl_conn) == TLS1_3_VERSION)
+    {
+        time_t        now, time, timeout, conf_timeout;
+        SSL_SESSION  *sess;
+
+        /*
+         * OpenSSL with TLSv1.3 updates the session creation time on
+         * session resumption and keeps the session timeout unmodified,
+         * making it possible to maintain the session forever, bypassing
+         * client certificate expiration and revocation.  To make sure
+         * session timeouts are actually used, we now update the session
+         * creation time and reduce the session timeout accordingly.
+         *
+         * BoringSSL with TLSv1.3 ignores configured session timeouts
+         * and uses a hardcoded timeout instead, 7 days.  So we update
+         * session timeout to the configured value as soon as a session
+         * is created.
+         */
+
+        c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn);
+        sess = SSL_get0_session(ssl_conn);
+
+        if (!c->ssl->session_timeout_set && sess) {
+            c->ssl->session_timeout_set = 1;
+
+            now = ngx_time();
+            time = SSL_SESSION_get_time(sess);
+            timeout = SSL_SESSION_get_timeout(sess);
+            conf_timeout = SSL_CTX_get_timeout(c->ssl->session_ctx);
+
+            timeout = ngx_min(timeout, conf_timeout);
+
+            if (now - time >= timeout) {
+                SSL_SESSION_set1_id_context(sess, (unsigned char *) "", 0);
+
+            } else {
+                SSL_SESSION_set_time(sess, now);
+                SSL_SESSION_set_timeout(sess, timeout - (now - time));
+            }
+        }
+    }
+
+#endif
+
     if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
         c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn);
 
diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h
--- a/src/event/ngx_event_openssl.h
+++ b/src/event/ngx_event_openssl.h
@@ -114,6 +114,7 @@ struct ngx_ssl_connection_s {
     unsigned                    no_send_shutdown:1;
     unsigned                    shutdown_without_free:1;
     unsigned                    handshake_buffer_set:1;
+    unsigned                    session_timeout_set:1;
     unsigned                    try_early_data:1;
     unsigned                    in_early:1;
     unsigned                    in_ocsp:1;

-- 
Maxim Dounin
http://mdounin.ru/



More information about the nginx-devel mailing list