chiark / gitweb /
gpg agent threading bugs: Add some `xxx' comments.
[gnupg2.git] / dirmngr / certcache.c
1 /* certcache.c - Certificate caching
2  *      Copyright (C) 2004, 2005, 2007, 2008 g10 Code GmbH
3  *
4  * This file is part of DirMngr.
5  *
6  * DirMngr is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * DirMngr is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <npth.h>
29
30 #include "dirmngr.h"
31 #include "misc.h"
32 #include "crlfetch.h"
33 #include "certcache.h"
34
35
36 #define MAX_EXTRA_CACHED_CERTS 1000
37
38 /* Constants used to classify search patterns.  */
39 enum pattern_class
40   {
41     PATTERN_UNKNOWN = 0,
42     PATTERN_EMAIL,
43     PATTERN_EMAIL_SUBSTR,
44     PATTERN_FINGERPRINT16,
45     PATTERN_FINGERPRINT20,
46     PATTERN_SHORT_KEYID,
47     PATTERN_LONG_KEYID,
48     PATTERN_SUBJECT,
49     PATTERN_SERIALNO,
50     PATTERN_SERIALNO_ISSUER,
51     PATTERN_ISSUER,
52     PATTERN_SUBSTR
53   };
54
55
56 /* A certificate cache item.  This consists of a the KSBA cert object
57    and some meta data for easier lookup.  We use a hash table to keep
58    track of all items and use the (randomly distributed) first byte of
59    the fingerprint directly as the hash which makes it pretty easy. */
60 struct cert_item_s
61 {
62   struct cert_item_s *next; /* Next item with the same hash value. */
63   ksba_cert_t cert;         /* The KSBA cert object or NULL is this is
64                                not a valid item.  */
65   unsigned char fpr[20];    /* The fingerprint of this object. */
66   char *issuer_dn;          /* The malloced issuer DN.  */
67   ksba_sexp_t sn;           /* The malloced serial number  */
68   char *subject_dn;         /* The malloced subject DN - maybe NULL.  */
69   struct
70   {
71     unsigned int loaded:1;  /* It has been explicitly loaded.  */
72     unsigned int trusted:1; /* This is a trusted root certificate.  */
73   } flags;
74 };
75 typedef struct cert_item_s *cert_item_t;
76
77 /* The actual cert cache consisting of 256 slots for items indexed by
78    the first byte of the fingerprint.  */
79 static cert_item_t cert_cache[256];
80
81 /* This is the global cache_lock variable. In general locking is not
82    needed but it would take extra efforts to make sure that no
83    indirect use of npth functions is done, so we simply lock it
84    always.  Note: We can't use static initialization, as that is not
85    available through w32-pth.  */
86 static npth_rwlock_t cert_cache_lock;
87
88 /* Flag to track whether the cache has been initialized.  */
89 static int initialization_done;
90
91 /* Total number of certificates loaded during initialization and
92    cached during operation.  */
93 static unsigned int total_loaded_certificates;
94 static unsigned int total_extra_certificates;
95
96
97 \f
98 /* Helper to do the cache locking.  */
99 static void
100 init_cache_lock (void)
101 {
102   int err;
103
104   err = npth_rwlock_init (&cert_cache_lock, NULL);
105   if (err)
106     log_fatal (_("can't initialize certificate cache lock: %s\n"),
107                strerror (err));
108 }
109
110 static void
111 acquire_cache_read_lock (void)
112 {
113   int err;
114
115   err = npth_rwlock_rdlock (&cert_cache_lock);
116   if (err)
117     log_fatal (_("can't acquire read lock on the certificate cache: %s\n"),
118                strerror (err));
119 }
120
121 static void
122 acquire_cache_write_lock (void)
123 {
124   int err;
125
126   err = npth_rwlock_wrlock (&cert_cache_lock);
127   if (err)
128     log_fatal (_("can't acquire write lock on the certificate cache: %s\n"),
129                strerror (err));
130 }
131
132 static void
133 release_cache_lock (void)
134 {
135   int err;
136
137   err = npth_rwlock_unlock (&cert_cache_lock);
138   if (err)
139     log_fatal (_("can't release lock on the certificate cache: %s\n"),
140                strerror (err));
141 }
142
143
144 /* Return false if both serial numbers match.  Can't be used for
145    sorting. */
146 static int
147 compare_serialno (ksba_sexp_t serial1, ksba_sexp_t serial2 )
148 {
149   unsigned char *a = serial1;
150   unsigned char *b = serial2;
151   return cmp_simple_canon_sexp (a, b);
152 }
153
154
155
156 /* Return a malloced canonical S-Expression with the serial number
157    converted from the hex string HEXSN.  Return NULL on memory
158    error. */
159 ksba_sexp_t
160 hexsn_to_sexp (const char *hexsn)
161 {
162   char *buffer, *p;
163   size_t len;
164   char numbuf[40];
165
166   len = unhexify (NULL, hexsn);
167   snprintf (numbuf, sizeof numbuf, "(%u:", (unsigned int)len);
168   buffer = xtrymalloc (strlen (numbuf) + len + 2 );
169   if (!buffer)
170     return NULL;
171   p = stpcpy (buffer, numbuf);
172   len = unhexify (p, hexsn);
173   p[len] = ')';
174   p[len+1] = 0;
175
176   return buffer;
177 }
178
179
180 /* Compute the fingerprint of the certificate CERT and put it into
181    the 20 bytes large buffer DIGEST.  Return address of this buffer.  */
182 unsigned char *
183 cert_compute_fpr (ksba_cert_t cert, unsigned char *digest)
184 {
185   gpg_error_t err;
186   gcry_md_hd_t md;
187
188   err = gcry_md_open (&md, GCRY_MD_SHA1, 0);
189   if (err)
190     log_fatal ("gcry_md_open failed: %s\n", gpg_strerror (err));
191
192   err = ksba_cert_hash (cert, 0, HASH_FNC, md);
193   if (err)
194     {
195       log_error ("oops: ksba_cert_hash failed: %s\n", gpg_strerror (err));
196       memset (digest, 0xff, 20); /* Use a dummy value. */
197     }
198   else
199     {
200       gcry_md_final (md);
201       memcpy (digest, gcry_md_read (md, GCRY_MD_SHA1), 20);
202     }
203   gcry_md_close (md);
204   return digest;
205 }
206
207
208 /* Cleanup one slot.  This releases all resourses but keeps the actual
209    slot in the cache marked for reuse. */
210 static void
211 clean_cache_slot (cert_item_t ci)
212 {
213   ksba_cert_t cert;
214
215   if (!ci->cert)
216     return; /* Already cleaned.  */
217
218   ksba_free (ci->sn);
219   ci->sn = NULL;
220   ksba_free (ci->issuer_dn);
221   ci->issuer_dn = NULL;
222   ksba_free (ci->subject_dn);
223   ci->subject_dn = NULL;
224   cert = ci->cert;
225   ci->cert = NULL;
226
227   ksba_cert_release (cert);
228 }
229
230
231 /* Put the certificate CERT into the cache.  It is assumed that the
232    cache is locked while this function is called. If FPR_BUFFER is not
233    NULL the fingerprint of the certificate will be stored there.
234    FPR_BUFFER neds to point to a buffer of at least 20 bytes. The
235    fingerprint will be stored on success or when the function returns
236    gpg_err_code(GPG_ERR_DUP_VALUE). */
237 static gpg_error_t
238 put_cert (ksba_cert_t cert, int is_loaded, int is_trusted, void *fpr_buffer)
239 {
240   unsigned char help_fpr_buffer[20], *fpr;
241   cert_item_t ci;
242
243   fpr = fpr_buffer? fpr_buffer : &help_fpr_buffer;
244
245   /* If we already reached the caching limit, drop a couple of certs
246      from the cache.  Our dropping strategy is simple: We keep a
247      static index counter and use this to start looking for
248      certificates, then we drop 5 percent of the oldest certificates
249      starting at that index.  For a large cache this is a fair way of
250      removing items. An LRU strategy would be better of course.
251      Because we append new entries to the head of the list and we want
252      to remove old ones first, we need to do this from the tail.  The
253      implementation is not very efficient but compared to the long
254      time it takes to retrieve a certifciate from an external resource
255      it seems to be reasonable. */
256   if (!is_loaded && total_extra_certificates >= MAX_EXTRA_CACHED_CERTS)
257     {
258       static int idx;
259       cert_item_t ci_mark;
260       int i;
261       unsigned int drop_count;
262
263       drop_count = MAX_EXTRA_CACHED_CERTS / 20;
264       if (drop_count < 2)
265         drop_count = 2;
266
267       log_info (_("dropping %u certificates from the cache\n"), drop_count);
268       assert (idx < 256);
269       for (i=idx; drop_count; i = ((i+1)%256))
270         {
271           ci_mark = NULL;
272           for (ci = cert_cache[i]; ci; ci = ci->next)
273             if (ci->cert && !ci->flags.loaded)
274               ci_mark = ci;
275           if (ci_mark)
276             {
277               clean_cache_slot (ci_mark);
278               drop_count--;
279               total_extra_certificates--;
280             }
281         }
282       if (i==idx)
283         idx++;
284       else
285         idx = i;
286       idx %= 256;
287     }
288
289   cert_compute_fpr (cert, fpr);
290   for (ci=cert_cache[*fpr]; ci; ci = ci->next)
291     if (ci->cert && !memcmp (ci->fpr, fpr, 20))
292       return gpg_error (GPG_ERR_DUP_VALUE);
293   /* Try to reuse an existing entry.  */
294   for (ci=cert_cache[*fpr]; ci; ci = ci->next)
295     if (!ci->cert)
296       break;
297   if (!ci)
298     { /* No: Create a new entry.  */
299       ci = xtrycalloc (1, sizeof *ci);
300       if (!ci)
301         return gpg_error_from_errno (errno);
302       ci->next = cert_cache[*fpr];
303       cert_cache[*fpr] = ci;
304     }
305   else
306     memset (&ci->flags, 0, sizeof ci->flags);
307
308   ksba_cert_ref (cert);
309   ci->cert = cert;
310   memcpy (ci->fpr, fpr, 20);
311   ci->sn = ksba_cert_get_serial (cert);
312   ci->issuer_dn = ksba_cert_get_issuer (cert, 0);
313   if (!ci->issuer_dn || !ci->sn)
314     {
315       clean_cache_slot (ci);
316       return gpg_error (GPG_ERR_INV_CERT_OBJ);
317     }
318   ci->subject_dn = ksba_cert_get_subject (cert, 0);
319   ci->flags.loaded  = !!is_loaded;
320   ci->flags.trusted = !!is_trusted;
321
322   if (is_loaded)
323     total_loaded_certificates++;
324   else
325     total_extra_certificates++;
326
327   return 0;
328 }
329
330
331 /* Load certificates from the directory DIRNAME.  All certificates
332    matching the pattern "*.crt" or "*.der"  are loaded.  We assume that
333    certificates are DER encoded and not PEM encapsulated. The cache
334    should be in a locked state when calling this function.  */
335 static gpg_error_t
336 load_certs_from_dir (const char *dirname, int are_trusted)
337 {
338   gpg_error_t err;
339   DIR *dir;
340   struct dirent *ep;
341   char *p;
342   size_t n;
343   estream_t fp;
344   ksba_reader_t reader;
345   ksba_cert_t cert;
346   char *fname = NULL;
347
348   dir = opendir (dirname);
349   if (!dir)
350     {
351       return 0; /* We do not consider this a severe error.  */
352     }
353
354   while ( (ep=readdir (dir)) )
355     {
356       p = ep->d_name;
357       if (*p == '.' || !*p)
358         continue; /* Skip any hidden files and invalid entries.  */
359       n = strlen (p);
360       if ( n < 5 || (strcmp (p+n-4,".crt") && strcmp (p+n-4,".der")))
361         continue; /* Not the desired "*.crt" or "*.der" pattern.  */
362
363       xfree (fname);
364       fname = make_filename (dirname, p, NULL);
365       fp = es_fopen (fname, "rb");
366       if (!fp)
367         {
368           log_error (_("can't open '%s': %s\n"),
369                      fname, strerror (errno));
370           continue;
371         }
372
373       err = create_estream_ksba_reader (&reader, fp);
374       if (err)
375         {
376           es_fclose (fp);
377           continue;
378         }
379
380       err = ksba_cert_new (&cert);
381       if (!err)
382         err = ksba_cert_read_der (cert, reader);
383       ksba_reader_release (reader);
384       es_fclose (fp);
385       if (err)
386         {
387           log_error (_("can't parse certificate '%s': %s\n"),
388                      fname, gpg_strerror (err));
389           ksba_cert_release (cert);
390           continue;
391         }
392
393       err = put_cert (cert, 1, are_trusted, NULL);
394       if (gpg_err_code (err) == GPG_ERR_DUP_VALUE)
395         log_info (_("certificate '%s' already cached\n"), fname);
396       else if (!err)
397         {
398           if (are_trusted)
399             log_info (_("trusted certificate '%s' loaded\n"), fname);
400           else
401             log_info (_("certificate '%s' loaded\n"), fname);
402           if (opt.verbose)
403             {
404               p = get_fingerprint_hexstring_colon (cert);
405               log_info (_("  SHA1 fingerprint = %s\n"), p);
406               xfree (p);
407
408               cert_log_name (_("   issuer ="), cert);
409               cert_log_subject (_("  subject ="), cert);
410             }
411         }
412       else
413         log_error (_("error loading certificate '%s': %s\n"),
414                      fname, gpg_strerror (err));
415       ksba_cert_release (cert);
416     }
417
418   xfree (fname);
419   closedir (dir);
420   return 0;
421 }
422
423
424 /* Initialize the certificate cache if not yet done.  */
425 void
426 cert_cache_init (void)
427 {
428   char *dname;
429
430   if (initialization_done)
431     return;
432   init_cache_lock ();
433   acquire_cache_write_lock ();
434
435   dname = make_filename (gnupg_sysconfdir (), "trusted-certs", NULL);
436   load_certs_from_dir (dname, 1);
437   xfree (dname);
438
439   dname = make_filename (gnupg_sysconfdir (), "extra-certs", NULL);
440   load_certs_from_dir (dname, 0);
441   xfree (dname);
442
443   initialization_done = 1;
444   release_cache_lock ();
445
446   cert_cache_print_stats ();
447 }
448
449 /* Deinitialize the certificate cache.  With FULL set to true even the
450    unused certificate slots are released. */
451 void
452 cert_cache_deinit (int full)
453 {
454   cert_item_t ci, ci2;
455   int i;
456
457   if (!initialization_done)
458     return;
459
460   acquire_cache_write_lock ();
461
462   for (i=0; i < 256; i++)
463     for (ci=cert_cache[i]; ci; ci = ci->next)
464       clean_cache_slot (ci);
465
466   if (full)
467     {
468       for (i=0; i < 256; i++)
469         {
470           for (ci=cert_cache[i]; ci; ci = ci2)
471             {
472               ci2 = ci->next;
473               xfree (ci);
474             }
475           cert_cache[i] = NULL;
476         }
477     }
478
479   total_loaded_certificates = 0;
480   total_extra_certificates = 0;
481   initialization_done = 0;
482   release_cache_lock ();
483 }
484
485 /* Print some statistics to the log file.  */
486 void
487 cert_cache_print_stats (void)
488 {
489   log_info (_("permanently loaded certificates: %u\n"),
490             total_loaded_certificates);
491   log_info (_("    runtime cached certificates: %u\n"),
492             total_extra_certificates);
493 }
494
495
496 /* Put CERT into the certificate cache.  */
497 gpg_error_t
498 cache_cert (ksba_cert_t cert)
499 {
500   gpg_error_t err;
501
502   acquire_cache_write_lock ();
503   err = put_cert (cert, 0, 0, NULL);
504   release_cache_lock ();
505   if (gpg_err_code (err) == GPG_ERR_DUP_VALUE)
506     log_info (_("certificate already cached\n"));
507   else if (!err)
508     log_info (_("certificate cached\n"));
509   else
510     log_error (_("error caching certificate: %s\n"), gpg_strerror (err));
511   return err;
512 }
513
514
515 /* Put CERT into the certificate cache and store the fingerprint of
516    the certificate into FPR_BUFFER.  If the certificate is already in
517    the cache do not print a warning; just store the
518    fingerprint. FPR_BUFFER needs to be at least 20 bytes. */
519 gpg_error_t
520 cache_cert_silent (ksba_cert_t cert, void *fpr_buffer)
521 {
522   gpg_error_t err;
523
524   acquire_cache_write_lock ();
525   err = put_cert (cert, 0, 0, fpr_buffer);
526   release_cache_lock ();
527   if (gpg_err_code (err) == GPG_ERR_DUP_VALUE)
528     err = 0;
529   if (err)
530     log_error (_("error caching certificate: %s\n"), gpg_strerror (err));
531   return err;
532 }
533
534
535 \f
536 /* Return a certificate object for the given fingerprint.  FPR is
537    expected to be a 20 byte binary SHA-1 fingerprint.  If no matching
538    certificate is available in the cache NULL is returned.  The caller
539    must release a returned certificate.  Note that although we are
540    using reference counting the caller should not just compare the
541    pointers to check for identical certificates. */
542 ksba_cert_t
543 get_cert_byfpr (const unsigned char *fpr)
544 {
545   cert_item_t ci;
546
547   acquire_cache_read_lock ();
548   for (ci=cert_cache[*fpr]; ci; ci = ci->next)
549     if (ci->cert && !memcmp (ci->fpr, fpr, 20))
550       {
551         ksba_cert_ref (ci->cert);
552         release_cache_lock ();
553         return ci->cert;
554       }
555
556   release_cache_lock ();
557   return NULL;
558 }
559
560 /* Return a certificate object for the given fingerprint.  STRING is
561    expected to be a SHA-1 fingerprint in standard hex notation with or
562    without colons.  If no matching certificate is available in the
563    cache NULL is returned.  The caller must release a returned
564    certificate.  Note that although we are using reference counting
565    the caller should not just compare the pointers to check for
566    identical certificates. */
567 ksba_cert_t
568 get_cert_byhexfpr (const char *string)
569 {
570   unsigned char fpr[20];
571   const char *s;
572   int i;
573
574   if (strchr (string, ':'))
575     {
576       for (s=string,i=0; i < 20 && hexdigitp (s) && hexdigitp(s+1);)
577         {
578           if (s[2] && s[2] != ':')
579             break; /* Invalid string. */
580           fpr[i++] = xtoi_2 (s);
581           s += 2;
582           if (i!= 20 && *s == ':')
583             s++;
584         }
585     }
586   else
587     {
588       for (s=string,i=0; i < 20 && hexdigitp (s) && hexdigitp(s+1); s+=2 )
589         fpr[i++] = xtoi_2 (s);
590     }
591   if (i!=20 || *s)
592     {
593       log_error (_("invalid SHA1 fingerprint string '%s'\n"), string);
594       return NULL;
595     }
596
597   return get_cert_byfpr (fpr);
598 }
599
600
601
602 /* Return the certificate matching ISSUER_DN and SERIALNO.  */
603 ksba_cert_t
604 get_cert_bysn (const char *issuer_dn, ksba_sexp_t serialno)
605 {
606   /* Simple and inefficient implementation.   fixme! */
607   cert_item_t ci;
608   int i;
609
610   acquire_cache_read_lock ();
611   for (i=0; i < 256; i++)
612     {
613       for (ci=cert_cache[i]; ci; ci = ci->next)
614         if (ci->cert && !strcmp (ci->issuer_dn, issuer_dn)
615             && !compare_serialno (ci->sn, serialno))
616           {
617             ksba_cert_ref (ci->cert);
618             release_cache_lock ();
619             return ci->cert;
620           }
621     }
622
623   release_cache_lock ();
624   return NULL;
625 }
626
627
628 /* Return the certificate matching ISSUER_DN.  SEQ should initially be
629    set to 0 and bumped up to get the next issuer with that DN. */
630 ksba_cert_t
631 get_cert_byissuer (const char *issuer_dn, unsigned int seq)
632 {
633   /* Simple and very inefficient implementation and API.  fixme! */
634   cert_item_t ci;
635   int i;
636
637   acquire_cache_read_lock ();
638   for (i=0; i < 256; i++)
639     {
640       for (ci=cert_cache[i]; ci; ci = ci->next)
641         if (ci->cert && !strcmp (ci->issuer_dn, issuer_dn))
642           if (!seq--)
643             {
644               ksba_cert_ref (ci->cert);
645               release_cache_lock ();
646               return ci->cert;
647             }
648     }
649
650   release_cache_lock ();
651   return NULL;
652 }
653
654
655 /* Return the certificate matching SUBJECT_DN.  SEQ should initially be
656    set to 0 and bumped up to get the next subject with that DN. */
657 ksba_cert_t
658 get_cert_bysubject (const char *subject_dn, unsigned int seq)
659 {
660   /* Simple and very inefficient implementation and API.  fixme! */
661   cert_item_t ci;
662   int i;
663
664   if (!subject_dn)
665     return NULL;
666
667   acquire_cache_read_lock ();
668   for (i=0; i < 256; i++)
669     {
670       for (ci=cert_cache[i]; ci; ci = ci->next)
671         if (ci->cert && ci->subject_dn
672             && !strcmp (ci->subject_dn, subject_dn))
673           if (!seq--)
674             {
675               ksba_cert_ref (ci->cert);
676               release_cache_lock ();
677               return ci->cert;
678             }
679     }
680
681   release_cache_lock ();
682   return NULL;
683 }
684
685
686
687 /* Return a value describing the the class of PATTERN.  The offset of
688    the actual string to be used for the comparison is stored at
689    R_OFFSET.  The offset of the serialnumer is stored at R_SN_OFFSET. */
690 static enum pattern_class
691 classify_pattern (const char *pattern, size_t *r_offset, size_t *r_sn_offset)
692 {
693   enum pattern_class result;
694   const char *s;
695   int hexprefix = 0;
696   int hexlength;
697
698   *r_offset = *r_sn_offset = 0;
699
700   /* Skip leading spaces. */
701   for(s = pattern; *s && spacep (s); s++ )
702     ;
703
704   switch (*s)
705     {
706     case 0:  /* Empty string is an error. */
707       result = PATTERN_UNKNOWN;
708       break;
709
710     case '.': /* An email address, compare from end.  */
711       result = PATTERN_UNKNOWN;  /* Not implemented.  */
712       break;
713
714     case '<': /* An email address.  */
715       result = PATTERN_EMAIL;
716       s++;
717       break;
718
719     case '@': /* Part of an email address.  */
720       result = PATTERN_EMAIL_SUBSTR;
721       s++;
722       break;
723
724     case '=':  /* Exact compare. */
725       result = PATTERN_UNKNOWN; /* Does not make sense for X.509.  */
726       break;
727
728     case '*':  /* Case insensitive substring search.  */
729       result = PATTERN_SUBSTR;
730       s++;
731       break;
732
733     case '+':  /* Compare individual words. */
734       result = PATTERN_UNKNOWN;  /* Not implemented.  */
735       break;
736
737     case '/': /* Subject's DN. */
738       s++;
739       if (!*s || spacep (s))
740         result = PATTERN_UNKNOWN; /* No DN or prefixed with a space. */
741       else
742         result = PATTERN_SUBJECT;
743       break;
744
745     case '#': /* Serial number or issuer DN. */
746       {
747         const char *si;
748
749         s++;
750         if ( *s == '/')
751           {
752             /* An issuer's DN is indicated by "#/" */
753             s++;
754             if (!*s || spacep (s))
755               result = PATTERN_UNKNOWN; /* No DN or prefixed with a space. */
756             else
757               result = PATTERN_ISSUER;
758           }
759         else
760           { /* Serialnumber + optional issuer ID. */
761             for (si=s; *si && *si != '/'; si++)
762               if (!strchr("01234567890abcdefABCDEF", *si))
763                 break;
764             if (*si && *si != '/')
765               result = PATTERN_UNKNOWN; /* Invalid digit in serial number. */
766             else
767               {
768                 *r_sn_offset = s - pattern;
769                 if (!*si)
770                   result = PATTERN_SERIALNO;
771                 else
772                   {
773                     s = si+1;
774                     if (!*s || spacep (s))
775                       result = PATTERN_UNKNOWN; /* No DN or prefixed
776                                                    with a space. */
777                     else
778                       result = PATTERN_SERIALNO_ISSUER;
779                   }
780               }
781           }
782       }
783       break;
784
785     case ':': /* Unified fingerprint. */
786       {
787         const char *se, *si;
788         int i;
789
790         se = strchr (++s, ':');
791         if (!se)
792           result = PATTERN_UNKNOWN;
793         else
794           {
795             for (i=0, si=s; si < se; si++, i++ )
796               if (!strchr("01234567890abcdefABCDEF", *si))
797                 break;
798             if ( si < se )
799               result = PATTERN_UNKNOWN; /* Invalid digit. */
800             else if (i == 32)
801               result = PATTERN_FINGERPRINT16;
802             else if (i == 40)
803               result = PATTERN_FINGERPRINT20;
804             else
805               result = PATTERN_UNKNOWN; /* Invalid length for a fingerprint. */
806           }
807       }
808       break;
809
810     case '&': /* Keygrip. */
811       result = PATTERN_UNKNOWN;  /* Not implemented.  */
812       break;
813
814     default:
815       if (s[0] == '0' && s[1] == 'x')
816         {
817           hexprefix = 1;
818           s += 2;
819         }
820
821       hexlength = strspn(s, "0123456789abcdefABCDEF");
822
823       /* Check if a hexadecimal number is terminated by EOS or blank. */
824       if (hexlength && s[hexlength] && !spacep (s+hexlength))
825         {
826           /* If the "0x" prefix is used a correct termination is required. */
827           if (hexprefix)
828             {
829               result = PATTERN_UNKNOWN;
830               break; /* switch */
831             }
832           hexlength = 0;  /* Not a hex number.  */
833         }
834
835       if (hexlength == 8 || (!hexprefix && hexlength == 9 && *s == '0'))
836         {
837           if (hexlength == 9)
838             s++;
839           result = PATTERN_SHORT_KEYID;
840         }
841       else if (hexlength == 16 || (!hexprefix && hexlength == 17 && *s == '0'))
842         {
843           if (hexlength == 17)
844             s++;
845           result = PATTERN_LONG_KEYID;
846         }
847       else if (hexlength == 32 || (!hexprefix && hexlength == 33 && *s == '0'))
848         {
849           if (hexlength == 33)
850             s++;
851           result = PATTERN_FINGERPRINT16;
852         }
853       else if (hexlength == 40 || (!hexprefix && hexlength == 41 && *s == '0'))
854         {
855           if (hexlength == 41)
856             s++;
857           result = PATTERN_FINGERPRINT20;
858         }
859       else if (!hexprefix)
860         {
861           /* The fingerprints used with X.509 are often delimited by
862              colons, so we try to single this case out. */
863           result = PATTERN_UNKNOWN;
864           hexlength = strspn (s, ":0123456789abcdefABCDEF");
865           if (hexlength == 59 && (!s[hexlength] || spacep (s+hexlength)))
866             {
867               int i, c;
868
869               for (i=0; i < 20; i++, s += 3)
870                 {
871                   c = hextobyte(s);
872                   if (c == -1 || (i < 19 && s[2] != ':'))
873                     break;
874                 }
875               if (i == 20)
876                 result = PATTERN_FINGERPRINT20;
877             }
878           if (result == PATTERN_UNKNOWN) /* Default to substring match. */
879             {
880               result = PATTERN_SUBSTR;
881             }
882         }
883       else /* A hex number with a prefix but with a wrong length.  */
884         result = PATTERN_UNKNOWN;
885     }
886
887   if (result != PATTERN_UNKNOWN)
888     *r_offset = s - pattern;
889   return result;
890 }
891
892
893
894 /* Given PATTERN, which is a string as used by GnuPG to specify a
895    certificate, return all matching certificates by calling the
896    supplied function RETFNC.  */
897 gpg_error_t
898 get_certs_bypattern (const char *pattern,
899                      gpg_error_t (*retfnc)(void*,ksba_cert_t),
900                      void *retfnc_data)
901 {
902   gpg_error_t err = GPG_ERR_BUG;
903   enum pattern_class class;
904   size_t offset, sn_offset;
905   const char *hexserialno;
906   ksba_sexp_t serialno = NULL;
907   ksba_cert_t cert = NULL;
908   unsigned int seq;
909
910   if (!pattern || !retfnc)
911     return gpg_error (GPG_ERR_INV_ARG);
912
913   class = classify_pattern (pattern, &offset, &sn_offset);
914   hexserialno = pattern + sn_offset;
915   pattern += offset;
916   switch (class)
917     {
918     case PATTERN_UNKNOWN:
919       err = gpg_error (GPG_ERR_INV_NAME);
920       break;
921
922     case PATTERN_FINGERPRINT20:
923       cert = get_cert_byhexfpr (pattern);
924       err = cert? 0 : gpg_error (GPG_ERR_NOT_FOUND);
925       break;
926
927     case PATTERN_SERIALNO_ISSUER:
928       serialno = hexsn_to_sexp (hexserialno);
929       if (!serialno)
930         err = gpg_error_from_syserror ();
931       else
932         {
933           cert = get_cert_bysn (pattern, serialno);
934           err = cert? 0 : gpg_error (GPG_ERR_NOT_FOUND);
935         }
936       break;
937
938     case PATTERN_ISSUER:
939       for (seq=0,err=0; !err && (cert = get_cert_byissuer (pattern, seq)); seq++)
940         {
941           err = retfnc (retfnc_data, cert);
942           ksba_cert_release (cert);
943           cert = NULL;
944         }
945       if (!err && !seq)
946         err = gpg_error (GPG_ERR_NOT_FOUND);
947       break;
948
949     case PATTERN_SUBJECT:
950       for (seq=0,err=0; !err && (cert = get_cert_bysubject (pattern, seq));seq++)
951         {
952           err = retfnc (retfnc_data, cert);
953           ksba_cert_release (cert);
954           cert = NULL;
955         }
956       if (!err && !seq)
957         err = gpg_error (GPG_ERR_NOT_FOUND);
958       break;
959
960     case PATTERN_EMAIL:
961     case PATTERN_EMAIL_SUBSTR:
962     case PATTERN_FINGERPRINT16:
963     case PATTERN_SHORT_KEYID:
964     case PATTERN_LONG_KEYID:
965     case PATTERN_SUBSTR:
966     case PATTERN_SERIALNO:
967       /* Not supported.  */
968       err = gpg_error (GPG_ERR_INV_NAME);
969     }
970
971
972   if (!err && cert)
973     err = retfnc (retfnc_data, cert);
974   ksba_cert_release (cert);
975   xfree (serialno);
976   return err;
977 }
978
979
980
981
982 \f
983 /* Return the certificate matching ISSUER_DN and SERIALNO; if it is
984    not already in the cache, try to find it from other resources.  */
985 ksba_cert_t
986 find_cert_bysn (ctrl_t ctrl, const char *issuer_dn, ksba_sexp_t serialno)
987 {
988   gpg_error_t err;
989   ksba_cert_t cert;
990   cert_fetch_context_t context = NULL;
991   char *hexsn, *buf;
992
993   /* First check whether it has already been cached.  */
994   cert = get_cert_bysn (issuer_dn, serialno);
995   if (cert)
996     return cert;
997
998   /* Ask back to the service requester to return the certificate.
999      This is because we can assume that he already used the
1000      certificate while checking for the CRL. */
1001   hexsn = serial_hex (serialno);
1002   if (!hexsn)
1003     {
1004       log_error ("serial_hex() failed\n");
1005       return NULL;
1006     }
1007   buf = xtrymalloc (1 + strlen (hexsn) + 1 + strlen (issuer_dn) + 1);
1008   if (!buf)
1009     {
1010       log_error ("can't allocate enough memory: %s\n", strerror (errno));
1011       xfree (hexsn);
1012       return NULL;
1013     }
1014   strcpy (stpcpy (stpcpy (stpcpy (buf, "#"), hexsn),"/"), issuer_dn);
1015   xfree (hexsn);
1016   cert = get_cert_local (ctrl, buf);
1017   xfree (buf);
1018   if (cert)
1019     {
1020       cache_cert (cert);
1021       return cert; /* Done. */
1022     }
1023
1024   if (DBG_LOOKUP)
1025     log_debug ("find_cert_bysn: certificate not returned by caller"
1026                " - doing lookup\n");
1027
1028   /* Retrieve the certificate from external resources. */
1029   while (!cert)
1030     {
1031       ksba_sexp_t sn;
1032       char *issdn;
1033
1034       if (!context)
1035         {
1036           err = ca_cert_fetch (ctrl, &context, issuer_dn);
1037           if (err)
1038             {
1039               log_error (_("error fetching certificate by S/N: %s\n"),
1040                          gpg_strerror (err));
1041               break;
1042             }
1043         }
1044
1045       err = fetch_next_ksba_cert (context, &cert);
1046       if (err)
1047         {
1048           log_error (_("error fetching certificate by S/N: %s\n"),
1049                      gpg_strerror (err) );
1050           break;
1051         }
1052
1053       issdn = ksba_cert_get_issuer (cert, 0);
1054       if (strcmp (issuer_dn, issdn))
1055         {
1056           log_debug ("find_cert_bysn: Ooops: issuer DN does not match\n");
1057           ksba_cert_release (cert);
1058           cert = NULL;
1059           ksba_free (issdn);
1060           break;
1061         }
1062
1063       sn = ksba_cert_get_serial (cert);
1064
1065       if (DBG_LOOKUP)
1066         {
1067           log_debug ("   considering certificate (#");
1068           dump_serial (sn);
1069           log_printf ("/");
1070           dump_string (issdn);
1071           log_printf (")\n");
1072         }
1073
1074       if (!compare_serialno (serialno, sn))
1075         {
1076           ksba_free (sn);
1077           ksba_free (issdn);
1078           cache_cert (cert);
1079           if (DBG_LOOKUP)
1080             log_debug ("   found\n");
1081           break; /* Ready.  */
1082         }
1083
1084       ksba_free (sn);
1085       ksba_free (issdn);
1086       ksba_cert_release (cert);
1087       cert = NULL;
1088     }
1089
1090   end_cert_fetch (context);
1091   return cert;
1092 }
1093
1094
1095 /* Return the certificate matching SUBJECT_DN and (if not NULL)
1096    KEYID. If it is not already in the cache, try to find it from other
1097    resources.  Note, that the external search does not work for user
1098    certificates because the LDAP lookup is on the caCertificate
1099    attribute. For our purposes this is just fine.  */
1100 ksba_cert_t
1101 find_cert_bysubject (ctrl_t ctrl, const char *subject_dn, ksba_sexp_t keyid)
1102 {
1103   gpg_error_t err;
1104   int seq;
1105   ksba_cert_t cert = NULL;
1106   cert_fetch_context_t context = NULL;
1107   ksba_sexp_t subj;
1108
1109   /* If we have certificates from an OCSP request we first try to use
1110      them.  This is because these certificates will really be the
1111      required ones and thus even in the case that they can't be
1112      uniquely located by the following code we can use them.  This is
1113      for example required by Telesec certificates where a keyId is
1114      used but the issuer certificate comes without a subject keyId! */
1115   if (ctrl->ocsp_certs && subject_dn)
1116     {
1117       cert_item_t ci;
1118       cert_ref_t cr;
1119       int i;
1120
1121       /* For efficiency reasons we won't use get_cert_bysubject here. */
1122       acquire_cache_read_lock ();
1123       for (i=0; i < 256; i++)
1124         for (ci=cert_cache[i]; ci; ci = ci->next)
1125           if (ci->cert && ci->subject_dn
1126               && !strcmp (ci->subject_dn, subject_dn))
1127             for (cr=ctrl->ocsp_certs; cr; cr = cr->next)
1128               if (!memcmp (ci->fpr, cr->fpr, 20))
1129                 {
1130                   ksba_cert_ref (ci->cert);
1131                   release_cache_lock ();
1132                   return ci->cert; /* We use this certificate. */
1133                 }
1134       release_cache_lock ();
1135       if (DBG_LOOKUP)
1136         log_debug ("find_cert_bysubject: certificate not in ocsp_certs\n");
1137     }
1138
1139
1140   /* First we check whether the certificate is cached.  */
1141   for (seq=0; (cert = get_cert_bysubject (subject_dn, seq)); seq++)
1142     {
1143       if (!keyid)
1144         break; /* No keyid requested, so return the first one found. */
1145       if (!ksba_cert_get_subj_key_id (cert, NULL, &subj)
1146           && !cmp_simple_canon_sexp (keyid, subj))
1147         {
1148           xfree (subj);
1149           break; /* Found matching cert. */
1150         }
1151       xfree (subj);
1152       ksba_cert_release (cert);
1153     }
1154   if (cert)
1155     return cert; /* Done.  */
1156
1157   if (DBG_LOOKUP)
1158     log_debug ("find_cert_bysubject: certificate not in cache\n");
1159
1160   /* Ask back to the service requester to return the certificate.
1161      This is because we can assume that he already used the
1162      certificate while checking for the CRL. */
1163   if (keyid)
1164     cert = get_cert_local_ski (ctrl, subject_dn, keyid);
1165   else
1166     {
1167       /* In contrast to get_cert_local_ski, get_cert_local uses any
1168          passed pattern, so we need to make sure that an exact subject
1169          search is done. */
1170       char *buf;
1171
1172       buf = xtrymalloc (1 + strlen (subject_dn) + 1);
1173       if (!buf)
1174         {
1175           log_error ("can't allocate enough memory: %s\n", strerror (errno));
1176           return NULL;
1177         }
1178       strcpy (stpcpy (buf, "/"), subject_dn);
1179       cert = get_cert_local (ctrl, buf);
1180       xfree (buf);
1181     }
1182   if (cert)
1183     {
1184       cache_cert (cert);
1185       return cert; /* Done. */
1186     }
1187
1188   if (DBG_LOOKUP)
1189     log_debug ("find_cert_bysubject: certificate not returned by caller"
1190                " - doing lookup\n");
1191
1192   /* Locate the certificate using external resources. */
1193   while (!cert)
1194     {
1195       char *subjdn;
1196
1197       if (!context)
1198         {
1199           err = ca_cert_fetch (ctrl, &context, subject_dn);
1200           if (err)
1201             {
1202               log_error (_("error fetching certificate by subject: %s\n"),
1203                          gpg_strerror (err));
1204               break;
1205             }
1206         }
1207
1208       err = fetch_next_ksba_cert (context, &cert);
1209       if (err)
1210         {
1211           log_error (_("error fetching certificate by subject: %s\n"),
1212                      gpg_strerror (err) );
1213           break;
1214         }
1215
1216       subjdn = ksba_cert_get_subject (cert, 0);
1217       if (strcmp (subject_dn, subjdn))
1218         {
1219           log_info ("find_cert_bysubject: subject DN does not match\n");
1220           ksba_cert_release (cert);
1221           cert = NULL;
1222           ksba_free (subjdn);
1223           continue;
1224         }
1225
1226
1227       if (DBG_LOOKUP)
1228         {
1229           log_debug ("   considering certificate (/");
1230           dump_string (subjdn);
1231           log_printf (")\n");
1232         }
1233       ksba_free (subjdn);
1234
1235       /* If no key ID has been provided, we return the first match.  */
1236       if (!keyid)
1237         {
1238           cache_cert (cert);
1239           if (DBG_LOOKUP)
1240             log_debug ("   found\n");
1241           break; /* Ready.  */
1242         }
1243
1244       /* With the key ID given we need to compare it.  */
1245       if (!ksba_cert_get_subj_key_id (cert, NULL, &subj))
1246         {
1247           if (!cmp_simple_canon_sexp (keyid, subj))
1248             {
1249               ksba_free (subj);
1250               cache_cert (cert);
1251               if (DBG_LOOKUP)
1252                 log_debug ("   found\n");
1253               break; /* Ready.  */
1254             }
1255         }
1256
1257       ksba_free (subj);
1258       ksba_cert_release (cert);
1259       cert = NULL;
1260     }
1261
1262   end_cert_fetch (context);
1263   return cert;
1264 }
1265
1266
1267
1268 /* Return 0 if the certificate is a trusted certificate. Returns
1269    GPG_ERR_NOT_TRUSTED if it is not trusted or other error codes in
1270    case of systems errors. */
1271 gpg_error_t
1272 is_trusted_cert (ksba_cert_t cert)
1273 {
1274   unsigned char fpr[20];
1275   cert_item_t ci;
1276
1277   cert_compute_fpr (cert, fpr);
1278
1279   acquire_cache_read_lock ();
1280   for (ci=cert_cache[*fpr]; ci; ci = ci->next)
1281     if (ci->cert && !memcmp (ci->fpr, fpr, 20))
1282       {
1283         if (ci->flags.trusted)
1284           {
1285             release_cache_lock ();
1286             return 0; /* Yes, it is trusted. */
1287           }
1288         break;
1289       }
1290
1291   release_cache_lock ();
1292   return gpg_error (GPG_ERR_NOT_TRUSTED);
1293 }
1294
1295
1296 \f
1297 /* Given the certificate CERT locate the issuer for this certificate
1298    and return it at R_CERT.  Returns 0 on success or
1299    GPG_ERR_NOT_FOUND.  */
1300 gpg_error_t
1301 find_issuing_cert (ctrl_t ctrl, ksba_cert_t cert, ksba_cert_t *r_cert)
1302 {
1303   gpg_error_t err;
1304   char *issuer_dn;
1305   ksba_cert_t issuer_cert = NULL;
1306   ksba_name_t authid;
1307   ksba_sexp_t authidno;
1308   ksba_sexp_t keyid;
1309
1310   *r_cert = NULL;
1311
1312   issuer_dn = ksba_cert_get_issuer (cert, 0);
1313   if (!issuer_dn)
1314     {
1315       log_error (_("no issuer found in certificate\n"));
1316       err = gpg_error (GPG_ERR_BAD_CERT);
1317       goto leave;
1318     }
1319
1320   /* First we need to check whether we can return that certificate
1321      using the authorithyKeyIdentifier.  */
1322   err = ksba_cert_get_auth_key_id (cert, &keyid, &authid, &authidno);
1323   if (err)
1324     {
1325       log_info (_("error getting authorityKeyIdentifier: %s\n"),
1326                 gpg_strerror (err));
1327     }
1328   else
1329     {
1330       const char *s = ksba_name_enum (authid, 0);
1331       if (s && *authidno)
1332         {
1333           issuer_cert = find_cert_bysn (ctrl, s, authidno);
1334         }
1335       if (!issuer_cert && keyid)
1336         {
1337           /* Not found by issuer+s/n.  Now that we have an AKI
1338              keyIdentifier look for a certificate with a matching
1339              SKI. */
1340           issuer_cert = find_cert_bysubject (ctrl, issuer_dn, keyid);
1341         }
1342       /* Print a note so that the user does not feel too helpless when
1343          an issuer certificate was found and gpgsm prints BAD
1344          signature because it is not the correct one. */
1345       if (!issuer_cert)
1346         {
1347           log_info ("issuer certificate ");
1348           if (keyid)
1349             {
1350               log_printf ("{");
1351               dump_serial (keyid);
1352               log_printf ("} ");
1353             }
1354           if (authidno)
1355             {
1356               log_printf ("(#");
1357               dump_serial (authidno);
1358               log_printf ("/");
1359               dump_string (s);
1360               log_printf (") ");
1361             }
1362           log_printf ("not found using authorityKeyIdentifier\n");
1363         }
1364       ksba_name_release (authid);
1365       xfree (authidno);
1366       xfree (keyid);
1367     }
1368
1369   /* If this did not work, try just with the issuer's name and assume
1370      that there is only one such certificate.  We only look into our
1371      cache then. */
1372   if (err || !issuer_cert)
1373     {
1374       issuer_cert = get_cert_bysubject (issuer_dn, 0);
1375       if (issuer_cert)
1376         err = 0;
1377     }
1378
1379  leave:
1380   if (!err && !issuer_cert)
1381     err = gpg_error (GPG_ERR_NOT_FOUND);
1382
1383   xfree (issuer_dn);
1384
1385   if (err)
1386     ksba_cert_release (issuer_cert);
1387   else
1388     *r_cert = issuer_cert;
1389
1390   return err;
1391 }