include <sys/socket.h> and fix ifdef wrapping for <sys/un.h>
[heimdal.git] / lib / asn1 / asn1_print.c
blob279e969b06b826e713a78d7c5b82bc6ac267993a
1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "der_locl.h"
37 #include <com_err.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <getarg.h>
41 #include <err.h>
42 #include <der.h>
44 RCSID("$Id$");
46 static int indent_flag = 1;
48 static unsigned long indefinite_form_loop;
49 static unsigned long indefinite_form_loop_max = 10000;
51 static size_t
52 loop (unsigned char *buf, size_t len, int indent)
54 unsigned char *start_buf = buf;
56 while (len > 0) {
57 int ret;
58 Der_class class;
59 Der_type type;
60 unsigned int tag;
61 size_t sz;
62 size_t length;
63 size_t loop_length = 0;
64 int end_tag = 0;
65 const char *tagname;
67 ret = der_get_tag (buf, len, &class, &type, &tag, &sz);
68 if (ret)
69 errx (1, "der_get_tag: %s", error_message (ret));
70 if (sz > len)
71 errx (1, "unreasonable length (%u) > %u",
72 (unsigned)sz, (unsigned)len);
73 buf += sz;
74 len -= sz;
75 if (indent_flag) {
76 int i;
77 for (i = 0; i < indent; ++i)
78 printf (" ");
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);
84 else
85 printf ("tag %d = ", tag);
86 ret = der_get_length (buf, len, &length, &sz);
87 if (ret)
88 errx (1, "der_get_tag: %s", error_message (ret));
89 if (sz > len)
90 errx (1, "unreasonable tag length (%u) > %u",
91 (unsigned)sz, (unsigned)len);
92 buf += sz;
93 len -= sz;
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*");
100 } else {
101 fflush(stdout);
102 errx(1, "indef form used on unsupported object");
104 end_tag = 1;
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++;
109 length = len;
110 } else if (length > len) {
111 printf("\n");
112 fflush(stdout);
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);
118 if (type == CONS) {
119 printf("\n");
120 loop_length = loop (buf, length, indent + 2);
121 } else {
122 printf(" IMPLICIT content\n");
124 } else if (class == ASN1_C_UNIV) {
125 switch (tag) {
126 case UT_EndOfContent:
127 printf (" INDEFINITE length was %lu\n",
128 (unsigned long)(buf - start_buf));
129 break;
130 case UT_Set :
131 case UT_Sequence :
132 printf ("%lu bytes {\n", (unsigned long)length);
133 loop_length = loop (buf, length, indent + 2);
134 if (indent_flag) {
135 int i;
136 for (i = 0; i < indent; ++i)
137 printf (" ");
138 printf ("}\n");
139 } else
140 printf ("} indent = %d\n", indent / 2);
141 break;
142 case UT_Integer : {
143 int val;
145 if (length <= sizeof(val)) {
146 ret = der_get_integer (buf, length, &val, NULL);
147 if (ret)
148 errx (1, "der_get_integer: %s", error_message (ret));
149 printf ("integer %d\n", val);
150 } else {
151 heim_integer vali;
152 char *p;
154 ret = der_get_heim_integer(buf, length, &vali, NULL);
155 if (ret)
156 errx (1, "der_get_heim_integer: %s",
157 error_message (ret));
158 ret = der_print_hex_heim_integer(&vali, &p);
159 if (ret)
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);
164 free(p);
166 break;
168 case UT_OctetString : {
169 heim_octet_string str;
170 int i;
171 unsigned char *uc;
173 ret = der_get_octet_string (buf, length, &str, NULL);
174 if (ret)
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]);
180 printf ("\n");
181 free (str.data);
182 break;
184 case UT_IA5String :
185 case UT_PrintableString : {
186 heim_printable_string str;
187 unsigned char *s;
188 size_t n;
190 memset(&str, 0, sizeof(str));
192 ret = der_get_printable_string (buf, length, &str, NULL);
193 if (ret)
194 errx (1, "der_get_general_string: %s",
195 error_message (ret));
196 s = str.data;
197 printf("\"");
198 for (n = 0; n < str.length; n++) {
199 if (isprint((int)s[n]))
200 printf ("%c", s[n]);
201 else
202 printf ("#%02x", s[n]);
204 printf("\"\n");
205 der_free_printable_string(&str);
206 break;
208 case UT_GeneralizedTime :
209 case UT_GeneralString :
210 case UT_VisibleString :
211 case UT_UTF8String : {
212 heim_general_string str;
214 ret = der_get_general_string (buf, length, &str, NULL);
215 if (ret)
216 errx (1, "der_get_general_string: %s",
217 error_message (ret));
218 printf ("\"%s\"\n", str);
219 free (str);
220 break;
222 case UT_OID: {
223 heim_oid o;
224 char *p;
226 ret = der_get_oid(buf, length, &o, NULL);
227 if (ret)
228 errx (1, "der_get_oid: %s", error_message (ret));
229 ret = der_print_heim_oid(&o, '.', &p);
230 der_free_oid(&o);
231 if (ret)
232 errx (1, "der_print_heim_oid: %s", error_message (ret));
233 printf("%s\n", p);
234 free(p);
236 break;
238 case UT_Enumerated: {
239 int num;
241 ret = der_get_integer (buf, length, &num, NULL);
242 if (ret)
243 errx (1, "der_get_enum: %s", error_message (ret));
245 printf("%u\n", num);
246 break;
248 default :
249 printf ("%lu bytes\n", (unsigned long)length);
250 break;
253 if (end_tag) {
254 if (loop_length == 0)
255 errx(1, "zero length INDEFINITE data ? indent = %d\n",
256 indent / 2);
257 if (loop_length < length)
258 length = loop_length;
259 if (indefinite_form_loop == 0)
260 errx(1, "internal error in indefinite form loop detection");
261 indefinite_form_loop--;
262 } else if (loop_length)
263 errx(1, "internal error for INDEFINITE form");
264 buf += length;
265 len -= length;
267 return 0;
270 static int
271 doit (const char *filename)
273 int fd = open (filename, O_RDONLY);
274 struct stat sb;
275 unsigned char *buf;
276 size_t len;
277 int ret;
279 if(fd < 0)
280 err (1, "opening %s for read", filename);
281 if (fstat (fd, &sb) < 0)
282 err (1, "stat %s", filename);
283 len = sb.st_size;
284 buf = emalloc (len);
285 if (read (fd, buf, len) != len)
286 errx (1, "read failed");
287 close (fd);
288 ret = loop (buf, len, 0);
289 free (buf);
290 return ret;
294 static int version_flag;
295 static int help_flag;
296 struct getargs args[] = {
297 { "indent", 0, arg_negative_flag, &indent_flag },
298 { "version", 0, arg_flag, &version_flag },
299 { "help", 0, arg_flag, &help_flag }
301 int num_args = sizeof(args) / sizeof(args[0]);
303 static void
304 usage(int code)
306 arg_printusage(args, num_args, NULL, "dump-file");
307 exit(code);
311 main(int argc, char **argv)
313 int optidx = 0;
315 setprogname (argv[0]);
316 initialize_asn1_error_table ();
317 if(getarg(args, num_args, argc, argv, &optidx))
318 usage(1);
319 if(help_flag)
320 usage(0);
321 if(version_flag) {
322 print_version(NULL);
323 exit(0);
325 argv += optidx;
326 argc -= optidx;
327 if (argc != 1)
328 usage (1);
329 return doit (argv[0]);