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