sbonazzo / rpms / cyrus-sasl

Forked from rpms/cyrus-sasl 2 years ago
Clone

Blame SOURCES/cyrus-sasl-2.1.26-gss-spnego.patch

6611fc
From 67ca66685e11acc0f69d5ff8013107d4b172e67f Mon Sep 17 00:00:00 2001
6611fc
From: Simo Sorce <simo@redhat.com>
6611fc
Date: Thu, 16 Feb 2017 15:25:56 -0500
6611fc
Subject: [PATCH] Fix GSS-SPNEGO mechanism's incompatible behavior
6611fc
6611fc
The GSS-SPNEGO mechanism has been designed and introduced by Microsoft for use
6611fc
by Active Directory clients. It allows to negotiate an underlying
6611fc
Security Mechanism like Krb5 or NTLMSSP.
6611fc
However, the implementaion in cyrus-sasl is broken and never correctly
6611fc
interoperated with Microsoft servers or clients. This patch fixes the
6611fc
compatibility issue which is caused by incorrectly trying to negotiate
6611fc
SSF layers explicitly instead of using the flags negotiated by GSSAPI
6611fc
as required by Microsoft's implementation.
6611fc
6611fc
Signed-off-by: Simo Sorce <simo@redhat.com>
6611fc
---
6611fc
 plugins/gssapi.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
6611fc
 1 file changed, 64 insertions(+), 6 deletions(-)
6611fc
6611fc
diff --git a/plugins/gssapi.c b/plugins/gssapi.c
6611fc
index bfc278d..010c236 100644
6611fc
--- a/plugins/gssapi.c
6611fc
+++ b/plugins/gssapi.c
6611fc
@@ -648,10 +648,62 @@ static void gssapi_common_mech_free(void *global_context __attribute__((unused))
6611fc
 #endif
6611fc
 }
6611fc
 
6611fc
+/* The GSS-SPNEGO mechanism does not do SSF negotiation, instead it uses the
6611fc
+ * flags negotiated by GSSAPI to determine If confidentiality or integrity are
6611fc
+ * used. These flags are stored in text->qop transalated as layers by the
6611fc
+ * caller */
6611fc
+static int gssapi_spnego_ssf(context_t *text, const sasl_utils_t *utils,
6611fc
+                             sasl_security_properties_t *props,
6611fc
+                             sasl_out_params_t *oparams)
6611fc
+{
6611fc
+    OM_uint32 maj_stat = 0, min_stat = 0;
6611fc
+    OM_uint32 max_input;
6611fc
+
6611fc
+    if (text->qop & LAYER_CONFIDENTIALITY) {
6611fc
+        oparams->encode = &gssapi_privacy_encode;
6611fc
+        oparams->decode = &gssapi_decode;
6611fc
+        oparams->mech_ssf = K5_MAX_SSF;
6611fc
+    } else if (text->qop & LAYER_INTEGRITY) {
6611fc
+        oparams->encode = &gssapi_integrity_encode;
6611fc
+        oparams->decode = &gssapi_decode;
6611fc
+        oparams->mech_ssf = 1;
6611fc
+    } else {
6611fc
+        oparams->encode = NULL;
6611fc
+        oparams->decode = NULL;
6611fc
+        oparams->mech_ssf = 0;
6611fc
+    }
6611fc
+
6611fc
+    if (oparams->mech_ssf) {
6611fc
+        maj_stat = gss_wrap_size_limit(&min_stat,
6611fc
+                                       text->gss_ctx,
6611fc
+                                       1,
6611fc
+                                       GSS_C_QOP_DEFAULT,
6611fc
+                                       (OM_uint32)oparams->maxoutbuf,
6611fc
+                                       &max_input);
6611fc
+
6611fc
+	if (max_input > oparams->maxoutbuf) {
6611fc
+	    /* Heimdal appears to get this wrong */
6611fc
+	    oparams->maxoutbuf -= (max_input - oparams->maxoutbuf);
6611fc
+	} else {
6611fc
+	    /* This code is actually correct */
6611fc
+	    oparams->maxoutbuf = max_input;
6611fc
+	}
6611fc
+    }
6611fc
+
6611fc
+    text->state = SASL_GSSAPI_STATE_AUTHENTICATED;
6611fc
+
6611fc
+    /* used by layers */
6611fc
+    _plug_decode_init(&text->decode_context, text->utils,
6611fc
+		      (props->maxbufsize > 0xFFFFFF) ? 0xFFFFFF :
6611fc
+                      props->maxbufsize);
6611fc
+
6611fc
+    return SASL_OK;
6611fc
+}
6611fc
+
6611fc
 /*****************************  Server Section  *****************************/
