Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/x509/v3_crld.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2024 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/conf.h>
13
#include <openssl/asn1.h>
14
#include <openssl/asn1t.h>
15
#include <openssl/x509v3.h>
16
17
#include "crypto/x509.h"
18
#include "ext_dat.h"
19
#include "x509_local.h"
20
21
static void *v2i_crld(const X509V3_EXT_METHOD *method,
22
                      X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
23
static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
24
                     int indent);
25
26
const X509V3_EXT_METHOD ossl_v3_crld = {
27
    NID_crl_distribution_points, 0, ASN1_ITEM_ref(CRL_DIST_POINTS),
28
    0, 0, 0, 0,
29
    0, 0,
30
    0,
31
    v2i_crld,
32
    i2r_crldp, 0,
33
    NULL
34
};
35
36
const X509V3_EXT_METHOD ossl_v3_freshest_crl = {
37
    NID_freshest_crl, 0, ASN1_ITEM_ref(CRL_DIST_POINTS),
38
    0, 0, 0, 0,
39
    0, 0,
40
    0,
41
    v2i_crld,
42
    i2r_crldp, 0,
43
    NULL
44
};
45
46
static STACK_OF(GENERAL_NAME) *gnames_from_sectname(X509V3_CTX *ctx,
47
                                                    char *sect)
48
0
{
49
0
    STACK_OF(CONF_VALUE) *gnsect;
50
0
    STACK_OF(GENERAL_NAME) *gens;
51
0
    if (*sect == '@')
52
0
        gnsect = X509V3_get_section(ctx, sect + 1);
53
0
    else
54
0
        gnsect = X509V3_parse_list(sect);
55
0
    if (!gnsect) {
56
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND);
57
0
        return NULL;
58
0
    }
59
0
    gens = v2i_GENERAL_NAMES(NULL, ctx, gnsect);
60
0
    if (*sect == '@')
61
0
        X509V3_section_free(ctx, gnsect);
62
0
    else
63
0
        sk_CONF_VALUE_pop_free(gnsect, X509V3_conf_free);
64
0
    return gens;
65
0
}
66
67
static int set_dist_point_name(DIST_POINT_NAME **pdp, X509V3_CTX *ctx,
68
                               CONF_VALUE *cnf)
69
0
{
70
0
    STACK_OF(GENERAL_NAME) *fnm = NULL;
71
0
    STACK_OF(X509_NAME_ENTRY) *rnm = NULL;
72
73
0
    if (cnf->value == NULL) {
74
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE);
75
0
        goto err;
76
0
    }
77
78
0
    if (strncmp(cnf->name, "fullname", 9) == 0) {
79
0
        fnm = gnames_from_sectname(ctx, cnf->value);
80
0
        if (!fnm)
81
0
            goto err;
82
0
    } else if (strcmp(cnf->name, "relativename") == 0) {
83
0
        int ret;
84
0
        STACK_OF(CONF_VALUE) *dnsect;
85
0
        X509_NAME *nm;
86
0
        nm = X509_NAME_new();
87
0
        if (nm == NULL)
88
0
            return -1;
89
0
        dnsect = X509V3_get_section(ctx, cnf->value);
90
0
        if (!dnsect) {
91
0
            X509_NAME_free(nm);
92
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND);
93
0
            return -1;
94
0
        }
95
0
        ret = X509V3_NAME_from_section(nm, dnsect, MBSTRING_ASC);
96
0
        X509V3_section_free(ctx, dnsect);
97
0
        rnm = nm->entries;
98
0
        nm->entries = NULL;
99
0
        X509_NAME_free(nm);
100
0
        if (!ret || sk_X509_NAME_ENTRY_num(rnm) <= 0)
101
0
            goto err;
102
        /*
103
         * Since its a name fragment can't have more than one RDNSequence
104
         */
105
0
        if (sk_X509_NAME_ENTRY_value(rnm,
106
0
                                     sk_X509_NAME_ENTRY_num(rnm) - 1)->set) {
107
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_MULTIPLE_RDNS);
108
0
            goto err;
109
0
        }
110
0
    } else
111
0
        return 0;
112
113
0
    if (*pdp) {
114
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_DISTPOINT_ALREADY_SET);
115
0
        goto err;
116
0
    }
117
118
0
    *pdp = DIST_POINT_NAME_new();
119
0
    if (*pdp == NULL)
120
0
        goto err;
