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