v3_prn.c revision 109998
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; 11255714Skris unsigned char *p; 11355714Skris X509V3_EXT_METHOD *method; 11455714Skris STACK_OF(CONF_VALUE) *nval = NULL; 11555714Skris int ok = 1; 116109998Smarkm if(!(method = X509V3_EXT_get(ext))) 117109998Smarkm return unknown_ext_print(out, ext, flag, indent, 0); 11855714Skris p = ext->value->data; 119109998Smarkm if(method->it) ext_str = ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it)); 120109998Smarkm else ext_str = method->d2i(NULL, &p, ext->value->length); 121109998Smarkm 122109998Smarkm if(!ext_str) return unknown_ext_print(out, ext, flag, indent, 1); 123109998Smarkm 12455714Skris if(method->i2s) { 12555714Skris if(!(value = method->i2s(method, ext_str))) { 12655714Skris ok = 0; 12755714Skris goto err; 12855714Skris } 12959191Skris#ifndef CHARSET_EBCDIC 13055714Skris BIO_printf(out, "%*s%s", indent, "", value); 13159191Skris#else 13259191Skris { 13372613Skris int len; 13472613Skris char *tmp; 13572613Skris len = strlen(value)+1; 13672613Skris tmp = OPENSSL_malloc(len); 13772613Skris if (tmp) 13872613Skris { 13972613Skris ascii2ebcdic(tmp, value, len); 14072613Skris BIO_printf(out, "%*s%s", indent, "", tmp); 14172613Skris OPENSSL_free(tmp); 14272613Skris } 14359191Skris } 14459191Skris#endif 14555714Skris } else if(method->i2v) { 14655714Skris if(!(nval = method->i2v(method, ext_str, NULL))) { 14755714Skris ok = 0; 14855714Skris goto err; 14955714Skris } 15055714Skris X509V3_EXT_val_prn(out, nval, indent, 15155714Skris method->ext_flags & X509V3_EXT_MULTILINE); 15255714Skris } else if(method->i2r) { 15355714Skris if(!method->i2r(method, ext_str, out, indent)) ok = 0; 15455714Skris } else ok = 0; 15555714Skris 15655714Skris err: 15755714Skris sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); 15868651Skris if(value) OPENSSL_free(value); 159109998Smarkm if(method->it) ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it)); 160109998Smarkm else method->ext_free(ext_str); 16155714Skris return ok; 16255714Skris} 16355714Skris 164109998Smarkmint X509V3_extensions_print(BIO *bp, char *title, STACK_OF(X509_EXTENSION) *exts, unsigned long flag, int indent) 165109998Smarkm{ 166109998Smarkm int i, j; 167109998Smarkm 168109998Smarkm if(sk_X509_EXTENSION_num(exts) <= 0) return 1; 169109998Smarkm 170109998Smarkm if(title) 171109998Smarkm { 172109998Smarkm BIO_printf(bp,"%*s%s:\n",indent, "", title); 173109998Smarkm indent += 4; 174109998Smarkm } 175109998Smarkm 176109998Smarkm for (i=0; i<sk_X509_EXTENSION_num(exts); i++) 177109998Smarkm { 178109998Smarkm ASN1_OBJECT *obj; 179109998Smarkm X509_EXTENSION *ex; 180109998Smarkm ex=sk_X509_EXTENSION_value(exts, i); 181109998Smarkm if (BIO_printf(bp,"%*s",indent, "") <= 0) return 0; 182109998Smarkm obj=X509_EXTENSION_get_object(ex); 183109998Smarkm i2a_ASN1_OBJECT(bp,obj); 184109998Smarkm j=X509_EXTENSION_get_critical(ex); 185109998Smarkm if (BIO_printf(bp,": %s\n",j?"critical":"","") <= 0) 186109998Smarkm return 0; 187109998Smarkm if(!X509V3_EXT_print(bp, ex, flag, 12)) 188109998Smarkm { 189109998Smarkm BIO_printf(bp, "%*s", indent + 4, ""); 190109998Smarkm M_ASN1_OCTET_STRING_print(bp,ex->value); 191109998Smarkm } 192109998Smarkm if (BIO_write(bp,"\n",1) <= 0) return 0; 193109998Smarkm } 194109998Smarkm return 1; 195109998Smarkm} 196109998Smarkm 197109998Smarkmstatic int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent, int supported) 198109998Smarkm{ 199109998Smarkm switch(flag & X509V3_EXT_UNKNOWN_MASK) { 200109998Smarkm 201109998Smarkm case X509V3_EXT_DEFAULT: 202109998Smarkm return 0; 203109998Smarkm 204109998Smarkm case X509V3_EXT_ERROR_UNKNOWN: 205109998Smarkm if(supported) 206109998Smarkm BIO_printf(out, "%*s<Parse Error>", indent, ""); 207109998Smarkm else 208109998Smarkm BIO_printf(out, "%*s<Not Supported>", indent, ""); 209109998Smarkm return 1; 210109998Smarkm 211109998Smarkm case X509V3_EXT_PARSE_UNKNOWN: 212109998Smarkm return ASN1_parse_dump(out, 213109998Smarkm ext->value->data, ext->value->length, indent, -1); 214109998Smarkm case X509V3_EXT_DUMP_UNKNOWN: 215109998Smarkm return BIO_dump_indent(out, (char *)ext->value->data, ext->value->length, indent); 216109998Smarkm 217109998Smarkm default: 218109998Smarkm return 1; 219109998Smarkm } 220109998Smarkm} 221109998Smarkm 222109998Smarkm 223109998Smarkm#ifndef OPENSSL_NO_FP_API 22455714Skrisint X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent) 22555714Skris{ 22655714Skris BIO *bio_tmp; 22755714Skris int ret; 22855714Skris if(!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) return 0; 22955714Skris ret = X509V3_EXT_print(bio_tmp, ext, flag, indent); 23055714Skris BIO_free(bio_tmp); 23155714Skris return ret; 23255714Skris} 23355714Skris#endif 234