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"
72 /* Indicate missing OPTIONAL fields */
73 #define ASN1_PCTX_FLAGS_SHOW_ABSENT 0x001
74 /* Mark start and end of SEQUENCE */
75 #define ASN1_PCTX_FLAGS_SHOW_SEQUENCE 0x002
76 /* Mark start and end of SEQUENCE/SET OF */
77 #define ASN1_PCTX_FLAGS_SHOW_SSOF 0x004
78 /* Show the ASN1 type of primitives */
79 #define ASN1_PCTX_FLAGS_SHOW_TYPE 0x008
80 /* Don't show ASN1 type of ANY */
81 #define ASN1_PCTX_FLAGS_NO_ANY_TYPE 0x010
82 /* Don't show ASN1 type of MSTRINGs */
83 #define ASN1_PCTX_FLAGS_NO_MSTRING_TYPE 0x020
84 /* Don't show field names in SEQUENCE */
85 #define ASN1_PCTX_FLAGS_NO_FIELD_NAME 0x040
86 /* Show structure names of each SEQUENCE field */
87 #define ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME 0x080
88 /* Don't show structure name even at top level */
89 #define ASN1_PCTX_FLAGS_NO_STRUCT_NAME 0x100
94 /* ASN1_PCTX routines */
96 ASN1_PCTX default_pctx =
98 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
106 ASN1_PCTX *ASN1_PCTX_new(void)
109 ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
112 ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
123 void ASN1_PCTX_free(ASN1_PCTX *p)
128 unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
133 void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
138 unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
143 void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
148 unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
150 return p->cert_flags;
153 void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
155 p->cert_flags = flags;
158 unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
163 void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
165 p->oid_flags = flags;
168 unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
173 void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
175 p->str_flags = flags;
178 /* Main print routines */
180 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
182 const char *fname, const char *sname,
183 int nohdr, const ASN1_PCTX *pctx);
184 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
185 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
186 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
187 const ASN1_ITEM *it, int indent,
188 const char *fname, const char *sname,
189 const ASN1_PCTX *pctx);
190 static int asn1_print_fsname(BIO *out, int indent,
191 const char *fname, const char *sname,
192 const ASN1_PCTX *pctx);
194 int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
195 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
199 pctx = &default_pctx;
200 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
204 return asn1_item_print_ctx(out, &ifld, indent, it,
205 NULL, sname, 0, pctx);
208 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
210 const char *fname, const char *sname,
211 int nohdr, const ASN1_PCTX *pctx)
213 const ASN1_TEMPLATE *tt;
214 const ASN1_EXTERN_FUNCS *ef;
219 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
221 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
223 if (BIO_puts(out, "<ABSENT>\n") <= 0)
231 case ASN1_ITYPE_PRIMITIVE:
234 if (!asn1_template_print_ctx(out, fld, indent,
235 it->templates, pctx))
239 case ASN1_ITYPE_MSTRING:
240 if (!asn1_primitive_print(out, fld, it,
241 indent, fname, sname,pctx))
245 case ASN1_ITYPE_EXTERN:
246 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
248 /* Use new style print routine if possible */
250 if (ef && ef->asn1_ex_print)
252 i = ef->asn1_ex_print(out, fld, indent, "", pctx);
255 if ((i == 2) && (BIO_puts(out, "\n") <= 0))
260 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
264 case ASN1_ITYPE_CHOICE:
266 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
269 /* CHOICE type, get selector */
270 i = asn1_get_choice_selector(fld, it);
271 /* This should never happen... */
272 if((i < 0) || (i >= it->tcount))
275 "ERROR: selector [%d] invalid\n", i) <= 0)
279 tt = it->templates + i;
280 tmpfld = asn1_get_field_ptr(fld, tt);
281 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
285 case ASN1_ITYPE_SEQUENCE:
286 case ASN1_ITYPE_NDEF_SEQUENCE:
287 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
291 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
293 if (BIO_puts(out, " {\n") <= 0)
298 if (BIO_puts(out, "\n") <= 0)
303 /* Print each field entry */
304 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
306 const ASN1_TEMPLATE *seqtt;
307 seqtt = asn1_do_adb(fld, tt, 1);
308 tmpfld = asn1_get_field_ptr(fld, seqtt);
309 if (!asn1_template_print_ctx(out, tmpfld,
310 indent + 2, seqtt, pctx))
313 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
315 if (BIO_printf(out, "%*s}\n", indent, "") < 0)
321 BIO_printf(out, "Unprocessed type %d\n", it->itype);
328 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
329 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
332 const char *sname, *fname;
334 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
335 sname = tt->item->sname;
338 if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
341 fname = tt->field_name;
342 if(flags & ASN1_TFLG_SK_MASK)
346 /* SET OF, SEQUENCE OF */
349 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
351 if(flags & ASN1_TFLG_SET_OF)
355 if (BIO_printf(out, "%*s%s OF %s {\n",
356 indent, "", tname, tt->field_name) <= 0)
359 else if (BIO_printf(out, "%*s%s:\n", indent, "",
363 for(i = 0; i < sk_num((STACK *)*fld); i++)
365 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
368 skitem = (ASN1_VALUE *)sk_value((STACK *)*fld, i);
369 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
370 tt->item, NULL, NULL, 1, pctx))
373 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
375 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
377 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
382 return asn1_item_print_ctx(out, fld, indent, tt->item,
383 fname, sname, 0, pctx);
386 static int asn1_print_fsname(BIO *out, int indent,
387 const char *fname, const char *sname,
388 const ASN1_PCTX *pctx)
390 static char spaces[] = " ";
391 const int nspaces = sizeof(spaces) - 1;
394 if (!sname && !fname)
398 while (indent > nspaces)
400 if (BIO_write(out, spaces, nspaces) != nspaces)
404 if (BIO_write(out, spaces, indent) != indent)
406 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
408 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
410 if (!sname && !fname)
414 if (BIO_puts(out, fname) <= 0)
421 if (BIO_printf(out, " (%s)", sname) <= 0)
426 if (BIO_puts(out, sname) <= 0)
430 if (BIO_write(out, ": ", 2) != 2)
435 static int asn1_print_boolean_ctx(BIO *out, const int bool,
436 const ASN1_PCTX *pctx)
455 if (BIO_puts(out, str) <= 0)
461 static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
462 const ASN1_PCTX *pctx)
466 s = i2s_ASN1_INTEGER(NULL, str);
467 if (BIO_puts(out, s) <= 0)
473 static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
474 const ASN1_PCTX *pctx)
478 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
481 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
482 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
487 static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
488 const ASN1_PCTX *pctx)
490 if (str->type == V_ASN1_BIT_STRING)
492 if (BIO_printf(out, " (%ld unused bits)\n",
493 str->flags & 0x7) <= 0)
496 else if (BIO_puts(out, "\n") <= 0)
498 if (BIO_dump_indent(out, (char *)str->data, str->length,
504 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
505 const ASN1_ITEM *it, int indent,
506 const char *fname, const char *sname,
507 const ASN1_PCTX *pctx)
511 int ret = 1, needlf = 1;
513 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
515 str = (ASN1_STRING *)*fld;
516 if (it->itype == ASN1_ITYPE_MSTRING)
517 utype = str->type & ~V_ASN1_NEG;
520 if (utype == V_ASN1_ANY)
522 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
524 fld = (ASN1_VALUE **)&atype->value.ptr;
525 str = (ASN1_STRING *)*fld;
526 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
529 pname = ASN1_tag2str(utype);
533 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
534 pname = ASN1_tag2str(utype);
539 if (utype == V_ASN1_NULL)
541 if (BIO_puts(out, "NULL\n") <= 0)
548 if (BIO_puts(out, pname) <= 0)
550 if (BIO_puts(out, ":") <= 0)
558 int bool = *(int *)fld;
561 ret = asn1_print_boolean_ctx(out, bool, pctx);
566 case V_ASN1_ENUMERATED:
567 ret = asn1_print_integer_ctx(out, str, pctx);
571 ret = ASN1_UTCTIME_print(out, str);
574 case V_ASN1_GENERALIZEDTIME:
575 ret = ASN1_GENERALIZEDTIME_print(out, str);
579 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
582 case V_ASN1_OCTET_STRING:
583 case V_ASN1_BIT_STRING:
584 ret = asn1_print_obstring_ctx(out, str, indent, pctx);
588 case V_ASN1_SEQUENCE:
591 if (BIO_puts(out, "\n") <= 0)
593 if (ASN1_parse_dump(out, str->data, str->length,
600 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
605 if (needlf && BIO_puts(out, "\n") <= 0)