tasn_prn.c 14.0 KB
Newer Older
D
 
Dr. Stephen Henson 已提交
1 2 3 4 5
/* tasn_prn.c */
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
 * project 2000.
 */
/* ====================================================================
6
 * Copyright (c) 2000,2005 The OpenSSL Project.  All rights reserved.
D
 
Dr. Stephen Henson 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */


#include <stddef.h>
61
#include "cryptlib.h"
D
 
Dr. Stephen Henson 已提交
62
#include <openssl/asn1.h>
63
#include <openssl/asn1t.h>
D
 
Dr. Stephen Henson 已提交
64 65 66
#include <openssl/objects.h>
#include <openssl/buffer.h>
#include <openssl/err.h>
67 68
#include <openssl/x509v3.h>
#include "asn1_locl.h"
D
 
Dr. Stephen Henson 已提交
69

70
/* Print routines.
D
 
Dr. Stephen Henson 已提交
71 72
 */

73
/* ASN1_PCTX routines */
D
 
Dr. Stephen Henson 已提交
74

75 76 77 78 79 80 81 82 83
ASN1_PCTX default_pctx = 
	{
	ASN1_PCTX_FLAGS_SHOW_ABSENT,	/* flags */
	0,	/* nm_flags */
	0,	/* cert_flags */
	0,	/* oid_flags */
	0	/* str_flags */
	};
	
D
 
Dr. Stephen Henson 已提交
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
ASN1_PCTX *ASN1_PCTX_new(void)
	{
	ASN1_PCTX *ret;
	ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
	if (ret == NULL)
		{
		ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
		return NULL;
		}
	ret->flags = 0;
	ret->nm_flags = 0;
	ret->cert_flags = 0;
	ret->oid_flags = 0;
	ret->str_flags = 0;
	return ret;
	}

void ASN1_PCTX_free(ASN1_PCTX *p)
	{
	OPENSSL_free(p);
	}

unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
	{
	return p->flags;
	}

void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
	{
	p->flags = flags;
	}

unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
	{
	return p->nm_flags;
	}

void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
	{
	p->nm_flags = flags;
	}

unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
	{
	return p->cert_flags;
	}

void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
	{
	p->cert_flags = flags;
	}

unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
	{
	return p->oid_flags;
	}

void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
	{
	p->oid_flags = flags;
	}

unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
	{
	return p->str_flags;
	}

void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
	{
	p->str_flags = flags;
	}

/* Main print routines */

static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
160 161 162
				const ASN1_ITEM *it,
				const char *fname, const char *sname,
				int nohdr, const ASN1_PCTX *pctx);
163

164 165
int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
				const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
166

167 168
static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
				const ASN1_ITEM *it, int indent,
169
				const char *fname, const char *sname,
170
				const ASN1_PCTX *pctx);
171

172 173 174
static int asn1_print_fsname(BIO *out, int indent,
			const char *fname, const char *sname,
			const ASN1_PCTX *pctx);
175 176 177 178

int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
				const ASN1_ITEM *it, const ASN1_PCTX *pctx)
	{
179
	const char *sname;
180 181
	if (pctx == NULL)
		pctx = &default_pctx;
182 183 184 185 186 187
	if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
		sname = NULL;
	else
		sname = it->sname;
	return asn1_item_print_ctx(out, &ifld, indent, it,
							NULL, sname, 0, pctx);
188 189 190
	}

static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
191 192 193
				const ASN1_ITEM *it,
				const char *fname, const char *sname,
				int nohdr, const ASN1_PCTX *pctx)
