2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
5 * Copyright (C) 2004 g10 Code GmbH
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
13 * This file contains all of the PGP routines necessary to sign, encrypt,
14 * verify and decrypt PGP messages in either the new PGP/MIME format, or
15 * in the older Application/Pgp format. It also contains some code to
16 * cache the user's passphrase for repeat use when decrypting or signing
25 #include "mutt_curses.h"
34 #include "lib/debug.h"
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h>
52 #ifdef HAVE_SYS_RESOURCE_H
53 # include <sys/resource.h>
56 #ifdef CRYPT_BACKEND_CLASSIC_PGP
58 #include "mutt_crypt.h"
59 #include "mutt_menu.h"
62 char PgpPass[LONG_STRING];
63 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
65 void pgp_void_passphrase (void)
67 memset (PgpPass, 0, sizeof (PgpPass));
71 int pgp_valid_passphrase (void)
73 time_t now = time (NULL);
75 if (pgp_use_gpg_agent ()) {
77 return 1; /* handled by gpg-agent */
81 /* Use cached copy. */
84 pgp_void_passphrase ();
86 if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
87 sizeof (PgpPass), M_PASS) == 0) {
88 PgpExptime = time (NULL) + PgpTimeout;
97 void pgp_forget_passphrase (void)
99 pgp_void_passphrase ();
100 mutt_message _("PGP passphrase forgotten.");
103 int pgp_use_gpg_agent (void)
105 return option (OPTUSEGPGAGENT) && getenv ("GPG_TTY")
106 && getenv ("GPG_AGENT_INFO");
109 char *pgp_keyid (pgp_key_t k)
111 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
114 return _pgp_keyid (k);
117 char *_pgp_keyid (pgp_key_t k)
119 if (option (OPTPGPLONGIDS))
122 return (k->keyid + 8);
125 /* ----------------------------------------------------------------------------
126 * Routines for handing PGP input.
131 /* Copy PGP output messages and look for signs of a good signature */
133 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
137 if (PgpGoodSign.pattern) {
142 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
143 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
144 debug_print (2, ("\"%s\" matches regexp.\n", line));
148 debug_print (2, ("\"%s\" doesn't match regexp.\n", line));
150 if (strncmp (line, "[GNUPG:] ", 9) == 0)
158 debug_print (2, ("No pattern.\n"));
159 mutt_copy_stream (fpin, fpout);
167 * Copy a clearsigned message, and strip the signature and PGP's
170 * XXX - charset handling: We assume that it is safe to do
171 * character set decoding first, dash decoding second here, while
172 * we do it the other way around in the main handler.
174 * (Note that we aren't worse than Outlook &c in this, and also
175 * note that we can successfully handle anything produced by any
176 * existing versions of mutt.)
179 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
181 char buf[HUGE_STRING];
182 short complete, armor_header;
188 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
190 for (complete = 1, armor_header = 1;
191 fgetconvs (buf, sizeof (buf), fc) != NULL;
192 complete = strchr (buf, '\n') != NULL) {
199 if (str_cmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
203 char *p = str_skip_initws (buf);
211 state_puts (s->prefix, s);
213 if (buf[0] == '-' && buf[1] == ' ')
214 state_puts (buf + 2, s);
219 fgetconv_close (&fc);
223 /* Support for the Application/PGP Content Type. */
225 void pgp_application_pgp_handler (BODY * m, STATE * s)
227 int needpass = -1, pgp_keyblock = 0;
228 int clearsign = 0, rv, rc;
230 long bytes, last_pos, offset;
231 char buf[HUGE_STRING];
232 char outfile[_POSIX_PATH_MAX];
233 char tmpfname[_POSIX_PATH_MAX];
234 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
238 short maybe_goodsig = 1;
239 short have_any_sigs = 0;
241 char body_charset[STRING];
243 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
245 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
247 fseek (s->fpin, m->offset, 0);
248 last_pos = m->offset;
250 for (bytes = m->length; bytes > 0;) {
251 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
254 offset = ftell (s->fpin);
255 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
258 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
260 start_pos = last_pos;
262 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
264 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
268 else if (!option (OPTDONTHANDLEPGPKEYS) &&
269 str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
274 /* XXX - we may wish to recode here */
276 state_puts (s->prefix, s);
281 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
283 /* Copy PGP material to temporary file */
284 mutt_mktemp (tmpfname);
285 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
286 mutt_perror (tmpfname);
291 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
292 offset = ftell (s->fpin);
293 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
299 && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
301 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
302 || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
307 /* leave tmpfp open in case we still need it - but flush it! */
311 /* Invoke PGP if needed */
312 if (!clearsign || (s->flags & M_VERIFY)) {
313 mutt_mktemp (outfile);
314 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
315 mutt_perror (tmpfname);
319 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
320 fileno (pgpout), -1, tmpfname,
322 safe_fclose (&pgpout);
327 ("[-- Error: unable to create PGP subprocess! --]\n"),
330 else { /* PGP started successfully */
333 if (!pgp_valid_passphrase ())
334 pgp_void_passphrase ();
335 if (pgp_use_gpg_agent ())
337 fprintf (pgpin, "%s\n", PgpPass);
340 safe_fclose (&pgpin);
342 if (s->flags & M_DISPLAY) {
343 crypt_current_time (s, "PGP");
344 rc = pgp_copy_checksig (pgperr, s->fpout);
347 safe_fclose (&pgperr);
348 rv = mutt_wait_filter (thepid);
350 if (s->flags & M_DISPLAY) {
355 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
356 * Sig _is_ correct if
357 * gpg_good_sign="" && pgp_decode_command returned 0
362 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
367 /* treat empty result as sign of failure */
369 if (! ftell(pgpout)) {
370 mutt_error _("Could not decrypt PGP message");
375 * Now, copy cleartext to the screen. NOTE - we expect that PGP
376 * outputs utf-8 cleartext. This may not always be true, but it
377 * seems to be a reasonable guess.
380 if (s->flags & M_DISPLAY) {
382 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
383 else if (pgp_keyblock)
384 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
386 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
392 pgp_copy_clearsigned (tmpfp, s, body_charset);
399 state_set_prefix (s);
400 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
401 while ((c = fgetconv (fc)) != EOF)
402 state_prefix_putc (c, s);
403 fgetconv_close (&fc);
406 if (s->flags & M_DISPLAY) {
407 state_putc ('\n', s);
409 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
410 mutt_message _("PGP message successfully decrypted.");
412 else if (pgp_keyblock)
413 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
415 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
420 /* XXX - we may wish to recode here */
422 state_puts (s->prefix, s);
428 m->goodsig = (maybe_goodsig && have_any_sigs);
431 safe_fclose (&tmpfp);
432 mutt_unlink (tmpfname);
435 safe_fclose (&pgpout);
436 mutt_unlink (outfile);
439 if (needpass == -1) {
441 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
447 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
450 char tempfile[_POSIX_PATH_MAX];
451 char buf[HUGE_STRING];
458 if (b->type != TYPETEXT)
461 if (tagged_only && !b->tagged)
464 mutt_mktemp (tempfile);
465 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
470 if ((tfp = fopen (tempfile, "r")) == NULL) {
475 while (fgets (buf, sizeof (buf), tfp)) {
476 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
477 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
479 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
481 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
488 if (!enc && !sgn && !key)
491 /* fix the content type */
493 mutt_set_parameter ("format", "fixed", &b->parameter);
495 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
497 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
499 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
504 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
509 for (; b; b = b->next) {
510 if (is_multipart (b))
511 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
512 else if (b->type == TYPETEXT) {
513 if ((r = mutt_is_application_pgp (b)))
516 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
527 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
529 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
530 FILE *fp, *pgpout, *pgperr;
535 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
537 if (!(fp = safe_fopen (sigfile, "w"))) {
538 mutt_perror (sigfile);
542 fseek (s->fpin, sigbdy->offset, 0);
543 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
546 mutt_mktemp (pgperrfile);
547 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
548 mutt_perror (pgperrfile);
553 crypt_current_time (s, "PGP");
555 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
556 -1, -1, fileno (pgperr),
557 tempfile, sigfile)) != -1) {
558 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
562 safe_fclose (&pgpout);
566 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
569 if ((rv = mutt_wait_filter (thepid)))
572 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
575 safe_fclose (&pgperr);
577 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
579 mutt_unlink (sigfile);
580 mutt_unlink (pgperrfile);
582 debug_print (1, ("returning %d.\n", badsig));
588 /* Extract pgp public keys from messages or attachments */
590 void pgp_extract_keys_from_messages (HEADER * h)
593 char tempfname[_POSIX_PATH_MAX];
597 mutt_parse_mime_message (Context, h);
598 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
602 mutt_mktemp (tempfname);
603 if (!(fpout = safe_fopen (tempfname, "w"))) {
604 mutt_perror (tempfname);
608 set_option (OPTDONTHANDLEPGPKEYS);
611 for (i = 0; i < Context->vcount; i++) {
612 if (Context->hdrs[Context->v2r[i]]->tagged) {
613 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
614 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
615 && !pgp_valid_passphrase ()) {
619 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
620 M_CM_DECODE | M_CM_CHARCONV, 0);
625 mutt_parse_mime_message (Context, h);
626 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
630 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
635 pgp_invoke_import (tempfname);
636 mutt_any_key_to_continue (NULL);
640 mutt_unlink (tempfname);
641 unset_option (OPTDONTHANDLEPGPKEYS);
645 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
649 char tempfname[_POSIX_PATH_MAX];
651 mutt_mktemp (tempfname);
652 if (!(tempfp = safe_fopen (tempfname, "w"))) {
653 mutt_perror (tempfname);
657 memset (&s, 0, sizeof (STATE));
662 mutt_body_handler (top, &s);
666 pgp_invoke_import (tempfname);
667 mutt_any_key_to_continue (NULL);
669 mutt_unlink (tempfname);
672 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
675 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
681 set_option (OPTDONTHANDLEPGPKEYS);
683 for (; top; top = top->next) {
684 if (!tag || top->tagged)
685 pgp_extract_keys_from_attachment (fp, top);
691 unset_option (OPTDONTHANDLEPGPKEYS);
694 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
696 char buf[LONG_STRING];
697 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
701 char pgperrfile[_POSIX_PATH_MAX];
702 char pgptmpfile[_POSIX_PATH_MAX];
706 mutt_mktemp (pgperrfile);
707 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
708 mutt_perror (pgperrfile);
713 mutt_mktemp (pgptmpfile);
714 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
715 mutt_perror (pgptmpfile);
720 /* Position the stream at the beginning of the body, and send the data to
721 * the temporary file.
724 fseek (s->fpin, a->offset, 0);
725 mutt_copy_bytes (s->fpin, pgptmp, a->length);
728 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
729 fileno (pgperr), pgptmpfile)) == -1) {
732 if (s->flags & M_DISPLAY)
734 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
739 /* send the PGP passphrase to the subprocess. Never do this if the
740 agent is active, because this might lead to a passphrase send as
742 if (!pgp_use_gpg_agent ())
743 fputs (PgpPass, pgpin);
747 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
748 * read_mime_header has a hard time parsing the message.
750 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
752 if (len > 1 && buf[len - 2] == '\r')
753 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
758 rv = mutt_wait_filter (thepid);
759 mutt_unlink (pgptmpfile);
761 if (s->flags & M_DISPLAY) {
764 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
768 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
775 if (fgetc (fpout) == EOF)
780 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
782 * Need to set the length of this body part.
784 fstat (fileno (fpout), &info);
785 tattach->length = info.st_size - tattach->offset;
787 /* See if we need to recurse on this MIME part. */
789 mutt_parse_part (fpout, tattach);
795 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
797 char tempfile[_POSIX_PATH_MAX];
801 if (!mutt_is_multipart_encrypted (b))
804 if (!b->parts || !b->parts->next)
809 memset (&s, 0, sizeof (s));
811 mutt_mktemp (tempfile);
812 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
813 mutt_perror (tempfile);
818 *cur = pgp_decrypt_part (b, &s, *fpout, p);
828 void pgp_encrypted_handler (BODY * a, STATE * s)
830 char tempfile[_POSIX_PATH_MAX];
836 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
837 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
838 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
839 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
840 if (s->flags & M_DISPLAY)
841 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
847 * Move forward to the application/pgp-encrypted body.
851 mutt_mktemp (tempfile);
852 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
853 if (s->flags & M_DISPLAY)
855 ("[-- Error: could not create temporary file! --]\n"),
860 if (s->flags & M_DISPLAY)
861 crypt_current_time (s, "PGP");
863 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
864 if (s->flags & M_DISPLAY)
866 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
871 mutt_body_handler (tattach, s);
875 * if a multipart/signed is the _only_ sub-part of a
876 * multipart/encrypted, cache signature verification
881 if (mutt_is_multipart_signed (tattach) && !tattach->next)
882 p->goodsig |= tattach->goodsig;
884 if (s->flags & M_DISPLAY) {
885 state_puts ("\n", s);
886 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
889 mutt_free_body (&tattach);
890 /* clear 'Invoking...' message, since there's no error */
891 mutt_message _("PGP message successfully decrypted.");
893 mutt_error _("Could not decrypt PGP message");
896 mutt_unlink (tempfile);
899 /* ----------------------------------------------------------------------------
900 * Routines for sending PGP/MIME messages.
904 BODY *pgp_sign_message (BODY * a)
907 char buffer[LONG_STRING];
908 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
909 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
914 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
916 mutt_mktemp (sigfile);
917 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
921 mutt_mktemp (signedfile);
922 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
923 mutt_perror (signedfile);
929 mutt_write_mime_header (a, sfp);
931 mutt_write_mime_body (a, sfp);
934 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
935 -1, -1, -1, signedfile)) == -1) {
936 mutt_perror (_("Can't open PGP subprocess!"));
944 if (!pgp_use_gpg_agent ())
945 fputs (PgpPass, pgpin);
950 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
951 * recommended for future releases of PGP.
953 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
954 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
955 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
956 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
957 fputs ("-----END PGP SIGNATURE-----\n", fp);
960 empty = 0; /* got some output, so we're ok */
963 /* check for errors from PGP */
965 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
967 fputs (buffer, stdout);
970 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
977 if (fclose (fp) != 0) {
978 mutt_perror ("fclose");
984 pgp_void_passphrase ();
985 mutt_any_key_to_continue (NULL);
990 return (NULL); /* fatal error while signing */
993 t = mutt_new_body ();
994 t->type = TYPEMULTIPART;
995 t->subtype = str_dup ("signed");
996 t->encoding = ENC7BIT;
998 t->disposition = DISPINLINE;
1000 mutt_generate_boundary (&t->parameter);
1001 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1002 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1007 t->parts->next = mutt_new_body ();
1009 t->type = TYPEAPPLICATION;
1010 t->subtype = str_dup ("pgp-signature");
1011 t->filename = str_dup (sigfile);
1013 t->disposition = DISPINLINE;
1014 t->encoding = ENC7BIT;
1015 t->unlink = 1; /* ok to remove this file after sending. */
1020 static short is_numerical_keyid (const char *s)
1022 /* or should we require the "0x"? */
1023 if (strncmp (s, "0x", 2) == 0)
1025 if (str_len (s) % 8)
1028 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1034 /* This routine attempts to find the keyids of the recipients of a message.
1035 * It returns NULL if any of the keys can not be found.
1037 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1039 char *keyID, *keylist = NULL, *t;
1040 size_t keylist_size = 0;
1041 size_t keylist_used = 0;
1042 ADDRESS *tmp = NULL, *addr = NULL;
1043 ADDRESS **last = &tmp;
1046 pgp_key_t k_info = NULL, key = NULL;
1048 const char *fqdn = mutt_fqdn (1);
1050 for (i = 0; i < 3; i++) {
1065 *last = rfc822_cpy_adr (p);
1067 last = &((*last)->next);
1071 rfc822_qualify (tmp, fqdn);
1073 tmp = mutt_remove_duplicates (tmp);
1075 for (p = tmp; p; p = p->next) {
1076 char buf[LONG_STRING];
1081 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1084 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1086 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1087 if (is_numerical_keyid (keyID)) {
1088 if (strncmp (keyID, "0x", 2) == 0)
1090 goto bypass_selection; /* you don't see this. */
1093 /* check for e-mail address */
1094 if ((t = strchr (keyID, '@')) &&
1095 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1097 rfc822_qualify (addr, fqdn);
1101 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1104 mem_free (&keylist);
1105 rfc822_free_address (&tmp);
1106 rfc822_free_address (&addr);
1112 pgp_invoke_getkeys (q);
1116 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1117 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1119 if ((key = pgp_ask_for_key (buf, q->mailbox,
1120 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1121 mem_free (&keylist);
1122 rfc822_free_address (&tmp);
1123 rfc822_free_address (&addr);
1130 keyID = pgp_keyid (key);
1133 keylist_size += str_len (keyID) + 4;
1134 mem_realloc (&keylist, keylist_size);
1135 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1137 keylist_used = str_len (keylist);
1139 pgp_free_key (&key);
1140 rfc822_free_address (&addr);
1143 rfc822_free_address (&tmp);
1147 /* Warning: "a" is no longer freed in this routine, you need
1148 * to free it later. This is necessary for $fcc_attach. */
1150 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1152 char buf[LONG_STRING];
1153 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1154 char pgpinfile[_POSIX_PATH_MAX];
1155 FILE *pgpin, *pgperr, *fpout, *fptmp;
1161 mutt_mktemp (tempfile);
1162 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1163 mutt_perror (tempfile);
1167 mutt_mktemp (pgperrfile);
1168 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1169 mutt_perror (pgperrfile);
1174 unlink (pgperrfile);
1176 mutt_mktemp (pgpinfile);
1177 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1178 mutt_perror (pgpinfile);
1186 convert_to_7bit (a);
1188 mutt_write_mime_header (a, fptmp);
1189 fputc ('\n', fptmp);
1190 mutt_write_mime_body (a, fptmp);
1193 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1194 fileno (fpout), fileno (pgperr),
1195 pgpinfile, keylist, sign)) == -1) {
1202 if (!pgp_use_gpg_agent ())
1203 fputs (PgpPass, pgpin);
1204 fputc ('\n', pgpin);
1208 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1216 empty = (fgetc (fpout) == EOF);
1221 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1223 fputs (buf, stdout);
1227 /* pause if there is any error output from PGP */
1229 mutt_any_key_to_continue (NULL);
1232 /* fatal error while trying to encrypt message */
1234 pgp_void_passphrase (); /* just in case */
1239 t = mutt_new_body ();
1240 t->type = TYPEMULTIPART;
1241 t->subtype = str_dup ("encrypted");
1242 t->encoding = ENC7BIT;
1244 t->disposition = DISPINLINE;
1246 mutt_generate_boundary (&t->parameter);
1247 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1249 t->parts = mutt_new_body ();
1250 t->parts->type = TYPEAPPLICATION;
1251 t->parts->subtype = str_dup ("pgp-encrypted");
1252 t->parts->encoding = ENC7BIT;
1254 t->parts->next = mutt_new_body ();
1255 t->parts->next->type = TYPEAPPLICATION;
1256 t->parts->next->subtype = str_dup ("octet-stream");
1257 t->parts->next->encoding = ENC7BIT;
1258 t->parts->next->filename = str_dup (tempfile);
1259 t->parts->next->use_disp = 1;
1260 t->parts->next->disposition = DISPINLINE;
1261 t->parts->next->unlink = 1; /* delete after sending the message */
1262 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1267 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1271 char pgpoutfile[_POSIX_PATH_MAX];
1272 char pgperrfile[_POSIX_PATH_MAX];
1273 char pgpinfile[_POSIX_PATH_MAX];
1275 char body_charset[STRING];
1277 const char *send_charset;
1279 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1289 if (a->type != TYPETEXT)
1291 if (ascii_strcasecmp (a->subtype, "plain"))
1294 if ((fp = fopen (a->filename, "r")) == NULL) {
1295 mutt_perror (a->filename);
1299 mutt_mktemp (pgpinfile);
1300 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1301 mutt_perror (pgpinfile);
1306 /* The following code is really correct: If noconv is set,
1307 * a's charset parameter contains the on-disk character set, and
1308 * we have to convert from that to utf-8. If noconv is not set,
1309 * we have to convert from $charset to utf-8.
1312 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1314 from_charset = body_charset;
1316 from_charset = Charset;
1318 if (!mutt_is_us_ascii (body_charset)) {
1322 if (flags & ENCRYPT)
1323 send_charset = "us-ascii";
1325 send_charset = "utf-8";
1327 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1328 while ((c = fgetconv (fc)) != EOF)
1331 fgetconv_close (&fc);
1334 send_charset = "us-ascii";
1335 mutt_copy_stream (fp, pgpin);
1340 mutt_mktemp (pgpoutfile);
1341 mutt_mktemp (pgperrfile);
1342 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1343 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1344 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1348 unlink (pgpoutfile);
1353 unlink (pgperrfile);
1355 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1356 -1, fileno (pgpout), fileno (pgperr),
1357 pgpinfile, keylist, flags)) == -1) {
1358 mutt_perror (_("Can't invoke PGP"));
1362 mutt_unlink (pgpinfile);
1363 unlink (pgpoutfile);
1367 if (pgp_use_gpg_agent ())
1370 fprintf (pgpin, "%s\n", PgpPass);
1373 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1376 mutt_unlink (pgpinfile);
1385 empty = (fgetc (pgpout) == EOF);
1390 while (fgets (buff, sizeof (buff), pgperr)) {
1392 fputs (buff, stdout);
1398 mutt_any_key_to_continue (NULL);
1402 pgp_void_passphrase (); /* just in case */
1403 unlink (pgpoutfile);
1407 b = mutt_new_body ();
1409 b->encoding = ENC7BIT;
1412 b->subtype = str_dup ("plain");
1414 mutt_set_parameter ("x-action",
1415 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1417 mutt_set_parameter ("charset", send_charset, &b->parameter);
1419 b->filename = str_dup (pgpoutfile);
1422 /* The following is intended to give a clue to some completely brain-dead
1423 * "mail environments" which are typically used by large corporations.
1426 b->d_filename = str_dup ("msg.pgp");
1431 b->disposition = DISPINLINE;
1437 if (!(flags & ENCRYPT))
1438 b->encoding = a->encoding;
1443 int pgp_send_menu (HEADER * msg, int *redraw)
1446 char input_signas[SHORT_STRING];
1448 char prompt[LONG_STRING];
1450 if (!(WithCrypto & APPLICATION_PGP))
1451 return msg->security;
1453 /* If autoinline and no crypto options set, then set inline. */
1454 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1455 && (msg->security & (SIGN | ENCRYPT))))
1456 msg->security |= INLINE;
1458 snprintf (prompt, sizeof (prompt),
1459 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1460 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1462 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1463 case 1: /* (e)ncrypt */
1464 msg->security |= ENCRYPT;
1465 msg->security &= ~SIGN;
1468 case 2: /* (s)ign */
1469 msg->security |= SIGN;
1470 msg->security &= ~ENCRYPT;
1473 case 3: /* sign (a)s */
1474 unset_option (OPTPGPCHECKTRUST);
1477 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1479 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1480 str_replace (&PgpSignAs, input_signas);
1483 msg->security |= SIGN;
1485 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1489 msg->security &= ~SIGN;
1493 *redraw = REDRAW_FULL;
1496 case 4: /* (b)oth */
1497 msg->security |= (ENCRYPT | SIGN);
1500 case 5: /* (i)nline */
1501 if ((msg->security & (ENCRYPT | SIGN)))
1502 msg->security ^= INLINE;
1504 msg->security &= ~INLINE;
1507 case 6: /* (f)orget it */
1508 case 7: /* (c)lear */
1513 if (msg->security) {
1514 if (!(msg->security & (ENCRYPT | SIGN)))
1517 msg->security |= APPLICATION_PGP;
1520 return (msg->security);
1524 #endif /* CRYPT_BACKEND_CLASSIC_PGP */