Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/x509/v3_prn.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
/* X509 v3 extension utilities */
11
12
#include <stdio.h>
13
#include "internal/cryptlib.h"
14
#include <openssl/conf.h>
15
#include <openssl/x509v3.h>
16
17
/* Extension printing routines */
18
19
static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
20
    unsigned long flag, int indent, int supported);
21
22
/* Print out a name+value stack */
23
24
void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent,
25
    int ml)
26
116k
{
27
116k
    int i;
28
116k
    CONF_VALUE *nval;
29
116k
    if (!val)
30
0
        return;
31
116k
    if (!ml || !sk_CONF_VALUE_num(val)) {
32
89.5k
        BIO_printf(out, "%*s", indent, "");
33
89.5k
        if (!sk_CONF_VALUE_num(val))
34
10.0k
            BIO_puts(out, "<EMPTY>\n");
35
89.5k
    }
36
369k
    for (i = 0; i < sk_CONF_VALUE_num(val); i++) {
37
252k
        if (ml) {
38
45.3k
            if (i > 0)
39
18.1k
                BIO_printf(out, "\n");
40
45.3k
            BIO_printf(out, "%*s", indent, "");
41
207k
        } else if (i > 0)
42
127k
            BIO_printf(out, ", ");
43
252k
        nval = sk_CONF_VALUE_value(val, i);
44
252k
        if (!nval->name)
45
57.1k
            BIO_puts(out, nval->value);
46
195k
        else if (!nval->value)
47
49.2k
            BIO_puts(out, nval->name);
48
146k
#ifndef CHARSET_EBCDIC
49
146k
        else
50
146k
            BIO_printf(out, "%s:%s", nval->name, nval->value);
51
#else
52
        else {
53
            int len;
54
            char *tmp;
55
            len = strlen(nval->value) + 1;
56
            tmp = OPENSSL_malloc(len);
57
            if (tmp != NULL) {
58
                ascii2ebcdic(tmp, nval->value, len);
59
                BIO_printf(out, "%s:%s", nval->name, tmp);
60
                OPENSSL_free(tmp);
61
            }
62
        }
63
#endif
64
252k
    }
65
116k
}
66
67
/* Main routine: print out a general extension */
68
69
int X509V3_EXT_print(BIO *out, const X509_EXTENSION *ext, unsigned long flag,
70
    int indent)
71
1.60M
{
72
1.60M
    void *ext_str = NULL;
73
1.60M
    char *value = NULL;
74
1.60M
    const ASN1_OCTET_STRING *extoct;
75
1.60M
    const unsigned char *p;
76
1.60M
    int extlen;
77
1.60M
    const X509V3_EXT_METHOD *method;
78
1.60M
    STACK_OF(CONF_VALUE) *nval = NULL;
79
1.60M
    int ok = 1;
80
81
1.60M
    extoct = X509_EXTENSION_get_data(ext);
82
1.60M
    p = ASN1_STRING_get0_data(extoct);
83
1.60M
    extlen = ASN1_STRING_length(extoct);
84
85
1.60M
    if ((method = X509V3_EXT_get(ext)) == NULL)
86
171k
        return unknown_ext_print(out, p, extlen, flag, indent, 0);
87
1.43M
    if (method->it)
88
1.32M
        ext_str = ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
89
114k
    else
90
114k
        ext_str = method->d2i(NULL, &p, extlen);
91
92
1.43M
    if (!ext_str)
93
758k
        return unknown_ext_print(out, p, extlen, flag, indent, 1);
94
95
675k
    if (method->i2s) {
96
47.7k
        if ((value = method->i2s(method, ext_str)) == NULL) {
97
1.80k
            ok = 0;
98
1.80k
            goto err;
99
1.80k
        }
100
45.9k
#ifndef CHARSET_EBCDIC
101
45.9k
        BIO_printf(out, "%*s%s", indent, "", value);
102
#else
103
        {
104
            int len;
105
            char *tmp;
106
            len = strlen(value) + 1;
107
            tmp = OPENSSL_malloc(len);
108
            if (tmp != NULL) {
109
                ascii2ebcdic(tmp, value, len);
110
                BIO_printf(out, "%*s%s", indent, "", tmp);
111
                OPENSSL_free(tmp);
112
            }
113
        }
114
#endif
115
627k
    } else if (method->i2v) {
116
120k
        if ((nval = method->i2v(method, ext_str, NULL)) == NULL) {
117
42.8k
            ok = 0;
118
42.8k
            goto err;
119
42.8k
        }
120
77.9k
        X509V3_EXT_val_prn(out, nval, indent,
121
77.9k
            method->ext_flags & X509V3_EXT_MULTILINE);
122
507k
    } else if (method->i2r) {
123
507k
        if (!method->i2r(method, ext_str, out, indent))
124
85.2k
            ok = 0;
125
507k
    } else
126
0
        ok = 0;
127
128
675k
err:
129
675k
    sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
130
675k
    OPENSSL_free(value);
131
675k
    if (method->it)
132
611k
        ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
133
63.9k
    else
134
63.9k
        method->ext_free(ext_str);
135
675k
    return ok;
136
675k
}
137
138
int X509V3_extensions_print(BIO *bp, const char *title,
139
    const STACK_OF(X509_EXTENSION) *exts,
140
    unsigned long flag, int indent)