121
0
    if (fnm) {
122
0
        (*pdp)->type = 0;
123
0
        (*pdp)->name.fullname = fnm;
124
0
    } else {
125
0
        (*pdp)->type = 1;
126
0
        (*pdp)->name.relativename = rnm;
127
0
    }
128
129
0
    return 1;
130
131
0
 err:
132
0
    sk_GENERAL_NAME_pop_free(fnm, GENERAL_NAME_free);
133
0
    sk_X509_NAME_ENTRY_pop_free(rnm, X509_NAME_ENTRY_free);
134
0
    return -1;
135
0
}
136
137
static const BIT_STRING_BITNAME reason_flags[] = {
138
    {0, "Unused", "unused"},
139
    {1, "Key Compromise", "keyCompromise"},
140
    {2, "CA Compromise", "CACompromise"},
141
    {3, "Affiliation Changed", "affiliationChanged"},
142
    {4, "Superseded", "superseded"},
143
    {5, "Cessation Of Operation", "cessationOfOperation"},
144
    {6, "Certificate Hold", "certificateHold"},
145
    {7, "Privilege Withdrawn", "privilegeWithdrawn"},
146
    {8, "AA Compromise", "AACompromise"},
147
    {-1, NULL, NULL}
148
};
149
150
static int set_reasons(ASN1_BIT_STRING **preas, char *value)
151
0
{
152
0
    STACK_OF(CONF_VALUE) *rsk = NULL;
153
0
    const BIT_STRING_BITNAME *pbn;
154
0
    const char *bnam;
155
0
    int i, ret = 0;
156
0
    rsk = X509V3_parse_list(value);
157
0
    if (rsk == NULL)
158
0
        return 0;
159
0
    if (*preas != NULL)
160
0
        goto err;
161
0
    for (i = 0; i < sk_CONF_VALUE_num(rsk); i++) {
162
0
        bnam = sk_CONF_VALUE_value(rsk, i)->name;
163
0
        if (*preas == NULL) {
164
0
            *preas = ASN1_BIT_STRING_new();
165
0
            if (*preas == NULL)
166
0
                goto err;
167
0
        }
168
0
        for (pbn = reason_flags; pbn->lname; pbn++) {
169
0
            if (strcmp(pbn->sname, bnam) == 0) {
170
0
                if (!ASN1_BIT_STRING_set_bit(*preas, pbn->bitnum, 1))
171
0
                    goto err;
172
0
                break;
173
0
            }
174
0
        }
175
0
        if (pbn->lname == NULL)
176
0
            goto err;
177
0
    }
178
0
    ret = 1;
179
180
0
 err:
181
0
    sk_CONF_VALUE_pop_free(rsk, X509V3_conf_free);
182
0
    return ret;
183
0
}
184
185
static int print_reasons(BIO *out, const char *rname,
186
                         ASN1_BIT_STRING *rflags, int indent)
187
14.4k
{
188
14.4k
    int first = 1;
189
14.4k
    const BIT_STRING_BITNAME *pbn;
190
14.4k
    BIO_printf(out, "%*s%s:\n%*s", indent, "", rname, indent + 2, "");
191
144k
    for (pbn = reason_flags; pbn->lname; pbn++) {
192
130k
        if (ASN1_BIT_STRING_get_bit(rflags, pbn->bitnum)) {
193
34.6k
            if (first)
194
8.55k
                first = 0;
195
26.0k
            else
196
26.0k
                BIO_puts(out, ", ");
197
34.6k
            BIO_puts(out, pbn->lname);
198
34.6k
        }
199
130k
    }
200
14.4k
    if (first)
201
5.91k
        BIO_puts(out, "<EMPTY>\n");
202
8.55k
    else
203
8.55k
        BIO_puts(out, "\n");
204
14.4k
    return 1;
205
14.4k
}
206
207
static DIST_POINT *crldp_from_section(X509V3_CTX *ctx,
208
                                      STACK_OF(CONF_VALUE) *nval)
209
0
{
210
0
    int i;
211
0
    CONF_VALUE *cnf;
212
0
    DIST_POINT *point = DIST_POINT_new();
213
214
0
    if (point == NULL)
215
0
        goto err;
216
0
    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
217
0
        int ret;
218
0
        cnf = sk_CONF_VALUE_value(nval, i);
219
0
        ret = set_dist_point_name(&point->distpoint, ctx, cnf);
220
0
        if (ret > 0)
221
0
            continue;
222
0
        if (ret < 0)
223
0
            goto err;
224
0
        if (strcmp(cnf->name, "reasons") == 0) {
225
0
            if (!set_reasons(&point->reasons, cnf->value))
226
0
                goto err;
227
0
        } else if (strcmp(cnf->name, "CRLissuer") == 0) {
228
0
            point->CRLissuer = gnames_from_sectname(ctx, cnf->value);
229
0
            if (point->CRLissuer == NULL)
230
0
                goto err;
231
0
        }
232
0
    }
