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.52 2002/08/08 13:50:23 aaron Exp $");
17 RCSID("$FreeBSD: src/crypto/openssh/sshconnect1.c,v 1.2.2.9 2003/02/03 17:31:08 des Exp $");
19 #include <openssl/bn.h>
20 #include <openssl/md5.h>
28 #define krb5_get_err_text(context,code) error_message(code)
48 #include "sshconnect.h"
55 /* Session id for the current session. */
56 u_char session_id[16];
57 u_int supported_authentications = 0;
59 extern Options options;
60 extern char *__progname;
63 * Checks if the user has an authentication agent, and if so, tries to
64 * authenticate using the agent.
67 try_agent_authentication(void)
71 AuthenticationConnection *auth;
77 /* Get connection to the agent. */
78 auth = ssh_get_authentication_connection();
82 if ((challenge = BN_new()) == NULL)
83 fatal("try_agent_authentication: BN_new failed");
84 /* Loop through identities served by the agent. */
85 for (key = ssh_get_first_identity(auth, &comment, 1);
87 key = ssh_get_next_identity(auth, &comment, 1)) {
89 /* Try this identity. */
90 debug("Trying RSA authentication via agent with '%.100s'", comment);
93 /* Tell the server that we are willing to authenticate using this key. */
94 packet_start(SSH_CMSG_AUTH_RSA);
95 packet_put_bignum(key->rsa->n);
99 /* Wait for server's response. */
100 type = packet_read();
102 /* The server sends failure if it doesn\'t like our key or
103 does not support RSA authentication. */
104 if (type == SSH_SMSG_FAILURE) {
105 debug("Server refused our key.");
109 /* Otherwise it should have sent a challenge. */
110 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
111 packet_disconnect("Protocol error during RSA authentication: %d",
114 packet_get_bignum(challenge);
117 debug("Received RSA challenge from server.");
119 /* Ask the agent to decrypt the challenge. */
120 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
122 * The agent failed to authenticate this identifier
123 * although it advertised it supports this. Just
124 * return a wrong value.
126 log("Authentication agent failed to decrypt challenge.");
127 memset(response, 0, sizeof(response));
130 debug("Sending response to RSA challenge.");
132 /* Send the decrypted challenge back to the server. */
133 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
134 for (i = 0; i < 16; i++)
135 packet_put_char(response[i]);
139 /* Wait for response from the server. */
140 type = packet_read();
142 /* The server returns success if it accepted the authentication. */
143 if (type == SSH_SMSG_SUCCESS) {
144 ssh_close_authentication_connection(auth);
145 BN_clear_free(challenge);
146 debug("RSA authentication accepted by server.");
149 /* Otherwise it should return failure. */
150 if (type != SSH_SMSG_FAILURE)
151 packet_disconnect("Protocol error waiting RSA auth response: %d",
154 ssh_close_authentication_connection(auth);
155 BN_clear_free(challenge);
156 debug("RSA authentication using agent refused.");
161 * Computes the proper response to a RSA challenge, and sends the response to
165 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
167 u_char buf[32], response[16];
171 /* Decrypt the challenge using the private key. */
172 /* XXX think about Bleichenbacher, too */
173 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
175 "respond_to_rsa_challenge: rsa_private_decrypt failed");
177 /* Compute the response. */
178 /* The response is MD5 of decrypted challenge plus session id. */
179 len = BN_num_bytes(challenge);
180 if (len <= 0 || len > sizeof(buf))
182 "respond_to_rsa_challenge: bad challenge length %d", len);
184 memset(buf, 0, sizeof(buf));
185 BN_bn2bin(challenge, buf + sizeof(buf) - len);
187 MD5_Update(&md, buf, 32);
188 MD5_Update(&md, session_id, 16);
189 MD5_Final(response, &md);
191 debug("Sending response to host key RSA challenge.");
193 /* Send the response back to the server. */
194 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
195 for (i = 0; i < 16; i++)
196 packet_put_char(response[i]);
200 memset(buf, 0, sizeof(buf));
201 memset(response, 0, sizeof(response));
202 memset(&md, 0, sizeof(md));
206 * Checks if the user has authentication file, and if so, tries to authenticate
210 try_rsa_authentication(int idx)
213 Key *public, *private;
214 char buf[300], *passphrase, *comment, *authfile;
217 public = options.identity_keys[idx];
218 authfile = options.identity_files[idx];
219 comment = xstrdup(authfile);
221 debug("Trying RSA authentication with key '%.100s'", comment);
223 /* Tell the server that we are willing to authenticate using this key. */
224 packet_start(SSH_CMSG_AUTH_RSA);
225 packet_put_bignum(public->rsa->n);
229 /* Wait for server's response. */
230 type = packet_read();
233 * The server responds with failure if it doesn\'t like our key or
234 * doesn\'t support RSA authentication.
236 if (type == SSH_SMSG_FAILURE) {
237 debug("Server refused our key.");
241 /* Otherwise, the server should respond with a challenge. */
242 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
243 packet_disconnect("Protocol error during RSA authentication: %d", type);
245 /* Get the challenge from the packet. */
246 if ((challenge = BN_new()) == NULL)
247 fatal("try_rsa_authentication: BN_new failed");
248 packet_get_bignum(challenge);
251 debug("Received RSA challenge from server.");
254 * If the key is not stored in external hardware, we have to
255 * load the private key. Try first with empty passphrase; if it
256 * fails, ask for a passphrase.
258 if (public->flags & KEY_FLAG_EXT)
261 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
262 if (private == NULL && !options.batch_mode) {
263 snprintf(buf, sizeof(buf),
264 "Enter passphrase for RSA key '%.100s': ", comment);
265 for (i = 0; i < options.number_of_password_prompts; i++) {
266 passphrase = read_passphrase(buf, 0);
267 if (strcmp(passphrase, "") != 0) {
268 private = key_load_private_type(KEY_RSA1,
269 authfile, passphrase, NULL);
272 debug2("no passphrase given, try next key");
275 memset(passphrase, 0, strlen(passphrase));
277 if (private != NULL || quit)
279 debug2("bad passphrase given, try again...");
282 /* We no longer need the comment. */
285 if (private == NULL) {
286 if (!options.batch_mode)
287 error("Bad passphrase.");
289 /* Send a dummy response packet to avoid protocol error. */
290 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
291 for (i = 0; i < 16; i++)
296 /* Expect the server to reject it... */
297 packet_read_expect(SSH_SMSG_FAILURE);
298 BN_clear_free(challenge);
302 /* Compute and send a response to the challenge. */
303 respond_to_rsa_challenge(challenge, private->rsa);
305 /* Destroy the private key unless it in external hardware. */
306 if (!(private->flags & KEY_FLAG_EXT))
309 /* We no longer need the challenge. */
310 BN_clear_free(challenge);
312 /* Wait for response from the server. */
313 type = packet_read();
314 if (type == SSH_SMSG_SUCCESS) {
315 debug("RSA authentication accepted by server.");
318 if (type != SSH_SMSG_FAILURE)
319 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
320 debug("RSA authentication refused.");
325 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
326 * authentication and RSA host authentication.
329 try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
334 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
336 /* Tell the server that we are willing to authenticate using this key. */
337 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
338 packet_put_cstring(local_user);
339 packet_put_int(BN_num_bits(host_key->rsa->n));
340 packet_put_bignum(host_key->rsa->e);
341 packet_put_bignum(host_key->rsa->n);
345 /* Wait for server's response. */
346 type = packet_read();
348 /* The server responds with failure if it doesn't admit our
349 .rhosts authentication or doesn't know our host key. */
350 if (type == SSH_SMSG_FAILURE) {
351 debug("Server refused our rhosts authentication or host key.");
354 /* Otherwise, the server should respond with a challenge. */
355 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
356 packet_disconnect("Protocol error during RSA authentication: %d", type);
358 /* Get the challenge from the packet. */
359 if ((challenge = BN_new()) == NULL)
360 fatal("try_rhosts_rsa_authentication: BN_new failed");
361 packet_get_bignum(challenge);
364 debug("Received RSA challenge for host key from server.");
366 /* Compute a response to the challenge. */
367 respond_to_rsa_challenge(challenge, host_key->rsa);
369 /* We no longer need the challenge. */
370 BN_clear_free(challenge);
372 /* Wait for response from the server. */
373 type = packet_read();
374 if (type == SSH_SMSG_SUCCESS) {
375 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
378 if (type != SSH_SMSG_FAILURE)
379 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
380 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
386 try_krb4_authentication(void)
388 KTEXT_ST auth; /* Kerberos data */
395 Key_schedule schedule;
396 u_long checksum, cksum;
398 struct sockaddr_in local, foreign;
401 /* Don't do anything if we don't have any tickets. */
402 if (stat(tkt_string(), &st) < 0)
405 strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)),
408 realm = (char *)krb_realmofhost(get_canonical_hostname(1));
410 debug("Kerberos v4: no realm for %s", get_canonical_hostname(1));
413 /* This can really be anything. */
414 checksum = (u_long)getpid();
416 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
418 debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]);
421 /* Get session key to decrypt the server's reply with. */
422 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
424 debug("get_cred failed: %s", krb_err_txt[r]);
427 des_key_sched((des_cblock *) cred.session, schedule);
429 /* Send authentication info to server. */
430 packet_start(SSH_CMSG_AUTH_KERBEROS);
431 packet_put_string((char *) auth.dat, auth.length);
435 /* Zero the buffer. */
436 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
438 slen = sizeof(local);
439 memset(&local, 0, sizeof(local));
440 if (getsockname(packet_get_connection_in(),
441 (struct sockaddr *)&local, &slen) < 0)
442 debug("getsockname failed: %s", strerror(errno));
444 slen = sizeof(foreign);
445 memset(&foreign, 0, sizeof(foreign));
446 if (getpeername(packet_get_connection_in(),
447 (struct sockaddr *)&foreign, &slen) < 0) {
448 debug("getpeername failed: %s", strerror(errno));
451 /* Get server reply. */
452 type = packet_read();
454 case SSH_SMSG_FAILURE:
455 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
456 debug("Kerberos v4 authentication failed.");
460 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
461 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
462 debug("Kerberos v4 authentication accepted.");
464 /* Get server's response. */
465 reply = packet_get_string((u_int *) &auth.length);
466 if (auth.length >= MAX_KTXT_LEN)
467 fatal("Kerberos v4: Malformed response from server");
468 memcpy(auth.dat, reply, auth.length);
474 * If his response isn't properly encrypted with the session
475 * key, and the decrypted checksum fails to match, he's
478 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
479 &foreign, &local, &msg_data);
481 debug("Kerberos v4 krb_rd_priv failed: %s",
483 packet_disconnect("Kerberos v4 challenge failed!");
485 /* Fetch the (incremented) checksum that we supplied in the request. */
486 memcpy((char *)&cksum, (char *)msg_data.app_data,
488 cksum = ntohl(cksum);
490 /* If it matches, we're golden. */
491 if (cksum == checksum + 1) {
492 debug("Kerberos v4 challenge successful.");
495 packet_disconnect("Kerberos v4 challenge failed!");
499 packet_disconnect("Protocol error on Kerberos v4 response: %d", type);
508 try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
510 krb5_error_code problem;
513 krb5_ccache ccache = NULL;
514 const char *remotehost;
517 krb5_ap_rep_enc_part *reply = NULL;
520 memset(&ap, 0, sizeof(ap));
522 problem = krb5_init_context(context);
524 debug("Kerberos v5: krb5_init_context failed");
529 problem = krb5_auth_con_init(*context, auth_context);
531 debug("Kerberos v5: krb5_auth_con_init failed");
537 problem = krb5_auth_con_setflags(*context, *auth_context,
538 KRB5_AUTH_CONTEXT_RET_TIME);
540 debug("Keberos v5: krb5_auth_con_setflags failed");
546 tkfile = krb5_cc_default_name(*context);
547 if (strncmp(tkfile, "FILE:", 5) == 0)
550 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
551 debug("Kerberos v5: could not get default ccache (permission denied).");
556 problem = krb5_cc_default(*context, &ccache);
558 debug("Kerberos v5: krb5_cc_default failed: %s",
559 krb5_get_err_text(*context, problem));
564 remotehost = get_canonical_hostname(1);
566 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
567 "host", remotehost, NULL, ccache, &ap);
569 debug("Kerberos v5: krb5_mk_req failed: %s",
570 krb5_get_err_text(*context, problem));
575 packet_start(SSH_CMSG_AUTH_KERBEROS);
576 packet_put_string((char *) ap.data, ap.length);
583 type = packet_read();
585 case SSH_SMSG_FAILURE:
586 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
587 debug("Kerberos v5 authentication failed.");
591 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
592 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
593 debug("Kerberos v5 authentication accepted.");
595 /* Get server's response. */
596 ap.data = packet_get_string((unsigned int *) &ap.length);
598 /* XXX je to dobre? */
600 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
608 packet_disconnect("Protocol error on Kerberos v5 response: %d",
617 krb5_cc_close(*context, ccache);
619 krb5_free_ap_rep_enc_part(*context, reply);
624 krb5_free_data_contents(*context, &ap);
631 send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
634 krb5_error_code problem;
636 krb5_ccache ccache = NULL;
639 krb5_kdc_flags flags;
643 const char *remotehost;
645 memset(&creds, 0, sizeof(creds));
646 memset(&outbuf, 0, sizeof(outbuf));
648 fd = packet_get_connection_in();
651 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
653 problem = krb5_auth_con_genaddrs(context, auth_context, fd,
654 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
655 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
660 problem = krb5_cc_default(context, &ccache);
664 problem = krb5_cc_get_principal(context, ccache, &creds.client);
668 remotehost = get_canonical_hostname(1);
671 problem = krb5_build_principal(context, &creds.server,
672 strlen(creds.client->realm), creds.client->realm,
673 "krbtgt", creds.client->realm, NULL);
675 problem = krb5_build_principal(context, &creds.server,
676 creds.client->realm.length, creds.client->realm.data,
677 "host", remotehost, NULL);
682 creds.times.endtime = 0;
686 flags.b.forwarded = 1;
687 flags.b.forwardable = krb5_config_get_bool(context, NULL,
688 "libdefaults", "forwardable", NULL);
689 problem = krb5_get_forwarded_creds(context, auth_context,
690 ccache, flags.i, remotehost, &creds, &outbuf);
693 problem = krb5_fwd_tgt_creds(context, auth_context, remotehost,
694 creds.client, creds.server, ccache, forwardable, &outbuf);
700 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
701 packet_put_string((char *)outbuf.data, outbuf.length);
705 type = packet_read();
707 if (type == SSH_SMSG_SUCCESS) {
710 krb5_unparse_name(context, creds.client, &pname);
711 debug("Kerberos v5 TGT forwarded (%s).", pname);
714 debug("Kerberos v5 TGT forwarding failed.");
720 debug("Kerberos v5 TGT forwarding failed: %s",
721 krb5_get_err_text(context, problem));
723 krb5_free_principal(context, creds.client);
725 krb5_free_principal(context, creds.server);
727 krb5_cc_close(context, ccache);
739 char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
742 /* Don't do anything if we don't have any tickets. */
743 if (stat(tkt_string(), &st) < 0)
746 creds = xmalloc(sizeof(*creds));
748 problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm);
752 problem = krb_get_cred("krbtgt", prealm, prealm, creds);
756 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
760 creds_to_radix(creds, (u_char *)buffer, sizeof(buffer));
762 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
763 packet_put_cstring(buffer);
767 type = packet_read();
769 if (type == SSH_SMSG_SUCCESS)
770 debug("Kerberos v4 TGT forwarded (%s%s%s@%s).",
771 creds->pname, creds->pinst[0] ? "." : "",
772 creds->pinst, creds->realm);
774 debug("Kerberos v4 TGT rejected.");
780 debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]);
785 send_afs_tokens(void)
788 struct ViceIoctl parms;
789 struct ClearToken ct;
791 char buf[2048], *p, *server_cell;
794 /* Move over ktc_GetToken, here's something leaner. */
795 for (i = 0; i < 100; i++) { /* just in case */
796 parms.in = (char *) &i;
797 parms.in_size = sizeof(i);
799 parms.out_size = sizeof(buf);
800 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
804 /* Get secret token. */
805 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
806 if (creds.ticket_st.length > MAX_KTXT_LEN)
809 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
810 p += creds.ticket_st.length;
812 /* Get clear token. */
813 memcpy(&len, p, sizeof(len));
814 if (len != sizeof(struct ClearToken))
819 p += sizeof(len); /* primary flag */
822 /* Flesh out our credentials. */
823 strlcpy(creds.service, "afs", sizeof(creds.service));
824 creds.instance[0] = '\0';
825 strlcpy(creds.realm, server_cell, REALM_SZ);
826 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
827 creds.issue_date = ct.BeginTimestamp;
828 creds.lifetime = krb_time_to_life(creds.issue_date,
830 creds.kvno = ct.AuthHandle;
831 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
832 creds.pinst[0] = '\0';
834 /* Encode token, ship it off. */
835 if (creds_to_radix(&creds, (u_char *)buffer,
836 sizeof(buffer)) <= 0)
838 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
839 packet_put_cstring(buffer);
843 /* Roger, Roger. Clearance, Clarence. What's your vector,
845 type = packet_read();
847 if (type == SSH_SMSG_FAILURE)
848 debug("AFS token for cell %s rejected.", server_cell);
849 else if (type != SSH_SMSG_SUCCESS)
850 packet_disconnect("Protocol error on AFS token response: %d", type);
857 * Tries to authenticate with any string-based challenge/response system.
858 * Note that the client code is not tied to s/key or TIS.
861 try_challenge_response_authentication(void)
866 char *challenge, *response;
868 debug("Doing challenge response authentication.");
870 for (i = 0; i < options.number_of_password_prompts; i++) {
871 /* request a challenge */
872 packet_start(SSH_CMSG_AUTH_TIS);
876 type = packet_read();
877 if (type != SSH_SMSG_FAILURE &&
878 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
879 packet_disconnect("Protocol error: got %d in response "
880 "to SSH_CMSG_AUTH_TIS", type);
882 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
883 debug("No challenge.");
886 challenge = packet_get_string(&clen);
888 snprintf(prompt, sizeof prompt, "%s%s", challenge,
889 strchr(challenge, '\n') ? "" : "\nResponse: ");
892 error("Permission denied, please try again.");
893 if (options.cipher == SSH_CIPHER_NONE)
894 log("WARNING: Encryption is disabled! "
895 "Response will be transmitted in clear text.");
896 response = read_passphrase(prompt, 0);
897 if (strcmp(response, "") == 0) {
901 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
902 ssh_put_password(response);
903 memset(response, 0, strlen(response));
907 type = packet_read();
908 if (type == SSH_SMSG_SUCCESS)
910 if (type != SSH_SMSG_FAILURE)
911 packet_disconnect("Protocol error: got %d in response "
912 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
919 * Tries to authenticate with plain passwd authentication.
922 try_password_authentication(char *prompt)
927 debug("Doing password authentication.");
928 if (options.cipher == SSH_CIPHER_NONE)
929 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
930 for (i = 0; i < options.number_of_password_prompts; i++) {
932 error("Permission denied, please try again.");
933 password = read_passphrase(prompt, 0);
934 packet_start(SSH_CMSG_AUTH_PASSWORD);
935 ssh_put_password(password);
936 memset(password, 0, strlen(password));
941 type = packet_read();
942 if (type == SSH_SMSG_SUCCESS)
944 if (type != SSH_SMSG_FAILURE)
945 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
955 ssh_kex(char *host, struct sockaddr *hostaddr)
959 Key *host_key, *server_key;
961 int ssh_cipher_default = SSH_CIPHER_3DES;
962 u_char session_key[SSH_SESSION_KEY_LENGTH];
964 u_int supported_ciphers;
965 u_int server_flags, client_flags;
968 debug("Waiting for server public key.");
970 /* Wait for a public key packet from the server. */
971 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
973 /* Get cookie from the packet. */
974 for (i = 0; i < 8; i++)
975 cookie[i] = packet_get_char();
977 /* Get the public key. */
978 server_key = key_new(KEY_RSA1);
979 bits = packet_get_int();
980 packet_get_bignum(server_key->rsa->e);
981 packet_get_bignum(server_key->rsa->n);
983 rbits = BN_num_bits(server_key->rsa->n);
985 log("Warning: Server lies about size of server public key: "
986 "actual size is %d bits vs. announced %d.", rbits, bits);
987 log("Warning: This may be due to an old implementation of ssh.");
989 /* Get the host key. */
990 host_key = key_new(KEY_RSA1);
991 bits = packet_get_int();
992 packet_get_bignum(host_key->rsa->e);
993 packet_get_bignum(host_key->rsa->n);
995 rbits = BN_num_bits(host_key->rsa->n);
997 log("Warning: Server lies about size of server host key: "
998 "actual size is %d bits vs. announced %d.", rbits, bits);
999 log("Warning: This may be due to an old implementation of ssh.");
1002 /* Get protocol flags. */
1003 server_flags = packet_get_int();
1004 packet_set_protocol_flags(server_flags);
1006 supported_ciphers = packet_get_int();
1007 supported_authentications = packet_get_int();
1010 debug("Received server public key (%d bits) and host key (%d bits).",
1011 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
1013 if (verify_host_key(host, hostaddr, host_key) == -1)
1014 fatal("Host key verification failed.");
1016 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
1018 compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
1020 /* Generate a session key. */
1024 * Generate an encryption key for the session. The key is a 256 bit
1025 * random number, interpreted as a 32-byte key, with the least
1026 * significant 8 bits being the first byte of the key.
1028 for (i = 0; i < 32; i++) {
1030 rand = arc4random();
1031 session_key[i] = rand & 0xff;
1036 * According to the protocol spec, the first byte of the session key
1037 * is the highest byte of the integer. The session key is xored with
1038 * the first 16 bytes of the session id.
1040 if ((key = BN_new()) == NULL)
1041 fatal("respond_to_rsa_challenge: BN_new failed");
1042 BN_set_word(key, 0);
1043 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
1044 BN_lshift(key, key, 8);
1046 BN_add_word(key, session_key[i] ^ session_id[i]);
1048 BN_add_word(key, session_key[i]);
1052 * Encrypt the integer using the public key and host key of the
1053 * server (key with smaller modulus first).
1055 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
1056 /* Public key has smaller modulus. */
1057 if (BN_num_bits(host_key->rsa->n) <
1058 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1059 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
1060 "SSH_KEY_BITS_RESERVED %d",
1061 BN_num_bits(host_key->rsa->n),
1062 BN_num_bits(server_key->rsa->n),
1063 SSH_KEY_BITS_RESERVED);
1065 rsa_public_encrypt(key, key, server_key->rsa);
1066 rsa_public_encrypt(key, key, host_key->rsa);
1068 /* Host key has smaller modulus (or they are equal). */
1069 if (BN_num_bits(server_key->rsa->n) <
1070 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1071 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
1072 "SSH_KEY_BITS_RESERVED %d",
1073 BN_num_bits(server_key->rsa->n),
1074 BN_num_bits(host_key->rsa->n),
1075 SSH_KEY_BITS_RESERVED);
1077 rsa_public_encrypt(key, key, host_key->rsa);
1078 rsa_public_encrypt(key, key, server_key->rsa);
1081 /* Destroy the public keys since we no longer need them. */
1082 key_free(server_key);
1085 if (options.cipher == SSH_CIPHER_NOT_SET) {
1086 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
1087 options.cipher = ssh_cipher_default;
1088 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
1089 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
1090 log("No valid SSH1 cipher, using %.100s instead.",
1091 cipher_name(ssh_cipher_default));
1092 options.cipher = ssh_cipher_default;
1094 /* Check that the selected cipher is supported. */
1095 if (!(supported_ciphers & (1 << options.cipher)))
1096 fatal("Selected cipher type %.100s not supported by server.",
1097 cipher_name(options.cipher));
1099 debug("Encryption type: %.100s", cipher_name(options.cipher));
1101 /* Send the encrypted session key to the server. */
1102 packet_start(SSH_CMSG_SESSION_KEY);
1103 packet_put_char(options.cipher);
1105 /* Send the cookie back to the server. */
1106 for (i = 0; i < 8; i++)
1107 packet_put_char(cookie[i]);
1109 /* Send and destroy the encrypted encryption key integer. */
1110 packet_put_bignum(key);
1113 /* Send protocol flags. */
1114 packet_put_int(client_flags);
1116 /* Send the packet now. */
1118 packet_write_wait();
1120 debug("Sent encrypted session key.");
1122 /* Set the encryption key. */
1123 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1125 /* We will no longer need the session key here. Destroy any extra copies. */
1126 memset(session_key, 0, sizeof(session_key));
1129 * Expect a success message from the server. Note that this message
1130 * will be received in encrypted form.
1132 packet_read_expect(SSH_SMSG_SUCCESS);
1134 debug("Received encrypted confirmation.");
1141 ssh_userauth1(const char *local_user, const char *server_user, char *host,
1142 Sensitive *sensitive)
1145 krb5_context context = NULL;
1146 krb5_auth_context auth_context = NULL;
1150 if (supported_authentications == 0)
1151 fatal("ssh_userauth1: server supports no auth methods");
1153 /* Send the name of the user to log in as on the server. */
1154 packet_start(SSH_CMSG_USER);
1155 packet_put_cstring(server_user);
1157 packet_write_wait();
1160 * The server should respond with success if no authentication is
1161 * needed (the user has no password). Otherwise the server responds
1164 type = packet_read();
1166 /* check whether the connection was accepted without authentication. */
1167 if (type == SSH_SMSG_SUCCESS)
1169 if (type != SSH_SMSG_FAILURE)
1170 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
1173 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1174 options.kerberos_authentication) {
1175 debug("Trying Kerberos v5 authentication.");
1177 if (try_krb5_authentication(&context, &auth_context)) {
1178 type = packet_read();
1179 if (type == SSH_SMSG_SUCCESS)
1181 if (type != SSH_SMSG_FAILURE)
1182 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
1188 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1189 options.kerberos_authentication) {
1190 debug("Trying Kerberos v4 authentication.");
1192 if (try_krb4_authentication()) {
1193 type = packet_read();
1194 if (type == SSH_SMSG_SUCCESS)
1196 if (type != SSH_SMSG_FAILURE)
1197 packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type);
1203 * Use rhosts authentication if running in privileged socket and we
1204 * do not wish to remain anonymous.
1206 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1207 options.rhosts_authentication) {
1208 debug("Trying rhosts authentication.");
1209 packet_start(SSH_CMSG_AUTH_RHOSTS);
1210 packet_put_cstring(local_user);
1212 packet_write_wait();
1214 /* The server should respond with success or failure. */
1215 type = packet_read();
1216 if (type == SSH_SMSG_SUCCESS)
1218 if (type != SSH_SMSG_FAILURE)
1219 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1223 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1226 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1227 options.rhosts_rsa_authentication) {
1228 for (i = 0; i < sensitive->nkeys; i++) {
1229 if (sensitive->keys[i] != NULL &&
1230 sensitive->keys[i]->type == KEY_RSA1 &&
1231 try_rhosts_rsa_authentication(local_user,
1232 sensitive->keys[i]))
1236 /* Try RSA authentication if the server supports it. */
1237 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1238 options.rsa_authentication) {
1240 * Try RSA authentication using the authentication agent. The
1241 * agent is tried first because no passphrase is needed for
1242 * it, whereas identity files may require passphrases.
1244 if (try_agent_authentication())
1247 /* Try RSA authentication for each identity. */
1248 for (i = 0; i < options.num_identity_files; i++)
1249 if (options.identity_keys[i] != NULL &&
1250 options.identity_keys[i]->type == KEY_RSA1 &&
1251 try_rsa_authentication(i))
1254 /* Try challenge response authentication if the server supports it. */
1255 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1256 options.challenge_response_authentication && !options.batch_mode) {
1257 if (try_challenge_response_authentication())
1260 /* Try password authentication if the server supports it. */
1261 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1262 options.password_authentication && !options.batch_mode) {
1265 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1267 if (try_password_authentication(prompt))
1270 /* All authentication methods have failed. Exit with an error message. */
1271 fatal("Permission denied.");
1276 /* Try Kerberos v5 TGT passing. */
1277 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1278 options.kerberos_tgt_passing && context && auth_context) {
1279 if (options.cipher == SSH_CIPHER_NONE)
1280 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1281 send_krb5_tgt(context, auth_context);
1284 krb5_auth_con_free(context, auth_context);
1286 krb5_free_context(context);
1290 /* Try Kerberos v4 TGT passing if the server supports it. */
1291 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1292 options.kerberos_tgt_passing) {
1293 if (options.cipher == SSH_CIPHER_NONE)
1294 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1297 /* Try AFS token passing if the server supports it. */
1298 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1299 options.afs_token_passing && k_hasafs()) {
1300 if (options.cipher == SSH_CIPHER_NONE)
1301 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1306 return; /* need statement after label */