Line data Source code
1 : /* validate.c - Validate a certificate chain.
2 : * Copyright (C) 2001, 2003, 2004, 2008 Free Software Foundation, Inc.
3 : * Copyright (C) 2004, 2006, 2008 g10 Code GmbH
4 : *
5 : * This file is part of DirMngr.
6 : *
7 : * DirMngr 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.
11 : *
12 : * DirMngr 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, write to the Free Software
19 : * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 : */
21 :
22 : #include <config.h>
23 :
24 : #include <stdio.h>
25 : #include <stdlib.h>
26 : #include <errno.h>
27 : #include <assert.h>
28 : #include <ctype.h>
29 :
30 : #include "dirmngr.h"
31 : #include "certcache.h"
32 : #include "crlcache.h"
33 : #include "validate.h"
34 : #include "misc.h"
35 :
36 : /* While running the validation function we need to keep track of the
37 : certificates and the validation outcome of each. We use this type
38 : for it. */
39 : struct chain_item_s
40 : {
41 : struct chain_item_s *next;
42 : ksba_cert_t cert; /* The certificate. */
43 : unsigned char fpr[20]; /* Fingerprint of the certificate. */
44 : int is_self_signed; /* This certificate is self-signed. */
45 : int is_valid; /* The certifiate is valid except for revocations. */
46 : };
47 : typedef struct chain_item_s *chain_item_t;
48 :
49 :
50 : /* A couple of constants with Object Identifiers. */
51 : static const char oid_kp_serverAuth[] = "1.3.6.1.5.5.7.3.1";
52 : static const char oid_kp_clientAuth[] = "1.3.6.1.5.5.7.3.2";
53 : static const char oid_kp_codeSigning[] = "1.3.6.1.5.5.7.3.3";
54 : static const char oid_kp_emailProtection[]= "1.3.6.1.5.5.7.3.4";
55 : static const char oid_kp_timeStamping[] = "1.3.6.1.5.5.7.3.8";
56 : static const char oid_kp_ocspSigning[] = "1.3.6.1.5.5.7.3.9";
57 :
58 :
59 : /* Prototypes. */
60 : static gpg_error_t check_cert_sig (ksba_cert_t issuer_cert, ksba_cert_t cert);
61 :
62 :
63 :
64 :
65 : /* Check whether CERT contains critical extensions we don't know
66 : about. */
67 : static gpg_error_t
68 0 : unknown_criticals (ksba_cert_t cert)
69 : {
70 : static const char *known[] = {
71 : "2.5.29.15", /* keyUsage */
72 : "2.5.29.19", /* basic Constraints */
73 : "2.5.29.32", /* certificatePolicies */
74 : "2.5.29.37", /* extendedKeyUsage */
75 : NULL
76 : };
77 : int i, idx, crit;
78 : const char *oid;
79 : int unsupported;
80 : strlist_t sl;
81 : gpg_error_t err, rc;
82 :
83 0 : rc = 0;
84 0 : for (idx=0; !(err=ksba_cert_get_extension (cert, idx,
85 0 : &oid, &crit, NULL, NULL));idx++)
86 : {
87 0 : if (!crit)
88 0 : continue;
89 0 : for (i=0; known[i] && strcmp (known[i],oid); i++)
90 : ;
91 0 : unsupported = !known[i];
92 :
93 : /* If this critical extension is not supported, check the list
94 : of to be ignored extensions to see whether we claim that it
95 : is supported. */
96 0 : if (unsupported && opt.ignored_cert_extensions)
97 : {
98 0 : for (sl=opt.ignored_cert_extensions;
99 0 : sl && strcmp (sl->d, oid); sl = sl->next)
100 : ;
101 0 : if (sl)
102 0 : unsupported = 0;
103 : }
104 :
105 0 : if (unsupported)
106 : {
107 0 : log_error (_("critical certificate extension %s is not supported"),
108 : oid);
109 0 : rc = gpg_error (GPG_ERR_UNSUPPORTED_CERT);
110 : }
111 : }
112 0 : if (err && gpg_err_code (err) != GPG_ERR_EOF)
113 0 : rc = err; /* Such an error takes precendence. */
114 :
115 0 : return rc;
116 : }
117 :
118 :
119 : /* Basic check for supported policies. */
120 : static gpg_error_t
121 0 : check_cert_policy (ksba_cert_t cert)
122 : {
123 : static const char *allowed[] = {
124 : "2.289.9.9",
125 : NULL
126 : };
127 : gpg_error_t err;
128 : int idx;
129 : char *p, *haystack;
130 : char *policies;
131 : int any_critical;
132 :
133 0 : err = ksba_cert_get_cert_policies (cert, &policies);
134 0 : if (gpg_err_code (err) == GPG_ERR_NO_DATA)
135 0 : return 0; /* No policy given. */
136 0 : if (err)
137 0 : return err;
138 :
139 : /* STRING is a line delimited list of certifiate policies as stored
140 : in the certificate. The line itself is colon delimited where the
141 : first field is the OID of the policy and the second field either
142 : N or C for normal or critical extension */
143 0 : if (opt.verbose > 1)
144 0 : log_info ("certificate's policy list: %s\n", policies);
145 :
146 : /* The check is very minimal but won't give false positives */
147 0 : any_critical = !!strstr (policies, ":C");
148 :
149 : /* See whether we find ALLOWED (which is an OID) in POLICIES */
150 0 : for (idx=0; allowed[idx]; idx++)
151 : {
152 0 : for (haystack=policies; (p=strstr (haystack, allowed[idx]));
153 0 : haystack = p+1)
154 : {
155 0 : if ( !(p == policies || p[-1] == '\n') )
156 0 : continue; /* Does not match the begin of a line. */
157 0 : if (p[strlen (allowed[idx])] != ':')
158 0 : continue; /* The length does not match. */
159 : /* Yep - it does match: Return okay. */
160 0 : ksba_free (policies);
161 0 : return 0;
162 : }
163 : }
164 :
165 0 : if (!any_critical)
166 : {
167 0 : log_info (_("Note: non-critical certificate policy not allowed"));
168 0 : err = 0;
169 : }
170 : else
171 : {
172 0 : log_info (_("certificate policy not allowed"));
173 0 : err = gpg_error (GPG_ERR_NO_POLICY_MATCH);
174 : }
175 :
176 0 : ksba_free (policies);
177 0 : return err;
178 : }
179 :
180 :
181 : static gpg_error_t
182 0 : allowed_ca (ksba_cert_t cert, int *chainlen)
183 : {
184 : gpg_error_t err;
185 : int flag;
186 :
187 0 : err = ksba_cert_is_ca (cert, &flag, chainlen);
188 0 : if (err)
189 0 : return err;
190 0 : if (!flag)
191 : {
192 0 : if (!is_trusted_cert (cert))
193 : {
194 : /* The German SigG Root CA's certificate does not flag
195 : itself as a CA; thus we relax this requirement if we
196 : trust a root CA. I think this is reasonable. Note, that
197 : gpgsm implements a far stricter scheme here. */
198 0 : if (chainlen)
199 0 : *chainlen = 3; /* That is what the SigG implements. */
200 0 : if (opt.verbose)
201 0 : log_info (_("accepting root CA not marked as a CA"));
202 : }
203 : else
204 : {
205 0 : log_error (_("issuer certificate is not marked as a CA"));
206 0 : return gpg_error (GPG_ERR_BAD_CA_CERT);
207 : }
208 : }
209 0 : return 0;
210 : }
211 :
212 : /* Helper for validate_cert_chain. */
213 : static gpg_error_t
214 0 : check_revocations (ctrl_t ctrl, chain_item_t chain)
215 : {
216 0 : gpg_error_t err = 0;
217 0 : int any_revoked = 0;
218 0 : int any_no_crl = 0;
219 0 : int any_crl_too_old = 0;
220 : chain_item_t ci;
221 :
222 0 : assert (ctrl->check_revocations_nest_level >= 0);
223 0 : assert (chain);
224 :
225 0 : if (ctrl->check_revocations_nest_level > 10)
226 : {
227 0 : log_error (_("CRL checking too deeply nested\n"));
228 0 : return gpg_error(GPG_ERR_BAD_CERT_CHAIN);
229 : }
230 0 : ctrl->check_revocations_nest_level++;
231 :
232 :
233 0 : for (ci=chain; ci; ci = ci->next)
234 : {
235 0 : assert (ci->cert);
236 0 : if (ci == chain)
237 : {
238 : /* It does not make sense to check the root certificate for
239 : revocations. In almost all cases this will lead to a
240 : catch-22 as the root certificate is the final trust
241 : anchor for the certificates and the CRLs. We expect the
242 : user to remove root certificates from the list of trusted
243 : certificates in case they have been revoked. */
244 0 : if (opt.verbose)
245 0 : cert_log_name (_("not checking CRL for"), ci->cert);
246 0 : continue;
247 : }
248 :
249 0 : if (opt.verbose)
250 0 : cert_log_name (_("checking CRL for"), ci->cert);
251 0 : err = crl_cache_cert_isvalid (ctrl, ci->cert, 0);
252 0 : if (gpg_err_code (err) == GPG_ERR_NO_CRL_KNOWN)
253 : {
254 0 : err = crl_cache_reload_crl (ctrl, ci->cert);
255 0 : if (!err)
256 0 : err = crl_cache_cert_isvalid (ctrl, ci->cert, 0);
257 : }
258 0 : switch (gpg_err_code (err))
259 : {
260 0 : case 0: err = 0; break;
261 0 : case GPG_ERR_CERT_REVOKED: any_revoked = 1; err = 0; break;
262 0 : case GPG_ERR_NO_CRL_KNOWN: any_no_crl = 1; err = 0; break;
263 0 : case GPG_ERR_CRL_TOO_OLD: any_crl_too_old = 1; err = 0; break;
264 0 : default: break;
265 : }
266 : }
267 0 : ctrl->check_revocations_nest_level--;
268 :
269 :
270 0 : if (err)
271 : ;
272 0 : else if (any_revoked)
273 0 : err = gpg_error (GPG_ERR_CERT_REVOKED);
274 0 : else if (any_no_crl)
275 0 : err = gpg_error (GPG_ERR_NO_CRL_KNOWN);
276 0 : else if (any_crl_too_old)
277 0 : err = gpg_error (GPG_ERR_CRL_TOO_OLD);
278 : else
279 0 : err = 0;
280 0 : return err;
281 : }
282 :
283 :
284 : /* Check whether CERT is a root certificate. ISSUERDN and SUBJECTDN
285 : are the DNs already extracted by the caller from CERT. Returns
286 : True if this is the case. */
287 : static int
288 0 : is_root_cert (ksba_cert_t cert, const char *issuerdn, const char *subjectdn)
289 : {
290 : gpg_error_t err;
291 0 : int result = 0;
292 : ksba_sexp_t serialno;
293 : ksba_sexp_t ak_keyid;
294 : ksba_name_t ak_name;
295 : ksba_sexp_t ak_sn;
296 : const char *ak_name_str;
297 0 : ksba_sexp_t subj_keyid = NULL;
298 :
299 0 : if (!issuerdn || !subjectdn)
300 0 : return 0; /* No. */
301 :
302 0 : if (strcmp (issuerdn, subjectdn))
303 0 : return 0; /* No. */
304 :
305 0 : err = ksba_cert_get_auth_key_id (cert, &ak_keyid, &ak_name, &ak_sn);
306 0 : if (err)
307 : {
308 0 : if (gpg_err_code (err) == GPG_ERR_NO_DATA)
309 0 : return 1; /* Yes. Without a authorityKeyIdentifier this needs
310 : to be the Root certifcate (our trust anchor). */
311 0 : log_error ("error getting authorityKeyIdentifier: %s\n",
312 : gpg_strerror (err));
313 0 : return 0; /* Well, it is broken anyway. Return No. */
314 : }
315 :
316 0 : serialno = ksba_cert_get_serial (cert);
317 0 : if (!serialno)
318 : {
319 0 : log_error ("error getting serialno: %s\n", gpg_strerror (err));
320 0 : goto leave;
321 : }
322 :
323 : /* Check whether the auth name's matches the issuer name+sn. If
324 : that is the case this is a root certificate. */
325 0 : ak_name_str = ksba_name_enum (ak_name, 0);
326 0 : if (ak_name_str
327 0 : && !strcmp (ak_name_str, issuerdn)
328 0 : && !cmp_simple_canon_sexp (ak_sn, serialno))
329 : {
330 0 : result = 1; /* Right, CERT is self-signed. */
331 0 : goto leave;
332 : }
333 :
334 : /* Similar for the ak_keyid. */
335 0 : if (ak_keyid && !ksba_cert_get_subj_key_id (cert, NULL, &subj_keyid)
336 0 : && !cmp_simple_canon_sexp (ak_keyid, subj_keyid))
337 : {
338 0 : result = 1; /* Right, CERT is self-signed. */
339 0 : goto leave;
340 : }
341 :
342 :
343 : leave:
344 0 : ksba_free (subj_keyid);
345 0 : ksba_free (ak_keyid);
346 0 : ksba_name_release (ak_name);
347 0 : ksba_free (ak_sn);
348 0 : ksba_free (serialno);
349 0 : return result;
350 : }
351 :
352 :
353 : /* Validate the certificate CHAIN up to the trust anchor. Optionally
354 : return the closest expiration time in R_EXPTIME (this is useful for
355 : caching issues). MODE is one of the VALIDATE_MODE_* constants.
356 :
357 : If R_TRUST_ANCHOR is not NULL and the validation would fail only
358 : because the root certificate is not trusted, the hexified
359 : fingerprint of that root certificate is stored at R_TRUST_ANCHOR
360 : and success is returned. The caller needs to free the value at
361 : R_TRUST_ANCHOR; in all other cases NULL is stored there. */
362 : gpg_error_t
363 0 : validate_cert_chain (ctrl_t ctrl, ksba_cert_t cert, ksba_isotime_t r_exptime,
364 : int mode, char **r_trust_anchor)
365 : {
366 0 : gpg_error_t err = 0;
367 : int depth, maxdepth;
368 0 : char *issuer = NULL;
369 0 : char *subject = NULL;
370 0 : ksba_cert_t subject_cert = NULL, issuer_cert = NULL;
371 : ksba_isotime_t current_time;
372 : ksba_isotime_t exptime;
373 0 : int any_expired = 0;
374 0 : int any_no_policy_match = 0;
375 : chain_item_t chain;
376 :
377 :
378 0 : if (r_exptime)
379 0 : *r_exptime = 0;
380 0 : *exptime = 0;
381 :
382 0 : if (r_trust_anchor)
383 0 : *r_trust_anchor = NULL;
384 :
385 0 : if (!opt.system_daemon)
386 : {
387 : /* For backward compatibility we only do this in daemon mode. */
388 0 : log_info (_("running in compatibility mode - "
389 : "certificate chain not checked!\n"));
390 0 : return 0; /* Okay. */
391 : }
392 :
393 0 : if (DBG_X509)
394 0 : dump_cert ("subject", cert);
395 :
396 : /* May the target certificate be used for this purpose? */
397 0 : switch (mode)
398 : {
399 : case VALIDATE_MODE_OCSP:
400 0 : err = cert_use_ocsp_p (cert);
401 0 : break;
402 : case VALIDATE_MODE_CRL:
403 : case VALIDATE_MODE_CRL_RECURSIVE:
404 0 : err = cert_use_crl_p (cert);
405 0 : break;
406 : default:
407 0 : err = 0;
408 0 : break;
409 : }
410 0 : if (err)
411 0 : return err;
412 :
413 : /* If we already validated the certificate not too long ago, we can
414 : avoid the excessive computations and lookups unless the caller
415 : asked for the expiration time. */
416 0 : if (!r_exptime)
417 : {
418 : size_t buflen;
419 : time_t validated_at;
420 :
421 0 : err = ksba_cert_get_user_data (cert, "validated_at",
422 : &validated_at, sizeof (validated_at),
423 : &buflen);
424 0 : if (err || buflen != sizeof (validated_at) || !validated_at)
425 0 : err = 0; /* Not available or other error. */
426 : else
427 : {
428 : /* If the validation is not older than 30 minutes we are ready. */
429 0 : if (validated_at < gnupg_get_time () + (30*60))
430 : {
431 0 : if (opt.verbose)
432 0 : log_info ("certificate is good (cached)\n");
433 : /* Note, that we can't jump to leave here as this would
434 : falsely updated the validation timestamp. */
435 0 : return 0;
436 : }
437 : }
438 : }
439 :
440 : /* Get the current time. */
441 0 : gnupg_get_isotime (current_time);
442 :
443 : /* We walk up the chain until we find a trust anchor. */
444 0 : subject_cert = cert;
445 0 : maxdepth = 10;
446 0 : chain = NULL;
447 0 : depth = 0;
448 : for (;;)
449 : {
450 : /* Get the subject and issuer name from the current
451 : certificate. */
452 0 : ksba_free (issuer);
453 0 : ksba_free (subject);
454 0 : issuer = ksba_cert_get_issuer (subject_cert, 0);
455 0 : subject = ksba_cert_get_subject (subject_cert, 0);
456 :
457 0 : if (!issuer)
458 : {
459 0 : log_error (_("no issuer found in certificate\n"));
460 0 : err = gpg_error (GPG_ERR_BAD_CERT);
461 0 : goto leave;
462 : }
463 :
464 : /* Handle the notBefore and notAfter timestamps. */
465 : {
466 : ksba_isotime_t not_before, not_after;
467 :
468 0 : err = ksba_cert_get_validity (subject_cert, 0, not_before);
469 0 : if (!err)
470 0 : err = ksba_cert_get_validity (subject_cert, 1, not_after);
471 0 : if (err)
472 : {
473 0 : log_error (_("certificate with invalid validity: %s"),
474 : gpg_strerror (err));
475 0 : err = gpg_error (GPG_ERR_BAD_CERT);
476 0 : goto leave;
477 : }
478 :
479 : /* Keep track of the nearest expiration time in EXPTIME. */
480 0 : if (*not_after)
481 : {
482 0 : if (!*exptime)
483 0 : gnupg_copy_time (exptime, not_after);
484 0 : else if (strcmp (not_after, exptime) < 0 )
485 0 : gnupg_copy_time (exptime, not_after);
486 : }
487 :
488 : /* Check whether the certificate is already valid. */
489 0 : if (*not_before && strcmp (current_time, not_before) < 0 )
490 : {
491 0 : log_error (_("certificate not yet valid"));
492 0 : log_info ("(valid from ");
493 0 : dump_isotime (not_before);
494 0 : log_printf (")\n");
495 0 : err = gpg_error (GPG_ERR_CERT_TOO_YOUNG);
496 0 : goto leave;
497 : }
498 :
499 : /* Now check whether the certificate has expired. */
500 0 : if (*not_after && strcmp (current_time, not_after) > 0 )
501 : {
502 0 : log_error (_("certificate has expired"));
503 0 : log_info ("(expired at ");
504 0 : dump_isotime (not_after);
505 0 : log_printf (")\n");
506 0 : any_expired = 1;
507 : }
508 : }
509 :
510 : /* Do we have any critical extensions in the certificate we
511 : can't handle? */
512 0 : err = unknown_criticals (subject_cert);
513 0 : if (err)
514 0 : goto leave; /* yes. */
515 :
516 : /* Check that given policies are allowed. */
517 0 : err = check_cert_policy (subject_cert);
518 0 : if (gpg_err_code (err) == GPG_ERR_NO_POLICY_MATCH)
519 : {
520 0 : any_no_policy_match = 1;
521 0 : err = 0;
522 : }
523 0 : else if (err)
524 0 : goto leave;
525 :
526 : /* Is this a self-signed certificate? */
527 0 : if (is_root_cert ( subject_cert, issuer, subject))
528 : {
529 : /* Yes, this is our trust anchor. */
530 0 : if (check_cert_sig (subject_cert, subject_cert) )
531 : {
532 0 : log_error (_("selfsigned certificate has a BAD signature"));
533 0 : err = gpg_error (depth? GPG_ERR_BAD_CERT_CHAIN
534 : : GPG_ERR_BAD_CERT);
535 0 : goto leave;
536 : }
537 :
538 : /* Is this certificate allowed to act as a CA. */
539 0 : err = allowed_ca (subject_cert, NULL);
540 0 : if (err)
541 0 : goto leave; /* No. */
542 :
543 0 : err = is_trusted_cert (subject_cert);
544 0 : if (!err)
545 : ; /* Yes we trust this cert. */
546 0 : else if (gpg_err_code (err) == GPG_ERR_NOT_TRUSTED)
547 : {
548 : char *fpr;
549 :
550 0 : log_error (_("root certificate is not marked trusted"));
551 0 : fpr = get_fingerprint_hexstring (subject_cert);
552 0 : log_info (_("fingerprint=%s\n"), fpr? fpr : "?");
553 0 : dump_cert ("issuer", subject_cert);
554 0 : if (r_trust_anchor)
555 : {
556 : /* Caller wants to do another trustiness check. */
557 0 : *r_trust_anchor = fpr;
558 0 : err = 0;
559 : }
560 : else
561 0 : xfree (fpr);
562 : }
563 : else
564 : {
565 0 : log_error (_("checking trustworthiness of "
566 : "root certificate failed: %s\n"),
567 : gpg_strerror (err));
568 : }
569 0 : if (err)
570 0 : goto leave;
571 :
572 : /* Prepend the certificate to our list. */
573 : {
574 : chain_item_t ci;
575 :
576 0 : ci = xtrycalloc (1, sizeof *ci);
577 0 : if (!ci)
578 : {
579 0 : err = gpg_error_from_errno (errno);
580 0 : goto leave;
581 : }
582 0 : ksba_cert_ref (subject_cert);
583 0 : ci->cert = subject_cert;
584 0 : cert_compute_fpr (subject_cert, ci->fpr);
585 0 : ci->next = chain;
586 0 : chain = ci;
587 : }
588 :
589 0 : if (opt.verbose)
590 : {
591 0 : if (r_trust_anchor && *r_trust_anchor)
592 0 : log_info ("root certificate is good but not trusted\n");
593 : else
594 0 : log_info ("root certificate is good and trusted\n");
595 : }
596 :
597 0 : break; /* Okay: a self-signed certicate is an end-point. */
598 : }
599 :
600 : /* To avoid loops, we use an arbitary limit on the length of
601 : the chain. */
602 0 : depth++;
603 0 : if (depth > maxdepth)
604 : {
605 0 : log_error (_("certificate chain too long\n"));
606 0 : err = gpg_error (GPG_ERR_BAD_CERT_CHAIN);
607 0 : goto leave;
608 : }
609 :
610 : /* Find the next cert up the tree. */
611 0 : ksba_cert_release (issuer_cert); issuer_cert = NULL;
612 0 : err = find_issuing_cert (ctrl, subject_cert, &issuer_cert);
613 0 : if (err)
614 : {
615 0 : if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
616 : {
617 0 : log_error (_("issuer certificate not found"));
618 0 : log_info ("issuer certificate: #/");
619 0 : dump_string (issuer);
620 0 : log_printf ("\n");
621 : }
622 : else
623 0 : log_error (_("issuer certificate not found: %s\n"),
624 : gpg_strerror (err));
625 : /* Use a better understandable error code. */
626 0 : err = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
627 0 : goto leave;
628 : }
629 :
630 : /* try_another_cert: */
631 0 : if (DBG_X509)
632 : {
633 0 : log_debug ("got issuer's certificate:\n");
634 0 : dump_cert ("issuer", issuer_cert);
635 : }
636 :
637 : /* Now check the signature of the certificate. Well, we
638 : should delay this until later so that faked certificates
639 : can't be turned into a DoS easily. */
640 0 : err = check_cert_sig (issuer_cert, subject_cert);
641 0 : if (err)
642 : {
643 0 : log_error (_("certificate has a BAD signature"));
644 : #if 0
645 : if (gpg_err_code (err) == GPG_ERR_BAD_SIGNATURE)
646 : {
647 : /* We now try to find other issuer certificates which
648 : might have been used. This is required because some
649 : CAs are reusing the issuer and subject DN for new
650 : root certificates without using a authorityKeyIdentifier. */
651 : rc = find_up (kh, subject_cert, issuer, 1);
652 : if (!rc)
653 : {
654 : ksba_cert_t tmp_cert;
655 :
656 : rc = keydb_get_cert (kh, &tmp_cert);
657 : if (rc || !compare_certs (issuer_cert, tmp_cert))
658 : {
659 : /* The find next did not work or returned an
660 : identical certificate. We better stop here
661 : to avoid infinite checks. */
662 : rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
663 : ksba_cert_release (tmp_cert);
664 : }
665 : else
666 : {
667 : do_list (0, lm, fp, _("found another possible matching "
668 : "CA certificate - trying again"));
669 : ksba_cert_release (issuer_cert);
670 : issuer_cert = tmp_cert;
671 : goto try_another_cert;
672 : }
673 : }
674 : }
675 : #endif
676 : /* We give a more descriptive error code than the one
677 : returned from the signature checking. */
678 0 : err = gpg_error (GPG_ERR_BAD_CERT_CHAIN);
679 0 : goto leave;
680 : }
681 :
682 : /* Check that the length of the chain is not longer than allowed
683 : by the CA. */
684 : {
685 : int chainlen;
686 :
687 0 : err = allowed_ca (issuer_cert, &chainlen);
688 0 : if (err)
689 0 : goto leave;
690 0 : if (chainlen >= 0 && (depth - 1) > chainlen)
691 : {
692 0 : log_error (_("certificate chain longer than allowed by CA (%d)"),
693 : chainlen);
694 0 : err = gpg_error (GPG_ERR_BAD_CERT_CHAIN);
695 0 : goto leave;
696 : }
697 : }
698 :
699 : /* May that certificate be used for certification? */
700 0 : err = cert_use_cert_p (issuer_cert);
701 0 : if (err)
702 0 : goto leave; /* No. */
703 :
704 : /* Prepend the certificate to our list. */
705 : {
706 : chain_item_t ci;
707 :
708 0 : ci = xtrycalloc (1, sizeof *ci);
709 0 : if (!ci)
710 : {
711 0 : err = gpg_error_from_errno (errno);
712 0 : goto leave;
713 : }
714 0 : ksba_cert_ref (subject_cert);
715 0 : ci->cert = subject_cert;
716 0 : cert_compute_fpr (subject_cert, ci->fpr);
717 0 : ci->next = chain;
718 0 : chain = ci;
719 : }
720 :
721 0 : if (opt.verbose)
722 0 : log_info (_("certificate is good\n"));
723 :
724 : /* Now to the next level up. */
725 0 : subject_cert = issuer_cert;
726 0 : issuer_cert = NULL;
727 0 : }
728 :
729 0 : if (!err)
730 : { /* If we encountered an error somewhere during the checks, set
731 : the error code to the most critical one */
732 0 : if (any_expired)
733 0 : err = gpg_error (GPG_ERR_CERT_EXPIRED);
734 0 : else if (any_no_policy_match)
735 0 : err = gpg_error (GPG_ERR_NO_POLICY_MATCH);
736 : }
737 :
738 0 : if (!err && opt.verbose)
739 : {
740 : chain_item_t citem;
741 :
742 0 : log_info (_("certificate chain is good\n"));
743 0 : for (citem = chain; citem; citem = citem->next)
744 0 : cert_log_name (" certificate", citem->cert);
745 : }
746 :
747 0 : if (!err && mode != VALIDATE_MODE_CRL)
748 : { /* Now that everything is fine, walk the chain and check each
749 : certificate for revocations.
750 :
751 : 1. item in the chain - The root certificate.
752 : 2. item - the CA below the root
753 : last item - the target certificate.
754 :
755 : Now for each certificate in the chain check whether it has
756 : been included in a CRL and thus be revoked. We don't do OCSP
757 : here because this does not seem to make much sense. This
758 : might become a recursive process and we should better cache
759 : our validity results to avoid double work. Far worse a
760 : catch-22 may happen for an improper setup hierachy and we
761 : need a way to break up such a deadlock. */
762 0 : err = check_revocations (ctrl, chain);
763 : }
764 :
765 0 : if (!err && opt.verbose)
766 : {
767 0 : if (r_trust_anchor && *r_trust_anchor)
768 0 : log_info ("target certificate may be valid\n");
769 : else
770 0 : log_info ("target certificate is valid\n");
771 : }
772 0 : else if (err && opt.verbose)
773 0 : log_info ("target certificate is NOT valid\n");
774 :
775 :
776 : leave:
777 0 : if (!err && !(r_trust_anchor && *r_trust_anchor))
778 : {
779 : /* With no error we can update the validation cache. We do this
780 : for all certificates in the chain. Note that we can't use
781 : the cache if the caller requested to check the trustiness of
782 : the root certificate himself. Adding such a feature would
783 : require us to also store the fingerprint of root
784 : certificate. */
785 : chain_item_t citem;
786 0 : time_t validated_at = gnupg_get_time ();
787 :
788 0 : for (citem = chain; citem; citem = citem->next)
789 : {
790 0 : err = ksba_cert_set_user_data (citem->cert, "validated_at",
791 : &validated_at, sizeof (validated_at));
792 0 : if (err)
793 : {
794 0 : log_error ("set_user_data(validated_at) failed: %s\n",
795 : gpg_strerror (err));
796 0 : err = 0;
797 : }
798 : }
799 : }
800 :
801 0 : if (r_exptime)
802 0 : gnupg_copy_time (r_exptime, exptime);
803 0 : ksba_free (issuer);
804 0 : ksba_free (subject);
805 0 : ksba_cert_release (issuer_cert);
806 0 : if (subject_cert != cert)
807 0 : ksba_cert_release (subject_cert);
808 0 : while (chain)
809 : {
810 0 : chain_item_t ci_next = chain->next;
811 0 : if (chain->cert)
812 0 : ksba_cert_release (chain->cert);
813 0 : xfree (chain);
814 0 : chain = ci_next;
815 : }
816 0 : if (err && r_trust_anchor && *r_trust_anchor)
817 : {
818 0 : xfree (*r_trust_anchor);
819 0 : *r_trust_anchor = NULL;
820 : }
821 0 : return err;
822 : }
823 :
824 :
825 :
826 : /* Return the public key algorithm id from the S-expression PKEY.
827 : FIXME: libgcrypt should provide such a function. Note that this
828 : implementation uses the names as used by libksba. */
829 : static int
830 0 : pk_algo_from_sexp (gcry_sexp_t pkey)
831 : {
832 : gcry_sexp_t l1, l2;
833 : const char *name;
834 : size_t n;
835 : int algo;
836 :
837 0 : l1 = gcry_sexp_find_token (pkey, "public-key", 0);
838 0 : if (!l1)
839 0 : return 0; /* Not found. */
840 0 : l2 = gcry_sexp_cadr (l1);
841 0 : gcry_sexp_release (l1);
842 :
843 0 : name = gcry_sexp_nth_data (l2, 0, &n);
844 0 : if (!name)
845 0 : algo = 0; /* Not found. */
846 0 : else if (n==3 && !memcmp (name, "rsa", 3))
847 0 : algo = GCRY_PK_RSA;
848 0 : else if (n==3 && !memcmp (name, "dsa", 3))
849 0 : algo = GCRY_PK_DSA;
850 0 : else if (n==13 && !memcmp (name, "ambiguous-rsa", 13))
851 0 : algo = GCRY_PK_RSA;
852 : else
853 0 : algo = 0;
854 0 : gcry_sexp_release (l2);
855 0 : return algo;
856 : }
857 :
858 :
859 : /* Check the signature on CERT using the ISSUER_CERT. This function
860 : does only test the cryptographic signature and nothing else. It is
861 : assumed that the ISSUER_CERT is valid. */
862 : static gpg_error_t
863 0 : check_cert_sig (ksba_cert_t issuer_cert, ksba_cert_t cert)
864 : {
865 : gpg_error_t err;
866 : const char *algoid;
867 : gcry_md_hd_t md;
868 : int i, algo;
869 : ksba_sexp_t p;
870 : size_t n;
871 : gcry_sexp_t s_sig, s_hash, s_pkey;
872 : const char *s;
873 : char algo_name[16+1]; /* hash algorithm name converted to lower case. */
874 : int digestlen;
875 : unsigned char *digest;
876 :
877 : /* Hash the target certificate using the algorithm from that certificate. */
878 0 : algoid = ksba_cert_get_digest_algo (cert);
879 0 : algo = gcry_md_map_name (algoid);
880 0 : if (!algo)
881 : {
882 0 : log_error (_("unknown hash algorithm '%s'\n"), algoid? algoid:"?");
883 0 : return gpg_error (GPG_ERR_GENERAL);
884 : }
885 0 : s = gcry_md_algo_name (algo);
886 0 : for (i=0; *s && i < sizeof algo_name - 1; s++, i++)
887 0 : algo_name[i] = tolower (*s);
888 0 : algo_name[i] = 0;
889 :
890 0 : err = gcry_md_open (&md, algo, 0);
891 0 : if (err)
892 : {
893 0 : log_error ("md_open failed: %s\n", gpg_strerror (err));
894 0 : return err;
895 : }
896 0 : if (DBG_HASHING)
897 0 : gcry_md_debug (md, "hash.cert");
898 :
899 0 : err = ksba_cert_hash (cert, 1, HASH_FNC, md);
900 0 : if (err)
901 : {
902 0 : log_error ("ksba_cert_hash failed: %s\n", gpg_strerror (err));
903 0 : gcry_md_close (md);
904 0 : return err;
905 : }
906 0 : gcry_md_final (md);
907 :
908 : /* Get the signature value out of the target certificate. */
909 0 : p = ksba_cert_get_sig_val (cert);
910 0 : n = gcry_sexp_canon_len (p, 0, NULL, NULL);
911 0 : if (!n)
912 : {
913 0 : log_error ("libksba did not return a proper S-Exp\n");
914 0 : gcry_md_close (md);
915 0 : ksba_free (p);
916 0 : return gpg_error (GPG_ERR_BUG);
917 : }
918 0 : if (DBG_CRYPTO)
919 : {
920 : int j;
921 0 : log_debug ("signature value:");
922 0 : for (j=0; j < n; j++)
923 0 : log_printf (" %02X", p[j]);
924 0 : log_printf ("\n");
925 : }
926 :
927 0 : err = gcry_sexp_sscan ( &s_sig, NULL, p, n);
928 0 : ksba_free (p);
929 0 : if (err)
930 : {
931 0 : log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (err));
932 0 : gcry_md_close (md);
933 0 : return err;
934 : }
935 :
936 : /* Get the public key from the issuer certificate. */
937 0 : p = ksba_cert_get_public_key (issuer_cert);
938 0 : n = gcry_sexp_canon_len (p, 0, NULL, NULL);
939 0 : if (!n)
940 : {
941 0 : log_error ("libksba did not return a proper S-Exp\n");
942 0 : gcry_md_close (md);
943 0 : ksba_free (p);
944 0 : gcry_sexp_release (s_sig);
945 0 : return gpg_error (GPG_ERR_BUG);
946 : }
947 0 : err = gcry_sexp_sscan ( &s_pkey, NULL, p, n);
948 0 : ksba_free (p);
949 0 : if (err)
950 : {
951 0 : log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (err));
952 0 : gcry_md_close (md);
953 0 : gcry_sexp_release (s_sig);
954 0 : return err;
955 : }
956 :
957 :
958 : /* Prepare the values for signature verification. At this point we
959 : have these values:
960 :
961 : S_PKEY - S-expression with the issuer's public key.
962 : S_SIG - Signature value as given in the certrificate.
963 : MD - Finalized hash context with hash of the certificate.
964 : ALGO_NAME - Lowercase hash algorithm name
965 : */
966 0 : digestlen = gcry_md_get_algo_dlen (algo);
967 0 : digest = gcry_md_read (md, algo);
968 0 : if (pk_algo_from_sexp (s_pkey) == GCRY_PK_DSA)
969 : {
970 0 : if (digestlen != 20)
971 : {
972 0 : log_error (_("DSA requires the use of a 160 bit hash algorithm\n"));
973 0 : gcry_md_close (md);
974 0 : gcry_sexp_release (s_sig);
975 0 : gcry_sexp_release (s_pkey);
976 0 : return gpg_error (GPG_ERR_INTERNAL);
977 : }
978 0 : if ( gcry_sexp_build (&s_hash, NULL, "(data(flags raw)(value %b))",
979 : (int)digestlen, digest) )
980 0 : BUG ();
981 : }
982 : else /* Not DSA. */
983 : {
984 0 : if ( gcry_sexp_build (&s_hash, NULL, "(data(flags pkcs1)(hash %s %b))",
985 : algo_name, (int)digestlen, digest) )
986 0 : BUG ();
987 :
988 : }
989 :
990 0 : err = gcry_pk_verify (s_sig, s_hash, s_pkey);
991 0 : if (DBG_X509)
992 0 : log_debug ("gcry_pk_verify: %s\n", gpg_strerror (err));
993 0 : gcry_md_close (md);
994 0 : gcry_sexp_release (s_sig);
995 0 : gcry_sexp_release (s_hash);
996 0 : gcry_sexp_release (s_pkey);
997 0 : return err;
998 : }
999 :
1000 :
1001 :
1002 : /* Return 0 if the cert is usable for encryption. A MODE of 0 checks
1003 : for signing, a MODE of 1 checks for encryption, a MODE of 2 checks
1004 : for verification and a MODE of 3 for decryption (just for
1005 : debugging). MODE 4 is for certificate signing, MODE 5 for OCSP
1006 : response signing, MODE 6 is for CRL signing. */
1007 : static int
1008 0 : cert_usage_p (ksba_cert_t cert, int mode)
1009 : {
1010 : gpg_error_t err;
1011 : unsigned int use;
1012 : char *extkeyusages;
1013 0 : int have_ocsp_signing = 0;
1014 :
1015 0 : err = ksba_cert_get_ext_key_usages (cert, &extkeyusages);
1016 0 : if (gpg_err_code (err) == GPG_ERR_NO_DATA)
1017 0 : err = 0; /* No policy given. */
1018 0 : if (!err)
1019 : {
1020 0 : unsigned int extusemask = ~0; /* Allow all. */
1021 :
1022 0 : if (extkeyusages)
1023 : {
1024 : char *p, *pend;
1025 0 : int any_critical = 0;
1026 :
1027 0 : extusemask = 0;
1028 :
1029 0 : p = extkeyusages;
1030 0 : while (p && (pend=strchr (p, ':')))
1031 : {
1032 0 : *pend++ = 0;
1033 : /* Only care about critical flagged usages. */
1034 0 : if ( *pend == 'C' )
1035 : {
1036 0 : any_critical = 1;
1037 0 : if ( !strcmp (p, oid_kp_serverAuth))
1038 0 : extusemask |= (KSBA_KEYUSAGE_DIGITAL_SIGNATURE
1039 : | KSBA_KEYUSAGE_KEY_ENCIPHERMENT
1040 : | KSBA_KEYUSAGE_KEY_AGREEMENT);
1041 0 : else if ( !strcmp (p, oid_kp_clientAuth))
1042 0 : extusemask |= (KSBA_KEYUSAGE_DIGITAL_SIGNATURE
1043 : | KSBA_KEYUSAGE_KEY_AGREEMENT);
1044 0 : else if ( !strcmp (p, oid_kp_codeSigning))
1045 0 : extusemask |= (KSBA_KEYUSAGE_DIGITAL_SIGNATURE);
1046 0 : else if ( !strcmp (p, oid_kp_emailProtection))
1047 0 : extusemask |= (KSBA_KEYUSAGE_DIGITAL_SIGNATURE
1048 : | KSBA_KEYUSAGE_NON_REPUDIATION
1049 : | KSBA_KEYUSAGE_KEY_ENCIPHERMENT
1050 : | KSBA_KEYUSAGE_KEY_AGREEMENT);
1051 0 : else if ( !strcmp (p, oid_kp_timeStamping))
1052 0 : extusemask |= (KSBA_KEYUSAGE_DIGITAL_SIGNATURE
1053 : | KSBA_KEYUSAGE_NON_REPUDIATION);
1054 : }
1055 :
1056 : /* This is a hack to cope with OCSP. Note that we do
1057 : not yet fully comply with the requirements and that
1058 : the entire CRL/OCSP checking thing should undergo a
1059 : thorough review and probably redesign. */
1060 0 : if ( !strcmp (p, oid_kp_ocspSigning))
1061 0 : have_ocsp_signing = 1;
1062 :
1063 0 : if ((p = strchr (pend, '\n')))
1064 0 : p++;
1065 : }
1066 0 : ksba_free (extkeyusages);
1067 0 : extkeyusages = NULL;
1068 :
1069 0 : if (!any_critical)
1070 0 : extusemask = ~0; /* Reset to the don't care mask. */
1071 : }
1072 :
1073 :
1074 0 : err = ksba_cert_get_key_usage (cert, &use);
1075 0 : if (gpg_err_code (err) == GPG_ERR_NO_DATA)
1076 : {
1077 0 : err = 0;
1078 0 : if (opt.verbose && mode < 2)
1079 0 : log_info (_("no key usage specified - assuming all usages\n"));
1080 0 : use = ~0;
1081 : }
1082 :
1083 : /* Apply extKeyUsage. */
1084 0 : use &= extusemask;
1085 :
1086 : }
1087 0 : if (err)
1088 : {
1089 0 : log_error (_("error getting key usage information: %s\n"),
1090 : gpg_strerror (err));
1091 0 : ksba_free (extkeyusages);
1092 0 : return err;
1093 : }
1094 :
1095 0 : if (mode == 4)
1096 : {
1097 0 : if ((use & (KSBA_KEYUSAGE_KEY_CERT_SIGN)))
1098 0 : return 0;
1099 0 : log_info (_("certificate should not have "
1100 : "been used for certification\n"));
1101 0 : return gpg_error (GPG_ERR_WRONG_KEY_USAGE);
1102 : }
1103 :
1104 0 : if (mode == 5)
1105 : {
1106 0 : if (use != ~0
1107 0 : && (have_ocsp_signing
1108 0 : || (use & (KSBA_KEYUSAGE_KEY_CERT_SIGN
1109 : |KSBA_KEYUSAGE_CRL_SIGN))))
1110 0 : return 0;
1111 0 : log_info (_("certificate should not have "
1112 : "been used for OCSP response signing\n"));
1113 0 : return gpg_error (GPG_ERR_WRONG_KEY_USAGE);
1114 : }
1115 :
1116 0 : if (mode == 6)
1117 : {
1118 0 : if ((use & (KSBA_KEYUSAGE_CRL_SIGN)))
1119 0 : return 0;
1120 0 : log_info (_("certificate should not have "
1121 : "been used for CRL signing\n"));
1122 0 : return gpg_error (GPG_ERR_WRONG_KEY_USAGE);
1123 : }
1124 :
1125 0 : if ((use & ((mode&1)?
1126 : (KSBA_KEYUSAGE_KEY_ENCIPHERMENT|KSBA_KEYUSAGE_DATA_ENCIPHERMENT):
1127 : (KSBA_KEYUSAGE_DIGITAL_SIGNATURE|KSBA_KEYUSAGE_NON_REPUDIATION)))
1128 : )
1129 0 : return 0;
1130 :
1131 0 : log_info (mode==3? _("certificate should not have been used "
1132 : "for encryption\n"):
1133 : mode==2? _("certificate should not have been used for signing\n"):
1134 : mode==1? _("certificate is not usable for encryption\n"):
1135 : _("certificate is not usable for signing\n"));
1136 0 : return gpg_error (GPG_ERR_WRONG_KEY_USAGE);
1137 : }
1138 :
1139 : /* Return 0 if the certificate CERT is usable for certification. */
1140 : gpg_error_t
1141 0 : cert_use_cert_p (ksba_cert_t cert)
1142 : {
1143 0 : return cert_usage_p (cert, 4);
1144 : }
1145 :
1146 : /* Return 0 if the certificate CERT is usable for signing OCSP
1147 : responses. */
1148 : gpg_error_t
1149 0 : cert_use_ocsp_p (ksba_cert_t cert)
1150 : {
1151 0 : return cert_usage_p (cert, 5);
1152 : }
1153 :
1154 : /* Return 0 if the certificate CERT is usable for signing CRLs. */
1155 : gpg_error_t
1156 0 : cert_use_crl_p (ksba_cert_t cert)
1157 : {
1158 0 : return cert_usage_p (cert, 6);
1159 : }
|