Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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 "internal/cryptlib.h"
12
#include <openssl/asn1t.h>
13
#include <openssl/x509.h>
14
#include "crypto/x509.h"
15
#include <openssl/x509v3.h>
16
#include "x509_local.h"
17
18
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
19
    const X509_REVOKED *const *b);
20
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
21
22
ASN1_SEQUENCE(X509_REVOKED) = {
23
    ASN1_EMBED(X509_REVOKED, serialNumber, ASN1_INTEGER),
24
    ASN1_SIMPLE(X509_REVOKED, revocationDate, ASN1_TIME),
25
    ASN1_SEQUENCE_OF_OPT(X509_REVOKED, extensions, X509_EXTENSION)
26
838k
} ASN1_SEQUENCE_END(X509_REVOKED)
27
838k
28
838k
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
29
838k
static int def_crl_lookup(X509_CRL *crl,
30
838k
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
31
838k
    const X509_NAME *issuer);
32
838k
33
838k
static X509_CRL_METHOD int_crl_meth = {
34
838k
    0,
35
838k
    0, 0,
36
838k
    def_crl_lookup,
37
838k
    def_crl_verify
38
838k
};
39
838k
40
838k
static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
41
838k
42
838k
/*
43
838k
 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
44
838k
 * the original encoding the signature won't be affected by reordering of the
45
838k
 * revoked field.
46
838k
 */
47
838k
static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
48
838k
    void *exarg)
49
1.32M
{
50
1.32M
    X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
51
52
1.32M
    if (!a || !a->revoked)
53
1.27M
        return 1;
54
47.4k
    switch (operation) {
55
        /*
56
         * Just set cmp function here. We don't sort because that would
57
         * affect the output of X509_CRL_print().
58
         */
59
22.5k
    case ASN1_OP_D2I_POST:
60
22.5k
        (void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
61
22.5k
        break;
62
47.4k
    }
63
47.4k
    return 1;
64
47.4k
}
65
66
ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
67
    ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
68
    ASN1_EMBED(X509_CRL_INFO, sig_alg, X509_ALGOR),
69
    ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
70
    ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
71
    ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
72
    ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
73
    ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
74
} ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
75
76
/*
77
 * Set CRL entry issuer according to CRL certificate issuer extension. Check
78
 * for unhandled critical CRL entry extensions.
79
 */
80
81
static int crl_set_issuers(X509_CRL *crl)
82
111k
{
83
84
111k
    int i, j;
85
111k
    GENERAL_NAMES *gens, *gtmp;
86
111k
    STACK_OF(X509_REVOKED) *revoked;
87
88
111k
    revoked = X509_CRL_get_REVOKED(crl);
89
90
111k
    gens = NULL;
91
266k
    for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
92
161k
        X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
93
161k
        STACK_OF(X509_EXTENSION) *exts;
94
161k
        ASN1_ENUMERATED *reason;
95
161k
        X509_EXTENSION *ext;
96
97
161k
        gtmp = X509_REVOKED_get_ext_d2i(rev,
98
161k
            NID_certificate_issuer, &j, NULL);
99
161k
        if (gtmp == NULL && j != -1) {
100
1.33k
            crl->flags |= EXFLAG_INVALID;
101
1.33k
            return 1;
102
1.33k
        }
103
104
160k
        if (gtmp != NULL) {
105
16.9k
            if (crl->issuers == NULL) {
106
2.55k
                crl->issuers = sk_GENERAL_NAMES_new_null();
107
2.55k
                if (crl->issuers == NULL) {
108
0
                    GENERAL_NAMES_free(gtmp);
109
0
                    return 0;
110
0
                }
111
2.55k
            }
112
16.9k
            if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp)) {
113
0
                GENERAL_NAMES_free(gtmp);
114
0
                return 0;
115
0
            }
116
16.9k
            gens = gtmp;
117
16.9k
        }
118
160k
        rev->issuer = gens;
119
120
160k
        reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
121
160k
        if (reason == NULL && j != -1) {
122
5.29k
            crl->flags |= EXFLAG_INVALID;
123
5.29k
            return 1;
124
5.29k
        }
125
126
155k
        if (reason != NULL) {
127
59.2k
            rev->reason = ASN1_ENUMERATED_get(reason);
128
59.2k
            ASN1_ENUMERATED_free(reason);
129
59.2k
        } else
130
96.0k
            rev->reason = CRL_REASON_NONE;
131
132
        /* Check for critical CRL entry extensions */
133
134
155k
        exts = rev->extensions;
135
136
273k
        for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
137
120k
            ext = sk_X509_EXTENSION_value(exts, j);
138
120k
            if (X509_EXTENSION_get_critical(ext)) {
139
2.33k
                if (OBJ_obj2nid(X509_EXTENSION_get_object(ext))
140
2.33k
                    == NID_certificate_issuer)
141
261
                    continue;
142
2.07k
                crl->flags |= EXFLAG_CRITICAL;
143
2.07k
                break;
144
2.33k
            }