233
234
0
    return point;
235
236
0
 err:
237
0
    DIST_POINT_free(point);
238
0
    return NULL;
239
0
}
240
241
static void *v2i_crld(const X509V3_EXT_METHOD *method,
242
                      X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
243
0
{
244
0
    STACK_OF(DIST_POINT) *crld;
245
0
    GENERAL_NAMES *gens = NULL;
246
0
    GENERAL_NAME *gen = NULL;
247
0
    CONF_VALUE *cnf;
248
0
    const int num = sk_CONF_VALUE_num(nval);
249
0
    int i;
250
251
0
    crld = sk_DIST_POINT_new_reserve(NULL, num);
252
0
    if (crld == NULL)
253
0
        goto merr;
254
0
    for (i = 0; i < num; i++) {
255
0
        DIST_POINT *point;
256
257
0
        cnf = sk_CONF_VALUE_value(nval, i);
258
0
        if (cnf->value == NULL) {
259
0
            STACK_OF(CONF_VALUE) *dpsect;
260
0
            dpsect = X509V3_get_section(ctx, cnf->name);
261
0
            if (!dpsect)
262
0
                goto err;
263
0
            point = crldp_from_section(ctx, dpsect);
264
0
            X509V3_section_free(ctx, dpsect);
265
0
            if (point == NULL)
266
0
                goto err;
267
0
            sk_DIST_POINT_push(crld, point); /* no failure as it was reserved */
268
0
        } else {
269
0
            if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
270
0
                goto err;
271
0
            if ((gens = GENERAL_NAMES_new()) == NULL)
272
0
                goto merr;
273
0
            if (!sk_GENERAL_NAME_push(gens, gen))
274
0
                goto merr;
275
0
            gen = NULL;
276
0
            if ((point = DIST_POINT_new()) == NULL)
277
0
                goto merr;
278
0
            sk_DIST_POINT_push(crld, point); /* no failure as it was reserved */
279
0
            if ((point->distpoint = DIST_POINT_NAME_new()) == NULL)
280
0
                goto merr;
281
0
            point->distpoint->name.fullname = gens;
282
0
            point->distpoint->type = 0;
283
0
            gens = NULL;
284
0
        }
285
0
    }
286
0
    return crld;
287
288
0
 merr:
289
0
    ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
290
0
 err:
291
0
    GENERAL_NAME_free(gen);
292
0
    GENERAL_NAMES_free(gens);
293
0
    sk_DIST_POINT_pop_free(crld, DIST_POINT_free);
294
0
    return NULL;
295
0
}
296
297
static int dpn_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
298
                  void *exarg)
299
1.00M
{
300
1.00M
    DIST_POINT_NAME *dpn = (DIST_POINT_NAME *)*pval;
301
302
1.00M
    switch (operation) {
303
182k
    case ASN1_OP_NEW_POST:
304
182k
        dpn->dpname = NULL;
305
182k
        break;
306
307
182k
    case ASN1_OP_FREE_POST:
308
182k
        X509_NAME_free(dpn->dpname);
309
182k
        break;
310
1.00M
    }
311
1.00M
    return 1;
312
1.00M
}
313
314
315
ASN1_CHOICE_cb(DIST_POINT_NAME, dpn_cb) = {
316
        ASN1_IMP_SEQUENCE_OF(DIST_POINT_NAME, name.fullname, GENERAL_NAME, 0),
317
        ASN1_IMP_SET_OF(DIST_POINT_NAME, name.relativename, X509_NAME_ENTRY, 1)
318
} ASN1_CHOICE_END_cb(DIST_POINT_NAME, DIST_POINT_NAME, type)
319
320
321
IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT_NAME)
322
323
ASN1_SEQUENCE(DIST_POINT) = {
324
        ASN1_EXP_OPT(DIST_POINT, distpoint, DIST_POINT_NAME, 0),
325
        ASN1_IMP_OPT(DIST_POINT, reasons, ASN1_BIT_STRING, 1),
326
        ASN1_IMP_SEQUENCE_OF_OPT(DIST_POINT, CRLissuer, GENERAL_NAME, 2)
327
} ASN1_SEQUENCE_END(DIST_POINT)
328
329
IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT)
330
331
ASN1_ITEM_TEMPLATE(CRL_DIST_POINTS) =
332
        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CRLDistributionPoints, DIST_POINT)
