Blame SOURCES/cyrus-sasl-2.1.27-Add-Channel-Binding-support-for-GSSAPI-GSS-SPNEGO.patch

b0654d
From aa8b6b2275fd14ba2cca3d2339ae61c7e7ddfa70 Mon Sep 17 00:00:00 2001
b0654d
From: Simo Sorce <simo@redhat.com>
b0654d
Date: Tue, 5 May 2020 14:08:48 -0400
b0654d
Subject: [PATCH] Add Channel Binding support for GSSAPI/GSS-SPNEGO
b0654d
b0654d
Backport of commit ids:
b0654d
829a6ed086432e26dafa9d1dcf892aef4c42cfbd
b0654d
944bd8a6205f840b105206ef83e8f6b9dff0138e
b0654d
b0654d
Signed-off-by: Simo Sorce <simo@redhat.com>
b0654d
---
b0654d
 plugins/gssapi.c     | 30 +++++++++++---
b0654d
 tests/runtests.py    | 93 ++++++++++++++++++++++++++++++++++++++++----
b0654d
 tests/t_common.c     | 24 ++++++++----
b0654d
 tests/t_common.h     |  5 ++-
b0654d
 tests/t_gssapi_cli.c | 24 ++++++++++--
b0654d
 tests/t_gssapi_srv.c | 24 ++++++++++--
b0654d
 6 files changed, 172 insertions(+), 28 deletions(-)
