256ebe
From e3020e43344ddbc32e62e06bbbf88a4f5d7cdc82 Mon Sep 17 00:00:00 2001
256ebe
From: Mohit Agrawal <moagrawa@redhat.com>
256ebe
Date: Fri, 10 May 2019 11:13:45 +0530
256ebe
Subject: [PATCH 141/141] socket/ssl: fix crl handling
256ebe
256ebe
Problem:
256ebe
Just setting the path to the CRL directory in socket_init() wasn't working.
256ebe
256ebe
Solution:
256ebe
Need to use special API to retrieve and set X509_VERIFY_PARAM and set
256ebe
the CRL checking flags explicitly.
256ebe
Also, setting the CRL checking flags is a big pain, since the connection
256ebe
is declared as failed if any CRL isn't found in the designated file or
256ebe
directory. A comment has been added to the code appropriately.
256ebe
256ebe
> Change-Id: I8a8ed2ddaf4b5eb974387d2f7b1a85c1ca39fe79
256ebe
> fixes: bz#1687326
256ebe
> Signed-off-by: Milind Changire <mchangir@redhat.com>
256ebe
> (Cherry pick from commit 06fa261207f0f0625c52fa977b96e5875e9a91e0)
256ebe
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/22334/)
256ebe
256ebe
Change-Id: I0958e9890035fd376f1e1eafc1452caf3edd184b
256ebe
BUG: 1583585
256ebe
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
256ebe
Reviewed-on: https://code.engineering.redhat.com/gerrit/166458
256ebe
Tested-by: RHGS Build Bot <nigelb@redhat.com>
256ebe
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
256ebe
---
256ebe
 configure.ac                          |   2 +
256ebe
 rpc/rpc-transport/socket/src/socket.c | 110 ++++++++++++++++++++++++++++------
256ebe
 rpc/rpc-transport/socket/src/socket.h |   2 +
256ebe
 tests/features/ssl-ciphers.t          |  13 +++-
256ebe
 4 files changed, 107 insertions(+), 20 deletions(-)
256ebe
256ebe
diff --git a/configure.ac b/configure.ac
256ebe
index 3065077..0e11d4c 100644
256ebe
--- a/configure.ac
256ebe
+++ b/configure.ac
256ebe
@@ -491,6 +491,8 @@ AC_CHECK_HEADERS([openssl/dh.h])
256ebe
 
256ebe
 AC_CHECK_HEADERS([openssl/ecdh.h])
256ebe
 
256ebe
+AC_CHECK_LIB([ssl], [SSL_CTX_get0_param], [AC_DEFINE([HAVE_SSL_CTX_GET0_PARAM], [1], [define if found OpenSSL SSL_CTX_get0_param])])
256ebe
+
256ebe
 dnl Math library
256ebe
 AC_CHECK_LIB([m], [pow], [MATH_LIB='-lm'], [MATH_LIB=''])
256ebe
 AC_SUBST(MATH_LIB)
256ebe
diff --git a/rpc/rpc-transport/socket/src/socket.c b/rpc/rpc-transport/socket/src/socket.c
256ebe
index f6de1d3..bf2fa71 100644
256ebe
--- a/rpc/rpc-transport/socket/src/socket.c
256ebe
+++ b/rpc/rpc-transport/socket/src/socket.c
256ebe
@@ -308,8 +308,65 @@ out:
256ebe
 #define ssl_write_one(t, b, l)                                                 \
256ebe
     ssl_do((t), (b), (l), (SSL_trinary_func *)SSL_write)
256ebe
 
