xref: /openbsd-src/usr.bin/ssh/kexgexs.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /* $OpenBSD: kexgexs.c,v 1.19 2014/02/02 03:44:31 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 <sys/param.h>
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <signal.h>
32 
33 #include <openssl/dh.h>
34 
35 #include "xmalloc.h"
36 #include "buffer.h"
37 #include "key.h"
38 #include "cipher.h"
39 #include "kex.h"
40 #include "log.h"
41 #include "packet.h"
42 #include "dh.h"
43 #include "ssh2.h"
44 #include "compat.h"
45 #ifdef GSSAPI
46 #include "ssh-gss.h"
47 #endif
48 #include "monitor_wrap.h"
49 
50 void
51 kexgex_server(Kex *kex)
52 {
53 	BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
54 	Key *server_host_public, *server_host_private;
55 	DH *dh;
56 	u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
57 	u_int sbloblen, klen, slen, hashlen;
58 	int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1;
59 	int type, kout;
60 
61 	if (kex->load_host_public_key == NULL ||
62 	    kex->load_host_private_key == NULL)
63 		fatal("Cannot load hostkey");
64 	server_host_public = kex->load_host_public_key(kex->hostkey_type);
65 	if (server_host_public == NULL)
66 		fatal("Unsupported hostkey type %d", kex->hostkey_type);
67 	server_host_private = kex->load_host_private_key(kex->hostkey_type);
68 
69 	type = packet_read();
70 	switch (type) {
71 	case SSH2_MSG_KEX_DH_GEX_REQUEST:
72 		debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
73 		omin = min = packet_get_int();
74 		onbits = nbits = packet_get_int();
75 		omax = max = packet_get_int();
76 		min = MAX(DH_GRP_MIN, min);
77 		max = MIN(DH_GRP_MAX, max);
78 		nbits = MAX(DH_GRP_MIN, nbits);
79 		nbits = MIN(DH_GRP_MAX, nbits);
80 		break;
81 	case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
82 		debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
83 		onbits = nbits = packet_get_int();
84 		/* unused for old GEX */
85 		omin = min = DH_GRP_MIN;
86 		omax = max = DH_GRP_MAX;
87 		break;
88 	default:
89 		fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
90 	}
91 	packet_check_eom();
92 
93 	if (omax < omin || onbits < omin || omax < onbits)
94 		fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
95 		    omin, onbits, omax);
96 
97 	/* Contact privileged parent */
98 	dh = PRIVSEP(choose_dh(min, nbits, max));
99 	if (dh == NULL)
100 		packet_disconnect("Protocol error: no matching DH grp found");
101 
102 	debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
103 	packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
104 	packet_put_bignum2(dh->p);
105 	packet_put_bignum2(dh->g);
106 	packet_send();
107 
108 	/* flush */
109 	packet_write_wait();
110 
111 	/* Compute our exchange value in parallel with the client */
112 	dh_gen_key(dh, kex->we_need * 8);
113 
114 	debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
115 	packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
116 
117 	/* key, cert */
118 	if ((dh_client_pub = BN_new()) == NULL)
119 		fatal("dh_client_pub == NULL");
120 	packet_get_bignum2(dh_client_pub);
121 	packet_check_eom();
122 
123 #ifdef DEBUG_KEXDH
124 	fprintf(stderr, "dh_client_pub= ");
125 	BN_print_fp(stderr, dh_client_pub);
126 	fprintf(stderr, "\n");
127 	debug("bits %d", BN_num_bits(dh_client_pub));
128 #endif
129 
130 #ifdef DEBUG_KEXDH
131 	DHparams_print_fp(stderr, dh);
132 	fprintf(stderr, "pub= ");
133 	BN_print_fp(stderr, dh->pub_key);
134 	fprintf(stderr, "\n");
135 #endif
136 	if (!dh_pub_is_valid(dh, dh_client_pub))
137 		packet_disconnect("bad client public DH value");
138 
139 	klen = DH_size(dh);
140 	kbuf = xmalloc(klen);
141 	if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
142 		fatal("DH_compute_key: failed");
143 #ifdef DEBUG_KEXDH
144 	dump_digest("shared secret", kbuf, kout);
145 #endif
146 	if ((shared_secret = BN_new()) == NULL)
147 		fatal("kexgex_server: BN_new failed");
148 	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
149 		fatal("kexgex_server: BN_bin2bn failed");
150 	explicit_bzero(kbuf, klen);
151 	free(kbuf);
152 
153 	key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
154 
155 	if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
156 		omin = min = omax = max = -1;
157 
158 	/* calc H */
159 	kexgex_hash(
160 	    kex->hash_alg,
161 	    kex->client_version_string,
162 	    kex->server_version_string,
163 	    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
164 	    buffer_ptr(&kex->my), buffer_len(&kex->my),
165 	    server_host_key_blob, sbloblen,
166 	    omin, onbits, omax,
167 	    dh->p, dh->g,
168 	    dh_client_pub,
169 	    dh->pub_key,
170 	    shared_secret,
171 	    &hash, &hashlen
172 	);
173 	BN_clear_free(dh_client_pub);
174 
175 	/* save session id := H */
176 	if (kex->session_id == NULL) {
177 		kex->session_id_len = hashlen;
178 		kex->session_id = xmalloc(kex->session_id_len);
179 		memcpy(kex->session_id, hash, kex->session_id_len);
180 	}
181 
182 	/* sign H */
183 	kex->sign(server_host_private, server_host_public, &signature, &slen,
184 	    hash, hashlen);
185 
186 	/* destroy_sensitive_data(); */
187 
188 	/* send server hostkey, DH pubkey 'f' and singed H */
189 	debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
190 	packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
191 	packet_put_string(server_host_key_blob, sbloblen);
192 	packet_put_bignum2(dh->pub_key);	/* f */
193 	packet_put_string(signature, slen);
194 	packet_send();
195 
196 	free(signature);
197 	free(server_host_key_blob);
198 	/* have keys, free DH */
199 	DH_free(dh);
200 
201 	kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
202 	BN_clear_free(shared_secret);
203 
204 	kex_finish(kex);
205 }
206