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"
46 #ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
50 #ifdef HAVE_SYS_RESOURCE_H
51 # include <sys/resource.h>
54 #ifdef CRYPT_BACKEND_CLASSIC_PGP
56 #include "mutt_crypt.h"
57 #include "mutt_menu.h"
61 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
63 void pgp_void_passphrase (void)
65 memset (PgpPass, 0, sizeof (PgpPass));
69 int pgp_valid_passphrase (void)
71 time_t now = time (NULL);
73 if (pgp_use_gpg_agent ()) {
75 return 1; /* handled by gpg-agent */
79 /* Use cached copy. */
82 pgp_void_passphrase ();
85 (_("Enter PGP passphrase:"), PgpPass, sizeof (PgpPass)) == 0) {
86 PgpExptime = time (NULL) + PgpTimeout;
95 void pgp_forget_passphrase (void)
97 pgp_void_passphrase ();
98 mutt_message _("PGP passphrase forgotten.");
101 int pgp_use_gpg_agent (void)
103 return option (OPTUSEGPGAGENT) && getenv ("GPG_TTY")
104 && getenv ("GPG_AGENT_INFO");
107 char *pgp_keyid (pgp_key_t k)
109 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
112 return _pgp_keyid (k);
115 char *_pgp_keyid (pgp_key_t k)
117 if (option (OPTPGPLONGIDS))
120 return (k->keyid + 8);
123 /* ----------------------------------------------------------------------------
124 * Routines for handing PGP input.
129 /* Copy PGP output messages and look for signs of a good signature */
131 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
135 if (PgpGoodSign.pattern) {
140 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
141 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
142 dprint (2, (debugfile, "pgp_copy_checksig: \"%s\" matches regexp.\n",
149 "pgp_copy_checksig: \"%s\" doesn't match regexp.\n", line));
151 if (strncmp (line, "[GNUPG:] ", 9) == 0)
159 dprint (2, (debugfile, "pgp_copy_checksig: No pattern.\n"));
160 mutt_copy_stream (fpin, fpout);
168 * Copy a clearsigned message, and strip the signature and PGP's
171 * XXX - charset handling: We assume that it is safe to do
172 * character set decoding first, dash decoding second here, while
173 * we do it the other way around in the main handler.
175 * (Note that we aren't worse than Outlook &c in this, and also
176 * note that we can successfully handle anything produced by any
177 * existing versions of mutt.)
180 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
182 char buf[HUGE_STRING];
183 short complete, armor_header;
189 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
191 for (complete = 1, armor_header = 1;
192 fgetconvs (buf, sizeof (buf), fc) != NULL;
193 complete = strchr (buf, '\n') != NULL) {
200 if (mutt_strcmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
204 char *p = mutt_skip_whitespace (buf);
212 state_puts (s->prefix, s);
214 if (buf[0] == '-' && buf[1] == ' ')
215 state_puts (buf + 2, s);
220 fgetconv_close (&fc);
224 /* Support for the Application/PGP Content Type. */
226 void pgp_application_pgp_handler (BODY * m, STATE * s)
228 int needpass = -1, pgp_keyblock = 0;
229 int clearsign = 0, rv, rc;
231 long bytes, last_pos, offset;
232 char buf[HUGE_STRING];
233 char outfile[_POSIX_PATH_MAX];
234 char tmpfname[_POSIX_PATH_MAX];
235 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
239 short maybe_goodsig = 1;
240 short have_any_sigs = 0;
242 char body_charset[STRING];
244 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
246 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
248 fseek (s->fpin, m->offset, 0);
249 last_pos = m->offset;
251 for (bytes = m->length; bytes > 0;) {
252 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
255 offset = ftell (s->fpin);
256 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
259 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0) {
261 start_pos = last_pos;
263 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
265 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
269 else if (!option (OPTDONTHANDLEPGPKEYS) &&
270 mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
275 /* XXX - we may wish to recode here */
277 state_puts (s->prefix, s);
282 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
284 /* Copy PGP material to temporary file */
285 mutt_mktemp (tmpfname);
286 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
287 mutt_perror (tmpfname);
292 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
293 offset = ftell (s->fpin);
294 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
300 && mutt_strcmp ("-----END PGP MESSAGE-----\n", buf) == 0)
302 && (mutt_strcmp ("-----END PGP SIGNATURE-----\n", buf) == 0
303 || mutt_strcmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
308 /* leave tmpfp open in case we still need it - but flush it! */
312 /* Invoke PGP if needed */
313 if (!clearsign || (s->flags & M_VERIFY)) {
314 mutt_mktemp (outfile);
315 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
316 mutt_perror (tmpfname);
320 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
321 fileno (pgpout), -1, tmpfname,
323 safe_fclose (&pgpout);
328 ("[-- Error: unable to create PGP subprocess! --]\n"),
331 else { /* PGP started successfully */
334 if (!pgp_valid_passphrase ())
335 pgp_void_passphrase ();
336 if (pgp_use_gpg_agent ())
338 fprintf (pgpin, "%s\n", PgpPass);
341 safe_fclose (&pgpin);
343 if (s->flags & M_DISPLAY) {
344 crypt_current_time (s, "PGP");
345 rc = pgp_copy_checksig (pgperr, s->fpout);
348 safe_fclose (&pgperr);
349 rv = mutt_wait_filter (thepid);
351 if (s->flags & M_DISPLAY) {
356 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
357 * Sig _is_ correct if
358 * gpg_good_sign="" && pgp_decode_command returned 0
363 state_putc ('\n', s);
364 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
371 * Now, copy cleartext to the screen. NOTE - we expect that PGP
372 * outputs utf-8 cleartext. This may not always be true, but it
373 * seems to be a reasonable guess.
376 if (s->flags & M_DISPLAY) {
378 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
379 else if (pgp_keyblock)
380 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
382 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
388 pgp_copy_clearsigned (tmpfp, s, body_charset);
395 state_set_prefix (s);
396 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
397 while ((c = fgetconv (fc)) != EOF)
398 state_prefix_putc (c, s);
399 fgetconv_close (&fc);
402 if (s->flags & M_DISPLAY) {
403 state_putc ('\n', s);
405 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
406 else if (pgp_keyblock)
407 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
409 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
413 safe_fclose (&tmpfp);
414 mutt_unlink (tmpfname);
417 safe_fclose (&pgpout);
418 mutt_unlink (outfile);
422 /* XXX - we may wish to recode here */
424 state_puts (s->prefix, s);
429 m->goodsig = (maybe_goodsig && have_any_sigs);
431 if (needpass == -1) {
433 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
439 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
442 char tempfile[_POSIX_PATH_MAX];
443 char buf[HUGE_STRING];
450 if (b->type != TYPETEXT)
453 if (tagged_only && !b->tagged)
456 mutt_mktemp (tempfile);
457 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
462 if ((tfp = fopen (tempfile, "r")) == NULL) {
467 while (fgets (buf, sizeof (buf), tfp)) {
468 if (mutt_strncmp ("-----BEGIN PGP ", buf, 15) == 0) {
469 if (mutt_strcmp ("MESSAGE-----\n", buf + 15) == 0)
471 else if (mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
473 else if (mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
480 if (!enc && !sgn && !key)
483 /* fix the content type */
485 mutt_set_parameter ("format", "fixed", &b->parameter);
487 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
489 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
491 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
496 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
501 for (; b; b = b->next) {
502 if (is_multipart (b))
503 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
504 else if (b->type == TYPETEXT) {
505 if ((r = mutt_is_application_pgp (b)))
508 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
519 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
521 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
522 FILE *fp, *pgpout, *pgperr;
527 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
529 if (!(fp = safe_fopen (sigfile, "w"))) {
530 mutt_perror (sigfile);
534 fseek (s->fpin, sigbdy->offset, 0);
535 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
538 mutt_mktemp (pgperrfile);
539 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
540 mutt_perror (pgperrfile);
545 crypt_current_time (s, "PGP");
547 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
548 -1, -1, fileno (pgperr),
549 tempfile, sigfile)) != -1) {
550 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
554 safe_fclose (&pgpout);
558 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
561 if ((rv = mutt_wait_filter (thepid)))
565 (debugfile, "pgp_verify_one: mutt_wait_filter returned %d.\n",
569 safe_fclose (&pgperr);
571 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
573 mutt_unlink (sigfile);
574 mutt_unlink (pgperrfile);
576 dprint (1, (debugfile, "pgp_verify_one: returning %d.\n", badsig));
582 /* Extract pgp public keys from messages or attachments */
584 void pgp_extract_keys_from_messages (HEADER * h)
587 char tempfname[_POSIX_PATH_MAX];
591 mutt_parse_mime_message (Context, h);
592 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
596 mutt_mktemp (tempfname);
597 if (!(fpout = safe_fopen (tempfname, "w"))) {
598 mutt_perror (tempfname);
602 set_option (OPTDONTHANDLEPGPKEYS);
605 for (i = 0; i < Context->vcount; i++) {
606 if (Context->hdrs[Context->v2r[i]]->tagged) {
607 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
608 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
609 && !pgp_valid_passphrase ()) {
613 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
614 M_CM_DECODE | M_CM_CHARCONV, 0);
619 mutt_parse_mime_message (Context, h);
620 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
624 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
629 pgp_invoke_import (tempfname);
630 mutt_any_key_to_continue (NULL);
634 mutt_unlink (tempfname);
635 unset_option (OPTDONTHANDLEPGPKEYS);
639 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
643 char tempfname[_POSIX_PATH_MAX];
645 mutt_mktemp (tempfname);
646 if (!(tempfp = safe_fopen (tempfname, "w"))) {
647 mutt_perror (tempfname);
651 memset (&s, 0, sizeof (STATE));
656 mutt_body_handler (top, &s);
660 pgp_invoke_import (tempfname);
661 mutt_any_key_to_continue (NULL);
663 mutt_unlink (tempfname);
666 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
669 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
675 set_option (OPTDONTHANDLEPGPKEYS);
677 for (; top; top = top->next) {
678 if (!tag || top->tagged)
679 pgp_extract_keys_from_attachment (fp, top);
685 unset_option (OPTDONTHANDLEPGPKEYS);
688 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
690 char buf[LONG_STRING];
691 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
695 char pgperrfile[_POSIX_PATH_MAX];
696 char pgptmpfile[_POSIX_PATH_MAX];
699 mutt_mktemp (pgperrfile);
700 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
701 mutt_perror (pgperrfile);
706 mutt_mktemp (pgptmpfile);
707 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
708 mutt_perror (pgptmpfile);
713 /* Position the stream at the beginning of the body, and send the data to
714 * the temporary file.
717 fseek (s->fpin, a->offset, 0);
718 mutt_copy_bytes (s->fpin, pgptmp, a->length);
721 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
722 fileno (pgperr), pgptmpfile)) == -1) {
725 if (s->flags & M_DISPLAY)
727 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
732 /* send the PGP passphrase to the subprocess. Never do this if the
733 agent is active, because this might lead to a passphrase send as
735 if (!pgp_use_gpg_agent ())
736 fputs (PgpPass, pgpin);
740 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
741 * read_mime_header has a hard time parsing the message.
743 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
744 len = mutt_strlen (buf);
745 if (len > 1 && buf[len - 2] == '\r')
746 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
751 mutt_wait_filter (thepid);
752 mutt_unlink (pgptmpfile);
754 if (s->flags & M_DISPLAY) {
757 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && p)
759 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
766 if (fgetc (fpout) == EOF)
771 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
773 * Need to set the length of this body part.
775 fstat (fileno (fpout), &info);
776 tattach->length = info.st_size - tattach->offset;
778 /* See if we need to recurse on this MIME part. */
780 mutt_parse_part (fpout, tattach);
786 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
788 char tempfile[_POSIX_PATH_MAX];
792 if (!mutt_is_multipart_encrypted (b))
795 if (!b->parts || !b->parts->next)
800 memset (&s, 0, sizeof (s));
802 mutt_mktemp (tempfile);
803 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
804 mutt_perror (tempfile);
809 *cur = pgp_decrypt_part (b, &s, *fpout, p);
819 void pgp_encrypted_handler (BODY * a, STATE * s)
821 char tempfile[_POSIX_PATH_MAX];
827 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
828 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
829 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
830 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
831 if (s->flags & M_DISPLAY)
832 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
838 * Move forward to the application/pgp-encrypted body.
842 mutt_mktemp (tempfile);
843 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
844 if (s->flags & M_DISPLAY)
846 ("[-- Error: could not create temporary file! --]\n"),
851 if (s->flags & M_DISPLAY)
852 crypt_current_time (s, "PGP");
854 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
855 if (s->flags & M_DISPLAY)
857 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
862 mutt_body_handler (tattach, s);
866 * if a multipart/signed is the _only_ sub-part of a
867 * multipart/encrypted, cache signature verification
872 if (mutt_is_multipart_signed (tattach) && !tattach->next)
873 p->goodsig |= tattach->goodsig;
875 if (s->flags & M_DISPLAY) {
876 state_puts ("\n", s);
877 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
880 mutt_free_body (&tattach);
884 mutt_unlink (tempfile);
887 /* ----------------------------------------------------------------------------
888 * Routines for sending PGP/MIME messages.
892 BODY *pgp_sign_message (BODY * a)
895 char buffer[LONG_STRING];
896 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
897 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
902 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
904 mutt_mktemp (sigfile);
905 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
909 mutt_mktemp (signedfile);
910 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
911 mutt_perror (signedfile);
917 mutt_write_mime_header (a, sfp);
919 mutt_write_mime_body (a, sfp);
922 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
923 -1, -1, -1, signedfile)) == -1) {
924 mutt_perror _("Can't open PGP subprocess!");
932 if (!pgp_use_gpg_agent ())
933 fputs (PgpPass, pgpin);
938 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
939 * recommended for future releases of PGP.
941 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
942 if (mutt_strcmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
943 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
944 else if (mutt_strcmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
945 fputs ("-----END PGP SIGNATURE-----\n", fp);
948 empty = 0; /* got some output, so we're ok */
951 /* check for errors from PGP */
953 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
955 fputs (buffer, stdout);
958 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
965 if (fclose (fp) != 0) {
966 mutt_perror ("fclose");
972 pgp_void_passphrase ();
973 mutt_any_key_to_continue (NULL);
978 return (NULL); /* fatal error while signing */
981 t = mutt_new_body ();
982 t->type = TYPEMULTIPART;
983 t->subtype = safe_strdup ("signed");
984 t->encoding = ENC7BIT;
986 t->disposition = DISPINLINE;
988 mutt_generate_boundary (&t->parameter);
989 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
990 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
995 t->parts->next = mutt_new_body ();
997 t->type = TYPEAPPLICATION;
998 t->subtype = safe_strdup ("pgp-signature");
999 t->filename = safe_strdup (sigfile);
1001 t->disposition = DISPINLINE;
1002 t->encoding = ENC7BIT;
1003 t->unlink = 1; /* ok to remove this file after sending. */
1008 static short is_numerical_keyid (const char *s)
1010 /* or should we require the "0x"? */
1011 if (strncmp (s, "0x", 2) == 0)
1013 if (mutt_strlen (s) % 8)
1016 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1022 /* This routine attempts to find the keyids of the recipients of a message.
1023 * It returns NULL if any of the keys can not be found.
1025 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1027 char *keyID, *keylist = NULL, *t;
1028 size_t keylist_size = 0;
1029 size_t keylist_used = 0;
1030 ADDRESS *tmp = NULL, *addr = NULL;
1031 ADDRESS **last = &tmp;
1034 pgp_key_t k_info = NULL, key = NULL;
1036 const char *fqdn = mutt_fqdn (1);
1038 for (i = 0; i < 3; i++) {
1053 *last = rfc822_cpy_adr (p);
1055 last = &((*last)->next);
1059 rfc822_qualify (tmp, fqdn);
1061 tmp = mutt_remove_duplicates (tmp);
1063 for (p = tmp; p; p = p->next) {
1064 char buf[LONG_STRING];
1069 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1072 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1074 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1075 if (is_numerical_keyid (keyID)) {
1076 if (strncmp (keyID, "0x", 2) == 0)
1078 goto bypass_selection; /* you don't see this. */
1081 /* check for e-mail address */
1082 if ((t = strchr (keyID, '@')) &&
1083 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1085 rfc822_qualify (addr, fqdn);
1089 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1093 rfc822_free_address (&tmp);
1094 rfc822_free_address (&addr);
1100 pgp_invoke_getkeys (q);
1104 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1105 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1107 if ((key = pgp_ask_for_key (buf, q->mailbox,
1108 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1110 rfc822_free_address (&tmp);
1111 rfc822_free_address (&addr);
1118 keyID = pgp_keyid (key);
1121 keylist_size += mutt_strlen (keyID) + 4;
1122 safe_realloc (&keylist, keylist_size);
1123 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1125 keylist_used = mutt_strlen (keylist);
1127 pgp_free_key (&key);
1128 rfc822_free_address (&addr);
1131 rfc822_free_address (&tmp);
1135 /* Warning: "a" is no longer freed in this routine, you need
1136 * to free it later. This is necessary for $fcc_attach. */
1138 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1140 char buf[LONG_STRING];
1141 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1142 char pgpinfile[_POSIX_PATH_MAX];
1143 FILE *pgpin, *pgperr, *fpout, *fptmp;
1149 mutt_mktemp (tempfile);
1150 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1151 mutt_perror (tempfile);
1155 mutt_mktemp (pgperrfile);
1156 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1157 mutt_perror (pgperrfile);
1162 unlink (pgperrfile);
1164 mutt_mktemp (pgpinfile);
1165 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1166 mutt_perror (pgpinfile);
1174 convert_to_7bit (a);
1176 mutt_write_mime_header (a, fptmp);
1177 fputc ('\n', fptmp);
1178 mutt_write_mime_body (a, fptmp);
1181 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1182 fileno (fpout), fileno (pgperr),
1183 pgpinfile, keylist, sign)) == -1) {
1190 if (!pgp_use_gpg_agent ())
1191 fputs (PgpPass, pgpin);
1192 fputc ('\n', pgpin);
1196 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1204 empty = (fgetc (fpout) == EOF);
1209 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1211 fputs (buf, stdout);
1215 /* pause if there is any error output from PGP */
1217 mutt_any_key_to_continue (NULL);
1220 /* fatal error while trying to encrypt message */
1225 t = mutt_new_body ();
1226 t->type = TYPEMULTIPART;
1227 t->subtype = safe_strdup ("encrypted");
1228 t->encoding = ENC7BIT;
1230 t->disposition = DISPINLINE;
1232 mutt_generate_boundary (&t->parameter);
1233 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1235 t->parts = mutt_new_body ();
1236 t->parts->type = TYPEAPPLICATION;
1237 t->parts->subtype = safe_strdup ("pgp-encrypted");
1238 t->parts->encoding = ENC7BIT;
1240 t->parts->next = mutt_new_body ();
1241 t->parts->next->type = TYPEAPPLICATION;
1242 t->parts->next->subtype = safe_strdup ("octet-stream");
1243 t->parts->next->encoding = ENC7BIT;
1244 t->parts->next->filename = safe_strdup (tempfile);
1245 t->parts->next->use_disp = 1;
1246 t->parts->next->disposition = DISPINLINE;
1247 t->parts->next->unlink = 1; /* delete after sending the message */
1248 t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime can save */
1253 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1257 char pgpoutfile[_POSIX_PATH_MAX];
1258 char pgperrfile[_POSIX_PATH_MAX];
1259 char pgpinfile[_POSIX_PATH_MAX];
1261 char body_charset[STRING];
1263 const char *send_charset;
1265 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1275 if (a->type != TYPETEXT)
1277 if (ascii_strcasecmp (a->subtype, "plain"))
1280 if ((fp = fopen (a->filename, "r")) == NULL) {
1281 mutt_perror (a->filename);
1285 mutt_mktemp (pgpinfile);
1286 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1287 mutt_perror (pgpinfile);
1292 /* The following code is really correct: If noconv is set,
1293 * a's charset parameter contains the on-disk character set, and
1294 * we have to convert from that to utf-8. If noconv is not set,
1295 * we have to convert from $charset to utf-8.
1298 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1300 from_charset = body_charset;
1302 from_charset = Charset;
1304 if (!mutt_is_us_ascii (body_charset)) {
1308 if (flags & ENCRYPT)
1309 send_charset = "us-ascii";
1311 send_charset = "utf-8";
1313 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1314 while ((c = fgetconv (fc)) != EOF)
1317 fgetconv_close (&fc);
1320 send_charset = "us-ascii";
1321 mutt_copy_stream (fp, pgpin);
1326 mutt_mktemp (pgpoutfile);
1327 mutt_mktemp (pgperrfile);
1328 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1329 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1330 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1334 unlink (pgpoutfile);
1339 unlink (pgperrfile);
1341 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1342 -1, fileno (pgpout), fileno (pgperr),
1343 pgpinfile, keylist, flags)) == -1) {
1344 mutt_perror _("Can't invoke PGP");
1348 mutt_unlink (pgpinfile);
1349 unlink (pgpoutfile);
1353 if (pgp_use_gpg_agent ())
1356 fprintf (pgpin, "%s\n", PgpPass);
1359 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1362 mutt_unlink (pgpinfile);
1371 empty = (fgetc (pgpout) == EOF);
1376 while (fgets (buff, sizeof (buff), pgperr)) {
1378 fputs (buff, stdout);
1384 mutt_any_key_to_continue (NULL);
1387 unlink (pgpoutfile);
1391 b = mutt_new_body ();
1393 b->encoding = ENC7BIT;
1396 b->subtype = safe_strdup ("plain");
1398 mutt_set_parameter ("x-action",
1399 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1401 mutt_set_parameter ("charset", send_charset, &b->parameter);
1403 b->filename = safe_strdup (pgpoutfile);
1406 /* The following is intended to give a clue to some completely brain-dead
1407 * "mail environments" which are typically used by large corporations.
1410 b->d_filename = safe_strdup ("msg.pgp");
1415 b->disposition = DISPINLINE;
1421 if (!(flags & ENCRYPT))
1422 b->encoding = a->encoding;
1427 int pgp_send_menu (HEADER * msg, int *redraw)
1430 char input_signas[SHORT_STRING];
1432 char prompt[LONG_STRING];
1434 if (!(WithCrypto & APPLICATION_PGP))
1435 return msg->security;
1437 /* If autoinline and no crypto options set, then set inline. */
1438 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1439 && (msg->security & (SIGN | ENCRYPT))))
1440 msg->security |= INLINE;
1442 snprintf (prompt, sizeof (prompt),
1443 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1444 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1446 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1447 case 1: /* (e)ncrypt */
1448 msg->security |= ENCRYPT;
1449 msg->security &= ~SIGN;
1452 case 2: /* (s)ign */
1453 msg->security |= SIGN;
1454 msg->security &= ~ENCRYPT;
1457 case 3: /* sign (a)s */
1458 unset_option (OPTPGPCHECKTRUST);
1461 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1463 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1464 mutt_str_replace (&PgpSignAs, input_signas);
1467 msg->security |= SIGN;
1469 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1473 msg->security &= ~SIGN;
1477 *redraw = REDRAW_FULL;
1480 case 4: /* (b)oth */
1481 msg->security |= (ENCRYPT | SIGN);
1484 case 5: /* (i)nline */
1485 if ((msg->security & (ENCRYPT | SIGN)))
1486 msg->security ^= INLINE;
1488 msg->security &= ~INLINE;
1491 case 6: /* (f)orget it */
1492 case 7: /* (c)lear */
1497 if (msg->security) {
1498 if (!(msg->security & (ENCRYPT | SIGN)))
1501 msg->security |= APPLICATION_PGP;
1504 return (msg->security);
1508 #endif /* CRYPT_BACKEND_CLASSIC_PGP */