v3_prn.c revision 55714
10SN/A/* v3_prn.c */
22362SN/A/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30SN/A * project 1999.
40SN/A */
50SN/A/* ====================================================================
60SN/A * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
72362SN/A *
80SN/A * Redistribution and use in source and binary forms, with or without
92362SN/A * modification, are permitted provided that the following conditions
100SN/A * are met:
110SN/A *
120SN/A * 1. Redistributions of source code must retain the above copyright
130SN/A *    notice, this list of conditions and the following disclaimer.
140SN/A *
150SN/A * 2. Redistributions in binary form must reproduce the above copyright
160SN/A *    notice, this list of conditions and the following disclaimer in
170SN/A *    the documentation and/or other materials provided with the
180SN/A *    distribution.
190SN/A *
200SN/A * 3. All advertising materials mentioning features or use of this
212362SN/A *    software must display the following acknowledgment:
222362SN/A *    "This product includes software developed by the OpenSSL Project
232362SN/A *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240SN/A *
250SN/A * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260SN/A *    endorse or promote products derived from this software without
270SN/A *    prior written permission. For written permission, please contact
280SN/A *    licensing@OpenSSL.org.
290SN/A *
300SN/A * 5. Products derived from this software may not be called "OpenSSL"
310SN/A *    nor may "OpenSSL" appear in their names without prior written
320SN/A *    permission of the OpenSSL Project.
330SN/A *
340SN/A * 6. Redistributions of any form whatsoever must retain the following
350SN/A *    acknowledgment:
360SN/A *    "This product includes software developed by the OpenSSL Project
370SN/A *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380SN/A *
390SN/A * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400SN/A * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420SN/A * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430SN/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450SN/A * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460SN/A * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480SN/A * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490SN/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500SN/A * OF THE POSSIBILITY OF SUCH DAMAGE.
510SN/A * ====================================================================
520SN/A *
530SN/A * This product includes cryptographic software written by Eric Young
540SN/A * (eay@cryptsoft.com).  This product includes software written by Tim
550SN/A * Hudson (tjh@cryptsoft.com).
560SN/A *
570SN/A */
580SN/A/* X509 v3 extension utilities */
590SN/A
600SN/A#include <stdio.h>
610SN/A#include "cryptlib.h"
620SN/A#include <openssl/conf.h>
630SN/A#include <openssl/x509v3.h>
640SN/A
650SN/A/* Extension printing routines */
660SN/A
670SN/A/* Print out a name+value stack */
680SN/A
690SN/Avoid X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, int ml)
700SN/A{
710SN/A	int i;
720SN/A	CONF_VALUE *nval;
730SN/A	if(!val) return;
740SN/A	if(!ml || !sk_CONF_VALUE_num(val)) {
750SN/A		BIO_printf(out, "%*s", indent, "");
760SN/A		if(!sk_CONF_VALUE_num(val)) BIO_puts(out, "<EMPTY>\n");
770SN/A	}
780SN/A	for(i = 0; i < sk_CONF_VALUE_num(val); i++) {
790SN/A		if(ml) BIO_printf(out, "%*s", indent, "");
800SN/A		else if(i > 0) BIO_printf(out, ", ");
810SN/A		nval = sk_CONF_VALUE_value(val, i);
820SN/A		if(!nval->name) BIO_puts(out, nval->value);
830SN/A		else if(!nval->value) BIO_puts(out, nval->name);
840SN/A		else BIO_printf(out, "%s:%s", nval->name, nval->value);
850SN/A		if(ml) BIO_puts(out, "\n");
860SN/A	}
870SN/A}
880SN/A
890SN/A/* Main routine: print out a general extension */
900SN/A
910SN/Aint X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, int flag, int indent)
920SN/A{
930SN/A	char *ext_str = NULL, *value = NULL;
940SN/A	unsigned char *p;
950SN/A	X509V3_EXT_METHOD *method;
960SN/A	STACK_OF(CONF_VALUE) *nval = NULL;
970SN/A	int ok = 1;
980SN/A	if(!(method = X509V3_EXT_get(ext))) return 0;
990SN/A	p = ext->value->data;
1000SN/A	if(!(ext_str = method->d2i(NULL, &p, ext->value->length))) return 0;
1010SN/A	if(method->i2s) {
1020SN/A		if(!(value = method->i2s(method, ext_str))) {
1030SN/A			ok = 0;
1040SN/A			goto err;
1050SN/A		}
1060SN/A		BIO_printf(out, "%*s%s", indent, "", value);
1070SN/A	} else if(method->i2v) {
1080SN/A		if(!(nval = method->i2v(method, ext_str, NULL))) {
1090SN/A			ok = 0;
1100SN/A			goto err;
1110SN/A		}
1120SN/A		X509V3_EXT_val_prn(out, nval, indent,
1130SN/A				 method->ext_flags & X509V3_EXT_MULTILINE);
1140SN/A	} else if(method->i2r) {
1150SN/A		if(!method->i2r(method, ext_str, out, indent)) ok = 0;
1160SN/A	} else ok = 0;
1170SN/A
1180SN/A	err:
1190SN/A		sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
1200SN/A		if(value) Free(value);
1210SN/A		method->ext_free(ext_str);
1220SN/A		return ok;
1230SN/A}
1240SN/A
1250SN/A#ifndef NO_FP_API
1260SN/Aint X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
1270SN/A{
1280SN/A	BIO *bio_tmp;
1290SN/A	int ret;
1300SN/A	if(!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) return 0;
1310SN/A	ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
1320SN/A	BIO_free(bio_tmp);
1330SN/A	return ret;
1340SN/A}
1350SN/A#endif
1360SN/A