mbedtls: add RFC 5705 keying material exporter support
PATCH 1/1 mbedtls: add RFC 5705 keying material exporter support
Since mbed TLS 2.18, mbed TLS can also implement RFC 5705. As a first step towards using the keying material exporter as a method to generate key material for the data channel, implement the --keying-material-exporter function we already have for OpenSSL also for mbed TLS builds.
Implementing RFC 5705 for mbed TLS is a bit more cumbersome, because the library itself only provides a callback that is called during connection setup, which enables us to implement RFC 5705 ourselves. To protect ourselves against mistakes, we immediately perform the required key derivation to generate the exporterd keying material, and only cache the derived key material until we can actually export it to the environment (similar to the OpenSSL builds).
To test this, I found it easiest to temporarily move the call to key_state_export_keying_material outside the if statement, and use a script that runs after connection setup (e.g. --ipchange) that prints the environment. E.g.
#!/bin/sh
env | sort
This should show the same value for the exported_keying_material env variable for both mbed TLS and OpenSSL builds. Of course you can also use the code as-is, and write a plugin to verify the same thing.
--- src/openvpn/init.c | 4 +-- src/openvpn/options.c | 4 +-- src/openvpn/options.h | 2 +- src/openvpn/ssl_mbedtls.c | 64 ++++++++++++++++++++++++++++++++++++++- src/openvpn/ssl_mbedtls.h | 5 +++ src/openvpn/ssl_openssl.c | 4 ++- src/openvpn/syshead.h | 14 ++++++++- 7 files changed, 89 insertions(+), 8 deletions(-)
src/openvpn/init.c +2 -2
@@ -2931,7 +2931,7 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) to.comp_options = options->comp;#endif -#if defined(ENABLE_CRYPTO_OPENSSL ) &&OPENSSL_VERSION_NUMBER >=0x10001000 +#ifdef HAVE_EKM if (options->keying_material_exporter_label) { to.ekm_size = options->keying_material_exporter_length;
@@ -2947,7 +2947,7 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) { to.ekm_size =0 ; } -#endif +#endif /* HAVE_EKM */ /* TLS handshake authentication (--tls-auth) */ if (options->ce.tls_auth_file)
src/openvpn/options.c +2 -2
@@ -661,7 +661,7 @@ static const char usage_message[] =" an explicit nsCertType designation t = 'client' | 'server'.\n" "--x509-track x : Save peer X509 attribute x in environment for use by\n" " plugins and management interface.\n" -#if defined(ENABLE_CRYPTO_OPENSSL ) &&OPENSSL_VERSION_NUMBER >=0x10001000 +#ifdef HAVE_EKM"--keying-material-exporter label len : Save Exported Keying Material (RFC5705)\n" " of len bytes (min. 16 bytes) using label in environment for use by plugins.\n" #endif
@@ -8468,7 +8468,7 @@ add_option(struct options *options, options->use_peer_id = true; options->peer_id =atoi (p[1 ]); } -#if defined(ENABLE_CRYPTO_OPENSSL ) &&OPENSSL_VERSION_NUMBER >=0x10001000 +#ifdef HAVE_EKM else if (streq(p[0], "keying-material-exporter") && p[1] && p[2]) {int ekm_length =positive_atoi (p[2 ]);
src/openvpn/options.h +1 -1
@@ -640,7 +640,7 @@ struct optionsbool use_peer_id;uint32_t peer_id; -#if defined(ENABLE_CRYPTO_OPENSSL ) &&OPENSSL_VERSION_NUMBER >=0x10001000 +#ifdef HAVE_EKM /* Keying Material Exporters [RFC 5705] */ const char *keying_material_exporter_label;int keying_material_exporter_length;
src/openvpn/ssl_mbedtls.c +63 -1
94 lines not on this page · raw message
src/openvpn/ssl_mbedtls.h +5 -0
@@ -33,6 +33,7 @@#include <mbedtls/ssl.h> #include <mbedtls/x509_crt.h> +#include <mbedtls/version.h> #if defined(ENABLE_PKCS11 )#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
@@ -111,6 +112,10 @@ struct key_state_ssl {mbedtls_ssl_config ssl_config;/**< mbedTLS global ssl config */ mbedtls_ssl_context *ctx;/**< mbedTLS connection context */ bio_ctx bio_ctx; + +/** Keying material exporter cache (RFC 5705). */ +uint8_t *exported_key_material; + };/**
src/openvpn/ssl_openssl.c +3 -1
@@ -160,7 +160,9 @@ key_state_export_keying_material(struct key_state_ssl *ssl,unsigned char *ekm = (unsigned char *)gc_malloc (size, true, &gc);if (SSL_export_keying_material (ssl->ssl, ekm, size, - session->opt->ekm_label, session->opt->ekm_label_size,NULL ,0 ,0 )) + session->opt->ekm_label, + session->opt->ekm_label_size, +NULL ,0 ,0 )) {unsigned int len = (size *2 ) +2 ;
src/openvpn/syshead.h +13 -1
@@ -548,11 +548,15 @@ socket_defined(const socket_descriptor_t sd)#undef ENABLE_DEF_AUTH#endif -/* Enable mbed TLS RNG prediction resistance support */ #ifdef ENABLE_CRYPTO_MBEDTLS +#include <mbedtls/version.h> #define ENABLE_PREDICTION_RESISTANCE #endif /* ENABLE_CRYPTO_MBEDTLS */ +#ifdef ENABLE_CRYPTO_OPENSSL +#include <openssl/opensslv.h> +#endif /* ENABLE_CRYPTO_OPENSSL */ +/* * Enable packet filter? */
@@ -597,6 +601,14 @@ socket_defined(const socket_descriptor_t sd)#define ENABLE_CRYPTOAPI #endif +/* +* Do we support RFC 5705 keying material exporters? +*/ +#if (defined(ENABLE_CRYPTO_MBEDTLS ) &&MBEDTLS_VERSION_NUMBER >=0x02120000 ) || \ + (defined(ENABLE_CRYPTO_OPENSSL ) &&OPENSSL_VERSION_NUMBER >=0x10001000 ) +#define HAVE_EKM +#endif +/* * Is poll available on this platform? */
Signature · 1 line
2.20.1
Am 11.11.19 um 00:10 schrieb Steffan Karger:
Quoted 19 lines
Since mbed TLS 2.18, mbed TLS can also implement RFC 5705. As a first step towards using the keying material exporter as a method to generate key material for the data channel, implement the --keying-material-exporter function we already have for OpenSSL also for mbed TLS builds.
Implementing RFC 5705 for mbed TLS is a bit more cumbersome, because the library itself only provides a callback that is called during connection setup, which enables us to implement RFC 5705 ourselves. To protect ourselves against mistakes, we immediately perform the required key derivation to generate the exporterd keying material, and only cache the derived key material until we can actually export it to the environment (similar to the OpenSSL builds).
To test this, I found it easiest to temporarily move the call to
key_state_export_keying_materialoutside the if statement, and use a script that runs after connection setup (e.g. --ipchange) that prints the environment. E.g.
Quoted 8 lines
#!/bin/sh
env | sortThis should show the same value for the
exported_keying_materialenv variable for both mbed TLS and OpenSSL builds. Of course you can also use the code as-is, and write a plugin to verify the same thing.Signed-off-by Steffan Karger <[email protected]...>
If we do a v2 of the patch could we rename HAVE_EKM to something better like HAVE_EXPORT_KEYMATERIAL? EKM is a bit a too short for my taste.
Otherwise it looks good to me. I cannot really test it because I currently do not have anything that uses export keymaterial.
As background why we want/need this patch. We currently use a fixed PRF for determining the data channel key. While what we use (SHA1 +MD5 like TLS 1.0) is perfectly fine, it would nice to instead use a more modern approach. And using RFC 5705 for something like key-method 3 avoids reinventing the wheel.
So from me point of view:
Arne
Hi,
Quoted 6 lines · Arne Schwabe
Am 11.11.19 um 00:10 schrieb Steffan Karger:
Quoted 5 lines
Since mbed TLS 2.18, mbed TLS can also implement RFC 5705. As a first step towards using the keying material exporter as a method to generate key material for the data channel, implement the --keying-material-exporter function we already have for OpenSSL also for mbed TLS builds.
I tried to apply this patch today, since I have such a nice ACK on it.
Applying to "master" went smooth, but the resulting code does not build for me:
../../../openvpn/src/openvpn/ssl_mbedtls.c:91:41: error: 'mbedtls_x509_crt_profile_suiteb' undeclared (first use in this function); did you mean 'openvpn_x509_crt_profile_suiteb'?
91 | #define openvpn_x509_crt_profile_suiteb mbedtls_x509_crt_profile_suiteb;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~this is on Gentoo linux, with Gentoo-provided "mbedtls-2.19.1" (so, should qualify as "2.18 or higher") and gcc 9.2.0.
Looking closer, I can see that current "git master" does not compile on this system either, with the same error message. Seems I should run local tests with mbedtls more often, especially after updating...
mbedtls 2.12.0 on the other Gentoo system (the t_server buildslave, which actually builds 2 times a week against mbedtls) works, as does mbedtls 2.16.3 on on FreeBSD.
So it seems this is something the mbedtls people broke in 2.19?
(And, for the record, anything newer than 2.12 is masked in gentoo, I just unmasked "give me the latest!" at some point in the past so got the fun today...)
Steffan, this will bite you anyway, some day :-) - can you have a look?
(I'll proceed to merge the patch...)
... awww... I think I might have found the underlying issue, trying to understand the MBEDTLS_VERSION_NUMBER convention...
I see you check for "MBEDTLS_VERSION_NUMBER >= 0x02120000", the comment says "from 2.18 up", and the thing Gentoo calls "2.19.1" installs a version.h which claims
version.h:#define MBEDTLS_VERSION_NUMBER 0x02110000
version.h:#define MBEDTLS_VERSION_STRING "2.17.0"
... but the tarball has the proper defines. WTF...
Awww...
Gentoo's "mbedtls 2.19.1" also installs "mbedcrypto 2.0.0", which *also* installs a mbedtls/version.h - and that one is the "2.17.0" one which ends up in the filesystem. This might be considered a bug in the .ebuild file, but I find it amazingly silly to have two packages who both have a "mbedtls/version.h"...
Ceterum censeo: mbedtls has looked long and hard at LibreSSL version numbering and decided "we can do this in more exciting ways"...
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Your patch has been applied to the master branch.
I ran test builds on FreeBSD/mbedtls 2.16.3 (t_client test) and Gentoo/mbedtls 2.12.0 (t_server test), which pass. No tests were done with the crypto material exporter - have no test rig for it (but I wouldn't mind some easy to set up scripts to *do* test it...)
Of course my mbedtls versions are all too old to provide the needed functionality in the first place, so I suspect "#if HAVE_EKM" takes care of me not seeing any effects... but see my other mail on what happens if I upgrade the Gentoo to 2.19.1... *bang* :-)
commit ab27c9f77b7cd10d530b3c6380465322969a9d21
Author: Steffan Karger Date: Mon Nov 11 00:10:18 2019 +0100
mbedtls: add RFC 5705 keying material exporter support
Signed-off-by: Steffan Karger <[email protected]...>
Acked-by: Arne Schwabe <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg19111.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering