From 58bfb83bb0c639b56164b3cac56dd263c26499a5 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Thu, 14 Jul 2016 11:02:31 +0100 Subject: [PATCH] Add buffer length tests for mbedtls_asn1_write_len() --- tests/suites/test_suite_asn1write.data | 33 ++++++++++++++-------- tests/suites/test_suite_asn1write.function | 9 +++--- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/tests/suites/test_suite_asn1write.data b/tests/suites/test_suite_asn1write.data index 0c4074ed3..3518ed945 100644 --- a/tests/suites/test_suite_asn1write.data +++ b/tests/suites/test_suite_asn1write.data @@ -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 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) -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) -mbedtls_asn1_write_len:128:"8180":2 +ASN.1 Write / Read Length #2 (Len = 127, buffer too small) +mbedtls_asn1_write_len:127:"7F":0:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL -ASN.1 Write / Read Length #3 (Len = 255, long form) -mbedtls_asn1_write_len:255:"81FF":2 +ASN.1 Write / Read Length #3 (Len = 128, long form) +mbedtls_asn1_write_len:128:"8180":2:2 -ASN.1 Write / Read Length #4 (Len = 256, long form) -mbedtls_asn1_write_len:256:"820100":3 +ASN.1 Write / Read Length #4 (Len = 255, long form) +mbedtls_asn1_write_len:255:"81FF":2:2 -ASN.1 Write / Read Length #5 (Len = 65535, max supported length) -mbedtls_asn1_write_len:65535:"82FFFF":3 +ASN.1 Write / Read Length #5 (Len = 255, buffer too small) +mbedtls_asn1_write_len:255:"81FF":1:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL -ASN.1 Write / Read Length #6 (Len = 65536, not supported) -mbedtls_asn1_write_len:65536:"":MBEDTLS_ERR_ASN1_INVALID_LENGTH +ASN.1 Write / Read Length #6 (Len = 256, long form) +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 diff --git a/tests/suites/test_suite_asn1write.function b/tests/suites/test_suite_asn1write.function index 160190f65..ba4db88d6 100644 --- a/tests/suites/test_suite_asn1write.function +++ b/tests/suites/test_suite_asn1write.function @@ -84,7 +84,8 @@ void mbedtls_asn1_write_ia5_string( char *str, char *hex_asn1, /* END_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; 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 ) ); 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 ); @@ -106,13 +107,13 @@ void mbedtls_asn1_write_len( int len, char *check_str, int result ) for( i = 0; i < GUARD_LEN; i++ ) { 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 ) { 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 ); }