Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / asn1 / asn1_print.c
blob668b4b693f01f8a1069f42b2f4c40af179078230
1 /* $NetBSD: asn1_print.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
3 /*
4 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
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
12 * are met:
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
35 * SUCH DAMAGE.
38 #include "der_locl.h"
39 #include <krb5/com_err.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <krb5/getarg.h>
43 #include <err.h>
44 #include <krb5/der.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;
52 static size_t
53 loop (unsigned char *buf, size_t len, int indent)
55 unsigned char *start_buf = buf;
57 while (len > 0) {
58 int ret;
59 Der_class class;
60 Der_type type;
61 unsigned int tag;
62 size_t sz;
63 size_t length;
64 size_t loop_length = 0;
65 int end_tag = 0;
66 const char *tagname;
68 ret = der_get_tag (buf, len, &class, &type, &tag, &sz);
69 if (ret)
70 errx (1, "der_get_tag: %s", error_message (ret));
71 if (sz > len)
72 errx (1, "unreasonable length (%u) > %u",
73 (unsigned)sz, (unsigned)len);
74 buf += sz;
75 len -= sz;
76 if (indent_flag) {
77 int i;
78 for (i = 0; i < indent; ++i)
79 printf (" ");
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);
85 else
86 printf ("tag %d = ", tag);
87 ret = der_get_length (buf, len, &length, &sz);
88 if (ret)
89 errx (1, "der_get_tag: %s", error_message (ret));
90 if (sz > len)
91 errx (1, "unreasonable tag length (%u) > %u",
92 (unsigned)sz, (unsigned)len);
93 buf += sz;
94 len -= sz;
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*");
101 } else {
102 fflush(stdout);
103 errx(1, "indef form used on unsupported object");
105 end_tag = 1;
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++;
110 length = len;
111 } else if (length > len) {
112 printf("\n");
113 fflush(stdout);
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);
119 if (type == CONS) {
120 printf("\n");
121 loop_length = loop (buf, length, indent + 2);
122 } else {
123 printf(" IMPLICIT content\n");
125 } else if (class == ASN1_C_UNIV) {
126 switch (tag) {
127 case UT_EndOfContent:
128 printf (" INDEFINITE length was %lu\n",
129 (unsigned long)(buf - start_buf));
130 break;
131 case UT_Set :
132 case UT_Sequence :
133 printf ("%lu bytes {\n", (unsigned long)length);
134 loop_length = loop (buf, length, indent + 2);
135 if (indent_flag) {
136 int i;
137 for (i = 0; i < indent; ++i)
138 printf (" ");
139 printf ("}\n");
140 } else
141 printf ("} indent = %d\n", indent / 2);
142 break;
143 case UT_Integer : {
144 int val;
146 if (length <= sizeof(val)) {
147 ret = der_get_integer (buf, length, &val, NULL);
148 if (ret)
149 errx (1, "der_get_integer: %s", error_message (ret));
150 printf ("integer %d\n", val);
151 } else {
152 heim_integer vali;
153 char *p;
155 ret = der_get_heim_integer(buf, length, &vali, NULL);
156 if (ret)
157 errx (1, "der_get_heim_integer: %s",
158 error_message (ret));
159 ret = der_print_hex_heim_integer(&vali, &p);
160 if (ret)
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);
165 free(p);
167 break;
169 case UT_OctetString : {
170 heim_octet_string str;
171 size_t i;
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);
178 if (inner_flag) {
179 Der_class class;
180 Der_type type;
181 unsigned int tag;
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;
189 printf("{\n");
190 loop (str.data, str.length, indent + 2);
191 for (i = 0; i < indent; ++i)
192 printf (" ");
193 printf ("}\n");
195 } else {
196 unsigned char *uc;
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]);
202 printf ("\n");
204 free (str.data);
205 break;
207 case UT_IA5String :
208 case UT_PrintableString : {
209 heim_printable_string str;
210 unsigned char *s;
211 size_t n;
213 memset(&str, 0, sizeof(str));
215 ret = der_get_printable_string (buf, length, &str, NULL);
216 if (ret)
217 errx (1, "der_get_general_string: %s",
218 error_message (ret));
219 s = str.data;
220 printf("\"");
221 for (n = 0; n < str.length; n++) {
222 if (isprint((int)s[n]))
223 printf ("%c", s[n]);
224 else
225 printf ("#%02x", s[n]);
227 printf("\"\n");
228 der_free_printable_string(&str);
229 break;
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);
238 if (ret)
239 errx (1, "der_get_general_string: %s",
240 error_message (ret));
241 printf ("\"%s\"\n", str);
242 free (str);
243 break;
245 case UT_OID: {
246 heim_oid o;
247 char *p;
249 ret = der_get_oid(buf, length, &o, NULL);
250 if (ret)
251 errx (1, "der_get_oid: %s", error_message (ret));
252 ret = der_print_heim_oid(&o, '.', &p);
253 der_free_oid(&o);
254 if (ret)
255 errx (1, "der_print_heim_oid: %s", error_message (ret));
256 printf("%s\n", p);
257 free(p);
259 break;
261 case UT_Enumerated: {
262 int num;
264 ret = der_get_integer (buf, length, &num, NULL);
265 if (ret)
266 errx (1, "der_get_enum: %s", error_message (ret));
268 printf("%u\n", num);
269 break;
271 default :
272 printf ("%lu bytes\n", (unsigned long)length);
273 break;
276 if (end_tag) {
277 if (loop_length == 0)
278 errx(1, "zero length INDEFINITE data ? indent = %d\n",
279 indent / 2);
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");
287 buf += length;
288 len -= length;
290 return 0;
293 static int
294 doit (const char *filename)
296 int fd = open (filename, O_RDONLY);
297 struct stat sb;
298 unsigned char *buf;
299 size_t len;
300 int ret;
302 if(fd < 0)
303 err (1, "opening %s for read", filename);
304 if (fstat (fd, &sb) < 0)
305 err (1, "stat %s", filename);
306 len = sb.st_size;
307 buf = emalloc (len);
308 if (read (fd, buf, len) != len)
309 errx (1, "read failed");
310 close (fd);
311 ret = loop (buf, len, 0);
312 free (buf);
313 return ret;
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]);
327 static void
328 usage(int code)
330 arg_printusage(args, num_args, NULL, "dump-file");
331 exit(code);
335 main(int argc, char **argv)
337 int optidx = 0;
339 setprogname (argv[0]);
340 initialize_asn1_error_table ();
341 if(getarg(args, num_args, argc, argv, &optidx))
342 usage(1);
343 if(help_flag)
344 usage(0);
345 if(version_flag) {
346 print_version(NULL);
347 exit(0);
349 argv += optidx;
350 argc -= optidx;
351 if (argc != 1)
352 usage (1);
353 return doit (argv[0]);