From: "plaisthos (Code Review)" Date: Wed, 1 Apr 2026 11:56:52 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., davidben@...410..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <5119223f08f9c12f4ef7f71ab53fa3544c6b83e7-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="NKJXHzJIJQc="; 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 --NKJXHzJIJQc= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: davidben, flichtenheld. plaisthos has uploaded a new patch set (#11) to the change originally creat= ed by davidben. ( http://gerrit.openvpn.net/c/openvpn/+/1289?usp=3Demail ) The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied= now. 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/11 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 fca1d73..3c33cc7 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); @@ -1374,6 +1393,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) @@ -1381,20 +1401,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); @@ -1410,7 +1418,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; @@ -1436,18 +1450,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 a15640d..3c57f7f 100644 --- a/src/openvpn/ssl_verify_openssl.c +++ b/src/openvpn/ssl_verify_openssl.c @@ -785,23 +785,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: 11 Gerrit-Owner: davidben Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-Attention: davidben --NKJXHzJIJQc= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

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

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

View Change

The change is no longer submitt= able: checks~ChecksSubmitRule is unsatisfied now.

ssl_open=
ssl: 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 l= oaded per verification at
the X509_STORE_CTX.

OpenVPN currently d= oes the former. However, it also supports CRL
reloading, and tries to re= load the CRL file before each connection.
OpenSSL does not really have a= good way to unload objects from an
X509_STORE. OpenVPN currently does i= t by grabbing the
STACK_OF(X509_OBJECT) out of the X509_STORE and manual= ly deleting all
the CRLs from it.

This mutates an OpenSSL interna= l object which bumps into problems if
OpenSSL ever switches to a more ef= ficient representation. See
https://github.com/openssl/openssl/pull/2859= 9

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

Additionall= y, this seems to cause two OpenVPN features to not work
together. I gath= er backend_tls_ctx_reload_crl is trying to clear the
CRLs loaded from la= st time it ran. But tls_ctx_load_ca with a ca_file
can also load CRLs. t= ls_ctx_load_ca with ca_path will also pick up CRLs
and backend_tls_ctx_r= eload_crl actually ends up clobbering some state
X509_LOOKUP_hash_dir in= ternally maintains on the X509_STORE. Likewise,
tls_verify_crl_missing c= an 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 th= e two CRLs separately. crl_file-based CRLs
now go onto a STACK_OF(X509_C= RL) tracked on the tls_root_ctx. Now this
field can be freely reloaded b= y OpenVPN without reconfiguring OpenSSL.
Instead, pass the current value= into OpenSSL at verification time. To
do so, we need to use the SSL_CT= X_set_cert_verify_callback, which allows
swapping out the X509_verify_ce= rt call, and also tweaking the
X509_STORE_CTX configuration before start= ing certificate verification.

Context: SSL_CTX_set_cert_verify_callb= ack and the existing
verify_callback are not the same. SSL_CTX_set_cert_= verify_callback wraps
the verification while verify_callback is called m= ultiple times
throughout verification. It's too late to reconfigure = X509_STORE_CTX in
verify_callback. verify_callback is usually not what y= ou want.
Sometimes current_cert and error_depth don't quite line up,= and
cert_hash_remember may end up called multiple times for a singlecertificate.

I suspect some of the other verify_callback logic woul= d 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 sup= pressing errors.
Application bookkeeping is better down elsewhere.
Add .clang-format section for STACK_OF since we otherwise format the
l= ine as STACK_OF(X509_CRL) * crls

Signed-off-by: David Benjamin <d= avidben@...410...>
Change-Id: I31ac2a763209114267c35c4a9182a12d8d82f6= fe
Signed-off-by: Arne Schwabe <arne@...1227...>
---
M .clan= g-format
M src/openvpn/openssl_compat.h
M src/openvpn/ssl_openssl.cM src/openvpn/ssl_openssl.h
M src/openvpn/ssl_verify_openssl.c
5 fi= les changed, 38 insertions(+), 55 deletions(-)

g=
it pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/89/1289/11
diff --git a/.clang-format b/.clang-format
index cbec0a= 4..4774d1e 100644
--- a/.clang-format
+++ b= /.clang-format
@@ -43,7 +43,10 @@
SpacesBe= foreTrailingComments: '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
<= span>--- a/src/openvpn/openssl_compat.h
+++ b/src/openvpn/o= penssl_compat.h
@@ -61,21 +61,6 @@
=
/* Functionality missing in LibreSSL before 3.5 */
#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER= < 0x3050000fL
-/**<= /span>
- * Destroy a X509 obje= ct
- *
- * @param obj X509 obje= ct
- */
-static inline void

-X509_OBJECT_free(X509_OBJECT *obj)
-{
- if (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_o= penssl.c
index fca1d73..3c33cc7 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;
<= span> 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;<= /span>
+ SSL *ssl;=
+
+ ssl =3D X509_STORE_CTX_get_ex_data(ctx, S= SL_get_ex_data_X509_STORE_CTX_idx());
+ ASSERT(ssl);
+ session =3D SSL_get_ex_data(ssl, mydata_index);+ ASSERT(session);<= br>+
+ /* Configure CRLs. */
+ X509_STORE_CTX_set0_crls(ctx, sessio= n->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_VERIF= Y_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);

@@ -1374,6 +1393,7 @@
backend_tls_ctx_reload_crl(struct= tls_root_ctx *ssl_ctx, 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(s= sl_ctx->ctx);
if (!store)
@@ -1381,= 20 +1401,8 @@
crypto_msg(M_FATAL, "Cannot get= certificate store");
}
<= br>- /* Always start with a cl= eared CRL list, for that we
- * we need to manually find the CRL object from the stack<= br>- * 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_X5= 09_OBJECT_value(objs, i);
- ASSERT(obj);
- {
- sk_X509_OBJECT_delete(objs, i);

- X509_OBJECT_fr= ee(obj);
- }

- }
+ sk_X509_CRL_pop_free(ssl_ctx->cr= ls, X509_CRL_free);
+= ssl_ctx->crls =3D NULL;

X509_= STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);<= /span>

@@ -1410,7 +1418,13 @@
= if (in =3D=3D NULL)
{
msg(= M_WARN, "CRL: cannot read: %s", print_key_filename(crl_file, crl_= inline));
- goto= end;
+ return= ;
+ }
+

+ crls =3D sk_X509_CRL_new_null();
+ if (crls =3D=3D NULL)+ {
+ crypto_msg(M_FATAL, "CRL: ca= nnot create CRL list");
}

int num_crls_loaded =3D 0;
@@ -1436,18 +145= 0,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",
- pri= nt_key_filename(crl_file, crl_inline));
- break;
+ crypto_msg(M_FATAL, "CRL: cannot add CRL = to list");
}
- X509_CRL_free(crl);
n= um_crls_loaded++;
}
msg(M_INFO, &= quot;CRL: loaded %d CRLs from file %s", num_crls_loaded, crl_file);
-end:
+ ssl_ctx->crls =3D crls; BIO_free(in);
}

<= span>diff --git a/src/openvpn/ssl_openssl.h b/src/openvpn/ssl_openssl.h
index 3926e91..373dc10 100644
--- a/src/openvp= n/ssl_openssl.h
+++ b/src/openvpn/ssl_openssl.h
<= span>@@ -41,6 +41,7 @@

SSL_CTX *ctx;
= time_t crl_last_mtime;
off_t crl_last_size;=
+ STACK_OF(X509_CRL) *cr= ls;
};

struct key_state= _ssl
diff --git a/src/openvpn/ssl_verify_openssl.c b/src/op= envpn/ssl_verify_openssl.c
index a15640d..3c57f7f 100644
--- a/src/openvpn/ssl_verify_openssl.c
+++ b/= src/openvpn/ssl_verify_openssl.c
@@ -785,23 +785,7 @@
return false;
}
<= /span>
- X509_STORE *store = =3D SSL_CTX_get_cert_store(opt->ssl_ctx->ctx);
- if (!store)
- {
- crypto_msg(M_FATAL, "Cannot get certificate store&qu= ot;);
- }
= -
- STACK_OF(X509_OBJECT) *objs =3D X509_STORE_get0_o= bjects(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);<= /span>
- if (X509_OBJEC= T_get_type(obj) =3D=3D X509_LU_CRL)
- {
- return false;
- }
- }<= /span>
- return true;
+ return opt->ssl_ct= x->crls =3D=3D NULL || sk_X509_CRL_num(opt->ssl_ctx->crls) =3D=3D = 0;
}

#endif /* defined(= ENABLE_CRYPTO_OPENSSL) */

To view, visi= t change= 1289. To unsubscribe, 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: 11
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: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: davidben <davidben@...2975...= ...>
= --NKJXHzJIJQc=--