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