blob: f0eee3bdfccca75cba0188b75859e2310b228008 [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: sshconnect1.c,v 1.68 2006/08/01 23:22:47 stevesk Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millereba71ba2000-04-29 23:57:08 +10006 * Code to connect to a remote host, and to perform the client side of the
7 * login (authentication) dialog.
8 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Millereba71ba2000-04-29 23:57:08 +100014 */
15
16#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +100017
18#include <openssl/bn.h>
Damien Miller2ce18da2002-02-13 13:54:27 +110019#include <openssl/md5.h>
Damien Millereba71ba2000-04-29 23:57:08 +100020
Damien Millera7a73ee2006-08-05 11:37:59 +100021#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100022#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100023#include <string.h>
24
Ben Lindstrom226cfa02001-01-22 05:34:40 +000025#include "ssh.h"
26#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100027#include "xmalloc.h"
28#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100029#include "buffer.h"
30#include "packet.h"
Darren Tuckere14e0052004-05-13 16:30:44 +100031#include "kex.h"
Damien Millereba71ba2000-04-29 23:57:08 +100032#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "readconf.h"
35#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100036#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "sshconnect.h"
38#include "authfile.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100039#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "cipher.h"
41#include "canohost.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000042#include "auth.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043
44/* Session id for the current session. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000045u_char session_id[16];
46u_int supported_authentications = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100047
48extern Options options;
49extern char *__progname;
50
51/*
52 * Checks if the user has an authentication agent, and if so, tries to
53 * authenticate using the agent.
54 */
Ben Lindstrombba81212001-06-25 05:01:22 +000055static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000056try_agent_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +100057{
Damien Millerad833b32000-08-23 10:46:23 +100058 int type;
Damien Millereba71ba2000-04-29 23:57:08 +100059 char *comment;
60 AuthenticationConnection *auth;
Ben Lindstrom46c16222000-12-22 01:43:59 +000061 u_char response[16];
62 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +100063 Key *key;
64 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +100065
66 /* Get connection to the agent. */
67 auth = ssh_get_authentication_connection();
68 if (!auth)
69 return 0;
70
Damien Millerda755162002-01-22 23:09:22 +110071 if ((challenge = BN_new()) == NULL)
72 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100073 /* Loop through identities served by the agent. */
Damien Millerad833b32000-08-23 10:46:23 +100074 for (key = ssh_get_first_identity(auth, &comment, 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +110075 key != NULL;
76 key = ssh_get_next_identity(auth, &comment, 1)) {
Damien Millereba71ba2000-04-29 23:57:08 +100077
78 /* Try this identity. */
79 debug("Trying RSA authentication via agent with '%.100s'", comment);
80 xfree(comment);
81
82 /* Tell the server that we are willing to authenticate using this key. */
83 packet_start(SSH_CMSG_AUTH_RSA);
Damien Millerad833b32000-08-23 10:46:23 +100084 packet_put_bignum(key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +100085 packet_send();
86 packet_write_wait();
87
88 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +110089 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +100090
Damien Miller788f2122005-11-05 15:14:59 +110091 /* The server sends failure if it doesn't like our key or
Damien Millereba71ba2000-04-29 23:57:08 +100092 does not support RSA authentication. */
93 if (type == SSH_SMSG_FAILURE) {
94 debug("Server refused our key.");
Damien Millerad833b32000-08-23 10:46:23 +100095 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +100096 continue;
97 }
98 /* Otherwise it should have sent a challenge. */
99 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
100 packet_disconnect("Protocol error during RSA authentication: %d",
101 type);
102
Damien Millerd432ccf2002-01-22 23:14:44 +1100103 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100104 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000105
106 debug("Received RSA challenge from server.");
107
108 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000109 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
110 /*
111 * The agent failed to authenticate this identifier
112 * although it advertised it supports this. Just
113 * return a wrong value.
114 */
Damien Miller996acd22003-04-09 20:59:48 +1000115 logit("Authentication agent failed to decrypt challenge.");
Damien Millereba71ba2000-04-29 23:57:08 +1000116 memset(response, 0, sizeof(response));
117 }
Damien Millerad833b32000-08-23 10:46:23 +1000118 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000119 debug("Sending response to RSA challenge.");
120
121 /* Send the decrypted challenge back to the server. */
122 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
123 for (i = 0; i < 16; i++)
124 packet_put_char(response[i]);
125 packet_send();
126 packet_write_wait();
127
128 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100129 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000130
131 /* The server returns success if it accepted the authentication. */
132 if (type == SSH_SMSG_SUCCESS) {
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000133 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000134 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000135 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000136 return 1;
137 }
138 /* Otherwise it should return failure. */
139 if (type != SSH_SMSG_FAILURE)
140 packet_disconnect("Protocol error waiting RSA auth response: %d",
141 type);
142 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000143 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000144 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000145 debug("RSA authentication using agent refused.");
146 return 0;
147}
148
149/*
150 * Computes the proper response to a RSA challenge, and sends the response to
151 * the server.
152 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static void
Damien Millereba71ba2000-04-29 23:57:08 +1000154respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
155{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000156 u_char buf[32], response[16];
Damien Millereba71ba2000-04-29 23:57:08 +1000157 MD5_CTX md;
158 int i, len;
159
160 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100161 /* XXX think about Bleichenbacher, too */
162 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
163 packet_disconnect(
164 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000165
166 /* Compute the response. */
167 /* The response is MD5 of decrypted challenge plus session id. */
168 len = BN_num_bytes(challenge);
Damien Millereccb9de2005-06-17 12:59:34 +1000169 if (len <= 0 || (u_int)len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100170 packet_disconnect(
171 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000172
173 memset(buf, 0, sizeof(buf));
174 BN_bn2bin(challenge, buf + sizeof(buf) - len);
175 MD5_Init(&md);
176 MD5_Update(&md, buf, 32);
177 MD5_Update(&md, session_id, 16);
178 MD5_Final(response, &md);
179
180 debug("Sending response to host key RSA challenge.");
181
182 /* Send the response back to the server. */
183 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
184 for (i = 0; i < 16; i++)
185 packet_put_char(response[i]);
186 packet_send();
187 packet_write_wait();
188
189 memset(buf, 0, sizeof(buf));
190 memset(response, 0, sizeof(response));
191 memset(&md, 0, sizeof(md));
192}
193
194/*
195 * Checks if the user has authentication file, and if so, tries to authenticate
196 * the user using it.
197 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000198static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000199try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000200{
201 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000202 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000203 char buf[300], *passphrase, *comment, *authfile;
Darren Tucker232b76f2006-05-06 17:41:51 +1000204 int i, perm_ok = 1, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000205
Ben Lindstromc5b68002001-07-04 04:52:03 +0000206 public = options.identity_keys[idx];
207 authfile = options.identity_files[idx];
208 comment = xstrdup(authfile);
209
Damien Millereba71ba2000-04-29 23:57:08 +1000210 debug("Trying RSA authentication with key '%.100s'", comment);
211
212 /* Tell the server that we are willing to authenticate using this key. */
213 packet_start(SSH_CMSG_AUTH_RSA);
214 packet_put_bignum(public->rsa->n);
215 packet_send();
216 packet_write_wait();
217
Damien Millereba71ba2000-04-29 23:57:08 +1000218 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100219 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000220
221 /*
Damien Miller788f2122005-11-05 15:14:59 +1100222 * The server responds with failure if it doesn't like our key or
223 * doesn't support RSA authentication.
Damien Millereba71ba2000-04-29 23:57:08 +1000224 */
225 if (type == SSH_SMSG_FAILURE) {
226 debug("Server refused our key.");
227 xfree(comment);
228 return 0;
229 }
230 /* Otherwise, the server should respond with a challenge. */
231 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
232 packet_disconnect("Protocol error during RSA authentication: %d", type);
233
234 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100235 if ((challenge = BN_new()) == NULL)
236 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100237 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100238 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000239
240 debug("Received RSA challenge from server.");
241
Damien Millereba71ba2000-04-29 23:57:08 +1000242 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000243 * If the key is not stored in external hardware, we have to
244 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000245 * fails, ask for a passphrase.
246 */
Ben Lindstrome143f612002-08-20 18:41:15 +0000247 if (public->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000248 private = public;
249 else
Darren Tucker232b76f2006-05-06 17:41:51 +1000250 private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
251 &perm_ok);
252 if (private == NULL && !options.batch_mode && perm_ok) {
Ben Lindstrom05209452001-06-25 05:16:02 +0000253 snprintf(buf, sizeof(buf),
254 "Enter passphrase for RSA key '%.100s': ", comment);
255 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000256 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000257 if (strcmp(passphrase, "") != 0) {
258 private = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000259 authfile, passphrase, NULL, NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000260 quit = 0;
261 } else {
262 debug2("no passphrase given, try next key");
263 quit = 1;
264 }
Damien Millereba71ba2000-04-29 23:57:08 +1000265 memset(passphrase, 0, strlen(passphrase));
266 xfree(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000267 if (private != NULL || quit)
268 break;
269 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000270 }
Damien Millereba71ba2000-04-29 23:57:08 +1000271 }
272 /* We no longer need the comment. */
273 xfree(comment);
274
Ben Lindstrom05209452001-06-25 05:16:02 +0000275 if (private == NULL) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000276 if (!options.batch_mode && perm_ok)
Ben Lindstrom05209452001-06-25 05:16:02 +0000277 error("Bad passphrase.");
278
279 /* Send a dummy response packet to avoid protocol error. */
280 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
281 for (i = 0; i < 16; i++)
282 packet_put_char(0);
283 packet_send();
284 packet_write_wait();
285
286 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100287 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000288 BN_clear_free(challenge);
289 return 0;
290 }
291
Damien Millereba71ba2000-04-29 23:57:08 +1000292 /* Compute and send a response to the challenge. */
293 respond_to_rsa_challenge(challenge, private->rsa);
294
Ben Lindstromc5b68002001-07-04 04:52:03 +0000295 /* Destroy the private key unless it in external hardware. */
296 if (!(private->flags & KEY_FLAG_EXT))
297 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000298
299 /* We no longer need the challenge. */
300 BN_clear_free(challenge);
301
302 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100303 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000304 if (type == SSH_SMSG_SUCCESS) {
305 debug("RSA authentication accepted by server.");
306 return 1;
307 }
308 if (type != SSH_SMSG_FAILURE)
309 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
310 debug("RSA authentication refused.");
311 return 0;
312}
313
314/*
315 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
316 * authentication and RSA host authentication.
317 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000318static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000319try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000320{
321 int type;
322 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000323
324 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
325
326 /* Tell the server that we are willing to authenticate using this key. */
327 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000328 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000329 packet_put_int(BN_num_bits(host_key->rsa->n));
330 packet_put_bignum(host_key->rsa->e);
331 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000332 packet_send();
333 packet_write_wait();
334
335 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100336 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000337
338 /* The server responds with failure if it doesn't admit our
339 .rhosts authentication or doesn't know our host key. */
340 if (type == SSH_SMSG_FAILURE) {
341 debug("Server refused our rhosts authentication or host key.");
342 return 0;
343 }
344 /* Otherwise, the server should respond with a challenge. */
345 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
346 packet_disconnect("Protocol error during RSA authentication: %d", type);
347
348 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100349 if ((challenge = BN_new()) == NULL)
350 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100351 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100352 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000353
354 debug("Received RSA challenge for host key from server.");
355
356 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000357 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000358
359 /* We no longer need the challenge. */
360 BN_clear_free(challenge);
361
362 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100363 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000364 if (type == SSH_SMSG_SUCCESS) {
365 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
366 return 1;
367 }
368 if (type != SSH_SMSG_FAILURE)
369 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
370 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
371 return 0;
372}
373
Damien Millereba71ba2000-04-29 23:57:08 +1000374/*
375 * Tries to authenticate with any string-based challenge/response system.
376 * Note that the client code is not tied to s/key or TIS.
377 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000378static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000379try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000380{
381 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000382 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000383 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000384 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000385
386 debug("Doing challenge response authentication.");
387
Damien Millereba71ba2000-04-29 23:57:08 +1000388 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000389 /* request a challenge */
390 packet_start(SSH_CMSG_AUTH_TIS);
391 packet_send();
392 packet_write_wait();
393
Damien Millerdff50992002-01-22 23:16:32 +1100394 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000395 if (type != SSH_SMSG_FAILURE &&
396 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
397 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000398 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000399 }
400 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000401 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000402 return 0;
403 }
404 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100405 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000406 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100407 strchr(challenge, '\n') ? "" : "\nResponse: ");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000408 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000409 if (i != 0)
410 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000411 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000412 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000413 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000414 response = read_passphrase(prompt, 0);
415 if (strcmp(response, "") == 0) {
416 xfree(response);
417 break;
418 }
Damien Millereba71ba2000-04-29 23:57:08 +1000419 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100420 ssh_put_password(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000421 memset(response, 0, strlen(response));
422 xfree(response);
423 packet_send();
424 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100425 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000426 if (type == SSH_SMSG_SUCCESS)
427 return 1;
428 if (type != SSH_SMSG_FAILURE)
429 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000430 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000431 }
432 /* failure */
433 return 0;
434}
435
436/*
437 * Tries to authenticate with plain passwd authentication.
438 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000439static int
Damien Millereba71ba2000-04-29 23:57:08 +1000440try_password_authentication(char *prompt)
441{
Damien Millerdff50992002-01-22 23:16:32 +1100442 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000443 char *password;
444
445 debug("Doing password authentication.");
446 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000447 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000448 for (i = 0; i < options.number_of_password_prompts; i++) {
449 if (i != 0)
450 error("Permission denied, please try again.");
451 password = read_passphrase(prompt, 0);
452 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100453 ssh_put_password(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000454 memset(password, 0, strlen(password));
455 xfree(password);
456 packet_send();
457 packet_write_wait();
458
Damien Millerdff50992002-01-22 23:16:32 +1100459 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000460 if (type == SSH_SMSG_SUCCESS)
461 return 1;
462 if (type != SSH_SMSG_FAILURE)
463 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
464 }
465 /* failure */
466 return 0;
467}
468
469/*
470 * SSH1 key exchange
471 */
472void
473ssh_kex(char *host, struct sockaddr *hostaddr)
474{
475 int i;
476 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100477 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000478 int bits, rbits;
479 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000480 u_char session_key[SSH_SESSION_KEY_LENGTH];
481 u_char cookie[8];
482 u_int supported_ciphers;
483 u_int server_flags, client_flags;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000484 u_int32_t rnd = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000485
486 debug("Waiting for server public key.");
487
488 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100489 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000490
491 /* Get cookie from the packet. */
492 for (i = 0; i < 8; i++)
493 cookie[i] = packet_get_char();
494
495 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100496 server_key = key_new(KEY_RSA1);
497 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100498 packet_get_bignum(server_key->rsa->e);
499 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000500
Damien Millerda755162002-01-22 23:09:22 +1100501 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000502 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000503 logit("Warning: Server lies about size of server public key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000504 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000505 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000506 }
507 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100508 host_key = key_new(KEY_RSA1);
509 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100510 packet_get_bignum(host_key->rsa->e);
511 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000512
Damien Millerda755162002-01-22 23:09:22 +1100513 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000514 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000515 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000516 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000517 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000518 }
519
520 /* Get protocol flags. */
521 server_flags = packet_get_int();
522 packet_set_protocol_flags(server_flags);
523
524 supported_ciphers = packet_get_int();
525 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100526 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000527
528 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100529 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000530
Damien Millerda755162002-01-22 23:09:22 +1100531 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000532 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000533
534 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
535
Darren Tuckere14e0052004-05-13 16:30:44 +1000536 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
Damien Millereba71ba2000-04-29 23:57:08 +1000537
538 /* Generate a session key. */
539 arc4random_stir();
540
541 /*
542 * Generate an encryption key for the session. The key is a 256 bit
543 * random number, interpreted as a 32-byte key, with the least
544 * significant 8 bits being the first byte of the key.
545 */
546 for (i = 0; i < 32; i++) {
547 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000548 rnd = arc4random();
549 session_key[i] = rnd & 0xff;
550 rnd >>= 8;
Damien Millereba71ba2000-04-29 23:57:08 +1000551 }
552
553 /*
554 * According to the protocol spec, the first byte of the session key
555 * is the highest byte of the integer. The session key is xored with
556 * the first 16 bytes of the session id.
557 */
Damien Millerda755162002-01-22 23:09:22 +1100558 if ((key = BN_new()) == NULL)
559 fatal("respond_to_rsa_challenge: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000560 BN_set_word(key, 0);
561 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
562 BN_lshift(key, key, 8);
563 if (i < 16)
564 BN_add_word(key, session_key[i] ^ session_id[i]);
565 else
566 BN_add_word(key, session_key[i]);
567 }
568
569 /*
570 * Encrypt the integer using the public key and host key of the
571 * server (key with smaller modulus first).
572 */
Damien Millerda755162002-01-22 23:09:22 +1100573 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000574 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +1100575 if (BN_num_bits(host_key->rsa->n) <
576 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
577 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100578 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100579 BN_num_bits(host_key->rsa->n),
580 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100581 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000582 }
Damien Millerda755162002-01-22 23:09:22 +1100583 rsa_public_encrypt(key, key, server_key->rsa);
584 rsa_public_encrypt(key, key, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000585 } else {
586 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +1100587 if (BN_num_bits(server_key->rsa->n) <
588 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
589 fatal("respond_to_rsa_challenge: server_key %d < host_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(server_key->rsa->n),
592 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100593 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000594 }
Damien Millerda755162002-01-22 23:09:22 +1100595 rsa_public_encrypt(key, key, host_key->rsa);
596 rsa_public_encrypt(key, key, server_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000597 }
598
599 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +1100600 key_free(server_key);
601 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000602
Damien Millere39cacc2000-11-29 12:18:44 +1100603 if (options.cipher == SSH_CIPHER_NOT_SET) {
604 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
605 options.cipher = ssh_cipher_default;
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000606 } else if (options.cipher == SSH_CIPHER_INVALID ||
Damien Millere39cacc2000-11-29 12:18:44 +1100607 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +1000608 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +1100609 cipher_name(ssh_cipher_default));
610 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +1000611 }
612 /* Check that the selected cipher is supported. */
613 if (!(supported_ciphers & (1 << options.cipher)))
614 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100615 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +1000616
617 debug("Encryption type: %.100s", cipher_name(options.cipher));
618
619 /* Send the encrypted session key to the server. */
620 packet_start(SSH_CMSG_SESSION_KEY);
621 packet_put_char(options.cipher);
622
623 /* Send the cookie back to the server. */
624 for (i = 0; i < 8; i++)
625 packet_put_char(cookie[i]);
626
627 /* Send and destroy the encrypted encryption key integer. */
628 packet_put_bignum(key);
629 BN_clear_free(key);
630
631 /* Send protocol flags. */
632 packet_put_int(client_flags);
633
634 /* Send the packet now. */
635 packet_send();
636 packet_write_wait();
637
638 debug("Sent encrypted session key.");
639
640 /* Set the encryption key. */
641 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
642
643 /* We will no longer need the session key here. Destroy any extra copies. */
644 memset(session_key, 0, sizeof(session_key));
645
646 /*
647 * Expect a success message from the server. Note that this message
648 * will be received in encrypted form.
649 */
Damien Millerdff50992002-01-22 23:16:32 +1100650 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +1000651
652 debug("Received encrypted confirmation.");
653}
654
655/*
656 * Authenticate user
657 */
658void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000659ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000660 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +1000661{
662 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100663
Damien Millereba71ba2000-04-29 23:57:08 +1000664 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000665 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +1000666
667 /* Send the name of the user to log in as on the server. */
668 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000669 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000670 packet_send();
671 packet_write_wait();
672
673 /*
674 * The server should respond with success if no authentication is
675 * needed (the user has no password). Otherwise the server responds
676 * with failure.
677 */
Damien Millerdff50992002-01-22 23:16:32 +1100678 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000679
680 /* check whether the connection was accepted without authentication. */
681 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000682 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000683 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000684 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100685
Damien Millereba71ba2000-04-29 23:57:08 +1000686 /*
Damien Millereba71ba2000-04-29 23:57:08 +1000687 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
688 * authentication.
689 */
690 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000691 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000692 for (i = 0; i < sensitive->nkeys; i++) {
693 if (sensitive->keys[i] != NULL &&
694 sensitive->keys[i]->type == KEY_RSA1 &&
695 try_rhosts_rsa_authentication(local_user,
696 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000697 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000698 }
Damien Millereba71ba2000-04-29 23:57:08 +1000699 }
700 /* Try RSA authentication if the server supports it. */
701 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
702 options.rsa_authentication) {
703 /*
704 * Try RSA authentication using the authentication agent. The
705 * agent is tried first because no passphrase is needed for
706 * it, whereas identity files may require passphrases.
707 */
708 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000709 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000710
711 /* Try RSA authentication for each identity. */
712 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000713 if (options.identity_keys[i] != NULL &&
714 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +0000715 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000716 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000717 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000718 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +1000719 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +0000720 options.challenge_response_authentication && !options.batch_mode) {
721 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000722 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000723 }
724 /* Try password authentication if the server supports it. */
725 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
726 options.password_authentication && !options.batch_mode) {
727 char prompt[80];
728
Ben Lindstrome0557162001-02-11 00:00:24 +0000729 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +1000730 server_user, host);
731 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000732 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000733 }
734 /* All authentication methods have failed. Exit with an error message. */
735 fatal("Permission denied.");
736 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000737
738 success:
Damien Miller40eb1d82001-07-14 12:16:59 +1000739 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +1000740}