diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index 79ddc23ee..536316e67 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -499,8 +499,6 @@ struct mbedtls_ssl_handshake_params #endif #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ - void (*calc_verify)(const mbedtls_ssl_context *, unsigned char *, size_t *); - #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; #endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ @@ -1006,7 +1004,6 @@ mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig ); mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash ); unsigned char mbedtls_ssl_hash_from_md_alg( int md ); -int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ); #if defined(MBEDTLS_ECP_C) int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ); @@ -1856,6 +1853,12 @@ MBEDTLS_ALWAYS_INLINE static inline void mbedtls_ssl_update_checksum( #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ } +int mbedtls_ssl_calc_verify( int minor_ver, + mbedtls_md_type_t hash, + mbedtls_ssl_context const *ssl, + unsigned char *dst, + size_t *hlen ); + #define MBEDTLS_SSL_CHK(f) do { if( ( ret = f ) < 0 ) goto cleanup; } while( 0 ) #if defined(MBEDTLS_USE_TINYCRYPT) diff --git a/library/ssl_cli.c b/library/ssl_cli.c index fd59d2c0b..4e99a801b 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -3889,7 +3889,10 @@ static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) sign: #endif - ssl->handshake->calc_verify( ssl, hash, &hashlen ); + mbedtls_ssl_calc_verify( + mbedtls_ssl_get_minor_ver( ssl ), + mbedtls_ssl_suite_get_mac( ciphersuite_info ), + ssl, hash, &hashlen ); #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ defined(MBEDTLS_SSL_PROTO_TLS1_1) diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 3b9b70e34..39edd834d 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -4524,7 +4524,14 @@ static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) */ md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] ); - if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) ) + if( +#if defined(MBEDTLS_SHA512_C) + md_alg != MBEDTLS_MD_SHA384 && +#endif +#if defined(MBEDTLS_SHA256_C) + md_alg != MBEDTLS_MD_SHA256 && +#endif + 1 ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg" " for verify message" ) ); @@ -4593,7 +4600,9 @@ static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) /* Calculate hash and verify signature */ { size_t dummy_hlen; - ssl->handshake->calc_verify( ssl, hash, &dummy_hlen ); + mbedtls_ssl_calc_verify( + mbedtls_ssl_get_minor_ver( ssl ), + md_alg, ssl, hash, &dummy_hlen ); } if( ( ret = mbedtls_pk_verify( peer_pk, diff --git a/library/ssl_tls.c b/library/ssl_tls.c index bbd2e4483..f23b0953e 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -1359,19 +1359,11 @@ static int ssl_populate_transform( mbedtls_ssl_transform *transform, return( 0 ); } -/* - * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions - * - * Inputs: - * - SSL/TLS minor version - * - hash associated with the ciphersuite (only used by TLS 1.2) - * - * Outputs: - * - the tls_prf, calc_verify and calc_finished members of handshake structure - */ -static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, - int minor_ver, - mbedtls_md_type_t hash ) +int mbedtls_ssl_calc_verify( int minor_ver, + mbedtls_md_type_t hash, + mbedtls_ssl_context const *ssl, + unsigned char *dst, + size_t *hlen ) { #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) (void) hash; @@ -1379,16 +1371,12 @@ static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, #if defined(MBEDTLS_SSL_PROTO_SSL3) if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) - { - handshake->calc_verify = ssl_calc_verify_ssl; - } + ssl_calc_verify_ssl( ssl, dst, hlen ); else #endif #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) - { - handshake->calc_verify = ssl_calc_verify_tls; - } + ssl_calc_verify_tls( ssl, dst, hlen ); else #endif #if defined(MBEDTLS_SSL_PROTO_TLS1_2) @@ -1396,14 +1384,14 @@ static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && hash == MBEDTLS_MD_SHA384 ) { - handshake->calc_verify = ssl_calc_verify_tls_sha384; + ssl_calc_verify_tls_sha384( ssl, dst, hlen ); } else #endif #if defined(MBEDTLS_SHA256_C) if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) { - handshake->calc_verify = ssl_calc_verify_tls_sha256; + ssl_calc_verify_tls_sha256( ssl, dst, hlen ); } else #endif @@ -1457,7 +1445,10 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, unsigned char session_hash[48]; size_t hash_len; - handshake->calc_verify( ssl, session_hash, &hash_len ); + mbedtls_ssl_calc_verify( + mbedtls_ssl_get_minor_ver( ssl ), + mbedtls_ssl_suite_get_mac( ciphersuite ), + ssl, session_hash, &hash_len ); MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", session_hash, hash_len ); @@ -1494,21 +1485,9 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) { int ret; - mbedtls_ssl_ciphersuite_handle_t const ciphersuite_info = - mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); - /* Set PRF, calc_verify and calc_finished function pointers */ - ret = ssl_set_handshake_prfs( ssl->handshake, - mbedtls_ssl_get_minor_ver( ssl ), - mbedtls_ssl_suite_get_mac( ciphersuite_info ) ); - if( ret != 0 ) - { - MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); - return( ret ); - } - /* Compute master secret if needed */ ret = ssl_compute_master( ssl->handshake, ssl->session_negotiate->master, @@ -12369,30 +12348,6 @@ int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, } #endif /* MBEDTLS_X509_CRT_PARSE_C */ -#if defined(MBEDTLS_SSL_PROTO_TLS1_2) -int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) -{ - switch( md ) - { -#if defined(MBEDTLS_SHA512_C) - case MBEDTLS_SSL_HASH_SHA384: - ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; - break; -#endif -#if defined(MBEDTLS_SHA256_C) - case MBEDTLS_SSL_HASH_SHA256: - ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; - break; -#endif - - default: - return( MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH ); - } - - return( 0 ); -} -#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ - #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ defined(MBEDTLS_SSL_PROTO_TLS1_1) int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,