From: "plaisthos (Code Review)" Date: Mon, 15 Jan 2024 12:46:09 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <87d8c51c54d0d2404a583ddae3c049d1a648dafc-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="YK+AuP/Qjbg="; 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 --YK+AuP/Qjbg= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld, plaisthos. 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 (#12). The following approvals got outdated and were removed: Code-Review-1 by cron2 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, 145 insertions(+), 5 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/66/466/12 diff --git a/doc/man-sections/script-options.rst b/doc/man-sections/script-= options.rst index 6f90e14..e05100a 100644 --- a/doc/man-sections/script-options.rst +++ b/doc/man-sections/script-options.rst @@ -423,6 +423,14 @@ See the `Environmental Variables`_ section below for additional parameters passed as environmental variables. +--tls-export-cert dir + Adds an environment variable ``peer_cert`` 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). @@ -633,6 +641,7 @@ Name of first ``--config`` file. Set on program initiation and reset on SIGHUP. + :code:`daemon` Set to "1" if the ``--daemon`` directive is specified, or "0" otherwis= e. Set on program initiation and reset on SIGHUP. @@ -763,6 +772,11 @@ modifier is specified, and deleted from the environment after the scri= pt returns. +:code:`peer_cert` + If the option ``--tls-export-cert`` is enabled, this option contains + 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:`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 f54f276..6975cbe 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -1995,6 +1995,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); @@ -3062,6 +3063,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_dir); MUST_BE_UNDEF(verify_x509_name); MUST_BE_UNDEF(tls_timeout); MUST_BE_UNDEF(renegotiate_bytes); @@ -4092,6 +4094,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) { @@ -9041,6 +9050,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 cbfff18..85de887 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..75a4b2e 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -31,6 +31,7 @@ #endif =20 #include "syshead.h" +#include #include "base64.h" #include "manage.h" @@ -457,6 +458,30 @@ 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, + const char *pem_export_fname) +{ + /* export the path to the current certificate in pem file format */ + setenv_str(es, "peer_cert", pem_export_fname); + + return backend_x509_write_pem(peer_cert, pem_export_fname) =3D=3D SUCC= ESS; +} + +static void +verify_cert_cert_delete_env(struct env_set *es, const char *pem_export_fna= me) +{ + env_set_del(es, "peer_cert"); + if (pem_export_fname) + { + unlink(pem_export_fname); + } +} + /* * call --tls-verify plug-in(s) */ @@ -572,18 +597,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 +732,19 @@ 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, pem_export_fnam= e)) + { + 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 +796,13 @@ ret =3D SUCCESS; cleanup: - + verify_cert_cert_delete_env(opt->es, 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); =20 /* + * 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: 12 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --YK+AuP/Qjbg= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld, plaisthos. <= /p>

plaisthos uploaded patch set #12 to this change.

= View Chan= ge

The following approvals got outdated and were removed: Code-Review-1 by cron2

Implement the --tls-export-cert feature

This = is a re-implementation of the --tls-export-cert feature. This
was necess= ary to due to missing approval to re-license the old
(now removed) code.= The re-implementation is based on the following
description of the feat= ure 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 fil= ename, which is provided via a
"peer_cert" environment varia= ble for the --tls-verify script or the
OPENVPN_PLUGIN_TLS_VERIFY plug-= in hook.

Once the script or plugin call has completed, OpenVPN shoul= d delete
this file.

Change-Id: Ia9b3f1813d2d0d492d17c87348b4cebd0= bf19ce2
Signed-off-by: Arne Schwabe <arne@...1227...>
---
M = doc/man-sections/script-options.rst
M src/openvpn/init.c
M src/openvp= n/options.c
M src/openvpn/options.h
M src/openvpn/ssl_common.h
M s= rc/openvpn/ssl_verify.c
M src/openvpn/ssl_verify_backend.h
M src/open= vpn/ssl_verify_mbedtls.c
M src/openvpn/ssl_verify_openssl.c
9 files c= hanged, 145 insertions(+), 5 deletions(-)

git pull ssh://gerrit.o=
penvpn.net:29418/openvpn refs/changes/66/466/12
diff --git a/doc/ma=
n-sections/script-options.rst b/doc/man-sections/script-options.rst<=
br>index 6f90e14..e05100a 100644
--- a/doc/man-sectio= ns/script-options.rst
+++ b/doc/man-sections/script-options= .rst
@@ -423,6 +423,14 @@
See the `Envir= onmental Variables`_ section below for additional
parame= ters passed as environmental variables.

+--tls-export-cert dir
+ Adds an environment variable ``pe= er_cert`` when calling the
+ ``--tls-verify`` script or executing the OPENVPN_PLUGIN_TLS_VERIFY= plugin
+ hook to ve= rify the certificate.
+
+ The environment= variable contains the path to a PEM encoded certificate
+ of the current peer certificate in t= he directory ``dir``.
+
--up cmd
Run command ``cmd`` after s= uccessful TUN/TAP device open (pre ``--user``
UID change= ).
@@ -633,6 +641,7 @@
Name of first `= `--config`` file. Set on program initiation and reset on
= SIGHUP.

+
:code:`daemon`
Set to "= ;1" if the ``--daemon`` directive is specified, or "0" other= wise.
Set on program initiation and reset on SIGHUP.
@@ -763,6 +772,11 @@
modifier is specif= ied, and deleted from the environment after the script
= returns.

+:code:`peer_cert`
+ If the option ``--tls-export-cert`` is enabled, this option conta= ins
+ the path to = the current peer certificate to be verified in PEM format.
+ See also the argument certificat= e_depth to the ``--tls-verify`` command.
+
:code:`proto`
T= he ``--proto`` parameter. Set on program initiation and reset on
= SIGHUP.
diff --git a/src/openvpn/init.c b/src/op= envpn/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_verify_script_via_file;
to.client_crresponse_script =3D options->client_crresp= onse_script;
to.tmp_dir =3D options->tmp_dir;

+ to.export_peer_cert_d= ir =3D options->tls_export_peer_cert_dir;
if (optio= ns->ccd_exclusive)
{
to.cl= ient_config_dir_exclusive =3D options->client_config_dir;
diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index f54f276..6975cbe 100644
--- a/src/openvpn/options.c=
+++ b/src/openvpn/options.c
@@ -1995,6 +19= 95,7 @@
SHOW_STR(cipher_list_tls13);
= SHOW_STR(tls_cert_profile);
SHOW_STR(tls_verify);<= /span>
+ SHOW_STR(tls_exp= ort_peer_cert_dir);
SHOW_INT(verify_x509_type);=
SHOW_STR(verify_x509_name);
SHOW_STR_IN= LINE(crl_file);
@@ -3062,6 +3063,7 @@
= MUST_BE_UNDEF(cipher_list_tls13);
MUST_BE_UNDE= F(tls_cert_profile);
MUST_BE_UNDEF(tls_verify);
+ MUST_BE_UNDEF(= tls_export_peer_cert_dir);
MUST_BE_UNDEF(verify_x5= 09_name);
MUST_BE_UNDEF(tls_timeout);
MUST_BE_UNDEF(renegotiate_bytes);

@@ -4092,6 += 4094,13 @@
= R_OK, "--crl-verify");
}
<= /span>
+ if (options->= tls_export_peer_cert_dir)
+ {
+ = errs |=3D check_file_access_chroot(options->chroot_dir, CHKACC_FILE,
+ = 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)
{
@@ -9041,6 +9050,11 @@
= string_substitute(p[1], ',', ' &= #39;, &options->gc),
"= tls-verify", true);
}
+ else if (streq(p[0], "tls-export-cert&= quot;) && 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 cbfff18..85de887 100644
--- a/src/openvp= n/options.h
+++ b/src/openvpn/options.h
@@ = -592,6 +592,7 @@
const char *tls_cert_profile;<= br> const char *ecdh_curve;
const char *tls_= verify;
+ const ch= ar *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/sr= c/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_v= erify_script_via_file;
const char *tmp_dir;
= + const char *export_peer_ce= rt_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..75a4b2e 100644
--- a/src/openvpn/ssl_v= erify.c
+++ b/src/openvpn/ssl_verify.c
@@ -= 31,6 +31,7 @@
#endif

#i= nclude "syshead.h"
+#include <string.h>

#incl= ude "base64.h"
#include "manage.h"
@@ -457,6 +458,30 @@
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, open= vpn_x509_cert_t *peer_cert,
+ const char *pem_export_fname)+{
+ /* export the path to the current certifica= te in pem file format */
+ setenv_str(es, "peer_cert", pem_export_fname);+
+ return backend_x509_write_pem(peer_cert, pem= _export_fname) =3D=3D SUCCESS;
+}
+=
+static void
+verify_cert_cert_delete_env(struct= env_set *es, const char *pem_export_fname)

+{
+ env_set_del(es, "peer_cert");
+ if (pem_export_fname)
+ {
+ unlink(pem_export_fname);
+ }
+}
+
/*
* call --tls-verify plug-in(s)
*/
@@ -572,18 +597,19 @@
= result_t
verify_cert(struct tls_session *session, openvpn_= x509_cert_t *cert, int cert_depth)
{
+ /* need to define these variables her= e so goto cleanup will always have
+ * them defined */
result_t ret =3D= FAILURE;
- char *su= bject =3D NULL;
- co= nst struct tls_options *opt;
struct gc_arena gc =3D gc= _new();
+ const ch= ar *pem_export_fname =3D NULL;

- opt =3D session->opt;
+ const struct tls_options *opt =3D s= ession->opt;
ASSERT(opt);
<= br> session->verified =3D false;

<= span> /* get the X509 name */

- subject =3D x509_get_subject(cert, &gc);
+ char *subject =3D x509_get_sub= ject(cert, &gc);

if (!subject)
= {
msg(D_TLS_ERRORS, "VERIFY ERROR: depth=3D= %d, could not extract X509 "
@@ -706,6 +732,19 @@


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

+ if (opt->export_peer_cert_di= r)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ pem_export_fname =3D pl= atform_create_temp_file(opt->export_peer_cert_dir,
+ = "pef", &gc);
+
+= if (!pem_export_fname
+ || !verify_cert_cert_export_env(opt->es, cert, pem= _export_fname))
+ = {
+ ms= g(D_TLS_ERRORS, "TLS Error: Failed to export certificate for "
+ "= --tls-export-cert in %s", opt->export_peer_cert_dir);
+ goto cleanup;
<= br>+ }
+ }
/* export = certificate values to the environment */
verify_cert_s= et_env(opt->es, cert, cert_depth, subject, common_name,
= opt->x509_track);
@@ -757,12 +7= 96,13 @@
ret =3D SUCCESS;

= cleanup:
-

+ verify_cert_cert_dele= te_env(opt->es, pem_export_fname);
if (ret !=3D SUC= CESS)
{
tls_clear_error(); /*= always? */
session->verified =3D false; /* dou= ble sure? */
}
+
gc_free(&gc);

return ret;
diff --git a/src/openvpn/ss= l_verify_backend.h b/src/openvpn/ssl_verify_backend.h
index= d402b1f..5301a51 100644
--- a/src/openvpn/ssl_verify_backe= nd.h
+++ b/src/openvpn/ssl_verify_backend.h
@@ -161,6 +161,17 @@

st= ruct 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 bac= kend_x509_write_pem(openvpn_x509_cert_t *cert,
+ const char *file= name);
+
+/*

* 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_mbedtls.c<= /span>
index 5612139..24a89c3 100644
--- a/src/ope= nvpn/ssl_verify_mbedtls.c
+++ b/src/openvpn/ssl_verify_mbed= tls.c
@@ -218,6 +218,41 @@
return buf;=
}

+result_t
+backend_x509_write_pem(openvpn_x509_cert_t *cert, const char *filena= me)
+{
+ /* mbed TLS does not make it e= asy to write a certificate in PEM format.

+ * The only way is to directly access the DER enc= oded raw certificate
= + * and PEM encode it ourselves */
+
+= struct gc_arena gc =3D gc_new();
+ /* just do a very loose upper bound for the base64 bas= ed PEM encoding
+ = * using 3 times the space for the base64 and 100 bytes for the
<= span style=3D"color: hsl(120, 100%, 40%);">+ * 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-&= gt;raw.len);
+=
+ if (!crypto_pem_encode= ("CERTIFICATE", &pem, &der, &gc))
+ {
+ goto err;
+ }
+
+ if (!buf= fer_write_file(filename, &pem))
+ {
+ goto err;
+= }
+
+ gc_free(&gc);

<= span style=3D"color: hsl(120, 100%, 40%);">+ return SUCCESS;
<= span style=3D"color: hsl(120, 100%, 40%);">+err:
+ msg(D_TLS_DEBUG_LOW, "Error writing X= 509 certificate to file %s",
+ filename);
+ gc_free(&gc);
+ return FAILURE;
+}
+
static struct buffer
x509_get_fingerprint(c= onst mbedtls_md_info_t *md_info, mbedtls_x509_crt *cert,
= struct gc_arena *gc)
diff --git a/src/o= penvpn/ssl_verify_openssl.c b/src/openvpn/ssl_verify_openssl.c
index 5afffc1..00fdec3 100644

--- a/src/openvpn/ssl_ver= ify_openssl.c
+++ b/src/openvpn/ssl_verify_openssl.c=
@@ -320,6 +320,29 @@
return format_hex_ex(as= n1_i->data, asn1_i->length, 0, 1, ":", gc);
}

+result_t
+backend_x= 509_write_pem(openvpn_x509_cert_t *cert, const char *filename)
+{
+ BIO *out =3D BIO_new_file(filename, "w&qu= ot;);
+ if (!out)<= /span>
+ {
+ 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, "Er= ror writing X509 certificate to file %s",
+ filename);
+ return FAILURE;
+}
+
struct buffer
x509_get_s= ha1_fingerprint(X509 *cert, struct gc_arena *gc)
{<= br>

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

<= /div>
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia9b3f1813d2d0d492d17c87348b= 4cebd0bf19ce2
Gerrit-Change-Number: 466
Gerrit-PatchSet: 12
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: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--YK+AuP/Qjbg=--