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
20 #include <lib-lib/lib-lib.h>
22 #ifdef HAVE_SYS_RESOURCE_H
23 # include <sys/resource.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>
41 char PgpPass[LONG_STRING];
42 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
44 void pgp_void_passphrase (void)
46 p_clear(PgpPass, countof(PgpPass));
50 int pgp_valid_passphrase (void)
52 time_t now = time (NULL);
54 if (pgp_use_gpg_agent ()) {
56 return 1; /* handled by gpg-agent */
60 /* Use cached copy. */
63 pgp_void_passphrase ();
65 if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
66 sizeof (PgpPass), M_PASS) == 0) {
67 PgpExptime = time (NULL) + PgpTimeout;
76 void pgp_forget_passphrase (void)
78 pgp_void_passphrase ();
79 mutt_message _("PGP passphrase forgotten.");
82 int pgp_use_gpg_agent (void) {
85 if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
88 if ((tty = ttyname(0)))
89 setenv ("GPG_TTY", tty, 0);
94 char *pgp_keyid (pgp_key_t k)
96 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
99 return _pgp_keyid (k);
102 char *_pgp_keyid (pgp_key_t k)
104 if (option (OPTPGPLONGIDS))
107 return (k->keyid + 8);
110 /* ----------------------------------------------------------------------------
111 * Routines for handing PGP input.
116 /* Copy PGP output messages and look for signs of a good signature */
118 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
122 if (PgpGoodSign.pattern) {
127 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
128 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
132 if (strncmp (line, "[GNUPG:] ", 9) == 0)
140 mutt_copy_stream (fpin, fpout);
148 * Copy a clearsigned message, and strip the signature and PGP's
151 * XXX - charset handling: We assume that it is safe to do
152 * character set decoding first, dash decoding second here, while
153 * we do it the other way around in the main handler.
155 * (Note that we aren't worse than Outlook &c in this, and also
156 * note that we can successfully handle anything produced by any
157 * existing versions of mutt.)
160 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
162 char buf[HUGE_STRING];
163 short complete, armor_header;
169 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
171 for (complete = 1, armor_header = 1;
172 fgetconvs (buf, sizeof (buf), fc) != NULL;
173 complete = strchr (buf, '\n') != NULL) {
180 if (m_strcmp(buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
184 char *p = vskipspaces(buf);
192 state_puts (s->prefix, s);
194 if (buf[0] == '-' && buf[1] == ' ')
195 state_puts (buf + 2, s);
200 fgetconv_close (&fc);
204 /* Support for the Application/PGP Content Type. */
206 int pgp_application_pgp_handler (BODY * m, STATE * s)
208 int could_not_decrypt = 0;
209 int needpass = -1, pgp_keyblock = 0;
211 int clearsign = 0, rv, rc;
214 off_t last_pos, offset;
215 char buf[HUGE_STRING];
216 char outfile[_POSIX_PATH_MAX];
217 char tmpfname[_POSIX_PATH_MAX];
218 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
222 short maybe_goodsig = 1;
223 short have_any_sigs = 0;
225 char body_charset[STRING];
227 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
229 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
231 fseeko (s->fpin, m->offset, 0);
232 last_pos = m->offset;
234 for (bytes = m->length; bytes > 0;) {
235 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
238 offset = ftello (s->fpin);
239 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
242 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
244 start_pos = last_pos;
246 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
248 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0) {
252 else if (!option (OPTDONTHANDLEPGPKEYS) &&
253 m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
258 /* XXX - we may wish to recode here */
260 state_puts (s->prefix, s);
265 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
267 /* Copy PGP material to temporary file */
268 mutt_mktemp (tmpfname);
269 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
270 mutt_perror (tmpfname);
275 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
276 offset = ftello (s->fpin);
277 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
283 && m_strcmp("-----END PGP MESSAGE-----\n", buf) == 0)
285 && (m_strcmp("-----END PGP SIGNATURE-----\n", buf) == 0
286 || m_strcmp("-----END PGP PUBLIC KEY BLOCK-----\n",
291 /* leave tmpfp open in case we still need it - but flush it! */
295 /* Invoke PGP if needed */
296 if (!clearsign || (s->flags & M_VERIFY)) {
297 mutt_mktemp (outfile);
298 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
299 mutt_perror (tmpfname);
303 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
304 fileno (pgpout), -1, tmpfname,
306 safe_fclose (&pgpout);
311 ("[-- Error: unable to create PGP subprocess! --]\n"),
314 else { /* PGP started successfully */
317 if (!pgp_valid_passphrase ())
318 pgp_void_passphrase ();
319 if (pgp_use_gpg_agent ())
321 fprintf (pgpin, "%s\n", PgpPass);
324 safe_fclose (&pgpin);
326 if (s->flags & M_DISPLAY) {
327 crypt_current_time (s, "PGP");
328 rc = pgp_copy_checksig (pgperr, s->fpout);
331 safe_fclose (&pgperr);
332 rv = mutt_wait_filter (thepid);
334 if (s->flags & M_DISPLAY) {
340 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
341 * Sig _is_ correct if
342 * gpg_good_sign="" && pgp_decode_command returned 0
347 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
351 /* treat empty result as sign of failure */
352 /* TODO: maybe on failure mutt should include the original undecoded text. */
358 if (!clearsign && (!pgpout || c == EOF)) {
359 could_not_decrypt = 1;
360 pgp_void_passphrase ();
363 if (could_not_decrypt && !(s->flags & M_DISPLAY)) {
364 mutt_error _("Could not decrypt PGP message");
372 * Now, copy cleartext to the screen. NOTE - we expect that PGP
373 * outputs utf-8 cleartext. This may not always be true, but it
374 * seems to be a reasonable guess.
377 if (s->flags & M_DISPLAY) {
379 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
380 else if (pgp_keyblock)
381 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
383 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
389 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 if (could_not_decrypt)
407 mutt_error _("Could not decrypt PGP message.");
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);
429 m->goodsig = (maybe_goodsig && have_any_sigs);
432 safe_fclose (&tmpfp);
433 mutt_unlink (tmpfname);
436 safe_fclose (&pgpout);
437 mutt_unlink (outfile);
440 if (needpass == -1) {
442 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
450 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
453 char tempfile[_POSIX_PATH_MAX];
454 char buf[HUGE_STRING];
461 if (b->type != TYPETEXT)
464 if (tagged_only && !b->tagged)
467 mutt_mktemp (tempfile);
468 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
473 if ((tfp = fopen (tempfile, "r")) == NULL) {
478 while (fgets (buf, sizeof (buf), tfp)) {
479 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
480 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
482 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0)
484 else if (m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
491 if (!enc && !sgn && !key)
494 /* fix the content type */
496 parameter_setval(&b->parameter, "format", "fixed");
498 parameter_setval(&b->parameter, "x-action", "pgp-encrypted");
500 parameter_setval(&b->parameter, "x-action", "pgp-signed");
502 parameter_setval(&b->parameter, "x-action", "pgp-keys");
507 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
512 for (; b; b = b->next) {
513 if (is_multipart (b))
514 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
515 else if (b->type == TYPETEXT) {
516 if ((r = mutt_is_application_pgp (b)))
519 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
530 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
532 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
533 FILE *fp, *pgpout, *pgperr;
538 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
540 if (!(fp = safe_fopen (sigfile, "w"))) {
541 mutt_perror (sigfile);
545 fseeko (s->fpin, sigbdy->offset, 0);
546 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
549 mutt_mktemp (pgperrfile);
550 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
551 mutt_perror (pgperrfile);
556 crypt_current_time (s, "PGP");
558 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
559 -1, -1, fileno (pgperr),
560 tempfile, sigfile)) != -1) {
561 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
565 safe_fclose (&pgpout);
569 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
572 if ((rv = mutt_wait_filter (thepid)))
576 safe_fclose (&pgperr);
578 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
580 mutt_unlink (sigfile);
581 mutt_unlink (pgperrfile);
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);
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 fseeko (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) {
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) {
775 mutt_error (_("Decryption failed."));
776 pgp_void_passphrase ();
782 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
784 * Need to set the length of this body part.
786 fstat (fileno (fpout), &info);
787 tattach->length = info.st_size - tattach->offset;
789 /* See if we need to recurse on this MIME part. */
791 mutt_parse_part (fpout, tattach);
797 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
799 char tempfile[_POSIX_PATH_MAX];
803 if (!mutt_is_multipart_encrypted (b))
806 if (!b->parts || !b->parts->next)
813 mutt_mktemp (tempfile);
814 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
815 mutt_perror (tempfile);
820 *cur = pgp_decrypt_part (b, &s, *fpout, p);
830 int pgp_encrypted_handler (BODY * a, STATE * s)
832 char tempfile[_POSIX_PATH_MAX];
839 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
840 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
841 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
842 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
843 if (s->flags & M_DISPLAY)
844 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
850 * Move forward to the application/pgp-encrypted body.
854 mutt_mktemp (tempfile);
855 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
856 if (s->flags & M_DISPLAY)
858 ("[-- Error: could not create temporary file! --]\n"),
863 if (s->flags & M_DISPLAY)
864 crypt_current_time (s, "PGP");
866 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
867 if (s->flags & M_DISPLAY)
869 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
874 rc = mutt_body_handler (tattach, s);
878 * if a multipart/signed is the _only_ sub-part of a
879 * multipart/encrypted, cache signature verification
884 if (mutt_is_multipart_signed (tattach) && !tattach->next)
885 p->goodsig |= tattach->goodsig;
887 if (s->flags & M_DISPLAY) {
888 state_puts ("\n", s);
889 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
892 body_list_wipe(&tattach);
893 /* clear 'Invoking...' message, since there's no error */
894 mutt_message _("PGP message successfully decrypted.");
896 mutt_error _("Could not decrypt PGP message");
897 pgp_void_passphrase ();
902 mutt_unlink (tempfile);
907 /* ----------------------------------------------------------------------------
908 * Routines for sending PGP/MIME messages.
912 BODY *pgp_sign_message (BODY * a)
915 char buffer[LONG_STRING];
916 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
917 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
922 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
924 mutt_mktemp (sigfile);
925 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
929 mutt_mktemp (signedfile);
930 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
931 mutt_perror (signedfile);
937 mutt_write_mime_header (a, sfp);
939 mutt_write_mime_body (a, sfp);
942 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
943 -1, -1, -1, signedfile)) == -1) {
944 mutt_perror (_("Can't open PGP subprocess!"));
952 if (!pgp_use_gpg_agent ())
953 fputs (PgpPass, pgpin);
958 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
959 * recommended for future releases of PGP.
961 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
962 if (m_strcmp("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
963 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
964 else if (m_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
965 fputs ("-----END PGP SIGNATURE-----\n", fp);
968 empty = 0; /* got some output, so we're ok */
971 /* check for errors from PGP */
973 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
975 fputs (buffer, stdout);
978 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
985 if (fclose (fp) != 0) {
986 mutt_perror ("fclose");
992 pgp_void_passphrase ();
993 mutt_any_key_to_continue (NULL);
998 return (NULL); /* fatal error while signing */
1002 t->type = TYPEMULTIPART;
1003 t->subtype = m_strdup("signed");
1004 t->encoding = ENC7BIT;
1006 t->disposition = DISPINLINE;
1008 parameter_set_boundary(&t->parameter);
1009 parameter_setval(&t->parameter, "protocol", "application/pgp-signature");
1010 parameter_setval(&t->parameter, "micalg", pgp_micalg (sigfile));
1015 t->parts->next = body_new();
1017 t->type = TYPEAPPLICATION;
1018 t->subtype = m_strdup("pgp-signature");
1019 t->filename = m_strdup(sigfile);
1021 t->disposition = DISPINLINE;
1022 t->encoding = ENC7BIT;
1023 t->unlink = 1; /* ok to remove this file after sending. */
1028 static short is_numerical_keyid (const char *s)
1030 /* or should we require the "0x"? */
1031 if (strncmp (s, "0x", 2) == 0)
1033 if (m_strlen(s) % 8)
1036 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1042 /* This routine attempts to find the keyids of the recipients of a message.
1043 * It returns NULL if any of the keys can not be found.
1045 char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
1047 char *keyID, *keylist = NULL, *t;
1048 size_t keylist_size = 0;
1049 size_t keylist_used = 0;
1050 address_t *tmp = NULL, *addr = NULL;
1051 address_t **last = &tmp;
1054 pgp_key_t k_info = NULL, key = NULL;
1056 const char *fqdn = mutt_fqdn (1);
1058 for (i = 0; i < 3; i++) {
1073 *last = address_list_dup (p);
1075 last = &((*last)->next);
1079 rfc822_qualify (tmp, fqdn);
1081 address_list_uniq(&tmp);
1083 for (p = tmp; p; p = p->next) {
1084 char buf[LONG_STRING];
1089 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1092 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1094 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1095 if (is_numerical_keyid (keyID)) {
1096 if (strncmp (keyID, "0x", 2) == 0)
1098 goto bypass_selection; /* you don't see this. */
1101 /* check for e-mail address */
1102 if ((t = strchr (keyID, '@')) &&
1103 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1105 rfc822_qualify (addr, fqdn);
1109 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1113 address_list_wipe(&tmp);
1114 address_list_wipe(&addr);
1120 pgp_invoke_getkeys (q);
1124 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1125 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1127 if ((key = pgp_ask_for_key (buf, q->mailbox,
1128 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1130 address_list_wipe(&tmp);
1131 address_list_wipe(&addr);
1138 keyID = pgp_keyid (key);
1141 keylist_size += m_strlen(keyID) + 4;
1142 p_realloc(&keylist, keylist_size);
1143 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1145 keylist_used = m_strlen(keylist);
1147 pgp_free_key (&key);
1148 address_list_wipe(&addr);
1151 address_list_wipe(&tmp);
1155 /* Warning: "a" is no longer freed in this routine, you need
1156 * to free it later. This is necessary for $fcc_attach. */
1158 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1160 char buf[LONG_STRING];
1161 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1162 char pgpinfile[_POSIX_PATH_MAX];
1163 FILE *pgpin, *pgperr, *fpout, *fptmp;
1169 mutt_mktemp (tempfile);
1170 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1171 mutt_perror (tempfile);
1175 mutt_mktemp (pgperrfile);
1176 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1177 mutt_perror (pgperrfile);
1182 unlink (pgperrfile);
1184 mutt_mktemp (pgpinfile);
1185 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1186 mutt_perror (pgpinfile);
1194 convert_to_7bit (a);
1196 mutt_write_mime_header (a, fptmp);
1197 fputc ('\n', fptmp);
1198 mutt_write_mime_body (a, fptmp);
1201 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1202 fileno (fpout), fileno (pgperr),
1203 pgpinfile, keylist, sign)) == -1) {
1210 if (!pgp_use_gpg_agent ())
1211 fputs (PgpPass, pgpin);
1212 fputc ('\n', pgpin);
1216 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1224 empty = (fgetc (fpout) == EOF);
1229 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1231 fputs (buf, stdout);
1235 /* pause if there is any error output from PGP */
1237 mutt_any_key_to_continue (NULL);
1240 /* fatal error while trying to encrypt message */
1242 pgp_void_passphrase (); /* just in case */
1248 t->type = TYPEMULTIPART;
1249 t->subtype = m_strdup("encrypted");
1250 t->encoding = ENC7BIT;
1252 t->disposition = DISPINLINE;
1254 parameter_set_boundary(&t->parameter);
1255 parameter_setval(&t->parameter, "protocol", "application/pgp-encrypted");
1257 t->parts = body_new();
1258 t->parts->type = TYPEAPPLICATION;
1259 t->parts->subtype = m_strdup("pgp-encrypted");
1260 t->parts->encoding = ENC7BIT;
1262 t->parts->next = body_new();
1263 t->parts->next->type = TYPEAPPLICATION;
1264 t->parts->next->subtype = m_strdup("octet-stream");
1265 t->parts->next->encoding = ENC7BIT;
1266 t->parts->next->filename = m_strdup(tempfile);
1267 t->parts->next->use_disp = 1;
1268 t->parts->next->disposition = DISPINLINE;
1269 t->parts->next->unlink = 1; /* delete after sending the message */
1270 t->parts->next->d_filename = m_strdup("msg.asc"); /* non pgp/mime can save */
1275 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1279 char pgpoutfile[_POSIX_PATH_MAX];
1280 char pgperrfile[_POSIX_PATH_MAX];
1281 char pgpinfile[_POSIX_PATH_MAX];
1283 char body_charset[STRING];
1285 const char *send_charset;
1287 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1297 if (a->type != TYPETEXT)
1299 if (ascii_strcasecmp (a->subtype, "plain"))
1302 if ((fp = fopen (a->filename, "r")) == NULL) {
1303 mutt_perror (a->filename);
1307 mutt_mktemp (pgpinfile);
1308 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1309 mutt_perror (pgpinfile);
1314 /* The following code is really correct: If noconv is set,
1315 * a's charset parameter contains the on-disk character set, and
1316 * we have to convert from that to utf-8. If noconv is not set,
1317 * we have to convert from $charset to utf-8.
1320 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1322 from_charset = body_charset;
1324 from_charset = Charset;
1326 if (!charset_is_us_ascii (body_charset)) {
1330 if (flags & ENCRYPT)
1331 send_charset = "us-ascii";
1333 send_charset = "utf-8";
1335 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1336 while ((c = fgetconv (fc)) != EOF)
1339 fgetconv_close (&fc);
1342 send_charset = "us-ascii";
1343 mutt_copy_stream (fp, pgpin);
1348 mutt_mktemp (pgpoutfile);
1349 mutt_mktemp (pgperrfile);
1350 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1351 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1352 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1356 unlink (pgpoutfile);
1361 unlink (pgperrfile);
1363 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1364 -1, fileno (pgpout), fileno (pgperr),
1365 pgpinfile, keylist, flags)) == -1) {
1366 mutt_perror (_("Can't invoke PGP"));
1370 mutt_unlink (pgpinfile);
1371 unlink (pgpoutfile);
1375 if (pgp_use_gpg_agent ())
1378 fprintf (pgpin, "%s\n", PgpPass);
1381 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1384 mutt_unlink (pgpinfile);
1393 empty = (fgetc (pgpout) == EOF);
1398 while (fgets (buff, sizeof (buff), pgperr)) {
1400 fputs (buff, stdout);
1406 mutt_any_key_to_continue (NULL);
1410 pgp_void_passphrase (); /* just in case */
1411 unlink (pgpoutfile);
1417 b->encoding = ENC7BIT;
1420 b->subtype = m_strdup("plain");
1422 parameter_setval(&b->parameter, "x-action",
1423 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed");
1424 parameter_setval(&b->parameter, "charset", send_charset);
1426 b->filename = m_strdup(pgpoutfile);
1428 b->disposition = DISPINLINE;
1434 if (!(flags & ENCRYPT))
1435 b->encoding = a->encoding;
1440 int pgp_send_menu (HEADER * msg, int *redraw)
1443 char input_signas[SHORT_STRING];
1445 char prompt[LONG_STRING];
1447 /* If autoinline and no crypto options set, then set inline. */
1448 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1449 && (msg->security & (SIGN | ENCRYPT))))
1450 msg->security |= INLINE;
1452 snprintf (prompt, sizeof (prompt),
1453 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1454 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1456 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1457 case 1: /* (e)ncrypt */
1458 msg->security |= ENCRYPT;
1459 msg->security &= ~SIGN;
1462 case 2: /* (s)ign */
1463 msg->security |= SIGN;
1464 msg->security &= ~ENCRYPT;
1467 case 3: /* sign (a)s */
1468 unset_option (OPTPGPCHECKTRUST);
1471 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1473 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1474 m_strreplace(&PgpSignAs, input_signas);
1477 msg->security |= SIGN;
1479 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1483 msg->security &= ~SIGN;
1487 *redraw = REDRAW_FULL;
1490 case 4: /* (b)oth */
1491 msg->security |= (ENCRYPT | SIGN);
1494 case 5: /* (i)nline */
1495 if ((msg->security & (ENCRYPT | SIGN)))
1496 msg->security ^= INLINE;
1498 msg->security &= ~INLINE;
1501 case 6: /* (f)orget it */
1502 case 7: /* (c)lear */
1507 if (msg->security) {
1508 if (!(msg->security & (ENCRYPT | SIGN)))
1511 msg->security |= APPLICATION_PGP;
1514 return (msg->security);