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>
25 #include <lib-lib/ascii.h>
26 #include <lib-lib/str.h>
27 #include <lib-lib/macros.h>
28 #include <lib-lib/file.h>
30 #include <lib-mime/mime.h>
32 #include <lib-ui/curses.h>
33 #include <lib-ui/enter.h>
34 #include <lib-ui/menu.h>
35 #include <lib-lib/debug.h>
56 #ifdef HAVE_SYS_TIME_H
57 # include <sys/time.h>
60 #ifdef HAVE_SYS_RESOURCE_H
61 # include <sys/resource.h>
67 char PgpPass[LONG_STRING];
68 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
70 void pgp_void_passphrase (void)
72 p_clear(PgpPass, countof(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 (m_strcmp(buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
214 char *p = vskipspaces(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 m_strlen(buf) */
272 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
274 start_pos = last_pos;
276 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
278 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0) {
282 else if (!option (OPTDONTHANDLEPGPKEYS) &&
283 m_strcmp("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 m_strlen(buf) */
313 && m_strcmp("-----END PGP MESSAGE-----\n", buf) == 0)
315 && (m_strcmp("-----END PGP SIGNATURE-----\n", buf) == 0
316 || m_strcmp("-----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);
425 state_set_prefix (s);
426 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
427 while ((c = fgetconv (fc)) != EOF)
428 state_prefix_putc (c, s);
429 fgetconv_close (&fc);
432 if (s->flags & M_DISPLAY) {
433 state_putc ('\n', s);
435 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
436 if (could_not_decrypt)
437 mutt_error _("Could not decrypt PGP message.");
439 mutt_message _("PGP message successfully decrypted.");
441 else if (pgp_keyblock)
442 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
444 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
449 /* XXX - we may wish to recode here */
451 state_puts (s->prefix, s);
459 m->goodsig = (maybe_goodsig && have_any_sigs);
462 safe_fclose (&tmpfp);
463 mutt_unlink (tmpfname);
466 safe_fclose (&pgpout);
467 mutt_unlink (outfile);
470 if (needpass == -1) {
472 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
480 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
483 char tempfile[_POSIX_PATH_MAX];
484 char buf[HUGE_STRING];
491 if (b->type != TYPETEXT)
494 if (tagged_only && !b->tagged)
497 mutt_mktemp (tempfile);
498 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
503 if ((tfp = fopen (tempfile, "r")) == NULL) {
508 while (fgets (buf, sizeof (buf), tfp)) {
509 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
510 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
512 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0)
514 else if (m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
521 if (!enc && !sgn && !key)
524 /* fix the content type */
526 mutt_set_parameter ("format", "fixed", &b->parameter);
528 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
530 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
532 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
537 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
542 for (; b; b = b->next) {
543 if (is_multipart (b))
544 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
545 else if (b->type == TYPETEXT) {
546 if ((r = mutt_is_application_pgp (b)))
549 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
560 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
562 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
563 FILE *fp, *pgpout, *pgperr;
568 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
570 if (!(fp = safe_fopen (sigfile, "w"))) {
571 mutt_perror (sigfile);
575 fseeko (s->fpin, sigbdy->offset, 0);
576 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
579 mutt_mktemp (pgperrfile);
580 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
581 mutt_perror (pgperrfile);
586 crypt_current_time (s, "PGP");
588 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
589 -1, -1, fileno (pgperr),
590 tempfile, sigfile)) != -1) {
591 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
595 safe_fclose (&pgpout);
599 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
602 if ((rv = mutt_wait_filter (thepid)))
605 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
608 safe_fclose (&pgperr);
610 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
612 mutt_unlink (sigfile);
613 mutt_unlink (pgperrfile);
615 debug_print (1, ("returning %d.\n", badsig));
621 /* Extract pgp public keys from messages or attachments */
623 void pgp_extract_keys_from_messages (HEADER * h)
626 char tempfname[_POSIX_PATH_MAX];
630 mutt_parse_mime_message (Context, h);
631 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
635 mutt_mktemp (tempfname);
636 if (!(fpout = safe_fopen (tempfname, "w"))) {
637 mutt_perror (tempfname);
641 set_option (OPTDONTHANDLEPGPKEYS);
644 for (i = 0; i < Context->vcount; i++) {
645 if (Context->hdrs[Context->v2r[i]]->tagged) {
646 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
647 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
648 && !pgp_valid_passphrase ()) {
652 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
653 M_CM_DECODE | M_CM_CHARCONV, 0);
658 mutt_parse_mime_message (Context, h);
659 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
663 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
668 pgp_invoke_import (tempfname);
669 mutt_any_key_to_continue (NULL);
673 mutt_unlink (tempfname);
674 unset_option (OPTDONTHANDLEPGPKEYS);
678 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
682 char tempfname[_POSIX_PATH_MAX];
684 mutt_mktemp (tempfname);
685 if (!(tempfp = safe_fopen (tempfname, "w"))) {
686 mutt_perror (tempfname);
695 mutt_body_handler (top, &s);
699 pgp_invoke_import (tempfname);
700 mutt_any_key_to_continue (NULL);
702 mutt_unlink (tempfname);
705 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
708 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
714 set_option (OPTDONTHANDLEPGPKEYS);
716 for (; top; top = top->next) {
717 if (!tag || top->tagged)
718 pgp_extract_keys_from_attachment (fp, top);
724 unset_option (OPTDONTHANDLEPGPKEYS);
727 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
729 char buf[LONG_STRING];
730 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
734 char pgperrfile[_POSIX_PATH_MAX];
735 char pgptmpfile[_POSIX_PATH_MAX];
739 mutt_mktemp (pgperrfile);
740 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
741 mutt_perror (pgperrfile);
746 mutt_mktemp (pgptmpfile);
747 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
748 mutt_perror (pgptmpfile);
753 /* Position the stream at the beginning of the body, and send the data to
754 * the temporary file.
757 fseeko (s->fpin, a->offset, 0);
758 mutt_copy_bytes (s->fpin, pgptmp, a->length);
761 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
762 fileno (pgperr), pgptmpfile)) == -1) {
765 if (s->flags & M_DISPLAY)
767 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
772 /* send the PGP passphrase to the subprocess. Never do this if the
773 agent is active, because this might lead to a passphrase send as
775 if (!pgp_use_gpg_agent ())
776 fputs (PgpPass, pgpin);
780 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
781 * read_mime_header has a hard time parsing the message.
783 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
785 if (len > 1 && buf[len - 2] == '\r')
786 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
791 rv = mutt_wait_filter (thepid);
792 mutt_unlink (pgptmpfile);
794 if (s->flags & M_DISPLAY) {
797 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
801 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
808 if (fgetc (fpout) == EOF) {
809 mutt_error (_("Decryption failed."));
810 pgp_void_passphrase ();
816 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
818 * Need to set the length of this body part.
820 fstat (fileno (fpout), &info);
821 tattach->length = info.st_size - tattach->offset;
823 /* See if we need to recurse on this MIME part. */
825 mutt_parse_part (fpout, tattach);
831 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
833 char tempfile[_POSIX_PATH_MAX];
837 if (!mutt_is_multipart_encrypted (b))
840 if (!b->parts || !b->parts->next)
847 mutt_mktemp (tempfile);
848 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
849 mutt_perror (tempfile);
854 *cur = pgp_decrypt_part (b, &s, *fpout, p);
864 int pgp_encrypted_handler (BODY * a, STATE * s)
866 char tempfile[_POSIX_PATH_MAX];
873 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
874 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
875 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
876 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
877 if (s->flags & M_DISPLAY)
878 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
884 * Move forward to the application/pgp-encrypted body.
888 mutt_mktemp (tempfile);
889 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
890 if (s->flags & M_DISPLAY)
892 ("[-- Error: could not create temporary file! --]\n"),
897 if (s->flags & M_DISPLAY)
898 crypt_current_time (s, "PGP");
900 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
901 if (s->flags & M_DISPLAY)
903 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
908 rc = mutt_body_handler (tattach, s);
912 * if a multipart/signed is the _only_ sub-part of a
913 * multipart/encrypted, cache signature verification
918 if (mutt_is_multipart_signed (tattach) && !tattach->next)
919 p->goodsig |= tattach->goodsig;
921 if (s->flags & M_DISPLAY) {
922 state_puts ("\n", s);
923 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
926 mutt_free_body (&tattach);
927 /* clear 'Invoking...' message, since there's no error */
928 mutt_message _("PGP message successfully decrypted.");
930 mutt_error _("Could not decrypt PGP message");
931 pgp_void_passphrase ();
936 mutt_unlink (tempfile);
941 /* ----------------------------------------------------------------------------
942 * Routines for sending PGP/MIME messages.
946 BODY *pgp_sign_message (BODY * a)
949 char buffer[LONG_STRING];
950 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
951 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
956 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
958 mutt_mktemp (sigfile);
959 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
963 mutt_mktemp (signedfile);
964 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
965 mutt_perror (signedfile);
971 mutt_write_mime_header (a, sfp);
973 mutt_write_mime_body (a, sfp);
976 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
977 -1, -1, -1, signedfile)) == -1) {
978 mutt_perror (_("Can't open PGP subprocess!"));
986 if (!pgp_use_gpg_agent ())
987 fputs (PgpPass, pgpin);
992 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
993 * recommended for future releases of PGP.
995 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
996 if (m_strcmp("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
997 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
998 else if (m_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
999 fputs ("-----END PGP SIGNATURE-----\n", fp);
1002 empty = 0; /* got some output, so we're ok */
1005 /* check for errors from PGP */
1007 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
1009 fputs (buffer, stdout);
1012 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1017 unlink (signedfile);
1019 if (fclose (fp) != 0) {
1020 mutt_perror ("fclose");
1026 pgp_void_passphrase ();
1027 mutt_any_key_to_continue (NULL);
1032 return (NULL); /* fatal error while signing */
1035 t = mutt_new_body ();
1036 t->type = TYPEMULTIPART;
1037 t->subtype = m_strdup("signed");
1038 t->encoding = ENC7BIT;
1040 t->disposition = DISPINLINE;
1042 mutt_generate_boundary (&t->parameter);
1043 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1044 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1049 t->parts->next = mutt_new_body ();
1051 t->type = TYPEAPPLICATION;
1052 t->subtype = m_strdup("pgp-signature");
1053 t->filename = m_strdup(sigfile);
1055 t->disposition = DISPINLINE;
1056 t->encoding = ENC7BIT;
1057 t->unlink = 1; /* ok to remove this file after sending. */
1062 static short is_numerical_keyid (const char *s)
1064 /* or should we require the "0x"? */
1065 if (strncmp (s, "0x", 2) == 0)
1067 if (m_strlen(s) % 8)
1070 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1076 /* This routine attempts to find the keyids of the recipients of a message.
1077 * It returns NULL if any of the keys can not be found.
1079 char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
1081 char *keyID, *keylist = NULL, *t;
1082 size_t keylist_size = 0;
1083 size_t keylist_used = 0;
1084 address_t *tmp = NULL, *addr = NULL;
1085 address_t **last = &tmp;
1088 pgp_key_t k_info = NULL, key = NULL;
1090 const char *fqdn = mutt_fqdn (1);
1092 for (i = 0; i < 3; i++) {
1107 *last = address_list_dup (p);
1109 last = &((*last)->next);
1113 rfc822_qualify (tmp, fqdn);
1115 tmp = mutt_remove_duplicates (tmp);
1117 for (p = tmp; p; p = p->next) {
1118 char buf[LONG_STRING];
1123 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1126 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1128 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1129 if (is_numerical_keyid (keyID)) {
1130 if (strncmp (keyID, "0x", 2) == 0)
1132 goto bypass_selection; /* you don't see this. */
1135 /* check for e-mail address */
1136 if ((t = strchr (keyID, '@')) &&
1137 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1139 rfc822_qualify (addr, fqdn);
1143 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1147 address_list_wipe(&tmp);
1148 address_list_wipe(&addr);
1154 pgp_invoke_getkeys (q);
1158 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1159 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1161 if ((key = pgp_ask_for_key (buf, q->mailbox,
1162 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1164 address_list_wipe(&tmp);
1165 address_list_wipe(&addr);
1172 keyID = pgp_keyid (key);
1175 keylist_size += m_strlen(keyID) + 4;
1176 p_realloc(&keylist, keylist_size);
1177 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1179 keylist_used = m_strlen(keylist);
1181 pgp_free_key (&key);
1182 address_list_wipe(&addr);
1185 address_list_wipe(&tmp);
1189 /* Warning: "a" is no longer freed in this routine, you need
1190 * to free it later. This is necessary for $fcc_attach. */
1192 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1194 char buf[LONG_STRING];
1195 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1196 char pgpinfile[_POSIX_PATH_MAX];
1197 FILE *pgpin, *pgperr, *fpout, *fptmp;
1203 mutt_mktemp (tempfile);
1204 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1205 mutt_perror (tempfile);
1209 mutt_mktemp (pgperrfile);
1210 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1211 mutt_perror (pgperrfile);
1216 unlink (pgperrfile);
1218 mutt_mktemp (pgpinfile);
1219 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1220 mutt_perror (pgpinfile);
1228 convert_to_7bit (a);
1230 mutt_write_mime_header (a, fptmp);
1231 fputc ('\n', fptmp);
1232 mutt_write_mime_body (a, fptmp);
1235 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1236 fileno (fpout), fileno (pgperr),
1237 pgpinfile, keylist, sign)) == -1) {
1244 if (!pgp_use_gpg_agent ())
1245 fputs (PgpPass, pgpin);
1246 fputc ('\n', pgpin);
1250 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1258 empty = (fgetc (fpout) == EOF);
1263 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1265 fputs (buf, stdout);
1269 /* pause if there is any error output from PGP */
1271 mutt_any_key_to_continue (NULL);
1274 /* fatal error while trying to encrypt message */
1276 pgp_void_passphrase (); /* just in case */
1281 t = mutt_new_body ();
1282 t->type = TYPEMULTIPART;
1283 t->subtype = m_strdup("encrypted");
1284 t->encoding = ENC7BIT;
1286 t->disposition = DISPINLINE;
1288 mutt_generate_boundary (&t->parameter);
1289 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1291 t->parts = mutt_new_body ();
1292 t->parts->type = TYPEAPPLICATION;
1293 t->parts->subtype = m_strdup("pgp-encrypted");
1294 t->parts->encoding = ENC7BIT;
1296 t->parts->next = mutt_new_body ();
1297 t->parts->next->type = TYPEAPPLICATION;
1298 t->parts->next->subtype = m_strdup("octet-stream");
1299 t->parts->next->encoding = ENC7BIT;
1300 t->parts->next->filename = m_strdup(tempfile);
1301 t->parts->next->use_disp = 1;
1302 t->parts->next->disposition = DISPINLINE;
1303 t->parts->next->unlink = 1; /* delete after sending the message */
1304 t->parts->next->d_filename = m_strdup("msg.asc"); /* non pgp/mime can save */
1309 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1313 char pgpoutfile[_POSIX_PATH_MAX];
1314 char pgperrfile[_POSIX_PATH_MAX];
1315 char pgpinfile[_POSIX_PATH_MAX];
1317 char body_charset[STRING];
1319 const char *send_charset;
1321 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1331 if (a->type != TYPETEXT)
1333 if (ascii_strcasecmp (a->subtype, "plain"))
1336 if ((fp = fopen (a->filename, "r")) == NULL) {
1337 mutt_perror (a->filename);
1341 mutt_mktemp (pgpinfile);
1342 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1343 mutt_perror (pgpinfile);
1348 /* The following code is really correct: If noconv is set,
1349 * a's charset parameter contains the on-disk character set, and
1350 * we have to convert from that to utf-8. If noconv is not set,
1351 * we have to convert from $charset to utf-8.
1354 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1356 from_charset = body_charset;
1358 from_charset = Charset;
1360 if (!mutt_is_us_ascii (body_charset)) {
1364 if (flags & ENCRYPT)
1365 send_charset = "us-ascii";
1367 send_charset = "utf-8";
1369 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1370 while ((c = fgetconv (fc)) != EOF)
1373 fgetconv_close (&fc);
1376 send_charset = "us-ascii";
1377 mutt_copy_stream (fp, pgpin);
1382 mutt_mktemp (pgpoutfile);
1383 mutt_mktemp (pgperrfile);
1384 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1385 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1386 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1390 unlink (pgpoutfile);
1395 unlink (pgperrfile);
1397 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1398 -1, fileno (pgpout), fileno (pgperr),
1399 pgpinfile, keylist, flags)) == -1) {
1400 mutt_perror (_("Can't invoke PGP"));
1404 mutt_unlink (pgpinfile);
1405 unlink (pgpoutfile);
1409 if (pgp_use_gpg_agent ())
1412 fprintf (pgpin, "%s\n", PgpPass);
1415 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1418 mutt_unlink (pgpinfile);
1427 empty = (fgetc (pgpout) == EOF);
1432 while (fgets (buff, sizeof (buff), pgperr)) {
1434 fputs (buff, stdout);
1440 mutt_any_key_to_continue (NULL);
1444 pgp_void_passphrase (); /* just in case */
1445 unlink (pgpoutfile);
1449 b = mutt_new_body ();
1451 b->encoding = ENC7BIT;
1454 b->subtype = m_strdup("plain");
1456 mutt_set_parameter ("x-action",
1457 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1459 mutt_set_parameter ("charset", send_charset, &b->parameter);
1461 b->filename = m_strdup(pgpoutfile);
1464 /* The following is intended to give a clue to some completely brain-dead
1465 * "mail environments" which are typically used by large corporations.
1468 b->d_filename = m_strdup("msg.pgp");
1473 b->disposition = DISPINLINE;
1479 if (!(flags & ENCRYPT))
1480 b->encoding = a->encoding;
1485 int pgp_send_menu (HEADER * msg, int *redraw)
1488 char input_signas[SHORT_STRING];
1490 char prompt[LONG_STRING];
1492 /* If autoinline and no crypto options set, then set inline. */
1493 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1494 && (msg->security & (SIGN | ENCRYPT))))
1495 msg->security |= INLINE;
1497 snprintf (prompt, sizeof (prompt),
1498 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1499 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1501 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1502 case 1: /* (e)ncrypt */
1503 msg->security |= ENCRYPT;
1504 msg->security &= ~SIGN;
1507 case 2: /* (s)ign */
1508 msg->security |= SIGN;
1509 msg->security &= ~ENCRYPT;
1512 case 3: /* sign (a)s */
1513 unset_option (OPTPGPCHECKTRUST);
1516 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1518 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1519 m_strreplace(&PgpSignAs, input_signas);
1522 msg->security |= SIGN;
1524 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1528 msg->security &= ~SIGN;
1532 *redraw = REDRAW_FULL;
1535 case 4: /* (b)oth */
1536 msg->security |= (ENCRYPT | SIGN);
1539 case 5: /* (i)nline */
1540 if ((msg->security & (ENCRYPT | SIGN)))
1541 msg->security ^= INLINE;
1543 msg->security &= ~INLINE;
1546 case 6: /* (f)orget it */
1547 case 7: /* (c)lear */
1552 if (msg->security) {
1553 if (!(msg->security & (ENCRYPT | SIGN)))
1556 msg->security |= APPLICATION_PGP;
1559 return (msg->security);