components/openssh/sources/kexgssc.c
author Tomas Kuthan <tomas.kuthan@oracle.com>
Tue, 20 Sep 2016 03:54:40 -0700
changeset 6930 31ef2580c45d
parent 4503 bf30d46ab06e
child 7561 64cbdac1f406
permissions -rw-r--r--
24525860 upgrade OpenSSH to 7.3p1 24320031 problem in UTILITY/OPENSSH 24461706 problem in UTILITY/OPENSSH
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
     1
/*
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
     2
 * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
     3
 *
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
     6
 * are met:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
     7
 * 1. Redistributions of source code must retain the above copyright
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
     8
 *    notice, this list of conditions and the following disclaimer.
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
     9
 * 2. Redistributions in binary form must reproduce the above copyright
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    10
 *    notice, this list of conditions and the following disclaimer in the
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    11
 *    documentation and/or other materials provided with the distribution.
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    12
 *
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    13
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    14
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    15
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    16
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    17
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    18
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    19
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    20
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    21
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    22
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    23
 */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    24
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    25
/*
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    26
 * May 22, 2015
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    27
 * In version 6.8 a new packet interface has been introduced to OpenSSH,
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    28
 * while the old packet API has been provided in opacket.c.
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    29
 * At this moment we are not rewritting GSS-API key exchange code to the new
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    30
 * API, just adjusting it to still work with new struct ssh.
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    31
 * Rewritting to the new API can be considered in the future.
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    32
 */
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    33
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    34
#include "includes.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    35
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    36
#ifdef GSSAPI
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    37
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    38
#include "includes.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    39
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    40
#include <openssl/crypto.h>
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    41
#include <openssl/bn.h>
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    42
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    43
#include <signal.h>	/* for sig_atomic_t in kex.h */
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    44
#include <string.h>
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    45
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    46
#include "xmalloc.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    47
#include "buffer.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    48
#include "ssh2.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    49
#include "key.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    50
#include "cipher.h"
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    51
#include "digest.h"
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    52
#include "kex.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    53
#include "log.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    54
#include "packet.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    55
#include "dh.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    56
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    57
#include "ssh-gss.h"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    58
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    59
int
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    60
kexgss_client(struct ssh *ssh) {
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    61
	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    62
	gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    63
	Gssctxt *ctxt;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    64
	OM_uint32 maj_status, min_status, ret_flags;
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    65
	uint_t klen, kout, slen = 0, strlen;
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    66
	BIGNUM *dh_server_pub = NULL;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    67
	BIGNUM *shared_secret = NULL;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    68
	BIGNUM *p = NULL;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    69
	BIGNUM *g = NULL;
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    70
	uchar_t *kbuf;
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    71
	uchar_t *serverhostkey = NULL;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    72
	uchar_t *empty = "";
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    73
	char *msg;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    74
	char *lang;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    75
	int type = 0;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    76
	int first = 1;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    77
	int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    78
	struct kex *kex = ssh->kex;
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    79
	int r;
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    80
	uchar_t hash[SSH_DIGEST_MAX_LENGTH];
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    81
	size_t hashlen;
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    82
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    83
	/* Initialise our GSSAPI world */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    84
	ssh_gssapi_build_ctx(&ctxt);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    85
	if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    86
	    == GSS_C_NO_OID)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    87
		fatal("Couldn't identify host exchange");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    88
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    89
	if (ssh_gssapi_import_name(ctxt, kex->gss_host))
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    90
		fatal("Couldn't import hostname");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    91
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    92
	switch (kex->kex_type) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    93
	case KEX_GSS_GRP1_SHA1:
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    94
		kex->dh = dh_new_group1();
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    95
		break;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    96
	case KEX_GSS_GRP14_SHA1:
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
    97
		kex->dh = dh_new_group14();
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    98
		break;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
    99
	case KEX_GSS_GEX_SHA1:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   100
		debug("Doing group exchange\n");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   101
		nbits = dh_estimate(kex->we_need * 8);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   102
		packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   103
		packet_put_int(min);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   104
		packet_put_int(nbits);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   105
		packet_put_int(max);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   106
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   107
		packet_send();
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   108
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   109
		packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   110
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   111
		if ((p = BN_new()) == NULL)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   112
			fatal("BN_new() failed");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   113
		packet_get_bignum2(p);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   114
		if ((g = BN_new()) == NULL)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   115
			fatal("BN_new() failed");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   116
		packet_get_bignum2(g);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   117
		packet_check_eom();
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   118
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   119
		if (BN_num_bits(p) < min || BN_num_bits(p) > max)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   120
			fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   121
			    min, BN_num_bits(p), max);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   122
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   123
		kex->dh = dh_new_group(g, p);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   124
		break;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   125
	default:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   126
		fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   127
	}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   128
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   129
	/* Step 1 - e is dh->pub_key */
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   130
	dh_gen_key(kex->dh, kex->we_need * 8);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   131
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   132
	/* This is f, we initialise it now to make life easier */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   133
	dh_server_pub = BN_new();
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   134
	if (dh_server_pub == NULL)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   135
		fatal("dh_server_pub == NULL");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   136
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   137
	token_ptr = GSS_C_NO_BUFFER;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   138
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   139
	do {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   140
		debug("Calling gss_init_sec_context");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   141
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   142
		maj_status = ssh_gssapi_init_ctx(ctxt,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   143
		    kex->gss_deleg_creds, token_ptr, &send_tok,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   144
		    &ret_flags);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   145
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   146
		if (GSS_ERROR(maj_status)) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   147
			if (send_tok.length != 0) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   148
				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   149
				packet_put_string(send_tok.value,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   150
				    send_tok.length);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   151
			}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   152
			fatal("gss_init_context failed");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   153
		}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   154
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   155
		/* If we've got an old receive buffer get rid of it */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   156
		if (token_ptr != GSS_C_NO_BUFFER)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   157
			free(recv_tok.value);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   158
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   159
		if (maj_status == GSS_S_COMPLETE) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   160
			/* If mutual state flag is not true, kex fails */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   161
			if (!(ret_flags & GSS_C_MUTUAL_FLAG))
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   162
				fatal("Mutual authentication failed");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   163
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   164
			/* If integ avail flag is not true kex fails */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   165
			if (!(ret_flags & GSS_C_INTEG_FLAG))
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   166
				fatal("Integrity check failed");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   167
		}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   168
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   169
		/*
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   170
		 * If we have data to send, then the last message that we
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   171
		 * received cannot have been a 'complete'.
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   172
		 */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   173
		if (send_tok.length != 0) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   174
			if (first) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   175
				packet_start(SSH2_MSG_KEXGSS_INIT);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   176
				packet_put_string(send_tok.value,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   177
				    send_tok.length);
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   178
				packet_put_bignum2(kex->dh->pub_key);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   179
				first = 0;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   180
			} else {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   181
				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   182
				packet_put_string(send_tok.value,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   183
				    send_tok.length);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   184
			}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   185
			packet_send();
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   186
			gss_release_buffer(&min_status, &send_tok);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   187
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   188
			/* If we've sent them data, they should reply */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   189
			do {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   190
				type = packet_read();
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   191
				if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   192
					debug("Received KEXGSS_HOSTKEY");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   193
					if (serverhostkey)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   194
						fatal("Server host key received"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   195
						    "more than once");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   196
					serverhostkey =
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   197
					    packet_get_string(&slen);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   198
				}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   199
			} while (type == SSH2_MSG_KEXGSS_HOSTKEY);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   200
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   201
			switch (type) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   202
			case SSH2_MSG_KEXGSS_CONTINUE:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   203
				debug("Received GSSAPI_CONTINUE");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   204
				if (maj_status == GSS_S_COMPLETE)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   205
					fatal("GSSAPI Continue received from"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   206
					    "server when complete");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   207
				recv_tok.value = packet_get_string(&strlen);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   208
				recv_tok.length = strlen;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   209
				break;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   210
			case SSH2_MSG_KEXGSS_COMPLETE:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   211
				debug("Received GSSAPI_COMPLETE");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   212
				packet_get_bignum2(dh_server_pub);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   213
				msg_tok.value =  packet_get_string(&strlen);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   214
				msg_tok.length = strlen;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   215
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   216
				/* Is there a token included? */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   217
				if (packet_get_char()) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   218
					recv_tok.value=
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   219
					    packet_get_string(&strlen);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   220
					recv_tok.length = strlen;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   221
					/* If complete - protocol error */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   222
					if (maj_status == GSS_S_COMPLETE)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   223
						packet_disconnect("Protocol"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   224
						    " error: received token"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   225
						    " when complete");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   226
				} else {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   227
					/* No token included */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   228
					if (maj_status != GSS_S_COMPLETE)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   229
						packet_disconnect("Protocol"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   230
						    " error: did not receive"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   231
						    " final token");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   232
				}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   233
				break;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   234
			case SSH2_MSG_KEXGSS_ERROR:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   235
				debug("Received Error");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   236
				maj_status = packet_get_int();
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   237
				min_status = packet_get_int();
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   238
				msg = packet_get_string(NULL);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   239
				lang = packet_get_string(NULL);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   240
				fatal("GSSAPI Error: \n%.400s", msg);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   241
			default:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   242
				packet_disconnect("Protocol error: didn't"
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   243
				    " expect packet type %d", type);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   244
			}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   245
			token_ptr = &recv_tok;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   246
		} else {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   247
			/* No data, and not complete */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   248
			if (maj_status != GSS_S_COMPLETE)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   249
				fatal("Not complete, and no token output");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   250
		}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   251
	} while (maj_status & GSS_S_CONTINUE_NEEDED);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   252
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   253
	/*
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   254
	 * We _must_ have received a COMPLETE message in reply from the
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   255
	 * server, which will have set dh_server_pub and msg_tok
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   256
	 */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   257
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   258
	if (type != SSH2_MSG_KEXGSS_COMPLETE)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   259
		fatal("Didn't receive SSH2_MSG_KEXGSS_COMPLETE when expected");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   260
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   261
	/* Check f in range [1, p-1] */
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   262
	if (!dh_pub_is_valid(kex->dh, dh_server_pub))
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   263
		packet_disconnect("bad server public DH value");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   264
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   265
	/* compute K=f^x mod p */
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   266
	klen = DH_size(kex->dh);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   267
	kbuf = xmalloc(klen);
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   268
	kout = DH_compute_key(kbuf, dh_server_pub, kex->dh);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   269
	if (kout < 0)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   270
		fatal("DH_compute_key: failed");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   271
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   272
	shared_secret = BN_new();
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   273
	if (shared_secret == NULL)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   274
		fatal("kexgss_client: BN_new failed");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   275
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   276
	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   277
		fatal("kexdh_client: BN_bin2bn failed");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   278
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   279
	memset(kbuf, 0, klen);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   280
	free(kbuf);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   281
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   282
	hashlen = sizeof (hash);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   283
	switch (kex->kex_type) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   284
	case KEX_GSS_GRP1_SHA1:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   285
	case KEX_GSS_GRP14_SHA1:
