Skip to contentSkip to conversation

Use new EVP_MAC API for HMAC implementation

PATCH Superseded Arne Schwabe 2 participants 9 messages Sep 19, 2021 – Oct 12, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/8 Use new EVP_MAC API for HMAC implementation

The old API is deprecated in OpenSSL 3.0 and the new API does not yet exist in OpenSSL 1.1. Emulating the new API would be more complex than just having two implementations. So this switches to a new hmac implementation for OpenSSL 3.0.

---
 src/openvpn/crypto_backend.h |  2 +-
 src/openvpn/crypto_mbedtls.c |  2 +-
 src/openvpn/crypto_openssl.c | 82 +++++++++++++++++++++++++++++++++++-
 src/openvpn/crypto_openssl.h |  4 ++
 4 files changed, 86 insertions(+), 4 deletions(-)
src/openvpn/crypto_backend.h +1 -1
index e9447f82f..e0bfdf585 100644
@@ -643,7 +643,7 @@ void hmac_ctx_cleanup(hmac_ctx_t *ctx);
  *
  * @return              Size of the HMAC, or \0 if ctx is NULL.
  */
-int hmac_ctx_size(const hmac_ctx_t *ctx);
+int hmac_ctx_size(hmac_ctx_t *ctx);
 
 /*
  * Resets the given HMAC context, preserving the associated key information
src/openvpn/crypto_mbedtls.c +1 -1
index c632849db..e2f5f4012 100644
@@ -939,7 +939,7 @@ hmac_ctx_cleanup(mbedtls_md_context_t *ctx)
 }
 
 int
-hmac_ctx_size(const mbedtls_md_context_t *ctx)
+hmac_ctx_size(mbedtls_md_context_t *ctx)
 {
     if (NULL == ctx)
     {
src/openvpn/crypto_openssl.c +80 -2

100 lines not on this page · raw message

src/openvpn/crypto_openssl.h +4 -0
index 59a31aacf..bafb3a245 100644
@@ -47,7 +47,11 @@ typedef EVP_CIPHER_CTX cipher_ctx_t;
 typedef EVP_MD_CTX md_ctx_t;
 
 /** Generic HMAC %context. */
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
 typedef HMAC_CTX hmac_ctx_t;
+#else
+typedef EVP_MAC_CTX hmac_ctx_t;
+#endif
 
 /** Maximum length of an IV */
 #define OPENVPN_MAX_IV_LENGTH   EVP_MAX_IV_LENGTH
Signature · 1 line
2.33.0
#1 lore raw

PATCH 2/8 Disable engine support for OpenSSL 3.0

Engine support is deprecated in OpenSSL 3.0. No longer use the deprecated API when running with OpenSSL 3.0

---
 src/openvpn/crypto_openssl.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
src/openvpn/crypto_openssl.c +7 -6
index f1b2d8b4a..34a564e46 100644
@@ -67,7 +67,7 @@
 #warning Some OpenSSL HMAC message digests now support key lengths greater than MAX_HMAC_KEY_LENGTH -- consider increasing MAX_HMAC_KEY_LENGTH
 #endif
 
-#if HAVE_OPENSSL_ENGINE
+#if HAVE_OPENSSL_ENGINE && OPENSSL_VERSION_NUMBER < 0x30000000L
 #include <openssl/ui.h>
 #include <openssl/engine.h>
 
@@ -132,7 +132,7 @@ setup_engine(const char *engine)
 void
 crypto_init_lib_engine(const char *engine_name)
 {
-#if HAVE_OPENSSL_ENGINE
+#if HAVE_OPENSSL_ENGINE && OPENSSL_VERSION_NUMBER < 0x30000000L
     if (!engine_initialized)
     {
         ASSERT(engine_name);
@@ -182,7 +182,7 @@ crypto_uninit_lib(void)
     fclose(fp);
 #endif
 
-#if HAVE_OPENSSL_ENGINE
+#if HAVE_OPENSSL_ENGINE && OPENSSL_VERSION_NUMBER < 0x30000000L
     if (engine_initialized)
     {
         ENGINE_cleanup();
@@ -368,7 +368,8 @@ show_available_digests(void)
 void
 show_available_engines(void)
 {
-#if HAVE_OPENSSL_ENGINE /* Only defined for OpenSSL */
+#if HAVE_OPENSSL_ENGINE && OPENSSL_VERSION_NUMBER < 0x30000000L
+    /* Only defined for OpenSSL */
     ENGINE *e;
 
     printf("OpenSSL Crypto Engines\n\n");
@@ -1151,7 +1152,7 @@ memcmp_constant_time(const void *a, const void *b, size_t size)
     return CRYPTO_memcmp(a, b, size);
 }
 
-#if HAVE_OPENSSL_ENGINE
+#if HAVE_OPENSSL_ENGINE && OPENSSL_VERSION_NUMBER < 0x30000000L
 static int
 ui_reader(UI *ui, UI_STRING *uis)
 {
@@ -1175,7 +1176,7 @@ ui_reader(UI *ui, UI_STRING *uis)
 EVP_PKEY *
 engine_load_key(const char *file, SSL_CTX *ctx)
 {
-#if HAVE_OPENSSL_ENGINE
+#if HAVE_OPENSSL_ENGINE && OPENSSL_VERSION_NUMBER < 0x30000000L
     UI_METHOD *ui;
     EVP_PKEY *pkey;
 
Signature · 1 line
2.33.0
#2 lore raw

PATCH 3/8 Implement DES ECB encrypt via EVP_CIPHER api

In reply to Arne Schwabe

Even though DES is super outdated and also NTLM is super outdated, eliminating the warnings for OpenSSL 3.0 is still a step in the right direction and using the correct APIs.

---
 src/openvpn/crypto_openssl.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
src/openvpn/crypto_openssl.c +19 -3
index 34a564e46..b4c59557b 100644
@@ -880,10 +880,26 @@ cipher_des_encrypt_ecb(const unsigned char key[DES_KEY_LENGTH],
                        unsigned char src[DES_KEY_LENGTH],
                        unsigned char dst[DES_KEY_LENGTH])
 {
-    DES_key_schedule sched;
+    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
+    if (!ctx)
+    {
+        crypto_msg(M_FATAL, "%s: EVP_CIPHER_CTX_new() failed", __func__);
+    }
+    if (!EVP_EncryptInit_ex(ctx, EVP_bf_ecb(), NULL, key, 0))
+    {
+        crypto_msg(M_FATAL, "%s: EVP_EncryptInit_ex() failed", __func__);
+    }
 
-    DES_set_key_unchecked((DES_cblock *)key, &sched);
-    DES_ecb_encrypt((DES_cblock *)src, (DES_cblock *)dst, &sched, DES_ENCRYPT);
+    int len;
+    if(!EVP_EncryptUpdate(ctx, dst, &len, src, DES_KEY_LENGTH))
+    {
+        crypto_msg(M_FATAL, "%s: EVP_EncryptUpdate() failed", __func__);
+    }
+
+    if (!EVP_EncryptFinal(ctx, dst + len, &len))
+    {
+        crypto_msg(M_FATAL, "%s: EVP_EncryptFinal() failed", __func__);
+    }
 }
 
 /*
Signature · 1 line
2.33.0
#3 lore raw

PATCH 4/8 Remove DES check with OpenSSL 3.0

In reply to Arne Schwabe

DES is very deprecated and accidently getting on the of the 16 insecure keys that OpenSSL checks is extremely unlikely so we no longer use the deprecated functions without replacement in OpenSSL 3.0.

---
 src/openvpn/crypto_openssl.c | 8 ++++++++
 1 file changed, 8 insertions(+)
src/openvpn/crypto_openssl.c +8 -0
index b4c59557b..9df6da02c 100644
@@ -522,6 +522,11 @@ key_des_num_cblocks(const EVP_CIPHER *kt)
 bool
 key_des_check(uint8_t *key, int key_len, int ndc)
 {
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+    /* DES is deprecated and the method to even check the keys is deprecated
+     * in OpenSSL 3.0. Instead of checking for the 16 weak/semi-weak keys
+     * we just accept them in OpenSSL 3.0 since the risk of randomly getting
+     * these is pretty weak */
     int i;
     struct buffer b;
 
@@ -554,6 +559,9 @@ key_des_check(uint8_t *key, int key_len, int ndc)
 err:
     ERR_clear_error();
     return false;
+#else
+    return true;
+#endif
 }
 
 void
Signature · 1 line
2.33.0
#4 lore raw

PATCH 5/8 Use EVP_PKEY based API for loading DH keys

In reply to Arne Schwabe

OpenSSL 3.0 replaces the DH API with a generic EVP_KEY based API to load DH parameters.

---
 src/openvpn/ssl_openssl.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
src/openvpn/ssl_openssl.c +21 -2
index 241206fb2..d8ac25fbc 100644
@@ -645,7 +645,6 @@ void
 tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file,
                        bool dh_file_inline)
 {
-    DH *dh;
     BIO *bio;
 
     ASSERT(NULL != ctx);
@@ -666,7 +665,26 @@ tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file,
         }
     }
 
-    dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+    EVP_PKEY *dh = PEM_read_bio_Parameters(bio, NULL);
+    BIO_free(bio);
+
+    if (!dh)
+    {
+        crypto_msg(M_FATAL, "Cannot load DH parameters from %s",
+                   print_key_filename(dh_file, dh_file_inline));
+    }
+    if (!SSL_CTX_set0_tmp_dh_pkey(ctx->ctx, dh))
+    {
+        crypto_msg(M_FATAL, "SSL_CTX_set_tmp_dh");
+    }
+
+    msg(D_TLS_DEBUG_LOW, "Diffie-Hellman initialized with %d bit key",
+        8 * EVP_PKEY_get_size(dh));
+
+    EVP_PKEY_free(dh);
+#else
+    DH *dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
     BIO_free(bio);
 
     if (!dh)
@@ -683,6 +701,7 @@ tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file,
         8 * DH_size(dh));
 
     DH_free(dh);
+#endif
 }
 
 void
