Add basic test for "pre-verify" callback

This commit is contained in:
Nicholas Wilson 2016-06-21 16:17:25 +01:00 committed by Simon Butcher
parent ca89d7f6d8
commit 016d9fd96f
2 changed files with 73 additions and 1 deletions

View File

@ -57,3 +57,6 @@ ssl_dtls_replay:"abcd12340000,abcd12340100":"abcd123400ff":0
SSL SET_HOSTNAME memory leak: call ssl_set_hostname twice
ssl_set_hostname_twice:"server0":"server1"
SSL preverify callback: basic
ssl_preverifycb:"data_files/server1.crt"

View File

@ -1,6 +1,35 @@
/* BEGIN_HEADER */
#include <mbedtls/ssl.h>
#include <mbedtls/ssl_internal.h>
#if defined(MBEDTLS_X509_CRT_PARSE_C)
static int pre_vrfy_data = 0;
static void pre_vrfy_fn( void *data, struct mbedtls_x509_crt *crt )
{
if( crt && crt->version == 3 )
*(int*)data = 1;
}
static unsigned char pre_vrfy_buffer[2048];
static int pre_vrfy_buffer_start = 0, pre_vrfy_buffer_end = 0;
static int pre_vrfy_send( void *ctx, const unsigned char *buf, size_t len )
{
(void)ctx;
if ( len > sizeof( pre_vrfy_buffer ) - pre_vrfy_buffer_end )
len = sizeof( pre_vrfy_buffer ) - pre_vrfy_buffer_end;
memcpy( pre_vrfy_buffer + pre_vrfy_buffer_end, buf, len );
pre_vrfy_buffer_end += (int)len;
return( len ? (int)len : MBEDTLS_ERR_SSL_WANT_WRITE );
}
static int pre_vrfy_recv( void *ctx, unsigned char *buf, size_t len )
{
(void)ctx;
if ( len > (size_t)(pre_vrfy_buffer_end - pre_vrfy_buffer_start) )
len = pre_vrfy_buffer_end - pre_vrfy_buffer_start;
memcpy( buf, pre_vrfy_buffer + pre_vrfy_buffer_start, len );
pre_vrfy_buffer_start += (int)len;
return( len ? (int)len : MBEDTLS_ERR_SSL_WANT_READ );
}
#endif
/* END_HEADER */
/* BEGIN_DEPENDENCIES
@ -52,4 +81,44 @@ void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
mbedtls_ssl_free( &ssl );
}
/* END_CASE */
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SSL_PREVERIFY_CB:MBEDTLS_FS_IO:MBEDTLS_KEY_EXCHANGE_RSA_ENABLED:MBEDTLS_AES_C:MBEDTLS_SHA256_C:MBEDTLS_CIPHER_MODE_CBC */
void ssl_preverifycb( char *crt_file )
{
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
mbedtls_x509_crt crt;
mbedtls_ssl_init( &ssl );
mbedtls_ssl_config_init( &conf );
mbedtls_x509_crt_init( &crt );
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
MBEDTLS_SSL_IS_SERVER,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
mbedtls_ssl_conf_ca_chain( &conf, &crt, NULL );
/* Write out a certificate record to a buffer */
ssl.transform_negotiate->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256 );
mbedtls_ssl_set_bio( &ssl, NULL, pre_vrfy_send, pre_vrfy_recv, NULL );
TEST_ASSERT( mbedtls_ssl_conf_own_cert( &conf, &crt, NULL ) == 0 );
TEST_ASSERT( mbedtls_ssl_write_certificate( &ssl ) == 0 );
/* Read in the certificate record, and check it calls the pre-verify callback */
conf.endpoint = MBEDTLS_SSL_IS_CLIENT;
mbedtls_ssl_conf_pre_verify( &conf, pre_vrfy_fn, (void*)&pre_vrfy_data );
TEST_ASSERT( mbedtls_ssl_parse_certificate( &ssl ) == 0 );
TEST_ASSERT( pre_vrfy_data != 0 );
mbedtls_ssl_free( &ssl );
mbedtls_ssl_config_free( &conf );
mbedtls_x509_crt_free( &crt );
}
/* END_CASE */