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 needpass = -1, pgp_keyblock = 0;
238 int clearsign = 0, rv, rc;
241 LOFF_T last_pos, offset;
242 char buf[HUGE_STRING];
243 char outfile[_POSIX_PATH_MAX];
244 char tmpfname[_POSIX_PATH_MAX];
245 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
249 short maybe_goodsig = 1;
250 short have_any_sigs = 0;
252 char body_charset[STRING];
254 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
256 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
258 fseeko (s->fpin, m->offset, 0);
259 last_pos = m->offset;
261 for (bytes = m->length; bytes > 0;) {
262 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
265 offset = ftello (s->fpin);
266 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
269 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
271 start_pos = last_pos;
273 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
275 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
279 else if (!option (OPTDONTHANDLEPGPKEYS) &&
280 str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
285 /* XXX - we may wish to recode here */
287 state_puts (s->prefix, s);
292 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
294 /* Copy PGP material to temporary file */
295 mutt_mktemp (tmpfname);
296 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
297 mutt_perror (tmpfname);
302 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
303 offset = ftello (s->fpin);
304 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
310 && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
312 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
313 || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
318 /* leave tmpfp open in case we still need it - but flush it! */
322 /* Invoke PGP if needed */
323 if (!clearsign || (s->flags & M_VERIFY)) {
324 mutt_mktemp (outfile);
325 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
326 mutt_perror (tmpfname);
330 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
331 fileno (pgpout), -1, tmpfname,
333 safe_fclose (&pgpout);
338 ("[-- Error: unable to create PGP subprocess! --]\n"),
341 else { /* PGP started successfully */
344 if (!pgp_valid_passphrase ())
345 pgp_void_passphrase ();
346 if (pgp_use_gpg_agent ())
348 fprintf (pgpin, "%s\n", PgpPass);
351 safe_fclose (&pgpin);
353 if (s->flags & M_DISPLAY) {
354 crypt_current_time (s, "PGP");
355 rc = pgp_copy_checksig (pgperr, s->fpout);
358 safe_fclose (&pgperr);
359 rv = mutt_wait_filter (thepid);
361 if (s->flags & M_DISPLAY) {
367 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
368 * Sig _is_ correct if
369 * gpg_good_sign="" && pgp_decode_command returned 0
374 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
378 /* treat empty result as sign of failure */
379 /* TODO: maybe on failure mutt should include the original undecoded text. */
385 if (!clearsign && (!pgpout || c == EOF)) {
386 mutt_error _("Could not decrypt PGP message");
388 pgp_void_passphrase ();
389 if (!(s->flags & M_DISPLAY)) {
397 * Now, copy cleartext to the screen. NOTE - we expect that PGP
398 * outputs utf-8 cleartext. This may not always be true, but it
399 * seems to be a reasonable guess.
402 if (s->flags & M_DISPLAY) {
404 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
405 else if (pgp_keyblock)
406 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
408 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
414 pgp_copy_clearsigned (tmpfp, s, body_charset);
421 state_set_prefix (s);
422 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
423 while ((c = fgetconv (fc)) != EOF)
424 state_prefix_putc (c, s);
425 fgetconv_close (&fc);
428 if (s->flags & M_DISPLAY) {
429 state_putc ('\n', s);
431 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
432 mutt_message _("PGP message successfully decrypted.");
434 else if (pgp_keyblock)
435 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
437 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
442 /* XXX - we may wish to recode here */
444 state_puts (s->prefix, s);
452 m->goodsig = (maybe_goodsig && have_any_sigs);
455 safe_fclose (&tmpfp);
456 mutt_unlink (tmpfname);
459 safe_fclose (&pgpout);
460 mutt_unlink (outfile);
463 if (needpass == -1) {
465 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
473 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
476 char tempfile[_POSIX_PATH_MAX];
477 char buf[HUGE_STRING];
484 if (b->type != TYPETEXT)
487 if (tagged_only && !b->tagged)
490 mutt_mktemp (tempfile);
491 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
496 if ((tfp = fopen (tempfile, "r")) == NULL) {
501 while (fgets (buf, sizeof (buf), tfp)) {
502 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
503 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
505 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
507 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
514 if (!enc && !sgn && !key)
517 /* fix the content type */
519 mutt_set_parameter ("format", "fixed", &b->parameter);
521 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
523 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
525 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
530 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
535 for (; b; b = b->next) {
536 if (is_multipart (b))
537 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
538 else if (b->type == TYPETEXT) {
539 if ((r = mutt_is_application_pgp (b)))
542 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
553 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
555 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
556 FILE *fp, *pgpout, *pgperr;
561 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
563 if (!(fp = safe_fopen (sigfile, "w"))) {
564 mutt_perror (sigfile);
568 fseeko (s->fpin, sigbdy->offset, 0);
569 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
572 mutt_mktemp (pgperrfile);
573 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
574 mutt_perror (pgperrfile);
579 crypt_current_time (s, "PGP");
581 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
582 -1, -1, fileno (pgperr),
583 tempfile, sigfile)) != -1) {
584 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
588 safe_fclose (&pgpout);
592 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
595 if ((rv = mutt_wait_filter (thepid)))
598 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
601 safe_fclose (&pgperr);
603 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
605 mutt_unlink (sigfile);
606 mutt_unlink (pgperrfile);
608 debug_print (1, ("returning %d.\n", badsig));
614 /* Extract pgp public keys from messages or attachments */
616 void pgp_extract_keys_from_messages (HEADER * h)
619 char tempfname[_POSIX_PATH_MAX];
623 mutt_parse_mime_message (Context, h);
624 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
628 mutt_mktemp (tempfname);
629 if (!(fpout = safe_fopen (tempfname, "w"))) {
630 mutt_perror (tempfname);
634 set_option (OPTDONTHANDLEPGPKEYS);
637 for (i = 0; i < Context->vcount; i++) {
638 if (Context->hdrs[Context->v2r[i]]->tagged) {
639 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
640 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
641 && !pgp_valid_passphrase ()) {
645 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
646 M_CM_DECODE | M_CM_CHARCONV, 0);
651 mutt_parse_mime_message (Context, h);
652 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
656 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
661 pgp_invoke_import (tempfname);
662 mutt_any_key_to_continue (NULL);
666 mutt_unlink (tempfname);
667 unset_option (OPTDONTHANDLEPGPKEYS);
671 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
675 char tempfname[_POSIX_PATH_MAX];
677 mutt_mktemp (tempfname);
678 if (!(tempfp = safe_fopen (tempfname, "w"))) {
679 mutt_perror (tempfname);
683 memset (&s, 0, sizeof (STATE));
688 mutt_body_handler (top, &s);
692 pgp_invoke_import (tempfname);
693 mutt_any_key_to_continue (NULL);
695 mutt_unlink (tempfname);
698 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
701 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
707 set_option (OPTDONTHANDLEPGPKEYS);
709 for (; top; top = top->next) {
710 if (!tag || top->tagged)
711 pgp_extract_keys_from_attachment (fp, top);
717 unset_option (OPTDONTHANDLEPGPKEYS);
720 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
722 char buf[LONG_STRING];
723 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
727 char pgperrfile[_POSIX_PATH_MAX];
728 char pgptmpfile[_POSIX_PATH_MAX];
732 mutt_mktemp (pgperrfile);
733 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
734 mutt_perror (pgperrfile);
739 mutt_mktemp (pgptmpfile);
740 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
741 mutt_perror (pgptmpfile);
746 /* Position the stream at the beginning of the body, and send the data to
747 * the temporary file.
750 fseeko (s->fpin, a->offset, 0);
751 mutt_copy_bytes (s->fpin, pgptmp, a->length);
754 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
755 fileno (pgperr), pgptmpfile)) == -1) {
758 if (s->flags & M_DISPLAY)
760 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
765 /* send the PGP passphrase to the subprocess. Never do this if the
766 agent is active, because this might lead to a passphrase send as
768 if (!pgp_use_gpg_agent ())
769 fputs (PgpPass, pgpin);
773 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
774 * read_mime_header has a hard time parsing the message.
776 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
778 if (len > 1 && buf[len - 2] == '\r')
779 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
784 rv = mutt_wait_filter (thepid);
785 mutt_unlink (pgptmpfile);
787 if (s->flags & M_DISPLAY) {
790 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
794 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
801 if (fgetc (fpout) == EOF) {
802 mutt_error (_("Decryption failed."));
803 pgp_void_passphrase ();
809 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
811 * Need to set the length of this body part.
813 fstat (fileno (fpout), &info);
814 tattach->length = info.st_size - tattach->offset;
816 /* See if we need to recurse on this MIME part. */
818 mutt_parse_part (fpout, tattach);
824 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
826 char tempfile[_POSIX_PATH_MAX];
830 if (!mutt_is_multipart_encrypted (b))
833 if (!b->parts || !b->parts->next)
838 memset (&s, 0, sizeof (s));
840 mutt_mktemp (tempfile);
841 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
842 mutt_perror (tempfile);
847 *cur = pgp_decrypt_part (b, &s, *fpout, p);
857 int pgp_encrypted_handler (BODY * a, STATE * s)
859 char tempfile[_POSIX_PATH_MAX];
866 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
867 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
868 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
869 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
870 if (s->flags & M_DISPLAY)
871 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
877 * Move forward to the application/pgp-encrypted body.
881 mutt_mktemp (tempfile);
882 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
883 if (s->flags & M_DISPLAY)
885 ("[-- Error: could not create temporary file! --]\n"),
890 if (s->flags & M_DISPLAY)
891 crypt_current_time (s, "PGP");
893 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
894 if (s->flags & M_DISPLAY)
896 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
901 rc = mutt_body_handler (tattach, s);
905 * if a multipart/signed is the _only_ sub-part of a
906 * multipart/encrypted, cache signature verification
911 if (mutt_is_multipart_signed (tattach) && !tattach->next)
912 p->goodsig |= tattach->goodsig;
914 if (s->flags & M_DISPLAY) {
915 state_puts ("\n", s);
916 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
919 mutt_free_body (&tattach);
920 /* clear 'Invoking...' message, since there's no error */
921 mutt_message _("PGP message successfully decrypted.");
923 mutt_error _("Could not decrypt PGP message");
924 pgp_void_passphrase ();
929 mutt_unlink (tempfile);
934 /* ----------------------------------------------------------------------------
935 * Routines for sending PGP/MIME messages.
939 BODY *pgp_sign_message (BODY * a)
942 char buffer[LONG_STRING];
943 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
944 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
949 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
951 mutt_mktemp (sigfile);
952 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
956 mutt_mktemp (signedfile);
957 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
958 mutt_perror (signedfile);
964 mutt_write_mime_header (a, sfp);
966 mutt_write_mime_body (a, sfp);
969 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
970 -1, -1, -1, signedfile)) == -1) {
971 mutt_perror (_("Can't open PGP subprocess!"));
979 if (!pgp_use_gpg_agent ())
980 fputs (PgpPass, pgpin);
985 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
986 * recommended for future releases of PGP.
988 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
989 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
990 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
991 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
992 fputs ("-----END PGP SIGNATURE-----\n", fp);
995 empty = 0; /* got some output, so we're ok */
998 /* check for errors from PGP */
1000 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
1002 fputs (buffer, stdout);
1005 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1010 unlink (signedfile);
1012 if (fclose (fp) != 0) {
1013 mutt_perror ("fclose");
1019 pgp_void_passphrase ();
1020 mutt_any_key_to_continue (NULL);
1025 return (NULL); /* fatal error while signing */
1028 t = mutt_new_body ();
1029 t->type = TYPEMULTIPART;
1030 t->subtype = str_dup ("signed");
1031 t->encoding = ENC7BIT;
1033 t->disposition = DISPINLINE;
1035 mutt_generate_boundary (&t->parameter);
1036 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1037 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1042 t->parts->next = mutt_new_body ();
1044 t->type = TYPEAPPLICATION;
1045 t->subtype = str_dup ("pgp-signature");
1046 t->filename = str_dup (sigfile);
1048 t->disposition = DISPINLINE;
1049 t->encoding = ENC7BIT;
1050 t->unlink = 1; /* ok to remove this file after sending. */
1055 static short is_numerical_keyid (const char *s)
1057 /* or should we require the "0x"? */
1058 if (strncmp (s, "0x", 2) == 0)
1060 if (str_len (s) % 8)
1063 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1069 /* This routine attempts to find the keyids of the recipients of a message.
1070 * It returns NULL if any of the keys can not be found.
1072 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1074 char *keyID, *keylist = NULL, *t;
1075 size_t keylist_size = 0;
1076 size_t keylist_used = 0;
1077 ADDRESS *tmp = NULL, *addr = NULL;
1078 ADDRESS **last = &tmp;
1081 pgp_key_t k_info = NULL, key = NULL;
1083 const char *fqdn = mutt_fqdn (1);
1085 for (i = 0; i < 3; i++) {
1100 *last = rfc822_cpy_adr (p);
1102 last = &((*last)->next);
1106 rfc822_qualify (tmp, fqdn);
1108 tmp = mutt_remove_duplicates (tmp);
1110 for (p = tmp; p; p = p->next) {
1111 char buf[LONG_STRING];
1116 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1119 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1121 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1122 if (is_numerical_keyid (keyID)) {
1123 if (strncmp (keyID, "0x", 2) == 0)
1125 goto bypass_selection; /* you don't see this. */
1128 /* check for e-mail address */
1129 if ((t = strchr (keyID, '@')) &&
1130 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1132 rfc822_qualify (addr, fqdn);
1136 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1139 mem_free (&keylist);
1140 rfc822_free_address (&tmp);
1141 rfc822_free_address (&addr);
1147 pgp_invoke_getkeys (q);
1151 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1152 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1154 if ((key = pgp_ask_for_key (buf, q->mailbox,
1155 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1156 mem_free (&keylist);
1157 rfc822_free_address (&tmp);
1158 rfc822_free_address (&addr);
1165 keyID = pgp_keyid (key);
1168 keylist_size += str_len (keyID) + 4;
1169 mem_realloc (&keylist, keylist_size);
1170 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1172 keylist_used = str_len (keylist);
1174 pgp_free_key (&key);
1175 rfc822_free_address (&addr);
1178 rfc822_free_address (&tmp);
1182 /* Warning: "a" is no longer freed in this routine, you need
1183 * to free it later. This is necessary for $fcc_attach. */
1185 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1187 char buf[LONG_STRING];
1188 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1189 char pgpinfile[_POSIX_PATH_MAX];
1190 FILE *pgpin, *pgperr, *fpout, *fptmp;
1196 mutt_mktemp (tempfile);
1197 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1198 mutt_perror (tempfile);
1202 mutt_mktemp (pgperrfile);
1203 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1204 mutt_perror (pgperrfile);
1209 unlink (pgperrfile);
1211 mutt_mktemp (pgpinfile);
1212 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1213 mutt_perror (pgpinfile);
1221 convert_to_7bit (a);
1223 mutt_write_mime_header (a, fptmp);
1224 fputc ('\n', fptmp);
1225 mutt_write_mime_body (a, fptmp);
1228 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1229 fileno (fpout), fileno (pgperr),
1230 pgpinfile, keylist, sign)) == -1) {
1237 if (!pgp_use_gpg_agent ())
1238 fputs (PgpPass, pgpin);
1239 fputc ('\n', pgpin);
1243 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1251 empty = (fgetc (fpout) == EOF);
1256 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1258 fputs (buf, stdout);
1262 /* pause if there is any error output from PGP */
1264 mutt_any_key_to_continue (NULL);
1267 /* fatal error while trying to encrypt message */
1269 pgp_void_passphrase (); /* just in case */
1274 t = mutt_new_body ();
1275 t->type = TYPEMULTIPART;
1276 t->subtype = str_dup ("encrypted");
1277 t->encoding = ENC7BIT;
1279 t->disposition = DISPINLINE;
1281 mutt_generate_boundary (&t->parameter);
1282 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1284 t->parts = mutt_new_body ();
1285 t->parts->type = TYPEAPPLICATION;
1286 t->parts->subtype = str_dup ("pgp-encrypted");
1287 t->parts->encoding = ENC7BIT;
1289 t->parts->next = mutt_new_body ();
1290 t->parts->next->type = TYPEAPPLICATION;
1291 t->parts->next->subtype = str_dup ("octet-stream");
1292 t->parts->next->encoding = ENC7BIT;
1293 t->parts->next->filename = str_dup (tempfile);
1294 t->parts->next->use_disp = 1;
1295 t->parts->next->disposition = DISPINLINE;
1296 t->parts->next->unlink = 1; /* delete after sending the message */
1297 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1302 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1306 char pgpoutfile[_POSIX_PATH_MAX];
1307 char pgperrfile[_POSIX_PATH_MAX];
1308 char pgpinfile[_POSIX_PATH_MAX];
1310 char body_charset[STRING];
1312 const char *send_charset;
1314 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1324 if (a->type != TYPETEXT)
1326 if (ascii_strcasecmp (a->subtype, "plain"))
1329 if ((fp = fopen (a->filename, "r")) == NULL) {
1330 mutt_perror (a->filename);
1334 mutt_mktemp (pgpinfile);
1335 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1336 mutt_perror (pgpinfile);
1341 /* The following code is really correct: If noconv is set,
1342 * a's charset parameter contains the on-disk character set, and
1343 * we have to convert from that to utf-8. If noconv is not set,
1344 * we have to convert from $charset to utf-8.
1347 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1349 from_charset = body_charset;
1351 from_charset = Charset;
1353 if (!mutt_is_us_ascii (body_charset)) {
1357 if (flags & ENCRYPT)
1358 send_charset = "us-ascii";
1360 send_charset = "utf-8";
1362 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1363 while ((c = fgetconv (fc)) != EOF)
1366 fgetconv_close (&fc);
1369 send_charset = "us-ascii";
1370 mutt_copy_stream (fp, pgpin);
1375 mutt_mktemp (pgpoutfile);
1376 mutt_mktemp (pgperrfile);
1377 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1378 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1379 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1383 unlink (pgpoutfile);
1388 unlink (pgperrfile);
1390 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1391 -1, fileno (pgpout), fileno (pgperr),
1392 pgpinfile, keylist, flags)) == -1) {
1393 mutt_perror (_("Can't invoke PGP"));
1397 mutt_unlink (pgpinfile);
1398 unlink (pgpoutfile);
1402 if (pgp_use_gpg_agent ())
1405 fprintf (pgpin, "%s\n", PgpPass);
1408 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1411 mutt_unlink (pgpinfile);
1420 empty = (fgetc (pgpout) == EOF);
1425 while (fgets (buff, sizeof (buff), pgperr)) {
1427 fputs (buff, stdout);
1433 mutt_any_key_to_continue (NULL);
1437 pgp_void_passphrase (); /* just in case */
1438 unlink (pgpoutfile);
1442 b = mutt_new_body ();
1444 b->encoding = ENC7BIT;
1447 b->subtype = str_dup ("plain");
1449 mutt_set_parameter ("x-action",
1450 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1452 mutt_set_parameter ("charset", send_charset, &b->parameter);
1454 b->filename = str_dup (pgpoutfile);
1457 /* The following is intended to give a clue to some completely brain-dead
1458 * "mail environments" which are typically used by large corporations.
1461 b->d_filename = str_dup ("msg.pgp");
1466 b->disposition = DISPINLINE;
1472 if (!(flags & ENCRYPT))
1473 b->encoding = a->encoding;
1478 int pgp_send_menu (HEADER * msg, int *redraw)
1481 char input_signas[SHORT_STRING];
1483 char prompt[LONG_STRING];
1485 if (!(WithCrypto & APPLICATION_PGP))
1486 return msg->security;
1488 /* If autoinline and no crypto options set, then set inline. */
1489 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1490 && (msg->security & (SIGN | ENCRYPT))))
1491 msg->security |= INLINE;
1493 snprintf (prompt, sizeof (prompt),
1494 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1495 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1497 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1498 case 1: /* (e)ncrypt */
1499 msg->security |= ENCRYPT;
1500 msg->security &= ~SIGN;
1503 case 2: /* (s)ign */
1504 msg->security |= SIGN;
1505 msg->security &= ~ENCRYPT;
1508 case 3: /* sign (a)s */
1509 unset_option (OPTPGPCHECKTRUST);
1512 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1514 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1515 str_replace (&PgpSignAs, input_signas);
1518 msg->security |= SIGN;
1520 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1524 msg->security &= ~SIGN;
1528 *redraw = REDRAW_FULL;
1531 case 4: /* (b)oth */
1532 msg->security |= (ENCRYPT | SIGN);
1535 case 5: /* (i)nline */
1536 if ((msg->security & (ENCRYPT | SIGN)))
1537 msg->security ^= INLINE;
1539 msg->security &= ~INLINE;
1542 case 6: /* (f)orget it */
1543 case 7: /* (c)lear */
1548 if (msg->security) {
1549 if (!(msg->security & (ENCRYPT | SIGN)))
1552 msg->security |= APPLICATION_PGP;
1555 return (msg->security);
1559 #endif /* CRYPT_BACKEND_CLASSIC_PGP */