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 */
373 if ((c = fgetc (pgpout)) == EOF) {
374 mutt_error _("Could not decrypt PGP message");
375 pgp_void_passphrase ();
383 * Now, copy cleartext to the screen. NOTE - we expect that PGP
384 * outputs utf-8 cleartext. This may not always be true, but it
385 * seems to be a reasonable guess.
388 if (s->flags & M_DISPLAY) {
390 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
391 else if (pgp_keyblock)
392 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
394 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
400 pgp_copy_clearsigned (tmpfp, s, body_charset);
407 state_set_prefix (s);
408 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
409 while ((c = fgetconv (fc)) != EOF)
410 state_prefix_putc (c, s);
411 fgetconv_close (&fc);
414 if (s->flags & M_DISPLAY) {
415 state_putc ('\n', s);
417 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
418 mutt_message _("PGP message successfully decrypted.");
420 else if (pgp_keyblock)
421 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
423 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
428 /* XXX - we may wish to recode here */
430 state_puts (s->prefix, s);
438 m->goodsig = (maybe_goodsig && have_any_sigs);
441 safe_fclose (&tmpfp);
442 mutt_unlink (tmpfname);
445 safe_fclose (&pgpout);
446 mutt_unlink (outfile);
449 if (needpass == -1) {
451 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
459 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
462 char tempfile[_POSIX_PATH_MAX];
463 char buf[HUGE_STRING];
470 if (b->type != TYPETEXT)
473 if (tagged_only && !b->tagged)
476 mutt_mktemp (tempfile);
477 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
482 if ((tfp = fopen (tempfile, "r")) == NULL) {
487 while (fgets (buf, sizeof (buf), tfp)) {
488 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
489 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
491 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
493 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
500 if (!enc && !sgn && !key)
503 /* fix the content type */
505 mutt_set_parameter ("format", "fixed", &b->parameter);
507 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
509 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
511 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
516 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
521 for (; b; b = b->next) {
522 if (is_multipart (b))
523 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
524 else if (b->type == TYPETEXT) {
525 if ((r = mutt_is_application_pgp (b)))
528 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
539 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
541 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
542 FILE *fp, *pgpout, *pgperr;
547 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
549 if (!(fp = safe_fopen (sigfile, "w"))) {
550 mutt_perror (sigfile);
554 fseek (s->fpin, sigbdy->offset, 0);
555 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
558 mutt_mktemp (pgperrfile);
559 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
560 mutt_perror (pgperrfile);
565 crypt_current_time (s, "PGP");
567 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
568 -1, -1, fileno (pgperr),
569 tempfile, sigfile)) != -1) {
570 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
574 safe_fclose (&pgpout);
578 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
581 if ((rv = mutt_wait_filter (thepid)))
584 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
587 safe_fclose (&pgperr);
589 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
591 mutt_unlink (sigfile);
592 mutt_unlink (pgperrfile);
594 debug_print (1, ("returning %d.\n", badsig));
600 /* Extract pgp public keys from messages or attachments */
602 void pgp_extract_keys_from_messages (HEADER * h)
605 char tempfname[_POSIX_PATH_MAX];
609 mutt_parse_mime_message (Context, h);
610 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
614 mutt_mktemp (tempfname);
615 if (!(fpout = safe_fopen (tempfname, "w"))) {
616 mutt_perror (tempfname);
620 set_option (OPTDONTHANDLEPGPKEYS);
623 for (i = 0; i < Context->vcount; i++) {
624 if (Context->hdrs[Context->v2r[i]]->tagged) {
625 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
626 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
627 && !pgp_valid_passphrase ()) {
631 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
632 M_CM_DECODE | M_CM_CHARCONV, 0);
637 mutt_parse_mime_message (Context, h);
638 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
642 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
647 pgp_invoke_import (tempfname);
648 mutt_any_key_to_continue (NULL);
652 mutt_unlink (tempfname);
653 unset_option (OPTDONTHANDLEPGPKEYS);
657 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
661 char tempfname[_POSIX_PATH_MAX];
663 mutt_mktemp (tempfname);
664 if (!(tempfp = safe_fopen (tempfname, "w"))) {
665 mutt_perror (tempfname);
669 memset (&s, 0, sizeof (STATE));
674 mutt_body_handler (top, &s);
678 pgp_invoke_import (tempfname);
679 mutt_any_key_to_continue (NULL);
681 mutt_unlink (tempfname);
684 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
687 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
693 set_option (OPTDONTHANDLEPGPKEYS);
695 for (; top; top = top->next) {
696 if (!tag || top->tagged)
697 pgp_extract_keys_from_attachment (fp, top);
703 unset_option (OPTDONTHANDLEPGPKEYS);
706 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
708 char buf[LONG_STRING];
709 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
713 char pgperrfile[_POSIX_PATH_MAX];
714 char pgptmpfile[_POSIX_PATH_MAX];
718 mutt_mktemp (pgperrfile);
719 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
720 mutt_perror (pgperrfile);
725 mutt_mktemp (pgptmpfile);
726 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
727 mutt_perror (pgptmpfile);
732 /* Position the stream at the beginning of the body, and send the data to
733 * the temporary file.
736 fseek (s->fpin, a->offset, 0);
737 mutt_copy_bytes (s->fpin, pgptmp, a->length);
740 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
741 fileno (pgperr), pgptmpfile)) == -1) {
744 if (s->flags & M_DISPLAY)
746 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
751 /* send the PGP passphrase to the subprocess. Never do this if the
752 agent is active, because this might lead to a passphrase send as
754 if (!pgp_use_gpg_agent ())
755 fputs (PgpPass, pgpin);
759 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
760 * read_mime_header has a hard time parsing the message.
762 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
764 if (len > 1 && buf[len - 2] == '\r')
765 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
770 rv = mutt_wait_filter (thepid);
771 mutt_unlink (pgptmpfile);
773 if (s->flags & M_DISPLAY) {
776 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
780 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
787 if (fgetc (fpout) == EOF) {
788 mutt_error (_("Decryption failed."));
789 pgp_void_passphrase ();
795 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
797 * Need to set the length of this body part.
799 fstat (fileno (fpout), &info);
800 tattach->length = info.st_size - tattach->offset;
802 /* See if we need to recurse on this MIME part. */
804 mutt_parse_part (fpout, tattach);
810 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
812 char tempfile[_POSIX_PATH_MAX];
816 if (!mutt_is_multipart_encrypted (b))
819 if (!b->parts || !b->parts->next)
824 memset (&s, 0, sizeof (s));
826 mutt_mktemp (tempfile);
827 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
828 mutt_perror (tempfile);
833 *cur = pgp_decrypt_part (b, &s, *fpout, p);
843 int pgp_encrypted_handler (BODY * a, STATE * s)
845 char tempfile[_POSIX_PATH_MAX];
852 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
853 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
854 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
855 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
856 if (s->flags & M_DISPLAY)
857 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
863 * Move forward to the application/pgp-encrypted body.
867 mutt_mktemp (tempfile);
868 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
869 if (s->flags & M_DISPLAY)
871 ("[-- Error: could not create temporary file! --]\n"),
876 if (s->flags & M_DISPLAY)
877 crypt_current_time (s, "PGP");
879 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
880 if (s->flags & M_DISPLAY)
882 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
887 rc = mutt_body_handler (tattach, s);
891 * if a multipart/signed is the _only_ sub-part of a
892 * multipart/encrypted, cache signature verification
897 if (mutt_is_multipart_signed (tattach) && !tattach->next)
898 p->goodsig |= tattach->goodsig;
900 if (s->flags & M_DISPLAY) {
901 state_puts ("\n", s);
902 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
905 mutt_free_body (&tattach);
906 /* clear 'Invoking...' message, since there's no error */
907 mutt_message _("PGP message successfully decrypted.");
909 mutt_error _("Could not decrypt PGP message");
910 pgp_void_passphrase ();
915 mutt_unlink (tempfile);
920 /* ----------------------------------------------------------------------------
921 * Routines for sending PGP/MIME messages.
925 BODY *pgp_sign_message (BODY * a)
928 char buffer[LONG_STRING];
929 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
930 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
935 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
937 mutt_mktemp (sigfile);
938 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
942 mutt_mktemp (signedfile);
943 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
944 mutt_perror (signedfile);
950 mutt_write_mime_header (a, sfp);
952 mutt_write_mime_body (a, sfp);
955 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
956 -1, -1, -1, signedfile)) == -1) {
957 mutt_perror (_("Can't open PGP subprocess!"));
965 if (!pgp_use_gpg_agent ())
966 fputs (PgpPass, pgpin);
971 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
972 * recommended for future releases of PGP.
974 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
975 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
976 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
977 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
978 fputs ("-----END PGP SIGNATURE-----\n", fp);
981 empty = 0; /* got some output, so we're ok */
984 /* check for errors from PGP */
986 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
988 fputs (buffer, stdout);
991 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
998 if (fclose (fp) != 0) {
999 mutt_perror ("fclose");
1005 pgp_void_passphrase ();
1006 mutt_any_key_to_continue (NULL);
1011 return (NULL); /* fatal error while signing */
1014 t = mutt_new_body ();
1015 t->type = TYPEMULTIPART;
1016 t->subtype = str_dup ("signed");
1017 t->encoding = ENC7BIT;
1019 t->disposition = DISPINLINE;
1021 mutt_generate_boundary (&t->parameter);
1022 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1023 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1028 t->parts->next = mutt_new_body ();
1030 t->type = TYPEAPPLICATION;
1031 t->subtype = str_dup ("pgp-signature");
1032 t->filename = str_dup (sigfile);
1034 t->disposition = DISPINLINE;
1035 t->encoding = ENC7BIT;
1036 t->unlink = 1; /* ok to remove this file after sending. */
1041 static short is_numerical_keyid (const char *s)
1043 /* or should we require the "0x"? */
1044 if (strncmp (s, "0x", 2) == 0)
1046 if (str_len (s) % 8)
1049 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1055 /* This routine attempts to find the keyids of the recipients of a message.
1056 * It returns NULL if any of the keys can not be found.
1058 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1060 char *keyID, *keylist = NULL, *t;
1061 size_t keylist_size = 0;
1062 size_t keylist_used = 0;
1063 ADDRESS *tmp = NULL, *addr = NULL;
1064 ADDRESS **last = &tmp;
1067 pgp_key_t k_info = NULL, key = NULL;
1069 const char *fqdn = mutt_fqdn (1);
1071 for (i = 0; i < 3; i++) {
1086 *last = rfc822_cpy_adr (p);
1088 last = &((*last)->next);
1092 rfc822_qualify (tmp, fqdn);
1094 tmp = mutt_remove_duplicates (tmp);
1096 for (p = tmp; p; p = p->next) {
1097 char buf[LONG_STRING];
1102 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1105 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1107 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1108 if (is_numerical_keyid (keyID)) {
1109 if (strncmp (keyID, "0x", 2) == 0)
1111 goto bypass_selection; /* you don't see this. */
1114 /* check for e-mail address */
1115 if ((t = strchr (keyID, '@')) &&
1116 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1118 rfc822_qualify (addr, fqdn);
1122 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1125 mem_free (&keylist);
1126 rfc822_free_address (&tmp);
1127 rfc822_free_address (&addr);
1133 pgp_invoke_getkeys (q);
1137 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1138 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1140 if ((key = pgp_ask_for_key (buf, q->mailbox,
1141 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1142 mem_free (&keylist);
1143 rfc822_free_address (&tmp);
1144 rfc822_free_address (&addr);
1151 keyID = pgp_keyid (key);
1154 keylist_size += str_len (keyID) + 4;
1155 mem_realloc (&keylist, keylist_size);
1156 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1158 keylist_used = str_len (keylist);
1160 pgp_free_key (&key);
1161 rfc822_free_address (&addr);
1164 rfc822_free_address (&tmp);
1168 /* Warning: "a" is no longer freed in this routine, you need
1169 * to free it later. This is necessary for $fcc_attach. */
1171 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1173 char buf[LONG_STRING];
1174 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1175 char pgpinfile[_POSIX_PATH_MAX];
1176 FILE *pgpin, *pgperr, *fpout, *fptmp;
1182 mutt_mktemp (tempfile);
1183 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1184 mutt_perror (tempfile);
1188 mutt_mktemp (pgperrfile);
1189 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1190 mutt_perror (pgperrfile);
1195 unlink (pgperrfile);
1197 mutt_mktemp (pgpinfile);
1198 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1199 mutt_perror (pgpinfile);
1207 convert_to_7bit (a);
1209 mutt_write_mime_header (a, fptmp);
1210 fputc ('\n', fptmp);
1211 mutt_write_mime_body (a, fptmp);
1214 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1215 fileno (fpout), fileno (pgperr),
1216 pgpinfile, keylist, sign)) == -1) {
1223 if (!pgp_use_gpg_agent ())
1224 fputs (PgpPass, pgpin);
1225 fputc ('\n', pgpin);
1229 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1237 empty = (fgetc (fpout) == EOF);
1242 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1244 fputs (buf, stdout);
1248 /* pause if there is any error output from PGP */
1250 mutt_any_key_to_continue (NULL);
1253 /* fatal error while trying to encrypt message */
1255 pgp_void_passphrase (); /* just in case */
1260 t = mutt_new_body ();
1261 t->type = TYPEMULTIPART;
1262 t->subtype = str_dup ("encrypted");
1263 t->encoding = ENC7BIT;
1265 t->disposition = DISPINLINE;
1267 mutt_generate_boundary (&t->parameter);
1268 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1270 t->parts = mutt_new_body ();
1271 t->parts->type = TYPEAPPLICATION;
1272 t->parts->subtype = str_dup ("pgp-encrypted");
1273 t->parts->encoding = ENC7BIT;
1275 t->parts->next = mutt_new_body ();
1276 t->parts->next->type = TYPEAPPLICATION;
1277 t->parts->next->subtype = str_dup ("octet-stream");
1278 t->parts->next->encoding = ENC7BIT;
1279 t->parts->next->filename = str_dup (tempfile);
1280 t->parts->next->use_disp = 1;
1281 t->parts->next->disposition = DISPINLINE;
1282 t->parts->next->unlink = 1; /* delete after sending the message */
1283 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1288 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1292 char pgpoutfile[_POSIX_PATH_MAX];
1293 char pgperrfile[_POSIX_PATH_MAX];
1294 char pgpinfile[_POSIX_PATH_MAX];
1296 char body_charset[STRING];
1298 const char *send_charset;
1300 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1310 if (a->type != TYPETEXT)
1312 if (ascii_strcasecmp (a->subtype, "plain"))
1315 if ((fp = fopen (a->filename, "r")) == NULL) {
1316 mutt_perror (a->filename);
1320 mutt_mktemp (pgpinfile);
1321 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1322 mutt_perror (pgpinfile);
1327 /* The following code is really correct: If noconv is set,
1328 * a's charset parameter contains the on-disk character set, and
1329 * we have to convert from that to utf-8. If noconv is not set,
1330 * we have to convert from $charset to utf-8.
1333 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1335 from_charset = body_charset;
1337 from_charset = Charset;
1339 if (!mutt_is_us_ascii (body_charset)) {
1343 if (flags & ENCRYPT)
1344 send_charset = "us-ascii";
1346 send_charset = "utf-8";
1348 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1349 while ((c = fgetconv (fc)) != EOF)
1352 fgetconv_close (&fc);
1355 send_charset = "us-ascii";
1356 mutt_copy_stream (fp, pgpin);
1361 mutt_mktemp (pgpoutfile);
1362 mutt_mktemp (pgperrfile);
1363 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1364 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1365 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1369 unlink (pgpoutfile);
1374 unlink (pgperrfile);
1376 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1377 -1, fileno (pgpout), fileno (pgperr),
1378 pgpinfile, keylist, flags)) == -1) {
1379 mutt_perror (_("Can't invoke PGP"));
1383 mutt_unlink (pgpinfile);
1384 unlink (pgpoutfile);
1388 if (pgp_use_gpg_agent ())
1391 fprintf (pgpin, "%s\n", PgpPass);
1394 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1397 mutt_unlink (pgpinfile);
1406 empty = (fgetc (pgpout) == EOF);
1411 while (fgets (buff, sizeof (buff), pgperr)) {
1413 fputs (buff, stdout);
1419 mutt_any_key_to_continue (NULL);
1423 pgp_void_passphrase (); /* just in case */
1424 unlink (pgpoutfile);
1428 b = mutt_new_body ();
1430 b->encoding = ENC7BIT;
1433 b->subtype = str_dup ("plain");
1435 mutt_set_parameter ("x-action",
1436 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1438 mutt_set_parameter ("charset", send_charset, &b->parameter);
1440 b->filename = str_dup (pgpoutfile);
1443 /* The following is intended to give a clue to some completely brain-dead
1444 * "mail environments" which are typically used by large corporations.
1447 b->d_filename = str_dup ("msg.pgp");
1452 b->disposition = DISPINLINE;
1458 if (!(flags & ENCRYPT))
1459 b->encoding = a->encoding;
1464 int pgp_send_menu (HEADER * msg, int *redraw)
1467 char input_signas[SHORT_STRING];
1469 char prompt[LONG_STRING];
1471 if (!(WithCrypto & APPLICATION_PGP))
1472 return msg->security;
1474 /* If autoinline and no crypto options set, then set inline. */
1475 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1476 && (msg->security & (SIGN | ENCRYPT))))
1477 msg->security |= INLINE;
1479 snprintf (prompt, sizeof (prompt),
1480 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1481 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1483 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1484 case 1: /* (e)ncrypt */
1485 msg->security |= ENCRYPT;
1486 msg->security &= ~SIGN;
1489 case 2: /* (s)ign */
1490 msg->security |= SIGN;
1491 msg->security &= ~ENCRYPT;
1494 case 3: /* sign (a)s */
1495 unset_option (OPTPGPCHECKTRUST);
1498 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1500 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1501 str_replace (&PgpSignAs, input_signas);
1504 msg->security |= SIGN;
1506 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1510 msg->security &= ~SIGN;
1514 *redraw = REDRAW_FULL;
1517 case 4: /* (b)oth */
1518 msg->security |= (ENCRYPT | SIGN);
1521 case 5: /* (i)nline */
1522 if ((msg->security & (ENCRYPT | SIGN)))
1523 msg->security ^= INLINE;
1525 msg->security &= ~INLINE;
1528 case 6: /* (f)orget it */
1529 case 7: /* (c)lear */
1534 if (msg->security) {
1535 if (!(msg->security & (ENCRYPT | SIGN)))
1538 msg->security |= APPLICATION_PGP;
1541 return (msg->security);
1545 #endif /* CRYPT_BACKEND_CLASSIC_PGP */