Nico Golde:
[apps/madmutt.git] / crypt.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5  * Copyright (C) 2001  Thomas Roessler <roessler@does-not-exist.org>
6  *                     Oliver Ehli <elmy@acm.org>
7  * Copyright (C) 2003  Werner Koch <wk@gnupg.org>
8  * Copyright (C) 2004 g10code GmbH
9  *
10  * This file is part of mutt-ng, see http://www.muttng.org/.
11  * It's licensed under the GNU General Public License,
12  * please see the file GPL in the top level source directory.
13  */
14
15 #if HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #include "mutt.h"
20 #include "mutt_curses.h"
21 #include "mime.h"
22 #include "copy.h"
23 #include "mutt_crypt.h"
24 #include "pgp.h"
25
26 #include "lib/mem.h"
27 #include "lib/intl.h"
28 #include "lib/str.h"
29
30 #include <sys/wait.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <ctype.h>
37
38 #ifdef HAVE_LOCALE_H
39 #include <locale.h>
40 #endif
41
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45
46 #ifdef HAVE_SYS_RESOURCE_H
47 # include <sys/resource.h>
48 #endif
49
50
51 /* print the current time to avoid spoofing of the signature output */
52 void crypt_current_time (STATE * s, char *app_name)
53 {
54   time_t t;
55   char p[STRING], tmp[STRING];
56
57   if (!WithCrypto)
58     return;
59
60   if (option (OPTCRYPTTIMESTAMP)) {
61     t = time (NULL);
62     setlocale (LC_TIME, "");
63     strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
64     setlocale (LC_TIME, "C");
65   }
66   else
67     *p = '\0';
68
69   snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
70             NONULL (app_name), p);
71   state_attach_puts (tmp, s);
72 }
73
74
75
76 void crypt_forget_passphrase (void)
77 {
78   if ((WithCrypto & APPLICATION_PGP))
79     crypt_pgp_void_passphrase ();
80
81   if ((WithCrypto & APPLICATION_SMIME))
82     crypt_smime_void_passphrase ();
83
84   if (WithCrypto)
85     mutt_message _("Passphrase(s) forgotten.");
86 }
87
88
89 #if defined(HAVE_SETRLIMIT) && (!defined(DEBUG))
90
91 static void disable_coredumps (void)
92 {
93   struct rlimit rl = { 0, 0 };
94   static short done = 0;
95
96   if (!done) {
97     setrlimit (RLIMIT_CORE, &rl);
98     done = 1;
99   }
100 }
101
102 #endif /* HAVE_SETRLIMIT */
103
104
105 int crypt_valid_passphrase (int flags)
106 {
107   int ret = 0;
108
109 # if defined(HAVE_SETRLIMIT) &&(!defined(DEBUG))
110   disable_coredumps ();
111 # endif
112
113   if ((WithCrypto & APPLICATION_PGP) && (flags & APPLICATION_PGP))
114     ret = crypt_pgp_valid_passphrase ();
115
116   if ((WithCrypto & APPLICATION_SMIME) && (flags & APPLICATION_SMIME))
117     ret = crypt_smime_valid_passphrase ();
118
119   return ret;
120 }
121
122
123
124 int mutt_protect (HEADER * msg, char *keylist)
125 {
126   BODY *pbody = NULL, *tmp_pbody = NULL;
127   BODY *tmp_smime_pbody = NULL;
128   BODY *tmp_pgp_pbody = NULL;
129   int flags = (WithCrypto & APPLICATION_PGP) ? msg->security : 0;
130   int i;
131
132   if (!WithCrypto)
133     return -1;
134
135   if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
136     return (-1);
137
138   if ((WithCrypto & APPLICATION_PGP)
139       && ((msg->security & PGPINLINE) == PGPINLINE)) {
140     /* they really want to send it inline... go for it */
141     if (!isendwin ())
142       mutt_endwin _("Invoking PGP...");
143
144     pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
145     if (pbody) {
146       msg->content = pbody;
147       return 0;
148     }
149
150     /* otherwise inline won't work...ask for revert */
151     if ((i =
152          query_quadoption (OPT_PGPMIMEAUTO,
153                            _
154                            ("Message can't be sent inline.  Revert to using PGP/MIME?")))
155         != M_YES) {
156       mutt_error _("Mail not sent.");
157
158       return -1;
159     }
160
161     /* go ahead with PGP/MIME */
162   }
163
164   if (!isendwin ())
165     mutt_endwin (NULL);
166
167   if ((WithCrypto & APPLICATION_SMIME))
168     tmp_smime_pbody = msg->content;
169   if ((WithCrypto & APPLICATION_PGP))
170     tmp_pgp_pbody = msg->content;
171
172   if (msg->security & SIGN) {
173     if ((WithCrypto & APPLICATION_SMIME)
174         && (msg->security & APPLICATION_SMIME)) {
175       if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
176         return -1;
177       pbody = tmp_smime_pbody = tmp_pbody;
178     }
179
180     if ((WithCrypto & APPLICATION_PGP)
181         && (msg->security & APPLICATION_PGP)
182         && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
183       if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
184         return -1;
185
186       flags &= ~SIGN;
187       pbody = tmp_pgp_pbody = tmp_pbody;
188     }
189
190     if (WithCrypto && (msg->security & APPLICATION_SMIME)
191         && (msg->security & APPLICATION_PGP)) {
192       /* here comes the draft ;-) */
193     }
194   }
195
196
197   if (msg->security & ENCRYPT) {
198     if ((WithCrypto & APPLICATION_SMIME)
199         && (msg->security & APPLICATION_SMIME)) {
200       if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
201                                                         keylist))) {
202         /* signed ? free it! */
203         return (-1);
204       }
205       /* free tmp_body if messages was signed AND encrypted ... */
206       if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
207         /* detatch and dont't delete msg->content,
208            which tmp_smime_pbody->parts after signing. */
209         tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
210         msg->content->next = NULL;
211         mutt_free_body (&tmp_smime_pbody);
212       }
213       pbody = tmp_pbody;
214     }
215
216     if ((WithCrypto & APPLICATION_PGP)
217         && (msg->security & APPLICATION_PGP)) {
218       if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
219                                                flags & SIGN))) {
220
221         /* did we perform a retainable signature? */
222         if (flags != msg->security) {
223           /* remove the outer multipart layer */
224           tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
225           /* get rid of the signature */
226           mutt_free_body (&tmp_pgp_pbody->next);
227         }
228
229         return (-1);
230       }
231
232       /* destroy temporary signature envelope when doing retainable 
233        * signatures.
234
235        */
236       if (flags != msg->security) {
237         tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
238         mutt_free_body (&tmp_pgp_pbody->next);
239       }
240     }
241   }
242
243   if (pbody)
244     msg->content = pbody;
245
246   return 0;
247 }
248
249
250
251
252 int mutt_is_multipart_signed (BODY * b)
253 {
254   char *p;
255
256   if (!b || !(b->type == TYPEMULTIPART) ||
257       !b->subtype || ascii_strcasecmp (b->subtype, "signed"))
258     return 0;
259
260   if (!(p = mutt_get_parameter ("protocol", b->parameter)))
261     return 0;
262
263   if (!(ascii_strcasecmp (p, "multipart/mixed")))
264     return SIGN;
265
266   if ((WithCrypto & APPLICATION_PGP)
267       && !(ascii_strcasecmp (p, "application/pgp-signature")))
268     return PGPSIGN;
269
270   if ((WithCrypto & APPLICATION_SMIME)
271       && !(ascii_strcasecmp (p, "application/x-pkcs7-signature")))
272     return SMIMESIGN;
273   if ((WithCrypto & APPLICATION_SMIME)
274       && !(ascii_strcasecmp (p, "application/pkcs7-signature")))
275     return SMIMESIGN;
276
277   return 0;
278 }
279
280
281 int mutt_is_multipart_encrypted (BODY * b)
282 {
283   if ((WithCrypto & APPLICATION_PGP)) {
284     char *p;
285
286     if (!b || b->type != TYPEMULTIPART ||
287         !b->subtype || ascii_strcasecmp (b->subtype, "encrypted") ||
288         !(p = mutt_get_parameter ("protocol", b->parameter)) ||
289         ascii_strcasecmp (p, "application/pgp-encrypted"))
290       return 0;
291
292     return PGPENCRYPT;
293   }
294
295   return 0;
296 }
297
298
299 int mutt_is_application_pgp (BODY * m)
300 {
301   int t = 0;
302   char *p;
303
304   if (m->type == TYPEAPPLICATION) {
305     if (!ascii_strcasecmp (m->subtype, "pgp")
306         || !ascii_strcasecmp (m->subtype, "x-pgp-message")) {
307       if ((p = mutt_get_parameter ("x-action", m->parameter))
308           && (!ascii_strcasecmp (p, "sign")
309               || !ascii_strcasecmp (p, "signclear")))
310         t |= PGPSIGN;
311
312       if ((p = mutt_get_parameter ("format", m->parameter)) &&
313           !ascii_strcasecmp (p, "keys-only"))
314         t |= PGPKEY;
315
316       if (!t)
317         t |= PGPENCRYPT;        /* not necessarily correct, but... */
318     }
319
320     if (!ascii_strcasecmp (m->subtype, "pgp-signed"))
321       t |= PGPSIGN;
322
323     if (!ascii_strcasecmp (m->subtype, "pgp-keys"))
324       t |= PGPKEY;
325   }
326   else if (m->type == TYPETEXT && ascii_strcasecmp ("plain", m->subtype) == 0) {
327     if (((p = mutt_get_parameter ("x-mutt-action", m->parameter))
328          || (p = mutt_get_parameter ("x-action", m->parameter))
329          || (p = mutt_get_parameter ("action", m->parameter)))
330         && !ascii_strncasecmp ("pgp-sign", p, 8))
331       t |= PGPSIGN;
332     else if (p && !ascii_strncasecmp ("pgp-encrypt", p, 11))
333       t |= PGPENCRYPT;
334     else if (p && !ascii_strncasecmp ("pgp-keys", p, 7))
335       t |= PGPKEY;
336   }
337   if (t)
338     t |= PGPINLINE;
339
340   return t;
341 }
342
343 int mutt_is_application_smime (BODY * m)
344 {
345   char *t = NULL;
346   int len, complain = 0;
347
348   if (!m)
349     return 0;
350
351   if ((m->type & TYPEAPPLICATION) && m->subtype) {
352     /* S/MIME MIME types don't need x- anymore, see RFC2311 */
353     if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
354         !ascii_strcasecmp (m->subtype, "pkcs7-mime")) {
355       if ((t = mutt_get_parameter ("smime-type", m->parameter))) {
356         if (!ascii_strcasecmp (t, "enveloped-data"))
357           return SMIMEENCRYPT;
358         else if (!ascii_strcasecmp (t, "signed-data"))
359           return (SMIMESIGN | SMIMEOPAQUE);
360         else
361           return 0;
362       }
363       /* Netscape 4.7 uses 
364        * Content-Description: S/MIME Encrypted Message
365        * instead of Content-Type parameter
366        */
367       if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
368         return SMIMEENCRYPT;
369       complain = 1;
370     }
371     else if (ascii_strcasecmp (m->subtype, "octet-stream"))
372       return 0;
373
374     t = mutt_get_parameter ("name", m->parameter);
375
376     if (!t)
377       t = m->d_filename;
378     if (!t)
379       t = m->filename;
380     if (!t) {
381       if (complain)
382         mutt_message (_
383                       ("S/MIME messages with no hints on content are unsupported."));
384       return 0;
385     }
386
387     /* no .p7c, .p10 support yet. */
388
389     len = safe_strlen (t) - 4;
390     if (len > 0 && *(t + len) == '.') {
391       len++;
392       if (!ascii_strcasecmp ((t + len), "p7m"))
393 #if 0
394         return SMIMEENCRYPT;
395 #else
396         /* Not sure if this is the correct thing to do, but 
397            it's required for compatibility with Outlook */
398         return (SMIMESIGN | SMIMEOPAQUE);
399 #endif
400       else if (!ascii_strcasecmp ((t + len), "p7s"))
401         return (SMIMESIGN | SMIMEOPAQUE);
402     }
403   }
404
405   return 0;
406 }
407
408
409
410
411
412
413 int crypt_query (BODY * m)
414 {
415   int t = 0;
416
417   if (!WithCrypto)
418     return 0;
419
420   if (!m)
421     return 0;
422
423   if (m->type == TYPEAPPLICATION) {
424     if ((WithCrypto & APPLICATION_PGP))
425       t |= mutt_is_application_pgp (m);
426
427     if ((WithCrypto & APPLICATION_SMIME)) {
428       t |= mutt_is_application_smime (m);
429       if (t && m->goodsig)
430         t |= GOODSIGN;
431       if (t && m->badsig)
432         t |= BADSIGN;
433     }
434   }
435   else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT) {
436     t |= mutt_is_application_pgp (m);
437     if (t && m->goodsig)
438       t |= GOODSIGN;
439   }
440
441   if (m->type == TYPEMULTIPART) {
442     t |= mutt_is_multipart_encrypted (m);
443     t |= mutt_is_multipart_signed (m);
444
445     if (t && m->goodsig)
446       t |= GOODSIGN;
447   }
448
449   if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
450     BODY *p;
451     int u, v, w;
452
453     u = m->parts ? 0xffffffff : 0;      /* Bits set in all parts */
454     w = 0;                      /* Bits set in any part  */
455
456     for (p = m->parts; p; p = p->next) {
457       v = crypt_query (p);
458       u &= v;
459       w |= v;
460     }
461     t |= u | (w & ~GOODSIGN);
462
463     if ((w & GOODSIGN) && !(u & GOODSIGN))
464       t |= PARTSIGN;
465   }
466
467   return t;
468 }
469
470
471
472
473 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
474 {
475   FILE *fp;
476   int c;
477   short hadcr;
478   size_t bytes;
479
480   if (!WithCrypto)
481     return -1;
482
483   if (!(fp = safe_fopen (tempfile, "w"))) {
484     mutt_perror (tempfile);
485     return -1;
486   }
487
488   fseek (s->fpin, a->hdr_offset, 0);
489   bytes = a->length + a->offset - a->hdr_offset;
490   hadcr = 0;
491   while (bytes > 0) {
492     if ((c = fgetc (s->fpin)) == EOF)
493       break;
494
495     bytes--;
496
497     if (c == '\r')
498       hadcr = 1;
499     else {
500       if (c == '\n' && !hadcr)
501         fputc ('\r', fp);
502
503       hadcr = 0;
504     }
505
506     fputc (c, fp);
507
508   }
509   fclose (fp);
510
511   return 0;
512 }
513
514
515
516 void convert_to_7bit (BODY * a)
517 {
518   if (!WithCrypto)
519     return;
520
521   while (a) {
522     if (a->type == TYPEMULTIPART) {
523       if (a->encoding != ENC7BIT) {
524         a->encoding = ENC7BIT;
525         convert_to_7bit (a->parts);
526       }
527       else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
528         convert_to_7bit (a->parts);
529     }
530     else if (a->type == TYPEMESSAGE &&
531              safe_strcasecmp (a->subtype, "delivery-status")) {
532       if (a->encoding != ENC7BIT)
533         mutt_message_to_7bit (a, NULL);
534     }
535     else if (a->encoding == ENC8BIT)
536       a->encoding = ENCQUOTEDPRINTABLE;
537     else if (a->encoding == ENCBINARY)
538       a->encoding = ENCBASE64;
539     else if (a->content && a->encoding != ENCBASE64 &&
540              (a->content->from || (a->content->space &&
541                                    option (OPTPGPSTRICTENC))))
542       a->encoding = ENCQUOTEDPRINTABLE;
543     a = a->next;
544   }
545 }
546
547
548
549
550 void crypt_extract_keys_from_messages (HEADER * h)
551 {
552   int i;
553   char tempfname[_POSIX_PATH_MAX], *mbox;
554   ADDRESS *tmp = NULL;
555   FILE *fpout;
556
557   if (!WithCrypto)
558     return;
559
560   mutt_mktemp (tempfname);
561   if (!(fpout = safe_fopen (tempfname, "w"))) {
562     mutt_perror (tempfname);
563     return;
564   }
565
566   if ((WithCrypto & APPLICATION_PGP))
567     set_option (OPTDONTHANDLEPGPKEYS);
568
569   if (!h) {
570     for (i = 0; i < Context->vcount; i++) {
571       if (Context->hdrs[Context->v2r[i]]->tagged) {
572         mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
573         if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
574             !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
575                                      security)) {
576           fclose (fpout);
577           break;
578         }
579
580         if ((WithCrypto & APPLICATION_PGP)
581             && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP)) {
582           mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
583                              M_CM_DECODE | M_CM_CHARCONV, 0);
584           fflush (fpout);
585
586           mutt_endwin (_("Trying to extract PGP keys...\n"));
587           crypt_pgp_invoke_import (tempfname);
588         }
589
590         if ((WithCrypto & APPLICATION_SMIME)
591             && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME)) {
592           if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
593             mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
594                                M_CM_NOHEADER | M_CM_DECODE_CRYPT
595                                | M_CM_DECODE_SMIME, 0);
596           else
597             mutt_copy_message (fpout, Context,
598                                Context->hdrs[Context->v2r[i]], 0, 0);
599           fflush (fpout);
600
601           if (Context->hdrs[Context->v2r[i]]->env->from)
602             tmp = mutt_expand_aliases (h->env->from);
603           else if (Context->hdrs[Context->v2r[i]]->env->sender)
604             tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
605                                        ->env->sender);
606           mbox = tmp ? tmp->mailbox : NULL;
607           if (mbox) {
608             mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
609             crypt_smime_invoke_import (tempfname, mbox);
610             tmp = NULL;
611           }
612         }
613
614         rewind (fpout);
615       }
616     }
617   }
618   else {
619     mutt_parse_mime_message (Context, h);
620     if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
621       if ((WithCrypto & APPLICATION_PGP)
622           && (h->security & APPLICATION_PGP)) {
623         mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
624         fflush (fpout);
625         mutt_endwin (_("Trying to extract PGP keys...\n"));
626         crypt_pgp_invoke_import (tempfname);
627       }
628
629       if ((WithCrypto & APPLICATION_SMIME)
630           && (h->security & APPLICATION_SMIME)) {
631         if (h->security & ENCRYPT)
632           mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
633                              | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
634         else
635           mutt_copy_message (fpout, Context, h, 0, 0);
636
637         fflush (fpout);
638         if (h->env->from)
639           tmp = mutt_expand_aliases (h->env->from);
640         else if (h->env->sender)
641           tmp = mutt_expand_aliases (h->env->sender);
642         mbox = tmp ? tmp->mailbox : NULL;
643         if (mbox) {             /* else ? */
644           mutt_message (_("Trying to extract S/MIME certificates...\n"));
645           crypt_smime_invoke_import (tempfname, mbox);
646         }
647       }
648     }
649   }
650
651   fclose (fpout);
652   if (isendwin ())
653     mutt_any_key_to_continue (NULL);
654
655   mutt_unlink (tempfname);
656
657   if ((WithCrypto & APPLICATION_PGP))
658     unset_option (OPTDONTHANDLEPGPKEYS);
659 }
660
661
662
663 int crypt_get_keys (HEADER * msg, char **keylist)
664 {
665   /* Do a quick check to make sure that we can find all of the encryption
666    * keys if the user has requested this service.
667    */
668
669   if (!WithCrypto)
670     return 0;
671
672   if ((WithCrypto & APPLICATION_PGP))
673     set_option (OPTPGPCHECKTRUST);
674
675   *keylist = NULL;
676
677   if (msg->security & ENCRYPT) {
678     if ((WithCrypto & APPLICATION_PGP)
679         && (msg->security & APPLICATION_PGP)) {
680       if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
681                                           msg->env->bcc)) == NULL)
682         return (-1);
683       unset_option (OPTPGPCHECKTRUST);
684     }
685     if ((WithCrypto & APPLICATION_SMIME)
686         && (msg->security & APPLICATION_SMIME)) {
687       if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
688                                             msg->env->bcc)) == NULL)
689         return (-1);
690     }
691   }
692
693   return (0);
694 }
695
696
697
698 static void crypt_fetch_signatures (BODY *** signatures, BODY * a, int *n)
699 {
700   if (!WithCrypto)
701     return;
702
703   for (; a; a = a->next) {
704     if (a->type == TYPEMULTIPART)
705       crypt_fetch_signatures (signatures, a->parts, n);
706     else {
707       if ((*n % 5) == 0)
708         safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
709
710       (*signatures)[(*n)++] = a;
711     }
712   }
713 }
714
715
716 /*
717  * This routine verifies a  "multipart/signed"  body.
718  */
719
720 void mutt_signed_handler (BODY * a, STATE * s)
721 {
722   char tempfile[_POSIX_PATH_MAX];
723   char *protocol;
724   int protocol_major = TYPEOTHER;
725   char *protocol_minor = NULL;
726
727   BODY *b = a;
728   BODY **signatures = NULL;
729   int sigcnt = 0;
730   int i;
731   short goodsig = 1;
732
733   if (!WithCrypto)
734     return;
735
736   protocol = mutt_get_parameter ("protocol", a->parameter);
737   a = a->parts;
738
739   /* extract the protocol information */
740
741   if (protocol) {
742     char major[STRING];
743     char *t;
744
745     if ((protocol_minor = strchr (protocol, '/')))
746       protocol_minor++;
747
748     strfcpy (major, protocol, sizeof (major));
749     if ((t = strchr (major, '/')))
750       *t = '\0';
751
752     protocol_major = mutt_check_mime_type (major);
753   }
754
755   /* consistency check */
756
757   if (!(a && a->next && a->next->type == protocol_major &&
758         !safe_strcasecmp (a->next->subtype, protocol_minor))) {
759     state_attach_puts (_("[-- Error: "
760                          "Inconsistent multipart/signed structure! --]\n\n"),
761                        s);
762     mutt_body_handler (a, s);
763     return;
764   }
765
766
767   if ((WithCrypto & APPLICATION_PGP)
768       && protocol_major == TYPEAPPLICATION
769       && !safe_strcasecmp (protocol_minor, "pgp-signature"));
770   else if ((WithCrypto & APPLICATION_SMIME)
771            && protocol_major == TYPEAPPLICATION
772            && !(safe_strcasecmp (protocol_minor, "x-pkcs7-signature")
773                 && safe_strcasecmp (protocol_minor, "pkcs7-signature")));
774   else if (protocol_major == TYPEMULTIPART
775            && !safe_strcasecmp (protocol_minor, "mixed"));
776   else {
777     state_printf (s, _("[-- Error: "
778                        "Unknown multipart/signed protocol %s! --]\n\n"),
779                   protocol);
780     mutt_body_handler (a, s);
781     return;
782   }
783
784   if (s->flags & M_DISPLAY) {
785
786     crypt_fetch_signatures (&signatures, a->next, &sigcnt);
787
788     if (sigcnt) {
789       mutt_mktemp (tempfile);
790       if (crypt_write_signed (a, s, tempfile) == 0) {
791         for (i = 0; i < sigcnt; i++) {
792           if ((WithCrypto & APPLICATION_PGP)
793               && signatures[i]->type == TYPEAPPLICATION
794               && !safe_strcasecmp (signatures[i]->subtype, "pgp-signature")) {
795             if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
796               goodsig = 0;
797
798             continue;
799           }
800
801           if ((WithCrypto & APPLICATION_SMIME)
802               && signatures[i]->type == TYPEAPPLICATION
803               &&
804               (!safe_strcasecmp (signatures[i]->subtype, "x-pkcs7-signature")
805                || !safe_strcasecmp (signatures[i]->subtype,
806                                     "pkcs7-signature"))) {
807             if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
808               goodsig = 0;
809
810             continue;
811           }
812
813           state_printf (s, _("[-- Warning: "
814                              "We can't verify %s/%s signatures. --]\n\n"),
815                         TYPE (signatures[i]), signatures[i]->subtype);
816         }
817       }
818
819       mutt_unlink (tempfile);
820
821       b->goodsig = goodsig;
822       b->badsig = !goodsig;
823
824       /* Now display the signed body */
825       state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
826
827
828       FREE (&signatures);
829     }
830     else
831       state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
832                          s);
833   }
834
835   mutt_body_handler (a, s);
836
837   if (s->flags & M_DISPLAY && sigcnt)
838     state_attach_puts (_("\n[-- End of signed data --]\n"), s);
839 }