Coverage Report

Created: 2023-06-08 06:41

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