Add mbedtls_x509_get_name memory leak unit test

Introduce a unit test to test mbedtls_x509_get_name() and add a testcase
with a corrupt DER-encoded name that causes mbedtls_x509_get_name() to
have to cleanup things it is allocated. If it fails to do this, a memory
leak is detected under Asan builds.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
This commit is contained in:
David Horstmann 2022-10-05 13:08:45 +01:00
parent 854be05949
commit 77ecc6e4e9
2 changed files with 42 additions and 0 deletions

View File

@ -427,6 +427,12 @@ X509 Get Modified DN #5 Name exactly 255 bytes, ending with comma requiring esca
depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_SHA1_C
mbedtls_x509_dn_gets_subject_replace:"data_files/server1.crt":"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234,":"":MBEDTLS_ERR_X509_BUFFER_TOO_SMALL
X509 Get Name Valid DN
mbedtls_x509_get_name:"310B3009060355040613024E4C3111300F060355040A0C08506F6C617253534C3119301706035504030C10506F6C617253534C2054657374204341":0
X509 Get Name Corrupted DN Mem Leak
mbedtls_x509_get_name:"310B3009060355040613024E4C3111300F060355040A0C08506F6C617253534C3019301706035504030C10506F6C617253534C2054657374204341":MBEDTLS_ERR_X509_INVALID_NAME + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
X509 Time Expired #1
depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_HAVE_TIME_DATE:MBEDTLS_SHA1_C
mbedtls_x509_time_is_past:"data_files/server1.crt":"valid_from":1

View File

@ -799,6 +799,42 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
void mbedtls_x509_get_name( char * hex_name, int exp_ret )
{
unsigned char *name;
unsigned char *p;
size_t name_len;
mbedtls_x509_name head;
mbedtls_x509_name *allocated, *prev;
int res;
name = mbedtls_test_unhexify_alloc( hex_name, &name_len );
p = name;
res = mbedtls_x509_get_name( &p, ( name + name_len ), &head );
if( res == 0 )
{
allocated = head.next;
head.next = NULL;
prev = NULL;
while( allocated != NULL )
{
prev = allocated;
allocated = allocated->next;
mbedtls_free( prev );
}
}
TEST_ASSERT( res == exp_ret );
mbedtls_free( name );
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
{