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