2021-08-06 10:29:08 +02:00
|
|
|
/*
|
|
|
|
* TLS 1.3 client-side functions
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* This file is part of mbed TLS ( https://tls.mbed.org )
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2022-01-28 03:24:45 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2021-08-06 10:29:08 +02:00
|
|
|
|
2021-08-24 09:59:48 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-09-01 11:48:49 +02:00
|
|
|
#include "mbedtls/debug.h"
|
|
|
|
#include "mbedtls/error.h"
|
2021-09-10 04:08:31 +02:00
|
|
|
#include "mbedtls/platform.h"
|
2021-08-17 04:44:40 +02:00
|
|
|
|
2021-09-14 13:30:36 +02:00
|
|
|
#include "ssl_misc.h"
|
2022-02-18 17:24:56 +01:00
|
|
|
#include "ssl_client.h"
|
2021-09-10 04:08:31 +02:00
|
|
|
#include "ssl_tls13_keys.h"
|
2021-09-14 13:30:36 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
/* Write extensions */
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
/*
|
|
|
|
* ssl_tls13_write_supported_versions_ext():
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolVersion versions<2..254>;
|
|
|
|
* } SupportedVersions;
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
|
2021-08-30 12:32:07 +02:00
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
2021-12-02 07:36:27 +01:00
|
|
|
size_t *out_len )
|
2021-08-17 04:44:40 +02:00
|
|
|
{
|
2021-08-27 10:59:09 +02:00
|
|
|
unsigned char *p = buf;
|
2022-04-08 01:07:11 +02:00
|
|
|
unsigned char versions_len = ( ssl->handshake->min_tls_version <=
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0;
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2021-08-31 06:51:25 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
|
2021-08-17 04:44:40 +02:00
|
|
|
|
2021-09-15 12:41:02 +02:00
|
|
|
/* Check if we have space to write the extension:
|
2021-09-08 10:41:02 +02:00
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_length (2 bytes)
|
|
|
|
* - versions_length (1 byte )
|
2022-03-30 17:20:47 +02:00
|
|
|
* - versions (2 or 4 bytes)
|
2021-08-31 06:51:25 +02:00
|
|
|
*/
|
2022-03-30 17:20:47 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
|
2021-08-06 10:29:08 +02:00
|
|
|
|
2022-02-10 14:35:27 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
|
|
|
|
MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
|
2021-08-30 12:32:07 +02:00
|
|
|
p += 4;
|
2021-08-18 10:46:28 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Length of versions */
|
2022-02-10 14:35:27 +01:00
|
|
|
*p++ = versions_len;
|
2021-08-27 09:14:01 +02:00
|
|
|
|
2021-09-02 06:59:12 +02:00
|
|
|
/* Write values of supported versions.
|
|
|
|
* They are defined by the configuration.
|
2022-02-10 14:35:27 +01:00
|
|
|
* Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
|
2021-08-27 10:59:09 +02:00
|
|
|
*/
|
2022-03-15 08:23:42 +01:00
|
|
|
mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_3 );
|
2022-02-10 14:35:27 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
|
2021-08-18 10:46:28 +02:00
|
|
|
|
|
|
|
|
2022-04-08 01:07:11 +02:00
|
|
|
if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
|
2022-02-10 14:35:27 +01:00
|
|
|
{
|
2022-03-15 08:23:42 +01:00
|
|
|
mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_2 );
|
2022-02-10 14:35:27 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = 5 + versions_len;
|
2021-08-18 10:46:28 +02:00
|
|
|
|
|
|
|
return( 0 );
|
2021-08-18 10:38:40 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 16:30:19 +02:00
|
|
|
static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
2021-10-13 07:36:05 +02:00
|
|
|
const unsigned char *end )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
((void) ssl);
|
|
|
|
|
2022-03-30 20:04:10 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
|
2022-03-15 08:23:42 +01:00
|
|
|
if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_3 )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
|
2021-10-11 15:45:31 +02:00
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2022-03-30 20:04:10 +02:00
|
|
|
if( &buf[2] != end )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
}
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-01-21 16:34:27 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len )
|
|
|
|
{
|
|
|
|
size_t list_len, name_len;
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char *end = buf + len;
|
|
|
|
|
|
|
|
/* If we didn't send it, the server shouldn't send it */
|
|
|
|
if( ssl->conf->alpn_list == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* opaque ProtocolName<1..2^8-1>;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolName protocol_name_list<2..2^16-1>
|
|
|
|
* } ProtocolNameList;
|
|
|
|
*
|
|
|
|
* the "ProtocolNameList" MUST contain exactly one "ProtocolName"
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
|
|
|
|
|
|
|
|
list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
p += 2;
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
|
|
|
|
|
|
|
|
name_len = *p++;
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
|
|
|
|
|
|
|
|
/* Check that the server chosen protocol was in our list and save it */
|
|
|
|
for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
|
|
|
|
{
|
|
|
|
if( name_len == strlen( *alpn ) &&
|
|
|
|
memcmp( buf + 3, *alpn, name_len ) == 0 )
|
|
|
|
{
|
|
|
|
ssl->alpn_chosen = *alpn;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
|
|
|
|
2022-01-14 08:35:47 +01:00
|
|
|
static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
|
2021-12-07 10:16:29 +01:00
|
|
|
{
|
|
|
|
uint16_t group_id = ssl->handshake->offered_group_id;
|
2022-03-15 10:19:18 +01:00
|
|
|
|
2021-12-07 10:16:29 +01:00
|
|
|
if( group_id == 0 )
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
|
2022-01-20 12:14:50 +01:00
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
2021-12-07 10:16:29 +01:00
|
|
|
if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
|
2022-01-19 07:56:30 +01:00
|
|
|
{
|
2022-03-15 10:19:18 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
/* Destroy generated private key. */
|
|
|
|
status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ret = psa_ssl_status_to_mbedtls( status );
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
|
2022-01-19 07:56:30 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
2022-01-20 12:14:50 +01:00
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_ECDH_C */
|
|
|
|
if( 0 /* other KEMs? */ )
|
2021-12-07 10:16:29 +01:00
|
|
|
{
|
|
|
|
/* Do something */
|
|
|
|
}
|
|
|
|
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
|
|
|
|
2021-09-01 11:48:49 +02:00
|
|
|
/*
|
|
|
|
* Functions for writing key_share extension.
|
|
|
|
*/
|
2021-09-08 10:41:02 +02:00
|
|
|
static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
|
|
|
|
uint16_t *group_id )
|
2021-09-01 11:48:49 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
2021-10-06 10:32:11 +02:00
|
|
|
const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
|
2021-09-15 12:41:02 +02:00
|
|
|
/* Pick first available ECDHE group compatible with TLS 1.3 */
|
2021-10-06 10:32:11 +02:00
|
|
|
if( group_list == NULL )
|
2021-09-15 12:41:02 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_CONFIG );
|
|
|
|
|
2021-10-06 10:32:11 +02:00
|
|
|
for ( ; *group_list != 0; group_list++ )
|
2021-09-01 11:48:49 +02:00
|
|
|
{
|
2021-11-18 08:29:56 +01:00
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
|
|
curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
|
|
|
|
if( curve_info != NULL &&
|
2021-10-06 10:32:11 +02:00
|
|
|
mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
|
2021-09-01 11:48:49 +02:00
|
|
|
{
|
2021-10-06 10:32:11 +02:00
|
|
|
*group_id = *group_list;
|
2021-09-01 11:48:49 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) ssl);
|
2021-09-08 10:41:02 +02:00
|
|
|
((void) group_id);
|
2021-09-01 11:48:49 +02:00
|
|
|
#endif /* MBEDTLS_ECDH_C */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add DHE named groups here.
|
2021-09-15 12:41:02 +02:00
|
|
|
* Pick first available DHE group compatible with TLS 1.3
|
2021-09-01 11:48:49 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/*
|
2021-09-01 11:48:49 +02:00
|
|
|
* ssl_tls13_write_key_share_ext
|
|
|
|
*
|
2021-09-15 12:41:02 +02:00
|
|
|
* Structure of key_share extension in ClientHello:
|
2021-09-01 11:48:49 +02:00
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* NamedGroup group;
|
|
|
|
* opaque key_exchange<1..2^16-1>;
|
|
|
|
* } KeyShareEntry;
|
|
|
|
* struct {
|
|
|
|
* KeyShareEntry client_shares<0..2^16-1>;
|
|
|
|
* } KeyShareClientHello;
|
2021-09-01 06:57:29 +02:00
|
|
|
*/
|
2021-09-01 11:48:49 +02:00
|
|
|
static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
2021-12-02 07:36:27 +01:00
|
|
|
size_t *out_len )
|
2021-09-01 11:48:49 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
2021-11-18 08:29:56 +01:00
|
|
|
unsigned char *client_shares; /* Start of client_shares */
|
|
|
|
size_t client_shares_len; /* Length of client_shares */
|
2021-09-01 11:48:49 +02:00
|
|
|
uint16_t group_id;
|
|
|
|
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0;
|
2021-09-01 11:48:49 +02:00
|
|
|
|
2021-09-08 10:41:02 +02:00
|
|
|
/* Check if we have space for header and length fields:
|
2021-09-01 11:48:49 +02:00
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_length (2 bytes)
|
|
|
|
* - client_shares_length (2 bytes)
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
|
|
|
|
p += 6;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
|
|
|
|
|
|
|
|
/* HRR could already have requested something else. */
|
|
|
|
group_id = ssl->handshake->offered_group_id;
|
2021-09-08 10:41:02 +02:00
|
|
|
if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
|
|
|
|
!mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
|
2021-09-01 11:48:49 +02:00
|
|
|
{
|
2021-09-08 10:41:02 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
|
2021-09-01 11:48:49 +02:00
|
|
|
&group_id ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dispatch to type-specific key generation function.
|
|
|
|
*
|
|
|
|
* So far, we're only supporting ECDHE. With the introduction
|
|
|
|
* of PQC KEMs, we'll want to have multiple branches, one per
|
|
|
|
* type of KEM, and dispatch to the corresponding crypto. And
|
|
|
|
* only one key share entry is allowed.
|
|
|
|
*/
|
2021-11-18 08:29:56 +01:00
|
|
|
client_shares = p;
|
2021-09-01 11:48:49 +02:00
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
2021-09-08 10:41:02 +02:00
|
|
|
if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
|
2021-09-01 11:48:49 +02:00
|
|
|
{
|
2021-09-15 12:41:02 +02:00
|
|
|
/* Pointer to group */
|
2021-11-18 08:29:56 +01:00
|
|
|
unsigned char *group = p;
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Length of key_exchange */
|
2022-02-10 15:56:26 +01:00
|
|
|
size_t key_exchange_len = 0;
|
2021-09-01 11:48:49 +02:00
|
|
|
|
|
|
|
/* Check there is space for header of KeyShareEntry
|
|
|
|
* - group (2 bytes)
|
|
|
|
* - key_exchange_length (2 bytes)
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
|
|
|
|
p += 4;
|
2022-03-30 16:43:29 +02:00
|
|
|
ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
|
|
|
|
ssl, group_id, p, end, &key_exchange_len );
|
2021-09-01 11:48:49 +02:00
|
|
|
p += key_exchange_len;
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
/* Write group */
|
2021-11-18 08:29:56 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Write key_exchange_length */
|
2021-11-18 08:29:56 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
|
2021-09-01 11:48:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_ECDH_C */
|
|
|
|
if( 0 /* other KEMs? */ )
|
|
|
|
{
|
|
|
|
/* Do something */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
|
2021-09-08 10:41:02 +02:00
|
|
|
/* Length of client_shares */
|
2021-11-18 08:29:56 +01:00
|
|
|
client_shares_len = p - client_shares;
|
2021-09-08 10:41:02 +02:00
|
|
|
if( client_shares_len == 0)
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
2021-09-08 11:55:09 +02:00
|
|
|
}
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Write extension_type */
|
|
|
|
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
|
|
|
|
/* Write extension_data_length */
|
2021-09-08 10:41:02 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Write client_shares_length */
|
2021-09-08 10:41:02 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
|
2021-09-01 11:48:49 +02:00
|
|
|
|
|
|
|
/* Update offered_group_id field */
|
|
|
|
ssl->handshake->offered_group_id = group_id;
|
|
|
|
|
|
|
|
/* Output the total length of key_share extension. */
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = p - buf;
|
2021-09-01 11:48:49 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
|
2021-09-01 11:48:49 +02:00
|
|
|
|
|
|
|
ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2022-01-24 11:12:51 +01:00
|
|
|
/*
|
|
|
|
* ssl_tls13_parse_hrr_key_share_ext()
|
|
|
|
* Parse key_share extension in Hello Retry Request
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* NamedGroup selected_group;
|
|
|
|
* } KeyShareHelloRetryRequest;
|
|
|
|
*/
|
2022-01-18 07:23:32 +01:00
|
|
|
static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
|
2022-01-14 05:03:11 +01:00
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
|
|
|
{
|
|
|
|
const mbedtls_ecp_curve_info *curve_info = NULL;
|
|
|
|
const unsigned char *p = buf;
|
2022-01-24 11:12:51 +01:00
|
|
|
int selected_group;
|
2022-01-14 05:03:11 +01:00
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
|
|
|
|
if( group_list == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_CONFIG );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
|
|
|
|
|
|
|
|
/* Read selected_group */
|
2022-01-17 03:05:52 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
2022-01-24 11:12:51 +01:00
|
|
|
selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
|
2022-01-14 05:03:11 +01:00
|
|
|
|
|
|
|
/* Upon receipt of this extension in a HelloRetryRequest, the client
|
|
|
|
* MUST first verify that the selected_group field corresponds to a
|
|
|
|
* group which was provided in the "supported_groups" extension in the
|
|
|
|
* original ClientHello.
|
|
|
|
* The supported_group was based on the info in ssl->conf->group_list.
|
|
|
|
*
|
|
|
|
* If the server provided a key share that was not sent in the ClientHello
|
|
|
|
* then the client MUST abort the handshake with an "illegal_parameter" alert.
|
|
|
|
*/
|
2022-01-18 07:23:32 +01:00
|
|
|
for( ; *group_list != 0; group_list++ )
|
2022-01-14 05:03:11 +01:00
|
|
|
{
|
|
|
|
curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
|
2022-01-24 11:12:51 +01:00
|
|
|
if( curve_info == NULL || curve_info->tls_id != selected_group )
|
2022-01-14 05:03:11 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We found a match */
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Client MUST verify that the selected_group field does not
|
|
|
|
* correspond to a group which was provided in the "key_share"
|
|
|
|
* extension in the original ClientHello. If the server sent an
|
|
|
|
* HRR message with a key share already provided in the
|
|
|
|
* ClientHello then the client MUST abort the handshake with
|
|
|
|
* an "illegal_parameter" alert.
|
|
|
|
*/
|
2022-01-24 11:12:51 +01:00
|
|
|
if( found == 0 || selected_group == ssl->handshake->offered_group_id )
|
2022-01-14 05:03:11 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
|
|
|
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember server's preference for next ClientHello */
|
2022-01-24 11:12:51 +01:00
|
|
|
ssl->handshake->offered_group_id = selected_group;
|
2022-01-14 05:03:11 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
/*
|
2021-10-13 07:36:05 +02:00
|
|
|
* ssl_tls13_parse_key_share_ext()
|
|
|
|
* Parse key_share extension in Server Hello
|
|
|
|
*
|
2021-09-10 04:08:31 +02:00
|
|
|
* struct {
|
|
|
|
* KeyShareEntry server_share;
|
|
|
|
* } KeyShareServerHello;
|
|
|
|
* struct {
|
|
|
|
* NamedGroup group;
|
|
|
|
* opaque key_exchange<1..2^16-1>;
|
|
|
|
* } KeyShareEntry;
|
|
|
|
*/
|
2021-10-11 15:45:31 +02:00
|
|
|
static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
|
2021-09-10 04:08:31 +02:00
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-09-10 04:08:31 +02:00
|
|
|
const unsigned char *p = buf;
|
2021-10-11 15:45:31 +02:00
|
|
|
uint16_t group, offered_group;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
|
|
|
* NamedGroup group; (2 bytes)
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
|
|
|
group = MBEDTLS_GET_UINT16_BE( p, 0 );
|
2021-09-10 04:08:31 +02:00
|
|
|
p += 2;
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* Check that the chosen group matches the one we offered. */
|
2021-09-10 04:08:31 +02:00
|
|
|
offered_group = ssl->handshake->offered_group_id;
|
2021-10-11 15:45:31 +02:00
|
|
|
if( offered_group != group )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
|
|
|
( "Invalid server key share, our group %u, their group %u",
|
2021-10-11 15:45:31 +02:00
|
|
|
(unsigned) offered_group, (unsigned) group ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
2021-10-11 15:45:31 +02:00
|
|
|
if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2022-02-10 10:32:02 +01:00
|
|
|
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 ) );
|
|
|
|
|
2022-04-10 12:20:43 +02:00
|
|
|
ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
|
2021-09-10 04:08:31 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
}
|
2021-10-13 07:36:05 +02:00
|
|
|
else
|
2021-09-10 04:08:31 +02:00
|
|
|
#endif /* MBEDTLS_ECDH_C */
|
2021-10-13 07:36:05 +02:00
|
|
|
if( 0 /* other KEMs? */ )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
/* Do something */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
|
|
|
|
ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:12:51 +01:00
|
|
|
/*
|
|
|
|
* ssl_tls13_parse_cookie_ext()
|
|
|
|
* Parse cookie extension in Hello Retry Request
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* opaque cookie<1..2^16-1>;
|
|
|
|
* } Cookie;
|
|
|
|
*
|
|
|
|
* When sending a HelloRetryRequest, the server MAY provide a "cookie"
|
|
|
|
* extension to the client (this is an exception to the usual rule that
|
|
|
|
* the only extensions that may be sent are those that appear in the
|
|
|
|
* ClientHello). When sending the new ClientHello, the client MUST copy
|
|
|
|
* the contents of the extension received in the HelloRetryRequest into
|
|
|
|
* a "cookie" extension in the new ClientHello. Clients MUST NOT use
|
|
|
|
* cookies in their initial ClientHello in subsequent connections.
|
|
|
|
*/
|
2022-01-21 05:32:58 +01:00
|
|
|
static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
|
|
|
{
|
2022-02-08 11:49:53 +01:00
|
|
|
uint16_t cookie_len;
|
2022-01-21 05:32:58 +01:00
|
|
|
const unsigned char *p = buf;
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
|
|
|
|
|
|
|
/* Retrieve length field of cookie */
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
|
|
|
cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
p += 2;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
|
|
|
|
|
2022-02-09 07:02:25 +01:00
|
|
|
mbedtls_free( handshake->cookie );
|
2022-02-08 11:49:53 +01:00
|
|
|
handshake->hrr_cookie_len = 0;
|
2022-02-09 07:02:25 +01:00
|
|
|
handshake->cookie = mbedtls_calloc( 1, cookie_len );
|
|
|
|
if( handshake->cookie == NULL )
|
2022-01-21 05:32:58 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
2022-02-08 11:49:53 +01:00
|
|
|
( "alloc failed ( %ud bytes )",
|
2022-01-21 05:32:58 +01:00
|
|
|
cookie_len ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
}
|
|
|
|
|
2022-02-09 07:02:25 +01:00
|
|
|
memcpy( handshake->cookie, p, cookie_len );
|
2022-02-08 11:49:53 +01:00
|
|
|
handshake->hrr_cookie_len = cookie_len;
|
2022-01-21 05:32:58 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-01-27 11:36:51 +01:00
|
|
|
static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
|
2022-02-07 09:32:16 +01:00
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
2022-02-08 11:31:07 +01:00
|
|
|
size_t *out_len )
|
2022-01-27 11:36:51 +01:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
2022-02-08 11:31:07 +01:00
|
|
|
*out_len = 0;
|
2022-02-10 08:31:25 +01:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-01-27 11:36:51 +01:00
|
|
|
|
2022-02-10 08:31:25 +01:00
|
|
|
if( handshake->cookie == NULL )
|
2022-01-27 11:36:51 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
|
2022-02-10 08:31:25 +01:00
|
|
|
handshake->cookie,
|
|
|
|
handshake->hrr_cookie_len );
|
2022-01-27 11:36:51 +01:00
|
|
|
|
2022-02-10 08:31:25 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
|
2022-01-27 11:36:51 +01:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
|
|
|
|
|
|
|
|
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
|
2022-02-10 08:31:25 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
|
|
|
|
MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
|
2022-02-07 09:32:16 +01:00
|
|
|
p += 6;
|
2022-01-27 11:36:51 +01:00
|
|
|
|
|
|
|
/* Cookie */
|
2022-02-10 08:31:25 +01:00
|
|
|
memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
|
2022-01-27 11:36:51 +01:00
|
|
|
|
2022-02-10 08:31:25 +01:00
|
|
|
*out_len = handshake->hrr_cookie_len + 6;
|
2022-01-27 11:36:51 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-02-18 17:24:56 +01:00
|
|
|
int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len )
|
2022-02-18 12:06:07 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *p = buf;
|
|
|
|
size_t ext_len;
|
|
|
|
|
|
|
|
*out_len = 0;
|
|
|
|
|
|
|
|
/* Write supported_versions extension
|
|
|
|
*
|
|
|
|
* Supported Versions Extension is mandatory with TLS 1.3.
|
|
|
|
*/
|
|
|
|
ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
p += ext_len;
|
|
|
|
|
|
|
|
/* Echo the cookie if the server provided one in its preceding
|
|
|
|
* HelloRetryRequest message.
|
|
|
|
*/
|
|
|
|
ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
p += ext_len;
|
|
|
|
|
|
|
|
if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
|
|
|
|
{
|
|
|
|
ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
p += ext_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = p - buf;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
/*
|
2021-10-11 15:45:31 +02:00
|
|
|
* Functions for parsing and processing Server Hello
|
2021-09-10 04:08:31 +02:00
|
|
|
*/
|
2022-04-05 11:04:20 +02:00
|
|
|
|
2022-03-30 09:57:11 +02:00
|
|
|
/**
|
|
|
|
* \brief Detect if the ServerHello contains a supported_versions extension
|
|
|
|
* or not.
|
|
|
|
*
|
|
|
|
* \param[in] ssl SSL context
|
|
|
|
* \param[in] buf Buffer containing the ServerHello message
|
|
|
|
* \param[in] end End of the buffer containing the ServerHello message
|
|
|
|
*
|
|
|
|
* \return 0 if the ServerHello does not contain a supported_versions extension
|
|
|
|
* \return 1 if the ServerHello contains a supported_versions extension
|
|
|
|
* \return A negative value if an error occurred while parsing the ServerHello.
|
|
|
|
*/
|
2022-02-10 16:45:15 +01:00
|
|
|
static int ssl_tls13_is_supported_versions_ext_present(
|
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
size_t legacy_session_id_echo_len;
|
|
|
|
size_t extensions_len;
|
|
|
|
const unsigned char *extensions_end;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check there is enough data to access the legacy_session_id_echo vector
|
2022-03-30 09:57:11 +02:00
|
|
|
* length:
|
|
|
|
* - legacy_version 2 bytes
|
|
|
|
* - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
|
|
|
|
* - legacy_session_id_echo length 1 byte
|
2022-02-10 16:45:15 +01:00
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
|
|
|
|
p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
|
|
|
|
legacy_session_id_echo_len = *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump to the extensions, jumping over:
|
|
|
|
* - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
|
|
|
|
* - cipher_suite 2 bytes
|
|
|
|
* - legacy_compression_method 1 byte
|
|
|
|
*/
|
|
|
|
p += legacy_session_id_echo_len + 4;
|
|
|
|
|
|
|
|
/* Case of no extension */
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
/* ...
|
|
|
|
* Extension extensions<6..2^16-1>;
|
|
|
|
* ...
|
|
|
|
* struct {
|
|
|
|
* ExtensionType extension_type; (2 bytes)
|
|
|
|
* opaque extension_data<0..2^16-1>;
|
|
|
|
* } Extension;
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
p += 2;
|
|
|
|
|
|
|
|
/* Check extensions do not go beyond the buffer of data. */
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
|
|
|
|
extensions_end = p + extensions_len;
|
|
|
|
|
|
|
|
while( p < extensions_end )
|
|
|
|
{
|
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
|
|
|
|
extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
|
|
|
|
p += 4;
|
|
|
|
|
|
|
|
if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
|
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
|
|
|
|
p += extension_data_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-04-05 11:04:20 +02:00
|
|
|
/* Returns a negative value on failure, and otherwise
|
|
|
|
* - 1 if the last eight bytes of the ServerHello random bytes indicate that
|
|
|
|
* the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
|
|
|
|
* - 0 otherwise
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
|
|
|
{
|
|
|
|
/* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
|
|
|
|
static const unsigned char magic_downgrade_string[] =
|
|
|
|
{ 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
|
|
|
|
const unsigned char *last_eight_bytes_of_random;
|
|
|
|
unsigned char last_byte_of_random;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
|
|
|
|
last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
|
|
|
|
|
|
|
|
if( memcmp( last_eight_bytes_of_random,
|
|
|
|
magic_downgrade_string,
|
|
|
|
sizeof( magic_downgrade_string ) ) == 0 )
|
|
|
|
{
|
|
|
|
last_byte_of_random = last_eight_bytes_of_random[7];
|
|
|
|
return( last_byte_of_random == 0 ||
|
|
|
|
last_byte_of_random == 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-10-15 12:46:14 +02:00
|
|
|
/* Returns a negative value on failure, and otherwise
|
2021-10-13 07:36:05 +02:00
|
|
|
* - SSL_SERVER_HELLO_COORDINATE_HELLO or
|
|
|
|
* - SSL_SERVER_HELLO_COORDINATE_HRR
|
2021-12-10 11:33:56 +01:00
|
|
|
* to indicate which message is expected and to be parsed next.
|
|
|
|
*/
|
2021-10-13 07:36:05 +02:00
|
|
|
#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
|
|
|
|
#define SSL_SERVER_HELLO_COORDINATE_HRR 1
|
|
|
|
static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Check whether this message is a HelloRetryRequest ( HRR ) message.
|
|
|
|
*
|
2021-10-11 15:45:31 +02:00
|
|
|
* Server Hello and HRR are only distinguished by Random set to the
|
2021-09-10 04:08:31 +02:00
|
|
|
* special value of the SHA-256 of "HelloRetryRequest".
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolVersion legacy_version = 0x0303;
|
|
|
|
* Random random;
|
|
|
|
* opaque legacy_session_id_echo<0..32>;
|
|
|
|
* CipherSuite cipher_suite;
|
|
|
|
* uint8 legacy_compression_method = 0;
|
2021-10-13 07:36:05 +02:00
|
|
|
* Extension extensions<6..2^16-1>;
|
2021-09-10 04:08:31 +02:00
|
|
|
* } ServerHello;
|
|
|
|
*
|
|
|
|
*/
|
2022-04-11 17:00:01 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
|
|
|
|
2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
|
2021-10-11 15:45:31 +02:00
|
|
|
|
2022-04-11 17:00:01 +02:00
|
|
|
if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
|
|
|
|
sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
return( SSL_SERVER_HELLO_COORDINATE_HRR );
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 07:36:05 +02:00
|
|
|
return( SSL_SERVER_HELLO_COORDINATE_HELLO );
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 16:01:04 +02:00
|
|
|
/* Fetch and preprocess
|
|
|
|
* Returns a negative value on failure, and otherwise
|
|
|
|
* - SSL_SERVER_HELLO_COORDINATE_HELLO or
|
2022-02-10 16:45:15 +01:00
|
|
|
* - SSL_SERVER_HELLO_COORDINATE_HRR or
|
|
|
|
* - SSL_SERVER_HELLO_COORDINATE_TLS1_2
|
2021-10-13 16:01:04 +02:00
|
|
|
*/
|
2022-02-10 16:45:15 +01:00
|
|
|
#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
|
2021-10-13 07:36:05 +02:00
|
|
|
static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char **buf,
|
|
|
|
size_t *buf_len )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-11 15:45:31 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-04-05 11:04:20 +02:00
|
|
|
const unsigned char *end;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2022-01-20 12:14:50 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
|
|
|
|
MBEDTLS_SSL_HS_SERVER_HELLO,
|
|
|
|
buf, buf_len ) );
|
2022-04-05 11:04:20 +02:00
|
|
|
end = *buf + *buf_len;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2022-02-10 16:45:15 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
|
2022-04-05 11:04:20 +02:00
|
|
|
ssl, *buf, end ) );
|
2022-02-10 16:45:15 +01:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2022-04-05 11:04:20 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK_NEG(
|
|
|
|
ssl_tls13_is_downgrade_negotiation( ssl, *buf, end ) );
|
|
|
|
|
|
|
|
/* If the server is negotiating TLS 1.2 or below and:
|
|
|
|
* . we did not propose TLS 1.2 or
|
|
|
|
* . the server responded it is TLS 1.3 capable but negotiating a lower
|
|
|
|
* version of the protocol and thus we are under downgrade attack
|
|
|
|
* abort the handshake with an "illegal parameter" alert.
|
2022-02-10 16:45:15 +01:00
|
|
|
*/
|
2022-04-05 11:04:20 +02:00
|
|
|
if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
|
2022-02-10 16:45:15 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
|
|
|
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->keep_current_message = 1;
|
2022-03-15 00:04:24 +01:00
|
|
|
ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
2022-02-10 16:45:15 +01:00
|
|
|
mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
|
|
|
|
*buf, *buf_len );
|
|
|
|
|
|
|
|
if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
|
|
|
|
{
|
|
|
|
ret = ssl_tls13_reset_key_share( ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
|
|
|
|
}
|
|
|
|
|
2022-04-05 11:04:20 +02:00
|
|
|
ret = ssl_server_hello_is_hrr( ssl, *buf, end );
|
2021-10-13 07:36:05 +02:00
|
|
|
switch( ret )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-13 16:01:04 +02:00
|
|
|
case SSL_SERVER_HELLO_COORDINATE_HELLO:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
|
|
|
|
break;
|
|
|
|
case SSL_SERVER_HELLO_COORDINATE_HRR:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
|
2022-01-14 08:35:47 +01:00
|
|
|
/* If a client receives a second
|
|
|
|
* HelloRetryRequest in the same connection (i.e., where the ClientHello
|
|
|
|
* was itself in response to a HelloRetryRequest), it MUST abort the
|
|
|
|
* handshake with an "unexpected_message" alert.
|
|
|
|
*/
|
2022-01-18 07:23:32 +01:00
|
|
|
if( ssl->handshake->hello_retry_request_count > 0 )
|
2022-01-14 08:35:47 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
|
|
|
|
MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
|
|
|
|
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
|
|
|
|
}
|
2022-01-21 03:53:13 +01:00
|
|
|
/*
|
|
|
|
* Clients must abort the handshake with an "illegal_parameter"
|
|
|
|
* alert if the HelloRetryRequest would not result in any change
|
|
|
|
* in the ClientHello.
|
|
|
|
* In a PSK only key exchange that what we expect.
|
|
|
|
*/
|
|
|
|
if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
|
|
|
( "Unexpected HRR in pure PSK key exchange." ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
|
|
|
}
|
2022-01-14 08:35:47 +01:00
|
|
|
|
2022-01-18 07:23:32 +01:00
|
|
|
ssl->handshake->hello_retry_request_count++;
|
2022-01-14 08:35:47 +01:00
|
|
|
|
2021-10-13 16:01:04 +02:00
|
|
|
break;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char **buf,
|
|
|
|
const unsigned char *end )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
const unsigned char *p = *buf;
|
2021-10-11 15:45:31 +02:00
|
|
|
size_t legacy_session_id_echo_len;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-09-19 12:05:08 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
|
2021-10-11 15:45:31 +02:00
|
|
|
legacy_session_id_echo_len = *p++ ;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
/* legacy_session_id_echo */
|
2021-10-11 15:45:31 +02:00
|
|
|
if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
|
|
|
|
memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
|
|
|
|
ssl->session_negotiate->id,
|
|
|
|
ssl->session_negotiate->id_len );
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
|
2021-10-11 15:45:31 +02:00
|
|
|
legacy_session_id_echo_len );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
p += legacy_session_id_echo_len;
|
2021-09-10 04:08:31 +02:00
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
|
2021-10-11 15:45:31 +02:00
|
|
|
ssl->session_negotiate->id_len );
|
2021-09-10 04:08:31 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse ServerHello message and configure context
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS 1.2
|
|
|
|
* Random random;
|
|
|
|
* opaque legacy_session_id_echo<0..32>;
|
|
|
|
* CipherSuite cipher_suite;
|
|
|
|
* uint8 legacy_compression_method = 0;
|
2021-10-13 07:36:05 +02:00
|
|
|
* Extension extensions<6..2^16-1>;
|
2021-09-10 04:08:31 +02:00
|
|
|
* } ServerHello;
|
|
|
|
*/
|
2021-10-11 16:30:19 +02:00
|
|
|
static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
2022-01-18 07:23:32 +01:00
|
|
|
const unsigned char *end,
|
|
|
|
int is_hrr )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-09-10 04:08:31 +02:00
|
|
|
const unsigned char *p = buf;
|
2022-01-20 12:14:50 +01:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2021-10-13 07:36:05 +02:00
|
|
|
size_t extensions_len;
|
|
|
|
const unsigned char *extensions_end;
|
2021-09-10 04:08:31 +02:00
|
|
|
uint16_t cipher_suite;
|
2021-09-19 12:05:08 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2022-01-26 04:29:10 +01:00
|
|
|
int fatal_alert = 0;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check there is space for minimal fields
|
|
|
|
*
|
|
|
|
* - legacy_version ( 2 bytes)
|
2021-10-26 04:44:32 +02:00
|
|
|
* - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
|
2021-09-10 04:08:31 +02:00
|
|
|
* - legacy_session_id_echo ( 1 byte ), minimum size
|
|
|
|
* - cipher_suite ( 2 bytes)
|
|
|
|
* - legacy_compression_method ( 1 byte )
|
|
|
|
*/
|
2021-10-26 04:44:32 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
2021-10-13 07:36:05 +02:00
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS 1.2
|
2021-10-11 15:45:31 +02:00
|
|
|
* ...
|
|
|
|
* with ProtocolVersion defined as:
|
|
|
|
* uint16 ProtocolVersion;
|
|
|
|
*/
|
2022-03-15 08:23:42 +01:00
|
|
|
if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_2 )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
|
|
|
|
MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
|
2022-01-24 11:12:51 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
|
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
p += 2;
|
|
|
|
|
2021-10-26 04:44:32 +02:00
|
|
|
/* ...
|
2021-10-11 15:45:31 +02:00
|
|
|
* Random random;
|
|
|
|
* ...
|
|
|
|
* with Random defined as:
|
2021-10-26 04:44:32 +02:00
|
|
|
* opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
|
2021-10-11 15:45:31 +02:00
|
|
|
*/
|
2022-01-18 11:47:33 +01:00
|
|
|
if( !is_hrr )
|
|
|
|
{
|
2022-01-20 12:14:50 +01:00
|
|
|
memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
|
2022-01-18 11:47:33 +01:00
|
|
|
MBEDTLS_SERVER_HELLO_RANDOM_LEN );
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
|
|
|
|
p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
|
|
|
|
}
|
2021-10-26 04:44:32 +02:00
|
|
|
p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
|
|
|
* opaque legacy_session_id_echo<0..32>;
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
|
|
|
* CipherSuite cipher_suite;
|
|
|
|
* ...
|
|
|
|
* with CipherSuite defined as:
|
|
|
|
* uint8 CipherSuite[2];
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
2021-09-10 04:08:31 +02:00
|
|
|
cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
p += 2;
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
|
2022-01-20 12:14:50 +01:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
|
2021-10-11 15:45:31 +02:00
|
|
|
/*
|
2022-03-30 22:09:48 +02:00
|
|
|
* Check whether this ciphersuite is valid and offered.
|
2021-10-11 15:45:31 +02:00
|
|
|
*/
|
2022-03-15 00:04:24 +01:00
|
|
|
if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
|
|
|
|
ssl->tls_version,
|
|
|
|
ssl->tls_version ) != 0 ) ||
|
2022-04-20 09:16:41 +02:00
|
|
|
!mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
2022-01-18 11:47:33 +01:00
|
|
|
/*
|
2022-01-20 12:14:50 +01:00
|
|
|
* If we received an HRR before and that the proposed selected
|
|
|
|
* ciphersuite in this server hello is not the same as the one
|
|
|
|
* proposed in the HRR, we abort the handshake and send an
|
|
|
|
* "illegal_parameter" alert.
|
2022-01-18 11:47:33 +01:00
|
|
|
*/
|
2022-01-20 12:14:50 +01:00
|
|
|
else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
|
|
|
|
( cipher_suite != ssl->session_negotiate->ciphersuite ) )
|
2022-01-18 11:47:33 +01:00
|
|
|
{
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
|
2022-01-20 12:14:50 +01:00
|
|
|
}
|
2022-01-18 11:47:33 +01:00
|
|
|
|
2022-01-26 10:49:29 +01:00
|
|
|
if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
|
2022-01-20 12:14:50 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
|
|
|
|
cipher_suite ) );
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2022-01-18 11:47:33 +01:00
|
|
|
}
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* Configure ciphersuites */
|
|
|
|
mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2022-01-20 12:14:50 +01:00
|
|
|
handshake->ciphersuite_info = ciphersuite_info;
|
2021-10-11 15:45:31 +02:00
|
|
|
ssl->session_negotiate->ciphersuite = cipher_suite;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
|
|
|
|
cipher_suite, ciphersuite_info->name ) );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
ssl->session_negotiate->start = time( NULL );
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
|
|
|
* uint8 legacy_compression_method = 0;
|
|
|
|
* ...
|
2021-09-10 04:08:31 +02:00
|
|
|
*/
|
2021-09-19 12:05:08 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
|
2021-09-10 04:08:31 +02:00
|
|
|
if( p[0] != 0 )
|
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
|
2021-10-13 07:36:05 +02:00
|
|
|
/* ...
|
|
|
|
* Extension extensions<6..2^16-1>;
|
|
|
|
* ...
|
2021-10-11 15:45:31 +02:00
|
|
|
* struct {
|
|
|
|
* ExtensionType extension_type; (2 bytes)
|
|
|
|
* opaque extension_data<0..2^16-1>;
|
|
|
|
* } Extension;
|
|
|
|
*/
|
2021-09-19 12:05:08 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
2021-10-11 15:45:31 +02:00
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
2021-09-10 04:08:31 +02:00
|
|
|
p += 2;
|
2021-09-19 12:05:08 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* Check extensions do not go beyond the buffer of data. */
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
|
|
|
|
extensions_end = p + extensions_len;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
while( p < extensions_end )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
2022-01-21 05:32:58 +01:00
|
|
|
const unsigned char *extension_data_end;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
|
2021-09-10 04:08:31 +02:00
|
|
|
extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
|
|
|
|
p += 4;
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
|
2022-01-21 05:32:58 +01:00
|
|
|
extension_data_end = p + extension_data_len;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
switch( extension_type )
|
|
|
|
{
|
2022-01-14 05:03:11 +01:00
|
|
|
case MBEDTLS_TLS_EXT_COOKIE:
|
|
|
|
|
2022-01-18 07:23:32 +01:00
|
|
|
if( !is_hrr )
|
|
|
|
{
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2022-01-18 07:23:32 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 05:32:58 +01:00
|
|
|
ret = ssl_tls13_parse_cookie_ext( ssl,
|
|
|
|
p, extension_data_end );
|
|
|
|
if( ret != 0 )
|
2022-01-14 05:03:11 +01:00
|
|
|
{
|
2022-01-21 05:32:58 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1,
|
|
|
|
"ssl_tls13_parse_cookie_ext",
|
|
|
|
ret );
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2022-01-14 05:03:11 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
|
2021-10-11 16:30:19 +02:00
|
|
|
ret = ssl_tls13_parse_supported_versions_ext( ssl,
|
2021-10-13 07:36:05 +02:00
|
|
|
p,
|
2022-01-21 05:32:58 +01:00
|
|
|
extension_data_end );
|
2021-09-10 04:08:31 +02:00
|
|
|
if( ret != 0 )
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
|
2021-10-11 15:45:31 +02:00
|
|
|
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_KEY_SHARE:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
|
2022-01-24 11:12:51 +01:00
|
|
|
if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
|
|
|
|
{
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2022-01-18 11:47:33 +01:00
|
|
|
if( is_hrr )
|
2022-01-18 07:23:32 +01:00
|
|
|
ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
|
2022-01-21 05:32:58 +01:00
|
|
|
p, extension_data_end );
|
2022-01-18 11:47:33 +01:00
|
|
|
else
|
2022-01-14 05:03:11 +01:00
|
|
|
ret = ssl_tls13_parse_key_share_ext( ssl,
|
2022-01-21 05:32:58 +01:00
|
|
|
p, extension_data_end );
|
2022-01-14 05:03:11 +01:00
|
|
|
if( ret != 0 )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1,
|
2021-10-11 15:45:31 +02:00
|
|
|
"ssl_tls13_parse_key_share_ext",
|
2021-09-10 04:08:31 +02:00
|
|
|
ret );
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-10-11 15:45:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
3,
|
|
|
|
( "unknown extension found: %u ( ignoring )",
|
|
|
|
extension_type ) );
|
|
|
|
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p += extension_data_len;
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:12:51 +01:00
|
|
|
cleanup:
|
|
|
|
|
2022-01-26 10:49:29 +01:00
|
|
|
if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
|
2022-01-24 11:12:51 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
|
|
|
|
MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
|
|
|
|
ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
|
|
|
|
}
|
2022-01-26 10:49:29 +01:00
|
|
|
else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
|
2022-01-24 11:12:51 +01:00
|
|
|
{
|
2022-01-18 11:47:33 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
2022-01-24 11:12:51 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-01-18 11:47:33 +01:00
|
|
|
}
|
2022-01-24 11:12:51 +01:00
|
|
|
return( ret );
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 12:14:50 +01:00
|
|
|
static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
|
|
|
|
|
|
|
/* Determine the key exchange mode:
|
|
|
|
* 1) If both the pre_shared_key and key_share extensions were received
|
|
|
|
* then the key exchange mode is PSK with EPHEMERAL.
|
|
|
|
* 2) If only the pre_shared_key extension was received then the key
|
|
|
|
* exchange mode is PSK-only.
|
|
|
|
* 3) If only the key_share extension was received then the key
|
|
|
|
* exchange mode is EPHEMERAL-only.
|
2021-09-05 13:41:30 +02:00
|
|
|
*/
|
2021-10-13 07:36:05 +02:00
|
|
|
switch( handshake->extensions_present &
|
|
|
|
( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
|
2021-09-05 13:41:30 +02:00
|
|
|
{
|
2021-10-13 16:01:04 +02:00
|
|
|
/* Only the pre_shared_key extension was received */
|
|
|
|
case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
|
2021-12-02 07:36:27 +01:00
|
|
|
handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
|
2021-10-13 16:01:04 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Only the key_share extension was received */
|
|
|
|
case MBEDTLS_SSL_EXT_KEY_SHARE:
|
2021-12-02 07:36:27 +01:00
|
|
|
handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
|
2021-10-13 16:01:04 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Both the pre_shared_key and key_share extensions were received */
|
|
|
|
case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
|
2021-12-02 07:36:27 +01:00
|
|
|
handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
|
2021-10-13 16:01:04 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Neither pre_shared_key nor key_share extension was received */
|
|
|
|
default:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
|
|
|
|
ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
|
|
|
goto cleanup;
|
2021-09-05 13:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
|
|
|
|
*
|
|
|
|
* TODO: We don't have to do this in case we offered 0-RTT and the
|
|
|
|
* server accepted it. In this case, we could skip generating
|
|
|
|
* the early secret. */
|
2021-11-12 09:53:56 +01:00
|
|
|
ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
|
2021-09-05 13:41:30 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2022-05-05 04:19:22 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
|
2021-09-05 13:41:30 +02:00
|
|
|
ret );
|
2021-10-11 15:45:31 +02:00
|
|
|
goto cleanup;
|
2021-09-05 13:41:30 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 05:16:55 +02:00
|
|
|
ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
|
2021-09-05 13:41:30 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2022-05-06 05:16:55 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1,
|
|
|
|
"mbedtls_ssl_tls13_compute_handshake_transform",
|
2021-10-11 16:30:19 +02:00
|
|
|
ret );
|
2021-10-11 15:45:31 +02:00
|
|
|
goto cleanup;
|
2021-09-05 13:41:30 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 04:19:22 +02:00
|
|
|
mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
|
2021-09-05 13:41:30 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
|
|
|
|
ssl->session_in = ssl->session_negotiate;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* State machine update
|
|
|
|
*/
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
cleanup:
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
}
|
2022-05-05 04:19:22 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
return( ret );
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 12:14:50 +01:00
|
|
|
static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
|
2021-12-07 10:16:29 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
/* If not offering early data, the client sends a dummy CCS record
|
|
|
|
* immediately before its second flight. This may either be before
|
2021-12-10 11:33:56 +01:00
|
|
|
* its second ClientHello or before its encrypted handshake flight.
|
|
|
|
*/
|
2021-12-07 10:16:29 +01:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl,
|
|
|
|
MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
|
|
|
|
#else
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
|
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
|
2022-01-19 07:56:30 +01:00
|
|
|
mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
|
2021-12-07 10:16:29 +01:00
|
|
|
|
2022-01-19 07:56:30 +01:00
|
|
|
/*
|
2022-01-20 12:14:50 +01:00
|
|
|
* We are going to re-generate a shared secret corresponding to the group
|
|
|
|
* selected by the server, which is different from the group for which we
|
|
|
|
* generated a shared secret in the first client hello.
|
|
|
|
* Thus, reset the shared secret.
|
2021-12-10 11:33:56 +01:00
|
|
|
*/
|
2022-01-14 08:35:47 +01:00
|
|
|
ret = ssl_tls13_reset_key_share( ssl );
|
2021-12-07 10:16:29 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
/*
|
2021-10-11 15:45:31 +02:00
|
|
|
* Wait and parse ServerHello handshake message.
|
2021-09-14 10:03:56 +02:00
|
|
|
* Handler for MBEDTLS_SSL_SERVER_HELLO
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-10-11 15:45:31 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-12-07 10:16:29 +01:00
|
|
|
unsigned char *buf = NULL;
|
|
|
|
size_t buf_len = 0;
|
2022-01-19 07:56:30 +01:00
|
|
|
int is_hrr = 0;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
|
|
|
|
|
|
|
|
/* Coordination step
|
|
|
|
* - Fetch record
|
|
|
|
* - Make sure it's either a ServerHello or a HRR.
|
|
|
|
* - Switch processing routine in case of HRR
|
|
|
|
*/
|
|
|
|
ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
|
|
|
|
|
2022-01-14 08:35:47 +01:00
|
|
|
ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
|
2022-01-18 07:23:32 +01:00
|
|
|
if( ret < 0 )
|
2022-01-14 08:35:47 +01:00
|
|
|
goto cleanup;
|
|
|
|
else
|
2022-01-18 07:23:32 +01:00
|
|
|
is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
|
2022-01-24 11:12:51 +01:00
|
|
|
|
2022-02-10 16:45:15 +01:00
|
|
|
if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2022-01-14 05:03:11 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
|
2022-01-18 07:23:32 +01:00
|
|
|
buf + buf_len,
|
|
|
|
is_hrr ) );
|
|
|
|
if( is_hrr )
|
2022-01-14 05:03:11 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2022-03-10 18:56:50 +01:00
|
|
|
mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
|
|
|
|
buf, buf_len );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2022-01-18 07:23:32 +01:00
|
|
|
if( is_hrr )
|
2022-01-20 12:14:50 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
|
2022-01-18 07:23:32 +01:00
|
|
|
else
|
2022-01-20 12:14:50 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
cleanup:
|
2022-01-27 04:48:27 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
|
|
|
|
is_hrr?"HelloRetryRequest":"ServerHello" ) );
|
2021-09-10 04:08:31 +02:00
|
|
|
return( ret );
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-09-13 09:30:09 +02:00
|
|
|
*
|
|
|
|
* EncryptedExtensions message
|
|
|
|
*
|
|
|
|
* The EncryptedExtensions message contains any extensions which
|
|
|
|
* should be protected, i.e., any which are not needed to establish
|
|
|
|
* the cryptographic context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Overview
|
2021-09-14 10:03:56 +02:00
|
|
|
*/
|
2021-09-13 09:30:09 +02:00
|
|
|
|
|
|
|
/* Main entry point; orchestrates the other functions */
|
2021-10-11 12:05:54 +02:00
|
|
|
static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-11 12:05:54 +02:00
|
|
|
static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end );
|
|
|
|
static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
|
2021-09-13 09:30:09 +02:00
|
|
|
|
|
|
|
/*
|
2021-09-16 05:02:14 +02:00
|
|
|
* Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
|
2021-09-13 09:30:09 +02:00
|
|
|
*/
|
2021-10-11 12:05:54 +02:00
|
|
|
static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
|
2021-09-13 09:30:09 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
|
2021-10-21 08:23:29 +02:00
|
|
|
MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
2021-09-13 09:30:09 +02:00
|
|
|
&buf, &buf_len ) );
|
|
|
|
|
|
|
|
/* Process the message contents */
|
2021-10-11 12:05:54 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(
|
2021-10-13 07:56:18 +02:00
|
|
|
ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2022-03-10 18:56:50 +01:00
|
|
|
mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
|
|
|
buf, buf_len );
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-11 12:05:54 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
|
2021-09-13 09:30:09 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-09 12:12:11 +02:00
|
|
|
/* Parse EncryptedExtensions message
|
|
|
|
* struct {
|
2021-10-11 12:05:54 +02:00
|
|
|
* Extension extensions<0..2^16-1>;
|
2021-10-09 12:12:11 +02:00
|
|
|
* } EncryptedExtensions;
|
|
|
|
*/
|
2021-10-11 12:05:54 +02:00
|
|
|
static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
2021-09-13 09:30:09 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2021-10-09 12:12:11 +02:00
|
|
|
size_t extensions_len;
|
2021-10-08 10:05:53 +02:00
|
|
|
const unsigned char *p = buf;
|
2021-10-13 07:56:18 +02:00
|
|
|
const unsigned char *extensions_end;
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-09 12:12:11 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
2021-10-11 12:05:54 +02:00
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
2021-10-09 12:12:11 +02:00
|
|
|
p += 2;
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-11 12:05:54 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
|
2021-10-13 07:56:18 +02:00
|
|
|
extensions_end = p + extensions_len;
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-13 07:56:18 +02:00
|
|
|
while( p < extensions_end )
|
2021-09-13 09:30:09 +02:00
|
|
|
{
|
2021-10-09 12:12:11 +02:00
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-09 12:12:11 +02:00
|
|
|
/*
|
|
|
|
* struct {
|
2021-10-11 12:05:54 +02:00
|
|
|
* ExtensionType extension_type; (2 bytes)
|
|
|
|
* opaque extension_data<0..2^16-1>;
|
2021-10-09 12:12:11 +02:00
|
|
|
* } Extension;
|
|
|
|
*/
|
2021-10-13 07:56:18 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
|
2021-10-09 12:12:11 +02:00
|
|
|
extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
|
|
|
|
p += 4;
|
|
|
|
|
2021-10-13 07:56:18 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-11 12:05:54 +02:00
|
|
|
/* The client MUST check EncryptedExtensions for the
|
2021-09-13 09:30:09 +02:00
|
|
|
* presence of any forbidden extensions and if any are found MUST abort
|
2021-10-09 12:12:11 +02:00
|
|
|
* the handshake with an "unsupported_extension" alert.
|
2021-09-13 09:30:09 +02:00
|
|
|
*/
|
2021-10-09 12:12:11 +02:00
|
|
|
switch( extension_type )
|
|
|
|
{
|
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
|
|
|
|
break;
|
|
|
|
|
2022-01-21 16:34:27 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
case MBEDTLS_TLS_EXT_ALPN:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
|
|
|
|
|
|
|
|
if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
2021-10-09 12:12:11 +02:00
|
|
|
default:
|
2021-10-11 12:05:54 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
3, ( "unsupported extension found: %u ", extension_type) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
2021-10-28 15:41:30 +02:00
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
|
2021-10-11 12:05:54 +02:00
|
|
|
MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
|
|
|
|
return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
|
2021-10-09 12:12:11 +02:00
|
|
|
}
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-09 12:12:11 +02:00
|
|
|
p += extension_data_len;
|
2021-10-11 12:05:54 +02:00
|
|
|
}
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-11 12:05:54 +02:00
|
|
|
/* Check that we consumed all the message. */
|
2021-10-13 12:19:02 +02:00
|
|
|
if( p != end )
|
2021-10-11 12:05:54 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
|
2021-10-28 15:41:30 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
2021-10-13 12:19:02 +02:00
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR );
|
2021-10-11 12:05:54 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_DECODE_ERROR );
|
2021-09-13 09:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2021-10-11 12:05:54 +02:00
|
|
|
static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-10-27 10:31:48 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2021-11-12 09:53:56 +01:00
|
|
|
if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
|
2021-10-27 10:31:48 +02:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
|
|
|
|
else
|
2021-10-29 04:08:19 +02:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
|
2021-10-27 10:31:48 +02:00
|
|
|
#else
|
|
|
|
((void) ssl);
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
|
|
|
|
#endif
|
2021-09-14 10:03:56 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:31:48 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2021-10-29 04:08:19 +02:00
|
|
|
/*
|
2022-01-16 13:14:45 +01:00
|
|
|
*
|
|
|
|
* STATE HANDLING: CertificateRequest
|
|
|
|
*
|
2021-10-29 04:08:19 +02:00
|
|
|
*/
|
2022-01-20 09:25:00 +01:00
|
|
|
#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
|
|
|
|
#define SSL_CERTIFICATE_REQUEST_SKIP 1
|
2022-01-16 13:14:45 +01:00
|
|
|
/* Coordination:
|
|
|
|
* Deals with the ambiguity of not knowing if a CertificateRequest
|
|
|
|
* will be sent. Returns a negative code on failure, or
|
|
|
|
* - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
|
|
|
|
* - SSL_CERTIFICATE_REQUEST_SKIP
|
|
|
|
* indicating if a Certificate Request is expected or not.
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
|
2021-10-29 04:08:19 +02:00
|
|
|
{
|
2022-01-18 06:47:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-10-29 04:08:19 +02:00
|
|
|
|
2022-02-08 16:21:13 +01:00
|
|
|
if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
|
2022-01-16 13:14:45 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
|
|
|
|
return( SSL_CERTIFICATE_REQUEST_SKIP );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
|
2021-10-29 04:08:19 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
2022-01-16 13:14:45 +01:00
|
|
|
ssl->keep_current_message = 1;
|
2021-10-29 04:08:19 +02:00
|
|
|
|
|
|
|
if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
|
|
|
|
( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
|
|
|
|
{
|
2022-01-16 13:14:45 +01:00
|
|
|
return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
|
2021-10-29 04:08:19 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 13:14:45 +01:00
|
|
|
return( SSL_CERTIFICATE_REQUEST_SKIP );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ssl_tls13_parse_certificate_request()
|
|
|
|
* Parse certificate request
|
|
|
|
* struct {
|
|
|
|
* opaque certificate_request_context<0..2^8-1>;
|
|
|
|
* Extension extensions<2..2^16-1>;
|
|
|
|
* } CertificateRequest;
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
size_t certificate_request_context_len = 0;
|
2022-01-26 10:21:54 +01:00
|
|
|
size_t extensions_len = 0;
|
|
|
|
const unsigned char *extensions_end;
|
|
|
|
unsigned char sig_alg_ext_found = 0;
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2022-01-26 10:21:54 +01:00
|
|
|
/* ...
|
|
|
|
* opaque certificate_request_context<0..2^8-1>
|
|
|
|
* ...
|
|
|
|
*/
|
2022-01-16 13:14:45 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
|
|
|
|
certificate_request_context_len = (size_t) p[0];
|
2022-01-20 09:25:00 +01:00
|
|
|
p += 1;
|
2022-01-16 13:14:45 +01:00
|
|
|
|
|
|
|
if( certificate_request_context_len > 0 )
|
|
|
|
{
|
2022-02-08 10:59:23 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
|
2022-01-16 13:14:45 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
|
|
|
|
p, certificate_request_context_len );
|
|
|
|
|
2022-01-26 10:21:54 +01:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-01-20 09:25:00 +01:00
|
|
|
handshake->certificate_request_context =
|
2022-01-28 09:52:13 +01:00
|
|
|
mbedtls_calloc( 1, certificate_request_context_len );
|
2022-01-20 09:25:00 +01:00
|
|
|
if( handshake->certificate_request_context == NULL )
|
2022-01-16 13:14:45 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
|
|
|
|
return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
}
|
2022-01-20 09:25:00 +01:00
|
|
|
memcpy( handshake->certificate_request_context, p,
|
2022-01-16 13:14:45 +01:00
|
|
|
certificate_request_context_len );
|
|
|
|
p += certificate_request_context_len;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:21:54 +01:00
|
|
|
/* ...
|
|
|
|
* Extension extensions<2..2^16-1>;
|
|
|
|
* ...
|
2022-01-16 13:14:45 +01:00
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
p += 2;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
|
|
|
|
extensions_end = p + extensions_len;
|
|
|
|
|
|
|
|
while( p < extensions_end )
|
|
|
|
{
|
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
|
|
|
|
extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
|
|
|
|
p += 4;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
|
2021-10-29 04:08:19 +02:00
|
|
|
|
2022-01-16 13:14:45 +01:00
|
|
|
switch( extension_type )
|
|
|
|
{
|
|
|
|
case MBEDTLS_TLS_EXT_SIG_ALG:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3,
|
2022-01-20 09:25:00 +01:00
|
|
|
( "found signature algorithms extension" ) );
|
2022-04-27 21:17:56 +02:00
|
|
|
ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
|
2022-05-13 16:27:29 +02:00
|
|
|
p + extension_data_len );
|
2022-01-16 13:14:45 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2022-01-26 10:21:54 +01:00
|
|
|
if( ! sig_alg_ext_found )
|
|
|
|
sig_alg_ext_found = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3,
|
|
|
|
( "Duplicate signature algorithms extensions found" ) );
|
2022-02-08 10:59:23 +01:00
|
|
|
goto decode_error;
|
2022-01-26 10:21:54 +01:00
|
|
|
}
|
2022-01-16 13:14:45 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
3,
|
|
|
|
( "unknown extension found: %u ( ignoring )",
|
|
|
|
extension_type ) );
|
2022-01-26 10:21:54 +01:00
|
|
|
break;
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
|
|
|
p += extension_data_len;
|
|
|
|
}
|
2022-01-20 09:25:00 +01:00
|
|
|
/* Check that we consumed all the message. */
|
|
|
|
if( p != end )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
2022-02-08 10:59:23 +01:00
|
|
|
( "CertificateRequest misaligned" ) );
|
|
|
|
goto decode_error;
|
2022-01-20 09:25:00 +01:00
|
|
|
}
|
|
|
|
/* Check that we found signature algorithms extension */
|
2022-01-26 10:21:54 +01:00
|
|
|
if( ! sig_alg_ext_found )
|
2022-01-20 09:25:00 +01:00
|
|
|
{
|
2022-01-28 09:52:13 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3,
|
|
|
|
( "no signature algorithms extension found" ) );
|
2022-02-08 10:59:23 +01:00
|
|
|
goto decode_error;
|
2022-01-20 09:25:00 +01:00
|
|
|
}
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2022-01-29 03:26:51 +01:00
|
|
|
ssl->handshake->client_auth = 1;
|
2021-10-29 04:08:19 +02:00
|
|
|
return( 0 );
|
2022-02-08 08:28:04 +01:00
|
|
|
|
2022-02-08 10:59:23 +01:00
|
|
|
decode_error:
|
2022-02-08 08:28:04 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_DECODE_ERROR );
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 06:47:05 +01:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
|
2022-01-16 13:14:45 +01:00
|
|
|
{
|
2022-01-18 06:47:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-01-16 13:14:45 +01:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
|
|
|
|
|
|
|
|
if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
|
|
|
|
{
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
|
|
|
|
MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
|
|
|
&buf, &buf_len ) );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
|
|
|
|
buf, buf + buf_len ) );
|
|
|
|
|
2022-03-10 18:56:50 +01:00
|
|
|
mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
|
|
|
buf, buf_len );
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
2022-01-20 09:25:00 +01:00
|
|
|
else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
|
2022-01-16 13:14:45 +01:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
|
2022-01-16 15:54:35 +01:00
|
|
|
ret = 0;
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2022-02-08 08:28:04 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto cleanup;
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
|
2022-01-29 03:26:51 +01:00
|
|
|
ssl->handshake->client_auth ? "a" : "no" ) );
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2022-01-18 06:47:05 +01:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
|
|
|
|
|
2022-01-16 13:14:45 +01:00
|
|
|
cleanup:
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
|
|
|
|
return( ret );
|
2021-10-29 04:08:19 +02:00
|
|
|
}
|
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-09-29 11:12:03 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_process_certificate( ssl );
|
2021-10-29 04:39:30 +02:00
|
|
|
if( ret != 0 )
|
2021-09-29 11:12:03 +02:00
|
|
|
return( ret );
|
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-09-12 14:16:03 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
|
|
|
|
return( 0 );
|
|
|
|
}
|
2021-10-27 10:31:48 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2021-12-06 09:06:46 +01:00
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_SERVER_FINISHED
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-11-03 07:40:11 +01:00
|
|
|
int ret;
|
|
|
|
|
2021-11-09 12:55:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_process_finished_message( ssl );
|
2021-11-03 07:40:11 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2022-05-19 09:33:10 +02:00
|
|
|
ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2021-11-24 16:25:31 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
mbedtls_ssl_handshake_set_state(
|
|
|
|
ssl,
|
|
|
|
MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
|
|
|
|
#else
|
2022-02-15 07:22:05 +01:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
|
2022-01-26 08:41:22 +01:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
2021-11-24 16:25:31 +01:00
|
|
|
|
2021-11-03 07:40:11 +01:00
|
|
|
return( 0 );
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 08:41:22 +01:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
2022-03-09 07:44:27 +01:00
|
|
|
int non_empty_certificate_msg = 0;
|
|
|
|
|
2022-01-28 09:16:08 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
|
|
|
( "Switch to handshake traffic keys for outbound traffic" ) );
|
2022-01-26 08:41:22 +01:00
|
|
|
mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2022-03-08 18:38:54 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2022-03-09 07:00:13 +01:00
|
|
|
if( ssl->handshake->client_auth )
|
2022-03-09 07:44:27 +01:00
|
|
|
{
|
|
|
|
int ret = mbedtls_ssl_tls13_write_certificate( ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2022-03-09 07:00:13 +01:00
|
|
|
|
2022-03-09 07:44:27 +01:00
|
|
|
if( mbedtls_ssl_own_cert( ssl ) != NULL )
|
|
|
|
non_empty_certificate_msg = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
|
|
|
|
}
|
2022-03-08 18:38:54 +01:00
|
|
|
#endif
|
2022-03-09 07:00:13 +01:00
|
|
|
|
2022-03-09 07:44:27 +01:00
|
|
|
if( non_empty_certificate_msg )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl,
|
|
|
|
MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
|
|
|
|
|
2022-03-09 07:00:13 +01:00
|
|
|
return( 0 );
|
2022-01-26 08:41:22 +01:00
|
|
|
}
|
|
|
|
|
2022-03-08 18:38:54 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2022-01-26 08:41:22 +01:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
2022-03-09 07:59:25 +01:00
|
|
|
int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
|
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
|
|
|
|
|
|
|
|
return( ret );
|
2022-01-26 08:41:22 +01:00
|
|
|
}
|
2022-01-29 15:12:42 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2022-01-26 08:41:22 +01:00
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CLIENT_FINISHED
|
|
|
|
*/
|
2021-09-22 09:40:30 +02:00
|
|
|
static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-11-15 04:33:57 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_write_finished_message( ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2022-05-19 09:33:10 +02:00
|
|
|
ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1,
|
|
|
|
"mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
|
|
|
|
return ( ret );
|
|
|
|
}
|
|
|
|
|
2021-11-15 04:33:57 +01:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
|
|
|
|
return( 0 );
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_FLUSH_BUFFERS
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-10-30 15:44:47 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
|
2021-09-28 11:58:26 +02:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
|
2021-09-14 10:03:56 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-10-30 15:44:47 +02:00
|
|
|
|
|
|
|
mbedtls_ssl_tls13_handshake_wrapup( ssl );
|
|
|
|
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
|
|
|
|
return( 0 );
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
|
2021-08-24 09:59:48 +02:00
|
|
|
{
|
2021-08-27 10:59:09 +02:00
|
|
|
int ret = 0;
|
2021-08-18 10:46:28 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
switch( ssl->state )
|
|
|
|
{
|
|
|
|
/*
|
2021-09-02 06:59:12 +02:00
|
|
|
* ssl->state is initialized as HELLO_REQUEST. It is the same
|
|
|
|
* as CLIENT_HELLO state.
|
2021-08-27 10:59:09 +02:00
|
|
|
*/
|
|
|
|
case MBEDTLS_SSL_HELLO_REQUEST:
|
|
|
|
case MBEDTLS_SSL_CLIENT_HELLO:
|
2022-02-18 17:24:56 +01:00
|
|
|
ret = mbedtls_ssl_write_client_hello( ssl );
|
2021-08-27 10:59:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_SERVER_HELLO:
|
2021-11-12 09:53:56 +01:00
|
|
|
ret = ssl_tls13_process_server_hello( ssl );
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
|
2021-10-11 12:05:54 +02:00
|
|
|
ret = ssl_tls13_process_encrypted_extensions( ssl );
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
2021-10-27 10:31:48 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2021-10-29 04:08:19 +02:00
|
|
|
case MBEDTLS_SSL_CERTIFICATE_REQUEST:
|
|
|
|
ret = ssl_tls13_process_certificate_request( ssl );
|
|
|
|
break;
|
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
case MBEDTLS_SSL_SERVER_CERTIFICATE:
|
2021-11-12 09:53:56 +01:00
|
|
|
ret = ssl_tls13_process_server_certificate( ssl );
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_CERTIFICATE_VERIFY:
|
2021-11-12 09:53:56 +01:00
|
|
|
ret = ssl_tls13_process_certificate_verify( ssl );
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
2021-10-27 10:31:48 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2021-09-14 10:03:56 +02:00
|
|
|
|
|
|
|
case MBEDTLS_SSL_SERVER_FINISHED:
|
2021-11-12 09:53:56 +01:00
|
|
|
ret = ssl_tls13_process_server_finished( ssl );
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
2022-01-26 08:41:22 +01:00
|
|
|
case MBEDTLS_SSL_CLIENT_CERTIFICATE:
|
|
|
|
ret = ssl_tls13_write_client_certificate( ssl );
|
|
|
|
break;
|
|
|
|
|
2022-03-08 18:38:54 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2022-01-26 08:41:22 +01:00
|
|
|
case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
|
|
|
|
ret = ssl_tls13_write_client_certificate_verify( ssl );
|
|
|
|
break;
|
2022-01-29 15:12:42 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2022-01-26 08:41:22 +01:00
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
case MBEDTLS_SSL_CLIENT_FINISHED:
|
2021-09-22 09:40:30 +02:00
|
|
|
ret = ssl_tls13_write_client_finished( ssl );
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_FLUSH_BUFFERS:
|
2021-11-12 09:53:56 +01:00
|
|
|
ret = ssl_tls13_flush_buffers( ssl );
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
|
2021-11-12 09:53:56 +01:00
|
|
|
ret = ssl_tls13_handshake_wrapup( ssl );
|
2021-08-27 10:59:09 +02:00
|
|
|
break;
|
|
|
|
|
2021-11-24 16:25:31 +01:00
|
|
|
/*
|
|
|
|
* Injection of dummy-CCS's for middlebox compatibility
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
2021-12-22 03:39:32 +01:00
|
|
|
case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
|
2022-02-02 16:02:47 +01:00
|
|
|
ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
|
|
|
|
if( ret == 0 )
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
|
|
|
|
ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
|
|
|
|
if( ret == 0 )
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
|
2021-11-24 16:25:31 +01:00
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
default:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2022-01-27 08:03:26 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
2021-08-06 10:29:08 +02:00
|
|
|
|