1/* $OpenBSD: kexgexs.c,v 1.14 2010/11/10 01:33:07 djm Exp $ */
2/*
3 * Copyright (c) 2000 Niels Provos.  All rights reserved.
4 * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
28
29#include <sys/param.h>
30
31#include <stdarg.h>
32#include <stdio.h>
33#include <string.h>
34#include <signal.h>
35
36#ifdef __APPLE_CRYPTO__
37#include "ossl-dh.h"
38#else
39#include <openssl/dh.h>
40#endif
41
42#include "xmalloc.h"
43#include "buffer.h"
44#include "key.h"
45#include "cipher.h"
46#include "kex.h"
47#include "log.h"
48#include "packet.h"
49#include "dh.h"
50#include "ssh2.h"
51#include "compat.h"
52#ifdef GSSAPI
53#include "ssh-gss.h"
54#endif
55#include "monitor_wrap.h"
56
57void
58kexgex_server(Kex *kex)
59{
60	BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
61	Key *server_host_public, *server_host_private;
62	DH *dh;
63	u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
64	u_int sbloblen, klen, slen, hashlen;
65	int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1;
66	int type, kout;
67
68	if (kex->load_host_public_key == NULL ||
69	    kex->load_host_private_key == NULL)
70		fatal("Cannot load hostkey");
71	server_host_public = kex->load_host_public_key(kex->hostkey_type);
72	if (server_host_public == NULL)
73		fatal("Unsupported hostkey type %d", kex->hostkey_type);
74	server_host_private = kex->load_host_private_key(kex->hostkey_type);
75	if (server_host_private == NULL)
76		fatal("Missing private key for hostkey type %d",
77		    kex->hostkey_type);
78
79
80	type = packet_read();
81	switch (type) {
82	case SSH2_MSG_KEX_DH_GEX_REQUEST:
83		debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
84		omin = min = packet_get_int();
85		onbits = nbits = packet_get_int();
86		omax = max = packet_get_int();
87		min = MAX(DH_GRP_MIN, min);
88		max = MIN(DH_GRP_MAX, max);
89		nbits = MAX(DH_GRP_MIN, nbits);
90		nbits = MIN(DH_GRP_MAX, nbits);
91		break;
92	case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
93		debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
94		onbits = nbits = packet_get_int();
95		/* unused for old GEX */
96		omin = min = DH_GRP_MIN;
97		omax = max = DH_GRP_MAX;
98		break;
99	default:
100		fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
101	}
102	packet_check_eom();
103
104	if (omax < omin || onbits < omin || omax < onbits)
105		fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
106		    omin, onbits, omax);
107
108	/* Contact privileged parent */
109	dh = PRIVSEP(choose_dh(min, nbits, max));
110	if (dh == NULL)
111		packet_disconnect("Protocol error: no matching DH grp found");
112
113	debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
114	packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
115	packet_put_bignum2(dh->p);
116	packet_put_bignum2(dh->g);
117	packet_send();
118
119	/* flush */
120	packet_write_wait();
121
122	/* Compute our exchange value in parallel with the client */
123	dh_gen_key(dh, kex->we_need * 8);
124
125	debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
126	packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
127
128	/* key, cert */
129	if ((dh_client_pub = BN_new()) == NULL)
130		fatal("dh_client_pub == NULL");
131	packet_get_bignum2(dh_client_pub);
132	packet_check_eom();
133
134#ifdef DEBUG_KEXDH
135	fprintf(stderr, "dh_client_pub= ");
136	BN_print_fp(stderr, dh_client_pub);
137	fprintf(stderr, "\n");
138	debug("bits %d", BN_num_bits(dh_client_pub));
139#endif
140
141#ifdef DEBUG_KEXDH
142	DHparams_print_fp(stderr, dh);
143	fprintf(stderr, "pub= ");
144	BN_print_fp(stderr, dh->pub_key);
145	fprintf(stderr, "\n");
146#endif
147	if (!dh_pub_is_valid(dh, dh_client_pub))
148		packet_disconnect("bad client public DH value");
149
150	klen = DH_size(dh);
151	kbuf = xmalloc(klen);
152	if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
153		fatal("DH_compute_key: failed");
154#ifdef DEBUG_KEXDH
155	dump_digest("shared secret", kbuf, kout);
156#endif
157	if ((shared_secret = BN_new()) == NULL)
158		fatal("kexgex_server: BN_new failed");
159	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
160		fatal("kexgex_server: BN_bin2bn failed");
161	memset(kbuf, 0, klen);
162	xfree(kbuf);
163
164	key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
165
166	if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
167		omin = min = omax = max = -1;
168
169	/* calc H */
170	kexgex_hash(
171	    kex->evp_md,
172	    kex->client_version_string,
173	    kex->server_version_string,
174	    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
175	    buffer_ptr(&kex->my), buffer_len(&kex->my),
176	    server_host_key_blob, sbloblen,
177	    omin, onbits, omax,
178	    dh->p, dh->g,
179	    dh_client_pub,
180	    dh->pub_key,
181	    shared_secret,
182	    &hash, &hashlen
183	);
184	BN_clear_free(dh_client_pub);
185
186	/* save session id := H */
187	if (kex->session_id == NULL) {
188		kex->session_id_len = hashlen;
189		kex->session_id = xmalloc(kex->session_id_len);
190		memcpy(kex->session_id, hash, kex->session_id_len);
191	}
192
193	/* sign H */
194	if (PRIVSEP(key_sign(server_host_private, &signature, &slen, hash,
195	    hashlen)) < 0)
196		fatal("kexgex_server: key_sign failed");
197
198	/* destroy_sensitive_data(); */
199
200	/* send server hostkey, DH pubkey 'f' and singed H */
201	debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
202	packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
203	packet_put_string(server_host_key_blob, sbloblen);
204	packet_put_bignum2(dh->pub_key);	/* f */
205	packet_put_string(signature, slen);
206	packet_send();
207
208	xfree(signature);
209	xfree(server_host_key_blob);
210	/* have keys, free DH */
211	DH_free(dh);
212
213	kex_derive_keys(kex, hash, hashlen, shared_secret);
214	BN_clear_free(shared_secret);
215
216	kex_finish(kex);
217}
218