Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/x509/x_crl.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include <openssl/asn1t.h>
12
#include <openssl/x509.h>
13
#include "crypto/x509.h"
14
#include <openssl/x509v3.h>
15
#include "x509_local.h"
16
17
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
18
    const X509_REVOKED *const *b);
19
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
20
21
ASN1_SEQUENCE(X509_REVOKED) = {
22
    ASN1_EMBED(X509_REVOKED, serialNumber, ASN1_INTEGER),
23
    ASN1_SIMPLE(X509_REVOKED, revocationDate, ASN1_TIME),
24
    ASN1_SEQUENCE_OF_OPT(X509_REVOKED, extensions, X509_EXTENSION)
25
1.01M
} ASN1_SEQUENCE_END(X509_REVOKED)
26
1.01M
27
1.01M
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
28
1.01M
static int def_crl_lookup(X509_CRL *crl,
29
1.01M
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
30
1.01M
    const X509_NAME *issuer);
31
1.01M
32
1.01M
static X509_CRL_METHOD int_crl_meth = {
33
1.01M
    0,
34
1.01M
    0, 0,
35
1.01M
    def_crl_lookup,
36
1.01M
    def_crl_verify
37
1.01M
};
38
1.01M
39
1.01M
static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
40
1.01M
41
1.01M
/*
42
1.01M
 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
43
1.01M
 * the original encoding the signature won't be affected by reordering of the
44
1.01M
 * revoked field.
45
1.01M
 */
46
1.01M
static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
47
1.01M
    void *exarg)
48
1.64M
{
49
1.64M
    X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
50
51
1.64M
    if (!a || !a->revoked)
52
1.59M
        return 1;
53
54.6k
    switch (operation) {
54
        /*
55
         * Just set cmp function here. We don't sort because that would
56
         * affect the output of X509_CRL_print().
57
         */
58
26.0k
    case ASN1_OP_D2I_POST:
59
26.0k
        (void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
60
26.0k
        break;
61
54.6k
    }
62
54.6k
    return 1;
63
54.6k
}
64
65
ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
66
    ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
67
    ASN1_EMBED(X509_CRL_INFO, sig_alg, X509_ALGOR),
68
    ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
69
    ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
70
    ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
71
    ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
72
    ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
73
} ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
74
75
/*
76
 * Set CRL entry issuer according to CRL certificate issuer extension. Check
77
 * for unhandled critical CRL entry extensions.
78
 */
79
80
static int crl_set_issuers(X509_CRL *crl)
81
65.0k
{
82
65.0k
    int i, j;
83
65.0k
    GENERAL_NAMES *most_recent_issuer, *gtmp;
84
65.0k
    STACK_OF(X509_REVOKED) *revoked;
85
86
    /*
87
     * The CRL's nextUpdate field is optional, but we will still verify
88
     * lastUpdate and treat missing or invalid values as a failure.
89
     */
90
65.0k
    if (crl->crl.lastUpdate == NULL) {
91
0
        crl->flags |= EXFLAG_INVALID;
92
0
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TIME_VALUE,
93
0
            "lastUpdate in CRL is not well-formed");
94
0
        return 0;
95
0
    }
96
97
65.0k
    revoked = X509_CRL_get_REVOKED(crl);
98
99
    /*
100
     * If this extension is not present on the first entry in an indirect CRL,
101
     * the certificate issuer defaults to the CRL issuer. Subsequent entries in
102
     * an indirect CRL, if this extension is not present, the certificate issuer
103
     * for the entry is the same as that for the preceding entry.
104
     * https://datatracker.ietf.org/doc/html/rfc5280#section-5.3.3
105
     */
106
65.0k
    most_recent_issuer = NULL;
107
108
124k
    for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
109
61.2k
        X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
110
61.2k
        STACK_OF(X509_EXTENSION) *exts;
111
61.2k
        ASN1_ENUMERATED *reason;
112
61.2k
        X509_EXTENSION *ext;
