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