Coverage Report

Created: 2025-12-31 06:58

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