113
61.2k
        const ASN1_TIME *rev_date;
114
61.2k
        ASN1_GENERALIZEDTIME *inv_date;
115
61.2k
        int nid;
116
117
        /*
118
         * The revocation date is a mandatory attribute. If we get NULL here, it
119
         * means the validation has failed and an error has been pushed onto the
120
         * error stack.
121
         */
122
61.2k
        if ((rev_date = X509_REVOKED_get0_revocationDate(rev)) == NULL) {
123
0
            crl->flags |= EXFLAG_INVALID;
124
0
            ERR_raise_data(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TIME_VALUE,
125
0
                "revocationDate in CRL is not well-formed");
126
0
            return 0;
127
0
        }
128
129
61.2k
        gtmp = X509_REVOKED_get_ext_d2i(rev, NID_certificate_issuer, &j, NULL);
130
61.2k
        if (gtmp == NULL && j != -1) {
131
646
            crl->flags |= EXFLAG_INVALID;
132
646
            return 1;
133
646
        }
134
135
60.6k
        if (gtmp != NULL) {
136
            /*
137
             * Validation to ensure Certificate Issuer extensions in CRL
138
             * entries only appear when the Indirect CRL flag is TRUE in the
139
             * Issuing Distribution Point (IDP) extension, as required by
140
             * https://datatracker.ietf.org/doc/html/rfc5280#section-5.3.3
141
             */
142
158
            if (crl->idp == NULL || !crl->idp->indirectCRL) {
143
27
                crl->flags |= EXFLAG_INVALID;
144
27
                ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_VALUE,
145
27
                    "CRL Certificate Issuer extension requires Indirect CRL flag to be set");
146
27
                GENERAL_NAMES_free(gtmp);
147
27
                return 0;
148
27
            }
149
150
131
            if (crl->issuers == NULL) {
151
36
                crl->issuers = sk_GENERAL_NAMES_new_null();
152
36
                if (crl->issuers == NULL) {
153
0
                    GENERAL_NAMES_free(gtmp);
154
0
                    return 0;
155
0
                }
156
36
            }
157
131
            if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp)) {
158
0
                GENERAL_NAMES_free(gtmp);
159
0
                return 0;
160
0
            }
161
131
            most_recent_issuer = gtmp;
162
131
        }
163
60.5k
        rev->issuer = most_recent_issuer;
164
165
60.5k
        reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
166
60.5k
        if (reason == NULL && j != -1) {
167
676
            crl->flags |= EXFLAG_INVALID;
168
676
            return 1;
169
676
        }
170
171
59.9k
        if (reason != NULL) {
172
10.3k
            rev->reason = ASN1_ENUMERATED_get(reason);
173
10.3k
            ASN1_ENUMERATED_free(reason);
174
10.3k
        } else
175
49.5k
            rev->reason = CRL_REASON_NONE;
176
177
        /* Check for critical CRL entry extensions and validate time. */
178
59.9k
        exts = rev->extensions;
179
180
80.3k
        for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
181
20.4k
            ext = sk_X509_EXTENSION_value(exts, j);
182
20.4k
            nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
183
184
            /*
185
             * If we obtain the extension but the time is NULL, it means that
186
             * the validation has failed and the reason will be pushed onto the
187
             * error stack.
188
             */
189
20.4k
            if (nid == NID_invalidity_date) {
190
1.71k
                if ((inv_date = X509V3_EXT_d2i(ext)) == NULL) {
191
52
                    crl->flags |= EXFLAG_INVALID;
192
52
                    ERR_raise_data(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TIME_VALUE,
193
52
                        "invalidityDate in CRL is not well-formed");
194
52
                    return 0;
195
52
                }
196
1.66k
                ASN1_GENERALIZEDTIME_free(inv_date);
197
1.66k
            }
