1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000 Niels Provos. All rights reserved.
3*7c478bd9Sstevel@tonic-gate * Copyright (c) 2001 Markus Friedl. All rights reserved.
4*7c478bd9Sstevel@tonic-gate *
5*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
6*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
7*7c478bd9Sstevel@tonic-gate * are met:
8*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
9*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
10*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
11*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
12*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*7c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*7c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*7c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*7c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*7c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*7c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*7c478bd9Sstevel@tonic-gate */
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate #include "includes.h"
27*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: kexgex.c,v 1.22 2002/03/24 17:27:03 stevesk Exp $");
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #include <openssl/bn.h>
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate #include "xmalloc.h"
34*7c478bd9Sstevel@tonic-gate #include "buffer.h"
35*7c478bd9Sstevel@tonic-gate #include "bufaux.h"
36*7c478bd9Sstevel@tonic-gate #include "key.h"
37*7c478bd9Sstevel@tonic-gate #include "kex.h"
38*7c478bd9Sstevel@tonic-gate #include "log.h"
39*7c478bd9Sstevel@tonic-gate #include "packet.h"
40*7c478bd9Sstevel@tonic-gate #include "dh.h"
41*7c478bd9Sstevel@tonic-gate #include "ssh2.h"
42*7c478bd9Sstevel@tonic-gate #include "compat.h"
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate void
kexgex_client(Kex * kex)45*7c478bd9Sstevel@tonic-gate kexgex_client(Kex *kex)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
48*7c478bd9Sstevel@tonic-gate BIGNUM *p = NULL, *g = NULL;
49*7c478bd9Sstevel@tonic-gate Key *server_host_key;
50*7c478bd9Sstevel@tonic-gate u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
51*7c478bd9Sstevel@tonic-gate u_int klen, kout, slen, sbloblen;
52*7c478bd9Sstevel@tonic-gate int min, max, nbits;
53*7c478bd9Sstevel@tonic-gate DH *dh;
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate nbits = dh_estimate(kex->we_need * 8);
56*7c478bd9Sstevel@tonic-gate
57*7c478bd9Sstevel@tonic-gate if (datafellows & SSH_OLD_DHGEX) {
58*7c478bd9Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD sent");
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate /* Old GEX request */
61*7c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
62*7c478bd9Sstevel@tonic-gate packet_put_int(nbits);
63*7c478bd9Sstevel@tonic-gate min = DH_GRP_MIN;
64*7c478bd9Sstevel@tonic-gate max = DH_GRP_MAX;
65*7c478bd9Sstevel@tonic-gate } else {
66*7c478bd9Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REQUEST sent");
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate /* New GEX request */
69*7c478bd9Sstevel@tonic-gate min = DH_GRP_MIN;
70*7c478bd9Sstevel@tonic-gate max = DH_GRP_MAX;
71*7c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
72*7c478bd9Sstevel@tonic-gate packet_put_int(min);
73*7c478bd9Sstevel@tonic-gate packet_put_int(nbits);
74*7c478bd9Sstevel@tonic-gate packet_put_int(max);
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
77*7c478bd9Sstevel@tonic-gate fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
78*7c478bd9Sstevel@tonic-gate min, nbits, max);
79*7c478bd9Sstevel@tonic-gate #endif
80*7c478bd9Sstevel@tonic-gate packet_send();
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
83*7c478bd9Sstevel@tonic-gate packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
84*7c478bd9Sstevel@tonic-gate
85*7c478bd9Sstevel@tonic-gate if ((p = BN_new()) == NULL)
86*7c478bd9Sstevel@tonic-gate fatal("BN_new");
87*7c478bd9Sstevel@tonic-gate packet_get_bignum2(p);
88*7c478bd9Sstevel@tonic-gate if ((g = BN_new()) == NULL)
89*7c478bd9Sstevel@tonic-gate fatal("BN_new");
90*7c478bd9Sstevel@tonic-gate packet_get_bignum2(g);
91*7c478bd9Sstevel@tonic-gate packet_check_eom();
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate if (BN_num_bits(p) < min || BN_num_bits(p) > max)
94*7c478bd9Sstevel@tonic-gate fatal("DH_GEX group out of range: %d !< %d !< %d",
95*7c478bd9Sstevel@tonic-gate min, BN_num_bits(p), max);
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate dh = dh_new_group(g, p);
98*7c478bd9Sstevel@tonic-gate dh_gen_key(dh, kex->we_need * 8);
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
101*7c478bd9Sstevel@tonic-gate DHparams_print_fp(stderr, dh);
102*7c478bd9Sstevel@tonic-gate fprintf(stderr, "pub= ");
103*7c478bd9Sstevel@tonic-gate BN_print_fp(stderr, dh->pub_key);
104*7c478bd9Sstevel@tonic-gate fprintf(stderr, "\n");
105*7c478bd9Sstevel@tonic-gate #endif
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
108*7c478bd9Sstevel@tonic-gate /* generate and send 'e', client DH public key */
109*7c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
110*7c478bd9Sstevel@tonic-gate packet_put_bignum2(dh->pub_key);
111*7c478bd9Sstevel@tonic-gate packet_send();
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
114*7c478bd9Sstevel@tonic-gate packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate /* key, cert */
117*7c478bd9Sstevel@tonic-gate server_host_key_blob = packet_get_string(&sbloblen);
118*7c478bd9Sstevel@tonic-gate server_host_key = key_from_blob(server_host_key_blob, sbloblen);
119*7c478bd9Sstevel@tonic-gate if (server_host_key == NULL)
120*7c478bd9Sstevel@tonic-gate fatal("cannot decode server_host_key_blob");
121*7c478bd9Sstevel@tonic-gate if (server_host_key->type != kex->hostkey_type)
122*7c478bd9Sstevel@tonic-gate fatal("type mismatch for decoded server_host_key_blob");
123*7c478bd9Sstevel@tonic-gate if (kex->verify_host_key == NULL)
124*7c478bd9Sstevel@tonic-gate fatal("cannot verify server_host_key");
125*7c478bd9Sstevel@tonic-gate if (kex->verify_host_key(server_host_key) == -1)
126*7c478bd9Sstevel@tonic-gate fatal("server_host_key verification failed");
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate /* DH paramter f, server public DH key */
129*7c478bd9Sstevel@tonic-gate if ((dh_server_pub = BN_new()) == NULL)
130*7c478bd9Sstevel@tonic-gate fatal("dh_server_pub == NULL");
131*7c478bd9Sstevel@tonic-gate packet_get_bignum2(dh_server_pub);
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
134*7c478bd9Sstevel@tonic-gate fprintf(stderr, "dh_server_pub= ");
135*7c478bd9Sstevel@tonic-gate BN_print_fp(stderr, dh_server_pub);
136*7c478bd9Sstevel@tonic-gate fprintf(stderr, "\n");
137*7c478bd9Sstevel@tonic-gate debug("bits %d", BN_num_bits(dh_server_pub));
138*7c478bd9Sstevel@tonic-gate #endif
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate /* signed H */
141*7c478bd9Sstevel@tonic-gate signature = packet_get_string(&slen);
142*7c478bd9Sstevel@tonic-gate packet_check_eom();
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate if (!dh_pub_is_valid(dh, dh_server_pub))
145*7c478bd9Sstevel@tonic-gate packet_disconnect("bad server public DH value");
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate klen = DH_size(dh);
148*7c478bd9Sstevel@tonic-gate kbuf = xmalloc(klen);
149*7c478bd9Sstevel@tonic-gate kout = DH_compute_key(kbuf, dh_server_pub, dh);
150*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
151*7c478bd9Sstevel@tonic-gate dump_digest("shared secret", kbuf, kout);
152*7c478bd9Sstevel@tonic-gate #endif
153*7c478bd9Sstevel@tonic-gate if ((shared_secret = BN_new()) == NULL)
154*7c478bd9Sstevel@tonic-gate fatal("kexgex_client: BN_new failed");
155*7c478bd9Sstevel@tonic-gate BN_bin2bn(kbuf, kout, shared_secret);
156*7c478bd9Sstevel@tonic-gate memset(kbuf, 0, klen);
157*7c478bd9Sstevel@tonic-gate xfree(kbuf);
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate if (datafellows & SSH_OLD_DHGEX)
160*7c478bd9Sstevel@tonic-gate min = max = -1;
161*7c478bd9Sstevel@tonic-gate
162*7c478bd9Sstevel@tonic-gate /* calc and verify H */
163*7c478bd9Sstevel@tonic-gate hash = kexgex_hash(
164*7c478bd9Sstevel@tonic-gate kex->client_version_string,
165*7c478bd9Sstevel@tonic-gate kex->server_version_string,
166*7c478bd9Sstevel@tonic-gate buffer_ptr(&kex->my), buffer_len(&kex->my),
167*7c478bd9Sstevel@tonic-gate buffer_ptr(&kex->peer), buffer_len(&kex->peer),
168*7c478bd9Sstevel@tonic-gate server_host_key_blob, sbloblen,
169*7c478bd9Sstevel@tonic-gate min, nbits, max,
170*7c478bd9Sstevel@tonic-gate dh->p, dh->g,
171*7c478bd9Sstevel@tonic-gate dh->pub_key,
172*7c478bd9Sstevel@tonic-gate dh_server_pub,
173*7c478bd9Sstevel@tonic-gate shared_secret
174*7c478bd9Sstevel@tonic-gate );
175*7c478bd9Sstevel@tonic-gate /* have keys, free DH */
176*7c478bd9Sstevel@tonic-gate DH_free(dh);
177*7c478bd9Sstevel@tonic-gate xfree(server_host_key_blob);
178*7c478bd9Sstevel@tonic-gate BN_clear_free(dh_server_pub);
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate if (key_verify(server_host_key, signature, slen, hash, 20) != 1)
181*7c478bd9Sstevel@tonic-gate fatal("key_verify failed for server_host_key");
182*7c478bd9Sstevel@tonic-gate key_free(server_host_key);
183*7c478bd9Sstevel@tonic-gate xfree(signature);
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate /* save session id */
186*7c478bd9Sstevel@tonic-gate if (kex->session_id == NULL) {
187*7c478bd9Sstevel@tonic-gate kex->session_id_len = 20;
188*7c478bd9Sstevel@tonic-gate kex->session_id = xmalloc(kex->session_id_len);
189*7c478bd9Sstevel@tonic-gate memcpy(kex->session_id, hash, kex->session_id_len);
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate kex_derive_keys(kex, hash, shared_secret);
192*7c478bd9Sstevel@tonic-gate BN_clear_free(shared_secret);
193*7c478bd9Sstevel@tonic-gate
194*7c478bd9Sstevel@tonic-gate kex_finish(kex);
195*7c478bd9Sstevel@tonic-gate }
196