145
120k
        }
146
155k
    }
147
148
104k
    return 1;
149
111k
}
150
151
/*
152
 * The X509_CRL structure needs a bit of customisation. Cache some extensions
153
 * and hash of the whole CRL or set EXFLAG_NO_FINGERPRINT if this fails.
154
 */
155
static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
156
    void *exarg)
157
1.25M
{
158
1.25M
    X509_CRL *crl = (X509_CRL *)*pval;
159
1.25M
    STACK_OF(X509_EXTENSION) *exts;
160
1.25M
    X509_EXTENSION *ext;
161
1.25M
    int idx, i;
162
163
1.25M
    switch (operation) {
164
146k
    case ASN1_OP_D2I_PRE:
165
146k
        if (crl->meth->crl_free) {
166
0
            if (!crl->meth->crl_free(crl))
167
0
                return 0;
168
0
        }
169
146k
        AUTHORITY_KEYID_free(crl->akid);
170
146k
        ISSUING_DIST_POINT_free(crl->idp);
171
146k
        ASN1_INTEGER_free(crl->crl_number);
172
146k
        ASN1_INTEGER_free(crl->base_crl_number);
173
146k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
174
        /* fall through */
175
176
293k
    case ASN1_OP_NEW_POST:
177
293k
        crl->idp = NULL;
178
293k
        crl->akid = NULL;
179
293k
        crl->flags = 0;
180
293k
        crl->idp_flags = 0;
181
293k
        crl->idp_reasons = CRLDP_ALL_REASONS;
182
293k
        crl->meth = default_crl_method;
183
293k
        crl->meth_data = NULL;
184
293k
        crl->issuers = NULL;
185
293k
        crl->crl_number = NULL;
186
293k
        crl->base_crl_number = NULL;
187
293k
        break;
188
189
111k
    case ASN1_OP_D2I_POST:
190
111k
        if (!X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL))
191
0
            crl->flags |= EXFLAG_NO_FINGERPRINT;
192
111k
        crl->idp = X509_CRL_get_ext_d2i(crl,
193
111k
            NID_issuing_distribution_point, &i,
194
111k
            NULL);
195
111k
        if (crl->idp != NULL) {
196
17.6k
            if (!setup_idp(crl, crl->idp))
197
750
                crl->flags |= EXFLAG_INVALID;
198
93.5k
        } else if (i != -1) {
199
36.1k
            crl->flags |= EXFLAG_INVALID;
200
36.1k
        }
201
202
111k
        crl->akid = X509_CRL_get_ext_d2i(crl,
203
111k
            NID_authority_key_identifier, &i,
204
111k
            NULL);
205
111k
        if (crl->akid == NULL && i != -1)
206
22.4k
            crl->flags |= EXFLAG_INVALID;
207
208
111k
        crl->crl_number = X509_CRL_get_ext_d2i(crl,
209
111k
            NID_crl_number, &i, NULL);
210
111k
        if (crl->crl_number == NULL && i != -1)
211
10.5k
            crl->flags |= EXFLAG_INVALID;
212
213
111k
        crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
214
111k
            NID_delta_crl, &i,
215
111k
            NULL);
216
111k
        if (crl->base_crl_number == NULL && i != -1)
217
5.78k
            crl->flags |= EXFLAG_INVALID;
218
        /* Delta CRLs must have CRL number */
219
111k
        if (crl->base_crl_number && !crl->crl_number)
220
710
            crl->flags |= EXFLAG_INVALID;
221
222
        /*
223
         * See if we have any unhandled critical CRL extensions and indicate
224
         * this in a flag. We only currently handle IDP so anything else
225
         * critical sets the flag. This code accesses the X509_CRL structure
226
         * directly: applications shouldn't do this.
227
         */
228
229
111k
        exts = crl->crl.extensions;
230
231
774k
        for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
232
667k
            int nid;
233
667k
            ext = sk_X509_EXTENSION_value(exts, idx);
234
667k
            nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
235
667k
            if (nid == NID_freshest_crl)
236
19.6k
                crl->flags |= EXFLAG_FRESHEST;
237
667k
            if (X509_EXTENSION_get_critical(ext)) {
238
                /* We handle IDP and deltas */
239
28.7k
                if ((nid == NID_issuing_distribution_point)
240
12.4k
                    || (nid == NID_authority_key_identifier)
241
5.99k
                    || (nid == NID_delta_crl))
242
24.5k
                    continue;
243
4.21k
                crl->flags |= EXFLAG_CRITICAL;
244
4.21k
                break;
245
28.7k
            }
246
667k
        }
247
248
111k
        if (!crl_set_issuers(crl))
249
0
            return 0;
250
251
111k
        if (crl->meth->crl_init) {
252
0
            if (crl->meth->crl_init(crl) == 0)
253
0
                return 0;
254
0
        }
255
256
111k
        crl->flags |= EXFLAG_SET;
257
111k
        break;
258
259
146k
    case ASN1_OP_FREE_POST:
260
146k
        if (crl->meth != NULL && crl->meth->crl_free != NULL) {
261
0
            if (!crl->meth->crl_free(crl))
262
0
                return 0;
263
0
        }
264
146k
        AUTHORITY_KEYID_free(crl->akid);
265
146k
        ISSUING_DIST_POINT_free(crl->idp);
266
146k
        ASN1_INTEGER_free(crl->crl_number);
267
146k
        ASN1_INTEGER_free(crl->base_crl_number);
268
146k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
269
146k
        OPENSSL_free(crl->propq);
270
146k
        break;
271
0
    case ASN1_OP_DUP_POST: {
272
0
        X509_CRL *old = exarg;
273
274
0
        if (!ossl_x509_crl_set0_libctx(crl, old->libctx, old->propq))
275
0
            return 0;
276
0
    } break;
277
1.25M
    }
278
1.25M
    return 1;
279
1.25M
}
280
281
/* Convert IDP into a more convenient form */
282
283
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
284
4.82k
{
285
4.82k
    int idp_only = 0;
286
287
    /* Set various flags according to IDP */
288
4.82k
    crl->idp_flags |= IDP_PRESENT;
289
4.82k
    if (idp->onlyuser > 0) {
290
95
        idp_only++;
291
95
        crl->idp_flags |= IDP_ONLYUSER;
292
95
    }
293
4.82k
    if (idp->onlyCA > 0) {
294
1.25k
        idp_only++;
295
1.25k
        crl->idp_flags |= IDP_ONLYCA;
296
1.25k
    }
297
4.82k
    if (idp->onlyattr > 0) {
298
155
        idp_only++;
299
155
        crl->idp_flags |= IDP_ONLYATTR;
300
155
    }
301
302
4.82k
    if (idp_only > 1)
303
77
        crl->idp_flags |= IDP_INVALID;
304
305
4.82k
    if (idp->indirectCRL > 0)
306
64
        crl->idp_flags |= IDP_INDIRECT;
307
308
4.82k
    if (idp->onlysomereasons) {
309
166
        crl->idp_flags |= IDP_REASONS;
310
166
        if (idp->onlysomereasons->length > 0)
311
57
            crl->idp_reasons = idp->onlysomereasons->data[0];
312
166
        if (idp->onlysomereasons->length > 1)
313
40
            crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
314
166
        crl->idp_reasons &= CRLDP_ALL_REASONS;
315
166
    }
316
317
4.82k
    return DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
318
4.82k
}
319
320
ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
321
    ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
322
    ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
323
    ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
324
} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
325
326
IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
327
328
IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
329
330
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
331
332
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
333
334
IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
335
336
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
337
    const X509_REVOKED *const *b)
338
24
{
339
24
    return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
340
24
        (ASN1_STRING *)&(*b)->serialNumber));
341
24
}
342
343
X509_CRL *X509_CRL_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
344
0
{
345
0
    X509_CRL *crl = NULL;
346
347
0
    crl = (X509_CRL *)ASN1_item_new((X509_CRL_it()));
348
0
    if (!ossl_x509_crl_set0_libctx(crl, libctx, propq)) {
349
0
        X509_CRL_free(crl);
350
0
        crl = NULL;
351
0
    }
352
0
    return crl;
353
0
}
354
355
int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
356
0
{
357
0
    X509_CRL_INFO *inf;
358
359
0
    inf = &crl->crl;
360
0
    if (inf->revoked == NULL)
361
0
        inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
362
0
    if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
363
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
364
0
        return 0;
365
0
    }
366
0
    inf->enc.modified = 1;
367
0
    return 1;
368
0
}
369
370
int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
371
2.92k
{
372
2.92k
    if (crl->meth->crl_verify)
373
2.92k
        return crl->meth->crl_verify(crl, r);
374
0
    return 0;
375
2.92k
}
376
377
int X509_CRL_get0_by_serial(X509_CRL *crl,
378
    X509_REVOKED **ret, const ASN1_INTEGER *serial)
379
0
{
380
0
    if (crl->meth->crl_lookup)
381
0
        return crl->meth->crl_lookup(crl, ret, serial, NULL);
382
0
    return 0;
383
0
}
384
385
int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
386
2.92k
{
387
2.92k
    if (crl->meth->crl_lookup)
388
2.92k
        return crl->meth->crl_lookup(crl, ret,
389
2.92k
            X509_get0_serialNumber(x),
390
2.92k
            X509_get_issuer_name(x));
391
0
    return 0;
392
2.92k
}
393
394
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
395
2.92k
{
396
2.92k
    if (X509_ALGOR_cmp(&crl->sig_alg, &crl->crl.sig_alg) != 0) {
397
1.33k
        ERR_raise(ERR_LIB_X509, X509_R_CRL_SIGNATURE_ALGORITHM_MISMATCH);
398
1.33k
        return 0;
399
1.33k
    }
400
1.58k
    return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_CRL_INFO),
