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