1/* tasn_prn.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in
17 *    the documentation and/or other materials provided with the
18 *    distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 *    software must display the following acknowledgment:
22 *    "This product includes software developed by the OpenSSL Project
23 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 *    endorse or promote products derived from this software without
27 *    prior written permission. For written permission, please contact
28 *    licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 *    nor may "OpenSSL" appear in their names without prior written
32 *    permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 *    acknowledgment:
36 *    "This product includes software developed by the OpenSSL Project
37 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com).  This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59
60#include <stddef.h>
61#include <openssl/asn1.h>
62#include <openssl/objects.h>
63#include <openssl/buffer.h>
64#include <openssl/err.h>
65#include <openssl/nasn.h>
66
67/* Print routines. Print out a whole structure from a template.
68 */
69
70static int asn1_item_print_nm(BIO *out, void *fld, int indent, const ASN1_ITEM *it, const char *name);
71
72int ASN1_item_print(BIO *out, void *fld, int indent, const ASN1_ITEM *it)
73{
74	return asn1_item_print_nm(out, fld, indent, it, it->sname);
75}
76
77static int asn1_item_print_nm(BIO *out, void *fld, int indent, const ASN1_ITEM *it, const char *name)
78{
79	ASN1_STRING *str;
80	const ASN1_TEMPLATE *tt;
81	void *tmpfld;
82	int i;
83	if(!fld) {
84		BIO_printf(out, "%*s%s ABSENT\n", indent, "", name);
85		return 1;
86	}
87	switch(it->itype) {
88
89		case ASN1_ITYPE_PRIMITIVE:
90		if(it->templates)
91			return ASN1_template_print(out, fld, indent, it->templates);
92		return asn1_primitive_print(out, fld, it->utype, indent, name);
93		break;
94
95		case ASN1_ITYPE_MSTRING:
96		str = fld;
97		return asn1_primitive_print(out, fld, str->type, indent, name);
98
99		case ASN1_ITYPE_EXTERN:
100		BIO_printf(out, "%*s%s:EXTERNAL TYPE %s %s\n", indent, "", name, it->sname, fld ? "" : "ABSENT");
101		return 1;
102		case ASN1_ITYPE_COMPAT:
103		BIO_printf(out, "%*s%s:COMPATIBLE TYPE %s %s\n", indent, "", name, it->sname, fld ? "" : "ABSENT");
104		return 1;
105
106
107		case ASN1_ITYPE_CHOICE:
108		/* CHOICE type, get selector */
109		i = asn1_get_choice_selector(fld, it);
110		/* This should never happen... */
111		if((i < 0) || (i >= it->tcount)) {
112			BIO_printf(out, "%s selector [%d] out of range\n", it->sname, i);
113			return 1;
114		}
115		tt = it->templates + i;
116		tmpfld = asn1_get_field(fld, tt);
117		return ASN1_template_print(out, tmpfld, indent, tt);
118
119		case ASN1_ITYPE_SEQUENCE:
120		BIO_printf(out, "%*s%s {\n", indent, "", name);
121		/* Get each field entry */
122		for(i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
123			tmpfld = asn1_get_field(fld, tt);
124			ASN1_template_print(out, tmpfld, indent + 2, tt);
125		}
126		BIO_printf(out, "%*s}\n", indent, "");
127		return 1;
128
129		default:
130		return 0;
131	}
132}
133
134int ASN1_template_print(BIO *out, void *fld, int indent, const ASN1_TEMPLATE *tt)
135{
136	int i, flags;
137#if 0
138	if(!fld) return 0;
139#endif
140	flags = tt->flags;
141	if(flags & ASN1_TFLG_SK_MASK) {
142		char *tname;
143		void *skitem;
144		/* SET OF, SEQUENCE OF */
145		if(flags & ASN1_TFLG_SET_OF) tname = "SET";
146		else tname = "SEQUENCE";
147		if(fld) {
148			BIO_printf(out, "%*s%s OF %s {\n", indent, "", tname, tt->field_name);
149			for(i = 0; i < sk_num(fld); i++) {
150				skitem = sk_value(fld, i);
151				asn1_item_print_nm(out, skitem, indent + 2, tt->item, "");
152			}
153			BIO_printf(out, "%*s}\n", indent, "");
154		} else
155			BIO_printf(out, "%*s%s OF %s ABSENT\n", indent, "", tname, tt->field_name);
156		return 1;
157	}
158	return asn1_item_print_nm(out, fld, indent, tt->item, tt->field_name);
159}
160
161static int asn1_primitive_print(BIO *out, void *fld, long utype, int indent, const char *name)
162{
163	ASN1_STRING *str = fld;
164	if(fld) {
165		if(utype == V_ASN1_BOOLEAN) {
166			int *bool = fld;
167if(*bool == -1) printf("BOOL MISSING\n");
168			BIO_printf(out, "%*s%s:%s", indent, "", "BOOLEAN", *bool ? "TRUE" : "FALSE");
169		} else if((utype == V_ASN1_INTEGER)
170			  || (utype == V_ASN1_ENUMERATED)) {
171			char *s, *nm;
172			s = i2s_ASN1_INTEGER(NULL, fld);
173			if(utype == V_ASN1_INTEGER) nm = "INTEGER";
174			else nm = "ENUMERATED";
175			BIO_printf(out, "%*s%s:%s", indent, "", nm, s);
176			OPENSSL_free(s);
177		} else if(utype == V_ASN1_NULL) {
178			BIO_printf(out, "%*s%s", indent, "", "NULL");
179		} else if(utype == V_ASN1_UTCTIME) {
180			BIO_printf(out, "%*s%s:%s:", indent, "", name, "UTCTIME");
181			ASN1_UTCTIME_print(out, str);
182		} else if(utype == V_ASN1_GENERALIZEDTIME) {
183			BIO_printf(out, "%*s%s:%s:", indent, "", name, "GENERALIZEDTIME");
184			ASN1_GENERALIZEDTIME_print(out, str);
185		} else if(utype == V_ASN1_OBJECT) {
186			char objbuf[80], *ln;
187			ln = OBJ_nid2ln(OBJ_obj2nid(fld));
188			if(!ln) ln = "";
189			OBJ_obj2txt(objbuf, sizeof objbuf, fld, 1);
190			BIO_printf(out, "%*s%s:%s (%s)", indent, "", "OBJECT", ln, objbuf);
191		} else {
192			BIO_printf(out, "%*s%s:", indent, "", name);
193			ASN1_STRING_print_ex(out, str, ASN1_STRFLGS_DUMP_UNKNOWN|ASN1_STRFLGS_SHOW_TYPE);
194		}
195		BIO_printf(out, "\n");
196	} else BIO_printf(out, "%*s%s [ABSENT]\n", indent, "", name);
197	return 1;
198}
199