v3_prn.c revision 59191
138889Sjdp/* v3_prn.c */
2218822Sdim/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3130561Sobrien * project 1999.
438889Sjdp */
538889Sjdp/* ====================================================================
685815Sobrien * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
738889Sjdp *
885815Sobrien * Redistribution and use in source and binary forms, with or without
985815Sobrien * modification, are permitted provided that the following conditions
1085815Sobrien * are met:
1185815Sobrien *
1238889Sjdp * 1. Redistributions of source code must retain the above copyright
1385815Sobrien *    notice, this list of conditions and the following disclaimer.
1485815Sobrien *
1585815Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1685815Sobrien *    notice, this list of conditions and the following disclaimer in
1738889Sjdp *    the documentation and/or other materials provided with the
1885815Sobrien *    distribution.
1985815Sobrien *
20218822Sdim * 3. All advertising materials mentioning features or use of this
21218822Sdim *    software must display the following acknowledgment:
2238889Sjdp *    "This product includes software developed by the OpenSSL Project
23218822Sdim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2438889Sjdp *
2538889Sjdp * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2638889Sjdp *    endorse or promote products derived from this software without
2738889Sjdp *    prior written permission. For written permission, please contact
2889857Sobrien *    licensing@OpenSSL.org.
2938889Sjdp *
3038889Sjdp * 5. Products derived from this software may not be called "OpenSSL"
3138889Sjdp *    nor may "OpenSSL" appear in their names without prior written
3285815Sobrien *    permission of the OpenSSL Project.
33130561Sobrien *
3438889Sjdp * 6. Redistributions of any form whatsoever must retain the following
35218822Sdim *    acknowledgment:
36218822Sdim *    "This product includes software developed by the OpenSSL Project
37218822Sdim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38218822Sdim *
39218822Sdim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40218822Sdim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41218822Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42218822Sdim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43218822Sdim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44218822Sdim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45218822Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46218822Sdim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47218822Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48218822Sdim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49218822Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50218822Sdim * OF THE POSSIBILITY OF SUCH DAMAGE.
51218822Sdim * ====================================================================
52218822Sdim *
53218822Sdim * This product includes cryptographic software written by Eric Young
54218822Sdim * (eay@cryptsoft.com).  This product includes software written by Tim
55218822Sdim * Hudson (tjh@cryptsoft.com).
56218822Sdim *
5738889Sjdp */
5838889Sjdp/* X509 v3 extension utilities */
5938889Sjdp
60218822Sdim#include <stdio.h>
61218822Sdim#include "cryptlib.h"
62218822Sdim#include <openssl/conf.h>
63218822Sdim#include <openssl/x509v3.h>
64218822Sdim
65218822Sdim/* Extension printing routines */
66218822Sdim
67218822Sdim/* Print out a name+value stack */
68218822Sdim
69218822Sdimvoid X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, int ml)
70218822Sdim{
71218822Sdim	int i;
72218822Sdim	CONF_VALUE *nval;
7338889Sjdp	if(!val) return;
7438889Sjdp	if(!ml || !sk_CONF_VALUE_num(val)) {
75218822Sdim		BIO_printf(out, "%*s", indent, "");
76218822Sdim		if(!sk_CONF_VALUE_num(val)) BIO_puts(out, "<EMPTY>\n");
77218822Sdim	}
78218822Sdim	for(i = 0; i < sk_CONF_VALUE_num(val); i++) {
79218822Sdim		if(ml) BIO_printf(out, "%*s", indent, "");
80218822Sdim		else if(i > 0) BIO_printf(out, ", ");
81218822Sdim		nval = sk_CONF_VALUE_value(val, i);
82218822Sdim		if(!nval->name) BIO_puts(out, nval->value);
83218822Sdim		else if(!nval->value) BIO_puts(out, nval->name);
84218822Sdim#ifndef CHARSET_EBCDIC
85218822Sdim		else BIO_printf(out, "%s:%s", nval->name, nval->value);
86218822Sdim#else
87218822Sdim		else {
8838889Sjdp			char tmp[10240]; /* 10k is BIO_printf's limit anyway */
8938889Sjdp			ascii2ebcdic(tmp, nval->value, strlen(nval->value)+1);
90218822Sdim			BIO_printf(out, "%s:%s", nval->name, tmp);
91218822Sdim		}
92218822Sdim#endif
93218822Sdim		if(ml) BIO_puts(out, "\n");
94218822Sdim	}
95218822Sdim}
96218822Sdim
97218822Sdim/* Main routine: print out a general extension */
98218822Sdim
99218822Sdimint X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, int flag, int indent)
100218822Sdim{
101218822Sdim	char *ext_str = NULL, *value = NULL;
102218822Sdim	unsigned char *p;
10338889Sjdp	X509V3_EXT_METHOD *method;
10438889Sjdp	STACK_OF(CONF_VALUE) *nval = NULL;
105218822Sdim	int ok = 1;
106218822Sdim	if(!(method = X509V3_EXT_get(ext))) return 0;
107218822Sdim	p = ext->value->data;
108218822Sdim	if(!(ext_str = method->d2i(NULL, &p, ext->value->length))) return 0;
109218822Sdim	if(method->i2s) {
110218822Sdim		if(!(value = method->i2s(method, ext_str))) {
111218822Sdim			ok = 0;
112218822Sdim			goto err;
113218822Sdim		}
114218822Sdim#ifndef CHARSET_EBCDIC
115218822Sdim		BIO_printf(out, "%*s%s", indent, "", value);
116218822Sdim#else
117218822Sdim		{
11838889Sjdp			char tmp[10240]; /* 10k is BIO_printf's limit anyway */
11938889Sjdp			ascii2ebcdic(tmp, value, strlen(value)+1);
12038889Sjdp			BIO_printf(out, "%*s%s", indent, "", tmp);
12138889Sjdp		}
12238889Sjdp#endif
12338889Sjdp	} else if(method->i2v) {
12460484Sobrien		if(!(nval = method->i2v(method, ext_str, NULL))) {
12538889Sjdp			ok = 0;
12638889Sjdp			goto err;
12738889Sjdp		}
12838889Sjdp		X509V3_EXT_val_prn(out, nval, indent,
12938889Sjdp				 method->ext_flags & X509V3_EXT_MULTILINE);
13038889Sjdp	} else if(method->i2r) {
13138889Sjdp		if(!method->i2r(method, ext_str, out, indent)) ok = 0;
13238889Sjdp	} else ok = 0;
13338889Sjdp
13438889Sjdp	err:
13538889Sjdp		sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
13638889Sjdp		if(value) Free(value);
13738889Sjdp		method->ext_free(ext_str);
138218822Sdim		return ok;
139218822Sdim}
14038889Sjdp
14138889Sjdp#ifndef NO_FP_API
14238889Sjdpint X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
14389857Sobrien{
14489857Sobrien	BIO *bio_tmp;
14589857Sobrien	int ret;
14689857Sobrien	if(!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) return 0;
14738889Sjdp	ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
14838889Sjdp	BIO_free(bio_tmp);
14938889Sjdp	return ret;
150218822Sdim}
151218822Sdim#endif
152218822Sdim