~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to sm/certchain.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* certchain.c - certificate chain validation
2
 
 * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2001, 2002, 2003, 2004, 2005,
 
3
 *               2006, 2007, 2008 Free Software Foundation, Inc.
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
6
7
 * GnuPG is free software; you can redistribute it and/or modify
7
8
 * 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
 * the Free Software Foundation; either version 3 of the License, or
9
10
 * (at your option) any later version.
10
11
 *
11
12
 * GnuPG is distributed in the hope that it will be useful,
14
15
 * GNU General Public License for more details.
15
16
 *
16
17
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
19
 */
20
20
 
21
21
#include <config.h>
37
37
#include "keydb.h"
38
38
#include "../kbx/keybox.h" /* for KEYBOX_FLAG_* */
39
39
#include "i18n.h"
40
 
 
 
40
#include "tlv.h"
 
41
 
 
42
 
 
43
/* Object to keep track of certain root certificates. */
 
44
struct marktrusted_info_s
 
45
{
 
46
  struct marktrusted_info_s *next;
 
47
  unsigned char fpr[20];
 
48
};
 
49
static struct marktrusted_info_s *marktrusted_info;
 
50
 
 
51
 
 
52
/* While running the validation function we want to keep track of the
 
53
   certificates in the chain.  This type is used for that.  */
 
54
struct chain_item_s
 
55
{
 
56
  struct chain_item_s *next;
 
57
  ksba_cert_t cert;      /* The certificate.  */
 
58
  int is_root;           /* The certificate is the root certificate.  */
 
59
};
 
60
typedef struct chain_item_s *chain_item_t;
 
61
 
 
62
 
 
63
static int is_root_cert (ksba_cert_t cert,
 
64
                         const char *issuerdn, const char *subjectdn);
 
65
static int get_regtp_ca_info (ctrl_t ctrl, ksba_cert_t cert, int *chainlen);
 
66
 
 
67
 
 
68
/* This function returns true if we already asked during this session
 
69
   whether the root certificate CERT shall be marked as trusted.  */
 
70
static int
 
71
already_asked_marktrusted (ksba_cert_t cert)
 
72
{
 
73
  unsigned char fpr[20];
 
74
  struct marktrusted_info_s *r;
 
75
 
 
76
  gpgsm_get_fingerprint (cert, GCRY_MD_SHA1, fpr, NULL);
 
77
  /* No context switches in the loop! */
 
78
  for (r=marktrusted_info; r; r= r->next)
 
79
    if (!memcmp (r->fpr, fpr, 20))
 
80
      return 1;
 
81
  return 0;
 
82
}
 
83
 
 
84
/* Flag certificate CERT as already asked whether it shall be marked
 
85
   as trusted.  */
 
86
static void
 
87
set_already_asked_marktrusted (ksba_cert_t cert)
 
88
{
 
89
 unsigned char fpr[20];
 
90
 struct marktrusted_info_s *r;
 
91
 
 
92
 gpgsm_get_fingerprint (cert, GCRY_MD_SHA1, fpr, NULL);
 
93
 for (r=marktrusted_info; r; r= r->next)
 
94
   if (!memcmp (r->fpr, fpr, 20))
 
95
     return; /* Already marked. */
 
96
 r = xtrycalloc (1, sizeof *r);
 
97
 if (!r)
 
98
   return;
 
99
 memcpy (r->fpr, fpr, 20);
 
100
 r->next = marktrusted_info;
 
101
 marktrusted_info = r;
 
102
}
41
103
 
42
104
/* If LISTMODE is true, print FORMAT using LISTMODE to FP.  If
43
105
   LISTMODE is false, use the string to print an log_info or, if
44
106
   IS_ERROR is true, and log_error. */
45
107
static void
46
 
do_list (int is_error, int listmode, FILE *fp, const char *format, ...)
 
108
do_list (int is_error, int listmode, estream_t fp, const char *format, ...)
47
109
{
48
110
  va_list arg_ptr;
49
111
 
52
114
    {
53
115
      if (fp)
54
116
        {
55
 
          fputs ("  [", fp);
56
 
          vfprintf (fp, format, arg_ptr);
57
 
          fputs ("]\n", fp);
 
117
          es_fputs ("  [", fp);
 
118
          es_vfprintf (fp, format, arg_ptr);
 
119
          es_fputs ("]\n", fp);
58
120
        }
59
121
    }
60
122
  else
82
144
}
83
145
 
84
146
 
85
 
static int
86
 
unknown_criticals (ksba_cert_t cert, int listmode, FILE *fp)
 
147
/* Return true if CERT has the validityModel extensions and defines
 
148
   the use of the chain model.  */
 
149
static int
 
150
has_validation_model_chain (ksba_cert_t cert, int listmode, estream_t listfp)
 
151
{
 
152
  gpg_error_t err;
 
153
  int idx, yes;
 
154
  const char *oid;
 
155
  size_t off, derlen, objlen, hdrlen;
 
156
  const unsigned char *der;
 
157
  int class, tag, constructed, ndef;
 
158
  char *oidbuf;
 
159
 
 
160
  for (idx=0; !(err=ksba_cert_get_extension (cert, idx,
 
161
                                             &oid, NULL, &off, &derlen));idx++)
 
162
    if (!strcmp (oid, "1.3.6.1.4.1.8301.3.5") )
 
163
      break;
 
164
  if (err)
 
165
    return 0; /* Not found.  */
 
166
  der = ksba_cert_get_image (cert, NULL);
 
167
  if (!der)
 
168
    {
 
169
      err = gpg_error (GPG_ERR_INV_OBJ); /* Oops  */
 
170
      goto leave;
 
171
    }
 
172
  der += off;
 
173
 
 
174
  err = parse_ber_header (&der, &derlen, &class, &tag, &constructed,
 
175
                          &ndef, &objlen, &hdrlen);
 
176
  if (!err && (objlen > derlen || tag != TAG_SEQUENCE))
 
177
    err = gpg_error (GPG_ERR_INV_OBJ);
 
178
  if (err)
 
179
    goto leave;
 
180
  derlen = objlen;
 
181
  err = parse_ber_header (&der, &derlen, &class, &tag, &constructed,
 
182
                          &ndef, &objlen, &hdrlen);
 
183
  if (!err && (objlen > derlen || tag != TAG_OBJECT_ID))
 
184
    err = gpg_error (GPG_ERR_INV_OBJ);
 
185
  if (err)
 
186
    goto leave;
 
187
  oidbuf = ksba_oid_to_str (der, objlen);
 
188
  if (!oidbuf)
 
189
    {
 
190
      err = gpg_error_from_syserror ();
 
191
      goto leave;
 
192
    }
 
193
 
 
194
  if (opt.verbose)
 
195
    do_list (0, listmode, listfp,
 
196
             _("validation model requested by certificate: %s"), 
 
197
              !strcmp (oidbuf, "1.3.6.1.4.1.8301.3.5.1")? _("chain") :
 
198
              !strcmp (oidbuf, "1.3.6.1.4.1.8301.3.5.2")? _("shell") :
 
199
              /* */                                       oidbuf);
 
200
  yes = !strcmp (oidbuf, "1.3.6.1.4.1.8301.3.5.1");
 
201
  ksba_free (oidbuf);
 
202
  return yes;
 
203
 
 
204
 
 
205
 leave:
 
206
  log_error ("error parsing validityModel: %s\n", gpg_strerror (err));
 
207
  return 0;
 
208
}
 
209
 
 
210
 
 
211
 
 
212
static int
 
213
unknown_criticals (ksba_cert_t cert, int listmode, estream_t fp)
87
214
{
88
215
  static const char *known[] = {
89
216
    "2.5.29.15", /* keyUsage */
 
217
    "2.5.29.17", /* subjectAltName
 
218
                    Japanese DoCoMo certs mark them as critical.  PKIX
 
219
                    only requires them as critical if subjectName is
 
220
                    empty.  I don't know whether our code gracefully
 
221
                    handles such empry subjectNames but that is
 
222
                    another story. */
90
223
    "2.5.29.19", /* basic Constraints */
91
224
    "2.5.29.32", /* certificatePolicies */
92
225
    "2.5.29.37", /* extendedKeyUsage - handled by certlist.c */
 
226
    "1.3.6.1.4.1.8301.3.5", /* validityModel - handled here. */
93
227
    NULL
94
228
  };
95
229
  int rc = 0, i, idx, crit;
111
245
          rc = gpg_error (GPG_ERR_UNSUPPORTED_CERT);
112
246
        }
113
247
    }
114
 
  if (err && gpg_err_code (err) != GPG_ERR_EOF)
 
248
  /* We ignore the error codes EOF as well as no-value. The later will
 
249
     occur for certificates with no extensions at all. */
 
250
  if (err
 
251
      && gpg_err_code (err) != GPG_ERR_EOF
 
252
      && gpg_err_code (err) != GPG_ERR_NO_VALUE)
115
253
    rc = err;
116
254
 
117
255
  return rc;
118
256
}
119
257
 
 
258
 
 
259
/* Check whether CERT is an allowed certificate.  This requires that
 
260
   CERT matches all requirements for such a CA, i.e. the
 
261
   BasicConstraints extension.  The function returns 0 on success and
 
262
   the awlloed length of the chain at CHAINLEN. */
120
263
static int
121
 
allowed_ca (ksba_cert_t cert, int *chainlen, int listmode, FILE *fp)
 
264
allowed_ca (ctrl_t ctrl, 
 
265
            ksba_cert_t cert, int *chainlen, int listmode, estream_t fp)
