2 * Copyright (c) 2000 Niels Provos. All rights reserved.
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 RCSID("$OpenBSD: kexgexc.c,v 1.2 2003/12/08 11:00:47 markus Exp $");
39 kexgex_client(Kex *kex)
41 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
42 BIGNUM *p = NULL, *g = NULL;
44 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
45 u_int klen, kout, slen, sbloblen;
49 nbits = dh_estimate(kex->we_need * 8);
51 if (datafellows & SSH_OLD_DHGEX) {
53 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
54 packet_put_int(nbits);
58 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
63 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
65 packet_put_int(nbits);
68 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
72 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
77 debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
78 packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
80 if ((p = BN_new()) == NULL)
82 packet_get_bignum2(p);
83 if ((g = BN_new()) == NULL)
85 packet_get_bignum2(g);
88 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
89 fatal("DH_GEX group out of range: %d !< %d !< %d",
90 min, BN_num_bits(p), max);
92 dh = dh_new_group(g, p);
93 dh_gen_key(dh, kex->we_need * 8);
96 DHparams_print_fp(stderr, dh);
97 fprintf(stderr, "pub= ");
98 BN_print_fp(stderr, dh->pub_key);
99 fprintf(stderr, "\n");
102 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
103 /* generate and send 'e', client DH public key */
104 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
105 packet_put_bignum2(dh->pub_key);
108 debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
109 packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
112 server_host_key_blob = packet_get_string(&sbloblen);
113 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
114 if (server_host_key == NULL)
115 fatal("cannot decode server_host_key_blob");
116 if (server_host_key->type != kex->hostkey_type)
117 fatal("type mismatch for decoded server_host_key_blob");
118 if (kex->verify_host_key == NULL)
119 fatal("cannot verify server_host_key");
120 if (kex->verify_host_key(server_host_key) == -1)
121 fatal("server_host_key verification failed");
123 /* DH paramter f, server public DH key */
124 if ((dh_server_pub = BN_new()) == NULL)
125 fatal("dh_server_pub == NULL");
126 packet_get_bignum2(dh_server_pub);
129 fprintf(stderr, "dh_server_pub= ");
130 BN_print_fp(stderr, dh_server_pub);
131 fprintf(stderr, "\n");
132 debug("bits %d", BN_num_bits(dh_server_pub));
136 signature = packet_get_string(&slen);
139 if (!dh_pub_is_valid(dh, dh_server_pub))
140 packet_disconnect("bad server public DH value");
143 kbuf = xmalloc(klen);
144 kout = DH_compute_key(kbuf, dh_server_pub, dh);
146 dump_digest("shared secret", kbuf, kout);
148 if ((shared_secret = BN_new()) == NULL)
149 fatal("kexgex_client: BN_new failed");
150 BN_bin2bn(kbuf, kout, shared_secret);
151 memset(kbuf, 0, klen);
154 if (datafellows & SSH_OLD_DHGEX)
157 /* calc and verify H */
159 kex->client_version_string,
160 kex->server_version_string,
161 buffer_ptr(&kex->my), buffer_len(&kex->my),
162 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
163 server_host_key_blob, sbloblen,
170 /* have keys, free DH */
172 xfree(server_host_key_blob);
173 BN_clear_free(dh_server_pub);
175 if (key_verify(server_host_key, signature, slen, hash, 20) != 1)
176 fatal("key_verify failed for server_host_key");
177 key_free(server_host_key);
180 /* save session id */
181 if (kex->session_id == NULL) {
182 kex->session_id_len = 20;
183 kex->session_id = xmalloc(kex->session_id_len);
184 memcpy(kex->session_id, hash, kex->session_id_len);
186 kex_derive_keys(kex, hash, shared_secret);
187 BN_clear_free(shared_secret);