Skip to content

Commit f317dc4

Browse files
authored
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
2 parents 4fde885 + 95c893d commit f317dc4

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

tests/suites/test_suite_asn1parse.data

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,59 @@
11
Empty length
2-
parse_prefixes:"04":0:MBEDTLS_ERR_ASN1_INVALID_LENGTH
2+
parse_prefixes:"04":MBEDTLS_ERR_ASN1_OUT_OF_DATA:UNPREDICTABLE_RESULT
3+
4+
Incomplete length
5+
parse_prefixes:"0481":MBEDTLS_ERR_ASN1_OUT_OF_DATA:UNPREDICTABLE_RESULT
36

47
Prefixes of OCTET STRING, length=0
5-
parse_prefixes:"04007e":2:0
8+
parse_prefixes:"0400":0:0
69

710
Prefixes of OCTET STRING, length=0 (0 length bytes)
8-
parse_prefixes:"04807e":2:MBEDTLS_ERR_ASN1_INVALID_LENGTH
11+
parse_prefixes:"0480":MBEDTLS_ERR_ASN1_INVALID_LENGTH:MBEDTLS_ERR_ASN1_INVALID_LENGTH
912

1013
Prefixes of OCTET STRING, length=1
11-
parse_prefixes:"0401417e":3:0
14+
parse_prefixes:"040141":0:0
1215

1316
Prefixes of OCTET STRING, length=2
14-
parse_prefixes:"040241427e":4:0
17+
parse_prefixes:"04024142":0:0
1518

1619
Prefixes of BOOLEAN, length=0
17-
parse_prefixes:"01007e":2:MBEDTLS_ERR_ASN1_INVALID_LENGTH
20+
parse_prefixes:"0100":MBEDTLS_ERR_ASN1_INVALID_LENGTH:MBEDTLS_ERR_ASN1_INVALID_LENGTH
1821

1922
Prefixes of BOOLEAN, length=1
20-
parse_prefixes:"0101007e":3:0
23+
parse_prefixes:"010100":0:0
2124

2225
Prefixes of BOOLEAN, length=2
23-
parse_prefixes:"010200007e":4:MBEDTLS_ERR_ASN1_INVALID_LENGTH
26+
parse_prefixes:"01020000":MBEDTLS_ERR_ASN1_INVALID_LENGTH:MBEDTLS_ERR_ASN1_INVALID_LENGTH
2427

2528
Prefixes of INTEGER, length=1
26-
parse_prefixes:"0201417e":3:0
29+
parse_prefixes:"020141":0:0
2730

2831
Prefixes of INTEGER, length=2
29-
parse_prefixes:"020241427e":4:0
32+
parse_prefixes:"02024142":0:0
3033

3134
Prefixes of INTEGER, length=5
32-
parse_prefixes:"020541424344457e":7:0
35+
parse_prefixes:"02054142434445":0:0
3336

3437
Prefixes of empty BIT STRING
35-
parse_prefixes:"03007e":2:MBEDTLS_ERR_ASN1_OUT_OF_DATA
38+
parse_prefixes:"0300":MBEDTLS_ERR_ASN1_OUT_OF_DATA:UNPREDICTABLE_RESULT
3639

3740
Prefixes of BIT STRING, unused_bits=0, payload_length=0
38-
parse_prefixes:"030100":3:0
41+
parse_prefixes:"030100":0:MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
3942

4043
Prefixes of BIT STRING, unused_bits=0, payload_length=1
41-
parse_prefixes:"0302002a":4:0
44+
parse_prefixes:"0302002a":0:MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
4245

4346
Prefixes of BIT STRING, unused_bits=1, payload_length=1
44-
parse_prefixes:"0302012a":4:0
47+
parse_prefixes:"0302012a":0:MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
4548

4649
Prefixes of empty SEQUENCE
47-
parse_prefixes:"30007e":2:0
50+
parse_prefixes:"3000":0:0
4851

