blob: e2ea47508f9ddd49b8bfbc4a527ed10756efb591 [file] [log] [blame]
Lev Walkincb523912017-09-30 19:33:23 -07001#include <T.h>
2
3#include <string.h>
4#include <errno.h>
5#include <unistd.h>
6#include <sys/stat.h>
7#include <sysexits.h>
8
Lev Walkind3cce462017-10-01 13:43:36 -07009#ifdef ASN1_TEXT
10#define STRINGIFY(x) #x
11#define ASN1_STR STRINGIFY(ASN1_TEXT)
12#else
13#define ASN1_STR "T"
14#endif
15
16static const struct encoding_map {
17 const char *name;
18 const char *dir_name;
19 enum asn_transfer_syntax syntax;
20} encodings[] = {
21 {"DER", "der", ATS_DER},
22 {"OER", "oer", ATS_CANONICAL_OER},
23 {"UPER", "uper", ATS_UNALIGNED_CANONICAL_PER},
24 {"XER", "xer", ATS_CANONICAL_XER},
25};
26
27static enum asn_transfer_syntax
28lookup_syntax(const char *name) {
29 if(name) {
30 for(size_t i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++) {
31 struct encoding_map enc = encodings[i];
32 if(strcasecmp(name, enc.name) == 0) {
33 return enc.syntax;
34 }
35 }
36 }
37 return ATS_INVALID;
38}
39
40
Lev Walkincb523912017-09-30 19:33:23 -070041#ifdef ENABLE_LIBFUZZER
42
Lev Walkind3cce462017-10-01 13:43:36 -070043static int initialized;
44static enum asn_transfer_syntax syntax;
45static void __attribute__((constructor)) initialize() {
46 initialized = 1;
47 const char *data_dir = getenv("ASN1_DATA_DIR");
48 if(data_dir && strrchr(data_dir, '/')) {
49 data_dir = strrchr(data_dir, '/') + 1;
50 }
51 syntax = lookup_syntax(data_dir);
52 if(syntax == ATS_INVALID) {
53 fprintf(stderr,
54 "Expected ASN1_DATA_DIR={der,oer,uper,xer} environment "
55 "variable.\n");
56 exit(EX_UNAVAILABLE);
57 }
58}
59
Lev Walkincb523912017-09-30 19:33:23 -070060int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
61
62int
63LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Lev Walkind3cce462017-10-01 13:43:36 -070064 if(!initialized) exit(0);
65
66 /*
67 * Try to decode whatever garbage that comes in Data/Size.
68 * The idea is that we should not crash, and we should not leak memory,
69 * no matter what garbage we're dealing with.
70 */
71 T_t *structure = 0;
72 (void)asn_decode(0, syntax, &asn_DEF_T, (void **)&structure, Data, Size);
73 ASN_STRUCT_FREE(asn_DEF_T, structure);
74
Lev Walkincb523912017-09-30 19:33:23 -070075 return 0;
76}
77
78#else /* The usual check */
79
80static void
81usage(const char *progname) {
82 fprintf(stderr,
83 "Usage: %s {-c|-g <dir>} [-n <number>] [-e <encoding> ...]\n"
84 "OPTIONS:\n"
85 " -c Check encode-decode round-trip on random data\n"
86 " -g <dir> Generate random data for selected encodings\n"
87 " -n <number> Number of iterations for -c and -g\n"
88 " -e <encoding> Encodings to test or generate random data for\n"
89 "Encodings (ASN.1 Transfer Syntaxes):\n"
90 " DER Distinguished Encoding Rules (compatible with "
91 "BER)\n"
92 " OER Canonical Octet Encoding Rules\n"
93 " UPER Canonical Unaligned Packed Encoding Rules\n"
94 " XER XML Encoding Rules\n",
95 progname);
96}
97
Lev Walkincb523912017-09-30 19:33:23 -070098static int
99file_write_cb(const void *data, size_t size, void *key) {
100 return fwrite(data, 1, size, (FILE *)key) == size ? 0 : -1;
101}
102
103static void
104generate_random_data(enum asn_transfer_syntax syntax, const char *top_dirname, int iterations) {
105 char dirname[PATH_MAX];
106 size_t dirname_len = 0;
107 dirname[dirname_len] = '\0';
108
109 for(size_t i = 0; i < sizeof(encodings)/sizeof(encodings[0]); i++) {
110 struct encoding_map enc = encodings[i];
111 if(enc.syntax == syntax) {
112 int r = snprintf(dirname, sizeof(dirname), "%s/%s", top_dirname,
Lev Walkind3cce462017-10-01 13:43:36 -0700113 enc.dir_name);
Lev Walkincb523912017-09-30 19:33:23 -0700114 if(r >= sizeof(dirname) - sizeof("filename.bin")) {
115 fprintf(stderr, "Too long filenames\n");
116 exit(EX_SOFTWARE);
117 }
118 dirname_len = r;
119 fprintf(stderr, "Generating %d random %s values of %s into %s\n",
120 iterations, enc.name, asn_DEF_T.name, dirname);
121 break;
122 }
123 }
124 assert(dirname[0]);
125
126 (void)mkdir(top_dirname, 0777);
127
128 if(mkdir(dirname, 0777) == -1) {
129 if(errno == EEXIST) {
130 fprintf(stderr, "%s: is already present, remove.\n", dirname);
131 fprintf(stderr, "%s: not overwriting potentially valuable data.\n",
132 dirname);
133 }
134 perror(dirname);
135 exit(2);
136 }
137
Lev Walkince6b0a62017-10-01 16:59:20 -0700138 size_t generated_ok = 0;
Lev Walkincb523912017-09-30 19:33:23 -0700139 for(int i = 0; i < iterations; i++) {
140 T_t *structure = 0;
141 FILE *f;
142 snprintf(&dirname[dirname_len], sizeof(dirname) - dirname_len,
143 "/%03d.bin", i);
144
145 if(asn_random_fill(&asn_DEF_T, (void **)&structure, 128) == -1) {
146 assert(structure == 0);
147 fprintf(stderr, "Can't generate %d'th value, skipping\n", i);
148 continue;
149 }
150 assert(structure != 0);
151
152 const char *filename = dirname;
153 f = fopen(filename, "wb");
154 if(!f) {
155 perror(filename);
156 assert(f);
157 exit(EX_SOFTWARE);
158 }
159 asn_enc_rval_t rval =
160 asn_encode(0, syntax, &asn_DEF_T, structure, file_write_cb, f);
161 fclose(f);
162 if(rval.encoded == -1) {
163 fprintf(stderr, "Cannot encode a random value of T into %s:\n",
164 filename);
165 if(rval.failed_type) {
166 fprintf(stderr, "(Failed type: %s)\n", rval.failed_type->name);
167 }
168 asn_fprint(stderr, &asn_DEF_T, structure);
169 exit(EX_SOFTWARE);
170 }
171
172 if(i < 5) {
173 fprintf(stderr, "[%s] ", &filename[dirname_len+1]);
174 asn_fprint(stderr, &asn_DEF_T, structure);
175 } else if(i == 5) {
176 fprintf(stderr, "... and so on\n");
177 }
178
179 ASN_STRUCT_FREE(asn_DEF_T, structure);
Lev Walkince6b0a62017-10-01 16:59:20 -0700180 generated_ok++;
181 }
182
183 if(!generated_ok) {
184 fprintf(stderr, "Requested to generate %d values, but failed.\n",
185 iterations);
186 exit(EX_SOFTWARE);
Lev Walkincb523912017-09-30 19:33:23 -0700187 }
188
189}
190
191static void
192check_random_roundtrip(enum asn_transfer_syntax syntax, int iterations) {
193 char tmp_buffer[512];
194 char *buffer = tmp_buffer;
195 size_t buffer_size = sizeof(tmp_buffer);
196 struct encoding_map enc;
197
198 for(size_t i = 0; i < sizeof(encodings)/sizeof(encodings[0]); i++) {
199 enc = encodings[i];
200 if(enc.syntax == syntax) {
201 fprintf(stderr, "Testing %d iterations of round-trip for %s\n",
202 iterations, enc.name);
203 break;
204 }
205 }
206
207 for(int i = 0; i < iterations; i++) {
208 T_t *structure = 0;
209 T_t *decoded_structure = 0;
210
211 if(asn_random_fill(&asn_DEF_T, (void **)&structure, 128) == -1) {
212 assert(structure == 0);
213 fprintf(stderr, "Can't generate %d'th value, skipping\n", i);
214 continue;
215 }
216 assert(structure != 0);
217
218 asn_enc_rval_t er;
219 for(;;) {
220 er = asn_encode_to_buffer(
221 0, syntax, &asn_DEF_T, structure, buffer, buffer_size);
Lev Walkind3cce462017-10-01 13:43:36 -0700222 if(er.encoded == -1) {
223 fprintf(stderr, "Encoded T into %zd bytes\n", er.encoded);
224 fprintf(stderr, "Structure %s:\n",
225 sizeof(ASN1_STR) > 60 ? "T" : ASN1_STR);
226 asn_fprint(stderr, &asn_DEF_T, structure);
227 assert(er.encoded >= 0);
228 exit(EX_SOFTWARE);
229 }
Lev Walkincb523912017-09-30 19:33:23 -0700230 if(er.encoded > buffer_size && buffer == tmp_buffer) {
231 buffer = malloc(er.encoded + 1);
232 assert(buffer);
233 buffer[er.encoded] = '\0';
234 buffer_size = er.encoded;
235 continue;
236 }
237 break;
238 }
239 assert(er.encoded <= buffer_size);
240
241 asn_dec_rval_t rval =
242 asn_decode(0, syntax, &asn_DEF_T, (void **)&decoded_structure,
243 buffer, er.encoded);
244 if(rval.code == RC_OK) {
Lev Walkind3cce462017-10-01 13:43:36 -0700245 /* Everything's cool... or is it? Expecting a proper consumed */
246 if(rval.consumed != er.encoded) {
247 fprintf(stderr, "Encoded into %zd, yet consumed %zu",
248 er.encoded, rval.consumed);
249 fprintf(stderr, "Structure:\n");
250 asn_fprint(stderr, &asn_DEF_T, structure);
251 assert(rval.consumed == er.encoded);
252 exit(EX_SOFTWARE);
253 }
Lev Walkincb523912017-09-30 19:33:23 -0700254 } else {
255 fprintf(stderr,
256 "Decoding %zu bytes of T yielded %s after byte %zu\n",
257 er.encoded, rval.code == RC_FAIL ? "RC_FAIL" : "RC_WMORE",
258 rval.consumed);
259 fprintf(stderr, "Structure:\n");
260 asn_fprint(stderr, &asn_DEF_T, structure);
261 exit(EX_SOFTWARE);
262 }
263
Lev Walkind3cce462017-10-01 13:43:36 -0700264 /*
265 * Confirm that we decoded the same data.
266 */
267 int cmp = asn_DEF_T.op->compare_struct(&asn_DEF_T, structure,
268 decoded_structure);
269 if(cmp != 0) {
270 fprintf(stderr, "Random %s value:\n", ASN1_STR);
271 asn_fprint(stderr, &asn_DEF_T, structure);
272 fprintf(stderr, "Decoded %s value:\n", ASN1_STR);
273 asn_fprint(stderr, &asn_DEF_T, decoded_structure);
274 assert(cmp == 0);
275 }
276 ASN_STRUCT_FREE(asn_DEF_T, structure);
277 ASN_STRUCT_FREE(asn_DEF_T, decoded_structure);
Lev Walkincb523912017-09-30 19:33:23 -0700278
279 if(buffer != tmp_buffer) {
280 free(buffer);
281 buffer = tmp_buffer;
282 buffer_size = sizeof(tmp_buffer);
283 }
284
285 if(i < 5) {
286 fprintf(stderr, "[%03d] round-trip in %zd bytes OK\n", i,
287 er.encoded);
288 } else if(i == 5) {
289 fprintf(stderr, "... and so on\n");
290 }
291 }
292
293 fprintf(stderr, "OK %d iterations of round-trip for %s\n", iterations,
294 enc.name);
295}
296
297int main(int argc, char **argv) {
298 uint32_t enabled_encodings = 0;
299 enum {
300 MODE_UNKNOWN,
301 MODE_GENERATE_RANDOM_DATA,
302 MODE_CHECK_RANDOM_ROUNDTRIP
303 } mode = MODE_UNKNOWN;
304 const char *generate_into_dir = NULL;
305 int iterations = 100;
306 int c;
307
308 while((c = getopt(argc, argv, "ce:g:hn:")) != -1) {
309 switch(c) {
310 case 'c':
311 mode = MODE_CHECK_RANDOM_ROUNDTRIP;
312 break;
313 case 'e':
Lev Walkind3cce462017-10-01 13:43:36 -0700314 enabled_encodings |= 1 << lookup_syntax(optarg);
Lev Walkincb523912017-09-30 19:33:23 -0700315 if(enabled_encodings & (1 << ATS_INVALID)) {
316 fprintf(stderr, "-e %s: Unknown (unsupported?) encoding\n",
317 optarg);
318 exit(EX_UNAVAILABLE);
319 }
320 break;
321 case 'g':
322 mode = MODE_GENERATE_RANDOM_DATA;
323 generate_into_dir = optarg;
324 break;
325 case 'h':
326 usage(argv[0]);
327 exit(0);
328 case 'n':
329 iterations = atoi(optarg);
330 if(iterations <= 0) {
331 fprintf(stderr, "-n %s: positive value expected\n", optarg);
332 exit(EX_DATAERR);
333 }
334 break;
335 default:
336 usage(argv[0]);
337 exit(2);
338 }
339 }
340
341 if(mode == MODE_UNKNOWN) {
342 usage(argv[0]);
343 exit(2);
344 } else if(!enabled_encodings) {
345 for(size_t i = 0; i < sizeof(encodings)/sizeof(encodings[0]); i++) {
346 enabled_encodings |= 1 << encodings[i].syntax;
347 }
348 }
349
350 /* Enumerate requested encodings (-e ...) */
351 for(unsigned i = 0; i < 8*sizeof(enabled_encodings)-1; i++) {
352 if(enabled_encodings & (1 << i)) {
353 enum asn_transfer_syntax syntax = i;
354 switch(mode) {
355 case MODE_UNKNOWN:
356 assert(mode != MODE_UNKNOWN);
357 break;
358 case MODE_GENERATE_RANDOM_DATA:
359 generate_random_data(syntax, generate_into_dir, iterations);
360 break;
361 case MODE_CHECK_RANDOM_ROUNDTRIP:
362 check_random_roundtrip(syntax, iterations);
363 break;
364 }
365 }
366 }
367
368 return 0;
369}
370
371#endif