From: "plaisthos (Code Review)" Date: Mon, 20 Nov 2023 17:10:59 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <83dc89926b2ed38485070f38ea6db9bac32b8e76-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="eqK8mrt0e7A="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Allow the TLS session to send out TLS alerts List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel --eqK8mrt0e7A= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello flichtenheld, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/449?usp=3Demail to look at the new patch set (#5). Change subject: Allow the TLS session to send out TLS alerts ...................................................................... Allow the TLS session to send out TLS alerts Previous OpenVPN versions shut down the TLS control channel immediately when encountering an error. This also meant that we would not send out TLS alerts to notify a client about potential problems like mismatching TLS versions or having no common cipher. This commit adds a new key_state S_ERROR_PRE which still allows to send out the remaining TLS packets of the control session which are typically the alert message and then going to S_ERROR. We do not wait for retries. So this is more a one-shot notify but that is acceptable in this situation. Sending out alerts is a slight compromise in security as alerts give out a bit of information that otherwise is not given out. But since all other consumers TLS implementations are already doing th= is and TLS implementations (nowadays) are very careful not to leak (sensitive) information by alerts and since the user experience is much better with alerts, this compromise is worth it. Change-Id: I0ad48915004ddee587e97c8ed190ba8ee989e48d --- M Changes.rst M src/openvpn/ssl.c M src/openvpn/ssl_backend.h M src/openvpn/ssl_common.h M src/openvpn/ssl_mbedtls.c M src/openvpn/ssl_openssl.c 6 files changed, 89 insertions(+), 19 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/49/449/5 diff --git a/Changes.rst b/Changes.rst index 3676dce..dbef0d9 100644 --- a/Changes.rst +++ b/Changes.rst @@ -1,5 +1,16 @@ Overview of changes in 2.7 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D +New features +------------ +TLS alerts + OpenVPN 2.7 will send out TLS alerts to peers informing them if the TLS + session shuts down or when the TLS implementation informs the peer abo= ut + an error in the TLS session (e.g. mismatching TLS versions). This impr= oves + the user experience as the client shows an error instead of running in= to + a timeout when the server just stops responding completely. + +Deprecated features +------------------- ``secret`` support has been removed by default. static key mode (non-TLS) is no longer considered "good and secure eno= ugh" for today's requirements. Use TLS mode instead. If deploying a PKI CA diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 20f4956..f794a37 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -853,6 +853,9 @@ case S_ERROR: return "S_ERROR"; + case S_ERROR_PRE: + return "S_ERROR_PRE"; + case S_GENERATED_KEYS: return "S_GENERATED_KEYS"; @@ -2840,6 +2843,25 @@ } static bool +check_outgoing_ciphertext(struct key_state *ks, struct tls_session *sessio= n, + bool *continue_tls_process) +{ + /* Outgoing Ciphertext to reliable buffer */ + if (ks->state >=3D S_PRE_START) + { + struct buffer *buf =3D reliable_get_buf_output_sequenced(ks->send_= reliable); + if (buf) + { + if (!write_outgoing_tls_ciphertext(session, continue_tls_proce= ss)) + { + return false; + } + } + } + return true; +} + +static bool tls_process_state(struct tls_multi *multi, struct tls_session *session, struct buffer *to_link, @@ -2863,7 +2885,7 @@ } /* Are we timed out on receive? */ - if (now >=3D ks->must_negotiate && ks->state < S_ACTIVE) + if (now >=3D ks->must_negotiate && ks->state >=3D S_UNDEF && ks->state= < S_ACTIVE) { msg(D_TLS_ERRORS, "TLS Error: TLS key negotiation failed to occur within %d seco= nds (check your network connectivity)", @@ -2917,6 +2939,16 @@ return false; } + if (ks->state =3D=3D S_ERROR_PRE) + { + /* When we end up here, we had one last chance to send an outstand= ing + * packet that contained an alert. We do not ensure that this pack= et + * has been successfully delivered (ie wait for the ACK etc) + * but rather stop processing now */ + ks->state =3D S_ERROR; + return false; + } + /* Write incoming ciphertext to TLS object */ struct reliable_entry *entry =3D reliable_get_entry_sequenced(ks->rec_= reliable); if (entry) @@ -2997,29 +3029,31 @@ dmsg(D_TLS_DEBUG, "Outgoing Plaintext -> TLS"); } } - - /* Outgoing Ciphertext to reliable buffer */ - if (ks->state >=3D S_START) + if (!check_outgoing_ciphertext(ks, session, &continue_tls_process)) { - buf =3D reliable_get_buf_output_sequenced(ks->send_reliable); - if (buf) - { - if (!write_outgoing_tls_ciphertext(session, &continue_tls_proc= ess)) - { - goto error; - } - } + goto error; } return continue_tls_process; error: tls_clear_error(); - ks->state =3D S_ERROR; + + /* Shut down the TLS session but do a last read from the TLS + * object to be able to read potential TLS alerts */ + key_state_ssl_shutdown(&ks->ks_ssl); + check_outgoing_ciphertext(ks, session, &continue_tls_process); + + /* Put ourselves in the pre error state that will only send out the + * control channel packets but nothing else */ + ks->state =3D S_ERROR_PRE; + msg(D_TLS_ERRORS, "TLS Error: TLS handshake failed"); INCR_ERROR; - return false; - + return true; } + + + /* * This is the primary routine for processing TLS stuff inside the * the main event loop. When this routine exits @@ -3127,7 +3161,7 @@ } /* When should we wake up again? */ - if (ks->state >=3D S_INITIAL) + if (ks->state >=3D S_INITIAL || ks->state =3D=3D S_ERROR_PRE) { compute_earliest_wakeup(wakeup, reliable_send_timeout(ks->send_reliable)); @@ -3280,7 +3314,7 @@ session_id_print(&ks->session_id_remote, &gc), print_link_socket_actual(&ks->remote_addr, &gc)); - if (ks->state >=3D S_INITIAL && link_socket_actual_defined(&ks->re= mote_addr)) + if ((ks->state >=3D S_INITIAL || ks->state =3D=3D S_ERROR_PRE) && = link_socket_actual_defined(&ks->remote_addr)) { struct link_socket_actual *tla =3D NULL; @@ -3358,7 +3392,8 @@ { msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion faile= d"); ks->authenticated =3D KS_AUTH_FALSE; - ks->state =3D S_ERROR; + key_state_ssl_shutdown(&ks->ks_ssl); + ks->state =3D S_ERROR_PRE; } /* Update auth token on the client if needed on renegotiation diff --git a/src/openvpn/ssl_backend.h b/src/openvpn/ssl_backend.h index 3854d59..1ca9745 100644 --- a/src/openvpn/ssl_backend.h +++ b/src/openvpn/ssl_backend.h @@ -372,6 +372,13 @@ const struct tls_root_ctx *ssl_ctx, bool is_server= , struct tls_session *session); /** + * Sets a TLS session to be shutdown state, so the TLS library will genera= te + * a shutdown alert. + */ +void +key_state_ssl_shutdown(struct key_state_ssl *ks_ssl); + +/** * Free the SSL channel part of the given key state. * * @param ks_ssl The SSL channel's state info to free diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index d3edc5f..d38d8ec 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -74,7 +74,10 @@ * * @{ */ -#define S_ERROR -1 /**< Error state. */ +#define S_ERROR (-2) /**< Error state. */ +#define S_ERROR_PRE (-1) /**< Error state but try to send out aler= ts + * before killing the keystore and moving + * it to S_ERROR */ #define S_UNDEF 0 /**< Undefined state, used after a \c * key_state is cleaned up. */ #define S_INITIAL 1 /**< Initial \c key_state state after diff --git a/src/openvpn/ssl_mbedtls.c b/src/openvpn/ssl_mbedtls.c index 9c9167d..c8aca4f 100644 --- a/src/openvpn/ssl_mbedtls.c +++ b/src/openvpn/ssl_mbedtls.c @@ -1275,6 +1275,14 @@ ssl_bio_read, NULL); } + +void +key_state_ssl_shutdown(struct key_state_ssl *ks_ssl) +{ + mbedtls_ssl_send_alert_message(ks_ssl->ctx, MBEDTLS_SSL_ALERT_LEVEL_FA= TAL, + MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY); +} + void key_state_ssl_free(struct key_state_ssl *ks_ssl) { diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c index 82872bf..622b9b5 100644 --- a/src/openvpn/ssl_openssl.c +++ b/src/openvpn/ssl_openssl.c @@ -1961,6 +1961,12 @@ } void +key_state_ssl_shutdown(struct key_state_ssl *ks_ssl) +{ + SSL_set_shutdown(ks_ssl->ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOW= N); +} + +void key_state_ssl_free(struct key_state_ssl *ks_ssl) { if (ks_ssl->ssl) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/449?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I0ad48915004ddee587e97c8ed190ba8ee989e48d Gerrit-Change-Number: 449 Gerrit-PatchSet: 5 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --eqK8mrt0e7A= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

plaisthos uploaded patch set #5 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/449?usp=3Demail">View Chang= e

Allow the TLS session to send out TLS alerts

Previous OpenVPN = versions shut down the TLS control channel immediately
when encountering= an error. This also meant that we would not send out
TLS alerts to noti= fy a client about potential problems like mismatching
TLS versions or ha= ving no common cipher.

This commit adds a new key_state S_ERROR_PRE = which still allows to
send out the remaining TLS packets of the control = session which are
typically the alert message and then going to S_ERROR.= We do not
wait for retries. So this is more a one-shot notify but that = is
acceptable in this situation.

Sending out alerts is a slight c= ompromise in security as alerts give
out a bit of information that other= wise is not given
out. But since all other consumers TLS implementations= are already doing this
and TLS implementations (nowadays) are very care= ful not to leak (sensitive)
information by alerts and since the user exp= erience is much better with
alerts, this compromise is worth it.

= Change-Id: I0ad48915004ddee587e97c8ed190ba8ee989e48d
---
M Changes.rs= t
M src/openvpn/ssl.c
M src/openvpn/ssl_backend.h
M src/openvpn/ss= l_common.h
M src/openvpn/ssl_mbedtls.c
M src/openvpn/ssl_openssl.c6 files changed, 89 insertions(+), 19 deletions(-)

git pull ssh:=
//gerrit.openvpn.net:29418/openvpn refs/changes/49/449/5
diff --git=
 a/Changes.rst b/Changes.rst
index 3676dce..dbef0d9 100644<= /span>
--- a/Changes.rst
+++ b/Changes.rst<= br>@@ -1,5 +1,16 @@
Overview of changes in 2.7
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D
+New = features
+-----------= -
+TLS alerts<= br>+ OpenVPN 2.7 will send o= ut TLS alerts to peers informing them if the TLS
+ session shuts down or when the TLS impleme= ntation informs the peer about
+ an error in the TLS session (e.g. mismatching TLS versions).= This improves
+ t= he user experience as the client shows an error instead of running into
+ a timeout when the = server just stops responding completely.
+
+Deprecated features
+-------------------
``secret`` support has been removed = by default.
static key mode (non-TLS) is no longer con= sidered "good and secure enough"
for today&#= 39;s requirements. Use TLS mode instead. If deploying a PKI CA
= diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c
in= dex 20f4956..f794a37 100644
--- a/src/openvpn/ssl.c<= br>+++ b/src/openvpn/ssl.c
@@ -853,6 +853,9 @@=
case S_ERROR:
return "= S_ERROR";

+ case S_ERROR_PRE:
+ return "S_ERROR_PRE";
+

case S= _GENERATED_KEYS:
return "S_GENERATED_KEYS= ";

@@ -2840,6 +2843,25 @@
= }

