2 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2000,2002-4,2006 Thomas Roessler <roessler@does-not-exist.org>
4 * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
5 * Oliver Ehli <elmy@acm.org>
6 * Copyright (C) 2003 Werner Koch <wk@gnupg.org>
7 * Copyright (C) 2004 g10code GmbH
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include "mutt_curses.h"
32 #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, char *app_name)
59 char p[STRING], tmp[STRING];
64 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"), 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;
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) && ((msg->security & PGPINLINE) == PGPINLINE))
145 /* they really want to send it inline... go for it */
146 if (!isendwin ()) mutt_endwin _("Invoking PGP...");
147 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
150 msg->content = pbody;
154 /* otherwise inline won't work...ask for revert */
155 if ((i = query_quadoption (OPT_PGPMIMEAUTO, _("Message can't be sent inline. Revert to using PGP/MIME?"))) != M_YES)
157 mutt_error _("Mail not sent.");
161 /* go ahead with PGP/MIME */
164 if (!isendwin ()) mutt_endwin (NULL);
166 if ((WithCrypto & APPLICATION_SMIME))
167 tmp_smime_pbody = msg->content;
168 if ((WithCrypto & APPLICATION_PGP))
169 tmp_pgp_pbody = msg->content;
171 if (option (OPTCRYPTUSEPKA) && (msg->security & SIGN))
173 /* Set sender (necessary for e.g. PKA). */
175 if ((WithCrypto & APPLICATION_SMIME)
176 && (msg->security & APPLICATION_SMIME))
177 crypt_smime_set_sender (msg->env->from->mailbox);
178 else if ((WithCrypto & APPLICATION_PGP)
179 && (msg->security & APPLICATION_PGP))
180 crypt_pgp_set_sender (msg->env->from->mailbox);
183 if (msg->security & SIGN)
185 if ((WithCrypto & APPLICATION_SMIME)
186 && (msg->security & APPLICATION_SMIME))
188 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
190 pbody = tmp_smime_pbody = tmp_pbody;
193 if ((WithCrypto & APPLICATION_PGP)
194 && (msg->security & APPLICATION_PGP)
195 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG)))
197 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
201 pbody = tmp_pgp_pbody = tmp_pbody;
205 && (msg->security & APPLICATION_SMIME)
206 && (msg->security & APPLICATION_PGP))
208 /* here comes the draft ;-) */
213 if (msg->security & ENCRYPT)
215 if ((WithCrypto & APPLICATION_SMIME)
216 && (msg->security & APPLICATION_SMIME))
218 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
221 /* signed ? free it! */
224 /* free tmp_body if messages was signed AND encrypted ... */
225 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody)
227 /* detatch and dont't delete msg->content,
228 which tmp_smime_pbody->parts after signing. */
229 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
230 msg->content->next = NULL;
231 mutt_free_body (&tmp_smime_pbody);
236 if ((WithCrypto & APPLICATION_PGP)
237 && (msg->security & APPLICATION_PGP))
239 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
243 /* did we perform a retainable signature? */
244 if (flags != msg->security)
246 /* remove the outer multipart layer */
247 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
248 /* get rid of the signature */
249 mutt_free_body (&tmp_pgp_pbody->next);
255 /* destroy temporary signature envelope when doing retainable
259 if (flags != msg->security)
261 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
262 mutt_free_body (&tmp_pgp_pbody->next);
268 msg->content = pbody;
276 int mutt_is_multipart_signed (BODY *b)
280 if (!b || !(b->type == TYPEMULTIPART) ||
281 !b->subtype || ascii_strcasecmp(b->subtype, "signed"))
284 if (!(p = mutt_get_parameter("protocol", b->parameter)))
287 if (!(ascii_strcasecmp (p, "multipart/mixed")))
290 if ((WithCrypto & APPLICATION_PGP)
291 && !(ascii_strcasecmp (p, "application/pgp-signature")))
294 if ((WithCrypto & APPLICATION_SMIME)
295 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
297 if ((WithCrypto & APPLICATION_SMIME)
298 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
305 int mutt_is_multipart_encrypted (BODY *b)
307 if ((WithCrypto & APPLICATION_PGP))
311 if (!b || b->type != TYPEMULTIPART ||
312 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
313 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
314 ascii_strcasecmp (p, "application/pgp-encrypted"))
324 int mutt_is_application_pgp (BODY *m)
329 if (m->type == TYPEAPPLICATION)
331 if (!ascii_strcasecmp (m->subtype, "pgp") || !ascii_strcasecmp (m->subtype, "x-pgp-message"))
333 if ((p = mutt_get_parameter ("x-action", m->parameter))
334 && (!ascii_strcasecmp (p, "sign") || !ascii_strcasecmp (p, "signclear")))
337 if ((p = mutt_get_parameter ("format", m->parameter)) &&
338 !ascii_strcasecmp (p, "keys-only"))
341 if(!t) t |= PGPENCRYPT; /* not necessarily correct, but... */
344 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
347 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
350 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0)
352 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
353 || (p = mutt_get_parameter ("x-action", m->parameter))
354 || (p = mutt_get_parameter ("action", m->parameter)))
355 && !ascii_strncasecmp ("pgp-sign", p, 8))
357 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
359 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
368 int mutt_is_application_smime (BODY *m)
376 if ((m->type & TYPEAPPLICATION) && m->subtype)
378 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
379 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
380 !ascii_strcasecmp (m->subtype, "pkcs7-mime"))
382 if ((t = mutt_get_parameter ("smime-type", m->parameter)))
384 if (!ascii_strcasecmp (t, "enveloped-data"))
386 else if (!ascii_strcasecmp (t, "signed-data"))
387 return (SMIMESIGN|SMIMEOPAQUE);
391 * Content-Description: S/MIME Encrypted Message
392 * instead of Content-Type parameter
394 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
398 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
401 t = mutt_get_parameter ("name", m->parameter);
403 if (!t) t = m->d_filename;
404 if (!t) t = m->filename;
408 mutt_message (_("S/MIME messages with no hints on content are unsupported."));
412 /* no .p7c, .p10 support yet. */
414 len = mutt_strlen (t) - 4;
415 if (len > 0 && *(t+len) == '.')
418 if (!ascii_strcasecmp ((t+len), "p7m"))
422 /* Not sure if this is the correct thing to do, but
423 it's required for compatibility with Outlook */
424 return (SMIMESIGN|SMIMEOPAQUE);
426 else if (!ascii_strcasecmp ((t+len), "p7s"))
427 return (SMIMESIGN|SMIMEOPAQUE);
439 int crypt_query (BODY *m)
449 if (m->type == TYPEAPPLICATION)
451 if ((WithCrypto & APPLICATION_PGP))
452 t |= mutt_is_application_pgp(m);
454 if ((WithCrypto & APPLICATION_SMIME))
456 t |= mutt_is_application_smime(m);
457 if (t && m->goodsig) t |= GOODSIGN;
458 if (t && m->badsig) t |= BADSIGN;
461 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT)
463 t |= mutt_is_application_pgp (m);
468 if (m->type == TYPEMULTIPART)
470 t |= mutt_is_multipart_encrypted(m);
471 t |= mutt_is_multipart_signed (m);
477 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE)
482 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
483 w = 0; /* Bits set in any part */
485 for (p = m->parts; p; p = p->next)
490 t |= u | (w & ~GOODSIGN);
492 if ((w & GOODSIGN) && !(u & GOODSIGN))
502 int crypt_write_signed(BODY *a, STATE *s, const char *tempfile)
512 if (!(fp = safe_fopen (tempfile, "w")))
514 mutt_perror (tempfile);
518 fseeko (s->fpin, a->hdr_offset, 0);
519 bytes = a->length + a->offset - a->hdr_offset;
523 if ((c = fgetc (s->fpin)) == EOF)
532 if (c == '\n' && !hadcr)
548 void convert_to_7bit (BODY *a)
555 if (a->type == TYPEMULTIPART)
557 if (a->encoding != ENC7BIT)
559 a->encoding = ENC7BIT;
560 convert_to_7bit(a->parts);
562 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
563 convert_to_7bit (a->parts);
565 else if (a->type == TYPEMESSAGE &&
566 ascii_strcasecmp(a->subtype, "delivery-status"))
568 if(a->encoding != ENC7BIT)
569 mutt_message_to_7bit (a, NULL);
571 else if (a->encoding == ENC8BIT)
572 a->encoding = ENCQUOTEDPRINTABLE;
573 else if (a->encoding == ENCBINARY)
574 a->encoding = ENCBASE64;
575 else if (a->content && a->encoding != ENCBASE64 &&
576 (a->content->from || (a->content->space &&
577 option (OPTPGPSTRICTENC))))
578 a->encoding = ENCQUOTEDPRINTABLE;
586 void crypt_extract_keys_from_messages (HEADER * h)
589 char tempfname[_POSIX_PATH_MAX], *mbox;
596 mutt_mktemp (tempfname, sizeof (tempfname));
597 if (!(fpout = safe_fopen (tempfname, "w")))
599 mutt_perror (tempfname);
603 if ((WithCrypto & APPLICATION_PGP))
604 set_option (OPTDONTHANDLEPGPKEYS);
608 for (i = 0; i < Context->vcount; i++)
610 if (Context->hdrs[Context->v2r[i]]->tagged)
612 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
613 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
614 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->security))
616 safe_fclose (&fpout);
620 if ((WithCrypto & APPLICATION_PGP)
621 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP))
623 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
624 M_CM_DECODE|M_CM_CHARCONV, 0);
627 mutt_endwin (_("Trying to extract PGP keys...\n"));
628 crypt_pgp_invoke_import (tempfname);
631 if ((WithCrypto & APPLICATION_SMIME)
632 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME))
634 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
635 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
636 M_CM_NOHEADER|M_CM_DECODE_CRYPT
637 |M_CM_DECODE_SMIME, 0);
639 mutt_copy_message (fpout, Context,
640 Context->hdrs[Context->v2r[i]], 0, 0);
643 if (Context->hdrs[Context->v2r[i]]->env->from)
644 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]->env->from);
645 else if (Context->hdrs[Context->v2r[i]]->env->sender)
646 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
648 mbox = tmp ? tmp->mailbox : NULL;
651 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
652 crypt_smime_invoke_import (tempfname, mbox);
663 mutt_parse_mime_message (Context, h);
664 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security)))
666 if ((WithCrypto & APPLICATION_PGP)
667 && (h->security & APPLICATION_PGP))
669 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
671 mutt_endwin (_("Trying to extract PGP keys...\n"));
672 crypt_pgp_invoke_import (tempfname);
675 if ((WithCrypto & APPLICATION_SMIME)
676 && (h->security & APPLICATION_SMIME))
678 if (h->security & ENCRYPT)
679 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
681 |M_CM_DECODE_SMIME, 0);
683 mutt_copy_message (fpout, Context, h, 0, 0);
686 if (h->env->from) tmp = mutt_expand_aliases (h->env->from);
687 else if (h->env->sender) tmp = mutt_expand_aliases (h->env->sender);
688 mbox = tmp ? tmp->mailbox : NULL;
689 if (mbox) /* else ? */
691 mutt_message (_("Trying to extract S/MIME certificates...\n"));
692 crypt_smime_invoke_import (tempfname, mbox);
698 safe_fclose (&fpout);
700 mutt_any_key_to_continue (NULL);
702 mutt_unlink (tempfname);
704 if ((WithCrypto & APPLICATION_PGP))
705 unset_option (OPTDONTHANDLEPGPKEYS);
710 int crypt_get_keys (HEADER *msg, char **keylist)
712 /* Do a quick check to make sure that we can find all of the encryption
713 * keys if the user has requested this service.
719 if ((WithCrypto & APPLICATION_PGP))
720 set_option (OPTPGPCHECKTRUST);
724 if (msg->security & ENCRYPT)
726 if ((WithCrypto & APPLICATION_PGP)
727 && (msg->security & APPLICATION_PGP))
729 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
730 msg->env->bcc)) == NULL)
732 unset_option (OPTPGPCHECKTRUST);
734 if ((WithCrypto & APPLICATION_SMIME)
735 && (msg->security & APPLICATION_SMIME))
737 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
738 msg->env->bcc)) == NULL)
748 static void crypt_fetch_signatures (BODY ***signatures, BODY *a, int *n)
753 for (; a; a = a->next)
755 if (a->type == TYPEMULTIPART)
756 crypt_fetch_signatures (signatures, a->parts, n);
760 safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
762 (*signatures)[(*n)++] = a;
769 * This routine verifies a "multipart/signed" body.
772 int mutt_signed_handler (BODY *a, STATE *s)
774 char tempfile[_POSIX_PATH_MAX];
776 int protocol_major = TYPEOTHER;
777 char *protocol_minor = NULL;
780 BODY **signatures = NULL;
789 protocol = mutt_get_parameter ("protocol", a->parameter);
792 /* extract the protocol information */
799 if ((protocol_minor = strchr (protocol, '/'))) protocol_minor++;
801 strfcpy (major, protocol, sizeof(major));
802 if((t = strchr(major, '/')))
805 protocol_major = mutt_check_mime_type (major);
808 /* consistency check */
810 if (!(a && a->next && a->next->type == protocol_major &&
811 !mutt_strcasecmp (a->next->subtype, protocol_minor)))
813 state_attach_puts (_("[-- Error: "
814 "Inconsistent multipart/signed structure! --]\n\n"),
816 return mutt_body_handler (a, s);
820 if ((WithCrypto & APPLICATION_PGP)
821 && protocol_major == TYPEAPPLICATION
822 && !ascii_strcasecmp (protocol_minor, "pgp-signature"))
824 else if ((WithCrypto & APPLICATION_SMIME)
825 && protocol_major == TYPEAPPLICATION
826 && !(ascii_strcasecmp (protocol_minor, "x-pkcs7-signature")
827 && ascii_strcasecmp (protocol_minor, "pkcs7-signature")))
829 else if (protocol_major == TYPEMULTIPART
830 && !ascii_strcasecmp (protocol_minor, "mixed"))
834 state_printf (s, _("[-- Error: "
835 "Unknown multipart/signed protocol %s! --]\n\n"),
837 return mutt_body_handler (a, s);
840 if (s->flags & M_DISPLAY)
843 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
847 mutt_mktemp (tempfile, sizeof (tempfile));
848 if (crypt_write_signed (a, s, tempfile) == 0)
850 for (i = 0; i < sigcnt; i++)
852 if ((WithCrypto & APPLICATION_PGP)
853 && signatures[i]->type == TYPEAPPLICATION
854 && !ascii_strcasecmp (signatures[i]->subtype, "pgp-signature"))
856 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
862 if ((WithCrypto & APPLICATION_SMIME)
863 && signatures[i]->type == TYPEAPPLICATION
864 && (!ascii_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
865 || !ascii_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
867 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
873 state_printf (s, _("[-- Warning: "
874 "We can't verify %s/%s signatures. --]\n\n"),
875 TYPE(signatures[i]), signatures[i]->subtype);
879 mutt_unlink (tempfile);
881 b->goodsig = goodsig;
882 b->badsig = !goodsig;
884 /* Now display the signed body */
885 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
891 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"), s);
894 rc = mutt_body_handler (a, s);
896 if (s->flags & M_DISPLAY && sigcnt)
897 state_attach_puts (_("\n[-- End of signed data --]\n"), s);