blob: 08589f5ee5347a7d9c14345afb1ace0dd19ba73b [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 Miller76c04802015-01-13 19:38:18 +110018#ifdef WITH_SSH1
19
Damien Millerd7834352006-08-05 12:39:39 +100020#include <sys/types.h>
21#include <sys/socket.h>
22
Damien Millereba71ba2000-04-29 23:57:08 +100023#include <openssl/bn.h>
Damien Millereba71ba2000-04-29 23:57:08 +100024
Damien Millerded319c2006-09-01 15:38:36 +100025#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100026#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100027#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100028#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100029#include <signal.h>
30#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100031
Damien Millerd7834352006-08-05 12:39:39 +100032#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#include "ssh.h"
34#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100035#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100036#include "buffer.h"
37#include "packet.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "key.h"
39#include "cipher.h"
Darren Tuckere14e0052004-05-13 16:30:44 +100040#include "kex.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100043#include "misc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "readconf.h"
Damien Miller994cf142000-07-21 10:19:44 +100045#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "sshconnect.h"
47#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "hostfile.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000050#include "auth.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110051#include "digest.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052
53/* Session id for the current session. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000054u_char session_id[16];
55u_int supported_authentications = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100056
57extern Options options;
58extern char *__progname;
59
60/*
61 * Checks if the user has an authentication agent, and if so, tries to
62 * authenticate using the agent.
63 */
Ben Lindstrombba81212001-06-25 05:01:22 +000064static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000065try_agent_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +100066{
Damien Millerad833b32000-08-23 10:46:23 +100067 int type;
Damien Millereba71ba2000-04-29 23:57:08 +100068 char *comment;
69 AuthenticationConnection *auth;
Ben Lindstrom46c16222000-12-22 01:43:59 +000070 u_char response[16];
71 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +100072 Key *key;
73 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +100074
75 /* Get connection to the agent. */
76 auth = ssh_get_authentication_connection();
77 if (!auth)
78 return 0;
79
Damien Millerda755162002-01-22 23:09:22 +110080 if ((challenge = BN_new()) == NULL)
81 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100082 /* Loop through identities served by the agent. */
Damien Millerad833b32000-08-23 10:46:23 +100083 for (key = ssh_get_first_identity(auth, &comment, 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +110084 key != NULL;
85 key = ssh_get_next_identity(auth, &comment, 1)) {
Damien Millereba71ba2000-04-29 23:57:08 +100086
87 /* Try this identity. */
88 debug("Trying RSA authentication via agent with '%.100s'", comment);
Darren Tuckera627d422013-06-02 07:31:17 +100089 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +100090
91 /* Tell the server that we are willing to authenticate using this key. */
92 packet_start(SSH_CMSG_AUTH_RSA);
Damien Millerad833b32000-08-23 10:46:23 +100093 packet_put_bignum(key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +100094 packet_send();
95 packet_write_wait();
96
97 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +110098 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +100099
Damien Miller788f2122005-11-05 15:14:59 +1100100 /* The server sends failure if it doesn't like our key or
Damien Millereba71ba2000-04-29 23:57:08 +1000101 does not support RSA authentication. */
102 if (type == SSH_SMSG_FAILURE) {
103 debug("Server refused our key.");
Damien Millerad833b32000-08-23 10:46:23 +1000104 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000105 continue;
106 }
107 /* Otherwise it should have sent a challenge. */
108 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
109 packet_disconnect("Protocol error during RSA authentication: %d",
110 type);
111
Damien Millerd432ccf2002-01-22 23:14:44 +1100112 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100113 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000114
115 debug("Received RSA challenge from server.");
116
117 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000118 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
119 /*
120 * The agent failed to authenticate this identifier
121 * although it advertised it supports this. Just
122 * return a wrong value.
123 */
Damien Miller996acd22003-04-09 20:59:48 +1000124 logit("Authentication agent failed to decrypt challenge.");
Damien Millera5103f42014-02-04 11:20:14 +1100125 explicit_bzero(response, sizeof(response));
Damien Millereba71ba2000-04-29 23:57:08 +1000126 }
Damien Millerad833b32000-08-23 10:46:23 +1000127 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000128 debug("Sending response to RSA challenge.");
129
130 /* Send the decrypted challenge back to the server. */
131 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
132 for (i = 0; i < 16; i++)
133 packet_put_char(response[i]);
134 packet_send();
135 packet_write_wait();
136
137 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100138 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000139
140 /* The server returns success if it accepted the authentication. */
141 if (type == SSH_SMSG_SUCCESS) {
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 Millerad833b32000-08-23 10:46:23 +1000144 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000145 return 1;
146 }
147 /* Otherwise it should return failure. */
148 if (type != SSH_SMSG_FAILURE)
149 packet_disconnect("Protocol error waiting RSA auth response: %d",
150 type);
151 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000152 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000153 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000154 debug("RSA authentication using agent refused.");
155 return 0;
156}
157
158/*
159 * Computes the proper response to a RSA challenge, and sends the response to
160 * the server.
161 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void
Damien Millereba71ba2000-04-29 23:57:08 +1000163respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
164{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000165 u_char buf[32], response[16];
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100166 struct ssh_digest_ctx *md;
Damien Millereba71ba2000-04-29 23:57:08 +1000167 int i, len;
168
169 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100170 /* XXX think about Bleichenbacher, too */
Damien Miller86687062014-07-02 15:28:02 +1000171 if (rsa_private_decrypt(challenge, challenge, prv) != 0)
Damien Miller7650bc62001-01-30 09:27:26 +1100172 packet_disconnect(
173 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000174
175 /* Compute the response. */
176 /* The response is MD5 of decrypted challenge plus session id. */
177 len = BN_num_bytes(challenge);
Damien Millereccb9de2005-06-17 12:59:34 +1000178 if (len <= 0 || (u_int)len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100179 packet_disconnect(
180 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000181
182 memset(buf, 0, sizeof(buf));
183 BN_bn2bin(challenge, buf + sizeof(buf) - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100184 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
185 ssh_digest_update(md, buf, 32) < 0 ||
186 ssh_digest_update(md, session_id, 16) < 0 ||
187 ssh_digest_final(md, response, sizeof(response)) < 0)
188 fatal("%s: md5 failed", __func__);
189 ssh_digest_free(md);
Damien Millereba71ba2000-04-29 23:57:08 +1000190
191 debug("Sending response to host key RSA challenge.");
192
193 /* Send the response back to the server. */
194 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
195 for (i = 0; i < 16; i++)
196 packet_put_char(response[i]);
197 packet_send();
198 packet_write_wait();
199
Damien Millera5103f42014-02-04 11:20:14 +1100200 explicit_bzero(buf, sizeof(buf));
201 explicit_bzero(response, sizeof(response));
202 explicit_bzero(&md, sizeof(md));
Damien Millereba71ba2000-04-29 23:57:08 +1000203}
204
205/*
206 * Checks if the user has authentication file, and if so, tries to authenticate
207 * the user using it.
208 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000210try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000211{
212 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000213 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000214 char buf[300], *passphrase, *comment, *authfile;
Darren Tucker232b76f2006-05-06 17:41:51 +1000215 int i, perm_ok = 1, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000216
Ben Lindstromc5b68002001-07-04 04:52:03 +0000217 public = options.identity_keys[idx];
218 authfile = options.identity_files[idx];
219 comment = xstrdup(authfile);
220
Damien Millereba71ba2000-04-29 23:57:08 +1000221 debug("Trying RSA authentication with key '%.100s'", comment);
222
223 /* Tell the server that we are willing to authenticate using this key. */
224 packet_start(SSH_CMSG_AUTH_RSA);
225 packet_put_bignum(public->rsa->n);
226 packet_send();
227 packet_write_wait();
228
Damien Millereba71ba2000-04-29 23:57:08 +1000229 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100230 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000231
232 /*
Damien Miller788f2122005-11-05 15:14:59 +1100233 * The server responds with failure if it doesn't like our key or
234 * doesn't support RSA authentication.
Damien Millereba71ba2000-04-29 23:57:08 +1000235 */
236 if (type == SSH_SMSG_FAILURE) {
237 debug("Server refused our key.");
Darren Tuckera627d422013-06-02 07:31:17 +1000238 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000239 return 0;
240 }
241 /* Otherwise, the server should respond with a challenge. */
242 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
243 packet_disconnect("Protocol error during RSA authentication: %d", type);
244
245 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100246 if ((challenge = BN_new()) == NULL)
247 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100248 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100249 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000250
251 debug("Received RSA challenge from server.");
252
Damien Millereba71ba2000-04-29 23:57:08 +1000253 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000254 * If the key is not stored in external hardware, we have to
255 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000256 * fails, ask for a passphrase.
257 */
Damien Miller86687062014-07-02 15:28:02 +1000258 if (public->flags & SSHKEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000259 private = public;
260 else
Darren Tucker232b76f2006-05-06 17:41:51 +1000261 private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
262 &perm_ok);
263 if (private == NULL && !options.batch_mode && perm_ok) {
Ben Lindstrom05209452001-06-25 05:16:02 +0000264 snprintf(buf, sizeof(buf),
265 "Enter passphrase for RSA key '%.100s': ", comment);
266 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000267 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000268 if (strcmp(passphrase, "") != 0) {
269 private = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000270 authfile, passphrase, NULL, NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000271 quit = 0;
272 } else {
273 debug2("no passphrase given, try next key");
274 quit = 1;
275 }
Damien Millera5103f42014-02-04 11:20:14 +1100276 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +1000277 free(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000278 if (private != NULL || quit)
279 break;
280 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000281 }
Damien Millereba71ba2000-04-29 23:57:08 +1000282 }
283 /* We no longer need the comment. */
Darren Tuckera627d422013-06-02 07:31:17 +1000284 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000285
Ben Lindstrom05209452001-06-25 05:16:02 +0000286 if (private == NULL) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000287 if (!options.batch_mode && perm_ok)
Ben Lindstrom05209452001-06-25 05:16:02 +0000288 error("Bad passphrase.");
289
290 /* Send a dummy response packet to avoid protocol error. */
291 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
292 for (i = 0; i < 16; i++)
293 packet_put_char(0);
294 packet_send();
295 packet_write_wait();
296
297 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100298 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000299 BN_clear_free(challenge);
300 return 0;
301 }
302
Damien Millereba71ba2000-04-29 23:57:08 +1000303 /* Compute and send a response to the challenge. */
304 respond_to_rsa_challenge(challenge, private->rsa);
305
Ben Lindstromc5b68002001-07-04 04:52:03 +0000306 /* Destroy the private key unless it in external hardware. */
Damien Miller86687062014-07-02 15:28:02 +1000307 if (!(private->flags & SSHKEY_FLAG_EXT))
Ben Lindstromc5b68002001-07-04 04:52:03 +0000308 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000309
310 /* We no longer need the challenge. */
311 BN_clear_free(challenge);
312
313 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100314 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000315 if (type == SSH_SMSG_SUCCESS) {
316 debug("RSA authentication accepted by server.");
317 return 1;
318 }
319 if (type != SSH_SMSG_FAILURE)
320 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
321 debug("RSA authentication refused.");
322 return 0;
323}
324
325/*
326 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
327 * authentication and RSA host authentication.
328 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000329static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000330try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000331{
332 int type;
333 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000334
335 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
336
337 /* Tell the server that we are willing to authenticate using this key. */
338 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000339 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000340 packet_put_int(BN_num_bits(host_key->rsa->n));
341 packet_put_bignum(host_key->rsa->e);
342 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000343 packet_send();
344 packet_write_wait();
345
346 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100347 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000348
349 /* The server responds with failure if it doesn't admit our
350 .rhosts authentication or doesn't know our host key. */
351 if (type == SSH_SMSG_FAILURE) {
352 debug("Server refused our rhosts authentication or host key.");
353 return 0;
354 }
355 /* Otherwise, the server should respond with a challenge. */
356 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
357 packet_disconnect("Protocol error during RSA authentication: %d", type);
358
359 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100360 if ((challenge = BN_new()) == NULL)
361 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100362 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100363 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000364
365 debug("Received RSA challenge for host key from server.");
366
367 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000368 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000369
370 /* We no longer need the challenge. */
371 BN_clear_free(challenge);
372
373 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100374 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000375 if (type == SSH_SMSG_SUCCESS) {
376 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
377 return 1;
378 }
379 if (type != SSH_SMSG_FAILURE)
380 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
381 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
382 return 0;
383}
384
Damien Millereba71ba2000-04-29 23:57:08 +1000385/*
386 * Tries to authenticate with any string-based challenge/response system.
387 * Note that the client code is not tied to s/key or TIS.
388 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000389static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000390try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000391{
392 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000393 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000394 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000395 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000396
397 debug("Doing challenge response authentication.");
398
Damien Millereba71ba2000-04-29 23:57:08 +1000399 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000400 /* request a challenge */
401 packet_start(SSH_CMSG_AUTH_TIS);
402 packet_send();
403 packet_write_wait();
404
Damien Millerdff50992002-01-22 23:16:32 +1100405 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000406 if (type != SSH_SMSG_FAILURE &&
407 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
408 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000409 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000410 }
411 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000412 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000413 return 0;
414 }
415 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100416 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000417 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100418 strchr(challenge, '\n') ? "" : "\nResponse: ");
Darren Tuckera627d422013-06-02 07:31:17 +1000419 free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000420 if (i != 0)
421 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000422 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000423 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000424 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000425 response = read_passphrase(prompt, 0);
426 if (strcmp(response, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000427 free(response);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000428 break;
429 }
Damien Millereba71ba2000-04-29 23:57:08 +1000430 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100431 ssh_put_password(response);
Damien Millera5103f42014-02-04 11:20:14 +1100432 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +1000433 free(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000434 packet_send();
435 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100436 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000437 if (type == SSH_SMSG_SUCCESS)
438 return 1;
439 if (type != SSH_SMSG_FAILURE)
440 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000441 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000442 }
443 /* failure */
444 return 0;
445}
446
447/*
448 * Tries to authenticate with plain passwd authentication.
449 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000450static int
Damien Millereba71ba2000-04-29 23:57:08 +1000451try_password_authentication(char *prompt)
452{
Damien Millerdff50992002-01-22 23:16:32 +1100453 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000454 char *password;
455
456 debug("Doing password authentication.");
457 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000458 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000459 for (i = 0; i < options.number_of_password_prompts; i++) {
460 if (i != 0)
461 error("Permission denied, please try again.");
462 password = read_passphrase(prompt, 0);
463 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100464 ssh_put_password(password);
Damien Millera5103f42014-02-04 11:20:14 +1100465 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000466 free(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000467 packet_send();
468 packet_write_wait();
469
Damien Millerdff50992002-01-22 23:16:32 +1100470 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000471 if (type == SSH_SMSG_SUCCESS)
472 return 1;
473 if (type != SSH_SMSG_FAILURE)
474 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
475 }
476 /* failure */
477 return 0;
478}
479
480/*
481 * SSH1 key exchange
482 */
483void
484ssh_kex(char *host, struct sockaddr *hostaddr)
485{
486 int i;
487 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100488 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000489 int bits, rbits;
490 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000491 u_char session_key[SSH_SESSION_KEY_LENGTH];
492 u_char cookie[8];
493 u_int supported_ciphers;
494 u_int server_flags, client_flags;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000495 u_int32_t rnd = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000496
497 debug("Waiting for server public key.");
498
499 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100500 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000501
502 /* Get cookie from the packet. */
503 for (i = 0; i < 8; i++)
504 cookie[i] = packet_get_char();
505
506 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100507 server_key = key_new(KEY_RSA1);
508 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100509 packet_get_bignum(server_key->rsa->e);
510 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000511
Damien Millerda755162002-01-22 23:09:22 +1100512 rbits = BN_num_bits(server_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 public 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 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100519 host_key = key_new(KEY_RSA1);
520 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100521 packet_get_bignum(host_key->rsa->e);
522 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000523
Damien Millerda755162002-01-22 23:09:22 +1100524 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000525 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000526 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000527 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000528 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000529 }
530
531 /* Get protocol flags. */
532 server_flags = packet_get_int();
533 packet_set_protocol_flags(server_flags);
534
535 supported_ciphers = packet_get_int();
536 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100537 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000538
539 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100540 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000541
Damien Millerda755162002-01-22 23:09:22 +1100542 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000543 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000544
545 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
546
Darren Tuckere14e0052004-05-13 16:30:44 +1000547 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
Damien Millereba71ba2000-04-29 23:57:08 +1000548
Damien Millereba71ba2000-04-29 23:57:08 +1000549 /*
550 * Generate an encryption key for the session. The key is a 256 bit
551 * random number, interpreted as a 32-byte key, with the least
552 * significant 8 bits being the first byte of the key.
553 */
554 for (i = 0; i < 32; i++) {
555 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000556 rnd = arc4random();
557 session_key[i] = rnd & 0xff;
558 rnd >>= 8;
Damien Millereba71ba2000-04-29 23:57:08 +1000559 }
560
561 /*
562 * According to the protocol spec, the first byte of the session key
563 * is the highest byte of the integer. The session key is xored with
564 * the first 16 bytes of the session id.
565 */
Damien Millerda755162002-01-22 23:09:22 +1100566 if ((key = BN_new()) == NULL)
Darren Tucker0bc85572006-11-07 23:14:41 +1100567 fatal("ssh_kex: BN_new failed");
568 if (BN_set_word(key, 0) == 0)
569 fatal("ssh_kex: BN_set_word failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000570 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
Darren Tucker0bc85572006-11-07 23:14:41 +1100571 if (BN_lshift(key, key, 8) == 0)
572 fatal("ssh_kex: BN_lshift failed");
573 if (i < 16) {
574 if (BN_add_word(key, session_key[i] ^ session_id[i])
575 == 0)
576 fatal("ssh_kex: BN_add_word failed");
577 } else {
578 if (BN_add_word(key, session_key[i]) == 0)
579 fatal("ssh_kex: BN_add_word failed");
580 }
Damien Millereba71ba2000-04-29 23:57:08 +1000581 }
582
583 /*
584 * Encrypt the integer using the public key and host key of the
585 * server (key with smaller modulus first).
586 */
Damien Millerda755162002-01-22 23:09:22 +1100587 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000588 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +1100589 if (BN_num_bits(host_key->rsa->n) <
590 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
591 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100592 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100593 BN_num_bits(host_key->rsa->n),
594 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100595 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000596 }
Damien Miller86687062014-07-02 15:28:02 +1000597 if (rsa_public_encrypt(key, key, server_key->rsa) != 0 ||
598 rsa_public_encrypt(key, key, host_key->rsa) != 0)
599 fatal("%s: rsa_public_encrypt failed", __func__);
Damien Millereba71ba2000-04-29 23:57:08 +1000600 } else {
601 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +1100602 if (BN_num_bits(server_key->rsa->n) <
603 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
604 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100605 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100606 BN_num_bits(server_key->rsa->n),
607 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100608 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000609 }
Damien Miller86687062014-07-02 15:28:02 +1000610 if (rsa_public_encrypt(key, key, host_key->rsa) != 0 ||
611 rsa_public_encrypt(key, key, server_key->rsa) != 0)
612 fatal("%s: rsa_public_encrypt failed", __func__);
Damien Millereba71ba2000-04-29 23:57:08 +1000613 }
614
615 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +1100616 key_free(server_key);
617 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000618
Damien Millere39cacc2000-11-29 12:18:44 +1100619 if (options.cipher == SSH_CIPHER_NOT_SET) {
620 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
621 options.cipher = ssh_cipher_default;
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000622 } else if (options.cipher == SSH_CIPHER_INVALID ||
Damien Millere39cacc2000-11-29 12:18:44 +1100623 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +1000624 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +1100625 cipher_name(ssh_cipher_default));
626 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +1000627 }
628 /* Check that the selected cipher is supported. */
629 if (!(supported_ciphers & (1 << options.cipher)))
630 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100631 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +1000632
633 debug("Encryption type: %.100s", cipher_name(options.cipher));
634
635 /* Send the encrypted session key to the server. */
636 packet_start(SSH_CMSG_SESSION_KEY);
637 packet_put_char(options.cipher);
638
639 /* Send the cookie back to the server. */
640 for (i = 0; i < 8; i++)
641 packet_put_char(cookie[i]);
642
643 /* Send and destroy the encrypted encryption key integer. */
644 packet_put_bignum(key);
645 BN_clear_free(key);
646
647 /* Send protocol flags. */
648 packet_put_int(client_flags);
649
650 /* Send the packet now. */
651 packet_send();
652 packet_write_wait();
653
654 debug("Sent encrypted session key.");
655
656 /* Set the encryption key. */
657 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
658
Damien Millera5103f42014-02-04 11:20:14 +1100659 /*
660 * We will no longer need the session key here.
661 * Destroy any extra copies.
662 */
663 explicit_bzero(session_key, sizeof(session_key));
Damien Millereba71ba2000-04-29 23:57:08 +1000664
665 /*
666 * Expect a success message from the server. Note that this message
667 * will be received in encrypted form.
668 */
Damien Millerdff50992002-01-22 23:16:32 +1100669 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +1000670
671 debug("Received encrypted confirmation.");
672}
673
674/*
675 * Authenticate user
676 */
677void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000678ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000679 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +1000680{
681 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100682
Damien Millereba71ba2000-04-29 23:57:08 +1000683 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000684 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +1000685
686 /* Send the name of the user to log in as on the server. */
687 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000688 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000689 packet_send();
690 packet_write_wait();
691
692 /*
693 * The server should respond with success if no authentication is
694 * needed (the user has no password). Otherwise the server responds
695 * with failure.
696 */
Damien Millerdff50992002-01-22 23:16:32 +1100697 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000698
699 /* check whether the connection was accepted without authentication. */
700 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000701 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000702 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000703 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100704
Damien Millereba71ba2000-04-29 23:57:08 +1000705 /*
Damien Millereba71ba2000-04-29 23:57:08 +1000706 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
707 * authentication.
708 */
709 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000710 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000711 for (i = 0; i < sensitive->nkeys; i++) {
712 if (sensitive->keys[i] != NULL &&
713 sensitive->keys[i]->type == KEY_RSA1 &&
714 try_rhosts_rsa_authentication(local_user,
715 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000716 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000717 }
Damien Millereba71ba2000-04-29 23:57:08 +1000718 }
719 /* Try RSA authentication if the server supports it. */
720 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
721 options.rsa_authentication) {
722 /*
723 * Try RSA authentication using the authentication agent. The
724 * agent is tried first because no passphrase is needed for
725 * it, whereas identity files may require passphrases.
726 */
727 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000728 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000729
730 /* Try RSA authentication for each identity. */
731 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000732 if (options.identity_keys[i] != NULL &&
733 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +0000734 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000735 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000736 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000737 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +1000738 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +0000739 options.challenge_response_authentication && !options.batch_mode) {
740 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000741 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000742 }
743 /* Try password authentication if the server supports it. */
744 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
745 options.password_authentication && !options.batch_mode) {
746 char prompt[80];
747
Ben Lindstrome0557162001-02-11 00:00:24 +0000748 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +1000749 server_user, host);
750 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000751 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000752 }
753 /* All authentication methods have failed. Exit with an error message. */
754 fatal("Permission denied.");
755 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000756
757 success:
Damien Miller40eb1d82001-07-14 12:16:59 +1000758 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +1000759}
Damien Miller76c04802015-01-13 19:38:18 +1100760
761#endif /* WITH_SSH1 */