Add missing key exchange files
authorLuke Howard <lukeh@padl.com>
Sun, 27 Mar 2011 13:27:07 +0000 (00:27 +1100)
committerLuke Howard <lukeh@padl.com>
Sun, 27 Mar 2011 13:27:07 +0000 (00:27 +1100)
ChangeLog.gssapi [new file with mode: 0644]
kexgssc.c [new file with mode: 0644]
kexgsss.c [new file with mode: 0644]

diff --git a/ChangeLog.gssapi b/ChangeLog.gssapi
new file mode 100644 (file)
index 0000000..f117a33
--- /dev/null
@@ -0,0 +1,113 @@
+20110101
+  - Finally update for OpenSSH 5.6p1
+  - Add GSSAPIServerIdentity option from Jim Basney
+20100308
+  - [ Makefile.in, key.c, key.h ]
+    Updates for OpenSSH 5.4p1
+  - [ servconf.c ]
+    Include GSSAPI options in the sshd -T configuration dump, and flag
+    some older configuration options as being unsupported. Thanks to Colin 
+    Watson.
+  -
+
+20100124
+  - [ sshconnect2.c ]
+    Adapt to deal with additional element in Authmethod structure. Thanks to
+    Colin Watson
+
+20090615
+  - [ gss-genr.c gss-serv.c kexgssc.c kexgsss.c monitor.c sshconnect2.c
+      sshd.c ]
+    Fix issues identified by Greg Hudson following a code review
+       Check return value of gss_indicate_mechs
+       Protect GSSAPI calls in monitor, so they can only be used if enabled
+       Check return values of bignum functions in key exchange
+       Use BN_clear_free to clear other side's DH value
+       Make ssh_gssapi_id_kex more robust
+       Only configure kex table pointers if GSSAPI is enabled
+       Don't leak mechanism list, or gss mechanism list
+       Cast data.length before printing
+       If serverkey isn't provided, use an empty string, rather than NULL
+
+20090201
+  - [ gss-genr.c gss-serv.c kex.h kexgssc.c readconf.c readconf.h ssh-gss.h
+      ssh_config.5 sshconnet2.c ]
+    Add support for the GSSAPIClientIdentity option, which allows the user
+    to specify which GSSAPI identity to use to contact a given server
+
+20080404
+  - [ gss-serv.c ]
+    Add code to actually implement GSSAPIStrictAcceptCheck, which had somehow
+    been omitted from a previous version of this patch. Reported by Borislav
+    Stoichkov
+
+20070317
+  - [ gss-serv-krb5.c ]
+    Remove C99ism, where new_ccname was being declared in the middle of a 
+    function
+
+20061220
+  - [ servconf.c ]
+    Make default for GSSAPIStrictAcceptorCheck be Yes, to match previous, and 
+    documented, behaviour. Reported by Dan Watson.
+
+20060910
+  - [ gss-genr.c kexgssc.c kexgsss.c kex.h monitor.c sshconnect2.c sshd.c
+      ssh-gss.h ]
+    add support for gss-group14-sha1 key exchange mechanisms
+  - [ gss-serv.c servconf.c servconf.h sshd_config sshd_config.5 ]
+    Add GSSAPIStrictAcceptorCheck option to allow the disabling of
+    acceptor principal checking on multi-homed machines.
+    <Bugzilla #928>
+  - [ sshd_config ssh_config ]
+    Add settings for GSSAPIKeyExchange and GSSAPITrustDNS to the sample
+    configuration files
+  - [ kexgss.c kegsss.c sshconnect2.c sshd.c ]
+    Code cleanup. Replace strlen/xmalloc/snprintf sequences with xasprintf()
+    Limit length of error messages displayed by client
+
+20060909
+  - [ gss-genr.c gss-serv.c ]
+    move ssh_gssapi_acquire_cred() and ssh_gssapi_server_ctx to be server
+    only, where they belong 
+    <Bugzilla #1225>
+
+20060829
+  - [ gss-serv-krb5.c ]
+    Fix CCAPI credentials cache name when creating KRB5CCNAME environment 
+    variable
+
+20060828
+  - [ gss-genr.c ]
+    Avoid Heimdal context freeing problem
+    <Fixed upstream 20060829>
+
+20060818
+  - [ gss-genr.c ssh-gss.h sshconnect2.c ]
+    Make sure that SPENGO is disabled 
+    <Bugzilla #1218 - Fixed upstream 20060818>
+
+20060421
+  - [ gssgenr.c, sshconnect2.c ]
+    a few type changes (signed versus unsigned, int versus size_t) to
+    fix compiler errors/warnings 
+    (from jbasney AT ncsa.uiuc.edu)
+  - [ kexgssc.c, sshconnect2.c ]
+    fix uninitialized variable warnings
+    (from jbasney AT ncsa.uiuc.edu)
+  - [ gssgenr.c ]
+    pass oid to gss_display_status (helpful when using GSSAPI mechglue)
+    (from jbasney AT ncsa.uiuc.edu)
+    <Bugzilla #1220 >
+  - [ gss-serv-krb5.c ]
+    #ifdef HAVE_GSSAPI_KRB5 should be #ifdef HAVE_GSSAPI_KRB5_H
+    (from jbasney AT ncsa.uiuc.edu)
+    <Fixed upstream 20060304>
+  - [ readconf.c, readconf.h, ssh_config.5, sshconnect2.c 
+    add client-side GssapiKeyExchange option
+    (from jbasney AT ncsa.uiuc.edu)
+  - [ sshconnect2.c ]
+    add support for GssapiTrustDns option for gssapi-with-mic
+    (from jbasney AT ncsa.uiuc.edu)
+    <gssapi-with-mic support is Bugzilla #1008>
diff --git a/kexgssc.c b/kexgssc.c
new file mode 100644 (file)
index 0000000..39be405
--- /dev/null
+++ b/kexgssc.c
@@ -0,0 +1,334 @@
+/*
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "includes.h"
+
+#ifdef GSSAPI
+
+#include "includes.h"
+
+#include <openssl/crypto.h>
+#include <openssl/bn.h>
+
+#include <string.h>
+
+#include "xmalloc.h"
+#include "buffer.h"
+#include "ssh2.h"
+#include "key.h"
+#include "cipher.h"
+#include "kex.h"
+#include "log.h"
+#include "packet.h"
+#include "dh.h"
+
+#include "ssh-gss.h"
+
+void
+kexgss_client(Kex *kex) {
+       gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
+       gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
+       Gssctxt *ctxt;
+       OM_uint32 maj_status, min_status, ret_flags;
+       u_int klen, kout, slen = 0, hashlen, strlen;
+       DH *dh; 
+       BIGNUM *dh_server_pub = NULL;
+       BIGNUM *shared_secret = NULL;
+       BIGNUM *p = NULL;
+       BIGNUM *g = NULL;       
+       u_char *kbuf, *hash;
+       u_char *serverhostkey = NULL;
+       u_char *empty = "";
+       char *msg;
+       char *lang;
+       int type = 0;
+       int first = 1;
+       int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
+
+       /* Initialise our GSSAPI world */       
+       ssh_gssapi_build_ctx(&ctxt);
+       if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type) 
+           == GSS_C_NO_OID)
+               fatal("Couldn't identify host exchange");
+
+       if (ssh_gssapi_import_name(ctxt, kex->gss_host))
+               fatal("Couldn't import hostname");
+
+       if (kex->gss_client && 
+           ssh_gssapi_client_identity(ctxt, kex->gss_client))
+               fatal("Couldn't acquire client credentials");
+
+       switch (kex->kex_type) {
+       case KEX_GSS_GRP1_SHA1:
+               dh = dh_new_group1();
+               break;
+       case KEX_GSS_GRP14_SHA1:
+               dh = dh_new_group14();
+               break;
+       case KEX_GSS_GEX_SHA1:
+               debug("Doing group exchange\n");
+               nbits = dh_estimate(kex->we_need * 8);
+               packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
+               packet_put_int(min);
+               packet_put_int(nbits);
+               packet_put_int(max);
+
+               packet_send();
+
+               packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
+
+               if ((p = BN_new()) == NULL)
+                       fatal("BN_new() failed");
+               packet_get_bignum2(p);
+               if ((g = BN_new()) == NULL)
+                       fatal("BN_new() failed");
+               packet_get_bignum2(g);
+               packet_check_eom();
+
+               if (BN_num_bits(p) < min || BN_num_bits(p) > max)
+                       fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
+                           min, BN_num_bits(p), max);
+
+               dh = dh_new_group(g, p);
+               break;
+       default:
+               fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
+       }
+       
+       /* Step 1 - e is dh->pub_key */
+       dh_gen_key(dh, kex->we_need * 8);
+
+       /* This is f, we initialise it now to make life easier */
+       dh_server_pub = BN_new();
+       if (dh_server_pub == NULL)
+               fatal("dh_server_pub == NULL");
+
+       token_ptr = GSS_C_NO_BUFFER;
+                        
+       do {
+               debug("Calling gss_init_sec_context");
+               
+               maj_status = ssh_gssapi_init_ctx(ctxt,
+                   kex->gss_deleg_creds, token_ptr, &send_tok,
+                   &ret_flags);
+
+               if (GSS_ERROR(maj_status)) {
+                       if (send_tok.length != 0) {
+                               packet_start(SSH2_MSG_KEXGSS_CONTINUE);
+                               packet_put_string(send_tok.value,
+                                   send_tok.length);
+                       }
+                       fatal("gss_init_context failed");
+               }
+
+               /* If we've got an old receive buffer get rid of it */
+               if (token_ptr != GSS_C_NO_BUFFER)
+                       xfree(recv_tok.value);
+
+               if (maj_status == GSS_S_COMPLETE) {
+                       /* If mutual state flag is not true, kex fails */
+                       if (!(ret_flags & GSS_C_MUTUAL_FLAG))
+                               fatal("Mutual authentication failed");
+
+                       /* If integ avail flag is not true kex fails */
+                       if (!(ret_flags & GSS_C_INTEG_FLAG))
+                               fatal("Integrity check failed");
+               }
+
+               /* 
+                * If we have data to send, then the last message that we
+                * received cannot have been a 'complete'. 
+                */
+               if (send_tok.length != 0) {
+                       if (first) {
+                               packet_start(SSH2_MSG_KEXGSS_INIT);
+                               packet_put_string(send_tok.value,
+                                   send_tok.length);
+                               packet_put_bignum2(dh->pub_key);
+                               first = 0;
+                       } else {
+                               packet_start(SSH2_MSG_KEXGSS_CONTINUE);
+                               packet_put_string(send_tok.value,
+                                   send_tok.length);
+                       }
+                       packet_send();
+                       gss_release_buffer(&min_status, &send_tok);
+
+                       /* If we've sent them data, they should reply */
+                       do {    
+                               type = packet_read();
+                               if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
+                                       debug("Received KEXGSS_HOSTKEY");
+                                       if (serverhostkey)
+                                               fatal("Server host key received more than once");
+                                       serverhostkey = 
+                                           packet_get_string(&slen);
+                               }
+                       } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
+
+                       switch (type) {
+                       case SSH2_MSG_KEXGSS_CONTINUE:
+                               debug("Received GSSAPI_CONTINUE");
+                               if (maj_status == GSS_S_COMPLETE) 
+                                       fatal("GSSAPI Continue received from server when complete");
+                               recv_tok.value = packet_get_string(&strlen);
+                               recv_tok.length = strlen; 
+                               break;
+                       case SSH2_MSG_KEXGSS_COMPLETE:
+                               debug("Received GSSAPI_COMPLETE");
+                               packet_get_bignum2(dh_server_pub);
+                               msg_tok.value =  packet_get_string(&strlen);
+                               msg_tok.length = strlen; 
+
+                               /* Is there a token included? */
+                               if (packet_get_char()) {
+                                       recv_tok.value=
+                                           packet_get_string(&strlen);
+                                       recv_tok.length = strlen;
+                                       /* If we're already complete - protocol error */
+                                       if (maj_status == GSS_S_COMPLETE)
+                                               packet_disconnect("Protocol error: received token when complete");
+                                       } else {
+                                               /* No token included */
+                                               if (maj_status != GSS_S_COMPLETE)
+                                                       packet_disconnect("Protocol error: did not receive final token");
+                               }
+                               break;
+                       case SSH2_MSG_KEXGSS_ERROR:
+                               debug("Received Error");
+                               maj_status = packet_get_int();
+                               min_status = packet_get_int();
+                               msg = packet_get_string(NULL);
+                               lang = packet_get_string(NULL);
+                               fatal("GSSAPI Error: \n%.400s",msg);
+                       default:
+                               packet_disconnect("Protocol error: didn't expect packet type %d",
+                               type);
+                       }
+                       token_ptr = &recv_tok;
+               } else {
+                       /* No data, and not complete */
+                       if (maj_status != GSS_S_COMPLETE)
+                               fatal("Not complete, and no token output");
+               }
+       } while (maj_status & GSS_S_CONTINUE_NEEDED);
+
+       /* 
+        * We _must_ have received a COMPLETE message in reply from the 
+        * server, which will have set dh_server_pub and msg_tok 
+        */
+
+       if (type != SSH2_MSG_KEXGSS_COMPLETE)
+               fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
+
+       /* Check f in range [1, p-1] */
+       if (!dh_pub_is_valid(dh, dh_server_pub))
+               packet_disconnect("bad server public DH value");
+
+       /* compute K=f^x mod p */
+       klen = DH_size(dh);
+       kbuf = xmalloc(klen);
+       kout = DH_compute_key(kbuf, dh_server_pub, dh);
+       if (kout < 0)
+               fatal("DH_compute_key: failed");
+
+       shared_secret = BN_new();
+       if (shared_secret == NULL)
+               fatal("kexgss_client: BN_new failed");
+
+       if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
+               fatal("kexdh_client: BN_bin2bn failed");
+
+       memset(kbuf, 0, klen);
+       xfree(kbuf);
+
+       switch (kex->kex_type) {
+       case KEX_GSS_GRP1_SHA1:
+       case KEX_GSS_GRP14_SHA1:
+               kex_dh_hash( kex->client_version_string, 
+                   kex->server_version_string,
+                   buffer_ptr(&kex->my), buffer_len(&kex->my),
+                   buffer_ptr(&kex->peer), buffer_len(&kex->peer),
+                   (serverhostkey ? serverhostkey : empty), slen,
+                   dh->pub_key,        /* e */
+                   dh_server_pub,      /* f */
+                   shared_secret,      /* K */
+                   &hash, &hashlen
+               );
+               break;
+       case KEX_GSS_GEX_SHA1:
+               kexgex_hash(
+                   kex->evp_md,
+                   kex->client_version_string,
+                   kex->server_version_string,
+                   buffer_ptr(&kex->my), buffer_len(&kex->my),
+                   buffer_ptr(&kex->peer), buffer_len(&kex->peer),
+                   (serverhostkey ? serverhostkey : empty), slen,
+                   min, nbits, max,
+                   dh->p, dh->g,
+                   dh->pub_key,
+                   dh_server_pub,
+                   shared_secret,
+                   &hash, &hashlen
+               );
+               break;
+       default:
+               fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
+       }
+
+       gssbuf.value = hash;
+       gssbuf.length = hashlen;
+
+       /* Verify that the hash matches the MIC we just got. */
+       if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
+               packet_disconnect("Hash's MIC didn't verify");
+
+       xfree(msg_tok.value);
+
+       DH_free(dh);
+       if (serverhostkey)
+               xfree(serverhostkey);
+       BN_clear_free(dh_server_pub);
+
+       /* save session id */
+       if (kex->session_id == NULL) {
+               kex->session_id_len = hashlen;
+               kex->session_id = xmalloc(kex->session_id_len);
+               memcpy(kex->session_id, hash, kex->session_id_len);
+       }
+
+       if (kex->gss_deleg_creds)
+               ssh_gssapi_credentials_updated(ctxt);
+
+       if (gss_kex_context == NULL)
+               gss_kex_context = ctxt;
+       else
+               ssh_gssapi_delete_ctx(&ctxt);
+
+       kex_derive_keys(kex, hash, hashlen, shared_secret);
+       BN_clear_free(shared_secret);
+       kex_finish(kex);
+}
+
+#endif /* GSSAPI */
diff --git a/kexgsss.c b/kexgsss.c
new file mode 100644 (file)
index 0000000..d2cb8b0
--- /dev/null
+++ b/kexgsss.c
@@ -0,0 +1,283 @@
+/*
+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "includes.h"
+
+#ifdef GSSAPI
+
+#include <string.h>
+
+#include <openssl/crypto.h>
+#include <openssl/bn.h>
+
+#include "xmalloc.h"
+#include "buffer.h"
+#include "ssh2.h"
+#include "key.h"
+#include "cipher.h"
+#include "kex.h"
+#include "log.h"
+#include "packet.h"
+#include "dh.h"
+#include "ssh-gss.h"
+#include "monitor_wrap.h"
+#include "servconf.h"
+
+extern ServerOptions options;
+
+void
+kexgss_server(Kex *kex)
+{
+       OM_uint32 maj_status, min_status;
+       
+       /* 
+        * Some GSSAPI implementations use the input value of ret_flags (an
+        * output variable) as a means of triggering mechanism specific 
+        * features. Initializing it to zero avoids inadvertently 
+        * activating this non-standard behaviour.
+        */
+
+       OM_uint32 ret_flags = 0;
+       gss_buffer_desc gssbuf, recv_tok, msg_tok;
+       gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
+       Gssctxt *ctxt = NULL;
+       u_int slen, klen, kout, hashlen;
+       u_char *kbuf, *hash;
+       DH *dh;
+       int min = -1, max = -1, nbits = -1;
+       BIGNUM *shared_secret = NULL;
+       BIGNUM *dh_client_pub = NULL;
+       int type = 0;
+       gss_OID oid;
+       char *mechs;
+
+       /* Initialise GSSAPI */
+
+       /* If we're rekeying, privsep means that some of the private structures
+        * in the GSSAPI code are no longer available. This kludges them back
+        * into life
+        */
+       if (!ssh_gssapi_oid_table_ok()) 
+               if ((mechs = ssh_gssapi_server_mechanisms()))
+                       xfree(mechs);
+
+       debug2("%s: Identifying %s", __func__, kex->name);
+       oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type);
+       if (oid == GSS_C_NO_OID)
+          fatal("Unknown gssapi mechanism");
+
+       debug2("%s: Acquiring credentials", __func__);
+
+       if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
+               fatal("Unable to acquire credentials for the server");
+
+       switch (kex->kex_type) {
+       case KEX_GSS_GRP1_SHA1:
+               dh = dh_new_group1();
+               break;
+       case KEX_GSS_GRP14_SHA1:
+               dh = dh_new_group14();
+               break;
+       case KEX_GSS_GEX_SHA1:
+               debug("Doing group exchange");
+               packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ);
+               min = packet_get_int();
+               nbits = packet_get_int();
+               max = packet_get_int();
+               min = MAX(DH_GRP_MIN, min);
+               max = MIN(DH_GRP_MAX, max);
+               packet_check_eom();
+               if (max < min || nbits < min || max < nbits)
+                       fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
+                           min, nbits, max);
+               dh = PRIVSEP(choose_dh(min, nbits, max));
+               if (dh == NULL)
+                       packet_disconnect("Protocol error: no matching group found");
+
+               packet_start(SSH2_MSG_KEXGSS_GROUP);
+               packet_put_bignum2(dh->p);
+               packet_put_bignum2(dh->g);
+               packet_send();
+
+               packet_write_wait();
+               break;
+       default:
+               fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
+       }
+
+       dh_gen_key(dh, kex->we_need * 8);
+
+       do {
+               debug("Wait SSH2_MSG_GSSAPI_INIT");
+               type = packet_read();
+               switch(type) {
+               case SSH2_MSG_KEXGSS_INIT:
+                       if (dh_client_pub != NULL) 
+                               fatal("Received KEXGSS_INIT after initialising");
+                       recv_tok.value = packet_get_string(&slen);
+                       recv_tok.length = slen; 
+
+                       if ((dh_client_pub = BN_new()) == NULL)
+                               fatal("dh_client_pub == NULL");
+
+                       packet_get_bignum2(dh_client_pub);
+
+                       /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
+                       break;
+               case SSH2_MSG_KEXGSS_CONTINUE:
+                       recv_tok.value = packet_get_string(&slen);
+                       recv_tok.length = slen; 
+                       break;
+               default:
+                       packet_disconnect(
+                           "Protocol error: didn't expect packet type %d",
+                           type);
+               }
+
+               maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, 
+                   &send_tok, &ret_flags));
+
+               xfree(recv_tok.value);
+
+               if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
+                       fatal("Zero length token output when incomplete");
+
+               if (dh_client_pub == NULL)
+                       fatal("No client public key");
+               
+               if (maj_status & GSS_S_CONTINUE_NEEDED) {
+                       debug("Sending GSSAPI_CONTINUE");
+                       packet_start(SSH2_MSG_KEXGSS_CONTINUE);
+                       packet_put_string(send_tok.value, send_tok.length);
+                       packet_send();
+                       gss_release_buffer(&min_status, &send_tok);
+               }
+       } while (maj_status & GSS_S_CONTINUE_NEEDED);
+
+       if (GSS_ERROR(maj_status)) {
+               if (send_tok.length > 0) {
+                       packet_start(SSH2_MSG_KEXGSS_CONTINUE);
+                       packet_put_string(send_tok.value, send_tok.length);
+                       packet_send();
+               }
+               fatal("accept_ctx died");
+       }
+
+       if (!(ret_flags & GSS_C_MUTUAL_FLAG))
+               fatal("Mutual Authentication flag wasn't set");
+
+       if (!(ret_flags & GSS_C_INTEG_FLAG))
+               fatal("Integrity flag wasn't set");
+       
+       if (!dh_pub_is_valid(dh, dh_client_pub))
+               packet_disconnect("bad client public DH value");
+
+       klen = DH_size(dh);
+       kbuf = xmalloc(klen); 
+       kout = DH_compute_key(kbuf, dh_client_pub, dh);
+       if (kout < 0)
+               fatal("DH_compute_key: failed");
+
+       shared_secret = BN_new();
+       if (shared_secret == NULL)
+               fatal("kexgss_server: BN_new failed");
+
+       if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
+               fatal("kexgss_server: BN_bin2bn failed");
+
+       memset(kbuf, 0, klen);
+       xfree(kbuf);
+
+       switch (kex->kex_type) {
+       case KEX_GSS_GRP1_SHA1:
+       case KEX_GSS_GRP14_SHA1:
+               kex_dh_hash(
+                   kex->client_version_string, kex->server_version_string,
+                   buffer_ptr(&kex->peer), buffer_len(&kex->peer),
+                   buffer_ptr(&kex->my), buffer_len(&kex->my),
+                   NULL, 0, /* Change this if we start sending host keys */
+                   dh_client_pub, dh->pub_key, shared_secret,
+                   &hash, &hashlen
+               );
+               break;
+       case KEX_GSS_GEX_SHA1:
+               kexgex_hash(
+                   kex->evp_md,
+                   kex->client_version_string, kex->server_version_string,
+                   buffer_ptr(&kex->peer), buffer_len(&kex->peer),
+                   buffer_ptr(&kex->my), buffer_len(&kex->my),
+                   NULL, 0,
+                   min, nbits, max,
+                   dh->p, dh->g,
+                   dh_client_pub,
+                   dh->pub_key,
+                   shared_secret,
+                   &hash, &hashlen
+               );
+               break;
+       default:
+               fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
+       }
+
+       BN_clear_free(dh_client_pub);
+
+       if (kex->session_id == NULL) {
+               kex->session_id_len = hashlen;
+               kex->session_id = xmalloc(kex->session_id_len);
+               memcpy(kex->session_id, hash, kex->session_id_len);
+       }
+
+       gssbuf.value = hash;
+       gssbuf.length = hashlen;
+
+       if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
+               fatal("Couldn't get MIC");
+
+       packet_start(SSH2_MSG_KEXGSS_COMPLETE);
+       packet_put_bignum2(dh->pub_key);
+       packet_put_string(msg_tok.value,msg_tok.length);
+
+       if (send_tok.length != 0) {
+               packet_put_char(1); /* true */
+               packet_put_string(send_tok.value, send_tok.length);
+       } else {
+               packet_put_char(0); /* false */
+       }
+       packet_send();
+
+       gss_release_buffer(&min_status, &send_tok);
+       gss_release_buffer(&min_status, &msg_tok);
+
+       if (gss_kex_context == NULL)
+               gss_kex_context = ctxt;
+       else 
+               ssh_gssapi_delete_ctx(&ctxt);
+
+       DH_free(dh);
+
+       kex_derive_keys(kex, hash, hashlen, shared_secret);
+       BN_clear_free(shared_secret);
+       kex_finish(kex);
+}
+#endif /* GSSAPI */