]>
Commit | Line | Data |
---|---|---|
b46fa825 | 1 | /* |
f97edba6 | 2 | * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved. |
b46fa825 | 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 | ||
27 | #ifdef GSSAPI | |
28 | ||
f713db99 | 29 | #include <string.h> |
30 | ||
b46fa825 | 31 | #include <openssl/crypto.h> |
32 | #include <openssl/bn.h> | |
33 | ||
34 | #include "xmalloc.h" | |
35 | #include "buffer.h" | |
f713db99 | 36 | #include "ssh2.h" |
30460aeb | 37 | #include "key.h" |
f713db99 | 38 | #include "cipher.h" |
b46fa825 | 39 | #include "kex.h" |
40 | #include "log.h" | |
41 | #include "packet.h" | |
42 | #include "dh.h" | |
b46fa825 | 43 | #include "ssh-gss.h" |
44 | #include "monitor_wrap.h" | |
f97edba6 | 45 | #include "servconf.h" |
23987cb8 | 46 | |
47 | static void kex_gss_send_error(Gssctxt *ctxt); | |
f97edba6 | 48 | extern ServerOptions options; |
b46fa825 | 49 | |
50 | void | |
51 | kexgss_server(Kex *kex) | |
52 | { | |
b46fa825 | 53 | OM_uint32 maj_status, min_status; |
54 | ||
fe4ad273 | 55 | /* |
56 | * Some GSSAPI implementations use the input value of ret_flags (an | |
e23e524c | 57 | * output variable) as a means of triggering mechanism specific |
58 | * features. Initializing it to zero avoids inadvertently | |
fe4ad273 | 59 | * activating this non-standard behaviour. |
60 | */ | |
b46fa825 | 61 | |
62 | OM_uint32 ret_flags = 0; | |
fe4ad273 | 63 | gss_buffer_desc gssbuf, recv_tok, msg_tok; |
64 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | |
b46fa825 | 65 | Gssctxt *ctxt = NULL; |
f472818d | 66 | u_int slen, klen, kout, hashlen; |
67 | u_char *kbuf, *hash; | |
fe4ad273 | 68 | DH *dh; |
69 | int min = -1, max = -1, nbits = -1; | |
70 | BIGNUM *shared_secret = NULL; | |
71 | BIGNUM *dh_client_pub = NULL; | |
72 | int type = 0; | |
23987cb8 | 73 | gss_OID oid; |
f97edba6 | 74 | char *mechs; |
f713db99 | 75 | |
b46fa825 | 76 | /* Initialise GSSAPI */ |
77 | ||
fe4ad273 | 78 | /* If we're rekeying, privsep means that some of the private structures |
79 | * in the GSSAPI code are no longer available. This kludges them back | |
f713db99 | 80 | * into life |
fe4ad273 | 81 | */ |
82 | if (!ssh_gssapi_oid_table_ok()) | |
f97edba6 | 83 | if ((mechs = ssh_gssapi_server_mechanisms())) |
84 | xfree(mechs); | |
fe4ad273 | 85 | |
86 | debug2("%s: Identifying %s", __func__, kex->name); | |
f713db99 | 87 | oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type); |
88 | if (oid == GSS_C_NO_OID) | |
fe4ad273 | 89 | fatal("Unknown gssapi mechanism"); |
90 | ||
91 | debug2("%s: Acquiring credentials", __func__); | |
92 | ||
93 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid)))) { | |
23987cb8 | 94 | kex_gss_send_error(ctxt); |
f713db99 | 95 | fatal("Unable to acquire credentials for the server"); |
96 | } | |
97 | ||
98 | switch (kex->kex_type) { | |
99 | case KEX_GSS_GRP1_SHA1: | |
100 | dh = dh_new_group1(); | |
101 | break; | |
102 | case KEX_GSS_GRP14_SHA1: | |
103 | dh = dh_new_group14(); | |
104 | break; | |
105 | case KEX_GSS_GEX_SHA1: | |
fe4ad273 | 106 | debug("Doing group exchange"); |
107 | packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ); | |
108 | min = packet_get_int(); | |
109 | nbits = packet_get_int(); | |
110 | max = packet_get_int(); | |
111 | min = MAX(DH_GRP_MIN, min); | |
112 | max = MIN(DH_GRP_MAX, max); | |
113 | packet_check_eom(); | |
114 | if (max < min || nbits < min || max < nbits) | |
115 | fatal("GSS_GEX, bad parameters: %d !< %d !< %d", | |
116 | min, nbits, max); | |
117 | dh = PRIVSEP(choose_dh(min, nbits, max)); | |
118 | if (dh == NULL) | |
119 | packet_disconnect("Protocol error: no matching group found"); | |
120 | ||
121 | packet_start(SSH2_MSG_KEXGSS_GROUP); | |
122 | packet_put_bignum2(dh->p); | |
123 | packet_put_bignum2(dh->g); | |
124 | packet_send(); | |
125 | ||
126 | packet_write_wait(); | |
f713db99 | 127 | break; |
128 | default: | |
129 | fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type); | |
fe4ad273 | 130 | } |
f713db99 | 131 | |
fe4ad273 | 132 | dh_gen_key(dh, kex->we_need * 8); |
133 | ||
b46fa825 | 134 | do { |
135 | debug("Wait SSH2_MSG_GSSAPI_INIT"); | |
136 | type = packet_read(); | |
137 | switch(type) { | |
138 | case SSH2_MSG_KEXGSS_INIT: | |
fe4ad273 | 139 | if (dh_client_pub != NULL) |
140 | fatal("Received KEXGSS_INIT after initialising"); | |
141 | recv_tok.value = packet_get_string(&slen); | |
142 | recv_tok.length = slen; | |
143 | ||
144 | if ((dh_client_pub = BN_new()) == NULL) | |
145 | fatal("dh_client_pub == NULL"); | |
146 | ||
147 | packet_get_bignum2(dh_client_pub); | |
148 | ||
149 | /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ | |
b46fa825 | 150 | break; |
151 | case SSH2_MSG_KEXGSS_CONTINUE: | |
fe4ad273 | 152 | recv_tok.value = packet_get_string(&slen); |
153 | recv_tok.length = slen; | |
b46fa825 | 154 | break; |
155 | default: | |
fe4ad273 | 156 | packet_disconnect( |
157 | "Protocol error: didn't expect packet type %d", | |
158 | type); | |
b46fa825 | 159 | } |
b46fa825 | 160 | |
fe4ad273 | 161 | maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, |
162 | &send_tok, &ret_flags)); | |
163 | ||
164 | xfree(recv_tok.value); | |
165 | ||
166 | if (maj_status != GSS_S_COMPLETE && send_tok.length == 0) | |
167 | fatal("Zero length token output when incomplete"); | |
168 | ||
e23e524c | 169 | if (dh_client_pub == NULL) |
170 | fatal("No client public key"); | |
171 | ||
c045675e | 172 | if (maj_status & GSS_S_CONTINUE_NEEDED) { |
b46fa825 | 173 | debug("Sending GSSAPI_CONTINUE"); |
174 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | |
c7931c9a | 175 | packet_put_string((char *)send_tok.value, send_tok.length); |
b46fa825 | 176 | packet_send(); |
b46fa825 | 177 | gss_release_buffer(&min_status, &send_tok); |
178 | } | |
179 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | |
180 | ||
181 | if (GSS_ERROR(maj_status)) { | |
23987cb8 | 182 | kex_gss_send_error(ctxt); |
fe4ad273 | 183 | if (send_tok.length > 0) { |
e23e524c | 184 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); |
c7931c9a | 185 | packet_put_string((char *)send_tok.value, send_tok.length); |
e23e524c | 186 | packet_send(); |
fe4ad273 | 187 | } |
188 | packet_disconnect("GSSAPI Key Exchange handshake failed"); | |
b46fa825 | 189 | } |
fe4ad273 | 190 | |
23987cb8 | 191 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) |
fe4ad273 | 192 | fatal("Mutual Authentication flag wasn't set"); |
193 | ||
23987cb8 | 194 | if (!(ret_flags & GSS_C_INTEG_FLAG)) |
fe4ad273 | 195 | fatal("Integrity flag wasn't set"); |
b46fa825 | 196 | |
fe4ad273 | 197 | if (!dh_pub_is_valid(dh, dh_client_pub)) |
198 | packet_disconnect("bad client public DH value"); | |
b46fa825 | 199 | |
fe4ad273 | 200 | klen = DH_size(dh); |
201 | kbuf = xmalloc(klen); | |
202 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | |
f97edba6 | 203 | if (kout < 0) |
204 | fatal("DH_compute_key: failed"); | |
b46fa825 | 205 | |
206 | shared_secret = BN_new(); | |
f97edba6 | 207 | if (shared_secret == NULL) |
208 | fatal("kexgss_server: BN_new failed"); | |
209 | ||
210 | if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) | |
211 | fatal("kexgss_server: BN_bin2bn failed"); | |
212 | ||
b46fa825 | 213 | memset(kbuf, 0, klen); |
214 | xfree(kbuf); | |
fe4ad273 | 215 | |
f713db99 | 216 | switch (kex->kex_type) { |
217 | case KEX_GSS_GRP1_SHA1: | |
218 | case KEX_GSS_GRP14_SHA1: | |
219 | kex_dh_hash( | |
220 | kex->client_version_string, kex->server_version_string, | |
221 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
222 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
223 | NULL, 0, /* Change this if we start sending host keys */ | |
224 | dh_client_pub, dh->pub_key, shared_secret, | |
225 | &hash, &hashlen | |
226 | ); | |
227 | break; | |
228 | case KEX_GSS_GEX_SHA1: | |
08822d99 | 229 | kexgex_hash( |
230 | kex->evp_md, | |
fe4ad273 | 231 | kex->client_version_string, kex->server_version_string, |
232 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
233 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
234 | NULL, 0, | |
235 | min, nbits, max, | |
236 | dh->p, dh->g, | |
237 | dh_client_pub, | |
238 | dh->pub_key, | |
08822d99 | 239 | shared_secret, |
240 | &hash, &hashlen | |
fe4ad273 | 241 | ); |
f713db99 | 242 | break; |
243 | default: | |
244 | fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type); | |
fe4ad273 | 245 | } |
f713db99 | 246 | |
f97edba6 | 247 | BN_clear_free(dh_client_pub); |
fe4ad273 | 248 | |
b46fa825 | 249 | if (kex->session_id == NULL) { |
d33fd7cd | 250 | kex->session_id_len = hashlen; |
b46fa825 | 251 | kex->session_id = xmalloc(kex->session_id_len); |
252 | memcpy(kex->session_id, hash, kex->session_id_len); | |
253 | } | |
fe4ad273 | 254 | |
b46fa825 | 255 | gssbuf.value = hash; |
d33fd7cd | 256 | gssbuf.length = hashlen; |
fe4ad273 | 257 | |
258 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok)))) | |
23987cb8 | 259 | fatal("Couldn't get MIC"); |
fe4ad273 | 260 | |
b46fa825 | 261 | packet_start(SSH2_MSG_KEXGSS_COMPLETE); |
262 | packet_put_bignum2(dh->pub_key); | |
263 | packet_put_string((char *)msg_tok.value,msg_tok.length); | |
264 | ||
fe4ad273 | 265 | if (send_tok.length != 0) { |
b46fa825 | 266 | packet_put_char(1); /* true */ |
fe4ad273 | 267 | packet_put_string((char *)send_tok.value, send_tok.length); |
b46fa825 | 268 | } else { |
269 | packet_put_char(0); /* false */ | |
270 | } | |
fe4ad273 | 271 | packet_send(); |
272 | ||
273 | gss_release_buffer(&min_status, &send_tok); | |
274 | gss_release_buffer(&min_status, &msg_tok); | |
275 | ||
276 | if (gss_kex_context == NULL) | |
277 | gss_kex_context = ctxt; | |
278 | else | |
279 | ssh_gssapi_delete_ctx(&ctxt); | |
280 | ||
b46fa825 | 281 | DH_free(dh); |
282 | ||
08822d99 | 283 | kex_derive_keys(kex, hash, hashlen, shared_secret); |
b46fa825 | 284 | BN_clear_free(shared_secret); |
285 | kex_finish(kex); | |
f97edba6 | 286 | |
287 | /* If this was a rekey, then save out any delegated credentials we | |
288 | * just exchanged. */ | |
289 | if (options.gss_store_rekey) | |
290 | ssh_gssapi_rekey_creds(); | |
b46fa825 | 291 | } |
292 | ||
23987cb8 | 293 | static void |
294 | kex_gss_send_error(Gssctxt *ctxt) { | |
295 | char *errstr; | |
296 | OM_uint32 maj,min; | |
297 | ||
298 | errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min)); | |
299 | if (errstr) { | |
300 | packet_start(SSH2_MSG_KEXGSS_ERROR); | |
301 | packet_put_int(maj); | |
302 | packet_put_int(min); | |
303 | packet_put_cstring(errstr); | |
304 | packet_put_cstring(""); | |
305 | packet_send(); | |
306 | packet_write_wait(); | |
307 | /* XXX - We should probably log the error locally here */ | |
308 | xfree(errstr); | |
309 | } | |
310 | } | |
b46fa825 | 311 | #endif /* GSSAPI */ |