Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile errors without 0-RTT, MPS and compatible mode #359

Open
wants to merge 17 commits into
base: tls13-prototype
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/mbedtls/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,7 @@ void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
}
#endif

const char * mbedtls_debug_get_state_string( mbedtls_ssl_states stat);


#endif /* debug.h */
8 changes: 8 additions & 0 deletions include/mbedtls/mbedtls_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,14 @@
*/
#define MBEDTLS_ZERO_RTT

/**
* \def MBEDTLS_TLS13_EARLY_DATA
*
* Allows to add extension early_data for TLS 1.3
*
*/
#define MBEDTLS_TLS13_EARLY_DATA

/**
* \def MBEDTLS_SSL_DEBUG_HANDSHAKE_HASHES
*
Expand Down
7 changes: 5 additions & 2 deletions include/mbedtls/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,20 +648,23 @@ typedef enum
MBEDTLS_SSL_HELLO_RETRY_REQUEST,
MBEDTLS_SSL_SECOND_CLIENT_HELLO,
MBEDTLS_SSL_SECOND_SERVER_HELLO,
MBEDTLS_SSL_EARLY_DATA,
#if defined(MBEDTLS_TLS13_EARLY_DATA)
MBEDTLS_SSL_EARLY_APP_DATA,
MBEDTLS_SSL_END_OF_EARLY_DATA,
#endif
MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY,
MBEDTLS_SSL_ENCRYPTED_EXTENSIONS,
MBEDTLS_SSL_HANDSHAKE_FINISH_ACK,
#if defined(MBEDTLS_SSL_NEW_SESSION_TICKET)
MBEDTLS_SSL_CLIENT_NEW_SESSION_TICKET,
#endif
#if defined(MBEDTLS_SSL_TLS13_COMPATIBILITY_MODE)
MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO,
MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED,
MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO,
MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO,
MBEDTLS_SSL_SERVER_CCS_AFTER_HRR,
#endif /* MBEDTLS_SSL_TLS13_COMPATIBILITY_MODE */
MBEDTLS_SSL_EARLY_APP_DATA
#endif
}
mbedtls_ssl_states;
Expand Down
51 changes: 51 additions & 0 deletions library/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,55 @@ void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
}
#endif /* MBEDTLS_ECDH_C */

