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