Split ssl_init() -> ssl_setup()

This commit is contained in:
Manuel Pégourié-Gonnard 2015-04-29 00:48:22 +02:00
parent ec160c0f53
commit 41d479e7df
15 changed files with 57 additions and 41 deletions

View File

@ -15,6 +15,7 @@ API Changes
* Headers are now found in the 'mbedtls' directory (previously 'polarssl'). * Headers are now found in the 'mbedtls' directory (previously 'polarssl').
* The following _init() functions that could return errors have * The following _init() functions that could return errors have
been split into an _init() that returns void and another function: been split into an _init() that returns void and another function:
mbedtls_ssl_init() -> mbedtls_ssl_setup()
mbedtls_ccm_init() -> mbedtls_ccm_setkey() mbedtls_ccm_init() -> mbedtls_ccm_setkey()
mbedtls_gcm_init() -> mbedtls_gcm_setkey() mbedtls_gcm_init() -> mbedtls_gcm_setkey()
mbedtls_hmac_drbg_init() -> mbedtls_hmac_drbg_init(_buf)() mbedtls_hmac_drbg_init() -> mbedtls_hmac_drbg_init(_buf)()

View File

@ -1098,14 +1098,22 @@ int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name );
/** /**
* \brief Initialize an SSL context * \brief Initialize an SSL context
* (An individual SSL context is not thread-safe) * Just makes the context ready for mbetls_ssl_setup() or
* mbedtls_ssl_free()
*
* \param ssl SSL context
*/
void mbedtls_ssl_init( mbedtls_ssl_context *ssl );
/**
* \brief Set up an SSL context for use
* *
* \param ssl SSL context * \param ssl SSL context
* *
* \return 0 if successful, or MBEDTLS_ERR_SSL_MALLOC_FAILED if * \return 0 if successful, or MBEDTLS_ERR_SSL_MALLOC_FAILED if
* memory allocation failed * memory allocation failed
*/ */
int mbedtls_ssl_init( mbedtls_ssl_context *ssl ); int mbedtls_ssl_setup( mbedtls_ssl_context *ssl );
/** /**
* \brief Reset an already initialized SSL context for re-use * \brief Reset an already initialized SSL context for re-use

View File

@ -4944,13 +4944,19 @@ static int ssl_cookie_check_dummy( void *ctx,
/* /*
* Initialize an SSL context * Initialize an SSL context
*/ */
int mbedtls_ssl_init( mbedtls_ssl_context *ssl ) void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
{
memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
}
/*
* Setup an SSL context
*/
int mbedtls_ssl_setup( mbedtls_ssl_context *ssl )
{ {
int ret; int ret;
int len = MBEDTLS_SSL_BUFFER_LEN; int len = MBEDTLS_SSL_BUFFER_LEN;
memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
/* /*
* Sane defaults * Sane defaults
*/ */

View File

@ -104,7 +104,7 @@ int main( int argc, char *argv[] )
/* /*
* 0. Initialize the RNG and the session data * 0. Initialize the RNG and the session data
*/ */
memset( &ssl, 0, sizeof( mbedtls_ssl_context ) ); mbedtls_ssl_init( &ssl );
mbedtls_x509_crt_init( &cacert ); mbedtls_x509_crt_init( &cacert );
mbedtls_ctr_drbg_init( &ctr_drbg ); mbedtls_ctr_drbg_init( &ctr_drbg );
@ -160,9 +160,9 @@ int main( int argc, char *argv[] )
mbedtls_printf( " . Setting up the DTLS structure..." ); mbedtls_printf( " . Setting up the DTLS structure..." );
fflush( stdout ); fflush( stdout );
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
mbedtls_printf( " failed\n ! mbedtls_ssl_init returned %d\n\n", ret ); mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
goto exit; goto exit;
} }

View File

@ -103,7 +103,7 @@ int main( void )
mbedtls_ssl_cache_context cache; mbedtls_ssl_cache_context cache;
#endif #endif
memset( &ssl, 0, sizeof(mbedtls_ssl_context) ); mbedtls_ssl_init( &ssl );
mbedtls_ssl_cookie_init( &cookie_ctx ); mbedtls_ssl_cookie_init( &cookie_ctx );
#if defined(MBEDTLS_SSL_CACHE_C) #if defined(MBEDTLS_SSL_CACHE_C)
mbedtls_ssl_cache_init( &cache ); mbedtls_ssl_cache_init( &cache );
@ -190,9 +190,9 @@ int main( void )
printf( " . Setting up the DTLS data..." ); printf( " . Setting up the DTLS data..." );
fflush( stdout ); fflush( stdout );
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
printf( " failed\n ! mbedtls_ssl_init returned %d\n\n", ret ); printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
goto exit; goto exit;
} }

View File

