From: "cron2 (Code Review)" Date: Fri, 17 Nov 2023 11:47:08 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., max@...2606... Message-ID: <4c6589de382cc084d5748684de73f17f257f734a-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="/C0qobHQo74="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Enable key export with mbed TLS 3.x.y List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: MaxF , plaisthos , flichtenheld Cc: openvpn-devel --/C0qobHQo74= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#6) to the change originally created by= MaxF. ( http://gerrit.openvpn.net/c/openvpn/+/402?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by flichtenheld Change subject: Enable key export with mbed TLS 3.x.y ...................................................................... Enable key export with mbed TLS 3.x.y Change-Id: I8e90530726b7f7ba3cee0438f2d81a1ac42e821b Signed-off-by: Max Fillinger Acked-by: Frank Lichtenheld Message-Id: <20231117091401.25793-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg27= 458.html Signed-off-by: Gert Doering --- M config.h.cmake.in M configure.ac M src/openvpn/Makefile.am M src/openvpn/crypto_mbedtls.c M src/openvpn/mbedtls_compat.h M src/openvpn/ssl_mbedtls.c 6 files changed, 107 insertions(+), 21 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/02/402/6 diff --git a/config.h.cmake.in b/config.h.cmake.in index 1c0dd6f..19b79bc 100644 --- a/config.h.cmake.in +++ b/config.h.cmake.in @@ -387,7 +387,10 @@ #undef HAVE_VSNPRINTF /* we always assume a recent mbed TLS version */ -#define HAVE_CTR_DRBG_UPDATE_RET 1 +#define HAVE_MBEDTLS_PSA_CRYPTO_H 1 +#define HAVE_MBEDTLS_SSL_TLS_PRF 1 +#define HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB 1 +#define HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET 1 /* Path to ifconfig tool */ #define IFCONFIG_PATH "@IFCONFIG_PATH@" diff --git a/configure.ac b/configure.ac index 7e5763d..84eaad6 100644 --- a/configure.ac +++ b/configure.ac @@ -1025,11 +1025,11 @@ [AC_MSG_ERROR([mbed TLS version >=3D 2.0.0 or >=3D 3.2.1 required])] ) - AC_CHECK_HEADER( - psa/crypto.h, - [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [1], [yes])], - [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [0], [no])] - ) + AC_CHECK_HEADER( + psa/crypto.h, + [AC_DEFINE([HAVE_MBEDTLS_PSA_CRYPTO_H], [1], [yes])], + [AC_DEFINE([HAVE_MBEDTLS_PSA_CRYPTO_H], [0], [no])] + ) AC_CHECK_FUNCS( [ \ @@ -1040,16 +1040,32 @@ [AC_MSG_ERROR([mbed TLS check for AEAD support failed])] ) + AC_CHECK_FUNC( + [mbedtls_ssl_tls_prf], + [AC_DEFINE([HAVE_MBEDTLS_SSL_TLS_PRF], [1], [yes])], + [AC_DEFINE([HAVE_MBEDTLS_SSL_TLS_PRF], [0], [no])] + ) + have_export_keying_material=3D"yes" AC_CHECK_FUNC( [mbedtls_ssl_conf_export_keys_ext_cb], - , - [have_export_keying_material=3D"no"] + [AC_DEFINE([HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB], [1], [yes])], + [AC_DEFINE([HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB], [0], [no])] ) + if test "x$ac_cv_func_mbedtls_ssl_conf_export_keys_ext_cb" !=3D xyes; then + AC_CHECK_FUNC( + [mbedtls_ssl_set_export_keys_cb], + [AC_DEFINE([HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB], [1], [yes])], + [AC_DEFINE([HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB], [0], [no])] + ) + if test "x$ac_cv_func_mbedtls_ssl_set_export_keys_cb" !=3D xyes; then + have_export_keying_material=3D"no" + fi + fi AC_CHECK_FUNC( [mbedtls_ctr_drbg_update_ret], - AC_DEFINE([HAVE_CTR_DRBG_UPDATE_RET], [1], + AC_DEFINE([HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET], [1], [Use mbedtls_ctr_drbg_update_ret from mbed TLS]), ) diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am index 52deef8..b953961 100644 --- a/src/openvpn/Makefile.am +++ b/src/openvpn/Makefile.am @@ -82,6 +82,7 @@ ovpn_dco_win.h \ platform.c platform.h \ console.c console.h console_builtin.c console_systemd.c \ + mbedtls_compat.h \ mroute.c mroute.h \ mss.c mss.h \ mstats.c mstats.h \ diff --git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.c index aaf6ef7..ad3439c 100644 --- a/src/openvpn/crypto_mbedtls.c +++ b/src/openvpn/crypto_mbedtls.c @@ -989,8 +989,9 @@ return diff; } -/* mbedtls-2.18.0 or newer */ -#ifdef HAVE_MBEDTLS_SSL_TLS_PRF +/* mbedtls-2.18.0 or newer implements tls_prf, but prf_tls1 is removed + * from recent versions, so we use our own implementation if necessary. */ +#if HAVE_MBEDTLS_SSL_TLS_PRF && defined(MBEDTLS_SSL_TLS_PRF_TLS1) bool ssl_tls1_PRF(const uint8_t *seed, int seed_len, const uint8_t *secret, int secret_len, uint8_t *output, int output_len) @@ -999,7 +1000,7 @@ secret_len, "", seed, seed_len, out= put, output_len)); } -#else /* ifdef HAVE_MBEDTLS_SSL_TLS_PRF */ +#else /* HAVE_MBEDTLS_SSL_TLS_PRF && defined(MBEDTLS_SSL_TLS_PRF_TLS1) */ /* * Generate the hash required by for the \c tls1_PRF function. * @@ -1128,5 +1129,5 @@ gc_free(&gc); return true; } -#endif /* ifdef HAVE_MBEDTLS_SSL_TLS_PRF */ +#endif /* HAVE_MBEDTLS_SSL_TLS_PRF && defined(MBEDTLS_SSL_TLS_PRF_TLS1) */ #endif /* ENABLE_CRYPTO_MBEDTLS */ diff --git a/src/openvpn/mbedtls_compat.h b/src/openvpn/mbedtls_compat.h index fe7c3f9..610215b 100644 --- a/src/openvpn/mbedtls_compat.h +++ b/src/openvpn/mbedtls_compat.h @@ -33,6 +33,8 @@ #ifndef MBEDTLS_COMPAT_H_ #define MBEDTLS_COMPAT_H_ +#include "syshead.h" + #include "errlevel.h" #include @@ -41,24 +43,25 @@ #include #include #include +#include #include #include -#if MBEDTLS_HAVE_PSA_CRYPTO_H +#if HAVE_MBEDTLS_PSA_CRYPTO_H #include #endif =20 static inline void mbedtls_compat_psa_crypto_init(void) { -#if MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) +#if HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) if (psa_crypto_init() !=3D PSA_SUCCESS) { msg(M_FATAL, "mbedtls: psa_crypto_init() failed"); } #else return; -#endif /* MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */ +#endif /* HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */ } /* @@ -74,14 +77,14 @@ const unsigned char *additional, size_t add_len) { -#if HAVE_CTR_DRBG_UPDATE_RET +#if HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET return mbedtls_ctr_drbg_update_ret(ctx, additional, add_len); #elif MBEDTLS_VERSION_NUMBER < 0x03020100 mbedtls_ctr_drbg_update(ctx, additional, add_len); return 0; #else return mbedtls_ctr_drbg_update(ctx, additional, add_len); -#endif /* HAVE_CTR_DRBG_UPDATE_RET */ +#endif /* HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET */ } static inline int diff --git a/src/openvpn/ssl_mbedtls.c b/src/openvpn/ssl_mbedtls.c index 6a3cd44..9c9167d 100644 --- a/src/openvpn/ssl_mbedtls.c +++ b/src/openvpn/ssl_mbedtls.c @@ -173,6 +173,16 @@ } #ifdef HAVE_EXPORT_KEYING_MATERIAL + +#if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB +/* + * Key export callback for older versions of mbed TLS, to be used with + * mbedtls_ssl_conf_export_keys_ext_cb(). It is called with the master + * secret, client random and server random, and the type of PRF function + * to use. + * + * Mbed TLS stores this callback in the mbedtls_ssl_config struct and it + * is used in the mbedtls_ssl_contexts set up from that config. */ int mbedtls_ssl_export_keys_cb(void *p_expkey, const unsigned char *ms, const unsigned char *kb, size_t maclen, @@ -193,8 +203,55 @@ memcpy(cache->master_secret, ms, sizeof(cache->master_secret)); cache->tls_prf_type =3D tls_prf_type; - return true; + return 0; } +#elif HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB +/* + * Key export callback for newer versions of mbed TLS, to be used with + * mbedtls_ssl_set_export_keys_cb(). When used with TLS 1.2, the callback + * is called with the TLS 1.2 master secret, client random, server random + * and the type of PRF to use. With TLS 1.3, it is called with several + * different keys (indicated by type), but unfortunately not the exporter + * master secret. + * + * Unlike in older versions, the callback is not stored in the + * mbedtls_ssl_config. It is placed in the mbedtls_ssl_context after it + * has been set up. */ +void +mbedtls_ssl_export_keys_cb(void *p_expkey, + mbedtls_ssl_key_export_type type, + const unsigned char *secret, + size_t secret_len, + const unsigned char client_random[32], + const unsigned char server_random[32], + mbedtls_tls_prf_types tls_prf_type) +{ + /* Since we can't get the TLS 1.3 exporter master secret, we ignore al= l key + * types except MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET. */ + if (type !=3D MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) + { + return; + } + + struct tls_session *session =3D p_expkey; + struct key_state_ssl *ks_ssl =3D &session->key[KS_PRIMARY].ks_ssl; + struct tls_key_cache *cache =3D &ks_ssl->tls_key_cache; + + /* The TLS 1.2 master secret has a fixed size, so if secret_len has + * a different value, something is wrong with mbed TLS. */ + if (secret_len !=3D sizeof(cache->master_secret)) + { + msg(M_FATAL, + "ERROR: Incorrect TLS 1.2 master secret length: Got %zu, expec= ted %zu", + secret_len, sizeof(cache->master_secret)); + } + + memcpy(cache->client_server_random, client_random, 32); + memcpy(cache->client_server_random + 32, server_random, 32); + memcpy(cache->master_secret, secret, sizeof(cache->master_secret)); + cache->tls_prf_type =3D tls_prf_type; +} +#endif /* HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB */ bool key_state_export_keying_material(struct tls_session *session, @@ -1196,8 +1253,8 @@ mbedtls_ssl_conf_max_version(ks_ssl->ssl_config, major, minor); } =20 -#ifdef HAVE_EXPORT_KEYING_MATERIAL - /* Initialize keying material exporter */ +#if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB + /* Initialize keying material exporter, old style. */ mbedtls_ssl_conf_export_keys_ext_cb(ks_ssl->ssl_config, mbedtls_ssl_export_keys_cb, sessio= n); #endif @@ -1207,6 +1264,11 @@ mbedtls_ssl_init(ks_ssl->ctx); mbed_ok(mbedtls_ssl_setup(ks_ssl->ctx, ks_ssl->ssl_config)); +#if HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB + /* Initialize keying material exporter, new style. */ + mbedtls_ssl_set_export_keys_cb(ks_ssl->ctx, mbedtls_ssl_export_keys_cb= , session); +#endif + /* Initialise BIOs */ ALLOC_OBJ_CLEAR(ks_ssl->bio_ctx, bio_ctx); mbedtls_ssl_set_bio(ks_ssl->ctx, ks_ssl->bio_ctx, ssl_bio_write, -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/402?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: I8e90530726b7f7ba3cee0438f2d81a1ac42e821b Gerrit-Change-Number: 402 Gerrit-PatchSet: 6 Gerrit-Owner: MaxF Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --/C0qobHQo74= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #6 to the change originally cr= eated by MaxF.

