From a39386a3afe2d7e0cc717a49f970f53d974fda53 Mon Sep 17 00:00:00 2001 From: sangsu Date: Wed, 8 Jun 2016 10:17:41 +0900 Subject: Imported Upstream version 4.8 Change-Id: I25e57ece28b9ebb637a2b7356f57e11b7f2eb807 Signed-off-by: sangsu --- src/asn1Decoding.c | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'src/asn1Decoding.c') diff --git a/src/asn1Decoding.c b/src/asn1Decoding.c index 5649990..b52e791 100644 --- a/src/asn1Decoding.c +++ b/src/asn1Decoding.c @@ -34,7 +34,7 @@ #include "benchmark.h" static int decode (asn1_node definitions, const char *typeName, void *der, - int der_len, int benchmark); + int der_len, int benchmark, int strict); /* This feature is available in gcc versions 2.5 and later. */ #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) @@ -57,6 +57,8 @@ Decodes DER data in ENCODED file, for the ASN1TYPE element\n\ described in ASN.1 DEFINITIONS file, and print decoded structures.\n\ \n"); printf ("\ + -b, --benchmark perform a benchmark on decoding\n\ + -s, --strict use strict DER decoding\n\ -h, --help display this help and exit\n\ -v, --version output version information and exit\n"); emit_bug_reporting_address (); @@ -69,6 +71,9 @@ main (int argc, char *argv[]) { static const struct option long_options[] = { {"help", no_argument, 0, 'h'}, + {"strict", no_argument, 0, 's'}, + {"debug", no_argument, 0, 'd'}, + {"benchmark", no_argument, 0, 'b'}, {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; @@ -82,6 +87,7 @@ main (int argc, char *argv[]) int asn1_result = ASN1_SUCCESS; unsigned char *der; int der_len = 0, benchmark = 0; + int strict = 0, debug = 0; /* FILE *outputFile; */ set_program_name (argv[0]); @@ -92,7 +98,7 @@ main (int argc, char *argv[]) { option_result = - getopt_long (argc, argv, "hbvc", long_options, &option_index); + getopt_long (argc, argv, "hbdsvc", long_options, &option_index); if (option_result == -1) break; @@ -105,6 +111,12 @@ main (int argc, char *argv[]) case 'b': benchmark = 1; break; + case 'd': + debug = 1; + break; + case 's': + strict = 1; + break; case 'v': /* VERSION */ version_etc (stdout, program_name, PACKAGE, VERSION, "Fabio Fiorina", NULL); @@ -174,6 +186,12 @@ main (int argc, char *argv[]) der_len = tmplen; } + /* read_binary_file() returns a buffer with more data than required, + * with this reallocation we ensure that memory accesses outside the + * boundaries are detected */ + if (der != NULL && debug != 0) + der = realloc(der, der_len); + if (der == NULL) { fprintf (stderr, "asn1Decoding: could not read '%s'\n", @@ -186,7 +204,6 @@ main (int argc, char *argv[]) exit (1); } - /*****************************************/ /* ONLY FOR TEST */ /*****************************************/ @@ -202,7 +219,7 @@ main (int argc, char *argv[]) fclose(inputFile); */ - if (decode (definitions, typeName, der, der_len, benchmark) != ASN1_SUCCESS) + if (decode (definitions, typeName, der, der_len, benchmark, strict) != ASN1_SUCCESS) { asn1_delete_structure (&definitions); free (inputFileAsnName); @@ -228,7 +245,7 @@ main (int argc, char *argv[]) static int simple_decode (asn1_node definitions, const char *typeName, void *der, - int der_len, int benchmark) + int der_len, int benchmark, int strict) { int asn1_result; @@ -248,8 +265,12 @@ simple_decode (asn1_node definitions, const char *typeName, void *der, return asn1_result; } - asn1_result = - asn1_der_decoding (&structure, der, der_len, errorDescription); + if (strict != 0) + asn1_result = + asn1_der_decoding2(&structure, der, &der_len, ASN1_DECODE_FLAG_STRICT_DER, errorDescription); + else + asn1_result = + asn1_der_decoding (&structure, der, der_len, errorDescription); if (!benchmark) fprintf (stderr, "\nDecoding: %s\n", asn1_strerror (asn1_result)); @@ -272,19 +293,19 @@ simple_decode (asn1_node definitions, const char *typeName, void *der, static int decode (asn1_node definitions, const char *typeName, void *der, int der_len, - int benchmark) + int benchmark, int strict) { struct benchmark_st st; if (benchmark == 0) - return simple_decode (definitions, typeName, der, der_len, benchmark); + return simple_decode (definitions, typeName, der, der_len, benchmark, strict); else { start_benchmark (&st); do { - simple_decode (definitions, typeName, der, der_len, benchmark); + simple_decode (definitions, typeName, der, der_len, benchmark, strict); st.size++; } while (benchmark_must_finish == 0); -- cgit v1.2.3