2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
5 * Copyright (C) 2004 g10 Code GmbH
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
13 * This file contains all of the PGP routines necessary to sign, encrypt,
14 * verify and decrypt PGP messages in either the new PGP/MIME format, or
15 * in the older Application/Pgp format. It also contains some code to
16 * cache the user's passphrase for repeat use when decrypting or signing
24 #include <lib-lib/mem.h>
30 #include "mutt_curses.h"
39 #include "lib/debug.h"
53 #ifdef HAVE_SYS_TIME_H
54 # include <sys/time.h>
57 #ifdef HAVE_SYS_RESOURCE_H
58 # include <sys/resource.h>
61 #ifdef CRYPT_BACKEND_CLASSIC_PGP
63 #include "mutt_crypt.h"
64 #include "mutt_menu.h"
67 char PgpPass[LONG_STRING];
68 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
70 void pgp_void_passphrase (void)
72 memset (PgpPass, 0, sizeof (PgpPass));
76 int pgp_valid_passphrase (void)
78 time_t now = time (NULL);
80 if (pgp_use_gpg_agent ()) {
82 return 1; /* handled by gpg-agent */
86 /* Use cached copy. */
89 pgp_void_passphrase ();
91 if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
92 sizeof (PgpPass), M_PASS) == 0) {
93 PgpExptime = time (NULL) + PgpTimeout;
102 void pgp_forget_passphrase (void)
104 pgp_void_passphrase ();
105 mutt_message _("PGP passphrase forgotten.");
108 int pgp_use_gpg_agent (void) {
111 if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
114 if ((tty = ttyname(0)))
115 setenv ("GPG_TTY", tty, 0);
120 char *pgp_keyid (pgp_key_t k)
122 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
125 return _pgp_keyid (k);
128 char *_pgp_keyid (pgp_key_t k)
130 if (option (OPTPGPLONGIDS))
133 return (k->keyid + 8);
136 /* ----------------------------------------------------------------------------
137 * Routines for handing PGP input.
142 /* Copy PGP output messages and look for signs of a good signature */
144 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
148 if (PgpGoodSign.pattern) {
153 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
154 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
155 debug_print (2, ("\"%s\" matches regexp.\n", line));
159 debug_print (2, ("\"%s\" doesn't match regexp.\n", line));
161 if (strncmp (line, "[GNUPG:] ", 9) == 0)
169 debug_print (2, ("No pattern.\n"));
170 mutt_copy_stream (fpin, fpout);
178 * Copy a clearsigned message, and strip the signature and PGP's
181 * XXX - charset handling: We assume that it is safe to do
182 * character set decoding first, dash decoding second here, while
183 * we do it the other way around in the main handler.
185 * (Note that we aren't worse than Outlook &c in this, and also
186 * note that we can successfully handle anything produced by any
187 * existing versions of mutt.)
190 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
192 char buf[HUGE_STRING];
193 short complete, armor_header;
199 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
201 for (complete = 1, armor_header = 1;
202 fgetconvs (buf, sizeof (buf), fc) != NULL;
203 complete = strchr (buf, '\n') != NULL) {
210 if (str_cmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
214 char *p = str_skip_initws (buf);
222 state_puts (s->prefix, s);
224 if (buf[0] == '-' && buf[1] == ' ')
225 state_puts (buf + 2, s);
230 fgetconv_close (&fc);
234 /* Support for the Application/PGP Content Type. */
236 int pgp_application_pgp_handler (BODY * m, STATE * s)
238 int could_not_decrypt = 0;
239 int needpass = -1, pgp_keyblock = 0;
241 int clearsign = 0, rv, rc;
244 off_t last_pos, offset;
245 char buf[HUGE_STRING];
246 char outfile[_POSIX_PATH_MAX];
247 char tmpfname[_POSIX_PATH_MAX];
248 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
252 short maybe_goodsig = 1;
253 short have_any_sigs = 0;
255 char body_charset[STRING];
257 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
259 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
261 fseeko (s->fpin, m->offset, 0);
262 last_pos = m->offset;
264 for (bytes = m->length; bytes > 0;) {
265 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
268 offset = ftello (s->fpin);
269 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
272 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
274 start_pos = last_pos;
276 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
278 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
282 else if (!option (OPTDONTHANDLEPGPKEYS) &&
283 str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
288 /* XXX - we may wish to recode here */
290 state_puts (s->prefix, s);
295 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
297 /* Copy PGP material to temporary file */
298 mutt_mktemp (tmpfname);
299 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
300 mutt_perror (tmpfname);
305 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
306 offset = ftello (s->fpin);
307 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
313 && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
315 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
316 || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
321 /* leave tmpfp open in case we still need it - but flush it! */
325 /* Invoke PGP if needed */
326 if (!clearsign || (s->flags & M_VERIFY)) {
327 mutt_mktemp (outfile);
328 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
329 mutt_perror (tmpfname);
333 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
334 fileno (pgpout), -1, tmpfname,
336 safe_fclose (&pgpout);
341 ("[-- Error: unable to create PGP subprocess! --]\n"),
344 else { /* PGP started successfully */
347 if (!pgp_valid_passphrase ())
348 pgp_void_passphrase ();
349 if (pgp_use_gpg_agent ())
351 fprintf (pgpin, "%s\n", PgpPass);
354 safe_fclose (&pgpin);
356 if (s->flags & M_DISPLAY) {
357 crypt_current_time (s, "PGP");
358 rc = pgp_copy_checksig (pgperr, s->fpout);
361 safe_fclose (&pgperr);
362 rv = mutt_wait_filter (thepid);
364 if (s->flags & M_DISPLAY) {
370 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
371 * Sig _is_ correct if
372 * gpg_good_sign="" && pgp_decode_command returned 0
377 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
381 /* treat empty result as sign of failure */
382 /* TODO: maybe on failure mutt should include the original undecoded text. */
388 if (!clearsign && (!pgpout || c == EOF)) {
389 could_not_decrypt = 1;
390 pgp_void_passphrase ();
393 if (could_not_decrypt && !(s->flags & M_DISPLAY)) {
394 mutt_error _("Could not decrypt PGP message");
402 * Now, copy cleartext to the screen. NOTE - we expect that PGP
403 * outputs utf-8 cleartext. This may not always be true, but it
404 * seems to be a reasonable guess.
407 if (s->flags & M_DISPLAY) {
409 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
410 else if (pgp_keyblock)
411 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
413 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
419 pgp_copy_clearsigned (tmpfp, s, body_charset);
426 state_set_prefix (s);
427 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
428 while ((c = fgetconv (fc)) != EOF)
429 state_prefix_putc (c, s);
430 fgetconv_close (&fc);
433 if (s->flags & M_DISPLAY) {
434 state_putc ('\n', s);
436 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
437 if (could_not_decrypt)
438 mutt_error _("Could not decrypt PGP message.");
440 mutt_message _("PGP message successfully decrypted.");
442 else if (pgp_keyblock)
443 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
445 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
450 /* XXX - we may wish to recode here */
452 state_puts (s->prefix, s);
460 m->goodsig = (maybe_goodsig && have_any_sigs);
463 safe_fclose (&tmpfp);
464 mutt_unlink (tmpfname);
467 safe_fclose (&pgpout);
468 mutt_unlink (outfile);
471 if (needpass == -1) {
473 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
481 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
484 char tempfile[_POSIX_PATH_MAX];
485 char buf[HUGE_STRING];
492 if (b->type != TYPETEXT)
495 if (tagged_only && !b->tagged)
498 mutt_mktemp (tempfile);
499 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
504 if ((tfp = fopen (tempfile, "r")) == NULL) {
509 while (fgets (buf, sizeof (buf), tfp)) {
510 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
511 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
513 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
515 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
522 if (!enc && !sgn && !key)
525 /* fix the content type */
527 mutt_set_parameter ("format", "fixed", &b->parameter);
529 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
531 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
533 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
538 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
543 for (; b; b = b->next) {
544 if (is_multipart (b))
545 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
546 else if (b->type == TYPETEXT) {
547 if ((r = mutt_is_application_pgp (b)))
550 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
561 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
563 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
564 FILE *fp, *pgpout, *pgperr;
569 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
571 if (!(fp = safe_fopen (sigfile, "w"))) {
572 mutt_perror (sigfile);
576 fseeko (s->fpin, sigbdy->offset, 0);
577 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
580 mutt_mktemp (pgperrfile);
581 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
582 mutt_perror (pgperrfile);
587 crypt_current_time (s, "PGP");
589 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
590 -1, -1, fileno (pgperr),
591 tempfile, sigfile)) != -1) {
592 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
596 safe_fclose (&pgpout);
600 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
603 if ((rv = mutt_wait_filter (thepid)))
606 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
609 safe_fclose (&pgperr);
611 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
613 mutt_unlink (sigfile);
614 mutt_unlink (pgperrfile);
616 debug_print (1, ("returning %d.\n", badsig));
622 /* Extract pgp public keys from messages or attachments */
624 void pgp_extract_keys_from_messages (HEADER * h)
627 char tempfname[_POSIX_PATH_MAX];
631 mutt_parse_mime_message (Context, h);
632 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
636 mutt_mktemp (tempfname);
637 if (!(fpout = safe_fopen (tempfname, "w"))) {
638 mutt_perror (tempfname);
642 set_option (OPTDONTHANDLEPGPKEYS);
645 for (i = 0; i < Context->vcount; i++) {
646 if (Context->hdrs[Context->v2r[i]]->tagged) {
647 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
648 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
649 && !pgp_valid_passphrase ()) {
653 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
654 M_CM_DECODE | M_CM_CHARCONV, 0);
659 mutt_parse_mime_message (Context, h);
660 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
664 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
669 pgp_invoke_import (tempfname);
670 mutt_any_key_to_continue (NULL);
674 mutt_unlink (tempfname);
675 unset_option (OPTDONTHANDLEPGPKEYS);
679 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
683 char tempfname[_POSIX_PATH_MAX];
685 mutt_mktemp (tempfname);
686 if (!(tempfp = safe_fopen (tempfname, "w"))) {
687 mutt_perror (tempfname);
691 memset (&s, 0, sizeof (STATE));
696 mutt_body_handler (top, &s);
700 pgp_invoke_import (tempfname);
701 mutt_any_key_to_continue (NULL);
703 mutt_unlink (tempfname);
706 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
709 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
715 set_option (OPTDONTHANDLEPGPKEYS);
717 for (; top; top = top->next) {
718 if (!tag || top->tagged)
719 pgp_extract_keys_from_attachment (fp, top);
725 unset_option (OPTDONTHANDLEPGPKEYS);
728 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
730 char buf[LONG_STRING];
731 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
735 char pgperrfile[_POSIX_PATH_MAX];
736 char pgptmpfile[_POSIX_PATH_MAX];
740 mutt_mktemp (pgperrfile);
741 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
742 mutt_perror (pgperrfile);
747 mutt_mktemp (pgptmpfile);
748 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
749 mutt_perror (pgptmpfile);
754 /* Position the stream at the beginning of the body, and send the data to
755 * the temporary file.
758 fseeko (s->fpin, a->offset, 0);
759 mutt_copy_bytes (s->fpin, pgptmp, a->length);
762 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
763 fileno (pgperr), pgptmpfile)) == -1) {
766 if (s->flags & M_DISPLAY)
768 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
773 /* send the PGP passphrase to the subprocess. Never do this if the
774 agent is active, because this might lead to a passphrase send as
776 if (!pgp_use_gpg_agent ())
777 fputs (PgpPass, pgpin);
781 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
782 * read_mime_header has a hard time parsing the message.
784 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
786 if (len > 1 && buf[len - 2] == '\r')
787 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
792 rv = mutt_wait_filter (thepid);
793 mutt_unlink (pgptmpfile);
795 if (s->flags & M_DISPLAY) {
798 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
802 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
809 if (fgetc (fpout) == EOF) {
810 mutt_error (_("Decryption failed."));
811 pgp_void_passphrase ();
817 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
819 * Need to set the length of this body part.
821 fstat (fileno (fpout), &info);
822 tattach->length = info.st_size - tattach->offset;
824 /* See if we need to recurse on this MIME part. */
826 mutt_parse_part (fpout, tattach);
832 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
834 char tempfile[_POSIX_PATH_MAX];
838 if (!mutt_is_multipart_encrypted (b))
841 if (!b->parts || !b->parts->next)
846 memset (&s, 0, sizeof (s));
848 mutt_mktemp (tempfile);
849 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
850 mutt_perror (tempfile);
855 *cur = pgp_decrypt_part (b, &s, *fpout, p);
865 int pgp_encrypted_handler (BODY * a, STATE * s)
867 char tempfile[_POSIX_PATH_MAX];
874 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
875 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
876 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
877 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
878 if (s->flags & M_DISPLAY)
879 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
885 * Move forward to the application/pgp-encrypted body.
889 mutt_mktemp (tempfile);
890 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
891 if (s->flags & M_DISPLAY)
893 ("[-- Error: could not create temporary file! --]\n"),
898 if (s->flags & M_DISPLAY)
899 crypt_current_time (s, "PGP");
901 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
902 if (s->flags & M_DISPLAY)
904 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
909 rc = mutt_body_handler (tattach, s);
913 * if a multipart/signed is the _only_ sub-part of a
914 * multipart/encrypted, cache signature verification
919 if (mutt_is_multipart_signed (tattach) && !tattach->next)
920 p->goodsig |= tattach->goodsig;
922 if (s->flags & M_DISPLAY) {
923 state_puts ("\n", s);
924 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
927 mutt_free_body (&tattach);
928 /* clear 'Invoking...' message, since there's no error */
929 mutt_message _("PGP message successfully decrypted.");
931 mutt_error _("Could not decrypt PGP message");
932 pgp_void_passphrase ();
937 mutt_unlink (tempfile);
942 /* ----------------------------------------------------------------------------
943 * Routines for sending PGP/MIME messages.
947 BODY *pgp_sign_message (BODY * a)
950 char buffer[LONG_STRING];
951 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
952 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
957 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
959 mutt_mktemp (sigfile);
960 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
964 mutt_mktemp (signedfile);
965 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
966 mutt_perror (signedfile);
972 mutt_write_mime_header (a, sfp);
974 mutt_write_mime_body (a, sfp);
977 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
978 -1, -1, -1, signedfile)) == -1) {
979 mutt_perror (_("Can't open PGP subprocess!"));
987 if (!pgp_use_gpg_agent ())
988 fputs (PgpPass, pgpin);
993 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
994 * recommended for future releases of PGP.
996 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
997 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
998 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
999 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
1000 fputs ("-----END PGP SIGNATURE-----\n", fp);
1003 empty = 0; /* got some output, so we're ok */
1006 /* check for errors from PGP */
1008 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
1010 fputs (buffer, stdout);
1013 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1018 unlink (signedfile);
1020 if (fclose (fp) != 0) {
1021 mutt_perror ("fclose");
1027 pgp_void_passphrase ();
1028 mutt_any_key_to_continue (NULL);
1033 return (NULL); /* fatal error while signing */
1036 t = mutt_new_body ();
1037 t->type = TYPEMULTIPART;
1038 t->subtype = str_dup ("signed");
1039 t->encoding = ENC7BIT;
1041 t->disposition = DISPINLINE;
1043 mutt_generate_boundary (&t->parameter);
1044 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1045 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1050 t->parts->next = mutt_new_body ();
1052 t->type = TYPEAPPLICATION;
1053 t->subtype = str_dup ("pgp-signature");
1054 t->filename = str_dup (sigfile);
1056 t->disposition = DISPINLINE;
1057 t->encoding = ENC7BIT;
1058 t->unlink = 1; /* ok to remove this file after sending. */
1063 static short is_numerical_keyid (const char *s)
1065 /* or should we require the "0x"? */
1066 if (strncmp (s, "0x", 2) == 0)
1068 if (str_len (s) % 8)
1071 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1077 /* This routine attempts to find the keyids of the recipients of a message.
1078 * It returns NULL if any of the keys can not be found.
1080 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1082 char *keyID, *keylist = NULL, *t;
1083 size_t keylist_size = 0;
1084 size_t keylist_used = 0;
1085 ADDRESS *tmp = NULL, *addr = NULL;
1086 ADDRESS **last = &tmp;
1089 pgp_key_t k_info = NULL, key = NULL;
1091 const char *fqdn = mutt_fqdn (1);
1093 for (i = 0; i < 3; i++) {
1108 *last = rfc822_cpy_adr (p);
1110 last = &((*last)->next);
1114 rfc822_qualify (tmp, fqdn);
1116 tmp = mutt_remove_duplicates (tmp);
1118 for (p = tmp; p; p = p->next) {
1119 char buf[LONG_STRING];
1124 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1127 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1129 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1130 if (is_numerical_keyid (keyID)) {
1131 if (strncmp (keyID, "0x", 2) == 0)
1133 goto bypass_selection; /* you don't see this. */
1136 /* check for e-mail address */
1137 if ((t = strchr (keyID, '@')) &&
1138 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1140 rfc822_qualify (addr, fqdn);
1144 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1148 rfc822_free_address (&tmp);
1149 rfc822_free_address (&addr);
1155 pgp_invoke_getkeys (q);
1159 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1160 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1162 if ((key = pgp_ask_for_key (buf, q->mailbox,
1163 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1165 rfc822_free_address (&tmp);
1166 rfc822_free_address (&addr);
1173 keyID = pgp_keyid (key);
1176 keylist_size += str_len (keyID) + 4;
1177 mem_realloc (&keylist, keylist_size);
1178 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1180 keylist_used = str_len (keylist);
1182 pgp_free_key (&key);
1183 rfc822_free_address (&addr);
1186 rfc822_free_address (&tmp);
1190 /* Warning: "a" is no longer freed in this routine, you need
1191 * to free it later. This is necessary for $fcc_attach. */
1193 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1195 char buf[LONG_STRING];
1196 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1197 char pgpinfile[_POSIX_PATH_MAX];
1198 FILE *pgpin, *pgperr, *fpout, *fptmp;
1204 mutt_mktemp (tempfile);
1205 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1206 mutt_perror (tempfile);
1210 mutt_mktemp (pgperrfile);
1211 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1212 mutt_perror (pgperrfile);
1217 unlink (pgperrfile);
1219 mutt_mktemp (pgpinfile);
1220 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1221 mutt_perror (pgpinfile);
1229 convert_to_7bit (a);
1231 mutt_write_mime_header (a, fptmp);
1232 fputc ('\n', fptmp);
1233 mutt_write_mime_body (a, fptmp);
1236 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1237 fileno (fpout), fileno (pgperr),
1238 pgpinfile, keylist, sign)) == -1) {
1245 if (!pgp_use_gpg_agent ())
1246 fputs (PgpPass, pgpin);
1247 fputc ('\n', pgpin);
1251 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1259 empty = (fgetc (fpout) == EOF);
1264 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1266 fputs (buf, stdout);
1270 /* pause if there is any error output from PGP */
1272 mutt_any_key_to_continue (NULL);
1275 /* fatal error while trying to encrypt message */
1277 pgp_void_passphrase (); /* just in case */
1282 t = mutt_new_body ();
1283 t->type = TYPEMULTIPART;
1284 t->subtype = str_dup ("encrypted");
1285 t->encoding = ENC7BIT;
1287 t->disposition = DISPINLINE;
1289 mutt_generate_boundary (&t->parameter);
1290 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1292 t->parts = mutt_new_body ();
1293 t->parts->type = TYPEAPPLICATION;
1294 t->parts->subtype = str_dup ("pgp-encrypted");
1295 t->parts->encoding = ENC7BIT;
1297 t->parts->next = mutt_new_body ();
1298 t->parts->next->type = TYPEAPPLICATION;
1299 t->parts->next->subtype = str_dup ("octet-stream");
1300 t->parts->next->encoding = ENC7BIT;
1301 t->parts->next->filename = str_dup (tempfile);
1302 t->parts->next->use_disp = 1;
1303 t->parts->next->disposition = DISPINLINE;
1304 t->parts->next->unlink = 1; /* delete after sending the message */
1305 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1310 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1314 char pgpoutfile[_POSIX_PATH_MAX];
1315 char pgperrfile[_POSIX_PATH_MAX];
1316 char pgpinfile[_POSIX_PATH_MAX];
1318 char body_charset[STRING];
1320 const char *send_charset;
1322 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1332 if (a->type != TYPETEXT)
1334 if (ascii_strcasecmp (a->subtype, "plain"))
1337 if ((fp = fopen (a->filename, "r")) == NULL) {
1338 mutt_perror (a->filename);
1342 mutt_mktemp (pgpinfile);
1343 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1344 mutt_perror (pgpinfile);
1349 /* The following code is really correct: If noconv is set,
1350 * a's charset parameter contains the on-disk character set, and
1351 * we have to convert from that to utf-8. If noconv is not set,
1352 * we have to convert from $charset to utf-8.
1355 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1357 from_charset = body_charset;
1359 from_charset = Charset;
1361 if (!mutt_is_us_ascii (body_charset)) {
1365 if (flags & ENCRYPT)
1366 send_charset = "us-ascii";
1368 send_charset = "utf-8";
1370 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1371 while ((c = fgetconv (fc)) != EOF)
1374 fgetconv_close (&fc);
1377 send_charset = "us-ascii";
1378 mutt_copy_stream (fp, pgpin);
1383 mutt_mktemp (pgpoutfile);
1384 mutt_mktemp (pgperrfile);
1385 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1386 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1387 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1391 unlink (pgpoutfile);
1396 unlink (pgperrfile);
1398 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1399 -1, fileno (pgpout), fileno (pgperr),
1400 pgpinfile, keylist, flags)) == -1) {
1401 mutt_perror (_("Can't invoke PGP"));
1405 mutt_unlink (pgpinfile);
1406 unlink (pgpoutfile);
1410 if (pgp_use_gpg_agent ())
1413 fprintf (pgpin, "%s\n", PgpPass);
1416 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1419 mutt_unlink (pgpinfile);
1428 empty = (fgetc (pgpout) == EOF);
1433 while (fgets (buff, sizeof (buff), pgperr)) {
1435 fputs (buff, stdout);
1441 mutt_any_key_to_continue (NULL);
1445 pgp_void_passphrase (); /* just in case */
1446 unlink (pgpoutfile);
1450 b = mutt_new_body ();
1452 b->encoding = ENC7BIT;
1455 b->subtype = str_dup ("plain");
1457 mutt_set_parameter ("x-action",
1458 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1460 mutt_set_parameter ("charset", send_charset, &b->parameter);
1462 b->filename = str_dup (pgpoutfile);
1465 /* The following is intended to give a clue to some completely brain-dead
1466 * "mail environments" which are typically used by large corporations.
1469 b->d_filename = str_dup ("msg.pgp");
1474 b->disposition = DISPINLINE;
1480 if (!(flags & ENCRYPT))
1481 b->encoding = a->encoding;
1486 int pgp_send_menu (HEADER * msg, int *redraw)
1489 char input_signas[SHORT_STRING];
1491 char prompt[LONG_STRING];
1493 if (!(WithCrypto & APPLICATION_PGP))
1494 return msg->security;
1496 /* If autoinline and no crypto options set, then set inline. */
1497 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1498 && (msg->security & (SIGN | ENCRYPT))))
1499 msg->security |= INLINE;
1501 snprintf (prompt, sizeof (prompt),
1502 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1503 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1505 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1506 case 1: /* (e)ncrypt */
1507 msg->security |= ENCRYPT;
1508 msg->security &= ~SIGN;
1511 case 2: /* (s)ign */
1512 msg->security |= SIGN;
1513 msg->security &= ~ENCRYPT;
1516 case 3: /* sign (a)s */
1517 unset_option (OPTPGPCHECKTRUST);
1520 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1522 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1523 str_replace (&PgpSignAs, input_signas);
1526 msg->security |= SIGN;
1528 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1532 msg->security &= ~SIGN;
1536 *redraw = REDRAW_FULL;
1539 case 4: /* (b)oth */
1540 msg->security |= (ENCRYPT | SIGN);
1543 case 5: /* (i)nline */
1544 if ((msg->security & (ENCRYPT | SIGN)))
1545 msg->security ^= INLINE;
1547 msg->security &= ~INLINE;
1550 case 6: /* (f)orget it */
1551 case 7: /* (c)lear */
1556 if (msg->security) {
1557 if (!(msg->security & (ENCRYPT | SIGN)))
1560 msg->security |= APPLICATION_PGP;
1563 return (msg->security);
1567 #endif /* CRYPT_BACKEND_CLASSIC_PGP */