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
24 #include <lib-lib/mem.h>
25 #include <lib-lib/ascii.h>
26 #include <lib-lib/str.h>
27 #include <lib-lib/macros.h>
28 #include <lib-lib/file.h>
30 #include <lib-mime/mime.h>
35 #include "mutt_curses.h"
40 #include "lib/debug.h"
54 #ifdef HAVE_SYS_TIME_H
55 # include <sys/time.h>
58 #ifdef HAVE_SYS_RESOURCE_H
59 # include <sys/resource.h>
62 #ifdef CRYPT_BACKEND_CLASSIC_PGP
64 #include "mutt_crypt.h"
65 #include "mutt_menu.h"
68 char PgpPass[LONG_STRING];
69 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
71 void pgp_void_passphrase (void)
73 p_clear(PgpPass, sizeof(PgpPass));
77 int pgp_valid_passphrase (void)
79 time_t now = time (NULL);
81 if (pgp_use_gpg_agent ()) {
83 return 1; /* handled by gpg-agent */
87 /* Use cached copy. */
90 pgp_void_passphrase ();
92 if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
93 sizeof (PgpPass), M_PASS) == 0) {
94 PgpExptime = time (NULL) + PgpTimeout;
103 void pgp_forget_passphrase (void)
105 pgp_void_passphrase ();
106 mutt_message _("PGP passphrase forgotten.");
109 int pgp_use_gpg_agent (void) {
112 if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
115 if ((tty = ttyname(0)))
116 setenv ("GPG_TTY", tty, 0);
121 char *pgp_keyid (pgp_key_t k)
123 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
126 return _pgp_keyid (k);
129 char *_pgp_keyid (pgp_key_t k)
131 if (option (OPTPGPLONGIDS))
134 return (k->keyid + 8);
137 /* ----------------------------------------------------------------------------
138 * Routines for handing PGP input.
143 /* Copy PGP output messages and look for signs of a good signature */
145 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
149 if (PgpGoodSign.pattern) {
154 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
155 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
156 debug_print (2, ("\"%s\" matches regexp.\n", line));
160 debug_print (2, ("\"%s\" doesn't match regexp.\n", line));
162 if (strncmp (line, "[GNUPG:] ", 9) == 0)
170 debug_print (2, ("No pattern.\n"));
171 mutt_copy_stream (fpin, fpout);
179 * Copy a clearsigned message, and strip the signature and PGP's
182 * XXX - charset handling: We assume that it is safe to do
183 * character set decoding first, dash decoding second here, while
184 * we do it the other way around in the main handler.
186 * (Note that we aren't worse than Outlook &c in this, and also
187 * note that we can successfully handle anything produced by any
188 * existing versions of mutt.)
191 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
193 char buf[HUGE_STRING];
194 short complete, armor_header;
200 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
202 for (complete = 1, armor_header = 1;
203 fgetconvs (buf, sizeof (buf), fc) != NULL;
204 complete = strchr (buf, '\n') != NULL) {
211 if (m_strcmp(buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
215 char *p = vskipspaces(buf);
223 state_puts (s->prefix, s);
225 if (buf[0] == '-' && buf[1] == ' ')
226 state_puts (buf + 2, s);
231 fgetconv_close (&fc);
235 /* Support for the Application/PGP Content Type. */
237 int pgp_application_pgp_handler (BODY * m, STATE * s)
239 int could_not_decrypt = 0;
240 int needpass = -1, pgp_keyblock = 0;
242 int clearsign = 0, rv, rc;
245 off_t last_pos, offset;
246 char buf[HUGE_STRING];
247 char outfile[_POSIX_PATH_MAX];
248 char tmpfname[_POSIX_PATH_MAX];
249 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
253 short maybe_goodsig = 1;
254 short have_any_sigs = 0;
256 char body_charset[STRING];
258 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
260 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
262 fseeko (s->fpin, m->offset, 0);
263 last_pos = m->offset;
265 for (bytes = m->length; bytes > 0;) {
266 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
269 offset = ftello (s->fpin);
270 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
273 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
275 start_pos = last_pos;
277 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
279 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0) {
283 else if (!option (OPTDONTHANDLEPGPKEYS) &&
284 m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
289 /* XXX - we may wish to recode here */
291 state_puts (s->prefix, s);
296 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
298 /* Copy PGP material to temporary file */
299 mutt_mktemp (tmpfname);
300 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
301 mutt_perror (tmpfname);
306 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
307 offset = ftello (s->fpin);
308 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
314 && m_strcmp("-----END PGP MESSAGE-----\n", buf) == 0)
316 && (m_strcmp("-----END PGP SIGNATURE-----\n", buf) == 0
317 || m_strcmp("-----END PGP PUBLIC KEY BLOCK-----\n",
322 /* leave tmpfp open in case we still need it - but flush it! */
326 /* Invoke PGP if needed */
327 if (!clearsign || (s->flags & M_VERIFY)) {
328 mutt_mktemp (outfile);
329 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
330 mutt_perror (tmpfname);
334 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
335 fileno (pgpout), -1, tmpfname,
337 safe_fclose (&pgpout);
342 ("[-- Error: unable to create PGP subprocess! --]\n"),
345 else { /* PGP started successfully */
348 if (!pgp_valid_passphrase ())
349 pgp_void_passphrase ();
350 if (pgp_use_gpg_agent ())
352 fprintf (pgpin, "%s\n", PgpPass);
355 safe_fclose (&pgpin);
357 if (s->flags & M_DISPLAY) {
358 crypt_current_time (s, "PGP");
359 rc = pgp_copy_checksig (pgperr, s->fpout);
362 safe_fclose (&pgperr);
363 rv = mutt_wait_filter (thepid);
365 if (s->flags & M_DISPLAY) {
371 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
372 * Sig _is_ correct if
373 * gpg_good_sign="" && pgp_decode_command returned 0
378 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
382 /* treat empty result as sign of failure */
383 /* TODO: maybe on failure mutt should include the original undecoded text. */
389 if (!clearsign && (!pgpout || c == EOF)) {
390 could_not_decrypt = 1;
391 pgp_void_passphrase ();
394 if (could_not_decrypt && !(s->flags & M_DISPLAY)) {
395 mutt_error _("Could not decrypt PGP message");
403 * Now, copy cleartext to the screen. NOTE - we expect that PGP
404 * outputs utf-8 cleartext. This may not always be true, but it
405 * seems to be a reasonable guess.
408 if (s->flags & M_DISPLAY) {
410 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
411 else if (pgp_keyblock)
412 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
414 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
420 pgp_copy_clearsigned (tmpfp, s, body_charset);
427 state_set_prefix (s);
428 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
429 while ((c = fgetconv (fc)) != EOF)
430 state_prefix_putc (c, s);
431 fgetconv_close (&fc);
434 if (s->flags & M_DISPLAY) {
435 state_putc ('\n', s);
437 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
438 if (could_not_decrypt)
439 mutt_error _("Could not decrypt PGP message.");
441 mutt_message _("PGP message successfully decrypted.");
443 else if (pgp_keyblock)
444 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
446 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
451 /* XXX - we may wish to recode here */
453 state_puts (s->prefix, s);
461 m->goodsig = (maybe_goodsig && have_any_sigs);
464 safe_fclose (&tmpfp);
465 mutt_unlink (tmpfname);
468 safe_fclose (&pgpout);
469 mutt_unlink (outfile);
472 if (needpass == -1) {
474 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
482 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
485 char tempfile[_POSIX_PATH_MAX];
486 char buf[HUGE_STRING];
493 if (b->type != TYPETEXT)
496 if (tagged_only && !b->tagged)
499 mutt_mktemp (tempfile);
500 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
505 if ((tfp = fopen (tempfile, "r")) == NULL) {
510 while (fgets (buf, sizeof (buf), tfp)) {
511 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
512 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
514 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0)
516 else if (m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
523 if (!enc && !sgn && !key)
526 /* fix the content type */
528 mutt_set_parameter ("format", "fixed", &b->parameter);
530 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
532 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
534 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
539 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
544 for (; b; b = b->next) {
545 if (is_multipart (b))
546 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
547 else if (b->type == TYPETEXT) {
548 if ((r = mutt_is_application_pgp (b)))
551 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
562 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
564 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
565 FILE *fp, *pgpout, *pgperr;
570 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
572 if (!(fp = safe_fopen (sigfile, "w"))) {
573 mutt_perror (sigfile);
577 fseeko (s->fpin, sigbdy->offset, 0);
578 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
581 mutt_mktemp (pgperrfile);
582 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
583 mutt_perror (pgperrfile);
588 crypt_current_time (s, "PGP");
590 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
591 -1, -1, fileno (pgperr),
592 tempfile, sigfile)) != -1) {
593 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
597 safe_fclose (&pgpout);
601 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
604 if ((rv = mutt_wait_filter (thepid)))
607 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
610 safe_fclose (&pgperr);
612 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
614 mutt_unlink (sigfile);
615 mutt_unlink (pgperrfile);
617 debug_print (1, ("returning %d.\n", badsig));
623 /* Extract pgp public keys from messages or attachments */
625 void pgp_extract_keys_from_messages (HEADER * h)
628 char tempfname[_POSIX_PATH_MAX];
632 mutt_parse_mime_message (Context, h);
633 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
637 mutt_mktemp (tempfname);
638 if (!(fpout = safe_fopen (tempfname, "w"))) {
639 mutt_perror (tempfname);
643 set_option (OPTDONTHANDLEPGPKEYS);
646 for (i = 0; i < Context->vcount; i++) {
647 if (Context->hdrs[Context->v2r[i]]->tagged) {
648 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
649 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
650 && !pgp_valid_passphrase ()) {
654 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
655 M_CM_DECODE | M_CM_CHARCONV, 0);
660 mutt_parse_mime_message (Context, h);
661 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
665 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
670 pgp_invoke_import (tempfname);
671 mutt_any_key_to_continue (NULL);
675 mutt_unlink (tempfname);
676 unset_option (OPTDONTHANDLEPGPKEYS);
680 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
684 char tempfname[_POSIX_PATH_MAX];
686 mutt_mktemp (tempfname);
687 if (!(tempfp = safe_fopen (tempfname, "w"))) {
688 mutt_perror (tempfname);
697 mutt_body_handler (top, &s);
701 pgp_invoke_import (tempfname);
702 mutt_any_key_to_continue (NULL);
704 mutt_unlink (tempfname);
707 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
710 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
716 set_option (OPTDONTHANDLEPGPKEYS);
718 for (; top; top = top->next) {
719 if (!tag || top->tagged)
720 pgp_extract_keys_from_attachment (fp, top);
726 unset_option (OPTDONTHANDLEPGPKEYS);
729 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
731 char buf[LONG_STRING];
732 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
736 char pgperrfile[_POSIX_PATH_MAX];
737 char pgptmpfile[_POSIX_PATH_MAX];
741 mutt_mktemp (pgperrfile);
742 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
743 mutt_perror (pgperrfile);
748 mutt_mktemp (pgptmpfile);
749 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
750 mutt_perror (pgptmpfile);
755 /* Position the stream at the beginning of the body, and send the data to
756 * the temporary file.
759 fseeko (s->fpin, a->offset, 0);
760 mutt_copy_bytes (s->fpin, pgptmp, a->length);
763 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
764 fileno (pgperr), pgptmpfile)) == -1) {
767 if (s->flags & M_DISPLAY)
769 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
774 /* send the PGP passphrase to the subprocess. Never do this if the
775 agent is active, because this might lead to a passphrase send as
777 if (!pgp_use_gpg_agent ())
778 fputs (PgpPass, pgpin);
782 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
783 * read_mime_header has a hard time parsing the message.
785 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
787 if (len > 1 && buf[len - 2] == '\r')
788 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
793 rv = mutt_wait_filter (thepid);
794 mutt_unlink (pgptmpfile);
796 if (s->flags & M_DISPLAY) {
799 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
803 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
810 if (fgetc (fpout) == EOF) {
811 mutt_error (_("Decryption failed."));
812 pgp_void_passphrase ();
818 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
820 * Need to set the length of this body part.
822 fstat (fileno (fpout), &info);
823 tattach->length = info.st_size - tattach->offset;
825 /* See if we need to recurse on this MIME part. */
827 mutt_parse_part (fpout, tattach);
833 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
835 char tempfile[_POSIX_PATH_MAX];
839 if (!mutt_is_multipart_encrypted (b))
842 if (!b->parts || !b->parts->next)
849 mutt_mktemp (tempfile);
850 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
851 mutt_perror (tempfile);
856 *cur = pgp_decrypt_part (b, &s, *fpout, p);
866 int pgp_encrypted_handler (BODY * a, STATE * s)
868 char tempfile[_POSIX_PATH_MAX];
875 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
876 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
877 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
878 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
879 if (s->flags & M_DISPLAY)
880 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
886 * Move forward to the application/pgp-encrypted body.
890 mutt_mktemp (tempfile);
891 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
892 if (s->flags & M_DISPLAY)
894 ("[-- Error: could not create temporary file! --]\n"),
899 if (s->flags & M_DISPLAY)
900 crypt_current_time (s, "PGP");
902 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
903 if (s->flags & M_DISPLAY)
905 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
910 rc = mutt_body_handler (tattach, s);
914 * if a multipart/signed is the _only_ sub-part of a
915 * multipart/encrypted, cache signature verification
920 if (mutt_is_multipart_signed (tattach) && !tattach->next)
921 p->goodsig |= tattach->goodsig;
923 if (s->flags & M_DISPLAY) {
924 state_puts ("\n", s);
925 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
928 mutt_free_body (&tattach);
929 /* clear 'Invoking...' message, since there's no error */
930 mutt_message _("PGP message successfully decrypted.");
932 mutt_error _("Could not decrypt PGP message");
933 pgp_void_passphrase ();
938 mutt_unlink (tempfile);
943 /* ----------------------------------------------------------------------------
944 * Routines for sending PGP/MIME messages.
948 BODY *pgp_sign_message (BODY * a)
951 char buffer[LONG_STRING];
952 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
953 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
958 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
960 mutt_mktemp (sigfile);
961 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
965 mutt_mktemp (signedfile);
966 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
967 mutt_perror (signedfile);
973 mutt_write_mime_header (a, sfp);
975 mutt_write_mime_body (a, sfp);
978 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
979 -1, -1, -1, signedfile)) == -1) {
980 mutt_perror (_("Can't open PGP subprocess!"));
988 if (!pgp_use_gpg_agent ())
989 fputs (PgpPass, pgpin);
994 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
995 * recommended for future releases of PGP.
997 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
998 if (m_strcmp("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
999 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
1000 else if (m_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
1001 fputs ("-----END PGP SIGNATURE-----\n", fp);
1004 empty = 0; /* got some output, so we're ok */
1007 /* check for errors from PGP */
1009 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
1011 fputs (buffer, stdout);
1014 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1019 unlink (signedfile);
1021 if (fclose (fp) != 0) {
1022 mutt_perror ("fclose");
1028 pgp_void_passphrase ();
1029 mutt_any_key_to_continue (NULL);
1034 return (NULL); /* fatal error while signing */
1037 t = mutt_new_body ();
1038 t->type = TYPEMULTIPART;
1039 t->subtype = m_strdup("signed");
1040 t->encoding = ENC7BIT;
1042 t->disposition = DISPINLINE;
1044 mutt_generate_boundary (&t->parameter);
1045 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1046 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1051 t->parts->next = mutt_new_body ();
1053 t->type = TYPEAPPLICATION;
1054 t->subtype = m_strdup("pgp-signature");
1055 t->filename = m_strdup(sigfile);
1057 t->disposition = DISPINLINE;
1058 t->encoding = ENC7BIT;
1059 t->unlink = 1; /* ok to remove this file after sending. */
1064 static short is_numerical_keyid (const char *s)
1066 /* or should we require the "0x"? */
1067 if (strncmp (s, "0x", 2) == 0)
1069 if (m_strlen(s) % 8)
1072 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1078 /* This routine attempts to find the keyids of the recipients of a message.
1079 * It returns NULL if any of the keys can not be found.
1081 char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
1083 char *keyID, *keylist = NULL, *t;
1084 size_t keylist_size = 0;
1085 size_t keylist_used = 0;
1086 address_t *tmp = NULL, *addr = NULL;
1087 address_t **last = &tmp;
1090 pgp_key_t k_info = NULL, key = NULL;
1092 const char *fqdn = mutt_fqdn (1);
1094 for (i = 0; i < 3; i++) {
1109 *last = address_list_dup (p);
1111 last = &((*last)->next);
1115 rfc822_qualify (tmp, fqdn);
1117 tmp = mutt_remove_duplicates (tmp);
1119 for (p = tmp; p; p = p->next) {
1120 char buf[LONG_STRING];
1125 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1128 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1130 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1131 if (is_numerical_keyid (keyID)) {
1132 if (strncmp (keyID, "0x", 2) == 0)
1134 goto bypass_selection; /* you don't see this. */
1137 /* check for e-mail address */
1138 if ((t = strchr (keyID, '@')) &&
1139 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1141 rfc822_qualify (addr, fqdn);
1145 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1149 address_delete (&tmp);
1150 address_delete (&addr);
1156 pgp_invoke_getkeys (q);
1160 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1161 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1163 if ((key = pgp_ask_for_key (buf, q->mailbox,
1164 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1166 address_delete (&tmp);
1167 address_delete (&addr);
1174 keyID = pgp_keyid (key);
1177 keylist_size += m_strlen(keyID) + 4;
1178 p_realloc(&keylist, keylist_size);
1179 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1181 keylist_used = m_strlen(keylist);
1183 pgp_free_key (&key);
1184 address_delete (&addr);
1187 address_delete (&tmp);
1191 /* Warning: "a" is no longer freed in this routine, you need
1192 * to free it later. This is necessary for $fcc_attach. */
1194 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1196 char buf[LONG_STRING];
1197 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1198 char pgpinfile[_POSIX_PATH_MAX];
1199 FILE *pgpin, *pgperr, *fpout, *fptmp;
1205 mutt_mktemp (tempfile);
1206 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1207 mutt_perror (tempfile);
1211 mutt_mktemp (pgperrfile);
1212 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1213 mutt_perror (pgperrfile);
1218 unlink (pgperrfile);
1220 mutt_mktemp (pgpinfile);
1221 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1222 mutt_perror (pgpinfile);
1230 convert_to_7bit (a);
1232 mutt_write_mime_header (a, fptmp);
1233 fputc ('\n', fptmp);
1234 mutt_write_mime_body (a, fptmp);
1237 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1238 fileno (fpout), fileno (pgperr),
1239 pgpinfile, keylist, sign)) == -1) {
1246 if (!pgp_use_gpg_agent ())
1247 fputs (PgpPass, pgpin);
1248 fputc ('\n', pgpin);
1252 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1260 empty = (fgetc (fpout) == EOF);
1265 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1267 fputs (buf, stdout);
1271 /* pause if there is any error output from PGP */
1273 mutt_any_key_to_continue (NULL);
1276 /* fatal error while trying to encrypt message */
1278 pgp_void_passphrase (); /* just in case */
1283 t = mutt_new_body ();
1284 t->type = TYPEMULTIPART;
1285 t->subtype = m_strdup("encrypted");
1286 t->encoding = ENC7BIT;
1288 t->disposition = DISPINLINE;
1290 mutt_generate_boundary (&t->parameter);
1291 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1293 t->parts = mutt_new_body ();
1294 t->parts->type = TYPEAPPLICATION;
1295 t->parts->subtype = m_strdup("pgp-encrypted");
1296 t->parts->encoding = ENC7BIT;
1298 t->parts->next = mutt_new_body ();
1299 t->parts->next->type = TYPEAPPLICATION;
1300 t->parts->next->subtype = m_strdup("octet-stream");
1301 t->parts->next->encoding = ENC7BIT;
1302 t->parts->next->filename = m_strdup(tempfile);
1303 t->parts->next->use_disp = 1;
1304 t->parts->next->disposition = DISPINLINE;
1305 t->parts->next->unlink = 1; /* delete after sending the message */
1306 t->parts->next->d_filename = m_strdup("msg.asc"); /* non pgp/mime can save */
1311 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1315 char pgpoutfile[_POSIX_PATH_MAX];
1316 char pgperrfile[_POSIX_PATH_MAX];
1317 char pgpinfile[_POSIX_PATH_MAX];
1319 char body_charset[STRING];
1321 const char *send_charset;
1323 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1333 if (a->type != TYPETEXT)
1335 if (ascii_strcasecmp (a->subtype, "plain"))
1338 if ((fp = fopen (a->filename, "r")) == NULL) {
1339 mutt_perror (a->filename);
1343 mutt_mktemp (pgpinfile);
1344 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1345 mutt_perror (pgpinfile);
1350 /* The following code is really correct: If noconv is set,
1351 * a's charset parameter contains the on-disk character set, and
1352 * we have to convert from that to utf-8. If noconv is not set,
1353 * we have to convert from $charset to utf-8.
1356 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1358 from_charset = body_charset;
1360 from_charset = Charset;
1362 if (!mutt_is_us_ascii (body_charset)) {
1366 if (flags & ENCRYPT)
1367 send_charset = "us-ascii";
1369 send_charset = "utf-8";
1371 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1372 while ((c = fgetconv (fc)) != EOF)
1375 fgetconv_close (&fc);
1378 send_charset = "us-ascii";
1379 mutt_copy_stream (fp, pgpin);
1384 mutt_mktemp (pgpoutfile);
1385 mutt_mktemp (pgperrfile);
1386 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1387 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1388 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1392 unlink (pgpoutfile);
1397 unlink (pgperrfile);
1399 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1400 -1, fileno (pgpout), fileno (pgperr),
1401 pgpinfile, keylist, flags)) == -1) {
1402 mutt_perror (_("Can't invoke PGP"));
1406 mutt_unlink (pgpinfile);
1407 unlink (pgpoutfile);
1411 if (pgp_use_gpg_agent ())
1414 fprintf (pgpin, "%s\n", PgpPass);
1417 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1420 mutt_unlink (pgpinfile);
1429 empty = (fgetc (pgpout) == EOF);
1434 while (fgets (buff, sizeof (buff), pgperr)) {
1436 fputs (buff, stdout);
1442 mutt_any_key_to_continue (NULL);
1446 pgp_void_passphrase (); /* just in case */
1447 unlink (pgpoutfile);
1451 b = mutt_new_body ();
1453 b->encoding = ENC7BIT;
1456 b->subtype = m_strdup("plain");
1458 mutt_set_parameter ("x-action",
1459 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1461 mutt_set_parameter ("charset", send_charset, &b->parameter);
1463 b->filename = m_strdup(pgpoutfile);
1466 /* The following is intended to give a clue to some completely brain-dead
1467 * "mail environments" which are typically used by large corporations.
1470 b->d_filename = m_strdup("msg.pgp");
1475 b->disposition = DISPINLINE;
1481 if (!(flags & ENCRYPT))
1482 b->encoding = a->encoding;
1487 int pgp_send_menu (HEADER * msg, int *redraw)
1490 char input_signas[SHORT_STRING];
1492 char prompt[LONG_STRING];
1494 if (!(WithCrypto & APPLICATION_PGP))
1495 return msg->security;
1497 /* If autoinline and no crypto options set, then set inline. */
1498 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1499 && (msg->security & (SIGN | ENCRYPT))))
1500 msg->security |= INLINE;
1502 snprintf (prompt, sizeof (prompt),
1503 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1504 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1506 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1507 case 1: /* (e)ncrypt */
1508 msg->security |= ENCRYPT;
1509 msg->security &= ~SIGN;
1512 case 2: /* (s)ign */
1513 msg->security |= SIGN;
1514 msg->security &= ~ENCRYPT;
1517 case 3: /* sign (a)s */
1518 unset_option (OPTPGPCHECKTRUST);
1521 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1523 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1524 m_strreplace(&PgpSignAs, input_signas);
1527 msg->security |= SIGN;
1529 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1533 msg->security &= ~SIGN;
1537 *redraw = REDRAW_FULL;
1540 case 4: /* (b)oth */
1541 msg->security |= (ENCRYPT | SIGN);
1544 case 5: /* (i)nline */
1545 if ((msg->security & (ENCRYPT | SIGN)))
1546 msg->security ^= INLINE;
1548 msg->security &= ~INLINE;
1551 case 6: /* (f)orget it */
1552 case 7: /* (c)lear */
1557 if (msg->security) {
1558 if (!(msg->security & (ENCRYPT | SIGN)))
1561 msg->security |= APPLICATION_PGP;
1564 return (msg->security);
1568 #endif /* CRYPT_BACKEND_CLASSIC_PGP */