Coverage Report

Created: 2025-08-28 06:59

/src/boringssl/crypto/x509/v3_prn.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
// X509 v3 extension utilities
16
17
#include <stdio.h>
18
19
#include <openssl/bio.h>
20
#include <openssl/conf.h>
21
#include <openssl/mem.h>
22
#include <openssl/x509.h>
23
24
// Extension printing routines
25
26
static int unknown_ext_print(BIO *out, const X509_EXTENSION *ext,
27
                             unsigned long flag, int indent, int supported);
28
29
// Print out a name+value stack
30
static void X509V3_EXT_val_prn(BIO *out, const STACK_OF(CONF_VALUE) *val,
31
2.54k
                               int indent, int ml) {
32
2.54k
  if (!val) {
33
0
    return;
34
0
  }
35
2.54k
  if (!ml || !sk_CONF_VALUE_num(val)) {
36
2.27k
    BIO_printf(out, "%*s", indent, "");
37
2.27k
    if (!sk_CONF_VALUE_num(val)) {
38
214
      BIO_puts(out, "<EMPTY>\n");
39
214
    }
40
2.27k
  }
41
18.3k
  for (size_t i = 0; i < sk_CONF_VALUE_num(val); i++) {
42
15.8k
    if (ml) {
43
463
      BIO_printf(out, "%*s", indent, "");
44
15.3k
    } else if (i > 0) {
45
13.3k
      BIO_printf(out, ", ");
46
13.3k
    }
47
15.8k
    const CONF_VALUE *nval = sk_CONF_VALUE_value(val, i);
48
15.8k
    if (!nval->name) {
49
779
      BIO_puts(out, nval->value);
50
15.0k
    } else if (!nval->value) {
51
6.30k
      BIO_puts(out, nval->name);
52
8.75k
    } else {
53
8.75k
      BIO_printf(out, "%s:%s", nval->name, nval->value);
54
8.75k
    }
55
15.8k
    if (ml) {
56
463
      BIO_puts(out, "\n");
57
463
    }
58
15.8k
  }
59
2.54k
}
60
61
// Main routine: print out a general extension
62
63
int X509V3_EXT_print(BIO *out, const X509_EXTENSION *ext, unsigned long flag,
64
26.9k
                     int indent) {
65
26.9k
  const X509V3_EXT_METHOD *method = X509V3_EXT_get(ext);
66
26.9k
  if (method == NULL) {
67
12.4k
    return unknown_ext_print(out, ext, flag, indent, 0);
68
12.4k
  }
69
14.4k
  const ASN1_STRING *ext_data = X509_EXTENSION_get_data(ext);
70
14.4k
  const unsigned char *p = ASN1_STRING_get0_data(ext_data);
71
14.4k
  void *ext_str = ASN1_item_d2i(NULL, &p, ASN1_STRING_length(ext_data),
72
14.4k
                                ASN1_ITEM_ptr(method->it));
73
14.4k
  if (!ext_str) {
74
7.72k
    return unknown_ext_print(out, ext, flag, indent, 1);
75
7.72k
  }
76
77
6.75k
  char *value = NULL;
78
6.75k
  STACK_OF(CONF_VALUE) *nval = NULL;
79
6.75k
  int ok = 0;
80
6.75k
  if (method->i2s) {
81
2.06k
    if (!(value = method->i2s(method, ext_str))) {
82
15
      goto err;
83
15
    }
84
2.05k
    BIO_printf(out, "%*s%s", indent, "", value);
85
4.68k
  } else if (method->i2v) {
86
3.21k
    if (!(nval = method->i2v(method, ext_str, NULL))) {
87
673
      goto err;
88
673
    }
89
2.54k
    X509V3_EXT_val_prn(out, nval, indent,
90
2.54k
                       method->ext_flags & X509V3_EXT_MULTILINE);
91
2.54k
  } else if (method->i2r) {
92
1.47k
    if (!method->i2r(method, ext_str, out, indent)) {
93
0
      goto err;
94
0
    }
95
1.47k
  } else {
96
0
    OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED);
97
0
    goto err;
98
0
  }
99
100
6.07k
  ok = 1;
101
102
6.75k
err:
103
6.75k
  sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
104
6.75k
  OPENSSL_free(value);
105
6.75k
  ASN1_item_free(reinterpret_cast<ASN1_VALUE *>(ext_str),
106
6.75k
                 ASN1_ITEM_ptr(method->it));
107
6.75k
  return ok;
108
6.07k
}
109
110
int X509V3_extensions_print(BIO *bp, const char *title,
111
                            const STACK_OF(X509_EXTENSION) *exts,
112
2.61k
                            unsigned long flag, int indent) {
113
2.61k
  size_t i;
114
2.61k
  int j;
115
116
2.61k
  if (sk_X509_EXTENSION_num(exts) <= 0) {
117
251
    return 1;
118
251
  }
119
120
2.36k
  if (title) {
121
2.36k
    BIO_printf(bp, "%*s%s:\n", indent, "", title);
122
2.36k
    indent += 4;
123
2.36k
  }
124
125
29.3k
  for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
126
26.9k
    const X509_EXTENSION *ex = sk_X509_EXTENSION_value(exts, i);
127
26.9k
    if (indent && BIO_printf(bp, "%*s", indent, "") <= 0) {
128
0
      return 0;
129
0
    }
130
26.9k
    const ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex);
131
26.9k
    i2a_ASN1_OBJECT(bp, obj);
132
26.9k
    j = X509_EXTENSION_get_critical(ex);
133
26.9k
    if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0) {
134
0
      return 0;
135
0
    }
136
26.9k
    if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
137
20.8k
      BIO_printf(bp, "%*s", indent + 4, "");
138
20.8k
      ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
139
20.8k
    }
140
26.9k
    if (BIO_write(bp, "\n", 1) <= 0) {
141
0
      return 0;
142
0
    }
143
26.9k
  }
144
2.36k
  return 1;
145
2.36k
}
146
147
static int unknown_ext_print(BIO *out, const X509_EXTENSION *ext,
148
20.1k
                             unsigned long flag, int indent, int supported) {
149
20.1k
  switch (flag & X509V3_EXT_UNKNOWN_MASK) {
150
20.1k
    case X509V3_EXT_DEFAULT:
151
20.1k
      return 0;
152
153
0
    case X509V3_EXT_ERROR_UNKNOWN:
154
0
      if (supported) {
155
0
        BIO_printf(out, "%*s<Parse Error>", indent, "");
156
0
      } else {
157
0
        BIO_printf(out, "%*s<Not Supported>", indent, "");
158
0
      }
159
0
      return 1;
160
161
0
    case X509V3_EXT_PARSE_UNKNOWN:
162
0
    case X509V3_EXT_DUMP_UNKNOWN: {
163
0
      const ASN1_STRING *data = X509_EXTENSION_get_data(ext);
164
0
      return BIO_hexdump(out, ASN1_STRING_get0_data(data),
165
0
                         ASN1_STRING_length(data), indent);
166
0
    }
167
168
0
    default:
169
0
      return 1;
170
20.1k
  }
171
20.1k
}
172
173
int X509V3_EXT_print_fp(FILE *fp, const X509_EXTENSION *ext, int flag,
174
0
                        int indent) {
175
0
  BIO *bio_tmp;
176
0
  int ret;
177
0
  if (!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) {
178
0
    return 0;
179
0
  }
180
0
  ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
181
0
  BIO_free(bio_tmp);
182
0
  return ret;
183
0
}