Skip to contentSkip to conversation

Move openvpn specific key expansion into its own function

PATCH In review Arne Schwabe 5 participants 29 messages Aug 25, 2020 – Apr 3, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 Move openvpn specific key expansion into its own function

This moves the OpenVPN specific PRF into its own function also simplifies the code a bit by passing tls_session directly instead of 5 of its fields.

Patch v2: Rebase

Patch v4: rewrite/fix comments,
fix potential not initialised before goto issue

---
 src/openvpn/ssl.c | 119 ++++++++++++++++++++++++++++++----------------
 1 file changed, 79 insertions(+), 40 deletions(-)
src/openvpn/ssl.c +79 -40
index 3fcaa25f..b4d94b8a 100644
@@ -1765,27 +1765,37 @@ openvpn_PRF(const uint8_t *secret,
     VALGRIND_MAKE_READABLE((void *)output, output_len);
 }
 
-/*
- * Using source entropy from local and remote hosts, mix into
- * master key.
- */
-static bool
-generate_key_expansion(struct key_ctx_bi *key,
-                       const struct key_type *key_type,
-                       const struct key_source2 *key_src,
-                       const struct session_id *client_sid,
-                       const struct session_id *server_sid,
-                       bool server)
+static void
+init_key_contexts(struct key_ctx_bi *key,
+                  const struct key_type *key_type,
+                  bool server,
+                  struct key2 *key2)
+{
+    /* Initialize key contexts */
+    int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL;
+    init_key_ctx_bi(key, key2, key_direction, key_type, "Data Channel");
+
+    /* Initialize implicit IVs */
+    key_ctx_update_implicit_iv(&key->encrypt, (*key2).keys[(int)server].hmac,
+                               MAX_HMAC_KEY_LENGTH);
+    key_ctx_update_implicit_iv(&key->decrypt, (*key2).keys[1-(int)server].hmac,
+                               MAX_HMAC_KEY_LENGTH);
+
+}
+
+
+static struct key2
+generate_key_expansion_openvpn_prf(const struct tls_session *session)
 {
     uint8_t master[48] = { 0 };
-    struct key2 key2 = { 0 };
-    bool ret = false;
 
-    if (key->initialized)
-    {
-        msg(D_TLS_ERRORS, "TLS Error: key already initialized");
-        goto exit;
-    }
+    const struct key_state *ks = &session->key[KS_PRIMARY];
+    const struct key_source2 *key_src = ks->key_src;
+
+    const struct session_id *client_sid = session->opt->server ?
+                                          &ks->session_id_remote : &session->session_id;
+    const struct session_id *server_sid = !session->opt->server ?
+                                          &ks->session_id_remote : &session->session_id;
 
     /* debugging print of source key material */
     key_source2_print(key_src);
@@ -1803,6 +1813,7 @@ generate_key_expansion(struct key_ctx_bi *key,
                 master,
                 sizeof(master));
 
+    struct key2 key2;
     /* compute key expansion */
     openvpn_PRF(master,
                 sizeof(master),
@@ -1815,41 +1826,73 @@ generate_key_expansion(struct key_ctx_bi *key,
                 server_sid,
                 (uint8_t *)key2.keys,
                 sizeof(key2.keys));
+    secure_memzero(&master, sizeof(master));
 
+    /*
+     * fixup_key only correctly sets DES parity bits if the cipher is a
+     * DES variant.
+     *
+     * The newer OpenSSL and mbed TLS libraries (those that support EKM)
+     * ignore these bits.
+     *
+     * We keep the DES fixup here as compatibility.
+     * OpenVPN3 never did this fixup anyway. So this code is *probably* not
+     * required but we keep it for compatibility until we remove DES support
+     * since it does not hurt either.
+     */
+    for (int i = 0; i < 2; ++i)
+    {
+        fixup_key(&key2.keys[i], &session->opt->key_type);
+    }
     key2.n = 2;
 
-    key2_print(&key2, key_type, "Master Encrypt", "Master Decrypt");
+    return key2;
+}
+
+/*
+ * Using source entropy from local and remote hosts, mix into
+ * master key.
+ */
+static bool
+generate_key_expansion(struct key_ctx_bi *key,
+                       const struct tls_session *session)
+{
+    bool ret = false;
+    struct key2 key2;
+
+    if (key->initialized)
+    {
+        msg(D_TLS_ERRORS, "TLS Error: key already initialized");
+        goto exit;
+    }
+
+
+    bool server = session->opt->server;
+
+    key2 = generate_key_expansion_openvpn_prf(session);
+
+    key2_print(&key2, &session->opt->key_type,
+               "Master Encrypt", "Master Decrypt");
 
     /* check for weak keys */
     for (int i = 0; i < 2; ++i)
     {
-        fixup_key(&key2.keys[i], key_type);
-        if (!check_key(&key2.keys[i], key_type))
+        if (!check_key(&key2.keys[i], &session->opt->key_type))
         {
             msg(D_TLS_ERRORS, "TLS Error: Bad dynamic key generated");
             goto exit;
         }
     }
-
-    /* Initialize OpenSSL key contexts */
-    int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL;
-    init_key_ctx_bi(key, &key2, key_direction, key_type, "Data Channel");
-
-    /* Initialize implicit IVs */
-    key_ctx_update_implicit_iv(&key->encrypt, key2.keys[(int)server].hmac,
-                               MAX_HMAC_KEY_LENGTH);
-    key_ctx_update_implicit_iv(&key->decrypt, key2.keys[1-(int)server].hmac,
-                               MAX_HMAC_KEY_LENGTH);
-
+    init_key_contexts(key, &session->opt->key_type, server, &key2);
     ret = true;
 
 exit:
-    secure_memzero(&master, sizeof(master));
     secure_memzero(&key2, sizeof(key2));
 
     return ret;
 }
 
+
 static void
 key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len)
 {
@@ -1879,10 +1922,7 @@ tls_session_generate_data_channel_keys(struct tls_session *session)
 {
     bool ret = false;
     struct key_state *ks = &session->key[KS_PRIMARY];   /* primary key */
-    const struct session_id *client_sid = session->opt->server ?
-                                          &ks->session_id_remote : &session->session_id;
-    const struct session_id *server_sid = !session->opt->server ?
-                                          &ks->session_id_remote : &session->session_id;
+
 
     if (ks->authenticated == KS_AUTH_FALSE)
     {
@@ -1891,9 +1931,8 @@ tls_session_generate_data_channel_keys(struct tls_session *session)
     }
 
     ks->crypto_options.flags = session->opt->crypto_flags;
-    if (!generate_key_expansion(&ks->crypto_options.key_ctx_bi,
-                                &session->opt->key_type, ks->key_src, client_sid, server_sid,
-                                session->opt->server))
+
+    if (!generate_key_expansion(&ks->crypto_options.key_ctx_bi, session))
     {
         msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion failed");
         goto cleanup;
Signature · 1 line
2.26.2
#1 lore raw

PATCH v4 2/2 Implement generating data channel keys via EKM/RFC 5705

OpenVPN currently uses its own (based on TLS 1.0) key derivation mechanism to generate the 256 bytes key data in key2 struct that are then used used to generate encryption/hmac/iv vectors. While this mechanism is still secure, it is not state of the art.

Instead of modernising our own approach, this commit implements key derivation using the Keying Material Exporters API introduced by RFC 5705.

We also use an opportunistic approach of negotiating the use of EKM (exported key material) through an IV_PROTO flag and prefer EKM to our own PRF if both client and server support it. The use of EKM is pushed to the client as part of NCP as key-derivation tls-ekm.

We still exchange the random data (112 bytes from client to server and 64 byte from server to client) for the OpenVPN PRF but do not use it. Removing that exchange would break the handshake and make a key-method 3 or similar necessary.

As a side effect, this makes a little bit easier to have a FIPS compatible version of OpenVPN since we do not rely on calling MD5 anymore.

Side note: this commit breaks the (not yet merged) WolfSSL support as it claims to support EKM in the OpenSSL compat API but always returns an error if you try to use it.

Patch v2: rebase/change to V2 of EKM refactoring

Patch v3: add Changes.rst

---
 Changes.rst                      | 11 +++++++
 doc/doxygen/doc_key_generation.h | 14 +++++++--
 src/openvpn/crypto.h             |  4 +++
 src/openvpn/init.c               |  1 +
 src/openvpn/multi.c              |  4 +++
 src/openvpn/options.c            | 14 +++++++++
 src/openvpn/options.h            |  3 ++
 src/openvpn/push.c               |  5 ++-
 src/openvpn/ssl.c                | 54 ++++++++++++++++++++++++++++----
 src/openvpn/ssl.h                |  2 ++
 src/openvpn/ssl_backend.h        |  2 ++
 src/openvpn/ssl_mbedtls.c        |  7 ++---
 12 files changed, 107 insertions(+), 14 deletions(-)
Changes.rst +11 -0
index f67e1d76..2a2829e7 100644
@@ -1,3 +1,14 @@
+Overview of changes in 2.6
+==========================
+
+
+New features
+------------
+Keying Material Exporters (RFC 5705) based key generation
+    As part of the cipher negotiation OpenVPN will automatically prefer
+    the RFC5705 based key material generation to the current custom
+    OpenVPN PRF. This feature requires OpenSSL or mbed TLS 2.18+.
+
 Overview of changes in 2.5
 ==========================
 
doc/doxygen/doc_key_generation.h +12 -2
index 4bb9c708..cef773a9 100644
@@ -58,6 +58,12 @@
  *
  * @subsection key_generation_method_2 Key method 2
  *
+ * There are two methods for generating key data when using key method 2
+ * the first is OpenVPN's traditional approach that exchanges random
+ * data and uses a PRF and the other is using the RFC5705 keying material
+ * exporter to generate the key material. For both methods the random
+ * data is exchange but only used in the traditional method.
+ *
  * -# The client generates random material in the following amounts:
  *    - Pre-master secret: 48 bytes
  *    - Client's PRF seed for master secret: 32 bytes
@@ -73,8 +79,12 @@
  *    server's random material.
  *
  * %Key method 2 %key expansion is performed by the \c
- * generate_key_expansion() function.  Please refer to its source code for
- * details of the %key expansion process.
+ * generate_key_expansion_openvpn_prf() function.  Please refer to its source
+ * code for details of the %key expansion process.
+ *
+ * When the client sends the IV_PROTO_TLS_KEY_EXPORT flag and the server replies
+ * with `key-derivation tls-ekm` the RFC5705 key material exporter with the
+ * label EXPORTER-OpenVPN-datakeys is used for the key data.
  *
  * @subsection key_generation_random Source of random material
  *
src/openvpn/crypto.h +4 -0
index 999f643e..ec935ca5 100644
@@ -254,6 +254,10 @@ struct crypto_options
 #define CO_MUTE_REPLAY_WARNINGS (1<<2)
     /**< Bit-flag indicating not to display
      *   replay warnings. */
+#define CO_USE_TLS_KEY_MATERIAL_EXPORT  (1<<3)
+    /**< Bit-flag indicating that key derivation
+     * is done using TLS keying material export [RFC5705]
+     */
     unsigned int flags;         /**< Bit-flags determining behavior of
                                  *   security operation functions. */
 };
src/openvpn/init.c +1 -0
index a785934a..dff090b1 100644
@@ -676,6 +676,7 @@ restore_ncp_options(struct context *c)
     c->options.ciphername = c->c1.ciphername;
     c->options.authname = c->c1.authname;
     c->options.keysize = c->c1.keysize;
+    c->options.data_channel_use_ekm = false;
 }
 
 void
src/openvpn/multi.c +4 -0
index 13738180..a5862020 100644
@@ -1817,6 +1817,10 @@ multi_client_set_protocol_options(struct context *c)
         c->c2.push_request_received = true;
     }
 
+#ifdef HAVE_EXPORT_KEYING_MATERIAL
+    o->data_channel_use_ekm = (proto & IV_PROTO_TLS_KEY_EXPORT);
+#endif
+
     /* Select cipher if client supports Negotiable Crypto Parameters */
     if (!o->ncp_enabled)
     {
src/openvpn/options.c +14 -0
index 8bf82c57..90e78a7b 100644
@@ -7947,6 +7947,20 @@ add_option(struct options *options,
         }
         options->ncp_ciphers = p[1];
     }
+    else if (streq(p[0], "key-derivation") && p[1])
+    {
+        VERIFY_PERMISSION(OPT_P_NCP)
+#ifdef HAVE_EXPORT_KEYING_MATERIAL
+        if (streq(p[1], "tls-ekm"))
+        {
+            options->data_channel_use_ekm = true;
+        }
+        else
+#endif
+        {
+            msg(msglevel, "Unknown key-derivation method %s", p[1]);
+        }
+    }
     else if (streq(p[0], "ncp-disable") && !p[1])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INSTANCE);
src/openvpn/options.h +3 -0
index 877e9396..c730c6a7 100644
@@ -648,6 +648,9 @@ struct options
     /* Useful when packets sent by openvpn itself are not subject
      * to the routing tables that would move packets into the tunnel. */
     bool allow_recursive_routing;
+
+    /* Use RFC 5705 key export */
+    bool data_channel_use_ekm;
 };
 
 #define streq(x, y) (!strcmp((x), (y)))
src/openvpn/push.c +4 -1
index e0d2eeaf..17bba948 100644
@@ -479,7 +479,10 @@ prepare_push_reply(struct context *c, struct gc_arena *gc,
     {
         push_option_fmt(gc, push_list, M_USAGE, "cipher %s", o->ciphername);
     }
-
+    if (o->data_channel_use_ekm)
+    {
+        push_option_fmt(gc, push_list, M_USAGE, "key-derivation tls-ekm");
+    }
     return true;
 }
 
src/openvpn/ssl.c +48 -6

98 lines not on this page · raw message

src/openvpn/ssl.h +2 -0
index 005628f6..f00f8abd 100644
@@ -116,6 +116,8 @@
  * to wait for a push-request to send a push-reply */
 #define IV_PROTO_REQUEST_PUSH   (1<<2)
 
+/** Supports key derivation via TLS key material exporter [RFC5705] */
+#define IV_PROTO_TLS_KEY_EXPORT (1<<3)
 
 /* Default field in X509 to be username */
 #define X509_USERNAME_FIELD_DEFAULT "CN"
src/openvpn/ssl_backend.h +2 -0
index cf9fba25..06ced86a 100644
@@ -389,6 +389,8 @@ void key_state_ssl_free(struct key_state_ssl *ks_ssl);
 void backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx,
                                 const char *crl_file, bool crl_inline);
 
+#define EXPORT_KEY_DATA_LABEL       "EXPORTER-OpenVPN-datakeys"
+#define EXPORT_KEY_DATA_EKM_SIZE    (2 * (MAX_CIPHER_KEY_LENGTH + MAX_HMAC_KEY_LENGTH))
 /**
  * Keying Material Exporters [RFC 5705] allows additional keying material to be
  * derived from existing TLS channel. This exported keying material can then be
src/openvpn/ssl_mbedtls.c +2 -5
index 4ec355a9..f375e957 100644
@@ -1168,11 +1168,8 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,
 
 #ifdef HAVE_EXPORT_KEYING_MATERIAL
     /* Initialize keying material exporter */
-    if (session->opt->ekm_size)
-    {
-        mbedtls_ssl_conf_export_keys_ext_cb(ks_ssl->ssl_config,
-                                            mbedtls_ssl_export_keys_cb, session);
-    }
+    mbedtls_ssl_conf_export_keys_ext_cb(ks_ssl->ssl_config,
+                                        mbedtls_ssl_export_keys_cb, session);
 #endif
 
     /* Initialise SSL context */
Signature · 1 line
2.26.2
#2 lore raw

Re: [Openvpn-devel] [PATCH v4 2/2] Implement generating data channel keys via EKM/RFC 5705

This sentence is badly constructed, so, use some grammar to help it read correctly:

Quoted 11 lines · Arne Schwabe

+ * When the client sends the IV_PROTO_TLS_KEY_EXPORT flag and the server replies
+ * with key-derivation tls-ekm the RFC5705 key material exporter with the
+ * label EXPORTER-OpenVPN-datakeys is used for the key data.
*

+ * When the client sends the IV_PROTO_TLS_KEY_EXPORT flag and the server replies
+ * with key-derivation tls-ekm *then* the RFC5705 key material exporter, with the
+ * label EXPORTER-OpenVPN-datakeys, is used for the key data.
*

Sorry to be a pain ..

#3 lore raw

PATCH 3/5 Allow running a default configuration with TLS libraries without BF-CBC

In reply to Arne Schwabe

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special case this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

---
 src/openvpn/init.c    | 18 +++++++++------
 src/openvpn/options.c | 51 ++++++++++++++++++++++++++++++++++++-------
 2 files changed, 54 insertions(+), 15 deletions(-)
src/openvpn/init.c +11 -7
index dff090b1..1e0baf2a 100644
@@ -2752,14 +2752,18 @@ do_init_crypto_tls_c1(struct context *c)
 #endif /* if P2MP */
         }
 
-        /* Do not warn if we only have BF-CBC in options->ciphername
-         * because it is still the default cipher */
-        bool warn = !streq(options->ciphername, "BF-CBC")
-             || options->enable_ncp_fallback;
-        /* Get cipher & hash algorithms */
-        init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
-                      options->keysize, true, warn);
 
+        if (!options->ncp_enabled || options->enable_ncp_fallback
+            || !streq(options->ciphername, "BF-CBC"))
+        {
+            /* Get cipher & hash algorithms
+             * skip BF-CBC for NCP setups when cipher as this is the default
+             * and is also special cased later to allow it to be not available
+             * as we need to construct a fake BF-CBC occ string
+             */
+            init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
+                          options->keysize, true, true);
+        }
         /* Initialize PRNG with config-specified digest */
         prng_init(options->prng_hash, options->prng_nonce_secret_len);
 
src/openvpn/options.c +43 -8

72 lines not on this page · raw message

Signature · 1 line
2.26.2
#4 lore raw

PATCH 4/5 Check return values in md_ctx_init and hmac_ctx_init

Without this OpenVPN will later segfault on a FIPS enabled system due to the algorithm available but not allowed.

---
 src/openvpn/crypto_openssl.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
src/openvpn/crypto_openssl.c +8 -2
index c60d4a54..75557cca 100644
@@ -954,7 +954,10 @@ md_ctx_init(EVP_MD_CTX *ctx, const EVP_MD *kt)
     ASSERT(NULL != ctx && NULL != kt);
 
     EVP_MD_CTX_init(ctx);
-    EVP_DigestInit(ctx, kt);
+    if (EVP_DigestInit(ctx, kt) != 1)
+    {
+        crypto_msg(M_FATAL, "EVP_DigestInit failed");
+    }
 }
 
 void
