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.
20 #include "mutt_curses.h"
23 #include "mutt_crypt.h"
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
42 #ifdef HAVE_SYS_RESOURCE_H
43 # include <sys/resource.h>
47 /* print the current time to avoid spoofing of the signature output */
48 void crypt_current_time (STATE * s, char *app_name)
51 char p[STRING], tmp[STRING];
56 if (option (OPTCRYPTTIMESTAMP)) {
58 setlocale (LC_TIME, "");
59 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
60 setlocale (LC_TIME, "C");
65 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
66 NONULL (app_name), p);
67 state_attach_puts (tmp, s);
72 void crypt_forget_passphrase (void)
74 if ((WithCrypto & APPLICATION_PGP))
75 crypt_pgp_void_passphrase ();
77 if ((WithCrypto & APPLICATION_SMIME))
78 crypt_smime_void_passphrase ();
81 mutt_message _("Passphrase(s) forgotten.");
85 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
87 static void disable_coredumps (void)
89 struct rlimit rl = { 0, 0 };
90 static short done = 0;
93 setrlimit (RLIMIT_CORE, &rl);
98 #endif /* HAVE_SETRLIMIT */
101 int crypt_valid_passphrase (int flags)
105 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
106 disable_coredumps ();
109 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
110 ret = crypt_pgp_valid_passphrase ();
112 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
113 ret = crypt_smime_valid_passphrase ();
120 int mutt_protect (HEADER * msg, char *keylist)
122 BODY *pbody = NULL, *tmp_pbody = NULL;
123 BODY *tmp_smime_pbody = NULL;
124 BODY *tmp_pgp_pbody = NULL;
125 int flags = (WithCrypto & APPLICATION_PGP) ? msg->security : 0;
131 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
134 if ((WithCrypto & APPLICATION_PGP)
135 && ((msg->security & PGPINLINE) == PGPINLINE)) {
136 /* they really want to send it inline... go for it */
138 mutt_endwin _("Invoking PGP...");
140 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
142 msg->content = pbody;
146 /* otherwise inline won't work...ask for revert */
148 query_quadoption (OPT_PGPMIMEAUTO,
150 ("Message can't be sent inline. Revert to using PGP/MIME?")))
152 mutt_error _("Mail not sent.");
157 /* go ahead with PGP/MIME */
163 if ((WithCrypto & APPLICATION_SMIME))
164 tmp_smime_pbody = msg->content;
165 if ((WithCrypto & APPLICATION_PGP))
166 tmp_pgp_pbody = msg->content;
168 if (msg->security & SIGN) {
169 if ((WithCrypto & APPLICATION_SMIME)
170 && (msg->security & APPLICATION_SMIME)) {
171 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
173 pbody = tmp_smime_pbody = tmp_pbody;
176 if ((WithCrypto & APPLICATION_PGP)
177 && (msg->security & APPLICATION_PGP)
178 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
179 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
183 pbody = tmp_pgp_pbody = tmp_pbody;
186 if (WithCrypto && (msg->security & APPLICATION_SMIME)
187 && (msg->security & APPLICATION_PGP)) {
188 /* here comes the draft ;-) */
193 if (msg->security & ENCRYPT) {
194 if ((WithCrypto & APPLICATION_SMIME)
195 && (msg->security & APPLICATION_SMIME)) {
196 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
198 /* signed ? free it! */
201 /* free tmp_body if messages was signed AND encrypted ... */
202 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
203 /* detatch and dont't delete msg->content,
204 which tmp_smime_pbody->parts after signing. */
205 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
206 msg->content->next = NULL;
207 mutt_free_body (&tmp_smime_pbody);
212 if ((WithCrypto & APPLICATION_PGP)
213 && (msg->security & APPLICATION_PGP)) {
214 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
217 /* did we perform a retainable signature? */
218 if (flags != msg->security) {
219 /* remove the outer multipart layer */
220 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
221 /* get rid of the signature */
222 mutt_free_body (&tmp_pgp_pbody->next);
228 /* destroy temporary signature envelope when doing retainable
232 if (flags != msg->security) {
233 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
234 mutt_free_body (&tmp_pgp_pbody->next);
240 msg->content = pbody;
248 int mutt_is_multipart_signed (BODY * b)
252 if (!b || !(b->type == TYPEMULTIPART) ||
253 !b->subtype || ascii_strcasecmp (b->subtype, "signed"))
256 if (!(p = mutt_get_parameter ("protocol", b->parameter)))
259 if (!(ascii_strcasecmp (p, "multipart/mixed")))
262 if ((WithCrypto & APPLICATION_PGP)
263 && !(ascii_strcasecmp (p, "application/pgp-signature")))
266 if ((WithCrypto & APPLICATION_SMIME)
267 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
269 if ((WithCrypto & APPLICATION_SMIME)
270 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
277 int mutt_is_multipart_encrypted (BODY * b)
279 if ((WithCrypto & APPLICATION_PGP)) {
282 if (!b || b->type != TYPEMULTIPART ||
283 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
284 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
285 ascii_strcasecmp (p, "application/pgp-encrypted"))
295 int mutt_is_application_pgp (BODY * m)
300 if (m->type == TYPEAPPLICATION) {
301 if (!ascii_strcasecmp (m->subtype, "pgp")
302 || !ascii_strcasecmp (m->subtype, "x-pgp-message")) {
303 if ((p = mutt_get_parameter ("x-action", m->parameter))
304 && (!ascii_strcasecmp (p, "sign")
305 || !ascii_strcasecmp (p, "signclear")))
308 if ((p = mutt_get_parameter ("format", m->parameter)) &&
309 !ascii_strcasecmp (p, "keys-only"))
313 t |= PGPENCRYPT; /* not necessarily correct, but... */
316 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
319 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
322 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0) {
323 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
324 || (p = mutt_get_parameter ("x-action", m->parameter))
325 || (p = mutt_get_parameter ("action", m->parameter)))
326 && !ascii_strncasecmp ("pgp-sign", p, 8))
328 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
330 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
339 int mutt_is_application_smime (BODY * m)
342 int len, complain = 0;
347 if ((m->type & TYPEAPPLICATION) && m->subtype) {
348 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
349 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
350 !ascii_strcasecmp (m->subtype, "pkcs7-mime")) {
351 if ((t = mutt_get_parameter ("smime-type", m->parameter))) {
352 if (!ascii_strcasecmp (t, "enveloped-data"))
354 else if (!ascii_strcasecmp (t, "signed-data"))
355 return (SMIMESIGN | SMIMEOPAQUE);
360 * Content-Description: S/MIME Encrypted Message
361 * instead of Content-Type parameter
363 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
367 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
370 t = mutt_get_parameter ("name", m->parameter);
379 ("S/MIME messages with no hints on content are unsupported."));
383 /* no .p7c, .p10 support yet. */
385 len = mutt_strlen (t) - 4;
386 if (len > 0 && *(t + len) == '.') {
388 if (!ascii_strcasecmp ((t + len), "p7m"))
392 /* Not sure if this is the correct thing to do, but
393 it's required for compatibility with Outlook */
394 return (SMIMESIGN | SMIMEOPAQUE);
396 else if (!ascii_strcasecmp ((t + len), "p7s"))
397 return (SMIMESIGN | SMIMEOPAQUE);
409 int crypt_query (BODY * m)
419 if (m->type == TYPEAPPLICATION) {
420 if ((WithCrypto & APPLICATION_PGP))
421 t |= mutt_is_application_pgp (m);
423 if ((WithCrypto & APPLICATION_SMIME)) {
424 t |= mutt_is_application_smime (m);
431 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT) {
432 t |= mutt_is_application_pgp (m);
437 if (m->type == TYPEMULTIPART) {
438 t |= mutt_is_multipart_encrypted (m);
439 t |= mutt_is_multipart_signed (m);
445 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
449 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
450 w = 0; /* Bits set in any part */
452 for (p = m->parts; p; p = p->next) {
457 t |= u | (w & ~GOODSIGN);
459 if ((w & GOODSIGN) && !(u & GOODSIGN))
469 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
479 if (!(fp = safe_fopen (tempfile, "w"))) {
480 mutt_perror (tempfile);
484 fseek (s->fpin, a->hdr_offset, 0);
485 bytes = a->length + a->offset - a->hdr_offset;
488 if ((c = fgetc (s->fpin)) == EOF)
496 if (c == '\n' && !hadcr)
512 void convert_to_7bit (BODY * a)
518 if (a->type == TYPEMULTIPART) {
519 if (a->encoding != ENC7BIT) {
520 a->encoding = ENC7BIT;
521 convert_to_7bit (a->parts);
523 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
524 convert_to_7bit (a->parts);
526 else if (a->type == TYPEMESSAGE &&
527 mutt_strcasecmp (a->subtype, "delivery-status")) {
528 if (a->encoding != ENC7BIT)
529 mutt_message_to_7bit (a, NULL);
531 else if (a->encoding == ENC8BIT)
532 a->encoding = ENCQUOTEDPRINTABLE;
533 else if (a->encoding == ENCBINARY)
534 a->encoding = ENCBASE64;
535 else if (a->content && a->encoding != ENCBASE64 &&
536 (a->content->from || (a->content->space &&
537 option (OPTPGPSTRICTENC))))
538 a->encoding = ENCQUOTEDPRINTABLE;
546 void crypt_extract_keys_from_messages (HEADER * h)
549 char tempfname[_POSIX_PATH_MAX], *mbox;
556 mutt_mktemp (tempfname);
557 if (!(fpout = safe_fopen (tempfname, "w"))) {
558 mutt_perror (tempfname);
562 if ((WithCrypto & APPLICATION_PGP))
563 set_option (OPTDONTHANDLEPGPKEYS);
566 for (i = 0; i < Context->vcount; i++) {
567 if (Context->hdrs[Context->v2r[i]]->tagged) {
568 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
569 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
570 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
576 if ((WithCrypto & APPLICATION_PGP)
577 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP)) {
578 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
579 M_CM_DECODE | M_CM_CHARCONV, 0);
582 mutt_endwin (_("Trying to extract PGP keys...\n"));
583 crypt_pgp_invoke_import (tempfname);
586 if ((WithCrypto & APPLICATION_SMIME)
587 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME)) {
588 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
589 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
590 M_CM_NOHEADER | M_CM_DECODE_CRYPT
591 | M_CM_DECODE_SMIME, 0);
593 mutt_copy_message (fpout, Context,
594 Context->hdrs[Context->v2r[i]], 0, 0);
597 if (Context->hdrs[Context->v2r[i]]->env->from)
598 tmp = mutt_expand_aliases (h->env->from);
599 else if (Context->hdrs[Context->v2r[i]]->env->sender)
600 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
602 mbox = tmp ? tmp->mailbox : NULL;
604 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
605 crypt_smime_invoke_import (tempfname, mbox);
615 mutt_parse_mime_message (Context, h);
616 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
617 if ((WithCrypto & APPLICATION_PGP)
618 && (h->security & APPLICATION_PGP)) {
619 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
621 mutt_endwin (_("Trying to extract PGP keys...\n"));
622 crypt_pgp_invoke_import (tempfname);
625 if ((WithCrypto & APPLICATION_SMIME)
626 && (h->security & APPLICATION_SMIME)) {
627 if (h->security & ENCRYPT)
628 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
629 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
631 mutt_copy_message (fpout, Context, h, 0, 0);
635 tmp = mutt_expand_aliases (h->env->from);
636 else if (h->env->sender)
637 tmp = mutt_expand_aliases (h->env->sender);
638 mbox = tmp ? tmp->mailbox : NULL;
639 if (mbox) { /* else ? */
640 mutt_message (_("Trying to extract S/MIME certificates...\n"));
641 crypt_smime_invoke_import (tempfname, mbox);
649 mutt_any_key_to_continue (NULL);
651 mutt_unlink (tempfname);
653 if ((WithCrypto & APPLICATION_PGP))
654 unset_option (OPTDONTHANDLEPGPKEYS);
659 int crypt_get_keys (HEADER * msg, char **keylist)
661 /* Do a quick check to make sure that we can find all of the encryption
662 * keys if the user has requested this service.
668 if ((WithCrypto & APPLICATION_PGP))
669 set_option (OPTPGPCHECKTRUST);
673 if (msg->security & ENCRYPT) {
674 if ((WithCrypto & APPLICATION_PGP)
675 && (msg->security & APPLICATION_PGP)) {
676 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
677 msg->env->bcc)) == NULL)
679 unset_option (OPTPGPCHECKTRUST);
681 if ((WithCrypto & APPLICATION_SMIME)
682 && (msg->security & APPLICATION_SMIME)) {
683 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
684 msg->env->bcc)) == NULL)
694 static void crypt_fetch_signatures (BODY *** signatures, BODY * a, int *n)
699 for (; a; a = a->next) {
700 if (a->type == TYPEMULTIPART)
701 crypt_fetch_signatures (signatures, a->parts, n);
704 safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
706 (*signatures)[(*n)++] = a;
713 * This routine verifies a "multipart/signed" body.
716 void mutt_signed_handler (BODY * a, STATE * s)
718 char tempfile[_POSIX_PATH_MAX];
720 int protocol_major = TYPEOTHER;
721 char *protocol_minor = NULL;
724 BODY **signatures = NULL;
732 protocol = mutt_get_parameter ("protocol", a->parameter);
735 /* extract the protocol information */
741 if ((protocol_minor = strchr (protocol, '/')))
744 strfcpy (major, protocol, sizeof (major));
745 if ((t = strchr (major, '/')))
748 protocol_major = mutt_check_mime_type (major);
751 /* consistency check */
753 if (!(a && a->next && a->next->type == protocol_major &&
754 !mutt_strcasecmp (a->next->subtype, protocol_minor))) {
755 state_attach_puts (_("[-- Error: "
756 "Inconsistent multipart/signed structure! --]\n\n"),
758 mutt_body_handler (a, s);
763 if ((WithCrypto & APPLICATION_PGP)
764 && protocol_major == TYPEAPPLICATION
765 && !mutt_strcasecmp (protocol_minor, "pgp-signature"));
766 else if ((WithCrypto & APPLICATION_SMIME)
767 && protocol_major == TYPEAPPLICATION
768 && !(mutt_strcasecmp (protocol_minor, "x-pkcs7-signature")
769 && mutt_strcasecmp (protocol_minor, "pkcs7-signature")));
770 else if (protocol_major == TYPEMULTIPART
771 && !mutt_strcasecmp (protocol_minor, "mixed"));
773 state_printf (s, _("[-- Error: "
774 "Unknown multipart/signed protocol %s! --]\n\n"),
776 mutt_body_handler (a, s);
780 if (s->flags & M_DISPLAY) {
782 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
785 mutt_mktemp (tempfile);
786 if (crypt_write_signed (a, s, tempfile) == 0) {
787 for (i = 0; i < sigcnt; i++) {
788 if ((WithCrypto & APPLICATION_PGP)
789 && signatures[i]->type == TYPEAPPLICATION
790 && !mutt_strcasecmp (signatures[i]->subtype, "pgp-signature")) {
791 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
797 if ((WithCrypto & APPLICATION_SMIME)
798 && signatures[i]->type == TYPEAPPLICATION
800 (!mutt_strcasecmp (signatures[i]->subtype, "x-pkcs7-signature")
801 || !mutt_strcasecmp (signatures[i]->subtype,
802 "pkcs7-signature"))) {
803 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
809 state_printf (s, _("[-- Warning: "
810 "We can't verify %s/%s signatures. --]\n\n"),
811 TYPE (signatures[i]), signatures[i]->subtype);
815 mutt_unlink (tempfile);
817 b->goodsig = goodsig;
818 b->badsig = !goodsig;
820 /* Now display the signed body */
821 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
827 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
831 mutt_body_handler (a, s);
833 if (s->flags & M_DISPLAY && sigcnt)
834 state_attach_puts (_("\n[-- End of signed data --]\n"), s);