kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
8f2528
diff -up openssh-7.4p1/auth2.c.gsskex openssh-7.4p1/auth2.c
8f2528
--- openssh-7.4p1/auth2.c.gsskex	2016-12-23 13:38:53.685300997 +0100
8f2528
+++ openssh-7.4p1/auth2.c	2016-12-23 13:38:53.725301005 +0100
8f2528
@@ -70,6 +70,7 @@ extern Authmethod method_passwd;
8f2528
 extern Authmethod method_kbdint;
8f2528
 extern Authmethod method_hostbased;
8f2528
 #ifdef GSSAPI
8f2528
+extern Authmethod method_gsskeyex;
8f2528
 extern Authmethod method_gssapi;
8f2528
 #endif
8f2528
 
8f2528
@@ -77,6 +78,7 @@ Authmethod *authmethods[] = {
8f2528
 	&method_none,
8f2528
 	&method_pubkey,
8f2528
 #ifdef GSSAPI
8f2528
+	&method_gsskeyex,
8f2528
 	&method_gssapi,
8f2528
 #endif
8f2528
 	&method_passwd,
8f2528
diff -up openssh-7.4p1/auth2-gss.c.gsskex openssh-7.4p1/auth2-gss.c
8f2528
--- openssh-7.4p1/auth2-gss.c.gsskex	2016-12-23 13:38:53.685300997 +0100
8f2528
+++ openssh-7.4p1/auth2-gss.c	2016-12-23 13:38:53.725301005 +0100
8f2528
@@ -31,6 +31,7 @@
8f2528
 #include <sys/types.h>
8f2528
 
8f2528
 #include <stdarg.h>
8f2528
+#include <string.h>
8f2528
 
8f2528
 #include "xmalloc.h"
8f2528
 #include "key.h"
8f2528
@@ -53,6 +54,40 @@ static int input_gssapi_mic(int type, u_
8f2528
 static int input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
8f2528
 static int input_gssapi_errtok(int, u_int32_t, void *);
8f2528
 
8f2528
+/* 
8f2528
+ * The 'gssapi_keyex' userauth mechanism.
8f2528
+ */
8f2528
+static int
8f2528
+userauth_gsskeyex(Authctxt *authctxt)
8f2528
+{
8f2528
+	int authenticated = 0;
8f2528
+	Buffer b;
8f2528
+	gss_buffer_desc mic, gssbuf;
8f2528
+	u_int len;
8f2528
+
8f2528
+	mic.value = packet_get_string(&len;;
8f2528
+	mic.length = len;
8f2528
+
8f2528
+	packet_check_eom();
8f2528
+
8f2528
+	ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
8f2528
+	    "gssapi-keyex");
8f2528
+
8f2528
+	gssbuf.value = buffer_ptr(&b);
8f2528
+	gssbuf.length = buffer_len(&b);
8f2528
+
8f2528
+	/* gss_kex_context is NULL with privsep, so we can't check it here */
8f2528
+	if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context, 
8f2528
+	    &gssbuf, &mic))))
8f2528
+		authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
8f2528
+		    authctxt->pw));
8f2528
+	
8f2528
+	buffer_free(&b);
8f2528
+	free(mic.value);
8f2528
+
8f2528
+	return (authenticated);
8f2528
+}
8f2528
+
8f2528
 /*
8f2528
  * We only support those mechanisms that we know about (ie ones that we know
8f2528
  * how to check local user kuserok and the like)
8f2528
@@ -238,7 +273,8 @@ input_gssapi_exchange_complete(int type,
8f2528
 
8f2528
 	packet_check_eom();
8f2528
 
8f2528
-	authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
8f2528
+	authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
8f2528
+	    authctxt->pw));
8f2528
 
8f2528
 	authctxt->postponed = 0;
8f2528
 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
8f2528
@@ -281,7 +317,8 @@ input_gssapi_mic(int type, u_int32_t ple
8f2528
 	gssbuf.length = buffer_len(&b);
8f2528
 
8f2528
 	if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
8f2528
-		authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
8f2528
+		authenticated = 
8f2528
+		    PRIVSEP(ssh_gssapi_userok(authctxt->user, authctxt->pw));
8f2528
 	else
8f2528
 		logit("GSSAPI MIC check failed");
8f2528
 
8f2528
@@ -299,6 +336,12 @@ input_gssapi_mic(int type, u_int32_t ple
8f2528
 	return 0;
8f2528
 }
8f2528
 
8f2528
+Authmethod method_gsskeyex = {
8f2528
+	"gssapi-keyex",
8f2528
+	userauth_gsskeyex,
8f2528
+	&options.gss_authentication
8f2528
+};
8f2528
+
8f2528
 Authmethod method_gssapi = {
8f2528
 	"gssapi-with-mic",
8f2528
 	userauth_gssapi,
8f2528
diff -up openssh-7.4p1/clientloop.c.gsskex openssh-7.4p1/clientloop.c
8f2528
--- openssh-7.4p1/clientloop.c.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/clientloop.c	2016-12-23 13:38:53.725301005 +0100
8f2528
@@ -113,6 +113,10 @@
8f2528
 #include "ssherr.h"
8f2528
 #include "hostfile.h"
8f2528
 
8f2528
+#ifdef GSSAPI
8f2528
+#include "ssh-gss.h"
8f2528
+#endif
8f2528
+
8f2528
 /* import options */
8f2528
 extern Options options;
8f2528
 
8f2528
@@ -1664,9 +1668,18 @@ client_loop(int have_pty, int escape_cha
8f2528
 			break;
8f2528
 
8f2528
 		/* Do channel operations unless rekeying in progress. */
8f2528
-		if (!ssh_packet_is_rekeying(active_state))
8f2528
+		if (!ssh_packet_is_rekeying(active_state)) {
8f2528
 			channel_after_select(readset, writeset);
8f2528
 
8f2528
+#ifdef GSSAPI
8f2528
+			if (options.gss_renewal_rekey &&
8f2528
+			    ssh_gssapi_credentials_updated(NULL)) {
8f2528
+				debug("credentials updated - forcing rekey");
8f2528
+				need_rekeying = 1;
8f2528
+			}
8f2528
+#endif
8f2528
+		}
8f2528
+
8f2528
 		/* Buffer input from the connection.  */
8f2528
 		client_process_net_input(readset);
8f2528
 
8f2528
diff -up openssh-7.4p1/configure.ac.gsskex openssh-7.4p1/configure.ac
8f2528
--- openssh-7.4p1/configure.ac.gsskex	2016-12-23 13:38:53.716301003 +0100
8f2528
+++ openssh-7.4p1/configure.ac	2016-12-23 13:38:53.726301005 +0100
8f2528
@@ -623,6 +623,30 @@ main() { if (NSVersionOfRunTimeLibrary("
8f2528
 	    [Use tunnel device compatibility to OpenBSD])
8f2528
 	AC_DEFINE([SSH_TUN_PREPEND_AF], [1],
8f2528
 	    [Prepend the address family to IP tunnel traffic])
8f2528
+	AC_MSG_CHECKING(if we have the Security Authorization Session API)
8f2528
+	AC_TRY_COMPILE([#include <Security/AuthSession.h>],
8f2528
+		[SessionCreate(0, 0);],
8f2528
+		[ac_cv_use_security_session_api="yes"
8f2528
+		 AC_DEFINE(USE_SECURITY_SESSION_API, 1, 
8f2528
+			[platform has the Security Authorization Session API])
8f2528
+		 LIBS="$LIBS -framework Security"
8f2528
+		 AC_MSG_RESULT(yes)],
8f2528
+		[ac_cv_use_security_session_api="no"
8f2528
+		 AC_MSG_RESULT(no)])
8f2528
+	AC_MSG_CHECKING(if we have an in-memory credentials cache)
8f2528
+	AC_TRY_COMPILE(
8f2528
+		[#include <Kerberos/Kerberos.h>],
8f2528
+		[cc_context_t c;
8f2528
+		 (void) cc_initialize (&c, 0, NULL, NULL);],
8f2528
+		[AC_DEFINE(USE_CCAPI, 1, 
8f2528
+			[platform uses an in-memory credentials cache])
8f2528
+		 LIBS="$LIBS -framework Security"
8f2528
+		 AC_MSG_RESULT(yes)
8f2528
+		 if test "x$ac_cv_use_security_session_api" = "xno"; then
8f2528
+			AC_MSG_ERROR(*** Need a security framework to use the credentials cache API ***)
8f2528
+		fi],
8f2528
+		[AC_MSG_RESULT(no)]
8f2528
+	)
8f2528
 	m4_pattern_allow([AU_IPv])
8f2528
 	AC_CHECK_DECL([AU_IPv4], [],
8f2528
 	    AC_DEFINE([AU_IPv4], [0], [System only supports IPv4 audit records])
8f2528
diff -up openssh-7.4p1/gss-genr.c.gsskex openssh-7.4p1/gss-genr.c
8f2528
--- openssh-7.4p1/gss-genr.c.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/gss-genr.c	2016-12-23 13:38:53.726301005 +0100
8f2528
@@ -40,12 +40,167 @@
8f2528
 #include "buffer.h"
8f2528
 #include "log.h"
8f2528
 #include "ssh2.h"
8f2528
+#include "cipher.h"
8f2528
+#include "key.h"
8f2528
+#include "kex.h"
8f2528
+#include <openssl/evp.h>
8f2528
 
8f2528
 #include "ssh-gss.h"
8f2528
 
8f2528
 extern u_char *session_id2;
8f2528
 extern u_int session_id2_len;
8f2528
 
8f2528
+typedef struct {
8f2528
+	char *encoded;
8f2528
+	gss_OID oid;
8f2528
+} ssh_gss_kex_mapping;
8f2528
+
8f2528
+/*
8f2528
+ * XXX - It would be nice to find a more elegant way of handling the
8f2528
+ * XXX   passing of the key exchange context to the userauth routines
8f2528
+ */
8f2528
+
8f2528
+Gssctxt *gss_kex_context = NULL;
8f2528
+
8f2528
+static ssh_gss_kex_mapping *gss_enc2oid = NULL;
8f2528
+
8f2528
+int 
8f2528
+ssh_gssapi_oid_table_ok() {
8f2528
+	return (gss_enc2oid != NULL);
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
+ * Return a list of the gss-group1-sha1 mechanisms supported by this program
8f2528
+ *
8f2528
+ * We test mechanisms to ensure that we can use them, to avoid starting
8f2528
+ * a key exchange with a bad mechanism
8f2528
+ */
8f2528
+
8f2528
+char *
8f2528
+ssh_gssapi_client_mechanisms(const char *host, const char *client) {
8f2528
+	gss_OID_set gss_supported;
8f2528
+	OM_uint32 min_status;
8f2528
+
8f2528
+	if (GSS_ERROR(gss_indicate_mechs(&min_status, &gss_supported)))
8f2528
+		return NULL;
8f2528
+
8f2528
+	return(ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism,
8f2528
+	    host, client));
8f2528
+}
8f2528
+
8f2528
+char *
8f2528
+ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check,
8f2528
+    const char *host, const char *client) {
8f2528
+	Buffer buf;
8f2528
+	size_t i;
8f2528
+	int oidpos, enclen;
8f2528
+	char *mechs, *encoded;
8f2528
+	u_char digest[EVP_MAX_MD_SIZE];
8f2528
+	char deroid[2];
8f2528
+	const EVP_MD *evp_md = EVP_md5();
8f2528
+	EVP_MD_CTX md;
8f2528
+
8f2528
+	if (gss_enc2oid != NULL) {
8f2528
+		for (i = 0; gss_enc2oid[i].encoded != NULL; i++)
8f2528
+			free(gss_enc2oid[i].encoded);
8f2528
+		free(gss_enc2oid);
8f2528
+	}
8f2528
+
8f2528
+	gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping) *
8f2528
+	    (gss_supported->count + 1));
8f2528
+
8f2528
+	buffer_init(&buf;;
8f2528
+
8f2528
+	oidpos = 0;
8f2528
+	for (i = 0; i < gss_supported->count; i++) {
8f2528
+		if (gss_supported->elements[i].length < 128 &&
8f2528
+		    (*check)(NULL, &(gss_supported->elements[i]), host, client)) {
8f2528
+
8f2528
+			deroid[0] = SSH_GSS_OIDTYPE;
8f2528
+			deroid[1] = gss_supported->elements[i].length;
8f2528
+
8f2528
+			EVP_DigestInit(&md, evp_md);
8f2528
+			EVP_DigestUpdate(&md, deroid, 2);
8f2528
+			EVP_DigestUpdate(&md,
8f2528
+			    gss_supported->elements[i].elements,
8f2528
+			    gss_supported->elements[i].length);
8f2528
+			EVP_DigestFinal(&md, digest, NULL);
8f2528
+
8f2528
+			encoded = xmalloc(EVP_MD_size(evp_md) * 2);
8f2528
+			enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
8f2528
+			    encoded, EVP_MD_size(evp_md) * 2);
8f2528
+
8f2528
+			if (oidpos != 0)
8f2528
+				buffer_put_char(&buf, ',');
8f2528
+
8f2528
+			buffer_append(&buf, KEX_GSS_GEX_SHA1_ID,
8f2528
+			    sizeof(KEX_GSS_GEX_SHA1_ID) - 1);
8f2528
+			buffer_append(&buf, encoded, enclen);
8f2528
+			buffer_put_char(&buf, ',');
8f2528
+			buffer_append(&buf, KEX_GSS_GRP1_SHA1_ID, 
8f2528
+			    sizeof(KEX_GSS_GRP1_SHA1_ID) - 1);
8f2528
+			buffer_append(&buf, encoded, enclen);
8f2528
+			buffer_put_char(&buf, ',');
8f2528
+			buffer_append(&buf, KEX_GSS_GRP14_SHA1_ID,
8f2528
+			    sizeof(KEX_GSS_GRP14_SHA1_ID) - 1);
8f2528
+			buffer_append(&buf, encoded, enclen);
8f2528
+
8f2528
+			gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]);
8f2528
+			gss_enc2oid[oidpos].encoded = encoded;
8f2528
+			oidpos++;
8f2528
+		}
8f2528
+	}
8f2528
+	gss_enc2oid[oidpos].oid = NULL;
8f2528
+	gss_enc2oid[oidpos].encoded = NULL;
8f2528
+
8f2528
+	buffer_put_char(&buf, '\0');
8f2528
+
8f2528
+	mechs = xmalloc(buffer_len(&buf));
8f2528
+	buffer_get(&buf, mechs, buffer_len(&buf));
8f2528
+	buffer_free(&buf;;
8f2528
+
8f2528
+	if (strlen(mechs) == 0) {
8f2528
+		free(mechs);
8f2528
+		mechs = NULL;
8f2528
+	}
8f2528
+	
8f2528
+	return (mechs);
8f2528
+}
8f2528
+
8f2528
+gss_OID
8f2528
+ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int kex_type) {
8f2528
+	int i = 0;
8f2528
+	
8f2528
+	switch (kex_type) {
8f2528
+	case KEX_GSS_GRP1_SHA1:
8f2528
+		if (strlen(name) < sizeof(KEX_GSS_GRP1_SHA1_ID))
8f2528
+			return GSS_C_NO_OID;
8f2528
+		name += sizeof(KEX_GSS_GRP1_SHA1_ID) - 1;
8f2528
+		break;
8f2528
+	case KEX_GSS_GRP14_SHA1:
8f2528
+		if (strlen(name) < sizeof(KEX_GSS_GRP14_SHA1_ID))
8f2528
+			return GSS_C_NO_OID;
8f2528
+		name += sizeof(KEX_GSS_GRP14_SHA1_ID) - 1;
8f2528
+		break;
8f2528
+	case KEX_GSS_GEX_SHA1:
8f2528
+		if (strlen(name) < sizeof(KEX_GSS_GEX_SHA1_ID))
8f2528
+			return GSS_C_NO_OID;
8f2528
+		name += sizeof(KEX_GSS_GEX_SHA1_ID) - 1;
8f2528
+		break;
8f2528
+	default:
8f2528
+		return GSS_C_NO_OID;
8f2528
+	}
8f2528
+
8f2528
+	while (gss_enc2oid[i].encoded != NULL &&
8f2528
+	    strcmp(name, gss_enc2oid[i].encoded) != 0)
8f2528
+		i++;
8f2528
+
8f2528
+	if (gss_enc2oid[i].oid != NULL && ctx != NULL)
8f2528
+		ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid);
8f2528
+
8f2528
+	return gss_enc2oid[i].oid;
8f2528
+}
8f2528
+
8f2528
 /* Check that the OID in a data stream matches that in the context */
8f2528
 int
8f2528
 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
8f2528
@@ -198,7 +353,7 @@ ssh_gssapi_init_ctx(Gssctxt *ctx, int de
8f2528
 	}
8f2528
 
8f2528
 	ctx->major = gss_init_sec_context(&ctx->minor,
8f2528
-	    GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
8f2528
+	    ctx->client_creds, &ctx->context, ctx->name, ctx->oid,
8f2528
 	    GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
8f2528
 	    0, NULL, recv_tok, NULL, send_tok, flags, NULL);
8f2528
 
8f2528
@@ -228,8 +383,42 @@ ssh_gssapi_import_name(Gssctxt *ctx, con
8f2528
 }
8f2528
 
8f2528
 OM_uint32
8f2528
+ssh_gssapi_client_identity(Gssctxt *ctx, const char *name)
8f2528
+{
8f2528
+	gss_buffer_desc gssbuf;
8f2528
+	gss_name_t gssname;
8f2528
+	OM_uint32 status;
8f2528
+	gss_OID_set oidset;
8f2528
+
8f2528
+	gssbuf.value = (void *) name;
8f2528
+	gssbuf.length = strlen(gssbuf.value);
8f2528
+
8f2528
+	gss_create_empty_oid_set(&status, &oidset);
8f2528
+	gss_add_oid_set_member(&status, ctx->oid, &oidset);
8f2528
+
8f2528
+	ctx->major = gss_import_name(&ctx->minor, &gssbuf,
8f2528
+	    GSS_C_NT_USER_NAME, &gssname);
8f2528
+
8f2528
+	if (!ctx->major)
8f2528
+		ctx->major = gss_acquire_cred(&ctx->minor, 
8f2528
+		    gssname, 0, oidset, GSS_C_INITIATE, 
8f2528
+		    &ctx->client_creds, NULL, NULL);
8f2528
+
8f2528
+	gss_release_name(&status, &gssname);
8f2528
+	gss_release_oid_set(&status, &oidset);
8f2528
+
8f2528
+	if (ctx->major)
8f2528
+		ssh_gssapi_error(ctx);
8f2528
+
8f2528
+	return(ctx->major);
8f2528
+}
8f2528
+
8f2528
+OM_uint32
8f2528
 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
8f2528
 {
8f2528
+	if (ctx == NULL) 
8f2528
+		return -1;
8f2528
+
8f2528
 	if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
8f2528
 	    GSS_C_QOP_DEFAULT, buffer, hash)))
8f2528
 		ssh_gssapi_error(ctx);
8f2528
@@ -237,6 +426,19 @@ ssh_gssapi_sign(Gssctxt *ctx, gss_buffer
8f2528
 	return (ctx->major);
8f2528
 }
8f2528
 
8f2528
+/* Priviledged when used by server */
8f2528
+OM_uint32
8f2528
+ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
8f2528
+{
8f2528
+	if (ctx == NULL)
8f2528
+		return -1;
8f2528
+
8f2528
+	ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
8f2528
+	    gssbuf, gssmic, NULL);
8f2528
+
8f2528
+	return (ctx->major);
8f2528
+}
8f2528
+
8f2528
 void
8f2528
 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
8f2528
     const char *context)
8f2528
@@ -250,11 +452,16 @@ ssh_gssapi_buildmic(Buffer *b, const cha
8f2528
 }
8f2528
 
8f2528
 int
8f2528
-ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
8f2528
+ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host, 
8f2528
+    const char *client)
8f2528
 {
8f2528
 	gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
8f2528
 	OM_uint32 major, minor;
8f2528
 	gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
8f2528
+	Gssctxt *intctx = NULL;
8f2528
+
8f2528
+	if (ctx == NULL)
8f2528
+		ctx = &intct;;
8f2528
 
8f2528
 	/* RFC 4462 says we MUST NOT do SPNEGO */
8f2528
 	if (oid->length == spnego_oid.length && 
8f2528
@@ -264,6 +471,10 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx
8f2528
 	ssh_gssapi_build_ctx(ctx);
8f2528
 	ssh_gssapi_set_oid(*ctx, oid);
8f2528
 	major = ssh_gssapi_import_name(*ctx, host);
8f2528
+
8f2528
+	if (!GSS_ERROR(major) && client)
8f2528
+		major = ssh_gssapi_client_identity(*ctx, client);
8f2528
+
8f2528
 	if (!GSS_ERROR(major)) {
8f2528
 		major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, 
8f2528
 		    NULL);
8f2528
@@ -273,10 +484,66 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx
8f2528
 			    GSS_C_NO_BUFFER);
8f2528
 	}
8f2528
 
8f2528
-	if (GSS_ERROR(major)) 
8f2528
+	if (GSS_ERROR(major) || intctx != NULL) 
8f2528
 		ssh_gssapi_delete_ctx(ctx);
8f2528
 
8f2528
 	return (!GSS_ERROR(major));
8f2528
 }
8f2528
 
8f2528
+int
8f2528
+ssh_gssapi_credentials_updated(Gssctxt *ctxt) {
8f2528
+	static gss_name_t saved_name = GSS_C_NO_NAME;
8f2528
+	static OM_uint32 saved_lifetime = 0;
8f2528
+	static gss_OID saved_mech = GSS_C_NO_OID;
8f2528
+	static gss_name_t name;
8f2528
+	static OM_uint32 last_call = 0;
8f2528
+	OM_uint32 lifetime, now, major, minor;
8f2528
+	int equal;
8f2528
+	
8f2528
+	now = time(NULL);
8f2528
+
8f2528
+	if (ctxt) {
8f2528
+		debug("Rekey has happened - updating saved versions");
8f2528
+
8f2528
+		if (saved_name != GSS_C_NO_NAME)
8f2528
+			gss_release_name(&minor, &saved_name);
8f2528
+
8f2528
+		major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
8f2528
+		    &saved_name, &saved_lifetime, NULL, NULL);
8f2528
+
8f2528
+		if (!GSS_ERROR(major)) {
8f2528
+			saved_mech = ctxt->oid;
8f2528
+		        saved_lifetime+= now;
8f2528
+		} else {
8f2528
+			/* Handle the error */
8f2528
+		}
8f2528
+		return 0;
8f2528
+	}
8f2528
+
8f2528
+	if (now - last_call < 10)
8f2528
+		return 0;
8f2528
+
8f2528
+	last_call = now;
8f2528
+
8f2528
+	if (saved_mech == GSS_C_NO_OID)
8f2528
+		return 0;
8f2528
+	
8f2528
+	major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL, 
8f2528
+	    &name, &lifetime, NULL, NULL);
8f2528
+	if (major == GSS_S_CREDENTIALS_EXPIRED)
8f2528
+		return 0;
8f2528
+	else if (GSS_ERROR(major))
8f2528
+		return 0;
8f2528
+
8f2528
+	major = gss_compare_name(&minor, saved_name, name, &equal);
8f2528
+	gss_release_name(&minor, &name);
8f2528
+	if (GSS_ERROR(major))
8f2528
+		return 0;
8f2528
+
8f2528
+	if (equal && (saved_lifetime < lifetime + now - 10))
8f2528
+		return 1;
8f2528
+
8f2528
+	return 0;
8f2528
+}
8f2528
+
8f2528
 #endif /* GSSAPI */
8f2528
diff -up openssh-7.4p1/gss-serv.c.gsskex openssh-7.4p1/gss-serv.c
8f2528
--- openssh-7.4p1/gss-serv.c.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/gss-serv.c	2016-12-23 13:38:53.727301005 +0100
8f2528
@@ -45,17 +45,19 @@
8f2528
 #include "session.h"
8f2528
 #include "misc.h"
8f2528
 #include "servconf.h"
8f2528
+#include "uidswap.h"
8f2528
 
8f2528
 #include "ssh-gss.h"
8f2528
+#include "monitor_wrap.h"
8f2528
 
8f2528
 extern ServerOptions options;
8f2528
 
8f2528
 static ssh_gssapi_client gssapi_client =
8f2528
-    { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
8f2528
-    GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}};
8f2528
+    { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER, GSS_C_NO_CREDENTIAL,
8f2528
+    GSS_C_NO_NAME, NULL, {NULL, NULL, NULL, NULL, NULL}, 0, 0};
8f2528
 
8f2528
 ssh_gssapi_mech gssapi_null_mech =
