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.14 2001/01/08 21:55:41 markus Exp $");
18 #include <openssl/bn.h>
19 #include <openssl/dsa.h>
20 #include <openssl/rsa.h>
21 #include <openssl/evp.h>
33 #include "sshconnect.h"
36 /* Session id for the current session. */
37 u_char session_id[16];
38 u_int supported_authentications = 0;
40 extern Options options;
41 extern char *__progname;
44 * Checks if the user has an authentication agent, and if so, tries to
45 * authenticate using the agent.
48 try_agent_authentication()
52 AuthenticationConnection *auth;
59 /* Get connection to the agent. */
60 auth = ssh_get_authentication_connection();
66 /* Loop through identities served by the agent. */
67 for (key = ssh_get_first_identity(auth, &comment, 1);
69 key = ssh_get_next_identity(auth, &comment, 1)) {
71 /* Try this identity. */
72 debug("Trying RSA authentication via agent with '%.100s'", comment);
75 /* Tell the server that we are willing to authenticate using this key. */
76 packet_start(SSH_CMSG_AUTH_RSA);
77 packet_put_bignum(key->rsa->n);
81 /* Wait for server's response. */
82 type = packet_read(&plen);
84 /* The server sends failure if it doesn\'t like our key or
85 does not support RSA authentication. */
86 if (type == SSH_SMSG_FAILURE) {
87 debug("Server refused our key.");
91 /* Otherwise it should have sent a challenge. */
92 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
93 packet_disconnect("Protocol error during RSA authentication: %d",
96 packet_get_bignum(challenge, &clen);
98 packet_integrity_check(plen, clen, type);
100 debug("Received RSA challenge from server.");
102 /* Ask the agent to decrypt the challenge. */
103 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
105 * The agent failed to authenticate this identifier
106 * although it advertised it supports this. Just
107 * return a wrong value.
109 log("Authentication agent failed to decrypt challenge.");
110 memset(response, 0, sizeof(response));
113 debug("Sending response to RSA challenge.");
115 /* Send the decrypted challenge back to the server. */
116 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
117 for (i = 0; i < 16; i++)
118 packet_put_char(response[i]);
122 /* Wait for response from the server. */
123 type = packet_read(&plen);
125 /* The server returns success if it accepted the authentication. */
126 if (type == SSH_SMSG_SUCCESS) {
127 ssh_close_authentication_connection(auth);
128 BN_clear_free(challenge);
129 debug("RSA authentication accepted by server.");
132 /* Otherwise it should return failure. */
133 if (type != SSH_SMSG_FAILURE)
134 packet_disconnect("Protocol error waiting RSA auth response: %d",
137 ssh_close_authentication_connection(auth);
138 BN_clear_free(challenge);
139 debug("RSA authentication using agent refused.");
144 * Computes the proper response to a RSA challenge, and sends the response to
148 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
150 u_char buf[32], response[16];
154 /* Decrypt the challenge using the private key. */
155 rsa_private_decrypt(challenge, challenge, prv);
157 /* Compute the response. */
158 /* The response is MD5 of decrypted challenge plus session id. */
159 len = BN_num_bytes(challenge);
160 if (len <= 0 || len > sizeof(buf))
161 packet_disconnect("respond_to_rsa_challenge: bad challenge length %d",
164 memset(buf, 0, sizeof(buf));
165 BN_bn2bin(challenge, buf + sizeof(buf) - len);
167 MD5_Update(&md, buf, 32);
168 MD5_Update(&md, session_id, 16);
169 MD5_Final(response, &md);
171 debug("Sending response to host key RSA challenge.");
173 /* Send the response back to the server. */
174 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
175 for (i = 0; i < 16; i++)
176 packet_put_char(response[i]);
180 memset(buf, 0, sizeof(buf));
181 memset(response, 0, sizeof(response));
182 memset(&md, 0, sizeof(md));
186 * Checks if the user has authentication file, and if so, tries to authenticate
190 try_rsa_authentication(const char *authfile)
195 char *passphrase, *comment;
199 /* Try to load identification for the authentication key. */
200 public = key_new(KEY_RSA1);
201 if (!load_public_key(authfile, public, &comment)) {
203 /* Could not load it. Fail. */
206 debug("Trying RSA authentication with key '%.100s'", comment);
208 /* Tell the server that we are willing to authenticate using this key. */
209 packet_start(SSH_CMSG_AUTH_RSA);
210 packet_put_bignum(public->rsa->n);
214 /* We no longer need the public key. */
217 /* Wait for server's response. */
218 type = packet_read(&plen);
221 * The server responds with failure if it doesn\'t like our key or
222 * doesn\'t support RSA authentication.
224 if (type == SSH_SMSG_FAILURE) {
225 debug("Server refused our key.");
229 /* Otherwise, the server should respond with a challenge. */
230 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
231 packet_disconnect("Protocol error during RSA authentication: %d", type);
233 /* Get the challenge from the packet. */
234 challenge = BN_new();
235 packet_get_bignum(challenge, &clen);
237 packet_integrity_check(plen, clen, type);
239 debug("Received RSA challenge from server.");
241 private = key_new(KEY_RSA1);
243 * Load the private key. Try first with empty passphrase; if it
244 * fails, ask for a passphrase.
246 if (!load_private_key(authfile, "", private, NULL)) {
248 snprintf(buf, sizeof buf, "Enter passphrase for RSA key '%.100s': ",
250 if (!options.batch_mode)
251 passphrase = read_passphrase(buf, 0);
253 debug("Will not query passphrase for %.100s in batch mode.",
255 passphrase = xstrdup("");
258 /* Load the authentication file using the pasphrase. */
259 if (!load_private_key(authfile, passphrase, private, NULL)) {
260 memset(passphrase, 0, strlen(passphrase));
262 error("Bad passphrase.");
264 /* Send a dummy response packet to avoid protocol error. */
265 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
266 for (i = 0; i < 16; i++)
271 /* Expect the server to reject it... */
272 packet_read_expect(&plen, SSH_SMSG_FAILURE);
275 BN_clear_free(challenge);
278 /* Destroy the passphrase. */
279 memset(passphrase, 0, strlen(passphrase));
282 /* We no longer need the comment. */
285 /* Compute and send a response to the challenge. */
286 respond_to_rsa_challenge(challenge, private->rsa);
288 /* Destroy the private key. */
291 /* We no longer need the challenge. */
292 BN_clear_free(challenge);
294 /* Wait for response from the server. */
295 type = packet_read(&plen);
296 if (type == SSH_SMSG_SUCCESS) {
297 debug("RSA authentication accepted by server.");
300 if (type != SSH_SMSG_FAILURE)
301 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
302 debug("RSA authentication refused.");
307 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
308 * authentication and RSA host authentication.
311 try_rhosts_rsa_authentication(const char *local_user, RSA * host_key)
317 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
319 /* Tell the server that we are willing to authenticate using this key. */
320 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
321 packet_put_string(local_user, strlen(local_user));
322 packet_put_int(BN_num_bits(host_key->n));
323 packet_put_bignum(host_key->e);
324 packet_put_bignum(host_key->n);
328 /* Wait for server's response. */
329 type = packet_read(&plen);
331 /* The server responds with failure if it doesn't admit our
332 .rhosts authentication or doesn't know our host key. */
333 if (type == SSH_SMSG_FAILURE) {
334 debug("Server refused our rhosts authentication or host key.");
337 /* Otherwise, the server should respond with a challenge. */
338 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
339 packet_disconnect("Protocol error during RSA authentication: %d", type);
341 /* Get the challenge from the packet. */
342 challenge = BN_new();
343 packet_get_bignum(challenge, &clen);
345 packet_integrity_check(plen, clen, type);
347 debug("Received RSA challenge for host key from server.");
349 /* Compute a response to the challenge. */
350 respond_to_rsa_challenge(challenge, host_key);
352 /* We no longer need the challenge. */
353 BN_clear_free(challenge);
355 /* Wait for response from the server. */
356 type = packet_read(&plen);
357 if (type == SSH_SMSG_SUCCESS) {
358 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
361 if (type != SSH_SMSG_FAILURE)
362 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
363 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
369 try_kerberos_authentication()
371 KTEXT_ST auth; /* Kerberos data */
378 Key_schedule schedule;
379 u_long checksum, cksum;
381 struct sockaddr_in local, foreign;
384 /* Don't do anything if we don't have any tickets. */
385 if (stat(tkt_string(), &st) < 0)
388 strncpy(inst, (char *) krb_get_phost(get_canonical_hostname()), INST_SZ);
390 realm = (char *) krb_realmofhost(get_canonical_hostname());
392 debug("Kerberos V4: no realm for %s", get_canonical_hostname());
395 /* This can really be anything. */
396 checksum = (u_long) getpid();
398 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
400 debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]);
403 /* Get session key to decrypt the server's reply with. */
404 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
406 debug("get_cred failed: %s", krb_err_txt[r]);
409 des_key_sched((des_cblock *) cred.session, schedule);
411 /* Send authentication info to server. */
412 packet_start(SSH_CMSG_AUTH_KERBEROS);
413 packet_put_string((char *) auth.dat, auth.length);
417 /* Zero the buffer. */
418 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
420 slen = sizeof(local);
421 memset(&local, 0, sizeof(local));
422 if (getsockname(packet_get_connection_in(),
423 (struct sockaddr *) & local, &slen) < 0)
424 debug("getsockname failed: %s", strerror(errno));
426 slen = sizeof(foreign);
427 memset(&foreign, 0, sizeof(foreign));
428 if (getpeername(packet_get_connection_in(),
429 (struct sockaddr *) & foreign, &slen) < 0) {
430 debug("getpeername failed: %s", strerror(errno));
433 /* Get server reply. */
434 type = packet_read(&plen);
436 case SSH_SMSG_FAILURE:
437 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
438 debug("Kerberos V4 authentication failed.");
442 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
443 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
444 debug("Kerberos V4 authentication accepted.");
446 /* Get server's response. */
447 reply = packet_get_string((u_int *) &auth.length);
448 memcpy(auth.dat, reply, auth.length);
451 packet_integrity_check(plen, 4 + auth.length, type);
454 * If his response isn't properly encrypted with the session
455 * key, and the decrypted checksum fails to match, he's
458 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
459 &foreign, &local, &msg_data);
461 debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
462 packet_disconnect("Kerberos V4 challenge failed!");
464 /* Fetch the (incremented) checksum that we supplied in the request. */
465 (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum));
466 cksum = ntohl(cksum);
468 /* If it matches, we're golden. */
469 if (cksum == checksum + 1) {
470 debug("Kerberos V4 challenge successful.");
473 packet_disconnect("Kerberos V4 challenge failed!");
477 packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
489 char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
494 /* Don't do anything if we don't have any tickets. */
495 if (stat(tkt_string(), &st) < 0)
498 creds = xmalloc(sizeof(*creds));
500 if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
501 debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]);
504 if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
505 debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
508 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
509 debug("Kerberos V4 ticket expired: %s", TKT_FILE);
512 creds_to_radix(creds, (u_char *)buffer, sizeof buffer);
515 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
516 packet_put_string(buffer, strlen(buffer));
520 type = packet_read(&plen);
522 if (type == SSH_SMSG_FAILURE)
523 debug("Kerberos TGT for realm %s rejected.", prealm);
524 else if (type != SSH_SMSG_SUCCESS)
525 packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
531 send_afs_tokens(void)
534 struct ViceIoctl parms;
535 struct ClearToken ct;
536 int i, type, len, plen;
537 char buf[2048], *p, *server_cell;
540 /* Move over ktc_GetToken, here's something leaner. */
541 for (i = 0; i < 100; i++) { /* just in case */
542 parms.in = (char *) &i;
543 parms.in_size = sizeof(i);
545 parms.out_size = sizeof(buf);
546 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
550 /* Get secret token. */
551 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
552 if (creds.ticket_st.length > MAX_KTXT_LEN)
555 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
556 p += creds.ticket_st.length;
558 /* Get clear token. */
559 memcpy(&len, p, sizeof(len));
560 if (len != sizeof(struct ClearToken))
565 p += sizeof(len); /* primary flag */
568 /* Flesh out our credentials. */
569 strlcpy(creds.service, "afs", sizeof creds.service);
570 creds.instance[0] = '\0';
571 strlcpy(creds.realm, server_cell, REALM_SZ);
572 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
573 creds.issue_date = ct.BeginTimestamp;
574 creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
575 creds.kvno = ct.AuthHandle;
576 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
577 creds.pinst[0] = '\0';
579 /* Encode token, ship it off. */
580 if (creds_to_radix(&creds, (u_char*) buffer, sizeof buffer) <= 0)
582 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
583 packet_put_string(buffer, strlen(buffer));
587 /* Roger, Roger. Clearance, Clarence. What's your vector,
589 type = packet_read(&plen);
591 if (type == SSH_SMSG_FAILURE)
592 debug("AFS token for cell %s rejected.", server_cell);
593 else if (type != SSH_SMSG_SUCCESS)
594 packet_disconnect("Protocol error on AFS token response: %d", type);
601 * Tries to authenticate with any string-based challenge/response system.
602 * Note that the client code is not tied to s/key or TIS.
605 try_skey_authentication()
611 char *challenge, *response;
613 debug("Doing skey authentication.");
615 for (i = 0; i < options.number_of_password_prompts; i++) {
616 /* request a challenge */
617 packet_start(SSH_CMSG_AUTH_TIS);
621 type = packet_read(&payload_len);
622 if (type != SSH_SMSG_FAILURE &&
623 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
624 packet_disconnect("Protocol error: got %d in response "
625 "to skey-auth", type);
627 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
628 debug("No challenge for skey authentication.");
631 challenge = packet_get_string(&clen);
632 packet_integrity_check(payload_len, (4 + clen), type);
633 snprintf(prompt, sizeof prompt, "%s\nResponse: ", challenge);
636 error("Permission denied, please try again.");
637 if (options.cipher == SSH_CIPHER_NONE)
638 log("WARNING: Encryption is disabled! "
639 "Reponse will be transmitted in clear text.");
640 response = read_passphrase(prompt, 0);
641 if (strcmp(response, "") == 0) {
645 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
646 packet_put_string(response, strlen(response));
647 memset(response, 0, strlen(response));
651 type = packet_read(&payload_len);
652 if (type == SSH_SMSG_SUCCESS)
654 if (type != SSH_SMSG_FAILURE)
655 packet_disconnect("Protocol error: got %d in response "
656 "to skey-auth-reponse", type);
663 * Tries to authenticate with plain passwd authentication.
666 try_password_authentication(char *prompt)
668 int type, i, payload_len;
671 debug("Doing password authentication.");
672 if (options.cipher == SSH_CIPHER_NONE)
673 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
674 for (i = 0; i < options.number_of_password_prompts; i++) {
676 error("Permission denied, please try again.");
677 password = read_passphrase(prompt, 0);
678 packet_start(SSH_CMSG_AUTH_PASSWORD);
679 packet_put_string(password, strlen(password));
680 memset(password, 0, strlen(password));
685 type = packet_read(&payload_len);
686 if (type == SSH_SMSG_SUCCESS)
688 if (type != SSH_SMSG_FAILURE)
689 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
699 ssh_kex(char *host, struct sockaddr *hostaddr)
707 int ssh_cipher_default = SSH_CIPHER_3DES;
708 u_char session_key[SSH_SESSION_KEY_LENGTH];
710 u_int supported_ciphers;
711 u_int server_flags, client_flags;
712 int payload_len, clen, sum_len = 0;
715 debug("Waiting for server public key.");
717 /* Wait for a public key packet from the server. */
718 packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
720 /* Get cookie from the packet. */
721 for (i = 0; i < 8; i++)
722 cookie[i] = packet_get_char();
724 /* Get the public key. */
725 public_key = RSA_new();
726 bits = packet_get_int();/* bits */
727 public_key->e = BN_new();
728 packet_get_bignum(public_key->e, &clen);
730 public_key->n = BN_new();
731 packet_get_bignum(public_key->n, &clen);
734 rbits = BN_num_bits(public_key->n);
736 log("Warning: Server lies about size of server public key: "
737 "actual size is %d bits vs. announced %d.", rbits, bits);
738 log("Warning: This may be due to an old implementation of ssh.");
740 /* Get the host key. */
741 host_key = RSA_new();
742 bits = packet_get_int();/* bits */
743 host_key->e = BN_new();
744 packet_get_bignum(host_key->e, &clen);
746 host_key->n = BN_new();
747 packet_get_bignum(host_key->n, &clen);
750 rbits = BN_num_bits(host_key->n);
752 log("Warning: Server lies about size of server host key: "
753 "actual size is %d bits vs. announced %d.", rbits, bits);
754 log("Warning: This may be due to an old implementation of ssh.");
757 /* Get protocol flags. */
758 server_flags = packet_get_int();
759 packet_set_protocol_flags(server_flags);
761 supported_ciphers = packet_get_int();
762 supported_authentications = packet_get_int();
764 debug("Received server public key (%d bits) and host key (%d bits).",
765 BN_num_bits(public_key->n), BN_num_bits(host_key->n));
767 packet_integrity_check(payload_len,
768 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
769 SSH_SMSG_PUBLIC_KEY);
772 check_host_key(host, hostaddr, &k,
773 options.user_hostfile, options.system_hostfile);
775 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
777 compute_session_id(session_id, cookie, host_key->n, public_key->n);
779 /* Generate a session key. */
783 * Generate an encryption key for the session. The key is a 256 bit
784 * random number, interpreted as a 32-byte key, with the least
785 * significant 8 bits being the first byte of the key.
787 for (i = 0; i < 32; i++) {
790 session_key[i] = rand & 0xff;
795 * According to the protocol spec, the first byte of the session key
796 * is the highest byte of the integer. The session key is xored with
797 * the first 16 bytes of the session id.
801 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
802 BN_lshift(key, key, 8);
804 BN_add_word(key, session_key[i] ^ session_id[i]);
806 BN_add_word(key, session_key[i]);
810 * Encrypt the integer using the public key and host key of the
811 * server (key with smaller modulus first).
813 if (BN_cmp(public_key->n, host_key->n) < 0) {
814 /* Public key has smaller modulus. */
815 if (BN_num_bits(host_key->n) <
816 BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
817 fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
818 "SSH_KEY_BITS_RESERVED %d",
819 BN_num_bits(host_key->n),
820 BN_num_bits(public_key->n),
821 SSH_KEY_BITS_RESERVED);
823 rsa_public_encrypt(key, key, public_key);
824 rsa_public_encrypt(key, key, host_key);
826 /* Host key has smaller modulus (or they are equal). */
827 if (BN_num_bits(public_key->n) <
828 BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
829 fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
830 "SSH_KEY_BITS_RESERVED %d",
831 BN_num_bits(public_key->n),
832 BN_num_bits(host_key->n),
833 SSH_KEY_BITS_RESERVED);
835 rsa_public_encrypt(key, key, host_key);
836 rsa_public_encrypt(key, key, public_key);
839 /* Destroy the public keys since we no longer need them. */
840 RSA_free(public_key);
843 if (options.cipher == SSH_CIPHER_NOT_SET) {
844 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
845 options.cipher = ssh_cipher_default;
846 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
847 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
848 log("No valid SSH1 cipher, using %.100s instead.",
849 cipher_name(ssh_cipher_default));
850 options.cipher = ssh_cipher_default;
852 /* Check that the selected cipher is supported. */
853 if (!(supported_ciphers & (1 << options.cipher)))
854 fatal("Selected cipher type %.100s not supported by server.",
855 cipher_name(options.cipher));
857 debug("Encryption type: %.100s", cipher_name(options.cipher));
859 /* Send the encrypted session key to the server. */
860 packet_start(SSH_CMSG_SESSION_KEY);
861 packet_put_char(options.cipher);
863 /* Send the cookie back to the server. */
864 for (i = 0; i < 8; i++)
865 packet_put_char(cookie[i]);
867 /* Send and destroy the encrypted encryption key integer. */
868 packet_put_bignum(key);
871 /* Send protocol flags. */
872 packet_put_int(client_flags);
874 /* Send the packet now. */
878 debug("Sent encrypted session key.");
880 /* Set the encryption key. */
881 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
883 /* We will no longer need the session key here. Destroy any extra copies. */
884 memset(session_key, 0, sizeof(session_key));
887 * Expect a success message from the server. Note that this message
888 * will be received in encrypted form.
890 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
892 debug("Received encrypted confirmation.");
900 const char* local_user,
901 const char* server_user,
903 int host_key_valid, RSA *own_host_key)
908 if (supported_authentications == 0)
909 fatal("ssh_userauth: server supports no auth methods");
911 /* Send the name of the user to log in as on the server. */
912 packet_start(SSH_CMSG_USER);
913 packet_put_string(server_user, strlen(server_user));
918 * The server should respond with success if no authentication is
919 * needed (the user has no password). Otherwise the server responds
922 type = packet_read(&payload_len);
924 /* check whether the connection was accepted without authentication. */
925 if (type == SSH_SMSG_SUCCESS)
927 if (type != SSH_SMSG_FAILURE)
928 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
932 /* Try Kerberos tgt passing if the server supports it. */
933 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
934 options.kerberos_tgt_passing) {
935 if (options.cipher == SSH_CIPHER_NONE)
936 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
937 (void) send_kerberos_tgt();
939 /* Try AFS token passing if the server supports it. */
940 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
941 options.afs_token_passing && k_hasafs()) {
942 if (options.cipher == SSH_CIPHER_NONE)
943 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
949 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
950 options.kerberos_authentication) {
951 debug("Trying Kerberos authentication.");
952 if (try_kerberos_authentication()) {
953 /* The server should respond with success or failure. */
954 type = packet_read(&payload_len);
955 if (type == SSH_SMSG_SUCCESS)
957 if (type != SSH_SMSG_FAILURE)
958 packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
964 * Use rhosts authentication if running in privileged socket and we
965 * do not wish to remain anonymous.
967 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
968 options.rhosts_authentication) {
969 debug("Trying rhosts authentication.");
970 packet_start(SSH_CMSG_AUTH_RHOSTS);
971 packet_put_string(local_user, strlen(local_user));
975 /* The server should respond with success or failure. */
976 type = packet_read(&payload_len);
977 if (type == SSH_SMSG_SUCCESS)
979 if (type != SSH_SMSG_FAILURE)
980 packet_disconnect("Protocol error: got %d in response to rhosts auth",
984 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
987 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
988 options.rhosts_rsa_authentication && host_key_valid) {
989 if (try_rhosts_rsa_authentication(local_user, own_host_key))
992 /* Try RSA authentication if the server supports it. */
993 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
994 options.rsa_authentication) {
996 * Try RSA authentication using the authentication agent. The
997 * agent is tried first because no passphrase is needed for
998 * it, whereas identity files may require passphrases.
1000 if (try_agent_authentication())
1003 /* Try RSA authentication for each identity. */
1004 for (i = 0; i < options.num_identity_files; i++)
1005 if (options.identity_files_type[i] == KEY_RSA1 &&
1006 try_rsa_authentication(options.identity_files[i]))
1009 /* Try skey authentication if the server supports it. */
1010 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1011 options.skey_authentication && !options.batch_mode) {
1012 if (try_skey_authentication())
1015 /* Try password authentication if the server supports it. */
1016 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1017 options.password_authentication && !options.batch_mode) {
1020 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
1022 if (try_password_authentication(prompt))
1025 /* All authentication methods have failed. Exit with an error message. */
1026 fatal("Permission denied.");