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