2 * Copyright (C) 1999-2001 Tommi Komulainen <Tommi.Komulainen@iki.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <openssl/ssl.h>
24 #include <openssl/x509.h>
25 #include <openssl/err.h>
26 #include <openssl/rand.h>
33 #include "mutt_socket.h"
34 #include "mutt_menu.h"
35 #include "mutt_curses.h"
38 #if OPENSSL_VERSION_NUMBER >= 0x00904000L
39 #define READ_X509_KEY(fp, key) PEM_read_X509(fp, key, NULL, NULL)
41 #define READ_X509_KEY(fp, key) PEM_read_X509(fp, key, NULL)
44 /* Just in case OpenSSL doesn't define DEVRANDOM */
46 #define DEVRANDOM "/dev/urandom"
49 /* This is ugly, but as RAND_status came in on OpenSSL version 0.9.5
50 * and the code has to support older versions too, this is seemed to
51 * be cleaner way compared to having even uglier #ifdefs all around.
53 #ifdef HAVE_RAND_STATUS
54 #define HAVE_ENTROPY() (RAND_status() == 1)
56 static int entropy_byte_count = 0;
57 /* OpenSSL fills the entropy pool from /dev/urandom if it exists */
58 #define HAVE_ENTROPY() (!access(DEVRANDOM, R_OK) || entropy_byte_count >= 16)
61 typedef struct _sslsockdata
69 /* local prototypes */
71 static int add_entropy (const char *file);
72 static int ssl_socket_read (CONNECTION* conn, char* buf, size_t len);
73 static int ssl_socket_write (CONNECTION* conn, const char* buf, size_t len);
74 static int ssl_socket_open (CONNECTION * conn);
75 static int ssl_socket_close (CONNECTION * conn);
76 static int tls_close (CONNECTION* conn);
77 static int ssl_check_certificate (sslsockdata * data);
78 static void ssl_get_client_cert(sslsockdata *ssldata, CONNECTION *conn);
79 static int ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata);
80 static int ssl_negotiate (sslsockdata*);
82 /* mutt_ssl_starttls: Negotiate TLS over an already opened connection.
83 * TODO: Merge this code better with ssl_socket_open. */
84 int mutt_ssl_starttls (CONNECTION* conn)
92 ssldata = (sslsockdata*) safe_calloc (1, sizeof (sslsockdata));
93 /* the ssl_use_xxx protocol options don't apply. We must use TLS in TLS. */
94 if (! (ssldata->ctx = SSL_CTX_new (TLSv1_client_method ())))
96 dprint (1, (debugfile, "mutt_ssl_starttls: Error allocating SSL_CTX\n"));
100 ssl_get_client_cert(ssldata, conn);
102 if (! (ssldata->ssl = SSL_new (ssldata->ctx)))
104 dprint (1, (debugfile, "mutt_ssl_starttls: Error allocating SSL\n"));
108 if (SSL_set_fd (ssldata->ssl, conn->fd) != 1)
110 dprint (1, (debugfile, "mutt_ssl_starttls: Error setting fd\n"));
114 if (ssl_negotiate (ssldata))
117 /* hmm. watch out if we're starting TLS over any method other than raw. */
118 conn->sockdata = ssldata;
119 conn->conn_read = ssl_socket_read;
120 conn->conn_write = ssl_socket_write;
121 conn->conn_close = tls_close;
123 conn->ssf = SSL_CIPHER_get_bits (SSL_get_current_cipher (ssldata->ssl),
129 FREE (&ssldata->ssl);
131 FREE (&ssldata->ctx);
139 * OpenSSL library needs to be fed with sufficient entropy. On systems
140 * with /dev/urandom, this is done transparently by the library itself,
141 * on other systems we need to fill the entropy pool ourselves.
143 * Even though only OpenSSL 0.9.5 and later will complain about the
144 * lack of entropy, we try to our best and fill the pool with older
145 * versions also. (That's the reason for the ugly #ifdefs and macros,
146 * otherwise I could have simply #ifdef'd the whole ssl_init funcion)
150 char path[_POSIX_PATH_MAX];
151 static unsigned char init_complete = 0;
156 if (! HAVE_ENTROPY())
158 /* load entropy from files */
159 add_entropy (SslEntropyFile);
160 add_entropy (RAND_file_name (path, sizeof (path)));
162 /* load entropy from egd sockets */
164 add_entropy (getenv ("EGDSOCKET"));
165 snprintf (path, sizeof(path), "%s/.entropy", NONULL(Homedir));
167 add_entropy ("/tmp/entropy");
170 /* shuffle $RANDFILE (or ~/.rnd if unset) */
171 RAND_write_file (RAND_file_name (path, sizeof (path)));
173 if (! HAVE_ENTROPY())
175 mutt_error (_("Failed to find enough entropy on your system"));
181 /* I don't think you can do this just before reading the error. The call
182 * itself might clobber the last SSL error. */
183 SSL_load_error_strings();
189 static int add_entropy (const char *file)
196 if (stat (file, &st) == -1)
197 return errno == ENOENT ? 0 : -1;
199 mutt_message (_("Filling entropy pool: %s...\n"),
202 /* check that the file permissions are secure */
203 if (st.st_uid != getuid () ||
204 ((st.st_mode & (S_IWGRP | S_IRGRP)) != 0) ||
205 ((st.st_mode & (S_IWOTH | S_IROTH)) != 0))
207 mutt_error (_("%s has insecure permissions!"), file);
216 n = RAND_load_file (file, -1);
218 #ifndef HAVE_RAND_STATUS
219 if (n > 0) entropy_byte_count += n;
224 static int ssl_socket_open_err (CONNECTION *conn)
226 mutt_error (_("SSL disabled due the lack of entropy"));
232 int mutt_ssl_socket_setup (CONNECTION * conn)
236 conn->conn_open = ssl_socket_open_err;
240 conn->conn_open = ssl_socket_open;
241 conn->conn_read = ssl_socket_read;
242 conn->conn_write = ssl_socket_write;
243 conn->conn_close = ssl_socket_close;
244 conn->conn_poll = raw_socket_poll;
249 static int ssl_socket_read (CONNECTION* conn, char* buf, size_t len)
251 sslsockdata *data = conn->sockdata;
252 return SSL_read (data->ssl, buf, len);
255 static int ssl_socket_write (CONNECTION* conn, const char* buf, size_t len)
257 sslsockdata *data = conn->sockdata;
258 return SSL_write (data->ssl, buf, len);
261 static int ssl_socket_open (CONNECTION * conn)
266 if (raw_socket_open (conn) < 0)
269 data = (sslsockdata *) safe_calloc (1, sizeof (sslsockdata));
270 conn->sockdata = data;
272 data->ctx = SSL_CTX_new (SSLv23_client_method ());
274 /* disable SSL protocols as needed */
275 if (!option(OPTTLSV1))
277 SSL_CTX_set_options(data->ctx, SSL_OP_NO_TLSv1);
279 if (!option(OPTSSLV2))
281 SSL_CTX_set_options(data->ctx, SSL_OP_NO_SSLv2);
283 if (!option(OPTSSLV3))
285 SSL_CTX_set_options(data->ctx, SSL_OP_NO_SSLv3);
288 ssl_get_client_cert(data, conn);
290 data->ssl = SSL_new (data->ctx);
291 SSL_set_fd (data->ssl, conn->fd);
293 if (ssl_negotiate(data))
295 mutt_socket_close (conn);
299 conn->ssf = SSL_CIPHER_get_bits (SSL_get_current_cipher (data->ssl),
305 /* ssl_negotiate: After SSL state has been initialised, attempt to negotiate
306 * SSL over the wire, including certificate checks. */
307 static int ssl_negotiate (sslsockdata* ssldata)
312 #if OPENSSL_VERSION_NUMBER >= 0x00906000L
313 /* This only exists in 0.9.6 and above. Without it we may get interrupted
314 * reads or writes. Bummer. */
315 SSL_set_mode (ssldata->ssl, SSL_MODE_AUTO_RETRY);
318 if ((err = SSL_connect (ssldata->ssl)) != 1)
320 switch (SSL_get_error (ssldata->ssl, err))
322 case SSL_ERROR_SYSCALL:
323 errmsg = _("I/O error");
326 errmsg = ERR_error_string (ERR_get_error (), NULL);
329 errmsg = _("unknown error");
332 mutt_error (_("SSL failed: %s"), errmsg);
338 ssldata->cert = SSL_get_peer_certificate (ssldata->ssl);
341 mutt_error (_("Unable to get certificate from peer"));
346 if (!ssl_check_certificate (ssldata))
349 mutt_message (_("SSL connection using %s (%s)"),
350 SSL_get_cipher_version (ssldata->ssl), SSL_get_cipher_name (ssldata->ssl));
356 static int ssl_socket_close (CONNECTION * conn)
358 sslsockdata *data = conn->sockdata;
361 SSL_shutdown (data->ssl);
363 /* hold onto this for the life of mutt, in case we want to reconnect.
364 * The purist in me wants a mutt_exit hook. */
366 X509_free (data->cert);
368 SSL_free (data->ssl);
369 SSL_CTX_free (data->ctx);
370 FREE (&conn->sockdata);
373 return raw_socket_close (conn);
376 static int tls_close (CONNECTION* conn)
380 rc = ssl_socket_close (conn);
381 conn->conn_read = raw_socket_read;
382 conn->conn_write = raw_socket_write;
383 conn->conn_close = raw_socket_close;
388 static char *x509_get_part (char *line, const char *ndx)
390 static char ret[SHORT_STRING];
393 strfcpy (ret, _("Unknown"), sizeof (ret));
395 c = strstr (line, ndx);
399 c2 = strchr (c, '/');
402 strfcpy (ret, c, sizeof (ret));
410 static void x509_fingerprint (char *s, int l, X509 * cert)
412 unsigned char md[EVP_MAX_MD_SIZE];
416 if (!X509_digest (cert, EVP_md5 (), md, &n))
418 snprintf (s, l, _("[unable to calculate]"));
422 for (j = 0; j < (int) n; j++)
425 snprintf (ch, 8, "%02X%s", md[j], (j % 2 ? " " : ""));
426 safe_strcat (s, l, ch);
431 static char *asn1time_to_string (ASN1_UTCTIME *tm)
436 strfcpy (buf, _("[invalid date]"), sizeof (buf));
438 bio = BIO_new (BIO_s_mem());
441 if (ASN1_TIME_print (bio, tm))
442 (void) BIO_read (bio, buf, sizeof (buf));
449 static int check_certificate_by_signer (X509 *peercert)
455 ctx = X509_STORE_new ();
456 if (ctx == NULL) return 0;
458 if (option (OPTSSLSYSTEMCERTS))
460 if (X509_STORE_set_default_paths (ctx))
463 dprint (2, (debugfile, "X509_STORE_set_default_paths failed\n"));
466 if (X509_STORE_load_locations (ctx, SslCertFile, NULL))
469 dprint (2, (debugfile, "X509_STORE_load_locations_failed\n"));
474 X509_STORE_free (ctx);
478 X509_STORE_CTX_init (&xsc, ctx, peercert, NULL);
480 pass = (X509_verify_cert (&xsc) > 0);
484 char buf[SHORT_STRING];
487 err = X509_STORE_CTX_get_error (&xsc);
488 snprintf (buf, sizeof (buf), "%s (%d)",
489 X509_verify_cert_error_string(err), err);
490 dprint (2, (debugfile, "X509_verify_cert: %s\n", buf));
493 X509_STORE_CTX_cleanup (&xsc);
494 X509_STORE_free (ctx);
499 static int compare_certificates (X509 *cert, X509 *peercert,
500 unsigned char *peermd, unsigned int peermdlen)
502 unsigned char md[EVP_MAX_MD_SIZE];
505 /* Avoid CPU-intensive digest calculation if the certificates are
506 * not even remotely equal.
508 if (X509_subject_name_cmp (cert, peercert) != 0 ||
509 X509_issuer_name_cmp (cert, peercert) != 0)
512 if (!X509_digest (cert, EVP_sha1(), md, &mdlen) || peermdlen != mdlen)
515 if (memcmp(peermd, md, mdlen) != 0)
521 static int check_certificate_cache (X509 *peercert)
523 unsigned char peermd[EVP_MAX_MD_SIZE];
524 unsigned int peermdlen;
528 if (!X509_digest (peercert, EVP_sha1(), peermd, &peermdlen))
533 for (scert = SslSessionCerts; scert; scert = scert->next)
535 cert = *(X509**)scert->data;
536 if (!compare_certificates (cert, peercert, peermd, peermdlen))
545 static int check_certificate_by_digest (X509 *peercert)
547 unsigned char peermd[EVP_MAX_MD_SIZE];
548 unsigned int peermdlen;
553 /* expiration check */
554 if (X509_cmp_current_time (X509_get_notBefore (peercert)) >= 0)
556 dprint (2, (debugfile, "Server certificate is not yet valid\n"));
557 mutt_error (_("Server certificate is not yet valid"));
561 if (X509_cmp_current_time (X509_get_notAfter (peercert)) <= 0)
563 dprint (2, (debugfile, "Server certificate has expired"));
564 mutt_error (_("Server certificate has expired"));
569 if ((fp = fopen (SslCertFile, "rt")) == NULL)
572 if (!X509_digest (peercert, EVP_sha1(), peermd, &peermdlen))
578 while ((cert = READ_X509_KEY (fp, &cert)) != NULL)
580 pass = compare_certificates (cert, peercert, peermd, peermdlen) ? 0 : 1;
591 static int ssl_check_certificate (sslsockdata * data)
594 {"/CN=", "/Email=", "/O=", "/OU=", "/L=", "/ST=", "/C="};
595 char helpstr[LONG_STRING];
596 char buf[SHORT_STRING];
600 char *name = NULL, *c;
602 /* check session cache first */
603 if (check_certificate_cache (data->cert))
605 dprint (1, (debugfile, "ssl_check_certificate: using cached certificate\n"));
609 if (check_certificate_by_signer (data->cert))
611 dprint (1, (debugfile, "ssl_check_certificate: signer check passed\n"));
615 /* automatic check from user's database */
616 if (SslCertFile && check_certificate_by_digest (data->cert))
618 dprint (1, (debugfile, "ssl_check_certificate: digest check passed\n"));
622 /* interactive check from user */
623 menu = mutt_new_menu ();
625 menu->dialog = (char **) safe_calloc (1, menu->max * sizeof (char *));
626 for (i = 0; i < menu->max; i++)
627 menu->dialog[i] = (char *) safe_calloc (1, SHORT_STRING * sizeof (char));
630 strfcpy (menu->dialog[row], _("This certificate belongs to:"), SHORT_STRING);
632 name = X509_NAME_oneline (X509_get_subject_name (data->cert),
634 for (i = 0; i < 5; i++)
636 c = x509_get_part (name, part[i]);
637 snprintf (menu->dialog[row++], SHORT_STRING, " %s", c);
641 strfcpy (menu->dialog[row], _("This certificate was issued by:"), SHORT_STRING);
643 name = X509_NAME_oneline (X509_get_issuer_name (data->cert),
645 for (i = 0; i < 5; i++)
647 c = x509_get_part (name, part[i]);
648 snprintf (menu->dialog[row++], SHORT_STRING, " %s", c);
652 snprintf (menu->dialog[row++], SHORT_STRING, _("This certificate is valid"));
653 snprintf (menu->dialog[row++], SHORT_STRING, _(" from %s"),
654 asn1time_to_string (X509_get_notBefore (data->cert)));
655 snprintf (menu->dialog[row++], SHORT_STRING, _(" to %s"),
656 asn1time_to_string (X509_get_notAfter (data->cert)));
660 x509_fingerprint (buf, sizeof (buf), data->cert);
661 snprintf (menu->dialog[row++], SHORT_STRING, _("Fingerprint: %s"), buf);
663 menu->title = _("SSL Certificate check");
664 if (SslCertFile && X509_cmp_current_time (X509_get_notAfter (data->cert)) >= 0
665 && X509_cmp_current_time (X509_get_notBefore (data->cert)) < 0)
667 menu->prompt = _("(r)eject, accept (o)nce, (a)ccept always");
668 menu->keys = _("roa");
672 menu->prompt = _("(r)eject, accept (o)nce");
673 menu->keys = _("ro");
677 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_GENERIC, OP_EXIT);
678 safe_strcat (helpstr, sizeof (helpstr), buf);
679 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_GENERIC, OP_HELP);
680 safe_strcat (helpstr, sizeof (helpstr), buf);
681 menu->help = helpstr;
684 set_option(OPTUNBUFFEREDINPUT);
687 switch (mutt_menuLoop (menu))
690 case OP_MAX + 1: /* reject */
694 case OP_MAX + 3: /* accept always */
696 if ((fp = fopen (SslCertFile, "a")))
698 if (PEM_write_X509 (fp, data->cert))
704 mutt_error (_("Warning: Couldn't save certificate"));
709 mutt_message (_("Certificate saved"));
713 case OP_MAX + 2: /* accept once */
715 /* keep a handle on accepted certificates in case we want to
716 * open up another connection to the same server in this session */
717 SslSessionCerts = mutt_add_list_n (SslSessionCerts, &data->cert,
722 unset_option(OPTUNBUFFEREDINPUT);
723 mutt_menuDestroy (&menu);
727 static void ssl_get_client_cert(sslsockdata *ssldata, CONNECTION *conn)
731 dprint (2, (debugfile, "Using client certificate %s\n", SslClientCert));
732 SSL_CTX_set_default_passwd_cb_userdata(ssldata->ctx, &conn->account);
733 SSL_CTX_set_default_passwd_cb(ssldata->ctx, ssl_passwd_cb);
734 SSL_CTX_use_certificate_file(ssldata->ctx, SslClientCert, SSL_FILETYPE_PEM);
735 SSL_CTX_use_PrivateKey_file(ssldata->ctx, SslClientCert, SSL_FILETYPE_PEM);
739 static int ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
741 ACCOUNT *account = (ACCOUNT*)userdata;
743 if (mutt_account_getuser (account))
746 dprint (2, (debugfile, "ssl_passwd_cb: getting password for %s@%s:%u\n",
747 account->user, account->host, account->port));
749 if (mutt_account_getpass (account))
752 return snprintf(buf, size, "%s", account->pass);