From: "cron2 (Code Review)" Date: Fri, 17 Apr 2026 10:21:28 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., davidben@...410..., openvpn-devel@lists.sourceforge.net, frank@...2641..., max@...2606... Message-ID: <250b31b135eb0f7167171361fbbb6f8f2969a6cc-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="MkTZ4kBitN8="; 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 Cc: MaxF , plaisthos , flichtenheld , openvpn-devel --MkTZ4kBitN8= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/12= 89?usp=3Demail ) 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 Github: see also openssl/openssl#28599 Signed-off-by: David Benjamin Change-Id: I31ac2a763209114267c35c4a9182a12d8d82f6fe Signed-off-by: Arne Schwabe Acked-by: Arne Schwabe Acked-by: MaxF Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1289 Message-Id: <20260416174142.28918-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg36= 641.html Signed-off-by: Gert Doering --- 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(-) 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 fd05f43..efe5b5b 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 115b8d2..a30099d 100644 --- a/src/openvpn/ssl_verify_openssl.c +++ b/src/openvpn/ssl_verify_openssl.c @@ -808,23 +808,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: merged Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I31ac2a763209114267c35c4a9182a12d8d82f6fe Gerrit-Change-Number: 1289 Gerrit-PatchSet: 13 Gerrit-Owner: davidben Gerrit-Reviewer: MaxF Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --MkTZ4kBitN8= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 sub= mitted this change.

View Change

ssl_openssl: Fix some CRL mixups

There ar= e 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_ST= ORE_CTX.

OpenVPN currently does the former. However, it also support= s 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
X= 509_STORE. OpenVPN currently does it by grabbing the
STACK_OF(X509_OBJEC= T) out of the X509_STORE and manually deleting all
the CRLs from it.
=
This mutates an OpenSSL internal object which bumps into problems ifOpenSSL ever switches to a more efficient representation. See
https://g= ithub.com/openssl/openssl/pull/28599

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

Additionally, this seems to cause two OpenVPN featu= res 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 pi= ck up CRLs
and backend_tls_ctx_reload_crl actually ends up clobbering so= me state
X509_LOOKUP_hash_dir internally maintains on the X509_STORE. Li= kewise,
tls_verify_crl_missing can get confused between
backend_tls_c= tx_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 CR= Ls
now go onto a STACK_OF(X509_CRL) tracked on the tls_root_ctx. Now thi= s
field can be freely reloaded by OpenVPN without reconfiguring OpenSSL.=
Instead, pass the current value into OpenSSL at verification time. To<= br>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_S= TORE_CTX configuration before starting certificate verification.

Con= text: SSL_CTX_set_cert_verify_callback and the existing
verify_callback = are not the same. SSL_CTX_set_cert_verify_callback wraps
the verificatio= n while verify_callback is called multiple times
throughout verification= . It's too late to reconfigure X509_STORE_CTX in
verify_callback. ve= rify_callback is usually not what you want.
Sometimes current_cert and e= rror_depth don't quite line up, and
cert_hash_remember may end up ca= lled multiple times for a single
certificate.

I suspect some of t= he other verify_callback logic would also be better
done in the new call= back, but I've left it alone to keep this change
minimal. verify_cal= lback is really only usable for suppressing errors.
Application bookkeep= ing is better down elsewhere.

Add .clang-format section for STACK_OF= since we otherwise format the
line as STACK_OF(X509_CRL) * crls

= Github: see also openssl/openssl#28599
Signed-off-by: David Benjamin <= ;davidben@...410...>
Change-Id: I31ac2a763209114267c35c4a9182a12d8d82= f6fe
Signed-off-by: Arne Schwabe <arne@...1227...>
Acked-by: Ar= ne Schwabe <arne-openvpn@...1227...>
Acked-by: MaxF <max@...2854...= 6...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1289
Mess= age-Id: <20260416174142.28918-1-gert@...1296...>
URL: https://www.= mail-archive.com/openvpn-devel@lists.sourceforge.net/msg36641.html
Signe= d-off-by: Gert Doering <gert@...1296...>
---
M .clang-formatM src/openvpn/openssl_compat.h
M src/openvpn/ssl_openssl.c
M src/ope= nvpn/ssl_openssl.h
M src/openvpn/ssl_verify_openssl.c
5 files changed= , 38 insertions(+), 55 deletions(-)

diff --git a/.clang-format b/.clang-format
index cbec0a4= ..4774d1e 100644
--- a/.clang-format
+++ b/= .clang-format
@@ -43,7 +43,10 @@
SpacesBef= oreTrailingComments: '2'
SpacesInParens: Never

TabWidth: '4'
-TypeNames: [DWORD]
+TypeNames:
+ - 'DWORD'
+ - 'STACK_OF'
+TypenameMacros: ['STACK_OF']
UseTab: Never<= /span>
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/op= enssl_compat.h
@@ -61,21 +61,6 @@
<= br> /* Functionality missing in LibreSSL before 3.5 */
#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER = < 0x3050000fL
-/**
- * Destroy a X509 objec= t
- *
- * @param obj X509 objec= t
- */
-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<= /span>
diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_op= enssl.c
index fd05f43..efe5b5b 100644
--- a= /src/openvpn/ssl_openssl.c
+++ b/src/openvpn/ssl_openssl.c<= /span>
@@ -141,6 +141,8 @@
ASSERT(NULL !=3D c= tx);
SSL_CTX_free(ctx->ctx);
c= tx->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 *a= rg)
+{
+ struct tls_session *session;
+ SSL *ssl;<= br>+
+ ssl =3D X509_STORE_CTX_get_ex_data(ctx, SS= L_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-&= gt;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_file, bool crl_inline)
{
BIO *in =3D NULL;
+ STACK_OF(X509_CRL) *crls =3D NULL;
<= span>
X509_STORE *store =3D SSL_CTX_get_cert_store(ss= l_ctx->ctx);
if (!store)
@@ -1328,2= 0 +1348,8 @@
crypto_msg(M_FATAL, "Cannot get = certificate store");
}
- /* Always start with a cle= ared CRL list, for that we
- * we need to manually find the CRL object from the stack- * and remove it */=
- STACK_OF(X509_OBJECT) *o= bjs =3D X509_STORE_get0_objects(store);
- for (int i =3D 0; i < sk_X509_OBJECT_num(objs); i+= +)
- {
- X509_OBJECT *obj =3D sk_X50= 9_OBJECT_value(objs, i);
- if (X509_OBJECT_get_type(obj) =3D=3D X509_LU_CRL)
- {
- sk_X509_OBJECT_delete(objs, i);

- X509_OBJECT_fre= e(obj);
- }

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

X509_S= TORE_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: cannot read: %s", print_key_filename(crl_file, crl_i= nline));
- 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;
@@ -1383,18 +139= 7,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 115b8d2..a30099d 100644
--- a/src/openvpn/ssl_verify_openssl.c
+++ b/= src/openvpn/ssl_verify_openssl.c
@@ -808,23 +808,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: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I31ac2a763209114267c35c4a918= 2a12d8d82f6fe
Gerrit-Change-Number: 1289
Gerrit-PatchSet: 13
Gerrit-Owner: davidben <davidben@...410...&= gt;
Gerrit-Reviewer: MaxF <max@...2606...> <= /div>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --MkTZ4kBitN8=--