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).
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 /* SET OF, SEQUENCE OF */
360 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
362 if(flags & ASN1_TFLG_SET_OF)
366 if (BIO_printf(out, "%*s%s OF %s {\n",
367 indent, "", tname, tt->field_name) <= 0)
370 else if (BIO_printf(out, "%*s%s:\n", indent, "",
374 for(i = 0; i < sk_num((STACK *)*fld); i++)
376 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
379 skitem = (ASN1_VALUE *)sk_value((STACK *)*fld, i);
380 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
381 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
384 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
386 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
388 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
393 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
394 fname, sname, 0, pctx);
397 static int asn1_print_fsname(BIO *out, int indent,
398 const char *fname, const char *sname,
399 const ASN1_PCTX *pctx)
401 static char spaces[] = " ";
402 const int nspaces = sizeof(spaces) - 1;
405 if (!sname && !fname)
409 while (indent > nspaces)
411 if (BIO_write(out, spaces, nspaces) != nspaces)
415 if (BIO_write(out, spaces, indent) != indent)
417 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
419 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
421 if (!sname && !fname)
425 if (BIO_puts(out, fname) <= 0)
432 if (BIO_printf(out, " (%s)", sname) <= 0)
437 if (BIO_puts(out, sname) <= 0)
441 if (BIO_write(out, ": ", 2) != 2)
446 static int asn1_print_boolean_ctx(BIO *out, const int bool,
447 const ASN1_PCTX *pctx)
466 if (BIO_puts(out, str) <= 0)
472 static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
473 const ASN1_PCTX *pctx)
477 s = i2s_ASN1_INTEGER(NULL, str);
478 if (BIO_puts(out, s) <= 0)
484 static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
485 const ASN1_PCTX *pctx)
489 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
492 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
493 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
498 static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
499 const ASN1_PCTX *pctx)
501 if (str->type == V_ASN1_BIT_STRING)
503 if (BIO_printf(out, " (%ld unused bits)\n",
504 str->flags & 0x7) <= 0)
507 else if (BIO_puts(out, "\n") <= 0)
509 if ((str->length > 0)
510 && BIO_dump_indent(out, (char *)str->data, str->length,
516 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
517 const ASN1_ITEM *it, int indent,
518 const char *fname, const char *sname,
519 const ASN1_PCTX *pctx)
523 int ret = 1, needlf = 1;
525 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
527 str = (ASN1_STRING *)*fld;
528 if (it->itype == ASN1_ITYPE_MSTRING)
529 utype = str->type & ~V_ASN1_NEG;
532 if (utype == V_ASN1_ANY)
534 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
536 fld = (ASN1_VALUE **)&atype->value.ptr;
537 str = (ASN1_STRING *)*fld;
538 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
541 pname = ASN1_tag2str(utype);
545 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
546 pname = ASN1_tag2str(utype);
551 if (utype == V_ASN1_NULL)
553 if (BIO_puts(out, "NULL\n") <= 0)
560 if (BIO_puts(out, pname) <= 0)
562 if (BIO_puts(out, ":") <= 0)
570 int bool = *(int *)fld;
573 ret = asn1_print_boolean_ctx(out, bool, pctx);
578 case V_ASN1_ENUMERATED:
579 ret = asn1_print_integer_ctx(out, str, pctx);
583 ret = ASN1_UTCTIME_print(out, str);
586 case V_ASN1_GENERALIZEDTIME:
587 ret = ASN1_GENERALIZEDTIME_print(out, str);
591 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
594 case V_ASN1_OCTET_STRING:
595 case V_ASN1_BIT_STRING:
596 ret = asn1_print_obstring_ctx(out, str, indent, pctx);
600 case V_ASN1_SEQUENCE:
603 if (BIO_puts(out, "\n") <= 0)
605 if (ASN1_parse_dump(out, str->data, str->length,
612 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
617 if (needlf && BIO_puts(out, "\n") <= 0)