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.6 2000/09/07 20:27:54 deraadt Exp $");
18 #include <openssl/bn.h>
19 #include <openssl/dsa.h>
20 #include <openssl/rsa.h>
21 #include <openssl/evp.h>
34 #include "sshconnect.h"
37 /* Session id for the current session. */
38 unsigned char session_id[16];
39 unsigned int supported_authentications = 0;
41 extern Options options;
42 extern char *__progname;
45 * Checks if the user has an authentication agent, and if so, tries to
46 * authenticate using the agent.
49 try_agent_authentication()
53 AuthenticationConnection *auth;
54 unsigned char response[16];
60 /* Get connection to the agent. */
61 auth = ssh_get_authentication_connection();
66 key = key_new(KEY_RSA);
68 /* Loop through identities served by the agent. */
69 for (key = ssh_get_first_identity(auth, &comment, 1);
71 key = ssh_get_next_identity(auth, &comment, 1)) {
73 /* Try this identity. */
74 debug("Trying RSA authentication via agent with '%.100s'", comment);
77 /* Tell the server that we are willing to authenticate using this key. */
78 packet_start(SSH_CMSG_AUTH_RSA);
79 packet_put_bignum(key->rsa->n);
83 /* Wait for server's response. */
84 type = packet_read(&plen);
86 /* The server sends failure if it doesn\'t like our key or
87 does not support RSA authentication. */
88 if (type == SSH_SMSG_FAILURE) {
89 debug("Server refused our key.");
93 /* Otherwise it should have sent a challenge. */
94 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
95 packet_disconnect("Protocol error during RSA authentication: %d",
98 packet_get_bignum(challenge, &clen);
100 packet_integrity_check(plen, clen, type);
102 debug("Received RSA challenge from server.");
104 /* Ask the agent to decrypt the challenge. */
105 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
107 * The agent failed to authenticate this identifier
108 * although it advertised it supports this. Just
109 * return a wrong value.
111 log("Authentication agent failed to decrypt challenge.");
112 memset(response, 0, sizeof(response));
115 debug("Sending response to RSA challenge.");
117 /* Send the decrypted challenge back to the server. */
118 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
119 for (i = 0; i < 16; i++)
120 packet_put_char(response[i]);
124 /* Wait for response from the server. */
125 type = packet_read(&plen);
127 /* The server returns success if it accepted the authentication. */
128 if (type == SSH_SMSG_SUCCESS) {
129 BN_clear_free(challenge);
130 debug("RSA authentication accepted by server.");
133 /* Otherwise it should return failure. */
134 if (type != SSH_SMSG_FAILURE)
135 packet_disconnect("Protocol error waiting RSA auth response: %d",
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 unsigned 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_RSA);
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_RSA);
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);
276 /* Destroy the passphrase. */
277 memset(passphrase, 0, strlen(passphrase));
280 /* We no longer need the comment. */
283 /* Compute and send a response to the challenge. */
284 respond_to_rsa_challenge(challenge, private->rsa);
286 /* Destroy the private key. */
289 /* We no longer need the challenge. */
290 BN_clear_free(challenge);
292 /* Wait for response from the server. */
293 type = packet_read(&plen);
294 if (type == SSH_SMSG_SUCCESS) {
295 debug("RSA authentication accepted by server.");
298 if (type != SSH_SMSG_FAILURE)
299 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
300 debug("RSA authentication refused.");
305 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
306 * authentication and RSA host authentication.
309 try_rhosts_rsa_authentication(const char *local_user, RSA * host_key)
315 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
317 /* Tell the server that we are willing to authenticate using this key. */
318 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
319 packet_put_string(local_user, strlen(local_user));
320 packet_put_int(BN_num_bits(host_key->n));
321 packet_put_bignum(host_key->e);
322 packet_put_bignum(host_key->n);
326 /* Wait for server's response. */
327 type = packet_read(&plen);
329 /* The server responds with failure if it doesn't admit our
330 .rhosts authentication or doesn't know our host key. */
331 if (type == SSH_SMSG_FAILURE) {
332 debug("Server refused our rhosts authentication or host key.");
335 /* Otherwise, the server should respond with a challenge. */
336 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
337 packet_disconnect("Protocol error during RSA authentication: %d", type);
339 /* Get the challenge from the packet. */
340 challenge = BN_new();
341 packet_get_bignum(challenge, &clen);
343 packet_integrity_check(plen, clen, type);
345 debug("Received RSA challenge for host key from server.");
347 /* Compute a response to the challenge. */
348 respond_to_rsa_challenge(challenge, host_key);
350 /* We no longer need the challenge. */
351 BN_clear_free(challenge);
353 /* Wait for response from the server. */
354 type = packet_read(&plen);
355 if (type == SSH_SMSG_SUCCESS) {
356 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
359 if (type != SSH_SMSG_FAILURE)
360 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
361 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
367 try_kerberos_authentication()
369 KTEXT_ST auth; /* Kerberos data */
376 Key_schedule schedule;
377 u_long checksum, cksum;
379 struct sockaddr_in local, foreign;
382 /* Don't do anything if we don't have any tickets. */
383 if (stat(tkt_string(), &st) < 0)
386 strncpy(inst, (char *) krb_get_phost(get_canonical_hostname()), INST_SZ);
388 realm = (char *) krb_realmofhost(get_canonical_hostname());
390 debug("Kerberos V4: no realm for %s", get_canonical_hostname());
393 /* This can really be anything. */
394 checksum = (u_long) getpid();
396 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
398 debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]);
401 /* Get session key to decrypt the server's reply with. */
402 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
404 debug("get_cred failed: %s", krb_err_txt[r]);
407 des_key_sched((des_cblock *) cred.session, schedule);
409 /* Send authentication info to server. */
410 packet_start(SSH_CMSG_AUTH_KERBEROS);
411 packet_put_string((char *) auth.dat, auth.length);
415 /* Zero the buffer. */
416 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
418 slen = sizeof(local);
419 memset(&local, 0, sizeof(local));
420 if (getsockname(packet_get_connection_in(),
421 (struct sockaddr *) & local, &slen) < 0)
422 debug("getsockname failed: %s", strerror(errno));
424 slen = sizeof(foreign);
425 memset(&foreign, 0, sizeof(foreign));
426 if (getpeername(packet_get_connection_in(),
427 (struct sockaddr *) & foreign, &slen) < 0) {
428 debug("getpeername failed: %s", strerror(errno));
431 /* Get server reply. */
432 type = packet_read(&plen);
434 case SSH_SMSG_FAILURE:
435 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
436 debug("Kerberos V4 authentication failed.");
440 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
441 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
442 debug("Kerberos V4 authentication accepted.");
444 /* Get server's response. */
445 reply = packet_get_string((unsigned int *) &auth.length);
446 memcpy(auth.dat, reply, auth.length);
449 packet_integrity_check(plen, 4 + auth.length, type);
452 * If his response isn't properly encrypted with the session
453 * key, and the decrypted checksum fails to match, he's
456 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
457 &foreign, &local, &msg_data);
459 debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
460 packet_disconnect("Kerberos V4 challenge failed!");
462 /* Fetch the (incremented) checksum that we supplied in the request. */
463 (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum));
464 cksum = ntohl(cksum);
466 /* If it matches, we're golden. */
467 if (cksum == checksum + 1) {
468 debug("Kerberos V4 challenge successful.");
471 packet_disconnect("Kerberos V4 challenge failed!");
475 packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
487 char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
492 /* Don't do anything if we don't have any tickets. */
493 if (stat(tkt_string(), &st) < 0)
496 creds = xmalloc(sizeof(*creds));
498 if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
499 debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]);
502 if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
503 debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
506 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
507 debug("Kerberos V4 ticket expired: %s", TKT_FILE);
510 creds_to_radix(creds, (unsigned char *)buffer, sizeof buffer);
513 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
514 packet_put_string(buffer, strlen(buffer));
518 type = packet_read(&plen);
520 if (type == SSH_SMSG_FAILURE)
521 debug("Kerberos TGT for realm %s rejected.", prealm);
522 else if (type != SSH_SMSG_SUCCESS)
523 packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
529 send_afs_tokens(void)
532 struct ViceIoctl parms;
533 struct ClearToken ct;
534 int i, type, len, plen;
535 char buf[2048], *p, *server_cell;
538 /* Move over ktc_GetToken, here's something leaner. */
539 for (i = 0; i < 100; i++) { /* just in case */
540 parms.in = (char *) &i;
541 parms.in_size = sizeof(i);
543 parms.out_size = sizeof(buf);
544 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
548 /* Get secret token. */
549 memcpy(&creds.ticket_st.length, p, sizeof(unsigned int));
550 if (creds.ticket_st.length > MAX_KTXT_LEN)
552 p += sizeof(unsigned int);
553 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
554 p += creds.ticket_st.length;
556 /* Get clear token. */
557 memcpy(&len, p, sizeof(len));
558 if (len != sizeof(struct ClearToken))
563 p += sizeof(len); /* primary flag */
566 /* Flesh out our credentials. */
567 strlcpy(creds.service, "afs", sizeof creds.service);
568 creds.instance[0] = '\0';
569 strlcpy(creds.realm, server_cell, REALM_SZ);
570 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
571 creds.issue_date = ct.BeginTimestamp;
572 creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
573 creds.kvno = ct.AuthHandle;
574 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
575 creds.pinst[0] = '\0';
577 /* Encode token, ship it off. */
578 if (creds_to_radix(&creds, (unsigned char*) buffer, sizeof buffer) <= 0)
580 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
581 packet_put_string(buffer, strlen(buffer));
585 /* Roger, Roger. Clearance, Clarence. What's your vector,
587 type = packet_read(&plen);
589 if (type == SSH_SMSG_FAILURE)
590 debug("AFS token for cell %s rejected.", server_cell);
591 else if (type != SSH_SMSG_SUCCESS)
592 packet_disconnect("Protocol error on AFS token response: %d", type);
599 * Tries to authenticate with any string-based challenge/response system.
600 * Note that the client code is not tied to s/key or TIS.
603 try_skey_authentication()
608 char *challenge, *response;
610 debug("Doing skey authentication.");
612 /* request a challenge */
613 packet_start(SSH_CMSG_AUTH_TIS);
617 type = packet_read(&payload_len);
618 if (type != SSH_SMSG_FAILURE &&
619 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
620 packet_disconnect("Protocol error: got %d in response "
621 "to skey-auth", type);
623 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
624 debug("No challenge for skey authentication.");
627 challenge = packet_get_string(&clen);
628 packet_integrity_check(payload_len, (4 + clen), type);
629 if (options.cipher == SSH_CIPHER_NONE)
630 log("WARNING: Encryption is disabled! "
631 "Reponse will be transmitted in clear text.");
632 fprintf(stderr, "%s\n", challenge);
635 for (i = 0; i < options.number_of_password_prompts; i++) {
637 error("Permission denied, please try again.");
638 response = read_passphrase("Response: ", 0);
639 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
640 packet_put_string(response, strlen(response));
641 memset(response, 0, strlen(response));
645 type = packet_read(&payload_len);
646 if (type == SSH_SMSG_SUCCESS)
648 if (type != SSH_SMSG_FAILURE)
649 packet_disconnect("Protocol error: got %d in response "
650 "to skey-auth-reponse", type);
657 * Tries to authenticate with plain passwd authentication.
660 try_password_authentication(char *prompt)
662 int type, i, payload_len;
665 debug("Doing password authentication.");
666 if (options.cipher == SSH_CIPHER_NONE)
667 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
668 for (i = 0; i < options.number_of_password_prompts; i++) {
670 error("Permission denied, please try again.");
671 password = read_passphrase(prompt, 0);
672 packet_start(SSH_CMSG_AUTH_PASSWORD);
673 packet_put_string(password, strlen(password));
674 memset(password, 0, strlen(password));
679 type = packet_read(&payload_len);
680 if (type == SSH_SMSG_SUCCESS)
682 if (type != SSH_SMSG_FAILURE)
683 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
693 ssh_kex(char *host, struct sockaddr *hostaddr)
701 int ssh_cipher_default = SSH_CIPHER_3DES;
702 unsigned char session_key[SSH_SESSION_KEY_LENGTH];
703 unsigned char cookie[8];
704 unsigned int supported_ciphers;
705 unsigned int server_flags, client_flags;
706 int payload_len, clen, sum_len = 0;
709 debug("Waiting for server public key.");
711 /* Wait for a public key packet from the server. */
712 packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
714 /* Get cookie from the packet. */
715 for (i = 0; i < 8; i++)
716 cookie[i] = packet_get_char();
718 /* Get the public key. */
719 public_key = RSA_new();
720 bits = packet_get_int();/* bits */
721 public_key->e = BN_new();
722 packet_get_bignum(public_key->e, &clen);
724 public_key->n = BN_new();
725 packet_get_bignum(public_key->n, &clen);
728 rbits = BN_num_bits(public_key->n);
730 log("Warning: Server lies about size of server public key: "
731 "actual size is %d bits vs. announced %d.", rbits, bits);
732 log("Warning: This may be due to an old implementation of ssh.");
734 /* Get the host key. */
735 host_key = RSA_new();
736 bits = packet_get_int();/* bits */
737 host_key->e = BN_new();
738 packet_get_bignum(host_key->e, &clen);
740 host_key->n = BN_new();
741 packet_get_bignum(host_key->n, &clen);
744 rbits = BN_num_bits(host_key->n);
746 log("Warning: Server lies about size of server host key: "
747 "actual size is %d bits vs. announced %d.", rbits, bits);
748 log("Warning: This may be due to an old implementation of ssh.");
751 /* Get protocol flags. */
752 server_flags = packet_get_int();
753 packet_set_protocol_flags(server_flags);
755 supported_ciphers = packet_get_int();
756 supported_authentications = packet_get_int();
758 debug("Received server public key (%d bits) and host key (%d bits).",
759 BN_num_bits(public_key->n), BN_num_bits(host_key->n));
761 packet_integrity_check(payload_len,
762 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
763 SSH_SMSG_PUBLIC_KEY);
766 check_host_key(host, hostaddr, &k,
767 options.user_hostfile, options.system_hostfile);
769 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
771 compute_session_id(session_id, cookie, host_key->n, public_key->n);
773 /* Generate a session key. */
777 * Generate an encryption key for the session. The key is a 256 bit
778 * random number, interpreted as a 32-byte key, with the least
779 * significant 8 bits being the first byte of the key.
781 for (i = 0; i < 32; i++) {
784 session_key[i] = rand & 0xff;
789 * According to the protocol spec, the first byte of the session key
790 * is the highest byte of the integer. The session key is xored with
791 * the first 16 bytes of the session id.
795 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
796 BN_lshift(key, key, 8);
798 BN_add_word(key, session_key[i] ^ session_id[i]);
800 BN_add_word(key, session_key[i]);
804 * Encrypt the integer using the public key and host key of the
805 * server (key with smaller modulus first).
807 if (BN_cmp(public_key->n, host_key->n) < 0) {
808 /* Public key has smaller modulus. */
809 if (BN_num_bits(host_key->n) <
810 BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
811 fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
812 "SSH_KEY_BITS_RESERVED %d",
813 BN_num_bits(host_key->n),
814 BN_num_bits(public_key->n),
815 SSH_KEY_BITS_RESERVED);
817 rsa_public_encrypt(key, key, public_key);
818 rsa_public_encrypt(key, key, host_key);
820 /* Host key has smaller modulus (or they are equal). */
821 if (BN_num_bits(public_key->n) <
822 BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
823 fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
824 "SSH_KEY_BITS_RESERVED %d",
825 BN_num_bits(public_key->n),
826 BN_num_bits(host_key->n),
827 SSH_KEY_BITS_RESERVED);
829 rsa_public_encrypt(key, key, host_key);
830 rsa_public_encrypt(key, key, public_key);
833 /* Destroy the public keys since we no longer need them. */
834 RSA_free(public_key);
837 if (options.cipher == SSH_CIPHER_ILLEGAL) {
838 log("No valid SSH1 cipher, using %.100s instead.",
839 cipher_name(SSH_FALLBACK_CIPHER));
840 options.cipher = SSH_FALLBACK_CIPHER;
841 } else if (options.cipher == SSH_CIPHER_NOT_SET) {
842 if (cipher_mask1() & supported_ciphers & (1 << ssh_cipher_default))
843 options.cipher = ssh_cipher_default;
845 debug("Cipher %s not supported, using %.100s instead.",
846 cipher_name(ssh_cipher_default),
847 cipher_name(SSH_FALLBACK_CIPHER));
848 options.cipher = SSH_FALLBACK_CIPHER;
851 /* Check that the selected cipher is supported. */
852 if (!(supported_ciphers & (1 << options.cipher)))
853 fatal("Selected cipher type %.100s not supported by server.",
854 cipher_name(options.cipher));
856 debug("Encryption type: %.100s", cipher_name(options.cipher));
858 /* Send the encrypted session key to the server. */
859 packet_start(SSH_CMSG_SESSION_KEY);
860 packet_put_char(options.cipher);
862 /* Send the cookie back to the server. */
863 for (i = 0; i < 8; i++)
864 packet_put_char(cookie[i]);
866 /* Send and destroy the encrypted encryption key integer. */
867 packet_put_bignum(key);
870 /* Send protocol flags. */
871 packet_put_int(client_flags);
873 /* Send the packet now. */
877 debug("Sent encrypted session key.");
879 /* Set the encryption key. */
880 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
882 /* We will no longer need the session key here. Destroy any extra copies. */
883 memset(session_key, 0, sizeof(session_key));
886 * Expect a success message from the server. Note that this message
887 * will be received in encrypted form.
889 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
891 debug("Received encrypted confirmation.");
899 const char* local_user,
900 const char* server_user,
902 int host_key_valid, RSA *own_host_key)
907 if (supported_authentications == 0)
908 fatal("ssh_userauth: server supports no auth methods");
910 /* Send the name of the user to log in as on the server. */
911 packet_start(SSH_CMSG_USER);
912 packet_put_string(server_user, strlen(server_user));
917 * The server should respond with success if no authentication is
918 * needed (the user has no password). Otherwise the server responds
921 type = packet_read(&payload_len);
923 /* check whether the connection was accepted without authentication. */
924 if (type == SSH_SMSG_SUCCESS)
926 if (type != SSH_SMSG_FAILURE)
927 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
931 /* Try Kerberos tgt passing if the server supports it. */
932 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
933 options.kerberos_tgt_passing) {
934 if (options.cipher == SSH_CIPHER_NONE)
935 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
936 (void) send_kerberos_tgt();
938 /* Try AFS token passing if the server supports it. */
939 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
940 options.afs_token_passing && k_hasafs()) {
941 if (options.cipher == SSH_CIPHER_NONE)
942 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
948 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
949 options.kerberos_authentication) {
950 debug("Trying Kerberos authentication.");
951 if (try_kerberos_authentication()) {
952 /* The server should respond with success or failure. */
953 type = packet_read(&payload_len);
954 if (type == SSH_SMSG_SUCCESS)
956 if (type != SSH_SMSG_FAILURE)
957 packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
963 * Use rhosts authentication if running in privileged socket and we
964 * do not wish to remain anonymous.
966 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
967 options.rhosts_authentication) {
968 debug("Trying rhosts authentication.");
969 packet_start(SSH_CMSG_AUTH_RHOSTS);
970 packet_put_string(local_user, strlen(local_user));
974 /* The server should respond with success or failure. */
975 type = packet_read(&payload_len);
976 if (type == SSH_SMSG_SUCCESS)
978 if (type != SSH_SMSG_FAILURE)
979 packet_disconnect("Protocol error: got %d in response to rhosts auth",
983 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
986 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
987 options.rhosts_rsa_authentication && host_key_valid) {
988 if (try_rhosts_rsa_authentication(local_user, own_host_key))
991 /* Try RSA authentication if the server supports it. */
992 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
993 options.rsa_authentication) {
995 * Try RSA authentication using the authentication agent. The
996 * agent is tried first because no passphrase is needed for
997 * it, whereas identity files may require passphrases.
999 if (try_agent_authentication())
1002 /* Try RSA authentication for each identity. */
1003 for (i = 0; i < options.num_identity_files; i++)
1004 if (try_rsa_authentication(options.identity_files[i]))
1007 /* Try skey authentication if the server supports it. */
1008 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1009 options.skey_authentication && !options.batch_mode) {
1010 if (try_skey_authentication())
1013 /* Try password authentication if the server supports it. */
1014 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1015 options.password_authentication && !options.batch_mode) {
1018 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
1020 if (try_password_authentication(prompt))
1023 /* All authentication methods have failed. Exit with an error message. */
1024 fatal("Permission denied.");