From: "cron2 (Code Review)" Date: Fri, 17 Nov 2023 11:47:10 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., max@...2606... Message-ID: <4c6589de382cc084d5748684de73f17f257f734a-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="fSPvj2or47I="; 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 Cc: flichtenheld , plaisthos , openvpn-devel --fSPvj2or47I= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/40= 2?usp=3Demail ) 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(-) 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 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); } -#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: merged --fSPvj2or47I= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

Enable key export with mbed TLS 3.x.y

C= hange-Id: I8e90530726b7f7ba3cee0438f2d81a1ac42e821b
Signed-off-by: Max F= illinger <maximilian.fillinger@...2507...>
Acked-by: Frank Lichten= held <frank@...2641...>
Message-Id: <20231117091401.25793-1-ger= t@...1296...>
URL: https://www.mail-archive.com/openvpn-devel@...1096...= ourceforge.net/msg27458.html
Signed-off-by: Gert Doering <gert@...1826...= 6...>
---
M config.h.cmake.in
M configure.ac
M src/openvpn/M= akefile.am
M src/openvpn/crypto_mbedtls.c
M src/openvpn/mbedtls_compa= t.h
M src/openvpn/ssl_mbedtls.c
6 files changed, 107 insertions(+), 2= 1 deletions(-)

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 alw= ays 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 HA= VE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB 1
+#define HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET 1
=
/* Path to ifconfig tool */
#define IFC= ONFIG_PATH "@IFCONFIG_PATH@"
diff --git a/configu= re.ac b/configure.ac
index 7e5763d..84eaad6 100644--- a/configure.ac
+++ b/configure.ac
@@ -1025,11 +1025,11 @@

[AC_MSG_ERROR([mbed TLS versi= on >=3D 2.0.0 or >=3D 3.2.1 required])]
)
- AC_CHECK_= HEADER(
- psa/cr= ypto.h,
- [AC_DE= FINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [1], [yes])],
- [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [= 0], [no])]
- )
+ AC_CHECK_HEADER(<= br>+ psa/crypto.h,
<= span style=3D"color: hsl(120, 100%, 40%);">+ [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 fa= iled])]
)

+ AC_CHECK_FUNC(
+ [mbedtls_ssl_tls_prf],
+ [AC_DEFINE([HAVE_MBEDTLS_SSL_TLS_PRF], [1], [y= es])],
+ [AC_DEFINE(= [HAVE_MBEDTLS_SSL_TLS_PRF], [0], [no])]
+ )
- ,- [have_export_keying_material= =3D"no"]
+ = [AC_DEFINE([HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB], [1], [yes])],

