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.
38 kexgex_client(Kex *kex)
40 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
41 BIGNUM *p = NULL, *g = NULL;
43 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
44 u_int klen, kout, slen, sbloblen, hashlen;
48 nbits = dh_estimate(kex->we_need * 8);
50 if (datafellows & SSH_OLD_DHGEX) {
52 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
53 packet_put_int(nbits);
57 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
62 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
64 packet_put_int(nbits);
67 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
71 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
76 debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
77 packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
79 if ((p = BN_new()) == NULL)
81 packet_get_bignum2(p);
82 if ((g = BN_new()) == NULL)
84 packet_get_bignum2(g);
87 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
88 fatal("DH_GEX group out of range: %d !< %d !< %d",
89 min, BN_num_bits(p), max);
91 dh = dh_new_group(g, p);
92 dh_gen_key(dh, kex->we_need * 8);
95 DHparams_print_fp(stderr, dh);
96 fprintf(stderr, "pub= ");
97 BN_print_fp(stderr, dh->pub_key);
98 fprintf(stderr, "\n");
101 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
102 /* generate and send 'e', client DH public key */
103 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
104 packet_put_bignum2(dh->pub_key);
107 debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
108 packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
111 server_host_key_blob = packet_get_string(&sbloblen);
112 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
113 if (server_host_key == NULL)
114 fatal("cannot decode server_host_key_blob");
115 if (server_host_key->type != kex->hostkey_type)
116 fatal("type mismatch for decoded server_host_key_blob");
117 if (kex->verify_host_key == NULL)
118 fatal("cannot verify server_host_key");
119 if (kex->verify_host_key(server_host_key) == -1)
120 fatal("server_host_key verification failed");
122 /* DH paramter f, server public DH key */
123 if ((dh_server_pub = BN_new()) == NULL)
124 fatal("dh_server_pub == NULL");
125 packet_get_bignum2(dh_server_pub);
128 fprintf(stderr, "dh_server_pub= ");
129 BN_print_fp(stderr, dh_server_pub);
130 fprintf(stderr, "\n");
131 debug("bits %d", BN_num_bits(dh_server_pub));
135 signature = packet_get_string(&slen);
138 if (!dh_pub_is_valid(dh, dh_server_pub))
139 packet_disconnect("bad server public DH value");
142 kbuf = xmalloc(klen);
143 kout = DH_compute_key(kbuf, dh_server_pub, dh);
145 dump_digest("shared secret", kbuf, kout);
147 if ((shared_secret = BN_new()) == NULL)
148 fatal("kexgex_client: BN_new failed");
149 BN_bin2bn(kbuf, kout, shared_secret);
150 memset(kbuf, 0, klen);
153 if (datafellows & SSH_OLD_DHGEX)
156 /* 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,
172 /* have keys, free DH */
174 xfree(server_host_key_blob);
175 BN_clear_free(dh_server_pub);
177 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
178 fatal("key_verify failed for server_host_key");
179 key_free(server_host_key);
182 /* save session id */
183 if (kex->session_id == NULL) {
184 kex->session_id_len = hashlen;
185 kex->session_id = xmalloc(kex->session_id_len);
186 memcpy(kex->session_id, hash, kex->session_id_len);
188 kex_derive_keys(kex, hash, hashlen, shared_secret);
189 BN_clear_free(shared_secret);