333
ASN1_ITEM_TEMPLATE_END(CRL_DIST_POINTS)
334
335
IMPLEMENT_ASN1_FUNCTIONS(CRL_DIST_POINTS)
336
337
ASN1_SEQUENCE(ISSUING_DIST_POINT) = {
338
        ASN1_EXP_OPT(ISSUING_DIST_POINT, distpoint, DIST_POINT_NAME, 0),
339
        ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyuser, ASN1_FBOOLEAN, 1),
340
        ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyCA, ASN1_FBOOLEAN, 2),
341
        ASN1_IMP_OPT(ISSUING_DIST_POINT, onlysomereasons, ASN1_BIT_STRING, 3),
342
        ASN1_IMP_OPT(ISSUING_DIST_POINT, indirectCRL, ASN1_FBOOLEAN, 4),
343
        ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyattr, ASN1_FBOOLEAN, 5)
344
} ASN1_SEQUENCE_END(ISSUING_DIST_POINT)
345
346
IMPLEMENT_ASN1_FUNCTIONS(ISSUING_DIST_POINT)
347
348
static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
349
                   int indent);
350
static void *v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
351
                     STACK_OF(CONF_VALUE) *nval);
352
353
const X509V3_EXT_METHOD ossl_v3_idp = {
354
    NID_issuing_distribution_point, X509V3_EXT_MULTILINE,
355
    ASN1_ITEM_ref(ISSUING_DIST_POINT),
356
    0, 0, 0, 0,
357
    0, 0,
358
    0,
359
    v2i_idp,
360
    i2r_idp, 0,
361
    NULL
362
};
363
364
static void *v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
365
                     STACK_OF(CONF_VALUE) *nval)
366
0
{
367
0
    ISSUING_DIST_POINT *idp = NULL;
368
0
    CONF_VALUE *cnf;
369
0
    char *name, *val;
370
0
    int i, ret;
371
0
    idp = ISSUING_DIST_POINT_new();
372
0
    if (idp == NULL)
373
0
        goto merr;
374
0
    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
375
0
        cnf = sk_CONF_VALUE_value(nval, i);
376
0
        name = cnf->name;
377
0
        val = cnf->value;
378
0
        ret = set_dist_point_name(&idp->distpoint, ctx, cnf);
379
0
        if (ret > 0)
380
0
            continue;
381
0
        if (ret < 0)
382
0
            goto err;
383
0
        if (strcmp(name, "onlyuser") == 0) {
384
0
            if (!X509V3_get_value_bool(cnf, &idp->onlyuser))
385
0
                goto err;
386
0
        } else if (strcmp(name, "onlyCA") == 0) {
387
0
            if (!X509V3_get_value_bool(cnf, &idp->onlyCA))
388
0
                goto err;
389
0
        } else if (strcmp(name, "onlyAA") == 0) {
390
0
            if (!X509V3_get_value_bool(cnf, &idp->onlyattr))
391
0
                goto err;
392
0
        } else if (strcmp(name, "indirectCRL") == 0) {
393
0
            if (!X509V3_get_value_bool(cnf, &idp->indirectCRL))
394
0
                goto err;
395
0
        } else if (strcmp(name, "onlysomereasons") == 0) {
396
0
            if (!set_reasons(&idp->onlysomereasons, val))
397
0
                goto err;
398
0
        } else {
399
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NAME);
400
0
            X509V3_conf_add_error_name_value(cnf);
401
0
            goto err;
402
0
        }
403
0
    }
404
0
    return idp;
405
406
0
 merr:
407
0
    ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
408
0
 err:
409
0
    ISSUING_DIST_POINT_free(idp);
410
0
    return NULL;
411
0
}
412
413
static int print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent)
414
11.2k
{
415
11.2k
    int i;
416
26.3k
    for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
417
15.1k
        if (i > 0)
418
6.80k
            BIO_puts(out, "\n");
419
15.1k
        BIO_printf(out, "%*s", indent + 2, "");
420
15.1k
        GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i));
421
15.1k
    }
422
11.2k
    return 1;
