xref: /openbsd/lib/libcrypto/asn1/tasn_prn.c (revision 2c2de1f8)
1 /* $OpenBSD: tasn_prn.c,v 1.27 2024/03/02 09:04:07 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000,2005 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58 
59 #include <stddef.h>
60 
61 #include <openssl/asn1.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/buffer.h>
64 #include <openssl/err.h>
65 #include <openssl/objects.h>
66 #include <openssl/x509v3.h>
67 
68 #include "asn1_local.h"
69 
70 /* Print routines.
71  */
72 
73 /* ASN1_PCTX routines */
74 
75 static const ASN1_PCTX default_pctx = {
76 	.flags = ASN1_PCTX_FLAGS_SHOW_ABSENT,
77 };
78 
79 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
80     const ASN1_ITEM *it, const char *fname, const char *sname, int nohdr,
81     const ASN1_PCTX *pctx);
82 
83 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
84     const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
85 
86 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
87     const ASN1_ITEM *it, int indent, const char *fname, const char *sname,
88     const ASN1_PCTX *pctx);
89 
90 static int asn1_print_fsname(BIO *out, int indent, const char *fname,
91     const char *sname, const ASN1_PCTX *pctx);
92 
93 int
ASN1_item_print(BIO * out,ASN1_VALUE * ifld,int indent,const ASN1_ITEM * it,const ASN1_PCTX * pctx)94 ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent, const ASN1_ITEM *it,
95     const ASN1_PCTX *pctx)
96 {
97 	const char *sname;
98 
99 	if (pctx == NULL)
100 		pctx = &default_pctx;
101 	if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
102 		sname = NULL;
103 	else
104 		sname = it->sname;
105 	return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname,
106 	    0, pctx);
107 }
108 LCRYPTO_ALIAS(ASN1_item_print);
109 
110 static int
asn1_item_print_ctx(BIO * out,ASN1_VALUE ** fld,int indent,const ASN1_ITEM * it,const char * fname,const char * sname,int nohdr,const ASN1_PCTX * pctx)111 asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, const ASN1_ITEM *it,
112     const char *fname, const char *sname, int nohdr, const ASN1_PCTX *pctx)
113 {
114 	const ASN1_TEMPLATE *tt;
115 	const ASN1_EXTERN_FUNCS *ef;
116 	ASN1_VALUE **tmpfld;
117 	const ASN1_AUX *aux = it->funcs;
118 	ASN1_aux_cb *asn1_cb;
119 	ASN1_PRINT_ARG parg;
120 	int i;
121 
122 	if (aux && aux->asn1_cb) {
123 		parg.out = out;
124 		parg.indent = indent;
125 		parg.pctx = pctx;
126 		asn1_cb = aux->asn1_cb;
127 	} else
128 		asn1_cb = NULL;
129 
130 	if ((it->itype != ASN1_ITYPE_PRIMITIVE ||
131 	    it->utype != V_ASN1_BOOLEAN) && *fld == NULL) {
132 		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
133 			if (!nohdr &&
134 			    !asn1_print_fsname(out, indent, fname, sname, pctx))
135 				return 0;
136 			if (BIO_puts(out, "<ABSENT>\n") <= 0)
137 				return 0;
138 		}
139 		return 1;
140 	}
141 
142 	switch (it->itype) {
143 	case ASN1_ITYPE_PRIMITIVE:
144 		if (it->templates) {
145 			if (!asn1_template_print_ctx(out, fld, indent,
146 			    it->templates, pctx))
147 				return 0;
148 		}
149 		/* fall thru */
150 	case ASN1_ITYPE_MSTRING:
151 		if (!asn1_primitive_print(out, fld, it,
152 		    indent, fname, sname, pctx))
153 			return 0;
154 		break;
155 
156 	case ASN1_ITYPE_EXTERN:
157 		if (!nohdr &&
158 		    !asn1_print_fsname(out, indent, fname, sname, pctx))
159 			return 0;
160 		/* Use new style print routine if possible */
161 		ef = it->funcs;
162 		if (ef && ef->asn1_ex_print) {
163 			i = ef->asn1_ex_print(out, fld, indent, "", pctx);
164 			if (!i)
165 				return 0;
166 			if ((i == 2) && (BIO_puts(out, "\n") <= 0))
167 				return 0;
168 			return 1;
169 		} else if (sname &&
170 		    BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
171 			return 0;
172 		break;
173 
174 	case ASN1_ITYPE_CHOICE:
175 		/* CHOICE type, get selector */
176 		i = asn1_get_choice_selector(fld, it);
177 		/* This should never happen... */
178 		if ((i < 0) || (i >= it->tcount)) {
179 			if (BIO_printf(out,
180 			    "ERROR: selector [%d] invalid\n", i) <= 0)
181 				return 0;
182 			return 1;
183 		}
184 		tt = it->templates + i;
185 		tmpfld = asn1_get_field_ptr(fld, tt);
186 		if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
187 			return 0;
188 		break;
189 
190 	case ASN1_ITYPE_SEQUENCE:
191 	case ASN1_ITYPE_NDEF_SEQUENCE:
192 		if (!nohdr &&
193 		    !asn1_print_fsname(out, indent, fname, sname, pctx))
194 			return 0;
195 		if (fname || sname) {
196 			if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
197 				if (BIO_puts(out, " {\n") <= 0)
198 					return 0;
199 			} else {
200 				if (BIO_puts(out, "\n") <= 0)
201 					return 0;
202 			}
203 		}
204 
205 		if (asn1_cb) {
206 			i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
207 			if (i == 0)
208 				return 0;
209 			if (i == 2)
210 				return 1;
211 		}
212 
213 		/* Print each field entry */
214 		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
215 			const ASN1_TEMPLATE *seqtt;
216 
217 			seqtt = asn1_do_adb(fld, tt, 1);
218 			if (seqtt == NULL)
219 				return 0;
220 			tmpfld = asn1_get_field_ptr(fld, seqtt);
221 			if (!asn1_template_print_ctx(out, tmpfld, indent + 2,
222 			    seqtt, pctx))
223 				return 0;
224 		}
225 		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
226 			if (BIO_printf(out, "%*s}\n", indent, "") < 0)
227 				return 0;
228 		}
229 
230 		if (asn1_cb) {
231 			i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
232 			if (i == 0)
233 				return 0;
234 		}
235 		break;
236 
237 	default:
238 		BIO_printf(out, "Unprocessed type %d\n", it->itype);
239 		return 0;
240 	}
241 
242 	return 1;
243 }
244 
245 int
asn1_template_print_ctx(BIO * out,ASN1_VALUE ** fld,int indent,const ASN1_TEMPLATE * tt,const ASN1_PCTX * pctx)246 asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
247     const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
248 {
249 	int i, flags;
250 	const char *sname, *fname;
251 
252 	flags = tt->flags;
253 	if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
254 		sname = tt->item->sname;
255 	else
256 		sname = NULL;
257 	if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
258 		fname = NULL;
259 	else
260 		fname = tt->field_name;
261 	if (flags & ASN1_TFLG_SK_MASK) {
262 		char *tname;
263 		ASN1_VALUE *skitem;
264 		STACK_OF(ASN1_VALUE) *stack;
265 
266 		/* SET OF, SEQUENCE OF */
267 		if (fname) {
268 			if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
269 				if (flags & ASN1_TFLG_SET_OF)
270 					tname = "SET";
271 				else
272 					tname = "SEQUENCE";
273 				if (BIO_printf(out, "%*s%s OF %s {\n",
274 				    indent, "", tname, tt->field_name) <= 0)
275 					return 0;
276 			} else if (BIO_printf(out, "%*s%s:\n", indent, "",
277 			    fname) <= 0)
278 				return 0;
279 		}
280 		stack = (STACK_OF(ASN1_VALUE) *)*fld;
281 		for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
282 			if ((i > 0) && (BIO_puts(out, "\n") <= 0))
283 				return 0;
284 			skitem = sk_ASN1_VALUE_value(stack, i);
285 			if (!asn1_item_print_ctx(out, &skitem, indent + 2,
286 			    tt->item, NULL, NULL, 1, pctx))
287 				return 0;
288 		}
289 		if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
290 			return 0;
291 		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
292 			if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
293 				return 0;
294 		}
295 		return 1;
296 	}
297 	return asn1_item_print_ctx(out, fld, indent, tt->item,
298 	    fname, sname, 0, pctx);
299 }
300 
301 static int
asn1_print_fsname(BIO * out,int indent,const char * fname,const char * sname,const ASN1_PCTX * pctx)302 asn1_print_fsname(BIO *out, int indent, const char *fname, const char *sname,
303     const ASN1_PCTX *pctx)
304 {
305 	if (indent < 0)
306 		return 0;
307 	if (!BIO_indent(out, indent, indent))
308 		return 0;
309 	if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
310 		sname = NULL;
311 	if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
312 		fname = NULL;
313 	if (!sname && !fname)
314 		return 1;
315 	if (fname) {
316 		if (BIO_puts(out, fname) <= 0)
317 			return 0;
318 	}
319 	if (sname) {
320 		if (fname) {
321 			if (BIO_printf(out, " (%s)", sname) <= 0)
322 				return 0;
323 		} else {
324 			if (BIO_puts(out, sname) <= 0)
325 				return 0;
326 		}
327 	}
328 	if (BIO_write(out, ": ", 2) != 2)
329 		return 0;
330 	return 1;
331 }
332 
333 static int
asn1_print_boolean_ctx(BIO * out,int boolval,const ASN1_PCTX * pctx)334 asn1_print_boolean_ctx(BIO *out, int boolval, const ASN1_PCTX *pctx)
335 {
336 	const char *str;
337 	switch (boolval) {
338 	case -1:
339 		str = "BOOL ABSENT";
340 		break;
341 
342 	case 0:
343 		str = "FALSE";
344 		break;
345 
346 	default:
347 		str = "TRUE";
348 		break;
349 
350 	}
351 
352 	if (BIO_puts(out, str) <= 0)
353 		return 0;
354 	return 1;
355 
356 }
357 
358 static int
asn1_print_integer_ctx(BIO * out,ASN1_INTEGER * str,const ASN1_PCTX * pctx)359 asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str, const ASN1_PCTX *pctx)
360 {
361 	char *s;
362 	int ret = 1;
363 	if ((s = i2s_ASN1_INTEGER(NULL, str)) == NULL)
364 		return 0;
365 	if (BIO_puts(out, s) <= 0)
366 		ret = 0;
367 	free(s);
368 	return ret;
369 }
370 
371 static int
asn1_print_oid_ctx(BIO * out,const ASN1_OBJECT * oid,const ASN1_PCTX * pctx)372 asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid, const ASN1_PCTX *pctx)
373 {
374 	char objbuf[80];
375 	const char *ln;
376 	ln = OBJ_nid2ln(OBJ_obj2nid(oid));
377 	if (!ln)
378 		ln = "";
379 	OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
380 	if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
381 		return 0;
382 	return 1;
383 }
384 
385 static int
asn1_print_obstring_ctx(BIO * out,ASN1_STRING * str,int indent,const ASN1_PCTX * pctx)386 asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
387     const ASN1_PCTX *pctx)
388 {
389 	if (str->type == V_ASN1_BIT_STRING) {
390 		if (BIO_printf(out, " (%ld unused bits)\n",
391 		    str->flags & 0x7) <= 0)
392 			return 0;
393 	} else if (BIO_puts(out, "\n") <= 0)
394 		return 0;
395 	if ((str->length > 0) &&
396 	    BIO_dump_indent(out, (char *)str->data, str->length,
397 	    indent + 2) <= 0)
398 		return 0;
399 	return 1;
400 }
401 
402 static int
asn1_primitive_print(BIO * out,ASN1_VALUE ** fld,const ASN1_ITEM * it,int indent,const char * fname,const char * sname,const ASN1_PCTX * pctx)403 asn1_primitive_print(BIO *out, ASN1_VALUE **fld, const ASN1_ITEM *it,
404     int indent, const char *fname, const char *sname, const ASN1_PCTX *pctx)
405 {
406 	long utype;
407 	ASN1_STRING *str;
408 	int ret = 1, needlf = 1;
409 	const char *pname;
410 
411 	if (!asn1_print_fsname(out, indent, fname, sname, pctx))
412 		return 0;
413 
414 	if (it != NULL && it->funcs != NULL) {
415 		const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
416 
417 		if (pf->prim_print == NULL)
418 			return 0;
419 
420 		return pf->prim_print(out, fld, it, indent, pctx);
421 	}
422 	if (it->itype == ASN1_ITYPE_MSTRING) {
423 		str = (ASN1_STRING *)*fld;
424 		utype = str->type & ~V_ASN1_NEG;
425 	} else {
426 		utype = it->utype;
427 		if (utype == V_ASN1_BOOLEAN)
428 			str = NULL;
429 		else
430 			str = (ASN1_STRING *)*fld;
431 	}
432 	if (utype == V_ASN1_ANY) {
433 		ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
434 		utype = atype->type;
435 		fld = &atype->value.asn1_value;
436 		str = (ASN1_STRING *)*fld;
437 		if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
438 			pname = NULL;
439 		else
440 			pname = ASN1_tag2str(utype);
441 	} else {
442 		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
443 			pname = ASN1_tag2str(utype);
444 		else
445 			pname = NULL;
446 	}
447 
448 	if (utype == V_ASN1_NULL) {
449 		if (BIO_puts(out, "NULL\n") <= 0)
450 			return 0;
451 		return 1;
452 	}
453 
454 	if (pname) {
455 		if (BIO_puts(out, pname) <= 0)
456 			return 0;
457 		if (BIO_puts(out, ":") <= 0)
458 			return 0;
459 	}
460 
461 	switch (utype) {
462 	case V_ASN1_BOOLEAN:
463 		{
464 			int boolval = *(int *)fld;
465 			if (boolval == -1)
466 				boolval = it->size;
467 			ret = asn1_print_boolean_ctx(out, boolval, pctx);
468 		}
469 		break;
470 
471 	case V_ASN1_INTEGER:
472 	case V_ASN1_ENUMERATED:
473 		ret = asn1_print_integer_ctx(out, str, pctx);
474 		break;
475 
476 	case V_ASN1_UTCTIME:
477 		ret = ASN1_UTCTIME_print(out, str);
478 		break;
479 
480 	case V_ASN1_GENERALIZEDTIME:
481 		ret = ASN1_GENERALIZEDTIME_print(out, str);
482 		break;
483 
484 	case V_ASN1_OBJECT:
485 		ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
486 		break;
487 
488 	case V_ASN1_OCTET_STRING:
489 	case V_ASN1_BIT_STRING:
490 		ret = asn1_print_obstring_ctx(out, str, indent, pctx);
491 		needlf = 0;
492 		break;
493 
494 	case V_ASN1_SEQUENCE:
495 	case V_ASN1_SET:
496 	case V_ASN1_OTHER:
497 		if (BIO_puts(out, "\n") <= 0)
498 			return 0;
499 		if (ASN1_parse_dump(out, str->data, str->length,
500 		    indent, 0) <= 0)
501 			ret = 0;
502 		needlf = 0;
503 		break;
504 
505 	default:
506 		ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
507 	}
508 	if (!ret)
509 		return 0;
510 	if (needlf && BIO_puts(out, "\n") <= 0)
511 		return 0;
512 	return 1;
513 }
514