2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 "crypto/ctype.h"
16 #include "internal/cryptlib.h"
17 #include <openssl/crypto.h>
18 #include <openssl/buffer.h>
19 #include <openssl/evp.h>
20 #include <openssl/asn1.h>
21 #include <openssl/x509.h>
22 #include <openssl/x509v3.h>
23 #include <openssl/objects.h>
24 #include "internal/dane.h"
25 #include "crypto/x509.h"
26 #include "x509_local.h"
28 /* CRL score values */
30 /* No unhandled critical extensions */
32 #define CRL_SCORE_NOCRITICAL 0x100
34 /* certificate is within CRL scope */
36 #define CRL_SCORE_SCOPE 0x080
40 #define CRL_SCORE_TIME 0x040
42 /* Issuer name matches certificate */
44 #define CRL_SCORE_ISSUER_NAME 0x020
46 /* If this score or above CRL is probably valid */
48 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
50 /* CRL issuer is certificate issuer */
52 #define CRL_SCORE_ISSUER_CERT 0x018
54 /* CRL issuer is on certificate path */
56 #define CRL_SCORE_SAME_PATH 0x008
58 /* CRL issuer matches CRL AKID */
60 #define CRL_SCORE_AKID 0x004
62 /* Have a delta CRL with valid times */
64 #define CRL_SCORE_TIME_DELTA 0x002
66 static int build_chain(X509_STORE_CTX
*ctx
);
67 static int verify_chain(X509_STORE_CTX
*ctx
);
68 static int dane_verify(X509_STORE_CTX
*ctx
);
69 static int null_callback(int ok
, X509_STORE_CTX
*e
);
70 static int check_issued(X509_STORE_CTX
*ctx
, X509
*x
, X509
*issuer
);
71 static X509
*find_issuer(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
, X509
*x
);
72 static int check_chain(X509_STORE_CTX
*ctx
);
73 static int check_name_constraints(X509_STORE_CTX
*ctx
);
74 static int check_id(X509_STORE_CTX
*ctx
);
75 static int check_trust(X509_STORE_CTX
*ctx
, int num_untrusted
);
76 static int check_revocation(X509_STORE_CTX
*ctx
);
77 static int check_cert(X509_STORE_CTX
*ctx
);
78 static int check_policy(X509_STORE_CTX
*ctx
);
79 static int get_issuer_sk(X509
**issuer
, X509_STORE_CTX
*ctx
, X509
*x
);
80 static int check_dane_issuer(X509_STORE_CTX
*ctx
, int depth
);
81 static int check_key_level(X509_STORE_CTX
*ctx
, X509
*cert
);
82 static int check_sig_level(X509_STORE_CTX
*ctx
, X509
*cert
);
83 static int check_curve(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
)
109 * Return 1 if given cert is considered self-signed, 0 if not, or -1 on error.
110 * This actually verifies self-signedness only if requested.
111 * It calls X509v3_cache_extensions()
112 * to match issuer and subject names (i.e., the cert being self-issued) and any
113 * present authority key identifier to match the subject key identifier, etc.
115 int X509_self_signed(X509
*cert
, int verify_signature
)
119 if ((pkey
= X509_get0_pubkey(cert
)) == NULL
) { /* handles cert == NULL */
120 ERR_raise(ERR_LIB_X509
, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY
);
123 if (!x509v3_cache_extensions(cert
))
125 if ((cert
->ex_flags
& EXFLAG_SS
) == 0)
127 if (!verify_signature
)
129 return X509_verify(cert
, pkey
);
132 /* Given a certificate try and find an exact match in the store */
133 static X509
*lookup_cert_match(X509_STORE_CTX
*ctx
, X509
*x
)
135 STACK_OF(X509
) *certs
;
138 /* Lookup all certs with matching subject name */
139 certs
= ctx
->lookup_certs(ctx
, X509_get_subject_name(x
));
142 /* Look for exact match */
143 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
144 xtmp
= sk_X509_value(certs
, i
);
145 if (!X509_cmp(xtmp
, x
))
149 if (xtmp
!= NULL
&& !X509_up_ref(xtmp
))
151 sk_X509_pop_free(certs
, X509_free
);
156 * Inform the verify callback of an error.
157 * If B<x> is not NULL it is the error cert, otherwise use the chain cert at
159 * If B<err> is not X509_V_OK, that's the error value, otherwise leave
160 * unchanged (presumably set by the caller).
162 * Returns 0 to abort verification with an error, non-zero to continue.
164 static int verify_cb_cert(X509_STORE_CTX
*ctx
, X509
*x
, int depth
, int err
)
166 ctx
->error_depth
= depth
;
167 ctx
->current_cert
= (x
!= NULL
) ? x
: sk_X509_value(ctx
->chain
, depth
);
168 if (err
!= X509_V_OK
)
170 return ctx
->verify_cb(0, ctx
);
173 #define CHECK_CB(cond, ctx, cert, depth, err) \
174 if ((cond) && verify_cb_cert(ctx, cert, depth, err) == 0) \
178 * Inform the verify callback of an error, CRL-specific variant. Here, the
179 * error depth and certificate are already set, we just specify the error
182 * Returns 0 to abort verification with an error, non-zero to continue.
184 static int verify_cb_crl(X509_STORE_CTX
*ctx
, int err
)
187 return ctx
->verify_cb(0, ctx
);
190 static int check_auth_level(X509_STORE_CTX
*ctx
)
193 int num
= sk_X509_num(ctx
->chain
);
195 if (ctx
->param
->auth_level
<= 0)
198 for (i
= 0; i
< num
; ++i
) {
199 X509
*cert
= sk_X509_value(ctx
->chain
, i
);
202 * We've already checked the security of the leaf key, so here we only
203 * check the security of issuer keys.
205 CHECK_CB(i
> 0 && !check_key_level(ctx
, cert
),
206 ctx
, cert
, i
, X509_V_ERR_CA_KEY_TOO_SMALL
);
208 * We also check the signature algorithm security of all certificates
209 * except those of the trust anchor at index num-1.
211 CHECK_CB(i
< num
- 1 && !check_sig_level(ctx
, cert
),
212 ctx
, cert
, i
, X509_V_ERR_CA_MD_TOO_WEAK
);
217 static int verify_chain(X509_STORE_CTX
*ctx
)
223 * Before either returning with an error, or continuing with CRL checks,
224 * instantiate chain public key parameters.
226 if ((ok
= build_chain(ctx
)) == 0 ||
227 (ok
= check_chain(ctx
)) == 0 ||
228 (ok
= check_auth_level(ctx
)) == 0 ||
229 (ok
= check_id(ctx
)) == 0 || 1)
230 X509_get_pubkey_parameters(NULL
, ctx
->chain
);
231 if (ok
== 0 || (ok
= ctx
->check_revocation(ctx
)) == 0)
234 err
= X509_chain_check_suiteb(&ctx
->error_depth
, NULL
, ctx
->chain
,
236 CHECK_CB(err
!= X509_V_OK
, ctx
, NULL
, ctx
->error_depth
, err
);
238 /* Verify chain signatures and expiration times */
239 ok
= (ctx
->verify
!= NULL
) ? ctx
->verify(ctx
) : internal_verify(ctx
);
243 if ((ok
= check_name_constraints(ctx
)) == 0)
246 #ifndef OPENSSL_NO_RFC3779
247 /* RFC 3779 path validation, now that CRL check has been done */
248 if ((ok
= X509v3_asid_validate_path(ctx
)) == 0)
250 if ((ok
= X509v3_addr_validate_path(ctx
)) == 0)
254 /* If we get this far evaluate policies */
255 if (ctx
->param
->flags
& X509_V_FLAG_POLICY_CHECK
)
256 ok
= ctx
->check_policy(ctx
);
260 int X509_verify_cert(X509_STORE_CTX
*ctx
)
262 SSL_DANE
*dane
= ctx
->dane
;
265 if (ctx
->cert
== NULL
) {
266 ERR_raise(ERR_LIB_X509
, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY
);
267 ctx
->error
= X509_V_ERR_INVALID_CALL
;
271 if (ctx
->chain
!= NULL
) {
273 * This X509_STORE_CTX has already been used to verify a cert. We
274 * cannot do another one.
276 ERR_raise(ERR_LIB_X509
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
277 ctx
->error
= X509_V_ERR_INVALID_CALL
;
281 if (!X509_add_cert_new(&ctx
->chain
, ctx
->cert
, X509_ADD_FLAG_UP_REF
)) {
282 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
285 ctx
->num_untrusted
= 1;
287 /* If the peer's public key is too weak, we can stop early. */
288 CHECK_CB(!check_key_level(ctx
, ctx
->cert
),
289 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
);
317 * Below check 'issuer != x' is an optimization and safety precaution:
318 * Candidate issuer cert cannot be the same as the subject cert 'x'.
320 if (issuer
!= x
&& ctx
->check_issued(ctx
, x
, issuer
)) {
322 if (x509_check_cert_time(ctx
, rv
, -1))
330 * Check that the given certificate 'x' is issued by the certificate 'issuer'
331 * and the issuer is not yet in ctx->chain, where the exceptional case
332 * that 'x' is self-issued and ctx->chain has just one element is allowed.
334 static int check_issued(X509_STORE_CTX
*ctx
, X509
*x
, X509
*issuer
)
336 if (x509_likely_issued(issuer
, x
) != X509_V_OK
)
338 if ((x
->ex_flags
& EXFLAG_SI
) == 0 || sk_X509_num(ctx
->chain
) != 1) {
342 for (i
= 0; i
< sk_X509_num(ctx
->chain
); i
++) {
343 ch
= sk_X509_value(ctx
->chain
, i
);
344 if (ch
== issuer
|| X509_cmp(ch
, issuer
) == 0)
351 /* Alternative lookup method: look from a STACK stored in other_ctx */
352 static int get_issuer_sk(X509
**issuer
, X509_STORE_CTX
*ctx
, X509
*x
)
354 *issuer
= find_issuer(ctx
, ctx
->other_ctx
, x
);
356 if (*issuer
== NULL
|| !X509_up_ref(*issuer
))
366 static STACK_OF(X509
) *lookup_certs_sk(X509_STORE_CTX
*ctx
,
369 STACK_OF(X509
) *sk
= NULL
;
373 for (i
= 0; i
< sk_X509_num(ctx
->other_ctx
); i
++) {
374 x
= sk_X509_value(ctx
->other_ctx
, i
);
375 if (X509_NAME_cmp(nm
, X509_get_subject_name(x
)) == 0) {
376 if (!X509_add_cert_new(&sk
, x
, X509_ADD_FLAG_UP_REF
)) {
377 sk_X509_pop_free(sk
, X509_free
);
378 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
387 * Check EE or CA certificate purpose. For trusted certificates explicit local
388 * auxiliary trust can be used to override EKU-restrictions.
390 static int check_purpose(X509_STORE_CTX
*ctx
, X509
*x
, int purpose
, int depth
,
393 int tr_ok
= X509_TRUST_UNTRUSTED
;
396 * For trusted certificates we want to see whether any auxiliary trust
397 * settings trump the purpose constraints.
399 * This is complicated by the fact that the trust ordinals in
400 * ctx->param->trust are entirely independent of the purpose ordinals in
401 * ctx->param->purpose!
403 * What connects them is their mutual initialization via calls from
404 * X509_STORE_CTX_set_default() into X509_VERIFY_PARAM_lookup() which sets
405 * related values of both param->trust and param->purpose. It is however
406 * typically possible to infer associated trust values from a purpose value
407 * via the X509_PURPOSE API.
409 * Therefore, we can only check for trust overrides when the purpose we're
410 * checking is the same as ctx->param->purpose and ctx->param->trust is
413 if (depth
>= ctx
->num_untrusted
&& purpose
== ctx
->param
->purpose
)
414 tr_ok
= X509_check_trust(x
, ctx
->param
->trust
, X509_TRUST_NO_SS_COMPAT
);
417 case X509_TRUST_TRUSTED
:
419 case X509_TRUST_REJECTED
:
422 switch (X509_check_purpose(x
, purpose
, must_be_ca
> 0)) {
428 if ((ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
) == 0)
434 return verify_cb_cert(ctx
, x
, depth
, X509_V_ERR_INVALID_PURPOSE
);
438 * Check a certificate chains extensions for consistency with the supplied
442 static int check_chain(X509_STORE_CTX
*ctx
)
444 int i
, must_be_ca
, plen
= 0;
446 int proxy_path_length
= 0;
448 int allow_proxy_certs
;
449 int num
= sk_X509_num(ctx
->chain
);
452 * must_be_ca can have 1 of 3 values:
453 * -1: we accept both CA and non-CA certificates, to allow direct
454 * use of self-signed certificates (which are marked as CA).
455 * 0: we only accept non-CA certificates. This is currently not
456 * used, but the possibility is present for future extensions.
457 * 1: we only accept CA certificates. This is currently used for
458 * all certificates in the chain except the leaf certificate.
462 /* CRL path validation */
464 allow_proxy_certs
= 0;
465 purpose
= X509_PURPOSE_CRL_SIGN
;
468 ! !(ctx
->param
->flags
& X509_V_FLAG_ALLOW_PROXY_CERTS
);
469 purpose
= ctx
->param
->purpose
;
472 for (i
= 0; i
< num
; i
++) {
475 x
= sk_X509_value(ctx
->chain
, i
);
476 CHECK_CB((ctx
->param
->flags
& X509_V_FLAG_IGNORE_CRITICAL
) == 0
477 && (x
->ex_flags
& EXFLAG_CRITICAL
) != 0,
478 ctx
, x
, i
, X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION
);
479 CHECK_CB(!allow_proxy_certs
&& (x
->ex_flags
& EXFLAG_PROXY
),
480 ctx
, x
, i
, X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED
);
481 ret
= X509_check_ca(x
);
482 switch (must_be_ca
) {
484 CHECK_CB((ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
) != 0
485 && ret
!= 1 && ret
!= 0,
486 ctx
, x
, i
, X509_V_ERR_INVALID_CA
);
490 CHECK_CB(ret
!= 0, ctx
, x
, i
, X509_V_ERR_INVALID_NON_CA
);
494 /* X509_V_FLAG_X509_STRICT is implicit for intermediate CAs */
497 || ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
)
498 && ret
!= 1), ctx
, x
, i
, X509_V_ERR_INVALID_CA
);
503 /* Check for presence of explicit elliptic curve parameters */
504 ret
= check_curve(x
);
505 CHECK_CB(ret
< 0, ctx
, x
, i
, X509_V_ERR_UNSPECIFIED
);
506 CHECK_CB(ret
== 0, ctx
, x
, i
, X509_V_ERR_EC_KEY_EXPLICIT_PARAMS
);
509 * Do the following set of checks only if strict checking is requrested
510 * and not for self-issued (including self-signed) EE (non-CA) certs
511 * because RFC 5280 does not apply to them according RFC 6818 section 2.
513 if ((ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
) != 0
516 * !(i == 0 && (x->ex_flags & EXFLAG_CA) == 0
517 * && (x->ex_flags & EXFLAG_SI) != 0)
519 /* Check Basic Constraints according to RFC 5280 section 4.2.1.9 */
520 if (x
->ex_pathlen
!= -1) {
521 CHECK_CB((x
->ex_flags
& EXFLAG_CA
) == 0,
522 ctx
, x
, i
, X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA
);
523 CHECK_CB((x
->ex_kusage
& KU_KEY_CERT_SIGN
) == 0, ctx
, x
, i
,
524 X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN
);
526 CHECK_CB((x
->ex_flags
& EXFLAG_CA
) != 0
527 && (x
->ex_flags
& EXFLAG_BCONS
) != 0
528 && (x
->ex_flags
& EXFLAG_BCONS_CRITICAL
) == 0,
529 ctx
, x
, i
, X509_V_ERR_CA_BCONS_NOT_CRITICAL
);
530 /* Check Key Usage according to RFC 5280 section 4.2.1.3 */
531 if ((x
->ex_flags
& EXFLAG_CA
) != 0) {
532 CHECK_CB((x
->ex_flags
& EXFLAG_KUSAGE
) == 0,
533 ctx
, x
, i
, X509_V_ERR_CA_CERT_MISSING_KEY_USAGE
);
535 CHECK_CB((x
->ex_kusage
& KU_KEY_CERT_SIGN
) != 0, ctx
, x
, i
,
536 X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA
);
538 /* Check issuer is non-empty acc. to RFC 5280 section 4.1.2.4 */
539 CHECK_CB(X509_NAME_entry_count(X509_get_issuer_name(x
)) == 0,
540 ctx
, x
, i
, X509_V_ERR_ISSUER_NAME_EMPTY
);
541 /* Check subject is non-empty acc. to RFC 5280 section 4.1.2.6 */
542 CHECK_CB(((x
->ex_flags
& EXFLAG_CA
) != 0
543 || (x
->ex_kusage
& KU_CRL_SIGN
) != 0
544 || x
->altname
== NULL
545 ) && X509_NAME_entry_count(X509_get_subject_name(x
)) == 0,
546 ctx
, x
, i
, X509_V_ERR_SUBJECT_NAME_EMPTY
);
547 CHECK_CB(X509_NAME_entry_count(X509_get_subject_name(x
)) == 0
548 && x
->altname
!= NULL
549 && (x
->ex_flags
& EXFLAG_SAN_CRITICAL
) == 0,
550 ctx
, x
, i
, X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL
);
551 /* Check SAN is non-empty according to RFC 5280 section 4.2.1.6 */
552 CHECK_CB(x
->altname
!= NULL
&& sk_GENERAL_NAME_num(x
->altname
) <= 0,
553 ctx
, x
, i
, X509_V_ERR_EMPTY_SUBJECT_ALT_NAME
);
554 /* TODO add more checks on SAN entries */
555 /* Check sig alg consistency acc. to RFC 5280 section 4.1.1.2 */
556 CHECK_CB(X509_ALGOR_cmp(&x
->sig_alg
, &x
->cert_info
.signature
) != 0,
557 ctx
, x
, i
, X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY
);
558 CHECK_CB(x
->akid
!= NULL
559 && (x
->ex_flags
& EXFLAG_AKID_CRITICAL
) != 0,
560 ctx
, x
, i
, X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL
);
561 CHECK_CB(x
->skid
!= NULL
562 && (x
->ex_flags
& EXFLAG_SKID_CRITICAL
) != 0,
563 ctx
, x
, i
, X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL
);
564 if (X509_get_version(x
) >= 2) { /* at least X.509v3 */
565 /* Check AKID presence acc. to RFC 5280 section 4.2.1.1 */
566 CHECK_CB(i
+ 1 < num
/*
567 * this means not last cert in chain,
568 * taken as "generated by conforming CAs"
570 && (x
->akid
== NULL
|| x
->akid
->keyid
== NULL
), ctx
,
571 x
, i
, X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER
);
572 /* Check SKID presence acc. to RFC 5280 section 4.2.1.2 */
573 CHECK_CB((x
->ex_flags
& EXFLAG_CA
) != 0 && x
->skid
== NULL
,
574 ctx
, x
, i
, X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER
);
576 CHECK_CB(sk_X509_EXTENSION_num(X509_get0_extensions(x
)) > 0,
577 ctx
, x
, i
, X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3
);
581 /* check_purpose() makes the callback as needed */
582 if (purpose
> 0 && !check_purpose(ctx
, x
, purpose
, i
, must_be_ca
))
585 CHECK_CB(i
> 1 && x
->ex_pathlen
!= -1
586 && plen
> x
->ex_pathlen
+ proxy_path_length
,
587 ctx
, x
, i
, X509_V_ERR_PATH_LENGTH_EXCEEDED
);
588 /* Increment path length if not a self-issued intermediate CA */
589 if (i
> 0 && (x
->ex_flags
& EXFLAG_SI
) == 0)
592 * If this certificate is a proxy certificate, the next certificate
593 * must be another proxy certificate or a EE certificate. If not,
594 * the next certificate must be a CA certificate.
596 if (x
->ex_flags
& EXFLAG_PROXY
) {
598 * RFC3820, 4.1.3 (b)(1) stipulates that if pCPathLengthConstraint
599 * is less than max_path_length, the former should be copied to
600 * the latter, and 4.1.4 (a) stipulates that max_path_length
601 * should be verified to be larger than zero and decrement it.
603 * Because we're checking the certs in the reverse order, we start
604 * with verifying that proxy_path_length isn't larger than pcPLC,
605 * and copy the latter to the former if it is, and finally,
606 * increment proxy_path_length.
608 if (x
->ex_pcpathlen
!= -1) {
609 CHECK_CB(proxy_path_length
> x
->ex_pcpathlen
,
610 ctx
, x
, i
, X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED
);
611 proxy_path_length
= x
->ex_pcpathlen
;
621 static int has_san_id(X509
*x
, int gtype
)
625 GENERAL_NAMES
*gs
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
630 for (i
= 0; i
< sk_GENERAL_NAME_num(gs
); i
++) {
631 GENERAL_NAME
*g
= sk_GENERAL_NAME_value(gs
, i
);
633 if (g
->type
== gtype
) {
638 GENERAL_NAMES_free(gs
);
642 static int check_name_constraints(X509_STORE_CTX
*ctx
)
646 /* Check name constraints for all certificates */
647 for (i
= sk_X509_num(ctx
->chain
) - 1; i
>= 0; i
--) {
648 X509
*x
= sk_X509_value(ctx
->chain
, i
);
651 /* Ignore self-issued certs unless last in chain */
652 if (i
&& (x
->ex_flags
& EXFLAG_SI
))
656 * Proxy certificates policy has an extra constraint, where the
657 * certificate subject MUST be the issuer with a single CN entry
659 * (RFC 3820: 3.4, 4.1.3 (a)(4))
661 if (x
->ex_flags
& EXFLAG_PROXY
) {
662 X509_NAME
*tmpsubject
= X509_get_subject_name(x
);
663 X509_NAME
*tmpissuer
= X509_get_issuer_name(x
);
664 X509_NAME_ENTRY
*tmpentry
= NULL
;
665 int last_object_nid
= 0;
667 int last_object_loc
= X509_NAME_entry_count(tmpsubject
) - 1;
669 /* Check that there are at least two RDNs */
670 if (last_object_loc
< 1) {
671 err
= X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION
;
672 goto proxy_name_done
;
676 * Check that there is exactly one more RDN in subject as
677 * there is in issuer.
679 if (X509_NAME_entry_count(tmpsubject
)
680 != X509_NAME_entry_count(tmpissuer
) + 1) {
681 err
= X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION
;
682 goto proxy_name_done
;
686 * Check that the last subject component isn't part of a
689 if (X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject
,
691 == X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject
,
692 last_object_loc
- 1))) {
693 err
= X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION
;
694 goto proxy_name_done
;
698 * Check that the last subject RDN is a commonName, and that
699 * all the previous RDNs match the issuer exactly
701 tmpsubject
= X509_NAME_dup(tmpsubject
);
702 if (tmpsubject
== NULL
) {
703 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
704 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
709 X509_NAME_delete_entry(tmpsubject
, last_object_loc
);
711 OBJ_obj2nid(X509_NAME_ENTRY_get_object(tmpentry
));
713 if (last_object_nid
!= NID_commonName
714 || X509_NAME_cmp(tmpsubject
, tmpissuer
) != 0) {
715 err
= X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION
;
718 X509_NAME_ENTRY_free(tmpentry
);
719 X509_NAME_free(tmpsubject
);
722 CHECK_CB(err
!= X509_V_OK
, ctx
, x
, i
, err
);
726 * Check against constraints for all certificates higher in chain
727 * including trust anchor. Trust anchor not strictly speaking needed
728 * but if it includes constraints it is to be assumed it expects them
731 for (j
= sk_X509_num(ctx
->chain
) - 1; j
> i
; j
--) {
732 NAME_CONSTRAINTS
*nc
= sk_X509_value(ctx
->chain
, j
)->nc
;
735 int rv
= NAME_CONSTRAINTS_check(x
, nc
);
737 /* If EE certificate check commonName too */
738 if (rv
== X509_V_OK
&& i
== 0
739 && (ctx
->param
->hostflags
740 & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT
) == 0
741 && ((ctx
->param
->hostflags
742 & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
) != 0
743 || !has_san_id(x
, GEN_DNS
)))
744 rv
= NAME_CONSTRAINTS_check_CN(x
, nc
);
749 case X509_V_ERR_OUT_OF_MEM
:
752 CHECK_CB(1, ctx
, x
, i
, rv
);
761 static int check_id_error(X509_STORE_CTX
*ctx
, int errcode
)
763 return verify_cb_cert(ctx
, ctx
->cert
, 0, errcode
);
766 static int check_hosts(X509
*x
, X509_VERIFY_PARAM
*vpm
)
769 int n
= sk_OPENSSL_STRING_num(vpm
->hosts
);
772 if (vpm
->peername
!= NULL
) {
773 OPENSSL_free(vpm
->peername
);
774 vpm
->peername
= NULL
;
776 for (i
= 0; i
< n
; ++i
) {
777 name
= sk_OPENSSL_STRING_value(vpm
->hosts
, i
);
778 if (X509_check_host(x
, name
, 0, vpm
->hostflags
, &vpm
->peername
) > 0)
784 static int check_id(X509_STORE_CTX
*ctx
)
786 X509_VERIFY_PARAM
*vpm
= ctx
->param
;
788 if (vpm
->hosts
&& check_hosts(x
, vpm
) <= 0) {
789 if (!check_id_error(ctx
, X509_V_ERR_HOSTNAME_MISMATCH
))
792 if (vpm
->email
&& X509_check_email(x
, vpm
->email
, vpm
->emaillen
, 0) <= 0) {
793 if (!check_id_error(ctx
, X509_V_ERR_EMAIL_MISMATCH
))
796 if (vpm
->ip
&& X509_check_ip(x
, vpm
->ip
, vpm
->iplen
, 0) <= 0) {
797 if (!check_id_error(ctx
, X509_V_ERR_IP_ADDRESS_MISMATCH
))
803 static int check_trust(X509_STORE_CTX
*ctx
, int num_untrusted
)
808 SSL_DANE
*dane
= ctx
->dane
;
809 int num
= sk_X509_num(ctx
->chain
);
813 * Check for a DANE issuer at depth 1 or greater, if it is a DANE-TA(2)
814 * match, we're done, otherwise we'll merely record the match depth.
816 if (DANETLS_HAS_TA(dane
) && num_untrusted
> 0 && num_untrusted
< num
) {
817 switch (trust
= check_dane_issuer(ctx
, num_untrusted
)) {
818 case X509_TRUST_TRUSTED
:
819 case X509_TRUST_REJECTED
:
825 * Check trusted certificates in chain at depth num_untrusted and up.
826 * Note, that depths 0..num_untrusted-1 may also contain trusted
827 * certificates, but the caller is expected to have already checked those,
828 * and wants to incrementally check just any added since.
830 for (i
= num_untrusted
; i
< num
; i
++) {
831 x
= sk_X509_value(ctx
->chain
, i
);
832 trust
= X509_check_trust(x
, ctx
->param
->trust
, 0);
833 /* If explicitly trusted return trusted */
834 if (trust
== X509_TRUST_TRUSTED
)
836 if (trust
== X509_TRUST_REJECTED
)
841 * If we are looking at a trusted certificate, and accept partial chains,
842 * the chain is PKIX trusted.
844 if (num_untrusted
< num
) {
845 if (ctx
->param
->flags
& X509_V_FLAG_PARTIAL_CHAIN
)
847 return X509_TRUST_UNTRUSTED
;
850 if (num_untrusted
== num
&& ctx
->param
->flags
& X509_V_FLAG_PARTIAL_CHAIN
) {
852 * Last-resort call with no new trusted certificates, check the leaf
853 * for a direct trust store match.
856 x
= sk_X509_value(ctx
->chain
, i
);
857 mx
= lookup_cert_match(ctx
, x
);
859 return X509_TRUST_UNTRUSTED
;
862 * Check explicit auxiliary trust/reject settings. If none are set,
863 * we'll accept X509_TRUST_UNTRUSTED when not self-signed.
865 trust
= X509_check_trust(mx
, ctx
->param
->trust
, 0);
866 if (trust
== X509_TRUST_REJECTED
) {
871 /* Replace leaf with trusted match */
872 (void) sk_X509_set(ctx
->chain
, 0, mx
);
874 ctx
->num_untrusted
= 0;
879 * If no trusted certs in chain at all return untrusted and allow
880 * standard (no issuer cert) etc errors to be indicated.
882 return X509_TRUST_UNTRUSTED
;
885 return verify_cb_cert(ctx
, x
, i
, X509_V_ERR_CERT_REJECTED
) == 0
886 ? X509_TRUST_REJECTED
: X509_TRUST_UNTRUSTED
;
889 if (!DANETLS_ENABLED(dane
))
890 return X509_TRUST_TRUSTED
;
892 dane
->pdpth
= num_untrusted
;
893 /* With DANE, PKIX alone is not trusted until we have both */
894 if (dane
->mdpth
>= 0)
895 return X509_TRUST_TRUSTED
;
896 return X509_TRUST_UNTRUSTED
;
899 static int check_revocation(X509_STORE_CTX
*ctx
)
901 int i
= 0, last
= 0, ok
= 0;
902 if (!(ctx
->param
->flags
& X509_V_FLAG_CRL_CHECK
))
904 if (ctx
->param
->flags
& X509_V_FLAG_CRL_CHECK_ALL
)
905 last
= sk_X509_num(ctx
->chain
) - 1;
907 /* If checking CRL paths this isn't the EE certificate */
912 for (i
= 0; i
<= last
; i
++) {
913 ctx
->error_depth
= i
;
914 ok
= check_cert(ctx
);
921 static int check_cert(X509_STORE_CTX
*ctx
)
923 X509_CRL
*crl
= NULL
, *dcrl
= NULL
;
925 int cnum
= ctx
->error_depth
;
926 X509
*x
= sk_X509_value(ctx
->chain
, cnum
);
928 ctx
->current_cert
= x
;
929 ctx
->current_issuer
= NULL
;
930 ctx
->current_crl_score
= 0;
931 ctx
->current_reasons
= 0;
933 if (x
->ex_flags
& EXFLAG_PROXY
)
936 while (ctx
->current_reasons
!= CRLDP_ALL_REASONS
) {
937 unsigned int last_reasons
= ctx
->current_reasons
;
939 /* Try to retrieve relevant CRL */
941 ok
= ctx
->get_crl(ctx
, &crl
, x
);
943 ok
= get_crl_delta(ctx
, &crl
, &dcrl
, x
);
945 * If error looking up CRL, nothing we can do except notify callback
948 ok
= verify_cb_crl(ctx
, X509_V_ERR_UNABLE_TO_GET_CRL
);
951 ctx
->current_crl
= crl
;
952 ok
= ctx
->check_crl(ctx
, crl
);
957 ok
= ctx
->check_crl(ctx
, dcrl
);
960 ok
= ctx
->cert_crl(ctx
, dcrl
, x
);
966 /* Don't look in full CRL if delta reason is removefromCRL */
968 ok
= ctx
->cert_crl(ctx
, crl
, x
);
978 * If reasons not updated we won't get anywhere by another iteration,
981 if (last_reasons
== ctx
->current_reasons
) {
982 ok
= verify_cb_crl(ctx
, X509_V_ERR_UNABLE_TO_GET_CRL
);
990 ctx
->current_crl
= NULL
;
994 /* Check CRL times against values in X509_STORE_CTX */
996 static int check_crl_time(X509_STORE_CTX
*ctx
, X509_CRL
*crl
, int notify
)
1002 ctx
->current_crl
= crl
;
1003 if (ctx
->param
->flags
& X509_V_FLAG_USE_CHECK_TIME
)
1004 ptime
= &ctx
->param
->check_time
;
1005 else if (ctx
->param
->flags
& X509_V_FLAG_NO_CHECK_TIME
)
1010 i
= X509_cmp_time(X509_CRL_get0_lastUpdate(crl
), ptime
);
1014 if (!verify_cb_crl(ctx
, X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
))
1021 if (!verify_cb_crl(ctx
, X509_V_ERR_CRL_NOT_YET_VALID
))
1025 if (X509_CRL_get0_nextUpdate(crl
)) {
1026 i
= X509_cmp_time(X509_CRL_get0_nextUpdate(crl
), ptime
);
1031 if (!verify_cb_crl(ctx
, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
))
1034 /* Ignore expiry of base CRL is delta is valid */
1035 if ((i
< 0) && !(ctx
->current_crl_score
& CRL_SCORE_TIME_DELTA
)) {
1038 if (!verify_cb_crl(ctx
, X509_V_ERR_CRL_HAS_EXPIRED
))
1044 ctx
->current_crl
= NULL
;
1049 static int get_crl_sk(X509_STORE_CTX
*ctx
, X509_CRL
**pcrl
, X509_CRL
**pdcrl
,
1050 X509
**pissuer
, int *pscore
, unsigned int *preasons
,
1051 STACK_OF(X509_CRL
) *crls
)
1053 int i
, crl_score
, best_score
= *pscore
;
1054 unsigned int reasons
, best_reasons
= 0;
1055 X509
*x
= ctx
->current_cert
;
1056 X509_CRL
*crl
, *best_crl
= NULL
;
1057 X509
*crl_issuer
= NULL
, *best_crl_issuer
= NULL
;
1059 for (i
= 0; i
< sk_X509_CRL_num(crls
); i
++) {
1060 crl
= sk_X509_CRL_value(crls
, i
);
1061 reasons
= *preasons
;
1062 crl_score
= get_crl_score(ctx
, &crl_issuer
, &reasons
, crl
, x
);
1063 if (crl_score
< best_score
|| crl_score
== 0)
1065 /* If current CRL is equivalent use it if it is newer */
1066 if (crl_score
== best_score
&& best_crl
!= NULL
) {
1068 if (ASN1_TIME_diff(&day
, &sec
, X509_CRL_get0_lastUpdate(best_crl
),
1069 X509_CRL_get0_lastUpdate(crl
)) == 0)
1072 * ASN1_TIME_diff never returns inconsistent signs for |day|
1075 if (day
<= 0 && sec
<= 0)
1079 best_crl_issuer
= crl_issuer
;
1080 best_score
= crl_score
;
1081 best_reasons
= reasons
;
1085 X509_CRL_free(*pcrl
);
1087 *pissuer
= best_crl_issuer
;
1088 *pscore
= best_score
;
1089 *preasons
= best_reasons
;
1090 X509_CRL_up_ref(best_crl
);
1091 X509_CRL_free(*pdcrl
);
1093 get_delta_sk(ctx
, pdcrl
, pscore
, best_crl
, crls
);
1096 if (best_score
>= CRL_SCORE_VALID
)
1103 * Compare two CRL extensions for delta checking purposes. They should be
1104 * both present or both absent. If both present all fields must be identical.
1107 static int crl_extension_match(X509_CRL
*a
, X509_CRL
*b
, int nid
)
1109 ASN1_OCTET_STRING
*exta
, *extb
;
1111 i
= X509_CRL_get_ext_by_NID(a
, nid
, -1);
1113 /* Can't have multiple occurrences */
1114 if (X509_CRL_get_ext_by_NID(a
, nid
, i
) != -1)
1116 exta
= X509_EXTENSION_get_data(X509_CRL_get_ext(a
, i
));
1120 i
= X509_CRL_get_ext_by_NID(b
, nid
, -1);
1124 if (X509_CRL_get_ext_by_NID(b
, nid
, i
) != -1)
1126 extb
= X509_EXTENSION_get_data(X509_CRL_get_ext(b
, i
));
1136 if (ASN1_OCTET_STRING_cmp(exta
, extb
))
1142 /* See if a base and delta are compatible */
1144 static int check_delta_base(X509_CRL
*delta
, X509_CRL
*base
)
1146 /* Delta CRL must be a delta */
1147 if (!delta
->base_crl_number
)
1149 /* Base must have a CRL number */
1150 if (!base
->crl_number
)
1152 /* Issuer names must match */
1153 if (X509_NAME_cmp(X509_CRL_get_issuer(base
), X509_CRL_get_issuer(delta
)))
1155 /* AKID and IDP must match */
1156 if (!crl_extension_match(delta
, base
, NID_authority_key_identifier
))
1158 if (!crl_extension_match(delta
, base
, NID_issuing_distribution_point
))
1160 /* Delta CRL base number must not exceed Full CRL number. */
1161 if (ASN1_INTEGER_cmp(delta
->base_crl_number
, base
->crl_number
) > 0)
1163 /* Delta CRL number must exceed full CRL number */
1164 if (ASN1_INTEGER_cmp(delta
->crl_number
, base
->crl_number
) > 0)
1170 * For a given base CRL find a delta... maybe extend to delta scoring or
1171 * retrieve a chain of deltas...
1174 static void get_delta_sk(X509_STORE_CTX
*ctx
, X509_CRL
**dcrl
, int *pscore
,
1175 X509_CRL
*base
, STACK_OF(X509_CRL
) *crls
)
1179 if (!(ctx
->param
->flags
& X509_V_FLAG_USE_DELTAS
))
1181 if (!((ctx
->current_cert
->ex_flags
| base
->flags
) & EXFLAG_FRESHEST
))
1183 for (i
= 0; i
< sk_X509_CRL_num(crls
); i
++) {
1184 delta
= sk_X509_CRL_value(crls
, i
);
1185 if (check_delta_base(delta
, base
)) {
1186 if (check_crl_time(ctx
, delta
, 0))
1187 *pscore
|= CRL_SCORE_TIME_DELTA
;
1188 X509_CRL_up_ref(delta
);
1197 * For a given CRL return how suitable it is for the supplied certificate
1198 * 'x'. The return value is a mask of several criteria. If the issuer is not
1199 * the certificate issuer this is returned in *pissuer. The reasons mask is
1200 * also used to determine if the CRL is suitable: if no new reasons the CRL
1201 * is rejected, otherwise reasons is updated.
1204 static int get_crl_score(X509_STORE_CTX
*ctx
, X509
**pissuer
,
1205 unsigned int *preasons
, X509_CRL
*crl
, X509
*x
)
1209 unsigned int tmp_reasons
= *preasons
, crl_reasons
;
1211 /* First see if we can reject CRL straight away */
1213 /* Invalid IDP cannot be processed */
1214 if (crl
->idp_flags
& IDP_INVALID
)
1216 /* Reason codes or indirect CRLs need extended CRL support */
1217 if (!(ctx
->param
->flags
& X509_V_FLAG_EXTENDED_CRL_SUPPORT
)) {
1218 if (crl
->idp_flags
& (IDP_INDIRECT
| IDP_REASONS
))
1220 } else if (crl
->idp_flags
& IDP_REASONS
) {
1221 /* If no new reasons reject */
1222 if (!(crl
->idp_reasons
& ~tmp_reasons
))
1225 /* Don't process deltas at this stage */
1226 else if (crl
->base_crl_number
)
1228 /* If issuer name doesn't match certificate need indirect CRL */
1229 if (X509_NAME_cmp(X509_get_issuer_name(x
), X509_CRL_get_issuer(crl
))) {
1230 if (!(crl
->idp_flags
& IDP_INDIRECT
))
1233 crl_score
|= CRL_SCORE_ISSUER_NAME
;
1235 if (!(crl
->flags
& EXFLAG_CRITICAL
))
1236 crl_score
|= CRL_SCORE_NOCRITICAL
;
1239 if (check_crl_time(ctx
, crl
, 0))
1240 crl_score
|= CRL_SCORE_TIME
;
1242 /* Check authority key ID and locate certificate issuer */
1243 crl_akid_check(ctx
, crl
, pissuer
, &crl_score
);
1245 /* If we can't locate certificate issuer at this point forget it */
1247 if (!(crl_score
& CRL_SCORE_AKID
))
1250 /* Check cert for matching CRL distribution points */
1252 if (crl_crldp_check(x
, crl
, crl_score
, &crl_reasons
)) {
1253 /* If no new reasons reject */
1254 if (!(crl_reasons
& ~tmp_reasons
))
1256 tmp_reasons
|= crl_reasons
;
1257 crl_score
|= CRL_SCORE_SCOPE
;
1260 *preasons
= tmp_reasons
;
1266 static void crl_akid_check(X509_STORE_CTX
*ctx
, X509_CRL
*crl
,
1267 X509
**pissuer
, int *pcrl_score
)
1269 X509
*crl_issuer
= NULL
;
1270 const X509_NAME
*cnm
= X509_CRL_get_issuer(crl
);
1271 int cidx
= ctx
->error_depth
;
1274 if (cidx
!= sk_X509_num(ctx
->chain
) - 1)
1277 crl_issuer
= sk_X509_value(ctx
->chain
, cidx
);
1279 if (X509_check_akid(crl_issuer
, crl
->akid
) == X509_V_OK
) {
1280 if (*pcrl_score
& CRL_SCORE_ISSUER_NAME
) {
1281 *pcrl_score
|= CRL_SCORE_AKID
| CRL_SCORE_ISSUER_CERT
;
1282 *pissuer
= crl_issuer
;
1287 for (cidx
++; cidx
< sk_X509_num(ctx
->chain
); cidx
++) {
1288 crl_issuer
= sk_X509_value(ctx
->chain
, cidx
);
1289 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer
), cnm
))
1291 if (X509_check_akid(crl_issuer
, crl
->akid
) == X509_V_OK
) {
1292 *pcrl_score
|= CRL_SCORE_AKID
| CRL_SCORE_SAME_PATH
;
1293 *pissuer
= crl_issuer
;
1298 /* Anything else needs extended CRL support */
1300 if (!(ctx
->param
->flags
& X509_V_FLAG_EXTENDED_CRL_SUPPORT
))
1304 * Otherwise the CRL issuer is not on the path. Look for it in the set of
1305 * untrusted certificates.
1307 for (i
= 0; i
< sk_X509_num(ctx
->untrusted
); i
++) {
1308 crl_issuer
= sk_X509_value(ctx
->untrusted
, i
);
1309 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer
), cnm
))
1311 if (X509_check_akid(crl_issuer
, crl
->akid
) == X509_V_OK
) {
1312 *pissuer
= crl_issuer
;
1313 *pcrl_score
|= CRL_SCORE_AKID
;
1320 * Check the path of a CRL issuer certificate. This creates a new
1321 * X509_STORE_CTX and populates it with most of the parameters from the
1322 * parent. This could be optimised somewhat since a lot of path checking will
1323 * be duplicated by the parent, but this will rarely be used in practice.
1326 static int check_crl_path(X509_STORE_CTX
*ctx
, X509
*x
)
1328 X509_STORE_CTX crl_ctx
;
1331 /* Don't allow recursive CRL path validation */
1334 if (!X509_STORE_CTX_init(&crl_ctx
, ctx
->store
, x
, ctx
->untrusted
))
1337 crl_ctx
.crls
= ctx
->crls
;
1338 /* Copy verify params across */
1339 X509_STORE_CTX_set0_param(&crl_ctx
, ctx
->param
);
1341 crl_ctx
.parent
= ctx
;
1342 crl_ctx
.verify_cb
= ctx
->verify_cb
;
1344 /* Verify CRL issuer */
1345 ret
= X509_verify_cert(&crl_ctx
);
1349 /* Check chain is acceptable */
1350 ret
= check_crl_chain(ctx
, ctx
->chain
, crl_ctx
.chain
);
1352 X509_STORE_CTX_cleanup(&crl_ctx
);
1357 * RFC3280 says nothing about the relationship between CRL path and
1358 * certificate path, which could lead to situations where a certificate could
1359 * be revoked or validated by a CA not authorised to do so. RFC5280 is more
1360 * strict and states that the two paths must end in the same trust anchor,
1361 * though some discussions remain... until this is resolved we use the
1365 static int check_crl_chain(X509_STORE_CTX
*ctx
,
1366 STACK_OF(X509
) *cert_path
,
1367 STACK_OF(X509
) *crl_path
)
1369 X509
*cert_ta
, *crl_ta
;
1370 cert_ta
= sk_X509_value(cert_path
, sk_X509_num(cert_path
) - 1);
1371 crl_ta
= sk_X509_value(crl_path
, sk_X509_num(crl_path
) - 1);
1372 if (!X509_cmp(cert_ta
, crl_ta
))
1378 * Check for match between two dist point names: three separate cases.
1379 * 1. Both are relative names and compare X509_NAME types.
1380 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1381 * 3. Both are full names and compare two GENERAL_NAMES.
1382 * 4. One is NULL: automatic match.
1385 static int idp_check_dp(DIST_POINT_NAME
*a
, DIST_POINT_NAME
*b
)
1387 X509_NAME
*nm
= NULL
;
1388 GENERAL_NAMES
*gens
= NULL
;
1389 GENERAL_NAME
*gena
, *genb
;
1396 /* Case 1: two X509_NAME */
1400 if (!X509_NAME_cmp(a
->dpname
, b
->dpname
))
1405 /* Case 2: set name and GENERAL_NAMES appropriately */
1407 gens
= b
->name
.fullname
;
1408 } else if (b
->type
== 1) {
1411 /* Case 2: set name and GENERAL_NAMES appropriately */
1412 gens
= a
->name
.fullname
;
1416 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1418 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
1419 gena
= sk_GENERAL_NAME_value(gens
, i
);
1420 if (gena
->type
!= GEN_DIRNAME
)
1422 if (!X509_NAME_cmp(nm
, gena
->d
.directoryName
))
1428 /* Else case 3: two GENERAL_NAMES */
1430 for (i
= 0; i
< sk_GENERAL_NAME_num(a
->name
.fullname
); i
++) {
1431 gena
= sk_GENERAL_NAME_value(a
->name
.fullname
, i
);
1432 for (j
= 0; j
< sk_GENERAL_NAME_num(b
->name
.fullname
); j
++) {
1433 genb
= sk_GENERAL_NAME_value(b
->name
.fullname
, j
);
1434 if (!GENERAL_NAME_cmp(gena
, genb
))
1443 static int crldp_check_crlissuer(DIST_POINT
*dp
, X509_CRL
*crl
, int crl_score
)
1446 const X509_NAME
*nm
= X509_CRL_get_issuer(crl
);
1447 /* If no CRLissuer return is successful iff don't need a match */
1449 return ! !(crl_score
& CRL_SCORE_ISSUER_NAME
);
1450 for (i
= 0; i
< sk_GENERAL_NAME_num(dp
->CRLissuer
); i
++) {
1451 GENERAL_NAME
*gen
= sk_GENERAL_NAME_value(dp
->CRLissuer
, i
);
1452 if (gen
->type
!= GEN_DIRNAME
)
1454 if (!X509_NAME_cmp(gen
->d
.directoryName
, nm
))
1460 /* Check CRLDP and IDP */
1462 static int crl_crldp_check(X509
*x
, X509_CRL
*crl
, int crl_score
,
1463 unsigned int *preasons
)
1466 if (crl
->idp_flags
& IDP_ONLYATTR
)
1468 if (x
->ex_flags
& EXFLAG_CA
) {
1469 if (crl
->idp_flags
& IDP_ONLYUSER
)
1472 if (crl
->idp_flags
& IDP_ONLYCA
)
1475 *preasons
= crl
->idp_reasons
;
1476 for (i
= 0; i
< sk_DIST_POINT_num(x
->crldp
); i
++) {
1477 DIST_POINT
*dp
= sk_DIST_POINT_value(x
->crldp
, i
);
1478 if (crldp_check_crlissuer(dp
, crl
, crl_score
)) {
1479 if (!crl
->idp
|| idp_check_dp(dp
->distpoint
, crl
->idp
->distpoint
)) {
1480 *preasons
&= dp
->dp_reasons
;
1485 if ((!crl
->idp
|| !crl
->idp
->distpoint
)
1486 && (crl_score
& CRL_SCORE_ISSUER_NAME
))
1492 * Retrieve CRL corresponding to current certificate. If deltas enabled try
1493 * to find a delta CRL too
1496 static int get_crl_delta(X509_STORE_CTX
*ctx
,
1497 X509_CRL
**pcrl
, X509_CRL
**pdcrl
, X509
*x
)
1500 X509
*issuer
= NULL
;
1502 unsigned int reasons
;
1503 X509_CRL
*crl
= NULL
, *dcrl
= NULL
;
1504 STACK_OF(X509_CRL
) *skcrl
;
1505 const X509_NAME
*nm
= X509_get_issuer_name(x
);
1507 reasons
= ctx
->current_reasons
;
1508 ok
= get_crl_sk(ctx
, &crl
, &dcrl
,
1509 &issuer
, &crl_score
, &reasons
, ctx
->crls
);
1513 /* Lookup CRLs from store */
1515 skcrl
= ctx
->lookup_crls(ctx
, nm
);
1517 /* If no CRLs found and a near match from get_crl_sk use that */
1521 get_crl_sk(ctx
, &crl
, &dcrl
, &issuer
, &crl_score
, &reasons
, skcrl
);
1523 sk_X509_CRL_pop_free(skcrl
, X509_CRL_free
);
1526 /* If we got any kind of CRL use it and return success */
1528 ctx
->current_issuer
= issuer
;
1529 ctx
->current_crl_score
= crl_score
;
1530 ctx
->current_reasons
= reasons
;
1538 /* Check CRL validity */
1539 static int check_crl(X509_STORE_CTX
*ctx
, X509_CRL
*crl
)
1541 X509
*issuer
= NULL
;
1542 EVP_PKEY
*ikey
= NULL
;
1543 int cnum
= ctx
->error_depth
;
1544 int chnum
= sk_X509_num(ctx
->chain
) - 1;
1546 /* If we have an alternative CRL issuer cert use that */
1547 if (ctx
->current_issuer
)
1548 issuer
= ctx
->current_issuer
;
1550 * Else find CRL issuer: if not last certificate then issuer is next
1551 * certificate in chain.
1553 else if (cnum
< chnum
)
1554 issuer
= sk_X509_value(ctx
->chain
, cnum
+ 1);
1556 issuer
= sk_X509_value(ctx
->chain
, chnum
);
1557 /* If not self-issued, can't check signature */
1558 if (!ctx
->check_issued(ctx
, issuer
, issuer
) &&
1559 !verify_cb_crl(ctx
, X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
))
1567 * Skip most tests for deltas because they have already been done
1569 if (!crl
->base_crl_number
) {
1570 /* Check for cRLSign bit if keyUsage present */
1571 if ((issuer
->ex_flags
& EXFLAG_KUSAGE
) &&
1572 !(issuer
->ex_kusage
& KU_CRL_SIGN
) &&
1573 !verify_cb_crl(ctx
, X509_V_ERR_KEYUSAGE_NO_CRL_SIGN
))
1576 if (!(ctx
->current_crl_score
& CRL_SCORE_SCOPE
) &&
1577 !verify_cb_crl(ctx
, X509_V_ERR_DIFFERENT_CRL_SCOPE
))
1580 if (!(ctx
->current_crl_score
& CRL_SCORE_SAME_PATH
) &&
1581 check_crl_path(ctx
, ctx
->current_issuer
) <= 0 &&
1582 !verify_cb_crl(ctx
, X509_V_ERR_CRL_PATH_VALIDATION_ERROR
))
1585 if ((crl
->idp_flags
& IDP_INVALID
) &&
1586 !verify_cb_crl(ctx
, X509_V_ERR_INVALID_EXTENSION
))
1590 if (!(ctx
->current_crl_score
& CRL_SCORE_TIME
) &&
1591 !check_crl_time(ctx
, crl
, 1))
1594 /* Attempt to get issuer certificate public key */
1595 ikey
= X509_get0_pubkey(issuer
);
1598 !verify_cb_crl(ctx
, X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
))
1602 int rv
= X509_CRL_check_suiteb(crl
, ikey
, ctx
->param
->flags
);
1604 if (rv
!= X509_V_OK
&& !verify_cb_crl(ctx
, rv
))
1606 /* Verify CRL signature */
1607 if (X509_CRL_verify(crl
, ikey
) <= 0 &&
1608 !verify_cb_crl(ctx
, X509_V_ERR_CRL_SIGNATURE_FAILURE
))
1614 /* Check certificate against CRL */
1615 static int cert_crl(X509_STORE_CTX
*ctx
, X509_CRL
*crl
, X509
*x
)
1620 * The rules changed for this... previously if a CRL contained unhandled
1621 * critical extensions it could still be used to indicate a certificate
1622 * was revoked. This has since been changed since critical extensions can
1623 * change the meaning of CRL entries.
1625 if (!(ctx
->param
->flags
& X509_V_FLAG_IGNORE_CRITICAL
)
1626 && (crl
->flags
& EXFLAG_CRITICAL
) &&
1627 !verify_cb_crl(ctx
, X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION
))
1630 * Look for serial number of certificate in CRL. If found, make sure
1631 * reason is not removeFromCRL.
1633 if (X509_CRL_get0_by_cert(crl
, &rev
, x
)) {
1634 if (rev
->reason
== CRL_REASON_REMOVE_FROM_CRL
)
1636 if (!verify_cb_crl(ctx
, X509_V_ERR_CERT_REVOKED
))
1643 static int check_policy(X509_STORE_CTX
*ctx
)
1650 * With DANE, the trust anchor might be a bare public key, not a
1651 * certificate! In that case our chain does not have the trust anchor
1652 * certificate as a top-most element. This comports well with RFC5280
1653 * chain verification, since there too, the trust anchor is not part of the
1654 * chain to be verified. In particular, X509_policy_check() does not look
1655 * at the TA cert, but assumes that it is present as the top-most chain
1656 * element. We therefore temporarily push a NULL cert onto the chain if it
1657 * was verified via a bare public key, and pop it off right after the
1658 * X509_policy_check() call.
1660 if (ctx
->bare_ta_signed
&& !sk_X509_push(ctx
->chain
, NULL
)) {
1661 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
1662 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
1665 ret
= X509_policy_check(&ctx
->tree
, &ctx
->explicit_policy
, ctx
->chain
,
1666 ctx
->param
->policies
, ctx
->param
->flags
);
1667 if (ctx
->bare_ta_signed
)
1668 (void)sk_X509_pop(ctx
->chain
);
1670 if (ret
== X509_PCY_TREE_INTERNAL
) {
1671 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
1672 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
1675 /* Invalid or inconsistent extensions */
1676 if (ret
== X509_PCY_TREE_INVALID
) {
1679 /* Locate certificates with bad extensions and notify callback. */
1680 for (i
= 1; i
< sk_X509_num(ctx
->chain
); i
++) {
1681 X509
*x
= sk_X509_value(ctx
->chain
, i
);
1683 CHECK_CB((x
->ex_flags
& EXFLAG_INVALID_POLICY
) != 0,
1684 ctx
, x
, i
, X509_V_ERR_INVALID_POLICY_EXTENSION
);
1688 if (ret
== X509_PCY_TREE_FAILURE
) {
1689 ctx
->current_cert
= NULL
;
1690 ctx
->error
= X509_V_ERR_NO_EXPLICIT_POLICY
;
1691 return ctx
->verify_cb(0, ctx
);
1693 if (ret
!= X509_PCY_TREE_VALID
) {
1694 ERR_raise(ERR_LIB_X509
, ERR_R_INTERNAL_ERROR
);
1698 if (ctx
->param
->flags
& X509_V_FLAG_NOTIFY_POLICY
) {
1699 ctx
->current_cert
= NULL
;
1701 * Verification errors need to be "sticky", a callback may have allowed
1702 * an SSL handshake to continue despite an error, and we must then
1703 * remain in an error state. Therefore, we MUST NOT clear earlier
1704 * verification errors by setting the error to X509_V_OK.
1706 if (!ctx
->verify_cb(2, ctx
))
1714 * Check certificate validity times.
1715 * If depth >= 0, invoke verification callbacks on error, otherwise just return
1716 * the validation status.
1718 * Return 1 on success, 0 otherwise.
1720 int x509_check_cert_time(X509_STORE_CTX
*ctx
, X509
*x
, int depth
)
1725 if (ctx
->param
->flags
& X509_V_FLAG_USE_CHECK_TIME
)
1726 ptime
= &ctx
->param
->check_time
;
1727 else if (ctx
->param
->flags
& X509_V_FLAG_NO_CHECK_TIME
)
1732 i
= X509_cmp_time(X509_get0_notBefore(x
), ptime
);
1733 if (i
>= 0 && depth
< 0)
1735 CHECK_CB(i
== 0, ctx
, x
, depth
, X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
);
1736 CHECK_CB(i
> 0, ctx
, x
, depth
, X509_V_ERR_CERT_NOT_YET_VALID
);
1738 i
= X509_cmp_time(X509_get0_notAfter(x
), ptime
);
1739 if (i
<= 0 && depth
< 0)
1741 CHECK_CB(i
== 0, ctx
, x
, depth
, X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
);
1742 CHECK_CB(i
< 0, ctx
, x
, depth
, X509_V_ERR_CERT_HAS_EXPIRED
);
1746 /* verify the issuer signatures and cert times of ctx->chain */
1747 static int internal_verify(X509_STORE_CTX
*ctx
)
1749 int n
= sk_X509_num(ctx
->chain
) - 1;
1750 X509
*xi
= sk_X509_value(ctx
->chain
, n
);
1754 * With DANE-verified bare public key TA signatures, it remains only to
1755 * check the timestamps of the top certificate. We report the issuer as
1756 * NULL, since all we have is a bare key.
1758 if (ctx
->bare_ta_signed
) {
1761 goto check_cert_time
;
1764 if (ctx
->check_issued(ctx
, xi
, xi
))
1765 xs
= xi
; /* the typical case: last cert in the chain is self-issued */
1767 if (ctx
->param
->flags
& X509_V_FLAG_PARTIAL_CHAIN
) {
1769 goto check_cert_time
;
1772 CHECK_CB(1, ctx
, xi
, 0, X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
);
1775 goto check_cert_time
;
1779 ctx
->error_depth
= n
;
1780 xs
= sk_X509_value(ctx
->chain
, n
);
1784 * Do not clear ctx->error=0, it must be "sticky", only the user's callback
1785 * is allowed to reset errors (at its own peril).
1789 * For each iteration of this loop:
1790 * n is the subject depth
1791 * xs is the subject cert, for which the signature is to be checked
1792 * xi is the supposed issuer cert containing the public key to use
1793 * Initially xs == xi if the last cert in the chain is self-issued.
1795 * Skip signature check for self-signed certificates unless explicitly
1796 * asked for because it does not add any security and just wastes time.
1798 if (xs
!= xi
|| ((ctx
->param
->flags
& X509_V_FLAG_CHECK_SS_SIGNATURE
)
1799 && (xi
->ex_flags
& EXFLAG_SS
) != 0)) {
1802 * If the issuer's public key is not available or its key usage
1803 * does not support issuing the subject cert, report the issuer
1804 * cert and its depth (rather than n, the depth of the subject).
1806 int issuer_depth
= n
+ (xs
== xi
? 0 : 1);
1808 * According to https://tools.ietf.org/html/rfc5280#section-6.1.4
1809 * step (n) we must check any given key usage extension in a CA cert
1810 * when preparing the verification of a certificate issued by it.
1811 * According to https://tools.ietf.org/html/rfc5280#section-4.2.1.3
1812 * we must not verify a certifiate signature if the key usage of the
1813 * CA certificate that issued the certificate prohibits signing.
1814 * In case the 'issuing' certificate is the last in the chain and is
1815 * not a CA certificate but a 'self-issued' end-entity cert (i.e.,
1816 * xs == xi && !(xi->ex_flags & EXFLAG_CA)) RFC 5280 does not apply
1817 * (see https://tools.ietf.org/html/rfc6818#section-2) and thus
1818 * we are free to ignore any key usage restrictions on such certs.
1820 int ret
= xs
== xi
&& (xi
->ex_flags
& EXFLAG_CA
) == 0
1821 ? X509_V_OK
: x509_signing_allowed(xi
, xs
);
1823 CHECK_CB(ret
!= X509_V_OK
, ctx
, xi
, issuer_depth
, ret
);
1824 if ((pkey
= X509_get0_pubkey(xi
)) == NULL
) {
1825 CHECK_CB(1, ctx
, xi
, issuer_depth
,
1826 X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
);
1828 CHECK_CB(X509_verify(xs
, pkey
) <= 0,
1829 ctx
, xs
, n
, X509_V_ERR_CERT_SIGNATURE_FAILURE
);
1834 /* Calls verify callback as needed */
1835 if (!x509_check_cert_time(ctx
, xs
, n
))
1839 * Signal success at this depth. However, the previous error (if any)
1842 ctx
->current_issuer
= xi
;
1843 ctx
->current_cert
= xs
;
1844 ctx
->error_depth
= n
;
1845 if (!ctx
->verify_cb(1, ctx
))
1850 xs
= sk_X509_value(ctx
->chain
, n
);
1856 int X509_cmp_current_time(const ASN1_TIME
*ctm
)
1858 return X509_cmp_time(ctm
, NULL
);
1861 int X509_cmp_time(const ASN1_TIME
*ctm
, time_t *cmp_time
)
1863 static const size_t utctime_length
= sizeof("YYMMDDHHMMSSZ") - 1;
1864 static const size_t generalizedtime_length
= sizeof("YYYYMMDDHHMMSSZ") - 1;
1865 ASN1_TIME
*asn1_cmp_time
= NULL
;
1866 int i
, day
, sec
, ret
= 0;
1867 #ifdef CHARSET_EBCDIC
1868 const char upper_z
= 0x5A;
1870 const char upper_z
= 'Z';
1873 * Note that ASN.1 allows much more slack in the time format than RFC5280.
1874 * In RFC5280, the representation is fixed:
1875 * UTCTime: YYMMDDHHMMSSZ
1876 * GeneralizedTime: YYYYMMDDHHMMSSZ
1878 * We do NOT currently enforce the following RFC 5280 requirement:
1879 * "CAs conforming to this profile MUST always encode certificate
1880 * validity dates through the year 2049 as UTCTime; certificate validity
1881 * dates in 2050 or later MUST be encoded as GeneralizedTime."
1883 switch (ctm
->type
) {
1884 case V_ASN1_UTCTIME
:
1885 if (ctm
->length
!= (int)(utctime_length
))
1888 case V_ASN1_GENERALIZEDTIME
:
1889 if (ctm
->length
!= (int)(generalizedtime_length
))
1897 * Verify the format: the ASN.1 functions we use below allow a more
1898 * flexible format than what's mandated by RFC 5280.
1899 * Digit and date ranges will be verified in the conversion methods.
1901 for (i
= 0; i
< ctm
->length
- 1; i
++) {
1902 if (!ascii_isdigit(ctm
->data
[i
]))
1905 if (ctm
->data
[ctm
->length
- 1] != upper_z
)
1909 * There is ASN1_UTCTIME_cmp_time_t but no
1910 * ASN1_GENERALIZEDTIME_cmp_time_t or ASN1_TIME_cmp_time_t,
1911 * so we go through ASN.1
1913 asn1_cmp_time
= X509_time_adj(NULL
, 0, cmp_time
);
1914 if (asn1_cmp_time
== NULL
)
1916 if (!ASN1_TIME_diff(&day
, &sec
, ctm
, asn1_cmp_time
))
1920 * X509_cmp_time comparison is <=.
1921 * The return value 0 is reserved for errors.
1923 ret
= (day
>= 0 && sec
>= 0) ? -1 : 1;
1926 ASN1_TIME_free(asn1_cmp_time
);
1931 * Return 0 if time should not be checked or reference time is in range,
1932 * or else 1 if it is past the end, or -1 if it is before the start
1934 int X509_cmp_timeframe(const X509_VERIFY_PARAM
*vpm
,
1935 const ASN1_TIME
*start
, const ASN1_TIME
*end
)
1938 time_t *time
= NULL
;
1939 unsigned long flags
= vpm
== NULL
? 0 : X509_VERIFY_PARAM_get_flags(vpm
);
1941 if ((flags
& X509_V_FLAG_USE_CHECK_TIME
) != 0) {
1942 ref_time
= X509_VERIFY_PARAM_get_time(vpm
);
1944 } else if ((flags
& X509_V_FLAG_NO_CHECK_TIME
) != 0) {
1945 return 0; /* this means ok */
1946 } /* else reference time is the current time */
1948 if (end
!= NULL
&& X509_cmp_time(end
, time
) < 0)
1950 if (start
!= NULL
&& X509_cmp_time(start
, time
) > 0)
1955 ASN1_TIME
*X509_gmtime_adj(ASN1_TIME
*s
, long adj
)
1957 return X509_time_adj(s
, adj
, NULL
);
1960 ASN1_TIME
*X509_time_adj(ASN1_TIME
*s
, long offset_sec
, time_t *in_tm
)
1962 return X509_time_adj_ex(s
, 0, offset_sec
, in_tm
);
1965 ASN1_TIME
*X509_time_adj_ex(ASN1_TIME
*s
,
1966 int offset_day
, long offset_sec
, time_t *in_tm
)
1975 if (s
&& !(s
->flags
& ASN1_STRING_FLAG_MSTRING
)) {
1976 if (s
->type
== V_ASN1_UTCTIME
)
1977 return ASN1_UTCTIME_adj(s
, t
, offset_day
, offset_sec
);
1978 if (s
->type
== V_ASN1_GENERALIZEDTIME
)
1979 return ASN1_GENERALIZEDTIME_adj(s
, t
, offset_day
, offset_sec
);
1981 return ASN1_TIME_adj(s
, t
, offset_day
, offset_sec
);
1984 int X509_get_pubkey_parameters(EVP_PKEY
*pkey
, STACK_OF(X509
) *chain
)
1986 EVP_PKEY
*ktmp
= NULL
, *ktmp2
;
1989 if ((pkey
!= NULL
) && !EVP_PKEY_missing_parameters(pkey
))
1992 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
1993 ktmp
= X509_get0_pubkey(sk_X509_value(chain
, i
));
1995 ERR_raise(ERR_LIB_X509
, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY
);
1998 if (!EVP_PKEY_missing_parameters(ktmp
))
2002 ERR_raise(ERR_LIB_X509
, X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN
);
2006 /* first, populate the other certs */
2007 for (j
= i
- 1; j
>= 0; j
--) {
2008 ktmp2
= X509_get0_pubkey(sk_X509_value(chain
, j
));
2009 EVP_PKEY_copy_parameters(ktmp2
, ktmp
);
2013 EVP_PKEY_copy_parameters(pkey
, ktmp
);
2017 /* Make a delta CRL as the diff between two full CRLs */
2019 X509_CRL
*X509_CRL_diff(X509_CRL
*base
, X509_CRL
*newer
,
2020 EVP_PKEY
*skey
, const EVP_MD
*md
, unsigned int flags
)
2022 X509_CRL
*crl
= NULL
;
2024 STACK_OF(X509_REVOKED
) *revs
= NULL
;
2025 /* CRLs can't be delta already */
2026 if (base
->base_crl_number
|| newer
->base_crl_number
) {
2027 ERR_raise(ERR_LIB_X509
, X509_R_CRL_ALREADY_DELTA
);
2030 /* Base and new CRL must have a CRL number */
2031 if (!base
->crl_number
|| !newer
->crl_number
) {
2032 ERR_raise(ERR_LIB_X509
, X509_R_NO_CRL_NUMBER
);
2035 /* Issuer names must match */
2036 if (X509_NAME_cmp(X509_CRL_get_issuer(base
), X509_CRL_get_issuer(newer
))) {
2037 ERR_raise(ERR_LIB_X509
, X509_R_ISSUER_MISMATCH
);
2040 /* AKID and IDP must match */
2041 if (!crl_extension_match(base
, newer
, NID_authority_key_identifier
)) {
2042 ERR_raise(ERR_LIB_X509
, X509_R_AKID_MISMATCH
);
2045 if (!crl_extension_match(base
, newer
, NID_issuing_distribution_point
)) {
2046 ERR_raise(ERR_LIB_X509
, X509_R_IDP_MISMATCH
);
2049 /* Newer CRL number must exceed full CRL number */
2050 if (ASN1_INTEGER_cmp(newer
->crl_number
, base
->crl_number
) <= 0) {
2051 ERR_raise(ERR_LIB_X509
, X509_R_NEWER_CRL_NOT_NEWER
);
2054 /* CRLs must verify */
2055 if (skey
&& (X509_CRL_verify(base
, skey
) <= 0 ||
2056 X509_CRL_verify(newer
, skey
) <= 0)) {
2057 ERR_raise(ERR_LIB_X509
, X509_R_CRL_VERIFY_FAILURE
);
2060 /* Create new CRL */
2061 crl
= X509_CRL_new();
2062 if (crl
== NULL
|| !X509_CRL_set_version(crl
, 1))
2064 /* Set issuer name */
2065 if (!X509_CRL_set_issuer_name(crl
, X509_CRL_get_issuer(newer
)))
2068 if (!X509_CRL_set1_lastUpdate(crl
, X509_CRL_get0_lastUpdate(newer
)))
2070 if (!X509_CRL_set1_nextUpdate(crl
, X509_CRL_get0_nextUpdate(newer
)))
2073 /* Set base CRL number: must be critical */
2075 if (!X509_CRL_add1_ext_i2d(crl
, NID_delta_crl
, base
->crl_number
, 1, 0))
2079 * Copy extensions across from newest CRL to delta: this will set CRL
2080 * number to correct value too.
2083 for (i
= 0; i
< X509_CRL_get_ext_count(newer
); i
++) {
2084 X509_EXTENSION
*ext
;
2085 ext
= X509_CRL_get_ext(newer
, i
);
2086 if (!X509_CRL_add_ext(crl
, ext
, -1))
2090 /* Go through revoked entries, copying as needed */
2092 revs
= X509_CRL_get_REVOKED(newer
);
2094 for (i
= 0; i
< sk_X509_REVOKED_num(revs
); i
++) {
2095 X509_REVOKED
*rvn
, *rvtmp
;
2096 rvn
= sk_X509_REVOKED_value(revs
, i
);
2098 * Add only if not also in base. TODO: need something cleverer here
2099 * for some more complex CRLs covering multiple CAs.
2101 if (!X509_CRL_get0_by_serial(base
, &rvtmp
, &rvn
->serialNumber
)) {
2102 rvtmp
= X509_REVOKED_dup(rvn
);
2105 if (!X509_CRL_add0_revoked(crl
, rvtmp
)) {
2106 X509_REVOKED_free(rvtmp
);
2111 /* TODO: optionally prune deleted entries */
2113 if (skey
&& md
&& !X509_CRL_sign(crl
, skey
, md
))
2119 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
2124 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX
*ctx
, int idx
, void *data
)
2126 return CRYPTO_set_ex_data(&ctx
->ex_data
, idx
, data
);
2129 void *X509_STORE_CTX_get_ex_data(const X509_STORE_CTX
*ctx
, int idx
)
2131 return CRYPTO_get_ex_data(&ctx
->ex_data
, idx
);
2134 int X509_STORE_CTX_get_error(const X509_STORE_CTX
*ctx
)
2139 void X509_STORE_CTX_set_error(X509_STORE_CTX
*ctx
, int err
)
2144 int X509_STORE_CTX_get_error_depth(const X509_STORE_CTX
*ctx
)
2146 return ctx
->error_depth
;
2149 void X509_STORE_CTX_set_error_depth(X509_STORE_CTX
*ctx
, int depth
)
2151 ctx
->error_depth
= depth
;
2154 X509
*X509_STORE_CTX_get_current_cert(const X509_STORE_CTX
*ctx
)
2156 return ctx
->current_cert
;
2159 void X509_STORE_CTX_set_current_cert(X509_STORE_CTX
*ctx
, X509
*x
)
2161 ctx
->current_cert
= x
;
2164 STACK_OF(X509
) *X509_STORE_CTX_get0_chain(const X509_STORE_CTX
*ctx
)
2169 STACK_OF(X509
) *X509_STORE_CTX_get1_chain(const X509_STORE_CTX
*ctx
)
2173 return X509_chain_up_ref(ctx
->chain
);
2176 X509
*X509_STORE_CTX_get0_current_issuer(const X509_STORE_CTX
*ctx
)
2178 return ctx
->current_issuer
;
2181 X509_CRL
*X509_STORE_CTX_get0_current_crl(const X509_STORE_CTX
*ctx
)
2183 return ctx
->current_crl
;
2186 X509_STORE_CTX
*X509_STORE_CTX_get0_parent_ctx(const X509_STORE_CTX
*ctx
)
2191 void X509_STORE_CTX_set_cert(X509_STORE_CTX
*ctx
, X509
*x
)
2196 void X509_STORE_CTX_set0_crls(X509_STORE_CTX
*ctx
, STACK_OF(X509_CRL
) *sk
)
2201 int X509_STORE_CTX_set_purpose(X509_STORE_CTX
*ctx
, int purpose
)
2204 * XXX: Why isn't this function always used to set the associated trust?
2205 * Should there even be a VPM->trust field at all? Or should the trust
2206 * always be inferred from the purpose by X509_STORE_CTX_init().
2208 return X509_STORE_CTX_purpose_inherit(ctx
, 0, purpose
, 0);
2211 int X509_STORE_CTX_set_trust(X509_STORE_CTX
*ctx
, int trust
)
2214 * XXX: See above, this function would only be needed when the default
2215 * trust for the purpose needs an override in a corner case.
2217 return X509_STORE_CTX_purpose_inherit(ctx
, 0, 0, trust
);
2221 * This function is used to set the X509_STORE_CTX purpose and trust values.
2222 * This is intended to be used when another structure has its own trust and
2223 * purpose values which (if set) will be inherited by the ctx. If they aren't
2224 * set then we will usually have a default purpose in mind which should then
2225 * be used to set the trust value. An example of this is SSL use: an SSL
2226 * structure will have its own purpose and trust settings which the
2227 * application can set: if they aren't set then we use the default of SSL
2231 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX
*ctx
, int def_purpose
,
2232 int purpose
, int trust
)
2235 /* If purpose not set use default */
2237 purpose
= def_purpose
;
2238 /* If we have a purpose then check it is valid */
2241 idx
= X509_PURPOSE_get_by_id(purpose
);
2243 ERR_raise(ERR_LIB_X509
, X509_R_UNKNOWN_PURPOSE_ID
);
2246 ptmp
= X509_PURPOSE_get0(idx
);
2247 if (ptmp
->trust
== X509_TRUST_DEFAULT
) {
2248 idx
= X509_PURPOSE_get_by_id(def_purpose
);
2250 * XXX: In the two callers above def_purpose is always 0, which is
2251 * not a known value, so idx will always be -1. How is the
2252 * X509_TRUST_DEFAULT case actually supposed to be handled?
2255 ERR_raise(ERR_LIB_X509
, X509_R_UNKNOWN_PURPOSE_ID
);
2258 ptmp
= X509_PURPOSE_get0(idx
);
2260 /* If trust not set then get from purpose default */
2262 trust
= ptmp
->trust
;
2265 idx
= X509_TRUST_get_by_id(trust
);
2267 ERR_raise(ERR_LIB_X509
, X509_R_UNKNOWN_TRUST_ID
);
2272 if (purpose
&& !ctx
->param
->purpose
)
2273 ctx
->param
->purpose
= purpose
;
2274 if (trust
&& !ctx
->param
->trust
)
2275 ctx
->param
->trust
= trust
;
2279 X509_STORE_CTX
*X509_STORE_CTX_new_ex(OSSL_LIB_CTX
*libctx
, const char *propq
)
2281 X509_STORE_CTX
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
2284 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
2288 ctx
->libctx
= libctx
;
2289 if (propq
!= NULL
) {
2290 ctx
->propq
= OPENSSL_strdup(propq
);
2291 if (ctx
->propq
== NULL
) {
2293 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
2301 X509_STORE_CTX
*X509_STORE_CTX_new(void)
2303 return X509_STORE_CTX_new_ex(NULL
, NULL
);
2307 void X509_STORE_CTX_free(X509_STORE_CTX
*ctx
)
2312 X509_STORE_CTX_cleanup(ctx
);
2314 /* libctx and propq survive X509_STORE_CTX_cleanup() */
2315 OPENSSL_free(ctx
->propq
);
2320 int X509_STORE_CTX_init(X509_STORE_CTX
*ctx
, X509_STORE
*store
, X509
*x509
,
2321 STACK_OF(X509
) *chain
)
2327 ctx
->untrusted
= chain
;
2329 ctx
->num_untrusted
= 0;
2330 ctx
->other_ctx
= NULL
;
2334 ctx
->explicit_policy
= 0;
2335 ctx
->error_depth
= 0;
2336 ctx
->current_cert
= NULL
;
2337 ctx
->current_issuer
= NULL
;
2338 ctx
->current_crl
= NULL
;
2339 ctx
->current_crl_score
= 0;
2340 ctx
->current_reasons
= 0;
2344 ctx
->bare_ta_signed
= 0;
2345 /* Zero ex_data to make sure we're cleanup-safe */
2346 memset(&ctx
->ex_data
, 0, sizeof(ctx
->ex_data
));
2348 /* store->cleanup is always 0 in OpenSSL, if set must be idempotent */
2350 ctx
->cleanup
= store
->cleanup
;
2354 if (store
&& store
->check_issued
)
2355 ctx
->check_issued
= store
->check_issued
;
2357 ctx
->check_issued
= check_issued
;
2359 if (store
&& store
->get_issuer
)
2360 ctx
->get_issuer
= store
->get_issuer
;
2362 ctx
->get_issuer
= X509_STORE_CTX_get1_issuer
;
2364 if (store
&& store
->verify_cb
)
2365 ctx
->verify_cb
= store
->verify_cb
;
2367 ctx
->verify_cb
= null_callback
;
2369 if (store
&& store
->verify
)
2370 ctx
->verify
= store
->verify
;
2372 ctx
->verify
= internal_verify
;
2374 if (store
&& store
->check_revocation
)
2375 ctx
->check_revocation
= store
->check_revocation
;
2377 ctx
->check_revocation
= check_revocation
;
2379 if (store
&& store
->get_crl
)
2380 ctx
->get_crl
= store
->get_crl
;
2382 ctx
->get_crl
= NULL
;
2384 if (store
&& store
->check_crl
)
2385 ctx
->check_crl
= store
->check_crl
;
2387 ctx
->check_crl
= check_crl
;
2389 if (store
&& store
->cert_crl
)
2390 ctx
->cert_crl
= store
->cert_crl
;
2392 ctx
->cert_crl
= cert_crl
;
2394 if (store
&& store
->check_policy
)
2395 ctx
->check_policy
= store
->check_policy
;
2397 ctx
->check_policy
= check_policy
;
2399 if (store
&& store
->lookup_certs
)
2400 ctx
->lookup_certs
= store
->lookup_certs
;
2402 ctx
->lookup_certs
= X509_STORE_CTX_get1_certs
;
2404 if (store
&& store
->lookup_crls
)
2405 ctx
->lookup_crls
= store
->lookup_crls
;
2407 ctx
->lookup_crls
= X509_STORE_CTX_get1_crls
;
2409 ctx
->param
= X509_VERIFY_PARAM_new();
2410 if (ctx
->param
== NULL
) {
2411 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
2416 * Inherit callbacks and flags from X509_STORE if not set use defaults.
2419 ret
= X509_VERIFY_PARAM_inherit(ctx
->param
, store
->param
);
2421 ctx
->param
->inh_flags
|= X509_VP_FLAG_DEFAULT
| X509_VP_FLAG_ONCE
;
2424 ret
= X509_VERIFY_PARAM_inherit(ctx
->param
,
2425 X509_VERIFY_PARAM_lookup("default"));
2428 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
2433 * XXX: For now, continue to inherit trust from VPM, but infer from the
2434 * purpose if this still yields the default value.
2436 if (ctx
->param
->trust
== X509_TRUST_DEFAULT
) {
2437 int idx
= X509_PURPOSE_get_by_id(ctx
->param
->purpose
);
2438 X509_PURPOSE
*xp
= X509_PURPOSE_get0(idx
);
2441 ctx
->param
->trust
= X509_PURPOSE_get_trust(xp
);
2444 if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX
, ctx
,
2447 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
2451 * On error clean up allocated storage, if the store context was not
2452 * allocated with X509_STORE_CTX_new() this is our last chance to do so.
2454 X509_STORE_CTX_cleanup(ctx
);
2459 * Set alternative lookup method: just a STACK of trusted certificates. This
2460 * avoids X509_STORE nastiness where it isn't needed.
2462 void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
)
2464 ctx
->other_ctx
= sk
;
2465 ctx
->get_issuer
= get_issuer_sk
;
2466 ctx
->lookup_certs
= lookup_certs_sk
;
2469 void X509_STORE_CTX_cleanup(X509_STORE_CTX
*ctx
)
2472 * We need to be idempotent because, unfortunately, free() also calls
2473 * cleanup(), so the natural call sequence new(), init(), cleanup(), free()
2474 * calls cleanup() for the same object twice! Thus we must zero the
2475 * pointers below after they're freed!
2477 /* Seems to always be 0 in OpenSSL, do this at most once. */
2478 if (ctx
->cleanup
!= NULL
) {
2480 ctx
->cleanup
= NULL
;
2482 if (ctx
->param
!= NULL
) {
2483 if (ctx
->parent
== NULL
)
2484 X509_VERIFY_PARAM_free(ctx
->param
);
2487 X509_policy_tree_free(ctx
->tree
);
2489 sk_X509_pop_free(ctx
->chain
, X509_free
);
2491 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX
, ctx
, &(ctx
->ex_data
));
2492 memset(&ctx
->ex_data
, 0, sizeof(ctx
->ex_data
));
2495 void X509_STORE_CTX_set_depth(X509_STORE_CTX
*ctx
, int depth
)
2497 X509_VERIFY_PARAM_set_depth(ctx
->param
, depth
);
2500 void X509_STORE_CTX_set_flags(X509_STORE_CTX
*ctx
, unsigned long flags
)
2502 X509_VERIFY_PARAM_set_flags(ctx
->param
, flags
);
2505 void X509_STORE_CTX_set_time(X509_STORE_CTX
*ctx
, unsigned long flags
,
2508 X509_VERIFY_PARAM_set_time(ctx
->param
, t
);
2511 X509
*X509_STORE_CTX_get0_cert(const X509_STORE_CTX
*ctx
)
2516 STACK_OF(X509
) *X509_STORE_CTX_get0_untrusted(const X509_STORE_CTX
*ctx
)
2518 return ctx
->untrusted
;
2521 void X509_STORE_CTX_set0_untrusted(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
)
2523 ctx
->untrusted
= sk
;
2526 void X509_STORE_CTX_set0_verified_chain(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
)
2528 sk_X509_pop_free(ctx
->chain
, X509_free
);
2532 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX
*ctx
,
2533 X509_STORE_CTX_verify_cb verify_cb
)
2535 ctx
->verify_cb
= verify_cb
;
2538 X509_STORE_CTX_verify_cb
X509_STORE_CTX_get_verify_cb(const X509_STORE_CTX
*ctx
)
2540 return ctx
->verify_cb
;
2543 void X509_STORE_CTX_set_verify(X509_STORE_CTX
*ctx
,
2544 X509_STORE_CTX_verify_fn verify
)
2546 ctx
->verify
= verify
;
2549 X509_STORE_CTX_verify_fn
X509_STORE_CTX_get_verify(const X509_STORE_CTX
*ctx
)
2554 X509_STORE_CTX_get_issuer_fn
X509_STORE_CTX_get_get_issuer(const X509_STORE_CTX
*ctx
)
2556 return ctx
->get_issuer
;
2559 X509_STORE_CTX_check_issued_fn
2560 X509_STORE_CTX_get_check_issued(const X509_STORE_CTX
*ctx
)
2562 return ctx
->check_issued
;
2565 X509_STORE_CTX_check_revocation_fn
2566 X509_STORE_CTX_get_check_revocation(const X509_STORE_CTX
*ctx
)
2568 return ctx
->check_revocation
;
2571 X509_STORE_CTX_get_crl_fn
X509_STORE_CTX_get_get_crl(const X509_STORE_CTX
*ctx
)
2573 return ctx
->get_crl
;
2576 X509_STORE_CTX_check_crl_fn
X509_STORE_CTX_get_check_crl(const X509_STORE_CTX
*ctx
)
2578 return ctx
->check_crl
;
2581 X509_STORE_CTX_cert_crl_fn
X509_STORE_CTX_get_cert_crl(const X509_STORE_CTX
*ctx
)
2583 return ctx
->cert_crl
;
2586 X509_STORE_CTX_check_policy_fn
2587 X509_STORE_CTX_get_check_policy(const X509_STORE_CTX
*ctx
)
2589 return ctx
->check_policy
;
2592 X509_STORE_CTX_lookup_certs_fn
2593 X509_STORE_CTX_get_lookup_certs(const X509_STORE_CTX
*ctx
)
2595 return ctx
->lookup_certs
;
2598 X509_STORE_CTX_lookup_crls_fn
2599 X509_STORE_CTX_get_lookup_crls(const X509_STORE_CTX
*ctx
)
2601 return ctx
->lookup_crls
;
2604 X509_STORE_CTX_cleanup_fn
X509_STORE_CTX_get_cleanup(const X509_STORE_CTX
*ctx
)
2606 return ctx
->cleanup
;
2609 X509_POLICY_TREE
*X509_STORE_CTX_get0_policy_tree(const X509_STORE_CTX
*ctx
)
2614 int X509_STORE_CTX_get_explicit_policy(const X509_STORE_CTX
*ctx
)
2616 return ctx
->explicit_policy
;
2619 int X509_STORE_CTX_get_num_untrusted(const X509_STORE_CTX
*ctx
)
2621 return ctx
->num_untrusted
;
2624 int X509_STORE_CTX_set_default(X509_STORE_CTX
*ctx
, const char *name
)
2626 const X509_VERIFY_PARAM
*param
;
2628 param
= X509_VERIFY_PARAM_lookup(name
);
2631 return X509_VERIFY_PARAM_inherit(ctx
->param
, param
);
2634 X509_VERIFY_PARAM
*X509_STORE_CTX_get0_param(const X509_STORE_CTX
*ctx
)
2639 void X509_STORE_CTX_set0_param(X509_STORE_CTX
*ctx
, X509_VERIFY_PARAM
*param
)
2641 X509_VERIFY_PARAM_free(ctx
->param
);
2645 void X509_STORE_CTX_set0_dane(X509_STORE_CTX
*ctx
, SSL_DANE
*dane
)
2650 static unsigned char *dane_i2d(
2653 unsigned int *i2dlen
)
2655 unsigned char *buf
= NULL
;
2659 * Extract ASN.1 DER form of certificate or public key.
2662 case DANETLS_SELECTOR_CERT
:
2663 len
= i2d_X509(cert
, &buf
);
2665 case DANETLS_SELECTOR_SPKI
:
2666 len
= i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert
), &buf
);
2669 ERR_raise(ERR_LIB_X509
, X509_R_BAD_SELECTOR
);
2673 if (len
< 0 || buf
== NULL
) {
2674 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
2678 *i2dlen
= (unsigned int)len
;
2682 #define DANETLS_NONE 256 /* impossible uint8_t */
2684 static int dane_match(X509_STORE_CTX
*ctx
, X509
*cert
, int depth
)
2686 SSL_DANE
*dane
= ctx
->dane
;
2687 unsigned usage
= DANETLS_NONE
;
2688 unsigned selector
= DANETLS_NONE
;
2689 unsigned ordinal
= DANETLS_NONE
;
2690 unsigned mtype
= DANETLS_NONE
;
2691 unsigned char *i2dbuf
= NULL
;
2692 unsigned int i2dlen
= 0;
2693 unsigned char mdbuf
[EVP_MAX_MD_SIZE
];
2694 unsigned char *cmpbuf
= NULL
;
2695 unsigned int cmplen
= 0;
2699 danetls_record
*t
= NULL
;
2702 mask
= (depth
== 0) ? DANETLS_EE_MASK
: DANETLS_TA_MASK
;
2705 * The trust store is not applicable with DANE-TA(2)
2707 if (depth
>= ctx
->num_untrusted
)
2708 mask
&= DANETLS_PKIX_MASK
;
2711 * If we've previously matched a PKIX-?? record, no need to test any
2712 * further PKIX-?? records, it remains to just build the PKIX chain.
2713 * Had the match been a DANE-?? record, we'd be done already.
2715 if (dane
->mdpth
>= 0)
2716 mask
&= ~DANETLS_PKIX_MASK
;
2719 * https://tools.ietf.org/html/rfc7671#section-5.1
2720 * https://tools.ietf.org/html/rfc7671#section-5.2
2721 * https://tools.ietf.org/html/rfc7671#section-5.3
2722 * https://tools.ietf.org/html/rfc7671#section-5.4
2724 * We handle DANE-EE(3) records first as they require no chain building
2725 * and no expiration or hostname checks. We also process digests with
2726 * higher ordinals first and ignore lower priorities except Full(0) which
2727 * is always processed (last). If none match, we then process PKIX-EE(1).
2729 * NOTE: This relies on DANE usages sorting before the corresponding PKIX
2730 * usages in SSL_dane_tlsa_add(), and also on descending sorting of digest
2731 * priorities. See twin comment in ssl/ssl_lib.c.
2733 * We expect that most TLSA RRsets will have just a single usage, so we
2734 * don't go out of our way to cache multiple selector-specific i2d buffers
2735 * across usages, but if the selector happens to remain the same as switch
2736 * usages, that's OK. Thus, a set of "3 1 1", "3 0 1", "1 1 1", "1 0 1",
2737 * records would result in us generating each of the certificate and public
2738 * key DER forms twice, but more typically we'd just see multiple "3 1 1"
2739 * or multiple "3 0 1" records.
2741 * As soon as we find a match at any given depth, we stop, because either
2742 * we've matched a DANE-?? record and the peer is authenticated, or, after
2743 * exhausting all DANE-?? records, we've matched a PKIX-?? record, which is
2744 * sufficient for DANE, and what remains to do is ordinary PKIX validation.
2746 recnum
= (dane
->umask
& mask
) ? sk_danetls_record_num(dane
->trecs
) : 0;
2747 for (i
= 0; matched
== 0 && i
< recnum
; ++i
) {
2748 t
= sk_danetls_record_value(dane
->trecs
, i
);
2749 if ((DANETLS_USAGE_BIT(t
->usage
) & mask
) == 0)
2751 if (t
->usage
!= usage
) {
2754 /* Reset digest agility for each usage/selector pair */
2755 mtype
= DANETLS_NONE
;
2756 ordinal
= dane
->dctx
->mdord
[t
->mtype
];
2758 if (t
->selector
!= selector
) {
2759 selector
= t
->selector
;
2761 /* Update per-selector state */
2762 OPENSSL_free(i2dbuf
);
2763 i2dbuf
= dane_i2d(cert
, selector
, &i2dlen
);
2767 /* Reset digest agility for each usage/selector pair */
2768 mtype
= DANETLS_NONE
;
2769 ordinal
= dane
->dctx
->mdord
[t
->mtype
];
2770 } else if (t
->mtype
!= DANETLS_MATCHING_FULL
) {
2774 * <https://tools.ietf.org/html/rfc7671#section-9>
2776 * For a fixed selector, after processing all records with the
2777 * highest mtype ordinal, ignore all mtypes with lower ordinals
2778 * other than "Full".
2780 if (dane
->dctx
->mdord
[t
->mtype
] < ordinal
)
2785 * Each time we hit a (new selector or) mtype, re-compute the relevant
2786 * digest, more complex caching is not worth the code space.
2788 if (t
->mtype
!= mtype
) {
2789 const EVP_MD
*md
= dane
->dctx
->mdevp
[mtype
= t
->mtype
];
2795 if (!EVP_Digest(i2dbuf
, i2dlen
, cmpbuf
, &cmplen
, md
, 0)) {
2803 * Squirrel away the certificate and depth if we have a match. Any
2804 * DANE match is dispositive, but with PKIX we still need to build a
2807 if (cmplen
== t
->dlen
&&
2808 memcmp(cmpbuf
, t
->data
, cmplen
) == 0) {
2809 if (DANETLS_USAGE_BIT(usage
) & DANETLS_DANE_MASK
)
2811 if (matched
|| dane
->mdpth
< 0) {
2812 dane
->mdpth
= depth
;
2814 OPENSSL_free(dane
->mcert
);
2822 /* Clear the one-element DER cache */
2823 OPENSSL_free(i2dbuf
);
2827 static int check_dane_issuer(X509_STORE_CTX
*ctx
, int depth
)
2829 SSL_DANE
*dane
= ctx
->dane
;
2833 if (!DANETLS_HAS_TA(dane
) || depth
== 0)
2834 return X509_TRUST_UNTRUSTED
;
2837 * Record any DANE trust anchor matches, for the first depth to test, if
2838 * there's one at that depth. (This'll be false for length 1 chains looking
2839 * for an exact match for the leaf certificate).
2841 cert
= sk_X509_value(ctx
->chain
, depth
);
2842 if (cert
!= NULL
&& (matched
= dane_match(ctx
, cert
, depth
)) < 0)
2843 return X509_TRUST_REJECTED
;
2845 ctx
->num_untrusted
= depth
- 1;
2846 return X509_TRUST_TRUSTED
;
2849 return X509_TRUST_UNTRUSTED
;
2852 static int check_dane_pkeys(X509_STORE_CTX
*ctx
)
2854 SSL_DANE
*dane
= ctx
->dane
;
2856 int num
= ctx
->num_untrusted
;
2857 X509
*cert
= sk_X509_value(ctx
->chain
, num
- 1);
2858 int recnum
= sk_danetls_record_num(dane
->trecs
);
2861 for (i
= 0; i
< recnum
; ++i
) {
2862 t
= sk_danetls_record_value(dane
->trecs
, i
);
2863 if (t
->usage
!= DANETLS_USAGE_DANE_TA
||
2864 t
->selector
!= DANETLS_SELECTOR_SPKI
||
2865 t
->mtype
!= DANETLS_MATCHING_FULL
||
2866 X509_verify(cert
, t
->spki
) <= 0)
2869 /* Clear any PKIX-?? matches that failed to extend to a full chain */
2870 X509_free(dane
->mcert
);
2873 /* Record match via a bare TA public key */
2874 ctx
->bare_ta_signed
= 1;
2875 dane
->mdpth
= num
- 1;
2878 /* Prune any excess chain certificates */
2879 num
= sk_X509_num(ctx
->chain
);
2880 for (; num
> ctx
->num_untrusted
; --num
)
2881 X509_free(sk_X509_pop(ctx
->chain
));
2883 return X509_TRUST_TRUSTED
;
2886 return X509_TRUST_UNTRUSTED
;
2889 static void dane_reset(SSL_DANE
*dane
)
2892 * Reset state to verify another chain, or clear after failure.
2894 X509_free(dane
->mcert
);
2901 static int check_leaf_suiteb(X509_STORE_CTX
*ctx
, X509
*cert
)
2903 int err
= X509_chain_check_suiteb(NULL
, cert
, NULL
, ctx
->param
->flags
);
2905 CHECK_CB(err
!= X509_V_OK
, ctx
, cert
, 0, err
);
2909 static int dane_verify(X509_STORE_CTX
*ctx
)
2911 X509
*cert
= ctx
->cert
;
2912 SSL_DANE
*dane
= ctx
->dane
;
2919 * When testing the leaf certificate, if we match a DANE-EE(3) record,
2920 * dane_match() returns 1 and we're done. If however we match a PKIX-EE(1)
2921 * record, the match depth and matching TLSA record are recorded, but the
2922 * return value is 0, because we still need to find a PKIX trust anchor.
2923 * Therefore, when DANE authentication is enabled (required), we're done
2925 * + matched < 0, internal error.
2926 * + matched == 1, we matched a DANE-EE(3) record
2927 * + matched == 0, mdepth < 0 (no PKIX-EE match) and there are no
2928 * DANE-TA(2) or PKIX-TA(0) to test.
2930 matched
= dane_match(ctx
, ctx
->cert
, 0);
2931 done
= matched
!= 0 || (!DANETLS_HAS_TA(dane
) && dane
->mdpth
< 0);
2934 X509_get_pubkey_parameters(NULL
, ctx
->chain
);
2937 /* Callback invoked as needed */
2938 if (!check_leaf_suiteb(ctx
, cert
))
2940 /* Callback invoked as needed */
2941 if ((dane
->flags
& DANE_FLAG_NO_DANE_EE_NAMECHECKS
) == 0 &&
2944 /* Bypass internal_verify(), issue depth 0 success callback */
2945 ctx
->error_depth
= 0;
2946 ctx
->current_cert
= cert
;
2947 return ctx
->verify_cb(1, ctx
);
2951 ctx
->error_depth
= 0;
2952 ctx
->current_cert
= cert
;
2953 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
2958 /* Fail early, TA-based success is not possible */
2959 if (!check_leaf_suiteb(ctx
, cert
))
2961 return verify_cb_cert(ctx
, cert
, 0, X509_V_ERR_DANE_NO_MATCH
);
2965 * Chain verification for usages 0/1/2. TLSA record matching of depth > 0
2966 * certificates happens in-line with building the rest of the chain.
2968 return verify_chain(ctx
);
2971 /* Get issuer, without duplicate suppression */
2972 static int get_issuer(X509
**issuer
, X509_STORE_CTX
*ctx
, X509
*cert
)
2974 STACK_OF(X509
) *saved_chain
= ctx
->chain
;
2978 ok
= ctx
->get_issuer(issuer
, ctx
, cert
);
2979 ctx
->chain
= saved_chain
;
2984 static int build_chain(X509_STORE_CTX
*ctx
)
2986 SSL_DANE
*dane
= ctx
->dane
;
2987 int num
= sk_X509_num(ctx
->chain
);
2988 X509
*cert
= sk_X509_value(ctx
->chain
, num
- 1);
2990 STACK_OF(X509
) *sktmp
= NULL
;
2991 unsigned int search
;
2992 int may_trusted
= 0;
2993 int may_alternate
= 0;
2994 int trust
= X509_TRUST_UNTRUSTED
;
2995 int alt_untrusted
= 0;
3000 /* Our chain starts with a single untrusted element. */
3001 if (!ossl_assert(num
== 1 && ctx
->num_untrusted
== num
)) {
3002 ERR_raise(ERR_LIB_X509
, ERR_R_INTERNAL_ERROR
);
3003 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3007 self_signed
= X509_self_signed(cert
, 0);
3008 if (self_signed
< 0) {
3009 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3013 #define S_DOUNTRUSTED (1 << 0) /* Search untrusted chain */
3014 #define S_DOTRUSTED (1 << 1) /* Search trusted store */
3015 #define S_DOALTERNATE (1 << 2) /* Retry with pruned alternate chain */
3017 * Set up search policy, untrusted if possible, trusted-first if enabled.
3018 * If we're doing DANE and not doing PKIX-TA/PKIX-EE, we never look in the
3019 * trust_store, otherwise we might look there first. If not trusted-first,
3020 * and alternate chains are not disabled, try building an alternate chain
3021 * if no luck with untrusted first.
3023 search
= (ctx
->untrusted
!= NULL
) ? S_DOUNTRUSTED
: 0;
3024 if (DANETLS_HAS_PKIX(dane
) || !DANETLS_HAS_DANE(dane
)) {
3025 if (search
== 0 || ctx
->param
->flags
& X509_V_FLAG_TRUSTED_FIRST
)
3026 search
|= S_DOTRUSTED
;
3027 else if (!(ctx
->param
->flags
& X509_V_FLAG_NO_ALT_CHAINS
))
3033 * Shallow-copy the stack of untrusted certificates (with TLS, this is
3034 * typically the content of the peer's certificate message) so can make
3035 * multiple passes over it, while free to remove elements as we go.
3037 if (ctx
->untrusted
&& (sktmp
= sk_X509_dup(ctx
->untrusted
)) == NULL
) {
3038 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
3039 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
3044 * If we got any "DANE-TA(2) Cert(0) Full(0)" trust anchors from DNS, add
3045 * them to our working copy of the untrusted certificate stack. Since the
3046 * caller of X509_STORE_CTX_init() may have provided only a leaf cert with
3047 * no corresponding stack of untrusted certificates, we may need to create
3048 * an empty stack first. [ At present only the ssl library provides DANE
3049 * support, and ssl_verify_cert_chain() always provides a non-null stack
3050 * containing at least the leaf certificate, but we must be prepared for
3053 if (DANETLS_ENABLED(dane
) && dane
->certs
!= NULL
) {
3054 if (sktmp
== NULL
&& (sktmp
= sk_X509_new_null()) == NULL
) {
3055 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
3056 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
3059 if (!X509_add_certs(sktmp
, dane
->certs
, X509_ADD_FLAG_DEFAULT
)) {
3060 sk_X509_free(sktmp
);
3061 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
3067 * Still absurdly large, but arithmetically safe, a lower hard upper bound
3068 * might be reasonable.
3070 if (ctx
->param
->depth
> INT_MAX
/2)
3071 ctx
->param
->depth
= INT_MAX
/2;
3074 * Try to extend the chain until we reach an ultimately trusted issuer.
3075 * Build chains up to one longer the limit, later fail if we hit the limit,
3076 * with an X509_V_ERR_CERT_CHAIN_TOO_LONG error code.
3078 depth
= ctx
->param
->depth
+ 1;
3080 while (search
!= 0) {
3085 * Look in the trust store if enabled for first lookup, or we've run
3086 * out of untrusted issuers and search here is not disabled. When we
3087 * reach the depth limit, we stop extending the chain, if by that point
3088 * we've not found a trust anchor, any trusted chain would be too long.
3090 * The error reported to the application verify callback is at the
3091 * maximal valid depth with the current certificate equal to the last
3092 * not ultimately-trusted issuer. For example, with verify_depth = 0,
3093 * the callback will report errors at depth=1 when the immediate issuer
3094 * of the leaf certificate is not a trust anchor. No attempt will be
3095 * made to locate an issuer for that certificate, since such a chain
3096 * would be a-priori too long.
3098 if ((search
& S_DOTRUSTED
) != 0) {
3099 i
= num
= sk_X509_num(ctx
->chain
);
3100 if ((search
& S_DOALTERNATE
) != 0) {
3102 * As high up the chain as we can, look for an alternative
3103 * trusted issuer of an untrusted certificate that currently
3104 * has an untrusted issuer. We use the alt_untrusted variable
3105 * to track how far up the chain we find the first match. It
3106 * is only if and when we find a match, that we prune the chain
3107 * and reset ctx->num_untrusted to the reduced count of
3108 * untrusted certificates. While we're searching for such a
3109 * match (which may never be found), it is neither safe nor
3110 * wise to preemptively modify either the chain or
3111 * ctx->num_untrusted.
3113 * Note, like ctx->num_untrusted, alt_untrusted is a count of
3114 * untrusted certificates, not a "depth".
3118 x
= sk_X509_value(ctx
->chain
, i
-1);
3120 ok
= (depth
< num
) ? 0 : get_issuer(&xtmp
, ctx
, x
);
3123 trust
= X509_TRUST_REJECTED
;
3124 ctx
->error
= X509_V_ERR_STORE_LOOKUP
;
3131 * Alternative trusted issuer for a mid-chain untrusted cert?
3132 * Pop the untrusted cert's successors and retry. We might now
3133 * be able to complete a valid chain via the trust store. Note
3134 * that despite the current trust store match we might still
3135 * fail complete the chain to a suitable trust anchor, in which
3136 * case we may prune some more untrusted certificates and try
3137 * again. Thus the S_DOALTERNATE bit may yet be turned on
3138 * again with an even shorter untrusted chain!
3140 * If in the process we threw away our matching PKIX-TA trust
3141 * anchor, reset DANE trust. We might find a suitable trusted
3142 * certificate among the ones from the trust store.
3144 if ((search
& S_DOALTERNATE
) != 0) {
3145 if (!ossl_assert(num
> i
&& i
> 0 && !self_signed
)) {
3146 ERR_raise(ERR_LIB_X509
, ERR_R_INTERNAL_ERROR
);
3148 trust
= X509_TRUST_REJECTED
;
3149 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3153 search
&= ~S_DOALTERNATE
;
3154 for (; num
> i
; --num
)
3155 X509_free(sk_X509_pop(ctx
->chain
));
3156 ctx
->num_untrusted
= num
;
3158 if (DANETLS_ENABLED(dane
) &&
3159 dane
->mdpth
>= ctx
->num_untrusted
) {
3161 X509_free(dane
->mcert
);
3164 if (DANETLS_ENABLED(dane
) &&
3165 dane
->pdpth
>= ctx
->num_untrusted
)
3170 * Self-signed untrusted certificates get replaced by their
3171 * trusted matching issuer. Otherwise, grow the chain.
3174 if (!sk_X509_push(ctx
->chain
, x
= xtmp
)) {
3176 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
3177 trust
= X509_TRUST_REJECTED
;
3178 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
3182 self_signed
= X509_self_signed(x
, 0);
3183 if (self_signed
< 0) {
3184 sk_X509_free(sktmp
);
3185 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3188 } else if (num
== ctx
->num_untrusted
) {
3190 * We have a self-signed certificate that has the same
3191 * subject name (and perhaps keyid and/or serial number) as
3192 * a trust anchor. We must have an exact match to avoid
3193 * possible impersonation via key substitution etc.
3195 if (X509_cmp(x
, xtmp
) != 0) {
3196 /* Self-signed untrusted mimic. */
3201 ctx
->num_untrusted
= --num
;
3202 (void) sk_X509_set(ctx
->chain
, num
, x
= xtmp
);
3207 * We've added a new trusted certificate to the chain, recheck
3208 * trust. If not done, and not self-signed look deeper.
3209 * Whether or not we're doing "trusted first", we no longer
3210 * look for untrusted certificates from the peer's chain.
3212 * At this point ctx->num_trusted and num must reflect the
3213 * correct number of untrusted certificates, since the DANE
3214 * logic in check_trust() depends on distinguishing CAs from
3215 * "the wire" from CAs from the trust store. In particular, the
3216 * certificate at depth "num" should be the new trusted
3217 * certificate with ctx->num_untrusted <= num.
3220 if (!ossl_assert(ctx
->num_untrusted
<= num
)) {
3221 ERR_raise(ERR_LIB_X509
, ERR_R_INTERNAL_ERROR
);
3222 trust
= X509_TRUST_REJECTED
;
3223 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3227 search
&= ~S_DOUNTRUSTED
;
3228 switch (trust
= check_trust(ctx
, num
)) {
3229 case X509_TRUST_TRUSTED
:
3230 case X509_TRUST_REJECTED
:
3240 * No dispositive decision, and either self-signed or no match, if
3241 * we were doing untrusted-first, and alt-chains are not disabled,
3242 * do that, by repeatedly losing one untrusted element at a time,
3243 * and trying to extend the shorted chain.
3245 if ((search
& S_DOUNTRUSTED
) == 0) {
3246 /* Continue search for a trusted issuer of a shorter chain? */
3247 if ((search
& S_DOALTERNATE
) != 0 && --alt_untrusted
> 0)
3249 /* Still no luck and no fallbacks left? */
3250 if (!may_alternate
|| (search
& S_DOALTERNATE
) != 0 ||
3251 ctx
->num_untrusted
< 2)
3253 /* Search for a trusted issuer of a shorter chain */
3254 search
|= S_DOALTERNATE
;
3255 alt_untrusted
= ctx
->num_untrusted
- 1;
3261 * Extend chain with peer-provided certificates
3263 if ((search
& S_DOUNTRUSTED
) != 0) {
3264 num
= sk_X509_num(ctx
->chain
);
3265 if (!ossl_assert(num
== ctx
->num_untrusted
)) {
3266 ERR_raise(ERR_LIB_X509
, ERR_R_INTERNAL_ERROR
);
3267 trust
= X509_TRUST_REJECTED
;
3268 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3272 x
= sk_X509_value(ctx
->chain
, num
-1);
3275 * Once we run out of untrusted issuers, we stop looking for more
3276 * and start looking only in the trust store if enabled.
3278 xtmp
= (self_signed
|| depth
< num
) ? NULL
3279 : find_issuer(ctx
, sktmp
, x
);
3281 search
&= ~S_DOUNTRUSTED
;
3283 search
|= S_DOTRUSTED
;
3287 /* Drop this issuer from future consideration */
3288 (void) sk_X509_delete_ptr(sktmp
, xtmp
);
3290 if (!X509_up_ref(xtmp
)) {
3291 ERR_raise(ERR_LIB_X509
, ERR_R_INTERNAL_ERROR
);
3292 trust
= X509_TRUST_REJECTED
;
3293 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3298 if (!sk_X509_push(ctx
->chain
, xtmp
)) {
3300 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
3301 trust
= X509_TRUST_REJECTED
;
3302 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
3308 ++ctx
->num_untrusted
;
3309 self_signed
= X509_self_signed(xtmp
, 0);
3310 if (self_signed
< 0) {
3311 sk_X509_free(sktmp
);
3312 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
3317 * Check for DANE-TA trust of the topmost untrusted certificate.
3319 switch (trust
= check_dane_issuer(ctx
, ctx
->num_untrusted
- 1)) {
3320 case X509_TRUST_TRUSTED
:
3321 case X509_TRUST_REJECTED
:
3327 sk_X509_free(sktmp
);
3330 * Last chance to make a trusted chain, either bare DANE-TA public-key
3331 * signers, or else direct leaf PKIX trust.
3333 num
= sk_X509_num(ctx
->chain
);
3335 if (trust
== X509_TRUST_UNTRUSTED
&& DANETLS_HAS_DANE_TA(dane
))
3336 trust
= check_dane_pkeys(ctx
);
3337 if (trust
== X509_TRUST_UNTRUSTED
&& num
== ctx
->num_untrusted
)
3338 trust
= check_trust(ctx
, num
);
3342 case X509_TRUST_TRUSTED
:
3344 case X509_TRUST_REJECTED
:
3345 /* Callback already issued */
3347 case X509_TRUST_UNTRUSTED
:
3349 num
= sk_X509_num(ctx
->chain
);
3350 CHECK_CB(num
> depth
, ctx
, NULL
, num
-1, X509_V_ERR_CERT_CHAIN_TOO_LONG
);
3351 CHECK_CB(DANETLS_ENABLED(dane
)
3352 && (!DANETLS_HAS_PKIX(dane
) || dane
->pdpth
>= 0),
3353 ctx
, NULL
, num
-1, X509_V_ERR_DANE_NO_MATCH
);
3355 return verify_cb_cert(ctx
, NULL
, num
-1,
3356 sk_X509_num(ctx
->chain
) == 1
3357 ? X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
3358 : X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
);
3359 return verify_cb_cert(ctx
, NULL
, num
-1,
3360 ctx
->num_untrusted
< num
3361 ? X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
3362 : X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
);
3366 static const int minbits_table
[] = { 80, 112, 128, 192, 256 };
3367 static const int NUM_AUTH_LEVELS
= OSSL_NELEM(minbits_table
);
3370 * Check whether the public key of ``cert`` meets the security level of
3373 * Returns 1 on success, 0 otherwise.
3375 static int check_key_level(X509_STORE_CTX
*ctx
, X509
*cert
)
3377 EVP_PKEY
*pkey
= X509_get0_pubkey(cert
);
3378 int level
= ctx
->param
->auth_level
;
3381 * At security level zero, return without checking for a supported public
3382 * key type. Some engines support key types not understood outside the
3383 * engine, and we only need to understand the key when enforcing a security
3389 /* Unsupported or malformed keys are not secure */
3393 if (level
> NUM_AUTH_LEVELS
)
3394 level
= NUM_AUTH_LEVELS
;
3396 return EVP_PKEY_security_bits(pkey
) >= minbits_table
[level
- 1];
3400 * Check whether the public key of ``cert`` does not use explicit params
3401 * for an elliptic curve.
3403 * Returns 1 on success, 0 if check fails, -1 for other errors.
3405 static int check_curve(X509
*cert
)
3407 #ifndef OPENSSL_NO_EC
3408 EVP_PKEY
*pkey
= X509_get0_pubkey(cert
);
3410 /* Unsupported or malformed key */
3414 if (EVP_PKEY_id(pkey
) == EVP_PKEY_EC
) {
3417 ret
= EC_KEY_decoded_from_explicit_params(EVP_PKEY_get0_EC_KEY(pkey
));
3418 return ret
< 0 ? ret
: !ret
;
3426 * Check whether the signature digest algorithm of ``cert`` meets the security
3427 * level of ``ctx``. Should not be checked for trust anchors (whether
3428 * self-signed or otherwise).
3430 * Returns 1 on success, 0 otherwise.
3432 static int check_sig_level(X509_STORE_CTX
*ctx
, X509
*cert
)
3435 int level
= ctx
->param
->auth_level
;
3439 if (level
> NUM_AUTH_LEVELS
)
3440 level
= NUM_AUTH_LEVELS
;
3442 if (!X509_get_signature_info(cert
, NULL
, NULL
, &secbits
, NULL
))
3445 return secbits
>= minbits_table
[level
- 1];