const char * mbedtls_debug_get_state_string( mbedtls_ssl_states stat)
{
static const char * stat_str[]={
[MBEDTLS_SSL_HELLO_REQUEST] ="MBEDTLS_SSL_HELLO_REQUEST",
[MBEDTLS_SSL_CLIENT_HELLO] ="MBEDTLS_SSL_CLIENT_HELLO",
[MBEDTLS_SSL_SERVER_HELLO] ="MBEDTLS_SSL_SERVER_HELLO",
[MBEDTLS_SSL_SERVER_CERTIFICATE] ="MBEDTLS_SSL_SERVER_CERTIFICATE",
[MBEDTLS_SSL_SERVER_KEY_EXCHANGE] ="MBEDTLS_SSL_SERVER_KEY_EXCHANGE",
[MBEDTLS_SSL_CERTIFICATE_REQUEST] ="MBEDTLS_SSL_CERTIFICATE_REQUEST",
[MBEDTLS_SSL_SERVER_HELLO_DONE] ="MBEDTLS_SSL_SERVER_HELLO_DONE",
[MBEDTLS_SSL_CLIENT_CERTIFICATE] ="MBEDTLS_SSL_CLIENT_CERTIFICATE",
[MBEDTLS_SSL_CLIENT_KEY_EXCHANGE] ="MBEDTLS_SSL_CLIENT_KEY_EXCHANGE",
[MBEDTLS_SSL_CERTIFICATE_VERIFY] ="MBEDTLS_SSL_CERTIFICATE_VERIFY",
[MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC] ="MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC",
[MBEDTLS_SSL_CLIENT_FINISHED] ="MBEDTLS_SSL_CLIENT_FINISHED",
[MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC] ="MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC",
[MBEDTLS_SSL_SERVER_FINISHED] ="MBEDTLS_SSL_SERVER_FINISHED",
[MBEDTLS_SSL_FLUSH_BUFFERS] ="MBEDTLS_SSL_FLUSH_BUFFERS",
[MBEDTLS_SSL_HANDSHAKE_WRAPUP] ="MBEDTLS_SSL_HANDSHAKE_WRAPUP",
[MBEDTLS_SSL_HANDSHAKE_OVER] ="MBEDTLS_SSL_HANDSHAKE_OVER",
[MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET] ="MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET",
[MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET_FLUSH] ="MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET_FLUSH",
[MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT] ="MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT",
#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
[MBEDTLS_SSL_HELLO_RETRY_REQUEST] ="MBEDTLS_SSL_HELLO_RETRY_REQUEST",
[MBEDTLS_SSL_SECOND_CLIENT_HELLO] ="MBEDTLS_SSL_SECOND_CLIENT_HELLO",
[MBEDTLS_SSL_SECOND_SERVER_HELLO] ="MBEDTLS_SSL_SECOND_SERVER_HELLO",
#if defined(MBEDTLS_TLS13_EARLY_DATA)
[MBEDTLS_SSL_EARLY_APP_DATA] ="MBEDTLS_SSL_EARLY_APP_DATA",
[MBEDTLS_SSL_END_OF_EARLY_DATA] ="MBEDTLS_SSL_END_OF_EARLY_DATA",
#endif
[MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY] ="MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY",
[MBEDTLS_SSL_ENCRYPTED_EXTENSIONS] ="MBEDTLS_SSL_ENCRYPTED_EXTENSIONS",
[MBEDTLS_SSL_HANDSHAKE_FINISH_ACK] ="MBEDTLS_SSL_HANDSHAKE_FINISH_ACK",
#if defined(MBEDTLS_SSL_NEW_SESSION_TICKET)
[MBEDTLS_SSL_CLIENT_NEW_SESSION_TICKET] ="MBEDTLS_SSL_CLIENT_NEW_SESSION_TICKET",
#endif
#if defined(MBEDTLS_SSL_TLS13_COMPATIBILITY_MODE)
[MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO] ="MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO",
[MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED] ="MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED",
[MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO] ="MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO",
[MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO] ="MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO",
[MBEDTLS_SSL_SERVER_CCS_AFTER_HRR] ="MBEDTLS_SSL_SERVER_CCS_AFTER_HRR",
#endif /* MBEDTLS_SSL_TLS13_COMPATIBILITY_MODE */
#endif
};
if( stat < 0 || stat >= sizeof(stat_str)/sizeof(stat_str[0]) )
return( "null" );
return( stat_str[stat] ? stat_str[stat] : "null" );
}

#endif /* MBEDTLS_DEBUG_C */
3 changes: 2 additions & 1 deletion library/ssl_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ static inline int mbedtls_ssl_get_psk_to_offer( const mbedtls_ssl_context *ssl,
{
ptrs_present = 1;
}

#if defined(MBEDTLS_SSL_NEW_SESSION_TICKET)
/* Check if a ticket has been configured. */
if( ssl->session_negotiate != NULL &&
ssl->session_negotiate->ticket != NULL )
Expand All @@ -1487,6 +1487,7 @@ static inline int mbedtls_ssl_get_psk_to_offer( const mbedtls_ssl_context *ssl,
}
return( 0 );
}
#endif /* MBEDTLS_SSL_NEW_SESSION_TICKET */

/* Check if an external PSK has been configured. */
if( ssl->conf->psk != NULL )
Expand Down
20 changes: 11 additions & 9 deletions library/ssl_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -4405,15 +4405,7 @@ int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
}


#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
/* mbedtls_ssl_conf_tls13_key_exchange( ) allows to set the key exchange mode. */
int mbedtls_ssl_conf_tls13_key_exchange( mbedtls_ssl_config* conf,
const int key_exchange_mode )
{
conf->key_exchange_modes = key_exchange_mode;
return 0;
}
#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */



void mbedtls_ssl_remove_hs_psk( mbedtls_ssl_context* ssl )
Expand Down Expand Up @@ -4509,6 +4501,16 @@ void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
}
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */

#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
/* mbedtls_ssl_conf_tls13_key_exchange( ) allows to set the key exchange mode. */
int mbedtls_ssl_conf_tls13_key_exchange( mbedtls_ssl_config* conf,
const int key_exchange_mode )
{
conf->key_exchange_modes = key_exchange_mode;
return 0;
}
#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */

#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
const unsigned char *dhm_P, size_t P_len,
Expand Down
114 changes: 62 additions & 52 deletions library/ssl_tls13_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,12 @@ int ssl_write_early_data_process( mbedtls_ssl_context* ssl )
#endif /* MBEDTLS_SSL_USE_MPS */

#else /* MBEDTLS_ZERO_RTT */
#if defined(MBEDTLS_SSL_USE_MPS)
((void) buf);
((void) buf_len);
((void) msg);
((void) msg_len);
#endif
/* Should never happen */
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );

Expand Down Expand Up @@ -339,9 +341,10 @@ static int ssl_write_early_data_coordinate( mbedtls_ssl_context* ssl )

static int ssl_write_early_data_postprocess( mbedtls_ssl_context* ssl )
{
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
/* Clear PSK we've used for the 0-RTT. */
mbedtls_ssl_remove_hs_psk( ssl );

#endif
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
return ( 0 );
}
Expand Down Expand Up @@ -1006,7 +1009,7 @@ int mbedtls_ssl_write_pre_shared_key_ext( mbedtls_ssl_context *ssl,

#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */


#if defined(MBEDTLS_SSL_COOKIE_C)
static int ssl_write_cookie_ext( mbedtls_ssl_context *ssl,
unsigned char* buf,
unsigned char* end,
Expand Down Expand Up @@ -1054,6 +1057,7 @@ static int ssl_write_cookie_ext( mbedtls_ssl_context *ssl,

return( 0 );
}
#endif /* MBEDTLS_SSL_COOKIE_C */

#if defined(MBEDTLS_ECDH_C)
/*
Expand Down Expand Up @@ -1406,8 +1410,10 @@ static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl )
{
#if defined(MBEDTLS_SSL_TLS13_COMPATIBILITY_MODE)
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO );
#else
#elif defined(MBEDTLS_TLS13_EARLY_DATA)
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_EARLY_APP_DATA );
#else
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
#endif /* MBEDTLS_SSL_TLS13_COMPATIBILITY_MODE */

return( 0 );
Expand Down Expand Up @@ -1661,11 +1667,49 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl,
total_ext_len += cur_ext_len;
buf += cur_ext_len;

#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
/* The supported_groups and the key_share extensions are
* REQUIRED for ECDHE ciphersuites.
*/
ret = ssl_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
if( ret != 0 )
return( ret );

total_ext_len += cur_ext_len;
buf += cur_ext_len;

/* The supported_signature_algorithms extension is REQUIRED for
* certificate authenticated ciphersuites. */
ret = mbedtls_ssl_write_signature_algorithms_ext( ssl, buf, end, &cur_ext_len );
if( ret != 0 )
return( ret );

total_ext_len += cur_ext_len;
buf += cur_ext_len;

/* We need to send the key shares under three conditions:
* 1 ) A certificate-based ciphersuite is being offered. In this case
* supported_groups and supported_signature extensions have been successfully added.
* 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
* psk_key_exchange_modes has been added as the last extension.
* 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
*/

ret = ssl_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
if( ret != 0 )
return( ret );

total_ext_len += cur_ext_len;
buf += cur_ext_len;
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */

#if defined(MBEDTLS_SSL_COOKIE_C)
/* For TLS / DTLS 1.3 we need to support the use of cookies
* ( if the server provided them ) */
ssl_write_cookie_ext( ssl, buf, end, &cur_ext_len );
total_ext_len += cur_ext_len;
buf += cur_ext_len;
#endif /* MBEDTLS_SSL_COOKIE_C */

#if defined(MBEDTLS_SSL_ALPN)
ssl_write_alpn_ext( ssl, buf, end, &cur_ext_len );
Expand Down Expand Up @@ -1719,42 +1763,6 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl,
buf += cur_ext_len;
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */

#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
/* The supported_groups and the key_share extensions are
* REQUIRED for ECDHE ciphersuites.
*/
ret = ssl_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
if( ret != 0 )
return( ret );

total_ext_len += cur_ext_len;
buf += cur_ext_len;

/* The supported_signature_algorithms extension is REQUIRED for
* certificate authenticated ciphersuites. */
ret = mbedtls_ssl_write_signature_algorithms_ext( ssl, buf, end, &cur_ext_len );
if( ret != 0 )
return( ret );

total_ext_len += cur_ext_len;
buf += cur_ext_len;

/* We need to send the key shares under three conditions:
* 1 ) A certificate-based ciphersuite is being offered. In this case
* supported_groups and supported_signature extensions have been successfully added.
* 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
* psk_key_exchange_modes has been added as the last extension.
* 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
*/

ret = ssl_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
if( ret != 0 )
return( ret );

total_ext_len += cur_ext_len;
buf += cur_ext_len;
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */

#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
{
size_t bytes_written;
Expand Down Expand Up @@ -2459,7 +2467,7 @@ static int ssl_encrypted_extensions_process( mbedtls_ssl_context* ssl )
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_fetch_handshake_msg( ssl,
MBEDTLS_SSL_HS_ENCRYPTED_EXTENSION,
&buf, &buflen ) );

MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
/* Process the message contents */
MBEDTLS_SSL_PROC_CHK( ssl_encrypted_extensions_parse( ssl, buf, buflen ) );

Expand Down Expand Up @@ -3926,7 +3934,7 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl )
MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
}
MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s(%d)", mbedtls_debug_get_state_string(ssl->state), ssl->state ) );

