From: "plaisthos (Code Review)" Date: Tue, 2 Jan 2024 16:52:02 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <98305f55c9389f8216385b3311013a99b119bc97-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="DDN9RSrryCQ="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Implement the --tls-export-cert feature List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: cron2 , flichtenheld Cc: openvpn-devel --DDN9RSrryCQ= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld. Hello cron2, flichtenheld, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/466?usp=3Demail to look at the new patch set (#9). Change subject: Implement the --tls-export-cert feature ...................................................................... Implement the --tls-export-cert feature This is a re-implementation of the --tls-export-cert feature. This was necessary to due to missing approval to re-license the old (now removed) code. The re-implementation is based on the following description of the feature provided by David: Add an option to export certificate in PEM format of the remote peer to a given directory. For example: --tls-export-cert /var/tmp This option should use a randomised filename, which is provided via a "peer_cert" environment variable for the --tls-verify script or the OPENVPN_PLUGIN_TLS_VERIFY plug-in hook. Once the script or plugin call has completed, OpenVPN should delete this file. Change-Id: Ia9b3f1813d2d0d492d17c87348b4cebd0bf19ce2 Signed-off-by: Arne Schwabe --- M doc/man-sections/script-options.rst M src/openvpn/init.c M src/openvpn/options.c M src/openvpn/options.h M src/openvpn/ssl_common.h M src/openvpn/ssl_verify.c M src/openvpn/ssl_verify_backend.h M src/openvpn/ssl_verify_mbedtls.c M src/openvpn/ssl_verify_openssl.c 9 files changed, 174 insertions(+), 5 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/66/466/9 diff --git a/doc/man-sections/script-options.rst b/doc/man-sections/script-= options.rst index 6f90e14..53c9f97 100644 --- a/doc/man-sections/script-options.rst +++ b/doc/man-sections/script-options.rst @@ -423,6 +423,15 @@ See the `Environmental Variables`_ section below for additional parameters passed as environmental variables. +--tls-export-cert dir + Adds an environment variable ``peer_cert_{x}`` (and an alias + ``peer_cert`` for ``peer_cert_0`` for compatibility) when calling the + ``--tls-verify`` script or executing the OPENVPN_PLUGIN_TLS_VERIFY plugin + hook to verify the certificate. + + The environment variable contains the path to a PEM encoded certificate + of the current peer certificate in the directory ``dir``. + --up cmd Run command ``cmd`` after successful TUN/TAP device open (pre ``--user`` UID change). @@ -763,6 +772,15 @@ modifier is specified, and deleted from the environment after the scri= pt returns. +: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:`peer_cert` + Identical to `peer_cert_0` for compatibility with older + versions. + :code:`proto` The ``--proto`` parameter. Set on program initiation and reset on SIGHUP. diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 9e2b3845..c5cc154 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -3336,6 +3336,7 @@ to.auth_user_pass_verify_script_via_file =3D options->auth_user_pass_v= erify_script_via_file; to.client_crresponse_script =3D options->client_crresponse_script; to.tmp_dir =3D options->tmp_dir; + to.export_peer_cert_dir =3D options->tls_export_peer_cert_dir; if (options->ccd_exclusive) { to.client_config_dir_exclusive =3D options->client_config_dir; diff --git a/src/openvpn/options.c b/src/openvpn/options.c index e498114..1c0a6bd 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -1986,6 +1986,7 @@ SHOW_STR(cipher_list_tls13); SHOW_STR(tls_cert_profile); SHOW_STR(tls_verify); + SHOW_STR(tls_export_peer_cert_dir); SHOW_INT(verify_x509_type); SHOW_STR(verify_x509_name); SHOW_STR_INLINE(crl_file); @@ -3048,6 +3049,7 @@ MUST_BE_UNDEF(cipher_list_tls13); MUST_BE_UNDEF(tls_cert_profile); MUST_BE_UNDEF(tls_verify); + MUST_BE_UNDEF(tls_export_peer_cert_path); MUST_BE_UNDEF(verify_x509_name); MUST_BE_UNDEF(tls_timeout); MUST_BE_UNDEF(renegotiate_bytes); @@ -4053,6 +4055,13 @@ R_OK, "--crl-verify"); } + if (options->tls_export_peer_cert_dir) + { + errs |=3D check_file_access_chroot(options->chroot_dir, CHKACC_FIL= E, + options->tls_export_peer_cert_dir, + W_OK, "--tls-export-cert"); + } + ASSERT(options->connection_list); for (int i =3D 0; i < options->connection_list->len; ++i) { @@ -8997,6 +9006,11 @@ string_substitute(p[1], ',', ' ', &options->gc), "tls-verify", true); } + else if (streq(p[0], "tls-export-cert") && p[1] && !p[2]) + { + VERIFY_PERMISSION(OPT_P_SCRIPT); + options->tls_export_peer_cert_dir =3D p[1]; + } else if (streq(p[0], "compat-names")) { VERIFY_PERMISSION(OPT_P_GENERAL); diff --git a/src/openvpn/options.h b/src/openvpn/options.h index c4514e1..e52a953 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -592,6 +592,7 @@ const char *tls_cert_profile; const char *ecdh_curve; const char *tls_verify; + const char *tls_export_peer_cert_dir; int verify_x509_type; const char *verify_x509_name; const char *crl_file; diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 925660b..f085e0d 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -374,6 +374,7 @@ const char *client_crresponse_script; bool auth_user_pass_verify_script_via_file; const char *tmp_dir; + const char *export_peer_cert_dir; const char *auth_user_pass_file; bool auth_user_pass_file_inline; diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index bd7e512..35d3377 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -31,6 +31,7 @@ #endif #include "syshead.h" +#include #include "base64.h" #include "manage.h" @@ -457,6 +458,54 @@ gc_free(&gc); } +/** + * 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) +{ + 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; + } + + /* 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); + + /* 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); + } + + 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) */ @@ -572,18 +621,19 @@ result_t verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int ce= rt_depth) { + /* need to define these variables here so goto cleanup will always have + * them defined */ result_t ret =3D FAILURE; - char *subject =3D NULL; - const struct tls_options *opt; struct gc_arena gc =3D gc_new(); + const char *pem_export_fname =3D NULL; - opt =3D session->opt; + const struct tls_options *opt =3D session->opt; ASSERT(opt); session->verified =3D false; /* get the X509 name */ - subject =3D x509_get_subject(cert, &gc); + char *subject =3D x509_get_subject(cert, &gc); if (!subject) { msg(D_TLS_ERRORS, "VERIFY ERROR: depth=3D%d, could not extract X50= 9 " @@ -706,6 +756,20 @@ session->verify_maxlevel =3D max_int(session->verify_maxlevel, cert_de= pth); + 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)) + { + msg(D_TLS_ERRORS, "TLS Error: Failed to export certificate for= " + "--tls-export-cert in %s", opt->export_peer_cert_dir); + goto cleanup; + } + } /* export certificate values to the environment */ verify_cert_set_env(opt->es, cert, cert_depth, subject, common_name, opt->x509_track); @@ -757,12 +821,13 @@ ret =3D SUCCESS; cleanup: - + verify_cert_cert_delete_env(opt->es, cert_depth, pem_export_fname); if (ret !=3D SUCCESS) { tls_clear_error(); /* always? */ session->verified =3D false; /* double sure? */ } + gc_free(&gc); return ret; diff --git a/src/openvpn/ssl_verify_backend.h b/src/openvpn/ssl_verify_back= end.h index d402b1f..5301a51 100644 --- a/src/openvpn/ssl_verify_backend.h +++ b/src/openvpn/ssl_verify_backend.h @@ -161,6 +161,17 @@ struct gc_arena *gc); /* + * Write the certificate to the file in PEM format. + * + * + * @param cert Certificate to serialise. + * + * @return \c FAILURE, \c or SUCCESS + */ +result_t backend_x509_write_pem(openvpn_x509_cert_t *cert, + const char *filename); + +/* * Save X509 fields to environment, using the naming convention: * * X509_{cert_depth}_{name}=3D{value} diff --git a/src/openvpn/ssl_verify_mbedtls.c b/src/openvpn/ssl_verify_mbed= tls.c index 5612139..24a89c3 100644 --- a/src/openvpn/ssl_verify_mbedtls.c +++ b/src/openvpn/ssl_verify_mbedtls.c @@ -218,6 +218,41 @@ return buf; } +result_t +backend_x509_write_pem(openvpn_x509_cert_t *cert, const char *filename) +{ + /* mbed TLS does not make it easy to write a certificate in PEM format. + * The only way is to directly access the DER encoded raw certificate + * and PEM encode it ourselves */ + + struct gc_arena gc =3D gc_new(); + /* just do a very loose upper bound for the base64 based PEM encoding + * using 3 times the space for the base64 and 100 bytes for the + * headers and footer */ + struct buffer pem =3D alloc_buf_gc(cert->raw.len * 3 + 100, &gc); + + struct buffer der =3D {}; + buf_set_read(&der, cert->raw.p, cert->raw.len); + + if (!crypto_pem_encode("CERTIFICATE", &pem, &der, &gc)) + { + goto err; + } + + if (!buffer_write_file(filename, &pem)) + { + goto err; + } + + gc_free(&gc); + return SUCCESS; +err: + msg(D_TLS_DEBUG_LOW, "Error writing X509 certificate to file %s", + filename); + gc_free(&gc); + return FAILURE; +} + static struct buffer x509_get_fingerprint(const mbedtls_md_info_t *md_info, mbedtls_x509_crt *c= ert, struct gc_arena *gc) diff --git a/src/openvpn/ssl_verify_openssl.c b/src/openvpn/ssl_verify_open= ssl.c index 5afffc1..00fdec3 100644 --- a/src/openvpn/ssl_verify_openssl.c +++ b/src/openvpn/ssl_verify_openssl.c @@ -320,6 +320,29 @@ return format_hex_ex(asn1_i->data, asn1_i->length, 0, 1, ":", gc); } +result_t +backend_x509_write_pem(openvpn_x509_cert_t *cert, const char *filename) +{ + BIO *out =3D BIO_new_file(filename, "w"); + if (!out) + { + goto err; + } + + if (!PEM_write_bio_X509(out, cert)) + { + goto err; + } + BIO_free(out); + + return SUCCESS; +err: + BIO_free(out); + crypto_msg(D_TLS_DEBUG_LOW, "Error writing X509 certificate to file %s= ", + filename); + return FAILURE; +} + struct buffer x509_get_sha1_fingerprint(X509 *cert, struct gc_arena *gc) { -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/466?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: Ia9b3f1813d2d0d492d17c87348b4cebd0bf19ce2 Gerrit-Change-Number: 466 Gerrit-PatchSet: 9 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --DDN9RSrryCQ= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld.

plaisthos uploaded patch set #9 to this change.

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

Implement the --tls-export-cert feature

This is a re-implement= ation of the --tls-export-cert feature. This
was necessary to due to mis= sing approval to re-license the old
(now removed) code. The re-implement= ation is based on the following
description of the feature provided by D= avid:

Add an option to export certificate in PEM format of the rem= ote
peer to a given directory.

For example: --tls-export-cert= /var/tmp

This option should use a randomised filename, which is p= rovided via a
"peer_cert" environment variable for the --tls= -verify script or the
OPENVPN_PLUGIN_TLS_VERIFY plug-in hook.

O= nce the script or plugin call has completed, OpenVPN should delete
this = file.

Change-Id: Ia9b3f1813d2d0d492d17c87348b4cebd0bf19ce2
Signed= -off-by: Arne Schwabe <arne@...1227...>
---
M doc/man-sections/= script-options.rst
M src/openvpn/init.c
M src/openvpn/options.c
M = src/openvpn/options.h
M src/openvpn/ssl_common.h
M src/openvpn/ssl_ve= rify.c
M src/openvpn/ssl_verify_backend.h
M src/openvpn/ssl_verify_mb= edtls.c
M src/openvpn/ssl_verify_openssl.c
9 files changed, 174 inser= tions(+), 5 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/=
openvpn refs/changes/66/466/9
diff --git a/doc/man-sections/script-=
options.rst b/doc/man-sections/script-options.rst
index 6f9= 0e14..53c9f97 100644
--- a/doc/man-sections/script-options.= rst
+++ b/doc/man-sections/script-options.rst
@@ -423,6 +423,15 @@
See the `Environmental Variables= `_ section below for additional
parameters passed as env= ironmental variables.

+--tls-export-cert dir
+ Adds an environment variable ``peer_cert_{x}`` (and= an alias
+ ``peer_c= ert`` for ``peer_cert_0`` for compatibility) when calling the
+ ``--tls-verify`` script or exe= cuting the OPENVPN_PLUGIN_TLS_VERIFY plugin
+ hook to verify the certificate.
+
+ The environment variable contains the path to a PEM en= coded certificate
+ = of the current peer certificate in the directory ``dir``.
+
--up cmd
= Run command ``cmd`` after successful TUN/TAP device open (pre ``--= user``
UID change).
@@ -763,6 +772,15 @@=
modifier is specified, and deleted from the environme= nt after the script
returns.
<= br>+: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:`peer_cert`
+ Identical to `peer_cert_0` for compatibility with older=
+ versions.
+

:code:`proto`<= /span>
The ``--proto`` parameter. Set on program initiation a= nd reset on
SIGHUP.
diff --git a/src/o= penvpn/init.c b/src/openvpn/init.c
index 9e2b3845..c5cc154 = 100644
--- a/src/openvpn/init.c
+++ b/src/o= penvpn/init.c
@@ -3336,6 +3336,7 @@
to= .auth_user_pass_verify_script_via_file =3D options->auth_user_pass_verif= y_script_via_file;
to.client_crresponse_script =3D opt= ions->client_crresponse_script;
to.tmp_dir =3D opti= ons->tmp_dir;
+ = to.export_peer_cert_dir =3D options->tls_export_peer_cert_dir; if (options->ccd_exclusive)
{ to.client_config_dir_exclusive =3D options->client_conf= ig_dir;
diff --git a/src/openvpn/options.c b/src/openvpn/op= tions.c
index e498114..1c0a6bd 100644
--- a= /src/openvpn/options.c
+++ b/src/openvpn/options.c@@ -1986,6 +1986,7 @@
SHOW_STR(cipher_list_tls= 13);
SHOW_STR(tls_cert_profile);
= SHOW_STR(tls_verify);
+ SHOW_STR(tls_export_peer_cert_dir);
SHOW_INT(ver= ify_x509_type);
SHOW_STR(verify_x509_name);
= SHOW_STR_INLINE(crl_file);
@@ -3048,6 +3049,7 @@=
MUST_BE_UNDEF(cipher_list_tls13);
MUST_BE_UNDEF(tls_cert_profile);

MUST_BE= _UNDEF(tls_verify);
+= MUST_BE_UNDEF(tls_export_peer_cert_path);
= MUST_BE_UNDEF(verify_x509_name);
MUST_BE_UNDEF(tls= _timeout);
MUST_BE_UNDEF(renegotiate_bytes);
@@ -4053,6 +4055,13 @@
= R_OK, "--crl-verify");
= }

