Coverage Report

Created: 2025-11-16 06:40

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