]>
Commit | Line | Data |
---|---|---|
98a58eda | 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: kexgexc.c,v 1.1 2003/02/16 17:09:57 markus Exp $"); | |
28 | ||
29 | #include "xmalloc.h" | |
30 | #include "key.h" | |
31 | #include "kex.h" | |
32 | #include "log.h" | |
33 | #include "packet.h" | |
34 | #include "dh.h" | |
35 | #include "ssh2.h" | |
36 | #include "compat.h" | |
37 | ||
38 | void | |
39 | kexgex_client(Kex *kex) | |
40 | { | |
41 | BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; | |
42 | BIGNUM *p = NULL, *g = NULL; | |
43 | Key *server_host_key; | |
44 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; | |
45 | u_int klen, kout, slen, sbloblen; | |
46 | int min, max, nbits; | |
47 | DH *dh; | |
48 | ||
49 | nbits = dh_estimate(kex->we_need * 8); | |
50 | ||
51 | if (datafellows & SSH_OLD_DHGEX) { | |
52 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD sent"); | |
53 | ||
54 | /* Old GEX request */ | |
55 | packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD); | |
56 | packet_put_int(nbits); | |
57 | min = DH_GRP_MIN; | |
58 | max = DH_GRP_MAX; | |
59 | } else { | |
60 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST sent"); | |
61 | ||
62 | /* New GEX request */ | |
63 | min = DH_GRP_MIN; | |
64 | max = DH_GRP_MAX; | |
65 | packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST); | |
66 | packet_put_int(min); | |
67 | packet_put_int(nbits); | |
68 | packet_put_int(max); | |
69 | } | |
70 | #ifdef DEBUG_KEXDH | |
71 | fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n", | |
72 | min, nbits, max); | |
73 | #endif | |
74 | packet_send(); | |
75 | ||
76 | debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP"); | |
77 | packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP); | |
78 | ||
79 | if ((p = BN_new()) == NULL) | |
80 | fatal("BN_new"); | |
81 | packet_get_bignum2(p); | |
82 | if ((g = BN_new()) == NULL) | |
83 | fatal("BN_new"); | |
84 | packet_get_bignum2(g); | |
85 | packet_check_eom(); | |
86 | ||
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); | |
90 | ||
91 | dh = dh_new_group(g, p); | |
92 | dh_gen_key(dh, kex->we_need * 8); | |
93 | ||
94 | #ifdef DEBUG_KEXDH | |
95 | DHparams_print_fp(stderr, dh); | |
96 | fprintf(stderr, "pub= "); | |
97 | BN_print_fp(stderr, dh->pub_key); | |
98 | fprintf(stderr, "\n"); | |
99 | #endif | |
100 | ||
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); | |
105 | packet_send(); | |
106 | ||
107 | debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY"); | |
108 | packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY); | |
109 | ||
110 | /* key, cert */ | |
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"); | |
121 | ||
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); | |
126 | ||
127 | #ifdef DEBUG_KEXDH | |
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)); | |
132 | #endif | |
133 | ||
134 | /* signed H */ | |
135 | signature = packet_get_string(&slen); | |
136 | packet_check_eom(); | |
137 | ||
138 | if (!dh_pub_is_valid(dh, dh_server_pub)) | |
139 | packet_disconnect("bad server public DH value"); | |
140 | ||
141 | klen = DH_size(dh); | |
142 | kbuf = xmalloc(klen); | |
143 | kout = DH_compute_key(kbuf, dh_server_pub, dh); | |
144 | #ifdef DEBUG_KEXDH | |
145 | dump_digest("shared secret", kbuf, kout); | |
146 | #endif | |
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); | |
151 | xfree(kbuf); | |
152 | ||
153 | if (datafellows & SSH_OLD_DHGEX) | |
154 | min = max = -1; | |
155 | ||
156 | /* calc and verify H */ | |
157 | hash = kexgex_hash( | |
158 | kex->client_version_string, | |
159 | kex->server_version_string, | |
160 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
161 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
162 | server_host_key_blob, sbloblen, | |
163 | min, nbits, max, | |
164 | dh->p, dh->g, | |
165 | dh->pub_key, | |
166 | dh_server_pub, | |
167 | shared_secret | |
168 | ); | |
169 | /* have keys, free DH */ | |
170 | DH_free(dh); | |
171 | xfree(server_host_key_blob); | |
172 | BN_clear_free(dh_server_pub); | |
173 | ||
174 | if (key_verify(server_host_key, signature, slen, hash, 20) != 1) | |
175 | fatal("key_verify failed for server_host_key"); | |
176 | key_free(server_host_key); | |
177 | xfree(signature); | |
178 | ||
179 | /* save session id */ | |
180 | if (kex->session_id == NULL) { | |
181 | kex->session_id_len = 20; | |
182 | kex->session_id = xmalloc(kex->session_id_len); | |
183 | memcpy(kex->session_id, hash, kex->session_id_len); | |
184 | } | |
185 | kex_derive_keys(kex, hash, shared_secret); | |
186 | BN_clear_free(shared_secret); | |
187 | ||
188 | kex_finish(kex); | |
189 | } |