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>
44 static int indent_flag
= 1;
45 static int inner_flag
= 0;
47 static unsigned long indefinite_form_loop
;
48 static unsigned long indefinite_form_loop_max
= 10000;
51 loop (unsigned char *buf
, size_t len
, int indent
)
53 unsigned char *start_buf
= buf
;
62 size_t loop_length
= 0;
66 ret
= der_get_tag (buf
, len
, &class, &type
, &tag
, &sz
);
68 errx (1, "der_get_tag: %s", error_message (ret
));
70 errx (1, "unreasonable length (%u) > %u",
71 (unsigned)sz
, (unsigned)len
);
76 for (i
= 0; i
< indent
; ++i
)
79 printf ("%s %s ", der_get_class_name(class), der_get_type_name(type
));
80 tagname
= der_get_tag_name(tag
);
81 if (class == ASN1_C_UNIV
&& tagname
!= NULL
)
82 printf ("%s = ", tagname
);
84 printf ("tag %d = ", tag
);
85 ret
= der_get_length (buf
, len
, &length
, &sz
);
87 errx (1, "der_get_tag: %s", error_message (ret
));
89 errx (1, "unreasonable tag length (%u) > %u",
90 (unsigned)sz
, (unsigned)len
);
93 if (length
== ASN1_INDEFINITE
) {
94 if ((class == ASN1_C_UNIV
&& type
== PRIM
&& tag
== UT_OctetString
) ||
95 (class == ASN1_C_CONTEXT
&& type
== CONS
) ||
96 (class == ASN1_C_UNIV
&& type
== CONS
&& tag
== UT_Sequence
) ||
97 (class == ASN1_C_UNIV
&& type
== CONS
&& tag
== UT_Set
)) {
98 printf("*INDEFINITE FORM*");
101 errx(1, "indef form used on unsupported object");
104 if (indefinite_form_loop
> indefinite_form_loop_max
)
105 errx(1, "indefinite form used recursively more then %lu "
106 "times, aborting", indefinite_form_loop_max
);
107 indefinite_form_loop
++;
109 } else if (length
> len
) {
112 errx (1, "unreasonable inner length (%u) > %u",
113 (unsigned)length
, (unsigned)len
);
115 if (class == ASN1_C_CONTEXT
|| class == ASN1_C_APPL
) {
116 printf ("%lu bytes [%u]", (unsigned long)length
, tag
);
119 loop_length
= loop (buf
, length
, indent
+ 2);
121 printf(" IMPLICIT content\n");
123 } else if (class == ASN1_C_UNIV
) {
125 case UT_EndOfContent
:
126 printf (" INDEFINITE length was %lu\n",
127 (unsigned long)(buf
- start_buf
));
131 printf ("%lu bytes {\n", (unsigned long)length
);
132 loop_length
= loop (buf
, length
, indent
+ 2);
135 for (i
= 0; i
< indent
; ++i
)
139 printf ("} indent = %d\n", indent
/ 2);
144 if (length
<= sizeof(val
)) {
145 ret
= der_get_integer (buf
, length
, &val
, NULL
);
147 errx (1, "der_get_integer: %s", error_message (ret
));
148 printf ("integer %d\n", val
);
153 ret
= der_get_heim_integer(buf
, length
, &vali
, NULL
);
155 errx (1, "der_get_heim_integer: %s",
156 error_message (ret
));
157 ret
= der_print_hex_heim_integer(&vali
, &p
);
159 errx (1, "der_print_hex_heim_integer: %s",
160 error_message (ret
));
161 printf ("BIG NUM integer: length %lu %s\n",
162 (unsigned long)length
, p
);
167 case UT_OctetString
: {
168 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
);
181 ret
= der_get_tag(str
.data
, str
.length
,
182 &class2
, &type2
, &tag2
, &sz
);
183 if (ret
|| sz
> str
.length
||
184 type2
!= CONS
|| tag2
!= UT_Sequence
)
185 goto just_an_octet_string
;
188 loop (str
.data
, str
.length
, indent
+ 2);
189 for (i
= 0; i
< indent
; ++i
)
196 just_an_octet_string
:
197 uc
= (unsigned char *)str
.data
;
198 for (i
= 0; i
< min(16,length
); ++i
)
199 printf ("%02x", uc
[i
]);
206 case UT_PrintableString
: {
207 heim_printable_string str
;
211 memset(&str
, 0, sizeof(str
));
213 ret
= der_get_printable_string (buf
, length
, &str
, NULL
);
215 errx (1, "der_get_general_string: %s",
216 error_message (ret
));
219 for (n
= 0; n
< str
.length
; n
++) {
220 if (isprint((int)s
[n
]))
223 printf ("#%02x", s
[n
]);
226 der_free_printable_string(&str
);
229 case UT_GeneralizedTime
:
230 case UT_GeneralString
:
231 case UT_VisibleString
:
232 case UT_UTF8String
: {
233 heim_general_string str
;
235 ret
= der_get_general_string (buf
, length
, &str
, NULL
);
237 errx (1, "der_get_general_string: %s",
238 error_message (ret
));
239 printf ("\"%s\"\n", str
);
247 ret
= der_get_oid(buf
, length
, &o
, NULL
);
249 errx (1, "der_get_oid: %s", error_message (ret
));
250 ret
= der_print_heim_oid(&o
, '.', &p
);
253 errx (1, "der_print_heim_oid: %s", error_message (ret
));
259 case UT_Enumerated
: {
262 ret
= der_get_integer (buf
, length
, &num
, NULL
);
264 errx (1, "der_get_enum: %s", error_message (ret
));
270 printf ("%lu bytes\n", (unsigned long)length
);
275 if (loop_length
== 0)
276 errx(1, "zero length INDEFINITE data ? indent = %d\n",
278 if (loop_length
< length
)
279 length
= loop_length
;
280 if (indefinite_form_loop
== 0)
281 errx(1, "internal error in indefinite form loop detection");
282 indefinite_form_loop
--;
283 } else if (loop_length
)
284 errx(1, "internal error for INDEFINITE form");
292 doit (const char *filename
)
294 int fd
= open (filename
, O_RDONLY
);
301 err (1, "opening %s for read", filename
);
302 if (fstat (fd
, &sb
) < 0)
303 err (1, "stat %s", filename
);
306 if (read (fd
, buf
, len
) != len
)
307 errx (1, "read failed");
309 ret
= loop (buf
, len
, 0);
315 static int version_flag
;
316 static int help_flag
;
317 struct getargs args
[] = {
318 { "indent", 0, arg_negative_flag
, &indent_flag
, NULL
, NULL
},
319 { "inner", 0, arg_flag
, &inner_flag
,
320 "try to parse inner structures of OCTET STRING", NULL
},
321 { "version", 0, arg_flag
, &version_flag
, NULL
, NULL
},
322 { "help", 0, arg_flag
, &help_flag
, NULL
, NULL
}
324 int num_args
= sizeof(args
) / sizeof(args
[0]);
329 arg_printusage(args
, num_args
, NULL
, "dump-file");
334 main(int argc
, char **argv
)
338 setprogname (argv
[0]);
339 initialize_asn1_error_table ();
340 if(getarg(args
, num_args
, argc
, argv
, &optidx
))
352 return doit (argv
[0]);