From b760f001d723d6e85d28dcdaa083dd0790db48d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 22 Jul 2014 15:53:27 +0200 Subject: [PATCH] Extract generate client random to a function --- library/ssl_cli.c | 63 +++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/library/ssl_cli.c b/library/ssl_cli.c index e72cedfe1..88a4f5af5 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -444,6 +444,38 @@ static void ssl_write_alpn_ext( ssl_context *ssl, } #endif /* POLARSSL_SSL_ALPN */ +/* + * Generate random bytes for ClientHello + */ +static int ssl_generate_random( ssl_context *ssl ) +{ + int ret; + unsigned char *p = ssl->handshake->randbytes; +#if defined(POLARSSL_HAVE_TIME) + time_t t; +#endif + +#if defined(POLARSSL_HAVE_TIME) + t = time( NULL ); + *p++ = (unsigned char)( t >> 24 ); + *p++ = (unsigned char)( t >> 16 ); + *p++ = (unsigned char)( t >> 8 ); + *p++ = (unsigned char)( t ); + + SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) ); +#else + if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 ) + return( ret ); + + p += 4; +#endif /* POLARSSL_HAVE_TIME */ + + if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 ) + return( ret ); + + return( 0 ); +} + static int ssl_write_client_hello( ssl_context *ssl ) { int ret; @@ -451,9 +483,6 @@ static int ssl_write_client_hello( ssl_context *ssl ) unsigned char *buf; unsigned char *p, *q; unsigned char offer_compress; -#if defined(POLARSSL_HAVE_TIME) - time_t t; -#endif const int *ciphersuites; const ssl_ciphersuite_t *ciphersuite_info; @@ -494,29 +523,15 @@ static int ssl_write_client_hello( ssl_context *ssl ) SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]", buf[4], buf[5] ) ); -#if defined(POLARSSL_HAVE_TIME) - t = time( NULL ); - *p++ = (unsigned char)( t >> 24 ); - *p++ = (unsigned char)( t >> 16 ); - *p++ = (unsigned char)( t >> 8 ); - *p++ = (unsigned char)( t ); - - SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) ); -#else - if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 ) + if( ( ret = ssl_generate_random( ssl ) ) != 0 ) + { + SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); return( ret ); + } - p += 4; -#endif /* POLARSSL_HAVE_TIME */ - - if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 ) - return( ret ); - - p += 28; - - memcpy( ssl->handshake->randbytes, buf + 6, 32 ); - - SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 ); + memcpy( p, ssl->handshake->randbytes, 32 ); + SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 ); + p += 32; /* * 38 . 38 session id length