Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/x509/x_crl.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2021 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
14.1k
{
285
14.1k
    int idp_only = 0;
286
287
    /* Set various flags according to IDP */
288
14.1k
    crl->idp_flags |= IDP_PRESENT;
289
14.1k
    if (idp->onlyuser > 0) {
290
509
        idp_only++;
291
509
        crl->idp_flags |= IDP_ONLYUSER;
292
509
    }
293
14.1k
    if (idp->onlyCA > 0) {
294
879
        idp_only++;
295
879
        crl->idp_flags |= IDP_ONLYCA;
296
879
    }
297
14.1k
    if (idp->onlyattr > 0) {
298
610
        idp_only++;
299
610
        crl->idp_flags |= IDP_ONLYATTR;
300
610
    }
301
302
14.1k
    if (idp_only > 1)
303
435
        crl->idp_flags |= IDP_INVALID;
304
305
14.1k
    if (idp->indirectCRL > 0)
306
477
        crl->idp_flags |= IDP_INDIRECT;
307
308
14.1k
    if (idp->onlysomereasons) {
309
1.21k
        crl->idp_flags |= IDP_REASONS;
310
1.21k
        if (idp->onlysomereasons->length > 0)
311
699
            crl->idp_reasons = idp->onlysomereasons->data[0];
312
1.21k
        if (idp->onlysomereasons->length > 1)
313
463
            crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
314
1.21k
        crl->idp_reasons &= CRLDP_ALL_REASONS;
315
1.21k
    }
316
317
14.1k
    return DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
318
14.1k
}
319
320
ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
321
    ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
322
    ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
323
    ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
324
} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
325
326
IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
327
328
IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
329
330
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
331
332
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
333
334
IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
335
336
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
337
    const X509_REVOKED *const *b)
338
2.26k
{
339
2.26k
    return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
340
2.26k
        (ASN1_STRING *)&(*b)->serialNumber));
341
2.26k
}
342
343
X509_CRL *X509_CRL_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
344
0
{
345
0
    X509_CRL *crl = NULL;
346
347
0
    crl = (X509_CRL *)ASN1_item_new((X509_CRL_it()));
348
0
    if (!ossl_x509_crl_set0_libctx(crl, libctx, propq)) {
349
0
        X509_CRL_free(crl);
350
0
        crl = NULL;
351
0
    }
352
0
    return crl;
353
0
}
354
355
int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
356
0
{
357
0
    X509_CRL_INFO *inf;
358
359
0
    inf = &crl->crl;
360
0
    if (inf->revoked == NULL)
361
0
        inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
362
0
    if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
363
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
364
0
        return 0;
365
0
    }
366
0
    inf->enc.modified = 1;
367
0
    return 1;
368
0
}
369
370
int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
371
5.27k
{
372
5.27k
    if (crl->meth->crl_verify)
373
5.27k
        return crl->meth->crl_verify(crl, r);
374
0
    return 0;
375
5.27k
}
376
377
int X509_CRL_get0_by_serial(X509_CRL *crl,
378
    X509_REVOKED **ret, const ASN1_INTEGER *serial)
379
0
{
380
0
    if (crl->meth->crl_lookup)
381
0
        return crl->meth->crl_lookup(crl, ret, serial, NULL);
382
0
    return 0;
383
0
}
384
385
int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
386
5.27k
{
387
5.27k
    if (crl->meth->crl_lookup)
388
5.27k
        return crl->meth->crl_lookup(crl, ret,
389
5.27k
            X509_get0_serialNumber(x),
390
5.27k
            X509_get_issuer_name(x));
391
0
    return 0;
392
5.27k
}
393
394
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
395
5.27k
{
396
5.27k
    return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_CRL_INFO),
397
5.27k
        &crl->sig_alg, &crl->signature, &crl->crl, NULL,
398
5.27k
        r, crl->libctx, crl->propq);
399
5.27k
}
400
401
static int crl_revoked_issuer_match(X509_CRL *crl, const X509_NAME *nm,
402
    X509_REVOKED *rev)
403
16
{
404
16
    int i;
405
406
16
    if (!rev->issuer) {
407
16
        if (!nm)
408
0
            return 1;
409
16
        if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
410
16
            return 1;
411
0
        return 0;
412
16
    }
413
414
0
    if (!nm)
415
0
        nm = X509_CRL_get_issuer(crl);
416
417
0
    for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
418
0
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
419
0
        if (gen->type != GEN_DIRNAME)
420
0
            continue;
421
0
        if (!X509_NAME_cmp(nm, gen->d.directoryName))
422
0
            return 1;
423
0
    }
424
0
    return 0;
425
0
}
426
427
static int def_crl_lookup(X509_CRL *crl,
428
    X509_REVOKED **ret, const ASN1_INTEGER *serial,
429
    const X509_NAME *issuer)
430
5.27k
{
431
5.27k
    X509_REVOKED rtmp, *rev;
432
5.27k
    int idx, num;
433
434
5.27k
    if (crl->crl.revoked == NULL)
435
5.20k
        return 0;
436
437
    /*
438
     * Sort revoked into serial number order if not already sorted. Do this
439
     * under a lock to avoid race condition.
440
     */
441
77
    if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
442
76
        if (!CRYPTO_THREAD_write_lock(crl->lock))
443
0
            return 0;
444
76
        sk_X509_REVOKED_sort(crl->crl.revoked);
445
76
        CRYPTO_THREAD_unlock(crl->lock);
446
76
    }
447
77
    rtmp.serialNumber = *serial;
448
77
    idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
449
77
    if (idx < 0)
450
61
        return 0;
451
    /* Need to look for matching name */
452
16
    for (num = sk_X509_REVOKED_num(crl->crl.revoked); idx < num; idx++) {
453
16
        rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
454
16
        if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
455
0
            return 0;
456
16
        if (crl_revoked_issuer_match(crl, issuer, rev)) {
457
16
            if (ret)
458
16
                *ret = rev;
459
16
            if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
460
0
                return 2;
461
16
            return 1;
462
16
        }
463
16
    }
464
0
    return 0;
465
16
}
466
467
void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
468
0
{
469
0
    if (meth == NULL)
470
0
        default_crl_method = &int_crl_meth;
471
0
    else
472
0
        default_crl_method = meth;
473
0
}
474
475
X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init)(X509_CRL *crl),
476
    int (*crl_free)(X509_CRL *crl),
477
    int (*crl_lookup)(X509_CRL *crl,
478
        X509_REVOKED **ret,
479
        const ASN1_INTEGER *ser,
480
        const X509_NAME *issuer),
481
    int (*crl_verify)(X509_CRL *crl,
482
        EVP_PKEY *pk))
483
0
{
484
0
    X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
485
486
0
    if (m == NULL)
487
0
        return NULL;
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
}