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