v3_prn.c revision 160814
155714Skris/* v3_prn.c */ 255714Skris/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 355714Skris * project 1999. 455714Skris */ 555714Skris/* ==================================================================== 655714Skris * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 755714Skris * 855714Skris * Redistribution and use in source and binary forms, with or without 955714Skris * modification, are permitted provided that the following conditions 1055714Skris * are met: 1155714Skris * 1255714Skris * 1. Redistributions of source code must retain the above copyright 1355714Skris * notice, this list of conditions and the following disclaimer. 1455714Skris * 1555714Skris * 2. Redistributions in binary form must reproduce the above copyright 1655714Skris * notice, this list of conditions and the following disclaimer in 1755714Skris * the documentation and/or other materials provided with the 1855714Skris * distribution. 1955714Skris * 2055714Skris * 3. All advertising materials mentioning features or use of this 2155714Skris * software must display the following acknowledgment: 2255714Skris * "This product includes software developed by the OpenSSL Project 2355714Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2455714Skris * 2555714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2655714Skris * endorse or promote products derived from this software without 2755714Skris * prior written permission. For written permission, please contact 2855714Skris * licensing@OpenSSL.org. 2955714Skris * 3055714Skris * 5. Products derived from this software may not be called "OpenSSL" 3155714Skris * nor may "OpenSSL" appear in their names without prior written 3255714Skris * permission of the OpenSSL Project. 3355714Skris * 3455714Skris * 6. Redistributions of any form whatsoever must retain the following 3555714Skris * acknowledgment: 3655714Skris * "This product includes software developed by the OpenSSL Project 3755714Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3855714Skris * 3955714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4055714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4155714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4255714Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4355714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4455714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4555714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4655714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4755714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4855714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4955714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5055714Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 5155714Skris * ==================================================================== 5255714Skris * 5355714Skris * This product includes cryptographic software written by Eric Young 5455714Skris * (eay@cryptsoft.com). This product includes software written by Tim 5555714Skris * Hudson (tjh@cryptsoft.com). 5655714Skris * 5755714Skris */ 5855714Skris/* X509 v3 extension utilities */ 5955714Skris 6055714Skris#include <stdio.h> 6155714Skris#include "cryptlib.h" 6255714Skris#include <openssl/conf.h> 6355714Skris#include <openssl/x509v3.h> 6455714Skris 6555714Skris/* Extension printing routines */ 6655714Skris 67109998Smarkmstatic int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent, int supported); 68109998Smarkm 6955714Skris/* Print out a name+value stack */ 7055714Skris 7155714Skrisvoid X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, int ml) 7255714Skris{ 7355714Skris int i; 7455714Skris CONF_VALUE *nval; 7555714Skris if(!val) return; 7655714Skris if(!ml || !sk_CONF_VALUE_num(val)) { 7755714Skris BIO_printf(out, "%*s", indent, ""); 7855714Skris if(!sk_CONF_VALUE_num(val)) BIO_puts(out, "<EMPTY>\n"); 7955714Skris } 8055714Skris for(i = 0; i < sk_CONF_VALUE_num(val); i++) { 8155714Skris if(ml) BIO_printf(out, "%*s", indent, ""); 8255714Skris else if(i > 0) BIO_printf(out, ", "); 8355714Skris nval = sk_CONF_VALUE_value(val, i); 8455714Skris if(!nval->name) BIO_puts(out, nval->value); 8555714Skris else if(!nval->value) BIO_puts(out, nval->name); 8659191Skris#ifndef CHARSET_EBCDIC 8755714Skris else BIO_printf(out, "%s:%s", nval->name, nval->value); 8859191Skris#else 8959191Skris else { 9072613Skris int len; 9172613Skris char *tmp; 9272613Skris len = strlen(nval->value)+1; 9372613Skris tmp = OPENSSL_malloc(len); 9472613Skris if (tmp) 9572613Skris { 9672613Skris ascii2ebcdic(tmp, nval->value, len); 9772613Skris BIO_printf(out, "%s:%s", nval->name, tmp); 9872613Skris OPENSSL_free(tmp); 9972613Skris } 10059191Skris } 10159191Skris#endif 10255714Skris if(ml) BIO_puts(out, "\n"); 10355714Skris } 10455714Skris} 10555714Skris 10655714Skris/* Main routine: print out a general extension */ 10755714Skris 108109998Smarkmint X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent) 10955714Skris{ 110109998Smarkm void *ext_str = NULL; 111109998Smarkm char *value = NULL; 112160814Ssimon const unsigned char *p; 11355714Skris X509V3_EXT_METHOD *method; 11455714Skris STACK_OF(CONF_VALUE) *nval = NULL; 11555714Skris int ok = 1; 116160814Ssimon 117109998Smarkm if(!(method = X509V3_EXT_get(ext))) 118109998Smarkm return unknown_ext_print(out, ext, flag, indent, 0); 11955714Skris p = ext->value->data; 120109998Smarkm if(method->it) ext_str = ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it)); 121109998Smarkm else ext_str = method->d2i(NULL, &p, ext->value->length); 122109998Smarkm 123109998Smarkm if(!ext_str) return unknown_ext_print(out, ext, flag, indent, 1); 124109998Smarkm 12555714Skris if(method->i2s) { 12655714Skris if(!(value = method->i2s(method, ext_str))) { 12755714Skris ok = 0; 12855714Skris goto err; 12955714Skris } 13059191Skris#ifndef CHARSET_EBCDIC 13155714Skris BIO_printf(out, "%*s%s", indent, "", value); 13259191Skris#else 13359191Skris { 13472613Skris int len; 13572613Skris char *tmp; 13672613Skris len = strlen(value)+1; 13772613Skris tmp = OPENSSL_malloc(len); 13872613Skris if (tmp) 13972613Skris { 14072613Skris ascii2ebcdic(tmp, value, len); 14172613Skris BIO_printf(out, "%*s%s", indent, "", tmp); 14272613Skris OPENSSL_free(tmp); 14372613Skris } 14459191Skris } 14559191Skris#endif 14655714Skris } else if(method->i2v) { 14755714Skris if(!(nval = method->i2v(method, ext_str, NULL))) { 14855714Skris ok = 0; 14955714Skris goto err; 15055714Skris } 15155714Skris X509V3_EXT_val_prn(out, nval, indent, 15255714Skris method->ext_flags & X509V3_EXT_MULTILINE); 15355714Skris } else if(method->i2r) { 15455714Skris if(!method->i2r(method, ext_str, out, indent)) ok = 0; 15555714Skris } else ok = 0; 15655714Skris 15755714Skris err: 15855714Skris sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); 15968651Skris if(value) OPENSSL_free(value); 160109998Smarkm if(method->it) ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it)); 161109998Smarkm else method->ext_free(ext_str); 16255714Skris return ok; 16355714Skris} 16455714Skris 165109998Smarkmint X509V3_extensions_print(BIO *bp, char *title, STACK_OF(X509_EXTENSION) *exts, unsigned long flag, int indent) 166109998Smarkm{ 167109998Smarkm int i, j; 168109998Smarkm 169109998Smarkm if(sk_X509_EXTENSION_num(exts) <= 0) return 1; 170109998Smarkm 171109998Smarkm if(title) 172109998Smarkm { 173109998Smarkm BIO_printf(bp,"%*s%s:\n",indent, "", title); 174109998Smarkm indent += 4; 175109998Smarkm } 176109998Smarkm 177109998Smarkm for (i=0; i<sk_X509_EXTENSION_num(exts); i++) 178109998Smarkm { 179109998Smarkm ASN1_OBJECT *obj; 180109998Smarkm X509_EXTENSION *ex; 181109998Smarkm ex=sk_X509_EXTENSION_value(exts, i); 182120631Snectar if (indent && BIO_printf(bp,"%*s",indent, "") <= 0) return 0; 183109998Smarkm obj=X509_EXTENSION_get_object(ex); 184109998Smarkm i2a_ASN1_OBJECT(bp,obj); 185109998Smarkm j=X509_EXTENSION_get_critical(ex); 186160814Ssimon if (BIO_printf(bp,": %s\n",j?"critical":"") <= 0) 187109998Smarkm return 0; 188120631Snectar if(!X509V3_EXT_print(bp, ex, flag, indent + 4)) 189109998Smarkm { 190109998Smarkm BIO_printf(bp, "%*s", indent + 4, ""); 191109998Smarkm M_ASN1_OCTET_STRING_print(bp,ex->value); 192109998Smarkm } 193109998Smarkm if (BIO_write(bp,"\n",1) <= 0) return 0; 194109998Smarkm } 195109998Smarkm return 1; 196109998Smarkm} 197109998Smarkm 198109998Smarkmstatic int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent, int supported) 199109998Smarkm{ 200109998Smarkm switch(flag & X509V3_EXT_UNKNOWN_MASK) { 201109998Smarkm 202109998Smarkm case X509V3_EXT_DEFAULT: 203109998Smarkm return 0; 204109998Smarkm 205109998Smarkm case X509V3_EXT_ERROR_UNKNOWN: 206109998Smarkm if(supported) 207109998Smarkm BIO_printf(out, "%*s<Parse Error>", indent, ""); 208109998Smarkm else 209109998Smarkm BIO_printf(out, "%*s<Not Supported>", indent, ""); 210109998Smarkm return 1; 211109998Smarkm 212109998Smarkm case X509V3_EXT_PARSE_UNKNOWN: 213109998Smarkm return ASN1_parse_dump(out, 214109998Smarkm ext->value->data, ext->value->length, indent, -1); 215109998Smarkm case X509V3_EXT_DUMP_UNKNOWN: 216109998Smarkm return BIO_dump_indent(out, (char *)ext->value->data, ext->value->length, indent); 217109998Smarkm 218109998Smarkm default: 219109998Smarkm return 1; 220109998Smarkm } 221109998Smarkm} 222109998Smarkm 223109998Smarkm 224109998Smarkm#ifndef OPENSSL_NO_FP_API 22555714Skrisint X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent) 22655714Skris{ 22755714Skris BIO *bio_tmp; 22855714Skris int ret; 22955714Skris if(!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) return 0; 23055714Skris ret = X509V3_EXT_print(bio_tmp, ext, flag, indent); 23155714Skris BIO_free(bio_tmp); 23255714Skris return ret; 23355714Skris} 23455714Skris#endif 235