Rocco Rutte:
[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;
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 (!pgpout || c == EOF) {
378             mutt_error _("Could not decrypt PGP message");
379             pgp_void_passphrase ();
380             rc = -1;
381             goto out;
382         }
383       }
384
385       /*
386        * Now, copy cleartext to the screen.  NOTE - we expect that PGP
387        * outputs utf-8 cleartext.  This may not always be true, but it 
388        * seems to be a reasonable guess.
389        */
390
391       if (s->flags & M_DISPLAY) {
392         if (needpass)
393           state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
394         else if (pgp_keyblock)
395           state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"), s);
396         else
397           state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"), s);
398       }
399
400       if (clearsign) {
401         rewind (tmpfp);
402         if (tmpfp)
403           pgp_copy_clearsigned (tmpfp, s, body_charset);
404       }
405       else if (pgpout) {
406         FGETCONV *fc;
407         int c;
408
409         rewind (pgpout);
410         state_set_prefix (s);
411         fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
412         while ((c = fgetconv (fc)) != EOF)
413           state_prefix_putc (c, s);
414         fgetconv_close (&fc);
415       }
416
417       if (s->flags & M_DISPLAY) {
418         state_putc ('\n', s);
419         if (needpass) {
420           state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
421           mutt_message _("PGP message successfully decrypted.");
422         }
423         else if (pgp_keyblock)
424           state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
425         else
426           state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
427       }
428
429     }
430     else {
431       /* XXX - we may wish to recode here */
432       if (s->prefix)
433         state_puts (s->prefix, s);
434       state_puts (buf, s);
435     }
436   }
437
438   rc = 0;
439
440 out:
441   m->goodsig = (maybe_goodsig && have_any_sigs);
442
443   if (tmpfp) {
444     safe_fclose (&tmpfp);
445     mutt_unlink (tmpfname);
446   }
447   if (pgpout) {
448     safe_fclose (&pgpout);
449     mutt_unlink (outfile);
450   }
451
452   if (needpass == -1) {
453     state_attach_puts (_
454                        ("[-- Error: could not find beginning of PGP message! --]\n\n"),
455                        s);
456     return (-1);
457   }
458
459   return (rc);
460 }
461
462 static int pgp_check_traditional_one_body (FILE * fp, BODY * b,
463                                            int tagged_only)
464 {
465   char tempfile[_POSIX_PATH_MAX];
466   char buf[HUGE_STRING];
467   FILE *tfp;
468
469   short sgn = 0;
470   short enc = 0;
471   short key = 0;
472
473   if (b->type != TYPETEXT)
474     return 0;
475
476   if (tagged_only && !b->tagged)
477     return 0;
478
479   mutt_mktemp (tempfile);
480   if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0) {
481     unlink (tempfile);
482     return 0;
483   }
484
485   if ((tfp = fopen (tempfile, "r")) == NULL) {
486     unlink (tempfile);
487     return 0;
488   }
489
490   while (fgets (buf, sizeof (buf), tfp)) {
491     if (str_ncmp ("-----BEGIN PGP ", buf, 15) == 0) {
492       if (str_cmp ("MESSAGE-----\n", buf + 15) == 0)
493         enc = 1;
494       else if (str_cmp ("SIGNED MESSAGE-----\n", buf + 15) == 0)
495         sgn = 1;
496       else if (str_cmp ("PUBLIC KEY BLOCK-----\n", buf + 15) == 0)
497         key = 1;
498     }
499   }
500   safe_fclose (&tfp);
501   unlink (tempfile);
502
503   if (!enc && !sgn && !key)
504     return 0;
505
506   /* fix the content type */
507
508   mutt_set_parameter ("format", "fixed", &b->parameter);
509   if (enc)
510     mutt_set_parameter ("x-action", "pgp-encrypted", &b->parameter);
511   else if (sgn)
512     mutt_set_parameter ("x-action", "pgp-signed", &b->parameter);
513   else if (key)
514     mutt_set_parameter ("x-action", "pgp-keys", &b->parameter);
515
516   return 1;
517 }
518
519 int pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
520 {
521   int rv = 0;
522   int r;
523
524   for (; b; b = b->next) {
525     if (is_multipart (b))
526       rv = pgp_check_traditional (fp, b->parts, tagged_only) || rv;
527     else if (b->type == TYPETEXT) {
528       if ((r = mutt_is_application_pgp (b)))
529         rv = rv || r;
530       else
531         rv = pgp_check_traditional_one_body (fp, b, tagged_only) || rv;
532     }
533   }
534
535   return rv;
536 }
537
538
539
540
541
542 int pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempfile)
543 {
544   char sigfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
545   FILE *fp, *pgpout, *pgperr;
546   pid_t thepid;
547   int badsig = -1;
548   int rv;
549
550   snprintf (sigfile, sizeof (sigfile), "%s.asc", tempfile);
551
552   if (!(fp = safe_fopen (sigfile, "w"))) {
553     mutt_perror (sigfile);
554     return -1;
555   }
556
557   fseek (s->fpin, sigbdy->offset, 0);
558   mutt_copy_bytes (s->fpin, fp, sigbdy->length);
559   fclose (fp);
560
561   mutt_mktemp (pgperrfile);
562   if (!(pgperr = safe_fopen (pgperrfile, "w+"))) {
563     mutt_perror (pgperrfile);
564     unlink (sigfile);
565     return -1;
566   }
567
568   crypt_current_time (s, "PGP");
569
570   if ((thepid = pgp_invoke_verify (NULL, &pgpout, NULL,
571                                    -1, -1, fileno (pgperr),
572                                    tempfile, sigfile)) != -1) {
573     if (pgp_copy_checksig (pgpout, s->fpout) >= 0)
574       badsig = 0;
575
576
577     safe_fclose (&pgpout);
578     fflush (pgperr);
579     rewind (pgperr);
580
581     if (pgp_copy_checksig (pgperr, s->fpout) >= 0)
582       badsig = 0;
583
584     if ((rv = mutt_wait_filter (thepid)))
585       badsig = -1;
586
587     debug_print (1, ("mutt_wait_filter returned %d.\n", rv));
588   }
589
590   safe_fclose (&pgperr);
591
592   state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
593
594   mutt_unlink (sigfile);
595   mutt_unlink (pgperrfile);
596
597   debug_print (1, ("returning %d.\n", badsig));
598
599   return badsig;
600 }
601
602
603 /* Extract pgp public keys from messages or attachments */
604
605 void pgp_extract_keys_from_messages (HEADER * h)
606 {
607   int i;
608   char tempfname[_POSIX_PATH_MAX];
609   FILE *fpout;
610
611   if (h) {
612     mutt_parse_mime_message (Context, h);
613     if (h->security & PGPENCRYPT && !pgp_valid_passphrase ())
614       return;
615   }
616
617   mutt_mktemp (tempfname);
618   if (!(fpout = safe_fopen (tempfname, "w"))) {
619     mutt_perror (tempfname);
620     return;
621   }
622
623   set_option (OPTDONTHANDLEPGPKEYS);
624
625   if (!h) {
626     for (i = 0; i < Context->vcount; i++) {
627       if (Context->hdrs[Context->v2r[i]]->tagged) {
628         mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
629         if (Context->hdrs[Context->v2r[i]]->security & PGPENCRYPT
630             && !pgp_valid_passphrase ()) {
631           fclose (fpout);
632           goto bailout;
633         }
634         mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
635                            M_CM_DECODE | M_CM_CHARCONV, 0);
636       }
637     }
638   }
639   else {
640     mutt_parse_mime_message (Context, h);
641     if (h->security & PGPENCRYPT && !pgp_valid_passphrase ()) {
642       fclose (fpout);
643       goto bailout;
644     }
645     mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
646   }
647
648   fclose (fpout);
649   mutt_endwin (NULL);
650   pgp_invoke_import (tempfname);
651   mutt_any_key_to_continue (NULL);
652
653 bailout:
654
655   mutt_unlink (tempfname);
656   unset_option (OPTDONTHANDLEPGPKEYS);
657
658 }
659
660 static void pgp_extract_keys_from_attachment (FILE * fp, BODY * top)
661 {
662   STATE s;
663   FILE *tempfp;
664   char tempfname[_POSIX_PATH_MAX];
665
666   mutt_mktemp (tempfname);
667   if (!(tempfp = safe_fopen (tempfname, "w"))) {
668     mutt_perror (tempfname);
669     return;
670   }
671
672   memset (&s, 0, sizeof (STATE));
673
674   s.fpin = fp;
675   s.fpout = tempfp;
676
677   mutt_body_handler (top, &s);
678
679   fclose (tempfp);
680
681   pgp_invoke_import (tempfname);
682   mutt_any_key_to_continue (NULL);
683
684   mutt_unlink (tempfname);
685 }
686
687 void pgp_extract_keys_from_attachment_list (FILE * fp, int tag, BODY * top)
688 {
689   if (!fp) {
690     mutt_error _("Internal error. Inform <roessler@does-not-exist.org>.");
691
692     return;
693   }
694
695   mutt_endwin (NULL);
696   set_option (OPTDONTHANDLEPGPKEYS);
697
698   for (; top; top = top->next) {
699     if (!tag || top->tagged)
700       pgp_extract_keys_from_attachment (fp, top);
701
702     if (!tag)
703       break;
704   }
705
706   unset_option (OPTDONTHANDLEPGPKEYS);
707 }
708
709 BODY *pgp_decrypt_part (BODY * a, STATE * s, FILE * fpout, BODY * p)
710 {
711   char buf[LONG_STRING];
712   FILE *pgpin, *pgpout, *pgperr, *pgptmp;
713   struct stat info;
714   BODY *tattach;
715   int len;
716   char pgperrfile[_POSIX_PATH_MAX];
717   char pgptmpfile[_POSIX_PATH_MAX];
718   pid_t thepid;
719   int rv;
720
721   mutt_mktemp (pgperrfile);
722   if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
723     mutt_perror (pgperrfile);
724     return NULL;
725   }
726   unlink (pgperrfile);
727
728   mutt_mktemp (pgptmpfile);
729   if ((pgptmp = safe_fopen (pgptmpfile, "w")) == NULL) {
730     mutt_perror (pgptmpfile);
731     fclose (pgperr);
732     return NULL;
733   }
734
735   /* Position the stream at the beginning of the body, and send the data to
736    * the temporary file.
737    */
738
739   fseek (s->fpin, a->offset, 0);
740   mutt_copy_bytes (s->fpin, pgptmp, a->length);
741   fclose (pgptmp);
742
743   if ((thepid = pgp_invoke_decrypt (&pgpin, &pgpout, NULL, -1, -1,
744                                     fileno (pgperr), pgptmpfile)) == -1) {
745     fclose (pgperr);
746     unlink (pgptmpfile);
747     if (s->flags & M_DISPLAY)
748       state_attach_puts (_
749                          ("[-- Error: could not create a PGP subprocess! --]\n\n"),
750                          s);
751     return (NULL);
752   }
753
754   /* send the PGP passphrase to the subprocess.  Never do this if the
755      agent is active, because this might lead to a passphrase send as
756      the message. */
757   if (!pgp_use_gpg_agent ())
758     fputs (PgpPass, pgpin);
759   fputc ('\n', pgpin);
760   fclose (pgpin);
761
762   /* Read the output from PGP, and make sure to change CRLF to LF, otherwise
763    * read_mime_header has a hard time parsing the message.
764    */
765   while (fgets (buf, sizeof (buf) - 1, pgpout) != NULL) {
766     len = str_len (buf);
767     if (len > 1 && buf[len - 2] == '\r')
768       strcpy (buf + len - 2, "\n");     /* __STRCPY_CHECKED__ */
769     fputs (buf, fpout);
770   }
771
772   fclose (pgpout);
773   rv = mutt_wait_filter (thepid);
774   mutt_unlink (pgptmpfile);
775
776   if (s->flags & M_DISPLAY) {
777     fflush (pgperr);
778     rewind (pgperr);
779     if (pgp_copy_checksig (pgperr, s->fpout) == 0 && !rv && p)
780       p->goodsig = 1;
781     else
782       p->goodsig = 0;
783     state_attach_puts (_("[-- End of PGP output --]\n\n"), s);
784   }
785   fclose (pgperr);
786
787   fflush (fpout);
788   rewind (fpout);
789
790   if (fgetc (fpout) == EOF) {
791     mutt_error (_("Decryption failed."));
792     pgp_void_passphrase ();
793     return NULL;
794   }
795
796   rewind (fpout);
797
798   if ((tattach = mutt_read_mime_header (fpout, 0)) != NULL) {
799     /*
800      * Need to set the length of this body part.
801      */
802     fstat (fileno (fpout), &info);
803     tattach->length = info.st_size - tattach->offset;
804
805     /* See if we need to recurse on this MIME part.  */
806
807     mutt_parse_part (fpout, tattach);
808   }
809
810   return (tattach);
811 }
812
813 int pgp_decrypt_mime (FILE * fpin, FILE ** fpout, BODY * b, BODY ** cur)
814 {
815   char tempfile[_POSIX_PATH_MAX];
816   STATE s;
817   BODY *p = b;
818
819   if (!mutt_is_multipart_encrypted (b))
820     return -1;
821
822   if (!b->parts || !b->parts->next)
823     return -1;
824
825   b = b->parts->next;
826
827   memset (&s, 0, sizeof (s));
828   s.fpin = fpin;
829   mutt_mktemp (tempfile);
830   if ((*fpout = safe_fopen (tempfile, "w+")) == NULL) {
831     mutt_perror (tempfile);
832     return (-1);
833   }
834   unlink (tempfile);
835
836   *cur = pgp_decrypt_part (b, &s, *fpout, p);
837
838   rewind (*fpout);
839
840   if (!*cur)
841     return -1;
842
843   return (0);
844 }
845
846 int pgp_encrypted_handler (BODY * a, STATE * s)
847 {
848   char tempfile[_POSIX_PATH_MAX];
849   FILE *fpout, *fpin;
850   BODY *tattach;
851   BODY *p = a;
852   int rc = 0;
853
854   a = a->parts;
855   if (!a || a->type != TYPEAPPLICATION || !a->subtype ||
856       ascii_strcasecmp ("pgp-encrypted", a->subtype) != 0 ||
857       !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype ||
858       ascii_strcasecmp ("octet-stream", a->next->subtype) != 0) {
859     if (s->flags & M_DISPLAY)
860       state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
861                          s);
862     return (-1);
863   }
864
865   /*
866    * Move forward to the application/pgp-encrypted body.
867    */
868   a = a->next;
869
870   mutt_mktemp (tempfile);
871   if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
872     if (s->flags & M_DISPLAY)
873       state_attach_puts (_
874                          ("[-- Error: could not create temporary file! --]\n"),
875                          s);
876     return (-1);
877   }
878
879   if (s->flags & M_DISPLAY)
880     crypt_current_time (s, "PGP");
881
882   if ((tattach = pgp_decrypt_part (a, s, fpout, p)) != NULL) {
883     if (s->flags & M_DISPLAY)
884       state_attach_puts (_
885                          ("[-- The following data is PGP/MIME encrypted --]\n\n"),
886                          s);
887
888     fpin = s->fpin;
889     s->fpin = fpout;
890     rc = mutt_body_handler (tattach, s);
891     s->fpin = fpin;
892
893     /* 
894      * if a multipart/signed is the _only_ sub-part of a
895      * multipart/encrypted, cache signature verification
896      * status.
897      *
898      */
899
900     if (mutt_is_multipart_signed (tattach) && !tattach->next)
901       p->goodsig |= tattach->goodsig;
902
903     if (s->flags & M_DISPLAY) {
904       state_puts ("\n", s);
905       state_attach_puts (_("[-- End of PGP/MIME encrypted data --]\n"), s);
906     }
907
908     mutt_free_body (&tattach);
909     /* clear 'Invoking...' message, since there's no error */
910     mutt_message _("PGP message successfully decrypted.");
911   } else {
912     mutt_error _("Could not decrypt PGP message");
913     pgp_void_passphrase ();
914     rc = -1;
915   }
916
917   fclose (fpout);
918   mutt_unlink (tempfile);
919
920   return (rc);
921 }
922
923 /* ----------------------------------------------------------------------------
924  * Routines for sending PGP/MIME messages.
925  */
926
927
928 BODY *pgp_sign_message (BODY * a)
929 {
930   BODY *t;
931   char buffer[LONG_STRING];
932   char sigfile[_POSIX_PATH_MAX], signedfile[_POSIX_PATH_MAX];
933   FILE *pgpin, *pgpout, *pgperr, *fp, *sfp;
934   int err = 0;
935   int empty = 1;
936   pid_t thepid;
937
938   convert_to_7bit (a);          /* Signed data _must_ be in 7-bit format. */
939
940   mutt_mktemp (sigfile);
941   if ((fp = safe_fopen (sigfile, "w")) == NULL) {
942     return (NULL);
943   }
944
945   mutt_mktemp (signedfile);
946   if ((sfp = safe_fopen (signedfile, "w")) == NULL) {
947     mutt_perror (signedfile);
948     fclose (fp);
949     unlink (sigfile);
950     return NULL;
951   }
952
953   mutt_write_mime_header (a, sfp);
954   fputc ('\n', sfp);
955   mutt_write_mime_body (a, sfp);
956   fclose (sfp);
957
958   if ((thepid = pgp_invoke_sign (&pgpin, &pgpout, &pgperr,
959                                  -1, -1, -1, signedfile)) == -1) {
960     mutt_perror (_("Can't open PGP subprocess!"));
961
962     fclose (fp);
963     unlink (sigfile);
964     unlink (signedfile);
965     return NULL;
966   }
967
968   if (!pgp_use_gpg_agent ())
969     fputs (PgpPass, pgpin);
970   fputc ('\n', pgpin);
971   fclose (pgpin);
972
973   /*
974    * Read back the PGP signature.  Also, change MESSAGE=>SIGNATURE as
975    * recommended for future releases of PGP.
976    */
977   while (fgets (buffer, sizeof (buffer) - 1, pgpout) != NULL) {
978     if (str_cmp ("-----BEGIN PGP MESSAGE-----\n", buffer) == 0)
979       fputs ("-----BEGIN PGP SIGNATURE-----\n", fp);
980     else if (str_cmp ("-----END PGP MESSAGE-----\n", buffer) == 0)
981       fputs ("-----END PGP SIGNATURE-----\n", fp);
982     else
983       fputs (buffer, fp);
984     empty = 0;                  /* got some output, so we're ok */
985   }
986
987   /* check for errors from PGP */
988   err = 0;
989   while (fgets (buffer, sizeof (buffer) - 1, pgperr) != NULL) {
990     err = 1;
991     fputs (buffer, stdout);
992   }
993
994   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
995     empty = 1;
996
997   fclose (pgperr);
998   fclose (pgpout);
999   unlink (signedfile);
1000
1001   if (fclose (fp) != 0) {
1002     mutt_perror ("fclose");
1003     unlink (sigfile);
1004     return (NULL);
1005   }
1006
1007   if (err) {
1008     pgp_void_passphrase ();
1009     mutt_any_key_to_continue (NULL);
1010   }
1011
1012   if (empty) {
1013     unlink (sigfile);
1014     return (NULL);              /* fatal error while signing */
1015   }
1016
1017   t = mutt_new_body ();
1018   t->type = TYPEMULTIPART;
1019   t->subtype = str_dup ("signed");
1020   t->encoding = ENC7BIT;
1021   t->use_disp = 0;
1022   t->disposition = DISPINLINE;
1023
1024   mutt_generate_boundary (&t->parameter);
1025   mutt_set_parameter ("protocol", "application/pgp-signature", &t->parameter);
1026   mutt_set_parameter ("micalg", pgp_micalg (sigfile), &t->parameter);
1027
1028   t->parts = a;
1029   a = t;
1030
1031   t->parts->next = mutt_new_body ();
1032   t = t->parts->next;
1033   t->type = TYPEAPPLICATION;
1034   t->subtype = str_dup ("pgp-signature");
1035   t->filename = str_dup (sigfile);
1036   t->use_disp = 0;
1037   t->disposition = DISPINLINE;
1038   t->encoding = ENC7BIT;
1039   t->unlink = 1;                /* ok to remove this file after sending. */
1040
1041   return (a);
1042 }
1043
1044 static short is_numerical_keyid (const char *s)
1045 {
1046   /* or should we require the "0x"? */
1047   if (strncmp (s, "0x", 2) == 0)
1048     s += 2;
1049   if (str_len (s) % 8)
1050     return 0;
1051   while (*s)
1052     if (strchr ("0123456789ABCDEFabcdef", *s++) == NULL)
1053       return 0;
1054
1055   return 1;
1056 }
1057
1058 /* This routine attempts to find the keyids of the recipients of a message.
1059  * It returns NULL if any of the keys can not be found.
1060  */
1061 char *pgp_findKeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
1062 {
1063   char *keyID, *keylist = NULL, *t;
1064   size_t keylist_size = 0;
1065   size_t keylist_used = 0;
1066   ADDRESS *tmp = NULL, *addr = NULL;
1067   ADDRESS **last = &tmp;
1068   ADDRESS *p, *q;
1069   int i;
1070   pgp_key_t k_info = NULL, key = NULL;
1071
1072   const char *fqdn = mutt_fqdn (1);
1073
1074   for (i = 0; i < 3; i++) {
1075     switch (i) {
1076     case 0:
1077       p = to;
1078       break;
1079     case 1:
1080       p = cc;
1081       break;
1082     case 2:
1083       p = bcc;
1084       break;
1085     default:
1086       abort ();
1087     }
1088
1089     *last = rfc822_cpy_adr (p);
1090     while (*last)
1091       last = &((*last)->next);
1092   }
1093
1094   if (fqdn)
1095     rfc822_qualify (tmp, fqdn);
1096
1097   tmp = mutt_remove_duplicates (tmp);
1098
1099   for (p = tmp; p; p = p->next) {
1100     char buf[LONG_STRING];
1101
1102     q = p;
1103     k_info = NULL;
1104
1105     if ((keyID = mutt_crypt_hook (p)) != NULL) {
1106       int r;
1107
1108       snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"), keyID,
1109                 p->mailbox);
1110       if ((r = mutt_yesorno (buf, M_YES)) == M_YES) {
1111         if (is_numerical_keyid (keyID)) {
1112           if (strncmp (keyID, "0x", 2) == 0)
1113             keyID += 2;
1114           goto bypass_selection;        /* you don't see this. */
1115         }
1116
1117         /* check for e-mail address */
1118         if ((t = strchr (keyID, '@')) &&
1119             (addr = rfc822_parse_adrlist (NULL, keyID))) {
1120           if (fqdn)
1121             rfc822_qualify (addr, fqdn);
1122           q = addr;
1123         }
1124         else
1125           k_info = pgp_getkeybystr (keyID, KEYFLAG_CANENCRYPT, PGP_PUBRING);
1126       }
1127       else if (r == -1) {
1128         mem_free (&keylist);
1129         rfc822_free_address (&tmp);
1130         rfc822_free_address (&addr);
1131         return NULL;
1132       }
1133     }
1134
1135     if (k_info == NULL)
1136       pgp_invoke_getkeys (q);
1137
1138     if (k_info == NULL
1139         && (k_info =
1140             pgp_getkeybyaddr (q, KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1141       snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
1142
1143       if ((key = pgp_ask_for_key (buf, q->mailbox,
1144                                   KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL) {
1145         mem_free (&keylist);
1146         rfc822_free_address (&tmp);
1147         rfc822_free_address (&addr);
1148         return NULL;
1149       }
1150     }
1151     else
1152       key = k_info;
1153
1154     keyID = pgp_keyid (key);
1155
1156   bypass_selection:
1157     keylist_size += str_len (keyID) + 4;
1158     mem_realloc (&keylist, keylist_size);
1159     sprintf (keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", /* __SPRINTF_CHECKED__ */
1160              keyID);
1161     keylist_used = str_len (keylist);
1162
1163     pgp_free_key (&key);
1164     rfc822_free_address (&addr);
1165
1166   }
1167   rfc822_free_address (&tmp);
1168   return (keylist);
1169 }
1170
1171 /* Warning: "a" is no longer freed in this routine, you need
1172  * to free it later.  This is necessary for $fcc_attach. */
1173
1174 BODY *pgp_encrypt_message (BODY * a, char *keylist, int sign)
1175 {
1176   char buf[LONG_STRING];
1177   char tempfile[_POSIX_PATH_MAX], pgperrfile[_POSIX_PATH_MAX];
1178   char pgpinfile[_POSIX_PATH_MAX];
1179   FILE *pgpin, *pgperr, *fpout, *fptmp;
1180   BODY *t;
1181   int err = 0;
1182   int empty = 0;
1183   pid_t thepid;
1184
1185   mutt_mktemp (tempfile);
1186   if ((fpout = safe_fopen (tempfile, "w+")) == NULL) {
1187     mutt_perror (tempfile);
1188     return (NULL);
1189   }
1190
1191   mutt_mktemp (pgperrfile);
1192   if ((pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1193     mutt_perror (pgperrfile);
1194     unlink (tempfile);
1195     fclose (fpout);
1196     return NULL;
1197   }
1198   unlink (pgperrfile);
1199
1200   mutt_mktemp (pgpinfile);
1201   if ((fptmp = safe_fopen (pgpinfile, "w")) == NULL) {
1202     mutt_perror (pgpinfile);
1203     unlink (tempfile);
1204     fclose (fpout);
1205     fclose (pgperr);
1206     return NULL;
1207   }
1208
1209   if (sign)
1210     convert_to_7bit (a);
1211
1212   mutt_write_mime_header (a, fptmp);
1213   fputc ('\n', fptmp);
1214   mutt_write_mime_body (a, fptmp);
1215   fclose (fptmp);
1216
1217   if ((thepid = pgp_invoke_encrypt (&pgpin, NULL, NULL, -1,
1218                                     fileno (fpout), fileno (pgperr),
1219                                     pgpinfile, keylist, sign)) == -1) {
1220     fclose (pgperr);
1221     unlink (pgpinfile);
1222     return (NULL);
1223   }
1224
1225   if (sign) {
1226     if (!pgp_use_gpg_agent ())
1227       fputs (PgpPass, pgpin);
1228     fputc ('\n', pgpin);
1229   }
1230   fclose (pgpin);
1231
1232   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1233     empty = 1;
1234
1235   unlink (pgpinfile);
1236
1237   fflush (fpout);
1238   rewind (fpout);
1239   if (!empty)
1240     empty = (fgetc (fpout) == EOF);
1241   fclose (fpout);
1242
1243   fflush (pgperr);
1244   rewind (pgperr);
1245   while (fgets (buf, sizeof (buf) - 1, pgperr) != NULL) {
1246     err = 1;
1247     fputs (buf, stdout);
1248   }
1249   fclose (pgperr);
1250
1251   /* pause if there is any error output from PGP */
1252   if (err)
1253     mutt_any_key_to_continue (NULL);
1254
1255   if (empty) {
1256     /* fatal error while trying to encrypt message */
1257     if (sign)
1258       pgp_void_passphrase (); /* just in case */
1259     unlink (tempfile);
1260     return (NULL);
1261   }
1262
1263   t = mutt_new_body ();
1264   t->type = TYPEMULTIPART;
1265   t->subtype = str_dup ("encrypted");
1266   t->encoding = ENC7BIT;
1267   t->use_disp = 0;
1268   t->disposition = DISPINLINE;
1269
1270   mutt_generate_boundary (&t->parameter);
1271   mutt_set_parameter ("protocol", "application/pgp-encrypted", &t->parameter);
1272
1273   t->parts = mutt_new_body ();
1274   t->parts->type = TYPEAPPLICATION;
1275   t->parts->subtype = str_dup ("pgp-encrypted");
1276   t->parts->encoding = ENC7BIT;
1277
1278   t->parts->next = mutt_new_body ();
1279   t->parts->next->type = TYPEAPPLICATION;
1280   t->parts->next->subtype = str_dup ("octet-stream");
1281   t->parts->next->encoding = ENC7BIT;
1282   t->parts->next->filename = str_dup (tempfile);
1283   t->parts->next->use_disp = 1;
1284   t->parts->next->disposition = DISPINLINE;
1285   t->parts->next->unlink = 1;   /* delete after sending the message */
1286   t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime can save */
1287
1288   return (t);
1289 }
1290
1291 BODY *pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
1292 {
1293   BODY *b;
1294
1295   char pgpoutfile[_POSIX_PATH_MAX];
1296   char pgperrfile[_POSIX_PATH_MAX];
1297   char pgpinfile[_POSIX_PATH_MAX];
1298
1299   char body_charset[STRING];
1300   char *from_charset;
1301   const char *send_charset;
1302
1303   FILE *pgpout = NULL, *pgperr = NULL, *pgpin = NULL;
1304   FILE *fp;
1305
1306   int empty = 0;
1307   int err;
1308
1309   char buff[STRING];
1310
1311   pid_t thepid;
1312
1313   if (a->type != TYPETEXT)
1314     return NULL;
1315   if (ascii_strcasecmp (a->subtype, "plain"))
1316     return NULL;
1317
1318   if ((fp = fopen (a->filename, "r")) == NULL) {
1319     mutt_perror (a->filename);
1320     return NULL;
1321   }
1322
1323   mutt_mktemp (pgpinfile);
1324   if ((pgpin = safe_fopen (pgpinfile, "w")) == NULL) {
1325     mutt_perror (pgpinfile);
1326     fclose (fp);
1327     return NULL;
1328   }
1329
1330   /* The following code is really correct:  If noconv is set,
1331    * a's charset parameter contains the on-disk character set, and
1332    * we have to convert from that to utf-8.  If noconv is not set,
1333    * we have to convert from $charset to utf-8.
1334    */
1335
1336   mutt_get_body_charset (body_charset, sizeof (body_charset), a);
1337   if (a->noconv)
1338     from_charset = body_charset;
1339   else
1340     from_charset = Charset;
1341
1342   if (!mutt_is_us_ascii (body_charset)) {
1343     int c;
1344     FGETCONV *fc;
1345
1346     if (flags & ENCRYPT)
1347       send_charset = "us-ascii";
1348     else
1349       send_charset = "utf-8";
1350
1351     fc = fgetconv_open (fp, from_charset, "utf-8", M_ICONV_HOOK_FROM);
1352     while ((c = fgetconv (fc)) != EOF)
1353       fputc (c, pgpin);
1354
1355     fgetconv_close (&fc);
1356   }
1357   else {
1358     send_charset = "us-ascii";
1359     mutt_copy_stream (fp, pgpin);
1360   }
1361   safe_fclose (&fp);
1362   fclose (pgpin);
1363
1364   mutt_mktemp (pgpoutfile);
1365   mutt_mktemp (pgperrfile);
1366   if ((pgpout = safe_fopen (pgpoutfile, "w+")) == NULL ||
1367       (pgperr = safe_fopen (pgperrfile, "w+")) == NULL) {
1368     mutt_perror (pgpout ? pgperrfile : pgpoutfile);
1369     unlink (pgpinfile);
1370     if (pgpout) {
1371       fclose (pgpout);
1372       unlink (pgpoutfile);
1373     }
1374     return NULL;
1375   }
1376
1377   unlink (pgperrfile);
1378
1379   if ((thepid = pgp_invoke_traditional (&pgpin, NULL, NULL,
1380                                         -1, fileno (pgpout), fileno (pgperr),
1381                                         pgpinfile, keylist, flags)) == -1) {
1382     mutt_perror (_("Can't invoke PGP"));
1383
1384     fclose (pgpout);
1385     fclose (pgperr);
1386     mutt_unlink (pgpinfile);
1387     unlink (pgpoutfile);
1388     return NULL;
1389   }
1390
1391   if (pgp_use_gpg_agent ())
1392     *PgpPass = 0;
1393   if (flags & SIGN)
1394     fprintf (pgpin, "%s\n", PgpPass);
1395   fclose (pgpin);
1396
1397   if (mutt_wait_filter (thepid) && option (OPTPGPCHECKEXIT))
1398     empty = 1;
1399
1400   mutt_unlink (pgpinfile);
1401
1402   fflush (pgpout);
1403   fflush (pgperr);
1404
1405   rewind (pgpout);
1406   rewind (pgperr);
1407
1408   if (!empty)
1409     empty = (fgetc (pgpout) == EOF);
1410   fclose (pgpout);
1411
1412   err = 0;
1413
1414   while (fgets (buff, sizeof (buff), pgperr)) {
1415     err = 1;
1416     fputs (buff, stdout);
1417   }
1418
1419   fclose (pgperr);
1420
1421   if (err)
1422     mutt_any_key_to_continue (NULL);
1423
1424   if (empty) {
1425     if (flags & SIGN)
1426       pgp_void_passphrase (); /* just in case */
1427     unlink (pgpoutfile);
1428     return NULL;
1429   }
1430
1431   b = mutt_new_body ();
1432
1433   b->encoding = ENC7BIT;
1434
1435   b->type = TYPETEXT;
1436   b->subtype = str_dup ("plain");
1437
1438   mutt_set_parameter ("x-action",
1439                       flags & ENCRYPT ? "pgp-encrypted" : "pgp-signed",
1440                       &b->parameter);
1441   mutt_set_parameter ("charset", send_charset, &b->parameter);
1442
1443   b->filename = str_dup (pgpoutfile);
1444
1445 #if 0
1446   /* The following is intended to give a clue to some completely brain-dead 
1447    * "mail environments" which are typically used by large corporations.
1448    */
1449
1450   b->d_filename = str_dup ("msg.pgp");
1451   b->use_disp = 1;
1452
1453 #endif
1454
1455   b->disposition = DISPINLINE;
1456   b->unlink = 1;
1457
1458   b->noconv = 1;
1459   b->use_disp = 0;
1460
1461   if (!(flags & ENCRYPT))
1462     b->encoding = a->encoding;
1463
1464   return b;
1465 }
1466
1467 int pgp_send_menu (HEADER * msg, int *redraw)
1468 {
1469   pgp_key_t p;
1470   char input_signas[SHORT_STRING];
1471
1472   char prompt[LONG_STRING];
1473
1474   if (!(WithCrypto & APPLICATION_PGP))
1475     return msg->security;
1476
1477   /* If autoinline and no crypto options set, then set inline. */
1478   if (option (OPTPGPAUTOINLINE) && !((msg->security & APPLICATION_PGP)
1479                                      && (msg->security & (SIGN | ENCRYPT))))
1480     msg->security |= INLINE;
1481
1482   snprintf (prompt, sizeof (prompt),
1483             _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, %s, or (c)lear? "),
1484             (msg->security & INLINE) ? _("PGP/M(i)ME") : _("(i)nline"));
1485
1486   switch (mutt_multi_choice (prompt, _("esabifc"))) {
1487   case 1:                      /* (e)ncrypt */
1488     msg->security |= ENCRYPT;
1489     msg->security &= ~SIGN;
1490     break;
1491
1492   case 2:                      /* (s)ign */
1493     msg->security |= SIGN;
1494     msg->security &= ~ENCRYPT;
1495     break;
1496
1497   case 3:                      /* sign (a)s */
1498     unset_option (OPTPGPCHECKTRUST);
1499
1500     if ((p =
1501          pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
1502                           PGP_PUBRING))) {
1503       snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
1504       str_replace (&PgpSignAs, input_signas);
1505       pgp_free_key (&p);
1506
1507       msg->security |= SIGN;
1508
1509       crypt_pgp_void_passphrase ();     /* probably need a different passphrase */
1510     }
1511 #if 0
1512     else {
1513       msg->security &= ~SIGN;
1514     }
1515 #endif
1516
1517     *redraw = REDRAW_FULL;
1518     break;
1519
1520   case 4:                      /* (b)oth */
1521     msg->security |= (ENCRYPT | SIGN);
1522     break;
1523
1524   case 5:                      /* (i)nline */
1525     if ((msg->security & (ENCRYPT | SIGN)))
1526       msg->security ^= INLINE;
1527     else
1528       msg->security &= ~INLINE;
1529     break;
1530
1531   case 6:                      /* (f)orget it */
1532   case 7:                      /* (c)lear     */
1533     msg->security = 0;
1534     break;
1535   }
1536
1537   if (msg->security) {
1538     if (!(msg->security & (ENCRYPT | SIGN)))
1539       msg->security = 0;
1540     else
1541       msg->security |= APPLICATION_PGP;
1542   }
1543
1544   return (msg->security);
1545 }
1546
1547
1548 #endif /* CRYPT_BACKEND_CLASSIC_PGP */