xref: /openbsd/lib/libcrypto/x509/x509_prn.c (revision 03004f8f)
1*03004f8fStb /* $OpenBSD: x509_prn.c,v 1.6 2023/05/08 05:30:38 tb Exp $ */
2e500e238Sjsing /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3e500e238Sjsing  * project 1999.
4e500e238Sjsing  */
5e500e238Sjsing /* ====================================================================
6e500e238Sjsing  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7e500e238Sjsing  *
8e500e238Sjsing  * Redistribution and use in source and binary forms, with or without
9e500e238Sjsing  * modification, are permitted provided that the following conditions
10e500e238Sjsing  * are met:
11e500e238Sjsing  *
12e500e238Sjsing  * 1. Redistributions of source code must retain the above copyright
13e500e238Sjsing  *    notice, this list of conditions and the following disclaimer.
14e500e238Sjsing  *
15e500e238Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
16e500e238Sjsing  *    notice, this list of conditions and the following disclaimer in
17e500e238Sjsing  *    the documentation and/or other materials provided with the
18e500e238Sjsing  *    distribution.
19e500e238Sjsing  *
20e500e238Sjsing  * 3. All advertising materials mentioning features or use of this
21e500e238Sjsing  *    software must display the following acknowledgment:
22e500e238Sjsing  *    "This product includes software developed by the OpenSSL Project
23e500e238Sjsing  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24e500e238Sjsing  *
25e500e238Sjsing  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26e500e238Sjsing  *    endorse or promote products derived from this software without
27e500e238Sjsing  *    prior written permission. For written permission, please contact
28e500e238Sjsing  *    licensing@OpenSSL.org.
29e500e238Sjsing  *
30e500e238Sjsing  * 5. Products derived from this software may not be called "OpenSSL"
31e500e238Sjsing  *    nor may "OpenSSL" appear in their names without prior written
32e500e238Sjsing  *    permission of the OpenSSL Project.
33e500e238Sjsing  *
34e500e238Sjsing  * 6. Redistributions of any form whatsoever must retain the following
35e500e238Sjsing  *    acknowledgment:
36e500e238Sjsing  *    "This product includes software developed by the OpenSSL Project
37e500e238Sjsing  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38e500e238Sjsing  *
39e500e238Sjsing  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40e500e238Sjsing  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41e500e238Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42e500e238Sjsing  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43e500e238Sjsing  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44e500e238Sjsing  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45e500e238Sjsing  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46e500e238Sjsing  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47e500e238Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48e500e238Sjsing  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49e500e238Sjsing  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50e500e238Sjsing  * OF THE POSSIBILITY OF SUCH DAMAGE.
51e500e238Sjsing  * ====================================================================
52e500e238Sjsing  *
53e500e238Sjsing  * This product includes cryptographic software written by Eric Young
54e500e238Sjsing  * (eay@cryptsoft.com).  This product includes software written by Tim
55e500e238Sjsing  * Hudson (tjh@cryptsoft.com).
56e500e238Sjsing  *
57e500e238Sjsing  */
58e500e238Sjsing /* X509 v3 extension utilities */
59e500e238Sjsing 
60e500e238Sjsing #include <stdio.h>
61e500e238Sjsing 
62e500e238Sjsing #include <openssl/conf.h>
63e500e238Sjsing #include <openssl/x509v3.h>
64e500e238Sjsing 
65c9675a23Stb #include "x509_local.h"
66838f0b6dStb 
67e500e238Sjsing /* Extension printing routines */
68e500e238Sjsing 
69e500e238Sjsing static int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
70e500e238Sjsing     int indent, int supported);
71e500e238Sjsing 
72e500e238Sjsing /* Print out a name+value stack */
73e500e238Sjsing 
74e500e238Sjsing void
X509V3_EXT_val_prn(BIO * out,STACK_OF (CONF_VALUE)* val,int indent,int ml)75e500e238Sjsing X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, int ml)
76e500e238Sjsing {
77e500e238Sjsing 	int i;
78e500e238Sjsing 	CONF_VALUE *nval;
79e500e238Sjsing 
80e500e238Sjsing 	if (!val)
81e500e238Sjsing 		return;
82e500e238Sjsing 	if (!ml || !sk_CONF_VALUE_num(val)) {
83e500e238Sjsing 		BIO_printf(out, "%*s", indent, "");
84e500e238Sjsing 		if (!sk_CONF_VALUE_num(val))
85e500e238Sjsing 			BIO_puts(out, "<EMPTY>\n");
86e500e238Sjsing 	}
87e500e238Sjsing 	for (i = 0; i < sk_CONF_VALUE_num(val); i++) {
88e500e238Sjsing 		if (ml)
89e500e238Sjsing 			BIO_printf(out, "%*s", indent, "");
90e500e238Sjsing 		else if (i > 0) BIO_printf(out, ", ");
91e500e238Sjsing 			nval = sk_CONF_VALUE_value(val, i);
92e500e238Sjsing 		if (!nval->name)
93e500e238Sjsing 			BIO_puts(out, nval->value);
94e500e238Sjsing 		else if (!nval->value)
95e500e238Sjsing 			BIO_puts(out, nval->name);
96e500e238Sjsing 		else
97e500e238Sjsing 			BIO_printf(out, "%s:%s", nval->name, nval->value);
98e500e238Sjsing 		if (ml)
99e500e238Sjsing 			BIO_puts(out, "\n");
100e500e238Sjsing 	}
101e500e238Sjsing }
102cedac418Stb LCRYPTO_ALIAS(X509V3_EXT_val_prn);
103e500e238Sjsing 
104e500e238Sjsing /* Main routine: print out a general extension */
105e500e238Sjsing 
106e500e238Sjsing int
X509V3_EXT_print(BIO * out,X509_EXTENSION * ext,unsigned long flag,int indent)107e500e238Sjsing X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent)
108e500e238Sjsing {
109e500e238Sjsing 	void *ext_str = NULL;
110e500e238Sjsing 	char *value = NULL;
111e500e238Sjsing 	const unsigned char *p;
112e500e238Sjsing 	const X509V3_EXT_METHOD *method;
113e500e238Sjsing 	STACK_OF(CONF_VALUE) *nval = NULL;
114e500e238Sjsing 	int ok = 1;
115e500e238Sjsing 
116e500e238Sjsing 	if (!(method = X509V3_EXT_get(ext)))
117e500e238Sjsing 		return unknown_ext_print(out, ext, flag, indent, 0);
118e500e238Sjsing 	p = ext->value->data;
119e500e238Sjsing 	if (method->it)
120e500e238Sjsing 		ext_str = ASN1_item_d2i(NULL, &p, ext->value->length,
121e500e238Sjsing 		    method->it);
122e500e238Sjsing 	else
123e500e238Sjsing 		ext_str = method->d2i(NULL, &p, ext->value->length);
124e500e238Sjsing 
125e500e238Sjsing 	if (!ext_str)
126e500e238Sjsing 		return unknown_ext_print(out, ext, flag, indent, 1);
127e500e238Sjsing 
128e500e238Sjsing 	if (method->i2s) {
129e500e238Sjsing 		if (!(value = method->i2s(method, ext_str))) {
130e500e238Sjsing 			ok = 0;
131e500e238Sjsing 			goto err;
132e500e238Sjsing 		}
133e500e238Sjsing 		BIO_printf(out, "%*s%s", indent, "", value);
134e500e238Sjsing 	} else if (method->i2v) {
135e500e238Sjsing 		if (!(nval = method->i2v(method, ext_str, NULL))) {
136e500e238Sjsing 			ok = 0;
137e500e238Sjsing 			goto err;
138e500e238Sjsing 		}
139e500e238Sjsing 		X509V3_EXT_val_prn(out, nval, indent,
140e500e238Sjsing 		    method->ext_flags & X509V3_EXT_MULTILINE);
141e500e238Sjsing 	} else if (method->i2r) {
142e500e238Sjsing 		if (!method->i2r(method, ext_str, out, indent))
143e500e238Sjsing 			ok = 0;
144e500e238Sjsing 	} else
145e500e238Sjsing 		ok = 0;
146e500e238Sjsing 
147e500e238Sjsing err:
148e500e238Sjsing 	sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
149e500e238Sjsing 	free(value);
150e500e238Sjsing 	if (method->it)
151e500e238Sjsing 		ASN1_item_free(ext_str, method->it);
152e500e238Sjsing 	else
153e500e238Sjsing 		method->ext_free(ext_str);
154e500e238Sjsing 	return ok;
155e500e238Sjsing }
156cedac418Stb LCRYPTO_ALIAS(X509V3_EXT_print);
157e500e238Sjsing 
158e500e238Sjsing int
X509V3_extensions_print(BIO * bp,const char * title,const STACK_OF (X509_EXTENSION)* exts,unsigned long flag,int indent)159e500e238Sjsing X509V3_extensions_print(BIO *bp, const char *title,
160e500e238Sjsing     const STACK_OF(X509_EXTENSION) *exts, unsigned long flag, int indent)
161e500e238Sjsing {
162e500e238Sjsing 	int i, j;
163e500e238Sjsing 
164e500e238Sjsing 	if (sk_X509_EXTENSION_num(exts) <= 0)
165e500e238Sjsing 		return 1;
166e500e238Sjsing 
167e500e238Sjsing 	if (title) {
168e500e238Sjsing 		BIO_printf(bp, "%*s%s:\n",indent, "", title);
169e500e238Sjsing 		indent += 4;
170e500e238Sjsing 	}
171e500e238Sjsing 
172e500e238Sjsing 	for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
173e500e238Sjsing 		ASN1_OBJECT *obj;
174e500e238Sjsing 		X509_EXTENSION *ex;
175e500e238Sjsing 		ex = sk_X509_EXTENSION_value(exts, i);
176e500e238Sjsing 		if (indent && BIO_printf(bp, "%*s",indent, "") <= 0)
177e500e238Sjsing 			return 0;
178e500e238Sjsing 		obj = X509_EXTENSION_get_object(ex);
179e500e238Sjsing 		i2a_ASN1_OBJECT(bp, obj);
180e500e238Sjsing 		j = X509_EXTENSION_get_critical(ex);
181e500e238Sjsing 		if (BIO_printf(bp, ":%s\n", j ? " critical" : "") <= 0)
182e500e238Sjsing 			return 0;
183e500e238Sjsing 		if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
184e500e238Sjsing 			BIO_printf(bp, "%*s", indent + 4, "");
185e500e238Sjsing 			ASN1_STRING_print(bp, ex->value);
186e500e238Sjsing 		}
187e500e238Sjsing 		if (BIO_write(bp, "\n",1) <= 0)
188e500e238Sjsing 			return 0;
189e500e238Sjsing 	}
190e500e238Sjsing 	return 1;
191e500e238Sjsing }
192cedac418Stb LCRYPTO_ALIAS(X509V3_extensions_print);
193e500e238Sjsing 
194e500e238Sjsing static int
unknown_ext_print(BIO * out,X509_EXTENSION * ext,unsigned long flag,int indent,int supported)195e500e238Sjsing unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
196e500e238Sjsing     int indent, int supported)
197e500e238Sjsing {
198e500e238Sjsing 	switch (flag & X509V3_EXT_UNKNOWN_MASK) {
199e500e238Sjsing 	case X509V3_EXT_DEFAULT:
200e500e238Sjsing 		return 0;
201e500e238Sjsing 	case X509V3_EXT_ERROR_UNKNOWN:
202e500e238Sjsing 		if (supported)
203e500e238Sjsing 			BIO_printf(out, "%*s<Parse Error>", indent, "");
204e500e238Sjsing 		else
205e500e238Sjsing 			BIO_printf(out, "%*s<Not Supported>", indent, "");
206e500e238Sjsing 		return 1;
207e500e238Sjsing 	case X509V3_EXT_PARSE_UNKNOWN:
208e500e238Sjsing 		return ASN1_parse_dump(out,
209e500e238Sjsing 		    ext->value->data, ext->value->length, indent, -1);
210e500e238Sjsing 	case X509V3_EXT_DUMP_UNKNOWN:
211e500e238Sjsing 		return BIO_dump_indent(out, (char *)ext->value->data,
212e500e238Sjsing 		    ext->value->length, indent);
213e500e238Sjsing 	default:
214e500e238Sjsing 		return 1;
215e500e238Sjsing 	}
216e500e238Sjsing }
217e500e238Sjsing 
218e500e238Sjsing 
219e500e238Sjsing int
X509V3_EXT_print_fp(FILE * fp,X509_EXTENSION * ext,int flag,int indent)220e500e238Sjsing X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
221e500e238Sjsing {
222e500e238Sjsing 	BIO *bio_tmp;
223e500e238Sjsing 	int ret;
224e500e238Sjsing 
225e500e238Sjsing 	if (!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)))
226e500e238Sjsing 		return 0;
227e500e238Sjsing 	ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
228e500e238Sjsing 	BIO_free(bio_tmp);
229e500e238Sjsing 	return ret;
230e500e238Sjsing }
231cedac418Stb LCRYPTO_ALIAS(X509V3_EXT_print_fp);
232