Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 5 | * Code to connect to a remote host, and to perform the client side of the |
| 6 | * login (authentication) dialog. |
| 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include "includes.h" |
Darren Tucker | ec960f2 | 2003-08-13 20:37:05 +1000 | [diff] [blame] | 16 | RCSID("$OpenBSD: sshconnect1.c,v 1.55 2003/08/13 08:46:31 markus Exp $"); |
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 | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 21 | #ifdef KRB5 |
| 22 | #include <krb5.h> |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 23 | #ifndef HEIMDAL |
| 24 | #define krb5_get_err_text(context,code) error_message(code) |
| 25 | #endif /* !HEIMDAL */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 26 | #endif |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 27 | |
| 28 | #include "ssh.h" |
| 29 | #include "ssh1.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 30 | #include "xmalloc.h" |
| 31 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 32 | #include "buffer.h" |
| 33 | #include "packet.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 34 | #include "mpaux.h" |
| 35 | #include "uidswap.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 36 | #include "log.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 37 | #include "readconf.h" |
| 38 | #include "key.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 39 | #include "authfd.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 40 | #include "sshconnect.h" |
| 41 | #include "authfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 42 | #include "readpass.h" |
| 43 | #include "cipher.h" |
| 44 | #include "canohost.h" |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 45 | #include "auth.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 46 | |
| 47 | /* Session id for the current session. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 48 | u_char session_id[16]; |
| 49 | u_int supported_authentications = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 50 | |
| 51 | extern Options options; |
| 52 | extern char *__progname; |
| 53 | |
| 54 | /* |
| 55 | * Checks if the user has an authentication agent, and if so, tries to |
| 56 | * authenticate using the agent. |
| 57 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 58 | static int |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 59 | try_agent_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 60 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 61 | int type; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 62 | char *comment; |
| 63 | AuthenticationConnection *auth; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 64 | u_char response[16]; |
| 65 | u_int i; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 66 | Key *key; |
| 67 | BIGNUM *challenge; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 68 | |
| 69 | /* Get connection to the agent. */ |
| 70 | auth = ssh_get_authentication_connection(); |
| 71 | if (!auth) |
| 72 | return 0; |
| 73 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 74 | if ((challenge = BN_new()) == NULL) |
| 75 | fatal("try_agent_authentication: BN_new failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 76 | /* Loop through identities served by the agent. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 77 | for (key = ssh_get_first_identity(auth, &comment, 1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 78 | key != NULL; |
| 79 | key = ssh_get_next_identity(auth, &comment, 1)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 80 | |
| 81 | /* Try this identity. */ |
| 82 | debug("Trying RSA authentication via agent with '%.100s'", comment); |
| 83 | xfree(comment); |
| 84 | |
| 85 | /* Tell the server that we are willing to authenticate using this key. */ |
| 86 | packet_start(SSH_CMSG_AUTH_RSA); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 87 | packet_put_bignum(key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 88 | packet_send(); |
| 89 | packet_write_wait(); |
| 90 | |
| 91 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 92 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 93 | |
| 94 | /* The server sends failure if it doesn\'t like our key or |
| 95 | does not support RSA authentication. */ |
| 96 | if (type == SSH_SMSG_FAILURE) { |
| 97 | debug("Server refused our key."); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 98 | key_free(key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 99 | continue; |
| 100 | } |
| 101 | /* Otherwise it should have sent a challenge. */ |
| 102 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 103 | packet_disconnect("Protocol error during RSA authentication: %d", |
| 104 | type); |
| 105 | |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 106 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 107 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 108 | |
| 109 | debug("Received RSA challenge from server."); |
| 110 | |
| 111 | /* Ask the agent to decrypt the challenge. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 112 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { |
| 113 | /* |
| 114 | * The agent failed to authenticate this identifier |
| 115 | * although it advertised it supports this. Just |
| 116 | * return a wrong value. |
| 117 | */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 118 | logit("Authentication agent failed to decrypt challenge."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 119 | memset(response, 0, sizeof(response)); |
| 120 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 121 | key_free(key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 122 | debug("Sending response to RSA challenge."); |
| 123 | |
| 124 | /* Send the decrypted challenge back to the server. */ |
| 125 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 126 | for (i = 0; i < 16; i++) |
| 127 | packet_put_char(response[i]); |
| 128 | packet_send(); |
| 129 | packet_write_wait(); |
| 130 | |
| 131 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 132 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 133 | |
| 134 | /* The server returns success if it accepted the authentication. */ |
| 135 | if (type == SSH_SMSG_SUCCESS) { |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 136 | ssh_close_authentication_connection(auth); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 137 | BN_clear_free(challenge); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 138 | debug("RSA authentication accepted by server."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 139 | return 1; |
| 140 | } |
| 141 | /* Otherwise it should return failure. */ |
| 142 | if (type != SSH_SMSG_FAILURE) |
| 143 | packet_disconnect("Protocol error waiting RSA auth response: %d", |
| 144 | type); |
| 145 | } |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 146 | ssh_close_authentication_connection(auth); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 147 | BN_clear_free(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 148 | debug("RSA authentication using agent refused."); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Computes the proper response to a RSA challenge, and sends the response to |
| 154 | * the server. |
| 155 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 156 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 157 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
| 158 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 159 | u_char buf[32], response[16]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 160 | MD5_CTX md; |
| 161 | int i, len; |
| 162 | |
| 163 | /* Decrypt the challenge using the private key. */ |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 164 | /* XXX think about Bleichenbacher, too */ |
| 165 | if (rsa_private_decrypt(challenge, challenge, prv) <= 0) |
| 166 | packet_disconnect( |
| 167 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 168 | |
| 169 | /* Compute the response. */ |
| 170 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 171 | len = BN_num_bytes(challenge); |
| 172 | if (len <= 0 || len > sizeof(buf)) |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 173 | packet_disconnect( |
| 174 | "respond_to_rsa_challenge: bad challenge length %d", len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 175 | |
| 176 | memset(buf, 0, sizeof(buf)); |
| 177 | BN_bn2bin(challenge, buf + sizeof(buf) - len); |
| 178 | MD5_Init(&md); |
| 179 | MD5_Update(&md, buf, 32); |
| 180 | MD5_Update(&md, session_id, 16); |
| 181 | MD5_Final(response, &md); |
| 182 | |
| 183 | debug("Sending response to host key RSA challenge."); |
| 184 | |
| 185 | /* Send the response back to the server. */ |
| 186 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 187 | for (i = 0; i < 16; i++) |
| 188 | packet_put_char(response[i]); |
| 189 | packet_send(); |
| 190 | packet_write_wait(); |
| 191 | |
| 192 | memset(buf, 0, sizeof(buf)); |
| 193 | memset(response, 0, sizeof(response)); |
| 194 | memset(&md, 0, sizeof(md)); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Checks if the user has authentication file, and if so, tries to authenticate |
| 199 | * the user using it. |
| 200 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 201 | static int |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 202 | try_rsa_authentication(int idx) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 203 | { |
| 204 | BIGNUM *challenge; |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 205 | Key *public, *private; |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 206 | char buf[300], *passphrase, *comment, *authfile; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 207 | int i, type, quit; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 208 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 209 | public = options.identity_keys[idx]; |
| 210 | authfile = options.identity_files[idx]; |
| 211 | comment = xstrdup(authfile); |
| 212 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 213 | debug("Trying RSA authentication with key '%.100s'", comment); |
| 214 | |
| 215 | /* Tell the server that we are willing to authenticate using this key. */ |
| 216 | packet_start(SSH_CMSG_AUTH_RSA); |
| 217 | packet_put_bignum(public->rsa->n); |
| 218 | packet_send(); |
| 219 | packet_write_wait(); |
| 220 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 221 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 222 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * The server responds with failure if it doesn\'t like our key or |
| 226 | * doesn\'t support RSA authentication. |
| 227 | */ |
| 228 | if (type == SSH_SMSG_FAILURE) { |
| 229 | debug("Server refused our key."); |
| 230 | xfree(comment); |
| 231 | return 0; |
| 232 | } |
| 233 | /* Otherwise, the server should respond with a challenge. */ |
| 234 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 235 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
| 236 | |
| 237 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 238 | if ((challenge = BN_new()) == NULL) |
| 239 | fatal("try_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 240 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 241 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 242 | |
| 243 | debug("Received RSA challenge from server."); |
| 244 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 245 | /* |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 246 | * If the key is not stored in external hardware, we have to |
| 247 | * load the private key. Try first with empty passphrase; if it |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 248 | * fails, ask for a passphrase. |
| 249 | */ |
Ben Lindstrom | e143f61 | 2002-08-20 18:41:15 +0000 | [diff] [blame] | 250 | if (public->flags & KEY_FLAG_EXT) |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 251 | private = public; |
| 252 | else |
| 253 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 254 | if (private == NULL && !options.batch_mode) { |
| 255 | snprintf(buf, sizeof(buf), |
| 256 | "Enter passphrase for RSA key '%.100s': ", comment); |
| 257 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 258 | passphrase = read_passphrase(buf, 0); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 259 | if (strcmp(passphrase, "") != 0) { |
| 260 | private = key_load_private_type(KEY_RSA1, |
| 261 | authfile, passphrase, NULL); |
| 262 | quit = 0; |
| 263 | } else { |
| 264 | debug2("no passphrase given, try next key"); |
| 265 | quit = 1; |
| 266 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 267 | memset(passphrase, 0, strlen(passphrase)); |
| 268 | xfree(passphrase); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 269 | if (private != NULL || quit) |
| 270 | break; |
| 271 | debug2("bad passphrase given, try again..."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 272 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 273 | } |
| 274 | /* We no longer need the comment. */ |
| 275 | xfree(comment); |
| 276 | |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 277 | if (private == NULL) { |
| 278 | if (!options.batch_mode) |
| 279 | error("Bad passphrase."); |
| 280 | |
| 281 | /* Send a dummy response packet to avoid protocol error. */ |
| 282 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 283 | for (i = 0; i < 16; i++) |
| 284 | packet_put_char(0); |
| 285 | packet_send(); |
| 286 | packet_write_wait(); |
| 287 | |
| 288 | /* Expect the server to reject it... */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 289 | packet_read_expect(SSH_SMSG_FAILURE); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 290 | BN_clear_free(challenge); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 294 | /* Compute and send a response to the challenge. */ |
| 295 | respond_to_rsa_challenge(challenge, private->rsa); |
| 296 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 297 | /* Destroy the private key unless it in external hardware. */ |
| 298 | if (!(private->flags & KEY_FLAG_EXT)) |
| 299 | key_free(private); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 300 | |
| 301 | /* We no longer need the challenge. */ |
| 302 | BN_clear_free(challenge); |
| 303 | |
| 304 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 305 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 306 | if (type == SSH_SMSG_SUCCESS) { |
| 307 | debug("RSA authentication accepted by server."); |
| 308 | return 1; |
| 309 | } |
| 310 | if (type != SSH_SMSG_FAILURE) |
| 311 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 312 | debug("RSA authentication refused."); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv |
| 318 | * authentication and RSA host authentication. |
| 319 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 320 | static int |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 321 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 322 | { |
| 323 | int type; |
| 324 | BIGNUM *challenge; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 325 | |
| 326 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); |
| 327 | |
| 328 | /* Tell the server that we are willing to authenticate using this key. */ |
| 329 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 330 | packet_put_cstring(local_user); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 331 | packet_put_int(BN_num_bits(host_key->rsa->n)); |
| 332 | packet_put_bignum(host_key->rsa->e); |
| 333 | packet_put_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 334 | packet_send(); |
| 335 | packet_write_wait(); |
| 336 | |
| 337 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 338 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 339 | |
| 340 | /* The server responds with failure if it doesn't admit our |
| 341 | .rhosts authentication or doesn't know our host key. */ |
| 342 | if (type == SSH_SMSG_FAILURE) { |
| 343 | debug("Server refused our rhosts authentication or host key."); |
| 344 | return 0; |
| 345 | } |
| 346 | /* Otherwise, the server should respond with a challenge. */ |
| 347 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 348 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
| 349 | |
| 350 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 351 | if ((challenge = BN_new()) == NULL) |
| 352 | fatal("try_rhosts_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 353 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 354 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 355 | |
| 356 | debug("Received RSA challenge for host key from server."); |
| 357 | |
| 358 | /* Compute a response to the challenge. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 359 | respond_to_rsa_challenge(challenge, host_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 360 | |
| 361 | /* We no longer need the challenge. */ |
| 362 | BN_clear_free(challenge); |
| 363 | |
| 364 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 365 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 366 | if (type == SSH_SMSG_SUCCESS) { |
| 367 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); |
| 368 | return 1; |
| 369 | } |
| 370 | if (type != SSH_SMSG_FAILURE) |
| 371 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 372 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); |
| 373 | return 0; |
| 374 | } |
| 375 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 376 | #ifdef KRB5 |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 377 | static int |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 378 | try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context) |
| 379 | { |
| 380 | krb5_error_code problem; |
| 381 | const char *tkfile; |
| 382 | struct stat buf; |
| 383 | krb5_ccache ccache = NULL; |
| 384 | const char *remotehost; |
| 385 | krb5_data ap; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 386 | int type; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 387 | krb5_ap_rep_enc_part *reply = NULL; |
| 388 | int ret; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 389 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 390 | memset(&ap, 0, sizeof(ap)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 391 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 392 | problem = krb5_init_context(context); |
| 393 | if (problem) { |
| 394 | debug("Kerberos v5: krb5_init_context failed"); |
| 395 | ret = 0; |
| 396 | goto out; |
| 397 | } |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 398 | |
| 399 | problem = krb5_auth_con_init(*context, auth_context); |
| 400 | if (problem) { |
| 401 | debug("Kerberos v5: krb5_auth_con_init failed"); |
| 402 | ret = 0; |
| 403 | goto out; |
| 404 | } |
| 405 | |
| 406 | #ifndef HEIMDAL |
| 407 | problem = krb5_auth_con_setflags(*context, *auth_context, |
| 408 | KRB5_AUTH_CONTEXT_RET_TIME); |
| 409 | if (problem) { |
| 410 | debug("Keberos v5: krb5_auth_con_setflags failed"); |
| 411 | ret = 0; |
| 412 | goto out; |
| 413 | } |
| 414 | #endif |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 415 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 416 | tkfile = krb5_cc_default_name(*context); |
| 417 | if (strncmp(tkfile, "FILE:", 5) == 0) |
| 418 | tkfile += 5; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 419 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 420 | if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) { |
| 421 | debug("Kerberos v5: could not get default ccache (permission denied)."); |
| 422 | ret = 0; |
| 423 | goto out; |
| 424 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 425 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 426 | problem = krb5_cc_default(*context, &ccache); |
| 427 | if (problem) { |
| 428 | debug("Kerberos v5: krb5_cc_default failed: %s", |
| 429 | krb5_get_err_text(*context, problem)); |
| 430 | ret = 0; |
| 431 | goto out; |
| 432 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 433 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 434 | remotehost = get_canonical_hostname(1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 435 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 436 | problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED, |
| 437 | "host", remotehost, NULL, ccache, &ap); |
| 438 | if (problem) { |
| 439 | debug("Kerberos v5: krb5_mk_req failed: %s", |
| 440 | krb5_get_err_text(*context, problem)); |
| 441 | ret = 0; |
| 442 | goto out; |
| 443 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 444 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 445 | packet_start(SSH_CMSG_AUTH_KERBEROS); |
| 446 | packet_put_string((char *) ap.data, ap.length); |
| 447 | packet_send(); |
| 448 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 449 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 450 | xfree(ap.data); |
| 451 | ap.length = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 452 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 453 | type = packet_read(); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 454 | switch (type) { |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 455 | case SSH_SMSG_FAILURE: |
| 456 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ |
| 457 | debug("Kerberos v5 authentication failed."); |
| 458 | ret = 0; |
| 459 | break; |
| 460 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 461 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 462 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ |
| 463 | debug("Kerberos v5 authentication accepted."); |
| 464 | |
| 465 | /* Get server's response. */ |
| 466 | ap.data = packet_get_string((unsigned int *) &ap.length); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 467 | packet_check_eom(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 468 | /* XXX je to dobre? */ |
| 469 | |
| 470 | problem = krb5_rd_rep(*context, *auth_context, &ap, &reply); |
| 471 | if (problem) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 472 | ret = 0; |
| 473 | } |
| 474 | ret = 1; |
| 475 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 476 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 477 | default: |
| 478 | packet_disconnect("Protocol error on Kerberos v5 response: %d", |
| 479 | type); |
| 480 | ret = 0; |
| 481 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 482 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 483 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 484 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 485 | out: |
| 486 | if (ccache != NULL) |
| 487 | krb5_cc_close(*context, ccache); |
| 488 | if (reply != NULL) |
| 489 | krb5_free_ap_rep_enc_part(*context, reply); |
| 490 | if (ap.length > 0) |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 491 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 492 | krb5_data_free(&ap); |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 493 | #else |
| 494 | krb5_free_data_contents(*context, &ap); |
| 495 | #endif |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 496 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 497 | return (ret); |
| 498 | } |
| 499 | |
| 500 | static void |
| 501 | send_krb5_tgt(krb5_context context, krb5_auth_context auth_context) |
| 502 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 503 | int fd, type; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 504 | krb5_error_code problem; |
| 505 | krb5_data outbuf; |
| 506 | krb5_ccache ccache = NULL; |
| 507 | krb5_creds creds; |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 508 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 509 | krb5_kdc_flags flags; |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 510 | #else |
| 511 | int forwardable; |
| 512 | #endif |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 513 | const char *remotehost; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 514 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 515 | memset(&creds, 0, sizeof(creds)); |
| 516 | memset(&outbuf, 0, sizeof(outbuf)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 517 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 518 | fd = packet_get_connection_in(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 519 | |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 520 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 521 | problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd); |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 522 | #else |
| 523 | problem = krb5_auth_con_genaddrs(context, auth_context, fd, |
| 524 | KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR | |
| 525 | KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR); |
| 526 | #endif |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 527 | if (problem) |
| 528 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 529 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 530 | problem = krb5_cc_default(context, &ccache); |
| 531 | if (problem) |
| 532 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 533 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 534 | problem = krb5_cc_get_principal(context, ccache, &creds.client); |
| 535 | if (problem) |
| 536 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 537 | |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 538 | remotehost = get_canonical_hostname(1); |
| 539 | |
| 540 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 541 | problem = krb5_build_principal(context, &creds.server, |
| 542 | strlen(creds.client->realm), creds.client->realm, |
| 543 | "krbtgt", creds.client->realm, NULL); |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 544 | #else |
| 545 | problem = krb5_build_principal(context, &creds.server, |
| 546 | creds.client->realm.length, creds.client->realm.data, |
| 547 | "host", remotehost, NULL); |
| 548 | #endif |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 549 | if (problem) |
| 550 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 551 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 552 | creds.times.endtime = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 553 | |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 554 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 555 | flags.i = 0; |
| 556 | flags.b.forwarded = 1; |
| 557 | flags.b.forwardable = krb5_config_get_bool(context, NULL, |
| 558 | "libdefaults", "forwardable", NULL); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 559 | problem = krb5_get_forwarded_creds(context, auth_context, |
| 560 | ccache, flags.i, remotehost, &creds, &outbuf); |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 561 | #else |
| 562 | forwardable = 1; |
| 563 | problem = krb5_fwd_tgt_creds(context, auth_context, remotehost, |
| 564 | creds.client, creds.server, ccache, forwardable, &outbuf); |
| 565 | #endif |
| 566 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 567 | if (problem) |
| 568 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 569 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 570 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
| 571 | packet_put_string((char *)outbuf.data, outbuf.length); |
| 572 | packet_send(); |
| 573 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 574 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 575 | type = packet_read(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 576 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 577 | if (type == SSH_SMSG_SUCCESS) { |
| 578 | char *pname; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 579 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 580 | krb5_unparse_name(context, creds.client, &pname); |
| 581 | debug("Kerberos v5 TGT forwarded (%s).", pname); |
| 582 | xfree(pname); |
| 583 | } else |
| 584 | debug("Kerberos v5 TGT forwarding failed."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 585 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 586 | return; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 587 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 588 | out: |
| 589 | if (problem) |
| 590 | debug("Kerberos v5 TGT forwarding failed: %s", |
| 591 | krb5_get_err_text(context, problem)); |
| 592 | if (creds.client) |
| 593 | krb5_free_principal(context, creds.client); |
| 594 | if (creds.server) |
| 595 | krb5_free_principal(context, creds.server); |
| 596 | if (ccache) |
| 597 | krb5_cc_close(context, ccache); |
| 598 | if (outbuf.data) |
| 599 | xfree(outbuf.data); |
| 600 | } |
| 601 | #endif /* KRB5 */ |
| 602 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 603 | /* |
| 604 | * Tries to authenticate with any string-based challenge/response system. |
| 605 | * Note that the client code is not tied to s/key or TIS. |
| 606 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 607 | static int |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 608 | try_challenge_response_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 609 | { |
| 610 | int type, i; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 611 | u_int clen; |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 612 | char prompt[1024]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 613 | char *challenge, *response; |
Ben Lindstrom | bdfb4df | 2001-10-03 17:12:43 +0000 | [diff] [blame] | 614 | |
| 615 | debug("Doing challenge response authentication."); |
| 616 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 617 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 618 | /* request a challenge */ |
| 619 | packet_start(SSH_CMSG_AUTH_TIS); |
| 620 | packet_send(); |
| 621 | packet_write_wait(); |
| 622 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 623 | type = packet_read(); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 624 | if (type != SSH_SMSG_FAILURE && |
| 625 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
| 626 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 627 | "to SSH_CMSG_AUTH_TIS", type); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 628 | } |
| 629 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 630 | debug("No challenge."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 631 | return 0; |
| 632 | } |
| 633 | challenge = packet_get_string(&clen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 634 | packet_check_eom(); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 635 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 636 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 637 | xfree(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 638 | if (i != 0) |
| 639 | error("Permission denied, please try again."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 640 | if (options.cipher == SSH_CIPHER_NONE) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 641 | logit("WARNING: Encryption is disabled! " |
Damien Miller | f61c015 | 2002-04-23 20:56:02 +1000 | [diff] [blame] | 642 | "Response will be transmitted in clear text."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 643 | response = read_passphrase(prompt, 0); |
| 644 | if (strcmp(response, "") == 0) { |
| 645 | xfree(response); |
| 646 | break; |
| 647 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 648 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 649 | ssh_put_password(response); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 650 | memset(response, 0, strlen(response)); |
| 651 | xfree(response); |
| 652 | packet_send(); |
| 653 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 654 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 655 | if (type == SSH_SMSG_SUCCESS) |
| 656 | return 1; |
| 657 | if (type != SSH_SMSG_FAILURE) |
| 658 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 659 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 660 | } |
| 661 | /* failure */ |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Tries to authenticate with plain passwd authentication. |
| 667 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 668 | static int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 669 | try_password_authentication(char *prompt) |
| 670 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 671 | int type, i; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 672 | char *password; |
| 673 | |
| 674 | debug("Doing password authentication."); |
| 675 | if (options.cipher == SSH_CIPHER_NONE) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 676 | logit("WARNING: Encryption is disabled! Password will be transmitted in clear text."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 677 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 678 | if (i != 0) |
| 679 | error("Permission denied, please try again."); |
| 680 | password = read_passphrase(prompt, 0); |
| 681 | packet_start(SSH_CMSG_AUTH_PASSWORD); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 682 | ssh_put_password(password); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 683 | memset(password, 0, strlen(password)); |
| 684 | xfree(password); |
| 685 | packet_send(); |
| 686 | packet_write_wait(); |
| 687 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 688 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 689 | if (type == SSH_SMSG_SUCCESS) |
| 690 | return 1; |
| 691 | if (type != SSH_SMSG_FAILURE) |
| 692 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); |
| 693 | } |
| 694 | /* failure */ |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * SSH1 key exchange |
| 700 | */ |
| 701 | void |
| 702 | ssh_kex(char *host, struct sockaddr *hostaddr) |
| 703 | { |
| 704 | int i; |
| 705 | BIGNUM *key; |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 706 | Key *host_key, *server_key; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 707 | int bits, rbits; |
| 708 | int ssh_cipher_default = SSH_CIPHER_3DES; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 709 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
| 710 | u_char cookie[8]; |
| 711 | u_int supported_ciphers; |
| 712 | u_int server_flags, client_flags; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 713 | u_int32_t rand = 0; |
| 714 | |
| 715 | debug("Waiting for server public key."); |
| 716 | |
| 717 | /* Wait for a public key packet from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 718 | packet_read_expect(SSH_SMSG_PUBLIC_KEY); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 719 | |
| 720 | /* Get cookie from the packet. */ |
| 721 | for (i = 0; i < 8; i++) |
| 722 | cookie[i] = packet_get_char(); |
| 723 | |
| 724 | /* Get the public key. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 725 | server_key = key_new(KEY_RSA1); |
| 726 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 727 | packet_get_bignum(server_key->rsa->e); |
| 728 | packet_get_bignum(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 729 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 730 | rbits = BN_num_bits(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 731 | if (bits != rbits) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 732 | logit("Warning: Server lies about size of server public key: " |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 733 | "actual size is %d bits vs. announced %d.", rbits, bits); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 734 | logit("Warning: This may be due to an old implementation of ssh."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 735 | } |
| 736 | /* Get the host key. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 737 | host_key = key_new(KEY_RSA1); |
| 738 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 739 | packet_get_bignum(host_key->rsa->e); |
| 740 | packet_get_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 741 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 742 | rbits = BN_num_bits(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 743 | if (bits != rbits) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 744 | logit("Warning: Server lies about size of server host key: " |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 745 | "actual size is %d bits vs. announced %d.", rbits, bits); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 746 | logit("Warning: This may be due to an old implementation of ssh."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* Get protocol flags. */ |
| 750 | server_flags = packet_get_int(); |
| 751 | packet_set_protocol_flags(server_flags); |
| 752 | |
| 753 | supported_ciphers = packet_get_int(); |
| 754 | supported_authentications = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 755 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 756 | |
| 757 | debug("Received server public key (%d bits) and host key (%d bits).", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 758 | 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] | 759 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 760 | if (verify_host_key(host, hostaddr, host_key) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 761 | fatal("Host key verification failed."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 762 | |
| 763 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; |
| 764 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 765 | compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 766 | |
| 767 | /* Generate a session key. */ |
| 768 | arc4random_stir(); |
| 769 | |
| 770 | /* |
| 771 | * Generate an encryption key for the session. The key is a 256 bit |
| 772 | * random number, interpreted as a 32-byte key, with the least |
| 773 | * significant 8 bits being the first byte of the key. |
| 774 | */ |
| 775 | for (i = 0; i < 32; i++) { |
| 776 | if (i % 4 == 0) |
| 777 | rand = arc4random(); |
| 778 | session_key[i] = rand & 0xff; |
| 779 | rand >>= 8; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * According to the protocol spec, the first byte of the session key |
| 784 | * is the highest byte of the integer. The session key is xored with |
| 785 | * the first 16 bytes of the session id. |
| 786 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 787 | if ((key = BN_new()) == NULL) |
| 788 | fatal("respond_to_rsa_challenge: BN_new failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 789 | BN_set_word(key, 0); |
| 790 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { |
| 791 | BN_lshift(key, key, 8); |
| 792 | if (i < 16) |
| 793 | BN_add_word(key, session_key[i] ^ session_id[i]); |
| 794 | else |
| 795 | BN_add_word(key, session_key[i]); |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Encrypt the integer using the public key and host key of the |
| 800 | * server (key with smaller modulus first). |
| 801 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 802 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 803 | /* Public key has smaller modulus. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 804 | if (BN_num_bits(host_key->rsa->n) < |
| 805 | BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 806 | fatal("respond_to_rsa_challenge: host_key %d < server_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 807 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 808 | BN_num_bits(host_key->rsa->n), |
| 809 | BN_num_bits(server_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 810 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 811 | } |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 812 | rsa_public_encrypt(key, key, server_key->rsa); |
| 813 | rsa_public_encrypt(key, key, host_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 814 | } else { |
| 815 | /* Host key has smaller modulus (or they are equal). */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 816 | if (BN_num_bits(server_key->rsa->n) < |
| 817 | BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 818 | fatal("respond_to_rsa_challenge: server_key %d < host_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 819 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 820 | BN_num_bits(server_key->rsa->n), |
| 821 | BN_num_bits(host_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 822 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 823 | } |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 824 | rsa_public_encrypt(key, key, host_key->rsa); |
| 825 | rsa_public_encrypt(key, key, server_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /* Destroy the public keys since we no longer need them. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 829 | key_free(server_key); |
| 830 | key_free(host_key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 831 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 832 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
| 833 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) |
| 834 | options.cipher = ssh_cipher_default; |
| 835 | } else if (options.cipher == SSH_CIPHER_ILLEGAL || |
| 836 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 837 | logit("No valid SSH1 cipher, using %.100s instead.", |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 838 | cipher_name(ssh_cipher_default)); |
| 839 | options.cipher = ssh_cipher_default; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 840 | } |
| 841 | /* Check that the selected cipher is supported. */ |
| 842 | if (!(supported_ciphers & (1 << options.cipher))) |
| 843 | fatal("Selected cipher type %.100s not supported by server.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 844 | cipher_name(options.cipher)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 845 | |
| 846 | debug("Encryption type: %.100s", cipher_name(options.cipher)); |
| 847 | |
| 848 | /* Send the encrypted session key to the server. */ |
| 849 | packet_start(SSH_CMSG_SESSION_KEY); |
| 850 | packet_put_char(options.cipher); |
| 851 | |
| 852 | /* Send the cookie back to the server. */ |
| 853 | for (i = 0; i < 8; i++) |
| 854 | packet_put_char(cookie[i]); |
| 855 | |
| 856 | /* Send and destroy the encrypted encryption key integer. */ |
| 857 | packet_put_bignum(key); |
| 858 | BN_clear_free(key); |
| 859 | |
| 860 | /* Send protocol flags. */ |
| 861 | packet_put_int(client_flags); |
| 862 | |
| 863 | /* Send the packet now. */ |
| 864 | packet_send(); |
| 865 | packet_write_wait(); |
| 866 | |
| 867 | debug("Sent encrypted session key."); |
| 868 | |
| 869 | /* Set the encryption key. */ |
| 870 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); |
| 871 | |
| 872 | /* We will no longer need the session key here. Destroy any extra copies. */ |
| 873 | memset(session_key, 0, sizeof(session_key)); |
| 874 | |
| 875 | /* |
| 876 | * Expect a success message from the server. Note that this message |
| 877 | * will be received in encrypted form. |
| 878 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 879 | packet_read_expect(SSH_SMSG_SUCCESS); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 880 | |
| 881 | debug("Received encrypted confirmation."); |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Authenticate user |
| 886 | */ |
| 887 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 888 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 889 | Sensitive *sensitive) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 890 | { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 891 | #ifdef KRB5 |
| 892 | krb5_context context = NULL; |
| 893 | krb5_auth_context auth_context = NULL; |
| 894 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 895 | int i, type; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 896 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 897 | if (supported_authentications == 0) |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 898 | fatal("ssh_userauth1: server supports no auth methods"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 899 | |
| 900 | /* Send the name of the user to log in as on the server. */ |
| 901 | packet_start(SSH_CMSG_USER); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 902 | packet_put_cstring(server_user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 903 | packet_send(); |
| 904 | packet_write_wait(); |
| 905 | |
| 906 | /* |
| 907 | * The server should respond with success if no authentication is |
| 908 | * needed (the user has no password). Otherwise the server responds |
| 909 | * with failure. |
| 910 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 911 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 912 | |
| 913 | /* check whether the connection was accepted without authentication. */ |
| 914 | if (type == SSH_SMSG_SUCCESS) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 915 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 916 | if (type != SSH_SMSG_FAILURE) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 917 | 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] | 918 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 919 | #ifdef KRB5 |
| 920 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 921 | options.kerberos_authentication) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 922 | debug("Trying Kerberos v5 authentication."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 923 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 924 | if (try_krb5_authentication(&context, &auth_context)) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 925 | type = packet_read(); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 926 | if (type == SSH_SMSG_SUCCESS) |
| 927 | goto success; |
| 928 | if (type != SSH_SMSG_FAILURE) |
| 929 | packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type); |
| 930 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 931 | } |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 932 | #endif /* KRB5 */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 933 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 934 | /* |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 935 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host |
| 936 | * authentication. |
| 937 | */ |
| 938 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 939 | options.rhosts_rsa_authentication) { |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 940 | for (i = 0; i < sensitive->nkeys; i++) { |
| 941 | if (sensitive->keys[i] != NULL && |
| 942 | sensitive->keys[i]->type == KEY_RSA1 && |
| 943 | try_rhosts_rsa_authentication(local_user, |
| 944 | sensitive->keys[i])) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 945 | goto success; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 946 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 947 | } |
| 948 | /* Try RSA authentication if the server supports it. */ |
| 949 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && |
| 950 | options.rsa_authentication) { |
| 951 | /* |
| 952 | * Try RSA authentication using the authentication agent. The |
| 953 | * agent is tried first because no passphrase is needed for |
| 954 | * it, whereas identity files may require passphrases. |
| 955 | */ |
| 956 | if (try_agent_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 957 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 958 | |
| 959 | /* Try RSA authentication for each identity. */ |
| 960 | for (i = 0; i < options.num_identity_files; i++) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 961 | if (options.identity_keys[i] != NULL && |
| 962 | options.identity_keys[i]->type == KEY_RSA1 && |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 963 | try_rsa_authentication(i)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 964 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 965 | } |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 966 | /* Try challenge response authentication if the server supports it. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 967 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 968 | options.challenge_response_authentication && !options.batch_mode) { |
| 969 | if (try_challenge_response_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 970 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 971 | } |
| 972 | /* Try password authentication if the server supports it. */ |
| 973 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && |
| 974 | options.password_authentication && !options.batch_mode) { |
| 975 | char prompt[80]; |
| 976 | |
Ben Lindstrom | e055716 | 2001-02-11 00:00:24 +0000 | [diff] [blame] | 977 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 978 | server_user, host); |
| 979 | if (try_password_authentication(prompt)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 980 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 981 | } |
| 982 | /* All authentication methods have failed. Exit with an error message. */ |
| 983 | fatal("Permission denied."); |
| 984 | /* NOTREACHED */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 985 | |
| 986 | success: |
| 987 | #ifdef KRB5 |
| 988 | /* Try Kerberos v5 TGT passing. */ |
| 989 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && |
| 990 | options.kerberos_tgt_passing && context && auth_context) { |
| 991 | if (options.cipher == SSH_CIPHER_NONE) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 992 | logit("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 993 | send_krb5_tgt(context, auth_context); |
| 994 | } |
| 995 | if (auth_context) |
| 996 | krb5_auth_con_free(context, auth_context); |
| 997 | if (context) |
| 998 | krb5_free_context(context); |
| 999 | #endif |
Damien Miller | 40eb1d8 | 2001-07-14 12:16:59 +1000 | [diff] [blame] | 1000 | return; /* need statement after label */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1001 | } |