@@ -1011,7 +1014,10 @@ hmac_ctx_init(HMAC_CTX *ctx, const uint8_t *key, int key_len,
     ASSERT(NULL != kt && NULL != ctx);
 
     HMAC_CTX_reset(ctx);
-    HMAC_Init_ex(ctx, key, key_len, kt, NULL);
+    if  (HMAC_Init_ex(ctx, key, key_len, kt, NULL) != 1)
+    {
+        crypto_msg(M_FATAL, "HMAC_Init_ex failed");
+    }
 
     /* make sure we used a big enough key */
     ASSERT(HMAC_size(ctx) <= key_len);
Signature · 1 line
2.26.2
#9 lore raw

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

In reply to 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.

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

---
 src/openvpn/crypto_backend.h           |  19 +++
 src/openvpn/crypto_mbedtls.c           | 143 ++++++++++++++++++++
 src/openvpn/crypto_openssl.c           | 172 ++++++++++++++++++++++++-
 src/openvpn/ssl.c                      | 130 +------------------
 tests/unit_tests/openvpn/test_crypto.c |  32 +++++
 5 files changed, 366 insertions(+), 130 deletions(-)
src/openvpn/crypto_backend.h +19 -0
index 85cb084a..b72684ce 100644
@@ -699,4 +699,23 @@ 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
+ * fun ction 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
+ */
+void
+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 +143 -0

153 lines not on this page · raw message

src/openvpn/crypto_openssl.c +171 -1

182 lines not on this page · raw message

src/openvpn/ssl.c +1 -129

142 lines not on this page · raw message

tests/unit_tests/openvpn/test_crypto.c +32 -0
index ea9b99b2..6c68099e 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.26.2
Acked-by on v4 1/2
#27 lore raw
In reply to Arne Schwabe

Hi,

Quoted 8 lines · Arne Schwabe

This moves the OpenVPN specific PRF into its own function also simplifies the code a bit by passing tls_session directly instead of 5 of its fields.

Patch v2: Rebase

Patch v4: rewrite/fix comments,
fix potential not initialised before goto issue

Thanks. All my previous concerns were tackled accordingly.

Quoted 33 lines
---
 src/openvpn/ssl.c | 119 ++++++++++++++++++++++++++++++----------------
 1 file changed, 79 insertions(+), 40 deletions(-)
src/openvpn/ssl.c +9 -11
index 3fcaa25f..b4d94b8a 100644
@@ -1765,27 +1765,37 @@ openvpn_PRF(const uint8_t *secret,
     VALGRIND_MAKE_READABLE((void *)output, output_len);
 }
 
-/*
- * Using source entropy from local and remote hosts, mix into
- * master key.
- */
-static bool
-generate_key_expansion(struct key_ctx_bi *key,
-                       const struct key_type *key_type,
-                       const struct key_source2 *key_src,
-                       const struct session_id *client_sid,
-                       const struct session_id *server_sid,
-                       bool server)
+static void
+init_key_contexts(struct key_ctx_bi *key,
+                  const struct key_type *key_type,
+                  bool server,
+                  struct key2 *key2)
+{
+    /* Initialize key contexts */
+    int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL;
+    init_key_ctx_bi(key, key2, key_direction, key_type, "Data Channel");

src/openvpn/ssl.c openvpn_PRF(const uint8_t *secret, · line 1786

Quoted 10 lines
src/openvpn/ssl.c +10 -0
+
+    /* Initialize implicit IVs */
+    key_ctx_update_implicit_iv(&key->encrypt, (*key2).keys[(int)server].hmac,
+                               MAX_HMAC_KEY_LENGTH);
+    key_ctx_update_implicit_iv(&key->decrypt, (*key2).keys[1-(int)server].hmac,
+                               MAX_HMAC_KEY_LENGTH);
+
+}
+
+

One newline too much.

Quoted 106 lines
src/openvpn/ssl.c +56 -21
+static struct key2
+generate_key_expansion_openvpn_prf(const struct tls_session *session)
 {
     uint8_t master[48] = { 0 };
-    struct key2 key2 = { 0 };
-    bool ret = false;
 
-    if (key->initialized)
-    {
-        msg(D_TLS_ERRORS, "TLS Error: key already initialized");
-        goto exit;
-    }
+    const struct key_state *ks = &session->key[KS_PRIMARY];
+    const struct key_source2 *key_src = ks->key_src;
+
+    const struct session_id *client_sid = session->opt->server ?
+                                          &ks->session_id_remote : &session->session_id;
+    const struct session_id *server_sid = !session->opt->server ?
+                                          &ks->session_id_remote : &session->session_id;
 
     /* debugging print of source key material */
     key_source2_print(key_src);
@@ -1803,6 +1813,7 @@ generate_key_expansion(struct key_ctx_bi *key,
                 master,
                 sizeof(master));
 
+    struct key2 key2;
     /* compute key expansion */
     openvpn_PRF(master,
                 sizeof(master),
@@ -1815,41 +1826,73 @@ generate_key_expansion(struct key_ctx_bi *key,
                 server_sid,
                 (uint8_t *)key2.keys,
                 sizeof(key2.keys));
+    secure_memzero(&master, sizeof(master));
 
+    /*
+     * fixup_key only correctly sets DES parity bits if the cipher is a
+     * DES variant.
+     *
+     * The newer OpenSSL and mbed TLS libraries (those that support EKM)
+     * ignore these bits.
+     *
+     * We keep the DES fixup here as compatibility.
+     * OpenVPN3 never did this fixup anyway. So this code is *probably* not
+     * required but we keep it for compatibility until we remove DES support
+     * since it does not hurt either.
+     */
+    for (int i = 0; i < 2; ++i)
+    {
+        fixup_key(&key2.keys[i], &session->opt->key_type);
+    }
     key2.n = 2;
 
-    key2_print(&key2, key_type, "Master Encrypt", "Master Decrypt");
+    return key2;
+}
+
+/*
+ * Using source entropy from local and remote hosts, mix into
+ * master key.
+ */
+static bool
+generate_key_expansion(struct key_ctx_bi *key,
+                       const struct tls_session *session)
+{
+    bool ret = false;
+    struct key2 key2;
+
+    if (key->initialized)
+    {
+        msg(D_TLS_ERRORS, "TLS Error: key already initialized");
+        goto exit;
+    }
+
+
+    bool server = session->opt->server;
+
+    key2 = generate_key_expansion_openvpn_prf(session);
+
+    key2_print(&key2, &session->opt->key_type,
+               "Master Encrypt", "Master Decrypt");
 
     /* check for weak keys */
     for (int i = 0; i < 2; ++i)
     {
-        fixup_key(&key2.keys[i], key_type);
-        if (!check_key(&key2.keys[i], key_type))
+        if (!check_key(&key2.keys[i], &session->opt->key_type))
         {
             msg(D_TLS_ERRORS, "TLS Error: Bad dynamic key generated");
             goto exit;
         }
     }
-
-    /* Initialize OpenSSL key contexts */
-    int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL;
-    init_key_ctx_bi(key, &key2, key_direction, key_type, "Data Channel");
-
-    /* Initialize implicit IVs */
-    key_ctx_update_implicit_iv(&key->encrypt, key2.keys[(int)server].hmac,
-                               MAX_HMAC_KEY_LENGTH);
-    key_ctx_update_implicit_iv(&key->decrypt, key2.keys[1-(int)server].hmac,
-                               MAX_HMAC_KEY_LENGTH);
-
+    init_key_contexts(key, &session->opt->key_type, server, &key2);

src/openvpn/ssl.c generate_key_expansion(struct key_ctx_bi *key, · line 1895

Quoted 10 lines
src/openvpn/ssl.c +1 -1
     ret = true;
 
 exit:
-    secure_memzero(&master, sizeof(master));
     secure_memzero(&key2, sizeof(key2));
 
     return ret;
 }
 
+

Stray newline.

Quoted 16 lines
src/openvpn/ssl.c +1 -4
 static void
 key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len)
 {
@@ -1879,10 +1922,7 @@ tls_session_generate_data_channel_keys(struct tls_session *session)
 {
     bool ret = false;
     struct key_state *ks = &session->key[KS_PRIMARY];   /* primary key */
-    const struct session_id *client_sid = session->opt->server ?
-                                          &ks->session_id_remote : &session->session_id;
-    const struct session_id *server_sid = !session->opt->server ?
-                                          &ks->session_id_remote : &session->session_id;
+
 
     if (ks->authenticated == KS_AUTH_FALSE)
     {

src/openvpn/ssl.c tls_session_generate_data_channel_keys(struct tls_session *session) · line 1935

Quoted 8 lines
src/openvpn/ssl.c +2 -3
@@ -1891,9 +1931,8 @@ tls_session_generate_data_channel_keys(struct tls_session *session)
     }
 
     ks->crypto_options.flags = session->opt->crypto_flags;
-    if (!generate_key_expansion(&ks->crypto_options.key_ctx_bi,
-                                &session->opt->key_type, ks->key_src, client_sid, server_sid,
-                                session->opt->server))
+
+    if (!generate_key_expansion(&ks->crypto_options.key_ctx_bi, session))

Since you're passing session now, shouldn't you remove the key_ctx_bi argument too? That's also part of session: (struct key_state) *ks = &session->key[KS_PRIMARY].

src/openvpn/ssl.c tls_session_generate_data_channel_keys(struct tls_session *session) · line 1938

Quoted 4 lines
src/openvpn/ssl.c +0 -0
     {
         msg(D_TLS_ERRORS, "TLS Error: generate_key_expansion failed");
         goto cleanup;

Otherwise this looks good. I can also live with leaving key_ctx_bi in for now. The current version of the patch is a clear improvement (and paving the way for 2/2, ofc.)

So, as long as at least the whitespace is fixed:

-Steffan

#28 lore raw
In reply to Arne Schwabe

Your patch has been applied to the master branch.

(Since this is only needed for the new PRF functionality and it's very unlikely that we'll see bugfixes for "master *and* 2.5" for this code in future, the refactoring is also not needed in 2.5)

Whitespace has been fixed :-) (we really need T-Shirts with "the whitespace dragon made me do this" on them...)

Tested on the client side against an umodified server, works fine.

commit 15d0524327a10c2999f37375688196f8452f1029

Author: Arne Schwabe
Date:   Tue Aug 25 09:36:42 2020 +0200
     Move openvpn specific key expansion into its own function

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

--
kind regards,

Gert Doering

#25 lore raw

Re: [Openvpn-devel] [PATCH v4 2/2] Implement generating data channel keys via EKM/RFC 5705

In reply to Arne Schwabe

Hi,

Quoted 8 lines · Arne Schwabe

OpenVPN currently uses its own (based on TLS 1.0) key derivation mechanism to generate the 256 bytes key data in key2 struct that are then used used to generate encryption/hmac/iv vectors. While this mechanism is still secure, it is not state of the art.

Instead of modernising our own approach, this commit implements key derivation using the Keying Material Exporters API introduced by RFC 5705.

Thanks! I'm glad to see you're picking up the work I haven't been able to find time for.

Quoted 11 lines

We also use an opportunistic approach of negotiating the use of EKM (exported key material) through an IV_PROTO flag and prefer EKM to our own PRF if both client and server support it. The use of EKM is pushed to the client as part of NCP as key-derivation tls-ekm.

We still exchange the random data (112 bytes from client to server and 64 byte from server to client) for the OpenVPN PRF but do not use it. Removing that exchange would break the handshake and make a key-method 3 or similar necessary.

Quoted 8 lines

As a side effect, this makes a little bit easier to have a FIPS compatible version of OpenVPN since we do not rely on calling MD5 anymore.

Side note: this commit breaks the (not yet merged) WolfSSL support as it claims to support EKM in the OpenSSL compat API but always returns an error if you try to use it.

Signed off two times.

Quoted 58 lines

Patch v2: rebase/change to V2 of EKM refactoring

Patch v3: add Changes.rst

---
 Changes.rst                      | 11 +++++++
 doc/doxygen/doc_key_generation.h | 14 +++++++--
 src/openvpn/crypto.h             |  4 +++
 src/openvpn/init.c               |  1 +
 src/openvpn/multi.c              |  4 +++
 src/openvpn/options.c            | 14 +++++++++
 src/openvpn/options.h            |  3 ++
 src/openvpn/push.c               |  5 ++-
 src/openvpn/ssl.c                | 54 ++++++++++++++++++++++++++++----
 src/openvpn/ssl.h                |  2 ++
 src/openvpn/ssl_backend.h        |  2 ++
 src/openvpn/ssl_mbedtls.c        |  7 ++---
 12 files changed, 107 insertions(+), 14 deletions(-)
Changes.rst +11 -0
index f67e1d76..2a2829e7 100644
@@ -1,3 +1,14 @@
+Overview of changes in 2.6
+==========================
+
+
+New features
+------------
+Keying Material Exporters (RFC 5705) based key generation
+    As part of the cipher negotiation OpenVPN will automatically prefer
+    the RFC5705 based key material generation to the current custom
+    OpenVPN PRF. This feature requires OpenSSL or mbed TLS 2.18+.
+
 Overview of changes in 2.5
 ==========================
 
doc/doxygen/doc_key_generation.h +6 -0
index 4bb9c708..cef773a9 100644
@@ -58,6 +58,12 @@
  *
  * @subsection key_generation_method_2 Key method 2
  *
+ * There are two methods for generating key data when using key method 2
+ * the first is OpenVPN's traditional approach that exchanges random
+ * data and uses a PRF and the other is using the RFC5705 keying material
+ * exporter to generate the key material. For both methods the random
+ * data is exchange but only used in the traditional method.
+ *
  * -# The client generates random material in the following amounts:
  *    - Pre-master secret: 48 bytes
  *    - Client's PRF seed for master secret: 32 bytes
@@ -73,8 +79,12 @@
  *    server's random material.

doc/doxygen/doc_key_generation.h line 87

Quoted 10 lines
doc/doxygen/doc_key_generation.h +6 -2
  *
  * %Key method 2 %key expansion is performed by the \c
- * generate_key_expansion() function.  Please refer to its source code for
- * details of the %key expansion process.
+ * generate_key_expansion_openvpn_prf() function.  Please refer to its source
+ * code for details of the %key expansion process.
+ *
+ * When the client sends the IV_PROTO_TLS_KEY_EXPORT flag and the server replies
+ * with `key-derivation tls-ekm` the RFC5705 key material exporter with the
+ * label EXPORTER-OpenVPN-datakeys is used for the key data.

Did you request this label with IANA?

Also, a user could now use --keying-material-exporter to export the data channel keys to a plugin. I'm inclined to say we should prevent that, by rejecting our internal label as a user label.

src/openvpn/crypto.h struct crypto_options · line 258

Quoted 12 lines
doc/doxygen/doc_key_generation.h +0 -0
  * @subsection key_generation_random Source of random material
  *
src/openvpn/crypto.h +2 -0
index 999f643e..ec935ca5 100644
@@ -254,6 +254,10 @@ struct crypto_options
 #define CO_MUTE_REPLAY_WARNINGS (1<<2)
     /**< Bit-flag indicating not to display
      *   replay warnings. */
+#define CO_USE_TLS_KEY_MATERIAL_EXPORT  (1<<3)
+    /**< Bit-flag indicating that key derivation

Sugestion: "data channel key derivation".

Quoted 44 lines
src/openvpn/crypto.h +2 -0
+     * is done using TLS keying material export [RFC5705]
+     */
     unsigned int flags;         /**< Bit-flags determining behavior of
                                  *   security operation functions. */
 };
src/openvpn/init.c +1 -0
index a785934a..dff090b1 100644
@@ -676,6 +676,7 @@ restore_ncp_options(struct context *c)
     c->options.ciphername = c->c1.ciphername;
     c->options.authname = c->c1.authname;
     c->options.keysize = c->c1.keysize;
+    c->options.data_channel_use_ekm = false;
 }
 
 void
src/openvpn/multi.c +4 -0
index 13738180..a5862020 100644
@@ -1817,6 +1817,10 @@ multi_client_set_protocol_options(struct context *c)
         c->c2.push_request_received = true;
     }
 
+#ifdef HAVE_EXPORT_KEYING_MATERIAL
+    o->data_channel_use_ekm = (proto & IV_PROTO_TLS_KEY_EXPORT);
+#endif
+
     /* Select cipher if client supports Negotiable Crypto Parameters */
     if (!o->ncp_enabled)
     {
src/openvpn/options.c +4 -0
index 8bf82c57..90e78a7b 100644
@@ -7947,6 +7947,20 @@ add_option(struct options *options,
         }
         options->ncp_ciphers = p[1];
     }
+    else if (streq(p[0], "key-derivation") && p[1])
+    {
+        VERIFY_PERMISSION(OPT_P_NCP)
+#ifdef HAVE_EXPORT_KEYING_MATERIAL

src/openvpn/options.c add_option(struct options *options, · line 7963

Quoted 10 lines
src/openvpn/options.c +10 -0
+        if (streq(p[1], "tls-ekm"))
+        {
+            options->data_channel_use_ekm = true;
+        }
+        else
+#endif
+        {
+            msg(msglevel, "Unknown key-derivation method %s", p[1]);
+        }
+    }

This option is also accepted on the command line or config file, but is not documented in the man page or --help text. It would probably be best to not accept is on the command line or config file, but I'm not sure we have the infra in place for that.

src/openvpn/options.h struct options · line 652

Quoted 13 lines
src/openvpn/options.c +0 -0
     else if (streq(p[0], "ncp-disable") && !p[1])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INSTANCE);
src/openvpn/options.h +2 -0
index 877e9396..c730c6a7 100644
@@ -648,6 +648,9 @@ struct options
     /* Useful when packets sent by openvpn itself are not subject
      * to the routing tables that would move packets into the tunnel. */
     bool allow_recursive_routing;
+
+    /* Use RFC 5705 key export */

Suggestion: "Use RFC5705 key export to generate data channel keys".

Quoted 28 lines
src/openvpn/options.h +1 -0
+    bool data_channel_use_ekm;
 };
 
 #define streq(x, y) (!strcmp((x), (y)))
src/openvpn/push.c +4 -1
index e0d2eeaf..17bba948 100644
@@ -479,7 +479,10 @@ prepare_push_reply(struct context *c, struct gc_arena *gc,
     {
         push_option_fmt(gc, push_list, M_USAGE, "cipher %s", o->ciphername);
     }
-
+    if (o->data_channel_use_ekm)
+    {
+        push_option_fmt(gc, push_list, M_USAGE, "key-derivation tls-ekm");
+    }
     return true;
 }
 
src/openvpn/ssl.c +0 -0
index b4d94b8a..9643fbdd 100644
@@ -1784,6 +1784,29 @@ init_key_contexts(struct key_ctx_bi *key,
 }
 
 

src/openvpn/ssl.c init_key_contexts(struct key_ctx_bi *key, · line 1796

Quoted 10 lines
src/openvpn/ssl.c +10 -0
+static bool
+generate_key_expansion_tls_export(struct tls_session *session, struct key2 *key2)
+{
+    struct gc_arena gc = gc_new();
+    unsigned char *key2data;
+
+    key2data = key_state_export_keying_material(session,
+                                                EXPORT_KEY_DATA_LABEL,
+                                                strlen(EXPORT_KEY_DATA_LABEL),
+                                                EXPORT_KEY_DATA_EKM_SIZE,

Why not sizeof(key2->keys)? That makes it trivial to verify correctness (just read this function) and would make EXPORT_KEY_DATA_EKM_SIZE unneeded.

Quoted 26 lines
src/openvpn/ssl.c +14 -1
+                                                &gc);
+    if (!key2data)
+    {
+        return false;
+    }
+    memcpy(key2->keys, key2data, sizeof(key2->keys));
+    secure_memzero(key2data, sizeof(key2->keys));
+    key2->n = 2;
+
+    gc_free(&gc);
+    return true;
+}
+
 static struct key2
 generate_key_expansion_openvpn_prf(const struct tls_session *session)
 {
@@ -1855,7 +1878,7 @@ generate_key_expansion_openvpn_prf(const struct tls_session *session)
  */
 static bool
 generate_key_expansion(struct key_ctx_bi *key,
-                       const struct tls_session *session)
+                       struct tls_session *session)
 {
     bool ret = false;
     struct key2 key2;

src/openvpn/ssl.c generate_key_expansion(struct key_ctx_bi *key, · line 1896

Quoted 10 lines
src/openvpn/ssl.c +3 -2
@@ -1866,10 +1889,20 @@ generate_key_expansion(struct key_ctx_bi *key,
         goto exit;
     }
 
-
     bool server = session->opt->server;
 
-    key2 = generate_key_expansion_openvpn_prf(session);
+    if (session->opt->crypto_flags & CO_USE_TLS_KEY_MATERIAL_EXPORT)
+    {
+        if(!generate_key_expansion_tls_export(session, &key2))

Missing space between if and (.

Quoted 72 lines
src/openvpn/ssl.c +21 -3
+        {
+            msg(D_TLS_ERRORS, "TLS Error: Keying material export failed");
+            goto exit;
+        }
+    }
+    else
+    {
+        key2 = generate_key_expansion_openvpn_prf(session);
+    }
 
     key2_print(&key2, &session->opt->key_type,
                "Master Encrypt", "Master Decrypt");
@@ -1970,6 +2003,11 @@ tls_session_update_crypto_params(struct tls_session *session,
         return false;
     }
 
+    if (options->data_channel_use_ekm)
+    {
+        session->opt->crypto_flags |= CO_USE_TLS_KEY_MATERIAL_EXPORT;
+    }
+
     if (strcmp(options->ciphername, session->opt->config_ciphername))
     {
         msg(D_HANDSHAKE, "Data Channel: using negotiated cipher '%s'",
@@ -2254,13 +2292,11 @@ push_peer_info(struct buffer *buf, struct tls_session *session)
          * push request, also signal that the client wants
          * to get push-reply messages without without requiring a round
          * trip for a push request message*/
-        if(session->opt->pull)
+        if (session->opt->pull)
         {
             iv_proto |= IV_PROTO_REQUEST_PUSH;
         }
 
-        buf_printf(&out, "IV_PROTO=%d\n", iv_proto);
-
         /* support for Negotiable Crypto Parameters */
         if (session->opt->ncp_enabled
             && (session->opt->mode == MODE_SERVER || session->opt->pull))
@@ -2272,8 +2308,14 @@ push_peer_info(struct buffer *buf, struct tls_session *session)
                 buf_printf(&out, "IV_NCP=2\n");
             }
             buf_printf(&out, "IV_CIPHERS=%s\n", session->opt->config_ncp_ciphers);
+
+#ifdef HAVE_EXPORT_KEYING_MATERIAL
+            iv_proto |= IV_PROTO_TLS_KEY_EXPORT;
+#endif
         }
 
+        buf_printf(&out, "IV_PROTO=%d\n", iv_proto);
+
         /* push compression status */
 #ifdef USE_COMP
         comp_generate_peer_info_string(&session->opt->comp_options, &out);
src/openvpn/ssl.h +2 -0
index 005628f6..f00f8abd 100644
@@ -116,6 +116,8 @@
  * to wait for a push-request to send a push-reply */
 #define IV_PROTO_REQUEST_PUSH   (1<<2)
 
+/** Supports key derivation via TLS key material exporter [RFC5705] */
+#define IV_PROTO_TLS_KEY_EXPORT (1<<3)
 
 /* Default field in X509 to be username */
 #define X509_USERNAME_FIELD_DEFAULT "CN"
src/openvpn/ssl_backend.h +0 -0
index cf9fba25..06ced86a 100644

src/openvpn/ssl_backend.h void key_state_ssl_free(struct key_state_ssl *ks_ssl); · line 393

Quoted 5 lines
src/openvpn/ssl_backend.h +2 -0
@@ -389,6 +389,8 @@ void key_state_ssl_free(struct key_state_ssl *ks_ssl);
 void backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx,
                                 const char *crl_file, bool crl_inline);
 
+#define EXPORT_KEY_DATA_LABEL       "EXPORTER-OpenVPN-datakeys"
+#define EXPORT_KEY_DATA_EKM_SIZE    (2 * (MAX_CIPHER_KEY_LENGTH + MAX_HMAC_KEY_LENGTH))

This is probably not needed (see above). Otherwise, I'd like to see a (static) assert somewhere that this indeed matches the size we're going to memcpy() into the keys struct.

Quoted 12 lines
src/openvpn/ssl_backend.h +0 -0
 /**
  * Keying Material Exporters [RFC 5705] allows additional keying material to be
  * derived from existing TLS channel. This exported keying material can then be
src/openvpn/ssl_mbedtls.c +0 -0
index 4ec355a9..f375e957 100644
@@ -1168,11 +1168,8 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,
 
 #ifdef HAVE_EXPORT_KEYING_MATERIAL
     /* Initialize keying material exporter */

src/openvpn/ssl_mbedtls.c key_state_ssl_init(struct key_state_ssl *ks_ssl, · line 1175

Quoted 10 lines
src/openvpn/ssl_mbedtls.c +2 -5
-    if (session->opt->ekm_size)
-    {
-        mbedtls_ssl_conf_export_keys_ext_cb(ks_ssl->ssl_config,
-                                            mbedtls_ssl_export_keys_cb, session);
-    }
+    mbedtls_ssl_conf_export_keys_ext_cb(ks_ssl->ssl_config,
+                                        mbedtls_ssl_export_keys_cb, session);
 #endif
 
     /* Initialise SSL context */

So, looks good in general, but needs a bit more polishing :)

-Steffan

#26 lore raw

Re: [Openvpn-devel] [PATCH v4 2/2] Implement generating data channel keys via EKM/RFC 5705

Am 09.10.20 um 11:23 schrieb Steffan Karger:

Quoted 99 lines

Hi,

Quoted 8 lines · Arne Schwabe

OpenVPN currently uses its own (based on TLS 1.0) key derivation mechanism to generate the 256 bytes key data in key2 struct that are then used used to generate encryption/hmac/iv vectors. While this mechanism is still secure, it is not state of the art.

Instead of modernising our own approach, this commit implements key derivation using the Keying Material Exporters API introduced by RFC 5705.

Thanks! I'm glad to see you're picking up the work I haven't been able to find time for.

Quoted 11 lines

We also use an opportunistic approach of negotiating the use of EKM (exported key material) through an IV_PROTO flag and prefer EKM to our own PRF if both client and server support it. The use of EKM is pushed to the client as part of NCP as key-derivation tls-ekm.

We still exchange the random data (112 bytes from client to server and 64 byte from server to client) for the OpenVPN PRF but do not use it. Removing that exchange would break the handshake and make a key-method 3 or similar necessary.

Quoted 8 lines

As a side effect, this makes a little bit easier to have a FIPS compatible version of OpenVPN since we do not rely on calling MD5 anymore.

Side note: this commit breaks the (not yet merged) WolfSSL support as it claims to support EKM in the OpenSSL compat API but always returns an error if you try to use it.

Signed off two times.

Quoted 62 lines

Patch v2: rebase/change to V2 of EKM refactoring

Patch v3: add Changes.rst

---
 Changes.rst                      | 11 +++++++
 doc/doxygen/doc_key_generation.h | 14 +++++++--
 src/openvpn/crypto.h             |  4 +++
 src/openvpn/init.c               |  1 +
 src/openvpn/multi.c              |  4 +++
 src/openvpn/options.c            | 14 +++++++++
 src/openvpn/options.h            |  3 ++
 src/openvpn/push.c               |  5 ++-
 src/openvpn/ssl.c                | 54 ++++++++++++++++++++++++++++----
 src/openvpn/ssl.h                |  2 ++
 src/openvpn/ssl_backend.h        |  2 ++
 src/openvpn/ssl_mbedtls.c        |  7 ++---
 12 files changed, 107 insertions(+), 14 deletions(-)
Changes.rst +11 -0
index f67e1d76..2a2829e7 100644
@@ -1,3 +1,14 @@
+Overview of changes in 2.6
+==========================
+
+
+New features
+------------
+Keying Material Exporters (RFC 5705) based key generation
+    As part of the cipher negotiation OpenVPN will automatically prefer
+    the RFC5705 based key material generation to the current custom
+    OpenVPN PRF. This feature requires OpenSSL or mbed TLS 2.18+.
+
 Overview of changes in 2.5
 ==========================
 
doc/doxygen/doc_key_generation.h +6 -2
index 4bb9c708..cef773a9 100644
@@ -58,6 +58,12 @@
  *
  * @subsection key_generation_method_2 Key method 2
  *
+ * There are two methods for generating key data when using key method 2
+ * the first is OpenVPN's traditional approach that exchanges random
+ * data and uses a PRF and the other is using the RFC5705 keying material
+ * exporter to generate the key material. For both methods the random
+ * data is exchange but only used in the traditional method.
+ *
  * -# The client generates random material in the following amounts:
  *    - Pre-master secret: 48 bytes
  *    - Client's PRF seed for master secret: 32 bytes
@@ -73,8 +79,12 @@
  *    server's random material.
  *
  * %Key method 2 %key expansion is performed by the \c
- * generate_key_expansion() function.  Please refer to its source code for
- * details of the %key expansion process.
Quoted 8 lines

doc/doxygen/doc_key_generation.h line 87

Quoted 6 lines
doc/doxygen/doc_key_generation.h +6 -0
+ * generate_key_expansion_openvpn_prf() function.  Please refer to its source
+ * code for details of the %key expansion process.
+ *
+ * When the client sends the IV_PROTO_TLS_KEY_EXPORT flag and the server replies
+ * with `key-derivation tls-ekm` the RFC5705 key material exporter with the
+ * label EXPORTER-OpenVPN-datakeys is used for the key data.

Did you request this label with IANA?

It is not really described well how this actually works, from what I interpreted you need to write to the mailing list, which I did:
https://mailarchive.ietf.org/arch/msg/tls/IWFb5hLsqNlCcIMnEDDuPAkzXvg/

but I am lost what the next step would be.

Quoted 74 lines

Also, a user could now use --keying-material-exporter to export the data channel keys to a plugin. I'm inclined to say we should prevent that, by rejecting our internal label as a user label.

src/openvpn/crypto.h struct crypto_options · line 258

Quoted 12 lines
doc/doxygen/doc_key_generation.h +0 -0
  * @subsection key_generation_random Source of random material
  *
src/openvpn/crypto.h +2 -0
index 999f643e..ec935ca5 100644
@@ -254,6 +254,10 @@ struct crypto_options
 #define CO_MUTE_REPLAY_WARNINGS (1<<2)
     /**< Bit-flag indicating not to display
      *   replay warnings. */
+#define CO_USE_TLS_KEY_MATERIAL_EXPORT  (1<<3)
+    /**< Bit-flag indicating that key derivation

Sugestion: "data channel key derivation".

Quoted 51 lines
src/openvpn/crypto.h +2 -0
+     * is done using TLS keying material export [RFC5705]
+     */
     unsigned int flags;         /**< Bit-flags determining behavior of
                                  *   security operation functions. */
 };
src/openvpn/init.c +1 -0
index a785934a..dff090b1 100644
@@ -676,6 +676,7 @@ restore_ncp_options(struct context *c)
     c->options.ciphername = c->c1.ciphername;
     c->options.authname = c->c1.authname;
     c->options.keysize = c->c1.keysize;
+    c->options.data_channel_use_ekm = false;
 }
 
 void
src/openvpn/multi.c +4 -0
index 13738180..a5862020 100644
@@ -1817,6 +1817,10 @@ multi_client_set_protocol_options(struct context *c)
         c->c2.push_request_received = true;
     }
 
+#ifdef HAVE_EXPORT_KEYING_MATERIAL
+    o->data_channel_use_ekm = (proto & IV_PROTO_TLS_KEY_EXPORT);
+#endif
+
     /* Select cipher if client supports Negotiable Crypto Parameters */
     if (!o->ncp_enabled)
     {
src/openvpn/options.c +11 -0
index 8bf82c57..90e78a7b 100644
@@ -7947,6 +7947,20 @@ add_option(struct options *options,
         }
         options->ncp_ciphers = p[1];
     }
+    else if (streq(p[0], "key-derivation") && p[1])
+    {
+        VERIFY_PERMISSION(OPT_P_NCP)
+#ifdef HAVE_EXPORT_KEYING_MATERIAL
+        if (streq(p[1], "tls-ekm"))
+        {
+            options->data_channel_use_ekm = true;
+        }
+        else
+#endif
+        {
Quoted 8 lines

src/openvpn/options.c add_option(struct options *options, · line 7963

Quoted 3 lines
src/openvpn/options.c +3 -0
+            msg(msglevel, "Unknown key-derivation method %s", p[1]);
+        }
+    }

This option is also accepted on the command line or config file, but is not documented in the man page or --help text. It would probably be best to not accept is on the command line or config file, but I'm not sure we have the infra in place for that.

Sadly, we don't. Other options like peer-id or echo are also accepted from command line or config. Or we have it and nobody is aware of how to use it.

Quoted 3 lines

So, looks good in general, but needs a bit more polishing :)

I will do the polishing and send a new version :)

