1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-08-08 17:42:09 +03:00

Use ASN1 UTC tags for dates before 2000

Signed-off-by: Werner Lewis <werner.lewis@arm.com>
This commit is contained in:
Werner Lewis
2022-05-10 12:23:13 +01:00
parent a0026604a2
commit 1b54a05f77
4 changed files with 81 additions and 10 deletions

View File

@@ -208,8 +208,10 @@ void x509_crt_check( char *subject_key_file, char *subject_pwd,
mbedtls_x509write_cert crt;
unsigned char buf[4096];
unsigned char check_buf[5000];
unsigned char *p, *end;
unsigned char tag, sz;
mbedtls_mpi serial;
int ret;
int ret, before_tag, after_tag;
size_t olen = 0, pem_len = 0, buf_index = 0;
int der_len = -1;
FILE *f;
@@ -287,14 +289,16 @@ void x509_crt_check( char *subject_key_file, char *subject_pwd,
TEST_ASSERT( buf[buf_index] == 0 );
}
f = fopen( cert_check_file, "r" );
TEST_ASSERT( f != NULL );
olen = fread( check_buf, 1, sizeof( check_buf ), f );
fclose( f );
TEST_ASSERT( olen < sizeof( check_buf ) );
TEST_ASSERT( olen >= pem_len - 1 );
TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
if( *cert_check_file != '\0' )
{
f = fopen( cert_check_file, "r" );
TEST_ASSERT( f != NULL );
olen = fread( check_buf, 1, sizeof( check_buf ), f );
fclose( f );
TEST_ASSERT( olen < sizeof( check_buf ) );
TEST_ASSERT( olen >= pem_len - 1 );
TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
}
der_len = mbedtls_x509write_crt_der( &crt, buf, sizeof( buf ),
mbedtls_test_rnd_pseudo_rand,
@@ -304,6 +308,54 @@ void x509_crt_check( char *subject_key_file, char *subject_pwd,
if( der_len == 0 )
goto exit;
// Not testing against file, check date format
if( *cert_check_file == '\0' )
{
// UTC tag if before 2050, 2 digits less for year
if( not_before[0] == '2' && ( not_before[1] > '0' || not_before[2] > '4' ) )
{
before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
}
else
{
before_tag = MBEDTLS_ASN1_UTC_TIME;
not_before += 2;
}
if( not_after[0] == '2' && ( not_after[1] > '0' || not_after[2] > '4' ) )
{
after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
}
else
{
after_tag = MBEDTLS_ASN1_UTC_TIME;
not_after += 2;
}
end = buf + sizeof( buf );
for( p = end - der_len ; p < end ; )
{
tag = *p++;
sz = *p++;
if( tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME )
{
// Check correct tag and time written
TEST_ASSERT( before_tag == tag );
TEST_ASSERT( memcmp( p, not_before, sz - 1 ) == 0 );
p += sz;
tag = *p++;
sz = *p++;
TEST_ASSERT( after_tag == tag );
TEST_ASSERT( memcmp( p, not_after, sz - 1 ) == 0 );
break;
}
// Increment if long form ASN1 length
if( sz & 0x80 )
p += sz & 0x0F;
if( tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
p += sz;
}
TEST_ASSERT( p < end );
}
ret = mbedtls_x509write_crt_der( &crt, buf, (size_t)( der_len - 1 ),
mbedtls_test_rnd_pseudo_rand, &rnd_info );
TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );