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.
26 #include "mutt_curses.h"
29 #include "mutt_crypt.h"
44 #ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
48 #ifdef HAVE_SYS_RESOURCE_H
49 # include <sys/resource.h>
53 /* print the current time to avoid spoofing of the signature output */
54 void crypt_current_time(STATE *s, char *app_name)
57 char p[STRING], tmp[STRING];
62 if (option (OPTCRYPTTIMESTAMP))
65 setlocale (LC_TIME, "");
66 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
67 setlocale (LC_TIME, "C");
72 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"), NONULL(app_name), p);
73 state_attach_puts (tmp, s);
78 void crypt_forget_passphrase (void)
80 if ((WithCrypto & APPLICATION_PGP))
81 crypt_pgp_void_passphrase ();
83 if ((WithCrypto & APPLICATION_SMIME))
84 crypt_smime_void_passphrase ();
87 mutt_message _("Passphrase(s) forgotten.");
91 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
93 static void disable_coredumps (void)
95 struct rlimit rl = {0, 0};
96 static short done = 0;
100 setrlimit (RLIMIT_CORE, &rl);
105 #endif /* HAVE_SETRLIMIT */
108 int crypt_valid_passphrase(int flags)
110 time_t now = time (NULL);
113 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
114 disable_coredumps ();
117 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
118 ret = crypt_pgp_valid_passphrase ();
120 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
121 ret = crypt_smime_valid_passphrase ();
128 int mutt_protect (HEADER *msg, char *keylist)
130 BODY *pbody = NULL, *tmp_pbody = NULL;
131 BODY *tmp_smime_pbody = NULL;
132 BODY *tmp_pgp_pbody = NULL;
133 int flags = (WithCrypto & APPLICATION_PGP)? msg->security: 0;
139 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
142 if ((WithCrypto & APPLICATION_PGP) && ((msg->security & PGPINLINE) == PGPINLINE))
144 /* they really want to send it inline... go for it */
145 if (!isendwin ()) mutt_endwin _("Invoking PGP...");
146 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
149 msg->content = pbody;
153 /* otherwise inline won't work...ask for revert */
154 if ((i = query_quadoption (OPT_PGPMIMEAUTO, _("Message can't be sent inline. Revert to using PGP/MIME?"))) != M_YES)
156 mutt_error _("Mail not sent.");
160 /* go ahead with PGP/MIME */
163 if (!isendwin ()) mutt_endwin (NULL);
165 if ((WithCrypto & APPLICATION_SMIME))
166 tmp_smime_pbody = msg->content;
167 if ((WithCrypto & APPLICATION_PGP))
168 tmp_pgp_pbody = msg->content;
170 if (msg->security & SIGN)
172 if ((WithCrypto & APPLICATION_SMIME)
173 && (msg->security & APPLICATION_SMIME))
175 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
177 pbody = tmp_smime_pbody = tmp_pbody;
180 if ((WithCrypto & APPLICATION_PGP)
181 && (msg->security & APPLICATION_PGP)
182 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG)))
184 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
188 pbody = tmp_pgp_pbody = tmp_pbody;
192 && (msg->security & APPLICATION_SMIME)
193 && (msg->security & APPLICATION_PGP))
195 /* here comes the draft ;-) */
200 if (msg->security & ENCRYPT)
202 if ((WithCrypto & APPLICATION_SMIME)
203 && (msg->security & APPLICATION_SMIME))
205 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
208 /* signed ? free it! */
211 /* free tmp_body if messages was signed AND encrypted ... */
212 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody)
214 /* detatch and dont't delete msg->content,
215 which tmp_smime_pbody->parts after signing. */
216 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
217 msg->content->next = NULL;
218 mutt_free_body (&tmp_smime_pbody);
223 if ((WithCrypto & APPLICATION_PGP)
224 && (msg->security & APPLICATION_PGP))
226 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
230 /* did we perform a retainable signature? */
231 if (flags != msg->security)
233 /* remove the outer multipart layer */
234 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
235 /* get rid of the signature */
236 mutt_free_body (&tmp_pgp_pbody->next);
242 /* destroy temporary signature envelope when doing retainable
246 if (flags != msg->security)
248 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
249 mutt_free_body (&tmp_pgp_pbody->next);
255 msg->content = pbody;
263 int mutt_is_multipart_signed (BODY *b)
267 if (!b || !(b->type == TYPEMULTIPART) ||
268 !b->subtype || ascii_strcasecmp(b->subtype, "signed"))
271 if (!(p = mutt_get_parameter("protocol", b->parameter)))
274 if (!(ascii_strcasecmp (p, "multipart/mixed")))
277 if ((WithCrypto & APPLICATION_PGP)
278 && !(ascii_strcasecmp (p, "application/pgp-signature")))
281 if ((WithCrypto & APPLICATION_SMIME)
282 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
284 if ((WithCrypto & APPLICATION_SMIME)
285 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
292 int mutt_is_multipart_encrypted (BODY *b)
294 if ((WithCrypto & APPLICATION_PGP))
298 if (!b || b->type != TYPEMULTIPART ||
299 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
300 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
301 ascii_strcasecmp (p, "application/pgp-encrypted"))
311 int mutt_is_application_pgp (BODY *m)
316 if (m->type == TYPEAPPLICATION)
318 if (!ascii_strcasecmp (m->subtype, "pgp") || !ascii_strcasecmp (m->subtype, "x-pgp-message"))
320 if ((p = mutt_get_parameter ("x-action", m->parameter))
321 && (!ascii_strcasecmp (p, "sign") || !ascii_strcasecmp (p, "signclear")))
324 if ((p = mutt_get_parameter ("format", m->parameter)) &&
325 !ascii_strcasecmp (p, "keys-only"))
328 if(!t) t |= PGPENCRYPT; /* not necessarily correct, but... */
331 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
334 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
337 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0)
339 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
340 || (p = mutt_get_parameter ("x-action", m->parameter))
341 || (p = mutt_get_parameter ("action", m->parameter)))
342 && !ascii_strncasecmp ("pgp-sign", p, 8))
344 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
346 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
355 int mutt_is_application_smime (BODY *m)
360 if ((m->type & TYPEAPPLICATION) && m->subtype)
362 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
363 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
364 !ascii_strcasecmp (m->subtype, "pkcs7-mime"))
366 if ((t = mutt_get_parameter ("smime-type", m->parameter)))
368 if (!ascii_strcasecmp (t, "enveloped-data"))
370 else if (!ascii_strcasecmp (t, "signed-data"))
371 return (SMIMESIGN|SMIMEOPAQUE);
375 * Content-Description: S/MIME Encrypted Message
376 * instead of Content-Type parameter
378 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
382 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
385 t = mutt_get_parameter ("name", m->parameter);
387 if (!t) t = m->d_filename;
388 if (!t) t = m->filename;
392 mutt_message (_("S/MIME messages with no hints on content are unsupported."));
396 /* no .p7c, .p10 support yet. */
398 len = mutt_strlen (t) - 4;
399 if (len > 0 && *(t+len) == '.')
402 if (!ascii_strcasecmp ((t+len), "p7m"))
406 /* Not sure if this is the correct thing to do, but
407 it's required for compatibility with Outlook */
408 return (SMIMESIGN|SMIMEOPAQUE);
410 else if (!ascii_strcasecmp ((t+len), "p7s"))
411 return (SMIMESIGN|SMIMEOPAQUE);
423 int crypt_query (BODY *m)
433 if (m->type == TYPEAPPLICATION)
435 if ((WithCrypto & APPLICATION_PGP))
436 t |= mutt_is_application_pgp(m);
438 if ((WithCrypto & APPLICATION_SMIME))
440 t |= mutt_is_application_smime(m);
441 if (t && m->goodsig) t |= GOODSIGN;
442 if (t && m->badsig) t |= BADSIGN;
445 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT)
447 t |= mutt_is_application_pgp (m);
452 if (m->type == TYPEMULTIPART)
454 t |= mutt_is_multipart_encrypted(m);
455 t |= mutt_is_multipart_signed (m);
461 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE)
466 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
467 w = 0; /* Bits set in any part */
469 for (p = m->parts; p; p = p->next)
474 t |= u | (w & ~GOODSIGN);
476 if ((w & GOODSIGN) && !(u & GOODSIGN))
486 int crypt_write_signed(BODY *a, STATE *s, const char *tempfile)
496 if (!(fp = safe_fopen (tempfile, "w")))
498 mutt_perror (tempfile);
502 fseek (s->fpin, a->hdr_offset, 0);
503 bytes = a->length + a->offset - a->hdr_offset;
507 if ((c = fgetc (s->fpin)) == EOF)
516 if (c == '\n' && !hadcr)
532 void convert_to_7bit (BODY *a)
539 if (a->type == TYPEMULTIPART)
541 if (a->encoding != ENC7BIT)
543 a->encoding = ENC7BIT;
544 convert_to_7bit(a->parts);
546 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
547 convert_to_7bit (a->parts);
549 else if (a->type == TYPEMESSAGE &&
550 mutt_strcasecmp(a->subtype, "delivery-status"))
552 if(a->encoding != ENC7BIT)
553 mutt_message_to_7bit (a, NULL);
555 else if (a->encoding == ENC8BIT)
556 a->encoding = ENCQUOTEDPRINTABLE;
557 else if (a->encoding == ENCBINARY)
558 a->encoding = ENCBASE64;
559 else if (a->content && a->encoding != ENCBASE64 &&
560 (a->content->from || (a->content->space &&
561 option (OPTPGPSTRICTENC))))
562 a->encoding = ENCQUOTEDPRINTABLE;
570 void crypt_extract_keys_from_messages (HEADER * h)
573 char tempfname[_POSIX_PATH_MAX], *mbox;
580 mutt_mktemp (tempfname);
581 if (!(fpout = safe_fopen (tempfname, "w")))
583 mutt_perror (tempfname);
587 if ((WithCrypto & APPLICATION_PGP))
588 set_option (OPTDONTHANDLEPGPKEYS);
592 for (i = 0; i < Context->vcount; i++)
594 if (Context->hdrs[Context->v2r[i]]->tagged)
596 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
597 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
598 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->security))
604 if ((WithCrypto & APPLICATION_PGP)
605 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP))
607 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
608 M_CM_DECODE|M_CM_CHARCONV, 0);
611 mutt_endwin (_("Trying to extract PGP keys...\n"));
612 crypt_pgp_invoke_import (tempfname);
615 if ((WithCrypto & APPLICATION_SMIME)
616 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME))
618 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
619 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
620 M_CM_NOHEADER|M_CM_DECODE_CRYPT
621 |M_CM_DECODE_SMIME, 0);
623 mutt_copy_message (fpout, Context,
624 Context->hdrs[Context->v2r[i]], 0, 0);
627 if (Context->hdrs[Context->v2r[i]]->env->from)
628 tmp = mutt_expand_aliases (h->env->from);
629 else if (Context->hdrs[Context->v2r[i]]->env->sender)
630 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
632 mbox = tmp ? tmp->mailbox : NULL;
635 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
636 crypt_smime_invoke_import (tempfname, mbox);
647 mutt_parse_mime_message (Context, h);
648 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security)))
650 if ((WithCrypto & APPLICATION_PGP)
651 && (h->security & APPLICATION_PGP))
653 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
655 mutt_endwin (_("Trying to extract PGP keys...\n"));
656 crypt_pgp_invoke_import (tempfname);
659 if ((WithCrypto & APPLICATION_SMIME)
660 && (h->security & APPLICATION_SMIME))
662 if (h->security & ENCRYPT)
663 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
665 |M_CM_DECODE_SMIME, 0);
667 mutt_copy_message (fpout, Context, h, 0, 0);
670 if (h->env->from) tmp = mutt_expand_aliases (h->env->from);
671 else if (h->env->sender) tmp = mutt_expand_aliases (h->env->sender);
672 mbox = tmp ? tmp->mailbox : NULL;
673 if (mbox) /* else ? */
675 mutt_message (_("Trying to extract S/MIME certificates...\n"));
676 crypt_smime_invoke_import (tempfname, mbox);
684 mutt_any_key_to_continue (NULL);
686 mutt_unlink (tempfname);
688 if ((WithCrypto & APPLICATION_PGP))
689 unset_option (OPTDONTHANDLEPGPKEYS);
694 int crypt_get_keys (HEADER *msg, char **keylist)
696 /* Do a quick check to make sure that we can find all of the encryption
697 * keys if the user has requested this service.
703 if ((WithCrypto & APPLICATION_PGP))
704 set_option (OPTPGPCHECKTRUST);
708 if (msg->security & ENCRYPT)
710 if ((WithCrypto & APPLICATION_PGP)
711 && (msg->security & APPLICATION_PGP))
713 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
714 msg->env->bcc)) == NULL)
716 unset_option (OPTPGPCHECKTRUST);
718 if ((WithCrypto & APPLICATION_SMIME)
719 && (msg->security & APPLICATION_SMIME))
721 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
722 msg->env->bcc)) == NULL)
732 static void crypt_fetch_signatures (BODY ***signatures, BODY *a, int *n)
737 for (; a; a = a->next)
739 if (a->type == TYPEMULTIPART)
740 crypt_fetch_signatures (signatures, a->parts, n);
744 safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
746 (*signatures)[(*n)++] = a;
753 * This routine verifies a "multipart/signed" body.
756 void mutt_signed_handler (BODY *a, STATE *s)
758 char tempfile[_POSIX_PATH_MAX];
760 int protocol_major = TYPEOTHER;
761 char *protocol_minor = NULL;
764 BODY **signatures = NULL;
772 protocol = mutt_get_parameter ("protocol", a->parameter);
775 /* extract the protocol information */
782 if ((protocol_minor = strchr (protocol, '/'))) protocol_minor++;
784 strfcpy (major, protocol, sizeof(major));
785 if((t = strchr(major, '/')))
788 protocol_major = mutt_check_mime_type (major);
791 /* consistency check */
793 if (!(a && a->next && a->next->type == protocol_major &&
794 !mutt_strcasecmp (a->next->subtype, protocol_minor)))
796 state_attach_puts (_("[-- Error: "
797 "Inconsistent multipart/signed structure! --]\n\n"),
799 mutt_body_handler (a, s);
804 if ((WithCrypto & APPLICATION_PGP)
805 && protocol_major == TYPEAPPLICATION
806 && !mutt_strcasecmp (protocol_minor, "pgp-signature"))
808 else if ((WithCrypto & APPLICATION_SMIME)
809 && protocol_major == TYPEAPPLICATION
810 && !(mutt_strcasecmp (protocol_minor, "x-pkcs7-signature")
811 && mutt_strcasecmp (protocol_minor, "pkcs7-signature")))
813 else if (protocol_major == TYPEMULTIPART
814 && !mutt_strcasecmp (protocol_minor, "mixed"))
818 state_printf (s, _("[-- Error: "
819 "Unknown multipart/signed protocol %s! --]\n\n"),
821 mutt_body_handler (a, s);
825 if (s->flags & M_DISPLAY)
828 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
832 mutt_mktemp (tempfile);
833 if (crypt_write_signed (a, s, tempfile) == 0)
835 for (i = 0; i < sigcnt; i++)
837 if ((WithCrypto & APPLICATION_PGP)
838 && signatures[i]->type == TYPEAPPLICATION
839 && !mutt_strcasecmp (signatures[i]->subtype, "pgp-signature"))
841 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
847 if ((WithCrypto & APPLICATION_SMIME)
848 && signatures[i]->type == TYPEAPPLICATION
849 && (!mutt_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
850 || !mutt_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
852 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
858 state_printf (s, _("[-- Warning: "
859 "We can't verify %s/%s signatures. --]\n\n"),
860 TYPE(signatures[i]), signatures[i]->subtype);
864 mutt_unlink (tempfile);
866 b->goodsig = goodsig;
867 b->badsig = !goodsig;
869 /* Now display the signed body */
870 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
876 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"), s);
879 mutt_body_handler (a, s);
881 if (s->flags & M_DISPLAY && sigcnt)
882 state_attach_puts (_("\n[-- End of signed data --]\n"), s);