2 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2000 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>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 #include "mutt_curses.h"
28 #include "mutt_crypt.h"
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
47 #ifdef HAVE_SYS_RESOURCE_H
48 # include <sys/resource.h>
52 /* print the current time to avoid spoofing of the signature output */
53 void crypt_current_time(STATE *s, char *app_name)
56 char p[STRING], tmp[STRING];
61 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"), NONULL(app_name), p);
72 state_attach_puts (tmp, s);
77 void crypt_forget_passphrase (void)
79 if ((WithCrypto & APPLICATION_PGP))
80 crypt_pgp_void_passphrase ();
82 if ((WithCrypto & APPLICATION_SMIME))
83 crypt_smime_void_passphrase ();
86 mutt_message _("Passphrase(s) forgotten.");
90 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
92 static void disable_coredumps (void)
94 struct rlimit rl = {0, 0};
95 static short done = 0;
99 setrlimit (RLIMIT_CORE, &rl);
104 #endif /* HAVE_SETRLIMIT */
107 int crypt_valid_passphrase(int flags)
109 time_t now = time (NULL);
111 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
112 disable_coredumps ();
115 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
117 extern char PgpPass[STRING];
118 extern time_t PgpExptime;
120 if (pgp_use_gpg_agent())
123 return 1; /* handled by gpg-agent */
126 if (now < PgpExptime) return 1; /* just use the cached copy. */
127 crypt_pgp_void_passphrase ();
129 if (mutt_get_password (_("Enter PGP passphrase:"),
130 PgpPass, sizeof (PgpPass)) == 0)
132 PgpExptime = time (NULL) + PgpTimeout;
139 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
141 extern char SmimePass[STRING];
142 extern time_t SmimeExptime;
144 if (now < SmimeExptime) return (1);
145 crypt_smime_void_passphrase ();
147 if (mutt_get_password (_("Enter SMIME passphrase:"), SmimePass,
148 sizeof (SmimePass)) == 0)
150 SmimeExptime = time (NULL) + SmimeTimeout;
162 int mutt_protect (HEADER *msg, HEADER *cur, char *keylist)
164 BODY *pbody = NULL, *tmp_pbody = NULL;
165 BODY *tmp_smime_pbody = NULL;
166 BODY *tmp_pgp_pbody = NULL;
168 int flags = (WithCrypto & APPLICATION_PGP)? msg->security: 0;
174 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
177 if ((WithCrypto & APPLICATION_PGP) && (msg->security & APPLICATION_PGP))
179 if ((msg->content->type == TYPETEXT) &&
180 !ascii_strcasecmp (msg->content->subtype, "plain"))
182 if (cur && cur->security && option (OPTPGPAUTOTRAD)
183 && (option (OPTCRYPTREPLYENCRYPT)
184 || option (OPTCRYPTREPLYSIGN)
185 || option (OPTCRYPTREPLYSIGNENCRYPTED)))
187 if(mutt_is_application_pgp(cur->content))
192 if ((i = query_quadoption (OPT_PGPTRADITIONAL, _("Create a traditional (inline) PGP message?"))) == -1)
200 if (!isendwin ()) mutt_endwin _("Invoking PGP...");
201 if (!(pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist)))
204 msg->content = pbody;
209 if (!isendwin ()) mutt_endwin (NULL);
211 if ((WithCrypto & APPLICATION_SMIME))
212 tmp_smime_pbody = msg->content;
213 if ((WithCrypto & APPLICATION_PGP))
214 tmp_pgp_pbody = msg->content;
216 if (msg->security & SIGN)
218 if ((WithCrypto & APPLICATION_SMIME)
219 && (msg->security & APPLICATION_SMIME))
221 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
223 pbody = tmp_smime_pbody = tmp_pbody;
226 if ((WithCrypto & APPLICATION_PGP)
227 && (msg->security & APPLICATION_PGP)
228 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG)))
230 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
234 pbody = tmp_pgp_pbody = tmp_pbody;
238 && (msg->security & APPLICATION_SMIME)
239 && (msg->security & APPLICATION_PGP))
241 /* here comes the draft ;-) */
246 if (msg->security & ENCRYPT)
248 if ((WithCrypto & APPLICATION_SMIME)
249 && (msg->security & APPLICATION_SMIME))
251 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
254 /* signed ? free it! */
257 /* free tmp_body if messages was signed AND encrypted ... */
258 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody)
260 /* detatch and dont't delete msg->content,
261 which tmp_smime_pbody->parts after signing. */
262 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
263 msg->content->next = NULL;
264 mutt_free_body (&tmp_smime_pbody);
269 if ((WithCrypto & APPLICATION_PGP)
270 && (msg->security & APPLICATION_PGP))
272 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
276 /* did we perform a retainable signature? */
277 if (flags != msg->security)
279 /* remove the outer multipart layer */
280 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
281 /* get rid of the signature */
282 mutt_free_body (&tmp_pgp_pbody->next);
288 /* destroy temporary signature envelope when doing retainable
291 if (flags != msg->security)
293 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
294 mutt_free_body (&tmp_pgp_pbody->next);
300 msg->content = pbody;
308 int mutt_is_multipart_signed (BODY *b)
312 if (!b || !(b->type == TYPEMULTIPART) ||
313 !b->subtype || ascii_strcasecmp(b->subtype, "signed"))
316 if (!(p = mutt_get_parameter("protocol", b->parameter)))
319 if (!(ascii_strcasecmp (p, "multipart/mixed")))
322 if ((WithCrypto & APPLICATION_PGP)
323 && !(ascii_strcasecmp (p, "application/pgp-signature")))
326 if ((WithCrypto & APPLICATION_SMIME)
327 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
329 if ((WithCrypto & APPLICATION_SMIME)
330 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
337 int mutt_is_multipart_encrypted (BODY *b)
339 if ((WithCrypto & APPLICATION_PGP))
343 if (!b || b->type != TYPEMULTIPART ||
344 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
345 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
346 ascii_strcasecmp (p, "application/pgp-encrypted"))
356 int mutt_is_application_pgp (BODY *m)
361 if (m->type == TYPEAPPLICATION)
363 if (!ascii_strcasecmp (m->subtype, "pgp") || !ascii_strcasecmp (m->subtype, "x-pgp-message"))
365 if ((p = mutt_get_parameter ("x-action", m->parameter))
366 && (!ascii_strcasecmp (p, "sign") || !ascii_strcasecmp (p, "signclear")))
369 if ((p = mutt_get_parameter ("format", m->parameter)) &&
370 !ascii_strcasecmp (p, "keys-only"))
373 if(!t) t |= PGPENCRYPT; /* not necessarily correct, but... */
376 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
379 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
382 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0)
384 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
385 || (p = mutt_get_parameter ("x-action", m->parameter))
386 || (p = mutt_get_parameter ("action", m->parameter)))
387 && !ascii_strncasecmp ("pgp-sign", p, 8))
389 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
391 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
397 int mutt_is_application_smime (BODY *m)
402 if ((m->type & TYPEAPPLICATION) && m->subtype)
404 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
405 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
406 !ascii_strcasecmp (m->subtype, "pkcs7-mime"))
408 if ((t = mutt_get_parameter ("smime-type", m->parameter)))
410 if (!ascii_strcasecmp (t, "enveloped-data"))
412 else if (!ascii_strcasecmp (t, "signed-data"))
413 return (SMIMESIGN|SMIMEOPAQUE);
417 * Content-Description: S/MIME Encrypted Message
418 * instead of Content-Type parameter
420 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
424 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
427 t = mutt_get_parameter ("name", m->parameter);
429 if (!t) t = m->d_filename;
430 if (!t) t = m->filename;
434 mutt_message (_("S/MIME messages with no hints on content are unsupported."));
438 /* no .p7c, .p10 support yet. */
440 len = mutt_strlen (t) - 4;
441 if (len > 0 && *(t+len) == '.')
444 if (!ascii_strcasecmp ((t+len), "p7m"))
448 /* Not sure if this is the correct thing to do, but
449 it's required for compatibility with Outlook */
450 return (SMIMESIGN|SMIMEOPAQUE);
452 else if (!ascii_strcasecmp ((t+len), "p7s"))
453 return (SMIMESIGN|SMIMEOPAQUE);
465 int crypt_query (BODY *m)
475 if (m->type == TYPEAPPLICATION)
477 if ((WithCrypto & APPLICATION_PGP))
478 t |= mutt_is_application_pgp(m);
480 if ((WithCrypto & APPLICATION_SMIME))
482 t |= mutt_is_application_smime(m);
483 if (t && m->goodsig) t |= GOODSIGN;
484 if (t && m->badsig) t |= BADSIGN;
487 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT)
489 t |= mutt_is_application_pgp (m);
494 if (m->type == TYPEMULTIPART)
496 t |= mutt_is_multipart_encrypted(m);
497 t |= mutt_is_multipart_signed (m);
503 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE)
508 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
509 w = 0; /* Bits set in any part */
511 for (p = m->parts; p; p = p->next)
516 t |= u | (w & ~GOODSIGN);
518 if ((w & GOODSIGN) && !(u & GOODSIGN))
528 int crypt_write_signed(BODY *a, STATE *s, const char *tempfile)
538 if (!(fp = safe_fopen (tempfile, "w")))
540 mutt_perror (tempfile);
544 fseek (s->fpin, a->hdr_offset, 0);
545 bytes = a->length + a->offset - a->hdr_offset;
549 if ((c = fgetc (s->fpin)) == EOF)
558 if (c == '\n' && !hadcr)
574 void convert_to_7bit (BODY *a)
581 if (a->type == TYPEMULTIPART)
583 if (a->encoding != ENC7BIT)
585 a->encoding = ENC7BIT;
586 convert_to_7bit(a->parts);
588 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
589 convert_to_7bit (a->parts);
591 else if (a->type == TYPEMESSAGE &&
592 mutt_strcasecmp(a->subtype, "delivery-status"))
594 if(a->encoding != ENC7BIT)
595 mutt_message_to_7bit (a, NULL);
597 else if (a->encoding == ENC8BIT)
598 a->encoding = ENCQUOTEDPRINTABLE;
599 else if (a->encoding == ENCBINARY)
600 a->encoding = ENCBASE64;
601 else if (a->content && a->encoding != ENCBASE64 &&
602 (a->content->from || (a->content->space &&
603 option (OPTPGPSTRICTENC))))
604 a->encoding = ENCQUOTEDPRINTABLE;
612 void crypt_extract_keys_from_messages (HEADER * h)
615 char tempfname[_POSIX_PATH_MAX], *mbox;
622 mutt_mktemp (tempfname);
623 if (!(fpout = safe_fopen (tempfname, "w")))
625 mutt_perror (tempfname);
629 if ((WithCrypto & APPLICATION_PGP))
630 set_option (OPTDONTHANDLEPGPKEYS);
634 for (i = 0; i < Context->vcount; i++)
636 if (Context->hdrs[Context->v2r[i]]->tagged)
638 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
639 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
640 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->security))
646 if ((WithCrypto & APPLICATION_PGP)
647 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP))
649 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
650 M_CM_DECODE|M_CM_CHARCONV, 0);
653 mutt_endwin (_("Trying to extract PGP keys...\n"));
654 crypt_pgp_invoke_import (tempfname);
657 if ((WithCrypto & APPLICATION_SMIME)
658 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME))
660 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
661 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
662 M_CM_NOHEADER|M_CM_DECODE_CRYPT
663 |M_CM_DECODE_SMIME, 0);
665 mutt_copy_message (fpout, Context,
666 Context->hdrs[Context->v2r[i]], 0, 0);
669 if (Context->hdrs[Context->v2r[i]]->env->from)
670 tmp = mutt_expand_aliases (h->env->from);
671 else if (Context->hdrs[Context->v2r[i]]->env->sender)
672 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
674 mbox = tmp ? tmp->mailbox : NULL;
677 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
678 crypt_smime_invoke_import (tempfname, mbox);
689 mutt_parse_mime_message (Context, h);
690 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security)))
692 if ((WithCrypto & APPLICATION_PGP)
693 && (h->security & APPLICATION_PGP))
695 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
697 mutt_endwin (_("Trying to extract PGP keys...\n"));
698 crypt_pgp_invoke_import (tempfname);
701 if ((WithCrypto & APPLICATION_SMIME)
702 && (h->security & APPLICATION_SMIME))
704 if (h->security & ENCRYPT)
705 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
707 |M_CM_DECODE_SMIME, 0);
709 mutt_copy_message (fpout, Context, h, 0, 0);
712 if (h->env->from) tmp = mutt_expand_aliases (h->env->from);
713 else if (h->env->sender) tmp = mutt_expand_aliases (h->env->sender);
714 mbox = tmp ? tmp->mailbox : NULL;
715 if (mbox) /* else ? */
717 mutt_message (_("Trying to extract S/MIME certificates...\n"));
718 crypt_smime_invoke_import (tempfname, mbox);
726 mutt_any_key_to_continue (NULL);
728 mutt_unlink (tempfname);
730 if ((WithCrypto & APPLICATION_PGP))
731 unset_option (OPTDONTHANDLEPGPKEYS);
736 int crypt_get_keys (HEADER *msg, char **keylist)
738 /* Do a quick check to make sure that we can find all of the encryption
739 * keys if the user has requested this service.
745 if ((WithCrypto & APPLICATION_PGP))
746 set_option (OPTPGPCHECKTRUST);
750 if (msg->security & ENCRYPT)
752 if ((WithCrypto & APPLICATION_PGP)
753 && (msg->security & APPLICATION_PGP))
755 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
756 msg->env->bcc)) == NULL)
758 unset_option (OPTPGPCHECKTRUST);
760 if ((WithCrypto & APPLICATION_SMIME)
761 && (msg->security & APPLICATION_SMIME))
763 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
764 msg->env->bcc)) == NULL)
774 static void crypt_fetch_signatures (BODY ***signatures, BODY *a, int *n)
779 for (; a; a = a->next)
781 if (a->type == TYPEMULTIPART)
782 crypt_fetch_signatures (signatures, a->parts, n);
786 safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
788 (*signatures)[(*n)++] = a;
795 * This routine verifies a "multipart/signed" body.
798 void mutt_signed_handler (BODY *a, STATE *s)
800 char tempfile[_POSIX_PATH_MAX];
802 int protocol_major = TYPEOTHER;
803 char *protocol_minor = NULL;
806 BODY **signatures = NULL;
814 protocol = mutt_get_parameter ("protocol", a->parameter);
817 /* extract the protocol information */
824 if ((protocol_minor = strchr (protocol, '/'))) protocol_minor++;
826 strfcpy (major, protocol, sizeof(major));
827 if((t = strchr(major, '/')))
830 protocol_major = mutt_check_mime_type (major);
833 /* consistency check */
835 if (!(a && a->next && a->next->type == protocol_major &&
836 !mutt_strcasecmp (a->next->subtype, protocol_minor)))
838 state_attach_puts (_("[-- Error: "
839 "Inconsistent multipart/signed structure! --]\n\n"),
841 mutt_body_handler (a, s);
846 if ((WithCrypto & APPLICATION_PGP)
847 && protocol_major == TYPEAPPLICATION
848 && !mutt_strcasecmp (protocol_minor, "pgp-signature"))
850 else if ((WithCrypto & APPLICATION_SMIME)
851 && protocol_major == TYPEAPPLICATION
852 && !(mutt_strcasecmp (protocol_minor, "x-pkcs7-signature")
853 && mutt_strcasecmp (protocol_minor, "pkcs7-signature")))
855 else if (protocol_major == TYPEMULTIPART
856 && !mutt_strcasecmp (protocol_minor, "mixed"))
860 state_printf (s, _("[-- Error: "
861 "Unknown multipart/signed protocol %s! --]\n\n"),
863 mutt_body_handler (a, s);
867 if (s->flags & M_DISPLAY)
870 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
874 mutt_mktemp (tempfile);
875 if (crypt_write_signed (a, s, tempfile) == 0)
877 for (i = 0; i < sigcnt; i++)
879 if ((WithCrypto & APPLICATION_PGP)
880 && signatures[i]->type == TYPEAPPLICATION
881 && !mutt_strcasecmp (signatures[i]->subtype, "pgp-signature"))
883 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
889 if ((WithCrypto & APPLICATION_SMIME)
890 && signatures[i]->type == TYPEAPPLICATION
891 && (!mutt_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
892 || !mutt_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
894 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
900 state_printf (s, _("[-- Warning: "
901 "We can't verify %s/%s signatures. --]\n\n"),
902 TYPE(signatures[i]), signatures[i]->subtype);
906 mutt_unlink (tempfile);
908 b->goodsig = goodsig;
909 b->badsig = !goodsig;
911 /* Now display the signed body */
912 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
918 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"), s);
921 mutt_body_handler (a, s);
923 if (s->flags & M_DISPLAY && sigcnt)
924 state_attach_puts (_("\n[-- End of signed data --]\n"), s);