Skip to contentSkip to conversation

openssl: Fix compilation without deprecated OpenSSL 1.1 APIs

PATCH Superseded Rosen Penev 5 participants 15 messages Apr 3, 2019 – Oct 28, 2019 1 message not in the archive

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 openssl: Fix compilation without deprecated OpenSSL 1.1 APIs

EVP_CIPHER_CTX_init and _cleanup were deprecated in 1.1 and both were replaced with _reset.

Also removed initialization with OpenSSL 1.1 as it is no longer needed and causes compilation errors when disabling deprecated APIs.

Same with SSL_CTX_set_ecdh_auto as it got removed.

---
v2: Squashed previous patches together.

 configure.ac                 |  4 ++++
 src/openvpn/openssl_compat.h | 16 ++++++++++++++++
 src/openvpn/ssl_openssl.c    | 18 ++++++++++++------
 3 files changed, 32 insertions(+), 6 deletions(-)
configure.ac +4 -0
index dfb268ca..891799ea 100644
@@ -918,10 +918,14 @@ if test "${with_crypto_library}" = "openssl"; then
 			EVP_MD_CTX_new \
 			EVP_MD_CTX_free \
 			EVP_MD_CTX_reset \
+			EVP_CIPHER_CTX_init \
+			EVP_CIPHER_CTX_cleanup \
 			OpenSSL_version \
 			SSL_CTX_get_default_passwd_cb \
 			SSL_CTX_get_default_passwd_cb_userdata \
 			SSL_CTX_set_security_level \
+			X509_get0_notBefore \
+			X509_get0_notAfter \
 			X509_get0_pubkey \
 			X509_STORE_get0_objects \
 			X509_OBJECT_free \
src/openvpn/openssl_compat.h +16 -0
index a4072b9a..2453b85e 100644
@@ -89,6 +89,22 @@ EVP_MD_CTX_new(void)
 }
 #endif
 
+#if !defined(HAVE_EVP_CIPHER_CTX_INIT)
+#define EVP_CIPHER_CTX_init EVP_CIPHER_CTX_reset
+#endif
+
+#if !defined(HAVE_EVP_CIPHER_CTX_CLEANUP)
+#define EVP_CIPHER_CTX_cleanup EVP_CIPHER_CTX_reset
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTBEFORE)
+#define X509_get0_notBefore X509_get_notBefore
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTAFTER)
+#define X509_get0_notAfter X509_get_notAfter
+#endif
+
 #if !defined(HAVE_HMAC_CTX_RESET)
 /**
  * Reset a HMAC context
src/openvpn/ssl_openssl.c +12 -6
index 8bcebac4..e41cafa5 100644
@@ -76,12 +76,13 @@ int mydata_index; /* GLOBAL */
 void
 tls_init_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     SSL_library_init();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL
     SSL_load_error_strings();
-#endif
+# endif
     OpenSSL_add_all_algorithms();
-
+#endif
     mydata_index = SSL_get_ex_new_index(0, "struct session *", NULL, NULL, NULL);
     ASSERT(mydata_index >= 0);
 }
@@ -89,9 +90,11 @@ tls_init_lib(void)
 void
 tls_free_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     EVP_cleanup();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL
     ERR_free_strings();
+# endif
 #endif
 }
 
@@ -541,7 +544,7 @@ tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
         goto cleanup; /* Nothing to check if there is no certificate */
     }
 
-    ret = X509_cmp_time(X509_get_notBefore(cert), NULL);
+    ret = X509_cmp_time(X509_get0_notBefore(cert), NULL);
     if (ret == 0)
     {
         msg(D_TLS_DEBUG_MED, "Failed to read certificate notBefore field.");
@@ -551,7 +554,7 @@ tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
         msg(M_WARN, "WARNING: Your certificate is not yet valid!");
     }
 
