2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/types.h>
46 static int indent_flag
= 1;
48 static unsigned long indefinite_form_loop
;
49 static unsigned long indefinite_form_loop_max
= 10000;
52 loop (unsigned char *buf
, size_t len
, int indent
)
54 unsigned char *start_buf
= buf
;
63 size_t loop_length
= 0;
67 ret
= der_get_tag (buf
, len
, &class, &type
, &tag
, &sz
);
69 errx (1, "der_get_tag: %s", error_message (ret
));
71 errx (1, "unreasonable length (%u) > %u",
72 (unsigned)sz
, (unsigned)len
);
77 for (i
= 0; i
< indent
; ++i
)
80 printf ("%s %s ", der_get_class_name(class), der_get_type_name(type
));
81 tagname
= der_get_tag_name(tag
);
82 if (class == ASN1_C_UNIV
&& tagname
!= NULL
)
83 printf ("%s = ", tagname
);
85 printf ("tag %d = ", tag
);
86 ret
= der_get_length (buf
, len
, &length
, &sz
);
88 errx (1, "der_get_tag: %s", error_message (ret
));
90 errx (1, "unreasonable tag length (%u) > %u",
91 (unsigned)sz
, (unsigned)len
);
94 if (length
== ASN1_INDEFINITE
) {
95 if ((class == ASN1_C_UNIV
&& type
== PRIM
&& tag
== UT_OctetString
) ||
96 (class == ASN1_C_CONTEXT
&& type
== CONS
) ||
97 (class == ASN1_C_UNIV
&& type
== CONS
&& tag
== UT_Sequence
) ||
98 (class == ASN1_C_UNIV
&& type
== CONS
&& tag
== UT_Set
)) {
99 printf("*INDEFINITE FORM*");
102 errx(1, "indef form used on unsupported object");
105 if (indefinite_form_loop
> indefinite_form_loop_max
)
106 errx(1, "indefinite form used recursively more then %lu "
107 "times, aborting", indefinite_form_loop_max
);
108 indefinite_form_loop
++;
110 } else if (length
> len
) {
113 errx (1, "unreasonable inner length (%u) > %u",
114 (unsigned)length
, (unsigned)len
);
116 if (class == ASN1_C_CONTEXT
|| class == ASN1_C_APPL
) {
117 printf ("%lu bytes [%u]", (unsigned long)length
, tag
);
120 loop_length
= loop (buf
, length
, indent
+ 2);
122 printf(" IMPLICIT content\n");
124 } else if (class == ASN1_C_UNIV
) {
126 case UT_EndOfContent
:
127 printf (" INDEFINITE length was %lu\n",
128 (unsigned long)(buf
- start_buf
));
132 printf ("%lu bytes {\n", (unsigned long)length
);
133 loop_length
= loop (buf
, length
, indent
+ 2);
136 for (i
= 0; i
< indent
; ++i
)
140 printf ("} indent = %d\n", indent
/ 2);
145 if (length
<= sizeof(val
)) {
146 ret
= der_get_integer (buf
, length
, &val
, NULL
);
148 errx (1, "der_get_integer: %s", error_message (ret
));
149 printf ("integer %d\n", val
);
154 ret
= der_get_heim_integer(buf
, length
, &vali
, NULL
);
156 errx (1, "der_get_heim_integer: %s",
157 error_message (ret
));
158 ret
= der_print_hex_heim_integer(&vali
, &p
);
160 errx (1, "der_print_hex_heim_integer: %s",
161 error_message (ret
));
162 printf ("BIG NUM integer: length %lu %s\n",
163 (unsigned long)length
, p
);
168 case UT_OctetString
: {
169 heim_octet_string str
;
173 ret
= der_get_octet_string (buf
, length
, &str
, NULL
);
175 errx (1, "der_get_octet_string: %s", error_message (ret
));
176 printf ("(length %lu), ", (unsigned long)length
);
177 uc
= (unsigned char *)str
.data
;
178 for (i
= 0; i
< min(16,length
); ++i
)
179 printf ("%02x", uc
[i
]);
184 case UT_GeneralizedTime
:
185 case UT_GeneralString
:
186 case UT_PrintableString
:
187 case UT_VisibleString
:
189 case UT_UTF8String
: {
190 heim_general_string str
;
192 ret
= der_get_general_string (buf
, length
, &str
, NULL
);
194 errx (1, "der_get_general_string: %s",
195 error_message (ret
));
196 printf ("\"%s\"\n", str
);
204 ret
= der_get_oid(buf
, length
, &o
, NULL
);
206 errx (1, "der_get_oid: %s", error_message (ret
));
207 ret
= der_print_heim_oid(&o
, '.', &p
);
210 errx (1, "der_print_heim_oid: %s", error_message (ret
));
216 case UT_Enumerated
: {
219 ret
= der_get_integer (buf
, length
, &num
, NULL
);
221 errx (1, "der_get_enum: %s", error_message (ret
));
227 printf ("%lu bytes\n", (unsigned long)length
);
232 if (loop_length
== 0)
233 errx(1, "zero length INDEFINITE data ? indent = %d\n",
235 if (loop_length
< length
)
236 length
= loop_length
;
237 if (indefinite_form_loop
== 0)
238 errx(1, "internal error in indefinite form loop detection");
239 indefinite_form_loop
--;
240 } else if (loop_length
)
241 errx(1, "internal error for INDEFINITE form");
249 doit (const char *filename
)
251 int fd
= open (filename
, O_RDONLY
);
258 err (1, "opening %s for read", filename
);
259 if (fstat (fd
, &sb
) < 0)
260 err (1, "stat %s", filename
);
263 if (read (fd
, buf
, len
) != len
)
264 errx (1, "read failed");
266 ret
= loop (buf
, len
, 0);
272 static int version_flag
;
273 static int help_flag
;
274 struct getargs args
[] = {
275 { "indent", 0, arg_negative_flag
, &indent_flag
},
276 { "version", 0, arg_flag
, &version_flag
},
277 { "help", 0, arg_flag
, &help_flag
}
279 int num_args
= sizeof(args
) / sizeof(args
[0]);
284 arg_printusage(args
, num_args
, NULL
, "dump-file");
289 main(int argc
, char **argv
)
293 setprogname (argv
[0]);
294 initialize_asn1_error_table ();
295 if(getarg(args
, num_args
, argc
, argv
, &optidx
))
307 return doit (argv
[0]);