Thanks for reviewing.

Arne

#12 lore raw

Re: [Openvpn-devel] [PATCH 3/5] Allow running a default configuration with TLS libraries without BF-CBC

In reply to Arne Schwabe

Am 07.09.20 um 18:22 schrieb Arne Schwabe:

Quoted 1 line

Modern TLS libraries might drop Blowfish by default or distributions

OpenSSL 3.0 will be one of these that only provides Blowfish if you load the legacy provider (not included by default)

#13 lore raw

Re: [Openvpn-devel] [PATCH 3/5] Allow running a default configuration with TLS libraries without BF-CBC

In reply to Arne Schwabe

Hi,

Quoted 26 lines · Arne Schwabe

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special case this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

---
 src/openvpn/init.c    | 18 +++++++++------
 src/openvpn/options.c | 51 ++++++++++++++++++++++++++++++++++++-------
 2 files changed, 54 insertions(+), 15 deletions(-)
src/openvpn/init.c +0 -6
index dff090b1..1e0baf2a 100644
@@ -2752,14 +2752,18 @@ do_init_crypto_tls_c1(struct context *c)
 #endif /* if P2MP */
         }
 
-        /* Do not warn if we only have BF-CBC in options->ciphername
-         * because it is still the default cipher */
-        bool warn = !streq(options->ciphername, "BF-CBC")
-             || options->enable_ncp_fallback;
-        /* Get cipher & hash algorithms */
-        init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,

src/openvpn/init.c do_init_crypto_tls_c1(struct context *c) · line 2763

Quoted 10 lines
src/openvpn/init.c +8 -1
-                      options->keysize, true, warn);
 
+        if (!options->ncp_enabled || options->enable_ncp_fallback
+            || !streq(options->ciphername, "BF-CBC"))
+        {
+            /* Get cipher & hash algorithms
+             * skip BF-CBC for NCP setups when cipher as this is the default
+             * and is also special cased later to allow it to be not available
+             * as we need to construct a fake BF-CBC occ string
+             */

After our discussion I believe I understood what this part is about.

However, I think the comment could be made a bit more explicit. I would like to propose the following:

/*
* BF-CBC is allowed to be used only when explicitly configured
* as NCP-fallback or when NCP has been disabled.
* In all other cases don't attempt to initialize BF-CBC as it
* may not even be supported by the underlying SSL library. *
* Therefore, the key structure has to be initialized when:
* - any non-BF-CBC cipher was selected; or
* - BF-CBC is selected and NCP is disabled (explicit request to
* use the BF-CBC cipher); or
* - BF-CBC is selected, NCP is enabled and fallback is enabled
* (BF-CBC will be the fallback).
*
* Note that BF-CBC will still be part of the OCC string to retain
* backwards compatibility with older clients.
*/

This comment should be placed above the if-block.

At the same time I would like to propose a switch within the if-block conditions as follows:

if (!streq(options->ciphername, "BF-CBC") || !options->ncp_enabled

    || options->enable_ncp_fallback)

src/openvpn/init.c do_init_crypto_tls_c1(struct context *c) · line 2766

Quoted 3 lines
src/openvpn/init.c +3 -0
+            init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
+                          options->keysize, true, true);
+        }

Why do you always want to warn the user in this context?
By passing warn=true all the time (last argument) we will have openvpn always warning the user about "weak cipher selected", but ciphername could be anything at this point.

Quoted 12 lines
src/openvpn/init.c +0 -0
         /* Initialize PRNG with config-specified digest */
         prng_init(options->prng_hash, options->prng_nonce_secret_len);
 
src/openvpn/options.c +0 -1
index 90e78a7b..01da88ad 100644
@@ -3640,11 +3640,32 @@ calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
     {
         struct frame fake_frame = *frame;
         struct key_type fake_kt;
-        init_key_type(&fake_kt, o->ciphername, o->authname, o->keysize, true,

src/openvpn/options.c calc_options_string_link_mtu(const struct options *o, const struct frame *frame) · line 3649

Quoted 10 lines
src/openvpn/options.c +6 -3
-                      false);
+
         frame_remove_from_extra_frame(&fake_frame, crypto_max_overhead());
-        crypto_adjust_frame_parameters(&fake_frame, &fake_kt, o->replay,
-                                       cipher_kt_mode_ofb_cfb(fake_kt.cipher));
+
+        /* o->ciphername can be still BF-CBC and our SSL library might not like
+         * like it, workaround this important corner case in the name of
+         * compatibility and not stopping openvpn on our default configuration
+         */

I would rephrase a bit this comment to make it more explicit for the casual reader. See below.

Quoted 9 lines
src/openvpn/options.c +9 -0
+        if ((strcmp(o->ciphername, "BF-CBC") == 0)
+            && cipher_kt_get(o->ciphername) == NULL)
+        {
+            init_key_type(&fake_kt, "none", o->authname, o->keysize, true,
+                          false);
+
+            crypto_adjust_frame_parameters(&fake_frame, &fake_kt, o->replay,
+                                           cipher_kt_mode_ofb_cfb(fake_kt.cipher));
+            /* 64 bit block size, 64 bit IV size */

src/openvpn/options.c calc_options_string_link_mtu(const struct options *o, const struct frame *frame) · line 3668

Quoted 10 lines
src/openvpn/options.c +10 -0
+            frame_add_to_extra_frame(&fake_frame, 64/8 + 64/8);
+        }
+        else
+        {
+            init_key_type(&fake_kt, o->ciphername, o->authname, o->keysize, true,
+                          false);
+
+            crypto_adjust_frame_parameters(&fake_frame, &fake_kt, o->replay,
+                                           cipher_kt_mode_ofb_cfb(fake_kt.cipher));
+        }

I would suggest some refactoring here.
We can just assume that BF-CBC is not supported by the SSL library, while also reducing some code duplication:

const char *ciphername = o->ciphername;

...

/* o->ciphername might be BF-CBC even though the underlying SSL library
* does not support it. For this reason we workaround this corner case
* by pretending to have no encryption enabled and by manually adding
* the required packet overhead to the MTU computation.
*/
if (strcmp(o->ciphername, "BF-CBC") == 0)
{
ciphername = "none";
/* 64 bit block size, 64 bit IV size */ frame_add_to_extra_frame(&fake_frame, 64/8 + 64/8);
}

init_key_type(&fake_kt, ciphername, o->authname, o->keysize, true,

              false);
crypto_adjust_frame_parameters(&fake_frame, &fake_kt, o->replay,
                               cipher_kt_mode_ofb_cfb(fake_kt.cipher));
Quoted 31 lines
src/openvpn/options.c +17 -3
         frame_finalize(&fake_frame, o->ce.link_mtu_defined, o->ce.link_mtu,
                        o->ce.tun_mtu_defined, o->ce.tun_mtu);
         msg(D_MTU_DEBUG, "%s: link-mtu %u -> %d", __func__, (unsigned int) link_mtu,
@@ -3812,18 +3833,32 @@ options_string(const struct options *o,
                + (TLS_SERVER == true)
                <= 1);
 
-        init_key_type(&kt, o->ciphername, o->authname, o->keysize, true,
-                      false);
+        /* Skip resolving BF-CBC to allow SSL libraries without BF-CBC
+         * to work here in the default configuration */
+        const char *ciphername = o->ciphername;
+        int keysize;
+
+        if (strcmp(o->ciphername, "BF-CBC") == 0) {
+            init_key_type(&kt, "none", o->authname, o->keysize, true,
+                          false);
+            ciphername = cipher_kt_name(kt.cipher);
+            keysize = 128;
+        }
+        else
+        {
+            init_key_type(&kt, o->ciphername, o->authname, o->keysize, true,
+                          false);
+            keysize = kt.cipher_length * 8;
+        }
         /* Only announce the cipher to our peer if we are willing to
          * support it */
-        const char *ciphername = cipher_kt_name(kt.cipher);
         if (p2p_nopull || !o->ncp_enabled

src/openvpn/options.c options_string(const struct options *o, · line 3864

Quoted 10 lines
src/openvpn/options.c +1 -1
             || tls_item_in_cipher_list(ciphername, o->ncp_ciphers))
         {
             buf_printf(&out, ",cipher %s", ciphername);
         }
         buf_printf(&out, ",auth %s", md_kt_name(kt.digest));
-        buf_printf(&out, ",keysize %d", kt.cipher_length * 8);
+        buf_printf(&out, ",keysize %d", keysize);
         if (o->shared_secret_file)
         {
             buf_printf(&out, ",secret");

Regards,

Signature · 1 line
Antonio Quartulli
#14 lore raw

Re: [Openvpn-devel] [PATCH 3/5] Allow running a default configuration with TLS libraries without BF-CBC

Quoted 10 lines

src/openvpn/init.c do_init_crypto_tls_c1(struct context *c) · line 2766

Quoted 5 lines
src/openvpn/init.c +3 -0
+            init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
+                          options->keysize, true, true);
+        }

Why do you always want to warn the user in this context?
By passing warn=true all the time (last argument) we will have openvpn always warning the user about "weak cipher selected", but ciphername could be anything at this point.

I will change the warn to be only used if the cipehr is not used for OCC exclusively.

Quoted 61 lines
Quoted 13 lines
         /* Initialize PRNG with config-specified digest */
         prng_init(options->prng_hash, options->prng_nonce_secret_len);
src/openvpn/options.c +0 -1
index 90e78a7b..01da88ad 100644
@@ -3640,11 +3640,32 @@ calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
     {
         struct frame fake_frame = *frame;
         struct key_type fake_kt;
-        init_key_type(&fake_kt, o->ciphername, o->authname, o->keysize, true,

src/openvpn/options.c calc_options_string_link_mtu(const struct options *o, const struct frame *frame) · line 3649

Quoted 10 lines
src/openvpn/options.c +6 -3
-                      false);
+
         frame_remove_from_extra_frame(&fake_frame, crypto_max_overhead());
-        crypto_adjust_frame_parameters(&fake_frame, &fake_kt, o->replay,
-                                       cipher_kt_mode_ofb_cfb(fake_kt.cipher));
+
+        /* o->ciphername can be still BF-CBC and our SSL library might not like
+         * like it, workaround this important corner case in the name of
+         * compatibility and not stopping openvpn on our default configuration
+         */

I would rephrase a bit this comment to make it more explicit for the casual reader. See below.

Quoted 9 lines
src/openvpn/options.c +9 -0
+        if ((strcmp(o->ciphername, "BF-CBC") == 0)
+            && cipher_kt_get(o->ciphername) == NULL)
+        {
+            init_key_type(&fake_kt, "none", o->authname, o->keysize, true,
+                          false);
+
+            crypto_adjust_frame_parameters(&fake_frame, &fake_kt, o->replay,
+                                           cipher_kt_mode_ofb_cfb(fake_kt.cipher));
+            /* 64 bit block size, 64 bit IV size */

src/openvpn/options.c calc_options_string_link_mtu(const struct options *o, const struct frame *frame) · line 3668

Quoted 10 lines
src/openvpn/options.c +10 -0
+            frame_add_to_extra_frame(&fake_frame, 64/8 + 64/8);
+        }
+        else
+        {
+            init_key_type(&fake_kt, o->ciphername, o->authname, o->keysize, true,
+                          false);
+
+            crypto_adjust_frame_parameters(&fake_frame, &fake_kt, o->replay,
+                                           cipher_kt_mode_ofb_cfb(fake_kt.cipher));
+        }

I would suggest some refactoring here.
We can just assume that BF-CBC is not supported by the SSL library, while also reducing some code duplication:

const char *ciphername = o->ciphername;

...

/* o->ciphername might be BF-CBC even though the underlying SSL library
* does not support it. For this reason we workaround this corner case
* by pretending to have no encryption enabled and by manually adding
* the required packet overhead to the MTU computation.
*/
if (strcmp(o->ciphername, "BF-CBC") == 0)

Quoted 5 lines
{
   ciphername = "none";
   /* 64 bit block size, 64 bit IV size */
   frame_add_to_extra_frame(&fake_frame, 64/8 + 64/8);
}

That drops the adjusting for the HMAC size. I will add a comment to clarify what the other lines are good for.

Arne

#15 lore raw

Re: [Openvpn-devel] [PATCH 3/5] Allow running a default configuration with TLS libraries without BF-CBC

Hi,

Quoted 20 lines · Arne Schwabe
Quoted 14 lines

I would suggest some refactoring here.
We can just assume that BF-CBC is not supported by the SSL library, while also reducing some code duplication:

const char *ciphername = o->ciphername;

...

/* o->ciphername might be BF-CBC even though the underlying SSL library
* does not support it. For this reason we workaround this corner case
* by pretending to have no encryption enabled and by manually adding
* the required packet overhead to the MTU computation.
*/
if (strcmp(o->ciphername, "BF-CBC") == 0)

Quoted 5 lines
{
   ciphername = "none";
   /* 64 bit block size, 64 bit IV size */
   frame_add_to_extra_frame(&fake_frame, 64/8 + 64/8);
}
Quoted 2 lines

That drops the adjusting for the HMAC size. I will add a comment to clarify what the other lines are good for.

I moved the crypto_adjustment call below this block, since it is performed both for BF-CBC and non-BF-CBC. Doesn't it work?

Signature · 1 line
Antonio Quartulli
#16 lore raw

PATCH v2 1/1 Allow running a default configuration with TLS libraries without BF-CBC

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

Patch v2: add more clarifying comment, do not warn about OCC only insecure
ciphers, code improvements

---
 src/openvpn/init.c    | 32 ++++++++++++++++++++++--------
 src/openvpn/options.c | 46 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 64 insertions(+), 14 deletions(-)
src/openvpn/init.c +24 -8
index c3493c42..df0c7ebc 100644
@@ -2770,14 +2770,30 @@ do_init_crypto_tls_c1(struct context *c)
 #endif /* if P2MP */
         }
 
-        /* Do not warn if we only have BF-CBC in options->ciphername
-         * because it is still the default cipher */
-        bool warn = !streq(options->ciphername, "BF-CBC")
-             || options->enable_ncp_fallback;
-        /* Get cipher & hash algorithms */
-        init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
-                      options->keysize, true, warn);
-
+       /*
+        * BF-CBC is allowed to be used only when explicitly configured
+        * as NCP-fallback or when NCP has been disabled.
+        * In all other cases don't attempt to initialize BF-CBC as it
+        * may not even be supported by the underlying SSL library.
+        *
+        * Therefore, the key structure has to be initialized when:
+        * - any non-BF-CBC cipher was selected; or
+        * - BF-CBC is selected and NCP is disabled (explicit request to
+        *   use the BF-CBC cipher); or
+        * - BF-CBC is selected, NCP is enabled and fallback is enabled
+        *   (BF-CBC will be the fallback).
+        *
+        * Note that BF-CBC will still be part of the OCC string to retain
+        * backwards compatibility with older clients.
+        */
+        if (!streq(options->ciphername, "BF-CBC") || !options->ncp_enabled
+            || options->enable_ncp_fallback)
+        {
+            /* Do not warn if the if the cipher is used only in OCC */
+            bool warn = !options->ncp_enabled || options->enable_ncp_fallback;
+            init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
+                          options->keysize, true, warn);
+        }
         /* Initialize PRNG with config-specified digest */
         prng_init(options->prng_hash, options->prng_nonce_secret_len);
 
