From: "plaisthos (Code Review)" Date: Tue, 2 Jan 2024 13:46:40 +0000 References: Reply-To: arne-openvpn@...1227..., 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="NcU9hHR7P8c="; 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 --NcU9hHR7P8c= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld. Hello flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/487?usp=3Demail to review the following change. 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 src/openvpn/ssl_verify.c 1 file changed, 31 insertions(+), 37 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/87/487/1 diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index 35d3377..ff1a932 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,53 +460,51 @@ } /** + * 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, - int cert_depth, const char *pem_export_fname) +verify_cert_cert_export_env(const struct tls_options *opt, + openvpn_x509_cert_t *peer_cert, int cert_depth) { - char envname[64]; - /* Make copy of the filename to manage that copy by the gc_arena */ - char *pem_export_filename =3D strdup(pem_export_fname); - - if (!pem_export_filename) - { - return false; - } + 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]; /* export the path to the certificate in pem file format */ - openvpn_snprintf(envname, sizeof(envname), "peer_cert_%d", cert_depth); - setenv_str(es, envname, pem_export_filename); + 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); /* compatibility with older scripts/plugins that expect peer_cert with= out * suffix */ if (cert_depth =3D=3D 0) { - setenv_str(es, "peer_cert", pem_export_filename); + setenv_str(opt->es, "peer_cert", pem_export_filename); } return backend_x509_write_pem(peer_cert, pem_export_filename) =3D=3D S= UCCESS; } -static void -verify_cert_cert_delete_env(struct env_set *es, int cert_depth, - const char *pem_export_fname) -{ - char envname[64]; - openvpn_snprintf(envname, sizeof(envname), "peer_cert_%d", cert_depth); - env_set_del(es, envname); - - /* compatibility with older scripts/plugins that expect peer_cert with= out - * suffix */ - if (cert_depth =3D=3D 0) - { - env_set_del(es, "peer_cert"); - } - unlink(pem_export_fname); -} - /* * call --tls-verify plug-in(s) */ @@ -625,7 +624,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); @@ -758,12 +756,9 @@ 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, cert_depth, - pem_export_fname)) + + 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); @@ -821,7 +816,6 @@ ret =3D SUCCESS; cleanup: - verify_cert_cert_delete_env(opt->es, cert_depth, pem_export_fname); 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: 1 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --NcU9hHR7P8c= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld.

plaisthos would like flichtenheld to review this change.

Vi= ew Change

Keep exported certificate files for following calls

Si= nce 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 e= nv variables.

Change-Id: Ic494d43c835220ae71f10e3afbe53db918887370Signed-off-by: Arne Schwabe <arne@...1227...>
---
M src/openv= pn/ssl_verify.c
1 file changed, 31 insertions(+), 37 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/87/487/1
diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c
index 35d3377..ff1a932 100644
--- a/src/openvp= n/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_verif= y_openssl.h"
@@ -459,53 +460,51 @@
}<= /span>

/**
+ * Unlinks a file specified by in the env item that has = the form
+ * key=3Dfi= lename.
+ */+static void
+unlink_file_env(struct env_item *env= )
+{
+ /* values in env are always x=3D= y */
+ 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 e= nvironment and adds
* the filname
*/
static bool
-verify_cert_cert_export_env(struct env_set *es, openvpn_x509_cert_t *p= eer_cert,
- = int cert_depth, const char *pem_export_fname)
+verify_cert_cert_export_env(const= struct tls_options *opt,

+ openvpn_x509_cert_t *peer_cert, int cert_= depth)
{
- char envname[64];
- /* Make copy of the filename to manage that copy by the gc_arena */=
- char *pem_export_= filename =3D strdup(pem_export_fname);
-
- = if (!pem_export_filename)
- {
- retu= rn false;
- }=
+ struct gc_arena gc =3D= gc_new();
+ const= char *pem_export_filename =3D platform_create_temp_file(opt->export_pee= r_cert_dir,
+ = "pef", &a= mp;gc);
+ char env= str[128];

/* export the path to the = certificate in pem file format */
- openvpn_snprintf(envname, sizeof(envname), "peer_cert_= %d", cert_depth);
= - setenv_str(es, envname, pem_export_filename);
+ openvpn_snprintf(envstr, sizeof(envstr),= "peer_cert_%d=3D%s", cert_depth,
+ pem_export_filename);+ setenv_str(opt->es, e= nvstr, pem_export_filename);
+ env_set_add_specialfree(opt->es, envstr, &unlink_file_e= nv);

/* compatibility with older scr= ipts/plugins that expect peer_cert without
* suffix *= /
if (cert_depth =3D=3D 0)
{
- setenv_str(es, &qu= ot;peer_cert", pem_export_filename);
+ setenv_str(opt->es, "peer_cert",= pem_export_filename);
}

<= span> return backend_x509_write_pem(peer_cert, pem_export_filename) =3D= =3D SUCCESS;

}

-static void
-verify_cert_cert_delete_env(struct env_set *es, int cert_= depth,
- = const char *pem_export_fname)
-{
= - char envname[64];
= - openvpn_snprintf(envname, sizeof(envname), "peer_cert_%d", c= ert_depth);
- env_se= t_del(es, envname);
-
- /* compatibility wi= th older scripts/plugins that expect peer_cert without
- * suffix */
- if (cert_depth =3D=3D 0)
- {
- env_set_del(es, "peer_cert");- }
- unlink(pem_export_fname);
-}
-
/*
* call --tls-verify= plug-in(s)
*/
@@ -625,7 +624,6 @@=
* them defined */
result_t ret =3D FAI= LURE;
struct gc_arena gc =3D gc_new();
- const char *pem_export_fname =3D = NULL;


const struct tls_options *opt = =3D session->opt;
ASSERT(opt);
@@ -= 758,12 +756,9 @@

if (opt->export_= peer_cert_dir)
{
- pem_export_fname =3D platform_create_temp_file(opt-= >export_peer_cert_dir,
- "pef", &= amp;gc);

- if (!pem_export_fname
- || !verify_cert_cert_export_env(opt->es, cert, = cert_depth,
- = pem_export_fname))
+
+ if (!verify_cert_cert_export_env(opt, cert, cert_dep= th))
{
msg(D_TLS_ERRO= RS, "TLS Error: Failed to export certificate for "
"--tls-export-cert in %s", opt->export_peer= _cert_dir);

@@ -821,7 +816,6 @@
ret = =3D SUCCESS;

cleanup:
- verify_cert_cert_delete_env(opt->= es, cert_depth, pem_export_fname);
if (ret !=3D SUCCES= S)
{
tls_clear_error(); /* al= ways? */

To view, visit change 487. To unsub= scribe, 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: 1
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: newchange
--NcU9hHR7P8c=--