os/ossrv/ssl/tsrc/topenssl/src/asn1pars.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* apps/asn1pars.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
/* A nice addition from Dr Stephen Henson <shenson@bigfoot.com> to 
sl@0
    60
 * add the -strparse option which parses nested binary structures
sl@0
    61
 */
sl@0
    62
sl@0
    63
#include <stdio.h>
sl@0
    64
#include <stdlib.h>
sl@0
    65
#include <string.h>
sl@0
    66
#include "apps.h"
sl@0
    67
#include <openssl/err.h>
sl@0
    68
#include <openssl/evp.h>
sl@0
    69
#include <openssl/x509.h>
sl@0
    70
#include <openssl/pem.h>
sl@0
    71
sl@0
    72
/* -inform arg	- input format - default PEM (DER or PEM)
sl@0
    73
 * -in arg	- input file - default stdin
sl@0
    74
 * -i		- indent the details by depth
sl@0
    75
 * -offset	- where in the file to start
sl@0
    76
 * -length	- how many bytes to use
sl@0
    77
 * -oid file	- extra oid description file
sl@0
    78
 */
sl@0
    79
sl@0
    80
#undef PROG
sl@0
    81
#define PROG	asn1parse_main
sl@0
    82
sl@0
    83
sl@0
    84
sl@0
    85
int MAIN(int, char **);
sl@0
    86
sl@0
    87
static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf);
sl@0
    88
sl@0
    89
