diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index e39f79ca4..c9d9a2013 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -521,398 +521,6 @@ struct options const char *mki; /* The dtls mki value to use */ } opt; -#if defined(MBEDTLS_SSL_EXPORT_KEYS) -int eap_tls_key_derivation( void *p_expkey, - const unsigned char *ms, - const unsigned char *kb, - size_t maclen, - size_t keylen, - size_t ivlen, - const unsigned char client_random[32], - const unsigned char server_random[32], - mbedtls_tls_prf_types tls_prf_type ) -{ - eap_tls_keys *keys = (eap_tls_keys *)p_expkey; - - ( ( void ) kb ); - memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) ); - memcpy( keys->randbytes, client_random, 32 ); - memcpy( keys->randbytes + 32, server_random, 32 ); - keys->tls_prf_type = tls_prf_type; - - if( opt.debug_level > 2 ) - { - mbedtls_printf("exported maclen is %u\n", (unsigned)maclen); - mbedtls_printf("exported keylen is %u\n", (unsigned)keylen); - mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen); - } - return( 0 ); -} - -int nss_keylog_export( void *p_expkey, - const unsigned char *ms, - const unsigned char *kb, - size_t maclen, - size_t keylen, - size_t ivlen, - const unsigned char client_random[32], - const unsigned char server_random[32], - mbedtls_tls_prf_types tls_prf_type ) -{ - char nss_keylog_line[ 200 ]; - size_t const client_random_len = 32; - size_t const master_secret_len = 48; - size_t len = 0; - size_t j; - int ret = 0; - - ((void) p_expkey); - ((void) kb); - ((void) maclen); - ((void) keylen); - ((void) ivlen); - ((void) server_random); - ((void) tls_prf_type); - - len += sprintf( nss_keylog_line + len, - "%s", "CLIENT_RANDOM " ); - - for( j = 0; j < client_random_len; j++ ) - { - len += sprintf( nss_keylog_line + len, - "%02x", client_random[j] ); - } - - len += sprintf( nss_keylog_line + len, " " ); - - for( j = 0; j < master_secret_len; j++ ) - { - len += sprintf( nss_keylog_line + len, - "%02x", ms[j] ); - } - - len += sprintf( nss_keylog_line + len, "\n" ); - nss_keylog_line[ len ] = '\0'; - - mbedtls_printf( "\n" ); - mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" ); - mbedtls_printf( "%s", nss_keylog_line ); - mbedtls_printf( "---------------------------------------------\n" ); - - if( opt.nss_keylog_file != NULL ) - { - FILE *f; - - if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL ) - { - ret = -1; - goto exit; - } - - if( fwrite( nss_keylog_line, 1, len, f ) != len ) - { - ret = -1; - fclose( f ); - goto exit; - } - - fclose( f ); - } - -exit: - mbedtls_platform_zeroize( nss_keylog_line, - sizeof( nss_keylog_line ) ); - return( ret ); -} - -#if defined( MBEDTLS_SSL_DTLS_SRTP ) -int dtls_srtp_key_derivation( void *p_expkey, - const unsigned char *ms, - const unsigned char *kb, - size_t maclen, - size_t keylen, - size_t ivlen, - const unsigned char client_random[32], - const unsigned char server_random[32], - mbedtls_tls_prf_types tls_prf_type ) -{ - dtls_srtp_keys *keys = (dtls_srtp_keys *)p_expkey; - - ( ( void ) kb ); - memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) ); - memcpy( keys->randbytes, client_random, 32 ); - memcpy( keys->randbytes + 32, server_random, 32 ); - keys->tls_prf_type = tls_prf_type; - - if( opt.debug_level > 2 ) - { - mbedtls_printf( "exported maclen is %u\n", (unsigned) maclen ); - mbedtls_printf( "exported keylen is %u\n", (unsigned) keylen ); - mbedtls_printf( "exported ivlen is %u\n", (unsigned) ivlen ); - } - return( 0 ); -} -#endif /* MBEDTLS_SSL_DTLS_SRTP */ - -#endif /* MBEDTLS_SSL_EXPORT_KEYS */ - -void my_debug( void *ctx, int level, - const char *file, int line, - const char *str ) -{ - const char *p, *basename; - - /* Extract basename from file */ - for( p = basename = file; *p != '\0'; p++ ) - if( *p == '/' || *p == '\\' ) - basename = p + 1; - - mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", - basename, line, level, str ); - fflush( (FILE *) ctx ); -} - -mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ) -{ - (void) time; - return 0x5af2a056; -} - -int dummy_entropy( void *data, unsigned char *output, size_t len ) -{ - size_t i; - int ret; - (void) data; - - ret = mbedtls_entropy_func( data, output, len ); - for( i = 0; i < len; i++ ) - { - //replace result with pseudo random - output[i] = (unsigned char) rand(); - } - return( ret ); -} - -#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) -int ca_callback( void *data, mbedtls_x509_crt const *child, - mbedtls_x509_crt **candidates ) -{ - int ret = 0; - mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data; - mbedtls_x509_crt *first; - - /* This is a test-only implementation of the CA callback - * which always returns the entire list of trusted certificates. - * Production implementations managing a large number of CAs - * should use an efficient presentation and lookup for the - * set of trusted certificates (such as a hashtable) and only - * return those trusted certificates which satisfy basic - * parental checks, such as the matching of child `Issuer` - * and parent `Subject` field or matching key identifiers. */ - ((void) child); - - first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); - if( first == NULL ) - { - ret = -1; - goto exit; - } - mbedtls_x509_crt_init( first ); - - if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) - { - ret = -1; - goto exit; - } - - while( ca->next != NULL ) - { - ca = ca->next; - if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) - { - ret = -1; - goto exit; - } - } - -exit: - - if( ret != 0 ) - { - mbedtls_x509_crt_free( first ); - mbedtls_free( first ); - first = NULL; - } - - *candidates = first; - return( ret ); -} -#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ - -/* - * Test recv/send functions that make sure each try returns - * WANT_READ/WANT_WRITE at least once before sucesseding - */ -int delayed_recv( void *ctx, unsigned char *buf, size_t len ) -{ - static int first_try = 1; - int ret; - - if( first_try ) - { - first_try = 0; - return( MBEDTLS_ERR_SSL_WANT_READ ); - } - - ret = mbedtls_net_recv( ctx, buf, len ); - if( ret != MBEDTLS_ERR_SSL_WANT_READ ) - first_try = 1; /* Next call will be a new operation */ - return( ret ); -} - -int delayed_send( void *ctx, const unsigned char *buf, size_t len ) -{ - static int first_try = 1; - int ret; - - if( first_try ) - { - first_try = 0; - return( MBEDTLS_ERR_SSL_WANT_WRITE ); - } - - ret = mbedtls_net_send( ctx, buf, len ); - if( ret != MBEDTLS_ERR_SSL_WANT_WRITE ) - first_try = 1; /* Next call will be a new operation */ - return( ret ); -} - -#if defined(MBEDTLS_SSL_RECORD_CHECKING) -int ssl_check_record( mbedtls_ssl_context const *ssl, - unsigned char const *buf, size_t len ) -{ - int ret; - unsigned char *tmp_buf; - - /* Record checking may modify the input buffer, - * so make a copy. */ - tmp_buf = mbedtls_calloc( 1, len ); - if( tmp_buf == NULL ) - return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); - memcpy( tmp_buf, buf, len ); - - ret = mbedtls_ssl_check_record( ssl, tmp_buf, len ); - if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ) - { - int ret_repeated; - - /* Test-only: Make sure that mbedtls_ssl_check_record() - * doesn't alter state. */ - memcpy( tmp_buf, buf, len ); /* Restore buffer */ - ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len ); - if( ret != ret_repeated ) - { - mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" ); - return( -1 ); - } - - switch( ret ) - { - case 0: - break; - - case MBEDTLS_ERR_SSL_INVALID_RECORD: - if( opt.debug_level > 1 ) - mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" ); - break; - - case MBEDTLS_ERR_SSL_INVALID_MAC: - if( opt.debug_level > 1 ) - mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" ); - break; - - case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD: - if( opt.debug_level > 1 ) - mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" ); - break; - - default: - mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret ); - return( -1 ); - } - - /* Regardless of the outcome, forward the record to the stack. */ - } - - mbedtls_free( tmp_buf ); - - return( 0 ); -} -#endif /* MBEDTLS_SSL_RECORD_CHECKING */ - -int recv_cb( void *ctx, unsigned char *buf, size_t len ) -{ - io_ctx_t *io_ctx = (io_ctx_t*) ctx; - size_t recv_len; - int ret; - - if( opt.nbio == 2 ) - ret = delayed_recv( io_ctx->net, buf, len ); - else - ret = mbedtls_net_recv( io_ctx->net, buf, len ); - if( ret < 0 ) - return( ret ); - recv_len = (size_t) ret; - - if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) - { - /* Here's the place to do any datagram/record checking - * in between receiving the packet from the underlying - * transport and passing it on to the TLS stack. */ -#if defined(MBEDTLS_SSL_RECORD_CHECKING) - if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) - return( -1 ); -#endif /* MBEDTLS_SSL_RECORD_CHECKING */ - } - - return( (int) recv_len ); -} - -int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len, - uint32_t timeout ) -{ - io_ctx_t *io_ctx = (io_ctx_t*) ctx; - int ret; - size_t recv_len; - - ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout ); - if( ret < 0 ) - return( ret ); - recv_len = (size_t) ret; - - if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) - { - /* Here's the place to do any datagram/record checking - * in between receiving the packet from the underlying - * transport and passing it on to the TLS stack. */ -#if defined(MBEDTLS_SSL_RECORD_CHECKING) - if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) - return( -1 ); -#endif /* MBEDTLS_SSL_RECORD_CHECKING */ - } - - return( (int) recv_len ); -} - -int send_cb( void *ctx, unsigned char const *buf, size_t len ) -{ - io_ctx_t *io_ctx = (io_ctx_t*) ctx; - - if( opt.nbio == 2 ) - return( delayed_send( io_ctx->net, buf, len ) ); - - return( mbedtls_net_send( io_ctx->net, buf, len ) ); -} #include "ssl_test_common_source.c" #if defined(MBEDTLS_X509_CRT_PARSE_C) @@ -949,74 +557,6 @@ static int my_verify( void *data, mbedtls_x509_crt *crt, } #endif /* MBEDTLS_X509_CRT_PARSE_C */ -#if defined(MBEDTLS_X509_CRT_PARSE_C) -int ssl_sig_hashes_for_test[] = { -#if defined(MBEDTLS_SHA512_C) - MBEDTLS_MD_SHA512, - MBEDTLS_MD_SHA384, -#endif -#if defined(MBEDTLS_SHA256_C) - MBEDTLS_MD_SHA256, - MBEDTLS_MD_SHA224, -#endif -#if defined(MBEDTLS_SHA1_C) - /* Allow SHA-1 as we use it extensively in tests. */ - MBEDTLS_MD_SHA1, -#endif - MBEDTLS_MD_NONE -}; -#endif /* MBEDTLS_X509_CRT_PARSE_C */ - -/* - * Wait for an event from the underlying transport or the timer - * (Used in event-driven IO mode). - */ -#if !defined(MBEDTLS_TIMING_C) -int idle( mbedtls_net_context *fd, - int idle_reason ) -#else -int idle( mbedtls_net_context *fd, - mbedtls_timing_delay_context *timer, - int idle_reason ) -#endif -{ - int ret; - int poll_type = 0; - - if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE ) - poll_type = MBEDTLS_NET_POLL_WRITE; - else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ ) - poll_type = MBEDTLS_NET_POLL_READ; -#if !defined(MBEDTLS_TIMING_C) - else - return( 0 ); -#endif - - while( 1 ) - { - /* Check if timer has expired */ -#if defined(MBEDTLS_TIMING_C) - if( timer != NULL && - mbedtls_timing_get_delay( timer ) == 2 ) - { - break; - } -#endif /* MBEDTLS_TIMING_C */ - - /* Check if underlying transport became available */ - if( poll_type != 0 ) - { - ret = mbedtls_net_poll( fd, poll_type, 0 ); - if( ret < 0 ) - return( ret ); - if( ret == poll_type ) - break; - } - } - - return( 0 ); -} - #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) int report_cid_usage( mbedtls_ssl_context *ssl, const char *additional_description ) diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 12c5c5068..e65851fd1 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -624,398 +624,6 @@ struct options int support_mki; /* The dtls mki mki support */ } opt; -#if defined(MBEDTLS_SSL_EXPORT_KEYS) -int eap_tls_key_derivation( void *p_expkey, - const unsigned char *ms, - const unsigned char *kb, - size_t maclen, - size_t keylen, - size_t ivlen, - const unsigned char client_random[32], - const unsigned char server_random[32], - mbedtls_tls_prf_types tls_prf_type ) -{ - eap_tls_keys *keys = (eap_tls_keys *)p_expkey; - - ( ( void ) kb ); - memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) ); - memcpy( keys->randbytes, client_random, 32 ); - memcpy( keys->randbytes + 32, server_random, 32 ); - keys->tls_prf_type = tls_prf_type; - - if( opt.debug_level > 2 ) - { - mbedtls_printf("exported maclen is %u\n", (unsigned)maclen); - mbedtls_printf("exported keylen is %u\n", (unsigned)keylen); - mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen); - } - return( 0 ); -} - -int nss_keylog_export( void *p_expkey, - const unsigned char *ms, - const unsigned char *kb, - size_t maclen, - size_t keylen, - size_t ivlen, - const unsigned char client_random[32], - const unsigned char server_random[32], - mbedtls_tls_prf_types tls_prf_type ) -{ - char nss_keylog_line[ 200 ]; - size_t const client_random_len = 32; - size_t const master_secret_len = 48; - size_t len = 0; - size_t j; - int ret = 0; - - ((void) p_expkey); - ((void) kb); - ((void) maclen); - ((void) keylen); - ((void) ivlen); - ((void) server_random); - ((void) tls_prf_type); - - len += sprintf( nss_keylog_line + len, - "%s", "CLIENT_RANDOM " ); - - for( j = 0; j < client_random_len; j++ ) - { - len += sprintf( nss_keylog_line + len, - "%02x", client_random[j] ); - } - - len += sprintf( nss_keylog_line + len, " " ); - - for( j = 0; j < master_secret_len; j++ ) - { - len += sprintf( nss_keylog_line + len, - "%02x", ms[j] ); - } - - len += sprintf( nss_keylog_line + len, "\n" ); - nss_keylog_line[ len ] = '\0'; - - mbedtls_printf( "\n" ); - mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" ); - mbedtls_printf( "%s", nss_keylog_line ); - mbedtls_printf( "---------------------------------------------\n" ); - - if( opt.nss_keylog_file != NULL ) - { - FILE *f; - - if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL ) - { - ret = -1; - goto exit; - } - - if( fwrite( nss_keylog_line, 1, len, f ) != len ) - { - ret = -1; - fclose( f ); - goto exit; - } - - fclose( f ); - } - -exit: - mbedtls_platform_zeroize( nss_keylog_line, - sizeof( nss_keylog_line ) ); - return( ret ); -} - -#if defined( MBEDTLS_SSL_DTLS_SRTP ) -int dtls_srtp_key_derivation( void *p_expkey, - const unsigned char *ms, - const unsigned char *kb, - size_t maclen, - size_t keylen, - size_t ivlen, - const unsigned char client_random[32], - const unsigned char server_random[32], - mbedtls_tls_prf_types tls_prf_type ) -{ - dtls_srtp_keys *keys = (dtls_srtp_keys *)p_expkey; - - ( ( void ) kb ); - memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) ); - memcpy( keys->randbytes, client_random, 32 ); - memcpy( keys->randbytes + 32, server_random, 32 ); - keys->tls_prf_type = tls_prf_type; - - if( opt.debug_level > 2 ) - { - mbedtls_printf( "exported maclen is %u\n", (unsigned) maclen ); - mbedtls_printf( "exported keylen is %u\n", (unsigned) keylen ); - mbedtls_printf( "exported ivlen is %u\n", (unsigned) ivlen ); - } - return( 0 ); -} -#endif /* MBEDTLS_SSL_DTLS_SRTP */ - -#endif /* MBEDTLS_SSL_EXPORT_KEYS */ - -void my_debug( void *ctx, int level, - const char *file, int line, - const char *str ) -{ - const char *p, *basename; - - /* Extract basename from file */ - for( p = basename = file; *p != '\0'; p++ ) - if( *p == '/' || *p == '\\' ) - basename = p + 1; - - mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", - basename, line, level, str ); - fflush( (FILE *) ctx ); -} - -mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ) -{ - (void) time; - return 0x5af2a056; -} - -int dummy_entropy( void *data, unsigned char *output, size_t len ) -{ - size_t i; - int ret; - (void) data; - - ret = mbedtls_entropy_func( data, output, len ); - for( i = 0; i < len; i++ ) - { - //replace result with pseudo random - output[i] = (unsigned char) rand(); - } - return( ret ); -} - -#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) -int ca_callback( void *data, mbedtls_x509_crt const *child, - mbedtls_x509_crt **candidates ) -{ - int ret = 0; - mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data; - mbedtls_x509_crt *first; - - /* This is a test-only implementation of the CA callback - * which always returns the entire list of trusted certificates. - * Production implementations managing a large number of CAs - * should use an efficient presentation and lookup for the - * set of trusted certificates (such as a hashtable) and only - * return those trusted certificates which satisfy basic - * parental checks, such as the matching of child `Issuer` - * and parent `Subject` field or matching key identifiers. */ - ((void) child); - - first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); - if( first == NULL ) - { - ret = -1; - goto exit; - } - mbedtls_x509_crt_init( first ); - - if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) - { - ret = -1; - goto exit; - } - - while( ca->next != NULL ) - { - ca = ca->next; - if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) - { - ret = -1; - goto exit; - } - } - -exit: - - if( ret != 0 ) - { - mbedtls_x509_crt_free( first ); - mbedtls_free( first ); - first = NULL; - } - - *candidates = first; - return( ret ); -} -#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ - -/* - * Test recv/send functions that make sure each try returns - * WANT_READ/WANT_WRITE at least once before sucesseding - */ -int delayed_recv( void *ctx, unsigned char *buf, size_t len ) -{ - static int first_try = 1; - int ret; - - if( first_try ) - { - first_try = 0; - return( MBEDTLS_ERR_SSL_WANT_READ ); - } - - ret = mbedtls_net_recv( ctx, buf, len ); - if( ret != MBEDTLS_ERR_SSL_WANT_READ ) - first_try = 1; /* Next call will be a new operation */ - return( ret ); -} - -int delayed_send( void *ctx, const unsigned char *buf, size_t len ) -{ - static int first_try = 1; - int ret; - - if( first_try ) - { - first_try = 0; - return( MBEDTLS_ERR_SSL_WANT_WRITE ); - } - - ret = mbedtls_net_send( ctx, buf, len ); - if( ret != MBEDTLS_ERR_SSL_WANT_WRITE ) - first_try = 1; /* Next call will be a new operation */ - return( ret ); -} - -#if defined(MBEDTLS_SSL_RECORD_CHECKING) -int ssl_check_record( mbedtls_ssl_context const *ssl, - unsigned char const *buf, size_t len ) -{ - int ret; - unsigned char *tmp_buf; - - /* Record checking may modify the input buffer, - * so make a copy. */ - tmp_buf = mbedtls_calloc( 1, len ); - if( tmp_buf == NULL ) - return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); - memcpy( tmp_buf, buf, len ); - - ret = mbedtls_ssl_check_record( ssl, tmp_buf, len ); - if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ) - { - int ret_repeated; - - /* Test-only: Make sure that mbedtls_ssl_check_record() - * doesn't alter state. */ - memcpy( tmp_buf, buf, len ); /* Restore buffer */ - ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len ); - if( ret != ret_repeated ) - { - mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" ); - return( -1 ); - } - - switch( ret ) - { - case 0: - break; - - case MBEDTLS_ERR_SSL_INVALID_RECORD: - if( opt.debug_level > 1 ) - mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" ); - break; - - case MBEDTLS_ERR_SSL_INVALID_MAC: - if( opt.debug_level > 1 ) - mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" ); - break; - - case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD: - if( opt.debug_level > 1 ) - mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" ); - break; - - default: - mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret ); - return( -1 ); - } - - /* Regardless of the outcome, forward the record to the stack. */ - } - - mbedtls_free( tmp_buf ); - - return( 0 ); -} -#endif /* MBEDTLS_SSL_RECORD_CHECKING */ - -int recv_cb( void *ctx, unsigned char *buf, size_t len ) -{ - io_ctx_t *io_ctx = (io_ctx_t*) ctx; - size_t recv_len; - int ret; - - if( opt.nbio == 2 ) - ret = delayed_recv( io_ctx->net, buf, len ); - else - ret = mbedtls_net_recv( io_ctx->net, buf, len ); - if( ret < 0 ) - return( ret ); - recv_len = (size_t) ret; - - if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) - { - /* Here's the place to do any datagram/record checking - * in between receiving the packet from the underlying - * transport and passing it on to the TLS stack. */ -#if defined(MBEDTLS_SSL_RECORD_CHECKING) - if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) - return( -1 ); -#endif /* MBEDTLS_SSL_RECORD_CHECKING */ - } - - return( (int) recv_len ); -} - -int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len, - uint32_t timeout ) -{ - io_ctx_t *io_ctx = (io_ctx_t*) ctx; - int ret; - size_t recv_len; - - ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout ); - if( ret < 0 ) - return( ret ); - recv_len = (size_t) ret; - - if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) - { - /* Here's the place to do any datagram/record checking - * in between receiving the packet from the underlying - * transport and passing it on to the TLS stack. */ -#if defined(MBEDTLS_SSL_RECORD_CHECKING) - if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) - return( -1 ); -#endif /* MBEDTLS_SSL_RECORD_CHECKING */ - } - - return( (int) recv_len ); -} - -int send_cb( void *ctx, unsigned char const *buf, size_t len ) -{ - io_ctx_t *io_ctx = (io_ctx_t*) ctx; - - if( opt.nbio == 2 ) - return( delayed_send( io_ctx->net, buf, len ) ); - - return( mbedtls_net_send( io_ctx->net, buf, len ) ); -} #include "ssl_test_common_source.c" /* @@ -1335,24 +943,6 @@ void term_handler( int sig ) } #endif -#if defined(MBEDTLS_X509_CRT_PARSE_C) -int ssl_sig_hashes_for_test[] = { -#if defined(MBEDTLS_SHA512_C) - MBEDTLS_MD_SHA512, - MBEDTLS_MD_SHA384, -#endif -#if defined(MBEDTLS_SHA256_C) - MBEDTLS_MD_SHA256, - MBEDTLS_MD_SHA224, -#endif -#if defined(MBEDTLS_SHA1_C) - /* Allow SHA-1 as we use it extensively in tests. */ - MBEDTLS_MD_SHA1, -#endif - MBEDTLS_MD_NONE -}; -#endif /* MBEDTLS_X509_CRT_PARSE_C */ - /** Return true if \p ret is a status code indicating that there is an * operation in progress on an SSL connection, and false if it indicates * success or a fatal error. @@ -1591,56 +1181,6 @@ static void ssl_async_cancel( mbedtls_ssl_context *ssl ) } #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ -/* - * Wait for an event from the underlying transport or the timer - * (Used in event-driven IO mode). - */ -#if !defined(MBEDTLS_TIMING_C) -int idle( mbedtls_net_context *fd, - int idle_reason ) -#else -int idle( mbedtls_net_context *fd, - mbedtls_timing_delay_context *timer, - int idle_reason ) -#endif -{ - int ret; - int poll_type = 0; - - if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE ) - poll_type = MBEDTLS_NET_POLL_WRITE; - else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ ) - poll_type = MBEDTLS_NET_POLL_READ; -#if !defined(MBEDTLS_TIMING_C) - else - return( 0 ); -#endif - - while( 1 ) - { - /* Check if timer has expired */ -#if defined(MBEDTLS_TIMING_C) - if( timer != NULL && - mbedtls_timing_get_delay( timer ) == 2 ) - { - break; - } -#endif /* MBEDTLS_TIMING_C */ - - /* Check if underlying transport became available */ - if( poll_type != 0 ) - { - ret = mbedtls_net_poll( fd, poll_type, 0 ); - if( ret < 0 ) - return( ret ); - if( ret == poll_type ) - break; - } - } - - return( 0 ); -} - #if defined(MBEDTLS_USE_PSA_CRYPTO) static psa_status_t psa_setup_psk_key_slot( psa_key_id_t *slot, psa_algorithm_t alg, diff --git a/programs/ssl/ssl_test_common_source.c b/programs/ssl/ssl_test_common_source.c index 78b9331a4..d9e36078d 100644 --- a/programs/ssl/ssl_test_common_source.c +++ b/programs/ssl/ssl_test_common_source.c @@ -23,3 +23,283 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#if defined(MBEDTLS_SSL_EXPORT_KEYS) +int eap_tls_key_derivation( void *p_expkey, + const unsigned char *ms, + const unsigned char *kb, + size_t maclen, + size_t keylen, + size_t ivlen, + const unsigned char client_random[32], + const unsigned char server_random[32], + mbedtls_tls_prf_types tls_prf_type ) +{ + eap_tls_keys *keys = (eap_tls_keys *)p_expkey; + + ( ( void ) kb ); + memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) ); + memcpy( keys->randbytes, client_random, 32 ); + memcpy( keys->randbytes + 32, server_random, 32 ); + keys->tls_prf_type = tls_prf_type; + + if( opt.debug_level > 2 ) + { + mbedtls_printf("exported maclen is %u\n", (unsigned)maclen); + mbedtls_printf("exported keylen is %u\n", (unsigned)keylen); + mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen); + } + return( 0 ); +} + +int nss_keylog_export( void *p_expkey, + const unsigned char *ms, + const unsigned char *kb, + size_t maclen, + size_t keylen, + size_t ivlen, + const unsigned char client_random[32], + const unsigned char server_random[32], + mbedtls_tls_prf_types tls_prf_type ) +{ + char nss_keylog_line[ 200 ]; + size_t const client_random_len = 32; + size_t const master_secret_len = 48; + size_t len = 0; + size_t j; + int ret = 0; + + ((void) p_expkey); + ((void) kb); + ((void) maclen); + ((void) keylen); + ((void) ivlen); + ((void) server_random); + ((void) tls_prf_type); + + len += sprintf( nss_keylog_line + len, + "%s", "CLIENT_RANDOM " ); + + for( j = 0; j < client_random_len; j++ ) + { + len += sprintf( nss_keylog_line + len, + "%02x", client_random[j] ); + } + + len += sprintf( nss_keylog_line + len, " " ); + + for( j = 0; j < master_secret_len; j++ ) + { + len += sprintf( nss_keylog_line + len, + "%02x", ms[j] ); + } + + len += sprintf( nss_keylog_line + len, "\n" ); + nss_keylog_line[ len ] = '\0'; + + mbedtls_printf( "\n" ); + mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" ); + mbedtls_printf( "%s", nss_keylog_line ); + mbedtls_printf( "---------------------------------------------\n" ); + + if( opt.nss_keylog_file != NULL ) + { + FILE *f; + + if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL ) + { + ret = -1; + goto exit; + } + + if( fwrite( nss_keylog_line, 1, len, f ) != len ) + { + ret = -1; + fclose( f ); + goto exit; + } + + fclose( f ); + } + +exit: + mbedtls_platform_zeroize( nss_keylog_line, + sizeof( nss_keylog_line ) ); + return( ret ); +} + +#if defined( MBEDTLS_SSL_DTLS_SRTP ) +int dtls_srtp_key_derivation( void *p_expkey, + const unsigned char *ms, + const unsigned char *kb, + size_t maclen, + size_t keylen, + size_t ivlen, + const unsigned char client_random[32], + const unsigned char server_random[32], + mbedtls_tls_prf_types tls_prf_type ) +{ + dtls_srtp_keys *keys = (dtls_srtp_keys *)p_expkey; + + ( ( void ) kb ); + memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) ); + memcpy( keys->randbytes, client_random, 32 ); + memcpy( keys->randbytes + 32, server_random, 32 ); + keys->tls_prf_type = tls_prf_type; + + if( opt.debug_level > 2 ) + { + mbedtls_printf( "exported maclen is %u\n", (unsigned) maclen ); + mbedtls_printf( "exported keylen is %u\n", (unsigned) keylen ); + mbedtls_printf( "exported ivlen is %u\n", (unsigned) ivlen ); + } + return( 0 ); +} +#endif /* MBEDTLS_SSL_DTLS_SRTP */ + +#endif /* MBEDTLS_SSL_EXPORT_KEYS */ + +#if defined(MBEDTLS_SSL_RECORD_CHECKING) +int ssl_check_record( mbedtls_ssl_context const *ssl, + unsigned char const *buf, size_t len ) +{ + int ret; + unsigned char *tmp_buf; + + /* Record checking may modify the input buffer, + * so make a copy. */ + tmp_buf = mbedtls_calloc( 1, len ); + if( tmp_buf == NULL ) + return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); + memcpy( tmp_buf, buf, len ); + + ret = mbedtls_ssl_check_record( ssl, tmp_buf, len ); + if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ) + { + int ret_repeated; + + /* Test-only: Make sure that mbedtls_ssl_check_record() + * doesn't alter state. */ + memcpy( tmp_buf, buf, len ); /* Restore buffer */ + ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len ); + if( ret != ret_repeated ) + { + mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" ); + return( -1 ); + } + + switch( ret ) + { + case 0: + break; + + case MBEDTLS_ERR_SSL_INVALID_RECORD: + if( opt.debug_level > 1 ) + mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" ); + break; + + case MBEDTLS_ERR_SSL_INVALID_MAC: + if( opt.debug_level > 1 ) + mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" ); + break; + + case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD: + if( opt.debug_level > 1 ) + mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" ); + break; + + default: + mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret ); + return( -1 ); + } + + /* Regardless of the outcome, forward the record to the stack. */ + } + + mbedtls_free( tmp_buf ); + + return( 0 ); +} +#endif /* MBEDTLS_SSL_RECORD_CHECKING */ + +int recv_cb( void *ctx, unsigned char *buf, size_t len ) +{ + io_ctx_t *io_ctx = (io_ctx_t*) ctx; + size_t recv_len; + int ret; + + if( opt.nbio == 2 ) + ret = delayed_recv( io_ctx->net, buf, len ); + else + ret = mbedtls_net_recv( io_ctx->net, buf, len ); + if( ret < 0 ) + return( ret ); + recv_len = (size_t) ret; + + if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) + { + /* Here's the place to do any datagram/record checking + * in between receiving the packet from the underlying + * transport and passing it on to the TLS stack. */ +#if defined(MBEDTLS_SSL_RECORD_CHECKING) + if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) + return( -1 ); +#endif /* MBEDTLS_SSL_RECORD_CHECKING */ + } + + return( (int) recv_len ); +} + +int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len, + uint32_t timeout ) +{ + io_ctx_t *io_ctx = (io_ctx_t*) ctx; + int ret; + size_t recv_len; + + ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout ); + if( ret < 0 ) + return( ret ); + recv_len = (size_t) ret; + + if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) + { + /* Here's the place to do any datagram/record checking + * in between receiving the packet from the underlying + * transport and passing it on to the TLS stack. */ +#if defined(MBEDTLS_SSL_RECORD_CHECKING) + if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) + return( -1 ); +#endif /* MBEDTLS_SSL_RECORD_CHECKING */ + } + + return( (int) recv_len ); +} + +int send_cb( void *ctx, unsigned char const *buf, size_t len ) +{ + io_ctx_t *io_ctx = (io_ctx_t*) ctx; + + if( opt.nbio == 2 ) + return( delayed_send( io_ctx->net, buf, len ) ); + + return( mbedtls_net_send( io_ctx->net, buf, len ) ); +} + +#if defined(MBEDTLS_X509_CRT_PARSE_C) +int ssl_sig_hashes_for_test[] = { +#if defined(MBEDTLS_SHA512_C) + MBEDTLS_MD_SHA512, + MBEDTLS_MD_SHA384, +#endif +#if defined(MBEDTLS_SHA256_C) + MBEDTLS_MD_SHA256, + MBEDTLS_MD_SHA224, +#endif +#if defined(MBEDTLS_SHA1_C) + /* Allow SHA-1 as we use it extensively in tests. */ + MBEDTLS_MD_SHA1, +#endif + MBEDTLS_MD_NONE +}; +#endif /* MBEDTLS_X509_CRT_PARSE_C */ diff --git a/programs/ssl/ssl_test_lib.c b/programs/ssl/ssl_test_lib.c index 40a6aa92f..22453c19f 100644 --- a/programs/ssl/ssl_test_lib.c +++ b/programs/ssl/ssl_test_lib.c @@ -24,4 +24,177 @@ #if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) +void my_debug( void *ctx, int level, + const char *file, int line, + const char *str ) +{ + const char *p, *basename; + + /* Extract basename from file */ + for( p = basename = file; *p != '\0'; p++ ) + if( *p == '/' || *p == '\\' ) + basename = p + 1; + + mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", + basename, line, level, str ); + fflush( (FILE *) ctx ); +} + +mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ) +{ + (void) time; + return 0x5af2a056; +} + +int dummy_entropy( void *data, unsigned char *output, size_t len ) +{ + size_t i; + int ret; + (void) data; + + ret = mbedtls_entropy_func( data, output, len ); + for( i = 0; i < len; i++ ) + { + //replace result with pseudo random + output[i] = (unsigned char) rand(); + } + return( ret ); +} + +#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) +int ca_callback( void *data, mbedtls_x509_crt const *child, + mbedtls_x509_crt **candidates ) +{ + int ret = 0; + mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data; + mbedtls_x509_crt *first; + + /* This is a test-only implementation of the CA callback + * which always returns the entire list of trusted certificates. + * Production implementations managing a large number of CAs + * should use an efficient presentation and lookup for the + * set of trusted certificates (such as a hashtable) and only + * return those trusted certificates which satisfy basic + * parental checks, such as the matching of child `Issuer` + * and parent `Subject` field or matching key identifiers. */ + ((void) child); + + first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); + if( first == NULL ) + { + ret = -1; + goto exit; + } + mbedtls_x509_crt_init( first ); + + if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) + { + ret = -1; + goto exit; + } + + while( ca->next != NULL ) + { + ca = ca->next; + if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) + { + ret = -1; + goto exit; + } + } + +exit: + + if( ret != 0 ) + { + mbedtls_x509_crt_free( first ); + mbedtls_free( first ); + first = NULL; + } + + *candidates = first; + return( ret ); +} +#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ + +int delayed_recv( void *ctx, unsigned char *buf, size_t len ) +{ + static int first_try = 1; + int ret; + + if( first_try ) + { + first_try = 0; + return( MBEDTLS_ERR_SSL_WANT_READ ); + } + + ret = mbedtls_net_recv( ctx, buf, len ); + if( ret != MBEDTLS_ERR_SSL_WANT_READ ) + first_try = 1; /* Next call will be a new operation */ + return( ret ); +} + +int delayed_send( void *ctx, const unsigned char *buf, size_t len ) +{ + static int first_try = 1; + int ret; + + if( first_try ) + { + first_try = 0; + return( MBEDTLS_ERR_SSL_WANT_WRITE ); + } + + ret = mbedtls_net_send( ctx, buf, len ); + if( ret != MBEDTLS_ERR_SSL_WANT_WRITE ) + first_try = 1; /* Next call will be a new operation */ + return( ret ); +} + +#if !defined(MBEDTLS_TIMING_C) +int idle( mbedtls_net_context *fd, + int idle_reason ) +#else +int idle( mbedtls_net_context *fd, + mbedtls_timing_delay_context *timer, + int idle_reason ) +#endif +{ + int ret; + int poll_type = 0; + + if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE ) + poll_type = MBEDTLS_NET_POLL_WRITE; + else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ ) + poll_type = MBEDTLS_NET_POLL_READ; +#if !defined(MBEDTLS_TIMING_C) + else + return( 0 ); +#endif + + while( 1 ) + { + /* Check if timer has expired */ +#if defined(MBEDTLS_TIMING_C) + if( timer != NULL && + mbedtls_timing_get_delay( timer ) == 2 ) + { + break; + } +#endif /* MBEDTLS_TIMING_C */ + + /* Check if underlying transport became available */ + if( poll_type != 0 ) + { + ret = mbedtls_net_poll( fd, poll_type, 0 ); + if( ret < 0 ) + return( ret ); + if( ret == poll_type ) + break; + } + } + + return( 0 ); +} + #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */ diff --git a/programs/ssl/ssl_test_lib.h b/programs/ssl/ssl_test_lib.h index 88dd3ec7c..666e694c9 100644 --- a/programs/ssl/ssl_test_lib.h +++ b/programs/ssl/ssl_test_lib.h @@ -133,6 +133,35 @@ typedef struct mbedtls_net_context *net; } io_ctx_t; -#endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */ +void my_debug( void *ctx, int level, + const char *file, int line, + const char *str ); +mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ); + +int dummy_entropy( void *data, unsigned char *output, size_t len ); + +#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) +int ca_callback( void *data, mbedtls_x509_crt const *child, + mbedtls_x509_crt **candidates ); +#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ + +/* + * Test recv/send functions that make sure each try returns + * WANT_READ/WANT_WRITE at least once before sucesseding + */ +int delayed_recv( void *ctx, unsigned char *buf, size_t len ); +int delayed_send( void *ctx, const unsigned char *buf, size_t len ); + +/* + * Wait for an event from the underlying transport or the timer + * (Used in event-driven IO mode). + */ +int idle( mbedtls_net_context *fd, +#if defined(MBEDTLS_TIMING_C) + mbedtls_timing_delay_context *timer, +#endif + int idle_reason ); + +#endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */ #endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */