jonathancammack / rpms / openssh

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