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.47 2001/12/28 14:50:54 markus Exp $");
18 #include <openssl/bn.h>
19 #include <openssl/evp.h>
44 #include "sshconnect.h"
51 /* Session id for the current session. */
52 u_char session_id[16];
53 u_int supported_authentications = 0;
55 extern Options options;
56 extern char *__progname;
59 * Checks if the user has an authentication agent, and if so, tries to
60 * authenticate using the agent.
63 try_agent_authentication(void)
67 AuthenticationConnection *auth;
73 /* Get connection to the agent. */
74 auth = ssh_get_authentication_connection();
78 if ((challenge = BN_new()) == NULL)
79 fatal("try_agent_authentication: BN_new failed");
80 /* Loop through identities served by the agent. */
81 for (key = ssh_get_first_identity(auth, &comment, 1);
83 key = ssh_get_next_identity(auth, &comment, 1)) {
85 /* Try this identity. */
86 debug("Trying RSA authentication via agent with '%.100s'", comment);
89 /* Tell the server that we are willing to authenticate using this key. */
90 packet_start(SSH_CMSG_AUTH_RSA);
91 packet_put_bignum(key->rsa->n);
95 /* Wait for server's response. */
98 /* The server sends failure if it doesn\'t like our key or
99 does not support RSA authentication. */
100 if (type == SSH_SMSG_FAILURE) {
101 debug("Server refused our key.");
105 /* Otherwise it should have sent a challenge. */
106 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
107 packet_disconnect("Protocol error during RSA authentication: %d",
110 packet_get_bignum(challenge);
113 debug("Received RSA challenge from server.");
115 /* Ask the agent to decrypt the challenge. */
116 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
118 * The agent failed to authenticate this identifier
119 * although it advertised it supports this. Just
120 * return a wrong value.
122 log("Authentication agent failed to decrypt challenge.");
123 memset(response, 0, sizeof(response));
126 debug("Sending response to RSA challenge.");
128 /* Send the decrypted challenge back to the server. */
129 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
130 for (i = 0; i < 16; i++)
131 packet_put_char(response[i]);
135 /* Wait for response from the server. */
136 type = packet_read();
138 /* The server returns success if it accepted the authentication. */
139 if (type == SSH_SMSG_SUCCESS) {
140 ssh_close_authentication_connection(auth);
141 BN_clear_free(challenge);
142 debug("RSA authentication accepted by server.");
145 /* Otherwise it should return failure. */
146 if (type != SSH_SMSG_FAILURE)
147 packet_disconnect("Protocol error waiting RSA auth response: %d",
150 ssh_close_authentication_connection(auth);
151 BN_clear_free(challenge);
152 debug("RSA authentication using agent refused.");
157 * Computes the proper response to a RSA challenge, and sends the response to
161 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
163 u_char buf[32], response[16];
167 /* Decrypt the challenge using the private key. */
168 /* XXX think about Bleichenbacher, too */
169 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
171 "respond_to_rsa_challenge: rsa_private_decrypt failed");
173 /* Compute the response. */
174 /* The response is MD5 of decrypted challenge plus session id. */
175 len = BN_num_bytes(challenge);
176 if (len <= 0 || len > sizeof(buf))
178 "respond_to_rsa_challenge: bad challenge length %d", len);
180 memset(buf, 0, sizeof(buf));
181 BN_bn2bin(challenge, buf + sizeof(buf) - len);
183 MD5_Update(&md, buf, 32);
184 MD5_Update(&md, session_id, 16);
185 MD5_Final(response, &md);
187 debug("Sending response to host key RSA challenge.");
189 /* Send the response back to the server. */
190 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
191 for (i = 0; i < 16; i++)
192 packet_put_char(response[i]);
196 memset(buf, 0, sizeof(buf));
197 memset(response, 0, sizeof(response));
198 memset(&md, 0, sizeof(md));
202 * Checks if the user has authentication file, and if so, tries to authenticate
206 try_rsa_authentication(int idx)
209 Key *public, *private;
210 char buf[300], *passphrase, *comment, *authfile;
213 public = options.identity_keys[idx];
214 authfile = options.identity_files[idx];
215 comment = xstrdup(authfile);
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 /* Wait for server's response. */
226 type = packet_read();
229 * The server responds with failure if it doesn\'t like our key or
230 * doesn\'t support RSA authentication.
232 if (type == SSH_SMSG_FAILURE) {
233 debug("Server refused our key.");
237 /* Otherwise, the server should respond with a challenge. */
238 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
239 packet_disconnect("Protocol error during RSA authentication: %d", type);
241 /* Get the challenge from the packet. */
242 if ((challenge = BN_new()) == NULL)
243 fatal("try_rsa_authentication: BN_new failed");
244 packet_get_bignum(challenge);
247 debug("Received RSA challenge from server.");
250 * If the key is not stored in external hardware, we have to
251 * load the private key. Try first with empty passphrase; if it
252 * fails, ask for a passphrase.
254 if (public->flags && KEY_FLAG_EXT)
257 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
258 if (private == NULL && !options.batch_mode) {
259 snprintf(buf, sizeof(buf),
260 "Enter passphrase for RSA key '%.100s': ", comment);
261 for (i = 0; i < options.number_of_password_prompts; i++) {
262 passphrase = read_passphrase(buf, 0);
263 if (strcmp(passphrase, "") != 0) {
264 private = key_load_private_type(KEY_RSA1,
265 authfile, passphrase, NULL);
268 debug2("no passphrase given, try next key");
271 memset(passphrase, 0, strlen(passphrase));
273 if (private != NULL || quit)
275 debug2("bad passphrase given, try again...");
278 /* We no longer need the comment. */
281 if (private == NULL) {
282 if (!options.batch_mode)
283 error("Bad passphrase.");
285 /* Send a dummy response packet to avoid protocol error. */
286 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
287 for (i = 0; i < 16; i++)
292 /* Expect the server to reject it... */
293 packet_read_expect(SSH_SMSG_FAILURE);
294 BN_clear_free(challenge);
298 /* Compute and send a response to the challenge. */
299 respond_to_rsa_challenge(challenge, private->rsa);
301 /* Destroy the private key unless it in external hardware. */
302 if (!(private->flags & KEY_FLAG_EXT))
305 /* We no longer need the challenge. */
306 BN_clear_free(challenge);
308 /* Wait for response from the server. */
309 type = packet_read();
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, Key * host_key)
330 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
332 /* Tell the server that we are willing to authenticate using this key. */
333 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
334 packet_put_cstring(local_user);
335 packet_put_int(BN_num_bits(host_key->rsa->n));
336 packet_put_bignum(host_key->rsa->e);
337 packet_put_bignum(host_key->rsa->n);
341 /* Wait for server's response. */
342 type = packet_read();
344 /* The server responds with failure if it doesn't admit our
345 .rhosts authentication or doesn't know our host key. */
346 if (type == SSH_SMSG_FAILURE) {
347 debug("Server refused our rhosts authentication or host key.");
350 /* Otherwise, the server should respond with a challenge. */
351 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
352 packet_disconnect("Protocol error during RSA authentication: %d", type);
354 /* Get the challenge from the packet. */
355 if ((challenge = BN_new()) == NULL)
356 fatal("try_rhosts_rsa_authentication: BN_new failed");
357 packet_get_bignum(challenge);
360 debug("Received RSA challenge for host key from server.");
362 /* Compute a response to the challenge. */
363 respond_to_rsa_challenge(challenge, host_key->rsa);
365 /* We no longer need the challenge. */
366 BN_clear_free(challenge);
368 /* Wait for response from the server. */
369 type = packet_read();
370 if (type == SSH_SMSG_SUCCESS) {
371 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
374 if (type != SSH_SMSG_FAILURE)
375 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
376 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
382 try_krb4_authentication(void)
384 KTEXT_ST auth; /* Kerberos data */
391 Key_schedule schedule;
392 u_long checksum, cksum;
394 struct sockaddr_in local, foreign;
397 /* Don't do anything if we don't have any tickets. */
398 if (stat(tkt_string(), &st) < 0)
401 strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)),
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();
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);
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",
477 packet_disconnect("Kerberos v4 challenge failed!");
479 /* Fetch the (incremented) checksum that we supplied in the request. */
480 memcpy((char *)&cksum, (char *)msg_data.app_data,
482 cksum = ntohl(cksum);
484 /* If it matches, we're golden. */
485 if (cksum == checksum + 1) {
486 debug("Kerberos v4 challenge successful.");
489 packet_disconnect("Kerberos v4 challenge failed!");
493 packet_disconnect("Protocol error on Kerberos v4 response: %d", type);
502 try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
504 krb5_error_code problem;
507 krb5_ccache ccache = NULL;
508 const char *remotehost;
511 krb5_ap_rep_enc_part *reply = NULL;
514 memset(&ap, 0, sizeof(ap));
516 problem = krb5_init_context(context);
518 debug("Kerberos v5: krb5_init_context failed");
523 tkfile = krb5_cc_default_name(*context);
524 if (strncmp(tkfile, "FILE:", 5) == 0)
527 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
528 debug("Kerberos v5: could not get default ccache (permission denied).");
533 problem = krb5_cc_default(*context, &ccache);
535 debug("Kerberos v5: krb5_cc_default failed: %s",
536 krb5_get_err_text(*context, problem));
541 remotehost = get_canonical_hostname(1);
543 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
544 "host", remotehost, NULL, ccache, &ap);
546 debug("Kerberos v5: krb5_mk_req failed: %s",
547 krb5_get_err_text(*context, problem));
552 packet_start(SSH_CMSG_AUTH_KERBEROS);
553 packet_put_string((char *) ap.data, ap.length);
560 type = packet_read();
562 case SSH_SMSG_FAILURE:
563 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
564 debug("Kerberos v5 authentication failed.");
568 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
569 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
570 debug("Kerberos v5 authentication accepted.");
572 /* Get server's response. */
573 ap.data = packet_get_string((unsigned int *) &ap.length);
575 /* XXX je to dobre? */
577 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
585 packet_disconnect("Protocol error on Kerberos v5 response: %d",
594 krb5_cc_close(*context, ccache);
596 krb5_free_ap_rep_enc_part(*context, reply);
604 send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
607 krb5_error_code problem;
609 krb5_ccache ccache = NULL;
611 krb5_kdc_flags flags;
612 const char *remotehost;
614 memset(&creds, 0, sizeof(creds));
615 memset(&outbuf, 0, sizeof(outbuf));
617 fd = packet_get_connection_in();
619 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
623 problem = krb5_cc_default(context, &ccache);
627 problem = krb5_cc_get_principal(context, ccache, &creds.client);
631 problem = krb5_build_principal(context, &creds.server,
632 strlen(creds.client->realm), creds.client->realm,
633 "krbtgt", creds.client->realm, NULL);
637 creds.times.endtime = 0;
640 flags.b.forwarded = 1;
641 flags.b.forwardable = krb5_config_get_bool(context, NULL,
642 "libdefaults", "forwardable", NULL);
644 remotehost = get_canonical_hostname(1);
646 problem = krb5_get_forwarded_creds(context, auth_context,
647 ccache, flags.i, remotehost, &creds, &outbuf);
651 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
652 packet_put_string((char *)outbuf.data, outbuf.length);
656 type = packet_read();
658 if (type == SSH_SMSG_SUCCESS) {
661 krb5_unparse_name(context, creds.client, &pname);
662 debug("Kerberos v5 TGT forwarded (%s).", pname);
665 debug("Kerberos v5 TGT forwarding failed.");
671 debug("Kerberos v5 TGT forwarding failed: %s",
672 krb5_get_err_text(context, problem));
674 krb5_free_principal(context, creds.client);
676 krb5_free_principal(context, creds.server);
678 krb5_cc_close(context, ccache);
690 char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
693 /* Don't do anything if we don't have any tickets. */
694 if (stat(tkt_string(), &st) < 0)
697 creds = xmalloc(sizeof(*creds));
699 problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm);
703 problem = krb_get_cred("krbtgt", prealm, prealm, creds);
707 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
711 creds_to_radix(creds, (u_char *)buffer, sizeof(buffer));
713 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
714 packet_put_cstring(buffer);
718 type = packet_read();
720 if (type == SSH_SMSG_SUCCESS)
721 debug("Kerberos v4 TGT forwarded (%s%s%s@%s).",
722 creds->pname, creds->pinst[0] ? "." : "",
723 creds->pinst, creds->realm);
725 debug("Kerberos v4 TGT rejected.");
731 debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]);
736 send_afs_tokens(void)
739 struct ViceIoctl parms;
740 struct ClearToken ct;
742 char buf[2048], *p, *server_cell;
745 /* Move over ktc_GetToken, here's something leaner. */
746 for (i = 0; i < 100; i++) { /* just in case */
747 parms.in = (char *) &i;
748 parms.in_size = sizeof(i);
750 parms.out_size = sizeof(buf);
751 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
755 /* Get secret token. */
756 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
757 if (creds.ticket_st.length > MAX_KTXT_LEN)
760 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
761 p += creds.ticket_st.length;
763 /* Get clear token. */
764 memcpy(&len, p, sizeof(len));
765 if (len != sizeof(struct ClearToken))
770 p += sizeof(len); /* primary flag */
773 /* Flesh out our credentials. */
774 strlcpy(creds.service, "afs", sizeof(creds.service));
775 creds.instance[0] = '\0';
776 strlcpy(creds.realm, server_cell, REALM_SZ);
777 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
778 creds.issue_date = ct.BeginTimestamp;
779 creds.lifetime = krb_time_to_life(creds.issue_date,
781 creds.kvno = ct.AuthHandle;
782 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
783 creds.pinst[0] = '\0';
785 /* Encode token, ship it off. */
786 if (creds_to_radix(&creds, (u_char *)buffer,
787 sizeof(buffer)) <= 0)
789 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
790 packet_put_cstring(buffer);
794 /* Roger, Roger. Clearance, Clarence. What's your vector,
796 type = packet_read();
798 if (type == SSH_SMSG_FAILURE)
799 debug("AFS token for cell %s rejected.", server_cell);
800 else if (type != SSH_SMSG_SUCCESS)
801 packet_disconnect("Protocol error on AFS token response: %d", type);
808 * Tries to authenticate with any string-based challenge/response system.
809 * Note that the client code is not tied to s/key or TIS.
812 try_challenge_response_authentication(void)
817 char *challenge, *response;
819 debug("Doing challenge response authentication.");
821 for (i = 0; i < options.number_of_password_prompts; i++) {
822 /* request a challenge */
823 packet_start(SSH_CMSG_AUTH_TIS);
827 type = packet_read();
828 if (type != SSH_SMSG_FAILURE &&
829 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
830 packet_disconnect("Protocol error: got %d in response "
831 "to SSH_CMSG_AUTH_TIS", type);
833 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
834 debug("No challenge.");
837 challenge = packet_get_string(&clen);
839 snprintf(prompt, sizeof prompt, "%s%s", challenge,
840 strchr(challenge, '\n') ? "" : "\nResponse: ");
843 error("Permission denied, please try again.");
844 if (options.cipher == SSH_CIPHER_NONE)
845 log("WARNING: Encryption is disabled! "
846 "Reponse will be transmitted in clear text.");
847 response = read_passphrase(prompt, 0);
848 if (strcmp(response, "") == 0) {
852 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
853 ssh_put_password(response);
854 memset(response, 0, strlen(response));
858 type = packet_read();
859 if (type == SSH_SMSG_SUCCESS)
861 if (type != SSH_SMSG_FAILURE)
862 packet_disconnect("Protocol error: got %d in response "
863 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
870 * Tries to authenticate with plain passwd authentication.
873 try_password_authentication(char *prompt)
878 debug("Doing password authentication.");
879 if (options.cipher == SSH_CIPHER_NONE)
880 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
881 for (i = 0; i < options.number_of_password_prompts; i++) {
883 error("Permission denied, please try again.");
884 password = read_passphrase(prompt, 0);
885 packet_start(SSH_CMSG_AUTH_PASSWORD);
886 ssh_put_password(password);
887 memset(password, 0, strlen(password));
892 type = packet_read();
893 if (type == SSH_SMSG_SUCCESS)
895 if (type != SSH_SMSG_FAILURE)
896 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
906 ssh_kex(char *host, struct sockaddr *hostaddr)
910 Key *host_key, *server_key;
912 int ssh_cipher_default = SSH_CIPHER_3DES;
913 u_char session_key[SSH_SESSION_KEY_LENGTH];
915 u_int supported_ciphers;
916 u_int server_flags, client_flags;
919 debug("Waiting for server public key.");
921 /* Wait for a public key packet from the server. */
922 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
924 /* Get cookie from the packet. */
925 for (i = 0; i < 8; i++)
926 cookie[i] = packet_get_char();
928 /* Get the public key. */
929 server_key = key_new(KEY_RSA1);
930 bits = packet_get_int();
931 packet_get_bignum(server_key->rsa->e);
932 packet_get_bignum(server_key->rsa->n);
934 rbits = BN_num_bits(server_key->rsa->n);
936 log("Warning: Server lies about size of server public key: "
937 "actual size is %d bits vs. announced %d.", rbits, bits);
938 log("Warning: This may be due to an old implementation of ssh.");
940 /* Get the host key. */
941 host_key = key_new(KEY_RSA1);
942 bits = packet_get_int();
943 packet_get_bignum(host_key->rsa->e);
944 packet_get_bignum(host_key->rsa->n);
946 rbits = BN_num_bits(host_key->rsa->n);
948 log("Warning: Server lies about size of server host key: "
949 "actual size is %d bits vs. announced %d.", rbits, bits);
950 log("Warning: This may be due to an old implementation of ssh.");
953 /* Get protocol flags. */
954 server_flags = packet_get_int();
955 packet_set_protocol_flags(server_flags);
957 supported_ciphers = packet_get_int();
958 supported_authentications = packet_get_int();
961 debug("Received server public key (%d bits) and host key (%d bits).",
962 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
964 if (verify_host_key(host, hostaddr, host_key) == -1)
965 fatal("Host key verification failed.");
967 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
969 compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
971 /* Generate a session key. */
975 * Generate an encryption key for the session. The key is a 256 bit
976 * random number, interpreted as a 32-byte key, with the least
977 * significant 8 bits being the first byte of the key.
979 for (i = 0; i < 32; i++) {
982 session_key[i] = rand & 0xff;
987 * According to the protocol spec, the first byte of the session key
988 * is the highest byte of the integer. The session key is xored with
989 * the first 16 bytes of the session id.
991 if ((key = BN_new()) == NULL)
992 fatal("respond_to_rsa_challenge: BN_new failed");
994 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
995 BN_lshift(key, key, 8);
997 BN_add_word(key, session_key[i] ^ session_id[i]);
999 BN_add_word(key, session_key[i]);
1003 * Encrypt the integer using the public key and host key of the
1004 * server (key with smaller modulus first).
1006 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
1007 /* Public key has smaller modulus. */
1008 if (BN_num_bits(host_key->rsa->n) <
1009 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1010 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
1011 "SSH_KEY_BITS_RESERVED %d",
1012 BN_num_bits(host_key->rsa->n),
1013 BN_num_bits(server_key->rsa->n),
1014 SSH_KEY_BITS_RESERVED);
1016 rsa_public_encrypt(key, key, server_key->rsa);
1017 rsa_public_encrypt(key, key, host_key->rsa);
1019 /* Host key has smaller modulus (or they are equal). */
1020 if (BN_num_bits(server_key->rsa->n) <
1021 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1022 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
1023 "SSH_KEY_BITS_RESERVED %d",
1024 BN_num_bits(server_key->rsa->n),
1025 BN_num_bits(host_key->rsa->n),
1026 SSH_KEY_BITS_RESERVED);
1028 rsa_public_encrypt(key, key, host_key->rsa);
1029 rsa_public_encrypt(key, key, server_key->rsa);
1032 /* Destroy the public keys since we no longer need them. */
1033 key_free(server_key);
1036 if (options.cipher == SSH_CIPHER_NOT_SET) {
1037 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
1038 options.cipher = ssh_cipher_default;
1039 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
1040 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
1041 log("No valid SSH1 cipher, using %.100s instead.",
1042 cipher_name(ssh_cipher_default));
1043 options.cipher = ssh_cipher_default;
1045 /* Check that the selected cipher is supported. */
1046 if (!(supported_ciphers & (1 << options.cipher)))
1047 fatal("Selected cipher type %.100s not supported by server.",
1048 cipher_name(options.cipher));
1050 debug("Encryption type: %.100s", cipher_name(options.cipher));
1052 /* Send the encrypted session key to the server. */
1053 packet_start(SSH_CMSG_SESSION_KEY);
1054 packet_put_char(options.cipher);
1056 /* Send the cookie back to the server. */
1057 for (i = 0; i < 8; i++)
1058 packet_put_char(cookie[i]);
1060 /* Send and destroy the encrypted encryption key integer. */
1061 packet_put_bignum(key);
1064 /* Send protocol flags. */
1065 packet_put_int(client_flags);
1067 /* Send the packet now. */
1069 packet_write_wait();
1071 debug("Sent encrypted session key.");
1073 /* Set the encryption key. */
1074 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1076 /* We will no longer need the session key here. Destroy any extra copies. */
1077 memset(session_key, 0, sizeof(session_key));
1080 * Expect a success message from the server. Note that this message
1081 * will be received in encrypted form.
1083 packet_read_expect(SSH_SMSG_SUCCESS);
1085 debug("Received encrypted confirmation.");
1092 ssh_userauth1(const char *local_user, const char *server_user, char *host,
1093 Key **keys, int nkeys)
1096 krb5_context context = NULL;
1097 krb5_auth_context auth_context = NULL;
1101 if (supported_authentications == 0)
1102 fatal("ssh_userauth1: server supports no auth methods");
1104 /* Send the name of the user to log in as on the server. */
1105 packet_start(SSH_CMSG_USER);
1106 packet_put_cstring(server_user);
1108 packet_write_wait();
1111 * The server should respond with success if no authentication is
1112 * needed (the user has no password). Otherwise the server responds
1115 type = packet_read();
1117 /* check whether the connection was accepted without authentication. */
1118 if (type == SSH_SMSG_SUCCESS)
1120 if (type != SSH_SMSG_FAILURE)
1121 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
1124 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1125 options.kerberos_authentication) {
1126 debug("Trying Kerberos v5 authentication.");
1128 if (try_krb5_authentication(&context, &auth_context)) {
1129 type = packet_read();
1130 if (type == SSH_SMSG_SUCCESS)
1132 if (type != SSH_SMSG_FAILURE)
1133 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
1139 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1140 options.kerberos_authentication) {
1141 debug("Trying Kerberos v4 authentication.");
1143 if (try_krb4_authentication()) {
1144 type = packet_read();
1145 if (type == SSH_SMSG_SUCCESS)
1147 if (type != SSH_SMSG_FAILURE)
1148 packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type);
1154 * Use rhosts authentication if running in privileged socket and we
1155 * do not wish to remain anonymous.
1157 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1158 options.rhosts_authentication) {
1159 debug("Trying rhosts authentication.");
1160 packet_start(SSH_CMSG_AUTH_RHOSTS);
1161 packet_put_cstring(local_user);
1163 packet_write_wait();
1165 /* The server should respond with success or failure. */
1166 type = packet_read();
1167 if (type == SSH_SMSG_SUCCESS)
1169 if (type != SSH_SMSG_FAILURE)
1170 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1174 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1177 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1178 options.rhosts_rsa_authentication) {
1179 for (i = 0; i < nkeys; i++) {
1180 if (keys[i] != NULL && keys[i]->type == KEY_RSA1 &&
1181 try_rhosts_rsa_authentication(local_user, keys[i]))
1185 /* Try RSA authentication if the server supports it. */
1186 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1187 options.rsa_authentication) {
1189 * Try RSA authentication using the authentication agent. The
1190 * agent is tried first because no passphrase is needed for
1191 * it, whereas identity files may require passphrases.
1193 if (try_agent_authentication())
1196 /* Try RSA authentication for each identity. */
1197 for (i = 0; i < options.num_identity_files; i++)
1198 if (options.identity_keys[i] != NULL &&
1199 options.identity_keys[i]->type == KEY_RSA1 &&
1200 try_rsa_authentication(i))
1203 /* Try challenge response authentication if the server supports it. */
1204 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1205 options.challenge_response_authentication && !options.batch_mode) {
1206 if (try_challenge_response_authentication())
1209 /* Try password authentication if the server supports it. */
1210 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1211 options.password_authentication && !options.batch_mode) {
1214 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1216 if (try_password_authentication(prompt))
1219 /* All authentication methods have failed. Exit with an error message. */
1220 fatal("Permission denied.");
1225 /* Try Kerberos v5 TGT passing. */
1226 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1227 options.kerberos_tgt_passing && context && auth_context) {
1228 if (options.cipher == SSH_CIPHER_NONE)
1229 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1230 send_krb5_tgt(context, auth_context);
1233 krb5_auth_con_free(context, auth_context);
1235 krb5_free_context(context);
1239 /* Try Kerberos v4 TGT passing if the server supports it. */
1240 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1241 options.kerberos_tgt_passing) {
1242 if (options.cipher == SSH_CIPHER_NONE)
1243 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1246 /* Try AFS token passing if the server supports it. */
1247 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1248 options.afs_token_passing && k_hasafs()) {
1249 if (options.cipher == SSH_CIPHER_NONE)
1250 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1255 return; /* need statement after label */