Andreas Krennmair:
[apps/madmutt.git] / mutt_ssl.c
1 /*
2  * Copyright (C) 1999-2001 Tommi Komulainen <Tommi.Komulainen@iki.fi>
3  * 
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.
8  * 
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.
13  * 
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.
17  */
18
19 /* for SSL NO_* defines */
20 #include "config.h"
21
22 #include <openssl/ssl.h>
23 #include <openssl/x509.h>
24 #include <openssl/err.h>
25 #include <openssl/rand.h>
26
27 #undef _
28
29 #include <string.h>
30
31 #include "mutt.h"
32 #include "mutt_socket.h"
33 #include "mutt_menu.h"
34 #include "mutt_curses.h"
35 #include "mutt_ssl.h"
36
37 #if OPENSSL_VERSION_NUMBER >= 0x00904000L
38 #define READ_X509_KEY(fp, key)  PEM_read_X509(fp, key, NULL, NULL)
39 #else
40 #define READ_X509_KEY(fp, key)  PEM_read_X509(fp, key, NULL)
41 #endif
42
43 /* Just in case OpenSSL doesn't define DEVRANDOM */
44 #ifndef DEVRANDOM
45 #define DEVRANDOM "/dev/urandom"
46 #endif
47
48 /* This is ugly, but as RAND_status came in on OpenSSL version 0.9.5
49  * and the code has to support older versions too, this is seemed to
50  * be cleaner way compared to having even uglier #ifdefs all around.
51  */
52 #ifdef HAVE_RAND_STATUS
53 #define HAVE_ENTROPY()  (RAND_status() == 1)
54 #else
55 static int entropy_byte_count = 0;
56 /* OpenSSL fills the entropy pool from /dev/urandom if it exists */
57 #define HAVE_ENTROPY()  (!access(DEVRANDOM, R_OK) || entropy_byte_count >= 16)
58 #endif
59
60 typedef struct _sslsockdata
61 {
62   SSL_CTX *ctx;
63   SSL *ssl;
64   X509 *cert;
65 }
66 sslsockdata;
67
68 /* local prototypes */
69 int ssl_init (void);
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*);
80
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)
84 {
85   sslsockdata* ssldata;
86   int maxbits;
87
88   if (ssl_init())
89     goto bail;
90
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   {
95     dprint (1, (debugfile, "mutt_ssl_starttls: Error allocating SSL_CTX\n"));
96     goto bail_ssldata;
97   }
98
99   ssl_get_client_cert(ssldata, conn);
100
101   if (! (ssldata->ssl = SSL_new (ssldata->ctx)))
102   {
103     dprint (1, (debugfile, "mutt_ssl_starttls: Error allocating SSL\n"));
104     goto bail_ctx;
105   }
106
107   if (SSL_set_fd (ssldata->ssl, conn->fd) != 1)
108   {
109     dprint (1, (debugfile, "mutt_ssl_starttls: Error setting fd\n"));
110     goto bail_ssl;
111   }
112
113   if (ssl_negotiate (ssldata))
114     goto bail_ssl;
115
116   /* hmm. watch out if we're starting TLS over any method other than raw. */
117   conn->sockdata = ssldata;
118   conn->read = ssl_socket_read;
119   conn->write = ssl_socket_write;
120   conn->close = tls_close;
121
122   conn->ssf = SSL_CIPHER_get_bits (SSL_get_current_cipher (ssldata->ssl),
123     &maxbits);
124
125   return 0;
126
127  bail_ssl:
128   FREE (&ssldata->ssl);
129  bail_ctx:
130   FREE (&ssldata->ctx);
131  bail_ssldata:
132   FREE (&ssldata);
133  bail:
134   return -1;
135 }
136
137 /* 
138  * OpenSSL library needs to be fed with sufficient entropy. On systems
139  * with /dev/urandom, this is done transparently by the library itself,
140  * on other systems we need to fill the entropy pool ourselves.
141  *
142  * Even though only OpenSSL 0.9.5 and later will complain about the
143  * lack of entropy, we try to our best and fill the pool with older
144  * versions also. (That's the reason for the ugly #ifdefs and macros,
145  * otherwise I could have simply #ifdef'd the whole ssl_init funcion)
146  */
147 int ssl_init (void)
148 {
149   char path[_POSIX_PATH_MAX];
150   static unsigned char init_complete = 0;
151
152   if (init_complete)
153     return 0;
154
155   if (! HAVE_ENTROPY())
156   {
157     /* load entropy from files */
158     add_entropy (SslEntropyFile);
159     add_entropy (RAND_file_name (path, sizeof (path)));
160   
161     /* load entropy from egd sockets */
162 #ifdef HAVE_RAND_EGD
163     add_entropy (getenv ("EGDSOCKET"));
164     snprintf (path, sizeof(path), "%s/.entropy", NONULL(Homedir));
165     add_entropy (path);
166     add_entropy ("/tmp/entropy");
167 #endif
168
169     /* shuffle $RANDFILE (or ~/.rnd if unset) */
170     RAND_write_file (RAND_file_name (path, sizeof (path)));
171     mutt_clear_error ();
172     if (! HAVE_ENTROPY())
173     {
174       mutt_error (_("Failed to find enough entropy on your system"));
175       mutt_sleep (2);
176       return -1;
177     }
178   }
179
180   /* I don't think you can do this just before reading the error. The call
181    * itself might clobber the last SSL error. */
182   SSL_load_error_strings();
183   SSL_library_init();
184   init_complete = 1;
185   return 0;
186 }
187
188 static int add_entropy (const char *file)
189 {
190   struct stat st;
191   int n = -1;
192
193   if (!file) return 0;
194
195   if (stat (file, &st) == -1)
196     return errno == ENOENT ? 0 : -1;
197
198   mutt_message (_("Filling entropy pool: %s...\n"),
199                 file);
200   
201   /* check that the file permissions are secure */
202   if (st.st_uid != getuid () || 
203       ((st.st_mode & (S_IWGRP | S_IRGRP)) != 0) ||
204       ((st.st_mode & (S_IWOTH | S_IROTH)) != 0))
205   {
206     mutt_error (_("%s has insecure permissions!"), file);
207     mutt_sleep (2);
208     return -1;
209   }
210
211 #ifdef HAVE_RAND_EGD
212   n = RAND_egd (file);
213 #endif
214   if (n <= 0)
215     n = RAND_load_file (file, -1);
216
217 #ifndef HAVE_RAND_STATUS
218   if (n > 0) entropy_byte_count += n;
219 #endif
220   return n;
221 }
222
223 static int ssl_socket_open_err (CONNECTION *conn)
224 {
225   mutt_error (_("SSL disabled due the lack of entropy"));
226   mutt_sleep (2);
227   return -1;
228 }
229
230
231 int ssl_socket_setup (CONNECTION * conn)
232 {
233   if (ssl_init() < 0)
234   {
235     conn->open = ssl_socket_open_err;
236     return -1;
237   }
238
239   conn->open    = ssl_socket_open;
240   conn->read    = ssl_socket_read;
241   conn->write   = ssl_socket_write;
242   conn->close   = ssl_socket_close;
243
244   return 0;
245 }
246
247 static int ssl_socket_read (CONNECTION* conn, char* buf, size_t len)
248 {
249   sslsockdata *data = conn->sockdata;
250   return SSL_read (data->ssl, buf, len);
251 }
252
253 static int ssl_socket_write (CONNECTION* conn, const char* buf, size_t len)
254 {
255   sslsockdata *data = conn->sockdata;
256   return SSL_write (data->ssl, buf, len);
257 }
258
259 static int ssl_socket_open (CONNECTION * conn)
260 {
261   sslsockdata *data;
262   int maxbits;
263
264   if (raw_socket_open (conn) < 0)
265     return -1;
266
267   data = (sslsockdata *) safe_calloc (1, sizeof (sslsockdata));
268   conn->sockdata = data;
269
270   data->ctx = SSL_CTX_new (SSLv23_client_method ());
271
272   /* disable SSL protocols as needed */
273   if (!option(OPTTLSV1)) 
274   {
275     SSL_CTX_set_options(data->ctx, SSL_OP_NO_TLSv1);
276   }
277   if (!option(OPTSSLV2)) 
278   {
279     SSL_CTX_set_options(data->ctx, SSL_OP_NO_SSLv2);
280   }
281   if (!option(OPTSSLV3)) 
282   {
283     SSL_CTX_set_options(data->ctx, SSL_OP_NO_SSLv3);
284   }
285
286   ssl_get_client_cert(data, conn);
287
288   data->ssl = SSL_new (data->ctx);
289   SSL_set_fd (data->ssl, conn->fd);
290
291   if (ssl_negotiate(data))
292   {
293     mutt_socket_close (conn);
294     return -1;
295   }
296   
297   conn->ssf = SSL_CIPHER_get_bits (SSL_get_current_cipher (data->ssl),
298     &maxbits);
299
300   return 0;
301 }
302
303 /* ssl_negotiate: After SSL state has been initialised, attempt to negotiate
304  *   SSL over the wire, including certificate checks. */
305 static int ssl_negotiate (sslsockdata* ssldata)
306 {
307   int err;
308   const char* errmsg;
309
310 #if OPENSSL_VERSION_NUMBER >= 0x00906000L
311   /* This only exists in 0.9.6 and above. Without it we may get interrupted
312    *   reads or writes. Bummer. */
313   SSL_set_mode (ssldata->ssl, SSL_MODE_AUTO_RETRY);
314 #endif
315
316   if ((err = SSL_connect (ssldata->ssl)) != 1)
317   {
318     switch (SSL_get_error (ssldata->ssl, err))
319     {
320     case SSL_ERROR_SYSCALL:
321       errmsg = _("I/O error");
322       break;
323     case SSL_ERROR_SSL:
324       errmsg = ERR_error_string (ERR_get_error (), NULL);
325       break;
326     default:
327       errmsg = _("unknown error");
328     }
329     
330     mutt_error (_("SSL failed: %s"), errmsg);
331     mutt_sleep (1);
332
333     return -1;
334   }
335
336   ssldata->cert = SSL_get_peer_certificate (ssldata->ssl);
337   if (!ssldata->cert)
338   {
339     mutt_error (_("Unable to get certificate from peer"));
340     mutt_sleep (1);
341     return -1;
342   }
343
344   if (!ssl_check_certificate (ssldata))
345     return -1;
346
347   mutt_message (_("SSL connection using %s (%s)"), 
348     SSL_get_cipher_version (ssldata->ssl), SSL_get_cipher_name (ssldata->ssl));
349   mutt_sleep (0);
350
351   return 0;
352 }
353
354 static int ssl_socket_close (CONNECTION * conn)
355 {
356   sslsockdata *data = conn->sockdata;
357   if (data)
358   {
359     SSL_shutdown (data->ssl);
360
361     X509_free (data->cert);
362     SSL_free (data->ssl);
363     SSL_CTX_free (data->ctx);
364     FREE (&conn->sockdata);
365   }
366
367   return raw_socket_close (conn);
368 }
369
370 static int tls_close (CONNECTION* conn)
371 {
372   int rc;
373
374   rc = ssl_socket_close (conn);
375   conn->read = raw_socket_read;
376   conn->write = raw_socket_write;
377   conn->close = raw_socket_close;
378
379   return rc;
380 }
381
382 static char *x509_get_part (char *line, const char *ndx)
383 {
384   static char ret[SHORT_STRING];
385   char *c, *c2;
386
387   strfcpy (ret, _("Unknown"), sizeof (ret));
388
389   c = strstr (line, ndx);
390   if (c)
391   {
392     c += strlen (ndx);
393     c2 = strchr (c, '/');
394     if (c2)
395       *c2 = '\0';
396     strfcpy (ret, c, sizeof (ret));
397     if (c2)
398       *c2 = '/';
399   }
400
401   return ret;
402 }
403
404 static void x509_fingerprint (char *s, int l, X509 * cert)
405 {
406   unsigned char md[EVP_MAX_MD_SIZE];
407   unsigned int n;
408   int j;
409
410   if (!X509_digest (cert, EVP_md5 (), md, &n))
411   {
412     snprintf (s, l, "%s", _("[unable to calculate]"));
413   }
414   else
415   {
416     for (j = 0; j < (int) n; j++)
417     {
418       char ch[8];
419       snprintf (ch, 8, "%02X%s", md[j], (j % 2 ? " " : ""));
420       safe_strcat (s, l, ch);
421     }
422   }
423 }
424
425 static char *asn1time_to_string (ASN1_UTCTIME *tm)
426 {
427   static char buf[64];
428   BIO *bio;
429
430   strfcpy (buf, _("[invalid date]"), sizeof (buf));
431   
432   bio = BIO_new (BIO_s_mem());
433   if (bio)
434   {
435     if (ASN1_TIME_print (bio, tm))
436       (void) BIO_read (bio, buf, sizeof (buf));
437     BIO_free (bio);
438   }
439
440   return buf;
441 }
442
443 static int check_certificate_by_signer (X509 *peercert)
444 {
445   X509_STORE_CTX xsc;
446   X509_STORE *ctx;
447   int pass = 0;
448
449   ctx = X509_STORE_new ();
450   if (ctx == NULL) return 0;
451
452   if (option (OPTSSLSYSTEMCERTS))
453   {
454     if (X509_STORE_set_default_paths (ctx))
455       pass++;
456     else
457       dprint (2, (debugfile, "X509_STORE_set_default_paths failed\n"));
458   }
459
460   if (X509_STORE_load_locations (ctx, SslCertFile, NULL))
461     pass++;
462   else
463     dprint (2, (debugfile, "X509_STORE_load_locations_failed\n"));
464
465   if (pass == 0)
466   {
467     /* nothing to do */
468     X509_STORE_free (ctx);
469     return 0;
470   }
471
472   X509_STORE_CTX_init (&xsc, ctx, peercert, NULL);
473
474   pass = (X509_verify_cert (&xsc) > 0);
475 #ifdef DEBUG
476   if (! pass)
477   {
478     char buf[SHORT_STRING];
479     int err;
480
481     err = X509_STORE_CTX_get_error (&xsc);
482     snprintf (buf, sizeof (buf), "%s (%d)", 
483         X509_verify_cert_error_string(err), err);
484     dprint (2, (debugfile, "X509_verify_cert: %s\n", buf));
485   }
486 #endif
487   X509_STORE_CTX_cleanup (&xsc);
488   X509_STORE_free (ctx);
489
490   return pass;
491 }
492
493 static int check_certificate_by_digest (X509 *peercert)
494 {
495   unsigned char peermd[EVP_MAX_MD_SIZE];
496   unsigned int peermdlen;
497   X509 *cert = NULL;
498   int pass = 0;
499   FILE *fp;
500
501   /* expiration check */
502   if (X509_cmp_current_time (X509_get_notBefore (peercert)) >= 0)
503   {
504     dprint (2, (debugfile, "Server certificate is not yet valid\n"));
505     mutt_error (_("Server certificate is not yet valid"));
506     mutt_sleep (2);
507     return 0;
508   }
509   if (X509_cmp_current_time (X509_get_notAfter (peercert)) <= 0)
510   {
511     dprint (2, (debugfile, "Server certificate has expired"));
512     mutt_error (_("Server certificate has expired"));
513     mutt_sleep (2);
514     return 0;
515   }
516
517   if ((fp = fopen (SslCertFile, "rt")) == NULL)
518     return 0;
519
520   if (!X509_digest (peercert, EVP_sha1(), peermd, &peermdlen))
521   {
522     fclose (fp);
523     return 0;
524   }
525   
526   while ((cert = READ_X509_KEY (fp, &cert)) != NULL)
527   {
528     unsigned char md[EVP_MAX_MD_SIZE];
529     unsigned int mdlen;
530
531     /* Avoid CPU-intensive digest calculation if the certificates are
532      * not even remotely equal.
533      */
534     if (X509_subject_name_cmp (cert, peercert) != 0 ||
535         X509_issuer_name_cmp (cert, peercert) != 0)
536       continue;
537
538     if (!X509_digest (cert, EVP_sha1(), md, &mdlen) || peermdlen != mdlen)
539       continue;
540     
541     if (memcmp(peermd, md, mdlen) != 0)
542       continue;
543
544     pass = 1;
545     break;
546   }
547   X509_free (cert);
548   fclose (fp);
549
550   return pass;
551 }
552
553 static int ssl_check_certificate (sslsockdata * data)
554 {
555   char *part[] =
556   {"/CN=", "/Email=", "/O=", "/OU=", "/L=", "/ST=", "/C="};
557   char helpstr[SHORT_STRING];
558   char buf[SHORT_STRING];
559   MUTTMENU *menu;
560   int done, row, i;
561   FILE *fp;
562   char *name = NULL, *c;
563
564   if (check_certificate_by_signer (data->cert))
565   {
566     dprint (1, (debugfile, "ssl_check_certificate: signer check passed\n"));
567     return 1;
568   }
569
570   /* automatic check from user's database */
571   if (SslCertFile && check_certificate_by_digest (data->cert))
572   {
573     dprint (1, (debugfile, "ssl_check_certificate: digest check passed\n"));
574     return 1;
575   }
576
577   /* interactive check from user */
578   menu = mutt_new_menu ();
579   menu->max = 19;
580   menu->dialog = (char **) safe_calloc (1, menu->max * sizeof (char *));
581   for (i = 0; i < menu->max; i++)
582     menu->dialog[i] = (char *) safe_calloc (1, SHORT_STRING * sizeof (char));
583
584   row = 0;
585   strfcpy (menu->dialog[row], _("This certificate belongs to:"), SHORT_STRING);
586   row++;
587   name = X509_NAME_oneline (X509_get_subject_name (data->cert),
588                             buf, sizeof (buf));
589   for (i = 0; i < 5; i++)
590   {
591     c = x509_get_part (name, part[i]);
592     snprintf (menu->dialog[row++], SHORT_STRING, "   %s", c);
593   }
594
595   row++;
596   strfcpy (menu->dialog[row], _("This certificate was issued by:"), SHORT_STRING);
597   row++;
598   name = X509_NAME_oneline (X509_get_issuer_name (data->cert),
599                             buf, sizeof (buf));
600   for (i = 0; i < 5; i++)
601   {
602     c = x509_get_part (name, part[i]);
603     snprintf (menu->dialog[row++], SHORT_STRING, "   %s", c);
604   }
605
606   row++;
607   snprintf (menu->dialog[row++], SHORT_STRING, "%s", _("This certificate is valid"));
608   snprintf (menu->dialog[row++], SHORT_STRING, _("   from %s"), 
609       asn1time_to_string (X509_get_notBefore (data->cert)));
610   snprintf (menu->dialog[row++], SHORT_STRING, _("     to %s"), 
611       asn1time_to_string (X509_get_notAfter (data->cert)));
612
613   row++;
614   buf[0] = '\0';
615   x509_fingerprint (buf, sizeof (buf), data->cert);
616   snprintf (menu->dialog[row++], SHORT_STRING, _("Fingerprint: %s"), buf);
617
618   menu->title = _("SSL Certificate check");
619   if (SslCertFile)
620   {
621     menu->prompt = _("(r)eject, accept (o)nce, (a)ccept always");
622     menu->keys = _("roa");
623   }
624   else
625   {
626     menu->prompt = _("(r)eject, accept (o)nce");
627     menu->keys = _("ro");
628   }
629   
630   helpstr[0] = '\0';
631   mutt_make_help (buf, sizeof (buf), _("Exit  "), MENU_GENERIC, OP_EXIT);
632   safe_strcat (helpstr, sizeof (helpstr), buf);
633   mutt_make_help (buf, sizeof (buf), _("Help"), MENU_GENERIC, OP_HELP);
634   safe_strcat (helpstr, sizeof (helpstr), buf);
635   menu->help = helpstr;
636
637   done = 0;
638   set_option(OPTUNBUFFEREDINPUT);
639   while (!done)
640   {
641     switch (mutt_menuLoop (menu))
642     {
643       case -1:                  /* abort */
644       case OP_MAX + 1:          /* reject */
645       case OP_EXIT:
646         done = 1;
647         break;
648       case OP_MAX + 3:          /* accept always */
649         done = 0;
650         if ((fp = fopen (SslCertFile, "a")))
651         {
652           if (PEM_write_X509 (fp, data->cert))
653             done = 1;
654           fclose (fp);
655         }
656         if (!done)
657         {
658           mutt_error (_("Warning: Couldn't save certificate"));
659           mutt_sleep (2);
660         }
661         else
662         {
663           mutt_message (_("Certificate saved"));
664           mutt_sleep (0);
665         }
666         /* fall through */
667       case OP_MAX + 2:          /* accept once */
668         done = 2;
669         break;
670     }
671   }
672   unset_option(OPTUNBUFFEREDINPUT);
673   mutt_menuDestroy (&menu);
674   return (done == 2);
675 }
676
677 static void ssl_get_client_cert(sslsockdata *ssldata, CONNECTION *conn)
678 {
679   if (SslClientCert)
680   {
681     dprint (2, (debugfile, "Using client certificate %s\n", SslClientCert));
682     SSL_CTX_set_default_passwd_cb_userdata(ssldata->ctx, &conn->account);
683     SSL_CTX_set_default_passwd_cb(ssldata->ctx, ssl_passwd_cb);
684     SSL_CTX_use_certificate_file(ssldata->ctx, SslClientCert, SSL_FILETYPE_PEM);
685     SSL_CTX_use_PrivateKey_file(ssldata->ctx, SslClientCert, SSL_FILETYPE_PEM);
686   }
687 }
688
689 static int ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
690 {
691   ACCOUNT *account = (ACCOUNT*)userdata;
692
693   if (mutt_account_getuser (account))
694     return 0;
695
696   dprint (2, (debugfile, "ssl_passwd_cb: getting password for %s@%s:%u\n",
697               account->user, account->host, account->port));
698   
699   if (mutt_account_getpass (account))
700     return 0;
701
702   return snprintf(buf, size, "%s", account->pass);
703 }