kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
017ff1
diff --git a/Makefile.in b/Makefile.in
017ff1
index 581b121..2ad26ff 100644
017ff1
--- a/Makefile.in
017ff1
+++ b/Makefile.in
017ff1
@@ -77,6 +77,7 @@ LIBSSH_OBJS=authfd.o authfile.o bufaux.o bufbn.o buffer.o \
f09e2e
 	atomicio.o key.o dispatch.o kex.o mac.o uidswap.o uuencode.o misc.o \
f09e2e
 	monitor_fdpass.o rijndael.o ssh-dss.o ssh-ecdsa.o ssh-rsa.o dh.o \
f09e2e
 	kexdh.o kexgex.o kexdhc.o kexgexc.o bufec.o kexecdh.o kexecdhc.o \
f09e2e
+	kexgssc.o \
f09e2e
 	msg.o progressmeter.o dns.o entropy.o gss-genr.o umac.o umac128.o \
017ff1
 	ssh-pkcs11.o krl.o smult_curve25519_ref.o \
017ff1
 	kexc25519.o kexc25519c.o poly1305.o chacha.o cipher-chachapoly.o \
017ff1
@@ -96,7 +97,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passwd.o auth-rsa.o auth-rh-rsa.o \
017ff1
 	auth2-none.o auth2-passwd.o auth2-pubkey.o \
f09e2e
 	monitor_mm.o monitor.o monitor_wrap.o kexdhs.o kexgexs.o kexecdhs.o \
017ff1
 	kexc25519s.o auth-krb5.o \
f09e2e
-	auth2-gss.o gss-serv.o gss-serv-krb5.o \
017ff1
+	auth2-gss.o gss-serv.o gss-serv-krb5.o  kexgsss.o \
f09e2e
 	loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \
f09e2e
 	sftp-server.o sftp-common.o \
f09e2e
 	roaming_common.o roaming_serv.o \
017ff1
diff --git a/auth2-gss.c b/auth2-gss.c
017ff1
index 4756dd7..ad65059 100644
017ff1
--- a/auth2-gss.c
017ff1
+++ b/auth2-gss.c
017ff1
@@ -52,6 +52,40 @@ static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
f09e2e
 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
f09e2e
 static void input_gssapi_errtok(int, u_int32_t, void *);
f09e2e
 
f09e2e
+/* 
f09e2e
+ * The 'gssapi_keyex' userauth mechanism.
f09e2e
+ */
f09e2e
+static int
f09e2e
+userauth_gsskeyex(Authctxt *authctxt)
f09e2e
+{
f09e2e
+	int authenticated = 0;
f09e2e
+	Buffer b;
f09e2e
+	gss_buffer_desc mic, gssbuf;
f09e2e
+	u_int len;
f09e2e
+
f09e2e
+	mic.value = packet_get_string(&len;;
f09e2e
+	mic.length = len;
f09e2e
+
f09e2e
+	packet_check_eom();
f09e2e
+
f09e2e
+	ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
f09e2e
+	    "gssapi-keyex");
f09e2e
+
f09e2e
+	gssbuf.value = buffer_ptr(&b);
f09e2e
+	gssbuf.length = buffer_len(&b);
f09e2e
+
f09e2e
+	/* gss_kex_context is NULL with privsep, so we can't check it here */
f09e2e
+	if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context, 
f09e2e
+	    &gssbuf, &mic))))
f09e2e
+		authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
f09e2e
+		    authctxt->pw));
f09e2e
+	
f09e2e
+	buffer_free(&b);
f09e2e
+	free(mic.value);
f09e2e
+
f09e2e
+	return (authenticated);
f09e2e
+}
f09e2e
+
f09e2e
 /*
f09e2e
  * We only support those mechanisms that we know about (ie ones that we know
f09e2e
  * how to check local user kuserok and the like)
017ff1
@@ -235,7 +269,8 @@ input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
f09e2e
 
f09e2e
 	packet_check_eom();
f09e2e
 
f09e2e
-	authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
f09e2e
+	authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
f09e2e
+	    authctxt->pw));
f09e2e
 
f09e2e
 	authctxt->postponed = 0;
f09e2e
 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
017ff1
@@ -277,7 +312,8 @@ input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
f09e2e
 	gssbuf.length = buffer_len(&b);
f09e2e
 
f09e2e
 	if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
f09e2e
-		authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
f09e2e
+		authenticated = 
f09e2e
+		    PRIVSEP(ssh_gssapi_userok(authctxt->user, authctxt->pw));
f09e2e
 	else
f09e2e
 		logit("GSSAPI MIC check failed");
f09e2e
 
017ff1
@@ -294,6 +330,12 @@ input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
f09e2e
 	userauth_finish(authctxt, authenticated, "gssapi-with-mic", NULL);
f09e2e
 }
f09e2e
 
f09e2e
+Authmethod method_gsskeyex = {
f09e2e
+	"gssapi-keyex",
f09e2e
+	userauth_gsskeyex,
f09e2e
+	&options.gss_authentication
f09e2e
+};
f09e2e
+
f09e2e
 Authmethod method_gssapi = {
f09e2e
 	"gssapi-with-mic",
f09e2e
 	userauth_gssapi,
017ff1
diff --git a/auth2.c b/auth2.c
017ff1
index 5f4f26f..0f52b68 100644
017ff1
--- a/auth2.c
017ff1
+++ b/auth2.c
f09e2e
@@ -69,6 +69,7 @@ extern Authmethod method_passwd;
f09e2e
 extern Authmethod method_kbdint;
f09e2e
 extern Authmethod method_hostbased;
f09e2e
 #ifdef GSSAPI
f09e2e
+extern Authmethod method_gsskeyex;
f09e2e
 extern Authmethod method_gssapi;
f09e2e
 #endif
017ff1
 
017ff1
@@ -76,6 +77,7 @@ Authmethod *authmethods[] = {
f09e2e
 	&method_none,
f09e2e
 	&method_pubkey,
f09e2e
 #ifdef GSSAPI
f09e2e
+	&method_gsskeyex,
f09e2e
 	&method_gssapi,
f09e2e
 #endif
017ff1
 	&method_passwd,
017ff1
diff --git a/clientloop.c b/clientloop.c
017ff1
index 59ad3a2..9c60108 100644
017ff1
--- a/clientloop.c
017ff1
+++ b/clientloop.c
f09e2e
@@ -111,6 +111,10 @@
f09e2e
 #include "msg.h"
f09e2e
 #include "roaming.h"
f09e2e
 
f09e2e
+#ifdef GSSAPI
f09e2e
+#include "ssh-gss.h"
f09e2e
+#endif
f09e2e
+
f09e2e
 /* import options */
f09e2e
 extern Options options;
f09e2e
 
017ff1
@@ -1608,6 +1612,15 @@ client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
f09e2e
 		/* Do channel operations unless rekeying in progress. */
f09e2e
 		if (!rekeying) {
f09e2e
 			channel_after_select(readset, writeset);
f09e2e
+
f09e2e
+#ifdef GSSAPI
f09e2e
+			if (options.gss_renewal_rekey &&
f09e2e
+			    ssh_gssapi_credentials_updated(GSS_C_NO_CONTEXT)) {
f09e2e
+				debug("credentials updated - forcing rekey");
f09e2e
+				need_rekeying = 1;
f09e2e
+			}
f09e2e
+#endif
f09e2e
+
f09e2e
 			if (need_rekeying || packet_need_rekeying()) {
f09e2e
 				debug("need rekeying");
f09e2e
 				xxx_kex->done = 0;
017ff1
diff --git a/configure.ac b/configure.ac
017ff1
index 74e77db..9bde04e 100644
017ff1
--- a/configure.ac
017ff1
+++ b/configure.ac
017ff1
@@ -584,6 +584,30 @@ main() { if (NSVersionOfRunTimeLibrary("System") >= (60 << 16))
f09e2e
 	    [Use tunnel device compatibility to OpenBSD])
f09e2e
 	AC_DEFINE([SSH_TUN_PREPEND_AF], [1],
f09e2e
 	    [Prepend the address family to IP tunnel traffic])
f09e2e
+	AC_MSG_CHECKING(if we have the Security Authorization Session API)
f09e2e
+	AC_TRY_COMPILE([#include <Security/AuthSession.h>],
f09e2e
+		[SessionCreate(0, 0);],
f09e2e
+		[ac_cv_use_security_session_api="yes"
f09e2e
+		 AC_DEFINE(USE_SECURITY_SESSION_API, 1, 
f09e2e
+			[platform has the Security Authorization Session API])
f09e2e
+		 LIBS="$LIBS -framework Security"
f09e2e
+		 AC_MSG_RESULT(yes)],
f09e2e
+		[ac_cv_use_security_session_api="no"
f09e2e
+		 AC_MSG_RESULT(no)])
f09e2e
+	AC_MSG_CHECKING(if we have an in-memory credentials cache)
f09e2e
+	AC_TRY_COMPILE(
f09e2e
+		[#include <Kerberos/Kerberos.h>],
f09e2e
+		[cc_context_t c;
f09e2e
+		 (void) cc_initialize (&c, 0, NULL, NULL);],
f09e2e
+		[AC_DEFINE(USE_CCAPI, 1, 
f09e2e
+			[platform uses an in-memory credentials cache])
f09e2e
+		 LIBS="$LIBS -framework Security"
f09e2e
+		 AC_MSG_RESULT(yes)
f09e2e
+		 if test "x$ac_cv_use_security_session_api" = "xno"; then
f09e2e
+			AC_MSG_ERROR(*** Need a security framework to use the credentials cache API ***)
f09e2e
+		fi],
f09e2e
+		[AC_MSG_RESULT(no)]
f09e2e
+	)
f09e2e
 	m4_pattern_allow([AU_IPv])
f09e2e
 	AC_CHECK_DECL([AU_IPv4], [], 
f09e2e
 	    AC_DEFINE([AU_IPv4], [0], [System only supports IPv4 audit records])
017ff1
diff --git a/gss-genr.c b/gss-genr.c
017ff1
index b39281b..a3a2289 100644
017ff1
--- a/gss-genr.c
017ff1
+++ b/gss-genr.c
f09e2e
@@ -39,12 +39,167 @@
f09e2e
 #include "buffer.h"
f09e2e
 #include "log.h"
f09e2e
 #include "ssh2.h"
f09e2e
+#include "cipher.h"
f09e2e
+#include "key.h"
f09e2e
+#include "kex.h"
f09e2e
+#include <openssl/evp.h>
f09e2e
 
f09e2e
 #include "ssh-gss.h"
f09e2e
 
f09e2e
 extern u_char *session_id2;
f09e2e
 extern u_int session_id2_len;
f09e2e
 
f09e2e
+typedef struct {
f09e2e
+	char *encoded;
f09e2e
+	gss_OID oid;
f09e2e
+} ssh_gss_kex_mapping;
f09e2e
+
f09e2e
+/*
f09e2e
+ * XXX - It would be nice to find a more elegant way of handling the
f09e2e
+ * XXX   passing of the key exchange context to the userauth routines
f09e2e
+ */
f09e2e
+
f09e2e
+Gssctxt *gss_kex_context = NULL;
f09e2e
+
f09e2e
+static ssh_gss_kex_mapping *gss_enc2oid = NULL;
f09e2e
+
f09e2e
+int 
f09e2e
+ssh_gssapi_oid_table_ok() {
f09e2e
+	return (gss_enc2oid != NULL);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * Return a list of the gss-group1-sha1 mechanisms supported by this program
f09e2e
+ *
f09e2e
+ * We test mechanisms to ensure that we can use them, to avoid starting
f09e2e
+ * a key exchange with a bad mechanism
f09e2e
+ */
f09e2e
+
f09e2e
+char *
f09e2e
+ssh_gssapi_client_mechanisms(const char *host, const char *client) {
f09e2e
+	gss_OID_set gss_supported;
f09e2e
+	OM_uint32 min_status;
f09e2e
+
f09e2e
+	if (GSS_ERROR(gss_indicate_mechs(&min_status, &gss_supported)))
f09e2e
+		return NULL;
f09e2e
+
f09e2e
+	return(ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism,
f09e2e
+	    host, client));
f09e2e
+}
f09e2e
+
f09e2e
+char *
f09e2e
+ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check,
f09e2e
+    const char *host, const char *client) {
f09e2e
+	Buffer buf;
f09e2e
+	size_t i;
f09e2e
+	int oidpos, enclen;
f09e2e
+	char *mechs, *encoded;
f09e2e
+	u_char digest[EVP_MAX_MD_SIZE];
f09e2e
+	char deroid[2];
f09e2e
+	const EVP_MD *evp_md = EVP_md5();
f09e2e
+	EVP_MD_CTX md;
f09e2e
+
f09e2e
+	if (gss_enc2oid != NULL) {
f09e2e
+		for (i = 0; gss_enc2oid[i].encoded != NULL; i++)
f09e2e
+			free(gss_enc2oid[i].encoded);
f09e2e
+		free(gss_enc2oid);
f09e2e
+	}
f09e2e
+
f09e2e
+	gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping) *
f09e2e
+	    (gss_supported->count + 1));
f09e2e
+
f09e2e
+	buffer_init(&buf;;
f09e2e
+
f09e2e
+	oidpos = 0;
f09e2e
+	for (i = 0; i < gss_supported->count; i++) {
f09e2e
+		if (gss_supported->elements[i].length < 128 &&
f09e2e
+		    (*check)(NULL, &(gss_supported->elements[i]), host, client)) {
f09e2e
+
f09e2e
+			deroid[0] = SSH_GSS_OIDTYPE;
f09e2e
+			deroid[1] = gss_supported->elements[i].length;
f09e2e
+
f09e2e
+			EVP_DigestInit(&md, evp_md);
f09e2e
+			EVP_DigestUpdate(&md, deroid, 2);
f09e2e
+			EVP_DigestUpdate(&md,
f09e2e
+			    gss_supported->elements[i].elements,
f09e2e
+			    gss_supported->elements[i].length);
f09e2e
+			EVP_DigestFinal(&md, digest, NULL);
f09e2e
+
f09e2e
+			encoded = xmalloc(EVP_MD_size(evp_md) * 2);
f09e2e
+			enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
f09e2e
+			    encoded, EVP_MD_size(evp_md) * 2);
f09e2e
+
f09e2e
+			if (oidpos != 0)
f09e2e
+				buffer_put_char(&buf, ',');
f09e2e
+
f09e2e
+			buffer_append(&buf, KEX_GSS_GEX_SHA1_ID,
f09e2e
+			    sizeof(KEX_GSS_GEX_SHA1_ID) - 1);
f09e2e
+			buffer_append(&buf, encoded, enclen);
f09e2e
+			buffer_put_char(&buf, ',');
f09e2e
+			buffer_append(&buf, KEX_GSS_GRP1_SHA1_ID, 
f09e2e
+			    sizeof(KEX_GSS_GRP1_SHA1_ID) - 1);
f09e2e
+			buffer_append(&buf, encoded, enclen);
f09e2e
+			buffer_put_char(&buf, ',');
f09e2e
+			buffer_append(&buf, KEX_GSS_GRP14_SHA1_ID,
f09e2e
+			    sizeof(KEX_GSS_GRP14_SHA1_ID) - 1);
f09e2e
+			buffer_append(&buf, encoded, enclen);
f09e2e
+
f09e2e
+			gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]);
f09e2e
+			gss_enc2oid[oidpos].encoded = encoded;
f09e2e
+			oidpos++;
f09e2e
+		}
f09e2e
+	}
f09e2e
+	gss_enc2oid[oidpos].oid = NULL;
f09e2e
+	gss_enc2oid[oidpos].encoded = NULL;
f09e2e
+
f09e2e
+	buffer_put_char(&buf, '\0');
f09e2e
+
f09e2e
+	mechs = xmalloc(buffer_len(&buf));
f09e2e
+	buffer_get(&buf, mechs, buffer_len(&buf));
f09e2e
+	buffer_free(&buf;;
f09e2e
+
f09e2e
+	if (strlen(mechs) == 0) {
f09e2e
+		free(mechs);
f09e2e
+		mechs = NULL;
f09e2e
+	}
f09e2e
+	
f09e2e
+	return (mechs);
f09e2e
+}
f09e2e
+
f09e2e
+gss_OID
f09e2e
+ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int kex_type) {
f09e2e
+	int i = 0;
f09e2e
+	
f09e2e
+	switch (kex_type) {
f09e2e
+	case KEX_GSS_GRP1_SHA1:
f09e2e
+		if (strlen(name) < sizeof(KEX_GSS_GRP1_SHA1_ID))
f09e2e
+			return GSS_C_NO_OID;
f09e2e
+		name += sizeof(KEX_GSS_GRP1_SHA1_ID) - 1;
f09e2e
+		break;
f09e2e
+	case KEX_GSS_GRP14_SHA1:
f09e2e
+		if (strlen(name) < sizeof(KEX_GSS_GRP14_SHA1_ID))
f09e2e
+			return GSS_C_NO_OID;
f09e2e
+		name += sizeof(KEX_GSS_GRP14_SHA1_ID) - 1;
f09e2e
+		break;
f09e2e
+	case KEX_GSS_GEX_SHA1:
f09e2e
+		if (strlen(name) < sizeof(KEX_GSS_GEX_SHA1_ID))
f09e2e
+			return GSS_C_NO_OID;
f09e2e
+		name += sizeof(KEX_GSS_GEX_SHA1_ID) - 1;
f09e2e
+		break;
f09e2e
+	default:
f09e2e
+		return GSS_C_NO_OID;
f09e2e
+	}
f09e2e
+
f09e2e
+	while (gss_enc2oid[i].encoded != NULL &&
f09e2e
+	    strcmp(name, gss_enc2oid[i].encoded) != 0)
f09e2e
+		i++;
f09e2e
+
f09e2e
+	if (gss_enc2oid[i].oid != NULL && ctx != NULL)
f09e2e
+		ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid);
f09e2e
+
f09e2e
+	return gss_enc2oid[i].oid;
f09e2e
+}
f09e2e
+
f09e2e
 /* Check that the OID in a data stream matches that in the context */
