blob: a21a1d957689deed09b82d60dc4140ca28f884b7 [file] [log] [blame]
Damien Millera5103f42014-02-04 11:20:14 +11001/* $OpenBSD: kexgexc.c,v 1.17 2014/02/02 03:44:31 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
Damien Miller8e7fb332003-02-24 12:03:03 +110040#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "buffer.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110042#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100043#include "cipher.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110044#include "kex.h"
45#include "log.h"
46#include "packet.h"
47#include "dh.h"
48#include "ssh2.h"
49#include "compat.h"
50
51void
52kexgex_client(Kex *kex)
53{
54 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
55 BIGNUM *p = NULL, *g = NULL;
56 Key *server_host_key;
57 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
Damien Miller570c2ab2006-11-05 05:32:02 +110058 u_int klen, slen, sbloblen, hashlen;
59 int kout;
Damien Miller8e7fb332003-02-24 12:03:03 +110060 int min, max, nbits;
61 DH *dh;
62
Damien Miller76eea4a2014-01-26 09:37:25 +110063 nbits = dh_estimate(kex->dh_need * 8);
Damien Miller8e7fb332003-02-24 12:03:03 +110064
65 if (datafellows & SSH_OLD_DHGEX) {
Damien Miller8e7fb332003-02-24 12:03:03 +110066 /* Old GEX request */
67 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
68 packet_put_int(nbits);
69 min = DH_GRP_MIN;
70 max = DH_GRP_MAX;
Damien Miller8e7fb332003-02-24 12:03:03 +110071
Darren Tucker564f19e2003-12-09 19:18:07 +110072 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
73 } else {
Damien Miller8e7fb332003-02-24 12:03:03 +110074 /* New GEX request */
75 min = DH_GRP_MIN;
76 max = DH_GRP_MAX;
77 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
78 packet_put_int(min);
79 packet_put_int(nbits);
80 packet_put_int(max);
Darren Tucker564f19e2003-12-09 19:18:07 +110081
82 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
83 min, nbits, max);
Damien Miller8e7fb332003-02-24 12:03:03 +110084 }
85#ifdef DEBUG_KEXDH
86 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
87 min, nbits, max);
88#endif
89 packet_send();
90
91 debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
92 packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
93
94 if ((p = BN_new()) == NULL)
95 fatal("BN_new");
96 packet_get_bignum2(p);
97 if ((g = BN_new()) == NULL)
98 fatal("BN_new");
99 packet_get_bignum2(g);
100 packet_check_eom();
101
102 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
103 fatal("DH_GEX group out of range: %d !< %d !< %d",
104 min, BN_num_bits(p), max);
105
106 dh = dh_new_group(g, p);
107 dh_gen_key(dh, kex->we_need * 8);
108
109#ifdef DEBUG_KEXDH
110 DHparams_print_fp(stderr, dh);
111 fprintf(stderr, "pub= ");
112 BN_print_fp(stderr, dh->pub_key);
113 fprintf(stderr, "\n");
114#endif
115
116 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
117 /* generate and send 'e', client DH public key */
118 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
119 packet_put_bignum2(dh->pub_key);
120 packet_send();
121
122 debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
123 packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
124
125 /* key, cert */
126 server_host_key_blob = packet_get_string(&sbloblen);
127 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
128 if (server_host_key == NULL)
129 fatal("cannot decode server_host_key_blob");
130 if (server_host_key->type != kex->hostkey_type)
131 fatal("type mismatch for decoded server_host_key_blob");
132 if (kex->verify_host_key == NULL)
133 fatal("cannot verify server_host_key");
134 if (kex->verify_host_key(server_host_key) == -1)
135 fatal("server_host_key verification failed");
136
Damien Millerad6b14d2006-06-13 13:00:41 +1000137 /* DH parameter f, server public DH key */
Damien Miller8e7fb332003-02-24 12:03:03 +1100138 if ((dh_server_pub = BN_new()) == NULL)
139 fatal("dh_server_pub == NULL");
140 packet_get_bignum2(dh_server_pub);
141
142#ifdef DEBUG_KEXDH
143 fprintf(stderr, "dh_server_pub= ");
144 BN_print_fp(stderr, dh_server_pub);
145 fprintf(stderr, "\n");
146 debug("bits %d", BN_num_bits(dh_server_pub));
147#endif
148
149 /* signed H */
150 signature = packet_get_string(&slen);
151 packet_check_eom();
152
153 if (!dh_pub_is_valid(dh, dh_server_pub))
154 packet_disconnect("bad server public DH value");
155
156 klen = DH_size(dh);
157 kbuf = xmalloc(klen);
Damien Miller570c2ab2006-11-05 05:32:02 +1100158 if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0)
159 fatal("DH_compute_key: failed");
Damien Miller8e7fb332003-02-24 12:03:03 +1100160#ifdef DEBUG_KEXDH
161 dump_digest("shared secret", kbuf, kout);
162#endif
163 if ((shared_secret = BN_new()) == NULL)
164 fatal("kexgex_client: BN_new failed");
Darren Tucker0bc85572006-11-07 23:14:41 +1100165 if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
166 fatal("kexgex_client: BN_bin2bn failed");
Damien Millera5103f42014-02-04 11:20:14 +1100167 explicit_bzero(kbuf, klen);
Darren Tuckera627d422013-06-02 07:31:17 +1000168 free(kbuf);
Damien Miller8e7fb332003-02-24 12:03:03 +1100169
170 if (datafellows & SSH_OLD_DHGEX)
171 min = max = -1;
172
173 /* calc and verify H */
Damien Miller19bb3a52005-11-05 15:19:35 +1100174 kexgex_hash(
Damien Millerb3051d02014-01-10 10:58:53 +1100175 kex->hash_alg,
Damien Miller8e7fb332003-02-24 12:03:03 +1100176 kex->client_version_string,
177 kex->server_version_string,
178 buffer_ptr(&kex->my), buffer_len(&kex->my),
179 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
180 server_host_key_blob, sbloblen,
181 min, nbits, max,
182 dh->p, dh->g,
183 dh->pub_key,
184 dh_server_pub,
Damien Miller19bb3a52005-11-05 15:19:35 +1100185 shared_secret,
186 &hash, &hashlen
Damien Miller8e7fb332003-02-24 12:03:03 +1100187 );
Damien Miller19bb3a52005-11-05 15:19:35 +1100188
Damien Miller8e7fb332003-02-24 12:03:03 +1100189 /* have keys, free DH */
190 DH_free(dh);
Darren Tuckera627d422013-06-02 07:31:17 +1000191 free(server_host_key_blob);
Damien Miller8e7fb332003-02-24 12:03:03 +1100192 BN_clear_free(dh_server_pub);
193
Damien Miller19bb3a52005-11-05 15:19:35 +1100194 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
Damien Miller8e7fb332003-02-24 12:03:03 +1100195 fatal("key_verify failed for server_host_key");
196 key_free(server_host_key);
Darren Tuckera627d422013-06-02 07:31:17 +1000197 free(signature);
Damien Miller8e7fb332003-02-24 12:03:03 +1100198
199 /* save session id */
200 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100201 kex->session_id_len = hashlen;
Damien Miller8e7fb332003-02-24 12:03:03 +1100202 kex->session_id = xmalloc(kex->session_id_len);
203 memcpy(kex->session_id, hash, kex->session_id_len);
204 }
Damien Miller91b580e2014-01-12 19:21:22 +1100205 kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
Damien Miller8e7fb332003-02-24 12:03:03 +1100206 BN_clear_free(shared_secret);
207
208 kex_finish(kex);
209}
Damien Miller72ef7c12015-01-15 02:21:31 +1100210#endif /* WITH_OPENSSL */