xref: /freebsd/crypto/heimdal/lib/asn1/asn1_gen.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*
2  * Copyright (c) 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "der_locl.h"
35 #include <com_err.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <ctype.h>
39 #include <getarg.h>
40 #include <hex.h>
41 #include <err.h>
42 
43 RCSID("$Id$");
44 
45 static int
46 doit(const char *fn)
47 {
48     char buf[2048];
49     char *fnout = NULL;
50     const char *bname;
51     unsigned long line = 0;
52     FILE *f, *fout;
53     size_t offset = 0;
54 
55     f = fopen(fn, "r");
56     if (f == NULL)
57 	err(1, "fopen");
58 
59     bname = strrchr(fn, '/');
60     if (bname)
61 	bname++;
62     else
63 	bname = fn;
64 
65     if (asprintf(&fnout, "%s.out", bname) < 0 || fnout == NULL)
66 	errx(1, "malloc");
67 
68     fout = fopen(fnout, "w");
69     if (fout == NULL)
70 	err(1, "fopen: output file");
71 
72     while (fgets(buf, sizeof(buf), f) != NULL) {
73 	char *ptr, *class, *type, *tag, *length, *data, *foo;
74 	int ret, l, c, ty, ta;
75 	unsigned char p[6], *pdata;
76 	size_t sz;
77 
78 	line++;
79 
80 	buf[strcspn(buf, "\r\n")] = '\0';
81 	if (buf[0] == '#' || buf[0] == '\0')
82 	    continue;
83 
84 	ptr = buf;
85 	while (isspace((unsigned char)*ptr))
86 	       ptr++;
87 
88 	class = strtok_r(ptr, " \t\n", &foo);
89 	if (class == NULL) errx(1, "class missing on line %lu", line);
90 	type = strtok_r(NULL, " \t\n", &foo);
91 	if (type == NULL) errx(1, "type missing on line %lu", line);
92 	tag = strtok_r(NULL, " \t\n", &foo);
93 	if (tag == NULL) errx(1, "tag missing on line %lu", line);
94 	length = strtok_r(NULL, " \t\n", &foo);
95 	if (length == NULL) errx(1, "length missing on line %lu", line);
96 	data = strtok_r(NULL, " \t\n", &foo);
97 
98 	c = der_get_class_num(class);
99 	if (c == -1) errx(1, "no valid class on line %lu", line);
100 	ty = der_get_type_num(type);
101 	if (ty == -1) errx(1, "no valid type on line %lu", line);
102 	ta = der_get_tag_num(tag);
103 	if (ta == -1)
104 	    ta = atoi(tag);
105 
106 	l = atoi(length);
107 
108 	printf("line: %3lu offset: %3lu class: %d type: %d "
109 	       "tag: %3d length: %3d %s\n",
110 	       line, (unsigned long)offset, c, ty, ta, l,
111 	       data ? "<have data>" : "<no data>");
112 
113 	ret = der_put_length_and_tag(p + sizeof(p) - 1, sizeof(p),
114 				     l,
115 				     c,
116 				     ty,
117 				     ta,
118 				     &sz);
119 	if (ret)
120 	    errx(1, "der_put_length_and_tag: %d", ret);
121 
122 	if (fwrite(p + sizeof(p) - sz , sz, 1, fout) != 1)
123 	    err(1, "fwrite length/tag failed");
124 	offset += sz;
125 
126 	if (data) {
127 	    size_t datalen;
128 
129 	    datalen = strlen(data) / 2;
130 	    pdata = emalloc(sz);
131 
132 	    if (hex_decode(data, pdata, datalen) != datalen)
133 		errx(1, "failed to decode data");
134 
135 	    if (fwrite(pdata, datalen, 1, fout) != 1)
136 		err(1, "fwrite data failed");
137 	    offset += datalen;
138 
139 	    free(pdata);
140 	}
141     }
142     printf("line: eof offset: %lu\n", (unsigned long)offset);
143 
144     fclose(fout);
145     fclose(f);
146     return 0;
147 }
148 
149 
150 static int version_flag;
151 static int help_flag;
152 struct getargs args[] = {
153     { "version", 0, arg_flag, &version_flag },
154     { "help", 0, arg_flag, &help_flag }
155 };
156 int num_args = sizeof(args) / sizeof(args[0]);
157 
158 static void
159 usage(int code)
160 {
161     arg_printusage(args, num_args, NULL, "parse-file");
162     exit(code);
163 }
164 
165 int
166 main(int argc, char **argv)
167 {
168     int optidx = 0;
169 
170     setprogname (argv[0]);
171 
172     if(getarg(args, num_args, argc, argv, &optidx))
173 	usage(1);
174     if(help_flag)
175 	usage(0);
176     if(version_flag) {
177 	print_version(NULL);
178 	exit(0);
179     }
180     argv += optidx;
181     argc -= optidx;
182     if (argc != 1)
183 	usage (1);
184 
185     return doit (argv[0]);
186 }
187