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>
25 #include <lib-mime/mime.h>
29 #include "mutt_curses.h"
31 #include "mutt_crypt.h"
47 #ifdef HAVE_SYS_TIME_H
48 # include <sys/time.h>
51 #ifdef HAVE_SYS_RESOURCE_H
52 # include <sys/resource.h>
56 /* print the current time to avoid spoofing of the signature output */
57 void crypt_current_time (STATE * s, const char *app_name)
60 char p[STRING], tmp[STRING];
65 if (option (OPTCRYPTTIMESTAMP)) {
67 setlocale (LC_TIME, "");
68 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
69 setlocale (LC_TIME, "C");
74 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
75 NONULL (app_name), p);
76 state_attach_puts (tmp, s);
81 void crypt_forget_passphrase (void)
83 if ((WithCrypto & APPLICATION_PGP))
84 crypt_pgp_void_passphrase ();
86 if ((WithCrypto & APPLICATION_SMIME))
87 crypt_smime_void_passphrase ();
90 mutt_message _("Passphrase(s) forgotten.");
94 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
96 static void disable_coredumps (void)
98 struct rlimit rl = { 0, 0 };
99 static short done = 0;
102 setrlimit (RLIMIT_CORE, &rl);
107 #endif /* HAVE_SETRLIMIT */
110 int crypt_valid_passphrase (int flags)
114 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
115 disable_coredumps ();
118 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
119 ret = crypt_pgp_valid_passphrase ();
121 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
122 ret = crypt_smime_valid_passphrase ();
129 int mutt_protect (HEADER * msg, char *keylist)
131 BODY *pbody = NULL, *tmp_pbody = NULL;
132 BODY *tmp_smime_pbody = NULL;
133 BODY *tmp_pgp_pbody = NULL;
134 int flags = (WithCrypto & APPLICATION_PGP) ? msg->security : 0;
140 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
143 if ((WithCrypto & APPLICATION_PGP)
144 && ((msg->security & PGPINLINE) == PGPINLINE)) {
145 /* they really want to send it inline... go for it */
147 mutt_endwin _("Invoking PGP...");
149 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
151 msg->content = pbody;
155 /* otherwise inline won't work...ask for revert */
157 query_quadoption (OPT_PGPMIMEAUTO,
159 ("Message can't be sent inline. Revert to using PGP/MIME?")))
161 mutt_error _("Mail not sent.");
166 /* go ahead with PGP/MIME */
172 if ((WithCrypto & APPLICATION_SMIME))
173 tmp_smime_pbody = msg->content;
174 if ((WithCrypto & APPLICATION_PGP))
175 tmp_pgp_pbody = msg->content;
177 if (msg->security & SIGN) {
178 if ((WithCrypto & APPLICATION_SMIME)
179 && (msg->security & APPLICATION_SMIME)) {
180 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
182 pbody = tmp_smime_pbody = tmp_pbody;
185 if ((WithCrypto & APPLICATION_PGP)
186 && (msg->security & APPLICATION_PGP)
187 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
188 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
192 pbody = tmp_pgp_pbody = tmp_pbody;
195 if (WithCrypto && (msg->security & APPLICATION_SMIME)
196 && (msg->security & APPLICATION_PGP)) {
197 /* here comes the draft ;-) */
202 if (msg->security & ENCRYPT) {
203 if ((WithCrypto & APPLICATION_SMIME)
204 && (msg->security & APPLICATION_SMIME)) {
205 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
207 /* signed ? free it! */
210 /* free tmp_body if messages was signed AND encrypted ... */
211 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
212 /* detatch and dont't delete msg->content,
213 which tmp_smime_pbody->parts after signing. */
214 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
215 msg->content->next = NULL;
216 mutt_free_body (&tmp_smime_pbody);
221 if ((WithCrypto & APPLICATION_PGP)
222 && (msg->security & APPLICATION_PGP)) {
223 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
226 /* did we perform a retainable signature? */
227 if (flags != msg->security) {
228 /* remove the outer multipart layer */
229 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
230 /* get rid of the signature */
231 mutt_free_body (&tmp_pgp_pbody->next);
237 /* destroy temporary signature envelope when doing retainable
241 if (flags != msg->security) {
242 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
243 mutt_free_body (&tmp_pgp_pbody->next);
249 msg->content = pbody;
257 int mutt_is_multipart_signed (BODY * b)
261 if (!b || !(b->type == TYPEMULTIPART) ||
262 !b->subtype || ascii_strcasecmp (b->subtype, "signed"))
265 if (!(p = mutt_get_parameter ("protocol", b->parameter)))
268 if (!(ascii_strcasecmp (p, "multipart/mixed")))
271 if ((WithCrypto & APPLICATION_PGP)
272 && !(ascii_strcasecmp (p, "application/pgp-signature")))
275 if ((WithCrypto & APPLICATION_SMIME)
276 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
278 if ((WithCrypto & APPLICATION_SMIME)
279 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
286 int mutt_is_multipart_encrypted (BODY * b)
288 if ((WithCrypto & APPLICATION_PGP)) {
291 if (!b || b->type != TYPEMULTIPART ||
292 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
293 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
294 ascii_strcasecmp (p, "application/pgp-encrypted"))
304 int mutt_is_application_pgp (BODY * m)
309 if (m->type == TYPEAPPLICATION) {
310 if (!ascii_strcasecmp (m->subtype, "pgp")
311 || !ascii_strcasecmp (m->subtype, "x-pgp-message")) {
312 if ((p = mutt_get_parameter ("x-action", m->parameter))
313 && (!ascii_strcasecmp (p, "sign")
314 || !ascii_strcasecmp (p, "signclear")))
317 if ((p = mutt_get_parameter ("format", m->parameter)) &&
318 !ascii_strcasecmp (p, "keys-only"))
322 t |= PGPENCRYPT; /* not necessarily correct, but... */
325 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
328 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
331 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0) {
332 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
333 || (p = mutt_get_parameter ("x-action", m->parameter))
334 || (p = mutt_get_parameter ("action", m->parameter)))
335 && !ascii_strncasecmp ("pgp-sign", p, 8))
337 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
339 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
348 int mutt_is_application_smime (BODY * m)
351 int len, complain = 0;
356 if ((m->type & TYPEAPPLICATION) && m->subtype) {
357 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
358 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
359 !ascii_strcasecmp (m->subtype, "pkcs7-mime")) {
360 if ((t = mutt_get_parameter ("smime-type", m->parameter))) {
361 if (!ascii_strcasecmp (t, "enveloped-data"))
363 else if (!ascii_strcasecmp (t, "signed-data"))
364 return (SMIMESIGN | SMIMEOPAQUE);
369 * Content-Description: S/MIME Encrypted Message
370 * instead of Content-Type parameter
372 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
376 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
379 t = mutt_get_parameter ("name", m->parameter);
388 ("S/MIME messages with no hints on content are unsupported."));
392 /* no .p7c, .p10 support yet. */
394 len = m_strlen(t) - 4;
395 if (len > 0 && *(t + len) == '.') {
397 if (!ascii_strcasecmp ((t + len), "p7m"))
401 /* Not sure if this is the correct thing to do, but
402 it's required for compatibility with Outlook */
403 return (SMIMESIGN | SMIMEOPAQUE);
405 else if (!ascii_strcasecmp ((t + len), "p7s"))
406 return (SMIMESIGN | SMIMEOPAQUE);
418 int crypt_query (BODY * m)
428 if (m->type == TYPEAPPLICATION) {
429 if ((WithCrypto & APPLICATION_PGP))
430 t |= mutt_is_application_pgp (m);
432 if ((WithCrypto & APPLICATION_SMIME)) {
433 t |= mutt_is_application_smime (m);
440 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT) {
441 t |= mutt_is_application_pgp (m);
446 if (m->type == TYPEMULTIPART) {
447 t |= mutt_is_multipart_encrypted (m);
448 t |= mutt_is_multipart_signed (m);
454 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
458 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
459 w = 0; /* Bits set in any part */
461 for (p = m->parts; p; p = p->next) {
466 t |= u | (w & ~GOODSIGN);
468 if ((w & GOODSIGN) && !(u & GOODSIGN))
478 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
488 if (!(fp = safe_fopen (tempfile, "w"))) {
489 mutt_perror (tempfile);
493 fseeko (s->fpin, a->hdr_offset, 0);
494 bytes = a->length + a->offset - a->hdr_offset;
497 if ((c = fgetc (s->fpin)) == EOF)
505 if (c == '\n' && !hadcr)
521 void convert_to_7bit (BODY * a)
527 if (a->type == TYPEMULTIPART) {
528 if (a->encoding != ENC7BIT) {
529 a->encoding = ENC7BIT;
530 convert_to_7bit (a->parts);
532 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
533 convert_to_7bit (a->parts);
535 else if (a->type == TYPEMESSAGE &&
536 m_strcasecmp(a->subtype, "delivery-status")) {
537 if (a->encoding != ENC7BIT)
538 mutt_message_to_7bit (a, NULL);
540 else if (a->encoding == ENC8BIT)
541 a->encoding = ENCQUOTEDPRINTABLE;
542 else if (a->encoding == ENCBINARY)
543 a->encoding = ENCBASE64;
544 else if (a->content && a->encoding != ENCBASE64 &&
545 (a->content->from || (a->content->space &&
546 option (OPTPGPSTRICTENC))))
547 a->encoding = ENCQUOTEDPRINTABLE;
555 void crypt_extract_keys_from_messages (HEADER * h)
558 char tempfname[_POSIX_PATH_MAX], *mbox;
559 address_t *tmp = NULL;
565 mutt_mktemp (tempfname);
566 if (!(fpout = safe_fopen (tempfname, "w"))) {
567 mutt_perror (tempfname);
571 if ((WithCrypto & APPLICATION_PGP))
572 set_option (OPTDONTHANDLEPGPKEYS);
575 for (i = 0; i < Context->vcount; i++) {
576 if (Context->hdrs[Context->v2r[i]]->tagged) {
577 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
578 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
579 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
585 if ((WithCrypto & APPLICATION_PGP)
586 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP)) {
587 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
588 M_CM_DECODE | M_CM_CHARCONV, 0);
591 mutt_endwin (_("Trying to extract PGP keys...\n"));
592 crypt_pgp_invoke_import (tempfname);
595 if ((WithCrypto & APPLICATION_SMIME)
596 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME)) {
597 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
598 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
599 M_CM_NOHEADER | M_CM_DECODE_CRYPT
600 | M_CM_DECODE_SMIME, 0);
602 mutt_copy_message (fpout, Context,
603 Context->hdrs[Context->v2r[i]], 0, 0);
606 if (Context->hdrs[Context->v2r[i]]->env->from)
607 tmp = mutt_expand_aliases (h->env->from);
608 else if (Context->hdrs[Context->v2r[i]]->env->sender)
609 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
611 mbox = tmp ? tmp->mailbox : NULL;
613 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
614 crypt_smime_invoke_import (tempfname, mbox);
624 mutt_parse_mime_message (Context, h);
625 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
626 if ((WithCrypto & APPLICATION_PGP)
627 && (h->security & APPLICATION_PGP)) {
628 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
630 mutt_endwin (_("Trying to extract PGP keys...\n"));
631 crypt_pgp_invoke_import (tempfname);
634 if ((WithCrypto & APPLICATION_SMIME)
635 && (h->security & APPLICATION_SMIME)) {
636 if (h->security & ENCRYPT)
637 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
638 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
640 mutt_copy_message (fpout, Context, h, 0, 0);
644 tmp = mutt_expand_aliases (h->env->from);
645 else if (h->env->sender)
646 tmp = mutt_expand_aliases (h->env->sender);
647 mbox = tmp ? tmp->mailbox : NULL;
648 if (mbox) { /* else ? */
649 mutt_message (_("Trying to extract S/MIME certificates...\n"));
650 crypt_smime_invoke_import (tempfname, mbox);
658 mutt_any_key_to_continue (NULL);
660 mutt_unlink (tempfname);
662 if ((WithCrypto & APPLICATION_PGP))
663 unset_option (OPTDONTHANDLEPGPKEYS);
668 int crypt_get_keys (HEADER * msg, char **keylist)
670 /* Do a quick check to make sure that we can find all of the encryption
671 * keys if the user has requested this service.
677 if ((WithCrypto & APPLICATION_PGP))
678 set_option (OPTPGPCHECKTRUST);
682 if (msg->security & ENCRYPT) {
683 if ((WithCrypto & APPLICATION_PGP)
684 && (msg->security & APPLICATION_PGP)) {
685 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
686 msg->env->bcc)) == NULL)
688 unset_option (OPTPGPCHECKTRUST);
690 if ((WithCrypto & APPLICATION_SMIME)
691 && (msg->security & APPLICATION_SMIME)) {
692 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
693 msg->env->bcc)) == NULL)
703 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
708 for (; a; a = a->next) {
709 if (a->type == TYPEMULTIPART)
710 crypt_fetch_signatures (signatures, a->parts, n);
713 p_realloc(signatures, *n + 6);
715 (*signatures)[(*n)++] = a;
722 * This routine verifies a "multipart/signed" body.
725 int mutt_signed_handler (BODY * a, STATE * s)
727 char tempfile[_POSIX_PATH_MAX];
729 int protocol_major = TYPEOTHER;
730 char *protocol_minor = NULL;
733 BODY **signatures = NULL;
742 protocol = mutt_get_parameter ("protocol", a->parameter);
745 /* extract the protocol information */
751 if ((protocol_minor = strchr (protocol, '/')))
754 m_strcpy(major, sizeof(major), protocol);
755 if ((t = strchr (major, '/')))
758 protocol_major = mutt_check_mime_type (major);
761 /* consistency check */
763 if (!(a && a->next && a->next->type == protocol_major &&
764 !m_strcasecmp(a->next->subtype, protocol_minor))) {
765 state_attach_puts (_("[-- Error: "
766 "Inconsistent multipart/signed structure! --]\n\n"),
768 return mutt_body_handler (a, s);
772 if ((WithCrypto & APPLICATION_PGP)
773 && protocol_major == TYPEAPPLICATION
774 && !m_strcasecmp(protocol_minor, "pgp-signature"));
775 else if ((WithCrypto & APPLICATION_SMIME)
776 && protocol_major == TYPEAPPLICATION
777 && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
778 && m_strcasecmp(protocol_minor, "pkcs7-signature")));
779 else if (protocol_major == TYPEMULTIPART
780 && !m_strcasecmp(protocol_minor, "mixed"));
782 state_printf (s, _("[-- Error: "
783 "Unknown multipart/signed protocol %s! --]\n\n"),
785 return mutt_body_handler (a, s);
788 if (s->flags & M_DISPLAY) {
790 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
793 mutt_mktemp (tempfile);
794 if (crypt_write_signed (a, s, tempfile) == 0) {
795 for (i = 0; i < sigcnt; i++) {
796 if ((WithCrypto & APPLICATION_PGP)
797 && signatures[i]->type == TYPEAPPLICATION
798 && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
799 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
805 if ((WithCrypto & APPLICATION_SMIME)
806 && signatures[i]->type == TYPEAPPLICATION
808 (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
809 || !m_strcasecmp(signatures[i]->subtype,
810 "pkcs7-signature"))) {
811 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
817 state_printf (s, _("[-- Warning: "
818 "We can't verify %s/%s signatures. --]\n\n"),
819 TYPE (signatures[i]), signatures[i]->subtype);
823 mutt_unlink (tempfile);
825 b->goodsig = goodsig;
826 b->badsig = !goodsig;
828 /* Now display the signed body */
829 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
832 p_delete(&signatures);
835 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
839 rc = mutt_body_handler (a, s);
841 if (s->flags & M_DISPLAY && sigcnt)
842 state_attach_puts (_("\n[-- End of signed data --]\n"), s);