f09e2e
 int
f09e2e
 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
017ff1
@@ -197,7 +352,7 @@ ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
f09e2e
 	}
f09e2e
 
f09e2e
 	ctx->major = gss_init_sec_context(&ctx->minor,
f09e2e
-	    GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
f09e2e
+	    ctx->client_creds, &ctx->context, ctx->name, ctx->oid,
f09e2e
 	    GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
f09e2e
 	    0, NULL, recv_tok, NULL, send_tok, flags, NULL);
f09e2e
 
017ff1
@@ -227,8 +382,42 @@ ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
f09e2e
 }
f09e2e
 
f09e2e
 OM_uint32
f09e2e
+ssh_gssapi_client_identity(Gssctxt *ctx, const char *name)
f09e2e
+{
f09e2e
+	gss_buffer_desc gssbuf;
f09e2e
+	gss_name_t gssname;
f09e2e
+	OM_uint32 status;
f09e2e
+	gss_OID_set oidset;
f09e2e
+
f09e2e
+	gssbuf.value = (void *) name;
f09e2e
+	gssbuf.length = strlen(gssbuf.value);
f09e2e
+
f09e2e
+	gss_create_empty_oid_set(&status, &oidset);
f09e2e
+	gss_add_oid_set_member(&status, ctx->oid, &oidset);
f09e2e
+
f09e2e
+	ctx->major = gss_import_name(&ctx->minor, &gssbuf,
f09e2e
+	    GSS_C_NT_USER_NAME, &gssname);
f09e2e
+
f09e2e
+	if (!ctx->major)
f09e2e
+		ctx->major = gss_acquire_cred(&ctx->minor, 
f09e2e
+		    gssname, 0, oidset, GSS_C_INITIATE, 
f09e2e
+		    &ctx->client_creds, NULL, NULL);
f09e2e
+
f09e2e
+	gss_release_name(&status, &gssname);
f09e2e
+	gss_release_oid_set(&status, &oidset);
f09e2e
+
f09e2e
+	if (ctx->major)
f09e2e
+		ssh_gssapi_error(ctx);
f09e2e
+
f09e2e
+	return(ctx->major);
f09e2e
+}
f09e2e
+
f09e2e
+OM_uint32
f09e2e
 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
f09e2e
 {
f09e2e
+	if (ctx == NULL) 
f09e2e
+		return -1;
f09e2e
+
f09e2e
 	if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
f09e2e
 	    GSS_C_QOP_DEFAULT, buffer, hash)))
f09e2e
 		ssh_gssapi_error(ctx);
017ff1
@@ -236,6 +425,19 @@ ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
f09e2e
 	return (ctx->major);
f09e2e
 }
f09e2e
 
f09e2e
+/* Priviledged when used by server */
f09e2e
+OM_uint32
f09e2e
+ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
f09e2e
+{
f09e2e
+	if (ctx == NULL)
f09e2e
+		return -1;
f09e2e
+
f09e2e
+	ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
f09e2e
+	    gssbuf, gssmic, NULL);
f09e2e
+
f09e2e
+	return (ctx->major);
f09e2e
+}
f09e2e
+
f09e2e
 void
f09e2e
 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
f09e2e
     const char *context)
017ff1
@@ -249,11 +451,16 @@ ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
f09e2e
 }
f09e2e
 
f09e2e
 int
f09e2e
-ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
f09e2e
+ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host, 
f09e2e
+    const char *client)
f09e2e
 {
f09e2e
 	gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
f09e2e
 	OM_uint32 major, minor;
f09e2e
 	gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
f09e2e
+	Gssctxt *intctx = NULL;
f09e2e
+
f09e2e
+	if (ctx == NULL)
f09e2e
+		ctx = &intct;;
f09e2e
 
f09e2e
 	/* RFC 4462 says we MUST NOT do SPNEGO */
f09e2e
 	if (oid->length == spnego_oid.length && 
017ff1
@@ -263,6 +470,10 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
f09e2e
 	ssh_gssapi_build_ctx(ctx);
f09e2e
 	ssh_gssapi_set_oid(*ctx, oid);
f09e2e
 	major = ssh_gssapi_import_name(*ctx, host);
f09e2e
+
f09e2e
+	if (!GSS_ERROR(major) && client)
f09e2e
+		major = ssh_gssapi_client_identity(*ctx, client);
f09e2e
+
f09e2e
 	if (!GSS_ERROR(major)) {
f09e2e
 		major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, 
f09e2e
 		    NULL);
017ff1
@@ -272,10 +483,67 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
f09e2e
 			    GSS_C_NO_BUFFER);
f09e2e
 	}
f09e2e
 
f09e2e
-	if (GSS_ERROR(major)) 
f09e2e
+	if (GSS_ERROR(major) || intctx != NULL) 
f09e2e
 		ssh_gssapi_delete_ctx(ctx);
f09e2e
 
f09e2e
 	return (!GSS_ERROR(major));
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+ssh_gssapi_credentials_updated(Gssctxt *ctxt) {
f09e2e
+	static gss_name_t saved_name = GSS_C_NO_NAME;
f09e2e
+	static OM_uint32 saved_lifetime = 0;
f09e2e
+	static gss_OID saved_mech = GSS_C_NO_OID;
f09e2e
+	static gss_name_t name;
f09e2e
+	static OM_uint32 last_call = 0;
f09e2e
+	OM_uint32 lifetime, now, major, minor;
f09e2e
+	int equal;
f09e2e
+	gss_cred_usage_t usage = GSS_C_INITIATE;
f09e2e
+	
f09e2e
+	now = time(NULL);
f09e2e
+
f09e2e
+	if (ctxt) {
f09e2e
+		debug("Rekey has happened - updating saved versions");
f09e2e
+
f09e2e
+		if (saved_name != GSS_C_NO_NAME)
f09e2e
+			gss_release_name(&minor, &saved_name);
f09e2e
+
f09e2e
+		major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
f09e2e
+		    &saved_name, &saved_lifetime, NULL, NULL);
f09e2e
+
f09e2e
+		if (!GSS_ERROR(major)) {
f09e2e
+			saved_mech = ctxt->oid;
f09e2e
+		        saved_lifetime+= now;
f09e2e
+		} else {
f09e2e
+			/* Handle the error */
f09e2e
+		}
f09e2e
+		return 0;
f09e2e
+	}
f09e2e
+
f09e2e
+	if (now - last_call < 10)
f09e2e
+		return 0;
f09e2e
+
f09e2e
+	last_call = now;
f09e2e
+
f09e2e
+	if (saved_mech == GSS_C_NO_OID)
f09e2e
+		return 0;
f09e2e
+	
f09e2e
+	major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL, 
f09e2e
+	    &name, &lifetime, NULL, NULL);
f09e2e
+	if (major == GSS_S_CREDENTIALS_EXPIRED)
f09e2e
+		return 0;
f09e2e
+	else if (GSS_ERROR(major))
f09e2e
+		return 0;
f09e2e
+
f09e2e
+	major = gss_compare_name(&minor, saved_name, name, &equal);
f09e2e
+	gss_release_name(&minor, &name);
f09e2e
+	if (GSS_ERROR(major))
f09e2e
+		return 0;
f09e2e
+
f09e2e
+	if (equal && (saved_lifetime < lifetime + now - 10))
f09e2e
+		return 1;
f09e2e
+
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
 #endif /* GSSAPI */
017ff1
diff --git a/gss-serv-krb5.c b/gss-serv-krb5.c
017ff1
index 759fa10..42de994 100644
017ff1
--- a/gss-serv-krb5.c
017ff1
+++ b/gss-serv-krb5.c
017ff1
@@ -120,7 +120,7 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
f09e2e
 	krb5_error_code problem;
f09e2e
 	krb5_principal princ;
f09e2e
 	OM_uint32 maj_status, min_status;
f09e2e
-	int len;
f09e2e
+	const char *new_ccname, *new_cctype;
f09e2e
 	const char *errmsg;
f09e2e
 
f09e2e
 	if (client->creds == NULL) {
017ff1
@@ -180,11 +180,26 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
f09e2e
 		return;
f09e2e
 	}
f09e2e
 
f09e2e
-	client->store.filename = xstrdup(krb5_cc_get_name(krb_context, ccache));
f09e2e
+	new_cctype = krb5_cc_get_type(krb_context, ccache);
f09e2e
+	new_ccname = krb5_cc_get_name(krb_context, ccache);
f09e2e
+
f09e2e
 	client->store.envvar = "KRB5CCNAME";
f09e2e
-	len = strlen(client->store.filename) + 6;
f09e2e
-	client->store.envval = xmalloc(len);
f09e2e
-	snprintf(client->store.envval, len, "FILE:%s", client->store.filename);
f09e2e
+#ifdef USE_CCAPI
f09e2e
+	xasprintf(&client->store.envval, "API:%s", new_ccname);
f09e2e
+	client->store.filename = NULL;
f09e2e
+#else
f09e2e
+	if (new_ccname[0] == ':')
f09e2e
+		new_ccname++;
f09e2e
+	xasprintf(&client->store.envval, "%s:%s", new_cctype, new_ccname);
f09e2e
+	if (strcmp(new_cctype, "DIR") == 0) {
f09e2e
+		char *p;
f09e2e
+		p = strrchr(client->store.envval, '/');
f09e2e
+		if (p)
f09e2e
+			*p = '\0';
f09e2e
+	}
f09e2e
+	if ((strcmp(new_cctype, "FILE") == 0) || (strcmp(new_cctype, "DIR") == 0))
f09e2e
+		client->store.filename = xstrdup(new_ccname);
f09e2e
+#endif
f09e2e
 
f09e2e
 #ifdef USE_PAM
f09e2e
 	if (options.use_pam)
017ff1
@@ -193,9 +208,76 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
f09e2e
 
f09e2e
 	krb5_cc_close(krb_context, ccache);
f09e2e
 
f09e2e
+	client->store.data = krb_context;
f09e2e
+
f09e2e
 	return;
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+ssh_gssapi_krb5_updatecreds(ssh_gssapi_ccache *store, 
f09e2e
+    ssh_gssapi_client *client)
f09e2e
+{
f09e2e
+	krb5_ccache ccache = NULL;
f09e2e
+	krb5_principal principal = NULL;
f09e2e
+	char *name = NULL;
f09e2e
+	krb5_error_code problem;
f09e2e
+	OM_uint32 maj_status, min_status;
f09e2e
+
f09e2e
+   	if ((problem = krb5_cc_resolve(krb_context, store->envval, &ccache))) {
f09e2e
+                logit("krb5_cc_resolve(): %.100s",
f09e2e
+                    krb5_get_err_text(krb_context, problem));
f09e2e
+                return 0;
f09e2e
+       	}
f09e2e
+	
f09e2e
+	/* Find out who the principal in this cache is */
f09e2e
+	if ((problem = krb5_cc_get_principal(krb_context, ccache, 
f09e2e
+	    &principal))) {
f09e2e
+		logit("krb5_cc_get_principal(): %.100s",
f09e2e
+		    krb5_get_err_text(krb_context, problem));
f09e2e
+		krb5_cc_close(krb_context, ccache);
f09e2e
+		return 0;
f09e2e
+	}
f09e2e
+
f09e2e
+	if ((problem = krb5_unparse_name(krb_context, principal, &name))) {
f09e2e
+		logit("krb5_unparse_name(): %.100s",
f09e2e
+		    krb5_get_err_text(krb_context, problem));
f09e2e
+		krb5_free_principal(krb_context, principal);
f09e2e
+		krb5_cc_close(krb_context, ccache);
f09e2e
+		return 0;
f09e2e
+	}
f09e2e
+
f09e2e
+
f09e2e
+	if (strcmp(name,client->exportedname.value)!=0) {
f09e2e
+		debug("Name in local credentials cache differs. Not storing");
f09e2e
+		krb5_free_principal(krb_context, principal);
f09e2e
+		krb5_cc_close(krb_context, ccache);
f09e2e
+		krb5_free_unparsed_name(krb_context, name);
f09e2e
+		return 0;
f09e2e
+	}
f09e2e
+	krb5_free_unparsed_name(krb_context, name);
f09e2e
+
f09e2e
+	/* Name matches, so lets get on with it! */
f09e2e
+
f09e2e
+	if ((problem = krb5_cc_initialize(krb_context, ccache, principal))) {
f09e2e
+		logit("krb5_cc_initialize(): %.100s",
f09e2e
+		    krb5_get_err_text(krb_context, problem));
f09e2e
+		krb5_free_principal(krb_context, principal);
f09e2e
+		krb5_cc_close(krb_context, ccache);
f09e2e
+		return 0;
f09e2e
+	}
f09e2e
+
f09e2e
+	krb5_free_principal(krb_context, principal);
f09e2e
+
f09e2e
+	if ((maj_status = gss_krb5_copy_ccache(&min_status, client->creds,
f09e2e
+	    ccache))) {
f09e2e
+		logit("gss_krb5_copy_ccache() failed. Sorry!");
f09e2e
+		krb5_cc_close(krb_context, ccache);
f09e2e
+		return 0;
f09e2e
+	}
f09e2e
+
f09e2e
+	return 1;
f09e2e
+}
f09e2e
+
f09e2e
 ssh_gssapi_mech gssapi_kerberos_mech = {
f09e2e
 	"toWM5Slw5Ew8Mqkay+al2g==",
f09e2e
 	"Kerberos",
017ff1
@@ -203,7 +285,8 @@ ssh_gssapi_mech gssapi_kerberos_mech = {
f09e2e
 	NULL,
f09e2e
 	&ssh_gssapi_krb5_userok,
f09e2e
 	NULL,
f09e2e
-	&ssh_gssapi_krb5_storecreds
f09e2e
+	&ssh_gssapi_krb5_storecreds,
f09e2e
+	&ssh_gssapi_krb5_updatecreds
f09e2e
 };
f09e2e
 
f09e2e
 #endif /* KRB5 */
017ff1
diff --git a/gss-serv.c b/gss-serv.c
017ff1
index e61b37b..14f540e 100644
017ff1
--- a/gss-serv.c
017ff1
+++ b/gss-serv.c
f09e2e
@@ -45,15 +45,20 @@
f09e2e
 #include "channels.h"
f09e2e
 #include "session.h"
f09e2e
 #include "misc.h"
f09e2e
+#include "servconf.h"
f09e2e
+#include "uidswap.h"
f09e2e
 
f09e2e
 #include "ssh-gss.h"
f09e2e
+#include "monitor_wrap.h"
f09e2e
+
f09e2e
+extern ServerOptions options;
f09e2e
 
f09e2e
 static ssh_gssapi_client gssapi_client =
f09e2e
     { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
f09e2e
-    GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}};
f09e2e
+    GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME,  NULL, {NULL, NULL, NULL}, 0, 0};
f09e2e
 
f09e2e
 ssh_gssapi_mech gssapi_null_mech =
