Line data Source code
1 : /* certchain.c - certificate chain validation
2 : * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 : * 2006, 2007, 2008, 2011 Free Software Foundation, Inc.
4 : *
5 : * This file is part of GnuPG.
6 : *
7 : * GnuPG is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * GnuPG is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include <config.h>
22 : #include <stdio.h>
23 : #include <stdlib.h>
24 : #include <string.h>
25 : #include <errno.h>
26 : #include <unistd.h>
27 : #include <time.h>
28 : #include <stdarg.h>
29 : #include <assert.h>
30 :
31 : #include "gpgsm.h"
32 : #include <gcrypt.h>
33 : #include <ksba.h>
34 :
35 : #include "keydb.h"
36 : #include "../kbx/keybox.h" /* for KEYBOX_FLAG_* */
37 : #include "i18n.h"
38 : #include "tlv.h"
39 :
40 :
41 : /* Object to keep track of certain root certificates. */
42 : struct marktrusted_info_s
43 : {
44 : struct marktrusted_info_s *next;
45 : unsigned char fpr[20];
46 : };
47 : static struct marktrusted_info_s *marktrusted_info;
48 :
49 :
50 : /* While running the validation function we want to keep track of the
51 : certificates in the chain. This type is used for that. */
52 : struct chain_item_s
53 : {
54 : struct chain_item_s *next;
55 : ksba_cert_t cert; /* The certificate. */
56 : int is_root; /* The certificate is the root certificate. */
57 : };
58 : typedef struct chain_item_s *chain_item_t;
59 :
60 :
61 : static int is_root_cert (ksba_cert_t cert,
62 : const char *issuerdn, const char *subjectdn);
63 : static int get_regtp_ca_info (ctrl_t ctrl, ksba_cert_t cert, int *chainlen);
64 :
65 :
66 : /* This function returns true if we already asked during this session
67 : whether the root certificate CERT shall be marked as trusted. */
68 : static int
69 0 : already_asked_marktrusted (ksba_cert_t cert)
70 : {
71 : unsigned char fpr[20];
72 : struct marktrusted_info_s *r;
73 :
74 0 : gpgsm_get_fingerprint (cert, GCRY_MD_SHA1, fpr, NULL);
75 : /* No context switches in the loop! */
76 0 : for (r=marktrusted_info; r; r= r->next)
77 0 : if (!memcmp (r->fpr, fpr, 20))
78 0 : return 1;
79 0 : return 0;
80 : }
81 :
82 : /* Flag certificate CERT as already asked whether it shall be marked
83 : as trusted. */
84 : static void
85 0 : set_already_asked_marktrusted (ksba_cert_t cert)
86 : {
87 : unsigned char fpr[20];
88 : struct marktrusted_info_s *r;
89 :
90 0 : gpgsm_get_fingerprint (cert, GCRY_MD_SHA1, fpr, NULL);
91 0 : for (r=marktrusted_info; r; r= r->next)
92 0 : if (!memcmp (r->fpr, fpr, 20))
93 0 : return; /* Already marked. */
94 0 : r = xtrycalloc (1, sizeof *r);
95 0 : if (!r)
96 0 : return;
97 0 : memcpy (r->fpr, fpr, 20);
98 0 : r->next = marktrusted_info;
99 0 : marktrusted_info = r;
100 : }
101 :
102 : /* If LISTMODE is true, print FORMAT using LISTMODE to FP. If
103 : LISTMODE is false, use the string to print an log_info or, if
104 : IS_ERROR is true, and log_error. */
105 : static void
106 0 : do_list (int is_error, int listmode, estream_t fp, const char *format, ...)
107 : {
108 : va_list arg_ptr;
109 :
110 0 : va_start (arg_ptr, format) ;
111 0 : if (listmode)
112 : {
113 0 : if (fp)
114 : {
115 0 : es_fputs (" [", fp);
116 0 : es_vfprintf (fp, format, arg_ptr);
117 0 : es_fputs ("]\n", fp);
118 : }
119 : }
120 : else
121 : {
122 0 : log_logv (is_error? GPGRT_LOG_ERROR: GPGRT_LOG_INFO, format, arg_ptr);
123 0 : log_printf ("\n");
124 : }
125 0 : va_end (arg_ptr);
126 0 : }
127 :
128 : /* Return 0 if A and B are equal. */
129 : static int
130 0 : compare_certs (ksba_cert_t a, ksba_cert_t b)
131 : {
132 : const unsigned char *img_a, *img_b;
133 : size_t len_a, len_b;
134 :
135 0 : img_a = ksba_cert_get_image (a, &len_a);
136 0 : if (!img_a)
137 0 : return 1;
138 0 : img_b = ksba_cert_get_image (b, &len_b);
139 0 : if (!img_b)
140 0 : return 1;
141 0 : return !(len_a == len_b && !memcmp (img_a, img_b, len_a));
142 : }
143 :
144 :
145 : /* Return true if CERT has the validityModel extensions and defines
146 : the use of the chain model. */
147 : static int
148 0 : has_validation_model_chain (ksba_cert_t cert, int listmode, estream_t listfp)
149 : {
150 : gpg_error_t err;
151 : int idx, yes;
152 : const char *oid;
153 : size_t off, derlen, objlen, hdrlen;
154 : const unsigned char *der;
155 : int class, tag, constructed, ndef;
156 : char *oidbuf;
157 :
158 0 : for (idx=0; !(err=ksba_cert_get_extension (cert, idx,
159 0 : &oid, NULL, &off, &derlen));idx++)
160 0 : if (!strcmp (oid, "1.3.6.1.4.1.8301.3.5") )
161 0 : break;
162 0 : if (err)
163 0 : return 0; /* Not found. */
164 0 : der = ksba_cert_get_image (cert, NULL);
165 0 : if (!der)
166 : {
167 0 : err = gpg_error (GPG_ERR_INV_OBJ); /* Oops */
168 0 : goto leave;
169 : }
170 0 : der += off;
171 :
172 0 : err = parse_ber_header (&der, &derlen, &class, &tag, &constructed,
173 : &ndef, &objlen, &hdrlen);
174 0 : if (!err && (objlen > derlen || tag != TAG_SEQUENCE))
175 0 : err = gpg_error (GPG_ERR_INV_OBJ);
176 0 : if (err)
177 0 : goto leave;
178 0 : derlen = objlen;
179 0 : err = parse_ber_header (&der, &derlen, &class, &tag, &constructed,
180 : &ndef, &objlen, &hdrlen);
181 0 : if (!err && (objlen > derlen || tag != TAG_OBJECT_ID))
182 0 : err = gpg_error (GPG_ERR_INV_OBJ);
183 0 : if (err)
184 0 : goto leave;
185 0 : oidbuf = ksba_oid_to_str (der, objlen);
186 0 : if (!oidbuf)
187 : {
188 0 : err = gpg_error_from_syserror ();
189 0 : goto leave;
190 : }
191 :
192 0 : if (opt.verbose)
193 0 : do_list (0, listmode, listfp,
194 0 : _("validation model requested by certificate: %s"),
195 0 : !strcmp (oidbuf, "1.3.6.1.4.1.8301.3.5.1")? _("chain") :
196 0 : !strcmp (oidbuf, "1.3.6.1.4.1.8301.3.5.2")? _("shell") :
197 : /* */ oidbuf);
198 0 : yes = !strcmp (oidbuf, "1.3.6.1.4.1.8301.3.5.1");
199 0 : ksba_free (oidbuf);
200 0 : return yes;
201 :
202 :
203 : leave:
204 0 : log_error ("error parsing validityModel: %s\n", gpg_strerror (err));
205 0 : return 0;
206 : }
207 :
208 :
209 :
210 : static int
211 0 : unknown_criticals (ksba_cert_t cert, int listmode, estream_t fp)
212 : {
213 : static const char *known[] = {
214 : "2.5.29.15", /* keyUsage */
215 : "2.5.29.17", /* subjectAltName
216 : Japanese DoCoMo certs mark them as critical. PKIX
217 : only requires them as critical if subjectName is
218 : empty. I don't know whether our code gracefully
219 : handles such empry subjectNames but that is
220 : another story. */
221 : "2.5.29.19", /* basic Constraints */
222 : "2.5.29.32", /* certificatePolicies */
223 : "2.5.29.37", /* extendedKeyUsage - handled by certlist.c */
224 : "1.3.6.1.4.1.8301.3.5", /* validityModel - handled here. */
225 : NULL
226 : };
227 0 : int rc = 0, i, idx, crit;
228 : const char *oid;
229 : gpg_error_t err;
230 : int unsupported;
231 : strlist_t sl;
232 :
233 0 : for (idx=0; !(err=ksba_cert_get_extension (cert, idx,
234 0 : &oid, &crit, NULL, NULL));idx++)
235 : {
236 0 : if (!crit)
237 0 : continue;
238 0 : for (i=0; known[i] && strcmp (known[i],oid); i++)
239 : ;
240 0 : unsupported = !known[i];
241 :
242 : /* If this critical extension is not supported. Check the list
243 : of to be ignored extensions to see whether we claim that it
244 : is supported. */
245 0 : if (unsupported && opt.ignored_cert_extensions)
246 : {
247 0 : for (sl=opt.ignored_cert_extensions;
248 0 : sl && strcmp (sl->d, oid); sl = sl->next)
249 : ;
250 0 : if (sl)
251 0 : unsupported = 0;
252 : }
253 0 : if (unsupported)
254 : {
255 0 : do_list (1, listmode, fp,
256 0 : _("critical certificate extension %s is not supported"),
257 : oid);
258 0 : rc = gpg_error (GPG_ERR_UNSUPPORTED_CERT);
259 : }
260 : }
261 : /* We ignore the error codes EOF as well as no-value. The later will
262 : occur for certificates with no extensions at all. */
263 0 : if (err
264 0 : && gpg_err_code (err) != GPG_ERR_EOF
265 0 : && gpg_err_code (err) != GPG_ERR_NO_VALUE)
266 0 : rc = err;
267 :
268 0 : return rc;
269 : }
270 :
271 :
272 : /* Check whether CERT is an allowed certificate. This requires that
273 : CERT matches all requirements for such a CA, i.e. the
274 : BasicConstraints extension. The function returns 0 on success and
275 : the allowed length of the chain at CHAINLEN. */
276 : static int
277 0 : allowed_ca (ctrl_t ctrl,
278 : ksba_cert_t cert, int *chainlen, int listmode, estream_t fp)
279 : {
280 : gpg_error_t err;
281 : int flag;
282 :
283 0 : err = ksba_cert_is_ca (cert, &flag, chainlen);
284 0 : if (err)
285 0 : return err;
286 0 : if (!flag)
287 : {
288 0 : if (get_regtp_ca_info (ctrl, cert, chainlen))
289 : {
290 : /* Note that dirmngr takes a different way to cope with such
291 : certs. */
292 0 : return 0; /* RegTP issued certificate. */
293 : }
294 :
295 0 : do_list (1, listmode, fp,_("issuer certificate is not marked as a CA"));
296 0 : return gpg_error (GPG_ERR_BAD_CA_CERT);
297 : }
298 0 : return 0;
299 : }
300 :
301 :
302 : static int
303 0 : check_cert_policy (ksba_cert_t cert, int listmode, estream_t fplist)
304 : {
305 : gpg_error_t err;
306 : char *policies;
307 : FILE *fp;
308 : int any_critical;
309 :
310 0 : err = ksba_cert_get_cert_policies (cert, &policies);
311 0 : if (gpg_err_code (err) == GPG_ERR_NO_DATA)
312 0 : return 0; /* No policy given. */
313 0 : if (err)
314 0 : return err;
315 :
316 : /* STRING is a line delimited list of certificate policies as stored
317 : in the certificate. The line itself is colon delimited where the
318 : first field is the OID of the policy and the second field either
319 : N or C for normal or critical extension */
320 :
321 0 : if (opt.verbose > 1 && !listmode)
322 0 : log_info ("certificate's policy list: %s\n", policies);
323 :
324 : /* The check is very minimal but won't give false positives */
325 0 : any_critical = !!strstr (policies, ":C");
326 :
327 0 : if (!opt.policy_file)
328 : {
329 0 : xfree (policies);
330 0 : if (any_critical)
331 : {
332 0 : do_list (1, listmode, fplist,
333 0 : _("critical marked policy without configured policies"));
334 0 : return gpg_error (GPG_ERR_NO_POLICY_MATCH);
335 : }
336 0 : return 0;
337 : }
338 :
339 0 : fp = fopen (opt.policy_file, "r");
340 0 : if (!fp)
341 : {
342 0 : if (opt.verbose || errno != ENOENT)
343 0 : log_info (_("failed to open '%s': %s\n"),
344 0 : opt.policy_file, strerror (errno));
345 0 : xfree (policies);
346 : /* With no critical policies this is only a warning */
347 0 : if (!any_critical)
348 : {
349 0 : if (!opt.quiet)
350 0 : do_list (0, listmode, fplist,
351 0 : _("Note: non-critical certificate policy not allowed"));
352 0 : return 0;
353 : }
354 0 : do_list (1, listmode, fplist,
355 0 : _("certificate policy not allowed"));
356 0 : return gpg_error (GPG_ERR_NO_POLICY_MATCH);
357 : }
358 :
359 : for (;;)
360 : {
361 : int c;
362 : char *p, line[256];
363 : char *haystack, *allowed;
364 :
365 : /* read line */
366 : do
367 : {
368 0 : if (!fgets (line, DIM(line)-1, fp) )
369 : {
370 0 : gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
371 :
372 0 : xfree (policies);
373 0 : if (feof (fp))
374 : {
375 0 : fclose (fp);
376 : /* With no critical policies this is only a warning */
377 0 : if (!any_critical)
378 : {
379 0 : do_list (0, listmode, fplist,
380 0 : _("Note: non-critical certificate policy not allowed"));
381 0 : return 0;
382 : }
383 0 : do_list (1, listmode, fplist,
384 0 : _("certificate policy not allowed"));
385 0 : return gpg_error (GPG_ERR_NO_POLICY_MATCH);
386 : }
387 0 : fclose (fp);
388 0 : return tmperr;
389 : }
390 :
391 0 : if (!*line || line[strlen(line)-1] != '\n')
392 : {
393 : /* eat until end of line */
394 0 : while ( (c=getc (fp)) != EOF && c != '\n')
395 : ;
396 0 : fclose (fp);
397 0 : xfree (policies);
398 0 : return gpg_error (*line? GPG_ERR_LINE_TOO_LONG
399 : : GPG_ERR_INCOMPLETE_LINE);
400 : }
401 :
402 : /* Allow for empty lines and spaces */
403 0 : for (p=line; spacep (p); p++)
404 : ;
405 : }
406 0 : while (!*p || *p == '\n' || *p == '#');
407 :
408 : /* parse line */
409 0 : for (allowed=line; spacep (allowed); allowed++)
410 : ;
411 0 : p = strpbrk (allowed, " :\n");
412 0 : if (!*p || p == allowed)
413 : {
414 0 : fclose (fp);
415 0 : xfree (policies);
416 0 : return gpg_error (GPG_ERR_CONFIGURATION);
417 : }
418 0 : *p = 0; /* strip the rest of the line */
419 : /* See whether we find ALLOWED (which is an OID) in POLICIES */
420 0 : for (haystack=policies; (p=strstr (haystack, allowed)); haystack = p+1)
421 : {
422 0 : if ( !(p == policies || p[-1] == '\n') )
423 0 : continue; /* Does not match the begin of a line. */
424 0 : if (p[strlen (allowed)] != ':')
425 0 : continue; /* The length does not match. */
426 : /* Yep - it does match so return okay. */
427 0 : fclose (fp);
428 0 : xfree (policies);
429 0 : return 0;
430 : }
431 0 : }
432 : }
433 :
434 :
435 : /* Helper function for find_up. This resets the key handle and search
436 : for an issuer ISSUER with a subjectKeyIdentifier of KEYID. Returns
437 : 0 on success or -1 when not found. */
438 : static int
439 0 : find_up_search_by_keyid (KEYDB_HANDLE kh,
440 : const char *issuer, ksba_sexp_t keyid)
441 : {
442 : int rc;
443 0 : ksba_cert_t cert = NULL;
444 0 : ksba_sexp_t subj = NULL;
445 0 : int anyfound = 0;
446 : ksba_isotime_t not_before, last_not_before;
447 :
448 0 : keydb_search_reset (kh);
449 0 : while (!(rc = keydb_search_subject (kh, issuer)))
450 : {
451 0 : ksba_cert_release (cert); cert = NULL;
452 0 : rc = keydb_get_cert (kh, &cert);
453 0 : if (rc)
454 : {
455 0 : log_error ("keydb_get_cert() failed: rc=%d\n", rc);
456 0 : rc = -1;
457 0 : break;
458 : }
459 0 : xfree (subj);
460 0 : if (!ksba_cert_get_subj_key_id (cert, NULL, &subj))
461 : {
462 0 : if (!cmp_simple_canon_sexp (keyid, subj))
463 : {
464 : /* Found matching cert. */
465 0 : rc = ksba_cert_get_validity (cert, 0, not_before);
466 0 : if (rc)
467 : {
468 0 : log_error ("keydb_get_validity() failed: rc=%d\n", rc);
469 0 : rc = -1;
470 0 : break;
471 : }
472 :
473 0 : if (!anyfound || strcmp (last_not_before, not_before) < 0)
474 : {
475 : /* This certificate is the first one found or newer
476 : than the previous one. This copes with
477 : re-issuing CA certificates while keeping the same
478 : key information. */
479 0 : anyfound = 1;
480 0 : gnupg_copy_time (last_not_before, not_before);
481 0 : keydb_push_found_state (kh);
482 : }
483 : }
484 : }
485 : }
486 :
487 0 : if (anyfound)
488 : {
489 : /* Take the last saved one. */
490 0 : keydb_pop_found_state (kh);
491 0 : rc = 0; /* Ignore EOF or other error after the first cert. */
492 : }
493 :
494 0 : ksba_cert_release (cert);
495 0 : xfree (subj);
496 0 : return rc? -1:0;
497 : }
498 :
499 :
500 : static void
501 0 : find_up_store_certs_cb (void *cb_value, ksba_cert_t cert)
502 : {
503 0 : if (keydb_store_cert (cert, 1, NULL))
504 0 : log_error ("error storing issuer certificate as ephemeral\n");
505 0 : ++*(int*)cb_value;
506 0 : }
507 :
508 :
509 : /* Helper for find_up(). Locate the certificate for ISSUER using an
510 : external lookup. KH is the keydb context we are currently using.
511 : On success 0 is returned and the certificate may be retrieved from
512 : the keydb using keydb_get_cert(). KEYID is the keyIdentifier from
513 : the AKI or NULL. */
514 : static int
515 0 : find_up_external (ctrl_t ctrl, KEYDB_HANDLE kh,
516 : const char *issuer, ksba_sexp_t keyid)
517 : {
518 : int rc;
519 0 : strlist_t names = NULL;
520 0 : int count = 0;
521 : char *pattern;
522 : const char *s;
523 :
524 0 : if (opt.verbose)
525 0 : log_info (_("looking up issuer at external location\n"));
526 : /* The Dirmngr process is confused about unknown attributes. As a
527 : quick and ugly hack we locate the CN and use the issuer string
528 : starting at this attribite. Fixme: we should have far better
529 : parsing for external lookups in the Dirmngr. */
530 0 : s = strstr (issuer, "CN=");
531 0 : if (!s || s == issuer || s[-1] != ',')
532 0 : s = issuer;
533 0 : pattern = xtrymalloc (strlen (s)+2);
534 0 : if (!pattern)
535 0 : return gpg_error_from_syserror ();
536 0 : strcpy (stpcpy (pattern, "/"), s);
537 0 : add_to_strlist (&names, pattern);
538 0 : xfree (pattern);
539 :
540 0 : rc = gpgsm_dirmngr_lookup (ctrl, names, 0, find_up_store_certs_cb, &count);
541 0 : free_strlist (names);
542 :
543 0 : if (opt.verbose)
544 0 : log_info (_("number of issuers matching: %d\n"), count);
545 0 : if (rc)
546 : {
547 0 : log_error ("external key lookup failed: %s\n", gpg_strerror (rc));
548 0 : rc = -1;
549 : }
550 0 : else if (!count)
551 0 : rc = -1;
552 : else
553 : {
554 : int old;
555 : /* The issuers are currently stored in the ephemeral key DB, so
556 : we temporary switch to ephemeral mode. */
557 0 : old = keydb_set_ephemeral (kh, 1);
558 0 : if (keyid)
559 0 : rc = find_up_search_by_keyid (kh, issuer, keyid);
560 : else
561 : {
562 0 : keydb_search_reset (kh);
563 0 : rc = keydb_search_subject (kh, issuer);
564 : }
565 0 : keydb_set_ephemeral (kh, old);
566 : }
567 0 : return rc;
568 : }
569 :
570 :
571 : /* Helper for find_up(). Ask the dirmngr for the certificate for
572 : ISSUER with optional SERIALNO. KH is the keydb context we are
573 : currently using. With SUBJECT_MODE set, ISSUER is searched as the
574 : subject. On success 0 is returned and the certificate is available
575 : in the ephemeral DB. */
576 : static int
577 0 : find_up_dirmngr (ctrl_t ctrl, KEYDB_HANDLE kh,
578 : ksba_sexp_t serialno, const char *issuer, int subject_mode)
579 : {
580 : int rc;
581 0 : strlist_t names = NULL;
582 0 : int count = 0;
583 : char *pattern;
584 :
585 : (void)kh;
586 :
587 0 : if (opt.verbose)
588 0 : log_info (_("looking up issuer from the Dirmngr cache\n"));
589 0 : if (subject_mode)
590 : {
591 0 : pattern = xtrymalloc (strlen (issuer)+2);
592 0 : if (pattern)
593 0 : strcpy (stpcpy (pattern, "/"), issuer);
594 : }
595 0 : else if (serialno)
596 0 : pattern = gpgsm_format_sn_issuer (serialno, issuer);
597 : else
598 : {
599 0 : pattern = xtrymalloc (strlen (issuer)+3);
600 0 : if (pattern)
601 0 : strcpy (stpcpy (pattern, "#/"), issuer);
602 : }
603 0 : if (!pattern)
604 0 : return gpg_error_from_syserror ();
605 0 : add_to_strlist (&names, pattern);
606 0 : xfree (pattern);
607 :
608 0 : rc = gpgsm_dirmngr_lookup (ctrl, names, 1, find_up_store_certs_cb, &count);
609 0 : free_strlist (names);
610 :
611 0 : if (opt.verbose)
612 0 : log_info (_("number of matching certificates: %d\n"), count);
613 0 : if (rc && !opt.quiet)
614 0 : log_info (_("dirmngr cache-only key lookup failed: %s\n"),
615 : gpg_strerror (rc));
616 0 : return (!rc && count)? 0 : -1;
617 : }
618 :
619 :
620 :
621 : /* Locate issuing certificate for CERT. ISSUER is the name of the
622 : issuer used as a fallback if the other methods don't work. If
623 : FIND_NEXT is true, the function shall return the next possible
624 : issuer. The certificate itself is not directly returned but a
625 : keydb_get_cert on the keyDb context KH will return it. Returns 0
626 : on success, -1 if not found or an error code. */
627 : static int
628 0 : find_up (ctrl_t ctrl, KEYDB_HANDLE kh,
629 : ksba_cert_t cert, const char *issuer, int find_next)
630 : {
631 : ksba_name_t authid;
632 : ksba_sexp_t authidno;
633 : ksba_sexp_t keyid;
634 0 : int rc = -1;
635 :
636 0 : if (DBG_X509)
637 0 : log_debug ("looking for parent certificate\n");
638 0 : if (!ksba_cert_get_auth_key_id (cert, &keyid, &authid, &authidno))
639 : {
640 0 : const char *s = ksba_name_enum (authid, 0);
641 0 : if (s && *authidno)
642 : {
643 0 : rc = keydb_search_issuer_sn (kh, s, authidno);
644 0 : if (rc)
645 0 : keydb_search_reset (kh);
646 :
647 0 : if (!rc && DBG_X509)
648 0 : log_debug (" found via authid and sn+issuer\n");
649 :
650 : /* In case of an error, try to get the certificate from the
651 : dirmngr. That is done by trying to put that certifcate
652 : into the ephemeral DB and let the code below do the
653 : actual retrieve. Thus there is no error checking.
654 : Skipped in find_next mode as usual. */
655 0 : if (rc == -1 && !find_next)
656 0 : find_up_dirmngr (ctrl, kh, authidno, s, 0);
657 :
658 : /* In case of an error try the ephemeral DB. We can't do
659 : that in find_next mode because we can't keep the search
660 : state then. */
661 0 : if (rc == -1 && !find_next)
662 : {
663 0 : int old = keydb_set_ephemeral (kh, 1);
664 0 : if (!old)
665 : {
666 0 : rc = keydb_search_issuer_sn (kh, s, authidno);
667 0 : if (rc)
668 0 : keydb_search_reset (kh);
669 :
670 0 : if (!rc && DBG_X509)
671 0 : log_debug (" found via authid and sn+issuer (ephem)\n");
672 : }
673 0 : keydb_set_ephemeral (kh, old);
674 : }
675 0 : if (rc)
676 0 : rc = -1; /* Need to make sure to have this error code. */
677 : }
678 :
679 0 : if (rc == -1 && keyid && !find_next)
680 : {
681 : /* Not found by AIK.issuer_sn. Lets try the AIK.ki
682 : instead. Loop over all certificates with that issuer as
683 : subject and stop for the one with a matching
684 : subjectKeyIdentifier. */
685 : /* Fixme: Should we also search in the dirmngr? */
686 0 : rc = find_up_search_by_keyid (kh, issuer, keyid);
687 0 : if (!rc && DBG_X509)
688 0 : log_debug (" found via authid and keyid\n");
689 0 : if (rc)
690 : {
691 0 : int old = keydb_set_ephemeral (kh, 1);
692 0 : if (!old)
693 0 : rc = find_up_search_by_keyid (kh, issuer, keyid);
694 0 : if (!rc && DBG_X509)
695 0 : log_debug (" found via authid and keyid (ephem)\n");
696 0 : keydb_set_ephemeral (kh, old);
697 : }
698 0 : if (rc)
699 0 : rc = -1; /* Need to make sure to have this error code. */
700 : }
701 :
702 : /* If we still didn't found it, try to find it via the subject
703 : from the dirmngr-cache. */
704 0 : if (rc == -1 && !find_next)
705 : {
706 0 : if (!find_up_dirmngr (ctrl, kh, NULL, issuer, 1))
707 : {
708 0 : int old = keydb_set_ephemeral (kh, 1);
709 0 : if (keyid)
710 0 : rc = find_up_search_by_keyid (kh, issuer, keyid);
711 : else
712 : {
713 0 : keydb_search_reset (kh);
714 0 : rc = keydb_search_subject (kh, issuer);
715 : }
716 0 : keydb_set_ephemeral (kh, old);
717 : }
718 0 : if (rc)
719 0 : rc = -1; /* Need to make sure to have this error code. */
720 :
721 0 : if (!rc && DBG_X509)
722 0 : log_debug (" found via authid and issuer from dirmngr cache\n");
723 : }
724 :
725 : /* If we still didn't found it, try an external lookup. */
726 0 : if (rc == -1 && opt.auto_issuer_key_retrieve && !find_next)
727 : {
728 0 : rc = find_up_external (ctrl, kh, issuer, keyid);
729 0 : if (!rc && DBG_X509)
730 0 : log_debug (" found via authid and external lookup\n");
731 : }
732 :
733 :
734 : /* Print a note so that the user does not feel too helpless when
735 : an issuer certificate was found and gpgsm prints BAD
736 : signature because it is not the correct one. */
737 0 : if (rc == -1 && opt.quiet)
738 : ;
739 0 : else if (rc == -1)
740 : {
741 0 : log_info ("%sissuer certificate ", find_next?"next ":"");
742 0 : if (keyid)
743 : {
744 0 : log_printf ("{");
745 0 : gpgsm_dump_serial (keyid);
746 0 : log_printf ("} ");
747 : }
748 0 : if (authidno)
749 : {
750 0 : log_printf ("(#");
751 0 : gpgsm_dump_serial (authidno);
752 0 : log_printf ("/");
753 0 : gpgsm_dump_string (s);
754 0 : log_printf (") ");
755 : }
756 0 : log_printf ("not found using authorityKeyIdentifier\n");
757 : }
758 0 : else if (rc)
759 0 : log_error ("failed to find authorityKeyIdentifier: rc=%d\n", rc);
760 0 : xfree (keyid);
761 0 : ksba_name_release (authid);
762 0 : xfree (authidno);
763 : }
764 :
765 0 : if (rc) /* Not found via authorithyKeyIdentifier, try regular issuer name. */
766 0 : rc = keydb_search_subject (kh, issuer);
767 0 : if (rc == -1 && !find_next)
768 : {
769 : int old;
770 :
771 : /* Also try to get it from the Dirmngr cache. The function
772 : merely puts it into the ephemeral database. */
773 0 : find_up_dirmngr (ctrl, kh, NULL, issuer, 0);
774 :
775 : /* Not found, let us see whether we have one in the ephemeral key DB. */
776 0 : old = keydb_set_ephemeral (kh, 1);
777 0 : if (!old)
778 : {
779 0 : keydb_search_reset (kh);
780 0 : rc = keydb_search_subject (kh, issuer);
781 : }
782 0 : keydb_set_ephemeral (kh, old);
783 :
784 0 : if (!rc && DBG_X509)
785 0 : log_debug (" found via issuer\n");
786 : }
787 :
788 : /* Still not found. If enabled, try an external lookup. */
789 0 : if (rc == -1 && opt.auto_issuer_key_retrieve && !find_next)
790 : {
791 0 : rc = find_up_external (ctrl, kh, issuer, NULL);
792 0 : if (!rc && DBG_X509)
793 0 : log_debug (" found via issuer and external lookup\n");
794 : }
795 :
796 0 : return rc;
797 : }
798 :
799 :
800 : /* Return the next certificate up in the chain starting at START.
801 : Returns -1 when there are no more certificates. */
802 : int
803 3 : gpgsm_walk_cert_chain (ctrl_t ctrl, ksba_cert_t start, ksba_cert_t *r_next)
804 : {
805 3 : int rc = 0;
806 3 : char *issuer = NULL;
807 3 : char *subject = NULL;
808 3 : KEYDB_HANDLE kh = keydb_new (0);
809 :
810 3 : *r_next = NULL;
811 3 : if (!kh)
812 : {
813 0 : log_error (_("failed to allocate keyDB handle\n"));
814 0 : rc = gpg_error (GPG_ERR_GENERAL);
815 0 : goto leave;
816 : }
817 :
818 3 : issuer = ksba_cert_get_issuer (start, 0);
819 3 : subject = ksba_cert_get_subject (start, 0);
820 3 : if (!issuer)
821 : {
822 0 : log_error ("no issuer found in certificate\n");
823 0 : rc = gpg_error (GPG_ERR_BAD_CERT);
824 0 : goto leave;
825 : }
826 3 : if (!subject)
827 : {
828 0 : log_error ("no subject found in certificate\n");
829 0 : rc = gpg_error (GPG_ERR_BAD_CERT);
830 0 : goto leave;
831 : }
832 :
833 3 : if (is_root_cert (start, issuer, subject))
834 : {
835 3 : rc = -1; /* we are at the root */
836 3 : goto leave;
837 : }
838 :
839 0 : rc = find_up (ctrl, kh, start, issuer, 0);
840 0 : if (rc)
841 : {
842 : /* It is quite common not to have a certificate, so better don't
843 : print an error here. */
844 0 : if (rc != -1 && opt.verbose > 1)
845 0 : log_error ("failed to find issuer's certificate: rc=%d\n", rc);
846 0 : rc = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
847 0 : goto leave;
848 : }
849 :
850 0 : rc = keydb_get_cert (kh, r_next);
851 0 : if (rc)
852 : {
853 0 : log_error ("keydb_get_cert() failed: rc=%d\n", rc);
854 0 : rc = gpg_error (GPG_ERR_GENERAL);
855 : }
856 :
857 : leave:
858 3 : xfree (issuer);
859 3 : xfree (subject);
860 3 : keydb_release (kh);
861 3 : return rc;
862 : }
863 :
864 :
865 : /* Helper for gpgsm_is_root_cert. This one is used if the subject and
866 : issuer DNs are already known. */
867 : static int
868 6 : is_root_cert (ksba_cert_t cert, const char *issuerdn, const char *subjectdn)
869 : {
870 : gpg_error_t err;
871 6 : int result = 0;
872 : ksba_sexp_t serialno;
873 : ksba_sexp_t ak_keyid;
874 : ksba_name_t ak_name;
875 : ksba_sexp_t ak_sn;
876 : const char *ak_name_str;
877 6 : ksba_sexp_t subj_keyid = NULL;
878 :
879 6 : if (!issuerdn || !subjectdn)
880 0 : return 0; /* No. */
881 :
882 6 : if (strcmp (issuerdn, subjectdn))
883 0 : return 0; /* No. */
884 :
885 6 : err = ksba_cert_get_auth_key_id (cert, &ak_keyid, &ak_name, &ak_sn);
886 6 : if (err)
887 : {
888 0 : if (gpg_err_code (err) == GPG_ERR_NO_DATA)
889 0 : return 1; /* Yes. Without a authorityKeyIdentifier this needs
890 : to be the Root certifcate (our trust anchor). */
891 0 : log_error ("error getting authorityKeyIdentifier: %s\n",
892 : gpg_strerror (err));
893 0 : return 0; /* Well, it is broken anyway. Return No. */
894 : }
895 :
896 6 : serialno = ksba_cert_get_serial (cert);
897 6 : if (!serialno)
898 : {
899 0 : log_error ("error getting serialno: %s\n", gpg_strerror (err));
900 0 : goto leave;
901 : }
902 :
903 : /* Check whether the auth name's matches the issuer name+sn. If
904 : that is the case this is a root certificate. */
905 6 : ak_name_str = ksba_name_enum (ak_name, 0);
906 6 : if (ak_name_str
907 6 : && !strcmp (ak_name_str, issuerdn)
908 6 : && !cmp_simple_canon_sexp (ak_sn, serialno))
909 : {
910 6 : result = 1; /* Right, CERT is self-signed. */
911 6 : goto leave;
912 : }
913 :
914 : /* Similar for the ak_keyid. */
915 0 : if (ak_keyid && !ksba_cert_get_subj_key_id (cert, NULL, &subj_keyid)
916 0 : && !cmp_simple_canon_sexp (ak_keyid, subj_keyid))
917 : {
918 0 : result = 1; /* Right, CERT is self-signed. */
919 0 : goto leave;
920 : }
921 :
922 :
923 : leave:
924 6 : ksba_free (subj_keyid);
925 6 : ksba_free (ak_keyid);
926 6 : ksba_name_release (ak_name);
927 6 : ksba_free (ak_sn);
928 6 : ksba_free (serialno);
929 6 : return result;
930 : }
931 :
932 :
933 :
934 : /* Check whether the CERT is a root certificate. Returns True if this
935 : is the case. */
936 : int
937 0 : gpgsm_is_root_cert (ksba_cert_t cert)
938 : {
939 : char *issuer;
940 : char *subject;
941 : int yes;
942 :
943 0 : issuer = ksba_cert_get_issuer (cert, 0);
944 0 : subject = ksba_cert_get_subject (cert, 0);
945 0 : yes = is_root_cert (cert, issuer, subject);
946 0 : xfree (issuer);
947 0 : xfree (subject);
948 0 : return yes;
949 : }
950 :
951 :
952 : /* This is a helper for gpgsm_validate_chain. */
953 : static gpg_error_t
954 0 : is_cert_still_valid (ctrl_t ctrl, int force_ocsp, int lm, estream_t fp,
955 : ksba_cert_t subject_cert, ksba_cert_t issuer_cert,
956 : int *any_revoked, int *any_no_crl, int *any_crl_too_old)
957 : {
958 : gpg_error_t err;
959 :
960 0 : if (ctrl->offline || (opt.no_crl_check && !ctrl->use_ocsp))
961 : {
962 0 : audit_log_ok (ctrl->audit, AUDIT_CRL_CHECK,
963 : gpg_error (GPG_ERR_NOT_ENABLED));
964 0 : return 0;
965 : }
966 :
967 0 : err = gpgsm_dirmngr_isvalid (ctrl,
968 : subject_cert, issuer_cert,
969 0 : force_ocsp? 2 : !!ctrl->use_ocsp);
970 0 : audit_log_ok (ctrl->audit, AUDIT_CRL_CHECK, err);
971 :
972 0 : if (err)
973 : {
974 0 : if (!lm)
975 0 : gpgsm_cert_log_name (NULL, subject_cert);
976 0 : switch (gpg_err_code (err))
977 : {
978 : case GPG_ERR_CERT_REVOKED:
979 0 : do_list (1, lm, fp, _("certificate has been revoked"));
980 0 : *any_revoked = 1;
981 : /* Store that in the keybox so that key listings are able to
982 : return the revoked flag. We don't care about error,
983 : though. */
984 0 : keydb_set_cert_flags (subject_cert, 1, KEYBOX_FLAG_VALIDITY, 0,
985 : ~0, VALIDITY_REVOKED);
986 0 : break;
987 :
988 : case GPG_ERR_NO_CRL_KNOWN:
989 0 : do_list (1, lm, fp, _("no CRL found for certificate"));
990 0 : *any_no_crl = 1;
991 0 : break;
992 :
993 : case GPG_ERR_NO_DATA:
994 0 : do_list (1, lm, fp, _("the status of the certificate is unknown"));
995 0 : *any_no_crl = 1;
996 0 : break;
997 :
998 : case GPG_ERR_CRL_TOO_OLD:
999 0 : do_list (1, lm, fp, _("the available CRL is too old"));
1000 0 : if (!lm)
1001 0 : log_info (_("please make sure that the "
1002 : "\"dirmngr\" is properly installed\n"));
1003 0 : *any_crl_too_old = 1;
1004 0 : break;
1005 :
1006 : default:
1007 0 : do_list (1, lm, fp, _("checking the CRL failed: %s"),
1008 : gpg_strerror (err));
1009 0 : return err;
1010 : }
1011 : }
1012 0 : return 0;
1013 : }
1014 :
1015 :
1016 : /* Helper for gpgsm_validate_chain to check the validity period of
1017 : SUBJECT_CERT. The caller needs to pass EXPTIME which will be
1018 : updated to the nearest expiration time seen. A DEPTH of 0 indicates
1019 : the target certifciate, -1 the final root certificate and other
1020 : values intermediate certificates. */
1021 : static gpg_error_t
1022 0 : check_validity_period (ksba_isotime_t current_time,
1023 : ksba_cert_t subject_cert,
1024 : ksba_isotime_t exptime,
1025 : int listmode, estream_t listfp, int depth)
1026 : {
1027 : gpg_error_t err;
1028 : ksba_isotime_t not_before, not_after;
1029 :
1030 0 : err = ksba_cert_get_validity (subject_cert, 0, not_before);
1031 0 : if (!err)
1032 0 : err = ksba_cert_get_validity (subject_cert, 1, not_after);
1033 0 : if (err)
1034 : {
1035 0 : do_list (1, listmode, listfp,
1036 0 : _("certificate with invalid validity: %s"), gpg_strerror (err));
1037 0 : return gpg_error (GPG_ERR_BAD_CERT);
1038 : }
1039 :
1040 0 : if (*not_after)
1041 : {
1042 0 : if (!*exptime)
1043 0 : gnupg_copy_time (exptime, not_after);
1044 0 : else if (strcmp (not_after, exptime) < 0 )
1045 0 : gnupg_copy_time (exptime, not_after);
1046 : }
1047 :
1048 0 : if (*not_before && strcmp (current_time, not_before) < 0 )
1049 : {
1050 0 : do_list (1, listmode, listfp,
1051 : depth == 0 ? _("certificate not yet valid") :
1052 : depth == -1 ? _("root certificate not yet valid") :
1053 : /* other */ _("intermediate certificate not yet valid"));
1054 0 : if (!listmode)
1055 : {
1056 0 : log_info (" (valid from ");
1057 0 : dump_isotime (not_before);
1058 0 : log_printf (")\n");
1059 : }
1060 0 : return gpg_error (GPG_ERR_CERT_TOO_YOUNG);
1061 : }
1062 :
1063 0 : if (*not_after && strcmp (current_time, not_after) > 0 )
1064 : {
1065 0 : do_list (opt.ignore_expiration?0:1, listmode, listfp,
1066 : depth == 0 ? _("certificate has expired") :
1067 : depth == -1 ? _("root certificate has expired") :
1068 : /* other */ _("intermediate certificate has expired"));
1069 0 : if (!listmode)
1070 : {
1071 0 : log_info (" (expired at ");
1072 0 : dump_isotime (not_after);
1073 0 : log_printf (")\n");
1074 : }
1075 0 : if (opt.ignore_expiration)
1076 0 : log_info ("WARNING: ignoring expiration\n");
1077 : else
1078 0 : return gpg_error (GPG_ERR_CERT_EXPIRED);
1079 : }
1080 :
1081 0 : return 0;
1082 : }
1083 :
1084 : /* This is a variant of check_validity_period used with the chain
1085 : model. The dextra contraint here is that notBefore and notAfter
1086 : must exists and if the additional argument CHECK_TIME is given this
1087 : time is used to check the validity period of SUBJECT_CERT. */
1088 : static gpg_error_t
1089 0 : check_validity_period_cm (ksba_isotime_t current_time,
1090 : ksba_isotime_t check_time,
1091 : ksba_cert_t subject_cert,
1092 : ksba_isotime_t exptime,
1093 : int listmode, estream_t listfp, int depth)
1094 : {
1095 : gpg_error_t err;
1096 : ksba_isotime_t not_before, not_after;
1097 :
1098 0 : err = ksba_cert_get_validity (subject_cert, 0, not_before);
1099 0 : if (!err)
1100 0 : err = ksba_cert_get_validity (subject_cert, 1, not_after);
1101 0 : if (err)
1102 : {
1103 0 : do_list (1, listmode, listfp,
1104 0 : _("certificate with invalid validity: %s"), gpg_strerror (err));
1105 0 : return gpg_error (GPG_ERR_BAD_CERT);
1106 : }
1107 0 : if (!*not_before || !*not_after)
1108 : {
1109 0 : do_list (1, listmode, listfp,
1110 0 : _("required certificate attributes missing: %s%s%s"),
1111 0 : !*not_before? "notBefore":"",
1112 0 : (!*not_before && !*not_after)? ", ":"",
1113 0 : !*not_before? "notAfter":"");
1114 0 : return gpg_error (GPG_ERR_BAD_CERT);
1115 : }
1116 0 : if (strcmp (not_before, not_after) > 0 )
1117 : {
1118 0 : do_list (1, listmode, listfp,
1119 0 : _("certificate with invalid validity"));
1120 0 : log_info (" (valid from ");
1121 0 : dump_isotime (not_before);
1122 0 : log_printf (" expired at ");
1123 0 : dump_isotime (not_after);
1124 0 : log_printf (")\n");
1125 0 : return gpg_error (GPG_ERR_BAD_CERT);
1126 : }
1127 :
1128 0 : if (!*exptime)
1129 0 : gnupg_copy_time (exptime, not_after);
1130 0 : else if (strcmp (not_after, exptime) < 0 )
1131 0 : gnupg_copy_time (exptime, not_after);
1132 :
1133 0 : if (strcmp (current_time, not_before) < 0 )
1134 : {
1135 0 : do_list (1, listmode, listfp,
1136 : depth == 0 ? _("certificate not yet valid") :
1137 : depth == -1 ? _("root certificate not yet valid") :
1138 : /* other */ _("intermediate certificate not yet valid"));
1139 0 : if (!listmode)
1140 : {
1141 0 : log_info (" (valid from ");
1142 0 : dump_isotime (not_before);
1143 0 : log_printf (")\n");
1144 : }
1145 0 : return gpg_error (GPG_ERR_CERT_TOO_YOUNG);
1146 : }
1147 :
1148 0 : if (*check_time
1149 0 : && (strcmp (check_time, not_before) < 0
1150 0 : || strcmp (check_time, not_after) > 0))
1151 : {
1152 : /* Note that we don't need a case for the root certificate
1153 : because its own consitency has already been checked. */
1154 0 : do_list(opt.ignore_expiration?0:1, listmode, listfp,
1155 : depth == 0 ?
1156 : _("signature not created during lifetime of certificate") :
1157 : depth == 1 ?
1158 : _("certificate not created during lifetime of issuer") :
1159 : _("intermediate certificate not created during lifetime "
1160 : "of issuer"));
1161 0 : if (!listmode)
1162 : {
1163 0 : log_info (depth== 0? _(" ( signature created at ") :
1164 : /* */ _(" (certificate created at ") );
1165 0 : dump_isotime (check_time);
1166 0 : log_printf (")\n");
1167 0 : log_info (depth==0? _(" (certificate valid from ") :
1168 : /* */ _(" ( issuer valid from ") );
1169 0 : dump_isotime (not_before);
1170 0 : log_info (" to ");
1171 0 : dump_isotime (not_after);
1172 0 : log_printf (")\n");
1173 : }
1174 0 : if (opt.ignore_expiration)
1175 0 : log_info ("WARNING: ignoring expiration\n");
1176 : else
1177 0 : return gpg_error (GPG_ERR_CERT_EXPIRED);
1178 : }
1179 :
1180 0 : return 0;
1181 : }
1182 :
1183 :
1184 :
1185 : /* Ask the user whether he wants to mark the certificate CERT trusted.
1186 : Returns true if the CERT is the trusted. We also check whether the
1187 : agent is at all enabled to allow marktrusted and don't call it in
1188 : this session again if it is not. */
1189 : static int
1190 0 : ask_marktrusted (ctrl_t ctrl, ksba_cert_t cert, int listmode)
1191 : {
1192 : static int no_more_questions;
1193 : int rc;
1194 : char *fpr;
1195 0 : int success = 0;
1196 :
1197 0 : fpr = gpgsm_get_fingerprint_string (cert, GCRY_MD_SHA1);
1198 0 : log_info (_("fingerprint=%s\n"), fpr? fpr : "?");
1199 0 : xfree (fpr);
1200 :
1201 0 : if (no_more_questions)
1202 0 : rc = gpg_error (GPG_ERR_NOT_SUPPORTED);
1203 : else
1204 0 : rc = gpgsm_agent_marktrusted (ctrl, cert);
1205 0 : if (!rc)
1206 : {
1207 0 : log_info (_("root certificate has now been marked as trusted\n"));
1208 0 : success = 1;
1209 : }
1210 0 : else if (!listmode)
1211 : {
1212 0 : gpgsm_dump_cert ("issuer", cert);
1213 0 : log_info ("after checking the fingerprint, you may want "
1214 : "to add it manually to the list of trusted certificates.\n");
1215 : }
1216 :
1217 0 : if (gpg_err_code (rc) == GPG_ERR_NOT_SUPPORTED)
1218 : {
1219 0 : if (!no_more_questions)
1220 0 : log_info (_("interactive marking as trusted "
1221 : "not enabled in gpg-agent\n"));
1222 0 : no_more_questions = 1;
1223 : }
1224 0 : else if (gpg_err_code (rc) == GPG_ERR_CANCELED)
1225 : {
1226 0 : log_info (_("interactive marking as trusted "
1227 : "disabled for this session\n"));
1228 0 : no_more_questions = 1;
1229 : }
1230 : else
1231 0 : set_already_asked_marktrusted (cert);
1232 :
1233 0 : return success;
1234 : }
1235 :
1236 :
1237 :
1238 :
1239 : /* Validate a chain and optionally return the nearest expiration time
1240 : in R_EXPTIME. With LISTMODE set to 1 a special listmode is
1241 : activated where only information about the certificate is printed
1242 : to LISTFP and no output is send to the usual log stream. If
1243 : CHECKTIME_ARG is set, it is used only in the chain model instead of the
1244 : current time.
1245 :
1246 : Defined flag bits
1247 :
1248 : VALIDATE_FLAG_NO_DIRMNGR - Do not do any dirmngr isvalid checks.
1249 : VALIDATE_FLAG_CHAIN_MODEL - Check according to chain model.
1250 : VALIDATE_FLAG_STEED - Check according to the STEED model.
1251 : */
1252 : static int
1253 0 : do_validate_chain (ctrl_t ctrl, ksba_cert_t cert, ksba_isotime_t checktime_arg,
1254 : ksba_isotime_t r_exptime,
1255 : int listmode, estream_t listfp, unsigned int flags,
1256 : struct rootca_flags_s *rootca_flags)
1257 : {
1258 0 : int rc = 0, depth, maxdepth;
1259 0 : char *issuer = NULL;
1260 0 : char *subject = NULL;
1261 0 : KEYDB_HANDLE kh = NULL;
1262 0 : ksba_cert_t subject_cert = NULL, issuer_cert = NULL;
1263 : ksba_isotime_t current_time;
1264 : ksba_isotime_t check_time;
1265 : ksba_isotime_t exptime;
1266 0 : int any_expired = 0;
1267 0 : int any_revoked = 0;
1268 0 : int any_no_crl = 0;
1269 0 : int any_crl_too_old = 0;
1270 0 : int any_no_policy_match = 0;
1271 0 : int is_qualified = -1; /* Indicates whether the certificate stems
1272 : from a qualified root certificate.
1273 : -1 = unknown, 0 = no, 1 = yes. */
1274 0 : chain_item_t chain = NULL; /* A list of all certificates in the chain. */
1275 :
1276 :
1277 0 : gnupg_get_isotime (current_time);
1278 :
1279 0 : if ( (flags & VALIDATE_FLAG_CHAIN_MODEL) )
1280 : {
1281 0 : if (!strcmp (checktime_arg, "19700101T000000"))
1282 : {
1283 0 : do_list (1, listmode, listfp,
1284 0 : _("WARNING: creation time of signature not known - "
1285 : "assuming current time"));
1286 0 : gnupg_copy_time (check_time, current_time);
1287 : }
1288 : else
1289 0 : gnupg_copy_time (check_time, checktime_arg);
1290 : }
1291 : else
1292 0 : *check_time = 0;
1293 :
1294 0 : if (r_exptime)
1295 0 : *r_exptime = 0;
1296 0 : *exptime = 0;
1297 :
1298 0 : if (opt.no_chain_validation && !listmode)
1299 : {
1300 0 : log_info ("WARNING: bypassing certificate chain validation\n");
1301 0 : return 0;
1302 : }
1303 :
1304 0 : kh = keydb_new (0);
1305 0 : if (!kh)
1306 : {
1307 0 : log_error (_("failed to allocate keyDB handle\n"));
1308 0 : rc = gpg_error (GPG_ERR_GENERAL);
1309 0 : goto leave;
1310 : }
1311 :
1312 0 : if (DBG_X509 && !listmode)
1313 0 : gpgsm_dump_cert ("target", cert);
1314 :
1315 0 : subject_cert = cert;
1316 0 : ksba_cert_ref (subject_cert);
1317 0 : maxdepth = 50;
1318 0 : depth = 0;
1319 :
1320 : for (;;)
1321 : {
1322 : int is_root;
1323 0 : gpg_error_t istrusted_rc = -1;
1324 :
1325 : /* Put the certificate on our list. */
1326 : {
1327 : chain_item_t ci;
1328 :
1329 0 : ci = xtrycalloc (1, sizeof *ci);
1330 0 : if (!ci)
1331 : {
1332 0 : rc = gpg_error_from_syserror ();
1333 0 : goto leave;
1334 : }
1335 0 : ksba_cert_ref (subject_cert);
1336 0 : ci->cert = subject_cert;
1337 0 : ci->next = chain;
1338 0 : chain = ci;
1339 : }
1340 :
1341 0 : xfree (issuer);
1342 0 : xfree (subject);
1343 0 : issuer = ksba_cert_get_issuer (subject_cert, 0);
1344 0 : subject = ksba_cert_get_subject (subject_cert, 0);
1345 :
1346 0 : if (!issuer)
1347 : {
1348 0 : do_list (1, listmode, listfp, _("no issuer found in certificate"));
1349 0 : rc = gpg_error (GPG_ERR_BAD_CERT);
1350 0 : goto leave;
1351 : }
1352 :
1353 :
1354 : /* Is this a self-issued certificate (i.e. the root certificate)? */
1355 0 : is_root = is_root_cert (subject_cert, issuer, subject);
1356 0 : if (is_root)
1357 : {
1358 0 : chain->is_root = 1;
1359 : /* Check early whether the certificate is listed as trusted.
1360 : We used to do this only later but changed it to call the
1361 : check right here so that we can access special flags
1362 : associated with that specific root certificate. */
1363 0 : if (gpgsm_cert_has_well_known_private_key (subject_cert))
1364 : {
1365 0 : memset (rootca_flags, 0, sizeof *rootca_flags);
1366 0 : istrusted_rc = ((flags & VALIDATE_FLAG_STEED)
1367 0 : ? 0 : gpg_error (GPG_ERR_NOT_TRUSTED));
1368 : }
1369 : else
1370 0 : istrusted_rc = gpgsm_agent_istrusted (ctrl, subject_cert, NULL,
1371 : rootca_flags);
1372 0 : audit_log_cert (ctrl->audit, AUDIT_ROOT_TRUSTED,
1373 : subject_cert, istrusted_rc);
1374 : /* If the chain model extended attribute is used, make sure
1375 : that our chain model flag is set. */
1376 0 : if (!(flags & VALIDATE_FLAG_STEED)
1377 0 : && has_validation_model_chain (subject_cert, listmode, listfp))
1378 0 : rootca_flags->chain_model = 1;
1379 : }
1380 :
1381 :
1382 : /* Check the validity period. */
1383 0 : if ( (flags & VALIDATE_FLAG_CHAIN_MODEL) )
1384 0 : rc = check_validity_period_cm (current_time, check_time, subject_cert,
1385 : exptime, listmode, listfp,
1386 0 : (depth && is_root)? -1: depth);
1387 : else
1388 0 : rc = check_validity_period (current_time, subject_cert,
1389 : exptime, listmode, listfp,
1390 0 : (depth && is_root)? -1: depth);
1391 0 : if (gpg_err_code (rc) == GPG_ERR_CERT_EXPIRED)
1392 : {
1393 0 : any_expired = 1;
1394 0 : rc = 0;
1395 : }
1396 0 : else if (rc)
1397 0 : goto leave;
1398 :
1399 :
1400 : /* Assert that we understand all critical extensions. */
1401 0 : rc = unknown_criticals (subject_cert, listmode, listfp);
1402 0 : if (rc)
1403 0 : goto leave;
1404 :
1405 : /* Do a policy check. */
1406 0 : if (!opt.no_policy_check)
1407 : {
1408 0 : rc = check_cert_policy (subject_cert, listmode, listfp);
1409 0 : if (gpg_err_code (rc) == GPG_ERR_NO_POLICY_MATCH)
1410 : {
1411 0 : any_no_policy_match = 1;
1412 0 : rc = 1;
1413 : }
1414 0 : else if (rc)
1415 0 : goto leave;
1416 : }
1417 :
1418 :
1419 : /* If this is the root certificate we are at the end of the chain. */
1420 0 : if (is_root)
1421 : {
1422 0 : if (!istrusted_rc)
1423 : ; /* No need to check the certificate for a trusted one. */
1424 0 : else if (gpgsm_check_cert_sig (subject_cert, subject_cert) )
1425 : {
1426 : /* We only check the signature if the certificate is not
1427 : trusted for better diagnostics. */
1428 0 : do_list (1, listmode, listfp,
1429 0 : _("self-signed certificate has a BAD signature"));
1430 0 : if (DBG_X509)
1431 : {
1432 0 : gpgsm_dump_cert ("self-signing cert", subject_cert);
1433 : }
1434 0 : rc = gpg_error (depth? GPG_ERR_BAD_CERT_CHAIN
1435 : : GPG_ERR_BAD_CERT);
1436 0 : goto leave;
1437 : }
1438 0 : if (!rootca_flags->relax)
1439 : {
1440 0 : rc = allowed_ca (ctrl, subject_cert, NULL, listmode, listfp);
1441 0 : if (rc)
1442 0 : goto leave;
1443 : }
1444 :
1445 :
1446 : /* Set the flag for qualified signatures. This flag is
1447 : deduced from a list of root certificates allowed for
1448 : qualified signatures. */
1449 0 : if (is_qualified == -1 && !(flags & VALIDATE_FLAG_STEED))
1450 : {
1451 : gpg_error_t err;
1452 : size_t buflen;
1453 : char buf[1];
1454 :
1455 0 : if (!ksba_cert_get_user_data (cert, "is_qualified",
1456 : &buf, sizeof (buf),
1457 0 : &buflen) && buflen)
1458 : {
1459 : /* We already checked this for this certificate,
1460 : thus we simply take it from the user data. */
1461 0 : is_qualified = !!*buf;
1462 : }
1463 : else
1464 : {
1465 : /* Need to consult the list of root certificates for
1466 : qualified signatures. */
1467 0 : err = gpgsm_is_in_qualified_list (ctrl, subject_cert, NULL);
1468 0 : if (!err)
1469 0 : is_qualified = 1;
1470 0 : else if ( gpg_err_code (err) == GPG_ERR_NOT_FOUND)
1471 0 : is_qualified = 0;
1472 : else
1473 0 : log_error ("checking the list of qualified "
1474 : "root certificates failed: %s\n",
1475 : gpg_strerror (err));
1476 0 : if ( is_qualified != -1 )
1477 : {
1478 : /* Cache the result but don't care too much
1479 : about an error. */
1480 0 : buf[0] = !!is_qualified;
1481 0 : err = ksba_cert_set_user_data (subject_cert,
1482 : "is_qualified", buf, 1);
1483 0 : if (err)
1484 0 : log_error ("set_user_data(is_qualified) failed: %s\n",
1485 : gpg_strerror (err));
1486 : }
1487 : }
1488 : }
1489 :
1490 :
1491 : /* Act on the check for a trusted root certificates. */
1492 0 : rc = istrusted_rc;
1493 0 : if (!rc)
1494 : ;
1495 0 : else if (gpg_err_code (rc) == GPG_ERR_NOT_TRUSTED)
1496 : {
1497 0 : do_list (0, listmode, listfp,
1498 0 : _("root certificate is not marked trusted"));
1499 : /* If we already figured out that the certificate is
1500 : expired it does not make much sense to ask the user
1501 : whether we wants to trust the root certificate. We
1502 : should do this only if the certificate under question
1503 : will then be usable. If the certificate has a well
1504 : known private key asking the user does not make any
1505 : sense. */
1506 0 : if ( !any_expired
1507 0 : && !gpgsm_cert_has_well_known_private_key (subject_cert)
1508 0 : && (!listmode || !already_asked_marktrusted (subject_cert))
1509 0 : && ask_marktrusted (ctrl, subject_cert, listmode) )
1510 0 : rc = 0;
1511 : }
1512 : else
1513 : {
1514 0 : log_error (_("checking the trust list failed: %s\n"),
1515 : gpg_strerror (rc));
1516 : }
1517 :
1518 0 : if (rc)
1519 0 : goto leave;
1520 :
1521 : /* Check for revocations etc. */
1522 0 : if ((flags & VALIDATE_FLAG_NO_DIRMNGR))
1523 : ;
1524 0 : else if ((flags & VALIDATE_FLAG_STEED))
1525 : ; /* Fixme: check revocations via DNS. */
1526 0 : else if (opt.no_trusted_cert_crl_check || rootca_flags->relax)
1527 : ;
1528 : else
1529 0 : rc = is_cert_still_valid (ctrl,
1530 : (flags & VALIDATE_FLAG_CHAIN_MODEL),
1531 : listmode, listfp,
1532 : subject_cert, subject_cert,
1533 : &any_revoked, &any_no_crl,
1534 : &any_crl_too_old);
1535 0 : if (rc)
1536 0 : goto leave;
1537 :
1538 0 : break; /* Okay: a self-signed certicate is an end-point. */
1539 : } /* End is_root. */
1540 :
1541 :
1542 : /* Take care that the chain does not get too long. */
1543 0 : if ((depth+1) > maxdepth)
1544 : {
1545 0 : do_list (1, listmode, listfp, _("certificate chain too long\n"));
1546 0 : rc = gpg_error (GPG_ERR_BAD_CERT_CHAIN);
1547 0 : goto leave;
1548 : }
1549 :
1550 : /* Find the next cert up the tree. */
1551 0 : keydb_search_reset (kh);
1552 0 : rc = find_up (ctrl, kh, subject_cert, issuer, 0);
1553 0 : if (rc)
1554 : {
1555 0 : if (rc == -1)
1556 : {
1557 0 : do_list (0, listmode, listfp, _("issuer certificate not found"));
1558 0 : if (!listmode)
1559 : {
1560 0 : log_info ("issuer certificate: #/");
1561 0 : gpgsm_dump_string (issuer);
1562 0 : log_printf ("\n");
1563 : }
1564 : }
1565 : else
1566 0 : log_error ("failed to find issuer's certificate: rc=%d\n", rc);
1567 0 : rc = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
1568 0 : goto leave;
1569 : }
1570 :
1571 0 : ksba_cert_release (issuer_cert); issuer_cert = NULL;
1572 0 : rc = keydb_get_cert (kh, &issuer_cert);
1573 0 : if (rc)
1574 : {
1575 0 : log_error ("keydb_get_cert() failed: rc=%d\n", rc);
1576 0 : rc = gpg_error (GPG_ERR_GENERAL);
1577 0 : goto leave;
1578 : }
1579 :
1580 : try_another_cert:
1581 0 : if (DBG_X509)
1582 : {
1583 0 : log_debug ("got issuer's certificate:\n");
1584 0 : gpgsm_dump_cert ("issuer", issuer_cert);
1585 : }
1586 :
1587 0 : rc = gpgsm_check_cert_sig (issuer_cert, subject_cert);
1588 0 : if (rc)
1589 : {
1590 0 : do_list (0, listmode, listfp, _("certificate has a BAD signature"));
1591 0 : if (DBG_X509)
1592 : {
1593 0 : gpgsm_dump_cert ("signing issuer", issuer_cert);
1594 0 : gpgsm_dump_cert ("signed subject", subject_cert);
1595 : }
1596 0 : if (gpg_err_code (rc) == GPG_ERR_BAD_SIGNATURE)
1597 : {
1598 : /* We now try to find other issuer certificates which
1599 : might have been used. This is required because some
1600 : CAs are reusing the issuer and subject DN for new
1601 : root certificates. */
1602 : /* FIXME: Do this only if we don't have an
1603 : AKI.keyIdentifier */
1604 0 : rc = find_up (ctrl, kh, subject_cert, issuer, 1);
1605 0 : if (!rc)
1606 : {
1607 : ksba_cert_t tmp_cert;
1608 :
1609 0 : rc = keydb_get_cert (kh, &tmp_cert);
1610 0 : if (rc || !compare_certs (issuer_cert, tmp_cert))
1611 : {
1612 : /* The find next did not work or returned an
1613 : identical certificate. We better stop here
1614 : to avoid infinite checks. */
1615 0 : rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
1616 0 : ksba_cert_release (tmp_cert);
1617 : }
1618 : else
1619 : {
1620 0 : do_list (0, listmode, listfp,
1621 0 : _("found another possible matching "
1622 : "CA certificate - trying again"));
1623 0 : ksba_cert_release (issuer_cert);
1624 0 : issuer_cert = tmp_cert;
1625 0 : goto try_another_cert;
1626 : }
1627 : }
1628 : }
1629 :
1630 : /* We give a more descriptive error code than the one
1631 : returned from the signature checking. */
1632 0 : rc = gpg_error (GPG_ERR_BAD_CERT_CHAIN);
1633 0 : goto leave;
1634 : }
1635 :
1636 0 : is_root = gpgsm_is_root_cert (issuer_cert);
1637 0 : istrusted_rc = -1;
1638 :
1639 :
1640 : /* Check that a CA is allowed to issue certificates. */
1641 : {
1642 : int chainlen;
1643 :
1644 0 : rc = allowed_ca (ctrl, issuer_cert, &chainlen, listmode, listfp);
1645 0 : if (rc)
1646 : {
1647 : /* Not allowed. Check whether this is a trusted root
1648 : certificate and whether we allow special exceptions.
1649 : We could carry the result of the test over to the
1650 : regular root check at the top of the loop but for
1651 : clarity we won't do that. Given that the majority of
1652 : certificates carry proper BasicContraints our way of
1653 : overriding an error in the way is justified for
1654 : performance reasons. */
1655 0 : if (is_root)
1656 : {
1657 0 : if (gpgsm_cert_has_well_known_private_key (issuer_cert))
1658 : {
1659 0 : memset (rootca_flags, 0, sizeof *rootca_flags);
1660 0 : istrusted_rc = ((flags & VALIDATE_FLAG_STEED)
1661 0 : ? 0 : gpg_error (GPG_ERR_NOT_TRUSTED));
1662 : }
1663 : else
1664 0 : istrusted_rc = gpgsm_agent_istrusted
1665 : (ctrl, issuer_cert, NULL, rootca_flags);
1666 :
1667 0 : if (!istrusted_rc && rootca_flags->relax)
1668 : {
1669 : /* Ignore the error due to the relax flag. */
1670 0 : rc = 0;
1671 0 : chainlen = -1;
1672 : }
1673 : }
1674 : }
1675 0 : if (rc)
1676 0 : goto leave;
1677 0 : if (chainlen >= 0 && depth > chainlen)
1678 : {
1679 0 : do_list (1, listmode, listfp,
1680 0 : _("certificate chain longer than allowed by CA (%d)"),
1681 : chainlen);
1682 0 : rc = gpg_error (GPG_ERR_BAD_CERT_CHAIN);
1683 0 : goto leave;
1684 : }
1685 : }
1686 :
1687 : /* Is the certificate allowed to sign other certificates. */
1688 0 : if (!listmode)
1689 : {
1690 0 : rc = gpgsm_cert_use_cert_p (issuer_cert);
1691 0 : if (rc)
1692 : {
1693 : char numbuf[50];
1694 0 : sprintf (numbuf, "%d", rc);
1695 0 : gpgsm_status2 (ctrl, STATUS_ERROR, "certcert.issuer.keyusage",
1696 : numbuf, NULL);
1697 0 : goto leave;
1698 : }
1699 : }
1700 :
1701 : /* Check for revocations etc. Note that for a root certificate
1702 : this test is done a second time later. This should eventually
1703 : be fixed. */
1704 0 : if ((flags & VALIDATE_FLAG_NO_DIRMNGR))
1705 0 : rc = 0;
1706 0 : else if ((flags & VALIDATE_FLAG_STEED))
1707 0 : rc = 0; /* Fixme: XXX */
1708 0 : else if (is_root && (opt.no_trusted_cert_crl_check
1709 0 : || (!istrusted_rc && rootca_flags->relax)))
1710 0 : rc = 0;
1711 : else
1712 0 : rc = is_cert_still_valid (ctrl,
1713 : (flags & VALIDATE_FLAG_CHAIN_MODEL),
1714 : listmode, listfp,
1715 : subject_cert, issuer_cert,
1716 : &any_revoked, &any_no_crl, &any_crl_too_old);
1717 0 : if (rc)
1718 0 : goto leave;
1719 :
1720 :
1721 0 : if (opt.verbose && !listmode)
1722 0 : log_info (depth == 0 ? _("certificate is good\n") :
1723 : !is_root ? _("intermediate certificate is good\n") :
1724 : /* other */ _("root certificate is good\n"));
1725 :
1726 : /* Under the chain model the next check time is the creation
1727 : time of the subject certificate. */
1728 0 : if ( (flags & VALIDATE_FLAG_CHAIN_MODEL) )
1729 : {
1730 0 : rc = ksba_cert_get_validity (subject_cert, 0, check_time);
1731 0 : if (rc)
1732 : {
1733 : /* That will never happen as we have already checked
1734 : this above. */
1735 0 : BUG ();
1736 : }
1737 : }
1738 :
1739 : /* For the next round the current issuer becomes the new subject. */
1740 0 : keydb_search_reset (kh);
1741 0 : ksba_cert_release (subject_cert);
1742 0 : subject_cert = issuer_cert;
1743 0 : issuer_cert = NULL;
1744 0 : depth++;
1745 0 : } /* End chain traversal. */
1746 :
1747 0 : if (!listmode && !opt.quiet)
1748 : {
1749 0 : if (opt.no_policy_check)
1750 0 : log_info ("policies not checked due to %s option\n",
1751 : "--disable-policy-checks");
1752 0 : if (ctrl->offline || (opt.no_crl_check && !ctrl->use_ocsp))
1753 0 : log_info ("CRLs not checked due to %s option\n",
1754 0 : ctrl->offline ? "offline" : "--disable-crl-checks");
1755 : }
1756 :
1757 0 : if (!rc)
1758 : { /* If we encountered an error somewhere during the checks, set
1759 : the error code to the most critical one */
1760 0 : if (any_revoked)
1761 0 : rc = gpg_error (GPG_ERR_CERT_REVOKED);
1762 0 : else if (any_expired)
1763 0 : rc = gpg_error (GPG_ERR_CERT_EXPIRED);
1764 0 : else if (any_no_crl)
1765 0 : rc = gpg_error (GPG_ERR_NO_CRL_KNOWN);
1766 0 : else if (any_crl_too_old)
1767 0 : rc = gpg_error (GPG_ERR_CRL_TOO_OLD);
1768 0 : else if (any_no_policy_match)
1769 0 : rc = gpg_error (GPG_ERR_NO_POLICY_MATCH);
1770 : }
1771 :
1772 : leave:
1773 : /* If we have traversed a complete chain up to the root we will
1774 : reset the ephemeral flag for all these certificates. This is done
1775 : regardless of any error because those errors may only be
1776 : transient. */
1777 0 : if (chain && chain->is_root)
1778 : {
1779 : gpg_error_t err;
1780 : chain_item_t ci;
1781 :
1782 0 : for (ci = chain; ci; ci = ci->next)
1783 : {
1784 : /* Note that it is possible for the last certificate in the
1785 : chain (i.e. our target certificate) that it has not yet
1786 : been stored in the keybox and thus the flag can't be set.
1787 : We ignore this error becuase it will later be stored
1788 : anyway. */
1789 0 : err = keydb_set_cert_flags (ci->cert, 1, KEYBOX_FLAG_BLOB, 0,
1790 : KEYBOX_FLAG_BLOB_EPHEMERAL, 0);
1791 0 : if (!ci->next && gpg_err_code (err) == GPG_ERR_NOT_FOUND)
1792 : ;
1793 0 : else if (err)
1794 0 : log_error ("clearing ephemeral flag failed: %s\n",
1795 : gpg_strerror (err));
1796 : }
1797 : }
1798 :
1799 : /* If we have figured something about the qualified signature
1800 : capability of the certificate under question, store the result as
1801 : user data in all certificates of the chain. We do this even if the
1802 : validation itself failed. */
1803 0 : if (is_qualified != -1 && !(flags & VALIDATE_FLAG_STEED))
1804 : {
1805 : gpg_error_t err;
1806 : chain_item_t ci;
1807 : char buf[1];
1808 :
1809 0 : buf[0] = !!is_qualified;
1810 :
1811 0 : for (ci = chain; ci; ci = ci->next)
1812 : {
1813 0 : err = ksba_cert_set_user_data (ci->cert, "is_qualified", buf, 1);
1814 0 : if (err)
1815 : {
1816 0 : log_error ("set_user_data(is_qualified) failed: %s\n",
1817 : gpg_strerror (err));
1818 0 : if (!rc)
1819 0 : rc = err;
1820 : }
1821 : }
1822 : }
1823 :
1824 : /* If auditing has been enabled, record what is in the chain. */
1825 0 : if (ctrl->audit)
1826 : {
1827 : chain_item_t ci;
1828 :
1829 0 : audit_log (ctrl->audit, AUDIT_CHAIN_BEGIN);
1830 0 : for (ci = chain; ci; ci = ci->next)
1831 : {
1832 0 : audit_log_cert (ctrl->audit,
1833 0 : ci->is_root? AUDIT_CHAIN_ROOTCERT : AUDIT_CHAIN_CERT,
1834 : ci->cert, 0);
1835 : }
1836 0 : audit_log (ctrl->audit, AUDIT_CHAIN_END);
1837 : }
1838 :
1839 0 : if (r_exptime)
1840 0 : gnupg_copy_time (r_exptime, exptime);
1841 0 : xfree (issuer);
1842 0 : xfree (subject);
1843 0 : keydb_release (kh);
1844 0 : while (chain)
1845 : {
1846 0 : chain_item_t ci_next = chain->next;
1847 0 : ksba_cert_release (chain->cert);
1848 0 : xfree (chain);
1849 0 : chain = ci_next;
1850 : }
1851 0 : ksba_cert_release (issuer_cert);
1852 0 : ksba_cert_release (subject_cert);
1853 0 : return rc;
1854 : }
1855 :
1856 :
1857 : /* Validate a certificate chain. For a description see
1858 : do_validate_chain. This function is a wrapper to handle a root
1859 : certificate with the chain_model flag set. If RETFLAGS is not
1860 : NULL, flags indicating now the verification was done are stored
1861 : there. The only defined vits for RETFLAGS are
1862 : VALIDATE_FLAG_CHAIN_MODEL and VALIDATE_FLAG_STEED.
1863 :
1864 : If you are verifying a signature you should set CHECKTIME to the
1865 : creation time of the signature. If your are verifying a
1866 : certificate, set it nil (i.e. the empty string). If the creation
1867 : date of the signature is not known use the special date
1868 : "19700101T000000" which is treated in a special way here. */
1869 : int
1870 0 : gpgsm_validate_chain (ctrl_t ctrl, ksba_cert_t cert, ksba_isotime_t checktime,
1871 : ksba_isotime_t r_exptime,
1872 : int listmode, estream_t listfp, unsigned int flags,
1873 : unsigned int *retflags)
1874 : {
1875 : int rc;
1876 : struct rootca_flags_s rootca_flags;
1877 : unsigned int dummy_retflags;
1878 :
1879 0 : if (!retflags)
1880 0 : retflags = &dummy_retflags;
1881 :
1882 : /* If the session requested a certain validation mode make sure the
1883 : corresponding flags are set. */
1884 0 : if (ctrl->validation_model == 1)
1885 0 : flags |= VALIDATE_FLAG_CHAIN_MODEL;
1886 0 : else if (ctrl->validation_model == 2)
1887 0 : flags |= VALIDATE_FLAG_STEED;
1888 :
1889 : /* If the chain model was forced, set this immediately into
1890 : RETFLAGS. */
1891 0 : *retflags = (flags & VALIDATE_FLAG_CHAIN_MODEL);
1892 :
1893 0 : memset (&rootca_flags, 0, sizeof rootca_flags);
1894 :
1895 0 : rc = do_validate_chain (ctrl, cert, checktime,
1896 : r_exptime, listmode, listfp, flags,
1897 : &rootca_flags);
1898 0 : if (!rc && (flags & VALIDATE_FLAG_STEED))
1899 : {
1900 0 : *retflags |= VALIDATE_FLAG_STEED;
1901 : }
1902 0 : else if (gpg_err_code (rc) == GPG_ERR_CERT_EXPIRED
1903 0 : && !(flags & VALIDATE_FLAG_CHAIN_MODEL)
1904 0 : && (rootca_flags.valid && rootca_flags.chain_model))
1905 : {
1906 0 : do_list (0, listmode, listfp, _("switching to chain model"));
1907 0 : rc = do_validate_chain (ctrl, cert, checktime,
1908 : r_exptime, listmode, listfp,
1909 : (flags |= VALIDATE_FLAG_CHAIN_MODEL),
1910 : &rootca_flags);
1911 0 : *retflags |= VALIDATE_FLAG_CHAIN_MODEL;
1912 : }
1913 :
1914 0 : if (opt.verbose)
1915 0 : do_list (0, listmode, listfp, _("validation model used: %s"),
1916 0 : (*retflags & VALIDATE_FLAG_STEED)?
1917 : "steed" :
1918 0 : (*retflags & VALIDATE_FLAG_CHAIN_MODEL)?
1919 0 : _("chain"):_("shell"));
1920 :
1921 0 : return rc;
1922 : }
1923 :
1924 :
1925 : /* Check that the given certificate is valid but DO NOT check any
1926 : constraints. We assume that the issuers certificate is already in
1927 : the DB and that this one is valid; which it should be because it
1928 : has been checked using this function. */
1929 : int
1930 3 : gpgsm_basic_cert_check (ctrl_t ctrl, ksba_cert_t cert)
1931 : {
1932 3 : int rc = 0;
1933 3 : char *issuer = NULL;
1934 3 : char *subject = NULL;
1935 : KEYDB_HANDLE kh;
1936 3 : ksba_cert_t issuer_cert = NULL;
1937 :
1938 3 : if (opt.no_chain_validation)
1939 : {
1940 0 : log_info ("WARNING: bypassing basic certificate checks\n");
1941 0 : return 0;
1942 : }
1943 :
1944 3 : kh = keydb_new (0);
1945 3 : if (!kh)
1946 : {
1947 0 : log_error (_("failed to allocate keyDB handle\n"));
1948 0 : rc = gpg_error (GPG_ERR_GENERAL);
1949 0 : goto leave;
1950 : }
1951 :
1952 3 : issuer = ksba_cert_get_issuer (cert, 0);
1953 3 : subject = ksba_cert_get_subject (cert, 0);
1954 3 : if (!issuer)
1955 : {
1956 0 : log_error ("no issuer found in certificate\n");
1957 0 : rc = gpg_error (GPG_ERR_BAD_CERT);
1958 0 : goto leave;
1959 : }
1960 :
1961 3 : if (is_root_cert (cert, issuer, subject))
1962 : {
1963 3 : rc = gpgsm_check_cert_sig (cert, cert);
1964 3 : if (rc)
1965 : {
1966 0 : log_error ("self-signed certificate has a BAD signature: %s\n",
1967 : gpg_strerror (rc));
1968 0 : if (DBG_X509)
1969 : {
1970 0 : gpgsm_dump_cert ("self-signing cert", cert);
1971 : }
1972 0 : rc = gpg_error (GPG_ERR_BAD_CERT);
1973 0 : goto leave;
1974 : }
1975 : }
1976 : else
1977 : {
1978 : /* Find the next cert up the tree. */
1979 0 : keydb_search_reset (kh);
1980 0 : rc = find_up (ctrl, kh, cert, issuer, 0);
1981 0 : if (rc)
1982 : {
1983 0 : if (rc == -1)
1984 : {
1985 0 : log_info ("issuer certificate (#/");
1986 0 : gpgsm_dump_string (issuer);
1987 0 : log_printf (") not found\n");
1988 : }
1989 : else
1990 0 : log_error ("failed to find issuer's certificate: rc=%d\n", rc);
1991 0 : rc = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
1992 0 : goto leave;
1993 : }
1994 :
1995 0 : ksba_cert_release (issuer_cert); issuer_cert = NULL;
1996 0 : rc = keydb_get_cert (kh, &issuer_cert);
1997 0 : if (rc)
1998 : {
1999 0 : log_error ("keydb_get_cert() failed: rc=%d\n", rc);
2000 0 : rc = gpg_error (GPG_ERR_GENERAL);
2001 0 : goto leave;
2002 : }
2003 :
2004 0 : rc = gpgsm_check_cert_sig (issuer_cert, cert);
2005 0 : if (rc)
2006 : {
2007 0 : log_error ("certificate has a BAD signature: %s\n",
2008 : gpg_strerror (rc));
2009 0 : if (DBG_X509)
2010 : {
2011 0 : gpgsm_dump_cert ("signing issuer", issuer_cert);
2012 0 : gpgsm_dump_cert ("signed subject", cert);
2013 : }
2014 0 : rc = gpg_error (GPG_ERR_BAD_CERT);
2015 0 : goto leave;
2016 : }
2017 0 : if (opt.verbose)
2018 0 : log_info (_("certificate is good\n"));
2019 : }
2020 :
2021 : leave:
2022 3 : xfree (issuer);
2023 3 : xfree (subject);
2024 3 : keydb_release (kh);
2025 3 : ksba_cert_release (issuer_cert);
2026 3 : return rc;
2027 : }
2028 :
2029 :
2030 :
2031 : /* Check whether the certificate CERT has been issued by the German
2032 : authority for qualified signature. They do not set the
2033 : basicConstraints and thus we need this workaround. It works by
2034 : looking up the root certificate and checking whether that one is
2035 : listed as a qualified certificate for Germany.
2036 :
2037 : We also try to cache this data but as long as don't keep a
2038 : reference to the certificate this won't be used.
2039 :
2040 : Returns: True if CERT is a RegTP issued CA cert (i.e. the root
2041 : certificate itself or one of the CAs). In that case CHAINLEN will
2042 : receive the length of the chain which is either 0 or 1.
2043 : */
2044 : static int
2045 0 : get_regtp_ca_info (ctrl_t ctrl, ksba_cert_t cert, int *chainlen)
2046 : {
2047 : gpg_error_t err;
2048 : ksba_cert_t next;
2049 0 : int rc = 0;
2050 : int i, depth;
2051 : char country[3];
2052 : ksba_cert_t array[4];
2053 : char buf[2];
2054 : size_t buflen;
2055 : int dummy_chainlen;
2056 :
2057 0 : if (!chainlen)
2058 0 : chainlen = &dummy_chainlen;
2059 :
2060 0 : *chainlen = 0;
2061 0 : err = ksba_cert_get_user_data (cert, "regtp_ca_chainlen",
2062 : &buf, sizeof (buf), &buflen);
2063 0 : if (!err)
2064 : {
2065 : /* Got info. */
2066 0 : if (buflen < 2 || !*buf)
2067 0 : return 0; /* Nothing found. */
2068 0 : *chainlen = buf[1];
2069 0 : return 1; /* This is a regtp CA. */
2070 : }
2071 0 : else if (gpg_err_code (err) != GPG_ERR_NOT_FOUND)
2072 : {
2073 0 : log_error ("ksba_cert_get_user_data(%s) failed: %s\n",
2074 : "regtp_ca_chainlen", gpg_strerror (err));
2075 0 : return 0; /* Nothing found. */
2076 : }
2077 :
2078 : /* Need to gather the info. This requires to walk up the chain
2079 : until we have found the root. Because we are only interested in
2080 : German Bundesnetzagentur (former RegTP) derived certificates 3
2081 : levels are enough. (The German signature law demands a 3 tier
2082 : hierachy; thus there is only one CA between the EE and the Root
2083 : CA.) */
2084 0 : memset (&array, 0, sizeof array);
2085 :
2086 0 : depth = 0;
2087 0 : ksba_cert_ref (cert);
2088 0 : array[depth++] = cert;
2089 0 : ksba_cert_ref (cert);
2090 0 : while (depth < DIM(array) && !(rc=gpgsm_walk_cert_chain (ctrl, cert, &next)))
2091 : {
2092 0 : ksba_cert_release (cert);
2093 0 : ksba_cert_ref (next);
2094 0 : array[depth++] = next;
2095 0 : cert = next;
2096 : }
2097 0 : ksba_cert_release (cert);
2098 0 : if (rc != -1 || !depth || depth == DIM(array) )
2099 : {
2100 : /* We did not reached the root. */
2101 : goto leave;
2102 : }
2103 :
2104 : /* If this is a German signature law issued certificate, we store
2105 : additional additional information. */
2106 0 : if (!gpgsm_is_in_qualified_list (NULL, array[depth-1], country)
2107 0 : && !strcmp (country, "de"))
2108 : {
2109 : /* Setting the pathlen for the root CA and the CA flag for the
2110 : next one is all what we need to do. */
2111 0 : err = ksba_cert_set_user_data (array[depth-1], "regtp_ca_chainlen",
2112 : "\x01\x01", 2);
2113 0 : if (!err && depth > 1)
2114 0 : err = ksba_cert_set_user_data (array[depth-2], "regtp_ca_chainlen",
2115 : "\x01\x00", 2);
2116 0 : if (err)
2117 0 : log_error ("ksba_set_user_data(%s) failed: %s\n",
2118 : "regtp_ca_chainlen", gpg_strerror (err));
2119 0 : for (i=0; i < depth; i++)
2120 0 : ksba_cert_release (array[i]);
2121 0 : *chainlen = (depth>1? 0:1);
2122 0 : return 1;
2123 : }
2124 :
2125 : leave:
2126 : /* Nothing special with this certificate. Mark the target
2127 : certificate anyway to avoid duplicate lookups. */
2128 0 : err = ksba_cert_set_user_data (cert, "regtp_ca_chainlen", "", 1);
2129 0 : if (err)
2130 0 : log_error ("ksba_set_user_data(%s) failed: %s\n",
2131 : "regtp_ca_chainlen", gpg_strerror (err));
2132 0 : for (i=0; i < depth; i++)
2133 0 : ksba_cert_release (array[i]);
2134 0 : return 0;
2135 : }
|