xref: /minix3/crypto/external/bsd/heimdal/dist/lib/asn1/asn1_gen.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: asn1_gen.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2005 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "der_locl.h"
37ebfedea0SLionel Sambuc #include <krb5/com_err.h>
38ebfedea0SLionel Sambuc #include <sys/types.h>
39ebfedea0SLionel Sambuc #include <sys/stat.h>
40ebfedea0SLionel Sambuc #include <ctype.h>
41ebfedea0SLionel Sambuc #include <krb5/getarg.h>
42ebfedea0SLionel Sambuc #include <krb5/hex.h>
43ebfedea0SLionel Sambuc #include <err.h>
44ebfedea0SLionel Sambuc 
45*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
46ebfedea0SLionel Sambuc 
47ebfedea0SLionel Sambuc static int
doit(const char * fn)48ebfedea0SLionel Sambuc doit(const char *fn)
49ebfedea0SLionel Sambuc {
50ebfedea0SLionel Sambuc     char buf[2048];
51ebfedea0SLionel Sambuc     char *fnout = NULL;
52ebfedea0SLionel Sambuc     const char *bname;
53ebfedea0SLionel Sambuc     unsigned long line = 0;
54ebfedea0SLionel Sambuc     FILE *f, *fout;
55ebfedea0SLionel Sambuc     size_t offset = 0;
56ebfedea0SLionel Sambuc 
57ebfedea0SLionel Sambuc     f = fopen(fn, "r");
58ebfedea0SLionel Sambuc     if (f == NULL)
59ebfedea0SLionel Sambuc 	err(1, "fopen");
60ebfedea0SLionel Sambuc 
61ebfedea0SLionel Sambuc     bname = strrchr(fn, '/');
62ebfedea0SLionel Sambuc     if (bname)
63ebfedea0SLionel Sambuc 	bname++;
64ebfedea0SLionel Sambuc     else
65ebfedea0SLionel Sambuc 	bname = fn;
66ebfedea0SLionel Sambuc 
67ebfedea0SLionel Sambuc     if (asprintf(&fnout, "%s.out", bname) < 0 || fnout == NULL)
68ebfedea0SLionel Sambuc 	errx(1, "malloc");
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc     fout = fopen(fnout, "w");
71ebfedea0SLionel Sambuc     if (fout == NULL)
72ebfedea0SLionel Sambuc 	err(1, "fopen: output file");
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc     while (fgets(buf, sizeof(buf), f) != NULL) {
75ebfedea0SLionel Sambuc 	char *ptr, *class, *type, *tag, *length, *data, *foo;
76ebfedea0SLionel Sambuc 	int ret, l, c, ty, ta;
77ebfedea0SLionel Sambuc 	unsigned char p[6], *pdata;
78ebfedea0SLionel Sambuc 	size_t sz;
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc 	line++;
81ebfedea0SLionel Sambuc 
82ebfedea0SLionel Sambuc 	buf[strcspn(buf, "\r\n")] = '\0';
83ebfedea0SLionel Sambuc 	if (buf[0] == '#' || buf[0] == '\0')
84ebfedea0SLionel Sambuc 	    continue;
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc 	ptr = buf;
87ebfedea0SLionel Sambuc 	while (isspace((unsigned char)*ptr))
88ebfedea0SLionel Sambuc 	       ptr++;
89ebfedea0SLionel Sambuc 
90ebfedea0SLionel Sambuc 	class = strtok_r(ptr, " \t\n", &foo);
91ebfedea0SLionel Sambuc 	if (class == NULL) errx(1, "class missing on line %lu", line);
92ebfedea0SLionel Sambuc 	type = strtok_r(NULL, " \t\n", &foo);
93ebfedea0SLionel Sambuc 	if (type == NULL) errx(1, "type missing on line %lu", line);
94ebfedea0SLionel Sambuc 	tag = strtok_r(NULL, " \t\n", &foo);
95ebfedea0SLionel Sambuc 	if (tag == NULL) errx(1, "tag missing on line %lu", line);
96ebfedea0SLionel Sambuc 	length = strtok_r(NULL, " \t\n", &foo);
97ebfedea0SLionel Sambuc 	if (length == NULL) errx(1, "length missing on line %lu", line);
98ebfedea0SLionel Sambuc 	data = strtok_r(NULL, " \t\n", &foo);
99ebfedea0SLionel Sambuc 
100ebfedea0SLionel Sambuc 	c = der_get_class_num(class);
101ebfedea0SLionel Sambuc 	if (c == -1) errx(1, "no valid class on line %lu", line);
102ebfedea0SLionel Sambuc 	ty = der_get_type_num(type);
103ebfedea0SLionel Sambuc 	if (ty == -1) errx(1, "no valid type on line %lu", line);
104ebfedea0SLionel Sambuc 	ta = der_get_tag_num(tag);
105ebfedea0SLionel Sambuc 	if (ta == -1)
106ebfedea0SLionel Sambuc 	    ta = atoi(tag);
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc 	l = atoi(length);
109ebfedea0SLionel Sambuc 
110ebfedea0SLionel Sambuc 	printf("line: %3lu offset: %3lu class: %d type: %d "
111ebfedea0SLionel Sambuc 	       "tag: %3d length: %3d %s\n",
112ebfedea0SLionel Sambuc 	       line, (unsigned long)offset, c, ty, ta, l,
113ebfedea0SLionel Sambuc 	       data ? "<have data>" : "<no data>");
114ebfedea0SLionel Sambuc 
115ebfedea0SLionel Sambuc 	ret = der_put_length_and_tag(p + sizeof(p) - 1, sizeof(p),
116ebfedea0SLionel Sambuc 				     l,
117ebfedea0SLionel Sambuc 				     c,
118ebfedea0SLionel Sambuc 				     ty,
119ebfedea0SLionel Sambuc 				     ta,
120ebfedea0SLionel Sambuc 				     &sz);
121ebfedea0SLionel Sambuc 	if (ret)
122ebfedea0SLionel Sambuc 	    errx(1, "der_put_length_and_tag: %d", ret);
123ebfedea0SLionel Sambuc 
124ebfedea0SLionel Sambuc 	if (fwrite(p + sizeof(p) - sz , sz, 1, fout) != 1)
125ebfedea0SLionel Sambuc 	    err(1, "fwrite length/tag failed");
126ebfedea0SLionel Sambuc 	offset += sz;
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc 	if (data) {
129ebfedea0SLionel Sambuc 	    size_t datalen;
130ebfedea0SLionel Sambuc 
131ebfedea0SLionel Sambuc 	    datalen = strlen(data) / 2;
132ebfedea0SLionel Sambuc 	    pdata = emalloc(sz);
133ebfedea0SLionel Sambuc 
134ebfedea0SLionel Sambuc 	    if (hex_decode(data, pdata, datalen) != datalen)
135ebfedea0SLionel Sambuc 		errx(1, "failed to decode data");
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc 	    if (fwrite(pdata, datalen, 1, fout) != 1)
138ebfedea0SLionel Sambuc 		err(1, "fwrite data failed");
139ebfedea0SLionel Sambuc 	    offset += datalen;
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc 	    free(pdata);
142ebfedea0SLionel Sambuc 	}
143ebfedea0SLionel Sambuc     }
144ebfedea0SLionel Sambuc     printf("line: eof offset: %lu\n", (unsigned long)offset);
145ebfedea0SLionel Sambuc 
146ebfedea0SLionel Sambuc     fclose(fout);
147ebfedea0SLionel Sambuc     fclose(f);
148ebfedea0SLionel Sambuc     return 0;
149ebfedea0SLionel Sambuc }
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc 
152ebfedea0SLionel Sambuc static int version_flag;
153ebfedea0SLionel Sambuc static int help_flag;
154ebfedea0SLionel Sambuc struct getargs args[] = {
155ebfedea0SLionel Sambuc     { "version", 0, arg_flag, &version_flag },
156ebfedea0SLionel Sambuc     { "help", 0, arg_flag, &help_flag }
157ebfedea0SLionel Sambuc };
158ebfedea0SLionel Sambuc int num_args = sizeof(args) / sizeof(args[0]);
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc static void
usage(int code)161ebfedea0SLionel Sambuc usage(int code)
162ebfedea0SLionel Sambuc {
163ebfedea0SLionel Sambuc     arg_printusage(args, num_args, NULL, "parse-file");
164ebfedea0SLionel Sambuc     exit(code);
165ebfedea0SLionel Sambuc }
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc int
main(int argc,char ** argv)168ebfedea0SLionel Sambuc main(int argc, char **argv)
169ebfedea0SLionel Sambuc {
170ebfedea0SLionel Sambuc     int optidx = 0;
171ebfedea0SLionel Sambuc 
172ebfedea0SLionel Sambuc     setprogname (argv[0]);
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc     if(getarg(args, num_args, argc, argv, &optidx))
175ebfedea0SLionel Sambuc 	usage(1);
176ebfedea0SLionel Sambuc     if(help_flag)
177ebfedea0SLionel Sambuc 	usage(0);
178ebfedea0SLionel Sambuc     if(version_flag) {
179ebfedea0SLionel Sambuc 	print_version(NULL);
180ebfedea0SLionel Sambuc 	exit(0);
181ebfedea0SLionel Sambuc     }
182ebfedea0SLionel Sambuc     argv += optidx;
183ebfedea0SLionel Sambuc     argc -= optidx;
184ebfedea0SLionel Sambuc     if (argc != 1)
185ebfedea0SLionel Sambuc 	usage (1);
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc     return doit (argv[0]);
188ebfedea0SLionel Sambuc }
189