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. Note that the line has always a LF and spacep
409 : does not consider a LF a space. Thus strpbrk will always
410 : succeed. */
411 0 : for (allowed=line; spacep (allowed); allowed++)
412 : ;
413 0 : p = strpbrk (allowed, " :\n");
414 0 : if (!*p || p == allowed)
415 : {
416 0 : fclose (fp);
417 0 : xfree (policies);
418 0 : return gpg_error (GPG_ERR_CONFIGURATION);
419 : }
420 0 : *p = 0; /* strip the rest of the line */
421 : /* See whether we find ALLOWED (which is an OID) in POLICIES */
422 0 : for (haystack=policies; (p=strstr (haystack, allowed)); haystack = p+1)
423 : {
424 0 : if ( !(p == policies || p[-1] == '\n') )
425 0 : continue; /* Does not match the begin of a line. */
426 0 : if (p[strlen (allowed)] != ':')
427 0 : continue; /* The length does not match. */
428 : /* Yep - it does match so return okay. */
429 0 : fclose (fp);
430 0 : xfree (policies);
431 0 : return 0;
432 : }
433 0 : }
434 : }
435 :
436 :
437 : /* Helper function for find_up. This resets the key handle and search
438 : for an issuer ISSUER with a subjectKeyIdentifier of KEYID. Returns
439 : 0 on success or -1 when not found. */
440 : static int
441 0 : find_up_search_by_keyid (KEYDB_HANDLE kh,
442 : const char *issuer, ksba_sexp_t keyid)
443 : {
444 : int rc;
445 0 : ksba_cert_t cert = NULL;
446 0 : ksba_sexp_t subj = NULL;
447 0 : int anyfound = 0;
448 : ksba_isotime_t not_before, last_not_before;
449 :
450 0 : keydb_search_reset (kh);
451 0 : while (!(rc = keydb_search_subject (kh, issuer)))
452 : {
453 0 : ksba_cert_release (cert); cert = NULL;
454 0 : rc = keydb_get_cert (kh, &cert);
455 0 : if (rc)
456 : {
457 0 : log_error ("keydb_get_cert() failed: rc=%d\n", rc);
458 0 : rc = -1;
459 0 : break;
460 : }
461 0 : xfree (subj);
462 0 : if (!ksba_cert_get_subj_key_id (cert, NULL, &subj))
463 : {
464 0 : if (!cmp_simple_canon_sexp (keyid, subj))
465 : {
466 : /* Found matching cert. */
467 0 : rc = ksba_cert_get_validity (cert, 0, not_before);
468 0 : if (rc)
469 : {
470 0 : log_error ("keydb_get_validity() failed: rc=%d\n", rc);
471 0 : rc = -1;
472 0 : break;
473 : }
474 :
475 0 : if (!anyfound || strcmp (last_not_before, not_before) < 0)
476 : {
477 : /* This certificate is the first one found or newer
478 : than the previous one. This copes with
479 : re-issuing CA certificates while keeping the same
480 : key information. */
481 0 : anyfound = 1;
482 0 : gnupg_copy_time (last_not_before, not_before);
483 0 : keydb_push_found_state (kh);
484 : }
485 : }
486 : }
487 : }
488 :
489 0 : if (anyfound)
490 : {
491 : /* Take the last saved one. */
492 0 : keydb_pop_found_state (kh);
493 0 : rc = 0; /* Ignore EOF or other error after the first cert. */
494 : }
495 :
496 0 : ksba_cert_release (cert);
497 0 : xfree (subj);
498 0 : return rc? -1:0;
499 : }
500 :
501 :
502 : static void
503 0 : find_up_store_certs_cb (void *cb_value, ksba_cert_t cert)
504 : {
505 0 : if (keydb_store_cert (cert, 1, NULL))
506 0 : log_error ("error storing issuer certificate as ephemeral\n");
507 0 : ++*(int*)cb_value;
508 0 : }
509 :
510 :
511 : /* Helper for find_up(). Locate the certificate for ISSUER using an
512 : external lookup. KH is the keydb context we are currently using.
513 : On success 0 is returned and the certificate may be retrieved from
514 : the keydb using keydb_get_cert(). KEYID is the keyIdentifier from
515 : the AKI or NULL. */
516 : static int
517 0 : find_up_external (ctrl_t ctrl, KEYDB_HANDLE kh,
518 : const char *issuer, ksba_sexp_t keyid)
519 : {
520 : int rc;
521 0 : strlist_t names = NULL;
522 0 : int count = 0;
523 : char *pattern;
524 : const char *s;
525 :
526 0 : if (opt.verbose)
527 0 : log_info (_("looking up issuer at external location\n"));
528 : /* The Dirmngr process is confused about unknown attributes. As a
529 : quick and ugly hack we locate the CN and use the issuer string
530 : starting at this attribite. Fixme: we should have far better
531 : parsing for external lookups in the Dirmngr. */
532 0 : s = strstr (issuer, "CN=");
533 0 : if (!s || s == issuer || s[-1] != ',')
534 0 : s = issuer;
535 0 : pattern = xtrymalloc (strlen (s)+2);
536 0 : if (!pattern)
537 0 : return gpg_error_from_syserror ();
538 0 : strcpy (stpcpy (pattern, "/"), s);
539 0 : add_to_strlist (&names, pattern);
540 0 : xfree (pattern);
541 :
542 0 : rc = gpgsm_dirmngr_lookup (ctrl, names, 0, find_up_store_certs_cb, &count);
543 0 : free_strlist (names);
544 :
545 0 : if (opt.verbose)
546 0 : log_info (_("number of issuers matching: %d\n"), count);
547 0 : if (rc)
548 : {
549 0 : log_error ("external key lookup failed: %s\n", gpg_strerror (rc));
550 0 : rc = -1;
551 : }
552 0 : else if (!count)
553 0 : rc = -1;
554 : else
555 : {
556 : int old;
557 : /* The issuers are currently stored in the ephemeral key DB, so
558 : we temporary switch to ephemeral mode. */
559 0 : old = keydb_set_ephemeral (kh, 1);
560 0 : if (keyid)
561 0 : rc = find_up_search_by_keyid (kh, issuer, keyid);
562 : else
563 : {
564 0 : keydb_search_reset (kh);
565 0 : rc = keydb_search_subject (kh, issuer);
566 : }
567 0 : keydb_set_ephemeral (kh, old);
568 : }
569 0 : return rc;
570 : }
571 :
572 :
573 : /* Helper for find_up(). Ask the dirmngr for the certificate for
574 : ISSUER with optional SERIALNO. KH is the keydb context we are
575 : currently using. With SUBJECT_MODE set, ISSUER is searched as the
576 : subject. On success 0 is returned and the certificate is available
577 : in the ephemeral DB. */
578 : static int
579 0 : find_up_dirmngr (ctrl_t ctrl, KEYDB_HANDLE kh,
580 : ksba_sexp_t serialno, const char *issuer, int subject_mode)
581 : {
582 : int rc;
583 0 : strlist_t names = NULL;
584 0 : int count = 0;
585 : char *pattern;
586 :
587 : (void)kh;
588 :
589 0 : if (opt.verbose)
590 0 : log_info (_("looking up issuer from the Dirmngr cache\n"));
591 0 : if (subject_mode)
592 : {
593 0 : pattern = xtrymalloc (strlen (issuer)+2);
594 0 : if (pattern)
595 0 : strcpy (stpcpy (pattern, "/"), issuer);
596 : }
597 0 : else if (serialno)
598 0 : pattern = gpgsm_format_sn_issuer (serialno, issuer);
599 : else
600 : {
601 0 : pattern = xtrymalloc (strlen (issuer)+3);
602 0 : if (pattern)
603 0 : strcpy (stpcpy (pattern, "#/"), issuer);
604 : }
605 0 : if (!pattern)
606 0 : return gpg_error_from_syserror ();
607 0 : add_to_strlist (&names, pattern);
608 0 : xfree (pattern);
609 :
610 0 : rc = gpgsm_dirmngr_lookup (ctrl, names, 1, find_up_store_certs_cb, &count);
611 0 : free_strlist (names);
612 :
613 0 : if (opt.verbose)
614 0 : log_info (_("number of matching certificates: %d\n"), count);
615 0 : if (rc && !opt.quiet)
616 0 : log_info (_("dirmngr cache-only key lookup failed: %s\n"),
617 : gpg_strerror (rc));
618 0 : return (!rc && count)? 0 : -1;
619 : }
620 :
621 :
622 :
623 : /* Locate issuing certificate for CERT. ISSUER is the name of the
624 : issuer used as a fallback if the other methods don't work. If
625 : FIND_NEXT is true, the function shall return the next possible
626 : issuer. The certificate itself is not directly returned but a
627 : keydb_get_cert on the keyDb context KH will return it. Returns 0
628 : on success, -1 if not found or an error code. */
629 : static int
630 0 : find_up (ctrl_t ctrl, KEYDB_HANDLE kh,
631 : ksba_cert_t cert, const char *issuer, int find_next)
632 : {
633 : ksba_name_t authid;
634 : ksba_sexp_t authidno;
635 : ksba_sexp_t keyid;
636 0 : int rc = -1;
637 :
638 0 : if (DBG_X509)
639 0 : log_debug ("looking for parent certificate\n");
640 0 : if (!ksba_cert_get_auth_key_id (cert, &keyid, &authid, &authidno))
641 : {
642 0 : const char *s = ksba_name_enum (authid, 0);
643 0 : if (s && *authidno)
644 : {
645 0 : rc = keydb_search_issuer_sn (kh, s, authidno);
646 0 : if (rc)
647 0 : keydb_search_reset (kh);
648 :
649 0 : if (!rc && DBG_X509)
650 0 : log_debug (" found via authid and sn+issuer\n");
651 :
652 : /* In case of an error, try to get the certificate from the
653 : dirmngr. That is done by trying to put that certifcate
654 : into the ephemeral DB and let the code below do the
655 : actual retrieve. Thus there is no error checking.
656 : Skipped in find_next mode as usual. */
657 0 : if (rc == -1 && !find_next)
658 0 : find_up_dirmngr (ctrl, kh, authidno, s, 0);
659 :
660 : /* In case of an error try the ephemeral DB. We can't do
661 : that in find_next mode because we can't keep the search
662 : state then. */
663 0 : if (rc == -1 && !find_next)
664 : {
665 0 : int old = keydb_set_ephemeral (kh, 1);
666 0 : if (!old)
667 : {
668 0 : rc = keydb_search_issuer_sn (kh, s, authidno);
669 0 : if (rc)
670 0 : keydb_search_reset (kh);
671 :
672 0 : if (!rc && DBG_X509)
673 0 : log_debug (" found via authid and sn+issuer (ephem)\n");
674 : }
675 0 : keydb_set_ephemeral (kh, old);
676 : }
677 0 : if (rc)
678 0 : rc = -1; /* Need to make sure to have this error code. */
679 : }
680 :
681 0 : if (rc == -1 && keyid && !find_next)
682 : {
683 : /* Not found by AIK.issuer_sn. Lets try the AIK.ki
684 : instead. Loop over all certificates with that issuer as
685 : subject and stop for the one with a matching
686 : subjectKeyIdentifier. */
687 : /* Fixme: Should we also search in the dirmngr? */
688 0 : rc = find_up_search_by_keyid (kh, issuer, keyid);
689 0 : if (!rc && DBG_X509)
690 0 : log_debug (" found via authid and keyid\n");
691 0 : if (rc)
692 : {
693 0 : int old = keydb_set_ephemeral (kh, 1);
694 0 : if (!old)
695 0 : rc = find_up_search_by_keyid (kh, issuer, keyid);
696 0 : if (!rc && DBG_X509)
697 0 : log_debug (" found via authid and keyid (ephem)\n");
698 0 : keydb_set_ephemeral (kh, old);
699 : }
700 0 : if (rc)
701 0 : rc = -1; /* Need to make sure to have this error code. */
702 : }
703 :
704 : /* If we still didn't found it, try to find it via the subject
705 : from the dirmngr-cache. */
706 0 : if (rc == -1 && !find_next)
707 : {
708 0 : if (!find_up_dirmngr (ctrl, kh, NULL, issuer, 1))
709 : {
710 0 : int old = keydb_set_ephemeral (kh, 1);
711 0 : if (keyid)
712 0 : rc = find_up_search_by_keyid (kh, issuer, keyid);
713 : else
714 : {
715 0 : keydb_search_reset (kh);
716 0 : rc = keydb_search_subject (kh, issuer);
717 : }
718 0 : keydb_set_ephemeral (kh, old);
719 : }
720 0 : if (rc)
721 0 : rc = -1; /* Need to make sure to have this error code. */
722 :
723 0 : if (!rc && DBG_X509)
724 0 : log_debug (" found via authid and issuer from dirmngr cache\n");
725 : }
726 :
727 : /* If we still didn't found it, try an external lookup. */
728 0 : if (rc == -1 && opt.auto_issuer_key_retrieve && !find_next)
729 : {
730 0 : rc = find_up_external (ctrl, kh, issuer, keyid);
731 0 : if (!rc && DBG_X509)
732 0 : log_debug (" found via authid and external lookup\n");
733 : }
734 :
735 :
736 : /* Print a note so that the user does not feel too helpless when
737 : an issuer certificate was found and gpgsm prints BAD
738 : signature because it is not the correct one. */
739 0 : if (rc == -1 && opt.quiet)
740 : ;
741 0 : else if (rc == -1)
742 : {
743 0 : log_info ("%sissuer certificate ", find_next?"next ":"");
744 0 : if (keyid)
745 : {
746 0 : log_printf ("{");
747 0 : gpgsm_dump_serial (keyid);
748 0 : log_printf ("} ");
749 : }
750 0 : if (authidno)
751 : {
752 0 : log_printf ("(#");
753 0 : gpgsm_dump_serial (authidno);
754 0 : log_printf ("/");
755 0 : gpgsm_dump_string (s);
756 0 : log_printf (") ");
757 : }
758 0 : log_printf ("not found using authorityKeyIdentifier\n");
759 : }
760 0 : else if (rc)
761 0 : log_error ("failed to find authorityKeyIdentifier: rc=%d\n", rc);
762 0 : xfree (keyid);
763 0 : ksba_name_release (authid);
764 0 : xfree (authidno);
765 : }
766 :
767 0 : if (rc) /* Not found via authorithyKeyIdentifier, try regular issuer name. */
768 0 : rc = keydb_search_subject (kh, issuer);
769 0 : if (rc == -1 && !find_next)
770 : {
771 : int old;
772 :
773 : /* Also try to get it from the Dirmngr cache. The function
774 : merely puts it into the ephemeral database. */
775 0 : find_up_dirmngr (ctrl, kh, NULL, issuer, 0);
776 :
777 : /* Not found, let us see whether we have one in the ephemeral key DB. */
778 0 : old = keydb_set_ephemeral (kh, 1);
779 0 : if (!old)
780 : {
781 0 : keydb_search_reset (kh);
782 0 : rc = keydb_search_subject (kh, issuer);
783 : }
784 0 : keydb_set_ephemeral (kh, old);
785 :
786 0 : if (!rc && DBG_X509)
787 0 : log_debug (" found via issuer\n");
788 : }
789 :
790 : /* Still not found. If enabled, try an external lookup. */
791 0 : if (rc == -1 && opt.auto_issuer_key_retrieve && !find_next)
792 : {
793 0 : rc = find_up_external (ctrl, kh, issuer, NULL);
794 0 : if (!rc && DBG_X509)
795 0 : log_debug (" found via issuer and external lookup\n");
796 : }
797 :
798 0 : return rc;
799 : }
800 :
801 :
802 : /* Return the next certificate up in the chain starting at START.
803 : Returns -1 when there are no more certificates. */
804 : int
805 3 : gpgsm_walk_cert_chain (ctrl_t ctrl, ksba_cert_t start, ksba_cert_t *r_next)
806 : {
807 3 : int rc = 0;
808 3 : char *issuer = NULL;
809 3 : char *subject = NULL;
810 3 : KEYDB_HANDLE kh = keydb_new (0);
811 :
812 3 : *r_next = NULL;
813 3 : if (!kh)
814 : {
815 0 : log_error (_("failed to allocate keyDB handle\n"));
816 0 : rc = gpg_error (GPG_ERR_GENERAL);
817 0 : goto leave;
818 : }
819 :
820 3 : issuer = ksba_cert_get_issuer (start, 0);
821 3 : subject = ksba_cert_get_subject (start, 0);
822 3 : if (!issuer)
823 : {
824 0 : log_error ("no issuer found in certificate\n");
825 0 : rc = gpg_error (GPG_ERR_BAD_CERT);
826 0 : goto leave;
827 : }
828 3 : if (!subject)
829 : {
830 0 : log_error ("no subject found in certificate\n");
831 0 : rc = gpg_error (GPG_ERR_BAD_CERT);
832 0 : goto leave;
833 : }
834 :
835 3 : if (is_root_cert (start, issuer, subject))
836 : {
837 3 : rc = -1; /* we are at the root */
838 3 : goto leave;
839 : }
840 :
841 0 : rc = find_up (ctrl, kh, start, issuer, 0);
842 0 : if (rc)
843 : {
844 : /* It is quite common not to have a certificate, so better don't
845 : print an error here. */
846 0 : if (rc != -1 && opt.verbose > 1)
847 0 : log_error ("failed to find issuer's certificate: rc=%d\n", rc);
848 0 : rc = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
849 0 : goto leave;
850 : }
851 :
852 0 : rc = keydb_get_cert (kh, r_next);
853 0 : if (rc)
854 : {
855 0 : log_error ("keydb_get_cert() failed: rc=%d\n", rc);
856 0 : rc = gpg_error (GPG_ERR_GENERAL);
857 : }
858 :
859 : leave:
860 3 : xfree (issuer);
861 3 : xfree (subject);
862 3 : keydb_release (kh);
863 3 : return rc;
864 : }
865 :
866 :
867 : /* Helper for gpgsm_is_root_cert. This one is used if the subject and
868 : issuer DNs are already known. */
869 : static int
870 6 : is_root_cert (ksba_cert_t cert, const char *issuerdn, const char *subjectdn)
871 : {
872 : gpg_error_t err;
873 6 : int result = 0;
874 : ksba_sexp_t serialno;
875 : ksba_sexp_t ak_keyid;
876 : ksba_name_t ak_name;
877 : ksba_sexp_t ak_sn;
878 : const char *ak_name_str;
879 6 : ksba_sexp_t subj_keyid = NULL;
880 :
881 6 : if (!issuerdn || !subjectdn)
882 0 : return 0; /* No. */
883 :
884 6 : if (strcmp (issuerdn, subjectdn))
885 0 : return 0; /* No. */
886 :
887 6 : err = ksba_cert_get_auth_key_id (cert, &ak_keyid, &ak_name, &ak_sn);
888 6 : if (err)
889 : {
890 0 : if (gpg_err_code (err) == GPG_ERR_NO_DATA)
891 0 : return 1; /* Yes. Without a authorityKeyIdentifier this needs
892 : to be the Root certifcate (our trust anchor). */
893 0 : log_error ("error getting authorityKeyIdentifier: %s\n",
894 : gpg_strerror (err));
895 0 : return 0; /* Well, it is broken anyway. Return No. */
896 : }
897 :
898 6 : serialno = ksba_cert_get_serial (cert);
899 6 : if (!serialno)
900 : {
901 0 : log_error ("error getting serialno: %s\n", gpg_strerror (err));
902 0 : goto leave;
903 : }
904 :
905 : /* Check whether the auth name's matches the issuer name+sn. If
906 : that is the case this is a root certificate. */
907 6 : ak_name_str = ksba_name_enum (ak_name, 0);
908 6 : if (ak_name_str
909 6 : && !strcmp (ak_name_str, issuerdn)
910 6 : && !cmp_simple_canon_sexp (ak_sn, serialno))
911 : {
912 6 : result = 1; /* Right, CERT is self-signed. */
913 6 : goto leave;
914 : }
915 :
916 : /* Similar for the ak_keyid. */
917 0 : if (ak_keyid && !ksba_cert_get_subj_key_id (cert, NULL, &subj_keyid)
918 0 : && !cmp_simple_canon_sexp (ak_keyid, subj_keyid))
919 : {
920 0 : result = 1; /* Right, CERT is self-signed. */
921 0 : goto leave;
922 : }
923 :
924 :
925 : leave:
926 6 : ksba_free (subj_keyid);
927 6 : ksba_free (ak_keyid);
928 6 : ksba_name_release (ak_name);
929 6 : ksba_free (ak_sn);
930 6 : ksba_free (serialno);
931 6 : return result;
932 : }
933 :
934 :
935 :
936 : /* Check whether the CERT is a root certificate. Returns True if this
937 : is the case. */
938 : int
939 0 : gpgsm_is_root_cert (ksba_cert_t cert)
940 : {
941 : char *issuer;
942 : char *subject;
943 : int yes;
944 :
945 0 : issuer = ksba_cert_get_issuer (cert, 0);
946 0 : subject = ksba_cert_get_subject (cert, 0);
947 0 : yes = is_root_cert (cert, issuer, subject);
948 0 : xfree (issuer);
949 0 : xfree (subject);
950 0 : return yes;
951 : }
952 :
953 :
954 : /* This is a helper for gpgsm_validate_chain. */
955 : static gpg_error_t
956 0 : is_cert_still_valid (ctrl_t ctrl, int force_ocsp, int lm, estream_t fp,
957 : ksba_cert_t subject_cert, ksba_cert_t issuer_cert,
958 : int *any_revoked, int *any_no_crl, int *any_crl_too_old)
959 : {
960 : gpg_error_t err;
961 :
962 0 : if (ctrl->offline || (opt.no_crl_check && !ctrl->use_ocsp))
963 : {
964 0 : audit_log_ok (ctrl->audit, AUDIT_CRL_CHECK,
965 : gpg_error (GPG_ERR_NOT_ENABLED));
966 0 : return 0;
967 : }
968 :
969 0 : err = gpgsm_dirmngr_isvalid (ctrl,
970 : subject_cert, issuer_cert,
971 0 : force_ocsp? 2 : !!ctrl->use_ocsp);
972 0 : audit_log_ok (ctrl->audit, AUDIT_CRL_CHECK, err);
973 :
974 0 : if (err)
975 : {
976 0 : if (!lm)
977 0 : gpgsm_cert_log_name (NULL, subject_cert);
978 0 : switch (gpg_err_code (err))
979 : {
980 : case GPG_ERR_CERT_REVOKED:
981 0 : do_list (1, lm, fp, _("certificate has been revoked"));
982 0 : *any_revoked = 1;
983 : /* Store that in the keybox so that key listings are able to
984 : return the revoked flag. We don't care about error,
985 : though. */
986 0 : keydb_set_cert_flags (subject_cert, 1, KEYBOX_FLAG_VALIDITY, 0,
987 : ~0, VALIDITY_REVOKED);
988 0 : break;
989 :
990 : case GPG_ERR_NO_CRL_KNOWN:
991 0 : do_list (1, lm, fp, _("no CRL found for certificate"));
992 0 : *any_no_crl = 1;
993 0 : break;
994 :
995 : case GPG_ERR_NO_DATA:
996 0 : do_list (1, lm, fp, _("the status of the certificate is unknown"));
997 0 : *any_no_crl = 1;
998 0 : break;
999 :
1000 : case GPG_ERR_CRL_TOO_OLD:
1001 0 : do_list (1, lm, fp, _("the available CRL is too old"));
1002 0 : if (!lm)
1003 0 : log_info (_("please make sure that the "
1004 : "\"dirmngr\" is properly installed\n"));
1005 0 : *any_crl_too_old = 1;
1006 0 : break;
1007 :
1008 : default:
1009 0 : do_list (1, lm, fp, _("checking the CRL failed: %s"),
1010 : gpg_strerror (err));
1011 0 : return err;
1012 : }
1013 : }
1014 0 : return 0;
1015 : }
1016 :
1017 :
1018 : /* Helper for gpgsm_validate_chain to check the validity period of
1019 : SUBJECT_CERT. The caller needs to pass EXPTIME which will be
1020 : updated to the nearest expiration time seen. A DEPTH of 0 indicates
1021 : the target certifciate, -1 the final root certificate and other
1022 : values intermediate certificates. */
1023 : static gpg_error_t
1024 0 : check_validity_period (ksba_isotime_t current_time,
1025 : ksba_cert_t subject_cert,
1026 : ksba_isotime_t exptime,
1027 : int listmode, estream_t listfp, int depth)
1028 : {
1029 : gpg_error_t err;
1030 : ksba_isotime_t not_before, not_after;
1031 :
1032 0 : err = ksba_cert_get_validity (subject_cert, 0, not_before);
1033 0 : if (!err)
1034 0 : err = ksba_cert_get_validity (subject_cert, 1, not_after);
1035 0 : if (err)
1036 : {
1037 0 : do_list (1, listmode, listfp,
1038 0 : _("certificate with invalid validity: %s"), gpg_strerror (err));
1039 0 : return gpg_error (GPG_ERR_BAD_CERT);
1040 : }
1041 :
1042 0 : if (*not_after)
1043 : {
1044 0 : if (!*exptime)
1045 0 : gnupg_copy_time (exptime, not_after);
1046 0 : else if (strcmp (not_after, exptime) < 0 )
1047 0 : gnupg_copy_time (exptime, not_after);
1048 : }
1049 :
1050 0 : if (*not_before && strcmp (current_time, not_before) < 0 )
1051 : {
1052 0 : do_list (1, listmode, listfp,
1053 : depth == 0 ? _("certificate not yet valid") :
1054 : depth == -1 ? _("root certificate not yet valid") :
1055 : /* other */ _("intermediate certificate not yet valid"));
1056 0 : if (!listmode)
1057 : {
1058 0 : log_info (" (valid from ");
1059 0 : dump_isotime (not_before);
1060 0 : log_printf (")\n");
1061 : }
1062 0 : return gpg_error (GPG_ERR_CERT_TOO_YOUNG);
1063 : }
1064 :
1065 0 : if (*not_after && strcmp (current_time, not_after) > 0 )
1066 : {
1067 0 : do_list (opt.ignore_expiration?0:1, listmode, listfp,
1068 : depth == 0 ? _("certificate has expired") :
1069 : depth == -1 ? _("root certificate has expired") :
1070 : /* other */ _("intermediate certificate has expired"));
1071 0 : if (!listmode)
1072 : {
1073 0 : log_info (" (expired at ");
1074 0 : dump_isotime (not_after);
1075 0 : log_printf (")\n");
1076 : }
1077 0 : if (opt.ignore_expiration)
1078 0 : log_info ("WARNING: ignoring expiration\n");
1079 : else
1080 0 : return gpg_error (GPG_ERR_CERT_EXPIRED);
1081 : }
1082 :
1083 0 : return 0;
1084 : }
1085 :
1086 : /* This is a variant of check_validity_period used with the chain
1087 : model. The dextra contraint here is that notBefore and notAfter
1088 : must exists and if the additional argument CHECK_TIME is given this
1089 : time is used to check the validity period of SUBJECT_CERT. */
1090 : static gpg_error_t
1091 0 : check_validity_period_cm (ksba_isotime_t current_time,
1092 : ksba_isotime_t check_time,
1093 : ksba_cert_t subject_cert,
1094 : ksba_isotime_t exptime,
1095 : int listmode, estream_t listfp, int depth)
1096 : {
1097 : gpg_error_t err;
1098 : ksba_isotime_t not_before, not_after;
1099 :
1100 0 : err = ksba_cert_get_validity (subject_cert, 0, not_before);
1101 0 : if (!err)
1102 0 : err = ksba_cert_get_validity (subject_cert, 1, not_after);
1103 0 : if (err)
1104 : {
1105 0 : do_list (1, listmode, listfp,
1106 0 : _("certificate with invalid validity: %s"), gpg_strerror (err));
1107 0 : return gpg_error (GPG_ERR_BAD_CERT);
1108 : }
1109 0 : if (!*not_before || !*not_after)
1110 : {
1111 0 : do_list (1, listmode, listfp,
1112 0 : _("required certificate attributes missing: %s%s%s"),
1113 0 : !*not_before? "notBefore":"",
1114 0 : (!*not_before && !*not_after)? ", ":"",
1115 0 : !*not_before? "notAfter":"");
1116 0 : return gpg_error (GPG_ERR_BAD_CERT);
1117 : }
1118 0 : if (strcmp (not_before, not_after) > 0 )
1119 : {
1120 0 : do_list (1, listmode, listfp,
1121 0 : _("certificate with invalid validity"));
1122 0 : log_info (" (valid from ");
1123 0 : dump_isotime (not_before);
1124 0 : log_printf (" expired at ");
1125 0 : dump_isotime (not_after);
1126 0 : log_printf (")\n");
1127 0 : return gpg_error (GPG_ERR_BAD_CERT);
1128 : }
1129 :
1130 0 : if (!*exptime)
1131 0 : gnupg_copy_time (exptime, not_after);
1132 0 : else if (strcmp (not_after, exptime) < 0 )
1133 0 : gnupg_copy_time (exptime, not_after);
1134 :
1135 0 : if (strcmp (current_time, not_before) < 0 )
1136 : {
1137 0 : do_list (1, listmode, listfp,
1138 : depth == 0 ? _("certificate not yet valid") :
1139 : depth == -1 ? _("root certificate not yet valid") :
1140 : /* other */ _("intermediate certificate not yet valid"));
1141 0 : if (!listmode)
1142 : {
1143 0 : log_info (" (valid from ");
1144 0 : dump_isotime (not_before);
1145 0 : log_printf (")\n");
1146 : }
1147 0 : return gpg_error (GPG_ERR_CERT_TOO_YOUNG);
1148 : }
1149 :
1150 0 : if (*check_time
1151 0 : && (strcmp (check_time, not_before) < 0
1152 0 : || strcmp (check_time, not_after) > 0))
1153 : {
1154 : /* Note that we don't need a case for the root certificate
1155 : because its own consitency has already been checked. */
1156 0 : do_list(opt.ignore_expiration?0:1, listmode, listfp,
1157 : depth == 0 ?
1158 : _("signature not created during lifetime of certificate") :
1159 : depth == 1 ?
1160 : _("certificate not created during lifetime of issuer") :
1161 : _("intermediate certificate not created during lifetime "
1162 : "of issuer"));
1163 0 : if (!listmode)
1164 : {
1165 0 : log_info (depth== 0? _(" ( signature created at ") :
1166 : /* */ _(" (certificate created at ") );
1167 0 : dump_isotime (check_time);
1168 0 : log_printf (")\n");
1169 0 : log_info (depth==0? _(" (certificate valid from ") :
1170 : /* */ _(" ( issuer valid from ") );
1171 0 : dump_isotime (not_before);
1172 0 : log_info (" to ");
1173 0 : dump_isotime (not_after);
1174 0 : log_printf (")\n");
1175 : }
1176 0 : if (opt.ignore_expiration)
1177 0 : log_info ("WARNING: ignoring expiration\n");
1178 : else
1179 0 : return gpg_error (GPG_ERR_CERT_EXPIRED);
1180 : }
1181 :
1182 0 : return 0;
1183 : }
1184 :
1185 :
1186 :
1187 : /* Ask the user whether he wants to mark the certificate CERT trusted.
1188 : Returns true if the CERT is the trusted. We also check whether the
1189 : agent is at all enabled to allow marktrusted and don't call it in
1190 : this session again if it is not. */
1191 : static int
1192 0 : ask_marktrusted (ctrl_t ctrl, ksba_cert_t cert, int listmode)
1193 : {
1194 : static int no_more_questions;
1195 : int rc;
1196 : char *fpr;
1197 0 : int success = 0;
1198 :
1199 0 : fpr = gpgsm_get_fingerprint_string (cert, GCRY_MD_SHA1);
1200 0 : log_info (_("fingerprint=%s\n"), fpr? fpr : "?");
1201 0 : xfree (fpr);
1202 :
1203 0 : if (no_more_questions)
1204 0 : rc = gpg_error (GPG_ERR_NOT_SUPPORTED);
1205 : else
1206 0 : rc = gpgsm_agent_marktrusted (ctrl, cert);
1207 0 : if (!rc)
1208 : {
1209 0 : log_info (_("root certificate has now been marked as trusted\n"));
1210 0 : success = 1;
1211 : }
1212 0 : else if (!listmode)
1213 : {
1214 0 : gpgsm_dump_cert ("issuer", cert);
1215 0 : log_info ("after checking the fingerprint, you may want "
1216 : "to add it manually to the list of trusted certificates.\n");
1217 : }
1218 :
1219 0 : if (gpg_err_code (rc) == GPG_ERR_NOT_SUPPORTED)
1220 : {
1221 0 : if (!no_more_questions)
1222 0 : log_info (_("interactive marking as trusted "
1223 : "not enabled in gpg-agent\n"));
1224 0 : no_more_questions = 1;
1225 : }
1226 0 : else if (gpg_err_code (rc) == GPG_ERR_CANCELED)
1227 : {
1228 0 : log_info (_("interactive marking as trusted "
1229 : "disabled for this session\n"));
1230 0 : no_more_questions = 1;
1231 : }
1232 : else
1233 0 : set_already_asked_marktrusted (cert);
1234 :
1235 0 : return success;
1236 : }
1237 :
1238 :
1239 :
1240 :
1241 : /* Validate a chain and optionally return the nearest expiration time
1242 : in R_EXPTIME. With LISTMODE set to 1 a special listmode is
1243 : activated where only information about the certificate is printed
1244 : to LISTFP and no output is send to the usual log stream. If
1245 : CHECKTIME_ARG is set, it is used only in the chain model instead of the
1246 : current time.
1247 :
1248 : Defined flag bits
1249 :
1250 : VALIDATE_FLAG_NO_DIRMNGR - Do not do any dirmngr isvalid checks.
1251 : VALIDATE_FLAG_CHAIN_MODEL - Check according to chain model.
1252 : VALIDATE_FLAG_STEED - Check according to the STEED model.
1253 : */
1254 : static int
1255 0 : do_validate_chain (ctrl_t ctrl, ksba_cert_t cert, ksba_isotime_t checktime_arg,
1256 : ksba_isotime_t r_exptime,
1257 : int listmode, estream_t listfp, unsigned int flags,
1258 : struct rootca_flags_s *rootca_flags)
1259 : {
1260 0 : int rc = 0, depth, maxdepth;
1261 0 : char *issuer = NULL;
1262 0 : char *subject = NULL;
1263 0 : KEYDB_HANDLE kh = NULL;
1264 0 : ksba_cert_t subject_cert = NULL, issuer_cert = NULL;
1265 : ksba_isotime_t current_time;
1266 : ksba_isotime_t check_time;
1267 : ksba_isotime_t exptime;
1268 0 : int any_expired = 0;
1269 0 : int any_revoked = 0;
1270 0 : int any_no_crl = 0;
1271 0 : int any_crl_too_old = 0;
1272 0 : int any_no_policy_match = 0;
1273 0 : int is_qualified = -1; /* Indicates whether the certificate stems
1274 : from a qualified root certificate.
1275 : -1 = unknown, 0 = no, 1 = yes. */
1276 0 : chain_item_t chain = NULL; /* A list of all certificates in the chain. */
1277 :
1278 :
1279 0 : gnupg_get_isotime (current_time);
1280 :
1281 0 : if ( (flags & VALIDATE_FLAG_CHAIN_MODEL) )
1282 : {
1283 0 : if (!strcmp (checktime_arg, "19700101T000000"))
1284 : {
1285 0 : do_list (1, listmode, listfp,
1286 0 : _("WARNING: creation time of signature not known - "
1287 : "assuming current time"));
1288 0 : gnupg_copy_time (check_time, current_time);
1289 : }
1290 : else
1291 0 : gnupg_copy_time (check_time, checktime_arg);
1292 : }
1293 : else
1294 0 : *check_time = 0;
1295 :
1296 0 : if (r_exptime)
1297 0 : *r_exptime = 0;
1298 0 : *exptime = 0;
1299 :
1300 0 : if (opt.no_chain_validation && !listmode)
1301 : {
1302 0 : log_info ("WARNING: bypassing certificate chain validation\n");
1303 0 : return 0;
1304 : }
1305 :
1306 0 : kh = keydb_new (0);
1307 0 : if (!kh)
1308 : {
1309 0 : log_error (_("failed to allocate keyDB handle\n"));
1310 0 : rc = gpg_error (GPG_ERR_GENERAL);
1311 0 : goto leave;
1312 : }
1313 :
1314 0 : if (DBG_X509 && !listmode)
1315 0 : gpgsm_dump_cert ("target", cert);
1316 :
1317 0 : subject_cert = cert;
1318 0 : ksba_cert_ref (subject_cert);
1319 0 : maxdepth = 50;
1320 0 : depth = 0;
1321 :
1322 : for (;;)
1323 : {
1324 : int is_root;
1325 0 : gpg_error_t istrusted_rc = -1;
1326 :
1327 : /* Put the certificate on our list. */
1328 : {
1329 : chain_item_t ci;
1330 :
1331 0 : ci = xtrycalloc (1, sizeof *ci);
1332 0 : if (!ci)
1333 : {
1334 0 : rc = gpg_error_from_syserror ();
1335 0 : goto leave;
1336 : }
1337 0 : ksba_cert_ref (subject_cert);
1338 0 : ci->cert = subject_cert;
1339 0 : ci->next = chain;
1340 0 : chain = ci;
1341 : }
1342 :
1343 0 : xfree (issuer);
1344 0 : xfree (subject);
1345 0 : issuer = ksba_cert_get_issuer (subject_cert, 0);
1346 0 : subject = ksba_cert_get_subject (subject_cert, 0);
1347 :
1348 0 : if (!issuer)
1349 : {
1350 0 : do_list (1, listmode, listfp, _("no issuer found in certificate"));
1351 0 : rc = gpg_error (GPG_ERR_BAD_CERT);
1352 0 : goto leave;
1353 : }
1354 :
1355 :
1356 : /* Is this a self-issued certificate (i.e. the root certificate)? */
1357 0 : is_root = is_root_cert (subject_cert, issuer, subject);
1358 0 : if (is_root)
1359 : {
1360 0 : chain->is_root = 1;
1361 : /* Check early whether the certificate is listed as trusted.
1362 : We used to do this only later but changed it to call the
1363 : check right here so that we can access special flags
1364 : associated with that specific root certificate. */
1365 0 : if (gpgsm_cert_has_well_known_private_key (subject_cert))
1366 : {
1367 0 : memset (rootca_flags, 0, sizeof *rootca_flags);
1368 0 : istrusted_rc = ((flags & VALIDATE_FLAG_STEED)
1369 0 : ? 0 : gpg_error (GPG_ERR_NOT_TRUSTED));
1370 : }
1371 : else
1372 0 : istrusted_rc = gpgsm_agent_istrusted (ctrl, subject_cert, NULL,
1373 : rootca_flags);
1374 0 : audit_log_cert (ctrl->audit, AUDIT_ROOT_TRUSTED,
1375 : subject_cert, istrusted_rc);
1376 : /* If the chain model extended attribute is used, make sure
1377 : that our chain model flag is set. */
1378 0 : if (!(flags & VALIDATE_FLAG_STEED)
1379 0 : && has_validation_model_chain (subject_cert, listmode, listfp))
1380 0 : rootca_flags->chain_model = 1;
1381 : }
1382 :
1383 :
1384 : /* Check the validity period. */
1385 0 : if ( (flags & VALIDATE_FLAG_CHAIN_MODEL) )
1386 0 : rc = check_validity_period_cm (current_time, check_time, subject_cert,
1387 : exptime, listmode, listfp,
1388 0 : (depth && is_root)? -1: depth);
1389 : else
1390 0 : rc = check_validity_period (current_time, subject_cert,
1391 : exptime, listmode, listfp,
1392 0 : (depth && is_root)? -1: depth);
1393 0 : if (gpg_err_code (rc) == GPG_ERR_CERT_EXPIRED)
1394 0 : any_expired = 1;
1395 0 : else if (rc)
1396 0 : goto leave;
1397 :
1398 :
1399 : /* Assert that we understand all critical extensions. */
1400 0 : rc = unknown_criticals (subject_cert, listmode, listfp);
1401 0 : if (rc)
1402 0 : goto leave;
1403 :
1404 : /* Do a policy check. */
1405 0 : if (!opt.no_policy_check)
1406 : {
1407 0 : rc = check_cert_policy (subject_cert, listmode, listfp);
1408 0 : if (gpg_err_code (rc) == GPG_ERR_NO_POLICY_MATCH)
1409 : {
1410 0 : any_no_policy_match = 1;
1411 0 : rc = 1; /* Be on the safe side and set RC. */
1412 : }
1413 0 : else if (rc)
1414 0 : goto leave;
1415 : }
1416 :
1417 :
1418 : /* If this is the root certificate we are at the end of the chain. */
1419 0 : if (is_root)
1420 : {
1421 0 : if (!istrusted_rc)
1422 : ; /* No need to check the certificate for a trusted one. */
1423 0 : else if (gpgsm_check_cert_sig (subject_cert, subject_cert) )
1424 : {
1425 : /* We only check the signature if the certificate is not
1426 : trusted for better diagnostics. */
1427 0 : do_list (1, listmode, listfp,
1428 0 : _("self-signed certificate has a BAD signature"));
1429 0 : if (DBG_X509)
1430 : {
1431 0 : gpgsm_dump_cert ("self-signing cert", subject_cert);
1432 : }
1433 0 : rc = gpg_error (depth? GPG_ERR_BAD_CERT_CHAIN
1434 : : GPG_ERR_BAD_CERT);
1435 0 : goto leave;
1436 : }
1437 0 : if (!rootca_flags->relax)
1438 : {
1439 0 : rc = allowed_ca (ctrl, subject_cert, NULL, listmode, listfp);
1440 0 : if (rc)
1441 0 : goto leave;
1442 : }
1443 :
1444 :
1445 : /* Set the flag for qualified signatures. This flag is
1446 : deduced from a list of root certificates allowed for
1447 : qualified signatures. */
1448 0 : if (is_qualified == -1 && !(flags & VALIDATE_FLAG_STEED))
1449 : {
1450 : gpg_error_t err;
1451 : size_t buflen;
1452 : char buf[1];
1453 :
1454 0 : if (!ksba_cert_get_user_data (cert, "is_qualified",
1455 : &buf, sizeof (buf),
1456 0 : &buflen) && buflen)
1457 : {
1458 : /* We already checked this for this certificate,
1459 : thus we simply take it from the user data. */
1460 0 : is_qualified = !!*buf;
1461 : }
1462 : else
1463 : {
1464 : /* Need to consult the list of root certificates for
1465 : qualified signatures. */
1466 0 : err = gpgsm_is_in_qualified_list (ctrl, subject_cert, NULL);
1467 0 : if (!err)
1468 0 : is_qualified = 1;
1469 0 : else if ( gpg_err_code (err) == GPG_ERR_NOT_FOUND)
1470 0 : is_qualified = 0;
1471 : else
1472 0 : log_error ("checking the list of qualified "
1473 : "root certificates failed: %s\n",
1474 : gpg_strerror (err));
1475 0 : if ( is_qualified != -1 )
1476 : {
1477 : /* Cache the result but don't care too much
1478 : about an error. */
1479 0 : buf[0] = !!is_qualified;
1480 0 : err = ksba_cert_set_user_data (subject_cert,
1481 : "is_qualified", buf, 1);
1482 0 : if (err)
1483 0 : log_error ("set_user_data(is_qualified) failed: %s\n",
1484 : gpg_strerror (err));
1485 : }
1486 : }
1487 : }
1488 :
1489 :
1490 : /* Act on the check for a trusted root certificates. */
1491 0 : rc = istrusted_rc;
1492 0 : if (!rc)
1493 : ;
1494 0 : else if (gpg_err_code (rc) == GPG_ERR_NOT_TRUSTED)
1495 : {
1496 0 : do_list (0, listmode, listfp,
1497 0 : _("root certificate is not marked trusted"));
1498 : /* If we already figured out that the certificate is
1499 : expired it does not make much sense to ask the user
1500 : whether we wants to trust the root certificate. We
1501 : should do this only if the certificate under question
1502 : will then be usable. If the certificate has a well
1503 : known private key asking the user does not make any
1504 : sense. */
1505 0 : if ( !any_expired
1506 0 : && !gpgsm_cert_has_well_known_private_key (subject_cert)
1507 0 : && (!listmode || !already_asked_marktrusted (subject_cert))
1508 0 : && ask_marktrusted (ctrl, subject_cert, listmode) )
1509 0 : rc = 0;
1510 : }
1511 : else
1512 : {
1513 0 : log_error (_("checking the trust list failed: %s\n"),
1514 : gpg_strerror (rc));
1515 : }
1516 :
1517 0 : if (rc)
1518 0 : goto leave;
1519 :
1520 : /* Check for revocations etc. */
1521 0 : if ((flags & VALIDATE_FLAG_NO_DIRMNGR))
1522 : ;
1523 0 : else if ((flags & VALIDATE_FLAG_STEED))
1524 : ; /* Fixme: check revocations via DNS. */
1525 0 : else if (opt.no_trusted_cert_crl_check || rootca_flags->relax)
1526 : ;
1527 : else
1528 0 : rc = is_cert_still_valid (ctrl,
1529 : (flags & VALIDATE_FLAG_CHAIN_MODEL),
1530 : listmode, listfp,
1531 : subject_cert, subject_cert,
1532 : &any_revoked, &any_no_crl,
1533 : &any_crl_too_old);
1534 0 : if (rc)
1535 0 : goto leave;
1536 :
1537 0 : break; /* Okay: a self-signed certicate is an end-point. */
1538 : } /* End is_root. */
1539 :
1540 :
1541 : /* Take care that the chain does not get too long. */
1542 0 : if ((depth+1) > maxdepth)
1543 : {
1544 0 : do_list (1, listmode, listfp, _("certificate chain too long\n"));
1545 0 : rc = gpg_error (GPG_ERR_BAD_CERT_CHAIN);
1546 0 : goto leave;
1547 : }
1548 :
1549 : /* Find the next cert up the tree. */
1550 0 : keydb_search_reset (kh);
1551 0 : rc = find_up (ctrl, kh, subject_cert, issuer, 0);
1552 0 : if (rc)
1553 : {
1554 0 : if (rc == -1)
1555 : {
1556 0 : do_list (0, listmode, listfp, _("issuer certificate not found"));
1557 0 : if (!listmode)
1558 : {
1559 0 : log_info ("issuer certificate: #/");
1560 0 : gpgsm_dump_string (issuer);
1561 0 : log_printf ("\n");
1562 : }
1563 : }
1564 : else
1565 0 : log_error ("failed to find issuer's certificate: rc=%d\n", rc);
1566 0 : rc = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
1567 0 : goto leave;
1568 : }
1569 :
1570 0 : ksba_cert_release (issuer_cert); issuer_cert = NULL;
1571 0 : rc = keydb_get_cert (kh, &issuer_cert);
1572 0 : if (rc)
1573 : {
1574 0 : log_error ("keydb_get_cert() failed: rc=%d\n", rc);
1575 0 : rc = gpg_error (GPG_ERR_GENERAL);
1576 0 : goto leave;
1577 : }
1578 :
1579 : try_another_cert:
1580 0 : if (DBG_X509)
1581 : {
1582 0 : log_debug ("got issuer's certificate:\n");
1583 0 : gpgsm_dump_cert ("issuer", issuer_cert);
1584 : }
1585 :
1586 0 : rc = gpgsm_check_cert_sig (issuer_cert, subject_cert);
1587 0 : if (rc)
1588 : {
1589 0 : do_list (0, listmode, listfp, _("certificate has a BAD signature"));
1590 0 : if (DBG_X509)
1591 : {
1592 0 : gpgsm_dump_cert ("signing issuer", issuer_cert);
1593 0 : gpgsm_dump_cert ("signed subject", subject_cert);
1594 : }
1595 0 : if (gpg_err_code (rc) == GPG_ERR_BAD_SIGNATURE)
1596 : {
1597 : /* We now try to find other issuer certificates which
1598 : might have been used. This is required because some
1599 : CAs are reusing the issuer and subject DN for new
1600 : root certificates. */
1601 : /* FIXME: Do this only if we don't have an
1602 : AKI.keyIdentifier */
1603 0 : rc = find_up (ctrl, kh, subject_cert, issuer, 1);
1604 0 : if (!rc)
1605 : {
1606 : ksba_cert_t tmp_cert;
1607 :
1608 0 : rc = keydb_get_cert (kh, &tmp_cert);
1609 0 : if (rc || !compare_certs (issuer_cert, tmp_cert))
1610 : {
1611 : /* The find next did not work or returned an
1612 : identical certificate. We better stop here
1613 : to avoid infinite checks. */
1614 : /* No need to set RC because it is not used:
1615 : 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 because 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 : hierarchy; 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 : }
|