]>
Commit | Line | Data |
---|---|---|
5598e598 | 1 | /* |
905081a4 | 2 | * Copyright (c) 2001,2002 Simon Wilkinson. All rights reserved. |
5598e598 | 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 | ||
29 | #include <openssl/crypto.h> | |
30 | #include <openssl/bn.h> | |
31 | ||
32 | #include "xmalloc.h" | |
33 | #include "buffer.h" | |
34 | #include "bufaux.h" | |
35 | #include "kex.h" | |
36 | #include "log.h" | |
37 | #include "packet.h" | |
38 | #include "dh.h" | |
39 | #include "ssh2.h" | |
40 | #include "ssh-gss.h" | |
b59afbfe | 41 | #include "monitor_wrap.h" |
5598e598 | 42 | |
43 | /* This is now the same as the DH hash ... */ | |
44 | ||
45 | u_char * | |
46 | kex_gssapi_hash( | |
47 | char *client_version_string, | |
48 | char *server_version_string, | |
49 | char *ckexinit, int ckexinitlen, | |
50 | char *skexinit, int skexinitlen, | |
51 | u_char *serverhostkeyblob, int sbloblen, | |
52 | BIGNUM *client_dh_pub, | |
53 | BIGNUM *server_dh_pub, | |
54 | BIGNUM *shared_secret) | |
55 | { | |
56 | Buffer b; | |
57 | static u_char digest[EVP_MAX_MD_SIZE]; | |
58 | EVP_MD *evp_md = EVP_sha1(); | |
59 | EVP_MD_CTX md; | |
60 | ||
61 | buffer_init(&b); | |
62 | buffer_put_string(&b, client_version_string, strlen(client_version_string)); | |
63 | buffer_put_string(&b, server_version_string, strlen(server_version_string)); | |
64 | ||
65 | /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ | |
66 | buffer_put_int(&b, ckexinitlen+1); | |
67 | buffer_put_char(&b, SSH2_MSG_KEXINIT); | |
68 | buffer_append(&b, ckexinit, ckexinitlen); | |
69 | buffer_put_int(&b, skexinitlen+1); | |
70 | buffer_put_char(&b, SSH2_MSG_KEXINIT); | |
71 | buffer_append(&b, skexinit, skexinitlen); | |
72 | ||
73 | buffer_put_string(&b, serverhostkeyblob, sbloblen); | |
74 | buffer_put_bignum2(&b, client_dh_pub); | |
75 | buffer_put_bignum2(&b, server_dh_pub); | |
76 | buffer_put_bignum2(&b, shared_secret); | |
77 | ||
78 | #ifdef DEBUG_KEX | |
79 | buffer_dump(&b); | |
80 | #endif | |
81 | EVP_DigestInit(&md, evp_md); | |
82 | EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); | |
83 | EVP_DigestFinal(&md, digest, NULL); | |
84 | ||
85 | buffer_free(&b); | |
86 | ||
87 | #ifdef DEBUG_KEX | |
88 | dump_digest("hash", digest, evp_md->md_size); | |
89 | #endif | |
90 | return digest; | |
91 | } | |
92 | ||
93 | void | |
94 | kexgss_client(Kex *kex) | |
95 | { | |
96 | gss_buffer_desc gssbuf,send_tok,recv_tok, msg_tok, *token_ptr; | |
b59afbfe | 97 | Gssctxt *ctxt; |
5598e598 | 98 | OM_uint32 maj_status, min_status, ret_flags; |
5598e598 | 99 | unsigned int klen, kout; |
100 | DH *dh; | |
101 | BIGNUM *dh_server_pub = 0; | |
102 | BIGNUM *shared_secret = 0; | |
103 | unsigned char *kbuf; | |
104 | unsigned char *hash; | |
105 | unsigned char *serverhostkey; | |
106 | int type = 0; | |
107 | int first = 1; | |
108 | int slen = 0; | |
109 | ||
110 | /* Initialise our GSSAPI world */ | |
111 | ssh_gssapi_build_ctx(&ctxt); | |
b59afbfe | 112 | if (ssh_gssapi_id_kex(ctxt,kex->name)==NULL) { |
5598e598 | 113 | fatal("Couldn't identify host exchange"); |
114 | } | |
b59afbfe | 115 | if (ssh_gssapi_import_name(ctxt,kex->host)) { |
5598e598 | 116 | fatal("Couldn't import hostname "); |
117 | } | |
118 | ||
119 | /* This code should match that in ssh_dh1_client */ | |
120 | ||
121 | /* Step 1 - e is dh->pub_key */ | |
122 | dh = dh_new_group1(); | |
123 | dh_gen_key(dh, kex->we_need * 8); | |
124 | ||
125 | /* This is f, we initialise it now to make life easier */ | |
126 | dh_server_pub = BN_new(); | |
127 | if (dh_server_pub == NULL) { | |
128 | fatal("dh_server_pub == NULL"); | |
129 | } | |
130 | ||
131 | token_ptr = GSS_C_NO_BUFFER; | |
132 | ||
133 | do { | |
134 | debug("Calling gss_init_sec_context"); | |
135 | ||
b59afbfe | 136 | maj_status=ssh_gssapi_init_ctx(ctxt, |
5598e598 | 137 | kex->options.gss_deleg_creds, |
138 | token_ptr,&send_tok, | |
139 | &ret_flags); | |
140 | ||
141 | if (GSS_ERROR(maj_status)) { | |
142 | fatal("gss_init_context failed"); | |
143 | } | |
144 | ||
145 | /* If we've got an old receive buffer get rid of it */ | |
146 | if (token_ptr != GSS_C_NO_BUFFER) | |
147 | (void) gss_release_buffer(&min_status, &recv_tok); | |
148 | ||
149 | ||
150 | if (maj_status == GSS_S_COMPLETE) { | |
151 | /* If mutual state flag is not true, kex fails */ | |
152 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) { | |
153 | fatal("Mutual authentication failed"); | |
154 | } | |
155 | /* If integ avail flag is not true kex fails */ | |
156 | if (!(ret_flags & GSS_C_INTEG_FLAG)) { | |
157 | fatal("Integrity check failed"); | |
158 | } | |
159 | } | |
160 | ||
161 | /* If we have data to send, then the last message that we | |
162 | * received cannot have been a 'complete'. */ | |
163 | if (send_tok.length !=0) { | |
164 | if (first) { | |
165 | packet_start(SSH2_MSG_KEXGSS_INIT); | |
166 | packet_put_string(send_tok.value, | |
167 | send_tok.length); | |
168 | packet_put_bignum2(dh->pub_key); | |
169 | first=0; | |
170 | } else { | |
171 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | |
172 | packet_put_string(send_tok.value, | |
173 | send_tok.length); | |
174 | } | |
175 | packet_send(); | |
176 | packet_write_wait(); | |
177 | ||
178 | ||
179 | /* If we've sent them data, they'd better be polite | |
180 | * and reply. */ | |
181 | ||
6d25b646 | 182 | type = packet_read(); |
5598e598 | 183 | switch (type) { |
184 | case SSH2_MSG_KEXGSS_HOSTKEY: | |
185 | debug("Received KEXGSS_HOSTKEY"); | |
186 | serverhostkey=packet_get_string(&slen); | |
187 | break; | |
188 | case SSH2_MSG_KEXGSS_CONTINUE: | |
189 | debug("Received GSSAPI_CONTINUE"); | |
190 | if (maj_status == GSS_S_COMPLETE) | |
191 | fatal("GSSAPI Continue received from server when complete"); | |
192 | recv_tok.value=packet_get_string(&recv_tok.length); | |
193 | break; | |
194 | case SSH2_MSG_KEXGSS_COMPLETE: | |
195 | debug("Received GSSAPI_COMPLETE"); | |
6d25b646 | 196 | packet_get_bignum2(dh_server_pub); |
5598e598 | 197 | msg_tok.value= |
198 | packet_get_string(&msg_tok.length); | |
199 | ||
200 | /* Is there a token included? */ | |
201 | if (packet_get_char()) { | |
202 | recv_tok.value= | |
203 | packet_get_string(&recv_tok.length); | |
204 | /* If we're already complete - protocol error */ | |
205 | if (maj_status == GSS_S_COMPLETE) | |
206 | packet_disconnect("Protocol error: received token when complete"); | |
207 | } else { | |
208 | /* No token included */ | |
209 | if (maj_status != GSS_S_COMPLETE) | |
210 | packet_disconnect("Protocol error: did not receive final token"); | |
211 | } | |
212 | break; | |
213 | default: | |
214 | packet_disconnect("Protocol error: didn't expect packet type %d", | |
215 | type); | |
216 | } | |
217 | token_ptr=&recv_tok; | |
218 | } | |
219 | ||
220 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | |
221 | ||
222 | /* We _must_ have received a COMPLETE message in reply from the | |
223 | * server, which will have set dh_server_pub and msg_tok */ | |
224 | ||
225 | if (type!=SSH2_MSG_KEXGSS_COMPLETE) | |
226 | fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it"); | |
227 | ||
228 | /* Check f in range [1, p-1] */ | |
229 | if (!dh_pub_is_valid(dh, dh_server_pub)) | |
230 | packet_disconnect("bad server public DH value"); | |
231 | ||
232 | /* compute K=f^x mod p */ | |
233 | klen = DH_size(dh); | |
234 | kbuf = xmalloc(klen); | |
235 | kout = DH_compute_key(kbuf, dh_server_pub, dh); | |
236 | ||
237 | shared_secret = BN_new(); | |
238 | BN_bin2bn(kbuf,kout, shared_secret); | |
239 | memset(kbuf, 0, klen); | |
240 | xfree(kbuf); | |
241 | ||
242 | hash = kex_gssapi_hash( | |
243 | kex->client_version_string, | |
244 | kex->server_version_string, | |
245 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
246 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
247 | serverhostkey, slen, /* server host key */ | |
248 | dh->pub_key, /* e */ | |
249 | dh_server_pub, /* f */ | |
250 | shared_secret /* K */ | |
251 | ); | |
252 | ||
253 | gssbuf.value=hash; | |
254 | gssbuf.length=20; | |
255 | ||
256 | /* Verify that H matches the token we just got. */ | |
257 | if ((maj_status = gss_verify_mic(&min_status, | |
b59afbfe | 258 | ctxt->context, |
5598e598 | 259 | &gssbuf, |
260 | &msg_tok, | |
261 | NULL))) { | |
262 | ||
263 | packet_disconnect("Hash's MIC didn't verify"); | |
264 | } | |
265 | ||
266 | DH_free(dh); | |
267 | ssh_gssapi_delete_ctx(&ctxt); | |
268 | /* save session id */ | |
269 | if (kex->session_id == NULL) { | |
270 | kex->session_id_len = 20; | |
271 | kex->session_id = xmalloc(kex->session_id_len); | |
272 | memcpy(kex->session_id, hash, kex->session_id_len); | |
273 | } | |
274 | ||
275 | kex_derive_keys(kex, hash, shared_secret); | |
276 | BN_clear_free(shared_secret); | |
277 | kex_finish(kex); | |
278 | } | |
279 | ||
280 | ||
281 | ||
282 | ||
283 | void | |
284 | kexgss_server(Kex *kex) | |
285 | { | |
286 | ||
287 | OM_uint32 maj_status, min_status; | |
288 | ||
289 | /* Some GSSAPI implementations use the input value of ret_flags (an | |
290 | * output variable) as a means of triggering mechanism specific | |
291 | * features. Initializing it to zero avoids inadvertently | |
292 | * activating this non-standard behaviour.*/ | |
293 | ||
294 | OM_uint32 ret_flags = 0; | |
295 | gss_buffer_desc gssbuf,send_tok,recv_tok,msg_tok; | |
b59afbfe | 296 | Gssctxt *ctxt = NULL; |
5598e598 | 297 | unsigned int klen, kout; |
298 | unsigned char *kbuf; | |
299 | unsigned char *hash; | |
300 | DH *dh; | |
905081a4 | 301 | BIGNUM *shared_secret = NULL; |
302 | BIGNUM *dh_client_pub = NULL; | |
5598e598 | 303 | int type =0; |
b59afbfe | 304 | gss_OID oid; |
5598e598 | 305 | |
306 | /* Initialise GSSAPI */ | |
307 | ||
b59afbfe | 308 | debug2("%s: Identifying %s",__func__,kex->name); |
309 | oid=ssh_gssapi_id_kex(ctxt,kex->name); | |
310 | if (oid==NULL) { | |
311 | packet_disconnect("Unknown gssapi mechanism"); | |
312 | } | |
313 | ||
314 | debug2("%s: Acquiring credentials",__func__); | |
315 | ||
316 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt,oid)))) | |
317 | packet_disconnect("Unable to acquire credentials for the server"); | |
5598e598 | 318 | |
5598e598 | 319 | do { |
320 | debug("Wait SSH2_MSG_GSSAPI_INIT"); | |
6d25b646 | 321 | type = packet_read(); |
5598e598 | 322 | switch(type) { |
323 | case SSH2_MSG_KEXGSS_INIT: | |
905081a4 | 324 | if (dh_client_pub!=NULL) |
de4d4506 | 325 | packet_disconnect("Received KEXGSS_INIT after initialising"); |
5598e598 | 326 | recv_tok.value=packet_get_string(&recv_tok.length); |
905081a4 | 327 | |
328 | dh_client_pub = BN_new(); | |
329 | ||
330 | if (dh_client_pub == NULL) | |
331 | fatal("dh_client_pub == NULL"); | |
6d25b646 | 332 | packet_get_bignum2(dh_client_pub); |
905081a4 | 333 | |
5598e598 | 334 | /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ |
335 | break; | |
336 | case SSH2_MSG_KEXGSS_CONTINUE: | |
905081a4 | 337 | if (dh_client_pub == NULL) |
de4d4506 | 338 | packet_disconnect("Received KEXGSS_CONTINUE without initialising"); |
5598e598 | 339 | recv_tok.value=packet_get_string(&recv_tok.length); |
340 | break; | |
341 | default: | |
342 | packet_disconnect("Protocol error: didn't expect packet type %d", | |
343 | type); | |
344 | } | |
b59afbfe | 345 | |
346 | maj_status=PRIVSEP(ssh_gssapi_accept_ctx(ctxt,&recv_tok, | |
347 | &send_tok, &ret_flags)); | |
5598e598 | 348 | |
349 | gss_release_buffer(&min_status,&recv_tok); | |
3d9d1aaf | 350 | |
351 | #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG | |
352 | if (ret_flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG) { | |
de4d4506 | 353 | packet_disconnect("Limited proxy is not allowed in gssapi key exchange."); |
3d9d1aaf | 354 | } |
355 | #endif | |
5598e598 | 356 | |
357 | if (maj_status & GSS_S_CONTINUE_NEEDED) { | |
358 | debug("Sending GSSAPI_CONTINUE"); | |
359 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | |
360 | packet_put_string(send_tok.value,send_tok.length); | |
361 | packet_send(); | |
362 | packet_write_wait(); | |
363 | gss_release_buffer(&min_status, &send_tok); | |
364 | } | |
365 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | |
366 | ||
de4d4506 | 367 | if (GSS_ERROR(maj_status)) { |
368 | ssh_gssapi_send_error(maj_status,min_status); | |
369 | packet_disconnect("gssapi key exchange handshake failed"); | |
370 | } | |
371 | ||
5598e598 | 372 | debug("gss_complete"); |
de4d4506 | 373 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) { |
374 | ssh_gssapi_send_error(maj_status,min_status); | |
375 | packet_disconnect("gssapi mutual authentication failed"); | |
376 | } | |
5598e598 | 377 | |
de4d4506 | 378 | if (!(ret_flags & GSS_C_INTEG_FLAG)) { |
379 | ssh_gssapi_send_error(maj_status,min_status); | |
380 | packet_disconnect("gssapi channel integrity not established"); | |
381 | } | |
5598e598 | 382 | |
383 | dh = dh_new_group1(); | |
384 | dh_gen_key(dh, kex->we_need * 8); | |
385 | ||
386 | if (!dh_pub_is_valid(dh, dh_client_pub)) | |
387 | packet_disconnect("bad client public DH value"); | |
388 | ||
389 | klen = DH_size(dh); | |
390 | kbuf = xmalloc(klen); | |
391 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | |
392 | ||
393 | shared_secret = BN_new(); | |
394 | BN_bin2bn(kbuf, kout, shared_secret); | |
395 | memset(kbuf, 0, klen); | |
396 | xfree(kbuf); | |
397 | ||
398 | hash = kex_gssapi_hash( | |
399 | kex->client_version_string, | |
400 | kex->server_version_string, | |
401 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
402 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
403 | NULL, 0, /* Change this if we start sending host keys */ | |
404 | dh_client_pub, | |
405 | dh->pub_key, | |
406 | shared_secret | |
407 | ); | |
408 | BN_free(dh_client_pub); | |
409 | ||
410 | if (kex->session_id == NULL) { | |
411 | kex->session_id_len = 20; | |
412 | kex->session_id = xmalloc(kex->session_id_len); | |
413 | memcpy(kex->session_id, hash, kex->session_id_len); | |
414 | } | |
415 | ||
416 | gssbuf.value = hash; | |
417 | gssbuf.length = 20; /* Hashlen appears to always be 20 */ | |
418 | ||
b59afbfe | 419 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok)))) { |
de4d4506 | 420 | ssh_gssapi_send_error(maj_status,min_status); |
421 | packet_disconnect("Couldn't get MIC"); | |
b59afbfe | 422 | } |
423 | ||
5598e598 | 424 | packet_start(SSH2_MSG_KEXGSS_COMPLETE); |
425 | packet_put_bignum2(dh->pub_key); | |
426 | packet_put_string((char *)msg_tok.value,msg_tok.length); | |
427 | ||
428 | if (send_tok.length!=0) { | |
429 | packet_put_char(1); /* true */ | |
430 | packet_put_string((char *)send_tok.value,send_tok.length); | |
431 | } else { | |
432 | packet_put_char(0); /* false */ | |
433 | } | |
434 | packet_send(); | |
435 | packet_write_wait(); | |
436 | ||
b59afbfe | 437 | /* We used to store the client name and credentials here for later |
438 | * use. With privsep, its easier to do this as a by product of the | |
439 | * call to accept_context, which stores delegated information when | |
440 | * the context is complete */ | |
441 | ||
5598e598 | 442 | gss_release_buffer(&min_status, &send_tok); |
b59afbfe | 443 | |
444 | /* If we've got a context, delete it. It may be NULL if we've been | |
445 | * using privsep */ | |
5598e598 | 446 | ssh_gssapi_delete_ctx(&ctxt); |
b59afbfe | 447 | |
5598e598 | 448 | DH_free(dh); |
449 | ||
450 | kex_derive_keys(kex, hash, shared_secret); | |
451 | BN_clear_free(shared_secret); | |
452 | kex_finish(kex); | |
453 | } | |
454 | ||
455 | void | |
456 | kexgss(Kex *kex) | |
457 | { | |
458 | if (kex->server) | |
459 | kexgss_server(kex); | |
460 | else | |
461 | kexgss_client(kex); | |
462 | } | |
463 | ||
464 | #endif /* GSSAPI */ |