2 * Copyright (C) 1996-7,2000 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1998-2005 Thomas Roessler <roessler@does-not-exist.org>
4 * Copyright (C) 2004 g10 Code GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * This file contains all of the PGP routines necessary to sign, encrypt,
23 * verify and decrypt PGP messages in either the new PGP/MIME format, or
24 * in the older Application/Pgp format. It also contains some code to
25 * cache the user's passphrase for repeat use when decrypting or signing
34 #include "mutt_curses.h"
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
55 #ifdef HAVE_SYS_RESOURCE_H
56 # include <sys/resource.h>
59 #ifdef CRYPT_BACKEND_CLASSIC_PGP
61 #include "mutt_crypt.h"
62 #include "mutt_menu.h"
65 char PgpPass[LONG_STRING];
66 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
68 void pgp_void_passphrase (void)
70 memset (PgpPass, 0, sizeof (PgpPass));
74 int pgp_valid_passphrase (void)
76 time_t now = time (NULL);
78 if (pgp_use_gpg_agent())
81 return 1; /* handled by gpg-agent */
85 /* Use cached copy. */
88 pgp_void_passphrase ();
90 if (mutt_get_password (_("Enter PGP passphrase:"), PgpPass, sizeof (PgpPass)) == 0)
92 PgpExptime = time (NULL) + PgpTimeout;
101 void pgp_forget_passphrase (void)
103 pgp_void_passphrase ();
104 mutt_message _("PGP passphrase forgotten.");
107 int pgp_use_gpg_agent (void)
111 if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
114 if ((tty = ttyname(0)))
115 setenv("GPG_TTY", tty, 0);
120 char *pgp_keyid(pgp_key_t k)
122 if((k->flags & KEYFLAG_SUBKEY) && k->parent && option(OPTPGPIGNORESUB))
125 return _pgp_keyid(k);
128 char *_pgp_keyid(pgp_key_t k)
130 if(option(OPTPGPLONGIDS))
133 return (k->keyid + 8);
136 /* ----------------------------------------------------------------------------
137 * Routines for handing PGP input.
142 /* Copy PGP output messages and look for signs of a good signature */
144 static int pgp_copy_checksig (FILE *fpin, FILE *fpout)
148 if (PgpGoodSign.pattern)
154 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL)
156 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0)
158 dprint (2, (debugfile, "pgp_copy_checksig: \"%s\" matches regexp.\n",
163 dprint (2, (debugfile, "pgp_copy_checksig: \"%s\" doesn't match regexp.\n",
166 if (strncmp (line, "[GNUPG:] ", 9) == 0)
175 dprint (2, (debugfile, "pgp_copy_checksig: No pattern.\n"));
176 mutt_copy_stream (fpin, fpout);
184 * Copy a clearsigned message, and strip the signature and PGP's
187 * XXX - charset handling: We assume that it is safe to do
188 * character set decoding first, dash decoding second here, while
189 * we do it the other way around in the main handler.
191 * (Note that we aren't worse than Outlook &c in this, and also
192 * note that we can successfully handle anything produced by any
193 * existing versions of mutt.)
196 static void pgp_copy_clearsigned (FILE *fpin, STATE *s, char *charset)
198 char buf[HUGE_STRING];
199 short complete, armor_header;
205 /* fromcode comes from the MIME Content-Type charset label. It might
206 * be a wrong label, so we want the ability to do corrections via
207 * charset-hooks. Therefore we set flags to M_ICONV_HOOK_FROM.
209 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
211 for (complete = 1, armor_header = 1;
212 fgetconvs (buf, sizeof (buf), fc) != NULL;
213 complete = strchr (buf, '\n') != NULL)
222 if (mutt_strcmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
227 char *p = mutt_skip_whitespace (buf);
234 state_puts (s->prefix, s);
236 if (buf[0] == '-' && buf[1] == ' ')
237 state_puts (buf + 2, s);
242 fgetconv_close (&fc);
246 /* Support for the Application/PGP Content Type. */
248 int pgp_application_pgp_handler (BODY *m, STATE *s)
250 int could_not_decrypt = 0;
251 int needpass = -1, pgp_keyblock = 0;
252 int clearsign = 0, rv, rc;
253 int c = 1; /* silence GCC warning */
256 LOFF_T last_pos, offset;
257 char buf[HUGE_STRING];
258 char outfile[_POSIX_PATH_MAX];
259 char tmpfname[_POSIX_PATH_MAX];
260 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
264 short maybe_goodsig = 1;
265 short have_any_sigs = 0;
267 char body_charset[STRING];
268 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
270 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
272 fseeko (s->fpin, m->offset, 0);
273 last_pos = m->offset;
275 for (bytes = m->length; bytes > 0;)
277 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
280 offset = ftello (s->fpin);
281 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
284 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0)
287 start_pos = last_pos;
289 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
291 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
296 else if (!mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15))
303 /* XXX - we may wish to recode here */
305 state_puts (s->prefix, s);
310 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
312 /* Copy PGP material to temporary file */
313 mutt_mktemp (tmpfname);
314 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL)
316 mutt_perror (tmpfname);
321 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL)
323 offset = ftello (s->fpin);
324 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
329 if ((needpass && mutt_strcmp ("-----END PGP MESSAGE-----\n", buf) == 0) ||
331 && (mutt_strcmp ("-----END PGP SIGNATURE-----\n", buf) == 0
332 || mutt_strcmp ("-----END PGP PUBLIC KEY BLOCK-----\n",buf) == 0)))
336 /* leave tmpfp open in case we still need it - but flush it! */
339 /* Invoke PGP if needed */
340 if (!clearsign || (s->flags & M_VERIFY))
342 mutt_mktemp (outfile);
343 if ((pgpout = safe_fopen (outfile, "w+")) == NULL)
345 mutt_perror (tmpfname);
349 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
350 fileno (pgpout), -1, tmpfname,
353 safe_fclose (&pgpout);
357 state_attach_puts (_("[-- Error: unable to create PGP subprocess! --]\n"), s);
359 else /* PGP started successfully */
363 if (!pgp_valid_passphrase ()) pgp_void_passphrase();
364 if (pgp_use_gpg_agent())
366 fprintf (pgpin, "%s\n", PgpPass);
369 safe_fclose (&pgpin);
371 if (s->flags & M_DISPLAY)
373 crypt_current_time (s, "PGP");
374 rc = pgp_copy_checksig (pgperr, s->fpout);
377 safe_fclose (&pgperr);
378 rv = mutt_wait_filter (thepid);
380 if (s->flags & M_DISPLAY)
382 if (rc == 0) have_any_sigs = 1;
385 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
386 * Sig _is_ correct if
387 * gpg_good_sign="" && pgp_decode_command returned 0
389 if (rc == -1 || rv) maybe_goodsig = 0;
391 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
395 /* treat empty result as sign of failure */
396 /* TODO: maybe on failure mutt should include the original undecoded text. */
403 if (!clearsign && (!pgpout || c == EOF))
405 could_not_decrypt = 1;
406 pgp_void_passphrase ();
409 if (could_not_decrypt && !(s->flags & M_DISPLAY))
411 mutt_error _("Could not decrypt PGP message");
419 * Now, copy cleartext to the screen. NOTE - we expect that PGP
420 * outputs utf-8 cleartext. This may not always be true, but it
421 * seems to be a reasonable guess.
424 if(s->flags & M_DISPLAY)
427 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
428 else if (pgp_keyblock)
429 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
431 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
438 pgp_copy_clearsigned (tmpfp, s, body_charset);
445 state_set_prefix (s);
446 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
447 while ((c = fgetconv (fc)) != EOF)
448 state_prefix_putc (c, s);
449 fgetconv_close (&fc);
452 if (s->flags & M_DISPLAY)
454 state_putc ('\n', s);
457 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
458 if (could_not_decrypt)
459 mutt_error _("Could not decrypt PGP message");
461 mutt_message _("PGP message successfully decrypted.");
463 else if (pgp_keyblock)
464 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
466 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
472 /* why would we want to display this at all? */
473 /* XXX - we may wish to recode here */
475 state_puts (s->prefix, s);
484 m->goodsig = (maybe_goodsig && have_any_sigs);
488 safe_fclose (&tmpfp);
489 mutt_unlink (tmpfname);
493 safe_fclose (&pgpout);
494 mutt_unlink (outfile);
499 state_attach_puts (_("[-- Error: could not find beginning of PGP message! --]\n\n"), s);
506 static int pgp_check_traditional_one_body (FILE *fp, BODY *b, int tagged_only)
508 char tempfile[_POSIX_PATH_MAX];
509 char buf[HUGE_STRING];
516 if (b->type != TYPETEXT)
519 if (tagged_only && !b->tagged)
522 mutt_mktemp (tempfile);
523 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0)
529 if ((tfp = fopen (tempfile, "r")) == NULL)
535 while (fgets (buf, sizeof (buf), tfp))
537 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0)
539 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
541 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
543 else if (mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
550 if (!enc && !sgn && !key)
553 /* fix the content type */
555 mutt_set_parameter ("format", "fixed", &b->parameter);
557 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
559 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
561 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
566 int pgp_check_traditional (FILE *fp, BODY *b, int tagged_only)
570 for (; b; b = b->next)
572 if (is_multipart (b))
573 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
574 else if (b->type == TYPETEXT)
576 if ((r = mutt_is_application_pgp (b)))
579 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
590 int pgp_verify_one (BODY *sigbdy, STATE *s, const char *tempfile)
592 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
593 FILE *fp, *pgpout, *pgperr;
598 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
600 if(!(fp = safe_fopen (sigfile, "w")))
602 mutt_perror(sigfile);
606 fseeko (s->fpin, sigbdy->offset, 0);
607 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
610 mutt_mktemp(pgperrfile);
611 if(!(pgperr = safe_fopen(pgperrfile, "w+")))
613 mutt_perror(pgperrfile);
618 crypt_current_time (s, "PGP");
620 if((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
621 -1, -1, fileno(pgperr),
622 tempfile, sigfile)) != -1)
624 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
628 safe_fclose (&pgpout);
632 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
635 if ((rv = mutt_wait_filter (thepid)))
638 dprint (1, (debugfile, "pgp_verify_one: mutt_wait_filter returned %d.\n", rv));
641 safe_fclose (&pgperr);
643 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
645 mutt_unlink (sigfile);
646 mutt_unlink (pgperrfile);
648 dprint (1, (debugfile, "pgp_verify_one: returning %d.\n", badsig));
654 /* Extract pgp public keys from messages or attachments */
656 void pgp_extract_keys_from_messages (HEADER *h)
659 char tempfname[_POSIX_PATH_MAX];
664 mutt_parse_mime_message (Context, h);
665 if(h->security & PGPENCRYPT && !pgp_valid_passphrase ())
669 mutt_mktemp (tempfname);
670 if (!(fpout = safe_fopen (tempfname, "w")))
672 mutt_perror (tempfname);
676 set_option (OPTDONTHANDLEPGPKEYS);
680 for (i = 0; i < Context->vcount; i++)
682 if (Context->hdrs[Context->v2r[i]]->tagged)
684 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
685 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
686 && !pgp_valid_passphrase())
691 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
692 M_CM_DECODE|M_CM_CHARCONV, 0);
698 mutt_parse_mime_message (Context, h);
699 if (h->security & PGPENCRYPT && !pgp_valid_passphrase())
704 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
709 pgp_invoke_import (tempfname);
710 mutt_any_key_to_continue (NULL);
714 mutt_unlink (tempfname);
715 unset_option (OPTDONTHANDLEPGPKEYS);
719 static void pgp_extract_keys_from_attachment (FILE *fp, BODY *top)
723 char tempfname[_POSIX_PATH_MAX];
725 mutt_mktemp (tempfname);
726 if (!(tempfp = safe_fopen (tempfname, "w")))
728 mutt_perror (tempfname);
732 memset (&s, 0, sizeof (STATE));
737 mutt_body_handler (top, &s);
741 pgp_invoke_import (tempfname);
742 mutt_any_key_to_continue (NULL);
744 mutt_unlink (tempfname);
747 void pgp_extract_keys_from_attachment_list (FILE *fp, int tag, BODY *top)
751 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
756 set_option(OPTDONTHANDLEPGPKEYS);
758 for(; top; top = top->next)
760 if(!tag || top->tagged)
761 pgp_extract_keys_from_attachment (fp, top);
767 unset_option(OPTDONTHANDLEPGPKEYS);
770 BODY *pgp_decrypt_part (BODY *a, STATE *s, FILE *fpout, BODY *p)
772 char buf[LONG_STRING];
773 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
777 char pgperrfile[_POSIX_PATH_MAX];
778 char pgptmpfile[_POSIX_PATH_MAX];
782 mutt_mktemp (pgperrfile);
783 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
785 mutt_perror (pgperrfile);
790 mutt_mktemp (pgptmpfile);
791 if((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL)
793 mutt_perror (pgptmpfile);
798 /* Position the stream at the beginning of the body, and send the data to
799 * the temporary file.
802 fseeko (s->fpin, a->offset, 0);
803 mutt_copy_bytes (s->fpin, pgptmp, a->length);
806 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
807 fileno (pgperr), pgptmpfile)) == -1)
811 if (s->flags & M_DISPLAY)
812 state_attach_puts (_("[-- Error: could not create a PGP subprocess! --]\n\n"), s);
816 /* send the PGP passphrase to the subprocess. Never do this if the
817 agent is active, because this might lead to a passphrase send as
819 if (!pgp_use_gpg_agent())
820 fputs (PgpPass, pgpin);
824 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
825 * read_mime_header has a hard time parsing the message.
827 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL)
829 len = mutt_strlen (buf);
830 if (len > 1 && buf[len - 2] == '\r')
831 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
836 rv = mutt_wait_filter (thepid);
837 mutt_unlink(pgptmpfile);
839 if (s->flags & M_DISPLAY)
843 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
847 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
854 if (pgp_use_gpg_agent())
855 mutt_need_hard_redraw ();
857 if (fgetc (fpout) == EOF)
859 mutt_error _("Decryption failed");
860 pgp_void_passphrase ();
866 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL)
869 * Need to set the length of this body part.
871 fstat (fileno (fpout), &info);
872 tattach->length = info.st_size - tattach->offset;
874 /* See if we need to recurse on this MIME part. */
876 mutt_parse_part (fpout, tattach);
882 int pgp_decrypt_mime (FILE *fpin, FILE **fpout, BODY *b, BODY **cur)
884 char tempfile[_POSIX_PATH_MAX];
888 if(!mutt_is_multipart_encrypted(b))
891 if(!b->parts || !b->parts->next)
896 memset (&s, 0, sizeof (s));
898 mutt_mktemp (tempfile);
899 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL)
901 mutt_perror (tempfile);
906 *cur = pgp_decrypt_part (b, &s, *fpout, p);
916 int pgp_encrypted_handler (BODY *a, STATE *s)
918 char tempfile[_POSIX_PATH_MAX];
925 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
926 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
927 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
928 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0)
930 if (s->flags & M_DISPLAY)
931 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"), s);
936 * Move forward to the application/pgp-encrypted body.
940 mutt_mktemp (tempfile);
941 if ((fpout = safe_fopen (tempfile, "w+")) == NULL)
943 if (s->flags & M_DISPLAY)
944 state_attach_puts (_("[-- Error: could not create temporary file! --]\n"), s);
948 if (s->flags & M_DISPLAY) crypt_current_time (s, "PGP");
950 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL)
952 if (s->flags & M_DISPLAY)
953 state_attach_puts (_("[-- The following data is PGP/MIME encrypted --]\n\n"), s);
957 rc = mutt_body_handler (tattach, s);
961 * if a multipart/signed is the _only_ sub-part of a
962 * multipart/encrypted, cache signature verification
967 if (mutt_is_multipart_signed (tattach) && !tattach->next)
968 p->goodsig |= tattach->goodsig;
970 if (s->flags & M_DISPLAY)
972 state_puts ("\n", s);
973 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
976 mutt_free_body (&tattach);
977 /* clear 'Invoking...' message, since there's no error */
978 mutt_message _("PGP message successfully decrypted.");
982 mutt_error _("Could not decrypt PGP message");
983 /* void the passphrase, even if it's not necessarily the problem */
984 pgp_void_passphrase ();
989 mutt_unlink(tempfile);
994 /* ----------------------------------------------------------------------------
995 * Routines for sending PGP/MIME messages.
999 BODY *pgp_sign_message (BODY *a)
1002 char buffer[LONG_STRING];
1003 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
1004 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
1009 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
1011 mutt_mktemp (sigfile);
1012 if ((fp = safe_fopen (sigfile, "w")) == NULL)
1017 mutt_mktemp (signedfile);
1018 if ((sfp = safe_fopen(signedfile, "w")) == NULL)
1020 mutt_perror(signedfile);
1026 mutt_write_mime_header (a, sfp);
1028 mutt_write_mime_body (a, sfp);
1031 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
1032 -1, -1, -1, signedfile)) == -1)
1034 mutt_perror _("Can't open PGP subprocess!");
1041 if (!pgp_use_gpg_agent())
1042 fputs(PgpPass, pgpin);
1047 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
1048 * recommended for future releases of PGP.
1050 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL)
1052 if (mutt_strcmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
1053 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
1054 else if (mutt_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
1055 fputs ("-----END PGP SIGNATURE-----\n", fp);
1058 empty = 0; /* got some output, so we're ok */
1061 /* check for errors from PGP */
1063 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL)
1066 fputs (buffer, stdout);
1069 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1074 unlink (signedfile);
1076 if (fclose (fp) != 0)
1078 mutt_perror ("fclose");
1084 mutt_any_key_to_continue (NULL);
1088 /* most likely error is a bad passphrase, so automatically forget it */
1089 pgp_void_passphrase ();
1090 return (NULL); /* fatal error while signing */
1093 t = mutt_new_body ();
1094 t->type = TYPEMULTIPART;
1095 t->subtype = safe_strdup ("signed");
1096 t->encoding = ENC7BIT;
1098 t->disposition = DISPINLINE;
1100 mutt_generate_boundary (&t->parameter);
1101 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1102 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1107 t->parts->next = mutt_new_body ();
1109 t->type = TYPEAPPLICATION;
1110 t->subtype = safe_strdup ("pgp-signature");
1111 t->filename = safe_strdup (sigfile);
1113 t->disposition = DISPINLINE;
1114 t->encoding = ENC7BIT;
1115 t->unlink = 1; /* ok to remove this file after sending. */
1120 static short is_numerical_keyid (const char *s)
1122 /* or should we require the "0x"? */
1123 if (strncmp (s, "0x", 2) == 0)
1128 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1134 /* This routine attempts to find the keyids of the recipients of a message.
1135 * It returns NULL if any of the keys can not be found.
1137 char *pgp_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
1139 char *keyID, *keylist = NULL, *t;
1140 size_t keylist_size = 0;
1141 size_t keylist_used = 0;
1142 ADDRESS *tmp = NULL, *addr = NULL;
1143 ADDRESS **last = &tmp;
1146 pgp_key_t k_info = NULL, key = NULL;
1148 const char *fqdn = mutt_fqdn (1);
1150 for (i = 0; i < 3; i++)
1154 case 0: p = to; break;
1155 case 1: p = cc; break;
1156 case 2: p = bcc; break;
1160 *last = rfc822_cpy_adr (p);
1162 last = &((*last)->next);
1166 rfc822_qualify (tmp, fqdn);
1168 tmp = mutt_remove_duplicates (tmp);
1170 for (p = tmp; p ; p = p->next)
1172 char buf[LONG_STRING];
1177 if ((keyID = mutt_crypt_hook (p)) != NULL)
1180 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID, p->mailbox);
1181 if ((r = mutt_yesorno (buf, M_YES)) == M_YES)
1183 if (is_numerical_keyid (keyID))
1185 if (strncmp (keyID, "0x", 2) == 0)
1187 goto bypass_selection; /* you don't see this. */
1190 /* check for e-mail address */
1191 if ((t = strchr (keyID, '@')) &&
1192 (addr = rfc822_parse_adrlist (NULL, keyID)))
1194 if (fqdn) rfc822_qualify (addr, fqdn);
1198 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1203 rfc822_free_address (&tmp);
1204 rfc822_free_address (&addr);
1210 pgp_invoke_getkeys (q);
1212 if (k_info == NULL && (k_info = pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL)
1214 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1216 if ((key = pgp_ask_for_key (buf, q->mailbox,
1217 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL)
1220 rfc822_free_address (&tmp);
1221 rfc822_free_address (&addr);
1228 keyID = pgp_keyid (key);
1231 keylist_size += mutt_strlen (keyID) + 4;
1232 safe_realloc (&keylist, keylist_size);
1233 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1235 keylist_used = mutt_strlen (keylist);
1237 pgp_free_key (&key);
1238 rfc822_free_address (&addr);
1241 rfc822_free_address (&tmp);
1245 /* Warning: "a" is no longer freed in this routine, you need
1246 * to free it later. This is necessary for $fcc_attach. */
1248 BODY *pgp_encrypt_message (BODY *a, char *keylist, int sign)
1250 char buf[LONG_STRING];
1251 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1252 char pgpinfile[_POSIX_PATH_MAX];
1253 FILE *pgpin, *pgperr, *fpout, *fptmp;
1259 mutt_mktemp (tempfile);
1260 if ((fpout = safe_fopen (tempfile, "w+")) == NULL)
1262 mutt_perror (tempfile);
1266 mutt_mktemp (pgperrfile);
1267 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
1269 mutt_perror (pgperrfile);
1274 unlink (pgperrfile);
1276 mutt_mktemp(pgpinfile);
1277 if((fptmp = safe_fopen(pgpinfile, "w")) == NULL)
1279 mutt_perror(pgpinfile);
1287 convert_to_7bit (a);
1289 mutt_write_mime_header (a, fptmp);
1290 fputc ('\n', fptmp);
1291 mutt_write_mime_body (a, fptmp);
1294 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1295 fileno (fpout), fileno (pgperr),
1296 pgpinfile, keylist, sign)) == -1)
1305 if (!pgp_use_gpg_agent())
1306 fputs (PgpPass, pgpin);
1307 fputc ('\n', pgpin);
1311 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1319 empty = (fgetc (fpout) == EOF);
1324 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL)
1327 fputs (buf, stdout);
1331 /* pause if there is any error output from PGP */
1333 mutt_any_key_to_continue (NULL);
1337 /* fatal error while trying to encrypt message */
1339 pgp_void_passphrase (); /* just in case */
1344 t = mutt_new_body ();
1345 t->type = TYPEMULTIPART;
1346 t->subtype = safe_strdup ("encrypted");
1347 t->encoding = ENC7BIT;
1349 t->disposition = DISPINLINE;
1351 mutt_generate_boundary(&t->parameter);
1352 mutt_set_parameter("protocol", "application/pgp-encrypted", &t->parameter);
1354 t->parts = mutt_new_body ();
1355 t->parts->type = TYPEAPPLICATION;
1356 t->parts->subtype = safe_strdup ("pgp-encrypted");
1357 t->parts->encoding = ENC7BIT;
1359 t->parts->next = mutt_new_body ();
1360 t->parts->next->type = TYPEAPPLICATION;
1361 t->parts->next->subtype = safe_strdup ("octet-stream");
1362 t->parts->next->encoding = ENC7BIT;
1363 t->parts->next->filename = safe_strdup (tempfile);
1364 t->parts->next->use_disp = 1;
1365 t->parts->next->disposition = DISPINLINE;
1366 t->parts->next->unlink = 1; /* delete after sending the message */
1367 t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime can save */
1372 BODY *pgp_traditional_encryptsign (BODY *a, int flags, char *keylist)
1376 char pgpoutfile[_POSIX_PATH_MAX];
1377 char pgperrfile[_POSIX_PATH_MAX];
1378 char pgpinfile[_POSIX_PATH_MAX];
1380 char body_charset[STRING];
1382 const char *send_charset;
1384 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1394 if (a->type != TYPETEXT)
1396 if (ascii_strcasecmp (a->subtype, "plain"))
1399 if ((fp = fopen (a->filename, "r")) == NULL)
1401 mutt_perror (a->filename);
1405 mutt_mktemp (pgpinfile);
1406 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL)
1408 mutt_perror (pgpinfile);
1413 /* The following code is really correct: If noconv is set,
1414 * a's charset parameter contains the on-disk character set, and
1415 * we have to convert from that to utf-8. If noconv is not set,
1416 * we have to convert from $charset to utf-8.
1419 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1421 from_charset = body_charset;
1423 from_charset = Charset;
1425 if (!mutt_is_us_ascii (body_charset))
1430 if (flags & ENCRYPT)
1431 send_charset = "us-ascii";
1433 send_charset = "utf-8";
1435 /* fromcode is assumed to be correct: we set flags to 0 */
1436 fc = fgetconv_open (fp, from_charset, "utf-8", 0);
1437 while ((c = fgetconv (fc)) != EOF)
1440 fgetconv_close (&fc);
1444 send_charset = "us-ascii";
1445 mutt_copy_stream (fp, pgpin);
1450 mutt_mktemp (pgpoutfile);
1451 mutt_mktemp (pgperrfile);
1452 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1453 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
1455 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1460 unlink (pgpoutfile);
1465 unlink (pgperrfile);
1467 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1468 -1, fileno (pgpout), fileno (pgperr),
1469 pgpinfile, keylist, flags)) == -1)
1471 mutt_perror _("Can't invoke PGP");
1474 mutt_unlink (pgpinfile);
1475 unlink (pgpoutfile);
1479 if (pgp_use_gpg_agent())
1482 fprintf (pgpin, "%s\n", PgpPass);
1485 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1488 mutt_unlink (pgpinfile);
1497 empty = (fgetc (pgpout) == EOF);
1502 while (fgets (buff, sizeof (buff), pgperr))
1505 fputs (buff, stdout);
1511 mutt_any_key_to_continue (NULL);
1516 pgp_void_passphrase (); /* just in case */
1517 unlink (pgpoutfile);
1521 b = mutt_new_body ();
1523 b->encoding = ENC7BIT;
1526 b->subtype = safe_strdup ("plain");
1528 mutt_set_parameter ("x-action", flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1530 mutt_set_parameter ("charset", send_charset, &b->parameter);
1532 b->filename = safe_strdup (pgpoutfile);
1535 /* The following is intended to give a clue to some completely brain-dead
1536 * "mail environments" which are typically used by large corporations.
1539 b->d_filename = safe_strdup ("msg.pgp");
1544 b->disposition = DISPINLINE;
1550 if (!(flags & ENCRYPT))
1551 b->encoding = a->encoding;
1556 int pgp_send_menu (HEADER *msg, int *redraw)
1559 char input_signas[SHORT_STRING];
1561 char prompt[LONG_STRING];
1563 if (!(WithCrypto & APPLICATION_PGP))
1564 return msg->security;
1566 /* If autoinline and no crypto options set, then set inline. */
1567 if (option (OPTPGPAUTOINLINE) &&
1568 !((msg->security & APPLICATION_PGP) && (msg->security & (SIGN|ENCRYPT))))
1569 msg->security |= INLINE;
1571 snprintf (prompt, sizeof (prompt),
1572 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1573 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1575 switch (mutt_multi_choice (prompt, _("esabifc")))
1577 case 1: /* (e)ncrypt */
1578 msg->security |= ENCRYPT;
1579 msg->security &= ~SIGN;
1582 case 2: /* (s)ign */
1583 msg->security |= SIGN;
1584 msg->security &= ~ENCRYPT;
1587 case 3: /* sign (a)s */
1588 unset_option(OPTPGPCHECKTRUST);
1590 if ((p = pgp_ask_for_key (_("Sign as: "), NULL, 0, PGP_SECRING)))
1592 snprintf (input_signas, sizeof (input_signas), "0x%s",
1594 mutt_str_replace (&PgpSignAs, input_signas);
1597 msg->security |= SIGN;
1599 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1604 msg->security &= ~SIGN;
1608 *redraw = REDRAW_FULL;
1611 case 4: /* (b)oth */
1612 msg->security |= (ENCRYPT | SIGN);
1615 case 5: /* (i)nline */
1616 if ((msg->security & (ENCRYPT | SIGN)))
1617 msg->security ^= INLINE;
1619 msg->security &= ~INLINE;
1622 case 6: /* (f)orget it */
1623 case 7: /* (c)lear */
1630 if (! (msg->security & (ENCRYPT | SIGN)))
1633 msg->security |= APPLICATION_PGP;
1636 return (msg->security);
1640 #endif /* CRYPT_BACKEND_CLASSIC_PGP */