1264377Sdes/* $OpenBSD: kexgexc.c,v 1.17 2014/02/02 03:44:31 djm Exp $ */
2113908Sdes/*
3113908Sdes * Copyright (c) 2000 Niels Provos.  All rights reserved.
4113908Sdes * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5113908Sdes *
6113908Sdes * Redistribution and use in source and binary forms, with or without
7113908Sdes * modification, are permitted provided that the following conditions
8113908Sdes * are met:
9113908Sdes * 1. Redistributions of source code must retain the above copyright
10113908Sdes *    notice, this list of conditions and the following disclaimer.
11113908Sdes * 2. Redistributions in binary form must reproduce the above copyright
12113908Sdes *    notice, this list of conditions and the following disclaimer in the
13113908Sdes *    documentation and/or other materials provided with the distribution.
14113908Sdes *
15113908Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16113908Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17113908Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18113908Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19113908Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20113908Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21113908Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22113908Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23113908Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24113908Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25113908Sdes */
26113908Sdes
27113908Sdes#include "includes.h"
28113908Sdes
29162852Sdes#include <sys/types.h>
30162852Sdes
31221420Sdes#include <openssl/dh.h>
32221420Sdes
33162852Sdes#include <stdarg.h>
34162852Sdes#include <stdio.h>
35162852Sdes#include <string.h>
36162852Sdes#include <signal.h>
37162852Sdes
38113908Sdes#include "xmalloc.h"
39162852Sdes#include "buffer.h"
40113908Sdes#include "key.h"
41162852Sdes#include "cipher.h"
42113908Sdes#include "kex.h"
43113908Sdes#include "log.h"
44113908Sdes#include "packet.h"
45113908Sdes#include "dh.h"
46113908Sdes#include "ssh2.h"
47113908Sdes#include "compat.h"
48113908Sdes
49113908Sdesvoid
50113908Sdeskexgex_client(Kex *kex)
51113908Sdes{
52113908Sdes	BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
53113908Sdes	BIGNUM *p = NULL, *g = NULL;
54113908Sdes	Key *server_host_key;
55113908Sdes	u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
56164146Sdes	u_int klen, slen, sbloblen, hashlen;
57164146Sdes	int kout;
58113908Sdes	int min, max, nbits;
59113908Sdes	DH *dh;
60113908Sdes
61262566Sdes	nbits = dh_estimate(kex->dh_need * 8);
62113908Sdes
63113908Sdes	if (datafellows & SSH_OLD_DHGEX) {
64113908Sdes		/* Old GEX request */
65113908Sdes		packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
66113908Sdes		packet_put_int(nbits);
67113908Sdes		min = DH_GRP_MIN;
68113908Sdes		max = DH_GRP_MAX;
69126274Sdes
70126274Sdes		debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
71113908Sdes	} else {
72113908Sdes		/* New GEX request */
73113908Sdes		min = DH_GRP_MIN;
74113908Sdes		max = DH_GRP_MAX;
75113908Sdes		packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
76113908Sdes		packet_put_int(min);
77113908Sdes		packet_put_int(nbits);
78113908Sdes		packet_put_int(max);
79126274Sdes
80126274Sdes		debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
81126274Sdes		    min, nbits, max);
82113908Sdes	}
83113908Sdes#ifdef DEBUG_KEXDH
84113908Sdes	fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
85113908Sdes	    min, nbits, max);
86113908Sdes#endif
87113908Sdes	packet_send();
88113908Sdes
89113908Sdes	debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
90113908Sdes	packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
91113908Sdes
92113908Sdes	if ((p = BN_new()) == NULL)
93113908Sdes		fatal("BN_new");
94113908Sdes	packet_get_bignum2(p);
95113908Sdes	if ((g = BN_new()) == NULL)
96113908Sdes		fatal("BN_new");
97113908Sdes	packet_get_bignum2(g);
98113908Sdes	packet_check_eom();
99113908Sdes
100113908Sdes	if (BN_num_bits(p) < min || BN_num_bits(p) > max)
101113908Sdes		fatal("DH_GEX group out of range: %d !< %d !< %d",
102113908Sdes		    min, BN_num_bits(p), max);
103113908Sdes
104113908Sdes	dh = dh_new_group(g, p);
105113908Sdes	dh_gen_key(dh, kex->we_need * 8);
106113908Sdes
107113908Sdes#ifdef DEBUG_KEXDH
108113908Sdes	DHparams_print_fp(stderr, dh);
109113908Sdes	fprintf(stderr, "pub= ");
110113908Sdes	BN_print_fp(stderr, dh->pub_key);
111113908Sdes	fprintf(stderr, "\n");
112113908Sdes#endif
113113908Sdes
114113908Sdes	debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
115113908Sdes	/* generate and send 'e', client DH public key */
116113908Sdes	packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
117113908Sdes	packet_put_bignum2(dh->pub_key);
118113908Sdes	packet_send();
119113908Sdes
120113908Sdes	debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
121113908Sdes	packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
122113908Sdes
123113908Sdes	/* key, cert */
124113908Sdes	server_host_key_blob = packet_get_string(&sbloblen);
125113908Sdes	server_host_key = key_from_blob(server_host_key_blob, sbloblen);
126113908Sdes	if (server_host_key == NULL)
127113908Sdes		fatal("cannot decode server_host_key_blob");
128113908Sdes	if (server_host_key->type != kex->hostkey_type)
129113908Sdes		fatal("type mismatch for decoded server_host_key_blob");
130113908Sdes	if (kex->verify_host_key == NULL)
131113908Sdes		fatal("cannot verify server_host_key");
132113908Sdes	if (kex->verify_host_key(server_host_key) == -1)
133113908Sdes		fatal("server_host_key verification failed");
134113908Sdes
135162852Sdes	/* DH parameter f, server public DH key */
136113908Sdes	if ((dh_server_pub = BN_new()) == NULL)
137113908Sdes		fatal("dh_server_pub == NULL");
138113908Sdes	packet_get_bignum2(dh_server_pub);
139113908Sdes
140113908Sdes#ifdef DEBUG_KEXDH
141113908Sdes	fprintf(stderr, "dh_server_pub= ");
142113908Sdes	BN_print_fp(stderr, dh_server_pub);
143113908Sdes	fprintf(stderr, "\n");
144113908Sdes	debug("bits %d", BN_num_bits(dh_server_pub));
145113908Sdes#endif
146113908Sdes
147113908Sdes	/* signed H */
148113908Sdes	signature = packet_get_string(&slen);
149113908Sdes	packet_check_eom();
150113908Sdes
151113908Sdes	if (!dh_pub_is_valid(dh, dh_server_pub))
152113908Sdes		packet_disconnect("bad server public DH value");
153113908Sdes
154113908Sdes	klen = DH_size(dh);
155113908Sdes	kbuf = xmalloc(klen);
156164146Sdes	if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0)
157164146Sdes		fatal("DH_compute_key: failed");
158113908Sdes#ifdef DEBUG_KEXDH
159113908Sdes	dump_digest("shared secret", kbuf, kout);
160113908Sdes#endif
161113908Sdes	if ((shared_secret = BN_new()) == NULL)
162113908Sdes		fatal("kexgex_client: BN_new failed");
163164146Sdes	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
164164146Sdes		fatal("kexgex_client: BN_bin2bn failed");
165264377Sdes	explicit_bzero(kbuf, klen);
166255767Sdes	free(kbuf);
167113908Sdes
168113908Sdes	if (datafellows & SSH_OLD_DHGEX)
169113908Sdes		min = max = -1;
170113908Sdes
171113908Sdes	/* calc and verify H */
172157016Sdes	kexgex_hash(
173262566Sdes	    kex->hash_alg,
174113908Sdes	    kex->client_version_string,
175113908Sdes	    kex->server_version_string,
176113908Sdes	    buffer_ptr(&kex->my), buffer_len(&kex->my),
177113908Sdes	    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
178113908Sdes	    server_host_key_blob, sbloblen,
179113908Sdes	    min, nbits, max,
180113908Sdes	    dh->p, dh->g,
181113908Sdes	    dh->pub_key,
182113908Sdes	    dh_server_pub,
183157016Sdes	    shared_secret,
184157016Sdes	    &hash, &hashlen
185113908Sdes	);
186157016Sdes
187113908Sdes	/* have keys, free DH */
188113908Sdes	DH_free(dh);
189255767Sdes	free(server_host_key_blob);
190113908Sdes	BN_clear_free(dh_server_pub);
191113908Sdes
192157016Sdes	if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
193113908Sdes		fatal("key_verify failed for server_host_key");
194113908Sdes	key_free(server_host_key);
195255767Sdes	free(signature);
196113908Sdes
197113908Sdes	/* save session id */
198113908Sdes	if (kex->session_id == NULL) {
199157016Sdes		kex->session_id_len = hashlen;
200113908Sdes		kex->session_id = xmalloc(kex->session_id_len);
201113908Sdes		memcpy(kex->session_id, hash, kex->session_id_len);
202113908Sdes	}
203262566Sdes	kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
204113908Sdes	BN_clear_free(shared_secret);
205113908Sdes
206113908Sdes	kex_finish(kex);
207113908Sdes}
208