-    ret = X509_cmp_time(X509_get_notAfter(cert), NULL);
+    ret = X509_cmp_time(X509_get0_notAfter(cert), NULL);
     if (ret == 0)
     {
         msg(D_TLS_DEBUG_MED, "Failed to read certificate notAfter field.");
@@ -634,10 +637,13 @@ tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name
     else
     {
 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
+
         /* OpenSSL 1.0.2 and newer can automatically handle ECDH parameter
          * loading */
         SSL_CTX_set_ecdh_auto(ctx->ctx, 1);
         return;
+#endif
 #else
         /* For older OpenSSL we have to extract the curve from key on our own */
         EC_KEY *eckey = NULL;
Signature · 1 line
2.17.1
#1 lore raw

Am 04.04.19 um 00:56 schrieb Rosen Penev:

Quoted 8 lines

EVP_CIPHER_CTX_init and _cleanup were deprecated in 1.1 and both were replaced with _reset.

Also removed initialization with OpenSSL 1.1 as it is no longer needed and causes compilation errors when disabling deprecated APIs.

Same with SSL_CTX_set_ecdh_auto as it got removed.

src/openvpn/openssl_compat.h EVP_MD_CTX_new(void) · line 99

Quoted 8 lines
src/openvpn/openssl_compat.h +8 -0
+#if !defined(HAVE_EVP_CIPHER_CTX_INIT)
+#define EVP_CIPHER_CTX_init EVP_CIPHER_CTX_reset
+#endif
+
+#if !defined(HAVE_EVP_CIPHER_CTX_CLEANUP)
+#define EVP_CIPHER_CTX_cleanup EVP_CIPHER_CTX_reset
+#endif
+

Generally we tried to avoid things like implementing the old API with the new API but instead try to only use the current OpenSSL API and implement it using the older API on older OpenSSL version. Is there way for these functions to do that as well?

Arne

#2 lore raw
In reply to Rosen Penev

Am 04.04.19 um 00:56 schrieb Rosen Penev:

Quoted 8 lines

EVP_CIPHER_CTX_init and _cleanup were deprecated in 1.1 and both were replaced with _reset.

Also removed initialization with OpenSSL 1.1 as it is no longer needed and causes compilation errors when disabling deprecated APIs.

Same with SSL_CTX_set_ecdh_auto as it got removed.

This gets kind of an ACK but needs some additional changes to be really good.

src/openvpn/openssl_compat.h EVP_MD_CTX_new(void) · line 98

Quoted 8 lines
src/openvpn/openssl_compat.h +7 -0
+#if !defined(HAVE_EVP_CIPHER_CTX_INIT)
+#define EVP_CIPHER_CTX_init EVP_CIPHER_CTX_reset
+#endif
+
+#if !defined(HAVE_EVP_CIPHER_CTX_CLEANUP)
+#define EVP_CIPHER_CTX_cleanup EVP_CIPHER_CTX_reset
+#endif

These two keep the older API instead of switching to the new one, from OpenSSL.

# if OPENSSL_API_COMPAT < 0x10100000L
#  define EVP_CIPHER_CTX_init(c)      EVP_CIPHER_CTX_reset(c)
#  define EVP_CIPHER_CTX_cleanup(c)   EVP_CIPHER_CTX_reset(c)
# endif

Since just using only the new API in this case does not really work I think in case it would be better to rather always use EVP_CIPHER_CTX_reset isntead of init and have ifdefs in the 2-3 places where we actually use EVP_CIPHER_CTX_cleanup so we can remove the old API when we bump our minimum OpenSSL version (and find this thing easy since it is an ifdef depending on the openssl version).

Quoted 17 lines
src/openvpn/openssl_compat.h +9 -0
+
+#if !defined(HAVE_X509_GET0_NOTBEFORE)
+#define X509_get0_notBefore X509_get_notBefore
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTAFTER)
+#define X509_get0_notAfter X509_get_notAfter
+#endif
+
 #if !defined(HAVE_HMAC_CTX_RESET)
 /**
  * Reset a HMAC context
src/openvpn/ssl_openssl.c +0 -0
index 8bcebac4..e41cafa5 100644

src/openvpn/ssl_openssl.c int mydata_index; /* GLOBAL */ · line 81

Quoted 7 lines
src/openvpn/ssl_openssl.c +2 -1
@@ -76,12 +76,13 @@ int mydata_index; /* GLOBAL */
 void
 tls_init_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     SSL_library_init();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL

The space between # and ifndef looks wrong.

Arne

#3 lore raw

Am 14.06.19 um 12:38 schrieb Arne Schwabe:

Quoted 4 lines

Review

Quoted 3 lines
(quoted diff) +1 -1
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL

The space between # and ifndef looks wrong.

It's standard C. (Chapter 3.8 in the 1989/1990 standard, chapter 6.10 in recent editions, I checked 1999 and 2017/2018, although worded in a quite convoluted way) and I have yet to see a relevant C compiler that would reject this.

The # is a token by itself can can be separated from the other preprocessing tokens on the same line by space and horizontal-tab.

#4 lore raw

Am 12.07.19 um 18:37 schrieb Matthias Andree:

Quoted 10 lines

Am 14.06.19 um 12:38 schrieb Arne Schwabe:

Quoted 4 lines

Review

Quoted 3 lines
(quoted diff) +1 -1
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL

The space between # and ifndef looks wrong.

It's standard C. (Chapter 3.8 in the 1989/1990 standard, chapter 6.10 in recent editions, I checked 1999 and 2017/2018, although worded in a quite convoluted way) and I have yet to see a relevant C compiler that would reject this.

I know :).

Looks "wrong" in the sense that it does not match the style we want to have in openvpn.

#5 lore raw

Hi,

Quoted 2 lines · Arne Schwabe

Looks "wrong" in the sense that it does not match the style we want to have in openvpn.

Indeed. I have seen and used "# cpp-thing" in other projects, but
OpenVPN seems to have decided to not use space-indenting for nested #if-things.

So, we'll keep this (and try to get rid of #ifdefs anyway :-) ).

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]...
#6 lore raw
In reply to Arne Schwabe
Quoted 16 lines · Arne Schwabe

Am 04.04.19 um 00:56 schrieb Rosen Penev:

Quoted 8 lines

EVP_CIPHER_CTX_init and _cleanup were deprecated in 1.1 and both were replaced with _reset.

Also removed initialization with OpenSSL 1.1 as it is no longer needed and causes compilation errors when disabling deprecated APIs.

Same with SSL_CTX_set_ecdh_auto as it got removed.

This gets kind of an ACK but needs some additional changes to be really good.

Quoted 1 line
Quoted 10 lines

src/openvpn/openssl_compat.h EVP_MD_CTX_new(void) · line 98

