]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 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> | |
d02b48c6 | 23 | |
7e1b7485 RS |
24 | typedef enum OPTION_choice { |
25 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
26 | OPT_INFORM, OPT_IN, OPT_OUT, OPT_INDENT, OPT_NOOUT, | |
27 | OPT_OID, OPT_OFFSET, OPT_LENGTH, OPT_DUMP, OPT_DLIMIT, | |
28 | OPT_STRPARSE, OPT_GENSTR, OPT_GENCONF, OPT_STRICTPEM | |
29 | } OPTION_CHOICE; | |
30 | ||
31 | OPTIONS asn1parse_options[] = { | |
32 | {"help", OPT_HELP, '-', "Display this summary"}, | |
33 | {"inform", OPT_INFORM, 'F', "input format - one of DER PEM"}, | |
34 | {"in", OPT_IN, '<', "input file"}, | |
35 | {"out", OPT_OUT, '>', "output file (output format is always DER)"}, | |
6755ff11 | 36 | {"i", OPT_INDENT, 0, "indents the output"}, |
16e1b281 | 37 | {"noout", OPT_NOOUT, 0, "do not produce any output"}, |
7e1b7485 RS |
38 | {"offset", OPT_OFFSET, 'p', "offset into file"}, |
39 | {"length", OPT_LENGTH, 'p', "length of section in file"}, | |
40 | {"oid", OPT_OID, '<', "file of extra oid definitions"}, | |
41 | {"dump", OPT_DUMP, 0, "unknown data in hex form"}, | |
42 | {"dlimit", OPT_DLIMIT, 'p', | |
43 | "dump the first arg bytes of unknown data in hex form"}, | |
44 | {"strparse", OPT_STRPARSE, 's', | |
45 | "offset; a series of these can be used to 'dig'"}, | |
46 | {OPT_MORE_STR, 0, 0, "into multiple ASN1 blob wrappings"}, | |
47 | {"genstr", OPT_GENSTR, 's', "string to generate ASN1 structure from"}, | |
48 | {"genconf", OPT_GENCONF, 's', "file to generate ASN1 structure from"}, | |
49 | {OPT_MORE_STR, 0, 0, "(-inform will be ignored)"}, | |
50 | {"strictpem", OPT_STRICTPEM, 0, | |
51 | "do not attempt base64 decode outside PEM markers"}, | |
52 | {NULL} | |
53 | }; | |
667ac4ec | 54 | |
cc696296 | 55 | static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf); |
9ea1b878 | 56 | |
7e1b7485 | 57 | int asn1parse_main(int argc, char **argv) |
0f113f3e | 58 | { |
7e1b7485 RS |
59 | ASN1_TYPE *at = NULL; |
60 | BIO *in = NULL, *b64 = NULL, *derout = NULL; | |
0f113f3e MC |
61 | BUF_MEM *buf = NULL; |
62 | STACK_OF(OPENSSL_STRING) *osk = NULL; | |
7e1b7485 | 63 | char *genstr = NULL, *genconf = NULL; |
d012c1a1 MC |
64 | char *infile = NULL, *oidfile = NULL, *derfile = NULL; |
65 | unsigned char *str = NULL; | |
7e1b7485 RS |
66 | char *name = NULL, *header = NULL, *prog; |
67 | const unsigned char *ctmpbuf; | |
68 | int indent = 0, noout = 0, dump = 0, strictpem = 0, informat = FORMAT_PEM; | |
69 | int offset = 0, ret = 1, i, j; | |
70 | long num, tmplen; | |
71 | unsigned char *tmpbuf; | |
72 | unsigned int length = 0; | |
73 | OPTION_CHOICE o; | |
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: | |
116 | length = atoi(opt_arg()); | |
117 | break; | |
118 | case OPT_DUMP: | |
0f113f3e | 119 | dump = -1; |
7e1b7485 RS |
120 | break; |
121 | case OPT_DLIMIT: | |
122 | dump = atoi(opt_arg()); | |
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 MC |
136 | break; |
137 | } | |
0f113f3e | 138 | } |
7e1b7485 | 139 | argc = opt_num_rest(); |
03358517 KR |
140 | if (argc != 0) |
141 | goto opthelp; | |
58964a49 | 142 | |
0f113f3e | 143 | if (oidfile != NULL) { |
bdd58d98 | 144 | in = bio_open_default(oidfile, 'r', FORMAT_TEXT); |
7e1b7485 | 145 | if (in == NULL) |
0f113f3e | 146 | goto end; |
0f113f3e | 147 | OBJ_create_objects(in); |
7e1b7485 | 148 | BIO_free(in); |
0f113f3e MC |
149 | } |
150 | ||
bdd58d98 | 151 | if ((in = bio_open_default(infile, 'r', informat)) == NULL) |
7e1b7485 | 152 | goto end; |
0f113f3e | 153 | |
bdd58d98 | 154 | if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL) |
7e1b7485 | 155 | goto end; |
0f113f3e MC |
156 | |
157 | if (strictpem) { | |
d012c1a1 | 158 | if (PEM_read_bio(in, &name, &header, &str, &num) != |
0f113f3e MC |
159 | 1) { |
160 | BIO_printf(bio_err, "Error reading PEM file\n"); | |
161 | ERR_print_errors(bio_err); | |
162 | goto end; | |
163 | } | |
164 | } else { | |
165 | ||
166 | if ((buf = BUF_MEM_new()) == NULL) | |
167 | goto end; | |
168 | if (!BUF_MEM_grow(buf, BUFSIZ * 8)) | |
169 | goto end; /* Pre-allocate :-) */ | |
170 | ||
171 | if (genstr || genconf) { | |
ecf3a1fb | 172 | num = do_generate(genstr, genconf, buf); |
0f113f3e MC |
173 | if (num < 0) { |
174 | ERR_print_errors(bio_err); | |
175 | goto end; | |
176 | } | |
177 | } | |
178 | ||
179 | else { | |
180 | ||
181 | if (informat == FORMAT_PEM) { | |
182 | BIO *tmp; | |
183 | ||
184 | if ((b64 = BIO_new(BIO_f_base64())) == NULL) | |
185 | goto end; | |
186 | BIO_push(b64, in); | |
187 | tmp = in; | |
188 | in = b64; | |
189 | b64 = tmp; | |
190 | } | |
191 | ||
192 | num = 0; | |
193 | for (;;) { | |
194 | if (!BUF_MEM_grow(buf, (int)num + BUFSIZ)) | |
195 | goto end; | |
196 | i = BIO_read(in, &(buf->data[num]), BUFSIZ); | |
197 | if (i <= 0) | |
198 | break; | |
199 | num += i; | |
200 | } | |
201 | } | |
d012c1a1 | 202 | str = (unsigned char *)buf->data; |
0f113f3e MC |
203 | |
204 | } | |
205 | ||
206 | /* If any structs to parse go through in sequence */ | |
207 | ||
208 | if (sk_OPENSSL_STRING_num(osk)) { | |
d012c1a1 | 209 | tmpbuf = str; |
0f113f3e MC |
210 | tmplen = num; |
211 | for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) { | |
212 | ASN1_TYPE *atmp; | |
213 | int typ; | |
214 | j = atoi(sk_OPENSSL_STRING_value(osk, i)); | |
215 | if (j == 0) { | |
216 | BIO_printf(bio_err, "'%s' is an invalid number\n", | |
217 | sk_OPENSSL_STRING_value(osk, i)); | |
218 | continue; | |
219 | } | |
220 | tmpbuf += j; | |
221 | tmplen -= j; | |
222 | atmp = at; | |
223 | ctmpbuf = tmpbuf; | |
224 | at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen); | |
225 | ASN1_TYPE_free(atmp); | |
226 | if (!at) { | |
227 | BIO_printf(bio_err, "Error parsing structure\n"); | |
228 | ERR_print_errors(bio_err); | |
229 | goto end; | |
230 | } | |
231 | typ = ASN1_TYPE_get(at); | |
232 | if ((typ == V_ASN1_OBJECT) | |
e58c4d3c | 233 | || (typ == V_ASN1_BOOLEAN) |
0f113f3e | 234 | || (typ == V_ASN1_NULL)) { |
e58c4d3c | 235 | BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ)); |
0f113f3e MC |
236 | ERR_print_errors(bio_err); |
237 | goto end; | |
238 | } | |
239 | /* hmm... this is a little evil but it works */ | |
240 | tmpbuf = at->value.asn1_string->data; | |
241 | tmplen = at->value.asn1_string->length; | |
242 | } | |
d012c1a1 | 243 | str = tmpbuf; |
0f113f3e MC |
244 | num = tmplen; |
245 | } | |
246 | ||
247 | if (offset >= num) { | |
248 | BIO_printf(bio_err, "Error: offset too large\n"); | |
249 | goto end; | |
250 | } | |
251 | ||
252 | num -= offset; | |
253 | ||
254 | if ((length == 0) || ((long)length > num)) | |
255 | length = (unsigned int)num; | |
256 | if (derout) { | |
257 | if (BIO_write(derout, str + offset, length) != (int)length) { | |
258 | BIO_printf(bio_err, "Error writing output\n"); | |
259 | ERR_print_errors(bio_err); | |
260 | goto end; | |
261 | } | |
262 | } | |
263 | if (!noout && | |
d012c1a1 | 264 | !ASN1_parse_dump(bio_out, &(str[offset]), length, |
0f113f3e MC |
265 | indent, dump)) { |
266 | ERR_print_errors(bio_err); | |
267 | goto end; | |
268 | } | |
269 | ret = 0; | |
270 | end: | |
271 | BIO_free(derout); | |
ca3a82c3 | 272 | BIO_free(in); |
ca3a82c3 | 273 | BIO_free(b64); |
0f113f3e MC |
274 | if (ret != 0) |
275 | ERR_print_errors(bio_err); | |
25aaa98a | 276 | BUF_MEM_free(buf); |
b548a1f1 RS |
277 | OPENSSL_free(name); |
278 | OPENSSL_free(header); | |
279 | if (strictpem) | |
0f113f3e | 280 | OPENSSL_free(str); |
2ace7450 | 281 | ASN1_TYPE_free(at); |
25aaa98a | 282 | sk_OPENSSL_STRING_free(osk); |
7e1b7485 | 283 | return (ret); |
0f113f3e | 284 | } |
d02b48c6 | 285 | |
cc696296 | 286 | static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf) |
0f113f3e MC |
287 | { |
288 | CONF *cnf = NULL; | |
289 | int len; | |
0f113f3e MC |
290 | unsigned char *p; |
291 | ASN1_TYPE *atyp = NULL; | |
9ea1b878 | 292 | |
0f113f3e | 293 | if (genconf) { |
cc01d217 RS |
294 | if ((cnf = app_load_config(genconf)) == NULL) |
295 | goto err; | |
0f113f3e MC |
296 | if (!genstr) |
297 | genstr = NCONF_get_string(cnf, "default", "asn1"); | |
298 | if (!genstr) { | |
ecf3a1fb | 299 | BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf); |
0f113f3e MC |
300 | goto err; |
301 | } | |
302 | } | |
9ea1b878 | 303 | |
0f113f3e MC |
304 | atyp = ASN1_generate_nconf(genstr, cnf); |
305 | NCONF_free(cnf); | |
306 | cnf = NULL; | |
9ea1b878 | 307 | |
0f113f3e MC |
308 | if (!atyp) |
309 | return -1; | |
9ea1b878 | 310 | |
0f113f3e | 311 | len = i2d_ASN1_TYPE(atyp, NULL); |
9ea1b878 | 312 | |
0f113f3e MC |
313 | if (len <= 0) |
314 | goto err; | |
9ea1b878 | 315 | |
0f113f3e MC |
316 | if (!BUF_MEM_grow(buf, len)) |
317 | goto err; | |
9ea1b878 | 318 | |
0f113f3e | 319 | p = (unsigned char *)buf->data; |
9ea1b878 | 320 | |
0f113f3e | 321 | i2d_ASN1_TYPE(atyp, &p); |
9ea1b878 | 322 | |
0f113f3e MC |
323 | ASN1_TYPE_free(atyp); |
324 | return len; | |
9ea1b878 | 325 | |
0f113f3e MC |
326 | err: |
327 | NCONF_free(cnf); | |
328 | ASN1_TYPE_free(atyp); | |
0f113f3e | 329 | return -1; |
0f113f3e | 330 | } |