Annotation of src/usr.bin/ssh/sshconnect1.c, Revision 1.16
1.1 markus 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: *
1.6 deraadt 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".
1.1 markus 13: */
14:
15: #include "includes.h"
1.16 ! markus 16: RCSID("$OpenBSD: sshconnect1.c,v 1.15 2001/01/16 23:58:09 deraadt Exp $");
1.1 markus 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"
28: #include "mpaux.h"
29: #include "uidswap.h"
30: #include "readconf.h"
31: #include "key.h"
1.4 markus 32: #include "authfd.h"
1.1 markus 33: #include "sshconnect.h"
34: #include "authfile.h"
35:
36: /* Session id for the current session. */
1.13 markus 37: u_char session_id[16];
38: u_int supported_authentications = 0;
1.1 markus 39:
40: extern Options options;
41: extern char *__progname;
42:
43: /*
44: * Checks if the user has an authentication agent, and if so, tries to
45: * authenticate using the agent.
46: */
47: int
48: try_agent_authentication()
49: {
1.5 markus 50: int type;
1.1 markus 51: char *comment;
52: AuthenticationConnection *auth;
1.13 markus 53: u_char response[16];
54: u_int i;
1.5 markus 55: int plen, clen;
56: Key *key;
57: BIGNUM *challenge;
1.1 markus 58:
59: /* Get connection to the agent. */
60: auth = ssh_get_authentication_connection();
61: if (!auth)
62: return 0;
63:
64: challenge = BN_new();
65:
66: /* Loop through identities served by the agent. */
1.5 markus 67: for (key = ssh_get_first_identity(auth, &comment, 1);
68: key != NULL;
69: key = ssh_get_next_identity(auth, &comment, 1)) {
1.1 markus 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);
1.5 markus 77: packet_put_bignum(key->rsa->n);
1.1 markus 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.");
1.5 markus 88: key_free(key);
1.1 markus 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. */
1.5 markus 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: */
1.1 markus 109: log("Authentication agent failed to decrypt challenge.");
110: memset(response, 0, sizeof(response));
111: }
1.5 markus 112: key_free(key);
1.1 markus 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) {
1.14 markus 127: ssh_close_authentication_connection(auth);
1.5 markus 128: BN_clear_free(challenge);
1.1 markus 129: debug("RSA authentication accepted by server.");
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: }
1.14 markus 137: ssh_close_authentication_connection(auth);
1.1 markus 138: BN_clear_free(challenge);
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: */
147: void
148: respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
149: {
1.13 markus 150: u_char buf[32], response[16];
1.1 markus 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: */
189: int
190: try_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. */
1.9 markus 200: public = key_new(KEY_RSA1);
1.1 markus 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:
1.9 markus 241: private = key_new(KEY_RSA1);
1.1 markus 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);
1.14 markus 274: key_free(private);
275: BN_clear_free(challenge);
1.1 markus 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: */
310: int
311: try_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
368: int
369: try_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. */
1.13 markus 447: reply = packet_get_string((u_int *) &auth.length);
1.1 markus 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
485: int
486: send_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: }
1.13 markus 512: creds_to_radix(creds, (u_char *)buffer, sizeof buffer);
1.1 markus 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:
530: void
531: send_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. */
1.13 markus 551: memcpy(&creds.ticket_st.length, p, sizeof(u_int));
1.1 markus 552: if (creds.ticket_st.length > MAX_KTXT_LEN)
553: break;
1.13 markus 554: p += sizeof(u_int);
1.1 markus 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. */
1.15 deraadt 580: if (creds_to_radix(&creds, (u_char *) buffer, sizeof buffer) <= 0)
1.1 markus 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: */
604: int
605: try_skey_authentication()
606: {
607: int type, i;
608: int payload_len;
1.13 markus 609: u_int clen;
1.12 markus 610: char prompt[1024];
1.1 markus 611: char *challenge, *response;
612:
613: debug("Doing skey authentication.");
614:
1.12 markus 615: for (i = 0; i < options.number_of_password_prompts; i++) {
616: /* request a challenge */
617: packet_start(SSH_CMSG_AUTH_TIS);
618: packet_send();
619: packet_write_wait();
1.1 markus 620:
1.12 markus 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);
1.16 ! markus 633: snprintf(prompt, sizeof prompt, "%s%s", challenge,
! 634: strchr(challenge, '\n') ? "" : "\nResponse: ");
1.12 markus 635: xfree(challenge);
1.1 markus 636: if (i != 0)
637: error("Permission denied, please try again.");
1.12 markus 638: if (options.cipher == SSH_CIPHER_NONE)
639: log("WARNING: Encryption is disabled! "
640: "Reponse will be transmitted in clear text.");
641: response = read_passphrase(prompt, 0);
642: if (strcmp(response, "") == 0) {
643: xfree(response);
644: break;
645: }
1.1 markus 646: packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
647: packet_put_string(response, strlen(response));
648: memset(response, 0, strlen(response));
649: xfree(response);
650: packet_send();
651: packet_write_wait();
652: type = packet_read(&payload_len);
653: if (type == SSH_SMSG_SUCCESS)
654: return 1;
655: if (type != SSH_SMSG_FAILURE)
656: packet_disconnect("Protocol error: got %d in response "
1.12 markus 657: "to skey-auth-reponse", type);
1.1 markus 658: }
659: /* failure */
660: return 0;
661: }
662:
663: /*
664: * Tries to authenticate with plain passwd authentication.
665: */
666: int
667: try_password_authentication(char *prompt)
668: {
669: int type, i, payload_len;
670: char *password;
671:
672: debug("Doing password authentication.");
673: if (options.cipher == SSH_CIPHER_NONE)
674: log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
675: for (i = 0; i < options.number_of_password_prompts; i++) {
676: if (i != 0)
677: error("Permission denied, please try again.");
678: password = read_passphrase(prompt, 0);
679: packet_start(SSH_CMSG_AUTH_PASSWORD);
680: packet_put_string(password, strlen(password));
681: memset(password, 0, strlen(password));
682: xfree(password);
683: packet_send();
684: packet_write_wait();
685:
686: type = packet_read(&payload_len);
687: if (type == SSH_SMSG_SUCCESS)
688: return 1;
689: if (type != SSH_SMSG_FAILURE)
690: packet_disconnect("Protocol error: got %d in response to passwd auth", type);
691: }
692: /* failure */
693: return 0;
694: }
695:
696: /*
697: * SSH1 key exchange
698: */
699: void
700: ssh_kex(char *host, struct sockaddr *hostaddr)
701: {
702: int i;
703: BIGNUM *key;
704: RSA *host_key;
705: RSA *public_key;
706: Key k;
707: int bits, rbits;
708: int ssh_cipher_default = SSH_CIPHER_3DES;
1.13 markus 709: u_char session_key[SSH_SESSION_KEY_LENGTH];
710: u_char cookie[8];
711: u_int supported_ciphers;
712: u_int server_flags, client_flags;
1.1 markus 713: int payload_len, clen, sum_len = 0;
714: u_int32_t rand = 0;
715:
716: debug("Waiting for server public key.");
717:
718: /* Wait for a public key packet from the server. */
719: packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
720:
721: /* Get cookie from the packet. */
722: for (i = 0; i < 8; i++)
723: cookie[i] = packet_get_char();
724:
725: /* Get the public key. */
726: public_key = RSA_new();
727: bits = packet_get_int();/* bits */
728: public_key->e = BN_new();
729: packet_get_bignum(public_key->e, &clen);
730: sum_len += clen;
731: public_key->n = BN_new();
732: packet_get_bignum(public_key->n, &clen);
733: sum_len += clen;
734:
735: rbits = BN_num_bits(public_key->n);
736: if (bits != rbits) {
737: log("Warning: Server lies about size of server public key: "
738: "actual size is %d bits vs. announced %d.", rbits, bits);
739: log("Warning: This may be due to an old implementation of ssh.");
740: }
741: /* Get the host key. */
742: host_key = RSA_new();
743: bits = packet_get_int();/* bits */
744: host_key->e = BN_new();
745: packet_get_bignum(host_key->e, &clen);
746: sum_len += clen;
747: host_key->n = BN_new();
748: packet_get_bignum(host_key->n, &clen);
749: sum_len += clen;
750:
751: rbits = BN_num_bits(host_key->n);
752: if (bits != rbits) {
753: log("Warning: Server lies about size of server host key: "
754: "actual size is %d bits vs. announced %d.", rbits, bits);
755: log("Warning: This may be due to an old implementation of ssh.");
756: }
757:
758: /* Get protocol flags. */
759: server_flags = packet_get_int();
760: packet_set_protocol_flags(server_flags);
761:
762: supported_ciphers = packet_get_int();
763: supported_authentications = packet_get_int();
764:
765: debug("Received server public key (%d bits) and host key (%d bits).",
766: BN_num_bits(public_key->n), BN_num_bits(host_key->n));
767:
768: packet_integrity_check(payload_len,
769: 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
770: SSH_SMSG_PUBLIC_KEY);
1.9 markus 771: k.type = KEY_RSA1;
1.1 markus 772: k.rsa = host_key;
773: check_host_key(host, hostaddr, &k,
774: options.user_hostfile, options.system_hostfile);
775:
776: client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
777:
778: compute_session_id(session_id, cookie, host_key->n, public_key->n);
779:
780: /* Generate a session key. */
781: arc4random_stir();
782:
783: /*
784: * Generate an encryption key for the session. The key is a 256 bit
785: * random number, interpreted as a 32-byte key, with the least
786: * significant 8 bits being the first byte of the key.
787: */
788: for (i = 0; i < 32; i++) {
789: if (i % 4 == 0)
790: rand = arc4random();
791: session_key[i] = rand & 0xff;
792: rand >>= 8;
793: }
794:
795: /*
796: * According to the protocol spec, the first byte of the session key
797: * is the highest byte of the integer. The session key is xored with
798: * the first 16 bytes of the session id.
799: */
800: key = BN_new();
801: BN_set_word(key, 0);
802: for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
803: BN_lshift(key, key, 8);
804: if (i < 16)
805: BN_add_word(key, session_key[i] ^ session_id[i]);
806: else
807: BN_add_word(key, session_key[i]);
808: }
809:
810: /*
811: * Encrypt the integer using the public key and host key of the
812: * server (key with smaller modulus first).
813: */
814: if (BN_cmp(public_key->n, host_key->n) < 0) {
815: /* Public key has smaller modulus. */
816: if (BN_num_bits(host_key->n) <
817: BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
818: fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
819: "SSH_KEY_BITS_RESERVED %d",
820: BN_num_bits(host_key->n),
821: BN_num_bits(public_key->n),
822: SSH_KEY_BITS_RESERVED);
823: }
824: rsa_public_encrypt(key, key, public_key);
825: rsa_public_encrypt(key, key, host_key);
826: } else {
827: /* Host key has smaller modulus (or they are equal). */
828: if (BN_num_bits(public_key->n) <
829: BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
830: fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
831: "SSH_KEY_BITS_RESERVED %d",
832: BN_num_bits(public_key->n),
833: BN_num_bits(host_key->n),
834: SSH_KEY_BITS_RESERVED);
835: }
836: rsa_public_encrypt(key, key, host_key);
837: rsa_public_encrypt(key, key, public_key);
838: }
839:
840: /* Destroy the public keys since we no longer need them. */
841: RSA_free(public_key);
842: RSA_free(host_key);
843:
1.11 markus 844: if (options.cipher == SSH_CIPHER_NOT_SET) {
845: if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
846: options.cipher = ssh_cipher_default;
847: } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
1.10 markus 848: !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
1.3 markus 849: log("No valid SSH1 cipher, using %.100s instead.",
1.7 markus 850: cipher_name(ssh_cipher_default));
851: options.cipher = ssh_cipher_default;
1.1 markus 852: }
853: /* Check that the selected cipher is supported. */
854: if (!(supported_ciphers & (1 << options.cipher)))
855: fatal("Selected cipher type %.100s not supported by server.",
856: cipher_name(options.cipher));
857:
858: debug("Encryption type: %.100s", cipher_name(options.cipher));
859:
860: /* Send the encrypted session key to the server. */
861: packet_start(SSH_CMSG_SESSION_KEY);
862: packet_put_char(options.cipher);
863:
864: /* Send the cookie back to the server. */
865: for (i = 0; i < 8; i++)
866: packet_put_char(cookie[i]);
867:
868: /* Send and destroy the encrypted encryption key integer. */
869: packet_put_bignum(key);
870: BN_clear_free(key);
871:
872: /* Send protocol flags. */
873: packet_put_int(client_flags);
874:
875: /* Send the packet now. */
876: packet_send();
877: packet_write_wait();
878:
879: debug("Sent encrypted session key.");
880:
881: /* Set the encryption key. */
882: packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
883:
884: /* We will no longer need the session key here. Destroy any extra copies. */
885: memset(session_key, 0, sizeof(session_key));
886:
887: /*
888: * Expect a success message from the server. Note that this message
889: * will be received in encrypted form.
890: */
891: packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
892:
893: debug("Received encrypted confirmation.");
894: }
895:
896: /*
897: * Authenticate user
898: */
899: void
900: ssh_userauth(
1.15 deraadt 901: const char *local_user,
902: const char *server_user,
1.1 markus 903: char *host,
904: int host_key_valid, RSA *own_host_key)
905: {
906: int i, type;
907: int payload_len;
908:
909: if (supported_authentications == 0)
910: fatal("ssh_userauth: server supports no auth methods");
911:
912: /* Send the name of the user to log in as on the server. */
913: packet_start(SSH_CMSG_USER);
914: packet_put_string(server_user, strlen(server_user));
915: packet_send();
916: packet_write_wait();
917:
918: /*
919: * The server should respond with success if no authentication is
920: * needed (the user has no password). Otherwise the server responds
921: * with failure.
922: */
923: type = packet_read(&payload_len);
924:
925: /* check whether the connection was accepted without authentication. */
926: if (type == SSH_SMSG_SUCCESS)
927: return;
928: if (type != SSH_SMSG_FAILURE)
929: packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
930: type);
931:
932: #ifdef AFS
933: /* Try Kerberos tgt passing if the server supports it. */
934: if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
935: options.kerberos_tgt_passing) {
936: if (options.cipher == SSH_CIPHER_NONE)
937: log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
938: (void) send_kerberos_tgt();
939: }
940: /* Try AFS token passing if the server supports it. */
941: if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
942: options.afs_token_passing && k_hasafs()) {
943: if (options.cipher == SSH_CIPHER_NONE)
944: log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
945: send_afs_tokens();
946: }
947: #endif /* AFS */
948:
949: #ifdef KRB4
950: if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
951: options.kerberos_authentication) {
952: debug("Trying Kerberos authentication.");
953: if (try_kerberos_authentication()) {
954: /* The server should respond with success or failure. */
955: type = packet_read(&payload_len);
956: if (type == SSH_SMSG_SUCCESS)
957: return;
958: if (type != SSH_SMSG_FAILURE)
959: packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
960: }
961: }
962: #endif /* KRB4 */
963:
964: /*
965: * Use rhosts authentication if running in privileged socket and we
966: * do not wish to remain anonymous.
967: */
968: if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
969: options.rhosts_authentication) {
970: debug("Trying rhosts authentication.");
971: packet_start(SSH_CMSG_AUTH_RHOSTS);
972: packet_put_string(local_user, strlen(local_user));
973: packet_send();
974: packet_write_wait();
975:
976: /* The server should respond with success or failure. */
977: type = packet_read(&payload_len);
978: if (type == SSH_SMSG_SUCCESS)
979: return;
980: if (type != SSH_SMSG_FAILURE)
981: packet_disconnect("Protocol error: got %d in response to rhosts auth",
982: type);
983: }
984: /*
985: * Try .rhosts or /etc/hosts.equiv authentication with RSA host
986: * authentication.
987: */
988: if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
989: options.rhosts_rsa_authentication && host_key_valid) {
990: if (try_rhosts_rsa_authentication(local_user, own_host_key))
991: return;
992: }
993: /* Try RSA authentication if the server supports it. */
994: if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
995: options.rsa_authentication) {
996: /*
997: * Try RSA authentication using the authentication agent. The
998: * agent is tried first because no passphrase is needed for
999: * it, whereas identity files may require passphrases.
1000: */
1001: if (try_agent_authentication())
1002: return;
1003:
1004: /* Try RSA authentication for each identity. */
1005: for (i = 0; i < options.num_identity_files; i++)
1.9 markus 1006: if (options.identity_files_type[i] == KEY_RSA1 &&
1007: try_rsa_authentication(options.identity_files[i]))
1.1 markus 1008: return;
1009: }
1010: /* Try skey authentication if the server supports it. */
1011: if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1012: options.skey_authentication && !options.batch_mode) {
1013: if (try_skey_authentication())
1014: return;
1015: }
1016: /* Try password authentication if the server supports it. */
1017: if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1018: options.password_authentication && !options.batch_mode) {
1019: char prompt[80];
1020:
1021: snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
1022: server_user, host);
1023: if (try_password_authentication(prompt))
1024: return;
1025: }
1026: /* All authentication methods have failed. Exit with an error message. */
1027: fatal("Permission denied.");
1028: /* NOTREACHED */
1029: }