194
	{
D
 
Dr. Stephen Henson 已提交
195
	const ASN1_TEMPLATE *tt;
196 197
	const ASN1_EXTERN_FUNCS *ef;
	ASN1_VALUE **tmpfld;
198 199 200
	const ASN1_AUX *aux = it->funcs;
	ASN1_aux_cb *asn1_cb;
	ASN1_PRINT_ARG parg;
D
 
Dr. Stephen Henson 已提交
201
	int i;
202 203 204 205 206 207 208 209 210
	if (aux && aux->asn1_cb)
		{
		parg.out = out;
		parg.indent = indent;
		parg.pctx = pctx;
		asn1_cb = aux->asn1_cb;
		}
	else asn1_cb = 0;

211 212
	if(*fld == NULL)
		{
213
		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
214
			{
215 216
			if (!nohdr && !asn1_print_fsname(out, indent,
							fname, sname, pctx))
217 218 219 220
				return 0;
			if (BIO_puts(out, "<ABSENT>\n") <= 0)
				return 0;
			}
D
 
Dr. Stephen Henson 已提交
221
		return 1;
222
		}
D
 
Dr. Stephen Henson 已提交
223

224 225
	switch(it->itype)
		{
D
 
Dr. Stephen Henson 已提交
226 227
		case ASN1_ITYPE_PRIMITIVE:
		if(it->templates)
228 229 230 231 232 233
			{
			if (!asn1_template_print_ctx(out, fld, indent,
							it->templates, pctx))
				return 0;
			}
		/* fall thru */
D
 
Dr. Stephen Henson 已提交
234
		case ASN1_ITYPE_MSTRING:
235 236
		if (!asn1_primitive_print(out, fld, it,
				indent, fname, sname,pctx))
237 238
			return 0;
		break;
D
 
Dr. Stephen Henson 已提交
239 240

		case ASN1_ITYPE_EXTERN:
241 242
		if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
			return 0;
243 244 245 246
		/* Use new style print routine if possible */
		ef = it->funcs;
		if (ef && ef->asn1_ex_print)
			{
247 248
			i = ef->asn1_ex_print(out, fld, indent, "", pctx);
			if (!i)
249
				return 0;
250 251 252
			if ((i == 2) && (BIO_puts(out, "\n") <= 0))
				return 0;
			return 1;
253
			}
254 255
		else if (sname && 
			BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
256 257
			return 0;
		break;
D
 
Dr. Stephen Henson 已提交
258 259

		case ASN1_ITYPE_CHOICE:
260 261 262 263
#if 0
		if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
			return 0;
#endif
D
 
Dr. Stephen Henson 已提交
264 265 266
		/* CHOICE type, get selector */
		i = asn1_get_choice_selector(fld, it);
		/* This should never happen... */
267 268 269 270 271
		if((i < 0) || (i >= it->tcount))
			{
			if (BIO_printf(out,
				"ERROR: selector [%d] invalid\n", i) <= 0)
				return 0;
D
 
Dr. Stephen Henson 已提交
272
			return 1;
273
			}
D
 
Dr. Stephen Henson 已提交
274
		tt = it->templates + i;
275 276 277 278
		tmpfld = asn1_get_field_ptr(fld, tt);
		if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
			return 0;
		break;
D
 
Dr. Stephen Henson 已提交
279 280

		case ASN1_ITYPE_SEQUENCE:
281
		case ASN1_ITYPE_NDEF_SEQUENCE:
282 283 284
		if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
			return 0;
		if (fname || sname)
285
			{
286 287 288 289 290 291 292 293 294 295
			if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
				{
				if (BIO_puts(out, " {\n") <= 0)
					return 0;
				}
			else
				{
				if (BIO_puts(out, "\n") <= 0)
					return 0;
				}
296 297
			}

298 299 300 301 302 303 304 305 306
		if (asn1_cb)
			{
			i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
			if (i == 0)
				return 0;
			if (i == 2)
				return 1;
			}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		/* Print each field entry */
		for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
			{
			const ASN1_TEMPLATE *seqtt;
			seqtt = asn1_do_adb(fld, tt, 1);
			tmpfld = asn1_get_field_ptr(fld, seqtt);
			if (!asn1_template_print_ctx(out, tmpfld,
						indent + 2, seqtt, pctx))
				return 0;
			}
		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
			{
			if (BIO_printf(out, "%*s}\n", indent, "") < 0)
				return 0;
			}
322 323 324 325 326 327 328

		if (asn1_cb)
			{
			i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
			if (i == 0)
				return 0;
			}
329
		break;
D
 
Dr. Stephen Henson 已提交
330 331

		default:
332
		BIO_printf(out, "Unprocessed type %d\n", it->itype);
D
 
Dr. Stephen Henson 已提交
333
		return 0;
334
		}
335

336
	return 1;
D
 
Dr. Stephen Henson 已提交
337 338
	}

339 340 341
int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
				const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
	{
D
 
Dr. Stephen Henson 已提交
342
	int i, flags;
343
	const char *sname, *fname;
D
 
Dr. Stephen Henson 已提交
344
	flags = tt->flags;
345
	if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
D
Dr. Stephen Henson 已提交
346
		sname = ASN1_ITEM_ptr(tt->item)->sname;
347 348 349 350 351 352
	else
		sname = NULL;
	if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
		fname = NULL;
	else
		fname = tt->field_name;
353 354
	if(flags & ASN1_TFLG_SK_MASK)
		{
D
 
Dr. Stephen Henson 已提交
355
		char *tname;
356
		ASN1_VALUE *skitem;
D
 
Dr. Stephen Henson 已提交
357
		/* SET OF, SEQUENCE OF */
358
		if (fname)
359
			{
360 361 362 363 364 365 366 367 368 369 370 371
			if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
				{
				if(flags & ASN1_TFLG_SET_OF)
					tname = "SET";
				else
					tname = "SEQUENCE";
				if (BIO_printf(out, "%*s%s OF %s {\n",
					indent, "", tname, tt->field_name) <= 0)
					return 0;
				}
			else if (BIO_printf(out, "%*s%s:\n", indent, "",
					fname) <= 0)
372 373 374 375
				return 0;
			}
		for(i = 0; i < sk_num((STACK *)*fld); i++)
			{
376
			if ((i > 0) && (BIO_puts(out, "\n") <= 0))
377
				return 0;
378

379 380
			skitem = (ASN1_VALUE *)sk_value((STACK *)*fld, i);
			if (!asn1_item_print_ctx(out, &skitem, indent + 2,
D
Dr. Stephen Henson 已提交
381
				ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
382 383
				return 0;
			}
384 385
		if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
				return 0;
386 387 388 389
		if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
			{
			if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
				return 0;
D
 
Dr. Stephen Henson 已提交
390 391
			}
		return 1;
392
		}
D
Dr. Stephen Henson 已提交
393
	return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
394
							fname, sname, 0, pctx);
D
 
Dr. Stephen Henson 已提交
395
	}
396

397
static int asn1_print_fsname(BIO *out, int indent,
398 399 400 401 402
			const char *fname, const char *sname,
			const ASN1_PCTX *pctx)
	{
	static char spaces[] = "                    ";
	const int nspaces = sizeof(spaces) - 1;
403 404 405 406 407 408

#if 0
	if (!sname && !fname)
		return 1;
#endif

409 410 411 412 413
	while (indent > nspaces)
		{
		if (BIO_write(out, spaces, nspaces) != nspaces)
			return 0;
		indent -= nspaces;
D
 
Dr. Stephen Henson 已提交
414
		}
415 416
	if (BIO_write(out, spaces, indent) != indent)
		return 0;
417 418 419 420 421
	if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
		sname = NULL;
	if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
		fname = NULL;
	if (!sname && !fname)
422
		return 1;
423
	if (fname)
424 425 426 427
		{
		if (BIO_puts(out, fname) <= 0)
			return 0;
		}
428
	if (sname)
429
		{
430 431 432 433 434 435
		if (fname)
			{
			if (BIO_printf(out, " (%s)", sname) <= 0)
				return 0;
			}
		else
436 437 438 439 440
			{
			if (BIO_puts(out, sname) <= 0)
				return 0;
			}
		}
441
	if (BIO_write(out, ": ", 2) != 2)
442 443 444 445
		return 0;
	return 1;
	}

446 447
static int asn1_print_boolean_ctx(BIO *out, const int bool,
							const ASN1_PCTX *pctx)
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	{
	const char *str;
	switch (bool)
		{
		case -1:
		str = "BOOL ABSENT";
		break;

		case 0:
		str = "FALSE";
		break;

		default:
		str = "TRUE";
		break;

		}

	if (BIO_puts(out, str) <= 0)
		return 0;
	return 1;

	}

static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
						const ASN1_PCTX *pctx)
	{
	char *s;
	int ret = 1;
	s = i2s_ASN1_INTEGER(NULL, str);
478
	if (BIO_puts(out, s) <= 0)
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
		ret = 0;
	OPENSSL_free(s);
	return ret;
	}

static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
						const ASN1_PCTX *pctx)
	{
	char objbuf[80];
	const char *ln;
	ln = OBJ_nid2ln(OBJ_obj2nid(oid));
	if(!ln)
		ln = "";
	OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
	if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
		return 0;
	return 1;
	}

