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