blob: 3f20491c4374dbe6e32e5a344a18ff55da406de2 [file] [log] [blame]
djm@openbsd.org318be282015-04-13 02:04:08 +00001/* $OpenBSD: kexgexc.c,v 1.21 2015/04/13 02:04:08 djm Exp $ */
Damien Miller8e7fb332003-02-24 12:03:03 +11002/*
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"
Damien Miller8e7fb332003-02-24 12:03:03 +110028
Damien Miller72ef7c12015-01-15 02:21:31 +110029#ifdef WITH_OPENSSL
30
Damien Millerd7834352006-08-05 12:39:39 +100031#include <sys/types.h>
32
Damien Miller4499f4c2010-11-20 15:15:49 +110033#include <openssl/dh.h>
34
Damien Millerded319c2006-09-01 15:38:36 +100035#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100036#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100038#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000040#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000042#include "digest.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110043#include "kex.h"
44#include "log.h"
45#include "packet.h"
46#include "dh.h"
47#include "ssh2.h"
48#include "compat.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000049#include "dispatch.h"
50#include "ssherr.h"
51#include "sshbuf.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110052
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000053static int input_kex_dh_gex_group(int, u_int32_t, void *);
54static int input_kex_dh_gex_reply(int, u_int32_t, void *);
55
56int
57kexgex_client(struct ssh *ssh)
Damien Miller8e7fb332003-02-24 12:03:03 +110058{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000059 struct kex *kex = ssh->kex;
60 int r;
61 u_int nbits;
Damien Miller8e7fb332003-02-24 12:03:03 +110062
Damien Miller76eea4a2014-01-26 09:37:25 +110063 nbits = dh_estimate(kex->dh_need * 8);
Damien Miller8e7fb332003-02-24 12:03:03 +110064
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000065 kex->min = DH_GRP_MIN;
66 kex->max = DH_GRP_MAX;
67 kex->nbits = nbits;
djm@openbsd.org318be282015-04-13 02:04:08 +000068 /* New GEX request */
69 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
70 (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
71 (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
72 (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
73 (r = sshpkt_send(ssh)) != 0)
74 goto out;
75 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
76 kex->min, kex->nbits, kex->max);
Damien Miller8e7fb332003-02-24 12:03:03 +110077#ifdef DEBUG_KEXDH
78 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000079 kex->min, kex->nbits, kex->max);
Damien Miller8e7fb332003-02-24 12:03:03 +110080#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000081 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
82 &input_kex_dh_gex_group);
83 r = 0;
84 out:
85 return r;
86}
Damien Miller8e7fb332003-02-24 12:03:03 +110087
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000088static int
89input_kex_dh_gex_group(int type, u_int32_t seq, void *ctxt)
90{
91 struct ssh *ssh = ctxt;
92 struct kex *kex = ssh->kex;
93 BIGNUM *p = NULL, *g = NULL;
94 int r, bits;
Damien Miller8e7fb332003-02-24 12:03:03 +110095
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000096 debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
Damien Miller8e7fb332003-02-24 12:03:03 +110097
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000098 if ((p = BN_new()) == NULL ||
99 (g = BN_new()) == NULL) {
100 r = SSH_ERR_ALLOC_FAIL;
101 goto out;
102 }
103 if ((r = sshpkt_get_bignum2(ssh, p)) != 0 ||
104 (r = sshpkt_get_bignum2(ssh, g)) != 0 ||
105 (r = sshpkt_get_end(ssh)) != 0)
106 goto out;
107 if ((bits = BN_num_bits(p)) < 0 ||
108 (u_int)bits < kex->min || (u_int)bits > kex->max) {
109 r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
110 goto out;
111 }
112 if ((kex->dh = dh_new_group(g, p)) == NULL) {
113 r = SSH_ERR_ALLOC_FAIL;
114 goto out;
115 }
116 p = g = NULL; /* belong to kex->dh now */
Damien Miller8e7fb332003-02-24 12:03:03 +1100117
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000118 /* generate and send 'e', client DH public key */
119 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
120 (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
121 (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
122 (r = sshpkt_send(ssh)) != 0)
123 goto out;
124 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
Damien Miller8e7fb332003-02-24 12:03:03 +1100125#ifdef DEBUG_KEXDH
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000126 DHparams_print_fp(stderr, kex->dh);
Damien Miller8e7fb332003-02-24 12:03:03 +1100127 fprintf(stderr, "pub= ");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000128 BN_print_fp(stderr, kex->dh->pub_key);
Damien Miller8e7fb332003-02-24 12:03:03 +1100129 fprintf(stderr, "\n");
130#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000131 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
132 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
133 r = 0;
134out:
135 if (p)
136 BN_clear_free(p);
137 if (g)
138 BN_clear_free(g);
139 return r;
140}
Damien Miller8e7fb332003-02-24 12:03:03 +1100141
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000142static int
143input_kex_dh_gex_reply(int type, u_int32_t seq, void *ctxt)
144{
145 struct ssh *ssh = ctxt;
146 struct kex *kex = ssh->kex;
147 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
148 struct sshkey *server_host_key = NULL;
149 u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
150 u_char hash[SSH_DIGEST_MAX_LENGTH];
151 size_t klen = 0, slen, sbloblen, hashlen;
152 int kout, r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100153
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000154 debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
155 if (kex->verify_host_key == NULL) {
156 r = SSH_ERR_INVALID_ARGUMENT;
157 goto out;
158 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100159 /* key, cert */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000160 if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
161 &sbloblen)) != 0 ||
162 (r = sshkey_from_blob(server_host_key_blob, sbloblen,
163 &server_host_key)) != 0)
164 goto out;
165 if (server_host_key->type != kex->hostkey_type) {
166 r = SSH_ERR_KEY_TYPE_MISMATCH;
167 goto out;
168 }
djm@openbsd.org5104db72015-01-26 06:10:03 +0000169 if (server_host_key->type != kex->hostkey_type ||
170 (kex->hostkey_type == KEY_ECDSA &&
171 server_host_key->ecdsa_nid != kex->hostkey_nid)) {
172 r = SSH_ERR_KEY_TYPE_MISMATCH;
173 goto out;
174 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000175 if (kex->verify_host_key(server_host_key, ssh) == -1) {
176 r = SSH_ERR_SIGNATURE_INVALID;
177 goto out;
178 }
Damien Millerad6b14d2006-06-13 13:00:41 +1000179 /* DH parameter f, server public DH key */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000180 if ((dh_server_pub = BN_new()) == NULL) {
181 r = SSH_ERR_ALLOC_FAIL;
182 goto out;
183 }
184 /* signed H */
185 if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
186 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
187 (r = sshpkt_get_end(ssh)) != 0)
188 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100189#ifdef DEBUG_KEXDH
190 fprintf(stderr, "dh_server_pub= ");
191 BN_print_fp(stderr, dh_server_pub);
192 fprintf(stderr, "\n");
193 debug("bits %d", BN_num_bits(dh_server_pub));
194#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000195 if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
196 sshpkt_disconnect(ssh, "bad server public DH value");
197 r = SSH_ERR_MESSAGE_INCOMPLETE;
198 goto out;
199 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100200
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000201 klen = DH_size(kex->dh);
202 if ((kbuf = malloc(klen)) == NULL ||
203 (shared_secret = BN_new()) == NULL) {
204 r = SSH_ERR_ALLOC_FAIL;
205 goto out;
206 }
207 if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
208 BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
209 r = SSH_ERR_LIBCRYPTO_ERROR;
210 goto out;
211 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100212#ifdef DEBUG_KEXDH
213 dump_digest("shared secret", kbuf, kout);
214#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000215 if (ssh->compat & SSH_OLD_DHGEX)
216 kex->min = kex->max = -1;
Damien Miller8e7fb332003-02-24 12:03:03 +1100217
218 /* calc and verify H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000219 hashlen = sizeof(hash);
220 if ((r = kexgex_hash(
Damien Millerb3051d02014-01-10 10:58:53 +1100221 kex->hash_alg,
Damien Miller8e7fb332003-02-24 12:03:03 +1100222 kex->client_version_string,
223 kex->server_version_string,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000224 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
225 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
Damien Miller8e7fb332003-02-24 12:03:03 +1100226 server_host_key_blob, sbloblen,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000227 kex->min, kex->nbits, kex->max,
228 kex->dh->p, kex->dh->g,
229 kex->dh->pub_key,
Damien Miller8e7fb332003-02-24 12:03:03 +1100230 dh_server_pub,
Damien Miller19bb3a52005-11-05 15:19:35 +1100231 shared_secret,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000232 hash, &hashlen)) != 0)
233 goto out;
Damien Miller19bb3a52005-11-05 15:19:35 +1100234
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000235 if ((r = sshkey_verify(server_host_key, signature, slen, hash,
236 hashlen, ssh->compat)) != 0)
237 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100238
239 /* save session id */
240 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100241 kex->session_id_len = hashlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000242 kex->session_id = malloc(kex->session_id_len);
243 if (kex->session_id == NULL) {
244 r = SSH_ERR_ALLOC_FAIL;
245 goto out;
246 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100247 memcpy(kex->session_id, hash, kex->session_id_len);
248 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100249
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000250 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
251 r = kex_send_newkeys(ssh);
252 out:
253 explicit_bzero(hash, sizeof(hash));
254 DH_free(kex->dh);
255 kex->dh = NULL;
256 if (dh_server_pub)
257 BN_clear_free(dh_server_pub);
258 if (kbuf) {
259 explicit_bzero(kbuf, klen);
260 free(kbuf);
261 }
262 if (shared_secret)
263 BN_clear_free(shared_secret);
264 sshkey_free(server_host_key);
265 free(server_host_key_blob);
266 free(signature);
267 return r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100268}
Damien Miller72ef7c12015-01-15 02:21:31 +1100269#endif /* WITH_OPENSSL */