Quoted 7 lines
src/openvpn/openssl_compat.h +7 -0
+#if !defined(HAVE_EVP_CIPHER_CTX_INIT)
+#define EVP_CIPHER_CTX_init EVP_CIPHER_CTX_reset
+#endif
+
+#if !defined(HAVE_EVP_CIPHER_CTX_CLEANUP)
+#define EVP_CIPHER_CTX_cleanup EVP_CIPHER_CTX_reset
+#endif

These two keep the older API instead of switching to the new one, from OpenSSL.

Yes I know. I feel that it's a cleaner solution as _init and _cleanup both get defined to _reset.

Quoted 6 lines
# if OPENSSL_API_COMPAT < 0x10100000L
#  define EVP_CIPHER_CTX_init(c)      EVP_CIPHER_CTX_reset(c)
#  define EVP_CIPHER_CTX_cleanup(c)   EVP_CIPHER_CTX_reset(c)
# endif
Quoted 6 lines

Since just using only the new API in this case does not really work I think in case it would be better to rather always use EVP_CIPHER_CTX_reset isntead of init and have ifdefs in the 2-3 places where we actually use EVP_CIPHER_CTX_cleanup so we can remove the old API when we bump our minimum OpenSSL version (and find this thing easy since it is an ifdef depending on the openssl version).

OK. Will change.

Quoted 19 lines
Quoted 19 lines
src/openvpn/openssl_compat.h +9 -0
+
+#if !defined(HAVE_X509_GET0_NOTBEFORE)
+#define X509_get0_notBefore X509_get_notBefore
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTAFTER)
+#define X509_get0_notAfter X509_get_notAfter
+#endif
+
 #if !defined(HAVE_HMAC_CTX_RESET)
 /**
  * Reset a HMAC context
src/openvpn/ssl_openssl.c +0 -0
index 8bcebac4..e41cafa5 100644
@@ -76,12 +76,13 @@ int mydata_index; /* GLOBAL */
 void
Quoted 8 lines

src/openvpn/ssl_openssl.c int mydata_index; /* GLOBAL */ · line 81

Quoted 6 lines
src/openvpn/ssl_openssl.c +2 -1
 tls_init_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     SSL_library_init();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL

The space between # and ifndef looks wrong.

Will eliminate. Not sure exactly why I added the space ATM.

Quoted 4 lines

Arne

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#8 lore raw

Am 24.07.19 um 12:28 schrieb Steffan Karger:

Quoted 25 lines

Hi all,

Quoted 22 lines · Arne Schwabe

src/openvpn/openssl_compat.h EVP_MD_CTX_new(void) · line 98

Quoted 7 lines
src/openvpn/openssl_compat.h +7 -0
+#if !defined(HAVE_EVP_CIPHER_CTX_INIT)
+#define EVP_CIPHER_CTX_init EVP_CIPHER_CTX_reset
+#endif
+
+#if !defined(HAVE_EVP_CIPHER_CTX_CLEANUP)
+#define EVP_CIPHER_CTX_cleanup EVP_CIPHER_CTX_reset
+#endif

These two keep the older API instead of switching to the new one, from OpenSSL.

# if OPENSSL_API_COMPAT < 0x10100000L
#  define EVP_CIPHER_CTX_init(c)      EVP_CIPHER_CTX_reset(c)
#  define EVP_CIPHER_CTX_cleanup(c)   EVP_CIPHER_CTX_reset(c)
# endif

Since just using only the new API in this case does not really work I think in case it would be better to rather always use EVP_CIPHER_CTX_reset isntead of init and have ifdefs in the 2-3 places where we actually use EVP_CIPHER_CTX_cleanup so we can remove the old API when we bump our minimum OpenSSL version (and find this thing easy since it is an ifdef depending on the openssl version).

Okay, I double checked this.

The new API drops EVP_CIPHER_CTX_cleanup, so we have do either ifdef the EVP_CIPHER_CTX_cleanup calls or remove them completely.

Currently we only call the EVP_CIPHER_CTX_cleanup directly before EVP_CIPHER_CTX_free.

Looking at the 1.0.2 openssl code:

void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
{

   if (ctx) {
       EVP_CIPHER_CTX_cleanup(ctx);
      OPENSSL_free(ctx);

}
}

it looks like the EVP_CIPHER_CTX_cleanup is implicit already, so we can remove those call and drop the cipher_ctx_cleanup function and combine cipher_ctx_cleanup and _free into one for mbed TLS.

Does that sound reasonble?

Arne

#9 lore raw

PATCH v3 1/1 openssl: Fix compilation without deprecated OpenSSL 1.1 APIs

In reply to Rosen Penev

From: Rosen Penev <[email protected]...>

EVP_CIPHER_CTX_init and _cleanup were deprecated in 1.1 and both were replaced with _reset.

EVP_CIPHER_CTX_free in OpenSSL 1.1 replaces the cleanup/free combo of earlier OpenSSL version. And OpenSSL 1.0.2 already calls cleanup as part of _free.

Therefore we can remove the _cleanup calls and use the OpenSSL 1.1. API everywhere.

Also removed initialisation with OpenSSL 1.1 as it is no longer needed and causes compilation errors when disabling deprecated APIs.

Same with SSL_CTX_set_ecdh_auto as it got removed.

Patch V3: Use EVP_CIPHER_CTX_reset instead of init/cleanup

