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)
363 if ((m->type & TYPEAPPLICATION) && m->subtype)
365 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
366 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
367 !ascii_strcasecmp (m->subtype, "pkcs7-mime"))
369 if ((t = mutt_get_parameter ("smime-type", m->parameter)))
371 if (!ascii_strcasecmp (t, "enveloped-data"))
373 else if (!ascii_strcasecmp (t, "signed-data"))
374 return (SMIMESIGN|SMIMEOPAQUE);
378 * Content-Description: S/MIME Encrypted Message
379 * instead of Content-Type parameter
381 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
385 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
388 t = mutt_get_parameter ("name", m->parameter);
390 if (!t) t = m->d_filename;
391 if (!t) t = m->filename;
395 mutt_message (_("S/MIME messages with no hints on content are unsupported."));
399 /* no .p7c, .p10 support yet. */
401 len = mutt_strlen (t) - 4;
402 if (len > 0 && *(t+len) == '.')
405 if (!ascii_strcasecmp ((t+len), "p7m"))
409 /* Not sure if this is the correct thing to do, but
410 it's required for compatibility with Outlook */
411 return (SMIMESIGN|SMIMEOPAQUE);
413 else if (!ascii_strcasecmp ((t+len), "p7s"))
414 return (SMIMESIGN|SMIMEOPAQUE);
426 int crypt_query (BODY *m)
436 if (m->type == TYPEAPPLICATION)
438 if ((WithCrypto & APPLICATION_PGP))
439 t |= mutt_is_application_pgp(m);
441 if ((WithCrypto & APPLICATION_SMIME))
443 t |= mutt_is_application_smime(m);
444 if (t && m->goodsig) t |= GOODSIGN;
445 if (t && m->badsig) t |= BADSIGN;
448 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT)
450 t |= mutt_is_application_pgp (m);
455 if (m->type == TYPEMULTIPART)
457 t |= mutt_is_multipart_encrypted(m);
458 t |= mutt_is_multipart_signed (m);
464 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE)
469 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
470 w = 0; /* Bits set in any part */
472 for (p = m->parts; p; p = p->next)
477 t |= u | (w & ~GOODSIGN);
479 if ((w & GOODSIGN) && !(u & GOODSIGN))
489 int crypt_write_signed(BODY *a, STATE *s, const char *tempfile)
499 if (!(fp = safe_fopen (tempfile, "w")))
501 mutt_perror (tempfile);
505 fseek (s->fpin, a->hdr_offset, 0);
506 bytes = a->length + a->offset - a->hdr_offset;
510 if ((c = fgetc (s->fpin)) == EOF)
519 if (c == '\n' && !hadcr)
535 void convert_to_7bit (BODY *a)
542 if (a->type == TYPEMULTIPART)
544 if (a->encoding != ENC7BIT)
546 a->encoding = ENC7BIT;
547 convert_to_7bit(a->parts);
549 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
550 convert_to_7bit (a->parts);
552 else if (a->type == TYPEMESSAGE &&
553 mutt_strcasecmp(a->subtype, "delivery-status"))
555 if(a->encoding != ENC7BIT)
556 mutt_message_to_7bit (a, NULL);
558 else if (a->encoding == ENC8BIT)
559 a->encoding = ENCQUOTEDPRINTABLE;
560 else if (a->encoding == ENCBINARY)
561 a->encoding = ENCBASE64;
562 else if (a->content && a->encoding != ENCBASE64 &&
563 (a->content->from || (a->content->space &&
564 option (OPTPGPSTRICTENC))))
565 a->encoding = ENCQUOTEDPRINTABLE;
573 void crypt_extract_keys_from_messages (HEADER * h)
576 char tempfname[_POSIX_PATH_MAX], *mbox;
583 mutt_mktemp (tempfname);
584 if (!(fpout = safe_fopen (tempfname, "w")))
586 mutt_perror (tempfname);
590 if ((WithCrypto & APPLICATION_PGP))
591 set_option (OPTDONTHANDLEPGPKEYS);
595 for (i = 0; i < Context->vcount; i++)
597 if (Context->hdrs[Context->v2r[i]]->tagged)
599 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
600 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
601 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->security))
607 if ((WithCrypto & APPLICATION_PGP)
608 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP))
610 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
611 M_CM_DECODE|M_CM_CHARCONV, 0);
614 mutt_endwin (_("Trying to extract PGP keys...\n"));
615 crypt_pgp_invoke_import (tempfname);
618 if ((WithCrypto & APPLICATION_SMIME)
619 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME))
621 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
622 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
623 M_CM_NOHEADER|M_CM_DECODE_CRYPT
624 |M_CM_DECODE_SMIME, 0);
626 mutt_copy_message (fpout, Context,
627 Context->hdrs[Context->v2r[i]], 0, 0);
630 if (Context->hdrs[Context->v2r[i]]->env->from)
631 tmp = mutt_expand_aliases (h->env->from);
632 else if (Context->hdrs[Context->v2r[i]]->env->sender)
633 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
635 mbox = tmp ? tmp->mailbox : NULL;
638 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
639 crypt_smime_invoke_import (tempfname, mbox);
650 mutt_parse_mime_message (Context, h);
651 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security)))
653 if ((WithCrypto & APPLICATION_PGP)
654 && (h->security & APPLICATION_PGP))
656 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
658 mutt_endwin (_("Trying to extract PGP keys...\n"));
659 crypt_pgp_invoke_import (tempfname);
662 if ((WithCrypto & APPLICATION_SMIME)
663 && (h->security & APPLICATION_SMIME))
665 if (h->security & ENCRYPT)
666 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
668 |M_CM_DECODE_SMIME, 0);
670 mutt_copy_message (fpout, Context, h, 0, 0);
673 if (h->env->from) tmp = mutt_expand_aliases (h->env->from);
674 else if (h->env->sender) tmp = mutt_expand_aliases (h->env->sender);
675 mbox = tmp ? tmp->mailbox : NULL;
676 if (mbox) /* else ? */
678 mutt_message (_("Trying to extract S/MIME certificates...\n"));
679 crypt_smime_invoke_import (tempfname, mbox);
687 mutt_any_key_to_continue (NULL);
689 mutt_unlink (tempfname);
691 if ((WithCrypto & APPLICATION_PGP))
692 unset_option (OPTDONTHANDLEPGPKEYS);
697 int crypt_get_keys (HEADER *msg, char **keylist)
699 /* Do a quick check to make sure that we can find all of the encryption
700 * keys if the user has requested this service.
706 if ((WithCrypto & APPLICATION_PGP))
707 set_option (OPTPGPCHECKTRUST);
711 if (msg->security & ENCRYPT)
713 if ((WithCrypto & APPLICATION_PGP)
714 && (msg->security & APPLICATION_PGP))
716 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
717 msg->env->bcc)) == NULL)
719 unset_option (OPTPGPCHECKTRUST);
721 if ((WithCrypto & APPLICATION_SMIME)
722 && (msg->security & APPLICATION_SMIME))
724 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
725 msg->env->bcc)) == NULL)
735 static void crypt_fetch_signatures (BODY ***signatures, BODY *a, int *n)
740 for (; a; a = a->next)
742 if (a->type == TYPEMULTIPART)
743 crypt_fetch_signatures (signatures, a->parts, n);
747 safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
749 (*signatures)[(*n)++] = a;
756 * This routine verifies a "multipart/signed" body.
759 void mutt_signed_handler (BODY *a, STATE *s)
761 char tempfile[_POSIX_PATH_MAX];
763 int protocol_major = TYPEOTHER;
764 char *protocol_minor = NULL;
767 BODY **signatures = NULL;
775 protocol = mutt_get_parameter ("protocol", a->parameter);
778 /* extract the protocol information */
785 if ((protocol_minor = strchr (protocol, '/'))) protocol_minor++;
787 strfcpy (major, protocol, sizeof(major));
788 if((t = strchr(major, '/')))
791 protocol_major = mutt_check_mime_type (major);
794 /* consistency check */
796 if (!(a && a->next && a->next->type == protocol_major &&
797 !mutt_strcasecmp (a->next->subtype, protocol_minor)))
799 state_attach_puts (_("[-- Error: "
800 "Inconsistent multipart/signed structure! --]\n\n"),
802 mutt_body_handler (a, s);
807 if ((WithCrypto & APPLICATION_PGP)
808 && protocol_major == TYPEAPPLICATION
809 && !mutt_strcasecmp (protocol_minor, "pgp-signature"))
811 else if ((WithCrypto & APPLICATION_SMIME)
812 && protocol_major == TYPEAPPLICATION
813 && !(mutt_strcasecmp (protocol_minor, "x-pkcs7-signature")
814 && mutt_strcasecmp (protocol_minor, "pkcs7-signature")))
816 else if (protocol_major == TYPEMULTIPART
817 && !mutt_strcasecmp (protocol_minor, "mixed"))
821 state_printf (s, _("[-- Error: "
822 "Unknown multipart/signed protocol %s! --]\n\n"),
824 mutt_body_handler (a, s);
828 if (s->flags & M_DISPLAY)
831 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
835 mutt_mktemp (tempfile);
836 if (crypt_write_signed (a, s, tempfile) == 0)
838 for (i = 0; i < sigcnt; i++)
840 if ((WithCrypto & APPLICATION_PGP)
841 && signatures[i]->type == TYPEAPPLICATION
842 && !mutt_strcasecmp (signatures[i]->subtype, "pgp-signature"))
844 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
850 if ((WithCrypto & APPLICATION_SMIME)
851 && signatures[i]->type == TYPEAPPLICATION
852 && (!mutt_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
853 || !mutt_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
855 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
861 state_printf (s, _("[-- Warning: "
862 "We can't verify %s/%s signatures. --]\n\n"),
863 TYPE(signatures[i]), signatures[i]->subtype);
867 mutt_unlink (tempfile);
869 b->goodsig = goodsig;
870 b->badsig = !goodsig;
872 /* Now display the signed body */
873 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
879 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"), s);
882 mutt_body_handler (a, s);
884 if (s->flags & M_DISPLAY && sigcnt)
885 state_attach_puts (_("\n[-- End of signed data --]\n"), s);