jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone
f5835d
diff -up openssh/auth2.c.gsskex openssh/auth2.c
f5835d
--- openssh/auth2.c.gsskex	2018-08-22 11:47:33.260216045 +0200
f5835d
+++ openssh/auth2.c	2018-08-22 11:47:33.307216424 +0200
f5835d
@@ -74,6 +74,7 @@ extern Authmethod method_passwd;
f5835d
 extern Authmethod method_kbdint;
f5835d
 extern Authmethod method_hostbased;
f5835d
 #ifdef GSSAPI
f5835d
+extern Authmethod method_gsskeyex;
f5835d
 extern Authmethod method_gssapi;
f5835d
 #endif
f5835d
 
f5835d
@@ -81,6 +82,7 @@ Authmethod *authmethods[] = {
f5835d
 	&method_none,
f5835d
 	&method_pubkey,
f5835d
 #ifdef GSSAPI
f5835d
+	&method_gsskeyex,
f5835d
 	&method_gssapi,
f5835d
 #endif
f5835d
 	&method_passwd,
f5835d
diff -up openssh/auth2-gss.c.gsskex openssh/auth2-gss.c
f5835d
--- openssh/auth2-gss.c.gsskex	2018-08-22 11:47:33.260216045 +0200
f5835d
+++ openssh/auth2-gss.c	2018-08-22 13:00:48.722680124 +0200
f5835d
@@ -31,6 +31,7 @@
f5835d
 #include <sys/types.h>
f5835d
 
f5835d
 #include <stdarg.h>
f5835d
+#include <string.h>
f5835d
 
f5835d
 #include "xmalloc.h"
f5835d
 #include "sshkey.h"
f5835d
@@ -54,6 +55,44 @@ static int input_gssapi_mic(int type, u_
f5835d
 static int input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh);
f5835d
 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
f5835d
 
f5835d
+/* 
f5835d
+ * The 'gssapi_keyex' userauth mechanism.
f5835d
+ */
f5835d
+static int
f5835d
+userauth_gsskeyex(struct ssh *ssh)
f5835d
+{
f5835d
+	Authctxt *authctxt = ssh->authctxt;
f5835d
+	int authenticated = 0;
f5835d
+	struct sshbuf *b = NULL;
f5835d
+	gss_buffer_desc mic, gssbuf;
f5835d
+	u_int len;
f5835d
+
f5835d
+	mic.value = packet_get_string(&len;;
f5835d
+	mic.length = len;
f5835d
+
f5835d
+	packet_check_eom();
f5835d
+
f5835d
+	if ((b = sshbuf_new()) == NULL)
f5835d
+		fatal("%s: sshbuf_new failed", __func__);
f5835d
+
f5835d
+	ssh_gssapi_buildmic(b, authctxt->user, authctxt->service,
f5835d
+	    "gssapi-keyex");
f5835d
+
f5835d
+	gssbuf.value = sshbuf_mutable_ptr(b);
f5835d
+	gssbuf.length = sshbuf_len(b);
f5835d
+
f5835d
+	/* gss_kex_context is NULL with privsep, so we can't check it here */
f5835d
+	if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context, 
f5835d
+	    &gssbuf, &mic))))
f5835d
+		authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
f5835d
+		    authctxt->pw));
f5835d
+	
f5835d
+	sshbuf_free(b);
f5835d
+	free(mic.value);
f5835d
+
f5835d
+	return (authenticated);
f5835d
+}
f5835d
+
f5835d
 /*
f5835d
  * We only support those mechanisms that we know about (ie ones that we know
f5835d
  * how to check local user kuserok and the like)
f5835d
@@ -260,7 +296,8 @@ input_gssapi_exchange_complete(int type,
f5835d
 	if ((r = sshpkt_get_end(ssh)) != 0)
f5835d
 		fatal("%s: %s", __func__, ssh_err(r));
f5835d
 
f5835d
-	authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
f5835d
+	authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
f5835d
+	    authctxt->pw));
f5835d
 
f5835d
 	if ((!use_privsep || mm_is_monitor()) &&
f5835d
 	    (displayname = ssh_gssapi_displayname()) != NULL)
f5835d
@@ -313,7 +350,8 @@ input_gssapi_mic(int type, u_int32_t ple
f5835d
 	gssbuf.length = sshbuf_len(b);
f5835d
 
f5835d
 	if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
f5835d
-		authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
f5835d
+		authenticated = 
f5835d
+		    PRIVSEP(ssh_gssapi_userok(authctxt->user, authctxt->pw));
f5835d
 	else
f5835d
 		logit("GSSAPI MIC check failed");
f5835d
 
f5835d
@@ -335,6 +373,12 @@ input_gssapi_mic(int type, u_int32_t ple
f5835d
 	return 0;
f5835d
 }
f5835d
 
f5835d
+Authmethod method_gsskeyex = {
f5835d
+	"gssapi-keyex",
f5835d
+	userauth_gsskeyex,
f5835d
+	&options.gss_authentication
f5835d
+};
f5835d
+
f5835d
 Authmethod method_gssapi = {
f5835d
 	"gssapi-with-mic",
f5835d
 	userauth_gssapi,
f5835d
diff -up openssh/auth.c.gsskex openssh/auth.c
f5835d
--- openssh/auth.c.gsskex	2018-08-22 11:47:33.274216158 +0200
f5835d
+++ openssh/auth.c	2018-08-22 11:47:33.308216432 +0200
f5835d
@@ -395,6 +395,7 @@ auth_root_allowed(struct ssh *ssh, const
f5835d
 	case PERMIT_NO_PASSWD:
f5835d
 		if (strcmp(method, "publickey") == 0 ||
f5835d
 		    strcmp(method, "hostbased") == 0 ||
f5835d
+		    strcmp(method, "gssapi-keyex") == 0 ||
f5835d
 		    strcmp(method, "gssapi-with-mic") == 0)
f5835d
 			return 1;
f5835d
 		break;
f5835d
diff -up openssh/clientloop.c.gsskex openssh/clientloop.c
f5835d
--- openssh/clientloop.c.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/clientloop.c	2018-08-22 11:47:33.309216441 +0200
f5835d
@@ -112,6 +112,10 @@
f5835d
 #include "ssherr.h"
f5835d
 #include "hostfile.h"
f5835d
 
f5835d
+#ifdef GSSAPI
f5835d
+#include "ssh-gss.h"
f5835d
+#endif
f5835d
+
f5835d
 /* import options */
f5835d
 extern Options options;
f5835d
 
f5835d
@@ -1357,9 +1361,18 @@ client_loop(struct ssh *ssh, int have_pt
f5835d
 			break;
f5835d
 
f5835d
 		/* Do channel operations unless rekeying in progress. */
f5835d
-		if (!ssh_packet_is_rekeying(ssh))
f5835d
+		if (!ssh_packet_is_rekeying(ssh)) {
f5835d
 			channel_after_select(ssh, readset, writeset);
f5835d
 
f5835d
+#ifdef GSSAPI
f5835d
+			if (options.gss_renewal_rekey &&
f5835d
+			    ssh_gssapi_credentials_updated(NULL)) {
f5835d
+				debug("credentials updated - forcing rekey");
f5835d
+				need_rekeying = 1;
f5835d
+			}
f5835d
+#endif
f5835d
+		}
f5835d
+
f5835d
 		/* Buffer input from the connection.  */
f5835d
 		client_process_net_input(readset);
f5835d
 
f5835d
diff -up openssh/configure.ac.gsskex openssh/configure.ac
f5835d
--- openssh/configure.ac.gsskex	2018-08-22 11:47:33.296216335 +0200
f5835d
+++ openssh/configure.ac	2018-08-22 11:47:33.309216441 +0200
f5835d
@@ -673,6 +673,30 @@ main() { if (NSVersionOfRunTimeLibrary("
f5835d
 	    [Use tunnel device compatibility to OpenBSD])
f5835d
 	AC_DEFINE([SSH_TUN_PREPEND_AF], [1],
f5835d
 	    [Prepend the address family to IP tunnel traffic])
f5835d
+	AC_MSG_CHECKING(if we have the Security Authorization Session API)
f5835d
+	AC_TRY_COMPILE([#include <Security/AuthSession.h>],
f5835d
+		[SessionCreate(0, 0);],
f5835d
+		[ac_cv_use_security_session_api="yes"
f5835d
+		 AC_DEFINE(USE_SECURITY_SESSION_API, 1, 
f5835d
+			[platform has the Security Authorization Session API])
f5835d
+		 LIBS="$LIBS -framework Security"
f5835d
+		 AC_MSG_RESULT(yes)],
f5835d
+		[ac_cv_use_security_session_api="no"
f5835d
+		 AC_MSG_RESULT(no)])
f5835d
+	AC_MSG_CHECKING(if we have an in-memory credentials cache)
f5835d
+	AC_TRY_COMPILE(
f5835d
+		[#include <Kerberos/Kerberos.h>],
f5835d
+		[cc_context_t c;
f5835d
+		 (void) cc_initialize (&c, 0, NULL, NULL);],
f5835d
+		[AC_DEFINE(USE_CCAPI, 1, 
f5835d
+			[platform uses an in-memory credentials cache])
f5835d
+		 LIBS="$LIBS -framework Security"
f5835d
+		 AC_MSG_RESULT(yes)
f5835d
+		 if test "x$ac_cv_use_security_session_api" = "xno"; then
f5835d
+			AC_MSG_ERROR(*** Need a security framework to use the credentials cache API ***)
f5835d
+		fi],
f5835d
+		[AC_MSG_RESULT(no)]
f5835d
+	)
f5835d
 	m4_pattern_allow([AU_IPv])
f5835d
 	AC_CHECK_DECL([AU_IPv4], [],
f5835d
 	    AC_DEFINE([AU_IPv4], [0], [System only supports IPv4 audit records])
f5835d
diff -up openssh/gss-genr.c.gsskex openssh/gss-genr.c
f5835d
--- openssh/gss-genr.c.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/gss-genr.c	2018-08-22 13:18:47.444383602 +0200
f5835d
@@ -35,18 +35,177 @@
f5835d
 #include <string.h>
f5835d
 #include <signal.h>
f5835d
 #include <unistd.h>
f5835d
+#include <openssl/evp.h>
f5835d
 
f5835d
 #include "xmalloc.h"
f5835d
 #include "ssherr.h"
f5835d
 #include "sshbuf.h"
f5835d
 #include "log.h"
f5835d
 #include "ssh2.h"
f5835d
+#include "cipher.h"
f5835d
+#include "sshkey.h"
f5835d
+#include "kex.h"
f5835d
 
f5835d
 #include "ssh-gss.h"
f5835d
 
f5835d
 extern u_char *session_id2;
f5835d
 extern u_int session_id2_len;
f5835d
 
f5835d
+typedef struct {
f5835d
+	char *encoded;
f5835d
+	gss_OID oid;
f5835d
+} ssh_gss_kex_mapping;
f5835d
+
f5835d
+/*
f5835d
+ * XXX - It would be nice to find a more elegant way of handling the
f5835d
+ * XXX   passing of the key exchange context to the userauth routines
f5835d
+ */
f5835d
+
f5835d
+Gssctxt *gss_kex_context = NULL;
f5835d
+
f5835d
+static ssh_gss_kex_mapping *gss_enc2oid = NULL;
f5835d
+
f5835d
+int 
f5835d
+ssh_gssapi_oid_table_ok() {
f5835d
+	return (gss_enc2oid != NULL);
f5835d
+}
f5835d
+
f5835d
+/*
f5835d
+ * Return a list of the gss-group1-sha1 mechanisms supported by this program
f5835d
+ *
f5835d
+ * We test mechanisms to ensure that we can use them, to avoid starting
f5835d
+ * a key exchange with a bad mechanism
f5835d
+ */
f5835d
+
f5835d
+char *
f5835d
+ssh_gssapi_client_mechanisms(const char *host, const char *client) {
f5835d
+	gss_OID_set gss_supported;
f5835d
+	OM_uint32 min_status;
f5835d
+
f5835d
+	if (GSS_ERROR(gss_indicate_mechs(&min_status, &gss_supported)))
f5835d
+		return NULL;
f5835d
+
f5835d
+	return(ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism,
f5835d
+	    host, client));
f5835d
+}
f5835d
+
f5835d
+char *
f5835d
+ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check,
f5835d
+    const char *host, const char *client) {
f5835d
+	struct sshbuf *buf;
f5835d
+	size_t i;
f5835d
+	int oidpos, enclen, r;
f5835d
+	char *mechs, *encoded;
f5835d
+	u_char digest[EVP_MAX_MD_SIZE];
f5835d
+	char deroid[2];
f5835d
+	const EVP_MD *evp_md = EVP_md5();
f5835d
+	EVP_MD_CTX md;
f5835d
+
f5835d
+	if (gss_enc2oid != NULL) {
f5835d
+		for (i = 0; gss_enc2oid[i].encoded != NULL; i++)
f5835d
+			free(gss_enc2oid[i].encoded);
f5835d
+		free(gss_enc2oid);
f5835d
+	}
f5835d
+
f5835d
+	gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping) *
f5835d
+	    (gss_supported->count + 1));
f5835d
+
f5835d
+	if ((buf = sshbuf_new()) == NULL)
f5835d
+		fatal("%s: sshbuf_new failed", __func__);
f5835d
+
f5835d
+	oidpos = 0;
f5835d
+	for (i = 0; i < gss_supported->count; i++) {
f5835d
+		if (gss_supported->elements[i].length < 128 &&
f5835d
+		    (*check)(NULL, &(gss_supported->elements[i]), host, client)) {
f5835d
+
f5835d
+			deroid[0] = SSH_GSS_OIDTYPE;
f5835d
+			deroid[1] = gss_supported->elements[i].length;
f5835d
+
f5835d
+			EVP_DigestInit(&md, evp_md);
f5835d
+			EVP_DigestUpdate(&md, deroid, 2);
f5835d
+			EVP_DigestUpdate(&md,
f5835d
+			    gss_supported->elements[i].elements,
f5835d
+			    gss_supported->elements[i].length);
f5835d
+			EVP_DigestFinal(&md, digest, NULL);
f5835d
+
f5835d
+			encoded = xmalloc(EVP_MD_size(evp_md) * 2);
f5835d
+			enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
f5835d
+			    encoded, EVP_MD_size(evp_md) * 2);
f5835d
+
f5835d
+			if (oidpos != 0)
f5835d
+				if ((r = sshbuf_put_u8(buf, ',')) != 0)
f5835d
+					fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+			if ((r = sshbuf_put(buf, KEX_GSS_GEX_SHA1_ID,
f5835d
+			    sizeof(KEX_GSS_GEX_SHA1_ID) - 1)) != 0 ||
f5835d
+			    (r = sshbuf_put(buf, encoded, enclen)) != 0 ||
f5835d
+			    (r = sshbuf_put_u8(buf, ',')) != 0 ||
f5835d
+			    (r = sshbuf_put(buf, KEX_GSS_GRP1_SHA1_ID, 
f5835d
+			    sizeof(KEX_GSS_GRP1_SHA1_ID) - 1)) != 0 ||
f5835d
+			    (r = sshbuf_put(buf, encoded, enclen)) != 0 ||
f5835d
+			    (r = sshbuf_put_u8(buf, ',')) != 0 ||
f5835d
+			    (r = sshbuf_put(buf, KEX_GSS_GRP14_SHA1_ID,
f5835d
+			    sizeof(KEX_GSS_GRP14_SHA1_ID) - 1)) != 0 ||
f5835d
+			    (r = sshbuf_put(buf, encoded, enclen)) != 0)
f5835d
+		 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+			gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]);
f5835d
+			gss_enc2oid[oidpos].encoded = encoded;
f5835d
+			oidpos++;
f5835d
+		}
f5835d
+	}
f5835d
+	gss_enc2oid[oidpos].oid = NULL;
f5835d
+	gss_enc2oid[oidpos].encoded = NULL;
f5835d
+
f5835d
+	if ((r = sshbuf_put_u8(buf, '\0')) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mechs = xmalloc(sshbuf_len(buf));
f5835d
+	sshbuf_get(buf, mechs, sshbuf_len(buf));
f5835d
+	sshbuf_free(buf);
f5835d
+
f5835d
+	if (strlen(mechs) == 0) {
f5835d
+		free(mechs);
f5835d
+		mechs = NULL;
f5835d
+	}
f5835d
+
f5835d
+	return (mechs);
f5835d
+}
f5835d
+
f5835d
+gss_OID
f5835d
+ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int kex_type) {
f5835d
+	int i = 0;
f5835d
+	
f5835d
+	switch (kex_type) {
f5835d
+	case KEX_GSS_GRP1_SHA1:
f5835d
+		if (strlen(name) < sizeof(KEX_GSS_GRP1_SHA1_ID))
f5835d
+			return GSS_C_NO_OID;
f5835d
+		name += sizeof(KEX_GSS_GRP1_SHA1_ID) - 1;
f5835d
+		break;
f5835d
+	case KEX_GSS_GRP14_SHA1:
f5835d
+		if (strlen(name) < sizeof(KEX_GSS_GRP14_SHA1_ID))
f5835d
+			return GSS_C_NO_OID;
f5835d
+		name += sizeof(KEX_GSS_GRP14_SHA1_ID) - 1;
f5835d
+		break;
f5835d
+	case KEX_GSS_GEX_SHA1:
f5835d
+		if (strlen(name) < sizeof(KEX_GSS_GEX_SHA1_ID))
f5835d
+			return GSS_C_NO_OID;
f5835d
+		name += sizeof(KEX_GSS_GEX_SHA1_ID) - 1;
f5835d
+		break;
f5835d
+	default:
f5835d
+		return GSS_C_NO_OID;
f5835d
+	}
f5835d
+
f5835d
+	while (gss_enc2oid[i].encoded != NULL &&
f5835d
+	    strcmp(name, gss_enc2oid[i].encoded) != 0)
f5835d
+		i++;
f5835d
+
f5835d
+	if (gss_enc2oid[i].oid != NULL && ctx != NULL)
f5835d
+		ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid);
f5835d
+
f5835d
+	return gss_enc2oid[i].oid;
f5835d
+}
f5835d
+
f5835d
 /* sshbuf_get for gss_buffer_desc */