256ebe
+/* set crl verify flags only for server */
256ebe
+/* see man X509_VERIFY_PARAM_SET_FLAGS(3)
256ebe
+ * X509_V_FLAG_CRL_CHECK enables CRL checking for the certificate chain
256ebe
+ * leaf certificate. An error occurs if a suitable CRL cannot be found.
256ebe
+ * Since we're never going to revoke a gluster node cert, we better disable
256ebe
+ * CRL check for server certs to avoid getting error and failed connection
256ebe
+ * attempts.
256ebe
+ */
256ebe
+static void
256ebe
+ssl_clear_crl_verify_flags(SSL_CTX *ssl_ctx)
256ebe
+{
256ebe
+#ifdef X509_V_FLAG_CRL_CHECK_ALL
256ebe
+#ifdef HAVE_SSL_CTX_GET0_PARAM
256ebe
+    X509_VERIFY_PARAM *vpm;
256ebe
+
256ebe
+    vpm = SSL_CTX_get0_param(ssl_ctx);
256ebe
+    if (vpm) {
256ebe
+        X509_VERIFY_PARAM_clear_flags(
256ebe
+            vpm, (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL));
256ebe
+    }
256ebe
+#else
256ebe
+    /* CRL verify flag need not be cleared for rhel6 kind of clients */
256ebe
+#endif
256ebe
+#else
256ebe
+    gf_log(this->name, GF_LOG_ERROR, "OpenSSL version does not support CRL");
256ebe
+#endif
256ebe
+    return;
256ebe
+}
256ebe
+
256ebe
+/* set crl verify flags only for server */
256ebe
+static void
256ebe
+ssl_set_crl_verify_flags(SSL_CTX *ssl_ctx)
256ebe
+{
256ebe
+#ifdef X509_V_FLAG_CRL_CHECK_ALL
256ebe
+#ifdef HAVE_SSL_CTX_GET0_PARAM
256ebe
+    X509_VERIFY_PARAM *vpm;
256ebe
+
256ebe
+    vpm = SSL_CTX_get0_param(ssl_ctx);
256ebe
+    if (vpm) {
256ebe
+        unsigned long flags;
256ebe
+
256ebe
+        flags = X509_VERIFY_PARAM_get_flags(vpm);
256ebe
+        flags |= (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
256ebe
+        X509_VERIFY_PARAM_set_flags(vpm, flags);
256ebe
+    }
256ebe
+#else
256ebe
+    X509_STORE *x509store;
256ebe
+
256ebe
+    x509store = SSL_CTX_get_cert_store(ssl_ctx);
256ebe
+    X509_STORE_set_flags(x509store,
256ebe
+                         X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
256ebe
+#endif
256ebe
+#else
256ebe
+    gf_log(this->name, GF_LOG_ERROR, "OpenSSL version does not support CRL");
256ebe
+#endif
256ebe
+}
256ebe
+
256ebe
 int
256ebe
-ssl_setup_connection_prefix(rpc_transport_t *this)
256ebe
+ssl_setup_connection_prefix(rpc_transport_t *this, gf_boolean_t server)
256ebe
 {
256ebe
     int ret = -1;
256ebe
     socket_private_t *priv = NULL;
256ebe
@@ -332,6 +389,9 @@ ssl_setup_connection_prefix(rpc_transport_t *this)
256ebe
     priv->ssl_accepted = _gf_false;
256ebe
     priv->ssl_context_created = _gf_false;
256ebe
 
256ebe
+    if (!server && priv->crl_path)
256ebe
+        ssl_clear_crl_verify_flags(priv->ssl_ctx);
256ebe
+
256ebe
     priv->ssl_ssl = SSL_new(priv->ssl_ctx);
256ebe
     if (!priv->ssl_ssl) {
256ebe
         gf_log(this->name, GF_LOG_ERROR, "SSL_new failed");
256ebe
@@ -2664,7 +2724,7 @@ ssl_handle_server_connection_attempt(rpc_transport_t *this)
256ebe
     fd = priv->sock;
256ebe
 
256ebe
     if (!priv->ssl_context_created) {
256ebe
-        ret = ssl_setup_connection_prefix(this);
256ebe
+        ret = ssl_setup_connection_prefix(this, _gf_true);
256ebe
         if (ret < 0) {
256ebe
             gf_log(this->name, GF_LOG_TRACE,
256ebe
                    "> ssl_setup_connection_prefix() failed!");
256ebe
@@ -2718,7 +2778,7 @@ ssl_handle_client_connection_attempt(rpc_transport_t *this)
256ebe
         ret = -1;
256ebe
     } else {
256ebe
         if (!priv->ssl_context_created) {
256ebe
-            ret = ssl_setup_connection_prefix(this);
256ebe
+            ret = ssl_setup_connection_prefix(this, _gf_false);
256ebe
             if (ret < 0) {
256ebe
                 gf_log(this->name, GF_LOG_TRACE,
256ebe
                        "> ssl_setup_connection_prefix() "
256ebe
@@ -3085,7 +3145,30 @@ socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in,
256ebe
         gf_log(this->name, GF_LOG_TRACE, "XXX server:%s, client:%s",
256ebe
                new_trans->myinfo.identifier, new_trans->peerinfo.identifier);
256ebe
 
256ebe
+        /* Make options available to local socket_init() to create new
256ebe
+         * SSL_CTX per transport. A separate SSL_CTX per transport is
256ebe
+         * required to avoid setting crl checking options for client
256ebe
+         * connections. The verification options eventually get copied
256ebe
+         * to the SSL object. Unfortunately, there's no way to identify
256ebe
+         * whether socket_init() is being called after a client-side
256ebe
+         * connect() or a server-side accept(). Although, we could pass
256ebe
+         * a flag from the transport init() to the socket_init() and
256ebe
+         * from this place, this doesn't identify the case where the
256ebe
+         * server-side transport loading is done for the first time.
256ebe
+         * Also, SSL doesn't apply for UNIX sockets.
256ebe
+         */
256ebe
+        if (new_sockaddr.ss_family != AF_UNIX)
256ebe
+            new_trans->options = dict_ref(this->options);
256ebe
+        new_trans->ctx = this->ctx;
256ebe
+
256ebe
         ret = socket_init(new_trans);
256ebe
+
256ebe
+        /* reset options to NULL to avoid double free */
256ebe
+        if (new_sockaddr.ss_family != AF_UNIX) {
256ebe
+            dict_unref(new_trans->options);
256ebe
+            new_trans->options = NULL;
256ebe
+        }
256ebe
+
256ebe
         if (ret != 0) {
256ebe
             gf_log(this->name, GF_LOG_WARNING,
256ebe
                    "initialization of new_trans "
256ebe
@@ -4150,7 +4233,6 @@ ssl_setup_connection_params(rpc_transport_t *this)
256ebe
     char *cipher_list = DEFAULT_CIPHER_LIST;
256ebe
     char *dh_param = DEFAULT_DH_PARAM;
256ebe
     char *ec_curve = DEFAULT_EC_CURVE;
256ebe
-    char *crl_path = NULL;
256ebe
 
256ebe
     priv = this->private;
256ebe
 
256ebe
@@ -4192,6 +4274,7 @@ ssl_setup_connection_params(rpc_transport_t *this)
256ebe
     }
256ebe
     priv->ssl_ca_list = gf_strdup(priv->ssl_ca_list);
256ebe
 
256ebe
+    optstr = NULL;
256ebe
     if (dict_get_str(this->options, SSL_CRL_PATH_OPT, &optstr) == 0) {
256ebe
         if (!priv->ssl_enabled) {
256ebe
             gf_log(this->name, GF_LOG_WARNING,
256ebe
@@ -4199,9 +4282,9 @@ ssl_setup_connection_params(rpc_transport_t *this)
256ebe
                    SSL_ENABLED_OPT);
256ebe
         }
256ebe
         if (strcasecmp(optstr, "NULL") == 0)
256ebe
-            crl_path = NULL;
256ebe
+            priv->crl_path = NULL;
256ebe
         else
256ebe
-            crl_path = optstr;
256ebe
+            priv->crl_path = gf_strdup(optstr);
256ebe
     }
256ebe
 
256ebe
     gf_log(this->name, priv->ssl_enabled ? GF_LOG_INFO : GF_LOG_DEBUG,
256ebe
@@ -4343,24 +4426,15 @@ ssl_setup_connection_params(rpc_transport_t *this)
256ebe
         }
256ebe
 
256ebe
         if (!SSL_CTX_load_verify_locations(priv->ssl_ctx, priv->ssl_ca_list,
256ebe
-                                           crl_path)) {
256ebe
+                                           priv->crl_path)) {
256ebe
             gf_log(this->name, GF_LOG_ERROR, "could not load CA list");
256ebe
             goto err;
256ebe
         }
256ebe
 
256ebe
         SSL_CTX_set_verify_depth(priv->ssl_ctx, cert_depth);
256ebe
 
256ebe
-        if (crl_path) {
256ebe
-#ifdef X509_V_FLAG_CRL_CHECK_ALL
256ebe
-            X509_STORE *x509store;
256ebe
-
256ebe
-            x509store = SSL_CTX_get_cert_store(priv->ssl_ctx);
256ebe
-            X509_STORE_set_flags(
256ebe
-                x509store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
256ebe
-#else
256ebe
-            gf_log(this->name, GF_LOG_ERROR,
256ebe
-                   "OpenSSL version does not support CRL");
256ebe
-#endif
256ebe
+        if (priv->crl_path) {
256ebe
+            ssl_set_crl_verify_flags(priv->ssl_ctx);
256ebe
         }
256ebe
 
256ebe
         priv->ssl_session_id = session_id++;
256ebe
diff --git a/rpc/rpc-transport/socket/src/socket.h b/rpc/rpc-transport/socket/src/socket.h
256ebe
index e1ccae2..e7c0090 100644
256ebe
--- a/rpc/rpc-transport/socket/src/socket.h
256ebe
+++ b/rpc/rpc-transport/socket/src/socket.h
256ebe
@@ -14,6 +14,7 @@
256ebe
 #include <openssl/ssl.h>
256ebe
 #include <openssl/err.h>
256ebe
 #include <openssl/x509v3.h>
256ebe
+#include <openssl/x509_vfy.h>
256ebe
 #ifdef HAVE_OPENSSL_DH_H
256ebe
 #include <openssl/dh.h>
256ebe
 #endif
256ebe
@@ -246,6 +247,7 @@ typedef struct {
256ebe
     char *ssl_own_cert;
256ebe
     char *ssl_private_key;
256ebe
     char *ssl_ca_list;
256ebe
+    char *crl_path;
256ebe
     int pipe[2];
256ebe
     struct gf_sock_incoming incoming;
256ebe
     /* -1 = not connected. 0 = in progress. 1 = connected */
256ebe
diff --git a/tests/features/ssl-ciphers.t b/tests/features/ssl-ciphers.t
256ebe
index 563d37c..7e1e199 100644
256ebe
--- a/tests/features/ssl-ciphers.t
256ebe
+++ b/tests/features/ssl-ciphers.t
256ebe
@@ -175,8 +175,6 @@ BRICK_PORT=`brick_port $V0`
256ebe
 EXPECT "Y" openssl_connect -cipher EECDH -connect $H0:$BRICK_PORT
256ebe
 
256ebe
 # test revocation
256ebe
-# no need to restart the volume since the options are used
256ebe
-# by the client here.
256ebe
 TEST $CLI volume set $V0 ssl.crl-path $TMPDIR
256ebe
 EXPECT $TMPDIR volume_option $V0 ssl.crl-path
256ebe
 $GFS --volfile-id=$V0 --volfile-server=$H0 $M0
256ebe
@@ -189,14 +187,25 @@ TEST openssl ca -batch -config $SSL_CFG -revoke $SSL_CERT 2>&1
256ebe
 TEST openssl ca -config $SSL_CFG -gencrl -out $SSL_CRL 2>&1
256ebe
 
256ebe
 # Failed once revoked
256ebe
+# Although client fails to mount without restarting the server after crl-path
256ebe
+# is set when no actual crl file is found on the client, it would also fail
256ebe
+# when server is restarted for the same reason. Since the socket initialization
256ebe
+# code is the same for client and server, the crl verification flags need to
256ebe
+# be turned off for the client to avoid SSL searching for CRLs in the
256ebe
+# ssl.crl-path. If no CRL files are found in the ssl.crl-path, SSL fails the
256ebe
+# connect() attempt on the client.
256ebe
+TEST $CLI volume stop $V0
256ebe
+TEST $CLI volume start $V0
256ebe
 $GFS --volfile-id=$V0 --volfile-server=$H0 $M0
256ebe
 EXPECT "N" wait_mount $M0
256ebe
 TEST ! test -f $TEST_FILE
256ebe
 EXPECT_WITHIN $UMOUNT_TIMEOUT "Y" force_umount $M0
256ebe
 
256ebe
 # Succeed with CRL disabled
256ebe
+TEST $CLI volume stop $V0
256ebe
 TEST $CLI volume set $V0 ssl.crl-path NULL
256ebe
 EXPECT NULL volume_option $V0 ssl.crl-path
256ebe
+TEST $CLI volume start $V0
256ebe
 $GFS --volfile-id=$V0 --volfile-server=$H0 $M0
256ebe
 EXPECT "Y" wait_mount $M0
256ebe
 TEST test -f $TEST_FILE
256ebe
-- 
256ebe
1.8.3.1
256ebe