From 3652e99100045727731910834d04a9e4e9f85a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 1 Jul 2019 12:09:22 +0200 Subject: [PATCH] Add getter function for handshake->resume This makes the code more readable by having fewer #ifdefs all over the place. --- include/mbedtls/ssl_internal.h | 16 ++++++++++++++++ library/ssl_cli.c | 10 ++-------- library/ssl_srv.c | 8 +++----- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index cca71e745..c36c2ad6e 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -1111,4 +1111,20 @@ static inline unsigned int mbedtls_ssl_conf_get_ems_enforced( } #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ + +/* + * Accessor functions for optional fields of various structures + */ + +static inline int mbedtls_ssl_handshake_get_resume( + const mbedtls_ssl_handshake_params *handshake ) +{ +#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) + return( handshake->resume ); +#else + (void) handshake; + return( 0 ); +#endif +} + #endif /* ssl_internal.h */ diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 083ea3119..6731b97ab 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -888,11 +888,7 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_SSL_RENEGOTIATION) ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || #endif -#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) - ssl->handshake->resume == 0 ) -#else /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ - 0 ) -#endif + mbedtls_ssl_handshake_get_resume( ssl->handshake ) == 0 ) { n = 0; } @@ -1839,10 +1835,8 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) memcpy( ssl->session_negotiate->id, buf + 35, n ); } -#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", - ssl->handshake->resume ? "a" : "no" ) ); -#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ + mbedtls_ssl_handshake_get_resume( ssl->handshake ) ? "a" : "no" ) ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", i ) ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) ); diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 778618601..07bbe2d9a 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -2643,7 +2643,7 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) * It may be already set to 1 by ssl_parse_session_ticket_ext(). * If not, try looking up session ID in our cache. */ - if( ssl->handshake->resume == 0 && + if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == 0 && #if defined(MBEDTLS_SSL_RENEGOTIATION) ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE && #endif @@ -2657,7 +2657,7 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) #endif /* !MBEDTLS_SSL_NO_SESSION_CACHE */ #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) - if( ssl->handshake->resume == 1 ) + if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == 1 ) { /* * Resuming a session @@ -2714,10 +2714,8 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n ); -#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", - ssl->handshake->resume ? "a" : "no" ) ); -#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ + mbedtls_ssl_handshake_get_resume( ssl->handshake ) ? "a" : "no" ) ); *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 ); *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );