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) {
109 if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
112 if ((tty = ttyname(0)))
113 setenv ("GPG_TTY", tty, 0);
118 char *pgp_keyid (pgp_key_t k)
120 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
123 return _pgp_keyid (k);
126 char *_pgp_keyid (pgp_key_t k)
128 if (option (OPTPGPLONGIDS))
131 return (k->keyid + 8);
134 /* ----------------------------------------------------------------------------
135 * Routines for handing PGP input.
140 /* Copy PGP output messages and look for signs of a good signature */
142 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
146 if (PgpGoodSign.pattern) {
151 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
152 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
153 debug_print (2, ("\"%s\" matches regexp.\n", line));
157 debug_print (2, ("\"%s\" doesn't match regexp.\n", line));
159 if (strncmp (line, "[GNUPG:] ", 9) == 0)
167 debug_print (2, ("No pattern.\n"));
168 mutt_copy_stream (fpin, fpout);
176 * Copy a clearsigned message, and strip the signature and PGP's
179 * XXX - charset handling: We assume that it is safe to do
180 * character set decoding first, dash decoding second here, while
181 * we do it the other way around in the main handler.
183 * (Note that we aren't worse than Outlook &c in this, and also
184 * note that we can successfully handle anything produced by any
185 * existing versions of mutt.)
188 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
190 char buf[HUGE_STRING];
191 short complete, armor_header;
197 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
199 for (complete = 1, armor_header = 1;
200 fgetconvs (buf, sizeof (buf), fc) != NULL;
201 complete = strchr (buf, '\n') != NULL) {
208 if (str_cmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
212 char *p = str_skip_initws (buf);
220 state_puts (s->prefix, s);
222 if (buf[0] == '-' && buf[1] == ' ')
223 state_puts (buf + 2, s);
228 fgetconv_close (&fc);
232 /* Support for the Application/PGP Content Type. */
234 int pgp_application_pgp_handler (BODY * m, STATE * s)
236 int could_not_decrypt = 0;
237 int needpass = -1, pgp_keyblock = 0;
239 int clearsign = 0, rv, rc;
242 LOFF_T last_pos, offset;
243 char buf[HUGE_STRING];
244 char outfile[_POSIX_PATH_MAX];
245 char tmpfname[_POSIX_PATH_MAX];
246 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
250 short maybe_goodsig = 1;
251 short have_any_sigs = 0;
253 char body_charset[STRING];
255 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
257 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
259 fseeko (s->fpin, m->offset, 0);
260 last_pos = m->offset;
262 for (bytes = m->length; bytes > 0;) {
263 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
266 offset = ftello (s->fpin);
267 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
270 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
272 start_pos = last_pos;
274 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
276 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
280 else if (!option (OPTDONTHANDLEPGPKEYS) &&
281 str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
286 /* XXX - we may wish to recode here */
288 state_puts (s->prefix, s);
293 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
295 /* Copy PGP material to temporary file */
296 mutt_mktemp (tmpfname);
297 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
298 mutt_perror (tmpfname);
303 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
304 offset = ftello (s->fpin);
305 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
311 && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
313 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
314 || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
319 /* leave tmpfp open in case we still need it - but flush it! */
323 /* Invoke PGP if needed */
324 if (!clearsign || (s->flags & M_VERIFY)) {
325 mutt_mktemp (outfile);
326 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
327 mutt_perror (tmpfname);
331 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
332 fileno (pgpout), -1, tmpfname,
334 safe_fclose (&pgpout);
339 ("[-- Error: unable to create PGP subprocess! --]\n"),
342 else { /* PGP started successfully */
345 if (!pgp_valid_passphrase ())
346 pgp_void_passphrase ();
347 if (pgp_use_gpg_agent ())
349 fprintf (pgpin, "%s\n", PgpPass);
352 safe_fclose (&pgpin);
354 if (s->flags & M_DISPLAY) {
355 crypt_current_time (s, "PGP");
356 rc = pgp_copy_checksig (pgperr, s->fpout);
359 safe_fclose (&pgperr);
360 rv = mutt_wait_filter (thepid);
362 if (s->flags & M_DISPLAY) {
368 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
369 * Sig _is_ correct if
370 * gpg_good_sign="" && pgp_decode_command returned 0
375 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
379 /* treat empty result as sign of failure */
380 /* TODO: maybe on failure mutt should include the original undecoded text. */
386 if (!clearsign && (!pgpout || c == EOF)) {
387 could_not_decrypt = 1;
388 pgp_void_passphrase ();
391 if (could_not_decrypt && !(s->flags & M_DISPLAY)) {
392 mutt_error _("Could not decrypt PGP message");
400 * Now, copy cleartext to the screen. NOTE - we expect that PGP
401 * outputs utf-8 cleartext. This may not always be true, but it
402 * seems to be a reasonable guess.
405 if (s->flags & M_DISPLAY) {
407 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
408 else if (pgp_keyblock)
409 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
411 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
417 pgp_copy_clearsigned (tmpfp, s, body_charset);
424 state_set_prefix (s);
425 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
426 while ((c = fgetconv (fc)) != EOF)
427 state_prefix_putc (c, s);
428 fgetconv_close (&fc);
431 if (s->flags & M_DISPLAY) {
432 state_putc ('\n', s);
434 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
435 if (could_not_decrypt)
436 mutt_error _("Could not decrypt PGP message.");
438 mutt_message _("PGP message successfully decrypted.");
440 else if (pgp_keyblock)
441 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
443 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
448 /* XXX - we may wish to recode here */
450 state_puts (s->prefix, s);
458 m->goodsig = (maybe_goodsig && have_any_sigs);
461 safe_fclose (&tmpfp);
462 mutt_unlink (tmpfname);
465 safe_fclose (&pgpout);
466 mutt_unlink (outfile);
469 if (needpass == -1) {
471 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
479 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
482 char tempfile[_POSIX_PATH_MAX];
483 char buf[HUGE_STRING];
490 if (b->type != TYPETEXT)
493 if (tagged_only && !b->tagged)
496 mutt_mktemp (tempfile);
497 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
502 if ((tfp = fopen (tempfile, "r")) == NULL) {
507 while (fgets (buf, sizeof (buf), tfp)) {
508 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
509 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
511 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
513 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
520 if (!enc && !sgn && !key)
523 /* fix the content type */
525 mutt_set_parameter ("format", "fixed", &b->parameter);
527 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
529 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
531 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
536 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
541 for (; b; b = b->next) {
542 if (is_multipart (b))
543 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
544 else if (b->type == TYPETEXT) {
545 if ((r = mutt_is_application_pgp (b)))
548 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
559 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
561 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
562 FILE *fp, *pgpout, *pgperr;
567 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
569 if (!(fp = safe_fopen (sigfile, "w"))) {
570 mutt_perror (sigfile);
574 fseeko (s->fpin, sigbdy->offset, 0);
575 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
578 mutt_mktemp (pgperrfile);
579 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
580 mutt_perror (pgperrfile);
585 crypt_current_time (s, "PGP");
587 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
588 -1, -1, fileno (pgperr),
589 tempfile, sigfile)) != -1) {
590 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
594 safe_fclose (&pgpout);
598 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
601 if ((rv = mutt_wait_filter (thepid)))
604 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
607 safe_fclose (&pgperr);
609 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
611 mutt_unlink (sigfile);
612 mutt_unlink (pgperrfile);
614 debug_print (1, ("returning %d.\n", badsig));
620 /* Extract pgp public keys from messages or attachments */
622 void pgp_extract_keys_from_messages (HEADER * h)
625 char tempfname[_POSIX_PATH_MAX];
629 mutt_parse_mime_message (Context, h);
630 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
634 mutt_mktemp (tempfname);
635 if (!(fpout = safe_fopen (tempfname, "w"))) {
636 mutt_perror (tempfname);
640 set_option (OPTDONTHANDLEPGPKEYS);
643 for (i = 0; i < Context->vcount; i++) {
644 if (Context->hdrs[Context->v2r[i]]->tagged) {
645 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
646 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
647 && !pgp_valid_passphrase ()) {
651 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
652 M_CM_DECODE | M_CM_CHARCONV, 0);
657 mutt_parse_mime_message (Context, h);
658 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
662 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
667 pgp_invoke_import (tempfname);
668 mutt_any_key_to_continue (NULL);
672 mutt_unlink (tempfname);
673 unset_option (OPTDONTHANDLEPGPKEYS);
677 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
681 char tempfname[_POSIX_PATH_MAX];
683 mutt_mktemp (tempfname);
684 if (!(tempfp = safe_fopen (tempfname, "w"))) {
685 mutt_perror (tempfname);
689 memset (&s, 0, sizeof (STATE));
694 mutt_body_handler (top, &s);
698 pgp_invoke_import (tempfname);
699 mutt_any_key_to_continue (NULL);
701 mutt_unlink (tempfname);
704 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
707 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
713 set_option (OPTDONTHANDLEPGPKEYS);
715 for (; top; top = top->next) {
716 if (!tag || top->tagged)
717 pgp_extract_keys_from_attachment (fp, top);
723 unset_option (OPTDONTHANDLEPGPKEYS);
726 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
728 char buf[LONG_STRING];
729 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
733 char pgperrfile[_POSIX_PATH_MAX];
734 char pgptmpfile[_POSIX_PATH_MAX];
738 mutt_mktemp (pgperrfile);
739 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
740 mutt_perror (pgperrfile);
745 mutt_mktemp (pgptmpfile);
746 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
747 mutt_perror (pgptmpfile);
752 /* Position the stream at the beginning of the body, and send the data to
753 * the temporary file.
756 fseeko (s->fpin, a->offset, 0);
757 mutt_copy_bytes (s->fpin, pgptmp, a->length);
760 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
761 fileno (pgperr), pgptmpfile)) == -1) {
764 if (s->flags & M_DISPLAY)
766 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
771 /* send the PGP passphrase to the subprocess. Never do this if the
772 agent is active, because this might lead to a passphrase send as
774 if (!pgp_use_gpg_agent ())
775 fputs (PgpPass, pgpin);
779 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
780 * read_mime_header has a hard time parsing the message.
782 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
784 if (len > 1 && buf[len - 2] == '\r')
785 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
790 rv = mutt_wait_filter (thepid);
791 mutt_unlink (pgptmpfile);
793 if (s->flags & M_DISPLAY) {
796 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
800 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
807 if (fgetc (fpout) == EOF) {
808 mutt_error (_("Decryption failed."));
809 pgp_void_passphrase ();
815 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
817 * Need to set the length of this body part.
819 fstat (fileno (fpout), &info);
820 tattach->length = info.st_size - tattach->offset;
822 /* See if we need to recurse on this MIME part. */
824 mutt_parse_part (fpout, tattach);
830 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
832 char tempfile[_POSIX_PATH_MAX];
836 if (!mutt_is_multipart_encrypted (b))
839 if (!b->parts || !b->parts->next)
844 memset (&s, 0, sizeof (s));
846 mutt_mktemp (tempfile);
847 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
848 mutt_perror (tempfile);
853 *cur = pgp_decrypt_part (b, &s, *fpout, p);
863 int pgp_encrypted_handler (BODY * a, STATE * s)
865 char tempfile[_POSIX_PATH_MAX];
872 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
873 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
874 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
875 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
876 if (s->flags & M_DISPLAY)
877 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
883 * Move forward to the application/pgp-encrypted body.
887 mutt_mktemp (tempfile);
888 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
889 if (s->flags & M_DISPLAY)
891 ("[-- Error: could not create temporary file! --]\n"),
896 if (s->flags & M_DISPLAY)
897 crypt_current_time (s, "PGP");
899 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
900 if (s->flags & M_DISPLAY)
902 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
907 rc = mutt_body_handler (tattach, s);
911 * if a multipart/signed is the _only_ sub-part of a
912 * multipart/encrypted, cache signature verification
917 if (mutt_is_multipart_signed (tattach) && !tattach->next)
918 p->goodsig |= tattach->goodsig;
920 if (s->flags & M_DISPLAY) {
921 state_puts ("\n", s);
922 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
925 mutt_free_body (&tattach);
926 /* clear 'Invoking...' message, since there's no error */
927 mutt_message _("PGP message successfully decrypted.");
929 mutt_error _("Could not decrypt PGP message");
930 pgp_void_passphrase ();
935 mutt_unlink (tempfile);
940 /* ----------------------------------------------------------------------------
941 * Routines for sending PGP/MIME messages.
945 BODY *pgp_sign_message (BODY * a)
948 char buffer[LONG_STRING];
949 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
950 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
955 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
957 mutt_mktemp (sigfile);
958 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
962 mutt_mktemp (signedfile);
963 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
964 mutt_perror (signedfile);
970 mutt_write_mime_header (a, sfp);
972 mutt_write_mime_body (a, sfp);
975 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
976 -1, -1, -1, signedfile)) == -1) {
977 mutt_perror (_("Can't open PGP subprocess!"));
985 if (!pgp_use_gpg_agent ())
986 fputs (PgpPass, pgpin);
991 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
992 * recommended for future releases of PGP.
994 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
995 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
996 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
997 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
998 fputs ("-----END PGP SIGNATURE-----\n", fp);
1001 empty = 0; /* got some output, so we're ok */
1004 /* check for errors from PGP */
1006 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
1008 fputs (buffer, stdout);
1011 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1016 unlink (signedfile);
1018 if (fclose (fp) != 0) {
1019 mutt_perror ("fclose");
1025 pgp_void_passphrase ();
1026 mutt_any_key_to_continue (NULL);
1031 return (NULL); /* fatal error while signing */
1034 t = mutt_new_body ();
1035 t->type = TYPEMULTIPART;
1036 t->subtype = str_dup ("signed");
1037 t->encoding = ENC7BIT;
1039 t->disposition = DISPINLINE;
1041 mutt_generate_boundary (&t->parameter);
1042 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1043 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1048 t->parts->next = mutt_new_body ();
1050 t->type = TYPEAPPLICATION;
1051 t->subtype = str_dup ("pgp-signature");
1052 t->filename = str_dup (sigfile);
1054 t->disposition = DISPINLINE;
1055 t->encoding = ENC7BIT;
1056 t->unlink = 1; /* ok to remove this file after sending. */
1061 static short is_numerical_keyid (const char *s)
1063 /* or should we require the "0x"? */
1064 if (strncmp (s, "0x", 2) == 0)
1066 if (str_len (s) % 8)
1069 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1075 /* This routine attempts to find the keyids of the recipients of a message.
1076 * It returns NULL if any of the keys can not be found.
1078 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1080 char *keyID, *keylist = NULL, *t;
1081 size_t keylist_size = 0;
1082 size_t keylist_used = 0;
1083 ADDRESS *tmp = NULL, *addr = NULL;
1084 ADDRESS **last = &tmp;
1087 pgp_key_t k_info = NULL, key = NULL;
1089 const char *fqdn = mutt_fqdn (1);
1091 for (i = 0; i < 3; i++) {
1106 *last = rfc822_cpy_adr (p);
1108 last = &((*last)->next);
1112 rfc822_qualify (tmp, fqdn);
1114 tmp = mutt_remove_duplicates (tmp);
1116 for (p = tmp; p; p = p->next) {
1117 char buf[LONG_STRING];
1122 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1125 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1127 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1128 if (is_numerical_keyid (keyID)) {
1129 if (strncmp (keyID, "0x", 2) == 0)
1131 goto bypass_selection; /* you don't see this. */
1134 /* check for e-mail address */
1135 if ((t = strchr (keyID, '@')) &&
1136 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1138 rfc822_qualify (addr, fqdn);
1142 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1145 mem_free (&keylist);
1146 rfc822_free_address (&tmp);
1147 rfc822_free_address (&addr);
1153 pgp_invoke_getkeys (q);
1157 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1158 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1160 if ((key = pgp_ask_for_key (buf, q->mailbox,
1161 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1162 mem_free (&keylist);
1163 rfc822_free_address (&tmp);
1164 rfc822_free_address (&addr);
1171 keyID = pgp_keyid (key);
1174 keylist_size += str_len (keyID) + 4;
1175 mem_realloc (&keylist, keylist_size);
1176 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1178 keylist_used = str_len (keylist);
1180 pgp_free_key (&key);
1181 rfc822_free_address (&addr);
1184 rfc822_free_address (&tmp);
1188 /* Warning: "a" is no longer freed in this routine, you need
1189 * to free it later. This is necessary for $fcc_attach. */
1191 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1193 char buf[LONG_STRING];
1194 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1195 char pgpinfile[_POSIX_PATH_MAX];
1196 FILE *pgpin, *pgperr, *fpout, *fptmp;
1202 mutt_mktemp (tempfile);
1203 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1204 mutt_perror (tempfile);
1208 mutt_mktemp (pgperrfile);
1209 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1210 mutt_perror (pgperrfile);
1215 unlink (pgperrfile);
1217 mutt_mktemp (pgpinfile);
1218 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1219 mutt_perror (pgpinfile);
1227 convert_to_7bit (a);
1229 mutt_write_mime_header (a, fptmp);
1230 fputc ('\n', fptmp);
1231 mutt_write_mime_body (a, fptmp);
1234 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1235 fileno (fpout), fileno (pgperr),
1236 pgpinfile, keylist, sign)) == -1) {
1243 if (!pgp_use_gpg_agent ())
1244 fputs (PgpPass, pgpin);
1245 fputc ('\n', pgpin);
1249 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1257 empty = (fgetc (fpout) == EOF);
1262 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1264 fputs (buf, stdout);
1268 /* pause if there is any error output from PGP */
1270 mutt_any_key_to_continue (NULL);
1273 /* fatal error while trying to encrypt message */
1275 pgp_void_passphrase (); /* just in case */
1280 t = mutt_new_body ();
1281 t->type = TYPEMULTIPART;
1282 t->subtype = str_dup ("encrypted");
1283 t->encoding = ENC7BIT;
1285 t->disposition = DISPINLINE;
1287 mutt_generate_boundary (&t->parameter);
1288 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1290 t->parts = mutt_new_body ();
1291 t->parts->type = TYPEAPPLICATION;
1292 t->parts->subtype = str_dup ("pgp-encrypted");
1293 t->parts->encoding = ENC7BIT;
1295 t->parts->next = mutt_new_body ();
1296 t->parts->next->type = TYPEAPPLICATION;
1297 t->parts->next->subtype = str_dup ("octet-stream");
1298 t->parts->next->encoding = ENC7BIT;
1299 t->parts->next->filename = str_dup (tempfile);
1300 t->parts->next->use_disp = 1;
1301 t->parts->next->disposition = DISPINLINE;
1302 t->parts->next->unlink = 1; /* delete after sending the message */
1303 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1308 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1312 char pgpoutfile[_POSIX_PATH_MAX];
1313 char pgperrfile[_POSIX_PATH_MAX];
1314 char pgpinfile[_POSIX_PATH_MAX];
1316 char body_charset[STRING];
1318 const char *send_charset;
1320 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1330 if (a->type != TYPETEXT)
1332 if (ascii_strcasecmp (a->subtype, "plain"))
1335 if ((fp = fopen (a->filename, "r")) == NULL) {
1336 mutt_perror (a->filename);
1340 mutt_mktemp (pgpinfile);
1341 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1342 mutt_perror (pgpinfile);
1347 /* The following code is really correct: If noconv is set,
1348 * a's charset parameter contains the on-disk character set, and
1349 * we have to convert from that to utf-8. If noconv is not set,
1350 * we have to convert from $charset to utf-8.
1353 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1355 from_charset = body_charset;
1357 from_charset = Charset;
1359 if (!mutt_is_us_ascii (body_charset)) {
1363 if (flags & ENCRYPT)
1364 send_charset = "us-ascii";
1366 send_charset = "utf-8";
1368 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1369 while ((c = fgetconv (fc)) != EOF)
1372 fgetconv_close (&fc);
1375 send_charset = "us-ascii";
1376 mutt_copy_stream (fp, pgpin);
1381 mutt_mktemp (pgpoutfile);
1382 mutt_mktemp (pgperrfile);
1383 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1384 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1385 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1389 unlink (pgpoutfile);
1394 unlink (pgperrfile);
1396 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1397 -1, fileno (pgpout), fileno (pgperr),
1398 pgpinfile, keylist, flags)) == -1) {
1399 mutt_perror (_("Can't invoke PGP"));
1403 mutt_unlink (pgpinfile);
1404 unlink (pgpoutfile);
1408 if (pgp_use_gpg_agent ())
1411 fprintf (pgpin, "%s\n", PgpPass);
1414 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1417 mutt_unlink (pgpinfile);
1426 empty = (fgetc (pgpout) == EOF);
1431 while (fgets (buff, sizeof (buff), pgperr)) {
1433 fputs (buff, stdout);
1439 mutt_any_key_to_continue (NULL);
1443 pgp_void_passphrase (); /* just in case */
1444 unlink (pgpoutfile);
1448 b = mutt_new_body ();
1450 b->encoding = ENC7BIT;
1453 b->subtype = str_dup ("plain");
1455 mutt_set_parameter ("x-action",
1456 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1458 mutt_set_parameter ("charset", send_charset, &b->parameter);
1460 b->filename = str_dup (pgpoutfile);
1463 /* The following is intended to give a clue to some completely brain-dead
1464 * "mail environments" which are typically used by large corporations.
1467 b->d_filename = str_dup ("msg.pgp");
1472 b->disposition = DISPINLINE;
1478 if (!(flags & ENCRYPT))
1479 b->encoding = a->encoding;
1484 int pgp_send_menu (HEADER * msg, int *redraw)
1487 char input_signas[SHORT_STRING];
1489 char prompt[LONG_STRING];
1491 if (!(WithCrypto & APPLICATION_PGP))
1492 return msg->security;
1494 /* If autoinline and no crypto options set, then set inline. */
1495 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1496 && (msg->security & (SIGN | ENCRYPT))))
1497 msg->security |= INLINE;
1499 snprintf (prompt, sizeof (prompt),
1500 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1501 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1503 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1504 case 1: /* (e)ncrypt */
1505 msg->security |= ENCRYPT;
1506 msg->security &= ~SIGN;
1509 case 2: /* (s)ign */
1510 msg->security |= SIGN;
1511 msg->security &= ~ENCRYPT;
1514 case 3: /* sign (a)s */
1515 unset_option (OPTPGPCHECKTRUST);
1518 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1520 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1521 str_replace (&PgpSignAs, input_signas);
1524 msg->security |= SIGN;
1526 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1530 msg->security &= ~SIGN;
1534 *redraw = REDRAW_FULL;
1537 case 4: /* (b)oth */
1538 msg->security |= (ENCRYPT | SIGN);
1541 case 5: /* (i)nline */
1542 if ((msg->security & (ENCRYPT | SIGN)))
1543 msg->security ^= INLINE;
1545 msg->security &= ~INLINE;
1548 case 6: /* (f)orget it */
1549 case 7: /* (c)lear */
1554 if (msg->security) {
1555 if (!(msg->security & (ENCRYPT | SIGN)))
1558 msg->security |= APPLICATION_PGP;
1561 return (msg->security);
1565 #endif /* CRYPT_BACKEND_CLASSIC_PGP */