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"
|
|
|
|
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2021-08-06 10:29:08 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
|
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"
|
|
|
|
#include "ecdh_misc.h"
|
2021-09-10 04:08:31 +02:00
|
|
|
#include "ssl_tls13_keys.h"
|
2021-12-15 12:56:54 +01:00
|
|
|
#include "ssl_debug_helpers.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;
|
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 )
|
|
|
|
* - versions (2 bytes)
|
2021-08-31 06:51:25 +02:00
|
|
|
*/
|
2021-08-27 10:59:09 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write extension_type */
|
2021-08-30 12:32:07 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
|
2021-08-06 10:29:08 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write extension_data_length */
|
2021-08-31 10:16:19 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE( 3, 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 */
|
2021-08-27 10:59:09 +02:00
|
|
|
*p++ = 0x2;
|
2021-08-27 09:14:01 +02:00
|
|
|
|
2021-09-02 06:59:12 +02:00
|
|
|
/* Write values of supported versions.
|
2021-09-01 06:57:29 +02:00
|
|
|
*
|
2021-09-02 06:59:12 +02:00
|
|
|
* They are defined by the configuration.
|
2021-09-01 06:57:29 +02:00
|
|
|
*
|
2021-09-02 06:59:12 +02:00
|
|
|
* Currently, only one version is advertised.
|
2021-08-27 10:59:09 +02:00
|
|
|
*/
|
2021-08-30 12:32:07 +02:00
|
|
|
mbedtls_ssl_write_version( ssl->conf->max_major_ver,
|
|
|
|
ssl->conf->max_minor_ver,
|
|
|
|
ssl->conf->transport, p );
|
2021-08-18 10:46:28 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
|
2021-08-30 12:32:07 +02:00
|
|
|
ssl->conf->max_major_ver,
|
|
|
|
ssl->conf->max_minor_ver ) );
|
2021-08-18 10:46:28 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 7;
|
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);
|
|
|
|
|
2021-10-13 07:36:05 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
|
|
|
|
if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
|
2021-09-10 04:08:31 +02:00
|
|
|
buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-08-24 09:59:48 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
|
|
|
|
2021-09-01 11:05:13 +02:00
|
|
|
/*
|
|
|
|
* Functions for writing supported_groups extension.
|
|
|
|
*
|
|
|
|
* Stucture of supported_groups:
|
|
|
|
* enum {
|
|
|
|
* secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
|
|
|
|
* x25519(0x001D), x448(0x001E),
|
|
|
|
* ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
|
|
|
|
* ffdhe6144(0x0103), ffdhe8192(0x0104),
|
|
|
|
* ffdhe_private_use(0x01FC..0x01FF),
|
|
|
|
* ecdhe_private_use(0xFE00..0xFEFF),
|
|
|
|
* (0xFFFF)
|
|
|
|
* } NamedGroup;
|
|
|
|
* struct {
|
|
|
|
* NamedGroup named_group_list<2..2^16-1>;
|
|
|
|
* } NamedGroupList;
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
|
|
|
/*
|
|
|
|
* In versions of TLS prior to TLS 1.3, this extension was named
|
|
|
|
* 'elliptic_curves' and only contained elliptic curve groups.
|
|
|
|
*/
|
2021-09-08 10:41:02 +02:00
|
|
|
static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl,
|
2021-10-06 10:32:11 +02:00
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
2021-12-02 07:36:27 +01:00
|
|
|
size_t *out_len )
|
2021-09-01 11:05:13 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0;
|
2021-09-01 11:05:13 +02:00
|
|
|
|
2021-10-06 10:32:11 +02:00
|
|
|
const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
|
|
|
|
|
|
|
|
if( group_list == NULL )
|
2021-09-08 11:55:09 +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:05:13 +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-09-01 11:05:13 +02:00
|
|
|
continue;
|
2021-09-08 11:55:09 +02:00
|
|
|
|
2021-10-06 10:32:11 +02:00
|
|
|
if( !mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
|
2021-09-01 11:05:13 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2);
|
2021-10-06 10:32:11 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
|
2021-09-01 11:05:13 +02:00
|
|
|
p += 2;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
|
2021-11-18 08:29:56 +01:00
|
|
|
curve_info->name, *group_list ) );
|
2021-09-01 11:05:13 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = p - buf;
|
2021-09-01 11:05:13 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2021-09-08 10:41:02 +02:00
|
|
|
static int ssl_tls13_write_named_group_list_ecdhe( 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:05:13 +02:00
|
|
|
{
|
|
|
|
((void) ssl);
|
|
|
|
((void) buf);
|
|
|
|
((void) end);
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0;
|
2021-09-01 11:05:13 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECDH_C */
|
|
|
|
|
2021-09-08 10:41:02 +02:00
|
|
|
static int ssl_tls13_write_named_group_list_dhe( 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:05:13 +02:00
|
|
|
{
|
|
|
|
((void) ssl);
|
|
|
|
((void) buf);
|
|
|
|
((void) end);
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0;
|
2021-09-01 11:05:13 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "write_named_group_dhe is not implemented" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
|
|
|
|
}
|
|
|
|
|
2021-08-26 16:59:56 +02:00
|
|
|
static int ssl_tls13_write_supported_groups_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-27 10:59:09 +02:00
|
|
|
{
|
2021-09-01 11:05:13 +02:00
|
|
|
unsigned char *p = buf ;
|
2021-11-18 08:29:56 +01:00
|
|
|
unsigned char *named_group_list; /* Start of named_group_list */
|
|
|
|
size_t named_group_list_len; /* Length of named_group_list */
|
2021-09-08 10:41:02 +02:00
|
|
|
size_t output_len = 0;
|
|
|
|
int ret_ecdhe, ret_dhe;
|
2021-09-01 11:05:13 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0;
|
2021-09-01 11:05:13 +02:00
|
|
|
|
|
|
|
if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
|
|
|
|
|
2021-09-08 10:41:02 +02:00
|
|
|
/* Check if we have space for header and length fields:
|
2021-11-18 08:29:56 +01:00
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_length (2 bytes)
|
2021-09-08 10:41:02 +02:00
|
|
|
* - named_group_list_length (2 bytes)
|
|
|
|
*/
|
2021-09-01 11:05:13 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
|
|
|
|
p += 6;
|
|
|
|
|
2021-11-18 08:29:56 +01:00
|
|
|
named_group_list = p;
|
2021-09-08 10:41:02 +02:00
|
|
|
ret_ecdhe = ssl_tls13_write_named_group_list_ecdhe( ssl, p, end, &output_len );
|
2021-09-01 11:05:13 +02:00
|
|
|
if( ret_ecdhe != 0 )
|
|
|
|
{
|
2021-09-08 10:41:02 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_ecdhe", ret_ecdhe );
|
2021-09-01 11:05:13 +02:00
|
|
|
}
|
2021-09-08 10:41:02 +02:00
|
|
|
p += output_len;
|
2021-09-01 11:05:13 +02:00
|
|
|
|
2021-09-08 10:41:02 +02:00
|
|
|
ret_dhe = ssl_tls13_write_named_group_list_dhe( ssl, p, end, &output_len );
|
2021-09-01 11:05:13 +02:00
|
|
|
if( ret_dhe != 0 )
|
|
|
|
{
|
2021-09-08 10:41:02 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_dhe", ret_dhe );
|
2021-09-01 11:05:13 +02:00
|
|
|
}
|
2021-09-08 10:41:02 +02:00
|
|
|
p += output_len;
|
2021-09-01 11:05:13 +02:00
|
|
|
|
2021-09-15 12:41:02 +02:00
|
|
|
/* Both ECDHE and DHE failed. */
|
2021-09-01 11:05:13 +02:00
|
|
|
if( ret_ecdhe != 0 && ret_dhe != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Both ECDHE and DHE groups are fail. " ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Length of named_group_list*/
|
2021-11-18 08:29:56 +01:00
|
|
|
named_group_list_len = p - named_group_list;
|
2021-09-08 10:41:02 +02:00
|
|
|
if( named_group_list_len == 0 )
|
2021-09-01 11:05:13 +02:00
|
|
|
{
|
2021-09-15 12:41:02 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
|
2021-09-01 11:05:13 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write extension_type */
|
|
|
|
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
|
|
|
|
/* Write extension_data_length */
|
2021-09-08 10:41:02 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
|
2021-09-01 11:05:13 +02:00
|
|
|
/* Write length of named_group_list */
|
2021-09-08 10:41:02 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
|
2021-09-01 11:05:13 +02:00
|
|
|
|
2021-09-08 10:41:02 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", buf + 4, named_group_list_len + 2 );
|
2021-09-01 11:05:13 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = p - buf;
|
2021-09-01 11:05:13 +02:00
|
|
|
|
|
|
|
ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
|
|
|
|
|
2021-09-08 10:41:02 +02:00
|
|
|
return( 0 );
|
2021-08-27 10:59:09 +02:00
|
|
|
}
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 11:48:49 +02:00
|
|
|
/*
|
|
|
|
* Functions for writing key_share extension.
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
2021-09-08 11:55:09 +02:00
|
|
|
static int ssl_tls13_generate_and_write_ecdh_key_exchange(
|
2021-09-08 10:41:02 +02:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
uint16_t named_group,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
2021-12-02 07:36:27 +01:00
|
|
|
size_t *out_len )
|
2021-08-27 10:59:09 +02:00
|
|
|
{
|
2021-09-01 11:48:49 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
const mbedtls_ecp_curve_info *curve_info =
|
|
|
|
mbedtls_ecp_curve_info_from_tls_id( named_group );
|
|
|
|
|
|
|
|
if( curve_info == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
|
|
|
|
|
2021-09-15 05:10:15 +02:00
|
|
|
if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
|
|
|
|
curve_info->grp_id ) ) != 0 )
|
2021-09-01 11:48:49 +02:00
|
|
|
{
|
2021-09-15 12:41:02 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
|
2021-09-01 11:48:49 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
|
|
|
|
buf, end - buf,
|
|
|
|
ssl->conf->f_rng, ssl->conf->p_rng );
|
2021-09-01 11:48:49 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
|
|
|
|
MBEDTLS_DEBUG_ECDH_Q );
|
2021-09-01 09:59:36 +02:00
|
|
|
return( 0 );
|
2021-08-27 10:59:09 +02:00
|
|
|
}
|
2021-09-01 11:48:49 +02:00
|
|
|
#endif /* MBEDTLS_ECDH_C */
|
2021-08-24 09:59:48 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
|
|
|
|
return( 0 );
|
|
|
|
|
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 */
|
|
|
|
size_t key_exchange_len;
|
|
|
|
|
|
|
|
/* 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;
|
2021-09-08 10:41:02 +02:00
|
|
|
ret = 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
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
|
|
|
|
2021-10-11 16:30:19 +02:00
|
|
|
static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
|
|
mbedtls_ecp_group_id grp_id;
|
|
|
|
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
|
|
|
grp_id = ssl->handshake->ecdh_ctx.grp.id;
|
|
|
|
#else
|
|
|
|
grp_id = ssl->handshake->ecdh_ctx.grp_id;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
|
|
|
|
if( curve_info == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
|
|
|
|
|
|
|
|
if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
|
2021-10-13 07:36:05 +02:00
|
|
|
return( -1 );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
|
|
|
|
MBEDTLS_DEBUG_ECDH_QP );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:30:19 +02:00
|
|
|
static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t buf_len )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2021-10-11 16:30:19 +02:00
|
|
|
ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
|
2021-10-11 15:45:31 +02:00
|
|
|
buf, buf_len );
|
2021-09-10 04:08:31 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
|
2021-10-13 07:36:05 +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
|
|
|
}
|
|
|
|
|
2021-10-11 16:30:19 +02:00
|
|
|
if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-11 16:30:19 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
|
2021-10-13 07:36:05 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2021-10-11 15:45:31 +02:00
|
|
|
#endif /* MBEDTLS_ECDH_C */
|
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
|
|
|
{
|
|
|
|
/* Complete ECDHE key agreement */
|
2021-10-11 16:30:19 +02:00
|
|
|
ret = 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 );
|
|
|
|
}
|
|
|
|
|
2021-09-01 11:48:49 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write cipher_suites
|
2021-08-31 08:40:36 +02:00
|
|
|
* CipherSuite cipher_suites<2..2^16-2>;
|
|
|
|
*/
|
2021-09-01 06:57:29 +02:00
|
|
|
static int ssl_tls13_write_client_hello_cipher_suites(
|
2021-08-31 08:40:36 +02:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
2021-12-02 07:36:27 +01:00
|
|
|
size_t *out_len )
|
2021-08-31 08:40:36 +02:00
|
|
|
{
|
2021-09-07 11:26:06 +02:00
|
|
|
unsigned char *p = buf;
|
2021-09-02 06:59:12 +02:00
|
|
|
const int *ciphersuite_list;
|
2021-11-18 08:29:56 +01:00
|
|
|
unsigned char *cipher_suites; /* Start of the cipher_suites list */
|
2021-09-01 06:57:29 +02:00
|
|
|
size_t cipher_suites_len;
|
2021-08-31 08:40:36 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0 ;
|
2021-08-27 10:59:09 +02:00
|
|
|
|
2021-08-31 08:40:36 +02:00
|
|
|
/*
|
|
|
|
* Ciphersuite list
|
|
|
|
*
|
|
|
|
* This is a list of the symmetric cipher options supported by
|
|
|
|
* the client, specifically the record protection algorithm
|
|
|
|
* ( including secret key length ) and a hash to be used with
|
|
|
|
* HKDF, in descending order of client preference.
|
|
|
|
*/
|
2021-09-02 06:59:12 +02:00
|
|
|
ciphersuite_list = ssl->conf->ciphersuite_list;
|
2021-08-31 08:40:36 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Check there is space for the cipher suite list length (2 bytes). */
|
2021-09-06 15:28:08 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
|
|
|
|
p += 2;
|
2021-08-31 08:40:36 +02:00
|
|
|
|
2021-09-02 06:59:12 +02:00
|
|
|
/* Write cipher_suites */
|
2021-11-18 08:29:56 +01:00
|
|
|
cipher_suites = p;
|
2021-09-02 06:59:12 +02:00
|
|
|
for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
|
2021-08-31 08:40:36 +02:00
|
|
|
{
|
2021-09-02 06:59:12 +02:00
|
|
|
int cipher_suite = ciphersuite_list[i];
|
2021-09-01 06:57:29 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2021-08-31 08:40:36 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
|
2021-08-31 08:40:36 +02:00
|
|
|
if( ciphersuite_info == NULL )
|
|
|
|
continue;
|
2021-09-04 03:58:58 +02:00
|
|
|
if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
|
2021-08-31 08:40:36 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
|
2021-09-01 06:57:29 +02:00
|
|
|
(unsigned int) cipher_suite,
|
2021-08-31 08:40:36 +02:00
|
|
|
ciphersuite_info->name ) );
|
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Check there is space for the cipher suite identifier (2 bytes). */
|
2021-09-06 15:17:54 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
|
|
|
|
MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
|
|
|
|
p += 2;
|
2021-08-31 08:40:36 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 06:59:12 +02:00
|
|
|
/* Write the cipher_suites length in number of bytes */
|
2021-11-18 08:29:56 +01:00
|
|
|
cipher_suites_len = p - cipher_suites;
|
2021-09-01 06:57:29 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
|
2021-08-31 08:40:36 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3,
|
2021-09-01 06:57:29 +02:00
|
|
|
( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
|
|
|
|
cipher_suites_len/2 ) );
|
2021-08-31 08:40:36 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Output the total length of cipher_suites field. */
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = p - buf;
|
2021-08-31 12:31:09 +02:00
|
|
|
|
2021-08-31 08:40:36 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/*
|
|
|
|
* Structure of ClientHello message:
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS v1.2
|
|
|
|
* Random random;
|
|
|
|
* opaque legacy_session_id<0..32>;
|
|
|
|
* CipherSuite cipher_suites<2..2^16-2>;
|
|
|
|
* opaque legacy_compression_methods<1..2^8-1>;
|
|
|
|
* Extension extensions<8..2^16-1>;
|
|
|
|
* } ClientHello;
|
|
|
|
*/
|
2021-08-31 05:05:27 +02:00
|
|
|
static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
|
2021-08-30 12:32:07 +02:00
|
|
|
unsigned char *buf,
|
2021-09-02 07:59:41 +02:00
|
|
|
unsigned char *end,
|
2021-12-02 07:36:27 +01:00
|
|
|
size_t *out_len )
|
2021-08-24 09:59:48 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
int ret;
|
2021-11-18 08:29:56 +01:00
|
|
|
unsigned char *p_extensions_len; /* Pointer to extensions length */
|
|
|
|
size_t output_len; /* Length of buffer used by function */
|
|
|
|
size_t extensions_len; /* Length of the list of extensions*/
|
2021-08-24 09:59:48 +02:00
|
|
|
|
|
|
|
/* Buffer management */
|
2021-09-06 15:17:54 +02:00
|
|
|
unsigned char *p = buf;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* No validation needed here. It has been done by ssl_conf_check() */
|
2021-08-24 09:59:48 +02:00
|
|
|
ssl->major_ver = ssl->conf->min_major_ver;
|
|
|
|
ssl->minor_ver = ssl->conf->min_minor_ver;
|
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/*
|
|
|
|
* Write legacy_version
|
2021-08-31 08:40:36 +02:00
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS v1.2
|
2021-08-24 09:59:48 +02:00
|
|
|
*
|
2021-09-01 06:57:29 +02:00
|
|
|
* For TLS 1.3 we use the legacy version number {0x03, 0x03}
|
|
|
|
* instead of the true version number.
|
2021-08-24 09:59:48 +02:00
|
|
|
*/
|
2021-09-07 11:26:06 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
|
2021-09-06 15:17:54 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
|
2021-09-07 11:26:06 +02:00
|
|
|
p += 2;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write the random bytes ( random ).*/
|
2021-10-26 04:44:32 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
|
|
|
|
memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
|
2021-08-26 11:32:34 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
|
2021-10-26 04:44:32 +02:00
|
|
|
p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
|
|
|
|
p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/*
|
|
|
|
* Write legacy_session_id
|
|
|
|
*
|
|
|
|
* Versions of TLS before TLS 1.3 supported a "session resumption" feature
|
|
|
|
* which has been merged with pre-shared keys in this version. A client
|
|
|
|
* which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
|
|
|
|
* this field to that value. In compatibility mode, this field MUST be
|
|
|
|
* non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
|
|
|
|
* a new 32-byte value. This value need not be random but SHOULD be
|
|
|
|
* unpredictable to avoid implementations fixating on a specific value
|
|
|
|
* ( also known as ossification ). Otherwise, it MUST be set as a zero-length
|
|
|
|
* vector ( i.e., a zero-valued single byte length field ).
|
2021-08-24 09:59:48 +02:00
|
|
|
*/
|
2021-11-24 16:25:31 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
|
|
|
|
*p++ = (unsigned char)ssl->session_negotiate->id_len;
|
|
|
|
memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
|
|
|
|
p += ssl->session_negotiate->id_len;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
|
|
|
|
ssl->session_negotiate->id_len );
|
|
|
|
#else
|
2021-09-06 15:17:54 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
|
|
|
|
*p++ = 0; /* session id length set to zero */
|
2021-11-24 16:25:31 +01:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write cipher_suites */
|
2021-09-06 15:17:54 +02:00
|
|
|
ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
|
2021-09-04 03:58:58 +02:00
|
|
|
if( ret != 0 )
|
2021-08-31 08:40:36 +02:00
|
|
|
return( ret );
|
2021-09-06 15:17:54 +02:00
|
|
|
p += output_len;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write legacy_compression_methods
|
|
|
|
*
|
|
|
|
* For every TLS 1.3 ClientHello, this vector MUST contain exactly
|
2021-08-24 09:59:48 +02:00
|
|
|
* one byte set to zero, which corresponds to the 'null' compression
|
|
|
|
* method in prior versions of TLS.
|
|
|
|
*/
|
2021-09-06 15:17:54 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
|
|
|
|
*p++ = 1;
|
|
|
|
*p++ = MBEDTLS_SSL_COMPRESS_NULL;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write extensions */
|
|
|
|
|
|
|
|
/* Keeping track of the included extensions */
|
|
|
|
ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
|
|
|
/* First write extensions, then the total length */
|
2021-09-06 15:17:54 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
|
2021-11-18 08:29:56 +01:00
|
|
|
p_extensions_len = p;
|
2021-09-06 15:17:54 +02:00
|
|
|
p += 2;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write supported_versions extension
|
2021-08-24 09:59:48 +02:00
|
|
|
*
|
2021-09-01 06:57:29 +02:00
|
|
|
* Supported Versions Extension is mandatory with TLS 1.3.
|
2021-08-24 09:59:48 +02:00
|
|
|
*/
|
2021-09-06 15:17:54 +02:00
|
|
|
ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
|
2021-08-27 10:59:09 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2021-09-06 15:17:54 +02:00
|
|
|
p += output_len;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write supported_groups extension
|
|
|
|
*
|
|
|
|
* It is REQUIRED for ECDHE cipher_suites.
|
2021-08-24 09:59:48 +02:00
|
|
|
*/
|
2021-09-06 15:17:54 +02:00
|
|
|
ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len );
|
2021-08-24 09:59:48 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2021-09-06 15:17:54 +02:00
|
|
|
p += output_len;
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write key_share extension
|
|
|
|
*
|
|
|
|
* We need to send the key shares under three conditions:
|
2021-08-31 06:51:25 +02:00
|
|
|
* 1) A certificate-based ciphersuite is being offered. In this case
|
|
|
|
* supported_groups and supported_signature extensions have been
|
|
|
|
* successfully added.
|
|
|
|
* 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
|
2021-08-24 09:59:48 +02:00
|
|
|
* psk_key_exchange_modes has been added as the last extension.
|
2021-08-31 06:51:25 +02:00
|
|
|
* 3) Or, in case all ciphers are supported ( which includes #1 and #2
|
|
|
|
* from above )
|
2021-08-24 09:59:48 +02:00
|
|
|
*/
|
2021-09-01 11:48:49 +02:00
|
|
|
ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
|
2021-08-24 09:59:48 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2021-09-06 15:17:54 +02:00
|
|
|
p += output_len;
|
2021-08-31 08:40:36 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write signature_algorithms extension
|
|
|
|
*
|
|
|
|
* It is REQUIRED for certificate authenticated cipher_suites.
|
|
|
|
*/
|
2021-09-06 15:17:54 +02:00
|
|
|
ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len );
|
2021-09-01 06:57:29 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2021-09-06 15:17:54 +02:00
|
|
|
p += output_len;
|
2021-09-01 06:57:29 +02:00
|
|
|
|
2021-08-24 09:59:48 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
|
|
|
|
2021-11-05 11:52:12 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2021-11-09 04:10:05 +01:00
|
|
|
/* Write server name extension */
|
2021-11-05 11:52:12 +01:00
|
|
|
ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
p += output_len;
|
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
|
|
|
|
2021-08-24 09:59:48 +02:00
|
|
|
/* Add more extensions here */
|
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Write the length of the list of extensions. */
|
2021-11-18 08:29:56 +01:00
|
|
|
extensions_len = p - p_extensions_len - 2;
|
|
|
|
MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
|
2021-08-24 09:59:48 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
|
2021-09-01 09:51:48 +02:00
|
|
|
extensions_len ) );
|
2021-11-18 08:29:56 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = p - buf;
|
2021-08-24 09:59:48 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-09-12 14:18:56 +02:00
|
|
|
static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
|
2021-08-18 10:38:40 +02:00
|
|
|
{
|
2021-08-27 10:59:09 +02:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
|
|
|
|
return( 0 );
|
|
|
|
}
|
2021-08-24 10:29:02 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
2021-08-24 10:29:02 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
if( ssl->conf->f_rng == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_NO_RNG );
|
|
|
|
}
|
2021-08-24 10:29:02 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
|
|
|
|
ssl->handshake->randbytes,
|
2021-10-26 04:44:32 +02:00
|
|
|
MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
|
2021-08-27 10:59:09 +02:00
|
|
|
{
|
2021-09-03 15:09:22 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
|
2021-08-27 10:59:09 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2021-08-24 10:29:02 +02:00
|
|
|
|
2021-11-24 16:25:31 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
/*
|
|
|
|
* Create a session identifier for the purpose of middlebox compatibility
|
|
|
|
* only if one has not been created already.
|
|
|
|
*/
|
|
|
|
if( ssl->session_negotiate->id_len == 0 )
|
|
|
|
{
|
|
|
|
/* Creating a session id with 32 byte length */
|
|
|
|
if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
|
|
|
|
ssl->session_negotiate->id, 32 ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
ssl->session_negotiate->id_len = 32;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
2021-08-24 10:29:02 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
/*
|
2021-08-31 06:51:25 +02:00
|
|
|
* Write ClientHello handshake message.
|
2021-09-14 10:03:56 +02:00
|
|
|
* Handler for MBEDTLS_SSL_CLIENT_HELLO
|
2021-08-27 10:59:09 +02:00
|
|
|
*/
|
|
|
|
static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
2021-08-26 12:38:58 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
|
2021-08-24 10:29:02 +02:00
|
|
|
|
2021-09-02 07:53:46 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
|
2021-08-24 10:29:02 +02:00
|
|
|
|
2021-09-02 07:53:46 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
|
|
|
|
&buf, &buf_len ) );
|
2021-08-24 10:29:02 +02:00
|
|
|
|
2021-09-02 07:53:46 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
|
2021-09-02 07:59:41 +02:00
|
|
|
buf + buf_len,
|
2021-09-02 07:53:46 +02:00
|
|
|
&msg_len ) );
|
2021-08-24 10:29:02 +02:00
|
|
|
|
2021-09-02 07:53:46 +02:00
|
|
|
mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
|
|
|
|
MBEDTLS_SSL_HS_CLIENT_HELLO,
|
2021-09-02 06:59:12 +02:00
|
|
|
msg_len );
|
|
|
|
ssl->handshake->update_checksum( ssl, buf, msg_len );
|
2021-08-26 11:18:15 +02:00
|
|
|
|
2021-09-02 07:53:46 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
|
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
|
|
|
|
buf_len,
|
|
|
|
msg_len ) );
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
cleanup:
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
|
|
|
|
return ret;
|
2021-08-18 10:38:40 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
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
|
|
|
|
* to indicate which message is expected and to be parsed next. */
|
|
|
|
#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
|
|
|
{
|
2021-10-26 04:44:32 +02:00
|
|
|
static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
|
2021-09-10 04:08:31 +02:00
|
|
|
{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
|
|
|
|
0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
|
|
|
|
0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
|
|
|
|
0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
*
|
|
|
|
*/
|
2021-10-13 07:36:05 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
|
2021-10-11 15:45:31 +02:00
|
|
|
|
|
|
|
if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 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
|
|
|
|
* - SSL_SERVER_HELLO_COORDINATE_HRR
|
|
|
|
*/
|
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;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
|
|
|
|
|
|
|
|
if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
|
|
|
|
( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
|
|
|
|
MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
|
|
|
|
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
|
|
|
|
}
|
|
|
|
|
|
|
|
*buf = ssl->in_msg + 4;
|
|
|
|
*buf_len = ssl->in_hslen - 4;
|
|
|
|
|
2021-10-13 07:36:05 +02:00
|
|
|
ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
|
|
|
|
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" ) );
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
|
|
|
|
int cipher_suite )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-11 15:45:31 +02:00
|
|
|
const int *ciphersuite_list = ssl->conf->ciphersuite_list;
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
/* Check whether we have offered this ciphersuite */
|
2021-10-11 15:45:31 +02:00
|
|
|
for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-11 15:45:31 +02:00
|
|
|
if( ciphersuite_list[i] == cipher_suite )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
const unsigned char *end )
|
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;
|
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;
|
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;
|
|
|
|
*/
|
2021-09-10 04:08:31 +02:00
|
|
|
if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
|
|
|
|
p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
|
|
|
|
}
|
|
|
|
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
|
|
|
*/
|
2021-10-26 04:44:32 +02:00
|
|
|
memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
|
|
|
|
MBEDTLS_SERVER_HELLO_RANDOM_LEN );
|
2021-09-10 04:08:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
|
2021-10-26 04:44:32 +02:00
|
|
|
p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
|
|
|
|
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
|
|
|
{
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
|
|
|
return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether this ciphersuite is supported and offered.
|
|
|
|
* Via the force_ciphersuite version we may have instructed the client
|
|
|
|
* to use a different ciphersuite.
|
|
|
|
*/
|
2021-09-10 04:08:31 +02:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
|
2021-10-11 15:45:31 +02:00
|
|
|
if( ciphersuite_info == NULL ||
|
|
|
|
ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
|
2021-09-10 04:08:31 +02:00
|
|
|
cipher_suite ) );
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
ssl->handshake->ciphersuite_info = ciphersuite_info;
|
|
|
|
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" ) );
|
2021-09-10 04:08: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 );
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
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 );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
switch( extension_type )
|
|
|
|
{
|
|
|
|
case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3,
|
|
|
|
( "found supported_versions extension" ) );
|
|
|
|
|
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,
|
|
|
|
p + extension_data_len );
|
2021-09-10 04:08:31 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
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
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
|
|
|
|
MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
|
|
|
|
return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
|
|
|
case MBEDTLS_TLS_EXT_KEY_SHARE:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
|
2021-10-11 15:45:31 +02:00
|
|
|
if( ( ret = ssl_tls13_parse_key_share_ext( ssl,
|
2021-09-10 04:08:31 +02:00
|
|
|
p, p + extension_data_len ) ) != 0 )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
|
|
|
|
|
|
|
default:
|
2021-10-11 15:45:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
3,
|
|
|
|
( "unknown extension found: %u ( ignoring )",
|
|
|
|
extension_type ) );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
|
|
|
|
MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
|
|
|
|
return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p += extension_data_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:30:19 +02:00
|
|
|
static int ssl_tls13_finalize_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;
|
2021-09-05 13:41:30 +02:00
|
|
|
mbedtls_ssl_key_set traffic_keys;
|
2021-10-11 15:45:31 +02:00
|
|
|
mbedtls_ssl_transform *transform_handshake = NULL;
|
2021-10-13 07:36:05 +02:00
|
|
|
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 )
|
|
|
|
{
|
2021-11-12 09:53:56 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute handshake secret */
|
2021-10-11 11:47:07 +02:00
|
|
|
ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
|
2021-09-05 13:41:30 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2021-11-12 09:53:56 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
|
2021-10-11 15:45:31 +02:00
|
|
|
goto cleanup;
|
2021-09-05 13:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Next evolution in key schedule: Establish handshake secret and
|
|
|
|
* key material. */
|
2021-10-11 16:30:19 +02:00
|
|
|
ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
|
2021-09-05 13:41:30 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2021-10-11 16:30:19 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
|
|
|
|
ret );
|
2021-10-11 15:45:31 +02:00
|
|
|
goto cleanup;
|
2021-09-05 13:41:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 07:36:05 +02:00
|
|
|
transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
|
2021-09-05 13:41:30 +02:00
|
|
|
if( transform_handshake == NULL )
|
2021-10-11 15:45:31 +02:00
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2021-09-05 13:41:30 +02:00
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
|
|
|
|
ssl->conf->endpoint,
|
|
|
|
ssl->session_negotiate->ciphersuite,
|
|
|
|
&traffic_keys,
|
|
|
|
ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
|
2021-10-11 15:45:31 +02:00
|
|
|
goto cleanup;
|
2021-09-05 13:41:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 07:36:05 +02:00
|
|
|
handshake->transform_handshake = transform_handshake;
|
|
|
|
mbedtls_ssl_set_inbound_transform( ssl, 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:
|
|
|
|
|
2021-09-05 13:41:30 +02:00
|
|
|
mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
|
2021-10-11 15:45:31 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
mbedtls_free( transform_handshake );
|
2021-10-11 16:30:19 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
}
|
|
|
|
return( ret );
|
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-09-10 04:08:31 +02:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
|
|
|
|
|
|
|
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->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
|
|
|
|
ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
|
|
|
|
|
2021-10-13 07:36:05 +02:00
|
|
|
ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
|
2021-09-10 04:08:31 +02:00
|
|
|
/* Parsing step
|
|
|
|
* We know what message to expect by now and call
|
|
|
|
* the respective parsing function.
|
|
|
|
*/
|
|
|
|
if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
|
|
|
|
{
|
2021-10-11 16:30:19 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
|
|
|
|
buf + buf_len ) );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
|
|
|
|
MBEDTLS_SSL_HS_SERVER_HELLO,
|
|
|
|
buf, buf_len );
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 16:30:19 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
|
|
|
|
{
|
2021-10-11 15:45:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "HRR not supported" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
|
|
|
|
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
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
mbedtls_ssl_tls13_add_hs_msg_to_checksum(
|
2021-10-21 08:23:29 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = mbedtls_ssl_read_record( ssl, 0 );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
|
|
|
|
( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->keep_current_message = 1;
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
|
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
|
2021-11-03 07:40:11 +01:00
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
|
2021-11-24 16:25:31 +01:00
|
|
|
#endif
|
|
|
|
|
2021-11-03 07:40:11 +01:00
|
|
|
return( 0 );
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
2021-12-06 09:06:46 +01:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
|
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;
|
|
|
|
|
2021-11-24 16:25:31 +01:00
|
|
|
mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
|
|
|
|
|
2021-11-15 04:33:57 +01:00
|
|
|
ret = mbedtls_ssl_tls13_write_finished_message( ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
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_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
|
|
|
|
mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
|
|
|
|
mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
|
|
|
|
|
|
|
|
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-09-28 11:53:35 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
|
|
|
|
mbedtls_ssl_states_str( ssl->state ),
|
|
|
|
ssl->state ) );
|
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:
|
|
|
|
ret = ssl_tls13_write_client_hello( ssl );
|
|
|
|
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;
|
|
|
|
|
|
|
|
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)
|
|
|
|
case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
|
2021-12-06 09:06:46 +01:00
|
|
|
ret = ssl_tls13_write_change_cipher_spec( ssl );
|
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
|
|
|
|
2021-08-06 10:29:08 +02:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|