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