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