Coverage Report

Created: 2025-12-31 06:58

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.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
44.7k
{
83
84
44.7k
    int i, j;
85
44.7k
    GENERAL_NAMES *gens, *gtmp;
86
44.7k
    STACK_OF(X509_REVOKED) *revoked;
87
88
44.7k
    revoked = X509_CRL_get_REVOKED(crl);
89
90
44.7k
    gens = NULL;
91
69.1k
    for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
92
26.8k
        X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
93
26.8k
        STACK_OF(X509_EXTENSION) *exts;
94
26.8k
        ASN1_ENUMERATED *reason;
95
26.8k
        X509_EXTENSION *ext;
96
26.8k
        gtmp = X509_REVOKED_get_ext_d2i(rev,
97
26.8k
            NID_certificate_issuer, &j, NULL);
98
26.8k
        if (!gtmp && (j != -1)) {
99
1.49k
            crl->flags |= EXFLAG_INVALID;
100
1.49k
            return 1;
101
1.49k
        }
102
103
25.3k
        if (gtmp) {
104
3.96k
            gens = gtmp;
105
3.96k
            if (crl->issuers == NULL) {
106
528
                crl->issuers = sk_GENERAL_NAMES_new_null();
107
528
                if (crl->issuers == NULL) {
108
0
                    GENERAL_NAMES_free(gtmp);
109
0
                    return 0;
110
0
                }
111
528
            }
112
3.96k
            if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp)) {
113
0
                GENERAL_NAMES_free(gtmp);
114
0
                return 0;
115
0
            }
116
3.96k
        }
117
25.3k
        rev->issuer = gens;
118
119
25.3k
        reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
120
25.3k
        if (!reason && (j != -1)) {
121
839
            crl->flags |= EXFLAG_INVALID;
122
839
            return 1;
123
839
        }
124
125
24.4k
        if (reason) {
126
6.24k
            rev->reason = ASN1_ENUMERATED_get(reason);
127
6.24k
            ASN1_ENUMERATED_free(reason);
128
6.24k
        } else
129
18.2k
            rev->reason = CRL_REASON_NONE;
130
131
        /* Check for critical CRL entry extensions */
132
133
24.4k
        exts = rev->extensions;
134
135
37.5k
        for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
136
15.5k
            ext = sk_X509_EXTENSION_value(exts, j);
137
15.5k
            if (X509_EXTENSION_get_critical(ext)) {
138
3.37k
                if (OBJ_obj2nid(X509_EXTENSION_get_object(ext)) == NID_certificate_issuer)
139
927
                    continue;
140
2.44k
                crl->flags |= EXFLAG_CRITICAL;
141
2.44k
                break;
142
3.37k
            }
143
15.5k
        }
144
24.4k
    }
145
146
42.3k
    return 1;
147
44.7k
}
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.18M
{
156
2.18M
    X509_CRL *crl = (X509_CRL *)*pval;
157
2.18M
    STACK_OF(X509_EXTENSION) *exts;
158
2.18M
    X509_EXTENSION *ext;
159
2.18M
    int idx, i;
160
161
2.18M
    switch (operation) {
162
260k
    case ASN1_OP_D2I_PRE:
163
260k
        if (crl->meth->crl_free) {
164
0
            if (!crl->meth->crl_free(crl))
165
0
                return 0;
166
0
        }
167
260k
        AUTHORITY_KEYID_free(crl->akid);
168
260k
        ISSUING_DIST_POINT_free(crl->idp);
169
260k
        ASN1_INTEGER_free(crl->crl_number);
170
260k
        ASN1_INTEGER_free(crl->base_crl_number);
171
260k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
172
        /* fall thru */
173
174
521k
    case ASN1_OP_NEW_POST:
175
521k
        crl->idp = NULL;
176
521k
        crl->akid = NULL;
177
521k
        crl->flags = 0;
178
521k
        crl->idp_flags = 0;
179
521k
        crl->idp_reasons = CRLDP_ALL_REASONS;
180
521k
        crl->meth = default_crl_method;
181
521k
        crl->meth_data = NULL;
182
521k
        crl->issuers = NULL;
183
521k
        crl->crl_number = NULL;
184
521k
        crl->base_crl_number = NULL;
185
521k
        break;
186
187
187k
    case ASN1_OP_D2I_POST:
188
187k
        if (!X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL))
189
0
            crl->flags |= EXFLAG_NO_FINGERPRINT;
190
187k
        crl->idp = X509_CRL_get_ext_d2i(crl,
191
187k
            NID_issuing_distribution_point, &i,
192
187k
            NULL);
193
187k
        if (crl->idp != NULL) {
194
26.4k
            if (!setup_idp(crl, crl->idp))
195
4.75k
                crl->flags |= EXFLAG_INVALID;
196
160k
        } else if (i != -1) {
197
60.8k
            crl->flags |= EXFLAG_INVALID;
198
60.8k
        }
199
200
187k
        crl->akid = X509_CRL_get_ext_d2i(crl,
201
187k
            NID_authority_key_identifier, &i,
202
187k
            NULL);
203
187k
        if (crl->akid == NULL && i != -1)
204
39.4k
            crl->flags |= EXFLAG_INVALID;
205
206
187k
        crl->crl_number = X509_CRL_get_ext_d2i(crl,
207
187k
            NID_crl_number, &i, NULL);
208
187k
        if (crl->crl_number == NULL && i != -1)
209
12.4k
            crl->flags |= EXFLAG_INVALID;
210
211
187k
        crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
212
187k
            NID_delta_crl, &i,
213
187k
            NULL);
