Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/x509/x_crl.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 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.27M
} ASN1_SEQUENCE_END(X509_REVOKED)
27
1.27M
28
1.27M
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
29
1.27M
static int def_crl_lookup(X509_CRL *crl,
30
1.27M
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
31
1.27M
    const X509_NAME *issuer);
32
1.27M
33
1.27M
static X509_CRL_METHOD int_crl_meth = {
34
1.27M
    0,
35
1.27M
    0, 0,
36
1.27M
    def_crl_lookup,
37
1.27M
    def_crl_verify
38
1.27M
};
39
1.27M
40
1.27M
static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
41
1.27M
42
1.27M
/*
43
1.27M
 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
44
1.27M
 * the original encoding the signature won't be affected by reordering of the
45
1.27M
 * revoked field.
46
1.27M
 */
47
1.27M
static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
48
1.27M
    void *exarg)
49
1.73M
{
50
1.73M
    X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
51
52
1.73M
    if (!a || !a->revoked)
53
1.66M
        return 1;
54
65.1k
    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
31.6k
    case ASN1_OP_D2I_POST:
60
31.6k
        (void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
61
31.6k
        break;
62
65.1k
    }
63
65.1k
    return 1;
64
65.1k
}
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
330k
    for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
92
227k
        X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
93
227k
        STACK_OF(X509_EXTENSION) *exts;
94
227k
        ASN1_ENUMERATED *reason;
95
227k
        X509_EXTENSION *ext;
96
97
227k
        gtmp = X509_REVOKED_get_ext_d2i(rev,
98
227k
            NID_certificate_issuer, &j, NULL);
99
227k
        if (gtmp == NULL && j != -1) {
100
1.67k
            crl->flags |= EXFLAG_INVALID;
101
1.67k
            return 1;
102
1.67k
        }
103
104
225k
        if (gtmp != NULL) {
105
20.9k
            if (crl->issuers == NULL) {
106
3.15k
                crl->issuers = sk_GENERAL_NAMES_new_null();
107
3.15k
                if (crl->issuers == NULL) {
108
0
                    GENERAL_NAMES_free(gtmp);
109
0
                    return 0;
110
0
                }
111
3.15k
            }
112
20.9k
            if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp)) {
113
0
                GENERAL_NAMES_free(gtmp);
114
0
                return 0;
115
0
            }
116
20.9k
            gens = gtmp;
117
20.9k
        }
118
225k
        rev->issuer = gens;
119
120
225k
        reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
121
225k
        if (reason == NULL && j != -1) {
122
5.93k
            crl->flags |= EXFLAG_INVALID;
123
5.93k
            return 1;
124
5.93k
        }
125
126
219k
        if (reason != NULL) {
127
72.1k
            rev->reason = ASN1_ENUMERATED_get(reason);
128
72.1k
            ASN1_ENUMERATED_free(reason);
129
72.1k
        } else
130
147k
            rev->reason = CRL_REASON_NONE;
131
132
        /* Check for critical CRL entry extensions */
133
134
219k
        exts = rev->extensions;
135
136
360k
        for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
137
142k
            ext = sk_X509_EXTENSION_value(exts, j);
138
142k
            if (X509_EXTENSION_get_critical(ext)) {
139
2.09k
                if (OBJ_obj2nid(X509_EXTENSION_get_object(ext))
140
2.09k
                    == NID_certificate_issuer)
141
250
                    continue;
142
1.84k
                crl->flags |= EXFLAG_CRITICAL;
143
1.84k
                break;
144
2.09k
            }
145
142k
        }
146
219k
    }
147
148
103k
    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
