blob: 5e1802b1055092b534aa22da8014e60c44a4ce45 [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millereba71ba2000-04-29 23:57:08 +10005 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
7 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Millereba71ba2000-04-29 23:57:08 +100013 */
14
15#include "includes.h"
Darren Tuckerec960f22003-08-13 20:37:05 +100016RCSID("$OpenBSD: sshconnect1.c,v 1.55 2003/08/13 08:46:31 markus Exp $");
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
Ben Lindstromec95ed92001-07-04 04:21:14 +000021#ifdef KRB5
22#include <krb5.h>
Damien Millerfd4c9ee2002-04-13 11:04:40 +100023#ifndef HEIMDAL
24#define krb5_get_err_text(context,code) error_message(code)
25#endif /* !HEIMDAL */
Ben Lindstromec95ed92001-07-04 04:21:14 +000026#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027
28#include "ssh.h"
29#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100030#include "xmalloc.h"
31#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100032#include "buffer.h"
33#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "mpaux.h"
35#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "readconf.h"
38#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100039#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "sshconnect.h"
41#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "readpass.h"
43#include "cipher.h"
44#include "canohost.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000045#include "auth.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046
47/* Session id for the current session. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000048u_char session_id[16];
49u_int supported_authentications = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100050
51extern Options options;
52extern char *__progname;
53
54/*
55 * Checks if the user has an authentication agent, and if so, tries to
56 * authenticate using the agent.
57 */
Ben Lindstrombba81212001-06-25 05:01:22 +000058static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000059try_agent_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +100060{
Damien Millerad833b32000-08-23 10:46:23 +100061 int type;
Damien Millereba71ba2000-04-29 23:57:08 +100062 char *comment;
63 AuthenticationConnection *auth;
Ben Lindstrom46c16222000-12-22 01:43:59 +000064 u_char response[16];
65 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +100066 Key *key;
67 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +100068
69 /* Get connection to the agent. */
70 auth = ssh_get_authentication_connection();
71 if (!auth)
72 return 0;
73
Damien Millerda755162002-01-22 23:09:22 +110074 if ((challenge = BN_new()) == NULL)
75 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100076 /* Loop through identities served by the agent. */
Damien Millerad833b32000-08-23 10:46:23 +100077 for (key = ssh_get_first_identity(auth, &comment, 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +110078 key != NULL;
79 key = ssh_get_next_identity(auth, &comment, 1)) {
Damien Millereba71ba2000-04-29 23:57:08 +100080
81 /* Try this identity. */
82 debug("Trying RSA authentication via agent with '%.100s'", comment);
83 xfree(comment);
84
85 /* Tell the server that we are willing to authenticate using this key. */
86 packet_start(SSH_CMSG_AUTH_RSA);
Damien Millerad833b32000-08-23 10:46:23 +100087 packet_put_bignum(key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +100088 packet_send();
89 packet_write_wait();
90
91 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +110092 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +100093
94 /* The server sends failure if it doesn\'t like our key or
95 does not support RSA authentication. */
96 if (type == SSH_SMSG_FAILURE) {
97 debug("Server refused our key.");
Damien Millerad833b32000-08-23 10:46:23 +100098 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +100099 continue;
100 }
101 /* Otherwise it should have sent a challenge. */
102 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
103 packet_disconnect("Protocol error during RSA authentication: %d",
104 type);
105
Damien Millerd432ccf2002-01-22 23:14:44 +1100106 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100107 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000108
109 debug("Received RSA challenge from server.");
110
111 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000112 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
113 /*
114 * The agent failed to authenticate this identifier
115 * although it advertised it supports this. Just
116 * return a wrong value.
117 */
Damien Miller996acd22003-04-09 20:59:48 +1000118 logit("Authentication agent failed to decrypt challenge.");
Damien Millereba71ba2000-04-29 23:57:08 +1000119 memset(response, 0, sizeof(response));
120 }
Damien Millerad833b32000-08-23 10:46:23 +1000121 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000122 debug("Sending response to RSA challenge.");
123
124 /* Send the decrypted challenge back to the server. */
125 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
126 for (i = 0; i < 16; i++)
127 packet_put_char(response[i]);
128 packet_send();
129 packet_write_wait();
130
131 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100132 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000133
134 /* The server returns success if it accepted the authentication. */
135 if (type == SSH_SMSG_SUCCESS) {
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000136 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000137 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000138 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000139 return 1;
140 }
141 /* Otherwise it should return failure. */
142 if (type != SSH_SMSG_FAILURE)
143 packet_disconnect("Protocol error waiting RSA auth response: %d",
144 type);
145 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000146 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000147 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000148 debug("RSA authentication using agent refused.");
149 return 0;
150}
151
152/*
153 * Computes the proper response to a RSA challenge, and sends the response to
154 * the server.
155 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000156static void
Damien Millereba71ba2000-04-29 23:57:08 +1000157respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
158{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000159 u_char buf[32], response[16];
Damien Millereba71ba2000-04-29 23:57:08 +1000160 MD5_CTX md;
161 int i, len;
162
163 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100164 /* XXX think about Bleichenbacher, too */
165 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
166 packet_disconnect(
167 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000168
169 /* Compute the response. */
170 /* The response is MD5 of decrypted challenge plus session id. */
171 len = BN_num_bytes(challenge);
172 if (len <= 0 || len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100173 packet_disconnect(
174 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000175
176 memset(buf, 0, sizeof(buf));
177 BN_bn2bin(challenge, buf + sizeof(buf) - len);
178 MD5_Init(&md);
179 MD5_Update(&md, buf, 32);
180 MD5_Update(&md, session_id, 16);
181 MD5_Final(response, &md);
182
183 debug("Sending response to host key RSA challenge.");
184
185 /* Send the response back to the server. */
186 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
187 for (i = 0; i < 16; i++)
188 packet_put_char(response[i]);
189 packet_send();
190 packet_write_wait();
191
192 memset(buf, 0, sizeof(buf));
193 memset(response, 0, sizeof(response));
194 memset(&md, 0, sizeof(md));
195}
196
197/*
198 * Checks if the user has authentication file, and if so, tries to authenticate
199 * the user using it.
200 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000202try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000203{
204 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000205 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000206 char buf[300], *passphrase, *comment, *authfile;
Damien Millerdff50992002-01-22 23:16:32 +1100207 int i, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000208
Ben Lindstromc5b68002001-07-04 04:52:03 +0000209 public = options.identity_keys[idx];
210 authfile = options.identity_files[idx];
211 comment = xstrdup(authfile);
212
Damien Millereba71ba2000-04-29 23:57:08 +1000213 debug("Trying RSA authentication with key '%.100s'", comment);
214
215 /* Tell the server that we are willing to authenticate using this key. */
216 packet_start(SSH_CMSG_AUTH_RSA);
217 packet_put_bignum(public->rsa->n);
218 packet_send();
219 packet_write_wait();
220
Damien Millereba71ba2000-04-29 23:57:08 +1000221 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100222 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000223
224 /*
225 * The server responds with failure if it doesn\'t like our key or
226 * doesn\'t support RSA authentication.
227 */
228 if (type == SSH_SMSG_FAILURE) {
229 debug("Server refused our key.");
230 xfree(comment);
231 return 0;
232 }
233 /* Otherwise, the server should respond with a challenge. */
234 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
235 packet_disconnect("Protocol error during RSA authentication: %d", type);
236
237 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100238 if ((challenge = BN_new()) == NULL)
239 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100240 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100241 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000242
243 debug("Received RSA challenge from server.");
244
Damien Millereba71ba2000-04-29 23:57:08 +1000245 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000246 * If the key is not stored in external hardware, we have to
247 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000248 * fails, ask for a passphrase.
249 */
Ben Lindstrome143f612002-08-20 18:41:15 +0000250 if (public->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000251 private = public;
252 else
253 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000254 if (private == NULL && !options.batch_mode) {
255 snprintf(buf, sizeof(buf),
256 "Enter passphrase for RSA key '%.100s': ", comment);
257 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000258 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000259 if (strcmp(passphrase, "") != 0) {
260 private = key_load_private_type(KEY_RSA1,
261 authfile, passphrase, NULL);
262 quit = 0;
263 } else {
264 debug2("no passphrase given, try next key");
265 quit = 1;
266 }
Damien Millereba71ba2000-04-29 23:57:08 +1000267 memset(passphrase, 0, strlen(passphrase));
268 xfree(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000269 if (private != NULL || quit)
270 break;
271 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000272 }
Damien Millereba71ba2000-04-29 23:57:08 +1000273 }
274 /* We no longer need the comment. */
275 xfree(comment);
276
Ben Lindstrom05209452001-06-25 05:16:02 +0000277 if (private == NULL) {
278 if (!options.batch_mode)
279 error("Bad passphrase.");
280
281 /* Send a dummy response packet to avoid protocol error. */
282 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
283 for (i = 0; i < 16; i++)
284 packet_put_char(0);
285 packet_send();
286 packet_write_wait();
287
288 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100289 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000290 BN_clear_free(challenge);
291 return 0;
292 }
293
Damien Millereba71ba2000-04-29 23:57:08 +1000294 /* Compute and send a response to the challenge. */
295 respond_to_rsa_challenge(challenge, private->rsa);
296
Ben Lindstromc5b68002001-07-04 04:52:03 +0000297 /* Destroy the private key unless it in external hardware. */
298 if (!(private->flags & KEY_FLAG_EXT))
299 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000300
301 /* We no longer need the challenge. */
302 BN_clear_free(challenge);
303
304 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100305 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000306 if (type == SSH_SMSG_SUCCESS) {
307 debug("RSA authentication accepted by server.");
308 return 1;
309 }
310 if (type != SSH_SMSG_FAILURE)
311 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
312 debug("RSA authentication refused.");
313 return 0;
314}
315
316/*
317 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
318 * authentication and RSA host authentication.
319 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000320static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000321try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000322{
323 int type;
324 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000325
326 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
327
328 /* Tell the server that we are willing to authenticate using this key. */
329 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000330 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000331 packet_put_int(BN_num_bits(host_key->rsa->n));
332 packet_put_bignum(host_key->rsa->e);
333 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000334 packet_send();
335 packet_write_wait();
336
337 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100338 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000339
340 /* The server responds with failure if it doesn't admit our
341 .rhosts authentication or doesn't know our host key. */
342 if (type == SSH_SMSG_FAILURE) {
343 debug("Server refused our rhosts authentication or host key.");
344 return 0;
345 }
346 /* Otherwise, the server should respond with a challenge. */
347 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
348 packet_disconnect("Protocol error during RSA authentication: %d", type);
349
350 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100351 if ((challenge = BN_new()) == NULL)
352 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100353 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100354 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000355
356 debug("Received RSA challenge for host key from server.");
357
358 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000359 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000360
361 /* We no longer need the challenge. */
362 BN_clear_free(challenge);
363
364 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100365 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000366 if (type == SSH_SMSG_SUCCESS) {
367 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
368 return 1;
369 }
370 if (type != SSH_SMSG_FAILURE)
371 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
372 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
373 return 0;
374}
375
Ben Lindstromec95ed92001-07-04 04:21:14 +0000376#ifdef KRB5
Ben Lindstrombba81212001-06-25 05:01:22 +0000377static int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000378try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
379{
380 krb5_error_code problem;
381 const char *tkfile;
382 struct stat buf;
383 krb5_ccache ccache = NULL;
384 const char *remotehost;
385 krb5_data ap;
Damien Millerdff50992002-01-22 23:16:32 +1100386 int type;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000387 krb5_ap_rep_enc_part *reply = NULL;
388 int ret;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100389
Ben Lindstromec95ed92001-07-04 04:21:14 +0000390 memset(&ap, 0, sizeof(ap));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100391
Ben Lindstromec95ed92001-07-04 04:21:14 +0000392 problem = krb5_init_context(context);
393 if (problem) {
394 debug("Kerberos v5: krb5_init_context failed");
395 ret = 0;
396 goto out;
397 }
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000398
399 problem = krb5_auth_con_init(*context, auth_context);
400 if (problem) {
401 debug("Kerberos v5: krb5_auth_con_init failed");
402 ret = 0;
403 goto out;
404 }
405
406#ifndef HEIMDAL
407 problem = krb5_auth_con_setflags(*context, *auth_context,
408 KRB5_AUTH_CONTEXT_RET_TIME);
409 if (problem) {
410 debug("Keberos v5: krb5_auth_con_setflags failed");
411 ret = 0;
412 goto out;
413 }
414#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100415
Ben Lindstromec95ed92001-07-04 04:21:14 +0000416 tkfile = krb5_cc_default_name(*context);
417 if (strncmp(tkfile, "FILE:", 5) == 0)
418 tkfile += 5;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100419
Ben Lindstromec95ed92001-07-04 04:21:14 +0000420 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
421 debug("Kerberos v5: could not get default ccache (permission denied).");
422 ret = 0;
423 goto out;
424 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100425
Ben Lindstromec95ed92001-07-04 04:21:14 +0000426 problem = krb5_cc_default(*context, &ccache);
427 if (problem) {
428 debug("Kerberos v5: krb5_cc_default failed: %s",
429 krb5_get_err_text(*context, problem));
430 ret = 0;
431 goto out;
432 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100433
Ben Lindstromec95ed92001-07-04 04:21:14 +0000434 remotehost = get_canonical_hostname(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100435
Ben Lindstromec95ed92001-07-04 04:21:14 +0000436 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
437 "host", remotehost, NULL, ccache, &ap);
438 if (problem) {
439 debug("Kerberos v5: krb5_mk_req failed: %s",
440 krb5_get_err_text(*context, problem));
441 ret = 0;
442 goto out;
443 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100444
Ben Lindstromec95ed92001-07-04 04:21:14 +0000445 packet_start(SSH_CMSG_AUTH_KERBEROS);
446 packet_put_string((char *) ap.data, ap.length);
447 packet_send();
448 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100449
Ben Lindstromec95ed92001-07-04 04:21:14 +0000450 xfree(ap.data);
451 ap.length = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100452
Damien Millerdff50992002-01-22 23:16:32 +1100453 type = packet_read();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000454 switch (type) {
Damien Miller9f0f5c62001-12-21 14:45:46 +1100455 case SSH_SMSG_FAILURE:
456 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
457 debug("Kerberos v5 authentication failed.");
458 ret = 0;
459 break;
460
Ben Lindstromec95ed92001-07-04 04:21:14 +0000461 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
Damien Miller9f0f5c62001-12-21 14:45:46 +1100462 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
463 debug("Kerberos v5 authentication accepted.");
464
465 /* Get server's response. */
466 ap.data = packet_get_string((unsigned int *) &ap.length);
Damien Miller48b03fc2002-01-22 23:11:40 +1100467 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100468 /* XXX je to dobre? */
469
470 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
471 if (problem) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000472 ret = 0;
473 }
474 ret = 1;
475 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100476
Ben Lindstromec95ed92001-07-04 04:21:14 +0000477 default:
478 packet_disconnect("Protocol error on Kerberos v5 response: %d",
479 type);
480 ret = 0;
481 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100482
Ben Lindstromec95ed92001-07-04 04:21:14 +0000483 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100484
Ben Lindstromec95ed92001-07-04 04:21:14 +0000485 out:
486 if (ccache != NULL)
487 krb5_cc_close(*context, ccache);
488 if (reply != NULL)
489 krb5_free_ap_rep_enc_part(*context, reply);
490 if (ap.length > 0)
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000491#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000492 krb5_data_free(&ap);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000493#else
494 krb5_free_data_contents(*context, &ap);
495#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100496
Ben Lindstromec95ed92001-07-04 04:21:14 +0000497 return (ret);
498}
499
500static void
501send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
502{
Damien Millerdff50992002-01-22 23:16:32 +1100503 int fd, type;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000504 krb5_error_code problem;
505 krb5_data outbuf;
506 krb5_ccache ccache = NULL;
507 krb5_creds creds;
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000508#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000509 krb5_kdc_flags flags;
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000510#else
511 int forwardable;
512#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +0000513 const char *remotehost;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100514
Ben Lindstromec95ed92001-07-04 04:21:14 +0000515 memset(&creds, 0, sizeof(creds));
516 memset(&outbuf, 0, sizeof(outbuf));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100517
Ben Lindstromec95ed92001-07-04 04:21:14 +0000518 fd = packet_get_connection_in();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100519
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000520#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000521 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000522#else
523 problem = krb5_auth_con_genaddrs(context, auth_context, fd,
524 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
525 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
526#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +0000527 if (problem)
528 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100529
Ben Lindstromec95ed92001-07-04 04:21:14 +0000530 problem = krb5_cc_default(context, &ccache);
531 if (problem)
532 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100533
Ben Lindstromec95ed92001-07-04 04:21:14 +0000534 problem = krb5_cc_get_principal(context, ccache, &creds.client);
535 if (problem)
536 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100537
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000538 remotehost = get_canonical_hostname(1);
539
540#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000541 problem = krb5_build_principal(context, &creds.server,
542 strlen(creds.client->realm), creds.client->realm,
543 "krbtgt", creds.client->realm, NULL);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000544#else
545 problem = krb5_build_principal(context, &creds.server,
546 creds.client->realm.length, creds.client->realm.data,
547 "host", remotehost, NULL);
548#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +0000549 if (problem)
550 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100551
Ben Lindstromec95ed92001-07-04 04:21:14 +0000552 creds.times.endtime = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100553
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000554#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000555 flags.i = 0;
556 flags.b.forwarded = 1;
557 flags.b.forwardable = krb5_config_get_bool(context, NULL,
558 "libdefaults", "forwardable", NULL);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000559 problem = krb5_get_forwarded_creds(context, auth_context,
560 ccache, flags.i, remotehost, &creds, &outbuf);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000561#else
562 forwardable = 1;
563 problem = krb5_fwd_tgt_creds(context, auth_context, remotehost,
564 creds.client, creds.server, ccache, forwardable, &outbuf);
565#endif
566
Ben Lindstromec95ed92001-07-04 04:21:14 +0000567 if (problem)
568 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100569
Ben Lindstromec95ed92001-07-04 04:21:14 +0000570 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
571 packet_put_string((char *)outbuf.data, outbuf.length);
572 packet_send();
573 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100574
Damien Millerdff50992002-01-22 23:16:32 +1100575 type = packet_read();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100576
Ben Lindstromec95ed92001-07-04 04:21:14 +0000577 if (type == SSH_SMSG_SUCCESS) {
578 char *pname;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100579
Ben Lindstromec95ed92001-07-04 04:21:14 +0000580 krb5_unparse_name(context, creds.client, &pname);
581 debug("Kerberos v5 TGT forwarded (%s).", pname);
582 xfree(pname);
583 } else
584 debug("Kerberos v5 TGT forwarding failed.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100585
Ben Lindstromec95ed92001-07-04 04:21:14 +0000586 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100587
Ben Lindstromec95ed92001-07-04 04:21:14 +0000588 out:
589 if (problem)
590 debug("Kerberos v5 TGT forwarding failed: %s",
591 krb5_get_err_text(context, problem));
592 if (creds.client)
593 krb5_free_principal(context, creds.client);
594 if (creds.server)
595 krb5_free_principal(context, creds.server);
596 if (ccache)
597 krb5_cc_close(context, ccache);
598 if (outbuf.data)
599 xfree(outbuf.data);
600}
601#endif /* KRB5 */
602
Damien Millereba71ba2000-04-29 23:57:08 +1000603/*
604 * Tries to authenticate with any string-based challenge/response system.
605 * Note that the client code is not tied to s/key or TIS.
606 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000607static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000608try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000609{
610 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000611 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000612 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000613 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000614
615 debug("Doing challenge response authentication.");
616
Damien Millereba71ba2000-04-29 23:57:08 +1000617 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000618 /* request a challenge */
619 packet_start(SSH_CMSG_AUTH_TIS);
620 packet_send();
621 packet_write_wait();
622
Damien Millerdff50992002-01-22 23:16:32 +1100623 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000624 if (type != SSH_SMSG_FAILURE &&
625 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
626 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000627 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000628 }
629 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000630 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000631 return 0;
632 }
633 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100634 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000635 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100636 strchr(challenge, '\n') ? "" : "\nResponse: ");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000637 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000638 if (i != 0)
639 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000640 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000641 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000642 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000643 response = read_passphrase(prompt, 0);
644 if (strcmp(response, "") == 0) {
645 xfree(response);
646 break;
647 }
Damien Millereba71ba2000-04-29 23:57:08 +1000648 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100649 ssh_put_password(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000650 memset(response, 0, strlen(response));
651 xfree(response);
652 packet_send();
653 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100654 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000655 if (type == SSH_SMSG_SUCCESS)
656 return 1;
657 if (type != SSH_SMSG_FAILURE)
658 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000659 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000660 }
661 /* failure */
662 return 0;
663}
664
665/*
666 * Tries to authenticate with plain passwd authentication.
667 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000668static int
Damien Millereba71ba2000-04-29 23:57:08 +1000669try_password_authentication(char *prompt)
670{
Damien Millerdff50992002-01-22 23:16:32 +1100671 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000672 char *password;
673
674 debug("Doing password authentication.");
675 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000676 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000677 for (i = 0; i < options.number_of_password_prompts; i++) {
678 if (i != 0)
679 error("Permission denied, please try again.");
680 password = read_passphrase(prompt, 0);
681 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100682 ssh_put_password(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000683 memset(password, 0, strlen(password));
684 xfree(password);
685 packet_send();
686 packet_write_wait();
687
Damien Millerdff50992002-01-22 23:16:32 +1100688 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000689 if (type == SSH_SMSG_SUCCESS)
690 return 1;
691 if (type != SSH_SMSG_FAILURE)
692 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
693 }
694 /* failure */
695 return 0;
696}
697
698/*
699 * SSH1 key exchange
700 */
701void
702ssh_kex(char *host, struct sockaddr *hostaddr)
703{
704 int i;
705 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100706 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000707 int bits, rbits;
708 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000709 u_char session_key[SSH_SESSION_KEY_LENGTH];
710 u_char cookie[8];
711 u_int supported_ciphers;
712 u_int server_flags, client_flags;
Damien Millereba71ba2000-04-29 23:57:08 +1000713 u_int32_t rand = 0;
714
715 debug("Waiting for server public key.");
716
717 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100718 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000719
720 /* Get cookie from the packet. */
721 for (i = 0; i < 8; i++)
722 cookie[i] = packet_get_char();
723
724 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100725 server_key = key_new(KEY_RSA1);
726 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100727 packet_get_bignum(server_key->rsa->e);
728 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000729
Damien Millerda755162002-01-22 23:09:22 +1100730 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000731 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000732 logit("Warning: Server lies about size of server public key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000733 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000734 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000735 }
736 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100737 host_key = key_new(KEY_RSA1);
738 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100739 packet_get_bignum(host_key->rsa->e);
740 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000741
Damien Millerda755162002-01-22 23:09:22 +1100742 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000743 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000744 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000745 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000746 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000747 }
748
749 /* Get protocol flags. */
750 server_flags = packet_get_int();
751 packet_set_protocol_flags(server_flags);
752
753 supported_ciphers = packet_get_int();
754 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100755 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000756
757 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100758 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000759
Damien Millerda755162002-01-22 23:09:22 +1100760 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000761 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000762
763 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
764
Damien Millerda755162002-01-22 23:09:22 +1100765 compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000766
767 /* Generate a session key. */
768 arc4random_stir();
769
770 /*
771 * Generate an encryption key for the session. The key is a 256 bit
772 * random number, interpreted as a 32-byte key, with the least
773 * significant 8 bits being the first byte of the key.
774 */
775 for (i = 0; i < 32; i++) {
776 if (i % 4 == 0)
777 rand = arc4random();
778 session_key[i] = rand & 0xff;
779 rand >>= 8;
780 }
781
782 /*
783 * According to the protocol spec, the first byte of the session key
784 * is the highest byte of the integer. The session key is xored with
785 * the first 16 bytes of the session id.
786 */
Damien Millerda755162002-01-22 23:09:22 +1100787 if ((key = BN_new()) == NULL)
788 fatal("respond_to_rsa_challenge: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000789 BN_set_word(key, 0);
790 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
791 BN_lshift(key, key, 8);
792 if (i < 16)
793 BN_add_word(key, session_key[i] ^ session_id[i]);
794 else
795 BN_add_word(key, session_key[i]);
796 }
797
798 /*
799 * Encrypt the integer using the public key and host key of the
800 * server (key with smaller modulus first).
801 */
Damien Millerda755162002-01-22 23:09:22 +1100802 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000803 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +1100804 if (BN_num_bits(host_key->rsa->n) <
805 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
806 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100807 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100808 BN_num_bits(host_key->rsa->n),
809 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100810 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000811 }
Damien Millerda755162002-01-22 23:09:22 +1100812 rsa_public_encrypt(key, key, server_key->rsa);
813 rsa_public_encrypt(key, key, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000814 } else {
815 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +1100816 if (BN_num_bits(server_key->rsa->n) <
817 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
818 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100819 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100820 BN_num_bits(server_key->rsa->n),
821 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100822 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000823 }
Damien Millerda755162002-01-22 23:09:22 +1100824 rsa_public_encrypt(key, key, host_key->rsa);
825 rsa_public_encrypt(key, key, server_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000826 }
827
828 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +1100829 key_free(server_key);
830 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000831
Damien Millere39cacc2000-11-29 12:18:44 +1100832 if (options.cipher == SSH_CIPHER_NOT_SET) {
833 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
834 options.cipher = ssh_cipher_default;
835 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
836 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +1000837 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +1100838 cipher_name(ssh_cipher_default));
839 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +1000840 }
841 /* Check that the selected cipher is supported. */
842 if (!(supported_ciphers & (1 << options.cipher)))
843 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100844 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +1000845
846 debug("Encryption type: %.100s", cipher_name(options.cipher));
847
848 /* Send the encrypted session key to the server. */
849 packet_start(SSH_CMSG_SESSION_KEY);
850 packet_put_char(options.cipher);
851
852 /* Send the cookie back to the server. */
853 for (i = 0; i < 8; i++)
854 packet_put_char(cookie[i]);
855
856 /* Send and destroy the encrypted encryption key integer. */
857 packet_put_bignum(key);
858 BN_clear_free(key);
859
860 /* Send protocol flags. */
861 packet_put_int(client_flags);
862
863 /* Send the packet now. */
864 packet_send();
865 packet_write_wait();
866
867 debug("Sent encrypted session key.");
868
869 /* Set the encryption key. */
870 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
871
872 /* We will no longer need the session key here. Destroy any extra copies. */
873 memset(session_key, 0, sizeof(session_key));
874
875 /*
876 * Expect a success message from the server. Note that this message
877 * will be received in encrypted form.
878 */
Damien Millerdff50992002-01-22 23:16:32 +1100879 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +1000880
881 debug("Received encrypted confirmation.");
882}
883
884/*
885 * Authenticate user
886 */
887void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000888ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000889 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +1000890{
Ben Lindstromec95ed92001-07-04 04:21:14 +0000891#ifdef KRB5
892 krb5_context context = NULL;
893 krb5_auth_context auth_context = NULL;
894#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000895 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100896
Damien Millereba71ba2000-04-29 23:57:08 +1000897 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000898 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +1000899
900 /* Send the name of the user to log in as on the server. */
901 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000902 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000903 packet_send();
904 packet_write_wait();
905
906 /*
907 * The server should respond with success if no authentication is
908 * needed (the user has no password). Otherwise the server responds
909 * with failure.
910 */
Damien Millerdff50992002-01-22 23:16:32 +1100911 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000912
913 /* check whether the connection was accepted without authentication. */
914 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000915 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000916 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000917 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100918
Ben Lindstromec95ed92001-07-04 04:21:14 +0000919#ifdef KRB5
920 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100921 options.kerberos_authentication) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000922 debug("Trying Kerberos v5 authentication.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100923
Ben Lindstromec95ed92001-07-04 04:21:14 +0000924 if (try_krb5_authentication(&context, &auth_context)) {
Damien Millerdff50992002-01-22 23:16:32 +1100925 type = packet_read();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000926 if (type == SSH_SMSG_SUCCESS)
927 goto success;
928 if (type != SSH_SMSG_FAILURE)
929 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
930 }
Damien Millereba71ba2000-04-29 23:57:08 +1000931 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000932#endif /* KRB5 */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100933
Damien Millereba71ba2000-04-29 23:57:08 +1000934 /*
Damien Millereba71ba2000-04-29 23:57:08 +1000935 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
936 * authentication.
937 */
938 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000939 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000940 for (i = 0; i < sensitive->nkeys; i++) {
941 if (sensitive->keys[i] != NULL &&
942 sensitive->keys[i]->type == KEY_RSA1 &&
943 try_rhosts_rsa_authentication(local_user,
944 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000945 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000946 }
Damien Millereba71ba2000-04-29 23:57:08 +1000947 }
948 /* Try RSA authentication if the server supports it. */
949 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
950 options.rsa_authentication) {
951 /*
952 * Try RSA authentication using the authentication agent. The
953 * agent is tried first because no passphrase is needed for
954 * it, whereas identity files may require passphrases.
955 */
956 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000957 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000958
959 /* Try RSA authentication for each identity. */
960 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000961 if (options.identity_keys[i] != NULL &&
962 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +0000963 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000964 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000965 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000966 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +1000967 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +0000968 options.challenge_response_authentication && !options.batch_mode) {
969 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000970 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000971 }
972 /* Try password authentication if the server supports it. */
973 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
974 options.password_authentication && !options.batch_mode) {
975 char prompt[80];
976
Ben Lindstrome0557162001-02-11 00:00:24 +0000977 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +1000978 server_user, host);
979 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000980 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000981 }
982 /* All authentication methods have failed. Exit with an error message. */
983 fatal("Permission denied.");
984 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000985
986 success:
987#ifdef KRB5
988 /* Try Kerberos v5 TGT passing. */
989 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
990 options.kerberos_tgt_passing && context && auth_context) {
991 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000992 logit("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
Ben Lindstromec95ed92001-07-04 04:21:14 +0000993 send_krb5_tgt(context, auth_context);
994 }
995 if (auth_context)
996 krb5_auth_con_free(context, auth_context);
997 if (context)
998 krb5_free_context(context);
999#endif
Damien Miller40eb1d82001-07-14 12:16:59 +10001000 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +10001001}