blob: dd12a3af2c7201fc8f055f929b67c40a3f6a4e7c [file] [log] [blame]
Damien Miller7acefbb2014-07-18 14:11:24 +10001/* $OpenBSD: sshconnect1.c,v 1.76 2014/07/15 15:54:14 millert 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 Millereba71ba2000-04-29 23:57:08 +100022
Damien Millerded319c2006-09-01 15:38:36 +100023#include <stdarg.h>
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 Miller7acefbb2014-07-18 14:11:24 +100041#include "misc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "readconf.h"
Damien Miller994cf142000-07-21 10:19:44 +100043#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "sshconnect.h"
45#include "authfile.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 Miller4a1c7aa2014-02-04 11:03:36 +110049#include "digest.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050
51/* Session id for the current session. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000052u_char session_id[16];
53u_int supported_authentications = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100054
55extern Options options;
56extern char *__progname;
57
58/*
59 * Checks if the user has an authentication agent, and if so, tries to
60 * authenticate using the agent.
61 */
Ben Lindstrombba81212001-06-25 05:01:22 +000062static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000063try_agent_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +100064{
Damien Millerad833b32000-08-23 10:46:23 +100065 int type;
Damien Millereba71ba2000-04-29 23:57:08 +100066 char *comment;
67 AuthenticationConnection *auth;
Ben Lindstrom46c16222000-12-22 01:43:59 +000068 u_char response[16];
69 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +100070 Key *key;
71 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +100072
73 /* Get connection to the agent. */
74 auth = ssh_get_authentication_connection();
75 if (!auth)
76 return 0;
77
Damien Millerda755162002-01-22 23:09:22 +110078 if ((challenge = BN_new()) == NULL)
79 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100080 /* Loop through identities served by the agent. */
Damien Millerad833b32000-08-23 10:46:23 +100081 for (key = ssh_get_first_identity(auth, &comment, 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +110082 key != NULL;
83 key = ssh_get_next_identity(auth, &comment, 1)) {
Damien Millereba71ba2000-04-29 23:57:08 +100084
85 /* Try this identity. */
86 debug("Trying RSA authentication via agent with '%.100s'", comment);
Darren Tuckera627d422013-06-02 07:31:17 +100087 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +100088
89 /* Tell the server that we are willing to authenticate using this key. */
90 packet_start(SSH_CMSG_AUTH_RSA);
Damien Millerad833b32000-08-23 10:46:23 +100091 packet_put_bignum(key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +100092 packet_send();
93 packet_write_wait();
94
95 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +110096 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +100097
Damien Miller788f2122005-11-05 15:14:59 +110098 /* The server sends failure if it doesn't like our key or
Damien Millereba71ba2000-04-29 23:57:08 +100099 does not support RSA authentication. */
100 if (type == SSH_SMSG_FAILURE) {
101 debug("Server refused our key.");
Damien Millerad833b32000-08-23 10:46:23 +1000102 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000103 continue;
104 }
105 /* Otherwise it should have sent a challenge. */
106 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
107 packet_disconnect("Protocol error during RSA authentication: %d",
108 type);
109
Damien Millerd432ccf2002-01-22 23:14:44 +1100110 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100111 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000112
113 debug("Received RSA challenge from server.");
114
115 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000116 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
117 /*
118 * The agent failed to authenticate this identifier
119 * although it advertised it supports this. Just
120 * return a wrong value.
121 */
Damien Miller996acd22003-04-09 20:59:48 +1000122 logit("Authentication agent failed to decrypt challenge.");
Damien Millera5103f42014-02-04 11:20:14 +1100123 explicit_bzero(response, sizeof(response));
Damien Millereba71ba2000-04-29 23:57:08 +1000124 }
Damien Millerad833b32000-08-23 10:46:23 +1000125 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000126 debug("Sending response to RSA challenge.");
127
128 /* Send the decrypted challenge back to the server. */
129 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
130 for (i = 0; i < 16; i++)
131 packet_put_char(response[i]);
132 packet_send();
133 packet_write_wait();
134
135 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100136 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000137
138 /* The server returns success if it accepted the authentication. */
139 if (type == SSH_SMSG_SUCCESS) {
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000140 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000141 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000142 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000143 return 1;
144 }
145 /* Otherwise it should return failure. */
146 if (type != SSH_SMSG_FAILURE)
147 packet_disconnect("Protocol error waiting RSA auth response: %d",
148 type);
149 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000150 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000151 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000152 debug("RSA authentication using agent refused.");
153 return 0;
154}
155
156/*
157 * Computes the proper response to a RSA challenge, and sends the response to
158 * the server.
159 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static void
Damien Millereba71ba2000-04-29 23:57:08 +1000161respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
162{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000163 u_char buf[32], response[16];
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100164 struct ssh_digest_ctx *md;
Damien Millereba71ba2000-04-29 23:57:08 +1000165 int i, len;
166
167 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100168 /* XXX think about Bleichenbacher, too */
Damien Miller86687062014-07-02 15:28:02 +1000169 if (rsa_private_decrypt(challenge, challenge, prv) != 0)
Damien Miller7650bc62001-01-30 09:27:26 +1100170 packet_disconnect(
171 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000172
173 /* Compute the response. */
174 /* The response is MD5 of decrypted challenge plus session id. */
175 len = BN_num_bytes(challenge);
Damien Millereccb9de2005-06-17 12:59:34 +1000176 if (len <= 0 || (u_int)len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100177 packet_disconnect(
178 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000179
180 memset(buf, 0, sizeof(buf));
181 BN_bn2bin(challenge, buf + sizeof(buf) - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100182 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
183 ssh_digest_update(md, buf, 32) < 0 ||
184 ssh_digest_update(md, session_id, 16) < 0 ||
185 ssh_digest_final(md, response, sizeof(response)) < 0)
186 fatal("%s: md5 failed", __func__);
187 ssh_digest_free(md);
Damien Millereba71ba2000-04-29 23:57:08 +1000188
189 debug("Sending response to host key RSA challenge.");
190
191 /* Send the response back to the server. */
192 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
193 for (i = 0; i < 16; i++)
194 packet_put_char(response[i]);
195 packet_send();
196 packet_write_wait();
197
Damien Millera5103f42014-02-04 11:20:14 +1100198 explicit_bzero(buf, sizeof(buf));
199 explicit_bzero(response, sizeof(response));
200 explicit_bzero(&md, sizeof(md));
Damien Millereba71ba2000-04-29 23:57:08 +1000201}
202
203/*
204 * Checks if the user has authentication file, and if so, tries to authenticate
205 * the user using it.
206 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000208try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000209{
210 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000211 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000212 char buf[300], *passphrase, *comment, *authfile;
Darren Tucker232b76f2006-05-06 17:41:51 +1000213 int i, perm_ok = 1, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000214
Ben Lindstromc5b68002001-07-04 04:52:03 +0000215 public = options.identity_keys[idx];
216 authfile = options.identity_files[idx];
217 comment = xstrdup(authfile);
218
Damien Millereba71ba2000-04-29 23:57:08 +1000219 debug("Trying RSA authentication with key '%.100s'", comment);
220
221 /* Tell the server that we are willing to authenticate using this key. */
222 packet_start(SSH_CMSG_AUTH_RSA);
223 packet_put_bignum(public->rsa->n);
224 packet_send();
225 packet_write_wait();
226
Damien Millereba71ba2000-04-29 23:57:08 +1000227 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100228 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000229
230 /*
Damien Miller788f2122005-11-05 15:14:59 +1100231 * The server responds with failure if it doesn't like our key or
232 * doesn't support RSA authentication.
Damien Millereba71ba2000-04-29 23:57:08 +1000233 */
234 if (type == SSH_SMSG_FAILURE) {
235 debug("Server refused our key.");
Darren Tuckera627d422013-06-02 07:31:17 +1000236 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000237 return 0;
238 }
239 /* Otherwise, the server should respond with a challenge. */
240 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
241 packet_disconnect("Protocol error during RSA authentication: %d", type);
242
243 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100244 if ((challenge = BN_new()) == NULL)
245 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100246 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100247 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000248
249 debug("Received RSA challenge from server.");
250
Damien Millereba71ba2000-04-29 23:57:08 +1000251 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000252 * If the key is not stored in external hardware, we have to
253 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000254 * fails, ask for a passphrase.
255 */
Damien Miller86687062014-07-02 15:28:02 +1000256 if (public->flags & SSHKEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000257 private = public;
258 else
Darren Tucker232b76f2006-05-06 17:41:51 +1000259 private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
260 &perm_ok);
261 if (private == NULL && !options.batch_mode && perm_ok) {
Ben Lindstrom05209452001-06-25 05:16:02 +0000262 snprintf(buf, sizeof(buf),
263 "Enter passphrase for RSA key '%.100s': ", comment);
264 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000265 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000266 if (strcmp(passphrase, "") != 0) {
267 private = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000268 authfile, passphrase, NULL, NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000269 quit = 0;
270 } else {
271 debug2("no passphrase given, try next key");
272 quit = 1;
273 }
Damien Millera5103f42014-02-04 11:20:14 +1100274 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +1000275 free(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000276 if (private != NULL || quit)
277 break;
278 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000279 }
Damien Millereba71ba2000-04-29 23:57:08 +1000280 }
281 /* We no longer need the comment. */
Darren Tuckera627d422013-06-02 07:31:17 +1000282 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000283
Ben Lindstrom05209452001-06-25 05:16:02 +0000284 if (private == NULL) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000285 if (!options.batch_mode && perm_ok)
Ben Lindstrom05209452001-06-25 05:16:02 +0000286 error("Bad passphrase.");
287
288 /* Send a dummy response packet to avoid protocol error. */
289 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
290 for (i = 0; i < 16; i++)
291 packet_put_char(0);
292 packet_send();
293 packet_write_wait();
294
295 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100296 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000297 BN_clear_free(challenge);
298 return 0;
299 }
300
Damien Millereba71ba2000-04-29 23:57:08 +1000301 /* Compute and send a response to the challenge. */
302 respond_to_rsa_challenge(challenge, private->rsa);
303
Ben Lindstromc5b68002001-07-04 04:52:03 +0000304 /* Destroy the private key unless it in external hardware. */
Damien Miller86687062014-07-02 15:28:02 +1000305 if (!(private->flags & SSHKEY_FLAG_EXT))
Ben Lindstromc5b68002001-07-04 04:52:03 +0000306 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000307
308 /* We no longer need the challenge. */
309 BN_clear_free(challenge);
310
311 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100312 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000313 if (type == SSH_SMSG_SUCCESS) {
314 debug("RSA authentication accepted by server.");
315 return 1;
316 }
317 if (type != SSH_SMSG_FAILURE)
318 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
319 debug("RSA authentication refused.");
320 return 0;
321}
322
323/*
324 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
325 * authentication and RSA host authentication.
326 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000327static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000328try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000329{
330 int type;
331 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000332
333 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
334
335 /* Tell the server that we are willing to authenticate using this key. */
336 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000337 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000338 packet_put_int(BN_num_bits(host_key->rsa->n));
339 packet_put_bignum(host_key->rsa->e);
340 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000341 packet_send();
342 packet_write_wait();
343
344 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100345 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000346
347 /* The server responds with failure if it doesn't admit our
348 .rhosts authentication or doesn't know our host key. */
349 if (type == SSH_SMSG_FAILURE) {
350 debug("Server refused our rhosts authentication or host key.");
351 return 0;
352 }
353 /* Otherwise, the server should respond with a challenge. */
354 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
355 packet_disconnect("Protocol error during RSA authentication: %d", type);
356
357 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100358 if ((challenge = BN_new()) == NULL)
359 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100360 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100361 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000362
363 debug("Received RSA challenge for host key from server.");
364
365 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000366 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000367
368 /* We no longer need the challenge. */
369 BN_clear_free(challenge);
370
371 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100372 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000373 if (type == SSH_SMSG_SUCCESS) {
374 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
375 return 1;
376 }
377 if (type != SSH_SMSG_FAILURE)
378 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
379 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
380 return 0;
381}
382
Damien Millereba71ba2000-04-29 23:57:08 +1000383/*
384 * Tries to authenticate with any string-based challenge/response system.
385 * Note that the client code is not tied to s/key or TIS.
386 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000387static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000388try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000389{
390 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000391 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000392 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000393 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000394
395 debug("Doing challenge response authentication.");
396
Damien Millereba71ba2000-04-29 23:57:08 +1000397 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000398 /* request a challenge */
399 packet_start(SSH_CMSG_AUTH_TIS);
400 packet_send();
401 packet_write_wait();
402
Damien Millerdff50992002-01-22 23:16:32 +1100403 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000404 if (type != SSH_SMSG_FAILURE &&
405 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
406 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000407 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000408 }
409 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000410 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000411 return 0;
412 }
413 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100414 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000415 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100416 strchr(challenge, '\n') ? "" : "\nResponse: ");
Darren Tuckera627d422013-06-02 07:31:17 +1000417 free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000418 if (i != 0)
419 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000420 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000421 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000422 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000423 response = read_passphrase(prompt, 0);
424 if (strcmp(response, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000425 free(response);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000426 break;
427 }
Damien Millereba71ba2000-04-29 23:57:08 +1000428 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100429 ssh_put_password(response);
Damien Millera5103f42014-02-04 11:20:14 +1100430 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +1000431 free(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000432 packet_send();
433 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100434 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000435 if (type == SSH_SMSG_SUCCESS)
436 return 1;
437 if (type != SSH_SMSG_FAILURE)
438 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000439 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000440 }
441 /* failure */
442 return 0;
443}
444
445/*
446 * Tries to authenticate with plain passwd authentication.
447 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000448static int
Damien Millereba71ba2000-04-29 23:57:08 +1000449try_password_authentication(char *prompt)
450{
Damien Millerdff50992002-01-22 23:16:32 +1100451 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000452 char *password;
453
454 debug("Doing password authentication.");
455 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000456 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000457 for (i = 0; i < options.number_of_password_prompts; i++) {
458 if (i != 0)
459 error("Permission denied, please try again.");
460 password = read_passphrase(prompt, 0);
461 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100462 ssh_put_password(password);
Damien Millera5103f42014-02-04 11:20:14 +1100463 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000464 free(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000465 packet_send();
466 packet_write_wait();
467
Damien Millerdff50992002-01-22 23:16:32 +1100468 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000469 if (type == SSH_SMSG_SUCCESS)
470 return 1;
471 if (type != SSH_SMSG_FAILURE)
472 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
473 }
474 /* failure */
475 return 0;
476}
477
478/*
479 * SSH1 key exchange
480 */
481void
482ssh_kex(char *host, struct sockaddr *hostaddr)
483{
484 int i;
485 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100486 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000487 int bits, rbits;
488 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000489 u_char session_key[SSH_SESSION_KEY_LENGTH];
490 u_char cookie[8];
491 u_int supported_ciphers;
492 u_int server_flags, client_flags;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000493 u_int32_t rnd = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000494
495 debug("Waiting for server public key.");
496
497 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100498 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000499
500 /* Get cookie from the packet. */
501 for (i = 0; i < 8; i++)
502 cookie[i] = packet_get_char();
503
504 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100505 server_key = key_new(KEY_RSA1);
506 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100507 packet_get_bignum(server_key->rsa->e);
508 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000509
Damien Millerda755162002-01-22 23:09:22 +1100510 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000511 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000512 logit("Warning: Server lies about size of server public key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000513 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000514 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000515 }
516 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100517 host_key = key_new(KEY_RSA1);
518 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100519 packet_get_bignum(host_key->rsa->e);
520 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000521
Damien Millerda755162002-01-22 23:09:22 +1100522 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000523 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000524 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000525 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000526 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000527 }
528
529 /* Get protocol flags. */
530 server_flags = packet_get_int();
531 packet_set_protocol_flags(server_flags);
532
533 supported_ciphers = packet_get_int();
534 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100535 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000536
537 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100538 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000539
Damien Millerda755162002-01-22 23:09:22 +1100540 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000541 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000542
543 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
544
Darren Tuckere14e0052004-05-13 16:30:44 +1000545 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
Damien Millereba71ba2000-04-29 23:57:08 +1000546
Damien Millereba71ba2000-04-29 23:57:08 +1000547 /*
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)
Darren Tucker0bc85572006-11-07 23:14:41 +1100565 fatal("ssh_kex: BN_new failed");
566 if (BN_set_word(key, 0) == 0)
567 fatal("ssh_kex: BN_set_word failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000568 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
Darren Tucker0bc85572006-11-07 23:14:41 +1100569 if (BN_lshift(key, key, 8) == 0)
570 fatal("ssh_kex: BN_lshift failed");
571 if (i < 16) {
572 if (BN_add_word(key, session_key[i] ^ session_id[i])
573 == 0)
574 fatal("ssh_kex: BN_add_word failed");
575 } else {
576 if (BN_add_word(key, session_key[i]) == 0)
577 fatal("ssh_kex: BN_add_word failed");
578 }
Damien Millereba71ba2000-04-29 23:57:08 +1000579 }
580
581 /*
582 * Encrypt the integer using the public key and host key of the
583 * server (key with smaller modulus first).
584 */
Damien Millerda755162002-01-22 23:09:22 +1100585 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000586 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +1100587 if (BN_num_bits(host_key->rsa->n) <
588 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
589 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100590 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100591 BN_num_bits(host_key->rsa->n),
592 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100593 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000594 }
Damien Miller86687062014-07-02 15:28:02 +1000595 if (rsa_public_encrypt(key, key, server_key->rsa) != 0 ||
596 rsa_public_encrypt(key, key, host_key->rsa) != 0)
597 fatal("%s: rsa_public_encrypt failed", __func__);
Damien Millereba71ba2000-04-29 23:57:08 +1000598 } else {
599 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +1100600 if (BN_num_bits(server_key->rsa->n) <
601 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
602 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100603 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100604 BN_num_bits(server_key->rsa->n),
605 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100606 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000607 }
Damien Miller86687062014-07-02 15:28:02 +1000608 if (rsa_public_encrypt(key, key, host_key->rsa) != 0 ||
609 rsa_public_encrypt(key, key, server_key->rsa) != 0)
610 fatal("%s: rsa_public_encrypt failed", __func__);
Damien Millereba71ba2000-04-29 23:57:08 +1000611 }
612
613 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +1100614 key_free(server_key);
615 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000616
Damien Millere39cacc2000-11-29 12:18:44 +1100617 if (options.cipher == SSH_CIPHER_NOT_SET) {
618 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
619 options.cipher = ssh_cipher_default;
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000620 } else if (options.cipher == SSH_CIPHER_INVALID ||
Damien Millere39cacc2000-11-29 12:18:44 +1100621 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +1000622 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +1100623 cipher_name(ssh_cipher_default));
624 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +1000625 }
626 /* Check that the selected cipher is supported. */
627 if (!(supported_ciphers & (1 << options.cipher)))
628 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100629 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +1000630
631 debug("Encryption type: %.100s", cipher_name(options.cipher));
632
633 /* Send the encrypted session key to the server. */
634 packet_start(SSH_CMSG_SESSION_KEY);
635 packet_put_char(options.cipher);
636
637 /* Send the cookie back to the server. */
638 for (i = 0; i < 8; i++)
639 packet_put_char(cookie[i]);
640
641 /* Send and destroy the encrypted encryption key integer. */
642 packet_put_bignum(key);
643 BN_clear_free(key);
644
645 /* Send protocol flags. */
646 packet_put_int(client_flags);
647
648 /* Send the packet now. */
649 packet_send();
650 packet_write_wait();
651
652 debug("Sent encrypted session key.");
653
654 /* Set the encryption key. */
655 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
656
Damien Millera5103f42014-02-04 11:20:14 +1100657 /*
658 * We will no longer need the session key here.
659 * Destroy any extra copies.
660 */
661 explicit_bzero(session_key, sizeof(session_key));
Damien Millereba71ba2000-04-29 23:57:08 +1000662
663 /*
664 * Expect a success message from the server. Note that this message
665 * will be received in encrypted form.
666 */
Damien Millerdff50992002-01-22 23:16:32 +1100667 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +1000668
669 debug("Received encrypted confirmation.");
670}
671
672/*
673 * Authenticate user
674 */
675void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000676ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000677 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +1000678{
679 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100680
Damien Millereba71ba2000-04-29 23:57:08 +1000681 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000682 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +1000683
684 /* Send the name of the user to log in as on the server. */
685 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000686 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000687 packet_send();
688 packet_write_wait();
689
690 /*
691 * The server should respond with success if no authentication is
692 * needed (the user has no password). Otherwise the server responds
693 * with failure.
694 */
Damien Millerdff50992002-01-22 23:16:32 +1100695 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000696
697 /* check whether the connection was accepted without authentication. */
698 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000699 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000700 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000701 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100702
Damien Millereba71ba2000-04-29 23:57:08 +1000703 /*
Damien Millereba71ba2000-04-29 23:57:08 +1000704 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
705 * authentication.
706 */
707 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000708 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000709 for (i = 0; i < sensitive->nkeys; i++) {
710 if (sensitive->keys[i] != NULL &&
711 sensitive->keys[i]->type == KEY_RSA1 &&
712 try_rhosts_rsa_authentication(local_user,
713 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000714 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000715 }
Damien Millereba71ba2000-04-29 23:57:08 +1000716 }
717 /* Try RSA authentication if the server supports it. */
718 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
719 options.rsa_authentication) {
720 /*
721 * Try RSA authentication using the authentication agent. The
722 * agent is tried first because no passphrase is needed for
723 * it, whereas identity files may require passphrases.
724 */
725 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000726 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000727
728 /* Try RSA authentication for each identity. */
729 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000730 if (options.identity_keys[i] != NULL &&
731 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +0000732 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000733 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000734 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000735 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +1000736 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +0000737 options.challenge_response_authentication && !options.batch_mode) {
738 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000739 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000740 }
741 /* Try password authentication if the server supports it. */
742 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
743 options.password_authentication && !options.batch_mode) {
744 char prompt[80];
745
Ben Lindstrome0557162001-02-11 00:00:24 +0000746 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +1000747 server_user, host);
748 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000749 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000750 }
751 /* All authentication methods have failed. Exit with an error message. */
752 fatal("Permission denied.");
753 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000754
755 success:
Damien Miller40eb1d82001-07-14 12:16:59 +1000756 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +1000757}