2.18M
{
158
2.18M
    X509_CRL *crl = (X509_CRL *)*pval;
159
2.18M
    STACK_OF(X509_EXTENSION) *exts;
160
2.18M
    X509_EXTENSION *ext;
161
2.18M
    int idx, i;
162
163
2.18M
    switch (operation) {
164
260k
    case ASN1_OP_D2I_PRE:
165
260k
        if (crl->meth->crl_free) {
166
0
            if (!crl->meth->crl_free(crl))
167
0
                return 0;
168
0
        }
169
260k
        AUTHORITY_KEYID_free(crl->akid);
170
260k
        ISSUING_DIST_POINT_free(crl->idp);
171
260k
        ASN1_INTEGER_free(crl->crl_number);
172
260k
        ASN1_INTEGER_free(crl->base_crl_number);
173
260k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
174
        /* fall through */
175
176
521k
    case ASN1_OP_NEW_POST:
177
521k
        crl->idp = NULL;
178
521k
        crl->akid = NULL;
179
521k
        crl->flags = 0;
180
521k
        crl->idp_flags = 0;
181
521k
        crl->idp_reasons = CRLDP_ALL_REASONS;
182
521k
        crl->meth = default_crl_method;
183
521k
        crl->meth_data = NULL;
184
521k
        crl->issuers = NULL;
185
521k
        crl->crl_number = NULL;
186
521k
        crl->base_crl_number = NULL;
187
521k
        break;
188
189
187k
    case ASN1_OP_D2I_POST:
190
187k
        if (!X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL))
191
0
            crl->flags |= EXFLAG_NO_FINGERPRINT;
192
187k
        crl->idp = X509_CRL_get_ext_d2i(crl,
193
187k
            NID_issuing_distribution_point, &i,
194
187k
            NULL);
195
187k
        if (crl->idp != NULL) {
196
26.4k
            if (!setup_idp(crl, crl->idp))
197
4.75k
                crl->flags |= EXFLAG_INVALID;
198
160k
        } else if (i != -1) {
199
60.8k
            crl->flags |= EXFLAG_INVALID;
200
60.8k
        }
201
202
187k
        crl->akid = X509_CRL_get_ext_d2i(crl,
203
187k
            NID_authority_key_identifier, &i,
204
187k
            NULL);
205
187k
        if (crl->akid == NULL && i != -1)
206
39.4k
            crl->flags |= EXFLAG_INVALID;
207
208
187k
        crl->crl_number = X509_CRL_get_ext_d2i(crl,
209
187k
            NID_crl_number, &i, NULL);
210
187k
        if (crl->crl_number == NULL && i != -1)
211
12.4k
            crl->flags |= EXFLAG_INVALID;
212
213
187k
        crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
214
187k
            NID_delta_crl, &i,
215
187k
            NULL);
216
187k
        if (crl->base_crl_number == NULL && i != -1)
217
6.48k
            crl->flags |= EXFLAG_INVALID;
218
        /* Delta CRLs must have CRL number */
219
187k
        if (crl->base_crl_number && !crl->crl_number)
220
1.50k
            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
187k
        exts = crl->crl.extensions;
230
231
1.44M
        for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
232
1.25M
            int nid;
233
1.25M
            ext = sk_X509_EXTENSION_value(exts, idx);
234
1.25M
            nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
235
1.25M
            if (nid == NID_freshest_crl)
236
59.1k
                crl->flags |= EXFLAG_FRESHEST;
237
1.25M
            if (X509_EXTENSION_get_critical(ext)) {
238
                /* We handle IDP and deltas */
239
40.9k
                if ((nid == NID_issuing_distribution_point)
240
16.0k
                    || (nid == NID_authority_key_identifier)
241
8.21k
                    || (nid == NID_delta_crl))
242
34.5k
                    continue;
243
6.45k
                crl->flags |= EXFLAG_CRITICAL;
244
6.45k
                break;
245
40.9k
            }
246
1.25M
        }
247
248
187k
        if (!crl_set_issuers(crl))
249
29
            return 0;
250
251
187k
        if (crl->meth->crl_init) {
252
0
            if (crl->meth->crl_init(crl) == 0)
253
0
                return 0;
254
0
        }
255
256
187k
        crl->flags |= EXFLAG_SET;
257
187k
        break;
258
259
260k
    case ASN1_OP_FREE_POST:
260
260k
        if (crl->meth != NULL && crl->meth->crl_free != NULL) {
261
0
            if (!crl->meth->crl_free(crl))
262
0
                return 0;
263
0
        }
264
260k
        AUTHORITY_KEYID_free(crl->akid);
265
260k
        ISSUING_DIST_POINT_free(crl->idp);
266
260k
        ASN1_INTEGER_free(crl->crl_number);
267
260k
        ASN1_INTEGER_free(crl->base_crl_number);
268
260k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
269
260k
        OPENSSL_free(crl->propq);
270
260k
        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
2.18M
    }
278
2.18M
    return 1;