214
187k
        if (crl->base_crl_number == NULL && i != -1)
215
6.48k
            crl->flags |= EXFLAG_INVALID;
216
        /* Delta CRLs must have CRL number */
217
187k
        if (crl->base_crl_number && !crl->crl_number)
218
1.50k
            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
187k
        exts = crl->crl.extensions;
228
229
1.44M
        for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
230
1.25M
            int nid;
231
1.25M
            ext = sk_X509_EXTENSION_value(exts, idx);
232
1.25M
            nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
233
1.25M
            if (nid == NID_freshest_crl)
234
59.1k
                crl->flags |= EXFLAG_FRESHEST;
235
1.25M
            if (X509_EXTENSION_get_critical(ext)) {
236
                /* We handle IDP and deltas */
237
40.9k
                if ((nid == NID_issuing_distribution_point)
238
16.0k
                    || (nid == NID_authority_key_identifier)
239
8.21k
                    || (nid == NID_delta_crl))
240
34.5k
                    continue;
241
6.45k
                crl->flags |= EXFLAG_CRITICAL;
242
6.45k
                break;
243
40.9k
            }
244
1.25M
        }
245
246
187k
        if (!crl_set_issuers(crl))
247
29
            return 0;
248
249
187k
        if (crl->meth->crl_init) {
250
0
            if (crl->meth->crl_init(crl) == 0)
251
0
                return 0;
252
0
        }
253
254
187k
        crl->flags |= EXFLAG_SET;
255
187k
        break;
256
257
260k
    case ASN1_OP_FREE_POST:
258
260k
        if (crl->meth != NULL && crl->meth->crl_free != NULL) {
259
0
            if (!crl->meth->crl_free(crl))
260
0
                return 0;
261
0
        }
262
260k
        AUTHORITY_KEYID_free(crl->akid);
263
260k
        ISSUING_DIST_POINT_free(crl->idp);
264
260k
        ASN1_INTEGER_free(crl->crl_number);
265
260k
        ASN1_INTEGER_free(crl->base_crl_number);
266
260k
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
267
260k
        OPENSSL_free(crl->propq);
268
260k
        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.18M
    }
276
2.18M
    return 1;
277
2.18M
}
278
279
/* Convert IDP into a more convenient form */
280
281
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
282
14.1k
{
283
14.1k
    int idp_only = 0;
284
285
    /* Set various flags according to IDP */
286
14.1k
    crl->idp_flags |= IDP_PRESENT;
287
14.1k
    if (idp->onlyuser > 0) {
288
509
        idp_only++;
289
509
        crl->idp_flags |= IDP_ONLYUSER;
290
509
    }
291
14.1k
    if (idp->onlyCA > 0) {
292
879
        idp_only++;
293
879
        crl->idp_flags |= IDP_ONLYCA;
294
879
    }
295
14.1k
    if (idp->onlyattr > 0) {
296
610
        idp_only++;
297
610
        crl->idp_flags |= IDP_ONLYATTR;
298
610
    }
299
300
14.1k
    if (idp_only > 1)
301
435
        crl->idp_flags |= IDP_INVALID;
302
303
14.1k
    if (idp->indirectCRL > 0)
304
477
        crl->idp_flags |= IDP_INDIRECT;
305
306
14.1k
    if (idp->onlysomereasons) {
307
1.21k
        crl->idp_flags |= IDP_REASONS;
308
1.21k
        if (idp->onlysomereasons->length > 0)
309
699
            crl->idp_reasons = idp->onlysomereasons->data[0];
310
1.21k
        if (idp->onlysomereasons->length > 1)
311
463
            crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
312
1.21k
        crl->idp_reasons &= CRLDP_ALL_REASONS;
313
1.21k
    }
314
315
14.1k
    return DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
316
14.1k
}
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
2.26k
{
337
2.26k
    return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
338
2.26k
        (ASN1_STRING *)&(*b)->serialNumber));
