10Sstevel@tonic-gate /* v3_prn.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate * project 1999.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate /* X509 v3 extension utilities */
590Sstevel@tonic-gate
600Sstevel@tonic-gate #include <stdio.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/conf.h>
630Sstevel@tonic-gate #include <openssl/x509v3.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate /* Extension printing routines */
660Sstevel@tonic-gate
670Sstevel@tonic-gate static int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent, int supported);
680Sstevel@tonic-gate
690Sstevel@tonic-gate /* Print out a name+value stack */
700Sstevel@tonic-gate
X509V3_EXT_val_prn(BIO * out,STACK_OF (CONF_VALUE)* val,int indent,int ml)710Sstevel@tonic-gate void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, int ml)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate int i;
740Sstevel@tonic-gate CONF_VALUE *nval;
750Sstevel@tonic-gate if(!val) return;
760Sstevel@tonic-gate if(!ml || !sk_CONF_VALUE_num(val)) {
770Sstevel@tonic-gate BIO_printf(out, "%*s", indent, "");
780Sstevel@tonic-gate if(!sk_CONF_VALUE_num(val)) BIO_puts(out, "<EMPTY>\n");
790Sstevel@tonic-gate }
800Sstevel@tonic-gate for(i = 0; i < sk_CONF_VALUE_num(val); i++) {
810Sstevel@tonic-gate if(ml) BIO_printf(out, "%*s", indent, "");
820Sstevel@tonic-gate else if(i > 0) BIO_printf(out, ", ");
830Sstevel@tonic-gate nval = sk_CONF_VALUE_value(val, i);
840Sstevel@tonic-gate if(!nval->name) BIO_puts(out, nval->value);
850Sstevel@tonic-gate else if(!nval->value) BIO_puts(out, nval->name);
860Sstevel@tonic-gate #ifndef CHARSET_EBCDIC
870Sstevel@tonic-gate else BIO_printf(out, "%s:%s", nval->name, nval->value);
880Sstevel@tonic-gate #else
890Sstevel@tonic-gate else {
900Sstevel@tonic-gate int len;
910Sstevel@tonic-gate char *tmp;
920Sstevel@tonic-gate len = strlen(nval->value)+1;
930Sstevel@tonic-gate tmp = OPENSSL_malloc(len);
940Sstevel@tonic-gate if (tmp)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate ascii2ebcdic(tmp, nval->value, len);
970Sstevel@tonic-gate BIO_printf(out, "%s:%s", nval->name, tmp);
980Sstevel@tonic-gate OPENSSL_free(tmp);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate #endif
1020Sstevel@tonic-gate if(ml) BIO_puts(out, "\n");
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate /* Main routine: print out a general extension */
1070Sstevel@tonic-gate
X509V3_EXT_print(BIO * out,X509_EXTENSION * ext,unsigned long flag,int indent)1080Sstevel@tonic-gate int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate void *ext_str = NULL;
1110Sstevel@tonic-gate char *value = NULL;
112*2139Sjp161948 const unsigned char *p;
1130Sstevel@tonic-gate X509V3_EXT_METHOD *method;
1140Sstevel@tonic-gate STACK_OF(CONF_VALUE) *nval = NULL;
1150Sstevel@tonic-gate int ok = 1;
116*2139Sjp161948
1170Sstevel@tonic-gate if(!(method = X509V3_EXT_get(ext)))
1180Sstevel@tonic-gate return unknown_ext_print(out, ext, flag, indent, 0);
1190Sstevel@tonic-gate p = ext->value->data;
1200Sstevel@tonic-gate if(method->it) ext_str = ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it));
1210Sstevel@tonic-gate else ext_str = method->d2i(NULL, &p, ext->value->length);
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if(!ext_str) return unknown_ext_print(out, ext, flag, indent, 1);
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if(method->i2s) {
1260Sstevel@tonic-gate if(!(value = method->i2s(method, ext_str))) {
1270Sstevel@tonic-gate ok = 0;
1280Sstevel@tonic-gate goto err;
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate #ifndef CHARSET_EBCDIC
1310Sstevel@tonic-gate BIO_printf(out, "%*s%s", indent, "", value);
1320Sstevel@tonic-gate #else
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate int len;
1350Sstevel@tonic-gate char *tmp;
1360Sstevel@tonic-gate len = strlen(value)+1;
1370Sstevel@tonic-gate tmp = OPENSSL_malloc(len);
1380Sstevel@tonic-gate if (tmp)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate ascii2ebcdic(tmp, value, len);
1410Sstevel@tonic-gate BIO_printf(out, "%*s%s", indent, "", tmp);
1420Sstevel@tonic-gate OPENSSL_free(tmp);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate #endif
1460Sstevel@tonic-gate } else if(method->i2v) {
1470Sstevel@tonic-gate if(!(nval = method->i2v(method, ext_str, NULL))) {
1480Sstevel@tonic-gate ok = 0;
1490Sstevel@tonic-gate goto err;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate X509V3_EXT_val_prn(out, nval, indent,
1520Sstevel@tonic-gate method->ext_flags & X509V3_EXT_MULTILINE);
1530Sstevel@tonic-gate } else if(method->i2r) {
1540Sstevel@tonic-gate if(!method->i2r(method, ext_str, out, indent)) ok = 0;
1550Sstevel@tonic-gate } else ok = 0;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate err:
1580Sstevel@tonic-gate sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
1590Sstevel@tonic-gate if(value) OPENSSL_free(value);
1600Sstevel@tonic-gate if(method->it) ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
1610Sstevel@tonic-gate else method->ext_free(ext_str);
1620Sstevel@tonic-gate return ok;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
X509V3_extensions_print(BIO * bp,char * title,STACK_OF (X509_EXTENSION)* exts,unsigned long flag,int indent)1650Sstevel@tonic-gate int X509V3_extensions_print(BIO *bp, char *title, STACK_OF(X509_EXTENSION) *exts, unsigned long flag, int indent)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate int i, j;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if(sk_X509_EXTENSION_num(exts) <= 0) return 1;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if(title)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate BIO_printf(bp,"%*s%s:\n",indent, "", title);
1740Sstevel@tonic-gate indent += 4;
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate for (i=0; i<sk_X509_EXTENSION_num(exts); i++)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate ASN1_OBJECT *obj;
1800Sstevel@tonic-gate X509_EXTENSION *ex;
1810Sstevel@tonic-gate ex=sk_X509_EXTENSION_value(exts, i);
1820Sstevel@tonic-gate if (indent && BIO_printf(bp,"%*s",indent, "") <= 0) return 0;
1830Sstevel@tonic-gate obj=X509_EXTENSION_get_object(ex);
1840Sstevel@tonic-gate i2a_ASN1_OBJECT(bp,obj);
1850Sstevel@tonic-gate j=X509_EXTENSION_get_critical(ex);
186*2139Sjp161948 if (BIO_printf(bp,": %s\n",j?"critical":"") <= 0)
1870Sstevel@tonic-gate return 0;
1880Sstevel@tonic-gate if(!X509V3_EXT_print(bp, ex, flag, indent + 4))
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate BIO_printf(bp, "%*s", indent + 4, "");
1910Sstevel@tonic-gate M_ASN1_OCTET_STRING_print(bp,ex->value);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate if (BIO_write(bp,"\n",1) <= 0) return 0;
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate return 1;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
unknown_ext_print(BIO * out,X509_EXTENSION * ext,unsigned long flag,int indent,int supported)1980Sstevel@tonic-gate static int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent, int supported)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate switch(flag & X509V3_EXT_UNKNOWN_MASK) {
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate case X509V3_EXT_DEFAULT:
2030Sstevel@tonic-gate return 0;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate case X509V3_EXT_ERROR_UNKNOWN:
2060Sstevel@tonic-gate if(supported)
2070Sstevel@tonic-gate BIO_printf(out, "%*s<Parse Error>", indent, "");
2080Sstevel@tonic-gate else
2090Sstevel@tonic-gate BIO_printf(out, "%*s<Not Supported>", indent, "");
2100Sstevel@tonic-gate return 1;
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate case X509V3_EXT_PARSE_UNKNOWN:
2130Sstevel@tonic-gate return ASN1_parse_dump(out,
2140Sstevel@tonic-gate ext->value->data, ext->value->length, indent, -1);
2150Sstevel@tonic-gate case X509V3_EXT_DUMP_UNKNOWN:
2160Sstevel@tonic-gate return BIO_dump_indent(out, (char *)ext->value->data, ext->value->length, indent);
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate default:
2190Sstevel@tonic-gate return 1;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
X509V3_EXT_print_fp(FILE * fp,X509_EXTENSION * ext,int flag,int indent)2250Sstevel@tonic-gate int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate BIO *bio_tmp;
2280Sstevel@tonic-gate int ret;
2290Sstevel@tonic-gate if(!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) return 0;
2300Sstevel@tonic-gate ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
2310Sstevel@tonic-gate BIO_free(bio_tmp);
2320Sstevel@tonic-gate return ret;
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate #endif
235