blob: fd07bbf7418a15b7c87d1816c811cdc7829acd88 [file] [log] [blame]
Darren Tucker0bc85572006-11-07 23:14:41 +11001/* $OpenBSD: sshconnect1.c,v 1.70 2006/11/06 21:25:28 markus Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millereba71ba2000-04-29 23:57:08 +10006 * Code to connect to a remote host, and to perform the client side of the
7 * login (authentication) dialog.
8 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Millereba71ba2000-04-29 23:57:08 +100014 */
15
16#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +100017
Damien Millerd7834352006-08-05 12:39:39 +100018#include <sys/types.h>
19#include <sys/socket.h>
20
Damien Millereba71ba2000-04-29 23:57:08 +100021#include <openssl/bn.h>
Damien Miller2ce18da2002-02-13 13:54:27 +110022#include <openssl/md5.h>
Damien Millereba71ba2000-04-29 23:57:08 +100023
Damien Millerded319c2006-09-01 15:38:36 +100024#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100025#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100026#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100027#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100028#include <signal.h>
29#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100030
Damien Millerd7834352006-08-05 12:39:39 +100031#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000032#include "ssh.h"
33#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100035#include "buffer.h"
36#include "packet.h"
Damien Millerd7834352006-08-05 12:39:39 +100037#include "key.h"
38#include "cipher.h"
Darren Tuckere14e0052004-05-13 16:30:44 +100039#include "kex.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.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"
Darren Tuckere608ca22004-05-13 16:15:47 +100046#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "hostfile.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000049#include "auth.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);
87 xfree(comment);
88
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 Millereba71ba2000-04-29 23:57:08 +1000123 memset(response, 0, sizeof(response));
124 }
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 Millereba71ba2000-04-29 23:57:08 +1000164 MD5_CTX md;
165 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 */
169 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
170 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);
182 MD5_Init(&md);
183 MD5_Update(&md, buf, 32);
184 MD5_Update(&md, session_id, 16);
185 MD5_Final(response, &md);
186
187 debug("Sending response to host key RSA challenge.");
188
189 /* Send the response back to the server. */
190 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
191 for (i = 0; i < 16; i++)
192 packet_put_char(response[i]);
193 packet_send();
194 packet_write_wait();
195
196 memset(buf, 0, sizeof(buf));
197 memset(response, 0, sizeof(response));
198 memset(&md, 0, sizeof(md));
199}
200
201/*
202 * Checks if the user has authentication file, and if so, tries to authenticate
203 * the user using it.
204 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000205static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000206try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000207{
208 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000209 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000210 char buf[300], *passphrase, *comment, *authfile;
Darren Tucker232b76f2006-05-06 17:41:51 +1000211 int i, perm_ok = 1, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000212
Ben Lindstromc5b68002001-07-04 04:52:03 +0000213 public = options.identity_keys[idx];
214 authfile = options.identity_files[idx];
215 comment = xstrdup(authfile);
216
Damien Millereba71ba2000-04-29 23:57:08 +1000217 debug("Trying RSA authentication with key '%.100s'", comment);
218
219 /* Tell the server that we are willing to authenticate using this key. */
220 packet_start(SSH_CMSG_AUTH_RSA);
221 packet_put_bignum(public->rsa->n);
222 packet_send();
223 packet_write_wait();
224
Damien Millereba71ba2000-04-29 23:57:08 +1000225 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100226 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000227
228 /*
Damien Miller788f2122005-11-05 15:14:59 +1100229 * The server responds with failure if it doesn't like our key or
230 * doesn't support RSA authentication.
Damien Millereba71ba2000-04-29 23:57:08 +1000231 */
232 if (type == SSH_SMSG_FAILURE) {
233 debug("Server refused our key.");
234 xfree(comment);
235 return 0;
236 }
237 /* Otherwise, the server should respond with a challenge. */
238 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
239 packet_disconnect("Protocol error during RSA authentication: %d", type);
240
241 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100242 if ((challenge = BN_new()) == NULL)
243 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100244 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100245 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000246
247 debug("Received RSA challenge from server.");
248
Damien Millereba71ba2000-04-29 23:57:08 +1000249 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000250 * If the key is not stored in external hardware, we have to
251 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000252 * fails, ask for a passphrase.
253 */
Ben Lindstrome143f612002-08-20 18:41:15 +0000254 if (public->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000255 private = public;
256 else
Darren Tucker232b76f2006-05-06 17:41:51 +1000257 private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
258 &perm_ok);
259 if (private == NULL && !options.batch_mode && perm_ok) {
Ben Lindstrom05209452001-06-25 05:16:02 +0000260 snprintf(buf, sizeof(buf),
261 "Enter passphrase for RSA key '%.100s': ", comment);
262 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000263 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000264 if (strcmp(passphrase, "") != 0) {
265 private = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000266 authfile, passphrase, NULL, NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000267 quit = 0;
268 } else {
269 debug2("no passphrase given, try next key");
270 quit = 1;
271 }
Damien Millereba71ba2000-04-29 23:57:08 +1000272 memset(passphrase, 0, strlen(passphrase));
273 xfree(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000274 if (private != NULL || quit)
275 break;
276 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000277 }
Damien Millereba71ba2000-04-29 23:57:08 +1000278 }
279 /* We no longer need the comment. */
280 xfree(comment);
281
Ben Lindstrom05209452001-06-25 05:16:02 +0000282 if (private == NULL) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000283 if (!options.batch_mode && perm_ok)
Ben Lindstrom05209452001-06-25 05:16:02 +0000284 error("Bad passphrase.");
285
286 /* Send a dummy response packet to avoid protocol error. */
287 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
288 for (i = 0; i < 16; i++)
289 packet_put_char(0);
290 packet_send();
291 packet_write_wait();
292
293 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100294 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000295 BN_clear_free(challenge);
296 return 0;
297 }
298
Damien Millereba71ba2000-04-29 23:57:08 +1000299 /* Compute and send a response to the challenge. */
300 respond_to_rsa_challenge(challenge, private->rsa);
301
Ben Lindstromc5b68002001-07-04 04:52:03 +0000302 /* Destroy the private key unless it in external hardware. */
303 if (!(private->flags & KEY_FLAG_EXT))
304 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000305
306 /* We no longer need the challenge. */
307 BN_clear_free(challenge);
308
309 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100310 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000311 if (type == SSH_SMSG_SUCCESS) {
312 debug("RSA authentication accepted by server.");
313 return 1;
314 }
315 if (type != SSH_SMSG_FAILURE)
316 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
317 debug("RSA authentication refused.");
318 return 0;
319}
320
321/*
322 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
323 * authentication and RSA host authentication.
324 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000325static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000326try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000327{
328 int type;
329 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000330
331 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
332
333 /* Tell the server that we are willing to authenticate using this key. */
334 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000335 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000336 packet_put_int(BN_num_bits(host_key->rsa->n));
337 packet_put_bignum(host_key->rsa->e);
338 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000339 packet_send();
340 packet_write_wait();
341
342 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100343 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000344
345 /* The server responds with failure if it doesn't admit our
346 .rhosts authentication or doesn't know our host key. */
347 if (type == SSH_SMSG_FAILURE) {
348 debug("Server refused our rhosts authentication or host key.");
349 return 0;
350 }
351 /* Otherwise, the server should respond with a challenge. */
352 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
353 packet_disconnect("Protocol error during RSA authentication: %d", type);
354
355 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100356 if ((challenge = BN_new()) == NULL)
357 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100358 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100359 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000360
361 debug("Received RSA challenge for host key from server.");
362
363 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000364 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000365
366 /* We no longer need the challenge. */
367 BN_clear_free(challenge);
368
369 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100370 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000371 if (type == SSH_SMSG_SUCCESS) {
372 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
373 return 1;
374 }
375 if (type != SSH_SMSG_FAILURE)
376 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
377 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
378 return 0;
379}
380
Damien Millereba71ba2000-04-29 23:57:08 +1000381/*
382 * Tries to authenticate with any string-based challenge/response system.
383 * Note that the client code is not tied to s/key or TIS.
384 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000385static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000386try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000387{
388 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000389 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000390 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000391 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000392
393 debug("Doing challenge response authentication.");
394
Damien Millereba71ba2000-04-29 23:57:08 +1000395 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000396 /* request a challenge */
397 packet_start(SSH_CMSG_AUTH_TIS);
398 packet_send();
399 packet_write_wait();
400
Damien Millerdff50992002-01-22 23:16:32 +1100401 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000402 if (type != SSH_SMSG_FAILURE &&
403 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
404 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000405 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000406 }
407 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000408 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000409 return 0;
410 }
411 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100412 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000413 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100414 strchr(challenge, '\n') ? "" : "\nResponse: ");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000415 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000416 if (i != 0)
417 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000418 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000419 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000420 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000421 response = read_passphrase(prompt, 0);
422 if (strcmp(response, "") == 0) {
423 xfree(response);
424 break;
425 }
Damien Millereba71ba2000-04-29 23:57:08 +1000426 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100427 ssh_put_password(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000428 memset(response, 0, strlen(response));
429 xfree(response);
430 packet_send();
431 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100432 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000433 if (type == SSH_SMSG_SUCCESS)
434 return 1;
435 if (type != SSH_SMSG_FAILURE)
436 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000437 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000438 }
439 /* failure */
440 return 0;
441}
442
443/*
444 * Tries to authenticate with plain passwd authentication.
445 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000446static int
Damien Millereba71ba2000-04-29 23:57:08 +1000447try_password_authentication(char *prompt)
448{
Damien Millerdff50992002-01-22 23:16:32 +1100449 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000450 char *password;
451
452 debug("Doing password authentication.");
453 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000454 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000455 for (i = 0; i < options.number_of_password_prompts; i++) {
456 if (i != 0)
457 error("Permission denied, please try again.");
458 password = read_passphrase(prompt, 0);
459 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100460 ssh_put_password(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000461 memset(password, 0, strlen(password));
462 xfree(password);
463 packet_send();
464 packet_write_wait();
465
Damien Millerdff50992002-01-22 23:16:32 +1100466 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000467 if (type == SSH_SMSG_SUCCESS)
468 return 1;
469 if (type != SSH_SMSG_FAILURE)
470 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
471 }
472 /* failure */
473 return 0;
474}
475
476/*
477 * SSH1 key exchange
478 */
479void
480ssh_kex(char *host, struct sockaddr *hostaddr)
481{
482 int i;
483 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100484 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000485 int bits, rbits;
486 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000487 u_char session_key[SSH_SESSION_KEY_LENGTH];
488 u_char cookie[8];
489 u_int supported_ciphers;
490 u_int server_flags, client_flags;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000491 u_int32_t rnd = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000492
493 debug("Waiting for server public key.");
494
495 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100496 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000497
498 /* Get cookie from the packet. */
499 for (i = 0; i < 8; i++)
500 cookie[i] = packet_get_char();
501
502 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100503 server_key = key_new(KEY_RSA1);
504 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100505 packet_get_bignum(server_key->rsa->e);
506 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000507
Damien Millerda755162002-01-22 23:09:22 +1100508 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000509 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000510 logit("Warning: Server lies about size of server public key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000511 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000512 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000513 }
514 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100515 host_key = key_new(KEY_RSA1);
516 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100517 packet_get_bignum(host_key->rsa->e);
518 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000519
Damien Millerda755162002-01-22 23:09:22 +1100520 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000521 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000522 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000523 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000524 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000525 }
526
527 /* Get protocol flags. */
528 server_flags = packet_get_int();
529 packet_set_protocol_flags(server_flags);
530
531 supported_ciphers = packet_get_int();
532 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100533 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000534
535 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100536 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000537
Damien Millerda755162002-01-22 23:09:22 +1100538 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000539 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000540
541 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
542
Darren Tuckere14e0052004-05-13 16:30:44 +1000543 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
Damien Millereba71ba2000-04-29 23:57:08 +1000544
545 /* Generate a session key. */
546 arc4random_stir();
547
548 /*
549 * Generate an encryption key for the session. The key is a 256 bit
550 * random number, interpreted as a 32-byte key, with the least
551 * significant 8 bits being the first byte of the key.
552 */
553 for (i = 0; i < 32; i++) {
554 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000555 rnd = arc4random();
556 session_key[i] = rnd & 0xff;
557 rnd >>= 8;
Damien Millereba71ba2000-04-29 23:57:08 +1000558 }
559
560 /*
561 * According to the protocol spec, the first byte of the session key
562 * is the highest byte of the integer. The session key is xored with
563 * the first 16 bytes of the session id.
564 */
Damien Millerda755162002-01-22 23:09:22 +1100565 if ((key = BN_new()) == NULL)
Darren Tucker0bc85572006-11-07 23:14:41 +1100566 fatal("ssh_kex: BN_new failed");
567 if (BN_set_word(key, 0) == 0)
568 fatal("ssh_kex: BN_set_word failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000569 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
Darren Tucker0bc85572006-11-07 23:14:41 +1100570 if (BN_lshift(key, key, 8) == 0)
571 fatal("ssh_kex: BN_lshift failed");
572 if (i < 16) {
573 if (BN_add_word(key, session_key[i] ^ session_id[i])
574 == 0)
575 fatal("ssh_kex: BN_add_word failed");
576 } else {
577 if (BN_add_word(key, session_key[i]) == 0)
578 fatal("ssh_kex: BN_add_word failed");
579 }
Damien Millereba71ba2000-04-29 23:57:08 +1000580 }
581
582 /*
583 * Encrypt the integer using the public key and host key of the
584 * server (key with smaller modulus first).
585 */
Damien Millerda755162002-01-22 23:09:22 +1100586 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000587 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +1100588 if (BN_num_bits(host_key->rsa->n) <
589 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
590 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100591 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100592 BN_num_bits(host_key->rsa->n),
593 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100594 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000595 }
Damien Millerda755162002-01-22 23:09:22 +1100596 rsa_public_encrypt(key, key, server_key->rsa);
597 rsa_public_encrypt(key, key, host_key->rsa);
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 Millerda755162002-01-22 23:09:22 +1100608 rsa_public_encrypt(key, key, host_key->rsa);
609 rsa_public_encrypt(key, key, server_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000610 }
611
612 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +1100613 key_free(server_key);
614 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000615
Damien Millere39cacc2000-11-29 12:18:44 +1100616 if (options.cipher == SSH_CIPHER_NOT_SET) {
617 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
618 options.cipher = ssh_cipher_default;
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000619 } else if (options.cipher == SSH_CIPHER_INVALID ||
Damien Millere39cacc2000-11-29 12:18:44 +1100620 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +1000621 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +1100622 cipher_name(ssh_cipher_default));
623 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +1000624 }
625 /* Check that the selected cipher is supported. */
626 if (!(supported_ciphers & (1 << options.cipher)))
627 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100628 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +1000629
630 debug("Encryption type: %.100s", cipher_name(options.cipher));
631
632 /* Send the encrypted session key to the server. */
633 packet_start(SSH_CMSG_SESSION_KEY);
634 packet_put_char(options.cipher);
635
636 /* Send the cookie back to the server. */
637 for (i = 0; i < 8; i++)
638 packet_put_char(cookie[i]);
639
640 /* Send and destroy the encrypted encryption key integer. */
641 packet_put_bignum(key);
642 BN_clear_free(key);
643
644 /* Send protocol flags. */
645 packet_put_int(client_flags);
646
647 /* Send the packet now. */
648 packet_send();
649 packet_write_wait();
650
651 debug("Sent encrypted session key.");
652
653 /* Set the encryption key. */
654 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
655
656 /* We will no longer need the session key here. Destroy any extra copies. */
657 memset(session_key, 0, sizeof(session_key));
658
659 /*
660 * Expect a success message from the server. Note that this message
661 * will be received in encrypted form.
662 */
Damien Millerdff50992002-01-22 23:16:32 +1100663 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +1000664
665 debug("Received encrypted confirmation.");
666}
667
668/*
669 * Authenticate user
670 */
671void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000672ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000673 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +1000674{
675 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100676
Damien Millereba71ba2000-04-29 23:57:08 +1000677 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000678 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +1000679
680 /* Send the name of the user to log in as on the server. */
681 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000682 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000683 packet_send();
684 packet_write_wait();
685
686 /*
687 * The server should respond with success if no authentication is
688 * needed (the user has no password). Otherwise the server responds
689 * with failure.
690 */
Damien Millerdff50992002-01-22 23:16:32 +1100691 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000692
693 /* check whether the connection was accepted without authentication. */
694 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000695 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000696 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000697 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100698
Damien Millereba71ba2000-04-29 23:57:08 +1000699 /*
Damien Millereba71ba2000-04-29 23:57:08 +1000700 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
701 * authentication.
702 */
703 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000704 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000705 for (i = 0; i < sensitive->nkeys; i++) {
706 if (sensitive->keys[i] != NULL &&
707 sensitive->keys[i]->type == KEY_RSA1 &&
708 try_rhosts_rsa_authentication(local_user,
709 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000710 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000711 }
Damien Millereba71ba2000-04-29 23:57:08 +1000712 }
713 /* Try RSA authentication if the server supports it. */
714 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
715 options.rsa_authentication) {
716 /*
717 * Try RSA authentication using the authentication agent. The
718 * agent is tried first because no passphrase is needed for
719 * it, whereas identity files may require passphrases.
720 */
721 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000722 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000723
724 /* Try RSA authentication for each identity. */
725 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000726 if (options.identity_keys[i] != NULL &&
727 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +0000728 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000729 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000730 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000731 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +1000732 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +0000733 options.challenge_response_authentication && !options.batch_mode) {
734 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000735 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000736 }
737 /* Try password authentication if the server supports it. */
738 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
739 options.password_authentication && !options.batch_mode) {
740 char prompt[80];
741
Ben Lindstrome0557162001-02-11 00:00:24 +0000742 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +1000743 server_user, host);
744 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000745 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000746 }
747 /* All authentication methods have failed. Exit with an error message. */
748 fatal("Permission denied.");
749 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000750
751 success:
Damien Miller40eb1d82001-07-14 12:16:59 +1000752 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +1000753}