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