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>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 #include "mutt_curses.h"
32 #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, char *app_name)
60 char p[STRING], tmp[STRING];
65 if (option (OPTCRYPTTIMESTAMP))
68 setlocale (LC_TIME, "");
69 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
70 setlocale (LC_TIME, "C");
75 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"), NONULL(app_name), p);
76 state_attach_puts (tmp, s);
81 void crypt_forget_passphrase (void)
83 if ((WithCrypto & APPLICATION_PGP))
84 crypt_pgp_void_passphrase ();
86 if ((WithCrypto & APPLICATION_SMIME))
87 crypt_smime_void_passphrase ();
90 mutt_message _("Passphrase(s) forgotten.");
94 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
96 static void disable_coredumps (void)
98 struct rlimit rl = {0, 0};
99 static short done = 0;
103 setrlimit (RLIMIT_CORE, &rl);
108 #endif /* HAVE_SETRLIMIT */
111 int crypt_valid_passphrase(int flags)
113 time_t now = time (NULL);
116 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
117 disable_coredumps ();
120 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
121 ret = crypt_pgp_valid_passphrase ();
123 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
124 ret = crypt_smime_valid_passphrase ();
131 int mutt_protect (HEADER *msg, char *keylist)
133 BODY *pbody = NULL, *tmp_pbody = NULL;
134 BODY *tmp_smime_pbody = NULL;
135 BODY *tmp_pgp_pbody = NULL;
136 int flags = (WithCrypto & APPLICATION_PGP)? msg->security: 0;
142 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
145 if ((WithCrypto & APPLICATION_PGP) && ((msg->security & PGPINLINE) == PGPINLINE))
147 /* they really want to send it inline... go for it */
148 if (!isendwin ()) mutt_endwin _("Invoking PGP...");
149 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
152 msg->content = pbody;
156 /* otherwise inline won't work...ask for revert */
157 if ((i = query_quadoption (OPT_PGPMIMEAUTO, _("Message can't be sent inline. Revert to using PGP/MIME?"))) != M_YES)
159 mutt_error _("Mail not sent.");
163 /* go ahead with PGP/MIME */
166 if (!isendwin ()) mutt_endwin (NULL);
168 if ((WithCrypto & APPLICATION_SMIME))
169 tmp_smime_pbody = msg->content;
170 if ((WithCrypto & APPLICATION_PGP))
171 tmp_pgp_pbody = msg->content;
173 if (msg->security & SIGN)
175 if ((WithCrypto & APPLICATION_SMIME)
176 && (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)))
187 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
191 pbody = tmp_pgp_pbody = tmp_pbody;
195 && (msg->security & APPLICATION_SMIME)
196 && (msg->security & APPLICATION_PGP))
198 /* here comes the draft ;-) */
203 if (msg->security & ENCRYPT)
205 if ((WithCrypto & APPLICATION_SMIME)
206 && (msg->security & APPLICATION_SMIME))
208 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
211 /* signed ? free it! */
214 /* free tmp_body if messages was signed AND encrypted ... */
215 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody)
217 /* detatch and dont't delete msg->content,
218 which tmp_smime_pbody->parts after signing. */
219 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
220 msg->content->next = NULL;
221 mutt_free_body (&tmp_smime_pbody);
226 if ((WithCrypto & APPLICATION_PGP)
227 && (msg->security & APPLICATION_PGP))
229 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
233 /* did we perform a retainable signature? */
234 if (flags != msg->security)
236 /* remove the outer multipart layer */
237 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
238 /* get rid of the signature */
239 mutt_free_body (&tmp_pgp_pbody->next);
245 /* destroy temporary signature envelope when doing retainable
249 if (flags != msg->security)
251 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
252 mutt_free_body (&tmp_pgp_pbody->next);
258 msg->content = pbody;
266 int mutt_is_multipart_signed (BODY *b)
270 if (!b || !(b->type == TYPEMULTIPART) ||
271 !b->subtype || ascii_strcasecmp(b->subtype, "signed"))
274 if (!(p = mutt_get_parameter("protocol", b->parameter)))
277 if (!(ascii_strcasecmp (p, "multipart/mixed")))
280 if ((WithCrypto & APPLICATION_PGP)
281 && !(ascii_strcasecmp (p, "application/pgp-signature")))
284 if ((WithCrypto & APPLICATION_SMIME)
285 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
287 if ((WithCrypto & APPLICATION_SMIME)
288 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
295 int mutt_is_multipart_encrypted (BODY *b)
297 if ((WithCrypto & APPLICATION_PGP))
301 if (!b || b->type != TYPEMULTIPART ||
302 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
303 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
304 ascii_strcasecmp (p, "application/pgp-encrypted"))
314 int mutt_is_application_pgp (BODY *m)
319 if (m->type == TYPEAPPLICATION)
321 if (!ascii_strcasecmp (m->subtype, "pgp") || !ascii_strcasecmp (m->subtype, "x-pgp-message"))
323 if ((p = mutt_get_parameter ("x-action", m->parameter))
324 && (!ascii_strcasecmp (p, "sign") || !ascii_strcasecmp (p, "signclear")))
327 if ((p = mutt_get_parameter ("format", m->parameter)) &&
328 !ascii_strcasecmp (p, "keys-only"))
331 if(!t) t |= PGPENCRYPT; /* not necessarily correct, but... */
334 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
337 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
340 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0)
342 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
343 || (p = mutt_get_parameter ("x-action", m->parameter))
344 || (p = mutt_get_parameter ("action", m->parameter)))
345 && !ascii_strncasecmp ("pgp-sign", p, 8))
347 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
349 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
358 int mutt_is_application_smime (BODY *m)
366 if ((m->type & TYPEAPPLICATION) && m->subtype)
368 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
369 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
370 !ascii_strcasecmp (m->subtype, "pkcs7-mime"))
372 if ((t = mutt_get_parameter ("smime-type", m->parameter)))
374 if (!ascii_strcasecmp (t, "enveloped-data"))
376 else if (!ascii_strcasecmp (t, "signed-data"))
377 return (SMIMESIGN|SMIMEOPAQUE);
381 * Content-Description: S/MIME Encrypted Message
382 * instead of Content-Type parameter
384 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
388 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
391 t = mutt_get_parameter ("name", m->parameter);
393 if (!t) t = m->d_filename;
394 if (!t) t = m->filename;
398 mutt_message (_("S/MIME messages with no hints on content are unsupported."));
402 /* no .p7c, .p10 support yet. */
404 len = mutt_strlen (t) - 4;
405 if (len > 0 && *(t+len) == '.')
408 if (!ascii_strcasecmp ((t+len), "p7m"))
412 /* Not sure if this is the correct thing to do, but
413 it's required for compatibility with Outlook */
414 return (SMIMESIGN|SMIMEOPAQUE);
416 else if (!ascii_strcasecmp ((t+len), "p7s"))
417 return (SMIMESIGN|SMIMEOPAQUE);
429 int crypt_query (BODY *m)
439 if (m->type == TYPEAPPLICATION)
441 if ((WithCrypto & APPLICATION_PGP))
442 t |= mutt_is_application_pgp(m);
444 if ((WithCrypto & APPLICATION_SMIME))
446 t |= mutt_is_application_smime(m);
447 if (t && m->goodsig) t |= GOODSIGN;
448 if (t && m->badsig) t |= BADSIGN;
451 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT)
453 t |= mutt_is_application_pgp (m);
458 if (m->type == TYPEMULTIPART)
460 t |= mutt_is_multipart_encrypted(m);
461 t |= mutt_is_multipart_signed (m);
467 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE)
472 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
473 w = 0; /* Bits set in any part */
475 for (p = m->parts; p; p = p->next)
480 t |= u | (w & ~GOODSIGN);
482 if ((w & GOODSIGN) && !(u & GOODSIGN))
492 int crypt_write_signed(BODY *a, STATE *s, const char *tempfile)
502 if (!(fp = safe_fopen (tempfile, "w")))
504 mutt_perror (tempfile);
508 fseek (s->fpin, a->hdr_offset, 0);
509 bytes = a->length + a->offset - a->hdr_offset;
513 if ((c = fgetc (s->fpin)) == EOF)
522 if (c == '\n' && !hadcr)
538 void convert_to_7bit (BODY *a)
545 if (a->type == TYPEMULTIPART)
547 if (a->encoding != ENC7BIT)
549 a->encoding = ENC7BIT;
550 convert_to_7bit(a->parts);
552 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
553 convert_to_7bit (a->parts);
555 else if (a->type == TYPEMESSAGE &&
556 mutt_strcasecmp(a->subtype, "delivery-status"))
558 if(a->encoding != ENC7BIT)
559 mutt_message_to_7bit (a, NULL);
561 else if (a->encoding == ENC8BIT)
562 a->encoding = ENCQUOTEDPRINTABLE;
563 else if (a->encoding == ENCBINARY)
564 a->encoding = ENCBASE64;
565 else if (a->content && a->encoding != ENCBASE64 &&
566 (a->content->from || (a->content->space &&
567 option (OPTPGPSTRICTENC))))
568 a->encoding = ENCQUOTEDPRINTABLE;
576 void crypt_extract_keys_from_messages (HEADER * h)
579 char tempfname[_POSIX_PATH_MAX], *mbox;
586 mutt_mktemp (tempfname);
587 if (!(fpout = safe_fopen (tempfname, "w")))
589 mutt_perror (tempfname);
593 if ((WithCrypto & APPLICATION_PGP))
594 set_option (OPTDONTHANDLEPGPKEYS);
598 for (i = 0; i < Context->vcount; i++)
600 if (Context->hdrs[Context->v2r[i]]->tagged)
602 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
603 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
604 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->security))
610 if ((WithCrypto & APPLICATION_PGP)
611 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP))
613 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
614 M_CM_DECODE|M_CM_CHARCONV, 0);
617 mutt_endwin (_("Trying to extract PGP keys...\n"));
618 crypt_pgp_invoke_import (tempfname);
621 if ((WithCrypto & APPLICATION_SMIME)
622 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME))
624 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
625 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
626 M_CM_NOHEADER|M_CM_DECODE_CRYPT
627 |M_CM_DECODE_SMIME, 0);
629 mutt_copy_message (fpout, Context,
630 Context->hdrs[Context->v2r[i]], 0, 0);
633 if (Context->hdrs[Context->v2r[i]]->env->from)
634 tmp = mutt_expand_aliases (h->env->from);
635 else if (Context->hdrs[Context->v2r[i]]->env->sender)
636 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
638 mbox = tmp ? tmp->mailbox : NULL;
641 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
642 crypt_smime_invoke_import (tempfname, mbox);
653 mutt_parse_mime_message (Context, h);
654 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security)))
656 if ((WithCrypto & APPLICATION_PGP)
657 && (h->security & APPLICATION_PGP))
659 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
661 mutt_endwin (_("Trying to extract PGP keys...\n"));
662 crypt_pgp_invoke_import (tempfname);
665 if ((WithCrypto & APPLICATION_SMIME)
666 && (h->security & APPLICATION_SMIME))
668 if (h->security & ENCRYPT)
669 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
671 |M_CM_DECODE_SMIME, 0);
673 mutt_copy_message (fpout, Context, h, 0, 0);
676 if (h->env->from) tmp = mutt_expand_aliases (h->env->from);
677 else if (h->env->sender) tmp = mutt_expand_aliases (h->env->sender);
678 mbox = tmp ? tmp->mailbox : NULL;
679 if (mbox) /* else ? */
681 mutt_message (_("Trying to extract S/MIME certificates...\n"));
682 crypt_smime_invoke_import (tempfname, mbox);
690 mutt_any_key_to_continue (NULL);
692 mutt_unlink (tempfname);
694 if ((WithCrypto & APPLICATION_PGP))
695 unset_option (OPTDONTHANDLEPGPKEYS);
700 int crypt_get_keys (HEADER *msg, char **keylist)
702 /* Do a quick check to make sure that we can find all of the encryption
703 * keys if the user has requested this service.
709 if ((WithCrypto & APPLICATION_PGP))
710 set_option (OPTPGPCHECKTRUST);
714 if (msg->security & ENCRYPT)
716 if ((WithCrypto & APPLICATION_PGP)
717 && (msg->security & APPLICATION_PGP))
719 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
720 msg->env->bcc)) == NULL)
722 unset_option (OPTPGPCHECKTRUST);
724 if ((WithCrypto & APPLICATION_SMIME)
725 && (msg->security & APPLICATION_SMIME))
727 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
728 msg->env->bcc)) == NULL)
738 static void crypt_fetch_signatures (BODY ***signatures, BODY *a, int *n)
743 for (; a; a = a->next)
745 if (a->type == TYPEMULTIPART)
746 crypt_fetch_signatures (signatures, a->parts, n);
750 safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
752 (*signatures)[(*n)++] = a;
759 * This routine verifies a "multipart/signed" body.
762 void mutt_signed_handler (BODY *a, STATE *s)
764 char tempfile[_POSIX_PATH_MAX];
766 int protocol_major = TYPEOTHER;
767 char *protocol_minor = NULL;
770 BODY **signatures = NULL;
778 protocol = mutt_get_parameter ("protocol", a->parameter);
781 /* extract the protocol information */
788 if ((protocol_minor = strchr (protocol, '/'))) protocol_minor++;
790 strfcpy (major, protocol, sizeof(major));
791 if((t = strchr(major, '/')))
794 protocol_major = mutt_check_mime_type (major);
797 /* consistency check */
799 if (!(a && a->next && a->next->type == protocol_major &&
800 !mutt_strcasecmp (a->next->subtype, protocol_minor)))
802 state_attach_puts (_("[-- Error: "
803 "Inconsistent multipart/signed structure! --]\n\n"),
805 mutt_body_handler (a, s);
810 if ((WithCrypto & APPLICATION_PGP)
811 && protocol_major == TYPEAPPLICATION
812 && !mutt_strcasecmp (protocol_minor, "pgp-signature"))
814 else if ((WithCrypto & APPLICATION_SMIME)
815 && protocol_major == TYPEAPPLICATION
816 && !(mutt_strcasecmp (protocol_minor, "x-pkcs7-signature")
817 && mutt_strcasecmp (protocol_minor, "pkcs7-signature")))
819 else if (protocol_major == TYPEMULTIPART
820 && !mutt_strcasecmp (protocol_minor, "mixed"))
824 state_printf (s, _("[-- Error: "
825 "Unknown multipart/signed protocol %s! --]\n\n"),
827 mutt_body_handler (a, s);
831 if (s->flags & M_DISPLAY)
834 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
838 mutt_mktemp (tempfile);
839 if (crypt_write_signed (a, s, tempfile) == 0)
841 for (i = 0; i < sigcnt; i++)
843 if ((WithCrypto & APPLICATION_PGP)
844 && signatures[i]->type == TYPEAPPLICATION
845 && !mutt_strcasecmp (signatures[i]->subtype, "pgp-signature"))
847 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
853 if ((WithCrypto & APPLICATION_SMIME)
854 && signatures[i]->type == TYPEAPPLICATION
855 && (!mutt_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
856 || !mutt_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
858 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
864 state_printf (s, _("[-- Warning: "
865 "We can't verify %s/%s signatures. --]\n\n"),
866 TYPE(signatures[i]), signatures[i]->subtype);
870 mutt_unlink (tempfile);
872 b->goodsig = goodsig;
873 b->badsig = !goodsig;
875 /* Now display the signed body */
876 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
882 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"), s);
885 mutt_body_handler (a, s);
887 if (s->flags & M_DISPLAY && sigcnt)
888 state_attach_puts (_("\n[-- End of signed data --]\n"), s);