Merge pull request #350 from gilles-peskine-arm/asn1-tests-parse_prefixes-trailing_garbage

test_suite_asn1parse: improve testing of trailing garbage in parse_prefixes
This commit is contained in:
Janos Follath 2020-02-05 15:40:22 +00:00 committed by GitHub
commit f317dc4918
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 29 deletions

View File

@ -1,56 +1,59 @@
Empty length Empty length
parse_prefixes:"04":0:MBEDTLS_ERR_ASN1_INVALID_LENGTH parse_prefixes:"04":MBEDTLS_ERR_ASN1_OUT_OF_DATA:UNPREDICTABLE_RESULT
Incomplete length
parse_prefixes:"0481":MBEDTLS_ERR_ASN1_OUT_OF_DATA:UNPREDICTABLE_RESULT
Prefixes of OCTET STRING, length=0 Prefixes of OCTET STRING, length=0
parse_prefixes:"04007e":2:0 parse_prefixes:"0400":0:0
Prefixes of OCTET STRING, length=0 (0 length bytes) Prefixes of OCTET STRING, length=0 (0 length bytes)
parse_prefixes:"04807e":2:MBEDTLS_ERR_ASN1_INVALID_LENGTH parse_prefixes:"0480":MBEDTLS_ERR_ASN1_INVALID_LENGTH:MBEDTLS_ERR_ASN1_INVALID_LENGTH
Prefixes of OCTET STRING, length=1 Prefixes of OCTET STRING, length=1
parse_prefixes:"0401417e":3:0 parse_prefixes:"040141":0:0
Prefixes of OCTET STRING, length=2 Prefixes of OCTET STRING, length=2
parse_prefixes:"040241427e":4:0 parse_prefixes:"04024142":0:0
Prefixes of BOOLEAN, length=0 Prefixes of BOOLEAN, length=0
parse_prefixes:"01007e":2:MBEDTLS_ERR_ASN1_INVALID_LENGTH parse_prefixes:"0100":MBEDTLS_ERR_ASN1_INVALID_LENGTH:MBEDTLS_ERR_ASN1_INVALID_LENGTH
Prefixes of BOOLEAN, length=1 Prefixes of BOOLEAN, length=1
parse_prefixes:"0101007e":3:0 parse_prefixes:"010100":0:0
Prefixes of BOOLEAN, length=2 Prefixes of BOOLEAN, length=2
parse_prefixes:"010200007e":4:MBEDTLS_ERR_ASN1_INVALID_LENGTH parse_prefixes:"01020000":MBEDTLS_ERR_ASN1_INVALID_LENGTH:MBEDTLS_ERR_ASN1_INVALID_LENGTH
Prefixes of INTEGER, length=1 Prefixes of INTEGER, length=1
parse_prefixes:"0201417e":3:0 parse_prefixes:"020141":0:0
Prefixes of INTEGER, length=2 Prefixes of INTEGER, length=2
parse_prefixes:"020241427e":4:0 parse_prefixes:"02024142":0:0
Prefixes of INTEGER, length=5 Prefixes of INTEGER, length=5
parse_prefixes:"020541424344457e":7:0 parse_prefixes:"02054142434445":0:0
Prefixes of empty BIT STRING Prefixes of empty BIT STRING
parse_prefixes:"03007e":2:MBEDTLS_ERR_ASN1_OUT_OF_DATA parse_prefixes:"0300":MBEDTLS_ERR_ASN1_OUT_OF_DATA:UNPREDICTABLE_RESULT
Prefixes of BIT STRING, unused_bits=0, payload_length=0 Prefixes of BIT STRING, unused_bits=0, payload_length=0
parse_prefixes:"030100":3:0 parse_prefixes:"030100":0:MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
Prefixes of BIT STRING, unused_bits=0, payload_length=1 Prefixes of BIT STRING, unused_bits=0, payload_length=1
parse_prefixes:"0302002a":4:0 parse_prefixes:"0302002a":0:MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
Prefixes of BIT STRING, unused_bits=1, payload_length=1 Prefixes of BIT STRING, unused_bits=1, payload_length=1
parse_prefixes:"0302012a":4:0 parse_prefixes:"0302012a":0:MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
Prefixes of empty SEQUENCE Prefixes of empty SEQUENCE
parse_prefixes:"30007e":2:0 parse_prefixes:"3000":0:0
Prefixes of SEQUENCE of BOOLEAN, INTEGER, INTEGER Prefixes of SEQUENCE of BOOLEAN, INTEGER, INTEGER
parse_prefixes:"300b01010102012a02031234567e":13:0 parse_prefixes:"300b01010102012a0203123456":0:0
Prefixes of SEQUENCE of (SEQUENCE of INTEGER, INTEGER), INTEGER Prefixes of SEQUENCE of (SEQUENCE of INTEGER, INTEGER), INTEGER
parse_prefixes:"300b30060201410201420201617e":13:0 parse_prefixes:"300b3006020141020142020161":0:0
length=0 (short form) length=0 (short form)
get_len:"00":0 get_len:"00":0