141
165k
{
142
165k
    int i, j;
143
144
165k
    if (sk_X509_EXTENSION_num(exts) <= 0)
145
98.9k
        return 1;
146
147
66.0k
    if (title) {
148
66.0k
        BIO_printf(bp, "%*s%s:\n", indent, "", title);
149
66.0k
        indent += 4;
150
66.0k
    }
151
152
2.25M
    for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
153
2.18M
        const ASN1_OBJECT *obj;
154
2.18M
        const X509_EXTENSION *ex;
155
156
2.18M
        ex = sk_X509_EXTENSION_value(exts, i);
157
2.18M
        obj = X509_EXTENSION_get_object(ex);
158
2.18M
        if ((flag & X509_FLAG_EXTENSIONS_ONLY_KID) != 0
159
0
            && OBJ_obj2nid(obj) != NID_subject_key_identifier
160
0
            && OBJ_obj2nid(obj) != NID_authority_key_identifier)
161
0
            continue;
162
2.18M
        if (indent && BIO_printf(bp, "%*s", indent, "") <= 0)
163
0
            return 0;
164
2.18M
        i2a_ASN1_OBJECT(bp, obj);
165
2.18M
        j = X509_EXTENSION_get_critical(ex);
166
2.18M
        if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
167
0
            return 0;
168
2.18M
        if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
169
1.47M
            BIO_printf(bp, "%*s", indent + 4, "");
170
1.47M
            ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
171
1.47M
        }
172
2.18M
        if (BIO_write(bp, "\n", 1) <= 0)
173
0
            return 0;
174
2.18M
    }
175
66.0k
    return 1;
176
66.0k
}
177
178
static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
179
    unsigned long flag, int indent, int supported)
180
1.41M
{
181
1.41M
    switch (flag & X509V3_EXT_UNKNOWN_MASK) {
182
183
1.40M
    case X509V3_EXT_DEFAULT:
184
1.40M
        return 0;
185
186
0
    case X509V3_EXT_ERROR_UNKNOWN:
187
0
        if (supported)
188
0
            BIO_printf(out, "%*s<Parse Error>", indent, "");
189
0
        else
190
0
            BIO_printf(out, "%*s<Not Supported>", indent, "");
191
0
        return 1;
192
193
0
    case X509V3_EXT_PARSE_UNKNOWN:
194
0
        return ASN1_parse_dump(out, ext, extlen, indent, -1) > 0;
195
0
    case X509V3_EXT_DUMP_UNKNOWN:
196
0
        return BIO_dump_indent(out, (const char *)ext, extlen, indent) > 0;
197
198
8.97k
    default:
199
8.97k
        return 1;
200
1.41M
    }
201
1.41M
}
202
203
#ifndef OPENSSL_NO_STDIO
204
int X509V3_EXT_print_fp(FILE *fp, const X509_EXTENSION *ext, int flag, int indent)
205
0
{
206
0
    BIO *bio_tmp;
207
0
    int ret;
208
209
0
    if ((bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
210
0
        return 0;
211
0
    ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
212
0
    BIO_free(bio_tmp);
213
0
    return ret;
214
0
}
215
#endif