From: "plaisthos (Code Review)" Date: Fri, 12 Jan 2024 16:56:18 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <7768395139ca8a42c59c80512d6ab1ad766ef269-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="notIj/vIzzw="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Keep exported certificate files for following calls List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel --notIj/vIzzw= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld. Hello flichtenheld,=20 I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/487?usp=3Demail to look at the new patch set (#2). Change subject: Keep exported certificate files for following calls ...................................................................... Keep exported certificate files for following calls Since the lifetime of environment variables is quite different, we need to tie the lifetime of these files to their environment variables which in turn requires a special function to be called on the removal of these env variables. Change-Id: Ic494d43c835220ae71f10e3afbe53db918887370 Signed-off-by: Arne Schwabe --- M doc/man-sections/script-options.rst M src/openvpn/ssl_verify.c 2 files changed, 44 insertions(+), 19 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/87/487/2 diff --git a/doc/man-sections/script-options.rst b/doc/man-sections/script-= options.rst index e05100a..32943d2 100644 --- a/doc/man-sections/script-options.rst +++ b/doc/man-sections/script-options.rst @@ -424,7 +424,8 @@ parameters passed as environmental variables. --tls-export-cert dir - Adds an environment variable ``peer_cert`` when calling the + Adds an environment variable ``peer_cert_{x}`` (and an alias + ``peer_cert`` for the current certificate depth) when calling the ``--tls-verify`` script or executing the OPENVPN_PLUGIN_TLS_VERIFY plugin hook to verify the certificate. =20 @@ -777,6 +778,11 @@ the path to the current peer certificate to be verified in PEM format. See also the argument certificate_depth to the ``--tls-verify`` comman= d. +:code:`peer_cert_{n}` + If the option ``--tls-export-cert`` is enabled, this option contains + the path to the current peer certificate to be verified in PEM format + where ``n`` is the verification level. + :code:`proto` The ``--proto`` parameter. Set on program initiation and reset on SIGHUP. diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index 55b3cf0..de0d59c 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -39,6 +39,7 @@ #include "run_command.h" #include "ssl_verify.h" #include "ssl_verify_backend.h" +#include "platform.h" #ifdef ENABLE_CRYPTO_OPENSSL #include "ssl_verify_openssl.h" @@ -459,24 +460,45 @@ } =20 /** + * Unlinks a file specified by in the env item that has the form + * key=3Dfilename. + */ +static void +unlink_file_env(struct env_item *env) +{ + /* values in env are always x=3Dy */ + const char *filename =3D strchr(env->string, '=3D'); + ASSERT(filename); + + /* Move just past the =3D */ + filename +=3D 1; + + platform_unlink((const char *) filename); +} + +/** * Exports the certificate in \c peer_cert into the environment and adds * the filname */ static bool -verify_cert_cert_export_env(struct env_set *es, openvpn_x509_cert_t *peer_= cert, - const char *pem_export_fname) +verify_cert_cert_export_env(const struct tls_options *opt, + openvpn_x509_cert_t *peer_cert, int cert_depth) { - /* export the path to the current certificate in pem file format */ - setenv_str(es, "peer_cert", pem_export_fname); + struct gc_arena gc =3D gc_new(); + const char *pem_export_filename =3D platform_create_temp_file(opt->exp= ort_peer_cert_dir, + "pef", &gc= ); + char envstr[128]; - return backend_x509_write_pem(peer_cert, pem_export_fname) =3D=3D SUCC= ESS; -} + /* export the path to the certificate in pem file format */ + openvpn_snprintf(envstr, sizeof(envstr), "peer_cert_%d=3D%s", cert_dep= th, + pem_export_filename); + setenv_str(opt->es, envstr, pem_export_filename); + env_set_add_specialfree(opt->es, envstr, &unlink_file_env); -static void -verify_cert_cert_delete_env(struct env_set *es, const char *pem_export_fna= me) -{ - env_set_del(es, "peer_cert"); - unlink(pem_export_fname); + /* compatibility with older scripts/plugins that expect peer_cert with= out + * suffix */ + setenv_str(opt->es, "peer_cert", pem_export_filename); + return backend_x509_write_pem(peer_cert, pem_export_filename) =3D=3D S= UCCESS; } /* @@ -598,7 +620,6 @@ * them defined */ result_t ret =3D FAILURE; struct gc_arena gc =3D gc_new(); - const char *pem_export_fname =3D NULL; const struct tls_options *opt =3D session->opt; ASSERT(opt); @@ -731,11 +752,7 @@ if (opt->export_peer_cert_dir) { - pem_export_fname =3D platform_create_temp_file(opt->export_peer_ce= rt_dir, - "pef", &gc); - - if (!pem_export_fname - || !verify_cert_cert_export_env(opt->es, cert, pem_export_fnam= e)) + if (!verify_cert_cert_export_env(opt, cert, cert_depth)) { msg(D_TLS_ERRORS, "TLS Error: Failed to export certificate for= " "--tls-export-cert in %s", opt->export_peer_cert_dir); @@ -793,7 +810,9 @@ ret =3D SUCCESS; cleanup: - verify_cert_cert_delete_env(opt->es, pem_export_fname); + /* delete the variable for the current depth if present as it does not= make + * sense going forward in other calls of other scripts */ + env_set_del(opt->es, "peer_cert"); if (ret !=3D SUCCESS) { tls_clear_error(); /* always? */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/487?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: Ic494d43c835220ae71f10e3afbe53db918887370 Gerrit-Change-Number: 487 Gerrit-PatchSet: 2 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --notIj/vIzzw= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld.

plaisthos uploaded patch set #2 to this change.

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

Keep exported certificate files for following calls

Since the = lifetime of environment variables is quite different, we
need to tie the= lifetime of these files to their environment variables
which in turn re= quires a special function to be called on the removal
of these env varia= bles.

Change-Id: Ic494d43c835220ae71f10e3afbe53db918887370
Signed= -off-by: Arne Schwabe <arne@...1227...>
---
M doc/man-sections/= script-options.rst
M src/openvpn/ssl_verify.c
2 files changed, 44 ins= ertions(+), 19 deletions(-)

git pull ssh://gerrit.openvpn.net:294=
18/openvpn refs/changes/87/487/2
diff --git a/doc/man-sections/scri=
pt-options.rst b/doc/man-sections/script-options.rst
index = e05100a..32943d2 100644
--- a/doc/man-sections/script-optio= ns.rst
+++ b/doc/man-sections/script-options.rst
= @@ -424,7 +424,8 @@
parameters passed as environme= ntal variables.

--tls-export-cert dir
- Adds an environment va= riable ``peer_cert`` when calling the
+ Adds an environment variable ``peer_cert_{x}`` (and an = alias
+ ``peer_cert`= ` for the current certificate depth) when calling the
``= --tls-verify`` script or executing the OPENVPN_PLUGIN_TLS_VERIFY plugin
hook to verify the certificate.
@@ -777,6 +778,11 @@
the path to the current pe= er certificate to be verified in PEM format.
See also = the argument certificate_depth to the ``--tls-verify`` command.
<= span>

+:code:`peer_c= ert_{n}`
+ If the = option ``--tls-export-cert`` is enabled, this option contains
+ the path to the current peer = certificate to be verified in PEM format

+ where ``n`` is the verification level.
<= span style=3D"color: hsl(120, 100%, 40%);">+
:code:`proto`=
The ``--proto`` parameter. Set on program initiation = and reset on
SIGHUP.
diff --git a/src/= openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c
index 55b3c= f0..de0d59c 100644
--- a/src/openvpn/ssl_verify.c+++ b/src/openvpn/ssl_verify.c
@@ -39,6 +39,7 @@
#include "run_command.h"
#include = "ssl_verify.h"
#include "ssl_verify_backend= .h"
+#include &q= uot;platform.h"

#ifdef ENABLE_CRYPT= O_OPENSSL
#include "ssl_verify_openssl.h"=
@@ -459,24 +460,45 @@
}
=
/**
+ * Un= links a file specified by in the env item that has the form
+ * key=3Dfilename.

+ */
+static void
+unlink_file_env(struct env_item *env)
+{
+ /* values in env are always x=3Dy */
+ const char *filename =3D strchr(en= v->string, '=3D');
+ ASSERT(filename);
+
+ /* = Move just past the =3D */
+ filename +=3D 1;
+
+ platform= _unlink((const char *) filename);
+}
+
+/**
*= Exports the certificate in \c peer_cert into the environment and adds
* the filname
*/
static bo= ol
-verify_cert_cert_ex= port_env(struct env_set *es, openvpn_x509_cert_t *peer_cert,
- const ch= ar *pem_export_fname)

