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