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];
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 crypt_pgp_void_passphrase ();
81 crypt_smime_void_passphrase ();
82 mutt_message _("Passphrase(s) forgotten.");
86 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
88 static void disable_coredumps (void)
90 struct rlimit rl = { 0, 0 };
91 static short done = 0;
94 setrlimit (RLIMIT_CORE, &rl);
99 #endif /* HAVE_SETRLIMIT */
102 int crypt_valid_passphrase (int flags)
106 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
107 disable_coredumps ();
110 if (flags & APPLICATION_PGP)
111 ret = crypt_pgp_valid_passphrase ();
113 if (flags & APPLICATION_SMIME)
114 ret = crypt_smime_valid_passphrase ();
121 int mutt_protect (HEADER * msg, char *keylist)
123 BODY *pbody = NULL, *tmp_pbody = NULL;
124 BODY *tmp_smime_pbody = NULL;
125 BODY *tmp_pgp_pbody = NULL;
126 int flags = msg->security;
129 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
132 if ((msg->security & PGPINLINE) == PGPINLINE) {
133 /* they really want to send it inline... go for it */
135 mutt_endwin _("Invoking PGP...");
137 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
139 msg->content = pbody;
143 /* otherwise inline won't work...ask for revert */
145 query_quadoption (OPT_PGPMIMEAUTO,
147 ("Message can't be sent inline. Revert to using PGP/MIME?")))
149 mutt_error _("Mail not sent.");
154 /* go ahead with PGP/MIME */
160 tmp_smime_pbody = msg->content;
161 tmp_pgp_pbody = msg->content;
163 if (msg->security & SIGN) {
164 if (msg->security & APPLICATION_SMIME) {
165 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
167 pbody = tmp_smime_pbody = tmp_pbody;
170 if ((msg->security & APPLICATION_PGP)
171 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
172 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
176 pbody = tmp_pgp_pbody = tmp_pbody;
179 if ((msg->security & APPLICATION_SMIME)
180 && (msg->security & APPLICATION_PGP)) {
181 /* here comes the draft ;-) */
186 if (msg->security & ENCRYPT) {
187 if ((msg->security & APPLICATION_SMIME)) {
188 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
190 /* signed ? free it! */
193 /* free tmp_body if messages was signed AND encrypted ... */
194 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
195 /* detatch and dont't delete msg->content,
196 which tmp_smime_pbody->parts after signing. */
197 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
198 msg->content->next = NULL;
199 mutt_free_body (&tmp_smime_pbody);
204 if ((msg->security & APPLICATION_PGP)) {
205 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
208 /* did we perform a retainable signature? */
209 if (flags != msg->security) {
210 /* remove the outer multipart layer */
211 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
212 /* get rid of the signature */
213 mutt_free_body (&tmp_pgp_pbody->next);
219 /* destroy temporary signature envelope when doing retainable
223 if (flags != msg->security) {
224 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
225 mutt_free_body (&tmp_pgp_pbody->next);
231 msg->content = pbody;
239 int mutt_is_multipart_signed (BODY * b)
243 if (!b || !(b->type == TYPEMULTIPART) ||
244 !b->subtype || ascii_strcasecmp (b->subtype, "signed"))
247 if (!(p = mutt_get_parameter ("protocol", b->parameter)))
250 if (!(ascii_strcasecmp (p, "multipart/mixed")))
253 if (!(ascii_strcasecmp (p, "application/pgp-signature")))
256 if (!(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
258 if (!(ascii_strcasecmp (p, "application/pkcs7-signature")))
265 int mutt_is_multipart_encrypted (BODY * b)
269 if (!b || b->type != TYPEMULTIPART ||
270 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
271 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
272 ascii_strcasecmp (p, "application/pgp-encrypted"))
279 int mutt_is_application_pgp (BODY * m)
284 if (m->type == TYPEAPPLICATION) {
285 if (!ascii_strcasecmp (m->subtype, "pgp")
286 || !ascii_strcasecmp (m->subtype, "x-pgp-message")) {
287 if ((p = mutt_get_parameter ("x-action", m->parameter))
288 && (!ascii_strcasecmp (p, "sign")
289 || !ascii_strcasecmp (p, "signclear")))
292 if ((p = mutt_get_parameter ("format", m->parameter)) &&
293 !ascii_strcasecmp (p, "keys-only"))
297 t |= PGPENCRYPT; /* not necessarily correct, but... */
300 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
303 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
306 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0) {
307 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
308 || (p = mutt_get_parameter ("x-action", m->parameter))
309 || (p = mutt_get_parameter ("action", m->parameter)))
310 && !ascii_strncasecmp ("pgp-sign", p, 8))
312 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
314 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
323 int mutt_is_application_smime (BODY * m)
326 int len, complain = 0;
331 if ((m->type & TYPEAPPLICATION) && m->subtype) {
332 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
333 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
334 !ascii_strcasecmp (m->subtype, "pkcs7-mime")) {
335 if ((t = mutt_get_parameter ("smime-type", m->parameter))) {
336 if (!ascii_strcasecmp (t, "enveloped-data"))
338 else if (!ascii_strcasecmp (t, "signed-data"))
339 return (SMIMESIGN | SMIMEOPAQUE);
344 * Content-Description: S/MIME Encrypted Message
345 * instead of Content-Type parameter
347 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
351 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
354 t = mutt_get_parameter ("name", m->parameter);
363 ("S/MIME messages with no hints on content are unsupported."));
367 /* no .p7c, .p10 support yet. */
369 len = m_strlen(t) - 4;
370 if (len > 0 && *(t + len) == '.') {
372 if (!ascii_strcasecmp ((t + len), "p7m"))
376 /* Not sure if this is the correct thing to do, but
377 it's required for compatibility with Outlook */
378 return (SMIMESIGN | SMIMEOPAQUE);
380 else if (!ascii_strcasecmp ((t + len), "p7s"))
381 return (SMIMESIGN | SMIMEOPAQUE);
393 int crypt_query (BODY * m)
400 if (m->type == TYPEAPPLICATION) {
401 t |= mutt_is_application_pgp (m);
403 t |= mutt_is_application_smime (m);
409 else if (m->type == TYPETEXT) {
410 t |= mutt_is_application_pgp (m);
415 if (m->type == TYPEMULTIPART) {
416 t |= mutt_is_multipart_encrypted (m);
417 t |= mutt_is_multipart_signed (m);
423 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
427 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
428 w = 0; /* Bits set in any part */
430 for (p = m->parts; p; p = p->next) {
435 t |= u | (w & ~GOODSIGN);
437 if ((w & GOODSIGN) && !(u & GOODSIGN))
447 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
454 if (!(fp = safe_fopen (tempfile, "w"))) {
455 mutt_perror (tempfile);
459 fseeko (s->fpin, a->hdr_offset, 0);
460 bytes = a->length + a->offset - a->hdr_offset;
463 if ((c = fgetc (s->fpin)) == EOF)
471 if (c == '\n' && !hadcr)
487 void convert_to_7bit (BODY * a)
490 if (a->type == TYPEMULTIPART) {
491 if (a->encoding != ENC7BIT) {
492 a->encoding = ENC7BIT;
493 convert_to_7bit (a->parts);
495 else if (option (OPTPGPSTRICTENC))
496 convert_to_7bit (a->parts);
498 else if (a->type == TYPEMESSAGE &&
499 m_strcasecmp(a->subtype, "delivery-status")) {
500 if (a->encoding != ENC7BIT)
501 mutt_message_to_7bit (a, NULL);
503 else if (a->encoding == ENC8BIT)
504 a->encoding = ENCQUOTEDPRINTABLE;
505 else if (a->encoding == ENCBINARY)
506 a->encoding = ENCBASE64;
507 else if (a->content && a->encoding != ENCBASE64 &&
508 (a->content->from || (a->content->space &&
509 option (OPTPGPSTRICTENC))))
510 a->encoding = ENCQUOTEDPRINTABLE;
518 void crypt_extract_keys_from_messages (HEADER * h)
521 char tempfname[_POSIX_PATH_MAX], *mbox;
522 address_t *tmp = NULL;
525 mutt_mktemp (tempfname);
526 if (!(fpout = safe_fopen (tempfname, "w"))) {
527 mutt_perror (tempfname);
531 set_option (OPTDONTHANDLEPGPKEYS);
534 for (i = 0; i < Context->vcount; i++) {
535 if (Context->hdrs[Context->v2r[i]]->tagged) {
536 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
537 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
538 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
544 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP) {
545 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
546 M_CM_DECODE | M_CM_CHARCONV, 0);
549 mutt_endwin (_("Trying to extract PGP keys...\n"));
550 crypt_pgp_invoke_import (tempfname);
553 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME) {
554 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
555 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
556 M_CM_NOHEADER | M_CM_DECODE_CRYPT
557 | M_CM_DECODE_SMIME, 0);
559 mutt_copy_message (fpout, Context,
560 Context->hdrs[Context->v2r[i]], 0, 0);
563 if (Context->hdrs[Context->v2r[i]]->env->from)
564 tmp = mutt_expand_aliases (h->env->from);
565 else if (Context->hdrs[Context->v2r[i]]->env->sender)
566 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
568 mbox = tmp ? tmp->mailbox : NULL;
570 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
571 crypt_smime_invoke_import (tempfname, mbox);
581 mutt_parse_mime_message (Context, h);
582 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
583 if (h->security & APPLICATION_PGP) {
584 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
586 mutt_endwin (_("Trying to extract PGP keys...\n"));
587 crypt_pgp_invoke_import (tempfname);
590 if (h->security & APPLICATION_SMIME) {
591 if (h->security & ENCRYPT)
592 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
593 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
595 mutt_copy_message (fpout, Context, h, 0, 0);
599 tmp = mutt_expand_aliases (h->env->from);
600 else if (h->env->sender)
601 tmp = mutt_expand_aliases (h->env->sender);
602 mbox = tmp ? tmp->mailbox : NULL;
603 if (mbox) { /* else ? */
604 mutt_message (_("Trying to extract S/MIME certificates...\n"));
605 crypt_smime_invoke_import (tempfname, mbox);
613 mutt_any_key_to_continue (NULL);
615 mutt_unlink (tempfname);
617 unset_option (OPTDONTHANDLEPGPKEYS);
622 int crypt_get_keys (HEADER * msg, char **keylist)
624 /* Do a quick check to make sure that we can find all of the encryption
625 * keys if the user has requested this service.
628 set_option (OPTPGPCHECKTRUST);
632 if (msg->security & ENCRYPT) {
633 if (msg->security & APPLICATION_PGP) {
634 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
635 msg->env->bcc)) == NULL)
637 unset_option (OPTPGPCHECKTRUST);
639 if (msg->security & APPLICATION_SMIME) {
640 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
641 msg->env->bcc)) == NULL)
651 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
653 for (; a; a = a->next) {
654 if (a->type == TYPEMULTIPART)
655 crypt_fetch_signatures (signatures, a->parts, n);
658 p_realloc(signatures, *n + 6);
660 (*signatures)[(*n)++] = a;
667 * This routine verifies a "multipart/signed" body.
670 int mutt_signed_handler (BODY * a, STATE * s)
672 char tempfile[_POSIX_PATH_MAX];
674 int protocol_major = TYPEOTHER;
675 char *protocol_minor = NULL;
678 BODY **signatures = NULL;
684 protocol = mutt_get_parameter ("protocol", a->parameter);
687 /* extract the protocol information */
693 if ((protocol_minor = strchr (protocol, '/')))
696 m_strcpy(major, sizeof(major), protocol);
697 if ((t = strchr (major, '/')))
700 protocol_major = mutt_check_mime_type (major);
703 /* consistency check */
705 if (!(a && a->next && a->next->type == protocol_major &&
706 !m_strcasecmp(a->next->subtype, protocol_minor))) {
707 state_attach_puts (_("[-- Error: "
708 "Inconsistent multipart/signed structure! --]\n\n"),
710 return mutt_body_handler (a, s);
714 if (protocol_major == TYPEAPPLICATION
715 && !m_strcasecmp(protocol_minor, "pgp-signature"));
716 else if (protocol_major == TYPEAPPLICATION
717 && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
718 && m_strcasecmp(protocol_minor, "pkcs7-signature")));
719 else if (protocol_major == TYPEMULTIPART
720 && !m_strcasecmp(protocol_minor, "mixed"));
722 state_printf (s, _("[-- Error: "
723 "Unknown multipart/signed protocol %s! --]\n\n"),
725 return mutt_body_handler (a, s);
728 if (s->flags & M_DISPLAY) {
730 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
733 mutt_mktemp (tempfile);
734 if (crypt_write_signed (a, s, tempfile) == 0) {
735 for (i = 0; i < sigcnt; i++) {
736 if (signatures[i]->type == TYPEAPPLICATION
737 && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
738 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
744 if (signatures[i]->type == TYPEAPPLICATION
745 && (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
746 || !m_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
748 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
754 state_printf (s, _("[-- Warning: "
755 "We can't verify %s/%s signatures. --]\n\n"),
756 TYPE (signatures[i]), signatures[i]->subtype);
760 mutt_unlink (tempfile);
762 b->goodsig = goodsig;
763 b->badsig = !goodsig;
765 /* Now display the signed body */
766 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
769 p_delete(&signatures);
772 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
776 rc = mutt_body_handler (a, s);
778 if (s->flags & M_DISPLAY && sigcnt)
779 state_attach_puts (_("\n[-- End of signed data --]\n"), s);