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
25 #include "mutt_curses.h"
33 #include "lib/debug.h"
47 #ifdef HAVE_SYS_TIME_H
48 # include <sys/time.h>
51 #ifdef HAVE_SYS_RESOURCE_H
52 # include <sys/resource.h>
55 #ifdef CRYPT_BACKEND_CLASSIC_PGP
57 #include "mutt_crypt.h"
58 #include "mutt_menu.h"
61 char PgpPass[LONG_STRING];
62 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
64 void pgp_void_passphrase (void)
66 memset (PgpPass, 0, sizeof (PgpPass));
70 int pgp_valid_passphrase (void)
72 time_t now = time (NULL);
74 if (pgp_use_gpg_agent ()) {
76 return 1; /* handled by gpg-agent */
80 /* Use cached copy. */
83 pgp_void_passphrase ();
86 (_("Enter PGP passphrase:"), PgpPass, sizeof (PgpPass)) == 0) {
87 PgpExptime = time (NULL) + PgpTimeout;
96 void pgp_forget_passphrase (void)
98 pgp_void_passphrase ();
99 mutt_message _("PGP passphrase forgotten.");
102 int pgp_use_gpg_agent (void)
104 return option (OPTUSEGPGAGENT) && getenv ("GPG_TTY")
105 && getenv ("GPG_AGENT_INFO");
108 char *pgp_keyid (pgp_key_t k)
110 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
113 return _pgp_keyid (k);
116 char *_pgp_keyid (pgp_key_t k)
118 if (option (OPTPGPLONGIDS))
121 return (k->keyid + 8);
124 /* ----------------------------------------------------------------------------
125 * Routines for handing PGP input.
130 /* Copy PGP output messages and look for signs of a good signature */
132 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
136 if (PgpGoodSign.pattern) {
141 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
142 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
143 debug_print (2, ("\"%s\" matches regexp.\n", line));
147 debug_print (2, ("\"%s\" doesn't match regexp.\n", line));
149 if (strncmp (line, "[GNUPG:] ", 9) == 0)
157 debug_print (2, ("No pattern.\n"));
158 mutt_copy_stream (fpin, fpout);
166 * Copy a clearsigned message, and strip the signature and PGP's
169 * XXX - charset handling: We assume that it is safe to do
170 * character set decoding first, dash decoding second here, while
171 * we do it the other way around in the main handler.
173 * (Note that we aren't worse than Outlook &c in this, and also
174 * note that we can successfully handle anything produced by any
175 * existing versions of mutt.)
178 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
180 char buf[HUGE_STRING];
181 short complete, armor_header;
187 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
189 for (complete = 1, armor_header = 1;
190 fgetconvs (buf, sizeof (buf), fc) != NULL;
191 complete = strchr (buf, '\n') != NULL) {
198 if (mutt_strcmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
202 char *p = str_skip_initws (buf);
210 state_puts (s->prefix, s);
212 if (buf[0] == '-' && buf[1] == ' ')
213 state_puts (buf + 2, s);
218 fgetconv_close (&fc);
222 /* Support for the Application/PGP Content Type. */
224 void pgp_application_pgp_handler (BODY * m, STATE * s)
226 int needpass = -1, pgp_keyblock = 0;
227 int clearsign = 0, rv, rc;
229 long bytes, last_pos, offset;
230 char buf[HUGE_STRING];
231 char outfile[_POSIX_PATH_MAX];
232 char tmpfname[_POSIX_PATH_MAX];
233 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
237 short maybe_goodsig = 1;
238 short have_any_sigs = 0;
240 char body_charset[STRING];
242 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
244 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
246 fseek (s->fpin, m->offset, 0);
247 last_pos = m->offset;
249 for (bytes = m->length; bytes > 0;) {
250 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
253 offset = ftell (s->fpin);
254 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
257 if (safe_strncmp ("-----BEGIN PGP ", buf, 15) == 0) {
259 start_pos = last_pos;
261 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
263 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
267 else if (!option (OPTDONTHANDLEPGPKEYS) &&
268 mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
273 /* XXX - we may wish to recode here */
275 state_puts (s->prefix, s);
280 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
282 /* Copy PGP material to temporary file */
283 mutt_mktemp (tmpfname);
284 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
285 mutt_perror (tmpfname);
290 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
291 offset = ftell (s->fpin);
292 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
298 && mutt_strcmp ("-----END PGP MESSAGE-----\n", buf) == 0)
300 && (mutt_strcmp ("-----END PGP SIGNATURE-----\n", buf) == 0
301 || mutt_strcmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
306 /* leave tmpfp open in case we still need it - but flush it! */
310 /* Invoke PGP if needed */
311 if (!clearsign || (s->flags & M_VERIFY)) {
312 mutt_mktemp (outfile);
313 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
314 mutt_perror (tmpfname);
318 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
319 fileno (pgpout), -1, tmpfname,
321 safe_fclose (&pgpout);
326 ("[-- Error: unable to create PGP subprocess! --]\n"),
329 else { /* PGP started successfully */
332 if (!pgp_valid_passphrase ())
333 pgp_void_passphrase ();
334 if (pgp_use_gpg_agent ())
336 fprintf (pgpin, "%s\n", PgpPass);
339 safe_fclose (&pgpin);
341 if (s->flags & M_DISPLAY) {
342 crypt_current_time (s, "PGP");
343 rc = pgp_copy_checksig (pgperr, s->fpout);
346 safe_fclose (&pgperr);
347 rv = mutt_wait_filter (thepid);
349 if (s->flags & M_DISPLAY) {
354 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
355 * Sig _is_ correct if
356 * gpg_good_sign="" && pgp_decode_command returned 0
361 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
366 /* treat empty result as sign of failure */
367 if (! ftell(pgpout)) {
368 mutt_error _("Could not decrypt PGP message");
373 * Now, copy cleartext to the screen. NOTE - we expect that PGP
374 * outputs utf-8 cleartext. This may not always be true, but it
375 * seems to be a reasonable guess.
378 if (s->flags & M_DISPLAY) {
380 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
381 else if (pgp_keyblock)
382 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
384 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
390 pgp_copy_clearsigned (tmpfp, s, body_charset);
397 state_set_prefix (s);
398 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
399 while ((c = fgetconv (fc)) != EOF)
400 state_prefix_putc (c, s);
401 fgetconv_close (&fc);
404 if (s->flags & M_DISPLAY) {
405 state_putc ('\n', s);
407 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
408 mutt_message _("PGP message successfully decrypted.");
410 else if (pgp_keyblock)
411 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
413 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
418 /* XXX - we may wish to recode here */
420 state_puts (s->prefix, s);
426 m->goodsig = (maybe_goodsig && have_any_sigs);
429 safe_fclose (&tmpfp);
430 mutt_unlink (tmpfname);
433 safe_fclose (&pgpout);
434 mutt_unlink (outfile);
437 if (needpass == -1) {
439 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
445 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
448 char tempfile[_POSIX_PATH_MAX];
449 char buf[HUGE_STRING];
456 if (b->type != TYPETEXT)
459 if (tagged_only && !b->tagged)
462 mutt_mktemp (tempfile);
463 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
468 if ((tfp = fopen (tempfile, "r")) == NULL) {
473 while (fgets (buf, sizeof (buf), tfp)) {
474 if (safe_strncmp ("-----BEGIN PGP ", buf, 15) == 0) {
475 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
477 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
479 else if (mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
486 if (!enc && !sgn && !key)
489 /* fix the content type */
491 mutt_set_parameter ("format", "fixed", &b->parameter);
493 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
495 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
497 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
502 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
507 for (; b; b = b->next) {
508 if (is_multipart (b))
509 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
510 else if (b->type == TYPETEXT) {
511 if ((r = mutt_is_application_pgp (b)))
514 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
525 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
527 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
528 FILE *fp, *pgpout, *pgperr;
533 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
535 if (!(fp = safe_fopen (sigfile, "w"))) {
536 mutt_perror (sigfile);
540 fseek (s->fpin, sigbdy->offset, 0);
541 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
544 mutt_mktemp (pgperrfile);
545 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
546 mutt_perror (pgperrfile);
551 crypt_current_time (s, "PGP");
553 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
554 -1, -1, fileno (pgperr),
555 tempfile, sigfile)) != -1) {
556 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
560 safe_fclose (&pgpout);
564 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
567 if ((rv = mutt_wait_filter (thepid)))
570 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
573 safe_fclose (&pgperr);
575 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
577 mutt_unlink (sigfile);
578 mutt_unlink (pgperrfile);
580 debug_print (1, ("returning %d.\n", badsig));
586 /* Extract pgp public keys from messages or attachments */
588 void pgp_extract_keys_from_messages (HEADER * h)
591 char tempfname[_POSIX_PATH_MAX];
595 mutt_parse_mime_message (Context, h);
596 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
600 mutt_mktemp (tempfname);
601 if (!(fpout = safe_fopen (tempfname, "w"))) {
602 mutt_perror (tempfname);
606 set_option (OPTDONTHANDLEPGPKEYS);
609 for (i = 0; i < Context->vcount; i++) {
610 if (Context->hdrs[Context->v2r[i]]->tagged) {
611 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
612 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
613 && !pgp_valid_passphrase ()) {
617 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
618 M_CM_DECODE | M_CM_CHARCONV, 0);
623 mutt_parse_mime_message (Context, h);
624 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
628 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
633 pgp_invoke_import (tempfname);
634 mutt_any_key_to_continue (NULL);
638 mutt_unlink (tempfname);
639 unset_option (OPTDONTHANDLEPGPKEYS);
643 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
647 char tempfname[_POSIX_PATH_MAX];
649 mutt_mktemp (tempfname);
650 if (!(tempfp = safe_fopen (tempfname, "w"))) {
651 mutt_perror (tempfname);
655 memset (&s, 0, sizeof (STATE));
660 mutt_body_handler (top, &s);
664 pgp_invoke_import (tempfname);
665 mutt_any_key_to_continue (NULL);
667 mutt_unlink (tempfname);
670 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
673 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
679 set_option (OPTDONTHANDLEPGPKEYS);
681 for (; top; top = top->next) {
682 if (!tag || top->tagged)
683 pgp_extract_keys_from_attachment (fp, top);
689 unset_option (OPTDONTHANDLEPGPKEYS);
692 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
694 char buf[LONG_STRING];
695 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
699 char pgperrfile[_POSIX_PATH_MAX];
700 char pgptmpfile[_POSIX_PATH_MAX];
704 mutt_mktemp (pgperrfile);
705 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
706 mutt_perror (pgperrfile);
711 mutt_mktemp (pgptmpfile);
712 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
713 mutt_perror (pgptmpfile);
718 /* Position the stream at the beginning of the body, and send the data to
719 * the temporary file.
722 fseek (s->fpin, a->offset, 0);
723 mutt_copy_bytes (s->fpin, pgptmp, a->length);
726 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
727 fileno (pgperr), pgptmpfile)) == -1) {
730 if (s->flags & M_DISPLAY)
732 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
737 /* send the PGP passphrase to the subprocess. Never do this if the
738 agent is active, because this might lead to a passphrase send as
740 if (!pgp_use_gpg_agent ())
741 fputs (PgpPass, pgpin);
745 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
746 * read_mime_header has a hard time parsing the message.
748 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
749 len = mutt_strlen (buf);
750 if (len > 1 && buf[len - 2] == '\r')
751 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
756 rv = mutt_wait_filter (thepid);
757 mutt_unlink (pgptmpfile);
759 if (s->flags & M_DISPLAY) {
762 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
766 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
773 if (fgetc (fpout) == EOF)
778 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
780 * Need to set the length of this body part.
782 fstat (fileno (fpout), &info);
783 tattach->length = info.st_size - tattach->offset;
785 /* See if we need to recurse on this MIME part. */
787 mutt_parse_part (fpout, tattach);
793 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
795 char tempfile[_POSIX_PATH_MAX];
799 if (!mutt_is_multipart_encrypted (b))
802 if (!b->parts || !b->parts->next)
807 memset (&s, 0, sizeof (s));
809 mutt_mktemp (tempfile);
810 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
811 mutt_perror (tempfile);
816 *cur = pgp_decrypt_part (b, &s, *fpout, p);
826 void pgp_encrypted_handler (BODY * a, STATE * s)
828 char tempfile[_POSIX_PATH_MAX];
834 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
835 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
836 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
837 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
838 if (s->flags & M_DISPLAY)
839 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
845 * Move forward to the application/pgp-encrypted body.
849 mutt_mktemp (tempfile);
850 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
851 if (s->flags & M_DISPLAY)
853 ("[-- Error: could not create temporary file! --]\n"),
858 if (s->flags & M_DISPLAY)
859 crypt_current_time (s, "PGP");
861 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
862 if (s->flags & M_DISPLAY)
864 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
869 mutt_body_handler (tattach, s);
873 * if a multipart/signed is the _only_ sub-part of a
874 * multipart/encrypted, cache signature verification
879 if (mutt_is_multipart_signed (tattach) && !tattach->next)
880 p->goodsig |= tattach->goodsig;
882 if (s->flags & M_DISPLAY) {
883 state_puts ("\n", s);
884 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
887 mutt_free_body (&tattach);
888 /* clear 'Invoking...' message, since there's no error */
889 mutt_message _("PGP message successfully decrypted.");
891 mutt_error _("Could not decrypt PGP message");
894 mutt_unlink (tempfile);
897 /* ----------------------------------------------------------------------------
898 * Routines for sending PGP/MIME messages.
902 BODY *pgp_sign_message (BODY * a)
905 char buffer[LONG_STRING];
906 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
907 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
912 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
914 mutt_mktemp (sigfile);
915 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
919 mutt_mktemp (signedfile);
920 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
921 mutt_perror (signedfile);
927 mutt_write_mime_header (a, sfp);
929 mutt_write_mime_body (a, sfp);
932 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
933 -1, -1, -1, signedfile)) == -1) {
934 mutt_perror (_("Can't open PGP subprocess!"));
942 if (!pgp_use_gpg_agent ())
943 fputs (PgpPass, pgpin);
948 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
949 * recommended for future releases of PGP.
951 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
952 if (mutt_strcmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
953 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
954 else if (mutt_strcmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
955 fputs ("-----END PGP SIGNATURE-----\n", fp);
958 empty = 0; /* got some output, so we're ok */
961 /* check for errors from PGP */
963 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
965 fputs (buffer, stdout);
968 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
975 if (fclose (fp) != 0) {
976 mutt_perror ("fclose");
982 pgp_void_passphrase ();
983 mutt_any_key_to_continue (NULL);
988 return (NULL); /* fatal error while signing */
991 t = mutt_new_body ();
992 t->type = TYPEMULTIPART;
993 t->subtype = safe_strdup ("signed");
994 t->encoding = ENC7BIT;
996 t->disposition = DISPINLINE;
998 mutt_generate_boundary (&t->parameter);
999 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1000 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1005 t->parts->next = mutt_new_body ();
1007 t->type = TYPEAPPLICATION;
1008 t->subtype = safe_strdup ("pgp-signature");
1009 t->filename = safe_strdup (sigfile);
1011 t->disposition = DISPINLINE;
1012 t->encoding = ENC7BIT;
1013 t->unlink = 1; /* ok to remove this file after sending. */
1018 static short is_numerical_keyid (const char *s)
1020 /* or should we require the "0x"? */
1021 if (strncmp (s, "0x", 2) == 0)
1023 if (mutt_strlen (s) % 8)
1026 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1032 /* This routine attempts to find the keyids of the recipients of a message.
1033 * It returns NULL if any of the keys can not be found.
1035 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1037 char *keyID, *keylist = NULL, *t;
1038 size_t keylist_size = 0;
1039 size_t keylist_used = 0;
1040 ADDRESS *tmp = NULL, *addr = NULL;
1041 ADDRESS **last = &tmp;
1044 pgp_key_t k_info = NULL, key = NULL;
1046 const char *fqdn = mutt_fqdn (1);
1048 for (i = 0; i < 3; i++) {
1063 *last = rfc822_cpy_adr (p);
1065 last = &((*last)->next);
1069 rfc822_qualify (tmp, fqdn);
1071 tmp = mutt_remove_duplicates (tmp);
1073 for (p = tmp; p; p = p->next) {
1074 char buf[LONG_STRING];
1079 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1082 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1084 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1085 if (is_numerical_keyid (keyID)) {
1086 if (strncmp (keyID, "0x", 2) == 0)
1088 goto bypass_selection; /* you don't see this. */
1091 /* check for e-mail address */
1092 if ((t = strchr (keyID, '@')) &&
1093 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1095 rfc822_qualify (addr, fqdn);
1099 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1103 rfc822_free_address (&tmp);
1104 rfc822_free_address (&addr);
1110 pgp_invoke_getkeys (q);
1114 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1115 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1117 if ((key = pgp_ask_for_key (buf, q->mailbox,
1118 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1120 rfc822_free_address (&tmp);
1121 rfc822_free_address (&addr);
1128 keyID = pgp_keyid (key);
1131 keylist_size += mutt_strlen (keyID) + 4;
1132 safe_realloc (&keylist, keylist_size);
1133 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1135 keylist_used = mutt_strlen (keylist);
1137 pgp_free_key (&key);
1138 rfc822_free_address (&addr);
1141 rfc822_free_address (&tmp);
1145 /* Warning: "a" is no longer freed in this routine, you need
1146 * to free it later. This is necessary for $fcc_attach. */
1148 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1150 char buf[LONG_STRING];
1151 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1152 char pgpinfile[_POSIX_PATH_MAX];
1153 FILE *pgpin, *pgperr, *fpout, *fptmp;
1159 mutt_mktemp (tempfile);
1160 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1161 mutt_perror (tempfile);
1165 mutt_mktemp (pgperrfile);
1166 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1167 mutt_perror (pgperrfile);
1172 unlink (pgperrfile);
1174 mutt_mktemp (pgpinfile);
1175 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1176 mutt_perror (pgpinfile);
1184 convert_to_7bit (a);
1186 mutt_write_mime_header (a, fptmp);
1187 fputc ('\n', fptmp);
1188 mutt_write_mime_body (a, fptmp);
1191 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1192 fileno (fpout), fileno (pgperr),
1193 pgpinfile, keylist, sign)) == -1) {
1200 if (!pgp_use_gpg_agent ())
1201 fputs (PgpPass, pgpin);
1202 fputc ('\n', pgpin);
1206 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1214 empty = (fgetc (fpout) == EOF);
1219 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1221 fputs (buf, stdout);
1225 /* pause if there is any error output from PGP */
1227 mutt_any_key_to_continue (NULL);
1230 /* fatal error while trying to encrypt message */
1232 pgp_void_passphrase (); /* just in case */
1237 t = mutt_new_body ();
1238 t->type = TYPEMULTIPART;
1239 t->subtype = safe_strdup ("encrypted");
1240 t->encoding = ENC7BIT;
1242 t->disposition = DISPINLINE;
1244 mutt_generate_boundary (&t->parameter);
1245 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1247 t->parts = mutt_new_body ();
1248 t->parts->type = TYPEAPPLICATION;
1249 t->parts->subtype = safe_strdup ("pgp-encrypted");
1250 t->parts->encoding = ENC7BIT;
1252 t->parts->next = mutt_new_body ();
1253 t->parts->next->type = TYPEAPPLICATION;
1254 t->parts->next->subtype = safe_strdup ("octet-stream");
1255 t->parts->next->encoding = ENC7BIT;
1256 t->parts->next->filename = safe_strdup (tempfile);
1257 t->parts->next->use_disp = 1;
1258 t->parts->next->disposition = DISPINLINE;
1259 t->parts->next->unlink = 1; /* delete after sending the message */
1260 t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime can save */
1265 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1269 char pgpoutfile[_POSIX_PATH_MAX];
1270 char pgperrfile[_POSIX_PATH_MAX];
1271 char pgpinfile[_POSIX_PATH_MAX];
1273 char body_charset[STRING];
1275 const char *send_charset;
1277 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1287 if (a->type != TYPETEXT)
1289 if (ascii_strcasecmp (a->subtype, "plain"))
1292 if ((fp = fopen (a->filename, "r")) == NULL) {
1293 mutt_perror (a->filename);
1297 mutt_mktemp (pgpinfile);
1298 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1299 mutt_perror (pgpinfile);
1304 /* The following code is really correct: If noconv is set,
1305 * a's charset parameter contains the on-disk character set, and
1306 * we have to convert from that to utf-8. If noconv is not set,
1307 * we have to convert from $charset to utf-8.
1310 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1312 from_charset = body_charset;
1314 from_charset = Charset;
1316 if (!mutt_is_us_ascii (body_charset)) {
1320 if (flags & ENCRYPT)
1321 send_charset = "us-ascii";
1323 send_charset = "utf-8";
1325 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1326 while ((c = fgetconv (fc)) != EOF)
1329 fgetconv_close (&fc);
1332 send_charset = "us-ascii";
1333 mutt_copy_stream (fp, pgpin);
1338 mutt_mktemp (pgpoutfile);
1339 mutt_mktemp (pgperrfile);
1340 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1341 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1342 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1346 unlink (pgpoutfile);
1351 unlink (pgperrfile);
1353 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1354 -1, fileno (pgpout), fileno (pgperr),
1355 pgpinfile, keylist, flags)) == -1) {
1356 mutt_perror (_("Can't invoke PGP"));
1360 mutt_unlink (pgpinfile);
1361 unlink (pgpoutfile);
1365 if (pgp_use_gpg_agent ())
1368 fprintf (pgpin, "%s\n", PgpPass);
1371 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1374 mutt_unlink (pgpinfile);
1383 empty = (fgetc (pgpout) == EOF);
1388 while (fgets (buff, sizeof (buff), pgperr)) {
1390 fputs (buff, stdout);
1396 mutt_any_key_to_continue (NULL);
1400 pgp_void_passphrase (); /* just in case */
1401 unlink (pgpoutfile);
1405 b = mutt_new_body ();
1407 b->encoding = ENC7BIT;
1410 b->subtype = safe_strdup ("plain");
1412 mutt_set_parameter ("x-action",
1413 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1415 mutt_set_parameter ("charset", send_charset, &b->parameter);
1417 b->filename = safe_strdup (pgpoutfile);
1420 /* The following is intended to give a clue to some completely brain-dead
1421 * "mail environments" which are typically used by large corporations.
1424 b->d_filename = safe_strdup ("msg.pgp");
1429 b->disposition = DISPINLINE;
1435 if (!(flags & ENCRYPT))
1436 b->encoding = a->encoding;
1441 int pgp_send_menu (HEADER * msg, int *redraw)
1444 char input_signas[SHORT_STRING];
1446 char prompt[LONG_STRING];
1448 if (!(WithCrypto & APPLICATION_PGP))
1449 return msg->security;
1451 /* If autoinline and no crypto options set, then set inline. */
1452 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1453 && (msg->security & (SIGN | ENCRYPT))))
1454 msg->security |= INLINE;
1456 snprintf (prompt, sizeof (prompt),
1457 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1458 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1460 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1461 case 1: /* (e)ncrypt */
1462 msg->security |= ENCRYPT;
1463 msg->security &= ~SIGN;
1466 case 2: /* (s)ign */
1467 msg->security |= SIGN;
1468 msg->security &= ~ENCRYPT;
1471 case 3: /* sign (a)s */
1472 unset_option (OPTPGPCHECKTRUST);
1475 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1477 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1478 str_replace (&PgpSignAs, input_signas);
1481 msg->security |= SIGN;
1483 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1487 msg->security &= ~SIGN;
1491 *redraw = REDRAW_FULL;
1494 case 4: /* (b)oth */
1495 msg->security |= (ENCRYPT | SIGN);
1498 case 5: /* (i)nline */
1499 if ((msg->security & (ENCRYPT | SIGN)))
1500 msg->security ^= INLINE;
1502 msg->security &= ~INLINE;
1505 case 6: /* (f)orget it */
1506 case 7: /* (c)lear */
1511 if (msg->security) {
1512 if (!(msg->security & (ENCRYPT | SIGN)))
1515 msg->security |= APPLICATION_PGP;
1518 return (msg->security);
1522 #endif /* CRYPT_BACKEND_CLASSIC_PGP */