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