blob: 166e392e7560695e166dac6a29e7106a6ad357ba [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"
Damien Millerda755162002-01-22 23:09:22 +110016RCSID("$OpenBSD: sshconnect1.c,v 1.43 2001/12/27 18:22:16 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100017
18#include <openssl/bn.h>
Damien Millereba71ba2000-04-29 23:57:08 +100019#include <openssl/evp.h>
20
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#ifdef KRB4
22#include <krb.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +000024#ifdef KRB5
25#include <krb5.h>
26#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#ifdef AFS
28#include <kafs.h>
Ben Lindstromb1985f72001-01-23 00:19:15 +000029#include "radix.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#endif
31
32#include "ssh.h"
33#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "xmalloc.h"
35#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100036#include "buffer.h"
37#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100038#include "mpaux.h"
39#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "readconf.h"
42#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100043#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "sshconnect.h"
45#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "readpass.h"
47#include "cipher.h"
48#include "canohost.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 int plen, clen;
71 Key *key;
72 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +100073
74 /* Get connection to the agent. */
75 auth = ssh_get_authentication_connection();
76 if (!auth)
77 return 0;
78
Damien Millerda755162002-01-22 23:09:22 +110079 if ((challenge = BN_new()) == NULL)
80 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100081 /* Loop through identities served by the agent. */
Damien Millerad833b32000-08-23 10:46:23 +100082 for (key = ssh_get_first_identity(auth, &comment, 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +110083 key != NULL;
84 key = ssh_get_next_identity(auth, &comment, 1)) {
Damien Millereba71ba2000-04-29 23:57:08 +100085
86 /* Try this identity. */
87 debug("Trying RSA authentication via agent with '%.100s'", comment);
88 xfree(comment);
89
90 /* Tell the server that we are willing to authenticate using this key. */
91 packet_start(SSH_CMSG_AUTH_RSA);
Damien Millerad833b32000-08-23 10:46:23 +100092 packet_put_bignum(key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +100093 packet_send();
94 packet_write_wait();
95
96 /* Wait for server's response. */
97 type = packet_read(&plen);
98
99 /* The server sends failure if it doesn\'t like our key or
100 does not support RSA authentication. */
101 if (type == SSH_SMSG_FAILURE) {
102 debug("Server refused our key.");
Damien Millerad833b32000-08-23 10:46:23 +1000103 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000104 continue;
105 }
106 /* Otherwise it should have sent a challenge. */
107 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
108 packet_disconnect("Protocol error during RSA authentication: %d",
109 type);
110
111 packet_get_bignum(challenge, &clen);
112
113 packet_integrity_check(plen, clen, type);
114
115 debug("Received RSA challenge from server.");
116
117 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000118 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
119 /*
120 * The agent failed to authenticate this identifier
121 * although it advertised it supports this. Just
122 * return a wrong value.
123 */
Damien Millereba71ba2000-04-29 23:57:08 +1000124 log("Authentication agent failed to decrypt challenge.");
125 memset(response, 0, sizeof(response));
126 }
Damien Millerad833b32000-08-23 10:46:23 +1000127 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000128 debug("Sending response to RSA challenge.");
129
130 /* Send the decrypted challenge back to the server. */
131 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
132 for (i = 0; i < 16; i++)
133 packet_put_char(response[i]);
134 packet_send();
135 packet_write_wait();
136
137 /* Wait for response from the server. */
138 type = packet_read(&plen);
139
140 /* The server returns success if it accepted the authentication. */
141 if (type == SSH_SMSG_SUCCESS) {
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000142 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000143 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000144 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000145 return 1;
146 }
147 /* Otherwise it should return failure. */
148 if (type != SSH_SMSG_FAILURE)
149 packet_disconnect("Protocol error waiting RSA auth response: %d",
150 type);
151 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000152 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000153 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000154 debug("RSA authentication using agent refused.");
155 return 0;
156}
157
158/*
159 * Computes the proper response to a RSA challenge, and sends the response to
160 * the server.
161 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void
Damien Millereba71ba2000-04-29 23:57:08 +1000163respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
164{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000165 u_char buf[32], response[16];
Damien Millereba71ba2000-04-29 23:57:08 +1000166 MD5_CTX md;
167 int i, len;
168
169 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100170 /* XXX think about Bleichenbacher, too */
171 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
172 packet_disconnect(
173 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000174
175 /* Compute the response. */
176 /* The response is MD5 of decrypted challenge plus session id. */
177 len = BN_num_bytes(challenge);
178 if (len <= 0 || len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100179 packet_disconnect(
180 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000181
182 memset(buf, 0, sizeof(buf));
183 BN_bn2bin(challenge, buf + sizeof(buf) - len);
184 MD5_Init(&md);
185 MD5_Update(&md, buf, 32);
186 MD5_Update(&md, session_id, 16);
187 MD5_Final(response, &md);
188
189 debug("Sending response to host key RSA challenge.");
190
191 /* Send the response back to the server. */
192 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
193 for (i = 0; i < 16; i++)
194 packet_put_char(response[i]);
195 packet_send();
196 packet_write_wait();
197
198 memset(buf, 0, sizeof(buf));
199 memset(response, 0, sizeof(response));
200 memset(&md, 0, sizeof(md));
201}
202
203/*
204 * Checks if the user has authentication file, and if so, tries to authenticate
205 * the user using it.
206 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000208try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000209{
210 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000211 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000212 char buf[300], *passphrase, *comment, *authfile;
Ben Lindstrom05209452001-06-25 05:16:02 +0000213 int i, type, quit, plen, clen;
Damien Millereba71ba2000-04-29 23:57:08 +1000214
Ben Lindstromc5b68002001-07-04 04:52:03 +0000215 public = options.identity_keys[idx];
216 authfile = options.identity_files[idx];
217 comment = xstrdup(authfile);
218
Damien Millereba71ba2000-04-29 23:57:08 +1000219 debug("Trying RSA authentication with key '%.100s'", comment);
220
221 /* Tell the server that we are willing to authenticate using this key. */
222 packet_start(SSH_CMSG_AUTH_RSA);
223 packet_put_bignum(public->rsa->n);
224 packet_send();
225 packet_write_wait();
226
Damien Millereba71ba2000-04-29 23:57:08 +1000227 /* Wait for server's response. */
228 type = packet_read(&plen);
229
230 /*
231 * The server responds with failure if it doesn\'t like our key or
232 * doesn\'t support RSA authentication.
233 */
234 if (type == SSH_SMSG_FAILURE) {
235 debug("Server refused our key.");
236 xfree(comment);
237 return 0;
238 }
239 /* Otherwise, the server should respond with a challenge. */
240 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
241 packet_disconnect("Protocol error during RSA authentication: %d", type);
242
243 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100244 if ((challenge = BN_new()) == NULL)
245 fatal("try_rsa_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000246 packet_get_bignum(challenge, &clen);
247
248 packet_integrity_check(plen, clen, type);
249
250 debug("Received RSA challenge from server.");
251
Damien Millereba71ba2000-04-29 23:57:08 +1000252 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000253 * If the key is not stored in external hardware, we have to
254 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000255 * fails, ask for a passphrase.
256 */
Ben Lindstromc5b68002001-07-04 04:52:03 +0000257 if (public->flags && KEY_FLAG_EXT)
258 private = public;
259 else
260 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000261 if (private == NULL && !options.batch_mode) {
262 snprintf(buf, sizeof(buf),
263 "Enter passphrase for RSA key '%.100s': ", comment);
264 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000265 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000266 if (strcmp(passphrase, "") != 0) {
267 private = key_load_private_type(KEY_RSA1,
268 authfile, passphrase, NULL);
269 quit = 0;
270 } else {
271 debug2("no passphrase given, try next key");
272 quit = 1;
273 }
Damien Millereba71ba2000-04-29 23:57:08 +1000274 memset(passphrase, 0, strlen(passphrase));
275 xfree(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000276 if (private != NULL || quit)
277 break;
278 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000279 }
Damien Millereba71ba2000-04-29 23:57:08 +1000280 }
281 /* We no longer need the comment. */
282 xfree(comment);
283
Ben Lindstrom05209452001-06-25 05:16:02 +0000284 if (private == NULL) {
285 if (!options.batch_mode)
286 error("Bad passphrase.");
287
288 /* Send a dummy response packet to avoid protocol error. */
289 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
290 for (i = 0; i < 16; i++)
291 packet_put_char(0);
292 packet_send();
293 packet_write_wait();
294
295 /* Expect the server to reject it... */
296 packet_read_expect(&plen, SSH_SMSG_FAILURE);
297 BN_clear_free(challenge);
298 return 0;
299 }
300
Damien Millereba71ba2000-04-29 23:57:08 +1000301 /* Compute and send a response to the challenge. */
302 respond_to_rsa_challenge(challenge, private->rsa);
303
Ben Lindstromc5b68002001-07-04 04:52:03 +0000304 /* Destroy the private key unless it in external hardware. */
305 if (!(private->flags & KEY_FLAG_EXT))
306 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000307
308 /* We no longer need the challenge. */
309 BN_clear_free(challenge);
310
311 /* Wait for response from the server. */
312 type = packet_read(&plen);
313 if (type == SSH_SMSG_SUCCESS) {
314 debug("RSA authentication accepted by server.");
315 return 1;
316 }
317 if (type != SSH_SMSG_FAILURE)
318 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
319 debug("RSA authentication refused.");
320 return 0;
321}
322
323/*
324 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
325 * authentication and RSA host authentication.
326 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000327static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000328try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000329{
330 int type;
331 BIGNUM *challenge;
332 int plen, clen;
333
334 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
335
336 /* Tell the server that we are willing to authenticate using this key. */
337 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000338 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000339 packet_put_int(BN_num_bits(host_key->rsa->n));
340 packet_put_bignum(host_key->rsa->e);
341 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000342 packet_send();
343 packet_write_wait();
344
345 /* Wait for server's response. */
346 type = packet_read(&plen);
347
348 /* The server responds with failure if it doesn't admit our
349 .rhosts authentication or doesn't know our host key. */
350 if (type == SSH_SMSG_FAILURE) {
351 debug("Server refused our rhosts authentication or host key.");
352 return 0;
353 }
354 /* Otherwise, the server should respond with a challenge. */
355 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
356 packet_disconnect("Protocol error during RSA authentication: %d", type);
357
358 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100359 if ((challenge = BN_new()) == NULL)
360 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000361 packet_get_bignum(challenge, &clen);
362
363 packet_integrity_check(plen, clen, type);
364
365 debug("Received RSA challenge for host key from server.");
366
367 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000368 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000369
370 /* We no longer need the challenge. */
371 BN_clear_free(challenge);
372
373 /* Wait for response from the server. */
374 type = packet_read(&plen);
375 if (type == SSH_SMSG_SUCCESS) {
376 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
377 return 1;
378 }
379 if (type != SSH_SMSG_FAILURE)
380 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
381 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
382 return 0;
383}
384
385#ifdef KRB4
Ben Lindstrombba81212001-06-25 05:01:22 +0000386static int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000387try_krb4_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000388{
389 KTEXT_ST auth; /* Kerberos data */
390 char *reply;
391 char inst[INST_SZ];
392 char *realm;
393 CREDENTIALS cred;
394 int r, type, plen;
395 socklen_t slen;
396 Key_schedule schedule;
397 u_long checksum, cksum;
398 MSG_DAT msg_data;
399 struct sockaddr_in local, foreign;
400 struct stat st;
401
402 /* Don't do anything if we don't have any tickets. */
403 if (stat(tkt_string(), &st) < 0)
404 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100405
Ben Lindstromec95ed92001-07-04 04:21:14 +0000406 strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)),
407 INST_SZ);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100408
Ben Lindstromec95ed92001-07-04 04:21:14 +0000409 realm = (char *)krb_realmofhost(get_canonical_hostname(1));
Damien Millereba71ba2000-04-29 23:57:08 +1000410 if (!realm) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000411 debug("Kerberos v4: no realm for %s", get_canonical_hostname(1));
Damien Millereba71ba2000-04-29 23:57:08 +1000412 return 0;
413 }
414 /* This can really be anything. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000415 checksum = (u_long)getpid();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100416
Damien Millereba71ba2000-04-29 23:57:08 +1000417 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
418 if (r != KSUCCESS) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000419 debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]);
Damien Millereba71ba2000-04-29 23:57:08 +1000420 return 0;
421 }
422 /* Get session key to decrypt the server's reply with. */
423 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
424 if (r != KSUCCESS) {
425 debug("get_cred failed: %s", krb_err_txt[r]);
426 return 0;
427 }
428 des_key_sched((des_cblock *) cred.session, schedule);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100429
Damien Millereba71ba2000-04-29 23:57:08 +1000430 /* Send authentication info to server. */
431 packet_start(SSH_CMSG_AUTH_KERBEROS);
432 packet_put_string((char *) auth.dat, auth.length);
433 packet_send();
434 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100435
Damien Millereba71ba2000-04-29 23:57:08 +1000436 /* Zero the buffer. */
437 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100438
Damien Millereba71ba2000-04-29 23:57:08 +1000439 slen = sizeof(local);
440 memset(&local, 0, sizeof(local));
441 if (getsockname(packet_get_connection_in(),
Ben Lindstromec95ed92001-07-04 04:21:14 +0000442 (struct sockaddr *)&local, &slen) < 0)
Damien Millereba71ba2000-04-29 23:57:08 +1000443 debug("getsockname failed: %s", strerror(errno));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100444
Damien Millereba71ba2000-04-29 23:57:08 +1000445 slen = sizeof(foreign);
446 memset(&foreign, 0, sizeof(foreign));
447 if (getpeername(packet_get_connection_in(),
Ben Lindstromec95ed92001-07-04 04:21:14 +0000448 (struct sockaddr *)&foreign, &slen) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000449 debug("getpeername failed: %s", strerror(errno));
450 fatal_cleanup();
451 }
452 /* Get server reply. */
453 type = packet_read(&plen);
454 switch (type) {
455 case SSH_SMSG_FAILURE:
456 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000457 debug("Kerberos v4 authentication failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000458 return 0;
459 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100460
Damien Millereba71ba2000-04-29 23:57:08 +1000461 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
462 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000463 debug("Kerberos v4 authentication accepted.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100464
Damien Millereba71ba2000-04-29 23:57:08 +1000465 /* Get server's response. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000466 reply = packet_get_string((u_int *) &auth.length);
Damien Millereba71ba2000-04-29 23:57:08 +1000467 memcpy(auth.dat, reply, auth.length);
468 xfree(reply);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100469
Damien Millereba71ba2000-04-29 23:57:08 +1000470 packet_integrity_check(plen, 4 + auth.length, type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100471
Damien Millereba71ba2000-04-29 23:57:08 +1000472 /*
473 * If his response isn't properly encrypted with the session
474 * key, and the decrypted checksum fails to match, he's
475 * bogus. Bail out.
476 */
477 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
Ben Lindstromec95ed92001-07-04 04:21:14 +0000478 &foreign, &local, &msg_data);
Damien Millereba71ba2000-04-29 23:57:08 +1000479 if (r != KSUCCESS) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000480 debug("Kerberos v4 krb_rd_priv failed: %s",
481 krb_err_txt[r]);
482 packet_disconnect("Kerberos v4 challenge failed!");
Damien Millereba71ba2000-04-29 23:57:08 +1000483 }
484 /* Fetch the (incremented) checksum that we supplied in the request. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000485 memcpy((char *)&cksum, (char *)msg_data.app_data,
486 sizeof(cksum));
Damien Millereba71ba2000-04-29 23:57:08 +1000487 cksum = ntohl(cksum);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100488
Damien Millereba71ba2000-04-29 23:57:08 +1000489 /* If it matches, we're golden. */
490 if (cksum == checksum + 1) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000491 debug("Kerberos v4 challenge successful.");
Damien Millereba71ba2000-04-29 23:57:08 +1000492 return 1;
493 } else
Ben Lindstromec95ed92001-07-04 04:21:14 +0000494 packet_disconnect("Kerberos v4 challenge failed!");
Damien Millereba71ba2000-04-29 23:57:08 +1000495 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100496
Damien Millereba71ba2000-04-29 23:57:08 +1000497 default:
Ben Lindstromec95ed92001-07-04 04:21:14 +0000498 packet_disconnect("Protocol error on Kerberos v4 response: %d", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000499 }
500 return 0;
501}
502
503#endif /* KRB4 */
504
Ben Lindstromec95ed92001-07-04 04:21:14 +0000505#ifdef KRB5
Ben Lindstrombba81212001-06-25 05:01:22 +0000506static int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000507try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
508{
509 krb5_error_code problem;
510 const char *tkfile;
511 struct stat buf;
512 krb5_ccache ccache = NULL;
513 const char *remotehost;
514 krb5_data ap;
515 int type, payload_len;
516 krb5_ap_rep_enc_part *reply = NULL;
517 int ret;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100518
Ben Lindstromec95ed92001-07-04 04:21:14 +0000519 memset(&ap, 0, sizeof(ap));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100520
Ben Lindstromec95ed92001-07-04 04:21:14 +0000521 problem = krb5_init_context(context);
522 if (problem) {
523 debug("Kerberos v5: krb5_init_context failed");
524 ret = 0;
525 goto out;
526 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100527
Ben Lindstromec95ed92001-07-04 04:21:14 +0000528 tkfile = krb5_cc_default_name(*context);
529 if (strncmp(tkfile, "FILE:", 5) == 0)
530 tkfile += 5;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100531
Ben Lindstromec95ed92001-07-04 04:21:14 +0000532 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
533 debug("Kerberos v5: could not get default ccache (permission denied).");
534 ret = 0;
535 goto out;
536 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100537
Ben Lindstromec95ed92001-07-04 04:21:14 +0000538 problem = krb5_cc_default(*context, &ccache);
539 if (problem) {
540 debug("Kerberos v5: krb5_cc_default failed: %s",
541 krb5_get_err_text(*context, problem));
542 ret = 0;
543 goto out;
544 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100545
Ben Lindstromec95ed92001-07-04 04:21:14 +0000546 remotehost = get_canonical_hostname(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100547
Ben Lindstromec95ed92001-07-04 04:21:14 +0000548 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
549 "host", remotehost, NULL, ccache, &ap);
550 if (problem) {
551 debug("Kerberos v5: krb5_mk_req failed: %s",
552 krb5_get_err_text(*context, problem));
553 ret = 0;
554 goto out;
555 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100556
Ben Lindstromec95ed92001-07-04 04:21:14 +0000557 packet_start(SSH_CMSG_AUTH_KERBEROS);
558 packet_put_string((char *) ap.data, ap.length);
559 packet_send();
560 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100561
Ben Lindstromec95ed92001-07-04 04:21:14 +0000562 xfree(ap.data);
563 ap.length = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100564
Ben Lindstromec95ed92001-07-04 04:21:14 +0000565 type = packet_read(&payload_len);
566 switch (type) {
Damien Miller9f0f5c62001-12-21 14:45:46 +1100567 case SSH_SMSG_FAILURE:
568 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
569 debug("Kerberos v5 authentication failed.");
570 ret = 0;
571 break;
572
Ben Lindstromec95ed92001-07-04 04:21:14 +0000573 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
Damien Miller9f0f5c62001-12-21 14:45:46 +1100574 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
575 debug("Kerberos v5 authentication accepted.");
576
577 /* Get server's response. */
578 ap.data = packet_get_string((unsigned int *) &ap.length);
579
580 packet_integrity_check(payload_len, 4 + ap.length, type);
581 /* XXX je to dobre? */
582
583 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
584 if (problem) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000585 ret = 0;
586 }
587 ret = 1;
588 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100589
Ben Lindstromec95ed92001-07-04 04:21:14 +0000590 default:
591 packet_disconnect("Protocol error on Kerberos v5 response: %d",
592 type);
593 ret = 0;
594 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100595
Ben Lindstromec95ed92001-07-04 04:21:14 +0000596 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100597
Ben Lindstromec95ed92001-07-04 04:21:14 +0000598 out:
599 if (ccache != NULL)
600 krb5_cc_close(*context, ccache);
601 if (reply != NULL)
602 krb5_free_ap_rep_enc_part(*context, reply);
603 if (ap.length > 0)
604 krb5_data_free(&ap);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100605
Ben Lindstromec95ed92001-07-04 04:21:14 +0000606 return (ret);
607}
608
609static void
610send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
611{
612 int fd, type, payload_len;
613 krb5_error_code problem;
614 krb5_data outbuf;
615 krb5_ccache ccache = NULL;
616 krb5_creds creds;
617 krb5_kdc_flags flags;
618 const char *remotehost;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100619
Ben Lindstromec95ed92001-07-04 04:21:14 +0000620 memset(&creds, 0, sizeof(creds));
621 memset(&outbuf, 0, sizeof(outbuf));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100622
Ben Lindstromec95ed92001-07-04 04:21:14 +0000623 fd = packet_get_connection_in();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100624
Ben Lindstromec95ed92001-07-04 04:21:14 +0000625 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
626 if (problem)
627 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100628
Ben Lindstromec95ed92001-07-04 04:21:14 +0000629 problem = krb5_cc_default(context, &ccache);
630 if (problem)
631 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100632
Ben Lindstromec95ed92001-07-04 04:21:14 +0000633 problem = krb5_cc_get_principal(context, ccache, &creds.client);
634 if (problem)
635 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100636
Ben Lindstromec95ed92001-07-04 04:21:14 +0000637 problem = krb5_build_principal(context, &creds.server,
638 strlen(creds.client->realm), creds.client->realm,
639 "krbtgt", creds.client->realm, NULL);
640 if (problem)
641 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100642
Ben Lindstromec95ed92001-07-04 04:21:14 +0000643 creds.times.endtime = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100644
Ben Lindstromec95ed92001-07-04 04:21:14 +0000645 flags.i = 0;
646 flags.b.forwarded = 1;
647 flags.b.forwardable = krb5_config_get_bool(context, NULL,
648 "libdefaults", "forwardable", NULL);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100649
Ben Lindstromec95ed92001-07-04 04:21:14 +0000650 remotehost = get_canonical_hostname(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100651
Ben Lindstromec95ed92001-07-04 04:21:14 +0000652 problem = krb5_get_forwarded_creds(context, auth_context,
653 ccache, flags.i, remotehost, &creds, &outbuf);
654 if (problem)
655 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100656
Ben Lindstromec95ed92001-07-04 04:21:14 +0000657 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
658 packet_put_string((char *)outbuf.data, outbuf.length);
659 packet_send();
660 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100661
Ben Lindstromec95ed92001-07-04 04:21:14 +0000662 type = packet_read(&payload_len);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100663
Ben Lindstromec95ed92001-07-04 04:21:14 +0000664 if (type == SSH_SMSG_SUCCESS) {
665 char *pname;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100666
Ben Lindstromec95ed92001-07-04 04:21:14 +0000667 krb5_unparse_name(context, creds.client, &pname);
668 debug("Kerberos v5 TGT forwarded (%s).", pname);
669 xfree(pname);
670 } else
671 debug("Kerberos v5 TGT forwarding failed.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100672
Ben Lindstromec95ed92001-07-04 04:21:14 +0000673 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100674
Ben Lindstromec95ed92001-07-04 04:21:14 +0000675 out:
676 if (problem)
677 debug("Kerberos v5 TGT forwarding failed: %s",
678 krb5_get_err_text(context, problem));
679 if (creds.client)
680 krb5_free_principal(context, creds.client);
681 if (creds.server)
682 krb5_free_principal(context, creds.server);
683 if (ccache)
684 krb5_cc_close(context, ccache);
685 if (outbuf.data)
686 xfree(outbuf.data);
687}
688#endif /* KRB5 */
689
690#ifdef AFS
691static void
692send_krb4_tgt(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000693{
694 CREDENTIALS *creds;
Damien Millereba71ba2000-04-29 23:57:08 +1000695 struct stat st;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000696 char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
697 int problem, type, len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100698
Damien Millereba71ba2000-04-29 23:57:08 +1000699 /* Don't do anything if we don't have any tickets. */
700 if (stat(tkt_string(), &st) < 0)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000701 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100702
Damien Millereba71ba2000-04-29 23:57:08 +1000703 creds = xmalloc(sizeof(*creds));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100704
Ben Lindstromec95ed92001-07-04 04:21:14 +0000705 problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm);
706 if (problem)
707 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100708
Ben Lindstromec95ed92001-07-04 04:21:14 +0000709 problem = krb_get_cred("krbtgt", prealm, prealm, creds);
710 if (problem)
711 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100712
Damien Millereba71ba2000-04-29 23:57:08 +1000713 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000714 problem = RD_AP_EXP;
715 goto out;
Damien Millereba71ba2000-04-29 23:57:08 +1000716 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000717 creds_to_radix(creds, (u_char *)buffer, sizeof(buffer));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100718
Damien Millereba71ba2000-04-29 23:57:08 +1000719 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000720 packet_put_cstring(buffer);
Damien Millereba71ba2000-04-29 23:57:08 +1000721 packet_send();
722 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100723
Ben Lindstromec95ed92001-07-04 04:21:14 +0000724 type = packet_read(&len);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100725
Ben Lindstromec95ed92001-07-04 04:21:14 +0000726 if (type == SSH_SMSG_SUCCESS)
727 debug("Kerberos v4 TGT forwarded (%s%s%s@%s).",
728 creds->pname, creds->pinst[0] ? "." : "",
729 creds->pinst, creds->realm);
730 else
731 debug("Kerberos v4 TGT rejected.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100732
Ben Lindstromec95ed92001-07-04 04:21:14 +0000733 xfree(creds);
734 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100735
Ben Lindstromec95ed92001-07-04 04:21:14 +0000736 out:
737 debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]);
738 xfree(creds);
Damien Millereba71ba2000-04-29 23:57:08 +1000739}
740
Ben Lindstrombba81212001-06-25 05:01:22 +0000741static void
Damien Millereba71ba2000-04-29 23:57:08 +1000742send_afs_tokens(void)
743{
744 CREDENTIALS creds;
745 struct ViceIoctl parms;
746 struct ClearToken ct;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000747 int i, type, len;
Damien Millereba71ba2000-04-29 23:57:08 +1000748 char buf[2048], *p, *server_cell;
749 char buffer[8192];
Damien Miller9f0f5c62001-12-21 14:45:46 +1100750
Damien Millereba71ba2000-04-29 23:57:08 +1000751 /* Move over ktc_GetToken, here's something leaner. */
752 for (i = 0; i < 100; i++) { /* just in case */
753 parms.in = (char *) &i;
754 parms.in_size = sizeof(i);
755 parms.out = buf;
756 parms.out_size = sizeof(buf);
757 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
758 break;
759 p = buf;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100760
Damien Millereba71ba2000-04-29 23:57:08 +1000761 /* Get secret token. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000762 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
Damien Millereba71ba2000-04-29 23:57:08 +1000763 if (creds.ticket_st.length > MAX_KTXT_LEN)
764 break;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000765 p += sizeof(u_int);
Damien Millereba71ba2000-04-29 23:57:08 +1000766 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
767 p += creds.ticket_st.length;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100768
Damien Millereba71ba2000-04-29 23:57:08 +1000769 /* Get clear token. */
770 memcpy(&len, p, sizeof(len));
771 if (len != sizeof(struct ClearToken))
772 break;
773 p += sizeof(len);
774 memcpy(&ct, p, len);
775 p += len;
776 p += sizeof(len); /* primary flag */
777 server_cell = p;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100778
Damien Millereba71ba2000-04-29 23:57:08 +1000779 /* Flesh out our credentials. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000780 strlcpy(creds.service, "afs", sizeof(creds.service));
Damien Millereba71ba2000-04-29 23:57:08 +1000781 creds.instance[0] = '\0';
782 strlcpy(creds.realm, server_cell, REALM_SZ);
783 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
784 creds.issue_date = ct.BeginTimestamp;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000785 creds.lifetime = krb_time_to_life(creds.issue_date,
786 ct.EndTimestamp);
Damien Millereba71ba2000-04-29 23:57:08 +1000787 creds.kvno = ct.AuthHandle;
788 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
789 creds.pinst[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100790
Damien Millereba71ba2000-04-29 23:57:08 +1000791 /* Encode token, ship it off. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000792 if (creds_to_radix(&creds, (u_char *)buffer,
793 sizeof(buffer)) <= 0)
Damien Millereba71ba2000-04-29 23:57:08 +1000794 break;
795 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000796 packet_put_cstring(buffer);
Damien Millereba71ba2000-04-29 23:57:08 +1000797 packet_send();
798 packet_write_wait();
799
800 /* Roger, Roger. Clearance, Clarence. What's your vector,
801 Victor? */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000802 type = packet_read(&len);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100803
Damien Millereba71ba2000-04-29 23:57:08 +1000804 if (type == SSH_SMSG_FAILURE)
805 debug("AFS token for cell %s rejected.", server_cell);
806 else if (type != SSH_SMSG_SUCCESS)
807 packet_disconnect("Protocol error on AFS token response: %d", type);
808 }
809}
810
811#endif /* AFS */
812
813/*
814 * Tries to authenticate with any string-based challenge/response system.
815 * Note that the client code is not tied to s/key or TIS.
816 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000817static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000818try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000819{
820 int type, i;
821 int payload_len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000822 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000823 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000824 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000825
826 debug("Doing challenge response authentication.");
827
Damien Millereba71ba2000-04-29 23:57:08 +1000828 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000829 /* request a challenge */
830 packet_start(SSH_CMSG_AUTH_TIS);
831 packet_send();
832 packet_write_wait();
833
834 type = packet_read(&payload_len);
835 if (type != SSH_SMSG_FAILURE &&
836 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
837 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000838 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000839 }
840 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000841 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000842 return 0;
843 }
844 challenge = packet_get_string(&clen);
845 packet_integrity_check(payload_len, (4 + clen), type);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000846 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100847 strchr(challenge, '\n') ? "" : "\nResponse: ");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000848 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000849 if (i != 0)
850 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000851 if (options.cipher == SSH_CIPHER_NONE)
852 log("WARNING: Encryption is disabled! "
853 "Reponse will be transmitted in clear text.");
854 response = read_passphrase(prompt, 0);
855 if (strcmp(response, "") == 0) {
856 xfree(response);
857 break;
858 }
Damien Millereba71ba2000-04-29 23:57:08 +1000859 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100860 ssh_put_password(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000861 memset(response, 0, strlen(response));
862 xfree(response);
863 packet_send();
864 packet_write_wait();
865 type = packet_read(&payload_len);
866 if (type == SSH_SMSG_SUCCESS)
867 return 1;
868 if (type != SSH_SMSG_FAILURE)
869 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000870 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000871 }
872 /* failure */
873 return 0;
874}
875
876/*
877 * Tries to authenticate with plain passwd authentication.
878 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000879static int
Damien Millereba71ba2000-04-29 23:57:08 +1000880try_password_authentication(char *prompt)
881{
882 int type, i, payload_len;
883 char *password;
884
885 debug("Doing password authentication.");
886 if (options.cipher == SSH_CIPHER_NONE)
887 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
888 for (i = 0; i < options.number_of_password_prompts; i++) {
889 if (i != 0)
890 error("Permission denied, please try again.");
891 password = read_passphrase(prompt, 0);
892 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100893 ssh_put_password(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000894 memset(password, 0, strlen(password));
895 xfree(password);
896 packet_send();
897 packet_write_wait();
898
899 type = packet_read(&payload_len);
900 if (type == SSH_SMSG_SUCCESS)
901 return 1;
902 if (type != SSH_SMSG_FAILURE)
903 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
904 }
905 /* failure */
906 return 0;
907}
908
909/*
910 * SSH1 key exchange
911 */
912void
913ssh_kex(char *host, struct sockaddr *hostaddr)
914{
915 int i;
916 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100917 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000918 int bits, rbits;
919 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000920 u_char session_key[SSH_SESSION_KEY_LENGTH];
921 u_char cookie[8];
922 u_int supported_ciphers;
923 u_int server_flags, client_flags;
Damien Millereba71ba2000-04-29 23:57:08 +1000924 int payload_len, clen, sum_len = 0;
925 u_int32_t rand = 0;
926
927 debug("Waiting for server public key.");
928
929 /* Wait for a public key packet from the server. */
930 packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
931
932 /* Get cookie from the packet. */
933 for (i = 0; i < 8; i++)
934 cookie[i] = packet_get_char();
935
936 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100937 server_key = key_new(KEY_RSA1);
938 bits = packet_get_int();
939 packet_get_bignum(server_key->rsa->e, &clen);
Damien Millereba71ba2000-04-29 23:57:08 +1000940 sum_len += clen;
Damien Millerda755162002-01-22 23:09:22 +1100941 packet_get_bignum(server_key->rsa->n, &clen);
Damien Millereba71ba2000-04-29 23:57:08 +1000942 sum_len += clen;
943
Damien Millerda755162002-01-22 23:09:22 +1100944 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000945 if (bits != rbits) {
946 log("Warning: Server lies about size of server public key: "
947 "actual size is %d bits vs. announced %d.", rbits, bits);
948 log("Warning: This may be due to an old implementation of ssh.");
949 }
950 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100951 host_key = key_new(KEY_RSA1);
952 bits = packet_get_int();
953 packet_get_bignum(host_key->rsa->e, &clen);
Damien Millereba71ba2000-04-29 23:57:08 +1000954 sum_len += clen;
Damien Millerda755162002-01-22 23:09:22 +1100955 packet_get_bignum(host_key->rsa->n, &clen);
Damien Millereba71ba2000-04-29 23:57:08 +1000956 sum_len += clen;
957
Damien Millerda755162002-01-22 23:09:22 +1100958 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000959 if (bits != rbits) {
960 log("Warning: Server lies about size of server host key: "
961 "actual size is %d bits vs. announced %d.", rbits, bits);
962 log("Warning: This may be due to an old implementation of ssh.");
963 }
964
965 /* Get protocol flags. */
966 server_flags = packet_get_int();
967 packet_set_protocol_flags(server_flags);
968
969 supported_ciphers = packet_get_int();
970 supported_authentications = packet_get_int();
971
972 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100973 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000974
975 packet_integrity_check(payload_len,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100976 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
977 SSH_SMSG_PUBLIC_KEY);
Damien Millerda755162002-01-22 23:09:22 +1100978 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000979 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000980
981 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
982
Damien Millerda755162002-01-22 23:09:22 +1100983 compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000984
985 /* Generate a session key. */
986 arc4random_stir();
987
988 /*
989 * Generate an encryption key for the session. The key is a 256 bit
990 * random number, interpreted as a 32-byte key, with the least
991 * significant 8 bits being the first byte of the key.
992 */
993 for (i = 0; i < 32; i++) {
994 if (i % 4 == 0)
995 rand = arc4random();
996 session_key[i] = rand & 0xff;
997 rand >>= 8;
998 }
999
1000 /*
1001 * According to the protocol spec, the first byte of the session key
1002 * is the highest byte of the integer. The session key is xored with
1003 * the first 16 bytes of the session id.
1004 */
Damien Millerda755162002-01-22 23:09:22 +11001005 if ((key = BN_new()) == NULL)
1006 fatal("respond_to_rsa_challenge: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +10001007 BN_set_word(key, 0);
1008 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
1009 BN_lshift(key, key, 8);
1010 if (i < 16)
1011 BN_add_word(key, session_key[i] ^ session_id[i]);
1012 else
1013 BN_add_word(key, session_key[i]);
1014 }
1015
1016 /*
1017 * Encrypt the integer using the public key and host key of the
1018 * server (key with smaller modulus first).
1019 */
Damien Millerda755162002-01-22 23:09:22 +11001020 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +10001021 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +11001022 if (BN_num_bits(host_key->rsa->n) <
1023 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1024 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +11001025 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +11001026 BN_num_bits(host_key->rsa->n),
1027 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +11001028 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +10001029 }
Damien Millerda755162002-01-22 23:09:22 +11001030 rsa_public_encrypt(key, key, server_key->rsa);
1031 rsa_public_encrypt(key, key, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +10001032 } else {
1033 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +11001034 if (BN_num_bits(server_key->rsa->n) <
1035 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1036 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +11001037 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +11001038 BN_num_bits(server_key->rsa->n),
1039 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +11001040 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +10001041 }
Damien Millerda755162002-01-22 23:09:22 +11001042 rsa_public_encrypt(key, key, host_key->rsa);
1043 rsa_public_encrypt(key, key, server_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +10001044 }
1045
1046 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +11001047 key_free(server_key);
1048 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +10001049
Damien Millere39cacc2000-11-29 12:18:44 +11001050 if (options.cipher == SSH_CIPHER_NOT_SET) {
1051 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
1052 options.cipher = ssh_cipher_default;
1053 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
1054 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller30c3d422000-05-09 11:02:59 +10001055 log("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +11001056 cipher_name(ssh_cipher_default));
1057 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +10001058 }
1059 /* Check that the selected cipher is supported. */
1060 if (!(supported_ciphers & (1 << options.cipher)))
1061 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001062 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +10001063
1064 debug("Encryption type: %.100s", cipher_name(options.cipher));
1065
1066 /* Send the encrypted session key to the server. */
1067 packet_start(SSH_CMSG_SESSION_KEY);
1068 packet_put_char(options.cipher);
1069
1070 /* Send the cookie back to the server. */
1071 for (i = 0; i < 8; i++)
1072 packet_put_char(cookie[i]);
1073
1074 /* Send and destroy the encrypted encryption key integer. */
1075 packet_put_bignum(key);
1076 BN_clear_free(key);
1077
1078 /* Send protocol flags. */
1079 packet_put_int(client_flags);
1080
1081 /* Send the packet now. */
1082 packet_send();
1083 packet_write_wait();
1084
1085 debug("Sent encrypted session key.");
1086
1087 /* Set the encryption key. */
1088 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1089
1090 /* We will no longer need the session key here. Destroy any extra copies. */
1091 memset(session_key, 0, sizeof(session_key));
1092
1093 /*
1094 * Expect a success message from the server. Note that this message
1095 * will be received in encrypted form.
1096 */
1097 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1098
1099 debug("Received encrypted confirmation.");
1100}
1101
1102/*
1103 * Authenticate user
1104 */
1105void
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001106ssh_userauth1(const char *local_user, const char *server_user, char *host,
1107 Key **keys, int nkeys)
Damien Millereba71ba2000-04-29 23:57:08 +10001108{
Ben Lindstromec95ed92001-07-04 04:21:14 +00001109#ifdef KRB5
1110 krb5_context context = NULL;
1111 krb5_auth_context auth_context = NULL;
1112#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001113 int i, type;
1114 int payload_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001115
Damien Millereba71ba2000-04-29 23:57:08 +10001116 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001117 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +10001118
1119 /* Send the name of the user to log in as on the server. */
1120 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001121 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +10001122 packet_send();
1123 packet_write_wait();
1124
1125 /*
1126 * The server should respond with success if no authentication is
1127 * needed (the user has no password). Otherwise the server responds
1128 * with failure.
1129 */
1130 type = packet_read(&payload_len);
1131
1132 /* check whether the connection was accepted without authentication. */
1133 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001134 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001135 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001136 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001137
Ben Lindstromec95ed92001-07-04 04:21:14 +00001138#ifdef KRB5
1139 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
Damien Miller9f0f5c62001-12-21 14:45:46 +11001140 options.kerberos_authentication) {
Ben Lindstromec95ed92001-07-04 04:21:14 +00001141 debug("Trying Kerberos v5 authentication.");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001142
Ben Lindstromec95ed92001-07-04 04:21:14 +00001143 if (try_krb5_authentication(&context, &auth_context)) {
1144 type = packet_read(&payload_len);
1145 if (type == SSH_SMSG_SUCCESS)
1146 goto success;
1147 if (type != SSH_SMSG_FAILURE)
1148 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
1149 }
Damien Millereba71ba2000-04-29 23:57:08 +10001150 }
Ben Lindstromec95ed92001-07-04 04:21:14 +00001151#endif /* KRB5 */
Damien Miller9f0f5c62001-12-21 14:45:46 +11001152
Damien Millereba71ba2000-04-29 23:57:08 +10001153#ifdef KRB4
1154 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1155 options.kerberos_authentication) {
Ben Lindstromec95ed92001-07-04 04:21:14 +00001156 debug("Trying Kerberos v4 authentication.");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001157
Ben Lindstromec95ed92001-07-04 04:21:14 +00001158 if (try_krb4_authentication()) {
Damien Millereba71ba2000-04-29 23:57:08 +10001159 type = packet_read(&payload_len);
1160 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001161 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001162 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001163 packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type);
Damien Millereba71ba2000-04-29 23:57:08 +10001164 }
1165 }
1166#endif /* KRB4 */
Damien Miller9f0f5c62001-12-21 14:45:46 +11001167
Damien Millereba71ba2000-04-29 23:57:08 +10001168 /*
1169 * Use rhosts authentication if running in privileged socket and we
1170 * do not wish to remain anonymous.
1171 */
1172 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1173 options.rhosts_authentication) {
1174 debug("Trying rhosts authentication.");
1175 packet_start(SSH_CMSG_AUTH_RHOSTS);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001176 packet_put_cstring(local_user);
Damien Millereba71ba2000-04-29 23:57:08 +10001177 packet_send();
1178 packet_write_wait();
1179
1180 /* The server should respond with success or failure. */
1181 type = packet_read(&payload_len);
1182 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001183 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001184 if (type != SSH_SMSG_FAILURE)
1185 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1186 type);
1187 }
1188 /*
1189 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1190 * authentication.
1191 */
1192 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001193 options.rhosts_rsa_authentication) {
1194 for (i = 0; i < nkeys; i++) {
Ben Lindstrom9cb59af2001-04-17 18:08:15 +00001195 if (keys[i] != NULL && keys[i]->type == KEY_RSA1 &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001196 try_rhosts_rsa_authentication(local_user, keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +00001197 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001198 }
Damien Millereba71ba2000-04-29 23:57:08 +10001199 }
1200 /* Try RSA authentication if the server supports it. */
1201 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1202 options.rsa_authentication) {
1203 /*
1204 * Try RSA authentication using the authentication agent. The
1205 * agent is tried first because no passphrase is needed for
1206 * it, whereas identity files may require passphrases.
1207 */
1208 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +00001209 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001210
1211 /* Try RSA authentication for each identity. */
1212 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001213 if (options.identity_keys[i] != NULL &&
1214 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +00001215 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +00001216 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001217 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001218 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +10001219 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +00001220 options.challenge_response_authentication && !options.batch_mode) {
1221 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +00001222 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001223 }
1224 /* Try password authentication if the server supports it. */
1225 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1226 options.password_authentication && !options.batch_mode) {
1227 char prompt[80];
1228
Ben Lindstrome0557162001-02-11 00:00:24 +00001229 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +10001230 server_user, host);
1231 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +00001232 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001233 }
1234 /* All authentication methods have failed. Exit with an error message. */
1235 fatal("Permission denied.");
1236 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +00001237
1238 success:
1239#ifdef KRB5
1240 /* Try Kerberos v5 TGT passing. */
1241 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1242 options.kerberos_tgt_passing && context && auth_context) {
1243 if (options.cipher == SSH_CIPHER_NONE)
1244 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1245 send_krb5_tgt(context, auth_context);
1246 }
1247 if (auth_context)
1248 krb5_auth_con_free(context, auth_context);
1249 if (context)
1250 krb5_free_context(context);
1251#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +11001252
Ben Lindstromec95ed92001-07-04 04:21:14 +00001253#ifdef AFS
1254 /* Try Kerberos v4 TGT passing if the server supports it. */
1255 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1256 options.kerberos_tgt_passing) {
1257 if (options.cipher == SSH_CIPHER_NONE)
1258 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1259 send_krb4_tgt();
1260 }
1261 /* Try AFS token passing if the server supports it. */
1262 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1263 options.afs_token_passing && k_hasafs()) {
1264 if (options.cipher == SSH_CIPHER_NONE)
1265 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1266 send_afs_tokens();
1267 }
1268#endif /* AFS */
Tim Rice024acc42001-07-04 21:27:20 -07001269
Damien Miller40eb1d82001-07-14 12:16:59 +10001270 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +10001271}