View Change

The following approvals got outdated and = were removed: Code-Review+2 by flichtenheld

Enable key export with mbed TLS 3.x.y

= Change-Id: I8e90530726b7f7ba3cee0438f2d81a1ac42e821b
Signed-off-by: Max = Fillinger <maximilian.fillinger@...2507...>
Acked-by: Frank Lichte= nheld <frank@...2641...>
Message-Id: <20231117091401.25793-1-ge= rt@...1296...>
URL: https://www.mail-archive.com/openvpn-devel@...1199...= sourceforge.net/msg27458.html
Signed-off-by: Gert Doering <gert@...1711...= 96...>
---
M config.h.cmake.in
M configure.ac
M src/openvpn/= Makefile.am
M src/openvpn/crypto_mbedtls.c
M src/openvpn/mbedtls_comp= at.h
M src/openvpn/ssl_mbedtls.c
6 files changed, 107 insertions(+), = 21 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn r=
efs/changes/02/402/6
diff --git a/config.h.cmake.in b/config.h.cmak=
e.in
index 1c0dd6f..19b79bc 100644
--- a/co= nfig.h.cmake.in
+++ b/config.h.cmake.in
@@ = -387,7 +387,10 @@
#undef HAVE_VSNPRINTF
<= /span>
/* we always assume a recent mbed TLS version */-#define HAVE_CTR_DRBG_UPDATE_RE= T 1
+#define HAVE_MBE= DTLS_PSA_CRYPTO_H 1
+= #define HAVE_MBEDTLS_SSL_TLS_PRF 1
+#define HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB 1
+#define HAVE_MBEDTLS_CTR_DRBG_UPDA= TE_RET 1


