No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / asn1 / asn1_print.c
blobd2549a8a51a40cf158ca9e9426029af9e2445550
1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "der_locl.h"
35 #include <com_err.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <getarg.h>
39 #include <err.h>
40 #include <der.h>
42 __RCSID("$Heimdal: asn1_print.c 19539 2006-12-28 17:15:05Z lha $"
43 "$NetBSD$");
45 static int indent_flag = 1;
47 static unsigned long indefinite_form_loop;
48 static unsigned long indefinite_form_loop_max = 10000;
50 static size_t
51 loop (unsigned char *buf, size_t len, int indent)
53 unsigned char *start_buf = buf;
55 while (len > 0) {
56 int ret;
57 Der_class class;
58 Der_type type;
59 unsigned int tag;
60 size_t sz;
61 size_t length;
62 size_t loop_length = 0;
63 int end_tag = 0;
64 const char *tagname;
66 ret = der_get_tag (buf, len, &class, &type, &tag, &sz);
67 if (ret)
68 errx (1, "der_get_tag: %s", error_message (ret));
69 if (sz > len)
70 errx (1, "unreasonable length (%u) > %u",
71 (unsigned)sz, (unsigned)len);
72 buf += sz;
73 len -= sz;
74 if (indent_flag) {
75 int i;
76 for (i = 0; i < indent; ++i)
77 printf (" ");
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);
83 else
84 printf ("tag %d = ", tag);
85 ret = der_get_length (buf, len, &length, &sz);
86 if (ret)
87 errx (1, "der_get_tag: %s", error_message (ret));
88 if (sz > len)
89 errx (1, "unreasonable tag length (%u) > %u",
90 (unsigned)sz, (unsigned)len);
91 buf += sz;
92 len -= sz;
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*");
99 } else {
100 fflush(stdout);
101 errx(1, "indef form used on unsupported object");
103 end_tag = 1;
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++;
108 length = len;
109 } else if (length > len) {
110 printf("\n");
111 fflush(stdout);
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);
117 if (type == CONS) {
118 printf("\n");
119 loop_length = loop (buf, length, indent + 2);
120 } else {
121 printf(" IMPLICIT content\n");
123 } else if (class == ASN1_C_UNIV) {
124 switch (tag) {
125 case UT_EndOfContent:
126 printf (" INDEFINITE length was %lu\n",
127 (unsigned long)(buf - start_buf));
128 break;
129 case UT_Set :
130 case UT_Sequence :
131 printf ("%lu bytes {\n", (unsigned long)length);
132 loop_length = loop (buf, length, indent + 2);
133 if (indent_flag) {
134 int i;
135 for (i = 0; i < indent; ++i)
136 printf (" ");
137 printf ("}\n");
138 } else
139 printf ("} indent = %d\n", indent / 2);
140 break;
141 case UT_Integer : {
142 int val;
144 if (length <= sizeof(val)) {
145 ret = der_get_integer (buf, length, &val, NULL);
146 if (ret)
147 errx (1, "der_get_integer: %s", error_message (ret));
148 printf ("integer %d\n", val);
149 } else {
150 heim_integer vali;
151 char *p;
153 ret = der_get_heim_integer(buf, length, &vali, NULL);
154 if (ret)
155 errx (1, "der_get_heim_integer: %s",
156 error_message (ret));
157 ret = der_print_hex_heim_integer(&vali, &p);
158 if (ret)
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);
163 free(p);
165 break;
167 case UT_OctetString : {
168 heim_octet_string str;
169 int i;
170 unsigned char *uc;
172 ret = der_get_octet_string (buf, length, &str, NULL);
173 if (ret)
174 errx (1, "der_get_octet_string: %s", error_message (ret));
175 printf ("(length %lu), ", (unsigned long)length);
176 uc = (unsigned char *)str.data;
177 for (i = 0; i < min(16,length); ++i)
178 printf ("%02x", uc[i]);
179 printf ("\n");
180 free (str.data);
181 break;
183 case UT_GeneralizedTime :
184 case UT_GeneralString :
185 case UT_PrintableString :
186 case UT_VisibleString : {
187 heim_general_string str;
189 ret = der_get_general_string (buf, length, &str, NULL);
190 if (ret)
191 errx (1, "der_get_general_string: %s",
192 error_message (ret));
193 printf ("\"%s\"\n", str);
194 free (str);
195 break;
197 case UT_OID: {
198 heim_oid o;
199 char *p;
201 ret = der_get_oid(buf, length, &o, NULL);
202 if (ret)
203 errx (1, "der_get_oid: %s", error_message (ret));
204 ret = der_print_heim_oid(&o, '.', &p);
205 der_free_oid(&o);
206 if (ret)
207 errx (1, "der_print_heim_oid: %s", error_message (ret));
208 printf("%s\n", p);
209 free(p);
211 break;
213 case UT_Enumerated: {
214 int num;
216 ret = der_get_integer (buf, length, &num, NULL);
217 if (ret)
218 errx (1, "der_get_enum: %s", error_message (ret));
220 printf("%u\n", num);
221 break;
223 default :
224 printf ("%lu bytes\n", (unsigned long)length);
225 break;
228 if (end_tag) {
229 if (loop_length == 0)
230 errx(1, "zero length INDEFINITE data ? indent = %d\n",
231 indent / 2);
232 if (loop_length < length)
233 length = loop_length;
234 if (indefinite_form_loop == 0)
235 errx(1, "internal error in indefinite form loop detection");
236 indefinite_form_loop--;
237 } else if (loop_length)
238 errx(1, "internal error for INDEFINITE form");
239 buf += length;
240 len -= length;
242 return 0;
245 static int
246 doit (const char *filename)
248 int fd = open (filename, O_RDONLY);
249 struct stat sb;
250 unsigned char *buf;
251 size_t len;
252 int ret;
254 if(fd < 0)
255 err (1, "opening %s for read", filename);
256 if (fstat (fd, &sb) < 0)
257 err (1, "stat %s", filename);
258 len = sb.st_size;
259 buf = emalloc (len);
260 if (read (fd, buf, len) != len)
261 errx (1, "read failed");
262 close (fd);
263 ret = loop (buf, len, 0);
264 free (buf);
265 return 0;
269 static int version_flag;
270 static int help_flag;
271 struct getargs args[] = {
272 { "indent", 0, arg_negative_flag, &indent_flag },
273 { "version", 0, arg_flag, &version_flag },
274 { "help", 0, arg_flag, &help_flag }
276 int num_args = sizeof(args) / sizeof(args[0]);
278 static void
279 usage(int code)
281 arg_printusage(args, num_args, NULL, "dump-file");
282 exit(code);
286 main(int argc, char **argv)
288 int optidx = 0;
290 setprogname (argv[0]);
291 initialize_asn1_error_table ();
292 if(getarg(args, num_args, argc, argv, &optidx))
293 usage(1);
294 if(help_flag)
295 usage(0);
296 if(version_flag) {
297 print_version(NULL);
298 exit(0);
300 argv += optidx;
301 argc -= optidx;
302 if (argc != 1)
303 usage (1);
304 return doit (argv[0]);