2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000,2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
61 #include <openssl/asn1.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/objects.h>
64 #include <openssl/buffer.h>
65 #include <openssl/err.h>
66 #include <openssl/x509v3.h>
67 #include "asn1_locl.h"
69 #define ASN1_PCTX_FLAGS_SHOW_ABSENT 0x1
70 #define ASN1_PCTX_FLAGS_SHOW_SEQUENCE 0x2
71 #define ASN1_PCTX_FLAGS_SHOW_TYPE 0x4
72 #define ASN1_PCTX_FLAGS_NO_FIELD_NAME 0x8
73 #define ASN1_PCTX_FLAGS_NO_STRUCT_NAME 0x10
78 /* ASN1_PCTX routines */
80 ASN1_PCTX default_pctx =
82 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
90 ASN1_PCTX *ASN1_PCTX_new(void)
93 ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
96 ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
107 void ASN1_PCTX_free(ASN1_PCTX *p)
112 unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
117 void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
122 unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
127 void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
132 unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
134 return p->cert_flags;
137 void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
139 p->cert_flags = flags;
142 unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
147 void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
149 p->oid_flags = flags;
152 unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
157 void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
159 p->str_flags = flags;
162 /* Main print routines */
164 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
165 const ASN1_ITEM *it, const ASN1_PCTX *pctx);
166 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
167 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
168 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
169 const ASN1_ITEM *it, int indent,
170 const ASN1_PCTX *pctx);
172 int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
173 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
176 pctx = &default_pctx;
177 return asn1_item_print_ctx(out, &ifld, indent, it, pctx);
180 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
181 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
183 const ASN1_TEMPLATE *tt;
184 const ASN1_EXTERN_FUNCS *ef;
187 if (BIO_printf(out, "%s:", it->sname) <= 0)
191 if (!(pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT))
193 if (BIO_puts(out, "\n") <= 0)
198 if (BIO_puts(out, "<ABSENT>\n") <= 0)
206 case ASN1_ITYPE_PRIMITIVE:
209 if (!asn1_template_print_ctx(out, fld, indent,
210 it->templates, pctx))
214 case ASN1_ITYPE_MSTRING:
215 if (!asn1_primitive_print(out, fld, it, indent, pctx))
219 case ASN1_ITYPE_EXTERN:
220 /* Use new style print routine if possible */
222 if (ef && ef->asn1_ex_print)
224 if (!ef->asn1_ex_print(out, fld, indent, "", pctx))
227 else if (BIO_printf(out, ":EXTERNAL TYPE %s\n", it->sname) <= 0)
231 case ASN1_ITYPE_CHOICE:
232 /* CHOICE type, get selector */
233 i = asn1_get_choice_selector(fld, it);
234 /* This should never happen... */
235 if((i < 0) || (i >= it->tcount))
238 "ERROR: selector [%d] invalid\n", i) <= 0)
242 tt = it->templates + i;
243 tmpfld = asn1_get_field_ptr(fld, tt);
244 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
248 case ASN1_ITYPE_SEQUENCE:
249 case ASN1_ITYPE_NDEF_SEQUENCE:
250 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
252 if (BIO_puts(out, " {\n") <= 0)
257 if (BIO_puts(out, "\n") <= 0)
261 /* Print each field entry */
262 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
264 const ASN1_TEMPLATE *seqtt;
265 seqtt = asn1_do_adb(fld, tt, 1);
266 tmpfld = asn1_get_field_ptr(fld, seqtt);
267 if (!asn1_template_print_ctx(out, tmpfld,
268 indent + 2, seqtt, pctx))
271 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
273 if (BIO_printf(out, "%*s}\n", indent, "") < 0)
279 BIO_printf(out, "Unprocessed type %d\n", it->itype);
282 if (BIO_puts(out, "\n") <= 0)
287 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
288 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
292 if(flags & ASN1_TFLG_SK_MASK)
296 /* SET OF, SEQUENCE OF */
297 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
299 if(flags & ASN1_TFLG_SET_OF)
303 if (BIO_printf(out, "%*s%s OF %s {\n",
304 indent, "", tname, tt->field_name) <= 0)
307 else if (BIO_printf(out, "%*s%s:\n", indent, "",
308 tt->field_name) <= 0)
310 for(i = 0; i < sk_num((STACK *)*fld); i++)
312 if (BIO_printf(out, "%*s", indent + 2, "") <= 0)
314 skitem = (ASN1_VALUE *)sk_value((STACK *)*fld, i);
315 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
319 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
321 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
326 else if (BIO_printf(out, "%*s%s:", indent, "", tt->field_name) <= 0)
328 return asn1_item_print_ctx(out, fld, indent, tt->item, pctx);
331 int asn1_print_fsname(BIO *out, int indent,
332 const char *fname, const char *sname,
333 const ASN1_PCTX *pctx)
335 static char spaces[] = " ";
336 const int nspaces = sizeof(spaces) - 1;
337 while (indent > nspaces)
339 if (BIO_write(out, spaces, nspaces) != nspaces)
343 if (BIO_write(out, spaces, indent) != indent)
346 (ASN1_PCTX_FLAGS_NO_FIELD_NAME|ASN1_PCTX_FLAGS_NO_STRUCT_NAME)))
348 if (fname && !(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME))
350 if (BIO_puts(out, fname) <= 0)
353 if (sname && !(pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME))
355 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
357 if (BIO_puts(out, sname) <= 0)
360 else if (BIO_printf(out, " (%s)", sname) <= 0)
363 if (BIO_write(out, ":", 1) != 1)
368 int asn1_print_boolean_ctx(BIO *out, const int bool, const ASN1_PCTX *pctx)
387 if (BIO_puts(out, str) <= 0)
393 static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
394 const ASN1_PCTX *pctx)
398 s = i2s_ASN1_INTEGER(NULL, str);
399 if (BIO_printf(out, "%s", s) <= 0)
405 static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
406 const ASN1_PCTX *pctx)
410 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
413 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
414 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
419 static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
420 const ASN1_PCTX *pctx)
422 if (str->type == V_ASN1_BIT_STRING)
424 if (BIO_printf(out, " (%ld unused bits)\n",
425 str->flags & 0x7) <= 0)
428 else if (BIO_puts(out, "\n") <= 0)
430 if (BIO_dump_indent(out, (char *)str->data, str->length,
436 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
437 const ASN1_ITEM *it, int indent,
438 const ASN1_PCTX *pctx)
443 str = (ASN1_STRING *)*fld;
444 if (it->itype == ASN1_ITYPE_MSTRING)
445 utype = str->type & ~V_ASN1_NEG;
448 if (utype == V_ASN1_ANY)
450 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
452 fld = (ASN1_VALUE **)&atype->value.ptr;
453 str = (ASN1_STRING *)*fld;
455 if (BIO_puts(out, ASN1_tag2str(utype)) <= 0)
457 if (utype != V_ASN1_NULL && BIO_puts(out, ":") <= 0)
464 int bool = *(int *)fld;
467 ret = asn1_print_boolean_ctx(out, bool, pctx);
472 case V_ASN1_ENUMERATED:
473 ret = asn1_print_integer_ctx(out, str, pctx);
480 ret = ASN1_UTCTIME_print(out, str);
483 case V_ASN1_GENERALIZEDTIME:
484 ret = ASN1_GENERALIZEDTIME_print(out, str);
488 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
491 case V_ASN1_OCTET_STRING:
492 case V_ASN1_BIT_STRING:
493 ret = asn1_print_obstring_ctx(out, str, indent, pctx);
496 case V_ASN1_SEQUENCE:
498 if (ASN1_parse_dump(out, str->data, str->length,
504 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);