339
2.26k
}
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
5.27k
{
370
5.27k
    if (crl->meth->crl_verify)
371
5.27k
        return crl->meth->crl_verify(crl, r);
372
0
    return 0;
373
5.27k
}
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
5.27k
{
385
5.27k
    if (crl->meth->crl_lookup)
386
5.27k
        return crl->meth->crl_lookup(crl, ret,
387
5.27k
            X509_get0_serialNumber(x),
388
5.27k
            X509_get_issuer_name(x));
389
0
    return 0;
390
5.27k
}
391
392
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
393
5.27k
{
394
5.27k
    return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_CRL_INFO),
395
5.27k
        &crl->sig_alg, &crl->signature, &crl->crl, NULL,
396
5.27k
        r, crl->libctx, crl->propq);
397
5.27k
}
398
399
static int crl_revoked_issuer_match(X509_CRL *crl, const X509_NAME *nm,
400
    X509_REVOKED *rev)
401
16
{
402
16
    int i;
403
404
16
    if (!rev->issuer) {
405
16
        if (!nm)
406
0
            return 1;
407
16
        if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
408
16
            return 1;
409
0
        return 0;
410
16
    }
411
412
0
    if (!nm)
413
0
        nm = X509_CRL_get_issuer(crl);
414
415
0
    for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
416
0
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
417
0
        if (gen->type != GEN_DIRNAME)
418
0
            continue;
419
0
        if (!X509_NAME_cmp(nm, gen->d.directoryName))
420
0
            return 1;
421
0
    }
422
0
    return 0;
423
0
}
424
425
static int def_crl_lookup(X509_CRL *crl,
426
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
427
    const X509_NAME *issuer)
428
5.27k
{
429
5.27k
    X509_REVOKED rtmp, *rev;
430
5.27k
    int idx, num;
431
432
5.27k
    if (crl->crl.revoked == NULL)
433
5.20k
        return 0;
434
435
    /*
436
     * Sort revoked into serial number order if not already sorted. Do this
437
     * under a lock to avoid race condition.
438
     */
439
77
    if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
440
76
        if (!CRYPTO_THREAD_write_lock(crl->lock))
441
0
            return 0;
442
76
        sk_X509_REVOKED_sort(crl->crl.revoked);
443
76
        CRYPTO_THREAD_unlock(crl->lock);
444
76
    }
445
77
    rtmp.serialNumber = *serial;
446
77
    idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
447
77
    if (idx < 0)
448
61
        return 0;
449
    /* Need to look for matching name */
450
16
    for (num = sk_X509_REVOKED_num(crl->crl.revoked); idx < num; idx++) {
451
16
        rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
452
16
        if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
453
0
            return 0;
454
16
        if (crl_revoked_issuer_match(crl, issuer, rev)) {
455
16
            if (ret)
456
16
                *ret = rev;
457
16
            if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
458
0
                return 2;
459
16
            return 1;
460
16
        }
461
16
    }
462
0
    return 0;
463
16
}
464
465
void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
466
0
{
467
0
    if (meth == NULL)
468
0
        default_crl_method = &int_crl_meth;
469
0
    else
470
0
        default_crl_method = meth;
471
0
}
472
473
X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init)(X509_CRL *crl),
474
    int (*crl_free)(X509_CRL *crl),
475
    int (*crl_lookup)(X509_CRL *crl,
476
        X509_REVOKED **ret,
477
        const ASN1_INTEGER *ser,
478
        const X509_NAME *issuer),
479
    int (*crl_verify)(X509_CRL *crl,
480
        EVP_PKEY *pk))
481
0
{
482
0
    X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
483
484
0
    if (m == NULL) {
485
0
        ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
486
0
        return NULL;
487
0
    }
488
0
    m->crl_init = crl_init;
489
0
    m->crl_free = crl_free;
490
0
    m->crl_lookup = crl_lookup;
491
0
    m->crl_verify = crl_verify;
492
0
    m->flags = X509_CRL_METHOD_DYNAMIC;
493
0
    return m;
494
0
}
495
496
void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
497
0
{
498
0
    if (m == NULL || !(m->flags & X509_CRL_METHOD_DYNAMIC))
499
0
        return;
500
0
    OPENSSL_free(m);
501
0
}
502
503
void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
504
0
{
505
0
    crl->meth_data = dat;
506
0
}
507
508
void *X509_CRL_get_meth_data(X509_CRL *crl)
509
0
{
510
0
    return crl->meth_data;
511
0
}
512
513
int ossl_x509_crl_set0_libctx(X509_CRL *x, OSSL_LIB_CTX *libctx,
514
    const char *propq)
515
0
{
516
0
    if (x != NULL) {
517
0
        x->libctx = libctx;
518
0
        OPENSSL_free(x->propq);
519
0
        x->propq = NULL;
520
0
        if (propq != NULL) {
521
0
            x->propq = OPENSSL_strdup(propq);
522
0
            if (x->propq == NULL)
523
0
                return 0;
524
0
        }
525
0
    }
526
0
    return 1;
527
0
}