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.49 2002/03/14 15:24:27 markus Exp $");
18 #include <openssl/bn.h>
19 #include <openssl/md5.h>
27 #define krb5_get_err_text(context,code) error_message(code)
47 #include "sshconnect.h"
54 /* Session id for the current session. */
55 u_char session_id[16];
56 u_int supported_authentications = 0;
58 extern Options options;
59 extern char *__progname;
62 * Checks if the user has an authentication agent, and if so, tries to
63 * authenticate using the agent.
66 try_agent_authentication(void)
70 AuthenticationConnection *auth;
76 /* Get connection to the agent. */
77 auth = ssh_get_authentication_connection();
81 if ((challenge = BN_new()) == NULL)
82 fatal("try_agent_authentication: BN_new failed");
83 /* Loop through identities served by the agent. */
84 for (key = ssh_get_first_identity(auth, &comment, 1);
86 key = ssh_get_next_identity(auth, &comment, 1)) {
88 /* Try this identity. */
89 debug("Trying RSA authentication via agent with '%.100s'", comment);
92 /* Tell the server that we are willing to authenticate using this key. */
93 packet_start(SSH_CMSG_AUTH_RSA);
94 packet_put_bignum(key->rsa->n);
98 /* Wait for server's response. */
101 /* The server sends failure if it doesn\'t like our key or
102 does not support RSA authentication. */
103 if (type == SSH_SMSG_FAILURE) {
104 debug("Server refused our key.");
108 /* Otherwise it should have sent a challenge. */
109 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
110 packet_disconnect("Protocol error during RSA authentication: %d",
113 packet_get_bignum(challenge);
116 debug("Received RSA challenge from server.");
118 /* Ask the agent to decrypt the challenge. */
119 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
121 * The agent failed to authenticate this identifier
122 * although it advertised it supports this. Just
123 * return a wrong value.
125 log("Authentication agent failed to decrypt challenge.");
126 memset(response, 0, sizeof(response));
129 debug("Sending response to RSA challenge.");
131 /* Send the decrypted challenge back to the server. */
132 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
133 for (i = 0; i < 16; i++)
134 packet_put_char(response[i]);
138 /* Wait for response from the server. */
139 type = packet_read();
141 /* The server returns success if it accepted the authentication. */
142 if (type == SSH_SMSG_SUCCESS) {
143 ssh_close_authentication_connection(auth);
144 BN_clear_free(challenge);
145 debug("RSA authentication accepted by server.");
148 /* Otherwise it should return failure. */
149 if (type != SSH_SMSG_FAILURE)
150 packet_disconnect("Protocol error waiting RSA auth response: %d",
153 ssh_close_authentication_connection(auth);
154 BN_clear_free(challenge);
155 debug("RSA authentication using agent refused.");
160 * Computes the proper response to a RSA challenge, and sends the response to
164 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
166 u_char buf[32], response[16];
170 /* Decrypt the challenge using the private key. */
171 /* XXX think about Bleichenbacher, too */
172 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
174 "respond_to_rsa_challenge: rsa_private_decrypt failed");
176 /* Compute the response. */
177 /* The response is MD5 of decrypted challenge plus session id. */
178 len = BN_num_bytes(challenge);
179 if (len <= 0 || len > sizeof(buf))
181 "respond_to_rsa_challenge: bad challenge length %d", len);
183 memset(buf, 0, sizeof(buf));
184 BN_bn2bin(challenge, buf + sizeof(buf) - len);
186 MD5_Update(&md, buf, 32);
187 MD5_Update(&md, session_id, 16);
188 MD5_Final(response, &md);
190 debug("Sending response to host key RSA challenge.");
192 /* Send the response back to the server. */
193 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
194 for (i = 0; i < 16; i++)
195 packet_put_char(response[i]);
199 memset(buf, 0, sizeof(buf));
200 memset(response, 0, sizeof(response));
201 memset(&md, 0, sizeof(md));
205 * Checks if the user has authentication file, and if so, tries to authenticate
209 try_rsa_authentication(int idx)
212 Key *public, *private;
213 char buf[300], *passphrase, *comment, *authfile;
216 public = options.identity_keys[idx];
217 authfile = options.identity_files[idx];
218 comment = xstrdup(authfile);
220 debug("Trying RSA authentication with key '%.100s'", comment);
222 /* Tell the server that we are willing to authenticate using this key. */
223 packet_start(SSH_CMSG_AUTH_RSA);
224 packet_put_bignum(public->rsa->n);
228 /* Wait for server's response. */
229 type = packet_read();
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 if ((challenge = BN_new()) == NULL)
246 fatal("try_rsa_authentication: BN_new failed");
247 packet_get_bignum(challenge);
250 debug("Received RSA challenge from server.");
253 * If the key is not stored in external hardware, we have to
254 * load the private key. Try first with empty passphrase; if it
255 * fails, ask for a passphrase.
257 if (public->flags && KEY_FLAG_EXT)
260 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
261 if (private == NULL && !options.batch_mode) {
262 snprintf(buf, sizeof(buf),
263 "Enter passphrase for RSA key '%.100s': ", comment);
264 for (i = 0; i < options.number_of_password_prompts; i++) {
265 passphrase = read_passphrase(buf, 0);
266 if (strcmp(passphrase, "") != 0) {
267 private = key_load_private_type(KEY_RSA1,
268 authfile, passphrase, NULL);
271 debug2("no passphrase given, try next key");
274 memset(passphrase, 0, strlen(passphrase));
276 if (private != NULL || quit)
278 debug2("bad passphrase given, try again...");
281 /* We no longer need the comment. */
284 if (private == NULL) {
285 if (!options.batch_mode)
286 error("Bad passphrase.");
288 /* Send a dummy response packet to avoid protocol error. */
289 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
290 for (i = 0; i < 16; i++)
295 /* Expect the server to reject it... */
296 packet_read_expect(SSH_SMSG_FAILURE);
297 BN_clear_free(challenge);
301 /* Compute and send a response to the challenge. */
302 respond_to_rsa_challenge(challenge, private->rsa);
304 /* Destroy the private key unless it in external hardware. */
305 if (!(private->flags & KEY_FLAG_EXT))
308 /* We no longer need the challenge. */
309 BN_clear_free(challenge);
311 /* Wait for response from the server. */
312 type = packet_read();
313 if (type == SSH_SMSG_SUCCESS) {
314 debug("RSA authentication accepted by server.");
317 if (type != SSH_SMSG_FAILURE)
318 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
319 debug("RSA authentication refused.");
324 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
325 * authentication and RSA host authentication.
328 try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
333 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
335 /* Tell the server that we are willing to authenticate using this key. */
336 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
337 packet_put_cstring(local_user);
338 packet_put_int(BN_num_bits(host_key->rsa->n));
339 packet_put_bignum(host_key->rsa->e);
340 packet_put_bignum(host_key->rsa->n);
344 /* Wait for server's response. */
345 type = packet_read();
347 /* The server responds with failure if it doesn't admit our
348 .rhosts authentication or doesn't know our host key. */
349 if (type == SSH_SMSG_FAILURE) {
350 debug("Server refused our rhosts authentication or host key.");
353 /* Otherwise, the server should respond with a challenge. */
354 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
355 packet_disconnect("Protocol error during RSA authentication: %d", type);
357 /* Get the challenge from the packet. */
358 if ((challenge = BN_new()) == NULL)
359 fatal("try_rhosts_rsa_authentication: BN_new failed");
360 packet_get_bignum(challenge);
363 debug("Received RSA challenge for host key from server.");
365 /* Compute a response to the challenge. */
366 respond_to_rsa_challenge(challenge, host_key->rsa);
368 /* We no longer need the challenge. */
369 BN_clear_free(challenge);
371 /* Wait for response from the server. */
372 type = packet_read();
373 if (type == SSH_SMSG_SUCCESS) {
374 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
377 if (type != SSH_SMSG_FAILURE)
378 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
379 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
385 try_krb4_authentication(void)
387 KTEXT_ST auth; /* Kerberos data */
394 Key_schedule schedule;
395 u_long checksum, cksum;
397 struct sockaddr_in local, foreign;
400 /* Don't do anything if we don't have any tickets. */
401 if (stat(tkt_string(), &st) < 0)
404 strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)),
407 realm = (char *)krb_realmofhost(get_canonical_hostname(1));
409 debug("Kerberos v4: no realm for %s", get_canonical_hostname(1));
412 /* This can really be anything. */
413 checksum = (u_long)getpid();
415 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
417 debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]);
420 /* Get session key to decrypt the server's reply with. */
421 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
423 debug("get_cred failed: %s", krb_err_txt[r]);
426 des_key_sched((des_cblock *) cred.session, schedule);
428 /* Send authentication info to server. */
429 packet_start(SSH_CMSG_AUTH_KERBEROS);
430 packet_put_string((char *) auth.dat, auth.length);
434 /* Zero the buffer. */
435 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
437 slen = sizeof(local);
438 memset(&local, 0, sizeof(local));
439 if (getsockname(packet_get_connection_in(),
440 (struct sockaddr *)&local, &slen) < 0)
441 debug("getsockname failed: %s", strerror(errno));
443 slen = sizeof(foreign);
444 memset(&foreign, 0, sizeof(foreign));
445 if (getpeername(packet_get_connection_in(),
446 (struct sockaddr *)&foreign, &slen) < 0) {
447 debug("getpeername failed: %s", strerror(errno));
450 /* Get server reply. */
451 type = packet_read();
453 case SSH_SMSG_FAILURE:
454 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
455 debug("Kerberos v4 authentication failed.");
459 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
460 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
461 debug("Kerberos v4 authentication accepted.");
463 /* Get server's response. */
464 reply = packet_get_string((u_int *) &auth.length);
465 if (auth.length >= MAX_KTXT_LEN)
466 fatal("Kerberos v4: Malformed response from server");
467 memcpy(auth.dat, reply, auth.length);
473 * If his response isn't properly encrypted with the session
474 * key, and the decrypted checksum fails to match, he's
477 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
478 &foreign, &local, &msg_data);
480 debug("Kerberos v4 krb_rd_priv failed: %s",
482 packet_disconnect("Kerberos v4 challenge failed!");
484 /* Fetch the (incremented) checksum that we supplied in the request. */
485 memcpy((char *)&cksum, (char *)msg_data.app_data,
487 cksum = ntohl(cksum);
489 /* If it matches, we're golden. */
490 if (cksum == checksum + 1) {
491 debug("Kerberos v4 challenge successful.");
494 packet_disconnect("Kerberos v4 challenge failed!");
498 packet_disconnect("Protocol error on Kerberos v4 response: %d", type);
507 try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
509 krb5_error_code problem;
512 krb5_ccache ccache = NULL;
513 const char *remotehost;
516 krb5_ap_rep_enc_part *reply = NULL;
519 memset(&ap, 0, sizeof(ap));
521 problem = krb5_init_context(context);
523 debug("Kerberos v5: krb5_init_context failed");
528 problem = krb5_auth_con_init(*context, auth_context);
530 debug("Kerberos v5: krb5_auth_con_init failed");
536 problem = krb5_auth_con_setflags(*context, *auth_context,
537 KRB5_AUTH_CONTEXT_RET_TIME);
539 debug("Keberos v5: krb5_auth_con_setflags failed");
545 tkfile = krb5_cc_default_name(*context);
546 if (strncmp(tkfile, "FILE:", 5) == 0)
549 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
550 debug("Kerberos v5: could not get default ccache (permission denied).");
555 problem = krb5_cc_default(*context, &ccache);
557 debug("Kerberos v5: krb5_cc_default failed: %s",
558 krb5_get_err_text(*context, problem));
563 remotehost = get_canonical_hostname(1);
565 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
566 "host", remotehost, NULL, ccache, &ap);
568 debug("Kerberos v5: krb5_mk_req failed: %s",
569 krb5_get_err_text(*context, problem));
574 packet_start(SSH_CMSG_AUTH_KERBEROS);
575 packet_put_string((char *) ap.data, ap.length);
582 type = packet_read();
584 case SSH_SMSG_FAILURE:
585 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
586 debug("Kerberos v5 authentication failed.");
590 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
591 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
592 debug("Kerberos v5 authentication accepted.");
594 /* Get server's response. */
595 ap.data = packet_get_string((unsigned int *) &ap.length);
597 /* XXX je to dobre? */
599 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
607 packet_disconnect("Protocol error on Kerberos v5 response: %d",
616 krb5_cc_close(*context, ccache);
618 krb5_free_ap_rep_enc_part(*context, reply);
623 krb5_free_data_contents(*context, &ap);
630 send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
633 krb5_error_code problem;
635 krb5_ccache ccache = NULL;
638 krb5_kdc_flags flags;
642 const char *remotehost;
644 memset(&creds, 0, sizeof(creds));
645 memset(&outbuf, 0, sizeof(outbuf));
647 fd = packet_get_connection_in();
650 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
652 problem = krb5_auth_con_genaddrs(context, auth_context, fd,
653 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
654 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
659 problem = krb5_cc_default(context, &ccache);
663 problem = krb5_cc_get_principal(context, ccache, &creds.client);
667 remotehost = get_canonical_hostname(1);
670 problem = krb5_build_principal(context, &creds.server,
671 strlen(creds.client->realm), creds.client->realm,
672 "krbtgt", creds.client->realm, NULL);
674 problem = krb5_build_principal(context, &creds.server,
675 creds.client->realm.length, creds.client->realm.data,
676 "host", remotehost, NULL);
681 creds.times.endtime = 0;
685 flags.b.forwarded = 1;
686 flags.b.forwardable = krb5_config_get_bool(context, NULL,
687 "libdefaults", "forwardable", NULL);
688 problem = krb5_get_forwarded_creds(context, auth_context,
689 ccache, flags.i, remotehost, &creds, &outbuf);
692 problem = krb5_fwd_tgt_creds(context, auth_context, remotehost,
693 creds.client, creds.server, ccache, forwardable, &outbuf);
699 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
700 packet_put_string((char *)outbuf.data, outbuf.length);
704 type = packet_read();
706 if (type == SSH_SMSG_SUCCESS) {
709 krb5_unparse_name(context, creds.client, &pname);
710 debug("Kerberos v5 TGT forwarded (%s).", pname);
713 debug("Kerberos v5 TGT forwarding failed.");
719 debug("Kerberos v5 TGT forwarding failed: %s",
720 krb5_get_err_text(context, problem));
722 krb5_free_principal(context, creds.client);
724 krb5_free_principal(context, creds.server);
726 krb5_cc_close(context, ccache);
738 char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
741 /* Don't do anything if we don't have any tickets. */
742 if (stat(tkt_string(), &st) < 0)
745 creds = xmalloc(sizeof(*creds));
747 problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm);
751 problem = krb_get_cred("krbtgt", prealm, prealm, creds);
755 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
759 creds_to_radix(creds, (u_char *)buffer, sizeof(buffer));
761 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
762 packet_put_cstring(buffer);
766 type = packet_read();
768 if (type == SSH_SMSG_SUCCESS)
769 debug("Kerberos v4 TGT forwarded (%s%s%s@%s).",
770 creds->pname, creds->pinst[0] ? "." : "",
771 creds->pinst, creds->realm);
773 debug("Kerberos v4 TGT rejected.");
779 debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]);
784 send_afs_tokens(void)
787 struct ViceIoctl parms;
788 struct ClearToken ct;
790 char buf[2048], *p, *server_cell;
793 /* Move over ktc_GetToken, here's something leaner. */
794 for (i = 0; i < 100; i++) { /* just in case */
795 parms.in = (char *) &i;
796 parms.in_size = sizeof(i);
798 parms.out_size = sizeof(buf);
799 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
803 /* Get secret token. */
804 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
805 if (creds.ticket_st.length > MAX_KTXT_LEN)
808 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
809 p += creds.ticket_st.length;
811 /* Get clear token. */
812 memcpy(&len, p, sizeof(len));
813 if (len != sizeof(struct ClearToken))
818 p += sizeof(len); /* primary flag */
821 /* Flesh out our credentials. */
822 strlcpy(creds.service, "afs", sizeof(creds.service));
823 creds.instance[0] = '\0';
824 strlcpy(creds.realm, server_cell, REALM_SZ);
825 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
826 creds.issue_date = ct.BeginTimestamp;
827 creds.lifetime = krb_time_to_life(creds.issue_date,
829 creds.kvno = ct.AuthHandle;
830 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
831 creds.pinst[0] = '\0';
833 /* Encode token, ship it off. */
834 if (creds_to_radix(&creds, (u_char *)buffer,
835 sizeof(buffer)) <= 0)
837 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
838 packet_put_cstring(buffer);
842 /* Roger, Roger. Clearance, Clarence. What's your vector,
844 type = packet_read();
846 if (type == SSH_SMSG_FAILURE)
847 debug("AFS token for cell %s rejected.", server_cell);
848 else if (type != SSH_SMSG_SUCCESS)
849 packet_disconnect("Protocol error on AFS token response: %d", type);
856 * Tries to authenticate with any string-based challenge/response system.
857 * Note that the client code is not tied to s/key or TIS.
860 try_challenge_response_authentication(void)
865 char *challenge, *response;
867 debug("Doing challenge response authentication.");
869 for (i = 0; i < options.number_of_password_prompts; i++) {
870 /* request a challenge */
871 packet_start(SSH_CMSG_AUTH_TIS);
875 type = packet_read();
876 if (type != SSH_SMSG_FAILURE &&
877 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
878 packet_disconnect("Protocol error: got %d in response "
879 "to SSH_CMSG_AUTH_TIS", type);
881 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
882 debug("No challenge.");
885 challenge = packet_get_string(&clen);
887 snprintf(prompt, sizeof prompt, "%s%s", challenge,
888 strchr(challenge, '\n') ? "" : "\nResponse: ");
891 error("Permission denied, please try again.");
892 if (options.cipher == SSH_CIPHER_NONE)
893 log("WARNING: Encryption is disabled! "
894 "Reponse will be transmitted in clear text.");
895 response = read_passphrase(prompt, 0);
896 if (strcmp(response, "") == 0) {
900 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
901 ssh_put_password(response);
902 memset(response, 0, strlen(response));
906 type = packet_read();
907 if (type == SSH_SMSG_SUCCESS)
909 if (type != SSH_SMSG_FAILURE)
910 packet_disconnect("Protocol error: got %d in response "
911 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
918 * Tries to authenticate with plain passwd authentication.
921 try_password_authentication(char *prompt)
926 debug("Doing password authentication.");
927 if (options.cipher == SSH_CIPHER_NONE)
928 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
929 for (i = 0; i < options.number_of_password_prompts; i++) {
931 error("Permission denied, please try again.");
932 password = read_passphrase(prompt, 0);
933 packet_start(SSH_CMSG_AUTH_PASSWORD);
934 ssh_put_password(password);
935 memset(password, 0, strlen(password));
940 type = packet_read();
941 if (type == SSH_SMSG_SUCCESS)
943 if (type != SSH_SMSG_FAILURE)
944 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
954 ssh_kex(char *host, struct sockaddr *hostaddr)
958 Key *host_key, *server_key;
960 int ssh_cipher_default = SSH_CIPHER_3DES;
961 u_char session_key[SSH_SESSION_KEY_LENGTH];
963 u_int supported_ciphers;
964 u_int server_flags, client_flags;
967 debug("Waiting for server public key.");
969 /* Wait for a public key packet from the server. */
970 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
972 /* Get cookie from the packet. */
973 for (i = 0; i < 8; i++)
974 cookie[i] = packet_get_char();
976 /* Get the public key. */
977 server_key = key_new(KEY_RSA1);
978 bits = packet_get_int();
979 packet_get_bignum(server_key->rsa->e);
980 packet_get_bignum(server_key->rsa->n);
982 rbits = BN_num_bits(server_key->rsa->n);
984 log("Warning: Server lies about size of server public key: "
985 "actual size is %d bits vs. announced %d.", rbits, bits);
986 log("Warning: This may be due to an old implementation of ssh.");
988 /* Get the host key. */
989 host_key = key_new(KEY_RSA1);
990 bits = packet_get_int();
991 packet_get_bignum(host_key->rsa->e);
992 packet_get_bignum(host_key->rsa->n);
994 rbits = BN_num_bits(host_key->rsa->n);
996 log("Warning: Server lies about size of server host key: "
997 "actual size is %d bits vs. announced %d.", rbits, bits);
998 log("Warning: This may be due to an old implementation of ssh.");
1001 /* Get protocol flags. */
1002 server_flags = packet_get_int();
1003 packet_set_protocol_flags(server_flags);
1005 supported_ciphers = packet_get_int();
1006 supported_authentications = packet_get_int();
1009 debug("Received server public key (%d bits) and host key (%d bits).",
1010 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
1012 if (verify_host_key(host, hostaddr, host_key) == -1)
1013 fatal("Host key verification failed.");
1015 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
1017 compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
1019 /* Generate a session key. */
1023 * Generate an encryption key for the session. The key is a 256 bit
1024 * random number, interpreted as a 32-byte key, with the least
1025 * significant 8 bits being the first byte of the key.
1027 for (i = 0; i < 32; i++) {
1029 rand = arc4random();
1030 session_key[i] = rand & 0xff;
1035 * According to the protocol spec, the first byte of the session key
1036 * is the highest byte of the integer. The session key is xored with
1037 * the first 16 bytes of the session id.
1039 if ((key = BN_new()) == NULL)
1040 fatal("respond_to_rsa_challenge: BN_new failed");
1041 BN_set_word(key, 0);
1042 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
1043 BN_lshift(key, key, 8);
1045 BN_add_word(key, session_key[i] ^ session_id[i]);
1047 BN_add_word(key, session_key[i]);
1051 * Encrypt the integer using the public key and host key of the
1052 * server (key with smaller modulus first).
1054 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
1055 /* Public key has smaller modulus. */
1056 if (BN_num_bits(host_key->rsa->n) <
1057 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1058 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
1059 "SSH_KEY_BITS_RESERVED %d",
1060 BN_num_bits(host_key->rsa->n),
1061 BN_num_bits(server_key->rsa->n),
1062 SSH_KEY_BITS_RESERVED);
1064 rsa_public_encrypt(key, key, server_key->rsa);
1065 rsa_public_encrypt(key, key, host_key->rsa);
1067 /* Host key has smaller modulus (or they are equal). */
1068 if (BN_num_bits(server_key->rsa->n) <
1069 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1070 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
1071 "SSH_KEY_BITS_RESERVED %d",
1072 BN_num_bits(server_key->rsa->n),
1073 BN_num_bits(host_key->rsa->n),
1074 SSH_KEY_BITS_RESERVED);
1076 rsa_public_encrypt(key, key, host_key->rsa);
1077 rsa_public_encrypt(key, key, server_key->rsa);
1080 /* Destroy the public keys since we no longer need them. */
1081 key_free(server_key);
1084 if (options.cipher == SSH_CIPHER_NOT_SET) {
1085 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
1086 options.cipher = ssh_cipher_default;
1087 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
1088 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
1089 log("No valid SSH1 cipher, using %.100s instead.",
1090 cipher_name(ssh_cipher_default));
1091 options.cipher = ssh_cipher_default;
1093 /* Check that the selected cipher is supported. */
1094 if (!(supported_ciphers & (1 << options.cipher)))
1095 fatal("Selected cipher type %.100s not supported by server.",
1096 cipher_name(options.cipher));
1098 debug("Encryption type: %.100s", cipher_name(options.cipher));
1100 /* Send the encrypted session key to the server. */
1101 packet_start(SSH_CMSG_SESSION_KEY);
1102 packet_put_char(options.cipher);
1104 /* Send the cookie back to the server. */
1105 for (i = 0; i < 8; i++)
1106 packet_put_char(cookie[i]);
1108 /* Send and destroy the encrypted encryption key integer. */
1109 packet_put_bignum(key);
1112 /* Send protocol flags. */
1113 packet_put_int(client_flags);
1115 /* Send the packet now. */
1117 packet_write_wait();
1119 debug("Sent encrypted session key.");
1121 /* Set the encryption key. */
1122 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1124 /* We will no longer need the session key here. Destroy any extra copies. */
1125 memset(session_key, 0, sizeof(session_key));
1128 * Expect a success message from the server. Note that this message
1129 * will be received in encrypted form.
1131 packet_read_expect(SSH_SMSG_SUCCESS);
1133 debug("Received encrypted confirmation.");
1140 ssh_userauth1(const char *local_user, const char *server_user, char *host,
1141 Key **keys, int nkeys)
1144 krb5_context context = NULL;
1145 krb5_auth_context auth_context = NULL;
1149 if (supported_authentications == 0)
1150 fatal("ssh_userauth1: server supports no auth methods");
1152 /* Send the name of the user to log in as on the server. */
1153 packet_start(SSH_CMSG_USER);
1154 packet_put_cstring(server_user);
1156 packet_write_wait();
1159 * The server should respond with success if no authentication is
1160 * needed (the user has no password). Otherwise the server responds
1163 type = packet_read();
1165 /* check whether the connection was accepted without authentication. */
1166 if (type == SSH_SMSG_SUCCESS)
1168 if (type != SSH_SMSG_FAILURE)
1169 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
1172 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1173 options.kerberos_authentication) {
1174 debug("Trying Kerberos v5 authentication.");
1176 if (try_krb5_authentication(&context, &auth_context)) {
1177 type = packet_read();
1178 if (type == SSH_SMSG_SUCCESS)
1180 if (type != SSH_SMSG_FAILURE)
1181 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
1187 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1188 options.kerberos_authentication) {
1189 debug("Trying Kerberos v4 authentication.");
1191 if (try_krb4_authentication()) {
1192 type = packet_read();
1193 if (type == SSH_SMSG_SUCCESS)
1195 if (type != SSH_SMSG_FAILURE)
1196 packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type);
1202 * Use rhosts authentication if running in privileged socket and we
1203 * do not wish to remain anonymous.
1205 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1206 options.rhosts_authentication) {
1207 debug("Trying rhosts authentication.");
1208 packet_start(SSH_CMSG_AUTH_RHOSTS);
1209 packet_put_cstring(local_user);
1211 packet_write_wait();
1213 /* The server should respond with success or failure. */
1214 type = packet_read();
1215 if (type == SSH_SMSG_SUCCESS)
1217 if (type != SSH_SMSG_FAILURE)
1218 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1222 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1225 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1226 options.rhosts_rsa_authentication) {
1227 for (i = 0; i < nkeys; i++) {
1228 if (keys[i] != NULL && keys[i]->type == KEY_RSA1 &&
1229 try_rhosts_rsa_authentication(local_user, keys[i]))
1233 /* Try RSA authentication if the server supports it. */
1234 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1235 options.rsa_authentication) {
1237 * Try RSA authentication using the authentication agent. The
1238 * agent is tried first because no passphrase is needed for
1239 * it, whereas identity files may require passphrases.
1241 if (try_agent_authentication())
1244 /* Try RSA authentication for each identity. */
1245 for (i = 0; i < options.num_identity_files; i++)
1246 if (options.identity_keys[i] != NULL &&
1247 options.identity_keys[i]->type == KEY_RSA1 &&
1248 try_rsa_authentication(i))
1251 /* Try challenge response authentication if the server supports it. */
1252 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1253 options.challenge_response_authentication && !options.batch_mode) {
1254 if (try_challenge_response_authentication())
1257 /* Try password authentication if the server supports it. */
1258 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1259 options.password_authentication && !options.batch_mode) {
1262 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1264 if (try_password_authentication(prompt))
1267 /* All authentication methods have failed. Exit with an error message. */
1268 fatal("Permission denied.");
1273 /* Try Kerberos v5 TGT passing. */
1274 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1275 options.kerberos_tgt_passing && context && auth_context) {
1276 if (options.cipher == SSH_CIPHER_NONE)
1277 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1278 send_krb5_tgt(context, auth_context);
1281 krb5_auth_con_free(context, auth_context);
1283 krb5_free_context(context);
1287 /* Try Kerberos v4 TGT passing if the server supports it. */
1288 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1289 options.kerberos_tgt_passing) {
1290 if (options.cipher == SSH_CIPHER_NONE)
1291 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1294 /* Try AFS token passing if the server supports it. */
1295 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1296 options.afs_token_passing && k_hasafs()) {
1297 if (options.cipher == SSH_CIPHER_NONE)
1298 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1303 return; /* need statement after label */