2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
44 static int indent_flag
= 1;
46 static unsigned long indefinite_form_loop
;
47 static unsigned long indefinite_form_loop_max
= 10000;
50 loop (unsigned char *buf
, size_t len
, int indent
)
52 unsigned char *start_buf
= buf
;
61 size_t loop_length
= 0;
65 ret
= der_get_tag (buf
, len
, &class, &type
, &tag
, &sz
);
67 errx (1, "der_get_tag: %s", error_message (ret
));
69 errx (1, "unreasonable length (%u) > %u",
70 (unsigned)sz
, (unsigned)len
);
75 for (i
= 0; i
< indent
; ++i
)
78 printf ("%s %s ", der_get_class_name(class), der_get_type_name(type
));
79 tagname
= der_get_tag_name(tag
);
80 if (class == ASN1_C_UNIV
&& tagname
!= NULL
)
81 printf ("%s = ", tagname
);
83 printf ("tag %d = ", tag
);
84 ret
= der_get_length (buf
, len
, &length
, &sz
);
86 errx (1, "der_get_tag: %s", error_message (ret
));
88 errx (1, "unreasonable tag length (%u) > %u",
89 (unsigned)sz
, (unsigned)len
);
92 if (length
== ASN1_INDEFINITE
) {
93 if ((class == ASN1_C_UNIV
&& type
== PRIM
&& tag
== UT_OctetString
) ||
94 (class == ASN1_C_CONTEXT
&& type
== CONS
) ||
95 (class == ASN1_C_UNIV
&& type
== CONS
&& tag
== UT_Sequence
) ||
96 (class == ASN1_C_UNIV
&& type
== CONS
&& tag
== UT_Set
)) {
97 printf("*INDEFINITE FORM*");
100 errx(1, "indef form used on unsupported object");
103 if (indefinite_form_loop
> indefinite_form_loop_max
)
104 errx(1, "indefinite form used recursively more then %lu "
105 "times, aborting", indefinite_form_loop_max
);
106 indefinite_form_loop
++;
108 } else if (length
> len
) {
111 errx (1, "unreasonable inner length (%u) > %u",
112 (unsigned)length
, (unsigned)len
);
114 if (class == ASN1_C_CONTEXT
|| class == ASN1_C_APPL
) {
115 printf ("%lu bytes [%u]", (unsigned long)length
, tag
);
118 loop_length
= loop (buf
, length
, indent
+ 2);
120 printf(" IMPLICIT content\n");
122 } else if (class == ASN1_C_UNIV
) {
124 case UT_EndOfContent
:
125 printf (" INDEFINITE length was %lu\n",
126 (unsigned long)(buf
- start_buf
));
130 printf ("%lu bytes {\n", (unsigned long)length
);
131 loop_length
= loop (buf
, length
, indent
+ 2);
134 for (i
= 0; i
< indent
; ++i
)
138 printf ("} indent = %d\n", indent
/ 2);
143 if (length
<= sizeof(val
)) {
144 ret
= der_get_integer (buf
, length
, &val
, NULL
);
146 errx (1, "der_get_integer: %s", error_message (ret
));
147 printf ("integer %d\n", val
);
152 ret
= der_get_heim_integer(buf
, length
, &vali
, NULL
);
154 errx (1, "der_get_heim_integer: %s",
155 error_message (ret
));
156 ret
= der_print_hex_heim_integer(&vali
, &p
);
158 errx (1, "der_print_hex_heim_integer: %s",
159 error_message (ret
));
160 printf ("BIG NUM integer: length %lu %s\n",
161 (unsigned long)length
, p
);
166 case UT_OctetString
: {
167 heim_octet_string str
;
171 ret
= der_get_octet_string (buf
, length
, &str
, NULL
);
173 errx (1, "der_get_octet_string: %s", error_message (ret
));
174 printf ("(length %lu), ", (unsigned long)length
);
175 uc
= (unsigned char *)str
.data
;
176 for (i
= 0; i
< min(16,length
); ++i
)
177 printf ("%02x", uc
[i
]);
182 case UT_GeneralizedTime
:
183 case UT_GeneralString
:
184 case UT_PrintableString
:
185 case UT_VisibleString
: {
186 heim_general_string str
;
188 ret
= der_get_general_string (buf
, length
, &str
, NULL
);
190 errx (1, "der_get_general_string: %s",
191 error_message (ret
));
192 printf ("\"%s\"\n", str
);
200 ret
= der_get_oid(buf
, length
, &o
, NULL
);
202 errx (1, "der_get_oid: %s", error_message (ret
));
203 ret
= der_print_heim_oid(&o
, '.', &p
);
206 errx (1, "der_print_heim_oid: %s", error_message (ret
));
212 case UT_Enumerated
: {
215 ret
= der_get_integer (buf
, length
, &num
, NULL
);
217 errx (1, "der_get_enum: %s", error_message (ret
));
223 printf ("%lu bytes\n", (unsigned long)length
);
228 if (loop_length
== 0)
229 errx(1, "zero length INDEFINITE data ? indent = %d\n",
231 if (loop_length
< length
)
232 length
= loop_length
;
233 if (indefinite_form_loop
== 0)
234 errx(1, "internal error in indefinite form loop detection");
235 indefinite_form_loop
--;
236 } else if (loop_length
)
237 errx(1, "internal error for INDEFINITE form");
245 doit (const char *filename
)
247 int fd
= open (filename
, O_RDONLY
);
254 err (1, "opening %s for read", filename
);
255 if (fstat (fd
, &sb
) < 0)
256 err (1, "stat %s", filename
);
259 if (read (fd
, buf
, len
) != len
)
260 errx (1, "read failed");
262 ret
= loop (buf
, len
, 0);
268 static int version_flag
;
269 static int help_flag
;
270 struct getargs args
[] = {
271 { "indent", 0, arg_negative_flag
, &indent_flag
},
272 { "version", 0, arg_flag
, &version_flag
},
273 { "help", 0, arg_flag
, &help_flag
}
275 int num_args
= sizeof(args
) / sizeof(args
[0]);
280 arg_printusage(args
, num_args
, NULL
, "dump-file");
285 main(int argc
, char **argv
)
289 setprogname (argv
[0]);
290 initialize_asn1_error_table ();
291 if(getarg(args
, num_args
, argc
, argv
, &optidx
))
303 return doit (argv
[0]);