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)
115 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
116 disable_coredumps ();
119 if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
120 ret = crypt_pgp_valid_passphrase ();
122 if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
123 ret = crypt_smime_valid_passphrase ();
130 int mutt_protect (HEADER *msg, char *keylist)
132 BODY *pbody = NULL, *tmp_pbody = NULL;
133 BODY *tmp_smime_pbody = NULL;
134 BODY *tmp_pgp_pbody = NULL;
135 int flags = (WithCrypto & APPLICATION_PGP)? msg->security: 0;
141 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
144 if ((WithCrypto & APPLICATION_PGP) && ((msg->security & PGPINLINE) == PGPINLINE))
146 /* they really want to send it inline... go for it */
147 if (!isendwin ()) mutt_endwin _("Invoking PGP...");
148 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
151 msg->content = pbody;
155 /* otherwise inline won't work...ask for revert */
156 if ((i = query_quadoption (OPT_PGPMIMEAUTO, _("Message can't be sent inline. Revert to using PGP/MIME?"))) != M_YES)
158 mutt_error _("Mail not sent.");
162 /* go ahead with PGP/MIME */
165 if (!isendwin ()) mutt_endwin (NULL);
167 if ((WithCrypto & APPLICATION_SMIME))
168 tmp_smime_pbody = msg->content;
169 if ((WithCrypto & APPLICATION_PGP))
170 tmp_pgp_pbody = msg->content;
172 if (msg->security & SIGN)
174 if ((WithCrypto & APPLICATION_SMIME)
175 && (msg->security & APPLICATION_SMIME))
177 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
179 pbody = tmp_smime_pbody = tmp_pbody;
182 if ((WithCrypto & APPLICATION_PGP)
183 && (msg->security & APPLICATION_PGP)
184 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG)))
186 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
190 pbody = tmp_pgp_pbody = tmp_pbody;
194 && (msg->security & APPLICATION_SMIME)
195 && (msg->security & APPLICATION_PGP))
197 /* here comes the draft ;-) */
202 if (msg->security & ENCRYPT)
204 if ((WithCrypto & APPLICATION_SMIME)
205 && (msg->security & APPLICATION_SMIME))
207 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
210 /* signed ? free it! */
213 /* free tmp_body if messages was signed AND encrypted ... */
214 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody)
216 /* detatch and dont't delete msg->content,
217 which tmp_smime_pbody->parts after signing. */
218 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
219 msg->content->next = NULL;
220 mutt_free_body (&tmp_smime_pbody);
225 if ((WithCrypto & APPLICATION_PGP)
226 && (msg->security & APPLICATION_PGP))
228 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
232 /* did we perform a retainable signature? */
233 if (flags != msg->security)
235 /* remove the outer multipart layer */
236 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
237 /* get rid of the signature */
238 mutt_free_body (&tmp_pgp_pbody->next);
244 /* destroy temporary signature envelope when doing retainable
248 if (flags != msg->security)
250 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
251 mutt_free_body (&tmp_pgp_pbody->next);
257 msg->content = pbody;
265 int mutt_is_multipart_signed (BODY *b)
269 if (!b || !(b->type == TYPEMULTIPART) ||
270 !b->subtype || ascii_strcasecmp(b->subtype, "signed"))
273 if (!(p = mutt_get_parameter("protocol", b->parameter)))
276 if (!(ascii_strcasecmp (p, "multipart/mixed")))
279 if ((WithCrypto & APPLICATION_PGP)
280 && !(ascii_strcasecmp (p, "application/pgp-signature")))
283 if ((WithCrypto & APPLICATION_SMIME)
284 && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
286 if ((WithCrypto & APPLICATION_SMIME)
287 && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
294 int mutt_is_multipart_encrypted (BODY *b)
296 if ((WithCrypto & APPLICATION_PGP))
300 if (!b || b->type != TYPEMULTIPART ||
301 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
302 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
303 ascii_strcasecmp (p, "application/pgp-encrypted"))
313 int mutt_is_application_pgp (BODY *m)
318 if (m->type == TYPEAPPLICATION)
320 if (!ascii_strcasecmp (m->subtype, "pgp") || !ascii_strcasecmp (m->subtype, "x-pgp-message"))
322 if ((p = mutt_get_parameter ("x-action", m->parameter))
323 && (!ascii_strcasecmp (p, "sign") || !ascii_strcasecmp (p, "signclear")))
326 if ((p = mutt_get_parameter ("format", m->parameter)) &&
327 !ascii_strcasecmp (p, "keys-only"))
330 if(!t) t |= PGPENCRYPT; /* not necessarily correct, but... */
333 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
336 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
339 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0)
341 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
342 || (p = mutt_get_parameter ("x-action", m->parameter))
343 || (p = mutt_get_parameter ("action", m->parameter)))
344 && !ascii_strncasecmp ("pgp-sign", p, 8))
346 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
348 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
357 int mutt_is_application_smime (BODY *m)
365 if ((m->type & TYPEAPPLICATION) && m->subtype)
367 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
368 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
369 !ascii_strcasecmp (m->subtype, "pkcs7-mime"))
371 if ((t = mutt_get_parameter ("smime-type", m->parameter)))
373 if (!ascii_strcasecmp (t, "enveloped-data"))
375 else if (!ascii_strcasecmp (t, "signed-data"))
376 return (SMIMESIGN|SMIMEOPAQUE);
380 * Content-Description: S/MIME Encrypted Message
381 * instead of Content-Type parameter
383 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
387 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
390 t = mutt_get_parameter ("name", m->parameter);
392 if (!t) t = m->d_filename;
393 if (!t) t = m->filename;
397 mutt_message (_("S/MIME messages with no hints on content are unsupported."));
401 /* no .p7c, .p10 support yet. */
403 len = mutt_strlen (t) - 4;
404 if (len > 0 && *(t+len) == '.')
407 if (!ascii_strcasecmp ((t+len), "p7m"))
411 /* Not sure if this is the correct thing to do, but
412 it's required for compatibility with Outlook */
413 return (SMIMESIGN|SMIMEOPAQUE);
415 else if (!ascii_strcasecmp ((t+len), "p7s"))
416 return (SMIMESIGN|SMIMEOPAQUE);
428 int crypt_query (BODY *m)
438 if (m->type == TYPEAPPLICATION)
440 if ((WithCrypto & APPLICATION_PGP))
441 t |= mutt_is_application_pgp(m);
443 if ((WithCrypto & APPLICATION_SMIME))
445 t |= mutt_is_application_smime(m);
446 if (t && m->goodsig) t |= GOODSIGN;
447 if (t && m->badsig) t |= BADSIGN;
450 else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT)
452 t |= mutt_is_application_pgp (m);
457 if (m->type == TYPEMULTIPART)
459 t |= mutt_is_multipart_encrypted(m);
460 t |= mutt_is_multipart_signed (m);
466 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE)
471 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
472 w = 0; /* Bits set in any part */
474 for (p = m->parts; p; p = p->next)
479 t |= u | (w & ~GOODSIGN);
481 if ((w & GOODSIGN) && !(u & GOODSIGN))
491 int crypt_write_signed(BODY *a, STATE *s, const char *tempfile)
501 if (!(fp = safe_fopen (tempfile, "w")))
503 mutt_perror (tempfile);
507 fseek (s->fpin, a->hdr_offset, 0);
508 bytes = a->length + a->offset - a->hdr_offset;
512 if ((c = fgetc (s->fpin)) == EOF)
521 if (c == '\n' && !hadcr)
537 void convert_to_7bit (BODY *a)
544 if (a->type == TYPEMULTIPART)
546 if (a->encoding != ENC7BIT)
548 a->encoding = ENC7BIT;
549 convert_to_7bit(a->parts);
551 else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
552 convert_to_7bit (a->parts);
554 else if (a->type == TYPEMESSAGE &&
555 mutt_strcasecmp(a->subtype, "delivery-status"))
557 if(a->encoding != ENC7BIT)
558 mutt_message_to_7bit (a, NULL);
560 else if (a->encoding == ENC8BIT)
561 a->encoding = ENCQUOTEDPRINTABLE;
562 else if (a->encoding == ENCBINARY)
563 a->encoding = ENCBASE64;
564 else if (a->content && a->encoding != ENCBASE64 &&
565 (a->content->from || (a->content->space &&
566 option (OPTPGPSTRICTENC))))
567 a->encoding = ENCQUOTEDPRINTABLE;
575 void crypt_extract_keys_from_messages (HEADER * h)
578 char tempfname[_POSIX_PATH_MAX], *mbox;
585 mutt_mktemp (tempfname);
586 if (!(fpout = safe_fopen (tempfname, "w")))
588 mutt_perror (tempfname);
592 if ((WithCrypto & APPLICATION_PGP))
593 set_option (OPTDONTHANDLEPGPKEYS);
597 for (i = 0; i < Context->vcount; i++)
599 if (Context->hdrs[Context->v2r[i]]->tagged)
601 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
602 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
603 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->security))
609 if ((WithCrypto & APPLICATION_PGP)
610 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP))
612 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
613 M_CM_DECODE|M_CM_CHARCONV, 0);
616 mutt_endwin (_("Trying to extract PGP keys...\n"));
617 crypt_pgp_invoke_import (tempfname);
620 if ((WithCrypto & APPLICATION_SMIME)
621 && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME))
623 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
624 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
625 M_CM_NOHEADER|M_CM_DECODE_CRYPT
626 |M_CM_DECODE_SMIME, 0);
628 mutt_copy_message (fpout, Context,
629 Context->hdrs[Context->v2r[i]], 0, 0);
632 if (Context->hdrs[Context->v2r[i]]->env->from)
633 tmp = mutt_expand_aliases (h->env->from);
634 else if (Context->hdrs[Context->v2r[i]]->env->sender)
635 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
637 mbox = tmp ? tmp->mailbox : NULL;
640 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
641 crypt_smime_invoke_import (tempfname, mbox);
652 mutt_parse_mime_message (Context, h);
653 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security)))
655 if ((WithCrypto & APPLICATION_PGP)
656 && (h->security & APPLICATION_PGP))
658 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
660 mutt_endwin (_("Trying to extract PGP keys...\n"));
661 crypt_pgp_invoke_import (tempfname);
664 if ((WithCrypto & APPLICATION_SMIME)
665 && (h->security & APPLICATION_SMIME))
667 if (h->security & ENCRYPT)
668 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
670 |M_CM_DECODE_SMIME, 0);
672 mutt_copy_message (fpout, Context, h, 0, 0);
675 if (h->env->from) tmp = mutt_expand_aliases (h->env->from);
676 else if (h->env->sender) tmp = mutt_expand_aliases (h->env->sender);
677 mbox = tmp ? tmp->mailbox : NULL;
678 if (mbox) /* else ? */
680 mutt_message (_("Trying to extract S/MIME certificates...\n"));
681 crypt_smime_invoke_import (tempfname, mbox);
689 mutt_any_key_to_continue (NULL);
691 mutt_unlink (tempfname);
693 if ((WithCrypto & APPLICATION_PGP))
694 unset_option (OPTDONTHANDLEPGPKEYS);
699 int crypt_get_keys (HEADER *msg, char **keylist)
701 /* Do a quick check to make sure that we can find all of the encryption
702 * keys if the user has requested this service.
708 if ((WithCrypto & APPLICATION_PGP))
709 set_option (OPTPGPCHECKTRUST);
713 if (msg->security & ENCRYPT)
715 if ((WithCrypto & APPLICATION_PGP)
716 && (msg->security & APPLICATION_PGP))
718 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
719 msg->env->bcc)) == NULL)
721 unset_option (OPTPGPCHECKTRUST);
723 if ((WithCrypto & APPLICATION_SMIME)
724 && (msg->security & APPLICATION_SMIME))
726 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
727 msg->env->bcc)) == NULL)
737 static void crypt_fetch_signatures (BODY ***signatures, BODY *a, int *n)
742 for (; a; a = a->next)
744 if (a->type == TYPEMULTIPART)
745 crypt_fetch_signatures (signatures, a->parts, n);
749 safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
751 (*signatures)[(*n)++] = a;
758 * This routine verifies a "multipart/signed" body.
761 void mutt_signed_handler (BODY *a, STATE *s)
763 char tempfile[_POSIX_PATH_MAX];
765 int protocol_major = TYPEOTHER;
766 char *protocol_minor = NULL;
769 BODY **signatures = NULL;
777 protocol = mutt_get_parameter ("protocol", a->parameter);
780 /* extract the protocol information */
787 if ((protocol_minor = strchr (protocol, '/'))) protocol_minor++;
789 strfcpy (major, protocol, sizeof(major));
790 if((t = strchr(major, '/')))
793 protocol_major = mutt_check_mime_type (major);
796 /* consistency check */
798 if (!(a && a->next && a->next->type == protocol_major &&
799 !mutt_strcasecmp (a->next->subtype, protocol_minor)))
801 state_attach_puts (_("[-- Error: "
802 "Inconsistent multipart/signed structure! --]\n\n"),
804 mutt_body_handler (a, s);
809 if ((WithCrypto & APPLICATION_PGP)
810 && protocol_major == TYPEAPPLICATION
811 && !mutt_strcasecmp (protocol_minor, "pgp-signature"))
813 else if ((WithCrypto & APPLICATION_SMIME)
814 && protocol_major == TYPEAPPLICATION
815 && !(mutt_strcasecmp (protocol_minor, "x-pkcs7-signature")
816 && mutt_strcasecmp (protocol_minor, "pkcs7-signature")))
818 else if (protocol_major == TYPEMULTIPART
819 && !mutt_strcasecmp (protocol_minor, "mixed"))
823 state_printf (s, _("[-- Error: "
824 "Unknown multipart/signed protocol %s! --]\n\n"),
826 mutt_body_handler (a, s);
830 if (s->flags & M_DISPLAY)
833 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
837 mutt_mktemp (tempfile);
838 if (crypt_write_signed (a, s, tempfile) == 0)
840 for (i = 0; i < sigcnt; i++)
842 if ((WithCrypto & APPLICATION_PGP)
843 && signatures[i]->type == TYPEAPPLICATION
844 && !mutt_strcasecmp (signatures[i]->subtype, "pgp-signature"))
846 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
852 if ((WithCrypto & APPLICATION_SMIME)
853 && signatures[i]->type == TYPEAPPLICATION
854 && (!mutt_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
855 || !mutt_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
857 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
863 state_printf (s, _("[-- Warning: "
864 "We can't verify %s/%s signatures. --]\n\n"),
865 TYPE(signatures[i]), signatures[i]->subtype);
869 mutt_unlink (tempfile);
871 b->goodsig = goodsig;
872 b->badsig = !goodsig;
874 /* Now display the signed body */
875 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
881 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"), s);
884 mutt_body_handler (a, s);
886 if (s->flags & M_DISPLAY && sigcnt)
887 state_attach_puts (_("\n[-- End of signed data --]\n"), s);