2005-02-18 10:13:44 +00:00
|
|
|
/*
|
|
|
|
* Mode of operation:
|
|
|
|
* Each of the *.in files is XER-decoded, then converted into DER,
|
|
|
|
* then decoded from DER and encoded into XER again. The resulting
|
2013-03-27 08:56:23 +00:00
|
|
|
* stream is compared with the corresponding .out file.
|
2005-02-18 10:13:44 +00:00
|
|
|
*/
|
2005-02-14 17:20:23 +00:00
|
|
|
#undef NDEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2014-09-11 08:28:57 +00:00
|
|
|
#include <unistd.h> /* for chdir(2), getcwd(3) */
|
2005-02-14 17:20:23 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
2013-03-27 08:56:23 +00:00
|
|
|
#include <ctype.h> /* for isspace(3) */
|
2005-02-14 17:20:23 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <PDU.h>
|
|
|
|
|
2014-09-11 08:28:57 +00:00
|
|
|
#ifndef SRCDIR
|
|
|
|
#define SRCDIR_S ".."
|
|
|
|
#else
|
|
|
|
#define STRINGIFY_MACRO2(x) #x
|
|
|
|
#define STRINGIFY_MACRO(x) STRINGIFY_MACRO2(x)
|
|
|
|
#define SRCDIR_S STRINGIFY_MACRO(SRCDIR)
|
|
|
|
#endif
|
|
|
|
|
2017-10-19 09:16:35 +00:00
|
|
|
#ifdef ENABLE_LIBFUZZER
|
|
|
|
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
|
|
|
|
PDU_t *st = 0;
|
|
|
|
asn_dec_rval_t rval;
|
|
|
|
rval = asn_decode(0, ATS_BASIC_XER, &asn_DEF_PDU, (void **)&st, Data, Size);
|
|
|
|
assert(rval.consumed <= Size);
|
|
|
|
ASN_STRUCT_FREE(asn_DEF_PDU, st);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2005-02-14 17:20:23 +00:00
|
|
|
enum expectation {
|
|
|
|
EXP_OK, /* Encoding/decoding must succeed */
|
|
|
|
EXP_BROKEN, /* Decoding must fail */
|
|
|
|
EXP_DIFFERENT, /* Reconstruction will yield different encoding */
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned char buf[4096];
|
|
|
|
static int buf_offset;
|
|
|
|
|
|
|
|
static int
|
|
|
|
_buf_writer(const void *buffer, size_t size, void *app_key) {
|
|
|
|
unsigned char *b, *bend;
|
|
|
|
(void)app_key;
|
|
|
|
assert(buf_offset + size < sizeof(buf));
|
|
|
|
memcpy(buf + buf_offset, buffer, size);
|
|
|
|
b = buf + buf_offset;
|
|
|
|
bend = b + size;
|
2005-02-18 14:26:30 +00:00
|
|
|
fprintf(stderr, "=> [");
|
2013-03-27 08:56:23 +00:00
|
|
|
for(; b < bend; b++)
|
|
|
|
fprintf(stderr, "%c", *b);
|
2013-03-28 11:02:13 +00:00
|
|
|
fprintf(stderr, "]:%zd\n", size);
|
2005-02-14 17:20:23 +00:00
|
|
|
buf_offset += size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-02-18 10:13:44 +00:00
|
|
|
static void
|
2017-09-18 07:56:06 +00:00
|
|
|
save_object_as(PDU_t *st, enum asn_transfer_syntax syntax) {
|
|
|
|
asn_enc_rval_t rval; /* Return value */
|
|
|
|
|
|
|
|
buf_offset = 0;
|
|
|
|
|
|
|
|
rval = asn_encode(0, syntax, &asn_DEF_PDU, st, _buf_writer, 0);
|
|
|
|
|
|
|
|
if (rval.encoded == -1) {
|
2005-02-14 17:20:23 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Cannot encode %s: %s\n",
|
|
|
|
rval.failed_type->name, strerror(errno));
|
|
|
|
assert(rval.encoded != -1);
|
2005-02-18 10:13:44 +00:00
|
|
|
return;
|
2017-09-18 07:56:06 +00:00
|
|
|
}
|
2005-02-14 17:20:23 +00:00
|
|
|
|
2017-09-18 09:57:34 +00:00
|
|
|
fprintf(stderr, "SAVED OBJECT IN SIZE %d/%zu\n", buf_offset, rval.encoded);
|
2017-09-18 07:56:06 +00:00
|
|
|
|
2017-09-18 09:57:34 +00:00
|
|
|
assert(buf_offset == rval.encoded);
|
2005-02-14 17:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PDU_t *
|
2017-09-18 07:56:06 +00:00
|
|
|
load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum asn_transfer_syntax syntax) {
|
2005-02-14 17:20:23 +00:00
|
|
|
asn_dec_rval_t rval;
|
|
|
|
PDU_t *st = 0;
|
2013-03-28 12:54:12 +00:00
|
|
|
size_t csize = 1;
|
2005-02-14 17:20:23 +00:00
|
|
|
|
2005-02-18 14:26:30 +00:00
|
|
|
if(getenv("INITIAL_CHUNK_SIZE"))
|
|
|
|
csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
|
2005-02-14 17:20:23 +00:00
|
|
|
|
|
|
|
/* Perform multiple iterations with multiple chunks sizes */
|
2005-02-18 14:26:30 +00:00
|
|
|
for(; csize < 20; csize += 1) {
|
2005-02-14 17:20:23 +00:00
|
|
|
int fbuf_offset = 0;
|
|
|
|
int fbuf_left = size;
|
|
|
|
int fbuf_chunk = csize;
|
|
|
|
|
2013-03-28 12:54:12 +00:00
|
|
|
fprintf(stderr, "LOADING OBJECT OF SIZE %zd, chunks %zd\n",
|
2005-02-18 14:26:30 +00:00
|
|
|
size, csize);
|
|
|
|
|
Move type operations to another structure
Every type has free, print, check_constraints, ber_decoder, der_encoder,
xer_decoder, xer_encoder, uper_decoder, uper_encoder and outmost_tag
operations. We move them out to a separate structure asn_TYPE_operation_t.
Combined with previous logic simplification, these operations are based
on ASN.1 basic types, constructed types and string types. So we can
reduce the space occupied by asn_TYPE_descriptor_t variables.
2017-08-19 17:25:45 +00:00
|
|
|
if(st) ASN_STRUCT_FREE(asn_DEF_PDU, st);
|
2005-02-14 17:20:23 +00:00
|
|
|
st = 0;
|
|
|
|
|
|
|
|
do {
|
2014-09-17 08:19:31 +00:00
|
|
|
ASN_DEBUG("Decoding bytes %d..%d (left %d)",
|
2005-02-18 14:26:30 +00:00
|
|
|
fbuf_offset,
|
|
|
|
fbuf_chunk < fbuf_left
|
|
|
|
? fbuf_chunk : fbuf_left,
|
|
|
|
fbuf_left);
|
2014-09-17 08:19:31 +00:00
|
|
|
#ifdef EMIT_ASN_DEBUG
|
2005-02-18 14:26:30 +00:00
|
|
|
if(st) {
|
|
|
|
fprintf(stderr, "=== currently ===\n");
|
|
|
|
asn_fprint(stderr, &asn_DEF_PDU, st);
|
|
|
|
fprintf(stderr, "=== end ===\n");
|
|
|
|
}
|
2014-09-17 08:19:31 +00:00
|
|
|
#endif
|
2017-09-18 07:56:06 +00:00
|
|
|
rval = asn_decode(0, syntax, &asn_DEF_PDU, (void **)&st,
|
2005-02-14 17:20:23 +00:00
|
|
|
fbuf + fbuf_offset,
|
|
|
|
fbuf_chunk < fbuf_left
|
2005-02-18 10:13:44 +00:00
|
|
|
? fbuf_chunk : fbuf_left);
|
2005-02-14 17:20:23 +00:00
|
|
|
fbuf_offset += rval.consumed;
|
|
|
|
fbuf_left -= rval.consumed;
|
|
|
|
if(rval.code == RC_WMORE)
|
|
|
|
fbuf_chunk += 1; /* Give little more */
|
|
|
|
else
|
|
|
|
fbuf_chunk = csize; /* Back off */
|
|
|
|
} while(fbuf_left && rval.code == RC_WMORE);
|
|
|
|
|
|
|
|
if(expectation != EXP_BROKEN) {
|
|
|
|
assert(rval.code == RC_OK);
|
2017-09-18 07:56:06 +00:00
|
|
|
if(syntax == ATS_BER) {
|
2014-01-14 09:48:37 +00:00
|
|
|
assert(fbuf_offset == (ssize_t)size);
|
2005-02-18 10:13:44 +00:00
|
|
|
} else {
|
2017-09-18 07:38:43 +00:00
|
|
|
assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
|
2005-02-14 17:20:23 +00:00
|
|
|
&& fbuf[size - 1] == '\n')
|
2014-01-14 09:48:37 +00:00
|
|
|
|| (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size
|
2005-02-14 17:20:23 +00:00
|
|
|
&& fbuf[size - 2] == '\r'
|
|
|
|
&& fbuf[size - 1] == '\n')
|
2005-02-18 10:13:44 +00:00
|
|
|
);
|
|
|
|
}
|
2005-02-14 17:20:23 +00:00
|
|
|
} else {
|
|
|
|
assert(rval.code != RC_OK);
|
|
|
|
fprintf(stderr, "Failed, but this was expected\n");
|
Move type operations to another structure
Every type has free, print, check_constraints, ber_decoder, der_encoder,
xer_decoder, xer_encoder, uper_decoder, uper_encoder and outmost_tag
operations. We move them out to a separate structure asn_TYPE_operation_t.
Combined with previous logic simplification, these operations are based
on ASN.1 basic types, constructed types and string types. So we can
reduce the space occupied by asn_TYPE_descriptor_t variables.
2017-08-19 17:25:45 +00:00
|
|
|
ASN_STRUCT_FREE(asn_DEF_PDU, st);
|
2005-02-14 17:20:23 +00:00
|
|
|
st = 0; /* ignore leak for now */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2005-02-18 10:13:44 +00:00
|
|
|
static int
|
2014-01-14 09:48:37 +00:00
|
|
|
xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
|
|
|
|
char *obuf = obufp;
|
|
|
|
char *nbuf = nbufp;
|
2005-02-18 10:13:44 +00:00
|
|
|
char *oend = obuf + osize;
|
|
|
|
char *nend = nbuf + nsize;
|
|
|
|
|
|
|
|
if((osize && !nsize) || (!osize && nsize))
|
|
|
|
return 0; /* not equal apriori */
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
while(obuf < oend && isspace(*obuf)) obuf++;
|
|
|
|
while(nbuf < nend && isspace(*nbuf)) nbuf++;
|
|
|
|
|
|
|
|
if(obuf == oend || nbuf == nend) {
|
|
|
|
if(obuf == oend && nbuf == nend)
|
|
|
|
break;
|
|
|
|
fprintf(stderr, "%s data in reconstructed encoding\n",
|
|
|
|
(obuf == oend) ? "More" : "Less");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*obuf != *nbuf) {
|
|
|
|
printf("%c%c != %c%c\n",
|
|
|
|
obuf[0], obuf[1],
|
|
|
|
nbuf[0], nbuf[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
obuf++, nbuf++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2005-02-14 17:20:23 +00:00
|
|
|
|
|
|
|
static void
|
2014-01-14 09:48:37 +00:00
|
|
|
process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
|
2005-02-14 17:20:23 +00:00
|
|
|
PDU_t *st;
|
|
|
|
|
2017-09-18 07:56:06 +00:00
|
|
|
st = load_object_from(expectation, fbuf, size, ATS_BASIC_XER);
|
2005-02-14 17:20:23 +00:00
|
|
|
if(!st) return;
|
|
|
|
|
2005-02-18 10:13:44 +00:00
|
|
|
/* Save and re-load as DER */
|
2017-09-18 07:56:06 +00:00
|
|
|
save_object_as(st, ATS_DER);
|
2017-10-09 20:40:17 +00:00
|
|
|
ASN_STRUCT_FREE(asn_DEF_PDU, st);
|
2017-09-18 07:56:06 +00:00
|
|
|
st = load_object_from(expectation, buf, buf_offset, ATS_BER);
|
2005-02-18 10:13:44 +00:00
|
|
|
assert(st);
|
|
|
|
|
2017-09-18 07:56:06 +00:00
|
|
|
save_object_as(st, ATS_BASIC_XER);
|
2005-02-18 10:13:44 +00:00
|
|
|
fprintf(stderr, "=== original ===\n");
|
|
|
|
fwrite(fbuf, 1, size, stderr);
|
|
|
|
fprintf(stderr, "=== re-encoded ===\n");
|
|
|
|
fwrite(buf, 1, buf_offset, stderr);
|
|
|
|
fprintf(stderr, "=== end ===\n");
|
2005-02-14 17:20:23 +00:00
|
|
|
|
|
|
|
switch(expectation) {
|
|
|
|
case EXP_DIFFERENT:
|
2005-02-18 10:13:44 +00:00
|
|
|
assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
|
2005-02-14 17:20:23 +00:00
|
|
|
break;
|
|
|
|
case EXP_BROKEN:
|
2005-02-18 10:13:44 +00:00
|
|
|
assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
|
2005-02-14 17:20:23 +00:00
|
|
|
break;
|
|
|
|
case EXP_OK:
|
2005-02-18 10:13:44 +00:00
|
|
|
assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
|
2005-02-14 17:20:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
Move type operations to another structure
Every type has free, print, check_constraints, ber_decoder, der_encoder,
xer_decoder, xer_encoder, uper_decoder, uper_encoder and outmost_tag
operations. We move them out to a separate structure asn_TYPE_operation_t.
Combined with previous logic simplification, these operations are based
on ASN.1 basic types, constructed types and string types. So we can
reduce the space occupied by asn_TYPE_descriptor_t variables.
2017-08-19 17:25:45 +00:00
|
|
|
ASN_STRUCT_FREE(asn_DEF_PDU, st);
|
2005-02-14 17:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decode the .der files and try to regenerate them.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
process(const char *fname) {
|
2014-09-11 08:28:57 +00:00
|
|
|
char prevdir[256];
|
2014-01-14 09:48:37 +00:00
|
|
|
unsigned char fbuf[4096];
|
2005-02-14 17:20:23 +00:00
|
|
|
char *ext = strrchr(fname, '.');
|
|
|
|
enum expectation expectation;
|
2017-10-19 10:39:23 +00:00
|
|
|
char *cwd;
|
2005-02-14 17:20:23 +00:00
|
|
|
int ret;
|
|
|
|
int rd;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if(ext == 0 || strcmp(ext, ".in"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch(ext[-1]) {
|
|
|
|
case 'B': /* The file is intentionally broken */
|
|
|
|
expectation = EXP_BROKEN; break;
|
2013-03-27 08:56:23 +00:00
|
|
|
case 'X':
|
2005-02-14 17:20:23 +00:00
|
|
|
case 'D': /* Reconstructing should yield different data */
|
|
|
|
expectation = EXP_DIFFERENT; break;
|
2013-03-27 08:56:23 +00:00
|
|
|
case 'E':
|
2005-02-14 17:20:23 +00:00
|
|
|
default:
|
|
|
|
expectation = EXP_OK; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "\nProcessing file [../%s]\n", fname);
|
|
|
|
|
2017-10-19 10:39:23 +00:00
|
|
|
cwd = getcwd(prevdir, sizeof(prevdir));
|
|
|
|
assert(cwd != NULL);
|
2014-09-11 08:28:57 +00:00
|
|
|
ret = chdir(SRCDIR_S "/data-70");
|
2005-02-14 17:20:23 +00:00
|
|
|
assert(ret == 0);
|
|
|
|
fp = fopen(fname, "r");
|
2014-09-11 08:28:57 +00:00
|
|
|
ret = chdir(prevdir);
|
2005-02-14 17:20:23 +00:00
|
|
|
assert(ret == 0);
|
|
|
|
assert(fp);
|
|
|
|
|
|
|
|
rd = fread(fbuf, 1, sizeof(fbuf), fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
2014-01-14 09:48:37 +00:00
|
|
|
assert(rd > 0 && (size_t)rd < sizeof(fbuf)); /* expect small files */
|
2005-02-14 17:20:23 +00:00
|
|
|
|
2005-02-18 10:13:44 +00:00
|
|
|
process_XER_data(expectation, fbuf, rd);
|
2005-02-14 17:20:23 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main() {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *dent;
|
|
|
|
int processed_files = 0;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
/* Process a specific test file */
|
|
|
|
str = getenv("DATA_70_FILE");
|
2013-03-27 08:56:23 +00:00
|
|
|
if(str && strncmp(str, "data-70-", 8) == 0)
|
2005-02-14 17:20:23 +00:00
|
|
|
process(str);
|
|
|
|
|
2014-09-11 08:28:57 +00:00
|
|
|
dir = opendir(SRCDIR_S "/data-70");
|
2005-02-18 10:13:44 +00:00
|
|
|
assert(dir);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process each file in that directory.
|
|
|
|
*/
|
2005-02-14 17:20:23 +00:00
|
|
|
while((dent = readdir(dir))) {
|
|
|
|
if(strncmp(dent->d_name, "data-70-", 8) == 0)
|
|
|
|
if(process(dent->d_name))
|
|
|
|
processed_files++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(processed_files);
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-18 10:19:37 +00:00
|
|
|
#endif
|