~ubuntu-dev/ubuntu/lucid/mutt/lucid-201002110857

« back to all changes in this revision

Viewing changes to mutt_ssl.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2009-06-07 17:30:03 UTC
  • mto: (16.2.1 experimental) (2.3.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20090607173003-rg37ui3h2bbv7wl0
Tags: upstream-1.5.19
ImportĀ upstreamĀ versionĀ 1.5.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
#include <openssl/ssl.h>
24
24
#include <openssl/x509.h>
 
25
#include <openssl/x509v3.h>
25
26
#include <openssl/err.h>
26
27
#include <openssl/rand.h>
27
28
 
34
35
#include "mutt_menu.h"
35
36
#include "mutt_curses.h"
36
37
#include "mutt_ssl.h"
 
38
#include "mutt_idna.h"
37
39
 
38
40
#if OPENSSL_VERSION_NUMBER >= 0x00904000L
39
41
#define READ_X509_KEY(fp, key)  PEM_read_X509(fp, key, NULL, NULL)
58
60
#define HAVE_ENTROPY()  (!access(DEVRANDOM, R_OK) || entropy_byte_count >= 16)
59
61
#endif
60
62
 
 
63
/* keep a handle on accepted certificates in case we want to
 
64
 * open up another connection to the same server in this session */
 
65
static STACK_OF(X509) *SslSessionCerts = NULL;
 
66
 
61
67
typedef struct _sslsockdata
62
68
{
63
69
  SSL_CTX *ctx;
67
73
sslsockdata;
68
74
 
69
75
/* local prototypes */
70
 
int ssl_init (void);
 
76
static int ssl_init (void);
71
77
static int add_entropy (const char *file);
72
78
static int ssl_socket_read (CONNECTION* conn, char* buf, size_t len);
73
79
static int ssl_socket_write (CONNECTION* conn, const char* buf, size_t len);
74
80
static int ssl_socket_open (CONNECTION * conn);
75
81
static int ssl_socket_close (CONNECTION * conn);
76
82
static int tls_close (CONNECTION* conn);
77
 
static int ssl_check_certificate (sslsockdata * data);
 
83
static int ssl_cache_trusted_cert (X509 *cert);
 
84
static int ssl_check_certificate (CONNECTION *conn, sslsockdata * data);
 
85
static int interactive_check_cert (X509 *cert, int idx, int len);
78
86
static void ssl_get_client_cert(sslsockdata *ssldata, CONNECTION *conn);
79
87
static int ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata);
80
 
static int ssl_negotiate (sslsockdata*);
 
88
static int ssl_negotiate (CONNECTION *conn, sslsockdata*);
81
89
 
82
90
/* mutt_ssl_starttls: Negotiate TLS over an already opened connection.
83
91
 *   TODO: Merge this code better with ssl_socket_open. */
111
119
    goto bail_ssl;
112
120
  }
113
121
 
114
 
  if (ssl_negotiate (ssldata))
 
122
  if (ssl_negotiate (conn, ssldata))
115
123
    goto bail_ssl;
116
124
 
117
125
  /* hmm. watch out if we're starting TLS over any method other than raw. */