+ {
+ errs |=3D check_file_access_chroot(options->chroo= t_dir, CHKACC_FILE,
+= options->tls_export_peer_cert_d= ir,
+ = W_OK, "--tls-export-cert");
+ }

+
ASSERT(options->conne= ction_list);
for (int i =3D 0; i < options->conn= ection_list->len; ++i)
{
@@ -8997,6= +9006,11 @@
string_substitute(p[1= ], ',', ' ', &options->gc),
= "tls-verify", true);
}
+ else if (streq(p[0],= "tls-export-cert") && p[1] && !p[2])
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ VERIFY_PERMISSION(OPT_P_SCRIPT);
+ options->tls_= export_peer_cert_dir =3D p[1];
+ }
else if (streq(p[0], "compat-nam= es"))
{
VERIFY_PERMISSIO= N(OPT_P_GENERAL);
diff --git a/src/openvpn/options.h b/src/= openvpn/options.h
index c4514e1..e52a953 100644
<= span>--- a/src/openvpn/options.h

+++ b/src/openvpn/options.= h
@@ -592,6 +592,7 @@
const char *tls_= cert_profile;
const char *ecdh_curve;
= const char *tls_verify;
+ const char *tls_export_peer_cert_dir;
int= verify_x509_type;
const char *verify_x509_name;
const char *crl_file;
diff --git a/src/open= vpn/ssl_common.h b/src/openvpn/ssl_common.h
index 925660b..= f085e0d 100644
--- a/src/openvpn/ssl_common.h
+++ b/src/openvpn/ssl_common.h

@@ -374,6 +374,7 @@
const char *client_crresponse_script;
= bool auth_user_pass_verify_script_via_file;
const char= *tmp_dir;
+ const= char *export_peer_cert_dir;
const char *auth_user_pas= s_file;
bool auth_user_pass_file_inline;

diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ss= l_verify.c
index bd7e512..35d3377 100644
--= - a/src/openvpn/ssl_verify.c
+++ b/src/openvpn/ssl_verify.c=
@@ -31,6 +31,7 @@
#endif
=
#include "syshead.h"
+#include <string.h>

#include "base64.h"
#include &qu= ot;manage.h"
@@ -457,6 +458,54 @@
= gc_free(&gc);
}

