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"
74 /* ASN1_PCTX routines */
76 ASN1_PCTX default_pctx =
78 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
86 ASN1_PCTX *ASN1_PCTX_new(void)
89 ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
92 ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
103 void ASN1_PCTX_free(ASN1_PCTX *p)
108 unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
113 void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
118 unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
123 void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
128 unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
130 return p->cert_flags;
133 void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
135 p->cert_flags = flags;
138 unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
143 void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
145 p->oid_flags = flags;
148 unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
153 void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
155 p->str_flags = flags;
158 /* Main print routines */
160 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
162 const char *fname, const char *sname,
163 int nohdr, const ASN1_PCTX *pctx);
164 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
165 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
166 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
167 const ASN1_ITEM *it, int indent,
168 const char *fname, const char *sname,
169 const ASN1_PCTX *pctx);
170 static int asn1_print_fsname(BIO *out, int indent,
171 const char *fname, const char *sname,
172 const ASN1_PCTX *pctx);
174 int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
175 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
179 pctx = &default_pctx;
180 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
184 return asn1_item_print_ctx(out, &ifld, indent, it,
185 NULL, sname, 0, pctx);
188 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
190 const char *fname, const char *sname,
191 int nohdr, const ASN1_PCTX *pctx)
193 const ASN1_TEMPLATE *tt;
194 const ASN1_EXTERN_FUNCS *ef;
199 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
201 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
203 if (BIO_puts(out, "<ABSENT>\n") <= 0)
211 case ASN1_ITYPE_PRIMITIVE:
214 if (!asn1_template_print_ctx(out, fld, indent,
215 it->templates, pctx))
219 case ASN1_ITYPE_MSTRING:
220 if (!asn1_primitive_print(out, fld, it,
221 indent, fname, sname,pctx))
225 case ASN1_ITYPE_EXTERN:
226 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
228 /* Use new style print routine if possible */
230 if (ef && ef->asn1_ex_print)
232 i = ef->asn1_ex_print(out, fld, indent, "", pctx);
235 if ((i == 2) && (BIO_puts(out, "\n") <= 0))
240 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
244 case ASN1_ITYPE_CHOICE:
246 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
249 /* CHOICE type, get selector */
250 i = asn1_get_choice_selector(fld, it);
251 /* This should never happen... */
252 if((i < 0) || (i >= it->tcount))
255 "ERROR: selector [%d] invalid\n", i) <= 0)
259 tt = it->templates + i;
260 tmpfld = asn1_get_field_ptr(fld, tt);
261 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
265 case ASN1_ITYPE_SEQUENCE:
266 case ASN1_ITYPE_NDEF_SEQUENCE:
267 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
271 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
273 if (BIO_puts(out, " {\n") <= 0)
278 if (BIO_puts(out, "\n") <= 0)
283 /* Print each field entry */
284 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
286 const ASN1_TEMPLATE *seqtt;
287 seqtt = asn1_do_adb(fld, tt, 1);
288 tmpfld = asn1_get_field_ptr(fld, seqtt);
289 if (!asn1_template_print_ctx(out, tmpfld,
290 indent + 2, seqtt, pctx))
293 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
295 if (BIO_printf(out, "%*s}\n", indent, "") < 0)
301 BIO_printf(out, "Unprocessed type %d\n", it->itype);
308 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
309 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
312 const char *sname, *fname;
314 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
315 sname = tt->item->sname;
318 if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
321 fname = tt->field_name;
322 if(flags & ASN1_TFLG_SK_MASK)
326 /* SET OF, SEQUENCE OF */
329 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
331 if(flags & ASN1_TFLG_SET_OF)
335 if (BIO_printf(out, "%*s%s OF %s {\n",
336 indent, "", tname, tt->field_name) <= 0)
339 else if (BIO_printf(out, "%*s%s:\n", indent, "",
343 for(i = 0; i < sk_num((STACK *)*fld); i++)
345 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
348 skitem = (ASN1_VALUE *)sk_value((STACK *)*fld, i);
349 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
350 tt->item, NULL, NULL, 1, pctx))
353 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
355 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
357 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
362 return asn1_item_print_ctx(out, fld, indent, tt->item,
363 fname, sname, 0, pctx);
366 static int asn1_print_fsname(BIO *out, int indent,
367 const char *fname, const char *sname,
368 const ASN1_PCTX *pctx)
370 static char spaces[] = " ";
371 const int nspaces = sizeof(spaces) - 1;
374 if (!sname && !fname)
378 while (indent > nspaces)
380 if (BIO_write(out, spaces, nspaces) != nspaces)
384 if (BIO_write(out, spaces, indent) != indent)
386 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
388 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
390 if (!sname && !fname)
394 if (BIO_puts(out, fname) <= 0)
401 if (BIO_printf(out, " (%s)", sname) <= 0)
406 if (BIO_puts(out, sname) <= 0)
410 if (BIO_write(out, ": ", 2) != 2)
415 static int asn1_print_boolean_ctx(BIO *out, const int bool,
416 const ASN1_PCTX *pctx)
435 if (BIO_puts(out, str) <= 0)
441 static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
442 const ASN1_PCTX *pctx)
446 s = i2s_ASN1_INTEGER(NULL, str);
447 if (BIO_puts(out, s) <= 0)
453 static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
454 const ASN1_PCTX *pctx)
458 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
461 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
462 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
467 static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
468 const ASN1_PCTX *pctx)
470 if (str->type == V_ASN1_BIT_STRING)
472 if (BIO_printf(out, " (%ld unused bits)\n",
473 str->flags & 0x7) <= 0)
476 else if (BIO_puts(out, "\n") <= 0)
478 if (BIO_dump_indent(out, (char *)str->data, str->length,
484 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
485 const ASN1_ITEM *it, int indent,
486 const char *fname, const char *sname,
487 const ASN1_PCTX *pctx)
491 int ret = 1, needlf = 1;
493 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
495 str = (ASN1_STRING *)*fld;
496 if (it->itype == ASN1_ITYPE_MSTRING)
497 utype = str->type & ~V_ASN1_NEG;
500 if (utype == V_ASN1_ANY)
502 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
504 fld = (ASN1_VALUE **)&atype->value.ptr;
505 str = (ASN1_STRING *)*fld;
506 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
509 pname = ASN1_tag2str(utype);
513 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
514 pname = ASN1_tag2str(utype);
519 if (utype == V_ASN1_NULL)
521 if (BIO_puts(out, "NULL\n") <= 0)
528 if (BIO_puts(out, pname) <= 0)
530 if (BIO_puts(out, ":") <= 0)
538 int bool = *(int *)fld;
541 ret = asn1_print_boolean_ctx(out, bool, pctx);
546 case V_ASN1_ENUMERATED:
547 ret = asn1_print_integer_ctx(out, str, pctx);
551 ret = ASN1_UTCTIME_print(out, str);
554 case V_ASN1_GENERALIZEDTIME:
555 ret = ASN1_GENERALIZEDTIME_print(out, str);
559 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
562 case V_ASN1_OCTET_STRING:
563 case V_ASN1_BIT_STRING:
564 ret = asn1_print_obstring_ctx(out, str, indent, pctx);
568 case V_ASN1_SEQUENCE:
571 if (BIO_puts(out, "\n") <= 0)
573 if (ASN1_parse_dump(out, str->data, str->length,
580 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
585 if (needlf && BIO_puts(out, "\n") <= 0)