diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 362117fd9a..2408fd1211 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -1185,6 +1185,7 @@ int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush ); int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ); int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl ); +int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl ); int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ); int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ); diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 6deab2a8c7..a2e5f33a0d 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -1611,6 +1611,7 @@ static int ssl_tls1_3_process_server_finished( mbedtls_ssl_context *ssl ) if( ret != 0 ) return( ret ); + mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake ); mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED ); return( 0 ); } @@ -1618,9 +1619,14 @@ static int ssl_tls1_3_process_server_finished( mbedtls_ssl_context *ssl ) /* * Handler for MBEDTLS_SSL_CLIENT_FINISHED */ -static int ssl_tls1_3_write_client_finished( mbedtls_ssl_context *ssl ) +static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl ) { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) ); + int ret; + + ret = mbedtls_ssl_tls13_write_finished_message( ssl ); + if( ret != 0 ) + return( ret ); + mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS ); return( 0 ); } @@ -1689,7 +1695,7 @@ int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) break; case MBEDTLS_SSL_CLIENT_FINISHED: - ret = ssl_tls1_3_write_client_finished( ssl ); + ret = ssl_tls13_write_client_finished( ssl ); break; case MBEDTLS_SSL_FLUSH_BUFFERS: diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index b2a70f3cdd..f17bf994c2 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -29,6 +29,7 @@ #include "mbedtls/debug.h" #include "mbedtls/oid.h" #include "mbedtls/platform.h" +#include #include "ssl_misc.h" #include "ssl_tls13_keys.h" @@ -974,7 +975,7 @@ cleanup: } #endif /* MBEDTLS_SSL_CLI_C */ -static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context* ssl ) +static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl ) { #if defined(MBEDTLS_SSL_CLI_C) @@ -1014,6 +1015,93 @@ cleanup: return( ret ); } +/* + * + * STATE HANDLING: Write and send Finished message. + * + */ +/* + * Implement + */ + +static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl ) +{ + int ret; + + /* Compute transcript of handshake up to now. */ + ret = mbedtls_ssl_tls13_calculate_verify_data( ssl, + ssl->handshake->state_local.finished_out.digest, + sizeof( ssl->handshake->state_local.finished_out.digest ), + &ssl->handshake->state_local.finished_out.digest_len, + ssl->conf->endpoint ); + + if( ret != 0 ) + { + MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret ); + return( ret ); + } + + return( 0 ); +} + +static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl ) +{ + // TODO: Add back resumption keys calculation after MVP. + ((void) ssl); + + return( 0 ); +} + +static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl, + unsigned char *buf, + unsigned char *end, + size_t *olen ) +{ + size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len; + /* + * struct { + * opaque verify_data[Hash.length]; + * } Finished; + */ + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len ); + + memcpy( buf, ssl->handshake->state_local.finished_out.digest, + verify_data_len ); + + *olen = verify_data_len; + return( 0 ); +} + +/* Main entry point: orchestrates the other functions */ +int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + unsigned char *buf; + size_t buf_len, msg_len; + + MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) ); + + MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) ); + + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl, + MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) ); + + MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body( + ssl, buf, buf + buf_len, &msg_len ) ); + + mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED, + buf, msg_len ); + + MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl, + buf_len, msg_len ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) ); + +cleanup: + + MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) ); + return( ret ); +} #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */