Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/x509/v3_bcons.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2026 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/asn1.h>
13
#include <openssl/asn1t.h>
14
#include <openssl/conf.h>
15
#include <openssl/x509v3.h>
16
#include "ext_dat.h"
17
#include "x509_local.h"
18
19
#include <crypto/asn1.h>
20
21
static STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
22
    BASIC_CONSTRAINTS *bcons,
23
    STACK_OF(CONF_VALUE)
24
        *extlist);
25
static BASIC_CONSTRAINTS *v2i_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
26
    X509V3_CTX *ctx,
27
    STACK_OF(CONF_VALUE) *values);
28
29
const X509V3_EXT_METHOD ossl_v3_bcons = {
30
    NID_basic_constraints, 0,
31
    ASN1_ITEM_ref(BASIC_CONSTRAINTS),
32
    0, 0, 0, 0,
33
    0, 0,
34
    (X509V3_EXT_I2V)i2v_BASIC_CONSTRAINTS,
35
    (X509V3_EXT_V2I)v2i_BASIC_CONSTRAINTS,
36
    NULL, NULL,
37
    NULL
38
};
39
40
ASN1_SEQUENCE(BASIC_CONSTRAINTS) = {
41
    ASN1_OPT(BASIC_CONSTRAINTS, ca, ASN1_FBOOLEAN),
42
    ASN1_OPT(BASIC_CONSTRAINTS, pathlen, ASN1_INTEGER)
43
230k
} ASN1_SEQUENCE_END(BASIC_CONSTRAINTS)
44
230k
45
230k
IMPLEMENT_ASN1_FUNCTIONS(BASIC_CONSTRAINTS)
46
230k
47
230k
static STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
48
230k
    BASIC_CONSTRAINTS *bcons,
49
230k
    STACK_OF(CONF_VALUE)
50
230k
        *extlist)
51
230k
{
52
9.30k
    X509V3_add_value_bool("CA", bcons->ca, &extlist);
53
9.30k
    X509V3_add_value_int("pathlen", bcons->pathlen, &extlist);
54
9.30k
    return extlist;
55
9.30k
}
56
57
static BASIC_CONSTRAINTS *v2i_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
58
    X509V3_CTX *ctx,
59
    STACK_OF(CONF_VALUE) *values)
60
367
{
61
367
    BASIC_CONSTRAINTS *bcons = NULL;
62
367
    CONF_VALUE *val;
63
367
    int i, ca_seen = 0;
64
65
367
    if ((bcons = BASIC_CONSTRAINTS_new()) == NULL) {
66
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
67
0
        return NULL;
68
0
    }
69
557
    for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
70
404
        val = sk_CONF_VALUE_value(values, i);
71
404
        if (strcmp(val->name, "CA") == 0) {
72
307
            if (ca_seen) {
73
5
                ERR_raise_data(ERR_LIB_X509V3, X509V3_R_DUPLICATE_FIELD,
74
5
                    "field=%s", val->name);
75
5
                goto err;
76
5
            }
77
302
            if (!X509V3_get_value_bool(val, &bcons->ca))
78
126
                goto err;
79
176
            ca_seen = 1;
80
176
        } else if (strcmp(val->name, "pathlen") == 0) {
81
26
            if (bcons->pathlen != NULL) {
82
3
                ERR_raise_data(ERR_LIB_X509V3, X509V3_R_DUPLICATE_FIELD,
83
3
                    "field=%s", val->name);
84
3
                goto err;
85
3
            }
86
23
            if (!X509V3_get_value_int(val, &bcons->pathlen))
87
9
                goto err;
88
71
        } else {
89
71
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NAME);
90
71
            X509V3_conf_add_error_name_value(val);
91
71
            goto err;
92
71
        }
93
404
    }
94
153
    return bcons;
95
214
err:
96
214
    BASIC_CONSTRAINTS_free(bcons);
97
    return NULL;
98
367
}