2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
5 * Copyright (C) 2004 g10 Code GmbH
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
13 * This file contains all of the PGP routines necessary to sign, encrypt,
14 * verify and decrypt PGP messages in either the new PGP/MIME format, or
15 * in the older Application/Pgp format. It also contains some code to
16 * cache the user's passphrase for repeat use when decrypting or signing
24 #include <lib-lib/lib-lib.h>
26 #include <lib-mime/mime.h>
28 #include <lib-ui/curses.h>
29 #include <lib-ui/enter.h>
30 #include <lib-ui/menu.h>
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
55 #ifdef HAVE_SYS_RESOURCE_H
56 # include <sys/resource.h>
62 char PgpPass[LONG_STRING];
63 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
65 void pgp_void_passphrase (void)
67 p_clear(PgpPass, countof(PgpPass));
71 int pgp_valid_passphrase (void)
73 time_t now = time (NULL);
75 if (pgp_use_gpg_agent ()) {
77 return 1; /* handled by gpg-agent */
81 /* Use cached copy. */
84 pgp_void_passphrase ();
86 if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
87 sizeof (PgpPass), M_PASS) == 0) {
88 PgpExptime = time (NULL) + PgpTimeout;
97 void pgp_forget_passphrase (void)
99 pgp_void_passphrase ();
100 mutt_message _("PGP passphrase forgotten.");
103 int pgp_use_gpg_agent (void) {
106 if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
109 if ((tty = ttyname(0)))
110 setenv ("GPG_TTY", tty, 0);
115 char *pgp_keyid (pgp_key_t k)
117 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
120 return _pgp_keyid (k);
123 char *_pgp_keyid (pgp_key_t k)
125 if (option (OPTPGPLONGIDS))
128 return (k->keyid + 8);
131 /* ----------------------------------------------------------------------------
132 * Routines for handing PGP input.
137 /* Copy PGP output messages and look for signs of a good signature */
139 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
143 if (PgpGoodSign.pattern) {
148 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
149 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
153 if (strncmp (line, "[GNUPG:] ", 9) == 0)
161 mutt_copy_stream (fpin, fpout);
169 * Copy a clearsigned message, and strip the signature and PGP's
172 * XXX - charset handling: We assume that it is safe to do
173 * character set decoding first, dash decoding second here, while
174 * we do it the other way around in the main handler.
176 * (Note that we aren't worse than Outlook &c in this, and also
177 * note that we can successfully handle anything produced by any
178 * existing versions of mutt.)
181 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
183 char buf[HUGE_STRING];
184 short complete, armor_header;
190 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
192 for (complete = 1, armor_header = 1;
193 fgetconvs (buf, sizeof (buf), fc) != NULL;
194 complete = strchr (buf, '\n') != NULL) {
201 if (m_strcmp(buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
205 char *p = vskipspaces(buf);
213 state_puts (s->prefix, s);
215 if (buf[0] == '-' && buf[1] == ' ')
216 state_puts (buf + 2, s);
221 fgetconv_close (&fc);
225 /* Support for the Application/PGP Content Type. */
227 int pgp_application_pgp_handler (BODY * m, STATE * s)
229 int could_not_decrypt = 0;
230 int needpass = -1, pgp_keyblock = 0;
232 int clearsign = 0, rv, rc;
235 off_t last_pos, offset;
236 char buf[HUGE_STRING];
237 char outfile[_POSIX_PATH_MAX];
238 char tmpfname[_POSIX_PATH_MAX];
239 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
243 short maybe_goodsig = 1;
244 short have_any_sigs = 0;
246 char body_charset[STRING];
248 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
250 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
252 fseeko (s->fpin, m->offset, 0);
253 last_pos = m->offset;
255 for (bytes = m->length; bytes > 0;) {
256 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
259 offset = ftello (s->fpin);
260 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
263 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
265 start_pos = last_pos;
267 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
269 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0) {
273 else if (!option (OPTDONTHANDLEPGPKEYS) &&
274 m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
279 /* XXX - we may wish to recode here */
281 state_puts (s->prefix, s);
286 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
288 /* Copy PGP material to temporary file */
289 mutt_mktemp (tmpfname);
290 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
291 mutt_perror (tmpfname);
296 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
297 offset = ftello (s->fpin);
298 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
304 && m_strcmp("-----END PGP MESSAGE-----\n", buf) == 0)
306 && (m_strcmp("-----END PGP SIGNATURE-----\n", buf) == 0
307 || m_strcmp("-----END PGP PUBLIC KEY BLOCK-----\n",
312 /* leave tmpfp open in case we still need it - but flush it! */
316 /* Invoke PGP if needed */
317 if (!clearsign || (s->flags & M_VERIFY)) {
318 mutt_mktemp (outfile);
319 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
320 mutt_perror (tmpfname);
324 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
325 fileno (pgpout), -1, tmpfname,
327 safe_fclose (&pgpout);
332 ("[-- Error: unable to create PGP subprocess! --]\n"),
335 else { /* PGP started successfully */
338 if (!pgp_valid_passphrase ())
339 pgp_void_passphrase ();
340 if (pgp_use_gpg_agent ())
342 fprintf (pgpin, "%s\n", PgpPass);
345 safe_fclose (&pgpin);
347 if (s->flags & M_DISPLAY) {
348 crypt_current_time (s, "PGP");
349 rc = pgp_copy_checksig (pgperr, s->fpout);
352 safe_fclose (&pgperr);
353 rv = mutt_wait_filter (thepid);
355 if (s->flags & M_DISPLAY) {
361 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
362 * Sig _is_ correct if
363 * gpg_good_sign="" && pgp_decode_command returned 0
368 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
372 /* treat empty result as sign of failure */
373 /* TODO: maybe on failure mutt should include the original undecoded text. */
379 if (!clearsign && (!pgpout || c == EOF)) {
380 could_not_decrypt = 1;
381 pgp_void_passphrase ();
384 if (could_not_decrypt && !(s->flags & M_DISPLAY)) {
385 mutt_error _("Could not decrypt PGP message");
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);
416 state_set_prefix (s);
417 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
418 while ((c = fgetconv (fc)) != EOF)
419 state_prefix_putc (c, s);
420 fgetconv_close (&fc);
423 if (s->flags & M_DISPLAY) {
424 state_putc ('\n', s);
426 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
427 if (could_not_decrypt)
428 mutt_error _("Could not decrypt PGP message.");
430 mutt_message _("PGP message successfully decrypted.");
432 else if (pgp_keyblock)
433 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
435 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
440 /* XXX - we may wish to recode here */
442 state_puts (s->prefix, s);
450 m->goodsig = (maybe_goodsig && have_any_sigs);
453 safe_fclose (&tmpfp);
454 mutt_unlink (tmpfname);
457 safe_fclose (&pgpout);
458 mutt_unlink (outfile);
461 if (needpass == -1) {
463 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
471 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
474 char tempfile[_POSIX_PATH_MAX];
475 char buf[HUGE_STRING];
482 if (b->type != TYPETEXT)
485 if (tagged_only && !b->tagged)
488 mutt_mktemp (tempfile);
489 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
494 if ((tfp = fopen (tempfile, "r")) == NULL) {
499 while (fgets (buf, sizeof (buf), tfp)) {
500 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
501 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
503 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0)
505 else if (m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
512 if (!enc && !sgn && !key)
515 /* fix the content type */
517 parameter_setval(&b->parameter, "format", "fixed");
519 parameter_setval(&b->parameter, "x-action", "pgp-encrypted");
521 parameter_setval(&b->parameter, "x-action", "pgp-signed");
523 parameter_setval(&b->parameter, "x-action", "pgp-keys");
528 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
533 for (; b; b = b->next) {
534 if (is_multipart (b))
535 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
536 else if (b->type == TYPETEXT) {
537 if ((r = mutt_is_application_pgp (b)))
540 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
551 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
553 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
554 FILE *fp, *pgpout, *pgperr;
559 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
561 if (!(fp = safe_fopen (sigfile, "w"))) {
562 mutt_perror (sigfile);
566 fseeko (s->fpin, sigbdy->offset, 0);
567 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
570 mutt_mktemp (pgperrfile);
571 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
572 mutt_perror (pgperrfile);
577 crypt_current_time (s, "PGP");
579 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
580 -1, -1, fileno (pgperr),
581 tempfile, sigfile)) != -1) {
582 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
586 safe_fclose (&pgpout);
590 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
593 if ((rv = mutt_wait_filter (thepid)))
597 safe_fclose (&pgperr);
599 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
601 mutt_unlink (sigfile);
602 mutt_unlink (pgperrfile);
608 /* Extract pgp public keys from messages or attachments */
610 void pgp_extract_keys_from_messages (HEADER * h)
613 char tempfname[_POSIX_PATH_MAX];
617 mutt_parse_mime_message (Context, h);
618 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
622 mutt_mktemp (tempfname);
623 if (!(fpout = safe_fopen (tempfname, "w"))) {
624 mutt_perror (tempfname);
628 set_option (OPTDONTHANDLEPGPKEYS);
631 for (i = 0; i < Context->vcount; i++) {
632 if (Context->hdrs[Context->v2r[i]]->tagged) {
633 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
634 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
635 && !pgp_valid_passphrase ()) {
639 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
640 M_CM_DECODE | M_CM_CHARCONV, 0);
645 mutt_parse_mime_message (Context, h);
646 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
650 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
655 pgp_invoke_import (tempfname);
656 mutt_any_key_to_continue (NULL);
660 mutt_unlink (tempfname);
661 unset_option (OPTDONTHANDLEPGPKEYS);
665 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
669 char tempfname[_POSIX_PATH_MAX];
671 mutt_mktemp (tempfname);
672 if (!(tempfp = safe_fopen (tempfname, "w"))) {
673 mutt_perror (tempfname);
682 mutt_body_handler (top, &s);
686 pgp_invoke_import (tempfname);
687 mutt_any_key_to_continue (NULL);
689 mutt_unlink (tempfname);
692 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
695 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
701 set_option (OPTDONTHANDLEPGPKEYS);
703 for (; top; top = top->next) {
704 if (!tag || top->tagged)
705 pgp_extract_keys_from_attachment (fp, top);
711 unset_option (OPTDONTHANDLEPGPKEYS);
714 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
716 char buf[LONG_STRING];
717 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
721 char pgperrfile[_POSIX_PATH_MAX];
722 char pgptmpfile[_POSIX_PATH_MAX];
726 mutt_mktemp (pgperrfile);
727 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
728 mutt_perror (pgperrfile);
733 mutt_mktemp (pgptmpfile);
734 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
735 mutt_perror (pgptmpfile);
740 /* Position the stream at the beginning of the body, and send the data to
741 * the temporary file.
744 fseeko (s->fpin, a->offset, 0);
745 mutt_copy_bytes (s->fpin, pgptmp, a->length);
748 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
749 fileno (pgperr), pgptmpfile)) == -1) {
752 if (s->flags & M_DISPLAY)
754 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
759 /* send the PGP passphrase to the subprocess. Never do this if the
760 agent is active, because this might lead to a passphrase send as
762 if (!pgp_use_gpg_agent ())
763 fputs (PgpPass, pgpin);
767 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
768 * read_mime_header has a hard time parsing the message.
770 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
772 if (len > 1 && buf[len - 2] == '\r')
773 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
778 rv = mutt_wait_filter (thepid);
779 mutt_unlink (pgptmpfile);
781 if (s->flags & M_DISPLAY) {
784 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
788 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
795 if (fgetc (fpout) == EOF) {
796 mutt_error (_("Decryption failed."));
797 pgp_void_passphrase ();
803 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
805 * Need to set the length of this body part.
807 fstat (fileno (fpout), &info);
808 tattach->length = info.st_size - tattach->offset;
810 /* See if we need to recurse on this MIME part. */
812 mutt_parse_part (fpout, tattach);
818 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
820 char tempfile[_POSIX_PATH_MAX];
824 if (!mutt_is_multipart_encrypted (b))
827 if (!b->parts || !b->parts->next)
834 mutt_mktemp (tempfile);
835 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
836 mutt_perror (tempfile);
841 *cur = pgp_decrypt_part (b, &s, *fpout, p);
851 int pgp_encrypted_handler (BODY * a, STATE * s)
853 char tempfile[_POSIX_PATH_MAX];
860 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
861 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
862 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
863 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
864 if (s->flags & M_DISPLAY)
865 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
871 * Move forward to the application/pgp-encrypted body.
875 mutt_mktemp (tempfile);
876 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
877 if (s->flags & M_DISPLAY)
879 ("[-- Error: could not create temporary file! --]\n"),
884 if (s->flags & M_DISPLAY)
885 crypt_current_time (s, "PGP");
887 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
888 if (s->flags & M_DISPLAY)
890 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
895 rc = mutt_body_handler (tattach, s);
899 * if a multipart/signed is the _only_ sub-part of a
900 * multipart/encrypted, cache signature verification
905 if (mutt_is_multipart_signed (tattach) && !tattach->next)
906 p->goodsig |= tattach->goodsig;
908 if (s->flags & M_DISPLAY) {
909 state_puts ("\n", s);
910 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
913 body_list_wipe(&tattach);
914 /* clear 'Invoking...' message, since there's no error */
915 mutt_message _("PGP message successfully decrypted.");
917 mutt_error _("Could not decrypt PGP message");
918 pgp_void_passphrase ();
923 mutt_unlink (tempfile);
928 /* ----------------------------------------------------------------------------
929 * Routines for sending PGP/MIME messages.
933 BODY *pgp_sign_message (BODY * a)
936 char buffer[LONG_STRING];
937 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
938 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
943 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
945 mutt_mktemp (sigfile);
946 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
950 mutt_mktemp (signedfile);
951 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
952 mutt_perror (signedfile);
958 mutt_write_mime_header (a, sfp);
960 mutt_write_mime_body (a, sfp);
963 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
964 -1, -1, -1, signedfile)) == -1) {
965 mutt_perror (_("Can't open PGP subprocess!"));
973 if (!pgp_use_gpg_agent ())
974 fputs (PgpPass, pgpin);
979 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
980 * recommended for future releases of PGP.
982 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
983 if (m_strcmp("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
984 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
985 else if (m_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
986 fputs ("-----END PGP SIGNATURE-----\n", fp);
989 empty = 0; /* got some output, so we're ok */
992 /* check for errors from PGP */
994 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
996 fputs (buffer, stdout);
999 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1004 unlink (signedfile);
1006 if (fclose (fp) != 0) {
1007 mutt_perror ("fclose");
1013 pgp_void_passphrase ();
1014 mutt_any_key_to_continue (NULL);
1019 return (NULL); /* fatal error while signing */
1023 t->type = TYPEMULTIPART;
1024 t->subtype = m_strdup("signed");
1025 t->encoding = ENC7BIT;
1027 t->disposition = DISPINLINE;
1029 parameter_set_boundary(&t->parameter);
1030 parameter_setval(&t->parameter, "protocol", "application/pgp-signature");
1031 parameter_setval(&t->parameter, "micalg", pgp_micalg (sigfile));
1036 t->parts->next = body_new();
1038 t->type = TYPEAPPLICATION;
1039 t->subtype = m_strdup("pgp-signature");
1040 t->filename = m_strdup(sigfile);
1042 t->disposition = DISPINLINE;
1043 t->encoding = ENC7BIT;
1044 t->unlink = 1; /* ok to remove this file after sending. */
1049 static short is_numerical_keyid (const char *s)
1051 /* or should we require the "0x"? */
1052 if (strncmp (s, "0x", 2) == 0)
1054 if (m_strlen(s) % 8)
1057 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1063 /* This routine attempts to find the keyids of the recipients of a message.
1064 * It returns NULL if any of the keys can not be found.
1066 char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
1068 char *keyID, *keylist = NULL, *t;
1069 size_t keylist_size = 0;
1070 size_t keylist_used = 0;
1071 address_t *tmp = NULL, *addr = NULL;
1072 address_t **last = &tmp;
1075 pgp_key_t k_info = NULL, key = NULL;
1077 const char *fqdn = mutt_fqdn (1);
1079 for (i = 0; i < 3; i++) {
1094 *last = address_list_dup (p);
1096 last = &((*last)->next);
1100 rfc822_qualify (tmp, fqdn);
1102 address_list_uniq(&tmp);
1104 for (p = tmp; p; p = p->next) {
1105 char buf[LONG_STRING];
1110 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1113 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1115 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1116 if (is_numerical_keyid (keyID)) {
1117 if (strncmp (keyID, "0x", 2) == 0)
1119 goto bypass_selection; /* you don't see this. */
1122 /* check for e-mail address */
1123 if ((t = strchr (keyID, '@')) &&
1124 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1126 rfc822_qualify (addr, fqdn);
1130 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1134 address_list_wipe(&tmp);
1135 address_list_wipe(&addr);
1141 pgp_invoke_getkeys (q);
1145 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1146 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1148 if ((key = pgp_ask_for_key (buf, q->mailbox,
1149 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1151 address_list_wipe(&tmp);
1152 address_list_wipe(&addr);
1159 keyID = pgp_keyid (key);
1162 keylist_size += m_strlen(keyID) + 4;
1163 p_realloc(&keylist, keylist_size);
1164 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1166 keylist_used = m_strlen(keylist);
1168 pgp_free_key (&key);
1169 address_list_wipe(&addr);
1172 address_list_wipe(&tmp);
1176 /* Warning: "a" is no longer freed in this routine, you need
1177 * to free it later. This is necessary for $fcc_attach. */
1179 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1181 char buf[LONG_STRING];
1182 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1183 char pgpinfile[_POSIX_PATH_MAX];
1184 FILE *pgpin, *pgperr, *fpout, *fptmp;
1190 mutt_mktemp (tempfile);
1191 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1192 mutt_perror (tempfile);
1196 mutt_mktemp (pgperrfile);
1197 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1198 mutt_perror (pgperrfile);
1203 unlink (pgperrfile);
1205 mutt_mktemp (pgpinfile);
1206 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1207 mutt_perror (pgpinfile);
1215 convert_to_7bit (a);
1217 mutt_write_mime_header (a, fptmp);
1218 fputc ('\n', fptmp);
1219 mutt_write_mime_body (a, fptmp);
1222 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1223 fileno (fpout), fileno (pgperr),
1224 pgpinfile, keylist, sign)) == -1) {
1231 if (!pgp_use_gpg_agent ())
1232 fputs (PgpPass, pgpin);
1233 fputc ('\n', pgpin);
1237 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1245 empty = (fgetc (fpout) == EOF);
1250 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1252 fputs (buf, stdout);
1256 /* pause if there is any error output from PGP */
1258 mutt_any_key_to_continue (NULL);
1261 /* fatal error while trying to encrypt message */
1263 pgp_void_passphrase (); /* just in case */
1269 t->type = TYPEMULTIPART;
1270 t->subtype = m_strdup("encrypted");
1271 t->encoding = ENC7BIT;
1273 t->disposition = DISPINLINE;
1275 parameter_set_boundary(&t->parameter);
1276 parameter_setval(&t->parameter, "protocol", "application/pgp-encrypted");
1278 t->parts = body_new();
1279 t->parts->type = TYPEAPPLICATION;
1280 t->parts->subtype = m_strdup("pgp-encrypted");
1281 t->parts->encoding = ENC7BIT;
1283 t->parts->next = body_new();
1284 t->parts->next->type = TYPEAPPLICATION;
1285 t->parts->next->subtype = m_strdup("octet-stream");
1286 t->parts->next->encoding = ENC7BIT;
1287 t->parts->next->filename = m_strdup(tempfile);
1288 t->parts->next->use_disp = 1;
1289 t->parts->next->disposition = DISPINLINE;
1290 t->parts->next->unlink = 1; /* delete after sending the message */
1291 t->parts->next->d_filename = m_strdup("msg.asc"); /* non pgp/mime can save */
1296 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1300 char pgpoutfile[_POSIX_PATH_MAX];
1301 char pgperrfile[_POSIX_PATH_MAX];
1302 char pgpinfile[_POSIX_PATH_MAX];
1304 char body_charset[STRING];
1306 const char *send_charset;
1308 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1318 if (a->type != TYPETEXT)
1320 if (ascii_strcasecmp (a->subtype, "plain"))
1323 if ((fp = fopen (a->filename, "r")) == NULL) {
1324 mutt_perror (a->filename);
1328 mutt_mktemp (pgpinfile);
1329 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1330 mutt_perror (pgpinfile);
1335 /* The following code is really correct: If noconv is set,
1336 * a's charset parameter contains the on-disk character set, and
1337 * we have to convert from that to utf-8. If noconv is not set,
1338 * we have to convert from $charset to utf-8.
1341 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1343 from_charset = body_charset;
1345 from_charset = Charset;
1347 if (!charset_is_us_ascii (body_charset)) {
1351 if (flags & ENCRYPT)
1352 send_charset = "us-ascii";
1354 send_charset = "utf-8";
1356 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1357 while ((c = fgetconv (fc)) != EOF)
1360 fgetconv_close (&fc);
1363 send_charset = "us-ascii";
1364 mutt_copy_stream (fp, pgpin);
1369 mutt_mktemp (pgpoutfile);
1370 mutt_mktemp (pgperrfile);
1371 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1372 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1373 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1377 unlink (pgpoutfile);
1382 unlink (pgperrfile);
1384 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1385 -1, fileno (pgpout), fileno (pgperr),
1386 pgpinfile, keylist, flags)) == -1) {
1387 mutt_perror (_("Can't invoke PGP"));
1391 mutt_unlink (pgpinfile);
1392 unlink (pgpoutfile);
1396 if (pgp_use_gpg_agent ())
1399 fprintf (pgpin, "%s\n", PgpPass);
1402 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1405 mutt_unlink (pgpinfile);
1414 empty = (fgetc (pgpout) == EOF);
1419 while (fgets (buff, sizeof (buff), pgperr)) {
1421 fputs (buff, stdout);
1427 mutt_any_key_to_continue (NULL);
1431 pgp_void_passphrase (); /* just in case */
1432 unlink (pgpoutfile);
1438 b->encoding = ENC7BIT;
1441 b->subtype = m_strdup("plain");
1443 parameter_setval(&b->parameter, "x-action",
1444 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed");
1445 parameter_setval(&b->parameter, "charset", send_charset);
1447 b->filename = m_strdup(pgpoutfile);
1449 b->disposition = DISPINLINE;
1455 if (!(flags & ENCRYPT))
1456 b->encoding = a->encoding;
1461 int pgp_send_menu (HEADER * msg, int *redraw)
1464 char input_signas[SHORT_STRING];
1466 char prompt[LONG_STRING];
1468 /* If autoinline and no crypto options set, then set inline. */
1469 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1470 && (msg->security & (SIGN | ENCRYPT))))
1471 msg->security |= INLINE;
1473 snprintf (prompt, sizeof (prompt),
1474 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1475 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1477 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1478 case 1: /* (e)ncrypt */
1479 msg->security |= ENCRYPT;
1480 msg->security &= ~SIGN;
1483 case 2: /* (s)ign */
1484 msg->security |= SIGN;
1485 msg->security &= ~ENCRYPT;
1488 case 3: /* sign (a)s */
1489 unset_option (OPTPGPCHECKTRUST);
1492 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1494 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1495 m_strreplace(&PgpSignAs, input_signas);
1498 msg->security |= SIGN;
1500 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1504 msg->security &= ~SIGN;
1508 *redraw = REDRAW_FULL;
1511 case 4: /* (b)oth */
1512 msg->security |= (ENCRYPT | SIGN);
1515 case 5: /* (i)nline */
1516 if ((msg->security & (ENCRYPT | SIGN)))
1517 msg->security ^= INLINE;
1519 msg->security &= ~INLINE;
1522 case 6: /* (f)orget it */
1523 case 7: /* (c)lear */
1528 if (msg->security) {
1529 if (!(msg->security & (ENCRYPT | SIGN)))
1532 msg->security |= APPLICATION_PGP;
1535 return (msg->security);