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;
240 long bytes, last_pos, offset;
241 char buf[HUGE_STRING];
242 char outfile[_POSIX_PATH_MAX];
243 char tmpfname[_POSIX_PATH_MAX];
244 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
248 short maybe_goodsig = 1;
249 short have_any_sigs = 0;
251 char body_charset[STRING];
253 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
255 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
257 fseek (s->fpin, m->offset, 0);
258 last_pos = m->offset;
260 for (bytes = m->length; bytes > 0;) {
261 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
264 offset = ftell (s->fpin);
265 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
268 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
270 start_pos = last_pos;
272 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
274 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
278 else if (!option (OPTDONTHANDLEPGPKEYS) &&
279 str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
284 /* XXX - we may wish to recode here */
286 state_puts (s->prefix, s);
291 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
293 /* Copy PGP material to temporary file */
294 mutt_mktemp (tmpfname);
295 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
296 mutt_perror (tmpfname);
301 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
302 offset = ftell (s->fpin);
303 bytes -= (offset - last_pos); /* don't rely on str_len(buf) */
309 && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
311 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
312 || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
317 /* leave tmpfp open in case we still need it - but flush it! */
321 /* Invoke PGP if needed */
322 if (!clearsign || (s->flags & M_VERIFY)) {
323 mutt_mktemp (outfile);
324 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
325 mutt_perror (tmpfname);
329 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
330 fileno (pgpout), -1, tmpfname,
332 safe_fclose (&pgpout);
337 ("[-- Error: unable to create PGP subprocess! --]\n"),
340 else { /* PGP started successfully */
343 if (!pgp_valid_passphrase ())
344 pgp_void_passphrase ();
345 if (pgp_use_gpg_agent ())
347 fprintf (pgpin, "%s\n", PgpPass);
350 safe_fclose (&pgpin);
352 if (s->flags & M_DISPLAY) {
353 crypt_current_time (s, "PGP");
354 rc = pgp_copy_checksig (pgperr, s->fpout);
357 safe_fclose (&pgperr);
358 rv = mutt_wait_filter (thepid);
360 if (s->flags & M_DISPLAY) {
366 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
367 * Sig _is_ correct if
368 * gpg_good_sign="" && pgp_decode_command returned 0
373 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
377 /* treat empty result as sign of failure */
383 if (!clearsign && (!pgpout || c == EOF)) {
384 mutt_error _("Could not decrypt PGP message");
386 pgp_void_passphrase ();
393 * Now, copy cleartext to the screen. NOTE - we expect that PGP
394 * outputs utf-8 cleartext. This may not always be true, but it
395 * seems to be a reasonable guess.
398 if (s->flags & M_DISPLAY) {
400 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
401 else if (pgp_keyblock)
402 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
404 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
410 pgp_copy_clearsigned (tmpfp, s, body_charset);
417 state_set_prefix (s);
418 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
419 while ((c = fgetconv (fc)) != EOF)
420 state_prefix_putc (c, s);
421 fgetconv_close (&fc);
424 if (s->flags & M_DISPLAY) {
425 state_putc ('\n', s);
427 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
428 mutt_message _("PGP message successfully decrypted.");
430 else if (pgp_keyblock)
431 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
433 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
438 /* XXX - we may wish to recode here */
440 state_puts (s->prefix, s);
448 m->goodsig = (maybe_goodsig && have_any_sigs);
451 safe_fclose (&tmpfp);
452 mutt_unlink (tmpfname);
455 safe_fclose (&pgpout);
456 mutt_unlink (outfile);
459 if (needpass == -1) {
461 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
469 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
472 char tempfile[_POSIX_PATH_MAX];
473 char buf[HUGE_STRING];
480 if (b->type != TYPETEXT)
483 if (tagged_only && !b->tagged)
486 mutt_mktemp (tempfile);
487 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
492 if ((tfp = fopen (tempfile, "r")) == NULL) {
497 while (fgets (buf, sizeof (buf), tfp)) {
498 if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
499 if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
501 else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
503 else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
510 if (!enc && !sgn && !key)
513 /* fix the content type */
515 mutt_set_parameter ("format", "fixed", &b->parameter);
517 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
519 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
521 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
526 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
531 for (; b; b = b->next) {
532 if (is_multipart (b))
533 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
534 else if (b->type == TYPETEXT) {
535 if ((r = mutt_is_application_pgp (b)))
538 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
549 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
551 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
552 FILE *fp, *pgpout, *pgperr;
557 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
559 if (!(fp = safe_fopen (sigfile, "w"))) {
560 mutt_perror (sigfile);
564 fseek (s->fpin, sigbdy->offset, 0);
565 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
568 mutt_mktemp (pgperrfile);
569 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
570 mutt_perror (pgperrfile);
575 crypt_current_time (s, "PGP");
577 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
578 -1, -1, fileno (pgperr),
579 tempfile, sigfile)) != -1) {
580 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
584 safe_fclose (&pgpout);
588 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
591 if ((rv = mutt_wait_filter (thepid)))
594 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
597 safe_fclose (&pgperr);
599 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
601 mutt_unlink (sigfile);
602 mutt_unlink (pgperrfile);
604 debug_print (1, ("returning %d.\n", badsig));
610 /* Extract pgp public keys from messages or attachments */
612 void pgp_extract_keys_from_messages (HEADER * h)
615 char tempfname[_POSIX_PATH_MAX];
619 mutt_parse_mime_message (Context, h);
620 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
624 mutt_mktemp (tempfname);
625 if (!(fpout = safe_fopen (tempfname, "w"))) {
626 mutt_perror (tempfname);
630 set_option (OPTDONTHANDLEPGPKEYS);
633 for (i = 0; i < Context->vcount; i++) {
634 if (Context->hdrs[Context->v2r[i]]->tagged) {
635 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
636 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
637 && !pgp_valid_passphrase ()) {
641 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
642 M_CM_DECODE | M_CM_CHARCONV, 0);
647 mutt_parse_mime_message (Context, h);
648 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
652 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
657 pgp_invoke_import (tempfname);
658 mutt_any_key_to_continue (NULL);
662 mutt_unlink (tempfname);
663 unset_option (OPTDONTHANDLEPGPKEYS);
667 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
671 char tempfname[_POSIX_PATH_MAX];
673 mutt_mktemp (tempfname);
674 if (!(tempfp = safe_fopen (tempfname, "w"))) {
675 mutt_perror (tempfname);
679 memset (&s, 0, sizeof (STATE));
684 mutt_body_handler (top, &s);
688 pgp_invoke_import (tempfname);
689 mutt_any_key_to_continue (NULL);
691 mutt_unlink (tempfname);
694 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
697 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
703 set_option (OPTDONTHANDLEPGPKEYS);
705 for (; top; top = top->next) {
706 if (!tag || top->tagged)
707 pgp_extract_keys_from_attachment (fp, top);
713 unset_option (OPTDONTHANDLEPGPKEYS);
716 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
718 char buf[LONG_STRING];
719 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
723 char pgperrfile[_POSIX_PATH_MAX];
724 char pgptmpfile[_POSIX_PATH_MAX];
728 mutt_mktemp (pgperrfile);
729 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
730 mutt_perror (pgperrfile);
735 mutt_mktemp (pgptmpfile);
736 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
737 mutt_perror (pgptmpfile);
742 /* Position the stream at the beginning of the body, and send the data to
743 * the temporary file.
746 fseek (s->fpin, a->offset, 0);
747 mutt_copy_bytes (s->fpin, pgptmp, a->length);
750 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
751 fileno (pgperr), pgptmpfile)) == -1) {
754 if (s->flags & M_DISPLAY)
756 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
761 /* send the PGP passphrase to the subprocess. Never do this if the
762 agent is active, because this might lead to a passphrase send as
764 if (!pgp_use_gpg_agent ())
765 fputs (PgpPass, pgpin);
769 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
770 * read_mime_header has a hard time parsing the message.
772 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
774 if (len > 1 && buf[len - 2] == '\r')
775 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
780 rv = mutt_wait_filter (thepid);
781 mutt_unlink (pgptmpfile);
783 if (s->flags & M_DISPLAY) {
786 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
790 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
797 if (fgetc (fpout) == EOF) {
798 mutt_error (_("Decryption failed."));
799 pgp_void_passphrase ();
805 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
807 * Need to set the length of this body part.
809 fstat (fileno (fpout), &info);
810 tattach->length = info.st_size - tattach->offset;
812 /* See if we need to recurse on this MIME part. */
814 mutt_parse_part (fpout, tattach);
820 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
822 char tempfile[_POSIX_PATH_MAX];
826 if (!mutt_is_multipart_encrypted (b))
829 if (!b->parts || !b->parts->next)
834 memset (&s, 0, sizeof (s));
836 mutt_mktemp (tempfile);
837 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
838 mutt_perror (tempfile);
843 *cur = pgp_decrypt_part (b, &s, *fpout, p);
853 int pgp_encrypted_handler (BODY * a, STATE * s)
855 char tempfile[_POSIX_PATH_MAX];
862 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
863 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
864 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
865 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
866 if (s->flags & M_DISPLAY)
867 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
873 * Move forward to the application/pgp-encrypted body.
877 mutt_mktemp (tempfile);
878 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
879 if (s->flags & M_DISPLAY)
881 ("[-- Error: could not create temporary file! --]\n"),
886 if (s->flags & M_DISPLAY)
887 crypt_current_time (s, "PGP");
889 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
890 if (s->flags & M_DISPLAY)
892 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
897 rc = mutt_body_handler (tattach, s);
901 * if a multipart/signed is the _only_ sub-part of a
902 * multipart/encrypted, cache signature verification
907 if (mutt_is_multipart_signed (tattach) && !tattach->next)
908 p->goodsig |= tattach->goodsig;
910 if (s->flags & M_DISPLAY) {
911 state_puts ("\n", s);
912 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
915 mutt_free_body (&tattach);
916 /* clear 'Invoking...' message, since there's no error */
917 mutt_message _("PGP message successfully decrypted.");
919 mutt_error _("Could not decrypt PGP message");
920 pgp_void_passphrase ();
925 mutt_unlink (tempfile);
930 /* ----------------------------------------------------------------------------
931 * Routines for sending PGP/MIME messages.
935 BODY *pgp_sign_message (BODY * a)
938 char buffer[LONG_STRING];
939 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
940 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
945 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
947 mutt_mktemp (sigfile);
948 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
952 mutt_mktemp (signedfile);
953 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
954 mutt_perror (signedfile);
960 mutt_write_mime_header (a, sfp);
962 mutt_write_mime_body (a, sfp);
965 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
966 -1, -1, -1, signedfile)) == -1) {
967 mutt_perror (_("Can't open PGP subprocess!"));
975 if (!pgp_use_gpg_agent ())
976 fputs (PgpPass, pgpin);
981 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
982 * recommended for future releases of PGP.
984 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
985 if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
986 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
987 else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
988 fputs ("-----END PGP SIGNATURE-----\n", fp);
991 empty = 0; /* got some output, so we're ok */
994 /* check for errors from PGP */
996 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
998 fputs (buffer, stdout);
1001 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1006 unlink (signedfile);
1008 if (fclose (fp) != 0) {
1009 mutt_perror ("fclose");
1015 pgp_void_passphrase ();
1016 mutt_any_key_to_continue (NULL);
1021 return (NULL); /* fatal error while signing */
1024 t = mutt_new_body ();
1025 t->type = TYPEMULTIPART;
1026 t->subtype = str_dup ("signed");
1027 t->encoding = ENC7BIT;
1029 t->disposition = DISPINLINE;
1031 mutt_generate_boundary (&t->parameter);
1032 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1033 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1038 t->parts->next = mutt_new_body ();
1040 t->type = TYPEAPPLICATION;
1041 t->subtype = str_dup ("pgp-signature");
1042 t->filename = str_dup (sigfile);
1044 t->disposition = DISPINLINE;
1045 t->encoding = ENC7BIT;
1046 t->unlink = 1; /* ok to remove this file after sending. */
1051 static short is_numerical_keyid (const char *s)
1053 /* or should we require the "0x"? */
1054 if (strncmp (s, "0x", 2) == 0)
1056 if (str_len (s) % 8)
1059 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1065 /* This routine attempts to find the keyids of the recipients of a message.
1066 * It returns NULL if any of the keys can not be found.
1068 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1070 char *keyID, *keylist = NULL, *t;
1071 size_t keylist_size = 0;
1072 size_t keylist_used = 0;
1073 ADDRESS *tmp = NULL, *addr = NULL;
1074 ADDRESS **last = &tmp;
1077 pgp_key_t k_info = NULL, key = NULL;
1079 const char *fqdn = mutt_fqdn (1);
1081 for (i = 0; i < 3; i++) {
1096 *last = rfc822_cpy_adr (p);
1098 last = &((*last)->next);
1102 rfc822_qualify (tmp, fqdn);
1104 tmp = mutt_remove_duplicates (tmp);
1106 for (p = tmp; p; p = p->next) {
1107 char buf[LONG_STRING];
1112 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1115 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1117 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1118 if (is_numerical_keyid (keyID)) {
1119 if (strncmp (keyID, "0x", 2) == 0)
1121 goto bypass_selection; /* you don't see this. */
1124 /* check for e-mail address */
1125 if ((t = strchr (keyID, '@')) &&
1126 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1128 rfc822_qualify (addr, fqdn);
1132 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1135 mem_free (&keylist);
1136 rfc822_free_address (&tmp);
1137 rfc822_free_address (&addr);
1143 pgp_invoke_getkeys (q);
1147 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1148 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1150 if ((key = pgp_ask_for_key (buf, q->mailbox,
1151 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1152 mem_free (&keylist);
1153 rfc822_free_address (&tmp);
1154 rfc822_free_address (&addr);
1161 keyID = pgp_keyid (key);
1164 keylist_size += str_len (keyID) + 4;
1165 mem_realloc (&keylist, keylist_size);
1166 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1168 keylist_used = str_len (keylist);
1170 pgp_free_key (&key);
1171 rfc822_free_address (&addr);
1174 rfc822_free_address (&tmp);
1178 /* Warning: "a" is no longer freed in this routine, you need
1179 * to free it later. This is necessary for $fcc_attach. */
1181 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1183 char buf[LONG_STRING];
1184 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1185 char pgpinfile[_POSIX_PATH_MAX];
1186 FILE *pgpin, *pgperr, *fpout, *fptmp;
1192 mutt_mktemp (tempfile);
1193 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1194 mutt_perror (tempfile);
1198 mutt_mktemp (pgperrfile);
1199 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1200 mutt_perror (pgperrfile);
1205 unlink (pgperrfile);
1207 mutt_mktemp (pgpinfile);
1208 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1209 mutt_perror (pgpinfile);
1217 convert_to_7bit (a);
1219 mutt_write_mime_header (a, fptmp);
1220 fputc ('\n', fptmp);
1221 mutt_write_mime_body (a, fptmp);
1224 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1225 fileno (fpout), fileno (pgperr),
1226 pgpinfile, keylist, sign)) == -1) {
1233 if (!pgp_use_gpg_agent ())
1234 fputs (PgpPass, pgpin);
1235 fputc ('\n', pgpin);
1239 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1247 empty = (fgetc (fpout) == EOF);
1252 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1254 fputs (buf, stdout);
1258 /* pause if there is any error output from PGP */
1260 mutt_any_key_to_continue (NULL);
1263 /* fatal error while trying to encrypt message */
1265 pgp_void_passphrase (); /* just in case */
1270 t = mutt_new_body ();
1271 t->type = TYPEMULTIPART;
1272 t->subtype = str_dup ("encrypted");
1273 t->encoding = ENC7BIT;
1275 t->disposition = DISPINLINE;
1277 mutt_generate_boundary (&t->parameter);
1278 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1280 t->parts = mutt_new_body ();
1281 t->parts->type = TYPEAPPLICATION;
1282 t->parts->subtype = str_dup ("pgp-encrypted");
1283 t->parts->encoding = ENC7BIT;
1285 t->parts->next = mutt_new_body ();
1286 t->parts->next->type = TYPEAPPLICATION;
1287 t->parts->next->subtype = str_dup ("octet-stream");
1288 t->parts->next->encoding = ENC7BIT;
1289 t->parts->next->filename = str_dup (tempfile);
1290 t->parts->next->use_disp = 1;
1291 t->parts->next->disposition = DISPINLINE;
1292 t->parts->next->unlink = 1; /* delete after sending the message */
1293 t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1298 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1302 char pgpoutfile[_POSIX_PATH_MAX];
1303 char pgperrfile[_POSIX_PATH_MAX];
1304 char pgpinfile[_POSIX_PATH_MAX];
1306 char body_charset[STRING];
1308 const char *send_charset;
1310 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1320 if (a->type != TYPETEXT)
1322 if (ascii_strcasecmp (a->subtype, "plain"))
1325 if ((fp = fopen (a->filename, "r")) == NULL) {
1326 mutt_perror (a->filename);
1330 mutt_mktemp (pgpinfile);
1331 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1332 mutt_perror (pgpinfile);
1337 /* The following code is really correct: If noconv is set,
1338 * a's charset parameter contains the on-disk character set, and
1339 * we have to convert from that to utf-8. If noconv is not set,
1340 * we have to convert from $charset to utf-8.
1343 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1345 from_charset = body_charset;
1347 from_charset = Charset;
1349 if (!mutt_is_us_ascii (body_charset)) {
1353 if (flags & ENCRYPT)
1354 send_charset = "us-ascii";
1356 send_charset = "utf-8";
1358 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1359 while ((c = fgetconv (fc)) != EOF)
1362 fgetconv_close (&fc);
1365 send_charset = "us-ascii";
1366 mutt_copy_stream (fp, pgpin);
1371 mutt_mktemp (pgpoutfile);
1372 mutt_mktemp (pgperrfile);
1373 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1374 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1375 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1379 unlink (pgpoutfile);
1384 unlink (pgperrfile);
1386 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1387 -1, fileno (pgpout), fileno (pgperr),
1388 pgpinfile, keylist, flags)) == -1) {
1389 mutt_perror (_("Can't invoke PGP"));
1393 mutt_unlink (pgpinfile);
1394 unlink (pgpoutfile);
1398 if (pgp_use_gpg_agent ())
1401 fprintf (pgpin, "%s\n", PgpPass);
1404 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1407 mutt_unlink (pgpinfile);
1416 empty = (fgetc (pgpout) == EOF);
1421 while (fgets (buff, sizeof (buff), pgperr)) {
1423 fputs (buff, stdout);
1429 mutt_any_key_to_continue (NULL);
1433 pgp_void_passphrase (); /* just in case */
1434 unlink (pgpoutfile);
1438 b = mutt_new_body ();
1440 b->encoding = ENC7BIT;
1443 b->subtype = str_dup ("plain");
1445 mutt_set_parameter ("x-action",
1446 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1448 mutt_set_parameter ("charset", send_charset, &b->parameter);
1450 b->filename = str_dup (pgpoutfile);
1453 /* The following is intended to give a clue to some completely brain-dead
1454 * "mail environments" which are typically used by large corporations.
1457 b->d_filename = str_dup ("msg.pgp");
1462 b->disposition = DISPINLINE;
1468 if (!(flags & ENCRYPT))
1469 b->encoding = a->encoding;
1474 int pgp_send_menu (HEADER * msg, int *redraw)
1477 char input_signas[SHORT_STRING];
1479 char prompt[LONG_STRING];
1481 if (!(WithCrypto & APPLICATION_PGP))
1482 return msg->security;
1484 /* If autoinline and no crypto options set, then set inline. */
1485 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1486 && (msg->security & (SIGN | ENCRYPT))))
1487 msg->security |= INLINE;
1489 snprintf (prompt, sizeof (prompt),
1490 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1491 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1493 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1494 case 1: /* (e)ncrypt */
1495 msg->security |= ENCRYPT;
1496 msg->security &= ~SIGN;
1499 case 2: /* (s)ign */
1500 msg->security |= SIGN;
1501 msg->security &= ~ENCRYPT;
1504 case 3: /* sign (a)s */
1505 unset_option (OPTPGPCHECKTRUST);
1508 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1510 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1511 str_replace (&PgpSignAs, input_signas);
1514 msg->security |= SIGN;
1516 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1520 msg->security &= ~SIGN;
1524 *redraw = REDRAW_FULL;
1527 case 4: /* (b)oth */
1528 msg->security |= (ENCRYPT | SIGN);
1531 case 5: /* (i)nline */
1532 if ((msg->security & (ENCRYPT | SIGN)))
1533 msg->security ^= INLINE;
1535 msg->security &= ~INLINE;
1538 case 6: /* (f)orget it */
1539 case 7: /* (c)lear */
1544 if (msg->security) {
1545 if (!(msg->security & (ENCRYPT | SIGN)))
1548 msg->security |= APPLICATION_PGP;
1551 return (msg->security);
1555 #endif /* CRYPT_BACKEND_CLASSIC_PGP */