Improve demo/testing code in client/server2
Previously it was missing reset in case 1, and in case 2 the code was never
executed as the option value was reset to 0.
Tighten checking of return values of save(NULL, 0) now that it works.
Also, improve the printed output as well as the comments.
I checked manually that everything now works and fail in the expected way:
save, reset-or-reinit and load all succeed, but the subsequent read or write
fails.
diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c
index a9ab07f..37b047c 100644
--- a/programs/ssl/ssl_client2.c
+++ b/programs/ssl/ssl_client2.c
@@ -2924,14 +2924,10 @@
size_t buf_len;
unsigned char *context_buf = NULL;
- opt.serialize = 0;
- mbedtls_printf( " Serializing live connection..." );
+ mbedtls_printf( " . Serializing live connection..." );
ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
-
- /* Allow stub implementation returning 0 for now */
- if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL &&
- ret != 0 )
+ if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
{
mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
"-0x%x\n\n", -ret );
@@ -2950,14 +2946,32 @@
if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
buf_len, &buf_len ) ) != 0 )
{
- mbedtls_printf( "failed\n ! mbedtls_ssl_context_save returned "
+ mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
"-0x%x\n\n", -ret );
goto exit;
}
+ mbedtls_printf( " ok\n" );
+
+ if( opt.serialize == 1 )
+ {
+ mbedtls_printf( " . Reseting context..." );
+
+ if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
+ {
+ mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned "
+ "-0x%x\n\n", -ret );
+ goto exit;
+ }
+
+ mbedtls_printf( " ok\n" );
+ }
+
if( opt.serialize == 2 )
{
+ mbedtls_printf( " . Freeing and reinitializing context..." );
+
mbedtls_ssl_free( &ssl );
mbedtls_ssl_init( &ssl );
@@ -2965,7 +2979,7 @@
if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
- " -0x%x\n\n", -ret );
+ "-0x%x\n\n", -ret );
goto exit;
}
@@ -2973,8 +2987,8 @@
mbedtls_ssl_set_bio( &ssl, &server_fd, delayed_send,
delayed_recv, NULL );
else
- mbedtls_ssl_set_bio( &ssl, &server_fd,
- mbedtls_net_send, mbedtls_net_recv,
+ mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send,
+ mbedtls_net_recv,
opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
#if defined(MBEDTLS_TIMING_C)
@@ -2983,9 +2997,11 @@
mbedtls_timing_set_delay,
mbedtls_timing_get_delay );
#endif /* MBEDTLS_TIMING_C */
+
+ mbedtls_printf( " ok\n" );
}
- mbedtls_printf( " Deserializing connection..." );
+ mbedtls_printf( " . Deserializing connection..." );
if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
buf_len ) ) != 0 )
@@ -2995,6 +3011,8 @@
goto exit;
}
+
+ mbedtls_printf( " ok\n" );
}
#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */