Rocco Rutte:
[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)
364     return 0;
365
366   if ((m->type & TYPEAPPLICATION) && m->subtype)
367   {
368     /* S/MIME MIME types don't need x- anymore, see RFC2311 */
369     if (!ascii_strcasecmp (m->subtype, "x-pkcs7-mime") ||
370         !ascii_strcasecmp (m->subtype, "pkcs7-mime"))
371     {
372       if ((t = mutt_get_parameter ("smime-type", m->parameter)))
373       {
374         if (!ascii_strcasecmp (t, "enveloped-data"))
375           return SMIMEENCRYPT;
376         else if (!ascii_strcasecmp (t, "signed-data"))
377           return (SMIMESIGN|SMIMEOPAQUE);
378         else return 0;
379       }
380       /* Netscape 4.7 uses 
381        * Content-Description: S/MIME Encrypted Message
382        * instead of Content-Type parameter
383        */
384       if (!ascii_strcasecmp (m->description, "S/MIME Encrypted Message"))
385         return SMIMEENCRYPT;
386       complain = 1;
387     }
388     else if (ascii_strcasecmp (m->subtype, "octet-stream"))
389       return 0;
390
391     t = mutt_get_parameter ("name", m->parameter);
392
393     if (!t) t = m->d_filename;
394     if (!t) t = m->filename;
395     if (!t) 
396     {
397       if (complain)
398         mutt_message (_("S/MIME messages with no hints on content are unsupported."));
399       return 0;
400     }
401
402     /* no .p7c, .p10 support yet. */
403
404     len = mutt_strlen (t) - 4;
405     if (len > 0 && *(t+len) == '.')
406     {
407       len++;
408       if (!ascii_strcasecmp ((t+len), "p7m"))
409 #if 0
410        return SMIMEENCRYPT;
411 #else
412       /* Not sure if this is the correct thing to do, but 
413          it's required for compatibility with Outlook */
414        return (SMIMESIGN|SMIMEOPAQUE);
415 #endif
416       else if (!ascii_strcasecmp ((t+len), "p7s"))
417         return (SMIMESIGN|SMIMEOPAQUE);
418     }
419   }
420
421   return 0;
422 }
423
424
425
426
427
428
429 int crypt_query (BODY *m)
430 {
431   int t = 0;
432
433   if (!WithCrypto)
434     return 0;
435   
436   if (!m)
437     return 0;
438
439   if (m->type == TYPEAPPLICATION)
440   {
441     if ((WithCrypto & APPLICATION_PGP))
442       t |= mutt_is_application_pgp(m);
443
444     if ((WithCrypto & APPLICATION_SMIME))
445     {
446       t |= mutt_is_application_smime(m);
447       if (t && m->goodsig) t |= GOODSIGN;
448       if (t && m->badsig) t |= BADSIGN;
449     }
450   }
451   else if ((WithCrypto & APPLICATION_PGP) && m->type == TYPETEXT)
452   {
453     t |= mutt_is_application_pgp (m);
454     if (t && m->goodsig)
455       t |= GOODSIGN;
456   }
457   
458   if (m->type == TYPEMULTIPART)
459   {
460     t |= mutt_is_multipart_encrypted(m);
461     t |= mutt_is_multipart_signed (m);
462
463     if (t && m->goodsig) 
464       t |= GOODSIGN;
465   }
466
467   if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE)
468   {
469     BODY *p;
470     int u, v, w;
471     
472     u = m->parts ? 0xffffffff : 0;      /* Bits set in all parts */
473     w = 0;                              /* Bits set in any part  */
474  
475     for (p = m->parts; p; p = p->next)
476     {
477       v  = crypt_query (p);
478       u &= v; w |= v;
479     }
480     t |= u | (w & ~GOODSIGN);
481     
482     if ((w & GOODSIGN) && !(u & GOODSIGN))
483       t |= PARTSIGN;
484   }
485
486   return t;
487 }
488
489
490
491
492 int crypt_write_signed(BODY *a, STATE *s, const char *tempfile)
493 {
494   FILE *fp;
495   int c;
496   short hadcr;
497   size_t bytes;
498
499   if (!WithCrypto)
500     return -1;
501
502   if (!(fp = safe_fopen (tempfile, "w")))
503   {
504     mutt_perror (tempfile);
505     return -1;
506   }
507       
508   fseek (s->fpin, a->hdr_offset, 0);
509   bytes = a->length + a->offset - a->hdr_offset;
510   hadcr = 0;
511   while (bytes > 0)
512   {
513     if ((c = fgetc (s->fpin)) == EOF)
514       break;
515     
516     bytes--;
517     
518     if  (c == '\r')
519       hadcr = 1;
520     else 
521     {
522       if (c == '\n' && !hadcr)
523         fputc ('\r', fp);
524       
525       hadcr = 0;
526     }
527     
528     fputc (c, fp);
529     
530   }
531   fclose (fp);
532
533   return 0;
534 }
535
536
537
538 void convert_to_7bit (BODY *a)
539 {
540   if (!WithCrypto)
541     return;
542
543   while (a)
544   {
545     if (a->type == TYPEMULTIPART)
546     {
547       if (a->encoding != ENC7BIT)
548       {
549         a->encoding = ENC7BIT;
550         convert_to_7bit(a->parts);
551       }
552       else if ((WithCrypto & APPLICATION_PGP) && option (OPTPGPSTRICTENC))
553         convert_to_7bit (a->parts);
554     } 
555     else if (a->type == TYPEMESSAGE &&
556              mutt_strcasecmp(a->subtype, "delivery-status"))
557     {
558       if(a->encoding != ENC7BIT)
559         mutt_message_to_7bit (a, NULL);
560     }
561     else if (a->encoding == ENC8BIT)
562       a->encoding = ENCQUOTEDPRINTABLE;
563     else if (a->encoding == ENCBINARY)
564       a->encoding = ENCBASE64;
565     else if (a->content && a->encoding != ENCBASE64 &&
566              (a->content->from || (a->content->space && 
567                                    option (OPTPGPSTRICTENC))))
568       a->encoding = ENCQUOTEDPRINTABLE;
569     a = a->next;
570   }
571 }
572
573
574
575
576 void crypt_extract_keys_from_messages (HEADER * h)
577 {
578   int i;
579   char tempfname[_POSIX_PATH_MAX], *mbox;
580   ADDRESS *tmp = NULL;
581   FILE *fpout;
582
583   if (!WithCrypto)
584     return;
585
586   mutt_mktemp (tempfname);
587   if (!(fpout = safe_fopen (tempfname, "w")))
588   {
589     mutt_perror (tempfname);
590     return;
591   }
592
593   if ((WithCrypto & APPLICATION_PGP))
594     set_option (OPTDONTHANDLEPGPKEYS);
595
596   if (!h)
597   {
598     for (i = 0; i < Context->vcount; i++)
599     {
600       if (Context->hdrs[Context->v2r[i]]->tagged)
601       {
602         mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
603         if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
604             !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->security))
605         {
606           fclose (fpout);
607           break;
608         }
609
610         if ((WithCrypto & APPLICATION_PGP)
611             && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP))
612         {
613           mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]], 
614                              M_CM_DECODE|M_CM_CHARCONV, 0);
615           fflush(fpout);
616           
617           mutt_endwin (_("Trying to extract PGP keys...\n"));
618           crypt_pgp_invoke_import (tempfname);
619         }
620
621         if ((WithCrypto & APPLICATION_SMIME)
622             && (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME))
623         {
624           if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
625             mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
626                                M_CM_NOHEADER|M_CM_DECODE_CRYPT
627                                |M_CM_DECODE_SMIME, 0);
628           else
629             mutt_copy_message (fpout, Context,
630                                Context->hdrs[Context->v2r[i]], 0, 0);
631           fflush(fpout);
632
633           if (Context->hdrs[Context->v2r[i]]->env->from)
634             tmp = mutt_expand_aliases (h->env->from);
635           else if (Context->hdrs[Context->v2r[i]]->env->sender)
636             tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
637                                                     ->env->sender);
638           mbox = tmp ? tmp->mailbox : NULL;
639           if (mbox)
640           {
641             mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
642             crypt_smime_invoke_import (tempfname, mbox);
643             tmp = NULL;
644           }
645         }
646
647         rewind (fpout);
648       }
649     }
650   }
651   else
652   {
653     mutt_parse_mime_message (Context, h);
654     if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security)))
655     {
656       if ((WithCrypto & APPLICATION_PGP)
657           && (h->security & APPLICATION_PGP))
658       {
659         mutt_copy_message (fpout, Context, h, M_CM_DECODE|M_CM_CHARCONV, 0);
660         fflush(fpout);
661         mutt_endwin (_("Trying to extract PGP keys...\n"));
662         crypt_pgp_invoke_import (tempfname);
663       }
664
665       if ((WithCrypto & APPLICATION_SMIME)
666           && (h->security & APPLICATION_SMIME))
667       {
668         if (h->security & ENCRYPT)
669           mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
670                                                 |M_CM_DECODE_CRYPT
671                                                 |M_CM_DECODE_SMIME, 0);
672         else
673           mutt_copy_message (fpout, Context, h, 0, 0);
674
675         fflush(fpout);
676         if (h->env->from) tmp = mutt_expand_aliases (h->env->from);
677         else if (h->env->sender)  tmp = mutt_expand_aliases (h->env->sender); 
678         mbox = tmp ? tmp->mailbox : NULL;
679         if (mbox) /* else ? */
680         {
681           mutt_message (_("Trying to extract S/MIME certificates...\n"));
682           crypt_smime_invoke_import (tempfname, mbox);
683         }
684       }
685     }
686   }
687       
688   fclose (fpout);
689   if (isendwin())
690     mutt_any_key_to_continue (NULL);
691
692   mutt_unlink (tempfname);
693
694   if ((WithCrypto & APPLICATION_PGP))
695     unset_option (OPTDONTHANDLEPGPKEYS);
696 }
697
698
699
700 int crypt_get_keys (HEADER *msg, char **keylist)
701 {
702   /* Do a quick check to make sure that we can find all of the encryption
703    * keys if the user has requested this service.
704    */
705
706   if (!WithCrypto)
707     return 0;
708
709   if ((WithCrypto & APPLICATION_PGP))
710     set_option (OPTPGPCHECKTRUST);
711
712   *keylist = NULL;
713
714   if (msg->security & ENCRYPT)
715   {
716      if ((WithCrypto & APPLICATION_PGP)
717          && (msg->security & APPLICATION_PGP))
718      {
719        if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
720                                msg->env->bcc)) == NULL)
721            return (-1);
722        unset_option (OPTPGPCHECKTRUST);
723      }
724      if ((WithCrypto & APPLICATION_SMIME)
725          && (msg->security & APPLICATION_SMIME))
726      {
727        if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
728                                              msg->env->bcc)) == NULL)
729            return (-1);
730      }
731   }
732     
733   return (0);
734 }
735
736
737
738 static void crypt_fetch_signatures (BODY ***signatures, BODY *a, int *n)
739 {
740   if (!WithCrypto)
741     return;
742
743   for (; a; a = a->next)
744   {
745     if (a->type == TYPEMULTIPART)
746       crypt_fetch_signatures (signatures, a->parts, n);
747     else
748     {
749       if((*n % 5) == 0)
750         safe_realloc (signatures, (*n + 6) * sizeof (BODY **));
751
752       (*signatures)[(*n)++] = a;
753     }
754   }
755 }
756
757
758 /*
759  * This routine verifies a  "multipart/signed"  body.
760  */
761
762 void mutt_signed_handler (BODY *a, STATE *s)
763 {
764   char tempfile[_POSIX_PATH_MAX];
765   char *protocol;
766   int protocol_major = TYPEOTHER;
767   char *protocol_minor = NULL;
768   
769   BODY *b = a;
770   BODY **signatures = NULL;
771   int sigcnt = 0;
772   int i;
773   short goodsig = 1;
774
775   if (!WithCrypto)
776     return;
777
778   protocol = mutt_get_parameter ("protocol", a->parameter);
779   a = a->parts;
780
781   /* extract the protocol information */
782   
783   if (protocol)
784   {
785     char major[STRING];
786     char *t;
787
788     if ((protocol_minor = strchr (protocol, '/'))) protocol_minor++;
789     
790     strfcpy (major, protocol, sizeof(major));
791     if((t = strchr(major, '/')))
792       *t = '\0';
793     
794     protocol_major = mutt_check_mime_type (major);
795   }
796
797   /* consistency check */
798
799   if (!(a && a->next && a->next->type == protocol_major && 
800       !mutt_strcasecmp (a->next->subtype, protocol_minor)))
801   {
802     state_attach_puts (_("[-- Error: "
803                          "Inconsistent multipart/signed structure! --]\n\n"),
804                        s);
805     mutt_body_handler (a, s);
806     return;
807   }
808
809   
810   if ((WithCrypto & APPLICATION_PGP)
811       && protocol_major == TYPEAPPLICATION
812       && !mutt_strcasecmp (protocol_minor, "pgp-signature"))
813     ;
814   else if ((WithCrypto & APPLICATION_SMIME)
815            && protocol_major == TYPEAPPLICATION
816            && !(mutt_strcasecmp (protocol_minor, "x-pkcs7-signature")
817                && mutt_strcasecmp (protocol_minor, "pkcs7-signature")))
818     ;
819   else if (protocol_major == TYPEMULTIPART
820            && !mutt_strcasecmp (protocol_minor, "mixed"))
821     ;
822   else
823   {
824     state_printf (s, _("[-- Error: "
825                        "Unknown multipart/signed protocol %s! --]\n\n"),
826                   protocol);
827     mutt_body_handler (a, s);
828     return;
829   }
830   
831   if (s->flags & M_DISPLAY)
832   {
833     
834     crypt_fetch_signatures (&signatures, a->next, &sigcnt);
835     
836     if (sigcnt)
837     {
838       mutt_mktemp (tempfile);
839       if (crypt_write_signed (a, s, tempfile) == 0)
840       {
841         for (i = 0; i < sigcnt; i++)
842         {
843           if ((WithCrypto & APPLICATION_PGP)
844               && signatures[i]->type == TYPEAPPLICATION 
845               && !mutt_strcasecmp (signatures[i]->subtype, "pgp-signature"))
846           {
847             if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
848               goodsig = 0;
849             
850             continue;
851           }
852
853           if ((WithCrypto & APPLICATION_SMIME)
854               && signatures[i]->type == TYPEAPPLICATION 
855               && (!mutt_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
856                   || !mutt_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
857           {
858             if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
859               goodsig = 0;
860             
861             continue;
862           }
863
864           state_printf (s, _("[-- Warning: "
865                              "We can't verify %s/%s signatures. --]\n\n"),
866                           TYPE(signatures[i]), signatures[i]->subtype);
867         }
868       }
869       
870       mutt_unlink (tempfile);
871
872       b->goodsig = goodsig;
873       b->badsig  = !goodsig;
874       
875       /* Now display the signed body */
876       state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
877
878
879       FREE (&signatures);
880     }
881     else
882       state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"), s);
883   }
884   
885   mutt_body_handler (a, s);
886   
887   if (s->flags & M_DISPLAY && sigcnt)
888     state_attach_puts (_("\n[-- End of signed data --]\n"), s);
889 }
890
891