1 /* $NetBSD: asn1_print.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <krb5/com_err.h>
40 #include <sys/types.h>
42 #include <krb5/getarg.h>
46 static int indent_flag
= 1;
47 static int inner_flag
= 0;
49 static unsigned long indefinite_form_loop
;
50 static unsigned long indefinite_form_loop_max
= 10000;
53 loop (unsigned char *buf
, size_t len
, int indent
)
55 unsigned char *start_buf
= buf
;
64 size_t loop_length
= 0;
68 ret
= der_get_tag (buf
, len
, &class, &type
, &tag
, &sz
);
70 errx (1, "der_get_tag: %s", error_message (ret
));
72 errx (1, "unreasonable length (%u) > %u",
73 (unsigned)sz
, (unsigned)len
);
78 for (i
= 0; i
< indent
; ++i
)
81 printf ("%s %s ", der_get_class_name(class), der_get_type_name(type
));
82 tagname
= der_get_tag_name(tag
);
83 if (class == ASN1_C_UNIV
&& tagname
!= NULL
)
84 printf ("%s = ", tagname
);
86 printf ("tag %d = ", tag
);
87 ret
= der_get_length (buf
, len
, &length
, &sz
);
89 errx (1, "der_get_tag: %s", error_message (ret
));
91 errx (1, "unreasonable tag length (%u) > %u",
92 (unsigned)sz
, (unsigned)len
);
95 if (length
== ASN1_INDEFINITE
) {
96 if ((class == ASN1_C_UNIV
&& type
== PRIM
&& tag
== UT_OctetString
) ||
97 (class == ASN1_C_CONTEXT
&& type
== CONS
) ||
98 (class == ASN1_C_UNIV
&& type
== CONS
&& tag
== UT_Sequence
) ||
99 (class == ASN1_C_UNIV
&& type
== CONS
&& tag
== UT_Set
)) {
100 printf("*INDEFINITE FORM*");
103 errx(1, "indef form used on unsupported object");
106 if (indefinite_form_loop
> indefinite_form_loop_max
)
107 errx(1, "indefinite form used recursively more then %lu "
108 "times, aborting", indefinite_form_loop_max
);
109 indefinite_form_loop
++;
111 } else if (length
> len
) {
114 errx (1, "unreasonable inner length (%u) > %u",
115 (unsigned)length
, (unsigned)len
);
117 if (class == ASN1_C_CONTEXT
|| class == ASN1_C_APPL
) {
118 printf ("%lu bytes [%u]", (unsigned long)length
, tag
);
121 loop_length
= loop (buf
, length
, indent
+ 2);
123 printf(" IMPLICIT content\n");
125 } else if (class == ASN1_C_UNIV
) {
127 case UT_EndOfContent
:
128 printf (" INDEFINITE length was %lu\n",
129 (unsigned long)(buf
- start_buf
));
133 printf ("%lu bytes {\n", (unsigned long)length
);
134 loop_length
= loop (buf
, length
, indent
+ 2);
137 for (i
= 0; i
< indent
; ++i
)
141 printf ("} indent = %d\n", indent
/ 2);
146 if (length
<= sizeof(val
)) {
147 ret
= der_get_integer (buf
, length
, &val
, NULL
);
149 errx (1, "der_get_integer: %s", error_message (ret
));
150 printf ("integer %d\n", val
);
155 ret
= der_get_heim_integer(buf
, length
, &vali
, NULL
);
157 errx (1, "der_get_heim_integer: %s",
158 error_message (ret
));
159 ret
= der_print_hex_heim_integer(&vali
, &p
);
161 errx (1, "der_print_hex_heim_integer: %s",
162 error_message (ret
));
163 printf ("BIG NUM integer: length %lu %s\n",
164 (unsigned long)length
, p
);
169 case UT_OctetString
: {
170 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
);
183 ret
= der_get_tag(str
.data
, str
.length
,
184 &class, &type
, &tag
, &sz
);
185 if (ret
|| sz
> str
.length
||
186 type
!= CONS
|| tag
!= UT_Sequence
)
187 goto just_an_octet_string
;
190 loop (str
.data
, str
.length
, indent
+ 2);
191 for (i
= 0; i
< indent
; ++i
)
198 just_an_octet_string
:
199 uc
= (unsigned char *)str
.data
;
200 for (i
= 0; i
< min(16,length
); ++i
)
201 printf ("%02x", uc
[i
]);
208 case UT_PrintableString
: {
209 heim_printable_string str
;
213 memset(&str
, 0, sizeof(str
));
215 ret
= der_get_printable_string (buf
, length
, &str
, NULL
);
217 errx (1, "der_get_general_string: %s",
218 error_message (ret
));
221 for (n
= 0; n
< str
.length
; n
++) {
222 if (isprint((int)s
[n
]))
225 printf ("#%02x", s
[n
]);
228 der_free_printable_string(&str
);
231 case UT_GeneralizedTime
:
232 case UT_GeneralString
:
233 case UT_VisibleString
:
234 case UT_UTF8String
: {
235 heim_general_string str
;
237 ret
= der_get_general_string (buf
, length
, &str
, NULL
);
239 errx (1, "der_get_general_string: %s",
240 error_message (ret
));
241 printf ("\"%s\"\n", str
);
249 ret
= der_get_oid(buf
, length
, &o
, NULL
);
251 errx (1, "der_get_oid: %s", error_message (ret
));
252 ret
= der_print_heim_oid(&o
, '.', &p
);
255 errx (1, "der_print_heim_oid: %s", error_message (ret
));
261 case UT_Enumerated
: {
264 ret
= der_get_integer (buf
, length
, &num
, NULL
);
266 errx (1, "der_get_enum: %s", error_message (ret
));
272 printf ("%lu bytes\n", (unsigned long)length
);
277 if (loop_length
== 0)
278 errx(1, "zero length INDEFINITE data ? indent = %d\n",
280 if (loop_length
< length
)
281 length
= loop_length
;
282 if (indefinite_form_loop
== 0)
283 errx(1, "internal error in indefinite form loop detection");
284 indefinite_form_loop
--;
285 } else if (loop_length
)
286 errx(1, "internal error for INDEFINITE form");
294 doit (const char *filename
)
296 int fd
= open (filename
, O_RDONLY
);
303 err (1, "opening %s for read", filename
);
304 if (fstat (fd
, &sb
) < 0)
305 err (1, "stat %s", filename
);
308 if (read (fd
, buf
, len
) != len
)
309 errx (1, "read failed");
311 ret
= loop (buf
, len
, 0);
317 static int version_flag
;
318 static int help_flag
;
319 struct getargs args
[] = {
320 { "indent", 0, arg_negative_flag
, &indent_flag
},
321 { "inner", 0, arg_flag
, &inner_flag
, "try to parse inner structures of OCTET STRING" },
322 { "version", 0, arg_flag
, &version_flag
},
323 { "help", 0, arg_flag
, &help_flag
}
325 int num_args
= sizeof(args
) / sizeof(args
[0]);
330 arg_printusage(args
, num_args
, NULL
, "dump-file");
335 main(int argc
, char **argv
)
339 setprogname (argv
[0]);
340 initialize_asn1_error_table ();
341 if(getarg(args
, num_args
, argc
, argv
, &optidx
))
353 return doit (argv
[0]);