2 * Copyright (c) 1997 - 2002 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>
41 RCSID("$Id: asn1_print.c,v 1.11 2002/08/29 20:45:35 assar Exp $");
43 const char *class_names[] = {
50 const char *type_names[] = {
55 const char *tag_names[] = {
60 "OctetString", /* 4 */
75 "PrintableString", /* 19 */
80 "GeneralizedTime", /* 24 */
82 "VisibleString", /* 26 */
83 "GeneralString" /* 27 */
87 loop (unsigned char *buf, size_t len, int indent)
98 ret = der_get_tag (buf, len, &class, &type, &tag, &sz);
100 errx (1, "der_get_tag: %s", error_message (ret));
102 errx (1, "unreasonable length (%u) > %u",
103 (unsigned)sz, (unsigned)len);
106 for (i = 0; i < indent; ++i)
108 printf ("%s %s ", class_names[class], type_names[type]);
110 printf ("%s = ", tag_names[tag]);
112 printf ("tag %d = ", tag);
113 ret = der_get_length (buf, len, &length, &sz);
115 errx (1, "der_get_tag: %s", error_message (ret));
119 if (class == CONTEXT) {
120 printf ("[%d]\n", tag);
121 loop (buf, length, indent);
122 } else if (class == UNIV) {
126 loop (buf, length, indent + 2);
127 for (i = 0; i < indent; ++i)
134 ret = der_get_int (buf, length, &val, NULL);
136 errx (1, "der_get_int: %s", error_message (ret));
137 printf ("integer %d\n", val);
140 case UT_OctetString : {
145 ret = der_get_octet_string (buf, length, &str, NULL);
147 errx (1, "der_get_octet_string: %s", error_message (ret));
148 printf ("(length %lu), ", (unsigned long)length);
149 uc = (unsigned char *)str.data;
150 for (i = 0; i < 16; ++i)
151 printf ("%02x", uc[i]);
156 case UT_GeneralizedTime :
157 case UT_GeneralString : {
160 ret = der_get_general_string (buf, length, &str, NULL);
162 errx (1, "der_get_general_string: %s",
163 error_message (ret));
164 printf ("\"%s\"\n", str);
172 ret = der_get_oid(buf, length, &o, NULL);
174 errx (1, "der_get_oid: %s", error_message (ret));
176 for (i = 0; i < o.length ; i++)
177 printf("%d%s", o.components[i],
178 i < o.length - 1 ? "." : "");
184 printf ("%lu bytes\n", (unsigned long)length);
195 doit (const char *filename)
197 int fd = open (filename, O_RDONLY);
204 err (1, "opening %s for read", filename);
205 if (fstat (fd, &sb) < 0)
206 err (1, "stat %s", filename);
210 err (1, "malloc %u", (unsigned)len);
211 if (read (fd, buf, len) != len)
212 errx (1, "read failed");
214 ret = loop (buf, len, 0);
220 static int version_flag;
221 static int help_flag;
222 struct getargs args[] = {
223 { "version", 0, arg_flag, &version_flag },
224 { "help", 0, arg_flag, &help_flag }
226 int num_args = sizeof(args) / sizeof(args[0]);
231 arg_printusage(args, num_args, NULL, "dump-file");
236 main(int argc, char **argv)
240 setprogname (argv[0]);
241 initialize_asn1_error_table ();
242 if(getarg(args, num_args, argc, argv, &optind))
254 return doit (argv[0]);