423
11.2k
}
424
425
static int print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent)
426
32.2k
{
427
32.2k
    if (dpn->type == 0) {
428
11.5k
        BIO_printf(out, "%*sFull Name:\n", indent, "");
429
11.5k
        print_gens(out, dpn->name.fullname, indent);
430
20.6k
    } else {
431
20.6k
        X509_NAME ntmp;
432
20.6k
        ntmp.entries = dpn->name.relativename;
433
20.6k
        BIO_printf(out, "%*sRelative Name:\n%*s", indent, "", indent + 2, "");
434
20.6k
        X509_NAME_print_ex(out, &ntmp, 0, XN_FLAG_ONELINE);
435
20.6k
        BIO_puts(out, "\n");
436
20.6k
    }
437
32.2k
    return 1;
438
32.2k
}
439
440
static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
441
                   int indent)
442
61.0k
{
443
61.0k
    ISSUING_DIST_POINT *idp = pidp;
444
61.0k
    if (idp->distpoint)
445
28.0k
        print_distpoint(out, idp->distpoint, indent);
446
61.0k
    if (idp->onlyuser > 0)
447
3.52k
        BIO_printf(out, "%*sOnly User Certificates\n", indent, "");
448
61.0k
    if (idp->onlyCA > 0)
449
1.23k
        BIO_printf(out, "%*sOnly CA Certificates\n", indent, "");
450
61.0k
    if (idp->indirectCRL > 0)
451
2.16k
        BIO_printf(out, "%*sIndirect CRL\n", indent, "");
452
61.0k
    if (idp->onlysomereasons)
453
10.2k
        print_reasons(out, "Only Some Reasons", idp->onlysomereasons, indent);
454
61.0k
    if (idp->onlyattr > 0)
455
2.29k
        BIO_printf(out, "%*sOnly Attribute Certificates\n", indent, "");
456
61.0k
    if (!idp->distpoint && (idp->onlyuser <= 0) && (idp->onlyCA <= 0)
457
61.0k
        && (idp->indirectCRL <= 0) && !idp->onlysomereasons
458
61.0k
        && (idp->onlyattr <= 0))
459
13.6k
        BIO_printf(out, "%*s<EMPTY>\n", indent, "");
460
461
61.0k
    return 1;
462
61.0k
}
463
464
static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
465
                     int indent)
466
12.9k
{
467
12.9k
    STACK_OF(DIST_POINT) *crld = pcrldp;
468
12.9k
    DIST_POINT *point;
469
12.9k
    int i;
470
37.4k
    for (i = 0; i < sk_DIST_POINT_num(crld); i++) {
471
24.4k
        if (i > 0)
472
14.1k
            BIO_puts(out, "\n");
473
24.4k
        point = sk_DIST_POINT_value(crld, i);
474
24.4k
        if (point->distpoint)
475
4.08k
            print_distpoint(out, point->distpoint, indent);
476
24.4k
        if (point->reasons)
477
4.04k
            print_reasons(out, "Reasons", point->reasons, indent);
478
24.4k
        if (point->CRLissuer) {
479
4.36k
            BIO_printf(out, "%*sCRL Issuer:\n", indent, "");
480
4.36k
            print_gens(out, point->CRLissuer, indent);
481
4.36k
        }
482
24.4k
    }
483
12.9k
    return 1;
484
12.9k
}
485
486
/* Append any nameRelativeToCRLIssuer in dpn to iname, set in dpn->dpname */
487
int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, const X509_NAME *iname)
488
29.7k
{
489
29.7k
    int i;
490
29.7k
    STACK_OF(X509_NAME_ENTRY) *frag;
491
29.7k
    X509_NAME_ENTRY *ne;
492
493
29.7k
    if (dpn == NULL || dpn->type != 1)
494
10.6k
        return 1;
495
19.1k
    frag = dpn->name.relativename;
496
19.1k
    X509_NAME_free(dpn->dpname); /* just in case it was already set */
497
19.1k
    dpn->dpname = X509_NAME_dup(iname);
498
19.1k
    if (dpn->dpname == NULL)
499
4.33k
        return 0;
500
43.0k
    for (i = 0; i < sk_X509_NAME_ENTRY_num(frag); i++) {
501
28.5k
        ne = sk_X509_NAME_ENTRY_value(frag, i);
502
28.5k
        if (!X509_NAME_add_entry(dpn->dpname, ne, -1, i ? 0 : 1))
503
374
            goto err;
504
28.5k
    }
505
    /* generate cached encoding of name */
506
14.4k
    if (i2d_X509_NAME(dpn->dpname, NULL) >= 0)
507
9.05k
        return 1;
508
509
5.75k
 err:
510
5.75k
    X509_NAME_free(dpn->dpname);
511
5.75k
    dpn->dpname = NULL;
512
5.75k
    return 0;
513
14.4k
}