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.
19 #include <lib-lib/str.h>
20 #include <lib-lib/file.h>
21 #include <lib-lib/ascii.h>
22 #include <lib-lib/mem.h>
23 #include <lib-lib/macros.h>
27 #include "mutt_curses.h"
30 #include "mutt_crypt.h"
46 #ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
50 #ifdef HAVE_SYS_RESOURCE_H
51 # include <sys/resource.h>
55 /* print the current time to avoid spoofing of the signature output */
56 void crypt_current_time (STATE * s, const char *app_name)
59 char p[STRING], tmp[STRING];
64 if (option (OPTCRYPTTIMESTAMP)) {
66 setlocale (LC_TIME, "");
67 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
68 setlocale (LC_TIME, "C");
73 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
74 NONULL (app_name), p);
75 state_attach_puts (tmp, s);
80 void crypt_forget_passphrase (void)
82 if ((WithCrypto & APPLICATION_PGP))
83 crypt_pgp_void_passphrase ();
85 if ((WithCrypto & APPLICATION_SMIME))
86 crypt_smime_void_passphrase ();
89 mutt_message _("Passphrase(s) forgotten.");
93 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
95 static void disable_coredumps (void)
97 struct rlimit rl = { 0, 0 };
98 static short done = 0;
101 setrlimit (RLIMIT_CORE, &rl);
106 #endif /* HAVE_SETRLIMIT */
109 int crypt_valid_passphrase (int flags)
113 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
114 disable_coredumps ();
117 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
118 ret = crypt_pgp_valid_passphrase ();
120 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
121 ret = crypt_smime_valid_passphrase ();
128 int mutt_protect (HEADER * msg, char *keylist)
130 BODY *pbody = NULL, *tmp_pbody = NULL;
131 BODY *tmp_smime_pbody = NULL;
132 BODY *tmp_pgp_pbody = NULL;
133 int flags = (WithCrypto & APPLICATION_PGP) ? msg->security : 0;
139 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
142 if ((WithCrypto & APPLICATION_PGP)
143 && ((msg->security & PGPINLINE) == PGPINLINE)) {
144 /* they really want to send it inline... go for it */
146 mutt_endwin _("Invoking PGP...");
148 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
150 msg->content = pbody;
154 /* otherwise inline won't work...ask for revert */
156 query_quadoption (OPT_PGPMIMEAUTO,
158 ("Message can't be sent inline. Revert to using PGP/MIME?")))
160 mutt_error _("Mail not sent.");
165 /* go ahead with PGP/MIME */
171 if ((WithCrypto & APPLICATION_SMIME))
172 tmp_smime_pbody = msg->content;
173 if ((WithCrypto & APPLICATION_PGP))
174 tmp_pgp_pbody = msg->content;
176 if (msg->security & SIGN) {
177 if ((WithCrypto & APPLICATION_SMIME)
178 && (msg->security & APPLICATION_SMIME)) {
179 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
181 pbody = tmp_smime_pbody = tmp_pbody;
184 if ((WithCrypto & APPLICATION_PGP)
185 && (msg->security & APPLICATION_PGP)
186 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
187 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
191 pbody = tmp_pgp_pbody = tmp_pbody;
194 if (WithCrypto && (msg->security & APPLICATION_SMIME)
195 && (msg->security & APPLICATION_PGP)) {
196 /* here comes the draft ;-) */
201 if (msg->security & ENCRYPT) {
202 if ((WithCrypto & APPLICATION_SMIME)
203 && (msg->security & APPLICATION_SMIME)) {
204 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
206 /* signed ? free it! */
209 /* free tmp_body if messages was signed AND encrypted ... */
210 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
211 /* detatch and dont't delete msg->content,
212 which tmp_smime_pbody->parts after signing. */
213 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
214 msg->content->next = NULL;
215 mutt_free_body (&tmp_smime_pbody);
220 if ((WithCrypto & APPLICATION_PGP)
221 && (msg->security & APPLICATION_PGP)) {
222 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
225 /* did we perform a retainable signature? */
226 if (flags != msg->security) {
227 /* remove the outer multipart layer */
228 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
229 /* get rid of the signature */
230 mutt_free_body (&tmp_pgp_pbody->next);
236 /* destroy temporary signature envelope when doing retainable
240 if (flags != msg->security) {
241 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
242 mutt_free_body (&tmp_pgp_pbody->next);
248 msg->content = pbody;
256 int mutt_is_multipart_signed (BODY * b)
260 if (!b || !(b->type == TYPEMULTIPART) ||
261 !b->subtype || ascii_strcasecmp (b->subtype, "signed"))
264 if (!(p = mutt_get_parameter ("protocol", b->parameter)))
267 if (!(ascii_strcasecmp (p, "multipart/mixed")))
270 if ((WithCrypto & APPLICATION_PGP)
271 && !(ascii_strcasecmp (p, "application/pgp-signature")))
274 if ((WithCrypto & APPLICATION_SMIME)
275 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
277 if ((WithCrypto & APPLICATION_SMIME)
278 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
285 int mutt_is_multipart_encrypted (BODY * b)
287 if ((WithCrypto & APPLICATION_PGP)) {
290 if (!b || b->type != TYPEMULTIPART ||
291 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
292 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
293 ascii_strcasecmp (p, "application/pgp-encrypted"))
303 int mutt_is_application_pgp (BODY * m)
308 if (m->type == TYPEAPPLICATION) {
309 if (!ascii_strcasecmp (m->subtype, "pgp")
310 || !ascii_strcasecmp (m->subtype, "x-pgp-message")) {
311 if ((p = mutt_get_parameter ("x-action", m->parameter))
312 && (!ascii_strcasecmp (p, "sign")
313 || !ascii_strcasecmp (p, "signclear")))
316 if ((p = mutt_get_parameter ("format", m->parameter)) &&
317 !ascii_strcasecmp (p, "keys-only"))
321 t |= PGPENCRYPT; /* not necessarily correct, but... */
324 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
327 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
330 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0) {
331 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
332 || (p = mutt_get_parameter ("x-action", m->parameter))
333 || (p = mutt_get_parameter ("action", m->parameter)))
334 && !ascii_strncasecmp ("pgp-sign", p, 8))
336 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
338 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
347 int mutt_is_application_smime (BODY * m)
350 int len, complain = 0;
355 if ((m->type & TYPEAPPLICATION) && m->subtype) {
356 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
357 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
358 !ascii_strcasecmp (m->subtype, "pkcs7-mime")) {
359 if ((t = mutt_get_parameter ("smime-type", m->parameter))) {
360 if (!ascii_strcasecmp (t, "enveloped-data"))
362 else if (!ascii_strcasecmp (t, "signed-data"))
363 return (SMIMESIGN | SMIMEOPAQUE);
368 * Content-Description: S/MIME Encrypted Message
369 * instead of Content-Type parameter
371 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
375 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
378 t = mutt_get_parameter ("name", m->parameter);
387 ("S/MIME messages with no hints on content are unsupported."));
391 /* no .p7c, .p10 support yet. */
393 len = m_strlen(t) - 4;
394 if (len > 0 && *(t + len) == '.') {
396 if (!ascii_strcasecmp ((t + len), "p7m"))
400 /* Not sure if this is the correct thing to do, but
401 it's required for compatibility with Outlook */
402 return (SMIMESIGN | SMIMEOPAQUE);
404 else if (!ascii_strcasecmp ((t + len), "p7s"))
405 return (SMIMESIGN | SMIMEOPAQUE);
417 int crypt_query (BODY * m)
427 if (m->type == TYPEAPPLICATION) {
428 if ((WithCrypto & APPLICATION_PGP))
429 t |= mutt_is_application_pgp (m);
431 if ((WithCrypto & APPLICATION_SMIME)) {
432 t |= mutt_is_application_smime (m);
439 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT) {
440 t |= mutt_is_application_pgp (m);
445 if (m->type == TYPEMULTIPART) {
446 t |= mutt_is_multipart_encrypted (m);
447 t |= mutt_is_multipart_signed (m);
453 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
457 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
458 w = 0; /* Bits set in any part */
460 for (p = m->parts; p; p = p->next) {
465 t |= u | (w & ~GOODSIGN);
467 if ((w & GOODSIGN) && !(u & GOODSIGN))
477 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
487 if (!(fp = safe_fopen (tempfile, "w"))) {
488 mutt_perror (tempfile);
492 fseeko (s->fpin, a->hdr_offset, 0);
493 bytes = a->length + a->offset - a->hdr_offset;
496 if ((c = fgetc (s->fpin)) == EOF)
504 if (c == '\n' && !hadcr)
520 void convert_to_7bit (BODY * a)
526 if (a->type == TYPEMULTIPART) {
527 if (a->encoding != ENC7BIT) {
528 a->encoding = ENC7BIT;
529 convert_to_7bit (a->parts);
531 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
532 convert_to_7bit (a->parts);
534 else if (a->type == TYPEMESSAGE &&
535 m_strcasecmp(a->subtype, "delivery-status")) {
536 if (a->encoding != ENC7BIT)
537 mutt_message_to_7bit (a, NULL);
539 else if (a->encoding == ENC8BIT)
540 a->encoding = ENCQUOTEDPRINTABLE;
541 else if (a->encoding == ENCBINARY)
542 a->encoding = ENCBASE64;
543 else if (a->content && a->encoding != ENCBASE64 &&
544 (a->content->from || (a->content->space &&
545 option (OPTPGPSTRICTENC))))
546 a->encoding = ENCQUOTEDPRINTABLE;
554 void crypt_extract_keys_from_messages (HEADER * h)
557 char tempfname[_POSIX_PATH_MAX], *mbox;
564 mutt_mktemp (tempfname);
565 if (!(fpout = safe_fopen (tempfname, "w"))) {
566 mutt_perror (tempfname);
570 if ((WithCrypto & APPLICATION_PGP))
571 set_option (OPTDONTHANDLEPGPKEYS);
574 for (i = 0; i < Context->vcount; i++) {
575 if (Context->hdrs[Context->v2r[i]]->tagged) {
576 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
577 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
578 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
584 if ((WithCrypto & APPLICATION_PGP)
585 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP)) {
586 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
587 M_CM_DECODE | M_CM_CHARCONV, 0);
590 mutt_endwin (_("Trying to extract PGP keys...\n"));
591 crypt_pgp_invoke_import (tempfname);
594 if ((WithCrypto & APPLICATION_SMIME)
595 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME)) {
596 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
597 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
598 M_CM_NOHEADER | M_CM_DECODE_CRYPT
599 | M_CM_DECODE_SMIME, 0);
601 mutt_copy_message (fpout, Context,
602 Context->hdrs[Context->v2r[i]], 0, 0);
605 if (Context->hdrs[Context->v2r[i]]->env->from)
606 tmp = mutt_expand_aliases (h->env->from);
607 else if (Context->hdrs[Context->v2r[i]]->env->sender)
608 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
610 mbox = tmp ? tmp->mailbox : NULL;
612 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
613 crypt_smime_invoke_import (tempfname, mbox);
623 mutt_parse_mime_message (Context, h);
624 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
625 if ((WithCrypto & APPLICATION_PGP)
626 && (h->security & APPLICATION_PGP)) {
627 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
629 mutt_endwin (_("Trying to extract PGP keys...\n"));
630 crypt_pgp_invoke_import (tempfname);
633 if ((WithCrypto & APPLICATION_SMIME)
634 && (h->security & APPLICATION_SMIME)) {
635 if (h->security & ENCRYPT)
636 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
637 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
639 mutt_copy_message (fpout, Context, h, 0, 0);
643 tmp = mutt_expand_aliases (h->env->from);
644 else if (h->env->sender)
645 tmp = mutt_expand_aliases (h->env->sender);
646 mbox = tmp ? tmp->mailbox : NULL;
647 if (mbox) { /* else ? */
648 mutt_message (_("Trying to extract S/MIME certificates...\n"));
649 crypt_smime_invoke_import (tempfname, mbox);
657 mutt_any_key_to_continue (NULL);
659 mutt_unlink (tempfname);
661 if ((WithCrypto & APPLICATION_PGP))
662 unset_option (OPTDONTHANDLEPGPKEYS);
667 int crypt_get_keys (HEADER * msg, char **keylist)
669 /* Do a quick check to make sure that we can find all of the encryption
670 * keys if the user has requested this service.
676 if ((WithCrypto & APPLICATION_PGP))
677 set_option (OPTPGPCHECKTRUST);
681 if (msg->security & ENCRYPT) {
682 if ((WithCrypto & APPLICATION_PGP)
683 && (msg->security & APPLICATION_PGP)) {
684 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
685 msg->env->bcc)) == NULL)
687 unset_option (OPTPGPCHECKTRUST);
689 if ((WithCrypto & APPLICATION_SMIME)
690 && (msg->security & APPLICATION_SMIME)) {
691 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
692 msg->env->bcc)) == NULL)
702 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
707 for (; a; a = a->next) {
708 if (a->type == TYPEMULTIPART)
709 crypt_fetch_signatures (signatures, a->parts, n);
712 p_realloc(signatures, *n + 6);
714 (*signatures)[(*n)++] = a;
721 * This routine verifies a "multipart/signed" body.
724 int mutt_signed_handler (BODY * a, STATE * s)
726 char tempfile[_POSIX_PATH_MAX];
728 int protocol_major = TYPEOTHER;
729 char *protocol_minor = NULL;
732 BODY **signatures = NULL;
741 protocol = mutt_get_parameter ("protocol", a->parameter);
744 /* extract the protocol information */
750 if ((protocol_minor = strchr (protocol, '/')))
753 m_strcpy(major, sizeof(major), protocol);
754 if ((t = strchr (major, '/')))
757 protocol_major = mutt_check_mime_type (major);
760 /* consistency check */
762 if (!(a && a->next && a->next->type == protocol_major &&
763 !m_strcasecmp(a->next->subtype, protocol_minor))) {
764 state_attach_puts (_("[-- Error: "
765 "Inconsistent multipart/signed structure! --]\n\n"),
767 return mutt_body_handler (a, s);
771 if ((WithCrypto & APPLICATION_PGP)
772 && protocol_major == TYPEAPPLICATION
773 && !m_strcasecmp(protocol_minor, "pgp-signature"));
774 else if ((WithCrypto & APPLICATION_SMIME)
775 && protocol_major == TYPEAPPLICATION
776 && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
777 && m_strcasecmp(protocol_minor, "pkcs7-signature")));
778 else if (protocol_major == TYPEMULTIPART
779 && !m_strcasecmp(protocol_minor, "mixed"));
781 state_printf (s, _("[-- Error: "
782 "Unknown multipart/signed protocol %s! --]\n\n"),
784 return mutt_body_handler (a, s);
787 if (s->flags & M_DISPLAY) {
789 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
792 mutt_mktemp (tempfile);
793 if (crypt_write_signed (a, s, tempfile) == 0) {
794 for (i = 0; i < sigcnt; i++) {
795 if ((WithCrypto & APPLICATION_PGP)
796 && signatures[i]->type == TYPEAPPLICATION
797 && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
798 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
804 if ((WithCrypto & APPLICATION_SMIME)
805 && signatures[i]->type == TYPEAPPLICATION
807 (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
808 || !m_strcasecmp(signatures[i]->subtype,
809 "pkcs7-signature"))) {
810 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
816 state_printf (s, _("[-- Warning: "
817 "We can't verify %s/%s signatures. --]\n\n"),
818 TYPE (signatures[i]), signatures[i]->subtype);
822 mutt_unlink (tempfile);
824 b->goodsig = goodsig;
825 b->badsig = !goodsig;
827 /* Now display the signed body */
828 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
831 p_delete(&signatures);
834 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
838 rc = mutt_body_handler (a, s);
840 if (s->flags & M_DISPLAY && sigcnt)
841 state_attach_puts (_("\n[-- End of signed data --]\n"), s);