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/ascii.h>
21 #include <lib-lib/mem.h>
22 #include <lib-lib/macros.h>
26 #include "mutt_curses.h"
29 #include "mutt_crypt.h"
45 #ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
49 #ifdef HAVE_SYS_RESOURCE_H
50 # include <sys/resource.h>
54 /* print the current time to avoid spoofing of the signature output */
55 void crypt_current_time (STATE * s, const char *app_name)
58 char p[STRING], tmp[STRING];
63 if (option (OPTCRYPTTIMESTAMP)) {
65 setlocale (LC_TIME, "");
66 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
67 setlocale (LC_TIME, "C");
72 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
73 NONULL (app_name), p);
74 state_attach_puts (tmp, s);
79 void crypt_forget_passphrase (void)
81 if ((WithCrypto & APPLICATION_PGP))
82 crypt_pgp_void_passphrase ();
84 if ((WithCrypto & APPLICATION_SMIME))
85 crypt_smime_void_passphrase ();
88 mutt_message _("Passphrase(s) forgotten.");
92 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
94 static void disable_coredumps (void)
96 struct rlimit rl = { 0, 0 };
97 static short done = 0;
100 setrlimit (RLIMIT_CORE, &rl);
105 #endif /* HAVE_SETRLIMIT */
108 int crypt_valid_passphrase (int flags)
112 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
113 disable_coredumps ();
116 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
117 ret = crypt_pgp_valid_passphrase ();
119 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
120 ret = crypt_smime_valid_passphrase ();
127 int mutt_protect (HEADER * msg, char *keylist)
129 BODY *pbody = NULL, *tmp_pbody = NULL;
130 BODY *tmp_smime_pbody = NULL;
131 BODY *tmp_pgp_pbody = NULL;
132 int flags = (WithCrypto & APPLICATION_PGP) ? msg->security : 0;
138 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
141 if ((WithCrypto & APPLICATION_PGP)
142 && ((msg->security & PGPINLINE) == PGPINLINE)) {
143 /* they really want to send it inline... go for it */
145 mutt_endwin _("Invoking PGP...");
147 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
149 msg->content = pbody;
153 /* otherwise inline won't work...ask for revert */
155 query_quadoption (OPT_PGPMIMEAUTO,
157 ("Message can't be sent inline. Revert to using PGP/MIME?")))
159 mutt_error _("Mail not sent.");
164 /* go ahead with PGP/MIME */
170 if ((WithCrypto & APPLICATION_SMIME))
171 tmp_smime_pbody = msg->content;
172 if ((WithCrypto & APPLICATION_PGP))
173 tmp_pgp_pbody = msg->content;
175 if (msg->security & SIGN) {
176 if ((WithCrypto & APPLICATION_SMIME)
177 && (msg->security & APPLICATION_SMIME)) {
178 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
180 pbody = tmp_smime_pbody = tmp_pbody;
183 if ((WithCrypto & APPLICATION_PGP)
184 && (msg->security & APPLICATION_PGP)
185 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
186 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
190 pbody = tmp_pgp_pbody = tmp_pbody;
193 if (WithCrypto && (msg->security & APPLICATION_SMIME)
194 && (msg->security & APPLICATION_PGP)) {
195 /* here comes the draft ;-) */
200 if (msg->security & ENCRYPT) {
201 if ((WithCrypto & APPLICATION_SMIME)
202 && (msg->security & APPLICATION_SMIME)) {
203 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
205 /* signed ? free it! */
208 /* free tmp_body if messages was signed AND encrypted ... */
209 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
210 /* detatch and dont't delete msg->content,
211 which tmp_smime_pbody->parts after signing. */
212 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
213 msg->content->next = NULL;
214 mutt_free_body (&tmp_smime_pbody);
219 if ((WithCrypto & APPLICATION_PGP)
220 && (msg->security & APPLICATION_PGP)) {
221 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
224 /* did we perform a retainable signature? */
225 if (flags != msg->security) {
226 /* remove the outer multipart layer */
227 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
228 /* get rid of the signature */
229 mutt_free_body (&tmp_pgp_pbody->next);
235 /* destroy temporary signature envelope when doing retainable
239 if (flags != msg->security) {
240 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
241 mutt_free_body (&tmp_pgp_pbody->next);
247 msg->content = pbody;
255 int mutt_is_multipart_signed (BODY * b)
259 if (!b || !(b->type == TYPEMULTIPART) ||
260 !b->subtype || ascii_strcasecmp (b->subtype, "signed"))
263 if (!(p = mutt_get_parameter ("protocol", b->parameter)))
266 if (!(ascii_strcasecmp (p, "multipart/mixed")))
269 if ((WithCrypto & APPLICATION_PGP)
270 && !(ascii_strcasecmp (p, "application/pgp-signature")))
273 if ((WithCrypto & APPLICATION_SMIME)
274 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
276 if ((WithCrypto & APPLICATION_SMIME)
277 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
284 int mutt_is_multipart_encrypted (BODY * b)
286 if ((WithCrypto & APPLICATION_PGP)) {
289 if (!b || b->type != TYPEMULTIPART ||
290 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
291 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
292 ascii_strcasecmp (p, "application/pgp-encrypted"))
302 int mutt_is_application_pgp (BODY * m)
307 if (m->type == TYPEAPPLICATION) {
308 if (!ascii_strcasecmp (m->subtype, "pgp")
309 || !ascii_strcasecmp (m->subtype, "x-pgp-message")) {
310 if ((p = mutt_get_parameter ("x-action", m->parameter))
311 && (!ascii_strcasecmp (p, "sign")
312 || !ascii_strcasecmp (p, "signclear")))
315 if ((p = mutt_get_parameter ("format", m->parameter)) &&
316 !ascii_strcasecmp (p, "keys-only"))
320 t |= PGPENCRYPT; /* not necessarily correct, but... */
323 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
326 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
329 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0) {
330 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
331 || (p = mutt_get_parameter ("x-action", m->parameter))
332 || (p = mutt_get_parameter ("action", m->parameter)))
333 && !ascii_strncasecmp ("pgp-sign", p, 8))
335 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
337 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
346 int mutt_is_application_smime (BODY * m)
349 int len, complain = 0;
354 if ((m->type & TYPEAPPLICATION) && m->subtype) {
355 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
356 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
357 !ascii_strcasecmp (m->subtype, "pkcs7-mime")) {
358 if ((t = mutt_get_parameter ("smime-type", m->parameter))) {
359 if (!ascii_strcasecmp (t, "enveloped-data"))
361 else if (!ascii_strcasecmp (t, "signed-data"))
362 return (SMIMESIGN | SMIMEOPAQUE);
367 * Content-Description: S/MIME Encrypted Message
368 * instead of Content-Type parameter
370 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
374 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
377 t = mutt_get_parameter ("name", m->parameter);
386 ("S/MIME messages with no hints on content are unsupported."));
390 /* no .p7c, .p10 support yet. */
392 len = m_strlen(t) - 4;
393 if (len > 0 && *(t + len) == '.') {
395 if (!ascii_strcasecmp ((t + len), "p7m"))
399 /* Not sure if this is the correct thing to do, but
400 it's required for compatibility with Outlook */
401 return (SMIMESIGN | SMIMEOPAQUE);
403 else if (!ascii_strcasecmp ((t + len), "p7s"))
404 return (SMIMESIGN | SMIMEOPAQUE);
416 int crypt_query (BODY * m)
426 if (m->type == TYPEAPPLICATION) {
427 if ((WithCrypto & APPLICATION_PGP))
428 t |= mutt_is_application_pgp (m);
430 if ((WithCrypto & APPLICATION_SMIME)) {
431 t |= mutt_is_application_smime (m);
438 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT) {
439 t |= mutt_is_application_pgp (m);
444 if (m->type == TYPEMULTIPART) {
445 t |= mutt_is_multipart_encrypted (m);
446 t |= mutt_is_multipart_signed (m);
452 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
456 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
457 w = 0; /* Bits set in any part */
459 for (p = m->parts; p; p = p->next) {
464 t |= u | (w & ~GOODSIGN);
466 if ((w & GOODSIGN) && !(u & GOODSIGN))
476 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
486 if (!(fp = safe_fopen (tempfile, "w"))) {
487 mutt_perror (tempfile);
491 fseeko (s->fpin, a->hdr_offset, 0);
492 bytes = a->length + a->offset - a->hdr_offset;
495 if ((c = fgetc (s->fpin)) == EOF)
503 if (c == '\n' && !hadcr)
519 void convert_to_7bit (BODY * a)
525 if (a->type == TYPEMULTIPART) {
526 if (a->encoding != ENC7BIT) {
527 a->encoding = ENC7BIT;
528 convert_to_7bit (a->parts);
530 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
531 convert_to_7bit (a->parts);
533 else if (a->type == TYPEMESSAGE &&
534 str_casecmp (a->subtype, "delivery-status")) {
535 if (a->encoding != ENC7BIT)
536 mutt_message_to_7bit (a, NULL);
538 else if (a->encoding == ENC8BIT)
539 a->encoding = ENCQUOTEDPRINTABLE;
540 else if (a->encoding == ENCBINARY)
541 a->encoding = ENCBASE64;
542 else if (a->content && a->encoding != ENCBASE64 &&
543 (a->content->from || (a->content->space &&
544 option (OPTPGPSTRICTENC))))
545 a->encoding = ENCQUOTEDPRINTABLE;
553 void crypt_extract_keys_from_messages (HEADER * h)
556 char tempfname[_POSIX_PATH_MAX], *mbox;
563 mutt_mktemp (tempfname);
564 if (!(fpout = safe_fopen (tempfname, "w"))) {
565 mutt_perror (tempfname);
569 if ((WithCrypto & APPLICATION_PGP))
570 set_option (OPTDONTHANDLEPGPKEYS);
573 for (i = 0; i < Context->vcount; i++) {
574 if (Context->hdrs[Context->v2r[i]]->tagged) {
575 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
576 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
577 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
583 if ((WithCrypto & APPLICATION_PGP)
584 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP)) {
585 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
586 M_CM_DECODE | M_CM_CHARCONV, 0);
589 mutt_endwin (_("Trying to extract PGP keys...\n"));
590 crypt_pgp_invoke_import (tempfname);
593 if ((WithCrypto & APPLICATION_SMIME)
594 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME)) {
595 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
596 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
597 M_CM_NOHEADER | M_CM_DECODE_CRYPT
598 | M_CM_DECODE_SMIME, 0);
600 mutt_copy_message (fpout, Context,
601 Context->hdrs[Context->v2r[i]], 0, 0);
604 if (Context->hdrs[Context->v2r[i]]->env->from)
605 tmp = mutt_expand_aliases (h->env->from);
606 else if (Context->hdrs[Context->v2r[i]]->env->sender)
607 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
609 mbox = tmp ? tmp->mailbox : NULL;
611 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
612 crypt_smime_invoke_import (tempfname, mbox);
622 mutt_parse_mime_message (Context, h);
623 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
624 if ((WithCrypto & APPLICATION_PGP)
625 && (h->security & APPLICATION_PGP)) {
626 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
628 mutt_endwin (_("Trying to extract PGP keys...\n"));
629 crypt_pgp_invoke_import (tempfname);
632 if ((WithCrypto & APPLICATION_SMIME)
633 && (h->security & APPLICATION_SMIME)) {
634 if (h->security & ENCRYPT)
635 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
636 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
638 mutt_copy_message (fpout, Context, h, 0, 0);
642 tmp = mutt_expand_aliases (h->env->from);
643 else if (h->env->sender)
644 tmp = mutt_expand_aliases (h->env->sender);
645 mbox = tmp ? tmp->mailbox : NULL;
646 if (mbox) { /* else ? */
647 mutt_message (_("Trying to extract S/MIME certificates...\n"));
648 crypt_smime_invoke_import (tempfname, mbox);
656 mutt_any_key_to_continue (NULL);
658 mutt_unlink (tempfname);
660 if ((WithCrypto & APPLICATION_PGP))
661 unset_option (OPTDONTHANDLEPGPKEYS);
666 int crypt_get_keys (HEADER * msg, char **keylist)
668 /* Do a quick check to make sure that we can find all of the encryption
669 * keys if the user has requested this service.
675 if ((WithCrypto & APPLICATION_PGP))
676 set_option (OPTPGPCHECKTRUST);
680 if (msg->security & ENCRYPT) {
681 if ((WithCrypto & APPLICATION_PGP)
682 && (msg->security & APPLICATION_PGP)) {
683 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
684 msg->env->bcc)) == NULL)
686 unset_option (OPTPGPCHECKTRUST);
688 if ((WithCrypto & APPLICATION_SMIME)
689 && (msg->security & APPLICATION_SMIME)) {
690 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
691 msg->env->bcc)) == NULL)
701 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
706 for (; a; a = a->next) {
707 if (a->type == TYPEMULTIPART)
708 crypt_fetch_signatures (signatures, a->parts, n);
711 p_realloc(signatures, *n + 6);
713 (*signatures)[(*n)++] = a;
720 * This routine verifies a "multipart/signed" body.
723 int mutt_signed_handler (BODY * a, STATE * s)
725 char tempfile[_POSIX_PATH_MAX];
727 int protocol_major = TYPEOTHER;
728 char *protocol_minor = NULL;
731 BODY **signatures = NULL;
740 protocol = mutt_get_parameter ("protocol", a->parameter);
743 /* extract the protocol information */
749 if ((protocol_minor = strchr (protocol, '/')))
752 strfcpy (major, protocol, sizeof (major));
753 if ((t = strchr (major, '/')))
756 protocol_major = mutt_check_mime_type (major);
759 /* consistency check */
761 if (!(a && a->next && a->next->type == protocol_major &&
762 !str_casecmp (a->next->subtype, protocol_minor))) {
763 state_attach_puts (_("[-- Error: "
764 "Inconsistent multipart/signed structure! --]\n\n"),
766 return mutt_body_handler (a, s);
770 if ((WithCrypto & APPLICATION_PGP)
771 && protocol_major == TYPEAPPLICATION
772 && !str_casecmp (protocol_minor, "pgp-signature"));
773 else if ((WithCrypto & APPLICATION_SMIME)
774 && protocol_major == TYPEAPPLICATION
775 && !(str_casecmp (protocol_minor, "x-pkcs7-signature")
776 && str_casecmp (protocol_minor, "pkcs7-signature")));
777 else if (protocol_major == TYPEMULTIPART
778 && !str_casecmp (protocol_minor, "mixed"));
780 state_printf (s, _("[-- Error: "
781 "Unknown multipart/signed protocol %s! --]\n\n"),
783 return mutt_body_handler (a, s);
786 if (s->flags & M_DISPLAY) {
788 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
791 mutt_mktemp (tempfile);
792 if (crypt_write_signed (a, s, tempfile) == 0) {
793 for (i = 0; i < sigcnt; i++) {
794 if ((WithCrypto & APPLICATION_PGP)
795 && signatures[i]->type == TYPEAPPLICATION
796 && !str_casecmp (signatures[i]->subtype, "pgp-signature")) {
797 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
803 if ((WithCrypto & APPLICATION_SMIME)
804 && signatures[i]->type == TYPEAPPLICATION
806 (!str_casecmp (signatures[i]->subtype, "x-pkcs7-signature")
807 || !str_casecmp (signatures[i]->subtype,
808 "pkcs7-signature"))) {
809 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
815 state_printf (s, _("[-- Warning: "
816 "We can't verify %s/%s signatures. --]\n\n"),
817 TYPE (signatures[i]), signatures[i]->subtype);
821 mutt_unlink (tempfile);
823 b->goodsig = goodsig;
824 b->badsig = !goodsig;
826 /* Now display the signed body */
827 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
830 p_delete(&signatures);
833 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
837 rc = mutt_body_handler (a, s);
839 if (s->flags & M_DISPLAY && sigcnt)
840 state_attach_puts (_("\n[-- End of signed data --]\n"), s);