xref: /openbsd-src/usr.bin/ssh/kexgexs.c (revision fb8aa7497fded39583f40e800732f9c046411717)
1 /* $OpenBSD: kexgexs.c,v 1.29 2016/06/08 02:13:01 dtucker 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>	/* MIN MAX */
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <signal.h>
32 
33 #include <openssl/dh.h>
34 
35 #include "sshkey.h"
36 #include "cipher.h"
37 #include "digest.h"
38 #include "kex.h"
39 #include "log.h"
40 #include "packet.h"
41 #include "dh.h"
42 #include "ssh2.h"
43 #include "compat.h"
44 #ifdef GSSAPI
45 #include "ssh-gss.h"
46 #endif
47 #include "monitor_wrap.h"
48 #include "dispatch.h"
49 #include "ssherr.h"
50 #include "sshbuf.h"
51 
52 static int input_kex_dh_gex_request(int, u_int32_t, void *);
53 static int input_kex_dh_gex_init(int, u_int32_t, void *);
54 
55 int
56 kexgex_server(struct ssh *ssh)
57 {
58 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
59 	    &input_kex_dh_gex_request);
60 	debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
61 	return 0;
62 }
63 
64 static int
65 input_kex_dh_gex_request(int type, u_int32_t seq, void *ctxt)
66 {
67 	struct ssh *ssh = ctxt;
68 	struct kex *kex = ssh->kex;
69 	int r;
70 	u_int min = 0, max = 0, nbits = 0;
71 
72 	debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
73 	if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
74 	    (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
75 	    (r = sshpkt_get_u32(ssh, &max)) != 0 ||
76 	    (r = sshpkt_get_end(ssh)) != 0)
77 		goto out;
78 	kex->nbits = nbits;
79 	kex->min = min;
80 	kex->max = max;
81 	min = MAX(DH_GRP_MIN, min);
82 	max = MIN(DH_GRP_MAX, max);
83 	nbits = MAX(DH_GRP_MIN, nbits);
84 	nbits = MIN(DH_GRP_MAX, nbits);
85 
86 	if (kex->max < kex->min || kex->nbits < kex->min ||
87 	    kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
88 		r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
89 		goto out;
90 	}
91 
92 	/* Contact privileged parent */
93 	kex->dh = PRIVSEP(choose_dh(min, nbits, max));
94 	if (kex->dh == NULL) {
95 		sshpkt_disconnect(ssh, "no matching DH grp found");
96 		r = SSH_ERR_ALLOC_FAIL;
97 		goto out;
98 	}
99 	debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
100 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
101 	    (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
102 	    (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
103 	    (r = sshpkt_send(ssh)) != 0)
104 		goto out;
105 
106 	/* Compute our exchange value in parallel with the client */
107 	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
108 		goto out;
109 
110 	debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
111 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
112 	r = 0;
113  out:
114 	return r;
115 }
116 
117 static int
118 input_kex_dh_gex_init(int type, u_int32_t seq, void *ctxt)
119 {
120 	struct ssh *ssh = ctxt;
121 	struct kex *kex = ssh->kex;
122 	BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
123 	struct sshkey *server_host_public, *server_host_private;
124 	u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
125 	u_char hash[SSH_DIGEST_MAX_LENGTH];
126 	size_t sbloblen, slen;
127 	size_t klen = 0, hashlen;
128 	int kout, r;
129 
130 	if (kex->load_host_public_key == NULL ||
131 	    kex->load_host_private_key == NULL) {
132 		r = SSH_ERR_INVALID_ARGUMENT;
133 		goto out;
134 	}
135 	server_host_public = kex->load_host_public_key(kex->hostkey_type,
136 	    kex->hostkey_nid, ssh);
137 	server_host_private = kex->load_host_private_key(kex->hostkey_type,
138 	    kex->hostkey_nid, ssh);
139 	if (server_host_public == NULL) {
140 		r = SSH_ERR_NO_HOSTKEY_LOADED;
141 		goto out;
142 	}
143 
144 	/* key, cert */
145 	if ((dh_client_pub = BN_new()) == NULL) {
146 		r = SSH_ERR_ALLOC_FAIL;
147 		goto out;
148 	}
149 	if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
150 	    (r = sshpkt_get_end(ssh)) != 0)
151 		goto out;
152 
153 #ifdef DEBUG_KEXDH
154 	fprintf(stderr, "dh_client_pub= ");
155 	BN_print_fp(stderr, dh_client_pub);
156 	fprintf(stderr, "\n");
157 	debug("bits %d", BN_num_bits(dh_client_pub));
158 #endif
159 
160 #ifdef DEBUG_KEXDH
161 	DHparams_print_fp(stderr, kex->dh);
162 	fprintf(stderr, "pub= ");
163 	BN_print_fp(stderr, kex->dh->pub_key);
164 	fprintf(stderr, "\n");
165 #endif
166 	if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
167 		sshpkt_disconnect(ssh, "bad client public DH value");
168 		r = SSH_ERR_MESSAGE_INCOMPLETE;
169 		goto out;
170 	}
171 
172 	klen = DH_size(kex->dh);
173 	if ((kbuf = malloc(klen)) == NULL ||
174 	    (shared_secret = BN_new()) == NULL) {
175 		r = SSH_ERR_ALLOC_FAIL;
176 		goto out;
177 	}
178 	if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
179 	    BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
180 		r = SSH_ERR_LIBCRYPTO_ERROR;
181 		goto out;
182 	}
183 #ifdef DEBUG_KEXDH
184 	dump_digest("shared secret", kbuf, kout);
185 #endif
186 	if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
187 	    &sbloblen)) != 0)
188 		goto out;
189 	/* calc H */
190 	hashlen = sizeof(hash);
191 	if ((r = kexgex_hash(
192 	    kex->hash_alg,
193 	    kex->client_version_string,
194 	    kex->server_version_string,
195 	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
196 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
197 	    server_host_key_blob, sbloblen,
198 	    kex->min, kex->nbits, kex->max,
199 	    kex->dh->p, kex->dh->g,
200 	    dh_client_pub,
201 	    kex->dh->pub_key,
202 	    shared_secret,
203 	    hash, &hashlen)) != 0)
204 		goto out;
205 
206 	/* save session id := H */
207 	if (kex->session_id == NULL) {
208 		kex->session_id_len = hashlen;
209 		kex->session_id = malloc(kex->session_id_len);
210 		if (kex->session_id == NULL) {
211 			r = SSH_ERR_ALLOC_FAIL;
212 			goto out;
213 		}
214 		memcpy(kex->session_id, hash, kex->session_id_len);
215 	}
216 
217 	/* sign H */
218 	if ((r = kex->sign(server_host_private, server_host_public, &signature,
219 	     &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
220 		goto out;
221 
222 	/* destroy_sensitive_data(); */
223 
224 	/* send server hostkey, DH pubkey 'f' and singed H */
225 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
226 	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
227 	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||     /* f */
228 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
229 	    (r = sshpkt_send(ssh)) != 0)
230 		goto out;
231 
232 	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
233 		r = kex_send_newkeys(ssh);
234  out:
235 	DH_free(kex->dh);
236 	kex->dh = NULL;
237 	if (dh_client_pub)
238 		BN_clear_free(dh_client_pub);
239 	if (kbuf) {
240 		explicit_bzero(kbuf, klen);
241 		free(kbuf);
242 	}
243 	if (shared_secret)
244 		BN_clear_free(shared_secret);
245 	free(server_host_key_blob);
246 	free(signature);
247 	return r;
248 }
249