/* Path to ifconfig tool */
#define IFCONFIG_PATH "@IFCONFIG_PATH@"
<= span>diff --git a/configure.ac b/configure.ac

index 7e5763d= ..84eaad6 100644
--- a/configure.ac
+++ b/c= onfigure.ac
@@ -1025,11 +1025,11 @@
[AC_= MSG_ERROR([mbed TLS version >=3D 2.0.0 or >=3D 3.2.1 required])]
)

- AC_CHECK_HEADER(
- psa/crypto.h,
- [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [1], [yes])],
- [AC_DEFINE([MBED= TLS_HAVE_PSA_CRYPTO_H], [0], [no])]
- )
+= AC_CHECK_HEADER(
+ = psa/crypto.h,
+ [AC_= DEFINE([HAVE_MBEDTLS_PSA_CRYPTO_H], [1], [yes])],
+ [AC_DEFINE([HAVE_MBEDTLS_PSA_CRYPTO_H], [0]= , [no])]
+ )
AC_CHECK_FUNCS(
[ \@@ -1040,16 +1040,32 @@
[AC_MSG_ERROR([mbed TLS = check for AEAD support failed])]
)

+ AC_CHECK_FUNC(+ [mbedtls_ssl_tls_prf],
+ [AC_DEFINE([HAVE_MBED= TLS_SSL_TLS_PRF], [1], [yes])],
+ [AC_DEFINE([HAVE_MBEDTLS_SSL_TLS_PRF], [0], [no])]
= + )
+
have_export_keying_material= =3D"yes"
AC_CHECK_FUNC(
[mbe= dtls_ssl_conf_export_keys_ext_cb],
- ,
- [ha= ve_export_keying_material=3D"no"]
+ [AC_DEFINE([HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT= _CB], [1], [yes])],
+= [AC_DEFINE([HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB], [0], [no])]=
)
+ if te= st "x$ac_cv_func_mbedtls_ssl_conf_export_keys_ext_cb" !=3D xyes; = then
+ AC_CHECK_FUNC= (
+ [mbedtls_ssl_se= t_export_keys_cb],
+ = [AC_DEFINE([HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB], [1], [yes])],+ [AC_DEFINE([HAVE_MBEDTLS_S= SL_SET_EXPORT_KEYS_CB], [0], [no])]
+ )
+= if test "x$ac_cv_func_mbedtls_ssl_set_export_keys_cb" !=3D xyes= ; then
+ have_expor= t_keying_material=3D"no"
+ fi
+= fi

AC_CHECK_FUNC(
[= mbedtls_ctr_drbg_update_ret],
- AC_DEFINE([HAVE_CTR_DRBG_UPDATE_RET], [1],
+ AC_DEFINE([HAVE_MBEDTLS_CTR_DRBG_UPDATE= _RET], [1],
[Use mbedtls_ctr_drbg_update_ret from mbe= d TLS]),
)

diff --git a= /src/openvpn/Makefile.am b/src/openvpn/Makefile.am
index 52= deef8..b953961 100644
--- a/src/openvpn/Makefile.am<= br>+++ b/src/openvpn/Makefile.am
@@ -82,6 +82,7 @@
ovpn_dco_win.h \
platform.c platform.h \<= /span>
console.c console.h console_builtin.c console_systemd.c \=
+ mbedtls_compat.h \=
mroute.c mroute.h \
mss.c mss.h \
mstats.c mstats.h \
diff --git a/src/openvpn/= crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.c
index aaf6e= f7..ad3439c 100644
--- a/src/openvpn/crypto_mbedtls.c
+++ b/src/openvpn/crypto_mbedtls.c
@@ -989,8 +98= 9,9 @@

return diff;
= }
-/* mbedtls-2.18.0 o= r newer */
-#ifdef HAVE= _MBEDTLS_SSL_TLS_PRF
= +/* mbedtls-2.18.0 or newer implements tls_prf, but prf_tls1 is removed
+ * from recent versions= , so we use our own implementation if necessary. */
+#if HAVE_MBEDTLS_SSL_TLS_PRF && d= efined(MBEDTLS_SSL_TLS_PRF_TLS1)
bool
ssl= _tls1_PRF(const uint8_t *seed, int seed_len, const uint8_t *secret,<= br> int secret_len, uint8_t *output, int output_len)
@@ -999,7 +1000,7 @@
= secret_len, "", seed, seed_len, output,
= output_len));
}

