Coverage Report

Created: 2023-09-25 06:41

/src/openssl111/crypto/x509v3/v3_prn.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
6.08k
{
27
6.08k
    int i;
28
6.08k
    CONF_VALUE *nval;
29
6.08k
    if (!val)
30
0
        return;
31
6.08k
    if (!ml || !sk_CONF_VALUE_num(val)) {
32
4.82k
        BIO_printf(out, "%*s", indent, "");
33
4.82k
        if (!sk_CONF_VALUE_num(val))
34
588
            BIO_puts(out, "<EMPTY>\n");
35
4.82k
    }
36
24.1k
    for (i = 0; i < sk_CONF_VALUE_num(val); i++) {
37
18.0k
        if (ml)
38
2.76k
            BIO_printf(out, "%*s", indent, "");
39
15.2k
        else if (i > 0)
40
11.0k
            BIO_printf(out, ", ");
41
18.0k
        nval = sk_CONF_VALUE_value(val, i);
42
18.0k
        if (!nval->name)
43
7.24k
            BIO_puts(out, nval->value);
44
10.8k
        else if (!nval->value)
45
6.25k
            BIO_puts(out, nval->name);
46
4.56k
#ifndef CHARSET_EBCDIC
47
4.56k
        else
48
4.56k
            BIO_printf(out, "%s:%s", nval->name, nval->value);
49
#else
50
        else {
51
            int len;
52
            char *tmp;
53
            len = strlen(nval->value) + 1;
54
            tmp = OPENSSL_malloc(len);
55
            if (tmp != NULL) {
56
                ascii2ebcdic(tmp, nval->value, len);
57
                BIO_printf(out, "%s:%s", nval->name, tmp);
58
                OPENSSL_free(tmp);
59
            }
60
        }
61
#endif
62
18.0k
        if (ml)
63
2.76k
            BIO_puts(out, "\n");
64
18.0k
    }
65
6.08k
}
66
67
/* Main routine: print out a general extension */
68
69
int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
70
                     int indent)
71
105k
{
72
105k
    void *ext_str = NULL;
73
105k
    char *value = NULL;
74
105k
    ASN1_OCTET_STRING *extoct;
75
105k
    const unsigned char *p;
76
105k
    int extlen;
77
105k
    const X509V3_EXT_METHOD *method;
78
105k
    STACK_OF(CONF_VALUE) *nval = NULL;
79
105k
    int ok = 1;
80
81
105k
    extoct = X509_EXTENSION_get_data(ext);
82
105k
    p = ASN1_STRING_get0_data(extoct);
83
105k
    extlen = ASN1_STRING_length(extoct);
84
85
105k
    if ((method = X509V3_EXT_get(ext)) == NULL)
86
10.9k
        return unknown_ext_print(out, p, extlen, flag, indent, 0);
87
94.7k
    if (method->it)
88
81.4k
        ext_str = ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
89
13.2k
    else
90
13.2k
        ext_str = method->d2i(NULL, &p, extlen);
91
92
94.7k
    if (!ext_str)
93
54.1k
        return unknown_ext_print(out, p, extlen, flag, indent, 1);
94
95
40.6k
    if (method->i2s) {
96
3.69k
        if ((value = method->i2s(method, ext_str)) == NULL) {
97
194
            ok = 0;
98
194
            goto err;
99
194
        }
100
3.50k
#ifndef CHARSET_EBCDIC
101
3.50k
        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
36.9k
    } else if (method->i2v) {
116
8.77k
        if ((nval = method->i2v(method, ext_str, NULL)) == NULL) {
117
2.69k
            ok = 0;
118
2.69k
            goto err;
119
2.69k
        }
120
6.08k
        X509V3_EXT_val_prn(out, nval, indent,
121
6.08k
                           method->ext_flags & X509V3_EXT_MULTILINE);
122
28.1k
    } else if (method->i2r) {
123
28.1k
        if (!method->i2r(method, ext_str, out, indent))
124
593
            ok = 0;
125
28.1k
    } else
126
0
        ok = 0;
127
128
40.6k
 err:
129
40.6k
    sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
130
40.6k
    OPENSSL_free(value);
131
40.6k
    if (method->it)
132
33.4k
        ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
133
7.14k
    else
134
7.14k
        method->ext_free(ext_str);
135
40.6k
    return ok;
136
40.6k
}
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
23.1k
{
142
23.1k
    int i, j;
143
144
23.1k
    if (sk_X509_EXTENSION_num(exts) <= 0)
145
14.0k
        return 1;
146
147
9.08k
    if (title) {
148
9.08k
        BIO_printf(bp, "%*s%s:\n", indent, "", title);
149
9.08k
        indent += 4;
150
9.08k
    }
151
152
114k
    for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
153
105k
        ASN1_OBJECT *obj;
154
105k
        X509_EXTENSION *ex;
155
105k
        ex = sk_X509_EXTENSION_value(exts, i);
156
105k
        if (indent && BIO_printf(bp, "%*s", indent, "") <= 0)
157
0
            return 0;
158
105k
        obj = X509_EXTENSION_get_object(ex);
159
105k
        i2a_ASN1_OBJECT(bp, obj);
160
105k
        j = X509_EXTENSION_get_critical(ex);
161
105k
        if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
162
0
            return 0;
163
105k
        if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
164
68.5k
            BIO_printf(bp, "%*s", indent + 4, "");
165
68.5k
            ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
166
68.5k
        }
167
105k
        if (BIO_write(bp, "\n", 1) <= 0)
168
0
            return 0;
169
105k
    }
170
9.08k
    return 1;
171
9.08k
}
172
173
static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
174
                             unsigned long flag, int indent, int supported)
175
65.0k
{
176
65.0k
    switch (flag & X509V3_EXT_UNKNOWN_MASK) {
177
178
65.0k
    case X509V3_EXT_DEFAULT:
179
65.0k
        return 0;
180
181
0
    case X509V3_EXT_ERROR_UNKNOWN:
182
0
        if (supported)
183
0
            BIO_printf(out, "%*s<Parse Error>", indent, "");
184
0
        else
185
0
            BIO_printf(out, "%*s<Not Supported>", indent, "");
186
0
        return 1;
187
188
0
    case X509V3_EXT_PARSE_UNKNOWN:
189
0
        return ASN1_parse_dump(out, ext, extlen, indent, -1);
190
0
    case X509V3_EXT_DUMP_UNKNOWN:
191
0
        return BIO_dump_indent(out, (const char *)ext, extlen, indent);
192
193
0
    default:
194
0
        return 1;
195
65.0k
    }
196
65.0k
}
197
198
#ifndef OPENSSL_NO_STDIO
199
int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
200
0
{
201
0
    BIO *bio_tmp;
202
0
    int ret;
203
204
0
    if ((bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
205
0
        return 0;
206
0
    ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
207
0
    BIO_free(bio_tmp);
208
0
    return ret;
209
0
}
210
#endif