f09e2e
-    { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
f09e2e
+    { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
f09e2e
 
f09e2e
 #ifdef KRB5
f09e2e
 extern ssh_gssapi_mech gssapi_kerberos_mech;
017ff1
@@ -100,25 +105,32 @@ ssh_gssapi_acquire_cred(Gssctxt *ctx)
f09e2e
 	char lname[MAXHOSTNAMELEN];
f09e2e
 	gss_OID_set oidset;
f09e2e
 
f09e2e
-	gss_create_empty_oid_set(&status, &oidset);
f09e2e
-	gss_add_oid_set_member(&status, ctx->oid, &oidset);
f09e2e
+	if (options.gss_strict_acceptor) {
f09e2e
+		gss_create_empty_oid_set(&status, &oidset);
f09e2e
+		gss_add_oid_set_member(&status, ctx->oid, &oidset);
f09e2e
 
f09e2e
-	if (gethostname(lname, MAXHOSTNAMELEN)) {
f09e2e
-		gss_release_oid_set(&status, &oidset);
f09e2e
-		return (-1);
f09e2e
-	}
017ff1
+		if (gethostname(lname, MAXHOSTNAMELEN)) {
017ff1
+			gss_release_oid_set(&status, &oidset);
017ff1
+			return (-1);
017ff1
+		}
017ff1
+
f09e2e
+		if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
f09e2e
+			gss_release_oid_set(&status, &oidset);
f09e2e
+			return (ctx->major);
f09e2e
+		}
f09e2e
+
f09e2e
+		if ((ctx->major = gss_acquire_cred(&ctx->minor,
f09e2e
+		    ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, 
f09e2e
+		    NULL, NULL)))
f09e2e
+			ssh_gssapi_error(ctx);
f09e2e
 
f09e2e
-	if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
f09e2e
 		gss_release_oid_set(&status, &oidset);
f09e2e
 		return (ctx->major);
f09e2e
+	} else {
f09e2e
+		ctx->name = GSS_C_NO_NAME;
f09e2e
+		ctx->creds = GSS_C_NO_CREDENTIAL;
f09e2e
 	}
f09e2e
-
f09e2e
-	if ((ctx->major = gss_acquire_cred(&ctx->minor,
f09e2e
-	    ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
f09e2e
-		ssh_gssapi_error(ctx);
f09e2e
-
f09e2e
-	gss_release_oid_set(&status, &oidset);
f09e2e
-	return (ctx->major);
f09e2e
+	return GSS_S_COMPLETE;
f09e2e
 }
f09e2e
 
f09e2e
 /* Privileged */
017ff1
@@ -133,6 +145,29 @@ ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
f09e2e
 }
f09e2e
 
f09e2e
 /* Unprivileged */
f09e2e
+char *
f09e2e
+ssh_gssapi_server_mechanisms() {
f09e2e
+	gss_OID_set	supported;
f09e2e
+
f09e2e
+	ssh_gssapi_supported_oids(&supported);
f09e2e
+	return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
f09e2e
+	    NULL, NULL));
f09e2e
+}
f09e2e
+
f09e2e
+/* Unprivileged */
f09e2e
+int
f09e2e
+ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
f09e2e
+    const char *dummy) {
f09e2e
+	Gssctxt *ctx = NULL;
f09e2e
+	int res;
f09e2e
+ 
f09e2e
+	res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
f09e2e
+	ssh_gssapi_delete_ctx(&ctx;;
f09e2e
+
f09e2e
+	return (res);
f09e2e
+}
f09e2e
+
f09e2e
+/* Unprivileged */
f09e2e
 void
f09e2e
 ssh_gssapi_supported_oids(gss_OID_set *oidset)
f09e2e
 {
017ff1
@@ -142,7 +177,9 @@ ssh_gssapi_supported_oids(gss_OID_set *oidset)
f09e2e
 	gss_OID_set supported;
f09e2e
 
f09e2e
 	gss_create_empty_oid_set(&min_status, oidset);
f09e2e
-	gss_indicate_mechs(&min_status, &supported);
f09e2e
+
f09e2e
+	if (GSS_ERROR(gss_indicate_mechs(&min_status, &supported)))
f09e2e
+		return;
f09e2e
 
f09e2e
 	while (supported_mechs[i]->name != NULL) {
f09e2e
 		if (GSS_ERROR(gss_test_oid_set_member(&min_status,
017ff1
@@ -268,8 +305,48 @@ OM_uint32
f09e2e
 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
f09e2e
 {
f09e2e
 	int i = 0;
f09e2e
+	int equal = 0;
f09e2e
+	gss_name_t new_name = GSS_C_NO_NAME;
f09e2e
+	gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
f09e2e
+
f09e2e
+	if (options.gss_store_rekey && client->used && ctx->client_creds) {
f09e2e
+		if (client->mech->oid.length != ctx->oid->length ||
f09e2e
+		    (memcmp(client->mech->oid.elements,
f09e2e
+		     ctx->oid->elements, ctx->oid->length) !=0)) {
f09e2e
+			debug("Rekeyed credentials have different mechanism");
f09e2e
+			return GSS_S_COMPLETE;
f09e2e
+		}
f09e2e
+
f09e2e
+		if ((ctx->major = gss_inquire_cred_by_mech(&ctx->minor, 
f09e2e
+		    ctx->client_creds, ctx->oid, &new_name, 
f09e2e
+		    NULL, NULL, NULL))) {
f09e2e
+			ssh_gssapi_error(ctx);
f09e2e
+			return (ctx->major);
f09e2e
+		}
f09e2e
+
f09e2e
+		ctx->major = gss_compare_name(&ctx->minor, client->name, 
f09e2e
+		    new_name, &equal);
f09e2e
 
f09e2e
-	gss_buffer_desc ename;
f09e2e
+		if (GSS_ERROR(ctx->major)) {
f09e2e
+			ssh_gssapi_error(ctx);
f09e2e
+			return (ctx->major);
f09e2e
+		}
f09e2e
+ 
f09e2e
+		if (!equal) {
f09e2e
+			debug("Rekeyed credentials have different name");
f09e2e
+			return GSS_S_COMPLETE;
f09e2e
+		}
f09e2e
+
f09e2e
+		debug("Marking rekeyed credentials for export");
f09e2e
+
f09e2e
+		gss_release_name(&ctx->minor, &client->name);
f09e2e
+		gss_release_cred(&ctx->minor, &client->creds);
f09e2e
+		client->name = new_name;
f09e2e
+		client->creds = ctx->client_creds;
f09e2e
+        	ctx->client_creds = GSS_C_NO_CREDENTIAL;
f09e2e
+		client->updated = 1;
f09e2e
+		return GSS_S_COMPLETE;
f09e2e
+	}
f09e2e
 
f09e2e
 	client->mech = NULL;
f09e2e
 
017ff1
@@ -284,6 +361,13 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
f09e2e
 	if (client->mech == NULL)
f09e2e
 		return GSS_S_FAILURE;
f09e2e
 
f09e2e
+	if (ctx->client_creds &&
f09e2e
+	    (ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
f09e2e
+	     ctx->client_creds, ctx->oid, &client->name, NULL, NULL, NULL))) {
f09e2e
+		ssh_gssapi_error(ctx);
f09e2e
+		return (ctx->major);
f09e2e
+	}
f09e2e
+
f09e2e
 	if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
f09e2e
 	    &client->displayname, NULL))) {
f09e2e
 		ssh_gssapi_error(ctx);
017ff1
@@ -301,6 +385,8 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
f09e2e
 		return (ctx->major);
f09e2e
 	}
f09e2e
 
f09e2e
+	gss_release_buffer(&ctx->minor, &ename);
f09e2e
+
f09e2e
 	/* We can't copy this structure, so we just move the pointer to it */
f09e2e
 	client->creds = ctx->client_creds;
f09e2e
 	ctx->client_creds = GSS_C_NO_CREDENTIAL;
017ff1
@@ -311,11 +397,20 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
f09e2e
 void
f09e2e
 ssh_gssapi_cleanup_creds(void)
f09e2e
 {
f09e2e
-	if (gssapi_client.store.filename != NULL) {
f09e2e
-		/* Unlink probably isn't sufficient */
f09e2e
-		debug("removing gssapi cred file\"%s\"",
f09e2e
-		    gssapi_client.store.filename);
f09e2e
-		unlink(gssapi_client.store.filename);
f09e2e
+	krb5_ccache ccache = NULL;
f09e2e
+	krb5_error_code problem;
f09e2e
+
f09e2e
+	if (gssapi_client.store.data != NULL) {
f09e2e
+		if ((problem = krb5_cc_resolve(gssapi_client.store.data, gssapi_client.store.envval, &ccache))) {
f09e2e
+			debug("%s: krb5_cc_resolve(): %.100s", __func__,
f09e2e
+				krb5_get_err_text(gssapi_client.store.data, problem));
f09e2e
+		} else if ((problem = krb5_cc_destroy(gssapi_client.store.data, ccache))) {
f09e2e
+			debug("%s: krb5_cc_resolve(): %.100s", __func__,
f09e2e
+				krb5_get_err_text(gssapi_client.store.data, problem));
f09e2e
+		} else {
f09e2e
+			krb5_free_context(gssapi_client.store.data);
f09e2e
+			gssapi_client.store.data = NULL;
f09e2e
+		}
f09e2e
 	}
f09e2e
 }
f09e2e
 
017ff1
@@ -348,7 +443,7 @@ ssh_gssapi_do_child(char ***envp, u_int *envsizep)
f09e2e
 
f09e2e
 /* Privileged */
f09e2e
 int
f09e2e
-ssh_gssapi_userok(char *user)
f09e2e
+ssh_gssapi_userok(char *user, struct passwd *pw)
f09e2e
 {
f09e2e
 	OM_uint32 lmin;
f09e2e
 
017ff1
@@ -358,9 +453,11 @@ ssh_gssapi_userok(char *user)
f09e2e
 		return 0;
f09e2e
 	}
f09e2e
 	if (gssapi_client.mech && gssapi_client.mech->userok)
f09e2e
-		if ((*gssapi_client.mech->userok)(&gssapi_client, user))
f09e2e
+		if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
f09e2e
+			gssapi_client.used = 1;
f09e2e
+			gssapi_client.store.owner = pw;
f09e2e
 			return 1;
f09e2e
-		else {
f09e2e
+		} else {
f09e2e
 			/* Destroy delegated credentials if userok fails */
f09e2e
 			gss_release_buffer(&lmin, &gssapi_client.displayname);
f09e2e
 			gss_release_buffer(&lmin, &gssapi_client.exportedname);
017ff1
@@ -374,14 +471,90 @@ ssh_gssapi_userok(char *user)
f09e2e
 	return (0);
f09e2e
 }
f09e2e
 
f09e2e
-/* Privileged */
f09e2e
-OM_uint32
f09e2e
-ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
f09e2e
+/* These bits are only used for rekeying. The unpriviledged child is running 
f09e2e
+ * as the user, the monitor is root.
f09e2e
+ *
f09e2e
+ * In the child, we want to :
f09e2e
+ *    *) Ask the monitor to store our credentials into the store we specify
f09e2e
+ *    *) If it succeeds, maybe do a PAM update
f09e2e
+ */
f09e2e
+
f09e2e
+/* Stuff for PAM */
f09e2e
+
f09e2e
+#ifdef USE_PAM
f09e2e
+static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg, 
f09e2e
+    struct pam_response **resp, void *data)
f09e2e
 {
f09e2e
-	ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
f09e2e
-	    gssbuf, gssmic, NULL);
f09e2e
+	return (PAM_CONV_ERR);
f09e2e
+}
f09e2e
+#endif
f09e2e
 
f09e2e
-	return (ctx->major);
f09e2e
+void
f09e2e
+ssh_gssapi_rekey_creds() {
f09e2e
+	int ok;
f09e2e
+	int ret;
f09e2e
+#ifdef USE_PAM
f09e2e
+	pam_handle_t *pamh = NULL;
f09e2e
+	struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
f09e2e
+	char *envstr;
f09e2e
+#endif
f09e2e
+
f09e2e
+	if (gssapi_client.store.filename == NULL && 
f09e2e
+	    gssapi_client.store.envval == NULL &&
f09e2e
+	    gssapi_client.store.envvar == NULL)
f09e2e
+		return;
f09e2e
+ 
f09e2e
+	ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
f09e2e
+
f09e2e
+	if (!ok)
f09e2e
+		return;
f09e2e
+
f09e2e
+	debug("Rekeyed credentials stored successfully");
f09e2e
+
f09e2e
+	/* Actually managing to play with the ssh pam stack from here will
f09e2e
+	 * be next to impossible. In any case, we may want different options
f09e2e
+	 * for rekeying. So, use our own :)
f09e2e
+	 */
f09e2e
+#ifdef USE_PAM	
f09e2e
+	if (!use_privsep) {
f09e2e
+		debug("Not even going to try and do PAM with privsep disabled");
f09e2e
+		return;
f09e2e
+	}
f09e2e
+
f09e2e
+	ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
f09e2e
+ 	    &pamconv, &pamh);
f09e2e
+	if (ret)
f09e2e
+		return;
f09e2e
+
f09e2e
+	xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar, 
f09e2e
+	    gssapi_client.store.envval);
f09e2e
+
f09e2e
+	ret = pam_putenv(pamh, envstr);
f09e2e
+	if (!ret)
f09e2e
+		pam_setcred(pamh, PAM_REINITIALIZE_CRED);
f09e2e
+	pam_end(pamh, PAM_SUCCESS);
f09e2e
+#endif
f09e2e
+}
f09e2e
+
f09e2e
+int 
f09e2e
+ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
f09e2e
+	int ok = 0;
f09e2e
+
f09e2e
+	/* Check we've got credentials to store */
f09e2e
+	if (!gssapi_client.updated)
f09e2e
+		return 0;
f09e2e
+
f09e2e
+	gssapi_client.updated = 0;
f09e2e
+
f09e2e
+	temporarily_use_uid(gssapi_client.store.owner);
f09e2e
+	if (gssapi_client.mech && gssapi_client.mech->updatecreds)
f09e2e
+		ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
f09e2e
+	else
f09e2e
+		debug("No update function for this mechanism");
f09e2e
+
f09e2e
+	restore_uid();
f09e2e
+
f09e2e
+	return ok;
f09e2e
 }
f09e2e
 
f09e2e
 #endif
017ff1
diff --git a/kex.c b/kex.c
017ff1
index 74e2b86..bce2ab8 100644
017ff1
--- a/kex.c
017ff1
+++ b/kex.c
f09e2e
@@ -51,6 +51,10 @@
f09e2e
 #include "roaming.h"
017ff1
 #include "digest.h"
f09e2e
 
f09e2e
+#ifdef GSSAPI
f09e2e
+#include "ssh-gss.h"
f09e2e
+#endif
f09e2e
+
f09e2e
 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
f09e2e
 # if defined(HAVE_EVP_SHA256)
f09e2e
 # define evp_ssh_sha256 EVP_sha256
