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