blob: a394411208df0b42edef9c7a04e965de210bb359 [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
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 Millereba71ba2000-04-29 23:57:08 +10005 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
7 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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 Millereba71ba2000-04-29 23:57:08 +100013 */
14
15#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +100016
17#include <openssl/bn.h>
Damien Miller2ce18da2002-02-13 13:54:27 +110018#include <openssl/md5.h>
Damien Millereba71ba2000-04-29 23:57:08 +100019
Ben Lindstrom226cfa02001-01-22 05:34:40 +000020#include "ssh.h"
21#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100022#include "xmalloc.h"
23#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100024#include "buffer.h"
25#include "packet.h"
Darren Tuckere14e0052004-05-13 16:30:44 +100026#include "kex.h"
Damien Millereba71ba2000-04-29 23:57:08 +100027#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100029#include "readconf.h"
30#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100031#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100032#include "sshconnect.h"
33#include "authfile.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100034#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "cipher.h"
36#include "canohost.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000037#include "auth.h"
Damien Millereba71ba2000-04-29 23:57:08 +100038
39/* Session id for the current session. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000040u_char session_id[16];
41u_int supported_authentications = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100042
43extern Options options;
44extern char *__progname;
45
46/*
47 * Checks if the user has an authentication agent, and if so, tries to
48 * authenticate using the agent.
49 */
Ben Lindstrombba81212001-06-25 05:01:22 +000050static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000051try_agent_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +100052{
Damien Millerad833b32000-08-23 10:46:23 +100053 int type;
Damien Millereba71ba2000-04-29 23:57:08 +100054 char *comment;
55 AuthenticationConnection *auth;
Ben Lindstrom46c16222000-12-22 01:43:59 +000056 u_char response[16];
57 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +100058 Key *key;
59 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +100060
61 /* Get connection to the agent. */
62 auth = ssh_get_authentication_connection();
63 if (!auth)
64 return 0;
65
Damien Millerda755162002-01-22 23:09:22 +110066 if ((challenge = BN_new()) == NULL)
67 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100068 /* Loop through identities served by the agent. */
Damien Millerad833b32000-08-23 10:46:23 +100069 for (key = ssh_get_first_identity(auth, &comment, 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +110070 key != NULL;
71 key = ssh_get_next_identity(auth, &comment, 1)) {
Damien Millereba71ba2000-04-29 23:57:08 +100072
73 /* Try this identity. */
74 debug("Trying RSA authentication via agent with '%.100s'", comment);
75 xfree(comment);
76
77 /* Tell the server that we are willing to authenticate using this key. */
78 packet_start(SSH_CMSG_AUTH_RSA);
Damien Millerad833b32000-08-23 10:46:23 +100079 packet_put_bignum(key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +100080 packet_send();
81 packet_write_wait();
82
83 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +110084 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +100085
Damien Miller788f2122005-11-05 15:14:59 +110086 /* The server sends failure if it doesn't like our key or
Damien Millereba71ba2000-04-29 23:57:08 +100087 does not support RSA authentication. */
88 if (type == SSH_SMSG_FAILURE) {
89 debug("Server refused our key.");
Damien Millerad833b32000-08-23 10:46:23 +100090 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +100091 continue;
92 }
93 /* Otherwise it should have sent a challenge. */
94 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
95 packet_disconnect("Protocol error during RSA authentication: %d",
96 type);
97
Damien Millerd432ccf2002-01-22 23:14:44 +110098 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +110099 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000100
101 debug("Received RSA challenge from server.");
102
103 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000104 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
105 /*
106 * The agent failed to authenticate this identifier
107 * although it advertised it supports this. Just
108 * return a wrong value.
109 */
Damien Miller996acd22003-04-09 20:59:48 +1000110 logit("Authentication agent failed to decrypt challenge.");
Damien Millereba71ba2000-04-29 23:57:08 +1000111 memset(response, 0, sizeof(response));
112 }
Damien Millerad833b32000-08-23 10:46:23 +1000113 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000114 debug("Sending response to RSA challenge.");
115
116 /* Send the decrypted challenge back to the server. */
117 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
118 for (i = 0; i < 16; i++)
119 packet_put_char(response[i]);
120 packet_send();
121 packet_write_wait();
122
123 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100124 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000125
126 /* The server returns success if it accepted the authentication. */
127 if (type == SSH_SMSG_SUCCESS) {
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000128 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000129 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000130 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000131 return 1;
132 }
133 /* Otherwise it should return failure. */
134 if (type != SSH_SMSG_FAILURE)
135 packet_disconnect("Protocol error waiting RSA auth response: %d",
136 type);
137 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000138 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000139 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000140 debug("RSA authentication using agent refused.");
141 return 0;
142}
143
144/*
145 * Computes the proper response to a RSA challenge, and sends the response to
146 * the server.
147 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static void
Damien Millereba71ba2000-04-29 23:57:08 +1000149respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
150{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000151 u_char buf[32], response[16];
Damien Millereba71ba2000-04-29 23:57:08 +1000152 MD5_CTX md;
153 int i, len;
154
155 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100156 /* XXX think about Bleichenbacher, too */
157 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
158 packet_disconnect(
159 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000160
161 /* Compute the response. */
162 /* The response is MD5 of decrypted challenge plus session id. */
163 len = BN_num_bytes(challenge);
Damien Millereccb9de2005-06-17 12:59:34 +1000164 if (len <= 0 || (u_int)len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100165 packet_disconnect(
166 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000167
168 memset(buf, 0, sizeof(buf));
169 BN_bn2bin(challenge, buf + sizeof(buf) - len);
170 MD5_Init(&md);
171 MD5_Update(&md, buf, 32);
172 MD5_Update(&md, session_id, 16);
173 MD5_Final(response, &md);
174
175 debug("Sending response to host key RSA challenge.");
176
177 /* Send the response back to the server. */
178 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
179 for (i = 0; i < 16; i++)
180 packet_put_char(response[i]);
181 packet_send();
182 packet_write_wait();
183
184 memset(buf, 0, sizeof(buf));
185 memset(response, 0, sizeof(response));
186 memset(&md, 0, sizeof(md));
187}
188
189/*
190 * Checks if the user has authentication file, and if so, tries to authenticate
191 * the user using it.
192 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000193static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000194try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000195{
196 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000197 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000198 char buf[300], *passphrase, *comment, *authfile;
Damien Millerdff50992002-01-22 23:16:32 +1100199 int i, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000200
Ben Lindstromc5b68002001-07-04 04:52:03 +0000201 public = options.identity_keys[idx];
202 authfile = options.identity_files[idx];
203 comment = xstrdup(authfile);
204
Damien Millereba71ba2000-04-29 23:57:08 +1000205 debug("Trying RSA authentication with key '%.100s'", comment);
206
207 /* Tell the server that we are willing to authenticate using this key. */
208 packet_start(SSH_CMSG_AUTH_RSA);
209 packet_put_bignum(public->rsa->n);
210 packet_send();
211 packet_write_wait();
212
Damien Millereba71ba2000-04-29 23:57:08 +1000213 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100214 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000215
216 /*
Damien Miller788f2122005-11-05 15:14:59 +1100217 * The server responds with failure if it doesn't like our key or
218 * doesn't support RSA authentication.
Damien Millereba71ba2000-04-29 23:57:08 +1000219 */
220 if (type == SSH_SMSG_FAILURE) {
221 debug("Server refused our key.");
222 xfree(comment);
223 return 0;
224 }
225 /* Otherwise, the server should respond with a challenge. */
226 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
227 packet_disconnect("Protocol error during RSA authentication: %d", type);
228
229 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100230 if ((challenge = BN_new()) == NULL)
231 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100232 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100233 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000234
235 debug("Received RSA challenge from server.");
236
Damien Millereba71ba2000-04-29 23:57:08 +1000237 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000238 * If the key is not stored in external hardware, we have to
239 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000240 * fails, ask for a passphrase.
241 */
Ben Lindstrome143f612002-08-20 18:41:15 +0000242 if (public->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000243 private = public;
244 else
245 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000246 if (private == NULL && !options.batch_mode) {
247 snprintf(buf, sizeof(buf),
248 "Enter passphrase for RSA key '%.100s': ", comment);
249 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000250 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000251 if (strcmp(passphrase, "") != 0) {
252 private = key_load_private_type(KEY_RSA1,
253 authfile, passphrase, NULL);
254 quit = 0;
255 } else {
256 debug2("no passphrase given, try next key");
257 quit = 1;
258 }
Damien Millereba71ba2000-04-29 23:57:08 +1000259 memset(passphrase, 0, strlen(passphrase));
260 xfree(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000261 if (private != NULL || quit)
262 break;
263 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000264 }
Damien Millereba71ba2000-04-29 23:57:08 +1000265 }
266 /* We no longer need the comment. */
267 xfree(comment);
268
Ben Lindstrom05209452001-06-25 05:16:02 +0000269 if (private == NULL) {
270 if (!options.batch_mode)
271 error("Bad passphrase.");
272
273 /* Send a dummy response packet to avoid protocol error. */
274 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
275 for (i = 0; i < 16; i++)
276 packet_put_char(0);
277 packet_send();
278 packet_write_wait();
279
280 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100281 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000282 BN_clear_free(challenge);
283 return 0;
284 }
285
Damien Millereba71ba2000-04-29 23:57:08 +1000286 /* Compute and send a response to the challenge. */
287 respond_to_rsa_challenge(challenge, private->rsa);
288
Ben Lindstromc5b68002001-07-04 04:52:03 +0000289 /* Destroy the private key unless it in external hardware. */
290 if (!(private->flags & KEY_FLAG_EXT))
291 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000292
293 /* We no longer need the challenge. */
294 BN_clear_free(challenge);
295
296 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100297 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000298 if (type == SSH_SMSG_SUCCESS) {
299 debug("RSA authentication accepted by server.");
300 return 1;
301 }
302 if (type != SSH_SMSG_FAILURE)
303 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
304 debug("RSA authentication refused.");
305 return 0;
306}
307
308/*
309 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
310 * authentication and RSA host authentication.
311 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000312static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000313try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000314{
315 int type;
316 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000317
318 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
319
320 /* Tell the server that we are willing to authenticate using this key. */
321 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000322 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000323 packet_put_int(BN_num_bits(host_key->rsa->n));
324 packet_put_bignum(host_key->rsa->e);
325 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000326 packet_send();
327 packet_write_wait();
328
329 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100330 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000331
332 /* The server responds with failure if it doesn't admit our
333 .rhosts authentication or doesn't know our host key. */
334 if (type == SSH_SMSG_FAILURE) {
335 debug("Server refused our rhosts authentication or host key.");
336 return 0;
337 }
338 /* Otherwise, the server should respond with a challenge. */
339 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
340 packet_disconnect("Protocol error during RSA authentication: %d", type);
341
342 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100343 if ((challenge = BN_new()) == NULL)
344 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100345 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100346 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000347
348 debug("Received RSA challenge for host key from server.");
349
350 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000351 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000352
353 /* We no longer need the challenge. */
354 BN_clear_free(challenge);
355
356 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100357 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000358 if (type == SSH_SMSG_SUCCESS) {
359 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
360 return 1;
361 }
362 if (type != SSH_SMSG_FAILURE)
363 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
364 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
365 return 0;
366}
367
Damien Millereba71ba2000-04-29 23:57:08 +1000368/*
369 * Tries to authenticate with any string-based challenge/response system.
370 * Note that the client code is not tied to s/key or TIS.
371 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000372static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000373try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000374{
375 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000376 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000377 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000378 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000379
380 debug("Doing challenge response authentication.");
381
Damien Millereba71ba2000-04-29 23:57:08 +1000382 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000383 /* request a challenge */
384 packet_start(SSH_CMSG_AUTH_TIS);
385 packet_send();
386 packet_write_wait();
387
Damien Millerdff50992002-01-22 23:16:32 +1100388 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000389 if (type != SSH_SMSG_FAILURE &&
390 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
391 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000392 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000393 }
394 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000395 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000396 return 0;
397 }
398 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100399 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000400 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100401 strchr(challenge, '\n') ? "" : "\nResponse: ");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000402 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000403 if (i != 0)
404 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000405 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000406 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000407 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000408 response = read_passphrase(prompt, 0);
409 if (strcmp(response, "") == 0) {
410 xfree(response);
411 break;
412 }
Damien Millereba71ba2000-04-29 23:57:08 +1000413 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100414 ssh_put_password(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000415 memset(response, 0, strlen(response));
416 xfree(response);
417 packet_send();
418 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100419 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000420 if (type == SSH_SMSG_SUCCESS)
421 return 1;
422 if (type != SSH_SMSG_FAILURE)
423 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000424 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000425 }
426 /* failure */
427 return 0;
428}
429
430/*
431 * Tries to authenticate with plain passwd authentication.
432 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000433static int
Damien Millereba71ba2000-04-29 23:57:08 +1000434try_password_authentication(char *prompt)
435{
Damien Millerdff50992002-01-22 23:16:32 +1100436 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000437 char *password;
438
439 debug("Doing password authentication.");
440 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000441 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000442 for (i = 0; i < options.number_of_password_prompts; i++) {
443 if (i != 0)
444 error("Permission denied, please try again.");
445 password = read_passphrase(prompt, 0);
446 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100447 ssh_put_password(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000448 memset(password, 0, strlen(password));
449 xfree(password);
450 packet_send();
451 packet_write_wait();
452
Damien Millerdff50992002-01-22 23:16:32 +1100453 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000454 if (type == SSH_SMSG_SUCCESS)
455 return 1;
456 if (type != SSH_SMSG_FAILURE)
457 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
458 }
459 /* failure */
460 return 0;
461}
462
463/*
464 * SSH1 key exchange
465 */
466void
467ssh_kex(char *host, struct sockaddr *hostaddr)
468{
469 int i;
470 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100471 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000472 int bits, rbits;
473 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000474 u_char session_key[SSH_SESSION_KEY_LENGTH];
475 u_char cookie[8];
476 u_int supported_ciphers;
477 u_int server_flags, client_flags;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000478 u_int32_t rnd = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000479
480 debug("Waiting for server public key.");
481
482 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100483 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000484
485 /* Get cookie from the packet. */
486 for (i = 0; i < 8; i++)
487 cookie[i] = packet_get_char();
488
489 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100490 server_key = key_new(KEY_RSA1);
491 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100492 packet_get_bignum(server_key->rsa->e);
493 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000494
Damien Millerda755162002-01-22 23:09:22 +1100495 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000496 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000497 logit("Warning: Server lies about size of server public key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000498 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000499 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000500 }
501 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100502 host_key = key_new(KEY_RSA1);
503 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100504 packet_get_bignum(host_key->rsa->e);
505 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000506
Damien Millerda755162002-01-22 23:09:22 +1100507 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000508 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000509 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000510 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000511 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000512 }
513
514 /* Get protocol flags. */
515 server_flags = packet_get_int();
516 packet_set_protocol_flags(server_flags);
517
518 supported_ciphers = packet_get_int();
519 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100520 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000521
522 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100523 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000524
Damien Millerda755162002-01-22 23:09:22 +1100525 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000526 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000527
528 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
529
Darren Tuckere14e0052004-05-13 16:30:44 +1000530 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
Damien Millereba71ba2000-04-29 23:57:08 +1000531
532 /* Generate a session key. */
533 arc4random_stir();
534
535 /*
536 * Generate an encryption key for the session. The key is a 256 bit
537 * random number, interpreted as a 32-byte key, with the least
538 * significant 8 bits being the first byte of the key.
539 */
540 for (i = 0; i < 32; i++) {
541 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000542 rnd = arc4random();
543 session_key[i] = rnd & 0xff;
544 rnd >>= 8;
Damien Millereba71ba2000-04-29 23:57:08 +1000545 }
546
547 /*
548 * According to the protocol spec, the first byte of the session key
549 * is the highest byte of the integer. The session key is xored with
550 * the first 16 bytes of the session id.
551 */
Damien Millerda755162002-01-22 23:09:22 +1100552 if ((key = BN_new()) == NULL)
553 fatal("respond_to_rsa_challenge: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000554 BN_set_word(key, 0);
555 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
556 BN_lshift(key, key, 8);
557 if (i < 16)
558 BN_add_word(key, session_key[i] ^ session_id[i]);
559 else
560 BN_add_word(key, session_key[i]);
561 }
562
563 /*
564 * Encrypt the integer using the public key and host key of the
565 * server (key with smaller modulus first).
566 */
Damien Millerda755162002-01-22 23:09:22 +1100567 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000568 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +1100569 if (BN_num_bits(host_key->rsa->n) <
570 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
571 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100572 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100573 BN_num_bits(host_key->rsa->n),
574 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100575 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000576 }
Damien Millerda755162002-01-22 23:09:22 +1100577 rsa_public_encrypt(key, key, server_key->rsa);
578 rsa_public_encrypt(key, key, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000579 } else {
580 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +1100581 if (BN_num_bits(server_key->rsa->n) <
582 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
583 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100584 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100585 BN_num_bits(server_key->rsa->n),
586 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100587 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000588 }
Damien Millerda755162002-01-22 23:09:22 +1100589 rsa_public_encrypt(key, key, host_key->rsa);
590 rsa_public_encrypt(key, key, server_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000591 }
592
593 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +1100594 key_free(server_key);
595 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000596
Damien Millere39cacc2000-11-29 12:18:44 +1100597 if (options.cipher == SSH_CIPHER_NOT_SET) {
598 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
599 options.cipher = ssh_cipher_default;
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000600 } else if (options.cipher == SSH_CIPHER_INVALID ||
Damien Millere39cacc2000-11-29 12:18:44 +1100601 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +1000602 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +1100603 cipher_name(ssh_cipher_default));
604 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +1000605 }
606 /* Check that the selected cipher is supported. */
607 if (!(supported_ciphers & (1 << options.cipher)))
608 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100609 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +1000610
611 debug("Encryption type: %.100s", cipher_name(options.cipher));
612
613 /* Send the encrypted session key to the server. */
614 packet_start(SSH_CMSG_SESSION_KEY);
615 packet_put_char(options.cipher);
616
617 /* Send the cookie back to the server. */
618 for (i = 0; i < 8; i++)
619 packet_put_char(cookie[i]);
620
621 /* Send and destroy the encrypted encryption key integer. */
622 packet_put_bignum(key);
623 BN_clear_free(key);
624
625 /* Send protocol flags. */
626 packet_put_int(client_flags);
627
628 /* Send the packet now. */
629 packet_send();
630 packet_write_wait();
631
632 debug("Sent encrypted session key.");
633
634 /* Set the encryption key. */
635 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
636
637 /* We will no longer need the session key here. Destroy any extra copies. */
638 memset(session_key, 0, sizeof(session_key));
639
640 /*
641 * Expect a success message from the server. Note that this message
642 * will be received in encrypted form.
643 */
Damien Millerdff50992002-01-22 23:16:32 +1100644 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +1000645
646 debug("Received encrypted confirmation.");
647}
648
649/*
650 * Authenticate user
651 */
652void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000653ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000654 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +1000655{
656 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100657
Damien Millereba71ba2000-04-29 23:57:08 +1000658 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000659 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +1000660
661 /* Send the name of the user to log in as on the server. */
662 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000663 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000664 packet_send();
665 packet_write_wait();
666
667 /*
668 * The server should respond with success if no authentication is
669 * needed (the user has no password). Otherwise the server responds
670 * with failure.
671 */
Damien Millerdff50992002-01-22 23:16:32 +1100672 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000673
674 /* check whether the connection was accepted without authentication. */
675 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000676 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000677 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000678 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100679
Damien Millereba71ba2000-04-29 23:57:08 +1000680 /*
Damien Millereba71ba2000-04-29 23:57:08 +1000681 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
682 * authentication.
683 */
684 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000685 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000686 for (i = 0; i < sensitive->nkeys; i++) {
687 if (sensitive->keys[i] != NULL &&
688 sensitive->keys[i]->type == KEY_RSA1 &&
689 try_rhosts_rsa_authentication(local_user,
690 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000691 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000692 }
Damien Millereba71ba2000-04-29 23:57:08 +1000693 }
694 /* Try RSA authentication if the server supports it. */
695 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
696 options.rsa_authentication) {
697 /*
698 * Try RSA authentication using the authentication agent. The
699 * agent is tried first because no passphrase is needed for
700 * it, whereas identity files may require passphrases.
701 */
702 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000703 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000704
705 /* Try RSA authentication for each identity. */
706 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000707 if (options.identity_keys[i] != NULL &&
708 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +0000709 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000710 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000711 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000712 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +1000713 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +0000714 options.challenge_response_authentication && !options.batch_mode) {
715 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000716 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000717 }
718 /* Try password authentication if the server supports it. */
719 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
720 options.password_authentication && !options.batch_mode) {
721 char prompt[80];
722
Ben Lindstrome0557162001-02-11 00:00:24 +0000723 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +1000724 server_user, host);
725 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000726 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000727 }
728 /* All authentication methods have failed. Exit with an error message. */
729 fatal("Permission denied.");
730 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000731
732 success:
Damien Miller40eb1d82001-07-14 12:16:59 +1000733 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +1000734}