145
153
 * versions also. (That's the reason for the ugly #ifdefs and macros,
146
154
 * otherwise I could have simply #ifdef'd the whole ssl_init funcion)
147
155
 */
148
 
int ssl_init (void)
 
156
static int ssl_init (void)
149
157
{
150
158
  char path[_POSIX_PATH_MAX];
151
159
  static unsigned char init_complete = 0;
290
298
  data->ssl = SSL_new (data->ctx);
291
299
  SSL_set_fd (data->ssl, conn->fd);
292
300
 
293
 
  if (ssl_negotiate(data))
 
301
  if (ssl_negotiate(conn, data))
294
302
  {
295
303
    mutt_socket_close (conn);
296
304
    return -1;
304
312
 
305
313
/* ssl_negotiate: After SSL state has been initialised, attempt to negotiate
306
314
 *   SSL over the wire, including certificate checks. */
307
 
static int ssl_negotiate (sslsockdata* ssldata)
 
315
static int ssl_negotiate (CONNECTION *conn, sslsockdata* ssldata)
308
316
{
309
317
  int err;
310
318
  const char* errmsg;
343
351
    return -1;
344
352
  }
345
353
 
346
 
  if (!ssl_check_certificate (ssldata))
 
354
  if (!ssl_check_certificate (conn, ssldata))
347
355
    return -1;
348
356
 
349
357
  mutt_message (_("SSL connection using %s (%s)"), 
450
458
{
451
459
  X509_STORE_CTX xsc;
452
460
  X509_STORE *ctx;
453
 
  int pass = 0;
 
461
  int pass = 0, i;
454
462
 
455
463
  ctx = X509_STORE_new ();
456
464
  if (ctx == NULL) return 0;
468
476
  else
469
477
    dprint (2, (debugfile, "X509_STORE_load_locations_failed\n"));
470
478
 
 
479
  for (i = 0; i < sk_X509_num (SslSessionCerts); i++)
 
480
    pass += (X509_STORE_add_cert (ctx, sk_X509_value (SslSessionCerts, i)) != 0);
 
481
 
471
482
  if (pass == 0)
472
483
  {
473
484
    /* nothing to do */
475
486
    return 0;
476
487
  }
477
488
 
478
 
  X509_STORE_CTX_init (&xsc, ctx, peercert, NULL);
 
489
  X509_STORE_CTX_init (&xsc, ctx, peercert, SslSessionCerts);
479
490
 
480
491
  pass = (X509_verify_cert (&xsc) > 0);
481
492
#ifdef DEBUG
488
499
    snprintf (buf, sizeof (buf), "%s (%d)", 
489
500
        X509_verify_cert_error_string(err), err);
490
501
    dprint (2, (debugfile, "X509_verify_cert: %s\n", buf));
 
502
    dprint (2, (debugfile, " [%s]\n", peercert->name));
491
503
  }
492
504
#endif
493
505
  X509_STORE_CTX_cleanup (&xsc);
523
535
  unsigned char peermd[EVP_MAX_MD_SIZE];
524
536
  unsigned int peermdlen;
525
537
  X509 *cert;
526
 
  LIST *scert;
 
538
  int i;
527
539
 
528
 
  if (!X509_digest (peercert, EVP_sha1(), peermd, &peermdlen))
 
540
  if (!X509_digest (peercert, EVP_sha1(), peermd, &peermdlen)
 
541
      || !SslSessionCerts)
529
542
  {
530
543
    return 0;
531
544
  }
532
545
  
533
 
  for (scert = SslSessionCerts; scert; scert = scert->next)
 
546
  for (i = sk_X509_num (SslSessionCerts); i-- > 0;)
534
547
  {
535
 
    cert = *(X509**)scert->data;
 
548
    cert = sk_X509_value (SslSessionCerts, i);
536
549
    if (!compare_certificates (cert, peercert, peermd, peermdlen))
537
550
    {
538
551
      return 1;
588
601
  return pass;
589
602
}
590
603
 
591
 
static int ssl_check_certificate (sslsockdata * data)
 
604
/* port to mutt from msmtp's tls.c */
 
605
static int hostname_match (const char *hostname, const char *certname)
 
606
{
 
607
  const char *cmp1, *cmp2;
 
608
 
 
609
  if (strncmp(certname, "*.", 2) == 0)
 
610
  {
 
611
    cmp1 = certname + 2;
 
612
    cmp2 = strchr(hostname, '.');
 
613
    if (!cmp2)
 
614
    {
 
615
      return 0;
 
616
    }
 
617
    else
 
618
    {
 
619
      cmp2++;
 
620
    }
 
621
  }
 
622
  else
 
623
  {
 
624
    cmp1 = certname;
 
625
    cmp2 = hostname;
 
626
  }
 
627
 
 
628
  if (*cmp1 == '\0' || *cmp2 == '\0')
 
629
  {
 
630
    return 0;
 
631
  }
 
632
 
 
633
  if (strcasecmp(cmp1, cmp2) != 0)
 
634
  {
 
635
    return 0;
 
636
  }
 
637
 
 
638
  return 1;
 
639
}
 
640
 
 
641
/* port to mutt from msmtp's tls.c */
 
642
static int check_host (X509 *x509cert, const char *hostname, char *err, size_t errlen)
 
643
{
 
644
  int i, rc = 0;
 
645
  /* hostname in ASCII format: */
 
646
  char *hostname_ascii = NULL;
 
647
  /* needed to get the common name: */
 
648
  X509_NAME *x509_subject;
 
649
  char *buf = NULL;
 
650
  int bufsize;
 
651
  /* needed to get the DNS subjectAltNames: */
 
652
  STACK *subj_alt_names;
 
653
  int subj_alt_names_count;
 
654
  GENERAL_NAME *subj_alt_name;
 
655
  /* did we find a name matching hostname? */
 
656
  int match_found;
 
657
 
 
658
  /* Check if 'hostname' matches the one of the subjectAltName extensions of
 
659
   * type DNS or the Common Name (CN). */
 
660
 
 
661
#ifdef HAVE_LIBIDN
 
662
  if (idna_to_ascii_lz(hostname, &hostname_ascii, 0) != IDNA_SUCCESS)
 
663
  {
 
664
    hostname_ascii = safe_strdup(hostname);
 
665
  }
 
666
#else
 
667
  hostname_ascii = safe_strdup(hostname);
 
668
#endif
 
669
 
 
670
  /* Try the DNS subjectAltNames. */
 
671
  match_found = 0;
 
672
  if ((subj_alt_names = X509_get_ext_d2i(x509cert, NID_subject_alt_name,
 
673
                                         NULL, NULL)))
 
674
  {
 
675
    subj_alt_names_count = sk_GENERAL_NAME_num(subj_alt_names);
 
676
    for (i = 0; i < subj_alt_names_count; i++)
 
677
    {
 
678
      subj_alt_name = sk_GENERAL_NAME_value(subj_alt_names, i);
 
679
      if (subj_alt_name->type == GEN_DNS)
 
680
      {
 
681
        if ((match_found = hostname_match(hostname_ascii,
 
682
                                          (char *)(subj_alt_name->d.ia5->data))))
 
683
        {
 
684
          break;
 
685
        }
 
686
      }
 
687
    }
 
688
  }
 
689
 
 
690
  if (!match_found)
 
691
  {
 
692
    /* Try the common name */
 
693
    if (!(x509_subject = X509_get_subject_name(x509cert)))
 
694
    {
 
695
      if (err && errlen)
 
696
        strfcpy (err, _("cannot get certificate subject"), errlen);
 
697
      goto out;
 
698
    }
 
699
 
 
700
    bufsize = X509_NAME_get_text_by_NID(x509_subject, NID_commonName,
 
701
                                        NULL, 0);
 
702
    bufsize++;
 
703
    buf = safe_malloc((size_t)bufsize);
 
704
    if (X509_NAME_get_text_by_NID(x509_subject, NID_commonName,
 
705
                                  buf, bufsize) == -1)
 
706
    {
 
707
      if (err && errlen)
 
708
        strfcpy (err, _("cannot get certificate common name"), errlen);
 
709
      goto out;
 
710
    }
 
711
    match_found = hostname_match(hostname_ascii, buf);
 
712
  }
 
713
 
 
714
  if (!match_found)
 
715
  {
 
716
    if (err && errlen)
 
717
      snprintf (err, errlen, _("certificate owner does not match hostname %s"),
 
718
                hostname);
 
719
    goto out;
 
720
  }
 
721
 
 
722
  rc = 1;
 
723
 
 
724
out:
 
725
  FREE(&buf);
 
726
  FREE(&hostname_ascii);
 
727
 
 
728
  return rc;
 
729
}
 
730
 
 
731
static int ssl_cache_trusted_cert (X509 *c)
 
732
{
 
733
  dprint (1, (debugfile, "trusted: %s\n", c->name));
 
734
  if (!SslSessionCerts)
 
735
    SslSessionCerts = sk_new_null();
 
736
  return (sk_X509_push (SslSessionCerts, X509_dup(c)));
 
737
}
 
738
 
 
739
/* check whether cert is preauthorized */
 
740
static int ssl_check_preauth (X509 *cert, CONNECTION *conn)
 
741
{
 
742
  char buf[SHORT_STRING];
 
743
 
 
744
  /* check session cache first */
 
745
  if (check_certificate_cache (cert))
 
746
  {
 
747
    dprint (2, (debugfile, "ssl_check_preauth: using cached certificate\n"));
 
748
    return 1;
 
749
  }
 
750
 
 
751
  buf[0] = 0;
 
752
  if (!check_host (cert, conn->account.host, buf, sizeof (buf)))
 
753
  {
 
754
    mutt_error (_("Certificate host check failed: %s"), buf);
 
755
    mutt_sleep (2);
 
756
    return -1;
 
757
  }
 
758
  dprint (2, (debugfile, "ssl_check_preauth: hostname check passed\n"));
 
759
 
 
760
  if (check_certificate_by_signer (cert))
 
761
  {
 
762
    dprint (2, (debugfile, "ssl_check_preauth: signer check passed\n"));
 
763
    return 1;
 
764
  }
 
765
 
 
766
  /* automatic check from user's database */
 
767
  if (SslCertFile && check_certificate_by_digest (cert))
 
768
  {
 
769
    dprint (2, (debugfile, "ssl_check_preauth: digest check passed\n"));
 
770
    return 1;
 
771
  }
 
772
 
 
773
  return 0;
 
774
}
 
775
 
 
776
static int ssl_check_certificate (CONNECTION *conn, sslsockdata *data)
 
777
{
 
778
  int i, preauthrc, chain_len;
 
779
  STACK_OF(X509) *chain;
 
780
  X509 *cert;
 
781
 
 
782
  if ((preauthrc = ssl_check_preauth (data->cert, conn)) > 0)
 
783
    return preauthrc;
 
784
 
 
785
  chain = SSL_get_peer_cert_chain (data->ssl);
 
786
  chain_len = sk_X509_num (chain);
 
787
  if (!chain || (chain_len < 1))
 
788
    return interactive_check_cert (data->cert, 0, 0);
 
789
 
 
790
  /* check the chain from root to peer */
 
791
  for (i = chain_len-1; i >= 0; i--)
 
792
  {
 
793
    cert = sk_X509_value (chain, i);
 
794
    if (check_certificate_cache (cert))
 
795
      dprint (2, (debugfile, "ssl chain: already cached: %s\n", cert->name));
 
796
    else if (i /* 0 is the peer */ || !preauthrc)
 
797
    {
 
798
      if (check_certificate_by_signer (cert))
 
799
      {
 
800
        dprint (2, (debugfile, "ssl chain: checked by signer: %s\n", cert->name));
 
801
        ssl_cache_trusted_cert (cert);
 
802
        return 1;
 
803
      }
 
804
      else if (SslCertFile && check_certificate_by_digest (cert))
 
805
      {
 
806
        dprint (2, (debugfile, "ssl chain: trusted with file: %s\n", cert->name));
 
807
        ssl_cache_trusted_cert (cert);
 
808
        return 1;
 
809
      }
 
810
      else /* allow users to shoot their foot */
 
811
      {
 
812
        dprint (2, (debugfile, "ssl chain: check failed: %s\n", cert->name));
 
813
        if (interactive_check_cert (cert, i, chain_len))
 
814
          return 1;
 
815
      }
 
816
    }
 
817
    else /* highly suspicious because (i==0 && preauthrc < 0) */
 
818
      if (interactive_check_cert (cert, i, chain_len))
 
819
        return 1;
 
820
  }
 
821
 
 
822
  return 0;
 
823
}
 
824
 
 
825
static int interactive_check_cert (X509 *cert, int idx, int len)
592
826
{
593
827
  char *part[] =
594
 
  {"/CN=", "/Email=", "/O=", "/OU=", "/L=", "/ST=", "/C="};
 
828
    {"/CN=", "/Email=", "/O=", "/OU=", "/L=", "/ST=", "/C="};
595
829
  char helpstr[LONG_STRING];
596
 
  char buf[SHORT_STRING];
597
 
  MUTTMENU *menu;
 
830
  char buf[STRING];
 
831
  char title[STRING];
 
832
  MUTTMENU *menu = mutt_new_menu (-1);
598
833
  int done, row, i;
599
834
  FILE *fp;
600
835
  char *name = NULL, *c;
601
836
 
602
 
  /* check session cache first */
603
 
  if (check_certificate_cache (data->cert))
604
 
  {
605
 
    dprint (1, (debugfile, "ssl_check_certificate: using cached certificate\n"));
606
 
    return 1;
607
 
  }
608
 
 
609
 
  if (check_certificate_by_signer (data->cert))
610
 
  {
611
 
    dprint (1, (debugfile, "ssl_check_certificate: signer check passed\n"));
612
 
    return 1;
613
 
  }
614
 
 
615
 
  /* automatic check from user's database */
616
 
  if (SslCertFile && check_certificate_by_digest (data->cert))
617
 
  {
618
 
    dprint (1, (debugfile, "ssl_check_certificate: digest check passed\n"));
619
 
    return 1;
620
 
  }
621
 
 
622
 
  /* interactive check from user */
623
 
  menu = mutt_new_menu ();
 
837
  dprint (2, (debugfile, "interactive_check_cert: %s\n", cert->name));
 
838
 
624
839
  menu->max = 19;
625
840
  menu->dialog = (char **) safe_calloc (1, menu->max * sizeof (char *));
626
841
  for (i = 0; i < menu->max; i++)
629
844
  row = 0;
630
845
  strfcpy (menu->dialog[row], _("This certificate belongs to:"), SHORT_STRING);
631
846
  row++;
632
 
  name = X509_NAME_oneline (X509_get_subject_name (data->cert),
 
847
  name = X509_NAME_oneline (X509_get_subject_name (cert),
633
848
                            buf, sizeof (buf));
 
849
  dprint (2, (debugfile, "oneline: %s\n", name));
 
850
  
634
851
  for (i = 0; i < 5; i++)
635
852
  {
636
853
    c = x509_get_part (name, part[i]);
640
857
  row++;
641
858
  strfcpy (menu->dialog[row], _("This certificate was issued by:"), SHORT_STRING);
642
859
  row++;
643
 
  name = X509_NAME_oneline (X509_get_issuer_name (data->cert),
 
860
  name = X509_NAME_oneline (X509_get_issuer_name (cert),
644
861
                            buf, sizeof (buf));
645
862
  for (i = 0; i < 5; i++)
646
863
  {
651
868
  row++;
652
869
  snprintf (menu->dialog[row++], SHORT_STRING, _("This certificate is valid"));
653
870
  snprintf (menu->dialog[row++], SHORT_STRING, _("   from %s"), 
654
 
      asn1time_to_string (X509_get_notBefore (data->cert)));
 
871
      asn1time_to_string (X509_get_notBefore (cert)));
655
872
  snprintf (menu->dialog[row++], SHORT_STRING, _("     to %s"), 
656
 
      asn1time_to_string (X509_get_notAfter (data->cert)));
 
873
      asn1time_to_string (X509_get_notAfter (cert)));
657
874
 
658
875
  row++;
659
876
  buf[0] = '\0';
660
 
  x509_fingerprint (buf, sizeof (buf), data->cert);
 
877
  x509_fingerprint (buf, sizeof (buf), cert);
661
878
  snprintf (menu->dialog[row++], SHORT_STRING, _("Fingerprint: %s"), buf);
662
879
 
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)
 
880
  snprintf (title, sizeof (title),
 
881
            _("SSL Certificate check (certificate %d of %d in chain)"),
 
882
            len - idx, len);
 
883
  menu->title = title;
 
884
  if (SslCertFile && X509_cmp_current_time (X509_get_notAfter (cert)) >= 0
 
885
      && X509_cmp_current_time (X509_get_notBefore (cert)) < 0)
666
886
  {
667
887
    menu->prompt = _("(r)eject, accept (o)nce, (a)ccept always");
668
888
    menu->keys = _("roa");
695
915
        done = 0;
696
916
        if ((fp = fopen (SslCertFile, "a")))
697
917
        {
698
 
          if (PEM_write_X509 (fp, data->cert))
 
918
          if (PEM_write_X509 (fp, cert))
699
919
            done = 1;
700
920
          fclose (fp);
701
921
        }
712
932
        /* fall through */
713
933
      case OP_MAX + 2:          /* accept once */
714
934
        done = 2;
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,
718
 
                                           sizeof (X509 **));
 
935
        ssl_cache_trusted_cert (cert);
719
936
        break;
720
937
    }
721
938
  }
722
939
  unset_option(OPTUNBUFFEREDINPUT);
723
940
  mutt_menuDestroy (&menu);
 
941
  dprint (2, (debugfile, "ssl interactive_check_cert: done=%d\n", done));
724
942
  return (done == 2);
725
943
}
726
944
 
733
951
    SSL_CTX_set_default_passwd_cb(ssldata->ctx, ssl_passwd_cb);
734
952
    SSL_CTX_use_certificate_file(ssldata->ctx, SslClientCert, SSL_FILETYPE_PEM);
735
953
    SSL_CTX_use_PrivateKey_file(ssldata->ctx, SslClientCert, SSL_FILETYPE_PEM);
 
954
    
 
955
    /* if we are using a client cert, SASL may expect an external auth name */
 
956
    mutt_account_getuser (&conn->account);
736
957
  }
737
958
}
738
959