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