static bool
+check_outgoing_ciphertext(struct key_st= ate *ks, struct tls_session *session,
+ bool *continue_tls_process)
+{
+ /* Outgoing Ciphertext to reliable bu= ffer */
+ if (ks-&= gt;state >=3D S_PRE_START)
+ {
+ = struct buffer *buf =3D reliable_get_buf_output_sequenced(ks->send_re= liable);
+ if = (buf)
+ {
+ if (!write_o= utgoing_tls_ciphertext(session, continue_tls_process))
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
= +static bool
t= ls_process_state(struct tls_multi *multi,
= struct tls_session *session,
struct bu= ffer *to_link,
@@ -2863,7 +2885,7 @@
}=

/* Are we timed out on receive? */<= /span>
- if (now >=3D ks= ->must_negotiate && ks->state < S_ACTIVE)
+ if (now >=3D ks->must_nego= tiate && ks->state >=3D S_UNDEF && ks->state < = S_ACTIVE)
{
msg(D_TLS_ERRORS,=
"TLS Error: TLS key negotiation failed t= o occur within %d seconds (check your network connectivity)",@@ -2917,6 +2939,16 @@
return false;
}

+ if (ks->state =3D=3D S_ERROR_PRE)
+ {
+ /* When we end up here, we had one last chan= ce to send an outstanding
+ * packet that contained an alert. We do not ensure that this= packet
+ * h= as been successfully delivered (ie wait for the ACK etc)
+ * but rather stop processing= now */
+ ks-&= gt;state =3D S_ERROR;
+ return false;
+ }
+ /* Write incoming ciphertext to TLS object */
= struct reliable_entry *entry =3D reliable_get_entry_sequenced(ks->re= c_reliable);
if (entry)
@@ -2997,29 +3= 029,31 @@
dmsg(D_TLS_DEBUG, "Outgoing Pla= intext -> TLS");
}
}<= /span>
-
- /* Outgoing Ciphertext to reliable buff= er */
- if (ks->s= tate >=3D S_START)
+ if (!check_outgoing_ciphertext(ks, session, &continue_tls_process= ))
{
- buf =3D reliable_get_buf_output_sequenced(ks->send_reliable)= ;
- if (buf)
- {
- if (!write_outgoing_tls_c= iphertext(session, &continue_tls_process))

- {
- goto error;
- }
- }
+ goto error;
}
return continue_tls_process;
error:
= tls_clear_error();
- ks->state =3D S_ERROR;
+
+ = /* Shut down the TLS session but do a last read from the TLS
<= span style=3D"color: hsl(120, 100%, 40%);">+ * object to be able to rea= d potential TLS alerts */

+ key_state_ssl_shutdown(&ks->ks_ssl);
+ check_outgoing_ciphertext(ks, session= , &continue_tls_process);
+
+ /* Pu= t ourselves in the pre error state that will only send out the
+ * control channel packets b= ut nothing else */

+ = ks->state =3D S_ERROR_PRE;
+
msg(D_TLS_ERRORS, "TLS Error: TLS h= andshake failed");
INCR_ERROR;
- return false;
-
+ return true;
}
+
+
+ /*
* This is the primary routine for processing = TLS stuff inside the
* the main event loop. When this ro= utine exits
@@ -3127,7 +3161,7 @@
}

/* When should we wake up again? */
- if (ks->state >= ;=3D S_INITIAL)
+ = if (ks->state >=3D S_INITIAL || ks->state =3D=3D S_ERROR_PRE)
{
compute_earliest_wakeup(wakeup,=
reliable_send_timeout(ks-= >send_reliable));
@@ -3280,7 +3314,7 @@
= session_id_print(&ks->session_id_remote, &gc),
print_link_socket_actual(&ks->remote_addr= , &gc));

- if (ks->state >=3D S_INITIAL && link_socket_= actual_defined(&ks->remote_addr))
+ if ((ks->state >=3D S_INITIAL || ks->= state =3D=3D S_ERROR_PRE) && link_socket_actual_defined(&ks-&g= t;remote_addr))
{
str= uct link_socket_actual *tla =3D NULL;

@@ = -3358,7 +3392,8 @@
{
= msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion failed&qu= ot;);
ks->authenticated =3D KS_AUTH_FAL= SE;
- ks= ->state =3D S_ERROR;
+ key_state_ssl_shutdown(&ks->ks_ssl);
<= span style=3D"color: hsl(120, 100%, 40%);">+ ks->state = =3D S_ERROR_PRE;

}
/* Update auth token on the client if needed on renegot= iation
diff --git a/src/openvpn/ssl_backend.h b/src/openvpn= /ssl_backend.h
index 3854d59..1ca9745 100644
--- a/src/openvpn/ssl_backend.h

+++ b/src/openvpn/ssl_bac= kend.h
@@ -372,6 +372,13 @@
= const struct tls_root_ctx *ssl_ctx, bool is_server, struct tls_se= ssion *session);

/**
+ * Sets a TLS session to be shutdown st= ate, so the TLS library will generate
+ * a shutdown alert.
+ */
+key_state_ss= l_shutdown(struct key_state_ssl *ks_ssl);
+
-#define S_ERROR -1 /**< Error st= ate. */
+#define S_E= RROR (-2) /**< Error state. */
+#define S_ERROR_PRE (-1) /**< Error s= tate but try to send out alerts
+ * before killing the keystor= e and moving
+ = * it to S_ERROR */
#define S_U= NDEF 0 /**< Undefined state, used after a \c
* key_state is cleaned up. */
=
#define S_INITIAL 1 /**< Initial \c key_state sta= te after
diff --git a/src/openvpn/ssl_mbedtls.c b/src/openv= pn/ssl_mbedtls.c
index 9c9167d..c8aca4f 100644
--- a/src/openvpn/ssl_mbedtls.c

+++ b/src/openvpn/ssl_m= bedtls.c
@@ -1275,6 +1275,14 @@
= ssl_bio_read, NULL);
}