-#else /* ifdef HA= VE_MBEDTLS_SSL_TLS_PRF */
+#else /* HAVE_MBEDTLS_SSL_TLS_PRF && defined(MBEDTLS_SSL_TLS_= PRF_TLS1) */
/*
* Generate the hash requ= ired by for the \c tls1_PRF function.
*
@= @ -1128,5 +1129,5 @@
gc_free(&gc);
return true;

}
-#endif /* ifdef HAVE_MBEDTLS_SSL_TLS_PRF */
+#endif /* HAVE_MBEDTLS_SSL_TLS_PRF = && defined(MBEDTLS_SSL_TLS_PRF_TLS1) */

#endif /* = ENABLE_CRYPTO_MBEDTLS */
diff --git a/src/openvpn/mbedtls_c= ompat.h b/src/openvpn/mbedtls_compat.h
index fe7c3f9..61021= 5b 100644
--- a/src/openvpn/mbedtls_compat.h
+++ b/src/openvpn/mbedtls_compat.h

@@ -33,6 +33,8 @@
#ifndef MBEDTLS_COMPAT_H_
#define MBEDTLS_COM= PAT_H_

+#include "syshead.h"
+
#include "errlevel.h"
#include <mbedtls/cipher.h>
@@ -41,24 +43,25 @@

