1 /* certchain.c - certificate chain validation
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 * 2006 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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
33 #define JNLIB_NEED_LOG_LOGV /* We need log_logv. */
40 #include "../kbx/keybox.h" /* for KEYBOX_FLAG_* */
44 /* Object to keep track of certain root certificates. */
45 struct marktrusted_info_s
47 struct marktrusted_info_s
*next
;
48 unsigned char fpr
[20];
50 static struct marktrusted_info_s
*marktrusted_info
;
53 static int get_regtp_ca_info (ksba_cert_t cert
, int *chainlen
);
56 /* This function returns true if we already asked during this session
57 whether the root certificate CERT shall be marked as trusted. */
59 already_asked_marktrusted (ksba_cert_t cert
)
61 unsigned char fpr
[20];
62 struct marktrusted_info_s
*r
;
64 gpgsm_get_fingerprint (cert
, GCRY_MD_SHA1
, fpr
, NULL
);
65 /* No context switches in the loop! */
66 for (r
=marktrusted_info
; r
; r
= r
->next
)
67 if (!memcmp (r
->fpr
, fpr
, 20))
72 /* Flag certificate CERT as already asked whether it shall be marked
75 set_already_asked_marktrusted (ksba_cert_t cert
)
77 unsigned char fpr
[20];
78 struct marktrusted_info_s
*r
;
80 gpgsm_get_fingerprint (cert
, GCRY_MD_SHA1
, fpr
, NULL
);
81 for (r
=marktrusted_info
; r
; r
= r
->next
)
82 if (!memcmp (r
->fpr
, fpr
, 20))
83 return; /* Already marked. */
84 r
= xtrycalloc (1, sizeof *r
);
87 memcpy (r
->fpr
, fpr
, 20);
88 r
->next
= marktrusted_info
;
92 /* If LISTMODE is true, print FORMAT using LISTMODE to FP. If
93 LISTMODE is false, use the string to print an log_info or, if
94 IS_ERROR is true, and log_error. */
96 do_list (int is_error
, int listmode
, FILE *fp
, const char *format
, ...)
100 va_start (arg_ptr
, format
) ;
106 vfprintf (fp
, format
, arg_ptr
);
112 log_logv (is_error
? JNLIB_LOG_ERROR
: JNLIB_LOG_INFO
, format
, arg_ptr
);
118 /* Return 0 if A and B are equal. */
120 compare_certs (ksba_cert_t a
, ksba_cert_t b
)
122 const unsigned char *img_a
, *img_b
;
125 img_a
= ksba_cert_get_image (a
, &len_a
);
128 img_b
= ksba_cert_get_image (b
, &len_b
);
131 return !(len_a
== len_b
&& !memcmp (img_a
, img_b
, len_a
));
136 unknown_criticals (ksba_cert_t cert
, int listmode
, FILE *fp
)
138 static const char *known
[] = {
139 "2.5.29.15", /* keyUsage */
140 "2.5.29.19", /* basic Constraints */
141 "2.5.29.32", /* certificatePolicies */
142 "2.5.29.37", /* extendedKeyUsage - handled by certlist.c */
145 int rc
= 0, i
, idx
, crit
;
149 for (idx
=0; !(err
=ksba_cert_get_extension (cert
, idx
,
150 &oid
, &crit
, NULL
, NULL
));idx
++)
154 for (i
=0; known
[i
] && strcmp (known
[i
],oid
); i
++)
158 do_list (1, listmode
, fp
,
159 _("critical certificate extension %s is not supported"),
161 rc
= gpg_error (GPG_ERR_UNSUPPORTED_CERT
);
164 /* We ignore the error codes EOF as well as no-value. The later will
165 occur for certificates with no extensions at all. */
167 && gpg_err_code (err
) != GPG_ERR_EOF
168 && gpg_err_code (err
) != GPG_ERR_NO_VALUE
)
175 /* Check whether CERT is an allowed certificate. This requires that
176 CERT matches all requirements for such a CA, i.e. the
177 BasicConstraints extension. The function returns 0 on success and
178 the awlloed length of the chain at CHAINLEN. */
180 allowed_ca (ksba_cert_t cert
, int *chainlen
, int listmode
, FILE *fp
)
185 err
= ksba_cert_is_ca (cert
, &flag
, chainlen
);
190 if (get_regtp_ca_info (cert
, chainlen
))
192 /* Note that dirmngr takes a different way to cope with such
194 return 0; /* RegTP issued certificate. */
197 do_list (1, listmode
, fp
,_("issuer certificate is not marked as a CA"));
198 return gpg_error (GPG_ERR_BAD_CA_CERT
);
205 check_cert_policy (ksba_cert_t cert
, int listmode
, FILE *fplist
)
212 err
= ksba_cert_get_cert_policies (cert
, &policies
);
213 if (gpg_err_code (err
) == GPG_ERR_NO_DATA
214 || gpg_err_code (err
) == GPG_ERR_NO_VALUE
)
215 return 0; /* No policy given. */
219 /* STRING is a line delimited list of certifiate policies as stored
220 in the certificate. The line itself is colon delimited where the
221 first field is the OID of the policy and the second field either
222 N or C for normal or critical extension */
224 if (opt
.verbose
> 1 && !listmode
)
225 log_info ("certificate's policy list: %s\n", policies
);
227 /* The check is very minimal but won't give false positives */
228 any_critical
= !!strstr (policies
, ":C");
230 if (!opt
.policy_file
)
235 do_list (1, listmode
, fplist
,
236 _("critical marked policy without configured policies"));
237 return gpg_error (GPG_ERR_NO_POLICY_MATCH
);
242 fp
= fopen (opt
.policy_file
, "r");
245 if (opt
.verbose
|| errno
!= ENOENT
)
246 log_info (_("failed to open `%s': %s\n"),
247 opt
.policy_file
, strerror (errno
));
249 /* With no critical policies this is only a warning */
252 do_list (0, listmode
, fplist
,
253 _("note: non-critical certificate policy not allowed"));
256 do_list (1, listmode
, fplist
,
257 _("certificate policy not allowed"));
258 return gpg_error (GPG_ERR_NO_POLICY_MATCH
);
265 char *haystack
, *allowed
;
270 if (!fgets (line
, DIM(line
)-1, fp
) )
272 gpg_error_t tmperr
= gpg_error (gpg_err_code_from_errno (errno
));
278 /* With no critical policies this is only a warning */
281 do_list (0, listmode
, fplist
,
282 _("note: non-critical certificate policy not allowed"));
285 do_list (1, listmode
, fplist
,
286 _("certificate policy not allowed"));
287 return gpg_error (GPG_ERR_NO_POLICY_MATCH
);
293 if (!*line
|| line
[strlen(line
)-1] != '\n')
295 /* eat until end of line */
296 while ( (c
=getc (fp
)) != EOF
&& c
!= '\n')
300 return gpg_error (*line
? GPG_ERR_LINE_TOO_LONG
301 : GPG_ERR_INCOMPLETE_LINE
);
304 /* Allow for empty lines and spaces */
305 for (p
=line
; spacep (p
); p
++)
308 while (!*p
|| *p
== '\n' || *p
== '#');
311 for (allowed
=line
; spacep (allowed
); allowed
++)
313 p
= strpbrk (allowed
, " :\n");
314 if (!*p
|| p
== allowed
)
318 return gpg_error (GPG_ERR_CONFIGURATION
);
320 *p
= 0; /* strip the rest of the line */
321 /* See whether we find ALLOWED (which is an OID) in POLICIES */
322 for (haystack
=policies
; (p
=strstr (haystack
, allowed
)); haystack
= p
+1)
324 if ( !(p
== policies
|| p
[-1] == '\n') )
325 continue; /* Does not match the begin of a line. */
326 if (p
[strlen (allowed
)] != ':')
327 continue; /* The length does not match. */
328 /* Yep - it does match so return okay. */
337 /* Helper function for find_up. This resets the key handle and search
338 for an issuer ISSUER with a subjectKeyIdentifier of KEYID. Returns
339 0 obn success or -1 when not found. */
341 find_up_search_by_keyid (KEYDB_HANDLE kh
,
342 const char *issuer
, ksba_sexp_t keyid
)
345 ksba_cert_t cert
= NULL
;
346 ksba_sexp_t subj
= NULL
;
348 keydb_search_reset (kh
);
349 while (!(rc
= keydb_search_subject (kh
, issuer
)))
351 ksba_cert_release (cert
); cert
= NULL
;
352 rc
= keydb_get_cert (kh
, &cert
);
355 log_error ("keydb_get_cert() failed: rc=%d\n", rc
);
360 if (!ksba_cert_get_subj_key_id (cert
, NULL
, &subj
))
362 if (!cmp_simple_canon_sexp (keyid
, subj
))
363 break; /* Found matching cert. */
367 ksba_cert_release (cert
);
374 find_up_store_certs_cb (void *cb_value
, ksba_cert_t cert
)
376 if (keydb_store_cert (cert
, 1, NULL
))
377 log_error ("error storing issuer certificate as ephemeral\n");
382 /* Helper for find_up(). Locate the certificate for ISSUER using an
383 external lookup. KH is the keydb context we are currently using.
384 On success 0 is returned and the certificate may be retrieved from
385 the keydb using keydb_get_cert(). KEYID is the keyIdentifier from
388 find_up_external (KEYDB_HANDLE kh
, const char *issuer
, ksba_sexp_t keyid
)
391 strlist_t names
= NULL
;
397 log_info (_("looking up issuer at external location\n"));
398 /* The DIRMNGR process is confused about unknown attributes. As a
399 quick and ugly hack we locate the CN and use the issuer string
400 starting at this attribite. Fixme: we should have far better
401 parsing in the dirmngr. */
402 s
= strstr (issuer
, "CN=");
403 if (!s
|| s
== issuer
|| s
[-1] != ',')
406 pattern
= xtrymalloc (strlen (s
)+2);
408 return gpg_error_from_syserror ();
409 strcpy (stpcpy (pattern
, "/"), s
);
410 add_to_strlist (&names
, pattern
);
413 rc
= gpgsm_dirmngr_lookup (NULL
, names
, find_up_store_certs_cb
, &count
);
414 free_strlist (names
);
417 log_info (_("number of issuers matching: %d\n"), count
);
420 log_error ("external key lookup failed: %s\n", gpg_strerror (rc
));
428 /* The issuers are currently stored in the ephemeral key DB, so
429 we temporary switch to ephemeral mode. */
430 old
= keydb_set_ephemeral (kh
, 1);
432 rc
= find_up_search_by_keyid (kh
, issuer
, keyid
);
435 keydb_search_reset (kh
);
436 rc
= keydb_search_subject (kh
, issuer
);
438 keydb_set_ephemeral (kh
, old
);
444 /* Locate issuing certificate for CERT. ISSUER is the name of the
445 issuer used as a fallback if the other methods don't work. If
446 FIND_NEXT is true, the function shall return the next possible
447 issuer. The certificate itself is not directly returned but a
448 keydb_get_cert on the keyDb context KH will return it. Returns 0
449 on success, -1 if not found or an error code. */
451 find_up (KEYDB_HANDLE kh
, ksba_cert_t cert
, const char *issuer
, int find_next
)
454 ksba_sexp_t authidno
;
458 if (!ksba_cert_get_auth_key_id (cert
, &keyid
, &authid
, &authidno
))
460 const char *s
= ksba_name_enum (authid
, 0);
463 rc
= keydb_search_issuer_sn (kh
, s
, authidno
);
465 keydb_search_reset (kh
);
467 /* In case of an error try the ephemeral DB. We can't do
468 that in find_next mode because we can't keep the search
470 if (rc
== -1 && !find_next
)
472 int old
= keydb_set_ephemeral (kh
, 1);
475 rc
= keydb_search_issuer_sn (kh
, s
, authidno
);
477 keydb_search_reset (kh
);
479 keydb_set_ephemeral (kh
, old
);
484 if (rc
== -1 && keyid
&& !find_next
)
486 /* Not found by AIK.issuer_sn. Lets try the AIY.ki
487 instead. Loop over all certificates with that issuer as
488 subject and stop for the one with a matching
489 subjectKeyIdentifier. */
490 rc
= find_up_search_by_keyid (kh
, issuer
, keyid
);
493 int old
= keydb_set_ephemeral (kh
, 1);
495 rc
= find_up_search_by_keyid (kh
, issuer
, keyid
);
496 keydb_set_ephemeral (kh
, old
);
499 rc
= -1; /* Need to make sure to have this error code. */
502 /* If we still didn't found it, try an external lookup. */
503 if (rc
== -1 && opt
.auto_issuer_key_retrieve
&& !find_next
)
504 rc
= find_up_external (kh
, issuer
, keyid
);
506 /* Print a note so that the user does not feel too helpless when
507 an issuer certificate was found and gpgsm prints BAD
508 signature because it is not the correct one. */
511 log_info ("%sissuer certificate ", find_next
?"next ":"");
515 gpgsm_dump_serial (keyid
);
521 gpgsm_dump_serial (authidno
);
523 gpgsm_dump_string (s
);
526 log_printf ("not found using authorityKeyIdentifier\n");
529 log_error ("failed to find authorityKeyIdentifier: rc=%d\n", rc
);
531 ksba_name_release (authid
);
535 if (rc
) /* Not found via authorithyKeyIdentifier, try regular issuer name. */
536 rc
= keydb_search_subject (kh
, issuer
);
537 if (rc
== -1 && !find_next
)
539 /* Not found, lets see whether we have one in the ephemeral key DB. */
540 int old
= keydb_set_ephemeral (kh
, 1);
543 keydb_search_reset (kh
);
544 rc
= keydb_search_subject (kh
, issuer
);
546 keydb_set_ephemeral (kh
, old
);
549 /* Still not found. If enabled, try an external lookup. */
550 if (rc
== -1 && opt
.auto_issuer_key_retrieve
&& !find_next
)
551 rc
= find_up_external (kh
, issuer
, NULL
);
557 /* Return the next certificate up in the chain starting at START.
558 Returns -1 when there are no more certificates. */
560 gpgsm_walk_cert_chain (ksba_cert_t start
, ksba_cert_t
*r_next
)
564 char *subject
= NULL
;
565 KEYDB_HANDLE kh
= keydb_new (0);
570 log_error (_("failed to allocated keyDB handle\n"));
571 rc
= gpg_error (GPG_ERR_GENERAL
);
575 issuer
= ksba_cert_get_issuer (start
, 0);
576 subject
= ksba_cert_get_subject (start
, 0);
579 log_error ("no issuer found in certificate\n");
580 rc
= gpg_error (GPG_ERR_BAD_CERT
);
585 log_error ("no subject found in certificate\n");
586 rc
= gpg_error (GPG_ERR_BAD_CERT
);
590 if (!strcmp (issuer
, subject
))
592 rc
= -1; /* we are at the root */
596 rc
= find_up (kh
, start
, issuer
, 0);
599 /* it is quite common not to have a certificate, so better don't
600 print an error here */
601 if (rc
!= -1 && opt
.verbose
> 1)
602 log_error ("failed to find issuer's certificate: rc=%d\n", rc
);
603 rc
= gpg_error (GPG_ERR_MISSING_CERT
);
607 rc
= keydb_get_cert (kh
, r_next
);
610 log_error ("keydb_get_cert() failed: rc=%d\n", rc
);
611 rc
= gpg_error (GPG_ERR_GENERAL
);
622 /* Check whether the CERT is a root certificate. Returns True if this
625 gpgsm_is_root_cert (ksba_cert_t cert
)
631 issuer
= ksba_cert_get_issuer (cert
, 0);
632 subject
= ksba_cert_get_subject (cert
, 0);
633 yes
= (issuer
&& subject
&& !strcmp (issuer
, subject
));
640 /* This is a helper for gpgsm_validate_chain. */
642 is_cert_still_valid (ctrl_t ctrl
, int lm
, FILE *fp
,
643 ksba_cert_t subject_cert
, ksba_cert_t issuer_cert
,
644 int *any_revoked
, int *any_no_crl
, int *any_crl_too_old
)
646 if (!opt
.no_crl_check
|| ctrl
->use_ocsp
)
650 err
= gpgsm_dirmngr_isvalid (ctrl
,
651 subject_cert
, issuer_cert
, ctrl
->use_ocsp
);
654 /* Fixme: We should change the wording because we may
657 gpgsm_cert_log_name (NULL
, subject_cert
);
658 switch (gpg_err_code (err
))
660 case GPG_ERR_CERT_REVOKED
:
661 do_list (1, lm
, fp
, _("certificate has been revoked"));
663 /* Store that in the keybox so that key listings are
664 able to return the revoked flag. We don't care
665 about error, though. */
666 keydb_set_cert_flags (subject_cert
, KEYBOX_FLAG_VALIDITY
, 0,
669 case GPG_ERR_NO_CRL_KNOWN
:
670 do_list (1, lm
, fp
, _("no CRL found for certificate"));
673 case GPG_ERR_CRL_TOO_OLD
:
674 do_list (1, lm
, fp
, _("the available CRL is too old"));
676 log_info (_("please make sure that the "
677 "\"dirmngr\" is properly installed\n"));
678 *any_crl_too_old
= 1;
681 do_list (1, lm
, fp
, _("checking the CRL failed: %s"),
692 /* Validate a chain and optionally return the nearest expiration time
693 in R_EXPTIME. With LISTMODE set to 1 a special listmode is
694 activated where only information about the certificate is printed
695 to FP and no output is send to the usual log stream.
697 Defined flag bits: 0 - do not do any dirmngr isvalid checks.
700 gpgsm_validate_chain (ctrl_t ctrl
, ksba_cert_t cert
, ksba_isotime_t r_exptime
,
701 int listmode
, FILE *fp
, unsigned int flags
)
703 int rc
= 0, depth
= 0, maxdepth
;
705 char *subject
= NULL
;
706 KEYDB_HANDLE kh
= NULL
;
707 ksba_cert_t subject_cert
= NULL
, issuer_cert
= NULL
;
708 ksba_isotime_t current_time
;
709 ksba_isotime_t exptime
;
713 int any_crl_too_old
= 0;
714 int any_no_policy_match
= 0;
715 int is_qualified
= -1; /* Indicates whether the certificate stems
716 from a qualified root certificate.
717 -1 = unknown, 0 = no, 1 = yes. */
720 gnupg_get_isotime (current_time
);
725 if (opt
.no_chain_validation
&& !listmode
)
727 log_info ("WARNING: bypassing certificate chain validation\n");
734 log_error (_("failed to allocated keyDB handle\n"));
735 rc
= gpg_error (GPG_ERR_GENERAL
);
739 if (DBG_X509
&& !listmode
)
740 gpgsm_dump_cert ("target", cert
);
743 ksba_cert_ref (subject_cert
);
749 gpg_error_t istrusted_rc
= -1;
750 struct rootca_flags_s rootca_flags
;
754 issuer
= ksba_cert_get_issuer (subject_cert
, 0);
755 subject
= ksba_cert_get_subject (subject_cert
, 0);
759 do_list (1, lm
, fp
, _("no issuer found in certificate"));
760 rc
= gpg_error (GPG_ERR_BAD_CERT
);
764 /* Is this a self-issued certificate (i.e. the root certificate)? */
765 is_root
= (subject
&& !strcmp (issuer
, subject
));
768 /* Check early whether the certificate is listed as trusted.
769 We used to do this only later but changed it to call the
770 check right here so that we can access special flags
771 associated with that specific root certificate. */
772 istrusted_rc
= gpgsm_agent_istrusted (ctrl
, subject_cert
,
777 /* Check the validity period. */
779 ksba_isotime_t not_before
, not_after
;
781 rc
= ksba_cert_get_validity (subject_cert
, 0, not_before
);
783 rc
= ksba_cert_get_validity (subject_cert
, 1, not_after
);
786 do_list (1, lm
, fp
, _("certificate with invalid validity: %s"),
788 rc
= gpg_error (GPG_ERR_BAD_CERT
);
795 gnupg_copy_time (exptime
, not_after
);
796 else if (strcmp (not_after
, exptime
) < 0 )
797 gnupg_copy_time (exptime
, not_after
);
800 if (*not_before
&& strcmp (current_time
, not_before
) < 0 )
802 do_list (1, lm
, fp
, _("certificate not yet valid"));
805 log_info ("(valid from ");
806 gpgsm_dump_time (not_before
);
809 rc
= gpg_error (GPG_ERR_CERT_TOO_YOUNG
);
812 if (*not_after
&& strcmp (current_time
, not_after
) > 0 )
814 do_list (opt
.ignore_expiration
?0:1, lm
, fp
,
815 _("certificate has expired"));
818 log_info ("(expired at ");
819 gpgsm_dump_time (not_after
);
822 if (opt
.ignore_expiration
)
823 log_info ("WARNING: ignoring expiration\n");
829 /* Assert that we understand all critical extensions. */
830 rc
= unknown_criticals (subject_cert
, listmode
, fp
);
834 /* Do a policy check. */
835 if (!opt
.no_policy_check
)
837 rc
= check_cert_policy (subject_cert
, listmode
, fp
);
838 if (gpg_err_code (rc
) == GPG_ERR_NO_POLICY_MATCH
)
840 any_no_policy_match
= 1;
848 /* Is this a self-issued certificate? */
852 ; /* No need to check the certificate for a trusted one. */
853 else if (gpgsm_check_cert_sig (subject_cert
, subject_cert
) )
855 /* We only check the signature if the certificate is not
856 trusted for better diagnostics. */
858 _("self-signed certificate has a BAD signature"));
861 gpgsm_dump_cert ("self-signing cert", subject_cert
);
863 rc
= gpg_error (depth
? GPG_ERR_BAD_CERT_CHAIN
867 if (!rootca_flags
.relax
)
869 rc
= allowed_ca (subject_cert
, NULL
, listmode
, fp
);
875 /* Set the flag for qualified signatures. This flag is
876 deduced from a list of root certificates allowed for
877 qualified signatures. */
878 if (is_qualified
== -1)
884 if (!ksba_cert_get_user_data (cert
, "is_qualified",
888 /* We already checked this for this certificate,
889 thus we simply take it from the user data. */
890 is_qualified
= !!*buf
;
894 /* Need to consult the list of root certificates for
895 qualified signatures. */
896 err
= gpgsm_is_in_qualified_list (ctrl
, subject_cert
, NULL
);
899 else if ( gpg_err_code (err
) == GPG_ERR_NOT_FOUND
)
902 log_error ("checking the list of qualified "
903 "root certificates failed: %s\n",
905 if ( is_qualified
!= -1 )
907 /* Cache the result but don't care too much
909 buf
[0] = !!is_qualified
;
910 err
= ksba_cert_set_user_data (subject_cert
,
911 "is_qualified", buf
, 1);
913 log_error ("set_user_data(is_qualified) failed: %s\n",
920 /* Act on the check for a trusted root certificates. */
924 else if (gpg_err_code (rc
) == GPG_ERR_NOT_TRUSTED
)
926 do_list (0, lm
, fp
, _("root certificate is not marked trusted"));
927 /* If we already figured out that the certificate is
928 expired it does not make much sense to ask the user
929 whether we wants to trust the root certificate. He
930 should do this only if the certificate under question
931 will then be usable. We also check whether the agent
932 is at all enabled to allo marktrusted and don't call
933 it in this session again if it is not. */
935 && (!lm
|| !already_asked_marktrusted (subject_cert
)))
937 static int no_more_questions
; /* during this session. */
939 char *fpr
= gpgsm_get_fingerprint_string (subject_cert
,
941 log_info (_("fingerprint=%s\n"), fpr
? fpr
: "?");
943 if (no_more_questions
)
944 rc2
= gpg_error (GPG_ERR_NOT_SUPPORTED
);
946 rc2
= gpgsm_agent_marktrusted (ctrl
, subject_cert
);
949 log_info (_("root certificate has now"
950 " been marked as trusted\n"));
955 gpgsm_dump_cert ("issuer", subject_cert
);
956 log_info ("after checking the fingerprint, you may want "
957 "to add it manually to the list of trusted "
961 if (gpg_err_code (rc2
) == GPG_ERR_NOT_SUPPORTED
)
963 if (!no_more_questions
)
964 log_info (_("interactive marking as trusted "
965 "not enabled in gpg-agent\n"));
966 no_more_questions
= 1;
968 else if (gpg_err_code (rc2
) == GPG_ERR_CANCELED
)
970 log_info (_("interactive marking as trusted "
971 "disabled for this session\n"));
972 no_more_questions
= 1;
975 set_already_asked_marktrusted (subject_cert
);
980 log_error (_("checking the trust list failed: %s\n"),
987 /* Check for revocations etc. */
990 else if (opt
.no_trusted_cert_crl_check
|| rootca_flags
.relax
)
993 rc
= is_cert_still_valid (ctrl
, lm
, fp
,
994 subject_cert
, subject_cert
,
995 &any_revoked
, &any_no_crl
,
1000 break; /* Okay: a self-signed certicate is an end-point. */
1003 /* Take care that the chain does not get too long. */
1005 if (depth
> maxdepth
)
1007 do_list (1, lm
, fp
, _("certificate chain too long\n"));
1008 rc
= gpg_error (GPG_ERR_BAD_CERT_CHAIN
);
1012 /* Find the next cert up the tree. */
1013 keydb_search_reset (kh
);
1014 rc
= find_up (kh
, subject_cert
, issuer
, 0);
1019 do_list (0, lm
, fp
, _("issuer certificate not found"));
1022 log_info ("issuer certificate: #/");
1023 gpgsm_dump_string (issuer
);
1028 log_error ("failed to find issuer's certificate: rc=%d\n", rc
);
1029 rc
= gpg_error (GPG_ERR_MISSING_CERT
);
1033 ksba_cert_release (issuer_cert
); issuer_cert
= NULL
;
1034 rc
= keydb_get_cert (kh
, &issuer_cert
);
1037 log_error ("keydb_get_cert() failed: rc=%d\n", rc
);
1038 rc
= gpg_error (GPG_ERR_GENERAL
);
1045 log_debug ("got issuer's certificate:\n");
1046 gpgsm_dump_cert ("issuer", issuer_cert
);
1049 rc
= gpgsm_check_cert_sig (issuer_cert
, subject_cert
);
1052 do_list (0, lm
, fp
, _("certificate has a BAD signature"));
1055 gpgsm_dump_cert ("signing issuer", issuer_cert
);
1056 gpgsm_dump_cert ("signed subject", subject_cert
);
1058 if (gpg_err_code (rc
) == GPG_ERR_BAD_SIGNATURE
)
1060 /* We now try to find other issuer certificates which
1061 might have been used. This is required because some
1062 CAs are reusing the issuer and subject DN for new
1063 root certificates. */
1064 /* FIXME: Do this only if we don't have an
1065 AKI.keyIdentifier */
1066 rc
= find_up (kh
, subject_cert
, issuer
, 1);
1069 ksba_cert_t tmp_cert
;
1071 rc
= keydb_get_cert (kh
, &tmp_cert
);
1072 if (rc
|| !compare_certs (issuer_cert
, tmp_cert
))
1074 /* The find next did not work or returned an
1075 identical certificate. We better stop here
1076 to avoid infinite checks. */
1077 rc
= gpg_error (GPG_ERR_BAD_SIGNATURE
);
1078 ksba_cert_release (tmp_cert
);
1082 do_list (0, lm
, fp
, _("found another possible matching "
1083 "CA certificate - trying again"));
1084 ksba_cert_release (issuer_cert
);
1085 issuer_cert
= tmp_cert
;
1086 goto try_another_cert
;
1091 /* We give a more descriptive error code than the one
1092 returned from the signature checking. */
1093 rc
= gpg_error (GPG_ERR_BAD_CERT_CHAIN
);
1100 /* Check that a CA is allowed to issue certificates. */
1104 rc
= allowed_ca (issuer_cert
, &chainlen
, listmode
, fp
);
1107 /* Not allowed. Check whether this is a trusted root
1108 certificate and whether we allow special exceptions.
1109 We could carry the result of the test over to the
1110 regular root check at the top of the loop but for
1111 clarity we won't do that. Given that the majority of
1112 certificates carry proper BasicContraints our way of
1113 overriding an error in the way is justified for
1114 performance reasons. */
1115 if (gpgsm_is_root_cert (issuer_cert
))
1118 istrusted_rc
= gpgsm_agent_istrusted (ctrl
, issuer_cert
,
1120 if (!istrusted_rc
&& rootca_flags
.relax
)
1122 /* Ignore the error due to the relax flag. */
1130 if (chainlen
>= 0 && (depth
- 1) > chainlen
)
1133 _("certificate chain longer than allowed by CA (%d)"),
1135 rc
= gpg_error (GPG_ERR_BAD_CERT_CHAIN
);
1140 /* Is the certificate allowed to sign other certificates. */
1143 rc
= gpgsm_cert_use_cert_p (issuer_cert
);
1147 sprintf (numbuf
, "%d", rc
);
1148 gpgsm_status2 (ctrl
, STATUS_ERROR
, "certcert.issuer.keyusage",
1154 /* Check for revocations etc. Note that for a root certioficate
1155 this test is done a second time later. This should eventually
1159 else if (is_root
&& (opt
.no_trusted_cert_crl_check
1160 || (!istrusted_rc
&& rootca_flags
.relax
)))
1163 rc
= is_cert_still_valid (ctrl
, lm
, fp
,
1164 subject_cert
, issuer_cert
,
1165 &any_revoked
, &any_no_crl
, &any_crl_too_old
);
1170 if (opt
.verbose
&& !listmode
)
1171 log_info ("certificate is good\n");
1173 /* For the next round the current issuer becomes the new subject. */
1174 keydb_search_reset (kh
);
1175 ksba_cert_release (subject_cert
);
1176 subject_cert
= issuer_cert
;
1178 } /* End chain traversal. */
1182 if (opt
.no_policy_check
)
1183 log_info ("policies not checked due to %s option\n",
1184 "--disable-policy-checks");
1185 if (opt
.no_crl_check
&& !ctrl
->use_ocsp
)
1186 log_info ("CRLs not checked due to %s option\n",
1187 "--disable-crl-checks");
1191 { /* If we encountered an error somewhere during the checks, set
1192 the error code to the most critical one */
1194 rc
= gpg_error (GPG_ERR_CERT_REVOKED
);
1195 else if (any_expired
)
1196 rc
= gpg_error (GPG_ERR_CERT_EXPIRED
);
1197 else if (any_no_crl
)
1198 rc
= gpg_error (GPG_ERR_NO_CRL_KNOWN
);
1199 else if (any_crl_too_old
)
1200 rc
= gpg_error (GPG_ERR_CRL_TOO_OLD
);
1201 else if (any_no_policy_match
)
1202 rc
= gpg_error (GPG_ERR_NO_POLICY_MATCH
);
1206 if (is_qualified
!= -1)
1208 /* We figured something about the qualified signature capability
1209 of the certificate under question. Store the result as user
1210 data in the certificate object. We do this even if the
1211 validation itself failed. */
1212 /* Fixme: We should set this flag for all certificates in the
1213 chain for optimizing reasons. */
1217 buf
[0] = !!is_qualified
;
1218 err
= ksba_cert_set_user_data (cert
, "is_qualified", buf
, 1);
1221 log_error ("set_user_data(is_qualified) failed: %s\n",
1222 gpg_strerror (err
));
1228 gnupg_copy_time (r_exptime
, exptime
);
1232 ksba_cert_release (issuer_cert
);
1233 ksba_cert_release (subject_cert
);
1238 /* Check that the given certificate is valid but DO NOT check any
1239 constraints. We assume that the issuers certificate is already in
1240 the DB and that this one is valid; which it should be because it
1241 has been checked using this function. */
1243 gpgsm_basic_cert_check (ksba_cert_t cert
)
1246 char *issuer
= NULL
;
1247 char *subject
= NULL
;
1249 ksba_cert_t issuer_cert
= NULL
;
1251 if (opt
.no_chain_validation
)
1253 log_info ("WARNING: bypassing basic certificate checks\n");
1260 log_error (_("failed to allocated keyDB handle\n"));
1261 rc
= gpg_error (GPG_ERR_GENERAL
);
1265 issuer
= ksba_cert_get_issuer (cert
, 0);
1266 subject
= ksba_cert_get_subject (cert
, 0);
1269 log_error ("no issuer found in certificate\n");
1270 rc
= gpg_error (GPG_ERR_BAD_CERT
);
1274 if (subject
&& !strcmp (issuer
, subject
))
1276 rc
= gpgsm_check_cert_sig (cert
, cert
);
1279 log_error ("self-signed certificate has a BAD signature: %s\n",
1283 gpgsm_dump_cert ("self-signing cert", cert
);
1285 rc
= gpg_error (GPG_ERR_BAD_CERT
);
1291 /* Find the next cert up the tree. */
1292 keydb_search_reset (kh
);
1293 rc
= find_up (kh
, cert
, issuer
, 0);
1298 log_info ("issuer certificate (#/");
1299 gpgsm_dump_string (issuer
);
1300 log_printf (") not found\n");
1303 log_error ("failed to find issuer's certificate: rc=%d\n", rc
);
1304 rc
= gpg_error (GPG_ERR_MISSING_CERT
);
1308 ksba_cert_release (issuer_cert
); issuer_cert
= NULL
;
1309 rc
= keydb_get_cert (kh
, &issuer_cert
);
1312 log_error ("keydb_get_cert() failed: rc=%d\n", rc
);
1313 rc
= gpg_error (GPG_ERR_GENERAL
);
1317 rc
= gpgsm_check_cert_sig (issuer_cert
, cert
);
1320 log_error ("certificate has a BAD signature: %s\n",
1324 gpgsm_dump_cert ("signing issuer", issuer_cert
);
1325 gpgsm_dump_cert ("signed subject", cert
);
1327 rc
= gpg_error (GPG_ERR_BAD_CERT
);
1331 log_info ("certificate is good\n");
1337 ksba_cert_release (issuer_cert
);
1343 /* Check whether the certificate CERT has been issued by the German
1344 authority for qualified signature. They do not set the
1345 basicConstraints and thus we need this workaround. It works by
1346 looking up the root certificate and checking whether that one is
1347 listed as a qualified certificate for Germany.
1349 We also try to cache this data but as long as don't keep a
1350 reference to the certificate this won't be used.
1352 Returns: True if CERT is a RegTP issued CA cert (i.e. the root
1353 certificate itself or one of the CAs). In that case CHAINLEN will
1354 receive the length of the chain which is either 0 or 1.
1357 get_regtp_ca_info (ksba_cert_t cert
, int *chainlen
)
1364 ksba_cert_t array
[4];
1370 chainlen
= &dummy_chainlen
;
1373 err
= ksba_cert_get_user_data (cert
, "regtp_ca_chainlen",
1374 &buf
, sizeof (buf
), &buflen
);
1378 if (buflen
< 2 || !*buf
)
1379 return 0; /* Nothing found. */
1381 return 1; /* This is a regtp CA. */
1383 else if (gpg_err_code (err
) != GPG_ERR_NOT_FOUND
)
1385 log_error ("ksba_cert_get_user_data(%s) failed: %s\n",
1386 "regtp_ca_chainlen", gpg_strerror (err
));
1387 return 0; /* Nothing found. */
1390 /* Need to gather the info. This requires to walk up the chain
1391 until we have found the root. Because we are only interested in
1392 German Bundesnetzagentur (former RegTP) derived certificates 3
1393 levels are enough. (The German signature law demands a 3 tier
1394 hierachy; thus there is only one CA between the EE and the Root
1396 memset (&array
, 0, sizeof array
);
1399 ksba_cert_ref (cert
);
1400 array
[depth
++] = cert
;
1401 ksba_cert_ref (cert
);
1402 while (depth
< DIM(array
) && !(rc
=gpgsm_walk_cert_chain (cert
, &next
)))
1404 ksba_cert_release (cert
);
1405 ksba_cert_ref (next
);
1406 array
[depth
++] = next
;
1409 ksba_cert_release (cert
);
1410 if (rc
!= -1 || !depth
|| depth
== DIM(array
) )
1412 /* We did not reached the root. */
1416 /* If this is a German signature law issued certificate, we store
1417 additional additional information. */
1418 if (!gpgsm_is_in_qualified_list (NULL
, array
[depth
-1], country
)
1419 && !strcmp (country
, "de"))
1421 /* Setting the pathlen for the root CA and the CA flag for the
1422 next one is all what we need to do. */
1423 err
= ksba_cert_set_user_data (array
[depth
-1], "regtp_ca_chainlen",
1425 if (!err
&& depth
> 1)
1426 err
= ksba_cert_set_user_data (array
[depth
-2], "regtp_ca_chainlen",
1429 log_error ("ksba_set_user_data(%s) failed: %s\n",
1430 "regtp_ca_chainlen", gpg_strerror (err
));
1431 for (i
=0; i
< depth
; i
++)
1432 ksba_cert_release (array
[i
]);
1433 *chainlen
= (depth
>1? 0:1);
1438 /* Nothing special with this certificate. Mark the target
1439 certificate anyway to avoid duplicate lookups. */
1440 err
= ksba_cert_set_user_data (cert
, "regtp_ca_chainlen", "", 1);
1442 log_error ("ksba_set_user_data(%s) failed: %s\n",
1443 "regtp_ca_chainlen", gpg_strerror (err
));
1444 for (i
=0; i
< depth
; i
++)
1445 ksba_cert_release (array
[i
]);