Signature · 1 line
2.33.0
#5 lore raw

PATCH 6/8 Deprecate --ecdh-curve with OpenSSL 3.0 and adjust mbed TLS message

In reply to Arne Schwabe

OpenSSL 3.0 deprecates SSL_CTX_set_tmp_ecdh() in favour of SSL_CTX_set1_groups(3). We already support the SSL_CTX_set1_groups using the --tls-groups. Adjust both mbed TLS and OpenSSL 3.0 to say that --ecdh-curve is ingored and --tls-groups should be used.

---
 src/openvpn/ssl_mbedtls.c |  5 +++--
 src/openvpn/ssl_openssl.c | 12 +++++++++---
 2 files changed, 12 insertions(+), 5 deletions(-)
src/openvpn/ssl_mbedtls.c +3 -2
index cea88f41e..e7c45c099 100644
@@ -440,8 +440,9 @@ tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name
 {
     if (NULL != curve_name)
     {
-        msg(M_WARN, "WARNING: mbed TLS builds do not support specifying an ECDH "
-            "curve, using default curves.");
+        msg(M_WARN, "WARNING: mbed TLS builds do not support specifying an "
+            "ECDH curve with --ecdh-curve, using default curves. Use "
+            "--tls-groups to specify curves.");
     }
 }
 
src/openvpn/ssl_openssl.c +9 -3
index d8ac25fbc..68cdb880c 100644
@@ -705,10 +705,16 @@ tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file,
 }
 
 void
-tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name
-                         )
+tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name)
 {
-#ifndef OPENSSL_NO_EC
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+    if (curve_name != NULL)
+    {
+        msg(M_WARN, "WARNING: OpenSSL 3.0+ builds do not support specifying an "
+                    "ECDH curve with --ecdh-curve, using default curves. Use "
+                    "--tls-groups to specify groups.");
+    }
+#elif !defined(OPENSSL_NO_EC)
     int nid = NID_undef;
     EC_KEY *ecdh = NULL;
     const char *sname = NULL;
Signature · 1 line
2.33.0
#6 lore raw

PATCH 7/8 Remove DES key fixup code

In reply to Arne Schwabe

This code mainly sets the parity bits in the DES keys. As mbed TLS and OpenSSL already ignore these bits in the DES key and since DES is deprecated, remove this special DES code that is not even needed by the libraries.

---
 src/openvpn/crypto.c         | 46 ------------------------------------
 src/openvpn/crypto.h         |  2 --
 src/openvpn/crypto_backend.h |  9 -------
 src/openvpn/crypto_mbedtls.c | 19 ---------------
 src/openvpn/crypto_openssl.c | 21 ----------------
 src/openvpn/ntlm.c           |  1 -
 src/openvpn/ssl.c            | 18 --------------
 7 files changed, 116 deletions(-)
src/openvpn/crypto.c +0 -46
index 1dfc760f9..ce041153f 100644
@@ -956,45 +956,6 @@ check_key(struct key *key, const struct key_type *kt)
     return true;
 }
 
