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