src/openvpn/options.c +40 -6

67 lines not on this page · raw message

Signature · 1 line
2.30.0
Acked-by on v2 1/1
#17 lore raw

Re: [Openvpn-devel] [PATCH v2] Allow running a default configuration with TLS libraries without BF-CBC

Hi,

Quoted 6 lines · Arne Schwabe

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

Quoted 6 lines

Patch v2: add more clarifying comment, do not warn about OCC only insecure
ciphers, code improvements

Signature · 1 line
Antonio Quartulli
#5 lore raw

Re: [Openvpn-devel] [PATCH 4/5] Check return values in md_ctx_init and hmac_ctx_init

In reply to Arne Schwabe

Hi,

Quoted 13 lines · Arne Schwabe

Without this OpenVPN will later segfault on a FIPS enabled system due to the algorithm available but not allowed.

---
 src/openvpn/crypto_openssl.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
src/openvpn/crypto_openssl.c +0 -0
index c60d4a54..75557cca 100644

src/openvpn/crypto_openssl.c md_ctx_init(EVP_MD_CTX *ctx, const EVP_MD *kt) · line 957

Quoted 5 lines
src/openvpn/crypto_openssl.c +1 -1
@@ -954,7 +954,10 @@ md_ctx_init(EVP_MD_CTX *ctx, const EVP_MD *kt)
     ASSERT(NULL != ctx && NULL != kt);
 
     EVP_MD_CTX_init(ctx);
-    EVP_DigestInit(ctx, kt);
+    if (EVP_DigestInit(ctx, kt) != 1)

Based on the documentation, this function returns 0 on failure and 1 on success (basically a bool).

How about turning this into:

if (!EVP_DigestInit(ctx, kt))

?

This way it is clear that we are not just looking for '1' as a special return value, but simply for 'success'. (Same as we do for function returning a real bool).

What do you think?

src/openvpn/crypto_openssl.c hmac_ctx_init(HMAC_CTX *ctx, const uint8_t *key, int key_len, · line 1017

Quoted 12 lines
src/openvpn/crypto_openssl.c +4 -1
+    {
+        crypto_msg(M_FATAL, "EVP_DigestInit failed");
+    }
 }
 
 void
@@ -1011,7 +1014,10 @@ hmac_ctx_init(HMAC_CTX *ctx, const uint8_t *key, int key_len,
     ASSERT(NULL != kt && NULL != ctx);
 
     HMAC_CTX_reset(ctx);
-    HMAC_Init_ex(ctx, key, key_len, kt, NULL);
+    if  (HMAC_Init_ex(ctx, key, key_len, kt, NULL) != 1)

Same for this function.

Regards,

Signature · 1 line
Antonio Quartulli
#6 lore raw

PATCH v2 4/5 Check return values in md_ctx_init and hmac_ctx_init

Without this OpenVPN will later segfault on a FIPS enabled system due to the algorithm available but not allowed.

Patch V2: Use (!func) instead (func != 1)

---
 src/openvpn/crypto_openssl.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
src/openvpn/crypto_openssl.c +12 -3
index c60d4a54..e124a7b6 100644
@@ -954,7 +954,10 @@ md_ctx_init(EVP_MD_CTX *ctx, const EVP_MD *kt)
     ASSERT(NULL != ctx && NULL != kt);
 
     EVP_MD_CTX_init(ctx);
-    EVP_DigestInit(ctx, kt);
+    if (!EVP_DigestInit(ctx, kt))
+    {
+        crypto_msg(M_FATAL, "EVP_DigestInit failed");
+    }
 }
 
 void