198
20.4k
            if (X509_EXTENSION_get_critical(ext)) {
199
1.38k
                if (nid == NID_certificate_issuer)
200
123
                    continue;
201
1.26k
                crl->flags |= EXFLAG_CRITICAL;
202
1.26k
            }
203
20.4k
        }
204
59.9k
    }
205
206
63.6k
    return 1;
207
65.0k
}
208
209
/*
210
 * The X509_CRL structure needs a bit of customisation. Cache some extensions
211
 * and hash of the whole CRL or set EXFLAG_NO_FINGERPRINT if this fails.
212
 */
213
static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
214
    void *exarg)
215
809k
{
216
809k
    X509_CRL *crl = (X509_CRL *)*pval;
217
809k
    STACK_OF(X509_EXTENSION) *exts;
218
809k
    X509_EXTENSION *ext;
219
809k
    int idx, i;
220
221
809k
    switch (operation) {
222
97.9k
    case ASN1_OP_D2I_PRE:
223
97.9k
        if (crl->meth->crl_free) {
224
0
            if (!crl->meth->crl_free(crl))
225
0
                return 0;
226
0
        }
227
97.9k
        AUTHORITY_KEYID_free(crl->akid);
228
97.9k
        ISSUING_DIST_POINT_free(crl->idp);
229
97.9k
        ASN1_INTEGER_free(crl->crl_number);
230
97.9k
        ASN1_INTEGER_free(crl->base_crl_number);
231
97.9k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
232
        /* fall through */
233
234
195k
    case ASN1_OP_NEW_POST:
235
195k
        crl->idp = NULL;
236
195k
        crl->akid = NULL;
237
195k
        crl->flags = 0;
238
195k
        crl->idp_flags = 0;
239
195k
        crl->idp_reasons = CRLDP_ALL_REASONS;
240
195k
        crl->meth = default_crl_method;
241
195k
        crl->meth_data = NULL;
242
195k
        crl->issuers = NULL;
243
195k
        crl->crl_number = NULL;
244
195k
        crl->base_crl_number = NULL;
245
195k
        break;
246
247
65.4k
    case ASN1_OP_D2I_POST:
248
65.4k
        if (!X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL))
249
0
            crl->flags |= EXFLAG_NO_FINGERPRINT;
250
65.4k
        crl->idp = X509_CRL_get_ext_d2i(crl, NID_issuing_distribution_point, &i, NULL);
251
65.4k
        if (crl->idp == NULL && i != -1) {
252
219
            ERR_raise_data(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OBJECT,
253
219
                "CRL: malformed CRL issuing distribution point");
254
219
            return 0;
255
219
        }
256
65.2k
        if (crl->idp != NULL) {
257
8.91k
            if (!setup_idp(crl, crl->idp))
258
656
                crl->flags |= EXFLAG_INVALID;
259
56.3k
        } else if (i != -1) {
260
0
            crl->flags |= EXFLAG_INVALID;
261
0
        }
262
263
65.2k
        crl->akid = X509_CRL_get_ext_d2i(crl,
264
65.2k
            NID_authority_key_identifier, &i,
265
65.2k
            NULL);
266
65.2k
        if (crl->akid == NULL && i != -1)
267
19.6k
            crl->flags |= EXFLAG_INVALID;
268
269
65.2k
        crl->crl_number = X509_CRL_get_ext_d2i(crl, NID_crl_number, &i, NULL);
270
65.2k
        if (crl->crl_number == NULL && i != -1) {
271
112
            ERR_raise_data(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OBJECT,
272
112
                "CRL: malformed CRL number extension");
273
112
            return 0;
274
112
        }
275
65.1k
        crl->base_crl_number = X509_CRL_get_ext_d2i(crl, NID_delta_crl, &i, NULL);
276
65.1k
        if (crl->base_crl_number == NULL && i != -1) {
277
67
            ERR_raise_data(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OBJECT,
278
67
                "CRL: malformed Delta CRL Indicator");
279
67
            return 0;
280
67
        }
281
        /* Delta CRLs must have CRL number */
282
65.0k
        if (crl->base_crl_number && !crl->crl_number)
283
274
            crl->flags |= EXFLAG_INVALID;
284
        /*
285
         * See if we have any unhandled critical CRL extensions and indicate
286
         * this in a flag. We only currently handle IDP so anything else
287
         * critical sets the flag. This code accesses the X509_CRL structure
288
         * directly: applications shouldn't do this.
289
         */
290
291
65.0k
        exts = crl->crl.extensions;
292
293
562k
        for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
294
498k
            int nid;
295
498k
            ext = sk_X509_EXTENSION_value(exts, idx);
296
498k
            nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
297
498k
            if (nid == NID_freshest_crl)
298
110k
                crl->flags |= EXFLAG_FRESHEST;
299
498k
            if (X509_EXTENSION_get_critical(ext)) {
300
                /* We handle IDP and deltas */
301
3.48k
                if ((nid == NID_issuing_distribution_point)
302
2.45k
                    || (nid == NID_authority_key_identifier)
303
1.62k
                    || (nid == NID_delta_crl))
304
1.88k
                    continue;
305
1.60k
                crl->flags |= EXFLAG_CRITICAL;
306
1.60k
                break;
307
3.48k
            }
308
498k
        }
309
310
65.0k
        if (!crl_set_issuers(crl))
311
79
            return 0;
312
313
64.9k
        if (crl->meth->crl_init) {
314
0
            if (crl->meth->crl_init(crl) == 0)
315
0
                return 0;
316
0
        }
317
318
64.9k
        crl->flags |= EXFLAG_SET;
319
64.9k
        break;
320
321
97.9k
    case ASN1_OP_FREE_POST:
322
97.9k
        if (crl->meth != NULL && crl->meth->crl_free != NULL) {
323
0
            if (!crl->meth->crl_free(crl))
324
0
                return 0;
325
0
        }
326
97.9k
        AUTHORITY_KEYID_free(crl->akid);
327
97.9k
        ISSUING_DIST_POINT_free(crl->idp);
328
97.9k
        ASN1_INTEGER_free(crl->crl_number);
329
97.9k
        ASN1_INTEGER_free(crl->base_crl_number);
330
97.9k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
331
97.9k
        OPENSSL_free(crl->propq);
332
97.9k
        break;
333
0
    case ASN1_OP_DUP_POST: {
334
0
        X509_CRL *old = exarg;
335
336
0
        if (!ossl_x509_crl_set0_libctx(crl, old->libctx, old->propq))
337
0
            return 0;
338
0
    } break;
339
809k
    }
340
809k
    return 1;
341
809k
}
342
343
/* Convert IDP into a more convenient form */
344
345
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
346
21.0k
{
347
21.0k
    int idp_only = 0;
348
21.0k
    int ret = 0;
349
350
    /* Set various flags according to IDP */
351
21.0k
    crl->idp_flags |= IDP_PRESENT;
352
21.0k
    if (idp->onlyuser > 0) {
353
343
        idp_only++;
354
343
        crl->idp_flags |= IDP_ONLYUSER;
355
343
    }
356
21.0k
    if (idp->onlyCA > 0) {
357
2.25k
        idp_only++;
358
2.25k
        crl->idp_flags |= IDP_ONLYCA;
359
2.25k
    }
360
21.0k
    if (idp->onlyattr > 0) {
361
584
        idp_only++;
362
584
        crl->idp_flags |= IDP_ONLYATTR;
363
584
    }
364
365
21.0k
    if (idp_only > 1)
366
210
        crl->idp_flags |= IDP_INVALID;
367
368
21.0k
    if (idp->indirectCRL > 0)
369
181
        crl->idp_flags |= IDP_INDIRECT;
370
371
21.0k
    if (idp->onlysomereasons) {
372
1.97k
        crl->idp_flags |= IDP_REASONS;
373
1.97k
        if (idp->onlysomereasons->length > 0)
374
1.28k
            crl->idp_reasons = idp->onlysomereasons->data[0];
375
1.97k
        if (idp->onlysomereasons->length > 1)
376
962
            crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
377
1.97k
        crl->idp_reasons &= CRLDP_ALL_REASONS;
378
1.97k
    }
379
380
21.0k
    ret = DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
381
382
    /*
383
     * RFC5280 specifies that if onlyContainsUserCerts, onlyContainsCACerts,
384
     * indirectCRL, and OnlyContainsAttributeCerts are all FALSE, there must
385
     * be either a distributionPoint field or an onlySomeReasons field present.
386
     */
387
21.0k
    if (crl->idp_flags == IDP_PRESENT && idp->distpoint == NULL)
388
2.59k
        crl->idp_flags |= IDP_INVALID;
389
390
21.0k
    return ret;
391
21.0k
}
392
393
ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
394
    ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
395
    ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
396
    ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
397
} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
398
399
IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
400
401
IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
402
403
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
404
405
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
406
407
IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
408
409
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
410
    const X509_REVOKED *const *b)
411
22
{
412
22
    return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
413
22
        (ASN1_STRING *)&(*b)->serialNumber));
414
22
}
415
416
X509_CRL *X509_CRL_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
417
0
{
418
0
    X509_CRL *crl = NULL;
419
420
0
    crl = (X509_CRL *)ASN1_item_new((X509_CRL_it()));
421
0
    if (!ossl_x509_crl_set0_libctx(crl, libctx, propq)) {
422
0
        X509_CRL_free(crl);
423
0
        crl = NULL;
424
0
    }
425
0
    return crl;
426
0
}
427
428
int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
429
0
{
430
0
    X509_CRL_INFO *inf;
431
432
0
    inf = &crl->crl;
433
0
    if (inf->revoked == NULL)
434
0
        inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
435
0
    if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
436
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
437
0
        return 0;
438
0
    }
439
0
    inf->enc.modified = 1;
440
0
    return 1;
441
0
}
442
443
int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
444
2.98k
{
445
2.98k
    if (crl->meth->crl_verify)
446
2.98k
        return crl->meth->crl_verify(crl, r);
447
0
    return 0;
448
2.98k
}
449
450
int X509_CRL_get0_by_serial(X509_CRL *crl,
451
    X509_REVOKED **ret, const ASN1_INTEGER *serial)
452
0
{
453
0
    if (crl->meth->crl_lookup)
454
0
        return crl->meth->crl_lookup(crl, ret, serial, NULL);
455
0
    return 0;
456
0
}
457
458
int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, const X509 *x)
459
3.57k
{
460
3.57k
    if (crl->meth->crl_lookup)
461
3.57k
        return crl->meth->crl_lookup(crl, ret,
462
3.57k
            X509_get0_serialNumber(x),
463
3.57k
            X509_get_issuer_name(x));
464
0
    return 0;
465
3.57k
}
466
467
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
468
2.98k
{
469
2.98k
    if (X509_ALGOR_cmp(&crl->sig_alg, &crl->crl.sig_alg) != 0) {
470
1.27k
        ERR_raise(ERR_LIB_X509, X509_R_CRL_SIGNATURE_ALGORITHM_MISMATCH);
471
1.27k
        return 0;
472
1.27k
    }
473
1.70k
    return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_CRL_INFO),
474
1.70k
        &crl->sig_alg, &crl->signature, &crl->crl, NULL,
475
1.70k
        r, crl->libctx, crl->propq);
476
2.98k
}
477
478
static int crl_revoked_issuer_match(X509_CRL *crl, const X509_NAME *nm,
479
    X509_REVOKED *rev)