+/**
+ * Exports the certificate in \c peer_cert into the env= ironment and adds
+ *= the filname
+ */

+static bool
<= span style=3D"color: hsl(120, 100%, 40%);">+verify_cert_cert_export_env(str= uct env_set *es, openvpn_x509_cert_t *peer_cert,
+ int cert_depth, co= nst char *pem_export_fname)
+{
+ char e= nvname[64];
+ /* M= ake copy of the filename to manage that copy by the gc_arena */
<= span style=3D"color: hsl(120, 100%, 40%);">+ char *pem_export_filename = =3D strdup(pem_export_fname);
+
+ if (!= pem_export_filename)
= + {
+ retur= n false;
+ }
+
+ /* export the path to the certificate in= pem file format */
+= openvpn_snprintf(envname, sizeof(envname), "peer_cert_%d", ce= rt_depth);
+ seten= v_str(es, envname, pem_export_filename);
+
+ /* compatibility with older scripts/plugins that expect peer_cert wit= hout
+ * suffix *= /
+ if (cert_depth= =3D=3D 0)
+ {
+ setenv_str(es, = "peer_cert", pem_export_filename);
+ }
+
+ retu= rn 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", ce= rt_depth);
+ env_s= et_del(es, envname);
= +
+ /* compatibili= ty with older scripts/plugins that expect peer_cert without
+ * suffix */

+ if (cert_depth =3D=3D 0)+ {
+ env_set_del(es, "peer_cert&q= uot;);
+ }<= br>+ unlink(pem_export_fname= );
+}
+

/*
* call --tls-verify plug-in(s)

*/
@@ = -572,18 +621,19 @@
result_t
verify_cert(s= truct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
{
+ /= * need to define these variables here so goto cleanup will always have
+ * them defined */
result_t ret =3D FAILURE;
- char *subject =3D NULL;
- const struct tls_options *opt; struct gc_arena gc =3D gc_new();
+ const char *pem_export_fname =3D NULL;=

- opt = =3D session->opt;
= + const struct tls_options *opt =3D session->opt;
= ASSERT(opt);

session->verified = =3D false;

/* get the X509 name */
- subject =3D x509_ge= t_subject(cert, &gc);
+ char *subject =3D x509_get_subject(cert, &gc);
if (!subject)

{
msg(D_= TLS_ERRORS, "VERIFY ERROR: depth=3D%d, could not extract X509 "
@@ -706,6 +756,20 @@

= session->verify_maxlevel =3D max_int(session->verify_maxlevel, cert_d= epth);

+ if (opt->export_peer_cert_dir)
+ {
+ pem_export_fname =3D platform_create_temp_file(opt->exp= ort_peer_cert_dir,
+ = "pef", &g= c);
+
+ if (!pem_export_fname
+ || !verify_ce= rt_cert_export_env(opt->es, cert, cert_depth,
+ pe= m_export_fname))
+ = {
+ m= sg(D_TLS_ERRORS, "TLS Error: Failed to export certificate for "
+ "= ;--tls-export-cert in %s", opt->export_peer_cert_dir);
+ goto cleanup;
=
+ }
+ }
/* export= certificate values to the environment */
verify_cert_= set_env(opt->es, cert, cert_depth, subject, common_name,
opt->x509_track);

@@ -757,12 += 821,13 @@
ret =3D SUCCESS;
cleanup:
-
+ verify_cert_cert_del= ete_env(opt->es, cert_depth, pem_export_fname);
if = (ret !=3D SUCCESS)
{
tls_clea= r_error(); /* always? */
session->verified =3D = false; /* double sure? */
}
+
gc_free(&gc);=

return ret;
diff --git a/s= rc/openvpn/ssl_verify_backend.h b/src/openvpn/ssl_verify_backend.hindex d402b1f..5301a51 100644
--- a/src/openvpn/ssl= _verify_backend.h
+++ b/src/openvpn/ssl_verify_backend.h
@@ -161,6 +161,17 @@
= struct gc_arena *gc);

/*
+ * Write the certificate= to the file in PEM format.
+ *
+ *=
+ * @param cert Ce= rtificate to serialise.
+ *
+ * @return = \c FAILURE, \c or SUCCESS
+ */
= +result_t backend_x509_write_pem(openvpn_x509_cert_t *cert,
+ con= st char *filename);

+=
+/*
= * Save X509 fields to environment, using the naming convention: *
* X509_{cert_depth}_{name}=3D{value}diff --git a/src/openvpn/ssl_verify_mbedtls.c b/src/openvpn/ssl_veri= fy_mbedtls.c
index 5612139..24a89c3 100644
= --- a/src/openvpn/ssl_verify_mbedtls.c
+++ b/src/openvpn/ss= l_verify_mbedtls.c
@@ -218,6 +218,41 @@
= return buf;
}

+result_t
+backend_x509_write_pem(openvpn_x509_cert_t *cert, const= char *filename)
+{
+ /* mbed TLS does = not make it easy to write a certificate in PEM format.
+ * The only way is to directly acces= s the DER encoded raw certificate
+ * and PEM encode it ourselves */
+
+ struct gc_arena gc =3D gc_new();
+ /* just do a very loose upper bound f= or the base64 based PEM encoding
+ * using 3 times the space for the base64 and 100 bytes fo= r the
+ * headers= and footer */
+ s= truct buffer pem =3D alloc_buf_gc(cert->raw.len * 3 + 100, &gc);
+
+ struct buffer der =3D {};
<= span style=3D"color: hsl(120, 100%, 40%);">+ buf_set_read(&der, cert= ->raw.p, cert->raw.len);

+
+ if (= !crypto_pem_encode("CERTIFICATE", &pem, &der, &gc))<= /span>
+ {
+ goto err;

+ }
+
+ if (!buffer_write_file(filename, &pem))
+ {
+ goto err;
+ }
+
+ gc_free(&am= p;gc);
+ return SU= CCESS;
+err:+ msg(D_TLS_DEBUG_LOW, &qu= ot;Error writing X509 certificate to file %s",
+ filename);
+ gc_free(&gc);
+ return FAILURE;
+}
+
static struct buffer
x= 509_get_fingerprint(const mbedtls_md_info_t *md_info, mbedtls_x509_crt *cer= t,
struct gc_arena *gc)
diff --git a/src/openvpn/ssl_verify_openssl.c b/src/openvpn/ssl_verify_o= penssl.c

index 5afffc1..00fdec3 100644
--- = a/src/openvpn/ssl_verify_openssl.c
+++ b/src/openvpn/ssl_ve= rify_openssl.c
@@ -320,6 +320,29 @@
re= turn format_hex_ex(asn1_i->data, asn1_i->length, 0, 1, ":",= gc);
}

+result_t
+backend_x509_write_pem(openvpn_x509_cert_t *cert, const char *f= ilename)
+{+ BIO *out =3D BIO_new_file= (filename, "w");
+ if (!out)
= + {
+ goto = err;
+ }+
+ if (!PEM_write_bio_X509(out, cert))+ {
+ goto err;
+ }
+ BIO_free(out);
+
+= return SUCCESS;
+= err:
+ BIO_free(ou= t);
+ crypto_msg(D= _TLS_DEBUG_LOW, "Error writing X509 certificate to file %s",
+ filename)= ;
+ return FAILURE= ;
+}
+
struct buffer
x509_get_sha1_fingerprint(X509 *cert, struct gc_arena *gc)
{

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia9b3f1813d2d0d492d17c87348b= 4cebd0bf19ce2
Gerrit-Change-Number: 466
Gerrit-PatchSet: 9
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--DDN9RSrryCQ=--