]>
Commit | Line | Data |
---|---|---|
b5afdff5 | 1 | /* $OpenBSD: kexgexs.c,v 1.12 2009/06/21 07:37:15 dtucker Exp $ */ |
c20db291 | 2 | /* |
3 | * Copyright (c) 2000 Niels Provos. All rights reserved. | |
4 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
25 | */ | |
26 | ||
27 | #include "includes.h" | |
30460aeb | 28 | |
29 | #include <sys/param.h> | |
30 | ||
31 | #include <stdarg.h> | |
32 | #include <stdio.h> | |
33 | #include <string.h> | |
34 | #include <signal.h> | |
c20db291 | 35 | |
36 | #include "xmalloc.h" | |
30460aeb | 37 | #include "buffer.h" |
c20db291 | 38 | #include "key.h" |
30460aeb | 39 | #include "cipher.h" |
c20db291 | 40 | #include "kex.h" |
41 | #include "log.h" | |
42 | #include "packet.h" | |
43 | #include "dh.h" | |
44 | #include "ssh2.h" | |
45 | #include "compat.h" | |
30460aeb | 46 | #ifdef GSSAPI |
47 | #include "ssh-gss.h" | |
48 | #endif | |
c20db291 | 49 | #include "monitor_wrap.h" |
50 | ||
51 | void | |
52 | kexgex_server(Kex *kex) | |
53 | { | |
54 | BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; | |
55 | Key *server_host_key; | |
56 | DH *dh; | |
57 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; | |
240debe0 | 58 | u_int sbloblen, klen, slen, hashlen; |
5262cbfb | 59 | int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1; |
60 | int type, kout; | |
c20db291 | 61 | |
62 | if (kex->load_host_key == NULL) | |
63 | fatal("Cannot load hostkey"); | |
64 | server_host_key = kex->load_host_key(kex->hostkey_type); | |
65 | if (server_host_key == NULL) | |
66 | fatal("Unsupported hostkey type %d", kex->hostkey_type); | |
67 | ||
68 | type = packet_read(); | |
69 | switch (type) { | |
70 | case SSH2_MSG_KEX_DH_GEX_REQUEST: | |
71 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); | |
5262cbfb | 72 | omin = min = packet_get_int(); |
73 | onbits = nbits = packet_get_int(); | |
74 | omax = max = packet_get_int(); | |
c20db291 | 75 | min = MAX(DH_GRP_MIN, min); |
76 | max = MIN(DH_GRP_MAX, max); | |
5262cbfb | 77 | nbits = MAX(DH_GRP_MIN, nbits); |
78 | nbits = MIN(DH_GRP_MAX, nbits); | |
c20db291 | 79 | break; |
80 | case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD: | |
81 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received"); | |
5262cbfb | 82 | onbits = nbits = packet_get_int(); |
c20db291 | 83 | /* unused for old GEX */ |
5262cbfb | 84 | omin = min = DH_GRP_MIN; |
85 | omax = max = DH_GRP_MAX; | |
c20db291 | 86 | break; |
87 | default: | |
88 | fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type); | |
89 | } | |
90 | packet_check_eom(); | |
91 | ||
5262cbfb | 92 | if (omax < omin || onbits < omin || omax < onbits) |
c20db291 | 93 | fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d", |
5262cbfb | 94 | omin, onbits, omax); |
c20db291 | 95 | |
96 | /* Contact privileged parent */ | |
97 | dh = PRIVSEP(choose_dh(min, nbits, max)); | |
98 | if (dh == NULL) | |
99 | packet_disconnect("Protocol error: no matching DH grp found"); | |
100 | ||
101 | debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); | |
102 | packet_start(SSH2_MSG_KEX_DH_GEX_GROUP); | |
103 | packet_put_bignum2(dh->p); | |
104 | packet_put_bignum2(dh->g); | |
105 | packet_send(); | |
106 | ||
107 | /* flush */ | |
108 | packet_write_wait(); | |
109 | ||
110 | /* Compute our exchange value in parallel with the client */ | |
111 | dh_gen_key(dh, kex->we_need * 8); | |
112 | ||
113 | debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); | |
114 | packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT); | |
115 | ||
116 | /* key, cert */ | |
117 | if ((dh_client_pub = BN_new()) == NULL) | |
118 | fatal("dh_client_pub == NULL"); | |
119 | packet_get_bignum2(dh_client_pub); | |
120 | packet_check_eom(); | |
121 | ||
122 | #ifdef DEBUG_KEXDH | |
123 | fprintf(stderr, "dh_client_pub= "); | |
124 | BN_print_fp(stderr, dh_client_pub); | |
125 | fprintf(stderr, "\n"); | |
126 | debug("bits %d", BN_num_bits(dh_client_pub)); | |
127 | #endif | |
128 | ||
129 | #ifdef DEBUG_KEXDH | |
130 | DHparams_print_fp(stderr, dh); | |
131 | fprintf(stderr, "pub= "); | |
132 | BN_print_fp(stderr, dh->pub_key); | |
133 | fprintf(stderr, "\n"); | |
134 | #endif | |
135 | if (!dh_pub_is_valid(dh, dh_client_pub)) | |
136 | packet_disconnect("bad client public DH value"); | |
137 | ||
138 | klen = DH_size(dh); | |
139 | kbuf = xmalloc(klen); | |
240debe0 | 140 | if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0) |
141 | fatal("DH_compute_key: failed"); | |
c20db291 | 142 | #ifdef DEBUG_KEXDH |
143 | dump_digest("shared secret", kbuf, kout); | |
144 | #endif | |
145 | if ((shared_secret = BN_new()) == NULL) | |
146 | fatal("kexgex_server: BN_new failed"); | |
240debe0 | 147 | if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) |
148 | fatal("kexgex_server: BN_bin2bn failed"); | |
c20db291 | 149 | memset(kbuf, 0, klen); |
150 | xfree(kbuf); | |
151 | ||
152 | key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); | |
153 | ||
154 | if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) | |
5262cbfb | 155 | omin = min = omax = max = -1; |
c20db291 | 156 | |
08822d99 | 157 | /* calc H */ |
158 | kexgex_hash( | |
159 | kex->evp_md, | |
c20db291 | 160 | kex->client_version_string, |
161 | kex->server_version_string, | |
162 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
163 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
164 | server_host_key_blob, sbloblen, | |
5262cbfb | 165 | omin, onbits, omax, |
c20db291 | 166 | dh->p, dh->g, |
167 | dh_client_pub, | |
168 | dh->pub_key, | |
08822d99 | 169 | shared_secret, |
170 | &hash, &hashlen | |
c20db291 | 171 | ); |
172 | BN_clear_free(dh_client_pub); | |
173 | ||
174 | /* save session id := H */ | |
c20db291 | 175 | if (kex->session_id == NULL) { |
08822d99 | 176 | kex->session_id_len = hashlen; |
c20db291 | 177 | kex->session_id = xmalloc(kex->session_id_len); |
178 | memcpy(kex->session_id, hash, kex->session_id_len); | |
179 | } | |
180 | ||
181 | /* sign H */ | |
b5afdff5 | 182 | if (PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, |
183 | hashlen)) < 0) | |
184 | fatal("kexgex_server: key_sign failed"); | |
c20db291 | 185 | |
186 | /* destroy_sensitive_data(); */ | |
187 | ||
188 | /* send server hostkey, DH pubkey 'f' and singed H */ | |
189 | debug("SSH2_MSG_KEX_DH_GEX_REPLY sent"); | |
190 | packet_start(SSH2_MSG_KEX_DH_GEX_REPLY); | |
191 | packet_put_string(server_host_key_blob, sbloblen); | |
192 | packet_put_bignum2(dh->pub_key); /* f */ | |
193 | packet_put_string(signature, slen); | |
194 | packet_send(); | |
195 | ||
196 | xfree(signature); | |
197 | xfree(server_host_key_blob); | |
198 | /* have keys, free DH */ | |
199 | DH_free(dh); | |
200 | ||
08822d99 | 201 | kex_derive_keys(kex, hash, hashlen, shared_secret); |
c20db291 | 202 | BN_clear_free(shared_secret); |
203 | ||
204 | kex_finish(kex); | |
205 | } |