static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
						const ASN1_PCTX *pctx)
	{
	if (str->type == V_ASN1_BIT_STRING)
		{
		if (BIO_printf(out, " (%ld unused bits)\n",
					str->flags & 0x7) <= 0)
				return 0;
		}
	else if (BIO_puts(out, "\n") <= 0)
		return 0;
509 510
	if ((str->length > 0)
		&& BIO_dump_indent(out, (char *)str->data, str->length,
511 512
				indent + 2) <= 0)
		return 0;
D
 
Dr. Stephen Henson 已提交
513
	return 1;
514 515 516 517
	}

static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
				const ASN1_ITEM *it, int indent,
518
				const char *fname, const char *sname,
519 520 521 522
				const ASN1_PCTX *pctx)
	{
	long utype;
	ASN1_STRING *str;
523 524 525 526
	int ret = 1, needlf = 1;
	const char *pname;
	if (!asn1_print_fsname(out, indent, fname, sname, pctx))
			return 0;
527 528 529 530 531 532 533 534 535
	str = (ASN1_STRING *)*fld;
	if (it->itype == ASN1_ITYPE_MSTRING)
		utype = str->type & ~V_ASN1_NEG;
	else
		utype = it->utype;
	if (utype == V_ASN1_ANY)
		{
		ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
		utype = atype->type;
536
		fld = &atype->value.asn1_value;
537
		str = (ASN1_STRING *)*fld;
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
		if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
			pname = NULL;
		else 
			pname = ASN1_tag2str(utype);
		}
	else
		{
		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
			pname = ASN1_tag2str(utype);
		else 
			pname = NULL;
		}

	if (utype == V_ASN1_NULL)
		{
		if (BIO_puts(out, "NULL\n") <= 0)
			return 0;
		return 1;
		}

	if (pname)
		{
		if (BIO_puts(out, pname) <= 0)
			return 0;
		if (BIO_puts(out, ":") <= 0)
			return 0;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
		}

	switch (utype)
		{
		case V_ASN1_BOOLEAN:
			{
			int bool = *(int *)fld;
			if (bool == -1)
				bool = it->size;
			ret = asn1_print_boolean_ctx(out, bool, pctx);
			}
		break;

		case V_ASN1_INTEGER:
		case V_ASN1_ENUMERATED:
		ret = asn1_print_integer_ctx(out, str, pctx);
		break;

		case V_ASN1_UTCTIME:
		ret = ASN1_UTCTIME_print(out, str);
		break;

		case V_ASN1_GENERALIZEDTIME:
		ret = ASN1_GENERALIZEDTIME_print(out, str);
		break;

		case V_ASN1_OBJECT:
		ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
		break;

		case V_ASN1_OCTET_STRING:
		case V_ASN1_BIT_STRING:
		ret = asn1_print_obstring_ctx(out, str, indent, pctx);
597
		needlf = 0;
598 599 600 601
		break;

		case V_ASN1_SEQUENCE:
		case V_ASN1_SET:
602 603 604
		case V_ASN1_OTHER:
		if (BIO_puts(out, "\n") <= 0)
			return 0;
605 606 607
		if (ASN1_parse_dump(out, str->data, str->length,
						indent, 0) <= 0)
			ret = 0;
608
		needlf = 0;
609 610 611 612 613 614 615 616
		break;

		default:
		ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);

		}
	if (!ret)
		return 0;
617 618
	if (needlf && BIO_puts(out, "\n") <= 0)
		return 0;
619 620
	return 1;
	}