2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/pem.h>
18 #include <openssl/asn1t.h>
20 typedef 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
,
24 OPT_STRPARSE
, OPT_GENSTR
, OPT_GENCONF
, OPT_STRICTPEM
,
28 const OPTIONS asn1parse_options
[] = {
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)"},
33 {"i", OPT_INDENT
, 0, "indents the output"},
34 {"noout", OPT_NOOUT
, 0, "do not produce any output"},
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"},
49 {"item", OPT_ITEM
, 's', "item to parse and print"},
53 static int do_generate(char *genstr
, const char *genconf
, BUF_MEM
*buf
);
55 int asn1parse_main(int argc
, char **argv
)
58 BIO
*in
= NULL
, *b64
= NULL
, *derout
= NULL
;
60 STACK_OF(OPENSSL_STRING
) *osk
= NULL
;
61 char *genstr
= NULL
, *genconf
= NULL
;
62 char *infile
= NULL
, *oidfile
= NULL
, *derfile
= NULL
;
63 unsigned char *str
= NULL
;
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
;
69 unsigned char *tmpbuf
;
70 unsigned int length
= 0;
72 const ASN1_ITEM
*it
= NULL
;
74 prog
= opt_init(argc
, argv
, asn1parse_options
);
76 if ((osk
= sk_OPENSSL_STRING_new_null()) == NULL
) {
77 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
81 while ((o
= opt_next()) != OPT_EOF
) {
86 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
89 opt_help(asn1parse_options
);
93 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
112 offset
= strtol(opt_arg(), NULL
, 0);
115 length
= atoi(opt_arg());
121 dump
= atoi(opt_arg());
124 sk_OPENSSL_STRING_push(osk
, opt_arg());
134 informat
= FORMAT_PEM
;
137 it
= ASN1_ITEM_lookup(opt_arg());
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
);
147 BIO_printf(bio_err
, " %s\n", it
->sname
);
154 argc
= opt_num_rest();
158 if (oidfile
!= NULL
) {
159 in
= bio_open_default(oidfile
, 'r', FORMAT_TEXT
);
162 OBJ_create_objects(in
);
166 if ((in
= bio_open_default(infile
, 'r', informat
)) == NULL
)
169 if (derfile
&& (derout
= bio_open_default(derfile
, 'w', FORMAT_ASN1
)) == NULL
)
173 if (PEM_read_bio(in
, &name
, &header
, &str
, &num
) !=
175 BIO_printf(bio_err
, "Error reading PEM file\n");
176 ERR_print_errors(bio_err
);
181 if ((buf
= BUF_MEM_new()) == NULL
)
183 if (!BUF_MEM_grow(buf
, BUFSIZ
* 8))
184 goto end
; /* Pre-allocate :-) */
186 if (genstr
|| genconf
) {
187 num
= do_generate(genstr
, genconf
, buf
);
189 ERR_print_errors(bio_err
);
194 if (informat
== FORMAT_PEM
) {
197 if ((b64
= BIO_new(BIO_f_base64())) == NULL
)
207 if (!BUF_MEM_grow(buf
, (int)num
+ BUFSIZ
))
209 i
= BIO_read(in
, &(buf
->data
[num
]), BUFSIZ
);
215 str
= (unsigned char *)buf
->data
;
219 /* If any structs to parse go through in sequence */
221 if (sk_OPENSSL_STRING_num(osk
)) {
224 for (i
= 0; i
< sk_OPENSSL_STRING_num(osk
); i
++) {
227 j
= atoi(sk_OPENSSL_STRING_value(osk
, i
));
229 BIO_printf(bio_err
, "'%s' is an invalid number\n",
230 sk_OPENSSL_STRING_value(osk
, i
));
237 at
= d2i_ASN1_TYPE(NULL
, &ctmpbuf
, tmplen
);
238 ASN1_TYPE_free(atmp
);
240 BIO_printf(bio_err
, "Error parsing structure\n");
241 ERR_print_errors(bio_err
);
244 typ
= ASN1_TYPE_get(at
);
245 if ((typ
== V_ASN1_OBJECT
)
246 || (typ
== V_ASN1_BOOLEAN
)
247 || (typ
== V_ASN1_NULL
)) {
248 BIO_printf(bio_err
, "Can't parse %s type\n", ASN1_tag2str(typ
));
249 ERR_print_errors(bio_err
);
252 /* hmm... this is a little evil but it works */
253 tmpbuf
= at
->value
.asn1_string
->data
;
254 tmplen
= at
->value
.asn1_string
->length
;
261 BIO_printf(bio_err
, "Error: offset too large\n");
267 if ((length
== 0) || ((long)length
> num
))
268 length
= (unsigned int)num
;
269 if (derout
!= NULL
) {
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
);
277 const unsigned char *p
= str
+ offset
;
280 ASN1_VALUE
*value
= ASN1_item_d2i(NULL
, &p
, length
, it
);
282 BIO_printf(bio_err
, "Error parsing item %s\n", it
->sname
);
283 ERR_print_errors(bio_err
);
286 ASN1_item_print(bio_out
, value
, 0, it
, NULL
);
287 ASN1_item_free(value
, it
);
289 if (!ASN1_parse_dump(bio_out
, p
, length
, indent
, dump
)) {
290 ERR_print_errors(bio_err
);
301 ERR_print_errors(bio_err
);
304 OPENSSL_free(header
);
308 sk_OPENSSL_STRING_free(osk
);
312 static int do_generate(char *genstr
, const char *genconf
, BUF_MEM
*buf
)
317 ASN1_TYPE
*atyp
= NULL
;
319 if (genconf
!= NULL
) {
320 if ((cnf
= app_load_config(genconf
)) == NULL
)
323 genstr
= NCONF_get_string(cnf
, "default", "asn1");
324 if (genstr
== NULL
) {
325 BIO_printf(bio_err
, "Can't find 'asn1' in '%s'\n", genconf
);
330 atyp
= ASN1_generate_nconf(genstr
, cnf
);
337 len
= i2d_ASN1_TYPE(atyp
, NULL
);
342 if (!BUF_MEM_grow(buf
, len
))
345 p
= (unsigned char *)buf
->data
;
347 i2d_ASN1_TYPE(atyp
, &p
);
349 ASN1_TYPE_free(atyp
);
354 ASN1_TYPE_free(atyp
);