2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
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".
16 RCSID("$OpenBSD: sshconnect1.c,v 1.27 2001/02/15 23:19:59 markus Exp $");
18 #include <openssl/bn.h>
19 #include <openssl/evp.h>
41 #include "sshconnect.h"
47 /* Session id for the current session. */
48 u_char session_id[16];
49 u_int supported_authentications = 0;
51 extern Options options;
52 extern char *__progname;
55 * Checks if the user has an authentication agent, and if so, tries to
56 * authenticate using the agent.
59 try_agent_authentication(void)
63 AuthenticationConnection *auth;
70 /* Get connection to the agent. */
71 auth = ssh_get_authentication_connection();
77 /* Loop through identities served by the agent. */
78 for (key = ssh_get_first_identity(auth, &comment, 1);
80 key = ssh_get_next_identity(auth, &comment, 1)) {
82 /* Try this identity. */
83 debug("Trying RSA authentication via agent with '%.100s'", comment);
86 /* Tell the server that we are willing to authenticate using this key. */
87 packet_start(SSH_CMSG_AUTH_RSA);
88 packet_put_bignum(key->rsa->n);
92 /* Wait for server's response. */
93 type = packet_read(&plen);
95 /* The server sends failure if it doesn\'t like our key or
96 does not support RSA authentication. */
97 if (type == SSH_SMSG_FAILURE) {
98 debug("Server refused our key.");
102 /* Otherwise it should have sent a challenge. */
103 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
104 packet_disconnect("Protocol error during RSA authentication: %d",
107 packet_get_bignum(challenge, &clen);
109 packet_integrity_check(plen, clen, type);
111 debug("Received RSA challenge from server.");
113 /* Ask the agent to decrypt the challenge. */
114 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
116 * The agent failed to authenticate this identifier
117 * although it advertised it supports this. Just
118 * return a wrong value.
120 log("Authentication agent failed to decrypt challenge.");
121 memset(response, 0, sizeof(response));
124 debug("Sending response to RSA challenge.");
126 /* Send the decrypted challenge back to the server. */
127 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
128 for (i = 0; i < 16; i++)
129 packet_put_char(response[i]);
133 /* Wait for response from the server. */
134 type = packet_read(&plen);
136 /* The server returns success if it accepted the authentication. */
137 if (type == SSH_SMSG_SUCCESS) {
138 ssh_close_authentication_connection(auth);
139 BN_clear_free(challenge);
140 debug("RSA authentication accepted by server.");
143 /* Otherwise it should return failure. */
144 if (type != SSH_SMSG_FAILURE)
145 packet_disconnect("Protocol error waiting RSA auth response: %d",
148 ssh_close_authentication_connection(auth);
149 BN_clear_free(challenge);
150 debug("RSA authentication using agent refused.");
155 * Computes the proper response to a RSA challenge, and sends the response to
159 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
161 u_char buf[32], response[16];
165 /* Decrypt the challenge using the private key. */
166 /* XXX think about Bleichenbacher, too */
167 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
169 "respond_to_rsa_challenge: rsa_private_decrypt failed");
171 /* Compute the response. */
172 /* The response is MD5 of decrypted challenge plus session id. */
173 len = BN_num_bytes(challenge);
174 if (len <= 0 || len > sizeof(buf))
176 "respond_to_rsa_challenge: bad challenge length %d", len);
178 memset(buf, 0, sizeof(buf));
179 BN_bn2bin(challenge, buf + sizeof(buf) - len);
181 MD5_Update(&md, buf, 32);
182 MD5_Update(&md, session_id, 16);
183 MD5_Final(response, &md);
185 debug("Sending response to host key RSA challenge.");
187 /* Send the response back to the server. */
188 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
189 for (i = 0; i < 16; i++)
190 packet_put_char(response[i]);
194 memset(buf, 0, sizeof(buf));
195 memset(response, 0, sizeof(response));
196 memset(&md, 0, sizeof(md));
200 * Checks if the user has authentication file, and if so, tries to authenticate
204 try_rsa_authentication(const char *authfile)
209 char *passphrase, *comment;
213 /* Try to load identification for the authentication key. */
214 public = key_new(KEY_RSA1);
215 if (!load_public_key(authfile, public, &comment)) {
217 /* Could not load it. Fail. */
220 debug("Trying RSA authentication with key '%.100s'", comment);
222 /* Tell the server that we are willing to authenticate using this key. */
223 packet_start(SSH_CMSG_AUTH_RSA);
224 packet_put_bignum(public->rsa->n);
228 /* We no longer need the public key. */
231 /* Wait for server's response. */
232 type = packet_read(&plen);
235 * The server responds with failure if it doesn\'t like our key or
236 * doesn\'t support RSA authentication.
238 if (type == SSH_SMSG_FAILURE) {
239 debug("Server refused our key.");
243 /* Otherwise, the server should respond with a challenge. */
244 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
245 packet_disconnect("Protocol error during RSA authentication: %d", type);
247 /* Get the challenge from the packet. */
248 challenge = BN_new();
249 packet_get_bignum(challenge, &clen);
251 packet_integrity_check(plen, clen, type);
253 debug("Received RSA challenge from server.");
255 private = key_new(KEY_RSA1);
257 * Load the private key. Try first with empty passphrase; if it
258 * fails, ask for a passphrase.
260 if (!load_private_key(authfile, "", private, NULL)) {
262 snprintf(buf, sizeof buf, "Enter passphrase for RSA key '%.100s': ",
264 if (!options.batch_mode)
265 passphrase = read_passphrase(buf, 0);
267 debug("Will not query passphrase for %.100s in batch mode.",
269 passphrase = xstrdup("");
272 /* Load the authentication file using the pasphrase. */
273 if (!load_private_key(authfile, passphrase, private, NULL)) {
274 memset(passphrase, 0, strlen(passphrase));
276 error("Bad passphrase.");
278 /* Send a dummy response packet to avoid protocol error. */
279 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
280 for (i = 0; i < 16; i++)
285 /* Expect the server to reject it... */
286 packet_read_expect(&plen, SSH_SMSG_FAILURE);
289 BN_clear_free(challenge);
292 /* Destroy the passphrase. */
293 memset(passphrase, 0, strlen(passphrase));
296 /* We no longer need the comment. */
299 /* Compute and send a response to the challenge. */
300 respond_to_rsa_challenge(challenge, private->rsa);
302 /* Destroy the private key. */
305 /* We no longer need the challenge. */
306 BN_clear_free(challenge);
308 /* Wait for response from the server. */
309 type = packet_read(&plen);
310 if (type == SSH_SMSG_SUCCESS) {
311 debug("RSA authentication accepted by server.");
314 if (type != SSH_SMSG_FAILURE)
315 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
316 debug("RSA authentication refused.");
321 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
322 * authentication and RSA host authentication.
325 try_rhosts_rsa_authentication(const char *local_user, RSA * host_key)
331 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
333 /* Tell the server that we are willing to authenticate using this key. */
334 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
335 packet_put_string(local_user, strlen(local_user));
336 packet_put_int(BN_num_bits(host_key->n));
337 packet_put_bignum(host_key->e);
338 packet_put_bignum(host_key->n);
342 /* Wait for server's response. */
343 type = packet_read(&plen);
345 /* The server responds with failure if it doesn't admit our
346 .rhosts authentication or doesn't know our host key. */
347 if (type == SSH_SMSG_FAILURE) {
348 debug("Server refused our rhosts authentication or host key.");
351 /* Otherwise, the server should respond with a challenge. */
352 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
353 packet_disconnect("Protocol error during RSA authentication: %d", type);
355 /* Get the challenge from the packet. */
356 challenge = BN_new();
357 packet_get_bignum(challenge, &clen);
359 packet_integrity_check(plen, clen, type);
361 debug("Received RSA challenge for host key from server.");
363 /* Compute a response to the challenge. */
364 respond_to_rsa_challenge(challenge, host_key);
366 /* We no longer need the challenge. */
367 BN_clear_free(challenge);
369 /* Wait for response from the server. */
370 type = packet_read(&plen);
371 if (type == SSH_SMSG_SUCCESS) {
372 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
375 if (type != SSH_SMSG_FAILURE)
376 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
377 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
383 try_kerberos_authentication(void)
385 KTEXT_ST auth; /* Kerberos data */
392 Key_schedule schedule;
393 u_long checksum, cksum;
395 struct sockaddr_in local, foreign;
398 /* Don't do anything if we don't have any tickets. */
399 if (stat(tkt_string(), &st) < 0)
402 strncpy(inst, (char *) krb_get_phost(get_canonical_hostname(1)), INST_SZ);
404 realm = (char *) krb_realmofhost(get_canonical_hostname(1));
406 debug("Kerberos V4: no realm for %s", get_canonical_hostname(1));
409 /* This can really be anything. */
410 checksum = (u_long) getpid();
412 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
414 debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]);
417 /* Get session key to decrypt the server's reply with. */
418 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
420 debug("get_cred failed: %s", krb_err_txt[r]);
423 des_key_sched((des_cblock *) cred.session, schedule);
425 /* Send authentication info to server. */
426 packet_start(SSH_CMSG_AUTH_KERBEROS);
427 packet_put_string((char *) auth.dat, auth.length);
431 /* Zero the buffer. */
432 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
434 slen = sizeof(local);
435 memset(&local, 0, sizeof(local));
436 if (getsockname(packet_get_connection_in(),
437 (struct sockaddr *) & local, &slen) < 0)
438 debug("getsockname failed: %s", strerror(errno));
440 slen = sizeof(foreign);
441 memset(&foreign, 0, sizeof(foreign));
442 if (getpeername(packet_get_connection_in(),
443 (struct sockaddr *) & foreign, &slen) < 0) {
444 debug("getpeername failed: %s", strerror(errno));
447 /* Get server reply. */
448 type = packet_read(&plen);
450 case SSH_SMSG_FAILURE:
451 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
452 debug("Kerberos V4 authentication failed.");
456 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
457 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
458 debug("Kerberos V4 authentication accepted.");
460 /* Get server's response. */
461 reply = packet_get_string((u_int *) &auth.length);
462 memcpy(auth.dat, reply, auth.length);
465 packet_integrity_check(plen, 4 + auth.length, type);
468 * If his response isn't properly encrypted with the session
469 * key, and the decrypted checksum fails to match, he's
472 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
473 &foreign, &local, &msg_data);
475 debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
476 packet_disconnect("Kerberos V4 challenge failed!");
478 /* Fetch the (incremented) checksum that we supplied in the request. */
479 (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum));
480 cksum = ntohl(cksum);
482 /* If it matches, we're golden. */
483 if (cksum == checksum + 1) {
484 debug("Kerberos V4 challenge successful.");
487 packet_disconnect("Kerberos V4 challenge failed!");
491 packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
500 send_kerberos_tgt(void)
503 char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
508 /* Don't do anything if we don't have any tickets. */
509 if (stat(tkt_string(), &st) < 0)
512 creds = xmalloc(sizeof(*creds));
514 if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
515 debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]);
518 if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
519 debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
522 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
523 debug("Kerberos V4 ticket expired: %s", TKT_FILE);
526 creds_to_radix(creds, (u_char *)buffer, sizeof buffer);
529 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
530 packet_put_string(buffer, strlen(buffer));
534 type = packet_read(&plen);
536 if (type == SSH_SMSG_FAILURE)
537 debug("Kerberos TGT for realm %s rejected.", prealm);
538 else if (type != SSH_SMSG_SUCCESS)
539 packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
545 send_afs_tokens(void)
548 struct ViceIoctl parms;
549 struct ClearToken ct;
550 int i, type, len, plen;
551 char buf[2048], *p, *server_cell;
554 /* Move over ktc_GetToken, here's something leaner. */
555 for (i = 0; i < 100; i++) { /* just in case */
556 parms.in = (char *) &i;
557 parms.in_size = sizeof(i);
559 parms.out_size = sizeof(buf);
560 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
564 /* Get secret token. */
565 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
566 if (creds.ticket_st.length > MAX_KTXT_LEN)
569 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
570 p += creds.ticket_st.length;
572 /* Get clear token. */
573 memcpy(&len, p, sizeof(len));
574 if (len != sizeof(struct ClearToken))
579 p += sizeof(len); /* primary flag */
582 /* Flesh out our credentials. */
583 strlcpy(creds.service, "afs", sizeof creds.service);
584 creds.instance[0] = '\0';
585 strlcpy(creds.realm, server_cell, REALM_SZ);
586 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
587 creds.issue_date = ct.BeginTimestamp;
588 creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
589 creds.kvno = ct.AuthHandle;
590 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
591 creds.pinst[0] = '\0';
593 /* Encode token, ship it off. */
594 if (creds_to_radix(&creds, (u_char *) buffer, sizeof buffer) <= 0)
596 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
597 packet_put_string(buffer, strlen(buffer));
601 /* Roger, Roger. Clearance, Clarence. What's your vector,
603 type = packet_read(&plen);
605 if (type == SSH_SMSG_FAILURE)
606 debug("AFS token for cell %s rejected.", server_cell);
607 else if (type != SSH_SMSG_SUCCESS)
608 packet_disconnect("Protocol error on AFS token response: %d", type);
615 * Tries to authenticate with any string-based challenge/response system.
616 * Note that the client code is not tied to s/key or TIS.
619 try_challenge_reponse_authentication(void)
625 char *challenge, *response;
627 debug("Doing challenge reponse authentication.");
629 for (i = 0; i < options.number_of_password_prompts; i++) {
630 /* request a challenge */
631 packet_start(SSH_CMSG_AUTH_TIS);
635 type = packet_read(&payload_len);
636 if (type != SSH_SMSG_FAILURE &&
637 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
638 packet_disconnect("Protocol error: got %d in response "
639 "to SSH_CMSG_AUTH_TIS", type);
641 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
642 debug("No challenge.");
645 challenge = packet_get_string(&clen);
646 packet_integrity_check(payload_len, (4 + clen), type);
647 snprintf(prompt, sizeof prompt, "%s%s", challenge,
648 strchr(challenge, '\n') ? "" : "\nResponse: ");
651 error("Permission denied, please try again.");
652 if (options.cipher == SSH_CIPHER_NONE)
653 log("WARNING: Encryption is disabled! "
654 "Reponse will be transmitted in clear text.");
655 response = read_passphrase(prompt, 0);
656 if (strcmp(response, "") == 0) {
660 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
661 ssh_put_password(response);
662 memset(response, 0, strlen(response));
666 type = packet_read(&payload_len);
667 if (type == SSH_SMSG_SUCCESS)
669 if (type != SSH_SMSG_FAILURE)
670 packet_disconnect("Protocol error: got %d in response "
671 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
678 * Tries to authenticate with plain passwd authentication.
681 try_password_authentication(char *prompt)
683 int type, i, payload_len;
686 debug("Doing password authentication.");
687 if (options.cipher == SSH_CIPHER_NONE)
688 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
689 for (i = 0; i < options.number_of_password_prompts; i++) {
691 error("Permission denied, please try again.");
692 password = read_passphrase(prompt, 0);
693 packet_start(SSH_CMSG_AUTH_PASSWORD);
694 ssh_put_password(password);
695 memset(password, 0, strlen(password));
700 type = packet_read(&payload_len);
701 if (type == SSH_SMSG_SUCCESS)
703 if (type != SSH_SMSG_FAILURE)
704 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
714 ssh_kex(char *host, struct sockaddr *hostaddr)
722 int ssh_cipher_default = SSH_CIPHER_3DES;
723 u_char session_key[SSH_SESSION_KEY_LENGTH];
725 u_int supported_ciphers;
726 u_int server_flags, client_flags;
727 int payload_len, clen, sum_len = 0;
730 debug("Waiting for server public key.");
732 /* Wait for a public key packet from the server. */
733 packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
735 /* Get cookie from the packet. */
736 for (i = 0; i < 8; i++)
737 cookie[i] = packet_get_char();
739 /* Get the public key. */
740 public_key = RSA_new();
741 bits = packet_get_int();/* bits */
742 public_key->e = BN_new();
743 packet_get_bignum(public_key->e, &clen);
745 public_key->n = BN_new();
746 packet_get_bignum(public_key->n, &clen);
749 rbits = BN_num_bits(public_key->n);
751 log("Warning: Server lies about size of server public key: "
752 "actual size is %d bits vs. announced %d.", rbits, bits);
753 log("Warning: This may be due to an old implementation of ssh.");
755 /* Get the host key. */
756 host_key = RSA_new();
757 bits = packet_get_int();/* bits */
758 host_key->e = BN_new();
759 packet_get_bignum(host_key->e, &clen);
761 host_key->n = BN_new();
762 packet_get_bignum(host_key->n, &clen);
765 rbits = BN_num_bits(host_key->n);
767 log("Warning: Server lies about size of server host key: "
768 "actual size is %d bits vs. announced %d.", rbits, bits);
769 log("Warning: This may be due to an old implementation of ssh.");
772 /* Get protocol flags. */
773 server_flags = packet_get_int();
774 packet_set_protocol_flags(server_flags);
776 supported_ciphers = packet_get_int();
777 supported_authentications = packet_get_int();
779 debug("Received server public key (%d bits) and host key (%d bits).",
780 BN_num_bits(public_key->n), BN_num_bits(host_key->n));
782 packet_integrity_check(payload_len,
783 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
784 SSH_SMSG_PUBLIC_KEY);
787 check_host_key(host, hostaddr, &k,
788 options.user_hostfile, options.system_hostfile);
790 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
792 compute_session_id(session_id, cookie, host_key->n, public_key->n);
794 /* Generate a session key. */
798 * Generate an encryption key for the session. The key is a 256 bit
799 * random number, interpreted as a 32-byte key, with the least
800 * significant 8 bits being the first byte of the key.
802 for (i = 0; i < 32; i++) {
805 session_key[i] = rand & 0xff;
810 * According to the protocol spec, the first byte of the session key
811 * is the highest byte of the integer. The session key is xored with
812 * the first 16 bytes of the session id.
816 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
817 BN_lshift(key, key, 8);
819 BN_add_word(key, session_key[i] ^ session_id[i]);
821 BN_add_word(key, session_key[i]);
825 * Encrypt the integer using the public key and host key of the
826 * server (key with smaller modulus first).
828 if (BN_cmp(public_key->n, host_key->n) < 0) {
829 /* Public key has smaller modulus. */
830 if (BN_num_bits(host_key->n) <
831 BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
832 fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
833 "SSH_KEY_BITS_RESERVED %d",
834 BN_num_bits(host_key->n),
835 BN_num_bits(public_key->n),
836 SSH_KEY_BITS_RESERVED);
838 rsa_public_encrypt(key, key, public_key);
839 rsa_public_encrypt(key, key, host_key);
841 /* Host key has smaller modulus (or they are equal). */
842 if (BN_num_bits(public_key->n) <
843 BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
844 fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
845 "SSH_KEY_BITS_RESERVED %d",
846 BN_num_bits(public_key->n),
847 BN_num_bits(host_key->n),
848 SSH_KEY_BITS_RESERVED);
850 rsa_public_encrypt(key, key, host_key);
851 rsa_public_encrypt(key, key, public_key);
854 /* Destroy the public keys since we no longer need them. */
855 RSA_free(public_key);
858 if (options.cipher == SSH_CIPHER_NOT_SET) {
859 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
860 options.cipher = ssh_cipher_default;
861 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
862 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
863 log("No valid SSH1 cipher, using %.100s instead.",
864 cipher_name(ssh_cipher_default));
865 options.cipher = ssh_cipher_default;
867 /* Check that the selected cipher is supported. */
868 if (!(supported_ciphers & (1 << options.cipher)))
869 fatal("Selected cipher type %.100s not supported by server.",
870 cipher_name(options.cipher));
872 debug("Encryption type: %.100s", cipher_name(options.cipher));
874 /* Send the encrypted session key to the server. */
875 packet_start(SSH_CMSG_SESSION_KEY);
876 packet_put_char(options.cipher);
878 /* Send the cookie back to the server. */
879 for (i = 0; i < 8; i++)
880 packet_put_char(cookie[i]);
882 /* Send and destroy the encrypted encryption key integer. */
883 packet_put_bignum(key);
886 /* Send protocol flags. */
887 packet_put_int(client_flags);
889 /* Send the packet now. */
893 debug("Sent encrypted session key.");
895 /* Set the encryption key. */
896 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
898 /* We will no longer need the session key here. Destroy any extra copies. */
899 memset(session_key, 0, sizeof(session_key));
902 * Expect a success message from the server. Note that this message
903 * will be received in encrypted form.
905 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
907 debug("Received encrypted confirmation.");
915 const char *local_user,
916 const char *server_user,
918 int host_key_valid, RSA *own_host_key)
923 if (supported_authentications == 0)
924 fatal("ssh_userauth: server supports no auth methods");
926 /* Send the name of the user to log in as on the server. */
927 packet_start(SSH_CMSG_USER);
928 packet_put_string(server_user, strlen(server_user));
933 * The server should respond with success if no authentication is
934 * needed (the user has no password). Otherwise the server responds
937 type = packet_read(&payload_len);
939 /* check whether the connection was accepted without authentication. */
940 if (type == SSH_SMSG_SUCCESS)
942 if (type != SSH_SMSG_FAILURE)
943 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
947 /* Try Kerberos tgt passing if the server supports it. */
948 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
949 options.kerberos_tgt_passing) {
950 if (options.cipher == SSH_CIPHER_NONE)
951 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
952 (void) send_kerberos_tgt();
954 /* Try AFS token passing if the server supports it. */
955 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
956 options.afs_token_passing && k_hasafs()) {
957 if (options.cipher == SSH_CIPHER_NONE)
958 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
964 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
965 options.kerberos_authentication) {
966 debug("Trying Kerberos authentication.");
967 if (try_kerberos_authentication()) {
968 /* The server should respond with success or failure. */
969 type = packet_read(&payload_len);
970 if (type == SSH_SMSG_SUCCESS)
972 if (type != SSH_SMSG_FAILURE)
973 packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
979 * Use rhosts authentication if running in privileged socket and we
980 * do not wish to remain anonymous.
982 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
983 options.rhosts_authentication) {
984 debug("Trying rhosts authentication.");
985 packet_start(SSH_CMSG_AUTH_RHOSTS);
986 packet_put_string(local_user, strlen(local_user));
990 /* The server should respond with success or failure. */
991 type = packet_read(&payload_len);
992 if (type == SSH_SMSG_SUCCESS)
994 if (type != SSH_SMSG_FAILURE)
995 packet_disconnect("Protocol error: got %d in response to rhosts auth",
999 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1002 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1003 options.rhosts_rsa_authentication && host_key_valid) {
1004 if (try_rhosts_rsa_authentication(local_user, own_host_key))
1007 /* Try RSA authentication if the server supports it. */
1008 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1009 options.rsa_authentication) {
1011 * Try RSA authentication using the authentication agent. The
1012 * agent is tried first because no passphrase is needed for
1013 * it, whereas identity files may require passphrases.
1015 if (try_agent_authentication())
1018 /* Try RSA authentication for each identity. */
1019 for (i = 0; i < options.num_identity_files; i++)
1020 if (options.identity_files_type[i] == KEY_RSA1 &&
1021 try_rsa_authentication(options.identity_files[i]))
1024 /* Try challenge response authentication if the server supports it. */
1025 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1026 options.challenge_reponse_authentication && !options.batch_mode) {
1027 if (try_challenge_reponse_authentication())
1030 /* Try password authentication if the server supports it. */
1031 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1032 options.password_authentication && !options.batch_mode) {
1035 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1037 if (try_password_authentication(prompt))
1040 /* All authentication methods have failed. Exit with an error message. */
1041 fatal("Permission denied.");