Skip to contentSkip to conversation

Prefer TLS libraries TLS PRF function, fix OpenVPN in FIPS mode

PATCH In review Arne Schwabe 3 participants 5 messages Mar 5, 2021 – Mar 8, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 Prefer TLS libraries TLS PRF function, fix OpenVPN in FIPS mode

This moves from using our own copy of the TLS1 PRF function to using TLS library provided function where possible. This includes currently OpenSSL 1.1.0+ and mbed TLS 2.18+.

For the libraries where it is not possible to use the library's own function, we still use our own implementation. mbed TLS will continue to use our own old PRF function while for OpenSSL we will use a adapted version from OpenSSL 1.0.2t code. The version allows to be used in a FIPS enabled environment.

The old OpenSSL and mbed TLS implementation could have shared some more code but as we will eventually drop support for older TLS libraries, the separation makes it easier it remove that code invdidually.

In FIPS mode MD5 is normally forbidden, the TLS1 PRF1 function we use, makes uses of MD5, which in the past has caused OpenVPN to segfault. The new implementation for OpenSSL version of our custom implementation has added the special flags that tell OpenSSL that this specific use of MD5 is allowed in FIPS mode.

No FIPS conformitiy testing etc has been done, this is only about allowing OpenVPN on a system where FIPS mode has been enabled system wide (e.g. on RHEL derivates).

Patch v4: Handle the unlikely case that PRF generation fails. More formatting
fixes.
Patch v5: v4 with the formatting fixes actually commited. sigh.

Patch v6: More formatting fixes, make OpenSSL fucntion return bool instead
of int.

---
 Changes.rst                            |   6 +
 src/openvpn/crypto_backend.h           |  16 ++
 src/openvpn/crypto_mbedtls.c           | 132 +++++++++++++++
 src/openvpn/crypto_openssl.c           | 216 +++++++++++++++++++++++++
 src/openvpn/ssl.c                      | 206 ++++++-----------------
 tests/unit_tests/openvpn/test_crypto.c |  32 ++++
 6 files changed, 449 insertions(+), 159 deletions(-)
Changes.rst +6 -0
index 2a847564..62008e8d 100644
@@ -9,6 +9,12 @@ Keying Material Exporters (RFC 5705) based key generation
     the RFC5705 based key material generation to the current custom
     OpenVPN PRF. This feature requires OpenSSL or mbed TLS 2.18+.
 
+Compatibility with OpenSSL in FIPS mode
+    OpenVPN will now work with OpenSSL in FIPS mode. Note, no effort
+    has been made to check or implement all the
+    requirements/recommendation of FIPS 140-2. This just allows OpenVPN
+    to be run on a system that be configured OpenSSL in FIPS mode.
+
 Deprecated features
 -------------------
 ``inetd`` has been removed
src/openvpn/crypto_backend.h +16 -0
index 85cb084a..384ffc80 100644
@@ -699,4 +699,20 @@ const char *translate_cipher_name_from_openvpn(const char *cipher_name);
  */
 const char *translate_cipher_name_to_openvpn(const char *cipher_name);
 
+
+/**
+ * Calculates the TLS 1.0-1.1 PRF function. For the exact specification of the
+ * function definition see the TLS RFCs like RFC 4346.
+ *
+ * @param seed          seed to use
+ * @param seed_len      length of the seed
+ * @param secret        secret to use
+ * @param secret_len    length of the secret
+ * @param output        output destination
+ * @param output_len    length of output/number of bytes to generate
+ *
+ * @return              true if successful, false on any error
+ */
+bool ssl_tls1_PRF(const uint8_t *seed, int seed_len, const uint8_t *secret,
+                  int secret_len, uint8_t *output, int output_len);
 #endif /* CRYPTO_BACKEND_H_ */
src/openvpn/crypto_mbedtls.c +132 -0

142 lines not on this page · raw message

src/openvpn/crypto_openssl.c +216 -0

226 lines not on this page · raw message

src/openvpn/ssl.c +47 -159

259 lines not on this page · raw message

tests/unit_tests/openvpn/test_crypto.c +32 -0
index ea9b99b2..af83da68 100644
@@ -38,6 +38,7 @@
 #include <cmocka.h>
 
 #include "crypto.h"
+#include "ssl_backend.h"
 
 #include "mock_msg.h"
 
@@ -136,12 +137,43 @@ crypto_translate_cipher_names(void **state)
     test_cipher_names("id-aes256-GCM", "AES-256-GCM");
 }
 