b0654d
b0654d
diff --git a/plugins/gssapi.c b/plugins/gssapi.c
b0654d
index ff663da..5d900c5 100644
b0654d
--- a/plugins/gssapi.c
b0654d
+++ b/plugins/gssapi.c
b0654d
@@ -830,7 +830,9 @@ gssapi_server_mech_authneg(context_t *text,
b0654d
     gss_buffer_desc name_without_realm;
b0654d
     gss_name_t client_name_MN = NULL, without = NULL;
b0654d
     gss_OID mech_type;
b0654d
-	
b0654d
+    gss_channel_bindings_t bindings = GSS_C_NO_CHANNEL_BINDINGS;
b0654d
+    struct gss_channel_bindings_struct cb = {0};
b0654d
+
b0654d
     input_token = &real_input_token;
b0654d
     output_token = &real_output_token;
b0654d
     output_token->value = NULL; output_token->length = 0;
b0654d
@@ -902,6 +904,12 @@ gssapi_server_mech_authneg(context_t *text,
b0654d
 	real_input_token.length = clientinlen;
b0654d
     }
b0654d
 
b0654d
+    if (params->cbinding != NULL) {
b0654d
+        cb.application_data.length = params->cbinding->len;
b0654d
+        cb.application_data.value = params->cbinding->data;
b0654d
+        bindings = &cb;
b0654d
+    }
b0654d
+
b0654d
 
b0654d
     GSS_LOCK_MUTEX_CTX(params->utils, text);
b0654d
     maj_stat =
b0654d
@@ -909,7 +917,7 @@ gssapi_server_mech_authneg(context_t *text,
b0654d
 			       &(text->gss_ctx),
b0654d
 			       server_creds,
b0654d
 			       input_token,
b0654d
-			       GSS_C_NO_CHANNEL_BINDINGS,
b0654d
+			       bindings,
b0654d
 			       &text->client_name,
b0654d
 			       &mech_type,
b0654d
 			       output_token,
b0654d
@@ -1505,7 +1513,8 @@ static sasl_server_plug_t gssapi_server_plugins[] =
b0654d
 	| SASL_SEC_PASS_CREDENTIALS,
b0654d
 	SASL_FEAT_WANT_CLIENT_FIRST
b0654d
 	| SASL_FEAT_ALLOWS_PROXY
b0654d
-	| SASL_FEAT_DONTUSE_USERPASSWD,	/* features */
b0654d
+	| SASL_FEAT_DONTUSE_USERPASSWD
b0654d
+	| SASL_FEAT_CHANNEL_BINDING,	/* features */
b0654d
 	NULL,				/* glob_context */
b0654d
 	&gssapi_server_mech_new,	/* mech_new */
b0654d
 	&gssapi_server_mech_step,	/* mech_step */
b0654d
@@ -1529,6 +1538,7 @@ static sasl_server_plug_t gssapi_server_plugins[] =
b0654d
 	SASL_FEAT_WANT_CLIENT_FIRST
b0654d
 	| SASL_FEAT_ALLOWS_PROXY
b0654d
 	| SASL_FEAT_DONTUSE_USERPASSWD
b0654d
+	| SASL_FEAT_CHANNEL_BINDING
b0654d
 	| SASL_FEAT_SUPPORTS_HTTP,	/* features */
b0654d
 	&gss_spnego_oid,		/* glob_context */
b0654d
 	&gssapi_server_mech_new,	/* mech_new */
b0654d
@@ -1662,6 +1672,8 @@ static int gssapi_client_mech_step(void *conn_context,
b0654d
     input_token->value = NULL; 
b0654d
     input_token->length = 0;
b0654d
     gss_cred_id_t client_creds = (gss_cred_id_t)params->gss_creds;
b0654d
+    gss_channel_bindings_t bindings = GSS_C_NO_CHANNEL_BINDINGS;
b0654d
+    struct gss_channel_bindings_struct cb = {0};
b0654d
 
b0654d
     if (clientout)
b0654d
         *clientout = NULL;
b0654d
@@ -1777,6 +1789,12 @@ static int gssapi_client_mech_step(void *conn_context,
b0654d
 	    req_flags = req_flags |  GSS_C_DELEG_FLAG;
b0654d
 	}
b0654d
 
b0654d
+        if (params->cbinding != NULL) {
b0654d
+            cb.application_data.length = params->cbinding->len;
b0654d
+            cb.application_data.value = params->cbinding->data;
b0654d
+            bindings = &cb;
b0654d
+        }
b0654d
+
b0654d
 	GSS_LOCK_MUTEX_CTX(params->utils, text);
b0654d
 	maj_stat = gss_init_sec_context(&min_stat,
b0654d
 					client_creds, /* GSS_C_NO_CREDENTIAL */
b0654d
@@ -1785,7 +1803,7 @@ static int gssapi_client_mech_step(void *conn_context,
b0654d
 					text->mech_type,
b0654d
 					req_flags,
b0654d
 					0,
b0654d
-					GSS_C_NO_CHANNEL_BINDINGS,
b0654d
+					bindings,
b0654d
 					input_token,
b0654d
 					NULL,
b0654d
 					output_token,
b0654d
@@ -2190,7 +2208,8 @@ static sasl_client_plug_t gssapi_client_plugins[] =
b0654d
 	| SASL_SEC_PASS_CREDENTIALS,    /* security_flags */
b0654d
 	SASL_FEAT_NEEDSERVERFQDN
b0654d
 	| SASL_FEAT_WANT_CLIENT_FIRST
b0654d
-	| SASL_FEAT_ALLOWS_PROXY,	/* features */
b0654d
+	| SASL_FEAT_ALLOWS_PROXY
b0654d
+	| SASL_FEAT_CHANNEL_BINDING,	/* features */
b0654d
 	gssapi_required_prompts,	/* required_prompts */
b0654d
 	GSS_C_NO_OID,			/* glob_context */
b0654d
 	&gssapi_client_mech_new,	/* mech_new */
b0654d
@@ -2213,6 +2232,7 @@ static sasl_client_plug_t gssapi_client_plugins[] =
b0654d
 	SASL_FEAT_NEEDSERVERFQDN
b0654d
 	| SASL_FEAT_WANT_CLIENT_FIRST
b0654d
 	| SASL_FEAT_ALLOWS_PROXY
b0654d
+	| SASL_FEAT_CHANNEL_BINDING
b0654d
 	| SASL_FEAT_SUPPORTS_HTTP,	/* features */
b0654d
 	gssapi_required_prompts,	/* required_prompts */
b0654d
 	&gss_spnego_oid,		/* glob_context */
b0654d
diff --git a/tests/runtests.py b/tests/runtests.py
b0654d
index f645adf..fc9cf24 100755
b0654d
--- a/tests/runtests.py
b0654d
+++ b/tests/runtests.py
b0654d
@@ -1,6 +1,7 @@
b0654d
 #!/usr/bin/python3
b0654d
 
b0654d
 import argparse
b0654d
+import base64
b0654d
 import os
b0654d
 import shutil
b0654d
 import signal
b0654d
@@ -126,14 +127,7 @@ def setup_kdc(testdir, env):
b0654d
 
b0654d
     return kdc, env
b0654d
 
b0654d
-
b0654d
-def gssapi_tests(testdir):
b0654d
-    """ SASL/GSSAPI Tests """
b0654d
-    env = setup_socket_wrappers(testdir)
b0654d
-    kdc, kenv = setup_kdc(testdir, env)
b0654d
-    #print("KDC: {}, ENV: {}".format(kdc, kenv))
b0654d
-    kenv['KRB5_TRACE'] = os.path.join(testdir, 'trace.log')
b0654d
-
b0654d
+def gssapi_basic_test(kenv):
b0654d
     try:
b0654d
         srv = subprocess.Popen(["../tests/t_gssapi_srv"],
b0654d
                                stdout=subprocess.PIPE,
b0654d
@@ -155,11 +149,94 @@ def gssapi_tests(testdir):
b0654d
                 srv.returncode, srv.stderr.read().decode('utf-8')))
b0654d
     except Exception as e:
b0654d
         print("FAIL: {}".format(e))
b0654d
+        return
b0654d
+
b0654d
+    print("PASS: CLI({}) SRV({})".format(
b0654d
+        cli.stdout.read().decode('utf-8').strip(),
b0654d
+        srv.stdout.read().decode('utf-8').strip()))
b0654d
+
b0654d
+def gssapi_channel_binding_test(kenv):
b0654d
+    try:
b0654d
+        bindings = base64.b64encode("MATCHING CBS".encode('utf-8'))
b0654d
+        srv = subprocess.Popen(["../tests/t_gssapi_srv", "-c", bindings],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        srv.stdout.readline() # Wait for srv to say it is ready
b0654d
+        cli = subprocess.Popen(["../tests/t_gssapi_cli", "-c", bindings],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        try:
b0654d
+            cli.wait(timeout=5)
b0654d
+            srv.wait(timeout=5)
b0654d
+        except Exception as e:
b0654d
+            print("Failed on {}".format(e));
b0654d
+            cli.kill()
b0654d
+            srv.kill()
b0654d
+        if cli.returncode != 0 or srv.returncode != 0:
b0654d
+            raise Exception("CLI ({}): {} --> SRV ({}): {}".format(
b0654d
+                cli.returncode, cli.stderr.read().decode('utf-8'),
b0654d
+                srv.returncode, srv.stderr.read().decode('utf-8')))
b0654d
+    except Exception as e:
b0654d
+        print("FAIL: {}".format(e))
b0654d
+        return
b0654d
 
b0654d
     print("PASS: CLI({}) SRV({})".format(
b0654d
         cli.stdout.read().decode('utf-8').strip(),
b0654d
         srv.stdout.read().decode('utf-8').strip()))
b0654d
 
b0654d
+def gssapi_channel_binding_mismatch_test(kenv):
b0654d
+    result = "FAIL"
b0654d
+    try:
b0654d
+        bindings = base64.b64encode("SRV CBS".encode('utf-8'))
b0654d
+        srv = subprocess.Popen(["../tests/t_gssapi_srv", "-c", bindings],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        srv.stdout.readline() # Wait for srv to say it is ready
b0654d
+        bindings = base64.b64encode("CLI CBS".encode('utf-8'))
b0654d
+        cli = subprocess.Popen(["../tests/t_gssapi_cli", "-c", bindings],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        try:
b0654d
+            cli.wait(timeout=5)
b0654d
+            srv.wait(timeout=5)
b0654d
+        except Exception as e:
b0654d
+            print("Failed on {}".format(e));
b0654d
+            cli.kill()
b0654d
+            srv.kill()
b0654d
+        if cli.returncode != 0 or srv.returncode != 0:
b0654d
+            cli_err = cli.stderr.read().decode('utf-8').strip()
b0654d
+            srv_err = srv.stderr.read().decode('utf-8').strip()
b0654d
+            if "authentication failure" in srv_err:
b0654d
+                result = "PASS"
b0654d
+            raise Exception("CLI ({}): {} --> SRV ({}): {}".format(
b0654d
+                cli.returncode, cli_err, srv.returncode, srv_err))
b0654d
+    except Exception as e:
b0654d
+        print("{}: {}".format(result, e))
b0654d
+        return
b0654d
+
b0654d
+    print("FAIL: This test should fail [CLI({}) SRV({})]".format(
b0654d
+        cli.stdout.read().decode('utf-8').strip(),
b0654d
+        srv.stdout.read().decode('utf-8').strip()))
b0654d
+
b0654d
+def gssapi_tests(testdir):
b0654d
+    """ SASL/GSSAPI Tests """
b0654d
+    env = setup_socket_wrappers(testdir)
b0654d
+    kdc, kenv = setup_kdc(testdir, env)
b0654d
+    #print("KDC: {}, ENV: {}".format(kdc, kenv))
b0654d
+    kenv['KRB5_TRACE'] = os.path.join(testdir, 'trace.log')
b0654d
+
b0654d
+    print('GSSAPI BASIC:')
b0654d
+    print('    ', end='')
b0654d
+    gssapi_basic_test(kenv)
b0654d
+
b0654d
+    print('GSSAPI CHANNEL BINDING:')
b0654d
+    print('    ', end='')
b0654d
+    gssapi_channel_binding_test(kenv)
b0654d
+
b0654d
+    print('GSSAPI CHANNEL BINDING MISMTACH:')
b0654d
+    print('    ', end='')
b0654d
+    gssapi_channel_binding_mismatch_test(kenv)
b0654d
+
b0654d
     os.killpg(kdc.pid, signal.SIGTERM)
b0654d
 
b0654d
 
b0654d
diff --git a/tests/t_common.c b/tests/t_common.c
b0654d
index 7168b2f..478e6a1 100644
b0654d
--- a/tests/t_common.c
b0654d
+++ b/tests/t_common.c
b0654d
@@ -1,4 +1,5 @@
b0654d
-/* TBD, add (C) */
b0654d
+/* Copyright (C) Simo Sorce <simo@redhat.com>
b0654d
+ * See COPYING file for License */
b0654d
 
b0654d
 #include <t_common.h>
b0654d
 
b0654d
@@ -13,9 +14,6 @@ void send_string(int sd, const char *s, unsigned int l)
b0654d
 {
b0654d
     ssize_t ret;
b0654d
 
b0654d
-fprintf(stderr, "s:%u ", l);
b0654d
-fflush(stderr);
b0654d
-
b0654d
     ret = send(sd, &l, sizeof(l), 0);
b0654d
     if (ret != sizeof(l)) s_error("send size", ret, sizeof(l), errno);
b0654d
 
b0654d
@@ -34,8 +32,6 @@ void recv_string(int sd, char *buf, unsigned int *buflen)
b0654d
     if (ret != sizeof(l)) s_error("recv size", ret, sizeof(l), errno);
b0654d
 
b0654d
     if (l == 0) {
b0654d
-fprintf(stderr, "r:0 ");
b0654d
-fflush(stderr);
b0654d
         *buflen = 0;
b0654d
         return;
b0654d
     }
b0654d
@@ -45,8 +41,6 @@ fflush(stderr);
b0654d
     ret = recv(sd, buf, l, 0);
b0654d
     if (ret != l) s_error("recv data", ret, l, errno);
b0654d
 
b0654d
-fprintf(stderr, "r:%ld ", ret);
b0654d
-fflush(stderr);
b0654d
     *buflen = ret;
b0654d
 }
b0654d
 
b0654d
@@ -65,4 +59,18 @@ int getpath(void *context __attribute__((unused)), const char **path)
b0654d
     return SASL_OK;
b0654d
 }
b0654d
 
b0654d
+void parse_cb(sasl_channel_binding_t *cb, char *buf, unsigned max, char *in)
b0654d
+{
b0654d
+    unsigned len;
b0654d
+    int r;
b0654d
 
b0654d
+    r = sasl_decode64(in, strlen(in), buf, max, &len;;
b0654d
+    if (r != SASL_OK) {
b0654d
+        saslerr(r, "failed to parse channel bindings");
b0654d
+        exit(-1);
b0654d
+    }
b0654d
+    cb->name = "TEST BINDINGS";
b0654d
+    cb->critical = 0;
b0654d
+    cb->data = (unsigned char *)buf;
b0654d
+    cb->len = len;
b0654d
+}
b0654d
diff --git a/tests/t_common.h b/tests/t_common.h
b0654d
index 4ee1976..a10def1 100644
b0654d
--- a/tests/t_common.h
b0654d
+++ b/tests/t_common.h
b0654d
@@ -1,4 +1,5 @@
b0654d
-/* TBD, add (C) */
b0654d
+/* Copyright (C) Simo Sorce <simo@redhat.com>
b0654d
+ * See COPYING file for License */
b0654d
 
b0654d
 #include "config.h"
b0654d
 
b0654d
@@ -7,9 +8,11 @@
b0654d
 #include <sys/socket.h>
b0654d
 
b0654d
 #include <sasl.h>
b0654d
+#include <saslutil.h>
b0654d
 
b0654d
 void s_error(const char *hdr, ssize_t ret, ssize_t len, int err);
b0654d
 void send_string(int sd, const char *s, unsigned int l);
b0654d
 void recv_string(int sd, char *buf, unsigned int *buflen);
b0654d
 void saslerr(int why, const char *what);
b0654d
 int getpath(void *context __attribute__((unused)), const char **path);
b0654d
+void parse_cb(sasl_channel_binding_t *cb, char *buf, unsigned max, char *in);
b0654d
diff --git a/tests/t_gssapi_cli.c b/tests/t_gssapi_cli.c
b0654d
index c833c05..a44a3f5 100644
b0654d
--- a/tests/t_gssapi_cli.c
b0654d
+++ b/tests/t_gssapi_cli.c
b0654d
@@ -1,4 +1,5 @@
b0654d
-/* TBD, add (C) */
b0654d
+/* Copyright (C) Simo Sorce <simo@redhat.com>
b0654d
+ * See COPYING file for License */
b0654d
 
b0654d
 #include "t_common.h"
b0654d
 
b0654d
@@ -13,6 +14,7 @@
b0654d
 
b0654d
 #include <arpa/inet.h>
b0654d
 #include <saslplug.h>
b0654d
+#include <saslutil.h>
b0654d
 
b0654d
 static int setup_socket(void)
b0654d
 {
b0654d
@@ -32,7 +34,7 @@ static int setup_socket(void)
b0654d
     return sock;
b0654d
 }
b0654d
 
b0654d
-int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
b0654d
+int main(int argc, char *argv[])
b0654d
 {
b0654d
     sasl_callback_t callbacks[2] = {};
b0654d
     char buf[8192];
b0654d
@@ -40,8 +42,20 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
b0654d
     sasl_conn_t *conn;
b0654d
     const char *data;
b0654d
     unsigned int len;
b0654d
+    sasl_channel_binding_t cb = {0};
b0654d
+    char cb_buf[256];
b0654d
     int sd;
b0654d
-    int r;
b0654d
+    int c, r;
b0654d
+
b0654d
+    while ((c = getopt(argc, argv, "c:")) != EOF) {
b0654d
+        switch (c) {
b0654d
+        case 'c':
b0654d
+            parse_cb(&cb, cb_buf, 256, optarg);
b0654d
+            break;
b0654d
+        default:
b0654d
+            break;
b0654d
+        }
b0654d
+    }
b0654d
 
b0654d
     /* initialize the sasl library */
b0654d
     callbacks[0].id = SASL_CB_GETPATH;
b0654d
@@ -60,6 +74,10 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
b0654d
         exit(-1);
b0654d
     }
b0654d
 
b0654d
+    if (cb.name) {
b0654d
+        sasl_setprop(conn, SASL_CHANNEL_BINDING, &cb;;
b0654d
+    }
b0654d
+
b0654d
     r = sasl_client_start(conn, "GSSAPI", NULL, &data, &len, &chosenmech);
b0654d
     if (r != SASL_OK && r != SASL_CONTINUE) {
b0654d
 	saslerr(r, "starting SASL negotiation");
b0654d
diff --git a/tests/t_gssapi_srv.c b/tests/t_gssapi_srv.c
b0654d
index 29f538d..ef1217f 100644
b0654d
--- a/tests/t_gssapi_srv.c
b0654d
+++ b/tests/t_gssapi_srv.c
b0654d
@@ -1,4 +1,5 @@
b0654d
-/* TBD, add (C) */
b0654d
+/* Copyright (C) Simo Sorce <simo@redhat.com>
b0654d
+ * See COPYING file for License */
b0654d
 
b0654d
 #include "t_common.h"
b0654d
 
b0654d
@@ -44,15 +45,28 @@ static int setup_socket(void)
b0654d
     return sd;
b0654d
 }
b0654d
 
b0654d
-int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
b0654d
+int main(int argc, char *argv[])
b0654d
 {
b0654d
     sasl_callback_t callbacks[2] = {};
b0654d
     char buf[8192];
b0654d
     sasl_conn_t *conn;
b0654d
     const char *data;
b0654d
     unsigned int len;
b0654d
+    sasl_channel_binding_t cb = {0};
b0654d
+    unsigned char cb_buf[256];
b0654d
     int sd;
b0654d
-    int r;
b0654d
+    int c, r;
b0654d
+
b0654d
+    while ((c = getopt(argc, argv, "c:")) != EOF) {
b0654d
+        switch (c) {
b0654d
+        case 'c':
b0654d
+            parse_cb(&cb, cb_buf, 256, optarg);
b0654d
+            break;
b0654d
+        default:
b0654d
+            break;
b0654d
+        }
b0654d
+    }
b0654d
+
b0654d
 
b0654d
     /* initialize the sasl library */
b0654d
     callbacks[0].id = SASL_CB_GETPATH;
b0654d
@@ -72,6 +86,10 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
b0654d
         exit(-1);
b0654d
     }
b0654d
 
b0654d
+    if (cb.name) {
b0654d
+        sasl_setprop(conn, SASL_CHANNEL_BINDING, &cb;;
b0654d
+    }
b0654d
+
b0654d
     sd = setup_socket();
b0654d
 
b0654d
     len = 8192;
b0654d
-- 
b0654d
2.18.2
b0654d