@@ -1011,7 +1014,10 @@ hmac_ctx_init(HMAC_CTX *ctx, const uint8_t *key, int key_len,
     ASSERT(NULL != kt && NULL != ctx);
 
     HMAC_CTX_reset(ctx);
-    HMAC_Init_ex(ctx, key, key_len, kt, NULL);
+    if  (!HMAC_Init_ex(ctx, key, key_len, kt, NULL))
+    {
+        crypto_msg(M_FATAL, "HMAC_Init_ex failed");
+    }
 
     /* make sure we used a big enough key */
     ASSERT(HMAC_size(ctx) <= key_len);
@@ -1032,7 +1038,10 @@ hmac_ctx_size(const HMAC_CTX *ctx)
 void
 hmac_ctx_reset(HMAC_CTX *ctx)
 {
-    HMAC_Init_ex(ctx, NULL, 0, NULL, NULL);
+    if(!HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
+    {
+        crypto_msg(M_FATAL, "HMAC_Init_ex failed");
+    }
 }
 
 void
Signature · 1 line
2.30.0
#7 lore raw

Re: [Openvpn-devel] [PATCH v2 4/5] Check return values in md_ctx_init and hmac_ctx_init

Hi,

Quoted 39 lines · Arne Schwabe

Without this OpenVPN will later segfault on a FIPS enabled system due to the algorithm available but not allowed.

Patch V2: Use (!func) instead (func != 1)

---
 src/openvpn/crypto_openssl.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
src/openvpn/crypto_openssl.c +8 -2
index c60d4a54..e124a7b6 100644
@@ -954,7 +954,10 @@ md_ctx_init(EVP_MD_CTX *ctx, const EVP_MD *kt)
     ASSERT(NULL != ctx && NULL != kt);
 
     EVP_MD_CTX_init(ctx);
-    EVP_DigestInit(ctx, kt);
+    if (!EVP_DigestInit(ctx, kt))
+    {
+        crypto_msg(M_FATAL, "EVP_DigestInit failed");
+    }
 }
 
 void
@@ -1011,7 +1014,10 @@ hmac_ctx_init(HMAC_CTX *ctx, const uint8_t *key, int key_len,
     ASSERT(NULL != kt && NULL != ctx);
 
     HMAC_CTX_reset(ctx);
-    HMAC_Init_ex(ctx, key, key_len, kt, NULL);
+    if  (!HMAC_Init_ex(ctx, key, key_len, kt, NULL))
+    {
+        crypto_msg(M_FATAL, "HMAC_Init_ex failed");
+    }
 
     /* make sure we used a big enough key */
     ASSERT(HMAC_size(ctx) <= key_len);

src/openvpn/crypto_openssl.c hmac_ctx_size(const HMAC_CTX *ctx) · line 1045

Quoted 9 lines
src/openvpn/crypto_openssl.c +4 -1
@@ -1032,7 +1038,10 @@ hmac_ctx_size(const HMAC_CTX *ctx)
 void
 hmac_ctx_reset(HMAC_CTX *ctx)
 {
-    HMAC_Init_ex(ctx, NULL, 0, NULL, NULL);
+    if(!HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
+    {
+        crypto_msg(M_FATAL, "HMAC_Init_ex failed");
+    }
 }

I saw that you missed this case earlier, but I thought that this call cannot really fail.

Assuming it can fail under certain conditions, wouldn't the M_FATAL somewhat become a DoS on the server side?

Regards,

Signature · 1 line
Antonio Quartulli
#10 lore raw

Re: [Openvpn-devel] [PATCH 5/5] Prefer TLS libraries TLS PRF function, fix OpenVPN in FIPS mode

In reply to Arne Schwabe

Hi,

witht his review I want to open a broader discussion about the use of ASSERT in the OpenVPN code.

My comments below will get to the point.

Quoted 10 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.

Does this mean that a system that is FIPS enabled running mbedTLS <2.18 will still not work? Or the old mbedTLS implementeation had no issue?

Quoted 8 lines

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.

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

Could you add some description of why we need to change the PRF implementation for FISP-enabled systems?

Quoted 15 lines
---
 src/openvpn/crypto_backend.h           |  19 +++
 src/openvpn/crypto_mbedtls.c           | 143 ++++++++++++++++++++
 src/openvpn/crypto_openssl.c           | 172 ++++++++++++++++++++++++-
 src/openvpn/ssl.c                      | 130 +------------------
 tests/unit_tests/openvpn/test_crypto.c |  32 +++++
 5 files changed, 366 insertions(+), 130 deletions(-)
src/openvpn/crypto_backend.h +0 -0
index 85cb084a..b72684ce 100644

src/openvpn/crypto_backend.h const char *translate_cipher_name_from_openvpn(const char *cipher_name); · line 705

Quoted 7 lines
src/openvpn/crypto_backend.h +4 -0
@@ -699,4 +699,23 @@ 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
+ * fun ction definition see the TLS RFCs like RFC 4346.

typ0: "fun ction" -> "function"

Quoted 5 lines
src/openvpn/crypto_backend.h +5 -0
+ *
+ * @param seed          seed to use
+ * @param seed_len      length of the seed
+ * @param secret        secret to use
+ * @param secret_len    length of the secret

src/openvpn/crypto_backend.h const char *translate_cipher_name_from_openvpn(const char *cipher_name); · line 720

Quoted 10 lines
src/openvpn/crypto_backend.h +10 -0
+ * @param output        output destination
+ * @param output_len    length of output/number of bytes to generate
+ */
+void
+ssl_tls1_PRF(const uint8_t *seed,
+             int seed_len,
+             const uint8_t *secret,
+             int secret_len,
+             uint8_t *output,
+             int output_len);

We don't use this kind of indentation for prototypes. Please reindent it as the other functions in this file.
(I know you have moved this signature, but while at it, make it right :))

Quoted 16 lines
src/openvpn/crypto_backend.h +0 -0
 #endif /* CRYPTO_BACKEND_H_ */
src/openvpn/crypto_mbedtls.c +1 -0
index fbb1f120..dcdd964a 100644
@@ -54,6 +54,7 @@
 #include <mbedtls/pem.h>
 
 #include <mbedtls/entropy.h>
+#include <mbedtls/ssl.h>
 
 
 /*
@@ -984,4 +985,146 @@ memcmp_constant_time(const void *a, const void *b, size_t size)
 
     return diff;

src/openvpn/crypto_mbedtls.c memcmp_constant_time(const void *a, const void *b, size_t size) · line 996

Quoted 10 lines
src/openvpn/crypto_mbedtls.c +9 -0
 }
+/* mbedtls-2.18.0 or newer */
+#ifdef HAVE_MBEDTLS_SSL_TLS_PRF
+void
+ssl_tls1_PRF(const uint8_t *seed,
+            int seed_len,
+            const uint8_t *secret,
+            int secret_len,
+            uint8_t *output,
+            int output_len)

same indentation issue.

Quoted 14 lines
src/openvpn/crypto_mbedtls.c +14 -0
+{
+    mbedtls_ssl_tls_prf(MBEDTLS_SSL_TLS_PRF_TLS1, secret, secret_len, "", seed,
+                        seed_len, output, output_len);
+}
+#else
+/*
+ * Generate the hash required by for the \c tls1_PRF function.
+ *
+ * @param md_kt         Message digest to use
+ * @param sec           Secret to base the hash on
+ * @param sec_len       Length of the secret
+ * @param seed          Seed to hash
+ * @param seed_len      Length of the seed
+ * @param out           Output buffer

src/openvpn/crypto_mbedtls.c memcmp_constant_time(const void *a, const void *b, size_t size) · line 1020

Quoted 10 lines
src/openvpn/crypto_mbedtls.c +10 -0
+ * @param olen          Length of the output buffer
+ */
+static void
+tls1_P_hash(const md_kt_t *md_kt,
+            const uint8_t *sec,
+            int sec_len,
+            const uint8_t *seed,
+            int seed_len,
+            uint8_t *out,
+            int olen)

same indentation issue.

src/openvpn/crypto_mbedtls.c memcmp_constant_time(const void *a, const void *b, size_t size) · line 1028

Quoted 8 lines
src/openvpn/crypto_mbedtls.c +8 -0
+{
+    struct gc_arena gc = gc_new();
+    uint8_t A1[MAX_HMAC_KEY_LENGTH];
+
+#ifdef ENABLE_DEBUG
+    const int olen_orig = olen;
+    const uint8_t *out_orig = out;
+#endif

I think this block above is not needed anymore.

Quoted 7 lines
src/openvpn/crypto_mbedtls.c +7 -0
+
+    hmac_ctx_t *ctx = hmac_ctx_new();
+    hmac_ctx_t *ctx_tmp = hmac_ctx_new();
+
+    dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash sec: %s", format_hex(sec, sec_len, 0, &gc));
+    dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash seed: %s", format_hex(seed, seed_len, 0, &gc));
+

src/openvpn/crypto_mbedtls.c memcmp_constant_time(const void *a, const void *b, size_t size) · line 1045

Quoted 10 lines
src/openvpn/crypto_mbedtls.c +10 -0
+    int chunk = md_kt_size(md_kt);
+    unsigned int A1_len = md_kt_size(md_kt);
+
+    hmac_ctx_init(ctx, sec, sec_len, md_kt);
+    hmac_ctx_init(ctx_tmp, sec, sec_len, md_kt);
+
+    hmac_ctx_update(ctx,seed,seed_len);
+    hmac_ctx_final(ctx, A1);
+
+    for (;; )

Remove space after ;;

src/openvpn/crypto_mbedtls.c memcmp_constant_time(const void *a, const void *b, size_t size) · line 1051

Quoted 6 lines
src/openvpn/crypto_mbedtls.c +6 -0
+    {
+        hmac_ctx_reset(ctx);
+        hmac_ctx_reset(ctx_tmp);
+        hmac_ctx_update(ctx,A1,A1_len);
+        hmac_ctx_update(ctx_tmp,A1,A1_len);
+        hmac_ctx_update(ctx,seed,seed_len);

Add space after the ',' in all lines above.

src/openvpn/crypto_mbedtls.c memcmp_constant_time(const void *a, const void *b, size_t size) · line 1063

Quoted 12 lines
src/openvpn/crypto_mbedtls.c +12 -0
+
+        if (olen > chunk)
+        {
+            hmac_ctx_final(ctx, out);
+            out += chunk;
+            olen -= chunk;
+            hmac_ctx_final(ctx_tmp, A1); /* calc the next A1 value */
+        }
+        else    /* last one */
+        {
+            hmac_ctx_final(ctx, A1);
+            memcpy(out,A1,olen);

same

Quoted 29 lines
src/openvpn/crypto_mbedtls.c +29 -0
+            break;
+        }
+    }
+    hmac_ctx_cleanup(ctx);
+    hmac_ctx_free(ctx);
+    hmac_ctx_cleanup(ctx_tmp);
+    hmac_ctx_free(ctx_tmp);
+    secure_memzero(A1, sizeof(A1));
+
+    dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash out: %s", format_hex(out_orig, olen_orig, 0, &gc));
+    gc_free(&gc);
+}
+
+/*
+ * Use the TLS PRF function for generating data channel keys.
+ * This code is based on the OpenSSL library.
+ *
+ * TLS generates keys as such:
+ *
+ * master_secret[48] = PRF(pre_master_secret[48], "master secret",
+ *                         ClientHello.random[32] + ServerHello.random[32])
+ *
+ * key_block[] = PRF(SecurityParameters.master_secret[48],
+ *                 "key expansion",
+ *                 SecurityParameters.server_random[32] +
+ *                 SecurityParameters.client_random[32]);
+ *
+ * Notes:
+ *

src/openvpn/crypto_mbedtls.c memcmp_constant_time(const void *a, const void *b, size_t size) · line 1102

Quoted 10 lines
src/openvpn/crypto_mbedtls.c +10 -0
+ * (1) key_block contains a full set of 4 keys.
+ * (2) The pre-master secret is generated by the client.
+ */
+void
+ssl_tls1_PRF(const uint8_t *label,
+         int label_len,
+         const uint8_t *sec,
+         int slen,
+         uint8_t *out1,
+         int olen)

same indentation issue.

Quoted 33 lines
src/openvpn/crypto_mbedtls.c +27 -0
+{
+    struct gc_arena gc = gc_new();
+    const md_kt_t *md5 = md_kt_get("MD5");
+    const md_kt_t *sha1 = md_kt_get("SHA1");
+
+    uint8_t *out2 = (uint8_t *) gc_malloc(olen, false, &gc);
+
+    int len = slen/2;
+    const uint8_t *S1 = sec;
+    const uint8_t *S2 = &(sec[len]);
+    len += (slen&1); /* add for odd, make longer */
+
+    tls1_P_hash(md5,S1,len,label,label_len,out1,olen);
+    tls1_P_hash(sha1,S2,len,label,label_len,out2,olen);
+
+    for (int i = 0; i<olen; i++)
+    {
+        out1[i] ^= out2[i];
+    }
+
+    secure_memzero(out2, olen);
+
+    dmsg(D_SHOW_KEY_SOURCE, "tls1_PRF out[%d]: %s", olen, format_hex(out1, olen, 0, &gc));
+
+    gc_free(&gc);
+}
+#endif
 #endif /* ENABLE_CRYPTO_MBEDTLS */
src/openvpn/crypto_openssl.c +0 -0
index 75557cca..8ebc52e7 100644

src/openvpn/crypto_openssl.c line 55

Quoted 7 lines
src/openvpn/crypto_openssl.c +3 -1
@@ -50,7 +50,9 @@
 #include <openssl/objects.h>
 #include <openssl/rand.h>
 #include <openssl/ssl.h>
-
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L
+#include <openssl/kdf.h>
+#endif

Please, re-add the empty line here.

Quoted 15 lines
src/openvpn/crypto_openssl.c +8 -0
 /*
  * Check for key size creepage.
  */
@@ -1121,4 +1123,172 @@ engine_load_key(const char *file, SSL_CTX *ctx)
 #endif /* if HAVE_OPENSSL_ENGINE */
 }
 
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L
+void
+ssl_tls1_PRF(const uint8_t *seed,
+             int seed_len,
+             const uint8_t *secret,
+             int secret_len,
+             uint8_t *output,
+             int output_len)

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1143

Quoted 10 lines
src/openvpn/crypto_openssl.c +10 -0
+{
+    EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
+    ASSERT(EVP_PKEY_derive_init(pctx) == 1);
+
+    ASSERT(EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_md5_sha1()) == 1);
+
+    ASSERT(EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, secret, secret_len) == 1);
+
+    ASSERT(EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seed_len) == 1);
+

Are we sure we want all these ASSERTs?

In the past we have tried hard to get rid of them in code executed by the server at runtime, to avoid DoS that we can't foresee now. We rather want to errors as graceful as possible.

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1146

Quoted 3 lines
src/openvpn/crypto_openssl.c +3 -0
+    size_t out_len = output_len;
+    ASSERT (EVP_PKEY_derive(pctx, output, &out_len) == 1);
+    ASSERT (out_len == output_len);

I hope we get rid of the ASSERTs, but if we don't, please remove the space between 'ASSERT' and '('.

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1153

Quoted 7 lines
src/openvpn/crypto_openssl.c +7 -0
+}
+#else
+/*
+ * Generate the hash required by for the \c tls1_PRF function.
+ *
+ * We cannot use our normal hmac_* function as they do not work
+ * in a FIPS environment and need to use the EVP_MD_* API, which

which .. ? <-- sentence aborted.

By the way, this may become more obvious once you epxlain in the commit message why FIPS is broken, but maybe you could specify it here too?

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1166

Quoted 13 lines
src/openvpn/crypto_openssl.c +13 -0
+ *
+ * The function below is adapted from OpenSSL 1.0.2t
+ *
+ * @param md_kt         Message digest to use
+ * @param sec           Secret to base the hash on
+ * @param sec_len       Length of the secret
+ * @param seed          Seed to hash
+ * @param seed_len      Length of the seed
+ * @param out           Output buffer
+ * @param olen          Length of the output buffer
+ */
+static
+int tls1_P_hash(const EVP_MD *md, const unsigned char *sec,

This function is returning int even though it returns only success/failure. Shouldn't we make it bool?

At the same time I see a discrepancy with what mbedTLS does:
- OpenSSL checks the return value of each API call and possibly fails (even though the caller then makes an ASSERT on the return value - see comments above);
- mbedTLS has plenty ASSERTs and all functions return void.

Should we try to converge to what we consider the best behaviour? Given my argument above about ASSERT, I'd rather go with handling errors as best as we can and void ASSERTs.

Quoted 92 lines
src/openvpn/crypto_openssl.c +92 -0
+                int sec_len,
+                const void *seed, int seed_len,
+                unsigned char *out, int olen)
+{
+    int chunk;
+    size_t j;
+    EVP_MD_CTX ctx, ctx_tmp, ctx_init;
+    EVP_PKEY *mac_key;
+    unsigned char A1[EVP_MAX_MD_SIZE];
+    size_t A1_len;
+    int ret = 0;
+
+    chunk = EVP_MD_size(md);
+    OPENSSL_assert(chunk >= 0);
+
+    EVP_MD_CTX_init(&ctx);
+    EVP_MD_CTX_init(&ctx_tmp);
+    EVP_MD_CTX_init(&ctx_init);
+    EVP_MD_CTX_set_flags(&ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
+    mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
+    if (!mac_key)
+        goto err;
+    if (!EVP_DigestSignInit(&ctx_init, NULL, md, NULL, mac_key))
+        goto err;
+    if (!EVP_MD_CTX_copy_ex(&ctx, &ctx_init))
+        goto err;
+    if (!EVP_DigestSignUpdate(&ctx, seed, seed_len))
+        goto err;
+    if (!EVP_DigestSignFinal(&ctx, A1, &A1_len))
+        goto err;
+
+    for (;;) {
+        /* Reinit mac contexts */
+        if (!EVP_MD_CTX_copy_ex(&ctx, &ctx_init))
+            goto err;
+        if (!EVP_DigestSignUpdate(&ctx, A1, A1_len))
+            goto err;
+        if (olen > chunk && !EVP_MD_CTX_copy_ex(&ctx_tmp, &ctx))
+            goto err;
+        if (!EVP_DigestSignUpdate(&ctx, seed, seed_len))
+            goto err;
+
+        if (olen > chunk)
+        {
+            if (!EVP_DigestSignFinal(&ctx, out, &j))
+                goto err;
+            out += j;
+            olen -= j;
+            /* calc the next A1 value */
+            if (!EVP_DigestSignFinal(&ctx_tmp, A1, &A1_len))
+                goto err;
+        }
+        else
+        {
+            /* last one */
+
+            if (!EVP_DigestSignFinal(&ctx, A1, &A1_len))
+                goto err;
+            memcpy(out, A1, olen);
+            break;
+        }
+    }
+    ret = 1;
+err:
+    EVP_PKEY_free(mac_key);
+    EVP_MD_CTX_cleanup(&ctx);
+    EVP_MD_CTX_cleanup(&ctx_tmp);
+    EVP_MD_CTX_cleanup(&ctx_init);
+    OPENSSL_cleanse(A1, sizeof(A1));
+    return ret;
+}
+
+
+/*
+ * Use the TLS PRF function for generating data channel keys.
+ * This code is based on the OpenSSL library.
+ *
+ * TLS generates keys as such:
+ *
+ * master_secret[48] = PRF(pre_master_secret[48], "master secret",
+ *                         ClientHello.random[32] + ServerHello.random[32])
+ *
+ * key_block[] = PRF(SecurityParameters.master_secret[48],
+ *                 "key expansion",
+ *                 SecurityParameters.server_random[32] +
+ *                 SecurityParameters.client_random[32]);
+ *
+ * Notes:
+ *
+ * (1) key_block contains a full set of 4 keys.
+ * (2) The pre-master secret is generated by the client.
+ */

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1268

Quoted 10 lines
src/openvpn/crypto_openssl.c +10 -0
+void
+ssl_tls1_PRF(const uint8_t *label,
+         int label_len,
+         const uint8_t *sec,
+         int slen,
+         uint8_t *out1,
+         int olen)
+{
+    struct gc_arena gc = gc_new();
+    /* For some reason our md_kt_get("MD5") fails otherwise in the unit test */

any clue as to why?

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1272

Quoted 4 lines
src/openvpn/crypto_openssl.c +4 -0
+    const md_kt_t *md5 = EVP_md5();
+    const md_kt_t *sha1 = EVP_sha1();
+
+    uint8_t *out2 = (uint8_t *) gc_malloc(olen, false, &gc);

no space after the cast

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1274

Quoted 2 lines
src/openvpn/crypto_openssl.c +2 -0
+
+    int len = slen/2;

spaces around the operator

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1276

Quoted 2 lines
src/openvpn/crypto_openssl.c +2 -0
+    const uint8_t *S1 = sec;
+    const uint8_t *S2 = &(sec[len]);

why the ( ) ? address-of comes after the [] operator (alternative is to use "sec + len" (which is probably used more frequent in the code).

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1277

Quoted 1 line
src/openvpn/crypto_openssl.c +1 -0
+    len += (slen&1); /* add for odd, make longer */

spaces around the operator

src/openvpn/crypto_openssl.c engine_load_key(const char *file, SSL_CTX *ctx) · line 1282

Quoted 5 lines
src/openvpn/crypto_openssl.c +5 -0
+
+    ASSERT(tls1_P_hash(md5,S1,len,label,label_len,out1,olen));
+    ASSERT(tls1_P_hash(sha1,S2,len,label,label_len,out2,olen));
+
+    for (int i = 0; i<olen; i++)

space around the '<' operator

Quoted 173 lines
src/openvpn/crypto_openssl.c +11 -0
+    {
+        out1[i] ^= out2[i];
+    }
+
+    secure_memzero(out2, olen);
+
+    dmsg(D_SHOW_KEY_SOURCE, "tls1_PRF out[%d]: %s", olen, format_hex(out1, olen, 0, &gc));
+
+    gc_free(&gc);
+}
+#endif
 #endif /* ENABLE_CRYPTO_OPENSSL */
src/openvpn/ssl.c +1 -129
index d8f2cf0d..fa1de5c1 100644
@@ -1595,134 +1595,6 @@ key_source2_print(const struct key_source2 *k)
     key_source_print(&k->server, "Server");
 }
 
-/*
- * Generate the hash required by for the \c tls1_PRF function.
- *
- * @param md_kt         Message digest to use
- * @param sec           Secret to base the hash on
- * @param sec_len       Length of the secret
- * @param seed          Seed to hash
- * @param seed_len      Length of the seed
- * @param out           Output buffer
- * @param olen          Length of the output buffer
- */
-static void
-tls1_P_hash(const md_kt_t *md_kt,
-            const uint8_t *sec,
-            int sec_len,
-            const uint8_t *seed,
-            int seed_len,
-            uint8_t *out,
-            int olen)
-{
-    struct gc_arena gc = gc_new();
-    uint8_t A1[MAX_HMAC_KEY_LENGTH];
-
-#ifdef ENABLE_DEBUG
-    const int olen_orig = olen;
-    const uint8_t *out_orig = out;
-#endif
-
-    hmac_ctx_t *ctx = hmac_ctx_new();
-    hmac_ctx_t *ctx_tmp = hmac_ctx_new();
-
-    dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash sec: %s", format_hex(sec, sec_len, 0, &gc));
-    dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash seed: %s", format_hex(seed, seed_len, 0, &gc));
-
-    int chunk = md_kt_size(md_kt);
-    unsigned int A1_len = md_kt_size(md_kt);
-
-    hmac_ctx_init(ctx, sec, sec_len, md_kt);
-    hmac_ctx_init(ctx_tmp, sec, sec_len, md_kt);
-
-    hmac_ctx_update(ctx,seed,seed_len);
-    hmac_ctx_final(ctx, A1);
-
-    for (;; )
-    {
-        hmac_ctx_reset(ctx);
-        hmac_ctx_reset(ctx_tmp);
-        hmac_ctx_update(ctx,A1,A1_len);
-        hmac_ctx_update(ctx_tmp,A1,A1_len);
-        hmac_ctx_update(ctx,seed,seed_len);
-
-        if (olen > chunk)
-        {
-            hmac_ctx_final(ctx, out);
-            out += chunk;
-            olen -= chunk;
-            hmac_ctx_final(ctx_tmp, A1); /* calc the next A1 value */
-        }
-        else    /* last one */
-        {
-            hmac_ctx_final(ctx, A1);
-            memcpy(out,A1,olen);
-            break;
-        }
-    }
-    hmac_ctx_cleanup(ctx);
-    hmac_ctx_free(ctx);
-    hmac_ctx_cleanup(ctx_tmp);
-    hmac_ctx_free(ctx_tmp);
-    secure_memzero(A1, sizeof(A1));
-
-    dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash out: %s", format_hex(out_orig, olen_orig, 0, &gc));
-    gc_free(&gc);
-}
-
-/*
- * Use the TLS PRF function for generating data channel keys.
- * This code is based on the OpenSSL library.
- *
- * TLS generates keys as such:
- *
- * master_secret[48] = PRF(pre_master_secret[48], "master secret",
- *                         ClientHello.random[32] + ServerHello.random[32])
- *
- * key_block[] = PRF(SecurityParameters.master_secret[48],
- *                 "key expansion",
- *                 SecurityParameters.server_random[32] +
- *                 SecurityParameters.client_random[32]);
- *
- * Notes:
- *
- * (1) key_block contains a full set of 4 keys.
- * (2) The pre-master secret is generated by the client.
- */
-static void
-tls1_PRF(const uint8_t *label,
-         int label_len,
-         const uint8_t *sec,
-         int slen,
-         uint8_t *out1,
-         int olen)
-{
-    struct gc_arena gc = gc_new();
-    const md_kt_t *md5 = md_kt_get("MD5");
-    const md_kt_t *sha1 = md_kt_get("SHA1");
-
-    uint8_t *out2 = (uint8_t *) gc_malloc(olen, false, &gc);
-
-    int len = slen/2;
-    const uint8_t *S1 = sec;
-    const uint8_t *S2 = &(sec[len]);
-    len += (slen&1); /* add for odd, make longer */
-
-    tls1_P_hash(md5,S1,len,label,label_len,out1,olen);
-    tls1_P_hash(sha1,S2,len,label,label_len,out2,olen);
-
-    for (int i = 0; i<olen; i++)
-    {
-        out1[i] ^= out2[i];
-    }
-
-    secure_memzero(out2, olen);
-
-    dmsg(D_SHOW_KEY_SOURCE, "tls1_PRF out[%d]: %s", olen, format_hex(out1, olen, 0, &gc));
-
-    gc_free(&gc);
-}
-
 static void
 openvpn_PRF(const uint8_t *secret,
             int secret_len,
@@ -1757,7 +1629,7 @@ openvpn_PRF(const uint8_t *secret,
     }
 
     /* compute PRF */
-    tls1_PRF(BPTR(&seed), BLEN(&seed), secret, secret_len, output, output_len);
+    ssl_tls1_PRF(BPTR(&seed), BLEN(&seed), secret, secret_len, output, output_len);
 
     buf_clear(&seed);
     free_buf(&seed);
tests/unit_tests/openvpn/test_crypto.c +1 -0
index ea9b99b2..6c68099e 100644
@@ -38,6 +38,7 @@
 #include <cmocka.h>
 
 #include "crypto.h"
+#include "ssl_backend.h"
 
 #include "mock_msg.h"
 

tests/unit_tests/openvpn/test_crypto.c crypto_translate_cipher_names(void **state) · line 141

Quoted 5 lines
tests/unit_tests/openvpn/test_crypto.c +2 -0
@@ -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,

space after the opening {

tests/unit_tests/openvpn/test_crypto.c crypto_translate_cipher_names(void **state) · line 144

Quoted 3 lines
tests/unit_tests/openvpn/test_crypto.c +3 -0
+                               0x27, 0x91, 0x6a, 0xcf, 0xc2, 0xd5, 0x92, 0xfb,
+                               0xb1, 0x56, 0x7e, 0x4b, 0x4b, 0x14, 0x59, 0xe6,
+                               0xa9, 0x04, 0xac, 0x2d, 0xda, 0xb7, 0x2d, 0x67};

space before the closing }

Quoted 26 lines
tests/unit_tests/openvpn/test_crypto.c +25 -0
+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));
+}
+

