1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
---|
5 | * |
---|
6 | * Licensed under the OpenSSL license (the "License"). You may not use |
---|
7 | * this file except in compliance with the License. You can obtain a copy |
---|
8 | * in the file LICENSE in the source distribution or at |
---|
9 | * https://www.openssl.org/source/license.html |
---|
10 | */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <string.h> |
---|
15 | #include "apps.h" |
---|
16 | #include "progs.h" |
---|
17 | #include <openssl/err.h> |
---|
18 | #include <openssl/evp.h> |
---|
19 | #include <openssl/x509.h> |
---|
20 | #include <openssl/pem.h> |
---|
21 | #include <openssl/asn1t.h> |
---|
22 | |
---|
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, |
---|
27 | OPT_STRPARSE, OPT_GENSTR, OPT_GENCONF, OPT_STRICTPEM, |
---|
28 | OPT_ITEM |
---|
29 | } OPTION_CHOICE; |
---|
30 | |
---|
31 | const 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)"}, |
---|
36 | {"i", OPT_INDENT, 0, "indents the output"}, |
---|
37 | {"noout", OPT_NOOUT, 0, "do not produce any output"}, |
---|
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, 'p', |
---|
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 | {"item", OPT_ITEM, 's', "item to parse and print"}, |
---|
53 | {NULL} |
---|
54 | }; |
---|
55 | |
---|
56 | static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf); |
---|
57 | |
---|
58 | int asn1parse_main(int argc, char **argv) |
---|
59 | { |
---|
60 | ASN1_TYPE *at = NULL; |
---|
61 | BIO *in = NULL, *b64 = NULL, *derout = NULL; |
---|
62 | BUF_MEM *buf = NULL; |
---|
63 | STACK_OF(OPENSSL_STRING) *osk = NULL; |
---|
64 | char *genstr = NULL, *genconf = NULL; |
---|
65 | char *infile = NULL, *oidfile = NULL, *derfile = NULL; |
---|
66 | unsigned char *str = NULL; |
---|
67 | char *name = NULL, *header = NULL, *prog; |
---|
68 | const unsigned char *ctmpbuf; |
---|
69 | int indent = 0, noout = 0, dump = 0, strictpem = 0, informat = FORMAT_PEM; |
---|
70 | int offset = 0, ret = 1, i, j; |
---|
71 | long num, tmplen; |
---|
72 | unsigned char *tmpbuf; |
---|
73 | unsigned int length = 0; |
---|
74 | OPTION_CHOICE o; |
---|
75 | const ASN1_ITEM *it = NULL; |
---|
76 | |
---|
77 | prog = opt_init(argc, argv, asn1parse_options); |
---|
78 | |
---|
79 | if ((osk = sk_OPENSSL_STRING_new_null()) == NULL) { |
---|
80 | BIO_printf(bio_err, "%s: Memory allocation failure\n", prog); |
---|
81 | goto end; |
---|
82 | } |
---|
83 | |
---|
84 | while ((o = opt_next()) != OPT_EOF) { |
---|
85 | switch (o) { |
---|
86 | case OPT_EOF: |
---|
87 | case OPT_ERR: |
---|
88 | opthelp: |
---|
89 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
---|
90 | goto end; |
---|
91 | case OPT_HELP: |
---|
92 | opt_help(asn1parse_options); |
---|
93 | ret = 0; |
---|
94 | goto end; |
---|
95 | case OPT_INFORM: |
---|
96 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) |
---|
97 | goto opthelp; |
---|
98 | break; |
---|
99 | case OPT_IN: |
---|
100 | infile = opt_arg(); |
---|
101 | break; |
---|
102 | case OPT_OUT: |
---|
103 | derfile = opt_arg(); |
---|
104 | break; |
---|
105 | case OPT_INDENT: |
---|
106 | indent = 1; |
---|
107 | break; |
---|
108 | case OPT_NOOUT: |
---|
109 | noout = 1; |
---|
110 | break; |
---|
111 | case OPT_OID: |
---|
112 | oidfile = opt_arg(); |
---|
113 | break; |
---|
114 | case OPT_OFFSET: |
---|
115 | offset = strtol(opt_arg(), NULL, 0); |
---|
116 | break; |
---|
117 | case OPT_LENGTH: |
---|
118 | length = strtol(opt_arg(), NULL, 0); |
---|
119 | break; |
---|
120 | case OPT_DUMP: |
---|
121 | dump = -1; |
---|
122 | break; |
---|
123 | case OPT_DLIMIT: |
---|
124 | dump = strtol(opt_arg(), NULL, 0); |
---|
125 | break; |
---|
126 | case OPT_STRPARSE: |
---|
127 | sk_OPENSSL_STRING_push(osk, opt_arg()); |
---|
128 | break; |
---|
129 | case OPT_GENSTR: |
---|
130 | genstr = opt_arg(); |
---|
131 | break; |
---|
132 | case OPT_GENCONF: |
---|
133 | genconf = opt_arg(); |
---|
134 | break; |
---|
135 | case OPT_STRICTPEM: |
---|
136 | strictpem = 1; |
---|
137 | informat = FORMAT_PEM; |
---|
138 | break; |
---|
139 | case OPT_ITEM: |
---|
140 | it = ASN1_ITEM_lookup(opt_arg()); |
---|
141 | if (it == NULL) { |
---|
142 | size_t tmp; |
---|
143 | |
---|
144 | BIO_printf(bio_err, "Unknown item name %s\n", opt_arg()); |
---|
145 | BIO_puts(bio_err, "Supported types:\n"); |
---|
146 | for (tmp = 0;; tmp++) { |
---|
147 | it = ASN1_ITEM_get(tmp); |
---|
148 | if (it == NULL) |
---|
149 | break; |
---|
150 | BIO_printf(bio_err, " %s\n", it->sname); |
---|
151 | } |
---|
152 | goto end; |
---|
153 | } |
---|
154 | break; |
---|
155 | } |
---|
156 | } |
---|
157 | argc = opt_num_rest(); |
---|
158 | if (argc != 0) |
---|
159 | goto opthelp; |
---|
160 | |
---|
161 | if (oidfile != NULL) { |
---|
162 | in = bio_open_default(oidfile, 'r', FORMAT_TEXT); |
---|
163 | if (in == NULL) |
---|
164 | goto end; |
---|
165 | OBJ_create_objects(in); |
---|
166 | BIO_free(in); |
---|
167 | } |
---|
168 | |
---|
169 | if ((in = bio_open_default(infile, 'r', informat)) == NULL) |
---|
170 | goto end; |
---|
171 | |
---|
172 | if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL) |
---|
173 | goto end; |
---|
174 | |
---|
175 | if (strictpem) { |
---|
176 | if (PEM_read_bio(in, &name, &header, &str, &num) != |
---|
177 | 1) { |
---|
178 | BIO_printf(bio_err, "Error reading PEM file\n"); |
---|
179 | ERR_print_errors(bio_err); |
---|
180 | goto end; |
---|
181 | } |
---|
182 | } else { |
---|
183 | |
---|
184 | if ((buf = BUF_MEM_new()) == NULL) |
---|
185 | goto end; |
---|
186 | if (!BUF_MEM_grow(buf, BUFSIZ * 8)) |
---|
187 | goto end; /* Pre-allocate :-) */ |
---|
188 | |
---|
189 | if (genstr || genconf) { |
---|
190 | num = do_generate(genstr, genconf, buf); |
---|
191 | if (num < 0) { |
---|
192 | ERR_print_errors(bio_err); |
---|
193 | goto end; |
---|
194 | } |
---|
195 | } else { |
---|
196 | |
---|
197 | if (informat == FORMAT_PEM) { |
---|
198 | BIO *tmp; |
---|
199 | |
---|
200 | if ((b64 = BIO_new(BIO_f_base64())) == NULL) |
---|
201 | goto end; |
---|
202 | BIO_push(b64, in); |
---|
203 | tmp = in; |
---|
204 | in = b64; |
---|
205 | b64 = tmp; |
---|
206 | } |
---|
207 | |
---|
208 | num = 0; |
---|
209 | for (;;) { |
---|
210 | if (!BUF_MEM_grow(buf, num + BUFSIZ)) |
---|
211 | goto end; |
---|
212 | i = BIO_read(in, &(buf->data[num]), BUFSIZ); |
---|
213 | if (i <= 0) |
---|
214 | break; |
---|
215 | num += i; |
---|
216 | } |
---|
217 | } |
---|
218 | str = (unsigned char *)buf->data; |
---|
219 | |
---|
220 | } |
---|
221 | |
---|
222 | /* If any structs to parse go through in sequence */ |
---|
223 | |
---|
224 | if (sk_OPENSSL_STRING_num(osk)) { |
---|
225 | tmpbuf = str; |
---|
226 | tmplen = num; |
---|
227 | for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) { |
---|
228 | ASN1_TYPE *atmp; |
---|
229 | int typ; |
---|
230 | j = strtol(sk_OPENSSL_STRING_value(osk, i), NULL, 0); |
---|
231 | if (j <= 0 || j >= tmplen) { |
---|
232 | BIO_printf(bio_err, "'%s' is out of range\n", |
---|
233 | sk_OPENSSL_STRING_value(osk, i)); |
---|
234 | continue; |
---|
235 | } |
---|
236 | tmpbuf += j; |
---|
237 | tmplen -= j; |
---|
238 | atmp = at; |
---|
239 | ctmpbuf = tmpbuf; |
---|
240 | at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen); |
---|
241 | ASN1_TYPE_free(atmp); |
---|
242 | if (!at) { |
---|
243 | BIO_printf(bio_err, "Error parsing structure\n"); |
---|
244 | ERR_print_errors(bio_err); |
---|
245 | goto end; |
---|
246 | } |
---|
247 | typ = ASN1_TYPE_get(at); |
---|
248 | if ((typ == V_ASN1_OBJECT) |
---|
249 | || (typ == V_ASN1_BOOLEAN) |
---|
250 | || (typ == V_ASN1_NULL)) { |
---|
251 | BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ)); |
---|
252 | ERR_print_errors(bio_err); |
---|
253 | goto end; |
---|
254 | } |
---|
255 | /* hmm... this is a little evil but it works */ |
---|
256 | tmpbuf = at->value.asn1_string->data; |
---|
257 | tmplen = at->value.asn1_string->length; |
---|
258 | } |
---|
259 | str = tmpbuf; |
---|
260 | num = tmplen; |
---|
261 | } |
---|
262 | |
---|
263 | if (offset < 0 || offset >= num) { |
---|
264 | BIO_printf(bio_err, "Error: offset out of range\n"); |
---|
265 | goto end; |
---|
266 | } |
---|
267 | |
---|
268 | num -= offset; |
---|
269 | |
---|
270 | if (length == 0 || length > (unsigned int)num) |
---|
271 | length = (unsigned int)num; |
---|
272 | if (derout != NULL) { |
---|
273 | if (BIO_write(derout, str + offset, length) != (int)length) { |
---|
274 | BIO_printf(bio_err, "Error writing output\n"); |
---|
275 | ERR_print_errors(bio_err); |
---|
276 | goto end; |
---|
277 | } |
---|
278 | } |
---|
279 | if (!noout) { |
---|
280 | const unsigned char *p = str + offset; |
---|
281 | |
---|
282 | if (it != NULL) { |
---|
283 | ASN1_VALUE *value = ASN1_item_d2i(NULL, &p, length, it); |
---|
284 | if (value == NULL) { |
---|
285 | BIO_printf(bio_err, "Error parsing item %s\n", it->sname); |
---|
286 | ERR_print_errors(bio_err); |
---|
287 | goto end; |
---|
288 | } |
---|
289 | ASN1_item_print(bio_out, value, 0, it, NULL); |
---|
290 | ASN1_item_free(value, it); |
---|
291 | } else { |
---|
292 | if (!ASN1_parse_dump(bio_out, p, length, indent, dump)) { |
---|
293 | ERR_print_errors(bio_err); |
---|
294 | goto end; |
---|
295 | } |
---|
296 | } |
---|
297 | } |
---|
298 | ret = 0; |
---|
299 | end: |
---|
300 | BIO_free(derout); |
---|
301 | BIO_free(in); |
---|
302 | BIO_free(b64); |
---|
303 | if (ret != 0) |
---|
304 | ERR_print_errors(bio_err); |
---|
305 | BUF_MEM_free(buf); |
---|
306 | OPENSSL_free(name); |
---|
307 | OPENSSL_free(header); |
---|
308 | if (strictpem) |
---|
309 | OPENSSL_free(str); |
---|
310 | ASN1_TYPE_free(at); |
---|
311 | sk_OPENSSL_STRING_free(osk); |
---|
312 | return ret; |
---|
313 | } |
---|
314 | |
---|
315 | static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf) |
---|
316 | { |
---|
317 | CONF *cnf = NULL; |
---|
318 | int len; |
---|
319 | unsigned char *p; |
---|
320 | ASN1_TYPE *atyp = NULL; |
---|
321 | |
---|
322 | if (genconf != NULL) { |
---|
323 | if ((cnf = app_load_config(genconf)) == NULL) |
---|
324 | goto err; |
---|
325 | if (genstr == NULL) |
---|
326 | genstr = NCONF_get_string(cnf, "default", "asn1"); |
---|
327 | if (genstr == NULL) { |
---|
328 | BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf); |
---|
329 | goto err; |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | atyp = ASN1_generate_nconf(genstr, cnf); |
---|
334 | NCONF_free(cnf); |
---|
335 | cnf = NULL; |
---|
336 | |
---|
337 | if (atyp == NULL) |
---|
338 | return -1; |
---|
339 | |
---|
340 | len = i2d_ASN1_TYPE(atyp, NULL); |
---|
341 | |
---|
342 | if (len <= 0) |
---|
343 | goto err; |
---|
344 | |
---|
345 | if (!BUF_MEM_grow(buf, len)) |
---|
346 | goto err; |
---|
347 | |
---|
348 | p = (unsigned char *)buf->data; |
---|
349 | |
---|
350 | i2d_ASN1_TYPE(atyp, &p); |
---|
351 | |
---|
352 | ASN1_TYPE_free(atyp); |
---|
353 | return len; |
---|
354 | |
---|
355 | err: |
---|
356 | NCONF_free(cnf); |
---|
357 | ASN1_TYPE_free(atyp); |
---|
358 | return -1; |
---|
359 | } |
---|