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.20 2001/01/22 23:06:40 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()
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 rsa_private_decrypt(challenge, challenge, prv);
168 /* Compute the response. */
169 /* The response is MD5 of decrypted challenge plus session id. */
170 len = BN_num_bytes(challenge);
171 if (len <= 0 || len > sizeof(buf))
172 packet_disconnect("respond_to_rsa_challenge: bad challenge length %d",
175 memset(buf, 0, sizeof(buf));
176 BN_bn2bin(challenge, buf + sizeof(buf) - len);
178 MD5_Update(&md, buf, 32);
179 MD5_Update(&md, session_id, 16);
180 MD5_Final(response, &md);
182 debug("Sending response to host key RSA challenge.");
184 /* Send the response back to the server. */
185 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
186 for (i = 0; i < 16; i++)
187 packet_put_char(response[i]);
191 memset(buf, 0, sizeof(buf));
192 memset(response, 0, sizeof(response));
193 memset(&md, 0, sizeof(md));
197 * Checks if the user has authentication file, and if so, tries to authenticate
201 try_rsa_authentication(const char *authfile)
206 char *passphrase, *comment;
210 /* Try to load identification for the authentication key. */
211 public = key_new(KEY_RSA1);
212 if (!load_public_key(authfile, public, &comment)) {
214 /* Could not load it. Fail. */
217 debug("Trying RSA authentication with key '%.100s'", comment);
219 /* Tell the server that we are willing to authenticate using this key. */
220 packet_start(SSH_CMSG_AUTH_RSA);
221 packet_put_bignum(public->rsa->n);
225 /* We no longer need the public key. */
228 /* Wait for server's response. */
229 type = packet_read(&plen);
232 * The server responds with failure if it doesn\'t like our key or
233 * doesn\'t support RSA authentication.
235 if (type == SSH_SMSG_FAILURE) {
236 debug("Server refused our key.");
240 /* Otherwise, the server should respond with a challenge. */
241 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
242 packet_disconnect("Protocol error during RSA authentication: %d", type);
244 /* Get the challenge from the packet. */
245 challenge = BN_new();
246 packet_get_bignum(challenge, &clen);
248 packet_integrity_check(plen, clen, type);
250 debug("Received RSA challenge from server.");
252 private = key_new(KEY_RSA1);
254 * Load the private key. Try first with empty passphrase; if it
255 * fails, ask for a passphrase.
257 if (!load_private_key(authfile, "", private, NULL)) {
259 snprintf(buf, sizeof buf, "Enter passphrase for RSA key '%.100s': ",
261 if (!options.batch_mode)
262 passphrase = read_passphrase(buf, 0);
264 debug("Will not query passphrase for %.100s in batch mode.",
266 passphrase = xstrdup("");
269 /* Load the authentication file using the pasphrase. */
270 if (!load_private_key(authfile, passphrase, private, NULL)) {
271 memset(passphrase, 0, strlen(passphrase));
273 error("Bad passphrase.");
275 /* Send a dummy response packet to avoid protocol error. */
276 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
277 for (i = 0; i < 16; i++)
282 /* Expect the server to reject it... */
283 packet_read_expect(&plen, SSH_SMSG_FAILURE);
286 BN_clear_free(challenge);
289 /* Destroy the passphrase. */
290 memset(passphrase, 0, strlen(passphrase));
293 /* We no longer need the comment. */
296 /* Compute and send a response to the challenge. */
297 respond_to_rsa_challenge(challenge, private->rsa);
299 /* Destroy the private key. */
302 /* We no longer need the challenge. */
303 BN_clear_free(challenge);
305 /* Wait for response from the server. */
306 type = packet_read(&plen);
307 if (type == SSH_SMSG_SUCCESS) {
308 debug("RSA authentication accepted by server.");
311 if (type != SSH_SMSG_FAILURE)
312 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
313 debug("RSA authentication refused.");
318 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
319 * authentication and RSA host authentication.
322 try_rhosts_rsa_authentication(const char *local_user, RSA * host_key)
328 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
330 /* Tell the server that we are willing to authenticate using this key. */
331 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
332 packet_put_string(local_user, strlen(local_user));
333 packet_put_int(BN_num_bits(host_key->n));
334 packet_put_bignum(host_key->e);
335 packet_put_bignum(host_key->n);
339 /* Wait for server's response. */
340 type = packet_read(&plen);
342 /* The server responds with failure if it doesn't admit our
343 .rhosts authentication or doesn't know our host key. */
344 if (type == SSH_SMSG_FAILURE) {
345 debug("Server refused our rhosts authentication or host key.");
348 /* Otherwise, the server should respond with a challenge. */
349 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
350 packet_disconnect("Protocol error during RSA authentication: %d", type);
352 /* Get the challenge from the packet. */
353 challenge = BN_new();
354 packet_get_bignum(challenge, &clen);
356 packet_integrity_check(plen, clen, type);
358 debug("Received RSA challenge for host key from server.");
360 /* Compute a response to the challenge. */
361 respond_to_rsa_challenge(challenge, host_key);
363 /* We no longer need the challenge. */
364 BN_clear_free(challenge);
366 /* Wait for response from the server. */
367 type = packet_read(&plen);
368 if (type == SSH_SMSG_SUCCESS) {
369 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
372 if (type != SSH_SMSG_FAILURE)
373 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
374 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
380 try_kerberos_authentication()
382 KTEXT_ST auth; /* Kerberos data */
389 Key_schedule schedule;
390 u_long checksum, cksum;
392 struct sockaddr_in local, foreign;
395 /* Don't do anything if we don't have any tickets. */
396 if (stat(tkt_string(), &st) < 0)
399 strncpy(inst, (char *) krb_get_phost(get_canonical_hostname()), INST_SZ);
401 realm = (char *) krb_realmofhost(get_canonical_hostname());
403 debug("Kerberos V4: no realm for %s", get_canonical_hostname());
406 /* This can really be anything. */
407 checksum = (u_long) getpid();
409 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
411 debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]);
414 /* Get session key to decrypt the server's reply with. */
415 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
417 debug("get_cred failed: %s", krb_err_txt[r]);
420 des_key_sched((des_cblock *) cred.session, schedule);
422 /* Send authentication info to server. */
423 packet_start(SSH_CMSG_AUTH_KERBEROS);
424 packet_put_string((char *) auth.dat, auth.length);
428 /* Zero the buffer. */
429 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
431 slen = sizeof(local);
432 memset(&local, 0, sizeof(local));
433 if (getsockname(packet_get_connection_in(),
434 (struct sockaddr *) & local, &slen) < 0)
435 debug("getsockname failed: %s", strerror(errno));
437 slen = sizeof(foreign);
438 memset(&foreign, 0, sizeof(foreign));
439 if (getpeername(packet_get_connection_in(),
440 (struct sockaddr *) & foreign, &slen) < 0) {
441 debug("getpeername failed: %s", strerror(errno));
444 /* Get server reply. */
445 type = packet_read(&plen);
447 case SSH_SMSG_FAILURE:
448 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
449 debug("Kerberos V4 authentication failed.");
453 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
454 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
455 debug("Kerberos V4 authentication accepted.");
457 /* Get server's response. */
458 reply = packet_get_string((u_int *) &auth.length);
459 memcpy(auth.dat, reply, auth.length);
462 packet_integrity_check(plen, 4 + auth.length, type);
465 * If his response isn't properly encrypted with the session
466 * key, and the decrypted checksum fails to match, he's
469 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
470 &foreign, &local, &msg_data);
472 debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
473 packet_disconnect("Kerberos V4 challenge failed!");
475 /* Fetch the (incremented) checksum that we supplied in the request. */
476 (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum));
477 cksum = ntohl(cksum);
479 /* If it matches, we're golden. */
480 if (cksum == checksum + 1) {
481 debug("Kerberos V4 challenge successful.");
484 packet_disconnect("Kerberos V4 challenge failed!");
488 packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
500 char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
505 /* Don't do anything if we don't have any tickets. */
506 if (stat(tkt_string(), &st) < 0)
509 creds = xmalloc(sizeof(*creds));
511 if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
512 debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]);
515 if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
516 debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
519 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
520 debug("Kerberos V4 ticket expired: %s", TKT_FILE);
523 creds_to_radix(creds, (u_char *)buffer, sizeof buffer);
526 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
527 packet_put_string(buffer, strlen(buffer));
531 type = packet_read(&plen);
533 if (type == SSH_SMSG_FAILURE)
534 debug("Kerberos TGT for realm %s rejected.", prealm);
535 else if (type != SSH_SMSG_SUCCESS)
536 packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
542 send_afs_tokens(void)
545 struct ViceIoctl parms;
546 struct ClearToken ct;
547 int i, type, len, plen;
548 char buf[2048], *p, *server_cell;
551 /* Move over ktc_GetToken, here's something leaner. */
552 for (i = 0; i < 100; i++) { /* just in case */
553 parms.in = (char *) &i;
554 parms.in_size = sizeof(i);
556 parms.out_size = sizeof(buf);
557 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
561 /* Get secret token. */
562 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
563 if (creds.ticket_st.length > MAX_KTXT_LEN)
566 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
567 p += creds.ticket_st.length;
569 /* Get clear token. */
570 memcpy(&len, p, sizeof(len));
571 if (len != sizeof(struct ClearToken))
576 p += sizeof(len); /* primary flag */
579 /* Flesh out our credentials. */
580 strlcpy(creds.service, "afs", sizeof creds.service);
581 creds.instance[0] = '\0';
582 strlcpy(creds.realm, server_cell, REALM_SZ);
583 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
584 creds.issue_date = ct.BeginTimestamp;
585 creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
586 creds.kvno = ct.AuthHandle;
587 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
588 creds.pinst[0] = '\0';
590 /* Encode token, ship it off. */
591 if (creds_to_radix(&creds, (u_char *) buffer, sizeof buffer) <= 0)
593 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
594 packet_put_string(buffer, strlen(buffer));
598 /* Roger, Roger. Clearance, Clarence. What's your vector,
600 type = packet_read(&plen);
602 if (type == SSH_SMSG_FAILURE)
603 debug("AFS token for cell %s rejected.", server_cell);
604 else if (type != SSH_SMSG_SUCCESS)
605 packet_disconnect("Protocol error on AFS token response: %d", type);
612 * Tries to authenticate with any string-based challenge/response system.
613 * Note that the client code is not tied to s/key or TIS.
616 try_challenge_reponse_authentication()
622 char *challenge, *response;
624 debug("Doing challenge reponse authentication.");
626 for (i = 0; i < options.number_of_password_prompts; i++) {
627 /* request a challenge */
628 packet_start(SSH_CMSG_AUTH_TIS);
632 type = packet_read(&payload_len);
633 if (type != SSH_SMSG_FAILURE &&
634 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
635 packet_disconnect("Protocol error: got %d in response "
636 "to SSH_CMSG_AUTH_TIS", type);
638 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
639 debug("No challenge.");
642 challenge = packet_get_string(&clen);
643 packet_integrity_check(payload_len, (4 + clen), type);
644 snprintf(prompt, sizeof prompt, "%s%s", challenge,
645 strchr(challenge, '\n') ? "" : "\nResponse: ");
648 error("Permission denied, please try again.");
649 if (options.cipher == SSH_CIPHER_NONE)
650 log("WARNING: Encryption is disabled! "
651 "Reponse will be transmitted in clear text.");
652 response = read_passphrase(prompt, 0);
653 if (strcmp(response, "") == 0) {
657 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
658 packet_put_string(response, strlen(response));
659 memset(response, 0, strlen(response));
663 type = packet_read(&payload_len);
664 if (type == SSH_SMSG_SUCCESS)
666 if (type != SSH_SMSG_FAILURE)
667 packet_disconnect("Protocol error: got %d in response "
668 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
675 * Tries to authenticate with plain passwd authentication.
678 try_password_authentication(char *prompt)
680 int type, i, payload_len;
683 debug("Doing password authentication.");
684 if (options.cipher == SSH_CIPHER_NONE)
685 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
686 for (i = 0; i < options.number_of_password_prompts; i++) {
688 error("Permission denied, please try again.");
689 password = read_passphrase(prompt, 0);
690 packet_start(SSH_CMSG_AUTH_PASSWORD);
691 packet_put_string(password, strlen(password));
692 memset(password, 0, strlen(password));
697 type = packet_read(&payload_len);
698 if (type == SSH_SMSG_SUCCESS)
700 if (type != SSH_SMSG_FAILURE)
701 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
711 ssh_kex(char *host, struct sockaddr *hostaddr)
719 int ssh_cipher_default = SSH_CIPHER_3DES;
720 u_char session_key[SSH_SESSION_KEY_LENGTH];
722 u_int supported_ciphers;
723 u_int server_flags, client_flags;
724 int payload_len, clen, sum_len = 0;
727 debug("Waiting for server public key.");
729 /* Wait for a public key packet from the server. */
730 packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
732 /* Get cookie from the packet. */
733 for (i = 0; i < 8; i++)
734 cookie[i] = packet_get_char();
736 /* Get the public key. */
737 public_key = RSA_new();
738 bits = packet_get_int();/* bits */
739 public_key->e = BN_new();
740 packet_get_bignum(public_key->e, &clen);
742 public_key->n = BN_new();
743 packet_get_bignum(public_key->n, &clen);
746 rbits = BN_num_bits(public_key->n);
748 log("Warning: Server lies about size of server public key: "
749 "actual size is %d bits vs. announced %d.", rbits, bits);
750 log("Warning: This may be due to an old implementation of ssh.");
752 /* Get the host key. */
753 host_key = RSA_new();
754 bits = packet_get_int();/* bits */
755 host_key->e = BN_new();
756 packet_get_bignum(host_key->e, &clen);
758 host_key->n = BN_new();
759 packet_get_bignum(host_key->n, &clen);
762 rbits = BN_num_bits(host_key->n);
764 log("Warning: Server lies about size of server host key: "
765 "actual size is %d bits vs. announced %d.", rbits, bits);
766 log("Warning: This may be due to an old implementation of ssh.");
769 /* Get protocol flags. */
770 server_flags = packet_get_int();
771 packet_set_protocol_flags(server_flags);
773 supported_ciphers = packet_get_int();
774 supported_authentications = packet_get_int();
776 debug("Received server public key (%d bits) and host key (%d bits).",
777 BN_num_bits(public_key->n), BN_num_bits(host_key->n));
779 packet_integrity_check(payload_len,
780 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
781 SSH_SMSG_PUBLIC_KEY);
784 check_host_key(host, hostaddr, &k,
785 options.user_hostfile, options.system_hostfile);
787 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
789 compute_session_id(session_id, cookie, host_key->n, public_key->n);
791 /* Generate a session key. */
795 * Generate an encryption key for the session. The key is a 256 bit
796 * random number, interpreted as a 32-byte key, with the least
797 * significant 8 bits being the first byte of the key.
799 for (i = 0; i < 32; i++) {
802 session_key[i] = rand & 0xff;
807 * According to the protocol spec, the first byte of the session key
808 * is the highest byte of the integer. The session key is xored with
809 * the first 16 bytes of the session id.
813 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
814 BN_lshift(key, key, 8);
816 BN_add_word(key, session_key[i] ^ session_id[i]);
818 BN_add_word(key, session_key[i]);
822 * Encrypt the integer using the public key and host key of the
823 * server (key with smaller modulus first).
825 if (BN_cmp(public_key->n, host_key->n) < 0) {
826 /* Public key has smaller modulus. */
827 if (BN_num_bits(host_key->n) <
828 BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
829 fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
830 "SSH_KEY_BITS_RESERVED %d",
831 BN_num_bits(host_key->n),
832 BN_num_bits(public_key->n),
833 SSH_KEY_BITS_RESERVED);
835 rsa_public_encrypt(key, key, public_key);
836 rsa_public_encrypt(key, key, host_key);
838 /* Host key has smaller modulus (or they are equal). */
839 if (BN_num_bits(public_key->n) <
840 BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
841 fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
842 "SSH_KEY_BITS_RESERVED %d",
843 BN_num_bits(public_key->n),
844 BN_num_bits(host_key->n),
845 SSH_KEY_BITS_RESERVED);
847 rsa_public_encrypt(key, key, host_key);
848 rsa_public_encrypt(key, key, public_key);
851 /* Destroy the public keys since we no longer need them. */
852 RSA_free(public_key);
855 if (options.cipher == SSH_CIPHER_NOT_SET) {
856 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
857 options.cipher = ssh_cipher_default;
858 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
859 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
860 log("No valid SSH1 cipher, using %.100s instead.",
861 cipher_name(ssh_cipher_default));
862 options.cipher = ssh_cipher_default;
864 /* Check that the selected cipher is supported. */
865 if (!(supported_ciphers & (1 << options.cipher)))
866 fatal("Selected cipher type %.100s not supported by server.",
867 cipher_name(options.cipher));
869 debug("Encryption type: %.100s", cipher_name(options.cipher));
871 /* Send the encrypted session key to the server. */
872 packet_start(SSH_CMSG_SESSION_KEY);
873 packet_put_char(options.cipher);
875 /* Send the cookie back to the server. */
876 for (i = 0; i < 8; i++)
877 packet_put_char(cookie[i]);
879 /* Send and destroy the encrypted encryption key integer. */
880 packet_put_bignum(key);
883 /* Send protocol flags. */
884 packet_put_int(client_flags);
886 /* Send the packet now. */
890 debug("Sent encrypted session key.");
892 /* Set the encryption key. */
893 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
895 /* We will no longer need the session key here. Destroy any extra copies. */
896 memset(session_key, 0, sizeof(session_key));
899 * Expect a success message from the server. Note that this message
900 * will be received in encrypted form.
902 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
904 debug("Received encrypted confirmation.");
912 const char *local_user,
913 const char *server_user,
915 int host_key_valid, RSA *own_host_key)
920 if (supported_authentications == 0)
921 fatal("ssh_userauth: server supports no auth methods");
923 /* Send the name of the user to log in as on the server. */
924 packet_start(SSH_CMSG_USER);
925 packet_put_string(server_user, strlen(server_user));
930 * The server should respond with success if no authentication is
931 * needed (the user has no password). Otherwise the server responds
934 type = packet_read(&payload_len);
936 /* check whether the connection was accepted without authentication. */
937 if (type == SSH_SMSG_SUCCESS)
939 if (type != SSH_SMSG_FAILURE)
940 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
944 /* Try Kerberos tgt passing if the server supports it. */
945 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
946 options.kerberos_tgt_passing) {
947 if (options.cipher == SSH_CIPHER_NONE)
948 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
949 (void) send_kerberos_tgt();
951 /* Try AFS token passing if the server supports it. */
952 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
953 options.afs_token_passing && k_hasafs()) {
954 if (options.cipher == SSH_CIPHER_NONE)
955 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
961 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
962 options.kerberos_authentication) {
963 debug("Trying Kerberos authentication.");
964 if (try_kerberos_authentication()) {
965 /* The server should respond with success or failure. */
966 type = packet_read(&payload_len);
967 if (type == SSH_SMSG_SUCCESS)
969 if (type != SSH_SMSG_FAILURE)
970 packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
976 * Use rhosts authentication if running in privileged socket and we
977 * do not wish to remain anonymous.
979 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
980 options.rhosts_authentication) {
981 debug("Trying rhosts authentication.");
982 packet_start(SSH_CMSG_AUTH_RHOSTS);
983 packet_put_string(local_user, strlen(local_user));
987 /* The server should respond with success or failure. */
988 type = packet_read(&payload_len);
989 if (type == SSH_SMSG_SUCCESS)
991 if (type != SSH_SMSG_FAILURE)
992 packet_disconnect("Protocol error: got %d in response to rhosts auth",
996 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
999 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1000 options.rhosts_rsa_authentication && host_key_valid) {
1001 if (try_rhosts_rsa_authentication(local_user, own_host_key))
1004 /* Try RSA authentication if the server supports it. */
1005 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1006 options.rsa_authentication) {
1008 * Try RSA authentication using the authentication agent. The
1009 * agent is tried first because no passphrase is needed for
1010 * it, whereas identity files may require passphrases.
1012 if (try_agent_authentication())
1015 /* Try RSA authentication for each identity. */
1016 for (i = 0; i < options.num_identity_files; i++)
1017 if (options.identity_files_type[i] == KEY_RSA1 &&
1018 try_rsa_authentication(options.identity_files[i]))
1021 /* Try challenge response authentication if the server supports it. */
1022 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1023 options.challenge_reponse_authentication && !options.batch_mode) {
1024 if (try_challenge_reponse_authentication())
1027 /* Try password authentication if the server supports it. */
1028 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1029 options.password_authentication && !options.batch_mode) {
1032 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
1034 if (try_password_authentication(prompt))
1037 /* All authentication methods have failed. Exit with an error message. */
1038 fatal("Permission denied.");