More method functions for elliptic curves,
[openssl.git] / crypto / asn1 / tasn_prn.c
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
70 static int asn1_item_print_nm(BIO *out, void *fld, int indent, const ASN1_ITEM *it, const char *name);
71
72 int 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
77 static 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
134 int ASN1_template_print(BIO *out, void *fld, int indent, const ASN1_TEMPLATE *tt)
135 {
136         int i, flags;
137         //if(!fld) return 0;
138         flags = tt->flags;
139         if(flags & ASN1_TFLG_SK_MASK) {
140                 char *tname;
141                 void *skitem;
142                 /* SET OF, SEQUENCE OF */
143                 if(flags & ASN1_TFLG_SET_OF) tname = "SET";
144                 else tname = "SEQUENCE";
145                 if(fld) {
146                         BIO_printf(out, "%*s%s OF %s {\n", indent, "", tname, tt->field_name);
147                         for(i = 0; i < sk_num(fld); i++) {
148                                 skitem = sk_value(fld, i);
149                                 asn1_item_print_nm(out, skitem, indent + 2, tt->item, "");
150                         }
151                         BIO_printf(out, "%*s}\n", indent, "");
152                 } else 
153                         BIO_printf(out, "%*s%s OF %s ABSENT\n", indent, "", tname, tt->field_name);
154                 return 1;
155         }
156         return asn1_item_print_nm(out, fld, indent, tt->item, tt->field_name);
157 }
158
159 static int asn1_primitive_print(BIO *out, void *fld, long utype, int indent, const char *name)
160 {
161         ASN1_STRING *str = fld;
162         if(fld) {
163                 if(utype == V_ASN1_BOOLEAN) {
164                         int *bool = fld;
165 if(*bool == -1) printf("BOOL MISSING\n");
166                         BIO_printf(out, "%*s%s:%s", indent, "", "BOOLEAN", *bool ? "TRUE" : "FALSE");
167                 } else if((utype == V_ASN1_INTEGER) 
168                           || (utype == V_ASN1_ENUMERATED)) {
169                         char *s, *nm;
170                         s = i2s_ASN1_INTEGER(NULL, fld);
171                         if(utype == V_ASN1_INTEGER) nm = "INTEGER";
172                         else nm = "ENUMERATED";
173                         BIO_printf(out, "%*s%s:%s", indent, "", nm, s);
174                         OPENSSL_free(s);
175                 } else if(utype == V_ASN1_NULL) {
176                         BIO_printf(out, "%*s%s", indent, "", "NULL");
177                 } else if(utype == V_ASN1_UTCTIME) {
178                         BIO_printf(out, "%*s%s:%s:", indent, "", name, "UTCTIME");
179                         ASN1_UTCTIME_print(out, str);
180                 } else if(utype == V_ASN1_GENERALIZEDTIME) {
181                         BIO_printf(out, "%*s%s:%s:", indent, "", name, "GENERALIZEDTIME");
182                         ASN1_GENERALIZEDTIME_print(out, str);
183                 } else if(utype == V_ASN1_OBJECT) {
184                         char objbuf[80], *ln;
185                         ln = OBJ_nid2ln(OBJ_obj2nid(fld));
186                         if(!ln) ln = "";
187                         OBJ_obj2txt(objbuf, 80, fld, 1);
188                         BIO_printf(out, "%*s%s:%s (%s)", indent, "", "OBJECT", ln, objbuf);
189                 } else {
190                         BIO_printf(out, "%*s%s:", indent, "", name);
191                         ASN1_STRING_print_ex(out, str, ASN1_STRFLGS_DUMP_UNKNOWN|ASN1_STRFLGS_SHOW_TYPE);
192                 }
193                 BIO_printf(out, "\n");
194         } else BIO_printf(out, "%*s%s [ABSENT]\n", indent, "", name);
195         return 1;
196 }