4952
Prefixes of SEQUENCE of BOOLEAN, INTEGER, INTEGER
50-
parse_prefixes:"300b01010102012a02031234567e":13:0
53+
parse_prefixes:"300b01010102012a0203123456":0:0
5154

5255
Prefixes of SEQUENCE of (SEQUENCE of INTEGER, INTEGER), INTEGER
53-
parse_prefixes:"300b30060201410201420201617e":13:0
56+
parse_prefixes:"300b3006020141020142020161":0:0
5457

5558
length=0 (short form)
5659
get_len:"00":0

tests/suites/test_suite_asn1parse.function

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
#include "mbedtls/asn1write.h"
1010
#endif
1111

12+
/* Used internally to report an error that indicates a bug in a parsing function. */
1213
#define ERR_PARSE_INCONSISTENCY INT_MAX
1314

15+
/* Use this magic value in some tests to indicate that the expected result
16+
* should not be checked. */
17+
#define UNPREDICTABLE_RESULT 0x5552
18+
1419
static int nested_parse( unsigned char **const p,
1520
const unsigned char *const end )
1621
{
@@ -226,16 +231,26 @@ exit:
226231

227232
/* BEGIN_CASE */
228233
void parse_prefixes( const data_t *input,
229-
int actual_length_arg,
230-
int last_result )
234+
int full_result,
235+
int overfull_result )
231236
{
232-
size_t actual_length = actual_length_arg;
237+
/* full_result: expected result from parsing the given string. */
238+
/* overfull_result: expected_result from parsing the given string plus
239+
* some trailing garbage. This may be UNPREDICTABLE_RESULT to accept
240+
* any result: use this for invalid inputs that may or may not become
241+
* valid depending on what the trailing garbage is. */
242+
233243
unsigned char *buf = NULL;
234244
unsigned char *p = NULL;
235245
size_t buffer_size;
236246
int ret;
237247

238-
for( buffer_size = 1; buffer_size <= input->len; buffer_size++ )
248+
/* Test every prefix of the input, except the empty string.
249+
* The first byte of the string is the tag. Without a tag byte,
250+
* we wouldn't know what to parse the input as.
251+
* Also test the input followed by an extra byte.
252+
*/
253+
for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
239254
{
240255
test_set_step( buffer_size );
241256
/* Allocate a new buffer of exactly the length to parse each time.
@@ -244,18 +259,25 @@ void parse_prefixes( const data_t *input,
244259
memcpy( buf, input->x, buffer_size );
245260
p = buf;
246261
ret = nested_parse( &p, buf + buffer_size );
262+
247263
if( ret == ERR_PARSE_INCONSISTENCY )
248264
goto exit;
249-
if( actual_length > 0 && buffer_size >= actual_length )
265+
if( buffer_size < input->len )
250266
{
251-
TEST_EQUAL( ret, last_result );
252-
if( ret == 0 )
253-
TEST_ASSERT( p == buf + actual_length );
267+
TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
254268
}
255-
else
269+
else if( buffer_size == input->len )
256270
{
257-
TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
271+
TEST_EQUAL( ret, full_result );
258272
}
273+
else /* ( buffer_size > input->len ) */
274+
{
275+
if( overfull_result != UNPREDICTABLE_RESULT )
276+
TEST_EQUAL( ret, overfull_result );
277+
}
278+
if( ret == 0 )
279+
TEST_ASSERT( p == buf + input->len );
280+
259281
mbedtls_free( buf );
260282
buf = NULL;
261283
}
@@ -271,6 +293,12 @@ void get_len( const data_t *input, int actual_length_arg )
271293
size_t actual_length = actual_length_arg;
272294
size_t buffer_size;
273295

296+
/* Test prefixes of a buffer containing the given length string
297+
* followed by `actual_length` bytes of payload. To save a bit of
298+
* time, we skip some "boring" prefixes: we don't test prefixes where
299+
* the payload is truncated more than one byte away from either end,
300+
* and we only test the empty string on a 1-byte input.
301+
*/
274302
for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
275303
{
276304
if( ! get_len_step( input, buffer_size, actual_length ) )

0 commit comments

Comments
 (0)