From d736d086184da3abd6bbb8e63d4818bbb59cc64d Mon Sep 17 00:00:00 2001 From: Jarno Lamsa Date: Wed, 29 May 2019 15:15:08 +0300 Subject: [PATCH] Serialization/deserialization in ssl_client2 --- programs/ssl/ssl_client2.c | 51 +++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 9d556268d..81f3f750d 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -2353,7 +2353,56 @@ send_request: } /* - * 7c. Continue doing data exchanges? + * 7c. Simulate serialize/deserialize and go back to data exchange + */ + if( opt.serialize != 0) + { + size_t len; + unsigned char *buf = NULL; + + opt.serialize = 0; + mbedtls_printf( " Serializing live connection..." ); + + if( ( ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &len) ) != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) + { + mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned -0x%x\n\n", -ret ); + + goto exit; + } + + if( ( buf = mbedtls_calloc(1, len) ) == NULL ) + { + mbedtls_printf( " failed\n ! Couldn't allocate buffer for serialized context" ); + + goto exit; + } + + if( ( ret = mbedtls_ssl_context_save( &ssl, buf, len, &len ) ) != 0 ) + { + mbedtls_printf( "failed\n ! mbedtls_ssl_context_save returned -0x%x\n\n", -ret ); + + goto exit; + } + + mbedtls_ssl_free( &ssl ); + + mbedtls_printf( " Deserializing connection..." ); + + mbedtls_ssl_init( &ssl ); + + if( ( ret = mbedtls_ssl_context_load( &ssl, buf, len ) ) != 0 ) + { + mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned -0x%x\n\n", -ret ); + + goto exit; + } + + goto send_request; + } + + + /* + * 7d. Continue doing data exchanges? */ if( --opt.exchanges > 0 ) goto send_request;