2 /* Written by Dr Stephen N Henson (steve@openssl.org) 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).
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/objects.h>
65 #include <openssl/buffer.h>
66 #include <openssl/err.h>
67 #include <openssl/x509v3.h>
68 #include "asn1_locl.h"
73 /* ASN1_PCTX routines */
75 ASN1_PCTX default_pctx =
77 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
85 ASN1_PCTX *ASN1_PCTX_new(void)
88 ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
91 ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
102 void ASN1_PCTX_free(ASN1_PCTX *p)
107 unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
112 void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
117 unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
122 void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
127 unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
129 return p->cert_flags;
132 void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
134 p->cert_flags = flags;
137 unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
142 void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
144 p->oid_flags = flags;
147 unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
152 void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
154 p->str_flags = flags;
157 /* Main print routines */
159 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
161 const char *fname, const char *sname,
162 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);
167 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
168 const ASN1_ITEM *it, int indent,
169 const char *fname, const char *sname,
170 const ASN1_PCTX *pctx);
172 static int asn1_print_fsname(BIO *out, int indent,
173 const char *fname, const char *sname,
174 const ASN1_PCTX *pctx);
176 int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
177 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
181 pctx = &default_pctx;
182 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
186 return asn1_item_print_ctx(out, &ifld, indent, it,
187 NULL, sname, 0, pctx);
190 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
192 const char *fname, const char *sname,
193 int nohdr, const ASN1_PCTX *pctx)
195 const ASN1_TEMPLATE *tt;
196 const ASN1_EXTERN_FUNCS *ef;
198 const ASN1_AUX *aux = it->funcs;
199 ASN1_aux_cb *asn1_cb;
202 if (aux && aux->asn1_cb)
205 parg.indent = indent;
207 asn1_cb = aux->asn1_cb;
213 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
215 if (!nohdr && !asn1_print_fsname(out, indent,
218 if (BIO_puts(out, "<ABSENT>\n") <= 0)
226 case ASN1_ITYPE_PRIMITIVE:
229 if (!asn1_template_print_ctx(out, fld, indent,
230 it->templates, pctx))
234 case ASN1_ITYPE_MSTRING:
235 if (!asn1_primitive_print(out, fld, it,
236 indent, fname, sname,pctx))
240 case ASN1_ITYPE_EXTERN:
241 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
243 /* Use new style print routine if possible */
245 if (ef && ef->asn1_ex_print)
247 i = ef->asn1_ex_print(out, fld, indent, "", pctx);
250 if ((i == 2) && (BIO_puts(out, "\n") <= 0))
255 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
259 case ASN1_ITYPE_CHOICE:
261 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
264 /* CHOICE type, get selector */
265 i = asn1_get_choice_selector(fld, it);
266 /* This should never happen... */
267 if((i < 0) || (i >= it->tcount))
270 "ERROR: selector [%d] invalid\n", i) <= 0)
274 tt = it->templates + i;
275 tmpfld = asn1_get_field_ptr(fld, tt);
276 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
280 case ASN1_ITYPE_SEQUENCE:
281 case ASN1_ITYPE_NDEF_SEQUENCE:
282 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
286 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
288 if (BIO_puts(out, " {\n") <= 0)
293 if (BIO_puts(out, "\n") <= 0)
300 i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
307 /* Print each field entry */
308 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
310 const ASN1_TEMPLATE *seqtt;
311 seqtt = asn1_do_adb(fld, tt, 1);
312 tmpfld = asn1_get_field_ptr(fld, seqtt);
313 if (!asn1_template_print_ctx(out, tmpfld,
314 indent + 2, seqtt, pctx))
317 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
319 if (BIO_printf(out, "%*s}\n", indent, "") < 0)
325 i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
332 BIO_printf(out, "Unprocessed type %d\n", it->itype);
339 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
340 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
343 const char *sname, *fname;
345 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
346 sname = ASN1_ITEM_ptr(tt->item)->sname;
349 if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
352 fname = tt->field_name;
353 if(flags & ASN1_TFLG_SK_MASK)
357 STACK_OF(ASN1_VALUE) *stack;
359 /* SET OF, SEQUENCE OF */
362 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
364 if(flags & ASN1_TFLG_SET_OF)
368 if (BIO_printf(out, "%*s%s OF %s {\n",
369 indent, "", tname, tt->field_name) <= 0)
372 else if (BIO_printf(out, "%*s%s:\n", indent, "",
376 stack = (STACK_OF(ASN1_VALUE) *)*fld;
377 for(i = 0; i < sk_ASN1_VALUE_num(stack); i++)
379 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
382 skitem = sk_ASN1_VALUE_value(stack, i);
383 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
384 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
387 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
389 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
391 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
396 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
397 fname, sname, 0, pctx);
400 static int asn1_print_fsname(BIO *out, int indent,
401 const char *fname, const char *sname,
402 const ASN1_PCTX *pctx)
404 static char spaces[] = " ";
405 const int nspaces = sizeof(spaces) - 1;
408 if (!sname && !fname)
412 while (indent > nspaces)
414 if (BIO_write(out, spaces, nspaces) != nspaces)
418 if (BIO_write(out, spaces, indent) != indent)
420 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
422 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
424 if (!sname && !fname)
428 if (BIO_puts(out, fname) <= 0)
435 if (BIO_printf(out, " (%s)", sname) <= 0)
440 if (BIO_puts(out, sname) <= 0)
444 if (BIO_write(out, ": ", 2) != 2)
449 static int asn1_print_boolean_ctx(BIO *out, int boolval,
450 const ASN1_PCTX *pctx)
469 if (BIO_puts(out, str) <= 0)
475 static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
476 const ASN1_PCTX *pctx)
480 s = i2s_ASN1_INTEGER(NULL, str);
481 if (BIO_puts(out, s) <= 0)
487 static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
488 const ASN1_PCTX *pctx)
492 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
495 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
496 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
501 static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
502 const ASN1_PCTX *pctx)
504 if (str->type == V_ASN1_BIT_STRING)
506 if (BIO_printf(out, " (%ld unused bits)\n",
507 str->flags & 0x7) <= 0)
510 else if (BIO_puts(out, "\n") <= 0)
512 if ((str->length > 0)
513 && BIO_dump_indent(out, (char *)str->data, str->length,
519 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
520 const ASN1_ITEM *it, int indent,
521 const char *fname, const char *sname,
522 const ASN1_PCTX *pctx)
526 int ret = 1, needlf = 1;
528 const ASN1_PRIMITIVE_FUNCS *pf;
530 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
532 if (pf && pf->prim_print)
533 return pf->prim_print(out, fld, it, indent, pctx);
534 str = (ASN1_STRING *)*fld;
535 if (it->itype == ASN1_ITYPE_MSTRING)
536 utype = str->type & ~V_ASN1_NEG;
539 if (utype == V_ASN1_ANY)
541 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
543 fld = &atype->value.asn1_value;
544 str = (ASN1_STRING *)*fld;
545 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
548 pname = ASN1_tag2str(utype);
552 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
553 pname = ASN1_tag2str(utype);
558 if (utype == V_ASN1_NULL)
560 if (BIO_puts(out, "NULL\n") <= 0)
567 if (BIO_puts(out, pname) <= 0)
569 if (BIO_puts(out, ":") <= 0)
577 int boolval = *(int *)fld;
580 ret = asn1_print_boolean_ctx(out, boolval, pctx);
585 case V_ASN1_ENUMERATED:
586 ret = asn1_print_integer_ctx(out, str, pctx);
590 ret = ASN1_UTCTIME_print(out, str);
593 case V_ASN1_GENERALIZEDTIME:
594 ret = ASN1_GENERALIZEDTIME_print(out, str);
598 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
601 case V_ASN1_OCTET_STRING:
602 case V_ASN1_BIT_STRING:
603 ret = asn1_print_obstring_ctx(out, str, indent, pctx);
607 case V_ASN1_SEQUENCE:
610 if (BIO_puts(out, "\n") <= 0)
612 if (ASN1_parse_dump(out, str->data, str->length,
619 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
624 if (needlf && BIO_puts(out, "\n") <= 0)