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