2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
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).
60 #include "internal/cryptlib.h"
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 "internal/asn1_int.h"
68 #include "asn1_locl.h"
74 /* ASN1_PCTX routines */
76 static ASN1_PCTX default_pctx
= {
77 ASN1_PCTX_FLAGS_SHOW_ABSENT
, /* flags */
84 ASN1_PCTX
*ASN1_PCTX_new(void)
88 ret
= OPENSSL_zalloc(sizeof(*ret
));
90 ASN1err(ASN1_F_ASN1_PCTX_NEW
, ERR_R_MALLOC_FAILURE
);
96 void ASN1_PCTX_free(ASN1_PCTX
*p
)
101 unsigned long ASN1_PCTX_get_flags(ASN1_PCTX
*p
)
106 void ASN1_PCTX_set_flags(ASN1_PCTX
*p
, unsigned long flags
)
111 unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX
*p
)
116 void ASN1_PCTX_set_nm_flags(ASN1_PCTX
*p
, unsigned long flags
)
121 unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX
*p
)
123 return p
->cert_flags
;
126 void ASN1_PCTX_set_cert_flags(ASN1_PCTX
*p
, unsigned long flags
)
128 p
->cert_flags
= flags
;
131 unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX
*p
)
136 void ASN1_PCTX_set_oid_flags(ASN1_PCTX
*p
, unsigned long flags
)
138 p
->oid_flags
= flags
;
141 unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX
*p
)
146 void ASN1_PCTX_set_str_flags(ASN1_PCTX
*p
, unsigned long flags
)
148 p
->str_flags
= flags
;
151 /* Main print routines */
153 static int asn1_item_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
155 const char *fname
, const char *sname
,
156 int nohdr
, const ASN1_PCTX
*pctx
);
158 int asn1_template_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
159 const ASN1_TEMPLATE
*tt
, const ASN1_PCTX
*pctx
);
161 static int asn1_primitive_print(BIO
*out
, ASN1_VALUE
**fld
,
162 const ASN1_ITEM
*it
, int indent
,
163 const char *fname
, const char *sname
,
164 const ASN1_PCTX
*pctx
);
166 static int asn1_print_fsname(BIO
*out
, int indent
,
167 const char *fname
, const char *sname
,
168 const ASN1_PCTX
*pctx
);
170 int ASN1_item_print(BIO
*out
, ASN1_VALUE
*ifld
, int indent
,
171 const ASN1_ITEM
*it
, const ASN1_PCTX
*pctx
)
175 pctx
= &default_pctx
;
176 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_STRUCT_NAME
)
180 return asn1_item_print_ctx(out
, &ifld
, indent
, it
, NULL
, sname
, 0, pctx
);
183 static int asn1_item_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
185 const char *fname
, const char *sname
,
186 int nohdr
, const ASN1_PCTX
*pctx
)
188 const ASN1_TEMPLATE
*tt
;
189 const ASN1_EXTERN_FUNCS
*ef
;
191 const ASN1_AUX
*aux
= it
->funcs
;
192 ASN1_aux_cb
*asn1_cb
;
195 if (aux
&& aux
->asn1_cb
) {
197 parg
.indent
= indent
;
199 asn1_cb
= aux
->asn1_cb
;
204 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_ABSENT
) {
205 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
207 if (BIO_puts(out
, "<ABSENT>\n") <= 0)
214 case ASN1_ITYPE_PRIMITIVE
:
216 if (!asn1_template_print_ctx(out
, fld
, indent
,
217 it
->templates
, pctx
))
222 case ASN1_ITYPE_MSTRING
:
223 if (!asn1_primitive_print(out
, fld
, it
, indent
, fname
, sname
, pctx
))
227 case ASN1_ITYPE_EXTERN
:
228 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
230 /* Use new style print routine if possible */
232 if (ef
&& ef
->asn1_ex_print
) {
233 i
= ef
->asn1_ex_print(out
, fld
, indent
, "", pctx
);
236 if ((i
== 2) && (BIO_puts(out
, "\n") <= 0))
240 BIO_printf(out
, ":EXTERNAL TYPE %s\n", sname
) <= 0)
244 case ASN1_ITYPE_CHOICE
:
245 /* CHOICE type, get selector */
246 i
= asn1_get_choice_selector(fld
, it
);
247 /* This should never happen... */
248 if ((i
< 0) || (i
>= it
->tcount
)) {
249 if (BIO_printf(out
, "ERROR: selector [%d] invalid\n", i
) <= 0)
253 tt
= it
->templates
+ i
;
254 tmpfld
= asn1_get_field_ptr(fld
, tt
);
255 if (!asn1_template_print_ctx(out
, tmpfld
, indent
, tt
, pctx
))
259 case ASN1_ITYPE_SEQUENCE
:
260 case ASN1_ITYPE_NDEF_SEQUENCE
:
261 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
263 if (fname
|| sname
) {
264 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
) {
265 if (BIO_puts(out
, " {\n") <= 0)
268 if (BIO_puts(out
, "\n") <= 0)
274 i
= asn1_cb(ASN1_OP_PRINT_PRE
, fld
, it
, &parg
);
281 /* Print each field entry */
282 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
283 const ASN1_TEMPLATE
*seqtt
;
284 seqtt
= asn1_do_adb(fld
, tt
, 1);
287 tmpfld
= asn1_get_field_ptr(fld
, seqtt
);
288 if (!asn1_template_print_ctx(out
, tmpfld
,
289 indent
+ 2, seqtt
, pctx
))
292 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
) {
293 if (BIO_printf(out
, "%*s}\n", indent
, "") < 0)
298 i
= asn1_cb(ASN1_OP_PRINT_POST
, fld
, it
, &parg
);
305 BIO_printf(out
, "Unprocessed type %d\n", it
->itype
);
312 int asn1_template_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
313 const ASN1_TEMPLATE
*tt
, const ASN1_PCTX
*pctx
)
316 const char *sname
, *fname
;
318 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME
)
319 sname
= ASN1_ITEM_ptr(tt
->item
)->sname
;
322 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_FIELD_NAME
)
325 fname
= tt
->field_name
;
326 if (flags
& ASN1_TFLG_SK_MASK
) {
329 STACK_OF(ASN1_VALUE
) *stack
;
331 /* SET OF, SEQUENCE OF */
333 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SSOF
) {
334 if (flags
& ASN1_TFLG_SET_OF
)
338 if (BIO_printf(out
, "%*s%s OF %s {\n",
339 indent
, "", tname
, tt
->field_name
) <= 0)
341 } else if (BIO_printf(out
, "%*s%s:\n", indent
, "", fname
) <= 0)
344 stack
= (STACK_OF(ASN1_VALUE
) *)*fld
;
345 for (i
= 0; i
< sk_ASN1_VALUE_num(stack
); i
++) {
346 if ((i
> 0) && (BIO_puts(out
, "\n") <= 0))
349 skitem
= sk_ASN1_VALUE_value(stack
, i
);
350 if (!asn1_item_print_ctx(out
, &skitem
, indent
+ 2,
351 ASN1_ITEM_ptr(tt
->item
), NULL
, NULL
, 1,
355 if (!i
&& BIO_printf(out
, "%*s<EMPTY>\n", indent
+ 2, "") <= 0)
357 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
) {
358 if (BIO_printf(out
, "%*s}\n", indent
, "") <= 0)
363 return asn1_item_print_ctx(out
, fld
, indent
, ASN1_ITEM_ptr(tt
->item
),
364 fname
, sname
, 0, pctx
);
367 static int asn1_print_fsname(BIO
*out
, int indent
,
368 const char *fname
, const char *sname
,
369 const ASN1_PCTX
*pctx
)
371 static const char spaces
[] = " ";
372 static const int nspaces
= sizeof(spaces
) - 1;
374 while (indent
> nspaces
) {
375 if (BIO_write(out
, spaces
, nspaces
) != nspaces
)
379 if (BIO_write(out
, spaces
, indent
) != indent
)
381 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_STRUCT_NAME
)
383 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_FIELD_NAME
)
385 if (!sname
&& !fname
)
388 if (BIO_puts(out
, fname
) <= 0)
393 if (BIO_printf(out
, " (%s)", sname
) <= 0)
396 if (BIO_puts(out
, sname
) <= 0)
400 if (BIO_write(out
, ": ", 2) != 2)
405 static int asn1_print_boolean_ctx(BIO
*out
, int boolval
,
406 const ASN1_PCTX
*pctx
)
424 if (BIO_puts(out
, str
) <= 0)
430 static int asn1_print_integer_ctx(BIO
*out
, ASN1_INTEGER
*str
,
431 const ASN1_PCTX
*pctx
)
435 s
= i2s_ASN1_INTEGER(NULL
, str
);
436 if (BIO_puts(out
, s
) <= 0)
442 static int asn1_print_oid_ctx(BIO
*out
, const ASN1_OBJECT
*oid
,
443 const ASN1_PCTX
*pctx
)
447 ln
= OBJ_nid2ln(OBJ_obj2nid(oid
));
450 OBJ_obj2txt(objbuf
, sizeof objbuf
, oid
, 1);
451 if (BIO_printf(out
, "%s (%s)", ln
, objbuf
) <= 0)
456 static int asn1_print_obstring_ctx(BIO
*out
, ASN1_STRING
*str
, int indent
,
457 const ASN1_PCTX
*pctx
)
459 if (str
->type
== V_ASN1_BIT_STRING
) {
460 if (BIO_printf(out
, " (%ld unused bits)\n", str
->flags
& 0x7) <= 0)
462 } else if (BIO_puts(out
, "\n") <= 0)
464 if ((str
->length
> 0)
465 && BIO_dump_indent(out
, (char *)str
->data
, str
->length
,
471 static int asn1_primitive_print(BIO
*out
, ASN1_VALUE
**fld
,
472 const ASN1_ITEM
*it
, int indent
,
473 const char *fname
, const char *sname
,
474 const ASN1_PCTX
*pctx
)
478 int ret
= 1, needlf
= 1;
480 const ASN1_PRIMITIVE_FUNCS
*pf
;
482 if (!asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
484 if (pf
&& pf
->prim_print
)
485 return pf
->prim_print(out
, fld
, it
, indent
, pctx
);
486 str
= (ASN1_STRING
*)*fld
;
487 if (it
->itype
== ASN1_ITYPE_MSTRING
)
488 utype
= str
->type
& ~V_ASN1_NEG
;
491 if (utype
== V_ASN1_ANY
) {
492 ASN1_TYPE
*atype
= (ASN1_TYPE
*)*fld
;
494 fld
= &atype
->value
.asn1_value
;
495 str
= (ASN1_STRING
*)*fld
;
496 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_ANY_TYPE
)
499 pname
= ASN1_tag2str(utype
);
501 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_TYPE
)
502 pname
= ASN1_tag2str(utype
);
507 if (utype
== V_ASN1_NULL
) {
508 if (BIO_puts(out
, "NULL\n") <= 0)
514 if (BIO_puts(out
, pname
) <= 0)
516 if (BIO_puts(out
, ":") <= 0)
523 int boolval
= *(int *)fld
;
526 ret
= asn1_print_boolean_ctx(out
, boolval
, pctx
);
531 case V_ASN1_ENUMERATED
:
532 ret
= asn1_print_integer_ctx(out
, str
, pctx
);
536 ret
= ASN1_UTCTIME_print(out
, str
);
539 case V_ASN1_GENERALIZEDTIME
:
540 ret
= ASN1_GENERALIZEDTIME_print(out
, str
);
544 ret
= asn1_print_oid_ctx(out
, (const ASN1_OBJECT
*)*fld
, pctx
);
547 case V_ASN1_OCTET_STRING
:
548 case V_ASN1_BIT_STRING
:
549 ret
= asn1_print_obstring_ctx(out
, str
, indent
, pctx
);
553 case V_ASN1_SEQUENCE
:
556 if (BIO_puts(out
, "\n") <= 0)
558 if (ASN1_parse_dump(out
, str
->data
, str
->length
, indent
, 0) <= 0)
564 ret
= ASN1_STRING_print_ex(out
, str
, pctx
->str_flags
);
569 if (needlf
&& BIO_puts(out
, "\n") <= 0)