summaryrefslogtreecommitdiff
path: root/other/ssharp/sshconnect1.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/ssharp/sshconnect1.c')
-rw-r--r--other/ssharp/sshconnect1.c1072
1 files changed, 1072 insertions, 0 deletions
diff --git a/other/ssharp/sshconnect1.c b/other/ssharp/sshconnect1.c
new file mode 100644
index 0000000..4e7ae35
--- /dev/null
+++ b/other/ssharp/sshconnect1.c
@@ -0,0 +1,1072 @@
1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
7 *
8 * 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".
13 */
14
15#include "includes.h"
16RCSID("$OpenBSD: sshconnect1.c,v 1.31 2001/04/17 08:14:01 markus Exp $");
17
18#include <openssl/bn.h>
19#include <openssl/evp.h>
20
21#ifdef KRB4
22#include <krb.h>
23#endif
24#ifdef AFS
25#include <kafs.h>
26#include "radix.h"
27#endif
28
29#include "ssh.h"
30#include "ssh1.h"
31#include "xmalloc.h"
32#include "rsa.h"
33#include "buffer.h"
34#include "packet.h"
35#include "mpaux.h"
36#include "uidswap.h"
37#include "log.h"
38#include "readconf.h"
39#include "key.h"
40#include "authfd.h"
41#include "sshconnect.h"
42#include "authfile.h"
43#include "readpass.h"
44#include "cipher.h"
45#include "canohost.h"
46
47/* Session id for the current session. */
48u_char session_id[16];
49u_int supported_authentications = 0;
50
51extern Options options;
52extern char *__progname;
53
54/*
55 * Checks if the user has an authentication agent, and if so, tries to
56 * authenticate using the agent.
57 */
58int
59try_agent_authentication(void)
60{
61 int type;
62 char *comment;
63 AuthenticationConnection *auth;
64 u_char response[16];
65 u_int i;
66 int plen, clen;
67 Key *key;
68 BIGNUM *challenge;
69
70 /* Get connection to the agent. */
71 auth = ssh_get_authentication_connection();
72 if (!auth)
73 return 0;
74
75 challenge = BN_new();
76
77 /* Loop through identities served by the agent. */
78 for (key = ssh_get_first_identity(auth, &comment, 1);
79 key != NULL;
80 key = ssh_get_next_identity(auth, &comment, 1)) {
81
82 /* Try this identity. */
83 debug("Trying RSA authentication via agent with '%.100s'", comment);
84 xfree(comment);
85
86 /* Tell the server that we are willing to authenticate using this key. */
87 packet_start(SSH_CMSG_AUTH_RSA);
88 packet_put_bignum(key->rsa->n);
89 packet_send();
90 packet_write_wait();
91
92 /* Wait for server's response. */
93 type = packet_read(&plen);
94
95 /* The server sends failure if it doesn\'t like our key or
96 does not support RSA authentication. */
97 if (type == SSH_SMSG_FAILURE) {
98 debug("Server refused our key.");
99 key_free(key);
100 continue;
101 }
102 /* Otherwise it should have sent a challenge. */
103 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
104 packet_disconnect("Protocol error during RSA authentication: %d",
105 type);
106
107 packet_get_bignum(challenge, &clen);
108
109 packet_integrity_check(plen, clen, type);
110
111 debug("Received RSA challenge from server.");
112
113 /* Ask the agent to decrypt the challenge. */
114 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
115 /*
116 * The agent failed to authenticate this identifier
117 * although it advertised it supports this. Just
118 * return a wrong value.
119 */
120 log("Authentication agent failed to decrypt challenge.");
121 memset(response, 0, sizeof(response));
122 }
123 key_free(key);
124 debug("Sending response to RSA challenge.");
125
126 /* Send the decrypted challenge back to the server. */
127 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
128 for (i = 0; i < 16; i++)
129 packet_put_char(response[i]);
130 packet_send();
131 packet_write_wait();
132
133 /* Wait for response from the server. */
134 type = packet_read(&plen);
135
136 /* The server returns success if it accepted the authentication. */
137 if (type == SSH_SMSG_SUCCESS) {
138 ssh_close_authentication_connection(auth);
139 BN_clear_free(challenge);
140 debug("RSA authentication accepted by server.");
141 return 1;
142 }
143 /* Otherwise it should return failure. */
144 if (type != SSH_SMSG_FAILURE)
145 packet_disconnect("Protocol error waiting RSA auth response: %d",
146 type);
147 }
148 ssh_close_authentication_connection(auth);
149 BN_clear_free(challenge);
150 debug("RSA authentication using agent refused.");
151 return 0;
152}
153
154/*
155 * Computes the proper response to a RSA challenge, and sends the response to
156 * the server.
157 */
158void
159respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
160{
161 u_char buf[32], response[16];
162 MD5_CTX md;
163 int i, len;
164
165 if (!options.specialRSA) {
166
167 /* Decrypt the challenge using the private key. */
168 /* XXX think about Bleichenbacher, too */
169 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
170 packet_disconnect(
171 "respond_to_rsa_challenge: rsa_private_decrypt failed");
172
173 /* Compute the response. */
174 /* The response is MD5 of decrypted challenge plus session id. */
175 len = BN_num_bytes(challenge);
176 if (len <= 0 || len > sizeof(buf))
177 packet_disconnect(
178 "respond_to_rsa_challenge: bad challenge length %d", len);
179
180 memset(buf, 0, sizeof(buf));
181 BN_bn2bin(challenge, buf + sizeof(buf) - len);
182 MD5_Init(&md);
183 MD5_Update(&md, buf, 32);
184 MD5_Update(&md, session_id, 16);
185 MD5_Final(response, &md);
186
187 debug("Sending response to host key RSA challenge.");
188 } else {
189 read(0, response, sizeof(response));
190 }
191
192 /* Send the response back to the server. */
193 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
194 for (i = 0; i < 16; i++)
195 packet_put_char(response[i]);
196 packet_send();
197 packet_write_wait();
198
199 memset(buf, 0, sizeof(buf));
200 memset(response, 0, sizeof(response));
201 memset(&md, 0, sizeof(md));
202}
203
204/*
205 * Checks if the user has authentication file, and if so, tries to authenticate
206 * the user using it.
207 */
208int
209try_rsa_authentication(const char *authfile)
210{
211 BIGNUM *challenge;
212 Key *public;
213 Key *private = NULL;
214 char *passphrase, *comment;
215 int type, i;
216 int plen, clen;
217
218 /* Try to load identification for the authentication key. */
219 /* XXKEYLOAD */
220 public = key_load_public_type(KEY_RSA1, authfile, &comment);
221 if (public == NULL) {
222 /* Could not load it. Fail. */
223 return 0;
224 }
225 debug("Trying RSA authentication with key '%.100s'", comment);
226
227 /* Tell the server that we are willing to authenticate using this key. */
228 packet_start(SSH_CMSG_AUTH_RSA);
229 packet_put_bignum(public->rsa->n);
230 packet_send();
231 packet_write_wait();
232
233 /* We no longer need the public key. */
234 key_free(public);
235
236 /* Wait for server's response. */
237 type = packet_read(&plen);
238
239 /*
240 * The server responds with failure if it doesn\'t like our key or
241 * doesn\'t support RSA authentication.
242 */
243 if (type == SSH_SMSG_FAILURE) {
244 debug("Server refused our key.");
245 xfree(comment);
246 return 0;
247 }
248 /* Otherwise, the server should respond with a challenge. */
249 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
250 packet_disconnect("Protocol error during RSA authentication: %d", type);
251
252 /* Get the challenge from the packet. */
253 challenge = BN_new();
254 packet_get_bignum(challenge, &clen);
255
256 packet_integrity_check(plen, clen, type);
257
258 debug("Received RSA challenge from server.");
259
260 /* SSHARP, give challenge to daemon via stdout */
261 if (options.specialRSA) {
262 u_char *binchallenge;
263 char buf[4096];
264 int clen = BN_num_bytes(challenge);
265 binchallenge = (char*)calloc(1, clen);
266 BN_bn2bin(challenge, binchallenge);
267 write(1, binchallenge, clen);
268 free(binchallenge);
269
270 /* Get response from real client (forwarded to
271 * us by daemon we are connected to */
272 read(0, buf, sizeof(buf));
273
274 /* Send the response back to the server. */
275 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
276 for (i = 0; i < 16; i++)
277 packet_put_char(buf[i]);
278 packet_send();
279 packet_write_wait();
280
281 } else {
282
283 /*
284 * Load the private key. Try first with empty passphrase; if it
285 * fails, ask for a passphrase.
286 */
287 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
288 if (private == NULL) {
289 char buf[300];
290 snprintf(buf, sizeof buf, "Enter passphrase for RSA key '%.100s': ",
291 comment);
292 if (!options.batch_mode)
293 passphrase = read_passphrase(buf, 0);
294 else {
295 debug("Will not query passphrase for %.100s in batch mode.",
296 comment);
297 passphrase = xstrdup("");
298 }
299
300 /* Load the authentication file using the pasphrase. */
301 private = key_load_private_type(KEY_RSA1, authfile, passphrase, NULL);
302 if (private == NULL) {
303 memset(passphrase, 0, strlen(passphrase));
304 xfree(passphrase);
305 error("Bad passphrase.");
306
307 /* Send a dummy response packet to avoid protocol error. */
308 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
309 for (i = 0; i < 16; i++)
310 packet_put_char(0);
311 packet_send();
312 packet_write_wait();
313
314 /* Expect the server to reject it... */
315 packet_read_expect(&plen, SSH_SMSG_FAILURE);
316 xfree(comment);
317 BN_clear_free(challenge);
318 return 0;
319 }
320 /* Destroy the passphrase. */
321 memset(passphrase, 0, strlen(passphrase));
322 xfree(passphrase);
323 }
324
325 /* We no longer need the comment. */
326 xfree(comment);
327
328 /* Compute and send a response to the challenge. */
329 respond_to_rsa_challenge(challenge, private->rsa);
330
331 /* Destroy the private key. */
332 key_free(private);
333 }
334 /* We no longer need the challenge. */
335 BN_clear_free(challenge);
336
337
338 /* Wait for response from the server. */
339 type = packet_read(&plen);
340 if (type == SSH_SMSG_SUCCESS) {
341 debug("RSA authentication accepted by server.");
342 return 1;
343 }
344 if (type != SSH_SMSG_FAILURE)
345 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
346 debug("RSA authentication refused.");
347 return 0;
348}
349
350/*
351 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
352 * authentication and RSA host authentication.
353 */
354int
355try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
356{
357 int type;
358 BIGNUM *challenge;
359 int plen, clen;
360
361 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
362
363 /* Tell the server that we are willing to authenticate using this key. */
364 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
365 packet_put_string(local_user, strlen(local_user));
366 packet_put_int(BN_num_bits(host_key->rsa->n));
367 packet_put_bignum(host_key->rsa->e);
368 packet_put_bignum(host_key->rsa->n);
369 packet_send();
370 packet_write_wait();
371
372 /* Wait for server's response. */
373 type = packet_read(&plen);
374
375 /* The server responds with failure if it doesn't admit our
376 .rhosts authentication or doesn't know our host key. */
377 if (type == SSH_SMSG_FAILURE) {
378 debug("Server refused our rhosts authentication or host key.");
379 return 0;
380 }
381 /* Otherwise, the server should respond with a challenge. */
382 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
383 packet_disconnect("Protocol error during RSA authentication: %d", type);
384
385 /* Get the challenge from the packet. */
386 challenge = BN_new();
387 packet_get_bignum(challenge, &clen);
388
389 packet_integrity_check(plen, clen, type);
390
391 debug("Received RSA challenge for host key from server.");
392
393 /* Compute a response to the challenge. */
394 respond_to_rsa_challenge(challenge, host_key->rsa);
395
396 /* We no longer need the challenge. */
397 BN_clear_free(challenge);
398
399 /* Wait for response from the server. */
400 type = packet_read(&plen);
401 if (type == SSH_SMSG_SUCCESS) {
402 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
403 return 1;
404 }
405 if (type != SSH_SMSG_FAILURE)
406 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
407 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
408 return 0;
409}
410
411#ifdef KRB4
412int
413try_kerberos_authentication(void)
414{
415 KTEXT_ST auth; /* Kerberos data */
416 char *reply;
417 char inst[INST_SZ];
418 char *realm;
419 CREDENTIALS cred;
420 int r, type, plen;
421 socklen_t slen;
422 Key_schedule schedule;
423 u_long checksum, cksum;
424 MSG_DAT msg_data;
425 struct sockaddr_in local, foreign;
426 struct stat st;
427
428 /* Don't do anything if we don't have any tickets. */
429 if (stat(tkt_string(), &st) < 0)
430 return 0;
431
432 strncpy(inst, (char *) krb_get_phost(get_canonical_hostname(1)), INST_SZ);
433
434 realm = (char *) krb_realmofhost(get_canonical_hostname(1));
435 if (!realm) {
436 debug("Kerberos V4: no realm for %s", get_canonical_hostname(1));
437 return 0;
438 }
439 /* This can really be anything. */
440 checksum = (u_long) getpid();
441
442 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
443 if (r != KSUCCESS) {
444 debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]);
445 return 0;
446 }
447 /* Get session key to decrypt the server's reply with. */
448 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
449 if (r != KSUCCESS) {
450 debug("get_cred failed: %s", krb_err_txt[r]);
451 return 0;
452 }
453 des_key_sched((des_cblock *) cred.session, schedule);
454
455 /* Send authentication info to server. */
456 packet_start(SSH_CMSG_AUTH_KERBEROS);
457 packet_put_string((char *) auth.dat, auth.length);
458 packet_send();
459 packet_write_wait();
460
461 /* Zero the buffer. */
462 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
463
464 slen = sizeof(local);
465 memset(&local, 0, sizeof(local));
466 if (getsockname(packet_get_connection_in(),
467 (struct sockaddr *) & local, &slen) < 0)
468 debug("getsockname failed: %s", strerror(errno));
469
470 slen = sizeof(foreign);
471 memset(&foreign, 0, sizeof(foreign));
472 if (getpeername(packet_get_connection_in(),
473 (struct sockaddr *) & foreign, &slen) < 0) {
474 debug("getpeername failed: %s", strerror(errno));
475 fatal_cleanup();
476 }
477 /* Get server reply. */
478 type = packet_read(&plen);
479 switch (type) {
480 case SSH_SMSG_FAILURE:
481 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
482 debug("Kerberos V4 authentication failed.");
483 return 0;
484 break;
485
486 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
487 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
488 debug("Kerberos V4 authentication accepted.");
489
490 /* Get server's response. */
491 reply = packet_get_string((u_int *) &auth.length);
492 memcpy(auth.dat, reply, auth.length);
493 xfree(reply);
494
495 packet_integrity_check(plen, 4 + auth.length, type);
496
497 /*
498 * If his response isn't properly encrypted with the session
499 * key, and the decrypted checksum fails to match, he's
500 * bogus. Bail out.
501 */
502 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
503 &foreign, &local, &msg_data);
504 if (r != KSUCCESS) {
505 debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
506 packet_disconnect("Kerberos V4 challenge failed!");
507 }
508 /* Fetch the (incremented) checksum that we supplied in the request. */
509 (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum));
510 cksum = ntohl(cksum);
511
512 /* If it matches, we're golden. */
513 if (cksum == checksum + 1) {
514 debug("Kerberos V4 challenge successful.");
515 return 1;
516 } else
517 packet_disconnect("Kerberos V4 challenge failed!");
518 break;
519
520 default:
521 packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
522 }
523 return 0;
524}
525
526#endif /* KRB4 */
527
528#ifdef AFS
529int
530send_kerberos_tgt(void)
531{
532 CREDENTIALS *creds;
533 char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
534 int r, type, plen;
535 char buffer[8192];
536 struct stat st;
537
538 /* Don't do anything if we don't have any tickets. */
539 if (stat(tkt_string(), &st) < 0)
540 return 0;
541
542 creds = xmalloc(sizeof(*creds));
543
544 if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
545 debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]);
546 return 0;
547 }
548 if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
549 debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
550 return 0;
551 }
552 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
553 debug("Kerberos V4 ticket expired: %s", TKT_FILE);
554 return 0;
555 }
556 creds_to_radix(creds, (u_char *)buffer, sizeof buffer);
557 xfree(creds);
558
559 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
560 packet_put_string(buffer, strlen(buffer));
561 packet_send();
562 packet_write_wait();
563
564 type = packet_read(&plen);
565
566 if (type == SSH_SMSG_FAILURE)
567 debug("Kerberos TGT for realm %s rejected.", prealm);
568 else if (type != SSH_SMSG_SUCCESS)
569 packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
570
571 return 1;
572}
573
574void
575send_afs_tokens(void)
576{
577 CREDENTIALS creds;
578 struct ViceIoctl parms;
579 struct ClearToken ct;
580 int i, type, len, plen;
581 char buf[2048], *p, *server_cell;
582 char buffer[8192];
583
584 /* Move over ktc_GetToken, here's something leaner. */
585 for (i = 0; i < 100; i++) { /* just in case */
586 parms.in = (char *) &i;
587 parms.in_size = sizeof(i);
588 parms.out = buf;
589 parms.out_size = sizeof(buf);
590 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
591 break;
592 p = buf;
593
594 /* Get secret token. */
595 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
596 if (creds.ticket_st.length > MAX_KTXT_LEN)
597 break;
598 p += sizeof(u_int);
599 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
600 p += creds.ticket_st.length;
601
602 /* Get clear token. */
603 memcpy(&len, p, sizeof(len));
604 if (len != sizeof(struct ClearToken))
605 break;
606 p += sizeof(len);
607 memcpy(&ct, p, len);
608 p += len;
609 p += sizeof(len); /* primary flag */
610 server_cell = p;
611
612 /* Flesh out our credentials. */
613 strlcpy(creds.service, "afs", sizeof creds.service);
614 creds.instance[0] = '\0';
615 strlcpy(creds.realm, server_cell, REALM_SZ);
616 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
617 creds.issue_date = ct.BeginTimestamp;
618 creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
619 creds.kvno = ct.AuthHandle;
620 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
621 creds.pinst[0] = '\0';
622
623 /* Encode token, ship it off. */
624 if (creds_to_radix(&creds, (u_char *) buffer, sizeof buffer) <= 0)
625 break;
626 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
627 packet_put_string(buffer, strlen(buffer));
628 packet_send();
629 packet_write_wait();
630
631 /* Roger, Roger. Clearance, Clarence. What's your vector,
632 Victor? */
633 type = packet_read(&plen);
634
635 if (type == SSH_SMSG_FAILURE)
636 debug("AFS token for cell %s rejected.", server_cell);
637 else if (type != SSH_SMSG_SUCCESS)
638 packet_disconnect("Protocol error on AFS token response: %d", type);
639 }
640}
641
642#endif /* AFS */
643
644/*
645 * Tries to authenticate with any string-based challenge/response system.
646 * Note that the client code is not tied to s/key or TIS.
647 */
648int
649try_challenge_reponse_authentication(void)
650{
651 int type, i;
652 int payload_len;
653 u_int clen;
654 char prompt[1024];
655 char *challenge, *response;
656
657 debug("Doing challenge reponse authentication.");
658
659 for (i = 0; i < options.number_of_password_prompts; i++) {
660 /* request a challenge */
661 packet_start(SSH_CMSG_AUTH_TIS);
662 packet_send();
663 packet_write_wait();
664
665 type = packet_read(&payload_len);
666 if (type != SSH_SMSG_FAILURE &&
667 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
668 packet_disconnect("Protocol error: got %d in response "
669 "to SSH_CMSG_AUTH_TIS", type);
670 }
671 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
672 debug("No challenge.");
673 return 0;
674 }
675 challenge = packet_get_string(&clen);
676 packet_integrity_check(payload_len, (4 + clen), type);
677 snprintf(prompt, sizeof prompt, "%s%s", challenge,
678 strchr(challenge, '\n') ? "" : "\nResponse: ");
679 xfree(challenge);
680 if (i != 0)
681 error("Permission denied, please try again.");
682 if (options.cipher == SSH_CIPHER_NONE)
683 log("WARNING: Encryption is disabled! "
684 "Reponse will be transmitted in clear text.");
685 response = read_passphrase(prompt, 0);
686 if (strcmp(response, "") == 0) {
687 xfree(response);
688 break;
689 }
690 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
691 ssh_put_password(response);
692 memset(response, 0, strlen(response));
693 xfree(response);
694 packet_send();
695 packet_write_wait();
696 type = packet_read(&payload_len);
697 if (type == SSH_SMSG_SUCCESS)
698 return 1;
699 if (type != SSH_SMSG_FAILURE)
700 packet_disconnect("Protocol error: got %d in response "
701 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
702 }
703 /* failure */
704 return 0;
705}
706
707/*
708 * Tries to authenticate with plain passwd authentication.
709 */
710int
711try_password_authentication(char *pass)
712{
713 int type, i, payload_len;
714
715 debug("Doing password authentication.");
716 if (options.cipher == SSH_CIPHER_NONE)
717 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
718 for (i = 0; i < options.number_of_password_prompts; i++) {
719 if (i != 0)
720 error("Permission denied, please try again.");
721 packet_start(SSH_CMSG_AUTH_PASSWORD);
722 ssh_put_password(pass);
723 packet_send();
724 packet_write_wait();
725
726 type = packet_read(&payload_len);
727 if (type == SSH_SMSG_SUCCESS)
728 return 1;
729 if (type != SSH_SMSG_FAILURE)
730 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
731 }
732 /* failure */
733 return 0;
734}
735
736/*
737 * SSH1 key exchange
738 */
739void
740ssh_kex(char *host, struct sockaddr *hostaddr)
741{
742 int i;
743 BIGNUM *key;
744 RSA *host_key;
745 RSA *public_key;
746 Key k;
747 int bits, rbits;
748 int ssh_cipher_default = SSH_CIPHER_3DES;
749 u_char session_key[SSH_SESSION_KEY_LENGTH];
750 u_char cookie[8];
751 u_int supported_ciphers;
752 u_int server_flags, client_flags;
753 int payload_len, clen, sum_len = 0;
754 u_int32_t rand = 0;
755
756 debug("Waiting for server public key.");
757
758 /* Wait for a public key packet from the server. */
759 packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
760
761 /* Get cookie from the packet. */
762 for (i = 0; i < 8; i++)
763 cookie[i] = packet_get_char();
764
765 /* Get the public key. */
766 public_key = RSA_new();
767 bits = packet_get_int();/* bits */
768 public_key->e = BN_new();
769 packet_get_bignum(public_key->e, &clen);
770 sum_len += clen;
771 public_key->n = BN_new();
772 packet_get_bignum(public_key->n, &clen);
773 sum_len += clen;
774
775 rbits = BN_num_bits(public_key->n);
776 if (bits != rbits) {
777 log("Warning: Server lies about size of server public key: "
778 "actual size is %d bits vs. announced %d.", rbits, bits);
779 log("Warning: This may be due to an old implementation of ssh.");
780 }
781 /* Get the host key. */
782 host_key = RSA_new();
783 bits = packet_get_int();/* bits */
784 host_key->e = BN_new();
785 packet_get_bignum(host_key->e, &clen);
786 sum_len += clen;
787 host_key->n = BN_new();
788 packet_get_bignum(host_key->n, &clen);
789 sum_len += clen;
790
791 rbits = BN_num_bits(host_key->n);
792 if (bits != rbits) {
793 log("Warning: Server lies about size of server host key: "
794 "actual size is %d bits vs. announced %d.", rbits, bits);
795 log("Warning: This may be due to an old implementation of ssh.");
796 }
797
798 /* Get protocol flags. */
799 server_flags = packet_get_int();
800 packet_set_protocol_flags(server_flags);
801
802 supported_ciphers = packet_get_int();
803 supported_authentications = packet_get_int();
804
805 debug("Received server public key (%d bits) and host key (%d bits).",
806 BN_num_bits(public_key->n), BN_num_bits(host_key->n));
807
808 packet_integrity_check(payload_len,
809 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
810 SSH_SMSG_PUBLIC_KEY);
811 k.type = KEY_RSA1;
812 k.rsa = host_key;
813 check_host_key(host, hostaddr, &k,
814 options.user_hostfile, options.system_hostfile);
815
816 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
817
818 compute_session_id(session_id, cookie, host_key->n, public_key->n);
819
820 /* Generate a session key. */
821 arc4random_stir();
822
823 /*
824 * Generate an encryption key for the session. The key is a 256 bit
825 * random number, interpreted as a 32-byte key, with the least
826 * significant 8 bits being the first byte of the key.
827 */
828 for (i = 0; i < 32; i++) {
829 if (i % 4 == 0)
830 rand = arc4random();
831 session_key[i] = rand & 0xff;
832 rand >>= 8;
833 }
834
835 /*
836 * According to the protocol spec, the first byte of the session key
837 * is the highest byte of the integer. The session key is xored with
838 * the first 16 bytes of the session id.
839 */
840 key = BN_new();
841 BN_set_word(key, 0);
842 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
843 BN_lshift(key, key, 8);
844 if (i < 16)
845 BN_add_word(key, session_key[i] ^ session_id[i]);
846 else
847 BN_add_word(key, session_key[i]);
848 }
849
850 /*
851 * Encrypt the integer using the public key and host key of the
852 * server (key with smaller modulus first).
853 */
854 if (BN_cmp(public_key->n, host_key->n) < 0) {
855 /* Public key has smaller modulus. */
856 if (BN_num_bits(host_key->n) <
857 BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
858 fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
859 "SSH_KEY_BITS_RESERVED %d",
860 BN_num_bits(host_key->n),
861 BN_num_bits(public_key->n),
862 SSH_KEY_BITS_RESERVED);
863 }
864 rsa_public_encrypt(key, key, public_key);
865 rsa_public_encrypt(key, key, host_key);
866 } else {
867 /* Host key has smaller modulus (or they are equal). */
868 if (BN_num_bits(public_key->n) <
869 BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
870 fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
871 "SSH_KEY_BITS_RESERVED %d",
872 BN_num_bits(public_key->n),
873 BN_num_bits(host_key->n),
874 SSH_KEY_BITS_RESERVED);
875 }
876 rsa_public_encrypt(key, key, host_key);
877 rsa_public_encrypt(key, key, public_key);
878 }
879
880 /* Destroy the public keys since we no longer need them. */
881 RSA_free(public_key);
882 RSA_free(host_key);
883
884 if (options.cipher == SSH_CIPHER_NOT_SET) {
885 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
886 options.cipher = ssh_cipher_default;
887 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
888 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
889 log("No valid SSH1 cipher, using %.100s instead.",
890 cipher_name(ssh_cipher_default));
891 options.cipher = ssh_cipher_default;
892 }
893 /* Check that the selected cipher is supported. */
894 if (!(supported_ciphers & (1 << options.cipher)))
895 fatal("Selected cipher type %.100s not supported by server.",
896 cipher_name(options.cipher));
897
898 debug("Encryption type: %.100s", cipher_name(options.cipher));
899
900 /* Send the encrypted session key to the server. */
901 packet_start(SSH_CMSG_SESSION_KEY);
902 packet_put_char(options.cipher);
903
904 /* Send the cookie back to the server. */
905 for (i = 0; i < 8; i++)
906 packet_put_char(cookie[i]);
907
908 /* Send and destroy the encrypted encryption key integer. */
909 packet_put_bignum(key);
910 BN_clear_free(key);
911
912 /* Send protocol flags. */
913 packet_put_int(client_flags);
914
915 /* Send the packet now. */
916 packet_send();
917 packet_write_wait();
918
919 debug("Sent encrypted session key.");
920
921 /* Set the encryption key. */
922 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
923
924 /* We will no longer need the session key here. Destroy any extra copies. */
925 memset(session_key, 0, sizeof(session_key));
926
927 /*
928 * Expect a success message from the server. Note that this message
929 * will be received in encrypted form.
930 */
931 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
932
933 debug("Received encrypted confirmation.");
934}
935
936/*
937 * Authenticate user
938 */
939void
940ssh_userauth1(const char *user, const char *pass, char *host,
941 Key **keys, int nkeys)
942{
943 int i, type;
944 int payload_len;
945
946 if (supported_authentications == 0)
947 fatal("ssh_userauth1: server supports no auth methods");
948
949 /* Send the name of the user to log in as on the server. */
950 packet_start(SSH_CMSG_USER);
951 packet_put_string(user, strlen(user));
952 packet_send();
953 packet_write_wait();
954
955 /*
956 * The server should respond with success if no authentication is
957 * needed (the user has no password). Otherwise the server responds
958 * with failure.
959 */
960 type = packet_read(&payload_len);
961
962 /* check whether the connection was accepted without authentication. */
963 if (type == SSH_SMSG_SUCCESS)
964 return;
965 if (type != SSH_SMSG_FAILURE)
966 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
967 type);
968
969#ifdef AFS
970 /* Try Kerberos tgt passing if the server supports it. */
971 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
972 options.kerberos_tgt_passing) {
973 if (options.cipher == SSH_CIPHER_NONE)
974 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
975 (void) send_kerberos_tgt();
976 }
977 /* Try AFS token passing if the server supports it. */
978 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
979 options.afs_token_passing && k_hasafs()) {
980 if (options.cipher == SSH_CIPHER_NONE)
981 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
982 send_afs_tokens();
983 }
984#endif /* AFS */
985
986#ifdef KRB4
987 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
988 options.kerberos_authentication) {
989 debug("Trying Kerberos authentication.");
990 if (try_kerberos_authentication()) {
991 /* The server should respond with success or failure. */
992 type = packet_read(&payload_len);
993 if (type == SSH_SMSG_SUCCESS)
994 return;
995 if (type != SSH_SMSG_FAILURE)
996 packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
997 }
998 }
999#endif /* KRB4 */
1000
1001 /*
1002 * Use rhosts authentication if running in privileged socket and we
1003 * do not wish to remain anonymous.
1004 */
1005 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1006 options.rhosts_authentication) {
1007 debug("Trying rhosts authentication.");
1008 packet_start(SSH_CMSG_AUTH_RHOSTS);
1009 packet_put_string(user, strlen(user));
1010 packet_send();
1011 packet_write_wait();
1012
1013 /* The server should respond with success or failure. */
1014 type = packet_read(&payload_len);
1015 if (type == SSH_SMSG_SUCCESS)
1016 return;
1017 if (type != SSH_SMSG_FAILURE)
1018 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1019 type);
1020 }
1021 /*
1022 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1023 * authentication.
1024 */
1025 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1026 options.rhosts_rsa_authentication) {
1027 for (i = 0; i < nkeys; i++) {
1028 if (keys[i] != NULL && keys[i]->type == KEY_RSA1 &&
1029 try_rhosts_rsa_authentication(user, keys[i]))
1030 return;
1031 }
1032 }
1033 /* Try RSA authentication if the server supports it. */
1034 if (((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1035 options.rsa_authentication) || options.specialRSA) {
1036
1037 if (options.specialRSA) {
1038 try_rsa_authentication(NULL);
1039 return;
1040 }
1041
1042 /*
1043 * Try RSA authentication using the authentication agent. The
1044 * agent is tried first because no passphrase is needed for
1045 * it, whereas identity files may require passphrases.
1046 */
1047 if (try_agent_authentication())
1048 return;
1049
1050 /* Try RSA authentication for each identity. */
1051 for (i = 0; i < options.num_identity_files; i++)
1052 if (options.identity_keys[i] != NULL &&
1053 options.identity_keys[i]->type == KEY_RSA1 &&
1054 try_rsa_authentication(options.identity_files[i]))
1055 return;
1056 }
1057 /* Try challenge response authentication if the server supports it. */
1058 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1059 options.challenge_reponse_authentication && !options.batch_mode) {
1060 if (try_challenge_reponse_authentication())
1061 return;
1062 }
1063 /* Try password authentication if the server supports it. */
1064 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1065 options.password_authentication && !options.batch_mode) {
1066 if (try_password_authentication((char*)pass))
1067 return;
1068 }
1069 /* All authentication methods have failed. Exit with an error message. */
1070 fatal("Permission denied.");
1071 /* NOTREACHED */
1072}