+ [AC_DEFINE([HAVE_MBEDTL= S_SSL_CONF_EXPORT_KEYS_EXT_CB], [0], [no])]
)
<= span style=3D"color: hsl(120, 100%, 40%);">+ if test "x$ac_cv_func_mbe= dtls_ssl_conf_export_keys_ext_cb" !=3D xyes; then
+ AC_CHECK_FUNC(
+ [mbedtls_ssl_set_export_keys_cb],
+ [AC_DEFINE([HAVE_MBED= TLS_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&qu= ot;no"
+ fi
+ fi

AC_CHECK_FUNC(
[mbedtls_ctr_drbg_update= _ret],
- AC_DEFINE([HA= VE_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/openv= pn/Makefile.am
@@ -82,6 +82,7 @@
ovpn_dco= _win.h \
platform.c platform.h \
consol= e.c console.h console_builtin.c console_systemd.c \
+ mbedtls_compat.h \
mro= ute.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/o= penvpn/crypto_mbedtls.c
@@ -989,8 +989,9 @@

return diff;
}
-/* mbedtls-2.18.0 or newer */
<= span style=3D"color: hsl(0, 100%, 40%);">-#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 im= plementation if necessary. */
+#if HAVE_MBEDTLS_SSL_TLS_PRF && defined(MBEDTLS_SSL_TLS_P= RF_TLS1)
bool
ssl_tls1_PRF(const uint8_t = *seed, int seed_len, const uint8_t *secret,
i= nt secret_len, uint8_t *output, int output_len)
@@ -999,7 += 1000,7 @@
secret_le= n, "", seed, seed_len, output,
= output_len));
}
-#else /* ifdef HAVE_MBEDTLS_SSL_TLS_PRF *= /
+#else /* HAVE_MBED= TLS_SSL_TLS_PRF && defined(MBEDTLS_SSL_TLS_PRF_TLS1) */
<= span> /*

* 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(MBEDT= LS_SSL_TLS_PRF_TLS1) */
#endif /* ENABLE_CRYPTO_MBEDTLS */=
diff --git a/src/openvpn/mbedtls_compat.h b/src/openvpn/mb= edtls_compat.h
index fe7c3f9..610215b 100644
--- a/src/openvpn/mbedtls_compat.h

+++ b/src/openvpn/mbed= tls_compat.h
@@ -33,6 +33,8 @@
#ifndef MBE= DTLS_COMPAT_H_
#define MBEDTLS_COMPAT_H_
=
+#include "sysh= ead.h"
+<= br> #include "errlevel.h"

#include <mbedtls/cipher.h>

@@ -41,24 +43,25 @@
#include <mbedtls/md.h>
#include <m= bedtls/pem.h>
#include <mbedtls/pk.h>
<= span style=3D"color: hsl(120, 100%, 40%);">+#include <mbedtls/ssl.h><= /span>
#include <mbedtls/version.h>
#inclu= de <mbedtls/x509_crt.h>

-#if MBEDTLS_HAVE_PSA_CRYPTO_H
+#if HAVE_MBEDTLS_PSA_CRYPTO_H #include <psa/crypto.h>
#endif
static inline void
mbedtls_compa= t_psa_crypto_init(void)
{
-#if MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MBEDT= LS_PSA_CRYPTO_C)
+#if= HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C)<= br> if (psa_crypto_init() !=3D PSA_SUCCESS)
= {
msg(M_FATAL, "mbedtls: psa_crypto_init() fa= iled");
}
#else
= return;
-#endif /*= MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */
+#endif /* HAVE_MBEDTLS_P= SA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */
}<= /span>

/*
@@ -74,14 +77,14 @@
const unsigned char *additio= nal,
size_t add_len)=
{
-#if HAVE_= CTR_DRBG_UPDATE_RET
+= #if HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET
return mbedtls_ct= r_drbg_update_ret(ctx, additional, add_len);
#elif MBEDTLS= _VERSION_NUMBER < 0x03020100
mbedtls_ctr_drbg_updat= e(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 --g= it 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 H= AVE_EXPORT_KEYING_MATERIAL
+
+#if HAVE_MBE= DTLS_SSL_CONF_EXPORT_KEYS_EXT_CB
+/*
+ * K= ey export callback for older versions of mbed TLS, to be used with+ * mbedtls_ssl_conf_export_k= eys_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 c= allback in the mbedtls_ssl_config struct and it
+ * is used in the mbedtls_ssl_contexts set up f= rom that config. */
int
mbedtls_ssl_expor= t_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_pr= f_type =3D tls_prf_type;

- return true;
+ return 0;
}
+#elif HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB
+/*
+ * Key export callback for newer versio= ns of mbed TLS, to be used with
+ * mbedtls_ssl_set_export_keys_cb(). When used with TLS 1.2, th= e callback
+ * is cal= led 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 unfortunate= ly 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 un= signed 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 all key
+ = * types except MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET. */
+ if (type !=3D MBEDTLS_SSL_KE= Y_EXPORT_TLS12_MASTER_SECRET)

+ {
+ = return;
+ }
+
+ struct tls_session *session =3D p_exp= key;
+ 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<= br>+ * a different value, s= omething 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, expected %zu",

+ secret_len, sizeof(cache= ->master_secret));
+ }
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ memcpy(cache->client_ser= ver_random, client_random, 32);

+ memcpy(cache->client_server_random + 32, server_random, = 32);
+ memcpy(cach= e->master_secret, secret, sizeof(cache->master_secret));
+ cache->tls_prf_type =3D t= ls_prf_type;

+}
+#endif /* HAVE_MBEDTLS_SS= L_CONF_EXPORT_KEYS_EXT_CB */

bool=
key_state_export_keying_material(struct tls_session *session,
@@ -1196,8 +1253,8 @@
mbedtls_ssl_co= nf_max_version(ks_ssl->ssl_config, major, minor);
}=

-#if= def HAVE_EXPORT_KEYING_MATERIAL
- /* Initialize keying material exporter */
+#if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_E= XT_CB
+ /* Initial= ize keying material exporter, old style. */
mbedtls_ss= l_conf_export_keys_ext_cb(ks_ssl->ssl_config,
= mbedtls_ssl_export_keys_cb, session);=
#endif
@@ -1207,6 +1264,11 @@
= mbedtls_ssl_init(ks_ssl->ctx);
mbed_ok(mbedtls_s= sl_setup(ks_ssl->ctx, ks_ssl->ssl_config));
<= br>+#if HAVE_MBEDTLS_SSL_SET_EX= PORT_KEYS_CB
+ /* = Initialize keying material exporter, new style. */
+ mbedtls_ssl_set_export_keys_cb(ks_ssl-&g= t;ctx, mbedtls_ssl_export_keys_cb, session);
+#endif
+
/* Initialise BIOs */
= ALLOC_OBJ_CLEAR(ks_ssl->bio_ctx, bio_ctx);
mbedtl= s_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: merged
--fSPvj2or47I=--