#include "includes.h"
#ifdef GSSAPI
#include <openssl/crypto.h>
#include <openssl/bn.h>
#include "xmalloc.h"
#include "buffer.h"
#include "bufaux.h"
#include "kex.h"
#include "log.h"
#include "packet.h"
#include "dh.h"
#include "canohost.h"
#include "ssh2.h"
#include "ssh-gss.h"
void
kexgss_client(Kex *kex)
{
gss_buffer_desc gssbuf,send_tok,recv_tok, msg_tok, *token_ptr;
Gssctxt *ctxt;
OM_uint32 maj_status, min_status, ret_flags;
unsigned int klen, kout;
DH *dh;
BIGNUM *dh_server_pub = 0;
BIGNUM *shared_secret = 0;
unsigned char *kbuf;
unsigned char *hash;
unsigned char *serverhostkey;
char *msg;
char *lang;
int type = 0;
int first = 1;
int slen = 0;
u_int strlen;
ssh_gssapi_build_ctx(&ctxt);
if (ssh_gssapi_client_id_kex(ctxt,kex->name)==NULL) {
fatal("Couldn't identify host exchange");
}
if (ssh_gssapi_import_name(ctxt,get_canonical_hostname(1))) {
fatal("Couldn't import hostname ");
}
dh = dh_new_group1();
dh_gen_key(dh, kex->we_need * 8);
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->options.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 (token_ptr != GSS_C_NO_BUFFER)
(void) gss_release_buffer(&min_status, &recv_tok);
if (maj_status == GSS_S_COMPLETE) {
if (!(ret_flags & GSS_C_MUTUAL_FLAG)) {
fatal("Mutual authentication failed");
}
if (!(ret_flags & GSS_C_INTEG_FLAG)) {
fatal("Integrity check failed");
}
}
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();
packet_write_wait();
type = packet_read();
switch (type) {
case SSH2_MSG_KEXGSS_HOSTKEY:
debug("Received KEXGSS_HOSTKEY");
serverhostkey=packet_get_string(&slen);
break;
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;
if (packet_get_char()) {
recv_tok.value=
packet_get_string(&strlen);
recv_tok.length=strlen;
if (maj_status == GSS_S_COMPLETE)
packet_disconnect("Protocol error: received token when complete");
} else {
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);
debug("GSSAPI Error: \n%s",msg);
default:
packet_disconnect("Protocol error: didn't expect packet type %d",
type);
}
token_ptr=&recv_tok;
} else {
if (maj_status!=GSS_S_COMPLETE) {
fatal("Not complete, and no token output");
}
}
} while (maj_status & GSS_S_CONTINUE_NEEDED);
if (type!=SSH2_MSG_KEXGSS_COMPLETE)
fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
if (!dh_pub_is_valid(dh, dh_server_pub))
packet_disconnect("bad server public DH value");
klen = DH_size(dh);
kbuf = xmalloc(klen);
kout = DH_compute_key(kbuf, dh_server_pub, dh);
shared_secret = BN_new();
BN_bin2bn(kbuf,kout, shared_secret);
memset(kbuf, 0, klen);
xfree(kbuf);
hash = 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, slen,
dh->pub_key,
dh_server_pub,
shared_secret
);
gssbuf.value=hash;
gssbuf.length=20;
if ((maj_status = gss_verify_mic(&min_status,
ctxt->context,
&gssbuf,
&msg_tok,
NULL))) {
packet_disconnect("Hash's MIC didn't verify");
}
DH_free(dh);
ssh_gssapi_delete_ctx(&ctxt);
if (kex->session_id == NULL) {
kex->session_id_len = 20;
kex->session_id = xmalloc(kex->session_id_len);
memcpy(kex->session_id, hash, kex->session_id_len);
}
kex_derive_keys(kex, hash, shared_secret);
BN_clear_free(shared_secret);
kex_finish(kex);
}
#endif