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
28 #include "mutt_curses.h"
37 #include "lib/debug.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 ()) {
80 return 1; /* handled by gpg-agent */
84 /* Use cached copy. */
87 pgp_void_passphrase ();
89 if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
90 sizeof (PgpPass), M_PASS) == 0) {
91 PgpExptime = time (NULL) + PgpTimeout;
100 void pgp_forget_passphrase (void)
102 pgp_void_passphrase ();
103 mutt_message _("PGP passphrase forgotten.");
106 int pgp_use_gpg_agent (void)
108 return option (OPTUSEGPGAGENT) && getenv ("GPG_TTY")
109 && getenv ("GPG_AGENT_INFO");
112 char *pgp_keyid (pgp_key_t k)
114 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
117 return _pgp_keyid (k);
120 char *_pgp_keyid (pgp_key_t k)
122 if (option (OPTPGPLONGIDS))
125 return (k->keyid + 8);
128 /* ----------------------------------------------------------------------------
129 * Routines for handing PGP input.
134 /* Copy PGP output messages and look for signs of a good signature */
136 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
140 if (PgpGoodSign.pattern) {
145 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
146 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
147 debug_print (2, ("\"%s\" matches regexp.\n", line));
151 debug_print (2, ("\"%s\" doesn't match regexp.\n", line));
153 if (strncmp (line, "[GNUPG:] ", 9) == 0)
161 debug_print (2, ("No pattern.\n"));
162 mutt_copy_stream (fpin, fpout);
170 * Copy a clearsigned message, and strip the signature and PGP's
173 * XXX - charset handling: We assume that it is safe to do
174 * character set decoding first, dash decoding second here, while
175 * we do it the other way around in the main handler.
177 * (Note that we aren't worse than Outlook &c in this, and also
178 * note that we can successfully handle anything produced by any
179 * existing versions of mutt.)
182 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
184 char buf[HUGE_STRING];
185 short complete, armor_header;
191 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
193 for (complete = 1, armor_header = 1;
194 fgetconvs (buf, sizeof (buf), fc) != NULL;
195 complete = strchr (buf, '\n') != NULL) {
202 if (str_cmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
206 char *p = str_skip_initws (buf);
214 state_puts (s->prefix, s);
216 if (buf[0] == '-' && buf[1] == ' ')
217 state_puts (buf + 2, s);
222 fgetconv_close (&fc);
226 /* Support for the Application/PGP Content Type. */
228 void pgp_application_pgp_handler (BODY * m, STATE * s)
230 int needpass = -1, pgp_keyblock = 0;
231 int clearsign = 0, rv, rc;
233 long bytes, last_pos, offset;
234 char buf[HUGE_STRING];
235 char outfile[_POSIX_PATH_MAX];
236 char tmpfname[_POSIX_PATH_MAX];
237 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
241 short maybe_goodsig = 1;
242 short have_any_sigs = 0;
244 char body_charset[STRING];
246 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
248 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
250 fseek (s->fpin, m->offset, 0);
251 last_pos = m->offset;
253 for (bytes = m->length; bytes > 0;) {
254 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
257 offset = ftell (s->fpin);
258 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
261 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
263 start_pos = last_pos;
265 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
267 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
271 else if (!option (OPTDONTHANDLEPGPKEYS) &&
272 str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
277 /* XXX - we may wish to recode here */
279 state_puts (s->prefix, s);
284 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
286 /* Copy PGP material to temporary file */
287 mutt_mktemp (tmpfname);
288 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
289 mutt_perror (tmpfname);
294 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
295 offset = ftell (s->fpin);
296 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
302 && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
304 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
305 || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
310 /* leave tmpfp open in case we still need it - but flush it! */
314 /* Invoke PGP if needed */
315 if (!clearsign || (s->flags & M_VERIFY)) {
316 mutt_mktemp (outfile);
317 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
318 mutt_perror (tmpfname);
322 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
323 fileno (pgpout), -1, tmpfname,
325 safe_fclose (&pgpout);
330 ("[-- Error: unable to create PGP subprocess! --]\n"),
333 else { /* PGP started successfully */
336 if (!pgp_valid_passphrase ())
337 pgp_void_passphrase ();
338 if (pgp_use_gpg_agent ())
340 fprintf (pgpin, "%s\n", PgpPass);
343 safe_fclose (&pgpin);
345 if (s->flags & M_DISPLAY) {
346 crypt_current_time (s, "PGP");
347 rc = pgp_copy_checksig (pgperr, s->fpout);
350 safe_fclose (&pgperr);
351 rv = mutt_wait_filter (thepid);
353 if (s->flags & M_DISPLAY) {
358 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
359 * Sig _is_ correct if
360 * gpg_good_sign="" && pgp_decode_command returned 0
365 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
369 /* treat empty result as sign of failure */
370 if (!(pgpout && ftell (pgpout))) {
371 mutt_error _("Could not decrypt PGP message");
377 * Now, copy cleartext to the screen. NOTE - we expect that PGP
378 * outputs utf-8 cleartext. This may not always be true, but it
379 * seems to be a reasonable guess.
382 if (s->flags & M_DISPLAY) {
384 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
385 else if (pgp_keyblock)
386 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
388 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
394 pgp_copy_clearsigned (tmpfp, s, body_charset);
401 state_set_prefix (s);
402 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
403 while ((c = fgetconv (fc)) != EOF)
404 state_prefix_putc (c, s);
405 fgetconv_close (&fc);
408 if (s->flags & M_DISPLAY) {
409 state_putc ('\n', s);
411 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
412 mutt_message _("PGP message successfully decrypted.");
414 else if (pgp_keyblock)
415 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
417 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
422 /* XXX - we may wish to recode here */
424 state_puts (s->prefix, s);
430 m->goodsig = (maybe_goodsig && have_any_sigs);
433 safe_fclose (&tmpfp);
434 mutt_unlink (tmpfname);
437 safe_fclose (&pgpout);
438 mutt_unlink (outfile);
441 if (needpass == -1) {
443 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
449 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
452 char tempfile[_POSIX_PATH_MAX];
453 char buf[HUGE_STRING];
460 if (b->type != TYPETEXT)
463 if (tagged_only && !b->tagged)
466 mutt_mktemp (tempfile);
467 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
472 if ((tfp = fopen (tempfile, "r")) == NULL) {
477 while (fgets (buf, sizeof (buf), tfp)) {
478 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
479 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
481 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
483 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
490 if (!enc && !sgn && !key)
493 /* fix the content type */
495 mutt_set_parameter ("format", "fixed", &b->parameter);
497 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
499 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
501 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
506 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
511 for (; b; b = b->next) {
512 if (is_multipart (b))
513 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
514 else if (b->type == TYPETEXT) {
515 if ((r = mutt_is_application_pgp (b)))
518 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
529 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
531 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
532 FILE *fp, *pgpout, *pgperr;
537 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
539 if (!(fp = safe_fopen (sigfile, "w"))) {
540 mutt_perror (sigfile);
544 fseek (s->fpin, sigbdy->offset, 0);
545 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
548 mutt_mktemp (pgperrfile);
549 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
550 mutt_perror (pgperrfile);
555 crypt_current_time (s, "PGP");
557 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
558 -1, -1, fileno (pgperr),
559 tempfile, sigfile)) != -1) {
560 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
564 safe_fclose (&pgpout);
568 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
571 if ((rv = mutt_wait_filter (thepid)))
574 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
577 safe_fclose (&pgperr);
579 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
581 mutt_unlink (sigfile);
582 mutt_unlink (pgperrfile);
584 debug_print (1, ("returning %d.\n", badsig));
590 /* Extract pgp public keys from messages or attachments */
592 void pgp_extract_keys_from_messages (HEADER * h)
595 char tempfname[_POSIX_PATH_MAX];
599 mutt_parse_mime_message (Context, h);
600 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
604 mutt_mktemp (tempfname);
605 if (!(fpout = safe_fopen (tempfname, "w"))) {
606 mutt_perror (tempfname);
610 set_option (OPTDONTHANDLEPGPKEYS);
613 for (i = 0; i < Context->vcount; i++) {
614 if (Context->hdrs[Context->v2r[i]]->tagged) {
615 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
616 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
617 && !pgp_valid_passphrase ()) {
621 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
622 M_CM_DECODE | M_CM_CHARCONV, 0);
627 mutt_parse_mime_message (Context, h);
628 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
632 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
637 pgp_invoke_import (tempfname);
638 mutt_any_key_to_continue (NULL);
642 mutt_unlink (tempfname);
643 unset_option (OPTDONTHANDLEPGPKEYS);
647 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
651 char tempfname[_POSIX_PATH_MAX];
653 mutt_mktemp (tempfname);
654 if (!(tempfp = safe_fopen (tempfname, "w"))) {
655 mutt_perror (tempfname);
659 memset (&s, 0, sizeof (STATE));
664 mutt_body_handler (top, &s);
668 pgp_invoke_import (tempfname);
669 mutt_any_key_to_continue (NULL);
671 mutt_unlink (tempfname);
674 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
677 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
683 set_option (OPTDONTHANDLEPGPKEYS);
685 for (; top; top = top->next) {
686 if (!tag || top->tagged)
687 pgp_extract_keys_from_attachment (fp, top);
693 unset_option (OPTDONTHANDLEPGPKEYS);
696 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
698 char buf[LONG_STRING];
699 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
703 char pgperrfile[_POSIX_PATH_MAX];
704 char pgptmpfile[_POSIX_PATH_MAX];
708 mutt_mktemp (pgperrfile);
709 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
710 mutt_perror (pgperrfile);
715 mutt_mktemp (pgptmpfile);
716 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
717 mutt_perror (pgptmpfile);
722 /* Position the stream at the beginning of the body, and send the data to
723 * the temporary file.
726 fseek (s->fpin, a->offset, 0);
727 mutt_copy_bytes (s->fpin, pgptmp, a->length);
730 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
731 fileno (pgperr), pgptmpfile)) == -1) {
734 if (s->flags & M_DISPLAY)
736 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
741 /* send the PGP passphrase to the subprocess. Never do this if the
742 agent is active, because this might lead to a passphrase send as
744 if (!pgp_use_gpg_agent ())
745 fputs (PgpPass, pgpin);
749 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
750 * read_mime_header has a hard time parsing the message.
752 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
754 if (len > 1 && buf[len - 2] == '\r')
755 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
760 rv = mutt_wait_filter (thepid);
761 mutt_unlink (pgptmpfile);
763 if (s->flags & M_DISPLAY) {
766 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
770 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
777 if (fgetc (fpout) == EOF)
782 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
784 * Need to set the length of this body part.
786 fstat (fileno (fpout), &info);
787 tattach->length = info.st_size - tattach->offset;
789 /* See if we need to recurse on this MIME part. */
791 mutt_parse_part (fpout, tattach);
797 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
799 char tempfile[_POSIX_PATH_MAX];
803 if (!mutt_is_multipart_encrypted (b))
806 if (!b->parts || !b->parts->next)
811 memset (&s, 0, sizeof (s));
813 mutt_mktemp (tempfile);
814 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
815 mutt_perror (tempfile);
820 *cur = pgp_decrypt_part (b, &s, *fpout, p);
830 void pgp_encrypted_handler (BODY * a, STATE * s)
832 char tempfile[_POSIX_PATH_MAX];
838 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
839 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
840 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
841 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
842 if (s->flags & M_DISPLAY)
843 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
849 * Move forward to the application/pgp-encrypted body.
853 mutt_mktemp (tempfile);
854 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
855 if (s->flags & M_DISPLAY)
857 ("[-- Error: could not create temporary file! --]\n"),
862 if (s->flags & M_DISPLAY)
863 crypt_current_time (s, "PGP");
865 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
866 if (s->flags & M_DISPLAY)
868 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
873 mutt_body_handler (tattach, s);
877 * if a multipart/signed is the _only_ sub-part of a
878 * multipart/encrypted, cache signature verification
883 if (mutt_is_multipart_signed (tattach) && !tattach->next)
884 p->goodsig |= tattach->goodsig;
886 if (s->flags & M_DISPLAY) {
887 state_puts ("\n", s);
888 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
891 mutt_free_body (&tattach);
892 /* clear 'Invoking...' message, since there's no error */
893 mutt_message _("PGP message successfully decrypted.");
895 mutt_error _("Could not decrypt PGP message");
898 mutt_unlink (tempfile);
901 /* ----------------------------------------------------------------------------
902 * Routines for sending PGP/MIME messages.
906 BODY *pgp_sign_message (BODY * a)
909 char buffer[LONG_STRING];
910 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
911 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
916 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
918 mutt_mktemp (sigfile);
919 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
923 mutt_mktemp (signedfile);
924 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
925 mutt_perror (signedfile);
931 mutt_write_mime_header (a, sfp);
933 mutt_write_mime_body (a, sfp);
936 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
937 -1, -1, -1, signedfile)) == -1) {
938 mutt_perror (_("Can't open PGP subprocess!"));
946 if (!pgp_use_gpg_agent ())
947 fputs (PgpPass, pgpin);
952 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
953 * recommended for future releases of PGP.
955 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
956 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
957 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
958 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
959 fputs ("-----END PGP SIGNATURE-----\n", fp);
962 empty = 0; /* got some output, so we're ok */
965 /* check for errors from PGP */
967 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
969 fputs (buffer, stdout);
972 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
979 if (fclose (fp) != 0) {
980 mutt_perror ("fclose");
986 pgp_void_passphrase ();
987 mutt_any_key_to_continue (NULL);
992 return (NULL); /* fatal error while signing */
995 t = mutt_new_body ();
996 t->type = TYPEMULTIPART;
997 t->subtype = str_dup ("signed");
998 t->encoding = ENC7BIT;
1000 t->disposition = DISPINLINE;
1002 mutt_generate_boundary (&t->parameter);
1003 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1004 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1009 t->parts->next = mutt_new_body ();
1011 t->type = TYPEAPPLICATION;
1012 t->subtype = str_dup ("pgp-signature");
1013 t->filename = str_dup (sigfile);
1015 t->disposition = DISPINLINE;
1016 t->encoding = ENC7BIT;
1017 t->unlink = 1; /* ok to remove this file after sending. */
1022 static short is_numerical_keyid (const char *s)
1024 /* or should we require the "0x"? */
1025 if (strncmp (s, "0x", 2) == 0)
1027 if (str_len (s) % 8)
1030 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1036 /* This routine attempts to find the keyids of the recipients of a message.
1037 * It returns NULL if any of the keys can not be found.
1039 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1041 char *keyID, *keylist = NULL, *t;
1042 size_t keylist_size = 0;
1043 size_t keylist_used = 0;
1044 ADDRESS *tmp = NULL, *addr = NULL;
1045 ADDRESS **last = &tmp;
1048 pgp_key_t k_info = NULL, key = NULL;
1050 const char *fqdn = mutt_fqdn (1);
1052 for (i = 0; i < 3; i++) {
1067 *last = rfc822_cpy_adr (p);
1069 last = &((*last)->next);
1073 rfc822_qualify (tmp, fqdn);
1075 tmp = mutt_remove_duplicates (tmp);
1077 for (p = tmp; p; p = p->next) {
1078 char buf[LONG_STRING];
1083 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1086 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1088 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1089 if (is_numerical_keyid (keyID)) {
1090 if (strncmp (keyID, "0x", 2) == 0)
1092 goto bypass_selection; /* you don't see this. */
1095 /* check for e-mail address */
1096 if ((t = strchr (keyID, '@')) &&
1097 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1099 rfc822_qualify (addr, fqdn);
1103 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1106 mem_free (&keylist);
1107 rfc822_free_address (&tmp);
1108 rfc822_free_address (&addr);
1114 pgp_invoke_getkeys (q);
1118 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1119 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1121 if ((key = pgp_ask_for_key (buf, q->mailbox,
1122 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1123 mem_free (&keylist);
1124 rfc822_free_address (&tmp);
1125 rfc822_free_address (&addr);
1132 keyID = pgp_keyid (key);
1135 keylist_size += str_len (keyID) + 4;
1136 mem_realloc (&keylist, keylist_size);
1137 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1139 keylist_used = str_len (keylist);
1141 pgp_free_key (&key);
1142 rfc822_free_address (&addr);
1145 rfc822_free_address (&tmp);
1149 /* Warning: "a" is no longer freed in this routine, you need
1150 * to free it later. This is necessary for $fcc_attach. */
1152 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1154 char buf[LONG_STRING];
1155 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1156 char pgpinfile[_POSIX_PATH_MAX];
1157 FILE *pgpin, *pgperr, *fpout, *fptmp;
1163 mutt_mktemp (tempfile);
1164 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1165 mutt_perror (tempfile);
1169 mutt_mktemp (pgperrfile);
1170 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1171 mutt_perror (pgperrfile);
1176 unlink (pgperrfile);
1178 mutt_mktemp (pgpinfile);
1179 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1180 mutt_perror (pgpinfile);
1188 convert_to_7bit (a);
1190 mutt_write_mime_header (a, fptmp);
1191 fputc ('\n', fptmp);
1192 mutt_write_mime_body (a, fptmp);
1195 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1196 fileno (fpout), fileno (pgperr),
1197 pgpinfile, keylist, sign)) == -1) {
1204 if (!pgp_use_gpg_agent ())
1205 fputs (PgpPass, pgpin);
1206 fputc ('\n', pgpin);
1210 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1218 empty = (fgetc (fpout) == EOF);
1223 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1225 fputs (buf, stdout);
1229 /* pause if there is any error output from PGP */
1231 mutt_any_key_to_continue (NULL);
1234 /* fatal error while trying to encrypt message */
1236 pgp_void_passphrase (); /* just in case */
1241 t = mutt_new_body ();
1242 t->type = TYPEMULTIPART;
1243 t->subtype = str_dup ("encrypted");
1244 t->encoding = ENC7BIT;
1246 t->disposition = DISPINLINE;
1248 mutt_generate_boundary (&t->parameter);
1249 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1251 t->parts = mutt_new_body ();
1252 t->parts->type = TYPEAPPLICATION;
1253 t->parts->subtype = str_dup ("pgp-encrypted");
1254 t->parts->encoding = ENC7BIT;
1256 t->parts->next = mutt_new_body ();
1257 t->parts->next->type = TYPEAPPLICATION;
1258 t->parts->next->subtype = str_dup ("octet-stream");
1259 t->parts->next->encoding = ENC7BIT;
1260 t->parts->next->filename = str_dup (tempfile);
1261 t->parts->next->use_disp = 1;
1262 t->parts->next->disposition = DISPINLINE;
1263 t->parts->next->unlink = 1; /* delete after sending the message */
1264 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1269 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1273 char pgpoutfile[_POSIX_PATH_MAX];
1274 char pgperrfile[_POSIX_PATH_MAX];
1275 char pgpinfile[_POSIX_PATH_MAX];
1277 char body_charset[STRING];
1279 const char *send_charset;
1281 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1291 if (a->type != TYPETEXT)
1293 if (ascii_strcasecmp (a->subtype, "plain"))
1296 if ((fp = fopen (a->filename, "r")) == NULL) {
1297 mutt_perror (a->filename);
1301 mutt_mktemp (pgpinfile);
1302 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1303 mutt_perror (pgpinfile);
1308 /* The following code is really correct: If noconv is set,
1309 * a's charset parameter contains the on-disk character set, and
1310 * we have to convert from that to utf-8. If noconv is not set,
1311 * we have to convert from $charset to utf-8.
1314 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1316 from_charset = body_charset;
1318 from_charset = Charset;
1320 if (!mutt_is_us_ascii (body_charset)) {
1324 if (flags & ENCRYPT)
1325 send_charset = "us-ascii";
1327 send_charset = "utf-8";
1329 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1330 while ((c = fgetconv (fc)) != EOF)
1333 fgetconv_close (&fc);
1336 send_charset = "us-ascii";
1337 mutt_copy_stream (fp, pgpin);
1342 mutt_mktemp (pgpoutfile);
1343 mutt_mktemp (pgperrfile);
1344 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1345 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1346 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1350 unlink (pgpoutfile);
1355 unlink (pgperrfile);
1357 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1358 -1, fileno (pgpout), fileno (pgperr),
1359 pgpinfile, keylist, flags)) == -1) {
1360 mutt_perror (_("Can't invoke PGP"));
1364 mutt_unlink (pgpinfile);
1365 unlink (pgpoutfile);
1369 if (pgp_use_gpg_agent ())
1372 fprintf (pgpin, "%s\n", PgpPass);
1375 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1378 mutt_unlink (pgpinfile);
1387 empty = (fgetc (pgpout) == EOF);
1392 while (fgets (buff, sizeof (buff), pgperr)) {
1394 fputs (buff, stdout);
1400 mutt_any_key_to_continue (NULL);
1404 pgp_void_passphrase (); /* just in case */
1405 unlink (pgpoutfile);
1409 b = mutt_new_body ();
1411 b->encoding = ENC7BIT;
1414 b->subtype = str_dup ("plain");
1416 mutt_set_parameter ("x-action",
1417 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1419 mutt_set_parameter ("charset", send_charset, &b->parameter);
1421 b->filename = str_dup (pgpoutfile);
1424 /* The following is intended to give a clue to some completely brain-dead
1425 * "mail environments" which are typically used by large corporations.
1428 b->d_filename = str_dup ("msg.pgp");
1433 b->disposition = DISPINLINE;
1439 if (!(flags & ENCRYPT))
1440 b->encoding = a->encoding;
1445 int pgp_send_menu (HEADER * msg, int *redraw)
1448 char input_signas[SHORT_STRING];
1450 char prompt[LONG_STRING];
1452 if (!(WithCrypto & APPLICATION_PGP))
1453 return msg->security;
1455 /* If autoinline and no crypto options set, then set inline. */
1456 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1457 && (msg->security & (SIGN | ENCRYPT))))
1458 msg->security |= INLINE;
1460 snprintf (prompt, sizeof (prompt),
1461 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1462 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1464 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1465 case 1: /* (e)ncrypt */
1466 msg->security |= ENCRYPT;
1467 msg->security &= ~SIGN;
1470 case 2: /* (s)ign */
1471 msg->security |= SIGN;
1472 msg->security &= ~ENCRYPT;
1475 case 3: /* sign (a)s */
1476 unset_option (OPTPGPCHECKTRUST);
1479 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1481 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1482 str_replace (&PgpSignAs, input_signas);
1485 msg->security |= SIGN;
1487 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1491 msg->security &= ~SIGN;
1495 *redraw = REDRAW_FULL;
1498 case 4: /* (b)oth */
1499 msg->security |= (ENCRYPT | SIGN);
1502 case 5: /* (i)nline */
1503 if ((msg->security & (ENCRYPT | SIGN)))
1504 msg->security ^= INLINE;
1506 msg->security &= ~INLINE;
1509 case 6: /* (f)orget it */
1510 case 7: /* (c)lear */
1515 if (msg->security) {
1516 if (!(msg->security & (ENCRYPT | SIGN)))
1519 msg->security |= APPLICATION_PGP;
1522 return (msg->security);
1526 #endif /* CRYPT_BACKEND_CLASSIC_PGP */