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/mem.h>
25 #include <lib-lib/ascii.h>
26 #include <lib-lib/str.h>
27 #include <lib-lib/macros.h>
28 #include <lib-lib/file.h>
30 #include <lib-mime/mime.h>
32 #include <lib-ui/curses.h>
33 #include <lib-ui/enter.h>
34 #include <lib-ui/menu.h>
55 #ifdef HAVE_SYS_TIME_H
56 # include <sys/time.h>
59 #ifdef HAVE_SYS_RESOURCE_H
60 # include <sys/resource.h>
66 char PgpPass[LONG_STRING];
67 time_t PgpExptime = 0; /* when does the cached passphrase expire? */
69 void pgp_void_passphrase (void)
71 p_clear(PgpPass, countof(PgpPass));
75 int pgp_valid_passphrase (void)
77 time_t now = time (NULL);
79 if (pgp_use_gpg_agent ()) {
81 return 1; /* handled by gpg-agent */
85 /* Use cached copy. */
88 pgp_void_passphrase ();
90 if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
91 sizeof (PgpPass), M_PASS) == 0) {
92 PgpExptime = time (NULL) + PgpTimeout;
101 void pgp_forget_passphrase (void)
103 pgp_void_passphrase ();
104 mutt_message _("PGP passphrase forgotten.");
107 int pgp_use_gpg_agent (void) {
110 if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
113 if ((tty = ttyname(0)))
114 setenv ("GPG_TTY", tty, 0);
119 char *pgp_keyid (pgp_key_t k)
121 if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
124 return _pgp_keyid (k);
127 char *_pgp_keyid (pgp_key_t k)
129 if (option (OPTPGPLONGIDS))
132 return (k->keyid + 8);
135 /* ----------------------------------------------------------------------------
136 * Routines for handing PGP input.
141 /* Copy PGP output messages and look for signs of a good signature */
143 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
147 if (PgpGoodSign.pattern) {
152 while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
153 if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
157 if (strncmp (line, "[GNUPG:] ", 9) == 0)
165 mutt_copy_stream (fpin, fpout);
173 * Copy a clearsigned message, and strip the signature and PGP's
176 * XXX - charset handling: We assume that it is safe to do
177 * character set decoding first, dash decoding second here, while
178 * we do it the other way around in the main handler.
180 * (Note that we aren't worse than Outlook &c in this, and also
181 * note that we can successfully handle anything produced by any
182 * existing versions of mutt.)
185 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
187 char buf[HUGE_STRING];
188 short complete, armor_header;
194 fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
196 for (complete = 1, armor_header = 1;
197 fgetconvs (buf, sizeof (buf), fc) != NULL;
198 complete = strchr (buf, '\n') != NULL) {
205 if (m_strcmp(buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
209 char *p = vskipspaces(buf);
217 state_puts (s->prefix, s);
219 if (buf[0] == '-' && buf[1] == ' ')
220 state_puts (buf + 2, s);
225 fgetconv_close (&fc);
229 /* Support for the Application/PGP Content Type. */
231 int pgp_application_pgp_handler (BODY * m, STATE * s)
233 int could_not_decrypt = 0;
234 int needpass = -1, pgp_keyblock = 0;
236 int clearsign = 0, rv, rc;
239 off_t last_pos, offset;
240 char buf[HUGE_STRING];
241 char outfile[_POSIX_PATH_MAX];
242 char tmpfname[_POSIX_PATH_MAX];
243 FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
247 short maybe_goodsig = 1;
248 short have_any_sigs = 0;
250 char body_charset[STRING];
252 mutt_get_body_charset (body_charset, sizeof (body_charset), m);
254 rc = 0; /* silence false compiler warning if (s->flags & M_DISPLAY) */
256 fseeko (s->fpin, m->offset, 0);
257 last_pos = m->offset;
259 for (bytes = m->length; bytes > 0;) {
260 if (fgets (buf, sizeof (buf), s->fpin) == NULL)
263 offset = ftello (s->fpin);
264 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
267 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
269 start_pos = last_pos;
271 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
273 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0) {
277 else if (!option (OPTDONTHANDLEPGPKEYS) &&
278 m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
283 /* XXX - we may wish to recode here */
285 state_puts (s->prefix, s);
290 have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
292 /* Copy PGP material to temporary file */
293 mutt_mktemp (tmpfname);
294 if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
295 mutt_perror (tmpfname);
300 while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
301 offset = ftello (s->fpin);
302 bytes -= (offset - last_pos); /* don't rely on m_strlen(buf) */
308 && m_strcmp("-----END PGP MESSAGE-----\n", buf) == 0)
310 && (m_strcmp("-----END PGP SIGNATURE-----\n", buf) == 0
311 || m_strcmp("-----END PGP PUBLIC KEY BLOCK-----\n",
316 /* leave tmpfp open in case we still need it - but flush it! */
320 /* Invoke PGP if needed */
321 if (!clearsign || (s->flags & M_VERIFY)) {
322 mutt_mktemp (outfile);
323 if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
324 mutt_perror (tmpfname);
328 if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
329 fileno (pgpout), -1, tmpfname,
331 safe_fclose (&pgpout);
336 ("[-- Error: unable to create PGP subprocess! --]\n"),
339 else { /* PGP started successfully */
342 if (!pgp_valid_passphrase ())
343 pgp_void_passphrase ();
344 if (pgp_use_gpg_agent ())
346 fprintf (pgpin, "%s\n", PgpPass);
349 safe_fclose (&pgpin);
351 if (s->flags & M_DISPLAY) {
352 crypt_current_time (s, "PGP");
353 rc = pgp_copy_checksig (pgperr, s->fpout);
356 safe_fclose (&pgperr);
357 rv = mutt_wait_filter (thepid);
359 if (s->flags & M_DISPLAY) {
365 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
366 * Sig _is_ correct if
367 * gpg_good_sign="" && pgp_decode_command returned 0
372 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
376 /* treat empty result as sign of failure */
377 /* TODO: maybe on failure mutt should include the original undecoded text. */
383 if (!clearsign && (!pgpout || c == EOF)) {
384 could_not_decrypt = 1;
385 pgp_void_passphrase ();
388 if (could_not_decrypt && !(s->flags & M_DISPLAY)) {
389 mutt_error _("Could not decrypt PGP message");
397 * Now, copy cleartext to the screen. NOTE - we expect that PGP
398 * outputs utf-8 cleartext. This may not always be true, but it
399 * seems to be a reasonable guess.
402 if (s->flags & M_DISPLAY) {
404 state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
405 else if (pgp_keyblock)
406 state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
408 state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
414 pgp_copy_clearsigned (tmpfp, s, body_charset);
420 state_set_prefix (s);
421 fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
422 while ((c = fgetconv (fc)) != EOF)
423 state_prefix_putc (c, s);
424 fgetconv_close (&fc);
427 if (s->flags & M_DISPLAY) {
428 state_putc ('\n', s);
430 state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
431 if (could_not_decrypt)
432 mutt_error _("Could not decrypt PGP message.");
434 mutt_message _("PGP message successfully decrypted.");
436 else if (pgp_keyblock)
437 state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
439 state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
444 /* XXX - we may wish to recode here */
446 state_puts (s->prefix, s);
454 m->goodsig = (maybe_goodsig && have_any_sigs);
457 safe_fclose (&tmpfp);
458 mutt_unlink (tmpfname);
461 safe_fclose (&pgpout);
462 mutt_unlink (outfile);
465 if (needpass == -1) {
467 ("[-- Error: could not find beginning of PGP message! --]\n\n"),
475 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
478 char tempfile[_POSIX_PATH_MAX];
479 char buf[HUGE_STRING];
486 if (b->type != TYPETEXT)
489 if (tagged_only && !b->tagged)
492 mutt_mktemp (tempfile);
493 if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
498 if ((tfp = fopen (tempfile, "r")) == NULL) {
503 while (fgets (buf, sizeof (buf), tfp)) {
504 if (m_strncmp("-----BEGIN PGP ", buf, 15) == 0) {
505 if (m_strcmp("MESSAGE-----\n", buf + 15) == 0)
507 else if (m_strcmp("SIGNED MESSAGE-----\n", buf + 15) == 0)
509 else if (m_strcmp("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
516 if (!enc && !sgn && !key)
519 /* fix the content type */
521 mutt_set_parameter ("format", "fixed", &b->parameter);
523 mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
525 mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
527 mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
532 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
537 for (; b; b = b->next) {
538 if (is_multipart (b))
539 rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
540 else if (b->type == TYPETEXT) {
541 if ((r = mutt_is_application_pgp (b)))
544 rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
555 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
557 char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
558 FILE *fp, *pgpout, *pgperr;
563 snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
565 if (!(fp = safe_fopen (sigfile, "w"))) {
566 mutt_perror (sigfile);
570 fseeko (s->fpin, sigbdy->offset, 0);
571 mutt_copy_bytes (s->fpin, fp, sigbdy->length);
574 mutt_mktemp (pgperrfile);
575 if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
576 mutt_perror (pgperrfile);
581 crypt_current_time (s, "PGP");
583 if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
584 -1, -1, fileno (pgperr),
585 tempfile, sigfile)) != -1) {
586 if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
590 safe_fclose (&pgpout);
594 if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
597 if ((rv = mutt_wait_filter (thepid)))
601 safe_fclose (&pgperr);
603 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
605 mutt_unlink (sigfile);
606 mutt_unlink (pgperrfile);
612 /* Extract pgp public keys from messages or attachments */
614 void pgp_extract_keys_from_messages (HEADER * h)
617 char tempfname[_POSIX_PATH_MAX];
621 mutt_parse_mime_message (Context, h);
622 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
626 mutt_mktemp (tempfname);
627 if (!(fpout = safe_fopen (tempfname, "w"))) {
628 mutt_perror (tempfname);
632 set_option (OPTDONTHANDLEPGPKEYS);
635 for (i = 0; i < Context->vcount; i++) {
636 if (Context->hdrs[Context->v2r[i]]->tagged) {
637 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
638 if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
639 && !pgp_valid_passphrase ()) {
643 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
644 M_CM_DECODE | M_CM_CHARCONV, 0);
649 mutt_parse_mime_message (Context, h);
650 if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
654 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
659 pgp_invoke_import (tempfname);
660 mutt_any_key_to_continue (NULL);
664 mutt_unlink (tempfname);
665 unset_option (OPTDONTHANDLEPGPKEYS);
669 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
673 char tempfname[_POSIX_PATH_MAX];
675 mutt_mktemp (tempfname);
676 if (!(tempfp = safe_fopen (tempfname, "w"))) {
677 mutt_perror (tempfname);
686 mutt_body_handler (top, &s);
690 pgp_invoke_import (tempfname);
691 mutt_any_key_to_continue (NULL);
693 mutt_unlink (tempfname);
696 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
699 mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
705 set_option (OPTDONTHANDLEPGPKEYS);
707 for (; top; top = top->next) {
708 if (!tag || top->tagged)
709 pgp_extract_keys_from_attachment (fp, top);
715 unset_option (OPTDONTHANDLEPGPKEYS);
718 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
720 char buf[LONG_STRING];
721 FILE *pgpin, *pgpout, *pgperr, *pgptmp;
725 char pgperrfile[_POSIX_PATH_MAX];
726 char pgptmpfile[_POSIX_PATH_MAX];
730 mutt_mktemp (pgperrfile);
731 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
732 mutt_perror (pgperrfile);
737 mutt_mktemp (pgptmpfile);
738 if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
739 mutt_perror (pgptmpfile);
744 /* Position the stream at the beginning of the body, and send the data to
745 * the temporary file.
748 fseeko (s->fpin, a->offset, 0);
749 mutt_copy_bytes (s->fpin, pgptmp, a->length);
752 if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
753 fileno (pgperr), pgptmpfile)) == -1) {
756 if (s->flags & M_DISPLAY)
758 ("[-- Error: could not create a PGP subprocess! --]\n\n"),
763 /* send the PGP passphrase to the subprocess. Never do this if the
764 agent is active, because this might lead to a passphrase send as
766 if (!pgp_use_gpg_agent ())
767 fputs (PgpPass, pgpin);
771 /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
772 * read_mime_header has a hard time parsing the message.
774 while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
776 if (len > 1 && buf[len - 2] == '\r')
777 strcpy (buf + len - 2, "\n"); /* __STRCPY_CHECKED__ */
782 rv = mutt_wait_filter (thepid);
783 mutt_unlink (pgptmpfile);
785 if (s->flags & M_DISPLAY) {
788 if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
792 state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
799 if (fgetc (fpout) == EOF) {
800 mutt_error (_("Decryption failed."));
801 pgp_void_passphrase ();
807 if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
809 * Need to set the length of this body part.
811 fstat (fileno (fpout), &info);
812 tattach->length = info.st_size - tattach->offset;
814 /* See if we need to recurse on this MIME part. */
816 mutt_parse_part (fpout, tattach);
822 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
824 char tempfile[_POSIX_PATH_MAX];
828 if (!mutt_is_multipart_encrypted (b))
831 if (!b->parts || !b->parts->next)
838 mutt_mktemp (tempfile);
839 if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
840 mutt_perror (tempfile);
845 *cur = pgp_decrypt_part (b, &s, *fpout, p);
855 int pgp_encrypted_handler (BODY * a, STATE * s)
857 char tempfile[_POSIX_PATH_MAX];
864 if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
865 ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
866 !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
867 ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
868 if (s->flags & M_DISPLAY)
869 state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
875 * Move forward to the application/pgp-encrypted body.
879 mutt_mktemp (tempfile);
880 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
881 if (s->flags & M_DISPLAY)
883 ("[-- Error: could not create temporary file! --]\n"),
888 if (s->flags & M_DISPLAY)
889 crypt_current_time (s, "PGP");
891 if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
892 if (s->flags & M_DISPLAY)
894 ("[-- The following data is PGP/MIME encrypted --]\n\n"),
899 rc = mutt_body_handler (tattach, s);
903 * if a multipart/signed is the _only_ sub-part of a
904 * multipart/encrypted, cache signature verification
909 if (mutt_is_multipart_signed (tattach) && !tattach->next)
910 p->goodsig |= tattach->goodsig;
912 if (s->flags & M_DISPLAY) {
913 state_puts ("\n", s);
914 state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
917 mutt_free_body (&tattach);
918 /* clear 'Invoking...' message, since there's no error */
919 mutt_message _("PGP message successfully decrypted.");
921 mutt_error _("Could not decrypt PGP message");
922 pgp_void_passphrase ();
927 mutt_unlink (tempfile);
932 /* ----------------------------------------------------------------------------
933 * Routines for sending PGP/MIME messages.
937 BODY *pgp_sign_message (BODY * a)
940 char buffer[LONG_STRING];
941 char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
942 FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
947 convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
949 mutt_mktemp (sigfile);
950 if ((fp = safe_fopen (sigfile, "w")) == NULL) {
954 mutt_mktemp (signedfile);
955 if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
956 mutt_perror (signedfile);
962 mutt_write_mime_header (a, sfp);
964 mutt_write_mime_body (a, sfp);
967 if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
968 -1, -1, -1, signedfile)) == -1) {
969 mutt_perror (_("Can't open PGP subprocess!"));
977 if (!pgp_use_gpg_agent ())
978 fputs (PgpPass, pgpin);
983 * Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
984 * recommended for future releases of PGP.
986 while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
987 if (m_strcmp("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
988 fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
989 else if (m_strcmp("-----END PGP MESSAGE-----\n", buffer) == 0)
990 fputs ("-----END PGP SIGNATURE-----\n", fp);
993 empty = 0; /* got some output, so we're ok */
996 /* check for errors from PGP */
998 while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
1000 fputs (buffer, stdout);
1003 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1008 unlink (signedfile);
1010 if (fclose (fp) != 0) {
1011 mutt_perror ("fclose");
1017 pgp_void_passphrase ();
1018 mutt_any_key_to_continue (NULL);
1023 return (NULL); /* fatal error while signing */
1026 t = mutt_new_body ();
1027 t->type = TYPEMULTIPART;
1028 t->subtype = m_strdup("signed");
1029 t->encoding = ENC7BIT;
1031 t->disposition = DISPINLINE;
1033 mutt_generate_boundary (&t->parameter);
1034 mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1035 mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1040 t->parts->next = mutt_new_body ();
1042 t->type = TYPEAPPLICATION;
1043 t->subtype = m_strdup("pgp-signature");
1044 t->filename = m_strdup(sigfile);
1046 t->disposition = DISPINLINE;
1047 t->encoding = ENC7BIT;
1048 t->unlink = 1; /* ok to remove this file after sending. */
1053 static short is_numerical_keyid (const char *s)
1055 /* or should we require the "0x"? */
1056 if (strncmp (s, "0x", 2) == 0)
1058 if (m_strlen(s) % 8)
1061 if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1067 /* This routine attempts to find the keyids of the recipients of a message.
1068 * It returns NULL if any of the keys can not be found.
1070 char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
1072 char *keyID, *keylist = NULL, *t;
1073 size_t keylist_size = 0;
1074 size_t keylist_used = 0;
1075 address_t *tmp = NULL, *addr = NULL;
1076 address_t **last = &tmp;
1079 pgp_key_t k_info = NULL, key = NULL;
1081 const char *fqdn = mutt_fqdn (1);
1083 for (i = 0; i < 3; i++) {
1098 *last = address_list_dup (p);
1100 last = &((*last)->next);
1104 rfc822_qualify (tmp, fqdn);
1106 tmp = mutt_remove_duplicates (tmp);
1108 for (p = tmp; p; p = p->next) {
1109 char buf[LONG_STRING];
1114 if ((keyID = mutt_crypt_hook (p)) != NULL) {
1117 snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1119 if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1120 if (is_numerical_keyid (keyID)) {
1121 if (strncmp (keyID, "0x", 2) == 0)
1123 goto bypass_selection; /* you don't see this. */
1126 /* check for e-mail address */
1127 if ((t = strchr (keyID, '@')) &&
1128 (addr = rfc822_parse_adrlist (NULL, keyID))) {
1130 rfc822_qualify (addr, fqdn);
1134 k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1138 address_list_wipe(&tmp);
1139 address_list_wipe(&addr);
1145 pgp_invoke_getkeys (q);
1149 pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1150 snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1152 if ((key = pgp_ask_for_key (buf, q->mailbox,
1153 KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1155 address_list_wipe(&tmp);
1156 address_list_wipe(&addr);
1163 keyID = pgp_keyid (key);
1166 keylist_size += m_strlen(keyID) + 4;
1167 p_realloc(&keylist, keylist_size);
1168 sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1170 keylist_used = m_strlen(keylist);
1172 pgp_free_key (&key);
1173 address_list_wipe(&addr);
1176 address_list_wipe(&tmp);
1180 /* Warning: "a" is no longer freed in this routine, you need
1181 * to free it later. This is necessary for $fcc_attach. */
1183 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1185 char buf[LONG_STRING];
1186 char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1187 char pgpinfile[_POSIX_PATH_MAX];
1188 FILE *pgpin, *pgperr, *fpout, *fptmp;
1194 mutt_mktemp (tempfile);
1195 if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1196 mutt_perror (tempfile);
1200 mutt_mktemp (pgperrfile);
1201 if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1202 mutt_perror (pgperrfile);
1207 unlink (pgperrfile);
1209 mutt_mktemp (pgpinfile);
1210 if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1211 mutt_perror (pgpinfile);
1219 convert_to_7bit (a);
1221 mutt_write_mime_header (a, fptmp);
1222 fputc ('\n', fptmp);
1223 mutt_write_mime_body (a, fptmp);
1226 if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1227 fileno (fpout), fileno (pgperr),
1228 pgpinfile, keylist, sign)) == -1) {
1235 if (!pgp_use_gpg_agent ())
1236 fputs (PgpPass, pgpin);
1237 fputc ('\n', pgpin);
1241 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1249 empty = (fgetc (fpout) == EOF);
1254 while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1256 fputs (buf, stdout);
1260 /* pause if there is any error output from PGP */
1262 mutt_any_key_to_continue (NULL);
1265 /* fatal error while trying to encrypt message */
1267 pgp_void_passphrase (); /* just in case */
1272 t = mutt_new_body ();
1273 t->type = TYPEMULTIPART;
1274 t->subtype = m_strdup("encrypted");
1275 t->encoding = ENC7BIT;
1277 t->disposition = DISPINLINE;
1279 mutt_generate_boundary (&t->parameter);
1280 mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1282 t->parts = mutt_new_body ();
1283 t->parts->type = TYPEAPPLICATION;
1284 t->parts->subtype = m_strdup("pgp-encrypted");
1285 t->parts->encoding = ENC7BIT;
1287 t->parts->next = mutt_new_body ();
1288 t->parts->next->type = TYPEAPPLICATION;
1289 t->parts->next->subtype = m_strdup("octet-stream");
1290 t->parts->next->encoding = ENC7BIT;
1291 t->parts->next->filename = m_strdup(tempfile);
1292 t->parts->next->use_disp = 1;
1293 t->parts->next->disposition = DISPINLINE;
1294 t->parts->next->unlink = 1; /* delete after sending the message */
1295 t->parts->next->d_filename = m_strdup("msg.asc"); /* non pgp/mime can save */
1300 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1304 char pgpoutfile[_POSIX_PATH_MAX];
1305 char pgperrfile[_POSIX_PATH_MAX];
1306 char pgpinfile[_POSIX_PATH_MAX];
1308 char body_charset[STRING];
1310 const char *send_charset;
1312 FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1322 if (a->type != TYPETEXT)
1324 if (ascii_strcasecmp (a->subtype, "plain"))
1327 if ((fp = fopen (a->filename, "r")) == NULL) {
1328 mutt_perror (a->filename);
1332 mutt_mktemp (pgpinfile);
1333 if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1334 mutt_perror (pgpinfile);
1339 /* The following code is really correct: If noconv is set,
1340 * a's charset parameter contains the on-disk character set, and
1341 * we have to convert from that to utf-8. If noconv is not set,
1342 * we have to convert from $charset to utf-8.
1345 mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1347 from_charset = body_charset;
1349 from_charset = Charset;
1351 if (!charset_is_us_ascii (body_charset)) {
1355 if (flags & ENCRYPT)
1356 send_charset = "us-ascii";
1358 send_charset = "utf-8";
1360 fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1361 while ((c = fgetconv (fc)) != EOF)
1364 fgetconv_close (&fc);
1367 send_charset = "us-ascii";
1368 mutt_copy_stream (fp, pgpin);
1373 mutt_mktemp (pgpoutfile);
1374 mutt_mktemp (pgperrfile);
1375 if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1376 (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1377 mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1381 unlink (pgpoutfile);
1386 unlink (pgperrfile);
1388 if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1389 -1, fileno (pgpout), fileno (pgperr),
1390 pgpinfile, keylist, flags)) == -1) {
1391 mutt_perror (_("Can't invoke PGP"));
1395 mutt_unlink (pgpinfile);
1396 unlink (pgpoutfile);
1400 if (pgp_use_gpg_agent ())
1403 fprintf (pgpin, "%s\n", PgpPass);
1406 if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1409 mutt_unlink (pgpinfile);
1418 empty = (fgetc (pgpout) == EOF);
1423 while (fgets (buff, sizeof (buff), pgperr)) {
1425 fputs (buff, stdout);
1431 mutt_any_key_to_continue (NULL);
1435 pgp_void_passphrase (); /* just in case */
1436 unlink (pgpoutfile);
1440 b = mutt_new_body ();
1442 b->encoding = ENC7BIT;
1445 b->subtype = m_strdup("plain");
1447 mutt_set_parameter ("x-action",
1448 flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1450 mutt_set_parameter ("charset", send_charset, &b->parameter);
1452 b->filename = m_strdup(pgpoutfile);
1455 /* The following is intended to give a clue to some completely brain-dead
1456 * "mail environments" which are typically used by large corporations.
1459 b->d_filename = m_strdup("msg.pgp");
1464 b->disposition = DISPINLINE;
1470 if (!(flags & ENCRYPT))
1471 b->encoding = a->encoding;
1476 int pgp_send_menu (HEADER * msg, int *redraw)
1479 char input_signas[SHORT_STRING];
1481 char prompt[LONG_STRING];
1483 /* If autoinline and no crypto options set, then set inline. */
1484 if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1485 && (msg->security & (SIGN | ENCRYPT))))
1486 msg->security |= INLINE;
1488 snprintf (prompt, sizeof (prompt),
1489 _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1490 (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1492 switch (mutt_multi_choice (prompt, _("esabifc"))) {
1493 case 1: /* (e)ncrypt */
1494 msg->security |= ENCRYPT;
1495 msg->security &= ~SIGN;
1498 case 2: /* (s)ign */
1499 msg->security |= SIGN;
1500 msg->security &= ~ENCRYPT;
1503 case 3: /* sign (a)s */
1504 unset_option (OPTPGPCHECKTRUST);
1507 pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1509 snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1510 m_strreplace(&PgpSignAs, input_signas);
1513 msg->security |= SIGN;
1515 crypt_pgp_void_passphrase (); /* probably need a different passphrase */
1519 msg->security &= ~SIGN;
1523 *redraw = REDRAW_FULL;
1526 case 4: /* (b)oth */
1527 msg->security |= (ENCRYPT | SIGN);
1530 case 5: /* (i)nline */
1531 if ((msg->security & (ENCRYPT | SIGN)))
1532 msg->security ^= INLINE;
1534 msg->security &= ~INLINE;
1537 case 6: /* (f)orget it */
1538 case 7: /* (c)lear */
1543 if (msg->security) {
1544 if (!(msg->security & (ENCRYPT | SIGN)))
1547 msg->security |= APPLICATION_PGP;
1550 return (msg->security);