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