Avoid memory leaks if options repeated.
[openssl.git] / apps / asn1pars.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * A nice addition from Dr Stephen Henson <steve@openssl.org> to add the
12  * -strparse option which parses nested binary structures
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "apps.h"
19 #include <openssl/err.h>
20 #include <openssl/evp.h>
21 #include <openssl/x509.h>
22 #include <openssl/pem.h>
23
24 typedef enum OPTION_choice {
25     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
26     OPT_INFORM, OPT_IN, OPT_OUT, OPT_INDENT, OPT_NOOUT,
27     OPT_OID, OPT_OFFSET, OPT_LENGTH, OPT_DUMP, OPT_DLIMIT,
28     OPT_STRPARSE, OPT_GENSTR, OPT_GENCONF, OPT_STRICTPEM
29 } OPTION_CHOICE;
30
31 OPTIONS asn1parse_options[] = {
32     {"help", OPT_HELP, '-', "Display this summary"},
33     {"inform", OPT_INFORM, 'F', "input format - one of DER PEM"},
34     {"in", OPT_IN, '<', "input file"},
35     {"out", OPT_OUT, '>', "output file (output format is always DER)"},
36     {"i", OPT_INDENT, 0, "indents the output"},
37     {"noout", OPT_NOOUT, 0, "do not produce any output"},
38     {"offset", OPT_OFFSET, 'p', "offset into file"},
39     {"length", OPT_LENGTH, 'p', "length of section in file"},
40     {"oid", OPT_OID, '<', "file of extra oid definitions"},
41     {"dump", OPT_DUMP, 0, "unknown data in hex form"},
42     {"dlimit", OPT_DLIMIT, 'p',
43      "dump the first arg bytes of unknown data in hex form"},
44     {"strparse", OPT_STRPARSE, 's',
45      "offset; a series of these can be used to 'dig'"},
46     {OPT_MORE_STR, 0, 0, "into multiple ASN1 blob wrappings"},
47     {"genstr", OPT_GENSTR, 's', "string to generate ASN1 structure from"},
48     {"genconf", OPT_GENCONF, 's', "file to generate ASN1 structure from"},
49     {OPT_MORE_STR, 0, 0, "(-inform  will be ignored)"},
50     {"strictpem", OPT_STRICTPEM, 0,
51      "do not attempt base64 decode outside PEM markers"},
52     {NULL}
53 };
54
55 static int do_generate(char *genstr, char *genconf, BUF_MEM *buf);
56
57 int asn1parse_main(int argc, char **argv)
58 {
59     ASN1_TYPE *at = NULL;
60     BIO *in = NULL, *b64 = NULL, *derout = NULL;
61     BUF_MEM *buf = NULL;
62     STACK_OF(OPENSSL_STRING) *osk = NULL;
63     char *genstr = NULL, *genconf = NULL;
64     char *infile = NULL, *str = NULL, *oidfile = NULL, *derfile = NULL;
65     char *name = NULL, *header = NULL, *prog;
66     const unsigned char *ctmpbuf;
67     int indent = 0, noout = 0, dump = 0, strictpem = 0, informat = FORMAT_PEM;
68     int offset = 0, ret = 1, i, j;
69     long num, tmplen;
70     unsigned char *tmpbuf;
71     unsigned int length = 0;
72     OPTION_CHOICE o;
73
74     prog = opt_init(argc, argv, asn1parse_options);
75
76     if ((osk = sk_OPENSSL_STRING_new_null()) == NULL) {
77         BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
78         goto end;
79     }
80
81     while ((o = opt_next()) != OPT_EOF) {
82         switch (o) {
83         case OPT_EOF:
84         case OPT_ERR:
85  opthelp:
86             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
87             goto end;
88         case OPT_HELP:
89             opt_help(asn1parse_options);
90             ret = 0;
91             goto end;
92         case OPT_INFORM:
93             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
94                 goto opthelp;
95             break;
96         case OPT_IN:
97             infile = opt_arg();
98             break;
99         case OPT_OUT:
100             derfile = opt_arg();
101             break;
102         case OPT_INDENT:
103             indent = 1;
104             break;
105         case OPT_NOOUT:
106             noout = 1;
107             break;
108         case OPT_OID:
109             oidfile = opt_arg();
110             break;
111         case OPT_OFFSET:
112             offset = strtol(opt_arg(), NULL, 0);
113             break;
114         case OPT_LENGTH:
115             length = atoi(opt_arg());
116             break;
117         case OPT_DUMP:
118             dump = -1;
119             break;
120         case OPT_DLIMIT:
121             dump = atoi(opt_arg());
122             break;
123         case OPT_STRPARSE:
124             sk_OPENSSL_STRING_push(osk, opt_arg());
125             break;
126         case OPT_GENSTR:
127             genstr = opt_arg();
128             break;
129         case OPT_GENCONF:
130             genconf = opt_arg();
131             break;
132         case OPT_STRICTPEM:
133             strictpem = 1;
134             informat = FORMAT_PEM;
135             break;
136         }
137     }
138     argc = opt_num_rest();
139     if (argc != 0)
140         goto opthelp;
141
142     if (oidfile != NULL) {
143         in = bio_open_default(oidfile, 'r', FORMAT_TEXT);
144         if (in == NULL)
145             goto end;
146         OBJ_create_objects(in);
147         BIO_free(in);
148     }
149
150     if ((in = bio_open_default(infile, 'r', informat)) == NULL)
151         goto end;
152
153     if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL)
154         goto end;
155
156     if (strictpem) {
157         if (PEM_read_bio(in, &name, &header, (unsigned char **)&str, &num) !=
158             1) {
159             BIO_printf(bio_err, "Error reading PEM file\n");
160             ERR_print_errors(bio_err);
161             goto end;
162         }
163     } else {
164
165         if ((buf = BUF_MEM_new()) == NULL)
166             goto end;
167         if (!BUF_MEM_grow(buf, BUFSIZ * 8))
168             goto end;           /* Pre-allocate :-) */
169
170         if (genstr || genconf) {
171             num = do_generate(genstr, genconf, buf);
172             if (num < 0) {
173                 ERR_print_errors(bio_err);
174                 goto end;
175             }
176         }
177
178         else {
179
180             if (informat == FORMAT_PEM) {
181                 BIO *tmp;
182
183                 if ((b64 = BIO_new(BIO_f_base64())) == NULL)
184                     goto end;
185                 BIO_push(b64, in);
186                 tmp = in;
187                 in = b64;
188                 b64 = tmp;
189             }
190
191             num = 0;
192             for (;;) {
193                 if (!BUF_MEM_grow(buf, (int)num + BUFSIZ))
194                     goto end;
195                 i = BIO_read(in, &(buf->data[num]), BUFSIZ);
196                 if (i <= 0)
197                     break;
198                 num += i;
199             }
200         }
201         str = buf->data;
202
203     }
204
205     /* If any structs to parse go through in sequence */
206
207     if (sk_OPENSSL_STRING_num(osk)) {
208         tmpbuf = (unsigned char *)str;
209         tmplen = num;
210         for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) {
211             ASN1_TYPE *atmp;
212             int typ;
213             j = atoi(sk_OPENSSL_STRING_value(osk, i));
214             if (j == 0) {
215                 BIO_printf(bio_err, "'%s' is an invalid number\n",
216                            sk_OPENSSL_STRING_value(osk, i));
217                 continue;
218             }
219             tmpbuf += j;
220             tmplen -= j;
221             atmp = at;
222             ctmpbuf = tmpbuf;
223             at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen);
224             ASN1_TYPE_free(atmp);
225             if (!at) {
226                 BIO_printf(bio_err, "Error parsing structure\n");
227                 ERR_print_errors(bio_err);
228                 goto end;
229             }
230             typ = ASN1_TYPE_get(at);
231             if ((typ == V_ASN1_OBJECT)
232                 || (typ == V_ASN1_BOOLEAN)
233                 || (typ == V_ASN1_NULL)) {
234                 BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ));
235                 ERR_print_errors(bio_err);
236                 goto end;
237             }
238             /* hmm... this is a little evil but it works */
239             tmpbuf = at->value.asn1_string->data;
240             tmplen = at->value.asn1_string->length;
241         }
242         str = (char *)tmpbuf;
243         num = tmplen;
244     }
245
246     if (offset >= num) {
247         BIO_printf(bio_err, "Error: offset too large\n");
248         goto end;
249     }
250
251     num -= offset;
252
253     if ((length == 0) || ((long)length > num))
254         length = (unsigned int)num;
255     if (derout) {
256         if (BIO_write(derout, str + offset, length) != (int)length) {
257             BIO_printf(bio_err, "Error writing output\n");
258             ERR_print_errors(bio_err);
259             goto end;
260         }
261     }
262     if (!noout &&
263         !ASN1_parse_dump(bio_out, (unsigned char *)&(str[offset]), length,
264                          indent, dump)) {
265         ERR_print_errors(bio_err);
266         goto end;
267     }
268     ret = 0;
269  end:
270     BIO_free(derout);
271     BIO_free(in);
272     BIO_free(b64);
273     if (ret != 0)
274         ERR_print_errors(bio_err);
275     BUF_MEM_free(buf);
276     OPENSSL_free(name);
277     OPENSSL_free(header);
278     if (strictpem)
279         OPENSSL_free(str);
280     ASN1_TYPE_free(at);
281     sk_OPENSSL_STRING_free(osk);
282     return (ret);
283 }
284
285 static int do_generate(char *genstr, char *genconf, BUF_MEM *buf)
286 {
287     CONF *cnf = NULL;
288     int len;
289     unsigned char *p;
290     ASN1_TYPE *atyp = NULL;
291
292     if (genconf) {
293         if ((cnf = app_load_config(genconf)) == NULL)
294             goto err;
295         if (!genstr)
296             genstr = NCONF_get_string(cnf, "default", "asn1");
297         if (!genstr) {
298             BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf);
299             goto err;
300         }
301     }
302
303     atyp = ASN1_generate_nconf(genstr, cnf);
304     NCONF_free(cnf);
305     cnf = NULL;
306
307     if (!atyp)
308         return -1;
309
310     len = i2d_ASN1_TYPE(atyp, NULL);
311
312     if (len <= 0)
313         goto err;
314
315     if (!BUF_MEM_grow(buf, len))
316         goto err;
317
318     p = (unsigned char *)buf->data;
319
320     i2d_ASN1_TYPE(atyp, &p);
321
322     ASN1_TYPE_free(atyp);
323     return len;
324
325  err:
326     NCONF_free(cnf);
327     ASN1_TYPE_free(atyp);
328     return -1;
329 }