10Sstevel@tonic-gate /* apps/asn1pars.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate /* A nice addition from Dr Stephen Henson <shenson@bigfoot.com> to
600Sstevel@tonic-gate * add the -strparse option which parses nested binary structures
610Sstevel@tonic-gate */
620Sstevel@tonic-gate
630Sstevel@tonic-gate #include <stdio.h>
640Sstevel@tonic-gate #include <stdlib.h>
650Sstevel@tonic-gate #include <string.h>
660Sstevel@tonic-gate #include "apps.h"
670Sstevel@tonic-gate #include <openssl/err.h>
680Sstevel@tonic-gate #include <openssl/evp.h>
690Sstevel@tonic-gate #include <openssl/x509.h>
700Sstevel@tonic-gate #include <openssl/pem.h>
710Sstevel@tonic-gate
720Sstevel@tonic-gate /* -inform arg - input format - default PEM (DER or PEM)
730Sstevel@tonic-gate * -in arg - input file - default stdin
740Sstevel@tonic-gate * -i - indent the details by depth
750Sstevel@tonic-gate * -offset - where in the file to start
760Sstevel@tonic-gate * -length - how many bytes to use
770Sstevel@tonic-gate * -oid file - extra oid description file
780Sstevel@tonic-gate */
790Sstevel@tonic-gate
800Sstevel@tonic-gate #undef PROG
810Sstevel@tonic-gate #define PROG asn1parse_main
820Sstevel@tonic-gate
830Sstevel@tonic-gate int MAIN(int, char **);
840Sstevel@tonic-gate
85*2139Sjp161948 static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf);
86*2139Sjp161948
MAIN(int argc,char ** argv)870Sstevel@tonic-gate int MAIN(int argc, char **argv)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate int i,badops=0,offset=0,ret=1,j;
900Sstevel@tonic-gate unsigned int length=0;
910Sstevel@tonic-gate long num,tmplen;
920Sstevel@tonic-gate BIO *in=NULL,*out=NULL,*b64=NULL, *derout = NULL;
930Sstevel@tonic-gate int informat,indent=0, noout = 0, dump = 0;
940Sstevel@tonic-gate char *infile=NULL,*str=NULL,*prog,*oidfile=NULL, *derfile=NULL;
95*2139Sjp161948 char *genstr=NULL, *genconf=NULL;
960Sstevel@tonic-gate unsigned char *tmpbuf;
97*2139Sjp161948 const unsigned char *ctmpbuf;
980Sstevel@tonic-gate BUF_MEM *buf=NULL;
990Sstevel@tonic-gate STACK *osk=NULL;
1000Sstevel@tonic-gate ASN1_TYPE *at=NULL;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate informat=FORMAT_PEM;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate apps_startup();
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate if (bio_err == NULL)
1070Sstevel@tonic-gate if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1080Sstevel@tonic-gate BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (!load_config(bio_err, NULL))
1110Sstevel@tonic-gate goto end;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate prog=argv[0];
1140Sstevel@tonic-gate argc--;
1150Sstevel@tonic-gate argv++;
1160Sstevel@tonic-gate if ((osk=sk_new_null()) == NULL)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate BIO_printf(bio_err,"Memory allocation failure\n");
1190Sstevel@tonic-gate goto end;
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate while (argc >= 1)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate if (strcmp(*argv,"-inform") == 0)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate if (--argc < 1) goto bad;
1260Sstevel@tonic-gate informat=str2fmt(*(++argv));
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate else if (strcmp(*argv,"-in") == 0)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate if (--argc < 1) goto bad;
1310Sstevel@tonic-gate infile= *(++argv);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate else if (strcmp(*argv,"-out") == 0)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate if (--argc < 1) goto bad;
1360Sstevel@tonic-gate derfile= *(++argv);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate else if (strcmp(*argv,"-i") == 0)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate indent=1;
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate else if (strcmp(*argv,"-noout") == 0) noout = 1;
1430Sstevel@tonic-gate else if (strcmp(*argv,"-oid") == 0)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate if (--argc < 1) goto bad;
1460Sstevel@tonic-gate oidfile= *(++argv);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate else if (strcmp(*argv,"-offset") == 0)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate if (--argc < 1) goto bad;
1510Sstevel@tonic-gate offset= atoi(*(++argv));
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate else if (strcmp(*argv,"-length") == 0)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate if (--argc < 1) goto bad;
1560Sstevel@tonic-gate length= atoi(*(++argv));
1570Sstevel@tonic-gate if (length == 0) goto bad;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate else if (strcmp(*argv,"-dump") == 0)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate dump= -1;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate else if (strcmp(*argv,"-dlimit") == 0)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate if (--argc < 1) goto bad;
1660Sstevel@tonic-gate dump= atoi(*(++argv));
1670Sstevel@tonic-gate if (dump <= 0) goto bad;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate else if (strcmp(*argv,"-strparse") == 0)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate if (--argc < 1) goto bad;
1720Sstevel@tonic-gate sk_push(osk,*(++argv));
1730Sstevel@tonic-gate }
174*2139Sjp161948 else if (strcmp(*argv,"-genstr") == 0)
175*2139Sjp161948 {
176*2139Sjp161948 if (--argc < 1) goto bad;
177*2139Sjp161948 genstr= *(++argv);
178*2139Sjp161948 }
179*2139Sjp161948 else if (strcmp(*argv,"-genconf") == 0)
180*2139Sjp161948 {
181*2139Sjp161948 if (--argc < 1) goto bad;
182*2139Sjp161948 genconf= *(++argv);
183*2139Sjp161948 }
1840Sstevel@tonic-gate else
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate BIO_printf(bio_err,"unknown option %s\n",*argv);
1870Sstevel@tonic-gate badops=1;
1880Sstevel@tonic-gate break;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate argc--;
1910Sstevel@tonic-gate argv++;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if (badops)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate bad:
1970Sstevel@tonic-gate BIO_printf(bio_err,"%s [options] <infile\n",prog);
1980Sstevel@tonic-gate BIO_printf(bio_err,"where options are\n");
199*2139Sjp161948 BIO_printf(bio_err," -inform arg input format - one of DER PEM\n");
2000Sstevel@tonic-gate BIO_printf(bio_err," -in arg input file\n");
2010Sstevel@tonic-gate BIO_printf(bio_err," -out arg output file (output format is always DER\n");
2020Sstevel@tonic-gate BIO_printf(bio_err," -noout arg don't produce any output\n");
2030Sstevel@tonic-gate BIO_printf(bio_err," -offset arg offset into file\n");
2040Sstevel@tonic-gate BIO_printf(bio_err," -length arg length of section in file\n");
2050Sstevel@tonic-gate BIO_printf(bio_err," -i indent entries\n");
2060Sstevel@tonic-gate BIO_printf(bio_err," -dump dump unknown data in hex form\n");
2070Sstevel@tonic-gate BIO_printf(bio_err," -dlimit arg dump the first arg bytes of unknown data in hex form\n");
2080Sstevel@tonic-gate BIO_printf(bio_err," -oid file file of extra oid definitions\n");
2090Sstevel@tonic-gate BIO_printf(bio_err," -strparse offset\n");
2100Sstevel@tonic-gate BIO_printf(bio_err," a series of these can be used to 'dig' into multiple\n");
2110Sstevel@tonic-gate BIO_printf(bio_err," ASN1 blob wrappings\n");
212*2139Sjp161948 BIO_printf(bio_err," -genstr str string to generate ASN1 structure from\n");
213*2139Sjp161948 BIO_printf(bio_err," -genconf file file to generate ASN1 structure from\n");
2140Sstevel@tonic-gate goto end;
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate ERR_load_crypto_strings();
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate in=BIO_new(BIO_s_file());
2200Sstevel@tonic-gate out=BIO_new(BIO_s_file());
2210Sstevel@tonic-gate if ((in == NULL) || (out == NULL))
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate ERR_print_errors(bio_err);
2240Sstevel@tonic-gate goto end;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
2270Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer());
2300Sstevel@tonic-gate out = BIO_push(tmpbio, out);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate #endif
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate if (oidfile != NULL)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate if (BIO_read_filename(in,oidfile) <= 0)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate BIO_printf(bio_err,"problems opening %s\n",oidfile);
2390Sstevel@tonic-gate ERR_print_errors(bio_err);
2400Sstevel@tonic-gate goto end;
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate OBJ_create_objects(in);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (infile == NULL)
2460Sstevel@tonic-gate BIO_set_fp(in,stdin,BIO_NOCLOSE);
2470Sstevel@tonic-gate else
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate if (BIO_read_filename(in,infile) <= 0)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate perror(infile);
2520Sstevel@tonic-gate goto end;
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate if (derfile) {
2570Sstevel@tonic-gate if(!(derout = BIO_new_file(derfile, "wb"))) {
2580Sstevel@tonic-gate BIO_printf(bio_err,"problems opening %s\n",derfile);
2590Sstevel@tonic-gate ERR_print_errors(bio_err);
2600Sstevel@tonic-gate goto end;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate if ((buf=BUF_MEM_new()) == NULL) goto end;
2650Sstevel@tonic-gate if (!BUF_MEM_grow(buf,BUFSIZ*8)) goto end; /* Pre-allocate :-) */
2660Sstevel@tonic-gate
267*2139Sjp161948 if (genstr || genconf)
2680Sstevel@tonic-gate {
269*2139Sjp161948 num = do_generate(bio_err, genstr, genconf, buf);
270*2139Sjp161948 if (num < 0)
271*2139Sjp161948 {
272*2139Sjp161948 ERR_print_errors(bio_err);
2730Sstevel@tonic-gate goto end;
274*2139Sjp161948 }
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
277*2139Sjp161948 else
2780Sstevel@tonic-gate {
279*2139Sjp161948
280*2139Sjp161948 if (informat == FORMAT_PEM)
281*2139Sjp161948 {
282*2139Sjp161948 BIO *tmp;
283*2139Sjp161948
284*2139Sjp161948 if ((b64=BIO_new(BIO_f_base64())) == NULL)
285*2139Sjp161948 goto end;
286*2139Sjp161948 BIO_push(b64,in);
287*2139Sjp161948 tmp=in;
288*2139Sjp161948 in=b64;
289*2139Sjp161948 b64=tmp;
290*2139Sjp161948 }
291*2139Sjp161948
292*2139Sjp161948 num=0;
293*2139Sjp161948 for (;;)
294*2139Sjp161948 {
295*2139Sjp161948 if (!BUF_MEM_grow(buf,(int)num+BUFSIZ)) goto end;
296*2139Sjp161948 i=BIO_read(in,&(buf->data[num]),BUFSIZ);
297*2139Sjp161948 if (i <= 0) break;
298*2139Sjp161948 num+=i;
299*2139Sjp161948 }
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate str=buf->data;
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate /* If any structs to parse go through in sequence */
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate if (sk_num(osk))
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate tmpbuf=(unsigned char *)str;
3080Sstevel@tonic-gate tmplen=num;
3090Sstevel@tonic-gate for (i=0; i<sk_num(osk); i++)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate ASN1_TYPE *atmp;
312*2139Sjp161948 int typ;
3130Sstevel@tonic-gate j=atoi(sk_value(osk,i));
3140Sstevel@tonic-gate if (j == 0)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate BIO_printf(bio_err,"'%s' is an invalid number\n",sk_value(osk,i));
3170Sstevel@tonic-gate continue;
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate tmpbuf+=j;
3200Sstevel@tonic-gate tmplen-=j;
3210Sstevel@tonic-gate atmp = at;
322*2139Sjp161948 ctmpbuf = tmpbuf;
323*2139Sjp161948 at = d2i_ASN1_TYPE(NULL,&ctmpbuf,tmplen);
3240Sstevel@tonic-gate ASN1_TYPE_free(atmp);
3250Sstevel@tonic-gate if(!at)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate BIO_printf(bio_err,"Error parsing structure\n");
3280Sstevel@tonic-gate ERR_print_errors(bio_err);
3290Sstevel@tonic-gate goto end;
3300Sstevel@tonic-gate }
331*2139Sjp161948 typ = ASN1_TYPE_get(at);
332*2139Sjp161948 if ((typ == V_ASN1_OBJECT)
333*2139Sjp161948 || (typ == V_ASN1_NULL))
334*2139Sjp161948 {
335*2139Sjp161948 BIO_printf(bio_err, "Can't parse %s type\n",
336*2139Sjp161948 typ == V_ASN1_NULL ? "NULL" : "OBJECT");
337*2139Sjp161948 ERR_print_errors(bio_err);
338*2139Sjp161948 goto end;
339*2139Sjp161948 }
3400Sstevel@tonic-gate /* hmm... this is a little evil but it works */
3410Sstevel@tonic-gate tmpbuf=at->value.asn1_string->data;
3420Sstevel@tonic-gate tmplen=at->value.asn1_string->length;
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate str=(char *)tmpbuf;
3450Sstevel@tonic-gate num=tmplen;
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate if (offset >= num)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate BIO_printf(bio_err, "Error: offset too large\n");
3510Sstevel@tonic-gate goto end;
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate num -= offset;
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate if ((length == 0) || ((long)length > num)) length=(unsigned int)num;
3570Sstevel@tonic-gate if(derout) {
3580Sstevel@tonic-gate if(BIO_write(derout, str + offset, length) != (int)length) {
3590Sstevel@tonic-gate BIO_printf(bio_err, "Error writing output\n");
3600Sstevel@tonic-gate ERR_print_errors(bio_err);
3610Sstevel@tonic-gate goto end;
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate if (!noout &&
3650Sstevel@tonic-gate !ASN1_parse_dump(out,(unsigned char *)&(str[offset]),length,
3660Sstevel@tonic-gate indent,dump))
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate ERR_print_errors(bio_err);
3690Sstevel@tonic-gate goto end;
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate ret=0;
3720Sstevel@tonic-gate end:
3730Sstevel@tonic-gate BIO_free(derout);
3740Sstevel@tonic-gate if (in != NULL) BIO_free(in);
3750Sstevel@tonic-gate if (out != NULL) BIO_free_all(out);
3760Sstevel@tonic-gate if (b64 != NULL) BIO_free(b64);
3770Sstevel@tonic-gate if (ret != 0)
3780Sstevel@tonic-gate ERR_print_errors(bio_err);
3790Sstevel@tonic-gate if (buf != NULL) BUF_MEM_free(buf);
3800Sstevel@tonic-gate if (at != NULL) ASN1_TYPE_free(at);
3810Sstevel@tonic-gate if (osk != NULL) sk_free(osk);
3820Sstevel@tonic-gate OBJ_cleanup();
3830Sstevel@tonic-gate apps_shutdown();
3840Sstevel@tonic-gate OPENSSL_EXIT(ret);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate
do_generate(BIO * bio,char * genstr,char * genconf,BUF_MEM * buf)387*2139Sjp161948 static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf)
388*2139Sjp161948 {
389*2139Sjp161948 CONF *cnf = NULL;
390*2139Sjp161948 int len;
391*2139Sjp161948 long errline;
392*2139Sjp161948 unsigned char *p;
393*2139Sjp161948 ASN1_TYPE *atyp = NULL;
394*2139Sjp161948
395*2139Sjp161948 if (genconf)
396*2139Sjp161948 {
397*2139Sjp161948 cnf = NCONF_new(NULL);
398*2139Sjp161948 if (!NCONF_load(cnf, genconf, &errline))
399*2139Sjp161948 goto conferr;
400*2139Sjp161948 if (!genstr)
401*2139Sjp161948 genstr = NCONF_get_string(cnf, "default", "asn1");
402*2139Sjp161948 if (!genstr)
403*2139Sjp161948 {
404*2139Sjp161948 BIO_printf(bio, "Can't find 'asn1' in '%s'\n", genconf);
405*2139Sjp161948 goto err;
406*2139Sjp161948 }
407*2139Sjp161948 }
408*2139Sjp161948
409*2139Sjp161948 atyp = ASN1_generate_nconf(genstr, cnf);
410*2139Sjp161948 NCONF_free(cnf);
411*2139Sjp161948
412*2139Sjp161948 if (!atyp)
413*2139Sjp161948 return -1;
414*2139Sjp161948
415*2139Sjp161948 len = i2d_ASN1_TYPE(atyp, NULL);
416*2139Sjp161948
417*2139Sjp161948 if (len <= 0)
418*2139Sjp161948 goto err;
419*2139Sjp161948
420*2139Sjp161948 if (!BUF_MEM_grow(buf,len))
421*2139Sjp161948 goto err;
422*2139Sjp161948
423*2139Sjp161948 p=(unsigned char *)buf->data;
424*2139Sjp161948
425*2139Sjp161948 i2d_ASN1_TYPE(atyp, &p);
426*2139Sjp161948
427*2139Sjp161948 ASN1_TYPE_free(atyp);
428*2139Sjp161948 return len;
429*2139Sjp161948
430*2139Sjp161948 conferr:
431*2139Sjp161948
432*2139Sjp161948 if (errline > 0)
433*2139Sjp161948 BIO_printf(bio, "Error on line %ld of config file '%s'\n",
434*2139Sjp161948 errline, genconf);
435*2139Sjp161948 else
436*2139Sjp161948 BIO_printf(bio, "Error loading config file '%s'\n", genconf);
437*2139Sjp161948
438*2139Sjp161948 err:
439*2139Sjp161948 NCONF_free(cnf);
440*2139Sjp161948 ASN1_TYPE_free(atyp);
441*2139Sjp161948
442*2139Sjp161948 return -1;
443*2139Sjp161948
444*2139Sjp161948 }
445