122
266
{
123
267
  gpg_error_t err;
124
268
  int flag;
128
272
    return err;
129
273
  if (!flag)
130
274
    {
 
275
      if (get_regtp_ca_info (ctrl, cert, chainlen))
 
276
        {
 
277
          /* Note that dirmngr takes a different way to cope with such
 
278
             certs. */
 
279
          return 0; /* RegTP issued certificate. */
 
280
        }
 
281
 
131
282
      do_list (1, listmode, fp,_("issuer certificate is not marked as a CA"));
132
283
      return gpg_error (GPG_ERR_BAD_CA_CERT);
133
284
    }
136
287
 
137
288
 
138
289
static int
139
 
check_cert_policy (ksba_cert_t cert, int listmode, FILE *fplist)
 
290
check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
140
291
{
141
292
  gpg_error_t err;
142
293
  char *policies;
145
296
 
146
297
  err = ksba_cert_get_cert_policies (cert, &policies);
147
298
  if (gpg_err_code (err) == GPG_ERR_NO_DATA)
148
 
    return 0; /* no policy given */
 
299
    return 0; /* No policy given. */
149
300
  if (err)
150
301
    return err;
151
302
 
152
 
  /* STRING is a line delimited list of certifiate policies as stored
 
303
  /* STRING is a line delimited list of certificate policies as stored
153
304
     in the certificate.  The line itself is colon delimited where the
154
305
     first field is the OID of the policy and the second field either
155
306
     N or C for normal or critical extension */
182
333
      /* With no critical policies this is only a warning */
183
334
      if (!any_critical)
184
335
        {
185
 
          do_list (0, listmode, fplist,
186
 
                   _("note: non-critical certificate policy not allowed"));
 
336
          if (!opt.quiet)
 
337
            do_list (0, listmode, fplist,
 
338
                     _("note: non-critical certificate policy not allowed"));
187
339
          return 0;
188
340
        }
189
341
      do_list (1, listmode, fplist,
267
419
}
268
420
 
269
421
 
270
 
/* Helper fucntion for find_up.  This resets the key handle and search
 
422
/* Helper function for find_up.  This resets the key handle and search
271
423
   for an issuer ISSUER with a subjectKeyIdentifier of KEYID.  Returns
272
 
   0 obn success or -1 when not found. */
 
424
   0 on success or -1 when not found. */
273
425
static int
274
426
find_up_search_by_keyid (KEYDB_HANDLE kh,
275
427
                         const char *issuer, ksba_sexp_t keyid)
316
468
   external lookup.  KH is the keydb context we are currently using.
317
469
   On success 0 is returned and the certificate may be retrieved from
318
470
   the keydb using keydb_get_cert().  KEYID is the keyIdentifier from
319
 
   the AKI or NULL. */
 
471
   the AKI or NULL.  */
320
472
static int
321
 
find_up_external (KEYDB_HANDLE kh, const char *issuer, ksba_sexp_t keyid)
 
473
find_up_external (ctrl_t ctrl, KEYDB_HANDLE kh,
 
474
                  const char *issuer, ksba_sexp_t keyid)
322
475
{
323
476
  int rc;
324
477
  strlist_t names = NULL;
328
481
      
329
482
  if (opt.verbose)
330
483
    log_info (_("looking up issuer at external location\n"));
331
 
  /* The DIRMNGR process is confused about unknown attributes.  As a
 
484
  /* The Dirmngr process is confused about unknown attributes.  As a
332
485
     quick and ugly hack we locate the CN and use the issuer string
333
486
     starting at this attribite.  Fixme: we should have far better
334
 
     parsing in the dirmngr. */
 
487
     parsing for external lookups in the Dirmngr. */
335
488
  s = strstr (issuer, "CN=");
336
489
  if (!s || s == issuer || s[-1] != ',')
337
490
    s = issuer;
338
 
 
339
491
  pattern = xtrymalloc (strlen (s)+2);
340
492
  if (!pattern)
341
 
    return gpg_error_from_errno (errno);
 
493
    return gpg_error_from_syserror ();
342
494
  strcpy (stpcpy (pattern, "/"), s);
343
495
  add_to_strlist (&names, pattern);
344
496
  xfree (pattern);
345
497
 
346
 
  rc = gpgsm_dirmngr_lookup (NULL, names, find_up_store_certs_cb, &count);
 
498
  rc = gpgsm_dirmngr_lookup (ctrl, names, 0, find_up_store_certs_cb, &count);
347
499
  free_strlist (names);
348
500
 
349
501
  if (opt.verbose)
374
526
}
375
527
 
376
528
 
 
529
/* Helper for find_up().  Ask the dirmngr for the certificate for
 
530
   ISSUER with optional SERIALNO.  KH is the keydb context we are
 
531
   currently using.  With SUBJECT_MODE set, ISSUER is searched as the
 
532
   subject.  On success 0 is returned and the certificate is available
 
533
   in the ephemeral DB.  */
 
534
static int
 
535
find_up_dirmngr (ctrl_t ctrl, KEYDB_HANDLE kh,
 
536
                 ksba_sexp_t serialno, const char *issuer, int subject_mode)
 
537
{
 
538
  int rc;
 
539
  strlist_t names = NULL;
 
540
  int count = 0;
 
541
  char *pattern;
 
542
      
 
543
  if (opt.verbose)
 
544
    log_info (_("looking up issuer from the Dirmngr cache\n"));
 
545
  if (subject_mode)
 
546
    {
 
547
      pattern = xtrymalloc (strlen (issuer)+2);
 
548
      if (pattern)
 
549
        strcpy (stpcpy (pattern, "/"), issuer);
 
550
    }
 
551
  else if (serialno)
 
552
    pattern = gpgsm_format_sn_issuer (serialno, issuer);
 
553
  else
 
554
    {
 
555
      pattern = xtrymalloc (strlen (issuer)+3);
 
556
      if (pattern)
 
557
        strcpy (stpcpy (pattern, "#/"), issuer);
 
558
    }
 
559
  if (!pattern)
 
560
    return gpg_error_from_syserror ();
 
561
  add_to_strlist (&names, pattern);
 
562
  xfree (pattern);
 
563
 
 
564
  rc = gpgsm_dirmngr_lookup (ctrl, names, 1, find_up_store_certs_cb, &count);
 
565
  free_strlist (names);
 
566
 
 
567
  if (opt.verbose)
 
568
    log_info (_("number of matching certificates: %d\n"), count);
 
569
  if (rc && !opt.quiet) 
 
570
    log_info (_("dirmngr cache-only key lookup failed: %s\n"),
 
571
              gpg_strerror (rc));
 
572
  return (!rc && count)? 0 : -1;
 
573
}
 
574
 
 
575
 
 
576
 
377
577
/* Locate issuing certificate for CERT. ISSUER is the name of the
378
578
   issuer used as a fallback if the other methods don't work.  If
379
579
   FIND_NEXT is true, the function shall return the next possible
381
581
   keydb_get_cert on the keyDb context KH will return it.  Returns 0
382
582
   on success, -1 if not found or an error code.  */
383
583
static int
384
 
find_up (KEYDB_HANDLE kh, ksba_cert_t cert, const char *issuer, int find_next)
 
584
find_up (ctrl_t ctrl, KEYDB_HANDLE kh, 
 
585
         ksba_cert_t cert, const char *issuer, int find_next)
385
586
{
386
587
  ksba_name_t authid;
387
588
  ksba_sexp_t authidno;
397
598
          if (rc)
398
599
              keydb_search_reset (kh);
399
600
          
 
601
          /* In case of an error, try to get the certifcate from the
 
602
             dirmngr.  That is done by trying to put that certifcate
 
603
             into the ephemeral DB and let the code below do the
 
604
             actual retrieve.  Thus there is no error checking.
 
605
             Skipped in find_next mode as usual. */
 
606
          if (rc == -1 && !find_next)
 
607
            find_up_dirmngr (ctrl, kh, authidno, s, 0);
 
608
 
400
609
          /* In case of an error try the ephemeral DB.  We can't do
401
610
             that in find_next mode because we can't keep the search
402
611
             state then. */
411
620
                }
412
621
              keydb_set_ephemeral (kh, old);
413
622
            }
414
 
 
 
623
          if (rc) 
 
624
            rc = -1; /* Need to make sure to have this error code. */
415
625
        }
416
626
 
417
627
      if (rc == -1 && keyid && !find_next)
418
628
        {
419
 
          /* Not found by AIK.issuer_sn.  Lets try the AIY.ki
 
629
          /* Not found by AIK.issuer_sn.  Lets try the AIK.ki
420
630
             instead. Loop over all certificates with that issuer as
421
631
             subject and stop for the one with a matching
422
632
             subjectKeyIdentifier. */
 
633
          /* Fixme: Should we also search in the dirmngr?  */
423
634
          rc = find_up_search_by_keyid (kh, issuer, keyid);
424
635
          if (rc)
425
636
            {
432
643
            rc = -1; /* Need to make sure to have this error code. */
433
644
        }
434
645
 
 
646
      /* If we still didn't found it, try to find it via the subject
 
647
         from the dirmngr-cache.  */
 
648
      if (rc == -1 && !find_next)
 
649
        {
 
650
          if (!find_up_dirmngr (ctrl, kh, NULL, issuer, 1))
 
651
            {
 
652
              int old = keydb_set_ephemeral (kh, 1);
 
653
              if (keyid)
 
654
                rc = find_up_search_by_keyid (kh, issuer, keyid);
 
655
              else
 
656
                {
 
657
                  keydb_search_reset (kh);
 
658
                  rc = keydb_search_subject (kh, issuer);
 
659
                }
 
660
              keydb_set_ephemeral (kh, old);
 
661
            }
 
662
          if (rc) 
 
663
            rc = -1; /* Need to make sure to have this error code. */
 
664
        }
 
665
 
435
666
      /* If we still didn't found it, try an external lookup.  */
436
667
      if (rc == -1 && opt.auto_issuer_key_retrieve && !find_next)
437
 
        rc = find_up_external (kh, issuer, keyid);
 
668
        rc = find_up_external (ctrl, kh, issuer, keyid);
438
669
 
439
670
      /* Print a note so that the user does not feel too helpless when
440
671
         an issuer certificate was found and gpgsm prints BAD
441
672
         signature because it is not the correct one. */
442
 
      if (rc == -1)
 
673
      if (rc == -1 && opt.quiet)
 
674
        ;
 
675
      else if (rc == -1)
443
676
        {
444
677
          log_info ("%sissuer certificate ", find_next?"next ":"");
445
678
          if (keyid)
469
702
    rc = keydb_search_subject (kh, issuer);
470
703
  if (rc == -1 && !find_next)
471
704
    {
472
 
      /* Not found, lets see whether we have one in the ephemeral key DB. */
 
705
      /* Also try to get it from the Dirmngr cache.  The function
 
706
         merely puts it into the ephemeral database.  */
 
707
      find_up_dirmngr (ctrl, kh, NULL, issuer, 0);
 
708
 
 
709
      /* Not found, let us see whether we have one in the ephemeral key DB. */
473
710
      int old = keydb_set_ephemeral (kh, 1);
474
711
      if (!old)
475
712
        {
481
718
 
482
719
  /* Still not found.  If enabled, try an external lookup.  */
483
720
  if (rc == -1 && opt.auto_issuer_key_retrieve && !find_next)
484
 
    rc = find_up_external (kh, issuer, NULL);
 
721
    rc = find_up_external (ctrl, kh, issuer, NULL);
485
722
 
486
723
  return rc;
487
724
}
490
727
/* Return the next certificate up in the chain starting at START.
491
728
   Returns -1 when there are no more certificates. */
492
729
int
493
 
gpgsm_walk_cert_chain (ksba_cert_t start, ksba_cert_t *r_next)
 
730
gpgsm_walk_cert_chain (ctrl_t ctrl, ksba_cert_t start, ksba_cert_t *r_next)
494
731
{
495
732
  int rc = 0; 
496
733
  char *issuer = NULL;
520
757
      goto leave;
521
758
    }
522
759
 
523
 
  if (!strcmp (issuer, subject))
 
760
  if (is_root_cert (start, issuer, subject))
524
761
    {
525
762
      rc = -1; /* we are at the root */
526
763
      goto leave; 
527
764
    }
528
765
 
529
 
  rc = find_up (kh, start, issuer, 0);
 
766
  rc = find_up (ctrl, kh, start, issuer, 0);
530
767
  if (rc)
531
768
    {
532
 
      /* it is quite common not to have a certificate, so better don't
533
 
         print an error here */
 
769
      /* It is quite common not to have a certificate, so better don't
 
770
         print an error here.  */
534
771
      if (rc != -1 && opt.verbose > 1)
535
772
        log_error ("failed to find issuer's certificate: rc=%d\n", rc);
536
773
      rc = gpg_error (GPG_ERR_MISSING_CERT);
552
789
}
553
790
 
554
791
 
 
792
/* Helper for gpgsm_is_root_cert.  This one is used if the subject and
 
793
   issuer DNs are already known.  */
 
794
static int
 
795
is_root_cert (ksba_cert_t cert, const char *issuerdn, const char *subjectdn)
 
796
{
 
797
  gpg_error_t err;
 
798
  int result = 0;
 
799
  ksba_sexp_t serialno;
 
800
  ksba_sexp_t ak_keyid;
 
801
  ksba_name_t ak_name;
 
802
  ksba_sexp_t ak_sn;
 
803
  const char *ak_name_str;
 
804
  ksba_sexp_t subj_keyid = NULL;
 
805
 
 
806
  if (!issuerdn || !subjectdn)
 
807
    return 0;  /* No.  */
 
808
 
 
809
  if (strcmp (issuerdn, subjectdn))
 
810
    return 0;  /* No.  */
 
811
 
 
812
  err = ksba_cert_get_auth_key_id (cert, &ak_keyid, &ak_name, &ak_sn);
 
813
  if (err)
 
814
    {
 
815
      if (gpg_err_code (err) == GPG_ERR_NO_DATA)
 
816
        return 1; /* Yes. Without a authorityKeyIdentifier this needs
 
817
                     to be the Root certifcate (our trust anchor).  */
 
818
      log_error ("error getting authorityKeyIdentifier: %s\n",
 
819
                 gpg_strerror (err));
 
820
      return 0; /* Well, it is broken anyway.  Return No. */
 
821
    }
 
822
 
 
823
  serialno = ksba_cert_get_serial (cert);
 
824
  if (!serialno)
 
825
    {
 
826
      log_error ("error getting serialno: %s\n", gpg_strerror (err));
 
827
      goto leave;
 
828
    }
 
829
 
 
830
  /* Check whether the auth name's matches the issuer name+sn.  If
 
831
     that is the case this is a root certificate.  */
 
832
  ak_name_str = ksba_name_enum (ak_name, 0);
 
833
  if (ak_name_str
 
834
      && !strcmp (ak_name_str, issuerdn) 
 
835
      && !cmp_simple_canon_sexp (ak_sn, serialno))
 
836
    {
 
837
      result = 1;  /* Right, CERT is self-signed.  */
 
838
      goto leave;
 
839
    } 
 
840
   
 
841
  /* Similar for the ak_keyid. */
 
842
  if (ak_keyid && !ksba_cert_get_subj_key_id (cert, NULL, &subj_keyid)
 
843
      && !cmp_simple_canon_sexp (ak_keyid, subj_keyid))
 
844
    {
 
845
      result = 1;  /* Right, CERT is self-signed.  */
 
846
      goto leave;
 
847
    } 
 
848
 
 
849
 
 
850
 leave:
 
851
  ksba_free (subj_keyid);
 
852
  ksba_free (ak_keyid);
 
853
  ksba_name_release (ak_name);
 
854
  ksba_free (ak_sn);
 
855
  ksba_free (serialno);
 
856
  return result; 
 
857
}
 
858
 
 
859
 
 
860
 
555
861
/* Check whether the CERT is a root certificate.  Returns True if this
556
862
   is the case. */
557
863
int
563
869
 
564
870
  issuer = ksba_cert_get_issuer (cert, 0);
565
871
  subject = ksba_cert_get_subject (cert, 0);
566
 
  yes = (issuer && subject && !strcmp (issuer, subject));
 
872
  yes = is_root_cert (cert, issuer, subject);
567
873
  xfree (issuer);
568
874
  xfree (subject);
569
875
  return yes;
572
878
 
573
879
/* This is a helper for gpgsm_validate_chain. */
574
880
static gpg_error_t 
575
 
is_cert_still_valid (ctrl_t ctrl, int lm, FILE *fp,
 
881
is_cert_still_valid (ctrl_t ctrl, int force_ocsp, int lm, estream_t fp,
576
882
                     ksba_cert_t subject_cert, ksba_cert_t issuer_cert,
577
883
                     int *any_revoked, int *any_no_crl, int *any_crl_too_old)
578
884
{
579
 
  if (!opt.no_crl_check || ctrl->use_ocsp)
580
 
    {
581
 
      gpg_error_t err;
582
 
 
583
 
      err = gpgsm_dirmngr_isvalid (ctrl,
584
 
                                   subject_cert, issuer_cert, ctrl->use_ocsp);
585
 
      if (err)
586
 
        {
587
 
          /* Fixme: We should change the wording because we may
588
 
             have used OCSP. */
589
 
          switch (gpg_err_code (err))
590
 
            {
591
 
            case GPG_ERR_CERT_REVOKED:
592
 
              do_list (1, lm, fp, _("certificate has been revoked"));
593
 
              *any_revoked = 1;
594
 
              /* Store that in the keybox so that key listings are
595
 
                 able to return the revoked flag.  We don't care
596
 
                 about error, though. */
597
 
              keydb_set_cert_flags (subject_cert, KEYBOX_FLAG_VALIDITY, 0,
598
 
                                    VALIDITY_REVOKED);
599
 
              break;
600
 
            case GPG_ERR_NO_CRL_KNOWN:
601
 
              do_list (1, lm, fp, _("no CRL found for certificate"));
602
 
              *any_no_crl = 1;
603
 
              break;
604
 
            case GPG_ERR_CRL_TOO_OLD:
605
 
              do_list (1, lm, fp, _("the available CRL is too old"));
606
 
              if (!lm)
607
 
                log_info (_("please make sure that the "
608
 
                            "\"dirmngr\" is properly installed\n"));
609
 
              *any_crl_too_old = 1;
610
 
              break;
611
 
            default:
612
 
              do_list (1, lm, fp, _("checking the CRL failed: %s"),
613
 
                       gpg_strerror (err));
614
 
              return err;
615
 
            }
616
 
        }
617
 
    }
618
 
  return 0;
619
 
}
 
885
  gpg_error_t err;
 
886
 
 
887
  if (opt.no_crl_check && !ctrl->use_ocsp)
 
888
    return 0;
 
889
 
 
890
  err = gpgsm_dirmngr_isvalid (ctrl,
 
891
                               subject_cert, issuer_cert, 
 
892
                               force_ocsp? 2 : !!ctrl->use_ocsp);
 
893
  if (err)
 
894
    {
 
895
      if (!lm)
 
896
        gpgsm_cert_log_name (NULL, subject_cert);
 
897
      switch (gpg_err_code (err))
 
898
        {
 
899
        case GPG_ERR_CERT_REVOKED:
 
900
          do_list (1, lm, fp, _("certificate has been revoked"));
 
901
          *any_revoked = 1;
 
902
          /* Store that in the keybox so that key listings are able to
 
903
             return the revoked flag.  We don't care about error,
 
904
             though. */
 
905
          keydb_set_cert_flags (subject_cert, 1, KEYBOX_FLAG_VALIDITY, 0,
 
906
                                ~0, VALIDITY_REVOKED);
 
907
          break;
 
908
 
 
909
        case GPG_ERR_NO_CRL_KNOWN:
 
910
          do_list (1, lm, fp, _("no CRL found for certificate"));
 
911
          *any_no_crl = 1;
 
912
          break;
 
913
 
 
914
        case GPG_ERR_NO_DATA:
 
915
          do_list (1, lm, fp, _("the status of the certificate is unknown"));
 
916
          *any_no_crl = 1;
 
917
          break;
 
918
 
 
919
        case GPG_ERR_CRL_TOO_OLD:
 
920
          do_list (1, lm, fp, _("the available CRL is too old"));
 
921
          if (!lm)
 
922
            log_info (_("please make sure that the "
 
923
                        "\"dirmngr\" is properly installed\n"));
 
924
          *any_crl_too_old = 1;
 
925
          break;
 
926
          
 
927
        default:
 
928
          do_list (1, lm, fp, _("checking the CRL failed: %s"),
 
929
                   gpg_strerror (err));
 
930
          return err;
 
931
        }
 
932
    }
 
933
  return 0;
 
934
}
 
935
 
 
936
 
 
937
/* Helper for gpgsm_validate_chain to check the validity period of
 
938
   SUBJECT_CERT.  The caller needs to pass EXPTIME which will be
 
939
   updated to the nearest expiration time seen.  A DEPTH of 0 indicates
 
940
   the target certifciate, -1 the final root certificate and other
 
941
   values intermediate certificates. */ 
 
942
static gpg_error_t
 
943
check_validity_period (ksba_isotime_t current_time,
 
944
                       ksba_cert_t subject_cert,
 
945
                       ksba_isotime_t exptime,
 
946
                       int listmode, estream_t listfp, int depth)
 
947
{
 
948
  gpg_error_t err;
 
949
  ksba_isotime_t not_before, not_after;
 
950
 
 
951
  err = ksba_cert_get_validity (subject_cert, 0, not_before);
 
952
  if (!err)
 
953
    err = ksba_cert_get_validity (subject_cert, 1, not_after);
 
954
  if (err)
 
955
    {
 
956
      do_list (1, listmode, listfp,
 
957
               _("certificate with invalid validity: %s"), gpg_strerror (err));
 
958
      return gpg_error (GPG_ERR_BAD_CERT);
 
959
    }
 
960
 
 
961
  if (*not_after)
 
962
    {
 
963
      if (!*exptime)
 
964
        gnupg_copy_time (exptime, not_after);
 
965
      else if (strcmp (not_after, exptime) < 0 )
 
966
        gnupg_copy_time (exptime, not_after);
 
967
    }
 
968
 
 
969
  if (*not_before && strcmp (current_time, not_before) < 0 )
 
970
    {
 
971
      do_list (1, listmode, listfp, 
 
972
               depth ==  0 ? _("certificate not yet valid") :
 
973
               depth == -1 ? _("root certificate not yet valid") :
 
974
               /* other */   _("intermediate certificate not yet valid"));
 
975
      if (!listmode)
 
976
        {
 
977
          log_info ("  (valid from ");
 
978
          gpgsm_dump_time (not_before);
 
979
          log_printf (")\n");
 
980
        }
 
981
      return gpg_error (GPG_ERR_CERT_TOO_YOUNG);
 
982
    } 
 
983
           
 
984
  if (*not_after && strcmp (current_time, not_after) > 0 )
 
985
    {
 
986
      do_list (opt.ignore_expiration?0:1, listmode, listfp,
 
987
               depth == 0  ? _("certificate has expired") :
 
988
               depth == -1 ? _("root certificate has expired") :
 
989
               /* other  */  _("intermediate certificate has expired"));
 
990
      if (!listmode)
 
991
        {
 
992
          log_info ("  (expired at ");
 
993
          gpgsm_dump_time (not_after);
 
994
          log_printf (")\n");
 
995
        }
 
996
      if (opt.ignore_expiration)
 
997
        log_info ("WARNING: ignoring expiration\n");
 
998
      else
 
999
        return gpg_error (GPG_ERR_CERT_EXPIRED);
 
1000
    }      
 
1001
      
 
1002
  return 0;
 
1003
}
 
1004
 
 
1005
/* This is a variant of check_validity_period used with the chain
 
1006
   model.  The dextra contraint here is that notBefore and notAfter
 
1007
   must exists and if the additional argument CHECK_TIME is given this
 
1008
   time is used to check the validity period of SUBJECT_CERT.  */
 
1009
static gpg_error_t
 
1010
check_validity_period_cm (ksba_isotime_t current_time,
 
1011
                          ksba_isotime_t check_time,
 
1012
                          ksba_cert_t subject_cert,
 
1013
                          ksba_isotime_t exptime,
 
1014
                          int listmode, estream_t listfp, int depth)
 
1015
{
 
1016
  gpg_error_t err;
 
1017
  ksba_isotime_t not_before, not_after;
 
1018
 
 
1019
  err = ksba_cert_get_validity (subject_cert, 0, not_before);
 
1020
  if (!err)
 
1021
    err = ksba_cert_get_validity (subject_cert, 1, not_after);
 
1022
  if (err)
 
1023
    {
 
1024
      do_list (1, listmode, listfp,
 
1025
               _("certificate with invalid validity: %s"), gpg_strerror (err));
 
1026
      return gpg_error (GPG_ERR_BAD_CERT);
 
1027
    }
 
1028
  if (!*not_before || !*not_after)
 
1029
    {
 
1030
      do_list (1, listmode, listfp,
 
1031
               _("required certificate attributes missing: %s%s%s"),
 
1032
               !*not_before? "notBefore":"",
 
1033
               (!*not_before && !*not_after)? ", ":"",
 
1034
               !*not_before? "notAfter":"");
 
1035
      return gpg_error (GPG_ERR_BAD_CERT);
 
1036
    }
 
1037
  if (strcmp (not_before, not_after) > 0 )
 
1038
    {
 
1039
      do_list (1, listmode, listfp,
 
1040
               _("certificate with invalid validity"));
 
1041
      log_info ("  (valid from ");
 
1042
      gpgsm_dump_time (not_before);
 
1043
      log_printf (" expired at ");
 
1044
      gpgsm_dump_time (not_after);
 
1045
      log_printf (")\n");
 
1046
      return gpg_error (GPG_ERR_BAD_CERT);
 
1047
    }
 
1048
  
 
1049
  if (!*exptime)
 
1050
    gnupg_copy_time (exptime, not_after);
 
1051
  else if (strcmp (not_after, exptime) < 0 )
 
1052
    gnupg_copy_time (exptime, not_after);
 
1053
 
 
1054
  if (strcmp (current_time, not_before) < 0 )
 
1055
    {
 
1056
      do_list (1, listmode, listfp, 
 
1057
               depth ==  0 ? _("certificate not yet valid") :
 
1058
               depth == -1 ? _("root certificate not yet valid") :
 
1059
               /* other */   _("intermediate certificate not yet valid"));
 
1060
      if (!listmode)
 
1061
        {
 
1062
          log_info ("  (valid from ");
 
1063
          gpgsm_dump_time (not_before);
 
1064
          log_printf (")\n");
 
1065
        }
 
1066
      return gpg_error (GPG_ERR_CERT_TOO_YOUNG);
 
1067
    } 
 
1068
 
 
1069
  if (*check_time
 
1070
      && (strcmp (check_time, not_before) < 0 
 
1071
          || strcmp (check_time, not_after) > 0))
 
1072
    {
 
1073
      /* Note that we don't need a case for the root certificate
 
1074
         because its own consitency has already been checked.  */
 
1075
      do_list(opt.ignore_expiration?0:1, listmode, listfp,
 
1076
              depth == 0 ? 
 
1077
              _("signature not created during lifetime of certificate") :
 
1078
              depth == 1 ?
 
1079
              _("certificate not created during lifetime of issuer") :
 
1080
              _("intermediate certificate not created during lifetime "
 
1081
                "of issuer"));
 
1082
      if (!listmode)
 
1083
        {
 
1084
          log_info (depth== 0? _("  (  signature created at ") :
 
1085
                    /* */      _("  (certificate created at ") );
 
1086
          gpgsm_dump_time (check_time);
 
1087
          log_printf (")\n");
 
1088
          log_info (depth==0? _("  (certificate valid from ") :
 
1089
                    /* */     _("  (     issuer valid from ") );
 
1090
          gpgsm_dump_time (not_before);
 
1091
          log_info (" to ");
 
1092
          gpgsm_dump_time (not_after);
 
1093
          log_printf (")\n");
 
1094
        }
 
1095
      if (opt.ignore_expiration)
 
1096
        log_info ("WARNING: ignoring expiration\n");
 
1097
      else
 
1098
        return gpg_error (GPG_ERR_CERT_EXPIRED);
 
1099
    }
 
1100
 
 
1101
  return 0;
 
1102
}
 
1103
 
 
1104
 
 
1105
 
 
1106
/* Ask the user whether he wants to mark the certificate CERT trusted.
 
1107
   Returns true if the CERT is the trusted.  We also check whether the
 
1108
   agent is at all enabled to allow marktrusted and don't call it in
 
1109
   this session again if it is not.  */
 
1110
static int
 
1111
ask_marktrusted (ctrl_t ctrl, ksba_cert_t cert, int listmode)
 
1112
{
 
1113
  static int no_more_questions; 
 
1114
  int rc;
 
1115
  char *fpr;
 
1116
  int success = 0;
 
1117
 
 
1118
  fpr = gpgsm_get_fingerprint_string (cert, GCRY_MD_SHA1);
 
1119
  log_info (_("fingerprint=%s\n"), fpr? fpr : "?");
 
1120
  xfree (fpr);
 
1121
  
 
1122
  if (no_more_questions)
 
1123
    rc = gpg_error (GPG_ERR_NOT_SUPPORTED);
 
1124
  else
 
1125
    rc = gpgsm_agent_marktrusted (ctrl, cert);
 
1126
  if (!rc)
 
1127
    {
 
1128
      log_info (_("root certificate has now been marked as trusted\n"));
 
1129
      success = 1;
 
1130
    }
 
1131
  else if (!listmode)
 
1132
    {
 
1133
      gpgsm_dump_cert ("issuer", cert);
 
1134
      log_info ("after checking the fingerprint, you may want "
 
1135
                "to add it manually to the list of trusted certificates.\n");
 
1136
    }
 
1137
 
 
1138
  if (gpg_err_code (rc) == GPG_ERR_NOT_SUPPORTED)
 
1139
    {
 
1140
      if (!no_more_questions)
 
1141
        log_info (_("interactive marking as trusted "
 
1142
                    "not enabled in gpg-agent\n"));
 
1143
      no_more_questions = 1;
 
1144
    }
 
1145
  else if (gpg_err_code (rc) == GPG_ERR_CANCELED)
 
1146
    {
 
1147
      log_info (_("interactive marking as trusted "
 
1148
                  "disabled for this session\n"));
 
1149
      no_more_questions = 1;
 
1150
    }
 
1151
  else
 
1152
    set_already_asked_marktrusted (cert);
 
1153
 
 
1154
  return success;
 
1155
}
 
1156
 
620
1157
 
621
1158
 
622
1159
 
623
1160
/* Validate a chain and optionally return the nearest expiration time
624
1161
   in R_EXPTIME. With LISTMODE set to 1 a special listmode is
625
1162
   activated where only information about the certificate is printed
626
 
   to FP and no output is send to the usual log stream. 
627
 
 
628
 
   Defined flag bits: 0 - do not do any dirmngr isvalid checks.
 
1163
   to LISTFP and no output is send to the usual log stream.  If
 
1164
   CHECKTIME_ARG is set, it is used only in the chain model instead of the
 
1165
   current time.
 
1166
 
 
1167
   Defined flag bits
 
1168
 
 
1169
   VALIDATE_FLAG_NO_DIRMNGR  - Do not do any dirmngr isvalid checks.
 
1170
   VALIDATE_FLAG_CHAIN_MODEL - Check according to chain model.
629
1171
*/
630
 
int
631
 
gpgsm_validate_chain (ctrl_t ctrl, ksba_cert_t cert, ksba_isotime_t r_exptime,
632
 
                      int listmode, FILE *fp, unsigned int flags)
 
1172
static int
 
1173
do_validate_chain (ctrl_t ctrl, ksba_cert_t cert, ksba_isotime_t checktime_arg,
 
1174
                   ksba_isotime_t r_exptime,
 
1175
                   int listmode, estream_t listfp, unsigned int flags,
 
1176
                   struct rootca_flags_s *rootca_flags)
633
1177
{
634
 
  int rc = 0, depth = 0, maxdepth;
 
1178
  int rc = 0, depth, maxdepth;
635
1179
  char *issuer = NULL;
636
1180
  char *subject = NULL;
637
1181
  KEYDB_HANDLE kh = NULL;
638
1182
  ksba_cert_t subject_cert = NULL, issuer_cert = NULL;
639
1183
  ksba_isotime_t current_time;
 
1184
  ksba_isotime_t check_time;
640
1185
  ksba_isotime_t exptime;
641
1186
  int any_expired = 0;
642
1187
  int any_revoked = 0;
646
1191
  int is_qualified = -1; /* Indicates whether the certificate stems
647
1192
                            from a qualified root certificate.
648
1193
                            -1 = unknown, 0 = no, 1 = yes. */
649
 
  int lm = listmode;
 
1194
  chain_item_t chain = NULL; /* A list of all certificates in the chain.  */
 
1195
 
650
1196
 
651
1197
  gnupg_get_isotime (current_time);
 
1198
 
 
1199
  if ( (flags & VALIDATE_FLAG_CHAIN_MODEL) )
 
1200
    {
 
1201
      if (!strcmp (checktime_arg, "19700101T000000"))
 
1202
        {
 
1203
          do_list (1, listmode, listfp, 
 
1204
                   _("WARNING: creation time of signature not known - "
 
1205
                     "assuming current time"));
 
1206
          gnupg_copy_time (check_time, current_time);
 
1207
        }
 
1208
      else
 
1209
        gnupg_copy_time (check_time, checktime_arg);
 
1210
    }
 
1211
  else
 
1212
    *check_time = 0;
 
1213
 
652
1214
  if (r_exptime)
653
1215
    *r_exptime = 0;
654
1216
  *exptime = 0;
671
1233
    gpgsm_dump_cert ("target", cert);
672
1234
 
673
1235
  subject_cert = cert;
 
1236
  ksba_cert_ref (subject_cert);
674
1237
  maxdepth = 50;
 
1238
  depth = 0;
675
1239
 
676
1240
  for (;;)
677
1241
    {
 
1242
      int is_root;
 
1243
      gpg_error_t istrusted_rc = -1;
 
1244
 
 
1245
      /* Put the certificate on our list.  */
 
1246
      {
 
1247
        chain_item_t ci;
 
1248
 
 
1249
        ci = xtrycalloc (1, sizeof *ci);
 
1250
        if (!ci)
 
1251
          {
 
1252
            rc = gpg_error_from_syserror ();
 
1253
            goto leave;
 
1254
          }
 
1255
        ksba_cert_ref (subject_cert);
 
1256
        ci->cert = subject_cert;
 
1257
        ci->next = chain;
 
1258
        chain = ci;
 
1259
      }
 
1260
 
678
1261
      xfree (issuer);
679
1262
      xfree (subject);
680
1263
      issuer = ksba_cert_get_issuer (subject_cert, 0);
682
1265
 
683
1266
      if (!issuer)
684
1267
        {
685
 
          do_list (1, lm, fp,  _("no issuer found in certificate"));
 
1268
          do_list (1, listmode, listfp,  _("no issuer found in certificate"));
686
1269
          rc = gpg_error (GPG_ERR_BAD_CERT);
687
1270
          goto leave;
688
1271
        }
689
1272
 
690
 
      {
691
 
        ksba_isotime_t not_before, not_after;
692
 
 
693
 
        rc = ksba_cert_get_validity (subject_cert, 0, not_before);
694
 
        if (!rc)
695
 
          rc = ksba_cert_get_validity (subject_cert, 1, not_after);
696
 
        if (rc)
697
 
          {
698
 
            do_list (1, lm, fp, _("certificate with invalid validity: %s"),
699
 
                     gpg_strerror (rc));
700
 
            rc = gpg_error (GPG_ERR_BAD_CERT);
701
 
            goto leave;
702
 
          }
703
 
 
704
 
        if (*not_after)
705
 
          {
706
 
            if (!*exptime)
707
 
              gnupg_copy_time (exptime, not_after);
708
 
            else if (strcmp (not_after, exptime) < 0 )
709
 
              gnupg_copy_time (exptime, not_after);
710
 
          }
711
 
 
712
 
        if (*not_before && strcmp (current_time, not_before) < 0 )
713
 
          {
714
 
            do_list (1, lm, fp, _("certificate not yet valid"));
715
 
            if (!lm)
716
 
              {
717
 
                log_info ("(valid from ");
718
 
                gpgsm_dump_time (not_before);
719
 
                log_printf (")\n");
720
 
              }
721
 
            rc = gpg_error (GPG_ERR_CERT_TOO_YOUNG);
722
 
            goto leave;
723
 
          }            
724
 
        if (*not_after && strcmp (current_time, not_after) > 0 )
725
 
          {
726
 
            do_list (opt.ignore_expiration?0:1, lm, fp,
727
 
                     _("certificate has expired"));
728
 
            if (!lm)
729
 
              {
730
 
                log_info ("(expired at ");
731
 
                gpgsm_dump_time (not_after);
732
 
                log_printf (")\n");
733
 
              }
734
 
            if (opt.ignore_expiration)
735
 
                log_info ("WARNING: ignoring expiration\n");
736
 
            else
737
 
              any_expired = 1;
738
 
          }            
739
 
      }
740
 
 
741
 
      rc = unknown_criticals (subject_cert, listmode, fp);
 
1273
 
 
1274
      /* Is this a self-issued certificate (i.e. the root certificate)?  */
 
1275
      is_root = is_root_cert (subject_cert, issuer, subject);
 
1276
      if (is_root)
 
1277
        {
 
1278
          chain->is_root = 1;
 
1279
          /* Check early whether the certificate is listed as trusted.
 
1280
             We used to do this only later but changed it to call the
 
1281
             check right here so that we can access special flags
 
1282
             associated with that specific root certificate.  */
 
1283
          istrusted_rc = gpgsm_agent_istrusted (ctrl, subject_cert,
 
1284
                                                rootca_flags);
 
1285
          audit_log_cert (ctrl->audit, AUDIT_ROOT_TRUSTED,
 
1286
                          subject_cert, istrusted_rc);
 
1287
          /* If the chain model extended attribute is used, make sure
 
1288
             that our chain model flag is set. */
 
1289
          if (has_validation_model_chain (subject_cert, listmode, listfp))
 
1290
            rootca_flags->chain_model = 1;
 
1291
        }
 
1292
      
 
1293
 
 
1294
      /* Check the validity period. */
 
1295
      if ( (flags & VALIDATE_FLAG_CHAIN_MODEL) )
 
1296
        rc = check_validity_period_cm (current_time, check_time, subject_cert,
 
1297
                                       exptime, listmode, listfp,
 
1298
                                       (depth && is_root)? -1: depth);
 
1299
      else
 
1300
        rc = check_validity_period (current_time, subject_cert,
 
1301
                                    exptime, listmode, listfp,
 
1302
                                    (depth && is_root)? -1: depth);
 
1303
      if (gpg_err_code (rc) == GPG_ERR_CERT_EXPIRED)
 
1304
        {
 
1305
          any_expired = 1;
 
1306
          rc = 0;
 
1307
        }
 
1308
      else if (rc)
 
1309
        goto leave;
 
1310
        
 
1311
 
 
1312
      /* Assert that we understand all critical extensions. */
 
1313
      rc = unknown_criticals (subject_cert, listmode, listfp);
742
1314
      if (rc)
743
1315
        goto leave;
744
1316
 
 
1317
      /* Do a policy check. */
745
1318
      if (!opt.no_policy_check)
746
1319
        {
747
 
          rc = check_cert_policy (subject_cert, listmode, fp);
 
1320
          rc = check_cert_policy (subject_cert, listmode, listfp);
748
1321
          if (gpg_err_code (rc) == GPG_ERR_NO_POLICY_MATCH)
749
1322
            {
750
1323
              any_no_policy_match = 1;
755
1328
        }
756
1329
 
757
1330
 
758
 
      /* Is this a self-issued certificate? */
759
 
      if (subject && !strcmp (issuer, subject))
760
 
        {  /* Yes. */
761
 
          if (gpgsm_check_cert_sig (subject_cert, subject_cert) )
 
1331
      /* If this is the root certificate we are at the end of the chain.  */
 
1332
      if (is_root)
 
1333
        { 
 
1334
          if (!istrusted_rc)
 
1335
            ; /* No need to check the certificate for a trusted one. */
 
1336
          else if (gpgsm_check_cert_sig (subject_cert, subject_cert) )
762
1337
            {
763
 
              do_list (1, lm, fp,
 
1338
              /* We only check the signature if the certificate is not
 
1339
                 trusted for better diagnostics. */
 
1340
              do_list (1, listmode, listfp,
764
1341
                       _("self-signed certificate has a BAD signature"));
765
1342
              if (DBG_X509)
766
1343
                {
770
1347
                                   : GPG_ERR_BAD_CERT);
771
1348
              goto leave;
772
1349
            }
773
 
          rc = allowed_ca (subject_cert, NULL, listmode, fp);
774
 
          if (rc)
775
 
            goto leave;
776
 
 
 
1350
          if (!rootca_flags->relax)
 
1351
            {
 
1352
              rc = allowed_ca (ctrl, subject_cert, NULL, listmode, listfp);
 
1353
              if (rc)
 
1354
                goto leave;
 
1355
            }
 
1356
              
777
1357
          
778
1358
          /* Set the flag for qualified signatures.  This flag is
779
1359
             deduced from a list of root certificates allowed for
796
1376
                {
797
1377
                  /* Need to consult the list of root certificates for
798
1378
                     qualified signatures. */
799
 
                  err = gpgsm_is_in_qualified_list (ctrl, subject_cert);
 
1379
                  err = gpgsm_is_in_qualified_list (ctrl, subject_cert, NULL);
800
1380
                  if (!err)
801
1381
                    is_qualified = 1;
802
1382
                  else if ( gpg_err_code (err) == GPG_ERR_NOT_FOUND)
807
1387
                               gpg_strerror (err));
808
1388
                  if ( is_qualified != -1 )
809
1389
                    {
810
 
                      /* Cache the result but don't care toomuch about
811
 
                         an error. */
 
1390
                      /* Cache the result but don't care too much
 
1391
                         about an error. */
812
1392
                      buf[0] = !!is_qualified;
813
1393
                      err = ksba_cert_set_user_data (subject_cert,
814
1394
                                                     "is_qualified", buf, 1);
820
1400
            }
821
1401
 
822
1402
 
823
 
          /* Check whether we really trust this root certificate. */
824
 
          rc = gpgsm_agent_istrusted (ctrl, subject_cert);
 
1403
          /* Act on the check for a trusted root certificates. */
 
1404
          rc = istrusted_rc;
825
1405
          if (!rc)
826
1406
            ;
827
1407
          else if (gpg_err_code (rc) == GPG_ERR_NOT_TRUSTED)
828
1408
            {
829
 
              do_list (0, lm, fp, _("root certificate is not marked trusted"));
 
1409
              do_list (0, listmode, listfp, 
 
1410
                       _("root certificate is not marked trusted"));
830
1411
              /* If we already figured out that the certificate is
831
1412
                 expired it does not make much sense to ask the user
832
 
                 whether we wants to trust the root certificate.  He
 
1413
                 whether we wants to trust the root certificate.  We
833
1414
                 should do this only if the certificate under question
834
 
                 will then be usable. */
835
 
              if (!lm && !any_expired)
836
 
                {
837
 
                  int rc2;
838
 
                  char *fpr = gpgsm_get_fingerprint_string (subject_cert,
839
 
                                                            GCRY_MD_SHA1);
840
 
                  log_info (_("fingerprint=%s\n"), fpr? fpr : "?");
841
 
                  xfree (fpr);
842
 
                  rc2 = gpgsm_agent_marktrusted (ctrl, subject_cert);
843
 
                  if (!rc2)
844
 
                    {
845
 
                      log_info (_("root certificate has now"
846
 
                                  " been marked as trusted\n"));
847
 
                      rc = 0;
848
 
                    }
849
 
                  else 
850
 
                    {
851
 
                      gpgsm_dump_cert ("issuer", subject_cert);
852
 
                      log_info ("after checking the fingerprint, you may want "
853
 
                                "to add it manually to the list of trusted "
854
 
                                "certificates.\n");
855
 
                    }
856
 
                }
 
1415
                 will then be usable.  */
 
1416
              if ( !any_expired
 
1417
                   && (!listmode || !already_asked_marktrusted (subject_cert))
 
1418
                   && ask_marktrusted (ctrl, subject_cert, listmode) )
 
1419
                rc = 0;
857
1420
            }
858
1421
          else 
859
1422
            {
865
1428
            goto leave;
866
1429
 
867
1430
          /* Check for revocations etc. */
868
 
          if ((flags & 1))
 
1431
          if ((flags & VALIDATE_FLAG_NO_DIRMNGR))
869
1432
            ;
870
 
          else if (opt.no_trusted_cert_crl_check)
 
1433
          else if (opt.no_trusted_cert_crl_check || rootca_flags->relax)
871
1434
            ; 
872
1435
          else
873
 
            rc = is_cert_still_valid (ctrl, lm, fp,
 
1436
            rc = is_cert_still_valid (ctrl, 
 
1437
                                      (flags & VALIDATE_FLAG_CHAIN_MODEL),
 
1438
                                      listmode, listfp,
874
1439
                                      subject_cert, subject_cert,
875
1440
                                      &any_revoked, &any_no_crl,
876
1441
                                      &any_crl_too_old);
878
1443
            goto leave;
879
1444
 
880
1445
          break;  /* Okay: a self-signed certicate is an end-point. */
881
 
        }
 
1446
        } /* End is_root.  */
 
1447
 
882
1448
      
883
 
      depth++;
884
 
      if (depth > maxdepth)
 
1449
      /* Take care that the chain does not get too long. */
 
1450
      if ((depth+1) > maxdepth)
885
1451
        {
886
 
          do_list (1, lm, fp, _("certificate chain too long\n"));
 
1452
          do_list (1, listmode, listfp, _("certificate chain too long\n"));
887
1453
          rc = gpg_error (GPG_ERR_BAD_CERT_CHAIN);
888
1454
          goto leave;
889
1455
        }
890
1456
 
891
 
      /* find the next cert up the tree */
 
1457
      /* Find the next cert up the tree. */
892
1458
      keydb_search_reset (kh);
893
 
      rc = find_up (kh, subject_cert, issuer, 0);
 
1459
      rc = find_up (ctrl, kh, subject_cert, issuer, 0);
894
1460
      if (rc)
895
1461
        {
896
1462
          if (rc == -1)
897
1463
            {
898
 
              do_list (0, lm, fp, _("issuer certificate not found"));
899
 
              if (!lm)
 
1464
              do_list (0, listmode, listfp, _("issuer certificate not found"));
 
1465
              if (!listmode)
900
1466
                {
901
1467
                  log_info ("issuer certificate: #/");
902
1468
                  gpgsm_dump_string (issuer);
928
1494
      rc = gpgsm_check_cert_sig (issuer_cert, subject_cert);
929
1495
      if (rc)
930
1496
        {
931
 
          do_list (0, lm, fp, _("certificate has a BAD signature"));
 
1497
          do_list (0, listmode, listfp, _("certificate has a BAD signature"));
932
1498
          if (DBG_X509)
933
1499
            {
934
1500
              gpgsm_dump_cert ("signing issuer", issuer_cert);
942
1508
                 root certificates. */
943
1509
              /* FIXME: Do this only if we don't have an
944
1510
                 AKI.keyIdentifier */
945
 
              rc = find_up (kh, subject_cert, issuer, 1);
 
1511
              rc = find_up (ctrl, kh, subject_cert, issuer, 1);
946
1512
              if (!rc)
947
1513
                {
948
1514
                  ksba_cert_t tmp_cert;
958
1524
                    }
959
1525
                  else
960
1526
                    {
961
 
                      do_list (0, lm, fp, _("found another possible matching "
962
 
                                            "CA certificate - trying again"));
 
1527
                      do_list (0, listmode, listfp,
 
1528
                               _("found another possible matching "
 
1529
                                 "CA certificate - trying again"));
963
1530
                      ksba_cert_release (issuer_cert); 
964
1531
                      issuer_cert = tmp_cert;
965
1532
                      goto try_another_cert;
973
1540
          goto leave;
974
1541
        }
975
1542
 
 
1543
      is_root = gpgsm_is_root_cert (issuer_cert);
 
1544
      istrusted_rc = -1;
 
1545
 
 
1546
 
 
1547
      /* Check that a CA is allowed to issue certificates. */
976
1548
      {
977
1549
        int chainlen;
978
 
        rc = allowed_ca (issuer_cert, &chainlen, listmode, fp);
 
1550
 
 
1551
        rc = allowed_ca (ctrl, issuer_cert, &chainlen, listmode, listfp);
 
1552
        if (rc)
 
1553
          {
 
1554
            /* Not allowed.  Check whether this is a trusted root
 
1555
               certificate and whether we allow special exceptions.
 
1556
               We could carry the result of the test over to the
 
1557
               regular root check at the top of the loop but for
 
1558
               clarity we won't do that.  Given that the majority of
 
1559
               certificates carry proper BasicContraints our way of
 
1560
               overriding an error in the way is justified for
 
1561
               performance reasons. */
 
1562
            if (is_root)
 
1563
              {
 
1564
                istrusted_rc = gpgsm_agent_istrusted (ctrl, issuer_cert,
 
1565
                                                      rootca_flags);
 
1566
                if (!istrusted_rc && rootca_flags->relax)
 
1567
                  {
 
1568
                    /* Ignore the error due to the relax flag.  */
 
1569
                    rc = 0;
 
1570
                    chainlen = -1;
 
1571
                  }
 
1572
              }
 
1573
          }
979
1574
        if (rc)
980
1575
          goto leave;
981
 
        if (chainlen >= 0 && (depth - 1) > chainlen)
 
1576
        if (chainlen >= 0 && depth > chainlen)
982
1577
          {
983
 
            do_list (1, lm, fp,
 
1578
            do_list (1, listmode, listfp,
984
1579
                     _("certificate chain longer than allowed by CA (%d)"),
985
1580
                     chainlen);
986
1581
            rc = gpg_error (GPG_ERR_BAD_CERT_CHAIN);
988
1583
          }
989
1584
      }
990
1585
 
 
1586
      /* Is the certificate allowed to sign other certificates. */
991
1587
      if (!listmode)
992
1588
        {
993
1589
          rc = gpgsm_cert_use_cert_p (issuer_cert);
1001
1597
            }
1002
1598
        }
1003
1599
 
1004
 
      /* Check for revocations etc. */
1005
 
      if ((flags & 1))
 
1600
      /* Check for revocations etc.  Note that for a root certificate
 
1601
         this test is done a second time later. This should eventually
 
1602
         be fixed. */
 
1603
      if ((flags & VALIDATE_FLAG_NO_DIRMNGR))
1006
1604
        rc = 0;
 
1605
      else if (is_root && (opt.no_trusted_cert_crl_check
 
1606
                           || (!istrusted_rc && rootca_flags->relax)))
 
1607
        rc = 0; 
1007
1608
      else
1008
 
        rc = is_cert_still_valid (ctrl, lm, fp,
 
1609
        rc = is_cert_still_valid (ctrl, 
 
1610
                                  (flags & VALIDATE_FLAG_CHAIN_MODEL),
 
1611
                                  listmode, listfp,
1009
1612
                                  subject_cert, issuer_cert,
1010
1613
                                  &any_revoked, &any_no_crl, &any_crl_too_old);
1011
1614
      if (rc)
1013
1616
 
1014
1617
 
1015
1618
      if (opt.verbose && !listmode)
1016
 
        log_info ("certificate is good\n");
1017
 
      
 
1619
        log_info (depth == 0 ? _("certificate is good\n") :
 
1620
                  !is_root   ? _("intermediate certificate is good\n") :
 
1621
                  /* other */  _("root certificate is good\n"));
 
1622
 
 
1623
      /* Under the chain model the next check time is the creation
 
1624
         time of the subject certificate.  */
 
1625
      if ( (flags & VALIDATE_FLAG_CHAIN_MODEL) )
 
1626
        {
 
1627
          rc = ksba_cert_get_validity (subject_cert, 0, check_time);
 
1628
          if (rc)
 
1629
            {
 
1630
              /* That will never happen as we have already checked
 
1631
                 this above.  */
 
1632
              BUG ();
 
1633
            }
 
1634
        }
 
1635
 
 
1636
      /* For the next round the current issuer becomes the new subject.  */
1018
1637
      keydb_search_reset (kh);
 
1638
      ksba_cert_release (subject_cert);
1019
1639
      subject_cert = issuer_cert;
1020
1640
      issuer_cert = NULL;
 
1641
      depth++;
1021
1642
    } /* End chain traversal. */
1022
1643
 
1023
 
  if (!listmode)
 
1644
  if (!listmode && !opt.quiet)
1024
1645
    {
1025
1646
      if (opt.no_policy_check)
1026
1647
        log_info ("policies not checked due to %s option\n",
1046
1667
    }
1047
1668
  
1048
1669
 leave:
 
1670
  /* If we have traversed a complete chain up to the root we will
 
1671
     reset the ephemeral flag for all these certificates.  This is done
 
1672
     regardless of any error because those errors may only be
 
1673
     transient. */
 
1674
  if (chain && chain->is_root)
 
1675
    {
 
1676
      gpg_error_t err;
 
1677
      chain_item_t ci;
 
1678
      
 
1679
      for (ci = chain; ci; ci = ci->next)
 
1680
        {
 
1681
          /* Note that it is possible for the last certificate in the
 
1682
             chain (i.e. our target certificate) that it has not yet
 
1683
             been stored in the keybox and thus the flag can't be set.
 
1684
             We ignore this error becuase it will later be stored
 
1685
             anyway.  */
 
1686
          err = keydb_set_cert_flags (ci->cert, 1, KEYBOX_FLAG_BLOB, 0,
 
1687
                                      KEYBOX_FLAG_BLOB_EPHEMERAL, 0);
 
1688
          if (!ci->next && gpg_err_code (err) == GPG_ERR_NOT_FOUND)
 
1689
            ;
 
1690
          else if (err)
 
1691
            log_error ("clearing ephemeral flag failed: %s\n",
 
1692
                       gpg_strerror (err)); 
 
1693
        }
 
1694
    }
 
1695
 
 
1696
  /* If we have figured something about the qualified signature
 
1697
     capability of the certificate under question, store the result as
 
1698
     user data in all certificates of the chain.  We do this even if the
 
1699
     validation itself failed.  */
1049
1700
  if (is_qualified != -1)
1050
1701
    {
1051
 
      /* We figured something about the qualified signature capability
1052
 
         of the certificate under question.  Store the result as user
1053
 
         data in the certificate object.  We do this even if the
1054
 
         validation itself failed. */
1055
 
      /* Fixme: We should set this flag for all certificates in the
1056
 
         chain for optimizing reasons. */
 
1702
      gpg_error_t err;
 
1703
      chain_item_t ci;
1057
1704
      char buf[1];
1058
 
      gpg_error_t err;
1059
1705
 
1060
1706
      buf[0] = !!is_qualified;
1061
 
      err = ksba_cert_set_user_data (cert, "is_qualified", buf, 1);
1062
 
      if (err)
1063
 
        {
1064
 
          log_error ("set_user_data(is_qualified) failed: %s\n",
1065
 
                     gpg_strerror (err)); 
1066
 
          if (!rc)
1067
 
            rc = err;
1068
 
        }
1069
 
    }
 
1707
      
 
1708
      for (ci = chain; ci; ci = ci->next)
 
1709
        {
 
1710
          err = ksba_cert_set_user_data (ci->cert, "is_qualified", buf, 1);
 
1711
          if (err)
 
1712
            {
 
1713
              log_error ("set_user_data(is_qualified) failed: %s\n",
 
1714
                         gpg_strerror (err)); 
 
1715
              if (!rc)
 
1716
                rc = err;
 
1717
            }
 
1718
        }
 
1719
    }
 
1720
 
 
1721
  /* If auditing has been enabled, record what is in the chain.  */
 
1722
  if (ctrl->audit)
 
1723
    {
 
1724
      chain_item_t ci;
 
1725
 
 
1726
      audit_log (ctrl->audit, AUDIT_CHAIN_BEGIN);
 
1727
      for (ci = chain; ci; ci = ci->next)
 
1728
        {
 
1729
          audit_log_cert (ctrl->audit,
 
1730
                          ci->is_root? AUDIT_CHAIN_ROOTCERT : AUDIT_CHAIN_CERT,
 
1731
                          ci->cert, 0);
 
1732
        }
 
1733
      audit_log (ctrl->audit, AUDIT_CHAIN_END);
 
1734
    }
 
1735
 
1070
1736
  if (r_exptime)
1071
1737
    gnupg_copy_time (r_exptime, exptime);
1072
1738
  xfree (issuer);
 
1739
  xfree (subject);
1073
1740
  keydb_release (kh); 
 
1741
  while (chain)
 
1742
    {
 
1743
      chain_item_t ci_next = chain->next;
 
1744
      ksba_cert_release (chain->cert);
 
1745
      xfree (chain);
 
1746
      chain = ci_next;
 
1747
    }
1074
1748
  ksba_cert_release (issuer_cert);
1075
 
  if (subject_cert != cert)
1076
 
    ksba_cert_release (subject_cert);
 
1749
  ksba_cert_release (subject_cert);
 
1750
  return rc;
 
1751
}
 
1752
 
 
1753
 
 
1754
/* Validate a certificate chain.  For a description see
 
1755
   do_validate_chain.  This function is a wrapper to handle a root
 
1756
   certificate with the chain_model flag set.  If RETFLAGS is not
 
1757
   NULL, flags indicating now the verification was done are stored
 
1758
   there.  The only defined flag for RETFLAGS is
 
1759
   VALIDATE_FLAG_CHAIN_MODEL.
 
1760
 
 
1761
   If you are verifying a signature you should set CHECKTIME to the
 
1762
   creation time of the signature.  If your are verifying a
 
1763
   certificate, set it nil (i.e. the empty string).  If the creation
 
1764
   date of the signature is not known use the special date
 
1765
   "19700101T000000" which is treated in a special way here. */
 
1766
int
 
1767
gpgsm_validate_chain (ctrl_t ctrl, ksba_cert_t cert, ksba_isotime_t checktime,
 
1768
                      ksba_isotime_t r_exptime,
 
1769
                      int listmode, estream_t listfp, unsigned int flags,
 
1770
                      unsigned int *retflags)
 
1771
{
 
1772
  int rc;
 
1773
  struct rootca_flags_s rootca_flags;
 
1774
  unsigned int dummy_retflags;
 
1775
 
 
1776
  if (!retflags)
 
1777
    retflags = &dummy_retflags;
 
1778
 
 
1779
  if (ctrl->validation_model == 1)
 
1780
    flags |= VALIDATE_FLAG_CHAIN_MODEL;
 
1781
 
 
1782
  *retflags = (flags & VALIDATE_FLAG_CHAIN_MODEL);
 
1783
  memset (&rootca_flags, 0, sizeof rootca_flags);
 
1784
 
 
1785
  rc = do_validate_chain (ctrl, cert, checktime, 
 
1786
                          r_exptime, listmode, listfp, flags,
 
1787
                          &rootca_flags);
 
1788
  if (gpg_err_code (rc) == GPG_ERR_CERT_EXPIRED
 
1789
      && !(flags & VALIDATE_FLAG_CHAIN_MODEL)
 
1790
      && (rootca_flags.valid && rootca_flags.chain_model))
 
1791
    {
 
1792
      do_list (0, listmode, listfp, _("switching to chain model"));
 
1793
      rc = do_validate_chain (ctrl, cert, checktime,
 
1794
                              r_exptime, listmode, listfp, 
 
1795
                              (flags |= VALIDATE_FLAG_CHAIN_MODEL),
 
1796
                              &rootca_flags);
 
1797
      *retflags |= VALIDATE_FLAG_CHAIN_MODEL;
 
1798
    }
 
1799
 
 
1800
  if (opt.verbose)
 
1801
    do_list (0, listmode, listfp, _("validation model used: %s"), 
 
1802
             (*retflags & VALIDATE_FLAG_CHAIN_MODEL)?
 
1803
             _("chain"):_("shell"));
 
1804
  
1077
1805
  return rc;
1078
1806
}
1079
1807
 
1083
1811
   the DB and that this one is valid; which it should be because it
1084
1812
   has been checked using this function. */
1085
1813
int
1086
 
gpgsm_basic_cert_check (ksba_cert_t cert)
 
1814
gpgsm_basic_cert_check (ctrl_t ctrl, ksba_cert_t cert)
1087
1815
{
1088
1816
  int rc = 0;
1089
1817
  char *issuer = NULL;
1114
1842
      goto leave;
1115
1843
    }
1116
1844
 
1117
 
  if (subject && !strcmp (issuer, subject))
 
1845
  if (is_root_cert (cert, issuer, subject))
1118
1846
    {
1119
1847
      rc = gpgsm_check_cert_sig (cert, cert);
1120
1848
      if (rc)
1133
1861
    {
1134
1862
      /* Find the next cert up the tree. */
1135
1863
      keydb_search_reset (kh);
1136
 
      rc = find_up (kh, cert, issuer, 0);
 
1864
      rc = find_up (ctrl, kh, cert, issuer, 0);
1137
1865
      if (rc)
1138
1866
        {
1139
1867
          if (rc == -1)
1171
1899
          goto leave;
1172
1900
        }
1173
1901
      if (opt.verbose)
1174
 
        log_info ("certificate is good\n");
 
1902
        log_info (_("certificate is good\n"));
1175
1903
    }
1176
1904
 
1177
1905
 leave:
1178
1906
  xfree (issuer);
 
1907
  xfree (subject);
1179
1908
  keydb_release (kh); 
1180
1909
  ksba_cert_release (issuer_cert);
1181
1910
  return rc;
1182
1911
}
1183
1912
 
 
1913
 
 
1914
 
 
1915
/* Check whether the certificate CERT has been issued by the German
 
1916
   authority for qualified signature.  They do not set the
 
1917
   basicConstraints and thus we need this workaround.  It works by
 
1918
   looking up the root certificate and checking whether that one is
 
1919
   listed as a qualified certificate for Germany. 
 
1920
 
 
1921
   We also try to cache this data but as long as don't keep a
 
1922
   reference to the certificate this won't be used.
 
1923
 
 
1924
   Returns: True if CERT is a RegTP issued CA cert (i.e. the root
 
1925
   certificate itself or one of the CAs).  In that case CHAINLEN will
 
1926
   receive the length of the chain which is either 0 or 1.
 
1927
*/
 
1928
static int
 
1929
get_regtp_ca_info (ctrl_t ctrl, ksba_cert_t cert, int *chainlen)
 
1930
{
 
1931
  gpg_error_t err;
 
1932
  ksba_cert_t next;
 
1933
  int rc = 0;
 
1934
  int i, depth;
 
1935
  char country[3];
 
1936
  ksba_cert_t array[4];
 
1937
  char buf[2];
 
1938
  size_t buflen;
 
1939
  int dummy_chainlen;
 
1940
 
 
1941
  if (!chainlen)
 
1942
    chainlen = &dummy_chainlen;
 
1943
 
 
1944
  *chainlen = 0;
 
1945
  err = ksba_cert_get_user_data (cert, "regtp_ca_chainlen", 
 
1946
                                 &buf, sizeof (buf), &buflen);
 
1947
  if (!err)
 
1948
    {
 
1949
      /* Got info. */
 
1950
      if (buflen < 2 || !*buf)
 
1951
        return 0; /* Nothing found. */
 
1952
      *chainlen = buf[1];
 
1953
      return 1; /* This is a regtp CA. */
 
1954
    }
 
1955
  else if (gpg_err_code (err) != GPG_ERR_NOT_FOUND)
 
1956
    {
 
1957
      log_error ("ksba_cert_get_user_data(%s) failed: %s\n",
 
1958
                 "regtp_ca_chainlen", gpg_strerror (err));
 
1959
      return 0; /* Nothing found.  */
 
1960
    }
 
1961
 
 
1962
  /* Need to gather the info.  This requires to walk up the chain
 
1963
     until we have found the root.  Because we are only interested in
 
1964
     German Bundesnetzagentur (former RegTP) derived certificates 3
 
1965
     levels are enough.  (The German signature law demands a 3 tier
 
1966
     hierachy; thus there is only one CA between the EE and the Root
 
1967
     CA.)  */
 
1968
  memset (&array, 0, sizeof array);
 
1969
 
 
1970
  depth = 0;
 
1971
  ksba_cert_ref (cert);
 
1972
  array[depth++] = cert;
 
1973
  ksba_cert_ref (cert);
 
1974
  while (depth < DIM(array) && !(rc=gpgsm_walk_cert_chain (ctrl, cert, &next)))
 
1975
    {
 
1976
      ksba_cert_release (cert);
 
1977
      ksba_cert_ref (next);
 
1978
      array[depth++] = next;
 
1979
      cert = next;
 
1980
    }
 
1981
  ksba_cert_release (cert);
 
1982
  if (rc != -1 || !depth || depth == DIM(array) )
 
1983
    {
 
1984
      /* We did not reached the root. */
 
1985
      goto leave;
 
1986
    }
 
1987
 
 
1988
  /* If this is a German signature law issued certificate, we store
 
1989
     additional additional information. */
 
1990
  if (!gpgsm_is_in_qualified_list (NULL, array[depth-1], country)
 
1991
      && !strcmp (country, "de"))
 
1992
    {
 
1993
      /* Setting the pathlen for the root CA and the CA flag for the
 
1994
         next one is all what we need to do. */
 
1995
      err = ksba_cert_set_user_data (array[depth-1], "regtp_ca_chainlen",
 
1996
                                     "\x01\x01", 2);
 
1997
      if (!err && depth > 1)
 
1998
        err = ksba_cert_set_user_data (array[depth-2], "regtp_ca_chainlen",
 
1999
                                       "\x01\x00", 2);
 
2000
      if (err)
 
2001
        log_error ("ksba_set_user_data(%s) failed: %s\n",
 
2002
                   "regtp_ca_chainlen", gpg_strerror (err)); 
 
2003
      for (i=0; i < depth; i++)
 
2004
        ksba_cert_release (array[i]);
 
2005
      *chainlen = (depth>1? 0:1);
 
2006
      return 1;
 
2007
    }
 
2008
 
 
2009
 leave:
 
2010
  /* Nothing special with this certificate. Mark the target
 
2011
     certificate anyway to avoid duplicate lookups. */ 
 
2012
  err = ksba_cert_set_user_data (cert, "regtp_ca_chainlen", "", 1);
 
2013
  if (err)
 
2014
    log_error ("ksba_set_user_data(%s) failed: %s\n",
 
2015
               "regtp_ca_chainlen", gpg_strerror (err)); 
 
2016
  for (i=0; i < depth; i++)
 
2017
    ksba_cert_release (array[i]);
 
2018
  return 0;
 
2019
}