2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
6 * Oliver Ehli <elmy@acm.org>
7 * Copyright (C) 2003 Werner Koch <wk@gnupg.org>
8 * Copyright (C) 2004 g10code GmbH
10 * This file is part of mutt-ng, see http://www.muttng.org/.
11 * It's licensed under the GNU General Public License,
12 * please see the file GPL in the top level source directory.
20 #include "mutt_curses.h"
23 #include "mutt_crypt.h"
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
46 #ifdef HAVE_SYS_RESOURCE_H
47 # include <sys/resource.h>
51 /* print the current time to avoid spoofing of the signature output */
52 void crypt_current_time (STATE * s, char *app_name)
55 char p[STRING], tmp[STRING];
60 if (option (OPTCRYPTTIMESTAMP)) {
62 setlocale (LC_TIME, "");
63 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
64 setlocale (LC_TIME, "C");
69 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
70 NONULL (app_name), p);
71 state_attach_puts (tmp, s);
76 void crypt_forget_passphrase (void)
78 if ((WithCrypto & APPLICATION_PGP))
79 crypt_pgp_void_passphrase ();
81 if ((WithCrypto & APPLICATION_SMIME))
82 crypt_smime_void_passphrase ();
85 mutt_message _("Passphrase(s) forgotten.");
89 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
91 static void disable_coredumps (void)
93 struct rlimit rl = { 0, 0 };
94 static short done = 0;
97 setrlimit (RLIMIT_CORE, &rl);
102 #endif /* HAVE_SETRLIMIT */
105 int crypt_valid_passphrase (int flags)
109 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
110 disable_coredumps ();
113 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
114 ret = crypt_pgp_valid_passphrase ();
116 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
117 ret = crypt_smime_valid_passphrase ();
124 int mutt_protect (HEADER * msg, char *keylist)
126 BODY *pbody = NULL, *tmp_pbody = NULL;
127 BODY *tmp_smime_pbody = NULL;
128 BODY *tmp_pgp_pbody = NULL;
129 int flags = (WithCrypto & APPLICATION_PGP) ? msg->security : 0;
135 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
138 if ((WithCrypto & APPLICATION_PGP)
139 && ((msg->security & PGPINLINE) == PGPINLINE)) {
140 /* they really want to send it inline... go for it */
142 mutt_endwin _("Invoking PGP...");
144 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
146 msg->content = pbody;
150 /* otherwise inline won't work...ask for revert */
152 query_quadoption (OPT_PGPMIMEAUTO,
154 ("Message can't be sent inline. Revert to using PGP/MIME?")))
156 mutt_error _("Mail not sent.");
161 /* go ahead with PGP/MIME */
167 if ((WithCrypto & APPLICATION_SMIME))
168 tmp_smime_pbody = msg->content;
169 if ((WithCrypto & APPLICATION_PGP))
170 tmp_pgp_pbody = msg->content;
172 if (msg->security & SIGN) {
173 if ((WithCrypto & APPLICATION_SMIME)
174 && (msg->security & APPLICATION_SMIME)) {
175 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
177 pbody = tmp_smime_pbody = tmp_pbody;
180 if ((WithCrypto & APPLICATION_PGP)
181 && (msg->security & APPLICATION_PGP)
182 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
183 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
187 pbody = tmp_pgp_pbody = tmp_pbody;
190 if (WithCrypto && (msg->security & APPLICATION_SMIME)
191 && (msg->security & APPLICATION_PGP)) {
192 /* here comes the draft ;-) */
197 if (msg->security & ENCRYPT) {
198 if ((WithCrypto & APPLICATION_SMIME)
199 && (msg->security & APPLICATION_SMIME)) {
200 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
202 /* signed ? free it! */
205 /* free tmp_body if messages was signed AND encrypted ... */
206 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
207 /* detatch and dont't delete msg->content,
208 which tmp_smime_pbody->parts after signing. */
209 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
210 msg->content->next = NULL;
211 mutt_free_body (&tmp_smime_pbody);
216 if ((WithCrypto & APPLICATION_PGP)
217 && (msg->security & APPLICATION_PGP)) {
218 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
221 /* did we perform a retainable signature? */
222 if (flags != msg->security) {
223 /* remove the outer multipart layer */
224 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
225 /* get rid of the signature */
226 mutt_free_body (&tmp_pgp_pbody->next);
232 /* destroy temporary signature envelope when doing retainable
236 if (flags != msg->security) {
237 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
238 mutt_free_body (&tmp_pgp_pbody->next);
244 msg->content = pbody;
252 int mutt_is_multipart_signed (BODY * b)
256 if (!b || !(b->type == TYPEMULTIPART) ||
257 !b->subtype || ascii_strcasecmp (b->subtype, "signed"))
260 if (!(p = mutt_get_parameter ("protocol", b->parameter)))
263 if (!(ascii_strcasecmp (p, "multipart/mixed")))
266 if ((WithCrypto & APPLICATION_PGP)
267 && !(ascii_strcasecmp (p, "application/pgp-signature")))
270 if ((WithCrypto & APPLICATION_SMIME)
271 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
273 if ((WithCrypto & APPLICATION_SMIME)
274 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
281 int mutt_is_multipart_encrypted (BODY * b)
283 if ((WithCrypto & APPLICATION_PGP)) {
286 if (!b || b->type != TYPEMULTIPART ||
287 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
288 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
289 ascii_strcasecmp (p, "application/pgp-encrypted"))
299 int mutt_is_application_pgp (BODY * m)
304 if (m->type == TYPEAPPLICATION) {
305 if (!ascii_strcasecmp (m->subtype, "pgp")
306 || !ascii_strcasecmp (m->subtype, "x-pgp-message")) {
307 if ((p = mutt_get_parameter ("x-action", m->parameter))
308 && (!ascii_strcasecmp (p, "sign")
309 || !ascii_strcasecmp (p, "signclear")))
312 if ((p = mutt_get_parameter ("format", m->parameter)) &&
313 !ascii_strcasecmp (p, "keys-only"))
317 t |= PGPENCRYPT; /* not necessarily correct, but... */
320 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
323 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
326 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0) {
327 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
328 || (p = mutt_get_parameter ("x-action", m->parameter))
329 || (p = mutt_get_parameter ("action", m->parameter)))
330 && !ascii_strncasecmp ("pgp-sign", p, 8))
332 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
334 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
343 int mutt_is_application_smime (BODY * m)
346 int len, complain = 0;
351 if ((m->type & TYPEAPPLICATION) && m->subtype) {
352 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
353 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
354 !ascii_strcasecmp (m->subtype, "pkcs7-mime")) {
355 if ((t = mutt_get_parameter ("smime-type", m->parameter))) {
356 if (!ascii_strcasecmp (t, "enveloped-data"))
358 else if (!ascii_strcasecmp (t, "signed-data"))
359 return (SMIMESIGN | SMIMEOPAQUE);
364 * Content-Description: S/MIME Encrypted Message
365 * instead of Content-Type parameter
367 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
371 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
374 t = mutt_get_parameter ("name", m->parameter);
383 ("S/MIME messages with no hints on content are unsupported."));
387 /* no .p7c, .p10 support yet. */
389 len = mutt_strlen (t) - 4;
390 if (len > 0 && *(t + len) == '.') {
392 if (!ascii_strcasecmp ((t + len), "p7m"))
396 /* Not sure if this is the correct thing to do, but
397 it's required for compatibility with Outlook */
398 return (SMIMESIGN | SMIMEOPAQUE);
400 else if (!ascii_strcasecmp ((t + len), "p7s"))
401 return (SMIMESIGN | SMIMEOPAQUE);
413 int crypt_query (BODY * m)
423 if (m->type == TYPEAPPLICATION) {
424 if ((WithCrypto & APPLICATION_PGP))
425 t |= mutt_is_application_pgp (m);
427 if ((WithCrypto & APPLICATION_SMIME)) {
428 t |= mutt_is_application_smime (m);
435 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT) {
436 t |= mutt_is_application_pgp (m);
441 if (m->type == TYPEMULTIPART) {
442 t |= mutt_is_multipart_encrypted (m);
443 t |= mutt_is_multipart_signed (m);
449 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
453 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
454 w = 0; /* Bits set in any part */
456 for (p = m->parts; p; p = p->next) {
461 t |= u | (w & ~GOODSIGN);
463 if ((w & GOODSIGN) && !(u & GOODSIGN))
473 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
483 if (!(fp = safe_fopen (tempfile, "w"))) {
484 mutt_perror (tempfile);
488 fseek (s->fpin, a->hdr_offset, 0);
489 bytes = a->length + a->offset - a->hdr_offset;
492 if ((c = fgetc (s->fpin)) == EOF)
500 if (c == '\n' && !hadcr)
516 void convert_to_7bit (BODY * a)
522 if (a->type == TYPEMULTIPART) {
523 if (a->encoding != ENC7BIT) {
524 a->encoding = ENC7BIT;
525 convert_to_7bit (a->parts);
527 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
528 convert_to_7bit (a->parts);
530 else if (a->type == TYPEMESSAGE &&
531 mutt_strcasecmp (a->subtype, "delivery-status")) {
532 if (a->encoding != ENC7BIT)
533 mutt_message_to_7bit (a, NULL);
535 else if (a->encoding == ENC8BIT)
536 a->encoding = ENCQUOTEDPRINTABLE;
537 else if (a->encoding == ENCBINARY)
538 a->encoding = ENCBASE64;
539 else if (a->content && a->encoding != ENCBASE64 &&
540 (a->content->from || (a->content->space &&
541 option (OPTPGPSTRICTENC))))
542 a->encoding = ENCQUOTEDPRINTABLE;
550 void crypt_extract_keys_from_messages (HEADER * h)
553 char tempfname[_POSIX_PATH_MAX], *mbox;
560 mutt_mktemp (tempfname);
561 if (!(fpout = safe_fopen (tempfname, "w"))) {
562 mutt_perror (tempfname);
566 if ((WithCrypto & APPLICATION_PGP))
567 set_option (OPTDONTHANDLEPGPKEYS);
570 for (i = 0; i < Context->vcount; i++) {
571 if (Context->hdrs[Context->v2r[i]]->tagged) {
572 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
573 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
574 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
580 if ((WithCrypto & APPLICATION_PGP)
581 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP)) {
582 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
583 M_CM_DECODE | M_CM_CHARCONV, 0);
586 mutt_endwin (_("Trying to extract PGP keys...\n"));
587 crypt_pgp_invoke_import (tempfname);
590 if ((WithCrypto & APPLICATION_SMIME)
591 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME)) {
592 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
593 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
594 M_CM_NOHEADER | M_CM_DECODE_CRYPT
595 | M_CM_DECODE_SMIME, 0);
597 mutt_copy_message (fpout, Context,
598 Context->hdrs[Context->v2r[i]], 0, 0);
601 if (Context->hdrs[Context->v2r[i]]->env->from)
602 tmp = mutt_expand_aliases (h->env->from);
603 else if (Context->hdrs[Context->v2r[i]]->env->sender)
604 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
606 mbox = tmp ? tmp->mailbox : NULL;
608 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
609 crypt_smime_invoke_import (tempfname, mbox);
619 mutt_parse_mime_message (Context, h);
620 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
621 if ((WithCrypto & APPLICATION_PGP)
622 && (h->security & APPLICATION_PGP)) {
623 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
625 mutt_endwin (_("Trying to extract PGP keys...\n"));
626 crypt_pgp_invoke_import (tempfname);
629 if ((WithCrypto & APPLICATION_SMIME)
630 && (h->security & APPLICATION_SMIME)) {
631 if (h->security & ENCRYPT)
632 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
633 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
635 mutt_copy_message (fpout, Context, h, 0, 0);
639 tmp = mutt_expand_aliases (h->env->from);
640 else if (h->env->sender)
641 tmp = mutt_expand_aliases (h->env->sender);
642 mbox = tmp ? tmp->mailbox : NULL;
643 if (mbox) { /* else ? */
644 mutt_message (_("Trying to extract S/MIME certificates...\n"));
645 crypt_smime_invoke_import (tempfname, mbox);
653 mutt_any_key_to_continue (NULL);
655 mutt_unlink (tempfname);
657 if ((WithCrypto & APPLICATION_PGP))
658 unset_option (OPTDONTHANDLEPGPKEYS);
663 int crypt_get_keys (HEADER * msg, char **keylist)
665 /* Do a quick check to make sure that we can find all of the encryption
666 * keys if the user has requested this service.
672 if ((WithCrypto & APPLICATION_PGP))
673 set_option (OPTPGPCHECKTRUST);
677 if (msg->security & ENCRYPT) {
678 if ((WithCrypto & APPLICATION_PGP)
679 && (msg->security & APPLICATION_PGP)) {
680 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
681 msg->env->bcc)) == NULL)
683 unset_option (OPTPGPCHECKTRUST);
685 if ((WithCrypto & APPLICATION_SMIME)
686 && (msg->security & APPLICATION_SMIME)) {
687 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
688 msg->env->bcc)) == NULL)
698 static void crypt_fetch_signatures (BODY *** signatures, BODY * a, int *n)
703 for (; a; a = a->next) {
704 if (a->type == TYPEMULTIPART)
705 crypt_fetch_signatures (signatures, a->parts, n);
708 safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
710 (*signatures)[(*n)++] = a;
717 * This routine verifies a "multipart/signed" body.
720 void mutt_signed_handler (BODY * a, STATE * s)
722 char tempfile[_POSIX_PATH_MAX];
724 int protocol_major = TYPEOTHER;
725 char *protocol_minor = NULL;
728 BODY **signatures = NULL;
736 protocol = mutt_get_parameter ("protocol", a->parameter);
739 /* extract the protocol information */
745 if ((protocol_minor = strchr (protocol, '/')))
748 strfcpy (major, protocol, sizeof (major));
749 if ((t = strchr (major, '/')))
752 protocol_major = mutt_check_mime_type (major);
755 /* consistency check */
757 if (!(a && a->next && a->next->type == protocol_major &&
758 !mutt_strcasecmp (a->next->subtype, protocol_minor))) {
759 state_attach_puts (_("[-- Error: "
760 "Inconsistent multipart/signed structure! --]\n\n"),
762 mutt_body_handler (a, s);
767 if ((WithCrypto & APPLICATION_PGP)
768 && protocol_major == TYPEAPPLICATION
769 && !mutt_strcasecmp (protocol_minor, "pgp-signature"));
770 else if ((WithCrypto & APPLICATION_SMIME)
771 && protocol_major == TYPEAPPLICATION
772 && !(mutt_strcasecmp (protocol_minor, "x-pkcs7-signature")
773 && mutt_strcasecmp (protocol_minor, "pkcs7-signature")));
774 else if (protocol_major == TYPEMULTIPART
775 && !mutt_strcasecmp (protocol_minor, "mixed"));
777 state_printf (s, _("[-- Error: "
778 "Unknown multipart/signed protocol %s! --]\n\n"),
780 mutt_body_handler (a, s);
784 if (s->flags & M_DISPLAY) {
786 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
789 mutt_mktemp (tempfile);
790 if (crypt_write_signed (a, s, tempfile) == 0) {
791 for (i = 0; i < sigcnt; i++) {
792 if ((WithCrypto & APPLICATION_PGP)
793 && signatures[i]->type == TYPEAPPLICATION
794 && !mutt_strcasecmp (signatures[i]->subtype, "pgp-signature")) {
795 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
801 if ((WithCrypto & APPLICATION_SMIME)
802 && signatures[i]->type == TYPEAPPLICATION
804 (!mutt_strcasecmp (signatures[i]->subtype, "x-pkcs7-signature")
805 || !mutt_strcasecmp (signatures[i]->subtype,
806 "pkcs7-signature"))) {
807 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
813 state_printf (s, _("[-- Warning: "
814 "We can't verify %s/%s signatures. --]\n\n"),
815 TYPE (signatures[i]), signatures[i]->subtype);
819 mutt_unlink (tempfile);
821 b->goodsig = goodsig;
822 b->badsig = !goodsig;
824 /* Now display the signed body */
825 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
831 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
835 mutt_body_handler (a, s);
837 if (s->flags & M_DISPLAY && sigcnt)
838 state_attach_puts (_("\n[-- End of signed data --]\n"), s);