+
+static uint8_t good_prf[32] = {0xd9, 0x8c, 0x85, 0x18, 0xc8, 0x5e, 0x94, 0x69,
+                               0x27, 0x91, 0x6a, 0xcf, 0xc2, 0xd5, 0x92, 0xfb,
+                               0xb1, 0x56, 0x7e, 0x4b, 0x4b, 0x14, 0x59, 0xe6,
+                               0xa9, 0x04, 0xac, 0x2d, 0xda, 0xb7, 0x2d, 0x67};
+static void
+crypto_test_tls_prf(void **state)
+{
+    const char *seedstr = "Quis aute iure reprehenderit in voluptate "
+                          "velit esse cillum dolore";
+    const unsigned char *seed = (const unsigned char *)seedstr;
+    const size_t seed_len = strlen(seedstr);
+
+
+
+
+    const char* ipsumlorem = "Lorem ipsum dolor sit amet, consectetur "
+                             "adipisici elit, sed eiusmod tempor incidunt ut "
+                             "labore et dolore magna aliqua.";
+
+    const unsigned char *secret = (const unsigned char *) ipsumlorem;
+    size_t secret_len = strlen((const char *)secret);
+
+
+    uint8_t out[32];
+    ssl_tls1_PRF(seed, seed_len, secret, secret_len, out, sizeof(out));
+
+    assert_memory_equal(good_prf, out, sizeof(out));
+}
+
 int
 main(void)
 {
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(crypto_pem_encode_decode_loopback),
         cmocka_unit_test(crypto_translate_cipher_names),
+        cmocka_unit_test(crypto_test_tls_prf)
     };
 
 #if defined(ENABLE_CRYPTO_OPENSSL)
Signature · 1 line
2.30.1
Acked-by on v6 1/1
#1 lore raw

Hi,

Quoted 25 lines · Arne Schwabe

This moves from using our own copy of the TLS1 PRF function to using TLS library provided function where possible. This includes currently OpenSSL 1.1.0+ and mbed TLS 2.18+.

For the libraries where it is not possible to use the library's own function, we still use our own implementation. mbed TLS will continue to use our own old PRF function while for OpenSSL we will use a adapted version from OpenSSL 1.0.2t code. The version allows to be used in a FIPS enabled environment.

The old OpenSSL and mbed TLS implementation could have shared some more code but as we will eventually drop support for older TLS libraries, the separation makes it easier it remove that code invdidually.

In FIPS mode MD5 is normally forbidden, the TLS1 PRF1 function we use, makes uses of MD5, which in the past has caused OpenVPN to segfault. The new implementation for OpenSSL version of our custom implementation has added the special flags that tell OpenSSL that this specific use of MD5 is allowed in FIPS mode.

No FIPS conformitiy testing etc has been done, this is only about allowing OpenVPN on a system where FIPS mode has been enabled system wide (e.g. on RHEL derivates).

Quoted 8 lines

Patch v4: Handle the unlikely case that PRF generation fails. More formatting
fixes.
Patch v5: v4 with the formatting fixes actually commited. sigh.

Patch v6: More formatting fixes, make OpenSSL fucntion return bool instead
of int.

It looks good and passes my basic tests.

Signature · 1 line
Antonio Quartulli
#2 lore raw

Hi,

Quoted 4 lines · Antonio Quartulli
Quoted 3 lines · Arne Schwabe

This moves from using our own copy of the TLS1 PRF function to using TLS library provided function where possible. This includes currently OpenSSL 1.1.0+ and mbed TLS 2.18+.

[..]

Quoted 3 lines

It looks good and passes my basic tests.

ACKs are good, but SIGSEGV are bad.

This crashes for me on the "--tls-client, but no --client" test case, with "Linux, mbed TLS 2.25.0" (talking to a server with the same code, "3338f2d5a2b7 + this patch"

run openvpn --ca /home/gert/src/openvpn.git-keys/ca.crt --cert /home/gert/src/openvpn.git-keys/cron2-gentoo-i386.crt --key /home/gert/src/openvpn.git-keys/cron2-gentoo-i386.key --remote-cert-tls server --nobind --comp-lzo --verb 3

         --tls-client --dev tap --proto tcp-client --remote gentoo.ov.greenie.net 51204 --ifconfig 10.204.9.2 255.255.255.0 --comp-lzo --tun-ipv6 --ifconfig-ipv6 fd00:abcd:204:9::2/64 fd00:abcd:204:9::1 --route 10.204.0.0 255.255.0.0 10.204.9.1 --route-ipv6 fd00:abcd:204::/48 
         Segmentation fault

GDB tells me:

...

2021-03-07 19:27:15 ++ Certificate has EKU (str) TLS Web Server Authentication, expects TLS Web Server Authentication
2021-03-07 19:27:15 VERIFY EKU OK
2021-03-07 19:27:15 VERIFY OK: depth=0, C=US, ST=California, L=Pleasanton, O=OpenVPN community project, CN=server, [email protected]...

Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7f34ef8 in ?? () from /usr/lib64/libmbedtls.so.13 (gdb) where

#0  0x00007ffff7f34ef8 in ?? () from /usr/lib64/libmbedtls.so.13
#1  0x00007ffff7f35945 in ?? () from /usr/lib64/libmbedtls.so.13
#2  0x00007ffff7f37234 in ?? () from /usr/lib64/libmbedtls.so.13
#3  0x00007ffff7f3a727 in mbedtls_ssl_handshake_client_step () from /usr/lib64/libmbedtls.so.13
#4  0x00007ffff7f4c12d in mbedtls_ssl_handshake_step () from /usr/lib64/libmbedtls.so.13
#5  0x00007ffff7f4c1b8 in mbedtls_ssl_handshake () from /usr/lib64/libmbedtls.so.13
#6  0x00007ffff7f429b8 in mbedtls_ssl_read () from /usr/lib64/libmbedtls.so.13
#7  0x00005555555d0c38 in key_state_read_plaintext ([email protected]...=0x555555627d00, 
    [email protected]...=0x555555627eb0, [email protected]...=2048) at ../../../openvpn/src/openvpn/buffer.h:231