-/*
- * Make safe mutations to key to ensure it is valid,
- * such as ensuring correct parity on DES keys.
- *
- * This routine cannot guarantee it will generate a good
- * key.  You must always call check_key after this routine
- * to make sure.
- */
-void
-fixup_key(struct key *key, const struct key_type *kt)
-{
-    struct gc_arena gc = gc_new();
-    if (kt->cipher)
-    {
-#ifdef ENABLE_DEBUG
-        const struct key orig = *key;
-#endif
-        const int ndc = key_des_num_cblocks(kt->cipher);
-
-        if (ndc)
-        {
-            key_des_fixup(key->cipher, kt->cipher_length, ndc);
-        }
-
-#ifdef ENABLE_DEBUG
-        if (check_debug_level(D_CRYPTO_DEBUG))
-        {
-            if (memcmp(orig.cipher, key->cipher, kt->cipher_length))
-            {
-                dmsg(D_CRYPTO_DEBUG, "CRYPTO INFO: fixup_key: before=%s after=%s",
-                     format_hex(orig.cipher, kt->cipher_length, 0, &gc),
-                     format_hex(key->cipher, kt->cipher_length, 0, &gc));
-            }
-        }
-#endif
-    }
-    gc_free(&gc);
-}
-
 void
 check_replay_consistency(const struct key_type *kt, bool packet_id)
 {
@@ -1043,10 +1004,6 @@ generate_key_random(struct key *key, const struct key_type *kt)
         dmsg(D_SHOW_KEY_SOURCE, "Cipher source entropy: %s", format_hex(key->cipher, cipher_len, 0, &gc));
         dmsg(D_SHOW_KEY_SOURCE, "HMAC source entropy: %s", format_hex(key->hmac, hmac_len, 0, &gc));
 
-        if (kt)
-        {
-            fixup_key(key, kt);
-        }
     } while (kt && !check_key(key, kt));
 
     gc_free(&gc);
