1 /* $OpenBSD: tasn_prn.c,v 1.16 2017/01/29 17:49:22 beck Exp $ */
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).
61 #include <openssl/asn1.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/buffer.h>
64 #include <openssl/err.h>
65 #include <openssl/objects.h>
66 #include <openssl/x509v3.h>
68 #include "asn1_locl.h"
73 /* ASN1_PCTX routines */
75 ASN1_PCTX default_pctx
= {
76 ASN1_PCTX_FLAGS_SHOW_ABSENT
, /* flags */
88 ret
= malloc(sizeof(ASN1_PCTX
));
90 ASN1error(ERR_R_MALLOC_FAILURE
);
102 ASN1_PCTX_free(ASN1_PCTX
*p
)
108 ASN1_PCTX_get_flags(ASN1_PCTX
*p
)
114 ASN1_PCTX_set_flags(ASN1_PCTX
*p
, unsigned long flags
)
120 ASN1_PCTX_get_nm_flags(ASN1_PCTX
*p
)
126 ASN1_PCTX_set_nm_flags(ASN1_PCTX
*p
, unsigned long flags
)
132 ASN1_PCTX_get_cert_flags(ASN1_PCTX
*p
)
134 return p
->cert_flags
;
138 ASN1_PCTX_set_cert_flags(ASN1_PCTX
*p
, unsigned long flags
)
140 p
->cert_flags
= flags
;
144 ASN1_PCTX_get_oid_flags(ASN1_PCTX
*p
)
150 ASN1_PCTX_set_oid_flags(ASN1_PCTX
*p
, unsigned long flags
)
152 p
->oid_flags
= flags
;
156 ASN1_PCTX_get_str_flags(ASN1_PCTX
*p
)
162 ASN1_PCTX_set_str_flags(ASN1_PCTX
*p
, unsigned long flags
)
164 p
->str_flags
= flags
;
167 /* Main print routines */
169 static int asn1_item_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
170 const ASN1_ITEM
*it
, const char *fname
, const char *sname
, int nohdr
,
171 const ASN1_PCTX
*pctx
);
173 int asn1_template_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
174 const ASN1_TEMPLATE
*tt
, const ASN1_PCTX
*pctx
);
176 static int asn1_primitive_print(BIO
*out
, ASN1_VALUE
**fld
,
177 const ASN1_ITEM
*it
, int indent
, const char *fname
, const char *sname
,
178 const ASN1_PCTX
*pctx
);
180 static int asn1_print_fsname(BIO
*out
, int indent
, const char *fname
,
181 const char *sname
, const ASN1_PCTX
*pctx
);
184 ASN1_item_print(BIO
*out
, ASN1_VALUE
*ifld
, int indent
, const ASN1_ITEM
*it
,
185 const ASN1_PCTX
*pctx
)
190 pctx
= &default_pctx
;
191 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_STRUCT_NAME
)
195 return asn1_item_print_ctx(out
, &ifld
, indent
, it
, NULL
, sname
,
200 asn1_item_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
, const ASN1_ITEM
*it
,
201 const char *fname
, const char *sname
, int nohdr
, const ASN1_PCTX
*pctx
)
203 const ASN1_TEMPLATE
*tt
;
204 const ASN1_EXTERN_FUNCS
*ef
;
206 const ASN1_AUX
*aux
= it
->funcs
;
207 ASN1_aux_cb
*asn1_cb
;
211 if (aux
&& aux
->asn1_cb
) {
213 parg
.indent
= indent
;
215 asn1_cb
= aux
->asn1_cb
;
220 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_ABSENT
) {
222 !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
224 if (BIO_puts(out
, "<ABSENT>\n") <= 0)
231 case ASN1_ITYPE_PRIMITIVE
:
233 if (!asn1_template_print_ctx(out
, fld
, indent
,
234 it
->templates
, pctx
))
238 case ASN1_ITYPE_MSTRING
:
239 if (!asn1_primitive_print(out
, fld
, it
,
240 indent
, fname
, sname
, pctx
))
244 case ASN1_ITYPE_EXTERN
:
246 !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
248 /* Use new style print routine if possible */
250 if (ef
&& ef
->asn1_ex_print
) {
251 i
= ef
->asn1_ex_print(out
, fld
, indent
, "", pctx
);
254 if ((i
== 2) && (BIO_puts(out
, "\n") <= 0))
258 BIO_printf(out
, ":EXTERNAL TYPE %s\n", sname
) <= 0)
262 case ASN1_ITYPE_CHOICE
:
263 /* CHOICE type, get selector */
264 i
= asn1_get_choice_selector(fld
, it
);
265 /* This should never happen... */
266 if ((i
< 0) || (i
>= it
->tcount
)) {
268 "ERROR: selector [%d] invalid\n", i
) <= 0)
272 tt
= it
->templates
+ i
;
273 tmpfld
= asn1_get_field_ptr(fld
, tt
);
274 if (!asn1_template_print_ctx(out
, tmpfld
, indent
, tt
, pctx
))
278 case ASN1_ITYPE_SEQUENCE
:
279 case ASN1_ITYPE_NDEF_SEQUENCE
:
281 !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
283 if (fname
|| sname
) {
284 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
) {
285 if (BIO_puts(out
, " {\n") <= 0)
288 if (BIO_puts(out
, "\n") <= 0)
294 i
= asn1_cb(ASN1_OP_PRINT_PRE
, fld
, it
, &parg
);
301 /* Print each field entry */
302 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
303 const ASN1_TEMPLATE
*seqtt
;
305 seqtt
= asn1_do_adb(fld
, tt
, 1);
308 tmpfld
= asn1_get_field_ptr(fld
, seqtt
);
309 if (!asn1_template_print_ctx(out
, tmpfld
, indent
+ 2,
313 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
) {
314 if (BIO_printf(out
, "%*s}\n", indent
, "") < 0)
319 i
= asn1_cb(ASN1_OP_PRINT_POST
, fld
, it
, &parg
);
326 BIO_printf(out
, "Unprocessed type %d\n", it
->itype
);
334 asn1_template_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
335 const ASN1_TEMPLATE
*tt
, const ASN1_PCTX
*pctx
)
338 const char *sname
, *fname
;
341 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME
)
342 sname
= tt
->item
->sname
;
345 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_FIELD_NAME
)
348 fname
= tt
->field_name
;
349 if (flags
& ASN1_TFLG_SK_MASK
) {
352 STACK_OF(ASN1_VALUE
) *stack
;
354 /* SET OF, SEQUENCE OF */
356 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SSOF
) {
357 if (flags
& ASN1_TFLG_SET_OF
)
361 if (BIO_printf(out
, "%*s%s OF %s {\n",
362 indent
, "", tname
, tt
->field_name
) <= 0)
364 } else if (BIO_printf(out
, "%*s%s:\n", indent
, "",
368 stack
= (STACK_OF(ASN1_VALUE
) *)*fld
;
369 for (i
= 0; i
< sk_ASN1_VALUE_num(stack
); i
++) {
370 if ((i
> 0) && (BIO_puts(out
, "\n") <= 0))
372 skitem
= sk_ASN1_VALUE_value(stack
, i
);
373 if (!asn1_item_print_ctx(out
, &skitem
, indent
+ 2,
374 tt
->item
, NULL
, NULL
, 1, pctx
))
377 if (!i
&& BIO_printf(out
, "%*s<EMPTY>\n", indent
+ 2, "") <= 0)
379 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
) {
380 if (BIO_printf(out
, "%*s}\n", indent
, "") <= 0)
385 return asn1_item_print_ctx(out
, fld
, indent
, tt
->item
,
386 fname
, sname
, 0, pctx
);
390 asn1_print_fsname(BIO
*out
, int indent
, const char *fname
, const char *sname
,
391 const ASN1_PCTX
*pctx
)
393 static char spaces
[] = " ";
394 const int nspaces
= sizeof(spaces
) - 1;
396 while (indent
> nspaces
) {
397 if (BIO_write(out
, spaces
, nspaces
) != nspaces
)
401 if (BIO_write(out
, spaces
, indent
) != indent
)
403 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_STRUCT_NAME
)
405 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_FIELD_NAME
)
407 if (!sname
&& !fname
)
410 if (BIO_puts(out
, fname
) <= 0)
415 if (BIO_printf(out
, " (%s)", sname
) <= 0)
418 if (BIO_puts(out
, sname
) <= 0)
422 if (BIO_write(out
, ": ", 2) != 2)
428 asn1_print_boolean_ctx(BIO
*out
, int boolval
, const ASN1_PCTX
*pctx
)
446 if (BIO_puts(out
, str
) <= 0)
453 asn1_print_integer_ctx(BIO
*out
, ASN1_INTEGER
*str
, const ASN1_PCTX
*pctx
)
457 s
= i2s_ASN1_INTEGER(NULL
, str
);
458 if (BIO_puts(out
, s
) <= 0)
465 asn1_print_oid_ctx(BIO
*out
, const ASN1_OBJECT
*oid
, const ASN1_PCTX
*pctx
)
469 ln
= OBJ_nid2ln(OBJ_obj2nid(oid
));
472 OBJ_obj2txt(objbuf
, sizeof objbuf
, oid
, 1);
473 if (BIO_printf(out
, "%s (%s)", ln
, objbuf
) <= 0)
479 asn1_print_obstring_ctx(BIO
*out
, ASN1_STRING
*str
, int indent
,
480 const ASN1_PCTX
*pctx
)
482 if (str
->type
== V_ASN1_BIT_STRING
) {
483 if (BIO_printf(out
, " (%ld unused bits)\n",
484 str
->flags
& 0x7) <= 0)
486 } else if (BIO_puts(out
, "\n") <= 0)
488 if ((str
->length
> 0) &&
489 BIO_dump_indent(out
, (char *)str
->data
, str
->length
,
496 asn1_primitive_print(BIO
*out
, ASN1_VALUE
**fld
, const ASN1_ITEM
*it
,
497 int indent
, const char *fname
, const char *sname
, const ASN1_PCTX
*pctx
)
501 int ret
= 1, needlf
= 1;
503 const ASN1_PRIMITIVE_FUNCS
*pf
;
506 if (!asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
508 if (pf
&& pf
->prim_print
)
509 return pf
->prim_print(out
, fld
, it
, indent
, pctx
);
510 str
= (ASN1_STRING
*)*fld
;
511 if (it
->itype
== ASN1_ITYPE_MSTRING
)
512 utype
= str
->type
& ~V_ASN1_NEG
;
515 if (utype
== V_ASN1_ANY
) {
516 ASN1_TYPE
*atype
= (ASN1_TYPE
*)*fld
;
518 fld
= &atype
->value
.asn1_value
;
519 str
= (ASN1_STRING
*)*fld
;
520 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_ANY_TYPE
)
523 pname
= ASN1_tag2str(utype
);
525 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_TYPE
)
526 pname
= ASN1_tag2str(utype
);
531 if (utype
== V_ASN1_NULL
) {
532 if (BIO_puts(out
, "NULL\n") <= 0)
538 if (BIO_puts(out
, pname
) <= 0)
540 if (BIO_puts(out
, ":") <= 0)
547 int boolval
= *(int *)fld
;
550 ret
= asn1_print_boolean_ctx(out
, boolval
, pctx
);
555 case V_ASN1_ENUMERATED
:
556 ret
= asn1_print_integer_ctx(out
, str
, pctx
);
560 ret
= ASN1_UTCTIME_print(out
, str
);
563 case V_ASN1_GENERALIZEDTIME
:
564 ret
= ASN1_GENERALIZEDTIME_print(out
, str
);
568 ret
= asn1_print_oid_ctx(out
, (const ASN1_OBJECT
*)*fld
, pctx
);
571 case V_ASN1_OCTET_STRING
:
572 case V_ASN1_BIT_STRING
:
573 ret
= asn1_print_obstring_ctx(out
, str
, indent
, pctx
);
577 case V_ASN1_SEQUENCE
:
580 if (BIO_puts(out
, "\n") <= 0)
582 if (ASN1_parse_dump(out
, str
->data
, str
->length
,
589 ret
= ASN1_STRING_print_ex(out
, str
, pctx
->str_flags
);
593 if (needlf
&& BIO_puts(out
, "\n") <= 0)