#include <mbedtls/md.h><= br> #include <mbedtls/pem.h>
#include <mbed= tls/pk.h>
+#includ= e <mbedtls/ssl.h>
#include <mbedtls/version.h>=
#include <mbedtls/x509_crt.h>

-#if MBEDTLS_HAVE_PSA_CRYP= TO_H
+#if HAVE_MBEDTL= S_PSA_CRYPTO_H
#include <psa/crypto.h> #endif

static inline void=
mbedtls_compat_psa_crypto_init(void)
{-#if MBEDTLS_HAVE_PSA_CRYPTO_H = && defined(MBEDTLS_PSA_CRYPTO_C)
+#if HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDT= LS_PSA_CRYPTO_C)
if (psa_crypto_init() !=3D PSA_SUCCES= S)
{
msg(M_FATAL, "mbedt= ls: psa_crypto_init() failed");
}
#else

return;
-#endif /* MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MBEDT= LS_PSA_CRYPTO_C) */
+= #endif /* HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C= ) */
}

/*
@@ -74,14 +77,14 @@

cons= t unsigned char *additional,
= size_t add_len)
{
-#if HAVE_CTR_DRBG_UPDATE_RET
+#if HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET
return mbedtls_ctr_drbg_update_ret(ctx, additional, add_len);
=
#elif MBEDTLS_VERSION_NUMBER < 0x03020100
= mbedtls_ctr_drbg_update(ctx, additional, add_len);
r= eturn 0;
#else
return mbedtls_ctr_drb= g_update(ctx, additional, add_len);
-#endif /* HAVE_CTR_DRBG_UPDATE_RET */
+#endif /* HAVE_MBEDTLS_CTR_DRBG_UPDATE_RE= T */
}

