blob: 51f1f8088fcbae3e6e6791dedbaacbd742703569 [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: sshconnect1.c,v 1.69 2006/08/03 03:34:42 deraadt Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millereba71ba2000-04-29 23:57:08 +10006 * Code to connect to a remote host, and to perform the client side of the
7 * login (authentication) dialog.
8 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Millereba71ba2000-04-29 23:57:08 +100014 */
15
16#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +100017
Damien Millerd7834352006-08-05 12:39:39 +100018#include <sys/types.h>
19#include <sys/socket.h>
20
Damien Millereba71ba2000-04-29 23:57:08 +100021#include <openssl/bn.h>
Damien Miller2ce18da2002-02-13 13:54:27 +110022#include <openssl/md5.h>
Damien Millereba71ba2000-04-29 23:57:08 +100023
Damien Millera7a73ee2006-08-05 11:37:59 +100024#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100025#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100026#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100027#include <signal.h>
28#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029
Damien Millerd7834352006-08-05 12:39:39 +100030#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000031#include "ssh.h"
32#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100033#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "buffer.h"
35#include "packet.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#include "key.h"
37#include "cipher.h"
Darren Tuckere14e0052004-05-13 16:30:44 +100038#include "kex.h"
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "readconf.h"
Damien Miller994cf142000-07-21 10:19:44 +100042#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "sshconnect.h"
44#include "authfile.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100045#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#include "hostfile.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000048#include "auth.h"
Damien Millereba71ba2000-04-29 23:57:08 +100049
50/* Session id for the current session. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000051u_char session_id[16];
52u_int supported_authentications = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100053
54extern Options options;
55extern char *__progname;
56
57/*
58 * Checks if the user has an authentication agent, and if so, tries to
59 * authenticate using the agent.
60 */
Ben Lindstrombba81212001-06-25 05:01:22 +000061static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000062try_agent_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +100063{
Damien Millerad833b32000-08-23 10:46:23 +100064 int type;
Damien Millereba71ba2000-04-29 23:57:08 +100065 char *comment;
66 AuthenticationConnection *auth;
Ben Lindstrom46c16222000-12-22 01:43:59 +000067 u_char response[16];
68 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +100069 Key *key;
70 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +100071
72 /* Get connection to the agent. */
73 auth = ssh_get_authentication_connection();
74 if (!auth)
75 return 0;
76
Damien Millerda755162002-01-22 23:09:22 +110077 if ((challenge = BN_new()) == NULL)
78 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100079 /* Loop through identities served by the agent. */
Damien Millerad833b32000-08-23 10:46:23 +100080 for (key = ssh_get_first_identity(auth, &comment, 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +110081 key != NULL;
82 key = ssh_get_next_identity(auth, &comment, 1)) {
Damien Millereba71ba2000-04-29 23:57:08 +100083
84 /* Try this identity. */
85 debug("Trying RSA authentication via agent with '%.100s'", comment);
86 xfree(comment);
87
88 /* Tell the server that we are willing to authenticate using this key. */
89 packet_start(SSH_CMSG_AUTH_RSA);
Damien Millerad833b32000-08-23 10:46:23 +100090 packet_put_bignum(key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +100091 packet_send();
92 packet_write_wait();
93
94 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +110095 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +100096
Damien Miller788f2122005-11-05 15:14:59 +110097 /* The server sends failure if it doesn't like our key or
Damien Millereba71ba2000-04-29 23:57:08 +100098 does not support RSA authentication. */
99 if (type == SSH_SMSG_FAILURE) {
100 debug("Server refused our key.");
Damien Millerad833b32000-08-23 10:46:23 +1000101 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000102 continue;
103 }
104 /* Otherwise it should have sent a challenge. */
105 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
106 packet_disconnect("Protocol error during RSA authentication: %d",
107 type);
108
Damien Millerd432ccf2002-01-22 23:14:44 +1100109 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100110 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000111
112 debug("Received RSA challenge from server.");
113
114 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000115 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
116 /*
117 * The agent failed to authenticate this identifier
118 * although it advertised it supports this. Just
119 * return a wrong value.
120 */
Damien Miller996acd22003-04-09 20:59:48 +1000121 logit("Authentication agent failed to decrypt challenge.");
Damien Millereba71ba2000-04-29 23:57:08 +1000122 memset(response, 0, sizeof(response));
123 }
Damien Millerad833b32000-08-23 10:46:23 +1000124 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000125 debug("Sending response to RSA challenge.");
126
127 /* Send the decrypted challenge back to the server. */
128 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
129 for (i = 0; i < 16; i++)
130 packet_put_char(response[i]);
131 packet_send();
132 packet_write_wait();
133
134 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100135 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000136
137 /* The server returns success if it accepted the authentication. */
138 if (type == SSH_SMSG_SUCCESS) {
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000139 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000140 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000141 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000142 return 1;
143 }
144 /* Otherwise it should return failure. */
145 if (type != SSH_SMSG_FAILURE)
146 packet_disconnect("Protocol error waiting RSA auth response: %d",
147 type);
148 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000149 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000150 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000151 debug("RSA authentication using agent refused.");
152 return 0;
153}
154
155/*
156 * Computes the proper response to a RSA challenge, and sends the response to
157 * the server.
158 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000159static void
Damien Millereba71ba2000-04-29 23:57:08 +1000160respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
161{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000162 u_char buf[32], response[16];
Damien Millereba71ba2000-04-29 23:57:08 +1000163 MD5_CTX md;
164 int i, len;
165
166 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100167 /* XXX think about Bleichenbacher, too */
168 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
169 packet_disconnect(
170 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000171
172 /* Compute the response. */
173 /* The response is MD5 of decrypted challenge plus session id. */
174 len = BN_num_bytes(challenge);
Damien Millereccb9de2005-06-17 12:59:34 +1000175 if (len <= 0 || (u_int)len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100176 packet_disconnect(
177 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000178
179 memset(buf, 0, sizeof(buf));
180 BN_bn2bin(challenge, buf + sizeof(buf) - len);
181 MD5_Init(&md);
182 MD5_Update(&md, buf, 32);
183 MD5_Update(&md, session_id, 16);
184 MD5_Final(response, &md);
185
186 debug("Sending response to host key RSA challenge.");
187
188 /* Send the response back to the server. */
189 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
190 for (i = 0; i < 16; i++)
191 packet_put_char(response[i]);
192 packet_send();
193 packet_write_wait();
194
195 memset(buf, 0, sizeof(buf));
196 memset(response, 0, sizeof(response));
197 memset(&md, 0, sizeof(md));
198}
199
200/*
201 * Checks if the user has authentication file, and if so, tries to authenticate
202 * the user using it.
203 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000205try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000206{
207 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000208 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000209 char buf[300], *passphrase, *comment, *authfile;
Darren Tucker232b76f2006-05-06 17:41:51 +1000210 int i, perm_ok = 1, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000211
Ben Lindstromc5b68002001-07-04 04:52:03 +0000212 public = options.identity_keys[idx];
213 authfile = options.identity_files[idx];
214 comment = xstrdup(authfile);
215
Damien Millereba71ba2000-04-29 23:57:08 +1000216 debug("Trying RSA authentication with key '%.100s'", comment);
217
218 /* Tell the server that we are willing to authenticate using this key. */
219 packet_start(SSH_CMSG_AUTH_RSA);
220 packet_put_bignum(public->rsa->n);
221 packet_send();
222 packet_write_wait();
223
Damien Millereba71ba2000-04-29 23:57:08 +1000224 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100225 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000226
227 /*
Damien Miller788f2122005-11-05 15:14:59 +1100228 * The server responds with failure if it doesn't like our key or
229 * doesn't support RSA authentication.
Damien Millereba71ba2000-04-29 23:57:08 +1000230 */
231 if (type == SSH_SMSG_FAILURE) {
232 debug("Server refused our key.");
233 xfree(comment);
234 return 0;
235 }
236 /* Otherwise, the server should respond with a challenge. */
237 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
238 packet_disconnect("Protocol error during RSA authentication: %d", type);
239
240 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100241 if ((challenge = BN_new()) == NULL)
242 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100243 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100244 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000245
246 debug("Received RSA challenge from server.");
247
Damien Millereba71ba2000-04-29 23:57:08 +1000248 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000249 * If the key is not stored in external hardware, we have to
250 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000251 * fails, ask for a passphrase.
252 */
Ben Lindstrome143f612002-08-20 18:41:15 +0000253 if (public->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000254 private = public;
255 else
Darren Tucker232b76f2006-05-06 17:41:51 +1000256 private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
257 &perm_ok);
258 if (private == NULL && !options.batch_mode && perm_ok) {
Ben Lindstrom05209452001-06-25 05:16:02 +0000259 snprintf(buf, sizeof(buf),
260 "Enter passphrase for RSA key '%.100s': ", comment);
261 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000262 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000263 if (strcmp(passphrase, "") != 0) {
264 private = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000265 authfile, passphrase, NULL, NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000266 quit = 0;
267 } else {
268 debug2("no passphrase given, try next key");
269 quit = 1;
270 }
Damien Millereba71ba2000-04-29 23:57:08 +1000271 memset(passphrase, 0, strlen(passphrase));
272 xfree(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000273 if (private != NULL || quit)
274 break;
275 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000276 }
Damien Millereba71ba2000-04-29 23:57:08 +1000277 }
278 /* We no longer need the comment. */
279 xfree(comment);
280
Ben Lindstrom05209452001-06-25 05:16:02 +0000281 if (private == NULL) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000282 if (!options.batch_mode && perm_ok)
Ben Lindstrom05209452001-06-25 05:16:02 +0000283 error("Bad passphrase.");
284
285 /* Send a dummy response packet to avoid protocol error. */
286 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
287 for (i = 0; i < 16; i++)
288 packet_put_char(0);
289 packet_send();
290 packet_write_wait();
291
292 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100293 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000294 BN_clear_free(challenge);
295 return 0;
296 }
297
Damien Millereba71ba2000-04-29 23:57:08 +1000298 /* Compute and send a response to the challenge. */
299 respond_to_rsa_challenge(challenge, private->rsa);
300
Ben Lindstromc5b68002001-07-04 04:52:03 +0000301 /* Destroy the private key unless it in external hardware. */
302 if (!(private->flags & KEY_FLAG_EXT))
303 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000304
305 /* We no longer need the challenge. */
306 BN_clear_free(challenge);
307
308 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100309 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000310 if (type == SSH_SMSG_SUCCESS) {
311 debug("RSA authentication accepted by server.");
312 return 1;
313 }
314 if (type != SSH_SMSG_FAILURE)
315 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
316 debug("RSA authentication refused.");
317 return 0;
318}
319
320/*
321 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
322 * authentication and RSA host authentication.
323 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000324static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000325try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000326{
327 int type;
328 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000329
330 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
331
332 /* Tell the server that we are willing to authenticate using this key. */
333 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000334 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000335 packet_put_int(BN_num_bits(host_key->rsa->n));
336 packet_put_bignum(host_key->rsa->e);
337 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000338 packet_send();
339 packet_write_wait();
340
341 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100342 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000343
344 /* The server responds with failure if it doesn't admit our
345 .rhosts authentication or doesn't know our host key. */
346 if (type == SSH_SMSG_FAILURE) {
347 debug("Server refused our rhosts authentication or host key.");
348 return 0;
349 }
350 /* Otherwise, the server should respond with a challenge. */
351 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
352 packet_disconnect("Protocol error during RSA authentication: %d", type);
353
354 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100355 if ((challenge = BN_new()) == NULL)
356 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100357 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100358 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000359
360 debug("Received RSA challenge for host key from server.");
361
362 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000363 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000364
365 /* We no longer need the challenge. */
366 BN_clear_free(challenge);
367
368 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100369 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000370 if (type == SSH_SMSG_SUCCESS) {
371 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
372 return 1;
373 }
374 if (type != SSH_SMSG_FAILURE)
375 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
376 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
377 return 0;
378}
379
Damien Millereba71ba2000-04-29 23:57:08 +1000380/*
381 * Tries to authenticate with any string-based challenge/response system.
382 * Note that the client code is not tied to s/key or TIS.
383 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000384static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000385try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000386{
387 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000388 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000389 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000390 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000391
392 debug("Doing challenge response authentication.");
393
Damien Millereba71ba2000-04-29 23:57:08 +1000394 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000395 /* request a challenge */
396 packet_start(SSH_CMSG_AUTH_TIS);
397 packet_send();
398 packet_write_wait();
399
Damien Millerdff50992002-01-22 23:16:32 +1100400 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000401 if (type != SSH_SMSG_FAILURE &&
402 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
403 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000404 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000405 }
406 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000407 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000408 return 0;
409 }
410 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100411 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000412 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100413 strchr(challenge, '\n') ? "" : "\nResponse: ");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000414 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000415 if (i != 0)
416 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000417 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000418 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000419 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000420 response = read_passphrase(prompt, 0);
421 if (strcmp(response, "") == 0) {
422 xfree(response);
423 break;
424 }
Damien Millereba71ba2000-04-29 23:57:08 +1000425 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100426 ssh_put_password(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000427 memset(response, 0, strlen(response));
428 xfree(response);
429 packet_send();
430 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100431 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000432 if (type == SSH_SMSG_SUCCESS)
433 return 1;
434 if (type != SSH_SMSG_FAILURE)
435 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000436 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000437 }
438 /* failure */
439 return 0;
440}
441
442/*
443 * Tries to authenticate with plain passwd authentication.
444 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000445static int
Damien Millereba71ba2000-04-29 23:57:08 +1000446try_password_authentication(char *prompt)
447{
Damien Millerdff50992002-01-22 23:16:32 +1100448 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000449 char *password;
450
451 debug("Doing password authentication.");
452 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000453 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000454 for (i = 0; i < options.number_of_password_prompts; i++) {
455 if (i != 0)
456 error("Permission denied, please try again.");
457 password = read_passphrase(prompt, 0);
458 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100459 ssh_put_password(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000460 memset(password, 0, strlen(password));
461 xfree(password);
462 packet_send();
463 packet_write_wait();
464
Damien Millerdff50992002-01-22 23:16:32 +1100465 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000466 if (type == SSH_SMSG_SUCCESS)
467 return 1;
468 if (type != SSH_SMSG_FAILURE)
469 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
470 }
471 /* failure */
472 return 0;
473}
474
475/*
476 * SSH1 key exchange
477 */
478void
479ssh_kex(char *host, struct sockaddr *hostaddr)
480{
481 int i;
482 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100483 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000484 int bits, rbits;
485 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000486 u_char session_key[SSH_SESSION_KEY_LENGTH];
487 u_char cookie[8];
488 u_int supported_ciphers;
489 u_int server_flags, client_flags;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000490 u_int32_t rnd = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000491
492 debug("Waiting for server public key.");
493
494 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100495 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000496
497 /* Get cookie from the packet. */
498 for (i = 0; i < 8; i++)
499 cookie[i] = packet_get_char();
500
501 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100502 server_key = key_new(KEY_RSA1);
503 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100504 packet_get_bignum(server_key->rsa->e);
505 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000506
Damien Millerda755162002-01-22 23:09:22 +1100507 rbits = BN_num_bits(server_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 public 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 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100514 host_key = key_new(KEY_RSA1);
515 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100516 packet_get_bignum(host_key->rsa->e);
517 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000518
Damien Millerda755162002-01-22 23:09:22 +1100519 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000520 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000521 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000522 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000523 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000524 }
525
526 /* Get protocol flags. */
527 server_flags = packet_get_int();
528 packet_set_protocol_flags(server_flags);
529
530 supported_ciphers = packet_get_int();
531 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100532 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000533
534 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100535 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000536
Damien Millerda755162002-01-22 23:09:22 +1100537 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000538 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000539
540 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
541
Darren Tuckere14e0052004-05-13 16:30:44 +1000542 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
Damien Millereba71ba2000-04-29 23:57:08 +1000543
544 /* Generate a session key. */
545 arc4random_stir();
546
547 /*
548 * Generate an encryption key for the session. The key is a 256 bit
549 * random number, interpreted as a 32-byte key, with the least
550 * significant 8 bits being the first byte of the key.
551 */
552 for (i = 0; i < 32; i++) {
553 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000554 rnd = arc4random();
555 session_key[i] = rnd & 0xff;
556 rnd >>= 8;
Damien Millereba71ba2000-04-29 23:57:08 +1000557 }
558
559 /*
560 * According to the protocol spec, the first byte of the session key
561 * is the highest byte of the integer. The session key is xored with
562 * the first 16 bytes of the session id.
563 */
Damien Millerda755162002-01-22 23:09:22 +1100564 if ((key = BN_new()) == NULL)
565 fatal("respond_to_rsa_challenge: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000566 BN_set_word(key, 0);
567 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
568 BN_lshift(key, key, 8);
569 if (i < 16)
570 BN_add_word(key, session_key[i] ^ session_id[i]);
571 else
572 BN_add_word(key, session_key[i]);
573 }
574
575 /*
576 * Encrypt the integer using the public key and host key of the
577 * server (key with smaller modulus first).
578 */
Damien Millerda755162002-01-22 23:09:22 +1100579 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000580 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +1100581 if (BN_num_bits(host_key->rsa->n) <
582 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
583 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100584 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100585 BN_num_bits(host_key->rsa->n),
586 BN_num_bits(server_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, server_key->rsa);
590 rsa_public_encrypt(key, key, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000591 } else {
592 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +1100593 if (BN_num_bits(server_key->rsa->n) <
594 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
595 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100596 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100597 BN_num_bits(server_key->rsa->n),
598 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100599 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000600 }
Damien Millerda755162002-01-22 23:09:22 +1100601 rsa_public_encrypt(key, key, host_key->rsa);
602 rsa_public_encrypt(key, key, server_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000603 }
604
605 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +1100606 key_free(server_key);
607 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000608
Damien Millere39cacc2000-11-29 12:18:44 +1100609 if (options.cipher == SSH_CIPHER_NOT_SET) {
610 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
611 options.cipher = ssh_cipher_default;
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000612 } else if (options.cipher == SSH_CIPHER_INVALID ||
Damien Millere39cacc2000-11-29 12:18:44 +1100613 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +1000614 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +1100615 cipher_name(ssh_cipher_default));
616 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +1000617 }
618 /* Check that the selected cipher is supported. */
619 if (!(supported_ciphers & (1 << options.cipher)))
620 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100621 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +1000622
623 debug("Encryption type: %.100s", cipher_name(options.cipher));
624
625 /* Send the encrypted session key to the server. */
626 packet_start(SSH_CMSG_SESSION_KEY);
627 packet_put_char(options.cipher);
628
629 /* Send the cookie back to the server. */
630 for (i = 0; i < 8; i++)
631 packet_put_char(cookie[i]);
632
633 /* Send and destroy the encrypted encryption key integer. */
634 packet_put_bignum(key);
635 BN_clear_free(key);
636
637 /* Send protocol flags. */
638 packet_put_int(client_flags);
639
640 /* Send the packet now. */
641 packet_send();
642 packet_write_wait();
643
644 debug("Sent encrypted session key.");
645
646 /* Set the encryption key. */
647 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
648
649 /* We will no longer need the session key here. Destroy any extra copies. */
650 memset(session_key, 0, sizeof(session_key));
651
652 /*
653 * Expect a success message from the server. Note that this message
654 * will be received in encrypted form.
655 */
Damien Millerdff50992002-01-22 23:16:32 +1100656 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +1000657
658 debug("Received encrypted confirmation.");
659}
660
661/*
662 * Authenticate user
663 */
664void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000665ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000666 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +1000667{
668 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100669
Damien Millereba71ba2000-04-29 23:57:08 +1000670 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000671 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +1000672
673 /* Send the name of the user to log in as on the server. */
674 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000675 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000676 packet_send();
677 packet_write_wait();
678
679 /*
680 * The server should respond with success if no authentication is
681 * needed (the user has no password). Otherwise the server responds
682 * with failure.
683 */
Damien Millerdff50992002-01-22 23:16:32 +1100684 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000685
686 /* check whether the connection was accepted without authentication. */
687 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000688 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000689 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000690 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100691
Damien Millereba71ba2000-04-29 23:57:08 +1000692 /*
Damien Millereba71ba2000-04-29 23:57:08 +1000693 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
694 * authentication.
695 */
696 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000697 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000698 for (i = 0; i < sensitive->nkeys; i++) {
699 if (sensitive->keys[i] != NULL &&
700 sensitive->keys[i]->type == KEY_RSA1 &&
701 try_rhosts_rsa_authentication(local_user,
702 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000703 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000704 }
Damien Millereba71ba2000-04-29 23:57:08 +1000705 }
706 /* Try RSA authentication if the server supports it. */
707 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
708 options.rsa_authentication) {
709 /*
710 * Try RSA authentication using the authentication agent. The
711 * agent is tried first because no passphrase is needed for
712 * it, whereas identity files may require passphrases.
713 */
714 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000715 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000716
717 /* Try RSA authentication for each identity. */
718 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000719 if (options.identity_keys[i] != NULL &&
720 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +0000721 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000722 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000723 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000724 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +1000725 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +0000726 options.challenge_response_authentication && !options.batch_mode) {
727 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000728 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000729 }
730 /* Try password authentication if the server supports it. */
731 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
732 options.password_authentication && !options.batch_mode) {
733 char prompt[80];
734
Ben Lindstrome0557162001-02-11 00:00:24 +0000735 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +1000736 server_user, host);
737 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000738 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000739 }
740 /* All authentication methods have failed. Exit with an error message. */
741 fatal("Permission denied.");
742 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000743
744 success:
Damien Miller40eb1d82001-07-14 12:16:59 +1000745 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +1000746}