1 /*
2 * Copyright (c) 2000 Niels Provos. All rights reserved.
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26 #include "includes.h"
27 RCSID("$OpenBSD: kexgex.c,v 1.22 2002/03/24 17:27:03 stevesk Exp $");
28
29 #pragma ident "%Z%%M% %I% %E% SMI"
30
31 #include <openssl/opensslconf.h>
32 #include <openssl/bn.h>
33
34 #include "xmalloc.h"
35 #include "buffer.h"
36 #include "bufaux.h"
37 #include "key.h"
38 #include "kex.h"
39 #include "log.h"
40 #include "packet.h"
41 #include "dh.h"
42 #include "ssh2.h"
43 #include "compat.h"
44
45 void
46 kexgex_server(Kex *kex)
47 {
48 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
49 Key *server_host_key;
50 DH *dh;
51 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
52 u_int sbloblen, klen, kout, slen;
53 int min = -1, max = -1, nbits = -1, type;
54
55 if (kex->load_host_key == NULL)
56 fatal("Cannot load hostkey");
57 server_host_key = kex->load_host_key(kex->hostkey_type);
58 if (server_host_key == NULL)
59 fatal("Unsupported hostkey type %d", kex->hostkey_type);
60
61 type = packet_read();
62 switch (type) {
63 case SSH2_MSG_KEX_DH_GEX_REQUEST:
64 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
65 min = packet_get_int();
66 nbits = packet_get_int();
67 max = packet_get_int();
68 min = MAX(DH_GRP_MIN, min);
69 max = MIN(DH_GRP_MAX, max);
70 break;
71 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
72 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
73 nbits = packet_get_int();
74 min = DH_GRP_MIN;
75 max = DH_GRP_MAX;
76 /* unused for old GEX */
77 break;
78 default:
79 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
80 }
81 packet_check_eom();
82
83 if (max < min || nbits < min || max < nbits)
84 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
85 min, nbits, max);
86
87 /* Contact privileged parent */
88 dh = choose_dh(min, nbits, max);
89 if (dh == NULL)
90 packet_disconnect("Protocol error: no matching DH grp found");
91
92 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
93 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
94 packet_put_bignum2(dh->p);
95 packet_put_bignum2(dh->g);
96 packet_send();
97
98 /* flush */
99 packet_write_wait();
100
101 /* Compute our exchange value in parallel with the client */
102 dh_gen_key(dh, kex->we_need * 8);
103
104 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
105 packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
106
107 /* key, cert */
108 if ((dh_client_pub = BN_new()) == NULL)
109 fatal("dh_client_pub == NULL");
110 packet_get_bignum2(dh_client_pub);
111 packet_check_eom();
112
113 #ifdef DEBUG_KEXDH
114 fprintf(stderr, "dh_client_pub= ");
115 BN_print_fp(stderr, dh_client_pub);
116 fprintf(stderr, "\n");
117 debug("bits %d", BN_num_bits(dh_client_pub));
118 #endif
119
120 #ifdef DEBUG_KEXDH
121 DHparams_print_fp(stderr, dh);
122 fprintf(stderr, "pub= ");
123 BN_print_fp(stderr, dh->pub_key);
124 fprintf(stderr, "\n");
125 #endif
126 if (!dh_pub_is_valid(dh, dh_client_pub))
127 packet_disconnect("bad client public DH value");
128
129 klen = DH_size(dh);
130 kbuf = xmalloc(klen);
131 kout = DH_compute_key(kbuf, dh_client_pub, dh);
132 #ifdef DEBUG_KEXDH
133 dump_digest("shared secret", kbuf, kout);
134 #endif
135 if ((shared_secret = BN_new()) == NULL)
136 fatal("kexgex_server: BN_new failed");
137 BN_bin2bn(kbuf, kout, shared_secret);
138 memset(kbuf, 0, klen);
139 xfree(kbuf);
140
141 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
142
143 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
144 min = max = -1;
145
146 /* calc H */ /* XXX depends on 'kex' */
147 hash = kexgex_hash(
148 kex->client_version_string,
149 kex->server_version_string,
150 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
151 buffer_ptr(&kex->my), buffer_len(&kex->my),
152 server_host_key_blob, sbloblen,
153 min, nbits, max,
154 dh->p, dh->g,
155 dh_client_pub,
156 dh->pub_key,
157 shared_secret
158 );
159 BN_clear_free(dh_client_pub);
160
161 /* save session id := H */
162 /* XXX hashlen depends on KEX */
163 if (kex->session_id == NULL) {
164 kex->session_id_len = 20;
165 kex->session_id = xmalloc(kex->session_id_len);
166 memcpy(kex->session_id, hash, kex->session_id_len);
167 }
168
169 /* sign H */
170 /* XXX hashlen depends on KEX */
171 key_sign(server_host_key, &signature, &slen, hash, 20);
172
173 /* destroy_sensitive_data(); */
174
175 /* send server hostkey, DH pubkey 'f' and singed H */
176 debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
177 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
178 packet_put_string(server_host_key_blob, sbloblen);
179 packet_put_bignum2(dh->pub_key); /* f */
180 packet_put_string(signature, slen);
181 packet_send();
182
183 xfree(signature);
184 xfree(server_host_key_blob);
185 /* have keys, free DH */
186 DH_free(dh);
187
188 kex_derive_keys(kex, hash, shared_secret);
189 BN_clear_free(shared_secret);
190
191 kex_finish(kex);
192 }