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).
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 "asn1_locl.h"
74 /* ASN1_PCTX routines */
76 ASN1_PCTX default_pctx
=
78 ASN1_PCTX_FLAGS_SHOW_ABSENT
, /* flags */
86 ASN1_PCTX
*ASN1_PCTX_new(void)
89 ret
= OPENSSL_malloc(sizeof(ASN1_PCTX
));
92 ASN1err(ASN1_F_ASN1_PCTX_NEW
, ERR_R_MALLOC_FAILURE
);
103 void ASN1_PCTX_free(ASN1_PCTX
*p
)
108 unsigned long ASN1_PCTX_get_flags(ASN1_PCTX
*p
)
113 void ASN1_PCTX_set_flags(ASN1_PCTX
*p
, unsigned long flags
)
118 unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX
*p
)
123 void ASN1_PCTX_set_nm_flags(ASN1_PCTX
*p
, unsigned long flags
)
128 unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX
*p
)
130 return p
->cert_flags
;
133 void ASN1_PCTX_set_cert_flags(ASN1_PCTX
*p
, unsigned long flags
)
135 p
->cert_flags
= flags
;
138 unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX
*p
)
143 void ASN1_PCTX_set_oid_flags(ASN1_PCTX
*p
, unsigned long flags
)
145 p
->oid_flags
= flags
;
148 unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX
*p
)
153 void ASN1_PCTX_set_str_flags(ASN1_PCTX
*p
, unsigned long flags
)
155 p
->str_flags
= flags
;
158 /* Main print routines */
160 static int asn1_item_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
162 const char *fname
, const char *sname
,
163 int nohdr
, const ASN1_PCTX
*pctx
);
165 int asn1_template_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
166 const ASN1_TEMPLATE
*tt
, const ASN1_PCTX
*pctx
);
168 static int asn1_primitive_print(BIO
*out
, ASN1_VALUE
**fld
,
169 const ASN1_ITEM
*it
, int indent
,
170 const char *fname
, const char *sname
,
171 const ASN1_PCTX
*pctx
);
173 static int asn1_print_fsname(BIO
*out
, int indent
,
174 const char *fname
, const char *sname
,
175 const ASN1_PCTX
*pctx
);
177 int ASN1_item_print(BIO
*out
, ASN1_VALUE
*ifld
, int indent
,
178 const ASN1_ITEM
*it
, const ASN1_PCTX
*pctx
)
182 pctx
= &default_pctx
;
183 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_STRUCT_NAME
)
187 return asn1_item_print_ctx(out
, &ifld
, indent
, it
,
188 NULL
, sname
, 0, pctx
);
191 static int asn1_item_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
193 const char *fname
, const char *sname
,
194 int nohdr
, const ASN1_PCTX
*pctx
)
196 const ASN1_TEMPLATE
*tt
;
197 const ASN1_EXTERN_FUNCS
*ef
;
202 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_ABSENT
)
204 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
206 if (BIO_puts(out
, "<ABSENT>\n") <= 0)
214 case ASN1_ITYPE_PRIMITIVE
:
217 if (!asn1_template_print_ctx(out
, fld
, indent
,
218 it
->templates
, pctx
))
222 case ASN1_ITYPE_MSTRING
:
223 if (!asn1_primitive_print(out
, fld
, it
,
224 indent
, fname
, sname
,pctx
))
228 case ASN1_ITYPE_EXTERN
:
229 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
231 /* Use new style print routine if possible */
233 if (ef
&& ef
->asn1_ex_print
)
235 i
= ef
->asn1_ex_print(out
, fld
, indent
, "", pctx
);
238 if ((i
== 2) && (BIO_puts(out
, "\n") <= 0))
243 BIO_printf(out
, ":EXTERNAL TYPE %s\n", sname
) <= 0)
247 case ASN1_ITYPE_CHOICE
:
249 if (!nohdr
&& !asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
252 /* CHOICE type, get selector */
253 i
= asn1_get_choice_selector(fld
, it
);
254 /* This should never happen... */
255 if((i
< 0) || (i
>= it
->tcount
))
258 "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
))
274 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
)
276 if (BIO_puts(out
, " {\n") <= 0)
281 if (BIO_puts(out
, "\n") <= 0)
286 /* Print each field entry */
287 for(i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++)
289 const ASN1_TEMPLATE
*seqtt
;
290 seqtt
= asn1_do_adb(fld
, tt
, 1);
291 tmpfld
= asn1_get_field_ptr(fld
, seqtt
);
292 if (!asn1_template_print_ctx(out
, tmpfld
,
293 indent
+ 2, seqtt
, pctx
))
296 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
)
298 if (BIO_printf(out
, "%*s}\n", indent
, "") < 0)
304 BIO_printf(out
, "Unprocessed type %d\n", it
->itype
);
311 int asn1_template_print_ctx(BIO
*out
, ASN1_VALUE
**fld
, int indent
,
312 const ASN1_TEMPLATE
*tt
, const ASN1_PCTX
*pctx
)
315 const char *sname
, *fname
;
317 if(pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME
)
318 sname
= tt
->item
->sname
;
321 if(pctx
->flags
& ASN1_PCTX_FLAGS_NO_FIELD_NAME
)
324 fname
= tt
->field_name
;
325 if(flags
& ASN1_TFLG_SK_MASK
)
329 /* SET OF, SEQUENCE OF */
332 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)
342 else if (BIO_printf(out
, "%*s%s:\n", indent
, "",
346 for(i
= 0; i
< sk_num((STACK
*)*fld
); i
++)
348 if ((i
> 0) && (BIO_puts(out
, "\n") <= 0))
351 skitem
= (ASN1_VALUE
*)sk_value((STACK
*)*fld
, i
);
352 if (!asn1_item_print_ctx(out
, &skitem
, indent
+ 2,
353 tt
->item
, NULL
, NULL
, 1, pctx
))
356 if (!i
&& BIO_printf(out
, "%*s<EMPTY>\n", indent
+ 2, "") <= 0)
358 if(pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_SEQUENCE
)
360 if (BIO_printf(out
, "%*s}\n", indent
, "") <= 0)
365 return asn1_item_print_ctx(out
, fld
, indent
, tt
->item
,
366 fname
, sname
, 0, pctx
);
369 static int asn1_print_fsname(BIO
*out
, int indent
,
370 const char *fname
, const char *sname
,
371 const ASN1_PCTX
*pctx
)
373 static char spaces
[] = " ";
374 const int nspaces
= sizeof(spaces
) - 1;
377 if (!sname
&& !fname
)
381 while (indent
> nspaces
)
383 if (BIO_write(out
, spaces
, nspaces
) != nspaces
)
387 if (BIO_write(out
, spaces
, indent
) != indent
)
389 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_STRUCT_NAME
)
391 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_FIELD_NAME
)
393 if (!sname
&& !fname
)
397 if (BIO_puts(out
, fname
) <= 0)
404 if (BIO_printf(out
, " (%s)", sname
) <= 0)
409 if (BIO_puts(out
, sname
) <= 0)
413 if (BIO_write(out
, ": ", 2) != 2)
418 static int asn1_print_boolean_ctx(BIO
*out
, const int bool,
419 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
);
450 if (BIO_puts(out
, s
) <= 0)
456 static int asn1_print_oid_ctx(BIO
*out
, const ASN1_OBJECT
*oid
,
457 const ASN1_PCTX
*pctx
)
461 ln
= OBJ_nid2ln(OBJ_obj2nid(oid
));
464 OBJ_obj2txt(objbuf
, sizeof objbuf
, oid
, 1);
465 if (BIO_printf(out
, "%s (%s)", ln
, objbuf
) <= 0)
470 static int asn1_print_obstring_ctx(BIO
*out
, ASN1_STRING
*str
, int indent
,
471 const ASN1_PCTX
*pctx
)
473 if (str
->type
== V_ASN1_BIT_STRING
)
475 if (BIO_printf(out
, " (%ld unused bits)\n",
476 str
->flags
& 0x7) <= 0)
479 else if (BIO_puts(out
, "\n") <= 0)
481 if (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 if (!asn1_print_fsname(out
, indent
, fname
, sname
, pctx
))
498 str
= (ASN1_STRING
*)*fld
;
499 if (it
->itype
== ASN1_ITYPE_MSTRING
)
500 utype
= str
->type
& ~V_ASN1_NEG
;
503 if (utype
== V_ASN1_ANY
)
505 ASN1_TYPE
*atype
= (ASN1_TYPE
*)*fld
;
507 fld
= (ASN1_VALUE
**)&atype
->value
.ptr
;
508 str
= (ASN1_STRING
*)*fld
;
509 if (pctx
->flags
& ASN1_PCTX_FLAGS_NO_ANY_TYPE
)
512 pname
= ASN1_tag2str(utype
);
516 if (pctx
->flags
& ASN1_PCTX_FLAGS_SHOW_TYPE
)
517 pname
= ASN1_tag2str(utype
);
522 if (utype
== V_ASN1_NULL
)
524 if (BIO_puts(out
, "NULL\n") <= 0)
531 if (BIO_puts(out
, pname
) <= 0)
533 if (BIO_puts(out
, ":") <= 0)
541 int bool = *(int *)fld
;
544 ret
= asn1_print_boolean_ctx(out
, bool, pctx
);
549 case V_ASN1_ENUMERATED
:
550 ret
= asn1_print_integer_ctx(out
, str
, pctx
);
554 ret
= ASN1_UTCTIME_print(out
, str
);
557 case V_ASN1_GENERALIZEDTIME
:
558 ret
= ASN1_GENERALIZEDTIME_print(out
, str
);
562 ret
= asn1_print_oid_ctx(out
, (const ASN1_OBJECT
*)*fld
, pctx
);
565 case V_ASN1_OCTET_STRING
:
566 case V_ASN1_BIT_STRING
:
567 ret
= asn1_print_obstring_ctx(out
, str
, indent
, pctx
);
571 case V_ASN1_SEQUENCE
:
574 if (BIO_puts(out
, "\n") <= 0)
576 if (ASN1_parse_dump(out
, str
->data
, str
->length
,
583 ret
= ASN1_STRING_print_ex(out
, str
, pctx
->str_flags
);
588 if (needlf
&& BIO_puts(out
, "\n") <= 0)