Coverage Report

Created: 2018-08-29 13:53

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