6611fc
 
6611fc
 static int 
6611fc
-gssapi_server_mech_new(void *glob_context __attribute__((unused)), 
6611fc
+gssapi_server_mech_new(void *glob_context,
6611fc
 		       sasl_server_params_t *params,
6611fc
 		       const char *challenge __attribute__((unused)), 
6611fc
 		       unsigned challen __attribute__((unused)),
6611fc
@@ -673,6 +725,7 @@ gssapi_server_mech_new(void *glob_context __attribute__((unused)),
6611fc
     text->state = SASL_GSSAPI_STATE_AUTHNEG;
6611fc
     
6611fc
     text->http_mode = (params->flags & SASL_NEED_HTTP);
6611fc
+    text->mech_type = (gss_OID) glob_context;
6611fc
 
6611fc
     *conn_context = text;
6611fc
     
6611fc
@@ -686,7 +739,7 @@ gssapi_server_mech_authneg(context_t *text,
6611fc
 			   unsigned clientinlen,
6611fc
 			   const char **serverout,
6611fc
 			   unsigned *serveroutlen,
6611fc
-			   sasl_out_params_t *oparams __attribute__((unused)))
6611fc
+			   sasl_out_params_t *oparams)
6611fc
 {
6611fc
     gss_buffer_t input_token, output_token;
6611fc
     gss_buffer_desc real_input_token, real_output_token;
6611fc
@@ -965,8 +1018,9 @@ gssapi_server_mech_authneg(context_t *text,
6611fc
 	/* HTTP doesn't do any ssf negotiation */
6611fc
 	text->state = SASL_GSSAPI_STATE_AUTHENTICATED;
6611fc
 	ret = SASL_OK;
6611fc
-    }
6611fc
-    else {
6611fc
+    } else if (text->mech_type && text->mech_type == &gss_spnego_oid) {
6611fc
+        ret = gssapi_spnego_ssf(text, params->utils, &params->props, oparams);
6611fc
+    } else {
6611fc
 	/* Switch to ssf negotiation */
6611fc
 	text->state = SASL_GSSAPI_STATE_SSFCAP;
6611fc
 	ret = SASL_CONTINUE;
6611fc
@@ -1391,7 +1445,7 @@ static sasl_server_plug_t gssapi_server_plugins[] =
6611fc
 	| SASL_FEAT_ALLOWS_PROXY
6611fc
 	| SASL_FEAT_DONTUSE_USERPASSWD
6611fc
 	| SASL_FEAT_SUPPORTS_HTTP,	/* features */
6611fc
-	NULL,				/* glob_context */
6611fc
+	&gss_spnego_oid,		/* glob_context */
6611fc
 	&gssapi_server_mech_new,	/* mech_new */
6611fc
 	&gssapi_server_mech_step,	/* mech_step */
6611fc
 	&gssapi_common_mech_dispose,	/* mech_dispose */
6611fc
@@ -1769,7 +1823,11 @@ static int gssapi_client_mech_step(void *conn_context,
6611fc
 		text->state = SASL_GSSAPI_STATE_AUTHENTICATED;
6611fc
 		oparams->doneflag = 1;
6611fc
 		return SASL_OK;
6611fc
-	    }
6611fc
+	    } else if (text->mech_type && text->mech_type == &gss_spnego_oid) {
6611fc
+		oparams->doneflag = 1;
6611fc
+                return gssapi_spnego_ssf(text, params->utils, &params->props,
6611fc
+                                         oparams);
6611fc
+            }
6611fc
 
6611fc
 	    /* Switch to ssf negotiation */
6611fc
 	    text->state = SASL_GSSAPI_STATE_SSFCAP;
6611fc