@ -147,7 +147,7 @@ enum exit_codes
{ {
exit_ok = 0, exit_ok = 0,
ctr_drbg_seed_failed, ctr_drbg_seed_failed,
ssl_init_failed, ssl_setup_failed,
socket_failed, socket_failed,
connect_failed, connect_failed,
x509_crt_parse_failed, x509_crt_parse_failed,
@ -172,7 +172,7 @@ int main( void )
/* /*
* 0. Initialize and setup stuff * 0. Initialize and setup stuff
*/ */
memset( &ssl, 0, sizeof( mbedtls_ssl_context ) ); mbedtls_ssl_init( &ssl );
#if defined(MBEDTLS_X509_CRT_PARSE_C) #if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_x509_crt_init( &ca ); mbedtls_x509_crt_init( &ca );
#endif #endif
@ -181,13 +181,13 @@ int main( void )
if( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, if( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers, strlen( pers ) ) != 0 ) (const unsigned char *) pers, strlen( pers ) ) != 0 )
{ {
ret = ssl_init_failed; ret = ctr_drbg_seed_failed;
goto exit; goto exit;
} }
if( mbedtls_ssl_init( &ssl ) != 0 ) if( mbedtls_ssl_setup( &ssl ) != 0 )
{ {
ret = ssl_init_failed; ret = ssl_setup_failed;
goto exit; goto exit;
} }

View File

@ -92,7 +92,7 @@ int main( void )
/* /*
* 0. Initialize the RNG and the session data * 0. Initialize the RNG and the session data
*/ */
memset( &ssl, 0, sizeof( mbedtls_ssl_context ) ); mbedtls_ssl_init( &ssl );
mbedtls_x509_crt_init( &cacert ); mbedtls_x509_crt_init( &cacert );
mbedtls_ctr_drbg_init( &ctr_drbg ); mbedtls_ctr_drbg_init( &ctr_drbg );
@ -148,9 +148,9 @@ int main( void )
mbedtls_printf( " . Setting up the SSL/TLS structure..." ); mbedtls_printf( " . Setting up the SSL/TLS structure..." );
fflush( stdout ); fflush( stdout );
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
mbedtls_printf( " failed\n ! mbedtls_ssl_init returned %d\n\n", ret ); mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
goto exit; goto exit;
} }
@ -278,8 +278,6 @@ exit:
mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy ); mbedtls_entropy_free( &entropy );
memset( &ssl, 0, sizeof( ssl ) );
#if defined(_WIN32) #if defined(_WIN32)
mbedtls_printf( " + Press Enter to exit this program.\n" ); mbedtls_printf( " + Press Enter to exit this program.\n" );
fflush( stdout ); getchar(); fflush( stdout ); getchar();

View File

@ -414,7 +414,7 @@ int main( int argc, char *argv[] )
* Make sure memory references are valid. * Make sure memory references are valid.
*/ */
server_fd = 0; server_fd = 0;
memset( &ssl, 0, sizeof( mbedtls_ssl_context ) ); mbedtls_ssl_init( &ssl );
memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) ); memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
mbedtls_ctr_drbg_init( &ctr_drbg ); mbedtls_ctr_drbg_init( &ctr_drbg );
#if defined(MBEDTLS_X509_CRT_PARSE_C) #if defined(MBEDTLS_X509_CRT_PARSE_C)
@ -1047,9 +1047,9 @@ int main( int argc, char *argv[] )
mbedtls_printf( " . Setting up the SSL/TLS structure..." ); mbedtls_printf( " . Setting up the SSL/TLS structure..." );
fflush( stdout ); fflush( stdout );
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
mbedtls_printf( " failed\n ! mbedtls_ssl_init returned -0x%x\n\n", -ret ); mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
goto exit; goto exit;
} }

View File

@ -248,9 +248,9 @@ int main( void )
goto exit; goto exit;
} }
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
mbedtls_printf( " failed\n ! mbedtls_ssl_init returned %d\n\n", ret ); mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
goto exit; goto exit;
} }

View File

@ -367,7 +367,7 @@ int main( int argc, char *argv[] )
* Make sure memory references are valid in case we exit early. * Make sure memory references are valid in case we exit early.
*/ */
server_fd = 0; server_fd = 0;
memset( &ssl, 0, sizeof( mbedtls_ssl_context ) ); mbedtls_ssl_init( &ssl );
memset( &buf, 0, sizeof( buf ) ); memset( &buf, 0, sizeof( buf ) );
mbedtls_x509_crt_init( &cacert ); mbedtls_x509_crt_init( &cacert );
mbedtls_x509_crt_init( &clicert ); mbedtls_x509_crt_init( &clicert );
@ -582,9 +582,9 @@ int main( int argc, char *argv[] )
mbedtls_printf( " . Setting up the SSL/TLS structure..." ); mbedtls_printf( " . Setting up the SSL/TLS structure..." );
fflush( stdout ); fflush( stdout );
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
mbedtls_printf( " failed\n ! mbedtls_ssl_init returned %d\n\n", ret ); mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
goto exit; goto exit;
} }

View File

@ -133,7 +133,7 @@ static void *handle_ssl_connection( void *data )
mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ctr_drbg_context ctr_drbg;
/* Make sure memory references are valid */ /* Make sure memory references are valid */
memset( &ssl, 0, sizeof( mbedtls_ssl_context ) ); mbedtls_ssl_init( &ssl );
mbedtls_ctr_drbg_init( &ctr_drbg ); mbedtls_ctr_drbg_init( &ctr_drbg );
mbedtls_snprintf( pers, sizeof(pers), "SSL Pthread Thread %d", thread_id ); mbedtls_snprintf( pers, sizeof(pers), "SSL Pthread Thread %d", thread_id );
@ -158,9 +158,9 @@ static void *handle_ssl_connection( void *data )
*/ */
mbedtls_printf( " [ #%d ] Setting up the SSL data....\n", thread_id ); mbedtls_printf( " [ #%d ] Setting up the SSL data....\n", thread_id );
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
mbedtls_printf( " [ #%d ] failed: mbedtls_ssl_init returned -0x%04x\n", mbedtls_printf( " [ #%d ] failed: mbedtls_ssl_setup returned -0x%04x\n",
thread_id, -ret ); thread_id, -ret );
goto thread_exit; goto thread_exit;
} }

View File

@ -103,7 +103,7 @@ int main( void )
mbedtls_ssl_cache_context cache; mbedtls_ssl_cache_context cache;
#endif #endif
memset( &ssl, 0, sizeof(mbedtls_ssl_context) ); mbedtls_ssl_init( &ssl );
#if defined(MBEDTLS_SSL_CACHE_C) #if defined(MBEDTLS_SSL_CACHE_C)
mbedtls_ssl_cache_init( &cache ); mbedtls_ssl_cache_init( &cache );
#endif #endif
@ -189,9 +189,9 @@ int main( void )
mbedtls_printf( " . Setting up the SSL data...." ); mbedtls_printf( " . Setting up the SSL data...." );
fflush( stdout ); fflush( stdout );
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
mbedtls_printf( " failed\n ! mbedtls_ssl_init returned %d\n\n", ret ); mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
goto exit; goto exit;
} }

View File

@ -761,7 +761,7 @@ int main( int argc, char *argv[] )
* Make sure memory references are valid in case we exit early. * Make sure memory references are valid in case we exit early.
*/ */
listen_fd = 0; listen_fd = 0;
memset( &ssl, 0, sizeof( mbedtls_ssl_context ) ); mbedtls_ssl_init( &ssl );
mbedtls_ctr_drbg_init( &ctr_drbg ); mbedtls_ctr_drbg_init( &ctr_drbg );
#if defined(MBEDTLS_X509_CRT_PARSE_C) #if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_x509_crt_init( &cacert ); mbedtls_x509_crt_init( &cacert );
@ -1518,9 +1518,9 @@ int main( int argc, char *argv[] )
mbedtls_printf( " . Setting up the SSL/TLS structure..." ); mbedtls_printf( " . Setting up the SSL/TLS structure..." );
fflush( stdout ); fflush( stdout );
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
mbedtls_printf( " failed\n ! mbedtls_ssl_init returned -0x%x\n\n", -ret ); mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
goto exit; goto exit;
} }

View File

@ -160,6 +160,7 @@ int main( int argc, char *argv[] )
*/ */
server_fd = 0; server_fd = 0;
mbedtls_ctr_drbg_init( &ctr_drbg ); mbedtls_ctr_drbg_init( &ctr_drbg );
mbedtls_ssl_init( &ssl );
mbedtls_x509_crt_init( &cacert ); mbedtls_x509_crt_init( &cacert );
mbedtls_x509_crt_init( &clicert ); mbedtls_x509_crt_init( &clicert );
#if defined(MBEDTLS_X509_CRL_PARSE_C) #if defined(MBEDTLS_X509_CRL_PARSE_C)
@ -393,9 +394,9 @@ int main( int argc, char *argv[] )
/* /*
* 3. Setup stuff * 3. Setup stuff
*/ */
if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
{ {
mbedtls_printf( " failed\n ! mbedtls_ssl_init returned %d\n\n", ret ); mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
goto exit; goto exit;
} }

View File

@ -13,7 +13,9 @@ void ssl_dtls_replay( char *prevs, char *new, int ret )
mbedtls_ssl_context ssl; mbedtls_ssl_context ssl;
char *end_prevs = prevs + strlen( prevs ) + 1; char *end_prevs = prevs + strlen( prevs ) + 1;
TEST_ASSERT( mbedtls_ssl_init( &ssl ) == 0 ); mbedtls_ssl_init( &ssl );
TEST_ASSERT( mbedtls_ssl_setup( &ssl ) == 0 );
TEST_ASSERT( mbedtls_ssl_set_transport( &ssl, MBEDTLS_SSL_TRANSPORT_DATAGRAM ) == 0 ); TEST_ASSERT( mbedtls_ssl_set_transport( &ssl, MBEDTLS_SSL_TRANSPORT_DATAGRAM ) == 0 );
/* Read previous record numbers */ /* Read previous record numbers */