From: "plaisthos (Code Review)" Date: Tue, 31 Mar 2026 22:49:56 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., davidben@...410..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="VIWutFsRGXk="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: ssl_openssl: Fix some CRL mixups List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: davidben , flichtenheld Cc: openvpn-devel --VIWutFsRGXk= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: davidben, flichtenheld, plaisthos. plaisthos has uploaded a new patch set (#10) to the change originally creat= ed by davidben. ( http://gerrit.openvpn.net/c/openvpn/+/1289?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by plaisthos Change subject: ssl_openssl: Fix some CRL mixups ...................................................................... ssl_openssl: Fix some CRL mixups There are two ways to load CRLs in OpenSSL. They can be loaded at the X509_STORE, shared across verifications, or loaded per verification at the X509_STORE_CTX. OpenVPN currently does the former. However, it also supports CRL reloading, and tries to reload the CRL file before each connection. OpenSSL does not really have a good way to unload objects from an X509_STORE. OpenVPN currently does it by grabbing the STACK_OF(X509_OBJECT) out of the X509_STORE and manually deleting all the CRLs from it. This mutates an OpenSSL internal object which bumps into problems if OpenSSL ever switches to a more efficient representation. See https://github.com/openssl/openssl/pull/28599 (It's also not thread-safe, though it doesn't look like that impacts OpenVPN? Actually even reading that list doesn't work. See CVE-2024-0397. This OpenSSL API was simply broken.) Additionally, this seems to cause two OpenVPN features to not work together. I gather backend_tls_ctx_reload_crl is trying to clear the CRLs loaded from last time it ran. But tls_ctx_load_ca with a ca_file can also load CRLs. tls_ctx_load_ca with ca_path will also pick up CRLs and backend_tls_ctx_reload_crl actually ends up clobbering some state X509_LOOKUP_hash_dir internally maintains on the X509_STORE. Likewise, tls_verify_crl_missing can get confused between backend_tls_ctx_reload_crl's crl_file-based CRLs and CRLs from tls_ctx_load_ca. Avoid all this by tracking the two CRLs separately. crl_file-based CRLs now go onto a STACK_OF(X509_CRL) tracked on the tls_root_ctx. Now this field can be freely reloaded by OpenVPN without reconfiguring OpenSSL. Instead, pass the current value into OpenSSL at verification time. To do so, we need to use the SSL_CTX_set_cert_verify_callback, which allows swapping out the X509_verify_cert call, and also tweaking the X509_STORE_CTX configuration before starting certificate verification. Context: SSL_CTX_set_cert_verify_callback and the existing verify_callback are not the same. SSL_CTX_set_cert_verify_callback wraps the verification while verify_callback is called multiple times throughout verification. It's too late to reconfigure X509_STORE_CTX in verify_callback. verify_callback is usually not what you want. Sometimes current_cert and error_depth don't quite line up, and cert_hash_remember may end up called multiple times for a single certificate. I suspect some of the other verify_callback logic would also be better done in the new callback, but I've left it alone to keep this change minimal. verify_callback is really only usable for suppressing errors. Application bookkeeping is better down elsewhere. Add .clang-format section for STACK_OF since we otherwise format the line as STACK_OF(X509_CRL) * crls Signed-off-by: David Benjamin Change-Id: I31ac2a763209114267c35c4a9182a12d8d82f6fe Signed-off-by: Arne Schwabe --- M .clang-format M src/openvpn/openssl_compat.h M src/openvpn/ssl_openssl.c M src/openvpn/ssl_openssl.h M src/openvpn/ssl_verify_openssl.c 5 files changed, 38 insertions(+), 55 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/89/1289/10 diff --git a/.clang-format b/.clang-format index cbec0a4..4774d1e 100644 --- a/.clang-format +++ b/.clang-format @@ -43,7 +43,10 @@ SpacesBeforeTrailingComments: '2' SpacesInParens: Never TabWidth: '4' -TypeNames: [DWORD] +TypeNames: + - 'DWORD' + - 'STACK_OF' +TypenameMacros: ['STACK_OF'] UseTab: Never WhitespaceSensitiveMacros: [_STRINGIFY] --- diff --git a/src/openvpn/openssl_compat.h b/src/openvpn/openssl_compat.h index 8fdb39a..3494ce6 100644 --- a/src/openvpn/openssl_compat.h +++ b/src/openvpn/openssl_compat.h @@ -61,21 +61,6 @@ /* Functionality missing in LibreSSL before 3.5 */ #if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x305000= 0fL -/** - * Destroy a X509 object - * - * @param obj X509 object - */ -static inline void -X509_OBJECT_free(X509_OBJECT *obj) -{ - if (obj) - { - X509_OBJECT_free_contents(obj); - OPENSSL_free(obj); - } -} - #define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG #define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG #endif diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c index a26663a..c8dd55a 100644 --- a/src/openvpn/ssl_openssl.c +++ b/src/openvpn/ssl_openssl.c @@ -141,6 +141,8 @@ ASSERT(NULL !=3D ctx); SSL_CTX_free(ctx->ctx); ctx->ctx =3D NULL; + sk_X509_CRL_pop_free(ctx->crls, X509_CRL_free); + ctx->crls =3D NULL; unload_xkey_provider(); /* in case it is loaded */ } @@ -304,6 +306,22 @@ return true; } +static int +cert_verify_callback(X509_STORE_CTX *ctx, void *arg) +{ + struct tls_session *session; + SSL *ssl; + + ssl =3D X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX= _idx()); + ASSERT(ssl); + session =3D SSL_get_ex_data(ssl, mydata_index); + ASSERT(session); + + /* Configure CRLs. */ + X509_STORE_CTX_set0_crls(ctx, session->opt->ssl_ctx->crls); + return X509_verify_cert(ctx); +} + bool tls_ctx_set_options(struct tls_root_ctx *ctx, unsigned int ssl_flags) { @@ -346,6 +364,7 @@ verify_flags =3D SSL_VERIFY_PEER; } SSL_CTX_set_verify(ctx->ctx, verify_flags, verify_callback); + SSL_CTX_set_cert_verify_callback(ctx->ctx, cert_verify_callback, NULL); SSL_CTX_set_info_callback(ctx->ctx, info_callback); @@ -1321,6 +1340,7 @@ backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_f= ile, bool crl_inline) { BIO *in =3D NULL; + STACK_OF(X509_CRL) *crls =3D NULL; X509_STORE *store =3D SSL_CTX_get_cert_store(ssl_ctx->ctx); if (!store) @@ -1328,20 +1348,8 @@ crypto_msg(M_FATAL, "Cannot get certificate store"); } - /* Always start with a cleared CRL list, for that we - * we need to manually find the CRL object from the stack - * and remove it */ - STACK_OF(X509_OBJECT) *objs =3D X509_STORE_get0_objects(store); - for (int i =3D 0; i < sk_X509_OBJECT_num(objs); i++) - { - X509_OBJECT *obj =3D sk_X509_OBJECT_value(objs, i); - ASSERT(obj); - if (X509_OBJECT_get_type(obj) =3D=3D X509_LU_CRL) - { - sk_X509_OBJECT_delete(objs, i); - X509_OBJECT_free(obj); - } - } + sk_X509_CRL_pop_free(ssl_ctx->crls, X509_CRL_free); + ssl_ctx->crls =3D NULL; X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CH= ECK_ALL); @@ -1357,7 +1365,13 @@ if (in =3D=3D NULL) { msg(M_WARN, "CRL: cannot read: %s", print_key_filename(crl_file, c= rl_inline)); - goto end; + return; + } + + crls =3D sk_X509_CRL_new_null(); + if (crls =3D=3D NULL) + { + crypto_msg(M_FATAL, "CRL: cannot create CRL list"); } int num_crls_loaded =3D 0; @@ -1383,18 +1397,14 @@ break; } - if (!X509_STORE_add_crl(store, crl)) + if (!sk_X509_CRL_push(crls, crl)) { - X509_CRL_free(crl); - crypto_msg(M_WARN, "CRL: cannot add %s to store", - print_key_filename(crl_file, crl_inline)); - break; + crypto_msg(M_FATAL, "CRL: cannot add CRL to list"); } - X509_CRL_free(crl); num_crls_loaded++; } msg(M_INFO, "CRL: loaded %d CRLs from file %s", num_crls_loaded, crl_f= ile); -end: + ssl_ctx->crls =3D crls; BIO_free(in); } diff --git a/src/openvpn/ssl_openssl.h b/src/openvpn/ssl_openssl.h index 3926e91..373dc10 100644 --- a/src/openvpn/ssl_openssl.h +++ b/src/openvpn/ssl_openssl.h @@ -41,6 +41,7 @@ SSL_CTX *ctx; time_t crl_last_mtime; off_t crl_last_size; + STACK_OF(X509_CRL) *crls; }; struct key_state_ssl diff --git a/src/openvpn/ssl_verify_openssl.c b/src/openvpn/ssl_verify_open= ssl.c index 7c0cbb3..20188f4 100644 --- a/src/openvpn/ssl_verify_openssl.c +++ b/src/openvpn/ssl_verify_openssl.c @@ -783,23 +783,7 @@ return false; } - X509_STORE *store =3D SSL_CTX_get_cert_store(opt->ssl_ctx->ctx); - if (!store) - { - crypto_msg(M_FATAL, "Cannot get certificate store"); - } - - STACK_OF(X509_OBJECT) *objs =3D X509_STORE_get0_objects(store); - for (int i =3D 0; i < sk_X509_OBJECT_num(objs); i++) - { - X509_OBJECT *obj =3D sk_X509_OBJECT_value(objs, i); - ASSERT(obj); - if (X509_OBJECT_get_type(obj) =3D=3D X509_LU_CRL) - { - return false; - } - } - return true; + return opt->ssl_ctx->crls =3D=3D NULL || sk_X509_CRL_num(opt->ssl_ctx-= >crls) =3D=3D 0; } #endif /* defined(ENABLE_CRYPTO_OPENSSL) */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1289?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I31ac2a763209114267c35c4a9182a12d8d82f6fe Gerrit-Change-Number: 1289 Gerrit-PatchSet: 10 Gerrit-Owner: davidben Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-Attention: davidben --VIWutFsRGXk= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: davidben, flichtenheld, plaisthos.

plaisthos uploaded patch set #10 to the change original= ly created by davidben.

View Change

The following approvals got out= dated and were removed: Code-Review+2 by plaisthos

ssl_openssl: Fix some CRL mixup=
s

There are two ways to load CRLs in OpenSSL. They can be loaded at = the
X509_STORE, shared across verifications, or loaded per verification = at
the X509_STORE_CTX.

OpenVPN currently does the former. However= , it also supports CRL
reloading, and tries to reload the CRL file befor= e each connection.
OpenSSL does not really have a good way to unload obj= ects from an
X509_STORE. OpenVPN currently does it by grabbing the
ST= ACK_OF(X509_OBJECT) out of the X509_STORE and manually deleting all
the = CRLs from it.

This mutates an OpenSSL internal object which bumps in= to problems if
OpenSSL ever switches to a more efficient representation.= See
https://github.com/openssl/openssl/pull/28599

(It's also= not thread-safe, though it doesn't look like that impacts
OpenVPN? = Actually even reading that list doesn't work. See
CVE-2024-0397. Thi= s OpenSSL API was simply broken.)

Additionally, this seems to cause = two OpenVPN features to not work
together. I gather backend_tls_ctx_relo= ad_crl is trying to clear the
CRLs loaded from last time it ran. But tls= _ctx_load_ca with a ca_file
can also load CRLs. tls_ctx_load_ca with ca_= path will also pick up CRLs
and backend_tls_ctx_reload_crl actually ends= up clobbering some state
X509_LOOKUP_hash_dir internally maintains on t= he X509_STORE. Likewise,
tls_verify_crl_missing can get confused between=
backend_tls_ctx_reload_crl's crl_file-based CRLs and CRLs from
t= ls_ctx_load_ca.

Avoid all this by tracking the two CRLs separately. = crl_file-based CRLs
now go onto a STACK_OF(X509_CRL) tracked on the tls_= root_ctx. Now this
field can be freely reloaded by OpenVPN without recon= figuring OpenSSL.
Instead, pass the current value into OpenSSL at verifi= cation time. To
do so, we need to use the SSL_CTX_set_cert_verify_callb= ack, which allows
swapping out the X509_verify_cert call, and also tweak= ing the
X509_STORE_CTX configuration before starting certificate verific= ation.

Context: SSL_CTX_set_cert_verify_callback and the existingverify_callback are not the same. SSL_CTX_set_cert_verify_callback wrapsthe verification while verify_callback is called multiple times
throug= hout verification. It's too late to reconfigure X509_STORE_CTX in
ve= rify_callback. verify_callback is usually not what you want.
Sometimes c= urrent_cert and error_depth don't quite line up, and
cert_hash_remem= ber may end up called multiple times for a single
certificate.

I = suspect some of the other verify_callback logic would also be better
don= e in the new callback, but I've left it alone to keep this change
mi= nimal. verify_callback is really only usable for suppressing errors.
App= lication bookkeeping is better down elsewhere.

Add .clang-format sec= tion for STACK_OF since we otherwise format the
line as STACK_OF(X509_CR= L) * crls

Signed-off-by: David Benjamin <davidben@...410...>Change-Id: I31ac2a763209114267c35c4a9182a12d8d82f6fe
Signed-off-by: Ar= ne Schwabe <arne@...1227...>
---
M .clang-format
M src/openv= pn/openssl_compat.h
M src/openvpn/ssl_openssl.c
M src/openvpn/ssl_ope= nssl.h
M src/openvpn/ssl_verify_openssl.c
5 files changed, 38 inserti= ons(+), 55 deletions(-)

git pull ssh://gerrit.op=
envpn.net:29418/openvpn refs/changes/89/1289/10
diff --git a/.clang=
-format b/.clang-format
index cbec0a4..4774d1e 100644
--- a/.clang-format
+++ b/.clang-format@@ -43,7 +43,10 @@
SpacesBeforeTrailingComments: &= #39;2'
SpacesInParens: Never
TabWidth= : '4'
-TypeName= s: [DWORD]
+TypeNames= :
+ - 'DWORD'= ;
+ - 'STACK_OF&= #39;
+TypenameMacros:= ['STACK_OF']
UseTab: Never
White= spaceSensitiveMacros: [_STRINGIFY]
---
dif= f --git a/src/openvpn/openssl_compat.h b/src/openvpn/openssl_compat.h
index 8fdb39a..3494ce6 100644
--- a/src/openvpn/= openssl_compat.h
+++ b/src/openvpn/openssl_compat.h<= br>@@ -61,21 +61,6 @@

/* Functiona= lity missing in LibreSSL before 3.5 */
#if defined(LIBRESS= L_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x3050000fL
-/**
- * Destroy a X509 object
- *
- * @param obj X509 object
- */
-static inline void
-X509_OBJECT_free(X509_OBJECT *obj)
-{
- if (obj)
- = {
- X509_OBJEC= T_free_contents(obj);
-= OPENSSL_free(obj);
- }
-}-
#define EVP_C= TRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
#define EVP_CTRL_AEA= D_GET_TAG EVP_CTRL_GCM_GET_TAG
#endif
diff= --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c
index a26663a..c8dd55a 100644

--- a/src/openvpn/ssl_ope= nssl.c
+++ b/src/openvpn/ssl_openssl.c
@@ -= 141,6 +141,8 @@
ASSERT(NULL !=3D ctx);
SSL_CTX_free(ctx->ctx);

ctx->ctx =3D NULL;=
+ sk_X509_CRL_pop= _free(ctx->crls, X509_CRL_free);
+ ctx->crls =3D NULL;
unload_xkey= _provider(); /* in case it is loaded */
}
=
@@ -304,6 +306,22 @@
return true;
}