8f2528
-    { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
8f2528
+    { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
8f2528
 
8f2528
 #ifdef KRB5
8f2528
 extern ssh_gssapi_mech gssapi_kerberos_mech;
8f2528
@@ -142,6 +144,28 @@ ssh_gssapi_server_ctx(Gssctxt **ctx, gss
8f2528
 }
8f2528
 
8f2528
 /* Unprivileged */
8f2528
+char *
8f2528
+ssh_gssapi_server_mechanisms() {
8f2528
+	if (supported_oids == NULL)
8f2528
+		ssh_gssapi_prepare_supported_oids();
8f2528
+	return (ssh_gssapi_kex_mechs(supported_oids,
8f2528
+	    &ssh_gssapi_server_check_mech, NULL, NULL));
8f2528
+}
8f2528
+
8f2528
+/* Unprivileged */
8f2528
+int
8f2528
+ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
8f2528
+    const char *dummy) {
8f2528
+	Gssctxt *ctx = NULL;
8f2528
+	int res;
8f2528
+ 
8f2528
+	res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
8f2528
+	ssh_gssapi_delete_ctx(&ctx;;
8f2528
+
8f2528
+	return (res);
8f2528
+}
8f2528
+
8f2528
+/* Unprivileged */
8f2528
 void
8f2528
 ssh_gssapi_supported_oids(gss_OID_set *oidset)
8f2528
 {
8f2528
@@ -151,7 +176,9 @@ ssh_gssapi_supported_oids(gss_OID_set *o
8f2528
 	gss_OID_set supported;
8f2528
 
8f2528
 	gss_create_empty_oid_set(&min_status, oidset);
8f2528
-	gss_indicate_mechs(&min_status, &supported);
8f2528
+
8f2528
+	if (GSS_ERROR(gss_indicate_mechs(&min_status, &supported)))
8f2528
+		return;
8f2528
 
8f2528
 	while (supported_mechs[i]->name != NULL) {
8f2528
 		if (GSS_ERROR(gss_test_oid_set_member(&min_status,
8f2528
@@ -277,8 +304,48 @@ OM_uint32
8f2528
 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
8f2528
 {
8f2528
 	int i = 0;
8f2528
+	int equal = 0;
8f2528
+	gss_name_t new_name = GSS_C_NO_NAME;
8f2528
+	gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
8f2528
+
8f2528
+	if (options.gss_store_rekey && client->used && ctx->client_creds) {
8f2528
+		if (client->mech->oid.length != ctx->oid->length ||
8f2528
+		    (memcmp(client->mech->oid.elements,
8f2528
+		     ctx->oid->elements, ctx->oid->length) !=0)) {
8f2528
+			debug("Rekeyed credentials have different mechanism");
8f2528
+			return GSS_S_COMPLETE;
8f2528
+		}
8f2528
+
8f2528
+		if ((ctx->major = gss_inquire_cred_by_mech(&ctx->minor, 
8f2528
+		    ctx->client_creds, ctx->oid, &new_name, 
8f2528
+		    NULL, NULL, NULL))) {
8f2528
+			ssh_gssapi_error(ctx);
8f2528
+			return (ctx->major);
8f2528
+		}
8f2528
 
8f2528
-	gss_buffer_desc ename;
8f2528
+		ctx->major = gss_compare_name(&ctx->minor, client->name, 
8f2528
+		    new_name, &equal);
8f2528
+
8f2528
+		if (GSS_ERROR(ctx->major)) {
8f2528
+			ssh_gssapi_error(ctx);
8f2528
+			return (ctx->major);
8f2528
+		}
8f2528
+ 
8f2528
+		if (!equal) {
8f2528
+			debug("Rekeyed credentials have different name");
8f2528
+			return GSS_S_COMPLETE;
8f2528
+		}
8f2528
+
8f2528
+		debug("Marking rekeyed credentials for export");
8f2528
+
8f2528
+		gss_release_name(&ctx->minor, &client->name);
8f2528
+		gss_release_cred(&ctx->minor, &client->creds);
8f2528
+		client->name = new_name;
8f2528
+		client->creds = ctx->client_creds;
8f2528
+        	ctx->client_creds = GSS_C_NO_CREDENTIAL;
8f2528
+		client->updated = 1;
8f2528
+		return GSS_S_COMPLETE;
8f2528
+	}
8f2528
 
8f2528
 	client->mech = NULL;
8f2528
 
8f2528
@@ -293,6 +360,13 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_g
8f2528
 	if (client->mech == NULL)
8f2528
 		return GSS_S_FAILURE;
8f2528
 
8f2528
+	if (ctx->client_creds &&
8f2528
+	    (ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
8f2528
+	     ctx->client_creds, ctx->oid, &client->name, NULL, NULL, NULL))) {
8f2528
+		ssh_gssapi_error(ctx);
8f2528
+		return (ctx->major);
8f2528
+	}
8f2528
+
8f2528
 	if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
8f2528
 	    &client->displayname, NULL))) {
8f2528
 		ssh_gssapi_error(ctx);
8f2528
@@ -310,6 +384,8 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_g
8f2528
 		return (ctx->major);
8f2528
 	}
8f2528
 
8f2528
+	gss_release_buffer(&ctx->minor, &ename);
8f2528
+
8f2528
 	/* We can't copy this structure, so we just move the pointer to it */
8f2528
 	client->creds = ctx->client_creds;
8f2528
 	ctx->client_creds = GSS_C_NO_CREDENTIAL;
8f2528
@@ -320,11 +396,20 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_g
8f2528
 void
8f2528
 ssh_gssapi_cleanup_creds(void)
8f2528
 {
8f2528
-	if (gssapi_client.store.filename != NULL) {
8f2528
-		/* Unlink probably isn't sufficient */
8f2528
-		debug("removing gssapi cred file\"%s\"",
8f2528
-		    gssapi_client.store.filename);
8f2528
-		unlink(gssapi_client.store.filename);
8f2528
+	krb5_ccache ccache = NULL;
8f2528
+	krb5_error_code problem;
8f2528
+
8f2528
+	if (gssapi_client.store.data != NULL) {
8f2528
+		if ((problem = krb5_cc_resolve(gssapi_client.store.data, gssapi_client.store.envval, &ccache))) {
8f2528
+			debug("%s: krb5_cc_resolve(): %.100s", __func__,
8f2528
+				krb5_get_err_text(gssapi_client.store.data, problem));
8f2528
+		} else if ((problem = krb5_cc_destroy(gssapi_client.store.data, ccache))) {
8f2528
+			debug("%s: krb5_cc_resolve(): %.100s", __func__,
8f2528
+				krb5_get_err_text(gssapi_client.store.data, problem));
8f2528
+		} else {
8f2528
+			krb5_free_context(gssapi_client.store.data);
8f2528
+			gssapi_client.store.data = NULL;
8f2528
+		}
8f2528
 	}
8f2528
 }
8f2528
 
8f2528
@@ -357,7 +442,7 @@ ssh_gssapi_do_child(char ***envp, u_int
8f2528
 
8f2528
 /* Privileged */
8f2528
 int
8f2528
-ssh_gssapi_userok(char *user)
8f2528
+ssh_gssapi_userok(char *user, struct passwd *pw)
8f2528
 {
8f2528
 	OM_uint32 lmin;
8f2528
 
8f2528
@@ -367,9 +452,11 @@ ssh_gssapi_userok(char *user)
8f2528
 		return 0;
8f2528
 	}
8f2528
 	if (gssapi_client.mech && gssapi_client.mech->userok)
8f2528
-		if ((*gssapi_client.mech->userok)(&gssapi_client, user))
8f2528
+		if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
8f2528
+			gssapi_client.used = 1;
8f2528
+			gssapi_client.store.owner = pw;
8f2528
 			return 1;
8f2528
-		else {
8f2528
+		} else {
8f2528
 			/* Destroy delegated credentials if userok fails */
8f2528
 			gss_release_buffer(&lmin, &gssapi_client.displayname);
8f2528
 			gss_release_buffer(&lmin, &gssapi_client.exportedname);
8f2528
@@ -383,14 +470,90 @@ ssh_gssapi_userok(char *user)
8f2528
 	return (0);
8f2528
 }
8f2528
 
8f2528
-/* Privileged */
8f2528
-OM_uint32
8f2528
-ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
8f2528
+/* These bits are only used for rekeying. The unpriviledged child is running 
8f2528
+ * as the user, the monitor is root.
8f2528
+ *
8f2528
+ * In the child, we want to :
8f2528
+ *    *) Ask the monitor to store our credentials into the store we specify
8f2528
+ *    *) If it succeeds, maybe do a PAM update
8f2528
+ */
8f2528
+
8f2528
+/* Stuff for PAM */
8f2528
+
8f2528
+#ifdef USE_PAM
8f2528
+static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg, 
8f2528
+    struct pam_response **resp, void *data)
8f2528
 {
8f2528
-	ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
8f2528
-	    gssbuf, gssmic, NULL);
8f2528
+	return (PAM_CONV_ERR);
8f2528
+}
8f2528
+#endif
8f2528
 
8f2528
-	return (ctx->major);
8f2528
+void
8f2528
+ssh_gssapi_rekey_creds() {
8f2528
+	int ok;
8f2528
+	int ret;
8f2528
+#ifdef USE_PAM
8f2528
+	pam_handle_t *pamh = NULL;
8f2528
+	struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
8f2528
+	char *envstr;
8f2528
+#endif
8f2528
+
8f2528
+	if (gssapi_client.store.filename == NULL && 
8f2528
+	    gssapi_client.store.envval == NULL &&
8f2528
+	    gssapi_client.store.envvar == NULL)
8f2528
+		return;
8f2528
+ 
8f2528
+	ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
8f2528
+
8f2528
+	if (!ok)
8f2528
+		return;
8f2528
+
8f2528
+	debug("Rekeyed credentials stored successfully");
8f2528
+
8f2528
+	/* Actually managing to play with the ssh pam stack from here will
8f2528
+	 * be next to impossible. In any case, we may want different options
8f2528
+	 * for rekeying. So, use our own :)
8f2528
+	 */
8f2528
+#ifdef USE_PAM	
8f2528
+	if (!use_privsep) {
8f2528
+		debug("Not even going to try and do PAM with privsep disabled");
8f2528
+		return;
8f2528
+	}
8f2528
+
8f2528
+	ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
8f2528
+ 	    &pamconv, &pamh);
8f2528
+	if (ret)
8f2528
+		return;
8f2528
+
8f2528
+	xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar, 
8f2528
+	    gssapi_client.store.envval);
8f2528
+
8f2528
+	ret = pam_putenv(pamh, envstr);
8f2528
+	if (!ret)
8f2528
+		pam_setcred(pamh, PAM_REINITIALIZE_CRED);
8f2528
+	pam_end(pamh, PAM_SUCCESS);
8f2528
+#endif
8f2528
+}
8f2528
+
8f2528
+int 
8f2528
+ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
8f2528
+	int ok = 0;
8f2528
+
8f2528
+	/* Check we've got credentials to store */
8f2528
+	if (!gssapi_client.updated)
8f2528
+		return 0;
8f2528
+
8f2528
+	gssapi_client.updated = 0;
8f2528
+
8f2528
+	temporarily_use_uid(gssapi_client.store.owner);
8f2528
+	if (gssapi_client.mech && gssapi_client.mech->updatecreds)
8f2528
+		ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
8f2528
+	else
8f2528
+		debug("No update function for this mechanism");
8f2528
+
8f2528
+	restore_uid();
8f2528
+
8f2528
+	return ok;
8f2528
 }
8f2528
 
8f2528
 #endif
8f2528
diff -up openssh-7.4p1/gss-serv-krb5.c.gsskex openssh-7.4p1/gss-serv-krb5.c
8f2528
--- openssh-7.4p1/gss-serv-krb5.c.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/gss-serv-krb5.c	2016-12-23 13:38:53.727301005 +0100
8f2528
@@ -121,7 +121,7 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
8f2528
 	krb5_error_code problem;
8f2528
 	krb5_principal princ;
8f2528
 	OM_uint32 maj_status, min_status;
8f2528
-	int len;
8f2528
+	const char *new_ccname, *new_cctype;
8f2528
 	const char *errmsg;
8f2528
 
8f2528
 	if (client->creds == NULL) {
8f2528
@@ -181,11 +181,26 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
8f2528
 		return;
8f2528
 	}
8f2528
 
8f2528
-	client->store.filename = xstrdup(krb5_cc_get_name(krb_context, ccache));
8f2528
+	new_cctype = krb5_cc_get_type(krb_context, ccache);
8f2528
+	new_ccname = krb5_cc_get_name(krb_context, ccache);
8f2528
+
8f2528
 	client->store.envvar = "KRB5CCNAME";
8f2528
-	len = strlen(client->store.filename) + 6;
8f2528
-	client->store.envval = xmalloc(len);
8f2528
-	snprintf(client->store.envval, len, "FILE:%s", client->store.filename);
8f2528
+#ifdef USE_CCAPI
8f2528
+	xasprintf(&client->store.envval, "API:%s", new_ccname);
8f2528
+	client->store.filename = NULL;
8f2528
+#else
8f2528
+	if (new_ccname[0] == ':')
8f2528
+		new_ccname++;
8f2528
+	xasprintf(&client->store.envval, "%s:%s", new_cctype, new_ccname);
8f2528
+	if (strcmp(new_cctype, "DIR") == 0) {
8f2528
+		char *p;
8f2528
+		p = strrchr(client->store.envval, '/');
8f2528
+		if (p)
8f2528
+			*p = '\0';
8f2528
+	}
8f2528
+	if ((strcmp(new_cctype, "FILE") == 0) || (strcmp(new_cctype, "DIR") == 0))
8f2528
+		client->store.filename = xstrdup(new_ccname);
8f2528
+#endif
8f2528
 
8f2528
 #ifdef USE_PAM
8f2528
 	if (options.use_pam)
8f2528
@@ -194,9 +209,76 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
8f2528
 
8f2528
 	krb5_cc_close(krb_context, ccache);
8f2528
 
8f2528
+	client->store.data = krb_context;
8f2528
+
8f2528
 	return;
8f2528
 }
8f2528
 
8f2528
+int
8f2528
+ssh_gssapi_krb5_updatecreds(ssh_gssapi_ccache *store, 
8f2528
+    ssh_gssapi_client *client)
8f2528
+{
8f2528
+	krb5_ccache ccache = NULL;
8f2528
+	krb5_principal principal = NULL;
8f2528
+	char *name = NULL;
8f2528
+	krb5_error_code problem;
8f2528
+	OM_uint32 maj_status, min_status;
8f2528
+
8f2528
+   	if ((problem = krb5_cc_resolve(krb_context, store->envval, &ccache))) {
8f2528
+                logit("krb5_cc_resolve(): %.100s",
8f2528
+                    krb5_get_err_text(krb_context, problem));
8f2528
+                return 0;
8f2528
+       	}
8f2528
+	
8f2528
+	/* Find out who the principal in this cache is */
8f2528
+	if ((problem = krb5_cc_get_principal(krb_context, ccache, 
8f2528
+	    &principal))) {
8f2528
+		logit("krb5_cc_get_principal(): %.100s",
8f2528
+		    krb5_get_err_text(krb_context, problem));
8f2528
+		krb5_cc_close(krb_context, ccache);
8f2528
+		return 0;
8f2528
+	}
8f2528
+
8f2528
+	if ((problem = krb5_unparse_name(krb_context, principal, &name))) {
8f2528
+		logit("krb5_unparse_name(): %.100s",
8f2528
+		    krb5_get_err_text(krb_context, problem));
8f2528
+		krb5_free_principal(krb_context, principal);
8f2528
+		krb5_cc_close(krb_context, ccache);
8f2528
+		return 0;
8f2528
+	}
8f2528
+
8f2528
+
8f2528
+	if (strcmp(name,client->exportedname.value)!=0) {
8f2528
+		debug("Name in local credentials cache differs. Not storing");
8f2528
+		krb5_free_principal(krb_context, principal);
8f2528
+		krb5_cc_close(krb_context, ccache);
8f2528
+		krb5_free_unparsed_name(krb_context, name);
8f2528
+		return 0;
8f2528
+	}
8f2528
+	krb5_free_unparsed_name(krb_context, name);
8f2528
+
8f2528
+	/* Name matches, so lets get on with it! */
8f2528
+
8f2528
+	if ((problem = krb5_cc_initialize(krb_context, ccache, principal))) {
8f2528
+		logit("krb5_cc_initialize(): %.100s",
8f2528
+		    krb5_get_err_text(krb_context, problem));
8f2528
+		krb5_free_principal(krb_context, principal);
8f2528
+		krb5_cc_close(krb_context, ccache);
8f2528
+		return 0;
8f2528
+	}
8f2528
+
8f2528
+	krb5_free_principal(krb_context, principal);
8f2528
+
8f2528
+	if ((maj_status = gss_krb5_copy_ccache(&min_status, client->creds,
8f2528
+	    ccache))) {
8f2528
+		logit("gss_krb5_copy_ccache() failed. Sorry!");
8f2528
+		krb5_cc_close(krb_context, ccache);
8f2528
+		return 0;
8f2528
+	}
8f2528
+
8f2528
+	return 1;
8f2528
+}
8f2528
+
8f2528
 ssh_gssapi_mech gssapi_kerberos_mech = {
8f2528
 	"toWM5Slw5Ew8Mqkay+al2g==",
8f2528
 	"Kerberos",
8f2528
@@ -204,7 +286,8 @@ ssh_gssapi_mech gssapi_kerberos_mech = {
8f2528
 	NULL,
8f2528
 	&ssh_gssapi_krb5_userok,
8f2528
 	NULL,
8f2528
-	&ssh_gssapi_krb5_storecreds
8f2528
+	&ssh_gssapi_krb5_storecreds,
8f2528
+	&ssh_gssapi_krb5_updatecreds
8f2528
 };
8f2528
 
8f2528
 #endif /* KRB5 */
8f2528
diff -up openssh-7.4p1/kex.c.gsskex openssh-7.4p1/kex.c
8f2528
--- openssh-7.4p1/kex.c.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/kex.c	2016-12-23 13:39:56.064313151 +0100
8f2528
@@ -54,6 +54,10 @@
8f2528
 #include "sshbuf.h"
8f2528
 #include "digest.h"
8f2528
 
8f2528
+#ifdef GSSAPI
8f2528
+#include "ssh-gss.h"
8f2528
+#endif
8f2528
+
8f2528
 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
8f2528
 # if defined(HAVE_EVP_SHA256)
8f2528
 # define evp_ssh_sha256 EVP_sha256
8f2528
@@ -111,6 +115,11 @@ static const struct kexalg kexalgs[] = {
8f2528
 	{ KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
8f2528
 	{ KEX_CURVE25519_SHA256_OLD, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
8f2528
 #endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */
8f2528
+#ifdef GSSAPI
8f2528
+	{ KEX_GSS_GEX_SHA1_ID, KEX_GSS_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
8f2528
+	{ KEX_GSS_GRP1_SHA1_ID, KEX_GSS_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
8f2528
+	{ KEX_GSS_GRP14_SHA1_ID, KEX_GSS_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
8f2528
+#endif
8f2528
 	{ NULL, -1, -1, -1},
8f2528
 };
8f2528
 
8f2528
@@ -144,6 +153,12 @@ kex_alg_by_name(const char *name)
8f2528
 	for (k = kexalgs; k->name != NULL; k++) {
8f2528
 		if (strcmp(k->name, name) == 0)
8f2528
 			return k;
8f2528
+#ifdef GSSAPI
8f2528
+		if (strncmp(name, "gss-", 4) == 0) {
8f2528
+			if (strncmp(k->name, name, strlen(k->name)) == 0)
8f2528
+				return k;
8f2528
+		}
8f2528
+#endif
8f2528
 	}
8f2528
 	return NULL;
8f2528
 }
8f2528
diff -up openssh-7.4p1/kexgssc.c.gsskex openssh-7.4p1/kexgssc.c
8f2528
--- openssh-7.4p1/kexgssc.c.gsskex	2016-12-23 13:38:53.727301005 +0100
8f2528
+++ openssh-7.4p1/kexgssc.c	2016-12-23 13:38:53.727301005 +0100
8f2528
@@ -0,0 +1,338 @@
8f2528
+/*
8f2528
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
8f2528
+ *
8f2528
+ * Redistribution and use in source and binary forms, with or without
8f2528
+ * modification, are permitted provided that the following conditions
8f2528
+ * are met:
8f2528
+ * 1. Redistributions of source code must retain the above copyright
8f2528
+ *    notice, this list of conditions and the following disclaimer.
8f2528
+ * 2. Redistributions in binary form must reproduce the above copyright
8f2528
+ *    notice, this list of conditions and the following disclaimer in the
8f2528
+ *    documentation and/or other materials provided with the distribution.
8f2528
+ *
8f2528
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
8f2528
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8f2528
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
8f2528
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
8f2528
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
8f2528
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8f2528
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8f2528
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8f2528
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
8f2528
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8f2528
+ */
8f2528
+
8f2528
+#include "includes.h"
8f2528
+
8f2528
+#ifdef GSSAPI
8f2528
+
8f2528
+#include "includes.h"
8f2528
+
8f2528
+#include <openssl/crypto.h>
8f2528
+#include <openssl/bn.h>
8f2528
+
8f2528
+#include <string.h>
8f2528
+
8f2528
+#include "xmalloc.h"
8f2528
+#include "buffer.h"
8f2528
+#include "ssh2.h"
8f2528
+#include "key.h"
8f2528
+#include "cipher.h"
8f2528
+#include "kex.h"
8f2528
+#include "log.h"
8f2528
+#include "packet.h"
8f2528
+#include "dh.h"
8f2528
+#include "digest.h"
8f2528
+
8f2528
+#include "ssh-gss.h"
8f2528
+
8f2528
+int
8f2528
+kexgss_client(struct ssh *ssh) {
8f2528
+	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
8f2528
+	gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
8f2528
+	Gssctxt *ctxt;
8f2528
+	OM_uint32 maj_status, min_status, ret_flags;
8f2528
+	u_int klen, kout, slen = 0, strlen;
8f2528
+	DH *dh; 
8f2528
+	BIGNUM *dh_server_pub = NULL;
8f2528
+	BIGNUM *shared_secret = NULL;
8f2528
+	BIGNUM *p = NULL;
8f2528
+	BIGNUM *g = NULL;	
8f2528
+	u_char *kbuf;
8f2528
+	u_char *serverhostkey = NULL;
8f2528
+	u_char *empty = "";
8f2528
+	char *msg;
8f2528
+	char *lang;
8f2528
+	int type = 0;
8f2528
+	int first = 1;
8f2528
+	int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
8f2528
+	u_char hash[SSH_DIGEST_MAX_LENGTH];
8f2528
+	size_t hashlen;
8f2528
+
8f2528
+	/* Initialise our GSSAPI world */	
8f2528
+	ssh_gssapi_build_ctx(&ctxt);
8f2528
+	if (ssh_gssapi_id_kex(ctxt, ssh->kex->name, ssh->kex->kex_type) 
8f2528
+	    == GSS_C_NO_OID)
8f2528
+		fatal("Couldn't identify host exchange");
8f2528
+
8f2528
+	if (ssh_gssapi_import_name(ctxt, ssh->kex->gss_host))
8f2528
+		fatal("Couldn't import hostname");
8f2528
+
8f2528
+	if (ssh->kex->gss_client && 
8f2528
+	    ssh_gssapi_client_identity(ctxt, ssh->kex->gss_client))
8f2528
+		fatal("Couldn't acquire client credentials");
8f2528
+
8f2528
+	switch (ssh->kex->kex_type) {
8f2528
+	case KEX_GSS_GRP1_SHA1:
8f2528
+		dh = dh_new_group1();
8f2528
+		break;
8f2528
+	case KEX_GSS_GRP14_SHA1:
8f2528
+		dh = dh_new_group14();
8f2528
+		break;
8f2528
+	case KEX_GSS_GEX_SHA1:
8f2528
+		debug("Doing group exchange\n");
8f2528
+		nbits = dh_estimate(ssh->kex->we_need * 8);
8f2528
+		packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
8f2528
+		packet_put_int(min);
8f2528
+		packet_put_int(nbits);
8f2528
+		packet_put_int(max);
8f2528
+
8f2528
+		packet_send();
8f2528
+
8f2528
+		packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
8f2528
+
8f2528
+		if ((p = BN_new()) == NULL)
8f2528
+			fatal("BN_new() failed");
8f2528
+		packet_get_bignum2(p);
8f2528
+		if ((g = BN_new()) == NULL)
8f2528
+			fatal("BN_new() failed");
8f2528
+		packet_get_bignum2(g);
8f2528
+		packet_check_eom();
8f2528
+
8f2528
+		if (BN_num_bits(p) < min || BN_num_bits(p) > max)
8f2528
+			fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
8f2528
+			    min, BN_num_bits(p), max);
8f2528
+
8f2528
+		dh = dh_new_group(g, p);
8f2528
+		break;
8f2528
+	default:
8f2528
+		fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
8f2528
+	}
8f2528
+	
8f2528
+	/* Step 1 - e is dh->pub_key */
8f2528
+	dh_gen_key(dh, ssh->kex->we_need * 8);
8f2528
+
8f2528
+	/* This is f, we initialise it now to make life easier */
8f2528
+	dh_server_pub = BN_new();
8f2528
+	if (dh_server_pub == NULL)
8f2528
+		fatal("dh_server_pub == NULL");
8f2528
+
8f2528
+	token_ptr = GSS_C_NO_BUFFER;
8f2528
+			 
8f2528
+	do {
8f2528
+		debug("Calling gss_init_sec_context");
8f2528
+		
8f2528
+		maj_status = ssh_gssapi_init_ctx(ctxt,
8f2528
+		    ssh->kex->gss_deleg_creds, token_ptr, &send_tok,
8f2528
+		    &ret_flags);
8f2528
+
8f2528
+		if (GSS_ERROR(maj_status)) {
8f2528
+			if (send_tok.length != 0) {
8f2528
+				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
8f2528
+				packet_put_string(send_tok.value,
8f2528
+				    send_tok.length);
8f2528
+			}
8f2528
+			fatal("gss_init_context failed");
8f2528
+		}
8f2528
+
8f2528
+		/* If we've got an old receive buffer get rid of it */
8f2528
+		if (token_ptr != GSS_C_NO_BUFFER)
8f2528
+			free(recv_tok.value);
8f2528
+
8f2528
+		if (maj_status == GSS_S_COMPLETE) {
8f2528
+			/* If mutual state flag is not true, kex fails */
8f2528
+			if (!(ret_flags & GSS_C_MUTUAL_FLAG))
8f2528
+				fatal("Mutual authentication failed");
8f2528
+
8f2528
+			/* If integ avail flag is not true kex fails */
8f2528
+			if (!(ret_flags & GSS_C_INTEG_FLAG))
8f2528
+				fatal("Integrity check failed");
8f2528
+		}
8f2528
+
8f2528
+		/* 
8f2528
+		 * If we have data to send, then the last message that we
8f2528
+		 * received cannot have been a 'complete'. 
8f2528
+		 */
8f2528
+		if (send_tok.length != 0) {
8f2528
+			if (first) {
8f2528
+				packet_start(SSH2_MSG_KEXGSS_INIT);
8f2528
+				packet_put_string(send_tok.value,
8f2528
+				    send_tok.length);
8f2528
+				packet_put_bignum2(dh->pub_key);
8f2528
+				first = 0;
8f2528
+			} else {
8f2528
+				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
8f2528
+				packet_put_string(send_tok.value,
8f2528
+				    send_tok.length);
8f2528
+			}
8f2528
+			packet_send();
8f2528
+			gss_release_buffer(&min_status, &send_tok);
8f2528
+
8f2528
+			/* If we've sent them data, they should reply */
8f2528
+			do {	
8f2528
+				type = packet_read();
8f2528
+				if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
8f2528
+					debug("Received KEXGSS_HOSTKEY");
8f2528
+					if (serverhostkey)
8f2528
+						fatal("Server host key received more than once");
8f2528
+					serverhostkey = 
8f2528
+					    packet_get_string(&slen);
8f2528
+				}
8f2528
+			} while (type == SSH2_MSG_KEXGSS_HOSTKEY);
8f2528
+
8f2528
+			switch (type) {
8f2528
+			case SSH2_MSG_KEXGSS_CONTINUE:
8f2528
+				debug("Received GSSAPI_CONTINUE");
8f2528
+				if (maj_status == GSS_S_COMPLETE) 
8f2528
+					fatal("GSSAPI Continue received from server when complete");
8f2528
+				recv_tok.value = packet_get_string(&strlen);
8f2528
+				recv_tok.length = strlen; 
8f2528
+				break;
8f2528
+			case SSH2_MSG_KEXGSS_COMPLETE:
8f2528
+				debug("Received GSSAPI_COMPLETE");
8f2528
+				packet_get_bignum2(dh_server_pub);
8f2528
+				msg_tok.value =  packet_get_string(&strlen);
8f2528
+				msg_tok.length = strlen; 
8f2528
+
8f2528
+				/* Is there a token included? */
8f2528
+				if (packet_get_char()) {
8f2528
+					recv_tok.value=
8f2528
+					    packet_get_string(&strlen);
8f2528
+					recv_tok.length = strlen;
8f2528
+					/* If we're already complete - protocol error */
8f2528
+					if (maj_status == GSS_S_COMPLETE)
8f2528
+						packet_disconnect("Protocol error: received token when complete");
8f2528
+					} else {
8f2528
+						/* No token included */
8f2528
+						if (maj_status != GSS_S_COMPLETE)
8f2528
+							packet_disconnect("Protocol error: did not receive final token");
8f2528
+				}
8f2528
+				break;
8f2528
+			case SSH2_MSG_KEXGSS_ERROR:
8f2528
+				debug("Received Error");
8f2528
+				maj_status = packet_get_int();
8f2528
+				min_status = packet_get_int();
8f2528
+				msg = packet_get_string(NULL);
8f2528
+				lang = packet_get_string(NULL);
8f2528
+				fatal("GSSAPI Error: \n%.400s",msg);
8f2528
+			default:
8f2528
+				packet_disconnect("Protocol error: didn't expect packet type %d",
8f2528
+		    		type);
8f2528
+			}
8f2528
+			token_ptr = &recv_tok;
8f2528
+		} else {
8f2528
+			/* No data, and not complete */
8f2528
+			if (maj_status != GSS_S_COMPLETE)
8f2528
+				fatal("Not complete, and no token output");
8f2528
+		}
8f2528
+	} while (maj_status & GSS_S_CONTINUE_NEEDED);
8f2528
+
8f2528
+	/* 
8f2528
+	 * We _must_ have received a COMPLETE message in reply from the 
8f2528
+	 * server, which will have set dh_server_pub and msg_tok 
8f2528
+	 */
8f2528
+
8f2528
+	if (type != SSH2_MSG_KEXGSS_COMPLETE)
8f2528
+		fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
8f2528
+
8f2528
+	/* Check f in range [1, p-1] */
8f2528
+	if (!dh_pub_is_valid(dh, dh_server_pub))
8f2528
+		packet_disconnect("bad server public DH value");
8f2528
+
8f2528
+	/* compute K=f^x mod p */
8f2528
+	klen = DH_size(dh);
8f2528
+	kbuf = xmalloc(klen);
8f2528
+	kout = DH_compute_key(kbuf, dh_server_pub, dh);
8f2528
+	if ((int)kout < 0)
8f2528
+		fatal("DH_compute_key: failed");
8f2528
+
8f2528
+	shared_secret = BN_new();
8f2528
+	if (shared_secret == NULL)
8f2528
+		fatal("kexgss_client: BN_new failed");
8f2528
+
8f2528
+	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
8f2528
+		fatal("kexdh_client: BN_bin2bn failed");
8f2528
+
8f2528
+	memset(kbuf, 0, klen);
8f2528
+	free(kbuf);
8f2528
+
8f2528
+	hashlen = sizeof(hash);
8f2528
+	switch (ssh->kex->kex_type) {
8f2528
+	case KEX_GSS_GRP1_SHA1:
8f2528
+	case KEX_GSS_GRP14_SHA1:
8f2528
+		kex_dh_hash(ssh->kex->hash_alg, ssh->kex->client_version_string, 
8f2528
+		    ssh->kex->server_version_string,
8f2528
+		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
8f2528
+		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
8f2528
+		    (serverhostkey ? serverhostkey : empty), slen,
8f2528
+		    dh->pub_key,	/* e */
8f2528
+		    dh_server_pub,	/* f */
8f2528
+		    shared_secret,	/* K */
8f2528
+		    hash, &hashlen
8f2528
+		);
8f2528
+		break;
8f2528
+	case KEX_GSS_GEX_SHA1:
8f2528
+		kexgex_hash(
8f2528
+		    ssh->kex->hash_alg,
8f2528
+		    ssh->kex->client_version_string,
8f2528
+		    ssh->kex->server_version_string,
8f2528
+		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
8f2528
+		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
8f2528
+		    (serverhostkey ? serverhostkey : empty), slen,
8f2528
+ 		    min, nbits, max,
8f2528
+		    dh->p, dh->g,
8f2528
+		    dh->pub_key,
8f2528
+		    dh_server_pub,
8f2528
+		    shared_secret,
8f2528
+		    hash, &hashlen
8f2528
+		);
8f2528
+		break;
8f2528
+	default:
8f2528
+		fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
8f2528
+	}
8f2528
+
8f2528
+	gssbuf.value = hash;
8f2528
+	gssbuf.length = hashlen;
8f2528
+
8f2528
+	/* Verify that the hash matches the MIC we just got. */
8f2528
+	if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
8f2528
+		packet_disconnect("Hash's MIC didn't verify");
8f2528
+
8f2528
+	free(msg_tok.value);
8f2528
+
8f2528
+	DH_free(dh);
8f2528
+	if (serverhostkey)
8f2528
+		free(serverhostkey);
8f2528
+	BN_clear_free(dh_server_pub);
8f2528
+
8f2528
+	/* save session id */
8f2528
+	if (ssh->kex->session_id == NULL) {
8f2528
+		ssh->kex->session_id_len = hashlen;
8f2528
+		ssh->kex->session_id = xmalloc(ssh->kex->session_id_len);
8f2528
+		memcpy(ssh->kex->session_id, hash, ssh->kex->session_id_len);
8f2528
+	}
8f2528
+
8f2528
+	if (ssh->kex->gss_deleg_creds)
8f2528
+		ssh_gssapi_credentials_updated(ctxt);
8f2528
+
8f2528
+	if (gss_kex_context == NULL)
8f2528
+		gss_kex_context = ctxt;
8f2528
+	else
8f2528
+		ssh_gssapi_delete_ctx(&ctxt);
8f2528
+
8f2528
+	kex_derive_keys_bn(ssh, hash, hashlen, shared_secret);
8f2528
+	BN_clear_free(shared_secret);
8f2528
+	return kex_send_newkeys(ssh);
8f2528
+}
8f2528
+
8f2528
+#endif /* GSSAPI */
8f2528
diff -up openssh-7.4p1/kexgsss.c.gsskex openssh-7.4p1/kexgsss.c
8f2528
--- openssh-7.4p1/kexgsss.c.gsskex	2016-12-23 13:38:53.728301005 +0100
8f2528
+++ openssh-7.4p1/kexgsss.c	2016-12-23 13:38:53.728301005 +0100
8f2528
@@ -0,0 +1,297 @@
8f2528
+/*
8f2528
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
8f2528
+ *
8f2528
+ * Redistribution and use in source and binary forms, with or without
8f2528
+ * modification, are permitted provided that the following conditions
8f2528
+ * are met:
8f2528
+ * 1. Redistributions of source code must retain the above copyright
8f2528
+ *    notice, this list of conditions and the following disclaimer.
8f2528
+ * 2. Redistributions in binary form must reproduce the above copyright
8f2528
+ *    notice, this list of conditions and the following disclaimer in the
8f2528
+ *    documentation and/or other materials provided with the distribution.
8f2528
+ *
8f2528
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
8f2528
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8f2528
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
8f2528
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
8f2528
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
8f2528
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8f2528
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8f2528
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8f2528
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
8f2528
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8f2528
+ */
8f2528
+
8f2528
+#include "includes.h"
8f2528
+
8f2528
+#ifdef GSSAPI
8f2528
+
8f2528
+#include <string.h>
8f2528
+
8f2528
+#include <openssl/crypto.h>
8f2528
+#include <openssl/bn.h>
8f2528
+
8f2528
+#include "xmalloc.h"
8f2528
+#include "buffer.h"
8f2528
+#include "ssh2.h"
8f2528
+#include "key.h"
8f2528
+#include "cipher.h"
8f2528
+#include "kex.h"
8f2528
+#include "log.h"
8f2528
+#include "packet.h"
8f2528
+#include "dh.h"
8f2528
+#include "ssh-gss.h"
8f2528
+#include "monitor_wrap.h"
8f2528
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
8f2528
+#include "servconf.h"
8f2528
+#include "ssh-gss.h"
8f2528
+#include "digest.h"
8f2528
+
8f2528
+extern ServerOptions options;
8f2528
+
8f2528
+int
8f2528
+kexgss_server(struct ssh *ssh)
8f2528
+{
8f2528
+	OM_uint32 maj_status, min_status;
8f2528
+	
8f2528
+	/* 
8f2528
+	 * Some GSSAPI implementations use the input value of ret_flags (an
8f2528
+ 	 * output variable) as a means of triggering mechanism specific 
8f2528
+ 	 * features. Initializing it to zero avoids inadvertently 
8f2528
+ 	 * activating this non-standard behaviour.
8f2528
+	 */
8f2528
+
8f2528
+	OM_uint32 ret_flags = 0;
8f2528
+	gss_buffer_desc gssbuf, recv_tok, msg_tok;
8f2528
+	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
8f2528
+	Gssctxt *ctxt = NULL;
8f2528
+	u_int slen, klen, kout;
8f2528
+	u_char *kbuf;
8f2528
+	DH *dh;
8f2528
+	int min = -1, max = -1, nbits = -1;
8f2528
+	int cmin = -1, cmax = -1; /* client proposal */
8f2528
+	BIGNUM *shared_secret = NULL;
8f2528
+	BIGNUM *dh_client_pub = NULL;
8f2528
+	int type = 0;
8f2528
+	gss_OID oid;
8f2528
+	char *mechs;
8f2528
+	u_char hash[SSH_DIGEST_MAX_LENGTH];
8f2528
+	size_t hashlen;
8f2528
+
8f2528
+	/* Initialise GSSAPI */
8f2528
+
8f2528
+	/* If we're rekeying, privsep means that some of the private structures
8f2528
+	 * in the GSSAPI code are no longer available. This kludges them back
8f2528
+	 * into life
8f2528
+	 */
8f2528
+	if (!ssh_gssapi_oid_table_ok()) 
8f2528
+		if ((mechs = ssh_gssapi_server_mechanisms()))
8f2528
+			free(mechs);
8f2528
+
8f2528
+	debug2("%s: Identifying %s", __func__, ssh->kex->name);
8f2528
+	oid = ssh_gssapi_id_kex(NULL, ssh->kex->name, ssh->kex->kex_type);
8f2528
+	if (oid == GSS_C_NO_OID)
8f2528
+	   fatal("Unknown gssapi mechanism");
8f2528
+
8f2528
+	debug2("%s: Acquiring credentials", __func__);
8f2528
+
8f2528
+	if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
8f2528
+		fatal("Unable to acquire credentials for the server");
8f2528
+
8f2528
+	switch (ssh->kex->kex_type) {
8f2528
+	case KEX_GSS_GRP1_SHA1:
8f2528
+		dh = dh_new_group1();
8f2528
+		break;
8f2528
+	case KEX_GSS_GRP14_SHA1:
8f2528
+		dh = dh_new_group14();
8f2528
+		break;
8f2528
+	case KEX_GSS_GEX_SHA1:
8f2528
+		debug("Doing group exchange");
8f2528
+		packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ);
8f2528
+		/* store client proposal to provide valid signature */
8f2528
+		cmin = packet_get_int();
8f2528
+		nbits = packet_get_int();
8f2528
+		cmax = packet_get_int();
8f2528
+		min = MAX(DH_GRP_MIN, cmin);
8f2528
+		max = MIN(DH_GRP_MAX, cmax);
8f2528
+		packet_check_eom();
8f2528
+		if (max < min || nbits < min || max < nbits)
8f2528
+			fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
8f2528
+			    min, nbits, max);
8f2528
+		dh = PRIVSEP(choose_dh(min, nbits, max));
8f2528
+		if (dh == NULL)
8f2528
+			packet_disconnect("Protocol error: no matching group found");
8f2528
+
8f2528
+		packet_start(SSH2_MSG_KEXGSS_GROUP);
8f2528
+		packet_put_bignum2(dh->p);
8f2528
+		packet_put_bignum2(dh->g);
8f2528
+		packet_send();
8f2528
+
8f2528
+		packet_write_wait();
8f2528
+		break;
8f2528
+	default:
8f2528
+		fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
8f2528
+	}
8f2528
+
8f2528
+	dh_gen_key(dh, ssh->kex->we_need * 8);
8f2528
+
8f2528
+	do {
8f2528
+		debug("Wait SSH2_MSG_GSSAPI_INIT");
8f2528
+		type = packet_read();
8f2528
+		switch(type) {
8f2528
+		case SSH2_MSG_KEXGSS_INIT:
8f2528
+			if (dh_client_pub != NULL) 
8f2528
+				fatal("Received KEXGSS_INIT after initialising");
8f2528
+			recv_tok.value = packet_get_string(&slen);
8f2528
+			recv_tok.length = slen; 
8f2528
+
8f2528
+			if ((dh_client_pub = BN_new()) == NULL)
8f2528
+				fatal("dh_client_pub == NULL");
8f2528
+
8f2528
+			packet_get_bignum2(dh_client_pub);
8f2528
+
8f2528
+			/* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
8f2528
+			break;
8f2528
+		case SSH2_MSG_KEXGSS_CONTINUE:
8f2528
+			recv_tok.value = packet_get_string(&slen);
8f2528
+			recv_tok.length = slen; 
8f2528
+			break;
8f2528
+		default:
8f2528
+			packet_disconnect(
8f2528
+			    "Protocol error: didn't expect packet type %d",
8f2528
+			    type);
8f2528
+		}
8f2528
+
8f2528
+		maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, 
8f2528
+		    &send_tok, &ret_flags));
8f2528
+
8f2528
+		free(recv_tok.value);
8f2528
+
8f2528
+		if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
8f2528
+			fatal("Zero length token output when incomplete");
8f2528
+
8f2528
+		if (dh_client_pub == NULL)
8f2528
+			fatal("No client public key");
8f2528
+		
8f2528
+		if (maj_status & GSS_S_CONTINUE_NEEDED) {
8f2528
+			debug("Sending GSSAPI_CONTINUE");
8f2528
+			packet_start(SSH2_MSG_KEXGSS_CONTINUE);
8f2528
+			packet_put_string(send_tok.value, send_tok.length);
8f2528
+			packet_send();
8f2528
+			gss_release_buffer(&min_status, &send_tok);
8f2528
+		}
8f2528
+	} while (maj_status & GSS_S_CONTINUE_NEEDED);
8f2528
+
8f2528
+	if (GSS_ERROR(maj_status)) {
8f2528
+		if (send_tok.length > 0) {
8f2528
+			packet_start(SSH2_MSG_KEXGSS_CONTINUE);
8f2528
+			packet_put_string(send_tok.value, send_tok.length);
8f2528
+			packet_send();
8f2528
+		}
8f2528
+		fatal("accept_ctx died");
8f2528
+	}
8f2528
+
8f2528
+	if (!(ret_flags & GSS_C_MUTUAL_FLAG))
8f2528
+		fatal("Mutual Authentication flag wasn't set");
8f2528
+
8f2528
+	if (!(ret_flags & GSS_C_INTEG_FLAG))
8f2528
+		fatal("Integrity flag wasn't set");
8f2528
+	
8f2528
+	if (!dh_pub_is_valid(dh, dh_client_pub))
8f2528
+		packet_disconnect("bad client public DH value");
8f2528
+
8f2528
+	klen = DH_size(dh);
8f2528
+	kbuf = xmalloc(klen); 
8f2528
+	kout = DH_compute_key(kbuf, dh_client_pub, dh);
8f2528
+	if ((int)kout < 0)
8f2528
+		fatal("DH_compute_key: failed");
8f2528
+
8f2528
+	shared_secret = BN_new();
8f2528
+	if (shared_secret == NULL)
8f2528
+		fatal("kexgss_server: BN_new failed");
8f2528
+
8f2528
+	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
8f2528
+		fatal("kexgss_server: BN_bin2bn failed");
8f2528
+
8f2528
+	memset(kbuf, 0, klen);
8f2528
+	free(kbuf);
8f2528
+
8f2528
+	hashlen = sizeof(hash);
8f2528
+	switch (ssh->kex->kex_type) {
8f2528
+	case KEX_GSS_GRP1_SHA1:
8f2528
+	case KEX_GSS_GRP14_SHA1:
8f2528
+		kex_dh_hash(ssh->kex->hash_alg,
8f2528
+		    ssh->kex->client_version_string, ssh->kex->server_version_string,
8f2528
+		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
8f2528
+		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
8f2528
+		    NULL, 0, /* Change this if we start sending host keys */
8f2528
+		    dh_client_pub, dh->pub_key, shared_secret,
8f2528
+		    hash, &hashlen
8f2528
+		);
8f2528
+		break;
8f2528
+	case KEX_GSS_GEX_SHA1:
8f2528
+		kexgex_hash(
8f2528
+		    ssh->kex->hash_alg,
8f2528
+		    ssh->kex->client_version_string, ssh->kex->server_version_string,
8f2528
+		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
8f2528
+		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
8f2528
+		    NULL, 0,
8f2528
+		    cmin, nbits, cmax,
8f2528
+		    dh->p, dh->g,
8f2528
+		    dh_client_pub,
8f2528
+		    dh->pub_key,
8f2528
+		    shared_secret,
8f2528
+		    hash, &hashlen
8f2528
+		);
8f2528
+		break;
8f2528
+	default:
8f2528
+		fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
8f2528
+	}
8f2528
+
8f2528
+	BN_clear_free(dh_client_pub);
8f2528
+
8f2528
+	if (ssh->kex->session_id == NULL) {
8f2528
+		ssh->kex->session_id_len = hashlen;
8f2528
+		ssh->kex->session_id = xmalloc(ssh->kex->session_id_len);
8f2528
+		memcpy(ssh->kex->session_id, hash, ssh->kex->session_id_len);
8f2528
+	}
8f2528
+
8f2528
+	gssbuf.value = hash;
8f2528
+	gssbuf.length = hashlen;
8f2528
+
8f2528
+	if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
8f2528
+		fatal("Couldn't get MIC");
8f2528
+
8f2528
+	packet_start(SSH2_MSG_KEXGSS_COMPLETE);
8f2528
+	packet_put_bignum2(dh->pub_key);
8f2528
+	packet_put_string(msg_tok.value,msg_tok.length);
8f2528
+
8f2528
+	if (send_tok.length != 0) {
8f2528
+		packet_put_char(1); /* true */
8f2528
+		packet_put_string(send_tok.value, send_tok.length);
8f2528
+	} else {
8f2528
+		packet_put_char(0); /* false */
8f2528
+	}
8f2528
+	packet_send();
8f2528
+
8f2528
+	gss_release_buffer(&min_status, &send_tok);
8f2528
+	gss_release_buffer(&min_status, &msg_tok);
8f2528
+
8f2528
+	if (gss_kex_context == NULL)
8f2528
+		gss_kex_context = ctxt;
8f2528
+	else 
8f2528
+		ssh_gssapi_delete_ctx(&ctxt);
8f2528
+
8f2528
+	DH_free(dh);
8f2528
+
8f2528
+	kex_derive_keys_bn(ssh, hash, hashlen, shared_secret);
8f2528
+	BN_clear_free(shared_secret);
8f2528
+	kex_send_newkeys(ssh);
8f2528
+
8f2528
+	/* If this was a rekey, then save out any delegated credentials we
8f2528
+	 * just exchanged.  */
8f2528
+	if (options.gss_store_rekey)
8f2528
+		ssh_gssapi_rekey_creds();
8f2528
+	return 0;
8f2528
+}
8f2528
+#endif /* GSSAPI */
8f2528
diff -up openssh-7.4p1/kex.h.gsskex openssh-7.4p1/kex.h
8f2528
--- openssh-7.4p1/kex.h.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/kex.h	2016-12-23 13:38:53.728301005 +0100
8f2528
@@ -99,6 +99,11 @@ enum kex_exchange {
8f2528
 	KEX_DH_GEX_SHA256,
8f2528
 	KEX_ECDH_SHA2,
8f2528
 	KEX_C25519_SHA256,
8f2528
+#ifdef GSSAPI
8f2528
+	KEX_GSS_GRP1_SHA1,
8f2528
+	KEX_GSS_GRP14_SHA1,
8f2528
+	KEX_GSS_GEX_SHA1,
8f2528
+#endif
8f2528
 	KEX_MAX
8f2528
 };
8f2528
 
8f2528
@@ -147,6 +152,12 @@ struct kex {
8f2528
 	u_int	flags;
8f2528
 	int	hash_alg;
8f2528
 	int	ec_nid;
8f2528
+#ifdef GSSAPI
8f2528
+	int	gss_deleg_creds;
8f2528
+	int	gss_trust_dns;
8f2528
+	char    *gss_host;
8f2528
+	char	*gss_client;
8f2528
+#endif
8f2528
 	char	*client_version_string;
8f2528
 	char	*server_version_string;
8f2528
 	char	*failed_choice;
8f2528
@@ -196,6 +207,10 @@ int	 kexecdh_client(struct ssh *);
8f2528
 int	 kexecdh_server(struct ssh *);
8f2528
 int	 kexc25519_client(struct ssh *);
8f2528
 int	 kexc25519_server(struct ssh *);
8f2528
+#ifdef GSSAPI
8f2528
+int	 kexgss_client(struct ssh *);
8f2528
+int	 kexgss_server(struct ssh *);
8f2528
+#endif
8f2528
 
8f2528
 int	 kex_dh_hash(int, const char *, const char *,
8f2528
     const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
8f2528
diff -up openssh-7.4p1/Makefile.in.gsskex openssh-7.4p1/Makefile.in
8f2528
--- openssh-7.4p1/Makefile.in.gsskex	2016-12-23 13:38:53.723301004 +0100
8f2528
+++ openssh-7.4p1/Makefile.in	2016-12-23 13:40:32.226320197 +0100
8f2528
@@ -91,6 +91,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
8f2528
 	readpass.o rsa.o ttymodes.o xmalloc.o addrmatch.o \
8f2528
 	atomicio.o key.o dispatch.o mac.o uidswap.o uuencode.o misc.o utf8.o \
8f2528
 	monitor_fdpass.o rijndael.o ssh-dss.o ssh-ecdsa.o ssh-rsa.o dh.o \
8f2528
+	kexgssc.o \
8f2528
 	msg.o progressmeter.o dns.o entropy.o gss-genr.o umac.o umac128.o \
8f2528
 	ssh-pkcs11.o smult_curve25519_ref.o \
8f2528
 	poly1305.o chacha.o cipher-chachapoly.o \
8f2528
@@ -112,7 +113,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passw
8f2528
 	auth-skey.o auth-bsdauth.o auth2-hostbased.o auth2-kbdint.o \
8f2528
 	auth2-none.o auth2-passwd.o auth2-pubkey.o \
8f2528
 	monitor.o monitor_wrap.o auth-krb5.o \
8f2528
-	auth2-gss.o gss-serv.o gss-serv-krb5.o \
8f2528
+	auth2-gss.o gss-serv.o gss-serv-krb5.o kexgsss.o \
8f2528
 	loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \
8f2528
 	sftp-server.o sftp-common.o \
8f2528
 	sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \
8f2528
diff -up openssh-7.4p1/monitor.c.gsskex openssh-7.4p1/monitor.c
8f2528
--- openssh-7.4p1/monitor.c.gsskex	2016-12-23 13:38:53.687300997 +0100
8f2528
+++ openssh-7.4p1/monitor.c	2016-12-23 13:45:49.347381091 +0100
8f2528
@@ -160,6 +160,8 @@ int mm_answer_gss_setup_ctx(int, Buffer
8f2528
 int mm_answer_gss_accept_ctx(int, Buffer *);
8f2528
 int mm_answer_gss_userok(int, Buffer *);
8f2528
 int mm_answer_gss_checkmic(int, Buffer *);
8f2528
+int mm_answer_gss_sign(int, Buffer *);
8f2528
+int mm_answer_gss_updatecreds(int, Buffer *);
8f2528
 #endif
8f2528
 
8f2528
 #ifdef SSH_AUDIT_EVENTS
8f2528
@@ -236,11 +238,18 @@ struct mon_table mon_dispatch_proto20[]
8f2528
     {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
8f2528
     {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
8f2528
     {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
8f2528
+    {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
8f2528
 #endif
8f2528
     {0, 0, NULL}
8f2528
 };
8f2528
 
8f2528
 struct mon_table mon_dispatch_postauth20[] = {
8f2528
+#ifdef GSSAPI
8f2528
+    {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
8f2528
+    {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
8f2528
+    {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
8f2528
+    {MONITOR_REQ_GSSUPCREDS, 0, mm_answer_gss_updatecreds},
8f2528
+#endif
8f2528
 #ifdef WITH_OPENSSL
8f2528
     {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
8f2528
 #endif
8f2528
@@ -307,6 +316,10 @@ monitor_child_preauth(Authctxt *_authctx
8f2528
 	/* Permit requests for moduli and signatures */
8f2528
 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
8f2528
 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
8f2528
+#ifdef GSSAPI
8f2528
+	/* and for the GSSAPI key exchange */
8f2528
+	monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
8f2528
+#endif
8f2528
 
8f2528
 	/* The first few requests do not require asynchronous access */
8f2528
 	while (!authenticated) {
8f2528
@@ -406,6 +419,10 @@ monitor_child_postauth(struct monitor *p
8f2528
 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
8f2528
 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
8f2528
 	monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
8f2528
+#ifdef GSSAPI
8f2528
+	/* and for the GSSAPI key exchange */
8f2528
+	monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
8f2528
+#endif		
8f2528
 
8f2528
 	if (!no_pty_flag) {
8f2528
 		monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
8f2528
@@ -1633,6 +1650,13 @@ monitor_apply_keystate(struct monitor *p
8f2528
 # endif
8f2528
 #endif /* WITH_OPENSSL */
8f2528
 		kex->kex[KEX_C25519_SHA256] = kexc25519_server;
8f2528
+#ifdef GSSAPI
8f2528
+	if (options.gss_keyex) {
8f2528
+		kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
8f2528
+		kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
8f2528
+		kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
8f2528
+	}
8f2528
+#endif
8f2528
 		kex->load_host_public_key=&get_hostkey_public_by_type;
8f2528
 		kex->load_host_private_key=&get_hostkey_private_by_type;
8f2528
 		kex->host_key_index=&get_hostkey_index;
8f2528
@@ -1712,7 +1736,7 @@ mm_answer_gss_setup_ctx(int sock, Buffer
8f2528
 	OM_uint32 major;
8f2528
 	u_int len;
8f2528
 
8f2528
-	if (!options.gss_authentication)
8f2528
+	if (!options.gss_authentication && !options.gss_keyex)
8f2528
 		fatal("%s: GSSAPI authentication not enabled", __func__);
8f2528
 
8f2528
 	goid.elements = buffer_get_string(m, &len;;
8f2528
@@ -1742,7 +1766,7 @@ mm_answer_gss_accept_ctx(int sock, Buffe
8f2528
 	OM_uint32 flags = 0; /* GSI needs this */
8f2528
 	u_int len;
8f2528
 
8f2528
-	if (!options.gss_authentication)
8f2528
+	if (!options.gss_authentication && !options.gss_keyex)
8f2528
 		fatal("%s: GSSAPI authentication not enabled", __func__);
8f2528
 
8f2528
 	in.value = buffer_get_string(m, &len;;
8f2528
@@ -1762,6 +1786,7 @@ mm_answer_gss_accept_ctx(int sock, Buffe
8f2528
 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
8f2528
 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
8f2528
 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
8f2528
+		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
8f2528
 	}
8f2528
 	return (0);
8f2528
 }
8f2528
@@ -1773,7 +1798,7 @@ mm_answer_gss_checkmic(int sock, Buffer
8f2528
 	OM_uint32 ret;
8f2528
 	u_int len;
8f2528
 
8f2528
-	if (!options.gss_authentication)
8f2528
+	if (!options.gss_authentication && !options.gss_keyex)
8f2528
 		fatal("%s: GSSAPI authentication not enabled", __func__);
8f2528
 
8f2528
 	gssbuf.value = buffer_get_string(m, &len;;
8f2528
@@ -1802,10 +1827,11 @@ mm_answer_gss_userok(int sock, Buffer *m
8f2528
 {
8f2528
 	int authenticated;
8f2528
 
8f2528
-	if (!options.gss_authentication)
8f2528
+	if (!options.gss_authentication && !options.gss_keyex)
8f2528
 		fatal("%s: GSSAPI authentication not enabled", __func__);
8f2528
 
8f2528
-	authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
8f2528
+	authenticated = authctxt->valid &&
8f2528
+	    ssh_gssapi_userok(authctxt->user, authctxt->pw);
8f2528
 
8f2528
 	buffer_clear(m);
8f2528
 	buffer_put_int(m, authenticated);
8f2528
@@ -1818,5 +1844,73 @@ mm_answer_gss_userok(int sock, Buffer *m
8f2528
 	/* Monitor loop will terminate if authenticated */
8f2528
 	return (authenticated);
8f2528
 }
8f2528
+
8f2528
+int 
8f2528
+mm_answer_gss_sign(int socket, Buffer *m)
8f2528
+{
8f2528
+	gss_buffer_desc data;
8f2528
+	gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
8f2528
+	OM_uint32 major, minor;
8f2528
+	u_int len;
8f2528
+
8f2528
+	if (!options.gss_authentication && !options.gss_keyex)
8f2528
+		fatal("In GSSAPI monitor when GSSAPI is disabled");
8f2528
+
8f2528
+	data.value = buffer_get_string(m, &len;;
8f2528
+	data.length = len;
8f2528
+	if (data.length != 20) 
8f2528
+		fatal("%s: data length incorrect: %d", __func__, 
8f2528
+		    (int) data.length);
8f2528
+
8f2528
+	/* Save the session ID on the first time around */
8f2528
+	if (session_id2_len == 0) {
8f2528
+		session_id2_len = data.length;
8f2528
+		session_id2 = xmalloc(session_id2_len);
8f2528
+		memcpy(session_id2, data.value, session_id2_len);
8f2528
+	}
8f2528
+	major = ssh_gssapi_sign(gsscontext, &data, &hash);
8f2528
+
8f2528
+	free(data.value);
8f2528
+
8f2528
+	buffer_clear(m);
8f2528
+	buffer_put_int(m, major);
8f2528
+	buffer_put_string(m, hash.value, hash.length);
8f2528
+
8f2528
+	mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
8f2528
+
8f2528
+	gss_release_buffer(&minor, &hash);
8f2528
+
8f2528
+	/* Turn on getpwnam permissions */
8f2528
+	monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
8f2528
+	
8f2528
+	/* And credential updating, for when rekeying */
8f2528
+	monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
8f2528
+
8f2528
+	return (0);
8f2528
+}
8f2528
+
8f2528
+int
8f2528
+mm_answer_gss_updatecreds(int socket, Buffer *m) {
8f2528
+	ssh_gssapi_ccache store;
8f2528
+	int ok;
8f2528
+
8f2528
+	store.filename = buffer_get_string(m, NULL);
8f2528
+	store.envvar   = buffer_get_string(m, NULL);
8f2528
+	store.envval   = buffer_get_string(m, NULL);
8f2528
+
8f2528
+	ok = ssh_gssapi_update_creds(&store);
8f2528
+
8f2528
+	free(store.filename);
8f2528
+	free(store.envvar);
8f2528
+	free(store.envval);
8f2528
+
8f2528
+	buffer_clear(m);
8f2528
+	buffer_put_int(m, ok);
8f2528
+
8f2528
+	mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
8f2528
+
8f2528
+	return(0);
8f2528
+}
8f2528
+
8f2528
 #endif /* GSSAPI */
8f2528
 
8f2528
diff -up openssh-7.4p1/monitor.h.gsskex openssh-7.4p1/monitor.h
8f2528
--- openssh-7.4p1/monitor.h.gsskex	2016-12-23 13:38:53.687300997 +0100
8f2528
+++ openssh-7.4p1/monitor.h	2016-12-23 13:38:53.729301005 +0100
8f2528
@@ -60,6 +60,8 @@ enum monitor_reqtype {
8f2528
 #ifdef WITH_SELINUX
8f2528
 	MONITOR_REQ_AUTHROLE = 80,
8f2528
 #endif
8f2528
+	MONITOR_REQ_GSSSIGN = 82, MONITOR_ANS_GSSSIGN = 83,
8f2528
+	MONITOR_REQ_GSSUPCREDS = 84, MONITOR_ANS_GSSUPCREDS = 85,
8f2528
 
8f2528
 	MONITOR_REQ_PAM_START = 100,
8f2528
 	MONITOR_REQ_PAM_ACCOUNT = 102, MONITOR_ANS_PAM_ACCOUNT = 103,
8f2528
diff -up openssh-7.4p1/monitor_wrap.c.gsskex openssh-7.4p1/monitor_wrap.c
8f2528
--- openssh-7.4p1/monitor_wrap.c.gsskex	2016-12-23 13:38:53.687300997 +0100
8f2528
+++ openssh-7.4p1/monitor_wrap.c	2016-12-23 13:38:53.729301005 +0100
8f2528
@@ -943,7 +943,7 @@ mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss
8f2528
 }
8f2528
 
8f2528
 int
8f2528
-mm_ssh_gssapi_userok(char *user)
8f2528
+mm_ssh_gssapi_userok(char *user, struct passwd *pw)
8f2528
 {
8f2528
 	Buffer m;
8f2528
 	int authenticated = 0;
8f2528
@@ -960,5 +960,50 @@ mm_ssh_gssapi_userok(char *user)
8f2528
 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
8f2528
 	return (authenticated);
8f2528
 }
8f2528
+
8f2528
+OM_uint32
8f2528
+mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash)
8f2528
+{
8f2528
+	Buffer m;
8f2528
+	OM_uint32 major;
8f2528
+	u_int len;
8f2528
+
8f2528
+	buffer_init(&m);
8f2528
+	buffer_put_string(&m, data->value, data->length);
8f2528
+
8f2528
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, &m);
8f2528
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, &m);
8f2528
+
8f2528
+	major = buffer_get_int(&m);
8f2528
+	hash->value = buffer_get_string(&m, &len;;
8f2528
+	hash->length = len;
8f2528
+
8f2528
+	buffer_free(&m);
8f2528
+
8f2528
+	return(major);
8f2528
+}
8f2528
+
8f2528
+int
8f2528
+mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
8f2528
+{
8f2528
+	Buffer m;
8f2528
+	int ok;
8f2528
+
8f2528
+	buffer_init(&m);
8f2528
+
8f2528
+	buffer_put_cstring(&m, store->filename ? store->filename : "");
8f2528
+	buffer_put_cstring(&m, store->envvar ? store->envvar : "");
8f2528
+	buffer_put_cstring(&m, store->envval ? store->envval : "");
8f2528
+
8f2528
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUPCREDS, &m);
8f2528
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUPCREDS, &m);
8f2528
+
8f2528
+	ok = buffer_get_int(&m);
8f2528
+
8f2528
+	buffer_free(&m);
8f2528
+
8f2528
+	return (ok);
8f2528
+}
8f2528
+
8f2528
 #endif /* GSSAPI */
8f2528
 
8f2528
diff -up openssh-7.4p1/monitor_wrap.h.gsskex openssh-7.4p1/monitor_wrap.h
8f2528
--- openssh-7.4p1/monitor_wrap.h.gsskex	2016-12-23 13:38:53.687300997 +0100
8f2528
+++ openssh-7.4p1/monitor_wrap.h	2016-12-23 13:38:53.729301005 +0100
8f2528
@@ -58,8 +58,10 @@ int mm_key_verify(Key *, u_char *, u_int
8f2528
 OM_uint32 mm_ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
8f2528
 OM_uint32 mm_ssh_gssapi_accept_ctx(Gssctxt *,
8f2528
    gss_buffer_desc *, gss_buffer_desc *, OM_uint32 *);
8f2528
-int mm_ssh_gssapi_userok(char *user);
8f2528
+int mm_ssh_gssapi_userok(char *user, struct passwd *);
8f2528
 OM_uint32 mm_ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
8f2528
+OM_uint32 mm_ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
8f2528
+int mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *);
8f2528
 #endif
8f2528
 
8f2528
 #ifdef USE_PAM
8f2528
diff -up openssh-7.4p1/readconf.c.gsskex openssh-7.4p1/readconf.c
8f2528
--- openssh-7.4p1/readconf.c.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/readconf.c	2016-12-23 13:38:53.730301005 +0100
8f2528
@@ -160,6 +160,8 @@ typedef enum {
8f2528
 	oClearAllForwardings, oNoHostAuthenticationForLocalhost,
8f2528
 	oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
8f2528
 	oAddressFamily, oGssAuthentication, oGssDelegateCreds,
8f2528
+	oGssTrustDns, oGssKeyEx, oGssClientIdentity, oGssRenewalRekey,
8f2528
+	oGssServerIdentity, 
8f2528
 	oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
8f2528
 	oSendEnv, oControlPath, oControlMaster, oControlPersist,
8f2528
 	oHashKnownHosts,
8f2528
@@ -205,10 +207,19 @@ static struct {
8f2528
 	{ "afstokenpassing", oUnsupported },
8f2528
 #if defined(GSSAPI)
8f2528
 	{ "gssapiauthentication", oGssAuthentication },
8f2528
+	{ "gssapikeyexchange", oGssKeyEx },
8f2528
 	{ "gssapidelegatecredentials", oGssDelegateCreds },
8f2528
+	{ "gssapitrustdns", oGssTrustDns },
8f2528
+	{ "gssapiclientidentity", oGssClientIdentity },
8f2528
+	{ "gssapiserveridentity", oGssServerIdentity },
8f2528
+	{ "gssapirenewalforcesrekey", oGssRenewalRekey },
8f2528
 #else
8f2528
 	{ "gssapiauthentication", oUnsupported },
8f2528
+	{ "gssapikeyexchange", oUnsupported },
8f2528
 	{ "gssapidelegatecredentials", oUnsupported },
8f2528
+	{ "gssapitrustdns", oUnsupported },
8f2528
+	{ "gssapiclientidentity", oUnsupported },
8f2528
+	{ "gssapirenewalforcesrekey", oUnsupported },
8f2528
 #endif
8f2528
 	{ "fallbacktorsh", oDeprecated },
8f2528
 	{ "usersh", oDeprecated },
8f2528
@@ -961,10 +972,30 @@ parse_time:
8f2528
 		intptr = &options->gss_authentication;
8f2528
 		goto parse_flag;
8f2528
 
8f2528
+	case oGssKeyEx:
8f2528
+		intptr = &options->gss_keyex;
8f2528
+		goto parse_flag;
8f2528
+
8f2528
 	case oGssDelegateCreds:
8f2528
 		intptr = &options->gss_deleg_creds;
8f2528
 		goto parse_flag;
8f2528
 
8f2528
+	case oGssTrustDns:
8f2528
+		intptr = &options->gss_trust_dns;
8f2528
+		goto parse_flag;
8f2528
+
8f2528
+	case oGssClientIdentity:
8f2528
+		charptr = &options->gss_client_identity;
8f2528
+		goto parse_string;
8f2528
+
8f2528
+	case oGssServerIdentity:
8f2528
+		charptr = &options->gss_server_identity;
8f2528
+		goto parse_string;
8f2528
+
8f2528
+	case oGssRenewalRekey:
8f2528
+		intptr = &options->gss_renewal_rekey;
8f2528
+		goto parse_flag;
8f2528
+
8f2528
 	case oBatchMode:
8f2528
 		intptr = &options->batch_mode;
8f2528
 		goto parse_flag;
8f2528
@@ -1776,7 +1807,12 @@ initialize_options(Options * options)
8f2528
 	options->pubkey_authentication = -1;
8f2528
 	options->challenge_response_authentication = -1;
8f2528
 	options->gss_authentication = -1;
8f2528
+	options->gss_keyex = -1;
8f2528
 	options->gss_deleg_creds = -1;
8f2528
+	options->gss_trust_dns = -1;
8f2528
+	options->gss_renewal_rekey = -1;
8f2528
+	options->gss_client_identity = NULL;
8f2528
+	options->gss_server_identity = NULL;
8f2528
 	options->password_authentication = -1;
8f2528
 	options->kbd_interactive_authentication = -1;
8f2528
 	options->kbd_interactive_devices = NULL;
8f2528
@@ -1920,8 +1956,14 @@ fill_default_options(Options * options)
8f2528
 		options->challenge_response_authentication = 1;
8f2528
 	if (options->gss_authentication == -1)
8f2528
 		options->gss_authentication = 0;
8f2528
+	if (options->gss_keyex == -1)
8f2528
+		options->gss_keyex = 0;
8f2528
 	if (options->gss_deleg_creds == -1)
8f2528
 		options->gss_deleg_creds = 0;
8f2528
+	if (options->gss_trust_dns == -1)
8f2528
+		options->gss_trust_dns = 0;
8f2528
+	if (options->gss_renewal_rekey == -1)
8f2528
+		options->gss_renewal_rekey = 0;
8f2528
 	if (options->password_authentication == -1)
8f2528
 		options->password_authentication = 1;
8f2528
 	if (options->kbd_interactive_authentication == -1)
8f2528
diff -up openssh-7.4p1/readconf.h.gsskex openssh-7.4p1/readconf.h
8f2528
--- openssh-7.4p1/readconf.h.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/readconf.h	2016-12-23 13:38:53.730301005 +0100
8f2528
@@ -45,7 +45,12 @@ typedef struct {
8f2528
 	int     challenge_response_authentication;
8f2528
 					/* Try S/Key or TIS, authentication. */
8f2528
 	int     gss_authentication;	/* Try GSS authentication */
8f2528
+	int     gss_keyex;		/* Try GSS key exchange */
8f2528
 	int     gss_deleg_creds;	/* Delegate GSS credentials */
8f2528
+	int	gss_trust_dns;		/* Trust DNS for GSS canonicalization */
8f2528
+	int	gss_renewal_rekey;	/* Credential renewal forces rekey */
8f2528
+	char    *gss_client_identity;   /* Principal to initiate GSSAPI with */
8f2528
+	char    *gss_server_identity;   /* GSSAPI target principal */
8f2528
 	int     password_authentication;	/* Try password
8f2528
 						 * authentication. */
8f2528
 	int     kbd_interactive_authentication; /* Try keyboard-interactive auth. */
8f2528
diff -up openssh-7.4p1/regress/cert-hostkey.sh.gsskex openssh-7.4p1/regress/cert-hostkey.sh
8f2528
--- openssh-7.4p1/regress/cert-hostkey.sh.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/regress/cert-hostkey.sh	2016-12-23 13:38:53.731301006 +0100
8f2528
@@ -59,7 +59,7 @@ touch $OBJ/host_revoked_plain
8f2528
 touch $OBJ/host_revoked_cert
8f2528
 cat $OBJ/host_ca_key.pub $OBJ/host_ca_key2.pub > $OBJ/host_revoked_ca
8f2528
 
8f2528
-PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
8f2528
+PLAIN_TYPES=`$SSH -Q key-plain | grep -v null | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
8f2528
 
8f2528
 if echo "$PLAIN_TYPES" | grep '^rsa$' >/dev/null 2>&1 ; then
8f2528
 	PLAIN_TYPES="$PLAIN_TYPES rsa-sha2-256 rsa-sha2-512"
8f2528
diff -up openssh-7.4p1/regress/cert-userkey.sh.gsskex openssh-7.4p1/regress/cert-userkey.sh
8f2528
--- openssh-7.4p1/regress/cert-userkey.sh.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/regress/cert-userkey.sh	2016-12-23 13:38:53.731301006 +0100
8f2528
@@ -7,7 +7,7 @@ rm -f $OBJ/authorized_keys_$USER $OBJ/us
8f2528
 cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
8f2528
 cp $OBJ/ssh_proxy $OBJ/ssh_proxy_bak
8f2528
 
8f2528
-PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/;s/^ssh-//'`
8f2528
+PLAIN_TYPES=`$SSH -Q key-plain | grep -v null | sed 's/^ssh-dss/ssh-dsa/;s/^ssh-//'`
8f2528
 
8f2528
 if echo "$PLAIN_TYPES" | grep '^rsa$' >/dev/null 2>&1 ; then
8f2528
 	PLAIN_TYPES="$PLAIN_TYPES rsa-sha2-256 rsa-sha2-512"
8f2528
diff -up openssh-7.4p1/regress/kextype.sh.gsskex openssh-7.4p1/regress/kextype.sh
8f2528
--- openssh-7.4p1/regress/kextype.sh.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/regress/kextype.sh	2016-12-23 13:38:53.731301006 +0100
8f2528
@@ -14,6 +14,9 @@ echo "KexAlgorithms=$KEXOPT" >> $OBJ/ssh
8f2528
 
8f2528
 tries="1 2 3 4"
8f2528
 for k in `${SSH} -Q kex`; do
8f2528
+	if [ $k = "gss-gex-sha1-" -o $k = "gss-group1-sha1-" -o $k = "gss-group14-sha1-" ]; then
8f2528
+		continue
8f2528
+	fi
8f2528
 	verbose "kex $k"
8f2528
 	for i in $tries; do
8f2528
 		${SSH} -F $OBJ/ssh_proxy -o KexAlgorithms=$k x true
8f2528
diff -up openssh-7.4p1/regress/rekey.sh.gsskex openssh-7.4p1/regress/rekey.sh
8f2528
--- openssh-7.4p1/regress/rekey.sh.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/regress/rekey.sh	2016-12-23 13:38:53.731301006 +0100
8f2528
@@ -38,6 +38,9 @@ increase_datafile_size 300
8f2528
 
8f2528
 opts=""
8f2528
 for i in `${SSH} -Q kex`; do
8f2528
+	if [ $i = "gss-gex-sha1-" -o $i = "gss-group1-sha1-" -o $i = "gss-group14-sha1-" ]; then
8f2528
+		continue
8f2528
+	fi
8f2528
 	opts="$opts KexAlgorithms=$i"
8f2528
 done
8f2528
 for i in `${SSH} -Q cipher`; do
8f2528
@@ -56,6 +59,9 @@ done
8f2528
 if ${SSH} -Q cipher-auth | grep '^.*$' >/dev/null 2>&1 ; then
8f2528
   for c in `${SSH} -Q cipher-auth`; do
8f2528
     for kex in `${SSH} -Q kex`; do
8f2528
+	if [ $kex = "gss-gex-sha1-" -o $kex = "gss-group1-sha1-" -o $kex = "gss-group14-sha1-" ]; then
8f2528
+		continue
8f2528
+	fi
8f2528
 	verbose "client rekey $c $kex"
8f2528
 	ssh_data_rekeying "KexAlgorithms=$kex" -oRekeyLimit=256k -oCiphers=$c
8f2528
     done
8f2528
diff -up openssh-7.4p1/servconf.c.gsskex openssh-7.4p1/servconf.c
8f2528
--- openssh-7.4p1/servconf.c.gsskex	2016-12-23 13:38:53.717301003 +0100
8f2528
+++ openssh-7.4p1/servconf.c	2016-12-23 13:38:53.732301006 +0100
8f2528
@@ -113,8 +113,10 @@ initialize_server_options(ServerOptions
8f2528
 	options->kerberos_ticket_cleanup = -1;
8f2528
 	options->kerberos_get_afs_token = -1;
8f2528
 	options->gss_authentication=-1;
8f2528
+	options->gss_keyex = -1;
8f2528
 	options->gss_cleanup_creds = -1;
8f2528
 	options->gss_strict_acceptor = -1;
8f2528
+	options->gss_store_rekey = -1;
8f2528
 	options->password_authentication = -1;
8f2528
 	options->kbd_interactive_authentication = -1;
8f2528
 	options->challenge_response_authentication = -1;
8f2528
@@ -268,10 +270,14 @@ fill_default_server_options(ServerOption
8f2528
 		options->kerberos_get_afs_token = 0;
8f2528
 	if (options->gss_authentication == -1)
8f2528
 		options->gss_authentication = 0;
8f2528
+	if (options->gss_keyex == -1)
8f2528
+		options->gss_keyex = 0;
8f2528
 	if (options->gss_cleanup_creds == -1)
8f2528
 		options->gss_cleanup_creds = 1;
8f2528
 	if (options->gss_strict_acceptor == -1)
8f2528
 		options->gss_strict_acceptor = 0;
8f2528
+	if (options->gss_store_rekey == -1)
8f2528
+		options->gss_store_rekey = 0;
8f2528
 	if (options->password_authentication == -1)
8f2528
 		options->password_authentication = 1;
8f2528
 	if (options->kbd_interactive_authentication == -1)
8f2528
@@ -410,7 +416,7 @@ typedef enum {
8f2528
 	sHostKeyAlgorithms,
8f2528
 	sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
8f2528
 	sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
8f2528
-	sAcceptEnv, sPermitTunnel,
8f2528
+	sGssKeyEx, sGssStoreRekey, sAcceptEnv, sPermitTunnel,
8f2528
 	sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
8f2528
 	sUsePrivilegeSeparation, sAllowAgentForwarding,
8f2528
 	sHostCertificate,
8f2528
@@ -484,11 +490,17 @@ static struct {
8f2528
 	{ "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
8f2528
 	{ "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
8f2528
 	{ "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
8f2528
+	{ "gssapikeyexchange", sGssKeyEx, SSHCFG_GLOBAL },
8f2528
+	{ "gssapistorecredentialsonrekey", sGssStoreRekey, SSHCFG_GLOBAL },
8f2528
 #else
8f2528
 	{ "gssapiauthentication", sUnsupported, SSHCFG_ALL },
8f2528
 	{ "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
8f2528
 	{ "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
8f2528
+	{ "gssapikeyexchange", sUnsupported, SSHCFG_GLOBAL },
8f2528
+	{ "gssapistorecredentialsonrekey", sUnsupported, SSHCFG_GLOBAL },
8f2528
 #endif
8f2528
+	{ "gssusesessionccache", sUnsupported, SSHCFG_GLOBAL },
8f2528
+	{ "gssapiusesessioncredcache", sUnsupported, SSHCFG_GLOBAL },
8f2528
 	{ "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
8f2528
 	{ "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
8f2528
 	{ "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
8f2528
@@ -1211,6 +1223,10 @@ process_server_config_line(ServerOptions
8f2528
 		intptr = &options->gss_authentication;
8f2528
 		goto parse_flag;
8f2528
 
8f2528
+	case sGssKeyEx:
8f2528
+		intptr = &options->gss_keyex;
8f2528
+		goto parse_flag;
8f2528
+
8f2528
 	case sGssCleanupCreds:
8f2528
 		intptr = &options->gss_cleanup_creds;
8f2528
 		goto parse_flag;
8f2528
@@ -1219,6 +1235,10 @@ process_server_config_line(ServerOptions
8f2528
 		intptr = &options->gss_strict_acceptor;
8f2528
 		goto parse_flag;
8f2528
 
8f2528
+	case sGssStoreRekey:
8f2528
+		intptr = &options->gss_store_rekey;
8f2528
+		goto parse_flag;
8f2528
+
8f2528
 	case sPasswordAuthentication:
8f2528
 		intptr = &options->password_authentication;
8f2528
 		goto parse_flag;
8f2528
@@ -2257,6 +2277,9 @@ dump_config(ServerOptions *o)
8f2528
 #ifdef GSSAPI
8f2528
 	dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
8f2528
 	dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
8f2528
+	dump_cfg_fmtint(sGssKeyEx, o->gss_keyex);
8f2528
+	dump_cfg_fmtint(sGssStrictAcceptor, o->gss_strict_acceptor);
8f2528
+	dump_cfg_fmtint(sGssStoreRekey, o->gss_store_rekey);
8f2528
 #endif
8f2528
 	dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
8f2528
 	dump_cfg_fmtint(sKbdInteractiveAuthentication,
8f2528
diff -up openssh-7.4p1/servconf.h.gsskex openssh-7.4p1/servconf.h
8f2528
--- openssh-7.4p1/servconf.h.gsskex	2016-12-23 13:38:53.717301003 +0100
8f2528
+++ openssh-7.4p1/servconf.h	2016-12-23 13:38:53.732301006 +0100
8f2528
@@ -112,8 +112,10 @@ typedef struct {
8f2528
 	int     kerberos_get_afs_token;		/* If true, try to get AFS token if
8f2528
 						 * authenticated with Kerberos. */
8f2528
 	int     gss_authentication;	/* If true, permit GSSAPI authentication */
8f2528
+	int     gss_keyex;		/* If true, permit GSSAPI key exchange */
8f2528
 	int     gss_cleanup_creds;	/* If true, destroy cred cache on logout */
8f2528
 	int     gss_strict_acceptor;	/* If true, restrict the GSSAPI acceptor name */
8f2528
+	int 	gss_store_rekey;
8f2528
 	int     password_authentication;	/* If true, permit password
8f2528
 						 * authentication. */
8f2528
 	int     kbd_interactive_authentication;	/* If true, permit */
8f2528
diff -up openssh-7.4p1/ssh_config.5.gsskex openssh-7.4p1/ssh_config.5
8f2528
--- openssh-7.4p1/ssh_config.5.gsskex	2016-12-23 13:38:53.732301006 +0100
8f2528
+++ openssh-7.4p1/ssh_config.5	2016-12-23 13:48:00.502331870 +0100
8f2528
@@ -748,10 +748,40 @@ The default is
8f2528
 Specifies whether user authentication based on GSSAPI is allowed.
8f2528
 The default is
8f2528
 .Cm no .
8f2528
+.It Cm GSSAPIClientIdentity
8f2528
+If set, specifies the GSSAPI client identity that ssh should use when 
8f2528
+connecting to the server. The default is unset, which means that the default 
8f2528
+identity will be used.
8f2528
 .It Cm GSSAPIDelegateCredentials
8f2528
 Forward (delegate) credentials to the server.
8f2528
 The default is
8f2528
 .Cm no .
8f2528
+.It Cm GSSAPIKeyExchange
8f2528
+Specifies whether key exchange based on GSSAPI may be used. When using
8f2528
+GSSAPI key exchange the server need not have a host key.
8f2528
+The default is
8f2528
+.Dq no .
8f2528
+.It Cm GSSAPIRenewalForcesRekey
8f2528
+If set to 
8f2528
+.Dq yes
8f2528
+then renewal of the client's GSSAPI credentials will force the rekeying of the
8f2528
+ssh connection. With a compatible server, this can delegate the renewed 
8f2528
+credentials to a session on the server.
8f2528
+The default is
8f2528
+.Dq no .
8f2528
+.It Cm GSSAPIServerIdentity
8f2528
+If set, specifies the GSSAPI server identity that ssh should expect when 
8f2528
+connecting to the server. The default is unset, which means that the
8f2528
+expected GSSAPI server identity will be determined from the target
8f2528
+hostname.
8f2528
+.It Cm GSSAPITrustDns
8f2528
+Set to 
8f2528
+.Dq yes to indicate that the DNS is trusted to securely canonicalize
8f2528
+the name of the host being connected to. If 
8f2528
+.Dq no, the hostname entered on the
8f2528
+command line will be passed untouched to the GSSAPI library.
8f2528
+The default is
8f2528
+.Dq no .
8f2528
 .It Cm HashKnownHosts
8f2528
 Indicates that
8f2528
 .Xr ssh 1
8f2528
diff -up openssh-7.4p1/ssh_config.gsskex openssh-7.4p1/ssh_config
8f2528
--- openssh-7.4p1/ssh_config.gsskex	2016-12-23 13:38:53.708301001 +0100
8f2528
+++ openssh-7.4p1/ssh_config	2016-12-23 13:38:53.733301006 +0100
8f2528
@@ -26,6 +26,8 @@
8f2528
 #   HostbasedAuthentication no
8f2528
 #   GSSAPIAuthentication no
8f2528
 #   GSSAPIDelegateCredentials no
8f2528
+#   GSSAPIKeyExchange no
8f2528
+#   GSSAPITrustDNS no
8f2528
 #   BatchMode no
8f2528
 #   CheckHostIP yes
8f2528
 #   AddressFamily any
8f2528
diff -up openssh-7.4p1/sshconnect2.c.gsskex openssh-7.4p1/sshconnect2.c
8f2528
--- openssh-7.4p1/sshconnect2.c.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/sshconnect2.c	2016-12-23 13:38:53.733301006 +0100
8f2528
@@ -162,9 +162,34 @@ ssh_kex2(char *host, struct sockaddr *ho
8f2528
 	struct kex *kex;
8f2528
 	int r;
8f2528
 
8f2528
+#ifdef GSSAPI
8f2528
+	char *orig = NULL, *gss = NULL;
8f2528
+	char *gss_host = NULL;
8f2528
+#endif
8f2528
+
8f2528
 	xxx_host = host;
8f2528
 	xxx_hostaddr = hostaddr;
8f2528
 
8f2528
+#ifdef GSSAPI
8f2528
+	if (options.gss_keyex) {
8f2528
+		/* Add the GSSAPI mechanisms currently supported on this 
8f2528
+		 * client to the key exchange algorithm proposal */
8f2528
+		orig = options.kex_algorithms;
8f2528
+
8f2528
+		if (options.gss_trust_dns)
8f2528
+			gss_host = (char *)get_canonical_hostname(active_state, 1);
8f2528
+		else
8f2528
+			gss_host = host;
8f2528
+
8f2528
+		gss = ssh_gssapi_client_mechanisms(gss_host, options.gss_client_identity);
8f2528
+		if (gss) {
8f2528
+			debug("Offering GSSAPI proposal: %s", gss);
8f2528
+			xasprintf(&options.kex_algorithms,
8f2528
+			    "%s,%s", gss, orig);
8f2528
+		}
8f2528
+	}
8f2528
+#endif
8f2528
+
8f2528
 	if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
8f2528
 		fatal("%s: kex_names_cat", __func__);
8f2528
 	myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
8f2528
@@ -192,6 +217,17 @@ ssh_kex2(char *host, struct sockaddr *ho
8f2528
 		    order_hostkeyalgs(host, hostaddr, port));
8f2528
 	}
8f2528
 
8f2528
+#ifdef GSSAPI
8f2528
+	/* If we've got GSSAPI algorithms, then we also support the
8f2528
+	 * 'null' hostkey, as a last resort */
8f2528
+	if (options.gss_keyex && gss) {
8f2528
+		orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
8f2528
+		xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS], 
8f2528
+		    "%s,null", orig);
8f2528
+		free(gss);
8f2528
+	}
8f2528
+#endif
8f2528
+
8f2528
 	if (options.rekey_limit || options.rekey_interval)
8f2528
 		packet_set_rekey_limits((u_int32_t)options.rekey_limit,
8f2528
 		    (time_t)options.rekey_interval);
8f2528
@@ -212,11 +248,31 @@ ssh_kex2(char *host, struct sockaddr *ho
8f2528
 	kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
8f2528
 # endif
8f2528
 #endif
8f2528
+#ifdef GSSAPI
8f2528
+	if (options.gss_keyex) {
8f2528
+		kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
8f2528
+		kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
8f2528
+		kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
8f2528
+	}
8f2528
+#endif
8f2528
 	kex->kex[KEX_C25519_SHA256] = kexc25519_client;
8f2528
 	kex->client_version_string=client_version_string;
8f2528
 	kex->server_version_string=server_version_string;
8f2528
 	kex->verify_host_key=&verify_host_key_callback;
8f2528
 
8f2528
+#ifdef GSSAPI
8f2528
+	if (options.gss_keyex) {
8f2528
+		kex->gss_deleg_creds = options.gss_deleg_creds;
8f2528
+		kex->gss_trust_dns = options.gss_trust_dns;
8f2528
+		kex->gss_client = options.gss_client_identity;
8f2528
+		if (options.gss_server_identity) {
8f2528
+			kex->gss_host = options.gss_server_identity;
8f2528
+		} else {
8f2528
+			kex->gss_host = gss_host;
8f2528
+        }
8f2528
+	}
8f2528
+#endif
8f2528
+
8f2528
 	dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
8f2528
 
8f2528
 	/* remove ext-info from the KEX proposals for rekeying */
8f2528
@@ -311,6 +367,7 @@ int	input_gssapi_token(int type, u_int32
8f2528
 int	input_gssapi_hash(int type, u_int32_t, void *);
8f2528
 int	input_gssapi_error(int, u_int32_t, void *);
8f2528
 int	input_gssapi_errtok(int, u_int32_t, void *);
8f2528
+int	userauth_gsskeyex(Authctxt *authctxt);
8f2528
 #endif
8f2528
 
8f2528
 void	userauth(Authctxt *, char *);
8f2528
@@ -327,6 +384,11 @@ static char *authmethods_get(void);
8f2528
 
8f2528
 Authmethod authmethods[] = {
8f2528
 #ifdef GSSAPI
8f2528
+	{"gssapi-keyex",
8f2528
+		userauth_gsskeyex,
8f2528
+		NULL,
8f2528
+		&options.gss_authentication,
8f2528
+		NULL},
8f2528
 	{"gssapi-with-mic",
8f2528
 		userauth_gssapi,
8f2528
 		NULL,
8f2528
@@ -652,19 +714,31 @@ userauth_gssapi(Authctxt *authctxt)
8f2528
 	static u_int mech = 0;
8f2528
 	OM_uint32 min;
8f2528
 	int ok = 0;
8f2528
+	const char *gss_host;
8f2528
+
8f2528
+	if (options.gss_server_identity)
8f2528
+		gss_host = options.gss_server_identity;
8f2528
+	else if (options.gss_trust_dns)
8f2528
+		gss_host = get_canonical_hostname(active_state, 1);
8f2528
+	else
8f2528
+		gss_host = authctxt->host;
8f2528
 
8f2528
 	/* Try one GSSAPI method at a time, rather than sending them all at
8f2528
 	 * once. */
8f2528
 
8f2528
 	if (gss_supported == NULL)
8f2528
-		gss_indicate_mechs(&min, &gss_supported);
8f2528
+		if (GSS_ERROR(gss_indicate_mechs(&min, &gss_supported))) {
8f2528
+			gss_supported = NULL;
8f2528
+			return 0;
8f2528
+		}
8f2528
 
8f2528
 	/* Check to see if the mechanism is usable before we offer it */
8f2528
 	while (mech < gss_supported->count && !ok) {
8f2528
 		/* My DER encoding requires length<128 */
8f2528
 		if (gss_supported->elements[mech].length < 128 &&
8f2528
 		    ssh_gssapi_check_mechanism(&gssctxt, 
8f2528
-		    &gss_supported->elements[mech], authctxt->host)) {
8f2528
+		    &gss_supported->elements[mech], gss_host, 
8f2528
+                    options.gss_client_identity)) {
8f2528
 			ok = 1; /* Mechanism works */
8f2528
 		} else {
8f2528
 			mech++;
8f2528
@@ -761,8 +835,8 @@ input_gssapi_response(int type, u_int32_
8f2528
 {
8f2528
 	Authctxt *authctxt = ctxt;
8f2528
 	Gssctxt *gssctxt;
8f2528
-	int oidlen;
8f2528
-	char *oidv;
8f2528
+	u_int oidlen;
8f2528
+	u_char *oidv;
8f2528
 
8f2528
 	if (authctxt == NULL)
8f2528
 		fatal("input_gssapi_response: no authentication context");
8f2528
@@ -875,6 +949,48 @@ input_gssapi_error(int type, u_int32_t p
8f2528
 	free(lang);
8f2528
 	return 0;
8f2528
 }
8f2528
+
8f2528
+int
8f2528
+userauth_gsskeyex(Authctxt *authctxt)
8f2528
+{
8f2528
+	Buffer b;
8f2528
+	gss_buffer_desc gssbuf;
8f2528
+	gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
8f2528
+	OM_uint32 ms;
8f2528
+
8f2528
+	static int attempt = 0;
8f2528
+	if (attempt++ >= 1)
8f2528
+		return (0);
8f2528
+
8f2528
+	if (gss_kex_context == NULL) {
8f2528
+		debug("No valid Key exchange context"); 
8f2528
+		return (0);
8f2528
+	}
8f2528
+
8f2528
+	ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
8f2528
+	    "gssapi-keyex");
8f2528
+
8f2528
+	gssbuf.value = buffer_ptr(&b);
8f2528
+	gssbuf.length = buffer_len(&b);
8f2528
+
8f2528
+	if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
8f2528
+		buffer_free(&b);
8f2528
+		return (0);
8f2528
+	}
8f2528
+
8f2528
+	packet_start(SSH2_MSG_USERAUTH_REQUEST);
8f2528
+	packet_put_cstring(authctxt->server_user);
8f2528
+	packet_put_cstring(authctxt->service);
8f2528
+	packet_put_cstring(authctxt->method->name);
8f2528
+	packet_put_string(mic.value, mic.length);
8f2528
+	packet_send();
8f2528
+
8f2528
+	buffer_free(&b);
8f2528
+	gss_release_buffer(&ms, &mic);
8f2528
+
8f2528
+	return (1);
8f2528
+}
8f2528
+
8f2528
 #endif /* GSSAPI */
8f2528
 
8f2528
 int
8f2528
diff -up openssh-7.2p1/sshd.c.gsskex openssh-7.2p1/sshd.c
8f2528
--- openssh-7.2p1/sshd.c.gsskex	2016-02-19 10:01:04.860969328 +0100
8f2528
+++ openssh-7.2p1/sshd.c	2016-02-19 10:01:04.872969321 +0100
8f2528
@@ -547,7 +547,7 @@ privsep_preauth_child(void)
8f2528
 
8f2528
 #ifdef GSSAPI
8f2528
 	/* Cache supported mechanism OIDs for later use */
8f2528
-	if (options.gss_authentication)
8f2528
+	if (options.gss_authentication || options.gss_keyex)
8f2528
 		ssh_gssapi_prepare_supported_oids();
8f2528
 #endif
8f2528
 
8f2528
@@ -974,8 +974,9 @@ notify_hostkeys(struct ssh *ssh)
8f2528
 	}
8f2528
 	debug3("%s: sent %d hostkeys", __func__, nkeys);
8f2528
 	if (nkeys == 0)
8f2528
-		fatal("%s: no hostkeys", __func__);
8f2528
-	packet_send();
8f2528
+		debug3("%s: no hostkeys", __func__);
8f2528
+	else
8f2528
+		packet_send();
8f2528
 	sshbuf_free(buf);
8f2528
 }
8f2528
 
8f2528
@@ -1739,7 +1740,8 @@ main(int ac, char **av)
8f2528
 		    key ? "private" : "agent", i, sshkey_ssh_name(pubkey), fp);
8f2528
 		free(fp);
8f2528
 	}
8f2528
-	if (!sensitive_data.have_ssh2_key) {
8f2528
+	/* The GSSAPI key exchange can run without a host key */
8f2528
+	if (!sensitive_data.have_ssh2_key && !options.gss_keyex) {
8f2528
 		logit("sshd: no hostkeys available -- exiting.");
8f2528
 		exit(1);
8f2528
 	}
8f2528
@@ -2196,6 +2198,48 @@ do_ssh2_kex(void)
8f2528
 	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
8f2528
 	    list_hostkey_types());
8f2528
 
8f2528
+#ifdef GSSAPI
8f2528
+	{
8f2528
+	char *orig;
8f2528
+	char *gss = NULL;
8f2528
+	char *newstr = NULL;
8f2528
+	orig = myproposal[PROPOSAL_KEX_ALGS];
8f2528
+
8f2528
+	/* 
8f2528
+	 * If we don't have a host key, then there's no point advertising
8f2528
+	 * the other key exchange algorithms
8f2528
+	 */
8f2528
+
8f2528
+	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
8f2528
+		orig = NULL;
8f2528
+
8f2528
+	if (options.gss_keyex)
8f2528
+		gss = ssh_gssapi_server_mechanisms();
8f2528
+	else
8f2528
+		gss = NULL;
8f2528
+
8f2528
+	if (gss && orig)
8f2528
+		xasprintf(&newstr, "%s,%s", gss, orig);
8f2528
+	else if (gss)
8f2528
+		newstr = gss;
8f2528
+	else if (orig)
8f2528
+		newstr = orig;
8f2528
+
8f2528
+	/* 
8f2528
+	 * If we've got GSSAPI mechanisms, then we've got the 'null' host
8f2528
+	 * key alg, but we can't tell people about it unless its the only
8f2528
+  	 * host key algorithm we support
8f2528
+	 */
8f2528
+	if (gss && (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS])) == 0)
8f2528
+		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = "null";
8f2528
+
8f2528
+	if (newstr)
8f2528
+		myproposal[PROPOSAL_KEX_ALGS] = newstr;
8f2528
+	else
8f2528
+		fatal("No supported key exchange algorithms");
8f2528
+	}
8f2528
+#endif
8f2528
+
8f2528
 	/* start key exchange */
8f2528
 	if ((r = kex_setup(active_state, myproposal)) != 0)
8f2528
 		fatal("kex_setup: %s", ssh_err(r));
8f2528
@@ -2213,6 +2257,13 @@ do_ssh2_kex(void)
8f2528
 # endif
8f2528
 #endif
8f2528
 	kex->kex[KEX_C25519_SHA256] = kexc25519_server;
8f2528
+#ifdef GSSAPI
8f2528
+	if (options.gss_keyex) {
8f2528
+		kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
8f2528
+		kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
8f2528
+		kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
8f2528
+	}
8f2528
+#endif
8f2528
 	kex->server = 1;
8f2528
 	kex->client_version_string=client_version_string;
8f2528
 	kex->server_version_string=server_version_string;
8f2528
diff -up openssh-7.4p1/sshd_config.5.gsskex openssh-7.4p1/sshd_config.5
8f2528
--- openssh-7.4p1/sshd_config.5.gsskex	2016-12-23 13:38:53.734301006 +0100
8f2528
+++ openssh-7.4p1/sshd_config.5	2016-12-23 13:48:57.825310358 +0100
8f2528
@@ -628,6 +628,11 @@ Specifies whether to automatically destr
8f2528
 on logout.
8f2528
 The default is
8f2528
 .Cm yes .
8f2528
+.It Cm GSSAPIKeyExchange
8f2528
+Specifies whether key exchange based on GSSAPI is allowed. GSSAPI key exchange
8f2528
+doesn't rely on ssh keys to verify host identity.
8f2528
+The default is
8f2528
+.Dq no .
8f2528
 .It Cm GSSAPIStrictAcceptorCheck
8f2528
 Determines whether to be strict about the identity of the GSSAPI acceptor
8f2528
 a client authenticates against.
8f2528
@@ -642,6 +647,11 @@ machine's default store.
8f2528
 This facility is provided to assist with operation on multi homed machines.
8f2528
 The default is
8f2528
 .Cm yes .
8f2528
+.It Cm GSSAPIStoreCredentialsOnRekey
8f2528
+Controls whether the user's GSSAPI credentials should be updated following a 
8f2528
+successful connection rekeying. This option can be used to accepted renewed 
8f2528
+or updated credentials from a compatible client. The default is
8f2528
+.Dq no .
8f2528
 .It Cm HostbasedAcceptedKeyTypes
8f2528
 Specifies the key types that will be accepted for hostbased authentication
8f2528
 as a comma-separated pattern list.
8f2528
diff -up openssh-7.4p1/sshd_config.gsskex openssh-7.4p1/sshd_config
8f2528
--- openssh-7.4p1/sshd_config.gsskex	2016-12-23 13:38:53.719301003 +0100
8f2528
+++ openssh-7.4p1/sshd_config	2016-12-23 13:38:53.734301006 +0100
8f2528
@@ -77,6 +77,8 @@ ChallengeResponseAuthentication no
8f2528
 # GSSAPI options
8f2528
 GSSAPIAuthentication yes
8f2528
 GSSAPICleanupCredentials no
8f2528
+#GSSAPIStrictAcceptorCheck yes
8f2528
+#GSSAPIKeyExchange no
8f2528
 
8f2528
 # Set this to 'yes' to enable PAM authentication, account processing,
8f2528
 # and session processing. If this is enabled, PAM authentication will
8f2528
diff -up openssh-7.4p1/ssh-gss.h.gsskex openssh-7.4p1/ssh-gss.h
8f2528
--- openssh-7.4p1/ssh-gss.h.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/ssh-gss.h	2016-12-23 13:38:53.734301006 +0100
8f2528
@@ -1,6 +1,6 @@
8f2528
 /* $OpenBSD: ssh-gss.h,v 1.11 2014/02/26 20:28:44 djm Exp $ */
8f2528
 /*
8f2528
- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
8f2528
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
8f2528
  *
8f2528
  * Redistribution and use in source and binary forms, with or without
8f2528
  * modification, are permitted provided that the following conditions
8f2528
@@ -61,10 +61,22 @@
8f2528
 
8f2528
 #define SSH_GSS_OIDTYPE 0x06
8f2528
 
8f2528
+#define SSH2_MSG_KEXGSS_INIT                            30
8f2528
+#define SSH2_MSG_KEXGSS_CONTINUE                        31
8f2528
+#define SSH2_MSG_KEXGSS_COMPLETE                        32
8f2528
+#define SSH2_MSG_KEXGSS_HOSTKEY                         33
8f2528
+#define SSH2_MSG_KEXGSS_ERROR                           34
8f2528
+#define SSH2_MSG_KEXGSS_GROUPREQ			40
8f2528
+#define SSH2_MSG_KEXGSS_GROUP				41
8f2528
+#define KEX_GSS_GRP1_SHA1_ID				"gss-group1-sha1-"
8f2528
+#define KEX_GSS_GRP14_SHA1_ID				"gss-group14-sha1-"
8f2528
+#define KEX_GSS_GEX_SHA1_ID				"gss-gex-sha1-"
8f2528
+
8f2528
 typedef struct {
8f2528
 	char *filename;
8f2528
 	char *envvar;
8f2528
 	char *envval;
8f2528
+	struct passwd *owner;
8f2528
 	void *data;
8f2528
 } ssh_gssapi_ccache;
8f2528
 
8f2528
@@ -72,8 +84,11 @@ typedef struct {
8f2528
 	gss_buffer_desc displayname;
8f2528
 	gss_buffer_desc exportedname;
8f2528
 	gss_cred_id_t creds;
8f2528
+	gss_name_t name;
8f2528
 	struct ssh_gssapi_mech_struct *mech;
8f2528
 	ssh_gssapi_ccache store;
8f2528
+	int used;
8f2528
+	int updated;
8f2528
 } ssh_gssapi_client;
8f2528
 
8f2528
 typedef struct ssh_gssapi_mech_struct {
8f2528
@@ -84,6 +99,7 @@ typedef struct ssh_gssapi_mech_struct {
8f2528
 	int (*userok) (ssh_gssapi_client *, char *);
8f2528
 	int (*localname) (ssh_gssapi_client *, char **);
8f2528
 	void (*storecreds) (ssh_gssapi_client *);
8f2528
+	int (*updatecreds) (ssh_gssapi_ccache *, ssh_gssapi_client *);
8f2528
 } ssh_gssapi_mech;
8f2528
 
8f2528
 typedef struct {
8f2528
@@ -94,10 +110,11 @@ typedef struct {
8f2528
 	gss_OID		oid; /* client */
8f2528
 	gss_cred_id_t	creds; /* server */
8f2528
 	gss_name_t	client; /* server */
8f2528
-	gss_cred_id_t	client_creds; /* server */
8f2528
+	gss_cred_id_t	client_creds; /* both */
8f2528
 } Gssctxt;
8f2528
 
8f2528
 extern ssh_gssapi_mech *supported_mechs[];
8f2528
+extern Gssctxt *gss_kex_context;
8f2528
 
8f2528
 int  ssh_gssapi_check_oid(Gssctxt *, void *, size_t);
8f2528
 void ssh_gssapi_set_oid_data(Gssctxt *, void *, size_t);
8f2528
@@ -119,16 +136,32 @@ void ssh_gssapi_build_ctx(Gssctxt **);
8f2528
 void ssh_gssapi_delete_ctx(Gssctxt **);
8f2528
 OM_uint32 ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
8f2528
 void ssh_gssapi_buildmic(Buffer *, const char *, const char *, const char *);
8f2528
-int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *);
8f2528
+int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *, const char *);
8f2528
+OM_uint32 ssh_gssapi_client_identity(Gssctxt *, const char *);
8f2528
+int ssh_gssapi_credentials_updated(Gssctxt *);
8f2528
 
8f2528
 /* In the server */
8f2528
+typedef int ssh_gssapi_check_fn(Gssctxt **, gss_OID, const char *, 
8f2528
+    const char *);
8f2528
+char *ssh_gssapi_client_mechanisms(const char *, const char *);
8f2528
+char *ssh_gssapi_kex_mechs(gss_OID_set, ssh_gssapi_check_fn *, const char *,
8f2528
+    const char *);
8f2528
+gss_OID ssh_gssapi_id_kex(Gssctxt *, char *, int);
8f2528
+int ssh_gssapi_server_check_mech(Gssctxt **,gss_OID, const char *, 
8f2528
+    const char *);
8f2528
 OM_uint32 ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
8f2528
-int ssh_gssapi_userok(char *name);
8f2528
+int ssh_gssapi_userok(char *name, struct passwd *);
8f2528
 OM_uint32 ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
8f2528
 void ssh_gssapi_do_child(char ***, u_int *);
8f2528
 void ssh_gssapi_cleanup_creds(void);
8f2528
 void ssh_gssapi_storecreds(void);
8f2528
 
8f2528
+char *ssh_gssapi_server_mechanisms(void);
8f2528
+int ssh_gssapi_oid_table_ok();
8f2528
+
8f2528
+int ssh_gssapi_update_creds(ssh_gssapi_ccache *store);
8f2528
+
8f2528
+void ssh_gssapi_rekey_creds(void);
8f2528
 #endif /* GSSAPI */
8f2528
 
8f2528
 #endif /* _SSH_GSS_H */
8f2528
diff -up openssh-7.4p1/sshkey.c.gsskex openssh-7.4p1/sshkey.c
8f2528
--- openssh-7.4p1/sshkey.c.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/sshkey.c	2016-12-23 13:38:53.735301006 +0100
8f2528
@@ -114,6 +114,7 @@ static const struct keytype keytypes[] =
8f2528
 #  endif /* OPENSSL_HAS_NISTP521 */
8f2528
 # endif /* OPENSSL_HAS_ECC */
8f2528
 #endif /* WITH_OPENSSL */
8f2528
+	{ "null", "null", KEY_NULL, 0, 0, 1 },
8f2528
 	{ NULL, NULL, -1, -1, 0, 0 }
8f2528
 };
8f2528
 
8f2528
diff -up openssh-7.4p1/sshkey.h.gsskex openssh-7.4p1/sshkey.h
8f2528
--- openssh-7.4p1/sshkey.h.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/sshkey.h	2016-12-23 13:38:53.735301006 +0100
8f2528
@@ -62,6 +62,7 @@ enum sshkey_types {
8f2528
 	KEY_DSA_CERT,
8f2528
 	KEY_ECDSA_CERT,
8f2528
 	KEY_ED25519_CERT,
8f2528
+	KEY_NULL,
8f2528
 	KEY_UNSPEC
8f2528
 };
8f2528
 
8f2528
diff -up openssh-7.4p1/auth.c.gsskex openssh-7.4p1/auth.c
8f2528
--- openssh-7.4p1/auth.c.gsskex	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/auth.c	2016-12-23 13:38:53.735301006 +0100
8f2528
@@ -372,6 +372,7 @@ auth_root_allowed(const char *method)
8f2528
 	case PERMIT_NO_PASSWD:
8f2528
 		if (strcmp(method, "publickey") == 0 ||
8f2528
 		    strcmp(method, "hostbased") == 0 ||
8f2528
+		    strcmp(method, "gssapi-keyex") == 0 ||
8f2528
 		    strcmp(method, "gssapi-with-mic") == 0)
8f2528
 			return 1;
8f2528
 		break;
8f2528
@@ -795,99 +796,6 @@ fakepw(void)
8f2528
 }
8f2528
 
8f2528
 /*
8f2528
- * Returns the remote DNS hostname as a string. The returned string must not
8f2528
- * be freed. NB. this will usually trigger a DNS query the first time it is
8f2528
- * called.
8f2528
- * This function does additional checks on the hostname to mitigate some
8f2528
- * attacks on legacy rhosts-style authentication.
8f2528
- * XXX is RhostsRSAAuthentication vulnerable to these?
8f2528
- * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
8f2528
- */
8f2528
-
8f2528
-static char *
8f2528
-remote_hostname(struct ssh *ssh)
8f2528
-{
8f2528
-	struct sockaddr_storage from;
8f2528
-	socklen_t fromlen;
8f2528
-	struct addrinfo hints, *ai, *aitop;
8f2528
-	char name[NI_MAXHOST], ntop2[NI_MAXHOST];
8f2528
-	const char *ntop = ssh_remote_ipaddr(ssh);
8f2528
-
8f2528
-	/* Get IP address of client. */
8f2528
-	fromlen = sizeof(from);
8f2528
-	memset(&from, 0, sizeof(from));
8f2528
-	if (getpeername(ssh_packet_get_connection_in(ssh),
8f2528
-	    (struct sockaddr *)&from, &fromlen) < 0) {
8f2528
-		debug("getpeername failed: %.100s", strerror(errno));
8f2528
-		return strdup(ntop);
8f2528
-	}
8f2528
-
8f2528
-	ipv64_normalise_mapped(&from, &fromlen);
8f2528
-	if (from.ss_family == AF_INET6)
8f2528
-		fromlen = sizeof(struct sockaddr_in6);
8f2528
-
8f2528
-	debug3("Trying to reverse map address %.100s.", ntop);
8f2528
-	/* Map the IP address to a host name. */
8f2528
-	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
8f2528
-	    NULL, 0, NI_NAMEREQD) != 0) {
8f2528
-		/* Host name not found.  Use ip address. */
8f2528
-		return strdup(ntop);
8f2528
-	}
8f2528
-
8f2528
-	/*
8f2528
-	 * if reverse lookup result looks like a numeric hostname,
8f2528
-	 * someone is trying to trick us by PTR record like following:
8f2528
-	 *	1.1.1.10.in-addr.arpa.	IN PTR	2.3.4.5
8f2528
-	 */
8f2528
-	memset(&hints, 0, sizeof(hints));
8f2528
-	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
8f2528
-	hints.ai_flags = AI_NUMERICHOST;
8f2528
-	if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
8f2528
-		logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
8f2528
-		    name, ntop);
8f2528
-		freeaddrinfo(ai);
8f2528
-		return strdup(ntop);
8f2528
-	}
8f2528
-
8f2528
-	/* Names are stored in lowercase. */
8f2528
-	lowercase(name);
8f2528
-
8f2528
-	/*
8f2528
-	 * Map it back to an IP address and check that the given
8f2528
-	 * address actually is an address of this host.  This is
8f2528
-	 * necessary because anyone with access to a name server can
8f2528
-	 * define arbitrary names for an IP address. Mapping from
8f2528
-	 * name to IP address can be trusted better (but can still be
8f2528
-	 * fooled if the intruder has access to the name server of
8f2528
-	 * the domain).
8f2528
-	 */
8f2528
-	memset(&hints, 0, sizeof(hints));
8f2528
-	hints.ai_family = from.ss_family;
8f2528
-	hints.ai_socktype = SOCK_STREAM;
8f2528
-	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
8f2528
-		logit("reverse mapping checking getaddrinfo for %.700s "
8f2528
-		    "[%s] failed.", name, ntop);
8f2528
-		return strdup(ntop);
8f2528
-	}
8f2528
-	/* Look for the address from the list of addresses. */
8f2528
-	for (ai = aitop; ai; ai = ai->ai_next) {
8f2528
-		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
8f2528
-		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
8f2528
-		    (strcmp(ntop, ntop2) == 0))
8f2528
-				break;
8f2528
-	}
8f2528
-	freeaddrinfo(aitop);
8f2528
-	/* If we reached the end of the list, the address was not there. */
8f2528
-	if (ai == NULL) {
8f2528
-		/* Address not found for the host name. */
8f2528
-		logit("Address %.100s maps to %.600s, but this does not "
8f2528
-		    "map back to the address.", ntop, name);
8f2528
-		return strdup(ntop);
8f2528
-	}
8f2528
-	return strdup(name);
8f2528
-}
8f2528
-
8f2528
-/*
8f2528
  * Return the canonical name of the host in the other side of the current
8f2528
  * connection.  The host name is cached, so it is efficient to call this
8f2528
  * several times.
8f2528
diff -up openssh-7.4p1/openbsd-compat/port-linux.c.gsskex openssh-7.4p1/openbsd-compat/port-linux.c
8f2528
--- openssh-7.4p1/openbsd-compat/port-linux.c.gsskex	2016-12-23 13:38:53.688300997 +0100
8f2528
+++ openssh-7.4p1/openbsd-compat/port-linux.c	2016-12-23 13:38:53.735301006 +0100
8f2528
@@ -30,6 +30,8 @@
8f2528
 #include "log.h"
8f2528
 #include "xmalloc.h"
8f2528
 #include "port-linux.h"
8f2528
+#include "canohost.h"
8f2528
+#include "misc.h"
8f2528
 
8f2528
 #ifdef WITH_SELINUX
8f2528
 #include <selinux/selinux.h>
8f2528
@@ -279,4 +281,121 @@ oom_adjust_restore(void)
8f2528
 	return;
8f2528
 }
8f2528
 #endif /* LINUX_OOM_ADJUST */
8f2528
+
8f2528
+/****************  XXX moved from auth.c ****************/
8f2528
+
8f2528
+/*
8f2528
+ * Returns the remote DNS hostname as a string. The returned string must not
8f2528
+ * be freed. NB. this will usually trigger a DNS query the first time it is
8f2528
+ * called.
8f2528
+ * This function does additional checks on the hostname to mitigate some
8f2528
+ * attacks on legacy rhosts-style authentication.
8f2528
+ * XXX is RhostsRSAAuthentication vulnerable to these?
8f2528
+ * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
8f2528
+ */
8f2528
+
8f2528
+char *
8f2528
+remote_hostname(struct ssh *ssh)
8f2528
+{
8f2528
+	struct sockaddr_storage from;
8f2528
+	socklen_t fromlen;
8f2528
+	struct addrinfo hints, *ai, *aitop;
8f2528
+	char name[NI_MAXHOST], ntop2[NI_MAXHOST];
8f2528
+	const char *ntop = ssh_remote_ipaddr(ssh);
8f2528
+
8f2528
+	/* Get IP address of client. */
8f2528
+	fromlen = sizeof(from);
8f2528
+	memset(&from, 0, sizeof(from));
8f2528
+	if (getpeername(ssh_packet_get_connection_in(ssh),
8f2528
+	    (struct sockaddr *)&from, &fromlen) < 0) {
8f2528
+		debug("getpeername failed: %.100s", strerror(errno));
8f2528
+		return strdup(ntop);
8f2528
+	}
8f2528
+
8f2528
+	ipv64_normalise_mapped(&from, &fromlen);
8f2528
+	if (from.ss_family == AF_INET6)
8f2528
+		fromlen = sizeof(struct sockaddr_in6);
8f2528
+
8f2528
+	debug3("Trying to reverse map address %.100s.", ntop);
8f2528
+	/* Map the IP address to a host name. */
8f2528
+	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
8f2528
+	    NULL, 0, NI_NAMEREQD) != 0) {
8f2528
+		/* Host name not found.  Use ip address. */
8f2528
+		return strdup(ntop);
8f2528
+	}
8f2528
+
8f2528
+	/*
8f2528
+	 * if reverse lookup result looks like a numeric hostname,
8f2528
+	 * someone is trying to trick us by PTR record like following:
8f2528
+	 *	1.1.1.10.in-addr.arpa.	IN PTR	2.3.4.5
8f2528
+	 */
8f2528
+	memset(&hints, 0, sizeof(hints));
8f2528
+	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
8f2528
+	hints.ai_flags = AI_NUMERICHOST;
8f2528
+	if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
8f2528
+		logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
8f2528
+		    name, ntop);
8f2528
+		freeaddrinfo(ai);
8f2528
+		return strdup(ntop);
8f2528
+	}
8f2528
+
8f2528
+	/* Names are stored in lowercase. */
8f2528
+	lowercase(name);
8f2528
+
8f2528
+	/*
8f2528
+	 * Map it back to an IP address and check that the given
8f2528
+	 * address actually is an address of this host.  This is
8f2528
+	 * necessary because anyone with access to a name server can
8f2528
+	 * define arbitrary names for an IP address. Mapping from
8f2528
+	 * name to IP address can be trusted better (but can still be
8f2528
+	 * fooled if the intruder has access to the name server of
8f2528
+	 * the domain).
8f2528
+	 */
8f2528
+	memset(&hints, 0, sizeof(hints));
8f2528
+	hints.ai_family = from.ss_family;
8f2528
+	hints.ai_socktype = SOCK_STREAM;
8f2528
+	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
8f2528
+		logit("reverse mapping checking getaddrinfo for %.700s "
8f2528
+		    "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
8f2528
+		return strdup(ntop);
8f2528
+	}
8f2528
+	/* Look for the address from the list of addresses. */
8f2528
+	for (ai = aitop; ai; ai = ai->ai_next) {
8f2528
+		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
8f2528
+		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
8f2528
+		    (strcmp(ntop, ntop2) == 0))
8f2528
+				break;
8f2528
+	}
8f2528
+	freeaddrinfo(aitop);
8f2528
+	/* If we reached the end of the list, the address was not there. */
8f2528
+	if (ai == NULL) {
8f2528
+		/* Address not found for the host name. */
8f2528
+		logit("Address %.100s maps to %.600s, but this does not "
8f2528
+		    "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
8f2528
+		    ntop, name);
8f2528
+		return strdup(ntop);
8f2528
+	}
8f2528
+	return strdup(name);
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
+ * Return the canonical name of the host in the other side of the current
8f2528
+ * connection.  The host name is cached, so it is efficient to call this
8f2528
+ * several times.
8f2528
+ */
8f2528
+
8f2528
+const char *
8f2528
+get_canonical_hostname(struct ssh *ssh, int use_dns)
8f2528
+{
8f2528
+	static char *dnsname;
8f2528
+
8f2528
+	if (!use_dns)
8f2528
+		return ssh_remote_ipaddr(ssh);
8f2528
+	else if (dnsname != NULL)
8f2528
+		return dnsname;
8f2528
+	else {
8f2528
+		dnsname = remote_hostname(ssh);
8f2528
+		return dnsname;
8f2528
+	}
8f2528
+}
8f2528
 #endif /* WITH_SELINUX || LINUX_OOM_ADJUST */
8f2528
diff -up openssh-7.4p1/openbsd-compat/port-linux.h.gsskex openssh-7.4p1/openbsd-compat/port-linux.h
8f2528
--- openssh-7.4p1/openbsd-compat/port-linux.h.gsskex	2016-12-23 13:38:53.712301002 +0100
8f2528
+++ openssh-7.4p1/openbsd-compat/port-linux.h	2016-12-23 13:38:53.735301006 +0100
8f2528
@@ -16,6 +16,7 @@
8f2528
 
8f2528
 #ifndef _PORT_LINUX_H
8f2528
 #define _PORT_LINUX_H
8f2528
+#include "packet.h"
8f2528
 
8f2528
 #ifdef WITH_SELINUX
8f2528
 int ssh_selinux_enabled(void);
8f2528
@@ -36,4 +37,8 @@ void oom_adjust_setup(void);
8f2528
 
8f2528
 void linux_seed(void);
8f2528
 
8f2528
+const char *get_canonical_hostname(struct ssh *, int);
8f2528
+char *remote_hostname(struct ssh *);
8f2528
+
8f2528
+
8f2528
 #endif /* ! _PORT_LINUX_H */