3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000,2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * 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"
74 /* ASN1_PCTX routines */
76 ASN1_PCTX default_pctx
= {
77 ASN1_PCTX_FLAGS_SHOW_ABSENT
, /* flags */
84 ASN1_PCTX
*ASN1_PCTX_new(void)
87 ret
= OPENSSL_malloc(sizeof(ASN1_PCTX
));
89 ASN1err(ASN1_F_ASN1_PCTX_NEW
, ERR_R_MALLOC_FAILURE
);
100 void ASN1_PCTX_free(ASN1_PCTX
*p
)
105 unsigned long ASN1_PCTX_get_flags(ASN1_PCTX
*p
)
110 void ASN1_PCTX_set_flags(ASN1_PCTX
*p
, unsigned long flags
)
115 unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX
*p
)
120 void ASN1_PCTX_set_nm_flags(ASN1_PCTX
*p
, unsigned long flags
)
125 unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX
*p
)
127 return p
->cert_flags
;
130 void ASN1_PCTX_set_cert_flags(ASN1_PCTX
*p
, unsigned long flags
)
132 p
->cert_flags
= flags
;
135 unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX
*p
)
140 void ASN1_PCTX_set_oid_flags(ASN1_PCTX
*p
, unsigned long flags
)
142 p
->oid_flags
= flags
;
145 unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX
*p
)
150 void ASN1_PCTX_set_str_flags(ASN1_PCTX
*p
, unsigned long flags
)
152 p
->str_flags
= flags
;
155 /* Main print routines */
157 static int asn1_item_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
159 const char *fname
, const char *sname
,
160 int nohdr
, const ASN1_PCTX
*pctx
);
162 int asn1_template_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
163 const ASN1_TEMPLATE
*tt
, const ASN1_PCTX
*pctx
);
165 static int asn1_primitive_print(BIO
*out
, ASN1_VALUE
**fld
,
166 const ASN1_ITEM
*it
, int indent
,
167 const char *fname
, const char *sname
,
168 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
, NULL
, sname
, 0, pctx
);
187 static int asn1_item_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
189 const char *fname
, const char *sname
,
190 int nohdr
, const ASN1_PCTX
*pctx
)
192 const ASN1_TEMPLATE
*tt
;
193 const ASN1_EXTERN_FUNCS
*ef
;
195 const ASN1_AUX
*aux
= it
->funcs
;
196 ASN1_aux_cb
*asn1_cb
;
199 if (aux
&& aux
->asn1_cb
) {
201 parg
.indent
= indent
;
203 asn1_cb
= aux
->asn1_cb
;
207 if (((it
->itype
!= ASN1_ITYPE_PRIMITIVE
)
208 || (it
->utype
!= V_ASN1_BOOLEAN
)) && *fld
== NULL
) {
209 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_ABSENT
) {
210 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
212 if (BIO_puts(out
, "<ABSENT>\n") <= 0)
219 case ASN1_ITYPE_PRIMITIVE
:
221 if (!asn1_template_print_ctx(out
, fld
, indent
,
222 it
->templates
, pctx
))
227 case ASN1_ITYPE_MSTRING
:
228 if (!asn1_primitive_print(out
, fld
, it
, indent
, fname
, sname
, pctx
))
232 case ASN1_ITYPE_EXTERN
:
233 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
235 /* Use new style print routine if possible */
237 if (ef
&& ef
->asn1_ex_print
) {
238 i
= ef
->asn1_ex_print(out
, fld
, indent
, "", pctx
);
241 if ((i
== 2) && (BIO_puts(out
, "\n") <= 0))
245 BIO_printf(out
, ":EXTERNAL TYPE %s\n", sname
) <= 0)
249 case ASN1_ITYPE_CHOICE
:
251 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
254 /* CHOICE type, get selector */
255 i
= asn1_get_choice_selector(fld
, it
);
256 /* This should never happen... */
257 if ((i
< 0) || (i
>= it
->tcount
)) {
258 if (BIO_printf(out
, "ERROR: selector [%d] invalid\n", i
) <= 0)
262 tt
= it
->templates
+ i
;
263 tmpfld
= asn1_get_field_ptr(fld
, tt
);
264 if (!asn1_template_print_ctx(out
, tmpfld
, indent
, tt
, pctx
))
268 case ASN1_ITYPE_SEQUENCE
:
269 case ASN1_ITYPE_NDEF_SEQUENCE
:
270 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
272 if (fname
|| sname
) {
273 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
) {
274 if (BIO_puts(out
, " {\n") <= 0)
277 if (BIO_puts(out
, "\n") <= 0)
283 i
= asn1_cb(ASN1_OP_PRINT_PRE
, fld
, it
, &parg
);
290 /* Print each field entry */
291 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
292 const ASN1_TEMPLATE
*seqtt
;
293 seqtt
= asn1_do_adb(fld
, tt
, 1);
296 tmpfld
= asn1_get_field_ptr(fld
, seqtt
);
297 if (!asn1_template_print_ctx(out
, tmpfld
,
298 indent
+ 2, seqtt
, pctx
))
301 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
) {
302 if (BIO_printf(out
, "%*s}\n", indent
, "") < 0)
307 i
= asn1_cb(ASN1_OP_PRINT_POST
, fld
, it
, &parg
);
314 BIO_printf(out
, "Unprocessed type %d\n", it
->itype
);
321 int asn1_template_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
322 const ASN1_TEMPLATE
*tt
, const ASN1_PCTX
*pctx
)
325 const char *sname
, *fname
;
327 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME
)
328 sname
= ASN1_ITEM_ptr(tt
->item
)->sname
;
331 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_FIELD_NAME
)
334 fname
= tt
->field_name
;
335 if (flags
& ASN1_TFLG_SK_MASK
) {
338 STACK_OF(ASN1_VALUE
) *stack
;
340 /* SET OF, SEQUENCE OF */
342 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SSOF
) {
343 if (flags
& ASN1_TFLG_SET_OF
)
347 if (BIO_printf(out
, "%*s%s OF %s {\n",
348 indent
, "", tname
, tt
->field_name
) <= 0)
350 } else if (BIO_printf(out
, "%*s%s:\n", indent
, "", fname
) <= 0)
353 stack
= (STACK_OF(ASN1_VALUE
) *)*fld
;
354 for (i
= 0; i
< sk_ASN1_VALUE_num(stack
); i
++) {
355 if ((i
> 0) && (BIO_puts(out
, "\n") <= 0))
358 skitem
= sk_ASN1_VALUE_value(stack
, i
);
359 if (!asn1_item_print_ctx(out
, &skitem
, indent
+ 2,
360 ASN1_ITEM_ptr(tt
->item
), NULL
, NULL
, 1,
364 if (!i
&& BIO_printf(out
, "%*s<EMPTY>\n", indent
+ 2, "") <= 0)
366 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
) {
367 if (BIO_printf(out
, "%*s}\n", indent
, "") <= 0)
372 return asn1_item_print_ctx(out
, fld
, indent
, ASN1_ITEM_ptr(tt
->item
),
373 fname
, sname
, 0, pctx
);
376 static int asn1_print_fsname(BIO
*out
, int indent
,
377 const char *fname
, const char *sname
,
378 const ASN1_PCTX
*pctx
)
380 static char spaces
[] = " ";
381 const int nspaces
= sizeof(spaces
) - 1;
384 if (!sname
&& !fname
)
388 while (indent
> nspaces
) {
389 if (BIO_write(out
, spaces
, nspaces
) != nspaces
)
393 if (BIO_write(out
, spaces
, indent
) != indent
)
395 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_STRUCT_NAME
)
397 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_FIELD_NAME
)
399 if (!sname
&& !fname
)
402 if (BIO_puts(out
, fname
) <= 0)
407 if (BIO_printf(out
, " (%s)", sname
) <= 0)
410 if (BIO_puts(out
, sname
) <= 0)
414 if (BIO_write(out
, ": ", 2) != 2)
419 static int asn1_print_boolean_ctx(BIO
*out
, int boolval
,
420 const ASN1_PCTX
*pctx
)
438 if (BIO_puts(out
, str
) <= 0)
444 static int asn1_print_integer_ctx(BIO
*out
, ASN1_INTEGER
*str
,
445 const ASN1_PCTX
*pctx
)
449 s
= i2s_ASN1_INTEGER(NULL
, str
);
452 if (BIO_puts(out
, s
) <= 0)
458 static int asn1_print_oid_ctx(BIO
*out
, const ASN1_OBJECT
*oid
,
459 const ASN1_PCTX
*pctx
)
463 ln
= OBJ_nid2ln(OBJ_obj2nid(oid
));
466 OBJ_obj2txt(objbuf
, sizeof objbuf
, oid
, 1);
467 if (BIO_printf(out
, "%s (%s)", ln
, objbuf
) <= 0)
472 static int asn1_print_obstring_ctx(BIO
*out
, ASN1_STRING
*str
, int indent
,
473 const ASN1_PCTX
*pctx
)
475 if (str
->type
== V_ASN1_BIT_STRING
) {
476 if (BIO_printf(out
, " (%ld unused bits)\n", str
->flags
& 0x7) <= 0)
478 } else if (BIO_puts(out
, "\n") <= 0)
480 if ((str
->length
> 0)
481 && BIO_dump_indent(out
, (char *)str
->data
, str
->length
,
487 static int asn1_primitive_print(BIO
*out
, ASN1_VALUE
**fld
,
488 const ASN1_ITEM
*it
, int indent
,
489 const char *fname
, const char *sname
,
490 const ASN1_PCTX
*pctx
)
494 int ret
= 1, needlf
= 1;
496 const ASN1_PRIMITIVE_FUNCS
*pf
;
498 if (!asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
500 if (pf
&& pf
->prim_print
)
501 return pf
->prim_print(out
, fld
, it
, indent
, pctx
);
502 if (it
->itype
== ASN1_ITYPE_MSTRING
) {
503 str
= (ASN1_STRING
*)*fld
;
504 utype
= str
->type
& ~V_ASN1_NEG
;
507 if (utype
== V_ASN1_BOOLEAN
)
510 str
= (ASN1_STRING
*)*fld
;
512 if (utype
== V_ASN1_ANY
) {
513 ASN1_TYPE
*atype
= (ASN1_TYPE
*)*fld
;
515 fld
= &atype
->value
.asn1_value
;
516 str
= (ASN1_STRING
*)*fld
;
517 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_ANY_TYPE
)
520 pname
= ASN1_tag2str(utype
);
522 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_TYPE
)
523 pname
= ASN1_tag2str(utype
);
528 if (utype
== V_ASN1_NULL
) {
529 if (BIO_puts(out
, "NULL\n") <= 0)
535 if (BIO_puts(out
, pname
) <= 0)
537 if (BIO_puts(out
, ":") <= 0)
544 int boolval
= *(int *)fld
;
547 ret
= asn1_print_boolean_ctx(out
, boolval
, pctx
);
552 case V_ASN1_ENUMERATED
:
553 ret
= asn1_print_integer_ctx(out
, str
, pctx
);
557 ret
= ASN1_UTCTIME_print(out
, str
);
560 case V_ASN1_GENERALIZEDTIME
:
561 ret
= ASN1_GENERALIZEDTIME_print(out
, str
);
565 ret
= asn1_print_oid_ctx(out
, (const ASN1_OBJECT
*)*fld
, pctx
);
568 case V_ASN1_OCTET_STRING
:
569 case V_ASN1_BIT_STRING
:
570 ret
= asn1_print_obstring_ctx(out
, str
, indent
, pctx
);
574 case V_ASN1_SEQUENCE
:
577 if (BIO_puts(out
, "\n") <= 0)
579 if (ASN1_parse_dump(out
, str
->data
, str
->length
, indent
, 0) <= 0)
585 ret
= ASN1_STRING_print_ex(out
, str
, pctx
->str_flags
);
590 if (needlf
&& BIO_puts(out
, "\n") <= 0)