1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: x509_prn.c,v 1.2 2021/11/01 20:53:08 tb Exp $ */
28edacedfSDaniel Fojt /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
38edacedfSDaniel Fojt * project 1999.
48edacedfSDaniel Fojt */
58edacedfSDaniel Fojt /* ====================================================================
68edacedfSDaniel Fojt * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
78edacedfSDaniel Fojt *
88edacedfSDaniel Fojt * Redistribution and use in source and binary forms, with or without
98edacedfSDaniel Fojt * modification, are permitted provided that the following conditions
108edacedfSDaniel Fojt * are met:
118edacedfSDaniel Fojt *
128edacedfSDaniel Fojt * 1. Redistributions of source code must retain the above copyright
138edacedfSDaniel Fojt * notice, this list of conditions and the following disclaimer.
148edacedfSDaniel Fojt *
158edacedfSDaniel Fojt * 2. Redistributions in binary form must reproduce the above copyright
168edacedfSDaniel Fojt * notice, this list of conditions and the following disclaimer in
178edacedfSDaniel Fojt * the documentation and/or other materials provided with the
188edacedfSDaniel Fojt * distribution.
198edacedfSDaniel Fojt *
208edacedfSDaniel Fojt * 3. All advertising materials mentioning features or use of this
218edacedfSDaniel Fojt * software must display the following acknowledgment:
228edacedfSDaniel Fojt * "This product includes software developed by the OpenSSL Project
238edacedfSDaniel Fojt * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
248edacedfSDaniel Fojt *
258edacedfSDaniel Fojt * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
268edacedfSDaniel Fojt * endorse or promote products derived from this software without
278edacedfSDaniel Fojt * prior written permission. For written permission, please contact
288edacedfSDaniel Fojt * licensing@OpenSSL.org.
298edacedfSDaniel Fojt *
308edacedfSDaniel Fojt * 5. Products derived from this software may not be called "OpenSSL"
318edacedfSDaniel Fojt * nor may "OpenSSL" appear in their names without prior written
328edacedfSDaniel Fojt * permission of the OpenSSL Project.
338edacedfSDaniel Fojt *
348edacedfSDaniel Fojt * 6. Redistributions of any form whatsoever must retain the following
358edacedfSDaniel Fojt * acknowledgment:
368edacedfSDaniel Fojt * "This product includes software developed by the OpenSSL Project
378edacedfSDaniel Fojt * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
388edacedfSDaniel Fojt *
398edacedfSDaniel Fojt * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
408edacedfSDaniel Fojt * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
418edacedfSDaniel Fojt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
428edacedfSDaniel Fojt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
438edacedfSDaniel Fojt * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
448edacedfSDaniel Fojt * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
458edacedfSDaniel Fojt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
468edacedfSDaniel Fojt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
478edacedfSDaniel Fojt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
488edacedfSDaniel Fojt * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
498edacedfSDaniel Fojt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
508edacedfSDaniel Fojt * OF THE POSSIBILITY OF SUCH DAMAGE.
518edacedfSDaniel Fojt * ====================================================================
528edacedfSDaniel Fojt *
538edacedfSDaniel Fojt * This product includes cryptographic software written by Eric Young
548edacedfSDaniel Fojt * (eay@cryptsoft.com). This product includes software written by Tim
558edacedfSDaniel Fojt * Hudson (tjh@cryptsoft.com).
568edacedfSDaniel Fojt *
578edacedfSDaniel Fojt */
588edacedfSDaniel Fojt /* X509 v3 extension utilities */
598edacedfSDaniel Fojt
608edacedfSDaniel Fojt #include <stdio.h>
618edacedfSDaniel Fojt
628edacedfSDaniel Fojt #include <openssl/conf.h>
638edacedfSDaniel Fojt #include <openssl/x509v3.h>
648edacedfSDaniel Fojt
65*de0e0e4dSAntonio Huete Jimenez #include "x509_lcl.h"
66*de0e0e4dSAntonio Huete Jimenez
678edacedfSDaniel Fojt /* Extension printing routines */
688edacedfSDaniel Fojt
698edacedfSDaniel Fojt static int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
708edacedfSDaniel Fojt int indent, int supported);
718edacedfSDaniel Fojt
728edacedfSDaniel Fojt /* Print out a name+value stack */
738edacedfSDaniel Fojt
748edacedfSDaniel Fojt void
X509V3_EXT_val_prn(BIO * out,STACK_OF (CONF_VALUE)* val,int indent,int ml)758edacedfSDaniel Fojt X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, int ml)
768edacedfSDaniel Fojt {
778edacedfSDaniel Fojt int i;
788edacedfSDaniel Fojt CONF_VALUE *nval;
798edacedfSDaniel Fojt
808edacedfSDaniel Fojt if (!val)
818edacedfSDaniel Fojt return;
828edacedfSDaniel Fojt if (!ml || !sk_CONF_VALUE_num(val)) {
838edacedfSDaniel Fojt BIO_printf(out, "%*s", indent, "");
848edacedfSDaniel Fojt if (!sk_CONF_VALUE_num(val))
858edacedfSDaniel Fojt BIO_puts(out, "<EMPTY>\n");
868edacedfSDaniel Fojt }
878edacedfSDaniel Fojt for (i = 0; i < sk_CONF_VALUE_num(val); i++) {
888edacedfSDaniel Fojt if (ml)
898edacedfSDaniel Fojt BIO_printf(out, "%*s", indent, "");
908edacedfSDaniel Fojt else if (i > 0) BIO_printf(out, ", ");
918edacedfSDaniel Fojt nval = sk_CONF_VALUE_value(val, i);
928edacedfSDaniel Fojt if (!nval->name)
938edacedfSDaniel Fojt BIO_puts(out, nval->value);
948edacedfSDaniel Fojt else if (!nval->value)
958edacedfSDaniel Fojt BIO_puts(out, nval->name);
968edacedfSDaniel Fojt else
978edacedfSDaniel Fojt BIO_printf(out, "%s:%s", nval->name, nval->value);
988edacedfSDaniel Fojt if (ml)
998edacedfSDaniel Fojt BIO_puts(out, "\n");
1008edacedfSDaniel Fojt }
1018edacedfSDaniel Fojt }
1028edacedfSDaniel Fojt
1038edacedfSDaniel Fojt /* Main routine: print out a general extension */
1048edacedfSDaniel Fojt
1058edacedfSDaniel Fojt int
X509V3_EXT_print(BIO * out,X509_EXTENSION * ext,unsigned long flag,int indent)1068edacedfSDaniel Fojt X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent)
1078edacedfSDaniel Fojt {
1088edacedfSDaniel Fojt void *ext_str = NULL;
1098edacedfSDaniel Fojt char *value = NULL;
1108edacedfSDaniel Fojt const unsigned char *p;
1118edacedfSDaniel Fojt const X509V3_EXT_METHOD *method;
1128edacedfSDaniel Fojt STACK_OF(CONF_VALUE) *nval = NULL;
1138edacedfSDaniel Fojt int ok = 1;
1148edacedfSDaniel Fojt
1158edacedfSDaniel Fojt if (!(method = X509V3_EXT_get(ext)))
1168edacedfSDaniel Fojt return unknown_ext_print(out, ext, flag, indent, 0);
1178edacedfSDaniel Fojt p = ext->value->data;
1188edacedfSDaniel Fojt if (method->it)
1198edacedfSDaniel Fojt ext_str = ASN1_item_d2i(NULL, &p, ext->value->length,
1208edacedfSDaniel Fojt method->it);
1218edacedfSDaniel Fojt else
1228edacedfSDaniel Fojt ext_str = method->d2i(NULL, &p, ext->value->length);
1238edacedfSDaniel Fojt
1248edacedfSDaniel Fojt if (!ext_str)
1258edacedfSDaniel Fojt return unknown_ext_print(out, ext, flag, indent, 1);
1268edacedfSDaniel Fojt
1278edacedfSDaniel Fojt if (method->i2s) {
1288edacedfSDaniel Fojt if (!(value = method->i2s(method, ext_str))) {
1298edacedfSDaniel Fojt ok = 0;
1308edacedfSDaniel Fojt goto err;
1318edacedfSDaniel Fojt }
1328edacedfSDaniel Fojt BIO_printf(out, "%*s%s", indent, "", value);
1338edacedfSDaniel Fojt } else if (method->i2v) {
1348edacedfSDaniel Fojt if (!(nval = method->i2v(method, ext_str, NULL))) {
1358edacedfSDaniel Fojt ok = 0;
1368edacedfSDaniel Fojt goto err;
1378edacedfSDaniel Fojt }
1388edacedfSDaniel Fojt X509V3_EXT_val_prn(out, nval, indent,
1398edacedfSDaniel Fojt method->ext_flags & X509V3_EXT_MULTILINE);
1408edacedfSDaniel Fojt } else if (method->i2r) {
1418edacedfSDaniel Fojt if (!method->i2r(method, ext_str, out, indent))
1428edacedfSDaniel Fojt ok = 0;
1438edacedfSDaniel Fojt } else
1448edacedfSDaniel Fojt ok = 0;
1458edacedfSDaniel Fojt
1468edacedfSDaniel Fojt err:
1478edacedfSDaniel Fojt sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
1488edacedfSDaniel Fojt free(value);
1498edacedfSDaniel Fojt if (method->it)
1508edacedfSDaniel Fojt ASN1_item_free(ext_str, method->it);
1518edacedfSDaniel Fojt else
1528edacedfSDaniel Fojt method->ext_free(ext_str);
1538edacedfSDaniel Fojt return ok;
1548edacedfSDaniel Fojt }
1558edacedfSDaniel Fojt
1568edacedfSDaniel Fojt int
X509V3_extensions_print(BIO * bp,const char * title,const STACK_OF (X509_EXTENSION)* exts,unsigned long flag,int indent)1578edacedfSDaniel Fojt X509V3_extensions_print(BIO *bp, const char *title,
1588edacedfSDaniel Fojt const STACK_OF(X509_EXTENSION) *exts, unsigned long flag, int indent)
1598edacedfSDaniel Fojt {
1608edacedfSDaniel Fojt int i, j;
1618edacedfSDaniel Fojt
1628edacedfSDaniel Fojt if (sk_X509_EXTENSION_num(exts) <= 0)
1638edacedfSDaniel Fojt return 1;
1648edacedfSDaniel Fojt
1658edacedfSDaniel Fojt if (title) {
1668edacedfSDaniel Fojt BIO_printf(bp, "%*s%s:\n",indent, "", title);
1678edacedfSDaniel Fojt indent += 4;
1688edacedfSDaniel Fojt }
1698edacedfSDaniel Fojt
1708edacedfSDaniel Fojt for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
1718edacedfSDaniel Fojt ASN1_OBJECT *obj;
1728edacedfSDaniel Fojt X509_EXTENSION *ex;
1738edacedfSDaniel Fojt ex = sk_X509_EXTENSION_value(exts, i);
1748edacedfSDaniel Fojt if (indent && BIO_printf(bp, "%*s",indent, "") <= 0)
1758edacedfSDaniel Fojt return 0;
1768edacedfSDaniel Fojt obj = X509_EXTENSION_get_object(ex);
1778edacedfSDaniel Fojt i2a_ASN1_OBJECT(bp, obj);
1788edacedfSDaniel Fojt j = X509_EXTENSION_get_critical(ex);
1798edacedfSDaniel Fojt if (BIO_printf(bp, ": %s\n",j?"critical":"") <= 0)
1808edacedfSDaniel Fojt return 0;
1818edacedfSDaniel Fojt if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
1828edacedfSDaniel Fojt BIO_printf(bp, "%*s", indent + 4, "");
1838edacedfSDaniel Fojt ASN1_STRING_print(bp, ex->value);
1848edacedfSDaniel Fojt }
1858edacedfSDaniel Fojt if (BIO_write(bp, "\n",1) <= 0)
1868edacedfSDaniel Fojt return 0;
1878edacedfSDaniel Fojt }
1888edacedfSDaniel Fojt return 1;
1898edacedfSDaniel Fojt }
1908edacedfSDaniel Fojt
1918edacedfSDaniel Fojt static int
unknown_ext_print(BIO * out,X509_EXTENSION * ext,unsigned long flag,int indent,int supported)1928edacedfSDaniel Fojt unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
1938edacedfSDaniel Fojt int indent, int supported)
1948edacedfSDaniel Fojt {
1958edacedfSDaniel Fojt switch (flag & X509V3_EXT_UNKNOWN_MASK) {
1968edacedfSDaniel Fojt case X509V3_EXT_DEFAULT:
1978edacedfSDaniel Fojt return 0;
1988edacedfSDaniel Fojt case X509V3_EXT_ERROR_UNKNOWN:
1998edacedfSDaniel Fojt if (supported)
2008edacedfSDaniel Fojt BIO_printf(out, "%*s<Parse Error>", indent, "");
2018edacedfSDaniel Fojt else
2028edacedfSDaniel Fojt BIO_printf(out, "%*s<Not Supported>", indent, "");
2038edacedfSDaniel Fojt return 1;
2048edacedfSDaniel Fojt case X509V3_EXT_PARSE_UNKNOWN:
2058edacedfSDaniel Fojt return ASN1_parse_dump(out,
2068edacedfSDaniel Fojt ext->value->data, ext->value->length, indent, -1);
2078edacedfSDaniel Fojt case X509V3_EXT_DUMP_UNKNOWN:
2088edacedfSDaniel Fojt return BIO_dump_indent(out, (char *)ext->value->data,
2098edacedfSDaniel Fojt ext->value->length, indent);
2108edacedfSDaniel Fojt default:
2118edacedfSDaniel Fojt return 1;
2128edacedfSDaniel Fojt }
2138edacedfSDaniel Fojt }
2148edacedfSDaniel Fojt
2158edacedfSDaniel Fojt
2168edacedfSDaniel Fojt int
X509V3_EXT_print_fp(FILE * fp,X509_EXTENSION * ext,int flag,int indent)2178edacedfSDaniel Fojt X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
2188edacedfSDaniel Fojt {
2198edacedfSDaniel Fojt BIO *bio_tmp;
2208edacedfSDaniel Fojt int ret;
2218edacedfSDaniel Fojt
2228edacedfSDaniel Fojt if (!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)))
2238edacedfSDaniel Fojt return 0;
2248edacedfSDaniel Fojt ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
2258edacedfSDaniel Fojt BIO_free(bio_tmp);
2268edacedfSDaniel Fojt return ret;
2278edacedfSDaniel Fojt }
228