Add buffer length tests for mbedtls_asn1_write_len()

This commit is contained in:
Paul Bakker 2016-07-14 11:02:31 +01:00 committed by Simon Butcher
parent e325db9055
commit 58bfb83bb0
2 changed files with 26 additions and 16 deletions

View File

@ -50,22 +50,31 @@ ASN.1 Write IA5 String #5 (Buffer too small for string)
mbedtls_asn1_write_ia5_string:"ABC":"":2:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL mbedtls_asn1_write_ia5_string:"ABC":"":2:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
ASN.1 Write / Read Length #0 (Len = 0, short form) ASN.1 Write / Read Length #0 (Len = 0, short form)
mbedtls_asn1_write_len:0:"00":1 mbedtls_asn1_write_len:0:"00":1:1
ASN.1 Write / Read Length #1 (Len = 127, short form) ASN.1 Write / Read Length #1 (Len = 127, short form)
mbedtls_asn1_write_len:127:"7F":1 mbedtls_asn1_write_len:127:"7F":1:1
ASN.1 Write / Read Length #2 (Len = 128, long form) ASN.1 Write / Read Length #2 (Len = 127, buffer too small)
mbedtls_asn1_write_len:128:"8180":2 mbedtls_asn1_write_len:127:"7F":0:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
ASN.1 Write / Read Length #3 (Len = 255, long form) ASN.1 Write / Read Length #3 (Len = 128, long form)
mbedtls_asn1_write_len:255:"81FF":2 mbedtls_asn1_write_len:128:"8180":2:2
ASN.1 Write / Read Length #4 (Len = 256, long form) ASN.1 Write / Read Length #4 (Len = 255, long form)
mbedtls_asn1_write_len:256:"820100":3 mbedtls_asn1_write_len:255:"81FF":2:2
ASN.1 Write / Read Length #5 (Len = 65535, max supported length) ASN.1 Write / Read Length #5 (Len = 255, buffer too small)
mbedtls_asn1_write_len:65535:"82FFFF":3 mbedtls_asn1_write_len:255:"81FF":1:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
ASN.1 Write / Read Length #6 (Len = 65536, not supported) ASN.1 Write / Read Length #6 (Len = 256, long form)
mbedtls_asn1_write_len:65536:"":MBEDTLS_ERR_ASN1_INVALID_LENGTH mbedtls_asn1_write_len:256:"820100":3:3
ASN.1 Write / Read Length #7 (Len = 65535, max supported length)
mbedtls_asn1_write_len:65535:"82FFFF":3:3
ASN.1 Write / Read Length #8 (Len = 65535, buffer too small)
mbedtls_asn1_write_len:65535:"82FFFF":2:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
ASN.1 Write / Read Length #9 (Len = 65536, not supported)
mbedtls_asn1_write_len:65536:"":0:MBEDTLS_ERR_ASN1_INVALID_LENGTH

View File

@ -84,7 +84,8 @@ void mbedtls_asn1_write_ia5_string( char *str, char *hex_asn1,
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mbedtls_asn1_write_len( int len, char *check_str, int result ) void mbedtls_asn1_write_len( int len, char *check_str, int buf_len,
int result )
{ {
int ret; int ret;
unsigned char buf[150]; unsigned char buf[150];
@ -96,7 +97,7 @@ void mbedtls_asn1_write_len( int len, char *check_str, int result )
memset( asn1, 0, sizeof( asn1 ) ); memset( asn1, 0, sizeof( asn1 ) );
asn1_len = unhexify( asn1, check_str ); asn1_len = unhexify( asn1, check_str );
p = buf + GUARD_LEN + asn1_len; p = buf + GUARD_LEN + buf_len;
ret = mbedtls_asn1_write_len( &p, buf + GUARD_LEN, (size_t) len ); ret = mbedtls_asn1_write_len( &p, buf + GUARD_LEN, (size_t) len );
@ -106,13 +107,13 @@ void mbedtls_asn1_write_len( int len, char *check_str, int result )
for( i = 0; i < GUARD_LEN; i++ ) for( i = 0; i < GUARD_LEN; i++ )
{ {
TEST_ASSERT( buf[i] == GUARD_VAL ); TEST_ASSERT( buf[i] == GUARD_VAL );
TEST_ASSERT( buf[GUARD_LEN + asn1_len + i] == GUARD_VAL ); TEST_ASSERT( buf[GUARD_LEN + buf_len + i] == GUARD_VAL );
} }
if( result >= 0 ) if( result >= 0 )
{ {
TEST_ASSERT( (size_t) ret == asn1_len ); TEST_ASSERT( (size_t) ret == asn1_len );
TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + asn1_len ); TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + buf_len );
TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 ); TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 );
} }