]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
2 | * All rights reserved. |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
d02b48c6 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
d02b48c6 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
d02b48c6 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
d02b48c6 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
57 | ||
0f113f3e MC |
58 | /* |
59 | * A nice addition from Dr Stephen Henson <steve@openssl.org> to add the | |
60 | * -strparse option which parses nested binary structures | |
dfeab068 RE |
61 | */ |
62 | ||
d02b48c6 RE |
63 | #include <stdio.h> |
64 | #include <stdlib.h> | |
65 | #include <string.h> | |
66 | #include "apps.h" | |
ec577822 BM |
67 | #include <openssl/err.h> |
68 | #include <openssl/evp.h> | |
69 | #include <openssl/x509.h> | |
70 | #include <openssl/pem.h> | |
d02b48c6 | 71 | |
7e1b7485 RS |
72 | typedef enum OPTION_choice { |
73 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
74 | OPT_INFORM, OPT_IN, OPT_OUT, OPT_INDENT, OPT_NOOUT, | |
75 | OPT_OID, OPT_OFFSET, OPT_LENGTH, OPT_DUMP, OPT_DLIMIT, | |
76 | OPT_STRPARSE, OPT_GENSTR, OPT_GENCONF, OPT_STRICTPEM | |
77 | } OPTION_CHOICE; | |
78 | ||
79 | OPTIONS asn1parse_options[] = { | |
80 | {"help", OPT_HELP, '-', "Display this summary"}, | |
81 | {"inform", OPT_INFORM, 'F', "input format - one of DER PEM"}, | |
82 | {"in", OPT_IN, '<', "input file"}, | |
83 | {"out", OPT_OUT, '>', "output file (output format is always DER)"}, | |
6755ff11 | 84 | {"i", OPT_INDENT, 0, "indents the output"}, |
7e1b7485 RS |
85 | {"noout", OPT_NOOUT, 0, "don't produce any output"}, |
86 | {"offset", OPT_OFFSET, 'p', "offset into file"}, | |
87 | {"length", OPT_LENGTH, 'p', "length of section in file"}, | |
88 | {"oid", OPT_OID, '<', "file of extra oid definitions"}, | |
89 | {"dump", OPT_DUMP, 0, "unknown data in hex form"}, | |
90 | {"dlimit", OPT_DLIMIT, 'p', | |
91 | "dump the first arg bytes of unknown data in hex form"}, | |
92 | {"strparse", OPT_STRPARSE, 's', | |
93 | "offset; a series of these can be used to 'dig'"}, | |
94 | {OPT_MORE_STR, 0, 0, "into multiple ASN1 blob wrappings"}, | |
95 | {"genstr", OPT_GENSTR, 's', "string to generate ASN1 structure from"}, | |
96 | {"genconf", OPT_GENCONF, 's', "file to generate ASN1 structure from"}, | |
97 | {OPT_MORE_STR, 0, 0, "(-inform will be ignored)"}, | |
98 | {"strictpem", OPT_STRICTPEM, 0, | |
99 | "do not attempt base64 decode outside PEM markers"}, | |
100 | {NULL} | |
101 | }; | |
667ac4ec | 102 | |
ecf3a1fb | 103 | static int do_generate(char *genstr, char *genconf, BUF_MEM *buf); |
9ea1b878 | 104 | |
7e1b7485 | 105 | int asn1parse_main(int argc, char **argv) |
0f113f3e | 106 | { |
7e1b7485 RS |
107 | ASN1_TYPE *at = NULL; |
108 | BIO *in = NULL, *b64 = NULL, *derout = NULL; | |
0f113f3e MC |
109 | BUF_MEM *buf = NULL; |
110 | STACK_OF(OPENSSL_STRING) *osk = NULL; | |
7e1b7485 RS |
111 | char *genstr = NULL, *genconf = NULL; |
112 | char *infile = NULL, *str = NULL, *oidfile = NULL, *derfile = NULL; | |
113 | char *name = NULL, *header = NULL, *prog; | |
114 | const unsigned char *ctmpbuf; | |
115 | int indent = 0, noout = 0, dump = 0, strictpem = 0, informat = FORMAT_PEM; | |
116 | int offset = 0, ret = 1, i, j; | |
117 | long num, tmplen; | |
118 | unsigned char *tmpbuf; | |
119 | unsigned int length = 0; | |
120 | OPTION_CHOICE o; | |
0f113f3e | 121 | |
7e1b7485 | 122 | prog = opt_init(argc, argv, asn1parse_options); |
0f113f3e | 123 | |
0f113f3e | 124 | if ((osk = sk_OPENSSL_STRING_new_null()) == NULL) { |
7e1b7485 | 125 | BIO_printf(bio_err, "%s: Memory allocation failure\n", prog); |
0f113f3e MC |
126 | goto end; |
127 | } | |
7e1b7485 RS |
128 | |
129 | while ((o = opt_next()) != OPT_EOF) { | |
130 | switch (o) { | |
131 | case OPT_EOF: | |
132 | case OPT_ERR: | |
133 | opthelp: | |
134 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
135 | goto end; | |
136 | case OPT_HELP: | |
137 | opt_help(asn1parse_options); | |
138 | ret = 0; | |
139 | goto end; | |
140 | case OPT_INFORM: | |
141 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
142 | goto opthelp; | |
2d4deb25 | 143 | break; |
7e1b7485 RS |
144 | case OPT_IN: |
145 | infile = opt_arg(); | |
146 | break; | |
147 | case OPT_OUT: | |
148 | derfile = opt_arg(); | |
149 | break; | |
150 | case OPT_INDENT: | |
0f113f3e | 151 | indent = 1; |
7e1b7485 RS |
152 | break; |
153 | case OPT_NOOUT: | |
0f113f3e | 154 | noout = 1; |
7e1b7485 RS |
155 | break; |
156 | case OPT_OID: | |
157 | oidfile = opt_arg(); | |
158 | break; | |
159 | case OPT_OFFSET: | |
160 | offset = strtol(opt_arg(), NULL, 0); | |
161 | break; | |
162 | case OPT_LENGTH: | |
163 | length = atoi(opt_arg()); | |
164 | break; | |
165 | case OPT_DUMP: | |
0f113f3e | 166 | dump = -1; |
7e1b7485 RS |
167 | break; |
168 | case OPT_DLIMIT: | |
169 | dump = atoi(opt_arg()); | |
170 | break; | |
171 | case OPT_STRPARSE: | |
172 | sk_OPENSSL_STRING_push(osk, opt_arg()); | |
173 | break; | |
174 | case OPT_GENSTR: | |
175 | genstr = opt_arg(); | |
176 | break; | |
177 | case OPT_GENCONF: | |
178 | genconf = opt_arg(); | |
179 | break; | |
180 | case OPT_STRICTPEM: | |
0f113f3e MC |
181 | strictpem = 1; |
182 | informat = FORMAT_PEM; | |
0f113f3e MC |
183 | break; |
184 | } | |
0f113f3e | 185 | } |
7e1b7485 | 186 | argc = opt_num_rest(); |
03358517 KR |
187 | if (argc != 0) |
188 | goto opthelp; | |
58964a49 | 189 | |
0f113f3e | 190 | if (oidfile != NULL) { |
bdd58d98 | 191 | in = bio_open_default(oidfile, 'r', FORMAT_TEXT); |
7e1b7485 | 192 | if (in == NULL) |
0f113f3e | 193 | goto end; |
0f113f3e | 194 | OBJ_create_objects(in); |
7e1b7485 | 195 | BIO_free(in); |
0f113f3e MC |
196 | } |
197 | ||
bdd58d98 | 198 | if ((in = bio_open_default(infile, 'r', informat)) == NULL) |
7e1b7485 | 199 | goto end; |
0f113f3e | 200 | |
bdd58d98 | 201 | if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL) |
7e1b7485 | 202 | goto end; |
0f113f3e MC |
203 | |
204 | if (strictpem) { | |
205 | if (PEM_read_bio(in, &name, &header, (unsigned char **)&str, &num) != | |
206 | 1) { | |
207 | BIO_printf(bio_err, "Error reading PEM file\n"); | |
208 | ERR_print_errors(bio_err); | |
209 | goto end; | |
210 | } | |
211 | } else { | |
212 | ||
213 | if ((buf = BUF_MEM_new()) == NULL) | |
214 | goto end; | |
215 | if (!BUF_MEM_grow(buf, BUFSIZ * 8)) | |
216 | goto end; /* Pre-allocate :-) */ | |
217 | ||
218 | if (genstr || genconf) { | |
ecf3a1fb | 219 | num = do_generate(genstr, genconf, buf); |
0f113f3e MC |
220 | if (num < 0) { |
221 | ERR_print_errors(bio_err); | |
222 | goto end; | |
223 | } | |
224 | } | |
225 | ||
226 | else { | |
227 | ||
228 | if (informat == FORMAT_PEM) { | |
229 | BIO *tmp; | |
230 | ||
231 | if ((b64 = BIO_new(BIO_f_base64())) == NULL) | |
232 | goto end; | |
233 | BIO_push(b64, in); | |
234 | tmp = in; | |
235 | in = b64; | |
236 | b64 = tmp; | |
237 | } | |
238 | ||
239 | num = 0; | |
240 | for (;;) { | |
241 | if (!BUF_MEM_grow(buf, (int)num + BUFSIZ)) | |
242 | goto end; | |
243 | i = BIO_read(in, &(buf->data[num]), BUFSIZ); | |
244 | if (i <= 0) | |
245 | break; | |
246 | num += i; | |
247 | } | |
248 | } | |
249 | str = buf->data; | |
250 | ||
251 | } | |
252 | ||
253 | /* If any structs to parse go through in sequence */ | |
254 | ||
255 | if (sk_OPENSSL_STRING_num(osk)) { | |
256 | tmpbuf = (unsigned char *)str; | |
257 | tmplen = num; | |
258 | for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) { | |
259 | ASN1_TYPE *atmp; | |
260 | int typ; | |
261 | j = atoi(sk_OPENSSL_STRING_value(osk, i)); | |
262 | if (j == 0) { | |
263 | BIO_printf(bio_err, "'%s' is an invalid number\n", | |
264 | sk_OPENSSL_STRING_value(osk, i)); | |
265 | continue; | |
266 | } | |
267 | tmpbuf += j; | |
268 | tmplen -= j; | |
269 | atmp = at; | |
270 | ctmpbuf = tmpbuf; | |
271 | at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen); | |
272 | ASN1_TYPE_free(atmp); | |
273 | if (!at) { | |
274 | BIO_printf(bio_err, "Error parsing structure\n"); | |
275 | ERR_print_errors(bio_err); | |
276 | goto end; | |
277 | } | |
278 | typ = ASN1_TYPE_get(at); | |
279 | if ((typ == V_ASN1_OBJECT) | |
e58c4d3c | 280 | || (typ == V_ASN1_BOOLEAN) |
0f113f3e | 281 | || (typ == V_ASN1_NULL)) { |
e58c4d3c | 282 | BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ)); |
0f113f3e MC |
283 | ERR_print_errors(bio_err); |
284 | goto end; | |
285 | } | |
286 | /* hmm... this is a little evil but it works */ | |
287 | tmpbuf = at->value.asn1_string->data; | |
288 | tmplen = at->value.asn1_string->length; | |
289 | } | |
290 | str = (char *)tmpbuf; | |
291 | num = tmplen; | |
292 | } | |
293 | ||
294 | if (offset >= num) { | |
295 | BIO_printf(bio_err, "Error: offset too large\n"); | |
296 | goto end; | |
297 | } | |
298 | ||
299 | num -= offset; | |
300 | ||
301 | if ((length == 0) || ((long)length > num)) | |
302 | length = (unsigned int)num; | |
303 | if (derout) { | |
304 | if (BIO_write(derout, str + offset, length) != (int)length) { | |
305 | BIO_printf(bio_err, "Error writing output\n"); | |
306 | ERR_print_errors(bio_err); | |
307 | goto end; | |
308 | } | |
309 | } | |
310 | if (!noout && | |
7e1b7485 | 311 | !ASN1_parse_dump(bio_out, (unsigned char *)&(str[offset]), length, |
0f113f3e MC |
312 | indent, dump)) { |
313 | ERR_print_errors(bio_err); | |
314 | goto end; | |
315 | } | |
316 | ret = 0; | |
317 | end: | |
318 | BIO_free(derout); | |
ca3a82c3 | 319 | BIO_free(in); |
ca3a82c3 | 320 | BIO_free(b64); |
0f113f3e MC |
321 | if (ret != 0) |
322 | ERR_print_errors(bio_err); | |
25aaa98a | 323 | BUF_MEM_free(buf); |
b548a1f1 RS |
324 | OPENSSL_free(name); |
325 | OPENSSL_free(header); | |
326 | if (strictpem) | |
0f113f3e | 327 | OPENSSL_free(str); |
2ace7450 | 328 | ASN1_TYPE_free(at); |
25aaa98a | 329 | sk_OPENSSL_STRING_free(osk); |
7e1b7485 | 330 | return (ret); |
0f113f3e | 331 | } |
d02b48c6 | 332 | |
ecf3a1fb | 333 | static int do_generate(char *genstr, char *genconf, BUF_MEM *buf) |
0f113f3e MC |
334 | { |
335 | CONF *cnf = NULL; | |
336 | int len; | |
0f113f3e MC |
337 | unsigned char *p; |
338 | ASN1_TYPE *atyp = NULL; | |
9ea1b878 | 339 | |
0f113f3e | 340 | if (genconf) { |
cc01d217 RS |
341 | if ((cnf = app_load_config(genconf)) == NULL) |
342 | goto err; | |
0f113f3e MC |
343 | if (!genstr) |
344 | genstr = NCONF_get_string(cnf, "default", "asn1"); | |
345 | if (!genstr) { | |
ecf3a1fb | 346 | BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf); |
0f113f3e MC |
347 | goto err; |
348 | } | |
349 | } | |
9ea1b878 | 350 | |
0f113f3e MC |
351 | atyp = ASN1_generate_nconf(genstr, cnf); |
352 | NCONF_free(cnf); | |
353 | cnf = NULL; | |
9ea1b878 | 354 | |
0f113f3e MC |
355 | if (!atyp) |
356 | return -1; | |
9ea1b878 | 357 | |
0f113f3e | 358 | len = i2d_ASN1_TYPE(atyp, NULL); |
9ea1b878 | 359 | |
0f113f3e MC |
360 | if (len <= 0) |
361 | goto err; | |
9ea1b878 | 362 | |
0f113f3e MC |
363 | if (!BUF_MEM_grow(buf, len)) |
364 | goto err; | |
9ea1b878 | 365 | |
0f113f3e | 366 | p = (unsigned char *)buf->data; |
9ea1b878 | 367 | |
0f113f3e | 368 | i2d_ASN1_TYPE(atyp, &p); |
9ea1b878 | 369 | |
0f113f3e MC |
370 | ASN1_TYPE_free(atyp); |
371 | return len; | |
9ea1b878 | 372 | |
0f113f3e MC |
373 | err: |
374 | NCONF_free(cnf); | |
375 | ASN1_TYPE_free(atyp); | |
0f113f3e | 376 | return -1; |
0f113f3e | 377 | } |