---
 configure.ac                 |  3 +++
 src/openvpn/crypto.c         |  1 -
 src/openvpn/crypto_backend.h |  9 +--------
 src/openvpn/crypto_mbedtls.c |  7 +------
 src/openvpn/crypto_openssl.c |  8 +-------
 src/openvpn/openssl_compat.h | 12 ++++++++++++
 src/openvpn/ssl_openssl.c    | 18 ++++++++++++------
 7 files changed, 30 insertions(+), 28 deletions(-)
configure.ac +3 -0
index 59673e04..b8e2476f 100644
@@ -918,10 +918,13 @@ if test "${with_crypto_library}" = "openssl"; then
 			EVP_MD_CTX_new \
 			EVP_MD_CTX_free \
 			EVP_MD_CTX_reset \
+			EVP_CIPHER_CTX_reset \
 			OpenSSL_version \
 			SSL_CTX_get_default_passwd_cb \
 			SSL_CTX_get_default_passwd_cb_userdata \
 			SSL_CTX_set_security_level \
+			X509_get0_notBefore \
+			X509_get0_notAfter \
 			X509_get0_pubkey \
 			X509_STORE_get0_objects \
 			X509_OBJECT_free \
src/openvpn/crypto.c +0 -1
index 8a92a8c1..585bfbc6 100644
@@ -906,7 +906,6 @@ free_key_ctx(struct key_ctx *ctx)
 {
     if (ctx->cipher)
     {
-        cipher_ctx_cleanup(ctx->cipher);
         cipher_ctx_free(ctx->cipher);
         ctx->cipher = NULL;
     }
src/openvpn/crypto_backend.h +1 -8
index 7e9a4bd2..d119442f 100644
@@ -341,7 +341,7 @@ bool cipher_kt_mode_aead(const cipher_kt_t *cipher);
 cipher_ctx_t *cipher_ctx_new(void);
 
 /**
- * Free a cipher context
+ * Cleanup and free a cipher context
  *
  * @param ctx           Cipher context.
  */
@@ -360,13 +360,6 @@ void cipher_ctx_free(cipher_ctx_t *ctx);
 void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, int key_len,
                      const cipher_kt_t *kt, int enc);
 
-/**
- * Cleanup the specified context.
- *
- * @param ctx   Cipher context to cleanup.
- */
-void cipher_ctx_cleanup(cipher_ctx_t *ctx);
-
 /**
  * Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is
  * used.
src/openvpn/crypto_mbedtls.c +1 -6
index 2e931440..f924323d 100644
@@ -616,12 +616,6 @@ cipher_ctx_init(mbedtls_cipher_context_t *ctx, const uint8_t *key, int key_len,
     ASSERT(ctx->key_bitlen <= key_len*8);
 }
 
-void
-cipher_ctx_cleanup(mbedtls_cipher_context_t *ctx)
-{
-    mbedtls_cipher_free(ctx);
-}
-
 int
 cipher_ctx_iv_length(const mbedtls_cipher_context_t *ctx)
 {
@@ -861,6 +855,7 @@ md_ctx_new(void)
 void
 md_ctx_free(mbedtls_md_context_t *ctx)
 {
+    mbedtls_cipher_free(ctx);
     free(ctx);
 }
 
src/openvpn/crypto_openssl.c +1 -7
index c049e52d..520e40ee 100644
@@ -772,7 +772,7 @@ cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, int key_len,
 {
     ASSERT(NULL != kt && NULL != ctx);
 
-    EVP_CIPHER_CTX_init(ctx);
+    EVP_CIPHER_CTX_reset(ctx);
     if (!EVP_CipherInit(ctx, kt, NULL, NULL, enc))
     {
         crypto_msg(M_FATAL, "EVP cipher init #1");
@@ -792,12 +792,6 @@ cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, int key_len,
     ASSERT(EVP_CIPHER_CTX_key_length(ctx) <= key_len);
 }
 
-void
-cipher_ctx_cleanup(EVP_CIPHER_CTX *ctx)
-{
-    EVP_CIPHER_CTX_cleanup(ctx);
-}
-
 int
 cipher_ctx_iv_length(const EVP_CIPHER_CTX *ctx)
 {
src/openvpn/openssl_compat.h +12 -0
index a4072b9a..4ac8f24d 100644
@@ -89,6 +89,18 @@ EVP_MD_CTX_new(void)
 }
 #endif
 
+#if !defined(HAVE_EVP_CIPHER_CTX_RESET)
+#define EVP_CIPHER_CTX_reset EVP_CIPHER_CTX_init
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTBEFORE)
+#define X509_get0_notBefore X509_get_notBefore
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTAFTER)
+#define X509_get0_notAfter X509_get_notAfter
+#endif
+
 #if !defined(HAVE_HMAC_CTX_RESET)
 /**
  * Reset a HMAC context
src/openvpn/ssl_openssl.c +12 -6
index 05ca4113..c029d0f2 100644
@@ -76,12 +76,13 @@ int mydata_index; /* GLOBAL */
 void
 tls_init_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     SSL_library_init();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL
     SSL_load_error_strings();
-#endif
+# endif
     OpenSSL_add_all_algorithms();
-
+#endif
     mydata_index = SSL_get_ex_new_index(0, "struct session *", NULL, NULL, NULL);
     ASSERT(mydata_index >= 0);
 }
