2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* X509 v3 extension utilities */
13 #include "internal/cryptlib.h"
14 #include <openssl/conf.h>
15 #include <openssl/x509v3.h>
17 DEFINE_STACK_OF(CONF_VALUE)
18 DEFINE_STACK_OF(X509_EXTENSION)
20 /* Extension printing routines */
22 static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
23 unsigned long flag, int indent, int supported);
25 /* Print out a name+value stack */
27 void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent,
34 if (!ml || !sk_CONF_VALUE_num(val)) {
35 BIO_printf(out, "%*s", indent, "");
36 if (!sk_CONF_VALUE_num(val))
37 BIO_puts(out, "<EMPTY>\n");
39 for (i = 0; i < sk_CONF_VALUE_num(val); i++) {
42 BIO_printf(out, "\n");
43 BIO_printf(out, "%*s", indent, "");
46 BIO_printf(out, ", ");
47 nval = sk_CONF_VALUE_value(val, i);
49 BIO_puts(out, nval->value);
50 else if (!nval->value)
51 BIO_puts(out, nval->name);
52 #ifndef CHARSET_EBCDIC
54 BIO_printf(out, "%s:%s", nval->name, nval->value);
59 len = strlen(nval->value) + 1;
60 tmp = OPENSSL_malloc(len);
62 ascii2ebcdic(tmp, nval->value, len);
63 BIO_printf(out, "%s:%s", nval->name, tmp);
71 /* Main routine: print out a general extension */
73 int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
78 ASN1_OCTET_STRING *extoct;
79 const unsigned char *p;
81 const X509V3_EXT_METHOD *method;
82 STACK_OF(CONF_VALUE) *nval = NULL;
85 extoct = X509_EXTENSION_get_data(ext);
86 p = ASN1_STRING_get0_data(extoct);
87 extlen = ASN1_STRING_length(extoct);
89 if ((method = X509V3_EXT_get(ext)) == NULL)
90 return unknown_ext_print(out, p, extlen, flag, indent, 0);
92 ext_str = ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
94 ext_str = method->d2i(NULL, &p, extlen);
97 return unknown_ext_print(out, p, extlen, flag, indent, 1);
100 if ((value = method->i2s(method, ext_str)) == NULL) {
104 #ifndef CHARSET_EBCDIC
105 BIO_printf(out, "%*s%s", indent, "", value);
110 len = strlen(value) + 1;
111 tmp = OPENSSL_malloc(len);
113 ascii2ebcdic(tmp, value, len);
114 BIO_printf(out, "%*s%s", indent, "", tmp);
119 } else if (method->i2v) {
120 if ((nval = method->i2v(method, ext_str, NULL)) == NULL) {
124 X509V3_EXT_val_prn(out, nval, indent,
125 method->ext_flags & X509V3_EXT_MULTILINE);
126 } else if (method->i2r) {
127 if (!method->i2r(method, ext_str, out, indent))
133 sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
136 ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
138 method->ext_free(ext_str);
142 int X509V3_extensions_print(BIO *bp, const char *title,
143 const STACK_OF(X509_EXTENSION) *exts,
144 unsigned long flag, int indent)
148 if (sk_X509_EXTENSION_num(exts) <= 0)
152 BIO_printf(bp, "%*s%s:\n", indent, "", title);
156 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
159 ex = sk_X509_EXTENSION_value(exts, i);
160 if (indent && BIO_printf(bp, "%*s", indent, "") <= 0)
162 obj = X509_EXTENSION_get_object(ex);
163 i2a_ASN1_OBJECT(bp, obj);
164 j = X509_EXTENSION_get_critical(ex);
165 if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
167 if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
168 BIO_printf(bp, "%*s", indent + 4, "");
169 ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
171 if (BIO_write(bp, "\n", 1) <= 0)
177 static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
178 unsigned long flag, int indent, int supported)
180 switch (flag & X509V3_EXT_UNKNOWN_MASK) {
182 case X509V3_EXT_DEFAULT:
185 case X509V3_EXT_ERROR_UNKNOWN:
187 BIO_printf(out, "%*s<Parse Error>", indent, "");
189 BIO_printf(out, "%*s<Not Supported>", indent, "");
192 case X509V3_EXT_PARSE_UNKNOWN:
193 return ASN1_parse_dump(out, ext, extlen, indent, -1);
194 case X509V3_EXT_DUMP_UNKNOWN:
195 return BIO_dump_indent(out, (const char *)ext, extlen, indent);
202 #ifndef OPENSSL_NO_STDIO
203 int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
208 if ((bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
210 ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);