All of lore.kernel.org
 help / color / mirror / Atom feed
From: Emmanuel Deloget <logout@...260...>
To: openvpn-devel@lists.sourceforge.net
Subject: [Openvpn-devel] [PATCH 3/7] OpenSSL: don't use direct access to the internal of RSA
Date: Fri, 19 May 2017 12:38:12 +0200	[thread overview]
Message-ID: <20170519103816.19851-3-logout@...260...> (raw)
In-Reply-To: <20170519103816.19851-1-logout@...260...>

OpenSSL 1.1 does not allow us to directly access the internal of
any data type, including RSA. We have to use the defined
functions to do so.

Compatibility with OpenSSL 1.0 is kept by defining the corresponding
functions when they are not found in the library.

Signed-off-by: Emmanuel Deloget <logout@...260...>
---
 configure.ac                 |  3 ++
 src/openvpn/openssl_compat.h | 84 ++++++++++++++++++++++++++++++++++++++++++++
 src/openvpn/ssl_openssl.c    | 24 ++++++++-----
 3 files changed, 103 insertions(+), 8 deletions(-)

diff --git a/configure.ac b/configure.ac
index a92e8142..e4c053c8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -929,6 +929,9 @@ if test "${enable_crypto}" = "yes" -a "${with_crypto_library}" = "openssl"; then
 			EVP_PKEY_id \
 			EVP_PKEY_get0_RSA \
 			EVP_PKEY_get0_DSA \
+			RSA_set_flags \
+			RSA_get0_key \
+			RSA_set0_key \
 			RSA_meth_new \
 			RSA_meth_free \
 			RSA_meth_set_pub_enc \
diff --git a/src/openvpn/openssl_compat.h b/src/openvpn/openssl_compat.h
index 0d82cf25..29cd13a4 100644
--- a/src/openvpn/openssl_compat.h
+++ b/src/openvpn/openssl_compat.h
@@ -176,6 +176,90 @@ EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
 }
 #endif
 
+#if !defined(HAVE_RSA_SET_FLAGS)
+/**
+ * Set the RSA flags
+ *
+ * @param rsa                 The RSA object
+ * @param flags               New flags value
+ */
+static inline void
+RSA_set_flags(RSA *rsa, int flags)
+{
+    if (rsa)
+    {
+        rsa->flags = flags;
+    }
+}
+#endif
+
+#if !defined(HAVE_RSA_GET0_KEY)
+/**
+ * Get the RSA parameters
+ *
+ * @param rsa                 The RSA object
+ * @param n                   The @c n parameter
+ * @param e                   The @c e parameter
+ * @param d                   The @c d parameter
+ */
+static inline void
+RSA_get0_key(const RSA *rsa, const BIGNUM **n,
+             const BIGNUM **e, const BIGNUM **d)
+{
+    if (n != NULL)
+    {
+        *n = rsa ? rsa->n : NULL;
+    }
+    if (e != NULL)
+    {
+        *e = rsa ? rsa->e : NULL;
+    }
+    if (d != NULL)
+    {
+        *d = rsa ? rsa->d : NULL;
+    }
+}
+#endif
+
+#if !defined(HAVE_RSA_SET0_KEY)
+/**
+ * Set the RSA parameters
+ *
+ * @param rsa                 The RSA object
+ * @param n                   The @c n parameter
+ * @param e                   The @c e parameter
+ * @param d                   The @c d parameter
+ * @return                    1 on success, 0 on error
+ */
+static inline int
+RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d)
+{
+    if ((rsa->n == NULL && n == NULL)
+        || (rsa->e == NULL && e == NULL))
+    {
+        return 0;
+    }
+
+    if (n != NULL)
+    {
+        BN_free(rsa->n);
+        rsa->n = n;
+    }
+    if (e != NULL)
+    {
+        BN_free(rsa->e);
+        rsa->e = e;
+    }
+    if (d != NULL)
+    {
+        BN_free(rsa->d);
+        rsa->d = d;
+    }
+
+    return 1;
+}
+#endif
+
 #if !defined(HAVE_RSA_METH_NEW)
 /**
  * Allocate a new RSA method object
diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c
index 1c73641c..48479c0d 100644
--- a/src/openvpn/ssl_openssl.c
+++ b/src/openvpn/ssl_openssl.c
@@ -975,8 +975,8 @@ rsa_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, i
 static int
 rsa_finish(RSA *rsa)
 {
-    RSA_meth_free(rsa->meth);
-    rsa->meth = NULL;
+    const RSA_METHOD *meth = RSA_get_method(rsa);
+    RSA_meth_free((RSA_METHOD *)meth);
     return 1;
 }
 
@@ -1075,8 +1075,11 @@ tls_ctx_use_external_private_key(struct tls_root_ctx *ctx,
     pub_rsa = EVP_PKEY_get0_RSA(pkey);
 
     /* initialize RSA object */
