]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
6738bf14 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
0f113f3e | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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[] = { |
5388f986 | 30 | OPT_SECTION("General"), |
7e1b7485 | 31 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
32 | {"oid", OPT_OID, '<', "file of extra oid definitions"}, |
33 | ||
34 | OPT_SECTION("I/O"), | |
7e1b7485 RS |
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)"}, | |
16e1b281 | 38 | {"noout", OPT_NOOUT, 0, "do not produce any output"}, |
7e1b7485 RS |
39 | {"offset", OPT_OFFSET, 'p', "offset into file"}, |
40 | {"length", OPT_LENGTH, 'p', "length of section in file"}, | |
b998745a | 41 | {"strparse", OPT_STRPARSE, 'p', |
7e1b7485 | 42 | "offset; a series of these can be used to 'dig'"}, |
7e1b7485 | 43 | {"genstr", OPT_GENSTR, 's', "string to generate ASN1 structure from"}, |
5388f986 | 44 | {OPT_MORE_STR, 0, 0, "into multiple ASN1 blob wrappings"}, |
7e1b7485 | 45 | {"genconf", OPT_GENCONF, 's', "file to generate ASN1 structure from"}, |
7e1b7485 RS |
46 | {"strictpem", OPT_STRICTPEM, 0, |
47 | "do not attempt base64 decode outside PEM markers"}, | |
5fb10059 | 48 | {"item", OPT_ITEM, 's', "item to parse and print"}, |
5388f986 RS |
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"}, | |
7e1b7485 RS |
56 | {NULL} |
57 | }; | |
667ac4ec | 58 | |
cc696296 | 59 | static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf); |
9ea1b878 | 60 | |
7e1b7485 | 61 | int asn1parse_main(int argc, char **argv) |
0f113f3e | 62 | { |
7e1b7485 RS |
63 | ASN1_TYPE *at = NULL; |
64 | BIO *in = NULL, *b64 = NULL, *derout = NULL; | |
0f113f3e MC |
65 | BUF_MEM *buf = NULL; |
66 | STACK_OF(OPENSSL_STRING) *osk = NULL; | |
7e1b7485 | 67 | char *genstr = NULL, *genconf = NULL; |
d012c1a1 MC |
68 | char *infile = NULL, *oidfile = NULL, *derfile = NULL; |
69 | unsigned char *str = NULL; | |
7e1b7485 RS |
70 | char *name = NULL, *header = NULL, *prog; |
71 | const unsigned char *ctmpbuf; | |
72 | int indent = 0, noout = 0, dump = 0, strictpem = 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; | |
5fb10059 | 78 | const ASN1_ITEM *it = NULL; |
0f113f3e | 79 | |
7e1b7485 | 80 | prog = opt_init(argc, argv, asn1parse_options); |
0f113f3e | 81 | |
0f113f3e | 82 | if ((osk = sk_OPENSSL_STRING_new_null()) == NULL) { |
7e1b7485 | 83 | BIO_printf(bio_err, "%s: Memory allocation failure\n", prog); |
0f113f3e MC |
84 | goto end; |
85 | } | |
7e1b7485 RS |
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_PEMDER, &informat)) | |
100 | goto opthelp; | |
2d4deb25 | 101 | break; |
7e1b7485 RS |
102 | case OPT_IN: |
103 | infile = opt_arg(); | |
104 | break; | |
105 | case OPT_OUT: | |
106 | derfile = opt_arg(); | |
107 | break; | |
108 | case OPT_INDENT: | |
0f113f3e | 109 | indent = 1; |
7e1b7485 RS |
110 | break; |
111 | case OPT_NOOUT: | |
0f113f3e | 112 | noout = 1; |
7e1b7485 RS |
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: | |
18ada952 | 121 | length = strtol(opt_arg(), NULL, 0); |
7e1b7485 RS |
122 | break; |
123 | case OPT_DUMP: | |
0f113f3e | 124 | dump = -1; |
7e1b7485 RS |
125 | break; |
126 | case OPT_DLIMIT: | |
18ada952 | 127 | dump = strtol(opt_arg(), NULL, 0); |
7e1b7485 RS |
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: | |
0f113f3e MC |
139 | strictpem = 1; |
140 | informat = FORMAT_PEM; | |
0f113f3e | 141 | break; |
5fb10059 DSH |
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; | |
0f113f3e | 158 | } |
0f113f3e | 159 | } |
7e1b7485 | 160 | argc = opt_num_rest(); |
03358517 KR |
161 | if (argc != 0) |
162 | goto opthelp; | |
58964a49 | 163 | |
0f113f3e | 164 | if (oidfile != NULL) { |
bdd58d98 | 165 | in = bio_open_default(oidfile, 'r', FORMAT_TEXT); |
7e1b7485 | 166 | if (in == NULL) |
0f113f3e | 167 | goto end; |
0f113f3e | 168 | OBJ_create_objects(in); |
7e1b7485 | 169 | BIO_free(in); |
0f113f3e MC |
170 | } |
171 | ||
bdd58d98 | 172 | if ((in = bio_open_default(infile, 'r', informat)) == NULL) |
7e1b7485 | 173 | goto end; |
0f113f3e | 174 | |
bdd58d98 | 175 | if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL) |
7e1b7485 | 176 | goto end; |
0f113f3e | 177 | |
4f29f3a2 RL |
178 | if ((buf = BUF_MEM_new()) == NULL) |
179 | goto end; | |
0f113f3e | 180 | if (strictpem) { |
4f29f3a2 | 181 | if (PEM_read_bio(in, &name, &header, &str, &num) != 1) { |
0f113f3e MC |
182 | BIO_printf(bio_err, "Error reading PEM file\n"); |
183 | ERR_print_errors(bio_err); | |
184 | goto end; | |
185 | } | |
4f29f3a2 RL |
186 | buf->data = (char *)str; |
187 | buf->length = buf->max = num; | |
0f113f3e | 188 | } else { |
0f113f3e MC |
189 | if (!BUF_MEM_grow(buf, BUFSIZ * 8)) |
190 | goto end; /* Pre-allocate :-) */ | |
191 | ||
192 | if (genstr || genconf) { | |
ecf3a1fb | 193 | num = do_generate(genstr, genconf, buf); |
0f113f3e MC |
194 | if (num < 0) { |
195 | ERR_print_errors(bio_err); | |
196 | goto end; | |
197 | } | |
2234212c | 198 | } else { |
0f113f3e MC |
199 | |
200 | if (informat == FORMAT_PEM) { | |
201 | BIO *tmp; | |
202 | ||
203 | if ((b64 = BIO_new(BIO_f_base64())) == NULL) | |
204 | goto end; | |
205 | BIO_push(b64, in); | |
206 | tmp = in; | |
207 | in = b64; | |
208 | b64 = tmp; | |
209 | } | |
210 | ||
211 | num = 0; | |
212 | for (;;) { | |
21db0e1a | 213 | if (!BUF_MEM_grow(buf, num + BUFSIZ)) |
0f113f3e MC |
214 | goto end; |
215 | i = BIO_read(in, &(buf->data[num]), BUFSIZ); | |
216 | if (i <= 0) | |
217 | break; | |
218 | num += i; | |
219 | } | |
220 | } | |
d012c1a1 | 221 | str = (unsigned char *)buf->data; |
0f113f3e MC |
222 | |
223 | } | |
224 | ||
225 | /* If any structs to parse go through in sequence */ | |
226 | ||
227 | if (sk_OPENSSL_STRING_num(osk)) { | |
d012c1a1 | 228 | tmpbuf = str; |
0f113f3e MC |
229 | tmplen = num; |
230 | for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) { | |
231 | ASN1_TYPE *atmp; | |
232 | int typ; | |
18ada952 | 233 | j = strtol(sk_OPENSSL_STRING_value(osk, i), NULL, 0); |
752837e0 | 234 | if (j <= 0 || j >= tmplen) { |
b998745a | 235 | BIO_printf(bio_err, "'%s' is out of range\n", |
0f113f3e MC |
236 | sk_OPENSSL_STRING_value(osk, i)); |
237 | continue; | |
238 | } | |
239 | tmpbuf += j; | |
240 | tmplen -= j; | |
241 | atmp = at; | |
242 | ctmpbuf = tmpbuf; | |
243 | at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen); | |
244 | ASN1_TYPE_free(atmp); | |
245 | if (!at) { | |
246 | BIO_printf(bio_err, "Error parsing structure\n"); | |
247 | ERR_print_errors(bio_err); | |
248 | goto end; | |
249 | } | |
250 | typ = ASN1_TYPE_get(at); | |
251 | if ((typ == V_ASN1_OBJECT) | |
e58c4d3c | 252 | || (typ == V_ASN1_BOOLEAN) |
0f113f3e | 253 | || (typ == V_ASN1_NULL)) { |
e58c4d3c | 254 | BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ)); |
0f113f3e MC |
255 | ERR_print_errors(bio_err); |
256 | goto end; | |
257 | } | |
258 | /* hmm... this is a little evil but it works */ | |
259 | tmpbuf = at->value.asn1_string->data; | |
260 | tmplen = at->value.asn1_string->length; | |
261 | } | |
d012c1a1 | 262 | str = tmpbuf; |
0f113f3e MC |
263 | num = tmplen; |
264 | } | |
265 | ||
16e1eea6 | 266 | if (offset < 0 || offset >= num) { |
1518c55a | 267 | BIO_printf(bio_err, "Error: offset out of range\n"); |
0f113f3e MC |
268 | goto end; |
269 | } | |
270 | ||
271 | num -= offset; | |
272 | ||
16e1eea6 | 273 | if (length == 0 || length > (unsigned int)num) |
0f113f3e | 274 | length = (unsigned int)num; |
2234212c | 275 | if (derout != NULL) { |
0f113f3e MC |
276 | if (BIO_write(derout, str + offset, length) != (int)length) { |
277 | BIO_printf(bio_err, "Error writing output\n"); | |
278 | ERR_print_errors(bio_err); | |
279 | goto end; | |
280 | } | |
281 | } | |
5fb10059 DSH |
282 | if (!noout) { |
283 | const unsigned char *p = str + offset; | |
284 | ||
285 | if (it != NULL) { | |
286 | ASN1_VALUE *value = ASN1_item_d2i(NULL, &p, length, it); | |
287 | if (value == NULL) { | |
288 | BIO_printf(bio_err, "Error parsing item %s\n", it->sname); | |
289 | ERR_print_errors(bio_err); | |
290 | goto end; | |
291 | } | |
292 | ASN1_item_print(bio_out, value, 0, it, NULL); | |
293 | ASN1_item_free(value, it); | |
294 | } else { | |
295 | if (!ASN1_parse_dump(bio_out, p, length, indent, dump)) { | |
296 | ERR_print_errors(bio_err); | |
297 | goto end; | |
298 | } | |
299 | } | |
0f113f3e MC |
300 | } |
301 | ret = 0; | |
302 | end: | |
303 | BIO_free(derout); | |
ca3a82c3 | 304 | BIO_free(in); |
ca3a82c3 | 305 | BIO_free(b64); |
0f113f3e MC |
306 | if (ret != 0) |
307 | ERR_print_errors(bio_err); | |
25aaa98a | 308 | BUF_MEM_free(buf); |
b548a1f1 RS |
309 | OPENSSL_free(name); |
310 | OPENSSL_free(header); | |
2ace7450 | 311 | ASN1_TYPE_free(at); |
25aaa98a | 312 | sk_OPENSSL_STRING_free(osk); |
26a7d938 | 313 | return ret; |
0f113f3e | 314 | } |
d02b48c6 | 315 | |
cc696296 | 316 | static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf) |
0f113f3e MC |
317 | { |
318 | CONF *cnf = NULL; | |
319 | int len; | |
0f113f3e MC |
320 | unsigned char *p; |
321 | ASN1_TYPE *atyp = NULL; | |
9ea1b878 | 322 | |
2234212c | 323 | if (genconf != NULL) { |
cc01d217 RS |
324 | if ((cnf = app_load_config(genconf)) == NULL) |
325 | goto err; | |
2234212c | 326 | if (genstr == NULL) |
0f113f3e | 327 | genstr = NCONF_get_string(cnf, "default", "asn1"); |
2234212c | 328 | if (genstr == NULL) { |
ecf3a1fb | 329 | BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf); |
0f113f3e MC |
330 | goto err; |
331 | } | |
332 | } | |
9ea1b878 | 333 | |
0f113f3e MC |
334 | atyp = ASN1_generate_nconf(genstr, cnf); |
335 | NCONF_free(cnf); | |
336 | cnf = NULL; | |
9ea1b878 | 337 | |
2234212c | 338 | if (atyp == NULL) |
0f113f3e | 339 | return -1; |
9ea1b878 | 340 | |
0f113f3e | 341 | len = i2d_ASN1_TYPE(atyp, NULL); |
9ea1b878 | 342 | |
0f113f3e MC |
343 | if (len <= 0) |
344 | goto err; | |
9ea1b878 | 345 | |
0f113f3e MC |
346 | if (!BUF_MEM_grow(buf, len)) |
347 | goto err; | |
9ea1b878 | 348 | |
0f113f3e | 349 | p = (unsigned char *)buf->data; |
9ea1b878 | 350 | |
0f113f3e | 351 | i2d_ASN1_TYPE(atyp, &p); |
9ea1b878 | 352 | |
0f113f3e MC |
353 | ASN1_TYPE_free(atyp); |
354 | return len; | |
9ea1b878 | 355 | |
0f113f3e MC |
356 | err: |
357 | NCONF_free(cnf); | |
358 | ASN1_TYPE_free(atyp); | |
0f113f3e | 359 | return -1; |
0f113f3e | 360 | } |