mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-02 21:06:37 +03:00
Merge pull request #5351 from yuhaoth/pr/remove-duplicate-supported_group_ext
Remove duplicate function for writing supported_groups extension
This commit is contained in:
@ -7195,4 +7195,129 @@ int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
|
||||
}
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \
|
||||
defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
/*
|
||||
* Function for writing a supported groups (TLS 1.3) or supported elliptic
|
||||
* curves (TLS 1.2) extension.
|
||||
*
|
||||
* The "extension_data" field of a supported groups extension contains a
|
||||
* "NamedGroupList" value (TLS 1.3 RFC8446):
|
||||
* enum {
|
||||
* secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
|
||||
* x25519(0x001D), x448(0x001E),
|
||||
* ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
|
||||
* ffdhe6144(0x0103), ffdhe8192(0x0104),
|
||||
* ffdhe_private_use(0x01FC..0x01FF),
|
||||
* ecdhe_private_use(0xFE00..0xFEFF),
|
||||
* (0xFFFF)
|
||||
* } NamedGroup;
|
||||
* struct {
|
||||
* NamedGroup named_group_list<2..2^16-1>;
|
||||
* } NamedGroupList;
|
||||
*
|
||||
* The "extension_data" field of a supported elliptic curves extension contains
|
||||
* a "NamedCurveList" value (TLS 1.2 RFC 8422):
|
||||
* enum {
|
||||
* deprecated(1..22),
|
||||
* secp256r1 (23), secp384r1 (24), secp521r1 (25),
|
||||
* x25519(29), x448(30),
|
||||
* reserved (0xFE00..0xFEFF),
|
||||
* deprecated(0xFF01..0xFF02),
|
||||
* (0xFFFF)
|
||||
* } NamedCurve;
|
||||
* struct {
|
||||
* NamedCurve named_curve_list<2..2^16-1>
|
||||
* } NamedCurveList;
|
||||
*
|
||||
* The TLS 1.3 supported groups extension was defined to be a compatible
|
||||
* generalization of the TLS 1.2 supported elliptic curves extension. They both
|
||||
* share the same extension identifier.
|
||||
*
|
||||
* DHE groups are not supported yet.
|
||||
*/
|
||||
int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
|
||||
unsigned char *buf,
|
||||
const unsigned char *end,
|
||||
size_t *out_len )
|
||||
{
|
||||
unsigned char *p = buf ;
|
||||
unsigned char *named_group_list; /* Start of named_group_list */
|
||||
size_t named_group_list_len; /* Length of named_group_list */
|
||||
const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
|
||||
|
||||
*out_len = 0;
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
|
||||
|
||||
/* Check if we have space for header and length fields:
|
||||
* - extension_type (2 bytes)
|
||||
* - extension_data_length (2 bytes)
|
||||
* - named_group_list_length (2 bytes)
|
||||
*/
|
||||
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
|
||||
p += 6;
|
||||
|
||||
named_group_list = p;
|
||||
|
||||
if( group_list == NULL )
|
||||
return( MBEDTLS_ERR_SSL_BAD_CONFIG );
|
||||
|
||||
for( ; *group_list != 0; group_list++ )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
|
||||
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
|
||||
mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
|
||||
( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
|
||||
mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
|
||||
{
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
|
||||
if( curve_info == NULL )
|
||||
continue;
|
||||
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
|
||||
MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
|
||||
p += 2;
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
|
||||
curve_info->name, *group_list ) );
|
||||
}
|
||||
#endif /* MBEDTLS_ECP_C */
|
||||
/* Add DHE groups here */
|
||||
|
||||
}
|
||||
|
||||
/* Length of named_group_list */
|
||||
named_group_list_len = p - named_group_list;
|
||||
if( named_group_list_len == 0 )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
|
||||
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
||||
}
|
||||
|
||||
/* Write extension_type */
|
||||
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
|
||||
/* Write extension_data_length */
|
||||
MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
|
||||
/* Write length of named_group_list */
|
||||
MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
|
||||
buf + 4, named_group_list_len + 2 );
|
||||
|
||||
*out_len = p - buf;
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
||||
ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
|
||||
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED ||
|
||||
MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
|
||||
#endif /* MBEDTLS_SSL_TLS_C */
|
||||
|
Reference in New Issue
Block a user