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" |
Ben Lindstrom | e143f61 | 2002-08-20 18:41:15 +0000 | [diff] [blame] | 16 | RCSID("$OpenBSD: sshconnect1.c,v 1.52 2002/08/08 13:50:23 aaron 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 | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 21 | #ifdef KRB4 |
| 22 | #include <krb.h> |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 23 | #endif |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 24 | #ifdef KRB5 |
| 25 | #include <krb5.h> |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 26 | #ifndef HEIMDAL |
| 27 | #define krb5_get_err_text(context,code) error_message(code) |
| 28 | #endif /* !HEIMDAL */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 29 | #endif |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 30 | #ifdef AFS |
| 31 | #include <kafs.h> |
Ben Lindstrom | b1985f7 | 2001-01-23 00:19:15 +0000 | [diff] [blame] | 32 | #include "radix.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | #include "ssh.h" |
| 36 | #include "ssh1.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 37 | #include "xmalloc.h" |
| 38 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 39 | #include "buffer.h" |
| 40 | #include "packet.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 41 | #include "mpaux.h" |
| 42 | #include "uidswap.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 43 | #include "log.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "readconf.h" |
| 45 | #include "key.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 46 | #include "authfd.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 47 | #include "sshconnect.h" |
| 48 | #include "authfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 49 | #include "readpass.h" |
| 50 | #include "cipher.h" |
| 51 | #include "canohost.h" |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 52 | #include "auth.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 53 | |
| 54 | /* Session id for the current session. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 55 | u_char session_id[16]; |
| 56 | u_int supported_authentications = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 57 | |
| 58 | extern Options options; |
| 59 | extern char *__progname; |
| 60 | |
| 61 | /* |
| 62 | * Checks if the user has an authentication agent, and if so, tries to |
| 63 | * authenticate using the agent. |
| 64 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 65 | static int |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 66 | try_agent_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 67 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 68 | int type; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 69 | char *comment; |
| 70 | AuthenticationConnection *auth; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 71 | u_char response[16]; |
| 72 | u_int i; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 73 | Key *key; |
| 74 | BIGNUM *challenge; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 75 | |
| 76 | /* Get connection to the agent. */ |
| 77 | auth = ssh_get_authentication_connection(); |
| 78 | if (!auth) |
| 79 | return 0; |
| 80 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 81 | if ((challenge = BN_new()) == NULL) |
| 82 | fatal("try_agent_authentication: BN_new failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 83 | /* Loop through identities served by the agent. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 84 | for (key = ssh_get_first_identity(auth, &comment, 1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 85 | key != NULL; |
| 86 | key = ssh_get_next_identity(auth, &comment, 1)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 87 | |
| 88 | /* Try this identity. */ |
| 89 | debug("Trying RSA authentication via agent with '%.100s'", comment); |
| 90 | xfree(comment); |
| 91 | |
| 92 | /* Tell the server that we are willing to authenticate using this key. */ |
| 93 | packet_start(SSH_CMSG_AUTH_RSA); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 94 | packet_put_bignum(key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 95 | packet_send(); |
| 96 | packet_write_wait(); |
| 97 | |
| 98 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 99 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 100 | |
| 101 | /* The server sends failure if it doesn\'t like our key or |
| 102 | does not support RSA authentication. */ |
| 103 | if (type == SSH_SMSG_FAILURE) { |
| 104 | debug("Server refused our key."); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 105 | key_free(key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 106 | continue; |
| 107 | } |
| 108 | /* Otherwise it should have sent a challenge. */ |
| 109 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 110 | packet_disconnect("Protocol error during RSA authentication: %d", |
| 111 | type); |
| 112 | |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 113 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 114 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 115 | |
| 116 | debug("Received RSA challenge from server."); |
| 117 | |
| 118 | /* Ask the agent to decrypt the challenge. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 119 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { |
| 120 | /* |
| 121 | * The agent failed to authenticate this identifier |
| 122 | * although it advertised it supports this. Just |
| 123 | * return a wrong value. |
| 124 | */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 125 | log("Authentication agent failed to decrypt challenge."); |
| 126 | memset(response, 0, sizeof(response)); |
| 127 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 128 | key_free(key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 129 | debug("Sending response to RSA challenge."); |
| 130 | |
| 131 | /* Send the decrypted challenge back to the server. */ |
| 132 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 133 | for (i = 0; i < 16; i++) |
| 134 | packet_put_char(response[i]); |
| 135 | packet_send(); |
| 136 | packet_write_wait(); |
| 137 | |
| 138 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 139 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 140 | |
| 141 | /* The server returns success if it accepted the authentication. */ |
| 142 | if (type == SSH_SMSG_SUCCESS) { |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 143 | ssh_close_authentication_connection(auth); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 144 | BN_clear_free(challenge); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 145 | debug("RSA authentication accepted by server."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 146 | return 1; |
| 147 | } |
| 148 | /* Otherwise it should return failure. */ |
| 149 | if (type != SSH_SMSG_FAILURE) |
| 150 | packet_disconnect("Protocol error waiting RSA auth response: %d", |
| 151 | type); |
| 152 | } |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 153 | ssh_close_authentication_connection(auth); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 154 | BN_clear_free(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 155 | debug("RSA authentication using agent refused."); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Computes the proper response to a RSA challenge, and sends the response to |
| 161 | * the server. |
| 162 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 163 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 164 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
| 165 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 166 | u_char buf[32], response[16]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 167 | MD5_CTX md; |
| 168 | int i, len; |
| 169 | |
| 170 | /* Decrypt the challenge using the private key. */ |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 171 | /* XXX think about Bleichenbacher, too */ |
| 172 | if (rsa_private_decrypt(challenge, challenge, prv) <= 0) |
| 173 | packet_disconnect( |
| 174 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 175 | |
| 176 | /* Compute the response. */ |
| 177 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 178 | len = BN_num_bytes(challenge); |
| 179 | if (len <= 0 || len > sizeof(buf)) |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 180 | packet_disconnect( |
| 181 | "respond_to_rsa_challenge: bad challenge length %d", len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 182 | |
| 183 | memset(buf, 0, sizeof(buf)); |
| 184 | BN_bn2bin(challenge, buf + sizeof(buf) - len); |
| 185 | MD5_Init(&md); |
| 186 | MD5_Update(&md, buf, 32); |
| 187 | MD5_Update(&md, session_id, 16); |
| 188 | MD5_Final(response, &md); |
| 189 | |
| 190 | debug("Sending response to host key RSA challenge."); |
| 191 | |
| 192 | /* Send the response back to the server. */ |
| 193 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 194 | for (i = 0; i < 16; i++) |
| 195 | packet_put_char(response[i]); |
| 196 | packet_send(); |
| 197 | packet_write_wait(); |
| 198 | |
| 199 | memset(buf, 0, sizeof(buf)); |
| 200 | memset(response, 0, sizeof(response)); |
| 201 | memset(&md, 0, sizeof(md)); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Checks if the user has authentication file, and if so, tries to authenticate |
| 206 | * the user using it. |
| 207 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 208 | static int |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 209 | try_rsa_authentication(int idx) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 210 | { |
| 211 | BIGNUM *challenge; |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 212 | Key *public, *private; |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 213 | char buf[300], *passphrase, *comment, *authfile; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 214 | int i, type, quit; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 215 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 216 | public = options.identity_keys[idx]; |
| 217 | authfile = options.identity_files[idx]; |
| 218 | comment = xstrdup(authfile); |
| 219 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 220 | debug("Trying RSA authentication with key '%.100s'", comment); |
| 221 | |
| 222 | /* Tell the server that we are willing to authenticate using this key. */ |
| 223 | packet_start(SSH_CMSG_AUTH_RSA); |
| 224 | packet_put_bignum(public->rsa->n); |
| 225 | packet_send(); |
| 226 | packet_write_wait(); |
| 227 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 228 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 229 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * The server responds with failure if it doesn\'t like our key or |
| 233 | * doesn\'t support RSA authentication. |
| 234 | */ |
| 235 | if (type == SSH_SMSG_FAILURE) { |
| 236 | debug("Server refused our key."); |
| 237 | xfree(comment); |
| 238 | return 0; |
| 239 | } |
| 240 | /* Otherwise, the server should respond with a challenge. */ |
| 241 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 242 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
| 243 | |
| 244 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 245 | if ((challenge = BN_new()) == NULL) |
| 246 | fatal("try_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 247 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 248 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 249 | |
| 250 | debug("Received RSA challenge from server."); |
| 251 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 252 | /* |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 253 | * If the key is not stored in external hardware, we have to |
| 254 | * load the private key. Try first with empty passphrase; if it |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 255 | * fails, ask for a passphrase. |
| 256 | */ |
Ben Lindstrom | e143f61 | 2002-08-20 18:41:15 +0000 | [diff] [blame] | 257 | if (public->flags & KEY_FLAG_EXT) |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 258 | private = public; |
| 259 | else |
| 260 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 261 | if (private == NULL && !options.batch_mode) { |
| 262 | snprintf(buf, sizeof(buf), |
| 263 | "Enter passphrase for RSA key '%.100s': ", comment); |
| 264 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 265 | passphrase = read_passphrase(buf, 0); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 266 | if (strcmp(passphrase, "") != 0) { |
| 267 | private = key_load_private_type(KEY_RSA1, |
| 268 | authfile, passphrase, NULL); |
| 269 | quit = 0; |
| 270 | } else { |
| 271 | debug2("no passphrase given, try next key"); |
| 272 | quit = 1; |
| 273 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 274 | memset(passphrase, 0, strlen(passphrase)); |
| 275 | xfree(passphrase); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 276 | if (private != NULL || quit) |
| 277 | break; |
| 278 | debug2("bad passphrase given, try again..."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 279 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 280 | } |
| 281 | /* We no longer need the comment. */ |
| 282 | xfree(comment); |
| 283 | |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 284 | if (private == NULL) { |
| 285 | if (!options.batch_mode) |
| 286 | error("Bad passphrase."); |
| 287 | |
| 288 | /* Send a dummy response packet to avoid protocol error. */ |
| 289 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 290 | for (i = 0; i < 16; i++) |
| 291 | packet_put_char(0); |
| 292 | packet_send(); |
| 293 | packet_write_wait(); |
| 294 | |
| 295 | /* Expect the server to reject it... */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 296 | packet_read_expect(SSH_SMSG_FAILURE); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 297 | BN_clear_free(challenge); |
| 298 | return 0; |
| 299 | } |
| 300 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 301 | /* Compute and send a response to the challenge. */ |
| 302 | respond_to_rsa_challenge(challenge, private->rsa); |
| 303 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 304 | /* Destroy the private key unless it in external hardware. */ |
| 305 | if (!(private->flags & KEY_FLAG_EXT)) |
| 306 | key_free(private); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 307 | |
| 308 | /* We no longer need the challenge. */ |
| 309 | BN_clear_free(challenge); |
| 310 | |
| 311 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 312 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 313 | if (type == SSH_SMSG_SUCCESS) { |
| 314 | debug("RSA authentication accepted by server."); |
| 315 | return 1; |
| 316 | } |
| 317 | if (type != SSH_SMSG_FAILURE) |
| 318 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 319 | debug("RSA authentication refused."); |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv |
| 325 | * authentication and RSA host authentication. |
| 326 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 327 | static int |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 328 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 329 | { |
| 330 | int type; |
| 331 | BIGNUM *challenge; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 332 | |
| 333 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); |
| 334 | |
| 335 | /* Tell the server that we are willing to authenticate using this key. */ |
| 336 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 337 | packet_put_cstring(local_user); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 338 | packet_put_int(BN_num_bits(host_key->rsa->n)); |
| 339 | packet_put_bignum(host_key->rsa->e); |
| 340 | packet_put_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 341 | packet_send(); |
| 342 | packet_write_wait(); |
| 343 | |
| 344 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 345 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 346 | |
| 347 | /* The server responds with failure if it doesn't admit our |
| 348 | .rhosts authentication or doesn't know our host key. */ |
| 349 | if (type == SSH_SMSG_FAILURE) { |
| 350 | debug("Server refused our rhosts authentication or host key."); |
| 351 | return 0; |
| 352 | } |
| 353 | /* Otherwise, the server should respond with a challenge. */ |
| 354 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 355 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
| 356 | |
| 357 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 358 | if ((challenge = BN_new()) == NULL) |
| 359 | fatal("try_rhosts_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 360 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 361 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 362 | |
| 363 | debug("Received RSA challenge for host key from server."); |
| 364 | |
| 365 | /* Compute a response to the challenge. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 366 | respond_to_rsa_challenge(challenge, host_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 367 | |
| 368 | /* We no longer need the challenge. */ |
| 369 | BN_clear_free(challenge); |
| 370 | |
| 371 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 372 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 373 | if (type == SSH_SMSG_SUCCESS) { |
| 374 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); |
| 375 | return 1; |
| 376 | } |
| 377 | if (type != SSH_SMSG_FAILURE) |
| 378 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 379 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | #ifdef KRB4 |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 384 | static int |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 385 | try_krb4_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 386 | { |
| 387 | KTEXT_ST auth; /* Kerberos data */ |
| 388 | char *reply; |
| 389 | char inst[INST_SZ]; |
| 390 | char *realm; |
| 391 | CREDENTIALS cred; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 392 | int r, type; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 393 | socklen_t slen; |
| 394 | Key_schedule schedule; |
| 395 | u_long checksum, cksum; |
| 396 | MSG_DAT msg_data; |
| 397 | struct sockaddr_in local, foreign; |
| 398 | struct stat st; |
| 399 | |
| 400 | /* Don't do anything if we don't have any tickets. */ |
| 401 | if (stat(tkt_string(), &st) < 0) |
| 402 | return 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 403 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 404 | strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)), |
| 405 | INST_SZ); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 406 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 407 | realm = (char *)krb_realmofhost(get_canonical_hostname(1)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 408 | if (!realm) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 409 | debug("Kerberos v4: no realm for %s", get_canonical_hostname(1)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | /* This can really be anything. */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 413 | checksum = (u_long)getpid(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 414 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 415 | r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum); |
| 416 | if (r != KSUCCESS) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 417 | debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | /* Get session key to decrypt the server's reply with. */ |
| 421 | r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred); |
| 422 | if (r != KSUCCESS) { |
| 423 | debug("get_cred failed: %s", krb_err_txt[r]); |
| 424 | return 0; |
| 425 | } |
| 426 | des_key_sched((des_cblock *) cred.session, schedule); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 427 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 428 | /* Send authentication info to server. */ |
| 429 | packet_start(SSH_CMSG_AUTH_KERBEROS); |
| 430 | packet_put_string((char *) auth.dat, auth.length); |
| 431 | packet_send(); |
| 432 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 433 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 434 | /* Zero the buffer. */ |
| 435 | (void) memset(auth.dat, 0, MAX_KTXT_LEN); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 436 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 437 | slen = sizeof(local); |
| 438 | memset(&local, 0, sizeof(local)); |
| 439 | if (getsockname(packet_get_connection_in(), |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 440 | (struct sockaddr *)&local, &slen) < 0) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 441 | debug("getsockname failed: %s", strerror(errno)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 442 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 443 | slen = sizeof(foreign); |
| 444 | memset(&foreign, 0, sizeof(foreign)); |
| 445 | if (getpeername(packet_get_connection_in(), |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 446 | (struct sockaddr *)&foreign, &slen) < 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 447 | debug("getpeername failed: %s", strerror(errno)); |
| 448 | fatal_cleanup(); |
| 449 | } |
| 450 | /* Get server reply. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 451 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 452 | switch (type) { |
| 453 | case SSH_SMSG_FAILURE: |
| 454 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 455 | debug("Kerberos v4 authentication failed."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 456 | return 0; |
| 457 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 458 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 459 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
| 460 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 461 | debug("Kerberos v4 authentication accepted."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 462 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 463 | /* Get server's response. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 464 | reply = packet_get_string((u_int *) &auth.length); |
Ben Lindstrom | 5c15958 | 2002-03-22 01:08:07 +0000 | [diff] [blame] | 465 | if (auth.length >= MAX_KTXT_LEN) |
| 466 | fatal("Kerberos v4: Malformed response from server"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 467 | memcpy(auth.dat, reply, auth.length); |
| 468 | xfree(reply); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 469 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 470 | packet_check_eom(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 471 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 472 | /* |
| 473 | * If his response isn't properly encrypted with the session |
| 474 | * key, and the decrypted checksum fails to match, he's |
| 475 | * bogus. Bail out. |
| 476 | */ |
| 477 | r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session, |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 478 | &foreign, &local, &msg_data); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 479 | if (r != KSUCCESS) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 480 | debug("Kerberos v4 krb_rd_priv failed: %s", |
| 481 | krb_err_txt[r]); |
| 482 | packet_disconnect("Kerberos v4 challenge failed!"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 483 | } |
| 484 | /* Fetch the (incremented) checksum that we supplied in the request. */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 485 | memcpy((char *)&cksum, (char *)msg_data.app_data, |
| 486 | sizeof(cksum)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 487 | cksum = ntohl(cksum); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 488 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 489 | /* If it matches, we're golden. */ |
| 490 | if (cksum == checksum + 1) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 491 | debug("Kerberos v4 challenge successful."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 492 | return 1; |
| 493 | } else |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 494 | packet_disconnect("Kerberos v4 challenge failed!"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 495 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 496 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 497 | default: |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 498 | packet_disconnect("Protocol error on Kerberos v4 response: %d", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 499 | } |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | #endif /* KRB4 */ |
| 504 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 505 | #ifdef KRB5 |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 506 | static int |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 507 | try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context) |
| 508 | { |
| 509 | krb5_error_code problem; |
| 510 | const char *tkfile; |
| 511 | struct stat buf; |
| 512 | krb5_ccache ccache = NULL; |
| 513 | const char *remotehost; |
| 514 | krb5_data ap; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 515 | int type; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 516 | krb5_ap_rep_enc_part *reply = NULL; |
| 517 | int ret; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 518 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 519 | memset(&ap, 0, sizeof(ap)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 520 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 521 | problem = krb5_init_context(context); |
| 522 | if (problem) { |
| 523 | debug("Kerberos v5: krb5_init_context failed"); |
| 524 | ret = 0; |
| 525 | goto out; |
| 526 | } |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 527 | |
| 528 | problem = krb5_auth_con_init(*context, auth_context); |
| 529 | if (problem) { |
| 530 | debug("Kerberos v5: krb5_auth_con_init failed"); |
| 531 | ret = 0; |
| 532 | goto out; |
| 533 | } |
| 534 | |
| 535 | #ifndef HEIMDAL |
| 536 | problem = krb5_auth_con_setflags(*context, *auth_context, |
| 537 | KRB5_AUTH_CONTEXT_RET_TIME); |
| 538 | if (problem) { |
| 539 | debug("Keberos v5: krb5_auth_con_setflags failed"); |
| 540 | ret = 0; |
| 541 | goto out; |
| 542 | } |
| 543 | #endif |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 544 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 545 | tkfile = krb5_cc_default_name(*context); |
| 546 | if (strncmp(tkfile, "FILE:", 5) == 0) |
| 547 | tkfile += 5; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 548 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 549 | if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) { |
| 550 | debug("Kerberos v5: could not get default ccache (permission denied)."); |
| 551 | ret = 0; |
| 552 | goto out; |
| 553 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 554 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 555 | problem = krb5_cc_default(*context, &ccache); |
| 556 | if (problem) { |
| 557 | debug("Kerberos v5: krb5_cc_default failed: %s", |
| 558 | krb5_get_err_text(*context, problem)); |
| 559 | ret = 0; |
| 560 | goto out; |
| 561 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 562 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 563 | remotehost = get_canonical_hostname(1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 564 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 565 | problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED, |
| 566 | "host", remotehost, NULL, ccache, &ap); |
| 567 | if (problem) { |
| 568 | debug("Kerberos v5: krb5_mk_req failed: %s", |
| 569 | krb5_get_err_text(*context, problem)); |
| 570 | ret = 0; |
| 571 | goto out; |
| 572 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 573 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 574 | packet_start(SSH_CMSG_AUTH_KERBEROS); |
| 575 | packet_put_string((char *) ap.data, ap.length); |
| 576 | packet_send(); |
| 577 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 578 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 579 | xfree(ap.data); |
| 580 | ap.length = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 581 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 582 | type = packet_read(); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 583 | switch (type) { |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 584 | case SSH_SMSG_FAILURE: |
| 585 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ |
| 586 | debug("Kerberos v5 authentication failed."); |
| 587 | ret = 0; |
| 588 | break; |
| 589 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 590 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 591 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ |
| 592 | debug("Kerberos v5 authentication accepted."); |
| 593 | |
| 594 | /* Get server's response. */ |
| 595 | ap.data = packet_get_string((unsigned int *) &ap.length); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 596 | packet_check_eom(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 597 | /* XXX je to dobre? */ |
| 598 | |
| 599 | problem = krb5_rd_rep(*context, *auth_context, &ap, &reply); |
| 600 | if (problem) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 601 | ret = 0; |
| 602 | } |
| 603 | ret = 1; |
| 604 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 605 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 606 | default: |
| 607 | packet_disconnect("Protocol error on Kerberos v5 response: %d", |
| 608 | type); |
| 609 | ret = 0; |
| 610 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 611 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 612 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 613 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 614 | out: |
| 615 | if (ccache != NULL) |
| 616 | krb5_cc_close(*context, ccache); |
| 617 | if (reply != NULL) |
| 618 | krb5_free_ap_rep_enc_part(*context, reply); |
| 619 | if (ap.length > 0) |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 620 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 621 | krb5_data_free(&ap); |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 622 | #else |
| 623 | krb5_free_data_contents(*context, &ap); |
| 624 | #endif |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 625 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 626 | return (ret); |
| 627 | } |
| 628 | |
| 629 | static void |
| 630 | send_krb5_tgt(krb5_context context, krb5_auth_context auth_context) |
| 631 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 632 | int fd, type; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 633 | krb5_error_code problem; |
| 634 | krb5_data outbuf; |
| 635 | krb5_ccache ccache = NULL; |
| 636 | krb5_creds creds; |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 637 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 638 | krb5_kdc_flags flags; |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 639 | #else |
| 640 | int forwardable; |
| 641 | #endif |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 642 | const char *remotehost; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 643 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 644 | memset(&creds, 0, sizeof(creds)); |
| 645 | memset(&outbuf, 0, sizeof(outbuf)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 646 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 647 | fd = packet_get_connection_in(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 648 | |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 649 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 650 | problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd); |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 651 | #else |
| 652 | problem = krb5_auth_con_genaddrs(context, auth_context, fd, |
| 653 | KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR | |
| 654 | KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR); |
| 655 | #endif |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 656 | if (problem) |
| 657 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 658 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 659 | problem = krb5_cc_default(context, &ccache); |
| 660 | if (problem) |
| 661 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 662 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 663 | problem = krb5_cc_get_principal(context, ccache, &creds.client); |
| 664 | if (problem) |
| 665 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 666 | |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 667 | remotehost = get_canonical_hostname(1); |
| 668 | |
| 669 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 670 | problem = krb5_build_principal(context, &creds.server, |
| 671 | strlen(creds.client->realm), creds.client->realm, |
| 672 | "krbtgt", creds.client->realm, NULL); |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 673 | #else |
| 674 | problem = krb5_build_principal(context, &creds.server, |
| 675 | creds.client->realm.length, creds.client->realm.data, |
| 676 | "host", remotehost, NULL); |
| 677 | #endif |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 678 | if (problem) |
| 679 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 680 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 681 | creds.times.endtime = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 682 | |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 683 | #ifdef HEIMDAL |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 684 | flags.i = 0; |
| 685 | flags.b.forwarded = 1; |
| 686 | flags.b.forwardable = krb5_config_get_bool(context, NULL, |
| 687 | "libdefaults", "forwardable", NULL); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 688 | problem = krb5_get_forwarded_creds(context, auth_context, |
| 689 | ccache, flags.i, remotehost, &creds, &outbuf); |
Damien Miller | fd4c9ee | 2002-04-13 11:04:40 +1000 | [diff] [blame] | 690 | #else |
| 691 | forwardable = 1; |
| 692 | problem = krb5_fwd_tgt_creds(context, auth_context, remotehost, |
| 693 | creds.client, creds.server, ccache, forwardable, &outbuf); |
| 694 | #endif |
| 695 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 696 | if (problem) |
| 697 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 698 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 699 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
| 700 | packet_put_string((char *)outbuf.data, outbuf.length); |
| 701 | packet_send(); |
| 702 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 703 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 704 | type = packet_read(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 705 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 706 | if (type == SSH_SMSG_SUCCESS) { |
| 707 | char *pname; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 708 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 709 | krb5_unparse_name(context, creds.client, &pname); |
| 710 | debug("Kerberos v5 TGT forwarded (%s).", pname); |
| 711 | xfree(pname); |
| 712 | } else |
| 713 | debug("Kerberos v5 TGT forwarding failed."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 714 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 715 | return; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 716 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 717 | out: |
| 718 | if (problem) |
| 719 | debug("Kerberos v5 TGT forwarding failed: %s", |
| 720 | krb5_get_err_text(context, problem)); |
| 721 | if (creds.client) |
| 722 | krb5_free_principal(context, creds.client); |
| 723 | if (creds.server) |
| 724 | krb5_free_principal(context, creds.server); |
| 725 | if (ccache) |
| 726 | krb5_cc_close(context, ccache); |
| 727 | if (outbuf.data) |
| 728 | xfree(outbuf.data); |
| 729 | } |
| 730 | #endif /* KRB5 */ |
| 731 | |
| 732 | #ifdef AFS |
| 733 | static void |
| 734 | send_krb4_tgt(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 735 | { |
| 736 | CREDENTIALS *creds; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 737 | struct stat st; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 738 | char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ]; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 739 | int problem, type; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 740 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 741 | /* Don't do anything if we don't have any tickets. */ |
| 742 | if (stat(tkt_string(), &st) < 0) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 743 | return; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 744 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 745 | creds = xmalloc(sizeof(*creds)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 746 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 747 | problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm); |
| 748 | if (problem) |
| 749 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 750 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 751 | problem = krb_get_cred("krbtgt", prealm, prealm, creds); |
| 752 | if (problem) |
| 753 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 754 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 755 | if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 756 | problem = RD_AP_EXP; |
| 757 | goto out; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 758 | } |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 759 | creds_to_radix(creds, (u_char *)buffer, sizeof(buffer)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 760 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 761 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 762 | packet_put_cstring(buffer); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 763 | packet_send(); |
| 764 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 765 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 766 | type = packet_read(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 767 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 768 | if (type == SSH_SMSG_SUCCESS) |
| 769 | debug("Kerberos v4 TGT forwarded (%s%s%s@%s).", |
| 770 | creds->pname, creds->pinst[0] ? "." : "", |
| 771 | creds->pinst, creds->realm); |
| 772 | else |
| 773 | debug("Kerberos v4 TGT rejected."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 774 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 775 | xfree(creds); |
| 776 | return; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 777 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 778 | out: |
| 779 | debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]); |
| 780 | xfree(creds); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 781 | } |
| 782 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 783 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 784 | send_afs_tokens(void) |
| 785 | { |
| 786 | CREDENTIALS creds; |
| 787 | struct ViceIoctl parms; |
| 788 | struct ClearToken ct; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 789 | int i, type, len; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 790 | char buf[2048], *p, *server_cell; |
| 791 | char buffer[8192]; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 792 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 793 | /* Move over ktc_GetToken, here's something leaner. */ |
| 794 | for (i = 0; i < 100; i++) { /* just in case */ |
| 795 | parms.in = (char *) &i; |
| 796 | parms.in_size = sizeof(i); |
| 797 | parms.out = buf; |
| 798 | parms.out_size = sizeof(buf); |
| 799 | if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0) |
| 800 | break; |
| 801 | p = buf; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 802 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 803 | /* Get secret token. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 804 | memcpy(&creds.ticket_st.length, p, sizeof(u_int)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 805 | if (creds.ticket_st.length > MAX_KTXT_LEN) |
| 806 | break; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 807 | p += sizeof(u_int); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 808 | memcpy(creds.ticket_st.dat, p, creds.ticket_st.length); |
| 809 | p += creds.ticket_st.length; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 810 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 811 | /* Get clear token. */ |
| 812 | memcpy(&len, p, sizeof(len)); |
| 813 | if (len != sizeof(struct ClearToken)) |
| 814 | break; |
| 815 | p += sizeof(len); |
| 816 | memcpy(&ct, p, len); |
| 817 | p += len; |
| 818 | p += sizeof(len); /* primary flag */ |
| 819 | server_cell = p; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 820 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 821 | /* Flesh out our credentials. */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 822 | strlcpy(creds.service, "afs", sizeof(creds.service)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 823 | creds.instance[0] = '\0'; |
| 824 | strlcpy(creds.realm, server_cell, REALM_SZ); |
| 825 | memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ); |
| 826 | creds.issue_date = ct.BeginTimestamp; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 827 | creds.lifetime = krb_time_to_life(creds.issue_date, |
| 828 | ct.EndTimestamp); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 829 | creds.kvno = ct.AuthHandle; |
| 830 | snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId); |
| 831 | creds.pinst[0] = '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 832 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 833 | /* Encode token, ship it off. */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 834 | if (creds_to_radix(&creds, (u_char *)buffer, |
| 835 | sizeof(buffer)) <= 0) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 836 | break; |
| 837 | packet_start(SSH_CMSG_HAVE_AFS_TOKEN); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 838 | packet_put_cstring(buffer); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 839 | packet_send(); |
| 840 | packet_write_wait(); |
| 841 | |
| 842 | /* Roger, Roger. Clearance, Clarence. What's your vector, |
| 843 | Victor? */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 844 | type = packet_read(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 845 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 846 | if (type == SSH_SMSG_FAILURE) |
| 847 | debug("AFS token for cell %s rejected.", server_cell); |
| 848 | else if (type != SSH_SMSG_SUCCESS) |
| 849 | packet_disconnect("Protocol error on AFS token response: %d", type); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | #endif /* AFS */ |
| 854 | |
| 855 | /* |
| 856 | * Tries to authenticate with any string-based challenge/response system. |
| 857 | * Note that the client code is not tied to s/key or TIS. |
| 858 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 859 | static int |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 860 | try_challenge_response_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 861 | { |
| 862 | int type, i; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 863 | u_int clen; |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 864 | char prompt[1024]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 865 | char *challenge, *response; |
Ben Lindstrom | bdfb4df | 2001-10-03 17:12:43 +0000 | [diff] [blame] | 866 | |
| 867 | debug("Doing challenge response authentication."); |
| 868 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 869 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 870 | /* request a challenge */ |
| 871 | packet_start(SSH_CMSG_AUTH_TIS); |
| 872 | packet_send(); |
| 873 | packet_write_wait(); |
| 874 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 875 | type = packet_read(); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 876 | if (type != SSH_SMSG_FAILURE && |
| 877 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
| 878 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 879 | "to SSH_CMSG_AUTH_TIS", type); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 880 | } |
| 881 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 882 | debug("No challenge."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 883 | return 0; |
| 884 | } |
| 885 | challenge = packet_get_string(&clen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 886 | packet_check_eom(); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 887 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 888 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 889 | xfree(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 890 | if (i != 0) |
| 891 | error("Permission denied, please try again."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 892 | if (options.cipher == SSH_CIPHER_NONE) |
| 893 | log("WARNING: Encryption is disabled! " |
Damien Miller | f61c015 | 2002-04-23 20:56:02 +1000 | [diff] [blame] | 894 | "Response will be transmitted in clear text."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 895 | response = read_passphrase(prompt, 0); |
| 896 | if (strcmp(response, "") == 0) { |
| 897 | xfree(response); |
| 898 | break; |
| 899 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 900 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 901 | ssh_put_password(response); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 902 | memset(response, 0, strlen(response)); |
| 903 | xfree(response); |
| 904 | packet_send(); |
| 905 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 906 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 907 | if (type == SSH_SMSG_SUCCESS) |
| 908 | return 1; |
| 909 | if (type != SSH_SMSG_FAILURE) |
| 910 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 911 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 912 | } |
| 913 | /* failure */ |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Tries to authenticate with plain passwd authentication. |
| 919 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 920 | static int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 921 | try_password_authentication(char *prompt) |
| 922 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 923 | int type, i; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 924 | char *password; |
| 925 | |
| 926 | debug("Doing password authentication."); |
| 927 | if (options.cipher == SSH_CIPHER_NONE) |
| 928 | log("WARNING: Encryption is disabled! Password will be transmitted in clear text."); |
| 929 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 930 | if (i != 0) |
| 931 | error("Permission denied, please try again."); |
| 932 | password = read_passphrase(prompt, 0); |
| 933 | packet_start(SSH_CMSG_AUTH_PASSWORD); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 934 | ssh_put_password(password); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 935 | memset(password, 0, strlen(password)); |
| 936 | xfree(password); |
| 937 | packet_send(); |
| 938 | packet_write_wait(); |
| 939 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 940 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 941 | if (type == SSH_SMSG_SUCCESS) |
| 942 | return 1; |
| 943 | if (type != SSH_SMSG_FAILURE) |
| 944 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); |
| 945 | } |
| 946 | /* failure */ |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * SSH1 key exchange |
| 952 | */ |
| 953 | void |
| 954 | ssh_kex(char *host, struct sockaddr *hostaddr) |
| 955 | { |
| 956 | int i; |
| 957 | BIGNUM *key; |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 958 | Key *host_key, *server_key; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 959 | int bits, rbits; |
| 960 | int ssh_cipher_default = SSH_CIPHER_3DES; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 961 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
| 962 | u_char cookie[8]; |
| 963 | u_int supported_ciphers; |
| 964 | u_int server_flags, client_flags; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 965 | u_int32_t rand = 0; |
| 966 | |
| 967 | debug("Waiting for server public key."); |
| 968 | |
| 969 | /* Wait for a public key packet from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 970 | packet_read_expect(SSH_SMSG_PUBLIC_KEY); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 971 | |
| 972 | /* Get cookie from the packet. */ |
| 973 | for (i = 0; i < 8; i++) |
| 974 | cookie[i] = packet_get_char(); |
| 975 | |
| 976 | /* Get the public key. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 977 | server_key = key_new(KEY_RSA1); |
| 978 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 979 | packet_get_bignum(server_key->rsa->e); |
| 980 | packet_get_bignum(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 981 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 982 | rbits = BN_num_bits(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 983 | if (bits != rbits) { |
| 984 | log("Warning: Server lies about size of server public key: " |
| 985 | "actual size is %d bits vs. announced %d.", rbits, bits); |
| 986 | log("Warning: This may be due to an old implementation of ssh."); |
| 987 | } |
| 988 | /* Get the host key. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 989 | host_key = key_new(KEY_RSA1); |
| 990 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 991 | packet_get_bignum(host_key->rsa->e); |
| 992 | packet_get_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 993 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 994 | rbits = BN_num_bits(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 995 | if (bits != rbits) { |
| 996 | log("Warning: Server lies about size of server host key: " |
| 997 | "actual size is %d bits vs. announced %d.", rbits, bits); |
| 998 | log("Warning: This may be due to an old implementation of ssh."); |
| 999 | } |
| 1000 | |
| 1001 | /* Get protocol flags. */ |
| 1002 | server_flags = packet_get_int(); |
| 1003 | packet_set_protocol_flags(server_flags); |
| 1004 | |
| 1005 | supported_ciphers = packet_get_int(); |
| 1006 | supported_authentications = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1007 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1008 | |
| 1009 | debug("Received server public key (%d bits) and host key (%d bits).", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1010 | 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] | 1011 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1012 | if (verify_host_key(host, hostaddr, host_key) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 1013 | fatal("Host key verification failed."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1014 | |
| 1015 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; |
| 1016 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1017 | 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] | 1018 | |
| 1019 | /* Generate a session key. */ |
| 1020 | arc4random_stir(); |
| 1021 | |
| 1022 | /* |
| 1023 | * Generate an encryption key for the session. The key is a 256 bit |
| 1024 | * random number, interpreted as a 32-byte key, with the least |
| 1025 | * significant 8 bits being the first byte of the key. |
| 1026 | */ |
| 1027 | for (i = 0; i < 32; i++) { |
| 1028 | if (i % 4 == 0) |
| 1029 | rand = arc4random(); |
| 1030 | session_key[i] = rand & 0xff; |
| 1031 | rand >>= 8; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * According to the protocol spec, the first byte of the session key |
| 1036 | * is the highest byte of the integer. The session key is xored with |
| 1037 | * the first 16 bytes of the session id. |
| 1038 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1039 | if ((key = BN_new()) == NULL) |
| 1040 | fatal("respond_to_rsa_challenge: BN_new failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1041 | BN_set_word(key, 0); |
| 1042 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { |
| 1043 | BN_lshift(key, key, 8); |
| 1044 | if (i < 16) |
| 1045 | BN_add_word(key, session_key[i] ^ session_id[i]); |
| 1046 | else |
| 1047 | BN_add_word(key, session_key[i]); |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Encrypt the integer using the public key and host key of the |
| 1052 | * server (key with smaller modulus first). |
| 1053 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1054 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1055 | /* Public key has smaller modulus. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1056 | if (BN_num_bits(host_key->rsa->n) < |
| 1057 | BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 1058 | fatal("respond_to_rsa_challenge: host_key %d < server_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1059 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1060 | BN_num_bits(host_key->rsa->n), |
| 1061 | BN_num_bits(server_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1062 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1063 | } |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1064 | rsa_public_encrypt(key, key, server_key->rsa); |
| 1065 | rsa_public_encrypt(key, key, host_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1066 | } else { |
| 1067 | /* Host key has smaller modulus (or they are equal). */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1068 | if (BN_num_bits(server_key->rsa->n) < |
| 1069 | BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 1070 | fatal("respond_to_rsa_challenge: server_key %d < host_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1071 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1072 | BN_num_bits(server_key->rsa->n), |
| 1073 | BN_num_bits(host_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1074 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1075 | } |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1076 | rsa_public_encrypt(key, key, host_key->rsa); |
| 1077 | rsa_public_encrypt(key, key, server_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | /* Destroy the public keys since we no longer need them. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1081 | key_free(server_key); |
| 1082 | key_free(host_key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1083 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 1084 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
| 1085 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) |
| 1086 | options.cipher = ssh_cipher_default; |
| 1087 | } else if (options.cipher == SSH_CIPHER_ILLEGAL || |
| 1088 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1089 | log("No valid SSH1 cipher, using %.100s instead.", |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1090 | cipher_name(ssh_cipher_default)); |
| 1091 | options.cipher = ssh_cipher_default; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1092 | } |
| 1093 | /* Check that the selected cipher is supported. */ |
| 1094 | if (!(supported_ciphers & (1 << options.cipher))) |
| 1095 | fatal("Selected cipher type %.100s not supported by server.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1096 | cipher_name(options.cipher)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1097 | |
| 1098 | debug("Encryption type: %.100s", cipher_name(options.cipher)); |
| 1099 | |
| 1100 | /* Send the encrypted session key to the server. */ |
| 1101 | packet_start(SSH_CMSG_SESSION_KEY); |
| 1102 | packet_put_char(options.cipher); |
| 1103 | |
| 1104 | /* Send the cookie back to the server. */ |
| 1105 | for (i = 0; i < 8; i++) |
| 1106 | packet_put_char(cookie[i]); |
| 1107 | |
| 1108 | /* Send and destroy the encrypted encryption key integer. */ |
| 1109 | packet_put_bignum(key); |
| 1110 | BN_clear_free(key); |
| 1111 | |
| 1112 | /* Send protocol flags. */ |
| 1113 | packet_put_int(client_flags); |
| 1114 | |
| 1115 | /* Send the packet now. */ |
| 1116 | packet_send(); |
| 1117 | packet_write_wait(); |
| 1118 | |
| 1119 | debug("Sent encrypted session key."); |
| 1120 | |
| 1121 | /* Set the encryption key. */ |
| 1122 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); |
| 1123 | |
| 1124 | /* We will no longer need the session key here. Destroy any extra copies. */ |
| 1125 | memset(session_key, 0, sizeof(session_key)); |
| 1126 | |
| 1127 | /* |
| 1128 | * Expect a success message from the server. Note that this message |
| 1129 | * will be received in encrypted form. |
| 1130 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1131 | packet_read_expect(SSH_SMSG_SUCCESS); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1132 | |
| 1133 | debug("Received encrypted confirmation."); |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * Authenticate user |
| 1138 | */ |
| 1139 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1140 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1141 | Sensitive *sensitive) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1142 | { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1143 | #ifdef KRB5 |
| 1144 | krb5_context context = NULL; |
| 1145 | krb5_auth_context auth_context = NULL; |
| 1146 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1147 | int i, type; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1148 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1149 | if (supported_authentications == 0) |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1150 | fatal("ssh_userauth1: server supports no auth methods"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1151 | |
| 1152 | /* Send the name of the user to log in as on the server. */ |
| 1153 | packet_start(SSH_CMSG_USER); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 1154 | packet_put_cstring(server_user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1155 | packet_send(); |
| 1156 | packet_write_wait(); |
| 1157 | |
| 1158 | /* |
| 1159 | * The server should respond with success if no authentication is |
| 1160 | * needed (the user has no password). Otherwise the server responds |
| 1161 | * with failure. |
| 1162 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1163 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1164 | |
| 1165 | /* check whether the connection was accepted without authentication. */ |
| 1166 | if (type == SSH_SMSG_SUCCESS) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1167 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1168 | if (type != SSH_SMSG_FAILURE) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1169 | 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] | 1170 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1171 | #ifdef KRB5 |
| 1172 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1173 | options.kerberos_authentication) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1174 | debug("Trying Kerberos v5 authentication."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1175 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1176 | if (try_krb5_authentication(&context, &auth_context)) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1177 | type = packet_read(); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1178 | if (type == SSH_SMSG_SUCCESS) |
| 1179 | goto success; |
| 1180 | if (type != SSH_SMSG_FAILURE) |
| 1181 | packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type); |
| 1182 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1183 | } |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1184 | #endif /* KRB5 */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1185 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1186 | #ifdef KRB4 |
| 1187 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && |
| 1188 | options.kerberos_authentication) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1189 | debug("Trying Kerberos v4 authentication."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1190 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1191 | if (try_krb4_authentication()) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1192 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1193 | if (type == SSH_SMSG_SUCCESS) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1194 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1195 | if (type != SSH_SMSG_FAILURE) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1196 | packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | #endif /* KRB4 */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1200 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1201 | /* |
| 1202 | * Use rhosts authentication if running in privileged socket and we |
| 1203 | * do not wish to remain anonymous. |
| 1204 | */ |
| 1205 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) && |
| 1206 | options.rhosts_authentication) { |
| 1207 | debug("Trying rhosts authentication."); |
| 1208 | packet_start(SSH_CMSG_AUTH_RHOSTS); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 1209 | packet_put_cstring(local_user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1210 | packet_send(); |
| 1211 | packet_write_wait(); |
| 1212 | |
| 1213 | /* The server should respond with success or failure. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1214 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1215 | if (type == SSH_SMSG_SUCCESS) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1216 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1217 | if (type != SSH_SMSG_FAILURE) |
| 1218 | packet_disconnect("Protocol error: got %d in response to rhosts auth", |
| 1219 | type); |
| 1220 | } |
| 1221 | /* |
| 1222 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host |
| 1223 | * authentication. |
| 1224 | */ |
| 1225 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1226 | options.rhosts_rsa_authentication) { |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1227 | for (i = 0; i < sensitive->nkeys; i++) { |
| 1228 | if (sensitive->keys[i] != NULL && |
| 1229 | sensitive->keys[i]->type == KEY_RSA1 && |
| 1230 | try_rhosts_rsa_authentication(local_user, |
| 1231 | sensitive->keys[i])) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1232 | goto success; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1233 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1234 | } |
| 1235 | /* Try RSA authentication if the server supports it. */ |
| 1236 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && |
| 1237 | options.rsa_authentication) { |
| 1238 | /* |
| 1239 | * Try RSA authentication using the authentication agent. The |
| 1240 | * agent is tried first because no passphrase is needed for |
| 1241 | * it, whereas identity files may require passphrases. |
| 1242 | */ |
| 1243 | if (try_agent_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1244 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1245 | |
| 1246 | /* Try RSA authentication for each identity. */ |
| 1247 | for (i = 0; i < options.num_identity_files; i++) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1248 | if (options.identity_keys[i] != NULL && |
| 1249 | options.identity_keys[i]->type == KEY_RSA1 && |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 1250 | try_rsa_authentication(i)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1251 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1252 | } |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 1253 | /* Try challenge response authentication if the server supports it. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1254 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1255 | options.challenge_response_authentication && !options.batch_mode) { |
| 1256 | if (try_challenge_response_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1257 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1258 | } |
| 1259 | /* Try password authentication if the server supports it. */ |
| 1260 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && |
| 1261 | options.password_authentication && !options.batch_mode) { |
| 1262 | char prompt[80]; |
| 1263 | |
Ben Lindstrom | e055716 | 2001-02-11 00:00:24 +0000 | [diff] [blame] | 1264 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1265 | server_user, host); |
| 1266 | if (try_password_authentication(prompt)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1267 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1268 | } |
| 1269 | /* All authentication methods have failed. Exit with an error message. */ |
| 1270 | fatal("Permission denied."); |
| 1271 | /* NOTREACHED */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1272 | |
| 1273 | success: |
| 1274 | #ifdef KRB5 |
| 1275 | /* Try Kerberos v5 TGT passing. */ |
| 1276 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && |
| 1277 | options.kerberos_tgt_passing && context && auth_context) { |
| 1278 | if (options.cipher == SSH_CIPHER_NONE) |
| 1279 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); |
| 1280 | send_krb5_tgt(context, auth_context); |
| 1281 | } |
| 1282 | if (auth_context) |
| 1283 | krb5_auth_con_free(context, auth_context); |
| 1284 | if (context) |
| 1285 | krb5_free_context(context); |
| 1286 | #endif |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1287 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1288 | #ifdef AFS |
| 1289 | /* Try Kerberos v4 TGT passing if the server supports it. */ |
| 1290 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && |
| 1291 | options.kerberos_tgt_passing) { |
| 1292 | if (options.cipher == SSH_CIPHER_NONE) |
| 1293 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); |
| 1294 | send_krb4_tgt(); |
| 1295 | } |
| 1296 | /* Try AFS token passing if the server supports it. */ |
| 1297 | if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) && |
| 1298 | options.afs_token_passing && k_hasafs()) { |
| 1299 | if (options.cipher == SSH_CIPHER_NONE) |
| 1300 | log("WARNING: Encryption is disabled! Token will be transmitted in the clear!"); |
| 1301 | send_afs_tokens(); |
| 1302 | } |
| 1303 | #endif /* AFS */ |
Tim Rice | 024acc4 | 2001-07-04 21:27:20 -0700 | [diff] [blame] | 1304 | |
Damien Miller | 40eb1d8 | 2001-07-14 12:16:59 +1000 | [diff] [blame] | 1305 | return; /* need statement after label */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1306 | } |