if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
return( ret );
Expand All @@ -3945,10 +3953,20 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl )
ret = ssl_client_hello_process( ssl );
break;

#if defined(MBEDTLS_TLS13_EARLY_DATA)
case MBEDTLS_SSL_EARLY_APP_DATA:
ret = ssl_write_early_data_process( ssl );
break;

/*
* ==> (EndOfEarlyData)
* (Certificate)
* (CertificateVerify)
* (Finished)
*/
case MBEDTLS_SSL_END_OF_EARLY_DATA:
ret = ssl_write_end_of_early_data_process( ssl );
break;
#endif /* MBEDTLS_TLS13_EARLY_DATA */
/*
* <== ServerHello / HelloRetryRequest
* EncryptedExtensions
Expand Down Expand Up @@ -3981,16 +3999,6 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl )
ret = mbedtls_ssl_finished_in_process( ssl );
break;

/*
* ==> (EndOfEarlyData)
* (Certificate)
* (CertificateVerify)
* (Finished)
*/
case MBEDTLS_SSL_END_OF_EARLY_DATA:
ret = ssl_write_end_of_early_data_process( ssl );
break;

case MBEDTLS_SSL_CLIENT_CERTIFICATE:
ret = mbedtls_ssl_write_certificate_process( ssl );
break;
Expand All @@ -4003,6 +4011,7 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl )
ret = mbedtls_ssl_finished_out_process( ssl );
break;

#if defined(MBEDTLS_SSL_NEW_SESSION_TICKET)
/*
* <== NewSessionTicket
*/
Expand All @@ -4014,6 +4023,7 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl )

ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
break;
#endif /* MBEDTLS_SSL_NEW_SESSION_TICKET */

/*
* Injection of dummy-CCS's for middlebox compatibility
Expand Down
Loading