1 /* $OpenBSD: asn1pars.c,v 1.9 2018/02/07 05:47:55 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
59 /* A nice addition from Dr Stephen Henson <steve@openssl.org> to
60 * add the -strparse option which parses nested binary structures
71 #include <openssl/err.h>
72 #include <openssl/evp.h>
73 #include <openssl/pem.h>
74 #include <openssl/x509.h>
88 STACK_OF(OPENSSL_STRING
) *osk
;
92 asn1pars_opt_dlimit(char *arg
)
96 asn1pars_config
.dump
= strtonum(arg
, 1, INT_MAX
, &errstr
);
98 fprintf(stderr
, "-dlimit must be from 1 to INT_MAX: %s\n",
106 asn1pars_opt_length(char *arg
)
110 asn1pars_config
.length
= strtonum(arg
, 1, UINT_MAX
, &errstr
);
112 fprintf(stderr
, "-length must be from 1 to UINT_MAX: %s\n",
120 asn1pars_opt_strparse(char *arg
)
122 if (sk_OPENSSL_STRING_push(asn1pars_config
.osk
, arg
) == 0) {
123 fprintf(stderr
, "-strparse cannot add argument\n");
129 static struct option asn1pars_options
[] = {
132 .desc
= "Dump unknown data in hex form",
133 .type
= OPTION_VALUE
,
135 .opt
.value
= &asn1pars_config
.dump
,
140 .desc
= "Dump the first num bytes of unknown data in hex form",
141 .type
= OPTION_ARG_FUNC
,
142 .opt
.argfunc
= asn1pars_opt_dlimit
,
147 .desc
= "File to generate ASN.1 structure from",
149 .opt
.arg
= &asn1pars_config
.genconf
,
154 .desc
= "String to generate ASN.1 structure from",
156 .opt
.arg
= &asn1pars_config
.genstr
,
160 .desc
= "Indent output according to depth of structures",
162 .opt
.flag
= &asn1pars_config
.indent
,
167 .desc
= "The input file (default stdin)",
169 .opt
.arg
= &asn1pars_config
.infile
,
174 .desc
= "Input format (DER, TXT or PEM (default))",
175 .type
= OPTION_ARG_FORMAT
,
176 .opt
.value
= &asn1pars_config
.informat
,
181 .desc
= "Number of bytes to parse (default until EOF)",
182 .type
= OPTION_ARG_FUNC
,
183 .opt
.argfunc
= asn1pars_opt_length
,
187 .desc
= "Do not produce any output",
189 .opt
.flag
= &asn1pars_config
.noout
,
194 .desc
= "Offset to begin parsing",
195 .type
= OPTION_ARG_INT
,
196 .opt
.value
= &asn1pars_config
.offset
,
201 .desc
= "File containing additional object identifiers (OIDs)",
203 .opt
.arg
= &asn1pars_config
.oidfile
,
208 .desc
= "Output file in DER format",
210 .opt
.arg
= &asn1pars_config
.derfile
,
215 .desc
= "Parse the content octets of ASN.1 object starting at"
217 .type
= OPTION_ARG_FUNC
,
218 .opt
.argfunc
= asn1pars_opt_strparse
,
227 "usage: asn1parse [-i] [-dlimit num] [-dump] [-genconf file] "
229 " [-in file] [-inform fmt] [-length num] [-noout] [-offset num] "
231 " [-out file] [-strparse offset]\n\n");
232 options_usage(asn1pars_options
);
235 static int do_generate(BIO
*bio
, char *genstr
, char *genconf
, BUF_MEM
*buf
);
238 asn1parse_main(int argc
, char **argv
)
242 BIO
*in
= NULL
, *out
= NULL
, *b64
= NULL
, *derout
= NULL
;
244 const char *errstr
= NULL
;
245 unsigned char *tmpbuf
;
246 const unsigned char *ctmpbuf
;
248 ASN1_TYPE
*at
= NULL
;
250 if (single_execution
) {
251 if (pledge("stdio cpath wpath rpath", NULL
) == -1) {
257 memset(&asn1pars_config
, 0, sizeof(asn1pars_config
));
259 asn1pars_config
.informat
= FORMAT_PEM
;
260 if ((asn1pars_config
.osk
= sk_OPENSSL_STRING_new_null()) == NULL
) {
261 BIO_printf(bio_err
, "Memory allocation failure\n");
265 if (options_parse(argc
, argv
, asn1pars_options
, NULL
, NULL
) != 0) {
270 in
= BIO_new(BIO_s_file());
271 out
= BIO_new(BIO_s_file());
272 if ((in
== NULL
) || (out
== NULL
)) {
273 ERR_print_errors(bio_err
);
276 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
);
278 if (asn1pars_config
.oidfile
!= NULL
) {
279 if (BIO_read_filename(in
, asn1pars_config
.oidfile
) <= 0) {
280 BIO_printf(bio_err
, "problems opening %s\n",
281 asn1pars_config
.oidfile
);
282 ERR_print_errors(bio_err
);
285 OBJ_create_objects(in
);
287 if (asn1pars_config
.infile
== NULL
)
288 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
290 if (BIO_read_filename(in
, asn1pars_config
.infile
) <= 0) {
291 perror(asn1pars_config
.infile
);
296 if (asn1pars_config
.derfile
) {
297 if (!(derout
= BIO_new_file(asn1pars_config
.derfile
, "wb"))) {
298 BIO_printf(bio_err
, "problems opening %s\n",
299 asn1pars_config
.derfile
);
300 ERR_print_errors(bio_err
);
304 if ((buf
= BUF_MEM_new()) == NULL
)
306 if (!BUF_MEM_grow(buf
, BUFSIZ
* 8))
307 goto end
; /* Pre-allocate :-) */
309 if (asn1pars_config
.genstr
|| asn1pars_config
.genconf
) {
310 num
= do_generate(bio_err
, asn1pars_config
.genstr
,
311 asn1pars_config
.genconf
, buf
);
313 ERR_print_errors(bio_err
);
318 if (asn1pars_config
.informat
== FORMAT_PEM
) {
321 if ((b64
= BIO_new(BIO_f_base64())) == NULL
)
330 if (!BUF_MEM_grow(buf
, (int) num
+ BUFSIZ
))
332 i
= BIO_read(in
, &(buf
->data
[num
]), BUFSIZ
);
340 /* If any structs to parse go through in sequence */
342 if (sk_OPENSSL_STRING_num(asn1pars_config
.osk
)) {
343 tmpbuf
= (unsigned char *) str
;
345 for (i
= 0; i
< sk_OPENSSL_STRING_num(asn1pars_config
.osk
);
350 sk_OPENSSL_STRING_value(asn1pars_config
.osk
, i
),
351 1, INT_MAX
, &errstr
);
354 "'%s' is an invalid number: %s\n",
355 sk_OPENSSL_STRING_value(asn1pars_config
.osk
,
363 at
= d2i_ASN1_TYPE(NULL
, &ctmpbuf
, tmplen
);
364 ASN1_TYPE_free(atmp
);
366 BIO_printf(bio_err
, "Error parsing structure\n");
367 ERR_print_errors(bio_err
);
370 typ
= ASN1_TYPE_get(at
);
371 if ((typ
== V_ASN1_OBJECT
) ||
372 (typ
== V_ASN1_NULL
)) {
373 BIO_printf(bio_err
, "Can't parse %s type\n",
374 typ
== V_ASN1_NULL
? "NULL" : "OBJECT");
375 ERR_print_errors(bio_err
);
378 /* hmm... this is a little evil but it works */
379 tmpbuf
= at
->value
.asn1_string
->data
;
380 tmplen
= at
->value
.asn1_string
->length
;
382 str
= (char *) tmpbuf
;
385 if (asn1pars_config
.offset
>= num
) {
386 BIO_printf(bio_err
, "Error: offset too large\n");
389 num
-= asn1pars_config
.offset
;
391 if ((asn1pars_config
.length
== 0) ||
392 ((long)asn1pars_config
.length
> num
))
393 asn1pars_config
.length
= (unsigned int) num
;
395 if (BIO_write(derout
, str
+ asn1pars_config
.offset
,
396 asn1pars_config
.length
) != (int)asn1pars_config
.length
) {
397 BIO_printf(bio_err
, "Error writing output\n");
398 ERR_print_errors(bio_err
);
402 if (!asn1pars_config
.noout
&&
403 !ASN1_parse_dump(out
,
404 (unsigned char *)&(str
[asn1pars_config
.offset
]),
405 asn1pars_config
.length
, asn1pars_config
.indent
,
406 asn1pars_config
.dump
)) {
407 ERR_print_errors(bio_err
);
417 ERR_print_errors(bio_err
);
420 sk_OPENSSL_STRING_free(asn1pars_config
.osk
);
427 do_generate(BIO
* bio
, char *genstr
, char *genconf
, BUF_MEM
* buf
)
433 ASN1_TYPE
*atyp
= NULL
;
436 cnf
= NCONF_new(NULL
);
437 if (!NCONF_load(cnf
, genconf
, &errline
))
440 genstr
= NCONF_get_string(cnf
, "default", "asn1");
442 BIO_printf(bio
, "Can't find 'asn1' in '%s'\n", genconf
);
446 atyp
= ASN1_generate_nconf(genstr
, cnf
);
453 len
= i2d_ASN1_TYPE(atyp
, NULL
);
457 if (!BUF_MEM_grow(buf
, len
))
460 p
= (unsigned char *) buf
->data
;
462 i2d_ASN1_TYPE(atyp
, &p
);
464 ASN1_TYPE_free(atyp
);
470 BIO_printf(bio
, "Error on line %ld of config file '%s'\n",
473 BIO_printf(bio
, "Error loading config file '%s'\n", genconf
);
477 ASN1_TYPE_free(atyp
);