Coverage Report

Created: 2025-11-11 06:20

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