6930
31ef2580c45d 24525860 upgrade OpenSSH to 7.3p1
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 4503
diff changeset
   286
		kex_dh_hash(
31ef2580c45d 24525860 upgrade OpenSSH to 7.3p1
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 4503
diff changeset
   287
		    kex->hash_alg,
31ef2580c45d 24525860 upgrade OpenSSH to 7.3p1
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 4503
diff changeset
   288
		    kex->client_version_string,
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   289
		    kex->server_version_string,
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   290
		    buffer_ptr(kex->my), buffer_len(kex->my),
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   291
		    buffer_ptr(kex->peer), buffer_len(kex->peer),
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   292
		    (serverhostkey ? serverhostkey : empty), slen,
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   293
		    kex->dh->pub_key,	/* e */
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   294
		    dh_server_pub,	/* f */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   295
		    shared_secret,	/* K */
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   296
		    hash, &hashlen);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   297
		break;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   298
	case KEX_GSS_GEX_SHA1:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   299
		kexgex_hash(
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   300
		    kex->hash_alg,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   301
		    kex->client_version_string,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   302
		    kex->server_version_string,
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   303
		    buffer_ptr(kex->my), buffer_len(kex->my),
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   304
		    buffer_ptr(kex->peer), buffer_len(kex->peer),
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   305
		    (serverhostkey ? serverhostkey : empty), slen,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   306
		    min, nbits, max,
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   307
		    kex->dh->p, kex->dh->g,
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   308
		    kex->dh->pub_key,
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   309
		    dh_server_pub,
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   310
		    shared_secret,
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   311
		    hash, &hashlen);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   312
		break;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   313
	default:
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   314
		fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   315
	}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   316
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   317
	gssbuf.value = hash;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   318
	gssbuf.length = hashlen;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   319
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   320
	/* Verify that the hash matches the MIC we just got. */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   321
	if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   322
		packet_disconnect("Hash's MIC didn't verify");
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   323
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   324
	free(msg_tok.value);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   325
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   326
	DH_free(kex->dh);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   327
	if (serverhostkey)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   328
		free(serverhostkey);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   329
	BN_clear_free(dh_server_pub);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   330
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   331
	/* save session id */
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   332
	if (kex->session_id == NULL) {
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   333
		kex->session_id_len = hashlen;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   334
		kex->session_id = xmalloc(kex->session_id_len);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   335
		memcpy(kex->session_id, hash, kex->session_id_len);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   336
	}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   337
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   338
	if (gss_kex_context == NULL)
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   339
		gss_kex_context = ctxt;
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   340
	else
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   341
		ssh_gssapi_delete_ctx(&ctxt);
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   342
4503
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   343
	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   344
		r = kex_send_newkeys(ssh);
bf30d46ab06e PSARC/2015/179 OpenSSH 6.8
Tomas Kuthan <tomas.kuthan@oracle.com>
parents: 3574
diff changeset
   345
	return (r);
3574
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   346
}
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   347
80e9a4e240d3 PSARC/2014/390 OpenSSH GSSKEY
Tomas Kuthan <tomas.kuthan@oracle.com>
parents:
diff changeset
   348
#endif /* GSSAPI */