tests/unit_tests/openvpn/test_crypto.c crypto_translate_cipher_names(void **state) · line 179

Quoted 10 lines
tests/unit_tests/openvpn/test_crypto.c +1 -0
 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)

Regards,

Signature · 1 line
Antonio Quartulli
#8 lore raw

Re: [Openvpn-devel] [PATCH v2 4/5] Check return values in md_ctx_init and hmac_ctx_init

In reply to Antonio Quartulli
Quoted 7 lines

I saw that you missed this case earlier, but I thought that this call cannot really fail.

Assuming it can fail under certain conditions, wouldn't the M_FATAL somewhat become a DoS on the server side?

The condition it can fail is basically that the crypto library is unable or unwilling to create a context for that hash algorithm. If that happens we later segfault. This basically only happend on the OpenSSL in FIPS mode, which claim to have MD5 but will then not accept to create MD5. So a fatal fail here is better than a segfault.

Arne

#11 lore raw

Re: [Openvpn-devel] [PATCH 5/5] Prefer TLS libraries TLS PRF function, fix OpenVPN in FIPS mode

In reply to Antonio Quartulli

Am 29.01.21 um 15:09 schrieb Antonio Quartulli:

Quoted 19 lines

Hi,

witht his review I want to open a broader discussion about the use of ASSERT in the OpenVPN code.

My comments below will get to the point.

Quoted 10 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.

Quoted 2 lines

Does this mean that a system that is FIPS enabled running mbedTLS <2.18 will still not work? Or the old mbedTLS implementeation had no issue?

There is no special FIPS mode in mbed TLS. It just means that for OpenSSL 1.1.0+ and mbed TLS 2.18+, we rely on a library provided function instead of implementing the TLS1 PRF ourselves. When we drop OpenSSL 1.0.2 and mbed TLS <2.18 support our own version can be removed.

For OpenSSL the library function has the additional advantage that it is considered to be allowed in FIPS mode without special flags etc.

Quoted 6 lines
Quoted 6 lines

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.

Quoted 6 lines
Quoted 3 lines

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

Could you add some description of why we need to change the PRF implementation for FISP-enabled systems?

V2 will include this paragraph:

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.

Quoted 10 lines

src/openvpn/crypto_mbedtls.c memcmp_constant_time(const void *a, const void *b, size_t size) · line 1028

Quoted 8 lines
src/openvpn/crypto_mbedtls.c +8 -0
+{
+    struct gc_arena gc = gc_new();
+    uint8_t A1[MAX_HMAC_KEY_LENGTH];
+
+#ifdef ENABLE_DEBUG
+    const int olen_orig = olen;
+    const uint8_t *out_orig = out;
+#endif

I think this block above is not needed anymore.

The last line in the function uses it, I just inherited this code:

    dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash out: %s", format_hex(out_orig,

olen_orig, 0, &gc));

For the ASSERTs, I will send a follow up patch, which changes this code path into something that can catch the failure.

Arne

#18 lore raw

Re: [Openvpn-devel] [PATCH v2] Allow running a default configuration with TLS libraries without BF-CBC

In reply to Arne Schwabe

Hi,

Quoted 5 lines · Arne Schwabe

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

I was about to merge this, based on Antonio's ACK, but this part of the code confuses me:

Quoted 20 lines
src/openvpn/options.c +10 -2
index b81137cf..d52057cc 100644
@@ -3836,18 +3856,32 @@ options_string(const struct options *o,
                + (TLS_SERVER == true)
                <= 1);
 
-        init_key_type(&kt, o->ciphername, o->authname, o->keysize, true,
-                      false);
+        /* Skip resolving BF-CBC to allow SSL libraries without BF-CBC
+         * to work here in the default configuration */
+        const char *ciphername = o->ciphername;
+        int keysize;
+
+        if (strcmp(o->ciphername, "BF-CBC") == 0) {
+            init_key_type(&kt, "none", o->authname, o->keysize, true,
+                          false);
+            ciphername = cipher_kt_name(kt.cipher);
+            keysize = 128;

src/openvpn/options.c options_string(const struct options *o, · line 3843

Quoted 10 lines
src/openvpn/options.c +7 -1
+        }
+        else
+        {
+            init_key_type(&kt, o->ciphername, o->authname, o->keysize, true,
+                          false);
+            keysize = kt.cipher_length * 8;
+        }
         /* Only announce the cipher to our peer if we are willing to
          * support it */
-        const char *ciphername = cipher_kt_name(kt.cipher);

So the old code always sends "cipher_kt_name(kt.cipher)".

The new code adds a special case handling for "BF-CBC", calling init_key_type(none), but then does the "cipher_kt_name()" only for the "BF-CBC/none" case, no more for the "all other ciphers".

This looks like the wrong way around - shouldn't it do the

src/openvpn/options.c options_string(const struct options *o, · line 3867

Quoted 1 line
src/openvpn/options.c +1 -0
+            ciphername = cipher_kt_name(kt.cipher);

for the "not BF-CBC" case, and "ciphername = o->cipher" for "only BF-CBC"?

Call me confused...

As a side note, it seems to fail two of my t_client test cases, namely "talking to a 2.3 server with --cipher BF-CBC" and "talking to a 2.4 server with --ncp-disable", so maybe that code needs some more discussion. I have not investigated more into these failures, first want to understand what the code tries to do.

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]...
#19 lore raw

PATCH v3 1/1 Allow running a default configuration with TLS libraries without BF-CBC

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

Patch v2: add more clarifying comment, do not warn about OCC only insecure
ciphers, code improvements

Patch V3: Put ciphername resolution via ciper_kt_name in the right branch

---
 src/openvpn/crypto_backend.h |  2 ++
 src/openvpn/init.c           | 32 ++++++++++++++++++------
 src/openvpn/options.c        | 47 +++++++++++++++++++++++++++++++-----
 3 files changed, 67 insertions(+), 14 deletions(-)
src/openvpn/crypto_backend.h +2 -0
index 384ffc80..93f7e475 100644
@@ -241,6 +241,8 @@ const cipher_kt_t *cipher_kt_get(const char *ciphername);
  * The returned name is normalised to the OpenVPN config name in case the
  * name differs from the name used by the crypto library.
  *
+ * Returns [null-cipher] in case the cipher_kt is NULL.
+ *
  * @param cipher_kt     Static cipher parameters
  *
  * @return a statically allocated string describing the cipher.
src/openvpn/init.c +24 -8
index 46c933b1..5cf257eb 100644
@@ -2769,14 +2769,30 @@ do_init_crypto_tls_c1(struct context *c)
 #endif /* if P2MP */
         }
 
-        /* Do not warn if we only have BF-CBC in options->ciphername
-         * because it is still the default cipher */
-        bool warn = !streq(options->ciphername, "BF-CBC")
-             || options->enable_ncp_fallback;
-        /* Get cipher & hash algorithms */
-        init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
-                      options->keysize, true, warn);
-
+       /*
+        * BF-CBC is allowed to be used only when explicitly configured
+        * as NCP-fallback or when NCP has been disabled.
+        * In all other cases don't attempt to initialize BF-CBC as it
+        * may not even be supported by the underlying SSL library.
+        *
+        * Therefore, the key structure has to be initialized when:
+        * - any non-BF-CBC cipher was selected; or
+        * - BF-CBC is selected and NCP is disabled (explicit request to
+        *   use the BF-CBC cipher); or
+        * - BF-CBC is selected, NCP is enabled and fallback is enabled
+        *   (BF-CBC will be the fallback).
+        *
+        * Note that BF-CBC will still be part of the OCC string to retain
+        * backwards compatibility with older clients.
+        */
+        if (!streq(options->ciphername, "BF-CBC") || !options->ncp_enabled
+            || options->enable_ncp_fallback)
+        {
+            /* Do not warn if the if the cipher is used only in OCC */
+            bool warn = !options->ncp_enabled || options->enable_ncp_fallback;
+            init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
+                          options->keysize, true, warn);
+        }
         /* Initialize PRNG with config-specified digest */
         prng_init(options->prng_hash, options->prng_nonce_secret_len);
 
src/openvpn/options.c +41 -6

68 lines not on this page · raw message

Signature · 1 line
2.30.0
#20 lore raw

Re: [Openvpn-devel] [PATCH v3] Allow running a default configuration with TLS libraries without BF-CBC

Hi,

Quoted 6 lines · Arne Schwabe

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

Quoted 6 lines

Patch v2: add more clarifying comment, do not warn about OCC only insecure
ciphers, code improvements

Patch V3: Put ciphername resolution via ciper_kt_name in the right branch

This still fails one of my test cases - but only one (v2 failed two).

The test case is "udp / p2mp tun, 2.4 server with --ncp-disable" (on the server).

The client is called as
... --dev tun --proto udp --data-ciphers AES-256-GCM:AES-128-GCM:BF-CBC

(no --cipher setting) and logS

2021-02-16 20:04:51 --cipher is not set. Previous OpenVPN version defaulted to BF-CBC as fallback when cipher negotiation failed in this case. If you need this fallback please add '--data-ciphers-fallback BF-CBC' to your configuration and/or add BF-CBC to --data-ciphers.
...
2021-02-16 20:04:53 PUSH: Received control message: 'PUSH_REPLY,route 10.194.0.0255.255.0.0,route-ipv6 fd00:abcd:194::/48,tun-ipv6,route 10.194.103.1,topology net30,ping 10,ping-restart 30,ifconfig-ipv6 fd00:abcd:194:103::1000/64 fd00:abcd:194:103::1,ifconfig 10.194.103.6 10.194.103.5,peer-id 0'
2021-02-16 20:04:53 Using peer cipher 'BF-CBC'

but something seems to get confused about things:

2021-02-16 20:04:53 Initialization Sequence Completed
2021-02-16 20:05:03 Bad LZO decompression header byte: 166

The server agrees on BF-CBC (same log, different time zone):

