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.
22 #include "mutt_curses.h"
25 #include "mutt_crypt.h"
44 #ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
48 #ifdef HAVE_SYS_RESOURCE_H
49 # include <sys/resource.h>
53 /* print the current time to avoid spoofing of the signature output */
54 void crypt_current_time (STATE * s, char *app_name)
57 char p[STRING], tmp[STRING];
62 if (option (OPTCRYPTTIMESTAMP)) {
64 setlocale (LC_TIME, "");
65 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
66 setlocale (LC_TIME, "C");
71 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
72 NONULL (app_name), p);
73 state_attach_puts (tmp, s);
78 void crypt_forget_passphrase (void)
80 if ((WithCrypto & APPLICATION_PGP))
81 crypt_pgp_void_passphrase ();
83 if ((WithCrypto & APPLICATION_SMIME))
84 crypt_smime_void_passphrase ();
87 mutt_message _("Passphrase(s) forgotten.");
91 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
93 static void disable_coredumps (void)
95 struct rlimit rl = { 0, 0 };
96 static short done = 0;
99 setrlimit (RLIMIT_CORE, &rl);
104 #endif /* HAVE_SETRLIMIT */
107 int crypt_valid_passphrase (int flags)
111 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
112 disable_coredumps ();
115 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
116 ret = crypt_pgp_valid_passphrase ();
118 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
119 ret = crypt_smime_valid_passphrase ();
126 int mutt_protect (HEADER * msg, char *keylist)
128 BODY *pbody = NULL, *tmp_pbody = NULL;
129 BODY *tmp_smime_pbody = NULL;
130 BODY *tmp_pgp_pbody = NULL;
131 int flags = (WithCrypto & APPLICATION_PGP) ? msg->security : 0;
137 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
140 if ((WithCrypto & APPLICATION_PGP)
141 && ((msg->security & PGPINLINE) == PGPINLINE)) {
142 /* they really want to send it inline... go for it */
144 mutt_endwin _("Invoking PGP...");
146 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
148 msg->content = pbody;
152 /* otherwise inline won't work...ask for revert */
154 query_quadoption (OPT_PGPMIMEAUTO,
156 ("Message can't be sent inline. Revert to using PGP/MIME?")))
158 mutt_error _("Mail not sent.");
163 /* go ahead with PGP/MIME */
169 if ((WithCrypto & APPLICATION_SMIME))
170 tmp_smime_pbody = msg->content;
171 if ((WithCrypto & APPLICATION_PGP))
172 tmp_pgp_pbody = msg->content;
174 if (msg->security & SIGN) {
175 if ((WithCrypto & APPLICATION_SMIME)
176 && (msg->security & APPLICATION_SMIME)) {
177 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
179 pbody = tmp_smime_pbody = tmp_pbody;
182 if ((WithCrypto & APPLICATION_PGP)
183 && (msg->security & APPLICATION_PGP)
184 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
185 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
189 pbody = tmp_pgp_pbody = tmp_pbody;
192 if (WithCrypto && (msg->security & APPLICATION_SMIME)
193 && (msg->security & APPLICATION_PGP)) {
194 /* here comes the draft ;-) */
199 if (msg->security & ENCRYPT) {
200 if ((WithCrypto & APPLICATION_SMIME)
201 && (msg->security & APPLICATION_SMIME)) {
202 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
204 /* signed ? free it! */
207 /* free tmp_body if messages was signed AND encrypted ... */
208 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
209 /* detatch and dont't delete msg->content,
210 which tmp_smime_pbody->parts after signing. */
211 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
212 msg->content->next = NULL;
213 mutt_free_body (&tmp_smime_pbody);
218 if ((WithCrypto & APPLICATION_PGP)
219 && (msg->security & APPLICATION_PGP)) {
220 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
223 /* did we perform a retainable signature? */
224 if (flags != msg->security) {
225 /* remove the outer multipart layer */
226 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
227 /* get rid of the signature */
228 mutt_free_body (&tmp_pgp_pbody->next);
234 /* destroy temporary signature envelope when doing retainable
238 if (flags != msg->security) {
239 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
240 mutt_free_body (&tmp_pgp_pbody->next);
246 msg->content = pbody;
254 int mutt_is_multipart_signed (BODY * b)
258 if (!b || !(b->type == TYPEMULTIPART) ||
259 !b->subtype || ascii_strcasecmp (b->subtype, "signed"))
262 if (!(p = mutt_get_parameter ("protocol", b->parameter)))
265 if (!(ascii_strcasecmp (p, "multipart/mixed")))
268 if ((WithCrypto & APPLICATION_PGP)
269 && !(ascii_strcasecmp (p, "application/pgp-signature")))
272 if ((WithCrypto & APPLICATION_SMIME)
273 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
275 if ((WithCrypto & APPLICATION_SMIME)
276 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
283 int mutt_is_multipart_encrypted (BODY * b)
285 if ((WithCrypto & APPLICATION_PGP)) {
288 if (!b || b->type != TYPEMULTIPART ||
289 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
290 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
291 ascii_strcasecmp (p, "application/pgp-encrypted"))
301 int mutt_is_application_pgp (BODY * m)
306 if (m->type == TYPEAPPLICATION) {
307 if (!ascii_strcasecmp (m->subtype, "pgp")
308 || !ascii_strcasecmp (m->subtype, "x-pgp-message")) {
309 if ((p = mutt_get_parameter ("x-action", m->parameter))
310 && (!ascii_strcasecmp (p, "sign")
311 || !ascii_strcasecmp (p, "signclear")))
314 if ((p = mutt_get_parameter ("format", m->parameter)) &&
315 !ascii_strcasecmp (p, "keys-only"))
319 t |= PGPENCRYPT; /* not necessarily correct, but... */
322 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
325 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
328 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0) {
329 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
330 || (p = mutt_get_parameter ("x-action", m->parameter))
331 || (p = mutt_get_parameter ("action", m->parameter)))
332 && !ascii_strncasecmp ("pgp-sign", p, 8))
334 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
336 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
345 int mutt_is_application_smime (BODY * m)
348 int len, complain = 0;
353 if ((m->type & TYPEAPPLICATION) && m->subtype) {
354 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
355 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
356 !ascii_strcasecmp (m->subtype, "pkcs7-mime")) {
357 if ((t = mutt_get_parameter ("smime-type", m->parameter))) {
358 if (!ascii_strcasecmp (t, "enveloped-data"))
360 else if (!ascii_strcasecmp (t, "signed-data"))
361 return (SMIMESIGN | SMIMEOPAQUE);
366 * Content-Description: S/MIME Encrypted Message
367 * instead of Content-Type parameter
369 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
373 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
376 t = mutt_get_parameter ("name", m->parameter);
385 ("S/MIME messages with no hints on content are unsupported."));
389 /* no .p7c, .p10 support yet. */
391 len = str_len (t) - 4;
392 if (len > 0 && *(t + len) == '.') {
394 if (!ascii_strcasecmp ((t + len), "p7m"))
398 /* Not sure if this is the correct thing to do, but
399 it's required for compatibility with Outlook */
400 return (SMIMESIGN | SMIMEOPAQUE);
402 else if (!ascii_strcasecmp ((t + len), "p7s"))
403 return (SMIMESIGN | SMIMEOPAQUE);
415 int crypt_query (BODY * m)
425 if (m->type == TYPEAPPLICATION) {
426 if ((WithCrypto & APPLICATION_PGP))
427 t |= mutt_is_application_pgp (m);
429 if ((WithCrypto & APPLICATION_SMIME)) {
430 t |= mutt_is_application_smime (m);
437 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT) {
438 t |= mutt_is_application_pgp (m);
443 if (m->type == TYPEMULTIPART) {
444 t |= mutt_is_multipart_encrypted (m);
445 t |= mutt_is_multipart_signed (m);
451 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
455 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
456 w = 0; /* Bits set in any part */
458 for (p = m->parts; p; p = p->next) {
463 t |= u | (w & ~GOODSIGN);
465 if ((w & GOODSIGN) && !(u & GOODSIGN))
475 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
485 if (!(fp = safe_fopen (tempfile, "w"))) {
486 mutt_perror (tempfile);
490 fseeko (s->fpin, a->hdr_offset, 0);
491 bytes = a->length + a->offset - a->hdr_offset;
494 if ((c = fgetc (s->fpin)) == EOF)
502 if (c == '\n' && !hadcr)
518 void convert_to_7bit (BODY * a)
524 if (a->type == TYPEMULTIPART) {
525 if (a->encoding != ENC7BIT) {
526 a->encoding = ENC7BIT;
527 convert_to_7bit (a->parts);
529 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
530 convert_to_7bit (a->parts);
532 else if (a->type == TYPEMESSAGE &&
533 str_casecmp (a->subtype, "delivery-status")) {
534 if (a->encoding != ENC7BIT)
535 mutt_message_to_7bit (a, NULL);
537 else if (a->encoding == ENC8BIT)
538 a->encoding = ENCQUOTEDPRINTABLE;
539 else if (a->encoding == ENCBINARY)
540 a->encoding = ENCBASE64;
541 else if (a->content && a->encoding != ENCBASE64 &&
542 (a->content->from || (a->content->space &&
543 option (OPTPGPSTRICTENC))))
544 a->encoding = ENCQUOTEDPRINTABLE;
552 void crypt_extract_keys_from_messages (HEADER * h)
555 char tempfname[_POSIX_PATH_MAX], *mbox;
562 mutt_mktemp (tempfname);
563 if (!(fpout = safe_fopen (tempfname, "w"))) {
564 mutt_perror (tempfname);
568 if ((WithCrypto & APPLICATION_PGP))
569 set_option (OPTDONTHANDLEPGPKEYS);
572 for (i = 0; i < Context->vcount; i++) {
573 if (Context->hdrs[Context->v2r[i]]->tagged) {
574 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
575 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
576 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
582 if ((WithCrypto & APPLICATION_PGP)
583 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP)) {
584 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
585 M_CM_DECODE | M_CM_CHARCONV, 0);
588 mutt_endwin (_("Trying to extract PGP keys...\n"));
589 crypt_pgp_invoke_import (tempfname);
592 if ((WithCrypto & APPLICATION_SMIME)
593 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME)) {
594 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
595 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
596 M_CM_NOHEADER | M_CM_DECODE_CRYPT
597 | M_CM_DECODE_SMIME, 0);
599 mutt_copy_message (fpout, Context,
600 Context->hdrs[Context->v2r[i]], 0, 0);
603 if (Context->hdrs[Context->v2r[i]]->env->from)
604 tmp = mutt_expand_aliases (h->env->from);
605 else if (Context->hdrs[Context->v2r[i]]->env->sender)
606 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
608 mbox = tmp ? tmp->mailbox : NULL;
610 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
611 crypt_smime_invoke_import (tempfname, mbox);
621 mutt_parse_mime_message (Context, h);
622 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
623 if ((WithCrypto & APPLICATION_PGP)
624 && (h->security & APPLICATION_PGP)) {
625 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
627 mutt_endwin (_("Trying to extract PGP keys...\n"));
628 crypt_pgp_invoke_import (tempfname);
631 if ((WithCrypto & APPLICATION_SMIME)
632 && (h->security & APPLICATION_SMIME)) {
633 if (h->security & ENCRYPT)
634 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
635 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
637 mutt_copy_message (fpout, Context, h, 0, 0);
641 tmp = mutt_expand_aliases (h->env->from);
642 else if (h->env->sender)
643 tmp = mutt_expand_aliases (h->env->sender);
644 mbox = tmp ? tmp->mailbox : NULL;
645 if (mbox) { /* else ? */
646 mutt_message (_("Trying to extract S/MIME certificates...\n"));
647 crypt_smime_invoke_import (tempfname, mbox);
655 mutt_any_key_to_continue (NULL);
657 mutt_unlink (tempfname);
659 if ((WithCrypto & APPLICATION_PGP))
660 unset_option (OPTDONTHANDLEPGPKEYS);
665 int crypt_get_keys (HEADER * msg, char **keylist)
667 /* Do a quick check to make sure that we can find all of the encryption
668 * keys if the user has requested this service.
674 if ((WithCrypto & APPLICATION_PGP))
675 set_option (OPTPGPCHECKTRUST);
679 if (msg->security & ENCRYPT) {
680 if ((WithCrypto & APPLICATION_PGP)
681 && (msg->security & APPLICATION_PGP)) {
682 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
683 msg->env->bcc)) == NULL)
685 unset_option (OPTPGPCHECKTRUST);
687 if ((WithCrypto & APPLICATION_SMIME)
688 && (msg->security & APPLICATION_SMIME)) {
689 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
690 msg->env->bcc)) == NULL)
700 static void crypt_fetch_signatures (BODY *** signatures, BODY * a, int *n)
705 for (; a; a = a->next) {
706 if (a->type == TYPEMULTIPART)
707 crypt_fetch_signatures (signatures, a->parts, n);
710 mem_realloc (signatures, (*n + 6) * sizeof (BODY **));
712 (*signatures)[(*n)++] = a;
719 * This routine verifies a "multipart/signed" body.
722 int mutt_signed_handler (BODY * a, STATE * s)
724 char tempfile[_POSIX_PATH_MAX];
726 int protocol_major = TYPEOTHER;
727 char *protocol_minor = NULL;
730 BODY **signatures = NULL;
739 protocol = mutt_get_parameter ("protocol", a->parameter);
742 /* extract the protocol information */
748 if ((protocol_minor = strchr (protocol, '/')))
751 strfcpy (major, protocol, sizeof (major));
752 if ((t = strchr (major, '/')))
755 protocol_major = mutt_check_mime_type (major);
758 /* consistency check */
760 if (!(a && a->next && a->next->type == protocol_major &&
761 !str_casecmp (a->next->subtype, protocol_minor))) {
762 state_attach_puts (_("[-- Error: "
763 "Inconsistent multipart/signed structure! --]\n\n"),
765 return mutt_body_handler (a, s);
769 if ((WithCrypto & APPLICATION_PGP)
770 && protocol_major == TYPEAPPLICATION
771 && !str_casecmp (protocol_minor, "pgp-signature"));
772 else if ((WithCrypto & APPLICATION_SMIME)
773 && protocol_major == TYPEAPPLICATION
774 && !(str_casecmp (protocol_minor, "x-pkcs7-signature")
775 && str_casecmp (protocol_minor, "pkcs7-signature")));
776 else if (protocol_major == TYPEMULTIPART
777 && !str_casecmp (protocol_minor, "mixed"));
779 state_printf (s, _("[-- Error: "
780 "Unknown multipart/signed protocol %s! --]\n\n"),
782 return mutt_body_handler (a, s);
785 if (s->flags & M_DISPLAY) {
787 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
790 mutt_mktemp (tempfile);
791 if (crypt_write_signed (a, s, tempfile) == 0) {
792 for (i = 0; i < sigcnt; i++) {
793 if ((WithCrypto & APPLICATION_PGP)
794 && signatures[i]->type == TYPEAPPLICATION
795 && !str_casecmp (signatures[i]->subtype, "pgp-signature")) {
796 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
802 if ((WithCrypto & APPLICATION_SMIME)
803 && signatures[i]->type == TYPEAPPLICATION
805 (!str_casecmp (signatures[i]->subtype, "x-pkcs7-signature")
806 || !str_casecmp (signatures[i]->subtype,
807 "pkcs7-signature"))) {
808 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
814 state_printf (s, _("[-- Warning: "
815 "We can't verify %s/%s signatures. --]\n\n"),
816 TYPE (signatures[i]), signatures[i]->subtype);
820 mutt_unlink (tempfile);
822 b->goodsig = goodsig;
823 b->badsig = !goodsig;
825 /* Now display the signed body */
826 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
829 mem_free (&signatures);
832 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
836 rc = mutt_body_handler (a, s);
838 if (s->flags & M_DISPLAY && sigcnt)
839 state_attach_puts (_("\n[-- End of signed data --]\n"), s);