kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
f09e2e
diff -up openssh-6.3p1/ChangeLog.gssapi.gsskex openssh-6.3p1/ChangeLog.gssapi
f09e2e
--- openssh-6.3p1/ChangeLog.gssapi.gsskex	2013-10-11 15:15:17.284216176 +0200
f09e2e
+++ openssh-6.3p1/ChangeLog.gssapi	2013-10-11 15:15:17.284216176 +0200
f09e2e
@@ -0,0 +1,113 @@
f09e2e
+20110101
f09e2e
+  - Finally update for OpenSSH 5.6p1
f09e2e
+  - Add GSSAPIServerIdentity option from Jim Basney
f09e2e
+ 
f09e2e
+20100308
f09e2e
+  - [ Makefile.in, key.c, key.h ]
f09e2e
+    Updates for OpenSSH 5.4p1
f09e2e
+  - [ servconf.c ]
f09e2e
+    Include GSSAPI options in the sshd -T configuration dump, and flag
f09e2e
+    some older configuration options as being unsupported. Thanks to Colin 
f09e2e
+    Watson.
f09e2e
+  -
f09e2e
+
f09e2e
+20100124
f09e2e
+  - [ sshconnect2.c ]
f09e2e
+    Adapt to deal with additional element in Authmethod structure. Thanks to
f09e2e
+    Colin Watson
f09e2e
+
f09e2e
+20090615
f09e2e
+  - [ gss-genr.c gss-serv.c kexgssc.c kexgsss.c monitor.c sshconnect2.c
f09e2e
+      sshd.c ]
f09e2e
+    Fix issues identified by Greg Hudson following a code review
f09e2e
+	Check return value of gss_indicate_mechs
f09e2e
+	Protect GSSAPI calls in monitor, so they can only be used if enabled
f09e2e
+	Check return values of bignum functions in key exchange
f09e2e
+	Use BN_clear_free to clear other side's DH value
f09e2e
+	Make ssh_gssapi_id_kex more robust
f09e2e
+	Only configure kex table pointers if GSSAPI is enabled
f09e2e
+	Don't leak mechanism list, or gss mechanism list
f09e2e
+	Cast data.length before printing
f09e2e
+	If serverkey isn't provided, use an empty string, rather than NULL
f09e2e
+
f09e2e
+20090201
f09e2e
+  - [ gss-genr.c gss-serv.c kex.h kexgssc.c readconf.c readconf.h ssh-gss.h
f09e2e
+      ssh_config.5 sshconnet2.c ]
f09e2e
+    Add support for the GSSAPIClientIdentity option, which allows the user
f09e2e
+    to specify which GSSAPI identity to use to contact a given server
f09e2e
+
f09e2e
+20080404
f09e2e
+  - [ gss-serv.c ]
f09e2e
+    Add code to actually implement GSSAPIStrictAcceptCheck, which had somehow
f09e2e
+    been omitted from a previous version of this patch. Reported by Borislav
f09e2e
+    Stoichkov
f09e2e
+
f09e2e
+20070317
f09e2e
+  - [ gss-serv-krb5.c ]
f09e2e
+    Remove C99ism, where new_ccname was being declared in the middle of a 
f09e2e
+    function
f09e2e
+
f09e2e
+20061220
f09e2e
+  - [ servconf.c ]
f09e2e
+    Make default for GSSAPIStrictAcceptorCheck be Yes, to match previous, and 
f09e2e
+    documented, behaviour. Reported by Dan Watson.
f09e2e
+
f09e2e
+20060910
f09e2e
+  - [ gss-genr.c kexgssc.c kexgsss.c kex.h monitor.c sshconnect2.c sshd.c
f09e2e
+      ssh-gss.h ]
f09e2e
+    add support for gss-group14-sha1 key exchange mechanisms
f09e2e
+  - [ gss-serv.c servconf.c servconf.h sshd_config sshd_config.5 ]
f09e2e
+    Add GSSAPIStrictAcceptorCheck option to allow the disabling of
f09e2e
+    acceptor principal checking on multi-homed machines.
f09e2e
+    <Bugzilla #928>
f09e2e
+  - [ sshd_config ssh_config ]
f09e2e
+    Add settings for GSSAPIKeyExchange and GSSAPITrustDNS to the sample
f09e2e
+    configuration files
f09e2e
+  - [ kexgss.c kegsss.c sshconnect2.c sshd.c ]
f09e2e
+    Code cleanup. Replace strlen/xmalloc/snprintf sequences with xasprintf()
f09e2e
+    Limit length of error messages displayed by client
f09e2e
+
f09e2e
+20060909
f09e2e
+  - [ gss-genr.c gss-serv.c ]
f09e2e
+    move ssh_gssapi_acquire_cred() and ssh_gssapi_server_ctx to be server
f09e2e
+    only, where they belong 
f09e2e
+    <Bugzilla #1225>
f09e2e
+
f09e2e
+20060829
f09e2e
+  - [ gss-serv-krb5.c ]
f09e2e
+    Fix CCAPI credentials cache name when creating KRB5CCNAME environment 
f09e2e
+    variable
f09e2e
+
f09e2e
+20060828
f09e2e
+  - [ gss-genr.c ]
f09e2e
+    Avoid Heimdal context freeing problem
f09e2e
+    <Fixed upstream 20060829>
f09e2e
+
f09e2e
+20060818
f09e2e
+  - [ gss-genr.c ssh-gss.h sshconnect2.c ]
f09e2e
+    Make sure that SPENGO is disabled 
f09e2e
+    <Bugzilla #1218 - Fixed upstream 20060818>
f09e2e
+
f09e2e
+20060421
f09e2e
+  - [ gssgenr.c, sshconnect2.c ]
f09e2e
+    a few type changes (signed versus unsigned, int versus size_t) to
f09e2e
+    fix compiler errors/warnings 
f09e2e
+    (from jbasney AT ncsa.uiuc.edu)
f09e2e
+  - [ kexgssc.c, sshconnect2.c ]
f09e2e
+    fix uninitialized variable warnings
f09e2e
+    (from jbasney AT ncsa.uiuc.edu)
f09e2e
+  - [ gssgenr.c ]
f09e2e
+    pass oid to gss_display_status (helpful when using GSSAPI mechglue)
f09e2e
+    (from jbasney AT ncsa.uiuc.edu)
f09e2e
+    <Bugzilla #1220 >
f09e2e
+  - [ gss-serv-krb5.c ]
f09e2e
+    #ifdef HAVE_GSSAPI_KRB5 should be #ifdef HAVE_GSSAPI_KRB5_H
f09e2e
+    (from jbasney AT ncsa.uiuc.edu)
f09e2e
+    <Fixed upstream 20060304>
f09e2e
+  - [ readconf.c, readconf.h, ssh_config.5, sshconnect2.c 
f09e2e
+    add client-side GssapiKeyExchange option
f09e2e
+    (from jbasney AT ncsa.uiuc.edu)
f09e2e
+  - [ sshconnect2.c ]
f09e2e
+    add support for GssapiTrustDns option for gssapi-with-mic
f09e2e
+    (from jbasney AT ncsa.uiuc.edu)
f09e2e
+    <gssapi-with-mic support is Bugzilla #1008>
f09e2e
diff -up openssh-6.3p1/Makefile.in.gsskex openssh-6.3p1/Makefile.in
f09e2e
--- openssh-6.3p1/Makefile.in.gsskex	2013-10-11 15:15:17.281216190 +0200
f09e2e
+++ openssh-6.3p1/Makefile.in	2013-10-11 15:15:17.289216153 +0200
f09e2e
@@ -77,6 +77,7 @@ LIBSSH_OBJS=authfd.o authfile.o bufaux.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 \
f09e2e
 	jpake.o schnorr.o ssh-pkcs11.o krl.o auditstub.o
f09e2e
 
f09e2e
@@ -93,7 +94,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passw
f09e2e
 	auth2-none.o auth2-passwd.o auth2-pubkey.o auth2-jpake.o \
f09e2e
 	monitor_mm.o monitor.o monitor_wrap.o kexdhs.o kexgexs.o kexecdhs.o \
f09e2e
 	auth-krb5.o \
f09e2e
-	auth2-gss.o gss-serv.o gss-serv-krb5.o \
f09e2e
+	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 \
f09e2e
diff -up openssh-6.3p1/auth2-gss.c.gsskex openssh-6.3p1/auth2-gss.c
f09e2e
--- openssh-6.3p1/auth2-gss.c.gsskex	2013-10-11 15:15:17.213216506 +0200
f09e2e
+++ openssh-6.3p1/auth2-gss.c	2013-10-11 15:15:17.283216181 +0200
f09e2e
@@ -52,6 +52,40 @@ static void input_gssapi_mic(int type, u
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)
f09e2e
@@ -240,7 +274,8 @@ input_gssapi_exchange_complete(int type,
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);
f09e2e
@@ -282,7 +317,8 @@ input_gssapi_mic(int type, u_int32_t ple
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
 
f09e2e
@@ -299,6 +335,12 @@ input_gssapi_mic(int type, u_int32_t ple
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,
f09e2e
diff -up openssh-6.3p1/auth2.c.gsskex openssh-6.3p1/auth2.c
f09e2e
--- openssh-6.3p1/auth2.c.gsskex	2013-10-11 15:15:17.214216502 +0200
f09e2e
+++ openssh-6.3p1/auth2.c	2013-10-11 15:15:17.283216181 +0200
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
f09e2e
 #ifdef JPAKE
f09e2e
@@ -79,6 +80,7 @@ Authmethod *authmethods[] = {
f09e2e
 	&method_none,
f09e2e
 	&method_pubkey,
f09e2e
 #ifdef GSSAPI
f09e2e
+	&method_gsskeyex,
f09e2e
 	&method_gssapi,
f09e2e
 #endif
f09e2e
 #ifdef JPAKE
f09e2e
diff -up openssh-6.3p1/clientloop.c.gsskex openssh-6.3p1/clientloop.c
f09e2e
--- openssh-6.3p1/clientloop.c.gsskex	2013-10-11 15:15:17.178216669 +0200
f09e2e
+++ openssh-6.3p1/clientloop.c	2013-10-11 15:15:17.284216176 +0200
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
 
f09e2e
@@ -1608,6 +1612,15 @@ client_loop(int have_pty, int escape_cha
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;
f09e2e
diff -up openssh-6.3p1/configure.ac.gsskex openssh-6.3p1/configure.ac
f09e2e
--- openssh-6.3p1/configure.ac.gsskex	2013-10-11 15:15:17.273216227 +0200
f09e2e
+++ openssh-6.3p1/configure.ac	2013-10-11 15:15:17.285216171 +0200
f09e2e
@@ -548,6 +548,30 @@ main() { if (NSVersionOfRunTimeLibrary("
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])
f09e2e
diff -up openssh-6.3p1/gss-genr.c.gsskex openssh-6.3p1/gss-genr.c
f09e2e
--- openssh-6.3p1/gss-genr.c.gsskex	2013-06-01 23:31:18.000000000 +0200
f09e2e
+++ openssh-6.3p1/gss-genr.c	2013-10-11 15:15:17.286216167 +0200
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)
f09e2e
@@ -197,7 +352,7 @@ ssh_gssapi_init_ctx(Gssctxt *ctx, int de
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
 
f09e2e
@@ -227,8 +382,42 @@ ssh_gssapi_import_name(Gssctxt *ctx, con
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);
f09e2e
@@ -236,6 +425,19 @@ ssh_gssapi_sign(Gssctxt *ctx, gss_buffer
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)
f09e2e
@@ -249,11 +451,16 @@ ssh_gssapi_buildmic(Buffer *b, const cha
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 && 
f09e2e
@@ -263,6 +470,10 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx
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);
f09e2e
@@ -272,10 +483,67 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx
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 */
f09e2e
diff -up openssh-6.3p1/gss-serv-krb5.c.gsskex openssh-6.3p1/gss-serv-krb5.c
f09e2e
--- openssh-6.3p1/gss-serv-krb5.c.gsskex	2013-07-20 05:35:45.000000000 +0200
f09e2e
+++ openssh-6.3p1/gss-serv-krb5.c	2013-10-23 21:48:20.558346236 +0200
f09e2e
@@ -120,7 +120,7 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
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) {
f09e2e
@@ -174,11 +174,26 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
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)
f09e2e
@@ -187,9 +202,76 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_cl
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",
f09e2e
@@ -197,7 +279,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 */
f09e2e
diff -up openssh-6.3p1/gss-serv.c.gsskex openssh-6.3p1/gss-serv.c
f09e2e
--- openssh-6.3p1/gss-serv.c.gsskex	2013-07-20 05:35:45.000000000 +0200
f09e2e
+++ openssh-6.3p1/gss-serv.c	2013-10-23 21:51:52.212347754 +0200
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;
f09e2e
@@ -81,25 +86,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
+		}
f09e2e
 
f09e2e
-	if (gethostname(lname, MAXHOSTNAMELEN)) {
f09e2e
-		gss_release_oid_set(&status, &oidset);
f09e2e
-		return (-1);
f09e2e
-	}
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 */
f09e2e
@@ -114,6 +126,29 @@ ssh_gssapi_server_ctx(Gssctxt **ctx, gss
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
 {
f09e2e
@@ -123,7 +158,9 @@ ssh_gssapi_supported_oids(gss_OID_set *o
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,
f09e2e
@@ -249,8 +286,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
 
f09e2e
@@ -265,6 +342,13 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_g
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);
f09e2e
@@ -282,6 +366,8 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_g
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;
f09e2e
@@ -292,11 +378,20 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_g
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
 
f09e2e
@@ -329,7 +424,7 @@ ssh_gssapi_do_child(char ***envp, u_int
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
 
f09e2e
@@ -339,9 +434,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);
f09e2e
@@ -354,14 +451,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
f09e2e
diff -up openssh-6.3p1/kex.c.gsskex openssh-6.3p1/kex.c
f09e2e
--- openssh-6.3p1/kex.c.gsskex	2013-10-30 15:26:39.339608716 +0100
f09e2e
+++ openssh-6.3p1/kex.c	2013-10-31 10:50:41.254535382 +0100
f09e2e
@@ -51,6 +51,10 @@
f09e2e
 #include "roaming.h"
f09e2e
 #include "audit.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
f09e2e
@@ -81,6 +85,11 @@ static const struct kexalg kexalgs[] = {
f09e2e
 	{ KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1, EVP_sha384 },
f09e2e
 	{ KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1, EVP_sha512 },
f09e2e
 #endif
f09e2e
+#ifdef GSSAPI
f09e2e
+	{ KEX_GSS_GEX_SHA1_ID, KEX_GSS_GEX_SHA1, 0, EVP_sha1 },
f09e2e
+	{ KEX_GSS_GRP1_SHA1_ID, KEX_GSS_GRP1_SHA1, 0, EVP_sha1 },
f09e2e
+	{ KEX_GSS_GRP14_SHA1_ID, KEX_GSS_GRP14_SHA1, 0, EVP_sha1 },
f09e2e
+#endif
f09e2e
 	{ NULL, -1, -1, NULL},
f09e2e
 };
f09e2e
 
f09e2e
@@ -110,6 +119,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
 }
f09e2e
diff -up openssh-6.3p1/kex.h.gsskex openssh-6.3p1/kex.h
f09e2e
--- openssh-6.3p1/kex.h.gsskex	2013-10-11 15:15:17.197216581 +0200
f09e2e
+++ openssh-6.3p1/kex.h	2013-10-11 15:43:21.757429309 +0200
f09e2e
@@ -74,6 +74,9 @@ enum kex_exchange {
f09e2e
 	KEX_DH_GEX_SHA1,
f09e2e
 	KEX_DH_GEX_SHA256,
f09e2e
 	KEX_ECDH_SHA2,
f09e2e
+	KEX_GSS_GRP1_SHA1,
f09e2e
+	KEX_GSS_GRP14_SHA1,
f09e2e
+	KEX_GSS_GEX_SHA1,
f09e2e
 	KEX_MAX
f09e2e
 };
f09e2e
 
f09e2e
@@ -133,6 +136,12 @@ struct Kex {
f09e2e
 	int	flags;
f09e2e
 	const EVP_MD *evp_md;
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 *);
f09e2e
@@ -162,6 +171,11 @@ void	 kexgex_server(Kex *);
f09e2e
 void	 kexecdh_client(Kex *);
f09e2e
 void	 kexecdh_server(Kex *);
f09e2e
 
f09e2e
+#ifdef GSSAPI
f09e2e
+void	kexgss_client(Kex *);
f09e2e
+void	kexgss_server(Kex *);
f09e2e
+#endif
f09e2e
+
f09e2e
 void	newkeys_destroy(Newkeys *newkeys);
f09e2e
 
f09e2e
 void
f09e2e
diff -up openssh-6.3p1/kexgssc.c.gsskex openssh-6.3p1/kexgssc.c
f09e2e
--- openssh-6.3p1/kexgssc.c.gsskex	2013-10-11 15:15:17.287216162 +0200
f09e2e
+++ openssh-6.3p1/kexgssc.c	2013-10-11 15:15:17.287216162 +0200
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(
f09e2e
+		    kex->evp_md,
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
+
f09e2e
+	kex_derive_keys(kex, hash, hashlen, shared_secret);
f09e2e
+	BN_clear_free(shared_secret);
f09e2e
+	kex_finish(kex);
f09e2e
+}
f09e2e
+
f09e2e
+#endif /* GSSAPI */
f09e2e
diff -up openssh-6.3p1/kexgsss.c.gsskex openssh-6.3p1/kexgsss.c
f09e2e
--- openssh-6.3p1/kexgsss.c.gsskex	2013-10-11 15:15:17.287216162 +0200
f09e2e
+++ openssh-6.3p1/kexgsss.c	2013-10-11 15:15:17.287216162 +0200
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(
f09e2e
+		    kex->evp_md,
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
+
f09e2e
+	kex_derive_keys(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 */
f09e2e
diff -up openssh-6.3p1/key.c.gsskex openssh-6.3p1/key.c
f09e2e
--- openssh-6.3p1/key.c.gsskex	2013-10-11 15:15:17.288216158 +0200
f09e2e
+++ openssh-6.3p1/key.c	2013-10-11 15:41:44.982868222 +0200
f09e2e
@@ -968,6 +968,7 @@ static const struct keytype keytypes[] =
f09e2e
 	    KEY_RSA_CERT_V00, 0, 1 },
f09e2e
 	{ "ssh-dss-cert-v00@openssh.com", "DSA-CERT-V00",
f09e2e
 	    KEY_DSA_CERT_V00, 0, 1 },
f09e2e
+	{ "null", "null", KEY_NULL, 0, 0 },
f09e2e
 	{ NULL, NULL, -1, -1, 0 }
f09e2e
 };
f09e2e
 
f09e2e
diff -up openssh-6.3p1/key.h.gsskex openssh-6.3p1/key.h
f09e2e
--- openssh-6.3p1/key.h.gsskex	2013-10-11 15:15:17.198216576 +0200
f09e2e
+++ openssh-6.3p1/key.h	2013-10-11 15:15:17.289216153 +0200
f09e2e
@@ -44,6 +44,7 @@ enum types {
f09e2e
 	KEY_ECDSA_CERT,
f09e2e
 	KEY_RSA_CERT_V00,
f09e2e
 	KEY_DSA_CERT_V00,
f09e2e
+	KEY_NULL,
f09e2e
 	KEY_UNSPEC
f09e2e
 };
f09e2e
 enum fp_type {
f09e2e
diff -up openssh-6.3p1/monitor.c.gsskex openssh-6.3p1/monitor.c
f09e2e
--- openssh-6.3p1/monitor.c.gsskex	2013-10-11 15:15:17.214216502 +0200
f09e2e
+++ openssh-6.3p1/monitor.c	2013-10-11 15:15:17.290216148 +0200
f09e2e
@@ -187,6 +187,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
f09e2e
@@ -271,6 +273,7 @@ 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
f09e2e
 #ifdef JPAKE
f09e2e
     {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
f09e2e
@@ -283,6 +286,12 @@ struct mon_table mon_dispatch_proto20[]
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},
f09e2e
@@ -405,6 +414,10 @@ monitor_child_preauth(Authctxt *_authctx
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
 
f09e2e
@@ -519,6 +532,10 @@ monitor_child_postauth(struct monitor *p
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);
f09e2e
@@ -1968,6 +1985,13 @@ mm_get_kex(Buffer *m)
f09e2e
 	kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
f09e2e
 	kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
f09e2e
 	kex->kex[KEX_ECDH_SHA2] = kexecdh_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);
f09e2e
@@ -2192,6 +2216,9 @@ mm_answer_gss_setup_ctx(int sock, Buffer
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
 
f09e2e
@@ -2219,6 +2246,9 @@ mm_answer_gss_accept_ctx(int sock, Buffe
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);
f09e2e
@@ -2236,6 +2266,7 @@ mm_answer_gss_accept_ctx(int sock, Buffe
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
 }
f09e2e
@@ -2247,6 +2278,9 @@ mm_answer_gss_checkmic(int sock, Buffer
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;;
f09e2e
@@ -2273,7 +2307,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);
f09e2e
@@ -2286,6 +2324,74 @@ 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
 
f09e2e
 #ifdef JPAKE
f09e2e
diff -up openssh-6.3p1/monitor.h.gsskex openssh-6.3p1/monitor.h
f09e2e
--- openssh-6.3p1/monitor.h.gsskex	2013-10-11 15:15:17.215216497 +0200
f09e2e
+++ openssh-6.3p1/monitor.h	2013-10-11 15:15:17.290216148 +0200
f09e2e
@@ -64,6 +64,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,
f09e2e
diff -up openssh-6.3p1/monitor_wrap.c.gsskex openssh-6.3p1/monitor_wrap.c
f09e2e
--- openssh-6.3p1/monitor_wrap.c.gsskex	2013-10-11 15:15:17.215216497 +0200
f09e2e
+++ openssh-6.3p1/monitor_wrap.c	2013-10-11 15:15:17.290216148 +0200
f09e2e
@@ -1329,7 +1329,7 @@ mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss
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;
f09e2e
@@ -1346,6 +1346,51 @@ 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 : "");
f09e2e
+	
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);
f09e2e
+	
f09e2e
+	return (ok);
f09e2e
+}
f09e2e
+
f09e2e
 #endif /* GSSAPI */
f09e2e
 
f09e2e
 #ifdef JPAKE
f09e2e
diff -up openssh-6.3p1/monitor_wrap.h.gsskex openssh-6.3p1/monitor_wrap.h
f09e2e
--- openssh-6.3p1/monitor_wrap.h.gsskex	2013-10-11 15:15:17.215216497 +0200
f09e2e
+++ openssh-6.3p1/monitor_wrap.h	2013-10-11 15:15:17.290216148 +0200
f09e2e
@@ -62,8 +62,10 @@ BIGNUM *mm_auth_rsa_generate_challenge(K
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
f09e2e
diff -up openssh-6.3p1/readconf.c.gsskex openssh-6.3p1/readconf.c
f09e2e
--- openssh-6.3p1/readconf.c.gsskex	2013-07-18 08:09:05.000000000 +0200
f09e2e
+++ openssh-6.3p1/readconf.c	2013-10-11 15:15:17.291216143 +0200
f09e2e
@@ -132,6 +132,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,
f09e2e
@@ -172,10 +174,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 },
f09e2e
@@ -516,10 +527,30 @@ parse_flag:
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;
f09e2e
@@ -1168,7 +1199,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;
f09e2e
@@ -1268,8 +1304,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)
f09e2e
diff -up openssh-6.3p1/readconf.h.gsskex openssh-6.3p1/readconf.h
f09e2e
--- openssh-6.3p1/readconf.h.gsskex	2013-05-16 12:30:03.000000000 +0200
f09e2e
+++ openssh-6.3p1/readconf.h	2013-10-11 15:15:17.291216143 +0200
f09e2e
@@ -48,7 +48,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. */
f09e2e
diff -up openssh-6.3p1/servconf.c.gsskex openssh-6.3p1/servconf.c
f09e2e
--- openssh-6.3p1/servconf.c.gsskex	2013-10-11 15:15:17.273216227 +0200
f09e2e
+++ openssh-6.3p1/servconf.c	2013-10-11 15:15:17.292216139 +0200
f09e2e
@@ -107,7 +107,10 @@ initialize_server_options(ServerOptions
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;
f09e2e
@@ -241,8 +244,14 @@ fill_default_server_options(ServerOption
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)
f09e2e
@@ -342,7 +351,9 @@ typedef enum {
f09e2e
 	sBanner, sShowPatchLevel, sUseDNS, sHostbasedAuthentication,
f09e2e
 	sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
f09e2e
 	sClientAliveCountMax, sAuthorizedKeysFile,
f09e2e
-	sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
f09e2e
+	sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
f09e2e
+	sGssKeyEx, sGssStoreRekey,
f09e2e
+	sAcceptEnv, sPermitTunnel,
f09e2e
 	sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
f09e2e
 	sUsePrivilegeSeparation, sAllowAgentForwarding,
f09e2e
 	sZeroKnowledgePasswordAuthentication, sHostCertificate,
f09e2e
@@ -409,10 +420,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 },
f09e2e
@@ -1078,10 +1099,22 @@ process_server_config_line(ServerOptions
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;
f09e2e
@@ -1994,6 +2027,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
f09e2e
 #ifdef JPAKE
f09e2e
 	dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
f09e2e
diff -up openssh-6.3p1/servconf.h.gsskex openssh-6.3p1/servconf.h
f09e2e
--- openssh-6.3p1/servconf.h.gsskex	2013-10-11 15:15:17.273216227 +0200
f09e2e
+++ openssh-6.3p1/servconf.h	2013-10-11 15:15:17.292216139 +0200
f09e2e
@@ -111,7 +111,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 */
f09e2e
diff -up openssh-6.3p1/ssh-gss.h.gsskex openssh-6.3p1/ssh-gss.h
f09e2e
--- openssh-6.3p1/ssh-gss.h.gsskex	2013-02-25 01:24:44.000000000 +0100
f09e2e
+++ openssh-6.3p1/ssh-gss.h	2013-10-11 15:15:17.294216130 +0200
f09e2e
@@ -1,6 +1,6 @@
f09e2e
 /* $OpenBSD: ssh-gss.h,v 1.10 2007/06/12 08:20:00 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);
f09e2e
@@ -117,16 +134,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 */
f09e2e
diff -up openssh-6.3p1/ssh_config.5.gsskex openssh-6.3p1/ssh_config.5
f09e2e
--- openssh-6.3p1/ssh_config.5.gsskex	2013-07-18 08:11:50.000000000 +0200
f09e2e
+++ openssh-6.3p1/ssh_config.5	2013-10-11 15:15:17.292216139 +0200
f09e2e
@@ -529,11 +529,43 @@ Specifies whether user authentication ba
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
f09e2e
diff -up openssh-6.3p1/ssh_config.gsskex openssh-6.3p1/ssh_config
f09e2e
--- openssh-6.3p1/ssh_config.gsskex	2013-10-11 15:15:17.265216264 +0200
f09e2e
+++ openssh-6.3p1/ssh_config	2013-10-11 15:15:17.292216139 +0200
f09e2e
@@ -26,6 +26,8 @@
f09e2e
 #   HostbasedAuthentication no
f09e2e
 #   GSSAPIAuthentication no
f09e2e
 #   GSSAPIDelegateCredentials no
f09e2e
+#   GSSAPIKeyExchange no
f09e2e
+#   GSSAPITrustDNS no
f09e2e
 #   BatchMode no
f09e2e
 #   CheckHostIP yes
f09e2e
 #   AddressFamily any
f09e2e
diff -up openssh-6.3p1/sshconnect2.c.gsskex openssh-6.3p1/sshconnect2.c
f09e2e
--- openssh-6.3p1/sshconnect2.c.gsskex	2013-10-11 15:15:17.251216330 +0200
f09e2e
+++ openssh-6.3p1/sshconnect2.c	2013-10-11 15:28:22.617529416 +0200
f09e2e
@@ -162,9 +162,34 @@ ssh_kex2(char *host, struct sockaddr *ho
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;
f09e2e
@@ -207,6 +232,17 @@ ssh_kex2(char *host, struct sockaddr *ho
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);
f09e2e
@@ -218,10 +254,30 @@ ssh_kex2(char *host, struct sockaddr *ho
f09e2e
 	kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
f09e2e
 	kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
f09e2e
 	kex->kex[KEX_ECDH_SHA2] = kexecdh_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);
f09e2e
@@ -317,6 +373,7 @@ void	input_gssapi_token(int type, u_int3
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 *);
f09e2e
@@ -332,6 +389,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,
f09e2e
@@ -636,19 +698,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++;
f09e2e
@@ -745,8 +819,8 @@ input_gssapi_response(int type, u_int32_
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");
f09e2e
@@ -855,6 +929,48 @@ input_gssapi_error(int type, u_int32_t p
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
f09e2e
diff -up openssh-6.3p1/sshd.c.gsskex openssh-6.3p1/sshd.c
f09e2e
--- openssh-6.3p1/sshd.c.gsskex	2013-10-11 15:15:17.277216209 +0200
f09e2e
+++ openssh-6.3p1/sshd.c	2013-10-11 15:15:17.294216130 +0200
f09e2e
@@ -125,6 +125,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>
f09e2e
@@ -1794,10 +1798,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);
f09e2e
@@ -2130,6 +2137,60 @@ main(int ac, char **av)
f09e2e
 	/* Log the connection. */
f09e2e
 	verbose("Connection from %.500s port %d", remote_ip, remote_port);
f09e2e
 
f09e2e
+#ifdef USE_SECURITY_SESSION_API
f09e2e
+	/*
f09e2e
+	 * Create a new security session for use by the new user login if
f09e2e
+	 * the current session is the root session or we are not launched
f09e2e
+	 * by inetd (eg: debugging mode or server mode).  We do not
f09e2e
+	 * necessarily need to create a session if we are launched from
f09e2e
+	 * inetd because Panther xinetd will create a session for us.
f09e2e
+	 *
f09e2e
+	 * The only case where this logic will fail is if there is an
f09e2e
+	 * inetd running in a non-root session which is not creating
f09e2e
+	 * new sessions for us.  Then all the users will end up in the
f09e2e
+	 * same session (bad).
f09e2e
+	 *
f09e2e
+	 * When the client exits, the session will be destroyed for us
f09e2e
+	 * automatically.
f09e2e
+	 *
f09e2e
+	 * We must create the session before any credentials are stored
f09e2e
+	 * (including AFS pags, which happens a few lines below).
f09e2e
+	 */
f09e2e
+	{
f09e2e
+		OSStatus err = 0;
f09e2e
+		SecuritySessionId sid = 0;
f09e2e
+		SessionAttributeBits sattrs = 0;
f09e2e
+
f09e2e
+		err = SessionGetInfo(callerSecuritySession, &sid, &sattrs);
f09e2e
+		if (err)
f09e2e
+			error("SessionGetInfo() failed with error %.8X",
f09e2e
+			    (unsigned) err);
f09e2e
+		else
f09e2e
+			debug("Current Session ID is %.8X / Session Attributes are %.8X",
f09e2e
+			    (unsigned) sid, (unsigned) sattrs);
f09e2e
+
f09e2e
+		if (inetd_flag && !(sattrs & sessionIsRoot))
f09e2e
+			debug("Running in inetd mode in a non-root session... "
f09e2e
+			    "assuming inetd created the session for us.");
f09e2e
+		else {
f09e2e
+			debug("Creating new security session...");
f09e2e
+			err = SessionCreate(0, sessionHasTTY | sessionIsRemote);
f09e2e
+			if (err)
f09e2e
+				error("SessionCreate() failed with error %.8X",
f09e2e
+				    (unsigned) err);
f09e2e
+
f09e2e
+			err = SessionGetInfo(callerSecuritySession, &sid, 
f09e2e
+			    &sattrs);
f09e2e
+			if (err)
f09e2e
+				error("SessionGetInfo() failed with error %.8X",
f09e2e
+				    (unsigned) err);
f09e2e
+			else
f09e2e
+				debug("New Session ID is %.8X / Session Attributes are %.8X",
f09e2e
+				    (unsigned) sid, (unsigned) sattrs);
f09e2e
+		}
f09e2e
+	}
f09e2e
+#endif
f09e2e
+
f09e2e
 	/*
f09e2e
 	 * We don't want to listen forever unless the other side
f09e2e
 	 * successfully authenticates itself.  So we set up an alarm which is
f09e2e
@@ -2551,6 +2612,48 @@ do_ssh2_kex(void)
f09e2e
 
f09e2e
 	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = 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;
f09e2e
@@ -2558,6 +2661,13 @@ do_ssh2_kex(void)
f09e2e
 	kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
f09e2e
 	kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
f09e2e
 	kex->kex[KEX_ECDH_SHA2] = kexecdh_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;
f09e2e
diff -up openssh-6.3p1/sshd_config.5.gsskex openssh-6.3p1/sshd_config.5
f09e2e
--- openssh-6.3p1/sshd_config.5.gsskex	2013-10-11 15:15:17.274216223 +0200
f09e2e
+++ openssh-6.3p1/sshd_config.5	2013-10-11 15:15:17.294216130 +0200
f09e2e
@@ -484,12 +484,40 @@ Specifies whether user authentication ba
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
f09e2e
diff -up openssh-6.3p1/sshd_config.gsskex openssh-6.3p1/sshd_config
f09e2e
--- openssh-6.3p1/sshd_config.gsskex	2013-10-11 15:15:17.277216209 +0200
f09e2e
+++ openssh-6.3p1/sshd_config	2013-10-11 15:15:17.294216130 +0200
f09e2e
@@ -92,6 +92,8 @@ ChallengeResponseAuthentication no
f09e2e
 GSSAPIAuthentication yes
f09e2e
 #GSSAPICleanupCredentials yes
f09e2e
 GSSAPICleanupCredentials yes
f09e2e
+#GSSAPIStrictAcceptorCheck yes
f09e2e
+#GSSAPIKeyExchange no
f09e2e
 
f09e2e
 # Set this to 'yes' to enable PAM authentication, account processing, 
f09e2e
 # and session processing. If this is enabled, PAM authentication will