sl@0: /* v3_prn.c */ sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL sl@0: * project 1999. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1999 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* X509 v3 extension utilities */ sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: sl@0: /* Extension printing routines */ sl@0: sl@0: static int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent, int supported); sl@0: sl@0: /* Print out a name+value stack */ sl@0: sl@0: EXPORT_C void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, int ml) sl@0: { sl@0: int i; sl@0: CONF_VALUE *nval; sl@0: if(!val) return; sl@0: if(!ml || !sk_CONF_VALUE_num(val)) { sl@0: BIO_printf(out, "%*s", indent, ""); sl@0: if(!sk_CONF_VALUE_num(val)) BIO_puts(out, "\n"); sl@0: } sl@0: for(i = 0; i < sk_CONF_VALUE_num(val); i++) { sl@0: if(ml) BIO_printf(out, "%*s", indent, ""); sl@0: else if(i > 0) BIO_printf(out, ", "); sl@0: nval = sk_CONF_VALUE_value(val, i); sl@0: if(!nval->name) BIO_puts(out, nval->value); sl@0: else if(!nval->value) BIO_puts(out, nval->name); sl@0: #ifndef CHARSET_EBCDIC sl@0: else BIO_printf(out, "%s:%s", nval->name, nval->value); sl@0: #else sl@0: else { sl@0: int len; sl@0: char *tmp; sl@0: len = strlen(nval->value)+1; sl@0: tmp = OPENSSL_malloc(len); sl@0: if (tmp) sl@0: { sl@0: ascii2ebcdic(tmp, nval->value, len); sl@0: BIO_printf(out, "%s:%s", nval->name, tmp); sl@0: OPENSSL_free(tmp); sl@0: } sl@0: } sl@0: #endif sl@0: if(ml) BIO_puts(out, "\n"); sl@0: } sl@0: } sl@0: sl@0: /* Main routine: print out a general extension */ sl@0: sl@0: EXPORT_C int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent) sl@0: { sl@0: void *ext_str = NULL; sl@0: char *value = NULL; sl@0: const unsigned char *p; sl@0: X509V3_EXT_METHOD *method; sl@0: STACK_OF(CONF_VALUE) *nval = NULL; sl@0: int ok = 1; sl@0: sl@0: if(!(method = X509V3_EXT_get(ext))) sl@0: return unknown_ext_print(out, ext, flag, indent, 0); sl@0: p = ext->value->data; sl@0: if(method->it) ext_str = ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it)); sl@0: else ext_str = method->d2i(NULL, &p, ext->value->length); sl@0: sl@0: if(!ext_str) return unknown_ext_print(out, ext, flag, indent, 1); sl@0: sl@0: if(method->i2s) { sl@0: if(!(value = method->i2s(method, ext_str))) { sl@0: ok = 0; sl@0: goto err; sl@0: } sl@0: #ifndef CHARSET_EBCDIC sl@0: BIO_printf(out, "%*s%s", indent, "", value); sl@0: #else sl@0: { sl@0: int len; sl@0: char *tmp; sl@0: len = strlen(value)+1; sl@0: tmp = OPENSSL_malloc(len); sl@0: if (tmp) sl@0: { sl@0: ascii2ebcdic(tmp, value, len); sl@0: BIO_printf(out, "%*s%s", indent, "", tmp); sl@0: OPENSSL_free(tmp); sl@0: } sl@0: } sl@0: #endif sl@0: } else if(method->i2v) { sl@0: if(!(nval = method->i2v(method, ext_str, NULL))) { sl@0: ok = 0; sl@0: goto err; sl@0: } sl@0: X509V3_EXT_val_prn(out, nval, indent, sl@0: method->ext_flags & X509V3_EXT_MULTILINE); sl@0: } else if(method->i2r) { sl@0: if(!method->i2r(method, ext_str, out, indent)) ok = 0; sl@0: } else ok = 0; sl@0: sl@0: err: sl@0: sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); sl@0: if(value) OPENSSL_free(value); sl@0: if(method->it) ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it)); sl@0: else method->ext_free(ext_str); sl@0: return ok; sl@0: } sl@0: sl@0: EXPORT_C int X509V3_extensions_print(BIO *bp, char *title, STACK_OF(X509_EXTENSION) *exts, unsigned long flag, int indent) sl@0: { sl@0: int i, j; sl@0: sl@0: if(sk_X509_EXTENSION_num(exts) <= 0) return 1; sl@0: sl@0: if(title) sl@0: { sl@0: BIO_printf(bp,"%*s%s:\n",indent, "", title); sl@0: indent += 4; sl@0: } sl@0: sl@0: for (i=0; ivalue); sl@0: } sl@0: if (BIO_write(bp,"\n",1) <= 0) return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: static int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent, int supported) sl@0: { sl@0: switch(flag & X509V3_EXT_UNKNOWN_MASK) { sl@0: sl@0: case X509V3_EXT_DEFAULT: sl@0: return 0; sl@0: sl@0: case X509V3_EXT_ERROR_UNKNOWN: sl@0: if(supported) sl@0: BIO_printf(out, "%*s", indent, ""); sl@0: else sl@0: BIO_printf(out, "%*s", indent, ""); sl@0: return 1; sl@0: sl@0: case X509V3_EXT_PARSE_UNKNOWN: sl@0: return ASN1_parse_dump(out, sl@0: ext->value->data, ext->value->length, indent, -1); sl@0: case X509V3_EXT_DUMP_UNKNOWN: sl@0: return BIO_dump_indent(out, (char *)ext->value->data, ext->value->length, indent); sl@0: sl@0: default: sl@0: return 1; sl@0: } sl@0: } sl@0: sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent) sl@0: { sl@0: BIO *bio_tmp; sl@0: int ret; sl@0: if(!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) return 0; sl@0: ret = X509V3_EXT_print(bio_tmp, ext, flag, indent); sl@0: BIO_free(bio_tmp); sl@0: return ret; sl@0: } sl@0: #endif