2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* X509 v3 extension utilities */
13 #include "internal/cryptlib.h"
14 #include <openssl/conf.h>
15 #include <openssl/x509v3.h>
17 /* Extension printing routines */
19 static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
20 unsigned long flag, int indent, int supported);
22 /* Print out a name+value stack */
24 void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent,
31 if (!ml || !sk_CONF_VALUE_num(val)) {
32 BIO_printf(out, "%*s", indent, "");
33 if (!sk_CONF_VALUE_num(val))
34 BIO_puts(out, "<EMPTY>\n");
36 for (i = 0; i < sk_CONF_VALUE_num(val); i++) {
38 BIO_printf(out, "%*s", indent, "");
40 BIO_printf(out, ", ");
41 nval = sk_CONF_VALUE_value(val, i);
43 BIO_puts(out, nval->value);
44 else if (!nval->value)
45 BIO_puts(out, nval->name);
46 #ifndef CHARSET_EBCDIC
48 BIO_printf(out, "%s:%s", nval->name, nval->value);
53 len = strlen(nval->value) + 1;
54 tmp = OPENSSL_malloc(len);
56 ascii2ebcdic(tmp, nval->value, len);
57 BIO_printf(out, "%s:%s", nval->name, tmp);
67 /* Main routine: print out a general extension */
69 int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
74 ASN1_OCTET_STRING *extoct;
75 const unsigned char *p;
77 const X509V3_EXT_METHOD *method;
78 STACK_OF(CONF_VALUE) *nval = NULL;
81 extoct = X509_EXTENSION_get_data(ext);
82 p = ASN1_STRING_get0_data(extoct);
83 extlen = ASN1_STRING_length(extoct);
85 if ((method = X509V3_EXT_get(ext)) == NULL)
86 return unknown_ext_print(out, p, extlen, flag, indent, 0);
88 ext_str = ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
90 ext_str = method->d2i(NULL, &p, extlen);
93 return unknown_ext_print(out, p, extlen, flag, indent, 1);
96 if ((value = method->i2s(method, ext_str)) == NULL) {
100 #ifndef CHARSET_EBCDIC
101 BIO_printf(out, "%*s%s", indent, "", value);
106 len = strlen(value) + 1;
107 tmp = OPENSSL_malloc(len);
109 ascii2ebcdic(tmp, value, len);
110 BIO_printf(out, "%*s%s", indent, "", tmp);
115 } else if (method->i2v) {
116 if ((nval = method->i2v(method, ext_str, NULL)) == NULL) {
120 X509V3_EXT_val_prn(out, nval, indent,
121 method->ext_flags & X509V3_EXT_MULTILINE);
122 } else if (method->i2r) {
123 if (!method->i2r(method, ext_str, out, indent))
129 sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
132 ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
134 method->ext_free(ext_str);
138 int X509V3_extensions_print(BIO *bp, const char *title,
139 const STACK_OF(X509_EXTENSION) *exts,
140 unsigned long flag, int indent)
144 if (sk_X509_EXTENSION_num(exts) <= 0)
148 BIO_printf(bp, "%*s%s:\n", indent, "", title);
152 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
155 ex = sk_X509_EXTENSION_value(exts, i);
156 if (indent && BIO_printf(bp, "%*s", indent, "") <= 0)
158 obj = X509_EXTENSION_get_object(ex);
159 i2a_ASN1_OBJECT(bp, obj);
160 j = X509_EXTENSION_get_critical(ex);
161 if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
163 if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
164 BIO_printf(bp, "%*s", indent + 4, "");
165 ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
167 if (BIO_write(bp, "\n", 1) <= 0)
173 static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
174 unsigned long flag, int indent, int supported)
176 switch (flag & X509V3_EXT_UNKNOWN_MASK) {
178 case X509V3_EXT_DEFAULT:
181 case X509V3_EXT_ERROR_UNKNOWN:
183 BIO_printf(out, "%*s<Parse Error>", indent, "");
185 BIO_printf(out, "%*s<Not Supported>", indent, "");
188 case X509V3_EXT_PARSE_UNKNOWN:
189 return ASN1_parse_dump(out, ext, extlen, indent, -1);
190 case X509V3_EXT_DUMP_UNKNOWN:
191 return BIO_dump_indent(out, (const char *)ext, extlen, indent);
198 #ifndef OPENSSL_NO_STDIO
199 int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
204 if ((bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
206 ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);