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 int pgp_application_pgp_handler (BODY * m, STATE * s)
230 int needpass = -1, pgp_keyblock = 0;
232 int clearsign = 0, rv, rc;
234 long bytes, last_pos, offset;
235 char buf[HUGE_STRING];
236 char outfile[_POSIX_PATH_MAX];
237 char tmpfname[_POSIX_PATH_MAX];
238 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
242 short maybe_goodsig = 1;
243 short have_any_sigs = 0;
245 char body_charset[STRING];
247 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
249 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
251 fseek (s->fpin, m->offset, 0);
252 last_pos = m->offset;
254 for (bytes = m->length; bytes > 0;) {
255 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
258 offset = ftell (s->fpin);
259 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
262 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
264 start_pos = last_pos;
266 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
268 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
272 else if (!option (OPTDONTHANDLEPGPKEYS) &&
273 str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
278 /* XXX - we may wish to recode here */
280 state_puts (s->prefix, s);
285 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
287 /* Copy PGP material to temporary file */
288 mutt_mktemp (tmpfname);
289 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
290 mutt_perror (tmpfname);
295 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
296 offset = ftell (s->fpin);
297 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
303 && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
305 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
306 || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
311 /* leave tmpfp open in case we still need it - but flush it! */
315 /* Invoke PGP if needed */
316 if (!clearsign || (s->flags & M_VERIFY)) {
317 mutt_mktemp (outfile);
318 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
319 mutt_perror (tmpfname);
323 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
324 fileno (pgpout), -1, tmpfname,
326 safe_fclose (&pgpout);
331 ("[-- Error: unable to create PGP subprocess! --]\n"),
334 else { /* PGP started successfully */
337 if (!pgp_valid_passphrase ())
338 pgp_void_passphrase ();
339 if (pgp_use_gpg_agent ())
341 fprintf (pgpin, "%s\n", PgpPass);
344 safe_fclose (&pgpin);
346 if (s->flags & M_DISPLAY) {
347 crypt_current_time (s, "PGP");
348 rc = pgp_copy_checksig (pgperr, s->fpout);
351 safe_fclose (&pgperr);
352 rv = mutt_wait_filter (thepid);
354 if (s->flags & M_DISPLAY) {
360 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
361 * Sig _is_ correct if
362 * gpg_good_sign="" && pgp_decode_command returned 0
367 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
371 /* treat empty result as sign of failure */
377 if (!pgpout || c == EOF) {
378 mutt_error _("Could not decrypt PGP message");
379 pgp_void_passphrase ();
386 * Now, copy cleartext to the screen. NOTE - we expect that PGP
387 * outputs utf-8 cleartext. This may not always be true, but it
388 * seems to be a reasonable guess.
391 if (s->flags & M_DISPLAY) {
393 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
394 else if (pgp_keyblock)
395 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
397 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
403 pgp_copy_clearsigned (tmpfp, s, body_charset);
410 state_set_prefix (s);
411 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
412 while ((c = fgetconv (fc)) != EOF)
413 state_prefix_putc (c, s);
414 fgetconv_close (&fc);
417 if (s->flags & M_DISPLAY) {
418 state_putc ('\n', s);
420 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
421 mutt_message _("PGP message successfully decrypted.");
423 else if (pgp_keyblock)
424 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
426 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
431 /* XXX - we may wish to recode here */
433 state_puts (s->prefix, s);
441 m->goodsig = (maybe_goodsig && have_any_sigs);
444 safe_fclose (&tmpfp);
445 mutt_unlink (tmpfname);
448 safe_fclose (&pgpout);
449 mutt_unlink (outfile);
452 if (needpass == -1) {
454 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
462 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
465 char tempfile[_POSIX_PATH_MAX];
466 char buf[HUGE_STRING];
473 if (b->type != TYPETEXT)
476 if (tagged_only && !b->tagged)
479 mutt_mktemp (tempfile);
480 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
485 if ((tfp = fopen (tempfile, "r")) == NULL) {
490 while (fgets (buf, sizeof (buf), tfp)) {
491 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
492 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
494 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
496 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
503 if (!enc && !sgn && !key)
506 /* fix the content type */
508 mutt_set_parameter ("format", "fixed", &b->parameter);
510 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
512 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
514 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
519 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
524 for (; b; b = b->next) {
525 if (is_multipart (b))
526 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
527 else if (b->type == TYPETEXT) {
528 if ((r = mutt_is_application_pgp (b)))
531 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
542 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
544 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
545 FILE *fp, *pgpout, *pgperr;
550 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
552 if (!(fp = safe_fopen (sigfile, "w"))) {
553 mutt_perror (sigfile);
557 fseek (s->fpin, sigbdy->offset, 0);
558 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
561 mutt_mktemp (pgperrfile);
562 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
563 mutt_perror (pgperrfile);
568 crypt_current_time (s, "PGP");
570 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
571 -1, -1, fileno (pgperr),
572 tempfile, sigfile)) != -1) {
573 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
577 safe_fclose (&pgpout);
581 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
584 if ((rv = mutt_wait_filter (thepid)))
587 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
590 safe_fclose (&pgperr);
592 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
594 mutt_unlink (sigfile);
595 mutt_unlink (pgperrfile);
597 debug_print (1, ("returning %d.\n", badsig));
603 /* Extract pgp public keys from messages or attachments */
605 void pgp_extract_keys_from_messages (HEADER * h)
608 char tempfname[_POSIX_PATH_MAX];
612 mutt_parse_mime_message (Context, h);
613 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
617 mutt_mktemp (tempfname);
618 if (!(fpout = safe_fopen (tempfname, "w"))) {
619 mutt_perror (tempfname);
623 set_option (OPTDONTHANDLEPGPKEYS);
626 for (i = 0; i < Context->vcount; i++) {
627 if (Context->hdrs[Context->v2r[i]]->tagged) {
628 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
629 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
630 && !pgp_valid_passphrase ()) {
634 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
635 M_CM_DECODE | M_CM_CHARCONV, 0);
640 mutt_parse_mime_message (Context, h);
641 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
645 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
650 pgp_invoke_import (tempfname);
651 mutt_any_key_to_continue (NULL);
655 mutt_unlink (tempfname);
656 unset_option (OPTDONTHANDLEPGPKEYS);
660 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
664 char tempfname[_POSIX_PATH_MAX];
666 mutt_mktemp (tempfname);
667 if (!(tempfp = safe_fopen (tempfname, "w"))) {
668 mutt_perror (tempfname);
672 memset (&s, 0, sizeof (STATE));
677 mutt_body_handler (top, &s);
681 pgp_invoke_import (tempfname);
682 mutt_any_key_to_continue (NULL);
684 mutt_unlink (tempfname);
687 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
690 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
696 set_option (OPTDONTHANDLEPGPKEYS);
698 for (; top; top = top->next) {
699 if (!tag || top->tagged)
700 pgp_extract_keys_from_attachment (fp, top);
706 unset_option (OPTDONTHANDLEPGPKEYS);
709 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
711 char buf[LONG_STRING];
712 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
716 char pgperrfile[_POSIX_PATH_MAX];
717 char pgptmpfile[_POSIX_PATH_MAX];
721 mutt_mktemp (pgperrfile);
722 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
723 mutt_perror (pgperrfile);
728 mutt_mktemp (pgptmpfile);
729 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
730 mutt_perror (pgptmpfile);
735 /* Position the stream at the beginning of the body, and send the data to
736 * the temporary file.
739 fseek (s->fpin, a->offset, 0);
740 mutt_copy_bytes (s->fpin, pgptmp, a->length);
743 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
744 fileno (pgperr), pgptmpfile)) == -1) {
747 if (s->flags & M_DISPLAY)
749 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
754 /* send the PGP passphrase to the subprocess. Never do this if the
755 agent is active, because this might lead to a passphrase send as
757 if (!pgp_use_gpg_agent ())
758 fputs (PgpPass, pgpin);
762 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
763 * read_mime_header has a hard time parsing the message.
765 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
767 if (len > 1 && buf[len - 2] == '\r')
768 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
773 rv = mutt_wait_filter (thepid);
774 mutt_unlink (pgptmpfile);
776 if (s->flags & M_DISPLAY) {
779 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
783 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
790 if (fgetc (fpout) == EOF) {
791 mutt_error (_("Decryption failed."));
792 pgp_void_passphrase ();
798 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
800 * Need to set the length of this body part.
802 fstat (fileno (fpout), &info);
803 tattach->length = info.st_size - tattach->offset;
805 /* See if we need to recurse on this MIME part. */
807 mutt_parse_part (fpout, tattach);
813 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
815 char tempfile[_POSIX_PATH_MAX];
819 if (!mutt_is_multipart_encrypted (b))
822 if (!b->parts || !b->parts->next)
827 memset (&s, 0, sizeof (s));
829 mutt_mktemp (tempfile);
830 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
831 mutt_perror (tempfile);
836 *cur = pgp_decrypt_part (b, &s, *fpout, p);
846 int pgp_encrypted_handler (BODY * a, STATE * s)
848 char tempfile[_POSIX_PATH_MAX];
855 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
856 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
857 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
858 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
859 if (s->flags & M_DISPLAY)
860 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
866 * Move forward to the application/pgp-encrypted body.
870 mutt_mktemp (tempfile);
871 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
872 if (s->flags & M_DISPLAY)
874 ("[-- Error: could not create temporary file! --]\n"),
879 if (s->flags & M_DISPLAY)
880 crypt_current_time (s, "PGP");
882 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
883 if (s->flags & M_DISPLAY)
885 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
890 rc = mutt_body_handler (tattach, s);
894 * if a multipart/signed is the _only_ sub-part of a
895 * multipart/encrypted, cache signature verification
900 if (mutt_is_multipart_signed (tattach) && !tattach->next)
901 p->goodsig |= tattach->goodsig;
903 if (s->flags & M_DISPLAY) {
904 state_puts ("\n", s);
905 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
908 mutt_free_body (&tattach);
909 /* clear 'Invoking...' message, since there's no error */
910 mutt_message _("PGP message successfully decrypted.");
912 mutt_error _("Could not decrypt PGP message");
913 pgp_void_passphrase ();
918 mutt_unlink (tempfile);
923 /* ----------------------------------------------------------------------------
924 * Routines for sending PGP/MIME messages.
928 BODY *pgp_sign_message (BODY * a)
931 char buffer[LONG_STRING];
932 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
933 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
938 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
940 mutt_mktemp (sigfile);
941 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
945 mutt_mktemp (signedfile);
946 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
947 mutt_perror (signedfile);
953 mutt_write_mime_header (a, sfp);
955 mutt_write_mime_body (a, sfp);
958 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
959 -1, -1, -1, signedfile)) == -1) {
960 mutt_perror (_("Can't open PGP subprocess!"));
968 if (!pgp_use_gpg_agent ())
969 fputs (PgpPass, pgpin);
974 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
975 * recommended for future releases of PGP.
977 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
978 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
979 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
980 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
981 fputs ("-----END PGP SIGNATURE-----\n", fp);
984 empty = 0; /* got some output, so we're ok */
987 /* check for errors from PGP */
989 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
991 fputs (buffer, stdout);
994 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1001 if (fclose (fp) != 0) {
1002 mutt_perror ("fclose");
1008 pgp_void_passphrase ();
1009 mutt_any_key_to_continue (NULL);
1014 return (NULL); /* fatal error while signing */
1017 t = mutt_new_body ();
1018 t->type = TYPEMULTIPART;
1019 t->subtype = str_dup ("signed");
1020 t->encoding = ENC7BIT;
1022 t->disposition = DISPINLINE;
1024 mutt_generate_boundary (&t->parameter);
1025 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1026 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1031 t->parts->next = mutt_new_body ();
1033 t->type = TYPEAPPLICATION;
1034 t->subtype = str_dup ("pgp-signature");
1035 t->filename = str_dup (sigfile);
1037 t->disposition = DISPINLINE;
1038 t->encoding = ENC7BIT;
1039 t->unlink = 1; /* ok to remove this file after sending. */
1044 static short is_numerical_keyid (const char *s)
1046 /* or should we require the "0x"? */
1047 if (strncmp (s, "0x", 2) == 0)
1049 if (str_len (s) % 8)
1052 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1058 /* This routine attempts to find the keyids of the recipients of a message.
1059 * It returns NULL if any of the keys can not be found.
1061 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1063 char *keyID, *keylist = NULL, *t;
1064 size_t keylist_size = 0;
1065 size_t keylist_used = 0;
1066 ADDRESS *tmp = NULL, *addr = NULL;
1067 ADDRESS **last = &tmp;
1070 pgp_key_t k_info = NULL, key = NULL;
1072 const char *fqdn = mutt_fqdn (1);
1074 for (i = 0; i < 3; i++) {
1089 *last = rfc822_cpy_adr (p);
1091 last = &((*last)->next);
1095 rfc822_qualify (tmp, fqdn);
1097 tmp = mutt_remove_duplicates (tmp);
1099 for (p = tmp; p; p = p->next) {
1100 char buf[LONG_STRING];
1105 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1108 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1110 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1111 if (is_numerical_keyid (keyID)) {
1112 if (strncmp (keyID, "0x", 2) == 0)
1114 goto bypass_selection; /* you don't see this. */
1117 /* check for e-mail address */
1118 if ((t = strchr (keyID, '@')) &&
1119 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1121 rfc822_qualify (addr, fqdn);
1125 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1128 mem_free (&keylist);
1129 rfc822_free_address (&tmp);
1130 rfc822_free_address (&addr);
1136 pgp_invoke_getkeys (q);
1140 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1141 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1143 if ((key = pgp_ask_for_key (buf, q->mailbox,
1144 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1145 mem_free (&keylist);
1146 rfc822_free_address (&tmp);
1147 rfc822_free_address (&addr);
1154 keyID = pgp_keyid (key);
1157 keylist_size += str_len (keyID) + 4;
1158 mem_realloc (&keylist, keylist_size);
1159 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1161 keylist_used = str_len (keylist);
1163 pgp_free_key (&key);
1164 rfc822_free_address (&addr);
1167 rfc822_free_address (&tmp);
1171 /* Warning: "a" is no longer freed in this routine, you need
1172 * to free it later. This is necessary for $fcc_attach. */
1174 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1176 char buf[LONG_STRING];
1177 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1178 char pgpinfile[_POSIX_PATH_MAX];
1179 FILE *pgpin, *pgperr, *fpout, *fptmp;
1185 mutt_mktemp (tempfile);
1186 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1187 mutt_perror (tempfile);
1191 mutt_mktemp (pgperrfile);
1192 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1193 mutt_perror (pgperrfile);
1198 unlink (pgperrfile);
1200 mutt_mktemp (pgpinfile);
1201 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1202 mutt_perror (pgpinfile);
1210 convert_to_7bit (a);
1212 mutt_write_mime_header (a, fptmp);
1213 fputc ('\n', fptmp);
1214 mutt_write_mime_body (a, fptmp);
1217 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1218 fileno (fpout), fileno (pgperr),
1219 pgpinfile, keylist, sign)) == -1) {
1226 if (!pgp_use_gpg_agent ())
1227 fputs (PgpPass, pgpin);
1228 fputc ('\n', pgpin);
1232 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1240 empty = (fgetc (fpout) == EOF);
1245 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1247 fputs (buf, stdout);
1251 /* pause if there is any error output from PGP */
1253 mutt_any_key_to_continue (NULL);
1256 /* fatal error while trying to encrypt message */
1258 pgp_void_passphrase (); /* just in case */
1263 t = mutt_new_body ();
1264 t->type = TYPEMULTIPART;
1265 t->subtype = str_dup ("encrypted");
1266 t->encoding = ENC7BIT;
1268 t->disposition = DISPINLINE;
1270 mutt_generate_boundary (&t->parameter);
1271 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1273 t->parts = mutt_new_body ();
1274 t->parts->type = TYPEAPPLICATION;
1275 t->parts->subtype = str_dup ("pgp-encrypted");
1276 t->parts->encoding = ENC7BIT;
1278 t->parts->next = mutt_new_body ();
1279 t->parts->next->type = TYPEAPPLICATION;
1280 t->parts->next->subtype = str_dup ("octet-stream");
1281 t->parts->next->encoding = ENC7BIT;
1282 t->parts->next->filename = str_dup (tempfile);
1283 t->parts->next->use_disp = 1;
1284 t->parts->next->disposition = DISPINLINE;
1285 t->parts->next->unlink = 1; /* delete after sending the message */
1286 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1291 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1295 char pgpoutfile[_POSIX_PATH_MAX];
1296 char pgperrfile[_POSIX_PATH_MAX];
1297 char pgpinfile[_POSIX_PATH_MAX];
1299 char body_charset[STRING];
1301 const char *send_charset;
1303 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1313 if (a->type != TYPETEXT)
1315 if (ascii_strcasecmp (a->subtype, "plain"))
1318 if ((fp = fopen (a->filename, "r")) == NULL) {
1319 mutt_perror (a->filename);
1323 mutt_mktemp (pgpinfile);
1324 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1325 mutt_perror (pgpinfile);
1330 /* The following code is really correct: If noconv is set,
1331 * a's charset parameter contains the on-disk character set, and
1332 * we have to convert from that to utf-8. If noconv is not set,
1333 * we have to convert from $charset to utf-8.
1336 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1338 from_charset = body_charset;
1340 from_charset = Charset;
1342 if (!mutt_is_us_ascii (body_charset)) {
1346 if (flags & ENCRYPT)
1347 send_charset = "us-ascii";
1349 send_charset = "utf-8";
1351 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1352 while ((c = fgetconv (fc)) != EOF)
1355 fgetconv_close (&fc);
1358 send_charset = "us-ascii";
1359 mutt_copy_stream (fp, pgpin);
1364 mutt_mktemp (pgpoutfile);
1365 mutt_mktemp (pgperrfile);
1366 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1367 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1368 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1372 unlink (pgpoutfile);
1377 unlink (pgperrfile);
1379 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1380 -1, fileno (pgpout), fileno (pgperr),
1381 pgpinfile, keylist, flags)) == -1) {
1382 mutt_perror (_("Can't invoke PGP"));
1386 mutt_unlink (pgpinfile);
1387 unlink (pgpoutfile);
1391 if (pgp_use_gpg_agent ())
1394 fprintf (pgpin, "%s\n", PgpPass);
1397 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1400 mutt_unlink (pgpinfile);
1409 empty = (fgetc (pgpout) == EOF);
1414 while (fgets (buff, sizeof (buff), pgperr)) {
1416 fputs (buff, stdout);
1422 mutt_any_key_to_continue (NULL);
1426 pgp_void_passphrase (); /* just in case */
1427 unlink (pgpoutfile);
1431 b = mutt_new_body ();
1433 b->encoding = ENC7BIT;
1436 b->subtype = str_dup ("plain");
1438 mutt_set_parameter ("x-action",
1439 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1441 mutt_set_parameter ("charset", send_charset, &b->parameter);
1443 b->filename = str_dup (pgpoutfile);
1446 /* The following is intended to give a clue to some completely brain-dead
1447 * "mail environments" which are typically used by large corporations.
1450 b->d_filename = str_dup ("msg.pgp");
1455 b->disposition = DISPINLINE;
1461 if (!(flags & ENCRYPT))
1462 b->encoding = a->encoding;
1467 int pgp_send_menu (HEADER * msg, int *redraw)
1470 char input_signas[SHORT_STRING];
1472 char prompt[LONG_STRING];
1474 if (!(WithCrypto & APPLICATION_PGP))
1475 return msg->security;
1477 /* If autoinline and no crypto options set, then set inline. */
1478 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1479 && (msg->security & (SIGN | ENCRYPT))))
1480 msg->security |= INLINE;
1482 snprintf (prompt, sizeof (prompt),
1483 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1484 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1486 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1487 case 1: /* (e)ncrypt */
1488 msg->security |= ENCRYPT;
1489 msg->security &= ~SIGN;
1492 case 2: /* (s)ign */
1493 msg->security |= SIGN;
1494 msg->security &= ~ENCRYPT;
1497 case 3: /* sign (a)s */
1498 unset_option (OPTPGPCHECKTRUST);
1501 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1503 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1504 str_replace (&PgpSignAs, input_signas);
1507 msg->security |= SIGN;
1509 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1513 msg->security &= ~SIGN;
1517 *redraw = REDRAW_FULL;
1520 case 4: /* (b)oth */
1521 msg->security |= (ENCRYPT | SIGN);
1524 case 5: /* (i)nline */
1525 if ((msg->security & (ENCRYPT | SIGN)))
1526 msg->security ^= INLINE;
1528 msg->security &= ~INLINE;
1531 case 6: /* (f)orget it */
1532 case 7: /* (c)lear */
1537 if (msg->security) {
1538 if (!(msg->security & (ENCRYPT | SIGN)))
1541 msg->security |= APPLICATION_PGP;
1544 return (msg->security);
1548 #endif /* CRYPT_BACKEND_CLASSIC_PGP */