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