Coverage Report

Created: 2026-09-12 06:55

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