From b5ff6a4ced964b43ef8096812d9fbfa3305b8ea2 Mon Sep 17 00:00:00 2001 From: Jarno Lamsa Date: Thu, 6 Jun 2019 10:40:52 +0300 Subject: [PATCH] Add option for ssl-context re-initialization flow --- programs/ssl/ssl_client2.c | 30 +++++++++++++++++++++++++++--- programs/ssl/ssl_server2.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 415caa0cb..fd4e1803b 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -282,7 +282,9 @@ int main( void ) #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) #define USAGE_SERIALIZATION \ - " serialize=%%d default: 0 (do not serialize/deserialize)\n" + " serialize=%%d default: 0 (do not serialize/deserialize)\n" \ + " options: 1 (serialize)\n" \ + " 2 (serialize with re-initialization)\n" #else #define USAGE_SERIALIZATION "" #endif @@ -1229,7 +1231,7 @@ int main( int argc, char *argv[] ) else if( strcmp( p, "serialize") == 0 ) { opt.serialize = atoi( q ); - if( opt.serialize < 0 || opt.serialize > 1) + if( opt.serialize < 0 || opt.serialize > 2) goto usage; } else @@ -2363,7 +2365,7 @@ send_request: * 7c. Simulate serialize/deserialize and go back to data exchange */ #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) - if( opt.serialize != 0) + if( opt.serialize != 0 ) { size_t len; unsigned char *buf = NULL; @@ -2399,6 +2401,28 @@ send_request: goto exit; } + if( opt.serialize == 2 ) + { + mbedtls_ssl_free( &ssl ); + + mbedtls_ssl_init( &ssl ); + + if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) + { + mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", + -ret ); + goto exit; + } + + if( opt.nbio == 2 ) + mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL ); + else + mbedtls_ssl_set_bio( &ssl, &server_fd, + mbedtls_net_send, mbedtls_net_recv, + opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL ); + + } + mbedtls_printf( " Deserializing connection..." ); if( ( ret = mbedtls_ssl_context_load( &ssl, buf, len ) ) != 0 ) diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index b954a2ca0..ac956813e 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -387,7 +387,9 @@ int main( void ) #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) #define USAGE_SERIALIZATION \ - " serialize=%%d default: 0 (do not serialize/deserialize)\n" + " serialize=%%d default: 0 (do not serialize/deserialize)\n" \ + " options: 1 (serialize)\n" \ + " 2 (serialize with re-initialization)\n" #else #define USAGE_SERIALIZATION "" #endif @@ -1931,7 +1933,7 @@ int main( int argc, char *argv[] ) else if( strcmp( p, "serialize") == 0 ) { opt.serialize = atoi( q ); - if( opt.serialize < 0 || opt.serialize > 1) + if( opt.serialize < 0 || opt.serialize > 2) goto usage; } else @@ -3348,7 +3350,7 @@ data_exchange: * 7b. Simulate serialize/deserialize and go back to data exchange */ #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) - if( opt.serialize != 0) + if( opt.serialize != 0 ) { size_t len; unsigned char *buf = NULL; @@ -3384,6 +3386,27 @@ data_exchange: goto exit; } + if( opt.serialize == 2 ) + { + mbedtls_ssl_free( &ssl ); + + mbedtls_ssl_init( &ssl ); + + if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) + { + mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", + -ret ); + goto exit; + } + + if( opt.nbio == 2 ) + mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL ); + else + mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, + opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL ); + + } + mbedtls_printf( " Deserializing connection..." ); if( ( ret = mbedtls_ssl_context_load( &ssl, buf, len ) ) != 0 )