+verify_cert_cert_export_env(const struct tls_options *opt,
+ openvp= n_x509_cert_t *peer_cert, int cert_depth)

{
- /* export the path to the curren= t certificate in pem file format */
- setenv_str(es, "peer_cert", pem_export_fname);<= /span>
+ struct gc_arena = gc =3D gc_new();
+ = const char *pem_export_filename =3D platform_create_temp_file(opt->expo= rt_peer_cert_dir,
+ = "pef&quo= t;, &gc);
+ ch= ar envstr[128];

- return backend_x509_write_pem(peer_cert, pem_export_fname) = =3D=3D SUCCESS;
-}

+ /* export the path t= o the certificate in pem file format */
+ openvpn_snprintf(envstr, sizeof(envstr), "peer= _cert_%d=3D%s", cert_depth,
+ pem_export_filename);
+ setenv_str(opt->es, envstr, pem_= export_filename);
+ = env_set_add_specialfree(opt->es, envstr, &unlink_file_env);=

-static voi= d
-verify_cert_cert_del= ete_env(struct env_set *es, const char *pem_export_fname)
-{
- env_set_del(es, "peer_cert");
- unlink(pem_export_fname);<= br>+ /* compatibility with o= lder scripts/plugins that expect peer_cert without
+ * suffix */
+ setenv_str(opt->es, "peer_cert"= ;, pem_export_filename);
+ return backend_x509_write_pem(peer_cert, pem_export_filename) =3D= =3D SUCCESS;
}

/*

@@ -598,7 +620,6 @@
* them defined */
result_t ret =3D FAILURE;
struct gc_a= rena gc =3D gc_new();
-= const char *pem_export_fname =3D NULL;

const struct tls_options *opt =3D session->opt;

= ASSERT(opt);
@@ -731,11 +752,7 @@

if (opt->export_peer_cert_dir)
{<= /span>
- pem_export_fna= me =3D platform_create_temp_file(opt->export_peer_cert_dir,
- = "pef", &gc);

-
- || !verify_cert_cert_export_env(opt->es, cert, pe= m_export_fname))
+ = if (!verify_cert_cert_export_env(opt, cert, cert_depth))
{

msg(D_TLS_ERRORS, "TLS Erro= r: Failed to export certificate for "
= "--tls-export-cert in %s", opt->export_peer_cert_dir);=
@@ -793,7 +810,9 @@
ret =3D SUCCESS;<= br>
cleanup:
- verify_cert_cert_delete_env(opt->es, pem_export_fname= );
+ /* delete the= variable for the current depth if present as it does not make
+ * sense going forward in ot= her calls of other scripts */
+ env_set_del(opt->es, "peer_cert");
if (ret !=3D SUCCESS)
{
= tls_clear_error(); /* always? */

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ic494d43c835220ae71f10e3afbe= 53db918887370
Gerrit-Change-Number: 487
Gerrit-PatchSet: 2
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: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--notIj/vIzzw=--