mirror of
https://github.com/vlang/v.git
synced 2025-09-14 06:52:36 +03:00
thirdparty,net.mbedtls: update mbedtls
to latest compatible version v3.3.0 (#21118)
This commit is contained in:
parent
cb402a3340
commit
64a336932c
156 changed files with 16293 additions and 4396 deletions
176
thirdparty/mbedtls/library/ssl_tls12_server.c
vendored
176
thirdparty/mbedtls/library/ssl_tls12_server.c
vendored
|
@ -21,13 +21,7 @@
|
|||
|
||||
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_free free
|
||||
#endif
|
||||
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "ssl_misc.h"
|
||||
|
@ -36,6 +30,7 @@
|
|||
#include "mbedtls/platform_util.h"
|
||||
#include "constant_time_internal.h"
|
||||
#include "mbedtls/constant_time.h"
|
||||
#include "hash_info.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
@ -273,10 +268,11 @@ static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
|
|||
ssl->handshake->ecdh_ctx.point_format = p[0];
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
|
||||
( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
|
||||
p[0] );
|
||||
#endif
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
|
||||
return( 0 );
|
||||
}
|
||||
|
@ -294,16 +290,37 @@ static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
|
|||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
|
||||
const unsigned char *buf,
|
||||
size_t len )
|
||||
size_t len)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( ssl->handshake->psa_pake_ctx_is_ok != 1 )
|
||||
#else
|
||||
if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( ( ret = mbedtls_psa_ecjpake_read_round(
|
||||
&ssl->handshake->psa_pake_ctx, buf, len,
|
||||
MBEDTLS_ECJPAKE_ROUND_ONE ) ) != 0 )
|
||||
{
|
||||
psa_destroy_key( ssl->handshake->psa_pake_password );
|
||||
psa_pake_abort( &ssl->handshake->psa_pake_ctx );
|
||||
|
||||
MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round one", ret );
|
||||
mbedtls_ssl_send_alert_message(
|
||||
ssl,
|
||||
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
||||
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#else
|
||||
if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
|
||||
buf, len ) ) != 0 )
|
||||
{
|
||||
|
@ -312,6 +329,7 @@ static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
|
|||
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
|
||||
return( ret );
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/* Only mark the extension as OK when we're sure it is */
|
||||
ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
|
||||
|
@ -358,9 +376,6 @@ static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
|
|||
}
|
||||
|
||||
/*
|
||||
* Quoting draft-ietf-tls-dtls-connection-id-05
|
||||
* https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
|
||||
*
|
||||
* struct {
|
||||
* opaque cid<0..2^8-1>;
|
||||
* } ConnectionId;
|
||||
|
@ -713,11 +728,13 @@ static int ssl_pick_cert( mbedtls_ssl_context *ssl,
|
|||
#endif
|
||||
list = ssl->conf->key_cert;
|
||||
|
||||
int pk_alg_is_none = 0;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( pk_alg == PSA_ALG_NONE )
|
||||
pk_alg_is_none = ( pk_alg == PSA_ALG_NONE );
|
||||
#else
|
||||
if( pk_alg == MBEDTLS_PK_NONE )
|
||||
pk_alg_is_none = ( pk_alg == MBEDTLS_PK_NONE );
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if( pk_alg_is_none )
|
||||
return( 0 );
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
|
||||
|
@ -734,18 +751,21 @@ static int ssl_pick_cert( mbedtls_ssl_context *ssl,
|
|||
MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
|
||||
cur->cert );
|
||||
|
||||
int key_type_matches = 0;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
if( ( ssl->conf->f_async_sign_start == NULL &&
|
||||
ssl->conf->f_async_decrypt_start == NULL &&
|
||||
! mbedtls_pk_can_do_ext( cur->key, pk_alg, pk_usage ) ) ||
|
||||
! mbedtls_pk_can_do_ext( &cur->cert->pk, pk_alg, pk_usage ) )
|
||||
key_type_matches = ( ( ssl->conf->f_async_sign_start != NULL ||
|
||||
ssl->conf->f_async_decrypt_start != NULL ||
|
||||
mbedtls_pk_can_do_ext( cur->key, pk_alg, pk_usage ) ) &&
|
||||
mbedtls_pk_can_do_ext( &cur->cert->pk, pk_alg, pk_usage ) );
|
||||
#else
|
||||
if( ! mbedtls_pk_can_do_ext( cur->key, pk_alg, pk_usage ) )
|
||||
key_type_matches = (
|
||||
mbedtls_pk_can_do_ext( cur->key, pk_alg, pk_usage ) );
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
#else
|
||||
if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
|
||||
key_type_matches = mbedtls_pk_can_do( &cur->cert->pk, pk_alg );
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if( !key_type_matches )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
|
||||
continue;
|
||||
|
@ -922,6 +942,8 @@ static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
|
|||
|
||||
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
|
||||
|
||||
int renegotiating;
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
||||
read_record_header:
|
||||
#endif
|
||||
|
@ -930,9 +952,11 @@ read_record_header:
|
|||
* otherwise read it ourselves manually in order to support SSLv2
|
||||
* ClientHello, which doesn't use the same record layer format.
|
||||
*/
|
||||
renegotiating = 0;
|
||||
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
||||
if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
|
||||
renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE );
|
||||
#endif
|
||||
if( !renegotiating )
|
||||
{
|
||||
if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
|
||||
{
|
||||
|
@ -1298,7 +1322,10 @@ read_record_header:
|
|||
buf + ciph_offset + 2, ciph_len );
|
||||
|
||||
/*
|
||||
* Check the compression algorithms length and pick one
|
||||
* Check the compression algorithm's length.
|
||||
* The list contents are ignored because implementing
|
||||
* MBEDTLS_SSL_COMPRESS_NULL is mandatory and is the only
|
||||
* option supported by Mbed TLS.
|
||||
*/
|
||||
comp_offset = ciph_offset + 2 + ciph_len;
|
||||
|
||||
|
@ -1317,12 +1344,6 @@ read_record_header:
|
|||
MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
|
||||
buf + comp_offset + 1, comp_len );
|
||||
|
||||
ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
|
||||
/* See comments in ssl_write_client_hello() */
|
||||
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
|
||||
ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
|
||||
#endif
|
||||
/*
|
||||
* Check the extension length
|
||||
*/
|
||||
|
@ -1751,9 +1772,6 @@ static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
|
|||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
|
||||
|
||||
/*
|
||||
* Quoting draft-ietf-tls-dtls-connection-id-05
|
||||
* https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
|
||||
*
|
||||
* struct {
|
||||
* opaque cid<0..2^8-1>;
|
||||
* } ConnectionId;
|
||||
|
@ -1995,6 +2013,18 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
|
|||
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
|
||||
p += 2;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = mbedtls_psa_ecjpake_write_round( &ssl->handshake->psa_pake_ctx,
|
||||
p + 2, end - p - 2, &kkpp_len,
|
||||
MBEDTLS_ECJPAKE_ROUND_ONE );
|
||||
if ( ret != 0 )
|
||||
{
|
||||
psa_destroy_key( ssl->handshake->psa_pake_password );
|
||||
psa_pake_abort( &ssl->handshake->psa_pake_ctx );
|
||||
MBEDTLS_SSL_DEBUG_RET( 1 , "psa_pake_output", ret );
|
||||
return;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
|
||||
p + 2, end - p - 2, &kkpp_len,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng );
|
||||
|
@ -2003,6 +2033,7 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
|
|||
MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
|
||||
return;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
|
||||
p += 2;
|
||||
|
@ -2180,8 +2211,7 @@ static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl )
|
|||
if( ret != 0 )
|
||||
goto exit;
|
||||
|
||||
if( session->ciphersuite != session_tmp.ciphersuite ||
|
||||
session->compression != session_tmp.compression )
|
||||
if( session->ciphersuite != session_tmp.ciphersuite )
|
||||
{
|
||||
/* Mismatch between cached and negotiated session */
|
||||
goto exit;
|
||||
|
@ -2331,12 +2361,12 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
|
|||
|
||||
MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
|
||||
p += 2;
|
||||
*p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression );
|
||||
*p++ = MBEDTLS_BYTE_0( MBEDTLS_SSL_COMPRESS_NULL );
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
|
||||
mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
|
||||
MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
|
||||
(unsigned int) ssl->session_negotiate->compression ) );
|
||||
(unsigned int) MBEDTLS_SSL_COMPRESS_NULL ) );
|
||||
|
||||
/*
|
||||
* First write extensions, then the total length
|
||||
|
@ -2531,10 +2561,15 @@ static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
|
|||
if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
|
||||
continue;
|
||||
|
||||
MBEDTLS_PUT_UINT16_BE( *sig_alg, p, sa_len );
|
||||
/* Write elements at offsets starting from 1 (offset 0 is for the
|
||||
* length). Thus the offset of each element is the length of the
|
||||
* partial list including that element. */
|
||||
sa_len += 2;
|
||||
MBEDTLS_PUT_UINT16_BE( *sig_alg, p, sa_len );
|
||||
|
||||
}
|
||||
|
||||
/* Fill in list length. */
|
||||
MBEDTLS_PUT_UINT16_BE( sa_len, p, 0 );
|
||||
sa_len += 2;
|
||||
p += sa_len;
|
||||
|
@ -2808,6 +2843,46 @@ static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
|
|||
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
unsigned char *out_p = ssl->out_msg + ssl->out_msglen;
|
||||
unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
|
||||
ssl->out_msglen;
|
||||
size_t output_offset = 0;
|
||||
size_t output_len = 0;
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
|
||||
/*
|
||||
* The first 3 bytes are:
|
||||
* [0] MBEDTLS_ECP_TLS_NAMED_CURVE
|
||||
* [1, 2] elliptic curve's TLS ID
|
||||
*
|
||||
* However since we only support secp256r1 for now, we hardcode its
|
||||
* TLS ID here
|
||||
*/
|
||||
if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id(
|
||||
MBEDTLS_ECP_DP_SECP256R1 ) ) == NULL )
|
||||
{
|
||||
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
|
||||
}
|
||||
*out_p = MBEDTLS_ECP_TLS_NAMED_CURVE;
|
||||
MBEDTLS_PUT_UINT16_BE( curve_info->tls_id, out_p, 1 );
|
||||
output_offset += 3;
|
||||
|
||||
ret = mbedtls_psa_ecjpake_write_round( &ssl->handshake->psa_pake_ctx,
|
||||
out_p + output_offset,
|
||||
end_p - out_p - output_offset, &output_len,
|
||||
MBEDTLS_ECJPAKE_ROUND_TWO );
|
||||
if( ret != 0 )
|
||||
{
|
||||
psa_destroy_key( ssl->handshake->psa_pake_password );
|
||||
psa_pake_abort( &ssl->handshake->psa_pake_ctx );
|
||||
MBEDTLS_SSL_DEBUG_RET( 1 , "psa_pake_output", ret );
|
||||
return( ret );
|
||||
}
|
||||
|
||||
output_offset += output_len;
|
||||
ssl->out_msglen += output_offset;
|
||||
#else
|
||||
size_t len = 0;
|
||||
|
||||
ret = mbedtls_ecjpake_write_round_two(
|
||||
|
@ -2822,6 +2897,7 @@ static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
|
|||
}
|
||||
|
||||
ssl->out_msglen += len;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
|
||||
|
@ -3063,11 +3139,8 @@ curve_matching_done:
|
|||
|
||||
size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
|
||||
size_t hashlen = 0;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
unsigned char hash[PSA_HASH_MAX_SIZE];
|
||||
#else
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
#endif
|
||||
unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
|
||||
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
/*
|
||||
|
@ -3409,8 +3482,14 @@ static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
|
|||
size_t peer_pmssize )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
mbedtls_x509_crt *own_cert = mbedtls_ssl_own_cert( ssl );
|
||||
if( own_cert == NULL ) {
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no local certificate" ) );
|
||||
return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
|
||||
}
|
||||
mbedtls_pk_context *public_key = &own_cert->pk;
|
||||
mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
|
||||
mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
|
||||
size_t len = mbedtls_pk_get_len( public_key );
|
||||
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
|
@ -4036,6 +4115,18 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
|
|||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( ( ret = mbedtls_psa_ecjpake_read_round(
|
||||
&ssl->handshake->psa_pake_ctx, p, end - p,
|
||||
MBEDTLS_ECJPAKE_ROUND_TWO ) ) != 0 )
|
||||
{
|
||||
psa_destroy_key( ssl->handshake->psa_pake_password );
|
||||
psa_pake_abort( &ssl->handshake->psa_pake_ctx );
|
||||
|
||||
MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round two", ret );
|
||||
return( ret );
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
|
||||
p, end - p );
|
||||
if( ret != 0 )
|
||||
|
@ -4052,6 +4143,7 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
|
|||
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
|
||||
return( ret );
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue