v3_prn.c revision 72613
1116743Ssam/* v3_prn.c */
2186904Ssam/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3116743Ssam * project 1999.
4116743Ssam */
5116743Ssam/* ====================================================================
6116743Ssam * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7116743Ssam *
8116743Ssam * Redistribution and use in source and binary forms, with or without
9116743Ssam * modification, are permitted provided that the following conditions
10116743Ssam * are met:
11116743Ssam *
12116743Ssam * 1. Redistributions of source code must retain the above copyright
13116743Ssam *    notice, this list of conditions and the following disclaimer.
14116743Ssam *
15116743Ssam * 2. Redistributions in binary form must reproduce the above copyright
16116743Ssam *    notice, this list of conditions and the following disclaimer in
17116743Ssam *    the documentation and/or other materials provided with the
18116743Ssam *    distribution.
19116743Ssam *
20116743Ssam * 3. All advertising materials mentioning features or use of this
21116743Ssam *    software must display the following acknowledgment:
22116743Ssam *    "This product includes software developed by the OpenSSL Project
23116743Ssam *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24116743Ssam *
25116743Ssam * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26116743Ssam *    endorse or promote products derived from this software without
27116743Ssam *    prior written permission. For written permission, please contact
28116743Ssam *    licensing@OpenSSL.org.
29116743Ssam *
30116743Ssam * 5. Products derived from this software may not be called "OpenSSL"
31116743Ssam *    nor may "OpenSSL" appear in their names without prior written
32116743Ssam *    permission of the OpenSSL Project.
33116743Ssam *
34116743Ssam * 6. Redistributions of any form whatsoever must retain the following
35116743Ssam *    acknowledgment:
36116743Ssam *    "This product includes software developed by the OpenSSL Project
37116743Ssam *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38185522Ssam *
39185522Ssam * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40119783Ssam * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41116743Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42138570Ssam * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43116743Ssam * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44116743Ssam * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45116743Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46155481Ssam * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47116743Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48155481Ssam * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49155481Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50170530Ssam * OF THE POSSIBILITY OF SUCH DAMAGE.
51155481Ssam * ====================================================================
52178354Ssam *
53178354Ssam * This product includes cryptographic software written by Eric Young
54140438Ssam * (eay@cryptsoft.com).  This product includes software written by Tim
55138570Ssam * Hudson (tjh@cryptsoft.com).
56155480Ssam *
57138570Ssam */
58116743Ssam/* X509 v3 extension utilities */
59147067Ssam
60147067Ssam#include <stdio.h>
61147067Ssam#include "cryptlib.h"
62147067Ssam#include <openssl/conf.h>
63147057Ssam#include <openssl/x509v3.h>
64147057Ssam
65147057Ssam/* Extension printing routines */
66147057Ssam
67147057Ssam/* Print out a name+value stack */
68147057Ssam
69147057Ssamvoid X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, int ml)
70147057Ssam{
71147057Ssam	int i;
72147057Ssam	CONF_VALUE *nval;
73147057Ssam	if(!val) return;
74170530Ssam	if(!ml || !sk_CONF_VALUE_num(val)) {
75170530Ssam		BIO_printf(out, "%*s", indent, "");
76170530Ssam		if(!sk_CONF_VALUE_num(val)) BIO_puts(out, "<EMPTY>\n");
77170530Ssam	}
78138570Ssam	for(i = 0; i < sk_CONF_VALUE_num(val); i++) {
79116743Ssam		if(ml) BIO_printf(out, "%*s", indent, "");
80119150Ssam		else if(i > 0) BIO_printf(out, ", ");
81178354Ssam		nval = sk_CONF_VALUE_value(val, i);
82178354Ssam		if(!nval->name) BIO_puts(out, nval->value);
83170530Ssam		else if(!nval->value) BIO_puts(out, nval->name);
84138570Ssam#ifndef CHARSET_EBCDIC
85116743Ssam		else BIO_printf(out, "%s:%s", nval->name, nval->value);
86138570Ssam#else
87138570Ssam		else {
88116743Ssam			int len;
89138570Ssam			char *tmp;
90138570Ssam			len = strlen(nval->value)+1;
91138570Ssam			tmp = OPENSSL_malloc(len);
92138570Ssam			if (tmp)
93138570Ssam			{
94138570Ssam				ascii2ebcdic(tmp, nval->value, len);
95138570Ssam				BIO_printf(out, "%s:%s", nval->name, tmp);
96138570Ssam				OPENSSL_free(tmp);
97138570Ssam			}
98138570Ssam		}
99184358Ssam#endif
100184358Ssam		if(ml) BIO_puts(out, "\n");
101184358Ssam	}
102138570Ssam}
103116743Ssam
104138570Ssam/* Main routine: print out a general extension */
105116743Ssam
106186904Ssamint X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, int flag, int indent)
107186904Ssam{
108116743Ssam	char *ext_str = NULL, *value = NULL;
109165185Ssam	unsigned char *p;
110116743Ssam	X509V3_EXT_METHOD *method;
111138570Ssam	STACK_OF(CONF_VALUE) *nval = NULL;
112116743Ssam	int ok = 1;
113116743Ssam	if(!(method = X509V3_EXT_get(ext))) return 0;
114116743Ssam	p = ext->value->data;
115140438Ssam	if(!(ext_str = method->d2i(NULL, &p, ext->value->length))) return 0;
116116743Ssam	if(method->i2s) {
117116743Ssam		if(!(value = method->i2s(method, ext_str))) {
118138570Ssam			ok = 0;
119116743Ssam			goto err;
120186904Ssam		}
121186904Ssam#ifndef CHARSET_EBCDIC
122138570Ssam		BIO_printf(out, "%*s%s", indent, "", value);
123138570Ssam#else
124138570Ssam		{
125138570Ssam			int len;
126138570Ssam			char *tmp;
127138570Ssam			len = strlen(value)+1;
128138570Ssam			tmp = OPENSSL_malloc(len);
129158298Ssam			if (tmp)
130138570Ssam			{
131138570Ssam				ascii2ebcdic(tmp, value, len);
132138570Ssam				BIO_printf(out, "%*s%s", indent, "", tmp);
133138570Ssam				OPENSSL_free(tmp);
134138570Ssam			}
135138570Ssam		}
136138570Ssam#endif
137138570Ssam	} else if(method->i2v) {
138138570Ssam		if(!(nval = method->i2v(method, ext_str, NULL))) {
139138570Ssam			ok = 0;
140138570Ssam			goto err;
141138570Ssam		}
142138570Ssam		X509V3_EXT_val_prn(out, nval, indent,
143138570Ssam				 method->ext_flags & X509V3_EXT_MULTILINE);
144138570Ssam	} else if(method->i2r) {
145138570Ssam		if(!method->i2r(method, ext_str, out, indent)) ok = 0;
146138570Ssam	} else ok = 0;
147178354Ssam
148190579Ssam	err:
149186904Ssam		sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
150186904Ssam		if(value) OPENSSL_free(value);
151156073Ssam		method->ext_free(ext_str);
152138570Ssam		return ok;
153138570Ssam}
154138570Ssam
155138570Ssam#ifndef NO_FP_API
156155482Ssamint X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
157138570Ssam{
158138570Ssam	BIO *bio_tmp;
159155482Ssam	int ret;
160155482Ssam	if(!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) return 0;
161155482Ssam	ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
162167252Ssam	BIO_free(bio_tmp);
163161425Simp	return ret;
164138570Ssam}
165138570Ssam#endif
166138570Ssam