]> andersk Git - gssapi-openssh.git/blame - openssh/sshconnect1.c
added type cast to fix compiler warning
[gssapi-openssh.git] / openssh / sshconnect1.c
CommitLineData
3c0ef626 1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
7 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 */
14
15#include "includes.h"
44a053a3 16RCSID("$OpenBSD: sshconnect1.c,v 1.51 2002/05/23 19:24:30 markus Exp $");
3c0ef626 17
18#include <openssl/bn.h>
1e608e42 19#include <openssl/md5.h>
3c0ef626 20
21#ifdef KRB4
22#include <krb.h>
23#endif
24#ifdef KRB5
25#include <krb5.h>
63119dd9 26#ifndef HEIMDAL
27#define krb5_get_err_text(context,code) error_message(code)
28#endif /* !HEIMDAL */
3c0ef626 29#endif
30#ifdef AFS
31#include <kafs.h>
32#include "radix.h"
33#endif
34
35#include "ssh.h"
36#include "ssh1.h"
37#include "xmalloc.h"
38#include "rsa.h"
39#include "buffer.h"
40#include "packet.h"
41#include "mpaux.h"
42#include "uidswap.h"
43#include "log.h"
44#include "readconf.h"
45#include "key.h"
46#include "authfd.h"
47#include "sshconnect.h"
48#include "authfile.h"
49#include "readpass.h"
50#include "cipher.h"
51#include "canohost.h"
52#include "auth.h"
53
0b582e46 54#ifdef GSSAPI
3f726215 55#include "ssh-gss.h"
0b582e46 56#include "bufaux.h"
1aad58f1 57
0b582e46 58/*
59 * MD5 hash of host and session keys for verification. This is filled
60 * in in ssh_login() and then checked in try_gssapi_authentication().
61 */
62unsigned char ssh_key_digest[16];
63#endif /* GSSAPI */
0b582e46 64
3c0ef626 65/* Session id for the current session. */
66u_char session_id[16];
67u_int supported_authentications = 0;
68
69extern Options options;
70extern char *__progname;
71
72/*
73 * Checks if the user has an authentication agent, and if so, tries to
74 * authenticate using the agent.
75 */
76static int
77try_agent_authentication(void)
78{
79 int type;
80 char *comment;
81 AuthenticationConnection *auth;
82 u_char response[16];
83 u_int i;
3c0ef626 84 Key *key;
85 BIGNUM *challenge;
86
87 /* Get connection to the agent. */
88 auth = ssh_get_authentication_connection();
89 if (!auth)
90 return 0;
91
1e608e42 92 if ((challenge = BN_new()) == NULL)
93 fatal("try_agent_authentication: BN_new failed");
3c0ef626 94 /* Loop through identities served by the agent. */
95 for (key = ssh_get_first_identity(auth, &comment, 1);
1e608e42 96 key != NULL;
97 key = ssh_get_next_identity(auth, &comment, 1)) {
3c0ef626 98
99 /* Try this identity. */
100 debug("Trying RSA authentication via agent with '%.100s'", comment);
101 xfree(comment);
102
103 /* Tell the server that we are willing to authenticate using this key. */
104 packet_start(SSH_CMSG_AUTH_RSA);
105 packet_put_bignum(key->rsa->n);
106 packet_send();
107 packet_write_wait();
108
109 /* Wait for server's response. */
1e608e42 110 type = packet_read();
3c0ef626 111
112 /* The server sends failure if it doesn\'t like our key or
113 does not support RSA authentication. */
114 if (type == SSH_SMSG_FAILURE) {
115 debug("Server refused our key.");
116 key_free(key);
117 continue;
118 }
119 /* Otherwise it should have sent a challenge. */
120 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
121 packet_disconnect("Protocol error during RSA authentication: %d",
122 type);
123
1e608e42 124 packet_get_bignum(challenge);
125 packet_check_eom();
3c0ef626 126
127 debug("Received RSA challenge from server.");
128
129 /* Ask the agent to decrypt the challenge. */
130 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
131 /*
132 * The agent failed to authenticate this identifier
133 * although it advertised it supports this. Just
134 * return a wrong value.
135 */
136 log("Authentication agent failed to decrypt challenge.");
137 memset(response, 0, sizeof(response));
138 }
139 key_free(key);
140 debug("Sending response to RSA challenge.");
141
142 /* Send the decrypted challenge back to the server. */
143 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
144 for (i = 0; i < 16; i++)
145 packet_put_char(response[i]);
146 packet_send();
147 packet_write_wait();
148
149 /* Wait for response from the server. */
1e608e42 150 type = packet_read();
3c0ef626 151
152 /* The server returns success if it accepted the authentication. */
153 if (type == SSH_SMSG_SUCCESS) {
154 ssh_close_authentication_connection(auth);
155 BN_clear_free(challenge);
156 debug("RSA authentication accepted by server.");
157 return 1;
158 }
159 /* Otherwise it should return failure. */
160 if (type != SSH_SMSG_FAILURE)
161 packet_disconnect("Protocol error waiting RSA auth response: %d",
162 type);
163 }
164 ssh_close_authentication_connection(auth);
165 BN_clear_free(challenge);
166 debug("RSA authentication using agent refused.");
167 return 0;
168}
169
170/*
171 * Computes the proper response to a RSA challenge, and sends the response to
172 * the server.
173 */
174static void
175respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
176{
177 u_char buf[32], response[16];
178 MD5_CTX md;
179 int i, len;
180
181 /* Decrypt the challenge using the private key. */
182 /* XXX think about Bleichenbacher, too */
183 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
184 packet_disconnect(
185 "respond_to_rsa_challenge: rsa_private_decrypt failed");
186
187 /* Compute the response. */
188 /* The response is MD5 of decrypted challenge plus session id. */
189 len = BN_num_bytes(challenge);
190 if (len <= 0 || len > sizeof(buf))
191 packet_disconnect(
192 "respond_to_rsa_challenge: bad challenge length %d", len);
193
194 memset(buf, 0, sizeof(buf));
195 BN_bn2bin(challenge, buf + sizeof(buf) - len);
196 MD5_Init(&md);
197 MD5_Update(&md, buf, 32);
198 MD5_Update(&md, session_id, 16);
199 MD5_Final(response, &md);
200
201 debug("Sending response to host key RSA challenge.");
202
203 /* Send the response back to the server. */
204 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
205 for (i = 0; i < 16; i++)
206 packet_put_char(response[i]);
207 packet_send();
208 packet_write_wait();
209
210 memset(buf, 0, sizeof(buf));
211 memset(response, 0, sizeof(response));
212 memset(&md, 0, sizeof(md));
213}
214
215/*
216 * Checks if the user has authentication file, and if so, tries to authenticate
217 * the user using it.
218 */
219static int
220try_rsa_authentication(int idx)
221{
222 BIGNUM *challenge;
223 Key *public, *private;
224 char buf[300], *passphrase, *comment, *authfile;
1e608e42 225 int i, type, quit;
3c0ef626 226
227 public = options.identity_keys[idx];
228 authfile = options.identity_files[idx];
229 comment = xstrdup(authfile);
230
231 debug("Trying RSA authentication with key '%.100s'", comment);
232
233 /* Tell the server that we are willing to authenticate using this key. */
234 packet_start(SSH_CMSG_AUTH_RSA);
235 packet_put_bignum(public->rsa->n);
236 packet_send();
237 packet_write_wait();
238
239 /* Wait for server's response. */
1e608e42 240 type = packet_read();
3c0ef626 241
242 /*
243 * The server responds with failure if it doesn\'t like our key or
244 * doesn\'t support RSA authentication.
245 */
246 if (type == SSH_SMSG_FAILURE) {
247 debug("Server refused our key.");
248 xfree(comment);
249 return 0;
250 }
251 /* Otherwise, the server should respond with a challenge. */
252 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
253 packet_disconnect("Protocol error during RSA authentication: %d", type);
254
255 /* Get the challenge from the packet. */
1e608e42 256 if ((challenge = BN_new()) == NULL)
257 fatal("try_rsa_authentication: BN_new failed");
258 packet_get_bignum(challenge);
259 packet_check_eom();
3c0ef626 260
261 debug("Received RSA challenge from server.");
262
263 /*
264 * If the key is not stored in external hardware, we have to
265 * load the private key. Try first with empty passphrase; if it
266 * fails, ask for a passphrase.
267 */
268 if (public->flags && KEY_FLAG_EXT)
269 private = public;
270 else
271 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
272 if (private == NULL && !options.batch_mode) {
273 snprintf(buf, sizeof(buf),
274 "Enter passphrase for RSA key '%.100s': ", comment);
275 for (i = 0; i < options.number_of_password_prompts; i++) {
276 passphrase = read_passphrase(buf, 0);
277 if (strcmp(passphrase, "") != 0) {
278 private = key_load_private_type(KEY_RSA1,
279 authfile, passphrase, NULL);
280 quit = 0;
281 } else {
282 debug2("no passphrase given, try next key");
283 quit = 1;
284 }
285 memset(passphrase, 0, strlen(passphrase));
286 xfree(passphrase);
287 if (private != NULL || quit)
288 break;
289 debug2("bad passphrase given, try again...");
290 }
291 }
292 /* We no longer need the comment. */
293 xfree(comment);
294
295 if (private == NULL) {
296 if (!options.batch_mode)
297 error("Bad passphrase.");
298
299 /* Send a dummy response packet to avoid protocol error. */
300 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
301 for (i = 0; i < 16; i++)
302 packet_put_char(0);
303 packet_send();
304 packet_write_wait();
305
306 /* Expect the server to reject it... */
1e608e42 307 packet_read_expect(SSH_SMSG_FAILURE);
3c0ef626 308 BN_clear_free(challenge);
309 return 0;
310 }
311
312 /* Compute and send a response to the challenge. */
313 respond_to_rsa_challenge(challenge, private->rsa);
314
315 /* Destroy the private key unless it in external hardware. */
316 if (!(private->flags & KEY_FLAG_EXT))
317 key_free(private);
318
319 /* We no longer need the challenge. */
320 BN_clear_free(challenge);
321
322 /* Wait for response from the server. */
1e608e42 323 type = packet_read();
3c0ef626 324 if (type == SSH_SMSG_SUCCESS) {
325 debug("RSA authentication accepted by server.");
326 return 1;
327 }
328 if (type != SSH_SMSG_FAILURE)
329 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
330 debug("RSA authentication refused.");
331 return 0;
332}
333
334/*
335 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
336 * authentication and RSA host authentication.
337 */
338static int
339try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
340{
341 int type;
342 BIGNUM *challenge;
3c0ef626 343
344 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
345
346 /* Tell the server that we are willing to authenticate using this key. */
347 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
348 packet_put_cstring(local_user);
349 packet_put_int(BN_num_bits(host_key->rsa->n));
350 packet_put_bignum(host_key->rsa->e);
351 packet_put_bignum(host_key->rsa->n);
352 packet_send();
353 packet_write_wait();
354
355 /* Wait for server's response. */
1e608e42 356 type = packet_read();
3c0ef626 357
358 /* The server responds with failure if it doesn't admit our
359 .rhosts authentication or doesn't know our host key. */
360 if (type == SSH_SMSG_FAILURE) {
361 debug("Server refused our rhosts authentication or host key.");
362 return 0;
363 }
364 /* Otherwise, the server should respond with a challenge. */
365 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
366 packet_disconnect("Protocol error during RSA authentication: %d", type);
367
368 /* Get the challenge from the packet. */
1e608e42 369 if ((challenge = BN_new()) == NULL)
370 fatal("try_rhosts_rsa_authentication: BN_new failed");
371 packet_get_bignum(challenge);
372 packet_check_eom();
3c0ef626 373
374 debug("Received RSA challenge for host key from server.");
375
376 /* Compute a response to the challenge. */
377 respond_to_rsa_challenge(challenge, host_key->rsa);
378
379 /* We no longer need the challenge. */
380 BN_clear_free(challenge);
381
382 /* Wait for response from the server. */
1e608e42 383 type = packet_read();
3c0ef626 384 if (type == SSH_SMSG_SUCCESS) {
385 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
386 return 1;
387 }
388 if (type != SSH_SMSG_FAILURE)
389 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
390 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
391 return 0;
392}
393
394#ifdef KRB4
395static int
396try_krb4_authentication(void)
397{
398 KTEXT_ST auth; /* Kerberos data */
399 char *reply;
400 char inst[INST_SZ];
401 char *realm;
402 CREDENTIALS cred;
1e608e42 403 int r, type;
3c0ef626 404 socklen_t slen;
405 Key_schedule schedule;
406 u_long checksum, cksum;
407 MSG_DAT msg_data;
408 struct sockaddr_in local, foreign;
409 struct stat st;
410
411 /* Don't do anything if we don't have any tickets. */
412 if (stat(tkt_string(), &st) < 0)
413 return 0;
1e608e42 414
3c0ef626 415 strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)),
416 INST_SZ);
1e608e42 417
3c0ef626 418 realm = (char *)krb_realmofhost(get_canonical_hostname(1));
419 if (!realm) {
420 debug("Kerberos v4: no realm for %s", get_canonical_hostname(1));
421 return 0;
422 }
423 /* This can really be anything. */
424 checksum = (u_long)getpid();
1e608e42 425
3c0ef626 426 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
427 if (r != KSUCCESS) {
428 debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]);
429 return 0;
430 }
431 /* Get session key to decrypt the server's reply with. */
432 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
433 if (r != KSUCCESS) {
434 debug("get_cred failed: %s", krb_err_txt[r]);
435 return 0;
436 }
437 des_key_sched((des_cblock *) cred.session, schedule);
1e608e42 438
3c0ef626 439 /* Send authentication info to server. */
440 packet_start(SSH_CMSG_AUTH_KERBEROS);
441 packet_put_string((char *) auth.dat, auth.length);
442 packet_send();
443 packet_write_wait();
1e608e42 444
3c0ef626 445 /* Zero the buffer. */
446 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
1e608e42 447
3c0ef626 448 slen = sizeof(local);
449 memset(&local, 0, sizeof(local));
450 if (getsockname(packet_get_connection_in(),
451 (struct sockaddr *)&local, &slen) < 0)
452 debug("getsockname failed: %s", strerror(errno));
1e608e42 453
3c0ef626 454 slen = sizeof(foreign);
455 memset(&foreign, 0, sizeof(foreign));
456 if (getpeername(packet_get_connection_in(),
457 (struct sockaddr *)&foreign, &slen) < 0) {
458 debug("getpeername failed: %s", strerror(errno));
459 fatal_cleanup();
460 }
461 /* Get server reply. */
1e608e42 462 type = packet_read();
3c0ef626 463 switch (type) {
464 case SSH_SMSG_FAILURE:
465 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
466 debug("Kerberos v4 authentication failed.");
467 return 0;
468 break;
1e608e42 469
3c0ef626 470 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
471 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
472 debug("Kerberos v4 authentication accepted.");
1e608e42 473
3c0ef626 474 /* Get server's response. */
475 reply = packet_get_string((u_int *) &auth.length);
510132b6 476 if (auth.length >= MAX_KTXT_LEN)
477 fatal("Kerberos v4: Malformed response from server");
3c0ef626 478 memcpy(auth.dat, reply, auth.length);
479 xfree(reply);
1e608e42 480
481 packet_check_eom();
482
3c0ef626 483 /*
484 * If his response isn't properly encrypted with the session
485 * key, and the decrypted checksum fails to match, he's
486 * bogus. Bail out.
487 */
488 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
489 &foreign, &local, &msg_data);
490 if (r != KSUCCESS) {
491 debug("Kerberos v4 krb_rd_priv failed: %s",
492 krb_err_txt[r]);
493 packet_disconnect("Kerberos v4 challenge failed!");
494 }
495 /* Fetch the (incremented) checksum that we supplied in the request. */
496 memcpy((char *)&cksum, (char *)msg_data.app_data,
497 sizeof(cksum));
498 cksum = ntohl(cksum);
1e608e42 499
3c0ef626 500 /* If it matches, we're golden. */
501 if (cksum == checksum + 1) {
502 debug("Kerberos v4 challenge successful.");
503 return 1;
504 } else
505 packet_disconnect("Kerberos v4 challenge failed!");
506 break;
1e608e42 507
3c0ef626 508 default:
509 packet_disconnect("Protocol error on Kerberos v4 response: %d", type);
510 }
511 return 0;
512}
513
514#endif /* KRB4 */
515
516#ifdef KRB5
517static int
518try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
519{
520 krb5_error_code problem;
521 const char *tkfile;
522 struct stat buf;
523 krb5_ccache ccache = NULL;
524 const char *remotehost;
525 krb5_data ap;
1e608e42 526 int type;
3c0ef626 527 krb5_ap_rep_enc_part *reply = NULL;
528 int ret;
1e608e42 529
3c0ef626 530 memset(&ap, 0, sizeof(ap));
1e608e42 531
3c0ef626 532 problem = krb5_init_context(context);
533 if (problem) {
534 debug("Kerberos v5: krb5_init_context failed");
535 ret = 0;
536 goto out;
537 }
538
63119dd9 539 problem = krb5_auth_con_init(*context, auth_context);
540 if (problem) {
541 debug("Kerberos v5: krb5_auth_con_init failed");
542 ret = 0;
543 goto out;
544 }
545
1e608e42 546#ifndef HEIMDAL
63119dd9 547 problem = krb5_auth_con_setflags(*context, *auth_context,
548 KRB5_AUTH_CONTEXT_RET_TIME);
549 if (problem) {
1e608e42 550 debug("Keberos v5: krb5_auth_con_setflags failed");
63119dd9 551 ret = 0;
552 goto out;
1e608e42 553 }
63119dd9 554#endif
555
3c0ef626 556 tkfile = krb5_cc_default_name(*context);
557 if (strncmp(tkfile, "FILE:", 5) == 0)
558 tkfile += 5;
1e608e42 559
3c0ef626 560 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
561 debug("Kerberos v5: could not get default ccache (permission denied).");
562 ret = 0;
563 goto out;
564 }
1e608e42 565
3c0ef626 566 problem = krb5_cc_default(*context, &ccache);
567 if (problem) {
568 debug("Kerberos v5: krb5_cc_default failed: %s",
569 krb5_get_err_text(*context, problem));
570 ret = 0;
571 goto out;
572 }
1e608e42 573
3c0ef626 574 remotehost = get_canonical_hostname(1);
1e608e42 575
3c0ef626 576 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
577 "host", remotehost, NULL, ccache, &ap);
578 if (problem) {
579 debug("Kerberos v5: krb5_mk_req failed: %s",
580 krb5_get_err_text(*context, problem));
581 ret = 0;
582 goto out;
583 }
1e608e42 584
3c0ef626 585 packet_start(SSH_CMSG_AUTH_KERBEROS);
586 packet_put_string((char *) ap.data, ap.length);
587 packet_send();
588 packet_write_wait();
1e608e42 589
3c0ef626 590 xfree(ap.data);
591 ap.length = 0;
1e608e42 592
593 type = packet_read();
3c0ef626 594 switch (type) {
1e608e42 595 case SSH_SMSG_FAILURE:
596 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
597 debug("Kerberos v5 authentication failed.");
598 ret = 0;
599 break;
600
3c0ef626 601 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
1e608e42 602 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
603 debug("Kerberos v5 authentication accepted.");
604
605 /* Get server's response. */
606 ap.data = packet_get_string((unsigned int *) &ap.length);
607 packet_check_eom();
608 /* XXX je to dobre? */
609
610 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
611 if (problem) {
3c0ef626 612 ret = 0;
613 }
614 ret = 1;
615 break;
1e608e42 616
3c0ef626 617 default:
618 packet_disconnect("Protocol error on Kerberos v5 response: %d",
619 type);
620 ret = 0;
621 break;
1e608e42 622
3c0ef626 623 }
1e608e42 624
3c0ef626 625 out:
626 if (ccache != NULL)
627 krb5_cc_close(*context, ccache);
628 if (reply != NULL)
629 krb5_free_ap_rep_enc_part(*context, reply);
630 if (ap.length > 0)
63119dd9 631#ifdef HEIMDAL
3c0ef626 632 krb5_data_free(&ap);
63119dd9 633#else
1e608e42 634 krb5_free_data_contents(*context, &ap);
635#endif
636
3c0ef626 637 return (ret);
638}
639
640static void
641send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
642{
1e608e42 643 int fd, type;
3c0ef626 644 krb5_error_code problem;
645 krb5_data outbuf;
646 krb5_ccache ccache = NULL;
647 krb5_creds creds;
63119dd9 648#ifdef HEIMDAL
3c0ef626 649 krb5_kdc_flags flags;
63119dd9 650#else
651 int forwardable;
652#endif
3c0ef626 653 const char *remotehost;
1e608e42 654
3c0ef626 655 memset(&creds, 0, sizeof(creds));
656 memset(&outbuf, 0, sizeof(outbuf));
1e608e42 657
3c0ef626 658 fd = packet_get_connection_in();
1e608e42 659
660#ifdef HEIMDAL
3c0ef626 661 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
63119dd9 662#else
1e608e42 663 problem = krb5_auth_con_genaddrs(context, auth_context, fd,
63119dd9 664 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
665 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
63119dd9 666#endif
1e608e42 667 if (problem)
668 goto out;
669
3c0ef626 670 problem = krb5_cc_default(context, &ccache);
671 if (problem)
672 goto out;
1e608e42 673
3c0ef626 674 problem = krb5_cc_get_principal(context, ccache, &creds.client);
675 if (problem)
676 goto out;
1e608e42 677
678 remotehost = get_canonical_hostname(1);
3c0ef626 679
63119dd9 680#ifdef HEIMDAL
3c0ef626 681 problem = krb5_build_principal(context, &creds.server,
682 strlen(creds.client->realm), creds.client->realm,
683 "krbtgt", creds.client->realm, NULL);
63119dd9 684#else
685 problem = krb5_build_principal(context, &creds.server,
686 creds.client->realm.length, creds.client->realm.data,
1e608e42 687 "host", remotehost, NULL);
63119dd9 688#endif
3c0ef626 689 if (problem)
690 goto out;
1e608e42 691
3c0ef626 692 creds.times.endtime = 0;
1e608e42 693
694#ifdef HEIMDAL
3c0ef626 695 flags.i = 0;
696 flags.b.forwarded = 1;
697 flags.b.forwardable = krb5_config_get_bool(context, NULL,
698 "libdefaults", "forwardable", NULL);
3c0ef626 699 problem = krb5_get_forwarded_creds(context, auth_context,
700 ccache, flags.i, remotehost, &creds, &outbuf);
63119dd9 701#else
702 forwardable = 1;
1e608e42 703 problem = krb5_fwd_tgt_creds(context, auth_context, remotehost,
704 creds.client, creds.server, ccache, forwardable, &outbuf);
705#endif
63119dd9 706
1e608e42 707 if (problem)
708 goto out;
63119dd9 709
3c0ef626 710 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
711 packet_put_string((char *)outbuf.data, outbuf.length);
712 packet_send();
713 packet_write_wait();
1e608e42 714
715 type = packet_read();
716
3c0ef626 717 if (type == SSH_SMSG_SUCCESS) {
718 char *pname;
1e608e42 719
3c0ef626 720 krb5_unparse_name(context, creds.client, &pname);
721 debug("Kerberos v5 TGT forwarded (%s).", pname);
722 xfree(pname);
723 } else
724 debug("Kerberos v5 TGT forwarding failed.");
1e608e42 725
3c0ef626 726 return;
1e608e42 727
3c0ef626 728 out:
729 if (problem)
730 debug("Kerberos v5 TGT forwarding failed: %s",
731 krb5_get_err_text(context, problem));
732 if (creds.client)
733 krb5_free_principal(context, creds.client);
734 if (creds.server)
735 krb5_free_principal(context, creds.server);
736 if (ccache)
737 krb5_cc_close(context, ccache);
738 if (outbuf.data)
739 xfree(outbuf.data);
740}
741#endif /* KRB5 */
742
743#ifdef AFS
744static void
745send_krb4_tgt(void)
746{
747 CREDENTIALS *creds;
748 struct stat st;
749 char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
1e608e42 750 int problem, type;
751
3c0ef626 752 /* Don't do anything if we don't have any tickets. */
753 if (stat(tkt_string(), &st) < 0)
754 return;
1e608e42 755
3c0ef626 756 creds = xmalloc(sizeof(*creds));
1e608e42 757
3c0ef626 758 problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm);
759 if (problem)
760 goto out;
1e608e42 761
3c0ef626 762 problem = krb_get_cred("krbtgt", prealm, prealm, creds);
763 if (problem)
764 goto out;
1e608e42 765
3c0ef626 766 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
767 problem = RD_AP_EXP;
768 goto out;
769 }
770 creds_to_radix(creds, (u_char *)buffer, sizeof(buffer));
1e608e42 771
3c0ef626 772 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
773 packet_put_cstring(buffer);
774 packet_send();
775 packet_write_wait();
1e608e42 776
777 type = packet_read();
778
3c0ef626 779 if (type == SSH_SMSG_SUCCESS)
780 debug("Kerberos v4 TGT forwarded (%s%s%s@%s).",
781 creds->pname, creds->pinst[0] ? "." : "",
782 creds->pinst, creds->realm);
783 else
784 debug("Kerberos v4 TGT rejected.");
1e608e42 785
3c0ef626 786 xfree(creds);
787 return;
1e608e42 788
3c0ef626 789 out:
790 debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]);
791 xfree(creds);
792}
793
794static void
795send_afs_tokens(void)
796{
797 CREDENTIALS creds;
798 struct ViceIoctl parms;
799 struct ClearToken ct;
800 int i, type, len;
801 char buf[2048], *p, *server_cell;
802 char buffer[8192];
1e608e42 803
3c0ef626 804 /* Move over ktc_GetToken, here's something leaner. */
805 for (i = 0; i < 100; i++) { /* just in case */
806 parms.in = (char *) &i;
807 parms.in_size = sizeof(i);
808 parms.out = buf;
809 parms.out_size = sizeof(buf);
810 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
811 break;
812 p = buf;
1e608e42 813
3c0ef626 814 /* Get secret token. */
815 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
816 if (creds.ticket_st.length > MAX_KTXT_LEN)
817 break;
818 p += sizeof(u_int);
819 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
820 p += creds.ticket_st.length;
1e608e42 821
3c0ef626 822 /* Get clear token. */
823 memcpy(&len, p, sizeof(len));
824 if (len != sizeof(struct ClearToken))
825 break;
826 p += sizeof(len);
827 memcpy(&ct, p, len);
828 p += len;
829 p += sizeof(len); /* primary flag */
830 server_cell = p;
1e608e42 831
3c0ef626 832 /* Flesh out our credentials. */
833 strlcpy(creds.service, "afs", sizeof(creds.service));
834 creds.instance[0] = '\0';
835 strlcpy(creds.realm, server_cell, REALM_SZ);
836 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
837 creds.issue_date = ct.BeginTimestamp;
838 creds.lifetime = krb_time_to_life(creds.issue_date,
839 ct.EndTimestamp);
840 creds.kvno = ct.AuthHandle;
841 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
842 creds.pinst[0] = '\0';
1e608e42 843
3c0ef626 844 /* Encode token, ship it off. */
845 if (creds_to_radix(&creds, (u_char *)buffer,
846 sizeof(buffer)) <= 0)
847 break;
848 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
849 packet_put_cstring(buffer);
850 packet_send();
851 packet_write_wait();
852
853 /* Roger, Roger. Clearance, Clarence. What's your vector,
854 Victor? */
1e608e42 855 type = packet_read();
856
3c0ef626 857 if (type == SSH_SMSG_FAILURE)
858 debug("AFS token for cell %s rejected.", server_cell);
859 else if (type != SSH_SMSG_SUCCESS)
860 packet_disconnect("Protocol error on AFS token response: %d", type);
861 }
862}
863
864#endif /* AFS */
865
866/*
867 * Tries to authenticate with any string-based challenge/response system.
868 * Note that the client code is not tied to s/key or TIS.
869 */
870static int
871try_challenge_response_authentication(void)
872{
873 int type, i;
3c0ef626 874 u_int clen;
875 char prompt[1024];
876 char *challenge, *response;
877
878 debug("Doing challenge response authentication.");
879
880 for (i = 0; i < options.number_of_password_prompts; i++) {
881 /* request a challenge */
882 packet_start(SSH_CMSG_AUTH_TIS);
883 packet_send();
884 packet_write_wait();
885
1e608e42 886 type = packet_read();
3c0ef626 887 if (type != SSH_SMSG_FAILURE &&
888 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
889 packet_disconnect("Protocol error: got %d in response "
890 "to SSH_CMSG_AUTH_TIS", type);
891 }
892 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
893 debug("No challenge.");
894 return 0;
895 }
896 challenge = packet_get_string(&clen);
1e608e42 897 packet_check_eom();
3c0ef626 898 snprintf(prompt, sizeof prompt, "%s%s", challenge,
1e608e42 899 strchr(challenge, '\n') ? "" : "\nResponse: ");
3c0ef626 900 xfree(challenge);
901 if (i != 0)
902 error("Permission denied, please try again.");
903 if (options.cipher == SSH_CIPHER_NONE)
904 log("WARNING: Encryption is disabled! "
510132b6 905 "Response will be transmitted in clear text.");
3c0ef626 906 response = read_passphrase(prompt, 0);
907 if (strcmp(response, "") == 0) {
908 xfree(response);
909 break;
910 }
911 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
912 ssh_put_password(response);
913 memset(response, 0, strlen(response));
914 xfree(response);
915 packet_send();
916 packet_write_wait();
1e608e42 917 type = packet_read();
3c0ef626 918 if (type == SSH_SMSG_SUCCESS)
919 return 1;
920 if (type != SSH_SMSG_FAILURE)
921 packet_disconnect("Protocol error: got %d in response "
922 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
923 }
924 /* failure */
925 return 0;
926}
927
928/*
929 * Tries to authenticate with plain passwd authentication.
930 */
931static int
932try_password_authentication(char *prompt)
933{
1e608e42 934 int type, i;
3c0ef626 935 char *password;
936
937 debug("Doing password authentication.");
938 if (options.cipher == SSH_CIPHER_NONE)
939 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
940 for (i = 0; i < options.number_of_password_prompts; i++) {
941 if (i != 0)
942 error("Permission denied, please try again.");
943 password = read_passphrase(prompt, 0);
944 packet_start(SSH_CMSG_AUTH_PASSWORD);
945 ssh_put_password(password);
946 memset(password, 0, strlen(password));
947 xfree(password);
948 packet_send();
949 packet_write_wait();
950
1e608e42 951 type = packet_read();
3c0ef626 952 if (type == SSH_SMSG_SUCCESS)
953 return 1;
954 if (type != SSH_SMSG_FAILURE)
955 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
956 }
957 /* failure */
958 return 0;
959}
960
0b582e46 961#ifdef GSSAPI
962/*
963 * This code stolen from the gss-client.c sample program from MIT's
964 * kerberos 5 distribution.
965 */
966
967gss_cred_id_t gss_cred = GSS_C_NO_CREDENTIAL;
968
969static void display_status_1(m, code, type)
970 char *m;
971 OM_uint32 code;
972 int type;
973{
974 OM_uint32 maj_stat, min_stat;
975 gss_buffer_desc msg;
976 OM_uint32 msg_ctx;
977
978 msg_ctx = 0;
979 while (1) {
980 maj_stat = gss_display_status(&min_stat, code,
981 type, GSS_C_NULL_OID,
982 &msg_ctx, &msg);
983 debug("GSS-API error %s: %s", m, (char *)msg.value);
984 (void) gss_release_buffer(&min_stat, &msg);
985
986 if (!msg_ctx)
987 break;
988 }
989}
990
991static void display_gssapi_status(msg, maj_stat, min_stat)
992 char *msg;
993 OM_uint32 maj_stat;
994 OM_uint32 min_stat;
995{
996 display_status_1(msg, maj_stat, GSS_C_GSS_CODE);
997 display_status_1(msg, min_stat, GSS_C_MECH_CODE);
998}
999
1000#ifdef GSI
1001int get_gssapi_cred()
1002{
1003 OM_uint32 maj_stat;
1004 OM_uint32 min_stat;
1005
1006
1007 debug("calling gss_acquire_cred");
1008 maj_stat = gss_acquire_cred(&min_stat,
1009 GSS_C_NO_NAME,
1010 GSS_C_INDEFINITE,
1011 GSS_C_NO_OID_SET,
1012 GSS_C_INITIATE,
1013 &gss_cred,
1014 NULL,
1015 NULL);
1016
1017 if (maj_stat != GSS_S_COMPLETE) {
1018 display_gssapi_status("Failuring acquiring GSSAPI credentials",
1019 maj_stat, min_stat);
1020 gss_cred = GSS_C_NO_CREDENTIAL; /* should not be needed */
1021 return 0;
1022 }
1023
1024 return 1; /* Success */
1025}
1026
1027char * get_gss_our_name()
1028{
1029 OM_uint32 maj_stat;
1030 OM_uint32 min_stat;
1031 gss_name_t pname = GSS_C_NO_NAME;
1032 gss_buffer_desc tmpname;
1033 gss_buffer_t tmpnamed = &tmpname;
1034 char *retname;
1035
1036 debug("calling gss_inquire_cred");
1037 maj_stat = gss_inquire_cred(&min_stat,
1038 gss_cred,
1039 &pname,
1040 NULL,
1041 NULL,
1042 NULL);
1043 if (maj_stat != GSS_S_COMPLETE) {
1044 return NULL;
1045 }
1046
9d36d8cb 1047 maj_stat = gss_display_name(&min_stat,
1048 pname,
1049 tmpnamed,
1050 NULL);
0b582e46 1051 if (maj_stat != GSS_S_COMPLETE) {
9d36d8cb 1052 return NULL;
0b582e46 1053 }
9d36d8cb 1054 debug("gss_display_name finsished");
0b582e46 1055 retname = (char *)malloc(tmpname.length + 1);
1056 if (!retname) {
1057 return NULL;
1058 }
1059 memcpy(retname, tmpname.value, tmpname.length);
1060 retname[tmpname.length] = '\0';
1061
1062 gss_release_name(&min_stat, &pname);
1063 gss_release_buffer(&min_stat, tmpnamed);
1064
1065 return retname;
1066}
1067#endif /* GSI */
1068
1069int try_gssapi_authentication(char *host, Options *options)
1070{
1071 char *service_name = NULL;
1072 gss_buffer_desc name_tok;
1073 gss_buffer_desc send_tok;
1074 gss_buffer_desc recv_tok;
1075 gss_buffer_desc *token_ptr;
1076 gss_name_t target_name = NULL;
1077 gss_ctx_id_t gss_context;
1078 gss_OID_desc mech_oid;
1079 gss_OID name_type;
1080 gss_OID_set my_mechs;
1081 int my_mech_num;
1082 OM_uint32 maj_stat;
1083 OM_uint32 min_stat;
1084 int ret_stat = 0; /* 1 == success */
1085 OM_uint32 req_flags = 0;
1086 OM_uint32 ret_flags;
1087 int type;
1088 char *gssapi_auth_type = NULL;
1089 struct hostent *hostinfo;
2154b007 1090 char *addr;
007914b3 1091 unsigned int slen;
0b582e46 1092
1093 /*
1094 * host is not guarenteed to be a FQDN, so we need to make sure it is.
1095 */
1096 hostinfo = gethostbyname(host);
1097
9e495a24 1098 /* Use local hostname when coming in on loopback interface because
1099 we won't have 'localhost' credentials. */
1100 if (hostinfo && hostinfo->h_addrtype == AF_INET) {
1101 struct in_addr addr;
1102 addr = *(struct in_addr *)(hostinfo->h_addr);
1103 if (ntohl(addr.s_addr) == INADDR_LOOPBACK) {
1104 char buf[4096];
1105 if (gethostname(buf, 4096) == 0) {
1106 hostinfo = gethostbyname(buf);
1107 }
1108 }
1109 }
1110
2154b007 1111 if ((hostinfo == NULL) || (hostinfo->h_addr == NULL)) {
1112 debug("GSSAPI authentication: Unable to get FQDN for \"%s\"", host);
1113 goto cleanup;
1114 }
1115
1116 addr = xmalloc(hostinfo->h_length);
1117 memcpy(addr, hostinfo->h_addr, hostinfo->h_length);
1118 hostinfo = gethostbyaddr(addr, hostinfo->h_length, AF_INET);
1119 xfree(addr);
1120
9e495a24 1121 /* Go to the resolver to get the official hostname for our target.
1122 WARNING: This makes us vulnerable to DNS spoofing. */
0b582e46 1123 if ((hostinfo == NULL) || (hostinfo->h_name == NULL)) {
1124 debug("GSSAPI authentication: Unable to get FQDN for \"%s\"", host);
1125 goto cleanup;
1126 }
1127
1128 /*
1129 * Default flags
1130 */
1131 req_flags |= GSS_C_REPLAY_FLAG;
1132
1133 /* Do mutual authentication */
1134 req_flags |= GSS_C_MUTUAL_FLAG;
1135
3f726215 1136#ifdef KRB5
0b582e46 1137
1138 gssapi_auth_type = "GSSAPI/Kerberos 5";
1139
3f726215 1140#endif
0b582e46 1141
1142#ifdef GSI
1143
1144 gssapi_auth_type = "GSSAPI/GLOBUS";
1145
1146#endif /* GSI */
1147
1148 if (gssapi_auth_type == NULL) {
1149 debug("No GSSAPI type defined during compile");
1150 goto cleanup;
1151 }
1152
1153 debug("Attempting %s authentication", gssapi_auth_type);
1154
1aad58f1 1155 service_name = (char *) malloc(strlen("host") +
0b582e46 1156 strlen(hostinfo->h_name) +
1157 2 /* 1 for '@', 1 for NUL */);
1158
1159 if (service_name == NULL) {
1160 debug("malloc() failed");
1161 goto cleanup;
1162 }
1163
1164
1aad58f1 1165 sprintf(service_name, "host@%s", hostinfo->h_name);
0b582e46 1166
1167 name_type = GSS_C_NT_HOSTBASED_SERVICE;
1168
1169 debug("Service name is %s", service_name);
1170
1171 /* Forward credentials? */
1172
3f726215 1173#ifdef KRB5
0b582e46 1174 if (options->kerberos_tgt_passing) {
1175 debug("Forwarding Kerberos credentials");
1176 req_flags |= GSS_C_DELEG_FLAG;
1177 }
3f726215 1178#endif /* KRB5 */
0b582e46 1179
d0fc8d74 1180#ifdef GSSAPI
1181 if(options->gss_deleg_creds) {
0b582e46 1182 debug("Forwarding X509 proxy certificate");
1183 req_flags |= GSS_C_DELEG_FLAG;
1184 }
1185#ifdef GSS_C_GLOBUS_LIMITED_DELEG_PROXY_FLAG
d0fc8d74 1186 /* Forward limited credentials, overrides gss_deleg_creds */
1187 if(options->gss_globus_deleg_limited_proxy) {
0b582e46 1188 debug("Forwarding limited X509 proxy certificate");
1189 req_flags |= (GSS_C_DELEG_FLAG | GSS_C_GLOBUS_LIMITED_DELEG_PROXY_FLAG);
1190 }
1191#endif /* GSS_C_GLOBUS_LIMITED_DELEG_PROXY_FLAG */
1192
d0fc8d74 1193#endif /* GSSAPI */
0b582e46 1194
c728f45a 1195 debug("req_flags = %u", (unsigned int)req_flags);
0b582e46 1196
1197 name_tok.value = service_name;
1198 name_tok.length = strlen(service_name) + 1;
1199 maj_stat = gss_import_name(&min_stat, &name_tok,
1200 name_type, &target_name);
1201
1202 free(service_name);
1203 service_name = NULL;
1204
1205 if (maj_stat != GSS_S_COMPLETE) {
1206 display_gssapi_status("importing service name", maj_stat, min_stat);
1207 goto cleanup;
1208 }
1209
1210 maj_stat = gss_indicate_mechs(&min_stat, &my_mechs);
1211
1212 if (maj_stat != GSS_S_COMPLETE) {
1213 display_gssapi_status("indicating mechs", maj_stat, min_stat);
1214 goto cleanup;
1215 }
1216
1217 /*
1218 * Send over a packet to the daemon, letting it know we're doing
1219 * GSSAPI and our mech_oid(s).
1220 */
1221 debug("Sending mech oid to server");
1222 packet_start(SSH_CMSG_AUTH_GSSAPI);
1223 packet_put_int(my_mechs->count); /* Number of mechs we're sending */
1224 for (my_mech_num = 0; my_mech_num < my_mechs->count; my_mech_num++)
1225 packet_put_string(my_mechs->elements[my_mech_num].elements,
1226 my_mechs->elements[my_mech_num].length);
1227 packet_send();
1228 packet_write_wait();
1229
1230 /*
1231 * Get reply from the daemon to see if our mech was acceptable
1232 */
1e608e42 1233 type = packet_read();
0b582e46 1234
1235 switch (type) {
1236 case SSH_SMSG_AUTH_GSSAPI_RESPONSE:
1237 debug("Server accepted mechanism");
1238 /* Successful negotiation */
1239 break;
1240
1241 case SSH_MSG_AUTH_GSSAPI_ABORT:
1242 debug("Unable to negotiate GSSAPI mechanism type with server");
1243 packet_get_all();
1244 goto cleanup;
1245
1246 default:
1247 packet_disconnect("Protocol error during GSSAPI authentication:"
1248 " packet type %d received",
1249 type);
1250 /* Does not return */
1251 }
1252
1253 /* Read the mechanism the server returned */
007914b3 1254 mech_oid.elements = packet_get_string(&slen);
1255 mech_oid.length = slen; /* safe typecast */
0b582e46 1256 packet_get_all();
1257
1258 /*
1259 * Perform the context-establishement loop.
1260 *
1261 * On each pass through the loop, token_ptr points to the token
1262 * to send to the server (or GSS_C_NO_BUFFER on the first pass).
1263 * Every generated token is stored in send_tok which is then
1264 * transmitted to the server; every received token is stored in
1265 * recv_tok, which token_ptr is then set to, to be processed by
1266 * the next call to gss_init_sec_context.
1267 *
1268 * GSS-API guarantees that send_tok's length will be non-zero
1269 * if and only if the server is expecting another token from us,
1270 * and that gss_init_sec_context returns GSS_S_CONTINUE_NEEDED if
1271 * and only if the server has another token to send us.
1272 */
1273
1274 token_ptr = GSS_C_NO_BUFFER;
1275 gss_context = GSS_C_NO_CONTEXT;
1276
1277 do {
1278 maj_stat =
1279 gss_init_sec_context(&min_stat,
1280 gss_cred,
1281 &gss_context,
1282 target_name,
1283 &mech_oid,
1284 req_flags,
1285 0,
1286 NULL, /* no channel bindings */
1287 token_ptr,
1288 NULL, /* ignore mech type */
1289 &send_tok,
1290 &ret_flags,
1291 NULL); /* ignore time_rec */
1292
1293 if (token_ptr != GSS_C_NO_BUFFER)
1294 (void) gss_release_buffer(&min_stat, &recv_tok);
1295
1296 if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
1297 display_gssapi_status("initializing context", maj_stat, min_stat);
1298
1299 /* Send an abort message */
1300 packet_start(SSH_MSG_AUTH_GSSAPI_ABORT);
1301 packet_send();
1302 packet_write_wait();
1303
1304 goto cleanup;
1305 }
1306
1307 if (send_tok.length != 0) {
1308 debug("Sending authenticaton token...");
1309 packet_start(SSH_MSG_AUTH_GSSAPI_TOKEN);
1310 packet_put_string((char *) send_tok.value, send_tok.length);
1311 packet_send();
1312 packet_write_wait();
1313
1314 (void) gss_release_buffer(&min_stat, &send_tok);
1315 }
1316
1317 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
1318
1319 debug("Continue needed. Reading response...");
1320
1e608e42 1321 type = packet_read();
0b582e46 1322
1323 switch(type) {
1324
1325 case SSH_MSG_AUTH_GSSAPI_TOKEN:
1326 /* This is what we expected */
1327 break;
1328
1329 case SSH_MSG_AUTH_GSSAPI_ABORT:
1330 debug("Server aborted GSSAPI authentication.");
1331 packet_get_all();
1332 goto cleanup;
1333
1334 default:
1335 packet_disconnect("Protocol error during GSSAPI authentication:"
1336 " packet type %d received",
1337 type);
1338 /* Does not return */
1339 }
1340
007914b3 1341 recv_tok.value = packet_get_string(&slen);
1342 recv_tok.length=slen; /* safe typecast */
0b582e46 1343 packet_get_all();
1344 token_ptr = &recv_tok;
1345 }
1346 } while (maj_stat == GSS_S_CONTINUE_NEEDED);
1347
1348 /* Success */
1349 ret_stat = 1;
1350
1351 debug("%s authentication successful", gssapi_auth_type);
1352
1353 /*
1354 * Read hash of host and server keys and make sure it
1355 * matches what we got earlier.
1356 */
1357 debug("Reading hash of server and host keys...");
1e608e42 1358 type = packet_read();
0b582e46 1359
1360 if (type == SSH_MSG_AUTH_GSSAPI_ABORT) {
1361 debug("Server aborted GSSAPI authentication.");
1362 packet_get_all();
1363 ret_stat = 0;
1364 goto cleanup;
1365
1366 } else if (type == SSH_SMSG_AUTH_GSSAPI_HASH) {
1367 gss_buffer_desc wrapped_buf;
1368 gss_buffer_desc unwrapped_buf;
1369 int conf_state;
1370 gss_qop_t qop_state;
1371
1372
007914b3 1373 wrapped_buf.value = packet_get_string(&slen);
1374 wrapped_buf.length=slen; /* safe typecast */
0b582e46 1375 packet_get_all();
1376
1377 maj_stat = gss_unwrap(&min_stat,
1378 gss_context,
1379 &wrapped_buf,
1380 &unwrapped_buf,
1381 &conf_state,
1382 &qop_state);
1383
1384 if (maj_stat != GSS_S_COMPLETE) {
1385 display_gssapi_status("unwraping SSHD key hash",
1386 maj_stat, min_stat);
1387 packet_disconnect("Verification of SSHD keys through GSSAPI-secured channel failed: "
1388 "Unwrapping of hash failed.");
1389 }
1390
1391 if (unwrapped_buf.length != sizeof(ssh_key_digest)) {
1392 packet_disconnect("Verification of SSHD keys through GSSAPI-secured channel failed: "
1393 "Size of key hashes do not match (%d != %d)!",
007914b3 1394 (int)unwrapped_buf.length,
1395 (int)sizeof(ssh_key_digest));
0b582e46 1396 }
1397
1398 if (memcmp(ssh_key_digest, unwrapped_buf.value, sizeof(ssh_key_digest)) != 0) {
1399 packet_disconnect("Verification of SSHD keys through GSSAPI-secured channel failed: "
1400 "Hashes don't match!");
1401 }
1402
1403 debug("Verified SSHD keys through GSSAPI-secured channel.");
1404
1405 gss_release_buffer(&min_stat, &unwrapped_buf);
1406
1407 } else {
1408 packet_disconnect("Protocol error during GSSAPI authentication:"
1409 "packet type %d received", type);
1410 /* Does not return */
1411 }
1412
1413
1414 cleanup:
1415 if (target_name != NULL)
1416 (void) gss_release_name(&min_stat, &target_name);
1417
1418 return ret_stat;
1419}
1420
1421#endif /* GSSAPI */
1422
0b582e46 1423
3c0ef626 1424/*
1425 * SSH1 key exchange
1426 */
1427void
1428ssh_kex(char *host, struct sockaddr *hostaddr)
1429{
1430 int i;
1431 BIGNUM *key;
1e608e42 1432 Key *host_key, *server_key;
3c0ef626 1433 int bits, rbits;
1434 int ssh_cipher_default = SSH_CIPHER_3DES;
1435 u_char session_key[SSH_SESSION_KEY_LENGTH];
1436 u_char cookie[8];
1437 u_int supported_ciphers;
1438 u_int server_flags, client_flags;
3c0ef626 1439 u_int32_t rand = 0;
1440
1441 debug("Waiting for server public key.");
1442
1443 /* Wait for a public key packet from the server. */
1e608e42 1444 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
3c0ef626 1445
1446 /* Get cookie from the packet. */
1447 for (i = 0; i < 8; i++)
1448 cookie[i] = packet_get_char();
1449
1450 /* Get the public key. */
1e608e42 1451 server_key = key_new(KEY_RSA1);
1452 bits = packet_get_int();
1453 packet_get_bignum(server_key->rsa->e);
1454 packet_get_bignum(server_key->rsa->n);
1455
1456 rbits = BN_num_bits(server_key->rsa->n);
3c0ef626 1457 if (bits != rbits) {
1458 log("Warning: Server lies about size of server public key: "
1459 "actual size is %d bits vs. announced %d.", rbits, bits);
1460 log("Warning: This may be due to an old implementation of ssh.");
1461 }
1462 /* Get the host key. */
1e608e42 1463 host_key = key_new(KEY_RSA1);
1464 bits = packet_get_int();
1465 packet_get_bignum(host_key->rsa->e);
1466 packet_get_bignum(host_key->rsa->n);
1467
1468 rbits = BN_num_bits(host_key->rsa->n);
3c0ef626 1469 if (bits != rbits) {
1470 log("Warning: Server lies about size of server host key: "
1471 "actual size is %d bits vs. announced %d.", rbits, bits);
1472 log("Warning: This may be due to an old implementation of ssh.");
1473 }
1474
0b582e46 1475#ifdef GSSAPI
1476 {
1477 MD5_CTX md5context;
1478 Buffer buf;
1479 unsigned char *data;
1480 unsigned int data_len;
1481
1482 /*
1483 * Hash the server and host keys. Later we will check them against
1484 * a hash sent over a secure channel to make sure they are legit.
1485 */
1486 debug("Calculating MD5 hash of server and host keys...");
1487
1488 /* Write all the keys to a temporary buffer */
1489 buffer_init(&buf);
1490
1491 /* Server key */
1e608e42 1492 buffer_put_bignum(&buf, server_key->rsa->e);
1493 buffer_put_bignum(&buf, server_key->rsa->n);
0b582e46 1494
1495 /* Host key */
1e608e42 1496 buffer_put_bignum(&buf, host_key->rsa->e);
1497 buffer_put_bignum(&buf, host_key->rsa->n);
0b582e46 1498
1499 /* Get the resulting data */
1500 data = (unsigned char *) buffer_ptr(&buf);
1501 data_len = buffer_len(&buf);
1502
1503 /* And hash it */
1504 MD5_Init(&md5context);
1505 MD5_Update(&md5context, data, data_len);
1506 MD5_Final(ssh_key_digest, &md5context);
1507
1508 /* Clean up */
1509 buffer_clear(&buf);
1510 buffer_free(&buf);
1511 }
1512#endif /* GSSAPI */
0b582e46 1513
3c0ef626 1514 /* Get protocol flags. */
1515 server_flags = packet_get_int();
1516 packet_set_protocol_flags(server_flags);
1517
1518 supported_ciphers = packet_get_int();
1519 supported_authentications = packet_get_int();
1e608e42 1520 packet_check_eom();
3c0ef626 1521
1522 debug("Received server public key (%d bits) and host key (%d bits).",
1e608e42 1523 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
1524
1525 if (verify_host_key(host, hostaddr, host_key) == -1)
3c0ef626 1526 fatal("Host key verification failed.");
1527
1528 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
1529
1e608e42 1530 compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
3c0ef626 1531
1532 /* Generate a session key. */
1533 arc4random_stir();
1534
1535 /*
1536 * Generate an encryption key for the session. The key is a 256 bit
1537 * random number, interpreted as a 32-byte key, with the least
1538 * significant 8 bits being the first byte of the key.
1539 */
1540 for (i = 0; i < 32; i++) {
1541 if (i % 4 == 0)
1542 rand = arc4random();
1543 session_key[i] = rand & 0xff;
1544 rand >>= 8;
1545 }
1546
1547 /*
1548 * According to the protocol spec, the first byte of the session key
1549 * is the highest byte of the integer. The session key is xored with
1550 * the first 16 bytes of the session id.
1551 */
1e608e42 1552 if ((key = BN_new()) == NULL)
1553 fatal("respond_to_rsa_challenge: BN_new failed");
3c0ef626 1554 BN_set_word(key, 0);
1555 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
1556 BN_lshift(key, key, 8);
1557 if (i < 16)
1558 BN_add_word(key, session_key[i] ^ session_id[i]);
1559 else
1560 BN_add_word(key, session_key[i]);
1561 }
1562
1563 /*
1564 * Encrypt the integer using the public key and host key of the
1565 * server (key with smaller modulus first).
1566 */
1e608e42 1567 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
3c0ef626 1568 /* Public key has smaller modulus. */
1e608e42 1569 if (BN_num_bits(host_key->rsa->n) <
1570 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1571 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
1572 "SSH_KEY_BITS_RESERVED %d",
1573 BN_num_bits(host_key->rsa->n),
1574 BN_num_bits(server_key->rsa->n),
1575 SSH_KEY_BITS_RESERVED);
3c0ef626 1576 }
1e608e42 1577 rsa_public_encrypt(key, key, server_key->rsa);
1578 rsa_public_encrypt(key, key, host_key->rsa);
3c0ef626 1579 } else {
1580 /* Host key has smaller modulus (or they are equal). */
1e608e42 1581 if (BN_num_bits(server_key->rsa->n) <
1582 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1583 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
1584 "SSH_KEY_BITS_RESERVED %d",
1585 BN_num_bits(server_key->rsa->n),
1586 BN_num_bits(host_key->rsa->n),
1587 SSH_KEY_BITS_RESERVED);
3c0ef626 1588 }
1e608e42 1589 rsa_public_encrypt(key, key, host_key->rsa);
1590 rsa_public_encrypt(key, key, server_key->rsa);
3c0ef626 1591 }
1592
1593 /* Destroy the public keys since we no longer need them. */
1e608e42 1594 key_free(server_key);
1595 key_free(host_key);
3c0ef626 1596
1597 if (options.cipher == SSH_CIPHER_NOT_SET) {
1598 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
1599 options.cipher = ssh_cipher_default;
1600 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
1601 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
1602 log("No valid SSH1 cipher, using %.100s instead.",
1603 cipher_name(ssh_cipher_default));
1604 options.cipher = ssh_cipher_default;
1605 }
1606 /* Check that the selected cipher is supported. */
1607 if (!(supported_ciphers & (1 << options.cipher)))
1608 fatal("Selected cipher type %.100s not supported by server.",
1e608e42 1609 cipher_name(options.cipher));
3c0ef626 1610
1611 debug("Encryption type: %.100s", cipher_name(options.cipher));
1612
1613 /* Send the encrypted session key to the server. */
1614 packet_start(SSH_CMSG_SESSION_KEY);
1615 packet_put_char(options.cipher);
1616
1617 /* Send the cookie back to the server. */
1618 for (i = 0; i < 8; i++)
1619 packet_put_char(cookie[i]);
1620
1621 /* Send and destroy the encrypted encryption key integer. */
1622 packet_put_bignum(key);
1623 BN_clear_free(key);
1624
1625 /* Send protocol flags. */
1626 packet_put_int(client_flags);
1627
1628 /* Send the packet now. */
1629 packet_send();
1630 packet_write_wait();
1631
1632 debug("Sent encrypted session key.");
1633
1634 /* Set the encryption key. */
1635 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1636
1637 /* We will no longer need the session key here. Destroy any extra copies. */
1638 memset(session_key, 0, sizeof(session_key));
1639
1640 /*
1641 * Expect a success message from the server. Note that this message
1642 * will be received in encrypted form.
1643 */
1e608e42 1644 packet_read_expect(SSH_SMSG_SUCCESS);
3c0ef626 1645
1646 debug("Received encrypted confirmation.");
1647}
1648
1649/*
1650 * Authenticate user
1651 */
1652void
1653ssh_userauth1(const char *local_user, const char *server_user, char *host,
44a053a3 1654 Sensitive *sensitive)
3c0ef626 1655{
0b582e46 1656#ifdef GSSAPI
1657#ifdef GSI
1658 const char *save_server_user = NULL;
1659#endif /* GSI */
1660#endif /* GSSAPI */
0b582e46 1661
3c0ef626 1662#ifdef KRB5
1663 krb5_context context = NULL;
1664 krb5_auth_context auth_context = NULL;
1665#endif
1666 int i, type;
1e608e42 1667
3c0ef626 1668 if (supported_authentications == 0)
1669 fatal("ssh_userauth1: server supports no auth methods");
1670
0b582e46 1671#ifdef GSSAPI
1672#ifdef GSI
1673 /* if no user given, tack on the subject name after the server_user.
1674 * This will allow us to run gridmap early to get real user
1675 * This name will start with /C=
1676 */
1677 if ((supported_authentications & (1 << SSH_AUTH_GSSAPI)) &&
1678 options.gss_authentication) {
1679 if (get_gssapi_cred()) {
1680 char * retname;
1681 char * newname;
1682
1683
1684 save_server_user = server_user;
1685
1686 retname = get_gss_our_name();
1687
1688 if (retname) {
1689 debug("passing gssapi name '%s'", retname);
1690 if (server_user) {
1691 newname = (char *) malloc(strlen(retname) + strlen(server_user) + 4);
1692 if (newname) {
1693 strcpy(newname, server_user);
e85707fe 1694 if(options.implicit) {
0b582e46 1695 strcat(newname,":i:");
e85707fe 1696 } else {
0b582e46 1697 strcat(newname,":x:");
e85707fe 1698 }
0b582e46 1699 strcat(newname, retname);
1700 server_user = newname;
1701 free(retname);
1702 }
1703 }
1704 }
1705 } else {
1706 /*
1707 * If we couldn't successfully get our GSSAPI credentials then
1708 * turn off gssapi authentication
1709 */
1710 options.gss_authentication = 0;
1711 }
1712 debug("server_user %s", server_user);
1713 }
1714#endif /* GSI */
1715#endif /* GSSAPI */
0b582e46 1716
3c0ef626 1717 /* Send the name of the user to log in as on the server. */
1718 packet_start(SSH_CMSG_USER);
1719 packet_put_cstring(server_user);
1720 packet_send();
1721 packet_write_wait();
1722
0b582e46 1723#if defined(GSI)
1724 if(save_server_user)
1725 {
1726 server_user = save_server_user;
1727 }
1728#endif
3c0ef626 1729 /*
1730 * The server should respond with success if no authentication is
1731 * needed (the user has no password). Otherwise the server responds
1732 * with failure.
1733 */
1e608e42 1734 type = packet_read();
3c0ef626 1735
1736 /* check whether the connection was accepted without authentication. */
1737 if (type == SSH_SMSG_SUCCESS)
1738 goto success;
1739 if (type != SSH_SMSG_FAILURE)
1740 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
0b582e46 1741
0b582e46 1742#ifdef GSSAPI
1743 /* Try GSSAPI authentication */
1744 if ((supported_authentications & (1 << SSH_AUTH_GSSAPI)) &&
1745 options.gss_authentication)
1746 {
1aad58f1 1747 debug("Trying GSSAPI authentication...");
0b582e46 1748 try_gssapi_authentication(host, &options);
1749
1750 /*
1751 * XXX Hmmm. Kerberos authentication only reads a packet if it thinks
1752 * the authentication went OK, but the server seems to always send
1753 * a packet back. So I'm not sure if I'm missing something or
1754 * the Kerberos code is broken. - vwelch 1/27/99
1755 */
1756
1e608e42 1757 type = packet_read();
0b582e46 1758 if (type == SSH_SMSG_SUCCESS)
1759 return; /* Successful connection. */
1760 if (type != SSH_SMSG_FAILURE)
1761 packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
1762
1763 debug("GSSAPI authentication failed");
1764 }
1765#endif /* GSSAPI */
3c0ef626 1766
1767#ifdef KRB5
1768 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1e608e42 1769 options.kerberos_authentication) {
3c0ef626 1770 debug("Trying Kerberos v5 authentication.");
1e608e42 1771
3c0ef626 1772 if (try_krb5_authentication(&context, &auth_context)) {
1e608e42 1773 type = packet_read();
3c0ef626 1774 if (type == SSH_SMSG_SUCCESS)
1775 goto success;
1776 if (type != SSH_SMSG_FAILURE)
1777 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
1778 }
1779 }
1780#endif /* KRB5 */
1e608e42 1781
3c0ef626 1782#ifdef KRB4
1783 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1784 options.kerberos_authentication) {
1785 debug("Trying Kerberos v4 authentication.");
1e608e42 1786
3c0ef626 1787 if (try_krb4_authentication()) {
1e608e42 1788 type = packet_read();
3c0ef626 1789 if (type == SSH_SMSG_SUCCESS)
1790 goto success;
1791 if (type != SSH_SMSG_FAILURE)
1792 packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type);
1793 }
1794 }
1795#endif /* KRB4 */
1e608e42 1796
3c0ef626 1797 /*
1798 * Use rhosts authentication if running in privileged socket and we
1799 * do not wish to remain anonymous.
1800 */
1801 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1802 options.rhosts_authentication) {
1803 debug("Trying rhosts authentication.");
1804 packet_start(SSH_CMSG_AUTH_RHOSTS);
1805 packet_put_cstring(local_user);
1806 packet_send();
1807 packet_write_wait();
1808
1809 /* The server should respond with success or failure. */
1e608e42 1810 type = packet_read();
3c0ef626 1811 if (type == SSH_SMSG_SUCCESS)
1812 goto success;
1813 if (type != SSH_SMSG_FAILURE)
1814 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1815 type);
1816 }
1817 /*
1818 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1819 * authentication.
1820 */
1821 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1822 options.rhosts_rsa_authentication) {
44a053a3 1823 for (i = 0; i < sensitive->nkeys; i++) {
1824 if (sensitive->keys[i] != NULL &&
1825 sensitive->keys[i]->type == KEY_RSA1 &&
1826 try_rhosts_rsa_authentication(local_user,
1827 sensitive->keys[i]))
3c0ef626 1828 goto success;
1829 }
1830 }
1831 /* Try RSA authentication if the server supports it. */
1832 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1833 options.rsa_authentication) {
1834 /*
1835 * Try RSA authentication using the authentication agent. The
1836 * agent is tried first because no passphrase is needed for
1837 * it, whereas identity files may require passphrases.
1838 */
1839 if (try_agent_authentication())
1840 goto success;
1841
1842 /* Try RSA authentication for each identity. */
1843 for (i = 0; i < options.num_identity_files; i++)
1844 if (options.identity_keys[i] != NULL &&
1845 options.identity_keys[i]->type == KEY_RSA1 &&
1846 try_rsa_authentication(i))
1847 goto success;
1848 }
1849 /* Try challenge response authentication if the server supports it. */
1850 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1851 options.challenge_response_authentication && !options.batch_mode) {
1852 if (try_challenge_response_authentication())
1853 goto success;
1854 }
1855 /* Try password authentication if the server supports it. */
1856 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1857 options.password_authentication && !options.batch_mode) {
1858 char prompt[80];
1859
1860 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1861 server_user, host);
1862 if (try_password_authentication(prompt))
1863 goto success;
1864 }
1865 /* All authentication methods have failed. Exit with an error message. */
1866 fatal("Permission denied.");
1867 /* NOTREACHED */
1868
1869 success:
1870#ifdef KRB5
1871 /* Try Kerberos v5 TGT passing. */
1872 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1873 options.kerberos_tgt_passing && context && auth_context) {
1874 if (options.cipher == SSH_CIPHER_NONE)
1875 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1876 send_krb5_tgt(context, auth_context);
1877 }
1878 if (auth_context)
1879 krb5_auth_con_free(context, auth_context);
1880 if (context)
1881 krb5_free_context(context);
1882#endif
1e608e42 1883
3c0ef626 1884#ifdef AFS
1885 /* Try Kerberos v4 TGT passing if the server supports it. */
1886 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1887 options.kerberos_tgt_passing) {
1888 if (options.cipher == SSH_CIPHER_NONE)
1889 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1890 send_krb4_tgt();
1891 }
1892 /* Try AFS token passing if the server supports it. */
1893 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1894 options.afs_token_passing && k_hasafs()) {
1895 if (options.cipher == SSH_CIPHER_NONE)
1896 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1897 send_afs_tokens();
1898 }
1899#endif /* AFS */
1900
1901 return; /* need statement after label */
1902}
This page took 0.404313 seconds and 5 git commands to generate.