blob: a5fef1036d46f051fe198b3a1d05afe371dc2579 [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 Miller48b03fc2002-01-22 23:11:40 +110016RCSID("$OpenBSD: sshconnect1.c,v 1.45 2001/12/28 12:14:27 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);
Damien Miller48b03fc2002-01-22 23:11:40 +1100112 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000113
114 debug("Received RSA challenge from server.");
115
116 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000117 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
118 /*
119 * The agent failed to authenticate this identifier
120 * although it advertised it supports this. Just
121 * return a wrong value.
122 */
Damien Millereba71ba2000-04-29 23:57:08 +1000123 log("Authentication agent failed to decrypt challenge.");
124 memset(response, 0, sizeof(response));
125 }
Damien Millerad833b32000-08-23 10:46:23 +1000126 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000127 debug("Sending response to RSA challenge.");
128
129 /* Send the decrypted challenge back to the server. */
130 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
131 for (i = 0; i < 16; i++)
132 packet_put_char(response[i]);
133 packet_send();
134 packet_write_wait();
135
136 /* Wait for response from the server. */
137 type = packet_read(&plen);
138
139 /* The server returns success if it accepted the authentication. */
140 if (type == SSH_SMSG_SUCCESS) {
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000141 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000142 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000143 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000144 return 1;
145 }
146 /* Otherwise it should return failure. */
147 if (type != SSH_SMSG_FAILURE)
148 packet_disconnect("Protocol error waiting RSA auth response: %d",
149 type);
150 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000151 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000152 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000153 debug("RSA authentication using agent refused.");
154 return 0;
155}
156
157/*
158 * Computes the proper response to a RSA challenge, and sends the response to
159 * the server.
160 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000161static void
Damien Millereba71ba2000-04-29 23:57:08 +1000162respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
163{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000164 u_char buf[32], response[16];
Damien Millereba71ba2000-04-29 23:57:08 +1000165 MD5_CTX md;
166 int i, len;
167
168 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100169 /* XXX think about Bleichenbacher, too */
170 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
171 packet_disconnect(
172 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000173
174 /* Compute the response. */
175 /* The response is MD5 of decrypted challenge plus session id. */
176 len = BN_num_bytes(challenge);
177 if (len <= 0 || len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100178 packet_disconnect(
179 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000180
181 memset(buf, 0, sizeof(buf));
182 BN_bn2bin(challenge, buf + sizeof(buf) - len);
183 MD5_Init(&md);
184 MD5_Update(&md, buf, 32);
185 MD5_Update(&md, session_id, 16);
186 MD5_Final(response, &md);
187
188 debug("Sending response to host key RSA challenge.");
189
190 /* Send the response back to the server. */
191 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
192 for (i = 0; i < 16; i++)
193 packet_put_char(response[i]);
194 packet_send();
195 packet_write_wait();
196
197 memset(buf, 0, sizeof(buf));
198 memset(response, 0, sizeof(response));
199 memset(&md, 0, sizeof(md));
200}
201
202/*
203 * Checks if the user has authentication file, and if so, tries to authenticate
204 * the user using it.
205 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000206static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000207try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000208{
209 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000210 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000211 char buf[300], *passphrase, *comment, *authfile;
Ben Lindstrom05209452001-06-25 05:16:02 +0000212 int i, type, quit, plen, clen;
Damien Millereba71ba2000-04-29 23:57:08 +1000213
Ben Lindstromc5b68002001-07-04 04:52:03 +0000214 public = options.identity_keys[idx];
215 authfile = options.identity_files[idx];
216 comment = xstrdup(authfile);
217
Damien Millereba71ba2000-04-29 23:57:08 +1000218 debug("Trying RSA authentication with key '%.100s'", comment);
219
220 /* Tell the server that we are willing to authenticate using this key. */
221 packet_start(SSH_CMSG_AUTH_RSA);
222 packet_put_bignum(public->rsa->n);
223 packet_send();
224 packet_write_wait();
225
Damien Millereba71ba2000-04-29 23:57:08 +1000226 /* Wait for server's response. */
227 type = packet_read(&plen);
228
229 /*
230 * The server responds with failure if it doesn\'t like our key or
231 * doesn\'t support RSA authentication.
232 */
233 if (type == SSH_SMSG_FAILURE) {
234 debug("Server refused our key.");
235 xfree(comment);
236 return 0;
237 }
238 /* Otherwise, the server should respond with a challenge. */
239 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
240 packet_disconnect("Protocol error during RSA authentication: %d", type);
241
242 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100243 if ((challenge = BN_new()) == NULL)
244 fatal("try_rsa_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000245 packet_get_bignum(challenge, &clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100246 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000247
248 debug("Received RSA challenge from server.");
249
Damien Millereba71ba2000-04-29 23:57:08 +1000250 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000251 * If the key is not stored in external hardware, we have to
252 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000253 * fails, ask for a passphrase.
254 */
Ben Lindstromc5b68002001-07-04 04:52:03 +0000255 if (public->flags && KEY_FLAG_EXT)
256 private = public;
257 else
258 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000259 if (private == NULL && !options.batch_mode) {
260 snprintf(buf, sizeof(buf),
261 "Enter passphrase for RSA key '%.100s': ", comment);
262 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000263 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000264 if (strcmp(passphrase, "") != 0) {
265 private = key_load_private_type(KEY_RSA1,
266 authfile, passphrase, NULL);
267 quit = 0;
268 } else {
269 debug2("no passphrase given, try next key");
270 quit = 1;
271 }
Damien Millereba71ba2000-04-29 23:57:08 +1000272 memset(passphrase, 0, strlen(passphrase));
273 xfree(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000274 if (private != NULL || quit)
275 break;
276 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000277 }
Damien Millereba71ba2000-04-29 23:57:08 +1000278 }
279 /* We no longer need the comment. */
280 xfree(comment);
281
Ben Lindstrom05209452001-06-25 05:16:02 +0000282 if (private == NULL) {
283 if (!options.batch_mode)
284 error("Bad passphrase.");
285
286 /* Send a dummy response packet to avoid protocol error. */
287 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
288 for (i = 0; i < 16; i++)
289 packet_put_char(0);
290 packet_send();
291 packet_write_wait();
292
293 /* Expect the server to reject it... */
294 packet_read_expect(&plen, SSH_SMSG_FAILURE);
295 BN_clear_free(challenge);
296 return 0;
297 }
298
Damien Millereba71ba2000-04-29 23:57:08 +1000299 /* Compute and send a response to the challenge. */
300 respond_to_rsa_challenge(challenge, private->rsa);
301
Ben Lindstromc5b68002001-07-04 04:52:03 +0000302 /* Destroy the private key unless it in external hardware. */
303 if (!(private->flags & KEY_FLAG_EXT))
304 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000305
306 /* We no longer need the challenge. */
307 BN_clear_free(challenge);
308
309 /* Wait for response from the server. */
310 type = packet_read(&plen);
311 if (type == SSH_SMSG_SUCCESS) {
312 debug("RSA authentication accepted by server.");
313 return 1;
314 }
315 if (type != SSH_SMSG_FAILURE)
316 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
317 debug("RSA authentication refused.");
318 return 0;
319}
320
321/*
322 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
323 * authentication and RSA host authentication.
324 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000325static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000326try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000327{
328 int type;
329 BIGNUM *challenge;
330 int plen, clen;
331
332 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
333
334 /* Tell the server that we are willing to authenticate using this key. */
335 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000336 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000337 packet_put_int(BN_num_bits(host_key->rsa->n));
338 packet_put_bignum(host_key->rsa->e);
339 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000340 packet_send();
341 packet_write_wait();
342
343 /* Wait for server's response. */
344 type = packet_read(&plen);
345
346 /* The server responds with failure if it doesn't admit our
347 .rhosts authentication or doesn't know our host key. */
348 if (type == SSH_SMSG_FAILURE) {
349 debug("Server refused our rhosts authentication or host key.");
350 return 0;
351 }
352 /* Otherwise, the server should respond with a challenge. */
353 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
354 packet_disconnect("Protocol error during RSA authentication: %d", type);
355
356 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100357 if ((challenge = BN_new()) == NULL)
358 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000359 packet_get_bignum(challenge, &clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100360 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000361
362 debug("Received RSA challenge for host key from server.");
363
364 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000365 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000366
367 /* We no longer need the challenge. */
368 BN_clear_free(challenge);
369
370 /* Wait for response from the server. */
371 type = packet_read(&plen);
372 if (type == SSH_SMSG_SUCCESS) {
373 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
374 return 1;
375 }
376 if (type != SSH_SMSG_FAILURE)
377 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
378 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
379 return 0;
380}
381
382#ifdef KRB4
Ben Lindstrombba81212001-06-25 05:01:22 +0000383static int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000384try_krb4_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000385{
386 KTEXT_ST auth; /* Kerberos data */
387 char *reply;
388 char inst[INST_SZ];
389 char *realm;
390 CREDENTIALS cred;
391 int r, type, plen;
392 socklen_t slen;
393 Key_schedule schedule;
394 u_long checksum, cksum;
395 MSG_DAT msg_data;
396 struct sockaddr_in local, foreign;
397 struct stat st;
398
399 /* Don't do anything if we don't have any tickets. */
400 if (stat(tkt_string(), &st) < 0)
401 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100402
Ben Lindstromec95ed92001-07-04 04:21:14 +0000403 strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)),
404 INST_SZ);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100405
Ben Lindstromec95ed92001-07-04 04:21:14 +0000406 realm = (char *)krb_realmofhost(get_canonical_hostname(1));
Damien Millereba71ba2000-04-29 23:57:08 +1000407 if (!realm) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000408 debug("Kerberos v4: no realm for %s", get_canonical_hostname(1));
Damien Millereba71ba2000-04-29 23:57:08 +1000409 return 0;
410 }
411 /* This can really be anything. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000412 checksum = (u_long)getpid();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100413
Damien Millereba71ba2000-04-29 23:57:08 +1000414 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
415 if (r != KSUCCESS) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000416 debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]);
Damien Millereba71ba2000-04-29 23:57:08 +1000417 return 0;
418 }
419 /* Get session key to decrypt the server's reply with. */
420 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
421 if (r != KSUCCESS) {
422 debug("get_cred failed: %s", krb_err_txt[r]);
423 return 0;
424 }
425 des_key_sched((des_cblock *) cred.session, schedule);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100426
Damien Millereba71ba2000-04-29 23:57:08 +1000427 /* Send authentication info to server. */
428 packet_start(SSH_CMSG_AUTH_KERBEROS);
429 packet_put_string((char *) auth.dat, auth.length);
430 packet_send();
431 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100432
Damien Millereba71ba2000-04-29 23:57:08 +1000433 /* Zero the buffer. */
434 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100435
Damien Millereba71ba2000-04-29 23:57:08 +1000436 slen = sizeof(local);
437 memset(&local, 0, sizeof(local));
438 if (getsockname(packet_get_connection_in(),
Ben Lindstromec95ed92001-07-04 04:21:14 +0000439 (struct sockaddr *)&local, &slen) < 0)
Damien Millereba71ba2000-04-29 23:57:08 +1000440 debug("getsockname failed: %s", strerror(errno));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100441
Damien Millereba71ba2000-04-29 23:57:08 +1000442 slen = sizeof(foreign);
443 memset(&foreign, 0, sizeof(foreign));
444 if (getpeername(packet_get_connection_in(),
Ben Lindstromec95ed92001-07-04 04:21:14 +0000445 (struct sockaddr *)&foreign, &slen) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000446 debug("getpeername failed: %s", strerror(errno));
447 fatal_cleanup();
448 }
449 /* Get server reply. */
450 type = packet_read(&plen);
451 switch (type) {
452 case SSH_SMSG_FAILURE:
453 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000454 debug("Kerberos v4 authentication failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000455 return 0;
456 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100457
Damien Millereba71ba2000-04-29 23:57:08 +1000458 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
459 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000460 debug("Kerberos v4 authentication accepted.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100461
Damien Millereba71ba2000-04-29 23:57:08 +1000462 /* Get server's response. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000463 reply = packet_get_string((u_int *) &auth.length);
Damien Millereba71ba2000-04-29 23:57:08 +1000464 memcpy(auth.dat, reply, auth.length);
465 xfree(reply);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100466
Damien Miller48b03fc2002-01-22 23:11:40 +1100467 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100468
Damien Millereba71ba2000-04-29 23:57:08 +1000469 /*
470 * If his response isn't properly encrypted with the session
471 * key, and the decrypted checksum fails to match, he's
472 * bogus. Bail out.
473 */
474 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
Ben Lindstromec95ed92001-07-04 04:21:14 +0000475 &foreign, &local, &msg_data);
Damien Millereba71ba2000-04-29 23:57:08 +1000476 if (r != KSUCCESS) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000477 debug("Kerberos v4 krb_rd_priv failed: %s",
478 krb_err_txt[r]);
479 packet_disconnect("Kerberos v4 challenge failed!");
Damien Millereba71ba2000-04-29 23:57:08 +1000480 }
481 /* Fetch the (incremented) checksum that we supplied in the request. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000482 memcpy((char *)&cksum, (char *)msg_data.app_data,
483 sizeof(cksum));
Damien Millereba71ba2000-04-29 23:57:08 +1000484 cksum = ntohl(cksum);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100485
Damien Millereba71ba2000-04-29 23:57:08 +1000486 /* If it matches, we're golden. */
487 if (cksum == checksum + 1) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000488 debug("Kerberos v4 challenge successful.");
Damien Millereba71ba2000-04-29 23:57:08 +1000489 return 1;
490 } else
Ben Lindstromec95ed92001-07-04 04:21:14 +0000491 packet_disconnect("Kerberos v4 challenge failed!");
Damien Millereba71ba2000-04-29 23:57:08 +1000492 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100493
Damien Millereba71ba2000-04-29 23:57:08 +1000494 default:
Ben Lindstromec95ed92001-07-04 04:21:14 +0000495 packet_disconnect("Protocol error on Kerberos v4 response: %d", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000496 }
497 return 0;
498}
499
500#endif /* KRB4 */
501
Ben Lindstromec95ed92001-07-04 04:21:14 +0000502#ifdef KRB5
Ben Lindstrombba81212001-06-25 05:01:22 +0000503static int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000504try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
505{
506 krb5_error_code problem;
507 const char *tkfile;
508 struct stat buf;
509 krb5_ccache ccache = NULL;
510 const char *remotehost;
511 krb5_data ap;
512 int type, payload_len;
513 krb5_ap_rep_enc_part *reply = NULL;
514 int ret;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100515
Ben Lindstromec95ed92001-07-04 04:21:14 +0000516 memset(&ap, 0, sizeof(ap));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100517
Ben Lindstromec95ed92001-07-04 04:21:14 +0000518 problem = krb5_init_context(context);
519 if (problem) {
520 debug("Kerberos v5: krb5_init_context failed");
521 ret = 0;
522 goto out;
523 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100524
Ben Lindstromec95ed92001-07-04 04:21:14 +0000525 tkfile = krb5_cc_default_name(*context);
526 if (strncmp(tkfile, "FILE:", 5) == 0)
527 tkfile += 5;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100528
Ben Lindstromec95ed92001-07-04 04:21:14 +0000529 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
530 debug("Kerberos v5: could not get default ccache (permission denied).");
531 ret = 0;
532 goto out;
533 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100534
Ben Lindstromec95ed92001-07-04 04:21:14 +0000535 problem = krb5_cc_default(*context, &ccache);
536 if (problem) {
537 debug("Kerberos v5: krb5_cc_default failed: %s",
538 krb5_get_err_text(*context, problem));
539 ret = 0;
540 goto out;
541 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100542
Ben Lindstromec95ed92001-07-04 04:21:14 +0000543 remotehost = get_canonical_hostname(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100544
Ben Lindstromec95ed92001-07-04 04:21:14 +0000545 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
546 "host", remotehost, NULL, ccache, &ap);
547 if (problem) {
548 debug("Kerberos v5: krb5_mk_req failed: %s",
549 krb5_get_err_text(*context, problem));
550 ret = 0;
551 goto out;
552 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100553
Ben Lindstromec95ed92001-07-04 04:21:14 +0000554 packet_start(SSH_CMSG_AUTH_KERBEROS);
555 packet_put_string((char *) ap.data, ap.length);
556 packet_send();
557 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100558
Ben Lindstromec95ed92001-07-04 04:21:14 +0000559 xfree(ap.data);
560 ap.length = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100561
Ben Lindstromec95ed92001-07-04 04:21:14 +0000562 type = packet_read(&payload_len);
563 switch (type) {
Damien Miller9f0f5c62001-12-21 14:45:46 +1100564 case SSH_SMSG_FAILURE:
565 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
566 debug("Kerberos v5 authentication failed.");
567 ret = 0;
568 break;
569
Ben Lindstromec95ed92001-07-04 04:21:14 +0000570 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
Damien Miller9f0f5c62001-12-21 14:45:46 +1100571 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
572 debug("Kerberos v5 authentication accepted.");
573
574 /* Get server's response. */
575 ap.data = packet_get_string((unsigned int *) &ap.length);
Damien Miller48b03fc2002-01-22 23:11:40 +1100576 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100577 /* XXX je to dobre? */
578
579 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
580 if (problem) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000581 ret = 0;
582 }
583 ret = 1;
584 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100585
Ben Lindstromec95ed92001-07-04 04:21:14 +0000586 default:
587 packet_disconnect("Protocol error on Kerberos v5 response: %d",
588 type);
589 ret = 0;
590 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100591
Ben Lindstromec95ed92001-07-04 04:21:14 +0000592 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100593
Ben Lindstromec95ed92001-07-04 04:21:14 +0000594 out:
595 if (ccache != NULL)
596 krb5_cc_close(*context, ccache);
597 if (reply != NULL)
598 krb5_free_ap_rep_enc_part(*context, reply);
599 if (ap.length > 0)
600 krb5_data_free(&ap);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100601
Ben Lindstromec95ed92001-07-04 04:21:14 +0000602 return (ret);
603}
604
605static void
606send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
607{
608 int fd, type, payload_len;
609 krb5_error_code problem;
610 krb5_data outbuf;
611 krb5_ccache ccache = NULL;
612 krb5_creds creds;
613 krb5_kdc_flags flags;
614 const char *remotehost;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100615
Ben Lindstromec95ed92001-07-04 04:21:14 +0000616 memset(&creds, 0, sizeof(creds));
617 memset(&outbuf, 0, sizeof(outbuf));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100618
Ben Lindstromec95ed92001-07-04 04:21:14 +0000619 fd = packet_get_connection_in();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100620
Ben Lindstromec95ed92001-07-04 04:21:14 +0000621 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
622 if (problem)
623 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100624
Ben Lindstromec95ed92001-07-04 04:21:14 +0000625 problem = krb5_cc_default(context, &ccache);
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_get_principal(context, ccache, &creds.client);
630 if (problem)
631 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100632
Ben Lindstromec95ed92001-07-04 04:21:14 +0000633 problem = krb5_build_principal(context, &creds.server,
634 strlen(creds.client->realm), creds.client->realm,
635 "krbtgt", creds.client->realm, NULL);
636 if (problem)
637 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100638
Ben Lindstromec95ed92001-07-04 04:21:14 +0000639 creds.times.endtime = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100640
Ben Lindstromec95ed92001-07-04 04:21:14 +0000641 flags.i = 0;
642 flags.b.forwarded = 1;
643 flags.b.forwardable = krb5_config_get_bool(context, NULL,
644 "libdefaults", "forwardable", NULL);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100645
Ben Lindstromec95ed92001-07-04 04:21:14 +0000646 remotehost = get_canonical_hostname(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100647
Ben Lindstromec95ed92001-07-04 04:21:14 +0000648 problem = krb5_get_forwarded_creds(context, auth_context,
649 ccache, flags.i, remotehost, &creds, &outbuf);
650 if (problem)
651 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100652
Ben Lindstromec95ed92001-07-04 04:21:14 +0000653 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
654 packet_put_string((char *)outbuf.data, outbuf.length);
655 packet_send();
656 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100657
Ben Lindstromec95ed92001-07-04 04:21:14 +0000658 type = packet_read(&payload_len);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100659
Ben Lindstromec95ed92001-07-04 04:21:14 +0000660 if (type == SSH_SMSG_SUCCESS) {
661 char *pname;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100662
Ben Lindstromec95ed92001-07-04 04:21:14 +0000663 krb5_unparse_name(context, creds.client, &pname);
664 debug("Kerberos v5 TGT forwarded (%s).", pname);
665 xfree(pname);
666 } else
667 debug("Kerberos v5 TGT forwarding failed.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100668
Ben Lindstromec95ed92001-07-04 04:21:14 +0000669 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100670
Ben Lindstromec95ed92001-07-04 04:21:14 +0000671 out:
672 if (problem)
673 debug("Kerberos v5 TGT forwarding failed: %s",
674 krb5_get_err_text(context, problem));
675 if (creds.client)
676 krb5_free_principal(context, creds.client);
677 if (creds.server)
678 krb5_free_principal(context, creds.server);
679 if (ccache)
680 krb5_cc_close(context, ccache);
681 if (outbuf.data)
682 xfree(outbuf.data);
683}
684#endif /* KRB5 */
685
686#ifdef AFS
687static void
688send_krb4_tgt(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000689{
690 CREDENTIALS *creds;
Damien Millereba71ba2000-04-29 23:57:08 +1000691 struct stat st;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000692 char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
693 int problem, type, len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100694
Damien Millereba71ba2000-04-29 23:57:08 +1000695 /* Don't do anything if we don't have any tickets. */
696 if (stat(tkt_string(), &st) < 0)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000697 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100698
Damien Millereba71ba2000-04-29 23:57:08 +1000699 creds = xmalloc(sizeof(*creds));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100700
Ben Lindstromec95ed92001-07-04 04:21:14 +0000701 problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm);
702 if (problem)
703 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100704
Ben Lindstromec95ed92001-07-04 04:21:14 +0000705 problem = krb_get_cred("krbtgt", prealm, prealm, creds);
706 if (problem)
707 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100708
Damien Millereba71ba2000-04-29 23:57:08 +1000709 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000710 problem = RD_AP_EXP;
711 goto out;
Damien Millereba71ba2000-04-29 23:57:08 +1000712 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000713 creds_to_radix(creds, (u_char *)buffer, sizeof(buffer));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100714
Damien Millereba71ba2000-04-29 23:57:08 +1000715 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000716 packet_put_cstring(buffer);
Damien Millereba71ba2000-04-29 23:57:08 +1000717 packet_send();
718 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100719
Ben Lindstromec95ed92001-07-04 04:21:14 +0000720 type = packet_read(&len);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100721
Ben Lindstromec95ed92001-07-04 04:21:14 +0000722 if (type == SSH_SMSG_SUCCESS)
723 debug("Kerberos v4 TGT forwarded (%s%s%s@%s).",
724 creds->pname, creds->pinst[0] ? "." : "",
725 creds->pinst, creds->realm);
726 else
727 debug("Kerberos v4 TGT rejected.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100728
Ben Lindstromec95ed92001-07-04 04:21:14 +0000729 xfree(creds);
730 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100731
Ben Lindstromec95ed92001-07-04 04:21:14 +0000732 out:
733 debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]);
734 xfree(creds);
Damien Millereba71ba2000-04-29 23:57:08 +1000735}
736
Ben Lindstrombba81212001-06-25 05:01:22 +0000737static void
Damien Millereba71ba2000-04-29 23:57:08 +1000738send_afs_tokens(void)
739{
740 CREDENTIALS creds;
741 struct ViceIoctl parms;
742 struct ClearToken ct;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000743 int i, type, len;
Damien Millereba71ba2000-04-29 23:57:08 +1000744 char buf[2048], *p, *server_cell;
745 char buffer[8192];
Damien Miller9f0f5c62001-12-21 14:45:46 +1100746
Damien Millereba71ba2000-04-29 23:57:08 +1000747 /* Move over ktc_GetToken, here's something leaner. */
748 for (i = 0; i < 100; i++) { /* just in case */
749 parms.in = (char *) &i;
750 parms.in_size = sizeof(i);
751 parms.out = buf;
752 parms.out_size = sizeof(buf);
753 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
754 break;
755 p = buf;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100756
Damien Millereba71ba2000-04-29 23:57:08 +1000757 /* Get secret token. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000758 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
Damien Millereba71ba2000-04-29 23:57:08 +1000759 if (creds.ticket_st.length > MAX_KTXT_LEN)
760 break;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000761 p += sizeof(u_int);
Damien Millereba71ba2000-04-29 23:57:08 +1000762 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
763 p += creds.ticket_st.length;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100764
Damien Millereba71ba2000-04-29 23:57:08 +1000765 /* Get clear token. */
766 memcpy(&len, p, sizeof(len));
767 if (len != sizeof(struct ClearToken))
768 break;
769 p += sizeof(len);
770 memcpy(&ct, p, len);
771 p += len;
772 p += sizeof(len); /* primary flag */
773 server_cell = p;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100774
Damien Millereba71ba2000-04-29 23:57:08 +1000775 /* Flesh out our credentials. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000776 strlcpy(creds.service, "afs", sizeof(creds.service));
Damien Millereba71ba2000-04-29 23:57:08 +1000777 creds.instance[0] = '\0';
778 strlcpy(creds.realm, server_cell, REALM_SZ);
779 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
780 creds.issue_date = ct.BeginTimestamp;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000781 creds.lifetime = krb_time_to_life(creds.issue_date,
782 ct.EndTimestamp);
Damien Millereba71ba2000-04-29 23:57:08 +1000783 creds.kvno = ct.AuthHandle;
784 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
785 creds.pinst[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100786
Damien Millereba71ba2000-04-29 23:57:08 +1000787 /* Encode token, ship it off. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000788 if (creds_to_radix(&creds, (u_char *)buffer,
789 sizeof(buffer)) <= 0)
Damien Millereba71ba2000-04-29 23:57:08 +1000790 break;
791 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000792 packet_put_cstring(buffer);
Damien Millereba71ba2000-04-29 23:57:08 +1000793 packet_send();
794 packet_write_wait();
795
796 /* Roger, Roger. Clearance, Clarence. What's your vector,
797 Victor? */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000798 type = packet_read(&len);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100799
Damien Millereba71ba2000-04-29 23:57:08 +1000800 if (type == SSH_SMSG_FAILURE)
801 debug("AFS token for cell %s rejected.", server_cell);
802 else if (type != SSH_SMSG_SUCCESS)
803 packet_disconnect("Protocol error on AFS token response: %d", type);
804 }
805}
806
807#endif /* AFS */
808
809/*
810 * Tries to authenticate with any string-based challenge/response system.
811 * Note that the client code is not tied to s/key or TIS.
812 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000813static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000814try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000815{
816 int type, i;
817 int payload_len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000818 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000819 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000820 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000821
822 debug("Doing challenge response authentication.");
823
Damien Millereba71ba2000-04-29 23:57:08 +1000824 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000825 /* request a challenge */
826 packet_start(SSH_CMSG_AUTH_TIS);
827 packet_send();
828 packet_write_wait();
829
830 type = packet_read(&payload_len);
831 if (type != SSH_SMSG_FAILURE &&
832 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
833 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000834 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000835 }
836 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000837 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000838 return 0;
839 }
840 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100841 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000842 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100843 strchr(challenge, '\n') ? "" : "\nResponse: ");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000844 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000845 if (i != 0)
846 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000847 if (options.cipher == SSH_CIPHER_NONE)
848 log("WARNING: Encryption is disabled! "
849 "Reponse will be transmitted in clear text.");
850 response = read_passphrase(prompt, 0);
851 if (strcmp(response, "") == 0) {
852 xfree(response);
853 break;
854 }
Damien Millereba71ba2000-04-29 23:57:08 +1000855 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100856 ssh_put_password(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000857 memset(response, 0, strlen(response));
858 xfree(response);
859 packet_send();
860 packet_write_wait();
861 type = packet_read(&payload_len);
862 if (type == SSH_SMSG_SUCCESS)
863 return 1;
864 if (type != SSH_SMSG_FAILURE)
865 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000866 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000867 }
868 /* failure */
869 return 0;
870}
871
872/*
873 * Tries to authenticate with plain passwd authentication.
874 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000875static int
Damien Millereba71ba2000-04-29 23:57:08 +1000876try_password_authentication(char *prompt)
877{
878 int type, i, payload_len;
879 char *password;
880
881 debug("Doing password authentication.");
882 if (options.cipher == SSH_CIPHER_NONE)
883 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
884 for (i = 0; i < options.number_of_password_prompts; i++) {
885 if (i != 0)
886 error("Permission denied, please try again.");
887 password = read_passphrase(prompt, 0);
888 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100889 ssh_put_password(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000890 memset(password, 0, strlen(password));
891 xfree(password);
892 packet_send();
893 packet_write_wait();
894
895 type = packet_read(&payload_len);
896 if (type == SSH_SMSG_SUCCESS)
897 return 1;
898 if (type != SSH_SMSG_FAILURE)
899 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
900 }
901 /* failure */
902 return 0;
903}
904
905/*
906 * SSH1 key exchange
907 */
908void
909ssh_kex(char *host, struct sockaddr *hostaddr)
910{
911 int i;
912 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100913 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000914 int bits, rbits;
915 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000916 u_char session_key[SSH_SESSION_KEY_LENGTH];
917 u_char cookie[8];
918 u_int supported_ciphers;
919 u_int server_flags, client_flags;
Damien Millereba71ba2000-04-29 23:57:08 +1000920 int payload_len, clen, sum_len = 0;
921 u_int32_t rand = 0;
922
923 debug("Waiting for server public key.");
924
925 /* Wait for a public key packet from the server. */
926 packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
927
928 /* Get cookie from the packet. */
929 for (i = 0; i < 8; i++)
930 cookie[i] = packet_get_char();
931
932 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100933 server_key = key_new(KEY_RSA1);
934 bits = packet_get_int();
935 packet_get_bignum(server_key->rsa->e, &clen);
Damien Millereba71ba2000-04-29 23:57:08 +1000936 sum_len += clen;
Damien Millerda755162002-01-22 23:09:22 +1100937 packet_get_bignum(server_key->rsa->n, &clen);
Damien Millereba71ba2000-04-29 23:57:08 +1000938 sum_len += clen;
939
Damien Millerda755162002-01-22 23:09:22 +1100940 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000941 if (bits != rbits) {
942 log("Warning: Server lies about size of server public key: "
943 "actual size is %d bits vs. announced %d.", rbits, bits);
944 log("Warning: This may be due to an old implementation of ssh.");
945 }
946 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100947 host_key = key_new(KEY_RSA1);
948 bits = packet_get_int();
949 packet_get_bignum(host_key->rsa->e, &clen);
Damien Millereba71ba2000-04-29 23:57:08 +1000950 sum_len += clen;
Damien Millerda755162002-01-22 23:09:22 +1100951 packet_get_bignum(host_key->rsa->n, &clen);
Damien Millereba71ba2000-04-29 23:57:08 +1000952 sum_len += clen;
953
Damien Millerda755162002-01-22 23:09:22 +1100954 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000955 if (bits != rbits) {
956 log("Warning: Server lies about size of server host key: "
957 "actual size is %d bits vs. announced %d.", rbits, bits);
958 log("Warning: This may be due to an old implementation of ssh.");
959 }
960
961 /* Get protocol flags. */
962 server_flags = packet_get_int();
963 packet_set_protocol_flags(server_flags);
964
965 supported_ciphers = packet_get_int();
966 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100967 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000968
969 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100970 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000971
Damien Millerda755162002-01-22 23:09:22 +1100972 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000973 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000974
975 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
976
Damien Millerda755162002-01-22 23:09:22 +1100977 compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000978
979 /* Generate a session key. */
980 arc4random_stir();
981
982 /*
983 * Generate an encryption key for the session. The key is a 256 bit
984 * random number, interpreted as a 32-byte key, with the least
985 * significant 8 bits being the first byte of the key.
986 */
987 for (i = 0; i < 32; i++) {
988 if (i % 4 == 0)
989 rand = arc4random();
990 session_key[i] = rand & 0xff;
991 rand >>= 8;
992 }
993
994 /*
995 * According to the protocol spec, the first byte of the session key
996 * is the highest byte of the integer. The session key is xored with
997 * the first 16 bytes of the session id.
998 */
Damien Millerda755162002-01-22 23:09:22 +1100999 if ((key = BN_new()) == NULL)
1000 fatal("respond_to_rsa_challenge: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +10001001 BN_set_word(key, 0);
1002 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
1003 BN_lshift(key, key, 8);
1004 if (i < 16)
1005 BN_add_word(key, session_key[i] ^ session_id[i]);
1006 else
1007 BN_add_word(key, session_key[i]);
1008 }
1009
1010 /*
1011 * Encrypt the integer using the public key and host key of the
1012 * server (key with smaller modulus first).
1013 */
Damien Millerda755162002-01-22 23:09:22 +11001014 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +10001015 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +11001016 if (BN_num_bits(host_key->rsa->n) <
1017 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1018 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +11001019 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +11001020 BN_num_bits(host_key->rsa->n),
1021 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +11001022 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +10001023 }
Damien Millerda755162002-01-22 23:09:22 +11001024 rsa_public_encrypt(key, key, server_key->rsa);
1025 rsa_public_encrypt(key, key, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +10001026 } else {
1027 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +11001028 if (BN_num_bits(server_key->rsa->n) <
1029 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1030 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +11001031 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +11001032 BN_num_bits(server_key->rsa->n),
1033 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +11001034 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +10001035 }
Damien Millerda755162002-01-22 23:09:22 +11001036 rsa_public_encrypt(key, key, host_key->rsa);
1037 rsa_public_encrypt(key, key, server_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +10001038 }
1039
1040 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +11001041 key_free(server_key);
1042 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +10001043
Damien Millere39cacc2000-11-29 12:18:44 +11001044 if (options.cipher == SSH_CIPHER_NOT_SET) {
1045 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
1046 options.cipher = ssh_cipher_default;
1047 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
1048 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller30c3d422000-05-09 11:02:59 +10001049 log("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +11001050 cipher_name(ssh_cipher_default));
1051 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +10001052 }
1053 /* Check that the selected cipher is supported. */
1054 if (!(supported_ciphers & (1 << options.cipher)))
1055 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001056 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +10001057
1058 debug("Encryption type: %.100s", cipher_name(options.cipher));
1059
1060 /* Send the encrypted session key to the server. */
1061 packet_start(SSH_CMSG_SESSION_KEY);
1062 packet_put_char(options.cipher);
1063
1064 /* Send the cookie back to the server. */
1065 for (i = 0; i < 8; i++)
1066 packet_put_char(cookie[i]);
1067
1068 /* Send and destroy the encrypted encryption key integer. */
1069 packet_put_bignum(key);
1070 BN_clear_free(key);
1071
1072 /* Send protocol flags. */
1073 packet_put_int(client_flags);
1074
1075 /* Send the packet now. */
1076 packet_send();
1077 packet_write_wait();
1078
1079 debug("Sent encrypted session key.");
1080
1081 /* Set the encryption key. */
1082 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1083
1084 /* We will no longer need the session key here. Destroy any extra copies. */
1085 memset(session_key, 0, sizeof(session_key));
1086
1087 /*
1088 * Expect a success message from the server. Note that this message
1089 * will be received in encrypted form.
1090 */
1091 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1092
1093 debug("Received encrypted confirmation.");
1094}
1095
1096/*
1097 * Authenticate user
1098 */
1099void
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001100ssh_userauth1(const char *local_user, const char *server_user, char *host,
1101 Key **keys, int nkeys)
Damien Millereba71ba2000-04-29 23:57:08 +10001102{
Ben Lindstromec95ed92001-07-04 04:21:14 +00001103#ifdef KRB5
1104 krb5_context context = NULL;
1105 krb5_auth_context auth_context = NULL;
1106#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001107 int i, type;
1108 int payload_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001109
Damien Millereba71ba2000-04-29 23:57:08 +10001110 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001111 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +10001112
1113 /* Send the name of the user to log in as on the server. */
1114 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001115 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +10001116 packet_send();
1117 packet_write_wait();
1118
1119 /*
1120 * The server should respond with success if no authentication is
1121 * needed (the user has no password). Otherwise the server responds
1122 * with failure.
1123 */
1124 type = packet_read(&payload_len);
1125
1126 /* check whether the connection was accepted without authentication. */
1127 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001128 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001129 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001130 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001131
Ben Lindstromec95ed92001-07-04 04:21:14 +00001132#ifdef KRB5
1133 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
Damien Miller9f0f5c62001-12-21 14:45:46 +11001134 options.kerberos_authentication) {
Ben Lindstromec95ed92001-07-04 04:21:14 +00001135 debug("Trying Kerberos v5 authentication.");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001136
Ben Lindstromec95ed92001-07-04 04:21:14 +00001137 if (try_krb5_authentication(&context, &auth_context)) {
1138 type = packet_read(&payload_len);
1139 if (type == SSH_SMSG_SUCCESS)
1140 goto success;
1141 if (type != SSH_SMSG_FAILURE)
1142 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
1143 }
Damien Millereba71ba2000-04-29 23:57:08 +10001144 }
Ben Lindstromec95ed92001-07-04 04:21:14 +00001145#endif /* KRB5 */
Damien Miller9f0f5c62001-12-21 14:45:46 +11001146
Damien Millereba71ba2000-04-29 23:57:08 +10001147#ifdef KRB4
1148 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1149 options.kerberos_authentication) {
Ben Lindstromec95ed92001-07-04 04:21:14 +00001150 debug("Trying Kerberos v4 authentication.");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001151
Ben Lindstromec95ed92001-07-04 04:21:14 +00001152 if (try_krb4_authentication()) {
Damien Millereba71ba2000-04-29 23:57:08 +10001153 type = packet_read(&payload_len);
1154 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001155 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001156 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001157 packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type);
Damien Millereba71ba2000-04-29 23:57:08 +10001158 }
1159 }
1160#endif /* KRB4 */
Damien Miller9f0f5c62001-12-21 14:45:46 +11001161
Damien Millereba71ba2000-04-29 23:57:08 +10001162 /*
1163 * Use rhosts authentication if running in privileged socket and we
1164 * do not wish to remain anonymous.
1165 */
1166 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1167 options.rhosts_authentication) {
1168 debug("Trying rhosts authentication.");
1169 packet_start(SSH_CMSG_AUTH_RHOSTS);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001170 packet_put_cstring(local_user);
Damien Millereba71ba2000-04-29 23:57:08 +10001171 packet_send();
1172 packet_write_wait();
1173
1174 /* The server should respond with success or failure. */
1175 type = packet_read(&payload_len);
1176 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001177 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001178 if (type != SSH_SMSG_FAILURE)
1179 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1180 type);
1181 }
1182 /*
1183 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1184 * authentication.
1185 */
1186 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001187 options.rhosts_rsa_authentication) {
1188 for (i = 0; i < nkeys; i++) {
Ben Lindstrom9cb59af2001-04-17 18:08:15 +00001189 if (keys[i] != NULL && keys[i]->type == KEY_RSA1 &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001190 try_rhosts_rsa_authentication(local_user, keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +00001191 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001192 }
Damien Millereba71ba2000-04-29 23:57:08 +10001193 }
1194 /* Try RSA authentication if the server supports it. */
1195 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1196 options.rsa_authentication) {
1197 /*
1198 * Try RSA authentication using the authentication agent. The
1199 * agent is tried first because no passphrase is needed for
1200 * it, whereas identity files may require passphrases.
1201 */
1202 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +00001203 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001204
1205 /* Try RSA authentication for each identity. */
1206 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001207 if (options.identity_keys[i] != NULL &&
1208 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +00001209 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +00001210 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001211 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001212 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +10001213 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +00001214 options.challenge_response_authentication && !options.batch_mode) {
1215 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +00001216 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001217 }
1218 /* Try password authentication if the server supports it. */
1219 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1220 options.password_authentication && !options.batch_mode) {
1221 char prompt[80];
1222
Ben Lindstrome0557162001-02-11 00:00:24 +00001223 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +10001224 server_user, host);
1225 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +00001226 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001227 }
1228 /* All authentication methods have failed. Exit with an error message. */
1229 fatal("Permission denied.");
1230 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +00001231
1232 success:
1233#ifdef KRB5
1234 /* Try Kerberos v5 TGT passing. */
1235 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1236 options.kerberos_tgt_passing && context && auth_context) {
1237 if (options.cipher == SSH_CIPHER_NONE)
1238 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1239 send_krb5_tgt(context, auth_context);
1240 }
1241 if (auth_context)
1242 krb5_auth_con_free(context, auth_context);
1243 if (context)
1244 krb5_free_context(context);
1245#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +11001246
Ben Lindstromec95ed92001-07-04 04:21:14 +00001247#ifdef AFS
1248 /* Try Kerberos v4 TGT passing if the server supports it. */
1249 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1250 options.kerberos_tgt_passing) {
1251 if (options.cipher == SSH_CIPHER_NONE)
1252 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1253 send_krb4_tgt();
1254 }
1255 /* Try AFS token passing if the server supports it. */
1256 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1257 options.afs_token_passing && k_hasafs()) {
1258 if (options.cipher == SSH_CIPHER_NONE)
1259 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1260 send_afs_tokens();
1261 }
1262#endif /* AFS */
Tim Rice024acc42001-07-04 21:27:20 -07001263
Damien Miller40eb1d82001-07-14 12:16:59 +10001264 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +10001265}