480
1
{
481
1
    int i;
482
483
1
    if (!rev->issuer) {
484
1
        if (!nm)
485
0
            return 1;
486
1
        if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
487
1
            return 1;
488
0
        return 0;
489
1
    }
490
491
0
    if (!nm)
492
0
        nm = X509_CRL_get_issuer(crl);
493
494
0
    for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
495
0
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
496
0
        if (gen->type != GEN_DIRNAME)
497
0
            continue;
498
0
        if (!X509_NAME_cmp(nm, gen->d.directoryName))
499
0
            return 1;
500
0
    }
501
0
    return 0;
502
0
}
503
504
static int def_crl_lookup(X509_CRL *crl,
505
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
506
    const X509_NAME *issuer)
507
3.57k
{
508
3.57k
    X509_REVOKED rtmp, *rev;
509
3.57k
    int idx, num;
510
511
3.57k
    if (crl->crl.revoked == NULL)
512
3.54k
        return 0;
513
514
    /*
515
     * Sort revoked into serial number order if not already sorted. Do this
516
     * under a lock to avoid race condition.
517
     */
518
34
    if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
519
30
        if (!CRYPTO_THREAD_write_lock(crl->lock))
520
0
            return 0;
521
30
        sk_X509_REVOKED_sort(crl->crl.revoked);
522
30
        CRYPTO_THREAD_unlock(crl->lock);
523
30
    }
524
34
    rtmp.serialNumber = *serial;
525
34
    idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
526
34
    if (idx < 0)
527
33
        return 0;
528
    /* Need to look for matching name */
529
1
    for (num = sk_X509_REVOKED_num(crl->crl.revoked); idx < num; idx++) {
530
1
        rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
531
1
        if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
532
0
            return 0;
533
1
        if (crl_revoked_issuer_match(crl, issuer, rev)) {
534
1
            if (ret)
535
1
                *ret = rev;
536
1
            if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
537
0
                return 2;
538
1
            return 1;
539
1
        }
540
1
    }
541
0
    return 0;
542
1
}
543
544
void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
545
0
{
546
0
    if (meth == NULL)
547
0
        default_crl_method = &int_crl_meth;
548
0
    else
549
0
        default_crl_method = meth;
550
0
}
551
552
X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init)(X509_CRL *crl),
553
    int (*crl_free)(X509_CRL *crl),
554
    int (*crl_lookup)(X509_CRL *crl,
555
        X509_REVOKED **ret,
556
        const ASN1_INTEGER *ser,
557
        const X509_NAME *issuer),
558
    int (*crl_verify)(X509_CRL *crl,
559
        EVP_PKEY *pk))
560
0
{
561
0
    X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
562
563
0
    if (m == NULL)
564
0
        return NULL;
565
0
    m->crl_init = crl_init;
566
0
    m->crl_free = crl_free;
567
0
    m->crl_lookup = crl_lookup;
568
0
    m->crl_verify = crl_verify;
569
0
    m->flags = X509_CRL_METHOD_DYNAMIC;
570
0
    return m;
571
0
}
572
573
void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
574
0
{
575
0
    if (m == NULL || !(m->flags & X509_CRL_METHOD_DYNAMIC))
576
0
        return;
577
0
    OPENSSL_free(m);
578
0
}
579
580
void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
581
0
{
582
0
    crl->meth_data = dat;
583
0
}
584
585
void *X509_CRL_get_meth_data(X509_CRL *crl)
586
0
{
587
0
    return crl->meth_data;
588
0
}
589
590
int ossl_x509_crl_set0_libctx(X509_CRL *x, OSSL_LIB_CTX *libctx,
591
    const char *propq)
592
0
{
593
0
    if (x != NULL) {
594
0
        x->libctx = libctx;
595
0
        OPENSSL_free(x->propq);
596
0
        x->propq = NULL;
597
0
        if (propq != NULL) {
598
0
            x->propq = OPENSSL_strdup(propq);
599
0
            if (x->propq == NULL)
600
0
                return 0;
601
0
        }
602
0
    }
603
0
    return 1;
604
0
}