2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509.h>
18 #include <openssl/pem.h>
19 #include <openssl/asn1t.h>
21 typedef enum OPTION_choice {
22 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
23 OPT_INFORM, OPT_IN, OPT_OUT, OPT_INDENT, OPT_NOOUT,
24 OPT_OID, OPT_OFFSET, OPT_LENGTH, OPT_DUMP, OPT_DLIMIT,
25 OPT_STRPARSE, OPT_GENSTR, OPT_GENCONF, OPT_STRICTPEM,
29 const OPTIONS asn1parse_options[] = {
30 OPT_SECTION("General"),
31 {"help", OPT_HELP, '-', "Display this summary"},
32 {"oid", OPT_OID, '<', "file of extra oid definitions"},
35 {"inform", OPT_INFORM, 'F', "input format - one of DER PEM"},
36 {"in", OPT_IN, '<', "input file"},
37 {"out", OPT_OUT, '>', "output file (output format is always DER)"},
38 {"noout", OPT_NOOUT, 0, "do not produce any output"},
39 {"offset", OPT_OFFSET, 'p', "offset into file"},
40 {"length", OPT_LENGTH, 'p', "length of section in file"},
41 {"strparse", OPT_STRPARSE, 'p',
42 "offset; a series of these can be used to 'dig'"},
43 {"genstr", OPT_GENSTR, 's', "string to generate ASN1 structure from"},
44 {OPT_MORE_STR, 0, 0, "into multiple ASN1 blob wrappings"},
45 {"genconf", OPT_GENCONF, 's', "file to generate ASN1 structure from"},
46 {"strictpem", OPT_STRICTPEM, 0,
47 "do not attempt base64 decode outside PEM markers"},
48 {"item", OPT_ITEM, 's', "item to parse and print"},
49 {OPT_MORE_STR, 0, 0, "(-inform will be ignored)"},
51 OPT_SECTION("Formatting"),
52 {"i", OPT_INDENT, 0, "indents the output"},
53 {"dump", OPT_DUMP, 0, "unknown data in hex form"},
54 {"dlimit", OPT_DLIMIT, 'p',
55 "dump the first arg bytes of unknown data in hex form"},
59 static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf);
61 int asn1parse_main(int argc, char **argv)
64 BIO *in = NULL, *b64 = NULL, *derout = NULL;
66 STACK_OF(OPENSSL_STRING) *osk = NULL;
67 char *genstr = NULL, *genconf = NULL;
68 char *infile = NULL, *oidfile = NULL, *derfile = NULL;
69 unsigned char *str = NULL;
70 char *name = NULL, *header = NULL, *prog;
71 const unsigned char *ctmpbuf;
72 int indent = 0, noout = 0, dump = 0, strictpem = 0, informat = FORMAT_PEM;
73 int offset = 0, ret = 1, i, j;
75 unsigned char *tmpbuf;
76 unsigned int length = 0;
78 const ASN1_ITEM *it = NULL;
80 prog = opt_init(argc, argv, asn1parse_options);
82 if ((osk = sk_OPENSSL_STRING_new_null()) == NULL) {
83 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
87 while ((o = opt_next()) != OPT_EOF) {
92 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
95 opt_help(asn1parse_options);
99 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
118 offset = strtol(opt_arg(), NULL, 0);
121 length = strtol(opt_arg(), NULL, 0);
127 dump = strtol(opt_arg(), NULL, 0);
130 sk_OPENSSL_STRING_push(osk, opt_arg());
140 informat = FORMAT_PEM;
143 it = ASN1_ITEM_lookup(opt_arg());
147 BIO_printf(bio_err, "Unknown item name %s\n", opt_arg());
148 BIO_puts(bio_err, "Supported types:\n");
149 for (tmp = 0;; tmp++) {
150 it = ASN1_ITEM_get(tmp);
153 BIO_printf(bio_err, " %s\n", it->sname);
160 argc = opt_num_rest();
164 if (oidfile != NULL) {
165 in = bio_open_default(oidfile, 'r', FORMAT_TEXT);
168 OBJ_create_objects(in);
172 if ((in = bio_open_default(infile, 'r', informat)) == NULL)
175 if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL)
178 if ((buf = BUF_MEM_new()) == NULL)
181 if (PEM_read_bio(in, &name, &header, &str, &num) != 1) {
182 BIO_printf(bio_err, "Error reading PEM file\n");
183 ERR_print_errors(bio_err);
186 buf->data = (char *)str;
187 buf->length = buf->max = num;
189 if (!BUF_MEM_grow(buf, BUFSIZ * 8))
190 goto end; /* Pre-allocate :-) */
192 if (genstr || genconf) {
193 num = do_generate(genstr, genconf, buf);
195 ERR_print_errors(bio_err);
200 if (informat == FORMAT_PEM) {
203 if ((b64 = BIO_new(BIO_f_base64())) == NULL)
213 if (!BUF_MEM_grow(buf, num + BUFSIZ))
215 i = BIO_read(in, &(buf->data[num]), BUFSIZ);
221 str = (unsigned char *)buf->data;
225 /* If any structs to parse go through in sequence */
227 if (sk_OPENSSL_STRING_num(osk)) {
230 for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) {
233 j = strtol(sk_OPENSSL_STRING_value(osk, i), NULL, 0);
234 if (j <= 0 || j >= tmplen) {
235 BIO_printf(bio_err, "'%s' is out of range\n",
236 sk_OPENSSL_STRING_value(osk, i));
243 at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen);
244 ASN1_TYPE_free(atmp);
246 BIO_printf(bio_err, "Error parsing structure\n");
247 ERR_print_errors(bio_err);
250 typ = ASN1_TYPE_get(at);
251 if ((typ == V_ASN1_OBJECT)
252 || (typ == V_ASN1_BOOLEAN)
253 || (typ == V_ASN1_NULL)) {
254 BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ));
255 ERR_print_errors(bio_err);
258 /* hmm... this is a little evil but it works */
259 tmpbuf = at->value.asn1_string->data;
260 tmplen = at->value.asn1_string->length;
266 if (offset < 0 || offset >= num) {
267 BIO_printf(bio_err, "Error: offset out of range\n");
273 if (length == 0 || length > (unsigned int)num)
274 length = (unsigned int)num;
275 if (derout != NULL) {
276 if (BIO_write(derout, str + offset, length) != (int)length) {
277 BIO_printf(bio_err, "Error writing output\n");
278 ERR_print_errors(bio_err);
283 const unsigned char *p = str + offset;
286 ASN1_VALUE *value = ASN1_item_d2i(NULL, &p, length, it);
288 BIO_printf(bio_err, "Error parsing item %s\n", it->sname);
289 ERR_print_errors(bio_err);
292 ASN1_item_print(bio_out, value, 0, it, NULL);
293 ASN1_item_free(value, it);
295 if (!ASN1_parse_dump(bio_out, p, length, indent, dump)) {
296 ERR_print_errors(bio_err);
307 ERR_print_errors(bio_err);
310 OPENSSL_free(header);
312 sk_OPENSSL_STRING_free(osk);
316 static int do_generate(char *genstr, const char *genconf, BUF_MEM *buf)
321 ASN1_TYPE *atyp = NULL;
323 if (genconf != NULL) {
324 if ((cnf = app_load_config(genconf)) == NULL)
327 genstr = NCONF_get_string(cnf, "default", "asn1");
328 if (genstr == NULL) {
329 BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf);
334 atyp = ASN1_generate_nconf(genstr, cnf);
341 len = i2d_ASN1_TYPE(atyp, NULL);
346 if (!BUF_MEM_grow(buf, len))
349 p = (unsigned char *)buf->data;
351 i2d_ASN1_TYPE(atyp, &p);
353 ASN1_TYPE_free(atyp);
358 ASN1_TYPE_free(atyp);