2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
15 #include "internal/ctype.h"
16 #include "internal/cryptlib.h"
17 #include <openssl/crypto.h>
18 #include <openssl/lhash.h>
19 #include <openssl/buffer.h>
20 #include <openssl/evp.h>
21 #include <openssl/asn1.h>
22 #include <openssl/x509.h>
23 #include <openssl/x509v3.h>
24 #include <openssl/objects.h>
25 #include <internal/dane.h>
26 #include <internal/x509_int.h>
29 /* CRL score values */
31 /* No unhandled critical extensions */
33 #define CRL_SCORE_NOCRITICAL 0x100
35 /* certificate is within CRL scope */
37 #define CRL_SCORE_SCOPE 0x080
41 #define CRL_SCORE_TIME 0x040
43 /* Issuer name matches certificate */
45 #define CRL_SCORE_ISSUER_NAME 0x020
47 /* If this score or above CRL is probably valid */
49 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
51 /* CRL issuer is certificate issuer */
53 #define CRL_SCORE_ISSUER_CERT 0x018
55 /* CRL issuer is on certificate path */
57 #define CRL_SCORE_SAME_PATH 0x008
59 /* CRL issuer matches CRL AKID */
61 #define CRL_SCORE_AKID 0x004
63 /* Have a delta CRL with valid times */
65 #define CRL_SCORE_TIME_DELTA 0x002
67 static int build_chain(X509_STORE_CTX
*ctx
);
68 static int verify_chain(X509_STORE_CTX
*ctx
);
69 static int dane_verify(X509_STORE_CTX
*ctx
);
70 static int null_callback(int ok
, X509_STORE_CTX
*e
);
71 static int check_issued(X509_STORE_CTX
*ctx
, X509
*x
, X509
*issuer
);
72 static X509
*find_issuer(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
, X509
*x
);
73 static int check_chain_extensions(X509_STORE_CTX
*ctx
);
74 static int check_name_constraints(X509_STORE_CTX
*ctx
);
75 static int check_id(X509_STORE_CTX
*ctx
);
76 static int check_trust(X509_STORE_CTX
*ctx
, int num_untrusted
);
77 static int check_revocation(X509_STORE_CTX
*ctx
);
78 static int check_cert(X509_STORE_CTX
*ctx
);
79 static int check_policy(X509_STORE_CTX
*ctx
);
80 static int get_issuer_sk(X509
**issuer
, X509_STORE_CTX
*ctx
, X509
*x
);
81 static int check_dane_issuer(X509_STORE_CTX
*ctx
, int depth
);
82 static int check_key_level(X509_STORE_CTX
*ctx
, X509
*cert
);
83 static int check_sig_level(X509_STORE_CTX
*ctx
, X509
*cert
);
85 static int get_crl_score(X509_STORE_CTX
*ctx
, X509
**pissuer
,
86 unsigned int *preasons
, X509_CRL
*crl
, X509
*x
);
87 static int get_crl_delta(X509_STORE_CTX
*ctx
,
88 X509_CRL
**pcrl
, X509_CRL
**pdcrl
, X509
*x
);
89 static void get_delta_sk(X509_STORE_CTX
*ctx
, X509_CRL
**dcrl
,
90 int *pcrl_score
, X509_CRL
*base
,
91 STACK_OF(X509_CRL
) *crls
);
92 static void crl_akid_check(X509_STORE_CTX
*ctx
, X509_CRL
*crl
, X509
**pissuer
,
94 static int crl_crldp_check(X509
*x
, X509_CRL
*crl
, int crl_score
,
95 unsigned int *preasons
);
96 static int check_crl_path(X509_STORE_CTX
*ctx
, X509
*x
);
97 static int check_crl_chain(X509_STORE_CTX
*ctx
,
98 STACK_OF(X509
) *cert_path
,
99 STACK_OF(X509
) *crl_path
);
101 static int internal_verify(X509_STORE_CTX
*ctx
);
103 static int null_callback(int ok
, X509_STORE_CTX
*e
)
108 /* Return 1 is a certificate is self signed */
109 static int cert_self_signed(X509
*x
)
112 * FIXME: x509v3_cache_extensions() needs to detect more failures and not
113 * set EXFLAG_SET when that happens. Especially, if the failures are
114 * parse errors, rather than memory pressure!
116 X509_check_purpose(x
, -1, 0);
117 if (x
->ex_flags
& EXFLAG_SS
)
123 /* Given a certificate try and find an exact match in the store */
125 static X509
*lookup_cert_match(X509_STORE_CTX
*ctx
, X509
*x
)
127 STACK_OF(X509
) *certs
;
130 /* Lookup all certs with matching subject name */
131 certs
= ctx
->lookup_certs(ctx
, X509_get_subject_name(x
));
134 /* Look for exact match */
135 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
136 xtmp
= sk_X509_value(certs
, i
);
137 if (!X509_cmp(xtmp
, x
))
140 if (i
< sk_X509_num(certs
))
144 sk_X509_pop_free(certs
, X509_free
);
149 * Inform the verify callback of an error.
150 * If B<x> is not NULL it is the error cert, otherwise use the chain cert at
152 * If B<err> is not X509_V_OK, that's the error value, otherwise leave
153 * unchanged (presumably set by the caller).
155 * Returns 0 to abort verification with an error, non-zero to continue.
157 static int verify_cb_cert(X509_STORE_CTX
*ctx
, X509
*x
, int depth
, int err
)
159 ctx
->error_depth
= depth
;
160 ctx
->current_cert
= (x
!= NULL
) ? x
: sk_X509_value(ctx
->chain
, depth
);
161 if (err
!= X509_V_OK
)
163 return ctx
->verify_cb(0, ctx
);
167 * Inform the verify callback of an error, CRL-specific variant. Here, the
168 * error depth and certificate are already set, we just specify the error
171 * Returns 0 to abort verification with an error, non-zero to continue.
173 static int verify_cb_crl(X509_STORE_CTX
*ctx
, int err
)
176 return ctx
->verify_cb(0, ctx
);
179 static int check_auth_level(X509_STORE_CTX
*ctx
)
182 int num
= sk_X509_num(ctx
->chain
);
184 if (ctx
->param
->auth_level
<= 0)
187 for (i
= 0; i
< num
; ++i
) {
188 X509
*cert
= sk_X509_value(ctx
->chain
, i
);
191 * We've already checked the security of the leaf key, so here we only
192 * check the security of issuer keys.
194 if (i
> 0 && !check_key_level(ctx
, cert
) &&
195 verify_cb_cert(ctx
, cert
, i
, X509_V_ERR_CA_KEY_TOO_SMALL
) == 0)
198 * We also check the signature algorithm security of all certificates
199 * except those of the trust anchor at index num-1.
201 if (i
< num
- 1 && !check_sig_level(ctx
, cert
) &&
202 verify_cb_cert(ctx
, cert
, i
, X509_V_ERR_CA_MD_TOO_WEAK
) == 0)
208 static int verify_chain(X509_STORE_CTX
*ctx
)
214 * Before either returning with an error, or continuing with CRL checks,
215 * instantiate chain public key parameters.
217 if ((ok
= build_chain(ctx
)) == 0 ||
218 (ok
= check_chain_extensions(ctx
)) == 0 ||
219 (ok
= check_auth_level(ctx
)) == 0 ||
220 (ok
= check_name_constraints(ctx
)) == 0 ||
221 (ok
= check_id(ctx
)) == 0 || 1)
222 X509_get_pubkey_parameters(NULL
, ctx
->chain
);
223 if (ok
== 0 || (ok
= ctx
->check_revocation(ctx
)) == 0)
226 err
= X509_chain_check_suiteb(&ctx
->error_depth
, NULL
, ctx
->chain
,
228 if (err
!= X509_V_OK
) {
229 if ((ok
= verify_cb_cert(ctx
, NULL
, ctx
->error_depth
, err
)) == 0)
233 /* Verify chain signatures and expiration times */
234 ok
= (ctx
->verify
!= NULL
) ? ctx
->verify(ctx
) : internal_verify(ctx
);
238 #ifndef OPENSSL_NO_RFC3779
239 /* RFC 3779 path validation, now that CRL check has been done */
240 if ((ok
= X509v3_asid_validate_path(ctx
)) == 0)
242 if ((ok
= X509v3_addr_validate_path(ctx
)) == 0)
246 /* If we get this far evaluate policies */
247 if (ctx
->param
->flags
& X509_V_FLAG_POLICY_CHECK
)
248 ok
= ctx
->check_policy(ctx
);
252 int X509_verify_cert(X509_STORE_CTX
*ctx
)
254 SSL_DANE
*dane
= ctx
->dane
;
257 if (ctx
->cert
== NULL
) {
258 X509err(X509_F_X509_VERIFY_CERT
, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY
);
259 ctx
->error
= X509_V_ERR_INVALID_CALL
;
263 if (ctx
->chain
!= NULL
) {
265 * This X509_STORE_CTX has already been used to verify a cert. We
266 * cannot do another one.
268 X509err(X509_F_X509_VERIFY_CERT
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
269 ctx
->error
= X509_V_ERR_INVALID_CALL
;
274 * first we make sure the chain we are going to build is present and that
275 * the first entry is in place
277 if (((ctx
->chain
= sk_X509_new_null()) == NULL
) ||
278 (!sk_X509_push(ctx
->chain
, ctx
->cert
))) {
279 X509err(X509_F_X509_VERIFY_CERT
, ERR_R_MALLOC_FAILURE
);
280 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
283 X509_up_ref(ctx
->cert
);
284 ctx
->num_untrusted
= 1;
286 /* If the peer's public key is too weak, we can stop early. */
287 if (!check_key_level(ctx
, ctx
->cert
) &&
288 !verify_cb_cert(ctx
, ctx
->cert
, 0, X509_V_ERR_EE_KEY_TOO_SMALL
))
291 if (DANETLS_ENABLED(dane
))
292 ret
= dane_verify(ctx
);
294 ret
= verify_chain(ctx
);
297 * Safety-net. If we are returning an error, we must also set ctx->error,
298 * so that the chain is not considered verified should the error be ignored
299 * (e.g. TLS with SSL_VERIFY_NONE).
301 if (ret
<= 0 && ctx
->error
== X509_V_OK
)
302 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
307 * Given a STACK_OF(X509) find the issuer of cert (if any)
309 static X509
*find_issuer(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
, X509
*x
)
312 X509
*issuer
, *rv
= NULL
;
314 for (i
= 0; i
< sk_X509_num(sk
); i
++) {
315 issuer
= sk_X509_value(sk
, i
);
316 if (ctx
->check_issued(ctx
, x
, issuer
)) {
318 if (x509_check_cert_time(ctx
, rv
, -1))
325 /* Given a possible certificate and issuer check them */
327 static int check_issued(X509_STORE_CTX
*ctx
, X509
*x
, X509
*issuer
)
331 return cert_self_signed(x
);
332 ret
= X509_check_issued(issuer
, x
);
333 if (ret
== X509_V_OK
) {
336 /* Special case: single self signed certificate */
337 if (cert_self_signed(x
) && sk_X509_num(ctx
->chain
) == 1)
339 for (i
= 0; i
< sk_X509_num(ctx
->chain
); i
++) {
340 ch
= sk_X509_value(ctx
->chain
, i
);
341 if (ch
== issuer
|| !X509_cmp(ch
, issuer
)) {
342 ret
= X509_V_ERR_PATH_LOOP
;
348 return (ret
== X509_V_OK
);
351 /* Alternative lookup method: look from a STACK stored in other_ctx */
353 static int get_issuer_sk(X509
**issuer
, X509_STORE_CTX
*ctx
, X509
*x
)
355 *issuer
= find_issuer(ctx
, ctx
->other_ctx
, x
);
357 X509_up_ref(*issuer
);
363 static STACK_OF(X509
) *lookup_certs_sk(X509_STORE_CTX
*ctx
, X509_NAME
*nm
)
365 STACK_OF(X509
) *sk
= NULL
;
368 for (i
= 0; i
< sk_X509_num(ctx
->other_ctx
); i
++) {
369 x
= sk_X509_value(ctx
->other_ctx
, i
);
370 if (X509_NAME_cmp(nm
, X509_get_subject_name(x
)) == 0) {
372 sk
= sk_X509_new_null();
373 if (sk
== NULL
|| sk_X509_push(sk
, x
) == 0) {
374 sk_X509_pop_free(sk
, X509_free
);
384 * Check EE or CA certificate purpose. For trusted certificates explicit local
385 * auxiliary trust can be used to override EKU-restrictions.
387 static int check_purpose(X509_STORE_CTX
*ctx
, X509
*x
, int purpose
, int depth
,
390 int tr_ok
= X509_TRUST_UNTRUSTED
;
393 * For trusted certificates we want to see whether any auxiliary trust
394 * settings trump the purpose constraints.
396 * This is complicated by the fact that the trust ordinals in
397 * ctx->param->trust are entirely independent of the purpose ordinals in
398 * ctx->param->purpose!
400 * What connects them is their mutual initialization via calls from
401 * X509_STORE_CTX_set_default() into X509_VERIFY_PARAM_lookup() which sets
402 * related values of both param->trust and param->purpose. It is however
403 * typically possible to infer associated trust values from a purpose value
404 * via the X509_PURPOSE API.
406 * Therefore, we can only check for trust overrides when the purpose we're
407 * checking is the same as ctx->param->purpose and ctx->param->trust is
410 if (depth
>= ctx
->num_untrusted
&& purpose
== ctx
->param
->purpose
)
411 tr_ok
= X509_check_trust(x
, ctx
->param
->trust
, X509_TRUST_NO_SS_COMPAT
);
414 case X509_TRUST_TRUSTED
:
416 case X509_TRUST_REJECTED
:
419 switch (X509_check_purpose(x
, purpose
, must_be_ca
> 0)) {
425 if ((ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
) == 0)
431 return verify_cb_cert(ctx
, x
, depth
, X509_V_ERR_INVALID_PURPOSE
);
435 * Check a certificate chains extensions for consistency with the supplied
439 static int check_chain_extensions(X509_STORE_CTX
*ctx
)
441 int i
, must_be_ca
, plen
= 0;
443 int proxy_path_length
= 0;
445 int allow_proxy_certs
;
446 int num
= sk_X509_num(ctx
->chain
);
449 * must_be_ca can have 1 of 3 values:
450 * -1: we accept both CA and non-CA certificates, to allow direct
451 * use of self-signed certificates (which are marked as CA).
452 * 0: we only accept non-CA certificates. This is currently not
453 * used, but the possibility is present for future extensions.
454 * 1: we only accept CA certificates. This is currently used for
455 * all certificates in the chain except the leaf certificate.
459 /* CRL path validation */
461 allow_proxy_certs
= 0;
462 purpose
= X509_PURPOSE_CRL_SIGN
;
465 ! !(ctx
->param
->flags
& X509_V_FLAG_ALLOW_PROXY_CERTS
);
466 purpose
= ctx
->param
->purpose
;
469 for (i
= 0; i
< num
; i
++) {
471 x
= sk_X509_value(ctx
->chain
, i
);
472 if (!(ctx
->param
->flags
& X509_V_FLAG_IGNORE_CRITICAL
)
473 && (x
->ex_flags
& EXFLAG_CRITICAL
)) {
474 if (!verify_cb_cert(ctx
, x
, i
,
475 X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION
))
478 if (!allow_proxy_certs
&& (x
->ex_flags
& EXFLAG_PROXY
)) {
479 if (!verify_cb_cert(ctx
, x
, i
,
480 X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED
))
483 ret
= X509_check_ca(x
);
484 switch (must_be_ca
) {
486 if ((ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
)
487 && (ret
!= 1) && (ret
!= 0)) {
489 ctx
->error
= X509_V_ERR_INVALID_CA
;
496 ctx
->error
= X509_V_ERR_INVALID_NON_CA
;
501 /* X509_V_FLAG_X509_STRICT is implicit for intermediate CAs */
503 || ((i
+ 1 < num
|| ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
)
506 ctx
->error
= X509_V_ERR_INVALID_CA
;
511 if (ret
== 0 && !verify_cb_cert(ctx
, x
, i
, X509_V_OK
))
513 /* check_purpose() makes the callback as needed */
514 if (purpose
> 0 && !check_purpose(ctx
, x
, purpose
, i
, must_be_ca
))
516 /* Check pathlen if not self issued */
517 if ((i
> 1) && !(x
->ex_flags
& EXFLAG_SI
)
518 && (x
->ex_pathlen
!= -1)
519 && (plen
> (x
->ex_pathlen
+ proxy_path_length
+ 1))) {
520 if (!verify_cb_cert(ctx
, x
, i
, X509_V_ERR_PATH_LENGTH_EXCEEDED
))
523 /* Increment path length if not self issued */
524 if (!(x
->ex_flags
& EXFLAG_SI
))
527 * If this certificate is a proxy certificate, the next certificate
528 * must be another proxy certificate or a EE certificate. If not,
529 * the next certificate must be a CA certificate.
531 if (x
->ex_flags
& EXFLAG_PROXY
) {
533 * RFC3820, 4.1.3 (b)(1) stipulates that if pCPathLengthConstraint
534 * is less than max_path_length, the former should be copied to
535 * the latter, and 4.1.4 (a) stipulates that max_path_length
536 * should be verified to be larger than zero and decrement it.
538 * Because we're checking the certs in the reverse order, we start
539 * with verifying that proxy_path_length isn't larger than pcPLC,
540 * and copy the latter to the former if it is, and finally,
541 * increment proxy_path_length.
543 if (x
->ex_pcpathlen
!= -1) {
544 if (proxy_path_length
> x
->ex_pcpathlen
) {
545 if (!verify_cb_cert(ctx
, x
, i
,
546 X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED
))
549 proxy_path_length
= x
->ex_pcpathlen
;
559 static int check_name_constraints(X509_STORE_CTX
*ctx
)
563 /* Check name constraints for all certificates */
564 for (i
= sk_X509_num(ctx
->chain
) - 1; i
>= 0; i
--) {
565 X509
*x
= sk_X509_value(ctx
->chain
, i
);
568 /* Ignore self issued certs unless last in chain */
569 if (i
&& (x
->ex_flags
& EXFLAG_SI
))
573 * Proxy certificates policy has an extra constraint, where the
574 * certificate subject MUST be the issuer with a single CN entry
576 * (RFC 3820: 3.4, 4.1.3 (a)(4))
578 if (x
->ex_flags
& EXFLAG_PROXY
) {
579 X509_NAME
*tmpsubject
= X509_get_subject_name(x
);
580 X509_NAME
*tmpissuer
= X509_get_issuer_name(x
);
581 X509_NAME_ENTRY
*tmpentry
= NULL
;
582 int last_object_nid
= 0;
584 int last_object_loc
= X509_NAME_entry_count(tmpsubject
) - 1;
586 /* Check that there are at least two RDNs */
587 if (last_object_loc
< 1) {
588 err
= X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION
;
589 goto proxy_name_done
;
593 * Check that there is exactly one more RDN in subject as
594 * there is in issuer.
596 if (X509_NAME_entry_count(tmpsubject
)
597 != X509_NAME_entry_count(tmpissuer
) + 1) {
598 err
= X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION
;
599 goto proxy_name_done
;
603 * Check that the last subject component isn't part of a
606 if (X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject
,
608 == X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject
,
609 last_object_loc
- 1))) {
610 err
= X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION
;
611 goto proxy_name_done
;
615 * Check that the last subject RDN is a commonName, and that
616 * all the previous RDNs match the issuer exactly
618 tmpsubject
= X509_NAME_dup(tmpsubject
);
619 if (tmpsubject
== NULL
) {
620 X509err(X509_F_CHECK_NAME_CONSTRAINTS
, ERR_R_MALLOC_FAILURE
);
621 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
626 X509_NAME_delete_entry(tmpsubject
, last_object_loc
);
628 OBJ_obj2nid(X509_NAME_ENTRY_get_object(tmpentry
));
630 if (last_object_nid
!= NID_commonName
631 || X509_NAME_cmp(tmpsubject
, tmpissuer
) != 0) {
632 err
= X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION
;
635 X509_NAME_ENTRY_free(tmpentry
);
636 X509_NAME_free(tmpsubject
);
640 && !verify_cb_cert(ctx
, x
, i
, err
))
645 * Check against constraints for all certificates higher in chain
646 * including trust anchor. Trust anchor not strictly speaking needed
647 * but if it includes constraints it is to be assumed it expects them
650 for (j
= sk_X509_num(ctx
->chain
) - 1; j
> i
; j
--) {
651 NAME_CONSTRAINTS
*nc
= sk_X509_value(ctx
->chain
, j
)->nc
;
654 int rv
= NAME_CONSTRAINTS_check(x
, nc
);
656 /* If EE certificate check commonName too */
657 if (rv
== X509_V_OK
&& i
== 0)
658 rv
= NAME_CONSTRAINTS_check_CN(x
, nc
);
663 case X509_V_ERR_OUT_OF_MEM
:
666 if (!verify_cb_cert(ctx
, x
, i
, rv
))
676 static int check_id_error(X509_STORE_CTX
*ctx
, int errcode
)
678 return verify_cb_cert(ctx
, ctx
->cert
, 0, errcode
);
681 static int check_hosts(X509
*x
, X509_VERIFY_PARAM
*vpm
)
684 int n
= sk_OPENSSL_STRING_num(vpm
->hosts
);
687 if (vpm
->peername
!= NULL
) {
688 OPENSSL_free(vpm
->peername
);
689 vpm
->peername
= NULL
;
691 for (i
= 0; i
< n
; ++i
) {
692 name
= sk_OPENSSL_STRING_value(vpm
->hosts
, i
);
693 if (X509_check_host(x
, name
, 0, vpm
->hostflags
, &vpm
->peername
) > 0)
699 static int check_id(X509_STORE_CTX
*ctx
)
701 X509_VERIFY_PARAM
*vpm
= ctx
->param
;
703 if (vpm
->hosts
&& check_hosts(x
, vpm
) <= 0) {
704 if (!check_id_error(ctx
, X509_V_ERR_HOSTNAME_MISMATCH
))
707 if (vpm
->email
&& X509_check_email(x
, vpm
->email
, vpm
->emaillen
, 0) <= 0) {
708 if (!check_id_error(ctx
, X509_V_ERR_EMAIL_MISMATCH
))
711 if (vpm
->ip
&& X509_check_ip(x
, vpm
->ip
, vpm
->iplen
, 0) <= 0) {
712 if (!check_id_error(ctx
, X509_V_ERR_IP_ADDRESS_MISMATCH
))
718 static int check_trust(X509_STORE_CTX
*ctx
, int num_untrusted
)
723 SSL_DANE
*dane
= ctx
->dane
;
724 int num
= sk_X509_num(ctx
->chain
);
728 * Check for a DANE issuer at depth 1 or greater, if it is a DANE-TA(2)
729 * match, we're done, otherwise we'll merely record the match depth.
731 if (DANETLS_HAS_TA(dane
) && num_untrusted
> 0 && num_untrusted
< num
) {
732 switch (trust
= check_dane_issuer(ctx
, num_untrusted
)) {
733 case X509_TRUST_TRUSTED
:
734 case X509_TRUST_REJECTED
:
740 * Check trusted certificates in chain at depth num_untrusted and up.
741 * Note, that depths 0..num_untrusted-1 may also contain trusted
742 * certificates, but the caller is expected to have already checked those,
743 * and wants to incrementally check just any added since.
745 for (i
= num_untrusted
; i
< num
; i
++) {
746 x
= sk_X509_value(ctx
->chain
, i
);
747 trust
= X509_check_trust(x
, ctx
->param
->trust
, 0);
748 /* If explicitly trusted return trusted */
749 if (trust
== X509_TRUST_TRUSTED
)
751 if (trust
== X509_TRUST_REJECTED
)
756 * If we are looking at a trusted certificate, and accept partial chains,
757 * the chain is PKIX trusted.
759 if (num_untrusted
< num
) {
760 if (ctx
->param
->flags
& X509_V_FLAG_PARTIAL_CHAIN
)
762 return X509_TRUST_UNTRUSTED
;
765 if (num_untrusted
== num
&& ctx
->param
->flags
& X509_V_FLAG_PARTIAL_CHAIN
) {
767 * Last-resort call with no new trusted certificates, check the leaf
768 * for a direct trust store match.
771 x
= sk_X509_value(ctx
->chain
, i
);
772 mx
= lookup_cert_match(ctx
, x
);
774 return X509_TRUST_UNTRUSTED
;
777 * Check explicit auxiliary trust/reject settings. If none are set,
778 * we'll accept X509_TRUST_UNTRUSTED when not self-signed.
780 trust
= X509_check_trust(mx
, ctx
->param
->trust
, 0);
781 if (trust
== X509_TRUST_REJECTED
) {
786 /* Replace leaf with trusted match */
787 (void) sk_X509_set(ctx
->chain
, 0, mx
);
789 ctx
->num_untrusted
= 0;
794 * If no trusted certs in chain at all return untrusted and allow
795 * standard (no issuer cert) etc errors to be indicated.
797 return X509_TRUST_UNTRUSTED
;
800 if (!verify_cb_cert(ctx
, x
, i
, X509_V_ERR_CERT_REJECTED
))
801 return X509_TRUST_REJECTED
;
802 return X509_TRUST_UNTRUSTED
;
805 if (!DANETLS_ENABLED(dane
))
806 return X509_TRUST_TRUSTED
;
808 dane
->pdpth
= num_untrusted
;
809 /* With DANE, PKIX alone is not trusted until we have both */
810 if (dane
->mdpth
>= 0)
811 return X509_TRUST_TRUSTED
;
812 return X509_TRUST_UNTRUSTED
;
815 static int check_revocation(X509_STORE_CTX
*ctx
)
817 int i
= 0, last
= 0, ok
= 0;
818 if (!(ctx
->param
->flags
& X509_V_FLAG_CRL_CHECK
))
820 if (ctx
->param
->flags
& X509_V_FLAG_CRL_CHECK_ALL
)
821 last
= sk_X509_num(ctx
->chain
) - 1;
823 /* If checking CRL paths this isn't the EE certificate */
828 for (i
= 0; i
<= last
; i
++) {
829 ctx
->error_depth
= i
;
830 ok
= check_cert(ctx
);
837 static int check_cert(X509_STORE_CTX
*ctx
)
839 X509_CRL
*crl
= NULL
, *dcrl
= NULL
;
841 int cnum
= ctx
->error_depth
;
842 X509
*x
= sk_X509_value(ctx
->chain
, cnum
);
844 ctx
->current_cert
= x
;
845 ctx
->current_issuer
= NULL
;
846 ctx
->current_crl_score
= 0;
847 ctx
->current_reasons
= 0;
849 if (x
->ex_flags
& EXFLAG_PROXY
)
852 while (ctx
->current_reasons
!= CRLDP_ALL_REASONS
) {
853 unsigned int last_reasons
= ctx
->current_reasons
;
855 /* Try to retrieve relevant CRL */
857 ok
= ctx
->get_crl(ctx
, &crl
, x
);
859 ok
= get_crl_delta(ctx
, &crl
, &dcrl
, x
);
861 * If error looking up CRL, nothing we can do except notify callback
864 ok
= verify_cb_crl(ctx
, X509_V_ERR_UNABLE_TO_GET_CRL
);
867 ctx
->current_crl
= crl
;
868 ok
= ctx
->check_crl(ctx
, crl
);
873 ok
= ctx
->check_crl(ctx
, dcrl
);
876 ok
= ctx
->cert_crl(ctx
, dcrl
, x
);
882 /* Don't look in full CRL if delta reason is removefromCRL */
884 ok
= ctx
->cert_crl(ctx
, crl
, x
);
894 * If reasons not updated we won't get anywhere by another iteration,
897 if (last_reasons
== ctx
->current_reasons
) {
898 ok
= verify_cb_crl(ctx
, X509_V_ERR_UNABLE_TO_GET_CRL
);
906 ctx
->current_crl
= NULL
;
910 /* Check CRL times against values in X509_STORE_CTX */
912 static int check_crl_time(X509_STORE_CTX
*ctx
, X509_CRL
*crl
, int notify
)
918 ctx
->current_crl
= crl
;
919 if (ctx
->param
->flags
& X509_V_FLAG_USE_CHECK_TIME
)
920 ptime
= &ctx
->param
->check_time
;
921 else if (ctx
->param
->flags
& X509_V_FLAG_NO_CHECK_TIME
)
926 i
= X509_cmp_time(X509_CRL_get0_lastUpdate(crl
), ptime
);
930 if (!verify_cb_crl(ctx
, X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
))
937 if (!verify_cb_crl(ctx
, X509_V_ERR_CRL_NOT_YET_VALID
))
941 if (X509_CRL_get0_nextUpdate(crl
)) {
942 i
= X509_cmp_time(X509_CRL_get0_nextUpdate(crl
), ptime
);
947 if (!verify_cb_crl(ctx
, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
))
950 /* Ignore expiry of base CRL is delta is valid */
951 if ((i
< 0) && !(ctx
->current_crl_score
& CRL_SCORE_TIME_DELTA
)) {
954 if (!verify_cb_crl(ctx
, X509_V_ERR_CRL_HAS_EXPIRED
))
960 ctx
->current_crl
= NULL
;
965 static int get_crl_sk(X509_STORE_CTX
*ctx
, X509_CRL
**pcrl
, X509_CRL
**pdcrl
,
966 X509
**pissuer
, int *pscore
, unsigned int *preasons
,
967 STACK_OF(X509_CRL
) *crls
)
969 int i
, crl_score
, best_score
= *pscore
;
970 unsigned int reasons
, best_reasons
= 0;
971 X509
*x
= ctx
->current_cert
;
972 X509_CRL
*crl
, *best_crl
= NULL
;
973 X509
*crl_issuer
= NULL
, *best_crl_issuer
= NULL
;
975 for (i
= 0; i
< sk_X509_CRL_num(crls
); i
++) {
976 crl
= sk_X509_CRL_value(crls
, i
);
978 crl_score
= get_crl_score(ctx
, &crl_issuer
, &reasons
, crl
, x
);
979 if (crl_score
< best_score
|| crl_score
== 0)
981 /* If current CRL is equivalent use it if it is newer */
982 if (crl_score
== best_score
&& best_crl
!= NULL
) {
984 if (ASN1_TIME_diff(&day
, &sec
, X509_CRL_get0_lastUpdate(best_crl
),
985 X509_CRL_get0_lastUpdate(crl
)) == 0)
988 * ASN1_TIME_diff never returns inconsistent signs for |day|
991 if (day
<= 0 && sec
<= 0)
995 best_crl_issuer
= crl_issuer
;
996 best_score
= crl_score
;
997 best_reasons
= reasons
;
1001 X509_CRL_free(*pcrl
);
1003 *pissuer
= best_crl_issuer
;
1004 *pscore
= best_score
;
1005 *preasons
= best_reasons
;
1006 X509_CRL_up_ref(best_crl
);
1007 X509_CRL_free(*pdcrl
);
1009 get_delta_sk(ctx
, pdcrl
, pscore
, best_crl
, crls
);
1012 if (best_score
>= CRL_SCORE_VALID
)
1019 * Compare two CRL extensions for delta checking purposes. They should be
1020 * both present or both absent. If both present all fields must be identical.
1023 static int crl_extension_match(X509_CRL
*a
, X509_CRL
*b
, int nid
)
1025 ASN1_OCTET_STRING
*exta
, *extb
;
1027 i
= X509_CRL_get_ext_by_NID(a
, nid
, -1);
1029 /* Can't have multiple occurrences */
1030 if (X509_CRL_get_ext_by_NID(a
, nid
, i
) != -1)
1032 exta
= X509_EXTENSION_get_data(X509_CRL_get_ext(a
, i
));
1036 i
= X509_CRL_get_ext_by_NID(b
, nid
, -1);
1040 if (X509_CRL_get_ext_by_NID(b
, nid
, i
) != -1)
1042 extb
= X509_EXTENSION_get_data(X509_CRL_get_ext(b
, i
));
1052 if (ASN1_OCTET_STRING_cmp(exta
, extb
))
1058 /* See if a base and delta are compatible */
1060 static int check_delta_base(X509_CRL
*delta
, X509_CRL
*base
)
1062 /* Delta CRL must be a delta */
1063 if (!delta
->base_crl_number
)
1065 /* Base must have a CRL number */
1066 if (!base
->crl_number
)
1068 /* Issuer names must match */
1069 if (X509_NAME_cmp(X509_CRL_get_issuer(base
), X509_CRL_get_issuer(delta
)))
1071 /* AKID and IDP must match */
1072 if (!crl_extension_match(delta
, base
, NID_authority_key_identifier
))
1074 if (!crl_extension_match(delta
, base
, NID_issuing_distribution_point
))
1076 /* Delta CRL base number must not exceed Full CRL number. */
1077 if (ASN1_INTEGER_cmp(delta
->base_crl_number
, base
->crl_number
) > 0)
1079 /* Delta CRL number must exceed full CRL number */
1080 if (ASN1_INTEGER_cmp(delta
->crl_number
, base
->crl_number
) > 0)
1086 * For a given base CRL find a delta... maybe extend to delta scoring or
1087 * retrieve a chain of deltas...
1090 static void get_delta_sk(X509_STORE_CTX
*ctx
, X509_CRL
**dcrl
, int *pscore
,
1091 X509_CRL
*base
, STACK_OF(X509_CRL
) *crls
)
1095 if (!(ctx
->param
->flags
& X509_V_FLAG_USE_DELTAS
))
1097 if (!((ctx
->current_cert
->ex_flags
| base
->flags
) & EXFLAG_FRESHEST
))
1099 for (i
= 0; i
< sk_X509_CRL_num(crls
); i
++) {
1100 delta
= sk_X509_CRL_value(crls
, i
);
1101 if (check_delta_base(delta
, base
)) {
1102 if (check_crl_time(ctx
, delta
, 0))
1103 *pscore
|= CRL_SCORE_TIME_DELTA
;
1104 X509_CRL_up_ref(delta
);
1113 * For a given CRL return how suitable it is for the supplied certificate
1114 * 'x'. The return value is a mask of several criteria. If the issuer is not
1115 * the certificate issuer this is returned in *pissuer. The reasons mask is
1116 * also used to determine if the CRL is suitable: if no new reasons the CRL
1117 * is rejected, otherwise reasons is updated.
1120 static int get_crl_score(X509_STORE_CTX
*ctx
, X509
**pissuer
,
1121 unsigned int *preasons
, X509_CRL
*crl
, X509
*x
)
1125 unsigned int tmp_reasons
= *preasons
, crl_reasons
;
1127 /* First see if we can reject CRL straight away */
1129 /* Invalid IDP cannot be processed */
1130 if (crl
->idp_flags
& IDP_INVALID
)
1132 /* Reason codes or indirect CRLs need extended CRL support */
1133 if (!(ctx
->param
->flags
& X509_V_FLAG_EXTENDED_CRL_SUPPORT
)) {
1134 if (crl
->idp_flags
& (IDP_INDIRECT
| IDP_REASONS
))
1136 } else if (crl
->idp_flags
& IDP_REASONS
) {
1137 /* If no new reasons reject */
1138 if (!(crl
->idp_reasons
& ~tmp_reasons
))
1141 /* Don't process deltas at this stage */
1142 else if (crl
->base_crl_number
)
1144 /* If issuer name doesn't match certificate need indirect CRL */
1145 if (X509_NAME_cmp(X509_get_issuer_name(x
), X509_CRL_get_issuer(crl
))) {
1146 if (!(crl
->idp_flags
& IDP_INDIRECT
))
1149 crl_score
|= CRL_SCORE_ISSUER_NAME
;
1151 if (!(crl
->flags
& EXFLAG_CRITICAL
))
1152 crl_score
|= CRL_SCORE_NOCRITICAL
;
1155 if (check_crl_time(ctx
, crl
, 0))
1156 crl_score
|= CRL_SCORE_TIME
;
1158 /* Check authority key ID and locate certificate issuer */
1159 crl_akid_check(ctx
, crl
, pissuer
, &crl_score
);
1161 /* If we can't locate certificate issuer at this point forget it */
1163 if (!(crl_score
& CRL_SCORE_AKID
))
1166 /* Check cert for matching CRL distribution points */
1168 if (crl_crldp_check(x
, crl
, crl_score
, &crl_reasons
)) {
1169 /* If no new reasons reject */
1170 if (!(crl_reasons
& ~tmp_reasons
))
1172 tmp_reasons
|= crl_reasons
;
1173 crl_score
|= CRL_SCORE_SCOPE
;
1176 *preasons
= tmp_reasons
;
1182 static void crl_akid_check(X509_STORE_CTX
*ctx
, X509_CRL
*crl
,
1183 X509
**pissuer
, int *pcrl_score
)
1185 X509
*crl_issuer
= NULL
;
1186 X509_NAME
*cnm
= X509_CRL_get_issuer(crl
);
1187 int cidx
= ctx
->error_depth
;
1190 if (cidx
!= sk_X509_num(ctx
->chain
) - 1)
1193 crl_issuer
= sk_X509_value(ctx
->chain
, cidx
);
1195 if (X509_check_akid(crl_issuer
, crl
->akid
) == X509_V_OK
) {
1196 if (*pcrl_score
& CRL_SCORE_ISSUER_NAME
) {
1197 *pcrl_score
|= CRL_SCORE_AKID
| CRL_SCORE_ISSUER_CERT
;
1198 *pissuer
= crl_issuer
;
1203 for (cidx
++; cidx
< sk_X509_num(ctx
->chain
); cidx
++) {
1204 crl_issuer
= sk_X509_value(ctx
->chain
, cidx
);
1205 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer
), cnm
))
1207 if (X509_check_akid(crl_issuer
, crl
->akid
) == X509_V_OK
) {
1208 *pcrl_score
|= CRL_SCORE_AKID
| CRL_SCORE_SAME_PATH
;
1209 *pissuer
= crl_issuer
;
1214 /* Anything else needs extended CRL support */
1216 if (!(ctx
->param
->flags
& X509_V_FLAG_EXTENDED_CRL_SUPPORT
))
1220 * Otherwise the CRL issuer is not on the path. Look for it in the set of
1221 * untrusted certificates.
1223 for (i
= 0; i
< sk_X509_num(ctx
->untrusted
); i
++) {
1224 crl_issuer
= sk_X509_value(ctx
->untrusted
, i
);
1225 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer
), cnm
))
1227 if (X509_check_akid(crl_issuer
, crl
->akid
) == X509_V_OK
) {
1228 *pissuer
= crl_issuer
;
1229 *pcrl_score
|= CRL_SCORE_AKID
;
1236 * Check the path of a CRL issuer certificate. This creates a new
1237 * X509_STORE_CTX and populates it with most of the parameters from the
1238 * parent. This could be optimised somewhat since a lot of path checking will
1239 * be duplicated by the parent, but this will rarely be used in practice.
1242 static int check_crl_path(X509_STORE_CTX
*ctx
, X509
*x
)
1244 X509_STORE_CTX crl_ctx
;
1247 /* Don't allow recursive CRL path validation */
1250 if (!X509_STORE_CTX_init(&crl_ctx
, ctx
->ctx
, x
, ctx
->untrusted
))
1253 crl_ctx
.crls
= ctx
->crls
;
1254 /* Copy verify params across */
1255 X509_STORE_CTX_set0_param(&crl_ctx
, ctx
->param
);
1257 crl_ctx
.parent
= ctx
;
1258 crl_ctx
.verify_cb
= ctx
->verify_cb
;
1260 /* Verify CRL issuer */
1261 ret
= X509_verify_cert(&crl_ctx
);
1265 /* Check chain is acceptable */
1266 ret
= check_crl_chain(ctx
, ctx
->chain
, crl_ctx
.chain
);
1268 X509_STORE_CTX_cleanup(&crl_ctx
);
1273 * RFC3280 says nothing about the relationship between CRL path and
1274 * certificate path, which could lead to situations where a certificate could
1275 * be revoked or validated by a CA not authorised to do so. RFC5280 is more
1276 * strict and states that the two paths must end in the same trust anchor,
1277 * though some discussions remain... until this is resolved we use the
1281 static int check_crl_chain(X509_STORE_CTX
*ctx
,
1282 STACK_OF(X509
) *cert_path
,
1283 STACK_OF(X509
) *crl_path
)
1285 X509
*cert_ta
, *crl_ta
;
1286 cert_ta
= sk_X509_value(cert_path
, sk_X509_num(cert_path
) - 1);
1287 crl_ta
= sk_X509_value(crl_path
, sk_X509_num(crl_path
) - 1);
1288 if (!X509_cmp(cert_ta
, crl_ta
))
1294 * Check for match between two dist point names: three separate cases.
1295 * 1. Both are relative names and compare X509_NAME types.
1296 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1297 * 3. Both are full names and compare two GENERAL_NAMES.
1298 * 4. One is NULL: automatic match.
1301 static int idp_check_dp(DIST_POINT_NAME
*a
, DIST_POINT_NAME
*b
)
1303 X509_NAME
*nm
= NULL
;
1304 GENERAL_NAMES
*gens
= NULL
;
1305 GENERAL_NAME
*gena
, *genb
;
1312 /* Case 1: two X509_NAME */
1316 if (!X509_NAME_cmp(a
->dpname
, b
->dpname
))
1321 /* Case 2: set name and GENERAL_NAMES appropriately */
1323 gens
= b
->name
.fullname
;
1324 } else if (b
->type
== 1) {
1327 /* Case 2: set name and GENERAL_NAMES appropriately */
1328 gens
= a
->name
.fullname
;
1332 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1334 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
1335 gena
= sk_GENERAL_NAME_value(gens
, i
);
1336 if (gena
->type
!= GEN_DIRNAME
)
1338 if (!X509_NAME_cmp(nm
, gena
->d
.directoryName
))
1344 /* Else case 3: two GENERAL_NAMES */
1346 for (i
= 0; i
< sk_GENERAL_NAME_num(a
->name
.fullname
); i
++) {
1347 gena
= sk_GENERAL_NAME_value(a
->name
.fullname
, i
);
1348 for (j
= 0; j
< sk_GENERAL_NAME_num(b
->name
.fullname
); j
++) {
1349 genb
= sk_GENERAL_NAME_value(b
->name
.fullname
, j
);
1350 if (!GENERAL_NAME_cmp(gena
, genb
))
1359 static int crldp_check_crlissuer(DIST_POINT
*dp
, X509_CRL
*crl
, int crl_score
)
1362 X509_NAME
*nm
= X509_CRL_get_issuer(crl
);
1363 /* If no CRLissuer return is successful iff don't need a match */
1365 return ! !(crl_score
& CRL_SCORE_ISSUER_NAME
);
1366 for (i
= 0; i
< sk_GENERAL_NAME_num(dp
->CRLissuer
); i
++) {
1367 GENERAL_NAME
*gen
= sk_GENERAL_NAME_value(dp
->CRLissuer
, i
);
1368 if (gen
->type
!= GEN_DIRNAME
)
1370 if (!X509_NAME_cmp(gen
->d
.directoryName
, nm
))
1376 /* Check CRLDP and IDP */
1378 static int crl_crldp_check(X509
*x
, X509_CRL
*crl
, int crl_score
,
1379 unsigned int *preasons
)
1382 if (crl
->idp_flags
& IDP_ONLYATTR
)
1384 if (x
->ex_flags
& EXFLAG_CA
) {
1385 if (crl
->idp_flags
& IDP_ONLYUSER
)
1388 if (crl
->idp_flags
& IDP_ONLYCA
)
1391 *preasons
= crl
->idp_reasons
;
1392 for (i
= 0; i
< sk_DIST_POINT_num(x
->crldp
); i
++) {
1393 DIST_POINT
*dp
= sk_DIST_POINT_value(x
->crldp
, i
);
1394 if (crldp_check_crlissuer(dp
, crl
, crl_score
)) {
1395 if (!crl
->idp
|| idp_check_dp(dp
->distpoint
, crl
->idp
->distpoint
)) {
1396 *preasons
&= dp
->dp_reasons
;
1401 if ((!crl
->idp
|| !crl
->idp
->distpoint
)
1402 && (crl_score
& CRL_SCORE_ISSUER_NAME
))
1408 * Retrieve CRL corresponding to current certificate. If deltas enabled try
1409 * to find a delta CRL too
1412 static int get_crl_delta(X509_STORE_CTX
*ctx
,
1413 X509_CRL
**pcrl
, X509_CRL
**pdcrl
, X509
*x
)
1416 X509
*issuer
= NULL
;
1418 unsigned int reasons
;
1419 X509_CRL
*crl
= NULL
, *dcrl
= NULL
;
1420 STACK_OF(X509_CRL
) *skcrl
;
1421 X509_NAME
*nm
= X509_get_issuer_name(x
);
1423 reasons
= ctx
->current_reasons
;
1424 ok
= get_crl_sk(ctx
, &crl
, &dcrl
,
1425 &issuer
, &crl_score
, &reasons
, ctx
->crls
);
1429 /* Lookup CRLs from store */
1431 skcrl
= ctx
->lookup_crls(ctx
, nm
);
1433 /* If no CRLs found and a near match from get_crl_sk use that */
1437 get_crl_sk(ctx
, &crl
, &dcrl
, &issuer
, &crl_score
, &reasons
, skcrl
);
1439 sk_X509_CRL_pop_free(skcrl
, X509_CRL_free
);
1442 /* If we got any kind of CRL use it and return success */
1444 ctx
->current_issuer
= issuer
;
1445 ctx
->current_crl_score
= crl_score
;
1446 ctx
->current_reasons
= reasons
;
1454 /* Check CRL validity */
1455 static int check_crl(X509_STORE_CTX
*ctx
, X509_CRL
*crl
)
1457 X509
*issuer
= NULL
;
1458 EVP_PKEY
*ikey
= NULL
;
1459 int cnum
= ctx
->error_depth
;
1460 int chnum
= sk_X509_num(ctx
->chain
) - 1;
1462 /* if we have an alternative CRL issuer cert use that */
1463 if (ctx
->current_issuer
)
1464 issuer
= ctx
->current_issuer
;
1466 * Else find CRL issuer: if not last certificate then issuer is next
1467 * certificate in chain.
1469 else if (cnum
< chnum
)
1470 issuer
= sk_X509_value(ctx
->chain
, cnum
+ 1);
1472 issuer
= sk_X509_value(ctx
->chain
, chnum
);
1473 /* If not self signed, can't check signature */
1474 if (!ctx
->check_issued(ctx
, issuer
, issuer
) &&
1475 !verify_cb_crl(ctx
, X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
))
1483 * Skip most tests for deltas because they have already been done
1485 if (!crl
->base_crl_number
) {
1486 /* Check for cRLSign bit if keyUsage present */
1487 if ((issuer
->ex_flags
& EXFLAG_KUSAGE
) &&
1488 !(issuer
->ex_kusage
& KU_CRL_SIGN
) &&
1489 !verify_cb_crl(ctx
, X509_V_ERR_KEYUSAGE_NO_CRL_SIGN
))
1492 if (!(ctx
->current_crl_score
& CRL_SCORE_SCOPE
) &&
1493 !verify_cb_crl(ctx
, X509_V_ERR_DIFFERENT_CRL_SCOPE
))
1496 if (!(ctx
->current_crl_score
& CRL_SCORE_SAME_PATH
) &&
1497 check_crl_path(ctx
, ctx
->current_issuer
) <= 0 &&
1498 !verify_cb_crl(ctx
, X509_V_ERR_CRL_PATH_VALIDATION_ERROR
))
1501 if ((crl
->idp_flags
& IDP_INVALID
) &&
1502 !verify_cb_crl(ctx
, X509_V_ERR_INVALID_EXTENSION
))
1506 if (!(ctx
->current_crl_score
& CRL_SCORE_TIME
) &&
1507 !check_crl_time(ctx
, crl
, 1))
1510 /* Attempt to get issuer certificate public key */
1511 ikey
= X509_get0_pubkey(issuer
);
1514 !verify_cb_crl(ctx
, X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
))
1518 int rv
= X509_CRL_check_suiteb(crl
, ikey
, ctx
->param
->flags
);
1520 if (rv
!= X509_V_OK
&& !verify_cb_crl(ctx
, rv
))
1522 /* Verify CRL signature */
1523 if (X509_CRL_verify(crl
, ikey
) <= 0 &&
1524 !verify_cb_crl(ctx
, X509_V_ERR_CRL_SIGNATURE_FAILURE
))
1530 /* Check certificate against CRL */
1531 static int cert_crl(X509_STORE_CTX
*ctx
, X509_CRL
*crl
, X509
*x
)
1536 * The rules changed for this... previously if a CRL contained unhandled
1537 * critical extensions it could still be used to indicate a certificate
1538 * was revoked. This has since been changed since critical extensions can
1539 * change the meaning of CRL entries.
1541 if (!(ctx
->param
->flags
& X509_V_FLAG_IGNORE_CRITICAL
)
1542 && (crl
->flags
& EXFLAG_CRITICAL
) &&
1543 !verify_cb_crl(ctx
, X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION
))
1546 * Look for serial number of certificate in CRL. If found, make sure
1547 * reason is not removeFromCRL.
1549 if (X509_CRL_get0_by_cert(crl
, &rev
, x
)) {
1550 if (rev
->reason
== CRL_REASON_REMOVE_FROM_CRL
)
1552 if (!verify_cb_crl(ctx
, X509_V_ERR_CERT_REVOKED
))
1559 static int check_policy(X509_STORE_CTX
*ctx
)
1566 * With DANE, the trust anchor might be a bare public key, not a
1567 * certificate! In that case our chain does not have the trust anchor
1568 * certificate as a top-most element. This comports well with RFC5280
1569 * chain verification, since there too, the trust anchor is not part of the
1570 * chain to be verified. In particular, X509_policy_check() does not look
1571 * at the TA cert, but assumes that it is present as the top-most chain
1572 * element. We therefore temporarily push a NULL cert onto the chain if it
1573 * was verified via a bare public key, and pop it off right after the
1574 * X509_policy_check() call.
1576 if (ctx
->bare_ta_signed
&& !sk_X509_push(ctx
->chain
, NULL
)) {
1577 X509err(X509_F_CHECK_POLICY
, ERR_R_MALLOC_FAILURE
);
1578 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
1581 ret
= X509_policy_check(&ctx
->tree
, &ctx
->explicit_policy
, ctx
->chain
,
1582 ctx
->param
->policies
, ctx
->param
->flags
);
1583 if (ctx
->bare_ta_signed
)
1584 sk_X509_pop(ctx
->chain
);
1586 if (ret
== X509_PCY_TREE_INTERNAL
) {
1587 X509err(X509_F_CHECK_POLICY
, ERR_R_MALLOC_FAILURE
);
1588 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
1591 /* Invalid or inconsistent extensions */
1592 if (ret
== X509_PCY_TREE_INVALID
) {
1595 /* Locate certificates with bad extensions and notify callback. */
1596 for (i
= 1; i
< sk_X509_num(ctx
->chain
); i
++) {
1597 X509
*x
= sk_X509_value(ctx
->chain
, i
);
1599 if (!(x
->ex_flags
& EXFLAG_INVALID_POLICY
))
1601 if (!verify_cb_cert(ctx
, x
, i
,
1602 X509_V_ERR_INVALID_POLICY_EXTENSION
))
1607 if (ret
== X509_PCY_TREE_FAILURE
) {
1608 ctx
->current_cert
= NULL
;
1609 ctx
->error
= X509_V_ERR_NO_EXPLICIT_POLICY
;
1610 return ctx
->verify_cb(0, ctx
);
1612 if (ret
!= X509_PCY_TREE_VALID
) {
1613 X509err(X509_F_CHECK_POLICY
, ERR_R_INTERNAL_ERROR
);
1617 if (ctx
->param
->flags
& X509_V_FLAG_NOTIFY_POLICY
) {
1618 ctx
->current_cert
= NULL
;
1620 * Verification errors need to be "sticky", a callback may have allowed
1621 * an SSL handshake to continue despite an error, and we must then
1622 * remain in an error state. Therefore, we MUST NOT clear earlier
1623 * verification errors by setting the error to X509_V_OK.
1625 if (!ctx
->verify_cb(2, ctx
))
1633 * Check certificate validity times.
1634 * If depth >= 0, invoke verification callbacks on error, otherwise just return
1635 * the validation status.
1637 * Return 1 on success, 0 otherwise.
1639 int x509_check_cert_time(X509_STORE_CTX
*ctx
, X509
*x
, int depth
)
1644 if (ctx
->param
->flags
& X509_V_FLAG_USE_CHECK_TIME
)
1645 ptime
= &ctx
->param
->check_time
;
1646 else if (ctx
->param
->flags
& X509_V_FLAG_NO_CHECK_TIME
)
1651 i
= X509_cmp_time(X509_get0_notBefore(x
), ptime
);
1652 if (i
>= 0 && depth
< 0)
1654 if (i
== 0 && !verify_cb_cert(ctx
, x
, depth
,
1655 X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
))
1657 if (i
> 0 && !verify_cb_cert(ctx
, x
, depth
, X509_V_ERR_CERT_NOT_YET_VALID
))
1660 i
= X509_cmp_time(X509_get0_notAfter(x
), ptime
);
1661 if (i
<= 0 && depth
< 0)
1663 if (i
== 0 && !verify_cb_cert(ctx
, x
, depth
,
1664 X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
))
1666 if (i
< 0 && !verify_cb_cert(ctx
, x
, depth
, X509_V_ERR_CERT_HAS_EXPIRED
))
1671 static int internal_verify(X509_STORE_CTX
*ctx
)
1673 int n
= sk_X509_num(ctx
->chain
) - 1;
1674 X509
*xi
= sk_X509_value(ctx
->chain
, n
);
1678 * With DANE-verified bare public key TA signatures, it remains only to
1679 * check the timestamps of the top certificate. We report the issuer as
1680 * NULL, since all we have is a bare key.
1682 if (ctx
->bare_ta_signed
) {
1688 if (ctx
->check_issued(ctx
, xi
, xi
))
1691 if (ctx
->param
->flags
& X509_V_FLAG_PARTIAL_CHAIN
) {
1696 return verify_cb_cert(ctx
, xi
, 0,
1697 X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
);
1699 ctx
->error_depth
= n
;
1700 xs
= sk_X509_value(ctx
->chain
, n
);
1704 * Do not clear ctx->error=0, it must be "sticky", only the user's callback
1705 * is allowed to reset errors (at its own peril).
1711 * Skip signature check for self signed certificates unless explicitly
1712 * asked for. It doesn't add any security and just wastes time. If
1713 * the issuer's public key is unusable, report the issuer certificate
1714 * and its depth (rather than the depth of the subject).
1716 if (xs
!= xi
|| (ctx
->param
->flags
& X509_V_FLAG_CHECK_SS_SIGNATURE
)) {
1717 if ((pkey
= X509_get0_pubkey(xi
)) == NULL
) {
1718 if (!verify_cb_cert(ctx
, xi
, xi
!= xs
? n
+1 : n
,
1719 X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
))
1721 } else if (X509_verify(xs
, pkey
) <= 0) {
1722 if (!verify_cb_cert(ctx
, xs
, n
,
1723 X509_V_ERR_CERT_SIGNATURE_FAILURE
))
1729 /* Calls verify callback as needed */
1730 if (!x509_check_cert_time(ctx
, xs
, n
))
1734 * Signal success at this depth. However, the previous error (if any)
1737 ctx
->current_issuer
= xi
;
1738 ctx
->current_cert
= xs
;
1739 ctx
->error_depth
= n
;
1740 if (!ctx
->verify_cb(1, ctx
))
1745 xs
= sk_X509_value(ctx
->chain
, n
);
1751 int X509_cmp_current_time(const ASN1_TIME
*ctm
)
1753 return X509_cmp_time(ctm
, NULL
);
1756 int X509_cmp_time(const ASN1_TIME
*ctm
, time_t *cmp_time
)
1758 static const size_t utctime_length
= sizeof("YYMMDDHHMMSSZ") - 1;
1759 static const size_t generalizedtime_length
= sizeof("YYYYMMDDHHMMSSZ") - 1;
1760 ASN1_TIME
*asn1_cmp_time
= NULL
;
1761 int i
, day
, sec
, ret
= 0;
1764 * Note that ASN.1 allows much more slack in the time format than RFC5280.
1765 * In RFC5280, the representation is fixed:
1766 * UTCTime: YYMMDDHHMMSSZ
1767 * GeneralizedTime: YYYYMMDDHHMMSSZ
1769 * We do NOT currently enforce the following RFC 5280 requirement:
1770 * "CAs conforming to this profile MUST always encode certificate
1771 * validity dates through the year 2049 as UTCTime; certificate validity
1772 * dates in 2050 or later MUST be encoded as GeneralizedTime."
1774 switch (ctm
->type
) {
1775 case V_ASN1_UTCTIME
:
1776 if (ctm
->length
!= (int)(utctime_length
))
1779 case V_ASN1_GENERALIZEDTIME
:
1780 if (ctm
->length
!= (int)(generalizedtime_length
))
1788 * Verify the format: the ASN.1 functions we use below allow a more
1789 * flexible format than what's mandated by RFC 5280.
1790 * Digit and date ranges will be verified in the conversion methods.
1792 for (i
= 0; i
< ctm
->length
- 1; i
++) {
1793 if (!ossl_isdigit(ctm
->data
[i
]))
1796 if (ctm
->data
[ctm
->length
- 1] != 'Z')
1800 * There is ASN1_UTCTIME_cmp_time_t but no
1801 * ASN1_GENERALIZEDTIME_cmp_time_t or ASN1_TIME_cmp_time_t,
1802 * so we go through ASN.1
1804 asn1_cmp_time
= X509_time_adj(NULL
, 0, cmp_time
);
1805 if (asn1_cmp_time
== NULL
)
1807 if (!ASN1_TIME_diff(&day
, &sec
, ctm
, asn1_cmp_time
))
1811 * X509_cmp_time comparison is <=.
1812 * The return value 0 is reserved for errors.
1814 ret
= (day
>= 0 && sec
>= 0) ? -1 : 1;
1817 ASN1_TIME_free(asn1_cmp_time
);
1821 ASN1_TIME
*X509_gmtime_adj(ASN1_TIME
*s
, long adj
)
1823 return X509_time_adj(s
, adj
, NULL
);
1826 ASN1_TIME
*X509_time_adj(ASN1_TIME
*s
, long offset_sec
, time_t *in_tm
)
1828 return X509_time_adj_ex(s
, 0, offset_sec
, in_tm
);
1831 ASN1_TIME
*X509_time_adj_ex(ASN1_TIME
*s
,
1832 int offset_day
, long offset_sec
, time_t *in_tm
)
1841 if (s
&& !(s
->flags
& ASN1_STRING_FLAG_MSTRING
)) {
1842 if (s
->type
== V_ASN1_UTCTIME
)
1843 return ASN1_UTCTIME_adj(s
, t
, offset_day
, offset_sec
);
1844 if (s
->type
== V_ASN1_GENERALIZEDTIME
)
1845 return ASN1_GENERALIZEDTIME_adj(s
, t
, offset_day
, offset_sec
);
1847 return ASN1_TIME_adj(s
, t
, offset_day
, offset_sec
);
1850 int X509_get_pubkey_parameters(EVP_PKEY
*pkey
, STACK_OF(X509
) *chain
)
1852 EVP_PKEY
*ktmp
= NULL
, *ktmp2
;
1855 if ((pkey
!= NULL
) && !EVP_PKEY_missing_parameters(pkey
))
1858 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
1859 ktmp
= X509_get0_pubkey(sk_X509_value(chain
, i
));
1861 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS
,
1862 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY
);
1865 if (!EVP_PKEY_missing_parameters(ktmp
))
1869 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS
,
1870 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN
);
1874 /* first, populate the other certs */
1875 for (j
= i
- 1; j
>= 0; j
--) {
1876 ktmp2
= X509_get0_pubkey(sk_X509_value(chain
, j
));
1877 EVP_PKEY_copy_parameters(ktmp2
, ktmp
);
1881 EVP_PKEY_copy_parameters(pkey
, ktmp
);
1885 /* Make a delta CRL as the diff between two full CRLs */
1887 X509_CRL
*X509_CRL_diff(X509_CRL
*base
, X509_CRL
*newer
,
1888 EVP_PKEY
*skey
, const EVP_MD
*md
, unsigned int flags
)
1890 X509_CRL
*crl
= NULL
;
1892 STACK_OF(X509_REVOKED
) *revs
= NULL
;
1893 /* CRLs can't be delta already */
1894 if (base
->base_crl_number
|| newer
->base_crl_number
) {
1895 X509err(X509_F_X509_CRL_DIFF
, X509_R_CRL_ALREADY_DELTA
);
1898 /* Base and new CRL must have a CRL number */
1899 if (!base
->crl_number
|| !newer
->crl_number
) {
1900 X509err(X509_F_X509_CRL_DIFF
, X509_R_NO_CRL_NUMBER
);
1903 /* Issuer names must match */
1904 if (X509_NAME_cmp(X509_CRL_get_issuer(base
), X509_CRL_get_issuer(newer
))) {
1905 X509err(X509_F_X509_CRL_DIFF
, X509_R_ISSUER_MISMATCH
);
1908 /* AKID and IDP must match */
1909 if (!crl_extension_match(base
, newer
, NID_authority_key_identifier
)) {
1910 X509err(X509_F_X509_CRL_DIFF
, X509_R_AKID_MISMATCH
);
1913 if (!crl_extension_match(base
, newer
, NID_issuing_distribution_point
)) {
1914 X509err(X509_F_X509_CRL_DIFF
, X509_R_IDP_MISMATCH
);
1917 /* Newer CRL number must exceed full CRL number */
1918 if (ASN1_INTEGER_cmp(newer
->crl_number
, base
->crl_number
) <= 0) {
1919 X509err(X509_F_X509_CRL_DIFF
, X509_R_NEWER_CRL_NOT_NEWER
);
1922 /* CRLs must verify */
1923 if (skey
&& (X509_CRL_verify(base
, skey
) <= 0 ||
1924 X509_CRL_verify(newer
, skey
) <= 0)) {
1925 X509err(X509_F_X509_CRL_DIFF
, X509_R_CRL_VERIFY_FAILURE
);
1928 /* Create new CRL */
1929 crl
= X509_CRL_new();
1930 if (crl
== NULL
|| !X509_CRL_set_version(crl
, 1))
1932 /* Set issuer name */
1933 if (!X509_CRL_set_issuer_name(crl
, X509_CRL_get_issuer(newer
)))
1936 if (!X509_CRL_set1_lastUpdate(crl
, X509_CRL_get0_lastUpdate(newer
)))
1938 if (!X509_CRL_set1_nextUpdate(crl
, X509_CRL_get0_nextUpdate(newer
)))
1941 /* Set base CRL number: must be critical */
1943 if (!X509_CRL_add1_ext_i2d(crl
, NID_delta_crl
, base
->crl_number
, 1, 0))
1947 * Copy extensions across from newest CRL to delta: this will set CRL
1948 * number to correct value too.
1951 for (i
= 0; i
< X509_CRL_get_ext_count(newer
); i
++) {
1952 X509_EXTENSION
*ext
;
1953 ext
= X509_CRL_get_ext(newer
, i
);
1954 if (!X509_CRL_add_ext(crl
, ext
, -1))
1958 /* Go through revoked entries, copying as needed */
1960 revs
= X509_CRL_get_REVOKED(newer
);
1962 for (i
= 0; i
< sk_X509_REVOKED_num(revs
); i
++) {
1963 X509_REVOKED
*rvn
, *rvtmp
;
1964 rvn
= sk_X509_REVOKED_value(revs
, i
);
1966 * Add only if not also in base. TODO: need something cleverer here
1967 * for some more complex CRLs covering multiple CAs.
1969 if (!X509_CRL_get0_by_serial(base
, &rvtmp
, &rvn
->serialNumber
)) {
1970 rvtmp
= X509_REVOKED_dup(rvn
);
1973 if (!X509_CRL_add0_revoked(crl
, rvtmp
)) {
1974 X509_REVOKED_free(rvtmp
);
1979 /* TODO: optionally prune deleted entries */
1981 if (skey
&& md
&& !X509_CRL_sign(crl
, skey
, md
))
1987 X509err(X509_F_X509_CRL_DIFF
, ERR_R_MALLOC_FAILURE
);
1992 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX
*ctx
, int idx
, void *data
)
1994 return CRYPTO_set_ex_data(&ctx
->ex_data
, idx
, data
);
1997 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX
*ctx
, int idx
)
1999 return CRYPTO_get_ex_data(&ctx
->ex_data
, idx
);
2002 int X509_STORE_CTX_get_error(X509_STORE_CTX
*ctx
)
2007 void X509_STORE_CTX_set_error(X509_STORE_CTX
*ctx
, int err
)
2012 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX
*ctx
)
2014 return ctx
->error_depth
;
2017 void X509_STORE_CTX_set_error_depth(X509_STORE_CTX
*ctx
, int depth
)
2019 ctx
->error_depth
= depth
;
2022 X509
*X509_STORE_CTX_get_current_cert(X509_STORE_CTX
*ctx
)
2024 return ctx
->current_cert
;
2027 void X509_STORE_CTX_set_current_cert(X509_STORE_CTX
*ctx
, X509
*x
)
2029 ctx
->current_cert
= x
;
2032 STACK_OF(X509
) *X509_STORE_CTX_get0_chain(X509_STORE_CTX
*ctx
)
2037 STACK_OF(X509
) *X509_STORE_CTX_get1_chain(X509_STORE_CTX
*ctx
)
2041 return X509_chain_up_ref(ctx
->chain
);
2044 X509
*X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX
*ctx
)
2046 return ctx
->current_issuer
;
2049 X509_CRL
*X509_STORE_CTX_get0_current_crl(X509_STORE_CTX
*ctx
)
2051 return ctx
->current_crl
;
2054 X509_STORE_CTX
*X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX
*ctx
)
2059 void X509_STORE_CTX_set_cert(X509_STORE_CTX
*ctx
, X509
*x
)
2064 void X509_STORE_CTX_set0_crls(X509_STORE_CTX
*ctx
, STACK_OF(X509_CRL
) *sk
)
2069 int X509_STORE_CTX_set_purpose(X509_STORE_CTX
*ctx
, int purpose
)
2072 * XXX: Why isn't this function always used to set the associated trust?
2073 * Should there even be a VPM->trust field at all? Or should the trust
2074 * always be inferred from the purpose by X509_STORE_CTX_init().
2076 return X509_STORE_CTX_purpose_inherit(ctx
, 0, purpose
, 0);
2079 int X509_STORE_CTX_set_trust(X509_STORE_CTX
*ctx
, int trust
)
2082 * XXX: See above, this function would only be needed when the default
2083 * trust for the purpose needs an override in a corner case.
2085 return X509_STORE_CTX_purpose_inherit(ctx
, 0, 0, trust
);
2089 * This function is used to set the X509_STORE_CTX purpose and trust values.
2090 * This is intended to be used when another structure has its own trust and
2091 * purpose values which (if set) will be inherited by the ctx. If they aren't
2092 * set then we will usually have a default purpose in mind which should then
2093 * be used to set the trust value. An example of this is SSL use: an SSL
2094 * structure will have its own purpose and trust settings which the
2095 * application can set: if they aren't set then we use the default of SSL
2099 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX
*ctx
, int def_purpose
,
2100 int purpose
, int trust
)
2103 /* If purpose not set use default */
2105 purpose
= def_purpose
;
2106 /* If we have a purpose then check it is valid */
2109 idx
= X509_PURPOSE_get_by_id(purpose
);
2111 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT
,
2112 X509_R_UNKNOWN_PURPOSE_ID
);
2115 ptmp
= X509_PURPOSE_get0(idx
);
2116 if (ptmp
->trust
== X509_TRUST_DEFAULT
) {
2117 idx
= X509_PURPOSE_get_by_id(def_purpose
);
2119 * XXX: In the two callers above def_purpose is always 0, which is
2120 * not a known value, so idx will always be -1. How is the
2121 * X509_TRUST_DEFAULT case actually supposed to be handled?
2124 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT
,
2125 X509_R_UNKNOWN_PURPOSE_ID
);
2128 ptmp
= X509_PURPOSE_get0(idx
);
2130 /* If trust not set then get from purpose default */
2132 trust
= ptmp
->trust
;
2135 idx
= X509_TRUST_get_by_id(trust
);
2137 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT
,
2138 X509_R_UNKNOWN_TRUST_ID
);
2143 if (purpose
&& !ctx
->param
->purpose
)
2144 ctx
->param
->purpose
= purpose
;
2145 if (trust
&& !ctx
->param
->trust
)
2146 ctx
->param
->trust
= trust
;
2150 X509_STORE_CTX
*X509_STORE_CTX_new(void)
2152 X509_STORE_CTX
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
2155 X509err(X509_F_X509_STORE_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
2161 void X509_STORE_CTX_free(X509_STORE_CTX
*ctx
)
2166 X509_STORE_CTX_cleanup(ctx
);
2170 int X509_STORE_CTX_init(X509_STORE_CTX
*ctx
, X509_STORE
*store
, X509
*x509
,
2171 STACK_OF(X509
) *chain
)
2177 ctx
->untrusted
= chain
;
2179 ctx
->num_untrusted
= 0;
2180 ctx
->other_ctx
= NULL
;
2184 ctx
->explicit_policy
= 0;
2185 ctx
->error_depth
= 0;
2186 ctx
->current_cert
= NULL
;
2187 ctx
->current_issuer
= NULL
;
2188 ctx
->current_crl
= NULL
;
2189 ctx
->current_crl_score
= 0;
2190 ctx
->current_reasons
= 0;
2194 ctx
->bare_ta_signed
= 0;
2195 /* Zero ex_data to make sure we're cleanup-safe */
2196 memset(&ctx
->ex_data
, 0, sizeof(ctx
->ex_data
));
2198 /* store->cleanup is always 0 in OpenSSL, if set must be idempotent */
2200 ctx
->cleanup
= store
->cleanup
;
2204 if (store
&& store
->check_issued
)
2205 ctx
->check_issued
= store
->check_issued
;
2207 ctx
->check_issued
= check_issued
;
2209 if (store
&& store
->get_issuer
)
2210 ctx
->get_issuer
= store
->get_issuer
;
2212 ctx
->get_issuer
= X509_STORE_CTX_get1_issuer
;
2214 if (store
&& store
->verify_cb
)
2215 ctx
->verify_cb
= store
->verify_cb
;
2217 ctx
->verify_cb
= null_callback
;
2219 if (store
&& store
->verify
)
2220 ctx
->verify
= store
->verify
;
2222 ctx
->verify
= internal_verify
;
2224 if (store
&& store
->check_revocation
)
2225 ctx
->check_revocation
= store
->check_revocation
;
2227 ctx
->check_revocation
= check_revocation
;
2229 if (store
&& store
->get_crl
)
2230 ctx
->get_crl
= store
->get_crl
;
2232 ctx
->get_crl
= NULL
;
2234 if (store
&& store
->check_crl
)
2235 ctx
->check_crl
= store
->check_crl
;
2237 ctx
->check_crl
= check_crl
;
2239 if (store
&& store
->cert_crl
)
2240 ctx
->cert_crl
= store
->cert_crl
;
2242 ctx
->cert_crl
= cert_crl
;
2244 if (store
&& store
->check_policy
)
2245 ctx
->check_policy
= store
->check_policy
;
2247 ctx
->check_policy
= check_policy
;
2249 if (store
&& store
->lookup_certs
)
2250 ctx
->lookup_certs
= store
->lookup_certs
;
2252 ctx
->lookup_certs
= X509_STORE_CTX_get1_certs
;
2254 if (store
&& store
->lookup_crls
)
2255 ctx
->lookup_crls
= store
->lookup_crls
;
2257 ctx
->lookup_crls
= X509_STORE_CTX_get1_crls
;
2259 ctx
->param
= X509_VERIFY_PARAM_new();
2260 if (ctx
->param
== NULL
) {
2261 X509err(X509_F_X509_STORE_CTX_INIT
, ERR_R_MALLOC_FAILURE
);
2266 * Inherit callbacks and flags from X509_STORE if not set use defaults.
2269 ret
= X509_VERIFY_PARAM_inherit(ctx
->param
, store
->param
);
2271 ctx
->param
->inh_flags
|= X509_VP_FLAG_DEFAULT
| X509_VP_FLAG_ONCE
;
2274 ret
= X509_VERIFY_PARAM_inherit(ctx
->param
,
2275 X509_VERIFY_PARAM_lookup("default"));
2278 X509err(X509_F_X509_STORE_CTX_INIT
, ERR_R_MALLOC_FAILURE
);
2283 * XXX: For now, continue to inherit trust from VPM, but infer from the
2284 * purpose if this still yields the default value.
2286 if (ctx
->param
->trust
== X509_TRUST_DEFAULT
) {
2287 int idx
= X509_PURPOSE_get_by_id(ctx
->param
->purpose
);
2288 X509_PURPOSE
*xp
= X509_PURPOSE_get0(idx
);
2291 ctx
->param
->trust
= X509_PURPOSE_get_trust(xp
);
2294 if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX
, ctx
,
2297 X509err(X509_F_X509_STORE_CTX_INIT
, ERR_R_MALLOC_FAILURE
);
2301 * On error clean up allocated storage, if the store context was not
2302 * allocated with X509_STORE_CTX_new() this is our last chance to do so.
2304 X509_STORE_CTX_cleanup(ctx
);
2309 * Set alternative lookup method: just a STACK of trusted certificates. This
2310 * avoids X509_STORE nastiness where it isn't needed.
2312 void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
)
2314 ctx
->other_ctx
= sk
;
2315 ctx
->get_issuer
= get_issuer_sk
;
2316 ctx
->lookup_certs
= lookup_certs_sk
;
2319 void X509_STORE_CTX_cleanup(X509_STORE_CTX
*ctx
)
2322 * We need to be idempotent because, unfortunately, free() also calls
2323 * cleanup(), so the natural call sequence new(), init(), cleanup(), free()
2324 * calls cleanup() for the same object twice! Thus we must zero the
2325 * pointers below after they're freed!
2327 /* Seems to always be 0 in OpenSSL, do this at most once. */
2328 if (ctx
->cleanup
!= NULL
) {
2330 ctx
->cleanup
= NULL
;
2332 if (ctx
->param
!= NULL
) {
2333 if (ctx
->parent
== NULL
)
2334 X509_VERIFY_PARAM_free(ctx
->param
);
2337 X509_policy_tree_free(ctx
->tree
);
2339 sk_X509_pop_free(ctx
->chain
, X509_free
);
2341 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX
, ctx
, &(ctx
->ex_data
));
2342 memset(&ctx
->ex_data
, 0, sizeof(ctx
->ex_data
));
2345 void X509_STORE_CTX_set_depth(X509_STORE_CTX
*ctx
, int depth
)
2347 X509_VERIFY_PARAM_set_depth(ctx
->param
, depth
);
2350 void X509_STORE_CTX_set_flags(X509_STORE_CTX
*ctx
, unsigned long flags
)
2352 X509_VERIFY_PARAM_set_flags(ctx
->param
, flags
);
2355 void X509_STORE_CTX_set_time(X509_STORE_CTX
*ctx
, unsigned long flags
,
2358 X509_VERIFY_PARAM_set_time(ctx
->param
, t
);
2361 X509
*X509_STORE_CTX_get0_cert(X509_STORE_CTX
*ctx
)
2366 STACK_OF(X509
) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX
*ctx
)
2368 return ctx
->untrusted
;
2371 void X509_STORE_CTX_set0_untrusted(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
)
2373 ctx
->untrusted
= sk
;
2376 void X509_STORE_CTX_set0_verified_chain(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
)
2378 sk_X509_pop_free(ctx
->chain
, X509_free
);
2382 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX
*ctx
,
2383 X509_STORE_CTX_verify_cb verify_cb
)
2385 ctx
->verify_cb
= verify_cb
;
2388 X509_STORE_CTX_verify_cb
X509_STORE_CTX_get_verify_cb(X509_STORE_CTX
*ctx
)
2390 return ctx
->verify_cb
;
2393 void X509_STORE_CTX_set_verify(X509_STORE_CTX
*ctx
,
2394 X509_STORE_CTX_verify_fn verify
)
2396 ctx
->verify
= verify
;
2399 X509_STORE_CTX_verify_fn
X509_STORE_CTX_get_verify(X509_STORE_CTX
*ctx
)
2404 X509_STORE_CTX_get_issuer_fn
X509_STORE_CTX_get_get_issuer(X509_STORE_CTX
*ctx
)
2406 return ctx
->get_issuer
;
2409 X509_STORE_CTX_check_issued_fn
X509_STORE_CTX_get_check_issued(X509_STORE_CTX
*ctx
)
2411 return ctx
->check_issued
;
2414 X509_STORE_CTX_check_revocation_fn
X509_STORE_CTX_get_check_revocation(X509_STORE_CTX
*ctx
)
2416 return ctx
->check_revocation
;
2419 X509_STORE_CTX_get_crl_fn
X509_STORE_CTX_get_get_crl(X509_STORE_CTX
*ctx
)
2421 return ctx
->get_crl
;
2424 X509_STORE_CTX_check_crl_fn
X509_STORE_CTX_get_check_crl(X509_STORE_CTX
*ctx
)
2426 return ctx
->check_crl
;
2429 X509_STORE_CTX_cert_crl_fn
X509_STORE_CTX_get_cert_crl(X509_STORE_CTX
*ctx
)
2431 return ctx
->cert_crl
;
2434 X509_STORE_CTX_check_policy_fn
X509_STORE_CTX_get_check_policy(X509_STORE_CTX
*ctx
)
2436 return ctx
->check_policy
;
2439 X509_STORE_CTX_lookup_certs_fn
X509_STORE_CTX_get_lookup_certs(X509_STORE_CTX
*ctx
)
2441 return ctx
->lookup_certs
;
2444 X509_STORE_CTX_lookup_crls_fn
X509_STORE_CTX_get_lookup_crls(X509_STORE_CTX
*ctx
)
2446 return ctx
->lookup_crls
;
2449 X509_STORE_CTX_cleanup_fn
X509_STORE_CTX_get_cleanup(X509_STORE_CTX
*ctx
)
2451 return ctx
->cleanup
;
2454 X509_POLICY_TREE
*X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX
*ctx
)
2459 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX
*ctx
)
2461 return ctx
->explicit_policy
;
2464 int X509_STORE_CTX_get_num_untrusted(X509_STORE_CTX
*ctx
)
2466 return ctx
->num_untrusted
;
2469 int X509_STORE_CTX_set_default(X509_STORE_CTX
*ctx
, const char *name
)
2471 const X509_VERIFY_PARAM
*param
;
2472 param
= X509_VERIFY_PARAM_lookup(name
);
2475 return X509_VERIFY_PARAM_inherit(ctx
->param
, param
);
2478 X509_VERIFY_PARAM
*X509_STORE_CTX_get0_param(X509_STORE_CTX
*ctx
)
2483 void X509_STORE_CTX_set0_param(X509_STORE_CTX
*ctx
, X509_VERIFY_PARAM
*param
)
2485 X509_VERIFY_PARAM_free(ctx
->param
);
2489 void X509_STORE_CTX_set0_dane(X509_STORE_CTX
*ctx
, SSL_DANE
*dane
)
2494 static unsigned char *dane_i2d(
2497 unsigned int *i2dlen
)
2499 unsigned char *buf
= NULL
;
2503 * Extract ASN.1 DER form of certificate or public key.
2506 case DANETLS_SELECTOR_CERT
:
2507 len
= i2d_X509(cert
, &buf
);
2509 case DANETLS_SELECTOR_SPKI
:
2510 len
= i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert
), &buf
);
2513 X509err(X509_F_DANE_I2D
, X509_R_BAD_SELECTOR
);
2517 if (len
< 0 || buf
== NULL
) {
2518 X509err(X509_F_DANE_I2D
, ERR_R_MALLOC_FAILURE
);
2522 *i2dlen
= (unsigned int)len
;
2526 #define DANETLS_NONE 256 /* impossible uint8_t */
2528 static int dane_match(X509_STORE_CTX
*ctx
, X509
*cert
, int depth
)
2530 SSL_DANE
*dane
= ctx
->dane
;
2531 unsigned usage
= DANETLS_NONE
;
2532 unsigned selector
= DANETLS_NONE
;
2533 unsigned ordinal
= DANETLS_NONE
;
2534 unsigned mtype
= DANETLS_NONE
;
2535 unsigned char *i2dbuf
= NULL
;
2536 unsigned int i2dlen
= 0;
2537 unsigned char mdbuf
[EVP_MAX_MD_SIZE
];
2538 unsigned char *cmpbuf
= NULL
;
2539 unsigned int cmplen
= 0;
2543 danetls_record
*t
= NULL
;
2546 mask
= (depth
== 0) ? DANETLS_EE_MASK
: DANETLS_TA_MASK
;
2549 * The trust store is not applicable with DANE-TA(2)
2551 if (depth
>= ctx
->num_untrusted
)
2552 mask
&= DANETLS_PKIX_MASK
;
2555 * If we've previously matched a PKIX-?? record, no need to test any
2556 * further PKIX-?? records, it remains to just build the PKIX chain.
2557 * Had the match been a DANE-?? record, we'd be done already.
2559 if (dane
->mdpth
>= 0)
2560 mask
&= ~DANETLS_PKIX_MASK
;
2563 * https://tools.ietf.org/html/rfc7671#section-5.1
2564 * https://tools.ietf.org/html/rfc7671#section-5.2
2565 * https://tools.ietf.org/html/rfc7671#section-5.3
2566 * https://tools.ietf.org/html/rfc7671#section-5.4
2568 * We handle DANE-EE(3) records first as they require no chain building
2569 * and no expiration or hostname checks. We also process digests with
2570 * higher ordinals first and ignore lower priorities except Full(0) which
2571 * is always processed (last). If none match, we then process PKIX-EE(1).
2573 * NOTE: This relies on DANE usages sorting before the corresponding PKIX
2574 * usages in SSL_dane_tlsa_add(), and also on descending sorting of digest
2575 * priorities. See twin comment in ssl/ssl_lib.c.
2577 * We expect that most TLSA RRsets will have just a single usage, so we
2578 * don't go out of our way to cache multiple selector-specific i2d buffers
2579 * across usages, but if the selector happens to remain the same as switch
2580 * usages, that's OK. Thus, a set of "3 1 1", "3 0 1", "1 1 1", "1 0 1",
2581 * records would result in us generating each of the certificate and public
2582 * key DER forms twice, but more typically we'd just see multiple "3 1 1"
2583 * or multiple "3 0 1" records.
2585 * As soon as we find a match at any given depth, we stop, because either
2586 * we've matched a DANE-?? record and the peer is authenticated, or, after
2587 * exhausting all DANE-?? records, we've matched a PKIX-?? record, which is
2588 * sufficient for DANE, and what remains to do is ordinary PKIX validation.
2590 recnum
= (dane
->umask
& mask
) ? sk_danetls_record_num(dane
->trecs
) : 0;
2591 for (i
= 0; matched
== 0 && i
< recnum
; ++i
) {
2592 t
= sk_danetls_record_value(dane
->trecs
, i
);
2593 if ((DANETLS_USAGE_BIT(t
->usage
) & mask
) == 0)
2595 if (t
->usage
!= usage
) {
2598 /* Reset digest agility for each usage/selector pair */
2599 mtype
= DANETLS_NONE
;
2600 ordinal
= dane
->dctx
->mdord
[t
->mtype
];
2602 if (t
->selector
!= selector
) {
2603 selector
= t
->selector
;
2605 /* Update per-selector state */
2606 OPENSSL_free(i2dbuf
);
2607 i2dbuf
= dane_i2d(cert
, selector
, &i2dlen
);
2611 /* Reset digest agility for each usage/selector pair */
2612 mtype
= DANETLS_NONE
;
2613 ordinal
= dane
->dctx
->mdord
[t
->mtype
];
2614 } else if (t
->mtype
!= DANETLS_MATCHING_FULL
) {
2618 * <https://tools.ietf.org/html/rfc7671#section-9>
2620 * For a fixed selector, after processing all records with the
2621 * highest mtype ordinal, ignore all mtypes with lower ordinals
2622 * other than "Full".
2624 if (dane
->dctx
->mdord
[t
->mtype
] < ordinal
)
2629 * Each time we hit a (new selector or) mtype, re-compute the relevant
2630 * digest, more complex caching is not worth the code space.
2632 if (t
->mtype
!= mtype
) {
2633 const EVP_MD
*md
= dane
->dctx
->mdevp
[mtype
= t
->mtype
];
2639 if (!EVP_Digest(i2dbuf
, i2dlen
, cmpbuf
, &cmplen
, md
, 0)) {
2647 * Squirrel away the certificate and depth if we have a match. Any
2648 * DANE match is dispositive, but with PKIX we still need to build a
2651 if (cmplen
== t
->dlen
&&
2652 memcmp(cmpbuf
, t
->data
, cmplen
) == 0) {
2653 if (DANETLS_USAGE_BIT(usage
) & DANETLS_DANE_MASK
)
2655 if (matched
|| dane
->mdpth
< 0) {
2656 dane
->mdpth
= depth
;
2658 OPENSSL_free(dane
->mcert
);
2666 /* Clear the one-element DER cache */
2667 OPENSSL_free(i2dbuf
);
2671 static int check_dane_issuer(X509_STORE_CTX
*ctx
, int depth
)
2673 SSL_DANE
*dane
= ctx
->dane
;
2677 if (!DANETLS_HAS_TA(dane
) || depth
== 0)
2678 return X509_TRUST_UNTRUSTED
;
2681 * Record any DANE trust-anchor matches, for the first depth to test, if
2682 * there's one at that depth. (This'll be false for length 1 chains looking
2683 * for an exact match for the leaf certificate).
2685 cert
= sk_X509_value(ctx
->chain
, depth
);
2686 if (cert
!= NULL
&& (matched
= dane_match(ctx
, cert
, depth
)) < 0)
2687 return X509_TRUST_REJECTED
;
2689 ctx
->num_untrusted
= depth
- 1;
2690 return X509_TRUST_TRUSTED
;
2693 return X509_TRUST_UNTRUSTED
;
2696 static int check_dane_pkeys(X509_STORE_CTX
*ctx
)
2698 SSL_DANE
*dane
= ctx
->dane
;
2700 int num
= ctx
->num_untrusted
;
2701 X509
*cert
= sk_X509_value(ctx
->chain
, num
- 1);
2702 int recnum
= sk_danetls_record_num(dane
->trecs
);
2705 for (i
= 0; i
< recnum
; ++i
) {
2706 t
= sk_danetls_record_value(dane
->trecs
, i
);
2707 if (t
->usage
!= DANETLS_USAGE_DANE_TA
||
2708 t
->selector
!= DANETLS_SELECTOR_SPKI
||
2709 t
->mtype
!= DANETLS_MATCHING_FULL
||
2710 X509_verify(cert
, t
->spki
) <= 0)
2713 /* Clear any PKIX-?? matches that failed to extend to a full chain */
2714 X509_free(dane
->mcert
);
2717 /* Record match via a bare TA public key */
2718 ctx
->bare_ta_signed
= 1;
2719 dane
->mdpth
= num
- 1;
2722 /* Prune any excess chain certificates */
2723 num
= sk_X509_num(ctx
->chain
);
2724 for (; num
> ctx
->num_untrusted
; --num
)
2725 X509_free(sk_X509_pop(ctx
->chain
));
2727 return X509_TRUST_TRUSTED
;
2730 return X509_TRUST_UNTRUSTED
;
2733 static void dane_reset(SSL_DANE
*dane
)
2736 * Reset state to verify another chain, or clear after failure.
2738 X509_free(dane
->mcert
);
2745 static int check_leaf_suiteb(X509_STORE_CTX
*ctx
, X509
*cert
)
2747 int err
= X509_chain_check_suiteb(NULL
, cert
, NULL
, ctx
->param
->flags
);
2749 if (err
== X509_V_OK
)
2751 return verify_cb_cert(ctx
, cert
, 0, err
);
2754 static int dane_verify(X509_STORE_CTX
*ctx
)
2756 X509
*cert
= ctx
->cert
;
2757 SSL_DANE
*dane
= ctx
->dane
;
2764 * When testing the leaf certificate, if we match a DANE-EE(3) record,
2765 * dane_match() returns 1 and we're done. If however we match a PKIX-EE(1)
2766 * record, the match depth and matching TLSA record are recorded, but the
2767 * return value is 0, because we still need to find a PKIX trust-anchor.
2768 * Therefore, when DANE authentication is enabled (required), we're done
2770 * + matched < 0, internal error.
2771 * + matched == 1, we matched a DANE-EE(3) record
2772 * + matched == 0, mdepth < 0 (no PKIX-EE match) and there are no
2773 * DANE-TA(2) or PKIX-TA(0) to test.
2775 matched
= dane_match(ctx
, ctx
->cert
, 0);
2776 done
= matched
!= 0 || (!DANETLS_HAS_TA(dane
) && dane
->mdpth
< 0);
2779 X509_get_pubkey_parameters(NULL
, ctx
->chain
);
2782 /* Callback invoked as needed */
2783 if (!check_leaf_suiteb(ctx
, cert
))
2785 /* Callback invoked as needed */
2786 if ((dane
->flags
& DANE_FLAG_NO_DANE_EE_NAMECHECKS
) == 0 &&
2789 /* Bypass internal_verify(), issue depth 0 success callback */
2790 ctx
->error_depth
= 0;
2791 ctx
->current_cert
= cert
;
2792 return ctx
->verify_cb(1, ctx
);
2796 ctx
->error_depth
= 0;
2797 ctx
->current_cert
= cert
;
2798 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
2803 /* Fail early, TA-based success is not possible */
2804 if (!check_leaf_suiteb(ctx
, cert
))
2806 return verify_cb_cert(ctx
, cert
, 0, X509_V_ERR_DANE_NO_MATCH
);
2810 * Chain verification for usages 0/1/2. TLSA record matching of depth > 0
2811 * certificates happens in-line with building the rest of the chain.
2813 return verify_chain(ctx
);
2816 /* Get issuer, without duplicate suppression */
2817 static int get_issuer(X509
**issuer
, X509_STORE_CTX
*ctx
, X509
*cert
)
2819 STACK_OF(X509
) *saved_chain
= ctx
->chain
;
2823 ok
= ctx
->get_issuer(issuer
, ctx
, cert
);
2824 ctx
->chain
= saved_chain
;
2829 static int build_chain(X509_STORE_CTX
*ctx
)
2831 SSL_DANE
*dane
= ctx
->dane
;
2832 int num
= sk_X509_num(ctx
->chain
);
2833 X509
*cert
= sk_X509_value(ctx
->chain
, num
- 1);
2834 int ss
= cert_self_signed(cert
);
2835 STACK_OF(X509
) *sktmp
= NULL
;
2836 unsigned int search
;
2837 int may_trusted
= 0;
2838 int may_alternate
= 0;
2839 int trust
= X509_TRUST_UNTRUSTED
;
2840 int alt_untrusted
= 0;
2845 /* Our chain starts with a single untrusted element. */
2846 if (!ossl_assert(num
== 1 && ctx
->num_untrusted
== num
)) {
2847 X509err(X509_F_BUILD_CHAIN
, ERR_R_INTERNAL_ERROR
);
2848 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
2852 #define S_DOUNTRUSTED (1 << 0) /* Search untrusted chain */
2853 #define S_DOTRUSTED (1 << 1) /* Search trusted store */
2854 #define S_DOALTERNATE (1 << 2) /* Retry with pruned alternate chain */
2856 * Set up search policy, untrusted if possible, trusted-first if enabled.
2857 * If we're doing DANE and not doing PKIX-TA/PKIX-EE, we never look in the
2858 * trust_store, otherwise we might look there first. If not trusted-first,
2859 * and alternate chains are not disabled, try building an alternate chain
2860 * if no luck with untrusted first.
2862 search
= (ctx
->untrusted
!= NULL
) ? S_DOUNTRUSTED
: 0;
2863 if (DANETLS_HAS_PKIX(dane
) || !DANETLS_HAS_DANE(dane
)) {
2864 if (search
== 0 || ctx
->param
->flags
& X509_V_FLAG_TRUSTED_FIRST
)
2865 search
|= S_DOTRUSTED
;
2866 else if (!(ctx
->param
->flags
& X509_V_FLAG_NO_ALT_CHAINS
))
2872 * Shallow-copy the stack of untrusted certificates (with TLS, this is
2873 * typically the content of the peer's certificate message) so can make
2874 * multiple passes over it, while free to remove elements as we go.
2876 if (ctx
->untrusted
&& (sktmp
= sk_X509_dup(ctx
->untrusted
)) == NULL
) {
2877 X509err(X509_F_BUILD_CHAIN
, ERR_R_MALLOC_FAILURE
);
2878 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
2883 * If we got any "DANE-TA(2) Cert(0) Full(0)" trust-anchors from DNS, add
2884 * them to our working copy of the untrusted certificate stack. Since the
2885 * caller of X509_STORE_CTX_init() may have provided only a leaf cert with
2886 * no corresponding stack of untrusted certificates, we may need to create
2887 * an empty stack first. [ At present only the ssl library provides DANE
2888 * support, and ssl_verify_cert_chain() always provides a non-null stack
2889 * containing at least the leaf certificate, but we must be prepared for
2892 if (DANETLS_ENABLED(dane
) && dane
->certs
!= NULL
) {
2893 if (sktmp
== NULL
&& (sktmp
= sk_X509_new_null()) == NULL
) {
2894 X509err(X509_F_BUILD_CHAIN
, ERR_R_MALLOC_FAILURE
);
2895 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
2898 for (i
= 0; i
< sk_X509_num(dane
->certs
); ++i
) {
2899 if (!sk_X509_push(sktmp
, sk_X509_value(dane
->certs
, i
))) {
2900 sk_X509_free(sktmp
);
2901 X509err(X509_F_BUILD_CHAIN
, ERR_R_MALLOC_FAILURE
);
2902 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
2909 * Still absurdly large, but arithmetically safe, a lower hard upper bound
2910 * might be reasonable.
2912 if (ctx
->param
->depth
> INT_MAX
/2)
2913 ctx
->param
->depth
= INT_MAX
/2;
2916 * Try to Extend the chain until we reach an ultimately trusted issuer.
2917 * Build chains up to one longer the limit, later fail if we hit the limit,
2918 * with an X509_V_ERR_CERT_CHAIN_TOO_LONG error code.
2920 depth
= ctx
->param
->depth
+ 1;
2922 while (search
!= 0) {
2927 * Look in the trust store if enabled for first lookup, or we've run
2928 * out of untrusted issuers and search here is not disabled. When we
2929 * reach the depth limit, we stop extending the chain, if by that point
2930 * we've not found a trust-anchor, any trusted chain would be too long.
2932 * The error reported to the application verify callback is at the
2933 * maximal valid depth with the current certificate equal to the last
2934 * not ultimately-trusted issuer. For example, with verify_depth = 0,
2935 * the callback will report errors at depth=1 when the immediate issuer
2936 * of the leaf certificate is not a trust anchor. No attempt will be
2937 * made to locate an issuer for that certificate, since such a chain
2938 * would be a-priori too long.
2940 if ((search
& S_DOTRUSTED
) != 0) {
2941 i
= num
= sk_X509_num(ctx
->chain
);
2942 if ((search
& S_DOALTERNATE
) != 0) {
2944 * As high up the chain as we can, look for an alternative
2945 * trusted issuer of an untrusted certificate that currently
2946 * has an untrusted issuer. We use the alt_untrusted variable
2947 * to track how far up the chain we find the first match. It
2948 * is only if and when we find a match, that we prune the chain
2949 * and reset ctx->num_untrusted to the reduced count of
2950 * untrusted certificates. While we're searching for such a
2951 * match (which may never be found), it is neither safe nor
2952 * wise to preemptively modify either the chain or
2953 * ctx->num_untrusted.
2955 * Note, like ctx->num_untrusted, alt_untrusted is a count of
2956 * untrusted certificates, not a "depth".
2960 x
= sk_X509_value(ctx
->chain
, i
-1);
2962 ok
= (depth
< num
) ? 0 : get_issuer(&xtmp
, ctx
, x
);
2965 trust
= X509_TRUST_REJECTED
;
2966 ctx
->error
= X509_V_ERR_STORE_LOOKUP
;
2973 * Alternative trusted issuer for a mid-chain untrusted cert?
2974 * Pop the untrusted cert's successors and retry. We might now
2975 * be able to complete a valid chain via the trust store. Note
2976 * that despite the current trust-store match we might still
2977 * fail complete the chain to a suitable trust-anchor, in which
2978 * case we may prune some more untrusted certificates and try
2979 * again. Thus the S_DOALTERNATE bit may yet be turned on
2980 * again with an even shorter untrusted chain!
2982 * If in the process we threw away our matching PKIX-TA trust
2983 * anchor, reset DANE trust. We might find a suitable trusted
2984 * certificate among the ones from the trust store.
2986 if ((search
& S_DOALTERNATE
) != 0) {
2987 if (!ossl_assert(num
> i
&& i
> 0 && ss
== 0)) {
2988 X509err(X509_F_BUILD_CHAIN
, ERR_R_INTERNAL_ERROR
);
2990 trust
= X509_TRUST_REJECTED
;
2991 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
2995 search
&= ~S_DOALTERNATE
;
2996 for (; num
> i
; --num
)
2997 X509_free(sk_X509_pop(ctx
->chain
));
2998 ctx
->num_untrusted
= num
;
3000 if (DANETLS_ENABLED(dane
) &&
3001 dane
->mdpth
>= ctx
->num_untrusted
) {
3003 X509_free(dane
->mcert
);
3006 if (DANETLS_ENABLED(dane
) &&
3007 dane
->pdpth
>= ctx
->num_untrusted
)
3012 * Self-signed untrusted certificates get replaced by their
3013 * trusted matching issuer. Otherwise, grow the chain.
3016 if (!sk_X509_push(ctx
->chain
, x
= xtmp
)) {
3018 X509err(X509_F_BUILD_CHAIN
, ERR_R_MALLOC_FAILURE
);
3019 trust
= X509_TRUST_REJECTED
;
3020 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
3024 ss
= cert_self_signed(x
);
3025 } else if (num
== ctx
->num_untrusted
) {
3027 * We have a self-signed certificate that has the same
3028 * subject name (and perhaps keyid and/or serial number) as
3029 * a trust-anchor. We must have an exact match to avoid
3030 * possible impersonation via key substitution etc.
3032 if (X509_cmp(x
, xtmp
) != 0) {
3033 /* Self-signed untrusted mimic. */
3038 ctx
->num_untrusted
= --num
;
3039 (void) sk_X509_set(ctx
->chain
, num
, x
= xtmp
);
3044 * We've added a new trusted certificate to the chain, recheck
3045 * trust. If not done, and not self-signed look deeper.
3046 * Whether or not we're doing "trusted first", we no longer
3047 * look for untrusted certificates from the peer's chain.
3049 * At this point ctx->num_trusted and num must reflect the
3050 * correct number of untrusted certificates, since the DANE
3051 * logic in check_trust() depends on distinguishing CAs from
3052 * "the wire" from CAs from the trust store. In particular, the
3053 * certificate at depth "num" should be the new trusted
3054 * certificate with ctx->num_untrusted <= num.
3057 if (!ossl_assert(ctx
->num_untrusted
<= num
)) {
3058 X509err(X509_F_BUILD_CHAIN
, ERR_R_INTERNAL_ERROR
);
3059 trust
= X509_TRUST_REJECTED
;
3060 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3064 search
&= ~S_DOUNTRUSTED
;
3065 switch (trust
= check_trust(ctx
, num
)) {
3066 case X509_TRUST_TRUSTED
:
3067 case X509_TRUST_REJECTED
:
3077 * No dispositive decision, and either self-signed or no match, if
3078 * we were doing untrusted-first, and alt-chains are not disabled,
3079 * do that, by repeatedly losing one untrusted element at a time,
3080 * and trying to extend the shorted chain.
3082 if ((search
& S_DOUNTRUSTED
) == 0) {
3083 /* Continue search for a trusted issuer of a shorter chain? */
3084 if ((search
& S_DOALTERNATE
) != 0 && --alt_untrusted
> 0)
3086 /* Still no luck and no fallbacks left? */
3087 if (!may_alternate
|| (search
& S_DOALTERNATE
) != 0 ||
3088 ctx
->num_untrusted
< 2)
3090 /* Search for a trusted issuer of a shorter chain */
3091 search
|= S_DOALTERNATE
;
3092 alt_untrusted
= ctx
->num_untrusted
- 1;
3098 * Extend chain with peer-provided certificates
3100 if ((search
& S_DOUNTRUSTED
) != 0) {
3101 num
= sk_X509_num(ctx
->chain
);
3102 if (!ossl_assert(num
== ctx
->num_untrusted
)) {
3103 X509err(X509_F_BUILD_CHAIN
, ERR_R_INTERNAL_ERROR
);
3104 trust
= X509_TRUST_REJECTED
;
3105 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3109 x
= sk_X509_value(ctx
->chain
, num
-1);
3112 * Once we run out of untrusted issuers, we stop looking for more
3113 * and start looking only in the trust store if enabled.
3115 xtmp
= (ss
|| depth
< num
) ? NULL
: find_issuer(ctx
, sktmp
, x
);
3117 search
&= ~S_DOUNTRUSTED
;
3119 search
|= S_DOTRUSTED
;
3123 /* Drop this issuer from future consideration */
3124 (void) sk_X509_delete_ptr(sktmp
, xtmp
);
3126 if (!sk_X509_push(ctx
->chain
, xtmp
)) {
3127 X509err(X509_F_BUILD_CHAIN
, ERR_R_MALLOC_FAILURE
);
3128 trust
= X509_TRUST_REJECTED
;
3129 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
3134 X509_up_ref(x
= xtmp
);
3135 ++ctx
->num_untrusted
;
3136 ss
= cert_self_signed(xtmp
);
3139 * Check for DANE-TA trust of the topmost untrusted certificate.
3141 switch (trust
= check_dane_issuer(ctx
, ctx
->num_untrusted
- 1)) {
3142 case X509_TRUST_TRUSTED
:
3143 case X509_TRUST_REJECTED
:
3149 sk_X509_free(sktmp
);
3152 * Last chance to make a trusted chain, either bare DANE-TA public-key
3153 * signers, or else direct leaf PKIX trust.
3155 num
= sk_X509_num(ctx
->chain
);
3157 if (trust
== X509_TRUST_UNTRUSTED
&& DANETLS_HAS_DANE_TA(dane
))
3158 trust
= check_dane_pkeys(ctx
);
3159 if (trust
== X509_TRUST_UNTRUSTED
&& num
== ctx
->num_untrusted
)
3160 trust
= check_trust(ctx
, num
);
3164 case X509_TRUST_TRUSTED
:
3166 case X509_TRUST_REJECTED
:
3167 /* Callback already issued */
3169 case X509_TRUST_UNTRUSTED
:
3171 num
= sk_X509_num(ctx
->chain
);
3173 return verify_cb_cert(ctx
, NULL
, num
-1,
3174 X509_V_ERR_CERT_CHAIN_TOO_LONG
);
3175 if (DANETLS_ENABLED(dane
) &&
3176 (!DANETLS_HAS_PKIX(dane
) || dane
->pdpth
>= 0))
3177 return verify_cb_cert(ctx
, NULL
, num
-1, X509_V_ERR_DANE_NO_MATCH
);
3178 if (ss
&& sk_X509_num(ctx
->chain
) == 1)
3179 return verify_cb_cert(ctx
, NULL
, num
-1,
3180 X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
);
3182 return verify_cb_cert(ctx
, NULL
, num
-1,
3183 X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
);
3184 if (ctx
->num_untrusted
< num
)
3185 return verify_cb_cert(ctx
, NULL
, num
-1,
3186 X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
);
3187 return verify_cb_cert(ctx
, NULL
, num
-1,
3188 X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
);
3192 static const int minbits_table
[] = { 80, 112, 128, 192, 256 };
3193 static const int NUM_AUTH_LEVELS
= OSSL_NELEM(minbits_table
);
3196 * Check whether the public key of ``cert`` meets the security level of
3199 * Returns 1 on success, 0 otherwise.
3201 static int check_key_level(X509_STORE_CTX
*ctx
, X509
*cert
)
3203 EVP_PKEY
*pkey
= X509_get0_pubkey(cert
);
3204 int level
= ctx
->param
->auth_level
;
3206 /* Unsupported or malformed keys are not secure */
3212 if (level
> NUM_AUTH_LEVELS
)
3213 level
= NUM_AUTH_LEVELS
;
3215 return EVP_PKEY_security_bits(pkey
) >= minbits_table
[level
- 1];
3219 * Check whether the signature digest algorithm of ``cert`` meets the security
3220 * level of ``ctx``. Should not be checked for trust anchors (whether
3221 * self-signed or otherwise).
3223 * Returns 1 on success, 0 otherwise.
3225 static int check_sig_level(X509_STORE_CTX
*ctx
, X509
*cert
)
3228 int level
= ctx
->param
->auth_level
;
3232 if (level
> NUM_AUTH_LEVELS
)
3233 level
= NUM_AUTH_LEVELS
;
3235 if (!X509_get_signature_info(cert
, NULL
, NULL
, &secbits
, NULL
))
3238 return secbits
>= minbits_table
[level
- 1];