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