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 (!clearsign && (!pgpout || c == EOF)) {
378 mutt_error _("Could not decrypt PGP message");
380 pgp_void_passphrase ();
387 * Now, copy cleartext to the screen. NOTE - we expect that PGP
388 * outputs utf-8 cleartext. This may not always be true, but it
389 * seems to be a reasonable guess.
392 if (s->flags & M_DISPLAY) {
394 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
395 else if (pgp_keyblock)
396 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
398 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
404 pgp_copy_clearsigned (tmpfp, s, body_charset);
411 state_set_prefix (s);
412 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
413 while ((c = fgetconv (fc)) != EOF)
414 state_prefix_putc (c, s);
415 fgetconv_close (&fc);
418 if (s->flags & M_DISPLAY) {
419 state_putc ('\n', s);
421 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
422 mutt_message _("PGP message successfully decrypted.");
424 else if (pgp_keyblock)
425 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
427 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
432 /* XXX - we may wish to recode here */
434 state_puts (s->prefix, s);
442 m->goodsig = (maybe_goodsig && have_any_sigs);
445 safe_fclose (&tmpfp);
446 mutt_unlink (tmpfname);
449 safe_fclose (&pgpout);
450 mutt_unlink (outfile);
453 if (needpass == -1) {
455 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
463 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
466 char tempfile[_POSIX_PATH_MAX];
467 char buf[HUGE_STRING];
474 if (b->type != TYPETEXT)
477 if (tagged_only && !b->tagged)
480 mutt_mktemp (tempfile);
481 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
486 if ((tfp = fopen (tempfile, "r")) == NULL) {
491 while (fgets (buf, sizeof (buf), tfp)) {
492 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
493 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
495 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
497 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
504 if (!enc && !sgn && !key)
507 /* fix the content type */
509 mutt_set_parameter ("format", "fixed", &b->parameter);
511 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
513 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
515 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
520 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
525 for (; b; b = b->next) {
526 if (is_multipart (b))
527 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
528 else if (b->type == TYPETEXT) {
529 if ((r = mutt_is_application_pgp (b)))
532 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
543 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
545 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
546 FILE *fp, *pgpout, *pgperr;
551 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
553 if (!(fp = safe_fopen (sigfile, "w"))) {
554 mutt_perror (sigfile);
558 fseek (s->fpin, sigbdy->offset, 0);
559 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
562 mutt_mktemp (pgperrfile);
563 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
564 mutt_perror (pgperrfile);
569 crypt_current_time (s, "PGP");
571 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
572 -1, -1, fileno (pgperr),
573 tempfile, sigfile)) != -1) {
574 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
578 safe_fclose (&pgpout);
582 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
585 if ((rv = mutt_wait_filter (thepid)))
588 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
591 safe_fclose (&pgperr);
593 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
595 mutt_unlink (sigfile);
596 mutt_unlink (pgperrfile);
598 debug_print (1, ("returning %d.\n", badsig));
604 /* Extract pgp public keys from messages or attachments */
606 void pgp_extract_keys_from_messages (HEADER * h)
609 char tempfname[_POSIX_PATH_MAX];
613 mutt_parse_mime_message (Context, h);
614 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
618 mutt_mktemp (tempfname);
619 if (!(fpout = safe_fopen (tempfname, "w"))) {
620 mutt_perror (tempfname);
624 set_option (OPTDONTHANDLEPGPKEYS);
627 for (i = 0; i < Context->vcount; i++) {
628 if (Context->hdrs[Context->v2r[i]]->tagged) {
629 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
630 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
631 && !pgp_valid_passphrase ()) {
635 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
636 M_CM_DECODE | M_CM_CHARCONV, 0);
641 mutt_parse_mime_message (Context, h);
642 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
646 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
651 pgp_invoke_import (tempfname);
652 mutt_any_key_to_continue (NULL);
656 mutt_unlink (tempfname);
657 unset_option (OPTDONTHANDLEPGPKEYS);
661 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
665 char tempfname[_POSIX_PATH_MAX];
667 mutt_mktemp (tempfname);
668 if (!(tempfp = safe_fopen (tempfname, "w"))) {
669 mutt_perror (tempfname);
673 memset (&s, 0, sizeof (STATE));
678 mutt_body_handler (top, &s);
682 pgp_invoke_import (tempfname);
683 mutt_any_key_to_continue (NULL);
685 mutt_unlink (tempfname);
688 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
691 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
697 set_option (OPTDONTHANDLEPGPKEYS);
699 for (; top; top = top->next) {
700 if (!tag || top->tagged)
701 pgp_extract_keys_from_attachment (fp, top);
707 unset_option (OPTDONTHANDLEPGPKEYS);
710 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
712 char buf[LONG_STRING];
713 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
717 char pgperrfile[_POSIX_PATH_MAX];
718 char pgptmpfile[_POSIX_PATH_MAX];
722 mutt_mktemp (pgperrfile);
723 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
724 mutt_perror (pgperrfile);
729 mutt_mktemp (pgptmpfile);
730 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
731 mutt_perror (pgptmpfile);
736 /* Position the stream at the beginning of the body, and send the data to
737 * the temporary file.
740 fseek (s->fpin, a->offset, 0);
741 mutt_copy_bytes (s->fpin, pgptmp, a->length);
744 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
745 fileno (pgperr), pgptmpfile)) == -1) {
748 if (s->flags & M_DISPLAY)
750 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
755 /* send the PGP passphrase to the subprocess. Never do this if the
756 agent is active, because this might lead to a passphrase send as
758 if (!pgp_use_gpg_agent ())
759 fputs (PgpPass, pgpin);
763 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
764 * read_mime_header has a hard time parsing the message.
766 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
768 if (len > 1 && buf[len - 2] == '\r')
769 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
774 rv = mutt_wait_filter (thepid);
775 mutt_unlink (pgptmpfile);
777 if (s->flags & M_DISPLAY) {
780 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
784 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
791 if (fgetc (fpout) == EOF) {
792 mutt_error (_("Decryption failed."));
793 pgp_void_passphrase ();
799 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
801 * Need to set the length of this body part.
803 fstat (fileno (fpout), &info);
804 tattach->length = info.st_size - tattach->offset;
806 /* See if we need to recurse on this MIME part. */
808 mutt_parse_part (fpout, tattach);
814 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
816 char tempfile[_POSIX_PATH_MAX];
820 if (!mutt_is_multipart_encrypted (b))
823 if (!b->parts || !b->parts->next)
828 memset (&s, 0, sizeof (s));
830 mutt_mktemp (tempfile);
831 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
832 mutt_perror (tempfile);
837 *cur = pgp_decrypt_part (b, &s, *fpout, p);
847 int pgp_encrypted_handler (BODY * a, STATE * s)
849 char tempfile[_POSIX_PATH_MAX];
856 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
857 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
858 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
859 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
860 if (s->flags & M_DISPLAY)
861 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
867 * Move forward to the application/pgp-encrypted body.
871 mutt_mktemp (tempfile);
872 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
873 if (s->flags & M_DISPLAY)
875 ("[-- Error: could not create temporary file! --]\n"),
880 if (s->flags & M_DISPLAY)
881 crypt_current_time (s, "PGP");
883 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
884 if (s->flags & M_DISPLAY)
886 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
891 rc = mutt_body_handler (tattach, s);
895 * if a multipart/signed is the _only_ sub-part of a
896 * multipart/encrypted, cache signature verification
901 if (mutt_is_multipart_signed (tattach) && !tattach->next)
902 p->goodsig |= tattach->goodsig;
904 if (s->flags & M_DISPLAY) {
905 state_puts ("\n", s);
906 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
909 mutt_free_body (&tattach);
910 /* clear 'Invoking...' message, since there's no error */
911 mutt_message _("PGP message successfully decrypted.");
913 mutt_error _("Could not decrypt PGP message");
914 pgp_void_passphrase ();
919 mutt_unlink (tempfile);
924 /* ----------------------------------------------------------------------------
925 * Routines for sending PGP/MIME messages.
929 BODY *pgp_sign_message (BODY * a)
932 char buffer[LONG_STRING];
933 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
934 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
939 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
941 mutt_mktemp (sigfile);
942 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
946 mutt_mktemp (signedfile);
947 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
948 mutt_perror (signedfile);
954 mutt_write_mime_header (a, sfp);
956 mutt_write_mime_body (a, sfp);
959 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
960 -1, -1, -1, signedfile)) == -1) {
961 mutt_perror (_("Can't open PGP subprocess!"));
969 if (!pgp_use_gpg_agent ())
970 fputs (PgpPass, pgpin);
975 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
976 * recommended for future releases of PGP.
978 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
979 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
980 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
981 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
982 fputs ("-----END PGP SIGNATURE-----\n", fp);
985 empty = 0; /* got some output, so we're ok */
988 /* check for errors from PGP */
990 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
992 fputs (buffer, stdout);
995 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1000 unlink (signedfile);
1002 if (fclose (fp) != 0) {
1003 mutt_perror ("fclose");
1009 pgp_void_passphrase ();
1010 mutt_any_key_to_continue (NULL);
1015 return (NULL); /* fatal error while signing */
1018 t = mutt_new_body ();
1019 t->type = TYPEMULTIPART;
1020 t->subtype = str_dup ("signed");
1021 t->encoding = ENC7BIT;
1023 t->disposition = DISPINLINE;
1025 mutt_generate_boundary (&t->parameter);
1026 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1027 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1032 t->parts->next = mutt_new_body ();
1034 t->type = TYPEAPPLICATION;
1035 t->subtype = str_dup ("pgp-signature");
1036 t->filename = str_dup (sigfile);
1038 t->disposition = DISPINLINE;
1039 t->encoding = ENC7BIT;
1040 t->unlink = 1; /* ok to remove this file after sending. */
1045 static short is_numerical_keyid (const char *s)
1047 /* or should we require the "0x"? */
1048 if (strncmp (s, "0x", 2) == 0)
1050 if (str_len (s) % 8)
1053 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1059 /* This routine attempts to find the keyids of the recipients of a message.
1060 * It returns NULL if any of the keys can not be found.
1062 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1064 char *keyID, *keylist = NULL, *t;
1065 size_t keylist_size = 0;
1066 size_t keylist_used = 0;
1067 ADDRESS *tmp = NULL, *addr = NULL;
1068 ADDRESS **last = &tmp;
1071 pgp_key_t k_info = NULL, key = NULL;
1073 const char *fqdn = mutt_fqdn (1);
1075 for (i = 0; i < 3; i++) {
1090 *last = rfc822_cpy_adr (p);
1092 last = &((*last)->next);
1096 rfc822_qualify (tmp, fqdn);
1098 tmp = mutt_remove_duplicates (tmp);
1100 for (p = tmp; p; p = p->next) {
1101 char buf[LONG_STRING];
1106 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1109 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1111 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1112 if (is_numerical_keyid (keyID)) {
1113 if (strncmp (keyID, "0x", 2) == 0)
1115 goto bypass_selection; /* you don't see this. */
1118 /* check for e-mail address */
1119 if ((t = strchr (keyID, '@')) &&
1120 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1122 rfc822_qualify (addr, fqdn);
1126 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1129 mem_free (&keylist);
1130 rfc822_free_address (&tmp);
1131 rfc822_free_address (&addr);
1137 pgp_invoke_getkeys (q);
1141 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1142 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1144 if ((key = pgp_ask_for_key (buf, q->mailbox,
1145 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1146 mem_free (&keylist);
1147 rfc822_free_address (&tmp);
1148 rfc822_free_address (&addr);
1155 keyID = pgp_keyid (key);
1158 keylist_size += str_len (keyID) + 4;
1159 mem_realloc (&keylist, keylist_size);
1160 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1162 keylist_used = str_len (keylist);
1164 pgp_free_key (&key);
1165 rfc822_free_address (&addr);
1168 rfc822_free_address (&tmp);
1172 /* Warning: "a" is no longer freed in this routine, you need
1173 * to free it later. This is necessary for $fcc_attach. */
1175 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1177 char buf[LONG_STRING];
1178 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1179 char pgpinfile[_POSIX_PATH_MAX];
1180 FILE *pgpin, *pgperr, *fpout, *fptmp;
1186 mutt_mktemp (tempfile);
1187 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1188 mutt_perror (tempfile);
1192 mutt_mktemp (pgperrfile);
1193 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1194 mutt_perror (pgperrfile);
1199 unlink (pgperrfile);
1201 mutt_mktemp (pgpinfile);
1202 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1203 mutt_perror (pgpinfile);
1211 convert_to_7bit (a);
1213 mutt_write_mime_header (a, fptmp);
1214 fputc ('\n', fptmp);
1215 mutt_write_mime_body (a, fptmp);
1218 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1219 fileno (fpout), fileno (pgperr),
1220 pgpinfile, keylist, sign)) == -1) {
1227 if (!pgp_use_gpg_agent ())
1228 fputs (PgpPass, pgpin);
1229 fputc ('\n', pgpin);
1233 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1241 empty = (fgetc (fpout) == EOF);
1246 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1248 fputs (buf, stdout);
1252 /* pause if there is any error output from PGP */
1254 mutt_any_key_to_continue (NULL);
1257 /* fatal error while trying to encrypt message */
1259 pgp_void_passphrase (); /* just in case */
1264 t = mutt_new_body ();
1265 t->type = TYPEMULTIPART;
1266 t->subtype = str_dup ("encrypted");
1267 t->encoding = ENC7BIT;
1269 t->disposition = DISPINLINE;
1271 mutt_generate_boundary (&t->parameter);
1272 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1274 t->parts = mutt_new_body ();
1275 t->parts->type = TYPEAPPLICATION;
1276 t->parts->subtype = str_dup ("pgp-encrypted");
1277 t->parts->encoding = ENC7BIT;
1279 t->parts->next = mutt_new_body ();
1280 t->parts->next->type = TYPEAPPLICATION;
1281 t->parts->next->subtype = str_dup ("octet-stream");
1282 t->parts->next->encoding = ENC7BIT;
1283 t->parts->next->filename = str_dup (tempfile);
1284 t->parts->next->use_disp = 1;
1285 t->parts->next->disposition = DISPINLINE;
1286 t->parts->next->unlink = 1; /* delete after sending the message */
1287 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1292 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1296 char pgpoutfile[_POSIX_PATH_MAX];
1297 char pgperrfile[_POSIX_PATH_MAX];
1298 char pgpinfile[_POSIX_PATH_MAX];
1300 char body_charset[STRING];
1302 const char *send_charset;
1304 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1314 if (a->type != TYPETEXT)
1316 if (ascii_strcasecmp (a->subtype, "plain"))
1319 if ((fp = fopen (a->filename, "r")) == NULL) {
1320 mutt_perror (a->filename);
1324 mutt_mktemp (pgpinfile);
1325 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1326 mutt_perror (pgpinfile);
1331 /* The following code is really correct: If noconv is set,
1332 * a's charset parameter contains the on-disk character set, and
1333 * we have to convert from that to utf-8. If noconv is not set,
1334 * we have to convert from $charset to utf-8.
1337 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1339 from_charset = body_charset;
1341 from_charset = Charset;
1343 if (!mutt_is_us_ascii (body_charset)) {
1347 if (flags & ENCRYPT)
1348 send_charset = "us-ascii";
1350 send_charset = "utf-8";
1352 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1353 while ((c = fgetconv (fc)) != EOF)
1356 fgetconv_close (&fc);
1359 send_charset = "us-ascii";
1360 mutt_copy_stream (fp, pgpin);
1365 mutt_mktemp (pgpoutfile);
1366 mutt_mktemp (pgperrfile);
1367 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1368 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1369 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1373 unlink (pgpoutfile);
1378 unlink (pgperrfile);
1380 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1381 -1, fileno (pgpout), fileno (pgperr),
1382 pgpinfile, keylist, flags)) == -1) {
1383 mutt_perror (_("Can't invoke PGP"));
1387 mutt_unlink (pgpinfile);
1388 unlink (pgpoutfile);
1392 if (pgp_use_gpg_agent ())
1395 fprintf (pgpin, "%s\n", PgpPass);
1398 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1401 mutt_unlink (pgpinfile);
1410 empty = (fgetc (pgpout) == EOF);
1415 while (fgets (buff, sizeof (buff), pgperr)) {
1417 fputs (buff, stdout);
1423 mutt_any_key_to_continue (NULL);
1427 pgp_void_passphrase (); /* just in case */
1428 unlink (pgpoutfile);
1432 b = mutt_new_body ();
1434 b->encoding = ENC7BIT;
1437 b->subtype = str_dup ("plain");
1439 mutt_set_parameter ("x-action",
1440 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1442 mutt_set_parameter ("charset", send_charset, &b->parameter);
1444 b->filename = str_dup (pgpoutfile);
1447 /* The following is intended to give a clue to some completely brain-dead
1448 * "mail environments" which are typically used by large corporations.
1451 b->d_filename = str_dup ("msg.pgp");
1456 b->disposition = DISPINLINE;
1462 if (!(flags & ENCRYPT))
1463 b->encoding = a->encoding;
1468 int pgp_send_menu (HEADER * msg, int *redraw)
1471 char input_signas[SHORT_STRING];
1473 char prompt[LONG_STRING];
1475 if (!(WithCrypto & APPLICATION_PGP))
1476 return msg->security;
1478 /* If autoinline and no crypto options set, then set inline. */
1479 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1480 && (msg->security & (SIGN | ENCRYPT))))
1481 msg->security |= INLINE;
1483 snprintf (prompt, sizeof (prompt),
1484 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1485 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1487 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1488 case 1: /* (e)ncrypt */
1489 msg->security |= ENCRYPT;
1490 msg->security &= ~SIGN;
1493 case 2: /* (s)ign */
1494 msg->security |= SIGN;
1495 msg->security &= ~ENCRYPT;
1498 case 3: /* sign (a)s */
1499 unset_option (OPTPGPCHECKTRUST);
1502 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1504 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1505 str_replace (&PgpSignAs, input_signas);
1508 msg->security |= SIGN;
1510 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1514 msg->security &= ~SIGN;
1518 *redraw = REDRAW_FULL;
1521 case 4: /* (b)oth */
1522 msg->security |= (ENCRYPT | SIGN);
1525 case 5: /* (i)nline */
1526 if ((msg->security & (ENCRYPT | SIGN)))
1527 msg->security ^= INLINE;
1529 msg->security &= ~INLINE;
1532 case 6: /* (f)orget it */
1533 case 7: /* (c)lear */
1538 if (msg->security) {
1539 if (!(msg->security & (ENCRYPT | SIGN)))
1542 msg->security |= APPLICATION_PGP;
1545 return (msg->security);
1549 #endif /* CRYPT_BACKEND_CLASSIC_PGP */