Home | History | Annotate | Download | only in asn1
      1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      2  * All rights reserved.
      3  *
      4  * This package is an SSL implementation written
      5  * by Eric Young (eay (at) cryptsoft.com).
      6  * The implementation was written so as to conform with Netscapes SSL.
      7  *
      8  * This library is free for commercial and non-commercial use as long as
      9  * the following conditions are aheared to.  The following conditions
     10  * apply to all code found in this distribution, be it the RC4, RSA,
     11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     12  * included with this distribution is covered by the same copyright terms
     13  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     14  *
     15  * Copyright remains Eric Young's, and as such any Copyright notices in
     16  * the code are not to be removed.
     17  * If this package is used in a product, Eric Young should be given attribution
     18  * as the author of the parts of the library used.
     19  * This can be in the form of a textual message at program startup or
     20  * in documentation (online or textual) provided with the package.
     21  *
     22  * Redistribution and use in source and binary forms, with or without
     23  * modification, are permitted provided that the following conditions
     24  * are met:
     25  * 1. Redistributions of source code must retain the copyright
     26  *    notice, this list of conditions and the following disclaimer.
     27  * 2. Redistributions in binary form must reproduce the above copyright
     28  *    notice, this list of conditions and the following disclaimer in the
     29  *    documentation and/or other materials provided with the distribution.
     30  * 3. All advertising materials mentioning features or use of this software
     31  *    must display the following acknowledgement:
     32  *    "This product includes cryptographic software written by
     33  *     Eric Young (eay (at) cryptsoft.com)"
     34  *    The word 'cryptographic' can be left out if the rouines from the library
     35  *    being used are not cryptographic related :-).
     36  * 4. If you include any Windows specific code (or a derivative thereof) from
     37  *    the apps directory (application code) you must include an acknowledgement:
     38  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     39  *
     40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     50  * SUCH DAMAGE.
     51  *
     52  * The licence and distribution terms for any publically available version or
     53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     54  * copied and put under another distribution licence
     55  * [including the GNU Public Licence.] */
     56 
     57 #include <openssl/asn1.h>
     58 
     59 #include <openssl/bio.h>
     60 #include <openssl/err.h>
     61 #include <openssl/mem.h>
     62 
     63 
     64 #define ASN1_PARSE_MAXDEPTH 128
     65 
     66 static int asn1_print_info(BIO *bp, int tag, int xclass,int constructed,
     67 	int indent);
     68 static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
     69 	int offset, int depth, int indent, int dump);
     70 static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
     71 	     int indent)
     72 	{
     73 	static const char fmt[]="%-18s";
     74 	char str[128];
     75 	const char *p;
     76 
     77 	if (constructed & V_ASN1_CONSTRUCTED)
     78 		p="cons: ";
     79 	else
     80 		p="prim: ";
     81 	if (BIO_write(bp,p,6) < 6) goto err;
     82 	BIO_indent(bp,indent,128);
     83 
     84 	p=str;
     85 	if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
     86 		BIO_snprintf(str,sizeof str,"priv [ %d ] ",tag);
     87 	else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
     88 		BIO_snprintf(str,sizeof str,"cont [ %d ]",tag);
     89 	else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
     90 		BIO_snprintf(str,sizeof str,"appl [ %d ]",tag);
     91 	else if (tag > 30)
     92 		BIO_snprintf(str,sizeof str,"<ASN1 %d>",tag);
     93 	else
     94 		p = ASN1_tag2str(tag);
     95 
     96 	if (BIO_printf(bp,fmt,p) <= 0)
     97 		goto err;
     98 	return(1);
     99 err:
    100 	return(0);
    101 	}
    102 
    103 int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
    104 	{
    105 	return(asn1_parse2(bp,&pp,len,0,0,indent,0));
    106 	}
    107 
    108 int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent, int dump)
    109 	{
    110 	return(asn1_parse2(bp,&pp,len,0,0,indent,dump));
    111 	}
    112 
    113 static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offset,
    114 	     int depth, int indent, int dump)
    115 	{
    116 	const unsigned char *p,*ep,*tot,*op,*opp;
    117 	long len;
    118 	int tag,xclass,ret=0;
    119 	int nl,hl,j,r;
    120 	ASN1_OBJECT *o=NULL;
    121 	ASN1_OCTET_STRING *os=NULL;
    122 	/* ASN1_BMPSTRING *bmp=NULL;*/
    123 	int dump_indent;
    124 
    125 #if 0
    126 	dump_indent = indent;
    127 #else
    128 	dump_indent = 6;	/* Because we know BIO_dump_indent() */
    129 #endif
    130 
    131 	if (depth > ASN1_PARSE_MAXDEPTH)
    132 		{
    133 		BIO_puts(bp, "BAD RECURSION DEPTH\n");
    134 		return 0;
    135 		}
    136 
    137 	p= *pp;
    138 	tot=p+length;
    139 	op=p-1;
    140 	while ((p < tot) && (op < p))
    141 		{
    142 		op=p;
    143 		j=ASN1_get_object(&p,&len,&tag,&xclass,length);
    144 #ifdef LINT
    145 		j=j;
    146 #endif
    147 		if (j & 0x80)
    148 			{
    149 			if (BIO_puts(bp, "Error in encoding\n") <= 0)
    150 				goto end;
    151 			ret=0;
    152 			goto end;
    153 			}
    154 		hl=(p-op);
    155 		length-=hl;
    156 		/* if j == 0x21 it is a constructed indefinite length object */
    157 		if (BIO_printf(bp,"%5ld:",(long)offset+(long)(op- *pp))
    158 			<= 0) goto end;
    159 
    160 		if (j != (V_ASN1_CONSTRUCTED | 1))
    161 			{
    162 			if (BIO_printf(bp,"d=%-2d hl=%ld l=%4ld ",
    163 				depth,(long)hl,len) <= 0)
    164 				goto end;
    165 			}
    166 		else
    167 			{
    168 			if (BIO_printf(bp,"d=%-2d hl=%ld l=inf  ",
    169 				depth,(long)hl) <= 0)
    170 				goto end;
    171 			}
    172 		if (!asn1_print_info(bp,tag,xclass,j,(indent)?depth:0))
    173 			goto end;
    174 		if (j & V_ASN1_CONSTRUCTED)
    175 			{
    176 			ep=p+len;
    177 			if (BIO_puts(bp, "\n") <= 0) goto end;
    178 			if (len > length)
    179 				{
    180 				BIO_printf(bp,
    181 					"length is greater than %ld\n",length);
    182 				ret=0;
    183 				goto end;
    184 				}
    185 			if ((j == 0x21) && (len == 0))
    186 				{
    187 				for (;;)
    188 					{
    189 					r=asn1_parse2(bp,&p,(long)(tot-p),
    190 						offset+(p - *pp),depth+1,
    191 						indent,dump);
    192 					if (r == 0) { ret=0; goto end; }
    193 					if ((r == 2) || (p >= tot)) break;
    194 					}
    195 				}
    196 			else
    197 				while (p < ep)
    198 					{
    199 					r=asn1_parse2(bp,&p,(long)len,
    200 						offset+(p - *pp),depth+1,
    201 						indent,dump);
    202 					if (r == 0) { ret=0; goto end; }
    203 					}
    204 			}
    205 		else if (xclass != 0)
    206 			{
    207 			p+=len;
    208 			if (BIO_puts(bp, "\n") <= 0) goto end;
    209 			}
    210 		else
    211 			{
    212 			nl=0;
    213 			if (	(tag == V_ASN1_PRINTABLESTRING) ||
    214 				(tag == V_ASN1_T61STRING) ||
    215 				(tag == V_ASN1_IA5STRING) ||
    216 				(tag == V_ASN1_VISIBLESTRING) ||
    217 				(tag == V_ASN1_NUMERICSTRING) ||
    218 				(tag == V_ASN1_UTF8STRING) ||
    219 				(tag == V_ASN1_UTCTIME) ||
    220 				(tag == V_ASN1_GENERALIZEDTIME))
    221 				{
    222 				if (BIO_puts(bp, ":") <= 0) goto end;
    223 				if ((len > 0) &&
    224 					BIO_write(bp,(const char *)p,(int)len)
    225 					!= (int)len)
    226 					goto end;
    227 				}
    228 			else if (tag == V_ASN1_OBJECT)
    229 				{
    230 				opp=op;
    231 				if (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL)
    232 					{
    233 					if (BIO_puts(bp, ":") <= 0) goto end;
    234 					i2a_ASN1_OBJECT(bp,o);
    235 					}
    236 				else
    237 					{
    238 					if (BIO_puts(bp, ":BAD OBJECT") <= 0)
    239 						goto end;
    240 					}
    241 				}
    242 			else if (tag == V_ASN1_BOOLEAN)
    243 				{
    244 				int ii;
    245 
    246 				opp=op;
    247 				ii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl);
    248 				if (ii < 0)
    249 					{
    250 					if (BIO_puts(bp, "Bad boolean\n") <= 0)
    251 						goto end;
    252 					}
    253 				BIO_printf(bp,":%d",ii);
    254 				}
    255 			else if (tag == V_ASN1_BMPSTRING)
    256 				{
    257 				/* do the BMP thang */
    258 				}
    259 			else if (tag == V_ASN1_OCTET_STRING)
    260 				{
    261 				int i,printable=1;
    262 
    263 				opp=op;
    264 				os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl);
    265 				if (os != NULL && os->length > 0)
    266 					{
    267 					opp = os->data;
    268 					/* testing whether the octet string is
    269 					 * printable */
    270 					for (i=0; i<os->length; i++)
    271 						{
    272 						if ((	(opp[i] < ' ') &&
    273 							(opp[i] != '\n') &&
    274 							(opp[i] != '\r') &&
    275 							(opp[i] != '\t')) ||
    276 							(opp[i] > '~'))
    277 							{
    278 							printable=0;
    279 							break;
    280 							}
    281 						}
    282 					if (printable)
    283 					/* printable string */
    284 						{
    285 						if (BIO_puts(bp, ":") <= 0)
    286 							goto end;
    287 						if (BIO_write(bp,(const char *)opp,
    288 							os->length) <= 0)
    289 							goto end;
    290 						}
    291 					else if (!dump)
    292 					/* not printable => print octet string
    293 					 * as hex dump */
    294 						{
    295 						if (BIO_puts(bp, "[HEX DUMP]:") <= 0)
    296 							goto end;
    297 						for (i=0; i<os->length; i++)
    298 							{
    299 							if (BIO_printf(bp,"%02X"
    300 								, opp[i]) <= 0)
    301 								goto end;
    302 							}
    303 						}
    304 					else
    305 					/* print the normal dump */
    306 						{
    307 						if (!nl)
    308 							{
    309 							if (BIO_puts(bp, "\n") <= 0)
    310 								goto end;
    311 							}
    312 						if (!BIO_hexdump(bp, opp,
    313 							((dump == -1 || dump >
    314 							os->length)?os->length:dump),
    315 							dump_indent))
    316 							goto end;
    317 						nl=1;
    318 						}
    319 					}
    320 				if (os != NULL)
    321 					{
    322 					M_ASN1_OCTET_STRING_free(os);
    323 					os=NULL;
    324 					}
    325 				}
    326 			else if (tag == V_ASN1_INTEGER)
    327 				{
    328 				ASN1_INTEGER *bs;
    329 				int i;
    330 
    331 				opp=op;
    332 				bs=d2i_ASN1_INTEGER(NULL,&opp,len+hl);
    333 				if (bs != NULL)
    334 					{
    335 					if (BIO_puts(bp, ":") <= 0) goto end;
    336 					if (bs->type == V_ASN1_NEG_INTEGER)
    337 						if (BIO_puts(bp, "-") <= 0)
    338 							goto end;
    339 					for (i=0; i<bs->length; i++)
    340 						{
    341 						if (BIO_printf(bp,"%02X",
    342 							bs->data[i]) <= 0)
    343 							goto end;
    344 						}
    345 					if (bs->length == 0)
    346 						{
    347 						if (BIO_puts(bp, "00") <= 0)
    348 							goto end;
    349 						}
    350 					}
    351 				else
    352 					{
    353 					if (BIO_puts(bp, "BAD INTEGER") <= 0)
    354 						goto end;
    355 					}
    356 				M_ASN1_INTEGER_free(bs);
    357 				}
    358 			else if (tag == V_ASN1_ENUMERATED)
    359 				{
    360 				ASN1_ENUMERATED *bs;
    361 				int i;
    362 
    363 				opp=op;
    364 				bs=d2i_ASN1_ENUMERATED(NULL,&opp,len+hl);
    365 				if (bs != NULL)
    366 					{
    367 					if (BIO_puts(bp, ":") <= 0) goto end;
    368 					if (bs->type == V_ASN1_NEG_ENUMERATED)
    369 						if (BIO_puts(bp, "-") <= 0)
    370 							goto end;
    371 					for (i=0; i<bs->length; i++)
    372 						{
    373 						if (BIO_printf(bp,"%02X",
    374 							bs->data[i]) <= 0)
    375 							goto end;
    376 						}
    377 					if (bs->length == 0)
    378 						{
    379 						if (BIO_puts(bp, "00") <= 0)
    380 							goto end;
    381 						}
    382 					}
    383 				else
    384 					{
    385 					if (BIO_puts(bp, "BAD ENUMERATED") <= 0)
    386 						goto end;
    387 					}
    388 				M_ASN1_ENUMERATED_free(bs);
    389 				}
    390 			else if (len > 0 && dump)
    391 				{
    392 				if (!nl)
    393 					{
    394 					if (BIO_puts(bp, "\n") <= 0)
    395 						goto end;
    396 					}
    397 				if (!BIO_hexdump(bp,p,
    398 					((dump == -1 || dump > len)?len:dump),
    399 					dump_indent))
    400 					goto end;
    401 				nl=1;
    402 				}
    403 
    404 			if (!nl)
    405 				{
    406 				if (BIO_puts(bp, "\n") <= 0) goto end;
    407 				}
    408 			p+=len;
    409 			if ((tag == V_ASN1_EOC) && (xclass == 0))
    410 				{
    411 				ret=2; /* End of sequence */
    412 				goto end;
    413 				}
    414 			}
    415 		length-=len;
    416 		}
    417 	ret=1;
    418 end:
    419 	if (o != NULL) ASN1_OBJECT_free(o);
    420 	if (os != NULL) M_ASN1_OCTET_STRING_free(os);
    421 	*pp=p;
    422 	return(ret);
    423 	}
    424 
    425 const char *ASN1_tag2str(int tag)
    426 {
    427 	static const char * const tag2str[] = {
    428 	 "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", /* 0-4 */
    429 	 "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", /* 5-9 */
    430 	 "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>", 	    /* 10-13 */
    431 	"<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET", 		    /* 15-17 */
    432 	"NUMERICSTRING", "PRINTABLESTRING", "T61STRING",	    /* 18-20 */
    433 	"VIDEOTEXSTRING", "IA5STRING", "UTCTIME","GENERALIZEDTIME", /* 21-24 */
    434 	"GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",	    /* 25-27 */
    435 	"UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"		    /* 28-30 */
    436 	};
    437 
    438 	if((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
    439 							tag &= ~0x100;
    440 
    441 	if(tag < 0 || tag > 30) return "(unknown)";
    442 	return tag2str[tag];
    443 }
    444 
    445