2 * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/objects.h>
15 #include <openssl/buffer.h>
16 #include <openssl/err.h>
17 #include <openssl/x509v3.h>
18 #include "crypto/asn1.h"
19 #include "asn1_local.h"
25 /* ASN1_PCTX routines */
27 static ASN1_PCTX default_pctx = {
28 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
35 ASN1_PCTX *ASN1_PCTX_new(void)
39 ret = OPENSSL_zalloc(sizeof(*ret));
45 void ASN1_PCTX_free(ASN1_PCTX *p)
50 unsigned long ASN1_PCTX_get_flags(const ASN1_PCTX *p)
55 void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
60 unsigned long ASN1_PCTX_get_nm_flags(const ASN1_PCTX *p)
65 void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
70 unsigned long ASN1_PCTX_get_cert_flags(const ASN1_PCTX *p)
75 void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
77 p->cert_flags = flags;
80 unsigned long ASN1_PCTX_get_oid_flags(const ASN1_PCTX *p)
85 void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
90 unsigned long ASN1_PCTX_get_str_flags(const ASN1_PCTX *p)
95 void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
100 /* Main print routines */
102 static int asn1_item_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
104 const char *fname, const char *sname,
105 int nohdr, const ASN1_PCTX *pctx);
107 static int asn1_template_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
108 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
110 static int asn1_primitive_print(BIO *out, const ASN1_VALUE **fld,
111 const ASN1_ITEM *it, int indent,
112 const char *fname, const char *sname,
113 const ASN1_PCTX *pctx);
115 static int asn1_print_fsname(BIO *out, int indent,
116 const char *fname, const char *sname,
117 const ASN1_PCTX *pctx);
119 int ASN1_item_print(BIO *out, const ASN1_VALUE *ifld, int indent,
120 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
124 pctx = &default_pctx;
125 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
129 return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname, 0, pctx);
132 static int asn1_item_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
134 const char *fname, const char *sname,
135 int nohdr, const ASN1_PCTX *pctx)
137 const ASN1_TEMPLATE *tt;
138 const ASN1_EXTERN_FUNCS *ef;
139 const ASN1_VALUE **tmpfld;
140 const ASN1_AUX *aux = it->funcs;
141 ASN1_aux_const_cb *asn1_cb = NULL;
146 parg.indent = indent;
148 asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb
149 : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */
152 if (((it->itype != ASN1_ITYPE_PRIMITIVE)
153 || (it->utype != V_ASN1_BOOLEAN)) && *fld == NULL) {
154 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
155 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
157 if (BIO_puts(out, "<ABSENT>\n") <= 0)
164 case ASN1_ITYPE_PRIMITIVE:
166 if (!asn1_template_print_ctx(out, fld, indent,
167 it->templates, pctx))
172 case ASN1_ITYPE_MSTRING:
173 if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
177 case ASN1_ITYPE_EXTERN:
178 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
180 /* Use new style print routine if possible */
182 if (ef && ef->asn1_ex_print) {
183 i = ef->asn1_ex_print(out, fld, indent, "", pctx);
186 if ((i == 2) && (BIO_puts(out, "\n") <= 0))
190 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
194 case ASN1_ITYPE_CHOICE:
195 /* CHOICE type, get selector */
196 i = ossl_asn1_get_choice_selector_const(fld, it);
197 /* This should never happen... */
198 if ((i < 0) || (i >= it->tcount)) {
199 if (BIO_printf(out, "ERROR: selector [%d] invalid\n", i) <= 0)
203 tt = it->templates + i;
204 tmpfld = ossl_asn1_get_const_field_ptr(fld, tt);
205 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
209 case ASN1_ITYPE_SEQUENCE:
210 case ASN1_ITYPE_NDEF_SEQUENCE:
211 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
213 if (fname || sname) {
214 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
215 if (BIO_puts(out, " {\n") <= 0)
218 if (BIO_puts(out, "\n") <= 0)
224 i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
231 /* Print each field entry */
232 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
233 const ASN1_TEMPLATE *seqtt;
234 seqtt = ossl_asn1_do_adb(*fld, tt, 1);
237 tmpfld = ossl_asn1_get_const_field_ptr(fld, seqtt);
238 if (!asn1_template_print_ctx(out, tmpfld,
239 indent + 2, seqtt, pctx))
242 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
243 if (BIO_printf(out, "%*s}\n", indent, "") < 0)
248 i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
255 BIO_printf(out, "Unprocessed type %d\n", it->itype);
262 static int asn1_template_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
263 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
266 const char *sname, *fname;
267 const ASN1_VALUE *tfld;
269 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
270 sname = ASN1_ITEM_ptr(tt->item)->sname;
273 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
276 fname = tt->field_name;
279 * If field is embedded then fld needs fixing so it is a pointer to
280 * a pointer to a field.
282 if (flags & ASN1_TFLG_EMBED) {
283 tfld = (const ASN1_VALUE *)fld;
287 if (flags & ASN1_TFLG_SK_MASK) {
289 const ASN1_VALUE *skitem;
290 STACK_OF(const_ASN1_VALUE) *stack;
292 /* SET OF, SEQUENCE OF */
294 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
295 if (flags & ASN1_TFLG_SET_OF)
299 if (BIO_printf(out, "%*s%s OF %s {\n",
300 indent, "", tname, tt->field_name) <= 0)
302 } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
305 stack = (STACK_OF(const_ASN1_VALUE) *)*fld;
306 for (i = 0; i < sk_const_ASN1_VALUE_num(stack); i++) {
307 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
310 skitem = sk_const_ASN1_VALUE_value(stack, i);
311 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
312 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
316 if (i == 0 && BIO_printf(out, "%*s<%s>\n", indent + 2, "",
317 stack == NULL ? "ABSENT" : "EMPTY") <= 0)
319 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
320 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
325 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
326 fname, sname, 0, pctx);
329 static int asn1_print_fsname(BIO *out, int indent,
330 const char *fname, const char *sname,
331 const ASN1_PCTX *pctx)
333 static const char spaces[] = " ";
334 static const int nspaces = sizeof(spaces) - 1;
336 while (indent > nspaces) {
337 if (BIO_write(out, spaces, nspaces) != nspaces)
341 if (BIO_write(out, spaces, indent) != indent)
343 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
345 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
347 if (!sname && !fname)
350 if (BIO_puts(out, fname) <= 0)
355 if (BIO_printf(out, " (%s)", sname) <= 0)
358 if (BIO_puts(out, sname) <= 0)
362 if (BIO_write(out, ": ", 2) != 2)
367 static int asn1_print_boolean(BIO *out, int boolval)
385 if (BIO_puts(out, str) <= 0)
391 static int asn1_print_integer(BIO *out, const ASN1_INTEGER *str)
395 s = i2s_ASN1_INTEGER(NULL, str);
398 if (BIO_puts(out, s) <= 0)
404 static int asn1_print_oid(BIO *out, const ASN1_OBJECT *oid)
408 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
411 OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1);
412 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
417 static int asn1_print_obstring(BIO *out, const ASN1_STRING *str, int indent)
419 if (str->type == V_ASN1_BIT_STRING) {
420 if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
422 } else if (BIO_puts(out, "\n") <= 0)
424 if ((str->length > 0)
425 && BIO_dump_indent(out, (const char *)str->data, str->length,
431 static int asn1_primitive_print(BIO *out, const ASN1_VALUE **fld,
432 const ASN1_ITEM *it, int indent,
433 const char *fname, const char *sname,
434 const ASN1_PCTX *pctx)
438 int ret = 1, needlf = 1;
440 const ASN1_PRIMITIVE_FUNCS *pf;
442 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
444 if (pf && pf->prim_print)
445 return pf->prim_print(out, fld, it, indent, pctx);
446 if (it->itype == ASN1_ITYPE_MSTRING) {
447 str = (ASN1_STRING *)*fld;
448 utype = str->type & ~V_ASN1_NEG;
451 if (utype == V_ASN1_BOOLEAN)
454 str = (ASN1_STRING *)*fld;
456 if (utype == V_ASN1_ANY) {
457 const ASN1_TYPE *atype = (const ASN1_TYPE *)*fld;
459 fld = (const ASN1_VALUE **)&atype->value.asn1_value; /* actually is const */
460 str = (ASN1_STRING *)*fld;
461 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
464 pname = ASN1_tag2str(utype);
466 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
467 pname = ASN1_tag2str(utype);
472 if (utype == V_ASN1_NULL) {
473 if (BIO_puts(out, "NULL\n") <= 0)
479 if (BIO_puts(out, pname) <= 0)
481 if (BIO_puts(out, ":") <= 0)
488 int boolval = *(int *)fld;
491 ret = asn1_print_boolean(out, boolval);
496 case V_ASN1_ENUMERATED:
497 ret = asn1_print_integer(out, str);
501 ret = ASN1_UTCTIME_print(out, str);
504 case V_ASN1_GENERALIZEDTIME:
505 ret = ASN1_GENERALIZEDTIME_print(out, str);
509 ret = asn1_print_oid(out, (const ASN1_OBJECT *)*fld);
512 case V_ASN1_OCTET_STRING:
513 case V_ASN1_BIT_STRING:
514 ret = asn1_print_obstring(out, str, indent);
518 case V_ASN1_SEQUENCE:
521 if (BIO_puts(out, "\n") <= 0)
523 if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
529 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
534 if (needlf && BIO_puts(out, "\n") <= 0)