Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/x509/x_crl.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2022 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
1.25M
} ASN1_SEQUENCE_END(X509_REVOKED)
27
1.25M
28
1.25M
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
29
1.25M
static int def_crl_lookup(X509_CRL *crl,
30
1.25M
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
31
1.25M
    const X509_NAME *issuer);
32
1.25M
33
1.25M
static X509_CRL_METHOD int_crl_meth = {
34
1.25M
    0,
35
1.25M
    0, 0,
36
1.25M
    def_crl_lookup,
37
1.25M
    def_crl_verify
38
1.25M
};
39
1.25M
40
1.25M
static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
41
1.25M
42
1.25M
/*
43
1.25M
 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
44
1.25M
 * the original encoding the signature won't be affected by reordering of the
45
1.25M
 * revoked field.
46
1.25M
 */
47
1.25M
static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
48
1.25M
    void *exarg)
49
1.75M
{
50
1.75M
    X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
51
52
1.75M
    if (!a || !a->revoked)
53
1.68M
        return 1;
54
67.5k
    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
32.2k
    case ASN1_OP_D2I_POST:
60
32.2k
        (void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
61
32.2k
        break;
62
67.5k
    }
63
67.5k
    return 1;
64
67.5k
}
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
44.0k
{
83
84
44.0k
    int i, j;
85
44.0k
    GENERAL_NAMES *gens, *gtmp;
86
44.0k
    STACK_OF(X509_REVOKED) *revoked;
87
88
44.0k
    revoked = X509_CRL_get_REVOKED(crl);
89
90
44.0k
    gens = NULL;
91
99.8k
    for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
92
58.2k
        X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
93
58.2k
        STACK_OF(X509_EXTENSION) *exts;
94
58.2k
        ASN1_ENUMERATED *reason;
95
58.2k
        X509_EXTENSION *ext;
96
58.2k
        gtmp = X509_REVOKED_get_ext_d2i(rev,
97
58.2k
            NID_certificate_issuer, &j, NULL);
98
58.2k
        if (!gtmp && (j != -1)) {
99
1.38k
            crl->flags |= EXFLAG_INVALID;
100
1.38k
            return 1;
101
1.38k
        }
102
103
56.8k
        if (gtmp) {
104
30.8k
            gens = gtmp;
105
30.8k
            if (crl->issuers == NULL) {
106
401
                crl->issuers = sk_GENERAL_NAMES_new_null();
107
401
                if (crl->issuers == NULL) {
108
0
                    GENERAL_NAMES_free(gtmp);
109
0
                    return 0;
110
0
                }
111
401
            }
112
30.8k
            if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp)) {
113
0
                GENERAL_NAMES_free(gtmp);
114
0
                return 0;
115
0
            }
116
30.8k
        }
117
56.8k
        rev->issuer = gens;
118
119
56.8k
        reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
120
56.8k
        if (!reason && (j != -1)) {
121
987
            crl->flags |= EXFLAG_INVALID;
122
987
            return 1;
123
987
        }
124
125
55.8k
        if (reason) {
126
7.47k
            rev->reason = ASN1_ENUMERATED_get(reason);
127
7.47k
            ASN1_ENUMERATED_free(reason);
128
7.47k
        } else
129
48.3k
            rev->reason = CRL_REASON_NONE;
130
131
        /* Check for critical CRL entry extensions */
132
133
55.8k
        exts = rev->extensions;
134
135
96.6k
        for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
136
43.2k
            ext = sk_X509_EXTENSION_value(exts, j);
137
43.2k
            if (X509_EXTENSION_get_critical(ext)) {
138
3.41k
                if (OBJ_obj2nid(X509_EXTENSION_get_object(ext)) == NID_certificate_issuer)
139
991
                    continue;
140
2.42k
                crl->flags |= EXFLAG_CRITICAL;
141
2.42k
                break;
142
3.41k
            }
143
43.2k
        }
144
55.8k
    }
145
146
41.6k
    return 1;
147
44.0k
}
148
149
/*
150
 * The X509_CRL structure needs a bit of customisation. Cache some extensions
151
 * and hash of the whole CRL or set EXFLAG_NO_FINGERPRINT if this fails.
152
 */
153
static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
154
    void *exarg)