f5835d
 int
f5835d
 ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g)
f5835d
@@ -218,7 +373,7 @@ ssh_gssapi_init_ctx(Gssctxt *ctx, int de
f5835d
 	}
f5835d
 
f5835d
 	ctx->major = gss_init_sec_context(&ctx->minor,
f5835d
-	    GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
f5835d
+	    ctx->client_creds, &ctx->context, ctx->name, ctx->oid,
f5835d
 	    GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
f5835d
 	    0, NULL, recv_tok, NULL, send_tok, flags, NULL);
f5835d
 
f5835d
@@ -248,8 +403,42 @@ ssh_gssapi_import_name(Gssctxt *ctx, con
f5835d
 }
f5835d
 
f5835d
 OM_uint32
f5835d
+ssh_gssapi_client_identity(Gssctxt *ctx, const char *name)
f5835d
+{
f5835d
+	gss_buffer_desc gssbuf;
f5835d
+	gss_name_t gssname;
f5835d
+	OM_uint32 status;
f5835d
+	gss_OID_set oidset;
f5835d
+
f5835d
+	gssbuf.value = (void *) name;
f5835d
+	gssbuf.length = strlen(gssbuf.value);
f5835d
+
f5835d
+	gss_create_empty_oid_set(&status, &oidset);
f5835d
+	gss_add_oid_set_member(&status, ctx->oid, &oidset);
f5835d
+
f5835d
+	ctx->major = gss_import_name(&ctx->minor, &gssbuf,
f5835d
+	    GSS_C_NT_USER_NAME, &gssname);
f5835d
+
f5835d
+	if (!ctx->major)
f5835d
+		ctx->major = gss_acquire_cred(&ctx->minor, 
f5835d
+		    gssname, 0, oidset, GSS_C_INITIATE, 
f5835d
+		    &ctx->client_creds, NULL, NULL);
f5835d
+
f5835d
+	gss_release_name(&status, &gssname);
f5835d
+	gss_release_oid_set(&status, &oidset);
f5835d
+
f5835d
+	if (ctx->major)
f5835d
+		ssh_gssapi_error(ctx);
f5835d
+
f5835d
+	return(ctx->major);
f5835d
+}
f5835d
+
f5835d
+OM_uint32
f5835d
 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
f5835d
 {
f5835d
+	if (ctx == NULL) 
f5835d
+		return -1;
f5835d
+
f5835d
 	if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
f5835d
 	    GSS_C_QOP_DEFAULT, buffer, hash)))
f5835d
 		ssh_gssapi_error(ctx);
f5835d
@@ -257,6 +446,19 @@ ssh_gssapi_sign(Gssctxt *ctx, gss_buffer
f5835d
 	return (ctx->major);
f5835d
 }
f5835d
 
f5835d
+/* Priviledged when used by server */
f5835d
+OM_uint32
f5835d
+ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
f5835d
+{
f5835d
+	if (ctx == NULL)
f5835d
+		return -1;
f5835d
+
f5835d
+	ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
f5835d
+	    gssbuf, gssmic, NULL);
f5835d
+
f5835d
+	return (ctx->major);
f5835d
+}
f5835d
+
f5835d
 void
f5835d
 ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
f5835d
     const char *context)
f5835d
@@ -273,11 +475,16 @@ ssh_gssapi_buildmic(struct sshbuf *b, co
f5835d
 }
f5835d
 
f5835d
 int
f5835d
-ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
f5835d
+ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host, 
f5835d
+    const char *client)
f5835d
 {
f5835d
 	gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
f5835d
 	OM_uint32 major, minor;
f5835d
 	gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
f5835d
+	Gssctxt *intctx = NULL;
f5835d
+
f5835d
+	if (ctx == NULL)
f5835d
+		ctx = &intct;;
f5835d
 
f5835d
 	/* RFC 4462 says we MUST NOT do SPNEGO */
f5835d
 	if (oid->length == spnego_oid.length && 
f5835d
@@ -287,6 +494,10 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx
f5835d
 	ssh_gssapi_build_ctx(ctx);
f5835d
 	ssh_gssapi_set_oid(*ctx, oid);
f5835d
 	major = ssh_gssapi_import_name(*ctx, host);
f5835d
+
f5835d
+	if (!GSS_ERROR(major) && client)
f5835d
+		major = ssh_gssapi_client_identity(*ctx, client);
f5835d
+
f5835d
 	if (!GSS_ERROR(major)) {
f5835d
 		major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, 
f5835d
 		    NULL);
f5835d
@@ -296,10 +507,66 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx
f5835d
 			    GSS_C_NO_BUFFER);
f5835d
 	}
f5835d
 
f5835d
-	if (GSS_ERROR(major)) 
f5835d
+	if (GSS_ERROR(major) || intctx != NULL) 
f5835d
 		ssh_gssapi_delete_ctx(ctx);
f5835d
 
f5835d
 	return (!GSS_ERROR(major));
f5835d
 }
f5835d
 
f5835d
+int
f5835d
+ssh_gssapi_credentials_updated(Gssctxt *ctxt) {
f5835d
+	static gss_name_t saved_name = GSS_C_NO_NAME;
f5835d
+	static OM_uint32 saved_lifetime = 0;
f5835d
+	static gss_OID saved_mech = GSS_C_NO_OID;
f5835d
+	static gss_name_t name;
f5835d
+	static OM_uint32 last_call = 0;
f5835d
+	OM_uint32 lifetime, now, major, minor;
f5835d
+	int equal;
f5835d
+	
f5835d
+	now = time(NULL);
f5835d
+
f5835d
+	if (ctxt) {
f5835d
+		debug("Rekey has happened - updating saved versions");
f5835d
+
f5835d
+		if (saved_name != GSS_C_NO_NAME)
f5835d
+			gss_release_name(&minor, &saved_name);
f5835d
+
f5835d
+		major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
f5835d
+		    &saved_name, &saved_lifetime, NULL, NULL);
f5835d
+
f5835d
+		if (!GSS_ERROR(major)) {
f5835d
+			saved_mech = ctxt->oid;
f5835d
+		        saved_lifetime+= now;
f5835d
+		} else {
f5835d
+			/* Handle the error */
f5835d
+		}
f5835d
+		return 0;
f5835d
+	}
f5835d
+
f5835d
+	if (now - last_call < 10)
f5835d
+		return 0;
f5835d
+
f5835d
+	last_call = now;
f5835d
+
f5835d
+	if (saved_mech == GSS_C_NO_OID)
f5835d
+		return 0;
f5835d
+	
f5835d
+	major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL, 
f5835d
+	    &name, &lifetime, NULL, NULL);
f5835d
+	if (major == GSS_S_CREDENTIALS_EXPIRED)
f5835d
+		return 0;
f5835d
+	else if (GSS_ERROR(major))
f5835d
+		return 0;
f5835d
+
f5835d
+	major = gss_compare_name(&minor, saved_name, name, &equal);
f5835d
+	gss_release_name(&minor, &name);
f5835d
+	if (GSS_ERROR(major))
f5835d
+		return 0;
f5835d
+
f5835d
+	if (equal && (saved_lifetime < lifetime + now - 10))
f5835d
+		return 1;
f5835d
+
f5835d
+	return 0;
f5835d
+}
f5835d
+
f5835d
 #endif /* GSSAPI */
f5835d
diff -up openssh/gss-serv.c.gsskex openssh/gss-serv.c
f5835d
--- openssh/gss-serv.c.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/gss-serv.c	2018-08-22 11:47:33.310216448 +0200
f5835d
@@ -44,17 +44,19 @@
f5835d
 #include "session.h"
f5835d
 #include "misc.h"
f5835d
 #include "servconf.h"
f5835d
+#include "uidswap.h"
f5835d
 
f5835d
 #include "ssh-gss.h"
f5835d
+#include "monitor_wrap.h"
f5835d
 
f5835d
 extern ServerOptions options;
f5835d
 
f5835d
 static ssh_gssapi_client gssapi_client =
f5835d
-    { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
f5835d
-    GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}};
f5835d
+    { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER, GSS_C_NO_CREDENTIAL,
f5835d
+    GSS_C_NO_NAME, NULL, {NULL, NULL, NULL, NULL}, 0, 0};
f5835d
 
f5835d
 ssh_gssapi_mech gssapi_null_mech =
