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, 0)) != 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 *gpgcharset = NULL;
268 char body_charset[STRING];
269 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
271 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
273 fseeko (s->fpin, m->offset, 0);
274 last_pos = m->offset;
276 for (bytes = m->length; bytes > 0;)
278 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
281 offset = ftello (s->fpin);
282 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
285 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0)
288 start_pos = last_pos;
290 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
292 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
297 else if (!mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15))
304 /* XXX - we may wish to recode here */
306 state_puts (s->prefix, s);
311 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
313 /* Copy PGP material to temporary file */
314 mutt_mktemp (tmpfname);
315 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL)
317 mutt_perror (tmpfname);
322 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL)
324 offset = ftello (s->fpin);
325 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
330 if ((needpass && mutt_strcmp ("-----END PGP MESSAGE-----\n", buf) == 0) ||
332 && (mutt_strcmp ("-----END PGP SIGNATURE-----\n", buf) == 0
333 || mutt_strcmp ("-----END PGP PUBLIC KEY BLOCK-----\n",buf) == 0)))
335 /* remember optional Charset: armor header as defined by RfC4880 */
336 if (mutt_strncmp ("Charset: ", buf, 9) == 0)
339 gpgcharset = safe_strdup (buf + 9);
340 if ((l = mutt_strlen (gpgcharset)) > 0 && gpgcharset[l-1] == '\n')
342 if (mutt_check_charset (gpgcharset, 0) < 0)
343 mutt_str_replace (&gpgcharset, "UTF-8");
347 /* leave tmpfp open in case we still need it - but flush it! */
350 /* Invoke PGP if needed */
351 if (!clearsign || (s->flags & M_VERIFY))
353 mutt_mktemp (outfile);
354 if ((pgpout = safe_fopen (outfile, "w+")) == NULL)
356 mutt_perror (tmpfname);
360 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
361 fileno (pgpout), -1, tmpfname,
364 safe_fclose (&pgpout);
368 state_attach_puts (_("[-- Error: unable to create PGP subprocess! --]\n"), s);
370 else /* PGP started successfully */
374 if (!pgp_valid_passphrase ()) pgp_void_passphrase();
375 if (pgp_use_gpg_agent())
377 fprintf (pgpin, "%s\n", PgpPass);
380 safe_fclose (&pgpin);
382 if (s->flags & M_DISPLAY)
384 crypt_current_time (s, "PGP");
385 rc = pgp_copy_checksig (pgperr, s->fpout);
388 safe_fclose (&pgperr);
389 rv = mutt_wait_filter (thepid);
391 if (s->flags & M_DISPLAY)
393 if (rc == 0) have_any_sigs = 1;
396 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
397 * Sig _is_ correct if
398 * gpg_good_sign="" && pgp_decode_command returned 0
400 if (rc == -1 || rv) maybe_goodsig = 0;
402 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
404 if (pgp_use_gpg_agent())
405 mutt_need_hard_redraw ();
408 /* treat empty result as sign of failure */
409 /* TODO: maybe on failure mutt should include the original undecoded text. */
416 if (!clearsign && (!pgpout || c == EOF))
418 could_not_decrypt = 1;
419 pgp_void_passphrase ();
422 if (could_not_decrypt && !(s->flags & M_DISPLAY))
424 mutt_error _("Could not decrypt PGP message");
432 * Now, copy cleartext to the screen.
435 if(s->flags & M_DISPLAY)
438 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
439 else if (pgp_keyblock)
440 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
442 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
449 pgp_copy_clearsigned (tmpfp, s, body_charset);
455 char *expected_charset = gpgcharset && *gpgcharset ? gpgcharset : "utf-8";
457 dprint(4,(debugfile,"pgp: recoding inline from [%s] to [%s]\n",
458 expected_charset, Charset));
461 state_set_prefix (s);
462 fc = fgetconv_open (pgpout, expected_charset, Charset, M_ICONV_HOOK_FROM);
463 while ((c = fgetconv (fc)) != EOF)
464 state_prefix_putc (c, s);
465 fgetconv_close (&fc);
468 if (s->flags & M_DISPLAY)
470 state_putc ('\n', s);
473 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
474 if (could_not_decrypt)
475 mutt_error _("Could not decrypt PGP message");
477 mutt_message _("PGP message successfully decrypted.");
479 else if (pgp_keyblock)
480 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
482 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
488 /* why would we want to display this at all? */
489 /* XXX - we may wish to recode here */
491 state_puts (s->prefix, s);
500 m->goodsig = (maybe_goodsig && have_any_sigs);
504 safe_fclose (&tmpfp);
505 mutt_unlink (tmpfname);
509 safe_fclose (&pgpout);
510 mutt_unlink (outfile);
517 state_attach_puts (_("[-- Error: could not find beginning of PGP message! --]\n\n"), s);
524 static int pgp_check_traditional_one_body (FILE *fp, BODY *b, int tagged_only)
526 char tempfile[_POSIX_PATH_MAX];
527 char buf[HUGE_STRING];
534 if (b->type != TYPETEXT)
537 if (tagged_only && !b->tagged)
540 mutt_mktemp (tempfile);
541 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0)
547 if ((tfp = fopen (tempfile, "r")) == NULL)
553 while (fgets (buf, sizeof (buf), tfp))
555 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0)
557 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
559 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
561 else if (mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
568 if (!enc && !sgn && !key)
571 /* fix the content type */
573 mutt_set_parameter ("format", "fixed", &b->parameter);
575 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
577 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
579 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
584 int pgp_check_traditional (FILE *fp, BODY *b, int tagged_only)
588 for (; b; b = b->next)
590 if (is_multipart (b))
591 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
592 else if (b->type == TYPETEXT)
594 if ((r = mutt_is_application_pgp (b)))
597 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
608 int pgp_verify_one (BODY *sigbdy, STATE *s, const char *tempfile)
610 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
611 FILE *fp, *pgpout, *pgperr;
616 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
618 if(!(fp = safe_fopen (sigfile, "w")))
620 mutt_perror(sigfile);
624 fseeko (s->fpin, sigbdy->offset, 0);
625 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
628 mutt_mktemp(pgperrfile);
629 if(!(pgperr = safe_fopen(pgperrfile, "w+")))
631 mutt_perror(pgperrfile);
636 crypt_current_time (s, "PGP");
638 if((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
639 -1, -1, fileno(pgperr),
640 tempfile, sigfile)) != -1)
642 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
646 safe_fclose (&pgpout);
650 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
653 if ((rv = mutt_wait_filter (thepid)))
656 dprint (1, (debugfile, "pgp_verify_one: mutt_wait_filter returned %d.\n", rv));
659 safe_fclose (&pgperr);
661 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
663 mutt_unlink (sigfile);
664 mutt_unlink (pgperrfile);
666 dprint (1, (debugfile, "pgp_verify_one: returning %d.\n", badsig));
672 /* Extract pgp public keys from messages or attachments */
674 void pgp_extract_keys_from_messages (HEADER *h)
677 char tempfname[_POSIX_PATH_MAX];
682 mutt_parse_mime_message (Context, h);
683 if(h->security & PGPENCRYPT && !pgp_valid_passphrase ())
687 mutt_mktemp (tempfname);
688 if (!(fpout = safe_fopen (tempfname, "w")))
690 mutt_perror (tempfname);
694 set_option (OPTDONTHANDLEPGPKEYS);
698 for (i = 0; i < Context->vcount; i++)
700 if (Context->hdrs[Context->v2r[i]]->tagged)
702 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
703 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
704 && !pgp_valid_passphrase())
706 safe_fclose (&fpout);
709 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
710 M_CM_DECODE|M_CM_CHARCONV, 0);
716 mutt_parse_mime_message (Context, h);
717 if (h->security & PGPENCRYPT && !pgp_valid_passphrase())
719 safe_fclose (&fpout);
722 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
725 safe_fclose (&fpout);
727 pgp_invoke_import (tempfname);
728 mutt_any_key_to_continue (NULL);
732 mutt_unlink (tempfname);
733 unset_option (OPTDONTHANDLEPGPKEYS);
737 static void pgp_extract_keys_from_attachment (FILE *fp, BODY *top)
741 char tempfname[_POSIX_PATH_MAX];
743 mutt_mktemp (tempfname);
744 if (!(tempfp = safe_fopen (tempfname, "w")))
746 mutt_perror (tempfname);
750 memset (&s, 0, sizeof (STATE));
755 mutt_body_handler (top, &s);
757 safe_fclose (&tempfp);
759 pgp_invoke_import (tempfname);
760 mutt_any_key_to_continue (NULL);
762 mutt_unlink (tempfname);
765 void pgp_extract_keys_from_attachment_list (FILE *fp, int tag, BODY *top)
769 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
774 set_option(OPTDONTHANDLEPGPKEYS);
776 for(; top; top = top->next)
778 if(!tag || top->tagged)
779 pgp_extract_keys_from_attachment (fp, top);
785 unset_option(OPTDONTHANDLEPGPKEYS);
788 BODY *pgp_decrypt_part (BODY *a, STATE *s, FILE *fpout, BODY *p)
790 char buf[LONG_STRING];
791 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
795 char pgperrfile[_POSIX_PATH_MAX];
796 char pgptmpfile[_POSIX_PATH_MAX];
800 mutt_mktemp (pgperrfile);
801 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
803 mutt_perror (pgperrfile);
808 mutt_mktemp (pgptmpfile);
809 if((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL)
811 mutt_perror (pgptmpfile);
812 safe_fclose (&pgperr);
816 /* Position the stream at the beginning of the body, and send the data to
817 * the temporary file.
820 fseeko (s->fpin, a->offset, 0);
821 mutt_copy_bytes (s->fpin, pgptmp, a->length);
822 safe_fclose (&pgptmp);
824 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
825 fileno (pgperr), pgptmpfile)) == -1)
827 safe_fclose (&pgperr);
829 if (s->flags & M_DISPLAY)
830 state_attach_puts (_("[-- Error: could not create a PGP subprocess! --]\n\n"), s);
834 /* send the PGP passphrase to the subprocess. Never do this if the
835 agent is active, because this might lead to a passphrase send as
837 if (!pgp_use_gpg_agent())
838 fputs (PgpPass, pgpin);
840 safe_fclose (&pgpin);
842 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
843 * read_mime_header has a hard time parsing the message.
845 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL)
847 len = mutt_strlen (buf);
848 if (len > 1 && buf[len - 2] == '\r')
849 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
853 safe_fclose (&pgpout);
854 rv = mutt_wait_filter (thepid);
855 mutt_unlink(pgptmpfile);
857 if (s->flags & M_DISPLAY)
861 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
865 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
867 safe_fclose (&pgperr);
872 if (pgp_use_gpg_agent())
873 mutt_need_hard_redraw ();
875 if (fgetc (fpout) == EOF)
877 mutt_error _("Decryption failed");
878 pgp_void_passphrase ();
884 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL)
887 * Need to set the length of this body part.
889 fstat (fileno (fpout), &info);
890 tattach->length = info.st_size - tattach->offset;
892 /* See if we need to recurse on this MIME part. */
894 mutt_parse_part (fpout, tattach);
900 int pgp_decrypt_mime (FILE *fpin, FILE **fpout, BODY *b, BODY **cur)
902 char tempfile[_POSIX_PATH_MAX];
906 if(!mutt_is_multipart_encrypted(b))
909 if(!b->parts || !b->parts->next)
914 memset (&s, 0, sizeof (s));
916 mutt_mktemp (tempfile);
917 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL)
919 mutt_perror (tempfile);
924 *cur = pgp_decrypt_part (b, &s, *fpout, p);
934 int pgp_encrypted_handler (BODY *a, STATE *s)
936 char tempfile[_POSIX_PATH_MAX];
943 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
944 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
945 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
946 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0)
948 if (s->flags & M_DISPLAY)
949 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"), s);
954 * Move forward to the application/pgp-encrypted body.
958 mutt_mktemp (tempfile);
959 if ((fpout = safe_fopen (tempfile, "w+")) == NULL)
961 if (s->flags & M_DISPLAY)
962 state_attach_puts (_("[-- Error: could not create temporary file! --]\n"), s);
966 if (s->flags & M_DISPLAY) crypt_current_time (s, "PGP");
968 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL)
970 if (s->flags & M_DISPLAY)
971 state_attach_puts (_("[-- The following data is PGP/MIME encrypted --]\n\n"), s);
975 rc = mutt_body_handler (tattach, s);
979 * if a multipart/signed is the _only_ sub-part of a
980 * multipart/encrypted, cache signature verification
985 if (mutt_is_multipart_signed (tattach) && !tattach->next)
986 p->goodsig |= tattach->goodsig;
988 if (s->flags & M_DISPLAY)
990 state_puts ("\n", s);
991 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
994 mutt_free_body (&tattach);
995 /* clear 'Invoking...' message, since there's no error */
996 mutt_message _("PGP message successfully decrypted.");
1000 mutt_error _("Could not decrypt PGP message");
1001 /* void the passphrase, even if it's not necessarily the problem */
1002 pgp_void_passphrase ();
1006 safe_fclose (&fpout);
1007 mutt_unlink(tempfile);
1012 /* ----------------------------------------------------------------------------
1013 * Routines for sending PGP/MIME messages.
1017 BODY *pgp_sign_message (BODY *a)
1020 char buffer[LONG_STRING];
1021 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
1022 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
1027 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
1029 mutt_mktemp (sigfile);
1030 if ((fp = safe_fopen (sigfile, "w")) == NULL)
1035 mutt_mktemp (signedfile);
1036 if ((sfp = safe_fopen(signedfile, "w")) == NULL)
1038 mutt_perror(signedfile);
1044 mutt_write_mime_header (a, sfp);
1046 mutt_write_mime_body (a, sfp);
1049 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
1050 -1, -1, -1, signedfile)) == -1)
1052 mutt_perror _("Can't open PGP subprocess!");
1059 if (!pgp_use_gpg_agent())
1060 fputs(PgpPass, pgpin);
1062 safe_fclose (&pgpin);
1065 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
1066 * recommended for future releases of PGP.
1068 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL)
1070 if (mutt_strcmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
1071 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
1072 else if (mutt_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
1073 fputs ("-----END PGP SIGNATURE-----\n", fp);
1076 empty = 0; /* got some output, so we're ok */
1079 /* check for errors from PGP */
1081 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL)
1084 fputs (buffer, stdout);
1087 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1090 safe_fclose (&pgperr);
1091 safe_fclose (&pgpout);
1092 unlink (signedfile);
1094 if (fclose (fp) != 0)
1096 mutt_perror ("fclose");
1102 mutt_any_key_to_continue (NULL);
1106 /* most likely error is a bad passphrase, so automatically forget it */
1107 pgp_void_passphrase ();
1108 return (NULL); /* fatal error while signing */
1111 t = mutt_new_body ();
1112 t->type = TYPEMULTIPART;
1113 t->subtype = safe_strdup ("signed");
1114 t->encoding = ENC7BIT;
1116 t->disposition = DISPINLINE;
1118 mutt_generate_boundary (&t->parameter);
1119 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1120 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1125 t->parts->next = mutt_new_body ();
1127 t->type = TYPEAPPLICATION;
1128 t->subtype = safe_strdup ("pgp-signature");
1129 t->filename = safe_strdup (sigfile);
1131 t->disposition = DISPINLINE;
1132 t->encoding = ENC7BIT;
1133 t->unlink = 1; /* ok to remove this file after sending. */
1138 static short is_numerical_keyid (const char *s)
1140 /* or should we require the "0x"? */
1141 if (strncmp (s, "0x", 2) == 0)
1146 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1152 /* This routine attempts to find the keyids of the recipients of a message.
1153 * It returns NULL if any of the keys can not be found.
1155 char *pgp_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
1157 char *keyID, *keylist = NULL;
1158 size_t keylist_size = 0;
1159 size_t keylist_used = 0;
1160 ADDRESS *tmp = NULL, *addr = NULL;
1161 ADDRESS **last = &tmp;
1164 pgp_key_t k_info = NULL, key = NULL;
1166 const char *fqdn = mutt_fqdn (1);
1168 for (i = 0; i < 3; i++)
1172 case 0: p = to; break;
1173 case 1: p = cc; break;
1174 case 2: p = bcc; break;
1178 *last = rfc822_cpy_adr (p, 0);
1180 last = &((*last)->next);
1184 rfc822_qualify (tmp, fqdn);
1186 tmp = mutt_remove_duplicates (tmp);
1188 for (p = tmp; p ; p = p->next)
1190 char buf[LONG_STRING];
1195 if ((keyID = mutt_crypt_hook (p)) != NULL)
1198 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID, p->mailbox);
1199 if ((r = mutt_yesorno (buf, M_YES)) == M_YES)
1201 if (is_numerical_keyid (keyID))
1203 if (strncmp (keyID, "0x", 2) == 0)
1205 goto bypass_selection; /* you don't see this. */
1208 /* check for e-mail address */
1209 if (strchr (keyID, '@') &&
1210 (addr = rfc822_parse_adrlist (NULL, keyID)))
1212 if (fqdn) rfc822_qualify (addr, fqdn);
1216 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1221 rfc822_free_address (&tmp);
1222 rfc822_free_address (&addr);
1228 pgp_invoke_getkeys (q);
1230 if (k_info == NULL && (k_info = pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL)
1232 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1234 if ((key = pgp_ask_for_key (buf, q->mailbox,
1235 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL)
1238 rfc822_free_address (&tmp);
1239 rfc822_free_address (&addr);
1246 keyID = pgp_keyid (key);
1249 keylist_size += mutt_strlen (keyID) + 4;
1250 safe_realloc (&keylist, keylist_size);
1251 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1253 keylist_used = mutt_strlen (keylist);
1255 pgp_free_key (&key);
1256 rfc822_free_address (&addr);
1259 rfc822_free_address (&tmp);
1263 /* Warning: "a" is no longer freed in this routine, you need
1264 * to free it later. This is necessary for $fcc_attach. */
1266 BODY *pgp_encrypt_message (BODY *a, char *keylist, int sign)
1268 char buf[LONG_STRING];
1269 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1270 char pgpinfile[_POSIX_PATH_MAX];
1271 FILE *pgpin, *pgperr, *fpout, *fptmp;
1277 mutt_mktemp (tempfile);
1278 if ((fpout = safe_fopen (tempfile, "w+")) == NULL)
1280 mutt_perror (tempfile);
1284 mutt_mktemp (pgperrfile);
1285 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
1287 mutt_perror (pgperrfile);
1289 safe_fclose (&fpout);
1292 unlink (pgperrfile);
1294 mutt_mktemp(pgpinfile);
1295 if((fptmp = safe_fopen(pgpinfile, "w")) == NULL)
1297 mutt_perror(pgpinfile);
1299 safe_fclose (&fpout);
1300 safe_fclose (&pgperr);
1305 convert_to_7bit (a);
1307 mutt_write_mime_header (a, fptmp);
1308 fputc ('\n', fptmp);
1309 mutt_write_mime_body (a, fptmp);
1310 safe_fclose (&fptmp);
1312 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1313 fileno (fpout), fileno (pgperr),
1314 pgpinfile, keylist, sign)) == -1)
1316 safe_fclose (&pgperr);
1323 if (!pgp_use_gpg_agent())
1324 fputs (PgpPass, pgpin);
1325 fputc ('\n', pgpin);
1327 safe_fclose (&pgpin);
1329 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1337 empty = (fgetc (fpout) == EOF);
1338 safe_fclose (&fpout);
1342 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL)
1345 fputs (buf, stdout);
1347 safe_fclose (&pgperr);
1349 /* pause if there is any error output from PGP */
1351 mutt_any_key_to_continue (NULL);
1355 /* fatal error while trying to encrypt message */
1357 pgp_void_passphrase (); /* just in case */
1362 t = mutt_new_body ();
1363 t->type = TYPEMULTIPART;
1364 t->subtype = safe_strdup ("encrypted");
1365 t->encoding = ENC7BIT;
1367 t->disposition = DISPINLINE;
1369 mutt_generate_boundary(&t->parameter);
1370 mutt_set_parameter("protocol", "application/pgp-encrypted", &t->parameter);
1372 t->parts = mutt_new_body ();
1373 t->parts->type = TYPEAPPLICATION;
1374 t->parts->subtype = safe_strdup ("pgp-encrypted");
1375 t->parts->encoding = ENC7BIT;
1377 t->parts->next = mutt_new_body ();
1378 t->parts->next->type = TYPEAPPLICATION;
1379 t->parts->next->subtype = safe_strdup ("octet-stream");
1380 t->parts->next->encoding = ENC7BIT;
1381 t->parts->next->filename = safe_strdup (tempfile);
1382 t->parts->next->use_disp = 1;
1383 t->parts->next->disposition = DISPINLINE;
1384 t->parts->next->unlink = 1; /* delete after sending the message */
1385 t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime can save */
1390 BODY *pgp_traditional_encryptsign (BODY *a, int flags, char *keylist)
1394 char pgpoutfile[_POSIX_PATH_MAX];
1395 char pgperrfile[_POSIX_PATH_MAX];
1396 char pgpinfile[_POSIX_PATH_MAX];
1398 char body_charset[STRING];
1400 const char *send_charset;
1402 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1412 if (a->type != TYPETEXT)
1414 if (ascii_strcasecmp (a->subtype, "plain"))
1417 if ((fp = fopen (a->filename, "r")) == NULL)
1419 mutt_perror (a->filename);
1423 mutt_mktemp (pgpinfile);
1424 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL)
1426 mutt_perror (pgpinfile);
1431 /* The following code is really correct: If noconv is set,
1432 * a's charset parameter contains the on-disk character set, and
1433 * we have to convert from that to utf-8. If noconv is not set,
1434 * we have to convert from $charset to utf-8.
1437 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1439 from_charset = body_charset;
1441 from_charset = Charset;
1443 if (!mutt_is_us_ascii (body_charset))
1448 if (flags & ENCRYPT)
1449 send_charset = "us-ascii";
1451 send_charset = "utf-8";
1453 /* fromcode is assumed to be correct: we set flags to 0 */
1454 fc = fgetconv_open (fp, from_charset, "utf-8", 0);
1455 while ((c = fgetconv (fc)) != EOF)
1458 fgetconv_close (&fc);
1462 send_charset = "us-ascii";
1463 mutt_copy_stream (fp, pgpin);
1466 safe_fclose (&pgpin);
1468 mutt_mktemp (pgpoutfile);
1469 mutt_mktemp (pgperrfile);
1470 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1471 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
1473 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1477 safe_fclose (&pgpout);
1478 unlink (pgpoutfile);
1483 unlink (pgperrfile);
1485 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1486 -1, fileno (pgpout), fileno (pgperr),
1487 pgpinfile, keylist, flags)) == -1)
1489 mutt_perror _("Can't invoke PGP");
1490 safe_fclose (&pgpout);
1491 safe_fclose (&pgperr);
1492 mutt_unlink (pgpinfile);
1493 unlink (pgpoutfile);
1497 if (pgp_use_gpg_agent())
1500 fprintf (pgpin, "%s\n", PgpPass);
1501 safe_fclose (&pgpin);
1503 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1506 mutt_unlink (pgpinfile);
1515 empty = (fgetc (pgpout) == EOF);
1516 safe_fclose (&pgpout);
1520 while (fgets (buff, sizeof (buff), pgperr))
1523 fputs (buff, stdout);
1526 safe_fclose (&pgperr);
1529 mutt_any_key_to_continue (NULL);
1534 pgp_void_passphrase (); /* just in case */
1535 unlink (pgpoutfile);
1539 b = mutt_new_body ();
1541 b->encoding = ENC7BIT;
1544 b->subtype = safe_strdup ("plain");
1546 mutt_set_parameter ("x-action", flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1548 mutt_set_parameter ("charset", send_charset, &b->parameter);
1550 b->filename = safe_strdup (pgpoutfile);
1553 /* The following is intended to give a clue to some completely brain-dead
1554 * "mail environments" which are typically used by large corporations.
1557 b->d_filename = safe_strdup ("msg.pgp");
1562 b->disposition = DISPINLINE;
1568 if (!(flags & ENCRYPT))
1569 b->encoding = a->encoding;
1574 int pgp_send_menu (HEADER *msg, int *redraw)
1577 char input_signas[SHORT_STRING];
1579 char prompt[LONG_STRING];
1581 if (!(WithCrypto & APPLICATION_PGP))
1582 return msg->security;
1584 /* If autoinline and no crypto options set, then set inline. */
1585 if (option (OPTPGPAUTOINLINE) &&
1586 !((msg->security & APPLICATION_PGP) && (msg->security & (SIGN|ENCRYPT))))
1587 msg->security |= INLINE;
1589 snprintf (prompt, sizeof (prompt),
1590 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1591 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1593 switch (mutt_multi_choice (prompt, _("esabifc")))
1595 case 1: /* (e)ncrypt */
1596 msg->security |= ENCRYPT;
1597 msg->security &= ~SIGN;
1600 case 2: /* (s)ign */
1601 msg->security |= SIGN;
1602 msg->security &= ~ENCRYPT;
1605 case 3: /* sign (a)s */
1606 unset_option(OPTPGPCHECKTRUST);
1608 if ((p = pgp_ask_for_key (_("Sign as: "), NULL, 0, PGP_SECRING)))
1610 snprintf (input_signas, sizeof (input_signas), "0x%s",
1612 mutt_str_replace (&PgpSignAs, input_signas);
1615 msg->security |= SIGN;
1617 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1622 msg->security &= ~SIGN;
1626 *redraw = REDRAW_FULL;
1629 case 4: /* (b)oth */
1630 msg->security |= (ENCRYPT | SIGN);
1633 case 5: /* (i)nline */
1634 if ((msg->security & (ENCRYPT | SIGN)))
1635 msg->security ^= INLINE;
1637 msg->security &= ~INLINE;
1640 case 6: /* (f)orget it */
1641 case 7: /* (c)lear */
1648 if (! (msg->security & (ENCRYPT | SIGN)))
1651 msg->security |= APPLICATION_PGP;
1654 return (msg->security);
1658 #endif /* CRYPT_BACKEND_CLASSIC_PGP */