401
1.58k
        &crl->sig_alg, &crl->signature, &crl->crl, NULL,
402
1.58k
        r, crl->libctx, crl->propq);
403
2.92k
}
404
405
static int crl_revoked_issuer_match(X509_CRL *crl, const X509_NAME *nm,
406
    X509_REVOKED *rev)
407
2
{
408
2
    int i;
409
410
2
    if (!rev->issuer) {
411
2
        if (!nm)
412
0
            return 1;
413
2
        if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
414
2
            return 1;
415
0
        return 0;
416
2
    }
417
418
0
    if (!nm)
419
0
        nm = X509_CRL_get_issuer(crl);
420
421
0
    for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
422
0
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
423
0
        if (gen->type != GEN_DIRNAME)
424
0
            continue;
425
0
        if (!X509_NAME_cmp(nm, gen->d.directoryName))
426
0
            return 1;
427
0
    }
428
0
    return 0;
429
0
}
430
431
static int def_crl_lookup(X509_CRL *crl,
432
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
433
    const X509_NAME *issuer)
434
2.92k
{
435
2.92k
    X509_REVOKED rtmp, *rev;
436
2.92k
    int idx, num;
437
438
2.92k
    if (crl->crl.revoked == NULL)
439
2.88k
        return 0;
440
441
    /*
442
     * Sort revoked into serial number order if not already sorted. Do this
443
     * under a lock to avoid race condition.
444
     */
445
39
    if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
446
36
        if (!CRYPTO_THREAD_write_lock(crl->lock))
447
0
            return 0;
448
36
        sk_X509_REVOKED_sort(crl->crl.revoked);
449
36
        CRYPTO_THREAD_unlock(crl->lock);
450
36
    }
451
39
    rtmp.serialNumber = *serial;
452
39
    idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
453
39
    if (idx < 0)
454
37
        return 0;
455
    /* Need to look for matching name */
456
2
    for (num = sk_X509_REVOKED_num(crl->crl.revoked); idx < num; idx++) {
457
2
        rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
458
2
        if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
459
0
            return 0;
460
2
        if (crl_revoked_issuer_match(crl, issuer, rev)) {
461
2
            if (ret)
462
2
                *ret = rev;
463
2
            if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
464
0
                return 2;
465
2
            return 1;
466
2
        }
467
2
    }
468
0
    return 0;
469
2
}
470
471
void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
472
0
{
473
0
    if (meth == NULL)
474
0
        default_crl_method = &int_crl_meth;
475
0
    else
476
0
        default_crl_method = meth;
477
0
}
478
479
X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init)(X509_CRL *crl),
480
    int (*crl_free)(X509_CRL *crl),
481
    int (*crl_lookup)(X509_CRL *crl,
482
        X509_REVOKED **ret,
483
        const ASN1_INTEGER *ser,
484
        const X509_NAME *issuer),
485
    int (*crl_verify)(X509_CRL *crl,
486
        EVP_PKEY *pk))
487
0
{
488
0
    X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
489
490
0
    if (m == NULL)
491
0
        return NULL;
492
0
    m->crl_init = crl_init;
493
0
    m->crl_free = crl_free;
494
0
    m->crl_lookup = crl_lookup;
495
0
    m->crl_verify = crl_verify;
496
0
    m->flags = X509_CRL_METHOD_DYNAMIC;
497
0
    return m;
498
0
}
499
500
void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
501
0
{
502
0
    if (m == NULL || !(m->flags & X509_CRL_METHOD_DYNAMIC))
503
0
        return;
504
0
    OPENSSL_free(m);
505
0
}
506
507
void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
508
0
{
509
0
    crl->meth_data = dat;
510
0
}
511
512
void *X509_CRL_get_meth_data(X509_CRL *crl)
513
0
{
514
0
    return crl->meth_data;
515
0
}
516
517
int ossl_x509_crl_set0_libctx(X509_CRL *x, OSSL_LIB_CTX *libctx,
518
    const char *propq)
519
0
{
520
0
    if (x != NULL) {
521
0
        x->libctx = libctx;
522
0
        OPENSSL_free(x->propq);
523
0
        x->propq = NULL;
524
0
        if (propq != NULL) {
525
0
            x->propq = OPENSSL_strdup(propq);
526
0
            if (x->propq == NULL)
527
0
                return 0;
528
0
        }
529
0
    }
530
0
    return 1;
531
0
}