Coverage Report

Created: 2025-09-05 06:13

/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
1.48k
                               int indent, int ml) {
32
1.48k
  if (!val) {
33
0
    return;
34
0
  }
35
1.48k
  if (!ml || !sk_CONF_VALUE_num(val)) {
36
1.22k
    BIO_printf(out, "%*s", indent, "");
37
1.22k
    if (!sk_CONF_VALUE_num(val)) {
38
156
      BIO_puts(out, "<EMPTY>\n");
39
156
    }
40
1.22k
  }
41
12.8k
  for (size_t i = 0; i < sk_CONF_VALUE_num(val); i++) {
42
11.3k
    if (ml) {
43
427
      BIO_printf(out, "%*s", indent, "");
44
10.9k
    } else if (i > 0) {
45
9.85k
      BIO_printf(out, ", ");
46
9.85k
    }
47
11.3k
    const CONF_VALUE *nval = sk_CONF_VALUE_value(val, i);
48
11.3k
    if (!nval->name) {
49
941
      BIO_puts(out, nval->value);
50
10.4k
    } else if (!nval->value) {
51
2.47k
      BIO_puts(out, nval->name);
52
7.93k
    } else {
53
7.93k
      BIO_printf(out, "%s:%s", nval->name, nval->value);
54
7.93k
    }
55
11.3k
    if (ml) {
56
427
      BIO_puts(out, "\n");
57
427
    }
58
11.3k
  }
59
1.48k
}
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
18.9k
                     int indent) {
65
18.9k
  const X509V3_EXT_METHOD *method = X509V3_EXT_get(ext);
66
18.9k
  if (method == NULL) {
67
9.05k
    return unknown_ext_print(out, ext, flag, indent, 0);
68
9.05k
  }
69
9.88k
  const ASN1_STRING *ext_data = X509_EXTENSION_get_data(ext);
70
9.88k
  const unsigned char *p = ASN1_STRING_get0_data(ext_data);
71
9.88k
  void *ext_str = ASN1_item_d2i(NULL, &p, ASN1_STRING_length(ext_data),
72
9.88k
                                ASN1_ITEM_ptr(method->it));
73
9.88k
  if (!ext_str) {
74
5.66k
    return unknown_ext_print(out, ext, flag, indent, 1);
75
5.66k
  }
76
77
4.22k
  char *value = NULL;
78
4.22k
  STACK_OF(CONF_VALUE) *nval = NULL;
79
4.22k
  int ok = 0;
80
4.22k
  if (method->i2s) {
81
1.38k
    if (!(value = method->i2s(method, ext_str))) {
82
12
      goto err;
83
12
    }
84
1.37k
    BIO_printf(out, "%*s%s", indent, "", value);
85
2.83k
  } else if (method->i2v) {
86
1.90k
    if (!(nval = method->i2v(method, ext_str, NULL))) {
87
422
      goto err;
88
422
    }
89
1.48k
    X509V3_EXT_val_prn(out, nval, indent,
90
1.48k
                       method->ext_flags & X509V3_EXT_MULTILINE);
91
1.48k
  } else if (method->i2r) {
92
922
    if (!method->i2r(method, ext_str, out, indent)) {
93
0
      goto err;
94
0
    }
95
922
  } else {
96
0
    OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED);
97
0
    goto err;
98
0
  }
99
100
3.78k
  ok = 1;
101
102
4.22k
err:
103
4.22k
  sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
104
4.22k
  OPENSSL_free(value);
105
4.22k
  ASN1_item_free(reinterpret_cast<ASN1_VALUE *>(ext_str),
106
4.22k
                 ASN1_ITEM_ptr(method->it));
107
4.22k
  return ok;
108
3.78k
}
109
110
int X509V3_extensions_print(BIO *bp, const char *title,
111
                            const STACK_OF(X509_EXTENSION) *exts,
112
2.10k
                            unsigned long flag, int indent) {
113
2.10k
  size_t i;
114
2.10k
  int j;
115
116
2.10k
  if (sk_X509_EXTENSION_num(exts) <= 0) {
117
255
    return 1;
118
255
  }
119
120
1.85k
  if (title) {
121
1.85k
    BIO_printf(bp, "%*s%s:\n", indent, "", title);
122
1.85k
    indent += 4;
123
1.85k
  }
124
125
20.7k
  for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
126
18.9k
    const X509_EXTENSION *ex = sk_X509_EXTENSION_value(exts, i);
127
18.9k
    if (indent && BIO_printf(bp, "%*s", indent, "") <= 0) {
128
0
      return 0;
129
0
    }
130
18.9k
    const ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex);
131
18.9k
    i2a_ASN1_OBJECT(bp, obj);
132
18.9k
    j = X509_EXTENSION_get_critical(ex);
133
18.9k
    if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0) {
134
0
      return 0;
135
0
    }
136
18.9k
    if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
137
15.1k
      BIO_printf(bp, "%*s", indent + 4, "");
138
15.1k
      ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
139
15.1k
    }
140
18.9k
    if (BIO_write(bp, "\n", 1) <= 0) {
141
0
      return 0;
142
0
    }
143
18.9k
  }
144
1.85k
  return 1;
145
1.85k
}
146
147
static int unknown_ext_print(BIO *out, const X509_EXTENSION *ext,
148
14.7k
                             unsigned long flag, int indent, int supported) {
149
14.7k
  switch (flag & X509V3_EXT_UNKNOWN_MASK) {
150
14.7k
    case X509V3_EXT_DEFAULT:
151
14.7k
      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
14.7k
  }
171
14.7k
}
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
}