@@ -89,9 +90,11 @@ tls_init_lib(void)
 void
 tls_free_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     EVP_cleanup();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL
     ERR_free_strings();
+# endif
 #endif
 }
 
@@ -567,7 +570,7 @@ tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
         goto cleanup; /* Nothing to check if there is no certificate */
     }
 
-    ret = X509_cmp_time(X509_get_notBefore(cert), NULL);
+    ret = X509_cmp_time(X509_get0_notBefore(cert), NULL);
     if (ret == 0)
     {
         msg(D_TLS_DEBUG_MED, "Failed to read certificate notBefore field.");
@@ -577,7 +580,7 @@ tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
         msg(M_WARN, "WARNING: Your certificate is not yet valid!");
     }
 
-    ret = X509_cmp_time(X509_get_notAfter(cert), NULL);
+    ret = X509_cmp_time(X509_get0_notAfter(cert), NULL);
     if (ret == 0)
     {
         msg(D_TLS_DEBUG_MED, "Failed to read certificate notAfter field.");
@@ -660,10 +663,13 @@ tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name
     else
     {
 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
+
         /* OpenSSL 1.0.2 and newer can automatically handle ECDH parameter
          * loading */
         SSL_CTX_set_ecdh_auto(ctx->ctx, 1);
         return;
+#endif
 #else
         /* For older OpenSSL we have to extract the curve from key on our own */
         EC_KEY *eckey = NULL;
Signature · 1 line
2.22.0
#10 lore raw
Quoted 16 lines · Arne Schwabe

From: Rosen Penev <[email protected]...>

EVP_CIPHER_CTX_init and _cleanup were deprecated in 1.1 and both were replaced with _reset.

EVP_CIPHER_CTX_free in OpenSSL 1.1 replaces the cleanup/free combo of earlier OpenSSL version. And OpenSSL 1.0.2 already calls cleanup as part of _free.

Therefore we can remove the _cleanup calls and use the OpenSSL 1.1. API everywhere.

Also removed initialisation with OpenSSL 1.1 as it is no longer needed and causes compilation errors when disabling deprecated APIs.

Quoted 6 lines

Same with SSL_CTX_set_ecdh_auto as it got removed.

Patch V3: Use EVP_CIPHER_CTX_reset instead of init/cleanup

ACK

Quoted 210 lines
---
 configure.ac                 |  3 +++
 src/openvpn/crypto.c         |  1 -
 src/openvpn/crypto_backend.h |  9 +--------
 src/openvpn/crypto_mbedtls.c |  7 +------
 src/openvpn/crypto_openssl.c |  8 +-------
 src/openvpn/openssl_compat.h | 12 ++++++++++++
 src/openvpn/ssl_openssl.c    | 18 ++++++++++++------
 7 files changed, 30 insertions(+), 28 deletions(-)
configure.ac +3 -0
index 59673e04..b8e2476f 100644
@@ -918,10 +918,13 @@ if test "${with_crypto_library}" = "openssl"; then
                        EVP_MD_CTX_new \
                        EVP_MD_CTX_free \
                        EVP_MD_CTX_reset \
+                       EVP_CIPHER_CTX_reset \
                        OpenSSL_version \
                        SSL_CTX_get_default_passwd_cb \
                        SSL_CTX_get_default_passwd_cb_userdata \
                        SSL_CTX_set_security_level \
+                       X509_get0_notBefore \
+                       X509_get0_notAfter \
                        X509_get0_pubkey \
                        X509_STORE_get0_objects \
                        X509_OBJECT_free \
src/openvpn/crypto.c +0 -1
index 8a92a8c1..585bfbc6 100644
@@ -906,7 +906,6 @@ free_key_ctx(struct key_ctx *ctx)
 {
     if (ctx->cipher)
     {
-        cipher_ctx_cleanup(ctx->cipher);
         cipher_ctx_free(ctx->cipher);
         ctx->cipher = NULL;
     }
src/openvpn/crypto_backend.h +1 -8
index 7e9a4bd2..d119442f 100644
@@ -341,7 +341,7 @@ bool cipher_kt_mode_aead(const cipher_kt_t *cipher);
 cipher_ctx_t *cipher_ctx_new(void);
 
 /**
- * Free a cipher context
+ * Cleanup and free a cipher context
  *
  * @param ctx           Cipher context.
  */
@@ -360,13 +360,6 @@ void cipher_ctx_free(cipher_ctx_t *ctx);
 void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, int key_len,
                      const cipher_kt_t *kt, int enc);
 
-/**
- * Cleanup the specified context.
- *
- * @param ctx   Cipher context to cleanup.
- */
-void cipher_ctx_cleanup(cipher_ctx_t *ctx);
-
 /**
  * Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is
  * used.
src/openvpn/crypto_mbedtls.c +1 -6
index 2e931440..f924323d 100644
@@ -616,12 +616,6 @@ cipher_ctx_init(mbedtls_cipher_context_t *ctx, const uint8_t *key, int key_len,
     ASSERT(ctx->key_bitlen <= key_len*8);
 }
 
-void
-cipher_ctx_cleanup(mbedtls_cipher_context_t *ctx)
-{
-    mbedtls_cipher_free(ctx);
-}
-
 int
 cipher_ctx_iv_length(const mbedtls_cipher_context_t *ctx)
 {
@@ -861,6 +855,7 @@ md_ctx_new(void)
 void
 md_ctx_free(mbedtls_md_context_t *ctx)
 {
+    mbedtls_cipher_free(ctx);
     free(ctx);
 }
 
src/openvpn/crypto_openssl.c +1 -7
index c049e52d..520e40ee 100644
@@ -772,7 +772,7 @@ cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, int key_len,
 {
     ASSERT(NULL != kt && NULL != ctx);
 
-    EVP_CIPHER_CTX_init(ctx);
+    EVP_CIPHER_CTX_reset(ctx);
     if (!EVP_CipherInit(ctx, kt, NULL, NULL, enc))
     {
         crypto_msg(M_FATAL, "EVP cipher init #1");
@@ -792,12 +792,6 @@ cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, int key_len,
     ASSERT(EVP_CIPHER_CTX_key_length(ctx) <= key_len);
 }
 
-void
-cipher_ctx_cleanup(EVP_CIPHER_CTX *ctx)
-{
-    EVP_CIPHER_CTX_cleanup(ctx);
-}
-
 int
 cipher_ctx_iv_length(const EVP_CIPHER_CTX *ctx)
 {
src/openvpn/openssl_compat.h +12 -0
index a4072b9a..4ac8f24d 100644
@@ -89,6 +89,18 @@ EVP_MD_CTX_new(void)
 }
 #endif
 
+#if !defined(HAVE_EVP_CIPHER_CTX_RESET)
+#define EVP_CIPHER_CTX_reset EVP_CIPHER_CTX_init
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTBEFORE)
+#define X509_get0_notBefore X509_get_notBefore
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTAFTER)
+#define X509_get0_notAfter X509_get_notAfter
+#endif
+
 #if !defined(HAVE_HMAC_CTX_RESET)
 /**
  * Reset a HMAC context
src/openvpn/ssl_openssl.c +12 -7
index 05ca4113..c029d0f2 100644
@@ -76,12 +76,13 @@ int mydata_index; /* GLOBAL */
 void
 tls_init_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     SSL_library_init();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL
     SSL_load_error_strings();
-#endif
+# endif
     OpenSSL_add_all_algorithms();
-
+#endif
     mydata_index = SSL_get_ex_new_index(0, "struct session *", NULL, NULL, NULL);
     ASSERT(mydata_index >= 0);
 }
@@ -89,9 +90,11 @@ tls_init_lib(void)
 void
 tls_free_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     EVP_cleanup();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL
     ERR_free_strings();
+# endif
 #endif
 }
 