@@ -1589,9 +1546,6 @@ verify_fix_key2(struct key2 *key2, const struct key_type *kt, const char *shared
 
     for (i = 0; i < key2->n; ++i)
     {
-        /* Fix parity for DES keys and make sure not a weak key */
-        fixup_key(&key2->keys[i], kt);
-
         /* This should be a very improbable failure */
         if (!check_key(&key2->keys[i], kt))
         {
src/openvpn/crypto.h +0 -2
index 759da4bfb..e9ba21ab2 100644
@@ -288,8 +288,6 @@ void check_replay_consistency(const struct key_type *kt, bool packet_id);
 
 bool check_key(struct key *key, const struct key_type *kt);
 
-void fixup_key(struct key *key, const struct key_type *kt);
-
 bool write_key(const struct key *key, const struct key_type *kt,
                struct buffer *buf);
 
src/openvpn/crypto_backend.h +0 -9
index e0bfdf585..cc897acf4 100644
@@ -170,15 +170,6 @@ int key_des_num_cblocks(const cipher_kt_t *kt);
  */
 bool key_des_check(uint8_t *key, int key_len, int ndc);
 
-/*
- * Fix the given DES key, setting its parity to odd.
- *
- * @param key           Key to check
- * @param key_len       Length of the key, in bytes
- * @param ndc           Number of DES cblocks that the key is made up of.
- */
-void key_des_fixup(uint8_t *key, int key_len, int ndc);
-
 /**
  * Encrypt the given block, using DES ECB mode
  *
src/openvpn/crypto_mbedtls.c +0 -19
index e2f5f4012..2c4a1405c 100644
@@ -434,25 +434,6 @@ err:
     return false;
 }
 
-void
-key_des_fixup(uint8_t *key, int key_len, int ndc)
-{
-    int i;
-    struct buffer b;
-
-    buf_set_read(&b, key, key_len);
-    for (i = 0; i < ndc; ++i)
-    {
-        unsigned char *key = buf_read_alloc(&b, MBEDTLS_DES_KEY_SIZE);
-        if (!key)
-        {
-            msg(D_CRYPT_ERRORS, "CRYPTO INFO: fixup_key_DES: insufficient key material");
-            return;
-        }
-        mbedtls_des_key_set_parity(key);
-    }
-}
-
 /*
  *
  * Generic cipher key type functions
src/openvpn/crypto_openssl.c +0 -21
index 9df6da02c..8637be86d 100644
@@ -564,27 +564,6 @@ err:
 #endif
 }
 
-void
-key_des_fixup(uint8_t *key, int key_len, int ndc)
-{
-    int i;
-    struct buffer b;
-
-    buf_set_read(&b, key, key_len);
-    for (i = 0; i < ndc; ++i)
-    {
-        DES_cblock *dc = (DES_cblock *) buf_read_alloc(&b, sizeof(DES_cblock));
-        if (!dc)
-        {
-            msg(D_CRYPT_ERRORS, "CRYPTO INFO: fixup_key_DES: insufficient key material");
-            ERR_clear_error();
-            return;
-        }
-        DES_set_odd_parity(dc);
-    }
-}
-
-
 /*
  *
  * Generic cipher key type functions
src/openvpn/ntlm.c +0 -1
index 3abe3b7e3..28e68ded5 100644
@@ -67,7 +67,6 @@ create_des_keys(const unsigned char *hash, unsigned char *key)
     key[5] = ((hash[4] & 31) << 3) | (hash[5] >> 5);
     key[6] = ((hash[5] & 63) << 2) | (hash[6] >> 6);
     key[7] = ((hash[6] & 127) << 1);
-    key_des_fixup(key, 8, 1);
 }
 
 static void
src/openvpn/ssl.c +0 -18
index b2dc48be2..ee416a64c 100644
@@ -1739,24 +1739,6 @@ generate_key_expansion_openvpn_prf(const struct tls_session *session, struct key
     }
     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;
 
     return true;
Signature · 1 line
2.33.0
#7 lore raw

PATCH 8/8 Use EVP_PKEY_get_group_name to query group name

In reply to Arne Schwabe

EC_Key methods are deprecated in OpenSSL 3.0. Use EVP_PKEY_get_group_name instead to query the EC group name from an EVP_PKEY and add a compatibility function for older OpenSSL versions.

---
 src/openvpn/openssl_compat.h | 32 ++++++++++++++++++++++++++++++++
 src/openvpn/ssl_openssl.c    | 14 ++++++++------
 2 files changed, 40 insertions(+), 6 deletions(-)
src/openvpn/openssl_compat.h +32 -0
index ce8e2b360..933a71848 100644
@@ -46,6 +46,38 @@
 #include <openssl/ssl.h>
 #include <openssl/x509.h>
 
+/* Functionality missing in 1.1.1 */
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+
+/* Note that this is not a perfect emulation of the new function but
+ * is good enough for our case of printing certificate details during
+ * handshake */
+static inline
+int EVP_PKEY_get_group_name(EVP_PKEY *pkey, char *gname, size_t gname_sz,
+                            size_t *gname_len)
+                            {
+    if ((EVP_PKEY_get0_EC_KEY(pkey) == NULL ||
+        EVP_PKEY_get0_EC_KEY(pkey) != NULL))
+    {
+        return 0;
+    }
+    const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(pkey);
+    const EC_GROUP* group = EC_KEY_get0_group(ec);
+
+    int nid = EC_GROUP_get_curve_name(group);
+
+    if (nid != 0)
+    {
+        return 0;
+    }
+    const char *curve = OBJ_nid2sn(nid);
+
+    strncpy(gname, curve, gname_sz);
+    *gname_len = min_int(strlen(curve), gname_sz);
+    return 1;
+}
+#endif
+
 /* Functionality missing in 1.1.0 */
 #if OPENSSL_VERSION_NUMBER < 0x10101000L && !defined(ENABLE_CRYPTO_WOLFSSL)
 #define SSL_CTX_set1_groups SSL_CTX_set1_curves
src/openvpn/ssl_openssl.c +8 -6
index 68cdb880c..dc0ae20a7 100644
@@ -2049,13 +2049,15 @@ print_cert_details(X509 *cert, char *buf, size_t buflen)
     int typeid = EVP_PKEY_id(pkey);
 
 #ifndef OPENSSL_NO_EC
-    if (typeid == EVP_PKEY_EC && EVP_PKEY_get0_EC_KEY(pkey) != NULL)
+    char groupname[256];
+    if (typeid == EVP_PKEY_EC)
     {
-        const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
-        const EC_GROUP *group = EC_KEY_get0_group(ec);
-
-        int nid = EC_GROUP_get_curve_name(group);
-        if (nid == 0 || (curve = OBJ_nid2sn(nid)) == NULL)
+        size_t len;
+        if(EVP_PKEY_get_group_name(pkey, groupname, sizeof(groupname), &len))
+        {
+           curve = groupname;
+        }
+        else
         {
             curve = "(error getting curve name)";
         }
Signature · 1 line
2.33.0
#8 lore raw

Re: [Openvpn-devel] [PATCH 8/8] [OSSL 3.0] Use EVP_PKEY_get_group_name to query group name

Hi,

Looking at the easier ones in this set:

Quoted 14 lines · Arne Schwabe

EC_Key methods are deprecated in OpenSSL 3.0. Use EVP_PKEY_get_group_name instead to query the EC group name from an EVP_PKEY and add a compatibility function for older OpenSSL versions.

---
 src/openvpn/openssl_compat.h | 32 ++++++++++++++++++++++++++++++++
 src/openvpn/ssl_openssl.c    | 14 ++++++++------
 2 files changed, 40 insertions(+), 6 deletions(-)
src/openvpn/openssl_compat.h +0 -0
index ce8e2b360..933a71848 100644

src/openvpn/openssl_compat.h line 50

Quoted 5 lines
src/openvpn/openssl_compat.h +2 -0
@@ -46,6 +46,38 @@
 #include <openssl/ssl.h>
 #include <openssl/x509.h>
 
+/* Functionality missing in 1.1.1 */
+#if OPENSSL_VERSION_NUMBER < 0x30000000L

&& !defined(OPENSSL_NO_EC)

src/openvpn/openssl_compat.h line 60

Quoted 10 lines
src/openvpn/openssl_compat.h +10 -0
+
+/* Note that this is not a perfect emulation of the new function but
+ * is good enough for our case of printing certificate details during
+ * handshake */
+static inline
+int EVP_PKEY_get_group_name(EVP_PKEY *pkey, char *gname, size_t gname_sz,
+                            size_t *gname_len)
+                            {
+    if ((EVP_PKEY_get0_EC_KEY(pkey) == NULL ||
+        EVP_PKEY_get0_EC_KEY(pkey) != NULL))

both == NULL and != NULL on the same value? See also below

src/openvpn/openssl_compat.h line 64

Quoted 4 lines
src/openvpn/openssl_compat.h +4 -0
+    {
+        return 0;
+    }
+    const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(pkey);

I think you want to remove the 5 lines before this and add here

if (ec == NULL) {
    return 0;
}

src/openvpn/openssl_compat.h line 69

Quoted 5 lines
src/openvpn/openssl_compat.h +5 -0
+    const EC_GROUP* group = EC_KEY_get0_group(ec);
+
+    int nid = EC_GROUP_get_curve_name(group);
+
+    if (nid != 0)

You mean nid == 0 ?

src/openvpn/openssl_compat.h line 73

Quoted 4 lines
src/openvpn/openssl_compat.h +4 -0
+    {
+        return 0;
+    }
+    const char *curve = OBJ_nid2sn(nid);

Add a check for curve != NULL

src/openvpn/openssl_compat.h line 75

Quoted 2 lines
src/openvpn/openssl_compat.h +2 -0
+
+    strncpy(gname, curve, gname_sz);

strncpynt()

src/openvpn/openssl_compat.h line 76

Quoted 1 line
src/openvpn/openssl_compat.h +1 -0
+    *gname_len = min_int(strlen(curve), gname_sz);

gname_sz -> gname_sz - 1 to be consistent with strlen(curve)

Quoted 23 lines
src/openvpn/openssl_compat.h +4 -0
+    return 1;
+}
+#endif
+
 /* Functionality missing in 1.1.0 */
 #if OPENSSL_VERSION_NUMBER < 0x10101000L && !defined(ENABLE_CRYPTO_WOLFSSL)
 #define SSL_CTX_set1_groups SSL_CTX_set1_curves
src/openvpn/ssl_openssl.c +2 -5
index 68cdb880c..dc0ae20a7 100644
@@ -2049,13 +2049,15 @@ print_cert_details(X509 *cert, char *buf, size_t buflen)
     int typeid = EVP_PKEY_id(pkey);
 
 #ifndef OPENSSL_NO_EC
-    if (typeid == EVP_PKEY_EC && EVP_PKEY_get0_EC_KEY(pkey) != NULL)
+    char groupname[256];
+    if (typeid == EVP_PKEY_EC)
     {
-        const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
-        const EC_GROUP *group = EC_KEY_get0_group(ec);
-
-        int nid = EC_GROUP_get_curve_name(group);

src/openvpn/ssl_openssl.c print_cert_details(X509 *cert, char *buf, size_t buflen) · line 2063

Quoted 10 lines
src/openvpn/ssl_openssl.c +6 -1
-        if (nid == 0 || (curve = OBJ_nid2sn(nid)) == NULL)
+        size_t len;
+        if(EVP_PKEY_get_group_name(pkey, groupname, sizeof(groupname), &len))
+        {
+           curve = groupname;
+        }
+        else
         {
             curve = "(error getting curve name)";
         }

Selva