155
2.12M
{
156
2.12M
    X509_CRL *crl = (X509_CRL *)*pval;
157
2.12M
    STACK_OF(X509_EXTENSION) *exts;
158
2.12M
    X509_EXTENSION *ext;
159
2.12M
    int idx, i;
160
161
2.12M
    switch (operation) {
162
250k
    case ASN1_OP_D2I_PRE:
163
250k
        if (crl->meth->crl_free) {
164
0
            if (!crl->meth->crl_free(crl))
165
0
                return 0;
166
0
        }
167
250k
        AUTHORITY_KEYID_free(crl->akid);
168
250k
        ISSUING_DIST_POINT_free(crl->idp);
169
250k
        ASN1_INTEGER_free(crl->crl_number);
170
250k
        ASN1_INTEGER_free(crl->base_crl_number);
171
250k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
172
        /* fall thru */
173
174
500k
    case ASN1_OP_NEW_POST:
175
500k
        crl->idp = NULL;
176
500k
        crl->akid = NULL;
177
500k
        crl->flags = 0;
178
500k
        crl->idp_flags = 0;
179
500k
        crl->idp_reasons = CRLDP_ALL_REASONS;
180
500k
        crl->meth = default_crl_method;
181
500k
        crl->meth_data = NULL;
182
500k
        crl->issuers = NULL;
183
500k
        crl->crl_number = NULL;
184
500k
        crl->base_crl_number = NULL;
185
500k
        break;
186
187
188k
    case ASN1_OP_D2I_POST:
188
188k
        if (!X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL))
189
0
            crl->flags |= EXFLAG_NO_FINGERPRINT;
190
188k
        crl->idp = X509_CRL_get_ext_d2i(crl,
191
188k
            NID_issuing_distribution_point, &i,
192
188k
            NULL);
193
188k
        if (crl->idp != NULL) {
194
29.3k
            if (!setup_idp(crl, crl->idp))
195
3.53k
                crl->flags |= EXFLAG_INVALID;
196
158k
        } else if (i != -1) {
197
62.4k
            crl->flags |= EXFLAG_INVALID;
198
62.4k
        }
199
200
188k
        crl->akid = X509_CRL_get_ext_d2i(crl,
201
188k
            NID_authority_key_identifier, &i,
202
188k
            NULL);
203
188k
        if (crl->akid == NULL && i != -1)
204
37.6k
            crl->flags |= EXFLAG_INVALID;
205
206
188k
        crl->crl_number = X509_CRL_get_ext_d2i(crl,
207
188k
            NID_crl_number, &i, NULL);
208
188k
        if (crl->crl_number == NULL && i != -1)
209
13.7k
            crl->flags |= EXFLAG_INVALID;
210
211
188k
        crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
212
188k
            NID_delta_crl, &i,
213
188k
            NULL);
214
188k
        if (crl->base_crl_number == NULL && i != -1)
215
8.36k
            crl->flags |= EXFLAG_INVALID;
216
        /* Delta CRLs must have CRL number */
217
188k
        if (crl->base_crl_number && !crl->crl_number)
218
1.37k
            crl->flags |= EXFLAG_INVALID;
219
220
        /*
221
         * See if we have any unhandled critical CRL extensions and indicate
222
         * this in a flag. We only currently handle IDP so anything else
223
         * critical sets the flag. This code accesses the X509_CRL structure
224
         * directly: applications shouldn't do this.
225
         */
226
227
188k
        exts = crl->crl.extensions;
228
229
1.08M
        for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
230
906k
            int nid;
231
906k
            ext = sk_X509_EXTENSION_value(exts, idx);
232
906k
            nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
233
906k
            if (nid == NID_freshest_crl)
234
45.9k
                crl->flags |= EXFLAG_FRESHEST;
235
906k
            if (X509_EXTENSION_get_critical(ext)) {
236
                /* We handle IDP and deltas */
237
39.8k
                if ((nid == NID_issuing_distribution_point)
238
16.7k
                    || (nid == NID_authority_key_identifier)
239
8.56k
                    || (nid == NID_delta_crl))
240
34.0k
                    continue;
241
5.84k
                crl->flags |= EXFLAG_CRITICAL;
242
5.84k
                break;
243
39.8k
            }
244
906k
        }