+static int
+cert_verify_callback(X509_STORE_CTX *ctx, void *arg)
+{
+ struct tls_session *session;
+ SSL *ssl;
+
+ ssl =3D X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_S= TORE_CTX_idx());
+ = ASSERT(ssl);
+ se= ssion =3D SSL_get_ex_data(ssl, mydata_index);
+ ASSERT(session);
+
+ /* Configure CRLs. */
+ X509_STORE_CTX_set0_crls(ctx, session->opt->ssl_ctx= ->crls);
+ retu= rn X509_verify_cert(ctx);
+}
+
<= span> bool

tls_ctx_set_options(struct tls_root_ctx *ctx, u= nsigned int ssl_flags)
{
@@ -346,6 +364,7 = @@
verify_flags =3D SSL_VERIFY_PEER;
}

SSL_CTX_set_verify(ctx->ctx, verify_flags= , verify_callback);
+= SSL_CTX_set_cert_verify_callback(ctx->ctx, cert_verify_callback, NUL= L);

SSL_CTX_set_info_callback(ctx-&g= t;ctx, info_callback);

@@ -1321,6 +1340,7= @@
backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ct= x, const char *crl_file, bool crl_inline)
{
BIO *in =3D NULL;
+ STACK_OF(X509_CRL) *crls =3D NULL;

X509_STORE *store =3D SSL_CTX_get_cert_store(ssl_ctx->ctx);

if (!store)
@@ -1328,20 +1348,8 @@<= br> crypto_msg(M_FATAL, "Cannot get certificate store&qu= ot;);
}

- /* Always start with a cleared CRL list, for t= hat we
- * we need = to manually find the CRL object from the stack
- * and remove it */
- STACK_OF(X509_OBJECT) *objs =3D X509_STORE_g= et0_objects(store);
- = for (int i =3D 0; i < sk_X509_OBJECT_num(objs); i++)
- {
- X509_OBJECT *obj =3D sk_X509_OBJECT_value(objs,= i);
- ASSERT(ob= j);
- if (X509_O= BJECT_get_type(obj) =3D=3D X509_LU_CRL)
- {
- sk_X509_OBJECT_delete(objs, i);
- X509_OBJECT_free(obj);
- }
- }
+ sk_X509_CRL_pop_free(ssl_ctx->crls, X509_CRL_free);
+ ssl_ctx->crls = =3D NULL;

X509_STORE_set_flags(store= , X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);

@@ -1357,7 +1365,13 @@
if (in =3D=3D NULL= )
{
msg(M_WARN, "CRL: ca= nnot read: %s", print_key_filename(crl_file, crl_inline));
<= span style=3D"color: hsl(0, 100%, 40%);">- goto end;

+ return;

+ }
+
+ if (crls =3D=3D NULL)
+ {
+ crypto_msg(M_FATAL, "CRL: cannot create CRL list&q= uot;);
}

int nu= m_crls_loaded =3D 0;
@@ -1383,18 +1397,14 @@
break;
}
- if (!X509_STORE_add_cr= l(store, crl))
+ = if (!sk_X509_CRL_push(crls, crl))
{
<= span style=3D"color: hsl(0, 100%, 40%);">- X509_CRL_free(crl);
- crypto_msg(= M_WARN, "CRL: cannot add %s to store",
- print_key_filename(crl_fi= le, crl_inline));
- = break;
+ = crypto_msg(M_FATAL, "CRL: cannot add CRL to list");=
}
- = X509_CRL_free(crl);
num_crls_loaded++;

}
msg(M_INFO, "CRL: loaded %d CR= Ls from file %s", num_crls_loaded, crl_file);
-end:
+ ssl_ctx->crls =3D crls;
BIO_free(= in);
}