int MAIN(int argc, char **argv)
sl@0
    90
	{
sl@0
    91
	int i,badops=0,offset=0,ret=1,j;
sl@0
    92
	unsigned int length=0;
sl@0
    93
	long num,tmplen;
sl@0
    94
	BIO *in=NULL,*out=NULL,*b64=NULL, *derout = NULL;
sl@0
    95
	int informat,indent=0, noout = 0, dump = 0;
sl@0
    96
	char *infile=NULL,*str=NULL,*prog,*oidfile=NULL, *derfile=NULL;
sl@0
    97
	char *genstr=NULL, *genconf=NULL;
sl@0
    98
	unsigned char *tmpbuf;
sl@0
    99
	const unsigned char *ctmpbuf;
sl@0
   100
	BUF_MEM *buf=NULL;
sl@0
   101
	STACK *osk=NULL;
sl@0
   102
	ASN1_TYPE *at=NULL;
sl@0
   103
sl@0
   104
	informat=FORMAT_PEM;
sl@0
   105
sl@0
   106
	apps_startup();
sl@0
   107
sl@0
   108
	if (bio_err == NULL)
sl@0
   109
		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
sl@0
   110
			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
sl@0
   111
sl@0
   112
	if (!load_config(bio_err, NULL))
sl@0
   113
		goto end;
sl@0
   114
sl@0
   115
	prog=argv[0];
sl@0
   116
	argc--;
sl@0
   117
	argv++;
sl@0
   118
	if ((osk=sk_new_null()) == NULL)
sl@0
   119
		{
sl@0
   120
		BIO_printf(bio_err,"Memory allocation failure\n");
sl@0
   121
		goto end;
sl@0
   122
		}
sl@0
   123
	while (argc >= 1)
sl@0
   124
		{
sl@0
   125
		if 	(strcmp(*argv,"-inform") == 0)
sl@0
   126
			{
sl@0
   127
			if (--argc < 1) goto bad;
sl@0
   128
			informat=str2fmt(*(++argv));
sl@0
   129
			}
sl@0
   130
		else if (strcmp(*argv,"-in") == 0)
sl@0
   131
			{
sl@0
   132
			if (--argc < 1) goto bad;
sl@0
   133
			infile= *(++argv);
sl@0
   134
			}
sl@0
   135
		else if (strcmp(*argv,"-out") == 0)
sl@0
   136
			{
sl@0
   137
			if (--argc < 1) goto bad;
sl@0
   138
			derfile= *(++argv);
sl@0
   139
			}
sl@0
   140
		else if (strcmp(*argv,"-i") == 0)
sl@0
   141
			{
sl@0
   142
			indent=1;
sl@0
   143
			}
sl@0
   144
		else if (strcmp(*argv,"-noout") == 0) noout = 1;
sl@0
   145
		else if (strcmp(*argv,"-oid") == 0)
sl@0
   146
			{
sl@0
   147
			if (--argc < 1) goto bad;
sl@0
   148
			oidfile= *(++argv);
sl@0
   149
			}
sl@0
   150
		else if (strcmp(*argv,"-offset") == 0)
sl@0
   151
			{
sl@0
   152
			if (--argc < 1) goto bad;
sl@0
   153
			offset= atoi(*(++argv));
sl@0
   154
			}
sl@0
   155
		else if (strcmp(*argv,"-length") == 0)
sl@0
   156
			{
sl@0
   157
			if (--argc < 1) goto bad;
sl@0
   158
			length= atoi(*(++argv));
sl@0
   159
			if (length == 0) goto bad;
sl@0
   160
			}
sl@0
   161
		else if (strcmp(*argv,"-dump") == 0)
sl@0
   162
			{
sl@0
   163
			dump= -1;
sl@0
   164
			}
sl@0
   165
		else if (strcmp(*argv,"-dlimit") == 0)
sl@0
   166
			{
sl@0
   167
			if (--argc < 1) goto bad;
sl@0
   168
			dump= atoi(*(++argv));
sl@0
   169
			if (dump <= 0) goto bad;
sl@0
   170
			}
sl@0
   171
		else if (strcmp(*argv,"-strparse") == 0)
sl@0
   172
			{
sl@0
   173
			if (--argc < 1) goto bad;
sl@0
   174
			sk_push(osk,*(++argv));
sl@0
   175
			}
sl@0
   176
		else if (strcmp(*argv,"-genstr") == 0)
sl@0
   177
			{
sl@0
   178
			if (--argc < 1) goto bad;
sl@0
   179
			genstr= *(++argv);
sl@0
   180
			}
sl@0
   181
		else if (strcmp(*argv,"-genconf") == 0)
sl@0
   182
			{
sl@0
   183
			if (--argc < 1) goto bad;
sl@0
   184
			genconf= *(++argv);
sl@0
   185
			}
sl@0
   186
		else
sl@0
   187
			{
sl@0
   188
			BIO_printf(bio_err,"unknown option %s\n",*argv);
sl@0
   189
			badops=1;
sl@0
   190
			break;
sl@0
   191
			}
sl@0
   192
		argc--;
sl@0
   193
		argv++;
sl@0
   194
		}
sl@0
   195
sl@0
   196
	if (badops)
sl@0
   197
		{
sl@0
   198
bad:
sl@0
   199
		BIO_printf(bio_err,"%s [options] <infile\n",prog);
sl@0
   200
		BIO_printf(bio_err,"where options are\n");
sl@0
   201
		BIO_printf(bio_err," -inform arg   input format - one of DER PEM\n");
sl@0
   202
		BIO_printf(bio_err," -in arg       input file\n");
sl@0
   203
		BIO_printf(bio_err," -out arg      output file (output format is always DER\n");
sl@0
   204
		BIO_printf(bio_err," -noout arg    don't produce any output\n");
sl@0
   205
		BIO_printf(bio_err," -offset arg   offset into file\n");
sl@0
   206
		BIO_printf(bio_err," -length arg   length of section in file\n");
sl@0
   207
		BIO_printf(bio_err," -i            indent entries\n");
sl@0
   208
		BIO_printf(bio_err," -dump         dump unknown data in hex form\n");
sl@0
   209
		BIO_printf(bio_err," -dlimit arg   dump the first arg bytes of unknown data in hex form\n");
sl@0
   210
		BIO_printf(bio_err," -oid file     file of extra oid definitions\n");
sl@0
   211
		BIO_printf(bio_err," -strparse offset\n");
sl@0
   212
		BIO_printf(bio_err,"               a series of these can be used to 'dig' into multiple\n");
sl@0
   213
		BIO_printf(bio_err,"               ASN1 blob wrappings\n");
sl@0
   214
		BIO_printf(bio_err," -genstr str   string to generate ASN1 structure from\n");
sl@0
   215
		BIO_printf(bio_err," -genconf file file to generate ASN1 structure from\n");
sl@0
   216
		goto end;
sl@0
   217
		}
sl@0
   218
sl@0
   219
	ERR_load_crypto_strings();
sl@0
   220
sl@0
   221
	in=BIO_new(BIO_s_file());
sl@0
   222
	out=BIO_new(BIO_s_file());
sl@0
   223
	if ((in == NULL) || (out == NULL))
sl@0
   224
		{
sl@0
   225
		ERR_print_errors(bio_err);
sl@0
   226
		goto end;
sl@0
   227
		}
sl@0
   228
	BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
sl@0
   229
	
sl@0
   230
#ifdef OPENSSL_SYS_VMS
sl@0
   231
	{
sl@0
   232
	BIO *tmpbio = BIO_new(BIO_f_linebuffer());
sl@0
   233
	out = BIO_push(tmpbio, out);
sl@0
   234
	}
sl@0
   235
#endif
sl@0
   236
sl@0
   237
	if (oidfile != NULL)
sl@0
   238
		{
sl@0
   239
		if (BIO_read_filename(in,oidfile) <= 0)
sl@0
   240
			{
sl@0
   241
			BIO_printf(bio_err,"problems opening %s\n",oidfile);
sl@0
   242
			ERR_print_errors(bio_err);
sl@0
   243
			goto end;
sl@0
   244
			}
sl@0
   245
		OBJ_create_objects(in);
sl@0
   246
		}
sl@0
   247
sl@0
   248
	if (infile == NULL)
sl@0
   249
		BIO_set_fp(in,stdin,BIO_NOCLOSE);
sl@0
   250
	
sl@0
   251
	else
sl@0
   252
		{
sl@0
   253
		if (BIO_read_filename(in,infile) <= 0)
sl@0
   254
			{
sl@0
   255
			perror(infile);
sl@0
   256
			goto end;
sl@0
   257
			}
sl@0
   258
		}
sl@0
   259
sl@0
   260
	if (derfile) {
sl@0
   261
		if(!(derout = BIO_new_file(derfile, "wb"))) {
sl@0
   262
			BIO_printf(bio_err,"problems opening %s\n",derfile);
sl@0
   263
			ERR_print_errors(bio_err);
sl@0
   264
			goto end;
sl@0
   265
		}
sl@0
   266
	}
sl@0
   267
sl@0
   268
	if ((buf=BUF_MEM_new()) == NULL) goto end;
sl@0
   269
	if (!BUF_MEM_grow(buf,BUFSIZ*8)) goto end; /* Pre-allocate :-) */
sl@0
   270
sl@0
   271
	if (genstr || genconf)
sl@0
   272
		{
sl@0
   273
		num = do_generate(bio_err, genstr, genconf, buf);
sl@0
   274
		if (num < 0)
sl@0
   275
			{
sl@0
   276
			ERR_print_errors(bio_err);
sl@0
   277
			goto end;
sl@0
   278
			}
sl@0
   279
		}
sl@0
   280
sl@0
   281
	else
sl@0
   282
		{
sl@0
   283
sl@0
   284
		if (informat == FORMAT_PEM)
sl@0
   285
			{
sl@0
   286
			BIO *tmp;
sl@0
   287
sl@0
   288
			if ((b64=BIO_new(BIO_f_base64())) == NULL)
sl@0
   289
				goto end;
sl@0
   290
			BIO_push(b64,in);
sl@0
   291
			tmp=in;
sl@0
   292
			in=b64;
sl@0
   293
			b64=tmp;
sl@0
   294
			}
sl@0
   295
sl@0
   296
		num=0;
sl@0
   297
		for (;;)
sl@0
   298
			{
sl@0
   299
			if (!BUF_MEM_grow(buf,(int)num+BUFSIZ)) goto end;
sl@0
   300
			i=BIO_read(in,&(buf->data[num]),BUFSIZ);
sl@0
   301
			if (i <= 0) break;
sl@0
   302
			num+=i;
sl@0
   303
			}
sl@0
   304
		}
sl@0
   305
	str=buf->data;
sl@0
   306
sl@0
   307
	/* If any structs to parse go through in sequence */
sl@0
   308
sl@0
   309
	if (sk_num(osk))
sl@0
   310
		{
sl@0
   311
		tmpbuf=(unsigned char *)str;
sl@0
   312
		tmplen=num;
sl@0
   313
		for (i=0; i<sk_num(osk); i++)
sl@0
   314
			{
sl@0
   315
			ASN1_TYPE *atmp;
sl@0
   316
			int typ;
sl@0
   317
			j=atoi(sk_value(osk,i));
sl@0
   318
			if (j == 0)
sl@0
   319
				{
sl@0
   320
				BIO_printf(bio_err,"'%s' is an invalid number\n",sk_value(osk,i));
sl@0
   321
				continue;
sl@0
   322
				}
sl@0
   323
			tmpbuf+=j;
sl@0
   324
			tmplen-=j;
sl@0
   325
			atmp = at;
sl@0
   326
			ctmpbuf = tmpbuf;
sl@0
   327
			at = d2i_ASN1_TYPE(NULL,&ctmpbuf,tmplen);
sl@0
   328
			ASN1_TYPE_free(atmp);
sl@0
   329
			if(!at)
sl@0
   330
				{
sl@0
   331
				BIO_printf(bio_err,"Error parsing structure\n");
sl@0
   332
				ERR_print_errors(bio_err);
sl@0
   333
				goto end;
sl@0
   334
				}
sl@0
   335
			typ = ASN1_TYPE_get(at);
sl@0
   336
			if ((typ == V_ASN1_OBJECT)
sl@0
   337
				|| (typ == V_ASN1_NULL))
sl@0
   338
				{
sl@0
   339
				BIO_printf(bio_err, "Can't parse %s type\n",
sl@0
   340
					typ == V_ASN1_NULL ? "NULL" : "OBJECT");
sl@0
   341
				ERR_print_errors(bio_err);
sl@0
   342
				goto end;
sl@0
   343
				}
sl@0
   344
			/* hmm... this is a little evil but it works */
sl@0
   345
			tmpbuf=at->value.asn1_string->data;
sl@0
   346
			tmplen=at->value.asn1_string->length;
sl@0
   347
			}
sl@0
   348
		str=(char *)tmpbuf;
sl@0
   349
		num=tmplen;
sl@0
   350
		}
sl@0
   351
sl@0
   352
	if (offset >= num)
sl@0
   353
		{
sl@0
   354
		BIO_printf(bio_err, "Error: offset too large\n");
sl@0
   355
		goto end;
sl@0
   356
		}
sl@0
   357
sl@0
   358
	num -= offset;
sl@0
   359
sl@0
   360
	if ((length == 0) || ((long)length > num)) length=(unsigned int)num;
sl@0
   361
	if(derout) {
sl@0
   362
		if(BIO_write(derout, str + offset, length) != (int)length) {
sl@0
   363
			BIO_printf(bio_err, "Error writing output\n");
sl@0
   364
			ERR_print_errors(bio_err);
sl@0
   365
			goto end;
sl@0
   366
		}
sl@0
   367
	}
sl@0
   368
	if (!noout &&
sl@0
   369
	    !ASN1_parse_dump(out,(unsigned char *)&(str[offset]),length,
sl@0
   370
		    indent,dump))
sl@0
   371
		{
sl@0
   372
		ERR_print_errors(bio_err);
sl@0
   373
		goto end;
sl@0
   374
		}
sl@0
   375
	ret=0;
sl@0
   376
end:
sl@0
   377
	BIO_free(derout);
sl@0
   378
	if (in != NULL) BIO_free(in);
sl@0
   379
	if (out != NULL) BIO_free_all(out);
sl@0
   380
	if (b64 != NULL) BIO_free(b64);
sl@0
   381
	if (ret != 0)
sl@0
   382
		ERR_print_errors(bio_err);
sl@0
   383
	if (buf != NULL) BUF_MEM_free(buf);
sl@0
   384
	if (at != NULL) ASN1_TYPE_free(at);
sl@0
   385
	if (osk != NULL) sk_free(osk);
sl@0
   386
	OBJ_cleanup();
sl@0
   387
	apps_shutdown();
sl@0
   388
	OPENSSL_EXIT(ret);
sl@0
   389
	}
