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