Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 1 | #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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 9 | #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 | |
| 16 | static 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 | |
| 27 | static enum asn_transfer_syntax |
| 28 | lookup_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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 41 | #ifdef ENABLE_LIBFUZZER |
| 42 | |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 43 | static int initialized; |
| 44 | static enum asn_transfer_syntax syntax; |
| 45 | static 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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 60 | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); |
| 61 | |
| 62 | int |
| 63 | LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 64 | 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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | #else /* The usual check */ |
| 79 | |
| 80 | static void |
| 81 | usage(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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 98 | static int |
| 99 | file_write_cb(const void *data, size_t size, void *key) { |
| 100 | return fwrite(data, 1, size, (FILE *)key) == size ? 0 : -1; |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | generate_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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 113 | enc.dir_name); |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 114 | 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 Walkin | ce6b0a6 | 2017-10-01 16:59:20 -0700 | [diff] [blame] | 138 | size_t generated_ok = 0; |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 139 | 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 Walkin | ce6b0a6 | 2017-10-01 16:59:20 -0700 | [diff] [blame] | 180 | 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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | } |
| 190 | |
| 191 | static void |
| 192 | check_random_roundtrip(enum asn_transfer_syntax syntax, int iterations) { |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 193 | struct encoding_map enc; |
| 194 | |
| 195 | for(size_t i = 0; i < sizeof(encodings)/sizeof(encodings[0]); i++) { |
| 196 | enc = encodings[i]; |
| 197 | if(enc.syntax == syntax) { |
| 198 | fprintf(stderr, "Testing %d iterations of round-trip for %s\n", |
| 199 | iterations, enc.name); |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | for(int i = 0; i < iterations; i++) { |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame^] | 205 | char tmp_buffer[512]; |
| 206 | char *buffer = tmp_buffer; |
| 207 | size_t buffer_size = sizeof(tmp_buffer); |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 222 | 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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 230 | if(er.encoded > buffer_size && buffer == tmp_buffer) { |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame^] | 231 | fprintf(stderr, "Reallocate output buffer %zu -> %zu\n", |
| 232 | buffer_size, er.encoded); |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 233 | buffer = malloc(er.encoded + 1); |
| 234 | assert(buffer); |
| 235 | buffer[er.encoded] = '\0'; |
| 236 | buffer_size = er.encoded; |
| 237 | continue; |
| 238 | } |
| 239 | break; |
| 240 | } |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame^] | 241 | if(er.encoded > buffer_size) { |
| 242 | fprintf(stderr, "Data %zd does not fit into buffer %zu\n", |
| 243 | er.encoded, buffer_size); |
| 244 | assert(er.encoded <= buffer_size); |
| 245 | } |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 246 | |
| 247 | asn_dec_rval_t rval = |
| 248 | asn_decode(0, syntax, &asn_DEF_T, (void **)&decoded_structure, |
| 249 | buffer, er.encoded); |
| 250 | if(rval.code == RC_OK) { |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 251 | /* Everything's cool... or is it? Expecting a proper consumed */ |
| 252 | if(rval.consumed != er.encoded) { |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame^] | 253 | fprintf(stderr, "Encoded into %zd, yet consumed %zu\n", |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 254 | er.encoded, rval.consumed); |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame^] | 255 | fprintf(stderr, "Original random structure:\n"); |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 256 | asn_fprint(stderr, &asn_DEF_T, structure); |
| 257 | assert(rval.consumed == er.encoded); |
| 258 | exit(EX_SOFTWARE); |
| 259 | } |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 260 | } else { |
| 261 | fprintf(stderr, |
| 262 | "Decoding %zu bytes of T yielded %s after byte %zu\n", |
| 263 | er.encoded, rval.code == RC_FAIL ? "RC_FAIL" : "RC_WMORE", |
| 264 | rval.consumed); |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame^] | 265 | fprintf(stderr, "Original random structure:\n"); |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 266 | asn_fprint(stderr, &asn_DEF_T, structure); |
| 267 | exit(EX_SOFTWARE); |
| 268 | } |
| 269 | |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 270 | /* |
| 271 | * Confirm that we decoded the same data. |
| 272 | */ |
| 273 | int cmp = asn_DEF_T.op->compare_struct(&asn_DEF_T, structure, |
| 274 | decoded_structure); |
| 275 | if(cmp != 0) { |
| 276 | fprintf(stderr, "Random %s value:\n", ASN1_STR); |
| 277 | asn_fprint(stderr, &asn_DEF_T, structure); |
| 278 | fprintf(stderr, "Decoded %s value:\n", ASN1_STR); |
| 279 | asn_fprint(stderr, &asn_DEF_T, decoded_structure); |
| 280 | assert(cmp == 0); |
| 281 | } |
| 282 | ASN_STRUCT_FREE(asn_DEF_T, structure); |
| 283 | ASN_STRUCT_FREE(asn_DEF_T, decoded_structure); |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 284 | |
| 285 | if(buffer != tmp_buffer) { |
| 286 | free(buffer); |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | if(i < 5) { |
| 290 | fprintf(stderr, "[%03d] round-trip in %zd bytes OK\n", i, |
| 291 | er.encoded); |
| 292 | } else if(i == 5) { |
| 293 | fprintf(stderr, "... and so on\n"); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | fprintf(stderr, "OK %d iterations of round-trip for %s\n", iterations, |
| 298 | enc.name); |
| 299 | } |
| 300 | |
| 301 | int main(int argc, char **argv) { |
| 302 | uint32_t enabled_encodings = 0; |
| 303 | enum { |
| 304 | MODE_UNKNOWN, |
| 305 | MODE_GENERATE_RANDOM_DATA, |
| 306 | MODE_CHECK_RANDOM_ROUNDTRIP |
| 307 | } mode = MODE_UNKNOWN; |
| 308 | const char *generate_into_dir = NULL; |
| 309 | int iterations = 100; |
| 310 | int c; |
| 311 | |
| 312 | while((c = getopt(argc, argv, "ce:g:hn:")) != -1) { |
| 313 | switch(c) { |
| 314 | case 'c': |
| 315 | mode = MODE_CHECK_RANDOM_ROUNDTRIP; |
| 316 | break; |
| 317 | case 'e': |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 318 | enabled_encodings |= 1 << lookup_syntax(optarg); |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 319 | if(enabled_encodings & (1 << ATS_INVALID)) { |
| 320 | fprintf(stderr, "-e %s: Unknown (unsupported?) encoding\n", |
| 321 | optarg); |
| 322 | exit(EX_UNAVAILABLE); |
| 323 | } |
| 324 | break; |
| 325 | case 'g': |
| 326 | mode = MODE_GENERATE_RANDOM_DATA; |
| 327 | generate_into_dir = optarg; |
| 328 | break; |
| 329 | case 'h': |
| 330 | usage(argv[0]); |
| 331 | exit(0); |
| 332 | case 'n': |
| 333 | iterations = atoi(optarg); |
| 334 | if(iterations <= 0) { |
| 335 | fprintf(stderr, "-n %s: positive value expected\n", optarg); |
| 336 | exit(EX_DATAERR); |
| 337 | } |
| 338 | break; |
| 339 | default: |
| 340 | usage(argv[0]); |
| 341 | exit(2); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if(mode == MODE_UNKNOWN) { |
| 346 | usage(argv[0]); |
| 347 | exit(2); |
| 348 | } else if(!enabled_encodings) { |
| 349 | for(size_t i = 0; i < sizeof(encodings)/sizeof(encodings[0]); i++) { |
| 350 | enabled_encodings |= 1 << encodings[i].syntax; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /* Enumerate requested encodings (-e ...) */ |
| 355 | for(unsigned i = 0; i < 8*sizeof(enabled_encodings)-1; i++) { |
| 356 | if(enabled_encodings & (1 << i)) { |
| 357 | enum asn_transfer_syntax syntax = i; |
| 358 | switch(mode) { |
| 359 | case MODE_UNKNOWN: |
| 360 | assert(mode != MODE_UNKNOWN); |
| 361 | break; |
| 362 | case MODE_GENERATE_RANDOM_DATA: |
| 363 | generate_random_data(syntax, generate_into_dir, iterations); |
| 364 | break; |
| 365 | case MODE_CHECK_RANDOM_ROUNDTRIP: |
| 366 | check_random_roundtrip(syntax, iterations); |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | #endif |