155714Skris/* apps/asn1pars.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59194206Ssimon/* A nice addition from Dr Stephen Henson <steve@openssl.org> to
6055714Skris * add the -strparse option which parses nested binary structures
6155714Skris */
6255714Skris
6355714Skris#include <stdio.h>
6455714Skris#include <stdlib.h>
6555714Skris#include <string.h>
6655714Skris#include "apps.h"
6755714Skris#include <openssl/err.h>
6855714Skris#include <openssl/evp.h>
6955714Skris#include <openssl/x509.h>
7055714Skris#include <openssl/pem.h>
7155714Skris
7255714Skris/* -inform arg	- input format - default PEM (DER or PEM)
7355714Skris * -in arg	- input file - default stdin
7455714Skris * -i		- indent the details by depth
7555714Skris * -offset	- where in the file to start
7655714Skris * -length	- how many bytes to use
7759191Skris * -oid file	- extra oid description file
7855714Skris */
7955714Skris
8055714Skris#undef PROG
8155714Skris#define PROG	asn1parse_main
8255714Skris
8359191Skrisint MAIN(int, char **);
8459191Skris
85160814Ssimonstatic int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf);
86160814Ssimon
8755714Skrisint MAIN(int argc, char **argv)
8855714Skris	{
8955714Skris	int i,badops=0,offset=0,ret=1,j;
9055714Skris	unsigned int length=0;
9155714Skris	long num,tmplen;
9255714Skris	BIO *in=NULL,*out=NULL,*b64=NULL, *derout = NULL;
9368651Skris	int informat,indent=0, noout = 0, dump = 0;
9455714Skris	char *infile=NULL,*str=NULL,*prog,*oidfile=NULL, *derfile=NULL;
95160814Ssimon	char *genstr=NULL, *genconf=NULL;
9655714Skris	unsigned char *tmpbuf;
97160814Ssimon	const unsigned char *ctmpbuf;
9855714Skris	BUF_MEM *buf=NULL;
99238405Sjkim	STACK_OF(OPENSSL_STRING) *osk=NULL;
10055714Skris	ASN1_TYPE *at=NULL;
10155714Skris
10255714Skris	informat=FORMAT_PEM;
10355714Skris
10455714Skris	apps_startup();
10555714Skris
10655714Skris	if (bio_err == NULL)
10755714Skris		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
10855714Skris			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
10955714Skris
110109998Smarkm	if (!load_config(bio_err, NULL))
111109998Smarkm		goto end;
112109998Smarkm
11355714Skris	prog=argv[0];
11455714Skris	argc--;
11555714Skris	argv++;
116238405Sjkim	if ((osk=sk_OPENSSL_STRING_new_null()) == NULL)
11755714Skris		{
11868651Skris		BIO_printf(bio_err,"Memory allocation failure\n");
11955714Skris		goto end;
12055714Skris		}
12155714Skris	while (argc >= 1)
12255714Skris		{
12355714Skris		if 	(strcmp(*argv,"-inform") == 0)
12455714Skris			{
12555714Skris			if (--argc < 1) goto bad;
12655714Skris			informat=str2fmt(*(++argv));
12755714Skris			}
12855714Skris		else if (strcmp(*argv,"-in") == 0)
12955714Skris			{
13055714Skris			if (--argc < 1) goto bad;
13155714Skris			infile= *(++argv);
13255714Skris			}
13355714Skris		else if (strcmp(*argv,"-out") == 0)
13455714Skris			{
13555714Skris			if (--argc < 1) goto bad;
13655714Skris			derfile= *(++argv);
13755714Skris			}
13855714Skris		else if (strcmp(*argv,"-i") == 0)
13955714Skris			{
14055714Skris			indent=1;
14155714Skris			}
14259191Skris		else if (strcmp(*argv,"-noout") == 0) noout = 1;
14355714Skris		else if (strcmp(*argv,"-oid") == 0)
14455714Skris			{
14555714Skris			if (--argc < 1) goto bad;
14655714Skris			oidfile= *(++argv);
14755714Skris			}
14855714Skris		else if (strcmp(*argv,"-offset") == 0)
14955714Skris			{
15055714Skris			if (--argc < 1) goto bad;
15155714Skris			offset= atoi(*(++argv));
15255714Skris			}
15355714Skris		else if (strcmp(*argv,"-length") == 0)
15455714Skris			{
15555714Skris			if (--argc < 1) goto bad;
15655714Skris			length= atoi(*(++argv));
15755714Skris			if (length == 0) goto bad;
15855714Skris			}
15968651Skris		else if (strcmp(*argv,"-dump") == 0)
16068651Skris			{
16168651Skris			dump= -1;
16268651Skris			}
16368651Skris		else if (strcmp(*argv,"-dlimit") == 0)
16468651Skris			{
16568651Skris			if (--argc < 1) goto bad;
16668651Skris			dump= atoi(*(++argv));
16768651Skris			if (dump <= 0) goto bad;
16868651Skris			}
16955714Skris		else if (strcmp(*argv,"-strparse") == 0)
17055714Skris			{
17155714Skris			if (--argc < 1) goto bad;
172238405Sjkim			sk_OPENSSL_STRING_push(osk,*(++argv));
17355714Skris			}
174160814Ssimon		else if (strcmp(*argv,"-genstr") == 0)
175160814Ssimon			{
176160814Ssimon			if (--argc < 1) goto bad;
177160814Ssimon			genstr= *(++argv);
178160814Ssimon			}
179160814Ssimon		else if (strcmp(*argv,"-genconf") == 0)
180160814Ssimon			{
181160814Ssimon			if (--argc < 1) goto bad;
182160814Ssimon			genconf= *(++argv);
183160814Ssimon			}
18455714Skris		else
18555714Skris			{
18655714Skris			BIO_printf(bio_err,"unknown option %s\n",*argv);
18755714Skris			badops=1;
18855714Skris			break;
18955714Skris			}
19055714Skris		argc--;
19155714Skris		argv++;
19255714Skris		}
19355714Skris
19455714Skris	if (badops)
19555714Skris		{
19655714Skrisbad:
19755714Skris		BIO_printf(bio_err,"%s [options] <infile\n",prog);
19855714Skris		BIO_printf(bio_err,"where options are\n");
199160814Ssimon		BIO_printf(bio_err," -inform arg   input format - one of DER PEM\n");
20055714Skris		BIO_printf(bio_err," -in arg       input file\n");
201100936Snectar		BIO_printf(bio_err," -out arg      output file (output format is always DER\n");
20259191Skris		BIO_printf(bio_err," -noout arg    don't produce any output\n");
20355714Skris		BIO_printf(bio_err," -offset arg   offset into file\n");
20459191Skris		BIO_printf(bio_err," -length arg   length of section in file\n");
20555714Skris		BIO_printf(bio_err," -i            indent entries\n");
20668651Skris		BIO_printf(bio_err," -dump         dump unknown data in hex form\n");
20768651Skris		BIO_printf(bio_err," -dlimit arg   dump the first arg bytes of unknown data in hex form\n");
20855714Skris		BIO_printf(bio_err," -oid file     file of extra oid definitions\n");
20955714Skris		BIO_printf(bio_err," -strparse offset\n");
21055714Skris		BIO_printf(bio_err,"               a series of these can be used to 'dig' into multiple\n");
21155714Skris		BIO_printf(bio_err,"               ASN1 blob wrappings\n");
212160814Ssimon		BIO_printf(bio_err," -genstr str   string to generate ASN1 structure from\n");
213160814Ssimon		BIO_printf(bio_err," -genconf file file to generate ASN1 structure from\n");
21455714Skris		goto end;
21555714Skris		}
21655714Skris
21755714Skris	ERR_load_crypto_strings();
21855714Skris
21955714Skris	in=BIO_new(BIO_s_file());
22055714Skris	out=BIO_new(BIO_s_file());
22155714Skris	if ((in == NULL) || (out == NULL))
22255714Skris		{
22355714Skris		ERR_print_errors(bio_err);
22455714Skris		goto end;
22555714Skris		}
22655714Skris	BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
227109998Smarkm#ifdef OPENSSL_SYS_VMS
22868651Skris	{
22968651Skris	BIO *tmpbio = BIO_new(BIO_f_linebuffer());
23068651Skris	out = BIO_push(tmpbio, out);
23168651Skris	}
23268651Skris#endif
23355714Skris
23455714Skris	if (oidfile != NULL)
23555714Skris		{
23655714Skris		if (BIO_read_filename(in,oidfile) <= 0)
23755714Skris			{
23855714Skris			BIO_printf(bio_err,"problems opening %s\n",oidfile);
23955714Skris			ERR_print_errors(bio_err);
24055714Skris			goto end;
24155714Skris			}
24255714Skris		OBJ_create_objects(in);
24355714Skris		}
24455714Skris
24555714Skris	if (infile == NULL)
24655714Skris		BIO_set_fp(in,stdin,BIO_NOCLOSE);
24755714Skris	else
24855714Skris		{
24955714Skris		if (BIO_read_filename(in,infile) <= 0)
25055714Skris			{
25155714Skris			perror(infile);
25255714Skris			goto end;
25355714Skris			}
25455714Skris		}
25555714Skris
25655714Skris	if (derfile) {
25755714Skris		if(!(derout = BIO_new_file(derfile, "wb"))) {
25855714Skris			BIO_printf(bio_err,"problems opening %s\n",derfile);
25955714Skris			ERR_print_errors(bio_err);
26055714Skris			goto end;
26155714Skris		}
26255714Skris	}
26355714Skris
26455714Skris	if ((buf=BUF_MEM_new()) == NULL) goto end;
26555714Skris	if (!BUF_MEM_grow(buf,BUFSIZ*8)) goto end; /* Pre-allocate :-) */
26655714Skris
267160814Ssimon	if (genstr || genconf)
26855714Skris		{
269160814Ssimon		num = do_generate(bio_err, genstr, genconf, buf);
270160814Ssimon		if (num < 0)
271160814Ssimon			{
272160814Ssimon			ERR_print_errors(bio_err);
27355714Skris			goto end;
274160814Ssimon			}
27555714Skris		}
27655714Skris
277160814Ssimon	else
27855714Skris		{
279160814Ssimon
280160814Ssimon		if (informat == FORMAT_PEM)
281160814Ssimon			{
282160814Ssimon			BIO *tmp;
283160814Ssimon
284160814Ssimon			if ((b64=BIO_new(BIO_f_base64())) == NULL)
285160814Ssimon				goto end;
286160814Ssimon			BIO_push(b64,in);
287160814Ssimon			tmp=in;
288160814Ssimon			in=b64;
289160814Ssimon			b64=tmp;
290160814Ssimon			}
291160814Ssimon
292160814Ssimon		num=0;
293160814Ssimon		for (;;)
294160814Ssimon			{
295160814Ssimon			if (!BUF_MEM_grow(buf,(int)num+BUFSIZ)) goto end;
296160814Ssimon			i=BIO_read(in,&(buf->data[num]),BUFSIZ);
297160814Ssimon			if (i <= 0) break;
298160814Ssimon			num+=i;
299160814Ssimon			}
30055714Skris		}
30155714Skris	str=buf->data;
30255714Skris
30355714Skris	/* If any structs to parse go through in sequence */
30455714Skris
305238405Sjkim	if (sk_OPENSSL_STRING_num(osk))
30655714Skris		{
30755714Skris		tmpbuf=(unsigned char *)str;
30855714Skris		tmplen=num;
309238405Sjkim		for (i=0; i<sk_OPENSSL_STRING_num(osk); i++)
31055714Skris			{
311160814Ssimon			ASN1_TYPE *atmp;
312142425Snectar			int typ;
313238405Sjkim			j=atoi(sk_OPENSSL_STRING_value(osk,i));
31455714Skris			if (j == 0)
31555714Skris				{
316238405Sjkim				BIO_printf(bio_err,"'%s' is an invalid number\n",sk_OPENSSL_STRING_value(osk,i));
31755714Skris				continue;
31855714Skris				}
31955714Skris			tmpbuf+=j;
32055714Skris			tmplen-=j;
32155714Skris			atmp = at;
322160814Ssimon			ctmpbuf = tmpbuf;
323160814Ssimon			at = d2i_ASN1_TYPE(NULL,&ctmpbuf,tmplen);
32455714Skris			ASN1_TYPE_free(atmp);
32555714Skris			if(!at)
32655714Skris				{
32755714Skris				BIO_printf(bio_err,"Error parsing structure\n");
32855714Skris				ERR_print_errors(bio_err);
32955714Skris				goto end;
33055714Skris				}
331142425Snectar			typ = ASN1_TYPE_get(at);
332142425Snectar			if ((typ == V_ASN1_OBJECT)
333142425Snectar				|| (typ == V_ASN1_NULL))
334142425Snectar				{
335142425Snectar				BIO_printf(bio_err, "Can't parse %s type\n",
336142425Snectar					typ == V_ASN1_NULL ? "NULL" : "OBJECT");
337142425Snectar				ERR_print_errors(bio_err);
338142425Snectar				goto end;
339142425Snectar				}
34055714Skris			/* hmm... this is a little evil but it works */
34155714Skris			tmpbuf=at->value.asn1_string->data;
34255714Skris			tmplen=at->value.asn1_string->length;
34355714Skris			}
34455714Skris		str=(char *)tmpbuf;
34555714Skris		num=tmplen;
34655714Skris		}
34755714Skris
348127128Snectar	if (offset >= num)
349127128Snectar		{
350127128Snectar		BIO_printf(bio_err, "Error: offset too large\n");
351127128Snectar		goto end;
352127128Snectar		}
353127128Snectar
354127128Snectar	num -= offset;
355127128Snectar
356127128Snectar	if ((length == 0) || ((long)length > num)) length=(unsigned int)num;
35755714Skris	if(derout) {
35855714Skris		if(BIO_write(derout, str + offset, length) != (int)length) {
35955714Skris			BIO_printf(bio_err, "Error writing output\n");
36055714Skris			ERR_print_errors(bio_err);
36155714Skris			goto end;
36255714Skris		}
36355714Skris	}
36459191Skris	if (!noout &&
36568651Skris	    !ASN1_parse_dump(out,(unsigned char *)&(str[offset]),length,
36668651Skris		    indent,dump))
36755714Skris		{
36855714Skris		ERR_print_errors(bio_err);
36955714Skris		goto end;
37055714Skris		}
37155714Skris	ret=0;
37255714Skrisend:
37355714Skris	BIO_free(derout);
37455714Skris	if (in != NULL) BIO_free(in);
37568651Skris	if (out != NULL) BIO_free_all(out);
37655714Skris	if (b64 != NULL) BIO_free(b64);
37755714Skris	if (ret != 0)
37855714Skris		ERR_print_errors(bio_err);
37955714Skris	if (buf != NULL) BUF_MEM_free(buf);
38055714Skris	if (at != NULL) ASN1_TYPE_free(at);
381238405Sjkim	if (osk != NULL) sk_OPENSSL_STRING_free(osk);
38255714Skris	OBJ_cleanup();
383109998Smarkm	apps_shutdown();
384109998Smarkm	OPENSSL_EXIT(ret);
38555714Skris	}
38655714Skris
387160814Ssimonstatic int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf)
388160814Ssimon	{
389160814Ssimon	CONF *cnf = NULL;
390160814Ssimon	int len;
391160814Ssimon	long errline;
392160814Ssimon	unsigned char *p;
393160814Ssimon	ASN1_TYPE *atyp = NULL;
394160814Ssimon
395160814Ssimon	if (genconf)
396160814Ssimon		{
397160814Ssimon		cnf = NCONF_new(NULL);
398160814Ssimon		if (!NCONF_load(cnf, genconf, &errline))
399160814Ssimon			goto conferr;
400160814Ssimon		if (!genstr)
401160814Ssimon			genstr = NCONF_get_string(cnf, "default", "asn1");
402160814Ssimon		if (!genstr)
403160814Ssimon			{
404160814Ssimon			BIO_printf(bio, "Can't find 'asn1' in '%s'\n", genconf);
405160814Ssimon			goto err;
406160814Ssimon			}
407160814Ssimon		}
408160814Ssimon
409160814Ssimon	atyp = ASN1_generate_nconf(genstr, cnf);
410160814Ssimon	NCONF_free(cnf);
411237657Sjkim	cnf = NULL;
412160814Ssimon
413160814Ssimon	if (!atyp)
414160814Ssimon		return -1;
415160814Ssimon
416160814Ssimon	len = i2d_ASN1_TYPE(atyp, NULL);
417160814Ssimon
418160814Ssimon	if (len <= 0)
419160814Ssimon		goto err;
420160814Ssimon
421160814Ssimon	if (!BUF_MEM_grow(buf,len))
422160814Ssimon		goto err;
423160814Ssimon
424160814Ssimon	p=(unsigned char *)buf->data;
425160814Ssimon
426160814Ssimon	i2d_ASN1_TYPE(atyp, &p);
427160814Ssimon
428160814Ssimon	ASN1_TYPE_free(atyp);
429160814Ssimon	return len;
430160814Ssimon
431160814Ssimon	conferr:
432160814Ssimon
433160814Ssimon	if (errline > 0)
434160814Ssimon		BIO_printf(bio, "Error on line %ld of config file '%s'\n",
435160814Ssimon							errline, genconf);
436160814Ssimon	else
437160814Ssimon		BIO_printf(bio, "Error loading config file '%s'\n", genconf);
438160814Ssimon
439160814Ssimon	err:
440160814Ssimon	NCONF_free(cnf);
441160814Ssimon	ASN1_TYPE_free(atyp);
442160814Ssimon
443160814Ssimon	return -1;
444160814Ssimon
445160814Ssimon	}
446