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 char tmp[SHORT_STRING];
114 snprintf (tmp, sizeof (tmp), "GPG_TTY=%s", tty);
117 setenv("GPG_TTY", tty, 0);
124 char *pgp_keyid (pgp_key_t k)
126 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
129 return _pgp_keyid (k);
132 char *_pgp_keyid (pgp_key_t k)
134 if (option (OPTPGPLONGIDS))
137 return (k->keyid + 8);
140 /* ----------------------------------------------------------------------------
141 * Routines for handing PGP input.
146 /* Copy PGP output messages and look for signs of a good signature */
148 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
152 if (PgpGoodSign.pattern) {
157 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
158 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
159 debug_print (2, ("\"%s\" matches regexp.\n", line));
163 debug_print (2, ("\"%s\" doesn't match regexp.\n", line));
165 if (strncmp (line, "[GNUPG:] ", 9) == 0)
173 debug_print (2, ("No pattern.\n"));
174 mutt_copy_stream (fpin, fpout);
182 * Copy a clearsigned message, and strip the signature and PGP's
185 * XXX - charset handling: We assume that it is safe to do
186 * character set decoding first, dash decoding second here, while
187 * we do it the other way around in the main handler.
189 * (Note that we aren't worse than Outlook &c in this, and also
190 * note that we can successfully handle anything produced by any
191 * existing versions of mutt.)
194 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
196 char buf[HUGE_STRING];
197 short complete, armor_header;
203 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
205 for (complete = 1, armor_header = 1;
206 fgetconvs (buf, sizeof (buf), fc) != NULL;
207 complete = strchr (buf, '\n') != NULL) {
214 if (str_cmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
218 char *p = str_skip_initws (buf);
226 state_puts (s->prefix, s);
228 if (buf[0] == '-' && buf[1] == ' ')
229 state_puts (buf + 2, s);
234 fgetconv_close (&fc);
238 /* Support for the Application/PGP Content Type. */
240 int pgp_application_pgp_handler (BODY * m, STATE * s)
242 int needpass = -1, pgp_keyblock = 0;
244 int clearsign = 0, rv, rc;
246 long bytes, last_pos, offset;
247 char buf[HUGE_STRING];
248 char outfile[_POSIX_PATH_MAX];
249 char tmpfname[_POSIX_PATH_MAX];
250 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
254 short maybe_goodsig = 1;
255 short have_any_sigs = 0;
257 char body_charset[STRING];
259 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
261 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
263 fseek (s->fpin, m->offset, 0);
264 last_pos = m->offset;
266 for (bytes = m->length; bytes > 0;) {
267 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
270 offset = ftell (s->fpin);
271 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
274 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
276 start_pos = last_pos;
278 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
280 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
284 else if (!option (OPTDONTHANDLEPGPKEYS) &&
285 str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
290 /* XXX - we may wish to recode here */
292 state_puts (s->prefix, s);
297 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
299 /* Copy PGP material to temporary file */
300 mutt_mktemp (tmpfname);
301 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
302 mutt_perror (tmpfname);
307 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
308 offset = ftell (s->fpin);
309 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
315 && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
317 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
318 || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
323 /* leave tmpfp open in case we still need it - but flush it! */
327 /* Invoke PGP if needed */
328 if (!clearsign || (s->flags & M_VERIFY)) {
329 mutt_mktemp (outfile);
330 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
331 mutt_perror (tmpfname);
335 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
336 fileno (pgpout), -1, tmpfname,
338 safe_fclose (&pgpout);
343 ("[-- Error: unable to create PGP subprocess! --]\n"),
346 else { /* PGP started successfully */
349 if (!pgp_valid_passphrase ())
350 pgp_void_passphrase ();
351 if (pgp_use_gpg_agent ())
353 fprintf (pgpin, "%s\n", PgpPass);
356 safe_fclose (&pgpin);
358 if (s->flags & M_DISPLAY) {
359 crypt_current_time (s, "PGP");
360 rc = pgp_copy_checksig (pgperr, s->fpout);
363 safe_fclose (&pgperr);
364 rv = mutt_wait_filter (thepid);
366 if (s->flags & M_DISPLAY) {
372 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
373 * Sig _is_ correct if
374 * gpg_good_sign="" && pgp_decode_command returned 0
379 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
383 /* treat empty result as sign of failure */
389 if (!clearsign && (!pgpout || c == EOF)) {
390 mutt_error _("Could not decrypt PGP message");
392 pgp_void_passphrase ();
399 * Now, copy cleartext to the screen. NOTE - we expect that PGP
400 * outputs utf-8 cleartext. This may not always be true, but it
401 * seems to be a reasonable guess.
404 if (s->flags & M_DISPLAY) {
406 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
407 else if (pgp_keyblock)
408 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
410 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
416 pgp_copy_clearsigned (tmpfp, s, body_charset);
423 state_set_prefix (s);
424 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
425 while ((c = fgetconv (fc)) != EOF)
426 state_prefix_putc (c, s);
427 fgetconv_close (&fc);
430 if (s->flags & M_DISPLAY) {
431 state_putc ('\n', s);
433 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
434 mutt_message _("PGP message successfully decrypted.");
436 else if (pgp_keyblock)
437 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
439 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
444 /* XXX - we may wish to recode here */
446 state_puts (s->prefix, s);
454 m->goodsig = (maybe_goodsig && have_any_sigs);
457 safe_fclose (&tmpfp);
458 mutt_unlink (tmpfname);
461 safe_fclose (&pgpout);
462 mutt_unlink (outfile);
465 if (needpass == -1) {
467 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
475 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
478 char tempfile[_POSIX_PATH_MAX];
479 char buf[HUGE_STRING];
486 if (b->type != TYPETEXT)
489 if (tagged_only && !b->tagged)
492 mutt_mktemp (tempfile);
493 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
498 if ((tfp = fopen (tempfile, "r")) == NULL) {
503 while (fgets (buf, sizeof (buf), tfp)) {
504 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
505 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
507 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
509 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
516 if (!enc && !sgn && !key)
519 /* fix the content type */
521 mutt_set_parameter ("format", "fixed", &b->parameter);
523 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
525 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
527 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
532 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
537 for (; b; b = b->next) {
538 if (is_multipart (b))
539 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
540 else if (b->type == TYPETEXT) {
541 if ((r = mutt_is_application_pgp (b)))
544 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
555 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
557 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
558 FILE *fp, *pgpout, *pgperr;
563 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
565 if (!(fp = safe_fopen (sigfile, "w"))) {
566 mutt_perror (sigfile);
570 fseek (s->fpin, sigbdy->offset, 0);
571 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
574 mutt_mktemp (pgperrfile);
575 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
576 mutt_perror (pgperrfile);
581 crypt_current_time (s, "PGP");
583 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
584 -1, -1, fileno (pgperr),
585 tempfile, sigfile)) != -1) {
586 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
590 safe_fclose (&pgpout);
594 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
597 if ((rv = mutt_wait_filter (thepid)))
600 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
603 safe_fclose (&pgperr);
605 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
607 mutt_unlink (sigfile);
608 mutt_unlink (pgperrfile);
610 debug_print (1, ("returning %d.\n", badsig));
616 /* Extract pgp public keys from messages or attachments */
618 void pgp_extract_keys_from_messages (HEADER * h)
621 char tempfname[_POSIX_PATH_MAX];
625 mutt_parse_mime_message (Context, h);
626 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
630 mutt_mktemp (tempfname);
631 if (!(fpout = safe_fopen (tempfname, "w"))) {
632 mutt_perror (tempfname);
636 set_option (OPTDONTHANDLEPGPKEYS);
639 for (i = 0; i < Context->vcount; i++) {
640 if (Context->hdrs[Context->v2r[i]]->tagged) {
641 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
642 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
643 && !pgp_valid_passphrase ()) {
647 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
648 M_CM_DECODE | M_CM_CHARCONV, 0);
653 mutt_parse_mime_message (Context, h);
654 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
658 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
663 pgp_invoke_import (tempfname);
664 mutt_any_key_to_continue (NULL);
668 mutt_unlink (tempfname);
669 unset_option (OPTDONTHANDLEPGPKEYS);
673 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
677 char tempfname[_POSIX_PATH_MAX];
679 mutt_mktemp (tempfname);
680 if (!(tempfp = safe_fopen (tempfname, "w"))) {
681 mutt_perror (tempfname);
685 memset (&s, 0, sizeof (STATE));
690 mutt_body_handler (top, &s);
694 pgp_invoke_import (tempfname);
695 mutt_any_key_to_continue (NULL);
697 mutt_unlink (tempfname);
700 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
703 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
709 set_option (OPTDONTHANDLEPGPKEYS);
711 for (; top; top = top->next) {
712 if (!tag || top->tagged)
713 pgp_extract_keys_from_attachment (fp, top);
719 unset_option (OPTDONTHANDLEPGPKEYS);
722 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
724 char buf[LONG_STRING];
725 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
729 char pgperrfile[_POSIX_PATH_MAX];
730 char pgptmpfile[_POSIX_PATH_MAX];
734 mutt_mktemp (pgperrfile);
735 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
736 mutt_perror (pgperrfile);
741 mutt_mktemp (pgptmpfile);
742 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
743 mutt_perror (pgptmpfile);
748 /* Position the stream at the beginning of the body, and send the data to
749 * the temporary file.
752 fseek (s->fpin, a->offset, 0);
753 mutt_copy_bytes (s->fpin, pgptmp, a->length);
756 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
757 fileno (pgperr), pgptmpfile)) == -1) {
760 if (s->flags & M_DISPLAY)
762 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
767 /* send the PGP passphrase to the subprocess. Never do this if the
768 agent is active, because this might lead to a passphrase send as
770 if (!pgp_use_gpg_agent ())
771 fputs (PgpPass, pgpin);
775 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
776 * read_mime_header has a hard time parsing the message.
778 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
780 if (len > 1 && buf[len - 2] == '\r')
781 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
786 rv = mutt_wait_filter (thepid);
787 mutt_unlink (pgptmpfile);
789 if (s->flags & M_DISPLAY) {
792 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
796 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
803 if (fgetc (fpout) == EOF) {
804 mutt_error (_("Decryption failed."));
805 pgp_void_passphrase ();
811 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
813 * Need to set the length of this body part.
815 fstat (fileno (fpout), &info);
816 tattach->length = info.st_size - tattach->offset;
818 /* See if we need to recurse on this MIME part. */
820 mutt_parse_part (fpout, tattach);
826 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
828 char tempfile[_POSIX_PATH_MAX];
832 if (!mutt_is_multipart_encrypted (b))
835 if (!b->parts || !b->parts->next)
840 memset (&s, 0, sizeof (s));
842 mutt_mktemp (tempfile);
843 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
844 mutt_perror (tempfile);
849 *cur = pgp_decrypt_part (b, &s, *fpout, p);
859 int pgp_encrypted_handler (BODY * a, STATE * s)
861 char tempfile[_POSIX_PATH_MAX];
868 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
869 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
870 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
871 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
872 if (s->flags & M_DISPLAY)
873 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
879 * Move forward to the application/pgp-encrypted body.
883 mutt_mktemp (tempfile);
884 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
885 if (s->flags & M_DISPLAY)
887 ("[-- Error: could not create temporary file! --]\n"),
892 if (s->flags & M_DISPLAY)
893 crypt_current_time (s, "PGP");
895 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
896 if (s->flags & M_DISPLAY)
898 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
903 rc = mutt_body_handler (tattach, s);
907 * if a multipart/signed is the _only_ sub-part of a
908 * multipart/encrypted, cache signature verification
913 if (mutt_is_multipart_signed (tattach) && !tattach->next)
914 p->goodsig |= tattach->goodsig;
916 if (s->flags & M_DISPLAY) {
917 state_puts ("\n", s);
918 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
921 mutt_free_body (&tattach);
922 /* clear 'Invoking...' message, since there's no error */
923 mutt_message _("PGP message successfully decrypted.");
925 mutt_error _("Could not decrypt PGP message");
926 pgp_void_passphrase ();
931 mutt_unlink (tempfile);
936 /* ----------------------------------------------------------------------------
937 * Routines for sending PGP/MIME messages.
941 BODY *pgp_sign_message (BODY * a)
944 char buffer[LONG_STRING];
945 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
946 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
951 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
953 mutt_mktemp (sigfile);
954 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
958 mutt_mktemp (signedfile);
959 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
960 mutt_perror (signedfile);
966 mutt_write_mime_header (a, sfp);
968 mutt_write_mime_body (a, sfp);
971 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
972 -1, -1, -1, signedfile)) == -1) {
973 mutt_perror (_("Can't open PGP subprocess!"));
981 if (!pgp_use_gpg_agent ())
982 fputs (PgpPass, pgpin);
987 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
988 * recommended for future releases of PGP.
990 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
991 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
992 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
993 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
994 fputs ("-----END PGP SIGNATURE-----\n", fp);
997 empty = 0; /* got some output, so we're ok */
1000 /* check for errors from PGP */
1002 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
1004 fputs (buffer, stdout);
1007 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1012 unlink (signedfile);
1014 if (fclose (fp) != 0) {
1015 mutt_perror ("fclose");
1021 pgp_void_passphrase ();
1022 mutt_any_key_to_continue (NULL);
1027 return (NULL); /* fatal error while signing */
1030 t = mutt_new_body ();
1031 t->type = TYPEMULTIPART;
1032 t->subtype = str_dup ("signed");
1033 t->encoding = ENC7BIT;
1035 t->disposition = DISPINLINE;
1037 mutt_generate_boundary (&t->parameter);
1038 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1039 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1044 t->parts->next = mutt_new_body ();
1046 t->type = TYPEAPPLICATION;
1047 t->subtype = str_dup ("pgp-signature");
1048 t->filename = str_dup (sigfile);
1050 t->disposition = DISPINLINE;
1051 t->encoding = ENC7BIT;
1052 t->unlink = 1; /* ok to remove this file after sending. */
1057 static short is_numerical_keyid (const char *s)
1059 /* or should we require the "0x"? */
1060 if (strncmp (s, "0x", 2) == 0)
1062 if (str_len (s) % 8)
1065 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1071 /* This routine attempts to find the keyids of the recipients of a message.
1072 * It returns NULL if any of the keys can not be found.
1074 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1076 char *keyID, *keylist = NULL, *t;
1077 size_t keylist_size = 0;
1078 size_t keylist_used = 0;
1079 ADDRESS *tmp = NULL, *addr = NULL;
1080 ADDRESS **last = &tmp;
1083 pgp_key_t k_info = NULL, key = NULL;
1085 const char *fqdn = mutt_fqdn (1);
1087 for (i = 0; i < 3; i++) {
1102 *last = rfc822_cpy_adr (p);
1104 last = &((*last)->next);
1108 rfc822_qualify (tmp, fqdn);
1110 tmp = mutt_remove_duplicates (tmp);
1112 for (p = tmp; p; p = p->next) {
1113 char buf[LONG_STRING];
1118 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1121 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1123 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1124 if (is_numerical_keyid (keyID)) {
1125 if (strncmp (keyID, "0x", 2) == 0)
1127 goto bypass_selection; /* you don't see this. */
1130 /* check for e-mail address */
1131 if ((t = strchr (keyID, '@')) &&
1132 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1134 rfc822_qualify (addr, fqdn);
1138 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1141 mem_free (&keylist);
1142 rfc822_free_address (&tmp);
1143 rfc822_free_address (&addr);
1149 pgp_invoke_getkeys (q);
1153 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1154 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1156 if ((key = pgp_ask_for_key (buf, q->mailbox,
1157 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1158 mem_free (&keylist);
1159 rfc822_free_address (&tmp);
1160 rfc822_free_address (&addr);
1167 keyID = pgp_keyid (key);
1170 keylist_size += str_len (keyID) + 4;
1171 mem_realloc (&keylist, keylist_size);
1172 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1174 keylist_used = str_len (keylist);
1176 pgp_free_key (&key);
1177 rfc822_free_address (&addr);
1180 rfc822_free_address (&tmp);
1184 /* Warning: "a" is no longer freed in this routine, you need
1185 * to free it later. This is necessary for $fcc_attach. */
1187 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1189 char buf[LONG_STRING];
1190 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1191 char pgpinfile[_POSIX_PATH_MAX];
1192 FILE *pgpin, *pgperr, *fpout, *fptmp;
1198 mutt_mktemp (tempfile);
1199 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1200 mutt_perror (tempfile);
1204 mutt_mktemp (pgperrfile);
1205 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1206 mutt_perror (pgperrfile);
1211 unlink (pgperrfile);
1213 mutt_mktemp (pgpinfile);
1214 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1215 mutt_perror (pgpinfile);
1223 convert_to_7bit (a);
1225 mutt_write_mime_header (a, fptmp);
1226 fputc ('\n', fptmp);
1227 mutt_write_mime_body (a, fptmp);
1230 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1231 fileno (fpout), fileno (pgperr),
1232 pgpinfile, keylist, sign)) == -1) {
1239 if (!pgp_use_gpg_agent ())
1240 fputs (PgpPass, pgpin);
1241 fputc ('\n', pgpin);
1245 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1253 empty = (fgetc (fpout) == EOF);
1258 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1260 fputs (buf, stdout);
1264 /* pause if there is any error output from PGP */
1266 mutt_any_key_to_continue (NULL);
1269 /* fatal error while trying to encrypt message */
1271 pgp_void_passphrase (); /* just in case */
1276 t = mutt_new_body ();
1277 t->type = TYPEMULTIPART;
1278 t->subtype = str_dup ("encrypted");
1279 t->encoding = ENC7BIT;
1281 t->disposition = DISPINLINE;
1283 mutt_generate_boundary (&t->parameter);
1284 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1286 t->parts = mutt_new_body ();
1287 t->parts->type = TYPEAPPLICATION;
1288 t->parts->subtype = str_dup ("pgp-encrypted");
1289 t->parts->encoding = ENC7BIT;
1291 t->parts->next = mutt_new_body ();
1292 t->parts->next->type = TYPEAPPLICATION;
1293 t->parts->next->subtype = str_dup ("octet-stream");
1294 t->parts->next->encoding = ENC7BIT;
1295 t->parts->next->filename = str_dup (tempfile);
1296 t->parts->next->use_disp = 1;
1297 t->parts->next->disposition = DISPINLINE;
1298 t->parts->next->unlink = 1; /* delete after sending the message */
1299 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1304 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1308 char pgpoutfile[_POSIX_PATH_MAX];
1309 char pgperrfile[_POSIX_PATH_MAX];
1310 char pgpinfile[_POSIX_PATH_MAX];
1312 char body_charset[STRING];
1314 const char *send_charset;
1316 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1326 if (a->type != TYPETEXT)
1328 if (ascii_strcasecmp (a->subtype, "plain"))
1331 if ((fp = fopen (a->filename, "r")) == NULL) {
1332 mutt_perror (a->filename);
1336 mutt_mktemp (pgpinfile);
1337 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1338 mutt_perror (pgpinfile);
1343 /* The following code is really correct: If noconv is set,
1344 * a's charset parameter contains the on-disk character set, and
1345 * we have to convert from that to utf-8. If noconv is not set,
1346 * we have to convert from $charset to utf-8.
1349 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1351 from_charset = body_charset;
1353 from_charset = Charset;
1355 if (!mutt_is_us_ascii (body_charset)) {
1359 if (flags & ENCRYPT)
1360 send_charset = "us-ascii";
1362 send_charset = "utf-8";
1364 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1365 while ((c = fgetconv (fc)) != EOF)
1368 fgetconv_close (&fc);
1371 send_charset = "us-ascii";
1372 mutt_copy_stream (fp, pgpin);
1377 mutt_mktemp (pgpoutfile);
1378 mutt_mktemp (pgperrfile);
1379 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1380 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1381 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1385 unlink (pgpoutfile);
1390 unlink (pgperrfile);
1392 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1393 -1, fileno (pgpout), fileno (pgperr),
1394 pgpinfile, keylist, flags)) == -1) {
1395 mutt_perror (_("Can't invoke PGP"));
1399 mutt_unlink (pgpinfile);
1400 unlink (pgpoutfile);
1404 if (pgp_use_gpg_agent ())
1407 fprintf (pgpin, "%s\n", PgpPass);
1410 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1413 mutt_unlink (pgpinfile);
1422 empty = (fgetc (pgpout) == EOF);
1427 while (fgets (buff, sizeof (buff), pgperr)) {
1429 fputs (buff, stdout);
1435 mutt_any_key_to_continue (NULL);
1439 pgp_void_passphrase (); /* just in case */
1440 unlink (pgpoutfile);
1444 b = mutt_new_body ();
1446 b->encoding = ENC7BIT;
1449 b->subtype = str_dup ("plain");
1451 mutt_set_parameter ("x-action",
1452 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1454 mutt_set_parameter ("charset", send_charset, &b->parameter);
1456 b->filename = str_dup (pgpoutfile);
1459 /* The following is intended to give a clue to some completely brain-dead
1460 * "mail environments" which are typically used by large corporations.
1463 b->d_filename = str_dup ("msg.pgp");
1468 b->disposition = DISPINLINE;
1474 if (!(flags & ENCRYPT))
1475 b->encoding = a->encoding;
1480 int pgp_send_menu (HEADER * msg, int *redraw)
1483 char input_signas[SHORT_STRING];
1485 char prompt[LONG_STRING];
1487 if (!(WithCrypto & APPLICATION_PGP))
1488 return msg->security;
1490 /* If autoinline and no crypto options set, then set inline. */
1491 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1492 && (msg->security & (SIGN | ENCRYPT))))
1493 msg->security |= INLINE;
1495 snprintf (prompt, sizeof (prompt),
1496 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1497 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1499 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1500 case 1: /* (e)ncrypt */
1501 msg->security |= ENCRYPT;
1502 msg->security &= ~SIGN;
1505 case 2: /* (s)ign */
1506 msg->security |= SIGN;
1507 msg->security &= ~ENCRYPT;
1510 case 3: /* sign (a)s */
1511 unset_option (OPTPGPCHECKTRUST);
1514 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1516 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1517 str_replace (&PgpSignAs, input_signas);
1520 msg->security |= SIGN;
1522 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1526 msg->security &= ~SIGN;
1530 *redraw = REDRAW_FULL;
1533 case 4: /* (b)oth */
1534 msg->security |= (ENCRYPT | SIGN);
1537 case 5: /* (i)nline */
1538 if ((msg->security & (ENCRYPT | SIGN)))
1539 msg->security ^= INLINE;
1541 msg->security &= ~INLINE;
1544 case 6: /* (f)orget it */
1545 case 7: /* (c)lear */
1550 if (msg->security) {
1551 if (!(msg->security & (ENCRYPT | SIGN)))
1554 msg->security |= APPLICATION_PGP;
1557 return (msg->security);
1561 #endif /* CRYPT_BACKEND_CLASSIC_PGP */