Nico Golde:
[apps/madmutt.git] / pgp.c
1 /*
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
6  *
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.
10  */
11
12 /*
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
17  * a message.
18  */
19
20 #if HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include "mutt.h"
25 #include "enter.h"
26 #include "ascii.h"
27 #include "handler.h"
28 #include "mutt_curses.h"
29 #include "pgp.h"
30 #include "mime.h"
31 #include "copy.h"
32 #include "attach.h"
33
34 #include "lib/mem.h"
35 #include "lib/intl.h"
36 #include "lib/str.h"
37 #include "lib/debug.h"
38
39 #include <sys/wait.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <sys/stat.h>
44 #include <errno.h>
45 #include <ctype.h>
46
47 #ifdef HAVE_LOCALE_H
48 #include <locale.h>
49 #endif
50
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 #endif
54
55 #ifdef HAVE_SYS_RESOURCE_H
56 # include <sys/resource.h>
57 #endif
58
59 #ifdef CRYPT_BACKEND_CLASSIC_PGP
60
61 #include "mutt_crypt.h"
62 #include "mutt_menu.h"
63
64
65 char PgpPass[LONG_STRING];
66 time_t PgpExptime = 0;          /* when does the cached passphrase expire? */
67
68 void pgp_void_passphrase (void)
69 {
70   memset (PgpPass, 0, sizeof (PgpPass));
71   PgpExptime = 0;
72 }
73
74 int pgp_valid_passphrase (void)
75 {
76   time_t now = time (NULL);
77
78   if (pgp_use_gpg_agent ()) {
79     *PgpPass = 0;
80     return 1;                   /* handled by gpg-agent */
81   }
82
83   if (now < PgpExptime)
84     /* Use cached copy.  */
85     return 1;
86
87   pgp_void_passphrase ();
88
89   if (mutt_get_field_unbuffered (_("Enter PGP passphrase:"), PgpPass,
90                                  sizeof (PgpPass), M_PASS) == 0) {
91     PgpExptime = time (NULL) + PgpTimeout;
92     return (1);
93   }
94   else
95     PgpExptime = 0;
96
97   return 0;
98 }
99
100 void pgp_forget_passphrase (void)
101 {
102   pgp_void_passphrase ();
103   mutt_message _("PGP passphrase forgotten.");
104 }
105
106 int pgp_use_gpg_agent (void)
107 {
108   return option (OPTUSEGPGAGENT) && getenv ("GPG_TTY")
109     && getenv ("GPG_AGENT_INFO");
110 }
111
112 char *pgp_keyid (pgp_key_t k)
113 {
114   if ((k->flags & KEYFLAG_SUBKEY) && k->parent && option (OPTPGPIGNORESUB))
115     k = k->parent;
116
117   return _pgp_keyid (k);
118 }
119
120 char *_pgp_keyid (pgp_key_t k)
121 {
122   if (option (OPTPGPLONGIDS))
123     return k->keyid;
124   else
125     return (k->keyid + 8);
126 }
127
128 /* ----------------------------------------------------------------------------
129  * Routines for handing PGP input.
130  */
131
132
133
134 /* Copy PGP output messages and look for signs of a good signature */
135
136 static int pgp_copy_checksig (FILE * fpin, FILE * fpout)
137 {
138   int rv = -1;
139
140   if (PgpGoodSign.pattern) {
141     char *line = NULL;
142     int lineno = 0;
143     size_t linelen;
144
145     while ((line = mutt_read_line (line, &linelen, fpin, &lineno)) != NULL) {
146       if (regexec (PgpGoodSign.rx, line, 0, NULL, 0) == 0) {
147         debug_print (2, ("\"%s\" matches regexp.\n", line));
148         rv = 0;
149       }
150       else
151         debug_print (2, ("\"%s\" doesn't match regexp.\n", line));
152
153       if (strncmp (line, "[GNUPG:] ", 9) == 0)
154         continue;
155       fputs (line, fpout);
156       fputc ('\n', fpout);
157     }
158     mem_free (&line);
159   }
160   else {
161     debug_print (2, ("No pattern.\n"));
162     mutt_copy_stream (fpin, fpout);
163     rv = 1;
164   }
165
166   return rv;
167 }
168
169 /* 
170  * Copy a clearsigned message, and strip the signature and PGP's
171  * dash-escaping.
172  * 
173  * XXX - charset handling: We assume that it is safe to do
174  * character set decoding first, dash decoding second here, while
175  * we do it the other way around in the main handler.
176  * 
177  * (Note that we aren't worse than Outlook &c in this, and also
178  * note that we can successfully handle anything produced by any
179  * existing versions of mutt.) 
180  */
181
182 static void pgp_copy_clearsigned (FILE * fpin, STATE * s, char *charset)
183 {
184   char buf[HUGE_STRING];
185   short complete, armor_header;
186
187   FGETCONV *fc;
188
189   rewind (fpin);
190
191   fc = fgetconv_open (fpin, charset, Charset, M_ICONV_HOOK_FROM);
192
193   for (complete = 1, armor_header = 1;
194        fgetconvs (buf, sizeof (buf), fc) != NULL;
195        complete = strchr (buf, '\n') != NULL) {
196     if (!complete) {
197       if (!armor_header)
198         state_puts (buf, s);
199       continue;
200     }
201
202     if (str_cmp (buf, "-----BEGIN PGP SIGNATURE-----\n") == 0)
203       break;
204
205     if (armor_header) {
206       char *p = str_skip_initws (buf);
207
208       if (*p == '\0')
209         armor_header = 0;
210       continue;
211     }
212
213     if (s->prefix)
214       state_puts (s->prefix, s);
215
216     if (buf[0] == '-' && buf[1] == ' ')
217       state_puts (buf + 2, s);
218     else
219       state_puts (buf, s);
220   }
221
222   fgetconv_close (&fc);
223 }
224
225
226 /* Support for the Application/PGP Content Type. */
227
228 int pgp_application_pgp_handler (BODY * m, STATE * s)
229 {
230   int needpass = -1, pgp_keyblock = 0;
231   int c = 1;
232   int clearsign = 0, rv, rc;
233   long start_pos = 0;
234   long bytes, last_pos, offset;
235   char buf[HUGE_STRING];
236   char outfile[_POSIX_PATH_MAX];
237   char tmpfname[_POSIX_PATH_MAX];
238   FILE *pgpout = NULL, *pgpin = NULL, *pgperr = NULL;
239   FILE *tmpfp = NULL;
240   pid_t thepid;
241
242   short maybe_goodsig = 1;
243   short have_any_sigs = 0;
244
245   char body_charset[STRING];
246
247   mutt_get_body_charset (body_charset, sizeof (body_charset), m);
248
249   rc = 0;                       /* silence false compiler warning if (s->flags & M_DISPLAY) */
250
251   fseek (s->fpin, m->offset, 0);
252   last_pos = m->offset;
253
254   for (bytes = m->length; bytes > 0;) {
255     if (fgets (buf, sizeof (buf), s->fpin) == NULL)
256       break;
257
258     offset = ftell (s->fpin);
259     bytes -= (offset - last_pos);       /* don't rely on str_len(buf) */
260     last_pos = offset;
261
262     if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
263       clearsign = 0;
264       start_pos = last_pos;
265
266       if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
267         needpass = 1;
268       else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0) {
269         clearsign = 1;
270         needpass = 0;
271       }
272       else if (!option (OPTDONTHANDLEPGPKEYS) &&
273                str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0) {
274         needpass = 0;
275         pgp_keyblock = 1;
276       }
277       else {
278         /* XXX - we may wish to recode here */
279         if (s->prefix)
280           state_puts (s->prefix, s);
281         state_puts (buf, s);
282         continue;
283       }
284
285       have_any_sigs = have_any_sigs || (clearsign && (s->flags & M_VERIFY));
286
287       /* Copy PGP material to temporary file */
288       mutt_mktemp (tmpfname);
289       if ((tmpfp = safe_fopen (tmpfname, "w+")) == NULL) {
290         mutt_perror (tmpfname);
291         return (-1);
292       }
293
294       fputs (buf, tmpfp);
295       while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
296         offset = ftell (s->fpin);
297         bytes -= (offset - last_pos);   /* don't rely on str_len(buf) */
298         last_pos = offset;
299
300         fputs (buf, tmpfp);
301
302         if ((needpass
303              && str_cmp ("-----END PGP MESSAGE-----\n", buf) == 0)
304             || (!needpass
305                 && (str_cmp ("-----END PGP SIGNATURE-----\n", buf) == 0
306                     || str_cmp ("-----END PGP PUBLIC KEY BLOCK-----\n",
307                                     buf) == 0)))
308           break;
309       }
310
311       /* leave tmpfp open in case we still need it - but flush it! */
312       fflush (tmpfp);
313
314
315       /* Invoke PGP if needed */
316       if (!clearsign || (s->flags & M_VERIFY)) {
317         mutt_mktemp (outfile);
318         if ((pgpout = safe_fopen (outfile, "w+")) == NULL) {
319           mutt_perror (tmpfname);
320           return (-1);
321         }
322
323         if ((thepid = pgp_invoke_decode (&pgpin, NULL, &pgperr, -1,
324                                          fileno (pgpout), -1, tmpfname,
325                                          needpass)) == -1) {
326           safe_fclose (&pgpout);
327           maybe_goodsig = 0;
328           pgpin = NULL;
329           pgperr = NULL;
330           state_attach_puts (_
331                              ("[-- Error: unable to create PGP subprocess! --]\n"),
332                              s);
333         }
334         else {                  /* PGP started successfully */
335
336           if (needpass) {
337             if (!pgp_valid_passphrase ())
338               pgp_void_passphrase ();
339             if (pgp_use_gpg_agent ())
340               *PgpPass = 0;
341             fprintf (pgpin, "%s\n", PgpPass);
342           }
343
344           safe_fclose (&pgpin);
345
346           if (s->flags & M_DISPLAY) {
347             crypt_current_time (s, "PGP");
348             rc = pgp_copy_checksig (pgperr, s->fpout);
349           }
350
351           safe_fclose (&pgperr);
352           rv = mutt_wait_filter (thepid);
353
354           if (s->flags & M_DISPLAY) {
355             if (rc == 0)
356               have_any_sigs = 1;
357
358             /*
359              * Sig is bad if
360              * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
361              * Sig _is_ correct if
362              * gpg_good_sign="" && pgp_decode_command returned 0
363              */
364             if (rc == -1 || rv)
365               maybe_goodsig = 0;
366
367             state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
368           }
369         }
370
371         /* treat empty result as sign of failure */
372         if (pgpout) {
373           rewind (pgpout);
374           c = fgetc (pgpout);
375           ungetc (c, pgpout);
376         }
377         if (!clearsign && (!pgpout || c == EOF)) {
378             mutt_error _("Could not decrypt PGP message");
379             mutt_sleep (1);
380             pgp_void_passphrase ();
381             rc = -1;
382             goto out;
383         }
384       }
385
386       /*
387        * Now, copy cleartext to the screen.  NOTE - we expect that PGP
388        * outputs utf-8 cleartext.  This may not always be true, but it 
389        * seems to be a reasonable guess.
390        */
391
392       if (s->flags & M_DISPLAY) {
393         if (needpass)
394           state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
395         else if (pgp_keyblock)
396           state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
397         else
398           state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
399       }
400
401       if (clearsign) {
402         rewind (tmpfp);
403         if (tmpfp)
404           pgp_copy_clearsigned (tmpfp, s, body_charset);
405       }
406       else if (pgpout) {
407         FGETCONV *fc;
408         int c;
409
410         rewind (pgpout);
411         state_set_prefix (s);
412         fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
413         while ((c = fgetconv (fc)) != EOF)
414           state_prefix_putc (c, s);
415         fgetconv_close (&fc);
416       }
417
418       if (s->flags & M_DISPLAY) {
419         state_putc ('\n', s);
420         if (needpass) {
421           state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
422           mutt_message _("PGP message successfully decrypted.");
423         }
424         else if (pgp_keyblock)
425           state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
426         else
427           state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
428       }
429
430     }
431     else {
432       /* XXX - we may wish to recode here */
433       if (s->prefix)
434         state_puts (s->prefix, s);
435       state_puts (buf, s);
436     }
437   }
438
439   rc = 0;
440
441 out:
442   m->goodsig = (maybe_goodsig && have_any_sigs);
443
444   if (tmpfp) {
445     safe_fclose (&tmpfp);
446     mutt_unlink (tmpfname);
447   }
448   if (pgpout) {
449     safe_fclose (&pgpout);
450     mutt_unlink (outfile);
451   }
452
453   if (needpass == -1) {
454     state_attach_puts (_
455                        ("[-- Error: could not find beginning of PGP message! --]\n\n"),
456                        s);
457     return (-1);
458   }
459
460   return (rc);
461 }
462
463 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
464                                            int tagged_only)
465 {
466   char tempfile[_POSIX_PATH_MAX];
467   char buf[HUGE_STRING];
468   FILE *tfp;
469
470   short sgn = 0;
471   short enc = 0;
472   short key = 0;
473
474   if (b->type != TYPETEXT)
475     return 0;
476
477   if (tagged_only && !b->tagged)
478     return 0;
479
480   mutt_mktemp (tempfile);
481   if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
482     unlink (tempfile);
483     return 0;
484   }
485
486   if ((tfp = fopen (tempfile, "r")) == NULL) {
487     unlink (tempfile);
488     return 0;
489   }
490
491   while (fgets (buf, sizeof (buf), tfp)) {
492     if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
493       if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
494         enc = 1;
495       else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
496         sgn = 1;
497       else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
498         key = 1;
499     }
500   }
501   safe_fclose (&tfp);
502   unlink (tempfile);
503
504   if (!enc && !sgn && !key)
505     return 0;
506
507   /* fix the content type */
508
509   mutt_set_parameter ("format", "fixed", &b->parameter);
510   if (enc)
511     mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
512   else if (sgn)
513     mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
514   else if (key)
515     mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
516
517   return 1;
518 }
519
520 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
521 {
522   int rv = 0;
523   int r;
524
525   for (; b; b = b->next) {
526     if (is_multipart (b))
527       rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
528     else if (b->type == TYPETEXT) {
529       if ((r = mutt_is_application_pgp (b)))
530         rv = rv || r;
531       else
532         rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
533     }
534   }
535
536   return rv;
537 }
538
539
540
541
542
543 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
544 {
545   char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
546   FILE *fp, *pgpout, *pgperr;
547   pid_t thepid;
548   int badsig = -1;
549   int rv;
550
551   snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
552
553   if (!(fp = safe_fopen (sigfile, "w"))) {
554     mutt_perror (sigfile);
555     return -1;
556   }
557
558   fseek (s->fpin, sigbdy->offset, 0);
559   mutt_copy_bytes (s->fpin, fp, sigbdy->length);
560   fclose (fp);
561
562   mutt_mktemp (pgperrfile);
563   if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
564     mutt_perror (pgperrfile);
565     unlink (sigfile);
566     return -1;
567   }
568
569   crypt_current_time (s, "PGP");
570
571   if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
572                                    -1, -1, fileno (pgperr),
573                                    tempfile, sigfile)) != -1) {
574     if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
575       badsig = 0;
576
577
578     safe_fclose (&pgpout);
579     fflush (pgperr);
580     rewind (pgperr);
581
582     if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
583       badsig = 0;
584
585     if ((rv = mutt_wait_filter (thepid)))
586       badsig = -1;
587
588     debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
589   }
590
591   safe_fclose (&pgperr);
592
593   state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
594
595   mutt_unlink (sigfile);
596   mutt_unlink (pgperrfile);
597
598   debug_print (1, ("returning %d.\n", badsig));
599
600   return badsig;
601 }
602
603
604 /* Extract pgp public keys from messages or attachments */
605
606 void pgp_extract_keys_from_messages (HEADER * h)
607 {
608   int i;
609   char tempfname[_POSIX_PATH_MAX];
610   FILE *fpout;
611
612   if (h) {
613     mutt_parse_mime_message (Context, h);
614     if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
615       return;
616   }
617
618   mutt_mktemp (tempfname);
619   if (!(fpout = safe_fopen (tempfname, "w"))) {
620     mutt_perror (tempfname);
621     return;
622   }
623
624   set_option (OPTDONTHANDLEPGPKEYS);
625
626   if (!h) {
627     for (i = 0; i < Context->vcount; i++) {
628       if (Context->hdrs[Context->v2r[i]]->tagged) {
629         mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
630         if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
631             && !pgp_valid_passphrase ()) {
632           fclose (fpout);
633           goto bailout;
634         }
635         mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
636                            M_CM_DECODE | M_CM_CHARCONV, 0);
637       }
638     }
639   }
640   else {
641     mutt_parse_mime_message (Context, h);
642     if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
643       fclose (fpout);
644       goto bailout;
645     }
646     mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
647   }
648
649   fclose (fpout);
650   mutt_endwin (NULL);
651   pgp_invoke_import (tempfname);
652   mutt_any_key_to_continue (NULL);
653
654 bailout:
655
656   mutt_unlink (tempfname);
657   unset_option (OPTDONTHANDLEPGPKEYS);
658
659 }
660
661 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
662 {
663   STATE s;
664   FILE *tempfp;
665   char tempfname[_POSIX_PATH_MAX];
666
667   mutt_mktemp (tempfname);
668   if (!(tempfp = safe_fopen (tempfname, "w"))) {
669     mutt_perror (tempfname);
670     return;
671   }
672
673   memset (&s, 0, sizeof (STATE));
674
675   s.fpin = fp;
676   s.fpout = tempfp;
677
678   mutt_body_handler (top, &s);
679
680   fclose (tempfp);
681
682   pgp_invoke_import (tempfname);
683   mutt_any_key_to_continue (NULL);
684
685   mutt_unlink (tempfname);
686 }
687
688 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
689 {
690   if (!fp) {
691     mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
692
693     return;
694   }
695
696   mutt_endwin (NULL);
697   set_option (OPTDONTHANDLEPGPKEYS);
698
699   for (; top; top = top->next) {
700     if (!tag || top->tagged)
701       pgp_extract_keys_from_attachment (fp, top);
702
703     if (!tag)
704       break;
705   }
706
707   unset_option (OPTDONTHANDLEPGPKEYS);
708 }
709
710 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
711 {
712   char buf[LONG_STRING];
713   FILE *pgpin, *pgpout, *pgperr, *pgptmp;
714   struct stat info;
715   BODY *tattach;
716   int len;
717   char pgperrfile[_POSIX_PATH_MAX];
718   char pgptmpfile[_POSIX_PATH_MAX];
719   pid_t thepid;
720   int rv;
721
722   mutt_mktemp (pgperrfile);
723   if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
724     mutt_perror (pgperrfile);
725     return NULL;
726   }
727   unlink (pgperrfile);
728
729   mutt_mktemp (pgptmpfile);
730   if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
731     mutt_perror (pgptmpfile);
732     fclose (pgperr);
733     return NULL;
734   }
735
736   /* Position the stream at the beginning of the body, and send the data to
737    * the temporary file.
738    */
739
740   fseek (s->fpin, a->offset, 0);
741   mutt_copy_bytes (s->fpin, pgptmp, a->length);
742   fclose (pgptmp);
743
744   if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
745                                     fileno (pgperr), pgptmpfile)) == -1) {
746     fclose (pgperr);
747     unlink (pgptmpfile);
748     if (s->flags & M_DISPLAY)
749       state_attach_puts (_
750                          ("[-- Error: could not create a PGP subprocess! --]\n\n"),
751                          s);
752     return (NULL);
753   }
754
755   /* send the PGP passphrase to the subprocess.  Never do this if the
756      agent is active, because this might lead to a passphrase send as
757      the message. */
758   if (!pgp_use_gpg_agent ())
759     fputs (PgpPass, pgpin);
760   fputc ('\n', pgpin);
761   fclose (pgpin);
762
763   /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
764    * read_mime_header has a hard time parsing the message.
765    */
766   while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
767     len = str_len (buf);
768     if (len > 1 && buf[len - 2] == '\r')
769       strcpy (buf + len - 2, "\n");     /* __STRCPY_CHECKED__ */
770     fputs (buf, fpout);
771   }
772
773   fclose (pgpout);
774   rv = mutt_wait_filter (thepid);
775   mutt_unlink (pgptmpfile);
776
777   if (s->flags & M_DISPLAY) {
778     fflush (pgperr);
779     rewind (pgperr);
780     if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
781       p->goodsig = 1;
782     else
783       p->goodsig = 0;
784     state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
785   }
786   fclose (pgperr);
787
788   fflush (fpout);
789   rewind (fpout);
790
791   if (fgetc (fpout) == EOF) {
792     mutt_error (_("Decryption failed."));
793     pgp_void_passphrase ();
794     return NULL;
795   }
796
797   rewind (fpout);
798
799   if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
800     /*
801      * Need to set the length of this body part.
802      */
803     fstat (fileno (fpout), &info);
804     tattach->length = info.st_size - tattach->offset;
805
806     /* See if we need to recurse on this MIME part.  */
807
808     mutt_parse_part (fpout, tattach);
809   }
810
811   return (tattach);
812 }
813
814 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
815 {
816   char tempfile[_POSIX_PATH_MAX];
817   STATE s;
818   BODY *p = b;
819
820   if (!mutt_is_multipart_encrypted (b))
821     return -1;
822
823   if (!b->parts || !b->parts->next)
824     return -1;
825
826   b = b->parts->next;
827
828   memset (&s, 0, sizeof (s));
829   s.fpin = fpin;
830   mutt_mktemp (tempfile);
831   if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
832     mutt_perror (tempfile);
833     return (-1);
834   }
835   unlink (tempfile);
836
837   *cur = pgp_decrypt_part (b, &s, *fpout, p);
838
839   rewind (*fpout);
840
841   if (!*cur)
842     return -1;
843
844   return (0);
845 }
846
847 int pgp_encrypted_handler (BODY * a, STATE * s)
848 {
849   char tempfile[_POSIX_PATH_MAX];
850   FILE *fpout, *fpin;
851   BODY *tattach;
852   BODY *p = a;
853   int rc = 0;
854
855   a = a->parts;
856   if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
857       ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
858       !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
859       ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
860     if (s->flags & M_DISPLAY)
861       state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
862                          s);
863     return (-1);
864   }
865
866   /*
867    * Move forward to the application/pgp-encrypted body.
868    */
869   a = a->next;
870
871   mutt_mktemp (tempfile);
872   if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
873     if (s->flags & M_DISPLAY)
874       state_attach_puts (_
875                          ("[-- Error: could not create temporary file! --]\n"),
876                          s);
877     return (-1);
878   }
879
880   if (s->flags & M_DISPLAY)
881     crypt_current_time (s, "PGP");
882
883   if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
884     if (s->flags & M_DISPLAY)
885       state_attach_puts (_
886                          ("[-- The following data is PGP/MIME encrypted --]\n\n"),
887                          s);
888
889     fpin = s->fpin;
890     s->fpin = fpout;
891     rc = mutt_body_handler (tattach, s);
892     s->fpin = fpin;
893
894     /* 
895      * if a multipart/signed is the _only_ sub-part of a
896      * multipart/encrypted, cache signature verification
897      * status.
898      *
899      */
900
901     if (mutt_is_multipart_signed (tattach) && !tattach->next)
902       p->goodsig |= tattach->goodsig;
903
904     if (s->flags & M_DISPLAY) {
905       state_puts ("\n", s);
906       state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
907     }
908
909     mutt_free_body (&tattach);
910     /* clear 'Invoking...' message, since there's no error */
911     mutt_message _("PGP message successfully decrypted.");
912   } else {
913     mutt_error _("Could not decrypt PGP message");
914     pgp_void_passphrase ();
915     rc = -1;
916   }
917
918   fclose (fpout);
919   mutt_unlink (tempfile);
920
921   return (rc);
922 }
923
924 /* ----------------------------------------------------------------------------
925  * Routines for sending PGP/MIME messages.
926  */
927
928
929 BODY *pgp_sign_message (BODY * a)
930 {
931   BODY *t;
932   char buffer[LONG_STRING];
933   char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
934   FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
935   int err = 0;
936   int empty = 1;
937   pid_t thepid;
938
939   convert_to_7bit (a);          /* Signed data _must_ be in 7-bit format. */
940
941   mutt_mktemp (sigfile);
942   if ((fp = safe_fopen (sigfile, "w")) == NULL) {
943     return (NULL);
944   }
945
946   mutt_mktemp (signedfile);
947   if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
948     mutt_perror (signedfile);
949     fclose (fp);
950     unlink (sigfile);
951     return NULL;
952   }
953
954   mutt_write_mime_header (a, sfp);
955   fputc ('\n', sfp);
956   mutt_write_mime_body (a, sfp);
957   fclose (sfp);
958
959   if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
960                                  -1, -1, -1, signedfile)) == -1) {
961     mutt_perror (_("Can't open PGP subprocess!"));
962
963     fclose (fp);
964     unlink (sigfile);
965     unlink (signedfile);
966     return NULL;
967   }
968
969   if (!pgp_use_gpg_agent ())
970     fputs (PgpPass, pgpin);
971   fputc ('\n', pgpin);
972   fclose (pgpin);
973
974   /*
975    * Read back the PGP signature.  Also, change MESSAGE=>SIGNATURE as
976    * recommended for future releases of PGP.
977    */
978   while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
979     if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
980       fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
981     else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
982       fputs ("-----END PGP SIGNATURE-----\n", fp);
983     else
984       fputs (buffer, fp);
985     empty = 0;                  /* got some output, so we're ok */
986   }
987
988   /* check for errors from PGP */
989   err = 0;
990   while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
991     err = 1;
992     fputs (buffer, stdout);
993   }
994
995   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
996     empty = 1;
997
998   fclose (pgperr);
999   fclose (pgpout);
1000   unlink (signedfile);
1001
1002   if (fclose (fp) != 0) {
1003     mutt_perror ("fclose");
1004     unlink (sigfile);
1005     return (NULL);
1006   }
1007
1008   if (err) {
1009     pgp_void_passphrase ();
1010     mutt_any_key_to_continue (NULL);
1011   }
1012
1013   if (empty) {
1014     unlink (sigfile);
1015     return (NULL);              /* fatal error while signing */
1016   }
1017
1018   t = mutt_new_body ();
1019   t->type = TYPEMULTIPART;
1020   t->subtype = str_dup ("signed");
1021   t->encoding = ENC7BIT;
1022   t->use_disp = 0;
1023   t->disposition = DISPINLINE;
1024
1025   mutt_generate_boundary (&t->parameter);
1026   mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1027   mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1028
1029   t->parts = a;
1030   a = t;
1031
1032   t->parts->next = mutt_new_body ();
1033   t = t->parts->next;
1034   t->type = TYPEAPPLICATION;
1035   t->subtype = str_dup ("pgp-signature");
1036   t->filename = str_dup (sigfile);
1037   t->use_disp = 0;
1038   t->disposition = DISPINLINE;
1039   t->encoding = ENC7BIT;
1040   t->unlink = 1;                /* ok to remove this file after sending. */
1041
1042   return (a);
1043 }
1044
1045 static short is_numerical_keyid (const char *s)
1046 {
1047   /* or should we require the "0x"? */
1048   if (strncmp (s, "0x", 2) == 0)
1049     s += 2;
1050   if (str_len (s) % 8)
1051     return 0;
1052   while (*s)
1053     if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1054       return 0;
1055
1056   return 1;
1057 }
1058
1059 /* This routine attempts to find the keyids of the recipients of a message.
1060  * It returns NULL if any of the keys can not be found.
1061  */
1062 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1063 {
1064   char *keyID, *keylist = NULL, *t;
1065   size_t keylist_size = 0;
1066   size_t keylist_used = 0;
1067   ADDRESS *tmp = NULL, *addr = NULL;
1068   ADDRESS **last = &tmp;
1069   ADDRESS *p, *q;
1070   int i;
1071   pgp_key_t k_info = NULL, key = NULL;
1072
1073   const char *fqdn = mutt_fqdn (1);
1074
1075   for (i = 0; i < 3; i++) {
1076     switch (i) {
1077     case 0:
1078       p = to;
1079       break;
1080     case 1:
1081       p = cc;
1082       break;
1083     case 2:
1084       p = bcc;
1085       break;
1086     default:
1087       abort ();
1088     }
1089
1090     *last = rfc822_cpy_adr (p);
1091     while (*last)
1092       last = &((*last)->next);
1093   }
1094
1095   if (fqdn)
1096     rfc822_qualify (tmp, fqdn);
1097
1098   tmp = mutt_remove_duplicates (tmp);
1099
1100   for (p = tmp; p; p = p->next) {
1101     char buf[LONG_STRING];
1102
1103     q = p;
1104     k_info = NULL;
1105
1106     if ((keyID = mutt_crypt_hook (p)) != NULL) {
1107       int r;
1108
1109       snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1110                 p->mailbox);
1111       if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1112         if (is_numerical_keyid (keyID)) {
1113           if (strncmp (keyID, "0x", 2) == 0)
1114             keyID += 2;
1115           goto bypass_selection;        /* you don't see this. */
1116         }
1117
1118         /* check for e-mail address */
1119         if ((t = strchr (keyID, '@')) &&
1120             (addr = rfc822_parse_adrlist (NULL, keyID))) {
1121           if (fqdn)
1122             rfc822_qualify (addr, fqdn);
1123           q = addr;
1124         }
1125         else
1126           k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1127       }
1128       else if (r == -1) {
1129         mem_free (&keylist);
1130         rfc822_free_address (&tmp);
1131         rfc822_free_address (&addr);
1132         return NULL;
1133       }
1134     }
1135
1136     if (k_info == NULL)
1137       pgp_invoke_getkeys (q);
1138
1139     if (k_info == NULL
1140         && (k_info =
1141             pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1142       snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1143
1144       if ((key = pgp_ask_for_key (buf, q->mailbox,
1145                                   KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1146         mem_free (&keylist);
1147         rfc822_free_address (&tmp);
1148         rfc822_free_address (&addr);
1149         return NULL;
1150       }
1151     }
1152     else
1153       key = k_info;
1154
1155     keyID = pgp_keyid (key);
1156
1157   bypass_selection:
1158     keylist_size += str_len (keyID) + 4;
1159     mem_realloc (&keylist, keylist_size);
1160     sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1161              keyID);
1162     keylist_used = str_len (keylist);
1163
1164     pgp_free_key (&key);
1165     rfc822_free_address (&addr);
1166
1167   }
1168   rfc822_free_address (&tmp);
1169   return (keylist);
1170 }
1171
1172 /* Warning: "a" is no longer freed in this routine, you need
1173  * to free it later.  This is necessary for $fcc_attach. */
1174
1175 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1176 {
1177   char buf[LONG_STRING];
1178   char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1179   char pgpinfile[_POSIX_PATH_MAX];
1180   FILE *pgpin, *pgperr, *fpout, *fptmp;
1181   BODY *t;
1182   int err = 0;
1183   int empty = 0;
1184   pid_t thepid;
1185
1186   mutt_mktemp (tempfile);
1187   if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1188     mutt_perror (tempfile);
1189     return (NULL);
1190   }
1191
1192   mutt_mktemp (pgperrfile);
1193   if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1194     mutt_perror (pgperrfile);
1195     unlink (tempfile);
1196     fclose (fpout);
1197     return NULL;
1198   }
1199   unlink (pgperrfile);
1200
1201   mutt_mktemp (pgpinfile);
1202   if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1203     mutt_perror (pgpinfile);
1204     unlink (tempfile);
1205     fclose (fpout);
1206     fclose (pgperr);
1207     return NULL;
1208   }
1209
1210   if (sign)
1211     convert_to_7bit (a);
1212
1213   mutt_write_mime_header (a, fptmp);
1214   fputc ('\n', fptmp);
1215   mutt_write_mime_body (a, fptmp);
1216   fclose (fptmp);
1217
1218   if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1219                                     fileno (fpout), fileno (pgperr),
1220                                     pgpinfile, keylist, sign)) == -1) {
1221     fclose (pgperr);
1222     unlink (pgpinfile);
1223     return (NULL);
1224   }
1225
1226   if (sign) {
1227     if (!pgp_use_gpg_agent ())
1228       fputs (PgpPass, pgpin);
1229     fputc ('\n', pgpin);
1230   }
1231   fclose (pgpin);
1232
1233   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1234     empty = 1;
1235
1236   unlink (pgpinfile);
1237
1238   fflush (fpout);
1239   rewind (fpout);
1240   if (!empty)
1241     empty = (fgetc (fpout) == EOF);
1242   fclose (fpout);
1243
1244   fflush (pgperr);
1245   rewind (pgperr);
1246   while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1247     err = 1;
1248     fputs (buf, stdout);
1249   }
1250   fclose (pgperr);
1251
1252   /* pause if there is any error output from PGP */
1253   if (err)
1254     mutt_any_key_to_continue (NULL);
1255
1256   if (empty) {
1257     /* fatal error while trying to encrypt message */
1258     if (sign)
1259       pgp_void_passphrase (); /* just in case */
1260     unlink (tempfile);
1261     return (NULL);
1262   }
1263
1264   t = mutt_new_body ();
1265   t->type = TYPEMULTIPART;
1266   t->subtype = str_dup ("encrypted");
1267   t->encoding = ENC7BIT;
1268   t->use_disp = 0;
1269   t->disposition = DISPINLINE;
1270
1271   mutt_generate_boundary (&t->parameter);
1272   mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1273
1274   t->parts = mutt_new_body ();
1275   t->parts->type = TYPEAPPLICATION;
1276   t->parts->subtype = str_dup ("pgp-encrypted");
1277   t->parts->encoding = ENC7BIT;
1278
1279   t->parts->next = mutt_new_body ();
1280   t->parts->next->type = TYPEAPPLICATION;
1281   t->parts->next->subtype = str_dup ("octet-stream");
1282   t->parts->next->encoding = ENC7BIT;
1283   t->parts->next->filename = str_dup (tempfile);
1284   t->parts->next->use_disp = 1;
1285   t->parts->next->disposition = DISPINLINE;
1286   t->parts->next->unlink = 1;   /* delete after sending the message */
1287   t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1288
1289   return (t);
1290 }
1291
1292 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1293 {
1294   BODY *b;
1295
1296   char pgpoutfile[_POSIX_PATH_MAX];
1297   char pgperrfile[_POSIX_PATH_MAX];
1298   char pgpinfile[_POSIX_PATH_MAX];
1299
1300   char body_charset[STRING];
1301   char *from_charset;
1302   const char *send_charset;
1303
1304   FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1305   FILE *fp;
1306
1307   int empty = 0;
1308   int err;
1309
1310   char buff[STRING];
1311
1312   pid_t thepid;
1313
1314   if (a->type != TYPETEXT)
1315     return NULL;
1316   if (ascii_strcasecmp (a->subtype, "plain"))
1317     return NULL;
1318
1319   if ((fp = fopen (a->filename, "r")) == NULL) {
1320     mutt_perror (a->filename);
1321     return NULL;
1322   }
1323
1324   mutt_mktemp (pgpinfile);
1325   if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1326     mutt_perror (pgpinfile);
1327     fclose (fp);
1328     return NULL;
1329   }
1330
1331   /* The following code is really correct:  If noconv is set,
1332    * a's charset parameter contains the on-disk character set, and
1333    * we have to convert from that to utf-8.  If noconv is not set,
1334    * we have to convert from $charset to utf-8.
1335    */
1336
1337   mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1338   if (a->noconv)
1339     from_charset = body_charset;
1340   else
1341     from_charset = Charset;
1342
1343   if (!mutt_is_us_ascii (body_charset)) {
1344     int c;
1345     FGETCONV *fc;
1346
1347     if (flags & ENCRYPT)
1348       send_charset = "us-ascii";
1349     else
1350       send_charset = "utf-8";
1351
1352     fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1353     while ((c = fgetconv (fc)) != EOF)
1354       fputc (c, pgpin);
1355
1356     fgetconv_close (&fc);
1357   }
1358   else {
1359     send_charset = "us-ascii";
1360     mutt_copy_stream (fp, pgpin);
1361   }
1362   safe_fclose (&fp);
1363   fclose (pgpin);
1364
1365   mutt_mktemp (pgpoutfile);
1366   mutt_mktemp (pgperrfile);
1367   if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1368       (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1369     mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1370     unlink (pgpinfile);
1371     if (pgpout) {
1372       fclose (pgpout);
1373       unlink (pgpoutfile);
1374     }
1375     return NULL;
1376   }
1377
1378   unlink (pgperrfile);
1379
1380   if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1381                                         -1, fileno (pgpout), fileno (pgperr),
1382                                         pgpinfile, keylist, flags)) == -1) {
1383     mutt_perror (_("Can't invoke PGP"));
1384
1385     fclose (pgpout);
1386     fclose (pgperr);
1387     mutt_unlink (pgpinfile);
1388     unlink (pgpoutfile);
1389     return NULL;
1390   }
1391
1392   if (pgp_use_gpg_agent ())
1393     *PgpPass = 0;
1394   if (flags & SIGN)
1395     fprintf (pgpin, "%s\n", PgpPass);
1396   fclose (pgpin);
1397
1398   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1399     empty = 1;
1400
1401   mutt_unlink (pgpinfile);
1402
1403   fflush (pgpout);
1404   fflush (pgperr);
1405
1406   rewind (pgpout);
1407   rewind (pgperr);
1408
1409   if (!empty)
1410     empty = (fgetc (pgpout) == EOF);
1411   fclose (pgpout);
1412
1413   err = 0;
1414
1415   while (fgets (buff, sizeof (buff), pgperr)) {
1416     err = 1;
1417     fputs (buff, stdout);
1418   }
1419
1420   fclose (pgperr);
1421
1422   if (err)
1423     mutt_any_key_to_continue (NULL);
1424
1425   if (empty) {
1426     if (flags & SIGN)
1427       pgp_void_passphrase (); /* just in case */
1428     unlink (pgpoutfile);
1429     return NULL;
1430   }
1431
1432   b = mutt_new_body ();
1433
1434   b->encoding = ENC7BIT;
1435
1436   b->type = TYPETEXT;
1437   b->subtype = str_dup ("plain");
1438
1439   mutt_set_parameter ("x-action",
1440                       flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1441                       &b->parameter);
1442   mutt_set_parameter ("charset", send_charset, &b->parameter);
1443
1444   b->filename = str_dup (pgpoutfile);
1445
1446 #if 0
1447   /* The following is intended to give a clue to some completely brain-dead 
1448    * "mail environments" which are typically used by large corporations.
1449    */
1450
1451   b->d_filename = str_dup ("msg.pgp");
1452   b->use_disp = 1;
1453
1454 #endif
1455
1456   b->disposition = DISPINLINE;
1457   b->unlink = 1;
1458
1459   b->noconv = 1;
1460   b->use_disp = 0;
1461
1462   if (!(flags & ENCRYPT))
1463     b->encoding = a->encoding;
1464
1465   return b;
1466 }
1467
1468 int pgp_send_menu (HEADER * msg, int *redraw)
1469 {
1470   pgp_key_t p;
1471   char input_signas[SHORT_STRING];
1472
1473   char prompt[LONG_STRING];
1474
1475   if (!(WithCrypto & APPLICATION_PGP))
1476     return msg->security;
1477
1478   /* If autoinline and no crypto options set, then set inline. */
1479   if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1480                                      && (msg->security & (SIGN | ENCRYPT))))
1481     msg->security |= INLINE;
1482
1483   snprintf (prompt, sizeof (prompt),
1484             _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1485             (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1486
1487   switch (mutt_multi_choice (prompt, _("esabifc"))) {
1488   case 1:                      /* (e)ncrypt */
1489     msg->security |= ENCRYPT;
1490     msg->security &= ~SIGN;
1491     break;
1492
1493   case 2:                      /* (s)ign */
1494     msg->security |= SIGN;
1495     msg->security &= ~ENCRYPT;
1496     break;
1497
1498   case 3:                      /* sign (a)s */
1499     unset_option (OPTPGPCHECKTRUST);
1500
1501     if ((p =
1502          pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1503                           PGP_PUBRING))) {
1504       snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1505       str_replace (&PgpSignAs, input_signas);
1506       pgp_free_key (&p);
1507
1508       msg->security |= SIGN;
1509
1510       crypt_pgp_void_passphrase ();     /* probably need a different passphrase */
1511     }
1512 #if 0
1513     else {
1514       msg->security &= ~SIGN;
1515     }
1516 #endif
1517
1518     *redraw = REDRAW_FULL;
1519     break;
1520
1521   case 4:                      /* (b)oth */
1522     msg->security |= (ENCRYPT | SIGN);
1523     break;
1524
1525   case 5:                      /* (i)nline */
1526     if ((msg->security & (ENCRYPT | SIGN)))
1527       msg->security ^= INLINE;
1528     else
1529       msg->security &= ~INLINE;
1530     break;
1531
1532   case 6:                      /* (f)orget it */
1533   case 7:                      /* (c)lear     */
1534     msg->security = 0;
1535     break;
1536   }
1537
1538   if (msg->security) {
1539     if (!(msg->security & (ENCRYPT | SIGN)))
1540       msg->security = 0;
1541     else
1542       msg->security |= APPLICATION_PGP;
1543   }
1544
1545   return (msg->security);
1546 }
1547
1548
1549 #endif /* CRYPT_BACKEND_CLASSIC_PGP */