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