fix various issues

Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
This commit is contained in:
Jerry Yu 2022-02-15 11:05:59 +08:00
parent a23b9d954c
commit 3e536442f5
2 changed files with 76 additions and 84 deletions

View file

@ -1737,12 +1737,12 @@ int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl );
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
/* /*
* Handler of TLS 1.3 write certificate message * Handler of TLS 1.3 write Certificate message
*/ */
int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl ); int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl );
/* /*
* Handler of TLS 1.3 write certificate verify message * Handler of TLS 1.3 write Certificate Verify message
*/ */
int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl ); int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl );

View file

@ -855,12 +855,12 @@ cleanup:
* if it is available. * if it is available.
* Returns a negative error code on failure ( such as no certificate * Returns a negative error code on failure ( such as no certificate
* being available on the server ), and otherwise * being available on the server ), and otherwise
* SSL_WRITE_CERTIFICATE_AVAILABLE or * SSL_WRITE_CERTIFICATE_SEND or
* SSL_WRITE_CERTIFICATE_SKIP * SSL_WRITE_CERTIFICATE_SKIP
* indicating that a Certificate message should be written based * indicating that a Certificate message should be written based
* on the configured certificate, or whether it should be silently skipped. * on the configured certificate, or whether it should be silently skipped.
*/ */
#define SSL_WRITE_CERTIFICATE_AVAILABLE 0 #define SSL_WRITE_CERTIFICATE_SEND 0
#define SSL_WRITE_CERTIFICATE_SKIP 1 #define SSL_WRITE_CERTIFICATE_SKIP 1
static int ssl_tls13_write_certificate_coordinate( mbedtls_ssl_context* ssl ) static int ssl_tls13_write_certificate_coordinate( mbedtls_ssl_context* ssl )
@ -891,7 +891,7 @@ static int ssl_tls13_write_certificate_coordinate( mbedtls_ssl_context* ssl )
} }
#endif /* MBEDTLS_SSL_CLI_C */ #endif /* MBEDTLS_SSL_CLI_C */
return( SSL_WRITE_CERTIFICATE_AVAILABLE ); return( SSL_WRITE_CERTIFICATE_SEND );
} }
@ -920,12 +920,12 @@ static int ssl_tls13_write_certificate_coordinate( mbedtls_ssl_context* ssl )
* } Certificate; * } Certificate;
*/ */
static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl, static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
const unsigned char *buf, unsigned char *buf,
unsigned char *end, unsigned char *end,
size_t *olen ) size_t *out_len )
{ {
const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl ); const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
unsigned char *p = (unsigned char *)buf; unsigned char *p = buf;
unsigned char *certificate_list; unsigned char *certificate_list;
@ -933,6 +933,11 @@ static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
/* empty certificate_request_context with length 0 */ /* empty certificate_request_context with length 0 */
*p++ = 0; *p++ = 0;
/* Reserve space for certificate_list_len */
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
certificate_list = p;
p += 3;
#if defined(MBEDTLS_SSL_CLI_C) #if defined(MBEDTLS_SSL_CLI_C)
/* If the server requests client authentication but no suitable /* If the server requests client authentication but no suitable
* certificate is available, the client MUST send a * certificate is available, the client MUST send a
@ -941,23 +946,15 @@ static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
* *
* authmode indicates whether the client configuration required authentication. * authmode indicates whether the client configuration required authentication.
*/ */
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && crt == NULL )
( ( crt == NULL ) || ssl->conf->authmode == MBEDTLS_SSL_VERIFY_NONE ) )
{ {
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write empty client certificate" ) ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write empty client certificate" ) );
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3); MBEDTLS_PUT_UINT24_BE( 0, certificate_list, 0 );
MBEDTLS_PUT_UINT24_BE( 0, p, 0); *out_len = p - buf;
p += 3;
*olen = p - buf;
return( 0 ); return( 0 );
} }
#endif /* MBEDTLS_SSL_CLI_C */ #endif /* MBEDTLS_SSL_CLI_C */
certificate_list = p;
/* Reserve space for certificate_list_len */
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3);
p += 3;
MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt ); MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
while ( crt != NULL ) while ( crt != NULL )
@ -979,15 +976,14 @@ static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
p += 2; p += 2;
} }
MBEDTLS_PUT_UINT24_BE( p - certificate_list - 3, certificate_list, 0); MBEDTLS_PUT_UINT24_BE( p - certificate_list - 3, certificate_list, 0 );
*olen = p - buf; *out_len = p - buf;
return( 0 ); return( 0 );
} }
/* Update the state after handling the outgoing certificate message. */ static int ssl_tls13_finalize_write_certificate( mbedtls_ssl_context *ssl )
static int ssl_tls13_finalize_write_certificate( mbedtls_ssl_context* ssl )
{ {
#if defined(MBEDTLS_SSL_CLI_C) #if defined(MBEDTLS_SSL_CLI_C)
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
@ -1001,7 +997,7 @@ static int ssl_tls13_finalize_write_certificate( mbedtls_ssl_context* ssl )
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
} }
int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context* ssl ) int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
{ {
int ret; int ret;
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
@ -1009,7 +1005,7 @@ int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context* ssl )
/* Coordination: Check if we need to send a certificate. */ /* Coordination: Check if we need to send a certificate. */
MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_write_certificate_coordinate( ssl ) ); MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_write_certificate_coordinate( ssl ) );
if( ret == SSL_WRITE_CERTIFICATE_AVAILABLE ) if( ret == SSL_WRITE_CERTIFICATE_SEND )
{ {
unsigned char *buf; unsigned char *buf;
size_t buf_len, msg_len; size_t buf_len, msg_len;
@ -1043,7 +1039,10 @@ cleanup:
return( ret ); return( ret );
} }
/* Coordinate: Check whether a certificate verify message should be sent. /*
* STATE HANDLING: Output Certificate Verify
*/
/* Coordinate: Check whether a Certificate Verify message should be sent.
* Returns a negative value on failure, and otherwise * Returns a negative value on failure, and otherwise
* - SSL_WRITE_CERTIFICATE_VERIFY_SKIP * - SSL_WRITE_CERTIFICATE_VERIFY_SKIP
* - SSL_WRITE_CERTIFICATE_VERIFY_SEND * - SSL_WRITE_CERTIFICATE_VERIFY_SEND
@ -1052,7 +1051,7 @@ cleanup:
#define SSL_WRITE_CERTIFICATE_VERIFY_SKIP 0 #define SSL_WRITE_CERTIFICATE_VERIFY_SKIP 0
#define SSL_WRITE_CERTIFICATE_VERIFY_SEND 1 #define SSL_WRITE_CERTIFICATE_VERIFY_SEND 1
static int ssl_tls13_write_certificate_verify_coordinate( static int ssl_tls13_write_certificate_verify_coordinate(
mbedtls_ssl_context* ssl ) mbedtls_ssl_context *ssl )
{ {
mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl ); mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
@ -1091,39 +1090,40 @@ static int ssl_tls13_write_certificate_verify_coordinate(
#endif /* !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ #endif /* !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
} }
static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context* ssl, static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
unsigned char* buf, unsigned char *buf,
size_t buflen, unsigned char *end,
size_t* olen ) size_t *out_len )
{ {
int ret; int ret;
size_t n = 0; unsigned char *p = buf;
unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
size_t verify_buffer_len;
mbedtls_pk_context *own_key; mbedtls_pk_context *own_key;
size_t own_key_size;
unsigned int md_alg;
int sig_alg;
unsigned char verify_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
size_t verify_hash_len;
unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ]; unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
size_t handshake_hash_len; size_t handshake_hash_len;
unsigned char *p; unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
size_t verify_buffer_len;
size_t own_key_size;
unsigned int md_alg;
int algorithm;
size_t signature_len = 0;
const mbedtls_md_info_t *md_info; const mbedtls_md_info_t *md_info;
unsigned char * const end = buf + buflen; unsigned char verify_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
size_t verify_hash_len;
p = buf; MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 + MBEDTLS_TLS1_3_MD_MAX_SIZE );
if( buflen < 2 + MBEDTLS_MD_MAX_SIZE )
own_key = mbedtls_ssl_own_key( ssl );
if( own_key == NULL )
{ {
MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too short" ) ); MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
} }
/* /*
* Check whether the signature scheme corresponds to the key we are using * Check whether the signature scheme corresponds to the key we are using
*/ */
if( mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) ) != if( mbedtls_ssl_sig_from_pk( own_key ) != MBEDTLS_SSL_SIG_ECDSA )
MBEDTLS_SSL_SIG_ECDSA )
{ {
MBEDTLS_SSL_DEBUG_MSG( 1, MBEDTLS_SSL_DEBUG_MSG( 1,
( "CertificateVerify: Only ECDSA signature algorithms are supported." ) ); ( "CertificateVerify: Only ECDSA signature algorithms are supported." ) );
@ -1159,40 +1159,34 @@ static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context* ssl,
*/ */
/* Determine size of key */ /* Determine size of key */
own_key = mbedtls_ssl_own_key( ssl ); own_key_size = mbedtls_pk_get_bitlen( own_key );
if( own_key != NULL) switch( own_key_size )
{ {
own_key_size = mbedtls_pk_get_bitlen( own_key ); case 256:
switch( own_key_size ) md_alg = MBEDTLS_MD_SHA256;
{ algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
case 256: break;
md_alg = MBEDTLS_MD_SHA256; case 384:
sig_alg = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256; md_alg = MBEDTLS_MD_SHA384;
break; algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
case 384: break;
md_alg = MBEDTLS_MD_SHA384; default:
sig_alg = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384; MBEDTLS_SSL_DEBUG_MSG( 3,
break; ( "unknown key size: %" MBEDTLS_PRINTF_SIZET
default: " bits",
MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown key size: %" MBEDTLS_PRINTF_SIZET " bits", own_key_size ) );
own_key_size ) ); return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
}
} }
else
if( !mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
{ {
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
} }
if( !mbedtls_ssl_sig_alg_is_received( ssl, sig_alg ) ) MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
{ p += 2;
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
}
*(p++) = (unsigned char)( ( sig_alg >> 8 ) & 0xFF );
*(p++) = (unsigned char)( ( sig_alg >> 0 ) & 0xFF );
/* Hash verify buffer with indicated hash function */ /* Hash verify buffer with indicated hash function */
md_info = mbedtls_md_info_from_type( md_alg ); md_info = mbedtls_md_info_from_type( md_alg );
@ -1208,24 +1202,22 @@ static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context* ssl,
if( ( ret = mbedtls_pk_sign( own_key, md_alg, if( ( ret = mbedtls_pk_sign( own_key, md_alg,
verify_hash, verify_hash_len, verify_hash, verify_hash_len,
p + 2, (size_t)( end - ( p + 2 ) ), &n, p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
{ {
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
return( ret ); return( ret );
} }
// unsigned char * x= p;
p[0] = (unsigned char)( n >> 8 );
p[1] = (unsigned char)( n >> 0 );
p += 2 + n; MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
p += 2 + signature_len;
*out_len = (size_t)( p - buf );
*olen = (size_t)( p - buf );
MBEDTLS_SSL_DEBUG_BUF( 3, "xverify hash", buf, *olen );
return( ret ); return( ret );
} }
static int ssl_tls13_finalize_certificate_verify( mbedtls_ssl_context* ssl ) static int ssl_tls13_finalize_certificate_verify( mbedtls_ssl_context *ssl )
{ {
#if defined(MBEDTLS_SSL_CLI_C) #if defined(MBEDTLS_SSL_CLI_C)
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
@ -1241,7 +1233,7 @@ static int ssl_tls13_finalize_certificate_verify( mbedtls_ssl_context* ssl )
return( 0 ); return( 0 );
} }
int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context* ssl ) int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
{ {
int ret = 0; int ret = 0;
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
@ -1258,7 +1250,7 @@ int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context* ssl )
MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) ); MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body( MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
ssl, buf, buf_len, &msg_len ) ); ssl, buf, buf + buf_len, &msg_len ) );
mbedtls_ssl_tls13_add_hs_msg_to_checksum( mbedtls_ssl_tls13_add_hs_msg_to_checksum(
ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, msg_len ); ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, msg_len );