279
2.18M
}
280
281
/* Convert IDP into a more convenient form */
282
283
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
284
12.3k
{
285
12.3k
    int idp_only = 0;
286
12.3k
    int ret = 0;
287
288
    /* Set various flags according to IDP */
289
12.3k
    crl->idp_flags |= IDP_PRESENT;
290
12.3k
    if (idp->onlyuser > 0) {
291
135
        idp_only++;
292
135
        crl->idp_flags |= IDP_ONLYUSER;
293
135
    }
294
12.3k
    if (idp->onlyCA > 0) {
295
2.29k
        idp_only++;
296
2.29k
        crl->idp_flags |= IDP_ONLYCA;
297
2.29k
    }
298
12.3k
    if (idp->onlyattr > 0) {
299
273
        idp_only++;
300
273
        crl->idp_flags |= IDP_ONLYATTR;
301
273
    }
302
303
12.3k
    if (idp_only > 1)
304
94
        crl->idp_flags |= IDP_INVALID;
305
306
12.3k
    if (idp->indirectCRL > 0)
307
78
        crl->idp_flags |= IDP_INDIRECT;
308
309
12.3k
    if (idp->onlysomereasons) {
310
582
        crl->idp_flags |= IDP_REASONS;
311
582
        if (idp->onlysomereasons->length > 0)
312
137
            crl->idp_reasons = idp->onlysomereasons->data[0];
313
582
        if (idp->onlysomereasons->length > 1)
314
54
            crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
315
582
        crl->idp_reasons &= CRLDP_ALL_REASONS;
316
582
    }
317
318
12.3k
    ret = DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
319
320
    /*
321
     * RFC5280 specifies that if onlyContainsUserCerts, onlyContainsCACerts,
322
     * indirectCRL, and OnlyContainsAttributeCerts are all FALSE, there must
323
     * be either a distributionPoint field or an onlySomeReasons field present.
324
     */
325
12.3k
    if (crl->idp_flags == IDP_PRESENT && idp->distpoint == NULL)
326
1.84k
        crl->idp_flags |= IDP_INVALID;
327
328
12.3k
    return ret;
329
12.3k
}
330
331
ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
332
    ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
333
    ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
334
    ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
335
} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
336
337
IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
338
339
IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
340
341
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
342
343
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
344
345
IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
346
347
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
348
    const X509_REVOKED *const *b)
349
2.26k
{
350
2.26k
    return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
351
2.26k
        (ASN1_STRING *)&(*b)->serialNumber));
352
2.26k
}
353
354
X509_CRL *X509_CRL_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
355
0
{
356
0
    X509_CRL *crl = NULL;
357
358
0
    crl = (X509_CRL *)ASN1_item_new((X509_CRL_it()));
359
0
    if (!ossl_x509_crl_set0_libctx(crl, libctx, propq)) {
360
0
        X509_CRL_free(crl);
361
0
        crl = NULL;
362
0
    }
363
0
    return crl;
364
0
}
365
366
int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
367
0
{
368
0
    X509_CRL_INFO *inf;
369
370
0
    inf = &crl->crl;
371
0
    if (inf->revoked == NULL)
372
0
        inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
373
0
    if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
374
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
375
0
        return 0;
376
0
    }
377
0
    inf->enc.modified = 1;
378
0
    return 1;
379
0
}
380
381
int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
382
5.27k
{
383
5.27k
    if (crl->meth->crl_verify)
384
5.27k
        return crl->meth->crl_verify(crl, r);
385
0
    return 0;
386
5.27k
}
387
388
int X509_CRL_get0_by_serial(X509_CRL *crl,
389
    X509_REVOKED **ret, const ASN1_INTEGER *serial)
390
0
{
391
0
    if (crl->meth->crl_lookup)
392
0
        return crl->meth->crl_lookup(crl, ret, serial, NULL);
393
0
    return 0;
394
0
}
395
396
int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
397
5.27k
{
398
5.27k
    if (crl->meth->crl_lookup)
399
5.27k
        return crl->meth->crl_lookup(crl, ret,
400
5.27k
            X509_get0_serialNumber(x),
401
5.27k
            X509_get_issuer_name(x));
402
0
    return 0;
403
5.27k
}
404
405
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
406
5.27k
{
407
5.27k
    return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_CRL_INFO),
408
5.27k
        &crl->sig_alg, &crl->signature, &crl->crl, NULL,
409
5.27k
        r, crl->libctx, crl->propq);
410
5.27k
}
411
412
static int crl_revoked_issuer_match(X509_CRL *crl, const X509_NAME *nm,
413
    X509_REVOKED *rev)
414
16
{
415
16
    int i;
416
417
16
    if (!rev->issuer) {
418
16
        if (!nm)
419
0
            return 1;
420
16
        if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
421
16
            return 1;
422
0
        return 0;
423
16
    }
424
425
0
    if (!nm)
426
0
        nm = X509_CRL_get_issuer(crl);
427
428
0
    for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
429
0
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
430
0
        if (gen->type != GEN_DIRNAME)
431
0
            continue;
432
0
        if (!X509_NAME_cmp(nm, gen->d.directoryName))
433
0
            return 1;
434
0
    }
435
0
    return 0;
436
0
}
437
438
static int def_crl_lookup(X509_CRL *crl,
439
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
440
    const X509_NAME *issuer)
441
5.27k
{
442
5.27k
    X509_REVOKED rtmp, *rev;
443
5.27k
    int idx, num;
444
445
5.27k
    if (crl->crl.revoked == NULL)
446
5.20k
        return 0;
447
448
    /*
449
     * Sort revoked into serial number order if not already sorted. Do this
450
     * under a lock to avoid race condition.
451
     */
452
77
    if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
453
76
        if (!CRYPTO_THREAD_write_lock(crl->lock))
454
0
            return 0;
455
76
        sk_X509_REVOKED_sort(crl->crl.revoked);
456
76
        CRYPTO_THREAD_unlock(crl->lock);
457
76
    }
458
77
    rtmp.serialNumber = *serial;
459
77
    idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
460
77
    if (idx < 0)
461
61
        return 0;
462
    /* Need to look for matching name */
463
16
    for (num = sk_X509_REVOKED_num(crl->crl.revoked); idx < num; idx++) {
464
16
        rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
465
16
        if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
466
0
            return 0;
467
16
        if (crl_revoked_issuer_match(crl, issuer, rev)) {
468
16
            if (ret)
469
16
                *ret = rev;
470
16
            if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
471
0
                return 2;
472
16
            return 1;
473
16
        }
474
16
    }
475
0
    return 0;
476
16
}
477
478
void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
479
0
{
480
0
    if (meth == NULL)
481
0
        default_crl_method = &int_crl_meth;
482
0
    else
483
0
        default_crl_method = meth;
484
0
}
485
486
X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init)(X509_CRL *crl),
487
    int (*crl_free)(X509_CRL *crl),
488
    int (*crl_lookup)(X509_CRL *crl,
489
        X509_REVOKED **ret,
490
        const ASN1_INTEGER *ser,
491
        const X509_NAME *issuer),
492
    int (*crl_verify)(X509_CRL *crl,
493
        EVP_PKEY *pk))
494
0
{
495
0
    X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
496
497
0
    if (m == NULL)
498
0
        return NULL;
499
0
    m->crl_init = crl_init;
500
0
    m->crl_free = crl_free;
501
0
    m->crl_lookup = crl_lookup;
502
0
    m->crl_verify = crl_verify;
503
0
    m->flags = X509_CRL_METHOD_DYNAMIC;
504
0
    return m;
505
0
}
506
507
void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
508
0
{
509
0
    if (m == NULL || !(m->flags & X509_CRL_METHOD_DYNAMIC))
510
0
        return;
511
0
    OPENSSL_free(m);
512
0
}
513
514
void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
515
0
{
516
0
    crl->meth_data = dat;
517
0
}
518
519
void *X509_CRL_get_meth_data(X509_CRL *crl)
520
0
{
521
0
    return crl->meth_data;
522
0
}
523
524
int ossl_x509_crl_set0_libctx(X509_CRL *x, OSSL_LIB_CTX *libctx,
525
    const char *propq)
526
0
{
527
0
    if (x != NULL) {
528
0
        x->libctx = libctx;
529
0
        OPENSSL_free(x->propq);
530
0
        x->propq = NULL;
531
0
        if (propq != NULL) {
532
0
            x->propq = OPENSSL_strdup(propq);
533
0
            if (x->propq == NULL)
534
0
                return 0;
535
0
        }
536
0
    }
537
0
    return 1;
538
0
}