2020-08-21 14:20:06 +02:00
|
|
|
/*
|
|
|
|
* TLS 1.3 key schedule
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-09-08 11:31:33 +02:00
|
|
|
#include "common.h"
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2021-09-16 07:14:15 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-08-21 14:20:06 +02:00
|
|
|
#include "mbedtls/hkdf.h"
|
2021-05-24 07:39:41 +02:00
|
|
|
#include "mbedtls/debug.h"
|
2021-09-16 07:14:15 +02:00
|
|
|
#include "mbedtls/error.h"
|
2022-05-05 04:19:22 +02:00
|
|
|
#include "mbedtls/platform.h"
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2021-09-16 07:14:15 +02:00
|
|
|
#include "ssl_misc.h"
|
|
|
|
#include "ssl_tls13_keys.h"
|
2022-02-09 16:57:26 +01:00
|
|
|
#include "ssl_tls13_invasive.h"
|
|
|
|
|
|
|
|
#include "psa/crypto.h"
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2022-12-23 17:00:06 +01:00
|
|
|
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
|
|
|
psa_to_ssl_errors, \
|
|
|
|
psa_generic_status_to_mbedtls)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
|
2020-09-08 11:43:52 +02:00
|
|
|
.name = string,
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
|
2020-08-21 14:20:06 +02:00
|
|
|
{
|
|
|
|
/* This seems to work in C, despite the string literal being one
|
|
|
|
* character too long due to the 0-termination. */
|
2020-09-08 11:43:52 +02:00
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL_LIST
|
2020-08-21 14:20:06 +02:00
|
|
|
};
|
|
|
|
|
2020-09-08 12:33:48 +02:00
|
|
|
#undef MBEDTLS_SSL_TLS1_3_LABEL
|
2020-09-08 11:43:52 +02:00
|
|
|
|
2020-08-21 14:20:06 +02:00
|
|
|
/*
|
|
|
|
* This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
|
|
|
|
*
|
|
|
|
* The HkdfLabel is specified in RFC 8446 as follows:
|
|
|
|
*
|
|
|
|
* struct HkdfLabel {
|
|
|
|
* uint16 length; // Length of expanded key material
|
|
|
|
* opaque label<7..255>; // Always prefixed by "tls13 "
|
|
|
|
* opaque context<0..255>; // Usually a communication transcript hash
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - desired_length: Length of expanded key material
|
|
|
|
* Even though the standard allows expansion to up to
|
|
|
|
* 2**16 Bytes, TLS 1.3 never uses expansion to more than
|
|
|
|
* 255 Bytes, so we require `desired_length` to be at most
|
|
|
|
* 255. This allows us to save a few Bytes of code by
|
|
|
|
* hardcoding the writing of the high bytes.
|
2021-11-23 08:24:58 +01:00
|
|
|
* - (label, label_len): label + label length, without "tls13 " prefix
|
|
|
|
* The label length MUST be less than or equal to
|
|
|
|
* MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
|
|
|
|
* It is the caller's responsibility to ensure this.
|
|
|
|
* All (label, label length) pairs used in TLS 1.3
|
|
|
|
* can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
|
|
|
|
* - (ctx, ctx_len): context + context length
|
|
|
|
* The context length MUST be less than or equal to
|
|
|
|
* MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
|
|
|
|
* It is the caller's responsibility to ensure this.
|
2020-08-21 14:20:06 +02:00
|
|
|
* - dst: Target buffer for HkdfLabel structure,
|
|
|
|
* This MUST be a writable buffer of size
|
|
|
|
* at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
|
2021-11-23 08:24:58 +01:00
|
|
|
* - dst_len: Pointer at which to store the actual length of
|
|
|
|
* the HkdfLabel structure on success.
|
2020-08-21 14:20:06 +02:00
|
|
|
*/
|
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
static const char tls13_label_prefix[6] = "tls13 ";
|
2020-09-10 10:23:12 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
|
|
|
|
(2 /* expansion length */ \
|
|
|
|
+ 1 /* label length */ \
|
|
|
|
+ label_len \
|
|
|
|
+ 1 /* context length */ \
|
|
|
|
+ context_len)
|
2020-09-08 11:48:14 +02:00
|
|
|
|
|
|
|
#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
|
|
|
|
SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
|
2023-01-11 14:50:10 +01:00
|
|
|
sizeof(tls13_label_prefix) + \
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
static void ssl_tls13_hkdf_encode_label(
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t desired_length,
|
|
|
|
const unsigned char *label, size_t label_len,
|
|
|
|
const unsigned char *ctx, size_t ctx_len,
|
|
|
|
unsigned char *dst, size_t *dst_len)
|
2020-08-21 14:20:06 +02:00
|
|
|
{
|
2020-09-10 10:23:12 +02:00
|
|
|
size_t total_label_len =
|
2021-12-02 07:36:27 +01:00
|
|
|
sizeof(tls13_label_prefix) + label_len;
|
2020-08-21 14:20:06 +02:00
|
|
|
size_t total_hkdf_lbl_len =
|
2023-01-11 14:50:10 +01:00
|
|
|
SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
|
2020-08-21 14:20:06 +02:00
|
|
|
|
|
|
|
unsigned char *p = dst;
|
|
|
|
|
2020-09-16 10:45:27 +02:00
|
|
|
/* Add the size of the expanded key material.
|
|
|
|
* We're hardcoding the high byte to 0 here assuming that we never use
|
|
|
|
* TLS 1.3 HKDF key expansion to more than 255 Bytes. */
|
|
|
|
#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
|
2021-11-12 09:53:56 +01:00
|
|
|
#error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \
|
2023-01-11 14:50:10 +01:00
|
|
|
value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
|
2020-09-16 10:45:27 +02:00
|
|
|
#endif
|
|
|
|
|
2020-08-21 14:20:06 +02:00
|
|
|
*p++ = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
*p++ = MBEDTLS_BYTE_0(desired_length);
|
2020-08-21 14:20:06 +02:00
|
|
|
|
|
|
|
/* Add label incl. prefix */
|
2023-01-11 14:50:10 +01:00
|
|
|
*p++ = MBEDTLS_BYTE_0(total_label_len);
|
|
|
|
memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
|
2021-12-02 07:36:27 +01:00
|
|
|
p += sizeof(tls13_label_prefix);
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, label, label_len);
|
2021-11-18 08:29:56 +01:00
|
|
|
p += label_len;
|
2020-08-21 14:20:06 +02:00
|
|
|
|
|
|
|
/* Add context value */
|
2023-01-11 14:50:10 +01:00
|
|
|
*p++ = MBEDTLS_BYTE_0(ctx_len);
|
|
|
|
if (ctx_len != 0) {
|
|
|
|
memcpy(p, ctx, ctx_len);
|
|
|
|
}
|
2020-08-21 14:20:06 +02:00
|
|
|
|
|
|
|
/* Return total length to the caller. */
|
2021-11-18 08:29:56 +01:00
|
|
|
*dst_len = total_hkdf_lbl_len;
|
2020-08-21 14:20:06 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_hkdf_expand_label(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
const unsigned char *secret, size_t secret_len,
|
|
|
|
const unsigned char *label, size_t label_len,
|
|
|
|
const unsigned char *ctx, size_t ctx_len,
|
|
|
|
unsigned char *buf, size_t buf_len)
|
2020-08-21 14:20:06 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
|
2022-06-23 09:22:49 +02:00
|
|
|
size_t hkdf_label_len = 0;
|
2022-06-23 09:05:40 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-05-13 12:10:08 +02:00
|
|
|
psa_key_derivation_operation_t operation =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN) {
|
2020-08-21 14:20:06 +02:00
|
|
|
/* Should never happen since this is an internal
|
|
|
|
* function, and we know statically which labels
|
|
|
|
* are allowed. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-08-21 14:20:06 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
|
2020-08-21 14:20:06 +02:00
|
|
|
/* Should not happen, as above. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-08-21 14:20:06 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
|
2020-08-21 14:20:06 +02:00
|
|
|
/* Should not happen, as above. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-08-21 14:20:06 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_HASH(hash_alg)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-03-21 12:12:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_tls13_hkdf_encode_label(buf_len,
|
|
|
|
label, label_len,
|
|
|
|
ctx, ctx_len,
|
|
|
|
hkdf_label,
|
|
|
|
&hkdf_label_len);
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
|
2022-05-13 12:10:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_input_bytes(&operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SECRET,
|
|
|
|
secret,
|
|
|
|
secret_len);
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_input_bytes(&operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_INFO,
|
|
|
|
hkdf_label,
|
|
|
|
hkdf_label_len);
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_output_bytes(&operation,
|
|
|
|
buf,
|
|
|
|
buf_len);
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-06-23 09:05:40 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
abort_status = psa_key_derivation_abort(&operation);
|
|
|
|
status = (status == PSA_SUCCESS ? abort_status : status);
|
|
|
|
mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2020-08-21 14:20:06 +02:00
|
|
|
}
|
|
|
|
|
2022-11-23 11:08:04 +01:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-11-21 16:16:54 +01:00
|
|
|
static int ssl_tls13_make_traffic_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
const unsigned char *secret, size_t secret_len,
|
|
|
|
unsigned char *key, size_t key_len,
|
|
|
|
unsigned char *iv, size_t iv_len)
|
2022-11-21 16:16:54 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2022-11-29 08:19:27 +01:00
|
|
|
ret = mbedtls_ssl_tls13_hkdf_expand_label(
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg,
|
|
|
|
secret, secret_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
|
|
|
|
NULL, 0,
|
|
|
|
key, key_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-11-21 16:16:54 +01:00
|
|
|
|
2022-11-29 08:19:27 +01:00
|
|
|
ret = mbedtls_ssl_tls13_hkdf_expand_label(
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg,
|
|
|
|
secret, secret_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
|
|
|
|
NULL, 0,
|
|
|
|
iv, iv_len);
|
|
|
|
return ret;
|
2022-11-21 16:16:54 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 14:03:34 +02:00
|
|
|
/*
|
|
|
|
* The traffic keying material is generated from the following inputs:
|
|
|
|
*
|
|
|
|
* - One secret value per sender.
|
|
|
|
* - A purpose value indicating the specific value being generated
|
|
|
|
* - The desired lengths of key and IV.
|
|
|
|
*
|
|
|
|
* The expansion itself is based on HKDF:
|
|
|
|
*
|
|
|
|
* [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
|
|
|
|
* [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
|
|
|
|
*
|
|
|
|
* [sender] denotes the sending side and the Secret value is provided
|
|
|
|
* by the function caller. Note that we generate server and client side
|
|
|
|
* keys in a single function call.
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_make_traffic_keys(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
const unsigned char *client_secret,
|
|
|
|
const unsigned char *server_secret, size_t secret_len,
|
|
|
|
size_t key_len, size_t iv_len,
|
|
|
|
mbedtls_ssl_key_set *keys)
|
2020-08-21 14:03:34 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2022-11-21 16:16:54 +01:00
|
|
|
ret = ssl_tls13_make_traffic_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg, client_secret, secret_len,
|
|
|
|
keys->client_write_key, key_len,
|
|
|
|
keys->client_write_iv, iv_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-08-21 14:03:34 +02:00
|
|
|
|
2022-11-21 16:16:54 +01:00
|
|
|
ret = ssl_tls13_make_traffic_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg, server_secret, secret_len,
|
|
|
|
keys->server_write_key, key_len,
|
|
|
|
keys->server_write_iv, iv_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-08-21 14:03:34 +02:00
|
|
|
|
2020-09-08 12:01:00 +02:00
|
|
|
keys->key_len = key_len;
|
|
|
|
keys->iv_len = iv_len;
|
2020-08-21 14:03:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-08-21 14:03:34 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_secret(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
const unsigned char *secret, size_t secret_len,
|
|
|
|
const unsigned char *label, size_t label_len,
|
|
|
|
const unsigned char *ctx, size_t ctx_len,
|
|
|
|
int ctx_hashed,
|
|
|
|
unsigned char *dstbuf, size_t dstbuf_len)
|
2020-08-21 14:27:44 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char hashed_context[PSA_HASH_MAX_SIZE];
|
|
|
|
if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
|
|
|
|
PSA_HASH_LENGTH(hash_alg), &ctx_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2022-03-26 17:04:19 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
if (ctx_len > sizeof(hashed_context)) {
|
2020-09-09 13:57:16 +02:00
|
|
|
/* This should never happen since this function is internal
|
2020-09-09 13:58:29 +02:00
|
|
|
* and the code sets `ctx_hashed` correctly.
|
2020-09-09 13:57:16 +02:00
|
|
|
* Let's double-check nonetheless to not run at the risk
|
|
|
|
* of getting a stack overflow. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-09-09 13:57:16 +02:00
|
|
|
}
|
2020-08-21 14:27:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(hashed_context, ctx, ctx_len);
|
2020-08-21 14:27:44 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
|
|
|
|
secret, secret_len,
|
|
|
|
label, label_len,
|
|
|
|
hashed_context, ctx_len,
|
|
|
|
dstbuf, dstbuf_len);
|
2022-03-26 17:04:19 +01:00
|
|
|
|
2020-08-21 14:27:44 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_evolve_secret(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
const unsigned char *secret_old,
|
|
|
|
const unsigned char *input, size_t input_len,
|
|
|
|
unsigned char *secret_new)
|
2020-08-20 14:42:46 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-06-23 09:05:40 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-10-05 16:22:59 +02:00
|
|
|
size_t hlen;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
|
|
|
|
const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
|
2022-10-05 16:22:59 +02:00
|
|
|
const unsigned char *l_input = NULL;
|
|
|
|
size_t l_input_len;
|
|
|
|
|
2022-05-13 12:10:08 +02:00
|
|
|
psa_key_derivation_operation_t operation =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
2022-03-26 17:04:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_HASH(hash_alg)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-03-21 12:12:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hlen = PSA_HASH_LENGTH(hash_alg);
|
2020-08-20 14:42:46 +02:00
|
|
|
|
|
|
|
/* For non-initial runs, call Derive-Secret( ., "derived", "")
|
2020-09-16 10:24:14 +02:00
|
|
|
* on the old secret. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (secret_old != NULL) {
|
2021-11-12 09:53:56 +01:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg,
|
|
|
|
secret_old, hlen,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
|
|
|
|
NULL, 0, /* context */
|
|
|
|
MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
|
|
|
|
tmp_secret, hlen);
|
|
|
|
if (ret != 0) {
|
2020-08-20 14:42:46 +02:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-08-20 14:42:46 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 09:05:40 +02:00
|
|
|
ret = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (input != NULL && input_len != 0) {
|
2022-10-05 16:22:59 +02:00
|
|
|
l_input = input;
|
|
|
|
l_input_len = input_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-10-05 16:22:59 +02:00
|
|
|
l_input = all_zeroes_input;
|
|
|
|
l_input_len = hlen;
|
2020-08-20 14:42:46 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_setup(&operation,
|
|
|
|
PSA_ALG_HKDF_EXTRACT(hash_alg));
|
2020-08-20 14:42:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_input_bytes(&operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SALT,
|
|
|
|
tmp_secret,
|
|
|
|
hlen);
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_input_bytes(&operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SECRET,
|
|
|
|
l_input, l_input_len);
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_output_bytes(&operation,
|
|
|
|
secret_new,
|
|
|
|
PSA_HASH_LENGTH(hash_alg));
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-06-23 09:05:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
cleanup:
|
|
|
|
abort_status = psa_key_derivation_abort(&operation);
|
|
|
|
status = (status == PSA_SUCCESS ? abort_status : status);
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
|
|
|
|
return ret;
|
2020-08-20 14:42:46 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_early_secrets(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
unsigned char const *early_secret,
|
|
|
|
unsigned char const *transcript, size_t transcript_len,
|
|
|
|
mbedtls_ssl_tls13_early_secrets *derived)
|
2021-05-24 07:39:41 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/* We should never call this function with an unknown hash,
|
|
|
|
* but add an assertion anyway. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_HASH(hash_alg)) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 0
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* PSK -> HKDF-Extract = Early Secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret(., "c e traffic", ClientHello)
|
|
|
|
* | = client_early_traffic_secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret(., "e exp master", ClientHello)
|
|
|
|
* | = early_exporter_master_secret
|
|
|
|
* v
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Create client_early_traffic_secret */
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
early_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
|
|
|
|
transcript, transcript_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
|
|
|
|
derived->client_early_traffic_secret,
|
|
|
|
hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/* Create early exporter */
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
early_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
|
|
|
|
transcript, transcript_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
|
|
|
|
derived->early_exporter_master_secret,
|
|
|
|
hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-05-24 07:39:41 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_handshake_secrets(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
unsigned char const *handshake_secret,
|
|
|
|
unsigned char const *transcript, size_t transcript_len,
|
|
|
|
mbedtls_ssl_tls13_handshake_secrets *derived)
|
2021-05-24 07:39:41 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/* We should never call this function with an unknown hash,
|
|
|
|
* but add an assertion anyway. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_HASH(hash_alg)) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Handshake Secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "c hs traffic",
|
|
|
|
* | ClientHello...ServerHello )
|
|
|
|
* | = client_handshake_traffic_secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "s hs traffic",
|
|
|
|
* | ClientHello...ServerHello )
|
|
|
|
* | = server_handshake_traffic_secret
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute client_handshake_traffic_secret with
|
|
|
|
* Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
|
|
|
|
*/
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
handshake_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
|
|
|
|
transcript, transcript_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
|
|
|
|
derived->client_handshake_traffic_secret,
|
|
|
|
hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute server_handshake_traffic_secret with
|
|
|
|
* Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
|
|
|
|
*/
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
handshake_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
|
|
|
|
transcript, transcript_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
|
|
|
|
derived->server_handshake_traffic_secret,
|
|
|
|
hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-05-24 07:39:41 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_application_secrets(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
unsigned char const *application_secret,
|
|
|
|
unsigned char const *transcript, size_t transcript_len,
|
|
|
|
mbedtls_ssl_tls13_application_secrets *derived)
|
2021-05-24 07:39:41 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/* We should never call this function with an unknown hash,
|
|
|
|
* but add an assertion anyway. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_HASH(hash_alg)) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/* Generate {client,server}_application_traffic_secret_0
|
|
|
|
*
|
|
|
|
* Master Secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "c ap traffic",
|
|
|
|
* | ClientHello...server Finished )
|
|
|
|
* | = client_application_traffic_secret_0
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "s ap traffic",
|
|
|
|
* | ClientHello...Server Finished )
|
|
|
|
* | = server_application_traffic_secret_0
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "exp master",
|
|
|
|
* | ClientHello...server Finished)
|
|
|
|
* | = exporter_master_secret
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
application_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
|
|
|
|
transcript, transcript_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
|
|
|
|
derived->client_application_traffic_secret_N,
|
|
|
|
hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
application_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
|
|
|
|
transcript, transcript_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
|
|
|
|
derived->server_application_traffic_secret_N,
|
|
|
|
hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
application_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
|
|
|
|
transcript, transcript_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
|
|
|
|
derived->exporter_master_secret,
|
|
|
|
hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-05-24 07:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate resumption_master_secret for use with the ticket exchange.
|
|
|
|
*
|
2021-11-12 09:53:56 +01:00
|
|
|
* This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
|
2021-05-24 07:39:41 +02:00
|
|
|
* because it uses the transcript hash up to and including ClientFinished. */
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_resumption_master_secret(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
unsigned char const *application_secret,
|
|
|
|
unsigned char const *transcript, size_t transcript_len,
|
|
|
|
mbedtls_ssl_tls13_application_secrets *derived)
|
2021-05-24 07:39:41 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/* We should never call this function with an unknown hash,
|
|
|
|
* but add an assertion anyway. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_HASH(hash_alg)) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2021-05-24 07:39:41 +02:00
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
application_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
|
|
|
|
transcript, transcript_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
|
|
|
|
derived->resumption_master_secret,
|
|
|
|
hash_len);
|
2021-05-24 07:39:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-05-24 07:39:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-05-24 07:39:41 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 11:50:42 +01:00
|
|
|
/**
|
|
|
|
* \brief Transition into application stage of TLS 1.3 key schedule.
|
|
|
|
*
|
|
|
|
* The TLS 1.3 key schedule can be viewed as a simple state machine
|
|
|
|
* with states Initial -> Early -> Handshake -> Application, and
|
|
|
|
* this function represents the Handshake -> Application transition.
|
|
|
|
*
|
2023-01-05 10:36:12 +01:00
|
|
|
* In the handshake stage, ssl_tls13_generate_application_keys()
|
2022-12-13 11:50:42 +01:00
|
|
|
* can be used to derive the handshake traffic keys.
|
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on. This must be in key schedule
|
|
|
|
* stage \c Handshake.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-05 10:36:12 +01:00
|
|
|
static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
|
2021-09-18 08:20:25 +02:00
|
|
|
{
|
2021-10-28 10:03:38 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-11-11 04:37:45 +01:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-07-18 13:41:11 +02:00
|
|
|
psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
|
2023-01-11 14:50:10 +01:00
|
|
|
handshake->ciphersuite_info->mac);
|
2021-09-18 08:20:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute MasterSecret
|
|
|
|
*/
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_evolve_secret(
|
|
|
|
hash_alg,
|
|
|
|
handshake->tls13_master_secrets.handshake,
|
|
|
|
NULL, 0,
|
|
|
|
handshake->tls13_master_secrets.app);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
|
|
|
|
return ret;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(
|
|
|
|
4, "Master secret",
|
|
|
|
handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
|
|
|
|
unsigned char const *base_key,
|
|
|
|
unsigned char const *transcript,
|
|
|
|
unsigned char *dst,
|
|
|
|
size_t *dst_len)
|
2021-05-24 07:44:14 +02:00
|
|
|
{
|
2022-03-26 17:04:19 +01:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t hash_len = PSA_HASH_LENGTH(hash_alg);
|
2022-03-26 17:04:19 +01:00
|
|
|
unsigned char finished_key[PSA_MAC_MAX_SIZE];
|
2021-05-24 07:44:14 +02:00
|
|
|
int ret;
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t alg;
|
2021-05-24 07:44:14 +02:00
|
|
|
|
|
|
|
/* We should never call this function with an unknown hash,
|
|
|
|
* but add an assertion anyway. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_HASH(hash_alg)) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2021-05-24 07:44:14 +02:00
|
|
|
|
|
|
|
/* TLS 1.3 Finished message
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* opaque verify_data[Hash.length];
|
|
|
|
* } Finished;
|
|
|
|
*
|
|
|
|
* verify_data =
|
|
|
|
* HMAC( finished_key,
|
|
|
|
* Hash( Handshake Context +
|
|
|
|
* Certificate* +
|
|
|
|
* CertificateVerify* )
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* finished_key =
|
|
|
|
* HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
|
|
|
|
*/
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
ret = mbedtls_ssl_tls13_hkdf_expand_label(
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg, base_key, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
|
|
|
|
NULL, 0,
|
|
|
|
finished_key, hash_len);
|
|
|
|
if (ret != 0) {
|
2021-05-24 07:44:14 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-05-24 07:44:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
alg = PSA_ALG_HMAC(hash_alg);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
|
2022-03-26 17:04:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_import_key(&attributes, finished_key, hash_len, &key);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2021-05-24 07:44:14 +02:00
|
|
|
goto exit;
|
2022-03-26 17:04:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_compute(key, alg, transcript, hash_len,
|
|
|
|
dst, hash_len, dst_len);
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2021-05-24 07:44:14 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_destroy_key(key);
|
|
|
|
if (ret == 0) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-03-26 17:04:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
|
2022-03-26 17:04:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-05-24 07:44:14 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *dst,
|
|
|
|
size_t dst_len,
|
|
|
|
size_t *actual_len,
|
|
|
|
int from)
|
2021-09-18 08:20:25 +02:00
|
|
|
{
|
2021-10-22 08:32:32 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2021-11-10 04:07:04 +01:00
|
|
|
unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
|
2021-09-18 08:20:25 +02:00
|
|
|
size_t transcript_len;
|
|
|
|
|
2021-12-10 03:19:34 +01:00
|
|
|
unsigned char *base_key = NULL;
|
2021-12-10 10:55:23 +01:00
|
|
|
size_t base_key_len = 0;
|
2021-12-10 10:12:43 +01:00
|
|
|
mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
|
2023-01-11 14:50:10 +01:00
|
|
|
&ssl->handshake->tls13_hs_secrets;
|
2021-12-10 11:14:36 +01:00
|
|
|
|
|
|
|
mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
|
2022-03-26 17:04:19 +01:00
|
|
|
|
2022-07-18 13:41:11 +02:00
|
|
|
psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->ciphersuite_info->mac);
|
|
|
|
size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
|
2021-12-10 11:14:36 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
|
2021-12-10 11:14:36 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (from == MBEDTLS_SSL_IS_CLIENT) {
|
2021-12-10 10:55:23 +01:00
|
|
|
base_key = tls13_hs_secrets->client_handshake_traffic_secret;
|
2023-01-11 14:50:10 +01:00
|
|
|
base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
|
|
|
|
} else {
|
2021-12-10 10:55:23 +01:00
|
|
|
base_key = tls13_hs_secrets->server_handshake_traffic_secret;
|
2023-01-11 14:50:10 +01:00
|
|
|
base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
|
2021-12-10 10:55:23 +01:00
|
|
|
}
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (dst_len < hash_len) {
|
2021-12-10 10:12:43 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
|
|
|
goto exit;
|
|
|
|
}
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
|
|
|
|
transcript, sizeof(transcript),
|
|
|
|
&transcript_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
|
2021-10-28 10:03:38 +02:00
|
|
|
goto exit;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
|
|
|
|
transcript, dst, actual_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2021-10-28 10:03:38 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
|
2021-10-28 10:03:38 +02:00
|
|
|
|
|
|
|
exit:
|
2021-12-10 03:19:34 +01:00
|
|
|
/* Erase handshake secrets */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(base_key, base_key_len);
|
|
|
|
mbedtls_platform_zeroize(transcript, sizeof(transcript));
|
|
|
|
return ret;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
|
|
|
|
const psa_algorithm_t hash_alg,
|
|
|
|
unsigned char const *psk, size_t psk_len,
|
|
|
|
int psk_type,
|
|
|
|
unsigned char const *transcript,
|
|
|
|
unsigned char *result)
|
2021-05-24 07:44:14 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2022-03-26 17:28:06 +01:00
|
|
|
unsigned char binder_key[PSA_MAC_MAX_SIZE];
|
|
|
|
unsigned char early_secret[PSA_MAC_MAX_SIZE];
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
|
2022-03-26 17:04:19 +01:00
|
|
|
size_t actual_len;
|
2021-05-24 07:44:14 +02:00
|
|
|
|
2021-05-26 10:29:49 +02:00
|
|
|
#if !defined(MBEDTLS_DEBUG_C)
|
|
|
|
ssl = NULL; /* make sure we don't use it except for debug */
|
|
|
|
((void) ssl);
|
|
|
|
#endif
|
|
|
|
|
2021-05-24 07:44:14 +02:00
|
|
|
/* We should never call this function with an unknown hash,
|
|
|
|
* but add an assertion anyway. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_HASH(hash_alg)) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2021-05-24 07:44:14 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 0
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* PSK -> HKDF-Extract = Early Secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret(., "ext binder" | "res binder", "")
|
|
|
|
* | = binder_key
|
|
|
|
* v
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
|
|
|
|
NULL, /* Old secret */
|
|
|
|
psk, psk_len, /* Input */
|
|
|
|
early_secret);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
|
2021-05-24 07:44:14 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
|
|
|
|
early_secret, hash_len);
|
|
|
|
|
|
|
|
if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
early_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
|
|
|
|
NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
|
|
|
|
binder_key, hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
|
|
|
|
} else {
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_secret(
|
|
|
|
hash_alg,
|
|
|
|
early_secret, hash_len,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
|
|
|
|
NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
|
|
|
|
binder_key, hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
|
2021-05-24 07:44:14 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
|
2021-05-24 07:44:14 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The binding_value is computed in the same way as the Finished message
|
|
|
|
* but with the BaseKey being the binder_key.
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
|
|
|
|
result, &actual_len);
|
|
|
|
if (ret != 0) {
|
2021-05-24 07:44:14 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-05-24 07:44:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
|
2021-05-24 07:44:14 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
|
|
|
|
mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
|
|
|
|
return ret;
|
2021-05-24 07:44:14 +02:00
|
|
|
}
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
int mbedtls_ssl_tls13_populate_transform(
|
|
|
|
mbedtls_ssl_transform *transform,
|
|
|
|
int endpoint, int ciphersuite,
|
|
|
|
mbedtls_ssl_key_set const *traffic_keys,
|
|
|
|
mbedtls_ssl_context *ssl /* DEBUG ONLY */)
|
2021-03-22 08:50:44 +01:00
|
|
|
{
|
2022-01-19 16:00:22 +01:00
|
|
|
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
2021-03-22 08:50:44 +01:00
|
|
|
int ret;
|
|
|
|
mbedtls_cipher_info_t const *cipher_info;
|
2022-05-04 17:44:05 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2021-03-22 08:50:44 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
|
|
|
unsigned char const *key_enc;
|
|
|
|
unsigned char const *iv_enc;
|
|
|
|
unsigned char const *key_dec;
|
|
|
|
unsigned char const *iv_dec;
|
|
|
|
|
2022-01-12 10:29:03 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_key_type_t key_type;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_algorithm_t alg;
|
|
|
|
size_t key_bits;
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
|
|
|
#endif
|
|
|
|
|
2021-03-22 08:50:44 +01:00
|
|
|
#if !defined(MBEDTLS_DEBUG_C)
|
|
|
|
ssl = NULL; /* make sure we don't use it except for those cases */
|
|
|
|
(void) ssl;
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
|
|
|
|
if (ciphersuite_info == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
|
|
|
|
ciphersuite));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2021-04-20 06:27:57 +02:00
|
|
|
}
|
2021-03-22 08:50:44 +01:00
|
|
|
|
2022-05-04 17:44:05 +02:00
|
|
|
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
|
|
|
|
if (cipher_info == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
|
|
|
|
ciphersuite_info->cipher));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2021-03-22 08:50:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup cipher contexts in target transform
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
|
|
|
|
cipher_info)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
|
|
|
|
return ret;
|
2021-03-22 08:50:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
|
|
|
|
cipher_info)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
|
|
|
|
return ret;
|
2021-03-22 08:50:44 +01:00
|
|
|
}
|
2022-01-19 16:00:22 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2021-03-22 08:50:44 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2021-03-22 08:50:44 +01:00
|
|
|
key_enc = traffic_keys->server_write_key;
|
|
|
|
key_dec = traffic_keys->client_write_key;
|
|
|
|
iv_enc = traffic_keys->server_write_iv;
|
|
|
|
iv_dec = traffic_keys->client_write_iv;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-22 08:50:44 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2021-03-22 08:50:44 +01:00
|
|
|
key_enc = traffic_keys->client_write_key;
|
|
|
|
key_dec = traffic_keys->server_write_key;
|
|
|
|
iv_enc = traffic_keys->client_write_iv;
|
|
|
|
iv_dec = traffic_keys->server_write_iv;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-22 08:50:44 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
{
|
|
|
|
/* should not happen */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2021-03-22 08:50:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
|
|
|
|
memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
|
2021-03-22 08:50:44 +01:00
|
|
|
|
2022-01-19 16:00:22 +01:00
|
|
|
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
|
|
|
|
key_enc, cipher_info->key_bitlen,
|
|
|
|
MBEDTLS_ENCRYPT)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
|
|
|
|
return ret;
|
2021-03-22 08:50:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
|
|
|
|
key_dec, cipher_info->key_bitlen,
|
|
|
|
MBEDTLS_DECRYPT)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
|
|
|
|
return ret;
|
2021-03-22 08:50:44 +01:00
|
|
|
}
|
2022-01-19 16:00:22 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2021-03-22 08:50:44 +01:00
|
|
|
|
2022-01-17 15:28:57 +01:00
|
|
|
/*
|
|
|
|
* Setup other fields in SSL transform
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
|
2022-01-17 15:28:57 +01:00
|
|
|
transform->taglen = 8;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-01-17 15:28:57 +01:00
|
|
|
transform->taglen = 16;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-01-17 15:28:57 +01:00
|
|
|
|
|
|
|
transform->ivlen = traffic_keys->iv_len;
|
|
|
|
transform->maclen = 0;
|
|
|
|
transform->fixed_ivlen = transform->ivlen;
|
2022-03-14 17:34:51 +01:00
|
|
|
transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
|
2022-01-17 15:28:57 +01:00
|
|
|
|
|
|
|
/* We add the true record content type (1 Byte) to the plaintext and
|
2021-12-21 06:14:10 +01:00
|
|
|
* then pad to the configured granularity. The minimum length of the
|
2022-01-17 15:28:57 +01:00
|
|
|
* type-extended and padded plaintext is therefore the padding
|
|
|
|
* granularity. */
|
|
|
|
transform->minlen =
|
|
|
|
transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
|
|
|
|
|
2022-01-12 10:29:03 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-01-19 16:00:22 +01:00
|
|
|
/*
|
|
|
|
* Setup psa keys and alg
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
|
|
|
|
transform->taglen,
|
|
|
|
&alg,
|
|
|
|
&key_type,
|
|
|
|
&key_bits)) != PSA_SUCCESS) {
|
2023-03-29 08:54:51 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2022-01-12 10:29:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
transform->psa_alg = alg;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (alg != MBEDTLS_SSL_NULL_CIPHER) {
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
|
|
|
|
|
|
|
if ((status = psa_import_key(&attributes,
|
|
|
|
key_enc,
|
|
|
|
PSA_BITS_TO_BYTES(key_bits),
|
|
|
|
&transform->psa_key_enc)) != PSA_SUCCESS) {
|
2023-03-29 08:54:51 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2022-02-01 11:25:55 +01:00
|
|
|
}
|
2022-01-17 15:47:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
2022-01-17 15:47:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((status = psa_import_key(&attributes,
|
|
|
|
key_dec,
|
|
|
|
PSA_BITS_TO_BYTES(key_bits),
|
|
|
|
&transform->psa_key_dec)) != PSA_SUCCESS) {
|
2023-03-29 08:54:51 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2022-02-01 11:25:55 +01:00
|
|
|
}
|
2022-01-12 10:29:03 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-03-22 08:50:44 +01:00
|
|
|
}
|
|
|
|
|
2022-11-04 04:17:35 +01:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
|
|
|
static int ssl_tls13_get_cipher_key_info(
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
|
|
|
|
size_t *key_len, size_t *iv_len)
|
2022-11-04 04:17:35 +01:00
|
|
|
{
|
|
|
|
psa_key_type_t key_type;
|
|
|
|
psa_algorithm_t alg;
|
|
|
|
size_t taglen;
|
|
|
|
size_t key_bits;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
|
2022-11-04 04:17:35 +01:00
|
|
|
taglen = 8;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-11-04 04:17:35 +01:00
|
|
|
taglen = 16;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-04 04:17:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, taglen,
|
|
|
|
&alg, &key_type, &key_bits);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-04 04:17:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*key_len = PSA_BITS_TO_BYTES(key_bits);
|
2022-11-04 04:17:35 +01:00
|
|
|
|
|
|
|
/* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
|
|
|
|
*iv_len = 12;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-04 07:10:34 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2022-11-21 15:45:58 +01:00
|
|
|
/*
|
|
|
|
* ssl_tls13_generate_early_key() generates the key necessary for protecting
|
2022-11-22 14:55:56 +01:00
|
|
|
* the early application data and handshake messages as described in section 7
|
|
|
|
* of RFC 8446.
|
2022-11-21 15:45:58 +01:00
|
|
|
*
|
2022-11-22 14:55:56 +01:00
|
|
|
* NOTE: Only one key is generated, the key for the traffic from the client to
|
|
|
|
* the server. The TLS 1.3 specification does not define a secret and thus
|
|
|
|
* a key for server early traffic.
|
2022-11-21 15:45:58 +01:00
|
|
|
*/
|
2022-11-04 07:10:34 +01:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_key_set *traffic_keys)
|
2022-11-04 07:10:34 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_md_type_t md_type;
|
|
|
|
psa_algorithm_t hash_alg;
|
|
|
|
size_t hash_len;
|
|
|
|
unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
|
|
|
|
size_t transcript_len;
|
2022-11-29 08:19:27 +01:00
|
|
|
size_t key_len;
|
|
|
|
size_t iv_len;
|
2022-12-15 08:14:35 +01:00
|
|
|
mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
|
2022-11-04 07:10:34 +01:00
|
|
|
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2023-03-29 08:54:51 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
handshake->ciphersuite_info;
|
2022-11-04 07:10:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
|
2022-11-04 07:10:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
|
2022-11-23 11:26:20 +01:00
|
|
|
goto cleanup;
|
2022-11-04 07:10:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
md_type = ciphersuite_info->mac;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
|
|
|
|
hash_len = PSA_HASH_LENGTH(hash_alg);
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
|
|
|
|
transcript,
|
|
|
|
sizeof(transcript),
|
|
|
|
&transcript_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
"mbedtls_ssl_get_handshake_transcript",
|
|
|
|
ret);
|
2022-11-23 11:26:20 +01:00
|
|
|
goto cleanup;
|
2022-11-04 07:10:34 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 06:03:47 +01:00
|
|
|
ret = mbedtls_ssl_tls13_derive_early_secrets(
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg, handshake->tls13_master_secrets.early,
|
2022-12-13 07:58:45 +01:00
|
|
|
transcript, transcript_len, &tls13_early_secrets);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-11-04 07:10:34 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-01-11 14:50:10 +01:00
|
|
|
1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
|
2022-11-23 11:26:20 +01:00
|
|
|
goto cleanup;
|
2022-11-04 07:10:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(
|
|
|
|
4, "Client early traffic secret",
|
2022-12-13 07:58:45 +01:00
|
|
|
tls13_early_secrets.client_early_traffic_secret, hash_len);
|
2022-11-04 07:10:34 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Export client handshake traffic secret
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_export_keys != NULL) {
|
2022-11-21 06:03:47 +01:00
|
|
|
ssl->f_export_keys(
|
|
|
|
ssl->p_export_keys,
|
|
|
|
MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
|
2022-12-13 07:58:45 +01:00
|
|
|
tls13_early_secrets.client_early_traffic_secret,
|
2022-11-21 06:03:47 +01:00
|
|
|
hash_len,
|
|
|
|
handshake->randbytes,
|
|
|
|
handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
|
2022-11-04 07:10:34 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 16:16:54 +01:00
|
|
|
ret = ssl_tls13_make_traffic_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg,
|
2022-12-13 07:58:45 +01:00
|
|
|
tls13_early_secrets.client_early_traffic_secret,
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_len, traffic_keys->client_write_key, key_len,
|
|
|
|
traffic_keys->client_write_iv, iv_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
|
2022-11-23 11:26:20 +01:00
|
|
|
goto cleanup;
|
2022-11-04 07:10:34 +01:00
|
|
|
}
|
2022-11-21 16:16:54 +01:00
|
|
|
traffic_keys->key_len = key_len;
|
|
|
|
traffic_keys->iv_len = iv_len;
|
2022-11-04 07:10:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
|
|
|
|
traffic_keys->client_write_key,
|
|
|
|
traffic_keys->key_len);
|
2022-11-04 07:10:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
|
|
|
|
traffic_keys->client_write_iv,
|
|
|
|
traffic_keys->iv_len);
|
2022-11-04 07:10:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
|
2022-11-04 07:10:34 +01:00
|
|
|
|
2022-11-23 11:26:20 +01:00
|
|
|
cleanup:
|
2022-12-15 08:14:35 +01:00
|
|
|
/* Erase early secrets and transcript */
|
2022-11-23 11:26:20 +01:00
|
|
|
mbedtls_platform_zeroize(
|
2022-12-13 07:58:45 +01:00
|
|
|
&tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(transcript, sizeof(transcript));
|
|
|
|
return ret;
|
2022-11-04 07:10:34 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
|
2022-11-04 07:10:34 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ssl_key_set traffic_keys;
|
|
|
|
mbedtls_ssl_transform *transform_earlydata = NULL;
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
|
|
|
|
|
|
|
/* Next evolution in key schedule: Establish early_data secret and
|
|
|
|
* key material. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
|
|
|
|
ret);
|
2022-11-04 07:10:34 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
|
|
|
|
if (transform_earlydata == NULL) {
|
2022-11-04 07:10:34 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_populate_transform(
|
2023-01-11 14:50:10 +01:00
|
|
|
transform_earlydata,
|
|
|
|
ssl->conf->endpoint,
|
2023-02-07 04:17:45 +01:00
|
|
|
handshake->ciphersuite_info->id,
|
2023-01-11 14:50:10 +01:00
|
|
|
&traffic_keys,
|
|
|
|
ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
|
2022-11-04 07:10:34 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
handshake->transform_earlydata = transform_earlydata;
|
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_free(transform_earlydata);
|
|
|
|
}
|
2022-11-04 07:10:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-11-04 07:10:34 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
|
2021-09-09 08:31:24 +02:00
|
|
|
{
|
2021-09-16 07:14:15 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg;
|
2021-10-11 10:39:29 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-07-19 08:21:29 +02:00
|
|
|
unsigned char *psk = NULL;
|
|
|
|
size_t psk_len = 0;
|
2021-09-28 12:51:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (handshake->ciphersuite_info == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2021-09-09 08:31:24 +02:00
|
|
|
}
|
2021-09-16 07:14:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
|
|
|
|
ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2022-08-17 04:18:10 +02:00
|
|
|
}
|
2022-07-19 08:21:29 +02:00
|
|
|
}
|
2022-08-17 04:18:10 +02:00
|
|
|
#endif
|
2022-07-19 08:21:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
|
|
|
|
handshake->tls13_master_secrets.early);
|
2022-07-19 08:21:29 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
2022-10-04 16:38:25 +02:00
|
|
|
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free((void *) psk);
|
2022-07-19 08:21:29 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
|
|
|
|
return ret;
|
2021-09-09 08:31:24 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
|
|
|
|
handshake->tls13_master_secrets.early,
|
|
|
|
PSA_HASH_LENGTH(hash_alg));
|
|
|
|
return 0;
|
2021-09-09 08:31:24 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 11:50:42 +01:00
|
|
|
/**
|
|
|
|
* \brief Compute TLS 1.3 handshake traffic keys.
|
|
|
|
*
|
2023-01-05 10:36:12 +01:00
|
|
|
* ssl_tls13_generate_handshake_keys() generates keys necessary for
|
|
|
|
* protecting the handshake messages, as described in Section 7 of
|
2023-02-01 07:29:47 +01:00
|
|
|
* RFC 8446.
|
2022-12-13 11:50:42 +01:00
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on. This must be in
|
|
|
|
* key schedule stage \c Handshake, see
|
2023-01-05 10:36:12 +01:00
|
|
|
* ssl_tls13_key_schedule_stage_handshake().
|
2023-02-01 07:29:47 +01:00
|
|
|
* \param traffic_keys The address at which to store the handshake traffic
|
2022-12-13 11:50:42 +01:00
|
|
|
* keys. This must be writable but may be uninitialized.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-05 10:36:12 +01:00
|
|
|
static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_key_set *traffic_keys)
|
2021-09-16 12:59:08 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_md_type_t md_type;
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg;
|
|
|
|
size_t hash_len;
|
2021-10-13 05:22:16 +02:00
|
|
|
unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
|
2021-09-16 12:59:08 +02:00
|
|
|
size_t transcript_len;
|
2022-11-21 06:03:47 +01:00
|
|
|
size_t key_len;
|
|
|
|
size_t iv_len;
|
2021-09-16 12:59:08 +02:00
|
|
|
|
2021-10-13 05:22:16 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2023-03-29 08:54:51 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
handshake->ciphersuite_info;
|
|
|
|
mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
|
|
|
|
&handshake->tls13_hs_secrets;
|
2021-10-13 05:22:16 +02:00
|
|
|
|
2023-01-05 10:36:12 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
|
2021-09-16 12:59:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
|
2022-05-05 15:34:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-10-13 05:22:16 +02:00
|
|
|
md_type = ciphersuite_info->mac;
|
2022-03-26 17:04:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
|
|
|
|
hash_len = PSA_HASH_LENGTH(hash_alg);
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
|
|
|
|
transcript,
|
|
|
|
sizeof(transcript),
|
|
|
|
&transcript_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
"mbedtls_ssl_get_handshake_transcript",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2021-09-16 12:59:08 +02:00
|
|
|
}
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_handshake_secrets(
|
|
|
|
hash_alg, handshake->tls13_master_secrets.handshake,
|
|
|
|
transcript, transcript_len, tls13_hs_secrets);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2021-09-16 12:59:08 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
|
|
|
|
tls13_hs_secrets->client_handshake_traffic_secret,
|
|
|
|
hash_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
|
|
|
|
tls13_hs_secrets->server_handshake_traffic_secret,
|
|
|
|
hash_len);
|
2021-09-16 12:59:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Export client handshake traffic secret
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_export_keys != NULL) {
|
2023-03-29 08:54:51 +02:00
|
|
|
ssl->f_export_keys(
|
|
|
|
ssl->p_export_keys,
|
|
|
|
MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
|
|
|
|
tls13_hs_secrets->client_handshake_traffic_secret,
|
|
|
|
hash_len,
|
|
|
|
handshake->randbytes,
|
|
|
|
handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
|
|
|
|
MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
|
|
|
|
|
|
|
|
ssl->f_export_keys(
|
|
|
|
ssl->p_export_keys,
|
|
|
|
MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
|
|
|
|
tls13_hs_secrets->server_handshake_traffic_secret,
|
|
|
|
hash_len,
|
|
|
|
handshake->randbytes,
|
|
|
|
handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
|
|
|
|
MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_make_traffic_keys(
|
|
|
|
hash_alg,
|
|
|
|
tls13_hs_secrets->client_handshake_traffic_secret,
|
|
|
|
tls13_hs_secrets->server_handshake_traffic_secret,
|
|
|
|
hash_len, key_len, iv_len, traffic_keys);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
|
2021-09-16 12:59:08 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
|
|
|
|
traffic_keys->client_write_key,
|
|
|
|
traffic_keys->key_len);
|
2021-09-16 12:59:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
|
|
|
|
traffic_keys->server_write_key,
|
|
|
|
traffic_keys->key_len);
|
2021-09-16 12:59:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
|
|
|
|
traffic_keys->client_write_iv,
|
|
|
|
traffic_keys->iv_len);
|
2021-09-16 12:59:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
|
|
|
|
traffic_keys->server_write_iv,
|
|
|
|
traffic_keys->iv_len);
|
2021-09-16 12:59:08 +02:00
|
|
|
|
2023-01-05 10:36:12 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
|
2021-09-16 12:59:08 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-16 12:59:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 11:50:42 +01:00
|
|
|
/**
|
|
|
|
* \brief Transition into handshake stage of TLS 1.3 key schedule.
|
|
|
|
*
|
|
|
|
* The TLS 1.3 key schedule can be viewed as a simple state machine
|
|
|
|
* with states Initial -> Early -> Handshake -> Application, and
|
|
|
|
* this function represents the Early -> Handshake transition.
|
|
|
|
*
|
2023-01-05 10:36:12 +01:00
|
|
|
* In the handshake stage, ssl_tls13_generate_handshake_keys()
|
2022-12-13 11:50:42 +01:00
|
|
|
* can be used to derive the handshake traffic keys.
|
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on. This must be in key schedule
|
|
|
|
* stage \c Early.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-05 10:36:12 +01:00
|
|
|
static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
|
2021-09-09 11:14:45 +02:00
|
|
|
{
|
2021-10-11 11:47:07 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-07-18 13:41:11 +02:00
|
|
|
psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
|
2023-01-11 14:50:10 +01:00
|
|
|
handshake->ciphersuite_info->mac);
|
2022-10-05 17:20:21 +02:00
|
|
|
unsigned char *shared_secret = NULL;
|
|
|
|
size_t shared_secret_len = 0;
|
2021-09-09 11:14:45 +02:00
|
|
|
|
2022-10-20 14:37:35 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
2021-09-09 11:14:45 +02:00
|
|
|
/*
|
2021-10-11 11:47:07 +02:00
|
|
|
* Compute ECDHE secret used to compute the handshake secret from which
|
|
|
|
* client_handshake_traffic_secret and server_handshake_traffic_secret
|
|
|
|
* are derived in the handshake secret derivation stage.
|
2021-09-09 11:14:45 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
|
|
|
|
if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id)) {
|
2023-03-20 15:22:47 +01:00
|
|
|
#if defined(PSA_WANT_ALG_ECDH)
|
2023-01-11 14:50:10 +01:00
|
|
|
/* Compute ECDH shared secret. */
|
2022-10-05 15:37:11 +02:00
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2022-10-05 17:20:21 +02:00
|
|
|
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_get_key_attributes(handshake->ecdh_psa_privkey,
|
|
|
|
&key_attributes);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-10-05 17:20:21 +02:00
|
|
|
|
|
|
|
shared_secret_len = PSA_BITS_TO_BYTES(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_get_key_bits(&key_attributes));
|
|
|
|
shared_secret = mbedtls_calloc(1, shared_secret_len);
|
|
|
|
if (shared_secret == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2022-10-05 15:37:11 +02:00
|
|
|
|
|
|
|
status = psa_raw_key_agreement(
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
|
|
|
|
handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
|
|
|
|
shared_secret, shared_secret_len, &shared_secret_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
|
2022-10-05 17:20:21 +02:00
|
|
|
goto cleanup;
|
2022-10-05 15:37:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_destroy_key(handshake->ecdh_psa_privkey);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
|
2022-10-05 17:20:21 +02:00
|
|
|
goto cleanup;
|
2022-10-05 15:37:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
|
2023-03-20 15:22:47 +01:00
|
|
|
#endif /* PSA_WANT_ALG_ECDH */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2021-09-09 11:14:45 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-20 14:37:35 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
|
2021-09-09 11:14:45 +02:00
|
|
|
|
|
|
|
/*
|
2021-10-11 11:47:07 +02:00
|
|
|
* Compute the Handshake Secret
|
2021-09-09 11:14:45 +02:00
|
|
|
*/
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_evolve_secret(
|
|
|
|
hash_alg, handshake->tls13_master_secrets.early,
|
|
|
|
shared_secret, shared_secret_len,
|
|
|
|
handshake->tls13_master_secrets.handshake);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
|
2022-10-05 17:20:21 +02:00
|
|
|
goto cleanup;
|
2021-09-09 11:14:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
|
|
|
|
handshake->tls13_master_secrets.handshake,
|
|
|
|
PSA_HASH_LENGTH(hash_alg));
|
2021-09-09 11:14:45 +02:00
|
|
|
|
2022-10-05 17:20:21 +02:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (shared_secret != NULL) {
|
|
|
|
mbedtls_platform_zeroize(shared_secret, shared_secret_len);
|
|
|
|
mbedtls_free(shared_secret);
|
2022-10-05 17:20:21 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-09 11:14:45 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 11:50:42 +01:00
|
|
|
/**
|
|
|
|
* \brief Compute TLS 1.3 application traffic keys.
|
|
|
|
*
|
2023-01-05 10:36:12 +01:00
|
|
|
* ssl_tls13_generate_application_keys() generates application traffic
|
2023-02-01 07:29:47 +01:00
|
|
|
* keys, since any record following a 1-RTT Finished message MUST be
|
2023-01-05 10:36:12 +01:00
|
|
|
* encrypted under the application traffic key.
|
2022-12-13 11:50:42 +01:00
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on. This must be in
|
|
|
|
* key schedule stage \c Application, see
|
2023-01-05 10:36:12 +01:00
|
|
|
* ssl_tls13_key_schedule_stage_application().
|
2023-02-01 07:29:47 +01:00
|
|
|
* \param traffic_keys The address at which to store the application traffic
|
2022-12-13 11:50:42 +01:00
|
|
|
* keys. This must be writable but may be uninitialized.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
2021-09-18 08:20:25 +02:00
|
|
|
*/
|
2022-12-13 11:50:42 +01:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-05 10:36:12 +01:00
|
|
|
static int ssl_tls13_generate_application_keys(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_key_set *traffic_keys)
|
2021-09-18 08:20:25 +02:00
|
|
|
{
|
2021-10-22 08:32:32 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-11-11 04:37:45 +01:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2021-09-18 08:20:25 +02:00
|
|
|
|
|
|
|
/* Address at which to store the application secrets */
|
2021-11-12 09:53:56 +01:00
|
|
|
mbedtls_ssl_tls13_application_secrets * const app_secrets =
|
2021-09-18 08:20:25 +02:00
|
|
|
&ssl->session_negotiate->app_secrets;
|
|
|
|
|
|
|
|
/* Holding the transcript up to and including the ServerFinished */
|
2021-11-11 04:37:45 +01:00
|
|
|
unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
|
2021-09-18 08:20:25 +02:00
|
|
|
size_t transcript_len;
|
|
|
|
|
|
|
|
/* Variables relating to the hash for the chosen ciphersuite. */
|
|
|
|
mbedtls_md_type_t md_type;
|
2022-03-26 17:04:19 +01:00
|
|
|
|
|
|
|
psa_algorithm_t hash_alg;
|
|
|
|
size_t hash_len;
|
2021-09-18 08:20:25 +02:00
|
|
|
|
|
|
|
/* Variables relating to the cipher for the chosen ciphersuite. */
|
2021-11-18 08:29:56 +01:00
|
|
|
size_t key_len, iv_len;
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
|
2021-09-18 08:20:25 +02:00
|
|
|
|
|
|
|
/* Extract basic information about hash and ciphersuite */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
|
|
|
|
&key_len, &iv_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
|
2022-05-05 15:34:39 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2021-11-11 04:37:45 +01:00
|
|
|
md_type = handshake->ciphersuite_info->mac;
|
2022-03-26 17:04:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
|
|
|
|
hash_len = PSA_HASH_LENGTH(hash_alg);
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2021-12-21 06:14:10 +01:00
|
|
|
/* Compute current handshake transcript. It's the caller's responsibility
|
2021-09-18 08:20:25 +02:00
|
|
|
* to call this at the right time, that is, after the ServerFinished. */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
|
|
|
|
transcript, sizeof(transcript),
|
|
|
|
&transcript_len);
|
|
|
|
if (ret != 0) {
|
2021-10-12 10:43:37 +02:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-09-18 08:20:25 +02:00
|
|
|
|
|
|
|
/* Compute application secrets from master secret and transcript hash. */
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_derive_application_secrets(
|
|
|
|
hash_alg, handshake->tls13_master_secrets.app,
|
|
|
|
transcript, transcript_len, app_secrets);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2023-03-29 08:54:51 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
|
2021-10-12 10:43:37 +02:00
|
|
|
goto cleanup;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Derive first epoch of IV + Key for application traffic. */
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
ret = mbedtls_ssl_tls13_make_traffic_keys(
|
|
|
|
hash_alg,
|
|
|
|
app_secrets->client_application_traffic_secret_N,
|
|
|
|
app_secrets->server_application_traffic_secret_N,
|
|
|
|
hash_len, key_len, iv_len, traffic_keys);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
|
2021-10-12 10:43:37 +02:00
|
|
|
goto cleanup;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
|
|
|
|
app_secrets->client_application_traffic_secret_N,
|
|
|
|
hash_len);
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
|
|
|
|
app_secrets->server_application_traffic_secret_N,
|
|
|
|
hash_len);
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2021-11-03 07:40:11 +01:00
|
|
|
/*
|
|
|
|
* Export client/server application traffic secret 0
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_export_keys != NULL) {
|
2023-03-29 08:54:51 +02:00
|
|
|
ssl->f_export_keys(
|
|
|
|
ssl->p_export_keys,
|
|
|
|
MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
|
|
|
|
app_secrets->client_application_traffic_secret_N, hash_len,
|
|
|
|
handshake->randbytes,
|
|
|
|
handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
|
|
|
|
MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
|
|
|
|
a new constant for TLS 1.3! */);
|
|
|
|
|
|
|
|
ssl->f_export_keys(
|
|
|
|
ssl->p_export_keys,
|
|
|
|
MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
|
|
|
|
app_secrets->server_application_traffic_secret_N, hash_len,
|
|
|
|
handshake->randbytes,
|
|
|
|
handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
|
|
|
|
MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
|
|
|
|
a new constant for TLS 1.3! */);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
|
|
|
|
traffic_keys->client_write_key, key_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
|
|
|
|
traffic_keys->server_write_key, key_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
|
|
|
|
traffic_keys->client_write_iv, iv_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
|
|
|
|
traffic_keys->server_write_iv, iv_len);
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
|
|
|
|
|
|
|
|
cleanup:
|
2021-12-08 06:28:49 +01:00
|
|
|
/* randbytes is not used again */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(ssl->handshake->randbytes,
|
|
|
|
sizeof(ssl->handshake->randbytes));
|
2022-05-06 10:40:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(transcript, sizeof(transcript));
|
|
|
|
return ret;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
|
2022-05-05 04:19:22 +02:00
|
|
|
{
|
2022-05-06 10:40:05 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-05-05 04:19:22 +02:00
|
|
|
mbedtls_ssl_key_set traffic_keys;
|
|
|
|
mbedtls_ssl_transform *transform_handshake = NULL;
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
|
|
|
|
|
|
|
/* Compute handshake secret */
|
2023-01-05 10:36:12 +01:00
|
|
|
ret = ssl_tls13_key_schedule_stage_handshake(ssl);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
|
2022-05-05 04:19:22 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next evolution in key schedule: Establish handshake secret and
|
|
|
|
* key material. */
|
2023-01-05 10:36:12 +01:00
|
|
|
ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2023-01-05 10:36:12 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
|
2023-01-11 14:50:10 +01:00
|
|
|
ret);
|
2022-05-05 04:19:22 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
|
|
|
|
if (transform_handshake == NULL) {
|
2022-05-05 04:19:22 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2022-05-06 10:40:05 +02:00
|
|
|
ret = mbedtls_ssl_tls13_populate_transform(
|
2023-01-11 14:50:10 +01:00
|
|
|
transform_handshake,
|
|
|
|
ssl->conf->endpoint,
|
2023-02-07 04:17:45 +01:00
|
|
|
handshake->ciphersuite_info->id,
|
2023-01-11 14:50:10 +01:00
|
|
|
&traffic_keys,
|
|
|
|
ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
|
2022-05-05 04:19:22 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
handshake->transform_handshake = transform_handshake;
|
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_free(transform_handshake);
|
|
|
|
}
|
2022-05-05 04:19:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-05-05 04:19:22 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
|
2022-04-16 10:52:57 +02:00
|
|
|
{
|
2022-09-13 05:25:28 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-08-03 06:28:08 +02:00
|
|
|
mbedtls_md_type_t md_type;
|
2022-09-13 05:25:28 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
|
|
|
unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
|
2022-08-03 06:28:08 +02:00
|
|
|
size_t transcript_len;
|
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
|
2022-08-03 06:28:08 +02:00
|
|
|
|
2022-09-13 05:25:28 +02:00
|
|
|
md_type = handshake->ciphersuite_info->mac;
|
2022-08-03 06:28:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
|
|
|
|
transcript, sizeof(transcript),
|
|
|
|
&transcript_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-03 06:28:08 +02:00
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_translate_md(md_type),
|
|
|
|
handshake->tls13_master_secrets.app,
|
|
|
|
transcript, transcript_len,
|
|
|
|
&ssl->session_negotiate->app_secrets);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-03 06:28:08 +02:00
|
|
|
|
2022-04-16 10:52:57 +02:00
|
|
|
/* Erase master secrets */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
|
|
|
|
sizeof(handshake->tls13_master_secrets));
|
2022-08-03 06:28:08 +02:00
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(
|
|
|
|
4, "Resumption master secret",
|
|
|
|
ssl->session_negotiate->app_secrets.resumption_master_secret,
|
|
|
|
PSA_HASH_LENGTH(mbedtls_psa_translate_md(md_type)));
|
2022-09-13 05:25:28 +02:00
|
|
|
|
2023-03-29 08:54:51 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-16 10:52:57 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
|
2022-05-19 08:29:48 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ssl_key_set traffic_keys;
|
|
|
|
mbedtls_ssl_transform *transform_application = NULL;
|
|
|
|
|
2023-01-05 10:36:12 +01:00
|
|
|
ret = ssl_tls13_key_schedule_stage_application(ssl);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
2023-01-05 10:36:12 +01:00
|
|
|
"ssl_tls13_key_schedule_stage_application", ret);
|
2022-05-19 08:29:48 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-05 10:36:12 +01:00
|
|
|
ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
2023-01-05 10:36:12 +01:00
|
|
|
"ssl_tls13_generate_application_keys", ret);
|
2022-05-19 08:29:48 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
transform_application =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
|
|
|
|
if (transform_application == NULL) {
|
2022-05-19 08:29:48 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_populate_transform(
|
2023-01-11 14:50:10 +01:00
|
|
|
transform_application,
|
|
|
|
ssl->conf->endpoint,
|
2023-02-07 04:17:45 +01:00
|
|
|
ssl->handshake->ciphersuite_info->id,
|
2023-01-11 14:50:10 +01:00
|
|
|
&traffic_keys,
|
|
|
|
ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
|
2022-05-19 08:29:48 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->transform_application = transform_application;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_free(transform_application);
|
2022-05-19 08:29:48 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-05-19 08:29:48 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char **psk,
|
|
|
|
size_t *psk_len)
|
2022-07-26 10:58:57 +02:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2022-08-25 04:51:44 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-07-26 10:58:57 +02:00
|
|
|
|
|
|
|
*psk_len = 0;
|
|
|
|
*psk = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2022-07-26 10:58:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-07-26 10:58:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
|
|
|
|
*psk = mbedtls_calloc(1, *psk_len);
|
|
|
|
if (*psk == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2022-07-26 10:58:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_export_key(ssl->handshake->psk_opaque,
|
|
|
|
(uint8_t *) *psk, *psk_len, psk_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
mbedtls_free((void *) *psk);
|
2022-08-17 04:18:10 +02:00
|
|
|
*psk = NULL;
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2022-07-26 10:58:57 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-26 10:58:57 +02:00
|
|
|
#else
|
|
|
|
*psk = ssl->handshake->psk;
|
|
|
|
*psk_len = ssl->handshake->psk_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*psk == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
return 0;
|
2022-07-26 10:58:57 +02:00
|
|
|
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
}
|
2022-10-04 16:38:25 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
|
2022-07-26 10:58:57 +02:00
|
|
|
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|