f5835d
-    { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
f5835d
+    { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
f5835d
 
f5835d
 #ifdef KRB5
f5835d
 extern ssh_gssapi_mech gssapi_kerberos_mech;
f5835d
@@ -141,6 +143,28 @@ ssh_gssapi_server_ctx(Gssctxt **ctx, gss
f5835d
 }
f5835d
 
f5835d
 /* Unprivileged */
f5835d
+char *
f5835d
+ssh_gssapi_server_mechanisms() {
f5835d
+	if (supported_oids == NULL)
f5835d
+		ssh_gssapi_prepare_supported_oids();
f5835d
+	return (ssh_gssapi_kex_mechs(supported_oids,
f5835d
+	    &ssh_gssapi_server_check_mech, NULL, NULL));
f5835d
+}
f5835d
+
f5835d
+/* Unprivileged */
f5835d
+int
f5835d
+ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
f5835d
+    const char *dummy) {
f5835d
+	Gssctxt *ctx = NULL;
f5835d
+	int res;
f5835d
+ 
f5835d
+	res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
f5835d
+	ssh_gssapi_delete_ctx(&ctx;;
f5835d
+
f5835d
+	return (res);
f5835d
+}
f5835d
+
f5835d
+/* Unprivileged */
f5835d
 void
f5835d
 ssh_gssapi_supported_oids(gss_OID_set *oidset)
f5835d
 {
f5835d
@@ -150,7 +174,9 @@ ssh_gssapi_supported_oids(gss_OID_set *o
f5835d
 	gss_OID_set supported;
f5835d
 
f5835d
 	gss_create_empty_oid_set(&min_status, oidset);
f5835d
-	gss_indicate_mechs(&min_status, &supported);
f5835d
+
f5835d
+	if (GSS_ERROR(gss_indicate_mechs(&min_status, &supported)))
f5835d
+		return;
f5835d
 
f5835d
 	while (supported_mechs[i]->name != NULL) {
f5835d
 		if (GSS_ERROR(gss_test_oid_set_member(&min_status,
f5835d
@@ -276,8 +302,48 @@ OM_uint32
f5835d
 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
f5835d
 {
f5835d
 	int i = 0;
f5835d
+	int equal = 0;
f5835d
+	gss_name_t new_name = GSS_C_NO_NAME;
f5835d
+	gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
f5835d
+
f5835d
+	if (options.gss_store_rekey && client->used && ctx->client_creds) {
f5835d
+		if (client->mech->oid.length != ctx->oid->length ||
f5835d
+		    (memcmp(client->mech->oid.elements,
f5835d
+		     ctx->oid->elements, ctx->oid->length) !=0)) {
f5835d
+			debug("Rekeyed credentials have different mechanism");
f5835d
+			return GSS_S_COMPLETE;
f5835d
+		}
f5835d
+
f5835d
+		if ((ctx->major = gss_inquire_cred_by_mech(&ctx->minor, 
f5835d
+		    ctx->client_creds, ctx->oid, &new_name, 
f5835d
+		    NULL, NULL, NULL))) {
f5835d
+			ssh_gssapi_error(ctx);
f5835d
+			return (ctx->major);
f5835d
+		}
f5835d
+
f5835d
+		ctx->major = gss_compare_name(&ctx->minor, client->name, 
f5835d
+		    new_name, &equal);
f5835d
+
f5835d
+		if (GSS_ERROR(ctx->major)) {
f5835d
+			ssh_gssapi_error(ctx);
f5835d
+			return (ctx->major);
f5835d
+		}
f5835d
+ 
f5835d
+		if (!equal) {
f5835d
+			debug("Rekeyed credentials have different name");
f5835d
+			return GSS_S_COMPLETE;
f5835d
+		}
f5835d
+
f5835d
+		debug("Marking rekeyed credentials for export");
f5835d
 
f5835d
-	gss_buffer_desc ename;
f5835d
+		gss_release_name(&ctx->minor, &client->name);
f5835d
+		gss_release_cred(&ctx->minor, &client->creds);
f5835d
+		client->name = new_name;
f5835d
+		client->creds = ctx->client_creds;
f5835d
+        	ctx->client_creds = GSS_C_NO_CREDENTIAL;
f5835d
+		client->updated = 1;
f5835d
+		return GSS_S_COMPLETE;
f5835d
+	}
f5835d
 
f5835d
 	client->mech = NULL;
f5835d
 
f5835d
@@ -292,6 +358,13 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_g
f5835d
 	if (client->mech == NULL)
f5835d
 		return GSS_S_FAILURE;
f5835d
 
f5835d
+	if (ctx->client_creds &&
f5835d
+	    (ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
f5835d
+	     ctx->client_creds, ctx->oid, &client->name, NULL, NULL, NULL))) {
f5835d
+		ssh_gssapi_error(ctx);
f5835d
+		return (ctx->major);
f5835d
+	}
f5835d
+
f5835d
 	if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
f5835d
 	    &client->displayname, NULL))) {
f5835d
 		ssh_gssapi_error(ctx);
f5835d
@@ -309,6 +382,8 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_g
f5835d
 		return (ctx->major);
f5835d
 	}
f5835d
 
f5835d
+	gss_release_buffer(&ctx->minor, &ename);
f5835d
+
f5835d
 	/* We can't copy this structure, so we just move the pointer to it */
f5835d
 	client->creds = ctx->client_creds;
f5835d
 	ctx->client_creds = GSS_C_NO_CREDENTIAL;
f5835d
@@ -319,11 +394,20 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_g
f5835d
 void
f5835d
 ssh_gssapi_cleanup_creds(void)
f5835d
 {
f5835d
-	if (gssapi_client.store.filename != NULL) {
f5835d
-		/* Unlink probably isn't sufficient */
f5835d
-		debug("removing gssapi cred file\"%s\"",
f5835d
-		    gssapi_client.store.filename);
f5835d
-		unlink(gssapi_client.store.filename);
f5835d
+	krb5_ccache ccache = NULL;
f5835d
+	krb5_error_code problem;
f5835d
+
f5835d
+	if (gssapi_client.store.data != NULL) {
f5835d
+		if ((problem = krb5_cc_resolve(gssapi_client.store.data, gssapi_client.store.envval, &ccache))) {
f5835d
+			debug("%s: krb5_cc_resolve(): %.100s", __func__,
f5835d
+				krb5_get_err_text(gssapi_client.store.data, problem));
f5835d
+		} else if ((problem = krb5_cc_destroy(gssapi_client.store.data, ccache))) {
f5835d
+			debug("%s: krb5_cc_resolve(): %.100s", __func__,
f5835d
+				krb5_get_err_text(gssapi_client.store.data, problem));
f5835d
+		} else {
f5835d
+			krb5_free_context(gssapi_client.store.data);
f5835d
+			gssapi_client.store.data = NULL;
f5835d
+		}
f5835d
 	}
f5835d
 }
f5835d
 
f5835d
@@ -356,7 +440,7 @@ ssh_gssapi_do_child(char ***envp, u_int
f5835d
 
f5835d
 /* Privileged */
f5835d
 int
f5835d
-ssh_gssapi_userok(char *user)
f5835d
+ssh_gssapi_userok(char *user, struct passwd *pw)
f5835d
 {
f5835d
 	OM_uint32 lmin;
f5835d
 
f5835d
@@ -366,9 +450,11 @@ ssh_gssapi_userok(char *user)
f5835d
 		return 0;
f5835d
 	}
f5835d
 	if (gssapi_client.mech && gssapi_client.mech->userok)
f5835d
-		if ((*gssapi_client.mech->userok)(&gssapi_client, user))
f5835d
+		if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
f5835d
+			gssapi_client.used = 1;
f5835d
+			gssapi_client.store.owner = pw;
f5835d
 			return 1;
f5835d
-		else {
f5835d
+		} else {
f5835d
 			/* Destroy delegated credentials if userok fails */
f5835d
 			gss_release_buffer(&lmin, &gssapi_client.displayname);
f5835d
 			gss_release_buffer(&lmin, &gssapi_client.exportedname);
f5835d
@@ -382,14 +468,89 @@ ssh_gssapi_userok(char *user)
f5835d
 	return (0);
f5835d
 }
f5835d
 
f5835d
-/* Privileged */
f5835d
-OM_uint32
f5835d
-ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
f5835d
+/* These bits are only used for rekeying. The unpriviledged child is running 
f5835d
+ * as the user, the monitor is root.
f5835d
+ *
f5835d
+ * In the child, we want to :
f5835d
+ *    *) Ask the monitor to store our credentials into the store we specify
f5835d
+ *    *) If it succeeds, maybe do a PAM update
f5835d
+ */
f5835d
+
f5835d
+/* Stuff for PAM */
f5835d
+
f5835d
+#ifdef USE_PAM
f5835d
+static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg, 
f5835d
+    struct pam_response **resp, void *data)
f5835d
 {
f5835d
-	ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
f5835d
-	    gssbuf, gssmic, NULL);
f5835d
+	return (PAM_CONV_ERR);
f5835d
+}
f5835d
+#endif
f5835d
 
f5835d
-	return (ctx->major);
f5835d
+void
f5835d
+ssh_gssapi_rekey_creds() {
f5835d
+	int ok;
f5835d
+	int ret;
f5835d
+#ifdef USE_PAM
f5835d
+	pam_handle_t *pamh = NULL;
f5835d
+	struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
f5835d
+	char *envstr;
f5835d
+#endif
f5835d
+
f5835d
+	if (gssapi_client.store.envval == NULL &&
f5835d
+	    gssapi_client.store.envvar == NULL)
f5835d
+		return;
f5835d
+ 
f5835d
+	ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
f5835d
+
f5835d
+	if (!ok)
f5835d
+		return;
f5835d
+
f5835d
+	debug("Rekeyed credentials stored successfully");
f5835d
+
f5835d
+	/* Actually managing to play with the ssh pam stack from here will
f5835d
+	 * be next to impossible. In any case, we may want different options
f5835d
+	 * for rekeying. So, use our own :)
f5835d
+	 */
f5835d
+#ifdef USE_PAM	
f5835d
+	if (!use_privsep) {
f5835d
+		debug("Not even going to try and do PAM with privsep disabled");
f5835d
+		return;
f5835d
+	}
f5835d
+
f5835d
+	ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
f5835d
+ 	    &pamconv, &pamh);
f5835d
+	if (ret)
f5835d
+		return;
f5835d
+
f5835d
+	xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar, 
f5835d
+	    gssapi_client.store.envval);
f5835d
+
f5835d
+	ret = pam_putenv(pamh, envstr);
f5835d
+	if (!ret)
f5835d
+		pam_setcred(pamh, PAM_REINITIALIZE_CRED);
f5835d
+	pam_end(pamh, PAM_SUCCESS);
f5835d
+#endif
f5835d
+}
f5835d
+
f5835d
+int 
f5835d
+ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
f5835d
+	int ok = 0;
f5835d
+
f5835d
+	/* Check we've got credentials to store */
f5835d
+	if (!gssapi_client.updated)
f5835d
+		return 0;
f5835d
+
f5835d
+	gssapi_client.updated = 0;
f5835d
+
f5835d
+	temporarily_use_uid(gssapi_client.store.owner);
f5835d
+	if (gssapi_client.mech && gssapi_client.mech->updatecreds)
f5835d
+		ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
f5835d
+	else
f5835d
+		debug("No update function for this mechanism");
f5835d
+
f5835d
+	restore_uid();
f5835d
+
f5835d
+	return ok;
f5835d
 }
f5835d
 
f5835d
 /* Privileged */
f5835d
diff -up openssh/gss-serv-krb5.c.gsskex openssh/gss-serv-krb5.c
f5835d
--- openssh/gss-serv-krb5.c.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/gss-serv-krb5.c	2018-08-22 11:47:33.311216457 +0200
f5835d
@@ -120,7 +120,7 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
f5835d
 	krb5_error_code problem;
f5835d
 	krb5_principal princ;
f5835d
 	OM_uint32 maj_status, min_status;
f5835d
-	int len;
f5835d
+	const char *new_ccname, *new_cctype;
f5835d
 	const char *errmsg;
f5835d
 
f5835d
 	if (client->creds == NULL) {
f5835d
@@ -180,11 +180,23 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
f5835d
 		return;
f5835d
 	}
f5835d
 
f5835d
-	client->store.filename = xstrdup(krb5_cc_get_name(krb_context, ccache));
f5835d
+	new_cctype = krb5_cc_get_type(krb_context, ccache);
f5835d
+	new_ccname = krb5_cc_get_name(krb_context, ccache);
f5835d
+
f5835d
 	client->store.envvar = "KRB5CCNAME";
f5835d
-	len = strlen(client->store.filename) + 6;
f5835d
-	client->store.envval = xmalloc(len);
f5835d
-	snprintf(client->store.envval, len, "FILE:%s", client->store.filename);
f5835d
+#ifdef USE_CCAPI
f5835d
+	xasprintf(&client->store.envval, "API:%s", new_ccname);
f5835d
+#else
f5835d
+	if (new_ccname[0] == ':')
f5835d
+		new_ccname++;
f5835d
+	xasprintf(&client->store.envval, "%s:%s", new_cctype, new_ccname);
f5835d
+	if (strcmp(new_cctype, "DIR") == 0) {
f5835d
+		char *p;
f5835d
+		p = strrchr(client->store.envval, '/');
f5835d
+		if (p)
f5835d
+			*p = '\0';
f5835d
+	}
f5835d
+#endif
f5835d
 
f5835d
 #ifdef USE_PAM
f5835d
 	if (options.use_pam)
f5835d
@@ -193,9 +205,76 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
f5835d
 
f5835d
 	krb5_cc_close(krb_context, ccache);
f5835d
 
f5835d
+	client->store.data = krb_context;
f5835d
+
f5835d
 	return;
f5835d
 }
f5835d
 
f5835d
+int
f5835d
+ssh_gssapi_krb5_updatecreds(ssh_gssapi_ccache *store, 
f5835d
+    ssh_gssapi_client *client)
f5835d
+{
f5835d
+	krb5_ccache ccache = NULL;
f5835d
+	krb5_principal principal = NULL;
f5835d
+	char *name = NULL;
f5835d
+	krb5_error_code problem;
f5835d
+	OM_uint32 maj_status, min_status;
f5835d
+
f5835d
+   	if ((problem = krb5_cc_resolve(krb_context, store->envval, &ccache))) {
f5835d
+                logit("krb5_cc_resolve(): %.100s",
f5835d
+                    krb5_get_err_text(krb_context, problem));
f5835d
+                return 0;
f5835d
+       	}
f5835d
+	
f5835d
+	/* Find out who the principal in this cache is */
f5835d
+	if ((problem = krb5_cc_get_principal(krb_context, ccache, 
f5835d
+	    &principal))) {
f5835d
+		logit("krb5_cc_get_principal(): %.100s",
f5835d
+		    krb5_get_err_text(krb_context, problem));
f5835d
+		krb5_cc_close(krb_context, ccache);
f5835d
+		return 0;
f5835d
+	}
f5835d
+
f5835d
+	if ((problem = krb5_unparse_name(krb_context, principal, &name))) {
f5835d
+		logit("krb5_unparse_name(): %.100s",
f5835d
+		    krb5_get_err_text(krb_context, problem));
f5835d
+		krb5_free_principal(krb_context, principal);
f5835d
+		krb5_cc_close(krb_context, ccache);
f5835d
+		return 0;
f5835d
+	}
f5835d
+
f5835d
+
f5835d
+	if (strcmp(name,client->exportedname.value)!=0) {
f5835d
+		debug("Name in local credentials cache differs. Not storing");
f5835d
+		krb5_free_principal(krb_context, principal);
f5835d
+		krb5_cc_close(krb_context, ccache);
f5835d
+		krb5_free_unparsed_name(krb_context, name);
f5835d
+		return 0;
f5835d
+	}
f5835d
+	krb5_free_unparsed_name(krb_context, name);
f5835d
+
f5835d
+	/* Name matches, so lets get on with it! */
f5835d
+
f5835d
+	if ((problem = krb5_cc_initialize(krb_context, ccache, principal))) {
f5835d
+		logit("krb5_cc_initialize(): %.100s",
f5835d
+		    krb5_get_err_text(krb_context, problem));
f5835d
+		krb5_free_principal(krb_context, principal);
f5835d
+		krb5_cc_close(krb_context, ccache);
f5835d
+		return 0;
f5835d
+	}
f5835d
+
f5835d
+	krb5_free_principal(krb_context, principal);
f5835d
+
f5835d
+	if ((maj_status = gss_krb5_copy_ccache(&min_status, client->creds,
f5835d
+	    ccache))) {
f5835d
+		logit("gss_krb5_copy_ccache() failed. Sorry!");
f5835d
+		krb5_cc_close(krb_context, ccache);
f5835d
+		return 0;
f5835d
+	}
f5835d
+
f5835d
+	return 1;
f5835d
+}
f5835d
+
f5835d
 ssh_gssapi_mech gssapi_kerberos_mech = {
f5835d
 	"toWM5Slw5Ew8Mqkay+al2g==",
f5835d
 	"Kerberos",
f5835d
@@ -203,7 +282,8 @@ ssh_gssapi_mech gssapi_kerberos_mech = {
f5835d
 	NULL,
f5835d
 	&ssh_gssapi_krb5_userok,
f5835d
 	NULL,
f5835d
-	&ssh_gssapi_krb5_storecreds
f5835d
+	&ssh_gssapi_krb5_storecreds,
f5835d
+	&ssh_gssapi_krb5_updatecreds
f5835d
 };
f5835d
 
f5835d
 #endif /* KRB5 */
f5835d
diff -up openssh/kex.c.gsskex openssh/kex.c
f5835d
--- openssh/kex.c.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/kex.c	2018-08-22 11:47:33.311216457 +0200
f5835d
@@ -54,6 +54,10 @@
f5835d
 #include "sshbuf.h"
f5835d
 #include "digest.h"
f5835d
 
f5835d
+#ifdef GSSAPI
f5835d
+#include "ssh-gss.h"
f5835d
+#endif
f5835d
+
f5835d
 /* prototype */
f5835d
 static int kex_choose_conf(struct ssh *);
f5835d
 static int kex_input_newkeys(int, u_int32_t, struct ssh *);
f5835d
@@ -103,6 +107,11 @@ static const struct kexalg kexalgs[] = {
f5835d
 	{ KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
f5835d
 	{ KEX_CURVE25519_SHA256_OLD, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
f5835d
 #endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */
f5835d
+#ifdef GSSAPI
f5835d
+	{ KEX_GSS_GEX_SHA1_ID, KEX_GSS_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
f5835d
+	{ KEX_GSS_GRP1_SHA1_ID, KEX_GSS_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
f5835d
+	{ KEX_GSS_GRP14_SHA1_ID, KEX_GSS_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
f5835d
+#endif
f5835d
 	{ NULL, -1, -1, -1},
f5835d
 };
f5835d
 
f5835d
@@ -136,6 +145,12 @@ kex_alg_by_name(const char *name)
f5835d
 	for (k = kexalgs; k->name != NULL; k++) {
f5835d
 		if (strcmp(k->name, name) == 0)
f5835d
 			return k;
f5835d
+#ifdef GSSAPI
f5835d
+		if (strncmp(name, "gss-", 4) == 0) {
f5835d
+			if (strncmp(k->name, name, strlen(k->name)) == 0)
f5835d
+				return k;
f5835d
+		}
f5835d
+#endif
f5835d
 	}
f5835d
 	return NULL;
f5835d
 }
f5835d
diff -up openssh/kexgssc.c.gsskex openssh/kexgssc.c
f5835d
--- openssh/kexgssc.c.gsskex	2018-08-22 11:47:33.311216457 +0200
f5835d
+++ openssh/kexgssc.c	2018-08-22 11:47:33.311216457 +0200
f5835d
@@ -0,0 +1,338 @@
f5835d
+/*
f5835d
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
f5835d
+ *
f5835d
+ * Redistribution and use in source and binary forms, with or without
f5835d
+ * modification, are permitted provided that the following conditions
f5835d
+ * are met:
f5835d
+ * 1. Redistributions of source code must retain the above copyright
f5835d
+ *    notice, this list of conditions and the following disclaimer.
f5835d
+ * 2. Redistributions in binary form must reproduce the above copyright
f5835d
+ *    notice, this list of conditions and the following disclaimer in the
f5835d
+ *    documentation and/or other materials provided with the distribution.
f5835d
+ *
f5835d
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
f5835d
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f5835d
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f5835d
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f5835d
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f5835d
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f5835d
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f5835d
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f5835d
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f5835d
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f5835d
+ */
f5835d
+
f5835d
+#include "includes.h"
f5835d
+
f5835d
+#ifdef GSSAPI
f5835d
+
f5835d
+#include "includes.h"
f5835d
+
f5835d
+#include <openssl/crypto.h>
f5835d
+#include <openssl/bn.h>
f5835d
+
f5835d
+#include <string.h>
f5835d
+
f5835d
+#include "xmalloc.h"
f5835d
+#include "sshbuf.h"
f5835d
+#include "ssh2.h"
f5835d
+#include "sshkey.h"
f5835d
+#include "cipher.h"
f5835d
+#include "kex.h"
f5835d
+#include "log.h"
f5835d
+#include "packet.h"
f5835d
+#include "dh.h"
f5835d
+#include "digest.h"
f5835d
+
f5835d
+#include "ssh-gss.h"
f5835d
+
f5835d
+int
f5835d
+kexgss_client(struct ssh *ssh) {
f5835d
+	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
f5835d
+	gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
f5835d
+	Gssctxt *ctxt;
f5835d
+	OM_uint32 maj_status, min_status, ret_flags;
f5835d
+	u_int klen, kout, slen = 0, strlen;
f5835d
+	DH *dh; 
f5835d
+	BIGNUM *dh_server_pub = NULL;
f5835d
+	BIGNUM *shared_secret = NULL;
f5835d
+	BIGNUM *p = NULL;
f5835d
+	BIGNUM *g = NULL;	
f5835d
+	u_char *kbuf;
f5835d
+	u_char *serverhostkey = NULL;
f5835d
+	u_char *empty = "";
f5835d
+	char *msg;
f5835d
+	char *lang;
f5835d
+	int type = 0;
f5835d
+	int first = 1;
f5835d
+	int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
f5835d
+	u_char hash[SSH_DIGEST_MAX_LENGTH];
f5835d
+	size_t hashlen;
f5835d
+
f5835d
+	/* Initialise our GSSAPI world */	
f5835d
+	ssh_gssapi_build_ctx(&ctxt);
f5835d
+	if (ssh_gssapi_id_kex(ctxt, ssh->kex->name, ssh->kex->kex_type) 
f5835d
+	    == GSS_C_NO_OID)
f5835d
+		fatal("Couldn't identify host exchange");
f5835d
+
f5835d
+	if (ssh_gssapi_import_name(ctxt, ssh->kex->gss_host))
f5835d
+		fatal("Couldn't import hostname");
f5835d
+
f5835d
+	if (ssh->kex->gss_client && 
f5835d
+	    ssh_gssapi_client_identity(ctxt, ssh->kex->gss_client))
f5835d
+		fatal("Couldn't acquire client credentials");
f5835d
+
f5835d
+	switch (ssh->kex->kex_type) {
f5835d
+	case KEX_GSS_GRP1_SHA1:
f5835d
+		dh = dh_new_group1();
f5835d
+		break;
f5835d
+	case KEX_GSS_GRP14_SHA1:
f5835d
+		dh = dh_new_group14();
f5835d
+		break;
f5835d
+	case KEX_GSS_GEX_SHA1:
f5835d
+		debug("Doing group exchange\n");
f5835d
+		nbits = dh_estimate(ssh->kex->we_need * 8);
f5835d
+		packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
f5835d
+		packet_put_int(min);
f5835d
+		packet_put_int(nbits);
f5835d
+		packet_put_int(max);
f5835d
+
f5835d
+		packet_send();
f5835d
+
f5835d
+		packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
f5835d
+
f5835d
+		if ((p = BN_new()) == NULL)
f5835d
+			fatal("BN_new() failed");
f5835d
+		packet_get_bignum2(p);
f5835d
+		if ((g = BN_new()) == NULL)
f5835d
+			fatal("BN_new() failed");
f5835d
+		packet_get_bignum2(g);
f5835d
+		packet_check_eom();
f5835d
+
f5835d
+		if (BN_num_bits(p) < min || BN_num_bits(p) > max)
f5835d
+			fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
f5835d
+			    min, BN_num_bits(p), max);
f5835d
+
f5835d
+		dh = dh_new_group(g, p);
f5835d
+		break;
f5835d
+	default:
f5835d
+		fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
f5835d
+	}
f5835d
+	
f5835d
+	/* Step 1 - e is dh->pub_key */
f5835d
+	dh_gen_key(dh, ssh->kex->we_need * 8);
f5835d
+
f5835d
+	/* This is f, we initialise it now to make life easier */
f5835d
+	dh_server_pub = BN_new();
f5835d
+	if (dh_server_pub == NULL)
f5835d
+		fatal("dh_server_pub == NULL");
f5835d
+
f5835d
+	token_ptr = GSS_C_NO_BUFFER;
f5835d
+			 
f5835d
+	do {
f5835d
+		debug("Calling gss_init_sec_context");
f5835d
+		
f5835d
+		maj_status = ssh_gssapi_init_ctx(ctxt,
f5835d
+		    ssh->kex->gss_deleg_creds, token_ptr, &send_tok,
f5835d
+		    &ret_flags);
f5835d
+
f5835d
+		if (GSS_ERROR(maj_status)) {
f5835d
+			if (send_tok.length != 0) {
f5835d
+				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
f5835d
+				packet_put_string(send_tok.value,
f5835d
+				    send_tok.length);
f5835d
+			}
f5835d
+			fatal("gss_init_context failed");
f5835d
+		}
f5835d
+
f5835d
+		/* If we've got an old receive buffer get rid of it */
f5835d
+		if (token_ptr != GSS_C_NO_BUFFER)
f5835d
+			free(recv_tok.value);
f5835d
+
f5835d
+		if (maj_status == GSS_S_COMPLETE) {
f5835d
+			/* If mutual state flag is not true, kex fails */
f5835d
+			if (!(ret_flags & GSS_C_MUTUAL_FLAG))
f5835d
+				fatal("Mutual authentication failed");
f5835d
+
f5835d
+			/* If integ avail flag is not true kex fails */
f5835d
+			if (!(ret_flags & GSS_C_INTEG_FLAG))
f5835d
+				fatal("Integrity check failed");
f5835d
+		}
f5835d
+
f5835d
+		/* 
f5835d
+		 * If we have data to send, then the last message that we
f5835d
+		 * received cannot have been a 'complete'. 
f5835d
+		 */
f5835d
+		if (send_tok.length != 0) {
f5835d
+			if (first) {
f5835d
+				packet_start(SSH2_MSG_KEXGSS_INIT);
f5835d
+				packet_put_string(send_tok.value,
f5835d
+				    send_tok.length);
f5835d
+				packet_put_bignum2(dh->pub_key);
f5835d
+				first = 0;
f5835d
+			} else {
f5835d
+				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
f5835d
+				packet_put_string(send_tok.value,
f5835d
+				    send_tok.length);
f5835d
+			}
f5835d
+			packet_send();
f5835d
+			gss_release_buffer(&min_status, &send_tok);
f5835d
+
f5835d
+			/* If we've sent them data, they should reply */
f5835d
+			do {	
f5835d
+				type = packet_read();
f5835d
+				if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
f5835d
+					debug("Received KEXGSS_HOSTKEY");
f5835d
+					if (serverhostkey)
f5835d
+						fatal("Server host key received more than once");
f5835d
+					serverhostkey = 
f5835d
+					    packet_get_string(&slen);
f5835d
+				}
f5835d
+			} while (type == SSH2_MSG_KEXGSS_HOSTKEY);
f5835d
+
f5835d
+			switch (type) {
f5835d
+			case SSH2_MSG_KEXGSS_CONTINUE:
f5835d
+				debug("Received GSSAPI_CONTINUE");
f5835d
+				if (maj_status == GSS_S_COMPLETE) 
f5835d
+					fatal("GSSAPI Continue received from server when complete");
f5835d
+				recv_tok.value = packet_get_string(&strlen);
f5835d
+				recv_tok.length = strlen; 
f5835d
+				break;
f5835d
+			case SSH2_MSG_KEXGSS_COMPLETE:
f5835d
+				debug("Received GSSAPI_COMPLETE");
f5835d
+				packet_get_bignum2(dh_server_pub);
f5835d
+				msg_tok.value =  packet_get_string(&strlen);
f5835d
+				msg_tok.length = strlen; 
f5835d
+
f5835d
+				/* Is there a token included? */
f5835d
+				if (packet_get_char()) {
f5835d
+					recv_tok.value=
f5835d
+					    packet_get_string(&strlen);
f5835d
+					recv_tok.length = strlen;
f5835d
+					/* If we're already complete - protocol error */
f5835d
+					if (maj_status == GSS_S_COMPLETE)
f5835d
+						packet_disconnect("Protocol error: received token when complete");
f5835d
+					} else {
f5835d
+						/* No token included */
f5835d
+						if (maj_status != GSS_S_COMPLETE)
f5835d
+							packet_disconnect("Protocol error: did not receive final token");
f5835d
+				}
f5835d
+				break;
f5835d
+			case SSH2_MSG_KEXGSS_ERROR:
f5835d
+				debug("Received Error");
f5835d
+				maj_status = packet_get_int();
f5835d
+				min_status = packet_get_int();
f5835d
+				msg = packet_get_string(NULL);
f5835d
+				lang = packet_get_string(NULL);
f5835d
+				fatal("GSSAPI Error: \n%.400s",msg);
f5835d
+			default:
f5835d
+				packet_disconnect("Protocol error: didn't expect packet type %d",
f5835d
+		    		type);
f5835d
+			}
f5835d
+			token_ptr = &recv_tok;
f5835d
+		} else {
f5835d
+			/* No data, and not complete */
f5835d
+			if (maj_status != GSS_S_COMPLETE)
f5835d
+				fatal("Not complete, and no token output");
f5835d
+		}
f5835d
+	} while (maj_status & GSS_S_CONTINUE_NEEDED);
f5835d
+
f5835d
+	/* 
f5835d
+	 * We _must_ have received a COMPLETE message in reply from the 
f5835d
+	 * server, which will have set dh_server_pub and msg_tok 
f5835d
+	 */
f5835d
+
f5835d
+	if (type != SSH2_MSG_KEXGSS_COMPLETE)
f5835d
+		fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
f5835d
+
f5835d
+	/* Check f in range [1, p-1] */
f5835d
+	if (!dh_pub_is_valid(dh, dh_server_pub))
f5835d
+		packet_disconnect("bad server public DH value");
f5835d
+
f5835d
+	/* compute K=f^x mod p */
f5835d
+	klen = DH_size(dh);
f5835d
+	kbuf = xmalloc(klen);
f5835d
+	kout = DH_compute_key(kbuf, dh_server_pub, dh);
f5835d
+	if ((int)kout < 0)
f5835d
+		fatal("DH_compute_key: failed");
f5835d
+
f5835d
+	shared_secret = BN_new();
f5835d
+	if (shared_secret == NULL)
f5835d
+		fatal("kexgss_client: BN_new failed");
f5835d
+
f5835d
+	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
f5835d
+		fatal("kexdh_client: BN_bin2bn failed");
f5835d
+
f5835d
+	memset(kbuf, 0, klen);
f5835d
+	free(kbuf);
f5835d
+
f5835d
+	hashlen = sizeof(hash);
f5835d
+	switch (ssh->kex->kex_type) {
f5835d
+	case KEX_GSS_GRP1_SHA1:
f5835d
+	case KEX_GSS_GRP14_SHA1:
f5835d
+		kex_dh_hash(ssh->kex->hash_alg, ssh->kex->client_version_string, 
f5835d
+		    ssh->kex->server_version_string,
f5835d
+		    sshbuf_ptr(ssh->kex->my), sshbuf_len(ssh->kex->my),
f5835d
+		    sshbuf_ptr(ssh->kex->peer), sshbuf_len(ssh->kex->peer),
f5835d
+		    (serverhostkey ? serverhostkey : empty), slen,
f5835d
+		    dh->pub_key,	/* e */
f5835d
+		    dh_server_pub,	/* f */
f5835d
+		    shared_secret,	/* K */
f5835d
+		    hash, &hashlen
f5835d
+		);
f5835d
+		break;
f5835d
+	case KEX_GSS_GEX_SHA1:
f5835d
+		kexgex_hash(
f5835d
+		    ssh->kex->hash_alg,
f5835d
+		    ssh->kex->client_version_string,
f5835d
+		    ssh->kex->server_version_string,
f5835d
+		    sshbuf_ptr(ssh->kex->my), sshbuf_len(ssh->kex->my),
f5835d
+		    sshbuf_ptr(ssh->kex->peer), sshbuf_len(ssh->kex->peer),
f5835d
+		    (serverhostkey ? serverhostkey : empty), slen,
f5835d
+ 		    min, nbits, max,
f5835d
+		    dh->p, dh->g,
f5835d
+		    dh->pub_key,
f5835d
+		    dh_server_pub,
f5835d
+		    shared_secret,
f5835d
+		    hash, &hashlen
f5835d
+		);
f5835d
+		break;
f5835d
+	default:
f5835d
+		fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
f5835d
+	}
f5835d
+
f5835d
+	gssbuf.value = hash;
f5835d
+	gssbuf.length = hashlen;
f5835d
+
f5835d
+	/* Verify that the hash matches the MIC we just got. */
f5835d
+	if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
f5835d
+		packet_disconnect("Hash's MIC didn't verify");
f5835d
+
f5835d
+	free(msg_tok.value);
f5835d
+
f5835d
+	DH_free(dh);
f5835d
+	if (serverhostkey)
f5835d
+		free(serverhostkey);
f5835d
+	BN_clear_free(dh_server_pub);
f5835d
+
f5835d
+	/* save session id */
f5835d
+	if (ssh->kex->session_id == NULL) {
f5835d
+		ssh->kex->session_id_len = hashlen;
f5835d
+		ssh->kex->session_id = xmalloc(ssh->kex->session_id_len);
f5835d
+		memcpy(ssh->kex->session_id, hash, ssh->kex->session_id_len);
f5835d
+	}
f5835d
+
f5835d
+	if (ssh->kex->gss_deleg_creds)
f5835d
+		ssh_gssapi_credentials_updated(ctxt);
f5835d
+
f5835d
+	if (gss_kex_context == NULL)
f5835d
+		gss_kex_context = ctxt;
f5835d
+	else
f5835d
+		ssh_gssapi_delete_ctx(&ctxt);
f5835d
+
f5835d
+	kex_derive_keys_bn(ssh, hash, hashlen, shared_secret);
f5835d
+	BN_clear_free(shared_secret);
f5835d
+	return kex_send_newkeys(ssh);
f5835d
+}
f5835d
+
f5835d
+#endif /* GSSAPI */
f5835d
diff -up openssh/kexgsss.c.gsskex openssh/kexgsss.c
f5835d
--- openssh/kexgsss.c.gsskex	2018-08-22 11:47:33.311216457 +0200
f5835d
+++ openssh/kexgsss.c	2018-08-22 11:47:33.311216457 +0200
f5835d
@@ -0,0 +1,297 @@
f5835d
+/*
f5835d
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
f5835d
+ *
f5835d
+ * Redistribution and use in source and binary forms, with or without
f5835d
+ * modification, are permitted provided that the following conditions
f5835d
+ * are met:
f5835d
+ * 1. Redistributions of source code must retain the above copyright
f5835d
+ *    notice, this list of conditions and the following disclaimer.
f5835d
+ * 2. Redistributions in binary form must reproduce the above copyright
f5835d
+ *    notice, this list of conditions and the following disclaimer in the
f5835d
+ *    documentation and/or other materials provided with the distribution.
f5835d
+ *
f5835d
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
f5835d
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f5835d
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f5835d
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f5835d
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f5835d
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f5835d
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f5835d
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f5835d
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f5835d
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f5835d
+ */
f5835d
+
f5835d
+#include "includes.h"
f5835d
+
f5835d
+#ifdef GSSAPI
f5835d
+
f5835d
+#include <string.h>
f5835d
+
f5835d
+#include <openssl/crypto.h>
f5835d
+#include <openssl/bn.h>
f5835d
+
f5835d
+#include "xmalloc.h"
f5835d
+#include "sshbuf.h"
f5835d
+#include "ssh2.h"
f5835d
+#include "sshkey.h"
f5835d
+#include "cipher.h"
f5835d
+#include "kex.h"
f5835d
+#include "log.h"
f5835d
+#include "packet.h"
f5835d
+#include "dh.h"
f5835d
+#include "ssh-gss.h"
f5835d
+#include "monitor_wrap.h"
f5835d
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
f5835d
+#include "servconf.h"
f5835d
+#include "ssh-gss.h"
f5835d
+#include "digest.h"
f5835d
+
f5835d
+extern ServerOptions options;
f5835d
+
f5835d
+int
f5835d
+kexgss_server(struct ssh *ssh)
f5835d
+{
f5835d
+	OM_uint32 maj_status, min_status;
f5835d
+	
f5835d
+	/* 
f5835d
+	 * Some GSSAPI implementations use the input value of ret_flags (an
f5835d
+ 	 * output variable) as a means of triggering mechanism specific 
f5835d
+ 	 * features. Initializing it to zero avoids inadvertently 
f5835d
+ 	 * activating this non-standard behaviour.
f5835d
+	 */
f5835d
+
f5835d
+	OM_uint32 ret_flags = 0;
f5835d
+	gss_buffer_desc gssbuf, recv_tok, msg_tok;
f5835d
+	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
f5835d
+	Gssctxt *ctxt = NULL;
f5835d
+	u_int slen, klen, kout;
f5835d
+	u_char *kbuf;
f5835d
+	DH *dh;
f5835d
+	int min = -1, max = -1, nbits = -1;
f5835d
+	int cmin = -1, cmax = -1; /* client proposal */
f5835d
+	BIGNUM *shared_secret = NULL;
f5835d
+	BIGNUM *dh_client_pub = NULL;
f5835d
+	int type = 0;
f5835d
+	gss_OID oid;
f5835d
+	char *mechs;
f5835d
+	u_char hash[SSH_DIGEST_MAX_LENGTH];
f5835d
+	size_t hashlen;
f5835d
+
f5835d
+	/* Initialise GSSAPI */
f5835d
+
f5835d
+	/* If we're rekeying, privsep means that some of the private structures
f5835d
+	 * in the GSSAPI code are no longer available. This kludges them back
f5835d
+	 * into life
f5835d
+	 */
f5835d
+	if (!ssh_gssapi_oid_table_ok()) 
f5835d
+		if ((mechs = ssh_gssapi_server_mechanisms()))
f5835d
+			free(mechs);
f5835d
+
f5835d
+	debug2("%s: Identifying %s", __func__, ssh->kex->name);
f5835d
+	oid = ssh_gssapi_id_kex(NULL, ssh->kex->name, ssh->kex->kex_type);
f5835d
+	if (oid == GSS_C_NO_OID)
f5835d
+	   fatal("Unknown gssapi mechanism");
f5835d
+
f5835d
+	debug2("%s: Acquiring credentials", __func__);
f5835d
+
f5835d
+	if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
f5835d
+		fatal("Unable to acquire credentials for the server");
f5835d
+
f5835d
+	switch (ssh->kex->kex_type) {
f5835d
+	case KEX_GSS_GRP1_SHA1:
f5835d
+		dh = dh_new_group1();
f5835d
+		break;
f5835d
+	case KEX_GSS_GRP14_SHA1:
f5835d
+		dh = dh_new_group14();
f5835d
+		break;
f5835d
+	case KEX_GSS_GEX_SHA1:
f5835d
+		debug("Doing group exchange");
f5835d
+		packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ);
f5835d
+		/* store client proposal to provide valid signature */
f5835d
+		cmin = packet_get_int();
f5835d
+		nbits = packet_get_int();
f5835d
+		cmax = packet_get_int();
f5835d
+		min = MAX(DH_GRP_MIN, cmin);
f5835d
+		max = MIN(DH_GRP_MAX, cmax);
f5835d
+		packet_check_eom();
f5835d
+		if (max < min || nbits < min || max < nbits)
f5835d
+			fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
f5835d
+			    min, nbits, max);
f5835d
+		dh = PRIVSEP(choose_dh(min, nbits, max));
f5835d
+		if (dh == NULL)
f5835d
+			packet_disconnect("Protocol error: no matching group found");
f5835d
+
f5835d
+		packet_start(SSH2_MSG_KEXGSS_GROUP);
f5835d
+		packet_put_bignum2(dh->p);
f5835d
+		packet_put_bignum2(dh->g);
f5835d
+		packet_send();
f5835d
+
f5835d
+		packet_write_wait();
f5835d
+		break;
f5835d
+	default:
f5835d
+		fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
f5835d
+	}
f5835d
+
f5835d
+	dh_gen_key(dh, ssh->kex->we_need * 8);
f5835d
+
f5835d
+	do {
f5835d
+		debug("Wait SSH2_MSG_GSSAPI_INIT");
f5835d
+		type = packet_read();
f5835d
+		switch(type) {
f5835d
+		case SSH2_MSG_KEXGSS_INIT:
f5835d
+			if (dh_client_pub != NULL) 
f5835d
+				fatal("Received KEXGSS_INIT after initialising");
f5835d
+			recv_tok.value = packet_get_string(&slen);
f5835d
+			recv_tok.length = slen; 
f5835d
+
f5835d
+			if ((dh_client_pub = BN_new()) == NULL)
f5835d
+				fatal("dh_client_pub == NULL");
f5835d
+
f5835d
+			packet_get_bignum2(dh_client_pub);
f5835d
+
f5835d
+			/* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
f5835d
+			break;
f5835d
+		case SSH2_MSG_KEXGSS_CONTINUE:
f5835d
+			recv_tok.value = packet_get_string(&slen);
f5835d
+			recv_tok.length = slen; 
f5835d
+			break;
f5835d
+		default:
f5835d
+			packet_disconnect(
f5835d
+			    "Protocol error: didn't expect packet type %d",
f5835d
+			    type);
f5835d
+		}
f5835d
+
f5835d
+		maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, 
f5835d
+		    &send_tok, &ret_flags));
f5835d
+
f5835d
+		free(recv_tok.value);
f5835d
+
f5835d
+		if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
f5835d
+			fatal("Zero length token output when incomplete");
f5835d
+
f5835d
+		if (dh_client_pub == NULL)
f5835d
+			fatal("No client public key");
f5835d
+		
f5835d
+		if (maj_status & GSS_S_CONTINUE_NEEDED) {
f5835d
+			debug("Sending GSSAPI_CONTINUE");
f5835d
+			packet_start(SSH2_MSG_KEXGSS_CONTINUE);
f5835d
+			packet_put_string(send_tok.value, send_tok.length);
f5835d
+			packet_send();
f5835d
+			gss_release_buffer(&min_status, &send_tok);
f5835d
+		}
f5835d
+	} while (maj_status & GSS_S_CONTINUE_NEEDED);
f5835d
+
f5835d
+	if (GSS_ERROR(maj_status)) {
f5835d
+		if (send_tok.length > 0) {
f5835d
+			packet_start(SSH2_MSG_KEXGSS_CONTINUE);
f5835d
+			packet_put_string(send_tok.value, send_tok.length);
f5835d
+			packet_send();
f5835d
+		}
f5835d
+		fatal("accept_ctx died");
f5835d
+	}
f5835d
+
f5835d
+	if (!(ret_flags & GSS_C_MUTUAL_FLAG))
f5835d
+		fatal("Mutual Authentication flag wasn't set");
f5835d
+
f5835d
+	if (!(ret_flags & GSS_C_INTEG_FLAG))
f5835d
+		fatal("Integrity flag wasn't set");
f5835d
+	
f5835d
+	if (!dh_pub_is_valid(dh, dh_client_pub))
f5835d
+		packet_disconnect("bad client public DH value");
f5835d
+
f5835d
+	klen = DH_size(dh);
f5835d
+	kbuf = xmalloc(klen); 
f5835d
+	kout = DH_compute_key(kbuf, dh_client_pub, dh);
f5835d
+	if ((int)kout < 0)
f5835d
+		fatal("DH_compute_key: failed");
f5835d
+
f5835d
+	shared_secret = BN_new();
f5835d
+	if (shared_secret == NULL)
f5835d
+		fatal("kexgss_server: BN_new failed");
f5835d
+
f5835d
+	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
f5835d
+		fatal("kexgss_server: BN_bin2bn failed");
f5835d
+
f5835d
+	memset(kbuf, 0, klen);
f5835d
+	free(kbuf);
f5835d
+
f5835d
+	hashlen = sizeof(hash);
f5835d
+	switch (ssh->kex->kex_type) {
f5835d
+	case KEX_GSS_GRP1_SHA1:
f5835d
+	case KEX_GSS_GRP14_SHA1:
f5835d
+		kex_dh_hash(ssh->kex->hash_alg,
f5835d
+		    ssh->kex->client_version_string, ssh->kex->server_version_string,
f5835d
+		    sshbuf_ptr(ssh->kex->peer), sshbuf_len(ssh->kex->peer),
f5835d
+		    sshbuf_ptr(ssh->kex->my), sshbuf_len(ssh->kex->my),
f5835d
+		    NULL, 0, /* Change this if we start sending host keys */
f5835d
+		    dh_client_pub, dh->pub_key, shared_secret,
f5835d
+		    hash, &hashlen
f5835d
+		);
f5835d
+		break;
f5835d
+	case KEX_GSS_GEX_SHA1:
f5835d
+		kexgex_hash(
f5835d
+		    ssh->kex->hash_alg,
f5835d
+		    ssh->kex->client_version_string, ssh->kex->server_version_string,
f5835d
+		    sshbuf_ptr(ssh->kex->peer), sshbuf_len(ssh->kex->peer),
f5835d
+		    sshbuf_ptr(ssh->kex->my), sshbuf_len(ssh->kex->my),
f5835d
+		    NULL, 0,
f5835d
+		    cmin, nbits, cmax,
f5835d
+		    dh->p, dh->g,
f5835d
+		    dh_client_pub,
f5835d
+		    dh->pub_key,
f5835d
+		    shared_secret,
f5835d
+		    hash, &hashlen
f5835d
+		);
f5835d
+		break;
f5835d
+	default:
f5835d
+		fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
f5835d
+	}
f5835d
+
f5835d
+	BN_clear_free(dh_client_pub);
f5835d
+
f5835d
+	if (ssh->kex->session_id == NULL) {
f5835d
+		ssh->kex->session_id_len = hashlen;
f5835d
+		ssh->kex->session_id = xmalloc(ssh->kex->session_id_len);
f5835d
+		memcpy(ssh->kex->session_id, hash, ssh->kex->session_id_len);
f5835d
+	}
f5835d
+
f5835d
+	gssbuf.value = hash;
f5835d
+	gssbuf.length = hashlen;
f5835d
+
f5835d
+	if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
f5835d
+		fatal("Couldn't get MIC");
f5835d
+
f5835d
+	packet_start(SSH2_MSG_KEXGSS_COMPLETE);
f5835d
+	packet_put_bignum2(dh->pub_key);
f5835d
+	packet_put_string(msg_tok.value,msg_tok.length);
f5835d
+
f5835d
+	if (send_tok.length != 0) {
f5835d
+		packet_put_char(1); /* true */
f5835d
+		packet_put_string(send_tok.value, send_tok.length);
f5835d
+	} else {
f5835d
+		packet_put_char(0); /* false */
f5835d
+	}
f5835d
+	packet_send();
f5835d
+
f5835d
+	gss_release_buffer(&min_status, &send_tok);
f5835d
+	gss_release_buffer(&min_status, &msg_tok);
f5835d
+
f5835d
+	if (gss_kex_context == NULL)
f5835d
+		gss_kex_context = ctxt;
f5835d
+	else 
f5835d
+		ssh_gssapi_delete_ctx(&ctxt);
f5835d
+
f5835d
+	DH_free(dh);
f5835d
+
f5835d
+	kex_derive_keys_bn(ssh, hash, hashlen, shared_secret);
f5835d
+	BN_clear_free(shared_secret);
f5835d
+	kex_send_newkeys(ssh);
f5835d
+
f5835d
+	/* If this was a rekey, then save out any delegated credentials we
f5835d
+	 * just exchanged.  */
f5835d
+	if (options.gss_store_rekey)
f5835d
+		ssh_gssapi_rekey_creds();
f5835d
+	return 0;
f5835d
+}
f5835d
+#endif /* GSSAPI */
f5835d
diff -up openssh/kex.h.gsskex openssh/kex.h
f5835d
--- openssh/kex.h.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/kex.h	2018-08-22 11:47:33.311216457 +0200
f5835d
@@ -100,6 +100,11 @@ enum kex_exchange {
f5835d
 	KEX_DH_GEX_SHA256,
f5835d
 	KEX_ECDH_SHA2,
f5835d
 	KEX_C25519_SHA256,
f5835d
+#ifdef GSSAPI
f5835d
+	KEX_GSS_GRP1_SHA1,
f5835d
+	KEX_GSS_GRP14_SHA1,
f5835d
+	KEX_GSS_GEX_SHA1,
f5835d
+#endif
f5835d
 	KEX_MAX
f5835d
 };
f5835d
 
f5835d
@@ -148,6 +153,12 @@ struct kex {
f5835d
 	u_int	flags;
f5835d
 	int	hash_alg;
f5835d
 	int	ec_nid;
f5835d
+#ifdef GSSAPI
f5835d
+	int	gss_deleg_creds;
f5835d
+	int	gss_trust_dns;
f5835d
+	char    *gss_host;
f5835d
+	char	*gss_client;
f5835d
+#endif
f5835d
 	char	*client_version_string;
f5835d
 	char	*server_version_string;
f5835d
 	char	*failed_choice;
f5835d
@@ -197,6 +208,10 @@ int	 kexecdh_client(struct ssh *);
f5835d
 int	 kexecdh_server(struct ssh *);
f5835d
 int	 kexc25519_client(struct ssh *);
f5835d
 int	 kexc25519_server(struct ssh *);
f5835d
+#ifdef GSSAPI
f5835d
+int	 kexgss_client(struct ssh *);
f5835d
+int	 kexgss_server(struct ssh *);
f5835d
+#endif
f5835d
 
f5835d
 int	 kex_dh_hash(int, const char *, const char *,
f5835d
     const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
f5835d
diff -up openssh/Makefile.in.gsskex openssh/Makefile.in
f5835d
--- openssh/Makefile.in.gsskex	2018-08-22 11:47:33.312216465 +0200
f5835d
+++ openssh/Makefile.in	2018-08-22 13:19:54.955928277 +0200
f5835d
@@ -100,6 +100,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
f5835d
 	readpass.o ttymodes.o xmalloc.o addrmatch.o \
f5835d
 	atomicio.o dispatch.o mac.o uuencode.o misc.o utf8.o \
f5835d
 	monitor_fdpass.o rijndael.o ssh-dss.o ssh-ecdsa.o ssh-rsa.o dh.o \
f5835d
+	kexgssc.o \
f5835d
 	msg.o progressmeter.o dns.o entropy.o gss-genr.o umac.o umac128.o \
f5835d
 	ssh-pkcs11.o smult_curve25519_ref.o \
f5835d
 	poly1305.o chacha.o cipher-chachapoly.o \
f5835d
@@ -121,7 +122,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passw
f5835d
 	auth-bsdauth.o auth2-hostbased.o auth2-kbdint.o \
f5835d
 	auth2-none.o auth2-passwd.o auth2-pubkey.o \
f5835d
 	monitor.o monitor_wrap.o auth-krb5.o \
f5835d
-	auth2-gss.o gss-serv.o gss-serv-krb5.o \
f5835d
+	auth2-gss.o gss-serv.o gss-serv-krb5.o kexgsss.o \
f5835d
 	loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \
f5835d
 	sftp-server.o sftp-common.o \
f5835d
 	sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \
f5835d
diff -up openssh/monitor.c.gsskex openssh/monitor.c
f5835d
--- openssh/monitor.c.gsskex	2018-08-22 11:47:33.263216069 +0200
f5835d
+++ openssh/monitor.c	2018-08-22 13:22:19.589095240 +0200
f5835d
@@ -146,6 +146,8 @@ int mm_answer_gss_setup_ctx(int, struct
f5835d
 int mm_answer_gss_accept_ctx(int, struct sshbuf *);
f5835d
 int mm_answer_gss_userok(int, struct sshbuf *);
f5835d
 int mm_answer_gss_checkmic(int, struct sshbuf *);
f5835d
+int mm_answer_gss_sign(int, struct sshbuf *);
f5835d
+int mm_answer_gss_updatecreds(int, struct sshbuf *);
f5835d
 #endif
f5835d
 
f5835d
 #ifdef SSH_AUDIT_EVENTS
f5835d
@@ -219,11 +221,18 @@ struct mon_table mon_dispatch_proto20[]
f5835d
     {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
f5835d
     {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
f5835d
     {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
f5835d
+    {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
f5835d
 #endif
f5835d
     {0, 0, NULL}
f5835d
 };
f5835d
 
f5835d
 struct mon_table mon_dispatch_postauth20[] = {
f5835d
+#ifdef GSSAPI
f5835d
+    {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
f5835d
+    {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
f5835d
+    {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
f5835d
+    {MONITOR_REQ_GSSUPCREDS, 0, mm_answer_gss_updatecreds},
f5835d
+#endif
f5835d
 #ifdef WITH_OPENSSL
f5835d
     {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
f5835d
 #endif
f5835d
@@ -293,6 +302,10 @@ monitor_child_preauth(Authctxt *_authctx
f5835d
 	/* Permit requests for moduli and signatures */
f5835d
 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
f5835d
 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
f5835d
+#ifdef GSSAPI
f5835d
+	/* and for the GSSAPI key exchange */
f5835d
+	monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
f5835d
+#endif
f5835d
 
f5835d
 	/* The first few requests do not require asynchronous access */
f5835d
 	while (!authenticated) {
f5835d
@@ -405,6 +418,10 @@ monitor_child_postauth(struct monitor *p
f5835d
 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
f5835d
 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
f5835d
 	monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
f5835d
+#ifdef GSSAPI
f5835d
+	/* and for the GSSAPI key exchange */
f5835d
+	monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
f5835d
+#endif		
f5835d
 
f5835d
 	if (auth_opts->permit_pty_flag) {
f5835d
 		monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
f5835d
@@ -1695,6 +1712,13 @@ monitor_apply_keystate(struct monitor *p
f5835d
 # endif
f5835d
 #endif /* WITH_OPENSSL */
f5835d
 		kex->kex[KEX_C25519_SHA256] = kexc25519_server;
f5835d
+#ifdef GSSAPI
f5835d
+	if (options.gss_keyex) {
f5835d
+		kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
f5835d
+		kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
f5835d
+		kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
f5835d
+	}
f5835d
+#endif
f5835d
 		kex->load_host_public_key=&get_hostkey_public_by_type;
f5835d
 		kex->load_host_private_key=&get_hostkey_private_by_type;
f5835d
 		kex->host_key_index=&get_hostkey_index;
f5835d
@@ -1785,7 +1809,7 @@ mm_answer_gss_setup_ctx(int sock, struct
f5835d
 	u_char *p;
f5835d
 	int r;
f5835d
 
f5835d
-	if (!options.gss_authentication)
f5835d
+	if (!options.gss_authentication && !options.gss_keyex)
f5835d
 		fatal("%s: GSSAPI authentication not enabled", __func__);
f5835d
 
f5835d
 	if ((r = sshbuf_get_string(m, &p, &len)) != 0)
f5835d
@@ -1818,7 +1842,7 @@ mm_answer_gss_accept_ctx(int sock, struc
f5835d
 	OM_uint32 flags = 0; /* GSI needs this */
f5835d
 	int r;
f5835d
 
f5835d
-	if (!options.gss_authentication)
f5835d
+	if (!options.gss_authentication && !options.gss_keyex)
f5835d
 		fatal("%s: GSSAPI authentication not enabled", __func__);
f5835d
 
f5835d
 	if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
f5835d
@@ -1839,6 +1863,7 @@ mm_answer_gss_accept_ctx(int sock, struc
f5835d
 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
f5835d
 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
f5835d
 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
f5835d
+		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
f5835d
 	}
f5835d
 	return (0);
f5835d
 }
f5835d
@@ -1850,7 +1875,7 @@ mm_answer_gss_checkmic(int sock, struct
f5835d
 	OM_uint32 ret;
f5835d
 	int r;
f5835d
 
f5835d
-	if (!options.gss_authentication)
f5835d
+	if (!options.gss_authentication && !options.gss_keyex)
f5835d
 		fatal("%s: GSSAPI authentication not enabled", __func__);
f5835d
 
f5835d
 	if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
f5835d
@@ -1880,10 +1905,11 @@ mm_answer_gss_userok(int sock, struct ss
f5835d
 	int r, authenticated;
f5835d
 	const char *displayname;
f5835d
 
f5835d
-	if (!options.gss_authentication)
f5835d
+	if (!options.gss_authentication && !options.gss_keyex)
f5835d
 		fatal("%s: GSSAPI authentication not enabled", __func__);
f5835d
 
f5835d
-	authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
f5835d
+	authenticated = authctxt->valid &&
f5835d
+	    ssh_gssapi_userok(authctxt->user, authctxt->pw);
f5835d
 
f5835d
 	sshbuf_reset(m);
f5835d
 	if ((r = sshbuf_put_u32(m, authenticated)) != 0)
f5835d
@@ -1900,5 +1926,74 @@ mm_answer_gss_userok(int sock, struct ss
f5835d
 	/* Monitor loop will terminate if authenticated */
f5835d
 	return (authenticated);
f5835d
 }
f5835d
+
f5835d
+int 
f5835d
+mm_answer_gss_sign(int socket, struct sshbuf *m)
f5835d
+{
f5835d
+	gss_buffer_desc data;
f5835d
+	gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
f5835d
+	OM_uint32 major, minor;
f5835d
+	int r;
f5835d
+
f5835d
+	if (!options.gss_authentication && !options.gss_keyex)
f5835d
+		fatal("In GSSAPI monitor when GSSAPI is disabled");
f5835d
+
f5835d
+	if ((r = sshbuf_get_string(m, (u_char **)&data.value, &data.length)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+	if (data.length != 20) 
f5835d
+		fatal("%s: data length incorrect: %d", __func__, 
f5835d
+		    (int) data.length);
f5835d
+
f5835d
+	/* Save the session ID on the first time around */
f5835d
+	if (session_id2_len == 0) {
f5835d
+		session_id2_len = data.length;
f5835d
+		session_id2 = xmalloc(session_id2_len);
f5835d
+		memcpy(session_id2, data.value, session_id2_len);
f5835d
+	}
f5835d
+	major = ssh_gssapi_sign(gsscontext, &data, &hash);
f5835d
+
f5835d
+	free(data.value);
f5835d
+
f5835d
+	sshbuf_reset(m);
f5835d
+	if ((r = sshbuf_put_u32(m, major)) != 0 ||
f5835d
+	    (r = sshbuf_put_string(m, hash.value, hash.length)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
f5835d
+
f5835d
+	gss_release_buffer(&minor, &hash);
f5835d
+
f5835d
+	/* Turn on getpwnam permissions */
f5835d
+	monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
f5835d
+	
f5835d
+	/* And credential updating, for when rekeying */
f5835d
+	monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
f5835d
+
f5835d
+	return (0);
f5835d
+}
f5835d
+
f5835d
+int
f5835d
+mm_answer_gss_updatecreds(int socket, struct sshbuf *m) {
f5835d
+	ssh_gssapi_ccache store;
f5835d
+	int ok, r;
f5835d
+
f5835d
+	if ((r = sshbuf_get_cstring(m, &store.envvar, NULL)) != 0 ||
f5835d
+	    (r = sshbuf_get_cstring(m, &store.envval, NULL)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	ok = ssh_gssapi_update_creds(&store);
f5835d
+
f5835d
+	free(store.envvar);
f5835d
+	free(store.envval);
f5835d
+
f5835d
+	sshbuf_reset(m);
f5835d
+	if ((r = sshbuf_put_u32(m, ok)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
f5835d
+
f5835d
+	return(0);
f5835d
+}
f5835d
+
f5835d
 #endif /* GSSAPI */
f5835d
 
f5835d
diff -up openssh/monitor.h.gsskex openssh/monitor.h
f5835d
--- openssh/monitor.h.gsskex	2018-08-22 11:47:33.263216069 +0200
f5835d
+++ openssh/monitor.h	2018-08-22 11:47:33.313216473 +0200
f5835d
@@ -58,6 +58,8 @@ enum monitor_reqtype {
f5835d
 #ifdef WITH_SELINUX
f5835d
 	MONITOR_REQ_AUTHROLE = 80,
f5835d
 #endif
f5835d
+	MONITOR_REQ_GSSSIGN = 82, MONITOR_ANS_GSSSIGN = 83,
f5835d
+	MONITOR_REQ_GSSUPCREDS = 84, MONITOR_ANS_GSSUPCREDS = 85,
f5835d
 
f5835d
 	MONITOR_REQ_PAM_START = 100,
f5835d
 	MONITOR_REQ_PAM_ACCOUNT = 102, MONITOR_ANS_PAM_ACCOUNT = 103,
f5835d
diff -up openssh/monitor_wrap.c.gsskex openssh/monitor_wrap.c
f5835d
--- openssh/monitor_wrap.c.gsskex	2018-08-22 11:47:33.313216473 +0200
f5835d
+++ openssh/monitor_wrap.c	2018-08-22 13:27:38.665669643 +0200
f5835d
@@ -1004,7 +1004,7 @@ mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss
f5835d
 }
f5835d
 
f5835d
 int
f5835d
-mm_ssh_gssapi_userok(char *user)
f5835d
+mm_ssh_gssapi_userok(char *user, struct passwd *pw)
f5835d
 {
f5835d
 	struct sshbuf *m;
f5835d
 	int r, authenticated = 0;
f5835d
@@ -1023,4 +1023,52 @@ mm_ssh_gssapi_userok(char *user)
f5835d
 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
f5835d
 	return (authenticated);
f5835d
 }
f5835d
+
f5835d
+OM_uint32
f5835d
+mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash)
f5835d
+{
f5835d
+	struct sshbuf *m;
f5835d
+	OM_uint32 major;
f5835d
+	int r;
f5835d
+
f5835d
+	if ((m = sshbuf_new()) == NULL)
f5835d
+		fatal("%s: sshbuf_new failed", __func__);
f5835d
+	if ((r = sshbuf_put_string(m, data->value, data->length)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, m);
f5835d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, m);
f5835d
+
f5835d
+	if ((r = sshbuf_get_u32(m, &major)) != 0 ||
f5835d
+	    (r = sshbuf_get_string(m, (u_char **)&hash->value, &hash->length)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	sshbuf_free(m);
f5835d
+
f5835d
+	return(major);
f5835d
+}
f5835d
+
f5835d
+int
f5835d
+mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
f5835d
+{
f5835d
+	struct sshbuf *m;
f5835d
+	int ok, r;
f5835d
+
f5835d
+	if ((m = sshbuf_new()) == NULL)
f5835d
+		fatal("%s: sshbuf_new failed", __func__);
f5835d
+
f5835d
+	if ((r = sshbuf_put_cstring(m, store->envvar ? store->envvar : "")) != 0 ||
f5835d
+	    (r = sshbuf_put_cstring(m, store->envval ? store->envval : "")) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUPCREDS, m);
f5835d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUPCREDS, m);
f5835d
+
f5835d
+	if ((r = sshbuf_get_u32(m, &ok)) != 0)
f5835d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
f5835d
+
f5835d
+	sshbuf_free(m);
f5835d
+
f5835d
+	return (ok);
f5835d
+}
f5835d
 #endif /* GSSAPI */
f5835d
diff -up openssh/monitor_wrap.h.gsskex openssh/monitor_wrap.h
f5835d
--- openssh/monitor_wrap.h.gsskex	2018-08-22 11:47:33.263216069 +0200
f5835d
+++ openssh/monitor_wrap.h	2018-08-22 11:47:33.313216473 +0200
f5835d
@@ -63,8 +63,10 @@ int mm_sshkey_verify(const struct sshkey
f5835d
 OM_uint32 mm_ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
f5835d
 OM_uint32 mm_ssh_gssapi_accept_ctx(Gssctxt *,
f5835d
    gss_buffer_desc *, gss_buffer_desc *, OM_uint32 *);
f5835d
-int mm_ssh_gssapi_userok(char *user);
f5835d
+int mm_ssh_gssapi_userok(char *user, struct passwd *);
f5835d
 OM_uint32 mm_ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
f5835d
+OM_uint32 mm_ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
f5835d
+int mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *);
f5835d
 #endif
f5835d
 
f5835d
 #ifdef USE_PAM
f5835d
diff -up openssh/readconf.c.gsskex openssh/readconf.c
f5835d
--- openssh/readconf.c.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/readconf.c	2018-08-22 13:28:17.487982869 +0200
f5835d
@@ -161,6 +161,8 @@ typedef enum {
f5835d
 	oClearAllForwardings, oNoHostAuthenticationForLocalhost,
f5835d
 	oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
f5835d
 	oAddressFamily, oGssAuthentication, oGssDelegateCreds,
f5835d
+	oGssTrustDns, oGssKeyEx, oGssClientIdentity, oGssRenewalRekey,
f5835d
+	oGssServerIdentity, 
f5835d
 	oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
f5835d
 	oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
f5835d
 	oHashKnownHosts,
f5835d
@@ -201,10 +203,19 @@ static struct {
f5835d
 	/* Sometimes-unsupported options */
f5835d
 #if defined(GSSAPI)
f5835d
 	{ "gssapiauthentication", oGssAuthentication },
f5835d
+	{ "gssapikeyexchange", oGssKeyEx },
f5835d
 	{ "gssapidelegatecredentials", oGssDelegateCreds },
f5835d
+	{ "gssapitrustdns", oGssTrustDns },
f5835d
+	{ "gssapiclientidentity", oGssClientIdentity },
f5835d
+	{ "gssapiserveridentity", oGssServerIdentity },
f5835d
+	{ "gssapirenewalforcesrekey", oGssRenewalRekey },
f5835d
 # else
f5835d
 	{ "gssapiauthentication", oUnsupported },
f5835d
+	{ "gssapikeyexchange", oUnsupported },
f5835d
 	{ "gssapidelegatecredentials", oUnsupported },
f5835d
+	{ "gssapitrustdns", oUnsupported },
f5835d
+	{ "gssapiclientidentity", oUnsupported },
f5835d
+	{ "gssapirenewalforcesrekey", oUnsupported },
f5835d
 #endif
f5835d
 #ifdef ENABLE_PKCS11
f5835d
 	{ "smartcarddevice", oPKCS11Provider },
f5835d
@@ -973,10 +984,30 @@ parse_time:
f5835d
 		intptr = &options->gss_authentication;
f5835d
 		goto parse_flag;
f5835d
 
f5835d
+	case oGssKeyEx:
f5835d
+		intptr = &options->gss_keyex;
f5835d
+		goto parse_flag;
f5835d
+
f5835d
 	case oGssDelegateCreds:
f5835d
 		intptr = &options->gss_deleg_creds;
f5835d
 		goto parse_flag;
f5835d
 
f5835d
+	case oGssTrustDns:
f5835d
+		intptr = &options->gss_trust_dns;
f5835d
+		goto parse_flag;
f5835d
+
f5835d
+	case oGssClientIdentity:
f5835d
+		charptr = &options->gss_client_identity;
f5835d
+		goto parse_string;
f5835d
+
f5835d
+	case oGssServerIdentity:
f5835d
+		charptr = &options->gss_server_identity;
f5835d
+		goto parse_string;
f5835d
+
f5835d
+	case oGssRenewalRekey:
f5835d
+		intptr = &options->gss_renewal_rekey;
f5835d
+		goto parse_flag;
f5835d
+
f5835d
 	case oBatchMode:
f5835d
 		intptr = &options->batch_mode;
f5835d
 		goto parse_flag;
f5835d
@@ -1817,7 +1848,12 @@ initialize_options(Options * options)
f5835d
 	options->pubkey_authentication = -1;
f5835d
 	options->challenge_response_authentication = -1;
f5835d
 	options->gss_authentication = -1;
f5835d
+	options->gss_keyex = -1;
f5835d
 	options->gss_deleg_creds = -1;
f5835d
+	options->gss_trust_dns = -1;
f5835d
+	options->gss_renewal_rekey = -1;
f5835d
+	options->gss_client_identity = NULL;
f5835d
+	options->gss_server_identity = NULL;
f5835d
 	options->password_authentication = -1;
f5835d
 	options->kbd_interactive_authentication = -1;
f5835d
 	options->kbd_interactive_devices = NULL;
f5835d
@@ -1962,8 +1998,14 @@ fill_default_options(Options * options)
f5835d
 		options->challenge_response_authentication = 1;
f5835d
 	if (options->gss_authentication == -1)
f5835d
 		options->gss_authentication = 0;
f5835d
+	if (options->gss_keyex == -1)
f5835d
+		options->gss_keyex = 0;
f5835d
 	if (options->gss_deleg_creds == -1)
f5835d
 		options->gss_deleg_creds = 0;
f5835d
+	if (options->gss_trust_dns == -1)
f5835d
+		options->gss_trust_dns = 0;
f5835d
+	if (options->gss_renewal_rekey == -1)
f5835d
+		options->gss_renewal_rekey = 0;
f5835d
 	if (options->password_authentication == -1)
f5835d
 		options->password_authentication = 1;
f5835d
 	if (options->kbd_interactive_authentication == -1)
f5835d
@@ -2603,7 +2645,12 @@ dump_client_config(Options *o, const cha
f5835d
 	dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
f5835d
 #ifdef GSSAPI
f5835d
 	dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
f5835d
+	dump_cfg_fmtint(oGssKeyEx, o->gss_keyex);
f5835d
 	dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
f5835d
+	dump_cfg_fmtint(oGssTrustDns, o->gss_trust_dns);
f5835d
+	dump_cfg_string(oGssClientIdentity, o->gss_client_identity);
f5835d
+	dump_cfg_string(oGssServerIdentity, o->gss_client_identity);
f5835d
+	dump_cfg_fmtint(oGssRenewalRekey, o->gss_renewal_rekey);
f5835d
 #endif /* GSSAPI */
f5835d
 	dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
f5835d
 	dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
f5835d
diff -up openssh-7.9p1/readconf.h.gsskex openssh-7.9p1/readconf.h
f5835d
--- openssh-7.9p1/readconf.h.gsskex	2018-10-17 02:01:20.000000000 +0200
f5835d
+++ openssh-7.9p1/readconf.h	2018-11-14 09:14:06.277379857 +0100
f5835d
@@ -40,7 +40,12 @@ typedef struct {
f5835d
 	int     challenge_response_authentication;
f5835d
 					/* Try S/Key or TIS, authentication. */
f5835d
 	int     gss_authentication;	/* Try GSS authentication */
f5835d
+	int     gss_keyex;		/* Try GSS key exchange */
f5835d
 	int     gss_deleg_creds;	/* Delegate GSS credentials */
f5835d
+	int	gss_trust_dns;		/* Trust DNS for GSS canonicalization */
f5835d
+	int	gss_renewal_rekey;	/* Credential renewal forces rekey */
f5835d
+	char    *gss_client_identity;   /* Principal to initiate GSSAPI with */
f5835d
+	char    *gss_server_identity;   /* GSSAPI target principal */
f5835d
 	int     password_authentication;	/* Try password
f5835d
 						 * authentication. */
f5835d
 	int     kbd_interactive_authentication; /* Try keyboard-interactive auth. */
f5835d
diff -up openssh/regress/cert-hostkey.sh.gsskex openssh/regress/cert-hostkey.sh
f5835d
--- openssh/regress/cert-hostkey.sh.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/regress/cert-hostkey.sh	2018-08-22 11:47:33.314216481 +0200
f5835d
@@ -66,7 +66,7 @@ touch $OBJ/host_revoked_plain
f5835d
 touch $OBJ/host_revoked_cert
f5835d
 cat $OBJ/host_ca_key.pub $OBJ/host_ca_key2.pub > $OBJ/host_revoked_ca
f5835d
 
f5835d
-PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
f5835d
+PLAIN_TYPES=`$SSH -Q key-plain | grep -v null | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
f5835d
 
f5835d
 if echo "$PLAIN_TYPES" | grep '^rsa$' >/dev/null 2>&1 ; then
f5835d
 	PLAIN_TYPES="$PLAIN_TYPES rsa-sha2-256 rsa-sha2-512"
f5835d
diff -up openssh/regress/cert-userkey.sh.gsskex openssh/regress/cert-userkey.sh
f5835d
--- openssh/regress/cert-userkey.sh.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/regress/cert-userkey.sh	2018-08-22 11:47:33.314216481 +0200
f5835d
@@ -7,7 +7,7 @@ rm -f $OBJ/authorized_keys_$USER $OBJ/us
f5835d
 cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
f5835d
 cp $OBJ/ssh_proxy $OBJ/ssh_proxy_bak
f5835d
 
f5835d
-PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/;s/^ssh-//'`
f5835d
+PLAIN_TYPES=`$SSH -Q key-plain | grep -v null | sed 's/^ssh-dss/ssh-dsa/;s/^ssh-//'`
f5835d
 EXTRA_TYPES=""
f5835d
 
f5835d
 if echo "$PLAIN_TYPES" | grep '^rsa$' >/dev/null 2>&1 ; then
f5835d
diff -up openssh/regress/kextype.sh.gsskex openssh/regress/kextype.sh
f5835d
--- openssh/regress/kextype.sh.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/regress/kextype.sh	2018-08-22 11:47:33.315216489 +0200
f5835d
@@ -14,6 +14,9 @@ echo "KexAlgorithms=$KEXOPT" >> $OBJ/ssh
f5835d
 
f5835d
 tries="1 2 3 4"
f5835d
 for k in `${SSH} -Q kex`; do
f5835d
+	if [ $k = "gss-gex-sha1-" -o $k = "gss-group1-sha1-" -o $k = "gss-group14-sha1-" ]; then
f5835d
+		continue
f5835d
+	fi
f5835d
 	verbose "kex $k"
f5835d
 	for i in $tries; do
f5835d
 		${SSH} -F $OBJ/ssh_proxy -o KexAlgorithms=$k x true
f5835d
diff -up openssh/regress/rekey.sh.gsskex openssh/regress/rekey.sh
f5835d
--- openssh/regress/rekey.sh.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/regress/rekey.sh	2018-08-22 11:47:33.315216489 +0200
f5835d
@@ -38,6 +38,9 @@ increase_datafile_size 300
f5835d
 
f5835d
 opts=""
f5835d
 for i in `${SSH} -Q kex`; do
f5835d
+	if [ $i = "gss-gex-sha1-" -o $i = "gss-group1-sha1-" -o $i = "gss-group14-sha1-" ]; then
f5835d
+		continue
f5835d
+	fi
f5835d
 	opts="$opts KexAlgorithms=$i"
f5835d
 done
f5835d
 for i in `${SSH} -Q cipher`; do
f5835d
@@ -56,6 +59,9 @@ done
f5835d
 if ${SSH} -Q cipher-auth | grep '^.*$' >/dev/null 2>&1 ; then
f5835d
   for c in `${SSH} -Q cipher-auth`; do
f5835d
     for kex in `${SSH} -Q kex`; do
f5835d
+	if [ $kex = "gss-gex-sha1-" -o $kex = "gss-group1-sha1-" -o $kex = "gss-group14-sha1-" ]; then
f5835d
+		continue
f5835d
+	fi
f5835d
 	verbose "client rekey $c $kex"
f5835d
 	ssh_data_rekeying "KexAlgorithms=$kex" -oRekeyLimit=256k -oCiphers=$c
f5835d
     done
f5835d
diff -up openssh/servconf.c.gsskex openssh/servconf.c
f5835d
--- openssh/servconf.c.gsskex	2018-08-22 11:47:33.296216335 +0200
f5835d
+++ openssh/servconf.c	2018-08-22 13:28:41.905179879 +0200
f5835d
@@ -124,8 +124,10 @@ initialize_server_options(ServerOptions
f5835d
 	options->kerberos_ticket_cleanup = -1;
f5835d
 	options->kerberos_get_afs_token = -1;
f5835d
 	options->gss_authentication=-1;
f5835d
+	options->gss_keyex = -1;
f5835d
 	options->gss_cleanup_creds = -1;
f5835d
 	options->gss_strict_acceptor = -1;
f5835d
+	options->gss_store_rekey = -1;
f5835d
 	options->password_authentication = -1;
f5835d
 	options->kbd_interactive_authentication = -1;
f5835d
 	options->challenge_response_authentication = -1;
f5835d
@@ -334,10 +336,14 @@ fill_default_server_options(ServerOption
f5835d
 		options->kerberos_get_afs_token = 0;
f5835d
 	if (options->gss_authentication == -1)
f5835d
 		options->gss_authentication = 0;
f5835d
+	if (options->gss_keyex == -1)
f5835d
+		options->gss_keyex = 0;
f5835d
 	if (options->gss_cleanup_creds == -1)
f5835d
 		options->gss_cleanup_creds = 1;
f5835d
 	if (options->gss_strict_acceptor == -1)
f5835d
 		options->gss_strict_acceptor = 1;
f5835d
+	if (options->gss_store_rekey == -1)
f5835d
+		options->gss_store_rekey = 0;
f5835d
 	if (options->password_authentication == -1)
f5835d
 		options->password_authentication = 1;
f5835d
 	if (options->kbd_interactive_authentication == -1)
f5835d
@@ -484,7 +490,7 @@ typedef enum {
f5835d
 	sHostKeyAlgorithms,
f5835d
 	sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
f5835d
 	sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
f5835d
-	sAcceptEnv, sSetEnv, sPermitTunnel,
f5835d
+	sGssKeyEx, sGssStoreRekey, sAcceptEnv, sSetEnv, sPermitTunnel,
f5835d
 	sMatch, sPermitOpen, sPermitListen, sForceCommand, sChrootDirectory,
f5835d
 	sUsePrivilegeSeparation, sAllowAgentForwarding,
f5835d
 	sHostCertificate,
f5835d
@@ -559,11 +565,17 @@ static struct {
f5835d
 	{ "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
f5835d
 	{ "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
f5835d
 	{ "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
f5835d
+	{ "gssapikeyexchange", sGssKeyEx, SSHCFG_GLOBAL },
f5835d
+	{ "gssapistorecredentialsonrekey", sGssStoreRekey, SSHCFG_GLOBAL },
f5835d
 #else
f5835d
 	{ "gssapiauthentication", sUnsupported, SSHCFG_ALL },
f5835d
 	{ "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
f5835d
 	{ "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
f5835d
+	{ "gssapikeyexchange", sUnsupported, SSHCFG_GLOBAL },
f5835d
+	{ "gssapistorecredentialsonrekey", sUnsupported, SSHCFG_GLOBAL },
f5835d
 #endif
f5835d
+	{ "gssusesessionccache", sUnsupported, SSHCFG_GLOBAL },
f5835d
+	{ "gssapiusesessioncredcache", sUnsupported, SSHCFG_GLOBAL },
f5835d
 	{ "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
f5835d
 	{ "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
f5835d
 	{ "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
f5835d
@@ -1463,6 +1475,10 @@ process_server_config_line(ServerOptions
f5835d
 		intptr = &options->gss_authentication;
f5835d
 		goto parse_flag;
f5835d
 
f5835d
+	case sGssKeyEx:
f5835d
+		intptr = &options->gss_keyex;
f5835d
+		goto parse_flag;
f5835d
+
f5835d
 	case sGssCleanupCreds:
f5835d
 		intptr = &options->gss_cleanup_creds;
f5835d
 		goto parse_flag;
f5835d
@@ -1471,6 +1487,10 @@ process_server_config_line(ServerOptions
f5835d
 		intptr = &options->gss_strict_acceptor;
f5835d
 		goto parse_flag;
f5835d
 
f5835d
+	case sGssStoreRekey:
f5835d
+		intptr = &options->gss_store_rekey;
f5835d
+		goto parse_flag;
f5835d
+
f5835d
 	case sPasswordAuthentication:
f5835d
 		intptr = &options->password_authentication;
f5835d
 		goto parse_flag;
f5835d
@@ -2560,6 +2580,9 @@ dump_config(ServerOptions *o)
f5835d
 #ifdef GSSAPI
f5835d
 	dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
f5835d
 	dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
f5835d
+	dump_cfg_fmtint(sGssKeyEx, o->gss_keyex);
f5835d
+	dump_cfg_fmtint(sGssStrictAcceptor, o->gss_strict_acceptor);
f5835d
+	dump_cfg_fmtint(sGssStoreRekey, o->gss_store_rekey);
f5835d
 #endif
f5835d
 	dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
f5835d
 	dump_cfg_fmtint(sKbdInteractiveAuthentication,
f5835d
diff -up openssh/servconf.h.gsskex openssh/servconf.h
f5835d
--- openssh/servconf.h.gsskex	2018-08-22 11:47:33.296216335 +0200
f5835d
+++ openssh/servconf.h	2018-08-22 11:47:33.316216497 +0200
f5835d
@@ -124,8 +124,10 @@ typedef struct {
f5835d
 	int     kerberos_get_afs_token;		/* If true, try to get AFS token if
f5835d
 						 * authenticated with Kerberos. */
f5835d
 	int     gss_authentication;	/* If true, permit GSSAPI authentication */
f5835d
+	int     gss_keyex;		/* If true, permit GSSAPI key exchange */
f5835d
 	int     gss_cleanup_creds;	/* If true, destroy cred cache on logout */
f5835d
 	int     gss_strict_acceptor;	/* If true, restrict the GSSAPI acceptor name */
f5835d
+	int 	gss_store_rekey;
f5835d
 	int     password_authentication;	/* If true, permit password
f5835d
 						 * authentication. */
f5835d
 	int     kbd_interactive_authentication;	/* If true, permit */
f5835d
diff -up openssh/ssh_config.5.gsskex openssh/ssh_config.5
f5835d
--- openssh/ssh_config.5.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/ssh_config.5	2018-08-22 11:47:33.316216497 +0200
f5835d
@@ -718,10 +718,40 @@ The default is
f5835d
 Specifies whether user authentication based on GSSAPI is allowed.
f5835d
 The default is
f5835d
 .Cm no .
f5835d
+.It Cm GSSAPIClientIdentity
f5835d
+If set, specifies the GSSAPI client identity that ssh should use when 
f5835d
+connecting to the server. The default is unset, which means that the default 
f5835d
+identity will be used.
f5835d
 .It Cm GSSAPIDelegateCredentials
f5835d
 Forward (delegate) credentials to the server.
f5835d
 The default is
f5835d
 .Cm no .
f5835d
+.It Cm GSSAPIKeyExchange
f5835d
+Specifies whether key exchange based on GSSAPI may be used. When using
f5835d
+GSSAPI key exchange the server need not have a host key.
f5835d
+The default is
f5835d
+.Dq no .
f5835d
+.It Cm GSSAPIRenewalForcesRekey
f5835d
+If set to 
f5835d
+.Dq yes
f5835d
+then renewal of the client's GSSAPI credentials will force the rekeying of the
f5835d
+ssh connection. With a compatible server, this can delegate the renewed 
f5835d
+credentials to a session on the server.
f5835d
+The default is
f5835d
+.Dq no .
f5835d
+.It Cm GSSAPIServerIdentity
f5835d
+If set, specifies the GSSAPI server identity that ssh should expect when 
f5835d
+connecting to the server. The default is unset, which means that the
f5835d
+expected GSSAPI server identity will be determined from the target
f5835d
+hostname.
f5835d
+.It Cm GSSAPITrustDns
f5835d
+Set to 
f5835d
+.Dq yes to indicate that the DNS is trusted to securely canonicalize
f5835d
+the name of the host being connected to. If 
f5835d
+.Dq no, the hostname entered on the
f5835d
+command line will be passed untouched to the GSSAPI library.
f5835d
+The default is
f5835d
+.Dq no .
f5835d
 .It Cm HashKnownHosts
f5835d
 Indicates that
f5835d
 .Xr ssh 1
f5835d
diff -up openssh/ssh_config.gsskex openssh/ssh_config
f5835d
--- openssh/ssh_config.gsskex	2018-08-22 11:47:33.289216279 +0200
f5835d
+++ openssh/ssh_config	2018-08-22 11:47:33.316216497 +0200
f5835d
@@ -24,6 +24,8 @@
f5835d
 #   HostbasedAuthentication no
f5835d
 #   GSSAPIAuthentication no
f5835d
 #   GSSAPIDelegateCredentials no
f5835d
+#   GSSAPIKeyExchange no
f5835d
+#   GSSAPITrustDNS no
f5835d
 #   BatchMode no
f5835d
 #   CheckHostIP yes
f5835d
 #   AddressFamily any
f5835d
diff -up openssh/sshconnect2.c.gsskex openssh/sshconnect2.c
f5835d
--- openssh/sshconnect2.c.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/sshconnect2.c	2018-08-22 13:33:01.674275795 +0200
f5835d
@@ -82,6 +82,124 @@ extern char *client_version_string;
f5835d
 extern char *server_version_string;
f5835d
 extern Options options;
f5835d
 
f5835d
+/* XXX from auth.h -- refactoring move these useful functions away of client context*/
f5835d
+
f5835d
+/*
f5835d
+ * Returns the remote DNS hostname as a string. The returned string must not
f5835d
+ * be freed. NB. this will usually trigger a DNS query the first time it is
f5835d
+ * called.
f5835d
+ * This function does additional checks on the hostname to mitigate some
f5835d
+ * attacks on legacy rhosts-style authentication.
f5835d
+ * XXX is RhostsRSAAuthentication vulnerable to these?
f5835d
+ * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
f5835d
+ */
f5835d
+
f5835d
+static char *
f5835d
+remote_hostname(struct ssh *ssh)
f5835d
+{
f5835d
+	struct sockaddr_storage from;
f5835d
+	socklen_t fromlen;
f5835d
+	struct addrinfo hints, *ai, *aitop;
f5835d
+	char name[NI_MAXHOST], ntop2[NI_MAXHOST];
f5835d
+	const char *ntop = ssh_remote_ipaddr(ssh);
f5835d
+
f5835d
+	/* Get IP address of client. */
f5835d
+	fromlen = sizeof(from);
f5835d
+	memset(&from, 0, sizeof(from));
f5835d
+	if (getpeername(ssh_packet_get_connection_in(ssh),
f5835d
+	    (struct sockaddr *)&from, &fromlen) < 0) {
f5835d
+		debug("getpeername failed: %.100s", strerror(errno));
f5835d
+		return strdup(ntop);
f5835d
+	}
f5835d
+
f5835d
+	ipv64_normalise_mapped(&from, &fromlen);
f5835d
+	if (from.ss_family == AF_INET6)
f5835d
+		fromlen = sizeof(struct sockaddr_in6);
f5835d
+
f5835d
+	debug3("Trying to reverse map address %.100s.", ntop);
f5835d
+	/* Map the IP address to a host name. */
f5835d
+	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
f5835d
+	    NULL, 0, NI_NAMEREQD) != 0) {
f5835d
+		/* Host name not found.  Use ip address. */
f5835d
+		return strdup(ntop);
f5835d
+	}
f5835d
+
f5835d
+	/*
f5835d
+	 * if reverse lookup result looks like a numeric hostname,
f5835d
+	 * someone is trying to trick us by PTR record like following:
f5835d
+	 *	1.1.1.10.in-addr.arpa.	IN PTR	2.3.4.5
f5835d
+	 */
f5835d
+	memset(&hints, 0, sizeof(hints));
f5835d
+	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
f5835d
+	hints.ai_flags = AI_NUMERICHOST;
f5835d
+	if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
f5835d
+		logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
f5835d
+		    name, ntop);
f5835d
+		freeaddrinfo(ai);
f5835d
+		return strdup(ntop);
f5835d
+	}
f5835d
+
f5835d
+	/* Names are stored in lowercase. */
f5835d
+	lowercase(name);
f5835d
+
f5835d
+	/*
f5835d
+	 * Map it back to an IP address and check that the given
f5835d
+	 * address actually is an address of this host.  This is
f5835d
+	 * necessary because anyone with access to a name server can
f5835d
+	 * define arbitrary names for an IP address. Mapping from
f5835d
+	 * name to IP address can be trusted better (but can still be
f5835d
+	 * fooled if the intruder has access to the name server of
f5835d
+	 * the domain).
f5835d
+	 */
f5835d
+	memset(&hints, 0, sizeof(hints));
f5835d
+	hints.ai_family = from.ss_family;
f5835d
+	hints.ai_socktype = SOCK_STREAM;
f5835d
+	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
f5835d
+		logit("reverse mapping checking getaddrinfo for %.700s "
f5835d
+		    "[%s] failed.", name, ntop);
f5835d
+		return strdup(ntop);
f5835d
+	}
f5835d
+	/* Look for the address from the list of addresses. */
f5835d
+	for (ai = aitop; ai; ai = ai->ai_next) {
f5835d
+		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
f5835d
+		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
f5835d
+		    (strcmp(ntop, ntop2) == 0))
f5835d
+				break;
f5835d
+	}
f5835d
+	freeaddrinfo(aitop);
f5835d
+	/* If we reached the end of the list, the address was not there. */
f5835d
+	if (ai == NULL) {
f5835d
+		/* Address not found for the host name. */
f5835d
+		logit("Address %.100s maps to %.600s, but this does not "
f5835d
+		    "map back to the address.", ntop, name);
f5835d
+		return strdup(ntop);
f5835d
+	}
f5835d
+	return strdup(name);
f5835d
+}
f5835d
+
f5835d
+/*
f5835d
+ * Return the canonical name of the host in the other side of the current
f5835d
+ * connection.  The host name is cached, so it is efficient to call this
f5835d
+ * several times.
f5835d
+ */
f5835d
+
f5835d
+const char *
f5835d
+get_canonical_hostname(struct ssh *ssh, int use_dns)
f5835d
+{
f5835d
+	static char *dnsname;
f5835d
+
f5835d
+	if (!use_dns)
f5835d
+		return ssh_remote_ipaddr(ssh);
f5835d
+	else if (dnsname != NULL)
f5835d
+		return dnsname;
f5835d
+	else {
f5835d
+		dnsname = remote_hostname(ssh);
f5835d
+		return dnsname;
f5835d
+	}
f5835d
+}
f5835d
+
f5835d
+
f5835d
+
f5835d
 /*
f5835d
  * SSH2 key exchange
f5835d
  */
f5835d
@@ -162,9 +280,36 @@ ssh_kex2(char *host, struct sockaddr *ho
f5835d
 	struct kex *kex;
f5835d
 	int r;
f5835d
 
f5835d
+#ifdef GSSAPI
f5835d
+	char *orig = NULL, *gss = NULL;
f5835d
+	char *gss_host = NULL;
f5835d
+#endif
f5835d
+
f5835d
 	xxx_host = host;
f5835d
 	xxx_hostaddr = hostaddr;
f5835d
 
f5835d
+#ifdef GSSAPI
f5835d
+	if (options.gss_keyex) {
f5835d
+		/* Add the GSSAPI mechanisms currently supported on this 
f5835d
+		 * client to the key exchange algorithm proposal */
f5835d
+		orig = options.kex_algorithms;
f5835d
+
f5835d
+		if (options.gss_server_identity)
f5835d
+			gss_host = options.gss_server_identity;
f5835d
+		else if (options.gss_trust_dns)
f5835d
+			gss_host = (char *)get_canonical_hostname(active_state, 1);
f5835d
+		else
f5835d
+			gss_host = host;
f5835d
+
f5835d
+		gss = ssh_gssapi_client_mechanisms(gss_host, options.gss_client_identity);
f5835d
+		if (gss) {
f5835d
+			debug("Offering GSSAPI proposal: %s", gss);
f5835d
+			xasprintf(&options.kex_algorithms,
f5835d
+			    "%s,%s", gss, orig);
f5835d
+		}
f5835d
+	}
f5835d
+#endif
f5835d
+
f5835d
 	if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
f5835d
 		fatal("%s: kex_names_cat", __func__);
f5835d
 	myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
f5835d
@@ -194,6 +339,16 @@ ssh_kex2(char *host, struct sockaddr *ho
f5835d
 		    order_hostkeyalgs(host, hostaddr, port));
f5835d
 	}
f5835d
 
f5835d
+#ifdef GSSAPI
f5835d
+	/* If we've got GSSAPI algorithms, then we also support the
f5835d
+	 * 'null' hostkey, as a last resort */
f5835d
+	if (options.gss_keyex && gss) {
f5835d
+		orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
f5835d
+		xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
f5835d
+		    "%s,null", orig);
f5835d
+	}
f5835d
+#endif
f5835d
+
f5835d
 	if (options.rekey_limit || options.rekey_interval)
f5835d
 		packet_set_rekey_limits(options.rekey_limit,
f5835d
 		    options.rekey_interval);
f5835d
@@ -214,16 +369,46 @@ ssh_kex2(char *host, struct sockaddr *ho
f5835d
 	kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
f5835d
 # endif
f5835d
 #endif
f5835d
+#ifdef GSSAPI
f5835d
+	if (options.gss_keyex) {
f5835d
+		kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
f5835d
+		kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
f5835d
+		kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
f5835d
+	}
f5835d
+#endif
f5835d
 	kex->kex[KEX_C25519_SHA256] = kexc25519_client;
f5835d
 	kex->client_version_string=client_version_string;
f5835d
 	kex->server_version_string=server_version_string;
f5835d
 	kex->verify_host_key=&verify_host_key_callback;
f5835d
 
f5835d
+#ifdef GSSAPI
f5835d
+	if (options.gss_keyex) {
f5835d
+		kex->gss_deleg_creds = options.gss_deleg_creds;
f5835d
+		kex->gss_trust_dns = options.gss_trust_dns;
f5835d
+		kex->gss_client = options.gss_client_identity;
f5835d
+		if (options.gss_server_identity) {
f5835d
+			kex->gss_host = options.gss_server_identity;
f5835d
+		} else {
f5835d
+			kex->gss_host = gss_host;
f5835d
+        }
f5835d
+	}
f5835d
+#endif
f5835d
+
f5835d
 	ssh_dispatch_run_fatal(active_state, DISPATCH_BLOCK, &kex->done);
f5835d
 
f5835d
 	/* remove ext-info from the KEX proposals for rekeying */
f5835d
 	myproposal[PROPOSAL_KEX_ALGS] =
f5835d
 	    compat_kex_proposal(options.kex_algorithms);
f5835d
+#ifdef GSSAPI
f5835d
+	/* repair myproposal after it was crumpled by the */
f5835d
+	/* ext-info removal above */
f5835d
+	if (gss) {
f5835d
+		orig = myproposal[PROPOSAL_KEX_ALGS];
f5835d
+		xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
f5835d
+		    "%s,%s", gss, orig);
f5835d
+		free(gss);
f5835d
+	}
f5835d
+#endif
f5835d
 	if ((r = kex_prop2buf(kex->my, myproposal)) != 0)
f5835d
 		fatal("kex_prop2buf: %s", ssh_err(r));
f5835d
 
f5835d
@@ -314,6 +499,7 @@ int	input_gssapi_token(int type, u_int32
f5835d
 int	input_gssapi_hash(int type, u_int32_t, struct ssh *);
f5835d
 int	input_gssapi_error(int, u_int32_t, struct ssh *);
f5835d
 int	input_gssapi_errtok(int, u_int32_t, struct ssh *);
f5835d
+int	userauth_gsskeyex(Authctxt *authctxt);
f5835d
 #endif
f5835d
 
f5835d
 void	userauth(Authctxt *, char *);
f5835d
@@ -330,6 +505,11 @@ static char *authmethods_get(void);
f5835d
 
f5835d
 Authmethod authmethods[] = {
f5835d
 #ifdef GSSAPI
f5835d
+	{"gssapi-keyex",
f5835d
+		userauth_gsskeyex,
f5835d
+		NULL,
f5835d
+		&options.gss_authentication,
f5835d
+		NULL},
f5835d
 	{"gssapi-with-mic",
f5835d
 		userauth_gssapi,
f5835d
 		NULL,
f5835d
@@ -657,19 +837,31 @@ userauth_gssapi(Authctxt *authctxt)
f5835d
 	static u_int mech = 0;
f5835d
 	OM_uint32 min;
f5835d
 	int r, ok = 0;
f5835d
+	const char *gss_host;
f5835d
+
f5835d
+	if (options.gss_server_identity)
f5835d
+		gss_host = options.gss_server_identity;
f5835d
+	else if (options.gss_trust_dns)
f5835d
+		gss_host = get_canonical_hostname(active_state, 1);
f5835d
+	else
f5835d
+		gss_host = authctxt->host;
f5835d
 
f5835d
 	/* Try one GSSAPI method at a time, rather than sending them all at
f5835d
 	 * once. */
f5835d
 
f5835d
 	if (gss_supported == NULL)
f5835d
-		gss_indicate_mechs(&min, &gss_supported);
f5835d
+		if (GSS_ERROR(gss_indicate_mechs(&min, &gss_supported))) {
f5835d
+			gss_supported = NULL;
f5835d
+			return 0;
f5835d
+		}
f5835d
 
f5835d
 	/* Check to see if the mechanism is usable before we offer it */
f5835d
 	while (mech < gss_supported->count && !ok) {
f5835d
 		/* My DER encoding requires length<128 */
f5835d
 		if (gss_supported->elements[mech].length < 128 &&
f5835d
 		    ssh_gssapi_check_mechanism(&gssctxt,
f5835d
-		    &gss_supported->elements[mech], authctxt->host)) {
f5835d
+		    &gss_supported->elements[mech], gss_host, 
f5835d
+                    options.gss_client_identity)) {
f5835d
 			ok = 1; /* Mechanism works */
f5835d
 		} else {
f5835d
 			mech++;
f5835d
@@ -906,6 +1098,51 @@ input_gssapi_error(int type, u_int32_t p
f5835d
 	free(lang);
f5835d
 	return r;
f5835d
 }
f5835d
+
f5835d
+int
f5835d
+userauth_gsskeyex(Authctxt *authctxt)
f5835d
+{
f5835d
+	struct sshbuf *b = NULL;
f5835d
+	gss_buffer_desc gssbuf;
f5835d
+	gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
f5835d
+	OM_uint32 ms;
f5835d
+
f5835d
+	static int attempt = 0;
f5835d
+	if (attempt++ >= 1)
f5835d
+		return (0);
f5835d
+
f5835d
+	if (gss_kex_context == NULL) {
f5835d
+		debug("No valid Key exchange context"); 
f5835d
+		return (0);
f5835d
+	}
f5835d
+
f5835d
+	if ((b = sshbuf_new()) == NULL)
f5835d
+		fatal("%s: sshbuf_new failed", __func__);
f5835d
+
f5835d
+	ssh_gssapi_buildmic(b, authctxt->server_user, authctxt->service,
f5835d
+	    "gssapi-keyex");
f5835d
+
f5835d
+	gssbuf.value = sshbuf_mutable_ptr(b);
f5835d
+	gssbuf.length = sshbuf_len(b);
f5835d
+
f5835d
+	if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
f5835d
+		sshbuf_free(b);
f5835d
+		return (0);
f5835d
+	}
f5835d
+
f5835d
+	packet_start(SSH2_MSG_USERAUTH_REQUEST);
f5835d
+	packet_put_cstring(authctxt->server_user);
f5835d
+	packet_put_cstring(authctxt->service);
f5835d
+	packet_put_cstring(authctxt->method->name);
f5835d
+	packet_put_string(mic.value, mic.length);
f5835d
+	packet_send();
f5835d
+
f5835d
+	sshbuf_free(b);
f5835d
+	gss_release_buffer(&ms, &mic);
f5835d
+
f5835d
+	return (1);
f5835d
+}
f5835d
+
f5835d
 #endif /* GSSAPI */
f5835d
 
f5835d
 int
f5835d
diff -up openssh/sshd.c.gsskex openssh/sshd.c
f5835d
--- openssh/sshd.c.gsskex	2018-08-22 11:47:33.299216360 +0200
f5835d
+++ openssh/sshd.c	2018-08-22 13:34:28.455975954 +0200
f5835d
@@ -537,8 +537,7 @@ privsep_preauth_child(void)
f5835d
 
f5835d
 #ifdef GSSAPI
f5835d
 	/* Cache supported mechanism OIDs for later use */
f5835d
-	if (options.gss_authentication)
f5835d
-		ssh_gssapi_prepare_supported_oids();
f5835d
+	ssh_gssapi_prepare_supported_oids();
f5835d
 #endif
f5835d
 
f5835d
 	reseed_prngs();
f5835d
@@ -887,8 +887,9 @@ notify_hostkeys(struct ssh *ssh)
f5835d
 	}
f5835d
 	debug3("%s: sent %u hostkeys", __func__, nkeys);
f5835d
 	if (nkeys == 0)
f5835d
-		fatal("%s: no hostkeys", __func__);
f5835d
-	packet_send();
f5835d
+		debug3("%s: no hostkeys", __func__);
f5835d
+	else
f5835d
+		packet_send();
f5835d
 	sshbuf_free(buf);
f5835d
 }
f5835d
 
f5835d
@@ -1841,7 +1842,8 @@ main(int ac, char **av)
f5835d
 		free(fp);
f5835d
 	}
f5835d
 	accumulate_host_timing_secret(cfg, NULL);
f5835d
-	if (!sensitive_data.have_ssh2_key) {
f5835d
+	/* The GSSAPI key exchange can run without a host key */
f5835d
+	if (!sensitive_data.have_ssh2_key && !options.gss_keyex) {
f5835d
 		logit("sshd: no hostkeys available -- exiting.");
f5835d
 		exit(1);
f5835d
 	}
f5835d
@@ -2321,6 +2323,48 @@ do_ssh2_kex(void)
f5835d
 	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
f5835d
 	    list_hostkey_types());
f5835d
 
f5835d
+#ifdef GSSAPI
f5835d
+	{
f5835d
+	char *orig;
f5835d
+	char *gss = NULL;
f5835d
+	char *newstr = NULL;
f5835d
+	orig = myproposal[PROPOSAL_KEX_ALGS];
f5835d
+
f5835d
+	/* 
f5835d
+	 * If we don't have a host key, then there's no point advertising
f5835d
+	 * the other key exchange algorithms
f5835d
+	 */
f5835d
+
f5835d
+	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
f5835d
+		orig = NULL;
f5835d
+
f5835d
+	if (options.gss_keyex)
f5835d
+		gss = ssh_gssapi_server_mechanisms();
f5835d
+	else
f5835d
+		gss = NULL;
f5835d
+
f5835d
+	if (gss && orig)
f5835d
+		xasprintf(&newstr, "%s,%s", gss, orig);
f5835d
+	else if (gss)
f5835d
+		newstr = gss;
f5835d
+	else if (orig)
f5835d
+		newstr = orig;
f5835d
+
f5835d
+	/* 
f5835d
+	 * If we've got GSSAPI mechanisms, then we've got the 'null' host
f5835d
+	 * key alg, but we can't tell people about it unless its the only
f5835d
+  	 * host key algorithm we support
f5835d
+	 */
f5835d
+	if (gss && (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS])) == 0)
f5835d
+		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = "null";
f5835d
+
f5835d
+	if (newstr)
f5835d
+		myproposal[PROPOSAL_KEX_ALGS] = newstr;
f5835d
+	else
f5835d
+		fatal("No supported key exchange algorithms");
f5835d
+	}
f5835d
+#endif
f5835d
+
f5835d
 	/* start key exchange */
f5835d
 	if ((r = kex_setup(active_state, myproposal)) != 0)
f5835d
 		fatal("kex_setup: %s", ssh_err(r));
f5835d
@@ -2338,6 +2382,13 @@ do_ssh2_kex(void)
f5835d
 # endif
f5835d
 #endif
f5835d
 	kex->kex[KEX_C25519_SHA256] = kexc25519_server;
f5835d
+#ifdef GSSAPI
f5835d
+	if (options.gss_keyex) {
f5835d
+		kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
f5835d
+		kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
f5835d
+		kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
f5835d
+	}
f5835d
+#endif
f5835d
 	kex->server = 1;
f5835d
 	kex->client_version_string=client_version_string;
f5835d
 	kex->server_version_string=server_version_string;
f5835d
diff -up openssh/sshd_config.5.gsskex openssh/sshd_config.5
f5835d
--- openssh/sshd_config.5.gsskex	2018-08-22 11:47:33.297216344 +0200
f5835d
+++ openssh/sshd_config.5	2018-08-22 13:35:05.531275099 +0200
f5835d
@@ -642,6 +642,11 @@ Specifies whether to automatically destr
f5835d
 on logout.
f5835d
 The default is
f5835d
 .Cm yes .
f5835d
+.It Cm GSSAPIKeyExchange
f5835d
+Specifies whether key exchange based on GSSAPI is allowed. GSSAPI key exchange
f5835d
+doesn't rely on ssh keys to verify host identity.
f5835d
+The default is
f5835d
+.Dq no .
f5835d
 .It Cm GSSAPIStrictAcceptorCheck
f5835d
 Determines whether to be strict about the identity of the GSSAPI acceptor
f5835d
 a client authenticates against.
f5835d
@@ -656,6 +661,11 @@ machine's default store.
f5835d
 This facility is provided to assist with operation on multi homed machines.
f5835d
 The default is
f5835d
 .Cm yes .
f5835d
+.It Cm GSSAPIStoreCredentialsOnRekey
f5835d
+Controls whether the user's GSSAPI credentials should be updated following a 
f5835d
+successful connection rekeying. This option can be used to accepted renewed 
f5835d
+or updated credentials from a compatible client. The default is
f5835d
+.Dq no .
f5835d
 .It Cm HostbasedAcceptedKeyTypes
f5835d
 Specifies the key types that will be accepted for hostbased authentication
f5835d
 as a list of comma-separated patterns.
f5835d
diff -up openssh/sshd_config.gsskex openssh/sshd_config
f5835d
--- openssh/sshd_config.gsskex	2018-08-22 11:47:33.299216360 +0200
f5835d
+++ openssh/sshd_config	2018-08-22 11:47:33.318216513 +0200
f5835d
@@ -85,6 +85,8 @@ ChallengeResponseAuthentication no
f5835d
 # GSSAPI options
f5835d
 GSSAPIAuthentication yes
f5835d
 GSSAPICleanupCredentials no
f5835d
+#GSSAPIStrictAcceptorCheck yes
f5835d
+#GSSAPIKeyExchange no
f5835d
 
f5835d
 # Set this to 'yes' to enable PAM authentication, account processing,
f5835d
 # and session processing. If this is enabled, PAM authentication will
f5835d
diff -up openssh/ssh-gss.h.gsskex openssh/ssh-gss.h
f5835d
--- openssh/ssh-gss.h.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/ssh-gss.h	2018-08-22 13:36:44.773075793 +0200
f5835d
@@ -1,6 +1,6 @@
f5835d
 /* $OpenBSD: ssh-gss.h,v 1.14 2018/07/10 09:13:30 djm Exp $ */
f5835d
 /*
f5835d
- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
f5835d
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
f5835d
  *
f5835d
  * Redistribution and use in source and binary forms, with or without
f5835d
  * modification, are permitted provided that the following conditions
f5835d
@@ -61,10 +61,22 @@
f5835d
 
f5835d
 #define SSH_GSS_OIDTYPE 0x06
f5835d
 
f5835d
+#define SSH2_MSG_KEXGSS_INIT                            30
f5835d
+#define SSH2_MSG_KEXGSS_CONTINUE                        31
f5835d
+#define SSH2_MSG_KEXGSS_COMPLETE                        32
f5835d
+#define SSH2_MSG_KEXGSS_HOSTKEY                         33
f5835d
+#define SSH2_MSG_KEXGSS_ERROR                           34
f5835d
+#define SSH2_MSG_KEXGSS_GROUPREQ			40
f5835d
+#define SSH2_MSG_KEXGSS_GROUP				41
f5835d
+#define KEX_GSS_GRP1_SHA1_ID				"gss-group1-sha1-"
f5835d
+#define KEX_GSS_GRP14_SHA1_ID				"gss-group14-sha1-"
f5835d
+#define KEX_GSS_GEX_SHA1_ID				"gss-gex-sha1-"
f5835d
+
f5835d
 typedef struct {
f5835d
 	char *filename;
f5835d
 	char *envvar;
f5835d
 	char *envval;
f5835d
+	struct passwd *owner;
f5835d
 	void *data;
f5835d
 } ssh_gssapi_ccache;
f5835d
 
f5835d
@@ -72,8 +84,11 @@ typedef struct {
f5835d
 	gss_buffer_desc displayname;
f5835d
 	gss_buffer_desc exportedname;
f5835d
 	gss_cred_id_t creds;
f5835d
+	gss_name_t name;
f5835d
 	struct ssh_gssapi_mech_struct *mech;
f5835d
 	ssh_gssapi_ccache store;
f5835d
+	int used;
f5835d
+	int updated;
f5835d
 } ssh_gssapi_client;
f5835d
 
f5835d
 typedef struct ssh_gssapi_mech_struct {
f5835d
@@ -84,6 +99,7 @@ typedef struct ssh_gssapi_mech_struct {
f5835d
 	int (*userok) (ssh_gssapi_client *, char *);
f5835d
 	int (*localname) (ssh_gssapi_client *, char **);
f5835d
 	void (*storecreds) (ssh_gssapi_client *);
f5835d
+	int (*updatecreds) (ssh_gssapi_ccache *, ssh_gssapi_client *);
f5835d
 } ssh_gssapi_mech;
f5835d
 
f5835d
 typedef struct {
f5835d
@@ -94,10 +110,11 @@ typedef struct {
f5835d
 	gss_OID		oid; /* client */
f5835d
 	gss_cred_id_t	creds; /* server */
f5835d
 	gss_name_t	client; /* server */
f5835d
-	gss_cred_id_t	client_creds; /* server */
f5835d
+	gss_cred_id_t	client_creds; /* both */
f5835d
 } Gssctxt;
f5835d
 
f5835d
 extern ssh_gssapi_mech *supported_mechs[];
f5835d
+extern Gssctxt *gss_kex_context;
f5835d
 
f5835d
 int  ssh_gssapi_check_oid(Gssctxt *, void *, size_t);
f5835d
 void ssh_gssapi_set_oid_data(Gssctxt *, void *, size_t);
f5835d
@@ -123,17 +140,33 @@ void ssh_gssapi_delete_ctx(Gssctxt **);
f5835d
 OM_uint32 ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
f5835d
 void ssh_gssapi_buildmic(struct sshbuf *, const char *,
f5835d
     const char *, const char *);
f5835d
-int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *);
f5835d
+int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *, const char *);
f5835d
+OM_uint32 ssh_gssapi_client_identity(Gssctxt *, const char *);
f5835d
+int ssh_gssapi_credentials_updated(Gssctxt *);
f5835d
 
f5835d
 /* In the server */
f5835d
+typedef int ssh_gssapi_check_fn(Gssctxt **, gss_OID, const char *, 
f5835d
+    const char *);
f5835d
+char *ssh_gssapi_client_mechanisms(const char *, const char *);
f5835d
+char *ssh_gssapi_kex_mechs(gss_OID_set, ssh_gssapi_check_fn *, const char *,
f5835d
+    const char *);
f5835d
+gss_OID ssh_gssapi_id_kex(Gssctxt *, char *, int);
f5835d
+int ssh_gssapi_server_check_mech(Gssctxt **,gss_OID, const char *, 
f5835d
+    const char *);
f5835d
 OM_uint32 ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
f5835d
-int ssh_gssapi_userok(char *name);
f5835d
+int ssh_gssapi_userok(char *name, struct passwd *);
f5835d
 OM_uint32 ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
f5835d
 void ssh_gssapi_do_child(char ***, u_int *);
f5835d
 void ssh_gssapi_cleanup_creds(void);
f5835d
 void ssh_gssapi_storecreds(void);
f5835d
 const char *ssh_gssapi_displayname(void);
f5835d
 
f5835d
+char *ssh_gssapi_server_mechanisms(void);
f5835d
+int ssh_gssapi_oid_table_ok();
f5835d
+
f5835d
+int ssh_gssapi_update_creds(ssh_gssapi_ccache *store);
f5835d
+
f5835d
+void ssh_gssapi_rekey_creds(void);
f5835d
 #endif /* GSSAPI */
f5835d
 
f5835d
 #endif /* _SSH_GSS_H */
f5835d
diff -up openssh/sshkey.c.gsskex openssh/sshkey.c
f5835d
--- openssh/sshkey.c.gsskex	2018-08-22 11:47:33.319216521 +0200
f5835d
+++ openssh/sshkey.c	2018-08-22 13:37:18.979351804 +0200
f5835d
@@ -140,6 +140,7 @@ static const struct keytype keytypes[] =
f5835d
 #  endif /* OPENSSL_HAS_NISTP521 */
f5835d
 # endif /* OPENSSL_HAS_ECC */
f5835d
 #endif /* WITH_OPENSSL */
f5835d
+	{ "null", "null", NULL, KEY_NULL, 0, 0, 1 },
f5835d
 	{ NULL, NULL, NULL, -1, -1, 0, 0 }
f5835d
 };
f5835d
 
f5835d
diff -up openssh/sshkey.h.gsskex openssh/sshkey.h
f5835d
--- openssh/sshkey.h.gsskex	2018-08-20 07:57:29.000000000 +0200
f5835d
+++ openssh/sshkey.h	2018-08-22 11:47:33.320216529 +0200
f5835d
@@ -63,6 +63,7 @@ enum sshkey_types {
f5835d
 	KEY_ED25519_CERT,
f5835d
 	KEY_XMSS,
f5835d
 	KEY_XMSS_CERT,
f5835d
+	KEY_NULL,
f5835d
 	KEY_UNSPEC
f5835d
 };
f5835d