Coverage Report

Created: 2026-02-14 07:20

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