mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-28 00:21:48 +03:00
Merge pull request #5524 from mprse/tls_ecdh_2c
TLS ECDH 2c: ECHDE in TLS 1.3 (client-side)
This commit is contained in:
@ -146,33 +146,61 @@ static int ssl_tls13_generate_and_write_ecdh_key_exchange(
|
||||
unsigned char *end,
|
||||
size_t *out_len )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
const mbedtls_ecp_curve_info *curve_info =
|
||||
mbedtls_ecp_curve_info_from_tls_id( named_group );
|
||||
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
||||
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
||||
psa_key_attributes_t key_attributes;
|
||||
size_t own_pubkey_len;
|
||||
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
||||
size_t ecdh_bits = 0;
|
||||
|
||||
if( curve_info == NULL )
|
||||
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
|
||||
/* Convert EC group to PSA key type. */
|
||||
if( ( handshake->ecdh_psa_type =
|
||||
mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
|
||||
return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
||||
|
||||
if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
|
||||
curve_info->grp_id ) ) != 0 )
|
||||
if( ecdh_bits > 0xffff )
|
||||
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
||||
ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
|
||||
|
||||
key_attributes = psa_key_attributes_init();
|
||||
psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
|
||||
psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
|
||||
psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
|
||||
psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
|
||||
|
||||
/* Generate ECDH private key. */
|
||||
status = psa_generate_key( &key_attributes,
|
||||
&handshake->ecdh_psa_privkey );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
|
||||
ret = psa_ssl_status_to_mbedtls( status );
|
||||
MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
|
||||
return( ret );
|
||||
|
||||
}
|
||||
|
||||
ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
|
||||
buf, end - buf,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng );
|
||||
if( ret != 0 )
|
||||
/* Export the public part of the ECDH private key from PSA. */
|
||||
status = psa_export_public_key( handshake->ecdh_psa_privkey,
|
||||
buf, (size_t)( end - buf ),
|
||||
&own_pubkey_len );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
|
||||
ret = psa_ssl_status_to_mbedtls( status );
|
||||
MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
|
||||
return( ret );
|
||||
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_Q );
|
||||
if( own_pubkey_len > (size_t)( end - buf ) )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
|
||||
return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
||||
}
|
||||
|
||||
*out_len = own_pubkey_len;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_ECDH_C */
|
||||
@ -273,7 +301,7 @@ static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
|
||||
/* Pointer to group */
|
||||
unsigned char *group = p;
|
||||
/* Length of key_exchange */
|
||||
size_t key_exchange_len;
|
||||
size_t key_exchange_len = 0;
|
||||
|
||||
/* Check there is space for header of KeyShareEntry
|
||||
* - group (2 bytes)
|
||||
@ -281,8 +309,7 @@ static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
|
||||
*/
|
||||
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
|
||||
p += 4;
|
||||
ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
|
||||
p, end,
|
||||
ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
|
||||
&key_exchange_len );
|
||||
p += key_exchange_len;
|
||||
if( ret != 0 )
|
||||
@ -333,59 +360,24 @@ cleanup:
|
||||
|
||||
#if defined(MBEDTLS_ECDH_C)
|
||||
|
||||
static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
|
||||
{
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
mbedtls_ecp_group_id grp_id;
|
||||
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
||||
grp_id = ssl->handshake->ecdh_ctx.grp.id;
|
||||
#else
|
||||
grp_id = ssl->handshake->ecdh_ctx.grp_id;
|
||||
#endif
|
||||
|
||||
curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
|
||||
if( curve_info == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
||||
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
|
||||
|
||||
if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
|
||||
return( -1 );
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_QP );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
|
||||
const unsigned char *buf,
|
||||
size_t buf_len )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
uint8_t *p = (uint8_t*)buf;
|
||||
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
||||
|
||||
ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
|
||||
buf, buf_len );
|
||||
if( ret != 0 )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
|
||||
/* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
|
||||
uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
||||
p += 2;
|
||||
|
||||
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
||||
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
||||
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
||||
}
|
||||
/* Check if key size is consistent with given buffer length. */
|
||||
if ( peerkey_len > ( buf_len - 2 ) )
|
||||
return( MBEDTLS_ERR_SSL_DECODE_ERROR );
|
||||
|
||||
if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
|
||||
|
||||
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
||||
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
||||
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
||||
}
|
||||
/* Store peer's ECDH public key. */
|
||||
memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
|
||||
handshake->ecdh_psa_peerkey_len = peerkey_len;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
@ -504,7 +496,16 @@ static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
|
||||
#if defined(MBEDTLS_ECDH_C)
|
||||
if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
|
||||
{
|
||||
/* Complete ECDHE key agreement */
|
||||
const mbedtls_ecp_curve_info *curve_info =
|
||||
mbedtls_ecp_curve_info_from_tls_id( group );
|
||||
if( curve_info == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
|
||||
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
|
||||
|
||||
ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
|
Reference in New Issue
Block a user