Skip to contentSkip to conversation

Make cipher_kt_name always return normalised cipher name

PATCH In review Arne Schwabe 3 participants 8 messages Jun 5, 2020 – Jun 21, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Make cipher_kt_name always return normalised cipher name

The mbed TLS variant of the call already returned the normalised name while the OpenSSL variant did not. On top of that, all calls but one to cipher_kt_name were translate_cipher_name_to_openvpn. This commit moves the call of translate_cipher_name_to_openvpn into cipher_kt_name or avoids calling it twice in the case of mbed TLS.

The one case that did not translate_cipher_name_to_openvpn is an internal ssl_openssl.c method that should call EVP_CIPHER_name anyway.

Also simplify cipher_name_cmp function that is only used by openvpn --show-ciphers with the modified cipher_kt_name function.

---
 src/openvpn/crypto.c         |  4 ++--
 src/openvpn/crypto_backend.h |  2 ++
 src/openvpn/crypto_openssl.c | 13 +++++--------
 src/openvpn/options.c        |  3 +--
 src/openvpn/ssl_ncp.c        |  3 +--
 5 files changed, 11 insertions(+), 14 deletions(-)
src/openvpn/crypto.c +2 -2
index 2388027c..ba1fc095 100644
@@ -847,7 +847,7 @@ init_key_ctx(struct key_ctx *ctx, const struct key *key,
         cipher_ctx_init(ctx->cipher, key->cipher, kt->cipher_length,
                         kt->cipher, enc);
 
-        const char *ciphername = translate_cipher_name_to_openvpn(cipher_kt_name(kt->cipher));
+        const char *ciphername = cipher_kt_name(kt->cipher);
         msg(D_HANDSHAKE, "%s: Cipher '%s' initialized with %d bit key",
             prefix,
             ciphername,
@@ -1810,7 +1810,7 @@ print_cipher(const cipher_kt_t *cipher)
                                " by default" : "";
 
     printf("%s  (%d bit key%s, ",
-           translate_cipher_name_to_openvpn(cipher_kt_name(cipher)),
+           cipher_kt_name(cipher),
            cipher_kt_key_size(cipher) * 8, var_key_size);
 
     if (cipher_kt_block_size(cipher) == 1)
src/openvpn/crypto_backend.h +2 -0
index 1d206a8c..d46cb63f 100644
@@ -237,6 +237,8 @@ const cipher_kt_t *cipher_kt_get(const char *ciphername);
 
 /**
  * Retrieve a string describing the cipher (e.g. \c AES-128-CBC).
+ * The returned name is normalised to the OpenVPN config name in case the
+ * name differs from the name used by the crypto library.
  *
  * @param cipher_kt     Static cipher parameters
  *
src/openvpn/crypto_openssl.c +5 -8
index a5b2c45a..13ab4859 100644
@@ -266,12 +266,7 @@ cipher_name_cmp(const void *a, const void *b)
     const EVP_CIPHER *const *cipher_a = a;
     const EVP_CIPHER *const *cipher_b = b;
 
-    const char *cipher_name_a =
-        translate_cipher_name_to_openvpn(EVP_CIPHER_name(*cipher_a));
-    const char *cipher_name_b =
-        translate_cipher_name_to_openvpn(EVP_CIPHER_name(*cipher_b));
-
-    return strcmp(cipher_name_a, cipher_name_b);
+    return strcmp(cipher_kt_name(*cipher_a), cipher_kt_name(*cipher_b));
 }
 
 void
@@ -613,7 +608,9 @@ cipher_kt_name(const EVP_CIPHER *cipher_kt)
     {
         return "[null-cipher]";
     }
-    return EVP_CIPHER_name(cipher_kt);
+
+    const char *name = EVP_CIPHER_name(cipher_kt);
+    return translate_cipher_name_to_openvpn(name);
 }
 
 int
@@ -644,7 +641,7 @@ cipher_kt_block_size(const EVP_CIPHER *cipher)
 
     int block_size = EVP_CIPHER_block_size(cipher);
 
-    orig_name = cipher_kt_name(cipher);
+    orig_name = EVP_CIPHER_name(cipher);
     if (!orig_name)
     {
         goto cleanup;
src/openvpn/options.c +1 -2
index d1e68a51..ec912d34 100644
@@ -3779,8 +3779,7 @@ options_string(const struct options *o,
         init_key_type(&kt, o->ciphername, o->authname, o->keysize, true,
                       false);
 
-        buf_printf(&out, ",cipher %s",
-                   translate_cipher_name_to_openvpn(cipher_kt_name(kt.cipher)));
+        buf_printf(&out, ",cipher %s", cipher_kt_name(kt.cipher));
         buf_printf(&out, ",auth %s", md_kt_name(kt.digest));
         buf_printf(&out, ",keysize %d", kt.cipher_length * 8);
         if (o->shared_secret_file)
src/openvpn/ssl_ncp.c +1 -2
index 9ed6ff5f..042b0ce0 100644
@@ -116,8 +116,7 @@ mutate_ncp_cipher_list(const char *list, struct gc_arena *gc)
         }
         else
         {
-            const char *ovpn_cipher_name =
-                translate_cipher_name_to_openvpn(cipher_kt_name(ktc));
+            const char *ovpn_cipher_name = cipher_kt_name(ktc);
 
             if (buf_len(&new_list)> 0)
             {
Signature · 1 line
2.26.0
#1 lore raw

PATCH 2/3 Make cipher_kt_get also accept OpenVPN config cipher name

Basically calls to cipher_kt_get were calling translate_cipher_name_from_openvpn. The only two exception were the (broken) unit test and tls-crypt that uses cipher_kt_get("AES-256-CTR")

---
 src/openvpn/crypto.c         | 2 +-
 src/openvpn/crypto_backend.h | 3 ++-
 src/openvpn/crypto_mbedtls.c | 1 +
 src/openvpn/crypto_openssl.c | 1 +
 src/openvpn/ssl_ncp.c        | 8 ++++----
 5 files changed, 9 insertions(+), 6 deletions(-)
src/openvpn/crypto.c +1 -1
index ba1fc095..1ce98184 100644
@@ -763,7 +763,7 @@ init_key_type(struct key_type *kt, const char *ciphername,
     CLEAR(*kt);
     if (strcmp(ciphername, "none") != 0)
     {
-        kt->cipher = cipher_kt_get(translate_cipher_name_from_openvpn(ciphername));
+        kt->cipher = cipher_kt_get(ciphername);
         if (!kt->cipher)
         {
             msg(M_FATAL, "Cipher %s not supported", ciphername);
src/openvpn/crypto_backend.h +2 -1
index d46cb63f..85cb084a 100644
@@ -227,7 +227,8 @@ void cipher_des_encrypt_ecb(const unsigned char key[DES_KEY_LENGTH],
  * initialise encryption/decryption.
  *
  * @param ciphername    Name of the cipher to retrieve parameters for (e.g.
- *                      \c AES-128-CBC).
+ *                      \c AES-128-CBC). Will be translated to the library name
+ *                      from the openvpn config name if needed.
  *
  * @return              A statically allocated structure containing parameters
  *                      for the given cipher, or NULL if no matching parameters
src/openvpn/crypto_mbedtls.c +1 -0
index c8dcddc8..bb752557 100644
@@ -465,6 +465,7 @@ cipher_kt_get(const char *ciphername)
 
     ASSERT(ciphername);
 
+    ciphername = translate_cipher_name_from_openvpn(ciphername);
     cipher = mbedtls_cipher_info_from_string(ciphername);
 
     if (NULL == cipher)
src/openvpn/crypto_openssl.c +1 -0
index 13ab4859..bbaa5742 100644
@@ -580,6 +580,7 @@ cipher_kt_get(const char *ciphername)
 
     ASSERT(ciphername);
 
+    ciphername = translate_cipher_name_from_openvpn(ciphername);
     cipher = EVP_get_cipherbyname(ciphername);
 
     if (NULL == cipher)
src/openvpn/ssl_ncp.c +4 -4
index 042b0ce0..ea1dc960 100644
@@ -103,12 +103,12 @@ mutate_ncp_cipher_list(const char *list, struct gc_arena *gc)
     while (token)
     {
         /*
-         * Going through a roundtrip by using translate_cipher_name_from_openvpn
-         * and translate_cipher_name_to_openvpn also normalises the cipher name,
+         * Going through a roundtrip by using cipher_kt_get/cipher_kt_name
+         * (and translate_cipher_name_from_openvpn/
+         * translate_cipher_name_to_openvpn) also normalises the cipher name,
          * e.g. replacing AeS-128-gCm with AES-128-GCM
          */
-        const char *cipher_name = translate_cipher_name_from_openvpn(token);
-        const cipher_kt_t *ktc = cipher_kt_get(cipher_name);
+        const cipher_kt_t *ktc = cipher_kt_get(token);
         if (!ktc)
         {
             msg(M_WARN, "Unsupported cipher in --ncp-ciphers: %s", token);
Signature · 1 line
2.26.0
#4 lore raw

PATCH 3/3 Add unit test for cipher name translations

In reply to Arne Schwabe

The unit test duplicates some part of the test for the ncp-cipher list but that is not a bad thing.

---
 tests/unit_tests/openvpn/test_crypto.c | 68 ++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
tests/unit_tests/openvpn/test_crypto.c +68 -0
index fdf814de..ea9b99b2 100644
@@ -69,11 +69,79 @@ crypto_pem_encode_decode_loopback(void **state)
     gc_free(&gc);
 }
 
+static void
+test_translate_cipher(const char *ciphername, const char *openvpn_name)
+{
+    const cipher_kt_t *cipher = cipher_kt_get(ciphername);
+
+    /* Empty cipher is fine */
+    if (!cipher)
+    {
+        return;
+    }
+
+    const char *kt_name = cipher_kt_name(cipher);
+
+    assert_string_equal(kt_name, openvpn_name);
+}
+
+static void
+test_cipher_names(const char *ciphername, const char *openvpn_name)
+{
+    struct gc_arena gc = gc_new();
+    /* Go through some variants, if the cipher library accepts these, they
+     * should be normalised to the openvpn name */
+    char *upper = string_alloc(ciphername, &gc);
+    char *lower = string_alloc(ciphername, &gc);
+    char *random_case = string_alloc(ciphername, &gc);
+
+    for (int i = 0; i < strlen(ciphername); i++)
+    {
+        upper[i] = toupper(ciphername[i]);
+        lower[i] = tolower(ciphername[i]);
+        if (rand() & 0x1)
+        {
+            random_case[i] = upper[i];
+        }
+        else
+        {
+            random_case[i] = lower[i];
+        }
+    }
+
+    if (!openvpn_name)
+    {
+        openvpn_name = upper;
+    }
+
+    test_translate_cipher(upper, openvpn_name);
+    test_translate_cipher(lower, openvpn_name);
+    test_translate_cipher(random_case, openvpn_name);
+    test_translate_cipher(ciphername, openvpn_name);
+
+
+    gc_free(&gc);
+}
+
+static void
+crypto_translate_cipher_names(void **state)
+{
+    /* Test that a number of ciphers to see that they turn out correctly */
+    test_cipher_names("BF-CBC", NULL);
+    test_cipher_names("BLOWFISH-CBC", "BF-CBC");
+    test_cipher_names("Chacha20-Poly1305", NULL);
+    test_cipher_names("AES-128-GCM", NULL);
+    test_cipher_names("AES-128-CBC", NULL);
+    test_cipher_names("CAMELLIA-128-CFB128", "CAMELLIA-128-CFB");
+    test_cipher_names("id-aes256-GCM", "AES-256-GCM");
+}
+
 int
 main(void)
 {
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(crypto_pem_encode_decode_loopback),
+        cmocka_unit_test(crypto_translate_cipher_names),
     };
 
 #if defined(ENABLE_CRYPTO_OPENSSL)
Signature · 1 line
2.26.0
Acked-by on v1 1/3
#6 lore raw
In reply to Arne Schwabe

Hi,

Quoted 115 lines · Arne Schwabe

The mbed TLS variant of the call already returned the normalised name while the OpenSSL variant did not. On top of that, all calls but one to cipher_kt_name were translate_cipher_name_to_openvpn. This commit moves the call of translate_cipher_name_to_openvpn into cipher_kt_name or avoids calling it twice in the case of mbed TLS.

The one case that did not translate_cipher_name_to_openvpn is an internal ssl_openssl.c method that should call EVP_CIPHER_name anyway.

Also simplify cipher_name_cmp function that is only used by openvpn --show-ciphers with the modified cipher_kt_name function.

---
 src/openvpn/crypto.c         |  4 ++--
 src/openvpn/crypto_backend.h |  2 ++
 src/openvpn/crypto_openssl.c | 13 +++++--------
 src/openvpn/options.c        |  3 +--
 src/openvpn/ssl_ncp.c        |  3 +--
 5 files changed, 11 insertions(+), 14 deletions(-)
src/openvpn/crypto.c +2 -2
index 2388027c..ba1fc095 100644
@@ -847,7 +847,7 @@ init_key_ctx(struct key_ctx *ctx, const struct key *key,
         cipher_ctx_init(ctx->cipher, key->cipher, kt->cipher_length,
                         kt->cipher, enc);
 
-        const char *ciphername = translate_cipher_name_to_openvpn(cipher_kt_name(kt->cipher));
+        const char *ciphername = cipher_kt_name(kt->cipher);
         msg(D_HANDSHAKE, "%s: Cipher '%s' initialized with %d bit key",
             prefix,
             ciphername,
@@ -1810,7 +1810,7 @@ print_cipher(const cipher_kt_t *cipher)
                                " by default" : "";
 
     printf("%s  (%d bit key%s, ",
-           translate_cipher_name_to_openvpn(cipher_kt_name(cipher)),
+           cipher_kt_name(cipher),
            cipher_kt_key_size(cipher) * 8, var_key_size);
 
     if (cipher_kt_block_size(cipher) == 1)
src/openvpn/crypto_backend.h +2 -0
index 1d206a8c..d46cb63f 100644
@@ -237,6 +237,8 @@ const cipher_kt_t *cipher_kt_get(const char *ciphername);
 
 /**
  * Retrieve a string describing the cipher (e.g. \c AES-128-CBC).
+ * The returned name is normalised to the OpenVPN config name in case the
+ * name differs from the name used by the crypto library.
  *
  * @param cipher_kt     Static cipher parameters
  *
src/openvpn/crypto_openssl.c +5 -8
index a5b2c45a..13ab4859 100644
@@ -266,12 +266,7 @@ cipher_name_cmp(const void *a, const void *b)
     const EVP_CIPHER *const *cipher_a = a;
     const EVP_CIPHER *const *cipher_b = b;
 
-    const char *cipher_name_a =
-        translate_cipher_name_to_openvpn(EVP_CIPHER_name(*cipher_a));
-    const char *cipher_name_b =
-        translate_cipher_name_to_openvpn(EVP_CIPHER_name(*cipher_b));
-
-    return strcmp(cipher_name_a, cipher_name_b);
+    return strcmp(cipher_kt_name(*cipher_a), cipher_kt_name(*cipher_b));
 }
 
 void
@@ -613,7 +608,9 @@ cipher_kt_name(const EVP_CIPHER *cipher_kt)
     {
         return "[null-cipher]";
     }
-    return EVP_CIPHER_name(cipher_kt);
+
+    const char *name = EVP_CIPHER_name(cipher_kt);
+    return translate_cipher_name_to_openvpn(name);
 }
 
 int
@@ -644,7 +641,7 @@ cipher_kt_block_size(const EVP_CIPHER *cipher)
 
     int block_size = EVP_CIPHER_block_size(cipher);
 
-    orig_name = cipher_kt_name(cipher);
+    orig_name = EVP_CIPHER_name(cipher);
     if (!orig_name)
     {
         goto cleanup;
src/openvpn/options.c +1 -2
index d1e68a51..ec912d34 100644
@@ -3779,8 +3779,7 @@ options_string(const struct options *o,
         init_key_type(&kt, o->ciphername, o->authname, o->keysize, true,
                       false);
 
-        buf_printf(&out, ",cipher %s",
-                   translate_cipher_name_to_openvpn(cipher_kt_name(kt.cipher)));
+        buf_printf(&out, ",cipher %s", cipher_kt_name(kt.cipher));
         buf_printf(&out, ",auth %s", md_kt_name(kt.digest));
         buf_printf(&out, ",keysize %d", kt.cipher_length * 8);
         if (o->shared_secret_file)
src/openvpn/ssl_ncp.c +0 -0
index 9ed6ff5f..042b0ce0 100644

src/openvpn/ssl_ncp.c mutate_ncp_cipher_list(const char *list, struct gc_arena *gc) · line 122

Quoted 9 lines
src/openvpn/ssl_ncp.c +1 -2
@@ -116,8 +116,7 @@ mutate_ncp_cipher_list(const char *list, struct gc_arena *gc)
         }
         else
         {
-            const char *ovpn_cipher_name =
-                translate_cipher_name_to_openvpn(cipher_kt_name(ktc));
+            const char *ovpn_cipher_name = cipher_kt_name(ktc);
 
             if (buf_len(&new_list)> 0)
             {

Makes sense, changes look good and passes my tests.

-Steffan

Acked-by on v1 2/3
#2 lore raw

Re: [Openvpn-devel] [PATCH 2/3] Make cipher_kt_get also accept OpenVPN config cipher name

In reply to Arne Schwabe

Hi,

Quoted 76 lines · Arne Schwabe

Basically calls to cipher_kt_get were calling translate_cipher_name_from_openvpn. The only two exception were the (broken) unit test and tls-crypt that uses cipher_kt_get("AES-256-CTR")

---
 src/openvpn/crypto.c         | 2 +-
 src/openvpn/crypto_backend.h | 3 ++-
 src/openvpn/crypto_mbedtls.c | 1 +
 src/openvpn/crypto_openssl.c | 1 +
 src/openvpn/ssl_ncp.c        | 8 ++++----
 5 files changed, 9 insertions(+), 6 deletions(-)
src/openvpn/crypto.c +1 -1
index ba1fc095..1ce98184 100644
@@ -763,7 +763,7 @@ init_key_type(struct key_type *kt, const char *ciphername,
     CLEAR(*kt);
     if (strcmp(ciphername, "none") != 0)
     {
-        kt->cipher = cipher_kt_get(translate_cipher_name_from_openvpn(ciphername));
+        kt->cipher = cipher_kt_get(ciphername);
         if (!kt->cipher)
         {
             msg(M_FATAL, "Cipher %s not supported", ciphername);
src/openvpn/crypto_backend.h +2 -1
index d46cb63f..85cb084a 100644
@@ -227,7 +227,8 @@ void cipher_des_encrypt_ecb(const unsigned char key[DES_KEY_LENGTH],
  * initialise encryption/decryption.
  *
  * @param ciphername    Name of the cipher to retrieve parameters for (e.g.
- *                      \c AES-128-CBC).
+ *                      \c AES-128-CBC). Will be translated to the library name
+ *                      from the openvpn config name if needed.
  *
  * @return              A statically allocated structure containing parameters
  *                      for the given cipher, or NULL if no matching parameters
src/openvpn/crypto_mbedtls.c +1 -0
index c8dcddc8..bb752557 100644
@@ -465,6 +465,7 @@ cipher_kt_get(const char *ciphername)
 
     ASSERT(ciphername);
 
+    ciphername = translate_cipher_name_from_openvpn(ciphername);
     cipher = mbedtls_cipher_info_from_string(ciphername);
 
     if (NULL == cipher)
src/openvpn/crypto_openssl.c +1 -0
index 13ab4859..bbaa5742 100644
@@ -580,6 +580,7 @@ cipher_kt_get(const char *ciphername)
 
     ASSERT(ciphername);
 
+    ciphername = translate_cipher_name_from_openvpn(ciphername);
     cipher = EVP_get_cipherbyname(ciphername);
 
     if (NULL == cipher)
src/openvpn/ssl_ncp.c +1 -2
index 042b0ce0..ea1dc960 100644
@@ -103,12 +103,12 @@ mutate_ncp_cipher_list(const char *list, struct gc_arena *gc)
     while (token)
     {
         /*
-         * Going through a roundtrip by using translate_cipher_name_from_openvpn
-         * and translate_cipher_name_to_openvpn also normalises the cipher name,
+         * Going through a roundtrip by using cipher_kt_get/cipher_kt_name

src/openvpn/ssl_ncp.c mutate_ncp_cipher_list(const char *list, struct gc_arena *gc) · line 114

Quoted 10 lines
src/openvpn/ssl_ncp.c +3 -2
+         * (and translate_cipher_name_from_openvpn/
+         * translate_cipher_name_to_openvpn) also normalises the cipher name,
          * e.g. replacing AeS-128-gCm with AES-128-GCM
          */
-        const char *cipher_name = translate_cipher_name_from_openvpn(token);
-        const cipher_kt_t *ktc = cipher_kt_get(cipher_name);
+        const cipher_kt_t *ktc = cipher_kt_get(token);
         if (!ktc)
         {
             msg(M_WARN, "Unsupported cipher in --ncp-ciphers: %s", token);

This makes the cipher_kt_name and cipher_kt_get abstractions nicely consistent. Code looks good, and passes my tests.

-Steffan

#7 lore raw
In reply to Arne Schwabe

Your patch has been applied to the master branch.

commit ff531767eafed263d7bd8243138fbb276215000d

Author: Arne Schwabe
Date:   Fri Jun 5 13:25:17 2020 +0200
     Make cipher_kt_name always return normalised cipher name

     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]/msg19970.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#3 lore raw

[Openvpn-devel] [PATCH applied] Re: Make cipher_kt_get also accept OpenVPN config cipher name

In reply to Arne Schwabe

Your patch has been applied to the master branch.

commit 58bb8f3e146318a4e5c007fbe9c2b0e8cecf7a30

Author: Arne Schwabe
Date:   Fri Jun 5 13:25:18 2020 +0200
     Make cipher_kt_get also accept OpenVPN config cipher name

     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]/msg19969.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v1 3/3
#5 lore raw

[Openvpn-devel] [PATCH applied] Re: Add unit test for cipher name translations

In reply to Arne Schwabe

"It works"...

[==========] Running 2 test(s).
[ RUN ] crypto_pem_encode_decode_loopback
[ OK ] crypto_pem_encode_decode_loopback
[ RUN ] crypto_translate_cipher_names
[ OK ] crypto_translate_cipher_names
[==========] 2 test(s) run.
[ PASSED ] 2 test(s).
PASS: crypto_testdriver

Tested on:

Linux with mbedTLS 2.22.0-r1
Linux with OpenSSL 1.1.1
FreeBSD 11.3 with OpenSSL 1.0.2s
FreeBSD 12.1 with OpenSSL 1.1.1d

Your patch has been applied to the master branch.

commit 3bc12aefd571ab24543cff4cba2f0e25d3268941

Author: Arne Schwabe
Date:   Fri Jun 5 13:25:19 2020 +0200
     Add unit test for cipher name translations

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

--
kind regards,

Gert Doering