2021-08-18 08:38:40 +00:00
|
|
|
/*
|
|
|
|
* TLS 1.3 functionality shared between client and server
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2022-01-27 07:03:26 +00:00
|
|
|
#if defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2021-08-18 08:38:40 +00:00
|
|
|
|
2021-09-12 12:16:03 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-08-18 08:46:28 +00:00
|
|
|
#include "mbedtls/error.h"
|
2021-09-01 07:59:36 +00:00
|
|
|
#include "mbedtls/debug.h"
|
2021-09-12 12:16:03 +00:00
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
#include "mbedtls/platform.h"
|
2021-11-24 10:17:36 +00:00
|
|
|
#include "mbedtls/constant_time.h"
|
2021-09-22 07:40:30 +00:00
|
|
|
#include <string.h>
|
2021-08-18 08:46:28 +00:00
|
|
|
|
2021-08-18 08:38:40 +00:00
|
|
|
#include "ssl_misc.h"
|
2021-09-12 12:16:03 +00:00
|
|
|
#include "ssl_tls13_keys.h"
|
2021-08-18 08:38:40 +00:00
|
|
|
|
2021-11-12 08:53:56 +00:00
|
|
|
int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned hs_type,
|
|
|
|
unsigned char **buf,
|
2021-11-18 07:29:56 +00:00
|
|
|
size_t *buf_len )
|
2021-09-24 07:51:16 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2021-09-27 09:30:17 +00:00
|
|
|
if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
|
2021-09-24 07:51:16 +00:00
|
|
|
ssl->in_msg[0] != hs_type )
|
|
|
|
{
|
2021-09-27 09:30:17 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
|
2021-09-24 07:51:16 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
|
2021-09-29 08:46:37 +00:00
|
|
|
MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
|
2021-09-24 07:51:16 +00:00
|
|
|
ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2021-09-29 08:46:37 +00:00
|
|
|
/*
|
|
|
|
* Jump handshake header (4 bytes, see Section 4 of RFC 8446).
|
|
|
|
* ...
|
|
|
|
* HandshakeType msg_type;
|
|
|
|
* uint24 length;
|
|
|
|
* ...
|
|
|
|
*/
|
2021-11-18 07:29:56 +00:00
|
|
|
*buf = ssl->in_msg + 4;
|
|
|
|
*buf_len = ssl->in_hslen - 4;
|
2021-09-24 07:51:16 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:59:56 +00:00
|
|
|
int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
|
2021-08-30 10:32:07 +00:00
|
|
|
unsigned hs_type,
|
|
|
|
unsigned char **buf,
|
2021-09-02 04:59:12 +00:00
|
|
|
size_t *buf_len )
|
2021-08-18 08:38:40 +00:00
|
|
|
{
|
2021-09-01 04:57:29 +00:00
|
|
|
/*
|
|
|
|
* Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 )
|
|
|
|
* ...
|
|
|
|
* HandshakeType msg_type;
|
|
|
|
* uint24 length;
|
|
|
|
* ...
|
|
|
|
*/
|
2021-08-18 08:46:28 +00:00
|
|
|
*buf = ssl->out_msg + 4;
|
2021-09-02 04:59:12 +00:00
|
|
|
*buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
|
2021-08-18 08:46:28 +00:00
|
|
|
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = hs_type;
|
|
|
|
|
|
|
|
return( 0 );
|
2021-08-18 08:38:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 14:59:56 +00:00
|
|
|
int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
|
2021-08-30 10:32:07 +00:00
|
|
|
size_t buf_len,
|
|
|
|
size_t msg_len )
|
2021-08-18 08:38:40 +00:00
|
|
|
{
|
2021-08-18 08:46:28 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-11-18 07:29:56 +00:00
|
|
|
size_t msg_with_header_len;
|
2021-08-18 08:38:40 +00:00
|
|
|
((void) buf_len);
|
2021-08-18 08:46:28 +00:00
|
|
|
|
2021-09-01 04:57:29 +00:00
|
|
|
/* Add reserved 4 bytes for handshake header */
|
2021-11-18 07:29:56 +00:00
|
|
|
msg_with_header_len = msg_len + 4;
|
|
|
|
ssl->out_msglen = msg_with_header_len;
|
2022-02-02 14:33:46 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0, 0 ) );
|
2021-08-18 08:46:28 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
2021-08-18 08:38:40 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 08:53:56 +00:00
|
|
|
void mbedtls_ssl_tls13_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned hs_type,
|
|
|
|
unsigned char const *msg,
|
|
|
|
size_t msg_len )
|
2021-09-09 07:06:18 +00:00
|
|
|
{
|
|
|
|
mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
|
|
|
|
ssl->handshake->update_checksum( ssl, msg, msg_len );
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:59:56 +00:00
|
|
|
void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
|
2021-08-30 10:32:07 +00:00
|
|
|
unsigned hs_type,
|
|
|
|
size_t total_hs_len )
|
2021-08-18 08:38:40 +00:00
|
|
|
{
|
|
|
|
unsigned char hs_hdr[4];
|
|
|
|
|
|
|
|
/* Build HS header for checksum update. */
|
2021-08-26 10:38:58 +00:00
|
|
|
hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
|
|
|
|
hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
|
|
|
|
hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
|
|
|
|
hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
|
2021-08-18 08:38:40 +00:00
|
|
|
|
|
|
|
ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
|
|
|
|
}
|
|
|
|
|
2021-08-24 07:59:48 +00:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2022-01-20 08:25:00 +00:00
|
|
|
/* mbedtls_ssl_tls13_parse_sig_alg_ext()
|
|
|
|
*
|
|
|
|
* enum {
|
|
|
|
* ....
|
|
|
|
* ecdsa_secp256r1_sha256( 0x0403 ),
|
|
|
|
* ecdsa_secp384r1_sha384( 0x0503 ),
|
|
|
|
* ecdsa_secp521r1_sha512( 0x0603 ),
|
|
|
|
* ....
|
|
|
|
* } SignatureScheme;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* SignatureScheme supported_signature_algorithms<2..2^16-2>;
|
|
|
|
* } SignatureSchemeList;
|
|
|
|
*/
|
|
|
|
int mbedtls_ssl_tls13_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
2022-01-28 06:37:15 +00:00
|
|
|
size_t supported_sig_algs_len = 0;
|
|
|
|
const unsigned char *supported_sig_algs_end;
|
2022-01-26 09:21:54 +00:00
|
|
|
uint16_t sig_alg;
|
2022-02-08 09:59:23 +00:00
|
|
|
uint32_t common_idx = 0;
|
2022-01-20 08:25:00 +00:00
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
2022-01-28 06:37:15 +00:00
|
|
|
supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
2022-01-20 08:25:00 +00:00
|
|
|
p += 2;
|
|
|
|
|
2022-01-28 06:37:15 +00:00
|
|
|
memset( ssl->handshake->received_sig_algs, 0,
|
2022-02-08 07:28:04 +00:00
|
|
|
sizeof(ssl->handshake->received_sig_algs) );
|
2022-01-20 08:25:00 +00:00
|
|
|
|
2022-01-28 06:37:15 +00:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
|
|
|
|
supported_sig_algs_end = p + supported_sig_algs_len;
|
2022-02-08 07:28:04 +00:00
|
|
|
while( p < supported_sig_algs_end )
|
2022-01-20 08:25:00 +00:00
|
|
|
{
|
2022-01-28 06:37:15 +00:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
|
2022-01-26 09:21:54 +00:00
|
|
|
sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
|
2022-01-20 08:25:00 +00:00
|
|
|
p += 2;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
|
2022-01-26 09:21:54 +00:00
|
|
|
sig_alg ) );
|
2022-01-20 08:25:00 +00:00
|
|
|
|
2022-02-08 07:28:04 +00:00
|
|
|
if( ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ||
|
|
|
|
! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
|
2022-01-20 08:25:00 +00:00
|
|
|
{
|
2022-01-28 06:37:15 +00:00
|
|
|
ssl->handshake->received_sig_algs[common_idx] = sig_alg;
|
2022-01-26 09:21:54 +00:00
|
|
|
common_idx += 1;
|
2022-01-20 08:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Check that we consumed all the message. */
|
|
|
|
if( p != end )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
|
|
|
( "Signature algorithms extension length misaligned" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( common_idx == 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
}
|
|
|
|
|
2022-01-28 06:37:15 +00:00
|
|
|
ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS1_3_SIG_NONE;
|
2022-01-20 08:25:00 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-10-28 05:41:59 +00:00
|
|
|
/*
|
|
|
|
* STATE HANDLING: Read CertificateVerify
|
|
|
|
*/
|
2021-10-29 03:09:06 +00:00
|
|
|
/* Macro to express the maximum length of the verify structure.
|
2021-10-28 05:41:59 +00:00
|
|
|
*
|
|
|
|
* The structure is computed per TLS 1.3 specification as:
|
|
|
|
* - 64 bytes of octet 32,
|
|
|
|
* - 33 bytes for the context string
|
|
|
|
* (which is either "TLS 1.3, client CertificateVerify"
|
|
|
|
* or "TLS 1.3, server CertificateVerify"),
|
2021-10-29 03:09:06 +00:00
|
|
|
* - 1 byte for the octet 0x0, which serves as a separator,
|
2021-10-28 05:41:59 +00:00
|
|
|
* - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
|
|
|
|
* (depending on the size of the transcript_hash)
|
|
|
|
*
|
|
|
|
* This results in a total size of
|
|
|
|
* - 130 bytes for a SHA256-based transcript hash, or
|
|
|
|
* (64 + 33 + 1 + 32 bytes)
|
|
|
|
* - 146 bytes for a SHA384-based transcript hash.
|
|
|
|
* (64 + 33 + 1 + 48 bytes)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
|
|
|
|
33 + \
|
|
|
|
1 + \
|
|
|
|
MBEDTLS_TLS1_3_MD_MAX_SIZE \
|
|
|
|
)
|
|
|
|
|
2021-09-12 12:16:03 +00:00
|
|
|
/*
|
|
|
|
* The ssl_tls13_create_verify_structure() creates the verify structure.
|
|
|
|
* As input, it requires the transcript hash.
|
|
|
|
*
|
|
|
|
* The caller has to ensure that the buffer has size at least
|
|
|
|
* SSL_VERIFY_STRUCT_MAX_SIZE bytes.
|
|
|
|
*/
|
2021-10-29 03:09:06 +00:00
|
|
|
static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
|
2021-09-12 12:16:03 +00:00
|
|
|
size_t transcript_hash_len,
|
|
|
|
unsigned char *verify_buffer,
|
|
|
|
size_t *verify_buffer_len,
|
|
|
|
int from )
|
|
|
|
{
|
2021-10-25 04:42:58 +00:00
|
|
|
size_t idx;
|
2021-09-12 12:16:03 +00:00
|
|
|
|
|
|
|
/* RFC 8446, Section 4.4.3:
|
|
|
|
*
|
|
|
|
* The digital signature [in the CertificateVerify message] is then
|
|
|
|
* computed over the concatenation of:
|
|
|
|
* - A string that consists of octet 32 (0x20) repeated 64 times
|
|
|
|
* - The context string
|
|
|
|
* - A single 0 byte which serves as the separator
|
|
|
|
* - The content to be signed
|
|
|
|
*/
|
2021-10-28 05:41:59 +00:00
|
|
|
memset( verify_buffer, 0x20, 64 );
|
|
|
|
idx = 64;
|
2021-09-12 12:16:03 +00:00
|
|
|
|
|
|
|
if( from == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
{
|
|
|
|
memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
|
|
|
|
idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* from == MBEDTLS_SSL_IS_SERVER */
|
|
|
|
memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
|
|
|
|
idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
|
|
|
|
}
|
|
|
|
|
|
|
|
verify_buffer[idx++] = 0x0;
|
|
|
|
|
|
|
|
memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
|
|
|
|
idx += transcript_hash_len;
|
|
|
|
|
|
|
|
*verify_buffer_len = idx;
|
|
|
|
}
|
|
|
|
|
2021-10-28 05:41:59 +00:00
|
|
|
static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
|
2021-10-29 03:09:06 +00:00
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end,
|
|
|
|
const unsigned char *verify_buffer,
|
|
|
|
size_t verify_buffer_len )
|
2021-09-12 12:16:03 +00:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
uint16_t algorithm;
|
|
|
|
size_t signature_len;
|
|
|
|
mbedtls_pk_type_t sig_alg;
|
|
|
|
mbedtls_md_type_t md_alg;
|
2021-10-29 03:09:06 +00:00
|
|
|
unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
|
2021-09-12 12:16:03 +00:00
|
|
|
size_t verify_hash_len;
|
|
|
|
|
2021-12-02 06:36:27 +00:00
|
|
|
void const *options = NULL;
|
2021-11-03 08:51:56 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2021-12-02 06:36:27 +00:00
|
|
|
mbedtls_pk_rsassa_pss_options rsassa_pss_options;
|
2021-11-03 08:51:56 +00:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
|
|
|
|
2021-09-12 12:16:03 +00:00
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* SignatureScheme algorithm;
|
|
|
|
* opaque signature<0..2^16-1>;
|
|
|
|
* } CertificateVerify;
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
|
|
|
algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
p += 2;
|
|
|
|
|
|
|
|
/* RFC 8446 section 4.4.3
|
|
|
|
*
|
|
|
|
* If the CertificateVerify message is sent by a server, the signature algorithm
|
|
|
|
* MUST be one offered in the client's "signature_algorithms" extension unless
|
|
|
|
* no valid certificate chain can be produced without unsupported algorithms
|
|
|
|
*
|
|
|
|
* RFC 8446 section 4.4.2.2
|
|
|
|
*
|
|
|
|
* If the client cannot construct an acceptable chain using the provided
|
|
|
|
* certificates and decides to abort the handshake, then it MUST abort the handshake
|
|
|
|
* with an appropriate certificate-related alert (by default, "unsupported_certificate").
|
|
|
|
*
|
2021-10-29 12:12:51 +00:00
|
|
|
* Check if algorithm is an offered signature algorithm.
|
2021-09-12 12:16:03 +00:00
|
|
|
*/
|
2022-01-19 10:02:15 +00:00
|
|
|
if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
|
2021-09-12 12:16:03 +00:00
|
|
|
{
|
2021-10-14 07:59:37 +00:00
|
|
|
/* algorithm not in offered signature algorithms list */
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
|
|
|
|
"offered.",
|
|
|
|
( unsigned int ) algorithm ) );
|
2021-10-29 12:12:51 +00:00
|
|
|
goto error;
|
2021-09-12 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We currently only support ECDSA-based signatures */
|
|
|
|
switch( algorithm )
|
|
|
|
{
|
2021-11-12 08:53:56 +00:00
|
|
|
case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
|
2021-09-12 12:16:03 +00:00
|
|
|
md_alg = MBEDTLS_MD_SHA256;
|
|
|
|
sig_alg = MBEDTLS_PK_ECDSA;
|
|
|
|
break;
|
2021-11-12 08:53:56 +00:00
|
|
|
case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
|
2021-09-12 12:16:03 +00:00
|
|
|
md_alg = MBEDTLS_MD_SHA384;
|
|
|
|
sig_alg = MBEDTLS_PK_ECDSA;
|
|
|
|
break;
|
2021-11-12 08:53:56 +00:00
|
|
|
case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
|
2021-09-12 12:16:03 +00:00
|
|
|
md_alg = MBEDTLS_MD_SHA512;
|
|
|
|
sig_alg = MBEDTLS_PK_ECDSA;
|
|
|
|
break;
|
2021-11-03 08:51:56 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2022-02-22 08:42:29 +00:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2021-11-26 08:11:40 +00:00
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
|
2021-11-03 08:51:56 +00:00
|
|
|
md_alg = MBEDTLS_MD_SHA256;
|
|
|
|
sig_alg = MBEDTLS_PK_RSASSA_PSS;
|
|
|
|
break;
|
2022-02-22 08:42:29 +00:00
|
|
|
#endif /* MBEDTLS_SHA256_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
|
|
|
|
md_alg = MBEDTLS_MD_SHA384;
|
|
|
|
sig_alg = MBEDTLS_PK_RSASSA_PSS;
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SHA384_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
|
|
|
|
md_alg = MBEDTLS_MD_SHA256;
|
|
|
|
sig_alg = MBEDTLS_PK_RSASSA_PSS;
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SHA512_C */
|
2021-11-03 08:51:56 +00:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2021-09-12 12:16:03 +00:00
|
|
|
default:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
|
2021-10-29 12:12:51 +00:00
|
|
|
goto error;
|
2021-09-12 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
|
|
|
|
( unsigned int ) algorithm ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the certificate's key type matches the signature alg
|
|
|
|
*/
|
|
|
|
if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
|
2021-10-29 12:12:51 +00:00
|
|
|
goto error;
|
2021-09-12 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
|
|
|
|
signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
p += 2;
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
|
|
|
|
|
|
|
|
/* Hash verify buffer with indicated hash function */
|
|
|
|
switch( md_alg )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
verify_hash_len = 32;
|
2021-10-25 06:01:13 +00:00
|
|
|
ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
|
2021-09-12 12:16:03 +00:00
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SHA256_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
|
|
|
case MBEDTLS_MD_SHA384:
|
|
|
|
verify_hash_len = 48;
|
2021-10-25 06:01:13 +00:00
|
|
|
ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
|
2021-09-12 12:16:03 +00:00
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SHA384_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
case MBEDTLS_MD_SHA512:
|
|
|
|
verify_hash_len = 64;
|
2021-10-25 06:01:13 +00:00
|
|
|
ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
|
2021-09-12 12:16:03 +00:00
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SHA512_C */
|
|
|
|
|
2021-10-28 05:41:59 +00:00
|
|
|
default:
|
|
|
|
ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
|
|
|
break;
|
2021-09-12 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 06:01:13 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
|
2021-10-29 12:12:51 +00:00
|
|
|
goto error;
|
2021-10-25 06:01:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 12:16:03 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
|
2021-11-03 08:51:56 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
|
|
|
|
{
|
|
|
|
const mbedtls_md_info_t* md_info;
|
2021-12-02 06:36:27 +00:00
|
|
|
rsassa_pss_options.mgf1_hash_id = md_alg;
|
2021-11-03 08:51:56 +00:00
|
|
|
if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
2021-12-02 06:36:27 +00:00
|
|
|
rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
|
|
|
|
options = (const void*) &rsassa_pss_options;
|
2021-11-03 08:51:56 +00:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2021-09-12 12:16:03 +00:00
|
|
|
|
2021-12-02 06:36:27 +00:00
|
|
|
if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
|
2021-09-12 12:16:03 +00:00
|
|
|
&ssl->session_negotiate->peer_cert->pk,
|
|
|
|
md_alg, verify_hash, verify_hash_len,
|
2021-10-29 12:12:51 +00:00
|
|
|
p, signature_len ) ) == 0 )
|
2021-09-12 12:16:03 +00:00
|
|
|
{
|
2021-10-29 12:12:51 +00:00
|
|
|
return( 0 );
|
2021-09-12 12:16:03 +00:00
|
|
|
}
|
2021-10-29 12:12:51 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* RFC 8446 section 4.4.3
|
|
|
|
*
|
|
|
|
* If the verification fails, the receiver MUST terminate the handshake
|
|
|
|
* with a "decrypt_error" alert.
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
2021-09-12 12:16:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
|
|
|
|
|
|
|
int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
2021-10-25 07:06:49 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2021-09-12 12:16:03 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-10-25 07:06:49 +00:00
|
|
|
unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
|
|
|
|
size_t verify_buffer_len;
|
|
|
|
unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
|
|
|
|
size_t transcript_len;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
2021-09-12 12:16:03 +00:00
|
|
|
|
2021-10-25 07:06:49 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PROC_CHK(
|
2021-11-12 08:53:56 +00:00
|
|
|
mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
|
2021-10-25 07:06:49 +00:00
|
|
|
MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
|
|
|
|
|
|
|
|
/* Need to calculate the hash of the transcript first
|
2021-10-28 05:41:59 +00:00
|
|
|
* before reading the message since otherwise it gets
|
|
|
|
* included in the transcript
|
|
|
|
*/
|
2021-10-25 07:06:49 +00:00
|
|
|
ret = mbedtls_ssl_get_handshake_transcript( ssl,
|
|
|
|
ssl->handshake->ciphersuite_info->mac,
|
|
|
|
transcript, sizeof( transcript ),
|
|
|
|
&transcript_len );
|
|
|
|
if( ret != 0 )
|
2021-09-12 12:16:03 +00:00
|
|
|
{
|
2021-10-25 07:06:49 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
return( ret );
|
2021-09-12 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 07:06:49 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
|
|
|
|
|
|
|
|
/* Create verify structure */
|
|
|
|
ssl_tls13_create_verify_structure( transcript,
|
2021-10-28 05:41:59 +00:00
|
|
|
transcript_len,
|
|
|
|
verify_buffer,
|
|
|
|
&verify_buffer_len,
|
|
|
|
( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
|
|
|
|
MBEDTLS_SSL_IS_SERVER :
|
|
|
|
MBEDTLS_SSL_IS_CLIENT );
|
2021-10-25 07:06:49 +00:00
|
|
|
|
|
|
|
/* Process the message contents */
|
2021-10-28 05:41:59 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
|
|
|
|
buf + buf_len, verify_buffer, verify_buffer_len ) );
|
2021-10-25 07:06:49 +00:00
|
|
|
|
2021-11-12 08:53:56 +00:00
|
|
|
mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
|
2021-10-25 07:06:49 +00:00
|
|
|
MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
|
2021-09-12 12:16:03 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
|
2021-11-05 05:32:38 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
|
2021-09-12 12:16:03 +00:00
|
|
|
return( ret );
|
2021-10-25 07:06:49 +00:00
|
|
|
#else
|
|
|
|
((void) ssl);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2021-09-12 12:16:03 +00:00
|
|
|
}
|
2021-08-24 07:59:48 +00:00
|
|
|
|
2021-09-29 09:12:03 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* STATE HANDLING: Incoming Certificate, client-side only currently.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
/*
|
|
|
|
* Structure of Certificate message:
|
|
|
|
*
|
|
|
|
* enum {
|
|
|
|
* X509(0),
|
|
|
|
* RawPublicKey(2),
|
|
|
|
* (255)
|
|
|
|
* } CertificateType;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* select (certificate_type) {
|
|
|
|
* case RawPublicKey:
|
|
|
|
* * From RFC 7250 ASN.1_subjectPublicKeyInfo *
|
|
|
|
* opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
|
|
|
|
* case X509:
|
|
|
|
* opaque cert_data<1..2^24-1>;
|
|
|
|
* };
|
|
|
|
* Extension extensions<0..2^16-1>;
|
|
|
|
* } CertificateEntry;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* opaque certificate_request_context<0..2^8-1>;
|
|
|
|
* CertificateEntry certificate_list<0..2^24-1>;
|
|
|
|
* } Certificate;
|
|
|
|
*
|
|
|
|
*/
|
2021-10-26 07:16:45 +00:00
|
|
|
|
|
|
|
/* Parse certificate chain send by the server. */
|
2021-09-29 09:12:03 +00:00
|
|
|
static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t certificate_request_context_len = 0;
|
|
|
|
size_t certificate_list_len = 0;
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char *certificate_list_end;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
|
|
|
|
certificate_request_context_len = p[0];
|
2021-10-29 02:05:32 +00:00
|
|
|
certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
|
2021-10-26 07:16:45 +00:00
|
|
|
p += 4;
|
2021-09-29 09:12:03 +00:00
|
|
|
|
|
|
|
/* In theory, the certificate list can be up to 2^24 Bytes, but we don't
|
|
|
|
* support anything beyond 2^16 = 64K.
|
|
|
|
*/
|
2021-10-26 07:16:45 +00:00
|
|
|
if( ( certificate_request_context_len != 0 ) ||
|
|
|
|
( certificate_list_len >= 0x10000 ) )
|
2021-09-29 09:12:03 +00:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In case we tried to reuse a session but it failed */
|
|
|
|
if( ssl->session_negotiate->peer_cert != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
|
|
|
|
mbedtls_free( ssl->session_negotiate->peer_cert );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ssl->session_negotiate->peer_cert =
|
|
|
|
mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
|
|
|
|
sizeof( mbedtls_x509_crt ) ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
|
|
|
|
|
|
|
|
certificate_list_end = p + certificate_list_len;
|
2021-10-26 07:16:45 +00:00
|
|
|
while( p < certificate_list_end )
|
2021-09-29 09:12:03 +00:00
|
|
|
{
|
|
|
|
size_t cert_data_len, extensions_len;
|
|
|
|
|
2021-10-26 07:16:45 +00:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
|
2021-10-29 02:39:30 +00:00
|
|
|
cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
|
2021-09-29 09:12:03 +00:00
|
|
|
p += 3;
|
|
|
|
|
|
|
|
/* In theory, the CRT can be up to 2^24 Bytes, but we don't support
|
|
|
|
* anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
|
|
|
|
* check that we have a minimum of 128 bytes of data, this is not
|
|
|
|
* clear why we need that though.
|
|
|
|
*/
|
|
|
|
if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
|
2021-10-26 07:16:45 +00:00
|
|
|
{
|
2021-09-29 09:12:03 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
}
|
|
|
|
|
2021-10-26 07:16:45 +00:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
|
2021-09-29 09:12:03 +00:00
|
|
|
ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
|
|
|
|
p, cert_data_len );
|
|
|
|
|
|
|
|
switch( ret )
|
|
|
|
{
|
|
|
|
case 0: /*ok*/
|
|
|
|
break;
|
|
|
|
case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
|
|
|
|
/* Ignore certificate with an unknown algorithm: maybe a
|
|
|
|
prior certificate was already trusted. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_X509_ALLOC_FAILED:
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
|
|
|
|
MBEDTLS_ERR_X509_ALLOC_FAILED );
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
|
|
|
|
MBEDTLS_ERR_X509_UNKNOWN_VERSION );
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
default:
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
|
|
|
|
ret );
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
p += cert_data_len;
|
|
|
|
|
|
|
|
/* Certificate extensions length */
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
|
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
|
|
|
|
p += 2;
|
2021-10-26 07:16:45 +00:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
|
2021-09-29 09:12:03 +00:00
|
|
|
p += extensions_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that all the message is consumed. */
|
|
|
|
if( p != end )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
|
|
|
{
|
|
|
|
((void) ssl);
|
|
|
|
((void) buf);
|
|
|
|
((void) end);
|
|
|
|
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2021-10-26 07:16:45 +00:00
|
|
|
/* Validate certificate chain sent by the server. */
|
2021-09-29 09:12:03 +00:00
|
|
|
static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_x509_crt *ca_chain;
|
|
|
|
mbedtls_x509_crl *ca_crl;
|
2021-10-28 06:50:17 +00:00
|
|
|
uint32_t verify_result = 0;
|
2021-09-29 09:12:03 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
|
|
|
if( ssl->handshake->sni_ca_chain != NULL )
|
|
|
|
{
|
|
|
|
ca_chain = ssl->handshake->sni_ca_chain;
|
|
|
|
ca_crl = ssl->handshake->sni_ca_crl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
|
|
|
{
|
|
|
|
ca_chain = ssl->conf->ca_chain;
|
|
|
|
ca_crl = ssl->conf->ca_crl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main check: verify certificate
|
|
|
|
*/
|
|
|
|
ret = mbedtls_x509_crt_verify_with_profile(
|
|
|
|
ssl->session_negotiate->peer_cert,
|
|
|
|
ca_chain, ca_crl,
|
|
|
|
ssl->conf->cert_profile,
|
|
|
|
ssl->hostname,
|
2021-10-28 06:50:17 +00:00
|
|
|
&verify_result,
|
2021-09-29 09:12:03 +00:00
|
|
|
ssl->conf->f_vrfy, ssl->conf->p_vrfy );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Secondary checks: always done, but change 'ret' only if it was 0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
{
|
|
|
|
const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
|
|
|
|
|
|
|
|
/* If certificate uses an EC key, make sure the curve is OK */
|
|
|
|
if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
|
|
|
|
mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
|
|
|
|
{
|
2021-10-28 06:50:17 +00:00
|
|
|
verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
|
2021-09-29 09:12:03 +00:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
|
|
|
|
if( ret == 0 )
|
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
|
|
|
|
|
|
|
if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
|
|
|
|
ssl->handshake->ciphersuite_info,
|
|
|
|
!ssl->conf->endpoint,
|
2021-10-28 06:50:17 +00:00
|
|
|
&verify_result ) != 0 )
|
2021-09-29 09:12:03 +00:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
|
|
|
|
if( ret == 0 )
|
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( ca_chain == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
|
|
|
|
ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
/* The certificate may have been rejected for several reasons.
|
|
|
|
Pick one and send the corresponding alert. Which alert to send
|
|
|
|
may be a subject of debate in some cases. */
|
2021-10-28 06:50:17 +00:00
|
|
|
if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
|
2021-09-29 09:12:03 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
|
2021-10-28 06:50:17 +00:00
|
|
|
else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
|
2021-09-29 09:12:03 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
|
2021-10-29 02:39:30 +00:00
|
|
|
else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
|
|
|
|
MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
|
|
|
|
MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
|
|
|
|
MBEDTLS_X509_BADCERT_BAD_PK |
|
|
|
|
MBEDTLS_X509_BADCERT_BAD_KEY ) )
|
2021-09-29 09:12:03 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
|
2021-10-28 06:50:17 +00:00
|
|
|
else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
|
2021-09-29 09:12:03 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
|
2021-10-28 06:50:17 +00:00
|
|
|
else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
|
2021-09-29 09:12:03 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
|
2021-10-28 06:50:17 +00:00
|
|
|
else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
|
2021-09-29 09:12:03 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
|
|
|
|
else
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
2021-10-28 06:50:17 +00:00
|
|
|
if( verify_result != 0 )
|
2021-09-29 09:12:03 +00:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
|
2021-10-28 14:16:33 +00:00
|
|
|
(unsigned int) verify_result ) );
|
2021-09-29 09:12:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|
|
|
|
|
2021-10-28 06:50:17 +00:00
|
|
|
ssl->session_negotiate->verify_result = verify_result;
|
2021-09-29 09:12:03 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
((void) ssl);
|
|
|
|
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
|
|
|
|
|
2021-10-26 07:16:45 +00:00
|
|
|
int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
|
2021-09-29 09:12:03 +00:00
|
|
|
{
|
2021-10-26 07:16:45 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
|
|
|
|
2021-11-12 08:53:56 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
|
2021-10-26 07:16:45 +00:00
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE,
|
|
|
|
&buf, &buf_len ) );
|
|
|
|
|
|
|
|
/* Parse the certificate chain sent by the peer. */
|
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
|
|
|
|
/* Validate the certificate chain and set the verification results. */
|
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
|
|
|
|
|
2021-11-12 08:53:56 +00:00
|
|
|
mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
|
|
|
|
buf, buf_len );
|
2021-10-26 07:16:45 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
|
2021-10-26 09:50:08 +00:00
|
|
|
#else
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
|
2021-10-26 07:16:45 +00:00
|
|
|
return( ret );
|
2021-09-29 09:12:03 +00:00
|
|
|
}
|
2022-01-29 14:12:42 +00:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2022-01-30 09:54:19 +00:00
|
|
|
/*
|
|
|
|
* enum {
|
|
|
|
* X509(0),
|
|
|
|
* RawPublicKey(2),
|
|
|
|
* (255)
|
|
|
|
* } CertificateType;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* select (certificate_type) {
|
|
|
|
* case RawPublicKey:
|
|
|
|
* // From RFC 7250 ASN.1_subjectPublicKeyInfo
|
|
|
|
* opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
|
|
|
|
*
|
|
|
|
* case X509:
|
|
|
|
* opaque cert_data<1..2^24-1>;
|
|
|
|
* };
|
|
|
|
* Extension extensions<0..2^16-1>;
|
|
|
|
* } CertificateEntry;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* opaque certificate_request_context<0..2^8-1>;
|
|
|
|
* CertificateEntry certificate_list<0..2^24-1>;
|
|
|
|
* } Certificate;
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
|
2022-02-15 03:05:59 +00:00
|
|
|
unsigned char *buf,
|
2022-01-30 09:54:19 +00:00
|
|
|
unsigned char *end,
|
2022-02-15 03:05:59 +00:00
|
|
|
size_t *out_len )
|
2022-01-28 08:16:08 +00:00
|
|
|
{
|
|
|
|
const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
|
2022-02-15 03:05:59 +00:00
|
|
|
unsigned char *p = buf;
|
2022-02-18 04:10:03 +00:00
|
|
|
unsigned char *certificate_request_context =
|
|
|
|
ssl->handshake->certificate_request_context;
|
|
|
|
unsigned char certificate_request_context_len =
|
|
|
|
ssl->handshake->certificate_request_context_len;
|
|
|
|
unsigned char *p_certificate_list_len;
|
2022-01-28 08:16:08 +00:00
|
|
|
|
|
|
|
|
2022-02-16 03:21:37 +00:00
|
|
|
/* ...
|
|
|
|
* opaque certificate_request_context<0..2^8-1>;
|
|
|
|
* ...
|
|
|
|
*/
|
2022-02-18 04:10:03 +00:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
|
|
|
|
*p++ = certificate_request_context_len;
|
|
|
|
if( certificate_request_context_len > 0 )
|
2022-02-15 06:00:57 +00:00
|
|
|
{
|
2022-02-18 04:10:03 +00:00
|
|
|
memcpy( p, certificate_request_context, certificate_request_context_len );
|
|
|
|
p += certificate_request_context_len;
|
2022-02-15 06:00:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 03:21:37 +00:00
|
|
|
/* ...
|
|
|
|
* CertificateEntry certificate_list<0..2^24-1>;
|
|
|
|
* ...
|
|
|
|
*/
|
2022-02-15 03:05:59 +00:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
|
2022-02-18 04:10:03 +00:00
|
|
|
p_certificate_list_len = p;
|
2022-02-15 03:05:59 +00:00
|
|
|
p += 3;
|
|
|
|
|
2022-01-30 09:54:19 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
|
2022-01-28 08:16:08 +00:00
|
|
|
|
2022-02-18 04:10:03 +00:00
|
|
|
while( crt != NULL )
|
2022-01-28 08:16:08 +00:00
|
|
|
{
|
2022-01-30 09:54:19 +00:00
|
|
|
size_t cert_data_len = crt->raw.len;
|
2022-01-28 08:16:08 +00:00
|
|
|
|
2022-01-30 09:54:19 +00:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
|
|
|
|
MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
|
|
|
|
p += 3;
|
2022-01-28 08:16:08 +00:00
|
|
|
|
2022-01-30 09:54:19 +00:00
|
|
|
memcpy( p, crt->raw.p, cert_data_len );
|
|
|
|
p += cert_data_len;
|
|
|
|
crt = crt->next;
|
2022-01-28 08:16:08 +00:00
|
|
|
|
|
|
|
/* Currently, we don't have any certificate extensions defined.
|
|
|
|
* Hence, we are sending an empty extension with length zero.
|
|
|
|
*/
|
2022-01-30 09:54:19 +00:00
|
|
|
MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
|
|
|
|
p += 2;
|
2022-01-28 08:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 04:10:03 +00:00
|
|
|
MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
|
|
|
|
p_certificate_list_len, 0 );
|
2022-01-30 09:54:19 +00:00
|
|
|
|
2022-02-15 03:05:59 +00:00
|
|
|
*out_len = p - buf;
|
2022-01-28 08:16:08 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-02-15 03:05:59 +00:00
|
|
|
int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
|
2022-01-28 08:16:08 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2022-03-09 06:00:13 +00:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
2022-01-28 08:16:08 +00:00
|
|
|
|
2022-03-09 06:00:13 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
|
2022-01-28 08:16:08 +00:00
|
|
|
|
2022-03-09 06:00:13 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
|
|
|
|
MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
|
2022-01-28 08:16:08 +00:00
|
|
|
|
2022-03-09 06:00:13 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
|
|
|
|
buf,
|
|
|
|
buf + buf_len,
|
|
|
|
&msg_len ) );
|
2022-01-28 08:16:08 +00:00
|
|
|
|
2022-03-09 06:00:13 +00:00
|
|
|
mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
|
|
|
|
MBEDTLS_SSL_HS_CERTIFICATE,
|
|
|
|
buf,
|
|
|
|
msg_len );
|
2022-01-28 08:16:08 +00:00
|
|
|
|
2022-03-09 06:00:13 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg(
|
|
|
|
ssl, buf_len, msg_len ) );
|
2022-01-28 08:16:08 +00:00
|
|
|
cleanup:
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2022-02-15 03:05:59 +00:00
|
|
|
/*
|
|
|
|
* STATE HANDLING: Output Certificate Verify
|
|
|
|
*/
|
|
|
|
static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len )
|
2022-01-29 02:01:04 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2022-02-15 03:05:59 +00:00
|
|
|
unsigned char *p = buf;
|
|
|
|
mbedtls_pk_context *own_key;
|
|
|
|
|
|
|
|
unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
|
|
|
|
size_t handshake_hash_len;
|
2022-01-29 02:01:04 +00:00
|
|
|
unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
|
|
|
|
size_t verify_buffer_len;
|
2022-02-17 10:30:13 +00:00
|
|
|
unsigned char signature_type;
|
2022-02-23 02:40:19 +00:00
|
|
|
#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2022-01-29 02:01:04 +00:00
|
|
|
size_t own_key_size;
|
2022-02-23 02:40:19 +00:00
|
|
|
#endif /* MBEDTLS_ECDSA_C || MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2022-02-18 04:10:03 +00:00
|
|
|
mbedtls_md_type_t md_alg;
|
2022-02-22 02:28:13 +00:00
|
|
|
uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
|
2022-02-15 03:05:59 +00:00
|
|
|
size_t signature_len = 0;
|
|
|
|
const mbedtls_md_info_t *md_info;
|
2022-02-16 03:21:37 +00:00
|
|
|
unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
|
2022-01-29 02:01:04 +00:00
|
|
|
size_t verify_hash_len;
|
|
|
|
|
2022-02-23 04:23:05 +00:00
|
|
|
*out_len = 0;
|
|
|
|
|
2022-02-15 03:05:59 +00:00
|
|
|
own_key = mbedtls_ssl_own_key( ssl );
|
|
|
|
if( own_key == NULL )
|
2022-01-29 02:01:04 +00:00
|
|
|
{
|
2022-02-15 03:05:59 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
2022-01-29 02:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_get_handshake_transcript( ssl,
|
|
|
|
ssl->handshake->ciphersuite_info->mac,
|
|
|
|
handshake_hash,
|
|
|
|
sizeof( handshake_hash ),
|
|
|
|
&handshake_hash_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
|
|
|
|
handshake_hash,
|
|
|
|
handshake_hash_len);
|
|
|
|
|
|
|
|
ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
|
|
|
|
verify_buffer, &verify_buffer_len,
|
|
|
|
ssl->conf->endpoint );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* SignatureScheme algorithm;
|
|
|
|
* opaque signature<0..2^16-1>;
|
|
|
|
* } CertificateVerify;
|
|
|
|
*/
|
2022-02-17 10:30:13 +00:00
|
|
|
signature_type = mbedtls_ssl_sig_from_pk( own_key );
|
2022-02-23 02:40:19 +00:00
|
|
|
#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2022-02-22 02:28:13 +00:00
|
|
|
/* Determine the size of the key */
|
|
|
|
own_key_size = mbedtls_pk_get_bitlen( own_key );
|
2022-02-23 02:40:19 +00:00
|
|
|
#endif /* MBEDTLS_ECDSA_C || MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2022-02-17 10:30:13 +00:00
|
|
|
switch( signature_type )
|
2022-01-29 02:01:04 +00:00
|
|
|
{
|
2022-02-17 10:30:13 +00:00
|
|
|
#if defined(MBEDTLS_ECDSA_C)
|
|
|
|
case MBEDTLS_SSL_SIG_ECDSA:
|
|
|
|
switch( own_key_size )
|
|
|
|
{
|
|
|
|
case 256:
|
|
|
|
md_alg = MBEDTLS_MD_SHA256;
|
|
|
|
algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
|
|
|
|
break;
|
|
|
|
case 384:
|
|
|
|
md_alg = MBEDTLS_MD_SHA384;
|
|
|
|
algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
|
|
|
|
break;
|
|
|
|
case 521:
|
|
|
|
md_alg = MBEDTLS_MD_SHA512;
|
|
|
|
algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3,
|
|
|
|
( "unknown key size: %"
|
|
|
|
MBEDTLS_PRINTF_SIZET " bits",
|
|
|
|
own_key_size ) );
|
2022-02-22 02:28:13 +00:00
|
|
|
break;
|
2022-02-17 10:30:13 +00:00
|
|
|
}
|
2022-02-15 03:05:59 +00:00
|
|
|
break;
|
2022-02-17 10:30:13 +00:00
|
|
|
#endif /* MBEDTLS_ECDSA_C */
|
|
|
|
|
2022-02-23 02:40:19 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
case MBEDTLS_SSL_SIG_RSA:
|
|
|
|
if( own_key_size <= 2048 &&
|
|
|
|
mbedtls_ssl_sig_alg_is_received( ssl,
|
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
|
|
|
|
{
|
|
|
|
md_alg = MBEDTLS_MD_SHA256;
|
|
|
|
algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
|
2022-02-22 08:42:29 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
|
|
|
( "signature algorthm is rsa_pss_rsae_sha256" ) );
|
2022-02-23 02:40:19 +00:00
|
|
|
}
|
|
|
|
else if( own_key_size <= 3072 &&
|
|
|
|
mbedtls_ssl_sig_alg_is_received( ssl,
|
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
|
|
|
|
{
|
|
|
|
md_alg = MBEDTLS_MD_SHA384;
|
|
|
|
algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
|
2022-02-22 08:42:29 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
|
|
|
( "signature algorthm is rsa_pss_rsae_sha384" ) );
|
2022-02-23 02:40:19 +00:00
|
|
|
}
|
|
|
|
else if( own_key_size <= 4096 &&
|
|
|
|
mbedtls_ssl_sig_alg_is_received( ssl,
|
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
|
|
|
|
{
|
|
|
|
md_alg = MBEDTLS_MD_SHA512;
|
|
|
|
algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
|
2022-02-22 08:42:29 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
|
|
|
( "signature algorthm is rsa_pss_rsae_sha512" ) );
|
2022-02-23 02:40:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown key size: %"
|
|
|
|
MBEDTLS_PRINTF_SIZET " bits",
|
|
|
|
own_key_size ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( mbedtls_rsa_set_padding( mbedtls_pk_rsa( *own_key ),
|
|
|
|
MBEDTLS_RSA_PKCS_V21,
|
|
|
|
md_alg ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Set RSA padding Fail" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2022-02-15 03:05:59 +00:00
|
|
|
default:
|
2022-02-17 10:30:13 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
|
|
|
( "unkown pk type : %d", signature_type ) );
|
2022-02-23 02:40:19 +00:00
|
|
|
return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
2022-01-29 02:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 02:28:13 +00:00
|
|
|
if( algorithm == MBEDTLS_TLS1_3_SIG_NONE ||
|
|
|
|
! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
|
2022-01-29 02:01:04 +00:00
|
|
|
{
|
2022-02-18 08:42:24 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1,
|
2022-02-18 13:07:18 +00:00
|
|
|
( "signature algorithm not in received or offered list." ) );
|
2022-02-23 09:34:29 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
2022-02-18 08:42:24 +00:00
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
2022-02-16 03:21:37 +00:00
|
|
|
return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
2022-01-29 02:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 03:21:37 +00:00
|
|
|
/* Check there is space for the algorithm identifier (2 bytes) and the
|
|
|
|
* signature length (2 bytes).
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
|
2022-02-15 03:05:59 +00:00
|
|
|
MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
|
|
|
|
p += 2;
|
2022-01-29 02:01:04 +00:00
|
|
|
|
|
|
|
/* Hash verify buffer with indicated hash function */
|
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
|
|
|
if( md_info == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
|
|
|
|
ret = mbedtls_md( md_info, verify_buffer, verify_buffer_len, verify_hash );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
verify_hash_len = mbedtls_md_get_size( md_info );
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_pk_sign( own_key, md_alg,
|
|
|
|
verify_hash, verify_hash_len,
|
2022-02-15 03:05:59 +00:00
|
|
|
p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
|
2022-01-29 02:01:04 +00:00
|
|
|
ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2022-02-15 03:05:59 +00:00
|
|
|
MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
|
|
|
|
p += 2 + signature_len;
|
|
|
|
|
|
|
|
*out_len = (size_t)( p - buf );
|
2022-01-29 02:01:04 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2022-02-15 03:05:59 +00:00
|
|
|
int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
|
2022-01-29 02:01:04 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2022-02-15 06:22:05 +00:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
|
|
|
|
2022-01-29 02:01:04 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
|
|
|
|
|
2022-02-15 06:22:05 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
|
|
|
|
MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
|
2022-01-29 02:01:04 +00:00
|
|
|
|
2022-02-15 06:22:05 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
|
|
|
|
ssl, buf, buf + buf_len, &msg_len ) );
|
2022-01-29 02:01:04 +00:00
|
|
|
|
2022-02-15 06:22:05 +00:00
|
|
|
mbedtls_ssl_tls13_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, msg_len );
|
2022-01-29 02:01:04 +00:00
|
|
|
|
2022-02-15 06:22:05 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg(
|
|
|
|
ssl, buf_len, msg_len ) );
|
2022-01-29 02:01:04 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2022-01-29 14:12:42 +00:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
|
|
|
|
2021-09-18 06:20:25 +00:00
|
|
|
/*
|
|
|
|
*
|
2021-11-09 11:55:10 +00:00
|
|
|
* STATE HANDLING: Incoming Finished message.
|
2021-09-18 06:20:25 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Implementation
|
|
|
|
*/
|
|
|
|
|
2021-11-10 03:07:04 +00:00
|
|
|
static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
|
2021-09-18 06:20:25 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2021-11-09 11:55:10 +00:00
|
|
|
ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
|
2021-09-18 06:20:25 +00:00
|
|
|
ssl->handshake->state_local.finished_in.digest,
|
|
|
|
sizeof( ssl->handshake->state_local.finished_in.digest ),
|
|
|
|
&ssl->handshake->state_local.finished_in.digest_len,
|
2021-11-09 11:55:10 +00:00
|
|
|
ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
|
2021-11-11 06:13:22 +00:00
|
|
|
MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
|
2021-09-18 06:20:25 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2021-11-09 11:55:10 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
|
2021-09-18 06:20:25 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:55:10 +00:00
|
|
|
static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end )
|
2021-09-18 06:20:25 +00:00
|
|
|
{
|
2021-11-11 03:37:45 +00:00
|
|
|
/*
|
|
|
|
* struct {
|
2021-11-11 06:13:22 +00:00
|
|
|
* opaque verify_data[Hash.length];
|
2021-11-11 03:37:45 +00:00
|
|
|
* } Finished;
|
|
|
|
*/
|
|
|
|
const unsigned char *expected_verify_data =
|
|
|
|
ssl->handshake->state_local.finished_in.digest;
|
|
|
|
size_t expected_verify_data_len =
|
|
|
|
ssl->handshake->state_local.finished_in.digest_len;
|
2021-09-18 06:20:25 +00:00
|
|
|
/* Structural validation */
|
2021-11-11 03:37:45 +00:00
|
|
|
if( (size_t)( end - buf ) != expected_verify_data_len )
|
2021-09-18 06:20:25 +00:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
2021-11-11 06:13:22 +00:00
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR );
|
2021-09-18 06:20:25 +00:00
|
|
|
return( MBEDTLS_ERR_SSL_DECODE_ERROR );
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:55:10 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
|
2021-11-11 03:37:45 +00:00
|
|
|
expected_verify_data,
|
|
|
|
expected_verify_data_len );
|
2021-11-09 11:55:10 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
|
2021-11-11 03:37:45 +00:00
|
|
|
expected_verify_data_len );
|
2021-09-18 06:20:25 +00:00
|
|
|
|
|
|
|
/* Semantic validation */
|
2021-11-24 10:17:36 +00:00
|
|
|
if( mbedtls_ct_memcmp( buf,
|
|
|
|
expected_verify_data,
|
|
|
|
expected_verify_data_len ) != 0 )
|
2021-09-18 06:20:25 +00:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
|
|
|
|
|
2021-11-11 03:37:45 +00:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
|
2021-11-11 06:13:22 +00:00
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
2021-09-18 06:20:25 +00:00
|
|
|
return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
}
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-11-11 06:13:22 +00:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2021-11-10 03:07:04 +00:00
|
|
|
static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
|
2021-09-18 06:20:25 +00:00
|
|
|
{
|
2021-11-11 03:37:45 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-09-18 06:20:25 +00:00
|
|
|
mbedtls_ssl_key_set traffic_keys;
|
2021-10-28 09:54:34 +00:00
|
|
|
mbedtls_ssl_transform *transform_application = NULL;
|
2021-09-18 06:20:25 +00:00
|
|
|
|
2021-10-12 08:43:37 +00:00
|
|
|
ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
|
2021-09-18 06:20:25 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1,
|
2021-10-12 08:43:37 +00:00
|
|
|
"mbedtls_ssl_tls13_key_schedule_stage_application", ret );
|
2021-10-28 08:03:38 +00:00
|
|
|
goto cleanup;
|
2021-09-18 06:20:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 03:37:45 +00:00
|
|
|
ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
|
2021-09-18 06:20:25 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1,
|
2021-11-10 06:17:40 +00:00
|
|
|
"mbedtls_ssl_tls13_generate_application_keys", ret );
|
2021-10-28 08:03:38 +00:00
|
|
|
goto cleanup;
|
2021-09-18 06:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
transform_application =
|
|
|
|
mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
|
|
|
|
if( transform_application == NULL )
|
2021-10-28 08:03:38 +00:00
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2021-09-18 06:20:25 +00:00
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_populate_transform(
|
|
|
|
transform_application,
|
|
|
|
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-28 08:03:38 +00:00
|
|
|
goto cleanup;
|
2021-09-18 06:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssl->transform_application = transform_application;
|
|
|
|
|
2021-10-28 08:03:38 +00:00
|
|
|
cleanup:
|
|
|
|
|
2021-11-11 03:37:45 +00:00
|
|
|
mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
|
2021-11-09 11:55:10 +00:00
|
|
|
if( ret != 0 )
|
2021-10-28 09:54:34 +00:00
|
|
|
{
|
|
|
|
mbedtls_free( transform_application );
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
|
|
|
|
}
|
2021-10-28 08:03:38 +00:00
|
|
|
return( ret );
|
2021-09-18 06:20:25 +00:00
|
|
|
}
|
2021-11-11 06:13:22 +00:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
2021-09-18 06:20:25 +00:00
|
|
|
|
2021-11-09 12:30:09 +00:00
|
|
|
static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
|
2021-09-18 06:20:25 +00:00
|
|
|
{
|
|
|
|
|
2021-11-11 06:13:22 +00:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2021-09-18 06:20:25 +00:00
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
{
|
2021-11-10 03:07:04 +00:00
|
|
|
return( ssl_tls13_postprocess_server_finished_message( ssl ) );
|
2021-09-18 06:20:25 +00:00
|
|
|
}
|
2021-11-11 06:13:22 +00:00
|
|
|
#else
|
|
|
|
((void) ssl);
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
2021-09-18 06:20:25 +00:00
|
|
|
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:55:10 +00:00
|
|
|
int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
2021-11-11 03:37:45 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-11-09 11:55:10 +00:00
|
|
|
unsigned char *buf;
|
2021-11-18 07:29:56 +00:00
|
|
|
size_t buf_len;
|
2021-11-09 11:55:10 +00:00
|
|
|
|
2021-11-10 06:17:40 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
|
2021-11-09 11:55:10 +00:00
|
|
|
|
|
|
|
/* Preprocessing step: Compute handshake digest */
|
2021-11-10 03:07:04 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
|
2021-11-09 11:55:10 +00:00
|
|
|
|
2021-11-12 08:53:56 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
|
2021-11-09 11:55:10 +00:00
|
|
|
MBEDTLS_SSL_HS_FINISHED,
|
2021-11-18 07:29:56 +00:00
|
|
|
&buf, &buf_len ) );
|
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
|
2021-11-12 08:53:56 +00:00
|
|
|
mbedtls_ssl_tls13_add_hs_msg_to_checksum(
|
2021-11-18 07:29:56 +00:00
|
|
|
ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
|
2021-11-10 03:07:04 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
|
2021-11-09 11:55:10 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2021-11-10 06:17:40 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
|
2021-11-09 11:55:10 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2021-09-22 07:40:30 +00:00
|
|
|
/*
|
|
|
|
*
|
2021-11-09 12:30:09 +00:00
|
|
|
* STATE HANDLING: Write and send Finished message.
|
2021-09-22 07:40:30 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*
|
2021-11-11 08:16:19 +00:00
|
|
|
* Implement
|
2021-09-22 07:40:30 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-10 07:33:09 +00:00
|
|
|
static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
|
2021-09-22 07:40:30 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Compute transcript of handshake up to now. */
|
2021-11-09 12:30:09 +00:00
|
|
|
ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
|
2021-09-22 07:40:30 +00:00
|
|
|
ssl->handshake->state_local.finished_out.digest,
|
|
|
|
sizeof( ssl->handshake->state_local.finished_out.digest ),
|
|
|
|
&ssl->handshake->state_local.finished_out.digest_len,
|
|
|
|
ssl->conf->endpoint );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2021-12-08 07:57:57 +00:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
|
2021-09-22 07:40:30 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:30:09 +00:00
|
|
|
static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
|
2021-09-22 07:40:30 +00:00
|
|
|
{
|
2021-11-15 03:33:57 +00:00
|
|
|
// TODO: Add back resumption keys calculation after MVP.
|
|
|
|
((void) ssl);
|
2021-09-22 07:40:30 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-11-10 07:33:09 +00:00
|
|
|
static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
|
2021-11-11 08:16:19 +00:00
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
2021-12-02 06:36:27 +00:00
|
|
|
size_t *out_len )
|
2021-09-22 07:40:30 +00:00
|
|
|
{
|
2021-11-10 07:33:09 +00:00
|
|
|
size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
|
2021-11-15 03:33:57 +00:00
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* opaque verify_data[Hash.length];
|
|
|
|
* } Finished;
|
|
|
|
*/
|
2021-11-10 07:33:09 +00:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
|
2021-09-22 07:40:30 +00:00
|
|
|
|
|
|
|
memcpy( buf, ssl->handshake->state_local.finished_out.digest,
|
2021-11-10 07:33:09 +00:00
|
|
|
verify_data_len );
|
2021-09-22 07:40:30 +00:00
|
|
|
|
2021-12-02 06:36:27 +00:00
|
|
|
*out_len = verify_data_len;
|
2021-09-22 07:40:30 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2021-11-09 11:55:10 +00:00
|
|
|
|
2021-11-11 08:16:19 +00:00
|
|
|
/* Main entry point: orchestrates the other functions */
|
|
|
|
int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
|
|
|
|
|
2021-11-15 06:01:26 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
|
|
|
|
|
2021-11-11 08:16:19 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
|
|
|
|
MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
|
|
|
|
ssl, buf, buf + buf_len, &msg_len ) );
|
|
|
|
|
2021-11-12 08:53:56 +00:00
|
|
|
mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
|
|
|
|
buf, msg_len );
|
2021-11-11 08:16:19 +00:00
|
|
|
|
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
|
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
|
|
|
|
buf_len, msg_len ) );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2021-10-30 13:44:47 +00:00
|
|
|
void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
|
|
|
|
|
|
|
|
/*
|
2021-11-15 05:54:06 +00:00
|
|
|
* Free the previous session and switch to the current one.
|
2021-10-30 13:44:47 +00:00
|
|
|
*/
|
|
|
|
if( ssl->session )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_session_free( ssl->session );
|
|
|
|
mbedtls_free( ssl->session );
|
|
|
|
}
|
|
|
|
ssl->session = ssl->session_negotiate;
|
|
|
|
ssl->session_negotiate = NULL;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
|
|
|
|
}
|
|
|
|
|
2021-11-24 15:25:31 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* STATE HANDLING: Write ChangeCipherSpec
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *olen )
|
|
|
|
{
|
|
|
|
((void) ssl);
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
|
|
|
|
buf[0] = 1;
|
|
|
|
*olen = 1;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
|
|
|
|
|
|
|
|
/* Write CCS message */
|
|
|
|
MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
|
|
|
|
ssl, ssl->out_msg,
|
|
|
|
ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
|
|
|
|
&ssl->out_msglen ) );
|
|
|
|
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
|
|
|
|
|
|
|
|
/* Dispatch message */
|
2022-02-02 14:33:46 +00:00
|
|
|
MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
|
2021-11-24 15:25:31 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
|
2022-01-19 06:56:30 +00:00
|
|
|
/* Reset SSL context and update hash for handling HRR.
|
|
|
|
*
|
|
|
|
* Replace Transcript-Hash(X) by
|
|
|
|
* Transcript-Hash( message_hash ||
|
|
|
|
* 00 00 Hash.length ||
|
|
|
|
* X )
|
|
|
|
* A few states of the handshake are preserved, including:
|
|
|
|
* - session ID
|
|
|
|
* - session ticket
|
|
|
|
* - negotiated ciphersuite
|
|
|
|
*/
|
|
|
|
int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
|
2022-01-24 08:56:23 +00:00
|
|
|
size_t hash_len;
|
2022-01-19 06:56:30 +00:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
|
|
|
uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
|
2022-02-14 15:39:52 +00:00
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2022-01-19 06:56:30 +00:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
|
|
|
|
|
2022-01-24 08:56:23 +00:00
|
|
|
ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
|
|
|
|
hash_transcript + 4,
|
|
|
|
MBEDTLS_MD_MAX_SIZE,
|
|
|
|
&hash_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
|
|
|
|
hash_transcript[1] = 0;
|
|
|
|
hash_transcript[2] = 0;
|
|
|
|
hash_transcript[3] = (unsigned char) hash_len;
|
|
|
|
|
|
|
|
hash_len += 4;
|
|
|
|
|
2022-01-19 06:56:30 +00:00
|
|
|
if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
|
2022-01-24 08:56:23 +00:00
|
|
|
hash_transcript, hash_len );
|
2022-01-19 06:56:30 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_hash_abort( &ssl->handshake->fin_sha256_psa );
|
|
|
|
psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
|
|
|
|
#else
|
|
|
|
mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SHA256_C */
|
|
|
|
}
|
|
|
|
else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
|
2022-01-24 08:56:23 +00:00
|
|
|
hash_transcript, hash_len );
|
2022-01-19 06:56:30 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_hash_abort( &ssl->handshake->fin_sha384_psa );
|
|
|
|
psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
|
|
|
|
#else
|
|
|
|
mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SHA384_C */
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:56:23 +00:00
|
|
|
#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
|
|
|
|
ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
|
|
|
|
#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
|
2022-02-14 15:39:52 +00:00
|
|
|
|
|
|
|
/* 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 06:56:30 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2022-01-27 07:03:26 +00:00
|
|
|
#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */
|