@@ -567,7 +570,7 @@ tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
         goto cleanup; /* Nothing to check if there is no certificate */
     }
 
-    ret = X509_cmp_time(X509_get_notBefore(cert), NULL);
+    ret = X509_cmp_time(X509_get0_notBefore(cert), NULL);
     if (ret == 0)
     {
         msg(D_TLS_DEBUG_MED, "Failed to read certificate notBefore field.");
@@ -577,7 +580,7 @@ tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
         msg(M_WARN, "WARNING: Your certificate is not yet valid!");
     }
 
-    ret = X509_cmp_time(X509_get_notAfter(cert), NULL);
+    ret = X509_cmp_time(X509_get0_notAfter(cert), NULL);
     if (ret == 0)
     {
         msg(D_TLS_DEBUG_MED, "Failed to read certificate notAfter field.");
@@ -660,10 +663,13 @@ tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name
     else
     {
 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
+
         /* OpenSSL 1.0.2 and newer can automatically handle ECDH parameter
          * loading */
         SSL_CTX_set_ecdh_auto(ctx->ctx, 1);
         return;
+#endif
 #else
         /* For older OpenSSL we have to extract the curve from key on our own */
         EC_KEY *eckey = NULL;
--

2.22.0

Acked-by on v3 1/1
#11 lore raw
In reply to Arne Schwabe
Quoted 219 lines · Arne Schwabe

From: Rosen Penev <[email protected]...>

EVP_CIPHER_CTX_init and _cleanup were deprecated in 1.1 and both were replaced with _reset.

EVP_CIPHER_CTX_free in OpenSSL 1.1 replaces the cleanup/free combo of earlier OpenSSL version. And OpenSSL 1.0.2 already calls cleanup as part of _free.

Therefore we can remove the _cleanup calls and use the OpenSSL 1.1. API everywhere.

Also removed initialisation with OpenSSL 1.1 as it is no longer needed and causes compilation errors when disabling deprecated APIs.

Same with SSL_CTX_set_ecdh_auto as it got removed.

Patch V3: Use EVP_CIPHER_CTX_reset instead of init/cleanup

---
 configure.ac                 |  3 +++
 src/openvpn/crypto.c         |  1 -
 src/openvpn/crypto_backend.h |  9 +--------
 src/openvpn/crypto_mbedtls.c |  7 +------
 src/openvpn/crypto_openssl.c |  8 +-------
 src/openvpn/openssl_compat.h | 12 ++++++++++++
 src/openvpn/ssl_openssl.c    | 18 ++++++++++++------
 7 files changed, 30 insertions(+), 28 deletions(-)
configure.ac +3 -0
index 59673e04..b8e2476f 100644
@@ -918,10 +918,13 @@ if test "${with_crypto_library}" = "openssl"; then
 			EVP_MD_CTX_new \
 			EVP_MD_CTX_free \
 			EVP_MD_CTX_reset \
+			EVP_CIPHER_CTX_reset \
 			OpenSSL_version \
 			SSL_CTX_get_default_passwd_cb \
 			SSL_CTX_get_default_passwd_cb_userdata \
 			SSL_CTX_set_security_level \
+			X509_get0_notBefore \
+			X509_get0_notAfter \
 			X509_get0_pubkey \
 			X509_STORE_get0_objects \
 			X509_OBJECT_free \
src/openvpn/crypto.c +0 -1
index 8a92a8c1..585bfbc6 100644
@@ -906,7 +906,6 @@ free_key_ctx(struct key_ctx *ctx)
 {
     if (ctx->cipher)
     {
-        cipher_ctx_cleanup(ctx->cipher);
         cipher_ctx_free(ctx->cipher);
         ctx->cipher = NULL;
     }
src/openvpn/crypto_backend.h +1 -8
index 7e9a4bd2..d119442f 100644
@@ -341,7 +341,7 @@ bool cipher_kt_mode_aead(const cipher_kt_t *cipher);
 cipher_ctx_t *cipher_ctx_new(void);
 
 /**
- * Free a cipher context
+ * Cleanup and free a cipher context
  *
  * @param ctx           Cipher context.
  */
@@ -360,13 +360,6 @@ void cipher_ctx_free(cipher_ctx_t *ctx);
 void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, int key_len,
                      const cipher_kt_t *kt, int enc);
 
-/**
- * Cleanup the specified context.
- *
- * @param ctx   Cipher context to cleanup.
- */
-void cipher_ctx_cleanup(cipher_ctx_t *ctx);
-
 /**
  * Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is
  * used.
src/openvpn/crypto_mbedtls.c +1 -6
index 2e931440..f924323d 100644
@@ -616,12 +616,6 @@ cipher_ctx_init(mbedtls_cipher_context_t *ctx, const uint8_t *key, int key_len,
     ASSERT(ctx->key_bitlen <= key_len*8);
 }
 
-void
-cipher_ctx_cleanup(mbedtls_cipher_context_t *ctx)
-{
-    mbedtls_cipher_free(ctx);
-}
-
 int
 cipher_ctx_iv_length(const mbedtls_cipher_context_t *ctx)
 {
@@ -861,6 +855,7 @@ md_ctx_new(void)
 void
 md_ctx_free(mbedtls_md_context_t *ctx)
 {
+    mbedtls_cipher_free(ctx);
     free(ctx);
 }
 
src/openvpn/crypto_openssl.c +1 -7
index c049e52d..520e40ee 100644
@@ -772,7 +772,7 @@ cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, int key_len,
 {
     ASSERT(NULL != kt && NULL != ctx);
 
-    EVP_CIPHER_CTX_init(ctx);
+    EVP_CIPHER_CTX_reset(ctx);
     if (!EVP_CipherInit(ctx, kt, NULL, NULL, enc))
     {
         crypto_msg(M_FATAL, "EVP cipher init #1");
@@ -792,12 +792,6 @@ cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, int key_len,
     ASSERT(EVP_CIPHER_CTX_key_length(ctx) <= key_len);
 }
 
-void
-cipher_ctx_cleanup(EVP_CIPHER_CTX *ctx)
-{
-    EVP_CIPHER_CTX_cleanup(ctx);
-}
-
 int
 cipher_ctx_iv_length(const EVP_CIPHER_CTX *ctx)
 {
src/openvpn/openssl_compat.h +12 -0
index a4072b9a..4ac8f24d 100644
@@ -89,6 +89,18 @@ EVP_MD_CTX_new(void)
 }
 #endif
 
+#if !defined(HAVE_EVP_CIPHER_CTX_RESET)
+#define EVP_CIPHER_CTX_reset EVP_CIPHER_CTX_init
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTBEFORE)
+#define X509_get0_notBefore X509_get_notBefore
+#endif
+
+#if !defined(HAVE_X509_GET0_NOTAFTER)
+#define X509_get0_notAfter X509_get_notAfter
+#endif
+
 #if !defined(HAVE_HMAC_CTX_RESET)
 /**
  * Reset a HMAC context
src/openvpn/ssl_openssl.c +9 -6
index 05ca4113..c029d0f2 100644
@@ -76,12 +76,13 @@ int mydata_index; /* GLOBAL */
 void
 tls_init_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     SSL_library_init();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL
     SSL_load_error_strings();
-#endif
+# endif
     OpenSSL_add_all_algorithms();
-
+#endif
     mydata_index = SSL_get_ex_new_index(0, "struct session *", NULL, NULL, NULL);
     ASSERT(mydata_index >= 0);
 }
@@ -89,9 +90,11 @@ tls_init_lib(void)
 void
 tls_free_lib(void)
 {
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
     EVP_cleanup();
-#ifndef ENABLE_SMALL
+# ifndef ENABLE_SMALL
     ERR_free_strings();
+# endif
 #endif
 }
 
@@ -567,7 +570,7 @@ tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
         goto cleanup; /* Nothing to check if there is no certificate */
     }
 
