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;
319 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
320 sname = ASN1_ITEM_ptr(tt->item)->sname;
323 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
326 fname = tt->field_name;
329 * If field is embedded then fld needs fixing so it is a pointer to
330 * a pointer to a field.
332 if (flags & ASN1_TFLG_EMBED) {
333 tfld = (ASN1_VALUE *)fld;
337 if (flags & ASN1_TFLG_SK_MASK) {
340 STACK_OF(ASN1_VALUE) *stack;
342 /* SET OF, SEQUENCE OF */
344 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
345 if (flags & ASN1_TFLG_SET_OF)
349 if (BIO_printf(out, "%*s%s OF %s {\n",
350 indent, "", tname, tt->field_name) <= 0)
352 } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
355 stack = (STACK_OF(ASN1_VALUE) *)*fld;
356 for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
357 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
360 skitem = sk_ASN1_VALUE_value(stack, i);
361 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
362 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
366 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
368 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
369 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
374 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
375 fname, sname, 0, pctx);
378 static int asn1_print_fsname(BIO *out, int indent,
379 const char *fname, const char *sname,
380 const ASN1_PCTX *pctx)
382 static const char spaces[] = " ";
383 static const int nspaces = sizeof(spaces) - 1;
385 while (indent > nspaces) {
386 if (BIO_write(out, spaces, nspaces) != nspaces)
390 if (BIO_write(out, spaces, indent) != indent)
392 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
394 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
396 if (!sname && !fname)
399 if (BIO_puts(out, fname) <= 0)
404 if (BIO_printf(out, " (%s)", sname) <= 0)
407 if (BIO_puts(out, sname) <= 0)
411 if (BIO_write(out, ": ", 2) != 2)
416 static int asn1_print_boolean(BIO *out, int boolval)
434 if (BIO_puts(out, str) <= 0)
440 static int asn1_print_integer(BIO *out, ASN1_INTEGER *str)
444 s = i2s_ASN1_INTEGER(NULL, str);
445 if (BIO_puts(out, s) <= 0)
451 static int asn1_print_oid(BIO *out, const ASN1_OBJECT *oid)
455 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
458 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
459 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
464 static int asn1_print_obstring(BIO *out, ASN1_STRING *str, int indent)
466 if (str->type == V_ASN1_BIT_STRING) {
467 if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
469 } else if (BIO_puts(out, "\n") <= 0)
471 if ((str->length > 0)
472 && BIO_dump_indent(out, (char *)str->data, str->length,
478 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
479 const ASN1_ITEM *it, int indent,
480 const char *fname, const char *sname,
481 const ASN1_PCTX *pctx)
485 int ret = 1, needlf = 1;
487 const ASN1_PRIMITIVE_FUNCS *pf;
489 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
491 if (pf && pf->prim_print)
492 return pf->prim_print(out, fld, it, indent, pctx);
493 str = (ASN1_STRING *)*fld;
494 if (it->itype == ASN1_ITYPE_MSTRING)
495 utype = str->type & ~V_ASN1_NEG;
498 if (utype == V_ASN1_ANY) {
499 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
501 fld = &atype->value.asn1_value;
502 str = (ASN1_STRING *)*fld;
503 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
506 pname = ASN1_tag2str(utype);
508 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
509 pname = ASN1_tag2str(utype);
514 if (utype == V_ASN1_NULL) {
515 if (BIO_puts(out, "NULL\n") <= 0)
521 if (BIO_puts(out, pname) <= 0)
523 if (BIO_puts(out, ":") <= 0)
530 int boolval = *(int *)fld;
533 ret = asn1_print_boolean(out, boolval);
538 case V_ASN1_ENUMERATED:
539 ret = asn1_print_integer(out, str);
543 ret = ASN1_UTCTIME_print(out, str);
546 case V_ASN1_GENERALIZEDTIME:
547 ret = ASN1_GENERALIZEDTIME_print(out, str);
551 ret = asn1_print_oid(out, (const ASN1_OBJECT *)*fld);
554 case V_ASN1_OCTET_STRING:
555 case V_ASN1_BIT_STRING:
556 ret = asn1_print_obstring(out, str, indent);
560 case V_ASN1_SEQUENCE:
563 if (BIO_puts(out, "\n") <= 0)
565 if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
571 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
576 if (needlf && BIO_puts(out, "\n") <= 0)