1*0a6a1f1dSLionel Sambuc /* $NetBSD: asn1_print.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ebfedea0SLionel Sambuc *
10ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc * are met:
13ebfedea0SLionel Sambuc *
14ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc *
17ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
19ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
22ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
23ebfedea0SLionel Sambuc * without specific prior written permission.
24ebfedea0SLionel Sambuc *
25ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ebfedea0SLionel Sambuc * SUCH DAMAGE.
36ebfedea0SLionel Sambuc */
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc #include "der_locl.h"
39ebfedea0SLionel Sambuc #include <krb5/com_err.h>
40ebfedea0SLionel Sambuc #include <sys/types.h>
41ebfedea0SLionel Sambuc #include <sys/stat.h>
42ebfedea0SLionel Sambuc #include <krb5/getarg.h>
43ebfedea0SLionel Sambuc #include <err.h>
44ebfedea0SLionel Sambuc #include <krb5/der.h>
45ebfedea0SLionel Sambuc
46ebfedea0SLionel Sambuc static int indent_flag = 1;
47*0a6a1f1dSLionel Sambuc static int inner_flag = 0;
48ebfedea0SLionel Sambuc
49ebfedea0SLionel Sambuc static unsigned long indefinite_form_loop;
50ebfedea0SLionel Sambuc static unsigned long indefinite_form_loop_max = 10000;
51ebfedea0SLionel Sambuc
52ebfedea0SLionel Sambuc static size_t
loop(unsigned char * buf,size_t len,int indent)53ebfedea0SLionel Sambuc loop (unsigned char *buf, size_t len, int indent)
54ebfedea0SLionel Sambuc {
55ebfedea0SLionel Sambuc unsigned char *start_buf = buf;
56ebfedea0SLionel Sambuc
57ebfedea0SLionel Sambuc while (len > 0) {
58ebfedea0SLionel Sambuc int ret;
59ebfedea0SLionel Sambuc Der_class class;
60ebfedea0SLionel Sambuc Der_type type;
61ebfedea0SLionel Sambuc unsigned int tag;
62ebfedea0SLionel Sambuc size_t sz;
63ebfedea0SLionel Sambuc size_t length;
64ebfedea0SLionel Sambuc size_t loop_length = 0;
65ebfedea0SLionel Sambuc int end_tag = 0;
66ebfedea0SLionel Sambuc const char *tagname;
67ebfedea0SLionel Sambuc
68ebfedea0SLionel Sambuc ret = der_get_tag (buf, len, &class, &type, &tag, &sz);
69ebfedea0SLionel Sambuc if (ret)
70ebfedea0SLionel Sambuc errx (1, "der_get_tag: %s", error_message (ret));
71ebfedea0SLionel Sambuc if (sz > len)
72ebfedea0SLionel Sambuc errx (1, "unreasonable length (%u) > %u",
73ebfedea0SLionel Sambuc (unsigned)sz, (unsigned)len);
74ebfedea0SLionel Sambuc buf += sz;
75ebfedea0SLionel Sambuc len -= sz;
76ebfedea0SLionel Sambuc if (indent_flag) {
77ebfedea0SLionel Sambuc int i;
78ebfedea0SLionel Sambuc for (i = 0; i < indent; ++i)
79ebfedea0SLionel Sambuc printf (" ");
80ebfedea0SLionel Sambuc }
81ebfedea0SLionel Sambuc printf ("%s %s ", der_get_class_name(class), der_get_type_name(type));
82ebfedea0SLionel Sambuc tagname = der_get_tag_name(tag);
83ebfedea0SLionel Sambuc if (class == ASN1_C_UNIV && tagname != NULL)
84ebfedea0SLionel Sambuc printf ("%s = ", tagname);
85ebfedea0SLionel Sambuc else
86ebfedea0SLionel Sambuc printf ("tag %d = ", tag);
87ebfedea0SLionel Sambuc ret = der_get_length (buf, len, &length, &sz);
88ebfedea0SLionel Sambuc if (ret)
89ebfedea0SLionel Sambuc errx (1, "der_get_tag: %s", error_message (ret));
90ebfedea0SLionel Sambuc if (sz > len)
91ebfedea0SLionel Sambuc errx (1, "unreasonable tag length (%u) > %u",
92ebfedea0SLionel Sambuc (unsigned)sz, (unsigned)len);
93ebfedea0SLionel Sambuc buf += sz;
94ebfedea0SLionel Sambuc len -= sz;
95ebfedea0SLionel Sambuc if (length == ASN1_INDEFINITE) {
96ebfedea0SLionel Sambuc if ((class == ASN1_C_UNIV && type == PRIM && tag == UT_OctetString) ||
97ebfedea0SLionel Sambuc (class == ASN1_C_CONTEXT && type == CONS) ||
98ebfedea0SLionel Sambuc (class == ASN1_C_UNIV && type == CONS && tag == UT_Sequence) ||
99ebfedea0SLionel Sambuc (class == ASN1_C_UNIV && type == CONS && tag == UT_Set)) {
100ebfedea0SLionel Sambuc printf("*INDEFINITE FORM*");
101ebfedea0SLionel Sambuc } else {
102ebfedea0SLionel Sambuc fflush(stdout);
103ebfedea0SLionel Sambuc errx(1, "indef form used on unsupported object");
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc end_tag = 1;
106ebfedea0SLionel Sambuc if (indefinite_form_loop > indefinite_form_loop_max)
107ebfedea0SLionel Sambuc errx(1, "indefinite form used recursively more then %lu "
108ebfedea0SLionel Sambuc "times, aborting", indefinite_form_loop_max);
109ebfedea0SLionel Sambuc indefinite_form_loop++;
110ebfedea0SLionel Sambuc length = len;
111ebfedea0SLionel Sambuc } else if (length > len) {
112ebfedea0SLionel Sambuc printf("\n");
113ebfedea0SLionel Sambuc fflush(stdout);
114ebfedea0SLionel Sambuc errx (1, "unreasonable inner length (%u) > %u",
115ebfedea0SLionel Sambuc (unsigned)length, (unsigned)len);
116ebfedea0SLionel Sambuc }
117ebfedea0SLionel Sambuc if (class == ASN1_C_CONTEXT || class == ASN1_C_APPL) {
118ebfedea0SLionel Sambuc printf ("%lu bytes [%u]", (unsigned long)length, tag);
119ebfedea0SLionel Sambuc if (type == CONS) {
120ebfedea0SLionel Sambuc printf("\n");
121ebfedea0SLionel Sambuc loop_length = loop (buf, length, indent + 2);
122ebfedea0SLionel Sambuc } else {
123ebfedea0SLionel Sambuc printf(" IMPLICIT content\n");
124ebfedea0SLionel Sambuc }
125ebfedea0SLionel Sambuc } else if (class == ASN1_C_UNIV) {
126ebfedea0SLionel Sambuc switch (tag) {
127ebfedea0SLionel Sambuc case UT_EndOfContent:
128ebfedea0SLionel Sambuc printf (" INDEFINITE length was %lu\n",
129ebfedea0SLionel Sambuc (unsigned long)(buf - start_buf));
130ebfedea0SLionel Sambuc break;
131ebfedea0SLionel Sambuc case UT_Set :
132ebfedea0SLionel Sambuc case UT_Sequence :
133ebfedea0SLionel Sambuc printf ("%lu bytes {\n", (unsigned long)length);
134ebfedea0SLionel Sambuc loop_length = loop (buf, length, indent + 2);
135ebfedea0SLionel Sambuc if (indent_flag) {
136ebfedea0SLionel Sambuc int i;
137ebfedea0SLionel Sambuc for (i = 0; i < indent; ++i)
138ebfedea0SLionel Sambuc printf (" ");
139ebfedea0SLionel Sambuc printf ("}\n");
140ebfedea0SLionel Sambuc } else
141ebfedea0SLionel Sambuc printf ("} indent = %d\n", indent / 2);
142ebfedea0SLionel Sambuc break;
143ebfedea0SLionel Sambuc case UT_Integer : {
144ebfedea0SLionel Sambuc int val;
145ebfedea0SLionel Sambuc
146ebfedea0SLionel Sambuc if (length <= sizeof(val)) {
147ebfedea0SLionel Sambuc ret = der_get_integer (buf, length, &val, NULL);
148ebfedea0SLionel Sambuc if (ret)
149ebfedea0SLionel Sambuc errx (1, "der_get_integer: %s", error_message (ret));
150ebfedea0SLionel Sambuc printf ("integer %d\n", val);
151ebfedea0SLionel Sambuc } else {
152ebfedea0SLionel Sambuc heim_integer vali;
153ebfedea0SLionel Sambuc char *p;
154ebfedea0SLionel Sambuc
155ebfedea0SLionel Sambuc ret = der_get_heim_integer(buf, length, &vali, NULL);
156ebfedea0SLionel Sambuc if (ret)
157ebfedea0SLionel Sambuc errx (1, "der_get_heim_integer: %s",
158ebfedea0SLionel Sambuc error_message (ret));
159ebfedea0SLionel Sambuc ret = der_print_hex_heim_integer(&vali, &p);
160ebfedea0SLionel Sambuc if (ret)
161ebfedea0SLionel Sambuc errx (1, "der_print_hex_heim_integer: %s",
162ebfedea0SLionel Sambuc error_message (ret));
163ebfedea0SLionel Sambuc printf ("BIG NUM integer: length %lu %s\n",
164ebfedea0SLionel Sambuc (unsigned long)length, p);
165ebfedea0SLionel Sambuc free(p);
166ebfedea0SLionel Sambuc }
167ebfedea0SLionel Sambuc break;
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc case UT_OctetString : {
170ebfedea0SLionel Sambuc heim_octet_string str;
171*0a6a1f1dSLionel Sambuc size_t i;
172ebfedea0SLionel Sambuc
173ebfedea0SLionel Sambuc ret = der_get_octet_string (buf, length, &str, NULL);
174ebfedea0SLionel Sambuc if (ret)
175ebfedea0SLionel Sambuc errx (1, "der_get_octet_string: %s", error_message (ret));
176ebfedea0SLionel Sambuc printf ("(length %lu), ", (unsigned long)length);
177*0a6a1f1dSLionel Sambuc
178*0a6a1f1dSLionel Sambuc if (inner_flag) {
179*0a6a1f1dSLionel Sambuc Der_class class;
180*0a6a1f1dSLionel Sambuc Der_type type;
181*0a6a1f1dSLionel Sambuc unsigned int tag;
182*0a6a1f1dSLionel Sambuc
183*0a6a1f1dSLionel Sambuc ret = der_get_tag(str.data, str.length,
184*0a6a1f1dSLionel Sambuc &class, &type, &tag, &sz);
185*0a6a1f1dSLionel Sambuc if (ret || sz > str.length ||
186*0a6a1f1dSLionel Sambuc type != CONS || tag != UT_Sequence)
187*0a6a1f1dSLionel Sambuc goto just_an_octet_string;
188*0a6a1f1dSLionel Sambuc
189*0a6a1f1dSLionel Sambuc printf("{\n");
190*0a6a1f1dSLionel Sambuc loop (str.data, str.length, indent + 2);
191*0a6a1f1dSLionel Sambuc for (i = 0; i < indent; ++i)
192*0a6a1f1dSLionel Sambuc printf (" ");
193*0a6a1f1dSLionel Sambuc printf ("}\n");
194*0a6a1f1dSLionel Sambuc
195*0a6a1f1dSLionel Sambuc } else {
196*0a6a1f1dSLionel Sambuc unsigned char *uc;
197*0a6a1f1dSLionel Sambuc
198*0a6a1f1dSLionel Sambuc just_an_octet_string:
199ebfedea0SLionel Sambuc uc = (unsigned char *)str.data;
200ebfedea0SLionel Sambuc for (i = 0; i < min(16,length); ++i)
201ebfedea0SLionel Sambuc printf ("%02x", uc[i]);
202ebfedea0SLionel Sambuc printf ("\n");
203*0a6a1f1dSLionel Sambuc }
204ebfedea0SLionel Sambuc free (str.data);
205ebfedea0SLionel Sambuc break;
206ebfedea0SLionel Sambuc }
207ebfedea0SLionel Sambuc case UT_IA5String :
208ebfedea0SLionel Sambuc case UT_PrintableString : {
209ebfedea0SLionel Sambuc heim_printable_string str;
210ebfedea0SLionel Sambuc unsigned char *s;
211ebfedea0SLionel Sambuc size_t n;
212ebfedea0SLionel Sambuc
213ebfedea0SLionel Sambuc memset(&str, 0, sizeof(str));
214ebfedea0SLionel Sambuc
215ebfedea0SLionel Sambuc ret = der_get_printable_string (buf, length, &str, NULL);
216ebfedea0SLionel Sambuc if (ret)
217ebfedea0SLionel Sambuc errx (1, "der_get_general_string: %s",
218ebfedea0SLionel Sambuc error_message (ret));
219ebfedea0SLionel Sambuc s = str.data;
220ebfedea0SLionel Sambuc printf("\"");
221ebfedea0SLionel Sambuc for (n = 0; n < str.length; n++) {
222ebfedea0SLionel Sambuc if (isprint((int)s[n]))
223ebfedea0SLionel Sambuc printf ("%c", s[n]);
224ebfedea0SLionel Sambuc else
225ebfedea0SLionel Sambuc printf ("#%02x", s[n]);
226ebfedea0SLionel Sambuc }
227ebfedea0SLionel Sambuc printf("\"\n");
228ebfedea0SLionel Sambuc der_free_printable_string(&str);
229ebfedea0SLionel Sambuc break;
230ebfedea0SLionel Sambuc }
231ebfedea0SLionel Sambuc case UT_GeneralizedTime :
232ebfedea0SLionel Sambuc case UT_GeneralString :
233ebfedea0SLionel Sambuc case UT_VisibleString :
234ebfedea0SLionel Sambuc case UT_UTF8String : {
235ebfedea0SLionel Sambuc heim_general_string str;
236ebfedea0SLionel Sambuc
237ebfedea0SLionel Sambuc ret = der_get_general_string (buf, length, &str, NULL);
238ebfedea0SLionel Sambuc if (ret)
239ebfedea0SLionel Sambuc errx (1, "der_get_general_string: %s",
240ebfedea0SLionel Sambuc error_message (ret));
241ebfedea0SLionel Sambuc printf ("\"%s\"\n", str);
242ebfedea0SLionel Sambuc free (str);
243ebfedea0SLionel Sambuc break;
244ebfedea0SLionel Sambuc }
245ebfedea0SLionel Sambuc case UT_OID: {
246ebfedea0SLionel Sambuc heim_oid o;
247ebfedea0SLionel Sambuc char *p;
248ebfedea0SLionel Sambuc
249ebfedea0SLionel Sambuc ret = der_get_oid(buf, length, &o, NULL);
250ebfedea0SLionel Sambuc if (ret)
251ebfedea0SLionel Sambuc errx (1, "der_get_oid: %s", error_message (ret));
252ebfedea0SLionel Sambuc ret = der_print_heim_oid(&o, '.', &p);
253ebfedea0SLionel Sambuc der_free_oid(&o);
254ebfedea0SLionel Sambuc if (ret)
255ebfedea0SLionel Sambuc errx (1, "der_print_heim_oid: %s", error_message (ret));
256ebfedea0SLionel Sambuc printf("%s\n", p);
257ebfedea0SLionel Sambuc free(p);
258ebfedea0SLionel Sambuc
259ebfedea0SLionel Sambuc break;
260ebfedea0SLionel Sambuc }
261ebfedea0SLionel Sambuc case UT_Enumerated: {
262ebfedea0SLionel Sambuc int num;
263ebfedea0SLionel Sambuc
264ebfedea0SLionel Sambuc ret = der_get_integer (buf, length, &num, NULL);
265ebfedea0SLionel Sambuc if (ret)
266ebfedea0SLionel Sambuc errx (1, "der_get_enum: %s", error_message (ret));
267ebfedea0SLionel Sambuc
268ebfedea0SLionel Sambuc printf("%u\n", num);
269ebfedea0SLionel Sambuc break;
270ebfedea0SLionel Sambuc }
271ebfedea0SLionel Sambuc default :
272ebfedea0SLionel Sambuc printf ("%lu bytes\n", (unsigned long)length);
273ebfedea0SLionel Sambuc break;
274ebfedea0SLionel Sambuc }
275ebfedea0SLionel Sambuc }
276ebfedea0SLionel Sambuc if (end_tag) {
277ebfedea0SLionel Sambuc if (loop_length == 0)
278ebfedea0SLionel Sambuc errx(1, "zero length INDEFINITE data ? indent = %d\n",
279ebfedea0SLionel Sambuc indent / 2);
280ebfedea0SLionel Sambuc if (loop_length < length)
281ebfedea0SLionel Sambuc length = loop_length;
282ebfedea0SLionel Sambuc if (indefinite_form_loop == 0)
283ebfedea0SLionel Sambuc errx(1, "internal error in indefinite form loop detection");
284ebfedea0SLionel Sambuc indefinite_form_loop--;
285ebfedea0SLionel Sambuc } else if (loop_length)
286ebfedea0SLionel Sambuc errx(1, "internal error for INDEFINITE form");
287ebfedea0SLionel Sambuc buf += length;
288ebfedea0SLionel Sambuc len -= length;
289ebfedea0SLionel Sambuc }
290ebfedea0SLionel Sambuc return 0;
291ebfedea0SLionel Sambuc }
292ebfedea0SLionel Sambuc
293ebfedea0SLionel Sambuc static int
doit(const char * filename)294ebfedea0SLionel Sambuc doit (const char *filename)
295ebfedea0SLionel Sambuc {
296ebfedea0SLionel Sambuc int fd = open (filename, O_RDONLY);
297ebfedea0SLionel Sambuc struct stat sb;
298ebfedea0SLionel Sambuc unsigned char *buf;
299ebfedea0SLionel Sambuc size_t len;
300ebfedea0SLionel Sambuc int ret;
301ebfedea0SLionel Sambuc
302ebfedea0SLionel Sambuc if(fd < 0)
303ebfedea0SLionel Sambuc err (1, "opening %s for read", filename);
304ebfedea0SLionel Sambuc if (fstat (fd, &sb) < 0)
305ebfedea0SLionel Sambuc err (1, "stat %s", filename);
306ebfedea0SLionel Sambuc len = sb.st_size;
307ebfedea0SLionel Sambuc buf = emalloc (len);
308ebfedea0SLionel Sambuc if (read (fd, buf, len) != len)
309ebfedea0SLionel Sambuc errx (1, "read failed");
310ebfedea0SLionel Sambuc close (fd);
311ebfedea0SLionel Sambuc ret = loop (buf, len, 0);
312ebfedea0SLionel Sambuc free (buf);
313ebfedea0SLionel Sambuc return ret;
314ebfedea0SLionel Sambuc }
315ebfedea0SLionel Sambuc
316ebfedea0SLionel Sambuc
317ebfedea0SLionel Sambuc static int version_flag;
318ebfedea0SLionel Sambuc static int help_flag;
319ebfedea0SLionel Sambuc struct getargs args[] = {
320ebfedea0SLionel Sambuc { "indent", 0, arg_negative_flag, &indent_flag },
321*0a6a1f1dSLionel Sambuc { "inner", 0, arg_flag, &inner_flag, "try to parse inner structures of OCTET STRING" },
322ebfedea0SLionel Sambuc { "version", 0, arg_flag, &version_flag },
323ebfedea0SLionel Sambuc { "help", 0, arg_flag, &help_flag }
324ebfedea0SLionel Sambuc };
325ebfedea0SLionel Sambuc int num_args = sizeof(args) / sizeof(args[0]);
326ebfedea0SLionel Sambuc
327ebfedea0SLionel Sambuc static void
usage(int code)328ebfedea0SLionel Sambuc usage(int code)
329ebfedea0SLionel Sambuc {
330ebfedea0SLionel Sambuc arg_printusage(args, num_args, NULL, "dump-file");
331ebfedea0SLionel Sambuc exit(code);
332ebfedea0SLionel Sambuc }
333ebfedea0SLionel Sambuc
334ebfedea0SLionel Sambuc int
main(int argc,char ** argv)335ebfedea0SLionel Sambuc main(int argc, char **argv)
336ebfedea0SLionel Sambuc {
337ebfedea0SLionel Sambuc int optidx = 0;
338ebfedea0SLionel Sambuc
339ebfedea0SLionel Sambuc setprogname (argv[0]);
340ebfedea0SLionel Sambuc initialize_asn1_error_table ();
341ebfedea0SLionel Sambuc if(getarg(args, num_args, argc, argv, &optidx))
342ebfedea0SLionel Sambuc usage(1);
343ebfedea0SLionel Sambuc if(help_flag)
344ebfedea0SLionel Sambuc usage(0);
345ebfedea0SLionel Sambuc if(version_flag) {
346ebfedea0SLionel Sambuc print_version(NULL);
347ebfedea0SLionel Sambuc exit(0);
348ebfedea0SLionel Sambuc }
349ebfedea0SLionel Sambuc argv += optidx;
350ebfedea0SLionel Sambuc argc -= optidx;
351ebfedea0SLionel Sambuc if (argc != 1)
352ebfedea0SLionel Sambuc usage (1);
353ebfedea0SLionel Sambuc return doit (argv[0]);
354ebfedea0SLionel Sambuc }
355