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