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 */
368 if (! ftell(pgpout)) {
369 mutt_error _("Could not decrypt PGP message");
374 * Now, copy cleartext to the screen. NOTE - we expect that PGP
375 * outputs utf-8 cleartext. This may not always be true, but it
376 * seems to be a reasonable guess.
379 if (s->flags & M_DISPLAY) {
381 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
382 else if (pgp_keyblock)
383 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
385 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
391 pgp_copy_clearsigned (tmpfp, s, body_charset);
398 state_set_prefix (s);
399 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
400 while ((c = fgetconv (fc)) != EOF)
401 state_prefix_putc (c, s);
402 fgetconv_close (&fc);
405 if (s->flags & M_DISPLAY) {
406 state_putc ('\n', s);
408 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
409 mutt_message _("PGP message successfully decrypted.");
411 else if (pgp_keyblock)
412 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
414 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
419 /* XXX - we may wish to recode here */
421 state_puts (s->prefix, s);
427 m->goodsig = (maybe_goodsig && have_any_sigs);
430 safe_fclose (&tmpfp);
431 mutt_unlink (tmpfname);
434 safe_fclose (&pgpout);
435 mutt_unlink (outfile);
438 if (needpass == -1) {
440 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
446 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
449 char tempfile[_POSIX_PATH_MAX];
450 char buf[HUGE_STRING];
457 if (b->type != TYPETEXT)
460 if (tagged_only && !b->tagged)
463 mutt_mktemp (tempfile);
464 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
469 if ((tfp = fopen (tempfile, "r")) == NULL) {
474 while (fgets (buf, sizeof (buf), tfp)) {
475 if (safe_strncmp ("-----BEGIN PGP ", buf, 15) == 0) {
476 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
478 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
480 else if (mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
487 if (!enc && !sgn && !key)
490 /* fix the content type */
492 mutt_set_parameter ("format", "fixed", &b->parameter);
494 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
496 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
498 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
503 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
508 for (; b; b = b->next) {
509 if (is_multipart (b))
510 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
511 else if (b->type == TYPETEXT) {
512 if ((r = mutt_is_application_pgp (b)))
515 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
526 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
528 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
529 FILE *fp, *pgpout, *pgperr;
534 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
536 if (!(fp = safe_fopen (sigfile, "w"))) {
537 mutt_perror (sigfile);
541 fseek (s->fpin, sigbdy->offset, 0);
542 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
545 mutt_mktemp (pgperrfile);
546 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
547 mutt_perror (pgperrfile);
552 crypt_current_time (s, "PGP");
554 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
555 -1, -1, fileno (pgperr),
556 tempfile, sigfile)) != -1) {
557 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
561 safe_fclose (&pgpout);
565 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
568 if ((rv = mutt_wait_filter (thepid)))
571 debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
574 safe_fclose (&pgperr);
576 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
578 mutt_unlink (sigfile);
579 mutt_unlink (pgperrfile);
581 debug_print (1, ("returning %d.\n", badsig));
587 /* Extract pgp public keys from messages or attachments */
589 void pgp_extract_keys_from_messages (HEADER * h)
592 char tempfname[_POSIX_PATH_MAX];
596 mutt_parse_mime_message (Context, h);
597 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
601 mutt_mktemp (tempfname);
602 if (!(fpout = safe_fopen (tempfname, "w"))) {
603 mutt_perror (tempfname);
607 set_option (OPTDONTHANDLEPGPKEYS);
610 for (i = 0; i < Context->vcount; i++) {
611 if (Context->hdrs[Context->v2r[i]]->tagged) {
612 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
613 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
614 && !pgp_valid_passphrase ()) {
618 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
619 M_CM_DECODE | M_CM_CHARCONV, 0);
624 mutt_parse_mime_message (Context, h);
625 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
629 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
634 pgp_invoke_import (tempfname);
635 mutt_any_key_to_continue (NULL);
639 mutt_unlink (tempfname);
640 unset_option (OPTDONTHANDLEPGPKEYS);
644 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
648 char tempfname[_POSIX_PATH_MAX];
650 mutt_mktemp (tempfname);
651 if (!(tempfp = safe_fopen (tempfname, "w"))) {
652 mutt_perror (tempfname);
656 memset (&s, 0, sizeof (STATE));
661 mutt_body_handler (top, &s);
665 pgp_invoke_import (tempfname);
666 mutt_any_key_to_continue (NULL);
668 mutt_unlink (tempfname);
671 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
674 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
680 set_option (OPTDONTHANDLEPGPKEYS);
682 for (; top; top = top->next) {
683 if (!tag || top->tagged)
684 pgp_extract_keys_from_attachment (fp, top);
690 unset_option (OPTDONTHANDLEPGPKEYS);
693 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
695 char buf[LONG_STRING];
696 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
700 char pgperrfile[_POSIX_PATH_MAX];
701 char pgptmpfile[_POSIX_PATH_MAX];
705 mutt_mktemp (pgperrfile);
706 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
707 mutt_perror (pgperrfile);
712 mutt_mktemp (pgptmpfile);
713 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
714 mutt_perror (pgptmpfile);
719 /* Position the stream at the beginning of the body, and send the data to
720 * the temporary file.
723 fseek (s->fpin, a->offset, 0);
724 mutt_copy_bytes (s->fpin, pgptmp, a->length);
727 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
728 fileno (pgperr), pgptmpfile)) == -1) {
731 if (s->flags & M_DISPLAY)
733 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
738 /* send the PGP passphrase to the subprocess. Never do this if the
739 agent is active, because this might lead to a passphrase send as
741 if (!pgp_use_gpg_agent ())
742 fputs (PgpPass, pgpin);
746 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
747 * read_mime_header has a hard time parsing the message.
749 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
750 len = mutt_strlen (buf);
751 if (len > 1 && buf[len - 2] == '\r')
752 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
757 rv = mutt_wait_filter (thepid);
758 mutt_unlink (pgptmpfile);
760 if (s->flags & M_DISPLAY) {
763 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
767 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
774 if (fgetc (fpout) == EOF)
779 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
781 * Need to set the length of this body part.
783 fstat (fileno (fpout), &info);
784 tattach->length = info.st_size - tattach->offset;
786 /* See if we need to recurse on this MIME part. */
788 mutt_parse_part (fpout, tattach);
794 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
796 char tempfile[_POSIX_PATH_MAX];
800 if (!mutt_is_multipart_encrypted (b))
803 if (!b->parts || !b->parts->next)
808 memset (&s, 0, sizeof (s));
810 mutt_mktemp (tempfile);
811 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
812 mutt_perror (tempfile);
817 *cur = pgp_decrypt_part (b, &s, *fpout, p);
827 void pgp_encrypted_handler (BODY * a, STATE * s)
829 char tempfile[_POSIX_PATH_MAX];
835 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
836 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
837 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
838 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
839 if (s->flags & M_DISPLAY)
840 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
846 * Move forward to the application/pgp-encrypted body.
850 mutt_mktemp (tempfile);
851 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
852 if (s->flags & M_DISPLAY)
854 ("[-- Error: could not create temporary file! --]\n"),
859 if (s->flags & M_DISPLAY)
860 crypt_current_time (s, "PGP");
862 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
863 if (s->flags & M_DISPLAY)
865 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
870 mutt_body_handler (tattach, s);
874 * if a multipart/signed is the _only_ sub-part of a
875 * multipart/encrypted, cache signature verification
880 if (mutt_is_multipart_signed (tattach) && !tattach->next)
881 p->goodsig |= tattach->goodsig;
883 if (s->flags & M_DISPLAY) {
884 state_puts ("\n", s);
885 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
888 mutt_free_body (&tattach);
889 /* clear 'Invoking...' message, since there's no error */
890 mutt_message _("PGP message successfully decrypted.");
892 mutt_error _("Could not decrypt PGP message");
895 mutt_unlink (tempfile);
898 /* ----------------------------------------------------------------------------
899 * Routines for sending PGP/MIME messages.
903 BODY *pgp_sign_message (BODY * a)
906 char buffer[LONG_STRING];
907 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
908 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
913 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
915 mutt_mktemp (sigfile);
916 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
920 mutt_mktemp (signedfile);
921 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
922 mutt_perror (signedfile);
928 mutt_write_mime_header (a, sfp);
930 mutt_write_mime_body (a, sfp);
933 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
934 -1, -1, -1, signedfile)) == -1) {
935 mutt_perror (_("Can't open PGP subprocess!"));
943 if (!pgp_use_gpg_agent ())
944 fputs (PgpPass, pgpin);
949 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
950 * recommended for future releases of PGP.
952 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
953 if (mutt_strcmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
954 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
955 else if (mutt_strcmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
956 fputs ("-----END PGP SIGNATURE-----\n", fp);
959 empty = 0; /* got some output, so we're ok */
962 /* check for errors from PGP */
964 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
966 fputs (buffer, stdout);
969 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
976 if (fclose (fp) != 0) {
977 mutt_perror ("fclose");
983 pgp_void_passphrase ();
984 mutt_any_key_to_continue (NULL);
989 return (NULL); /* fatal error while signing */
992 t = mutt_new_body ();
993 t->type = TYPEMULTIPART;
994 t->subtype = safe_strdup ("signed");
995 t->encoding = ENC7BIT;
997 t->disposition = DISPINLINE;
999 mutt_generate_boundary (&t->parameter);
1000 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1001 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1006 t->parts->next = mutt_new_body ();
1008 t->type = TYPEAPPLICATION;
1009 t->subtype = safe_strdup ("pgp-signature");
1010 t->filename = safe_strdup (sigfile);
1012 t->disposition = DISPINLINE;
1013 t->encoding = ENC7BIT;
1014 t->unlink = 1; /* ok to remove this file after sending. */
1019 static short is_numerical_keyid (const char *s)
1021 /* or should we require the "0x"? */
1022 if (strncmp (s, "0x", 2) == 0)
1024 if (mutt_strlen (s) % 8)
1027 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1033 /* This routine attempts to find the keyids of the recipients of a message.
1034 * It returns NULL if any of the keys can not be found.
1036 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1038 char *keyID, *keylist = NULL, *t;
1039 size_t keylist_size = 0;
1040 size_t keylist_used = 0;
1041 ADDRESS *tmp = NULL, *addr = NULL;
1042 ADDRESS **last = &tmp;
1045 pgp_key_t k_info = NULL, key = NULL;
1047 const char *fqdn = mutt_fqdn (1);
1049 for (i = 0; i < 3; i++) {
1064 *last = rfc822_cpy_adr (p);
1066 last = &((*last)->next);
1070 rfc822_qualify (tmp, fqdn);
1072 tmp = mutt_remove_duplicates (tmp);
1074 for (p = tmp; p; p = p->next) {
1075 char buf[LONG_STRING];
1080 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1083 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1085 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1086 if (is_numerical_keyid (keyID)) {
1087 if (strncmp (keyID, "0x", 2) == 0)
1089 goto bypass_selection; /* you don't see this. */
1092 /* check for e-mail address */
1093 if ((t = strchr (keyID, '@')) &&
1094 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1096 rfc822_qualify (addr, fqdn);
1100 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1104 rfc822_free_address (&tmp);
1105 rfc822_free_address (&addr);
1111 pgp_invoke_getkeys (q);
1115 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1116 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1118 if ((key = pgp_ask_for_key (buf, q->mailbox,
1119 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1121 rfc822_free_address (&tmp);
1122 rfc822_free_address (&addr);
1129 keyID = pgp_keyid (key);
1132 keylist_size += mutt_strlen (keyID) + 4;
1133 safe_realloc (&keylist, keylist_size);
1134 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1136 keylist_used = mutt_strlen (keylist);
1138 pgp_free_key (&key);
1139 rfc822_free_address (&addr);
1142 rfc822_free_address (&tmp);
1146 /* Warning: "a" is no longer freed in this routine, you need
1147 * to free it later. This is necessary for $fcc_attach. */
1149 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1151 char buf[LONG_STRING];
1152 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1153 char pgpinfile[_POSIX_PATH_MAX];
1154 FILE *pgpin, *pgperr, *fpout, *fptmp;
1160 mutt_mktemp (tempfile);
1161 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1162 mutt_perror (tempfile);
1166 mutt_mktemp (pgperrfile);
1167 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1168 mutt_perror (pgperrfile);
1173 unlink (pgperrfile);
1175 mutt_mktemp (pgpinfile);
1176 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1177 mutt_perror (pgpinfile);
1185 convert_to_7bit (a);
1187 mutt_write_mime_header (a, fptmp);
1188 fputc ('\n', fptmp);
1189 mutt_write_mime_body (a, fptmp);
1192 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1193 fileno (fpout), fileno (pgperr),
1194 pgpinfile, keylist, sign)) == -1) {
1201 if (!pgp_use_gpg_agent ())
1202 fputs (PgpPass, pgpin);
1203 fputc ('\n', pgpin);
1207 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1215 empty = (fgetc (fpout) == EOF);
1220 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1222 fputs (buf, stdout);
1226 /* pause if there is any error output from PGP */
1228 mutt_any_key_to_continue (NULL);
1231 /* fatal error while trying to encrypt message */
1233 pgp_void_passphrase (); /* just in case */
1238 t = mutt_new_body ();
1239 t->type = TYPEMULTIPART;
1240 t->subtype = safe_strdup ("encrypted");
1241 t->encoding = ENC7BIT;
1243 t->disposition = DISPINLINE;
1245 mutt_generate_boundary (&t->parameter);
1246 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1248 t->parts = mutt_new_body ();
1249 t->parts->type = TYPEAPPLICATION;
1250 t->parts->subtype = safe_strdup ("pgp-encrypted");
1251 t->parts->encoding = ENC7BIT;
1253 t->parts->next = mutt_new_body ();
1254 t->parts->next->type = TYPEAPPLICATION;
1255 t->parts->next->subtype = safe_strdup ("octet-stream");
1256 t->parts->next->encoding = ENC7BIT;
1257 t->parts->next->filename = safe_strdup (tempfile);
1258 t->parts->next->use_disp = 1;
1259 t->parts->next->disposition = DISPINLINE;
1260 t->parts->next->unlink = 1; /* delete after sending the message */
1261 t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime can save */
1266 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1270 char pgpoutfile[_POSIX_PATH_MAX];
1271 char pgperrfile[_POSIX_PATH_MAX];
1272 char pgpinfile[_POSIX_PATH_MAX];
1274 char body_charset[STRING];
1276 const char *send_charset;
1278 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1288 if (a->type != TYPETEXT)
1290 if (ascii_strcasecmp (a->subtype, "plain"))
1293 if ((fp = fopen (a->filename, "r")) == NULL) {
1294 mutt_perror (a->filename);
1298 mutt_mktemp (pgpinfile);
1299 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1300 mutt_perror (pgpinfile);
1305 /* The following code is really correct: If noconv is set,
1306 * a's charset parameter contains the on-disk character set, and
1307 * we have to convert from that to utf-8. If noconv is not set,
1308 * we have to convert from $charset to utf-8.
1311 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1313 from_charset = body_charset;
1315 from_charset = Charset;
1317 if (!mutt_is_us_ascii (body_charset)) {
1321 if (flags & ENCRYPT)
1322 send_charset = "us-ascii";
1324 send_charset = "utf-8";
1326 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1327 while ((c = fgetconv (fc)) != EOF)
1330 fgetconv_close (&fc);
1333 send_charset = "us-ascii";
1334 mutt_copy_stream (fp, pgpin);
1339 mutt_mktemp (pgpoutfile);
1340 mutt_mktemp (pgperrfile);
1341 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1342 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1343 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1347 unlink (pgpoutfile);
1352 unlink (pgperrfile);
1354 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1355 -1, fileno (pgpout), fileno (pgperr),
1356 pgpinfile, keylist, flags)) == -1) {
1357 mutt_perror (_("Can't invoke PGP"));
1361 mutt_unlink (pgpinfile);
1362 unlink (pgpoutfile);
1366 if (pgp_use_gpg_agent ())
1369 fprintf (pgpin, "%s\n", PgpPass);
1372 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1375 mutt_unlink (pgpinfile);
1384 empty = (fgetc (pgpout) == EOF);
1389 while (fgets (buff, sizeof (buff), pgperr)) {
1391 fputs (buff, stdout);
1397 mutt_any_key_to_continue (NULL);
1401 pgp_void_passphrase (); /* just in case */
1402 unlink (pgpoutfile);
1406 b = mutt_new_body ();
1408 b->encoding = ENC7BIT;
1411 b->subtype = safe_strdup ("plain");
1413 mutt_set_parameter ("x-action",
1414 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1416 mutt_set_parameter ("charset", send_charset, &b->parameter);
1418 b->filename = safe_strdup (pgpoutfile);
1421 /* The following is intended to give a clue to some completely brain-dead
1422 * "mail environments" which are typically used by large corporations.
1425 b->d_filename = safe_strdup ("msg.pgp");
1430 b->disposition = DISPINLINE;
1436 if (!(flags & ENCRYPT))
1437 b->encoding = a->encoding;
1442 int pgp_send_menu (HEADER * msg, int *redraw)
1445 char input_signas[SHORT_STRING];
1447 char prompt[LONG_STRING];
1449 if (!(WithCrypto & APPLICATION_PGP))
1450 return msg->security;
1452 /* If autoinline and no crypto options set, then set inline. */
1453 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1454 && (msg->security & (SIGN | ENCRYPT))))
1455 msg->security |= INLINE;
1457 snprintf (prompt, sizeof (prompt),
1458 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1459 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1461 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1462 case 1: /* (e)ncrypt */
1463 msg->security |= ENCRYPT;
1464 msg->security &= ~SIGN;
1467 case 2: /* (s)ign */
1468 msg->security |= SIGN;
1469 msg->security &= ~ENCRYPT;
1472 case 3: /* sign (a)s */
1473 unset_option (OPTPGPCHECKTRUST);
1476 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1478 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1479 str_replace (&PgpSignAs, input_signas);
1482 msg->security |= SIGN;
1484 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1488 msg->security &= ~SIGN;
1492 *redraw = REDRAW_FULL;
1495 case 4: /* (b)oth */
1496 msg->security |= (ENCRYPT | SIGN);
1499 case 5: /* (i)nline */
1500 if ((msg->security & (ENCRYPT | SIGN)))
1501 msg->security ^= INLINE;
1503 msg->security &= ~INLINE;
1506 case 6: /* (f)orget it */
1507 case 7: /* (c)lear */
1512 if (msg->security) {
1513 if (!(msg->security & (ENCRYPT | SIGN)))
1516 msg->security |= APPLICATION_PGP;
1519 return (msg->security);
1523 #endif /* CRYPT_BACKEND_CLASSIC_PGP */