blob: 8152346e0dd3b1d761a2b7ef4e834fdc55a2d72b [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 Walkin397d59d2006-07-13 11:11:33 +000028enum expectation {
29 EXP_OK, /* Encoding/decoding must succeed */
Lev Walkin2a744a72013-03-27 01:56:23 -070030 EXP_CXER_EXACT, /* Encoding/decoding using CXER must be exact */
31 EXP_CXER_DIFF, /* Encoding/decoding using CXER must be different */
Lev Walkin397d59d2006-07-13 11:11:33 +000032 EXP_BROKEN, /* Decoding must fail */
33 EXP_DIFFERENT, /* Reconstruction will yield different encoding */
34};
35
36static unsigned char buf[4096];
37static int buf_offset;
38
39static int
40_buf_writer(const void *buffer, size_t size, void *app_key) {
41 unsigned char *b, *bend;
42 (void)app_key;
43 assert(buf_offset + size < sizeof(buf));
44 memcpy(buf + buf_offset, buffer, size);
45 b = buf + buf_offset;
46 bend = b + size;
47 fprintf(stderr, "=> [");
Lev Walkin2a744a72013-03-27 01:56:23 -070048 for(; b < bend; b++) {
49 if(*b >= 32 && *b < 127 && *b != '%')
50 fprintf(stderr, "%c", *b);
51 else
52 fprintf(stderr, "%%%02x", *b);
53 }
Lev Walkin1715b322013-03-28 04:02:13 -070054 fprintf(stderr, "]:%zd\n", size);
Lev Walkin397d59d2006-07-13 11:11:33 +000055 buf_offset += size;
56 return 0;
57}
58
Lev Walkin397d59d2006-07-13 11:11:33 +000059static void
Lev Walkin74898072017-09-18 00:56:06 -070060save_object_as(PDU_t *st, enum asn_transfer_syntax syntax) {
61 asn_enc_rval_t rval; /* Return value */
Lev Walkin397d59d2006-07-13 11:11:33 +000062
Lev Walkin74898072017-09-18 00:56:06 -070063 buf_offset = 0;
Lev Walkin397d59d2006-07-13 11:11:33 +000064
Lev Walkin74898072017-09-18 00:56:06 -070065 rval = asn_encode(0, syntax, &asn_DEF_PDU, st, _buf_writer, 0);
Lev Walkin397d59d2006-07-13 11:11:33 +000066
Lev Walkin74898072017-09-18 00:56:06 -070067 if (rval.encoded == -1) {
68 fprintf(stderr,
69 "Cannot encode %s: %s\n",
70 rval.failed_type->name, strerror(errno));
71 assert(rval.encoded != -1);
72 return;
73 }
74
75 fprintf(stderr, "SAVED OBJECT IN SIZE %d/%zd\n", buf_offset, rval.encoded);
76
Lev Walkin4fe28822017-09-18 02:57:34 -070077 assert(buf_offset == rval.encoded);
Lev Walkin397d59d2006-07-13 11:11:33 +000078}
79
80static PDU_t *
Lev Walkin74898072017-09-18 00:56:06 -070081load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum asn_transfer_syntax syntax) {
Lev Walkin397d59d2006-07-13 11:11:33 +000082 asn_dec_rval_t rval;
Lev Walkin397d59d2006-07-13 11:11:33 +000083 PDU_t *st = 0;
Lev Walkinb7c40b62013-03-28 05:54:12 -070084 size_t csize = 1;
Lev Walkin397d59d2006-07-13 11:11:33 +000085
Lev Walkin397d59d2006-07-13 11:11:33 +000086 if(getenv("INITIAL_CHUNK_SIZE"))
87 csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
88
89 /* Perform multiple iterations with multiple chunks sizes */
90 for(; csize < 20; csize += 1) {
91 int fbuf_offset = 0;
92 int fbuf_left = size;
93 int fbuf_chunk = csize;
94
Lev Walkinb7c40b62013-03-28 05:54:12 -070095 fprintf(stderr, "LOADING OBJECT OF SIZE %zd, chunks %zd\n",
Lev Walkin397d59d2006-07-13 11:11:33 +000096 size, csize);
97
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080098 if(st) ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin397d59d2006-07-13 11:11:33 +000099 st = 0;
100
101 do {
Lev Walkin15d38f42014-09-17 01:19:31 -0700102 ASN_DEBUG("Decoding bytes %d..%d (left %d)",
Lev Walkin397d59d2006-07-13 11:11:33 +0000103 fbuf_offset,
104 fbuf_chunk < fbuf_left
105 ? fbuf_chunk : fbuf_left,
106 fbuf_left);
Lev Walkin15d38f42014-09-17 01:19:31 -0700107#ifdef EMIT_ASN_DEBUG
Lev Walkin397d59d2006-07-13 11:11:33 +0000108 if(st) {
109 fprintf(stderr, "=== currently ===\n");
110 asn_fprint(stderr, &asn_DEF_PDU, st);
111 fprintf(stderr, "=== end ===\n");
112 }
Lev Walkin15d38f42014-09-17 01:19:31 -0700113#endif
Lev Walkin74898072017-09-18 00:56:06 -0700114 rval = asn_decode(0, syntax, &asn_DEF_PDU, (void **)&st,
Lev Walkin397d59d2006-07-13 11:11:33 +0000115 fbuf + fbuf_offset,
116 fbuf_chunk < fbuf_left
117 ? fbuf_chunk : fbuf_left);
118 fbuf_offset += rval.consumed;
119 fbuf_left -= rval.consumed;
120 if(rval.code == RC_WMORE)
121 fbuf_chunk += 1; /* Give little more */
122 else
123 fbuf_chunk = csize; /* Back off */
124 } while(fbuf_left && rval.code == RC_WMORE);
125
126 if(expectation != EXP_BROKEN) {
127 assert(rval.code == RC_OK);
Lev Walkin74898072017-09-18 00:56:06 -0700128 if(syntax == ATS_BER) {
Lev Walkin5b63acf2014-01-14 01:48:37 -0800129 assert(fbuf_offset == (ssize_t)size);
Lev Walkin397d59d2006-07-13 11:11:33 +0000130 } else {
Lev Walkina75b2472017-09-18 00:38:43 -0700131 assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
Lev Walkin397d59d2006-07-13 11:11:33 +0000132 && fbuf[size - 1] == '\n')
Lev Walkin5b63acf2014-01-14 01:48:37 -0800133 || (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size
Lev Walkin397d59d2006-07-13 11:11:33 +0000134 && fbuf[size - 2] == '\r'
135 && fbuf[size - 1] == '\n')
136 );
137 }
138 } else {
139 assert(rval.code != RC_OK);
140 fprintf(stderr, "Failed, but this was expected\n");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800141 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin397d59d2006-07-13 11:11:33 +0000142 st = 0; /* ignore leak for now */
143 }
144 }
145
146 if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
147 return st;
148}
149
150static int
Lev Walkin5b63acf2014-01-14 01:48:37 -0800151xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
152 char *obuf = obufp;
153 char *nbuf = nbufp;
Lev Walkin397d59d2006-07-13 11:11:33 +0000154 char *oend = obuf + osize;
155 char *nend = nbuf + nsize;
156
157 if((osize && !nsize) || (!osize && nsize))
158 return 0; /* not equal apriori */
159
160 while(1) {
161 while(obuf < oend && isspace(*obuf)) obuf++;
162 while(nbuf < nend && isspace(*nbuf)) nbuf++;
163
164 if(obuf == oend || nbuf == nend) {
165 if(obuf == oend && nbuf == nend)
166 break;
167 fprintf(stderr, "%s data in reconstructed encoding\n",
168 (obuf == oend) ? "More" : "Less");
169 return 0;
170 }
171
172 if(*obuf != *nbuf) {
173 printf("%c%c != %c%c\n",
174 obuf[0], obuf[1],
175 nbuf[0], nbuf[1]);
176 return 0;
177 }
178 obuf++, nbuf++;
179 }
180
181 return 1;
182}
183
184static void
Lev Walkin5b63acf2014-01-14 01:48:37 -0800185process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
Lev Walkin397d59d2006-07-13 11:11:33 +0000186 PDU_t *st;
187
Lev Walkin74898072017-09-18 00:56:06 -0700188 st = load_object_from(expectation, fbuf, size, ATS_BASIC_XER);
Lev Walkin397d59d2006-07-13 11:11:33 +0000189 if(!st) return;
190
191 /* Save and re-load as DER */
Lev Walkin74898072017-09-18 00:56:06 -0700192 save_object_as(st, ATS_DER);
193 st = load_object_from(expectation, buf, buf_offset, ATS_BER);
Lev Walkin397d59d2006-07-13 11:11:33 +0000194 assert(st);
195
Lev Walkin2a744a72013-03-27 01:56:23 -0700196 save_object_as(st,
197 (expectation == EXP_CXER_EXACT
198 || expectation == EXP_CXER_DIFF)
Lev Walkin74898072017-09-18 00:56:06 -0700199 ? ATS_CANONICAL_XER : ATS_BASIC_XER);
Lev Walkin397d59d2006-07-13 11:11:33 +0000200 fprintf(stderr, "=== original ===\n");
201 fwrite(fbuf, 1, size, stderr);
202 fprintf(stderr, "=== re-encoded ===\n");
203 fwrite(buf, 1, buf_offset, stderr);
204 fprintf(stderr, "=== end ===\n");
205
206 switch(expectation) {
207 case EXP_DIFFERENT:
208 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
209 break;
210 case EXP_BROKEN:
211 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
212 break;
Lev Walkin2a744a72013-03-27 01:56:23 -0700213 case EXP_CXER_EXACT:
214 buf[buf_offset++] = '\n';
Lev Walkin5b63acf2014-01-14 01:48:37 -0800215 assert((ssize_t)size == buf_offset);
Lev Walkin2a744a72013-03-27 01:56:23 -0700216 assert(memcmp(fbuf, buf, size) == 0);
217 break;
218 case EXP_CXER_DIFF:
219 buf[buf_offset++] = '\n';
Lev Walkin5b63acf2014-01-14 01:48:37 -0800220 assert((ssize_t)size != buf_offset
Lev Walkin2a744a72013-03-27 01:56:23 -0700221 || memcmp(fbuf, buf, size));
222 break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000223 case EXP_OK:
224 assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
225 break;
226 }
227
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800228 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin397d59d2006-07-13 11:11:33 +0000229}
230
231/*
232 * Decode the .der files and try to regenerate them.
233 */
234static int
235process(const char *fname) {
Lev Walkine0d321a2014-09-11 01:28:57 -0700236 char prevdir[256];
Lev Walkin5b63acf2014-01-14 01:48:37 -0800237 unsigned char fbuf[4096];
Lev Walkin397d59d2006-07-13 11:11:33 +0000238 char *ext = strrchr(fname, '.');
239 enum expectation expectation;
240 int ret;
241 int rd;
242 FILE *fp;
243
244 if(ext == 0 || strcmp(ext, ".in"))
245 return 0;
246
247 switch(ext[-1]) {
248 case 'B': /* The file is intentionally broken */
249 expectation = EXP_BROKEN; break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000250 case 'D': /* Reconstructing should yield different data */
251 expectation = EXP_DIFFERENT; break;
Lev Walkin2a744a72013-03-27 01:56:23 -0700252 case 'E': /* Byte to byte exact reconstruction */
253 expectation = EXP_CXER_EXACT; break;
254 case 'X': /* Should fail byte-to-byte comparison */
255 expectation = EXP_CXER_DIFF; break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000256 default:
257 expectation = EXP_OK; break;
258 }
259
260 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
261
Lev Walkine0d321a2014-09-11 01:28:57 -0700262 getcwd(prevdir, sizeof(prevdir));
263 ret = chdir(SRCDIR_S "/data-70");
Lev Walkin397d59d2006-07-13 11:11:33 +0000264 assert(ret == 0);
265 fp = fopen(fname, "r");
Lev Walkine0d321a2014-09-11 01:28:57 -0700266 ret = chdir(prevdir);
Lev Walkin397d59d2006-07-13 11:11:33 +0000267 assert(ret == 0);
268 assert(fp);
269
270 rd = fread(fbuf, 1, sizeof(fbuf), fp);
271 fclose(fp);
272
Lev Walkin5b63acf2014-01-14 01:48:37 -0800273 assert(rd < (ssize_t)sizeof(fbuf)); /* expect small files */
Lev Walkin397d59d2006-07-13 11:11:33 +0000274
275 process_XER_data(expectation, fbuf, rd);
276
277 return 1;
278}
279
Lev Walkinc8c286a2017-09-18 03:19:37 -0700280#ifdef ENABLE_LIBFUZZER
281
282int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
283 PDU_t *st = 0;
284 asn_dec_rval_t rval;
285 rval = asn_decode(0, ATS_BASIC_XER, &asn_DEF_PDU, (void **)&st, Data, Size);
286 assert(rval.consumed <= Size);
287 ASN_STRUCT_FREE(asn_DEF_PDU, st);
288 return 0;
289}
290
291#else
292
Lev Walkin397d59d2006-07-13 11:11:33 +0000293int
294main() {
295 DIR *dir;
296 struct dirent *dent;
297 int processed_files = 0;
298 char *str;
299
300 /* Process a specific test file */
301 str = getenv("DATA_70_FILE");
Lev Walkin2a744a72013-03-27 01:56:23 -0700302 if(str && strncmp(str, "data-70-", 8) == 0) {
Lev Walkin397d59d2006-07-13 11:11:33 +0000303 process(str);
Lev Walkin2a744a72013-03-27 01:56:23 -0700304 return 0;
305 }
Lev Walkin397d59d2006-07-13 11:11:33 +0000306
Lev Walkine0d321a2014-09-11 01:28:57 -0700307 dir = opendir(SRCDIR_S "/data-70");
Lev Walkin397d59d2006-07-13 11:11:33 +0000308 assert(dir);
309
310 /*
311 * Process each file in that directory.
312 */
313 while((dent = readdir(dir))) {
314 if(strncmp(dent->d_name, "data-70-", 8) == 0)
315 if(process(dent->d_name))
316 processed_files++;
317 }
318
319 assert(processed_files);
320 closedir(dir);
321
322 return 0;
323}
324
Lev Walkinc8c286a2017-09-18 03:19:37 -0700325#endif