017ff1
@@ -90,6 +94,11 @@ static const struct kexalg kexalgs[] = {
017ff1
 #ifdef HAVE_EVP_SHA256
017ff1
 	{ KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
f09e2e
 #endif
f09e2e
+#ifdef GSSAPI
017ff1
+	{ KEX_GSS_GEX_SHA1_ID, KEX_GSS_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
017ff1
+	{ KEX_GSS_GRP1_SHA1_ID, KEX_GSS_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
017ff1
+	{ KEX_GSS_GRP14_SHA1_ID, KEX_GSS_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
f09e2e
+#endif
017ff1
 	{ NULL, -1, -1, -1},
f09e2e
 };
f09e2e
 
017ff1
@@ -119,6 +128,12 @@ kex_alg_by_name(const char *name)
f09e2e
 	for (k = kexalgs; k->name != NULL; k++) {
f09e2e
 		if (strcmp(k->name, name) == 0)
f09e2e
 			return k;
f09e2e
+#ifdef GSSAPI
f09e2e
+		if (strncmp(name, "gss-", 4) == 0) {
f09e2e
+			if (strncmp(k->name, name, strlen(k->name)) == 0)
f09e2e
+				return k;
f09e2e
+		}
f09e2e
+#endif
f09e2e
 	}
f09e2e
 	return NULL;
f09e2e
 }
017ff1
diff --git a/kex.h b/kex.h
017ff1
index c85680e..313bb51 100644
017ff1
--- a/kex.h
017ff1
+++ b/kex.h
017ff1
@@ -76,6 +76,11 @@ enum kex_exchange {
f09e2e
 	KEX_DH_GEX_SHA256,
f09e2e
 	KEX_ECDH_SHA2,
017ff1
 	KEX_C25519_SHA256,
017ff1
+#ifdef GSSAPI
f09e2e
+	KEX_GSS_GRP1_SHA1,
f09e2e
+	KEX_GSS_GRP14_SHA1,
f09e2e
+	KEX_GSS_GEX_SHA1,
017ff1
+#endif
f09e2e
 	KEX_MAX
f09e2e
 };
f09e2e
 
017ff1
@@ -135,6 +140,12 @@ struct Kex {
f09e2e
 	int	flags;
017ff1
 	int	hash_alg;
f09e2e
 	int	ec_nid;
f09e2e
+#ifdef GSSAPI
f09e2e
+	int	gss_deleg_creds;
f09e2e
+	int	gss_trust_dns;
f09e2e
+	char    *gss_host;
f09e2e
+	char	*gss_client;
f09e2e
+#endif
f09e2e
 	char	*client_version_string;
f09e2e
 	char	*server_version_string;
f09e2e
 	int	(*verify_host_key)(Key *);
017ff1
@@ -166,6 +177,10 @@ void	 kexecdh_client(Kex *);
f09e2e
 void	 kexecdh_server(Kex *);
017ff1
 void	 kexc25519_client(Kex *);
017ff1
 void	 kexc25519_server(Kex *);
f09e2e
+#ifdef GSSAPI
017ff1
+void	 kexgss_client(Kex *);
017ff1
+void	 kexgss_server(Kex *);
f09e2e
+#endif
f09e2e
 
f09e2e
 void
017ff1
 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
017ff1
diff --git a/kexgssc.c b/kexgssc.c
017ff1
new file mode 100644
017ff1
index 0000000..e90b567
017ff1
--- /dev/null
017ff1
+++ b/kexgssc.c
f09e2e
@@ -0,0 +1,334 @@
f09e2e
+/*
f09e2e
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
f09e2e
+ *
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
f09e2e
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f09e2e
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f09e2e
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f09e2e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f09e2e
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f09e2e
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f09e2e
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f09e2e
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ */
f09e2e
+
f09e2e
+#include "includes.h"
f09e2e
+
f09e2e
+#ifdef GSSAPI
f09e2e
+
f09e2e
+#include "includes.h"
f09e2e
+
f09e2e
+#include <openssl/crypto.h>
f09e2e
+#include <openssl/bn.h>
f09e2e
+
f09e2e
+#include <string.h>
f09e2e
+
f09e2e
+#include "xmalloc.h"
f09e2e
+#include "buffer.h"
f09e2e
+#include "ssh2.h"
f09e2e
+#include "key.h"
f09e2e
+#include "cipher.h"
f09e2e
+#include "kex.h"
f09e2e
+#include "log.h"
f09e2e
+#include "packet.h"
f09e2e
+#include "dh.h"
f09e2e
+
f09e2e
+#include "ssh-gss.h"
f09e2e
+
f09e2e
+void
f09e2e
+kexgss_client(Kex *kex) {
f09e2e
+	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
f09e2e
+	gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
f09e2e
+	Gssctxt *ctxt;
f09e2e
+	OM_uint32 maj_status, min_status, ret_flags;
f09e2e
+	u_int klen, kout, slen = 0, hashlen, strlen;
f09e2e
+	DH *dh; 
f09e2e
+	BIGNUM *dh_server_pub = NULL;
f09e2e
+	BIGNUM *shared_secret = NULL;
f09e2e
+	BIGNUM *p = NULL;
f09e2e
+	BIGNUM *g = NULL;	
f09e2e
+	u_char *kbuf, *hash;
f09e2e
+	u_char *serverhostkey = NULL;
f09e2e
+	u_char *empty = "";
f09e2e
+	char *msg;
f09e2e
+	char *lang;
f09e2e
+	int type = 0;
f09e2e
+	int first = 1;
f09e2e
+	int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
f09e2e
+
f09e2e
+	/* Initialise our GSSAPI world */	
f09e2e
+	ssh_gssapi_build_ctx(&ctxt);
f09e2e
+	if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type) 
f09e2e
+	    == GSS_C_NO_OID)
f09e2e
+		fatal("Couldn't identify host exchange");
f09e2e
+
f09e2e
+	if (ssh_gssapi_import_name(ctxt, kex->gss_host))
f09e2e
+		fatal("Couldn't import hostname");
f09e2e
+
f09e2e
+	if (kex->gss_client && 
f09e2e
+	    ssh_gssapi_client_identity(ctxt, kex->gss_client))
f09e2e
+		fatal("Couldn't acquire client credentials");
f09e2e
+
f09e2e
+	switch (kex->kex_type) {
f09e2e
+	case KEX_GSS_GRP1_SHA1:
f09e2e
+		dh = dh_new_group1();
f09e2e
+		break;
f09e2e
+	case KEX_GSS_GRP14_SHA1:
f09e2e
+		dh = dh_new_group14();
f09e2e
+		break;
f09e2e
+	case KEX_GSS_GEX_SHA1:
f09e2e
+		debug("Doing group exchange\n");
f09e2e
+		nbits = dh_estimate(kex->we_need * 8);
f09e2e
+		packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
f09e2e
+		packet_put_int(min);
f09e2e
+		packet_put_int(nbits);
f09e2e
+		packet_put_int(max);
f09e2e
+
f09e2e
+		packet_send();
f09e2e
+
f09e2e
+		packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
f09e2e
+
f09e2e
+		if ((p = BN_new()) == NULL)
f09e2e
+			fatal("BN_new() failed");
f09e2e
+		packet_get_bignum2(p);
f09e2e
+		if ((g = BN_new()) == NULL)
f09e2e
+			fatal("BN_new() failed");
f09e2e
+		packet_get_bignum2(g);
f09e2e
+		packet_check_eom();
f09e2e
+
f09e2e
+		if (BN_num_bits(p) < min || BN_num_bits(p) > max)
f09e2e
+			fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
f09e2e
+			    min, BN_num_bits(p), max);
f09e2e
+
f09e2e
+		dh = dh_new_group(g, p);
f09e2e
+		break;
f09e2e
+	default:
f09e2e
+		fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
f09e2e
+	}
f09e2e
+	
f09e2e
+	/* Step 1 - e is dh->pub_key */
f09e2e
+	dh_gen_key(dh, kex->we_need * 8);
f09e2e
+
f09e2e
+	/* This is f, we initialise it now to make life easier */
f09e2e
+	dh_server_pub = BN_new();
f09e2e
+	if (dh_server_pub == NULL)
f09e2e
+		fatal("dh_server_pub == NULL");
f09e2e
+
f09e2e
+	token_ptr = GSS_C_NO_BUFFER;
f09e2e
+			 
f09e2e
+	do {
f09e2e
+		debug("Calling gss_init_sec_context");
f09e2e
+		
f09e2e
+		maj_status = ssh_gssapi_init_ctx(ctxt,
f09e2e
+		    kex->gss_deleg_creds, token_ptr, &send_tok,
f09e2e
+		    &ret_flags);
f09e2e
+
f09e2e
+		if (GSS_ERROR(maj_status)) {
f09e2e
+			if (send_tok.length != 0) {
f09e2e
+				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
f09e2e
+				packet_put_string(send_tok.value,
f09e2e
+				    send_tok.length);
f09e2e
+			}
f09e2e
+			fatal("gss_init_context failed");
f09e2e
+		}
f09e2e
+
f09e2e
+		/* If we've got an old receive buffer get rid of it */
f09e2e
+		if (token_ptr != GSS_C_NO_BUFFER)
f09e2e
+			free(recv_tok.value);
f09e2e
+
f09e2e
+		if (maj_status == GSS_S_COMPLETE) {
f09e2e
+			/* If mutual state flag is not true, kex fails */
f09e2e
+			if (!(ret_flags & GSS_C_MUTUAL_FLAG))
f09e2e
+				fatal("Mutual authentication failed");
f09e2e
+
f09e2e
+			/* If integ avail flag is not true kex fails */
f09e2e
+			if (!(ret_flags & GSS_C_INTEG_FLAG))
f09e2e
+				fatal("Integrity check failed");
f09e2e
+		}
f09e2e
+
f09e2e
+		/* 
f09e2e
+		 * If we have data to send, then the last message that we
f09e2e
+		 * received cannot have been a 'complete'. 
f09e2e
+		 */
f09e2e
+		if (send_tok.length != 0) {
f09e2e
+			if (first) {
f09e2e
+				packet_start(SSH2_MSG_KEXGSS_INIT);
f09e2e
+				packet_put_string(send_tok.value,
f09e2e
+				    send_tok.length);
f09e2e
+				packet_put_bignum2(dh->pub_key);
f09e2e
+				first = 0;
f09e2e
+			} else {
f09e2e
+				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
f09e2e
+				packet_put_string(send_tok.value,
f09e2e
+				    send_tok.length);
f09e2e
+			}
f09e2e
+			packet_send();
f09e2e
+			gss_release_buffer(&min_status, &send_tok);
f09e2e
+
f09e2e
+			/* If we've sent them data, they should reply */
f09e2e
+			do {	
f09e2e
+				type = packet_read();
f09e2e
+				if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
f09e2e
+					debug("Received KEXGSS_HOSTKEY");
f09e2e
+					if (serverhostkey)
f09e2e
+						fatal("Server host key received more than once");
f09e2e
+					serverhostkey = 
f09e2e
+					    packet_get_string(&slen);
f09e2e
+				}
f09e2e
+			} while (type == SSH2_MSG_KEXGSS_HOSTKEY);
f09e2e
+
f09e2e
+			switch (type) {
f09e2e
+			case SSH2_MSG_KEXGSS_CONTINUE:
f09e2e
+				debug("Received GSSAPI_CONTINUE");
f09e2e
+				if (maj_status == GSS_S_COMPLETE) 
f09e2e
+					fatal("GSSAPI Continue received from server when complete");
f09e2e
+				recv_tok.value = packet_get_string(&strlen);
f09e2e
+				recv_tok.length = strlen; 
f09e2e
+				break;
f09e2e
+			case SSH2_MSG_KEXGSS_COMPLETE:
f09e2e
+				debug("Received GSSAPI_COMPLETE");
f09e2e
+				packet_get_bignum2(dh_server_pub);
f09e2e
+				msg_tok.value =  packet_get_string(&strlen);
f09e2e
+				msg_tok.length = strlen; 
f09e2e
+
f09e2e
+				/* Is there a token included? */
f09e2e
+				if (packet_get_char()) {
f09e2e
+					recv_tok.value=
f09e2e
+					    packet_get_string(&strlen);
f09e2e
+					recv_tok.length = strlen;
f09e2e
+					/* If we're already complete - protocol error */
f09e2e
+					if (maj_status == GSS_S_COMPLETE)
f09e2e
+						packet_disconnect("Protocol error: received token when complete");
f09e2e
+					} else {
f09e2e
+						/* No token included */
f09e2e
+						if (maj_status != GSS_S_COMPLETE)
f09e2e
+							packet_disconnect("Protocol error: did not receive final token");
f09e2e
+				}
f09e2e
+				break;
f09e2e
+			case SSH2_MSG_KEXGSS_ERROR:
f09e2e
+				debug("Received Error");
f09e2e
+				maj_status = packet_get_int();
f09e2e
+				min_status = packet_get_int();
f09e2e
+				msg = packet_get_string(NULL);
f09e2e
+				lang = packet_get_string(NULL);
f09e2e
+				fatal("GSSAPI Error: \n%.400s",msg);
f09e2e
+			default:
f09e2e
+				packet_disconnect("Protocol error: didn't expect packet type %d",
f09e2e
+		    		type);
f09e2e
+			}
f09e2e
+			token_ptr = &recv_tok;
f09e2e
+		} else {
f09e2e
+			/* No data, and not complete */
f09e2e
+			if (maj_status != GSS_S_COMPLETE)
f09e2e
+				fatal("Not complete, and no token output");
f09e2e
+		}
f09e2e
+	} while (maj_status & GSS_S_CONTINUE_NEEDED);
f09e2e
+
f09e2e
+	/* 
f09e2e
+	 * We _must_ have received a COMPLETE message in reply from the 
f09e2e
+	 * server, which will have set dh_server_pub and msg_tok 
f09e2e
+	 */
f09e2e
+
f09e2e
+	if (type != SSH2_MSG_KEXGSS_COMPLETE)
f09e2e
+		fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
f09e2e
+
f09e2e
+	/* Check f in range [1, p-1] */
f09e2e
+	if (!dh_pub_is_valid(dh, dh_server_pub))
f09e2e
+		packet_disconnect("bad server public DH value");
f09e2e
+
f09e2e
+	/* compute K=f^x mod p */
f09e2e
+	klen = DH_size(dh);
f09e2e
+	kbuf = xmalloc(klen);
f09e2e
+	kout = DH_compute_key(kbuf, dh_server_pub, dh);
f09e2e
+	if ((int)kout < 0)
f09e2e
+		fatal("DH_compute_key: failed");
f09e2e
+
f09e2e
+	shared_secret = BN_new();
f09e2e
+	if (shared_secret == NULL)
f09e2e
+		fatal("kexgss_client: BN_new failed");
f09e2e
+
f09e2e
+	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
f09e2e
+		fatal("kexdh_client: BN_bin2bn failed");
f09e2e
+
f09e2e
+	memset(kbuf, 0, klen);
f09e2e
+	free(kbuf);
f09e2e
+
f09e2e
+	switch (kex->kex_type) {
f09e2e
+	case KEX_GSS_GRP1_SHA1:
f09e2e
+	case KEX_GSS_GRP14_SHA1:
f09e2e
+		kex_dh_hash( kex->client_version_string, 
f09e2e
+		    kex->server_version_string,
f09e2e
+		    buffer_ptr(&kex->my), buffer_len(&kex->my),
f09e2e
+		    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
f09e2e
+		    (serverhostkey ? serverhostkey : empty), slen,
f09e2e
+		    dh->pub_key,	/* e */
f09e2e
+		    dh_server_pub,	/* f */
f09e2e
+		    shared_secret,	/* K */
f09e2e
+		    &hash, &hashlen
f09e2e
+		);
f09e2e
+		break;
f09e2e
+	case KEX_GSS_GEX_SHA1:
f09e2e
+		kexgex_hash(
017ff1
+		    kex->hash_alg,
f09e2e
+		    kex->client_version_string,
f09e2e
+		    kex->server_version_string,
f09e2e
+		    buffer_ptr(&kex->my), buffer_len(&kex->my),
f09e2e
+		    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
f09e2e
+		    (serverhostkey ? serverhostkey : empty), slen,
f09e2e
+ 		    min, nbits, max,
f09e2e
+		    dh->p, dh->g,
f09e2e
+		    dh->pub_key,
f09e2e
+		    dh_server_pub,
f09e2e
+		    shared_secret,
f09e2e
+		    &hash, &hashlen
f09e2e
+		);
f09e2e
+		break;
f09e2e
+	default:
f09e2e
+		fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
f09e2e
+	}
f09e2e
+
f09e2e
+	gssbuf.value = hash;
f09e2e
+	gssbuf.length = hashlen;
f09e2e
+
f09e2e
+	/* Verify that the hash matches the MIC we just got. */
f09e2e
+	if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
f09e2e
+		packet_disconnect("Hash's MIC didn't verify");
f09e2e
+
f09e2e
+	free(msg_tok.value);
f09e2e
+
f09e2e
+	DH_free(dh);
f09e2e
+	if (serverhostkey)
f09e2e
+		free(serverhostkey);
f09e2e
+	BN_clear_free(dh_server_pub);
f09e2e
+
f09e2e
+	/* save session id */
f09e2e
+	if (kex->session_id == NULL) {
f09e2e
+		kex->session_id_len = hashlen;
f09e2e
+		kex->session_id = xmalloc(kex->session_id_len);
f09e2e
+		memcpy(kex->session_id, hash, kex->session_id_len);
f09e2e
+	}
f09e2e
+
f09e2e
+	if (kex->gss_deleg_creds)
f09e2e
+		ssh_gssapi_credentials_updated(ctxt);
f09e2e
+
f09e2e
+	if (gss_kex_context == NULL)
f09e2e
+		gss_kex_context = ctxt;
f09e2e
+	else
f09e2e
+		ssh_gssapi_delete_ctx(&ctxt);
f09e2e
+
017ff1
+	kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
f09e2e
+	BN_clear_free(shared_secret);
f09e2e
+	kex_finish(kex);
f09e2e
+}
f09e2e
+
f09e2e
+#endif /* GSSAPI */
017ff1
diff --git a/kexgsss.c b/kexgsss.c
017ff1
new file mode 100644
017ff1
index 0000000..6d7518c
017ff1
--- /dev/null
017ff1
+++ b/kexgsss.c
f09e2e
@@ -0,0 +1,288 @@
f09e2e
+/*
f09e2e
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
f09e2e
+ *
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
f09e2e
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f09e2e
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f09e2e
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f09e2e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f09e2e
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f09e2e
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f09e2e
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f09e2e
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ */
f09e2e
+
f09e2e
+#include "includes.h"
f09e2e
+
f09e2e
+#ifdef GSSAPI
f09e2e
+
f09e2e
+#include <string.h>
f09e2e
+
f09e2e
+#include <openssl/crypto.h>
f09e2e
+#include <openssl/bn.h>
f09e2e
+
f09e2e
+#include "xmalloc.h"
f09e2e
+#include "buffer.h"
f09e2e
+#include "ssh2.h"
f09e2e
+#include "key.h"
f09e2e
+#include "cipher.h"
f09e2e
+#include "kex.h"
f09e2e
+#include "log.h"
f09e2e
+#include "packet.h"
f09e2e
+#include "dh.h"
f09e2e
+#include "ssh-gss.h"
f09e2e
+#include "monitor_wrap.h"
f09e2e
+#include "servconf.h"
f09e2e
+
f09e2e
+extern ServerOptions options;
f09e2e
+
f09e2e
+void
f09e2e
+kexgss_server(Kex *kex)
f09e2e
+{
f09e2e
+	OM_uint32 maj_status, min_status;
f09e2e
+	
f09e2e
+	/* 
f09e2e
+	 * Some GSSAPI implementations use the input value of ret_flags (an
f09e2e
+ 	 * output variable) as a means of triggering mechanism specific 
f09e2e
+ 	 * features. Initializing it to zero avoids inadvertently 
f09e2e
+ 	 * activating this non-standard behaviour.
f09e2e
+	 */
f09e2e
+
f09e2e
+	OM_uint32 ret_flags = 0;
f09e2e
+	gss_buffer_desc gssbuf, recv_tok, msg_tok;
f09e2e
+	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
f09e2e
+	Gssctxt *ctxt = NULL;
f09e2e
+	u_int slen, klen, kout, hashlen;
f09e2e
+	u_char *kbuf, *hash;
f09e2e
+	DH *dh;
f09e2e
+	int min = -1, max = -1, nbits = -1;
f09e2e
+	BIGNUM *shared_secret = NULL;
f09e2e
+	BIGNUM *dh_client_pub = NULL;
f09e2e
+	int type = 0;
f09e2e
+	gss_OID oid;
f09e2e
+	char *mechs;
f09e2e
+
f09e2e
+	/* Initialise GSSAPI */
f09e2e
+
f09e2e
+	/* If we're rekeying, privsep means that some of the private structures
f09e2e
+	 * in the GSSAPI code are no longer available. This kludges them back
f09e2e
+	 * into life
f09e2e
+	 */
f09e2e
+	if (!ssh_gssapi_oid_table_ok()) 
f09e2e
+		if ((mechs = ssh_gssapi_server_mechanisms()))
f09e2e
+			free(mechs);
f09e2e
+
f09e2e
+	debug2("%s: Identifying %s", __func__, kex->name);
f09e2e
+	oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type);
f09e2e
+	if (oid == GSS_C_NO_OID)
f09e2e
+	   fatal("Unknown gssapi mechanism");
f09e2e
+
f09e2e
+	debug2("%s: Acquiring credentials", __func__);
f09e2e
+
f09e2e
+	if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
f09e2e
+		fatal("Unable to acquire credentials for the server");
f09e2e
+
f09e2e
+	switch (kex->kex_type) {
f09e2e
+	case KEX_GSS_GRP1_SHA1:
f09e2e
+		dh = dh_new_group1();
f09e2e
+		break;
f09e2e
+	case KEX_GSS_GRP14_SHA1:
f09e2e
+		dh = dh_new_group14();
f09e2e
+		break;
f09e2e
+	case KEX_GSS_GEX_SHA1:
f09e2e
+		debug("Doing group exchange");
f09e2e
+		packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ);
f09e2e
+		min = packet_get_int();
f09e2e
+		nbits = packet_get_int();
f09e2e
+		max = packet_get_int();
f09e2e
+		min = MAX(DH_GRP_MIN, min);
f09e2e
+		max = MIN(DH_GRP_MAX, max);
f09e2e
+		packet_check_eom();
f09e2e
+		if (max < min || nbits < min || max < nbits)
f09e2e
+			fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
f09e2e
+			    min, nbits, max);
f09e2e
+		dh = PRIVSEP(choose_dh(min, nbits, max));
f09e2e
+		if (dh == NULL)
f09e2e
+			packet_disconnect("Protocol error: no matching group found");
f09e2e
+
f09e2e
+		packet_start(SSH2_MSG_KEXGSS_GROUP);
f09e2e
+		packet_put_bignum2(dh->p);
f09e2e
+		packet_put_bignum2(dh->g);
f09e2e
+		packet_send();
f09e2e
+
f09e2e
+		packet_write_wait();
f09e2e
+		break;
f09e2e
+	default:
f09e2e
+		fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
f09e2e
+	}
f09e2e
+
f09e2e
+	dh_gen_key(dh, kex->we_need * 8);
f09e2e
+
f09e2e
+	do {
f09e2e
+		debug("Wait SSH2_MSG_GSSAPI_INIT");
f09e2e
+		type = packet_read();
f09e2e
+		switch(type) {
f09e2e
+		case SSH2_MSG_KEXGSS_INIT:
f09e2e
+			if (dh_client_pub != NULL) 
f09e2e
+				fatal("Received KEXGSS_INIT after initialising");
f09e2e
+			recv_tok.value = packet_get_string(&slen);
f09e2e
+			recv_tok.length = slen; 
f09e2e
+
f09e2e
+			if ((dh_client_pub = BN_new()) == NULL)
f09e2e
+				fatal("dh_client_pub == NULL");
f09e2e
+
f09e2e
+			packet_get_bignum2(dh_client_pub);
f09e2e
+
f09e2e
+			/* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
f09e2e
+			break;
f09e2e
+		case SSH2_MSG_KEXGSS_CONTINUE:
f09e2e
+			recv_tok.value = packet_get_string(&slen);
f09e2e
+			recv_tok.length = slen; 
f09e2e
+			break;
f09e2e
+		default:
f09e2e
+			packet_disconnect(
f09e2e
+			    "Protocol error: didn't expect packet type %d",
f09e2e
+			    type);
f09e2e
+		}
f09e2e
+
f09e2e
+		maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, 
f09e2e
+		    &send_tok, &ret_flags));
f09e2e
+
f09e2e
+		free(recv_tok.value);
f09e2e
+
f09e2e
+		if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
f09e2e
+			fatal("Zero length token output when incomplete");
f09e2e
+
f09e2e
+		if (dh_client_pub == NULL)
f09e2e
+			fatal("No client public key");
f09e2e
+		
f09e2e
+		if (maj_status & GSS_S_CONTINUE_NEEDED) {
f09e2e
+			debug("Sending GSSAPI_CONTINUE");
f09e2e
+			packet_start(SSH2_MSG_KEXGSS_CONTINUE);
f09e2e
+			packet_put_string(send_tok.value, send_tok.length);
f09e2e
+			packet_send();
f09e2e
+			gss_release_buffer(&min_status, &send_tok);
f09e2e
+		}
f09e2e
+	} while (maj_status & GSS_S_CONTINUE_NEEDED);
f09e2e
+
f09e2e
+	if (GSS_ERROR(maj_status)) {
f09e2e
+		if (send_tok.length > 0) {
f09e2e
+			packet_start(SSH2_MSG_KEXGSS_CONTINUE);
f09e2e
+			packet_put_string(send_tok.value, send_tok.length);
f09e2e
+			packet_send();
f09e2e
+		}
f09e2e
+		fatal("accept_ctx died");
f09e2e
+	}
f09e2e
+
f09e2e
+	if (!(ret_flags & GSS_C_MUTUAL_FLAG))
f09e2e
+		fatal("Mutual Authentication flag wasn't set");
f09e2e
+
f09e2e
+	if (!(ret_flags & GSS_C_INTEG_FLAG))
f09e2e
+		fatal("Integrity flag wasn't set");
f09e2e
+	
f09e2e
+	if (!dh_pub_is_valid(dh, dh_client_pub))
f09e2e
+		packet_disconnect("bad client public DH value");
f09e2e
+
f09e2e
+	klen = DH_size(dh);
f09e2e
+	kbuf = xmalloc(klen); 
f09e2e
+	kout = DH_compute_key(kbuf, dh_client_pub, dh);
f09e2e
+	if ((int)kout < 0)
f09e2e
+		fatal("DH_compute_key: failed");
f09e2e
+
f09e2e
+	shared_secret = BN_new();
f09e2e
+	if (shared_secret == NULL)
f09e2e
+		fatal("kexgss_server: BN_new failed");
f09e2e
+
f09e2e
+	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
f09e2e
+		fatal("kexgss_server: BN_bin2bn failed");
f09e2e
+
f09e2e
+	memset(kbuf, 0, klen);
f09e2e
+	free(kbuf);
f09e2e
+
f09e2e
+	switch (kex->kex_type) {
f09e2e
+	case KEX_GSS_GRP1_SHA1:
f09e2e
+	case KEX_GSS_GRP14_SHA1:
f09e2e
+		kex_dh_hash(
f09e2e
+		    kex->client_version_string, kex->server_version_string,
f09e2e
+		    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
f09e2e
+		    buffer_ptr(&kex->my), buffer_len(&kex->my),
f09e2e
+		    NULL, 0, /* Change this if we start sending host keys */
f09e2e
+		    dh_client_pub, dh->pub_key, shared_secret,
f09e2e
+		    &hash, &hashlen
f09e2e
+		);
f09e2e
+		break;
f09e2e
+	case KEX_GSS_GEX_SHA1:
f09e2e
+		kexgex_hash(
017ff1
+		    kex->hash_alg,
f09e2e
+		    kex->client_version_string, kex->server_version_string,
f09e2e
+		    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
f09e2e
+		    buffer_ptr(&kex->my), buffer_len(&kex->my),
f09e2e
+		    NULL, 0,
f09e2e
+		    min, nbits, max,
f09e2e
+		    dh->p, dh->g,
f09e2e
+		    dh_client_pub,
f09e2e
+		    dh->pub_key,
f09e2e
+		    shared_secret,
f09e2e
+		    &hash, &hashlen
f09e2e
+		);
f09e2e
+		break;
f09e2e
+	default:
f09e2e
+		fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
f09e2e
+	}
f09e2e
+
f09e2e
+	BN_clear_free(dh_client_pub);
f09e2e
+
f09e2e
+	if (kex->session_id == NULL) {
f09e2e
+		kex->session_id_len = hashlen;
f09e2e
+		kex->session_id = xmalloc(kex->session_id_len);
f09e2e
+		memcpy(kex->session_id, hash, kex->session_id_len);
f09e2e
+	}
f09e2e
+
f09e2e
+	gssbuf.value = hash;
f09e2e
+	gssbuf.length = hashlen;
f09e2e
+
f09e2e
+	if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
f09e2e
+		fatal("Couldn't get MIC");
f09e2e
+
f09e2e
+	packet_start(SSH2_MSG_KEXGSS_COMPLETE);
f09e2e
+	packet_put_bignum2(dh->pub_key);
f09e2e
+	packet_put_string(msg_tok.value,msg_tok.length);
f09e2e
+
f09e2e
+	if (send_tok.length != 0) {
f09e2e
+		packet_put_char(1); /* true */
f09e2e
+		packet_put_string(send_tok.value, send_tok.length);
f09e2e
+	} else {
f09e2e
+		packet_put_char(0); /* false */
f09e2e
+	}
f09e2e
+	packet_send();
f09e2e
+
f09e2e
+	gss_release_buffer(&min_status, &send_tok);
f09e2e
+	gss_release_buffer(&min_status, &msg_tok);
f09e2e
+
f09e2e
+	if (gss_kex_context == NULL)
f09e2e
+		gss_kex_context = ctxt;
f09e2e
+	else 
f09e2e
+		ssh_gssapi_delete_ctx(&ctxt);
f09e2e
+
f09e2e
+	DH_free(dh);
f09e2e
+
017ff1
+	kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
f09e2e
+	BN_clear_free(shared_secret);
f09e2e
+	kex_finish(kex);
f09e2e
+
f09e2e
+	/* If this was a rekey, then save out any delegated credentials we
f09e2e
+	 * just exchanged.  */
f09e2e
+	if (options.gss_store_rekey)
f09e2e
+		ssh_gssapi_rekey_creds();
f09e2e
+}
f09e2e
+#endif /* GSSAPI */
017ff1
diff --git a/key.c b/key.c
017ff1
index eb98ea8..900b9e3 100644
017ff1
--- a/key.c
017ff1
+++ b/key.c
017ff1
@@ -1013,6 +1013,7 @@ static const struct keytype keytypes[] = {
f09e2e
 	    KEY_DSA_CERT_V00, 0, 1 },
