]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x509_vfy.c
Make the OSSL_CMP manual conform with man-pages(7)
[thirdparty/openssl.git] / crypto / x509 / x509_vfy.c
CommitLineData
b1322259 1/*
7e06a675 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3e4b43b9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <time.h>
12#include <errno.h>
d9b8b89b 13#include <limits.h>
d02b48c6 14
25f2138b 15#include "crypto/ctype.h"
b39fc560 16#include "internal/cryptlib.h"
17f389bb 17#include <openssl/crypto.h>
ec577822
BM
18#include <openssl/buffer.h>
19#include <openssl/evp.h>
20#include <openssl/asn1.h>
21#include <openssl/x509.h>
11262391 22#include <openssl/x509v3.h>
ec577822 23#include <openssl/objects.h>
176db6dc 24#include "internal/dane.h"
25f2138b 25#include "crypto/x509.h"
706457b7 26#include "x509_local.h"
d02b48c6 27
d43c4497
DSH
28/* CRL score values */
29
30/* No unhandled critical extensions */
31
0f113f3e 32#define CRL_SCORE_NOCRITICAL 0x100
d43c4497
DSH
33
34/* certificate is within CRL scope */
35
0f113f3e 36#define CRL_SCORE_SCOPE 0x080
d43c4497
DSH
37
38/* CRL times valid */
39
0f113f3e 40#define CRL_SCORE_TIME 0x040
d43c4497
DSH
41
42/* Issuer name matches certificate */
43
0f113f3e 44#define CRL_SCORE_ISSUER_NAME 0x020
d43c4497
DSH
45
46/* If this score or above CRL is probably valid */
47
48#define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
49
50/* CRL issuer is certificate issuer */
51
0f113f3e 52#define CRL_SCORE_ISSUER_CERT 0x018
d43c4497
DSH
53
54/* CRL issuer is on certificate path */
55
0f113f3e 56#define CRL_SCORE_SAME_PATH 0x008
d43c4497
DSH
57
58/* CRL issuer matches CRL AKID */
59
0f113f3e 60#define CRL_SCORE_AKID 0x004
d43c4497
DSH
61
62/* Have a delta CRL with valid times */
63
0f113f3e 64#define CRL_SCORE_TIME_DELTA 0x002
d43c4497 65
d9b8b89b
VD
66static int build_chain(X509_STORE_CTX *ctx);
67static int verify_chain(X509_STORE_CTX *ctx);
170b7358 68static int dane_verify(X509_STORE_CTX *ctx);
0f113f3e 69static int null_callback(int ok, X509_STORE_CTX *e);
2f043896
DSH
70static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
71static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
67ecd65c 72static int check_chain(X509_STORE_CTX *ctx);
e9746e03 73static int check_name_constraints(X509_STORE_CTX *ctx);
3bf15e29 74static int check_id(X509_STORE_CTX *ctx);
d9b8b89b 75static int check_trust(X509_STORE_CTX *ctx, int num_untrusted);
b545dc67
DSH
76static int check_revocation(X509_STORE_CTX *ctx);
77static int check_cert(X509_STORE_CTX *ctx);
5d7c222d 78static int check_policy(X509_STORE_CTX *ctx);
c9a81b30 79static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
170b7358 80static int check_dane_issuer(X509_STORE_CTX *ctx, int depth);
fbb82a60
VD
81static int check_key_level(X509_STORE_CTX *ctx, X509 *cert);
82static int check_sig_level(X509_STORE_CTX *ctx, X509 *cert);
cccf532f 83static int check_curve(X509 *cert);
4b96839f
DSH
84
85static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
0f113f3e 86 unsigned int *preasons, X509_CRL *crl, X509 *x);
d43c4497 87static int get_crl_delta(X509_STORE_CTX *ctx,
0f113f3e
MC
88 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
89static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
90 int *pcrl_score, X509_CRL *base,
91 STACK_OF(X509_CRL) *crls);
92static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
93 int *pcrl_score);
4b96839f 94static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
0f113f3e 95 unsigned int *preasons);
9d84d4ed
DSH
96static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
97static int check_crl_chain(X509_STORE_CTX *ctx,
0f113f3e
MC
98 STACK_OF(X509) *cert_path,
99 STACK_OF(X509) *crl_path);
4b96839f 100
d02b48c6 101static int internal_verify(X509_STORE_CTX *ctx);
d02b48c6 102
6b691a5c 103static int null_callback(int ok, X509_STORE_CTX *e)
0f113f3e
MC
104{
105 return ok;
106}
d02b48c6 107
0d8dbb52 108/*-
ade08735 109 * Return 1 if given cert is considered self-signed, 0 if not, or -1 on error.
0d8dbb52
DDO
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.
ade08735 114 */
6725682d 115int X509_self_signed(X509 *cert, int verify_signature)
0f113f3e 116{
0d8dbb52 117 EVP_PKEY *pkey;
0c56a648 118
0d8dbb52 119 if ((pkey = X509_get0_pubkey(cert)) == NULL) { /* handles cert == NULL */
9311d0c4 120 ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
0d8dbb52
DDO
121 return -1;
122 }
6725682d 123 if (!x509v3_cache_extensions(cert))
0d8dbb52
DDO
124 return -1;
125 if ((cert->ex_flags & EXFLAG_SS) == 0)
0f113f3e 126 return 0;
0d8dbb52
DDO
127 if (!verify_signature)
128 return 1;
6725682d 129 return X509_verify(cert, pkey);
0d8dbb52 130}
2dabd822 131
0d8dbb52 132/* Given a certificate try and find an exact match in the store */
2dabd822 133static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
0f113f3e
MC
134{
135 STACK_OF(X509) *certs;
136 X509 *xtmp = NULL;
137 int i;
138 /* Lookup all certs with matching subject name */
bf973d06 139 ERR_set_mark();
0f113f3e 140 certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
bf973d06 141 ERR_pop_to_mark();
0f113f3e
MC
142 if (certs == NULL)
143 return NULL;
144 /* Look for exact match */
145 for (i = 0; i < sk_X509_num(certs); i++) {
146 xtmp = sk_X509_value(certs, i);
147 if (!X509_cmp(xtmp, x))
148 break;
e9e7b5df 149 xtmp = NULL;
0f113f3e 150 }
e9e7b5df 151 if (xtmp != NULL && !X509_up_ref(xtmp))
0f113f3e
MC
152 xtmp = NULL;
153 sk_X509_pop_free(certs, X509_free);
154 return xtmp;
155}
2dabd822 156
70dd3c65
VD
157/*-
158 * Inform the verify callback of an error.
e99505b4
DDO
159 * If 'x' is not NULL it is the error cert, otherwise use the chain cert at
160 * 'depth'
161 * If 'err' is not X509_V_OK, that's the error value, otherwise leave
70dd3c65
VD
162 * unchanged (presumably set by the caller).
163 *
164 * Returns 0 to abort verification with an error, non-zero to continue.
165 */
166static int verify_cb_cert(X509_STORE_CTX *ctx, X509 *x, int depth, int err)
167{
168 ctx->error_depth = depth;
169 ctx->current_cert = (x != NULL) ? x : sk_X509_value(ctx->chain, depth);
170 if (err != X509_V_OK)
171 ctx->error = err;
172 return ctx->verify_cb(0, ctx);
173}
174
6e5e118c
DO
175#define CHECK_CB(cond, ctx, cert, depth, err) \
176 if ((cond) && verify_cb_cert(ctx, cert, depth, err) == 0) \
177 return 0
178
70dd3c65
VD
179/*-
180 * Inform the verify callback of an error, CRL-specific variant. Here, the
181 * error depth and certificate are already set, we just specify the error
182 * number.
183 *
184 * Returns 0 to abort verification with an error, non-zero to continue.
185 */
186static int verify_cb_crl(X509_STORE_CTX *ctx, int err)
187{
188 ctx->error = err;
189 return ctx->verify_cb(0, ctx);
190}
191
fbb82a60
VD
192static int check_auth_level(X509_STORE_CTX *ctx)
193{
194 int i;
195 int num = sk_X509_num(ctx->chain);
196
197 if (ctx->param->auth_level <= 0)
198 return 1;
199
200 for (i = 0; i < num; ++i) {
201 X509 *cert = sk_X509_value(ctx->chain, i);
202
203 /*
204 * We've already checked the security of the leaf key, so here we only
205 * check the security of issuer keys.
206 */
6e5e118c
DO
207 CHECK_CB(i > 0 && !check_key_level(ctx, cert),
208 ctx, cert, i, X509_V_ERR_CA_KEY_TOO_SMALL);
fbb82a60
VD
209 /*
210 * We also check the signature algorithm security of all certificates
211 * except those of the trust anchor at index num-1.
212 */
6e5e118c
DO
213 CHECK_CB(i < num - 1 && !check_sig_level(ctx, cert),
214 ctx, cert, i, X509_V_ERR_CA_MD_TOO_WEAK);
fbb82a60
VD
215 }
216 return 1;
217}
218
d9b8b89b
VD
219static int verify_chain(X509_STORE_CTX *ctx)
220{
d9b8b89b
VD
221 int err;
222 int ok;
223
224 /*
225 * Before either returning with an error, or continuing with CRL checks,
226 * instantiate chain public key parameters.
227 */
228 if ((ok = build_chain(ctx)) == 0 ||
67ecd65c 229 (ok = check_chain(ctx)) == 0 ||
fbb82a60 230 (ok = check_auth_level(ctx)) == 0 ||
d9b8b89b
VD
231 (ok = check_id(ctx)) == 0 || 1)
232 X509_get_pubkey_parameters(NULL, ctx->chain);
233 if (ok == 0 || (ok = ctx->check_revocation(ctx)) == 0)
234 return ok;
235
236 err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
237 ctx->param->flags);
6e5e118c 238 CHECK_CB(err != X509_V_OK, ctx, NULL, ctx->error_depth, err);
d9b8b89b
VD
239
240 /* Verify chain signatures and expiration times */
241 ok = (ctx->verify != NULL) ? ctx->verify(ctx) : internal_verify(ctx);
242 if (!ok)
243 return ok;
244
8545051c
DB
245 if ((ok = check_name_constraints(ctx)) == 0)
246 return ok;
247
d9b8b89b
VD
248#ifndef OPENSSL_NO_RFC3779
249 /* RFC 3779 path validation, now that CRL check has been done */
9021a5df 250 if ((ok = X509v3_asid_validate_path(ctx)) == 0)
d9b8b89b 251 return ok;
9021a5df 252 if ((ok = X509v3_addr_validate_path(ctx)) == 0)
d9b8b89b
VD
253 return ok;
254#endif
255
256 /* If we get this far evaluate policies */
257 if (ctx->param->flags & X509_V_FLAG_POLICY_CHECK)
258 ok = ctx->check_policy(ctx);
259 return ok;
260}
261
6b691a5c 262int X509_verify_cert(X509_STORE_CTX *ctx)
0f113f3e 263{
b9aec69a 264 SSL_DANE *dane = ctx->dane;
f3e235ed 265 int ret;
d9b8b89b 266
0f113f3e 267 if (ctx->cert == NULL) {
9311d0c4 268 ERR_raise(ERR_LIB_X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
f3e235ed 269 ctx->error = X509_V_ERR_INVALID_CALL;
0f113f3e
MC
270 return -1;
271 }
d9b8b89b 272
aae41f8c
MC
273 if (ctx->chain != NULL) {
274 /*
275 * This X509_STORE_CTX has already been used to verify a cert. We
276 * cannot do another one.
277 */
9311d0c4 278 ERR_raise(ERR_LIB_X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
f3e235ed 279 ctx->error = X509_V_ERR_INVALID_CALL;
aae41f8c
MC
280 return -1;
281 }
0f113f3e 282
eeccc237 283 if (!X509_add_cert_new(&ctx->chain, ctx->cert, X509_ADD_FLAG_UP_REF)) {
f3e235ed 284 ctx->error = X509_V_ERR_OUT_OF_MEM;
d9b8b89b 285 return -1;
0f113f3e 286 }
d9b8b89b 287 ctx->num_untrusted = 1;
5d7c222d 288
fbb82a60 289 /* If the peer's public key is too weak, we can stop early. */
6e5e118c
DO
290 CHECK_CB(!check_key_level(ctx, ctx->cert),
291 ctx, ctx->cert, 0, X509_V_ERR_EE_KEY_TOO_SMALL);
fbb82a60 292
f3e235ed
VD
293 if (DANETLS_ENABLED(dane))
294 ret = dane_verify(ctx);
295 else
296 ret = verify_chain(ctx);
297
170b7358 298 /*
f3e235ed
VD
299 * Safety-net. If we are returning an error, we must also set ctx->error,
300 * so that the chain is not considered verified should the error be ignored
301 * (e.g. TLS with SSL_VERIFY_NONE).
170b7358 302 */
f3e235ed
VD
303 if (ret <= 0 && ctx->error == X509_V_OK)
304 ctx->error = X509_V_ERR_UNSPECIFIED;
305 return ret;
0f113f3e
MC
306}
307
3bed88a3
DDO
308static int sk_X509_contains(STACK_OF(X509) *sk, X509 *cert)
309{
310 int i, n = sk_X509_num(sk);
311
312 for (i = 0; i < n; i++)
313 if (X509_cmp(sk_X509_value(sk, i), cert) == 0)
314 return 1;
315 return 0;
316}
317
0f113f3e 318/*
4dd00918
DDO
319 * Find in given STACK_OF(X509) sk an issuer cert of given cert x.
320 * The issuer must not yet be in ctx->chain, where the exceptional case
321 * that x is self-issued and ctx->chain has just one element is allowed.
322 * Prefer the first one that is not expired, else take the last expired one.
2f043896 323 */
2f043896
DSH
324static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
325{
0f113f3e 326 int i;
c53f7355 327 X509 *issuer, *rv = NULL;
fbb82a60 328
0f113f3e 329 for (i = 0; i < sk_X509_num(sk); i++) {
c53f7355 330 issuer = sk_X509_value(sk, i);
4dd00918 331 if (ctx->check_issued(ctx, x, issuer)
3bed88a3
DDO
332 && (((x->ex_flags & EXFLAG_SI) != 0 && sk_X509_num(ctx->chain) == 1)
333 || !sk_X509_contains(ctx->chain, issuer))) {
4dd00918
DDO
334 if (x509_check_cert_time(ctx, issuer, -1))
335 return issuer;
c53f7355 336 rv = issuer;
c53f7355 337 }
0f113f3e 338 }
c53f7355 339 return rv;
2f043896
DSH
340}
341
3bed88a3
DDO
342/* Check that the given certificate 'x' is issued by the certificate 'issuer' */
343static int check_issued(ossl_unused X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
2f043896 344{
3bed88a3 345 return x509_likely_issued(issuer, x) == X509_V_OK;
2f043896
DSH
346}
347
348/* Alternative lookup method: look from a STACK stored in other_ctx */
2f043896
DSH
349static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
350{
0f113f3e 351 *issuer = find_issuer(ctx, ctx->other_ctx, x);
e9e7b5df
BE
352
353 if (*issuer == NULL || !X509_up_ref(*issuer))
354 goto err;
355
356 return 1;
357
358 err:
359 *issuer = NULL;
360 return 0;
2f043896 361}
2f043896 362
8cc86b81
DDO
363static STACK_OF(X509) *lookup_certs_sk(X509_STORE_CTX *ctx,
364 const X509_NAME *nm)
c864e761
DSH
365{
366 STACK_OF(X509) *sk = NULL;
367 X509 *x;
368 int i;
7fcdbd83 369
c864e761
DSH
370 for (i = 0; i < sk_X509_num(ctx->other_ctx); i++) {
371 x = sk_X509_value(ctx->other_ctx, i);
372 if (X509_NAME_cmp(nm, X509_get_subject_name(x)) == 0) {
eeccc237 373 if (!X509_add_cert_new(&sk, x, X509_ADD_FLAG_UP_REF)) {
bbc3c22c 374 sk_X509_pop_free(sk, X509_free);
7fcdbd83 375 ctx->error = X509_V_ERR_OUT_OF_MEM;
c864e761
DSH
376 return NULL;
377 }
c864e761
DSH
378 }
379 }
380 return sk;
381}
382
0daccd4d
VD
383/*
384 * Check EE or CA certificate purpose. For trusted certificates explicit local
385 * auxiliary trust can be used to override EKU-restrictions.
386 */
387static int check_purpose(X509_STORE_CTX *ctx, X509 *x, int purpose, int depth,
388 int must_be_ca)
389{
0daccd4d
VD
390 int tr_ok = X509_TRUST_UNTRUSTED;
391
392 /*
393 * For trusted certificates we want to see whether any auxiliary trust
33cc5dde 394 * settings trump the purpose constraints.
0daccd4d
VD
395 *
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!
399 *
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.
405 *
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
33cc5dde 408 * also set.
0daccd4d
VD
409 */
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);
412
33cc5dde
VD
413 switch (tr_ok) {
414 case X509_TRUST_TRUSTED:
0daccd4d 415 return 1;
33cc5dde
VD
416 case X509_TRUST_REJECTED:
417 break;
418 default:
419 switch (X509_check_purpose(x, purpose, must_be_ca > 0)) {
420 case 1:
421 return 1;
422 case 0:
423 break;
424 default:
425 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) == 0)
426 return 1;
427 }
428 break;
429 }
0daccd4d 430
70dd3c65 431 return verify_cb_cert(ctx, x, depth, X509_V_ERR_INVALID_PURPOSE);
0daccd4d
VD
432}
433
0f113f3e
MC
434/*
435 * Check a certificate chains extensions for consistency with the supplied
436 * purpose
11262391
DSH
437 */
438
67ecd65c 439static int check_chain(X509_STORE_CTX *ctx)
11262391 440{
0daccd4d 441 int i, must_be_ca, plen = 0;
0f113f3e 442 X509 *x;
0f113f3e
MC
443 int proxy_path_length = 0;
444 int purpose;
445 int allow_proxy_certs;
0daccd4d 446 int num = sk_X509_num(ctx->chain);
0f113f3e 447
35a1cc90
MC
448 /*-
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.
456 */
0f113f3e
MC
457 must_be_ca = -1;
458
459 /* CRL path validation */
460 if (ctx->parent) {
461 allow_proxy_certs = 0;
462 purpose = X509_PURPOSE_CRL_SIGN;
463 } else {
464 allow_proxy_certs =
465 ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
0f113f3e
MC
466 purpose = ctx->param->purpose;
467 }
468
0daccd4d 469 for (i = 0; i < num; i++) {
0f113f3e 470 int ret;
1e41dadf 471
0f113f3e 472 x = sk_X509_value(ctx->chain, i);
6e5e118c
DO
473 CHECK_CB((ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) == 0
474 && (x->ex_flags & EXFLAG_CRITICAL) != 0,
475 ctx, x, i, X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION);
476 CHECK_CB(!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY),
477 ctx, x, i, X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED);
0f113f3e
MC
478 ret = X509_check_ca(x);
479 switch (must_be_ca) {
480 case -1:
6e5e118c
DO
481 CHECK_CB((ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0
482 && ret != 1 && ret != 0,
483 ctx, x, i, X509_V_ERR_INVALID_CA);
0f113f3e
MC
484 break;
485 case 0:
5811387b 486 CHECK_CB(ret != 0, ctx, x, i, X509_V_ERR_INVALID_NON_CA);
0f113f3e
MC
487 break;
488 default:
4d9e33ac 489 /* X509_V_FLAG_X509_STRICT is implicit for intermediate CAs */
6e5e118c
DO
490 CHECK_CB(ret == 0
491 || ((i + 1 < num
492 || ctx->param->flags & X509_V_FLAG_X509_STRICT)
493 && ret != 1), ctx, x, i, X509_V_ERR_INVALID_CA);
0f113f3e
MC
494 break;
495 }
cccf532f
TM
496 if (num > 1) {
497 /* Check for presence of explicit elliptic curve parameters */
498 ret = check_curve(x);
6e5e118c
DO
499 CHECK_CB(ret < 0, ctx, x, i, X509_V_ERR_UNSPECIFIED);
500 CHECK_CB(ret == 0, ctx, x, i, X509_V_ERR_EC_KEY_EXPLICIT_PARAMS);
cccf532f 501 }
d72c8b45 502 /*
e99505b4 503 * Do the following set of checks only if strict checking is requested
d72c8b45
DDO
504 * and not for self-issued (including self-signed) EE (non-CA) certs
505 * because RFC 5280 does not apply to them according RFC 6818 section 2.
506 */
507 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0
e41a2c4c
DDO
508 && num > 1) { /*
509 * this should imply
510 * !(i == 0 && (x->ex_flags & EXFLAG_CA) == 0
511 * && (x->ex_flags & EXFLAG_SI) != 0)
512 */
1e41dadf
DDO
513 /* Check Basic Constraints according to RFC 5280 section 4.2.1.9 */
514 if (x->ex_pathlen != -1) {
3309c4b7
DO
515 CHECK_CB((x->ex_flags & EXFLAG_CA) == 0,
516 ctx, x, i, X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA);
517 CHECK_CB((x->ex_kusage & KU_KEY_CERT_SIGN) == 0, ctx, x, i,
518 X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN);
1e41dadf 519 }
3309c4b7
DO
520 CHECK_CB((x->ex_flags & EXFLAG_CA) != 0
521 && (x->ex_flags & EXFLAG_BCONS) != 0
522 && (x->ex_flags & EXFLAG_BCONS_CRITICAL) == 0,
523 ctx, x, i, X509_V_ERR_CA_BCONS_NOT_CRITICAL);
d72c8b45 524 /* Check Key Usage according to RFC 5280 section 4.2.1.3 */
bb377c8d 525 if ((x->ex_flags & EXFLAG_CA) != 0) {
3309c4b7
DO
526 CHECK_CB((x->ex_flags & EXFLAG_KUSAGE) == 0,
527 ctx, x, i, X509_V_ERR_CA_CERT_MISSING_KEY_USAGE);
bb377c8d 528 } else {
3309c4b7
DO
529 CHECK_CB((x->ex_kusage & KU_KEY_CERT_SIGN) != 0, ctx, x, i,
530 X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA);
bb377c8d 531 }
1e41dadf 532 /* Check issuer is non-empty acc. to RFC 5280 section 4.1.2.4 */
3309c4b7
DO
533 CHECK_CB(X509_NAME_entry_count(X509_get_issuer_name(x)) == 0,
534 ctx, x, i, X509_V_ERR_ISSUER_NAME_EMPTY);
1e41dadf 535 /* Check subject is non-empty acc. to RFC 5280 section 4.1.2.6 */
3309c4b7
DO
536 CHECK_CB(((x->ex_flags & EXFLAG_CA) != 0
537 || (x->ex_kusage & KU_CRL_SIGN) != 0
538 || x->altname == NULL
539 ) && X509_NAME_entry_count(X509_get_subject_name(x)) == 0,
540 ctx, x, i, X509_V_ERR_SUBJECT_NAME_EMPTY);
541 CHECK_CB(X509_NAME_entry_count(X509_get_subject_name(x)) == 0
542 && x->altname != NULL
543 && (x->ex_flags & EXFLAG_SAN_CRITICAL) == 0,
544 ctx, x, i, X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL);
1e41dadf 545 /* Check SAN is non-empty according to RFC 5280 section 4.2.1.6 */
3309c4b7
DO
546 CHECK_CB(x->altname != NULL && sk_GENERAL_NAME_num(x->altname) <= 0,
547 ctx, x, i, X509_V_ERR_EMPTY_SUBJECT_ALT_NAME);
1e41dadf
DDO
548 /* TODO add more checks on SAN entries */
549 /* Check sig alg consistency acc. to RFC 5280 section 4.1.1.2 */
3309c4b7
DO
550 CHECK_CB(X509_ALGOR_cmp(&x->sig_alg, &x->cert_info.signature) != 0,
551 ctx, x, i, X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY);
552 CHECK_CB(x->akid != NULL
553 && (x->ex_flags & EXFLAG_AKID_CRITICAL) != 0,
554 ctx, x, i, X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL);
555 CHECK_CB(x->skid != NULL
556 && (x->ex_flags & EXFLAG_SKID_CRITICAL) != 0,
557 ctx, x, i, X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL);
1e41dadf
DDO
558 if (X509_get_version(x) >= 2) { /* at least X.509v3 */
559 /* Check AKID presence acc. to RFC 5280 section 4.2.1.1 */
3309c4b7
DO
560 CHECK_CB(i + 1 < num /*
561 * this means not last cert in chain,
562 * taken as "generated by conforming CAs"
563 */
564 && (x->akid == NULL || x->akid->keyid == NULL), ctx,
565 x, i, X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER);
1e41dadf 566 /* Check SKID presence acc. to RFC 5280 section 4.2.1.2 */
3309c4b7
DO
567 CHECK_CB((x->ex_flags & EXFLAG_CA) != 0 && x->skid == NULL,
568 ctx, x, i, X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER);
82bdd641 569 } else {
3309c4b7
DO
570 CHECK_CB(sk_X509_EXTENSION_num(X509_get0_extensions(x)) > 0,
571 ctx, x, i, X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3);
1e41dadf 572 }
fa86e2ee 573 }
6e5e118c 574
70dd3c65
VD
575 /* check_purpose() makes the callback as needed */
576 if (purpose > 0 && !check_purpose(ctx, x, purpose, i, must_be_ca))
577 return 0;
e99505b4 578 /* Check path length */
6e5e118c
DO
579 CHECK_CB(i > 1 && x->ex_pathlen != -1
580 && plen > x->ex_pathlen + proxy_path_length,
581 ctx, x, i, X509_V_ERR_PATH_LENGTH_EXCEEDED);
ade08735 582 /* Increment path length if not a self-issued intermediate CA */
dc5831da 583 if (i > 0 && (x->ex_flags & EXFLAG_SI) == 0)
0f113f3e
MC
584 plen++;
585 /*
586 * If this certificate is a proxy certificate, the next certificate
587 * must be another proxy certificate or a EE certificate. If not,
588 * the next certificate must be a CA certificate.
589 */
590 if (x->ex_flags & EXFLAG_PROXY) {
ed17c7c1
RL
591 /*
592 * RFC3820, 4.1.3 (b)(1) stipulates that if pCPathLengthConstraint
593 * is less than max_path_length, the former should be copied to
594 * the latter, and 4.1.4 (a) stipulates that max_path_length
595 * should be verified to be larger than zero and decrement it.
596 *
597 * Because we're checking the certs in the reverse order, we start
598 * with verifying that proxy_path_length isn't larger than pcPLC,
599 * and copy the latter to the former if it is, and finally,
600 * increment proxy_path_length.
601 */
602 if (x->ex_pcpathlen != -1) {
6e5e118c
DO
603 CHECK_CB(proxy_path_length > x->ex_pcpathlen,
604 ctx, x, i, X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED);
ed17c7c1 605 proxy_path_length = x->ex_pcpathlen;
0f113f3e
MC
606 }
607 proxy_path_length++;
608 must_be_ca = 0;
609 } else
610 must_be_ca = 1;
611 }
0daccd4d 612 return 1;
11262391
DSH
613}
614
55a6250f
VD
615static int has_san_id(X509 *x, int gtype)
616{
617 int i;
618 int ret = 0;
619 GENERAL_NAMES *gs = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
620
621 if (gs == NULL)
622 return 0;
623
624 for (i = 0; i < sk_GENERAL_NAME_num(gs); i++) {
625 GENERAL_NAME *g = sk_GENERAL_NAME_value(gs, i);
626
627 if (g->type == gtype) {
628 ret = 1;
629 break;
630 }
631 }
632 GENERAL_NAMES_free(gs);
633 return ret;
634}
635
e9746e03 636static int check_name_constraints(X509_STORE_CTX *ctx)
0f113f3e 637{
70dd3c65
VD
638 int i;
639
0f113f3e
MC
640 /* Check name constraints for all certificates */
641 for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
70dd3c65
VD
642 X509 *x = sk_X509_value(ctx->chain, i);
643 int j;
644
ade08735 645 /* Ignore self-issued certs unless last in chain */
0f113f3e
MC
646 if (i && (x->ex_flags & EXFLAG_SI))
647 continue;
c8223538
RL
648
649 /*
650 * Proxy certificates policy has an extra constraint, where the
651 * certificate subject MUST be the issuer with a single CN entry
652 * added.
653 * (RFC 3820: 3.4, 4.1.3 (a)(4))
654 */
655 if (x->ex_flags & EXFLAG_PROXY) {
656 X509_NAME *tmpsubject = X509_get_subject_name(x);
657 X509_NAME *tmpissuer = X509_get_issuer_name(x);
658 X509_NAME_ENTRY *tmpentry = NULL;
659 int last_object_nid = 0;
660 int err = X509_V_OK;
661 int last_object_loc = X509_NAME_entry_count(tmpsubject) - 1;
662
663 /* Check that there are at least two RDNs */
664 if (last_object_loc < 1) {
665 err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;
666 goto proxy_name_done;
667 }
668
669 /*
670 * Check that there is exactly one more RDN in subject as
671 * there is in issuer.
672 */
673 if (X509_NAME_entry_count(tmpsubject)
674 != X509_NAME_entry_count(tmpissuer) + 1) {
675 err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;
676 goto proxy_name_done;
677 }
678
679 /*
680 * Check that the last subject component isn't part of a
e99505b4 681 * multi-valued RDN
c8223538
RL
682 */
683 if (X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject,
684 last_object_loc))
685 == X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject,
686 last_object_loc - 1))) {
687 err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;
688 goto proxy_name_done;
689 }
690
691 /*
692 * Check that the last subject RDN is a commonName, and that
693 * all the previous RDNs match the issuer exactly
694 */
695 tmpsubject = X509_NAME_dup(tmpsubject);
696 if (tmpsubject == NULL) {
9311d0c4 697 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
c8223538
RL
698 ctx->error = X509_V_ERR_OUT_OF_MEM;
699 return 0;
700 }
701
702 tmpentry =
703 X509_NAME_delete_entry(tmpsubject, last_object_loc);
704 last_object_nid =
705 OBJ_obj2nid(X509_NAME_ENTRY_get_object(tmpentry));
706
707 if (last_object_nid != NID_commonName
708 || X509_NAME_cmp(tmpsubject, tmpissuer) != 0) {
709 err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;
710 }
711
712 X509_NAME_ENTRY_free(tmpentry);
713 X509_NAME_free(tmpsubject);
714
715 proxy_name_done:
6e5e118c 716 CHECK_CB(err != X509_V_OK, ctx, x, i, err);
c8223538
RL
717 }
718
0f113f3e
MC
719 /*
720 * Check against constraints for all certificates higher in chain
721 * including trust anchor. Trust anchor not strictly speaking needed
722 * but if it includes constraints it is to be assumed it expects them
723 * to be obeyed.
724 */
725 for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
726 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
70dd3c65 727
0f113f3e 728 if (nc) {
70dd3c65
VD
729 int rv = NAME_CONSTRAINTS_check(x, nc);
730
5bd5dcd4 731 /* If EE certificate check commonName too */
55a6250f
VD
732 if (rv == X509_V_OK && i == 0
733 && (ctx->param->hostflags
734 & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT) == 0
735 && ((ctx->param->hostflags
736 & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) != 0
737 || !has_san_id(x, GEN_DNS)))
5bd5dcd4
DSH
738 rv = NAME_CONSTRAINTS_check_CN(x, nc);
739
f3e235ed
VD
740 switch (rv) {
741 case X509_V_OK:
742 break;
743 case X509_V_ERR_OUT_OF_MEM:
70dd3c65 744 return 0;
f3e235ed 745 default:
6e5e118c 746 CHECK_CB(1, ctx, x, i, rv);
f3e235ed
VD
747 break;
748 }
0f113f3e
MC
749 }
750 }
751 }
752 return 1;
753}
e9746e03 754
3bf15e29 755static int check_id_error(X509_STORE_CTX *ctx, int errcode)
0f113f3e 756{
70dd3c65 757 return verify_cb_cert(ctx, ctx->cert, 0, errcode);
0f113f3e 758}
3bf15e29 759
9689a6ae 760static int check_hosts(X509 *x, X509_VERIFY_PARAM *vpm)
0f113f3e
MC
761{
762 int i;
9689a6ae 763 int n = sk_OPENSSL_STRING_num(vpm->hosts);
0f113f3e
MC
764 char *name;
765
9689a6ae
DSH
766 if (vpm->peername != NULL) {
767 OPENSSL_free(vpm->peername);
768 vpm->peername = NULL;
a0724ef1 769 }
0f113f3e 770 for (i = 0; i < n; ++i) {
9689a6ae
DSH
771 name = sk_OPENSSL_STRING_value(vpm->hosts, i);
772 if (X509_check_host(x, name, 0, vpm->hostflags, &vpm->peername) > 0)
0f113f3e
MC
773 return 1;
774 }
775 return n == 0;
776}
8abffa4a 777
3bf15e29 778static int check_id(X509_STORE_CTX *ctx)
0f113f3e
MC
779{
780 X509_VERIFY_PARAM *vpm = ctx->param;
0f113f3e 781 X509 *x = ctx->cert;
9689a6ae 782 if (vpm->hosts && check_hosts(x, vpm) <= 0) {
0f113f3e
MC
783 if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
784 return 0;
785 }
9689a6ae 786 if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) {
0f113f3e
MC
787 if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
788 return 0;
789 }
9689a6ae 790 if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) {
0f113f3e
MC
791 if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
792 return 0;
793 }
794 return 1;
795}
3bf15e29 796
d9b8b89b 797static int check_trust(X509_STORE_CTX *ctx, int num_untrusted)
51630a37 798{
70dd3c65 799 int i;
0f113f3e 800 X509 *x = NULL;
d9b8b89b 801 X509 *mx;
b9aec69a 802 SSL_DANE *dane = ctx->dane;
d9b8b89b
VD
803 int num = sk_X509_num(ctx->chain);
804 int trust;
805
bdcadca2
VD
806 /*
807 * Check for a DANE issuer at depth 1 or greater, if it is a DANE-TA(2)
808 * match, we're done, otherwise we'll merely record the match depth.
809 */
810 if (DANETLS_HAS_TA(dane) && num_untrusted > 0 && num_untrusted < num) {
170b7358
VD
811 switch (trust = check_dane_issuer(ctx, num_untrusted)) {
812 case X509_TRUST_TRUSTED:
813 case X509_TRUST_REJECTED:
814 return trust;
815 }
816 }
817
d9b8b89b
VD
818 /*
819 * Check trusted certificates in chain at depth num_untrusted and up.
820 * Note, that depths 0..num_untrusted-1 may also contain trusted
821 * certificates, but the caller is expected to have already checked those,
822 * and wants to incrementally check just any added since.
823 */
824 for (i = num_untrusted; i < num; i++) {
0f113f3e 825 x = sk_X509_value(ctx->chain, i);
d9b8b89b 826 trust = X509_check_trust(x, ctx->param->trust, 0);
0f113f3e 827 /* If explicitly trusted return trusted */
d9b8b89b
VD
828 if (trust == X509_TRUST_TRUSTED)
829 goto trusted;
830 if (trust == X509_TRUST_REJECTED)
831 goto rejected;
0f113f3e 832 }
d9b8b89b 833
0f113f3e 834 /*
d9b8b89b
VD
835 * If we are looking at a trusted certificate, and accept partial chains,
836 * the chain is PKIX trusted.
0f113f3e 837 */
d9b8b89b
VD
838 if (num_untrusted < num) {
839 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN)
840 goto trusted;
841 return X509_TRUST_UNTRUSTED;
842 }
843
497ecc0d 844 if (num_untrusted == num && ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
d9b8b89b
VD
845 /*
846 * Last-resort call with no new trusted certificates, check the leaf
847 * for a direct trust store match.
848 */
bdcadca2
VD
849 i = 0;
850 x = sk_X509_value(ctx->chain, i);
0f113f3e 851 mx = lookup_cert_match(ctx, x);
d9b8b89b
VD
852 if (!mx)
853 return X509_TRUST_UNTRUSTED;
854
855 /*
856 * Check explicit auxiliary trust/reject settings. If none are set,
857 * we'll accept X509_TRUST_UNTRUSTED when not self-signed.
858 */
859 trust = X509_check_trust(mx, ctx->param->trust, 0);
860 if (trust == X509_TRUST_REJECTED) {
861 X509_free(mx);
862 goto rejected;
0f113f3e 863 }
d9b8b89b
VD
864
865 /* Replace leaf with trusted match */
866 (void) sk_X509_set(ctx->chain, 0, mx);
867 X509_free(x);
868 ctx->num_untrusted = 0;
869 goto trusted;
0f113f3e
MC
870 }
871
872 /*
873 * If no trusted certs in chain at all return untrusted and allow
874 * standard (no issuer cert) etc errors to be indicated.
875 */
876 return X509_TRUST_UNTRUSTED;
d9b8b89b
VD
877
878 rejected:
6e5e118c
DO
879 return verify_cb_cert(ctx, x, i, X509_V_ERR_CERT_REJECTED) == 0
880 ? X509_TRUST_REJECTED : X509_TRUST_UNTRUSTED;
d9b8b89b
VD
881
882 trusted:
170b7358
VD
883 if (!DANETLS_ENABLED(dane))
884 return X509_TRUST_TRUSTED;
885 if (dane->pdpth < 0)
886 dane->pdpth = num_untrusted;
887 /* With DANE, PKIX alone is not trusted until we have both */
888 if (dane->mdpth >= 0)
889 return X509_TRUST_TRUSTED;
890 return X509_TRUST_UNTRUSTED;
51630a37
DSH
891}
892
b545dc67 893static int check_revocation(X509_STORE_CTX *ctx)
0f113f3e 894{
4c9b0a03 895 int i = 0, last = 0, ok = 0;
0f113f3e
MC
896 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
897 return 1;
898 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
899 last = sk_X509_num(ctx->chain) - 1;
900 else {
901 /* If checking CRL paths this isn't the EE certificate */
902 if (ctx->parent)
903 return 1;
904 last = 0;
905 }
906 for (i = 0; i <= last; i++) {
907 ctx->error_depth = i;
908 ok = check_cert(ctx);
909 if (!ok)
910 return ok;
911 }
912 return 1;
913}
b545dc67
DSH
914
915static int check_cert(X509_STORE_CTX *ctx)
0f113f3e
MC
916{
917 X509_CRL *crl = NULL, *dcrl = NULL;
70dd3c65
VD
918 int ok = 0;
919 int cnum = ctx->error_depth;
920 X509 *x = sk_X509_value(ctx->chain, cnum);
921
0f113f3e
MC
922 ctx->current_cert = x;
923 ctx->current_issuer = NULL;
924 ctx->current_crl_score = 0;
925 ctx->current_reasons = 0;
70dd3c65 926
790555d6
RL
927 if (x->ex_flags & EXFLAG_PROXY)
928 return 1;
929
0f113f3e 930 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
70dd3c65
VD
931 unsigned int last_reasons = ctx->current_reasons;
932
0f113f3e
MC
933 /* Try to retrieve relevant CRL */
934 if (ctx->get_crl)
935 ok = ctx->get_crl(ctx, &crl, x);
936 else
937 ok = get_crl_delta(ctx, &crl, &dcrl, x);
938 /*
939 * If error looking up CRL, nothing we can do except notify callback
940 */
941 if (!ok) {
70dd3c65
VD
942 ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL);
943 goto done;
0f113f3e
MC
944 }
945 ctx->current_crl = crl;
946 ok = ctx->check_crl(ctx, crl);
947 if (!ok)
70dd3c65 948 goto done;
0f113f3e
MC
949
950 if (dcrl) {
951 ok = ctx->check_crl(ctx, dcrl);
952 if (!ok)
70dd3c65 953 goto done;
0f113f3e
MC
954 ok = ctx->cert_crl(ctx, dcrl, x);
955 if (!ok)
70dd3c65 956 goto done;
0f113f3e
MC
957 } else
958 ok = 1;
959
960 /* Don't look in full CRL if delta reason is removefromCRL */
961 if (ok != 2) {
962 ok = ctx->cert_crl(ctx, crl, x);
963 if (!ok)
70dd3c65 964 goto done;
0f113f3e
MC
965 }
966
967 X509_CRL_free(crl);
968 X509_CRL_free(dcrl);
969 crl = NULL;
970 dcrl = NULL;
971 /*
60250017 972 * If reasons not updated we won't get anywhere by another iteration,
0f113f3e
MC
973 * so exit loop.
974 */
975 if (last_reasons == ctx->current_reasons) {
70dd3c65
VD
976 ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL);
977 goto done;
0f113f3e
MC
978 }
979 }
70dd3c65 980 done:
0f113f3e
MC
981 X509_CRL_free(crl);
982 X509_CRL_free(dcrl);
983
984 ctx->current_crl = NULL;
985 return ok;
0f113f3e 986}
b545dc67 987
e1a27eb3
DSH
988/* Check CRL times against values in X509_STORE_CTX */
989
990static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
0f113f3e
MC
991{
992 time_t *ptime;
993 int i;
70dd3c65 994
0f113f3e
MC
995 if (notify)
996 ctx->current_crl = crl;
997 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
998 ptime = &ctx->param->check_time;
d35ff2c0
DW
999 else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
1000 return 1;
0f113f3e
MC
1001 else
1002 ptime = NULL;
1003
568ce3a5 1004 i = X509_cmp_time(X509_CRL_get0_lastUpdate(crl), ptime);
0f113f3e
MC
1005 if (i == 0) {
1006 if (!notify)
1007 return 0;
70dd3c65 1008 if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD))
0f113f3e
MC
1009 return 0;
1010 }
1011
1012 if (i > 0) {
1013 if (!notify)
1014 return 0;
70dd3c65 1015 if (!verify_cb_crl(ctx, X509_V_ERR_CRL_NOT_YET_VALID))
0f113f3e
MC
1016 return 0;
1017 }
1018
568ce3a5
DSH
1019 if (X509_CRL_get0_nextUpdate(crl)) {
1020 i = X509_cmp_time(X509_CRL_get0_nextUpdate(crl), ptime);
0f113f3e
MC
1021
1022 if (i == 0) {
1023 if (!notify)
1024 return 0;
70dd3c65 1025 if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD))
0f113f3e
MC
1026 return 0;
1027 }
e99505b4 1028 /* Ignore expiration of base CRL is delta is valid */
0f113f3e
MC
1029 if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
1030 if (!notify)
1031 return 0;
70dd3c65 1032 if (!verify_cb_crl(ctx, X509_V_ERR_CRL_HAS_EXPIRED))
0f113f3e
MC
1033 return 0;
1034 }
1035 }
1036
1037 if (notify)
1038 ctx->current_crl = NULL;
1039
1040 return 1;
1041}
e1a27eb3 1042
d43c4497 1043static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
0f113f3e
MC
1044 X509 **pissuer, int *pscore, unsigned int *preasons,
1045 STACK_OF(X509_CRL) *crls)
1046{
1047 int i, crl_score, best_score = *pscore;
1048 unsigned int reasons, best_reasons = 0;
1049 X509 *x = ctx->current_cert;
1050 X509_CRL *crl, *best_crl = NULL;
1051 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
1052
1053 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
1054 crl = sk_X509_CRL_value(crls, i);
1055 reasons = *preasons;
1056 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
8b7c51a0 1057 if (crl_score < best_score || crl_score == 0)
626aa248
DSH
1058 continue;
1059 /* If current CRL is equivalent use it if it is newer */
8b7c51a0 1060 if (crl_score == best_score && best_crl != NULL) {
626aa248 1061 int day, sec;
568ce3a5
DSH
1062 if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl),
1063 X509_CRL_get0_lastUpdate(crl)) == 0)
626aa248 1064 continue;
e032117d
DSH
1065 /*
1066 * ASN1_TIME_diff never returns inconsistent signs for |day|
1067 * and |sec|.
1068 */
1069 if (day <= 0 && sec <= 0)
626aa248 1070 continue;
0f113f3e 1071 }
626aa248
DSH
1072 best_crl = crl;
1073 best_crl_issuer = crl_issuer;
1074 best_score = crl_score;
1075 best_reasons = reasons;
0f113f3e
MC
1076 }
1077
1078 if (best_crl) {
222561fe 1079 X509_CRL_free(*pcrl);
0f113f3e
MC
1080 *pcrl = best_crl;
1081 *pissuer = best_crl_issuer;
1082 *pscore = best_score;
1083 *preasons = best_reasons;
65cbf983 1084 X509_CRL_up_ref(best_crl);
25aaa98a
RS
1085 X509_CRL_free(*pdcrl);
1086 *pdcrl = NULL;
0f113f3e
MC
1087 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
1088 }
1089
1090 if (best_score >= CRL_SCORE_VALID)
1091 return 1;
1092
1093 return 0;
1094}
1095
1096/*
1097 * Compare two CRL extensions for delta checking purposes. They should be
d43c4497
DSH
1098 * both present or both absent. If both present all fields must be identical.
1099 */
1100
1101static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
0f113f3e
MC
1102{
1103 ASN1_OCTET_STRING *exta, *extb;
1104 int i;
1105 i = X509_CRL_get_ext_by_NID(a, nid, -1);
1106 if (i >= 0) {
1107 /* Can't have multiple occurrences */
1108 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
1109 return 0;
1110 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
1111 } else
1112 exta = NULL;
d43c4497 1113
0f113f3e 1114 i = X509_CRL_get_ext_by_NID(b, nid, -1);
d43c4497 1115
0f113f3e 1116 if (i >= 0) {
d43c4497 1117
0f113f3e
MC
1118 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
1119 return 0;
1120 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
1121 } else
1122 extb = NULL;
d43c4497 1123
0f113f3e
MC
1124 if (!exta && !extb)
1125 return 1;
d43c4497 1126
0f113f3e
MC
1127 if (!exta || !extb)
1128 return 0;
d43c4497 1129
0f113f3e
MC
1130 if (ASN1_OCTET_STRING_cmp(exta, extb))
1131 return 0;
d43c4497 1132
0f113f3e
MC
1133 return 1;
1134}
d43c4497
DSH
1135
1136/* See if a base and delta are compatible */
1137
1138static int check_delta_base(X509_CRL *delta, X509_CRL *base)
0f113f3e
MC
1139{
1140 /* Delta CRL must be a delta */
1141 if (!delta->base_crl_number)
1142 return 0;
1143 /* Base must have a CRL number */
1144 if (!base->crl_number)
1145 return 0;
1146 /* Issuer names must match */
1147 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
1148 return 0;
1149 /* AKID and IDP must match */
1150 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
1151 return 0;
1152 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
1153 return 0;
1154 /* Delta CRL base number must not exceed Full CRL number. */
1155 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
1156 return 0;
1157 /* Delta CRL number must exceed full CRL number */
1158 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
1159 return 1;
1160 return 0;
1161}
1162
1163/*
1164 * For a given base CRL find a delta... maybe extend to delta scoring or
1165 * retrieve a chain of deltas...
d43c4497
DSH
1166 */
1167
1168static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
0f113f3e
MC
1169 X509_CRL *base, STACK_OF(X509_CRL) *crls)
1170{
1171 X509_CRL *delta;
1172 int i;
1173 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
1174 return;
1175 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
1176 return;
1177 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
1178 delta = sk_X509_CRL_value(crls, i);
1179 if (check_delta_base(delta, base)) {
1180 if (check_crl_time(ctx, delta, 0))
1181 *pscore |= CRL_SCORE_TIME_DELTA;
65cbf983 1182 X509_CRL_up_ref(delta);
0f113f3e
MC
1183 *dcrl = delta;
1184 return;
1185 }
1186 }
1187 *dcrl = NULL;
1188}
1189
1190/*
1191 * For a given CRL return how suitable it is for the supplied certificate
1192 * 'x'. The return value is a mask of several criteria. If the issuer is not
1193 * the certificate issuer this is returned in *pissuer. The reasons mask is
1194 * also used to determine if the CRL is suitable: if no new reasons the CRL
1195 * is rejected, otherwise reasons is updated.
4b96839f
DSH
1196 */
1197
1198static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
0f113f3e
MC
1199 unsigned int *preasons, X509_CRL *crl, X509 *x)
1200{
1201
1202 int crl_score = 0;
1203 unsigned int tmp_reasons = *preasons, crl_reasons;
1204
1205 /* First see if we can reject CRL straight away */
1206
1207 /* Invalid IDP cannot be processed */
1208 if (crl->idp_flags & IDP_INVALID)
1209 return 0;
1210 /* Reason codes or indirect CRLs need extended CRL support */
1211 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
1212 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
1213 return 0;
1214 } else if (crl->idp_flags & IDP_REASONS) {
1215 /* If no new reasons reject */
1216 if (!(crl->idp_reasons & ~tmp_reasons))
1217 return 0;
1218 }
1219 /* Don't process deltas at this stage */
1220 else if (crl->base_crl_number)
1221 return 0;
1222 /* If issuer name doesn't match certificate need indirect CRL */
1223 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
1224 if (!(crl->idp_flags & IDP_INDIRECT))
1225 return 0;
1226 } else
1227 crl_score |= CRL_SCORE_ISSUER_NAME;
1228
1229 if (!(crl->flags & EXFLAG_CRITICAL))
1230 crl_score |= CRL_SCORE_NOCRITICAL;
1231
e99505b4 1232 /* Check expiration */
0f113f3e
MC
1233 if (check_crl_time(ctx, crl, 0))
1234 crl_score |= CRL_SCORE_TIME;
1235
1236 /* Check authority key ID and locate certificate issuer */
1237 crl_akid_check(ctx, crl, pissuer, &crl_score);
1238
1239 /* If we can't locate certificate issuer at this point forget it */
1240
1241 if (!(crl_score & CRL_SCORE_AKID))
1242 return 0;
1243
1244 /* Check cert for matching CRL distribution points */
1245
1246 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
1247 /* If no new reasons reject */
1248 if (!(crl_reasons & ~tmp_reasons))
1249 return 0;
1250 tmp_reasons |= crl_reasons;
1251 crl_score |= CRL_SCORE_SCOPE;
1252 }
1253
1254 *preasons = tmp_reasons;
1255
1256 return crl_score;
1257
1258}
4b96839f
DSH
1259
1260static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
0f113f3e
MC
1261 X509 **pissuer, int *pcrl_score)
1262{
1263 X509 *crl_issuer = NULL;
8cc86b81 1264 const X509_NAME *cnm = X509_CRL_get_issuer(crl);
0f113f3e
MC
1265 int cidx = ctx->error_depth;
1266 int i;
1267
1268 if (cidx != sk_X509_num(ctx->chain) - 1)
1269 cidx++;
1270
1271 crl_issuer = sk_X509_value(ctx->chain, cidx);
1272
1273 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1274 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1275 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
1276 *pissuer = crl_issuer;
1277 return;
1278 }
1279 }
1280
1281 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1282 crl_issuer = sk_X509_value(ctx->chain, cidx);
1283 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1284 continue;
1285 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1286 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
1287 *pissuer = crl_issuer;
1288 return;
1289 }
1290 }
1291
1292 /* Anything else needs extended CRL support */
1293
1294 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1295 return;
1296
1297 /*
1298 * Otherwise the CRL issuer is not on the path. Look for it in the set of
1299 * untrusted certificates.
1300 */
1301 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1302 crl_issuer = sk_X509_value(ctx->untrusted, i);
1303 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1304 continue;
1305 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1306 *pissuer = crl_issuer;
1307 *pcrl_score |= CRL_SCORE_AKID;
1308 return;
1309 }
1310 }
1311}
1312
1313/*
1314 * Check the path of a CRL issuer certificate. This creates a new
9d84d4ed 1315 * X509_STORE_CTX and populates it with most of the parameters from the
0f113f3e
MC
1316 * parent. This could be optimised somewhat since a lot of path checking will
1317 * be duplicated by the parent, but this will rarely be used in practice.
9d84d4ed
DSH
1318 */
1319
1320static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
0f113f3e
MC
1321{
1322 X509_STORE_CTX crl_ctx;
1323 int ret;
70dd3c65 1324
0f113f3e
MC
1325 /* Don't allow recursive CRL path validation */
1326 if (ctx->parent)
1327 return 0;
faa9dcd4 1328 if (!X509_STORE_CTX_init(&crl_ctx, ctx->store, x, ctx->untrusted))
0f113f3e
MC
1329 return -1;
1330
1331 crl_ctx.crls = ctx->crls;
1332 /* Copy verify params across */
1333 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1334
1335 crl_ctx.parent = ctx;
1336 crl_ctx.verify_cb = ctx->verify_cb;
1337
1338 /* Verify CRL issuer */
1339 ret = X509_verify_cert(&crl_ctx);
0f113f3e
MC
1340 if (ret <= 0)
1341 goto err;
1342
1343 /* Check chain is acceptable */
0f113f3e
MC
1344 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1345 err:
1346 X509_STORE_CTX_cleanup(&crl_ctx);
1347 return ret;
1348}
1349
1350/*
1351 * RFC3280 says nothing about the relationship between CRL path and
1352 * certificate path, which could lead to situations where a certificate could
e99505b4 1353 * be revoked or validated by a CA not authorized to do so. RFC5280 is more
0f113f3e
MC
1354 * strict and states that the two paths must end in the same trust anchor,
1355 * though some discussions remain... until this is resolved we use the
1356 * RFC5280 version
9d84d4ed
DSH
1357 */
1358
1359static int check_crl_chain(X509_STORE_CTX *ctx,
0f113f3e
MC
1360 STACK_OF(X509) *cert_path,
1361 STACK_OF(X509) *crl_path)
1362{
1363 X509 *cert_ta, *crl_ta;
1364 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1365 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1366 if (!X509_cmp(cert_ta, crl_ta))
1367 return 1;
1368 return 0;
1369}
9d84d4ed 1370
3a83462d
MC
1371/*-
1372 * Check for match between two dist point names: three separate cases.
3e727a3b
DSH
1373 * 1. Both are relative names and compare X509_NAME types.
1374 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1375 * 3. Both are full names and compare two GENERAL_NAMES.
d0fff69d 1376 * 4. One is NULL: automatic match.
3e727a3b
DSH
1377 */
1378
3e727a3b 1379static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
0f113f3e
MC
1380{
1381 X509_NAME *nm = NULL;
1382 GENERAL_NAMES *gens = NULL;
1383 GENERAL_NAME *gena, *genb;
1384 int i, j;
1385 if (!a || !b)
1386 return 1;
1387 if (a->type == 1) {
1388 if (!a->dpname)
1389 return 0;
1390 /* Case 1: two X509_NAME */
1391 if (b->type == 1) {
1392 if (!b->dpname)
1393 return 0;
1394 if (!X509_NAME_cmp(a->dpname, b->dpname))
1395 return 1;
1396 else
1397 return 0;
1398 }
1399 /* Case 2: set name and GENERAL_NAMES appropriately */
1400 nm = a->dpname;
1401 gens = b->name.fullname;
1402 } else if (b->type == 1) {
1403 if (!b->dpname)
1404 return 0;
1405 /* Case 2: set name and GENERAL_NAMES appropriately */
1406 gens = a->name.fullname;
1407 nm = b->dpname;
1408 }
1409
1410 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1411 if (nm) {
1412 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1413 gena = sk_GENERAL_NAME_value(gens, i);
1414 if (gena->type != GEN_DIRNAME)
1415 continue;
1416 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1417 return 1;
1418 }
1419 return 0;
1420 }
1421
1422 /* Else case 3: two GENERAL_NAMES */
1423
1424 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1425 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1426 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1427 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1428 if (!GENERAL_NAME_cmp(gena, genb))
1429 return 1;
1430 }
1431 }
1432
1433 return 0;
1434
1435}
bc7535bc 1436
4b96839f 1437static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
0f113f3e
MC
1438{
1439 int i;
8cc86b81 1440 const X509_NAME *nm = X509_CRL_get_issuer(crl);
0f113f3e
MC
1441 /* If no CRLissuer return is successful iff don't need a match */
1442 if (!dp->CRLissuer)
1443 return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
1444 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1445 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1446 if (gen->type != GEN_DIRNAME)
1447 continue;
1448 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1449 return 1;
1450 }
1451 return 0;
1452}
d0fff69d 1453
4b96839f 1454/* Check CRLDP and IDP */
bc7535bc 1455
4b96839f 1456static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
0f113f3e
MC
1457 unsigned int *preasons)
1458{
1459 int i;
1460 if (crl->idp_flags & IDP_ONLYATTR)
1461 return 0;
1462 if (x->ex_flags & EXFLAG_CA) {
1463 if (crl->idp_flags & IDP_ONLYUSER)
1464 return 0;
1465 } else {
1466 if (crl->idp_flags & IDP_ONLYCA)
1467 return 0;
1468 }
1469 *preasons = crl->idp_reasons;
1470 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1471 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1472 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1473 if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1474 *preasons &= dp->dp_reasons;
1475 return 1;
1476 }
1477 }
1478 }
1479 if ((!crl->idp || !crl->idp->distpoint)
1480 && (crl_score & CRL_SCORE_ISSUER_NAME))
1481 return 1;
1482 return 0;
1483}
1484
1485/*
1486 * Retrieve CRL corresponding to current certificate. If deltas enabled try
1487 * to find a delta CRL too
b545dc67 1488 */
0f113f3e 1489
d43c4497 1490static int get_crl_delta(X509_STORE_CTX *ctx,
0f113f3e
MC
1491 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1492{
1493 int ok;
1494 X509 *issuer = NULL;
1495 int crl_score = 0;
1496 unsigned int reasons;
1497 X509_CRL *crl = NULL, *dcrl = NULL;
1498 STACK_OF(X509_CRL) *skcrl;
8cc86b81 1499 const X509_NAME *nm = X509_get_issuer_name(x);
70dd3c65 1500
0f113f3e
MC
1501 reasons = ctx->current_reasons;
1502 ok = get_crl_sk(ctx, &crl, &dcrl,
1503 &issuer, &crl_score, &reasons, ctx->crls);
0f113f3e
MC
1504 if (ok)
1505 goto done;
1506
1507 /* Lookup CRLs from store */
1508
1509 skcrl = ctx->lookup_crls(ctx, nm);
1510
1511 /* If no CRLs found and a near match from get_crl_sk use that */
1512 if (!skcrl && crl)
1513 goto done;
1514
1515 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1516
1517 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1518
1519 done:
0f113f3e
MC
1520 /* If we got any kind of CRL use it and return success */
1521 if (crl) {
1522 ctx->current_issuer = issuer;
1523 ctx->current_crl_score = crl_score;
1524 ctx->current_reasons = reasons;
1525 *pcrl = crl;
1526 *pdcrl = dcrl;
1527 return 1;
1528 }
0f113f3e
MC
1529 return 0;
1530}
b545dc67
DSH
1531
1532/* Check CRL validity */
1533static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
0f113f3e
MC
1534{
1535 X509 *issuer = NULL;
1536 EVP_PKEY *ikey = NULL;
70dd3c65
VD
1537 int cnum = ctx->error_depth;
1538 int chnum = sk_X509_num(ctx->chain) - 1;
1539
ade08735 1540 /* If we have an alternative CRL issuer cert use that */
0f113f3e
MC
1541 if (ctx->current_issuer)
1542 issuer = ctx->current_issuer;
0f113f3e
MC
1543 /*
1544 * Else find CRL issuer: if not last certificate then issuer is next
1545 * certificate in chain.
1546 */
1547 else if (cnum < chnum)
1548 issuer = sk_X509_value(ctx->chain, cnum + 1);
1549 else {
1550 issuer = sk_X509_value(ctx->chain, chnum);
ade08735 1551 /* If not self-issued, can't check signature */
70dd3c65
VD
1552 if (!ctx->check_issued(ctx, issuer, issuer) &&
1553 !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER))
1554 return 0;
0f113f3e
MC
1555 }
1556
70dd3c65
VD
1557 if (issuer == NULL)
1558 return 1;
0f113f3e 1559
70dd3c65
VD
1560 /*
1561 * Skip most tests for deltas because they have already been done
1562 */
1563 if (!crl->base_crl_number) {
1564 /* Check for cRLSign bit if keyUsage present */
1565 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1566 !(issuer->ex_kusage & KU_CRL_SIGN) &&
1567 !verify_cb_crl(ctx, X509_V_ERR_KEYUSAGE_NO_CRL_SIGN))
1568 return 0;
0f113f3e 1569
70dd3c65
VD
1570 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE) &&
1571 !verify_cb_crl(ctx, X509_V_ERR_DIFFERENT_CRL_SCOPE))
1572 return 0;
0f113f3e 1573
70dd3c65
VD
1574 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH) &&
1575 check_crl_path(ctx, ctx->current_issuer) <= 0 &&
1576 !verify_cb_crl(ctx, X509_V_ERR_CRL_PATH_VALIDATION_ERROR))
1577 return 0;
0f113f3e 1578
70dd3c65
VD
1579 if ((crl->idp_flags & IDP_INVALID) &&
1580 !verify_cb_crl(ctx, X509_V_ERR_INVALID_EXTENSION))
1581 return 0;
1582 }
0f113f3e 1583
70dd3c65
VD
1584 if (!(ctx->current_crl_score & CRL_SCORE_TIME) &&
1585 !check_crl_time(ctx, crl, 1))
1586 return 0;
0f113f3e 1587
70dd3c65
VD
1588 /* Attempt to get issuer certificate public key */
1589 ikey = X509_get0_pubkey(issuer);
0f113f3e 1590
70dd3c65
VD
1591 if (!ikey &&
1592 !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY))
1593 return 0;
0f113f3e 1594
70dd3c65
VD
1595 if (ikey) {
1596 int rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
0f113f3e 1597
70dd3c65
VD
1598 if (rv != X509_V_OK && !verify_cb_crl(ctx, rv))
1599 return 0;
1600 /* Verify CRL signature */
1601 if (X509_CRL_verify(crl, ikey) <= 0 &&
1602 !verify_cb_crl(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE))
1603 return 0;
1604 }
1605 return 1;
0f113f3e 1606}
b545dc67
DSH
1607
1608/* Check certificate against CRL */
1609static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
0f113f3e 1610{
0f113f3e 1611 X509_REVOKED *rev;
70dd3c65 1612
0f113f3e
MC
1613 /*
1614 * The rules changed for this... previously if a CRL contained unhandled
1615 * critical extensions it could still be used to indicate a certificate
70dd3c65 1616 * was revoked. This has since been changed since critical extensions can
0f113f3e
MC
1617 * change the meaning of CRL entries.
1618 */
1619 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
70dd3c65
VD
1620 && (crl->flags & EXFLAG_CRITICAL) &&
1621 !verify_cb_crl(ctx, X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION))
1622 return 0;
0f113f3e 1623 /*
70dd3c65
VD
1624 * Look for serial number of certificate in CRL. If found, make sure
1625 * reason is not removeFromCRL.
0f113f3e
MC
1626 */
1627 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1628 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1629 return 2;
70dd3c65 1630 if (!verify_cb_crl(ctx, X509_V_ERR_CERT_REVOKED))
0f113f3e
MC
1631 return 0;
1632 }
1633
1634 return 1;
1635}
b545dc67 1636
5d7c222d 1637static int check_policy(X509_STORE_CTX *ctx)
0f113f3e
MC
1638{
1639 int ret;
3921ded7 1640
0f113f3e
MC
1641 if (ctx->parent)
1642 return 1;
3921ded7
VD
1643 /*
1644 * With DANE, the trust anchor might be a bare public key, not a
1645 * certificate! In that case our chain does not have the trust anchor
1646 * certificate as a top-most element. This comports well with RFC5280
1647 * chain verification, since there too, the trust anchor is not part of the
1648 * chain to be verified. In particular, X509_policy_check() does not look
1649 * at the TA cert, but assumes that it is present as the top-most chain
1650 * element. We therefore temporarily push a NULL cert onto the chain if it
1651 * was verified via a bare public key, and pop it off right after the
1652 * X509_policy_check() call.
1653 */
1654 if (ctx->bare_ta_signed && !sk_X509_push(ctx->chain, NULL)) {
9311d0c4 1655 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
f3e235ed 1656 ctx->error = X509_V_ERR_OUT_OF_MEM;
3921ded7
VD
1657 return 0;
1658 }
0f113f3e
MC
1659 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1660 ctx->param->policies, ctx->param->flags);
3921ded7 1661 if (ctx->bare_ta_signed)
225c9660 1662 (void)sk_X509_pop(ctx->chain);
3921ded7 1663
895c2f84 1664 if (ret == X509_PCY_TREE_INTERNAL) {
9311d0c4 1665 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
f3e235ed 1666 ctx->error = X509_V_ERR_OUT_OF_MEM;
0f113f3e
MC
1667 return 0;
1668 }
1669 /* Invalid or inconsistent extensions */
895c2f84 1670 if (ret == X509_PCY_TREE_INVALID) {
0f113f3e 1671 int i;
70dd3c65
VD
1672
1673 /* Locate certificates with bad extensions and notify callback. */
0f113f3e 1674 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
70dd3c65
VD
1675 X509 *x = sk_X509_value(ctx->chain, i);
1676
6e5e118c
DO
1677 CHECK_CB((x->ex_flags & EXFLAG_INVALID_POLICY) != 0,
1678 ctx, x, i, X509_V_ERR_INVALID_POLICY_EXTENSION);
0f113f3e
MC
1679 }
1680 return 1;
1681 }
895c2f84 1682 if (ret == X509_PCY_TREE_FAILURE) {
0f113f3e
MC
1683 ctx->current_cert = NULL;
1684 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1685 return ctx->verify_cb(0, ctx);
1686 }
895c2f84 1687 if (ret != X509_PCY_TREE_VALID) {
9311d0c4 1688 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
895c2f84
VD
1689 return 0;
1690 }
0f113f3e
MC
1691
1692 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1693 ctx->current_cert = NULL;
f3e235ed
VD
1694 /*
1695 * Verification errors need to be "sticky", a callback may have allowed
1696 * an SSL handshake to continue despite an error, and we must then
1697 * remain in an error state. Therefore, we MUST NOT clear earlier
1698 * verification errors by setting the error to X509_V_OK.
1699 */
0f113f3e
MC
1700 if (!ctx->verify_cb(2, ctx))
1701 return 0;
1702 }
1703
1704 return 1;
1705}
5d7c222d 1706
70dd3c65
VD
1707/*-
1708 * Check certificate validity times.
1709 * If depth >= 0, invoke verification callbacks on error, otherwise just return
1710 * the validation status.
1711 *
1712 * Return 1 on success, 0 otherwise.
1713 */
1714int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth)
0f113f3e
MC
1715{
1716 time_t *ptime;
1717 int i;
1718
1719 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1720 ptime = &ctx->param->check_time;
d35ff2c0
DW
1721 else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
1722 return 1;
0f113f3e
MC
1723 else
1724 ptime = NULL;
1725
568ce3a5 1726 i = X509_cmp_time(X509_get0_notBefore(x), ptime);
70dd3c65
VD
1727 if (i >= 0 && depth < 0)
1728 return 0;
6e5e118c
DO
1729 CHECK_CB(i == 0, ctx, x, depth, X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD);
1730 CHECK_CB(i > 0, ctx, x, depth, X509_V_ERR_CERT_NOT_YET_VALID);
0f113f3e 1731
568ce3a5 1732 i = X509_cmp_time(X509_get0_notAfter(x), ptime);
70dd3c65
VD
1733 if (i <= 0 && depth < 0)
1734 return 0;
6e5e118c
DO
1735 CHECK_CB(i == 0, ctx, x, depth, X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD);
1736 CHECK_CB(i < 0, ctx, x, depth, X509_V_ERR_CERT_HAS_EXPIRED);
0f113f3e
MC
1737 return 1;
1738}
e1a27eb3 1739
0b670a21 1740/* verify the issuer signatures and cert times of ctx->chain */
6b691a5c 1741static int internal_verify(X509_STORE_CTX *ctx)
0f113f3e 1742{
70dd3c65
VD
1743 int n = sk_X509_num(ctx->chain) - 1;
1744 X509 *xi = sk_X509_value(ctx->chain, n);
1745 X509 *xs;
0f113f3e 1746
170b7358
VD
1747 /*
1748 * With DANE-verified bare public key TA signatures, it remains only to
1749 * check the timestamps of the top certificate. We report the issuer as
1750 * NULL, since all we have is a bare key.
1751 */
1752 if (ctx->bare_ta_signed) {
1753 xs = xi;
1754 xi = NULL;
0b670a21 1755 goto check_cert_time;
170b7358
VD
1756 }
1757
0b670a21
DDO
1758 if (ctx->check_issued(ctx, xi, xi))
1759 xs = xi; /* the typical case: last cert in the chain is self-issued */
0f113f3e
MC
1760 else {
1761 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
1762 xs = xi;
0b670a21 1763 goto check_cert_time;
0f113f3e 1764 }
02a25671 1765 if (n <= 0) {
6e5e118c 1766 CHECK_CB(1, ctx, xi, 0, X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE);
02a25671
AK
1767
1768 xs = xi;
1769 goto check_cert_time;
1770 }
1771
70dd3c65
VD
1772 n--;
1773 ctx->error_depth = n;
1774 xs = sk_X509_value(ctx->chain, n);
0f113f3e
MC
1775 }
1776
d9b8b89b
VD
1777 /*
1778 * Do not clear ctx->error=0, it must be "sticky", only the user's callback
1779 * is allowed to reset errors (at its own peril).
1780 */
0f113f3e 1781 while (n >= 0) {
0f113f3e 1782 /*
0b670a21
DDO
1783 * For each iteration of this loop:
1784 * n is the subject depth
1785 * xs is the subject cert, for which the signature is to be checked
1786 * xi is the supposed issuer cert containing the public key to use
1787 * Initially xs == xi if the last cert in the chain is self-issued.
1788 *
ade08735
DDO
1789 * Skip signature check for self-signed certificates unless explicitly
1790 * asked for because it does not add any security and just wastes time.
0f113f3e 1791 */
0b670a21
DDO
1792 if (xs != xi || ((ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)
1793 && (xi->ex_flags & EXFLAG_SS) != 0)) {
02369787 1794 EVP_PKEY *pkey;
0b670a21
DDO
1795 /*
1796 * If the issuer's public key is not available or its key usage
1797 * does not support issuing the subject cert, report the issuer
1798 * cert and its depth (rather than n, the depth of the subject).
1799 */
1800 int issuer_depth = n + (xs == xi ? 0 : 1);
1801 /*
1802 * According to https://tools.ietf.org/html/rfc5280#section-6.1.4
1803 * step (n) we must check any given key usage extension in a CA cert
1804 * when preparing the verification of a certificate issued by it.
1805 * According to https://tools.ietf.org/html/rfc5280#section-4.2.1.3
e99505b4
DDO
1806 * we must not verify a certificate signature if the key usage of
1807 * the CA certificate that issued the certificate prohibits signing.
0b670a21
DDO
1808 * In case the 'issuing' certificate is the last in the chain and is
1809 * not a CA certificate but a 'self-issued' end-entity cert (i.e.,
1810 * xs == xi && !(xi->ex_flags & EXFLAG_CA)) RFC 5280 does not apply
1811 * (see https://tools.ietf.org/html/rfc6818#section-2) and thus
1812 * we are free to ignore any key usage restrictions on such certs.
1813 */
1814 int ret = xs == xi && (xi->ex_flags & EXFLAG_CA) == 0
1815 ? X509_V_OK : x509_signing_allowed(xi, xs);
02369787 1816
6e5e118c 1817 CHECK_CB(ret != X509_V_OK, ctx, xi, issuer_depth, ret);
c01ff880 1818 if ((pkey = X509_get0_pubkey(xi)) == NULL) {
6e5e118c
DO
1819 CHECK_CB(1, ctx, xi, issuer_depth,
1820 X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY);
1821 } else {
1822 CHECK_CB(X509_verify(xs, pkey) <= 0,
1823 ctx, xs, n, X509_V_ERR_CERT_SIGNATURE_FAILURE);
0f113f3e 1824 }
0f113f3e
MC
1825 }
1826
3bed88a3 1827 check_cert_time: /* in addition to RFC 5280, do also for trusted (root) cert */
70dd3c65
VD
1828 /* Calls verify callback as needed */
1829 if (!x509_check_cert_time(ctx, xs, n))
1830 return 0;
0f113f3e 1831
70dd3c65
VD
1832 /*
1833 * Signal success at this depth. However, the previous error (if any)
1834 * is retained.
1835 */
0f113f3e
MC
1836 ctx->current_issuer = xi;
1837 ctx->current_cert = xs;
70dd3c65
VD
1838 ctx->error_depth = n;
1839 if (!ctx->verify_cb(1, ctx))
1840 return 0;
0f113f3e 1841
70dd3c65 1842 if (--n >= 0) {
0f113f3e
MC
1843 xi = xs;
1844 xs = sk_X509_value(ctx->chain, n);
1845 }
1846 }
70dd3c65 1847 return 1;
0f113f3e 1848}
d02b48c6 1849
91b73acb 1850int X509_cmp_current_time(const ASN1_TIME *ctm)
bbb72003 1851{
0f113f3e 1852 return X509_cmp_time(ctm, NULL);
bbb72003
DSH
1853}
1854
91b73acb 1855int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
0f113f3e 1856{
80770da3
EK
1857 static const size_t utctime_length = sizeof("YYMMDDHHMMSSZ") - 1;
1858 static const size_t generalizedtime_length = sizeof("YYYYMMDDHHMMSSZ") - 1;
1859 ASN1_TIME *asn1_cmp_time = NULL;
1860 int i, day, sec, ret = 0;
48102247 1861#ifdef CHARSET_EBCDIC
1862 const char upper_z = 0x5A;
1863#else
1864 const char upper_z = 'Z';
1865#endif
f48b83b4 1866 /*
80770da3
EK
1867 * Note that ASN.1 allows much more slack in the time format than RFC5280.
1868 * In RFC5280, the representation is fixed:
f48b83b4
EK
1869 * UTCTime: YYMMDDHHMMSSZ
1870 * GeneralizedTime: YYYYMMDDHHMMSSZ
80770da3
EK
1871 *
1872 * We do NOT currently enforce the following RFC 5280 requirement:
1873 * "CAs conforming to this profile MUST always encode certificate
1874 * validity dates through the year 2049 as UTCTime; certificate validity
1875 * dates in 2050 or later MUST be encoded as GeneralizedTime."
f48b83b4 1876 */
80770da3
EK
1877 switch (ctm->type) {
1878 case V_ASN1_UTCTIME:
1879 if (ctm->length != (int)(utctime_length))
0f113f3e 1880 return 0;
80770da3
EK
1881 break;
1882 case V_ASN1_GENERALIZEDTIME:
1883 if (ctm->length != (int)(generalizedtime_length))
0f113f3e 1884 return 0;
80770da3
EK
1885 break;
1886 default:
1887 return 0;
0f113f3e
MC
1888 }
1889
80770da3
EK
1890 /**
1891 * Verify the format: the ASN.1 functions we use below allow a more
1892 * flexible format than what's mandated by RFC 5280.
1893 * Digit and date ranges will be verified in the conversion methods.
1894 */
1895 for (i = 0; i < ctm->length - 1; i++) {
48102247 1896 if (!ascii_isdigit(ctm->data[i]))
f48b83b4 1897 return 0;
0f113f3e 1898 }
48102247 1899 if (ctm->data[ctm->length - 1] != upper_z)
f48b83b4 1900 return 0;
0f113f3e 1901
80770da3
EK
1902 /*
1903 * There is ASN1_UTCTIME_cmp_time_t but no
1904 * ASN1_GENERALIZEDTIME_cmp_time_t or ASN1_TIME_cmp_time_t,
1905 * so we go through ASN.1
1906 */
1907 asn1_cmp_time = X509_time_adj(NULL, 0, cmp_time);
1908 if (asn1_cmp_time == NULL)
1909 goto err;
1910 if (!ASN1_TIME_diff(&day, &sec, ctm, asn1_cmp_time))
1911 goto err;
0f113f3e 1912
80770da3
EK
1913 /*
1914 * X509_cmp_time comparison is <=.
1915 * The return value 0 is reserved for errors.
1916 */
1917 ret = (day >= 0 && sec >= 0) ? -1 : 1;
1918
1919 err:
1920 ASN1_TIME_free(asn1_cmp_time);
1921 return ret;
0f113f3e 1922}
d02b48c6 1923
25d7cd1d
DDO
1924/*
1925 * Return 0 if time should not be checked or reference time is in range,
1926 * or else 1 if it is past the end, or -1 if it is before the start
1927 */
1928int X509_cmp_timeframe(const X509_VERIFY_PARAM *vpm,
1929 const ASN1_TIME *start, const ASN1_TIME *end)
1930{
1931 time_t ref_time;
1932 time_t *time = NULL;
1933 unsigned long flags = vpm == NULL ? 0 : X509_VERIFY_PARAM_get_flags(vpm);
1934
1935 if ((flags & X509_V_FLAG_USE_CHECK_TIME) != 0) {
1936 ref_time = X509_VERIFY_PARAM_get_time(vpm);
1937 time = &ref_time;
1938 } else if ((flags & X509_V_FLAG_NO_CHECK_TIME) != 0) {
1939 return 0; /* this means ok */
1940 } /* else reference time is the current time */
1941
1942 if (end != NULL && X509_cmp_time(end, time) < 0)
1943 return 1;
1944 if (start != NULL && X509_cmp_time(start, time) > 0)
1945 return -1;
1946 return 0;
1947}
1948
284ef5f3 1949ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
bbb72003 1950{
0f113f3e 1951 return X509_time_adj(s, adj, NULL);
bbb72003
DSH
1952}
1953
87d3a0cd 1954ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
0f113f3e
MC
1955{
1956 return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1957}
87d3a0cd
DSH
1958
1959ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
0f113f3e
MC
1960 int offset_day, long offset_sec, time_t *in_tm)
1961{
1962 time_t t;
1963
1964 if (in_tm)
1965 t = *in_tm;
1966 else
1967 time(&t);
1968
1969 if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
1970 if (s->type == V_ASN1_UTCTIME)
1971 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
1972 if (s->type == V_ASN1_GENERALIZEDTIME)
1973 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
1974 }
1975 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1976}
d02b48c6 1977
7e258a56 1978int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
0f113f3e
MC
1979{
1980 EVP_PKEY *ktmp = NULL, *ktmp2;
1981 int i, j;
1982
1983 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
1984 return 1;
1985
1986 for (i = 0; i < sk_X509_num(chain); i++) {
c01ff880 1987 ktmp = X509_get0_pubkey(sk_X509_value(chain, i));
0f113f3e 1988 if (ktmp == NULL) {
9311d0c4 1989 ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
0f113f3e
MC
1990 return 0;
1991 }
1992 if (!EVP_PKEY_missing_parameters(ktmp))
1993 break;
0f113f3e
MC
1994 }
1995 if (ktmp == NULL) {
9311d0c4 1996 ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
0f113f3e
MC
1997 return 0;
1998 }
1999
2000 /* first, populate the other certs */
2001 for (j = i - 1; j >= 0; j--) {
c01ff880 2002 ktmp2 = X509_get0_pubkey(sk_X509_value(chain, j));
0f113f3e 2003 EVP_PKEY_copy_parameters(ktmp2, ktmp);
0f113f3e
MC
2004 }
2005
2006 if (pkey != NULL)
2007 EVP_PKEY_copy_parameters(pkey, ktmp);
0f113f3e
MC
2008 return 1;
2009}
d02b48c6 2010
e99505b4 2011/* Make a delta CRL as the difference between two full CRLs */
2e8cb108
DSH
2012
2013X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
0f113f3e
MC
2014 EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
2015{
2016 X509_CRL *crl = NULL;
2017 int i;
2018 STACK_OF(X509_REVOKED) *revs = NULL;
2019 /* CRLs can't be delta already */
2020 if (base->base_crl_number || newer->base_crl_number) {
9311d0c4 2021 ERR_raise(ERR_LIB_X509, X509_R_CRL_ALREADY_DELTA);
0f113f3e
MC
2022 return NULL;
2023 }
2024 /* Base and new CRL must have a CRL number */
2025 if (!base->crl_number || !newer->crl_number) {
9311d0c4 2026 ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_NUMBER);
0f113f3e
MC
2027 return NULL;
2028 }
2029 /* Issuer names must match */
2030 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
9311d0c4 2031 ERR_raise(ERR_LIB_X509, X509_R_ISSUER_MISMATCH);
0f113f3e
MC
2032 return NULL;
2033 }
2034 /* AKID and IDP must match */
2035 if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
9311d0c4 2036 ERR_raise(ERR_LIB_X509, X509_R_AKID_MISMATCH);
0f113f3e
MC
2037 return NULL;
2038 }
2039 if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
9311d0c4 2040 ERR_raise(ERR_LIB_X509, X509_R_IDP_MISMATCH);
0f113f3e
MC
2041 return NULL;
2042 }
2043 /* Newer CRL number must exceed full CRL number */
2044 if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
9311d0c4 2045 ERR_raise(ERR_LIB_X509, X509_R_NEWER_CRL_NOT_NEWER);
0f113f3e
MC
2046 return NULL;
2047 }
2048 /* CRLs must verify */
2049 if (skey && (X509_CRL_verify(base, skey) <= 0 ||
2050 X509_CRL_verify(newer, skey) <= 0)) {
9311d0c4 2051 ERR_raise(ERR_LIB_X509, X509_R_CRL_VERIFY_FAILURE);
0f113f3e
MC
2052 return NULL;
2053 }
2054 /* Create new CRL */
2055 crl = X509_CRL_new();
90945fa3 2056 if (crl == NULL || !X509_CRL_set_version(crl, 1))
0f113f3e
MC
2057 goto memerr;
2058 /* Set issuer name */
2059 if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
2060 goto memerr;
2061
568ce3a5 2062 if (!X509_CRL_set1_lastUpdate(crl, X509_CRL_get0_lastUpdate(newer)))
0f113f3e 2063 goto memerr;
568ce3a5 2064 if (!X509_CRL_set1_nextUpdate(crl, X509_CRL_get0_nextUpdate(newer)))
0f113f3e
MC
2065 goto memerr;
2066
2067 /* Set base CRL number: must be critical */
2068
2069 if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
2070 goto memerr;
2071
2072 /*
2073 * Copy extensions across from newest CRL to delta: this will set CRL
2074 * number to correct value too.
2075 */
2076
2077 for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
2078 X509_EXTENSION *ext;
2079 ext = X509_CRL_get_ext(newer, i);
2080 if (!X509_CRL_add_ext(crl, ext, -1))
2081 goto memerr;
2082 }
2083
2084 /* Go through revoked entries, copying as needed */
2085
2086 revs = X509_CRL_get_REVOKED(newer);
2087
2088 for (i = 0; i < sk_X509_REVOKED_num(revs); i++) {
2089 X509_REVOKED *rvn, *rvtmp;
2090 rvn = sk_X509_REVOKED_value(revs, i);
2091 /*
2092 * Add only if not also in base. TODO: need something cleverer here
2093 * for some more complex CRLs covering multiple CAs.
2094 */
34a42e14 2095 if (!X509_CRL_get0_by_serial(base, &rvtmp, &rvn->serialNumber)) {
0f113f3e
MC
2096 rvtmp = X509_REVOKED_dup(rvn);
2097 if (!rvtmp)
2098 goto memerr;
2099 if (!X509_CRL_add0_revoked(crl, rvtmp)) {
2100 X509_REVOKED_free(rvtmp);
2101 goto memerr;
2102 }
2103 }
2104 }
2105 /* TODO: optionally prune deleted entries */
2106
2107 if (skey && md && !X509_CRL_sign(crl, skey, md))
2108 goto memerr;
2109
2110 return crl;
2111
2112 memerr:
9311d0c4 2113 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
222561fe 2114 X509_CRL_free(crl);
0f113f3e
MC
2115 return NULL;
2116}
2117
6b691a5c 2118int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
0f113f3e
MC
2119{
2120 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
2121}
58964a49 2122
8cc86b81 2123void *X509_STORE_CTX_get_ex_data(const X509_STORE_CTX *ctx, int idx)
0f113f3e
MC
2124{
2125 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
2126}
58964a49 2127
8cc86b81 2128int X509_STORE_CTX_get_error(const X509_STORE_CTX *ctx)
0f113f3e
MC
2129{
2130 return ctx->error;
2131}
58964a49 2132
6b691a5c 2133void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
0f113f3e
MC
2134{
2135 ctx->error = err;
2136}
58964a49 2137
8cc86b81 2138int X509_STORE_CTX_get_error_depth(const X509_STORE_CTX *ctx)
0f113f3e
MC
2139{
2140 return ctx->error_depth;
2141}
58964a49 2142
51227177
VD
2143void X509_STORE_CTX_set_error_depth(X509_STORE_CTX *ctx, int depth)
2144{
2145 ctx->error_depth = depth;
2146}
2147
8cc86b81 2148X509 *X509_STORE_CTX_get_current_cert(const X509_STORE_CTX *ctx)
0f113f3e
MC
2149{
2150 return ctx->current_cert;
2151}
58964a49 2152
c9654873
VD
2153void X509_STORE_CTX_set_current_cert(X509_STORE_CTX *ctx, X509 *x)
2154{
2155 ctx->current_cert = x;
2156}
2157
8cc86b81 2158STACK_OF(X509) *X509_STORE_CTX_get0_chain(const X509_STORE_CTX *ctx)
0f113f3e
MC
2159{
2160 return ctx->chain;
2161}
58964a49 2162
8cc86b81 2163STACK_OF(X509) *X509_STORE_CTX_get1_chain(const X509_STORE_CTX *ctx)
0f113f3e
MC
2164{
2165 if (!ctx->chain)
2166 return NULL;
2167 return X509_chain_up_ref(ctx->chain);
2168}
25f923dd 2169
8cc86b81 2170X509 *X509_STORE_CTX_get0_current_issuer(const X509_STORE_CTX *ctx)
0f113f3e
MC
2171{
2172 return ctx->current_issuer;
2173}
2008e714 2174
8cc86b81 2175X509_CRL *X509_STORE_CTX_get0_current_crl(const X509_STORE_CTX *ctx)
0f113f3e
MC
2176{
2177 return ctx->current_crl;
2178}
2008e714 2179
8cc86b81 2180X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(const X509_STORE_CTX *ctx)
0f113f3e
MC
2181{
2182 return ctx->parent;
2183}
2008e714 2184
6b691a5c 2185void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
0f113f3e
MC
2186{
2187 ctx->cert = x;
2188}
58964a49 2189
e1a27eb3 2190void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
0f113f3e
MC
2191{
2192 ctx->crls = sk;
2193}
e1a27eb3 2194
13938ace 2195int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
0f113f3e 2196{
0daccd4d
VD
2197 /*
2198 * XXX: Why isn't this function always used to set the associated trust?
2199 * Should there even be a VPM->trust field at all? Or should the trust
2200 * always be inferred from the purpose by X509_STORE_CTX_init().
2201 */
0f113f3e
MC
2202 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
2203}
11262391 2204
bb7cd4e3 2205int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
0f113f3e 2206{
0daccd4d
VD
2207 /*
2208 * XXX: See above, this function would only be needed when the default
2209 * trust for the purpose needs an override in a corner case.
2210 */
0f113f3e
MC
2211 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
2212}
2213
2214/*
2215 * This function is used to set the X509_STORE_CTX purpose and trust values.
2216 * This is intended to be used when another structure has its own trust and
2217 * purpose values which (if set) will be inherited by the ctx. If they aren't
2218 * set then we will usually have a default purpose in mind which should then
2219 * be used to set the trust value. An example of this is SSL use: an SSL
2220 * structure will have its own purpose and trust settings which the
2221 * application can set: if they aren't set then we use the default of SSL
2222 * client/server.
13938ace
DSH
2223 */
2224
2225int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
0f113f3e
MC
2226 int purpose, int trust)
2227{
2228 int idx;
2229 /* If purpose not set use default */
12a765a5 2230 if (purpose == 0)
0f113f3e
MC
2231 purpose = def_purpose;
2232 /* If we have a purpose then check it is valid */
12a765a5 2233 if (purpose != 0) {
0f113f3e
MC
2234 X509_PURPOSE *ptmp;
2235 idx = X509_PURPOSE_get_by_id(purpose);
2236 if (idx == -1) {
9311d0c4 2237 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID);
0f113f3e
MC
2238 return 0;
2239 }
2240 ptmp = X509_PURPOSE_get0(idx);
2241 if (ptmp->trust == X509_TRUST_DEFAULT) {
2242 idx = X509_PURPOSE_get_by_id(def_purpose);
0daccd4d
VD
2243 /*
2244 * XXX: In the two callers above def_purpose is always 0, which is
2245 * not a known value, so idx will always be -1. How is the
2246 * X509_TRUST_DEFAULT case actually supposed to be handled?
2247 */
0f113f3e 2248 if (idx == -1) {
9311d0c4 2249 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID);
0f113f3e
MC
2250 return 0;
2251 }
2252 ptmp = X509_PURPOSE_get0(idx);
2253 }
2254 /* If trust not set then get from purpose default */
2255 if (!trust)
2256 trust = ptmp->trust;
2257 }
2258 if (trust) {
2259 idx = X509_TRUST_get_by_id(trust);
2260 if (idx == -1) {
9311d0c4 2261 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_TRUST_ID);
0f113f3e
MC
2262 return 0;
2263 }
2264 }
2265
2266 if (purpose && !ctx->param->purpose)
2267 ctx->param->purpose = purpose;
2268 if (trust && !ctx->param->trust)
2269 ctx->param->trust = trust;
2270 return 1;
51630a37
DSH
2271}
2272
b4250010 2273X509_STORE_CTX *X509_STORE_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
2f043896 2274{
b51bce94 2275 X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
b196e7d9 2276
90945fa3 2277 if (ctx == NULL) {
9311d0c4 2278 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2279 return NULL;
2280 }
1143c27b
MC
2281
2282 ctx->libctx = libctx;
2283 if (propq != NULL) {
2284 ctx->propq = OPENSSL_strdup(propq);
2285 if (ctx->propq == NULL) {
2286 OPENSSL_free(ctx);
9311d0c4 2287 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
1143c27b
MC
2288 return NULL;
2289 }
2290 }
2291
0f113f3e 2292 return ctx;
2f043896
DSH
2293}
2294
1143c27b
MC
2295X509_STORE_CTX *X509_STORE_CTX_new(void)
2296{
d8652be0 2297 return X509_STORE_CTX_new_ex(NULL, NULL);
1143c27b
MC
2298}
2299
2300
2f043896
DSH
2301void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
2302{
c001ce33 2303 if (ctx == NULL)
222561fe 2304 return;
c001ce33 2305
0f113f3e 2306 X509_STORE_CTX_cleanup(ctx);
1143c27b
MC
2307
2308 /* libctx and propq survive X509_STORE_CTX_cleanup() */
2309 OPENSSL_free(ctx->propq);
2310
0f113f3e 2311 OPENSSL_free(ctx);
2f043896
DSH
2312}
2313
79aa04ef 2314int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
0f113f3e
MC
2315 STACK_OF(X509) *chain)
2316{
2317 int ret = 1;
ecdaa1ae 2318
faa9dcd4 2319 ctx->store = store;
0f113f3e
MC
2320 ctx->cert = x509;
2321 ctx->untrusted = chain;
2322 ctx->crls = NULL;
d9b8b89b 2323 ctx->num_untrusted = 0;
0f113f3e
MC
2324 ctx->other_ctx = NULL;
2325 ctx->valid = 0;
2326 ctx->chain = NULL;
2327 ctx->error = 0;
2328 ctx->explicit_policy = 0;
2329 ctx->error_depth = 0;
2330 ctx->current_cert = NULL;
2331 ctx->current_issuer = NULL;
2332 ctx->current_crl = NULL;
2333 ctx->current_crl_score = 0;
2334 ctx->current_reasons = 0;
2335 ctx->tree = NULL;
2336 ctx->parent = NULL;
919ba009 2337 ctx->dane = NULL;
170b7358 2338 ctx->bare_ta_signed = 0;
e29c73c9
VD
2339 /* Zero ex_data to make sure we're cleanup-safe */
2340 memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
0f113f3e 2341
7b7eb472
VD
2342 /* store->cleanup is always 0 in OpenSSL, if set must be idempotent */
2343 if (store)
0f113f3e 2344 ctx->cleanup = store->cleanup;
7b7eb472 2345 else
0f113f3e
MC
2346 ctx->cleanup = 0;
2347
0f113f3e
MC
2348 if (store && store->check_issued)
2349 ctx->check_issued = store->check_issued;
2350 else
2351 ctx->check_issued = check_issued;
2352
2353 if (store && store->get_issuer)
2354 ctx->get_issuer = store->get_issuer;
2355 else
2356 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2357
2358 if (store && store->verify_cb)
2359 ctx->verify_cb = store->verify_cb;
2360 else
2361 ctx->verify_cb = null_callback;
2362
2363 if (store && store->verify)
2364 ctx->verify = store->verify;
2365 else
2366 ctx->verify = internal_verify;
2367
2368 if (store && store->check_revocation)
2369 ctx->check_revocation = store->check_revocation;
2370 else
2371 ctx->check_revocation = check_revocation;
2372
2373 if (store && store->get_crl)
2374 ctx->get_crl = store->get_crl;
311f2785
VD
2375 else
2376 ctx->get_crl = NULL;
0f113f3e
MC
2377
2378 if (store && store->check_crl)
2379 ctx->check_crl = store->check_crl;
2380 else
2381 ctx->check_crl = check_crl;
2382
2383 if (store && store->cert_crl)
2384 ctx->cert_crl = store->cert_crl;
2385 else
2386 ctx->cert_crl = cert_crl;
2387
0a5fe2eb
RL
2388 if (store && store->check_policy)
2389 ctx->check_policy = store->check_policy;
2390 else
2391 ctx->check_policy = check_policy;
2392
0f113f3e
MC
2393 if (store && store->lookup_certs)
2394 ctx->lookup_certs = store->lookup_certs;
2395 else
6ddbb4cd 2396 ctx->lookup_certs = X509_STORE_CTX_get1_certs;
0f113f3e
MC
2397
2398 if (store && store->lookup_crls)
2399 ctx->lookup_crls = store->lookup_crls;
2400 else
6ddbb4cd 2401 ctx->lookup_crls = X509_STORE_CTX_get1_crls;
0f113f3e 2402
ecdaa1ae 2403 ctx->param = X509_VERIFY_PARAM_new();
2404 if (ctx->param == NULL) {
9311d0c4 2405 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
ecdaa1ae 2406 goto err;
2407 }
2408
2409 /*
2410 * Inherit callbacks and flags from X509_STORE if not set use defaults.
2411 */
2412 if (store)
2413 ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2414 else
2415 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
2416
2417 if (ret)
2418 ret = X509_VERIFY_PARAM_inherit(ctx->param,
2419 X509_VERIFY_PARAM_lookup("default"));
2420
2421 if (ret == 0) {
9311d0c4 2422 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
ecdaa1ae 2423 goto err;
2424 }
2425
0daccd4d
VD
2426 /*
2427 * XXX: For now, continue to inherit trust from VPM, but infer from the
2428 * purpose if this still yields the default value.
2429 */
2430 if (ctx->param->trust == X509_TRUST_DEFAULT) {
2431 int idx = X509_PURPOSE_get_by_id(ctx->param->purpose);
2432 X509_PURPOSE *xp = X509_PURPOSE_get0(idx);
2433
2434 if (xp != NULL)
2435 ctx->param->trust = X509_PURPOSE_get_trust(xp);
2436 }
2437
e29c73c9
VD
2438 if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2439 &ctx->ex_data))
2440 return 1;
9311d0c4 2441 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
ecdaa1ae 2442
d9b8b89b 2443 err:
e29c73c9
VD
2444 /*
2445 * On error clean up allocated storage, if the store context was not
2446 * allocated with X509_STORE_CTX_new() this is our last chance to do so.
2447 */
ecdaa1ae 2448 X509_STORE_CTX_cleanup(ctx);
2449 return 0;
0f113f3e
MC
2450}
2451
2452/*
2453 * Set alternative lookup method: just a STACK of trusted certificates. This
2454 * avoids X509_STORE nastiness where it isn't needed.
2f043896 2455 */
f0e0fd51 2456void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2f043896 2457{
0f113f3e
MC
2458 ctx->other_ctx = sk;
2459 ctx->get_issuer = get_issuer_sk;
c864e761 2460 ctx->lookup_certs = lookup_certs_sk;
2f043896
DSH
2461}
2462
2463void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
0f113f3e 2464{
e29c73c9
VD
2465 /*
2466 * We need to be idempotent because, unfortunately, free() also calls
2467 * cleanup(), so the natural call sequence new(), init(), cleanup(), free()
2468 * calls cleanup() for the same object twice! Thus we must zero the
2469 * pointers below after they're freed!
2470 */
2471 /* Seems to always be 0 in OpenSSL, do this at most once. */
2472 if (ctx->cleanup != NULL) {
0f113f3e 2473 ctx->cleanup(ctx);
e29c73c9
VD
2474 ctx->cleanup = NULL;
2475 }
0f113f3e
MC
2476 if (ctx->param != NULL) {
2477 if (ctx->parent == NULL)
2478 X509_VERIFY_PARAM_free(ctx->param);
2479 ctx->param = NULL;
2480 }
222561fe
RS
2481 X509_policy_tree_free(ctx->tree);
2482 ctx->tree = NULL;
2483 sk_X509_pop_free(ctx->chain, X509_free);
2484 ctx->chain = NULL;
0f113f3e 2485 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
16f8d4eb 2486 memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
0f113f3e 2487}
13938ace 2488
5d7c222d 2489void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
0f113f3e
MC
2490{
2491 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2492}
bbb72003 2493
5d7c222d 2494void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
0f113f3e
MC
2495{
2496 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2497}
5d7c222d 2498
0f113f3e
MC
2499void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
2500 time_t t)
2501{
2502 X509_VERIFY_PARAM_set_time(ctx->param, t);
2503}
bbb72003 2504
8cc86b81 2505X509 *X509_STORE_CTX_get0_cert(const X509_STORE_CTX *ctx)
1060a50b
RL
2506{
2507 return ctx->cert;
2508}
2509
8cc86b81 2510STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(const X509_STORE_CTX *ctx)
1060a50b
RL
2511{
2512 return ctx->untrusted;
2513}
2514
2515void X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2516{
2517 ctx->untrusted = sk;
2518}
2519
2520void X509_STORE_CTX_set0_verified_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2521{
2522 sk_X509_pop_free(ctx->chain, X509_free);
2523 ctx->chain = sk;
2524}
2525
db089ad6 2526void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
f0e0fd51 2527 X509_STORE_CTX_verify_cb verify_cb)
0f113f3e
MC
2528{
2529 ctx->verify_cb = verify_cb;
2530}
db089ad6 2531
8cc86b81 2532X509_STORE_CTX_verify_cb X509_STORE_CTX_get_verify_cb(const X509_STORE_CTX *ctx)
f0e0fd51
RS
2533{
2534 return ctx->verify_cb;
2535}
2536
4a7b3a7b
VD
2537void X509_STORE_CTX_set_verify(X509_STORE_CTX *ctx,
2538 X509_STORE_CTX_verify_fn verify)
2539{
2540 ctx->verify = verify;
2541}
2542
8cc86b81 2543X509_STORE_CTX_verify_fn X509_STORE_CTX_get_verify(const X509_STORE_CTX *ctx)
f0e0fd51 2544{
1060a50b 2545 return ctx->verify;
f0e0fd51
RS
2546}
2547
8cc86b81 2548X509_STORE_CTX_get_issuer_fn X509_STORE_CTX_get_get_issuer(const X509_STORE_CTX *ctx)
f0e0fd51 2549{
1060a50b 2550 return ctx->get_issuer;
f0e0fd51
RS
2551}
2552
8cc86b81
DDO
2553X509_STORE_CTX_check_issued_fn
2554 X509_STORE_CTX_get_check_issued(const X509_STORE_CTX *ctx)
4dba585f 2555{
1060a50b 2556 return ctx->check_issued;
4dba585f
DSH
2557}
2558
8cc86b81
DDO
2559X509_STORE_CTX_check_revocation_fn
2560 X509_STORE_CTX_get_check_revocation(const X509_STORE_CTX *ctx)
f0e0fd51 2561{
1060a50b 2562 return ctx->check_revocation;
f0e0fd51
RS
2563}
2564
8cc86b81 2565X509_STORE_CTX_get_crl_fn X509_STORE_CTX_get_get_crl(const X509_STORE_CTX *ctx)
f0e0fd51 2566{
1060a50b 2567 return ctx->get_crl;
f0e0fd51
RS
2568}
2569
8cc86b81 2570X509_STORE_CTX_check_crl_fn X509_STORE_CTX_get_check_crl(const X509_STORE_CTX *ctx)
f0e0fd51 2571{
1060a50b
RL
2572 return ctx->check_crl;
2573}
2574
8cc86b81 2575X509_STORE_CTX_cert_crl_fn X509_STORE_CTX_get_cert_crl(const X509_STORE_CTX *ctx)
1060a50b
RL
2576{
2577 return ctx->cert_crl;
2578}
2579
8cc86b81
DDO
2580X509_STORE_CTX_check_policy_fn
2581 X509_STORE_CTX_get_check_policy(const X509_STORE_CTX *ctx)
1060a50b
RL
2582{
2583 return ctx->check_policy;
2584}
2585
8cc86b81
DDO
2586X509_STORE_CTX_lookup_certs_fn
2587 X509_STORE_CTX_get_lookup_certs(const X509_STORE_CTX *ctx)
1060a50b
RL
2588{
2589 return ctx->lookup_certs;
2590}
2591
8cc86b81
DDO
2592X509_STORE_CTX_lookup_crls_fn
2593 X509_STORE_CTX_get_lookup_crls(const X509_STORE_CTX *ctx)
1060a50b
RL
2594{
2595 return ctx->lookup_crls;
2596}
2597
8cc86b81 2598X509_STORE_CTX_cleanup_fn X509_STORE_CTX_get_cleanup(const X509_STORE_CTX *ctx)
1060a50b
RL
2599{
2600 return ctx->cleanup;
f0e0fd51
RS
2601}
2602
8cc86b81 2603X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(const X509_STORE_CTX *ctx)
0f113f3e
MC
2604{
2605 return ctx->tree;
2606}
5d7c222d 2607
8cc86b81 2608int X509_STORE_CTX_get_explicit_policy(const X509_STORE_CTX *ctx)
0f113f3e
MC
2609{
2610 return ctx->explicit_policy;
2611}
5d7c222d 2612
8cc86b81 2613int X509_STORE_CTX_get_num_untrusted(const X509_STORE_CTX *ctx)
7f3f41d8 2614{
d9b8b89b 2615 return ctx->num_untrusted;
7f3f41d8
MC
2616}
2617
5d7c222d 2618int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
0f113f3e
MC
2619{
2620 const X509_VERIFY_PARAM *param;
12a765a5 2621
0f113f3e 2622 param = X509_VERIFY_PARAM_lookup(name);
12a765a5 2623 if (param == NULL)
0f113f3e
MC
2624 return 0;
2625 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2626}
5d7c222d 2627
8cc86b81 2628X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(const X509_STORE_CTX *ctx)
0f113f3e
MC
2629{
2630 return ctx->param;
2631}
5d7c222d
DSH
2632
2633void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
0f113f3e 2634{
222561fe 2635 X509_VERIFY_PARAM_free(ctx->param);
0f113f3e
MC
2636 ctx->param = param;
2637}
d9b8b89b 2638
b9aec69a 2639void X509_STORE_CTX_set0_dane(X509_STORE_CTX *ctx, SSL_DANE *dane)
919ba009
VD
2640{
2641 ctx->dane = dane;
2642}
2643
170b7358
VD
2644static unsigned char *dane_i2d(
2645 X509 *cert,
2646 uint8_t selector,
2647 unsigned int *i2dlen)
2648{
2649 unsigned char *buf = NULL;
2650 int len;
2651
2652 /*
2653 * Extract ASN.1 DER form of certificate or public key.
2654 */
2655 switch (selector) {
2656 case DANETLS_SELECTOR_CERT:
2657 len = i2d_X509(cert, &buf);
2658 break;
2659 case DANETLS_SELECTOR_SPKI:
2660 len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &buf);
2661 break;
2662 default:
9311d0c4 2663 ERR_raise(ERR_LIB_X509, X509_R_BAD_SELECTOR);
170b7358
VD
2664 return NULL;
2665 }
2666
2667 if (len < 0 || buf == NULL) {
9311d0c4 2668 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
170b7358
VD
2669 return NULL;
2670 }
2671
2672 *i2dlen = (unsigned int)len;
2673 return buf;
2674}
2675
2676#define DANETLS_NONE 256 /* impossible uint8_t */
2677
2678static int dane_match(X509_STORE_CTX *ctx, X509 *cert, int depth)
2679{
b9aec69a 2680 SSL_DANE *dane = ctx->dane;
170b7358
VD
2681 unsigned usage = DANETLS_NONE;
2682 unsigned selector = DANETLS_NONE;
2683 unsigned ordinal = DANETLS_NONE;
2684 unsigned mtype = DANETLS_NONE;
2685 unsigned char *i2dbuf = NULL;
2686 unsigned int i2dlen = 0;
2687 unsigned char mdbuf[EVP_MAX_MD_SIZE];
2688 unsigned char *cmpbuf = NULL;
2689 unsigned int cmplen = 0;
2690 int i;
2691 int recnum;
2692 int matched = 0;
2693 danetls_record *t = NULL;
2694 uint32_t mask;
2695
2696 mask = (depth == 0) ? DANETLS_EE_MASK : DANETLS_TA_MASK;
2697
2698 /*
2699 * The trust store is not applicable with DANE-TA(2)
2700 */
2701 if (depth >= ctx->num_untrusted)
2702 mask &= DANETLS_PKIX_MASK;
2703
2704 /*
2705 * If we've previously matched a PKIX-?? record, no need to test any
02e112a8 2706 * further PKIX-?? records, it remains to just build the PKIX chain.
170b7358
VD
2707 * Had the match been a DANE-?? record, we'd be done already.
2708 */
2709 if (dane->mdpth >= 0)
2710 mask &= ~DANETLS_PKIX_MASK;
2711
2712 /*-
2713 * https://tools.ietf.org/html/rfc7671#section-5.1
2714 * https://tools.ietf.org/html/rfc7671#section-5.2
2715 * https://tools.ietf.org/html/rfc7671#section-5.3
2716 * https://tools.ietf.org/html/rfc7671#section-5.4
2717 *
2718 * We handle DANE-EE(3) records first as they require no chain building
2719 * and no expiration or hostname checks. We also process digests with
2720 * higher ordinals first and ignore lower priorities except Full(0) which
2721 * is always processed (last). If none match, we then process PKIX-EE(1).
2722 *
2723 * NOTE: This relies on DANE usages sorting before the corresponding PKIX
2724 * usages in SSL_dane_tlsa_add(), and also on descending sorting of digest
2725 * priorities. See twin comment in ssl/ssl_lib.c.
2726 *
2727 * We expect that most TLSA RRsets will have just a single usage, so we
2728 * don't go out of our way to cache multiple selector-specific i2d buffers
2729 * across usages, but if the selector happens to remain the same as switch
2730 * usages, that's OK. Thus, a set of "3 1 1", "3 0 1", "1 1 1", "1 0 1",
2731 * records would result in us generating each of the certificate and public
2732 * key DER forms twice, but more typically we'd just see multiple "3 1 1"
2733 * or multiple "3 0 1" records.
2734 *
2735 * As soon as we find a match at any given depth, we stop, because either
2736 * we've matched a DANE-?? record and the peer is authenticated, or, after
0d4fb843 2737 * exhausting all DANE-?? records, we've matched a PKIX-?? record, which is
170b7358
VD
2738 * sufficient for DANE, and what remains to do is ordinary PKIX validation.
2739 */
2740 recnum = (dane->umask & mask) ? sk_danetls_record_num(dane->trecs) : 0;
2741 for (i = 0; matched == 0 && i < recnum; ++i) {
2742 t = sk_danetls_record_value(dane->trecs, i);
2743 if ((DANETLS_USAGE_BIT(t->usage) & mask) == 0)
2744 continue;
2745 if (t->usage != usage) {
2746 usage = t->usage;
2747
2748 /* Reset digest agility for each usage/selector pair */
2749 mtype = DANETLS_NONE;
2750 ordinal = dane->dctx->mdord[t->mtype];
2751 }
2752 if (t->selector != selector) {
2753 selector = t->selector;
2754
2755 /* Update per-selector state */
2756 OPENSSL_free(i2dbuf);
2757 i2dbuf = dane_i2d(cert, selector, &i2dlen);
2758 if (i2dbuf == NULL)
2759 return -1;
2760
2761 /* Reset digest agility for each usage/selector pair */
2762 mtype = DANETLS_NONE;
2763 ordinal = dane->dctx->mdord[t->mtype];
2764 } else if (t->mtype != DANETLS_MATCHING_FULL) {
2765 /*-
2766 * Digest agility:
2767 *
2768 * <https://tools.ietf.org/html/rfc7671#section-9>
2769 *
2770 * For a fixed selector, after processing all records with the
2771 * highest mtype ordinal, ignore all mtypes with lower ordinals
2772 * other than "Full".
2773 */
2774 if (dane->dctx->mdord[t->mtype] < ordinal)
2775 continue;
2776 }
2777
2778 /*
2779 * Each time we hit a (new selector or) mtype, re-compute the relevant
2780 * digest, more complex caching is not worth the code space.
2781 */
2782 if (t->mtype != mtype) {
2783 const EVP_MD *md = dane->dctx->mdevp[mtype = t->mtype];
2784 cmpbuf = i2dbuf;
2785 cmplen = i2dlen;
2786
2787 if (md != NULL) {
dccd20d1
F
2788 cmpbuf = mdbuf;
2789 if (!EVP_Digest(i2dbuf, i2dlen, cmpbuf, &cmplen, md, 0)) {
2790 matched = -1;
170b7358
VD
2791 break;
2792 }
2793 }
2794 }
2795
2796 /*
2797 * Squirrel away the certificate and depth if we have a match. Any
2798 * DANE match is dispositive, but with PKIX we still need to build a
2799 * full chain.
2800 */
2801 if (cmplen == t->dlen &&
2802 memcmp(cmpbuf, t->data, cmplen) == 0) {
2803 if (DANETLS_USAGE_BIT(usage) & DANETLS_DANE_MASK)
2804 matched = 1;
2805 if (matched || dane->mdpth < 0) {
2806 dane->mdpth = depth;
2807 dane->mtlsa = t;
2808 OPENSSL_free(dane->mcert);
2809 dane->mcert = cert;
2810 X509_up_ref(cert);
2811 }
2812 break;
2813 }
2814 }
2815
2816 /* Clear the one-element DER cache */
2817 OPENSSL_free(i2dbuf);
2818 return matched;
2819}
2820
2821static int check_dane_issuer(X509_STORE_CTX *ctx, int depth)
2822{
b9aec69a 2823 SSL_DANE *dane = ctx->dane;
170b7358
VD
2824 int matched = 0;
2825 X509 *cert;
2826
2827 if (!DANETLS_HAS_TA(dane) || depth == 0)
2828 return X509_TRUST_UNTRUSTED;
2829
2830 /*
ade08735 2831 * Record any DANE trust anchor matches, for the first depth to test, if
170b7358
VD
2832 * there's one at that depth. (This'll be false for length 1 chains looking
2833 * for an exact match for the leaf certificate).
2834 */
2835 cert = sk_X509_value(ctx->chain, depth);
2836 if (cert != NULL && (matched = dane_match(ctx, cert, depth)) < 0)
2837 return X509_TRUST_REJECTED;
2838 if (matched > 0) {
2839 ctx->num_untrusted = depth - 1;
2840 return X509_TRUST_TRUSTED;
2841 }
2842
2843 return X509_TRUST_UNTRUSTED;
2844}
2845
2846static int check_dane_pkeys(X509_STORE_CTX *ctx)
2847{
b9aec69a 2848 SSL_DANE *dane = ctx->dane;
170b7358
VD
2849 danetls_record *t;
2850 int num = ctx->num_untrusted;
2851 X509 *cert = sk_X509_value(ctx->chain, num - 1);
2852 int recnum = sk_danetls_record_num(dane->trecs);
2853 int i;
2854
2855 for (i = 0; i < recnum; ++i) {
2856 t = sk_danetls_record_value(dane->trecs, i);
2857 if (t->usage != DANETLS_USAGE_DANE_TA ||
2858 t->selector != DANETLS_SELECTOR_SPKI ||
2859 t->mtype != DANETLS_MATCHING_FULL ||
6725682d 2860 X509_verify(cert, t->spki) <= 0)
170b7358
VD
2861 continue;
2862
c0a445a9 2863 /* Clear any PKIX-?? matches that failed to extend to a full chain */
170b7358
VD
2864 X509_free(dane->mcert);
2865 dane->mcert = NULL;
2866
2867 /* Record match via a bare TA public key */
2868 ctx->bare_ta_signed = 1;
2869 dane->mdpth = num - 1;
2870 dane->mtlsa = t;
2871
2872 /* Prune any excess chain certificates */
2873 num = sk_X509_num(ctx->chain);
2874 for (; num > ctx->num_untrusted; --num)
2875 X509_free(sk_X509_pop(ctx->chain));
2876
2877 return X509_TRUST_TRUSTED;
2878 }
2879
2880 return X509_TRUST_UNTRUSTED;
2881}
2882
b9aec69a 2883static void dane_reset(SSL_DANE *dane)
170b7358
VD
2884{
2885 /*
2886 * Reset state to verify another chain, or clear after failure.
2887 */
2888 X509_free(dane->mcert);
2889 dane->mcert = NULL;
2890 dane->mtlsa = NULL;
2891 dane->mdpth = -1;
2892 dane->pdpth = -1;
2893}
2894
6e328256
VD
2895static int check_leaf_suiteb(X509_STORE_CTX *ctx, X509 *cert)
2896{
2897 int err = X509_chain_check_suiteb(NULL, cert, NULL, ctx->param->flags);
2898
6e5e118c
DO
2899 CHECK_CB(err != X509_V_OK, ctx, cert, 0, err);
2900 return 1;
6e328256
VD
2901}
2902
170b7358
VD
2903static int dane_verify(X509_STORE_CTX *ctx)
2904{
2905 X509 *cert = ctx->cert;
b9aec69a 2906 SSL_DANE *dane = ctx->dane;
170b7358
VD
2907 int matched;
2908 int done;
2909
2910 dane_reset(dane);
2911
89ff989d
VD
2912 /*-
2913 * When testing the leaf certificate, if we match a DANE-EE(3) record,
2914 * dane_match() returns 1 and we're done. If however we match a PKIX-EE(1)
2915 * record, the match depth and matching TLSA record are recorded, but the
ade08735 2916 * return value is 0, because we still need to find a PKIX trust anchor.
89ff989d
VD
2917 * Therefore, when DANE authentication is enabled (required), we're done
2918 * if:
2919 * + matched < 0, internal error.
2920 * + matched == 1, we matched a DANE-EE(3) record
2921 * + matched == 0, mdepth < 0 (no PKIX-EE match) and there are no
2922 * DANE-TA(2) or PKIX-TA(0) to test.
2923 */
170b7358
VD
2924 matched = dane_match(ctx, ctx->cert, 0);
2925 done = matched != 0 || (!DANETLS_HAS_TA(dane) && dane->mdpth < 0);
2926
2927 if (done)
2928 X509_get_pubkey_parameters(NULL, ctx->chain);
2929
2930 if (matched > 0) {
70dd3c65 2931 /* Callback invoked as needed */
6e328256
VD
2932 if (!check_leaf_suiteb(ctx, cert))
2933 return 0;
5ae4ceb9
VD
2934 /* Callback invoked as needed */
2935 if ((dane->flags & DANE_FLAG_NO_DANE_EE_NAMECHECKS) == 0 &&
2936 !check_id(ctx))
2937 return 0;
70dd3c65 2938 /* Bypass internal_verify(), issue depth 0 success callback */
170b7358
VD
2939 ctx->error_depth = 0;
2940 ctx->current_cert = cert;
6e328256 2941 return ctx->verify_cb(1, ctx);
170b7358
VD
2942 }
2943
2944 if (matched < 0) {
2945 ctx->error_depth = 0;
2946 ctx->current_cert = cert;
2947 ctx->error = X509_V_ERR_OUT_OF_MEM;
2948 return -1;
2949 }
2950
2951 if (done) {
2952 /* Fail early, TA-based success is not possible */
6e328256
VD
2953 if (!check_leaf_suiteb(ctx, cert))
2954 return 0;
70dd3c65 2955 return verify_cb_cert(ctx, cert, 0, X509_V_ERR_DANE_NO_MATCH);
170b7358
VD
2956 }
2957
2958 /*
2959 * Chain verification for usages 0/1/2. TLSA record matching of depth > 0
2960 * certificates happens in-line with building the rest of the chain.
2961 */
2962 return verify_chain(ctx);
2963}
2964
fbb82a60
VD
2965/* Get issuer, without duplicate suppression */
2966static int get_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *cert)
2967{
2968 STACK_OF(X509) *saved_chain = ctx->chain;
2969 int ok;
2970
2971 ctx->chain = NULL;
2972 ok = ctx->get_issuer(issuer, ctx, cert);
2973 ctx->chain = saved_chain;
2974
2975 return ok;
2976}
2977
d9b8b89b
VD
2978static int build_chain(X509_STORE_CTX *ctx)
2979{
b9aec69a 2980 SSL_DANE *dane = ctx->dane;
d9b8b89b
VD
2981 int num = sk_X509_num(ctx->chain);
2982 X509 *cert = sk_X509_value(ctx->chain, num - 1);
0d8dbb52 2983 int self_signed;
d9b8b89b
VD
2984 STACK_OF(X509) *sktmp = NULL;
2985 unsigned int search;
170b7358 2986 int may_trusted = 0;
d9b8b89b
VD
2987 int may_alternate = 0;
2988 int trust = X509_TRUST_UNTRUSTED;
2989 int alt_untrusted = 0;
2990 int depth;
2991 int ok = 0;
2992 int i;
2993
2994 /* Our chain starts with a single untrusted element. */
24664a3b 2995 if (!ossl_assert(num == 1 && ctx->num_untrusted == num)) {
9311d0c4 2996 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
24664a3b
MC
2997 ctx->error = X509_V_ERR_UNSPECIFIED;
2998 return 0;
2999 }
d9b8b89b 3000
6725682d 3001 self_signed = X509_self_signed(cert, 0);
02369787 3002 if (self_signed < 0) {
0c56a648
MC
3003 ctx->error = X509_V_ERR_UNSPECIFIED;
3004 return 0;
3005 }
3006
d9b8b89b
VD
3007#define S_DOUNTRUSTED (1 << 0) /* Search untrusted chain */
3008#define S_DOTRUSTED (1 << 1) /* Search trusted store */
3009#define S_DOALTERNATE (1 << 2) /* Retry with pruned alternate chain */
3010 /*
3011 * Set up search policy, untrusted if possible, trusted-first if enabled.
170b7358
VD
3012 * If we're doing DANE and not doing PKIX-TA/PKIX-EE, we never look in the
3013 * trust_store, otherwise we might look there first. If not trusted-first,
3014 * and alternate chains are not disabled, try building an alternate chain
3015 * if no luck with untrusted first.
d9b8b89b
VD
3016 */
3017 search = (ctx->untrusted != NULL) ? S_DOUNTRUSTED : 0;
170b7358
VD
3018 if (DANETLS_HAS_PKIX(dane) || !DANETLS_HAS_DANE(dane)) {
3019 if (search == 0 || ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
3020 search |= S_DOTRUSTED;
3021 else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS))
3022 may_alternate = 1;
3023 may_trusted = 1;
3024 }
d9b8b89b
VD
3025
3026 /*
3027 * Shallow-copy the stack of untrusted certificates (with TLS, this is
3028 * typically the content of the peer's certificate message) so can make
3029 * multiple passes over it, while free to remove elements as we go.
3030 */
3031 if (ctx->untrusted && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
9311d0c4 3032 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
f3e235ed 3033 ctx->error = X509_V_ERR_OUT_OF_MEM;
d9b8b89b
VD
3034 return 0;
3035 }
3036
69664d6a 3037 /*
ade08735 3038 * If we got any "DANE-TA(2) Cert(0) Full(0)" trust anchors from DNS, add
69664d6a
VD
3039 * them to our working copy of the untrusted certificate stack. Since the
3040 * caller of X509_STORE_CTX_init() may have provided only a leaf cert with
3041 * no corresponding stack of untrusted certificates, we may need to create
3042 * an empty stack first. [ At present only the ssl library provides DANE
3043 * support, and ssl_verify_cert_chain() always provides a non-null stack
3044 * containing at least the leaf certificate, but we must be prepared for
3045 * this to change. ]
3046 */
170b7358 3047 if (DANETLS_ENABLED(dane) && dane->certs != NULL) {
69664d6a 3048 if (sktmp == NULL && (sktmp = sk_X509_new_null()) == NULL) {
9311d0c4 3049 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
f3e235ed 3050 ctx->error = X509_V_ERR_OUT_OF_MEM;
69664d6a
VD
3051 return 0;
3052 }
eeccc237
DDO
3053 if (!X509_add_certs(sktmp, dane->certs, X509_ADD_FLAG_DEFAULT)) {
3054 sk_X509_free(sktmp);
3055 ctx->error = X509_V_ERR_OUT_OF_MEM;
3056 return 0;
170b7358
VD
3057 }
3058 }
3059
d9b8b89b
VD
3060 /*
3061 * Still absurdly large, but arithmetically safe, a lower hard upper bound
3062 * might be reasonable.
3063 */
3064 if (ctx->param->depth > INT_MAX/2)
3065 ctx->param->depth = INT_MAX/2;
3066
3067 /*
ade08735 3068 * Try to extend the chain until we reach an ultimately trusted issuer.
d9b8b89b
VD
3069 * Build chains up to one longer the limit, later fail if we hit the limit,
3070 * with an X509_V_ERR_CERT_CHAIN_TOO_LONG error code.
3071 */
3072 depth = ctx->param->depth + 1;
3073
3074 while (search != 0) {
3075 X509 *x;
3076 X509 *xtmp = NULL;
3077
3078 /*
3079 * Look in the trust store if enabled for first lookup, or we've run
fbb82a60
VD
3080 * out of untrusted issuers and search here is not disabled. When we
3081 * reach the depth limit, we stop extending the chain, if by that point
ade08735 3082 * we've not found a trust anchor, any trusted chain would be too long.
fbb82a60
VD
3083 *
3084 * The error reported to the application verify callback is at the
3085 * maximal valid depth with the current certificate equal to the last
3086 * not ultimately-trusted issuer. For example, with verify_depth = 0,
3087 * the callback will report errors at depth=1 when the immediate issuer
3088 * of the leaf certificate is not a trust anchor. No attempt will be
3089 * made to locate an issuer for that certificate, since such a chain
3090 * would be a-priori too long.
d9b8b89b
VD
3091 */
3092 if ((search & S_DOTRUSTED) != 0) {
d9b8b89b
VD
3093 i = num = sk_X509_num(ctx->chain);
3094 if ((search & S_DOALTERNATE) != 0) {
3095 /*
3096 * As high up the chain as we can, look for an alternative
3097 * trusted issuer of an untrusted certificate that currently
3098 * has an untrusted issuer. We use the alt_untrusted variable
3099 * to track how far up the chain we find the first match. It
3100 * is only if and when we find a match, that we prune the chain
3101 * and reset ctx->num_untrusted to the reduced count of
3102 * untrusted certificates. While we're searching for such a
3103 * match (which may never be found), it is neither safe nor
3104 * wise to preemptively modify either the chain or
3105 * ctx->num_untrusted.
3106 *
3107 * Note, like ctx->num_untrusted, alt_untrusted is a count of
3108 * untrusted certificates, not a "depth".
3109 */
3110 i = alt_untrusted;
3111 }
3112 x = sk_X509_value(ctx->chain, i-1);
3113
fbb82a60 3114 ok = (depth < num) ? 0 : get_issuer(&xtmp, ctx, x);
d9b8b89b
VD
3115
3116 if (ok < 0) {
3117 trust = X509_TRUST_REJECTED;
f3e235ed 3118 ctx->error = X509_V_ERR_STORE_LOOKUP;
d9b8b89b
VD
3119 search = 0;
3120 continue;
3121 }
3122
3123 if (ok > 0) {
3124 /*
3125 * Alternative trusted issuer for a mid-chain untrusted cert?
3126 * Pop the untrusted cert's successors and retry. We might now
3127 * be able to complete a valid chain via the trust store. Note
ade08735
DDO
3128 * that despite the current trust store match we might still
3129 * fail complete the chain to a suitable trust anchor, in which
d9b8b89b
VD
3130 * case we may prune some more untrusted certificates and try
3131 * again. Thus the S_DOALTERNATE bit may yet be turned on
3132 * again with an even shorter untrusted chain!
170b7358
VD
3133 *
3134 * If in the process we threw away our matching PKIX-TA trust
3135 * anchor, reset DANE trust. We might find a suitable trusted
3136 * certificate among the ones from the trust store.
d9b8b89b
VD
3137 */
3138 if ((search & S_DOALTERNATE) != 0) {
02369787 3139 if (!ossl_assert(num > i && i > 0 && !self_signed)) {
9311d0c4 3140 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
24664a3b
MC
3141 X509_free(xtmp);
3142 trust = X509_TRUST_REJECTED;
3143 ctx->error = X509_V_ERR_UNSPECIFIED;
3144 search = 0;
3145 continue;
3146 }
d9b8b89b
VD
3147 search &= ~S_DOALTERNATE;
3148 for (; num > i; --num)
3149 X509_free(sk_X509_pop(ctx->chain));
3150 ctx->num_untrusted = num;
170b7358
VD
3151
3152 if (DANETLS_ENABLED(dane) &&
3153 dane->mdpth >= ctx->num_untrusted) {
3154 dane->mdpth = -1;
3155 X509_free(dane->mcert);
3156 dane->mcert = NULL;
3157 }
3158 if (DANETLS_ENABLED(dane) &&
3159 dane->pdpth >= ctx->num_untrusted)
3160 dane->pdpth = -1;
d9b8b89b
VD
3161 }
3162
3163 /*
3164 * Self-signed untrusted certificates get replaced by their
3165 * trusted matching issuer. Otherwise, grow the chain.
3166 */
02369787 3167 if (!self_signed) {
d9b8b89b
VD
3168 if (!sk_X509_push(ctx->chain, x = xtmp)) {
3169 X509_free(xtmp);
9311d0c4 3170 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
d9b8b89b 3171 trust = X509_TRUST_REJECTED;
f3e235ed 3172 ctx->error = X509_V_ERR_OUT_OF_MEM;
d9b8b89b
VD
3173 search = 0;
3174 continue;
3175 }
6725682d 3176 self_signed = X509_self_signed(x, 0);
02369787 3177 if (self_signed < 0) {
a268ed3a 3178 sk_X509_free(sktmp);
0c56a648
MC
3179 ctx->error = X509_V_ERR_UNSPECIFIED;
3180 return 0;
3181 }
d9b8b89b
VD
3182 } else if (num == ctx->num_untrusted) {
3183 /*
3184 * We have a self-signed certificate that has the same
3185 * subject name (and perhaps keyid and/or serial number) as
ade08735 3186 * a trust anchor. We must have an exact match to avoid
d9b8b89b
VD
3187 * possible impersonation via key substitution etc.
3188 */
3189 if (X509_cmp(x, xtmp) != 0) {
3190 /* Self-signed untrusted mimic. */
3191 X509_free(xtmp);
3192 ok = 0;
3193 } else {
3194 X509_free(x);
3195 ctx->num_untrusted = --num;
3196 (void) sk_X509_set(ctx->chain, num, x = xtmp);
3197 }
3198 }
3199
3200 /*
e99505b4 3201 * We've added a new trusted certificate to the chain, re-check
d9b8b89b
VD
3202 * trust. If not done, and not self-signed look deeper.
3203 * Whether or not we're doing "trusted first", we no longer
3204 * look for untrusted certificates from the peer's chain.
170b7358
VD
3205 *
3206 * At this point ctx->num_trusted and num must reflect the
3207 * correct number of untrusted certificates, since the DANE
3208 * logic in check_trust() depends on distinguishing CAs from
3209 * "the wire" from CAs from the trust store. In particular, the
3210 * certificate at depth "num" should be the new trusted
3211 * certificate with ctx->num_untrusted <= num.
d9b8b89b
VD
3212 */
3213 if (ok) {
24664a3b 3214 if (!ossl_assert(ctx->num_untrusted <= num)) {
9311d0c4 3215 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
24664a3b
MC
3216 trust = X509_TRUST_REJECTED;
3217 ctx->error = X509_V_ERR_UNSPECIFIED;
3218 search = 0;
3219 continue;
3220 }
d9b8b89b
VD
3221 search &= ~S_DOUNTRUSTED;
3222 switch (trust = check_trust(ctx, num)) {
3223 case X509_TRUST_TRUSTED:
3224 case X509_TRUST_REJECTED:
3225 search = 0;
3226 continue;
3227 }
02369787 3228 if (!self_signed)
d9b8b89b
VD
3229 continue;
3230 }
3231 }
3232
3233 /*
3234 * No dispositive decision, and either self-signed or no match, if
3235 * we were doing untrusted-first, and alt-chains are not disabled,
3236 * do that, by repeatedly losing one untrusted element at a time,
3237 * and trying to extend the shorted chain.
3238 */
3239 if ((search & S_DOUNTRUSTED) == 0) {
3240 /* Continue search for a trusted issuer of a shorter chain? */
3241 if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0)
3242 continue;
3243 /* Still no luck and no fallbacks left? */
3244 if (!may_alternate || (search & S_DOALTERNATE) != 0 ||
3245 ctx->num_untrusted < 2)
3246 break;
3247 /* Search for a trusted issuer of a shorter chain */
3248 search |= S_DOALTERNATE;
3249 alt_untrusted = ctx->num_untrusted - 1;
02369787 3250 self_signed = 0;
d9b8b89b
VD
3251 }
3252 }
3253
3254 /*
3255 * Extend chain with peer-provided certificates
3256 */
3257 if ((search & S_DOUNTRUSTED) != 0) {
3258 num = sk_X509_num(ctx->chain);
24664a3b 3259 if (!ossl_assert(num == ctx->num_untrusted)) {
9311d0c4 3260 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
24664a3b
MC
3261 trust = X509_TRUST_REJECTED;
3262 ctx->error = X509_V_ERR_UNSPECIFIED;
3263 search = 0;
3264 continue;
3265 }
d9b8b89b 3266 x = sk_X509_value(ctx->chain, num-1);
d9b8b89b
VD
3267
3268 /*
3269 * Once we run out of untrusted issuers, we stop looking for more
3270 * and start looking only in the trust store if enabled.
3271 */
02369787
DDO
3272 xtmp = (self_signed || depth < num) ? NULL
3273 : find_issuer(ctx, sktmp, x);
d9b8b89b
VD
3274 if (xtmp == NULL) {
3275 search &= ~S_DOUNTRUSTED;
3276 if (may_trusted)
3277 search |= S_DOTRUSTED;
3278 continue;
3279 }
3280
fbb82a60
VD
3281 /* Drop this issuer from future consideration */
3282 (void) sk_X509_delete_ptr(sktmp, xtmp);
3283
e9e7b5df 3284 if (!X509_up_ref(xtmp)) {
9311d0c4 3285 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
e9e7b5df
BE
3286 trust = X509_TRUST_REJECTED;
3287 ctx->error = X509_V_ERR_UNSPECIFIED;
3288 search = 0;
3289 continue;
3290 }
3291
fbb82a60 3292 if (!sk_X509_push(ctx->chain, xtmp)) {
e9e7b5df 3293 X509_free(xtmp);
9311d0c4 3294 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
d9b8b89b 3295 trust = X509_TRUST_REJECTED;
f3e235ed 3296 ctx->error = X509_V_ERR_OUT_OF_MEM;
d9b8b89b
VD
3297 search = 0;
3298 continue;
3299 }
fbb82a60 3300
e9e7b5df 3301 x = xtmp;
d9b8b89b 3302 ++ctx->num_untrusted;
7b424087 3303 self_signed = X509_self_signed(x, 0);
02369787 3304 if (self_signed < 0) {
2a4ad6a5 3305 sk_X509_free(sktmp);
02369787 3306 ctx->error = X509_V_ERR_UNSPECIFIED;
0c56a648
MC
3307 return 0;
3308 }
d9b8b89b 3309
170b7358
VD
3310 /*
3311 * Check for DANE-TA trust of the topmost untrusted certificate.
3312 */
3313 switch (trust = check_dane_issuer(ctx, ctx->num_untrusted - 1)) {
3314 case X509_TRUST_TRUSTED:
3315 case X509_TRUST_REJECTED:
3316 search = 0;
3317 continue;
3318 }
d9b8b89b
VD
3319 }
3320 }
3321 sk_X509_free(sktmp);
3322
3323 /*
170b7358
VD
3324 * Last chance to make a trusted chain, either bare DANE-TA public-key
3325 * signers, or else direct leaf PKIX trust.
d9b8b89b 3326 */
497ecc0d
VD
3327 num = sk_X509_num(ctx->chain);
3328 if (num <= depth) {
170b7358
VD
3329 if (trust == X509_TRUST_UNTRUSTED && DANETLS_HAS_DANE_TA(dane))
3330 trust = check_dane_pkeys(ctx);
497ecc0d
VD
3331 if (trust == X509_TRUST_UNTRUSTED && num == ctx->num_untrusted)
3332 trust = check_trust(ctx, num);
d9b8b89b
VD
3333 }
3334
3335 switch (trust) {
3336 case X509_TRUST_TRUSTED:
3337 return 1;
3338 case X509_TRUST_REJECTED:
70dd3c65 3339 /* Callback already issued */
d9b8b89b
VD
3340 return 0;
3341 case X509_TRUST_UNTRUSTED:
3342 default:
3343 num = sk_X509_num(ctx->chain);
6e5e118c
DO
3344 CHECK_CB(num > depth, ctx, NULL, num-1, X509_V_ERR_CERT_CHAIN_TOO_LONG);
3345 CHECK_CB(DANETLS_ENABLED(dane)
3346 && (!DANETLS_HAS_PKIX(dane) || dane->pdpth >= 0),
3347 ctx, NULL, num-1, X509_V_ERR_DANE_NO_MATCH);
02369787 3348 if (self_signed)
70dd3c65 3349 return verify_cb_cert(ctx, NULL, num-1,
6e5e118c
DO
3350 sk_X509_num(ctx->chain) == 1
3351 ? X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
3352 : X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN);
70dd3c65 3353 return verify_cb_cert(ctx, NULL, num-1,
6e5e118c
DO
3354 ctx->num_untrusted < num
3355 ? X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
3356 : X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY);
d9b8b89b
VD
3357 }
3358}
fbb82a60
VD
3359
3360static const int minbits_table[] = { 80, 112, 128, 192, 256 };
3361static const int NUM_AUTH_LEVELS = OSSL_NELEM(minbits_table);
3362
3363/*
3364 * Check whether the public key of ``cert`` meets the security level of
3365 * ``ctx``.
3366 *
3367 * Returns 1 on success, 0 otherwise.
3368 */
3369static int check_key_level(X509_STORE_CTX *ctx, X509 *cert)
3370{
3371 EVP_PKEY *pkey = X509_get0_pubkey(cert);
3372 int level = ctx->param->auth_level;
3373
baba1545
KG
3374 /*
3375 * At security level zero, return without checking for a supported public
3376 * key type. Some engines support key types not understood outside the
3377 * engine, and we only need to understand the key when enforcing a security
3378 * floor.
3379 */
3380 if (level <= 0)
3381 return 1;
3382
fbb82a60
VD
3383 /* Unsupported or malformed keys are not secure */
3384 if (pkey == NULL)
3385 return 0;
3386
fbb82a60
VD
3387 if (level > NUM_AUTH_LEVELS)
3388 level = NUM_AUTH_LEVELS;
3389
3390 return EVP_PKEY_security_bits(pkey) >= minbits_table[level - 1];
3391}
3392
cccf532f
TM
3393/*
3394 * Check whether the public key of ``cert`` does not use explicit params
3395 * for an elliptic curve.
3396 *
3397 * Returns 1 on success, 0 if check fails, -1 for other errors.
3398 */
3399static int check_curve(X509 *cert)
3400{
3401#ifndef OPENSSL_NO_EC
3402 EVP_PKEY *pkey = X509_get0_pubkey(cert);
3403
3404 /* Unsupported or malformed key */
3405 if (pkey == NULL)
3406 return -1;
3407
3408 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
3409 int ret;
3410
3411 ret = EC_KEY_decoded_from_explicit_params(EVP_PKEY_get0_EC_KEY(pkey));
3412 return ret < 0 ? ret : !ret;
3413 }
3414#endif
3415
3416 return 1;
3417}
3418
fbb82a60
VD
3419/*
3420 * Check whether the signature digest algorithm of ``cert`` meets the security
3421 * level of ``ctx``. Should not be checked for trust anchors (whether
3422 * self-signed or otherwise).
3423 *
3424 * Returns 1 on success, 0 otherwise.
3425 */
3426static int check_sig_level(X509_STORE_CTX *ctx, X509 *cert)
3427{
fbb82a60
VD
3428 int secbits = -1;
3429 int level = ctx->param->auth_level;
3430
3431 if (level <= 0)
3432 return 1;
3433 if (level > NUM_AUTH_LEVELS)
3434 level = NUM_AUTH_LEVELS;
3435
c3c8823c
DSH
3436 if (!X509_get_signature_info(cert, NULL, NULL, &secbits, NULL))
3437 return 0;
fbb82a60
VD
3438
3439 return secbits >= minbits_table[level - 1];
3440}