155714Skris/* v3_prn.c */ 2280304Sjkim/* 3280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4280304Sjkim * 1999. 555714Skris */ 655714Skris/* ==================================================================== 755714Skris * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 855714Skris * 955714Skris * Redistribution and use in source and binary forms, with or without 1055714Skris * modification, are permitted provided that the following conditions 1155714Skris * are met: 1255714Skris * 1355714Skris * 1. Redistributions of source code must retain the above copyright 14280304Sjkim * notice, this list of conditions and the following disclaimer. 1555714Skris * 1655714Skris * 2. Redistributions in binary form must reproduce the above copyright 1755714Skris * notice, this list of conditions and the following disclaimer in 1855714Skris * the documentation and/or other materials provided with the 1955714Skris * distribution. 2055714Skris * 2155714Skris * 3. All advertising materials mentioning features or use of this 2255714Skris * software must display the following acknowledgment: 2355714Skris * "This product includes software developed by the OpenSSL Project 2455714Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2555714Skris * 2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2755714Skris * endorse or promote products derived from this software without 2855714Skris * prior written permission. For written permission, please contact 2955714Skris * licensing@OpenSSL.org. 3055714Skris * 3155714Skris * 5. Products derived from this software may not be called "OpenSSL" 3255714Skris * nor may "OpenSSL" appear in their names without prior written 3355714Skris * permission of the OpenSSL Project. 3455714Skris * 3555714Skris * 6. Redistributions of any form whatsoever must retain the following 3655714Skris * acknowledgment: 3755714Skris * "This product includes software developed by the OpenSSL Project 3855714Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3955714Skris * 4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4355714Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 5255714Skris * ==================================================================== 5355714Skris * 5455714Skris * This product includes cryptographic software written by Eric Young 5555714Skris * (eay@cryptsoft.com). This product includes software written by Tim 5655714Skris * Hudson (tjh@cryptsoft.com). 5755714Skris * 5855714Skris */ 5955714Skris/* X509 v3 extension utilities */ 6055714Skris 6155714Skris#include <stdio.h> 6255714Skris#include "cryptlib.h" 6355714Skris#include <openssl/conf.h> 6455714Skris#include <openssl/x509v3.h> 6555714Skris 6655714Skris/* Extension printing routines */ 6755714Skris 68280304Sjkimstatic int unknown_ext_print(BIO *out, X509_EXTENSION *ext, 69280304Sjkim unsigned long flag, int indent, int supported); 70109998Smarkm 7155714Skris/* Print out a name+value stack */ 7255714Skris 73280304Sjkimvoid X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, 74280304Sjkim int ml) 7555714Skris{ 76280304Sjkim int i; 77280304Sjkim CONF_VALUE *nval; 78280304Sjkim if (!val) 79280304Sjkim return; 80280304Sjkim if (!ml || !sk_CONF_VALUE_num(val)) { 81280304Sjkim BIO_printf(out, "%*s", indent, ""); 82280304Sjkim if (!sk_CONF_VALUE_num(val)) 83280304Sjkim BIO_puts(out, "<EMPTY>\n"); 84280304Sjkim } 85280304Sjkim for (i = 0; i < sk_CONF_VALUE_num(val); i++) { 86280304Sjkim if (ml) 87280304Sjkim BIO_printf(out, "%*s", indent, ""); 88280304Sjkim else if (i > 0) 89280304Sjkim BIO_printf(out, ", "); 90280304Sjkim nval = sk_CONF_VALUE_value(val, i); 91280304Sjkim if (!nval->name) 92280304Sjkim BIO_puts(out, nval->value); 93280304Sjkim else if (!nval->value) 94280304Sjkim BIO_puts(out, nval->name); 9559191Skris#ifndef CHARSET_EBCDIC 96280304Sjkim else 97280304Sjkim BIO_printf(out, "%s:%s", nval->name, nval->value); 9859191Skris#else 99280304Sjkim else { 100280304Sjkim int len; 101280304Sjkim char *tmp; 102280304Sjkim len = strlen(nval->value) + 1; 103280304Sjkim tmp = OPENSSL_malloc(len); 104280304Sjkim if (tmp) { 105280304Sjkim ascii2ebcdic(tmp, nval->value, len); 106280304Sjkim BIO_printf(out, "%s:%s", nval->name, tmp); 107280304Sjkim OPENSSL_free(tmp); 108280304Sjkim } 109280304Sjkim } 11059191Skris#endif 111280304Sjkim if (ml) 112280304Sjkim BIO_puts(out, "\n"); 113280304Sjkim } 11455714Skris} 11555714Skris 11655714Skris/* Main routine: print out a general extension */ 11755714Skris 118280304Sjkimint X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, 119280304Sjkim int indent) 12055714Skris{ 121280304Sjkim void *ext_str = NULL; 122280304Sjkim char *value = NULL; 123280304Sjkim const unsigned char *p; 124280304Sjkim const X509V3_EXT_METHOD *method; 125280304Sjkim STACK_OF(CONF_VALUE) *nval = NULL; 126280304Sjkim int ok = 1; 127160814Ssimon 128280304Sjkim if (!(method = X509V3_EXT_get(ext))) 129280304Sjkim return unknown_ext_print(out, ext, flag, indent, 0); 130280304Sjkim p = ext->value->data; 131280304Sjkim if (method->it) 132280304Sjkim ext_str = 133280304Sjkim ASN1_item_d2i(NULL, &p, ext->value->length, 134280304Sjkim ASN1_ITEM_ptr(method->it)); 135280304Sjkim else 136280304Sjkim ext_str = method->d2i(NULL, &p, ext->value->length); 137109998Smarkm 138280304Sjkim if (!ext_str) 139280304Sjkim return unknown_ext_print(out, ext, flag, indent, 1); 140109998Smarkm 141280304Sjkim if (method->i2s) { 142280304Sjkim if (!(value = method->i2s(method, ext_str))) { 143280304Sjkim ok = 0; 144280304Sjkim goto err; 145280304Sjkim } 14659191Skris#ifndef CHARSET_EBCDIC 147280304Sjkim BIO_printf(out, "%*s%s", indent, "", value); 14859191Skris#else 149280304Sjkim { 150280304Sjkim int len; 151280304Sjkim char *tmp; 152280304Sjkim len = strlen(value) + 1; 153280304Sjkim tmp = OPENSSL_malloc(len); 154280304Sjkim if (tmp) { 155280304Sjkim ascii2ebcdic(tmp, value, len); 156280304Sjkim BIO_printf(out, "%*s%s", indent, "", tmp); 157280304Sjkim OPENSSL_free(tmp); 158280304Sjkim } 159280304Sjkim } 16059191Skris#endif 161280304Sjkim } else if (method->i2v) { 162280304Sjkim if (!(nval = method->i2v(method, ext_str, NULL))) { 163280304Sjkim ok = 0; 164280304Sjkim goto err; 165280304Sjkim } 166280304Sjkim X509V3_EXT_val_prn(out, nval, indent, 167280304Sjkim method->ext_flags & X509V3_EXT_MULTILINE); 168280304Sjkim } else if (method->i2r) { 169280304Sjkim if (!method->i2r(method, ext_str, out, indent)) 170280304Sjkim ok = 0; 171280304Sjkim } else 172280304Sjkim ok = 0; 17355714Skris 174280304Sjkim err: 175280304Sjkim sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); 176280304Sjkim if (value) 177280304Sjkim OPENSSL_free(value); 178280304Sjkim if (method->it) 179280304Sjkim ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it)); 180280304Sjkim else 181280304Sjkim method->ext_free(ext_str); 182280304Sjkim return ok; 18355714Skris} 18455714Skris 185280304Sjkimint X509V3_extensions_print(BIO *bp, char *title, 186280304Sjkim STACK_OF(X509_EXTENSION) *exts, 187280304Sjkim unsigned long flag, int indent) 188109998Smarkm{ 189280304Sjkim int i, j; 190109998Smarkm 191280304Sjkim if (sk_X509_EXTENSION_num(exts) <= 0) 192280304Sjkim return 1; 193109998Smarkm 194280304Sjkim if (title) { 195280304Sjkim BIO_printf(bp, "%*s%s:\n", indent, "", title); 196280304Sjkim indent += 4; 197280304Sjkim } 198109998Smarkm 199280304Sjkim for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { 200280304Sjkim ASN1_OBJECT *obj; 201280304Sjkim X509_EXTENSION *ex; 202280304Sjkim ex = sk_X509_EXTENSION_value(exts, i); 203280304Sjkim if (indent && BIO_printf(bp, "%*s", indent, "") <= 0) 204280304Sjkim return 0; 205280304Sjkim obj = X509_EXTENSION_get_object(ex); 206280304Sjkim i2a_ASN1_OBJECT(bp, obj); 207280304Sjkim j = X509_EXTENSION_get_critical(ex); 208280304Sjkim if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0) 209280304Sjkim return 0; 210280304Sjkim if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) { 211280304Sjkim BIO_printf(bp, "%*s", indent + 4, ""); 212280304Sjkim M_ASN1_OCTET_STRING_print(bp, ex->value); 213280304Sjkim } 214280304Sjkim if (BIO_write(bp, "\n", 1) <= 0) 215280304Sjkim return 0; 216280304Sjkim } 217280304Sjkim return 1; 218109998Smarkm} 219109998Smarkm 220280304Sjkimstatic int unknown_ext_print(BIO *out, X509_EXTENSION *ext, 221280304Sjkim unsigned long flag, int indent, int supported) 222109998Smarkm{ 223280304Sjkim switch (flag & X509V3_EXT_UNKNOWN_MASK) { 224109998Smarkm 225280304Sjkim case X509V3_EXT_DEFAULT: 226280304Sjkim return 0; 227109998Smarkm 228280304Sjkim case X509V3_EXT_ERROR_UNKNOWN: 229280304Sjkim if (supported) 230280304Sjkim BIO_printf(out, "%*s<Parse Error>", indent, ""); 231280304Sjkim else 232280304Sjkim BIO_printf(out, "%*s<Not Supported>", indent, ""); 233280304Sjkim return 1; 234109998Smarkm 235280304Sjkim case X509V3_EXT_PARSE_UNKNOWN: 236280304Sjkim return ASN1_parse_dump(out, 237280304Sjkim ext->value->data, ext->value->length, indent, 238280304Sjkim -1); 239280304Sjkim case X509V3_EXT_DUMP_UNKNOWN: 240280304Sjkim return BIO_dump_indent(out, (char *)ext->value->data, 241280304Sjkim ext->value->length, indent); 242109998Smarkm 243280304Sjkim default: 244280304Sjkim return 1; 245280304Sjkim } 246109998Smarkm} 247109998Smarkm 248109998Smarkm#ifndef OPENSSL_NO_FP_API 24955714Skrisint X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent) 25055714Skris{ 251280304Sjkim BIO *bio_tmp; 252280304Sjkim int ret; 253280304Sjkim if (!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) 254280304Sjkim return 0; 255280304Sjkim ret = X509V3_EXT_print(bio_tmp, ext, flag, indent); 256280304Sjkim BIO_free(bio_tmp); 257280304Sjkim return ret; 25855714Skris} 25955714Skris#endif 260