2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
6 * Oliver Ehli <elmy@acm.org>
7 * Copyright (C) 2003 Werner Koch <wk@gnupg.org>
8 * Copyright (C) 2004 g10code GmbH
10 * This file is part of mutt-ng, see http://www.muttng.org/.
11 * It's licensed under the GNU General Public License,
12 * please see the file GPL in the top level source directory.
19 #include <lib-lib/str.h>
20 #include <lib-lib/file.h>
21 #include <lib-lib/ascii.h>
22 #include <lib-lib/mem.h>
23 #include <lib-lib/macros.h>
25 #include <lib-mime/mime.h>
27 #include <lib-ui/curses.h>
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h>
52 #ifdef HAVE_SYS_RESOURCE_H
53 # include <sys/resource.h>
57 /* print the current time to avoid spoofing of the signature output */
58 void crypt_current_time (STATE * s, const char *app_name)
61 char p[STRING], tmp[STRING];
63 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"),
73 NONULL (app_name), p);
74 state_attach_puts (tmp, s);
79 void crypt_forget_passphrase (void)
81 crypt_pgp_void_passphrase ();
82 crypt_smime_void_passphrase ();
83 mutt_message _("Passphrase(s) forgotten.");
87 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
89 static void disable_coredumps (void)
91 struct rlimit rl = { 0, 0 };
92 static short done = 0;
95 setrlimit (RLIMIT_CORE, &rl);
100 #endif /* HAVE_SETRLIMIT */
103 int crypt_valid_passphrase (int flags)
107 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
108 disable_coredumps ();
111 if (flags & APPLICATION_PGP)
112 ret = crypt_pgp_valid_passphrase ();
114 if (flags & APPLICATION_SMIME)
115 ret = crypt_smime_valid_passphrase ();
122 int mutt_protect (HEADER * msg, char *keylist)
124 BODY *pbody = NULL, *tmp_pbody = NULL;
125 BODY *tmp_smime_pbody = NULL;
126 BODY *tmp_pgp_pbody = NULL;
127 int flags = msg->security;
130 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
133 if ((msg->security & PGPINLINE) == PGPINLINE) {
134 /* they really want to send it inline... go for it */
136 mutt_endwin _("Invoking PGP...");
138 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
140 msg->content = pbody;
144 /* otherwise inline won't work...ask for revert */
146 query_quadoption (OPT_PGPMIMEAUTO,
148 ("Message can't be sent inline. Revert to using PGP/MIME?")))
150 mutt_error _("Mail not sent.");
155 /* go ahead with PGP/MIME */
161 tmp_smime_pbody = msg->content;
162 tmp_pgp_pbody = msg->content;
164 if (msg->security & SIGN) {
165 if (msg->security & APPLICATION_SMIME) {
166 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
168 pbody = tmp_smime_pbody = tmp_pbody;
171 if ((msg->security & APPLICATION_PGP)
172 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
173 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
177 pbody = tmp_pgp_pbody = tmp_pbody;
180 if ((msg->security & APPLICATION_SMIME)
181 && (msg->security & APPLICATION_PGP)) {
182 /* here comes the draft ;-) */
187 if (msg->security & ENCRYPT) {
188 if ((msg->security & APPLICATION_SMIME)) {
189 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
191 /* signed ? free it! */
194 /* free tmp_body if messages was signed AND encrypted ... */
195 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
196 /* detatch and dont't delete msg->content,
197 which tmp_smime_pbody->parts after signing. */
198 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
199 msg->content->next = NULL;
200 mutt_free_body (&tmp_smime_pbody);
205 if ((msg->security & APPLICATION_PGP)) {
206 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
209 /* did we perform a retainable signature? */
210 if (flags != msg->security) {
211 /* remove the outer multipart layer */
212 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
213 /* get rid of the signature */
214 mutt_free_body (&tmp_pgp_pbody->next);
220 /* destroy temporary signature envelope when doing retainable
224 if (flags != msg->security) {
225 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
226 mutt_free_body (&tmp_pgp_pbody->next);
232 msg->content = pbody;
240 int mutt_is_multipart_signed (BODY * b)
244 if (!b || !(b->type == TYPEMULTIPART) ||
245 !b->subtype || ascii_strcasecmp (b->subtype, "signed"))
248 if (!(p = mutt_get_parameter ("protocol", b->parameter)))
251 if (!(ascii_strcasecmp (p, "multipart/mixed")))
254 if (!(ascii_strcasecmp (p, "application/pgp-signature")))
257 if (!(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
259 if (!(ascii_strcasecmp (p, "application/pkcs7-signature")))
266 int mutt_is_multipart_encrypted (BODY * b)
270 if (!b || b->type != TYPEMULTIPART ||
271 !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
272 !(p = mutt_get_parameter ("protocol", b->parameter)) ||
273 ascii_strcasecmp (p, "application/pgp-encrypted"))
280 int mutt_is_application_pgp (BODY * m)
285 if (m->type == TYPEAPPLICATION) {
286 if (!ascii_strcasecmp (m->subtype, "pgp")
287 || !ascii_strcasecmp (m->subtype, "x-pgp-message")) {
288 if ((p = mutt_get_parameter ("x-action", m->parameter))
289 && (!ascii_strcasecmp (p, "sign")
290 || !ascii_strcasecmp (p, "signclear")))
293 if ((p = mutt_get_parameter ("format", m->parameter)) &&
294 !ascii_strcasecmp (p, "keys-only"))
298 t |= PGPENCRYPT; /* not necessarily correct, but... */
301 if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
304 if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
307 else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0) {
308 if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
309 || (p = mutt_get_parameter ("x-action", m->parameter))
310 || (p = mutt_get_parameter ("action", m->parameter)))
311 && !ascii_strncasecmp ("pgp-sign", p, 8))
313 else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
315 else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
324 int mutt_is_application_smime (BODY * m)
327 int len, complain = 0;
332 if ((m->type & TYPEAPPLICATION) && m->subtype) {
333 /* S/MIME MIME types don't need x- anymore, see RFC2311 */
334 if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
335 !ascii_strcasecmp (m->subtype, "pkcs7-mime")) {
336 if ((t = mutt_get_parameter ("smime-type", m->parameter))) {
337 if (!ascii_strcasecmp (t, "enveloped-data"))
339 else if (!ascii_strcasecmp (t, "signed-data"))
340 return (SMIMESIGN | SMIMEOPAQUE);
345 * Content-Description: S/MIME Encrypted Message
346 * instead of Content-Type parameter
348 if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
352 else if (ascii_strcasecmp (m->subtype, "octet-stream"))
355 t = mutt_get_parameter ("name", m->parameter);
364 ("S/MIME messages with no hints on content are unsupported."));
368 /* no .p7c, .p10 support yet. */
370 len = m_strlen(t) - 4;
371 if (len > 0 && *(t + len) == '.') {
373 if (!ascii_strcasecmp ((t + len), "p7m"))
377 /* Not sure if this is the correct thing to do, but
378 it's required for compatibility with Outlook */
379 return (SMIMESIGN | SMIMEOPAQUE);
381 else if (!ascii_strcasecmp ((t + len), "p7s"))
382 return (SMIMESIGN | SMIMEOPAQUE);
394 int crypt_query (BODY * m)
401 if (m->type == TYPEAPPLICATION) {
402 t |= mutt_is_application_pgp (m);
404 t |= mutt_is_application_smime (m);
410 else if (m->type == TYPETEXT) {
411 t |= mutt_is_application_pgp (m);
416 if (m->type == TYPEMULTIPART) {
417 t |= mutt_is_multipart_encrypted (m);
418 t |= mutt_is_multipart_signed (m);
424 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
428 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
429 w = 0; /* Bits set in any part */
431 for (p = m->parts; p; p = p->next) {
436 t |= u | (w & ~GOODSIGN);
438 if ((w & GOODSIGN) && !(u & GOODSIGN))
448 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
455 if (!(fp = safe_fopen (tempfile, "w"))) {
456 mutt_perror (tempfile);
460 fseeko (s->fpin, a->hdr_offset, 0);
461 bytes = a->length + a->offset - a->hdr_offset;
464 if ((c = fgetc (s->fpin)) == EOF)
472 if (c == '\n' && !hadcr)
488 void convert_to_7bit (BODY * a)
491 if (a->type == TYPEMULTIPART) {
492 if (a->encoding != ENC7BIT) {
493 a->encoding = ENC7BIT;
494 convert_to_7bit (a->parts);
496 else if (option (OPTPGPSTRICTENC))
497 convert_to_7bit (a->parts);
499 else if (a->type == TYPEMESSAGE &&
500 m_strcasecmp(a->subtype, "delivery-status")) {
501 if (a->encoding != ENC7BIT)
502 mutt_message_to_7bit (a, NULL);
504 else if (a->encoding == ENC8BIT)
505 a->encoding = ENCQUOTEDPRINTABLE;
506 else if (a->encoding == ENCBINARY)
507 a->encoding = ENCBASE64;
508 else if (a->content && a->encoding != ENCBASE64 &&
509 (a->content->from || (a->content->space &&
510 option (OPTPGPSTRICTENC))))
511 a->encoding = ENCQUOTEDPRINTABLE;
519 void crypt_extract_keys_from_messages (HEADER * h)
522 char tempfname[_POSIX_PATH_MAX], *mbox;
523 address_t *tmp = NULL;
526 mutt_mktemp (tempfname);
527 if (!(fpout = safe_fopen (tempfname, "w"))) {
528 mutt_perror (tempfname);
532 set_option (OPTDONTHANDLEPGPKEYS);
535 for (i = 0; i < Context->vcount; i++) {
536 if (Context->hdrs[Context->v2r[i]]->tagged) {
537 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
538 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
539 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
545 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP) {
546 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
547 M_CM_DECODE | M_CM_CHARCONV, 0);
550 mutt_endwin (_("Trying to extract PGP keys...\n"));
551 crypt_pgp_invoke_import (tempfname);
554 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME) {
555 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
556 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
557 M_CM_NOHEADER | M_CM_DECODE_CRYPT
558 | M_CM_DECODE_SMIME, 0);
560 mutt_copy_message (fpout, Context,
561 Context->hdrs[Context->v2r[i]], 0, 0);
564 if (Context->hdrs[Context->v2r[i]]->env->from)
565 tmp = mutt_expand_aliases (h->env->from);
566 else if (Context->hdrs[Context->v2r[i]]->env->sender)
567 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
569 mbox = tmp ? tmp->mailbox : NULL;
571 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
572 crypt_smime_invoke_import (tempfname, mbox);
582 mutt_parse_mime_message (Context, h);
583 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
584 if (h->security & APPLICATION_PGP) {
585 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
587 mutt_endwin (_("Trying to extract PGP keys...\n"));
588 crypt_pgp_invoke_import (tempfname);
591 if (h->security & APPLICATION_SMIME) {
592 if (h->security & ENCRYPT)
593 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
594 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
596 mutt_copy_message (fpout, Context, h, 0, 0);
600 tmp = mutt_expand_aliases (h->env->from);
601 else if (h->env->sender)
602 tmp = mutt_expand_aliases (h->env->sender);
603 mbox = tmp ? tmp->mailbox : NULL;
604 if (mbox) { /* else ? */
605 mutt_message (_("Trying to extract S/MIME certificates...\n"));
606 crypt_smime_invoke_import (tempfname, mbox);
614 mutt_any_key_to_continue (NULL);
616 mutt_unlink (tempfname);
618 unset_option (OPTDONTHANDLEPGPKEYS);
623 int crypt_get_keys (HEADER * msg, char **keylist)
625 /* Do a quick check to make sure that we can find all of the encryption
626 * keys if the user has requested this service.
629 set_option (OPTPGPCHECKTRUST);
633 if (msg->security & ENCRYPT) {
634 if (msg->security & APPLICATION_PGP) {
635 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
636 msg->env->bcc)) == NULL)
638 unset_option (OPTPGPCHECKTRUST);
640 if (msg->security & APPLICATION_SMIME) {
641 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
642 msg->env->bcc)) == NULL)
652 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
654 for (; a; a = a->next) {
655 if (a->type == TYPEMULTIPART)
656 crypt_fetch_signatures (signatures, a->parts, n);
659 p_realloc(signatures, *n + 6);
661 (*signatures)[(*n)++] = a;
668 * This routine verifies a "multipart/signed" body.
671 int mutt_signed_handler (BODY * a, STATE * s)
673 char tempfile[_POSIX_PATH_MAX];
675 int protocol_major = TYPEOTHER;
676 char *protocol_minor = NULL;
679 BODY **signatures = NULL;
685 protocol = mutt_get_parameter ("protocol", a->parameter);
688 /* extract the protocol information */
694 if ((protocol_minor = strchr (protocol, '/')))
697 m_strcpy(major, sizeof(major), protocol);
698 if ((t = strchr (major, '/')))
701 protocol_major = mutt_check_mime_type (major);
704 /* consistency check */
706 if (!(a && a->next && a->next->type == protocol_major &&
707 !m_strcasecmp(a->next->subtype, protocol_minor))) {
708 state_attach_puts (_("[-- Error: "
709 "Inconsistent multipart/signed structure! --]\n\n"),
711 return mutt_body_handler (a, s);
715 if (protocol_major == TYPEAPPLICATION
716 && !m_strcasecmp(protocol_minor, "pgp-signature"));
717 else if (protocol_major == TYPEAPPLICATION
718 && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
719 && m_strcasecmp(protocol_minor, "pkcs7-signature")));
720 else if (protocol_major == TYPEMULTIPART
721 && !m_strcasecmp(protocol_minor, "mixed"));
723 state_printf (s, _("[-- Error: "
724 "Unknown multipart/signed protocol %s! --]\n\n"),
726 return mutt_body_handler (a, s);
729 if (s->flags & M_DISPLAY) {
731 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
734 mutt_mktemp (tempfile);
735 if (crypt_write_signed (a, s, tempfile) == 0) {
736 for (i = 0; i < sigcnt; i++) {
737 if (signatures[i]->type == TYPEAPPLICATION
738 && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
739 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
745 if (signatures[i]->type == TYPEAPPLICATION
746 && (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
747 || !m_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
749 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
755 state_printf (s, _("[-- Warning: "
756 "We can't verify %s/%s signatures. --]\n\n"),
757 TYPE (signatures[i]), signatures[i]->subtype);
761 mutt_unlink (tempfile);
763 b->goodsig = goodsig;
764 b->badsig = !goodsig;
766 /* Now display the signed body */
767 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
770 p_delete(&signatures);
773 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
777 rc = mutt_body_handler (a, s);
779 if (s->flags & M_DISPLAY && sigcnt)
780 state_attach_puts (_("\n[-- End of signed data --]\n"), s);