diff --git a/src/= openvpn/ssl_openssl.h b/src/openvpn/ssl_openssl.h
index 392= 6e91..373dc10 100644
--- a/src/openvpn/ssl_openssl.h=
+++ b/src/openvpn/ssl_openssl.h
@@ -41,6 +41,7 @@=
SSL_CTX *ctx;
time_t crl_last_mt= ime;
off_t crl_last_size;
+ STACK_OF(X509_CRL) *crls;
}= ;

struct key_state_ssl
d= iff --git a/src/openvpn/ssl_verify_openssl.c b/src/openvpn/ssl_verify_opens= sl.c
index 7c0cbb3..20188f4 100644
--- a/sr= c/openvpn/ssl_verify_openssl.c
+++ b/src/openvpn/ssl_verify= _openssl.c
@@ -783,23 +783,7 @@
re= turn false;
}

- X509_STORE *store =3D SSL_CTX_get_cert_= store(opt->ssl_ctx->ctx);
- if (!store)
- {
- cryp= to_msg(M_FATAL, "Cannot get certificate store");
- }
-
-= STACK_OF(X509_OBJECT) *objs =3D X509_STORE_get0_objects(store);<= br>- for (int i =3D 0; i < = sk_X509_OBJECT_num(objs); i++)
- {
- = X509_OBJECT *obj =3D sk_X509_OBJECT_value(objs, i);
- ASSERT(obj);
- if (X509_OBJECT_get_type(obj) =3D= =3D X509_LU_CRL)
- = {
- retur= n false;
- }
- }
- return true;
+ return opt->ssl_ctx->crls =3D=3D N= ULL || sk_X509_CRL_num(opt->ssl_ctx->crls) =3D=3D 0;
= }

#endif /* defined(ENABLE_CRYPTO_OPENS= SL) */

To view, visit change 1289. To unsub= scribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I31ac2a763209114267c35c4a918= 2a12d8d82f6fe
Gerrit-Change-Number: 1289
Gerrit-PatchSet: 10
Gerrit-Owner: davidben <davidben@...410...&= gt;
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: davidben <davidben@...2975...= ...>
= --VIWutFsRGXk=--