View File

@ -9,8 +9,13 @@
#include "mbedtls/asn1write.h" #include "mbedtls/asn1write.h"
#endif #endif
/* Used internally to report an error that indicates a bug in a parsing function. */
#define ERR_PARSE_INCONSISTENCY INT_MAX #define ERR_PARSE_INCONSISTENCY INT_MAX
/* Use this magic value in some tests to indicate that the expected result
* should not be checked. */
#define UNPREDICTABLE_RESULT 0x5552
static int nested_parse( unsigned char **const p, static int nested_parse( unsigned char **const p,
const unsigned char *const end ) const unsigned char *const end )
{ {
@ -226,16 +231,26 @@ exit:
/* BEGIN_CASE */ /* BEGIN_CASE */
void parse_prefixes( const data_t *input, void parse_prefixes( const data_t *input,
int actual_length_arg, int full_result,
int last_result ) int overfull_result )
{ {
size_t actual_length = actual_length_arg; /* full_result: expected result from parsing the given string. */
/* overfull_result: expected_result from parsing the given string plus
* some trailing garbage. This may be UNPREDICTABLE_RESULT to accept
* any result: use this for invalid inputs that may or may not become
* valid depending on what the trailing garbage is. */
unsigned char *buf = NULL; unsigned char *buf = NULL;
unsigned char *p = NULL; unsigned char *p = NULL;
size_t buffer_size; size_t buffer_size;
int ret; int ret;
for( buffer_size = 1; buffer_size <= input->len; buffer_size++ ) /* Test every prefix of the input, except the empty string.
* The first byte of the string is the tag. Without a tag byte,
* we wouldn't know what to parse the input as.
* Also test the input followed by an extra byte.
*/
for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
{ {
test_set_step( buffer_size ); test_set_step( buffer_size );
/* Allocate a new buffer of exactly the length to parse each time. /* Allocate a new buffer of exactly the length to parse each time.
@ -244,18 +259,25 @@ void parse_prefixes( const data_t *input,
memcpy( buf, input->x, buffer_size ); memcpy( buf, input->x, buffer_size );
p = buf; p = buf;
ret = nested_parse( &p, buf + buffer_size ); ret = nested_parse( &p, buf + buffer_size );
if( ret == ERR_PARSE_INCONSISTENCY ) if( ret == ERR_PARSE_INCONSISTENCY )
goto exit; goto exit;
if( actual_length > 0 && buffer_size >= actual_length ) if( buffer_size < input->len )
{
TEST_EQUAL( ret, last_result );
if( ret == 0 )
TEST_ASSERT( p == buf + actual_length );
}
else
{ {
TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA ); TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
} }
else if( buffer_size == input->len )
{
TEST_EQUAL( ret, full_result );
}
else /* ( buffer_size > input->len ) */
{
if( overfull_result != UNPREDICTABLE_RESULT )
TEST_EQUAL( ret, overfull_result );
}
if( ret == 0 )
TEST_ASSERT( p == buf + input->len );
mbedtls_free( buf ); mbedtls_free( buf );
buf = NULL; buf = NULL;
} }
@ -271,6 +293,12 @@ void get_len( const data_t *input, int actual_length_arg )
size_t actual_length = actual_length_arg; size_t actual_length = actual_length_arg;
size_t buffer_size; size_t buffer_size;
/* Test prefixes of a buffer containing the given length string
* followed by `actual_length` bytes of payload. To save a bit of
* time, we skip some "boring" prefixes: we don't test prefixes where
* the payload is truncated more than one byte away from either end,
* and we only test the empty string on a 1-byte input.
*/
for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ ) for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
{ {
if( ! get_len_step( input, buffer_size, actual_length ) ) if( ! get_len_step( input, buffer_size, actual_length ) )