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 "internal/asn1_int.h"
69 #include "asn1_locl.h"
75 /* ASN1_PCTX routines */
77 ASN1_PCTX default_pctx = {
78 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
85 ASN1_PCTX *ASN1_PCTX_new(void)
88 ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
90 ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
101 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, NULL, sname, 0, pctx);
189 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
191 const char *fname, const char *sname,
192 int nohdr, const ASN1_PCTX *pctx)
194 const ASN1_TEMPLATE *tt;
195 const ASN1_EXTERN_FUNCS *ef;
197 const ASN1_AUX *aux = it->funcs;
198 ASN1_aux_cb *asn1_cb;
201 if (aux && aux->asn1_cb) {
203 parg.indent = indent;
205 asn1_cb = aux->asn1_cb;
210 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
211 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
213 if (BIO_puts(out, "<ABSENT>\n") <= 0)
220 case ASN1_ITYPE_PRIMITIVE:
222 if (!asn1_template_print_ctx(out, fld, indent,
223 it->templates, pctx))
228 case ASN1_ITYPE_MSTRING:
229 if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
233 case ASN1_ITYPE_EXTERN:
234 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
236 /* Use new style print routine if possible */
238 if (ef && ef->asn1_ex_print) {
239 i = ef->asn1_ex_print(out, fld, indent, "", pctx);
242 if ((i == 2) && (BIO_puts(out, "\n") <= 0))
246 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
250 case ASN1_ITYPE_CHOICE:
251 /* CHOICE type, get selector */
252 i = asn1_get_choice_selector(fld, it);
253 /* This should never happen... */
254 if ((i < 0) || (i >= it->tcount)) {
255 if (BIO_printf(out, "ERROR: selector [%d] invalid\n", i) <= 0)
259 tt = it->templates + i;
260 tmpfld = asn1_get_field_ptr(fld, tt);
261 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
265 case ASN1_ITYPE_SEQUENCE:
266 case ASN1_ITYPE_NDEF_SEQUENCE:
267 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
269 if (fname || sname) {
270 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
271 if (BIO_puts(out, " {\n") <= 0)
274 if (BIO_puts(out, "\n") <= 0)
280 i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
287 /* Print each field entry */
288 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
289 const ASN1_TEMPLATE *seqtt;
290 seqtt = asn1_do_adb(fld, tt, 1);
293 tmpfld = asn1_get_field_ptr(fld, seqtt);
294 if (!asn1_template_print_ctx(out, tmpfld,
295 indent + 2, seqtt, pctx))
298 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
299 if (BIO_printf(out, "%*s}\n", indent, "") < 0)
304 i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
311 BIO_printf(out, "Unprocessed type %d\n", it->itype);
318 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
319 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
322 const char *sname, *fname;
324 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
325 sname = ASN1_ITEM_ptr(tt->item)->sname;
328 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
331 fname = tt->field_name;
332 if (flags & ASN1_TFLG_SK_MASK) {
335 STACK_OF(ASN1_VALUE) *stack;
337 /* SET OF, SEQUENCE OF */
339 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
340 if (flags & ASN1_TFLG_SET_OF)
344 if (BIO_printf(out, "%*s%s OF %s {\n",
345 indent, "", tname, tt->field_name) <= 0)
347 } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
350 stack = (STACK_OF(ASN1_VALUE) *)*fld;
351 for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
352 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
355 skitem = sk_ASN1_VALUE_value(stack, i);
356 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
357 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
361 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
363 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
364 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
369 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
370 fname, sname, 0, pctx);
373 static int asn1_print_fsname(BIO *out, int indent,
374 const char *fname, const char *sname,
375 const ASN1_PCTX *pctx)
377 static const char spaces[] = " ";
378 static const int nspaces = sizeof(spaces) - 1;
380 while (indent > nspaces) {
381 if (BIO_write(out, spaces, nspaces) != nspaces)
385 if (BIO_write(out, spaces, indent) != indent)
387 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
389 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
391 if (!sname && !fname)
394 if (BIO_puts(out, fname) <= 0)
399 if (BIO_printf(out, " (%s)", sname) <= 0)
402 if (BIO_puts(out, sname) <= 0)
406 if (BIO_write(out, ": ", 2) != 2)
411 static int asn1_print_boolean_ctx(BIO *out, int boolval,
412 const ASN1_PCTX *pctx)
430 if (BIO_puts(out, str) <= 0)
436 static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
437 const ASN1_PCTX *pctx)
441 s = i2s_ASN1_INTEGER(NULL, str);
442 if (BIO_puts(out, s) <= 0)
448 static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
449 const ASN1_PCTX *pctx)
453 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
456 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
457 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
462 static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
463 const ASN1_PCTX *pctx)
465 if (str->type == V_ASN1_BIT_STRING) {
466 if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
468 } else if (BIO_puts(out, "\n") <= 0)
470 if ((str->length > 0)
471 && BIO_dump_indent(out, (char *)str->data, str->length,
477 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
478 const ASN1_ITEM *it, int indent,
479 const char *fname, const char *sname,
480 const ASN1_PCTX *pctx)
484 int ret = 1, needlf = 1;
486 const ASN1_PRIMITIVE_FUNCS *pf;
488 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
490 if (pf && pf->prim_print)
491 return pf->prim_print(out, fld, it, indent, pctx);
492 str = (ASN1_STRING *)*fld;
493 if (it->itype == ASN1_ITYPE_MSTRING)
494 utype = str->type & ~V_ASN1_NEG;
497 if (utype == V_ASN1_ANY) {
498 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
500 fld = &atype->value.asn1_value;
501 str = (ASN1_STRING *)*fld;
502 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
505 pname = ASN1_tag2str(utype);
507 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
508 pname = ASN1_tag2str(utype);
513 if (utype == V_ASN1_NULL) {
514 if (BIO_puts(out, "NULL\n") <= 0)
520 if (BIO_puts(out, pname) <= 0)
522 if (BIO_puts(out, ":") <= 0)
529 int boolval = *(int *)fld;
532 ret = asn1_print_boolean_ctx(out, boolval, pctx);
537 case V_ASN1_ENUMERATED:
538 ret = asn1_print_integer_ctx(out, str, pctx);
542 ret = ASN1_UTCTIME_print(out, str);
545 case V_ASN1_GENERALIZEDTIME:
546 ret = ASN1_GENERALIZEDTIME_print(out, str);
550 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
553 case V_ASN1_OCTET_STRING:
554 case V_ASN1_BIT_STRING:
555 ret = asn1_print_obstring_ctx(out, str, indent, pctx);
559 case V_ASN1_SEQUENCE:
562 if (BIO_puts(out, "\n") <= 0)
564 if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
570 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
575 if (needlf && BIO_puts(out, "\n") <= 0)