diff options
author | Ben Lindstrom <mouring@eviladmin.org> | 2001-04-04 17:39:19 +0000 |
---|---|---|
committer | Ben Lindstrom <mouring@eviladmin.org> | 2001-04-04 17:39:19 +0000 |
commit | 22b19b43e420c9fc711a38631187820573d0f047 (patch) | |
tree | bad6afe1968acc7927423325902efd1054f1b4e0 | |
parent | f28f634a3ab81669c21fb078565c84410653b8b7 (diff) |
- (bal) Oops.. Missed including kexdh.c and kexgex.c in OpenBSD sync.
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | Makefile.in | 4 | ||||
-rw-r--r-- | kexdh.c | 304 | ||||
-rw-r--r-- | kexgex.c | 411 |
4 files changed, 719 insertions, 3 deletions
@@ -25,6 +25,7 @@ | |||
25 | (1) force rekeying with ~R, or | 25 | (1) force rekeying with ~R, or |
26 | (2) if the server requests rekeying. | 26 | (2) if the server requests rekeying. |
27 | works against ssh-2.0.12/2.0.13/2.1.0/2.2.0/2.3.0/2.3.1/2.4.0 | 27 | works against ssh-2.0.12/2.0.13/2.1.0/2.2.0/2.3.0/2.3.1/2.4.0 |
28 | - (bal) Oops.. Missed including kexdh.c and kexgex.c in OpenBSD sync. | ||
28 | 29 | ||
29 | 20010403 | 30 | 20010403 |
30 | - OpenBSD CVS Sync | 31 | - OpenBSD CVS Sync |
@@ -4835,4 +4836,4 @@ | |||
4835 | - Wrote replacements for strlcpy and mkdtemp | 4836 | - Wrote replacements for strlcpy and mkdtemp |
4836 | - Released 1.0pre1 | 4837 | - Released 1.0pre1 |
4837 | 4838 | ||
4838 | $Id: ChangeLog,v 1.1053 2001/04/04 02:03:04 mouring Exp $ | 4839 | $Id: ChangeLog,v 1.1054 2001/04/04 17:39:19 mouring Exp $ |
diff --git a/Makefile.in b/Makefile.in index 98d9a46c9..c1f197e40 100644 --- a/Makefile.in +++ b/Makefile.in | |||
@@ -1,4 +1,4 @@ | |||
1 | # $Id: Makefile.in,v 1.164 2001/03/30 00:55:20 djm Exp $ | 1 | # $Id: Makefile.in,v 1.165 2001/04/04 17:39:20 mouring Exp $ |
2 | 2 | ||
3 | prefix=@prefix@ | 3 | prefix=@prefix@ |
4 | exec_prefix=@exec_prefix@ | 4 | exec_prefix=@exec_prefix@ |
@@ -44,7 +44,7 @@ INSTALL_SSH_PRNG_CMDS=@INSTALL_SSH_PRNG_CMDS@ | |||
44 | 44 | ||
45 | TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-add$(EXEEXT) ssh-keygen$(EXEEXT) ssh-keyscan${EXEEXT} ssh-agent$(EXEEXT) scp$(EXEEXT) $(SFTP_PROGS) | 45 | TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-add$(EXEEXT) ssh-keygen$(EXEEXT) ssh-keyscan${EXEEXT} ssh-agent$(EXEEXT) scp$(EXEEXT) $(SFTP_PROGS) |
46 | 46 | ||
47 | LIBSSH_OBJS=atomicio.o authfd.o authfile.o bufaux.o buffer.o canohost.o channels.o cipher.o cli.o compat.o compress.o crc32.o deattack.o dh.o dispatch.o mac.o hostfile.o key.o kex.o log.o match.o misc.o mpaux.o nchan.o packet.o radix.o rijndael.o entropy.o readpass.o rsa.o ssh-dss.o ssh-rsa.o tildexpand.o ttymodes.o uidswap.o uuencode.o xmalloc.o | 47 | LIBSSH_OBJS=atomicio.o authfd.o authfile.o bufaux.o buffer.o canohost.o channels.o cipher.o cli.o compat.o compress.o crc32.o deattack.o dh.o dispatch.o mac.o hostfile.o key.o kex.o kexdh.o kexgex.o log.o match.o misc.o mpaux.o nchan.o packet.o radix.o rijndael.o entropy.o readpass.o rsa.o ssh-dss.o ssh-rsa.o tildexpand.o ttymodes.o uidswap.o uuencode.o xmalloc.o |
48 | 48 | ||
49 | SSHOBJS= ssh.o sshconnect.o sshconnect1.o sshconnect2.o readconf.o clientloop.o | 49 | SSHOBJS= ssh.o sshconnect.o sshconnect1.o sshconnect2.o readconf.o clientloop.o |
50 | 50 | ||
diff --git a/kexdh.c b/kexdh.c new file mode 100644 index 000000000..8449ec06a --- /dev/null +++ b/kexdh.c | |||
@@ -0,0 +1,304 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | ||
3 | * | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions | ||
6 | * are met: | ||
7 | * 1. Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * 2. Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * | ||
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
23 | */ | ||
24 | |||
25 | #include "includes.h" | ||
26 | RCSID("$OpenBSD: kexdh.c,v 1.2 2001/04/03 23:32:12 markus Exp $"); | ||
27 | |||
28 | #include <openssl/crypto.h> | ||
29 | #include <openssl/bn.h> | ||
30 | |||
31 | #include "xmalloc.h" | ||
32 | #include "buffer.h" | ||
33 | #include "bufaux.h" | ||
34 | #include "key.h" | ||
35 | #include "kex.h" | ||
36 | #include "log.h" | ||
37 | #include "packet.h" | ||
38 | #include "dh.h" | ||
39 | #include "ssh2.h" | ||
40 | |||
41 | u_char * | ||
42 | kex_dh_hash( | ||
43 | char *client_version_string, | ||
44 | char *server_version_string, | ||
45 | char *ckexinit, int ckexinitlen, | ||
46 | char *skexinit, int skexinitlen, | ||
47 | char *serverhostkeyblob, int sbloblen, | ||
48 | BIGNUM *client_dh_pub, | ||
49 | BIGNUM *server_dh_pub, | ||
50 | BIGNUM *shared_secret) | ||
51 | { | ||
52 | Buffer b; | ||
53 | static u_char digest[EVP_MAX_MD_SIZE]; | ||
54 | EVP_MD *evp_md = EVP_sha1(); | ||
55 | EVP_MD_CTX md; | ||
56 | |||
57 | buffer_init(&b); | ||
58 | buffer_put_string(&b, client_version_string, strlen(client_version_string)); | ||
59 | buffer_put_string(&b, server_version_string, strlen(server_version_string)); | ||
60 | |||
61 | /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ | ||
62 | buffer_put_int(&b, ckexinitlen+1); | ||
63 | buffer_put_char(&b, SSH2_MSG_KEXINIT); | ||
64 | buffer_append(&b, ckexinit, ckexinitlen); | ||
65 | buffer_put_int(&b, skexinitlen+1); | ||
66 | buffer_put_char(&b, SSH2_MSG_KEXINIT); | ||
67 | buffer_append(&b, skexinit, skexinitlen); | ||
68 | |||
69 | buffer_put_string(&b, serverhostkeyblob, sbloblen); | ||
70 | buffer_put_bignum2(&b, client_dh_pub); | ||
71 | buffer_put_bignum2(&b, server_dh_pub); | ||
72 | buffer_put_bignum2(&b, shared_secret); | ||
73 | |||
74 | #ifdef DEBUG_KEX | ||
75 | buffer_dump(&b); | ||
76 | #endif | ||
77 | EVP_DigestInit(&md, evp_md); | ||
78 | EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); | ||
79 | EVP_DigestFinal(&md, digest, NULL); | ||
80 | |||
81 | buffer_free(&b); | ||
82 | |||
83 | #ifdef DEBUG_KEX | ||
84 | dump_digest("hash", digest, evp_md->md_size); | ||
85 | #endif | ||
86 | return digest; | ||
87 | } | ||
88 | |||
89 | /* client */ | ||
90 | |||
91 | void | ||
92 | kexdh_client(Kex *kex) | ||
93 | { | ||
94 | BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; | ||
95 | DH *dh; | ||
96 | Key *server_host_key; | ||
97 | char *server_host_key_blob = NULL, *signature = NULL; | ||
98 | u_char *kbuf, *hash; | ||
99 | u_int klen, kout, slen, sbloblen; | ||
100 | int dlen, plen; | ||
101 | |||
102 | /* generate and send 'e', client DH public key */ | ||
103 | dh = dh_new_group1(); | ||
104 | dh_gen_key(dh, kex->we_need * 8); | ||
105 | packet_start(SSH2_MSG_KEXDH_INIT); | ||
106 | packet_put_bignum2(dh->pub_key); | ||
107 | packet_send(); | ||
108 | |||
109 | debug("sending SSH2_MSG_KEXDH_INIT"); | ||
110 | #ifdef DEBUG_KEXDH | ||
111 | DHparams_print_fp(stderr, dh); | ||
112 | fprintf(stderr, "pub= "); | ||
113 | BN_print_fp(stderr, dh->pub_key); | ||
114 | fprintf(stderr, "\n"); | ||
115 | #endif | ||
116 | |||
117 | debug("expecting SSH2_MSG_KEXDH_REPLY"); | ||
118 | packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY); | ||
119 | |||
120 | /* key, cert */ | ||
121 | server_host_key_blob = packet_get_string(&sbloblen); | ||
122 | server_host_key = key_from_blob(server_host_key_blob, sbloblen); | ||
123 | if (server_host_key == NULL) | ||
124 | fatal("cannot decode server_host_key_blob"); | ||
125 | |||
126 | if (kex->check_host_key == NULL) | ||
127 | fatal("cannot check server_host_key"); | ||
128 | kex->check_host_key(server_host_key); | ||
129 | |||
130 | /* DH paramter f, server public DH key */ | ||
131 | dh_server_pub = BN_new(); | ||
132 | if (dh_server_pub == NULL) | ||
133 | fatal("dh_server_pub == NULL"); | ||
134 | packet_get_bignum2(dh_server_pub, &dlen); | ||
135 | |||
136 | #ifdef DEBUG_KEXDH | ||
137 | fprintf(stderr, "dh_server_pub= "); | ||
138 | BN_print_fp(stderr, dh_server_pub); | ||
139 | fprintf(stderr, "\n"); | ||
140 | debug("bits %d", BN_num_bits(dh_server_pub)); | ||
141 | #endif | ||
142 | |||
143 | /* signed H */ | ||
144 | signature = packet_get_string(&slen); | ||
145 | packet_done(); | ||
146 | |||
147 | if (!dh_pub_is_valid(dh, dh_server_pub)) | ||
148 | packet_disconnect("bad server public DH value"); | ||
149 | |||
150 | klen = DH_size(dh); | ||
151 | kbuf = xmalloc(klen); | ||
152 | kout = DH_compute_key(kbuf, dh_server_pub, dh); | ||
153 | #ifdef DEBUG_KEXDH | ||
154 | dump_digest("shared secret", kbuf, kout); | ||
155 | #endif | ||
156 | shared_secret = BN_new(); | ||
157 | BN_bin2bn(kbuf, kout, shared_secret); | ||
158 | memset(kbuf, 0, klen); | ||
159 | xfree(kbuf); | ||
160 | |||
161 | /* calc and verify H */ | ||
162 | hash = kex_dh_hash( | ||
163 | kex->client_version_string, | ||
164 | kex->server_version_string, | ||
165 | buffer_ptr(&kex->my), buffer_len(&kex->my), | ||
166 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | ||
167 | server_host_key_blob, sbloblen, | ||
168 | dh->pub_key, | ||
169 | dh_server_pub, | ||
170 | shared_secret | ||
171 | ); | ||
172 | xfree(server_host_key_blob); | ||
173 | DH_free(dh); | ||
174 | BN_free(dh_server_pub); | ||
175 | |||
176 | if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1) | ||
177 | fatal("key_verify failed for server_host_key"); | ||
178 | key_free(server_host_key); | ||
179 | xfree(signature); | ||
180 | |||
181 | /* save session id */ | ||
182 | if (kex->session_id == NULL) { | ||
183 | kex->session_id_len = 20; | ||
184 | kex->session_id = xmalloc(kex->session_id_len); | ||
185 | memcpy(kex->session_id, hash, kex->session_id_len); | ||
186 | } | ||
187 | |||
188 | kex_derive_keys(kex, hash, shared_secret); | ||
189 | BN_clear_free(shared_secret); | ||
190 | kex_send_newkeys(); | ||
191 | } | ||
192 | |||
193 | /* server */ | ||
194 | |||
195 | void | ||
196 | kexdh_server(Kex *kex) | ||
197 | { | ||
198 | BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; | ||
199 | DH *dh; | ||
200 | Key *server_host_key; | ||
201 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; | ||
202 | u_int sbloblen, klen, kout; | ||
203 | int dlen, slen, plen; | ||
204 | |||
205 | /* generate server DH public key */ | ||
206 | dh = dh_new_group1(); | ||
207 | dh_gen_key(dh, kex->we_need * 8); | ||
208 | |||
209 | debug("expecting SSH2_MSG_KEXDH_INIT"); | ||
210 | packet_read_expect(&plen, SSH2_MSG_KEXDH_INIT); | ||
211 | |||
212 | if (kex->load_host_key == NULL) | ||
213 | fatal("Cannot load hostkey"); | ||
214 | server_host_key = kex->load_host_key(kex->hostkey_type); | ||
215 | if (server_host_key == NULL) | ||
216 | fatal("Unsupported hostkey type %d", kex->hostkey_type); | ||
217 | |||
218 | /* key, cert */ | ||
219 | dh_client_pub = BN_new(); | ||
220 | if (dh_client_pub == NULL) | ||
221 | fatal("dh_client_pub == NULL"); | ||
222 | packet_get_bignum2(dh_client_pub, &dlen); | ||
223 | |||
224 | #ifdef DEBUG_KEXDH | ||
225 | fprintf(stderr, "dh_client_pub= "); | ||
226 | BN_print_fp(stderr, dh_client_pub); | ||
227 | fprintf(stderr, "\n"); | ||
228 | debug("bits %d", BN_num_bits(dh_client_pub)); | ||
229 | #endif | ||
230 | |||
231 | #ifdef DEBUG_KEXDH | ||
232 | DHparams_print_fp(stderr, dh); | ||
233 | fprintf(stderr, "pub= "); | ||
234 | BN_print_fp(stderr, dh->pub_key); | ||
235 | fprintf(stderr, "\n"); | ||
236 | #endif | ||
237 | if (!dh_pub_is_valid(dh, dh_client_pub)) | ||
238 | packet_disconnect("bad client public DH value"); | ||
239 | |||
240 | klen = DH_size(dh); | ||
241 | kbuf = xmalloc(klen); | ||
242 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | ||
243 | #ifdef DEBUG_KEXDH | ||
244 | dump_digest("shared secret", kbuf, kout); | ||
245 | #endif | ||
246 | shared_secret = BN_new(); | ||
247 | BN_bin2bn(kbuf, kout, shared_secret); | ||
248 | memset(kbuf, 0, klen); | ||
249 | xfree(kbuf); | ||
250 | |||
251 | key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); | ||
252 | |||
253 | /* calc H */ | ||
254 | hash = kex_dh_hash( | ||
255 | kex->client_version_string, | ||
256 | kex->server_version_string, | ||
257 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | ||
258 | buffer_ptr(&kex->my), buffer_len(&kex->my), | ||
259 | (char *)server_host_key_blob, sbloblen, | ||
260 | dh_client_pub, | ||
261 | dh->pub_key, | ||
262 | shared_secret | ||
263 | ); | ||
264 | BN_free(dh_client_pub); | ||
265 | |||
266 | /* save session id := H */ | ||
267 | /* XXX hashlen depends on KEX */ | ||
268 | if (kex->session_id == NULL) { | ||
269 | kex->session_id_len = 20; | ||
270 | kex->session_id = xmalloc(kex->session_id_len); | ||
271 | memcpy(kex->session_id, hash, kex->session_id_len); | ||
272 | } | ||
273 | |||
274 | /* sign H */ | ||
275 | /* XXX hashlen depends on KEX */ | ||
276 | key_sign(server_host_key, &signature, &slen, hash, 20); | ||
277 | |||
278 | /* destroy_sensitive_data(); */ | ||
279 | |||
280 | /* send server hostkey, DH pubkey 'f' and singed H */ | ||
281 | packet_start(SSH2_MSG_KEXDH_REPLY); | ||
282 | packet_put_string((char *)server_host_key_blob, sbloblen); | ||
283 | packet_put_bignum2(dh->pub_key); /* f */ | ||
284 | packet_put_string((char *)signature, slen); | ||
285 | packet_send(); | ||
286 | xfree(signature); | ||
287 | xfree(server_host_key_blob); | ||
288 | |||
289 | kex_derive_keys(kex, hash, shared_secret); | ||
290 | BN_clear_free(shared_secret); | ||
291 | kex_send_newkeys(); | ||
292 | |||
293 | /* have keys, free DH */ | ||
294 | DH_free(dh); | ||
295 | } | ||
296 | |||
297 | void | ||
298 | kexdh(Kex *kex) | ||
299 | { | ||
300 | if (kex->server) | ||
301 | kexdh_server(kex); | ||
302 | else | ||
303 | kexdh_client(kex); | ||
304 | } | ||
diff --git a/kexgex.c b/kexgex.c new file mode 100644 index 000000000..6e8be78b5 --- /dev/null +++ b/kexgex.c | |||
@@ -0,0 +1,411 @@ | |||
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.2 2001/04/03 23:32:12 markus Exp $"); | ||
28 | |||
29 | #include <openssl/bn.h> | ||
30 | |||
31 | #include "xmalloc.h" | ||
32 | #include "buffer.h" | ||
33 | #include "bufaux.h" | ||
34 | #include "key.h" | ||
35 | #include "kex.h" | ||
36 | #include "log.h" | ||
37 | #include "packet.h" | ||
38 | #include "dh.h" | ||
39 | #include "ssh2.h" | ||
40 | #include "compat.h" | ||
41 | |||
42 | u_char * | ||
43 | kexgex_hash( | ||
44 | char *client_version_string, | ||
45 | char *server_version_string, | ||
46 | char *ckexinit, int ckexinitlen, | ||
47 | char *skexinit, int skexinitlen, | ||
48 | char *serverhostkeyblob, int sbloblen, | ||
49 | int min, int wantbits, int max, BIGNUM *prime, BIGNUM *gen, | ||
50 | BIGNUM *client_dh_pub, | ||
51 | BIGNUM *server_dh_pub, | ||
52 | BIGNUM *shared_secret) | ||
53 | { | ||
54 | Buffer b; | ||
55 | static u_char digest[EVP_MAX_MD_SIZE]; | ||
56 | EVP_MD *evp_md = EVP_sha1(); | ||
57 | EVP_MD_CTX md; | ||
58 | |||
59 | buffer_init(&b); | ||
60 | buffer_put_string(&b, client_version_string, strlen(client_version_string)); | ||
61 | buffer_put_string(&b, server_version_string, strlen(server_version_string)); | ||
62 | |||
63 | /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ | ||
64 | buffer_put_int(&b, ckexinitlen+1); | ||
65 | buffer_put_char(&b, SSH2_MSG_KEXINIT); | ||
66 | buffer_append(&b, ckexinit, ckexinitlen); | ||
67 | buffer_put_int(&b, skexinitlen+1); | ||
68 | buffer_put_char(&b, SSH2_MSG_KEXINIT); | ||
69 | buffer_append(&b, skexinit, skexinitlen); | ||
70 | |||
71 | buffer_put_string(&b, serverhostkeyblob, sbloblen); | ||
72 | if (min == -1 || max == -1) | ||
73 | buffer_put_int(&b, wantbits); | ||
74 | else { | ||
75 | buffer_put_int(&b, min); | ||
76 | buffer_put_int(&b, wantbits); | ||
77 | buffer_put_int(&b, max); | ||
78 | } | ||
79 | buffer_put_bignum2(&b, prime); | ||
80 | buffer_put_bignum2(&b, gen); | ||
81 | buffer_put_bignum2(&b, client_dh_pub); | ||
82 | buffer_put_bignum2(&b, server_dh_pub); | ||
83 | buffer_put_bignum2(&b, shared_secret); | ||
84 | |||
85 | #ifdef DEBUG_KEXDH | ||
86 | buffer_dump(&b); | ||
87 | #endif | ||
88 | EVP_DigestInit(&md, evp_md); | ||
89 | EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); | ||
90 | EVP_DigestFinal(&md, digest, NULL); | ||
91 | |||
92 | buffer_free(&b); | ||
93 | |||
94 | #ifdef DEBUG_KEXDH | ||
95 | dump_digest("hash", digest, evp_md->md_size); | ||
96 | #endif | ||
97 | return digest; | ||
98 | } | ||
99 | |||
100 | /* client */ | ||
101 | |||
102 | void | ||
103 | kexgex_client(Kex *kex) | ||
104 | { | ||
105 | BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; | ||
106 | BIGNUM *p = NULL, *g = NULL; | ||
107 | Key *server_host_key; | ||
108 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; | ||
109 | u_int klen, kout, slen, sbloblen; | ||
110 | int dlen, plen, min, max, nbits; | ||
111 | DH *dh; | ||
112 | |||
113 | nbits = dh_estimate(kex->we_need * 8); | ||
114 | |||
115 | if (datafellows & SSH_OLD_DHGEX) { | ||
116 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD sent"); | ||
117 | |||
118 | /* Old GEX request */ | ||
119 | packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD); | ||
120 | packet_put_int(nbits); | ||
121 | min = DH_GRP_MIN; | ||
122 | max = DH_GRP_MAX; | ||
123 | } else { | ||
124 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST sent"); | ||
125 | |||
126 | /* New GEX request */ | ||
127 | min = DH_GRP_MIN; | ||
128 | max = DH_GRP_MAX; | ||
129 | packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST); | ||
130 | packet_put_int(min); | ||
131 | packet_put_int(nbits); | ||
132 | packet_put_int(max); | ||
133 | } | ||
134 | #ifdef DEBUG_KEXDH | ||
135 | fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n", | ||
136 | min, nbits, max); | ||
137 | #endif | ||
138 | packet_send(); | ||
139 | |||
140 | debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP"); | ||
141 | packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP); | ||
142 | |||
143 | if ((p = BN_new()) == NULL) | ||
144 | fatal("BN_new"); | ||
145 | packet_get_bignum2(p, &dlen); | ||
146 | if ((g = BN_new()) == NULL) | ||
147 | fatal("BN_new"); | ||
148 | packet_get_bignum2(g, &dlen); | ||
149 | packet_done(); | ||
150 | |||
151 | if (BN_num_bits(p) < min || BN_num_bits(p) > max) | ||
152 | fatal("DH_GEX group out of range: %d !< %d !< %d", | ||
153 | min, BN_num_bits(p), max); | ||
154 | |||
155 | dh = dh_new_group(g, p); | ||
156 | dh_gen_key(dh, kex->we_need * 8); | ||
157 | |||
158 | #ifdef DEBUG_KEXDH | ||
159 | DHparams_print_fp(stderr, dh); | ||
160 | fprintf(stderr, "pub= "); | ||
161 | BN_print_fp(stderr, dh->pub_key); | ||
162 | fprintf(stderr, "\n"); | ||
163 | #endif | ||
164 | |||
165 | debug("SSH2_MSG_KEX_DH_GEX_INIT sent"); | ||
166 | /* generate and send 'e', client DH public key */ | ||
167 | packet_start(SSH2_MSG_KEX_DH_GEX_INIT); | ||
168 | packet_put_bignum2(dh->pub_key); | ||
169 | packet_send(); | ||
170 | |||
171 | debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY"); | ||
172 | packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY); | ||
173 | |||
174 | /* key, cert */ | ||
175 | server_host_key_blob = packet_get_string(&sbloblen); | ||
176 | server_host_key = key_from_blob(server_host_key_blob, sbloblen); | ||
177 | if (server_host_key == NULL) | ||
178 | fatal("cannot decode server_host_key_blob"); | ||
179 | |||
180 | if (kex->check_host_key == NULL) | ||
181 | fatal("cannot check server_host_key"); | ||
182 | kex->check_host_key(server_host_key); | ||
183 | |||
184 | /* DH paramter f, server public DH key */ | ||
185 | dh_server_pub = BN_new(); | ||
186 | if (dh_server_pub == NULL) | ||
187 | fatal("dh_server_pub == NULL"); | ||
188 | packet_get_bignum2(dh_server_pub, &dlen); | ||
189 | |||
190 | #ifdef DEBUG_KEXDH | ||
191 | fprintf(stderr, "dh_server_pub= "); | ||
192 | BN_print_fp(stderr, dh_server_pub); | ||
193 | fprintf(stderr, "\n"); | ||
194 | debug("bits %d", BN_num_bits(dh_server_pub)); | ||
195 | #endif | ||
196 | |||
197 | /* signed H */ | ||
198 | signature = packet_get_string(&slen); | ||
199 | packet_done(); | ||
200 | |||
201 | if (!dh_pub_is_valid(dh, dh_server_pub)) | ||
202 | packet_disconnect("bad server public DH value"); | ||
203 | |||
204 | klen = DH_size(dh); | ||
205 | kbuf = xmalloc(klen); | ||
206 | kout = DH_compute_key(kbuf, dh_server_pub, dh); | ||
207 | #ifdef DEBUG_KEXDH | ||
208 | dump_digest("shared secret", kbuf, kout); | ||
209 | #endif | ||
210 | shared_secret = BN_new(); | ||
211 | BN_bin2bn(kbuf, kout, shared_secret); | ||
212 | memset(kbuf, 0, klen); | ||
213 | xfree(kbuf); | ||
214 | |||
215 | if (datafellows & SSH_OLD_DHGEX) | ||
216 | min = max = -1; | ||
217 | |||
218 | /* calc and verify H */ | ||
219 | hash = kexgex_hash( | ||
220 | kex->client_version_string, | ||
221 | kex->server_version_string, | ||
222 | buffer_ptr(&kex->my), buffer_len(&kex->my), | ||
223 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | ||
224 | server_host_key_blob, sbloblen, | ||
225 | min, nbits, max, | ||
226 | dh->p, dh->g, | ||
227 | dh->pub_key, | ||
228 | dh_server_pub, | ||
229 | shared_secret | ||
230 | ); | ||
231 | xfree(server_host_key_blob); | ||
232 | BN_free(dh_server_pub); | ||
233 | |||
234 | if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1) | ||
235 | fatal("key_verify failed for server_host_key"); | ||
236 | key_free(server_host_key); | ||
237 | xfree(signature); | ||
238 | |||
239 | /* save session id */ | ||
240 | if (kex->session_id == NULL) { | ||
241 | kex->session_id_len = 20; | ||
242 | kex->session_id = xmalloc(kex->session_id_len); | ||
243 | memcpy(kex->session_id, hash, kex->session_id_len); | ||
244 | } | ||
245 | |||
246 | kex_derive_keys(kex, hash, shared_secret); | ||
247 | BN_clear_free(shared_secret); | ||
248 | |||
249 | kex_send_newkeys(); | ||
250 | |||
251 | /* have keys, free DH */ | ||
252 | DH_free(dh); | ||
253 | } | ||
254 | |||
255 | /* server */ | ||
256 | |||
257 | void | ||
258 | kexgex_server(Kex *kex) | ||
259 | { | ||
260 | BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; | ||
261 | Key *server_host_key; | ||
262 | DH *dh = dh; | ||
263 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; | ||
264 | u_int sbloblen, klen, kout; | ||
265 | int min = -1, max = -1, nbits = -1, type, plen, dlen, slen; | ||
266 | |||
267 | if (kex->load_host_key == NULL) | ||
268 | fatal("Cannot load hostkey"); | ||
269 | server_host_key = kex->load_host_key(kex->hostkey_type); | ||
270 | if (server_host_key == NULL) | ||
271 | fatal("Unsupported hostkey type %d", kex->hostkey_type); | ||
272 | |||
273 | type = packet_read(&plen); | ||
274 | switch(type){ | ||
275 | case SSH2_MSG_KEX_DH_GEX_REQUEST: | ||
276 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); | ||
277 | min = packet_get_int(); | ||
278 | nbits = packet_get_int(); | ||
279 | max = packet_get_int(); | ||
280 | min = MAX(DH_GRP_MIN, min); | ||
281 | max = MIN(DH_GRP_MAX, max); | ||
282 | break; | ||
283 | case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD: | ||
284 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received"); | ||
285 | nbits = packet_get_int(); | ||
286 | min = DH_GRP_MIN; | ||
287 | max = DH_GRP_MAX; | ||
288 | /* unused for old GEX */ | ||
289 | break; | ||
290 | default: | ||
291 | fatal("protocol error during kex, no DH_GEX_REQUEST"); | ||
292 | } | ||
293 | packet_done(); | ||
294 | |||
295 | if (max < min || nbits < min || max < nbits) | ||
296 | fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d", | ||
297 | min, nbits, max); | ||
298 | |||
299 | dh = choose_dh(min, nbits, max); | ||
300 | if (dh == NULL) | ||
301 | packet_disconnect("Protocol error: no matching DH grp found"); | ||
302 | |||
303 | debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); | ||
304 | packet_start(SSH2_MSG_KEX_DH_GEX_GROUP); | ||
305 | packet_put_bignum2(dh->p); | ||
306 | packet_put_bignum2(dh->g); | ||
307 | packet_send(); | ||
308 | |||
309 | /* flush */ | ||
310 | packet_write_wait(); | ||
311 | |||
312 | /* Compute our exchange value in parallel with the client */ | ||
313 | dh_gen_key(dh, kex->we_need * 8); | ||
314 | |||
315 | debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); | ||
316 | packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_INIT); | ||
317 | |||
318 | /* key, cert */ | ||
319 | dh_client_pub = BN_new(); | ||
320 | if (dh_client_pub == NULL) | ||
321 | fatal("dh_client_pub == NULL"); | ||
322 | packet_get_bignum2(dh_client_pub, &dlen); | ||
323 | |||
324 | #ifdef DEBUG_KEXDH | ||
325 | fprintf(stderr, "dh_client_pub= "); | ||
326 | BN_print_fp(stderr, dh_client_pub); | ||
327 | fprintf(stderr, "\n"); | ||
328 | debug("bits %d", BN_num_bits(dh_client_pub)); | ||
329 | #endif | ||
330 | |||
331 | #ifdef DEBUG_KEXDH | ||
332 | DHparams_print_fp(stderr, dh); | ||
333 | fprintf(stderr, "pub= "); | ||
334 | BN_print_fp(stderr, dh->pub_key); | ||
335 | fprintf(stderr, "\n"); | ||
336 | #endif | ||
337 | if (!dh_pub_is_valid(dh, dh_client_pub)) | ||
338 | packet_disconnect("bad client public DH value"); | ||
339 | |||
340 | klen = DH_size(dh); | ||
341 | kbuf = xmalloc(klen); | ||
342 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | ||
343 | #ifdef DEBUG_KEXDH | ||
344 | dump_digest("shared secret", kbuf, kout); | ||
345 | #endif | ||
346 | shared_secret = BN_new(); | ||
347 | BN_bin2bn(kbuf, kout, shared_secret); | ||
348 | memset(kbuf, 0, klen); | ||
349 | xfree(kbuf); | ||
350 | |||
351 | key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); | ||
352 | |||
353 | if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) | ||
354 | min = max = -1; | ||
355 | |||
356 | /* calc H */ /* XXX depends on 'kex' */ | ||
357 | hash = kexgex_hash( | ||
358 | kex->client_version_string, | ||
359 | kex->server_version_string, | ||
360 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | ||
361 | buffer_ptr(&kex->my), buffer_len(&kex->my), | ||
362 | (char *)server_host_key_blob, sbloblen, | ||
363 | min, nbits, max, | ||
364 | dh->p, dh->g, | ||
365 | dh_client_pub, | ||
366 | dh->pub_key, | ||
367 | shared_secret | ||
368 | ); | ||
369 | BN_free(dh_client_pub); | ||
370 | |||
371 | /* save session id := H */ | ||
372 | /* XXX hashlen depends on KEX */ | ||
373 | if (kex->session_id == NULL) { | ||
374 | kex->session_id_len = 20; | ||
375 | kex->session_id = xmalloc(kex->session_id_len); | ||
376 | memcpy(kex->session_id, hash, kex->session_id_len); | ||
377 | } | ||
378 | |||
379 | /* sign H */ | ||
380 | /* XXX hashlen depends on KEX */ | ||
381 | key_sign(server_host_key, &signature, &slen, hash, 20); | ||
382 | |||
383 | /* destroy_sensitive_data(); */ | ||
384 | |||
385 | /* send server hostkey, DH pubkey 'f' and singed H */ | ||
386 | debug("SSH2_MSG_KEX_DH_GEX_REPLY sent"); | ||
387 | packet_start(SSH2_MSG_KEX_DH_GEX_REPLY); | ||
388 | packet_put_string((char *)server_host_key_blob, sbloblen); | ||
389 | packet_put_bignum2(dh->pub_key); /* f */ | ||
390 | packet_put_string((char *)signature, slen); | ||
391 | packet_send(); | ||
392 | xfree(signature); | ||
393 | xfree(server_host_key_blob); | ||
394 | |||
395 | kex_derive_keys(kex, hash, shared_secret); | ||
396 | BN_clear_free(shared_secret); | ||
397 | |||
398 | kex_send_newkeys(); | ||
399 | |||
400 | /* have keys, free DH */ | ||
401 | DH_free(dh); | ||
402 | } | ||
403 | |||
404 | void | ||
405 | kexgex(Kex *kex) | ||
406 | { | ||
407 | if (kex->server) | ||
408 | kexgex_server(kex); | ||
409 | else | ||
410 | kexgex_client(kex); | ||
411 | } | ||