2 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1998,1999 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 (!option (OPTDONTHANDLEPGPKEYS) &&
297 mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
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)))
337 /* leave tmpfp open in case we still need it - but flush it! */
340 /* Invoke PGP if needed */
341 if (!clearsign || (s->flags & M_VERIFY))
343 mutt_mktemp (outfile);
344 if ((pgpout = safe_fopen (outfile, "w+")) == NULL)
346 mutt_perror (tmpfname);
350 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
351 fileno (pgpout), -1, tmpfname,
354 safe_fclose (&pgpout);
358 state_attach_puts (_("[-- Error: unable to create PGP subprocess! --]\n"), s);
360 else /* PGP started successfully */
364 if (!pgp_valid_passphrase ()) pgp_void_passphrase();
365 if (pgp_use_gpg_agent())
367 fprintf (pgpin, "%s\n", PgpPass);
370 safe_fclose (&pgpin);
372 if (s->flags & M_DISPLAY)
374 crypt_current_time (s, "PGP");
375 rc = pgp_copy_checksig (pgperr, s->fpout);
378 safe_fclose (&pgperr);
379 rv = mutt_wait_filter (thepid);
381 if (s->flags & M_DISPLAY)
383 if (rc == 0) have_any_sigs = 1;
386 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
387 * Sig _is_ correct if
388 * gpg_good_sign="" && pgp_decode_command returned 0
390 if (rc == -1 || rv) maybe_goodsig = 0;
392 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
396 /* treat empty result as sign of failure */
397 /* TODO: maybe on failure mutt should include the original undecoded text. */
404 if (!clearsign && (!pgpout || c == EOF))
406 could_not_decrypt = 1;
407 pgp_void_passphrase ();
410 if (could_not_decrypt && !(s->flags & M_DISPLAY))
412 mutt_error _("Could not decrypt PGP message");
420 * Now, copy cleartext to the screen. NOTE - we expect that PGP
421 * outputs utf-8 cleartext. This may not always be true, but it
422 * seems to be a reasonable guess.
425 if(s->flags & M_DISPLAY)
428 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
429 else if (pgp_keyblock)
430 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
432 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
439 pgp_copy_clearsigned (tmpfp, s, body_charset);
446 state_set_prefix (s);
447 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
448 while ((c = fgetconv (fc)) != EOF)
449 state_prefix_putc (c, s);
450 fgetconv_close (&fc);
453 if (s->flags & M_DISPLAY)
455 state_putc ('\n', s);
458 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
459 if (could_not_decrypt)
460 mutt_error _("Could not decrypt PGP message");
462 mutt_message _("PGP message successfully decrypted.");
464 else if (pgp_keyblock)
465 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
467 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
472 /* XXX - we may wish to recode here */
474 state_puts (s->prefix, s);
482 m->goodsig = (maybe_goodsig && have_any_sigs);
486 safe_fclose (&tmpfp);
487 mutt_unlink (tmpfname);
491 safe_fclose (&pgpout);
492 mutt_unlink (outfile);
497 state_attach_puts (_("[-- Error: could not find beginning of PGP message! --]\n\n"), s);
504 static int pgp_check_traditional_one_body (FILE *fp, BODY *b, int tagged_only)
506 char tempfile[_POSIX_PATH_MAX];
507 char buf[HUGE_STRING];
514 if (b->type != TYPETEXT)
517 if (tagged_only && !b->tagged)
520 mutt_mktemp (tempfile);
521 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0)
527 if ((tfp = fopen (tempfile, "r")) == NULL)
533 while (fgets (buf, sizeof (buf), tfp))
535 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0)
537 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
539 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
541 else if (mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
548 if (!enc && !sgn && !key)
551 /* fix the content type */
553 mutt_set_parameter ("format", "fixed", &b->parameter);
555 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
557 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
559 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
564 int pgp_check_traditional (FILE *fp, BODY *b, int tagged_only)
568 for (; b; b = b->next)
570 if (is_multipart (b))
571 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
572 else if (b->type == TYPETEXT)
574 if ((r = mutt_is_application_pgp (b)))
577 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
588 int pgp_verify_one (BODY *sigbdy, STATE *s, const char *tempfile)
590 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
591 FILE *fp, *pgpout, *pgperr;
596 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
598 if(!(fp = safe_fopen (sigfile, "w")))
600 mutt_perror(sigfile);
604 fseeko (s->fpin, sigbdy->offset, 0);
605 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
608 mutt_mktemp(pgperrfile);
609 if(!(pgperr = safe_fopen(pgperrfile, "w+")))
611 mutt_perror(pgperrfile);
616 crypt_current_time (s, "PGP");
618 if((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
619 -1, -1, fileno(pgperr),
620 tempfile, sigfile)) != -1)
622 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
626 safe_fclose (&pgpout);
630 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
633 if ((rv = mutt_wait_filter (thepid)))
636 dprint (1, (debugfile, "pgp_verify_one: mutt_wait_filter returned %d.\n", rv));
639 safe_fclose (&pgperr);
641 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
643 mutt_unlink (sigfile);
644 mutt_unlink (pgperrfile);
646 dprint (1, (debugfile, "pgp_verify_one: returning %d.\n", badsig));
652 /* Extract pgp public keys from messages or attachments */
654 void pgp_extract_keys_from_messages (HEADER *h)
657 char tempfname[_POSIX_PATH_MAX];
662 mutt_parse_mime_message (Context, h);
663 if(h->security & PGPENCRYPT && !pgp_valid_passphrase ())
667 mutt_mktemp (tempfname);
668 if (!(fpout = safe_fopen (tempfname, "w")))
670 mutt_perror (tempfname);
674 set_option (OPTDONTHANDLEPGPKEYS);
678 for (i = 0; i < Context->vcount; i++)
680 if (Context->hdrs[Context->v2r[i]]->tagged)
682 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
683 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
684 && !pgp_valid_passphrase())
689 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
690 M_CM_DECODE|M_CM_CHARCONV, 0);
696 mutt_parse_mime_message (Context, h);
697 if (h->security & PGPENCRYPT && !pgp_valid_passphrase())
702 mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
707 pgp_invoke_import (tempfname);
708 mutt_any_key_to_continue (NULL);
712 mutt_unlink (tempfname);
713 unset_option (OPTDONTHANDLEPGPKEYS);
717 static void pgp_extract_keys_from_attachment (FILE *fp, BODY *top)
721 char tempfname[_POSIX_PATH_MAX];
723 mutt_mktemp (tempfname);
724 if (!(tempfp = safe_fopen (tempfname, "w")))
726 mutt_perror (tempfname);
730 memset (&s, 0, sizeof (STATE));
735 mutt_body_handler (top, &s);
739 pgp_invoke_import (tempfname);
740 mutt_any_key_to_continue (NULL);
742 mutt_unlink (tempfname);
745 void pgp_extract_keys_from_attachment_list (FILE *fp, int tag, BODY *top)
749 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
754 set_option(OPTDONTHANDLEPGPKEYS);
756 for(; top; top = top->next)
758 if(!tag || top->tagged)
759 pgp_extract_keys_from_attachment (fp, top);
765 unset_option(OPTDONTHANDLEPGPKEYS);
768 BODY *pgp_decrypt_part (BODY *a, STATE *s, FILE *fpout, BODY *p)
770 char buf[LONG_STRING];
771 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
775 char pgperrfile[_POSIX_PATH_MAX];
776 char pgptmpfile[_POSIX_PATH_MAX];
780 mutt_mktemp (pgperrfile);
781 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
783 mutt_perror (pgperrfile);
788 mutt_mktemp (pgptmpfile);
789 if((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL)
791 mutt_perror (pgptmpfile);
796 /* Position the stream at the beginning of the body, and send the data to
797 * the temporary file.
800 fseeko (s->fpin, a->offset, 0);
801 mutt_copy_bytes (s->fpin, pgptmp, a->length);
804 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
805 fileno (pgperr), pgptmpfile)) == -1)
809 if (s->flags & M_DISPLAY)
810 state_attach_puts (_("[-- Error: could not create a PGP subprocess! --]\n\n"), s);
814 /* send the PGP passphrase to the subprocess. Never do this if the
815 agent is active, because this might lead to a passphrase send as
817 if (!pgp_use_gpg_agent())
818 fputs (PgpPass, pgpin);
822 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
823 * read_mime_header has a hard time parsing the message.
825 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL)
827 len = mutt_strlen (buf);
828 if (len > 1 && buf[len - 2] == '\r')
829 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
834 rv = mutt_wait_filter (thepid);
835 mutt_unlink(pgptmpfile);
837 if (s->flags & M_DISPLAY)
841 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
845 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
852 if (fgetc (fpout) == EOF)
854 mutt_error _("Decryption failed");
855 pgp_void_passphrase ();
861 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL)
864 * Need to set the length of this body part.
866 fstat (fileno (fpout), &info);
867 tattach->length = info.st_size - tattach->offset;
869 /* See if we need to recurse on this MIME part. */
871 mutt_parse_part (fpout, tattach);
877 int pgp_decrypt_mime (FILE *fpin, FILE **fpout, BODY *b, BODY **cur)
879 char tempfile[_POSIX_PATH_MAX];
883 if(!mutt_is_multipart_encrypted(b))
886 if(!b->parts || !b->parts->next)
891 memset (&s, 0, sizeof (s));
893 mutt_mktemp (tempfile);
894 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL)
896 mutt_perror (tempfile);
901 *cur = pgp_decrypt_part (b, &s, *fpout, p);
911 int pgp_encrypted_handler (BODY *a, STATE *s)
913 char tempfile[_POSIX_PATH_MAX];
920 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
921 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
922 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
923 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0)
925 if (s->flags & M_DISPLAY)
926 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"), s);
931 * Move forward to the application/pgp-encrypted body.
935 mutt_mktemp (tempfile);
936 if ((fpout = safe_fopen (tempfile, "w+")) == NULL)
938 if (s->flags & M_DISPLAY)
939 state_attach_puts (_("[-- Error: could not create temporary file! --]\n"), s);
943 if (s->flags & M_DISPLAY) crypt_current_time (s, "PGP");
945 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL)
947 if (s->flags & M_DISPLAY)
948 state_attach_puts (_("[-- The following data is PGP/MIME encrypted --]\n\n"), s);
952 rc = mutt_body_handler (tattach, s);
956 * if a multipart/signed is the _only_ sub-part of a
957 * multipart/encrypted, cache signature verification
962 if (mutt_is_multipart_signed (tattach) && !tattach->next)
963 p->goodsig |= tattach->goodsig;
965 if (s->flags & M_DISPLAY)
967 state_puts ("\n", s);
968 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
971 mutt_free_body (&tattach);
972 /* clear 'Invoking...' message, since there's no error */
973 mutt_message _("PGP message successfully decrypted.");
977 mutt_error _("Could not decrypt PGP message");
978 /* void the passphrase, even if it's not necessarily the problem */
979 pgp_void_passphrase ();
984 mutt_unlink(tempfile);
989 /* ----------------------------------------------------------------------------
990 * Routines for sending PGP/MIME messages.
994 BODY *pgp_sign_message (BODY *a)
997 char buffer[LONG_STRING];
998 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
999 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
1004 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
1006 mutt_mktemp (sigfile);
1007 if ((fp = safe_fopen (sigfile, "w")) == NULL)
1012 mutt_mktemp (signedfile);
1013 if ((sfp = safe_fopen(signedfile, "w")) == NULL)
1015 mutt_perror(signedfile);
1021 mutt_write_mime_header (a, sfp);
1023 mutt_write_mime_body (a, sfp);
1026 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
1027 -1, -1, -1, signedfile)) == -1)
1029 mutt_perror _("Can't open PGP subprocess!");
1036 if (!pgp_use_gpg_agent())
1037 fputs(PgpPass, pgpin);
1042 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
1043 * recommended for future releases of PGP.
1045 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL)
1047 if (mutt_strcmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
1048 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
1049 else if (mutt_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
1050 fputs ("-----END PGP SIGNATURE-----\n", fp);
1053 empty = 0; /* got some output, so we're ok */
1056 /* check for errors from PGP */
1058 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL)
1061 fputs (buffer, stdout);
1064 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1069 unlink (signedfile);
1071 if (fclose (fp) != 0)
1073 mutt_perror ("fclose");
1079 mutt_any_key_to_continue (NULL);
1083 /* most likely error is a bad passphrase, so automatically forget it */
1084 pgp_void_passphrase ();
1085 return (NULL); /* fatal error while signing */
1088 t = mutt_new_body ();
1089 t->type = TYPEMULTIPART;
1090 t->subtype = safe_strdup ("signed");
1091 t->encoding = ENC7BIT;
1093 t->disposition = DISPINLINE;
1095 mutt_generate_boundary (&t->parameter);
1096 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1097 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1102 t->parts->next = mutt_new_body ();
1104 t->type = TYPEAPPLICATION;
1105 t->subtype = safe_strdup ("pgp-signature");
1106 t->filename = safe_strdup (sigfile);
1108 t->disposition = DISPINLINE;
1109 t->encoding = ENC7BIT;
1110 t->unlink = 1; /* ok to remove this file after sending. */
1115 static short is_numerical_keyid (const char *s)
1117 /* or should we require the "0x"? */
1118 if (strncmp (s, "0x", 2) == 0)
1123 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1129 /* This routine attempts to find the keyids of the recipients of a message.
1130 * It returns NULL if any of the keys can not be found.
1132 char *pgp_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
1134 char *keyID, *keylist = NULL, *t;
1135 size_t keylist_size = 0;
1136 size_t keylist_used = 0;
1137 ADDRESS *tmp = NULL, *addr = NULL;
1138 ADDRESS **last = &tmp;
1141 pgp_key_t k_info = NULL, key = NULL;
1143 const char *fqdn = mutt_fqdn (1);
1145 for (i = 0; i < 3; i++)
1149 case 0: p = to; break;
1150 case 1: p = cc; break;
1151 case 2: p = bcc; break;
1155 *last = rfc822_cpy_adr (p);
1157 last = &((*last)->next);
1161 rfc822_qualify (tmp, fqdn);
1163 tmp = mutt_remove_duplicates (tmp);
1165 for (p = tmp; p ; p = p->next)
1167 char buf[LONG_STRING];
1172 if ((keyID = mutt_crypt_hook (p)) != NULL)
1175 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID, p->mailbox);
1176 if ((r = mutt_yesorno (buf, M_YES)) == M_YES)
1178 if (is_numerical_keyid (keyID))
1180 if (strncmp (keyID, "0x", 2) == 0)
1182 goto bypass_selection; /* you don't see this. */
1185 /* check for e-mail address */
1186 if ((t = strchr (keyID, '@')) &&
1187 (addr = rfc822_parse_adrlist (NULL, keyID)))
1189 if (fqdn) rfc822_qualify (addr, fqdn);
1193 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1198 rfc822_free_address (&tmp);
1199 rfc822_free_address (&addr);
1205 pgp_invoke_getkeys (q);
1207 if (k_info == NULL && (k_info = pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL)
1209 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1211 if ((key = pgp_ask_for_key (buf, q->mailbox,
1212 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL)
1215 rfc822_free_address (&tmp);
1216 rfc822_free_address (&addr);
1223 keyID = pgp_keyid (key);
1226 keylist_size += mutt_strlen (keyID) + 4;
1227 safe_realloc (&keylist, keylist_size);
1228 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1230 keylist_used = mutt_strlen (keylist);
1232 pgp_free_key (&key);
1233 rfc822_free_address (&addr);
1236 rfc822_free_address (&tmp);
1240 /* Warning: "a" is no longer freed in this routine, you need
1241 * to free it later. This is necessary for $fcc_attach. */
1243 BODY *pgp_encrypt_message (BODY *a, char *keylist, int sign)
1245 char buf[LONG_STRING];
1246 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1247 char pgpinfile[_POSIX_PATH_MAX];
1248 FILE *pgpin, *pgperr, *fpout, *fptmp;
1254 mutt_mktemp (tempfile);
1255 if ((fpout = safe_fopen (tempfile, "w+")) == NULL)
1257 mutt_perror (tempfile);
1261 mutt_mktemp (pgperrfile);
1262 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
1264 mutt_perror (pgperrfile);
1269 unlink (pgperrfile);
1271 mutt_mktemp(pgpinfile);
1272 if((fptmp = safe_fopen(pgpinfile, "w")) == NULL)
1274 mutt_perror(pgpinfile);
1282 convert_to_7bit (a);
1284 mutt_write_mime_header (a, fptmp);
1285 fputc ('\n', fptmp);
1286 mutt_write_mime_body (a, fptmp);
1289 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1290 fileno (fpout), fileno (pgperr),
1291 pgpinfile, keylist, sign)) == -1)
1300 if (!pgp_use_gpg_agent())
1301 fputs (PgpPass, pgpin);
1302 fputc ('\n', pgpin);
1306 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1314 empty = (fgetc (fpout) == EOF);
1319 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL)
1322 fputs (buf, stdout);
1326 /* pause if there is any error output from PGP */
1328 mutt_any_key_to_continue (NULL);
1332 /* fatal error while trying to encrypt message */
1334 pgp_void_passphrase (); /* just in case */
1339 t = mutt_new_body ();
1340 t->type = TYPEMULTIPART;
1341 t->subtype = safe_strdup ("encrypted");
1342 t->encoding = ENC7BIT;
1344 t->disposition = DISPINLINE;
1346 mutt_generate_boundary(&t->parameter);
1347 mutt_set_parameter("protocol", "application/pgp-encrypted", &t->parameter);
1349 t->parts = mutt_new_body ();
1350 t->parts->type = TYPEAPPLICATION;
1351 t->parts->subtype = safe_strdup ("pgp-encrypted");
1352 t->parts->encoding = ENC7BIT;
1354 t->parts->next = mutt_new_body ();
1355 t->parts->next->type = TYPEAPPLICATION;
1356 t->parts->next->subtype = safe_strdup ("octet-stream");
1357 t->parts->next->encoding = ENC7BIT;
1358 t->parts->next->filename = safe_strdup (tempfile);
1359 t->parts->next->use_disp = 1;
1360 t->parts->next->disposition = DISPINLINE;
1361 t->parts->next->unlink = 1; /* delete after sending the message */
1362 t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime can save */
1367 BODY *pgp_traditional_encryptsign (BODY *a, int flags, char *keylist)
1371 char pgpoutfile[_POSIX_PATH_MAX];
1372 char pgperrfile[_POSIX_PATH_MAX];
1373 char pgpinfile[_POSIX_PATH_MAX];
1375 char body_charset[STRING];
1377 const char *send_charset;
1379 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1389 if (a->type != TYPETEXT)
1391 if (ascii_strcasecmp (a->subtype, "plain"))
1394 if ((fp = fopen (a->filename, "r")) == NULL)
1396 mutt_perror (a->filename);
1400 mutt_mktemp (pgpinfile);
1401 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL)
1403 mutt_perror (pgpinfile);
1408 /* The following code is really correct: If noconv is set,
1409 * a's charset parameter contains the on-disk character set, and
1410 * we have to convert from that to utf-8. If noconv is not set,
1411 * we have to convert from $charset to utf-8.
1414 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1416 from_charset = body_charset;
1418 from_charset = Charset;
1420 if (!mutt_is_us_ascii (body_charset))
1425 if (flags & ENCRYPT)
1426 send_charset = "us-ascii";
1428 send_charset = "utf-8";
1430 /* fromcode is assumed to be correct: we set flags to 0 */
1431 fc = fgetconv_open (fp, from_charset, "utf-8", 0);
1432 while ((c = fgetconv (fc)) != EOF)
1435 fgetconv_close (&fc);
1439 send_charset = "us-ascii";
1440 mutt_copy_stream (fp, pgpin);
1445 mutt_mktemp (pgpoutfile);
1446 mutt_mktemp (pgperrfile);
1447 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1448 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL)
1450 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1455 unlink (pgpoutfile);
1460 unlink (pgperrfile);
1462 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1463 -1, fileno (pgpout), fileno (pgperr),
1464 pgpinfile, keylist, flags)) == -1)
1466 mutt_perror _("Can't invoke PGP");
1469 mutt_unlink (pgpinfile);
1470 unlink (pgpoutfile);
1474 if (pgp_use_gpg_agent())
1477 fprintf (pgpin, "%s\n", PgpPass);
1480 if(mutt_wait_filter (thepid) && option(OPTPGPCHECKEXIT))
1483 mutt_unlink (pgpinfile);
1492 empty = (fgetc (pgpout) == EOF);
1497 while (fgets (buff, sizeof (buff), pgperr))
1500 fputs (buff, stdout);
1506 mutt_any_key_to_continue (NULL);
1511 pgp_void_passphrase (); /* just in case */
1512 unlink (pgpoutfile);
1516 b = mutt_new_body ();
1518 b->encoding = ENC7BIT;
1521 b->subtype = safe_strdup ("plain");
1523 mutt_set_parameter ("x-action", flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1525 mutt_set_parameter ("charset", send_charset, &b->parameter);
1527 b->filename = safe_strdup (pgpoutfile);
1530 /* The following is intended to give a clue to some completely brain-dead
1531 * "mail environments" which are typically used by large corporations.
1534 b->d_filename = safe_strdup ("msg.pgp");
1539 b->disposition = DISPINLINE;
1545 if (!(flags & ENCRYPT))
1546 b->encoding = a->encoding;
1551 int pgp_send_menu (HEADER *msg, int *redraw)
1554 char input_signas[SHORT_STRING];
1556 char prompt[LONG_STRING];
1558 if (!(WithCrypto & APPLICATION_PGP))
1559 return msg->security;
1561 /* If autoinline and no crypto options set, then set inline. */
1562 if (option (OPTPGPAUTOINLINE) &&
1563 !((msg->security & APPLICATION_PGP) && (msg->security & (SIGN|ENCRYPT))))
1564 msg->security |= INLINE;
1566 snprintf (prompt, sizeof (prompt),
1567 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1568 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1570 switch (mutt_multi_choice (prompt, _("esabifc")))
1572 case 1: /* (e)ncrypt */
1573 msg->security |= ENCRYPT;
1574 msg->security &= ~SIGN;
1577 case 2: /* (s)ign */
1578 msg->security |= SIGN;
1579 msg->security &= ~ENCRYPT;
1582 case 3: /* sign (a)s */
1583 unset_option(OPTPGPCHECKTRUST);
1585 if ((p = pgp_ask_for_key (_("Sign as: "), NULL, 0, PGP_SECRING)))
1587 snprintf (input_signas, sizeof (input_signas), "0x%s",
1589 mutt_str_replace (&PgpSignAs, input_signas);
1592 msg->security |= SIGN;
1594 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1599 msg->security &= ~SIGN;
1603 *redraw = REDRAW_FULL;
1606 case 4: /* (b)oth */
1607 msg->security |= (ENCRYPT | SIGN);
1610 case 5: /* (i)nline */
1611 if ((msg->security & (ENCRYPT | SIGN)))
1612 msg->security ^= INLINE;
1614 msg->security &= ~INLINE;
1617 case 6: /* (f)orget it */
1618 case 7: /* (c)lear */
1625 if (! (msg->security & (ENCRYPT | SIGN)))
1628 msg->security |= APPLICATION_PGP;
1631 return (msg->security);
1635 #endif /* CRYPT_BACKEND_CLASSIC_PGP */