-    rsa->n = BN_dup(pub_rsa->n);
-    rsa->flags |= RSA_FLAG_EXT_PKEY;
+    const BIGNUM *n = NULL;
+    const BIGNUM *e = NULL;
+    RSA_get0_key(pub_rsa, &n, &e, NULL);
+    RSA_set0_key(rsa, BN_dup(n), BN_dup(e), NULL);
+    RSA_set_flags(rsa, RSA_flags(rsa) | RSA_FLAG_EXT_PKEY);
     if (!RSA_set_method(rsa, rsa_meth))
     {
         goto err;
@@ -1677,11 +1680,16 @@ print_details(struct key_state_ssl *ks_ssl, const char *prefix)
         EVP_PKEY *pkey = X509_get_pubkey(cert);
         if (pkey != NULL)
         {
-            if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA && EVP_PKEY_get0_RSA(pkey) != NULL
-                && pkey->pkey.rsa->n != NULL)
+            if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA && EVP_PKEY_get0_RSA(pkey) != NULL)
             {
-                openvpn_snprintf(s2, sizeof(s2), ", %d bit RSA",
-                                 BN_num_bits(pkey->pkey.rsa->n));
+                RSA *rsa = EVP_PKEY_get0_RSA(pkey);
+                const BIGNUM *n = NULL;
+                RSA_get0_key(rsa, &n, NULL, NULL);
+                if (n != NULL)
+                {
+                    openvpn_snprintf(s2, sizeof(s2), ", %d bit RSA",
+                                     BN_num_bits(n));
+                }
             }
             else if (EVP_PKEY_id(pkey) == EVP_PKEY_DSA && EVP_PKEY_get0_DSA(pkey) != NULL
                      && pkey->pkey.dsa->p != NULL)
-- 
2.11.0



  parent reply	other threads:[~2017-05-19 10:38 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-17 22:00 [Openvpn-devel] [RFC PATCH v1 00/15] Add support for OpenSSL 1.1.x logout
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 01/15] OpenSSL: don't use direct access to the internal of SSL_CTX logout
2017-02-22 20:27   ` Steffan Karger
2017-02-22 21:13   ` [Openvpn-devel] [PATCH applied] " Gert Doering
2017-02-23  8:03   ` [Openvpn-devel] [RFC PATCH v1 01/15] " Gert Doering
2017-02-23  9:23     ` Gert Doering
2017-02-23  9:31       ` Emmanuel Deloget
2017-02-23  9:36         ` Steffan Karger
2017-02-23 10:35       ` [Openvpn-devel] [PATCH] OpenSSL: 1.1 fallout - fix configure on old autoconf Steffan Karger
2017-02-23 10:59         ` [Openvpn-devel] [PATCH applied] " Gert Doering
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 02/15] OpenSSL: don't use direct access to the internal of X509_STORE logout
2017-02-22 20:36   ` Steffan Karger
2017-02-22 21:14   ` [Openvpn-devel] [PATCH applied] " Gert Doering
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 03/15] OpenSSL: don't use direct access to the internal of X509_OBJECT logout
2017-02-22 20:50   ` Steffan Karger
2017-02-22 21:14   ` [Openvpn-devel] [PATCH applied] " Gert Doering
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 04/15] OpenSSL: don't use direct access to the internal of RSA_METHOD logout
2017-02-22 22:13   ` Steffan Karger
2017-02-23  9:39     ` Emmanuel Deloget
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 05/15] OpenSSL: don't use direct access to the internal of X509 logout
2017-03-02 20:36   ` Steffan Karger
2017-03-02 21:26     ` Gert Doering
2017-03-04 15:13       ` Steffan Karger
2017-03-04 16:22         ` Emmanuel Deloget
2017-03-04 22:38         ` David Sommerseth
2017-03-27 15:49           ` Emmanuel Deloget
2017-03-28  8:43             ` Emmanuel Deloget
2017-03-28  8:49               ` Gert Doering
2017-05-18 20:49             ` [Openvpn-devel] OpenSSL 1.1 patch set - status? Gert Doering
2017-05-19 10:37               ` Emmanuel Deloget
2017-05-19 10:38                 ` [Openvpn-devel] [PATCH 1/7] OpenSSL: don't use direct access to the internal of X509 Emmanuel Deloget
2017-05-19 10:38                   ` [Openvpn-devel] [PATCH 2/7] OpenSSL: don't use direct access to the internal of EVP_PKEY Emmanuel Deloget
2017-06-11 19:40                     ` Steffan Karger
2017-05-19 10:38                   ` Emmanuel Deloget [this message]
2017-06-11 20:09                     ` [Openvpn-devel] [PATCH 3/7] OpenSSL: don't use direct access to the internal of RSA Steffan Karger
2017-05-19 10:38                   ` [Openvpn-devel] [PATCH 4/7] OpenSSL: don't use direct access to the internal of DSA Emmanuel Deloget
2017-06-11 20:11                     ` Steffan Karger
2017-05-19 10:38                   ` [Openvpn-devel] [PATCH 5/7] OpenSSL: don't use direct access to the internal of EVP_MD_CTX Emmanuel Deloget
2017-05-19 10:38                   ` [Openvpn-devel] [PATCH 6/7] OpenSSL: don't use direct access to the internal of EVP_CIPHER_CTX Emmanuel Deloget
2017-05-19 10:38                   ` [Openvpn-devel] [PATCH 7/7] OpenSSL: don't use direct access to the internal of HMAC_CTX Emmanuel Deloget
2017-06-11 19:36                   ` [Openvpn-devel] [PATCH 1/7] OpenSSL: don't use direct access to the internal of X509 Steffan Karger
2017-05-19 11:41                 ` [Openvpn-devel] OpenSSL 1.1 patch set - status? Gert Doering
2017-06-08 15:57                   ` Emmanuel Deloget
2017-06-08 16:11                     ` Steffan Karger
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 06/15] OpenSSL: don't use direct access to the internal of EVP_PKEY logout
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 07/15] OpenSSL: don't use direct access to the internal of RSA logout
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 08/15] OpenSSL: don't use direct access to the internal of DSA logout
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 09/15] OpenSSL: don't use direct access to the internal of X509_STORE_CTX logout
2017-02-21 21:30   ` Steffan Karger
2017-02-22 14:47     ` Christian Hesse
2017-02-22 15:34       ` Steffan Karger
2017-02-22 16:07   ` [Openvpn-devel] [PATCH applied] " Gert Doering
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 10/15] OpenSSL: don't use direct access to the internal of EVP_MD_CTX logout
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 11/15] OpenSSL: don't use direct access to the internal of EVP_CIPHER_CTX logout
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 12/15] OpenSSL: don't use direct access to the internal of HMAC_CTX logout
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 13/15] OpenSSL: SSLeay symbols are no longer available in OpenSSL 1.1 logout
2017-03-02 20:39   ` Steffan Karger
2017-03-05 12:21   ` [Openvpn-devel] [PATCH applied] " Gert Doering
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 14/15] OpenSSL: check for the SSL reason, not the full error logout
2017-02-19 12:36   ` Steffan Karger
2017-02-19 17:52   ` [Openvpn-devel] [PATCH applied] " Gert Doering
2017-02-17 22:00 ` [Openvpn-devel] [RFC PATCH v1 15/15] OpenSSL: constify getbio() parameters logout
2017-02-19 12:03 ` [Openvpn-devel] [RFC PATCH v1 00/15] Add support for OpenSSL 1.1.x Steffan Karger
2017-02-19 14:58   ` David Sommerseth
2017-02-19 15:09     ` Steffan Karger
2017-02-19 15:01   ` Emmanuel Deloget
2017-02-19 17:49   ` Gert Doering
2017-02-20 11:45     ` Emmanuel Deloget
2017-02-20 12:29       ` Christian Hesse
2017-02-20 13:33         ` Emmanuel Deloget
2017-02-20 13:53           ` Emmanuel Deloget
2017-02-20 14:52             ` Emmanuel Deloget
2017-02-20 15:02               ` Christian Hesse
2017-02-20 15:08                 ` Christian Hesse
2017-02-23 14:35                   ` [Openvpn-devel] [PATCH v3 " Emmanuel Deloget
2017-02-23 20:57                     ` Christian Hesse
2017-02-24 12:13                       ` Christian Hesse
2017-02-24 21:49                         ` Christian Hesse
2017-02-23 14:35                   ` [Openvpn-devel] [PATCH v3 04/15] OpenSSL: don't use direct access to the internal of RSA_METHOD Emmanuel Deloget
2017-03-02 20:01                     ` Steffan Karger
2017-03-05  9:53                     ` [Openvpn-devel] [PATCH applied] " Gert Doering
2017-03-05 10:29                       ` Steffan Karger
2017-03-05 12:08                         ` Emmanuel Deloget
2017-03-05 12:26                           ` Gert Doering
2017-02-23 14:35                   ` [Openvpn-devel] [PATCH v3 07/15] OpenSSL: don't use direct access to the internal of RSA Emmanuel Deloget
2017-02-20 12:37       ` [Openvpn-devel] [RFC PATCH v1 00/15] Add support for OpenSSL 1.1.x Gert Doering
2017-02-20 13:38         ` Emmanuel Deloget
2017-02-20 14:32 ` [Openvpn-devel] [RFC PATCH v2 " Emmanuel Deloget
2017-02-20 14:32   ` [Openvpn-devel] [RFC PATCH v2 06/15] OpenSSL: don't use direct access to the internal of EVP_PKEY Emmanuel Deloget
2017-02-20 14:32   ` [Openvpn-devel] [RFC PATCH v2 15/15] OpenSSL: use EVP_CipherInit_ex() instead of EVP_CipherInit() Emmanuel Deloget
2017-03-02 20:44     ` Steffan Karger
2017-03-05 12:21     ` [Openvpn-devel] [PATCH applied] " Gert Doering

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170519103816.19851-3-logout@...260... \
    --to=openvpn-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.