245
246
188k
        if (!crl_set_issuers(crl))
247
0
            return 0;
248
249
188k
        if (crl->meth->crl_init) {
250
0
            if (crl->meth->crl_init(crl) == 0)
251
0
                return 0;
252
0
        }
253
254
188k
        crl->flags |= EXFLAG_SET;
255
188k
        break;
256
257
250k
    case ASN1_OP_FREE_POST:
258
250k
        if (crl->meth != NULL && crl->meth->crl_free != NULL) {
259
0
            if (!crl->meth->crl_free(crl))
260
0
                return 0;
261
0
        }
262
250k
        AUTHORITY_KEYID_free(crl->akid);
263
250k
        ISSUING_DIST_POINT_free(crl->idp);
264
250k
        ASN1_INTEGER_free(crl->crl_number);
265
250k
        ASN1_INTEGER_free(crl->base_crl_number);
266
250k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
267
250k
        OPENSSL_free(crl->propq);
268
250k
        break;
269
0
    case ASN1_OP_DUP_POST: {
270
0
        X509_CRL *old = exarg;
271
272
0
        if (!ossl_x509_crl_set0_libctx(crl, old->libctx, old->propq))
273
0
            return 0;
274
0
    } break;
275
2.12M
    }
276
2.12M
    return 1;
277
2.12M
}
278
279
/* Convert IDP into a more convenient form */
280
281
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
282
17.0k
{
283
17.0k
    int idp_only = 0;
284
285
    /* Set various flags according to IDP */
286
17.0k
    crl->idp_flags |= IDP_PRESENT;
287
17.0k
    if (idp->onlyuser > 0) {
288
549
        idp_only++;
289
549
        crl->idp_flags |= IDP_ONLYUSER;
290
549
    }
291
17.0k
    if (idp->onlyCA > 0) {
292
1.60k
        idp_only++;
293
1.60k
        crl->idp_flags |= IDP_ONLYCA;
294
1.60k
    }
295
17.0k
    if (idp->onlyattr > 0) {
296
671
        idp_only++;
297
671
        crl->idp_flags |= IDP_ONLYATTR;
298
671
    }
299
300
17.0k
    if (idp_only > 1)
301
519
        crl->idp_flags |= IDP_INVALID;
302
303
17.0k
    if (idp->indirectCRL > 0)
304
510
        crl->idp_flags |= IDP_INDIRECT;
305
306
17.0k
    if (idp->onlysomereasons) {
307
1.30k
        crl->idp_flags |= IDP_REASONS;
308
1.30k
        if (idp->onlysomereasons->length > 0)
309
726
            crl->idp_reasons = idp->onlysomereasons->data[0];
310
1.30k
        if (idp->onlysomereasons->length > 1)
311
488
            crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
312
1.30k
        crl->idp_reasons &= CRLDP_ALL_REASONS;
313
1.30k
    }
314
315
17.0k
    return DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
316
17.0k
}
317
318
ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
319
    ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
320
    ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
321
    ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
322
} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
323
324
IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
325
326
IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
327
328
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
329
330
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
331
332
IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
333
334
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
335
    const X509_REVOKED *const *b)
336
25.1k
{
337
25.1k
    return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
338
25.1k
        (ASN1_STRING *)&(*b)->serialNumber));
339
25.1k
}
340
341
X509_CRL *X509_CRL_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
342
0
{
343
0
    X509_CRL *crl = NULL;
344
345
0
    crl = (X509_CRL *)ASN1_item_new((X509_CRL_it()));
346
0
    if (!ossl_x509_crl_set0_libctx(crl, libctx, propq)) {
347
0
        X509_CRL_free(crl);
348
0
        crl = NULL;
349
0
    }
350
0
    return crl;
351
0
}
352
353
int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
354
0
{
355
0
    X509_CRL_INFO *inf;
356
357
0
    inf = &crl->crl;
358
0
    if (inf->revoked == NULL)
359
0
        inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
360
0
    if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
361
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
362
0
        return 0;
363
0
    }
364
0
    inf->enc.modified = 1;
365
0
    return 1;
366
0
}
367
368
int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
369
3.86k
{
370
3.86k
    if (crl->meth->crl_verify)
371
3.86k
        return crl->meth->crl_verify(crl, r);
372
0
    return 0;
373
3.86k
}
374
375
int X509_CRL_get0_by_serial(X509_CRL *crl,
376
    X509_REVOKED **ret, const ASN1_INTEGER *serial)
377
0
{
378
0
    if (crl->meth->crl_lookup)
379
0
        return crl->meth->crl_lookup(crl, ret, serial, NULL);
380
0
    return 0;
381
0
}
382
383
int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
384
3.86k
{
385
3.86k
    if (crl->meth->crl_lookup)
386
3.86k
        return crl->meth->crl_lookup(crl, ret,
387
3.86k
            X509_get0_serialNumber(x),
388
3.86k
            X509_get_issuer_name(x));
389
0
    return 0;
390
3.86k
}
391
392
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
393
2.89k
{
394
2.89k
    if (X509_ALGOR_cmp(&crl->sig_alg, &crl->crl.sig_alg) != 0) {
395
1.60k
        ERR_raise(ERR_LIB_X509, X509_R_CRL_SIGNATURE_ALGORITHM_MISMATCH);
396
1.60k
        return 0;
397
1.60k
    }
398
1.28k
    return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_CRL_INFO),
399
1.28k
        &crl->sig_alg, &crl->signature, &crl->crl, NULL,
400
1.28k
        r, crl->libctx, crl->propq);
401
2.89k
}
402
403
static int crl_revoked_issuer_match(X509_CRL *crl, const X509_NAME *nm,
404
    X509_REVOKED *rev)
405
41
{
406
41
    int i;
407
408
41
    if (!rev->issuer) {
409
41
        if (!nm)
410
0
            return 1;
411
41
        if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
412
41
            return 1;
413
0
        return 0;
414
41
    }
415
416
0
    if (!nm)
417
0
        nm = X509_CRL_get_issuer(crl);
418
419
0
    for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
420
0
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
421
0
        if (gen->type != GEN_DIRNAME)
422
0
            continue;
423
0
        if (!X509_NAME_cmp(nm, gen->d.directoryName))
424
0
            return 1;
425
0
    }
426
0
    return 0;
427
0
}
428
429
static int def_crl_lookup(X509_CRL *crl,
430
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
431
    const X509_NAME *issuer)
432
3.86k
{
433
3.86k
    X509_REVOKED rtmp, *rev;
434
3.86k
    int idx, num;
435
436
3.86k
    if (crl->crl.revoked == NULL)
437
3.75k
        return 0;
438
439
    /*
440
     * Sort revoked into serial number order if not already sorted. Do this
441
     * under a lock to avoid race condition.
442
     */
443
112
    if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
444
111
        if (!CRYPTO_THREAD_write_lock(crl->lock))
445
0
            return 0;
446
111
        sk_X509_REVOKED_sort(crl->crl.revoked);
447
111
        CRYPTO_THREAD_unlock(crl->lock);
448
111
    }
449
112
    rtmp.serialNumber = *serial;
450
112
    idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
451
112
    if (idx < 0)
452
71
        return 0;
453
    /* Need to look for matching name */
454
41
    for (num = sk_X509_REVOKED_num(crl->crl.revoked); idx < num; idx++) {
455
41
        rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
456
41
        if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
457
0
            return 0;
458
41
        if (crl_revoked_issuer_match(crl, issuer, rev)) {
459
41
            if (ret)
460
41
                *ret = rev;
461
41
            if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
462
0
                return 2;
463
41
            return 1;
464
41
        }
465
41
    }
466
0
    return 0;
467
41
}
468
469
void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
470
0
{
471
0
    if (meth == NULL)
472
0
        default_crl_method = &int_crl_meth;
473
0
    else
474
0
        default_crl_method = meth;
475
0
}
476
477
X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init)(X509_CRL *crl),
478
    int (*crl_free)(X509_CRL *crl),
479
    int (*crl_lookup)(X509_CRL *crl,
480
        X509_REVOKED **ret,
481
        const ASN1_INTEGER *ser,
482
        const X509_NAME *issuer),
483
    int (*crl_verify)(X509_CRL *crl,
484
        EVP_PKEY *pk))
485
0
{
486
0
    X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
487
488
0
    if (m == NULL) {
489
0
        ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
490
0
        return NULL;
491
0
    }
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
}