5322N/A/*
5322N/A * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
5322N/A *
5322N/A * Redistribution and use in source and binary forms, with or without
5322N/A * modification, are permitted provided that the following conditions
5322N/A * are met:
5322N/A * 1. Redistributions of source code must retain the above copyright
5322N/A * notice, this list of conditions and the following disclaimer.
5322N/A * 2. Redistributions in binary form must reproduce the above copyright
5322N/A * notice, this list of conditions and the following disclaimer in the
5322N/A * documentation and/or other materials provided with the distribution.
5322N/A *
5322N/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
5322N/A * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5322N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5322N/A * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5322N/A * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5322N/A * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5322N/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5322N/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5322N/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5322N/A * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5322N/A */
5322N/A
5322N/A/*
5322N/A * May 22, 2015
5322N/A * In version 6.8 a new packet interface has been introduced to OpenSSH,
5322N/A * while the old packet API has been provided in opacket.c.
5322N/A * At this moment we are not rewritting GSS-API key exchange code to the new
5322N/A * API, just adjusting it to still work with new struct ssh.
5322N/A * Rewritting to the new API can be considered in the future.
5322N/A */
5322N/A
5322N/A#include "includes.h"
5322N/A
5322N/A#ifdef GSSAPI
5322N/A
5322N/A#include "includes.h"
5322N/A
5322N/A#include <openssl/crypto.h>
5322N/A#include <openssl/bn.h>
5322N/A
5322N/A#include <signal.h> /* for sig_atomic_t in kex.h */
5322N/A#include <string.h>
5322N/A
5322N/A#include "xmalloc.h"
5322N/A#include "buffer.h"
5322N/A#include "ssh2.h"
5322N/A#include "key.h"
5322N/A#include "cipher.h"
5322N/A#include "digest.h"
5322N/A#include "kex.h"
5322N/A#include "log.h"
5322N/A#include "packet.h"
5322N/A#include "dh.h"
5322N/A
5322N/A#include "ssh-gss.h"
5322N/A
5322N/Aint
5322N/Akexgss_client(struct ssh *ssh) {
5322N/A gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
5322N/A gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
5322N/A Gssctxt *ctxt;
5322N/A OM_uint32 maj_status, min_status, ret_flags;
5322N/A uint_t klen, kout, slen = 0, strlen;
5322N/A DH *dh;
5322N/A BIGNUM *dh_server_pub = NULL;
5322N/A BIGNUM *shared_secret = NULL;
5322N/A BIGNUM *p = NULL;
5322N/A BIGNUM *g = NULL;
5322N/A uchar_t *kbuf;
5322N/A uchar_t *serverhostkey = NULL;
5322N/A uchar_t *empty = "";
5322N/A char *msg;
5322N/A char *lang;
5322N/A int type = 0;
5322N/A int first = 1;
5322N/A int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
5322N/A struct kex *kex = ssh->kex;
5322N/A int r;
5322N/A uchar_t hash[SSH_DIGEST_MAX_LENGTH];
5322N/A size_t hashlen;
5322N/A
5322N/A /* Initialise our GSSAPI world */
5322N/A ssh_gssapi_build_ctx(&ctxt);
5322N/A if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type)
5322N/A == GSS_C_NO_OID)
5322N/A fatal("Couldn't identify host exchange");
5322N/A
5322N/A if (ssh_gssapi_import_name(ctxt, kex->gss_host))
5322N/A fatal("Couldn't import hostname");
5322N/A
5322N/A switch (kex->kex_type) {
5322N/A case KEX_GSS_GRP1_SHA1:
5322N/A kex->dh = dh_new_group1();
5322N/A break;
5322N/A case KEX_GSS_GRP14_SHA1:
5322N/A kex->dh = dh_new_group14();
5322N/A break;
5322N/A case KEX_GSS_GEX_SHA1:
5322N/A debug("Doing group exchange\n");
5322N/A nbits = dh_estimate(kex->we_need * 8);
5322N/A packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
5322N/A packet_put_int(min);
5322N/A packet_put_int(nbits);
5322N/A packet_put_int(max);
5322N/A
5322N/A packet_send();
5322N/A
5322N/A packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
5322N/A
5322N/A if ((p = BN_new()) == NULL)
5322N/A fatal("BN_new() failed");
5322N/A packet_get_bignum2(p);
5322N/A if ((g = BN_new()) == NULL)
5322N/A fatal("BN_new() failed");
5322N/A packet_get_bignum2(g);
5322N/A packet_check_eom();
5322N/A
5322N/A if (BN_num_bits(p) < min || BN_num_bits(p) > max)
5322N/A fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
5322N/A min, BN_num_bits(p), max);
5322N/A
5322N/A kex->dh = dh_new_group(g, p);
5322N/A break;
5322N/A default:
5322N/A fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
5322N/A }
5322N/A
5322N/A /* Step 1 - e is dh->pub_key */
5322N/A dh_gen_key(kex->dh, kex->we_need * 8);
5322N/A
5322N/A /* This is f, we initialise it now to make life easier */
5322N/A dh_server_pub = BN_new();
5322N/A if (dh_server_pub == NULL)
5322N/A fatal("dh_server_pub == NULL");
5322N/A
5322N/A token_ptr = GSS_C_NO_BUFFER;
5322N/A
5322N/A do {
5322N/A debug("Calling gss_init_sec_context");
5322N/A
5322N/A maj_status = ssh_gssapi_init_ctx(ctxt,
5322N/A kex->gss_deleg_creds, token_ptr, &send_tok,
5322N/A &ret_flags);
5322N/A
5322N/A if (GSS_ERROR(maj_status)) {
5322N/A if (send_tok.length != 0) {
5322N/A packet_start(SSH2_MSG_KEXGSS_CONTINUE);
5322N/A packet_put_string(send_tok.value,
5322N/A send_tok.length);
5322N/A }
5322N/A fatal("gss_init_context failed");
5322N/A }
5322N/A
5322N/A /* If we've got an old receive buffer get rid of it */
5322N/A if (token_ptr != GSS_C_NO_BUFFER)
5322N/A free(recv_tok.value);
5322N/A
5322N/A if (maj_status == GSS_S_COMPLETE) {
5322N/A /* If mutual state flag is not true, kex fails */
5322N/A if (!(ret_flags & GSS_C_MUTUAL_FLAG))
5322N/A fatal("Mutual authentication failed");
5322N/A
5322N/A /* If integ avail flag is not true kex fails */
5322N/A if (!(ret_flags & GSS_C_INTEG_FLAG))
5322N/A fatal("Integrity check failed");
5322N/A }
5322N/A
5322N/A /*
5322N/A * If we have data to send, then the last message that we
5322N/A * received cannot have been a 'complete'.
5322N/A */
5322N/A if (send_tok.length != 0) {
5322N/A if (first) {
5322N/A packet_start(SSH2_MSG_KEXGSS_INIT);
5322N/A packet_put_string(send_tok.value,
5322N/A send_tok.length);
5322N/A packet_put_bignum2(kex->dh->pub_key);
5322N/A first = 0;
5322N/A } else {
5322N/A packet_start(SSH2_MSG_KEXGSS_CONTINUE);
5322N/A packet_put_string(send_tok.value,
5322N/A send_tok.length);
5322N/A }
5322N/A packet_send();
5322N/A gss_release_buffer(&min_status, &send_tok);
5322N/A
5322N/A /* If we've sent them data, they should reply */
5322N/A do {
5322N/A type = packet_read();
5322N/A if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
5322N/A debug("Received KEXGSS_HOSTKEY");
5322N/A if (serverhostkey)
5322N/A fatal("Server host key received"
5322N/A "more than once");
5322N/A serverhostkey =
5322N/A packet_get_string(&slen);
5322N/A }
5322N/A } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
5322N/A
5322N/A switch (type) {
5322N/A case SSH2_MSG_KEXGSS_CONTINUE:
5322N/A debug("Received GSSAPI_CONTINUE");
5322N/A if (maj_status == GSS_S_COMPLETE)
5322N/A fatal("GSSAPI Continue received from"
5322N/A "server when complete");
5322N/A recv_tok.value = packet_get_string(&strlen);
5322N/A recv_tok.length = strlen;
5322N/A break;
5322N/A case SSH2_MSG_KEXGSS_COMPLETE:
5322N/A debug("Received GSSAPI_COMPLETE");
5322N/A packet_get_bignum2(dh_server_pub);
5322N/A msg_tok.value = packet_get_string(&strlen);
5322N/A msg_tok.length = strlen;
5322N/A
5322N/A /* Is there a token included? */
5322N/A if (packet_get_char()) {
5322N/A recv_tok.value=
5322N/A packet_get_string(&strlen);
5322N/A recv_tok.length = strlen;
5322N/A /* If complete - protocol error */
5322N/A if (maj_status == GSS_S_COMPLETE)
5322N/A packet_disconnect("Protocol"
5322N/A " error: received token"
5322N/A " when complete");
5322N/A } else {
5322N/A /* No token included */
5322N/A if (maj_status != GSS_S_COMPLETE)
5322N/A packet_disconnect("Protocol"
5322N/A " error: did not receive"
5322N/A " final token");
5322N/A }
5322N/A break;
5322N/A case SSH2_MSG_KEXGSS_ERROR:
5322N/A debug("Received Error");
5322N/A maj_status = packet_get_int();
5322N/A min_status = packet_get_int();
5322N/A msg = packet_get_string(NULL);
5322N/A lang = packet_get_string(NULL);
5322N/A fatal("GSSAPI Error: \n%.400s", msg);
5322N/A default:
5322N/A packet_disconnect("Protocol error: didn't"
5322N/A " expect packet type %d", type);
5322N/A }
5322N/A token_ptr = &recv_tok;
5322N/A } else {
5322N/A /* No data, and not complete */
5322N/A if (maj_status != GSS_S_COMPLETE)
5322N/A fatal("Not complete, and no token output");
5322N/A }
5322N/A } while (maj_status & GSS_S_CONTINUE_NEEDED);
5322N/A
5322N/A /*
5322N/A * We _must_ have received a COMPLETE message in reply from the
5322N/A * server, which will have set dh_server_pub and msg_tok
5322N/A */
5322N/A
5322N/A if (type != SSH2_MSG_KEXGSS_COMPLETE)
5322N/A fatal("Didn't receive SSH2_MSG_KEXGSS_COMPLETE when expected");
5322N/A
5322N/A /* Check f in range [1, p-1] */
5322N/A if (!dh_pub_is_valid(kex->dh, dh_server_pub))
5322N/A packet_disconnect("bad server public DH value");
5322N/A
5322N/A /* compute K=f^x mod p */
5322N/A klen = DH_size(kex->dh);
5322N/A kbuf = xmalloc(klen);
5322N/A kout = DH_compute_key(kbuf, dh_server_pub, kex->dh);
5322N/A if (kout < 0)
5322N/A fatal("DH_compute_key: failed");
5322N/A
5322N/A shared_secret = BN_new();
5322N/A if (shared_secret == NULL)
5322N/A fatal("kexgss_client: BN_new failed");
5322N/A
5322N/A if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
5322N/A fatal("kexdh_client: BN_bin2bn failed");
5322N/A
5322N/A memset(kbuf, 0, klen);
5322N/A free(kbuf);
5322N/A
5322N/A hashlen = sizeof (hash);
5322N/A switch (kex->kex_type) {
5322N/A case KEX_GSS_GRP1_SHA1:
5322N/A case KEX_GSS_GRP14_SHA1:
5322N/A kex_dh_hash(kex->client_version_string,
5322N/A kex->server_version_string,
5322N/A buffer_ptr(kex->my), buffer_len(kex->my),
5322N/A buffer_ptr(kex->peer), buffer_len(kex->peer),
5322N/A (serverhostkey ? serverhostkey : empty), slen,
5322N/A kex->dh->pub_key, /* e */
5322N/A dh_server_pub, /* f */
5322N/A shared_secret, /* K */
5322N/A hash, &hashlen);
5322N/A break;
5322N/A case KEX_GSS_GEX_SHA1:
5322N/A kexgex_hash(
5322N/A kex->hash_alg,
5322N/A kex->client_version_string,
5322N/A kex->server_version_string,
5322N/A buffer_ptr(kex->my), buffer_len(kex->my),
5322N/A buffer_ptr(kex->peer), buffer_len(kex->peer),
5322N/A (serverhostkey ? serverhostkey : empty), slen,
5322N/A min, nbits, max,
5322N/A kex->dh->p, kex->dh->g,
5322N/A kex->dh->pub_key,
5322N/A dh_server_pub,
5322N/A shared_secret,
5322N/A hash, &hashlen);
5322N/A break;
5322N/A default:
5322N/A fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
5322N/A }
5322N/A
5322N/A gssbuf.value = hash;
5322N/A gssbuf.length = hashlen;
5322N/A
5322N/A /* Verify that the hash matches the MIC we just got. */
5322N/A if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
5322N/A packet_disconnect("Hash's MIC didn't verify");
5322N/A
5322N/A free(msg_tok.value);
5322N/A
5322N/A DH_free(kex->dh);
5322N/A if (serverhostkey)
5322N/A free(serverhostkey);
5322N/A BN_clear_free(dh_server_pub);
5322N/A
5322N/A /* save session id */
5322N/A if (kex->session_id == NULL) {
5322N/A kex->session_id_len = hashlen;
5322N/A kex->session_id = xmalloc(kex->session_id_len);
5322N/A memcpy(kex->session_id, hash, kex->session_id_len);
5322N/A }
5322N/A
5322N/A if (gss_kex_context == NULL)
5322N/A gss_kex_context = ctxt;
5322N/A else
5322N/A ssh_gssapi_delete_ctx(&ctxt);
5322N/A
5322N/A if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
5322N/A r = kex_send_newkeys(ssh);
5322N/A return (r);
5322N/A}
5322N/A
5322N/A#endif /* GSSAPI */