Feb 16 14:04:51 phillip tun-udp-p2mp-2.4-noncp[29923]: 2001:608:0:814::f000:11 peer info: IV_CIPHERS=AES-256-GCM:AES-128-GCM:BF-CBC
Feb 16 14:04:51 phillip tun-udp-p2mp-2.4-noncp[29923]: 2001:608:0:814::f000:11 Outgoing Data Channel: Cipher 'BF-CBC' initialized with 128 bit key
Feb 16 14:04:51 phillip tun-udp-p2mp-2.4-noncp[29923]: 2001:608:0:814::f000:11 Incoming Data Channel: Cipher 'BF-CBC' initialized with 128 bit key

but packets fail decryption:

Feb 16 14:04:53 phillip tun-udp-p2mp-2.4-noncp[29923]: cron2-gentoo.ov-amd64/2001:608:0:814::f000:11 Authenticate/Decrypt packet error: packet HMAC authentication failed
Feb 16 14:05:06 phillip syslogd: last message repeated 103 times

Noticeable fact in the client log: there is no "Data Channel: Cipher..." line on the client, so maybe this is triggering a new corner case?

Notice 2: there is a test case talking to a 2.3 server *which succeeds*, but that one has "--cipher BF-CBC". So what fails seems to be "cipher initialization for 'implicit BF-CBC'".

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]...
#21 lore raw

PATCH v4 1/1 Allow running a default configuration with TLS libraries without BF-CBC

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

Patch v2: add more clarifying comment, do not warn about OCC only insecure
ciphers, code improvements

Patch V3: Put ciphername resolution via ciper_kt_name in the right branch

Patch V4: Fix cornercase of BF-CBC in data-ciphers not itialising cipher.

---
 src/openvpn/crypto_backend.h |  2 ++
 src/openvpn/init.c           | 32 ++++++++++++++++++------
 src/openvpn/options.c        | 47 +++++++++++++++++++++++++++++++-----
 3 files changed, 67 insertions(+), 14 deletions(-)
src/openvpn/crypto_backend.h +2 -0
index 384ffc80..93f7e475 100644
@@ -241,6 +241,8 @@ const cipher_kt_t *cipher_kt_get(const char *ciphername);
  * The returned name is normalised to the OpenVPN config name in case the
  * name differs from the name used by the crypto library.
  *
+ * Returns [null-cipher] in case the cipher_kt is NULL.
+ *
  * @param cipher_kt     Static cipher parameters
  *
  * @return a statically allocated string describing the cipher.
src/openvpn/init.c +24 -8
index 46c933b1..5cf257eb 100644
@@ -2769,14 +2769,30 @@ do_init_crypto_tls_c1(struct context *c)
 #endif /* if P2MP */
         }
 
-        /* Do not warn if we only have BF-CBC in options->ciphername
-         * because it is still the default cipher */
-        bool warn = !streq(options->ciphername, "BF-CBC")
-             || options->enable_ncp_fallback;
-        /* Get cipher & hash algorithms */
-        init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
-                      options->keysize, true, warn);
-
+       /*
+        * BF-CBC is allowed to be used only when explicitly configured
+        * as NCP-fallback or when NCP has been disabled.
+        * In all other cases don't attempt to initialize BF-CBC as it
+        * may not even be supported by the underlying SSL library.
+        *
+        * Therefore, the key structure has to be initialized when:
+        * - any non-BF-CBC cipher was selected; or
+        * - BF-CBC is selected and NCP is disabled (explicit request to
+        *   use the BF-CBC cipher); or
+        * - BF-CBC is selected, NCP is enabled and fallback is enabled
+        *   (BF-CBC will be the fallback).
+        *
+        * Note that BF-CBC will still be part of the OCC string to retain
+        * backwards compatibility with older clients.
+        */
+        if (!streq(options->ciphername, "BF-CBC") || !options->ncp_enabled
+            || options->enable_ncp_fallback)
+        {
+            /* Do not warn if the if the cipher is used only in OCC */
+            bool warn = !options->ncp_enabled || options->enable_ncp_fallback;
+            init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
+                          options->keysize, true, warn);
+        }
         /* Initialize PRNG with config-specified digest */
         prng_init(options->prng_hash, options->prng_nonce_secret_len);
 
src/openvpn/options.c +41 -6

68 lines not on this page · raw message

Signature · 1 line
2.30.0
#22 lore raw

PATCH v5 1/1 Allow running a default configuration with TLS libraries without BF-CBC

In reply to Gert Doering

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

Patch v2: add more clarifying comment, do not warn about OCC only insecure
ciphers, code improvements

Patch V3: Put ciphername resolution via ciper_kt_name in the right branch

Patch V4: Fix cornercase of BF-CBC in data-ciphers not itialising cipher.

Patch v5: I accidently resend v3 as v4. So v5 is just a resend of the real v4

---
 src/openvpn/crypto_backend.h |  2 ++
 src/openvpn/init.c           | 37 ++++++++++++++++++++++------
 src/openvpn/options.c        | 47 +++++++++++++++++++++++++++++++-----
 3 files changed, 72 insertions(+), 14 deletions(-)
src/openvpn/crypto_backend.h +2 -0
index 384ffc80..93f7e475 100644
@@ -241,6 +241,8 @@ const cipher_kt_t *cipher_kt_get(const char *ciphername);
  * The returned name is normalised to the OpenVPN config name in case the
  * name differs from the name used by the crypto library.
  *
+ * Returns [null-cipher] in case the cipher_kt is NULL.
+ *
  * @param cipher_kt     Static cipher parameters
  *
  * @return a statically allocated string describing the cipher.
src/openvpn/init.c +29 -8
index 46c933b1..cfd71482 100644
@@ -2769,14 +2769,35 @@ do_init_crypto_tls_c1(struct context *c)
 #endif /* if P2MP */
         }
 
-        /* Do not warn if we only have BF-CBC in options->ciphername
-         * because it is still the default cipher */
-        bool warn = !streq(options->ciphername, "BF-CBC")
-             || options->enable_ncp_fallback;
-        /* Get cipher & hash algorithms */
-        init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
-                      options->keysize, true, warn);
-
+       /*
+        * BF-CBC is allowed to be used only when explicitly configured
+        * as NCP-fallback or when NCP has been disabled or explicitly
+        * allowed in the in ncp_ciphers list.
+        * In all other cases do not attempt to initialize BF-CBC as it
+        * may not even be supported by the underlying SSL library.
+        *
+        * Therefore, the key structure has to be initialized when:
+        * - any non-BF-CBC cipher was selected; or
+        * - BF-CBC is selected and NCP is disabled (explicit request to
+        *   use the BF-CBC cipher); or
+        * - BF-CBC is selected, NCP is enabled and fallback is enabled
+        *   (BF-CBC will be the fallback).
+        * - BF-CBC is in data-ciphers and we negotiate to use BF-CBC:
+        *   If the negotiated cipher and options->ciphername are the
+        *   same we do not reinit the cipher
+        *
+        * Note that BF-CBC will still be part of the OCC string to retain
+        * backwards compatibility with older clients.
+        */
+        if (!streq(options->ciphername, "BF-CBC") || !options->ncp_enabled
+            || (options->ncp_enabled && tls_item_in_cipher_list("BF-CBC", options->ncp_ciphers))
+            || options->enable_ncp_fallback)
+        {
+            /* Do not warn if the if the cipher is used only in OCC */
+            bool warn = !options->ncp_enabled || options->enable_ncp_fallback;
+            init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
+                          options->keysize, true, warn);
+        }
         /* Initialize PRNG with config-specified digest */
         prng_init(options->prng_hash, options->prng_nonce_secret_len);
 
src/openvpn/options.c +41 -6

68 lines not on this page · raw message

Signature · 1 line
2.30.0
Acked-by on v5 1/1
#23 lore raw

Re: [Openvpn-devel] [PATCH v5] Allow running a default configuration with TLS libraries without BF-CBC

Hi,

Quoted 101 lines · Arne Schwabe

Modern TLS libraries might drop Blowfish by default or distributions might disable Blowfish in OpenSSL/mbed TLS. We still signal OCC options with BF-CBC compatible strings. To avoid requiring BF-CBC for this, special this one usage of BF-CBC enough to avoid a hard requirement on Blowfish in the default configuration.

Patch v2: add more clarifying comment, do not warn about OCC only insecure
ciphers, code improvements

Patch V3: Put ciphername resolution via ciper_kt_name in the right branch

Patch V4: Fix cornercase of BF-CBC in data-ciphers not itialising cipher.

Patch v5: I accidently resend v3 as v4. So v5 is just a resend of the real v4

---
 src/openvpn/crypto_backend.h |  2 ++
 src/openvpn/init.c           | 37 ++++++++++++++++++++++------
 src/openvpn/options.c        | 47 +++++++++++++++++++++++++++++++-----
 3 files changed, 72 insertions(+), 14 deletions(-)
src/openvpn/crypto_backend.h +2 -0
index 384ffc80..93f7e475 100644
@@ -241,6 +241,8 @@ const cipher_kt_t *cipher_kt_get(const char *ciphername);
  * The returned name is normalised to the OpenVPN config name in case the
  * name differs from the name used by the crypto library.
  *
+ * Returns [null-cipher] in case the cipher_kt is NULL.
+ *
  * @param cipher_kt     Static cipher parameters
  *
  * @return a statically allocated string describing the cipher.
src/openvpn/init.c +29 -8
index 46c933b1..cfd71482 100644
@@ -2769,14 +2769,35 @@ do_init_crypto_tls_c1(struct context *c)
 #endif /* if P2MP */
         }
 
-        /* Do not warn if we only have BF-CBC in options->ciphername
-         * because it is still the default cipher */
-        bool warn = !streq(options->ciphername, "BF-CBC")
-             || options->enable_ncp_fallback;
-        /* Get cipher & hash algorithms */
-        init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
-                      options->keysize, true, warn);
-
+       /*
+        * BF-CBC is allowed to be used only when explicitly configured
+        * as NCP-fallback or when NCP has been disabled or explicitly
+        * allowed in the in ncp_ciphers list.
+        * In all other cases do not attempt to initialize BF-CBC as it
+        * may not even be supported by the underlying SSL library.
+        *
+        * Therefore, the key structure has to be initialized when:
+        * - any non-BF-CBC cipher was selected; or
+        * - BF-CBC is selected and NCP is disabled (explicit request to
+        *   use the BF-CBC cipher); or
+        * - BF-CBC is selected, NCP is enabled and fallback is enabled
+        *   (BF-CBC will be the fallback).
+        * - BF-CBC is in data-ciphers and we negotiate to use BF-CBC:
+        *   If the negotiated cipher and options->ciphername are the
+        *   same we do not reinit the cipher
+        *
+        * Note that BF-CBC will still be part of the OCC string to retain
+        * backwards compatibility with older clients.
+        */
+        if (!streq(options->ciphername, "BF-CBC") || !options->ncp_enabled
+            || (options->ncp_enabled && tls_item_in_cipher_list("BF-CBC", options->ncp_ciphers))
+            || options->enable_ncp_fallback)
+        {
+            /* Do not warn if the if the cipher is used only in OCC */
+            bool warn = !options->ncp_enabled || options->enable_ncp_fallback;
+            init_key_type(&c->c1.ks.key_type, options->ciphername, options->authname,
+                          options->keysize, true, warn);
+        }
         /* Initialize PRNG with config-specified digest */
         prng_init(options->prng_hash, options->prng_nonce_secret_len);
 
src/openvpn/options.c +7 -2
index 059386b3..c02ad051 100644
@@ -3609,9 +3609,29 @@ calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
     {
         struct frame fake_frame = *frame;
         struct key_type fake_kt;
-        init_key_type(&fake_kt, o->ciphername, o->authname, o->keysize, true,
-                      false);
+
         frame_remove_from_extra_frame(&fake_frame, crypto_max_overhead());
+
+
+        /* o->ciphername might be BF-CBC even though the underlying SSL library
+         * does not support it. For this reason we workaround this corner case
+         * by pretending to have no encryption enabled and by manually adding
+         * the required packet overhead to the MTU computation.

src/openvpn/options.c calc_options_string_link_mtu(const struct options *o, const struct frame *frame) · line 3629

Quoted 10 lines
src/openvpn/options.c +10 -0
+         */
+        const char* ciphername = o->ciphername;
+
+        if (strcmp(o->ciphername, "BF-CBC") == 0)
+        {
+            /* none has no overhead, so use this to later add only --auth
+             * overhead */
+
+            /* overhead of BF-CBC: 64 bit block size, 64 bit IV size */
+            frame_add_to_extra_frame(&fake_frame, 64/8 + 64/8);

Please add space around the / operators.

Quoted 37 lines
src/openvpn/options.c +23 -3
+        }
+
+        init_key_type(&fake_kt, ciphername, o->authname, o->keysize, true,
+                      false);
+
         crypto_adjust_frame_parameters(&fake_frame, &fake_kt, o->replay,
                                        cipher_kt_mode_ofb_cfb(fake_kt.cipher));
         frame_finalize(&fake_frame, o->ce.link_mtu_defined, o->ce.link_mtu,
@@ -3781,18 +3801,33 @@ options_string(const struct options *o,
                + (TLS_SERVER == true)
                <= 1);
 
-        init_key_type(&kt, o->ciphername, o->authname, o->keysize, true,
-                      false);
+        /* Skip resolving BF-CBC to allow SSL libraries without BF-CBC
+         * to work here in the default configuration */
+        const char *ciphername = o->ciphername;
+        int keysize;
+
+        if (strcmp(o->ciphername, "BF-CBC") == 0)
+        {
+            init_key_type(&kt, "none", o->authname, o->keysize, true,
+                          false);
+            keysize = 128;
+        }
+        else
+        {
+            init_key_type(&kt, o->ciphername, o->authname, o->keysize, true,
+                          false);
+            ciphername = cipher_kt_name(kt.cipher);
+            keysize = kt.cipher_length * 8;
+        }
         /* Only announce the cipher to our peer if we are willing to
          * support it */
-        const char *ciphername = cipher_kt_name(kt.cipher);
         if (p2p_nopull || !o->ncp_enabled

src/openvpn/options.c options_string(const struct options *o, · line 3833

Quoted 10 lines
src/openvpn/options.c +1 -1
             || tls_item_in_cipher_list(ciphername, o->ncp_ciphers))
         {
             buf_printf(&out, ",cipher %s", ciphername);
         }
         buf_printf(&out, ",auth %s", md_kt_name(kt.digest));
-        buf_printf(&out, ",keysize %d", kt.cipher_length * 8);
+        buf_printf(&out, ",keysize %d", keysize);
         if (o->shared_secret_file)
         {
             buf_printf(&out, ",secret");

Other than the whitepsace complaint, this patch makes sense to me (as it did the previous version, but thanks Gert for spotting the BF-CBC problem).

It's ACK for me, but Gert's test rig will give its final verdict.

Signature · 1 line
Antonio Quartulli
Acked-by on v5 1/1
#24 lore raw

[Openvpn-devel] [PATCH applied] Re: Allow running a default configuration with TLS libraries without BF-CBC

In reply to Arne Schwabe

Taking Antonio's ACK on v3, adding my own (positive) test results on v5 to make my own ACK.

The code of v3 and v5 only differs in one line

++ || (options->ncp_enabled && tls_item_in_cipher_list("BF-CBC", options->ncp_ciphers))

which covers the special case broken in v3 (talking to a 2.4 server with --ncp-disable). Server side tests all succeed as well.

Your patch has been applied to the master branch.

commit 79ff3f79ebd98b880e77f323bd591eeb85df8411

Author: Arne Schwabe
Date:   Fri Feb 19 17:52:52 2021 +0100
     Allow running a default configuration with TLS libraries without BF-CBC

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

--
kind regards,

Gert Doering