+
+void
+key_state_ssl_shutdown(struct key_state_ssl *ks_ssl)
+{
+ mbedtls_ssl_send_alert_message(ks_ssl= ->ctx, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
+ MBEDTLS_SSL_ALERT_M= SG_CLOSE_NOTIFY);
+}<= /span>
+
vo= id
key_state_ssl_free(struct key_state_ssl *ks_ssl)=
{
diff --git a/src/openvpn/ssl_openssl.c b/src/o= penvpn/ssl_openssl.c
index 82872bf..622b9b5 100644--- a/src/openvpn/ssl_openssl.c
+++ b/src/openvpn/s= sl_openssl.c
@@ -1961,6 +1961,12 @@
}

void
+key_state_ssl_shutdown(struct key_state_ssl *ks_ssl)=
+{
+ SSL_set_shutdown(ks_ssl->ssl, SSL_SEN= T_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
+}
+<= /span>
+void
key_state_ssl_free(struct key_state_ssl *ks_ssl)

{
if (ks_ssl->ssl)

To = view, visit change 449. To unsubscribe, or for help writing mail filters, visit = settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I0ad48915004ddee587e97c8ed19= 0ba8ee989e48d
Gerrit-Change-Number: 449
Gerrit-PatchSet: 5
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--eqK8mrt0e7A=--