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