sl@0
   390
sl@0
   391
static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf)
sl@0
   392
	{
sl@0
   393
	CONF *cnf = NULL;
sl@0
   394
	int len;
sl@0
   395
	long errline;
sl@0
   396
	unsigned char *p;
sl@0
   397
	ASN1_TYPE *atyp = NULL;
sl@0
   398
sl@0
   399
	if (genconf)
sl@0
   400
		{
sl@0
   401
		cnf = NCONF_new(NULL);
sl@0
   402
		if (!NCONF_load(cnf, genconf, &errline))
sl@0
   403
			goto conferr;
sl@0
   404
		if (!genstr)
sl@0
   405
			genstr = NCONF_get_string(cnf, "default", "asn1");
sl@0
   406
		if (!genstr)
sl@0
   407
			{
sl@0
   408
			BIO_printf(bio, "Can't find 'asn1' in '%s'\n", genconf);
sl@0
   409
			goto err;
sl@0
   410
			}
sl@0
   411
		}
sl@0
   412
sl@0
   413
	atyp = ASN1_generate_nconf(genstr, cnf);
sl@0
   414
	NCONF_free(cnf);
sl@0
   415
sl@0
   416
	if (!atyp)
sl@0
   417
		return -1;
sl@0
   418
sl@0
   419
	len = i2d_ASN1_TYPE(atyp, NULL);
sl@0
   420
sl@0
   421
	if (len <= 0)
sl@0
   422
		goto err;
sl@0
   423
sl@0
   424
	if (!BUF_MEM_grow(buf,len))
sl@0
   425
		goto err;
sl@0
   426
sl@0
   427
	p=(unsigned char *)buf->data;
sl@0
   428
sl@0
   429
	i2d_ASN1_TYPE(atyp, &p);
sl@0
   430
sl@0
   431
	ASN1_TYPE_free(atyp);
sl@0
   432
	return len;
sl@0
   433
sl@0
   434
	conferr:
sl@0
   435
sl@0
   436
	if (errline > 0)
sl@0
   437
		BIO_printf(bio, "Error on line %ld of config file '%s'\n",
sl@0
   438
							errline, genconf);
sl@0
   439
	else
sl@0
   440
		BIO_printf(bio, "Error loading config file '%s'\n", genconf);
sl@0
   441
sl@0
   442
	err:
sl@0
   443
	NCONF_free(cnf);
sl@0
   444
	ASN1_TYPE_free(atyp);
sl@0
   445
sl@0
   446
	return -1;
sl@0
   447
sl@0
   448
	}