blob: 37024e7507c0b262aadc0b711e6808874b937b8a [file] [log] [blame]
Damien Millere7a1e5c2006-08-05 11:34:19 +10001/* $OpenBSD: sshconnect1.c,v 1.67 2006/07/26 13:57:17 stevesk 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
18#include <openssl/bn.h>
Damien Miller2ce18da2002-02-13 13:54:27 +110019#include <openssl/md5.h>
Damien Millereba71ba2000-04-29 23:57:08 +100020
Damien Millere7a1e5c2006-08-05 11:34:19 +100021#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100022#include <string.h>
23
Ben Lindstrom226cfa02001-01-22 05:34:40 +000024#include "ssh.h"
25#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100026#include "xmalloc.h"
27#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100028#include "buffer.h"
29#include "packet.h"
Darren Tuckere14e0052004-05-13 16:30:44 +100030#include "kex.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000032#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100033#include "readconf.h"
34#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100035#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100036#include "sshconnect.h"
37#include "authfile.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100038#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "cipher.h"
40#include "canohost.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000041#include "auth.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042
43/* Session id for the current session. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000044u_char session_id[16];
45u_int supported_authentications = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100046
47extern Options options;
48extern char *__progname;
49
50/*
51 * Checks if the user has an authentication agent, and if so, tries to
52 * authenticate using the agent.
53 */
Ben Lindstrombba81212001-06-25 05:01:22 +000054static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000055try_agent_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +100056{
Damien Millerad833b32000-08-23 10:46:23 +100057 int type;
Damien Millereba71ba2000-04-29 23:57:08 +100058 char *comment;
59 AuthenticationConnection *auth;
Ben Lindstrom46c16222000-12-22 01:43:59 +000060 u_char response[16];
61 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +100062 Key *key;
63 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +100064
65 /* Get connection to the agent. */
66 auth = ssh_get_authentication_connection();
67 if (!auth)
68 return 0;
69
Damien Millerda755162002-01-22 23:09:22 +110070 if ((challenge = BN_new()) == NULL)
71 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100072 /* Loop through identities served by the agent. */
Damien Millerad833b32000-08-23 10:46:23 +100073 for (key = ssh_get_first_identity(auth, &comment, 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +110074 key != NULL;
75 key = ssh_get_next_identity(auth, &comment, 1)) {
Damien Millereba71ba2000-04-29 23:57:08 +100076
77 /* Try this identity. */
78 debug("Trying RSA authentication via agent with '%.100s'", comment);
79 xfree(comment);
80
81 /* Tell the server that we are willing to authenticate using this key. */
82 packet_start(SSH_CMSG_AUTH_RSA);
Damien Millerad833b32000-08-23 10:46:23 +100083 packet_put_bignum(key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +100084 packet_send();
85 packet_write_wait();
86
87 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +110088 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +100089
Damien Miller788f2122005-11-05 15:14:59 +110090 /* The server sends failure if it doesn't like our key or
Damien Millereba71ba2000-04-29 23:57:08 +100091 does not support RSA authentication. */
92 if (type == SSH_SMSG_FAILURE) {
93 debug("Server refused our key.");
Damien Millerad833b32000-08-23 10:46:23 +100094 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +100095 continue;
96 }
97 /* Otherwise it should have sent a challenge. */
98 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
99 packet_disconnect("Protocol error during RSA authentication: %d",
100 type);
101
Damien Millerd432ccf2002-01-22 23:14:44 +1100102 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100103 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000104
105 debug("Received RSA challenge from server.");
106
107 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000108 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
109 /*
110 * The agent failed to authenticate this identifier
111 * although it advertised it supports this. Just
112 * return a wrong value.
113 */
Damien Miller996acd22003-04-09 20:59:48 +1000114 logit("Authentication agent failed to decrypt challenge.");
Damien Millereba71ba2000-04-29 23:57:08 +1000115 memset(response, 0, sizeof(response));
116 }
Damien Millerad833b32000-08-23 10:46:23 +1000117 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000118 debug("Sending response to RSA challenge.");
119
120 /* Send the decrypted challenge back to the server. */
121 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
122 for (i = 0; i < 16; i++)
123 packet_put_char(response[i]);
124 packet_send();
125 packet_write_wait();
126
127 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100128 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000129
130 /* The server returns success if it accepted the authentication. */
131 if (type == SSH_SMSG_SUCCESS) {
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000132 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000133 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000134 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000135 return 1;
136 }
137 /* Otherwise it should return failure. */
138 if (type != SSH_SMSG_FAILURE)
139 packet_disconnect("Protocol error waiting RSA auth response: %d",
140 type);
141 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000142 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000143 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000144 debug("RSA authentication using agent refused.");
145 return 0;
146}
147
148/*
149 * Computes the proper response to a RSA challenge, and sends the response to
150 * the server.
151 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000152static void
Damien Millereba71ba2000-04-29 23:57:08 +1000153respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
154{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000155 u_char buf[32], response[16];
Damien Millereba71ba2000-04-29 23:57:08 +1000156 MD5_CTX md;
157 int i, len;
158
159 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100160 /* XXX think about Bleichenbacher, too */
161 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
162 packet_disconnect(
163 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000164
165 /* Compute the response. */
166 /* The response is MD5 of decrypted challenge plus session id. */
167 len = BN_num_bytes(challenge);
Damien Millereccb9de2005-06-17 12:59:34 +1000168 if (len <= 0 || (u_int)len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100169 packet_disconnect(
170 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000171
172 memset(buf, 0, sizeof(buf));
173 BN_bn2bin(challenge, buf + sizeof(buf) - len);
174 MD5_Init(&md);
175 MD5_Update(&md, buf, 32);
176 MD5_Update(&md, session_id, 16);
177 MD5_Final(response, &md);
178
179 debug("Sending response to host key RSA challenge.");
180
181 /* Send the response back to the server. */
182 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
183 for (i = 0; i < 16; i++)
184 packet_put_char(response[i]);
185 packet_send();
186 packet_write_wait();
187
188 memset(buf, 0, sizeof(buf));
189 memset(response, 0, sizeof(response));
190 memset(&md, 0, sizeof(md));
191}
192
193/*
194 * Checks if the user has authentication file, and if so, tries to authenticate
195 * the user using it.
196 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000198try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000199{
200 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000201 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000202 char buf[300], *passphrase, *comment, *authfile;
Darren Tucker232b76f2006-05-06 17:41:51 +1000203 int i, perm_ok = 1, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000204
Ben Lindstromc5b68002001-07-04 04:52:03 +0000205 public = options.identity_keys[idx];
206 authfile = options.identity_files[idx];
207 comment = xstrdup(authfile);
208
Damien Millereba71ba2000-04-29 23:57:08 +1000209 debug("Trying RSA authentication with key '%.100s'", comment);
210
211 /* Tell the server that we are willing to authenticate using this key. */
212 packet_start(SSH_CMSG_AUTH_RSA);
213 packet_put_bignum(public->rsa->n);
214 packet_send();
215 packet_write_wait();
216
Damien Millereba71ba2000-04-29 23:57:08 +1000217 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100218 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000219
220 /*
Damien Miller788f2122005-11-05 15:14:59 +1100221 * The server responds with failure if it doesn't like our key or
222 * doesn't support RSA authentication.
Damien Millereba71ba2000-04-29 23:57:08 +1000223 */
224 if (type == SSH_SMSG_FAILURE) {
225 debug("Server refused our key.");
226 xfree(comment);
227 return 0;
228 }
229 /* Otherwise, the server should respond with a challenge. */
230 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
231 packet_disconnect("Protocol error during RSA authentication: %d", type);
232
233 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100234 if ((challenge = BN_new()) == NULL)
235 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100236 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100237 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000238
239 debug("Received RSA challenge from server.");
240
Damien Millereba71ba2000-04-29 23:57:08 +1000241 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000242 * If the key is not stored in external hardware, we have to
243 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000244 * fails, ask for a passphrase.
245 */
Ben Lindstrome143f612002-08-20 18:41:15 +0000246 if (public->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000247 private = public;
248 else
Darren Tucker232b76f2006-05-06 17:41:51 +1000249 private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
250 &perm_ok);
251 if (private == NULL && !options.batch_mode && perm_ok) {
Ben Lindstrom05209452001-06-25 05:16:02 +0000252 snprintf(buf, sizeof(buf),
253 "Enter passphrase for RSA key '%.100s': ", comment);
254 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000255 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000256 if (strcmp(passphrase, "") != 0) {
257 private = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000258 authfile, passphrase, NULL, NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000259 quit = 0;
260 } else {
261 debug2("no passphrase given, try next key");
262 quit = 1;
263 }
Damien Millereba71ba2000-04-29 23:57:08 +1000264 memset(passphrase, 0, strlen(passphrase));
265 xfree(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000266 if (private != NULL || quit)
267 break;
268 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000269 }
Damien Millereba71ba2000-04-29 23:57:08 +1000270 }
271 /* We no longer need the comment. */
272 xfree(comment);
273
Ben Lindstrom05209452001-06-25 05:16:02 +0000274 if (private == NULL) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000275 if (!options.batch_mode && perm_ok)
Ben Lindstrom05209452001-06-25 05:16:02 +0000276 error("Bad passphrase.");
277
278 /* Send a dummy response packet to avoid protocol error. */
279 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
280 for (i = 0; i < 16; i++)
281 packet_put_char(0);
282 packet_send();
283 packet_write_wait();
284
285 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100286 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000287 BN_clear_free(challenge);
288 return 0;
289 }
290
Damien Millereba71ba2000-04-29 23:57:08 +1000291 /* Compute and send a response to the challenge. */
292 respond_to_rsa_challenge(challenge, private->rsa);
293
Ben Lindstromc5b68002001-07-04 04:52:03 +0000294 /* Destroy the private key unless it in external hardware. */
295 if (!(private->flags & KEY_FLAG_EXT))
296 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000297
298 /* We no longer need the challenge. */
299 BN_clear_free(challenge);
300
301 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100302 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000303 if (type == SSH_SMSG_SUCCESS) {
304 debug("RSA authentication accepted by server.");
305 return 1;
306 }
307 if (type != SSH_SMSG_FAILURE)
308 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
309 debug("RSA authentication refused.");
310 return 0;
311}
312
313/*
314 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
315 * authentication and RSA host authentication.
316 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000317static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000318try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000319{
320 int type;
321 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000322
323 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
324
325 /* Tell the server that we are willing to authenticate using this key. */
326 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000327 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000328 packet_put_int(BN_num_bits(host_key->rsa->n));
329 packet_put_bignum(host_key->rsa->e);
330 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000331 packet_send();
332 packet_write_wait();
333
334 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100335 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000336
337 /* The server responds with failure if it doesn't admit our
338 .rhosts authentication or doesn't know our host key. */
339 if (type == SSH_SMSG_FAILURE) {
340 debug("Server refused our rhosts authentication or host key.");
341 return 0;
342 }
343 /* Otherwise, the server should respond with a challenge. */
344 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
345 packet_disconnect("Protocol error during RSA authentication: %d", type);
346
347 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100348 if ((challenge = BN_new()) == NULL)
349 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100350 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100351 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000352
353 debug("Received RSA challenge for host key from server.");
354
355 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000356 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000357
358 /* We no longer need the challenge. */
359 BN_clear_free(challenge);
360
361 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100362 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000363 if (type == SSH_SMSG_SUCCESS) {
364 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
365 return 1;
366 }
367 if (type != SSH_SMSG_FAILURE)
368 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
369 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
370 return 0;
371}
372
Damien Millereba71ba2000-04-29 23:57:08 +1000373/*
374 * Tries to authenticate with any string-based challenge/response system.
375 * Note that the client code is not tied to s/key or TIS.
376 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000377static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000378try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000379{
380 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000381 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000382 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000383 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000384
385 debug("Doing challenge response authentication.");
386
Damien Millereba71ba2000-04-29 23:57:08 +1000387 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000388 /* request a challenge */
389 packet_start(SSH_CMSG_AUTH_TIS);
390 packet_send();
391 packet_write_wait();
392
Damien Millerdff50992002-01-22 23:16:32 +1100393 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000394 if (type != SSH_SMSG_FAILURE &&
395 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
396 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000397 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000398 }
399 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000400 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000401 return 0;
402 }
403 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100404 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000405 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100406 strchr(challenge, '\n') ? "" : "\nResponse: ");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000407 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000408 if (i != 0)
409 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000410 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000411 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000412 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000413 response = read_passphrase(prompt, 0);
414 if (strcmp(response, "") == 0) {
415 xfree(response);
416 break;
417 }
Damien Millereba71ba2000-04-29 23:57:08 +1000418 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100419 ssh_put_password(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000420 memset(response, 0, strlen(response));
421 xfree(response);
422 packet_send();
423 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100424 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000425 if (type == SSH_SMSG_SUCCESS)
426 return 1;
427 if (type != SSH_SMSG_FAILURE)
428 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000429 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000430 }
431 /* failure */
432 return 0;
433}
434
435/*
436 * Tries to authenticate with plain passwd authentication.
437 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000438static int
Damien Millereba71ba2000-04-29 23:57:08 +1000439try_password_authentication(char *prompt)
440{
Damien Millerdff50992002-01-22 23:16:32 +1100441 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000442 char *password;
443
444 debug("Doing password authentication.");
445 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000446 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000447 for (i = 0; i < options.number_of_password_prompts; i++) {
448 if (i != 0)
449 error("Permission denied, please try again.");
450 password = read_passphrase(prompt, 0);
451 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100452 ssh_put_password(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000453 memset(password, 0, strlen(password));
454 xfree(password);
455 packet_send();
456 packet_write_wait();
457
Damien Millerdff50992002-01-22 23:16:32 +1100458 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000459 if (type == SSH_SMSG_SUCCESS)
460 return 1;
461 if (type != SSH_SMSG_FAILURE)
462 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
463 }
464 /* failure */
465 return 0;
466}
467
468/*
469 * SSH1 key exchange
470 */
471void
472ssh_kex(char *host, struct sockaddr *hostaddr)
473{
474 int i;
475 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100476 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000477 int bits, rbits;
478 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000479 u_char session_key[SSH_SESSION_KEY_LENGTH];
480 u_char cookie[8];
481 u_int supported_ciphers;
482 u_int server_flags, client_flags;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000483 u_int32_t rnd = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000484
485 debug("Waiting for server public key.");
486
487 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100488 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000489
490 /* Get cookie from the packet. */
491 for (i = 0; i < 8; i++)
492 cookie[i] = packet_get_char();
493
494 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100495 server_key = key_new(KEY_RSA1);
496 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100497 packet_get_bignum(server_key->rsa->e);
498 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000499
Damien Millerda755162002-01-22 23:09:22 +1100500 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000501 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000502 logit("Warning: Server lies about size of server public key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000503 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000504 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000505 }
506 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100507 host_key = key_new(KEY_RSA1);
508 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100509 packet_get_bignum(host_key->rsa->e);
510 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000511
Damien Millerda755162002-01-22 23:09:22 +1100512 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000513 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000514 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000515 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000516 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000517 }
518
519 /* Get protocol flags. */
520 server_flags = packet_get_int();
521 packet_set_protocol_flags(server_flags);
522
523 supported_ciphers = packet_get_int();
524 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100525 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000526
527 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100528 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000529
Damien Millerda755162002-01-22 23:09:22 +1100530 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000531 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000532
533 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
534
Darren Tuckere14e0052004-05-13 16:30:44 +1000535 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
Damien Millereba71ba2000-04-29 23:57:08 +1000536
537 /* Generate a session key. */
538 arc4random_stir();
539
540 /*
541 * Generate an encryption key for the session. The key is a 256 bit
542 * random number, interpreted as a 32-byte key, with the least
543 * significant 8 bits being the first byte of the key.
544 */
545 for (i = 0; i < 32; i++) {
546 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000547 rnd = arc4random();
548 session_key[i] = rnd & 0xff;
549 rnd >>= 8;
Damien Millereba71ba2000-04-29 23:57:08 +1000550 }
551
552 /*
553 * According to the protocol spec, the first byte of the session key
554 * is the highest byte of the integer. The session key is xored with
555 * the first 16 bytes of the session id.
556 */
Damien Millerda755162002-01-22 23:09:22 +1100557 if ((key = BN_new()) == NULL)
558 fatal("respond_to_rsa_challenge: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000559 BN_set_word(key, 0);
560 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
561 BN_lshift(key, key, 8);
562 if (i < 16)
563 BN_add_word(key, session_key[i] ^ session_id[i]);
564 else
565 BN_add_word(key, session_key[i]);
566 }
567
568 /*
569 * Encrypt the integer using the public key and host key of the
570 * server (key with smaller modulus first).
571 */
Damien Millerda755162002-01-22 23:09:22 +1100572 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000573 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +1100574 if (BN_num_bits(host_key->rsa->n) <
575 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
576 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100577 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100578 BN_num_bits(host_key->rsa->n),
579 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100580 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000581 }
Damien Millerda755162002-01-22 23:09:22 +1100582 rsa_public_encrypt(key, key, server_key->rsa);
583 rsa_public_encrypt(key, key, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000584 } else {
585 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +1100586 if (BN_num_bits(server_key->rsa->n) <
587 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
588 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100589 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100590 BN_num_bits(server_key->rsa->n),
591 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100592 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000593 }
Damien Millerda755162002-01-22 23:09:22 +1100594 rsa_public_encrypt(key, key, host_key->rsa);
595 rsa_public_encrypt(key, key, server_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000596 }
597
598 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +1100599 key_free(server_key);
600 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000601
Damien Millere39cacc2000-11-29 12:18:44 +1100602 if (options.cipher == SSH_CIPHER_NOT_SET) {
603 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
604 options.cipher = ssh_cipher_default;
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000605 } else if (options.cipher == SSH_CIPHER_INVALID ||
Damien Millere39cacc2000-11-29 12:18:44 +1100606 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +1000607 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +1100608 cipher_name(ssh_cipher_default));
609 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +1000610 }
611 /* Check that the selected cipher is supported. */
612 if (!(supported_ciphers & (1 << options.cipher)))
613 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100614 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +1000615
616 debug("Encryption type: %.100s", cipher_name(options.cipher));
617
618 /* Send the encrypted session key to the server. */
619 packet_start(SSH_CMSG_SESSION_KEY);
620 packet_put_char(options.cipher);
621
622 /* Send the cookie back to the server. */
623 for (i = 0; i < 8; i++)
624 packet_put_char(cookie[i]);
625
626 /* Send and destroy the encrypted encryption key integer. */
627 packet_put_bignum(key);
628 BN_clear_free(key);
629
630 /* Send protocol flags. */
631 packet_put_int(client_flags);
632
633 /* Send the packet now. */
634 packet_send();
635 packet_write_wait();
636
637 debug("Sent encrypted session key.");
638
639 /* Set the encryption key. */
640 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
641
642 /* We will no longer need the session key here. Destroy any extra copies. */
643 memset(session_key, 0, sizeof(session_key));
644
645 /*
646 * Expect a success message from the server. Note that this message
647 * will be received in encrypted form.
648 */
Damien Millerdff50992002-01-22 23:16:32 +1100649 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +1000650
651 debug("Received encrypted confirmation.");
652}
653
654/*
655 * Authenticate user
656 */
657void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000658ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000659 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +1000660{
661 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100662
Damien Millereba71ba2000-04-29 23:57:08 +1000663 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000664 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +1000665
666 /* Send the name of the user to log in as on the server. */
667 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000668 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000669 packet_send();
670 packet_write_wait();
671
672 /*
673 * The server should respond with success if no authentication is
674 * needed (the user has no password). Otherwise the server responds
675 * with failure.
676 */
Damien Millerdff50992002-01-22 23:16:32 +1100677 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000678
679 /* check whether the connection was accepted without authentication. */
680 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000681 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000682 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000683 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100684
Damien Millereba71ba2000-04-29 23:57:08 +1000685 /*
Damien Millereba71ba2000-04-29 23:57:08 +1000686 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
687 * authentication.
688 */
689 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000690 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000691 for (i = 0; i < sensitive->nkeys; i++) {
692 if (sensitive->keys[i] != NULL &&
693 sensitive->keys[i]->type == KEY_RSA1 &&
694 try_rhosts_rsa_authentication(local_user,
695 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000696 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000697 }
Damien Millereba71ba2000-04-29 23:57:08 +1000698 }
699 /* Try RSA authentication if the server supports it. */
700 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
701 options.rsa_authentication) {
702 /*
703 * Try RSA authentication using the authentication agent. The
704 * agent is tried first because no passphrase is needed for
705 * it, whereas identity files may require passphrases.
706 */
707 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000708 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000709
710 /* Try RSA authentication for each identity. */
711 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000712 if (options.identity_keys[i] != NULL &&
713 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +0000714 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000715 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000716 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000717 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +1000718 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +0000719 options.challenge_response_authentication && !options.batch_mode) {
720 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000721 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000722 }
723 /* Try password authentication if the server supports it. */
724 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
725 options.password_authentication && !options.batch_mode) {
726 char prompt[80];
727
Ben Lindstrome0557162001-02-11 00:00:24 +0000728 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +1000729 server_user, host);
730 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000731 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000732 }
733 /* All authentication methods have failed. Exit with an error message. */
734 fatal("Permission denied.");
735 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000736
737 success:
Damien Miller40eb1d82001-07-14 12:16:59 +1000738 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +1000739}