Skip to content

Commit 95c893d

Browse files
More systematic handling of trailing garbage in parse_prefixes
Before, the string to parse may contain trailing garbage (there was never more than one byte), and there was a separate argument indicating the length of the content. Now, the string to parse is the exact content, and the test code runs an extra test step with a trailing byte added.
1 parent ef41838 commit 95c893d

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-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: 28 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
{
@@ -176,10 +181,15 @@ exit:
176181

177182
/* BEGIN_CASE */
178183
void parse_prefixes( const data_t *input,
179-
int actual_length_arg,
180-
int last_result )
184+
int full_result,
185+
int overfull_result )
181186
{
182-
size_t actual_length = actual_length_arg;
187+
/* full_result: expected result from parsing the given string. */
188+
/* overfull_result: expected_result from parsing the given string plus
189+
* some trailing garbage. This may be UNPREDICTABLE_RESULT to accept
190+
* any result: use this for invalid inputs that may or may not become
191+
* valid depending on what the trailing garbage is. */
192+
183193
unsigned char *buf = NULL;
184194
unsigned char *p = NULL;
185195
size_t buffer_size;
@@ -188,8 +198,9 @@ void parse_prefixes( const data_t *input,
188198
/* Test every prefix of the input, except the empty string.
189199
* The first byte of the string is the tag. Without a tag byte,
190200
* we wouldn't know what to parse the input as.
201+
* Also test the input followed by an extra byte.
191202
*/
192-
for( buffer_size = 1; buffer_size <= input->len; buffer_size++ )
203+
for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
193204
{
194205
test_set_step( buffer_size );
195206
/* Allocate a new buffer of exactly the length to parse each time.
@@ -198,18 +209,25 @@ void parse_prefixes( const data_t *input,
198209
memcpy( buf, input->x, buffer_size );
199210
p = buf;
200211
ret = nested_parse( &p, buf + buffer_size );
212+
201213
if( ret == ERR_PARSE_INCONSISTENCY )
202214
goto exit;
203-
if( actual_length > 0 && buffer_size >= actual_length )
215+
if( buffer_size < input->len )
204216
{
205-
TEST_EQUAL( ret, last_result );
206-
if( ret == 0 )
207-
TEST_ASSERT( p == buf + actual_length );
217+
TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
208218
}
209-
else
219+
else if( buffer_size == input->len )
210220
{
211-
TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
221+
TEST_EQUAL( ret, full_result );
212222
}
223+
else /* ( buffer_size > input->len ) */
224+
{
225+
if( overfull_result != UNPREDICTABLE_RESULT )
226+
TEST_EQUAL( ret, overfull_result );
227+
}
228+
if( ret == 0 )
229+
TEST_ASSERT( p == buf + input->len );
230+
213231
mbedtls_free( buf );
214232
buf = NULL;
215233
}

0 commit comments

Comments
 (0)