1 /* certchain.c - certificate chain validation
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 * 2006, 2007, 2008 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
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.
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.
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/>.
31 #define JNLIB_NEED_LOG_LOGV /* We need log_logv. */
38 #include "../kbx/keybox.h" /* for KEYBOX_FLAG_* */
43 /* Object to keep track of certain root certificates. */
44 struct marktrusted_info_s
46 struct marktrusted_info_s
*next
;
47 unsigned char fpr
[20];
49 static struct marktrusted_info_s
*marktrusted_info
;
52 /* While running the validation function we want to keep track of the
53 certificates in the chain. This type is used for that. */
56 struct chain_item_s
*next
;
57 ksba_cert_t cert
; /* The certificate. */
58 int is_root
; /* The certificate is the root certificate. */
60 typedef struct chain_item_s
*chain_item_t
;
63 static int is_root_cert (ksba_cert_t cert
,
64 const char *issuerdn
, const char *subjectdn
);
65 static int get_regtp_ca_info (ctrl_t ctrl
, ksba_cert_t cert
, int *chainlen
);
68 /* This function returns true if we already asked during this session
69 whether the root certificate CERT shall be marked as trusted. */
71 already_asked_marktrusted (ksba_cert_t cert
)
73 unsigned char fpr
[20];
74 struct marktrusted_info_s
*r
;
76 gpgsm_get_fingerprint (cert
, GCRY_MD_SHA1
, fpr
, NULL
);
77 /* No context switches in the loop! */
78 for (r
=marktrusted_info
; r
; r
= r
->next
)
79 if (!memcmp (r
->fpr
, fpr
, 20))
84 /* Flag certificate CERT as already asked whether it shall be marked
87 set_already_asked_marktrusted (ksba_cert_t cert
)
89 unsigned char fpr
[20];
90 struct marktrusted_info_s
*r
;
92 gpgsm_get_fingerprint (cert
, GCRY_MD_SHA1
, fpr
, NULL
);
93 for (r
=marktrusted_info
; r
; r
= r
->next
)
94 if (!memcmp (r
->fpr
, fpr
, 20))
95 return; /* Already marked. */
96 r
= xtrycalloc (1, sizeof *r
);
99 memcpy (r
->fpr
, fpr
, 20);
100 r
->next
= marktrusted_info
;
101 marktrusted_info
= r
;
104 /* If LISTMODE is true, print FORMAT using LISTMODE to FP. If
105 LISTMODE is false, use the string to print an log_info or, if
106 IS_ERROR is true, and log_error. */
108 do_list (int is_error
, int listmode
, estream_t fp
, const char *format
, ...)
112 va_start (arg_ptr
, format
) ;
118 es_vfprintf (fp
, format
, arg_ptr
);
119 es_fputs ("]\n", fp
);
124 log_logv (is_error
? JNLIB_LOG_ERROR
: JNLIB_LOG_INFO
, format
, arg_ptr
);
130 /* Return 0 if A and B are equal. */
132 compare_certs (ksba_cert_t a
, ksba_cert_t b
)
134 const unsigned char *img_a
, *img_b
;
137 img_a
= ksba_cert_get_image (a
, &len_a
);
140 img_b
= ksba_cert_get_image (b
, &len_b
);
143 return !(len_a
== len_b
&& !memcmp (img_a
, img_b
, len_a
));
147 /* Return true if CERT has the validityModel extensions and defines
148 the use of the chain model. */
150 has_validation_model_chain (ksba_cert_t cert
, int listmode
, estream_t listfp
)
155 size_t off
, derlen
, objlen
, hdrlen
;
156 const unsigned char *der
;
157 int class, tag
, constructed
, ndef
;
160 for (idx
=0; !(err
=ksba_cert_get_extension (cert
, idx
,
161 &oid
, NULL
, &off
, &derlen
));idx
++)
162 if (!strcmp (oid
, "1.3.6.1.4.1.8301.3.5") )
165 return 0; /* Not found. */
166 der
= ksba_cert_get_image (cert
, NULL
);
169 err
= gpg_error (GPG_ERR_INV_OBJ
); /* Oops */
174 err
= parse_ber_header (&der
, &derlen
, &class, &tag
, &constructed
,
175 &ndef
, &objlen
, &hdrlen
);
176 if (!err
&& (objlen
> derlen
|| tag
!= TAG_SEQUENCE
))
177 err
= gpg_error (GPG_ERR_INV_OBJ
);
181 err
= parse_ber_header (&der
, &derlen
, &class, &tag
, &constructed
,
182 &ndef
, &objlen
, &hdrlen
);
183 if (!err
&& (objlen
> derlen
|| tag
!= TAG_OBJECT_ID
))
184 err
= gpg_error (GPG_ERR_INV_OBJ
);
187 oidbuf
= ksba_oid_to_str (der
, objlen
);
190 err
= gpg_error_from_syserror ();
195 do_list (0, listmode
, listfp
,
196 _("validation model requested by certificate: %s"),
197 !strcmp (oidbuf
, "1.3.6.1.4.1.8301.3.5.1")? _("chain") :
198 !strcmp (oidbuf
, "1.3.6.1.4.1.8301.3.5.2")? _("shell") :
200 yes
= !strcmp (oidbuf
, "1.3.6.1.4.1.8301.3.5.1");
206 log_error ("error parsing validityModel: %s\n", gpg_strerror (err
));
213 unknown_criticals (ksba_cert_t cert
, int listmode
, estream_t fp
)
215 static const char *known
[] = {
216 "2.5.29.15", /* keyUsage */
217 "2.5.29.17", /* subjectAltName
218 Japanese DoCoMo certs mark them as critical. PKIX
219 only requires them as critical if subjectName is
220 empty. I don't know whether our code gracefully
221 handles such empry subjectNames but that is
223 "2.5.29.19", /* basic Constraints */
224 "2.5.29.32", /* certificatePolicies */
225 "2.5.29.37", /* extendedKeyUsage - handled by certlist.c */
226 "1.3.6.1.4.1.8301.3.5", /* validityModel - handled here. */
229 int rc
= 0, i
, idx
, crit
;
233 for (idx
=0; !(err
=ksba_cert_get_extension (cert
, idx
,
234 &oid
, &crit
, NULL
, NULL
));idx
++)
238 for (i
=0; known
[i
] && strcmp (known
[i
],oid
); i
++)
242 do_list (1, listmode
, fp
,
243 _("critical certificate extension %s is not supported"),
245 rc
= gpg_error (GPG_ERR_UNSUPPORTED_CERT
);
248 /* We ignore the error codes EOF as well as no-value. The later will
249 occur for certificates with no extensions at all. */
251 && gpg_err_code (err
) != GPG_ERR_EOF
252 && gpg_err_code (err
) != GPG_ERR_NO_VALUE
)
259 /* Check whether CERT is an allowed certificate. This requires that
260 CERT matches all requirements for such a CA, i.e. the
261 BasicConstraints extension. The function returns 0 on success and
262 the awlloed length of the chain at CHAINLEN. */
264 allowed_ca (ctrl_t ctrl
,
265 ksba_cert_t cert
, int *chainlen
, int listmode
, estream_t fp
)
270 err
= ksba_cert_is_ca (cert
, &flag
, chainlen
);
275 if (get_regtp_ca_info (ctrl
, cert
, chainlen
))
277 /* Note that dirmngr takes a different way to cope with such
279 return 0; /* RegTP issued certificate. */
282 do_list (1, listmode
, fp
,_("issuer certificate is not marked as a CA"));
283 return gpg_error (GPG_ERR_BAD_CA_CERT
);
290 check_cert_policy (ksba_cert_t cert
, int listmode
, estream_t fplist
)
297 err
= ksba_cert_get_cert_policies (cert
, &policies
);
298 if (gpg_err_code (err
) == GPG_ERR_NO_DATA
)
299 return 0; /* No policy given. */
303 /* STRING is a line delimited list of certificate policies as stored
304 in the certificate. The line itself is colon delimited where the
305 first field is the OID of the policy and the second field either
306 N or C for normal or critical extension */
308 if (opt
.verbose
> 1 && !listmode
)
309 log_info ("certificate's policy list: %s\n", policies
);
311 /* The check is very minimal but won't give false positives */
312 any_critical
= !!strstr (policies
, ":C");
314 if (!opt
.policy_file
)
319 do_list (1, listmode
, fplist
,
320 _("critical marked policy without configured policies"));
321 return gpg_error (GPG_ERR_NO_POLICY_MATCH
);
326 fp
= fopen (opt
.policy_file
, "r");
329 if (opt
.verbose
|| errno
!= ENOENT
)
330 log_info (_("failed to open `%s': %s\n"),
331 opt
.policy_file
, strerror (errno
));
333 /* With no critical policies this is only a warning */
337 do_list (0, listmode
, fplist
,
338 _("note: non-critical certificate policy not allowed"));
341 do_list (1, listmode
, fplist
,
342 _("certificate policy not allowed"));
343 return gpg_error (GPG_ERR_NO_POLICY_MATCH
);
350 char *haystack
, *allowed
;
355 if (!fgets (line
, DIM(line
)-1, fp
) )
357 gpg_error_t tmperr
= gpg_error (gpg_err_code_from_errno (errno
));
363 /* With no critical policies this is only a warning */
366 do_list (0, listmode
, fplist
,
367 _("note: non-critical certificate policy not allowed"));
370 do_list (1, listmode
, fplist
,
371 _("certificate policy not allowed"));
372 return gpg_error (GPG_ERR_NO_POLICY_MATCH
);
378 if (!*line
|| line
[strlen(line
)-1] != '\n')
380 /* eat until end of line */
381 while ( (c
=getc (fp
)) != EOF
&& c
!= '\n')
385 return gpg_error (*line
? GPG_ERR_LINE_TOO_LONG
386 : GPG_ERR_INCOMPLETE_LINE
);
389 /* Allow for empty lines and spaces */
390 for (p
=line
; spacep (p
); p
++)
393 while (!*p
|| *p
== '\n' || *p
== '#');
396 for (allowed
=line
; spacep (allowed
); allowed
++)
398 p
= strpbrk (allowed
, " :\n");
399 if (!*p
|| p
== allowed
)
403 return gpg_error (GPG_ERR_CONFIGURATION
);
405 *p
= 0; /* strip the rest of the line */
406 /* See whether we find ALLOWED (which is an OID) in POLICIES */
407 for (haystack
=policies
; (p
=strstr (haystack
, allowed
)); haystack
= p
+1)
409 if ( !(p
== policies
|| p
[-1] == '\n') )
410 continue; /* Does not match the begin of a line. */
411 if (p
[strlen (allowed
)] != ':')
412 continue; /* The length does not match. */
413 /* Yep - it does match so return okay. */
422 /* Helper function for find_up. This resets the key handle and search
423 for an issuer ISSUER with a subjectKeyIdentifier of KEYID. Returns
424 0 on success or -1 when not found. */
426 find_up_search_by_keyid (KEYDB_HANDLE kh
,
427 const char *issuer
, ksba_sexp_t keyid
)
430 ksba_cert_t cert
= NULL
;
431 ksba_sexp_t subj
= NULL
;
433 keydb_search_reset (kh
);
434 while (!(rc
= keydb_search_subject (kh
, issuer
)))
436 ksba_cert_release (cert
); cert
= NULL
;
437 rc
= keydb_get_cert (kh
, &cert
);
440 log_error ("keydb_get_cert() failed: rc=%d\n", rc
);
445 if (!ksba_cert_get_subj_key_id (cert
, NULL
, &subj
))
447 if (!cmp_simple_canon_sexp (keyid
, subj
))
448 break; /* Found matching cert. */
452 ksba_cert_release (cert
);
459 find_up_store_certs_cb (void *cb_value
, ksba_cert_t cert
)
461 if (keydb_store_cert (cert
, 1, NULL
))
462 log_error ("error storing issuer certificate as ephemeral\n");
467 /* Helper for find_up(). Locate the certificate for ISSUER using an
468 external lookup. KH is the keydb context we are currently using.
469 On success 0 is returned and the certificate may be retrieved from
470 the keydb using keydb_get_cert(). KEYID is the keyIdentifier from
473 find_up_external (ctrl_t ctrl
, KEYDB_HANDLE kh
,
474 const char *issuer
, ksba_sexp_t keyid
)
477 strlist_t names
= NULL
;
483 log_info (_("looking up issuer at external location\n"));
484 /* The Dirmngr process is confused about unknown attributes. As a
485 quick and ugly hack we locate the CN and use the issuer string
486 starting at this attribite. Fixme: we should have far better
487 parsing for external lookups in the Dirmngr. */
488 s
= strstr (issuer
, "CN=");
489 if (!s
|| s
== issuer
|| s
[-1] != ',')
491 pattern
= xtrymalloc (strlen (s
)+2);
493 return gpg_error_from_syserror ();
494 strcpy (stpcpy (pattern
, "/"), s
);
495 add_to_strlist (&names
, pattern
);
498 rc
= gpgsm_dirmngr_lookup (ctrl
, names
, 0, find_up_store_certs_cb
, &count
);
499 free_strlist (names
);
502 log_info (_("number of issuers matching: %d\n"), count
);
505 log_error ("external key lookup failed: %s\n", gpg_strerror (rc
));
513 /* The issuers are currently stored in the ephemeral key DB, so
514 we temporary switch to ephemeral mode. */
515 old
= keydb_set_ephemeral (kh
, 1);
517 rc
= find_up_search_by_keyid (kh
, issuer
, keyid
);
520 keydb_search_reset (kh
);
521 rc
= keydb_search_subject (kh
, issuer
);
523 keydb_set_ephemeral (kh
, old
);
529 /* Helper for find_up(). Ask the dirmngr for the certificate for
530 ISSUER with optional SERIALNO. KH is the keydb context we are
531 currently using. With SUBJECT_MODE set, ISSUER is searched as the
532 subject. On success 0 is returned and the certificate is available
533 in the ephemeral DB. */
535 find_up_dirmngr (ctrl_t ctrl
, KEYDB_HANDLE kh
,
536 ksba_sexp_t serialno
, const char *issuer
, int subject_mode
)
539 strlist_t names
= NULL
;
546 log_info (_("looking up issuer from the Dirmngr cache\n"));
549 pattern
= xtrymalloc (strlen (issuer
)+2);
551 strcpy (stpcpy (pattern
, "/"), issuer
);
554 pattern
= gpgsm_format_sn_issuer (serialno
, issuer
);
557 pattern
= xtrymalloc (strlen (issuer
)+3);
559 strcpy (stpcpy (pattern
, "#/"), issuer
);
562 return gpg_error_from_syserror ();
563 add_to_strlist (&names
, pattern
);
566 rc
= gpgsm_dirmngr_lookup (ctrl
, names
, 1, find_up_store_certs_cb
, &count
);
567 free_strlist (names
);
570 log_info (_("number of matching certificates: %d\n"), count
);
571 if (rc
&& !opt
.quiet
)
572 log_info (_("dirmngr cache-only key lookup failed: %s\n"),
574 return (!rc
&& count
)? 0 : -1;
579 /* Locate issuing certificate for CERT. ISSUER is the name of the
580 issuer used as a fallback if the other methods don't work. If
581 FIND_NEXT is true, the function shall return the next possible
582 issuer. The certificate itself is not directly returned but a
583 keydb_get_cert on the keyDb context KH will return it. Returns 0
584 on success, -1 if not found or an error code. */
586 find_up (ctrl_t ctrl
, KEYDB_HANDLE kh
,
587 ksba_cert_t cert
, const char *issuer
, int find_next
)
590 ksba_sexp_t authidno
;
594 if (!ksba_cert_get_auth_key_id (cert
, &keyid
, &authid
, &authidno
))
596 const char *s
= ksba_name_enum (authid
, 0);
599 rc
= keydb_search_issuer_sn (kh
, s
, authidno
);
601 keydb_search_reset (kh
);
603 /* In case of an error, try to get the certificate from the
604 dirmngr. That is done by trying to put that certifcate
605 into the ephemeral DB and let the code below do the
606 actual retrieve. Thus there is no error checking.
607 Skipped in find_next mode as usual. */
608 if (rc
== -1 && !find_next
)
609 find_up_dirmngr (ctrl
, kh
, authidno
, s
, 0);
611 /* In case of an error try the ephemeral DB. We can't do
612 that in find_next mode because we can't keep the search
614 if (rc
== -1 && !find_next
)
616 int old
= keydb_set_ephemeral (kh
, 1);
619 rc
= keydb_search_issuer_sn (kh
, s
, authidno
);
621 keydb_search_reset (kh
);
623 keydb_set_ephemeral (kh
, old
);
626 rc
= -1; /* Need to make sure to have this error code. */
629 if (rc
== -1 && keyid
&& !find_next
)
631 /* Not found by AIK.issuer_sn. Lets try the AIK.ki
632 instead. Loop over all certificates with that issuer as
633 subject and stop for the one with a matching
634 subjectKeyIdentifier. */
635 /* Fixme: Should we also search in the dirmngr? */
636 rc
= find_up_search_by_keyid (kh
, issuer
, keyid
);
639 int old
= keydb_set_ephemeral (kh
, 1);
641 rc
= find_up_search_by_keyid (kh
, issuer
, keyid
);
642 keydb_set_ephemeral (kh
, old
);
645 rc
= -1; /* Need to make sure to have this error code. */
648 /* If we still didn't found it, try to find it via the subject
649 from the dirmngr-cache. */
650 if (rc
== -1 && !find_next
)
652 if (!find_up_dirmngr (ctrl
, kh
, NULL
, issuer
, 1))
654 int old
= keydb_set_ephemeral (kh
, 1);
656 rc
= find_up_search_by_keyid (kh
, issuer
, keyid
);
659 keydb_search_reset (kh
);
660 rc
= keydb_search_subject (kh
, issuer
);
662 keydb_set_ephemeral (kh
, old
);
665 rc
= -1; /* Need to make sure to have this error code. */
668 /* If we still didn't found it, try an external lookup. */
669 if (rc
== -1 && opt
.auto_issuer_key_retrieve
&& !find_next
)
670 rc
= find_up_external (ctrl
, kh
, issuer
, keyid
);
672 /* Print a note so that the user does not feel too helpless when
673 an issuer certificate was found and gpgsm prints BAD
674 signature because it is not the correct one. */
675 if (rc
== -1 && opt
.quiet
)
679 log_info ("%sissuer certificate ", find_next
?"next ":"");
683 gpgsm_dump_serial (keyid
);
689 gpgsm_dump_serial (authidno
);
691 gpgsm_dump_string (s
);
694 log_printf ("not found using authorityKeyIdentifier\n");
697 log_error ("failed to find authorityKeyIdentifier: rc=%d\n", rc
);
699 ksba_name_release (authid
);
703 if (rc
) /* Not found via authorithyKeyIdentifier, try regular issuer name. */
704 rc
= keydb_search_subject (kh
, issuer
);
705 if (rc
== -1 && !find_next
)
709 /* Also try to get it from the Dirmngr cache. The function
710 merely puts it into the ephemeral database. */
711 find_up_dirmngr (ctrl
, kh
, NULL
, issuer
, 0);
713 /* Not found, let us see whether we have one in the ephemeral key DB. */
714 old
= keydb_set_ephemeral (kh
, 1);
717 keydb_search_reset (kh
);
718 rc
= keydb_search_subject (kh
, issuer
);
720 keydb_set_ephemeral (kh
, old
);
723 /* Still not found. If enabled, try an external lookup. */
724 if (rc
== -1 && opt
.auto_issuer_key_retrieve
&& !find_next
)
725 rc
= find_up_external (ctrl
, kh
, issuer
, NULL
);
731 /* Return the next certificate up in the chain starting at START.
732 Returns -1 when there are no more certificates. */
734 gpgsm_walk_cert_chain (ctrl_t ctrl
, ksba_cert_t start
, ksba_cert_t
*r_next
)
738 char *subject
= NULL
;
739 KEYDB_HANDLE kh
= keydb_new (0);
744 log_error (_("failed to allocated keyDB handle\n"));
745 rc
= gpg_error (GPG_ERR_GENERAL
);
749 issuer
= ksba_cert_get_issuer (start
, 0);
750 subject
= ksba_cert_get_subject (start
, 0);
753 log_error ("no issuer found in certificate\n");
754 rc
= gpg_error (GPG_ERR_BAD_CERT
);
759 log_error ("no subject found in certificate\n");
760 rc
= gpg_error (GPG_ERR_BAD_CERT
);
764 if (is_root_cert (start
, issuer
, subject
))
766 rc
= -1; /* we are at the root */
770 rc
= find_up (ctrl
, kh
, start
, issuer
, 0);
773 /* It is quite common not to have a certificate, so better don't
774 print an error here. */
775 if (rc
!= -1 && opt
.verbose
> 1)
776 log_error ("failed to find issuer's certificate: rc=%d\n", rc
);
777 rc
= gpg_error (GPG_ERR_MISSING_CERT
);
781 rc
= keydb_get_cert (kh
, r_next
);
784 log_error ("keydb_get_cert() failed: rc=%d\n", rc
);
785 rc
= gpg_error (GPG_ERR_GENERAL
);
796 /* Helper for gpgsm_is_root_cert. This one is used if the subject and
797 issuer DNs are already known. */
799 is_root_cert (ksba_cert_t cert
, const char *issuerdn
, const char *subjectdn
)
803 ksba_sexp_t serialno
;
804 ksba_sexp_t ak_keyid
;
807 const char *ak_name_str
;
808 ksba_sexp_t subj_keyid
= NULL
;
810 if (!issuerdn
|| !subjectdn
)
813 if (strcmp (issuerdn
, subjectdn
))
816 err
= ksba_cert_get_auth_key_id (cert
, &ak_keyid
, &ak_name
, &ak_sn
);
819 if (gpg_err_code (err
) == GPG_ERR_NO_DATA
)
820 return 1; /* Yes. Without a authorityKeyIdentifier this needs
821 to be the Root certifcate (our trust anchor). */
822 log_error ("error getting authorityKeyIdentifier: %s\n",
824 return 0; /* Well, it is broken anyway. Return No. */
827 serialno
= ksba_cert_get_serial (cert
);
830 log_error ("error getting serialno: %s\n", gpg_strerror (err
));
834 /* Check whether the auth name's matches the issuer name+sn. If
835 that is the case this is a root certificate. */
836 ak_name_str
= ksba_name_enum (ak_name
, 0);
838 && !strcmp (ak_name_str
, issuerdn
)
839 && !cmp_simple_canon_sexp (ak_sn
, serialno
))
841 result
= 1; /* Right, CERT is self-signed. */
845 /* Similar for the ak_keyid. */
846 if (ak_keyid
&& !ksba_cert_get_subj_key_id (cert
, NULL
, &subj_keyid
)
847 && !cmp_simple_canon_sexp (ak_keyid
, subj_keyid
))
849 result
= 1; /* Right, CERT is self-signed. */
855 ksba_free (subj_keyid
);
856 ksba_free (ak_keyid
);
857 ksba_name_release (ak_name
);
859 ksba_free (serialno
);
865 /* Check whether the CERT is a root certificate. Returns True if this
868 gpgsm_is_root_cert (ksba_cert_t cert
)
874 issuer
= ksba_cert_get_issuer (cert
, 0);
875 subject
= ksba_cert_get_subject (cert
, 0);
876 yes
= is_root_cert (cert
, issuer
, subject
);
883 /* This is a helper for gpgsm_validate_chain. */
885 is_cert_still_valid (ctrl_t ctrl
, int force_ocsp
, int lm
, estream_t fp
,
886 ksba_cert_t subject_cert
, ksba_cert_t issuer_cert
,
887 int *any_revoked
, int *any_no_crl
, int *any_crl_too_old
)
891 if (opt
.no_crl_check
&& !ctrl
->use_ocsp
)
893 audit_log_ok (ctrl
->audit
, AUDIT_CRL_CHECK
,
894 gpg_error (GPG_ERR_NOT_ENABLED
));
898 err
= gpgsm_dirmngr_isvalid (ctrl
,
899 subject_cert
, issuer_cert
,
900 force_ocsp
? 2 : !!ctrl
->use_ocsp
);
901 audit_log_ok (ctrl
->audit
, AUDIT_CRL_CHECK
, err
);
906 gpgsm_cert_log_name (NULL
, subject_cert
);
907 switch (gpg_err_code (err
))
909 case GPG_ERR_CERT_REVOKED
:
910 do_list (1, lm
, fp
, _("certificate has been revoked"));
912 /* Store that in the keybox so that key listings are able to
913 return the revoked flag. We don't care about error,
915 keydb_set_cert_flags (subject_cert
, 1, KEYBOX_FLAG_VALIDITY
, 0,
916 ~0, VALIDITY_REVOKED
);
919 case GPG_ERR_NO_CRL_KNOWN
:
920 do_list (1, lm
, fp
, _("no CRL found for certificate"));
924 case GPG_ERR_NO_DATA
:
925 do_list (1, lm
, fp
, _("the status of the certificate is unknown"));
929 case GPG_ERR_CRL_TOO_OLD
:
930 do_list (1, lm
, fp
, _("the available CRL is too old"));
932 log_info (_("please make sure that the "
933 "\"dirmngr\" is properly installed\n"));
934 *any_crl_too_old
= 1;
938 do_list (1, lm
, fp
, _("checking the CRL failed: %s"),
947 /* Helper for gpgsm_validate_chain to check the validity period of
948 SUBJECT_CERT. The caller needs to pass EXPTIME which will be
949 updated to the nearest expiration time seen. A DEPTH of 0 indicates
950 the target certifciate, -1 the final root certificate and other
951 values intermediate certificates. */
953 check_validity_period (ksba_isotime_t current_time
,
954 ksba_cert_t subject_cert
,
955 ksba_isotime_t exptime
,
956 int listmode
, estream_t listfp
, int depth
)
959 ksba_isotime_t not_before
, not_after
;
961 err
= ksba_cert_get_validity (subject_cert
, 0, not_before
);
963 err
= ksba_cert_get_validity (subject_cert
, 1, not_after
);
966 do_list (1, listmode
, listfp
,
967 _("certificate with invalid validity: %s"), gpg_strerror (err
));
968 return gpg_error (GPG_ERR_BAD_CERT
);
974 gnupg_copy_time (exptime
, not_after
);
975 else if (strcmp (not_after
, exptime
) < 0 )
976 gnupg_copy_time (exptime
, not_after
);
979 if (*not_before
&& strcmp (current_time
, not_before
) < 0 )
981 do_list (1, listmode
, listfp
,
982 depth
== 0 ? _("certificate not yet valid") :
983 depth
== -1 ? _("root certificate not yet valid") :
984 /* other */ _("intermediate certificate not yet valid"));
987 log_info (" (valid from ");
988 dump_isotime (not_before
);
991 return gpg_error (GPG_ERR_CERT_TOO_YOUNG
);
994 if (*not_after
&& strcmp (current_time
, not_after
) > 0 )
996 do_list (opt
.ignore_expiration
?0:1, listmode
, listfp
,
997 depth
== 0 ? _("certificate has expired") :
998 depth
== -1 ? _("root certificate has expired") :
999 /* other */ _("intermediate certificate has expired"));
1002 log_info (" (expired at ");
1003 dump_isotime (not_after
);
1006 if (opt
.ignore_expiration
)
1007 log_info ("WARNING: ignoring expiration\n");
1009 return gpg_error (GPG_ERR_CERT_EXPIRED
);
1015 /* This is a variant of check_validity_period used with the chain
1016 model. The dextra contraint here is that notBefore and notAfter
1017 must exists and if the additional argument CHECK_TIME is given this
1018 time is used to check the validity period of SUBJECT_CERT. */
1020 check_validity_period_cm (ksba_isotime_t current_time
,
1021 ksba_isotime_t check_time
,
1022 ksba_cert_t subject_cert
,
1023 ksba_isotime_t exptime
,
1024 int listmode
, estream_t listfp
, int depth
)
1027 ksba_isotime_t not_before
, not_after
;
1029 err
= ksba_cert_get_validity (subject_cert
, 0, not_before
);
1031 err
= ksba_cert_get_validity (subject_cert
, 1, not_after
);
1034 do_list (1, listmode
, listfp
,
1035 _("certificate with invalid validity: %s"), gpg_strerror (err
));
1036 return gpg_error (GPG_ERR_BAD_CERT
);
1038 if (!*not_before
|| !*not_after
)
1040 do_list (1, listmode
, listfp
,
1041 _("required certificate attributes missing: %s%s%s"),
1042 !*not_before
? "notBefore":"",
1043 (!*not_before
&& !*not_after
)? ", ":"",
1044 !*not_before
? "notAfter":"");
1045 return gpg_error (GPG_ERR_BAD_CERT
);
1047 if (strcmp (not_before
, not_after
) > 0 )
1049 do_list (1, listmode
, listfp
,
1050 _("certificate with invalid validity"));
1051 log_info (" (valid from ");
1052 dump_isotime (not_before
);
1053 log_printf (" expired at ");
1054 dump_isotime (not_after
);
1056 return gpg_error (GPG_ERR_BAD_CERT
);
1060 gnupg_copy_time (exptime
, not_after
);
1061 else if (strcmp (not_after
, exptime
) < 0 )
1062 gnupg_copy_time (exptime
, not_after
);
1064 if (strcmp (current_time
, not_before
) < 0 )
1066 do_list (1, listmode
, listfp
,
1067 depth
== 0 ? _("certificate not yet valid") :
1068 depth
== -1 ? _("root certificate not yet valid") :
1069 /* other */ _("intermediate certificate not yet valid"));
1072 log_info (" (valid from ");
1073 dump_isotime (not_before
);
1076 return gpg_error (GPG_ERR_CERT_TOO_YOUNG
);
1080 && (strcmp (check_time
, not_before
) < 0
1081 || strcmp (check_time
, not_after
) > 0))
1083 /* Note that we don't need a case for the root certificate
1084 because its own consitency has already been checked. */
1085 do_list(opt
.ignore_expiration
?0:1, listmode
, listfp
,
1087 _("signature not created during lifetime of certificate") :
1089 _("certificate not created during lifetime of issuer") :
1090 _("intermediate certificate not created during lifetime "
1094 log_info (depth
== 0? _(" ( signature created at ") :
1095 /* */ _(" (certificate created at ") );
1096 dump_isotime (check_time
);
1098 log_info (depth
==0? _(" (certificate valid from ") :
1099 /* */ _(" ( issuer valid from ") );
1100 dump_isotime (not_before
);
1102 dump_isotime (not_after
);
1105 if (opt
.ignore_expiration
)
1106 log_info ("WARNING: ignoring expiration\n");
1108 return gpg_error (GPG_ERR_CERT_EXPIRED
);
1116 /* Ask the user whether he wants to mark the certificate CERT trusted.
1117 Returns true if the CERT is the trusted. We also check whether the
1118 agent is at all enabled to allow marktrusted and don't call it in
1119 this session again if it is not. */
1121 ask_marktrusted (ctrl_t ctrl
, ksba_cert_t cert
, int listmode
)
1123 static int no_more_questions
;
1128 fpr
= gpgsm_get_fingerprint_string (cert
, GCRY_MD_SHA1
);
1129 log_info (_("fingerprint=%s\n"), fpr
? fpr
: "?");
1132 if (no_more_questions
)
1133 rc
= gpg_error (GPG_ERR_NOT_SUPPORTED
);
1135 rc
= gpgsm_agent_marktrusted (ctrl
, cert
);
1138 log_info (_("root certificate has now been marked as trusted\n"));
1143 gpgsm_dump_cert ("issuer", cert
);
1144 log_info ("after checking the fingerprint, you may want "
1145 "to add it manually to the list of trusted certificates.\n");
1148 if (gpg_err_code (rc
) == GPG_ERR_NOT_SUPPORTED
)
1150 if (!no_more_questions
)
1151 log_info (_("interactive marking as trusted "
1152 "not enabled in gpg-agent\n"));
1153 no_more_questions
= 1;
1155 else if (gpg_err_code (rc
) == GPG_ERR_CANCELED
)
1157 log_info (_("interactive marking as trusted "
1158 "disabled for this session\n"));
1159 no_more_questions
= 1;
1162 set_already_asked_marktrusted (cert
);
1170 /* Validate a chain and optionally return the nearest expiration time
1171 in R_EXPTIME. With LISTMODE set to 1 a special listmode is
1172 activated where only information about the certificate is printed
1173 to LISTFP and no output is send to the usual log stream. If
1174 CHECKTIME_ARG is set, it is used only in the chain model instead of the
1179 VALIDATE_FLAG_NO_DIRMNGR - Do not do any dirmngr isvalid checks.
1180 VALIDATE_FLAG_CHAIN_MODEL - Check according to chain model.
1183 do_validate_chain (ctrl_t ctrl
, ksba_cert_t cert
, ksba_isotime_t checktime_arg
,
1184 ksba_isotime_t r_exptime
,
1185 int listmode
, estream_t listfp
, unsigned int flags
,
1186 struct rootca_flags_s
*rootca_flags
)
1188 int rc
= 0, depth
, maxdepth
;
1189 char *issuer
= NULL
;
1190 char *subject
= NULL
;
1191 KEYDB_HANDLE kh
= NULL
;
1192 ksba_cert_t subject_cert
= NULL
, issuer_cert
= NULL
;
1193 ksba_isotime_t current_time
;
1194 ksba_isotime_t check_time
;
1195 ksba_isotime_t exptime
;
1196 int any_expired
= 0;
1197 int any_revoked
= 0;
1199 int any_crl_too_old
= 0;
1200 int any_no_policy_match
= 0;
1201 int is_qualified
= -1; /* Indicates whether the certificate stems
1202 from a qualified root certificate.
1203 -1 = unknown, 0 = no, 1 = yes. */
1204 chain_item_t chain
= NULL
; /* A list of all certificates in the chain. */
1207 gnupg_get_isotime (current_time
);
1209 if ( (flags
& VALIDATE_FLAG_CHAIN_MODEL
) )
1211 if (!strcmp (checktime_arg
, "19700101T000000"))
1213 do_list (1, listmode
, listfp
,
1214 _("WARNING: creation time of signature not known - "
1215 "assuming current time"));
1216 gnupg_copy_time (check_time
, current_time
);
1219 gnupg_copy_time (check_time
, checktime_arg
);
1228 if (opt
.no_chain_validation
&& !listmode
)
1230 log_info ("WARNING: bypassing certificate chain validation\n");
1237 log_error (_("failed to allocated keyDB handle\n"));
1238 rc
= gpg_error (GPG_ERR_GENERAL
);
1242 if (DBG_X509
&& !listmode
)
1243 gpgsm_dump_cert ("target", cert
);
1245 subject_cert
= cert
;
1246 ksba_cert_ref (subject_cert
);
1253 gpg_error_t istrusted_rc
= -1;
1255 /* Put the certificate on our list. */
1259 ci
= xtrycalloc (1, sizeof *ci
);
1262 rc
= gpg_error_from_syserror ();
1265 ksba_cert_ref (subject_cert
);
1266 ci
->cert
= subject_cert
;
1273 issuer
= ksba_cert_get_issuer (subject_cert
, 0);
1274 subject
= ksba_cert_get_subject (subject_cert
, 0);
1278 do_list (1, listmode
, listfp
, _("no issuer found in certificate"));
1279 rc
= gpg_error (GPG_ERR_BAD_CERT
);
1284 /* Is this a self-issued certificate (i.e. the root certificate)? */
1285 is_root
= is_root_cert (subject_cert
, issuer
, subject
);
1289 /* Check early whether the certificate is listed as trusted.
1290 We used to do this only later but changed it to call the
1291 check right here so that we can access special flags
1292 associated with that specific root certificate. */
1293 istrusted_rc
= gpgsm_agent_istrusted (ctrl
, subject_cert
, NULL
,
1295 audit_log_cert (ctrl
->audit
, AUDIT_ROOT_TRUSTED
,
1296 subject_cert
, istrusted_rc
);
1297 /* If the chain model extended attribute is used, make sure
1298 that our chain model flag is set. */
1299 if (has_validation_model_chain (subject_cert
, listmode
, listfp
))
1300 rootca_flags
->chain_model
= 1;
1304 /* Check the validity period. */
1305 if ( (flags
& VALIDATE_FLAG_CHAIN_MODEL
) )
1306 rc
= check_validity_period_cm (current_time
, check_time
, subject_cert
,
1307 exptime
, listmode
, listfp
,
1308 (depth
&& is_root
)? -1: depth
);
1310 rc
= check_validity_period (current_time
, subject_cert
,
1311 exptime
, listmode
, listfp
,
1312 (depth
&& is_root
)? -1: depth
);
1313 if (gpg_err_code (rc
) == GPG_ERR_CERT_EXPIRED
)
1322 /* Assert that we understand all critical extensions. */
1323 rc
= unknown_criticals (subject_cert
, listmode
, listfp
);
1327 /* Do a policy check. */
1328 if (!opt
.no_policy_check
)
1330 rc
= check_cert_policy (subject_cert
, listmode
, listfp
);
1331 if (gpg_err_code (rc
) == GPG_ERR_NO_POLICY_MATCH
)
1333 any_no_policy_match
= 1;
1341 /* If this is the root certificate we are at the end of the chain. */
1345 ; /* No need to check the certificate for a trusted one. */
1346 else if (gpgsm_check_cert_sig (subject_cert
, subject_cert
) )
1348 /* We only check the signature if the certificate is not
1349 trusted for better diagnostics. */
1350 do_list (1, listmode
, listfp
,
1351 _("self-signed certificate has a BAD signature"));
1354 gpgsm_dump_cert ("self-signing cert", subject_cert
);
1356 rc
= gpg_error (depth
? GPG_ERR_BAD_CERT_CHAIN
1357 : GPG_ERR_BAD_CERT
);
1360 if (!rootca_flags
->relax
)
1362 rc
= allowed_ca (ctrl
, subject_cert
, NULL
, listmode
, listfp
);
1368 /* Set the flag for qualified signatures. This flag is
1369 deduced from a list of root certificates allowed for
1370 qualified signatures. */
1371 if (is_qualified
== -1)
1377 if (!ksba_cert_get_user_data (cert
, "is_qualified",
1381 /* We already checked this for this certificate,
1382 thus we simply take it from the user data. */
1383 is_qualified
= !!*buf
;
1387 /* Need to consult the list of root certificates for
1388 qualified signatures. */
1389 err
= gpgsm_is_in_qualified_list (ctrl
, subject_cert
, NULL
);
1392 else if ( gpg_err_code (err
) == GPG_ERR_NOT_FOUND
)
1395 log_error ("checking the list of qualified "
1396 "root certificates failed: %s\n",
1397 gpg_strerror (err
));
1398 if ( is_qualified
!= -1 )
1400 /* Cache the result but don't care too much
1402 buf
[0] = !!is_qualified
;
1403 err
= ksba_cert_set_user_data (subject_cert
,
1404 "is_qualified", buf
, 1);
1406 log_error ("set_user_data(is_qualified) failed: %s\n",
1407 gpg_strerror (err
));
1413 /* Act on the check for a trusted root certificates. */
1417 else if (gpg_err_code (rc
) == GPG_ERR_NOT_TRUSTED
)
1419 do_list (0, listmode
, listfp
,
1420 _("root certificate is not marked trusted"));
1421 /* If we already figured out that the certificate is
1422 expired it does not make much sense to ask the user
1423 whether we wants to trust the root certificate. We
1424 should do this only if the certificate under question
1425 will then be usable. */
1427 && (!listmode
|| !already_asked_marktrusted (subject_cert
))
1428 && ask_marktrusted (ctrl
, subject_cert
, listmode
) )
1433 log_error (_("checking the trust list failed: %s\n"),
1440 /* Check for revocations etc. */
1441 if ((flags
& VALIDATE_FLAG_NO_DIRMNGR
))
1443 else if (opt
.no_trusted_cert_crl_check
|| rootca_flags
->relax
)
1446 rc
= is_cert_still_valid (ctrl
,
1447 (flags
& VALIDATE_FLAG_CHAIN_MODEL
),
1449 subject_cert
, subject_cert
,
1450 &any_revoked
, &any_no_crl
,
1455 break; /* Okay: a self-signed certicate is an end-point. */
1456 } /* End is_root. */
1459 /* Take care that the chain does not get too long. */
1460 if ((depth
+1) > maxdepth
)
1462 do_list (1, listmode
, listfp
, _("certificate chain too long\n"));
1463 rc
= gpg_error (GPG_ERR_BAD_CERT_CHAIN
);
1467 /* Find the next cert up the tree. */
1468 keydb_search_reset (kh
);
1469 rc
= find_up (ctrl
, kh
, subject_cert
, issuer
, 0);
1474 do_list (0, listmode
, listfp
, _("issuer certificate not found"));
1477 log_info ("issuer certificate: #/");
1478 gpgsm_dump_string (issuer
);
1483 log_error ("failed to find issuer's certificate: rc=%d\n", rc
);
1484 rc
= gpg_error (GPG_ERR_MISSING_CERT
);
1488 ksba_cert_release (issuer_cert
); issuer_cert
= NULL
;
1489 rc
= keydb_get_cert (kh
, &issuer_cert
);
1492 log_error ("keydb_get_cert() failed: rc=%d\n", rc
);
1493 rc
= gpg_error (GPG_ERR_GENERAL
);
1500 log_debug ("got issuer's certificate:\n");
1501 gpgsm_dump_cert ("issuer", issuer_cert
);
1504 rc
= gpgsm_check_cert_sig (issuer_cert
, subject_cert
);
1507 do_list (0, listmode
, listfp
, _("certificate has a BAD signature"));
1510 gpgsm_dump_cert ("signing issuer", issuer_cert
);
1511 gpgsm_dump_cert ("signed subject", subject_cert
);
1513 if (gpg_err_code (rc
) == GPG_ERR_BAD_SIGNATURE
)
1515 /* We now try to find other issuer certificates which
1516 might have been used. This is required because some
1517 CAs are reusing the issuer and subject DN for new
1518 root certificates. */
1519 /* FIXME: Do this only if we don't have an
1520 AKI.keyIdentifier */
1521 rc
= find_up (ctrl
, kh
, subject_cert
, issuer
, 1);
1524 ksba_cert_t tmp_cert
;
1526 rc
= keydb_get_cert (kh
, &tmp_cert
);
1527 if (rc
|| !compare_certs (issuer_cert
, tmp_cert
))
1529 /* The find next did not work or returned an
1530 identical certificate. We better stop here
1531 to avoid infinite checks. */
1532 rc
= gpg_error (GPG_ERR_BAD_SIGNATURE
);
1533 ksba_cert_release (tmp_cert
);
1537 do_list (0, listmode
, listfp
,
1538 _("found another possible matching "
1539 "CA certificate - trying again"));
1540 ksba_cert_release (issuer_cert
);
1541 issuer_cert
= tmp_cert
;
1542 goto try_another_cert
;
1547 /* We give a more descriptive error code than the one
1548 returned from the signature checking. */
1549 rc
= gpg_error (GPG_ERR_BAD_CERT_CHAIN
);
1553 is_root
= gpgsm_is_root_cert (issuer_cert
);
1557 /* Check that a CA is allowed to issue certificates. */
1561 rc
= allowed_ca (ctrl
, issuer_cert
, &chainlen
, listmode
, listfp
);
1564 /* Not allowed. Check whether this is a trusted root
1565 certificate and whether we allow special exceptions.
1566 We could carry the result of the test over to the
1567 regular root check at the top of the loop but for
1568 clarity we won't do that. Given that the majority of
1569 certificates carry proper BasicContraints our way of
1570 overriding an error in the way is justified for
1571 performance reasons. */
1574 istrusted_rc
= gpgsm_agent_istrusted (ctrl
, issuer_cert
, NULL
,
1576 if (!istrusted_rc
&& rootca_flags
->relax
)
1578 /* Ignore the error due to the relax flag. */
1586 if (chainlen
>= 0 && depth
> chainlen
)
1588 do_list (1, listmode
, listfp
,
1589 _("certificate chain longer than allowed by CA (%d)"),
1591 rc
= gpg_error (GPG_ERR_BAD_CERT_CHAIN
);
1596 /* Is the certificate allowed to sign other certificates. */
1599 rc
= gpgsm_cert_use_cert_p (issuer_cert
);
1603 sprintf (numbuf
, "%d", rc
);
1604 gpgsm_status2 (ctrl
, STATUS_ERROR
, "certcert.issuer.keyusage",
1610 /* Check for revocations etc. Note that for a root certificate
1611 this test is done a second time later. This should eventually
1613 if ((flags
& VALIDATE_FLAG_NO_DIRMNGR
))
1615 else if (is_root
&& (opt
.no_trusted_cert_crl_check
1616 || (!istrusted_rc
&& rootca_flags
->relax
)))
1619 rc
= is_cert_still_valid (ctrl
,
1620 (flags
& VALIDATE_FLAG_CHAIN_MODEL
),
1622 subject_cert
, issuer_cert
,
1623 &any_revoked
, &any_no_crl
, &any_crl_too_old
);
1628 if (opt
.verbose
&& !listmode
)
1629 log_info (depth
== 0 ? _("certificate is good\n") :
1630 !is_root
? _("intermediate certificate is good\n") :
1631 /* other */ _("root certificate is good\n"));
1633 /* Under the chain model the next check time is the creation
1634 time of the subject certificate. */
1635 if ( (flags
& VALIDATE_FLAG_CHAIN_MODEL
) )
1637 rc
= ksba_cert_get_validity (subject_cert
, 0, check_time
);
1640 /* That will never happen as we have already checked
1646 /* For the next round the current issuer becomes the new subject. */
1647 keydb_search_reset (kh
);
1648 ksba_cert_release (subject_cert
);
1649 subject_cert
= issuer_cert
;
1652 } /* End chain traversal. */
1654 if (!listmode
&& !opt
.quiet
)
1656 if (opt
.no_policy_check
)
1657 log_info ("policies not checked due to %s option\n",
1658 "--disable-policy-checks");
1659 if (opt
.no_crl_check
&& !ctrl
->use_ocsp
)
1660 log_info ("CRLs not checked due to %s option\n",
1661 "--disable-crl-checks");
1665 { /* If we encountered an error somewhere during the checks, set
1666 the error code to the most critical one */
1668 rc
= gpg_error (GPG_ERR_CERT_REVOKED
);
1669 else if (any_expired
)
1670 rc
= gpg_error (GPG_ERR_CERT_EXPIRED
);
1671 else if (any_no_crl
)
1672 rc
= gpg_error (GPG_ERR_NO_CRL_KNOWN
);
1673 else if (any_crl_too_old
)
1674 rc
= gpg_error (GPG_ERR_CRL_TOO_OLD
);
1675 else if (any_no_policy_match
)
1676 rc
= gpg_error (GPG_ERR_NO_POLICY_MATCH
);
1680 /* If we have traversed a complete chain up to the root we will
1681 reset the ephemeral flag for all these certificates. This is done
1682 regardless of any error because those errors may only be
1684 if (chain
&& chain
->is_root
)
1689 for (ci
= chain
; ci
; ci
= ci
->next
)
1691 /* Note that it is possible for the last certificate in the
1692 chain (i.e. our target certificate) that it has not yet
1693 been stored in the keybox and thus the flag can't be set.
1694 We ignore this error becuase it will later be stored
1696 err
= keydb_set_cert_flags (ci
->cert
, 1, KEYBOX_FLAG_BLOB
, 0,
1697 KEYBOX_FLAG_BLOB_EPHEMERAL
, 0);
1698 if (!ci
->next
&& gpg_err_code (err
) == GPG_ERR_NOT_FOUND
)
1701 log_error ("clearing ephemeral flag failed: %s\n",
1702 gpg_strerror (err
));
1706 /* If we have figured something about the qualified signature
1707 capability of the certificate under question, store the result as
1708 user data in all certificates of the chain. We do this even if the
1709 validation itself failed. */
1710 if (is_qualified
!= -1)
1716 buf
[0] = !!is_qualified
;
1718 for (ci
= chain
; ci
; ci
= ci
->next
)
1720 err
= ksba_cert_set_user_data (ci
->cert
, "is_qualified", buf
, 1);
1723 log_error ("set_user_data(is_qualified) failed: %s\n",
1724 gpg_strerror (err
));
1731 /* If auditing has been enabled, record what is in the chain. */
1736 audit_log (ctrl
->audit
, AUDIT_CHAIN_BEGIN
);
1737 for (ci
= chain
; ci
; ci
= ci
->next
)
1739 audit_log_cert (ctrl
->audit
,
1740 ci
->is_root
? AUDIT_CHAIN_ROOTCERT
: AUDIT_CHAIN_CERT
,
1743 audit_log (ctrl
->audit
, AUDIT_CHAIN_END
);
1747 gnupg_copy_time (r_exptime
, exptime
);
1753 chain_item_t ci_next
= chain
->next
;
1754 ksba_cert_release (chain
->cert
);
1758 ksba_cert_release (issuer_cert
);
1759 ksba_cert_release (subject_cert
);
1764 /* Validate a certificate chain. For a description see
1765 do_validate_chain. This function is a wrapper to handle a root
1766 certificate with the chain_model flag set. If RETFLAGS is not
1767 NULL, flags indicating now the verification was done are stored
1768 there. The only defined flag for RETFLAGS is
1769 VALIDATE_FLAG_CHAIN_MODEL.
1771 If you are verifying a signature you should set CHECKTIME to the
1772 creation time of the signature. If your are verifying a
1773 certificate, set it nil (i.e. the empty string). If the creation
1774 date of the signature is not known use the special date
1775 "19700101T000000" which is treated in a special way here. */
1777 gpgsm_validate_chain (ctrl_t ctrl
, ksba_cert_t cert
, ksba_isotime_t checktime
,
1778 ksba_isotime_t r_exptime
,
1779 int listmode
, estream_t listfp
, unsigned int flags
,
1780 unsigned int *retflags
)
1783 struct rootca_flags_s rootca_flags
;
1784 unsigned int dummy_retflags
;
1787 retflags
= &dummy_retflags
;
1789 if (ctrl
->validation_model
== 1)
1790 flags
|= VALIDATE_FLAG_CHAIN_MODEL
;
1792 *retflags
= (flags
& VALIDATE_FLAG_CHAIN_MODEL
);
1793 memset (&rootca_flags
, 0, sizeof rootca_flags
);
1795 rc
= do_validate_chain (ctrl
, cert
, checktime
,
1796 r_exptime
, listmode
, listfp
, flags
,
1798 if (gpg_err_code (rc
) == GPG_ERR_CERT_EXPIRED
1799 && !(flags
& VALIDATE_FLAG_CHAIN_MODEL
)
1800 && (rootca_flags
.valid
&& rootca_flags
.chain_model
))
1802 do_list (0, listmode
, listfp
, _("switching to chain model"));
1803 rc
= do_validate_chain (ctrl
, cert
, checktime
,
1804 r_exptime
, listmode
, listfp
,
1805 (flags
|= VALIDATE_FLAG_CHAIN_MODEL
),
1807 *retflags
|= VALIDATE_FLAG_CHAIN_MODEL
;
1811 do_list (0, listmode
, listfp
, _("validation model used: %s"),
1812 (*retflags
& VALIDATE_FLAG_CHAIN_MODEL
)?
1813 _("chain"):_("shell"));
1819 /* Check that the given certificate is valid but DO NOT check any
1820 constraints. We assume that the issuers certificate is already in
1821 the DB and that this one is valid; which it should be because it
1822 has been checked using this function. */
1824 gpgsm_basic_cert_check (ctrl_t ctrl
, ksba_cert_t cert
)
1827 char *issuer
= NULL
;
1828 char *subject
= NULL
;
1830 ksba_cert_t issuer_cert
= NULL
;
1832 if (opt
.no_chain_validation
)
1834 log_info ("WARNING: bypassing basic certificate checks\n");
1841 log_error (_("failed to allocated keyDB handle\n"));
1842 rc
= gpg_error (GPG_ERR_GENERAL
);
1846 issuer
= ksba_cert_get_issuer (cert
, 0);
1847 subject
= ksba_cert_get_subject (cert
, 0);
1850 log_error ("no issuer found in certificate\n");
1851 rc
= gpg_error (GPG_ERR_BAD_CERT
);
1855 if (is_root_cert (cert
, issuer
, subject
))
1857 rc
= gpgsm_check_cert_sig (cert
, cert
);
1860 log_error ("self-signed certificate has a BAD signature: %s\n",
1864 gpgsm_dump_cert ("self-signing cert", cert
);
1866 rc
= gpg_error (GPG_ERR_BAD_CERT
);
1872 /* Find the next cert up the tree. */
1873 keydb_search_reset (kh
);
1874 rc
= find_up (ctrl
, kh
, cert
, issuer
, 0);
1879 log_info ("issuer certificate (#/");
1880 gpgsm_dump_string (issuer
);
1881 log_printf (") not found\n");
1884 log_error ("failed to find issuer's certificate: rc=%d\n", rc
);
1885 rc
= gpg_error (GPG_ERR_MISSING_CERT
);
1889 ksba_cert_release (issuer_cert
); issuer_cert
= NULL
;
1890 rc
= keydb_get_cert (kh
, &issuer_cert
);
1893 log_error ("keydb_get_cert() failed: rc=%d\n", rc
);
1894 rc
= gpg_error (GPG_ERR_GENERAL
);
1898 rc
= gpgsm_check_cert_sig (issuer_cert
, cert
);
1901 log_error ("certificate has a BAD signature: %s\n",
1905 gpgsm_dump_cert ("signing issuer", issuer_cert
);
1906 gpgsm_dump_cert ("signed subject", cert
);
1908 rc
= gpg_error (GPG_ERR_BAD_CERT
);
1912 log_info (_("certificate is good\n"));
1919 ksba_cert_release (issuer_cert
);
1925 /* Check whether the certificate CERT has been issued by the German
1926 authority for qualified signature. They do not set the
1927 basicConstraints and thus we need this workaround. It works by
1928 looking up the root certificate and checking whether that one is
1929 listed as a qualified certificate for Germany.
1931 We also try to cache this data but as long as don't keep a
1932 reference to the certificate this won't be used.
1934 Returns: True if CERT is a RegTP issued CA cert (i.e. the root
1935 certificate itself or one of the CAs). In that case CHAINLEN will
1936 receive the length of the chain which is either 0 or 1.
1939 get_regtp_ca_info (ctrl_t ctrl
, ksba_cert_t cert
, int *chainlen
)
1946 ksba_cert_t array
[4];
1952 chainlen
= &dummy_chainlen
;
1955 err
= ksba_cert_get_user_data (cert
, "regtp_ca_chainlen",
1956 &buf
, sizeof (buf
), &buflen
);
1960 if (buflen
< 2 || !*buf
)
1961 return 0; /* Nothing found. */
1963 return 1; /* This is a regtp CA. */
1965 else if (gpg_err_code (err
) != GPG_ERR_NOT_FOUND
)
1967 log_error ("ksba_cert_get_user_data(%s) failed: %s\n",
1968 "regtp_ca_chainlen", gpg_strerror (err
));
1969 return 0; /* Nothing found. */
1972 /* Need to gather the info. This requires to walk up the chain
1973 until we have found the root. Because we are only interested in
1974 German Bundesnetzagentur (former RegTP) derived certificates 3
1975 levels are enough. (The German signature law demands a 3 tier
1976 hierachy; thus there is only one CA between the EE and the Root
1978 memset (&array
, 0, sizeof array
);
1981 ksba_cert_ref (cert
);
1982 array
[depth
++] = cert
;
1983 ksba_cert_ref (cert
);
1984 while (depth
< DIM(array
) && !(rc
=gpgsm_walk_cert_chain (ctrl
, cert
, &next
)))
1986 ksba_cert_release (cert
);
1987 ksba_cert_ref (next
);
1988 array
[depth
++] = next
;
1991 ksba_cert_release (cert
);
1992 if (rc
!= -1 || !depth
|| depth
== DIM(array
) )
1994 /* We did not reached the root. */
1998 /* If this is a German signature law issued certificate, we store
1999 additional additional information. */
2000 if (!gpgsm_is_in_qualified_list (NULL
, array
[depth
-1], country
)
2001 && !strcmp (country
, "de"))
2003 /* Setting the pathlen for the root CA and the CA flag for the
2004 next one is all what we need to do. */
2005 err
= ksba_cert_set_user_data (array
[depth
-1], "regtp_ca_chainlen",
2007 if (!err
&& depth
> 1)
2008 err
= ksba_cert_set_user_data (array
[depth
-2], "regtp_ca_chainlen",
2011 log_error ("ksba_set_user_data(%s) failed: %s\n",
2012 "regtp_ca_chainlen", gpg_strerror (err
));
2013 for (i
=0; i
< depth
; i
++)
2014 ksba_cert_release (array
[i
]);
2015 *chainlen
= (depth
>1? 0:1);
2020 /* Nothing special with this certificate. Mark the target
2021 certificate anyway to avoid duplicate lookups. */
2022 err
= ksba_cert_set_user_data (cert
, "regtp_ca_chainlen", "", 1);
2024 log_error ("ksba_set_user_data(%s) failed: %s\n",
2025 "regtp_ca_chainlen", gpg_strerror (err
));
2026 for (i
=0; i
< depth
; i
++)
2027 ksba_cert_release (array
[i
]);