From e3c41ad8a4ecb6ecaefc66e1fc75b2a1687a6715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 13 May 2015 10:04:32 +0200 Subject: [PATCH] Use the new timer callback API in programs --- library/ssl_tls.c | 4 --- programs/ssl/dtls_client.c | 16 ++++++------ programs/ssl/dtls_server.c | 14 ++++++++--- programs/ssl/ssl_client2.c | 51 ++++++++++++++++---------------------- programs/ssl/ssl_server2.c | 50 +++++++++++++++++++------------------ 5 files changed, 65 insertions(+), 70 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index b14c13ed7..820f5a59e 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4975,10 +4975,6 @@ static int ssl_cookie_check_dummy( void *ctx, void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) { memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); - - /* Temporary WIP */ - mbedtls_ssl_set_timer_cb( ssl, &ssl->WIP_timer, - mbedtls_timing_set_delay, mbedtls_timing_get_delay ); } /* diff --git a/programs/ssl/dtls_client.c b/programs/ssl/dtls_client.c index 0b837ba60..e753db227 100644 --- a/programs/ssl/dtls_client.c +++ b/programs/ssl/dtls_client.c @@ -35,18 +35,14 @@ #endif #if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \ - !defined(MBEDTLS_NET_C) || \ + !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \ !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \ !defined(MBEDTLS_CERTS_C) - -int main( int argc, char *argv[] ) +int main( void ) { - ((void) argc); - ((void) argv); - mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or " - "MBEDTLS_NET_C and/or " + "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or " "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or " "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" ); @@ -95,6 +91,7 @@ int main( int argc, char *argv[] ) mbedtls_ssl_context ssl; mbedtls_ssl_config conf; mbedtls_x509_crt cacert; + mbedtls_timing_delay_context timer; ((void) argc); ((void) argv); @@ -194,6 +191,9 @@ int main( int argc, char *argv[] ) mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout ); + mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, + mbedtls_timing_get_delay ); + mbedtls_printf( " ok\n" ); /* @@ -342,6 +342,6 @@ exit: return( ret ); } #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C && - MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && + MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */ diff --git a/programs/ssl/dtls_server.c b/programs/ssl/dtls_server.c index 40416be96..a44ae0b7f 100644 --- a/programs/ssl/dtls_server.c +++ b/programs/ssl/dtls_server.c @@ -29,6 +29,7 @@ #if defined(MBEDTLS_PLATFORM_C) #include "mbedtls/platform.h" #else +#include #define mbedtls_printf printf #define mbedtls_fprintf fprintf #endif @@ -37,16 +38,17 @@ !defined(MBEDTLS_SSL_COOKIE_C) || !defined(MBEDTLS_NET_C) || \ !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \ - !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) + !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \ + !defined(MBEDTLS_TIMING_C) -#include int main( void ) { printf( "MBEDTLS_SSL_SRV_C and/or MBEDTLS_SSL_PROTO_DTLS and/or " "MBEDTLS_SSL_COOKIE_C and/or MBEDTLS_NET_C and/or " "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or " - "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" ); + "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C and/or " + "MBEDTLS_TIMING_C not defined.\n" ); return( 0 ); } #else @@ -100,6 +102,7 @@ int main( void ) mbedtls_ssl_config conf; mbedtls_x509_crt srvcert; mbedtls_pk_context pkey; + mbedtls_timing_delay_context timer; #if defined(MBEDTLS_SSL_CACHE_C) mbedtls_ssl_cache_context cache; #endif @@ -232,6 +235,9 @@ int main( void ) goto exit; } + mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, + mbedtls_timing_get_delay ); + printf( " ok\n" ); reset: @@ -419,4 +425,4 @@ exit: #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C - && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */ + && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_TIMING_C */ diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index ea1e79f3d..3ed0d29af 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -36,9 +36,20 @@ #define mbedtls_snprintf snprintf #endif -#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \ - defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_CLI_C) && \ - defined(MBEDTLS_NET_C) && defined(MBEDTLS_CTR_DRBG_C) +#if !defined(MBEDTLS_ENTROPY_C) || \ + !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \ + !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ + !defined(MBEDTLS_TIMING_C) +int main( void ) +{ + mbedtls_printf("MBEDTLS_ENTROPY_C and/or " + "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or " + "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or " + "MBEDTLS_TIMING_C not defined.\n"); + return( 0 ); +} +#else + #include "mbedtls/net.h" #include "mbedtls/ssl.h" #include "mbedtls/entropy.h" @@ -47,15 +58,11 @@ #include "mbedtls/x509.h" #include "mbedtls/error.h" #include "mbedtls/debug.h" +#include "mbedtls/timing.h" #include #include #include -#endif - -#if defined(MBEDTLS_TIMING_C) -#include "mbedtls/timing.h" -#endif #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #if !defined snprintf @@ -159,13 +166,6 @@ #define USAGE_RECSPLIT #endif -#if defined(MBEDTLS_TIMING_C) -#define USAGE_TIME \ - " reco_delay=%%d default: 0 seconds\n" -#else -#define USAGE_TIME "" -#endif /* MBEDTLS_TIMING_C */ - #if defined(MBEDTLS_SSL_ALPN) #define USAGE_ALPN \ " alpn=%%s default: \"\" (disabled)\n" \ @@ -239,7 +239,7 @@ USAGE_RENEGO \ " exchanges=%%d default: 1\n" \ " reconnect=%%d default: 0 (disabled)\n" \ - USAGE_TIME \ + " reco_delay=%%d default: 0 seconds\n" \ USAGE_TICKETS \ USAGE_MAX_FRAG_LEN \ USAGE_TRUNC_HMAC \ @@ -258,17 +258,6 @@ " force_ciphersuite= default: all enabled\n"\ " acceptable ciphersuite names:\n" -#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \ - !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \ - !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C) -int main( void ) -{ - mbedtls_printf("MBEDTLS_ENTROPY_C and/or " - "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or " - "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C not defined.\n"); - return( 0 ); -} -#else /* * global options */ @@ -403,6 +392,7 @@ int main( int argc, char *argv[] ) mbedtls_ssl_context ssl; mbedtls_ssl_config conf; mbedtls_ssl_session saved_session; + mbedtls_timing_delay_context timer; #if defined(MBEDTLS_X509_CRT_PARSE_C) uint32_t flags; mbedtls_x509_crt cacert; @@ -1198,6 +1188,9 @@ int main( int argc, char *argv[] ) #endif ); + mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, + mbedtls_timing_get_delay ); + mbedtls_printf( " ok\n" ); /* @@ -1495,10 +1488,8 @@ reconnect: mbedtls_net_close( server_fd ); -#if defined(MBEDTLS_TIMING_C) if( opt.reco_delay > 0 ) mbedtls_timing_m_sleep( 1000 * opt.reco_delay ); -#endif mbedtls_printf( " . Reconnecting with saved session..." ); fflush( stdout ); @@ -1589,4 +1580,4 @@ exit: } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && - MBEDTLS_CTR_DRBG_C */ + MBEDTLS_CTR_DRBG_C && MBEDTLS_TIMING_C */ diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 300dd8759..7a947edaa 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -36,17 +36,20 @@ #define mbedtls_printf printf #endif -#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO) -#define SNI_OPTION -#endif +#if !defined(MBEDTLS_ENTROPY_C) || \ + !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \ + !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ + !defined(MBEDTLS_TIMING_C) +int main( void ) +{ + mbedtls_printf("MBEDTLS_ENTROPY_C and/or " + "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or " + "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or " + "MBEDTLS_TIMING_C not defined.\n"); + return( 0 ); +} +#else -#if defined(_WIN32) -#include -#endif - -#if defined(MBEDTLS_ENTROPY_C) && \ - defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_SRV_C) && \ - defined(MBEDTLS_NET_C) && defined(MBEDTLS_CTR_DRBG_C) #include "mbedtls/net.h" #include "mbedtls/ssl.h" #include "mbedtls/entropy.h" @@ -59,7 +62,6 @@ #include #include #include -#endif #if !defined(_WIN32) #include @@ -77,6 +79,14 @@ #include "mbedtls/memory_buffer_alloc.h" #endif +#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO) +#define SNI_OPTION +#endif + +#if defined(_WIN32) +#include +#endif + #define DFL_SERVER_ADDR NULL #define DFL_SERVER_PORT 4433 #define DFL_DEBUG_LEVEL 0 @@ -327,18 +337,6 @@ " force_ciphersuite= default: all enabled\n" \ " acceptable ciphersuite names:\n" -#if !defined(MBEDTLS_ENTROPY_C) || \ - !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \ - !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C) -#include -int main( void ) -{ - mbedtls_printf("MBEDTLS_ENTROPY_C and/or " - "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or " - "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C not defined.\n"); - return( 0 ); -} -#else /* * global options */ @@ -722,6 +720,7 @@ int main( int argc, char *argv[] ) mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ssl_context ssl; mbedtls_ssl_config conf; + mbedtls_timing_delay_context timer; #if defined(MBEDTLS_SSL_RENEGOTIATION) unsigned char renego_period[8] = { 0 }; #endif @@ -1752,6 +1751,9 @@ int main( int argc, char *argv[] ) #endif ); + mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, + mbedtls_timing_get_delay ); + mbedtls_printf( " ok\n" ); reset: @@ -2226,4 +2228,4 @@ exit: } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && - MBEDTLS_CTR_DRBG_C */ + MBEDTLS_CTR_DRBG_C && MBEDTLS_TIMING_C */