Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1 | /* $OpenBSD: sshconnect1.c,v 1.76 2014/07/15 15:54:14 millert Exp $ */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 6 | * Code to connect to a remote host, and to perform the client side of the |
| 7 | * login (authentication) dialog. |
| 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * As far as I am concerned, the code I have written for this software |
| 10 | * can be used freely for any purpose. Any derived versions of this |
| 11 | * software must be clearly marked as such, and if the derived work is |
| 12 | * incompatible with the protocol description in the RFC file, it must be |
| 13 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "includes.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 17 | |
Damien Miller | 76c0480 | 2015-01-13 19:38:18 +1100 | [diff] [blame] | 18 | #ifdef WITH_SSH1 |
| 19 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/socket.h> |
| 22 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 23 | #include <openssl/bn.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 24 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 25 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 26 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 27 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 28 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 29 | #include <signal.h> |
| 30 | #include <pwd.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 31 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 32 | #include "xmalloc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 33 | #include "ssh.h" |
| 34 | #include "ssh1.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 35 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 36 | #include "buffer.h" |
| 37 | #include "packet.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 38 | #include "key.h" |
| 39 | #include "cipher.h" |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 40 | #include "kex.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 41 | #include "uidswap.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 42 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 43 | #include "misc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "readconf.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 45 | #include "authfd.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 46 | #include "sshconnect.h" |
| 47 | #include "authfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 48 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 49 | #include "hostfile.h" |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 50 | #include "auth.h" |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 51 | #include "digest.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 52 | |
| 53 | /* Session id for the current session. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 54 | u_char session_id[16]; |
| 55 | u_int supported_authentications = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 56 | |
| 57 | extern Options options; |
| 58 | extern char *__progname; |
| 59 | |
| 60 | /* |
| 61 | * Checks if the user has an authentication agent, and if so, tries to |
| 62 | * authenticate using the agent. |
| 63 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 64 | static int |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 65 | try_agent_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 66 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 67 | int type; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 68 | char *comment; |
| 69 | AuthenticationConnection *auth; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 70 | u_char response[16]; |
| 71 | u_int i; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 72 | Key *key; |
| 73 | BIGNUM *challenge; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 74 | |
| 75 | /* Get connection to the agent. */ |
| 76 | auth = ssh_get_authentication_connection(); |
| 77 | if (!auth) |
| 78 | return 0; |
| 79 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 80 | if ((challenge = BN_new()) == NULL) |
| 81 | fatal("try_agent_authentication: BN_new failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 82 | /* Loop through identities served by the agent. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 83 | for (key = ssh_get_first_identity(auth, &comment, 1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 84 | key != NULL; |
| 85 | key = ssh_get_next_identity(auth, &comment, 1)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 86 | |
| 87 | /* Try this identity. */ |
| 88 | debug("Trying RSA authentication via agent with '%.100s'", comment); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 89 | free(comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 90 | |
| 91 | /* Tell the server that we are willing to authenticate using this key. */ |
| 92 | packet_start(SSH_CMSG_AUTH_RSA); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 93 | packet_put_bignum(key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 94 | packet_send(); |
| 95 | packet_write_wait(); |
| 96 | |
| 97 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 98 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 99 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 100 | /* The server sends failure if it doesn't like our key or |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 101 | does not support RSA authentication. */ |
| 102 | if (type == SSH_SMSG_FAILURE) { |
| 103 | debug("Server refused our key."); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 104 | key_free(key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 105 | continue; |
| 106 | } |
| 107 | /* Otherwise it should have sent a challenge. */ |
| 108 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 109 | packet_disconnect("Protocol error during RSA authentication: %d", |
| 110 | type); |
| 111 | |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 112 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 113 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 114 | |
| 115 | debug("Received RSA challenge from server."); |
| 116 | |
| 117 | /* Ask the agent to decrypt the challenge. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 118 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { |
| 119 | /* |
| 120 | * The agent failed to authenticate this identifier |
| 121 | * although it advertised it supports this. Just |
| 122 | * return a wrong value. |
| 123 | */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 124 | logit("Authentication agent failed to decrypt challenge."); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 125 | explicit_bzero(response, sizeof(response)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 126 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 127 | key_free(key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 128 | debug("Sending response to RSA challenge."); |
| 129 | |
| 130 | /* Send the decrypted challenge back to the server. */ |
| 131 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 132 | for (i = 0; i < 16; i++) |
| 133 | packet_put_char(response[i]); |
| 134 | packet_send(); |
| 135 | packet_write_wait(); |
| 136 | |
| 137 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 138 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 139 | |
| 140 | /* The server returns success if it accepted the authentication. */ |
| 141 | if (type == SSH_SMSG_SUCCESS) { |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 142 | ssh_close_authentication_connection(auth); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 143 | BN_clear_free(challenge); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 144 | debug("RSA authentication accepted by server."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 145 | return 1; |
| 146 | } |
| 147 | /* Otherwise it should return failure. */ |
| 148 | if (type != SSH_SMSG_FAILURE) |
| 149 | packet_disconnect("Protocol error waiting RSA auth response: %d", |
| 150 | type); |
| 151 | } |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 152 | ssh_close_authentication_connection(auth); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 153 | BN_clear_free(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 154 | debug("RSA authentication using agent refused."); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Computes the proper response to a RSA challenge, and sends the response to |
| 160 | * the server. |
| 161 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 162 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 163 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
| 164 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 165 | u_char buf[32], response[16]; |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 166 | struct ssh_digest_ctx *md; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 167 | int i, len; |
| 168 | |
| 169 | /* Decrypt the challenge using the private key. */ |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 170 | /* XXX think about Bleichenbacher, too */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 171 | if (rsa_private_decrypt(challenge, challenge, prv) != 0) |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 172 | packet_disconnect( |
| 173 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 174 | |
| 175 | /* Compute the response. */ |
| 176 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 177 | len = BN_num_bytes(challenge); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 178 | if (len <= 0 || (u_int)len > sizeof(buf)) |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 179 | packet_disconnect( |
| 180 | "respond_to_rsa_challenge: bad challenge length %d", len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 181 | |
| 182 | memset(buf, 0, sizeof(buf)); |
| 183 | BN_bn2bin(challenge, buf + sizeof(buf) - len); |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 184 | if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL || |
| 185 | ssh_digest_update(md, buf, 32) < 0 || |
| 186 | ssh_digest_update(md, session_id, 16) < 0 || |
| 187 | ssh_digest_final(md, response, sizeof(response)) < 0) |
| 188 | fatal("%s: md5 failed", __func__); |
| 189 | ssh_digest_free(md); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 190 | |
| 191 | debug("Sending response to host key RSA challenge."); |
| 192 | |
| 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]); |
| 197 | packet_send(); |
| 198 | packet_write_wait(); |
| 199 | |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 200 | explicit_bzero(buf, sizeof(buf)); |
| 201 | explicit_bzero(response, sizeof(response)); |
| 202 | explicit_bzero(&md, sizeof(md)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Checks if the user has authentication file, and if so, tries to authenticate |
| 207 | * the user using it. |
| 208 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 209 | static int |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 210 | try_rsa_authentication(int idx) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 211 | { |
| 212 | BIGNUM *challenge; |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 213 | Key *public, *private; |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 214 | char buf[300], *passphrase, *comment, *authfile; |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 215 | int i, perm_ok = 1, type, quit; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 216 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 217 | public = options.identity_keys[idx]; |
| 218 | authfile = options.identity_files[idx]; |
| 219 | comment = xstrdup(authfile); |
| 220 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 221 | debug("Trying RSA authentication with key '%.100s'", comment); |
| 222 | |
| 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); |
| 226 | packet_send(); |
| 227 | packet_write_wait(); |
| 228 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 229 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 230 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 231 | |
| 232 | /* |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 233 | * The server responds with failure if it doesn't like our key or |
| 234 | * doesn't support RSA authentication. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 235 | */ |
| 236 | if (type == SSH_SMSG_FAILURE) { |
| 237 | debug("Server refused our key."); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 238 | free(comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 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); |
| 244 | |
| 245 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 246 | if ((challenge = BN_new()) == NULL) |
| 247 | fatal("try_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 248 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 249 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 250 | |
| 251 | debug("Received RSA challenge from server."); |
| 252 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 253 | /* |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 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 |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 256 | * fails, ask for a passphrase. |
| 257 | */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 258 | if (public->flags & SSHKEY_FLAG_EXT) |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 259 | private = public; |
| 260 | else |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 261 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL, |
| 262 | &perm_ok); |
| 263 | if (private == NULL && !options.batch_mode && perm_ok) { |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 264 | snprintf(buf, sizeof(buf), |
| 265 | "Enter passphrase for RSA key '%.100s': ", comment); |
| 266 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 267 | passphrase = read_passphrase(buf, 0); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 268 | if (strcmp(passphrase, "") != 0) { |
| 269 | private = key_load_private_type(KEY_RSA1, |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 270 | authfile, passphrase, NULL, NULL); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 271 | quit = 0; |
| 272 | } else { |
| 273 | debug2("no passphrase given, try next key"); |
| 274 | quit = 1; |
| 275 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 276 | explicit_bzero(passphrase, strlen(passphrase)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 277 | free(passphrase); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 278 | if (private != NULL || quit) |
| 279 | break; |
| 280 | debug2("bad passphrase given, try again..."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 281 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 282 | } |
| 283 | /* We no longer need the comment. */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 284 | free(comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 285 | |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 286 | if (private == NULL) { |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 287 | if (!options.batch_mode && perm_ok) |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 288 | error("Bad passphrase."); |
| 289 | |
| 290 | /* Send a dummy response packet to avoid protocol error. */ |
| 291 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 292 | for (i = 0; i < 16; i++) |
| 293 | packet_put_char(0); |
| 294 | packet_send(); |
| 295 | packet_write_wait(); |
| 296 | |
| 297 | /* Expect the server to reject it... */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 298 | packet_read_expect(SSH_SMSG_FAILURE); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 299 | BN_clear_free(challenge); |
| 300 | return 0; |
| 301 | } |
| 302 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 303 | /* Compute and send a response to the challenge. */ |
| 304 | respond_to_rsa_challenge(challenge, private->rsa); |
| 305 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 306 | /* Destroy the private key unless it in external hardware. */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 307 | if (!(private->flags & SSHKEY_FLAG_EXT)) |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 308 | key_free(private); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 309 | |
| 310 | /* We no longer need the challenge. */ |
| 311 | BN_clear_free(challenge); |
| 312 | |
| 313 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 314 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 315 | if (type == SSH_SMSG_SUCCESS) { |
| 316 | debug("RSA authentication accepted by server."); |
| 317 | return 1; |
| 318 | } |
| 319 | if (type != SSH_SMSG_FAILURE) |
| 320 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 321 | debug("RSA authentication refused."); |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv |
| 327 | * authentication and RSA host authentication. |
| 328 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 329 | static int |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 330 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 331 | { |
| 332 | int type; |
| 333 | BIGNUM *challenge; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 334 | |
| 335 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); |
| 336 | |
| 337 | /* Tell the server that we are willing to authenticate using this key. */ |
| 338 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 339 | packet_put_cstring(local_user); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 340 | packet_put_int(BN_num_bits(host_key->rsa->n)); |
| 341 | packet_put_bignum(host_key->rsa->e); |
| 342 | packet_put_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 343 | packet_send(); |
| 344 | packet_write_wait(); |
| 345 | |
| 346 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 347 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 348 | |
| 349 | /* The server responds with failure if it doesn't admit our |
| 350 | .rhosts authentication or doesn't know our host key. */ |
| 351 | if (type == SSH_SMSG_FAILURE) { |
| 352 | debug("Server refused our rhosts authentication or host key."); |
| 353 | return 0; |
| 354 | } |
| 355 | /* Otherwise, the server should respond with a challenge. */ |
| 356 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 357 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
| 358 | |
| 359 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 360 | if ((challenge = BN_new()) == NULL) |
| 361 | fatal("try_rhosts_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 362 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 363 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 364 | |
| 365 | debug("Received RSA challenge for host key from server."); |
| 366 | |
| 367 | /* Compute a response to the challenge. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 368 | respond_to_rsa_challenge(challenge, host_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 369 | |
| 370 | /* We no longer need the challenge. */ |
| 371 | BN_clear_free(challenge); |
| 372 | |
| 373 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 374 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 375 | if (type == SSH_SMSG_SUCCESS) { |
| 376 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); |
| 377 | return 1; |
| 378 | } |
| 379 | if (type != SSH_SMSG_FAILURE) |
| 380 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 381 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); |
| 382 | return 0; |
| 383 | } |
| 384 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 385 | /* |
| 386 | * Tries to authenticate with any string-based challenge/response system. |
| 387 | * Note that the client code is not tied to s/key or TIS. |
| 388 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 389 | static int |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 390 | try_challenge_response_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 391 | { |
| 392 | int type, i; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 393 | u_int clen; |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 394 | char prompt[1024]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 395 | char *challenge, *response; |
Ben Lindstrom | bdfb4df | 2001-10-03 17:12:43 +0000 | [diff] [blame] | 396 | |
| 397 | debug("Doing challenge response authentication."); |
| 398 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 399 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 400 | /* request a challenge */ |
| 401 | packet_start(SSH_CMSG_AUTH_TIS); |
| 402 | packet_send(); |
| 403 | packet_write_wait(); |
| 404 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 405 | type = packet_read(); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 406 | if (type != SSH_SMSG_FAILURE && |
| 407 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
| 408 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 409 | "to SSH_CMSG_AUTH_TIS", type); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 410 | } |
| 411 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 412 | debug("No challenge."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | challenge = packet_get_string(&clen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 416 | packet_check_eom(); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 417 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 418 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 419 | free(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 420 | if (i != 0) |
| 421 | error("Permission denied, please try again."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 422 | if (options.cipher == SSH_CIPHER_NONE) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 423 | logit("WARNING: Encryption is disabled! " |
Damien Miller | f61c015 | 2002-04-23 20:56:02 +1000 | [diff] [blame] | 424 | "Response will be transmitted in clear text."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 425 | response = read_passphrase(prompt, 0); |
| 426 | if (strcmp(response, "") == 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 427 | free(response); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 428 | break; |
| 429 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 430 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 431 | ssh_put_password(response); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 432 | explicit_bzero(response, strlen(response)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 433 | free(response); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 434 | packet_send(); |
| 435 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 436 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 437 | if (type == SSH_SMSG_SUCCESS) |
| 438 | return 1; |
| 439 | if (type != SSH_SMSG_FAILURE) |
| 440 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 441 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 442 | } |
| 443 | /* failure */ |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Tries to authenticate with plain passwd authentication. |
| 449 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 450 | static int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 451 | try_password_authentication(char *prompt) |
| 452 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 453 | int type, i; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 454 | char *password; |
| 455 | |
| 456 | debug("Doing password authentication."); |
| 457 | if (options.cipher == SSH_CIPHER_NONE) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 458 | logit("WARNING: Encryption is disabled! Password will be transmitted in clear text."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 459 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 460 | if (i != 0) |
| 461 | error("Permission denied, please try again."); |
| 462 | password = read_passphrase(prompt, 0); |
| 463 | packet_start(SSH_CMSG_AUTH_PASSWORD); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 464 | ssh_put_password(password); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 465 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 466 | free(password); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 467 | packet_send(); |
| 468 | packet_write_wait(); |
| 469 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 470 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 471 | if (type == SSH_SMSG_SUCCESS) |
| 472 | return 1; |
| 473 | if (type != SSH_SMSG_FAILURE) |
| 474 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); |
| 475 | } |
| 476 | /* failure */ |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * SSH1 key exchange |
| 482 | */ |
| 483 | void |
| 484 | ssh_kex(char *host, struct sockaddr *hostaddr) |
| 485 | { |
| 486 | int i; |
| 487 | BIGNUM *key; |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 488 | Key *host_key, *server_key; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 489 | int bits, rbits; |
| 490 | int ssh_cipher_default = SSH_CIPHER_3DES; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 491 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
| 492 | u_char cookie[8]; |
| 493 | u_int supported_ciphers; |
| 494 | u_int server_flags, client_flags; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 495 | u_int32_t rnd = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 496 | |
| 497 | debug("Waiting for server public key."); |
| 498 | |
| 499 | /* Wait for a public key packet from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 500 | packet_read_expect(SSH_SMSG_PUBLIC_KEY); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 501 | |
| 502 | /* Get cookie from the packet. */ |
| 503 | for (i = 0; i < 8; i++) |
| 504 | cookie[i] = packet_get_char(); |
| 505 | |
| 506 | /* Get the public key. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 507 | server_key = key_new(KEY_RSA1); |
| 508 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 509 | packet_get_bignum(server_key->rsa->e); |
| 510 | packet_get_bignum(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 511 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 512 | rbits = BN_num_bits(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 513 | if (bits != rbits) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 514 | logit("Warning: Server lies about size of server public key: " |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 515 | "actual size is %d bits vs. announced %d.", rbits, bits); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 516 | logit("Warning: This may be due to an old implementation of ssh."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 517 | } |
| 518 | /* Get the host key. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 519 | host_key = key_new(KEY_RSA1); |
| 520 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 521 | packet_get_bignum(host_key->rsa->e); |
| 522 | packet_get_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 523 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 524 | rbits = BN_num_bits(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 525 | if (bits != rbits) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 526 | logit("Warning: Server lies about size of server host key: " |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 527 | "actual size is %d bits vs. announced %d.", rbits, bits); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 528 | logit("Warning: This may be due to an old implementation of ssh."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* Get protocol flags. */ |
| 532 | server_flags = packet_get_int(); |
| 533 | packet_set_protocol_flags(server_flags); |
| 534 | |
| 535 | supported_ciphers = packet_get_int(); |
| 536 | supported_authentications = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 537 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 538 | |
| 539 | debug("Received server public key (%d bits) and host key (%d bits).", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 540 | BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 541 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 542 | if (verify_host_key(host, hostaddr, host_key) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 543 | fatal("Host key verification failed."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 544 | |
| 545 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; |
| 546 | |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 547 | derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 548 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 549 | /* |
| 550 | * Generate an encryption key for the session. The key is a 256 bit |
| 551 | * random number, interpreted as a 32-byte key, with the least |
| 552 | * significant 8 bits being the first byte of the key. |
| 553 | */ |
| 554 | for (i = 0; i < 32; i++) { |
| 555 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 556 | rnd = arc4random(); |
| 557 | session_key[i] = rnd & 0xff; |
| 558 | rnd >>= 8; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* |
| 562 | * According to the protocol spec, the first byte of the session key |
| 563 | * is the highest byte of the integer. The session key is xored with |
| 564 | * the first 16 bytes of the session id. |
| 565 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 566 | if ((key = BN_new()) == NULL) |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 567 | fatal("ssh_kex: BN_new failed"); |
| 568 | if (BN_set_word(key, 0) == 0) |
| 569 | fatal("ssh_kex: BN_set_word failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 570 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 571 | if (BN_lshift(key, key, 8) == 0) |
| 572 | fatal("ssh_kex: BN_lshift failed"); |
| 573 | if (i < 16) { |
| 574 | if (BN_add_word(key, session_key[i] ^ session_id[i]) |
| 575 | == 0) |
| 576 | fatal("ssh_kex: BN_add_word failed"); |
| 577 | } else { |
| 578 | if (BN_add_word(key, session_key[i]) == 0) |
| 579 | fatal("ssh_kex: BN_add_word failed"); |
| 580 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Encrypt the integer using the public key and host key of the |
| 585 | * server (key with smaller modulus first). |
| 586 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 587 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 588 | /* Public key has smaller modulus. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 589 | if (BN_num_bits(host_key->rsa->n) < |
| 590 | BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 591 | fatal("respond_to_rsa_challenge: host_key %d < server_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 592 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 593 | BN_num_bits(host_key->rsa->n), |
| 594 | BN_num_bits(server_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 595 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 596 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 597 | if (rsa_public_encrypt(key, key, server_key->rsa) != 0 || |
| 598 | rsa_public_encrypt(key, key, host_key->rsa) != 0) |
| 599 | fatal("%s: rsa_public_encrypt failed", __func__); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 600 | } else { |
| 601 | /* Host key has smaller modulus (or they are equal). */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 602 | if (BN_num_bits(server_key->rsa->n) < |
| 603 | BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 604 | fatal("respond_to_rsa_challenge: server_key %d < host_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 605 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 606 | BN_num_bits(server_key->rsa->n), |
| 607 | BN_num_bits(host_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 608 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 609 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 610 | if (rsa_public_encrypt(key, key, host_key->rsa) != 0 || |
| 611 | rsa_public_encrypt(key, key, server_key->rsa) != 0) |
| 612 | fatal("%s: rsa_public_encrypt failed", __func__); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* Destroy the public keys since we no longer need them. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 616 | key_free(server_key); |
| 617 | key_free(host_key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 618 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 619 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
| 620 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) |
| 621 | options.cipher = ssh_cipher_default; |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 622 | } else if (options.cipher == SSH_CIPHER_INVALID || |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 623 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 624 | logit("No valid SSH1 cipher, using %.100s instead.", |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 625 | cipher_name(ssh_cipher_default)); |
| 626 | options.cipher = ssh_cipher_default; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 627 | } |
| 628 | /* Check that the selected cipher is supported. */ |
| 629 | if (!(supported_ciphers & (1 << options.cipher))) |
| 630 | fatal("Selected cipher type %.100s not supported by server.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 631 | cipher_name(options.cipher)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 632 | |
| 633 | debug("Encryption type: %.100s", cipher_name(options.cipher)); |
| 634 | |
| 635 | /* Send the encrypted session key to the server. */ |
| 636 | packet_start(SSH_CMSG_SESSION_KEY); |
| 637 | packet_put_char(options.cipher); |
| 638 | |
| 639 | /* Send the cookie back to the server. */ |
| 640 | for (i = 0; i < 8; i++) |
| 641 | packet_put_char(cookie[i]); |
| 642 | |
| 643 | /* Send and destroy the encrypted encryption key integer. */ |
| 644 | packet_put_bignum(key); |
| 645 | BN_clear_free(key); |
| 646 | |
| 647 | /* Send protocol flags. */ |
| 648 | packet_put_int(client_flags); |
| 649 | |
| 650 | /* Send the packet now. */ |
| 651 | packet_send(); |
| 652 | packet_write_wait(); |
| 653 | |
| 654 | debug("Sent encrypted session key."); |
| 655 | |
| 656 | /* Set the encryption key. */ |
| 657 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); |
| 658 | |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 659 | /* |
| 660 | * We will no longer need the session key here. |
| 661 | * Destroy any extra copies. |
| 662 | */ |
| 663 | explicit_bzero(session_key, sizeof(session_key)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 664 | |
| 665 | /* |
| 666 | * Expect a success message from the server. Note that this message |
| 667 | * will be received in encrypted form. |
| 668 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 669 | packet_read_expect(SSH_SMSG_SUCCESS); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 670 | |
| 671 | debug("Received encrypted confirmation."); |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Authenticate user |
| 676 | */ |
| 677 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 678 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 679 | Sensitive *sensitive) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 680 | { |
| 681 | int i, type; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 682 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 683 | if (supported_authentications == 0) |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 684 | fatal("ssh_userauth1: server supports no auth methods"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 685 | |
| 686 | /* Send the name of the user to log in as on the server. */ |
| 687 | packet_start(SSH_CMSG_USER); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 688 | packet_put_cstring(server_user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 689 | packet_send(); |
| 690 | packet_write_wait(); |
| 691 | |
| 692 | /* |
| 693 | * The server should respond with success if no authentication is |
| 694 | * needed (the user has no password). Otherwise the server responds |
| 695 | * with failure. |
| 696 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 697 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 698 | |
| 699 | /* check whether the connection was accepted without authentication. */ |
| 700 | if (type == SSH_SMSG_SUCCESS) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 701 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 702 | if (type != SSH_SMSG_FAILURE) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 703 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 704 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 705 | /* |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 706 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host |
| 707 | * authentication. |
| 708 | */ |
| 709 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 710 | options.rhosts_rsa_authentication) { |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 711 | for (i = 0; i < sensitive->nkeys; i++) { |
| 712 | if (sensitive->keys[i] != NULL && |
| 713 | sensitive->keys[i]->type == KEY_RSA1 && |
| 714 | try_rhosts_rsa_authentication(local_user, |
| 715 | sensitive->keys[i])) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 716 | goto success; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 717 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 718 | } |
| 719 | /* Try RSA authentication if the server supports it. */ |
| 720 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && |
| 721 | options.rsa_authentication) { |
| 722 | /* |
| 723 | * Try RSA authentication using the authentication agent. The |
| 724 | * agent is tried first because no passphrase is needed for |
| 725 | * it, whereas identity files may require passphrases. |
| 726 | */ |
| 727 | if (try_agent_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 728 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 729 | |
| 730 | /* Try RSA authentication for each identity. */ |
| 731 | for (i = 0; i < options.num_identity_files; i++) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 732 | if (options.identity_keys[i] != NULL && |
| 733 | options.identity_keys[i]->type == KEY_RSA1 && |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 734 | try_rsa_authentication(i)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 735 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 736 | } |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 737 | /* Try challenge response authentication if the server supports it. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 738 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 739 | options.challenge_response_authentication && !options.batch_mode) { |
| 740 | if (try_challenge_response_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 741 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 742 | } |
| 743 | /* Try password authentication if the server supports it. */ |
| 744 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && |
| 745 | options.password_authentication && !options.batch_mode) { |
| 746 | char prompt[80]; |
| 747 | |
Ben Lindstrom | e055716 | 2001-02-11 00:00:24 +0000 | [diff] [blame] | 748 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 749 | server_user, host); |
| 750 | if (try_password_authentication(prompt)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 751 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 752 | } |
| 753 | /* All authentication methods have failed. Exit with an error message. */ |
| 754 | fatal("Permission denied."); |
| 755 | /* NOTREACHED */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 756 | |
| 757 | success: |
Damien Miller | 40eb1d8 | 2001-07-14 12:16:59 +1000 | [diff] [blame] | 758 | return; /* need statement after label */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 759 | } |
Damien Miller | 76c0480 | 2015-01-13 19:38:18 +1100 | [diff] [blame] | 760 | |
| 761 | #endif /* WITH_SSH1 */ |