Merge pull request #5817 from xkqian/tls13_add_server_name
Tls13 add server name
This commit is contained in:
commit
ca3c6a5698
7 changed files with 257 additions and 105 deletions
|
@ -335,6 +335,75 @@ static int ssl_tls13_check_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
|
|||
return( 1 );
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
||||
/*
|
||||
* Pick best ( private key, certificate chain ) pair based on the signature
|
||||
* algorithms supported by the client.
|
||||
*/
|
||||
static int ssl_tls13_pick_key_cert( mbedtls_ssl_context *ssl )
|
||||
{
|
||||
mbedtls_ssl_key_cert *key_cert, *key_cert_list;
|
||||
const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
|
||||
uint16_t key_sig_alg;
|
||||
|
||||
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
||||
if( ssl->handshake->sni_key_cert != NULL )
|
||||
key_cert_list = ssl->handshake->sni_key_cert;
|
||||
else
|
||||
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
||||
key_cert_list = ssl->conf->key_cert;
|
||||
|
||||
if( key_cert_list == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
|
||||
{
|
||||
for( key_cert = key_cert_list; key_cert != NULL;
|
||||
key_cert = key_cert->next )
|
||||
{
|
||||
int ret;
|
||||
MBEDTLS_SSL_DEBUG_CRT( 3, "certificate (chain) candidate",
|
||||
key_cert->cert );
|
||||
|
||||
/*
|
||||
* This avoids sending the client a cert it'll reject based on
|
||||
* keyUsage or other extensions.
|
||||
*/
|
||||
if( mbedtls_x509_crt_check_key_usage(
|
||||
key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ||
|
||||
mbedtls_x509_crt_check_extended_key_usage(
|
||||
key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
|
||||
MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ) ) != 0 )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
|
||||
"(extended) key usage extension" ) );
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = mbedtls_ssl_tls13_get_sig_alg_from_pk(
|
||||
ssl, &key_cert->cert->pk, &key_sig_alg );
|
||||
if( ret != 0 )
|
||||
continue;
|
||||
if( *sig_alg == key_sig_alg )
|
||||
{
|
||||
ssl->handshake->key_cert = key_cert;
|
||||
MBEDTLS_SSL_DEBUG_CRT(
|
||||
3, "selected certificate (chain)",
|
||||
ssl->handshake->key_cert->cert );
|
||||
return( 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return( -1 );
|
||||
}
|
||||
#endif /* MBEDTLS_X509_CRT_PARSE_C &&
|
||||
MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
||||
|
||||
/*
|
||||
*
|
||||
* STATE HANDLING: ClientHello
|
||||
|
@ -580,6 +649,21 @@ static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
|
|||
|
||||
switch( extension_type )
|
||||
{
|
||||
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
||||
case MBEDTLS_TLS_EXT_SERVERNAME:
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
|
||||
ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
|
||||
extension_data_end );
|
||||
if( ret != 0 )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_RET(
|
||||
1, "mbedtls_ssl_parse_servername_ext", ret );
|
||||
return( ret );
|
||||
}
|
||||
ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
|
||||
break;
|
||||
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
||||
|
||||
#if defined(MBEDTLS_ECDH_C)
|
||||
case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
|
||||
|
@ -685,10 +769,18 @@ static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
|
|||
ssl_tls13_debug_print_client_hello_exts( ssl );
|
||||
#endif /* MBEDTLS_DEBUG_C */
|
||||
|
||||
return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
|
||||
}
|
||||
|
||||
/* Update the handshake state machine */
|
||||
|
||||
static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
/*
|
||||
* Here we only support the ephemeral or (EC)DHE key echange mode
|
||||
*/
|
||||
|
||||
if( !ssl_tls13_check_ephemeral_key_exchange( ssl ) )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG(
|
||||
|
@ -699,14 +791,18 @@ static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
|
|||
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
||||
}
|
||||
|
||||
return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
|
||||
}
|
||||
|
||||
/* Update the handshake state machine */
|
||||
|
||||
static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
/*
|
||||
* Server certificate selection
|
||||
*/
|
||||
if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
|
||||
return( ret );
|
||||
}
|
||||
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
||||
ssl->handshake->sni_name = NULL;
|
||||
ssl->handshake->sni_name_len = 0;
|
||||
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
||||
|
||||
ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
|
||||
if( ret != 0 )
|
||||
|
@ -1337,6 +1433,11 @@ static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
|
|||
{
|
||||
int authmode;
|
||||
|
||||
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
||||
if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
|
||||
authmode = ssl->handshake->sni_authmode;
|
||||
else
|
||||
#endif
|
||||
authmode = ssl->conf->authmode;
|
||||
|
||||
if( authmode == MBEDTLS_SSL_VERIFY_NONE )
|
||||
|
@ -1450,13 +1551,17 @@ cleanup:
|
|||
static int ssl_tls13_write_server_certificate( mbedtls_ssl_context *ssl )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
if( mbedtls_ssl_own_cert( ssl ) == NULL )
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if( ( ssl_tls13_pick_key_cert( ssl ) != 0 ) ||
|
||||
mbedtls_ssl_own_cert( ssl ) == NULL )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate available." ) );
|
||||
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
||||
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
||||
return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
||||
}
|
||||
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
||||
|
||||
ret = mbedtls_ssl_tls13_write_certificate( ssl );
|
||||
if( ret != 0 )
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue