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., 59 Temple Place - Suite 330, Boston, MA 02111, 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;
58 /* OpenSSL fills the entropy pool from /dev/urandom if it exists */
59 #define HAVE_ENTROPY() (!access(DEVRANDOM, R_OK) || entropy_byte_count >= 16)
62 typedef struct _sslsockdata {
68 /* local prototypes */
70 static int add_entropy (const char *file);
71 static int ssl_socket_read (CONNECTION * conn, char *buf, size_t len);
72 static int ssl_socket_write (CONNECTION * conn, const char *buf, size_t len);
73 static int ssl_socket_open (CONNECTION * conn);
74 static int ssl_socket_close (CONNECTION * conn);
75 static int tls_close (CONNECTION * conn);
76 static int ssl_check_certificate (sslsockdata * data);
77 static void ssl_get_client_cert (sslsockdata * ssldata, CONNECTION * conn);
78 static int ssl_passwd_cb (char *buf, int size, int rwflag, void *userdata);
79 static int ssl_negotiate (sslsockdata *);
81 /* mutt_ssl_starttls: Negotiate TLS over an already opened connection.
82 * TODO: Merge this code better with ssl_socket_open. */
83 int mutt_ssl_starttls (CONNECTION * conn)
91 ssldata = (sslsockdata *) safe_calloc (1, sizeof (sslsockdata));
92 /* the ssl_use_xxx protocol options don't apply. We must use TLS in TLS. */
93 if (!(ssldata->ctx = SSL_CTX_new (TLSv1_client_method ()))) {
94 dprint (1, (debugfile, "mutt_ssl_starttls: Error allocating SSL_CTX\n"));
98 ssl_get_client_cert (ssldata, conn);
100 if (!(ssldata->ssl = SSL_new (ssldata->ctx))) {
101 dprint (1, (debugfile, "mutt_ssl_starttls: Error allocating SSL\n"));
105 if (SSL_set_fd (ssldata->ssl, conn->fd) != 1) {
106 dprint (1, (debugfile, "mutt_ssl_starttls: Error setting fd\n"));
110 if (ssl_negotiate (ssldata))
113 /* hmm. watch out if we're starting TLS over any method other than raw. */
114 conn->sockdata = ssldata;
115 conn->conn_read = ssl_socket_read;
116 conn->conn_write = ssl_socket_write;
117 conn->conn_close = tls_close;
119 conn->ssf = SSL_CIPHER_get_bits (SSL_get_current_cipher (ssldata->ssl),
125 FREE (&ssldata->ssl);
127 FREE (&ssldata->ctx);
135 * OpenSSL library needs to be fed with sufficient entropy. On systems
136 * with /dev/urandom, this is done transparently by the library itself,
137 * on other systems we need to fill the entropy pool ourselves.
139 * Even though only OpenSSL 0.9.5 and later will complain about the
140 * lack of entropy, we try to our best and fill the pool with older
141 * versions also. (That's the reason for the ugly #ifdefs and macros,
142 * otherwise I could have simply #ifdef'd the whole ssl_init funcion)
146 char path[_POSIX_PATH_MAX];
147 static unsigned char init_complete = 0;
152 if (!HAVE_ENTROPY ()) {
153 /* load entropy from files */
154 add_entropy (SslEntropyFile);
155 add_entropy (RAND_file_name (path, sizeof (path)));
157 /* load entropy from egd sockets */
159 add_entropy (getenv ("EGDSOCKET"));
160 snprintf (path, sizeof (path), "%s/.entropy", NONULL (Homedir));
162 add_entropy ("/tmp/entropy");
165 /* shuffle $RANDFILE (or ~/.rnd if unset) */
166 RAND_write_file (RAND_file_name (path, sizeof (path)));
168 if (!HAVE_ENTROPY ()) {
169 mutt_error (_("Failed to find enough entropy on your system"));
175 /* I don't think you can do this just before reading the error. The call
176 * itself might clobber the last SSL error. */
177 SSL_load_error_strings ();
183 static int add_entropy (const char *file)
191 if (stat (file, &st) == -1)
192 return errno == ENOENT ? 0 : -1;
194 mutt_message (_("Filling entropy pool: %s...\n"), file);
196 /* check that the file permissions are secure */
197 if (st.st_uid != getuid () ||
198 ((st.st_mode & (S_IWGRP | S_IRGRP)) != 0) ||
199 ((st.st_mode & (S_IWOTH | S_IROTH)) != 0)) {
200 mutt_error (_("%s has insecure permissions!"), file);
209 n = RAND_load_file (file, -1);
211 #ifndef HAVE_RAND_STATUS
213 entropy_byte_count += n;
218 static int ssl_socket_open_err (CONNECTION * conn)
220 mutt_error (_("SSL disabled due the lack of entropy"));
226 int ssl_socket_setup (CONNECTION * conn)
228 if (ssl_init () < 0) {
229 conn->conn_open = ssl_socket_open_err;
233 conn->conn_open = ssl_socket_open;
234 conn->conn_read = ssl_socket_read;
235 conn->conn_write = ssl_socket_write;
236 conn->conn_close = ssl_socket_close;
241 static int ssl_socket_read (CONNECTION * conn, char *buf, size_t len)
243 sslsockdata *data = conn->sockdata;
245 return SSL_read (data->ssl, buf, len);
248 static int ssl_socket_write (CONNECTION * conn, const char *buf, size_t len)
250 sslsockdata *data = conn->sockdata;
252 return SSL_write (data->ssl, buf, len);
255 static int ssl_socket_open (CONNECTION * conn)
260 if (raw_socket_open (conn) < 0)
263 data = (sslsockdata *) safe_calloc (1, sizeof (sslsockdata));
264 conn->sockdata = data;
266 data->ctx = SSL_CTX_new (SSLv23_client_method ());
268 /* disable SSL protocols as needed */
269 if (!option (OPTTLSV1)) {
270 SSL_CTX_set_options (data->ctx, SSL_OP_NO_TLSv1);
272 if (!option (OPTSSLV2)) {
273 SSL_CTX_set_options (data->ctx, SSL_OP_NO_SSLv2);
275 if (!option (OPTSSLV3)) {
276 SSL_CTX_set_options (data->ctx, SSL_OP_NO_SSLv3);
279 ssl_get_client_cert (data, conn);
281 data->ssl = SSL_new (data->ctx);
282 SSL_set_fd (data->ssl, conn->fd);
284 if (ssl_negotiate (data)) {
285 mutt_socket_close (conn);
289 conn->ssf = SSL_CIPHER_get_bits (SSL_get_current_cipher (data->ssl),
295 /* ssl_negotiate: After SSL state has been initialised, attempt to negotiate
296 * SSL over the wire, including certificate checks. */
297 static int ssl_negotiate (sslsockdata * ssldata)
302 #if OPENSSL_VERSION_NUMBER >= 0x00906000L
303 /* This only exists in 0.9.6 and above. Without it we may get interrupted
304 * reads or writes. Bummer. */
305 SSL_set_mode (ssldata->ssl, SSL_MODE_AUTO_RETRY);
308 if ((err = SSL_connect (ssldata->ssl)) != 1) {
309 switch (SSL_get_error (ssldata->ssl, err)) {
310 case SSL_ERROR_SYSCALL:
311 errmsg = _("I/O error");
314 errmsg = ERR_error_string (ERR_get_error (), NULL);
317 errmsg = _("unknown error");
320 mutt_error (_("SSL failed: %s"), errmsg);
326 ssldata->cert = SSL_get_peer_certificate (ssldata->ssl);
327 if (!ssldata->cert) {
328 mutt_error (_("Unable to get certificate from peer"));
333 if (!ssl_check_certificate (ssldata))
336 mutt_message (_("SSL connection using %s (%s)"),
337 SSL_get_cipher_version (ssldata->ssl),
338 SSL_get_cipher_name (ssldata->ssl));
344 static int ssl_socket_close (CONNECTION * conn)
346 sslsockdata *data = conn->sockdata;
349 SSL_shutdown (data->ssl);
351 X509_free (data->cert);
352 SSL_free (data->ssl);
353 SSL_CTX_free (data->ctx);
354 FREE (&conn->sockdata);
357 return raw_socket_close (conn);
360 static int tls_close (CONNECTION * conn)
364 rc = ssl_socket_close (conn);
365 conn->conn_read = raw_socket_read;
366 conn->conn_write = raw_socket_write;
367 conn->conn_close = raw_socket_close;
372 static char *x509_get_part (char *line, const char *ndx)
374 static char ret[SHORT_STRING];
377 strfcpy (ret, _("Unknown"), sizeof (ret));
379 c = strstr (line, ndx);
382 c2 = strchr (c, '/');
385 strfcpy (ret, c, sizeof (ret));
393 static void x509_fingerprint (char *s, int l, X509 * cert)
395 unsigned char md[EVP_MAX_MD_SIZE];
399 if (!X509_digest (cert, EVP_md5 (), md, &n)) {
400 snprintf (s, l, "%s", _("[unable to calculate]"));
403 for (j = 0; j < (int) n; j++) {
406 snprintf (ch, 8, "%02X%s", md[j], (j % 2 ? " " : ""));
407 safe_strcat (s, l, ch);
412 static char *asn1time_to_string (ASN1_UTCTIME * tm)
417 strfcpy (buf, _("[invalid date]"), sizeof (buf));
419 bio = BIO_new (BIO_s_mem ());
421 if (ASN1_TIME_print (bio, tm))
422 (void) BIO_read (bio, buf, sizeof (buf));
429 static int check_certificate_by_signer (X509 * peercert)
435 ctx = X509_STORE_new ();
439 if (option (OPTSSLSYSTEMCERTS)) {
440 if (X509_STORE_set_default_paths (ctx))
443 dprint (2, (debugfile, "X509_STORE_set_default_paths failed\n"));
446 if (X509_STORE_load_locations (ctx, SslCertFile, NULL))
449 dprint (2, (debugfile, "X509_STORE_load_locations_failed\n"));
453 X509_STORE_free (ctx);
457 X509_STORE_CTX_init (&xsc, ctx, peercert, NULL);
459 pass = (X509_verify_cert (&xsc) > 0);
462 char buf[SHORT_STRING];
465 err = X509_STORE_CTX_get_error (&xsc);
466 snprintf (buf, sizeof (buf), "%s (%d)",
467 X509_verify_cert_error_string (err), err);
468 dprint (2, (debugfile, "X509_verify_cert: %s\n", buf));
471 X509_STORE_CTX_cleanup (&xsc);
472 X509_STORE_free (ctx);
477 static int check_certificate_by_digest (X509 * peercert)
479 unsigned char peermd[EVP_MAX_MD_SIZE];
480 unsigned int peermdlen;
485 /* expiration check */
486 if (X509_cmp_current_time (X509_get_notBefore (peercert)) >= 0) {
487 dprint (2, (debugfile, "Server certificate is not yet valid\n"));
488 mutt_error (_("Server certificate is not yet valid"));
492 if (X509_cmp_current_time (X509_get_notAfter (peercert)) <= 0) {
493 dprint (2, (debugfile, "Server certificate has expired"));
494 mutt_error (_("Server certificate has expired"));
499 if ((fp = fopen (SslCertFile, "rt")) == NULL)
502 if (!X509_digest (peercert, EVP_sha1 (), peermd, &peermdlen)) {
507 while ((cert = READ_X509_KEY (fp, &cert)) != NULL) {
508 unsigned char md[EVP_MAX_MD_SIZE];
511 /* Avoid CPU-intensive digest calculation if the certificates are
512 * not even remotely equal.
514 if (X509_subject_name_cmp (cert, peercert) != 0 ||
515 X509_issuer_name_cmp (cert, peercert) != 0)
518 if (!X509_digest (cert, EVP_sha1 (), md, &mdlen) || peermdlen != mdlen)
521 if (memcmp (peermd, md, mdlen) != 0)
533 static int ssl_check_certificate (sslsockdata * data)
535 char *part[] = { "/CN=", "/Email=", "/O=", "/OU=", "/L=", "/ST=", "/C=" };
536 char helpstr[SHORT_STRING];
537 char buf[SHORT_STRING];
541 char *name = NULL, *c;
543 if (check_certificate_by_signer (data->cert)) {
544 dprint (1, (debugfile, "ssl_check_certificate: signer check passed\n"));
548 /* automatic check from user's database */
549 if (SslCertFile && check_certificate_by_digest (data->cert)) {
550 dprint (1, (debugfile, "ssl_check_certificate: digest check passed\n"));
554 /* interactive check from user */
555 menu = mutt_new_menu ();
557 menu->dialog = (char **) safe_calloc (1, menu->max * sizeof (char *));
558 for (i = 0; i < menu->max; i++)
559 menu->dialog[i] = (char *) safe_calloc (1, SHORT_STRING * sizeof (char));
562 strfcpy (menu->dialog[row], _("This certificate belongs to:"),
565 name = X509_NAME_oneline (X509_get_subject_name (data->cert),
567 for (i = 0; i < 5; i++) {
568 c = x509_get_part (name, part[i]);
569 snprintf (menu->dialog[row++], SHORT_STRING, " %s", c);
573 strfcpy (menu->dialog[row], _("This certificate was issued by:"),
576 name = X509_NAME_oneline (X509_get_issuer_name (data->cert),
578 for (i = 0; i < 5; i++) {
579 c = x509_get_part (name, part[i]);
580 snprintf (menu->dialog[row++], SHORT_STRING, " %s", c);
584 snprintf (menu->dialog[row++], SHORT_STRING, "%s",
585 _("This certificate is valid"));
586 snprintf (menu->dialog[row++], SHORT_STRING, _(" from %s"),
587 asn1time_to_string (X509_get_notBefore (data->cert)));
588 snprintf (menu->dialog[row++], SHORT_STRING, _(" to %s"),
589 asn1time_to_string (X509_get_notAfter (data->cert)));
593 x509_fingerprint (buf, sizeof (buf), data->cert);
594 snprintf (menu->dialog[row++], SHORT_STRING, _("Fingerprint: %s"), buf);
596 menu->title = _("SSL Certificate check");
598 menu->prompt = _("(r)eject, accept (o)nce, (a)ccept always");
599 menu->keys = _("roa");
602 menu->prompt = _("(r)eject, accept (o)nce");
603 menu->keys = _("ro");
607 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_GENERIC, OP_EXIT);
608 safe_strcat (helpstr, sizeof (helpstr), buf);
609 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_GENERIC, OP_HELP);
610 safe_strcat (helpstr, sizeof (helpstr), buf);
611 menu->help = helpstr;
614 set_option (OPTUNBUFFEREDINPUT);
616 switch (mutt_menuLoop (menu)) {
618 case OP_MAX + 1: /* reject */
622 case OP_MAX + 3: /* accept always */
624 if ((fp = fopen (SslCertFile, "a"))) {
625 if (PEM_write_X509 (fp, data->cert))
630 mutt_error (_("Warning: Couldn't save certificate"));
634 mutt_message (_("Certificate saved"));
638 case OP_MAX + 2: /* accept once */
643 unset_option (OPTUNBUFFEREDINPUT);
644 mutt_menuDestroy (&menu);
648 static void ssl_get_client_cert (sslsockdata * ssldata, CONNECTION * conn)
651 dprint (2, (debugfile, "Using client certificate %s\n", SslClientCert));
652 SSL_CTX_set_default_passwd_cb_userdata (ssldata->ctx, &conn->account);
653 SSL_CTX_set_default_passwd_cb (ssldata->ctx, ssl_passwd_cb);
654 SSL_CTX_use_certificate_file (ssldata->ctx, SslClientCert,
656 SSL_CTX_use_PrivateKey_file (ssldata->ctx, SslClientCert,
661 static int ssl_passwd_cb (char *buf, int size, int rwflag, void *userdata)
663 ACCOUNT *account = (ACCOUNT *) userdata;
665 if (mutt_account_getuser (account))
668 dprint (2, (debugfile, "ssl_passwd_cb: getting password for %s@%s:%u\n",
669 account->user, account->host, account->port));
671 if (mutt_account_getpass (account))
674 return snprintf (buf, size, "%s", account->pass);