blob: adb973d5b40cb9656d2a7a3877f74ed066e1f7a1 [file] [log] [blame]
Darren Tucker0bc85572006-11-07 23:14:41 +11001/* $OpenBSD: kexgexc.c,v 1.11 2006/11/06 21:25:28 markus 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 Millerd7834352006-08-05 12:39:39 +100029#include <sys/types.h>
30
Damien Millerded319c2006-09-01 15:38:36 +100031#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100033#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100035
Damien Miller8e7fb332003-02-24 12:03:03 +110036#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100037#include "buffer.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110038#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100039#include "cipher.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110040#include "kex.h"
41#include "log.h"
42#include "packet.h"
43#include "dh.h"
44#include "ssh2.h"
45#include "compat.h"
46
47void
48kexgex_client(Kex *kex)
49{
50 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
51 BIGNUM *p = NULL, *g = NULL;
52 Key *server_host_key;
53 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
Damien Miller570c2ab2006-11-05 05:32:02 +110054 u_int klen, slen, sbloblen, hashlen;
55 int kout;
Damien Miller8e7fb332003-02-24 12:03:03 +110056 int min, max, nbits;
57 DH *dh;
58
59 nbits = dh_estimate(kex->we_need * 8);
60
61 if (datafellows & SSH_OLD_DHGEX) {
Damien Miller8e7fb332003-02-24 12:03:03 +110062 /* Old GEX request */
63 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
64 packet_put_int(nbits);
65 min = DH_GRP_MIN;
66 max = DH_GRP_MAX;
Damien Miller8e7fb332003-02-24 12:03:03 +110067
Darren Tucker564f19e2003-12-09 19:18:07 +110068 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
69 } else {
Damien Miller8e7fb332003-02-24 12:03:03 +110070 /* New GEX request */
71 min = DH_GRP_MIN;
72 max = DH_GRP_MAX;
73 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
74 packet_put_int(min);
75 packet_put_int(nbits);
76 packet_put_int(max);
Darren Tucker564f19e2003-12-09 19:18:07 +110077
78 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
79 min, nbits, max);
Damien Miller8e7fb332003-02-24 12:03:03 +110080 }
81#ifdef DEBUG_KEXDH
82 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
83 min, nbits, max);
84#endif
85 packet_send();
86
87 debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
88 packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
89
90 if ((p = BN_new()) == NULL)
91 fatal("BN_new");
92 packet_get_bignum2(p);
93 if ((g = BN_new()) == NULL)
94 fatal("BN_new");
95 packet_get_bignum2(g);
96 packet_check_eom();
97
98 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
99 fatal("DH_GEX group out of range: %d !< %d !< %d",
100 min, BN_num_bits(p), max);
101
102 dh = dh_new_group(g, p);
103 dh_gen_key(dh, kex->we_need * 8);
104
105#ifdef DEBUG_KEXDH
106 DHparams_print_fp(stderr, dh);
107 fprintf(stderr, "pub= ");
108 BN_print_fp(stderr, dh->pub_key);
109 fprintf(stderr, "\n");
110#endif
111
112 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
113 /* generate and send 'e', client DH public key */
114 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
115 packet_put_bignum2(dh->pub_key);
116 packet_send();
117
118 debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
119 packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
120
121 /* key, cert */
122 server_host_key_blob = packet_get_string(&sbloblen);
123 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
124 if (server_host_key == NULL)
125 fatal("cannot decode server_host_key_blob");
126 if (server_host_key->type != kex->hostkey_type)
127 fatal("type mismatch for decoded server_host_key_blob");
128 if (kex->verify_host_key == NULL)
129 fatal("cannot verify server_host_key");
130 if (kex->verify_host_key(server_host_key) == -1)
131 fatal("server_host_key verification failed");
132
Damien Millerad6b14d2006-06-13 13:00:41 +1000133 /* DH parameter f, server public DH key */
Damien Miller8e7fb332003-02-24 12:03:03 +1100134 if ((dh_server_pub = BN_new()) == NULL)
135 fatal("dh_server_pub == NULL");
136 packet_get_bignum2(dh_server_pub);
137
138#ifdef DEBUG_KEXDH
139 fprintf(stderr, "dh_server_pub= ");
140 BN_print_fp(stderr, dh_server_pub);
141 fprintf(stderr, "\n");
142 debug("bits %d", BN_num_bits(dh_server_pub));
143#endif
144
145 /* signed H */
146 signature = packet_get_string(&slen);
147 packet_check_eom();
148
149 if (!dh_pub_is_valid(dh, dh_server_pub))
150 packet_disconnect("bad server public DH value");
151
152 klen = DH_size(dh);
153 kbuf = xmalloc(klen);
Damien Miller570c2ab2006-11-05 05:32:02 +1100154 if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0)
155 fatal("DH_compute_key: failed");
Damien Miller8e7fb332003-02-24 12:03:03 +1100156#ifdef DEBUG_KEXDH
157 dump_digest("shared secret", kbuf, kout);
158#endif
159 if ((shared_secret = BN_new()) == NULL)
160 fatal("kexgex_client: BN_new failed");
Darren Tucker0bc85572006-11-07 23:14:41 +1100161 if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
162 fatal("kexgex_client: BN_bin2bn failed");
Damien Miller8e7fb332003-02-24 12:03:03 +1100163 memset(kbuf, 0, klen);
164 xfree(kbuf);
165
166 if (datafellows & SSH_OLD_DHGEX)
167 min = max = -1;
168
169 /* calc and verify H */
Damien Miller19bb3a52005-11-05 15:19:35 +1100170 kexgex_hash(
171 kex->evp_md,
Damien Miller8e7fb332003-02-24 12:03:03 +1100172 kex->client_version_string,
173 kex->server_version_string,
174 buffer_ptr(&kex->my), buffer_len(&kex->my),
175 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
176 server_host_key_blob, sbloblen,
177 min, nbits, max,
178 dh->p, dh->g,
179 dh->pub_key,
180 dh_server_pub,
Damien Miller19bb3a52005-11-05 15:19:35 +1100181 shared_secret,
182 &hash, &hashlen
Damien Miller8e7fb332003-02-24 12:03:03 +1100183 );
Damien Miller19bb3a52005-11-05 15:19:35 +1100184
Damien Miller8e7fb332003-02-24 12:03:03 +1100185 /* have keys, free DH */
186 DH_free(dh);
187 xfree(server_host_key_blob);
188 BN_clear_free(dh_server_pub);
189
Damien Miller19bb3a52005-11-05 15:19:35 +1100190 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
Damien Miller8e7fb332003-02-24 12:03:03 +1100191 fatal("key_verify failed for server_host_key");
192 key_free(server_host_key);
193 xfree(signature);
194
195 /* save session id */
196 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100197 kex->session_id_len = hashlen;
Damien Miller8e7fb332003-02-24 12:03:03 +1100198 kex->session_id = xmalloc(kex->session_id_len);
199 memcpy(kex->session_id, hash, kex->session_id_len);
200 }
Damien Miller19bb3a52005-11-05 15:19:35 +1100201 kex_derive_keys(kex, hash, hashlen, shared_secret);
Damien Miller8e7fb332003-02-24 12:03:03 +1100202 BN_clear_free(shared_secret);
203
204 kex_finish(kex);
205}