#8  0x00005555555c9a38 in tls_process (wakeup=0x7fffffffd8b4, to_link_socket_info=<optimized out>, 
    to_link_addr=0x7fffffffd7c8, to_link=<optimized out>, session=<optimized out>, multi=0x555555627720)
    at ../../../openvpn/src/openvpn/ssl.c:2806
#9  tls_multi_process (multi=0x555555627720, [email protected]...=0x7fffffffe6d0, 
    [email protected]...=0x7fffffffe3c0, to_link_socket_info=<optimized out>, 
    [email protected]...=0x7fffffffd8d4) at ../../../openvpn/src/openvpn/ssl.c:3009
#10 0x000055555556c593 in check_tls ([email protected]...=0x7fffffffd910) at ../../../openvpn/src/openvpn/forward.c:148
#11 0x000055555556fc68 in pre_select ([email protected]...=0x7fffffffd910)
    at ../../../openvpn/src/openvpn/forward.c:1837
#12 0x000055555559375e in tunnel_point_to_point (c=0x7fffffffd910)
    at ../../../openvpn/src/openvpn/openvpn.c:79

#13 openvpn_main (argc=35, argv=0x7fffffffe998) at ../../../openvpn/src/openvpn/openvpn.c:283

It passes the test same case (same OS, same build tree) for OpenSSL 1.1.1j and also for FreeBSD/OpenSSL 1.0.2u and FreeBSD/mbed TLS 2.16.9.

As the commit message talks about "mbed TLS 2.18+", it seems that code gets irked by --tls-client/p2p.

I do not understand these code paths enough to do further educated guesses, but we can't merge it like that.

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]...
#3 lore raw
In reply to Arne Schwabe

After some discussion on IRC today, it turns out that I was holding my test rig wrong. As in: breakage occurs in the combination of "mbed TLS 2.25.0, TLS, TCP and --dev tap", but it does not actually matter whether this patch is applied or not - 2.5.1 breaks as well.

Arne's test found the commit in mbedTLS between 2.24 and 2.25 that introduces the breakage (360e2c41d8211e43), but this does not really explain anything - but it's fairly clear "not something broken in our code", or at least "not recently".

As discussed on IRC, I have fixed what whitespace uncrustify complained about ("if<blank>(", function return types on the preceding line, #endif with comment)

This all said, I now do actually understand what the patch does (and it looks good) and I think I have all versions of old/new mbedtls and old/new openssl covered, client and server. All pass, except for mbedtls 2.25.0 + tcp + tap.

Your patch has been applied to the master branch.

commit 06f6cf3ff850f2930bf4a864ae9898407e94ffb9

Author: Arne Schwabe
Date:   Fri Mar 5 15:13:52 2021 +0100
     Prefer TLS libraries TLS PRF function, fix OpenVPN in FIPS mode

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg21612.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#4 lore raw

Hi,

Quoted 4 lines · Gert Doering

After some discussion on IRC today, it turns out that I was holding my test rig wrong. As in: breakage occurs in the combination of "mbed TLS 2.25.0, TLS, TCP and --dev tap", but it does not actually matter whether this patch is applied or not - 2.5.1 breaks as well.

Arne found the trigger.

It is neither related to TCP nor to "--dev tap", but it needs a server side with "OpenSSL 1.1.1" *and* a client side with "mbedTLS 2.25" to trigger this - both ends need to negotiate curve25591, and then mbedTLS will crash in the debug print function.

https://github.com/ARMmbed/mbedtls/issues/4208

My current test rigs do not test this combination, except for this particular test case (tcp+tap towards --inetd server), so we were chasing red herrings for a while...

I need to think long and hard now how to add meaningful tests with the new test matrix

openssl 1.0.2u <-> openssl 1.1.1
mbedTLS "oldish" <-> mbedTLS "very new

to the test sets, without making the already-long server side test (40 minutes) go totally out of bounds...

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]...