blob: 9ed57325d5be4acad8fbb48322c751baa65612de [file] [log] [blame]
Lev Walkin397d59d2006-07-13 11:11:33 +00001/*
2 * Mode of operation:
3 * Each of the *.in files is XER-decoded, then converted into DER,
4 * then decoded from DER and encoded into XER again. The resulting
Lev Walkin2a744a72013-03-27 01:56:23 -07005 * stream is checked against rules specified in ../data-70/README file.
Lev Walkin397d59d2006-07-13 11:11:33 +00006 */
7#undef NDEBUG
8#include <stdio.h>
9#include <stdlib.h>
10#include <sys/types.h>
Lev Walkine0d321a2014-09-11 01:28:57 -070011#include <unistd.h> /* for chdir(2), getcwd(3) */
Lev Walkin397d59d2006-07-13 11:11:33 +000012#include <string.h>
Lev Walkin5b63acf2014-01-14 01:48:37 -080013#include <ctype.h>
Lev Walkin397d59d2006-07-13 11:11:33 +000014#include <dirent.h>
Lev Walkin397d59d2006-07-13 11:11:33 +000015#include <assert.h>
16#include <errno.h>
17
18#include <PDU.h>
19
Lev Walkine0d321a2014-09-11 01:28:57 -070020#ifndef SRCDIR
21#define SRCDIR_S ".."
22#else
23#define STRINGIFY_MACRO2(x) #x
24#define STRINGIFY_MACRO(x) STRINGIFY_MACRO2(x)
25#define SRCDIR_S STRINGIFY_MACRO(SRCDIR)
26#endif
27
Lev Walkinbc09dd42017-10-19 02:16:35 -070028#ifdef ENABLE_LIBFUZZER
29
30int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
31int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
32 PDU_t *st = 0;
33 asn_dec_rval_t rval;
34 rval = asn_decode(0, ATS_BASIC_XER, &asn_DEF_PDU, (void **)&st, Data, Size);
35 assert(rval.consumed <= Size);
36 ASN_STRUCT_FREE(asn_DEF_PDU, st);
37 return 0;
38}
39
40#else
41
Lev Walkin397d59d2006-07-13 11:11:33 +000042enum expectation {
43 EXP_OK, /* Encoding/decoding must succeed */
Lev Walkin2a744a72013-03-27 01:56:23 -070044 EXP_CXER_EXACT, /* Encoding/decoding using CXER must be exact */
45 EXP_CXER_DIFF, /* Encoding/decoding using CXER must be different */
Lev Walkin397d59d2006-07-13 11:11:33 +000046 EXP_BROKEN, /* Decoding must fail */
47 EXP_DIFFERENT, /* Reconstruction will yield different encoding */
48};
49
50static unsigned char buf[4096];
51static int buf_offset;
52
53static int
54_buf_writer(const void *buffer, size_t size, void *app_key) {
55 unsigned char *b, *bend;
56 (void)app_key;
57 assert(buf_offset + size < sizeof(buf));
58 memcpy(buf + buf_offset, buffer, size);
59 b = buf + buf_offset;
60 bend = b + size;
61 fprintf(stderr, "=> [");
Lev Walkin2a744a72013-03-27 01:56:23 -070062 for(; b < bend; b++) {
63 if(*b >= 32 && *b < 127 && *b != '%')
64 fprintf(stderr, "%c", *b);
65 else
66 fprintf(stderr, "%%%02x", *b);
67 }
Lev Walkin1715b322013-03-28 04:02:13 -070068 fprintf(stderr, "]:%zd\n", size);
Lev Walkin397d59d2006-07-13 11:11:33 +000069 buf_offset += size;
70 return 0;
71}
72
Lev Walkin397d59d2006-07-13 11:11:33 +000073static void
Lev Walkin74898072017-09-18 00:56:06 -070074save_object_as(PDU_t *st, enum asn_transfer_syntax syntax) {
75 asn_enc_rval_t rval; /* Return value */
Lev Walkin397d59d2006-07-13 11:11:33 +000076
Lev Walkin74898072017-09-18 00:56:06 -070077 buf_offset = 0;
Lev Walkin397d59d2006-07-13 11:11:33 +000078
Lev Walkin74898072017-09-18 00:56:06 -070079 rval = asn_encode(0, syntax, &asn_DEF_PDU, st, _buf_writer, 0);
Lev Walkin397d59d2006-07-13 11:11:33 +000080
Lev Walkin74898072017-09-18 00:56:06 -070081 if (rval.encoded == -1) {
82 fprintf(stderr,
83 "Cannot encode %s: %s\n",
84 rval.failed_type->name, strerror(errno));
85 assert(rval.encoded != -1);
86 return;
87 }
88
89 fprintf(stderr, "SAVED OBJECT IN SIZE %d/%zd\n", buf_offset, rval.encoded);
90
Lev Walkin4fe28822017-09-18 02:57:34 -070091 assert(buf_offset == rval.encoded);
Lev Walkin397d59d2006-07-13 11:11:33 +000092}
93
94static PDU_t *
Lev Walkin74898072017-09-18 00:56:06 -070095load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum asn_transfer_syntax syntax) {
Lev Walkin397d59d2006-07-13 11:11:33 +000096 asn_dec_rval_t rval;
Lev Walkin397d59d2006-07-13 11:11:33 +000097 PDU_t *st = 0;
Lev Walkinb7c40b62013-03-28 05:54:12 -070098 size_t csize = 1;
Lev Walkin397d59d2006-07-13 11:11:33 +000099
Lev Walkin397d59d2006-07-13 11:11:33 +0000100 if(getenv("INITIAL_CHUNK_SIZE"))
101 csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
102
103 /* Perform multiple iterations with multiple chunks sizes */
104 for(; csize < 20; csize += 1) {
105 int fbuf_offset = 0;
106 int fbuf_left = size;
107 int fbuf_chunk = csize;
108
Lev Walkinb7c40b62013-03-28 05:54:12 -0700109 fprintf(stderr, "LOADING OBJECT OF SIZE %zd, chunks %zd\n",
Lev Walkin397d59d2006-07-13 11:11:33 +0000110 size, csize);
111
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800112 if(st) ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin397d59d2006-07-13 11:11:33 +0000113 st = 0;
114
115 do {
Lev Walkin15d38f42014-09-17 01:19:31 -0700116 ASN_DEBUG("Decoding bytes %d..%d (left %d)",
Lev Walkin397d59d2006-07-13 11:11:33 +0000117 fbuf_offset,
118 fbuf_chunk < fbuf_left
119 ? fbuf_chunk : fbuf_left,
120 fbuf_left);
Lev Walkin15d38f42014-09-17 01:19:31 -0700121#ifdef EMIT_ASN_DEBUG
Lev Walkin397d59d2006-07-13 11:11:33 +0000122 if(st) {
123 fprintf(stderr, "=== currently ===\n");
124 asn_fprint(stderr, &asn_DEF_PDU, st);
125 fprintf(stderr, "=== end ===\n");
126 }
Lev Walkin15d38f42014-09-17 01:19:31 -0700127#endif
Lev Walkin74898072017-09-18 00:56:06 -0700128 rval = asn_decode(0, syntax, &asn_DEF_PDU, (void **)&st,
Lev Walkin397d59d2006-07-13 11:11:33 +0000129 fbuf + fbuf_offset,
130 fbuf_chunk < fbuf_left
131 ? fbuf_chunk : fbuf_left);
132 fbuf_offset += rval.consumed;
133 fbuf_left -= rval.consumed;
134 if(rval.code == RC_WMORE)
135 fbuf_chunk += 1; /* Give little more */
136 else
137 fbuf_chunk = csize; /* Back off */
138 } while(fbuf_left && rval.code == RC_WMORE);
139
140 if(expectation != EXP_BROKEN) {
141 assert(rval.code == RC_OK);
Lev Walkin74898072017-09-18 00:56:06 -0700142 if(syntax == ATS_BER) {
Lev Walkin5b63acf2014-01-14 01:48:37 -0800143 assert(fbuf_offset == (ssize_t)size);
Lev Walkin397d59d2006-07-13 11:11:33 +0000144 } else {
Lev Walkina75b2472017-09-18 00:38:43 -0700145 assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
Lev Walkin397d59d2006-07-13 11:11:33 +0000146 && fbuf[size - 1] == '\n')
Lev Walkin5b63acf2014-01-14 01:48:37 -0800147 || (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size
Lev Walkin397d59d2006-07-13 11:11:33 +0000148 && fbuf[size - 2] == '\r'
149 && fbuf[size - 1] == '\n')
150 );
151 }
152 } else {
153 assert(rval.code != RC_OK);
154 fprintf(stderr, "Failed, but this was expected\n");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800155 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin397d59d2006-07-13 11:11:33 +0000156 st = 0; /* ignore leak for now */
157 }
158 }
159
160 if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
161 return st;
162}
163
164static int
Lev Walkin5b63acf2014-01-14 01:48:37 -0800165xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
166 char *obuf = obufp;
167 char *nbuf = nbufp;
Lev Walkin397d59d2006-07-13 11:11:33 +0000168 char *oend = obuf + osize;
169 char *nend = nbuf + nsize;
170
171 if((osize && !nsize) || (!osize && nsize))
172 return 0; /* not equal apriori */
173
174 while(1) {
175 while(obuf < oend && isspace(*obuf)) obuf++;
176 while(nbuf < nend && isspace(*nbuf)) nbuf++;
177
178 if(obuf == oend || nbuf == nend) {
179 if(obuf == oend && nbuf == nend)
180 break;
181 fprintf(stderr, "%s data in reconstructed encoding\n",
182 (obuf == oend) ? "More" : "Less");
183 return 0;
184 }
185
186 if(*obuf != *nbuf) {
187 printf("%c%c != %c%c\n",
188 obuf[0], obuf[1],
189 nbuf[0], nbuf[1]);
190 return 0;
191 }
192 obuf++, nbuf++;
193 }
194
195 return 1;
196}
197
198static void
Lev Walkin5b63acf2014-01-14 01:48:37 -0800199process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
Lev Walkin397d59d2006-07-13 11:11:33 +0000200 PDU_t *st;
201
Lev Walkin74898072017-09-18 00:56:06 -0700202 st = load_object_from(expectation, fbuf, size, ATS_BASIC_XER);
Lev Walkin397d59d2006-07-13 11:11:33 +0000203 if(!st) return;
204
205 /* Save and re-load as DER */
Lev Walkin74898072017-09-18 00:56:06 -0700206 save_object_as(st, ATS_DER);
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300207 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin74898072017-09-18 00:56:06 -0700208 st = load_object_from(expectation, buf, buf_offset, ATS_BER);
Lev Walkin397d59d2006-07-13 11:11:33 +0000209 assert(st);
210
Lev Walkin2a744a72013-03-27 01:56:23 -0700211 save_object_as(st,
212 (expectation == EXP_CXER_EXACT
213 || expectation == EXP_CXER_DIFF)
Lev Walkin74898072017-09-18 00:56:06 -0700214 ? ATS_CANONICAL_XER : ATS_BASIC_XER);
Lev Walkin397d59d2006-07-13 11:11:33 +0000215 fprintf(stderr, "=== original ===\n");
216 fwrite(fbuf, 1, size, stderr);
217 fprintf(stderr, "=== re-encoded ===\n");
218 fwrite(buf, 1, buf_offset, stderr);
219 fprintf(stderr, "=== end ===\n");
220
221 switch(expectation) {
222 case EXP_DIFFERENT:
223 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
224 break;
225 case EXP_BROKEN:
226 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
227 break;
Lev Walkin2a744a72013-03-27 01:56:23 -0700228 case EXP_CXER_EXACT:
229 buf[buf_offset++] = '\n';
Lev Walkin5b63acf2014-01-14 01:48:37 -0800230 assert((ssize_t)size == buf_offset);
Lev Walkin2a744a72013-03-27 01:56:23 -0700231 assert(memcmp(fbuf, buf, size) == 0);
232 break;
233 case EXP_CXER_DIFF:
234 buf[buf_offset++] = '\n';
Lev Walkin5b63acf2014-01-14 01:48:37 -0800235 assert((ssize_t)size != buf_offset
Lev Walkin2a744a72013-03-27 01:56:23 -0700236 || memcmp(fbuf, buf, size));
237 break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000238 case EXP_OK:
239 assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
240 break;
241 }
242
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800243 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin397d59d2006-07-13 11:11:33 +0000244}
245
246/*
247 * Decode the .der files and try to regenerate them.
248 */
249static int
250process(const char *fname) {
Lev Walkine0d321a2014-09-11 01:28:57 -0700251 char prevdir[256];
Lev Walkin5b63acf2014-01-14 01:48:37 -0800252 unsigned char fbuf[4096];
Lev Walkin397d59d2006-07-13 11:11:33 +0000253 char *ext = strrchr(fname, '.');
254 enum expectation expectation;
Lev Walkin312e3e02017-10-19 03:49:50 -0700255 char *cwd;
Lev Walkin397d59d2006-07-13 11:11:33 +0000256 int ret;
257 int rd;
258 FILE *fp;
259
260 if(ext == 0 || strcmp(ext, ".in"))
261 return 0;
262
263 switch(ext[-1]) {
264 case 'B': /* The file is intentionally broken */
265 expectation = EXP_BROKEN; break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000266 case 'D': /* Reconstructing should yield different data */
267 expectation = EXP_DIFFERENT; break;
Lev Walkin2a744a72013-03-27 01:56:23 -0700268 case 'E': /* Byte to byte exact reconstruction */
269 expectation = EXP_CXER_EXACT; break;
270 case 'X': /* Should fail byte-to-byte comparison */
271 expectation = EXP_CXER_DIFF; break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000272 default:
273 expectation = EXP_OK; break;
274 }
275
276 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
277
Lev Walkin312e3e02017-10-19 03:49:50 -0700278 cwd = getcwd(prevdir, sizeof(prevdir));
279 assert(cwd != NULL);
Lev Walkine0d321a2014-09-11 01:28:57 -0700280 ret = chdir(SRCDIR_S "/data-70");
Lev Walkin397d59d2006-07-13 11:11:33 +0000281 assert(ret == 0);
282 fp = fopen(fname, "r");
Lev Walkine0d321a2014-09-11 01:28:57 -0700283 ret = chdir(prevdir);
Lev Walkin397d59d2006-07-13 11:11:33 +0000284 assert(ret == 0);
285 assert(fp);
286
287 rd = fread(fbuf, 1, sizeof(fbuf), fp);
288 fclose(fp);
289
Lev Walkin5b63acf2014-01-14 01:48:37 -0800290 assert(rd < (ssize_t)sizeof(fbuf)); /* expect small files */
Lev Walkin397d59d2006-07-13 11:11:33 +0000291
292 process_XER_data(expectation, fbuf, rd);
293
294 return 1;
295}
296
297int
298main() {
299 DIR *dir;
300 struct dirent *dent;
301 int processed_files = 0;
302 char *str;
303
304 /* Process a specific test file */
305 str = getenv("DATA_70_FILE");
Lev Walkin2a744a72013-03-27 01:56:23 -0700306 if(str && strncmp(str, "data-70-", 8) == 0) {
Lev Walkin397d59d2006-07-13 11:11:33 +0000307 process(str);
Lev Walkin2a744a72013-03-27 01:56:23 -0700308 return 0;
309 }
Lev Walkin397d59d2006-07-13 11:11:33 +0000310
Lev Walkine0d321a2014-09-11 01:28:57 -0700311 dir = opendir(SRCDIR_S "/data-70");
Lev Walkin397d59d2006-07-13 11:11:33 +0000312 assert(dir);
313
314 /*
315 * Process each file in that directory.
316 */
317 while((dent = readdir(dir))) {
318 if(strncmp(dent->d_name, "data-70-", 8) == 0)
319 if(process(dent->d_name))
320 processed_files++;
321 }
322
323 assert(processed_files);
324 closedir(dir);
325
326 return 0;
327}
328
Lev Walkinc8c286a2017-09-18 03:19:37 -0700329#endif