Coverage Report

Created: 2026-04-09 06:50

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