-    ret = X509_cmp_time(X509_get_notBefore(cert), NULL);
+    ret = X509_cmp_time(X509_get0_notBefore(cert), NULL);
     if (ret == 0)
     {
         msg(D_TLS_DEBUG_MED, "Failed to read certificate notBefore field.");
@@ -577,7 +580,7 @@ tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
         msg(M_WARN, "WARNING: Your certificate is not yet valid!");
     }
 
-    ret = X509_cmp_time(X509_get_notAfter(cert), NULL);
+    ret = X509_cmp_time(X509_get0_notAfter(cert), NULL);
     if (ret == 0)
     {
         msg(D_TLS_DEBUG_MED, "Failed to read certificate notAfter field.");
@@ -660,10 +663,13 @@ tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name
     else
     {
 #if OPENSSL_VERSION_NUMBER >= 0x10002000L

src/openvpn/ssl_openssl.c tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name · line 675

Quoted 10 lines
src/openvpn/ssl_openssl.c +3 -0
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
+
         /* OpenSSL 1.0.2 and newer can automatically handle ECDH parameter
          * loading */
         SSL_CTX_set_ecdh_auto(ctx->ctx, 1);
         return;
+#endif
 #else
         /* For older OpenSSL we have to extract the curve from key on our own */
         EC_KEY *eckey = NULL;

Code looks good, verified that TLS connections still works with mbedtls and openssl builds.

-Steffan

#12 lore raw
In reply to Arne Schwabe

Your patch has been applied to the master branch.

Is this also suitable for release/2.4? "You folks tell me, I do the cherry-picking" (if it applies) :-)

I have removed the extra spaces in "# if" constructs, as this is not something we use elsewhere on nested CPP expressions (it came up in the discussion, but was still part of this patch).

Tested lightly with openssl 1.0.2o and 1.1.1.

commit 8a01147ff77e4ae2e377744b89fbe4b6841b2bb0 (master)

Author: Rosen Penev
Date:   Wed Jul 24 17:29:34 2019 +0200
     openssl: Fix compilation without deprecated OpenSSL 1.1 APIs

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

--
kind regards,

Gert Doering

#13 lore raw
Quoted 5 lines · Gert Doering

Your patch has been applied to the master branch.

Is this also suitable for release/2.4? "You folks tell me, I do the cherry-picking" (if it applies) :-)

2.4 is what I did my testing on, so yes.

Quoted 27 lines

I have removed the extra spaces in "# if" constructs, as this is not something we use elsewhere on nested CPP expressions (it came up in the discussion, but was still part of this patch).

Tested lightly with openssl 1.0.2o and 1.1.1.

commit 8a01147ff77e4ae2e377744b89fbe4b6841b2bb0 (master)

Author: Rosen Penev
Date:   Wed Jul 24 17:29:34 2019 +0200
     openssl: Fix compilation without deprecated OpenSSL 1.1 APIs

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

--
kind regards,

Gert Doering

#14 lore raw

Hi,

Quoted 7 lines · Rosen Penev
Quoted 5 lines · Gert Doering

Your patch has been applied to the master branch.

Is this also suitable for release/2.4? "You folks tell me, I do the cherry-picking" (if it applies) :-)

2.4 is what I did my testing on, so yes.

So - took me a bit, but here we go. I backported this and the mbedtls explosive patch to release/2.4, for long-term compatibility reasons.

commit 416532f8e4125adb7862b2dce5c2d47d85b260df (HEAD -> release/2.4, mattock/re lease/2.4)

Author: Antonio Quartulli <[email protected]...>
Date:   Fri Aug 16 22:49:45 2019 +0200

mbedtls: fix segfault by calling mbedtls_cipher_free() in cipher_ctx_free()

commit 66b93b5e708b48778a5954fdcfe708b76b947a06

Author: Rosen Penev <[email protected]...>
Date:   Wed Jul 24 17:29:34 2019 +0200

openssl: Fix compilation without deprecated OpenSSL 1.1 APIs

I've sent the combo to the buildslaves, and no explosions were seen - and besides them, I tested mbedtls 2.17.0, OpenSSL 1.1.1 and OpenSSL 1.0.2o locally (no explosions either).

Good to go... :-)

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]...
#15 lore raw
In reply to Gert Doering

Hi,

Quoted 10 lines · Gert Doering

Your patch has been applied to the master branch.

Is this also suitable for release/2.4? "You folks tell me, I do the cherry-picking" (if it applies) :-)

I have removed the extra spaces in "# if" constructs, as this is not something we use elsewhere on nested CPP expressions (it came up in the discussion, but was still part of this patch).

Tested lightly with openssl 1.0.2o and 1.1.1.

I should have tested with mbedtls :-/ - buildbot tells me that a good number of platforms have started core dumping on the mbedtls client tests with this commit.

*** Error in `../src/openvpn/openvpn': free(): invalid next size (fast):
+0x0000000000c74850 ***
./t_client.sh: line 262: 8896 Aborted (core dumped) $RUN_SUDO
+"${top_builddir}/src/openvpn/openvpn" $openvpn_conf >> $LOGDIR/$SUF:openvpn.log
OpenVPN running with PID 8896

(I have seen this on fedora29 and one of the FreeBSDs, but there is "more red" - more details on mbedTLS versions in use can be provided)

Steffan, if you could have a look, this would be most appreciated...

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