static inline in= t
diff --git a/src/openvpn/ssl_mbedtls.c b/src/openvpn/ssl_= mbedtls.c
index 6a3cd44..9c9167d 100644
---= a/src/openvpn/ssl_mbedtls.c
+++ b/src/openvpn/ssl_mbedtls.= c
@@ -173,6 +173,16 @@
}
=
#ifdef HAVE_EXPORT_KEYING_MATERIAL
+
+#if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB
+/*

+ * Key export callback for older versions of mbed TL= S, to be used with
+ = * mbedtls_ssl_conf_export_keys_ext_cb(). It is called with the master
+ * secret, client random = and server random, and the type of PRF function
+ * to use.
+ *
+ * Mbed TLS stores this callback in the mbedtls_ssl_config struct and it<= /span>
+ * is used in the mb= edtls_ssl_contexts set up from that config. */
int<= br> mbedtls_ssl_export_keys_cb(void *p_expkey, const unsigned char *m= s,
const unsigned char *kb, siz= e_t maclen,
@@ -193,8 +203,55 @@
memcp= y(cache->master_secret, ms, sizeof(cache->master_secret));
= cache->tls_prf_type =3D tls_prf_type;

- return true;
= + return 0;
= }
+#elif HAVE_MBEDTL= S_SSL_SET_EXPORT_KEYS_CB
+/*
+ * Key expor= t callback for newer versions of mbed TLS, to be used with
+ * mbedtls_ssl_set_export_keys_cb().= When used with TLS 1.2, the callback
+ * is called with the TLS 1.2 master secret, client rando= m, server random
+ * = and the type of PRF to use. With TLS 1.3, it is called with several<= br>+ * different keys (indicate= d by type), but unfortunately not the exporter
+ * master secret.
+ *
+ * Unlike in older versions, the callback is not stored in the
+ * mbedtls_ssl_config. I= t is placed in the mbedtls_ssl_context after it
+ * has been set up. */
+void
+mbedtls_ssl_export_keys_cb(void *p_expkey,
+ mbedtls_s= sl_key_export_type type,
+ const unsigned char *secret,
+ size_t = secret_len,

+ = const unsigned char client_random[32],
+ const unsi= gned char server_random[32],
+ mbedtls_tls_prf_types tls_prf_type)
+{
+ /* Since we can't get the TLS 1.3= exporter master secret, we ignore all key
+ * types except MBEDTLS_SSL_KEY_EXPORT_TLS12_MAS= TER_SECRET. */
+ i= f (type !=3D MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET)
+ {
+ return;
+ }
+
+ struct tls= _session *session =3D p_expkey;
+ struct key_state_ssl *ks_ssl =3D &session->key[KS_PR= IMARY].ks_ssl;
+ s= truct tls_key_cache *cache =3D &ks_ssl->tls_key_cache;
+

+ /* The TLS 1.2 master secret has a fixed size, s= o if secret_len has
+= * a different value, something is wrong with mbed TLS. */
+ if (secret_len !=3D sizeof(c= ache->master_secret))

+ {
+ m= sg(M_FATAL,
+ = "ERROR: Incorrect TLS 1.2 master secret length: Got %zu, expected = %zu",
+ = secret_len, sizeof(cache->master_secret));
+ }
+
+ = memcpy(cache->client_server_random, client_random, 32);
+ memcpy(cache->client_server_r= andom + 32, server_random, 32);
+ memcpy(cache->master_secret, secret, sizeof(cache->ma= ster_secret));
+ c= ache->tls_prf_type =3D tls_prf_type;
+}
+#endif /* HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB */

bool
key_state_export_keying_material(stru= ct tls_session *session,
@@ -1196,8 +1253,8 @@
mbedtls_ssl_conf_max_version(ks_ssl->ssl_config, major, min= or);

}

-#ifdef HAVE_EXPORT_KEYING_MATERIAL
- /* Initialize keying material expo= rter */
+#if HAVE_MBE= DTLS_SSL_CONF_EXPORT_KEYS_EXT_CB
+ /* Initialize keying material exporter, old style. */
mbedtls_ssl_conf_export_keys_ext_cb(ks_ssl->ssl_config,=
mbedtls_ssl_expor= t_keys_cb, session);
#endif
@@ -1207,6 +12= 64,11 @@
mbedtls_ssl_init(ks_ssl->ctx);
<= span> mbed_ok(mbedtls_ssl_setup(ks_ssl->ctx, ks_ssl->ssl_config))= ;


+= #if HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB
+ /* Initialize keying material exporter, new style. = */
+ mbedtls_ssl_s= et_export_keys_cb(ks_ssl->ctx, mbedtls_ssl_export_keys_cb, session);
+#endif
+
/* Initialise = BIOs */
ALLOC_OBJ_CLEAR(ks_ssl->bio_ctx, bio_ctx);<= /span>
mbedtls_ssl_set_bio(ks_ssl->ctx, ks_ssl->bio_ctx= , ssl_bio_write,

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I8e90530726b7f7ba3cee0438f2d= 81a1ac42e821b
Gerrit-Change-Number: 402
Gerrit-PatchSet: 6
Gerrit-Owner: MaxF <max@...2606...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--/C0qobHQo74=--