017ff1
 	{ "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT",
017ff1
 	    KEY_ED25519_CERT, 0, 1 },
f09e2e
+	{ "null", "null", KEY_NULL, 0, 0 },
f09e2e
 	{ NULL, NULL, -1, -1, 0 }
f09e2e
 };
f09e2e
 
017ff1
diff --git a/key.h b/key.h
017ff1
index 0e3eea5..d51ed81 100644
017ff1
--- a/key.h
017ff1
+++ b/key.h
017ff1
@@ -46,6 +46,7 @@ enum types {
017ff1
 	KEY_ED25519_CERT,
f09e2e
 	KEY_RSA_CERT_V00,
f09e2e
 	KEY_DSA_CERT_V00,
f09e2e
+	KEY_NULL,
f09e2e
 	KEY_UNSPEC
f09e2e
 };
f09e2e
 enum fp_type {
017ff1
diff --git a/monitor.c b/monitor.c
017ff1
index 229fada..aa70945 100644
017ff1
--- a/monitor.c
017ff1
+++ b/monitor.c
017ff1
@@ -178,6 +178,8 @@ int mm_answer_gss_setup_ctx(int, Buffer *);
f09e2e
 int mm_answer_gss_accept_ctx(int, Buffer *);
f09e2e
 int mm_answer_gss_userok(int, Buffer *);
f09e2e
 int mm_answer_gss_checkmic(int, Buffer *);
f09e2e
+int mm_answer_gss_sign(int, Buffer *);
f09e2e
+int mm_answer_gss_updatecreds(int, Buffer *);
f09e2e
 #endif
f09e2e
 
f09e2e
 #ifdef SSH_AUDIT_EVENTS
017ff1
@@ -253,11 +255,18 @@ struct mon_table mon_dispatch_proto20[] = {
f09e2e
     {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
f09e2e
     {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
f09e2e
     {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
f09e2e
+    {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
f09e2e
 #endif
017ff1
     {0, 0, NULL}
f09e2e
 };
f09e2e
 
f09e2e
 struct mon_table mon_dispatch_postauth20[] = {
f09e2e
+#ifdef GSSAPI
f09e2e
+    {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
f09e2e
+    {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
f09e2e
+    {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
f09e2e
+    {MONITOR_REQ_GSSUPCREDS, 0, mm_answer_gss_updatecreds},
f09e2e
+#endif
f09e2e
     {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
f09e2e
     {MONITOR_REQ_SIGN, 0, mm_answer_sign},
f09e2e
     {MONITOR_REQ_PTY, 0, mm_answer_pty},
017ff1
@@ -366,6 +375,10 @@ monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
f09e2e
 		/* Permit requests for moduli and signatures */
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
f09e2e
+#ifdef GSSAPI
f09e2e
+		/* and for the GSSAPI key exchange */
f09e2e
+		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
f09e2e
+#endif
f09e2e
 	} else {
f09e2e
 		mon_dispatch = mon_dispatch_proto15;
f09e2e
 
017ff1
@@ -471,6 +484,10 @@ monitor_child_postauth(struct monitor *pmonitor)
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
f09e2e
+#ifdef GSSAPI
f09e2e
+		/* and for the GSSAPI key exchange */
f09e2e
+		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
f09e2e
+#endif		
f09e2e
 	} else {
f09e2e
 		mon_dispatch = mon_dispatch_postauth15;
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
017ff1
@@ -1866,6 +1883,13 @@ mm_get_kex(Buffer *m)
f09e2e
 	kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
f09e2e
 	kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
017ff1
 	kex->kex[KEX_C25519_SHA256] = kexc25519_server;
f09e2e
+#ifdef GSSAPI
f09e2e
+	if (options.gss_keyex) {
f09e2e
+		kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
f09e2e
+		kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
f09e2e
+		kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
f09e2e
+	}
f09e2e
+#endif
f09e2e
 	kex->server = 1;
f09e2e
 	kex->hostkey_type = buffer_get_int(m);
f09e2e
 	kex->kex_type = buffer_get_int(m);
017ff1
@@ -2073,6 +2097,9 @@ mm_answer_gss_setup_ctx(int sock, Buffer *m)
f09e2e
 	OM_uint32 major;
f09e2e
 	u_int len;
f09e2e
 
f09e2e
+	if (!options.gss_authentication && !options.gss_keyex)
f09e2e
+		fatal("In GSSAPI monitor when GSSAPI is disabled");
f09e2e
+
f09e2e
 	goid.elements = buffer_get_string(m, &len;;
f09e2e
 	goid.length = len;
f09e2e
 
017ff1
@@ -2100,6 +2127,9 @@ mm_answer_gss_accept_ctx(int sock, Buffer *m)
f09e2e
 	OM_uint32 flags = 0; /* GSI needs this */
f09e2e
 	u_int len;
f09e2e
 
f09e2e
+	if (!options.gss_authentication && !options.gss_keyex)
f09e2e
+		fatal("In GSSAPI monitor when GSSAPI is disabled");
f09e2e
+
f09e2e
 	in.value = buffer_get_string(m, &len;;
f09e2e
 	in.length = len;
f09e2e
 	major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
017ff1
@@ -2117,6 +2147,7 @@ mm_answer_gss_accept_ctx(int sock, Buffer *m)
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
f09e2e
+		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
f09e2e
 	}
f09e2e
 	return (0);
f09e2e
 }
017ff1
@@ -2128,6 +2159,9 @@ mm_answer_gss_checkmic(int sock, Buffer *m)
f09e2e
 	OM_uint32 ret;
f09e2e
 	u_int len;
f09e2e
 
f09e2e
+	if (!options.gss_authentication && !options.gss_keyex)
f09e2e
+		fatal("In GSSAPI monitor when GSSAPI is disabled");
f09e2e
+
f09e2e
 	gssbuf.value = buffer_get_string(m, &len;;
f09e2e
 	gssbuf.length = len;
f09e2e
 	mic.value = buffer_get_string(m, &len;;
017ff1
@@ -2154,7 +2188,11 @@ mm_answer_gss_userok(int sock, Buffer *m)
f09e2e
 {
f09e2e
 	int authenticated;
f09e2e
 
f09e2e
-	authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
f09e2e
+	if (!options.gss_authentication && !options.gss_keyex)
f09e2e
+		fatal("In GSSAPI monitor when GSSAPI is disabled");
f09e2e
+
f09e2e
+	authenticated = authctxt->valid && 
f09e2e
+	    ssh_gssapi_userok(authctxt->user, authctxt->pw);
f09e2e
 
f09e2e
 	buffer_clear(m);
f09e2e
 	buffer_put_int(m, authenticated);
017ff1
@@ -2167,5 +2205,73 @@ mm_answer_gss_userok(int sock, Buffer *m)
f09e2e
 	/* Monitor loop will terminate if authenticated */
f09e2e
 	return (authenticated);
f09e2e
 }
f09e2e
+
f09e2e
+int 
f09e2e
+mm_answer_gss_sign(int socket, Buffer *m)
f09e2e
+{
f09e2e
+	gss_buffer_desc data;
f09e2e
+	gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
f09e2e
+	OM_uint32 major, minor;
f09e2e
+	u_int len;
f09e2e
+
f09e2e
+	if (!options.gss_authentication && !options.gss_keyex)
f09e2e
+		fatal("In GSSAPI monitor when GSSAPI is disabled");
f09e2e
+
f09e2e
+	data.value = buffer_get_string(m, &len;;
f09e2e
+	data.length = len;
f09e2e
+	if (data.length != 20) 
f09e2e
+		fatal("%s: data length incorrect: %d", __func__, 
f09e2e
+		    (int) data.length);
f09e2e
+
f09e2e
+	/* Save the session ID on the first time around */
f09e2e
+	if (session_id2_len == 0) {
f09e2e
+		session_id2_len = data.length;
f09e2e
+		session_id2 = xmalloc(session_id2_len);
f09e2e
+		memcpy(session_id2, data.value, session_id2_len);
f09e2e
+	}
f09e2e
+	major = ssh_gssapi_sign(gsscontext, &data, &hash);
f09e2e
+
f09e2e
+	free(data.value);
f09e2e
+
f09e2e
+	buffer_clear(m);
f09e2e
+	buffer_put_int(m, major);
f09e2e
+	buffer_put_string(m, hash.value, hash.length);
f09e2e
+
f09e2e
+	mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
f09e2e
+
f09e2e
+	gss_release_buffer(&minor, &hash);
f09e2e
+
f09e2e
+	/* Turn on getpwnam permissions */
f09e2e
+	monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
f09e2e
+	
f09e2e
+	/* And credential updating, for when rekeying */
f09e2e
+	monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
f09e2e
+
f09e2e
+	return (0);
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_answer_gss_updatecreds(int socket, Buffer *m) {
f09e2e
+	ssh_gssapi_ccache store;
f09e2e
+	int ok;
f09e2e
+
f09e2e
+	store.filename = buffer_get_string(m, NULL);
f09e2e
+	store.envvar   = buffer_get_string(m, NULL);
f09e2e
+	store.envval   = buffer_get_string(m, NULL);
f09e2e
+
f09e2e
+	ok = ssh_gssapi_update_creds(&store);
f09e2e
+
f09e2e
+	free(store.filename);
f09e2e
+	free(store.envvar);
f09e2e
+	free(store.envval);
f09e2e
+
f09e2e
+	buffer_clear(m);
f09e2e
+	buffer_put_int(m, ok);
f09e2e
+
f09e2e
+	mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
f09e2e
+
f09e2e
+	return(0);
f09e2e
+}
f09e2e
+
f09e2e
 #endif /* GSSAPI */
f09e2e
 
017ff1
diff --git a/monitor.h b/monitor.h
017ff1
index 20e2b4a..ff79fbb 100644
017ff1
--- a/monitor.h
017ff1
+++ b/monitor.h
017ff1
@@ -60,6 +60,8 @@ enum monitor_reqtype {
f09e2e
 #ifdef WITH_SELINUX
f09e2e
 	MONITOR_REQ_AUTHROLE = 80,
f09e2e
 #endif
f09e2e
+	MONITOR_REQ_GSSSIGN = 82, MONITOR_ANS_GSSSIGN = 83,
f09e2e
+	MONITOR_REQ_GSSUPCREDS = 84, MONITOR_ANS_GSSUPCREDS = 85,
f09e2e
 
f09e2e
 	MONITOR_REQ_PAM_START = 100,
f09e2e
 	MONITOR_REQ_PAM_ACCOUNT = 102, MONITOR_ANS_PAM_ACCOUNT = 103,
017ff1
diff --git a/monitor_wrap.c b/monitor_wrap.c
017ff1
index d1b6d99..d1e1caa 100644
017ff1
--- a/monitor_wrap.c
017ff1
+++ b/monitor_wrap.c
017ff1
@@ -1290,7 +1290,7 @@ mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
f09e2e
 }
f09e2e
 
f09e2e
 int
f09e2e
-mm_ssh_gssapi_userok(char *user)
f09e2e
+mm_ssh_gssapi_userok(char *user, struct passwd *pw)
f09e2e
 {
f09e2e
 	Buffer m;
f09e2e
 	int authenticated = 0;
017ff1
@@ -1307,5 +1307,50 @@ mm_ssh_gssapi_userok(char *user)
f09e2e
 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
f09e2e
 	return (authenticated);
f09e2e
 }
f09e2e
+
f09e2e
+OM_uint32
f09e2e
+mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+	OM_uint32 major;
f09e2e
+	u_int len;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_string(&m, data->value, data->length);
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, &m);
f09e2e
+
f09e2e
+	major = buffer_get_int(&m);
f09e2e
+	hash->value = buffer_get_string(&m, &len;;
f09e2e
+	hash->length = len;
f09e2e
+
f09e2e
+	buffer_free(&m);
f09e2e
+
f09e2e
+	return(major);
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+	int ok;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+
f09e2e
+	buffer_put_cstring(&m, store->filename ? store->filename : "");
f09e2e
+	buffer_put_cstring(&m, store->envvar ? store->envvar : "");
f09e2e
+	buffer_put_cstring(&m, store->envval ? store->envval : "");
017ff1
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUPCREDS, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUPCREDS, &m);
f09e2e
+
f09e2e
+	ok = buffer_get_int(&m);
f09e2e
+
f09e2e
+	buffer_free(&m);
017ff1
+
f09e2e
+	return (ok);
f09e2e
+}
f09e2e
+
f09e2e
 #endif /* GSSAPI */
f09e2e
 
017ff1
diff --git a/monitor_wrap.h b/monitor_wrap.h
017ff1
index 9d5e5ba..93929e0 100644
017ff1
--- a/monitor_wrap.h
017ff1
+++ b/monitor_wrap.h
017ff1
@@ -61,8 +61,10 @@ BIGNUM *mm_auth_rsa_generate_challenge(Key *);
f09e2e
 OM_uint32 mm_ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
f09e2e
 OM_uint32 mm_ssh_gssapi_accept_ctx(Gssctxt *,
f09e2e
    gss_buffer_desc *, gss_buffer_desc *, OM_uint32 *);
f09e2e
-int mm_ssh_gssapi_userok(char *user);
f09e2e
+int mm_ssh_gssapi_userok(char *user, struct passwd *);
f09e2e
 OM_uint32 mm_ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
f09e2e
+OM_uint32 mm_ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
f09e2e
+int mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *);
f09e2e
 #endif
f09e2e
 
f09e2e
 #ifdef USE_PAM
017ff1
diff --git a/readconf.c b/readconf.c
017ff1
index dc884c9..7613ff2 100644
017ff1
--- a/readconf.c
017ff1
+++ b/readconf.c
017ff1
@@ -141,6 +141,8 @@ typedef enum {
f09e2e
 	oClearAllForwardings, oNoHostAuthenticationForLocalhost,
f09e2e
 	oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
f09e2e
 	oAddressFamily, oGssAuthentication, oGssDelegateCreds,
f09e2e
+	oGssTrustDns, oGssKeyEx, oGssClientIdentity, oGssRenewalRekey,
f09e2e
+	oGssServerIdentity, 
f09e2e
 	oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
f09e2e
 	oSendEnv, oControlPath, oControlMaster, oControlPersist,
f09e2e
 	oHashKnownHosts,
017ff1
@@ -183,10 +185,19 @@ static struct {
f09e2e
 	{ "afstokenpassing", oUnsupported },
f09e2e
 #if defined(GSSAPI)
f09e2e
 	{ "gssapiauthentication", oGssAuthentication },
f09e2e
+	{ "gssapikeyexchange", oGssKeyEx },
f09e2e
 	{ "gssapidelegatecredentials", oGssDelegateCreds },
f09e2e
+	{ "gssapitrustdns", oGssTrustDns },
f09e2e
+	{ "gssapiclientidentity", oGssClientIdentity },
f09e2e
+	{ "gssapiserveridentity", oGssServerIdentity },
f09e2e
+	{ "gssapirenewalforcesrekey", oGssRenewalRekey },
f09e2e
 #else
f09e2e
 	{ "gssapiauthentication", oUnsupported },
f09e2e
+	{ "gssapikeyexchange", oUnsupported },
f09e2e
 	{ "gssapidelegatecredentials", oUnsupported },
f09e2e
+	{ "gssapitrustdns", oUnsupported },
f09e2e
+	{ "gssapiclientidentity", oUnsupported },
f09e2e
+	{ "gssapirenewalforcesrekey", oUnsupported },
f09e2e
 #endif
f09e2e
 	{ "fallbacktorsh", oDeprecated },
f09e2e
 	{ "usersh", oDeprecated },
017ff1
@@ -841,10 +852,30 @@ parse_time:
f09e2e
 		intptr = &options->gss_authentication;
f09e2e
 		goto parse_flag;
f09e2e
 
f09e2e
+	case oGssKeyEx:
f09e2e
+		intptr = &options->gss_keyex;
f09e2e
+		goto parse_flag;
f09e2e
+
f09e2e
 	case oGssDelegateCreds:
f09e2e
 		intptr = &options->gss_deleg_creds;
f09e2e
 		goto parse_flag;
f09e2e
 
f09e2e
+	case oGssTrustDns:
f09e2e
+		intptr = &options->gss_trust_dns;
f09e2e
+		goto parse_flag;
f09e2e
+
f09e2e
+	case oGssClientIdentity:
f09e2e
+		charptr = &options->gss_client_identity;
f09e2e
+		goto parse_string;
f09e2e
+
f09e2e
+	case oGssServerIdentity:
f09e2e
+		charptr = &options->gss_server_identity;
f09e2e
+		goto parse_string;
f09e2e
+
f09e2e
+	case oGssRenewalRekey:
f09e2e
+		intptr = &options->gss_renewal_rekey;
f09e2e
+		goto parse_flag;
f09e2e
+
f09e2e
 	case oBatchMode:
f09e2e
 		intptr = &options->batch_mode;
f09e2e
 		goto parse_flag;
017ff1
@@ -1497,7 +1528,12 @@ initialize_options(Options * options)
f09e2e
 	options->pubkey_authentication = -1;
f09e2e
 	options->challenge_response_authentication = -1;
f09e2e
 	options->gss_authentication = -1;
f09e2e
+	options->gss_keyex = -1;
f09e2e
 	options->gss_deleg_creds = -1;
f09e2e
+	options->gss_trust_dns = -1;
f09e2e
+	options->gss_renewal_rekey = -1;
f09e2e
+	options->gss_client_identity = NULL;
f09e2e
+	options->gss_server_identity = NULL;
f09e2e
 	options->password_authentication = -1;
f09e2e
 	options->kbd_interactive_authentication = -1;
f09e2e
 	options->kbd_interactive_devices = NULL;
017ff1
@@ -1616,8 +1652,14 @@ fill_default_options(Options * options)
f09e2e
 		options->challenge_response_authentication = 1;
f09e2e
 	if (options->gss_authentication == -1)
f09e2e
 		options->gss_authentication = 0;
f09e2e
+	if (options->gss_keyex == -1)
f09e2e
+		options->gss_keyex = 0;
f09e2e
 	if (options->gss_deleg_creds == -1)
f09e2e
 		options->gss_deleg_creds = 0;
f09e2e
+	if (options->gss_trust_dns == -1)
f09e2e
+		options->gss_trust_dns = 0;
f09e2e
+	if (options->gss_renewal_rekey == -1)
f09e2e
+		options->gss_renewal_rekey = 0;
f09e2e
 	if (options->password_authentication == -1)
f09e2e
 		options->password_authentication = 1;
f09e2e
 	if (options->kbd_interactive_authentication == -1)
017ff1
diff --git a/readconf.h b/readconf.h
017ff1
index 75e3f8f..5cc97f0 100644
017ff1
--- a/readconf.h
017ff1
+++ b/readconf.h
017ff1
@@ -54,7 +54,12 @@ typedef struct {
f09e2e
 	int     challenge_response_authentication;
f09e2e
 					/* Try S/Key or TIS, authentication. */
f09e2e
 	int     gss_authentication;	/* Try GSS authentication */
f09e2e
+	int     gss_keyex;		/* Try GSS key exchange */
f09e2e
 	int     gss_deleg_creds;	/* Delegate GSS credentials */
f09e2e
+	int	gss_trust_dns;		/* Trust DNS for GSS canonicalization */
f09e2e
+	int	gss_renewal_rekey;	/* Credential renewal forces rekey */
f09e2e
+	char    *gss_client_identity;   /* Principal to initiate GSSAPI with */
f09e2e
+	char    *gss_server_identity;   /* GSSAPI target principal */
f09e2e
 	int     password_authentication;	/* Try password
f09e2e
 						 * authentication. */
f09e2e
 	int     kbd_interactive_authentication; /* Try keyboard-interactive auth. */
017ff1
diff --git a/regress/cert-hostkey.sh b/regress/cert-hostkey.sh
017ff1
index 1d9e0ed..1277409 100644
017ff1
--- a/regress/cert-hostkey.sh
017ff1
+++ b/regress/cert-hostkey.sh
017ff1
@@ -17,7 +17,7 @@ ${SSHKEYGEN} -q -N '' -t rsa  -f $OBJ/host_ca_key ||\
017ff1
 	cat $OBJ/host_ca_key.pub
017ff1
 ) > $OBJ/known_hosts-cert
017ff1
 
017ff1
-PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
017ff1
+PLAIN_TYPES=`$SSH -Q key-plain | grep -v null | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
017ff1
 
017ff1
 type_has_legacy() {
017ff1
 	case $1 in
017ff1
diff --git a/regress/cert-userkey.sh b/regress/cert-userkey.sh
017ff1
index b093a91..4c8da00 100644
017ff1
--- a/regress/cert-userkey.sh
017ff1
+++ b/regress/cert-userkey.sh
017ff1
@@ -6,7 +6,7 @@ tid="certified user keys"
017ff1
 rm -f $OBJ/authorized_keys_$USER $OBJ/user_ca_key* $OBJ/cert_user_key*
017ff1
 cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
017ff1
 
017ff1
-PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/;s/^ssh-//'`
017ff1
+PLAIN_TYPES=`$SSH -Q key-plain | grep -v null | sed 's/^ssh-dss/ssh-dsa/;s/^ssh-//'`
017ff1
 
017ff1
 type_has_legacy() {
017ff1
 	case $1 in
017ff1
diff --git a/regress/kextype.sh b/regress/kextype.sh
017ff1
index 8c2ac09..a2a87ca 100644
017ff1
--- a/regress/kextype.sh
017ff1
+++ b/regress/kextype.sh
017ff1
@@ -9,6 +9,9 @@ cp $OBJ/ssh_proxy $OBJ/ssh_proxy_bak
017ff1
 
017ff1
 tries="1 2 3 4"
017ff1
 for k in `${SSH} -Q kex`; do
017ff1
+	if [ $k = "gss-gex-sha1-" -o $k = "gss-group1-sha1-" -o $k = "gss-group14-sha1-" ]; then
017ff1
+		continue
017ff1
+	fi
017ff1
 	verbose "kex $k"
017ff1
 	for i in $tries; do
017ff1
 		${SSH} -F $OBJ/ssh_proxy -o KexAlgorithms=$k x true
017ff1
diff --git a/regress/rekey.sh b/regress/rekey.sh
017ff1
index cf9401e..31fb0f7 100644
017ff1
--- a/regress/rekey.sh
017ff1
+++ b/regress/rekey.sh
017ff1
@@ -30,6 +30,9 @@ increase_datafile_size 300
017ff1
 
017ff1
 opts=""
017ff1
 for i in `${SSH} -Q kex`; do
017ff1
+	if [ $i = "gss-gex-sha1-" -o $i = "gss-group1-sha1-" -o $i = "gss-group14-sha1-" ]; then
017ff1
+		continue
017ff1
+	fi
017ff1
 	opts="$opts KexAlgorithms=$i"
017ff1
 done
017ff1
 for i in `${SSH} -Q cipher`; do
017ff1
@@ -48,6 +51,9 @@ done
017ff1
 if ${SSH} -Q cipher-auth | grep '^.*$' >/dev/null 2>&1 ; then
017ff1
   for c in `${SSH} -Q cipher-auth`; do
017ff1
     for kex in `${SSH} -Q kex`; do
017ff1
+	if [ $kex = "gss-gex-sha1-" -o $kex = "gss-group1-sha1-" -o $kex = "gss-group14-sha1-" ]; then
017ff1
+		continue
017ff1
+	fi
017ff1
 	verbose "client rekey $c $kex"
017ff1
 	ssh_data_rekeying -oRekeyLimit=256k -oCiphers=$c -oKexAlgorithms=$kex
017ff1
     done
017ff1
diff --git a/servconf.c b/servconf.c
017ff1
index f763317..68fb9ef 100644
017ff1
--- a/servconf.c
017ff1
+++ b/servconf.c
017ff1
@@ -108,7 +108,10 @@ initialize_server_options(ServerOptions *options)
f09e2e
 	options->kerberos_ticket_cleanup = -1;
f09e2e
 	options->kerberos_get_afs_token = -1;
f09e2e
 	options->gss_authentication=-1;
f09e2e
+	options->gss_keyex = -1;
f09e2e
 	options->gss_cleanup_creds = -1;
f09e2e
+	options->gss_strict_acceptor = -1;
f09e2e
+	options->gss_store_rekey = -1;
f09e2e
 	options->password_authentication = -1;
f09e2e
 	options->kbd_interactive_authentication = -1;
f09e2e
 	options->challenge_response_authentication = -1;
017ff1
@@ -245,8 +248,14 @@ fill_default_server_options(ServerOptions *options)
f09e2e
 		options->kerberos_get_afs_token = 0;
f09e2e
 	if (options->gss_authentication == -1)
f09e2e
 		options->gss_authentication = 0;
f09e2e
+	if (options->gss_keyex == -1)
f09e2e
+		options->gss_keyex = 0;
f09e2e
 	if (options->gss_cleanup_creds == -1)
f09e2e
 		options->gss_cleanup_creds = 1;
f09e2e
+	if (options->gss_strict_acceptor == -1)
f09e2e
+		options->gss_strict_acceptor = 1;
f09e2e
+	if (options->gss_store_rekey == -1)
f09e2e
+		options->gss_store_rekey = 0;
f09e2e
 	if (options->password_authentication == -1)
f09e2e
 		options->password_authentication = 1;
f09e2e
 	if (options->kbd_interactive_authentication == -1)
017ff1
@@ -344,7 +353,8 @@ typedef enum {
f09e2e
 	sBanner, sShowPatchLevel, sUseDNS, sHostbasedAuthentication,
f09e2e
 	sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
f09e2e
 	sClientAliveCountMax, sAuthorizedKeysFile,
f09e2e
-	sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
f09e2e
+	sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
017ff1
+	sGssKeyEx, sGssStoreRekey, sAcceptEnv, sPermitTunnel,
f09e2e
 	sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
f09e2e
 	sUsePrivilegeSeparation, sAllowAgentForwarding,
017ff1
 	sHostCertificate,
017ff1
@@ -411,10 +421,20 @@ static struct {
f09e2e
 #ifdef GSSAPI
f09e2e
 	{ "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
f09e2e
 	{ "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
f09e2e
+	{ "gssapicleanupcreds", sGssCleanupCreds, SSHCFG_GLOBAL },
f09e2e
+	{ "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
f09e2e
+	{ "gssapikeyexchange", sGssKeyEx, SSHCFG_GLOBAL },
f09e2e
+	{ "gssapistorecredentialsonrekey", sGssStoreRekey, SSHCFG_GLOBAL },
f09e2e
 #else
f09e2e
 	{ "gssapiauthentication", sUnsupported, SSHCFG_ALL },
f09e2e
 	{ "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
f09e2e
+	{ "gssapicleanupcreds", sUnsupported, SSHCFG_GLOBAL },
f09e2e
+	{ "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
f09e2e
+	{ "gssapikeyexchange", sUnsupported, SSHCFG_GLOBAL },
f09e2e
+	{ "gssapistorecredentialsonrekey", sUnsupported, SSHCFG_GLOBAL },
f09e2e
 #endif
f09e2e
+	{ "gssusesessionccache", sUnsupported, SSHCFG_GLOBAL },
f09e2e
+	{ "gssapiusesessioncredcache", sUnsupported, SSHCFG_GLOBAL },
f09e2e
 	{ "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
f09e2e
 	{ "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
f09e2e
 	{ "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
017ff1
@@ -1091,10 +1111,22 @@ process_server_config_line(ServerOptions *options, char *line,
f09e2e
 		intptr = &options->gss_authentication;
f09e2e
 		goto parse_flag;
f09e2e
 
f09e2e
+	case sGssKeyEx:
f09e2e
+		intptr = &options->gss_keyex;
f09e2e
+		goto parse_flag;
f09e2e
+
f09e2e
 	case sGssCleanupCreds:
f09e2e
 		intptr = &options->gss_cleanup_creds;
f09e2e
 		goto parse_flag;
f09e2e
 
f09e2e
+	case sGssStrictAcceptor:
f09e2e
+		intptr = &options->gss_strict_acceptor;
f09e2e
+		goto parse_flag;
f09e2e
+
f09e2e
+	case sGssStoreRekey:
f09e2e
+		intptr = &options->gss_store_rekey;
f09e2e
+		goto parse_flag;
f09e2e
+
f09e2e
 	case sPasswordAuthentication:
f09e2e
 		intptr = &options->password_authentication;
f09e2e
 		goto parse_flag;
017ff1
@@ -2005,6 +2037,9 @@ dump_config(ServerOptions *o)
f09e2e
 #ifdef GSSAPI
f09e2e
 	dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
f09e2e
 	dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
f09e2e
+	dump_cfg_fmtint(sGssKeyEx, o->gss_keyex);
f09e2e
+	dump_cfg_fmtint(sGssStrictAcceptor, o->gss_strict_acceptor);
f09e2e
+	dump_cfg_fmtint(sGssStoreRekey, o->gss_store_rekey);
f09e2e
 #endif
017ff1
 	dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
017ff1
 	dump_cfg_fmtint(sKbdInteractiveAuthentication,
017ff1
diff --git a/servconf.h b/servconf.h
017ff1
index 4572066..37cfa9b 100644
017ff1
--- a/servconf.h
017ff1
+++ b/servconf.h
017ff1
@@ -112,7 +112,10 @@ typedef struct {
f09e2e
 	int     kerberos_get_afs_token;		/* If true, try to get AFS token if
f09e2e
 						 * authenticated with Kerberos. */
f09e2e
 	int     gss_authentication;	/* If true, permit GSSAPI authentication */
f09e2e
+	int     gss_keyex;		/* If true, permit GSSAPI key exchange */
f09e2e
 	int     gss_cleanup_creds;	/* If true, destroy cred cache on logout */
f09e2e
+	int 	gss_strict_acceptor;	/* If true, restrict the GSSAPI acceptor name */
f09e2e
+	int 	gss_store_rekey;
f09e2e
 	int     password_authentication;	/* If true, permit password
f09e2e
 						 * authentication. */
f09e2e
 	int     kbd_interactive_authentication;	/* If true, permit */
017ff1
diff --git a/ssh-gss.h b/ssh-gss.h
017ff1
index a99d7f0..0374c88 100644
017ff1
--- a/ssh-gss.h
017ff1
+++ b/ssh-gss.h
f09e2e
@@ -1,6 +1,6 @@
017ff1
 /* $OpenBSD: ssh-gss.h,v 1.11 2014/02/26 20:28:44 djm Exp $ */
f09e2e
 /*
f09e2e
- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
f09e2e
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
f09e2e
  *
f09e2e
  * Redistribution and use in source and binary forms, with or without
f09e2e
  * modification, are permitted provided that the following conditions
f09e2e
@@ -61,10 +61,22 @@
f09e2e
 
f09e2e
 #define SSH_GSS_OIDTYPE 0x06
f09e2e
 
f09e2e
+#define SSH2_MSG_KEXGSS_INIT                            30
f09e2e
+#define SSH2_MSG_KEXGSS_CONTINUE                        31
f09e2e
+#define SSH2_MSG_KEXGSS_COMPLETE                        32
f09e2e
+#define SSH2_MSG_KEXGSS_HOSTKEY                         33
f09e2e
+#define SSH2_MSG_KEXGSS_ERROR                           34
f09e2e
+#define SSH2_MSG_KEXGSS_GROUPREQ			40
f09e2e
+#define SSH2_MSG_KEXGSS_GROUP				41
f09e2e
+#define KEX_GSS_GRP1_SHA1_ID				"gss-group1-sha1-"
f09e2e
+#define KEX_GSS_GRP14_SHA1_ID				"gss-group14-sha1-"
f09e2e
+#define KEX_GSS_GEX_SHA1_ID				"gss-gex-sha1-"
f09e2e
+
f09e2e
 typedef struct {
f09e2e
 	char *filename;
f09e2e
 	char *envvar;
f09e2e
 	char *envval;
f09e2e
+	struct passwd *owner;
f09e2e
 	void *data;
f09e2e
 } ssh_gssapi_ccache;
f09e2e
 
f09e2e
@@ -72,8 +84,11 @@ typedef struct {
f09e2e
 	gss_buffer_desc displayname;
f09e2e
 	gss_buffer_desc exportedname;
f09e2e
 	gss_cred_id_t creds;
f09e2e
+	gss_name_t name;
f09e2e
 	struct ssh_gssapi_mech_struct *mech;
f09e2e
 	ssh_gssapi_ccache store;
f09e2e
+	int used;
f09e2e
+	int updated;
f09e2e
 } ssh_gssapi_client;
f09e2e
 
f09e2e
 typedef struct ssh_gssapi_mech_struct {
f09e2e
@@ -84,6 +99,7 @@ typedef struct ssh_gssapi_mech_struct {
f09e2e
 	int (*userok) (ssh_gssapi_client *, char *);
f09e2e
 	int (*localname) (ssh_gssapi_client *, char **);
f09e2e
 	void (*storecreds) (ssh_gssapi_client *);
f09e2e
+	int (*updatecreds) (ssh_gssapi_ccache *, ssh_gssapi_client *);
f09e2e
 } ssh_gssapi_mech;
f09e2e
 
f09e2e
 typedef struct {
f09e2e
@@ -94,10 +110,11 @@ typedef struct {
f09e2e
 	gss_OID		oid; /* client */
f09e2e
 	gss_cred_id_t	creds; /* server */
f09e2e
 	gss_name_t	client; /* server */
f09e2e
-	gss_cred_id_t	client_creds; /* server */
f09e2e
+	gss_cred_id_t	client_creds; /* both */
f09e2e
 } Gssctxt;
f09e2e
 
f09e2e
 extern ssh_gssapi_mech *supported_mechs[];
f09e2e
+extern Gssctxt *gss_kex_context;
f09e2e
 
f09e2e
 int  ssh_gssapi_check_oid(Gssctxt *, void *, size_t);
f09e2e
 void ssh_gssapi_set_oid_data(Gssctxt *, void *, size_t);
017ff1
@@ -119,16 +136,30 @@ void ssh_gssapi_build_ctx(Gssctxt **);
f09e2e
 void ssh_gssapi_delete_ctx(Gssctxt **);
f09e2e
 OM_uint32 ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
f09e2e
 void ssh_gssapi_buildmic(Buffer *, const char *, const char *, const char *);
f09e2e
-int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *);
f09e2e
+int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *, const char *);
f09e2e
+OM_uint32 ssh_gssapi_client_identity(Gssctxt *, const char *);
f09e2e
+int ssh_gssapi_credentials_updated(Gssctxt *);
f09e2e
 
f09e2e
 /* In the server */
f09e2e
+typedef int ssh_gssapi_check_fn(Gssctxt **, gss_OID, const char *, 
f09e2e
+    const char *);
f09e2e
+char *ssh_gssapi_client_mechanisms(const char *, const char *);
f09e2e
+char *ssh_gssapi_kex_mechs(gss_OID_set, ssh_gssapi_check_fn *, const char *,
f09e2e
+    const char *);
f09e2e
+gss_OID ssh_gssapi_id_kex(Gssctxt *, char *, int);
f09e2e
+int ssh_gssapi_server_check_mech(Gssctxt **,gss_OID, const char *, 
f09e2e
+    const char *);
f09e2e
 OM_uint32 ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
f09e2e
-int ssh_gssapi_userok(char *name);
f09e2e
+int ssh_gssapi_userok(char *name, struct passwd *);
f09e2e
 OM_uint32 ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
f09e2e
 void ssh_gssapi_do_child(char ***, u_int *);
f09e2e
 void ssh_gssapi_cleanup_creds(void);
f09e2e
 void ssh_gssapi_storecreds(void);
f09e2e
 
f09e2e
+char *ssh_gssapi_server_mechanisms(void);
f09e2e
+int ssh_gssapi_oid_table_ok();
f09e2e
+
f09e2e
+int ssh_gssapi_update_creds(ssh_gssapi_ccache *store);
f09e2e
 #endif /* GSSAPI */
f09e2e
 
f09e2e
 #endif /* _SSH_GSS_H */
017ff1
diff --git a/ssh_config b/ssh_config
017ff1
index 6d1abaf..b0d343b 100644
017ff1
--- a/ssh_config
017ff1
+++ b/ssh_config
017ff1
@@ -26,6 +26,8 @@
017ff1
 #   HostbasedAuthentication no
017ff1
 #   GSSAPIAuthentication no
017ff1
 #   GSSAPIDelegateCredentials no
017ff1
+#   GSSAPIKeyExchange no
017ff1
+#   GSSAPITrustDNS no
017ff1
 #   BatchMode no
017ff1
 #   CheckHostIP yes
017ff1
 #   AddressFamily any
017ff1
diff --git a/ssh_config.5 b/ssh_config.5
017ff1
index b580392..e7accd6 100644
017ff1
--- a/ssh_config.5
017ff1
+++ b/ssh_config.5
017ff1
@@ -682,11 +682,43 @@ Specifies whether user authentication based on GSSAPI is allowed.
f09e2e
 The default is
f09e2e
 .Dq no .
f09e2e
 Note that this option applies to protocol version 2 only.
f09e2e
+.It Cm GSSAPIKeyExchange
f09e2e
+Specifies whether key exchange based on GSSAPI may be used. When using
f09e2e
+GSSAPI key exchange the server need not have a host key.
f09e2e
+The default is
f09e2e
+.Dq no .
f09e2e
+Note that this option applies to protocol version 2 only.
f09e2e
+.It Cm GSSAPIClientIdentity
f09e2e
+If set, specifies the GSSAPI client identity that ssh should use when 
f09e2e
+connecting to the server. The default is unset, which means that the default 
f09e2e
+identity will be used.
f09e2e
+.It Cm GSSAPIServerIdentity
f09e2e
+If set, specifies the GSSAPI server identity that ssh should expect when 
f09e2e
+connecting to the server. The default is unset, which means that the
f09e2e
+expected GSSAPI server identity will be determined from the target
f09e2e
+hostname.
f09e2e
 .It Cm GSSAPIDelegateCredentials
f09e2e
 Forward (delegate) credentials to the server.
f09e2e
 The default is
f09e2e
 .Dq no .
f09e2e
-Note that this option applies to protocol version 2 only.
f09e2e
+Note that this option applies to protocol version 2 connections using GSSAPI.
f09e2e
+.It Cm GSSAPIRenewalForcesRekey
f09e2e
+If set to 
f09e2e
+.Dq yes
f09e2e
+then renewal of the client's GSSAPI credentials will force the rekeying of the
f09e2e
+ssh connection. With a compatible server, this can delegate the renewed 
f09e2e
+credentials to a session on the server.
f09e2e
+The default is
f09e2e
+.Dq no .
f09e2e
+.It Cm GSSAPITrustDns
f09e2e
+Set to 
f09e2e
+.Dq yes to indicate that the DNS is trusted to securely canonicalize
f09e2e
+the name of the host being connected to. If 
f09e2e
+.Dq no, the hostname entered on the
f09e2e
+command line will be passed untouched to the GSSAPI library.
f09e2e
+The default is
f09e2e
+.Dq no .
f09e2e
+This option only applies to protocol version 2 connections using GSSAPI.
f09e2e
 .It Cm HashKnownHosts
f09e2e
 Indicates that
f09e2e
 .Xr ssh 1
017ff1
diff --git a/sshconnect2.c b/sshconnect2.c
017ff1
index adbbfc7..cadf234 100644
017ff1
--- a/sshconnect2.c
017ff1
+++ b/sshconnect2.c
017ff1
@@ -158,9 +158,34 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
f09e2e
 {
f09e2e
 	Kex *kex;
f09e2e
 
f09e2e
+#ifdef GSSAPI
f09e2e
+	char *orig = NULL, *gss = NULL;
f09e2e
+	char *gss_host = NULL;
f09e2e
+#endif
f09e2e
+
f09e2e
 	xxx_host = host;
f09e2e
 	xxx_hostaddr = hostaddr;
f09e2e
 
f09e2e
+#ifdef GSSAPI
f09e2e
+	if (options.gss_keyex) {
f09e2e
+		/* Add the GSSAPI mechanisms currently supported on this 
f09e2e
+		 * client to the key exchange algorithm proposal */
f09e2e
+		orig = myproposal[PROPOSAL_KEX_ALGS];
f09e2e
+
f09e2e
+		if (options.gss_trust_dns)
f09e2e
+			gss_host = (char *)get_canonical_hostname(1);
f09e2e
+		else
f09e2e
+			gss_host = host;
f09e2e
+
f09e2e
+		gss = ssh_gssapi_client_mechanisms(gss_host, options.gss_client_identity);
f09e2e
+		if (gss) {
f09e2e
+			debug("Offering GSSAPI proposal: %s", gss);
f09e2e
+			xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
f09e2e
+			    "%s,%s", gss, orig);
f09e2e
+		}
f09e2e
+	}
f09e2e
+#endif
f09e2e
+
f09e2e
 	if (options.ciphers == (char *)-1) {
f09e2e
 		logit("No valid ciphers for protocol version 2 given, using defaults.");
f09e2e
 		options.ciphers = NULL;
017ff1
@@ -196,6 +221,17 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
f09e2e
 	if (options.kex_algorithms != NULL)
f09e2e
 		myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
f09e2e
 
f09e2e
+#ifdef GSSAPI
f09e2e
+	/* If we've got GSSAPI algorithms, then we also support the
f09e2e
+	 * 'null' hostkey, as a last resort */
f09e2e
+	if (options.gss_keyex && gss) {
f09e2e
+		orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
f09e2e
+		xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS], 
f09e2e
+		    "%s,null", orig);
f09e2e
+		free(gss);
f09e2e
+	}
f09e2e
+#endif
f09e2e
+
f09e2e
 	if (options.rekey_limit || options.rekey_interval)
f09e2e
 		packet_set_rekey_limits((u_int32_t)options.rekey_limit,
f09e2e
 		    (time_t)options.rekey_interval);
017ff1
@@ -208,10 +244,30 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
f09e2e
 	kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
f09e2e
 	kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
017ff1
 	kex->kex[KEX_C25519_SHA256] = kexc25519_client;
f09e2e
+#ifdef GSSAPI
f09e2e
+	if (options.gss_keyex) {
f09e2e
+		kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
f09e2e
+		kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
f09e2e
+		kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
f09e2e
+	}
f09e2e
+#endif
f09e2e
 	kex->client_version_string=client_version_string;
f09e2e
 	kex->server_version_string=server_version_string;
f09e2e
 	kex->verify_host_key=&verify_host_key_callback;
f09e2e
 
f09e2e
+#ifdef GSSAPI
f09e2e
+	if (options.gss_keyex) {
f09e2e
+		kex->gss_deleg_creds = options.gss_deleg_creds;
f09e2e
+		kex->gss_trust_dns = options.gss_trust_dns;
f09e2e
+		kex->gss_client = options.gss_client_identity;
f09e2e
+		if (options.gss_server_identity) {
f09e2e
+			kex->gss_host = options.gss_server_identity;
f09e2e
+		} else {
f09e2e
+			kex->gss_host = gss_host;
f09e2e
+        }
f09e2e
+	}
f09e2e
+#endif
f09e2e
+
f09e2e
 	xxx_kex = kex;
f09e2e
 
f09e2e
 	dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
017ff1
@@ -301,6 +357,7 @@ void	input_gssapi_token(int type, u_int32_t, void *);
f09e2e
 void	input_gssapi_hash(int type, u_int32_t, void *);
f09e2e
 void	input_gssapi_error(int, u_int32_t, void *);
f09e2e
 void	input_gssapi_errtok(int, u_int32_t, void *);
f09e2e
+int	userauth_gsskeyex(Authctxt *authctxt);
f09e2e
 #endif
f09e2e
 
f09e2e
 void	userauth(Authctxt *, char *);
017ff1
@@ -316,6 +373,11 @@ static char *authmethods_get(void);
f09e2e
 
f09e2e
 Authmethod authmethods[] = {
f09e2e
 #ifdef GSSAPI
f09e2e
+	{"gssapi-keyex",
f09e2e
+		userauth_gsskeyex,
f09e2e
+		NULL,
f09e2e
+		&options.gss_authentication,
f09e2e
+		NULL},
f09e2e
 	{"gssapi-with-mic",
f09e2e
 		userauth_gssapi,
f09e2e
 		NULL,
017ff1
@@ -613,19 +675,31 @@ userauth_gssapi(Authctxt *authctxt)
f09e2e
 	static u_int mech = 0;
f09e2e
 	OM_uint32 min;
f09e2e
 	int ok = 0;
f09e2e
+	const char *gss_host;
f09e2e
+
f09e2e
+	if (options.gss_server_identity)
f09e2e
+		gss_host = options.gss_server_identity;
f09e2e
+	else if (options.gss_trust_dns)
f09e2e
+		gss_host = get_canonical_hostname(1);
f09e2e
+	else
f09e2e
+		gss_host = authctxt->host;
f09e2e
 
f09e2e
 	/* Try one GSSAPI method at a time, rather than sending them all at
f09e2e
 	 * once. */
f09e2e
 
f09e2e
 	if (gss_supported == NULL)
f09e2e
-		gss_indicate_mechs(&min, &gss_supported);
f09e2e
+		if (GSS_ERROR(gss_indicate_mechs(&min, &gss_supported))) {
f09e2e
+			gss_supported = NULL;
f09e2e
+			return 0;
f09e2e
+		}
f09e2e
 
f09e2e
 	/* Check to see if the mechanism is usable before we offer it */
f09e2e
 	while (mech < gss_supported->count && !ok) {
f09e2e
 		/* My DER encoding requires length<128 */
f09e2e
 		if (gss_supported->elements[mech].length < 128 &&
f09e2e
 		    ssh_gssapi_check_mechanism(&gssctxt, 
f09e2e
-		    &gss_supported->elements[mech], authctxt->host)) {
f09e2e
+		    &gss_supported->elements[mech], gss_host, 
f09e2e
+                    options.gss_client_identity)) {
f09e2e
 			ok = 1; /* Mechanism works */
f09e2e
 		} else {
f09e2e
 			mech++;
017ff1
@@ -722,8 +796,8 @@ input_gssapi_response(int type, u_int32_t plen, void *ctxt)
f09e2e
 {
f09e2e
 	Authctxt *authctxt = ctxt;
f09e2e
 	Gssctxt *gssctxt;
f09e2e
-	int oidlen;
f09e2e
-	char *oidv;
f09e2e
+	u_int oidlen;
f09e2e
+	u_char *oidv;
f09e2e
 
f09e2e
 	if (authctxt == NULL)
f09e2e
 		fatal("input_gssapi_response: no authentication context");
017ff1
@@ -832,6 +906,48 @@ input_gssapi_error(int type, u_int32_t plen, void *ctxt)
f09e2e
 	free(msg);
f09e2e
 	free(lang);
f09e2e
 }
f09e2e
+
f09e2e
+int
f09e2e
+userauth_gsskeyex(Authctxt *authctxt)
f09e2e
+{
f09e2e
+	Buffer b;
f09e2e
+	gss_buffer_desc gssbuf;
f09e2e
+	gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
f09e2e
+	OM_uint32 ms;
f09e2e
+
f09e2e
+	static int attempt = 0;
f09e2e
+	if (attempt++ >= 1)
f09e2e
+		return (0);
f09e2e
+
f09e2e
+	if (gss_kex_context == NULL) {
f09e2e
+		debug("No valid Key exchange context"); 
f09e2e
+		return (0);
f09e2e
+	}
f09e2e
+
f09e2e
+	ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
f09e2e
+	    "gssapi-keyex");
f09e2e
+
f09e2e
+	gssbuf.value = buffer_ptr(&b);
f09e2e
+	gssbuf.length = buffer_len(&b);
f09e2e
+
f09e2e
+	if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
f09e2e
+		buffer_free(&b);
f09e2e
+		return (0);
f09e2e
+	}
f09e2e
+
f09e2e
+	packet_start(SSH2_MSG_USERAUTH_REQUEST);
f09e2e
+	packet_put_cstring(authctxt->server_user);
f09e2e
+	packet_put_cstring(authctxt->service);
f09e2e
+	packet_put_cstring(authctxt->method->name);
f09e2e
+	packet_put_string(mic.value, mic.length);
f09e2e
+	packet_send();
f09e2e
+
f09e2e
+	buffer_free(&b);
f09e2e
+	gss_release_buffer(&ms, &mic);
f09e2e
+
f09e2e
+	return (1);
f09e2e
+}
f09e2e
+
f09e2e
 #endif /* GSSAPI */
f09e2e
 
f09e2e
 int
017ff1
diff --git a/sshd.c b/sshd.c
017ff1
index 24ab272..e4e406e 100644
017ff1
--- a/sshd.c
017ff1
+++ b/sshd.c
017ff1
@@ -122,6 +122,10 @@
f09e2e
 #include "ssh-sandbox.h"
f09e2e
 #include "version.h"
f09e2e
 
f09e2e
+#ifdef USE_SECURITY_SESSION_API
f09e2e
+#include <Security/AuthSession.h>
f09e2e
+#endif
f09e2e
+
f09e2e
 #ifdef LIBWRAP
f09e2e
 #include <tcpd.h>
f09e2e
 #include <syslog.h>
017ff1
@@ -1744,10 +1748,13 @@ main(int ac, char **av)
f09e2e
 		logit("Disabling protocol version 1. Could not load host key");
f09e2e
 		options.protocol &= ~SSH_PROTO_1;
f09e2e
 	}
f09e2e
+#ifndef GSSAPI
f09e2e
+	/* The GSSAPI key exchange can run without a host key */
f09e2e
 	if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
f09e2e
 		logit("Disabling protocol version 2. Could not load host key");
f09e2e
 		options.protocol &= ~SSH_PROTO_2;
f09e2e
 	}
f09e2e
+#endif
f09e2e
 	if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
f09e2e
 		logit("sshd: no hostkeys available -- exiting.");
f09e2e
 		exit(1);
017ff1
@@ -2488,6 +2495,48 @@ do_ssh2_kex(void)
017ff1
 	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
017ff1
 	    list_hostkey_types());
f09e2e
 
f09e2e
+#ifdef GSSAPI
f09e2e
+	{
f09e2e
+	char *orig;
f09e2e
+	char *gss = NULL;
f09e2e
+	char *newstr = NULL;
f09e2e
+	orig = myproposal[PROPOSAL_KEX_ALGS];
f09e2e
+
f09e2e
+	/* 
f09e2e
+	 * If we don't have a host key, then there's no point advertising
f09e2e
+	 * the other key exchange algorithms
f09e2e
+	 */
f09e2e
+
f09e2e
+	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
f09e2e
+		orig = NULL;
f09e2e
+
f09e2e
+	if (options.gss_keyex)
f09e2e
+		gss = ssh_gssapi_server_mechanisms();
f09e2e
+	else
f09e2e
+		gss = NULL;
f09e2e
+
f09e2e
+	if (gss && orig)
f09e2e
+		xasprintf(&newstr, "%s,%s", gss, orig);
f09e2e
+	else if (gss)
f09e2e
+		newstr = gss;
f09e2e
+	else if (orig)
f09e2e
+		newstr = orig;
f09e2e
+
f09e2e
+	/* 
f09e2e
+	 * If we've got GSSAPI mechanisms, then we've got the 'null' host
f09e2e
+	 * key alg, but we can't tell people about it unless its the only
f09e2e
+  	 * host key algorithm we support
f09e2e
+	 */
f09e2e
+	if (gss && (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS])) == 0)
f09e2e
+		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = "null";
f09e2e
+
f09e2e
+	if (newstr)
f09e2e
+		myproposal[PROPOSAL_KEX_ALGS] = newstr;
f09e2e
+	else
f09e2e
+		fatal("No supported key exchange algorithms");
f09e2e
+	}
f09e2e
+#endif
f09e2e
+
f09e2e
 	/* start key exchange */
f09e2e
 	kex = kex_setup(myproposal);
f09e2e
 	kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
017ff1
@@ -2496,6 +2545,13 @@ do_ssh2_kex(void)
f09e2e
 	kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
f09e2e
 	kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
017ff1
 	kex->kex[KEX_C25519_SHA256] = kexc25519_server;
f09e2e
+#ifdef GSSAPI
f09e2e
+	if (options.gss_keyex) {
f09e2e
+		kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
f09e2e
+		kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
f09e2e
+		kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
f09e2e
+	}
f09e2e
+#endif
f09e2e
 	kex->server = 1;
f09e2e
 	kex->client_version_string=client_version_string;
f09e2e
 	kex->server_version_string=server_version_string;
017ff1
diff --git a/sshd_config b/sshd_config
017ff1
index c1b7c03..adfd7b1 100644
017ff1
--- a/sshd_config
017ff1
+++ b/sshd_config
017ff1
@@ -91,6 +91,8 @@ ChallengeResponseAuthentication no
017ff1
 # GSSAPI options
017ff1
 GSSAPIAuthentication yes
017ff1
 GSSAPICleanupCredentials no
017ff1
+#GSSAPIStrictAcceptorCheck yes
017ff1
+#GSSAPIKeyExchange no
017ff1
 
017ff1
 # Set this to 'yes' to enable PAM authentication, account processing,
017ff1
 # and session processing. If this is enabled, PAM authentication will
017ff1
diff --git a/sshd_config.5 b/sshd_config.5
017ff1
index 95b5f8c..1fb002d 100644
017ff1
--- a/sshd_config.5
017ff1
+++ b/sshd_config.5
017ff1
@@ -493,12 +493,40 @@ Specifies whether user authentication based on GSSAPI is allowed.
f09e2e
 The default is
f09e2e
 .Dq no .
f09e2e
 Note that this option applies to protocol version 2 only.
f09e2e
+.It Cm GSSAPIKeyExchange
f09e2e
+Specifies whether key exchange based on GSSAPI is allowed. GSSAPI key exchange
f09e2e
+doesn't rely on ssh keys to verify host identity.
f09e2e
+The default is
f09e2e
+.Dq no .
f09e2e
+Note that this option applies to protocol version 2 only.
f09e2e
 .It Cm GSSAPICleanupCredentials
f09e2e
 Specifies whether to automatically destroy the user's credentials cache
f09e2e
 on logout.
f09e2e
 The default is
f09e2e
 .Dq yes .
f09e2e
 Note that this option applies to protocol version 2 only.
f09e2e
+.It Cm GSSAPIStrictAcceptorCheck
f09e2e
+Determines whether to be strict about the identity of the GSSAPI acceptor 
f09e2e
+a client authenticates against. If
f09e2e
+.Dq yes
f09e2e
+then the client must authenticate against the
f09e2e
+.Pa host
f09e2e
+service on the current hostname. If 
f09e2e
+.Dq no
f09e2e
+then the client may authenticate against any service key stored in the 
f09e2e
+machine's default store. This facility is provided to assist with operation 
f09e2e
+on multi homed machines. 
f09e2e
+The default is
f09e2e
+.Dq yes .
f09e2e
+Note that this option applies only to protocol version 2 GSSAPI connections,
f09e2e
+and setting it to 
f09e2e
+.Dq no
f09e2e
+may only work with recent Kerberos GSSAPI libraries.
f09e2e
+.It Cm GSSAPIStoreCredentialsOnRekey
f09e2e
+Controls whether the user's GSSAPI credentials should be updated following a 
f09e2e
+successful connection rekeying. This option can be used to accepted renewed 
f09e2e
+or updated credentials from a compatible client. The default is
f09e2e
+.Dq no .
f09e2e
 .It Cm HostbasedAuthentication
f09e2e
 Specifies whether rhosts or /etc/hosts.equiv authentication together
f09e2e
 with successful public key client host authentication is allowed