blob: 1967883b61ec7f2ab08036b11e99f50548ede78c [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Hanno Beckerbe9d6642020-08-21 13:20:06 +01006 */
7
Hanno Becker58c5cea2020-09-08 10:31:33 +01008#include "common.h"
Hanno Beckerbe9d6642020-08-21 13:20:06 +01009
Ronald Cron6f135e12021-12-08 16:57:54 +010010#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010011
Hanno Beckerbe9d6642020-08-21 13:20:06 +010012#include <stdint.h>
13#include <string.h>
14
Jerry Yue3131ef2021-09-16 13:14:15 +080015#include "mbedtls/hkdf.h"
Valerio Settib4f50762024-01-17 10:24:52 +010016#include "debug_internal.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080017#include "mbedtls/error.h"
Jerry Yue110d252022-05-05 10:19:22 +080018#include "mbedtls/platform.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080019
20#include "ssl_misc.h"
21#include "ssl_tls13_keys.h"
Gabor Mezeia3eecd22022-02-09 16:57:26 +010022#include "ssl_tls13_invasive.h"
23
24#include "psa/crypto.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010025#include "mbedtls/psa_util.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080026
Andrzej Kurek00644842023-05-30 05:45:00 -040027/* Define a local translating function to save code size by not using too many
28 * arguments in each translating place. */
29static int local_err_translation(psa_status_t status)
30{
31 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040032 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040033 psa_generic_status_to_mbedtls);
34}
35#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050036
Gilles Peskine449bd832023-01-11 14:50:10 +010037#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010038 .name = string,
39
Xiaofei Bai746f9482021-11-12 08:53:56 +000040struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010041{
42 /* This seems to work in C, despite the string literal being one
43 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010044 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010045};
46
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010047#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010048
Hanno Beckerbe9d6642020-08-21 13:20:06 +010049/*
50 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
51 *
52 * The HkdfLabel is specified in RFC 8446 as follows:
53 *
54 * struct HkdfLabel {
55 * uint16 length; // Length of expanded key material
56 * opaque label<7..255>; // Always prefixed by "tls13 "
57 * opaque context<0..255>; // Usually a communication transcript hash
58 * };
59 *
60 * Parameters:
Max Fillinger7833b182024-12-02 19:26:13 +010061 * - desired_length: Length of expanded key material.
Max Fillinger5122dc62024-12-02 19:34:40 +010062 * The length field can hold numbers up to 2**16, but HKDF
63 * can only generate outputs of up to 255 * HASH_LEN bytes.
64 * It is the caller's responsibility to ensure that this
65 * limit is not exceeded. In TLS 1.3, SHA256 is the hash
66 * function with the smallest block size, so a length
67 * <= 255 * 32 = 8160 is always safe.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000068 * - (label, label_len): label + label length, without "tls13 " prefix
69 * The label length MUST be less than or equal to
Max Fillinger529931a2024-11-25 20:38:04 +010070 * MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000071 * It is the caller's responsibility to ensure this.
72 * All (label, label length) pairs used in TLS 1.3
73 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
74 * - (ctx, ctx_len): context + context length
75 * The context length MUST be less than or equal to
76 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
77 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010078 * - dst: Target buffer for HkdfLabel structure,
79 * This MUST be a writable buffer of size
80 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000081 * - dst_len: Pointer at which to store the actual length of
82 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010083 */
84
Felix Conway2e1399f2025-06-11 16:04:30 +010085/* We need to tell the compiler that we meant to leave out the null character. */
Felix Conway766be1f2025-06-12 10:52:36 +010086static const char tls13_label_prefix[6] MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010087
Gilles Peskine449bd832023-01-11 14:50:10 +010088#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
89 (2 /* expansion length */ \
90 + 1 /* label length */ \
91 + label_len \
92 + 1 /* context length */ \
93 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010094
95#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
96 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +010097 sizeof(tls13_label_prefix) + \
Max Fillinger529931a2024-11-25 20:38:04 +010098 MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN, \
Gilles Peskine449bd832023-01-11 14:50:10 +010099 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100100
Xiaofei Bai746f9482021-11-12 08:53:56 +0000101static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 size_t desired_length,
103 const unsigned char *label, size_t label_len,
104 const unsigned char *ctx, size_t ctx_len,
105 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100106{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100107 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000108 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100109 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100111
112 unsigned char *p = dst;
113
Max Fillingerffc47e62024-10-29 18:49:30 +0100114 /* Add the size of the expanded key material. */
115#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > UINT16_MAX
116#error "The desired key length must fit into an uint16 but \
117 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN is greater than UINT16_MAX"
Hanno Becker531fe302020-09-16 09:45:27 +0100118#endif
119
Max Fillingerffc47e62024-10-29 18:49:30 +0100120 *p++ = MBEDTLS_BYTE_1(desired_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100122
123 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 *p++ = MBEDTLS_BYTE_0(total_label_len);
125 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000126 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000128 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100129
130 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 *p++ = MBEDTLS_BYTE_0(ctx_len);
132 if (ctx_len != 0) {
133 memcpy(p, ctx, ctx_len);
134 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100135
136 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000137 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100138}
139
Xiaofei Bai746f9482021-11-12 08:53:56 +0000140int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 psa_algorithm_t hash_alg,
142 const unsigned char *secret, size_t secret_len,
143 const unsigned char *label, size_t label_len,
144 const unsigned char *ctx, size_t ctx_len,
145 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100146{
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200148 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
150 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200151 psa_key_derivation_operation_t operation =
152 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100153
Max Fillinger529931a2024-11-25 20:38:04 +0100154 if (label_len > MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100155 /* Should never happen since this is an internal
156 * function, and we know statically which labels
157 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100159 }
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100162 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100164 }
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100167 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100169 }
170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (!PSA_ALG_IS_HASH(hash_alg)) {
172 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
173 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 ssl_tls13_hkdf_encode_label(buf_len,
176 label, label_len,
177 ctx, ctx_len,
178 hkdf_label,
179 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (status != PSA_SUCCESS) {
184 goto cleanup;
185 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 status = psa_key_derivation_input_bytes(&operation,
188 PSA_KEY_DERIVATION_INPUT_SECRET,
189 secret,
190 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (status != PSA_SUCCESS) {
193 goto cleanup;
194 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 status = psa_key_derivation_input_bytes(&operation,
197 PSA_KEY_DERIVATION_INPUT_INFO,
198 hkdf_label,
199 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (status != PSA_SUCCESS) {
202 goto cleanup;
203 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 status = psa_key_derivation_output_bytes(&operation,
206 buf,
207 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if (status != PSA_SUCCESS) {
210 goto cleanup;
211 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200212
213cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 abort_status = psa_key_derivation_abort(&operation);
215 status = (status == PSA_SUCCESS ? abort_status : status);
216 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500217 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100218}
219
Jerry Yua5db6c02022-11-23 18:08:04 +0800220MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800221static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 psa_algorithm_t hash_alg,
223 const unsigned char *secret, size_t secret_len,
224 unsigned char *key, size_t key_len,
225 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800226{
227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
228
Jerry Yuaec08b32022-11-29 15:19:27 +0800229 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 hash_alg,
231 secret, secret_len,
232 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
233 NULL, 0,
234 key, key_len);
235 if (ret != 0) {
236 return ret;
237 }
Jerry Yua8771832022-11-21 23:16:54 +0800238
Jerry Yuaec08b32022-11-29 15:19:27 +0800239 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 hash_alg,
241 secret, secret_len,
242 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
243 NULL, 0,
244 iv, iv_len);
245 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800246}
247
Hanno Becker3385a4d2020-08-21 13:03:34 +0100248/*
249 * The traffic keying material is generated from the following inputs:
250 *
251 * - One secret value per sender.
252 * - A purpose value indicating the specific value being generated
253 * - The desired lengths of key and IV.
254 *
255 * The expansion itself is based on HKDF:
256 *
257 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
258 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
259 *
260 * [sender] denotes the sending side and the Secret value is provided
261 * by the function caller. Note that we generate server and client side
262 * keys in a single function call.
263 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000264int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 psa_algorithm_t hash_alg,
266 const unsigned char *client_secret,
267 const unsigned char *server_secret, size_t secret_len,
268 size_t key_len, size_t iv_len,
269 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100270{
271 int ret = 0;
272
Jerry Yua8771832022-11-21 23:16:54 +0800273 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 hash_alg, client_secret, secret_len,
275 keys->client_write_key, key_len,
276 keys->client_write_iv, iv_len);
277 if (ret != 0) {
278 return ret;
279 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100280
Jerry Yua8771832022-11-21 23:16:54 +0800281 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 hash_alg, server_secret, secret_len,
283 keys->server_write_key, key_len,
284 keys->server_write_iv, iv_len);
285 if (ret != 0) {
286 return ret;
287 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100288
Hanno Becker493ea7f2020-09-08 11:01:00 +0100289 keys->key_len = key_len;
290 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100293}
294
Xiaofei Bai746f9482021-11-12 08:53:56 +0000295int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 psa_algorithm_t hash_alg,
297 const unsigned char *secret, size_t secret_len,
298 const unsigned char *label, size_t label_len,
299 const unsigned char *ctx, size_t ctx_len,
300 int ctx_hashed,
301 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100302{
303 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
305 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100306 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
309 PSA_HASH_LENGTH(hash_alg), &ctx_len);
310 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500311 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100312 return ret;
313 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 } else {
315 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100316 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100317 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100318 * Let's double-check nonetheless to not run at the risk
319 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100321 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100324 }
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
327 secret, secret_len,
328 label, label_len,
329 hashed_context, ctx_len,
330 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100331
Hanno Beckerb35d5222020-08-21 13:27:44 +0100332}
333
Xiaofei Bai746f9482021-11-12 08:53:56 +0000334int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 psa_algorithm_t hash_alg,
336 const unsigned char *secret_old,
337 const unsigned char *input, size_t input_len,
338 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100339{
340 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
342 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200343 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
345 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200346 const unsigned char *l_input = NULL;
347 size_t l_input_len;
348
Przemek Stekield5ae3652022-05-13 12:10:08 +0200349 psa_key_derivation_operation_t operation =
350 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (!PSA_ALG_IS_HASH(hash_alg)) {
353 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
354 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100357
358 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100359 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000361 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 hash_alg,
363 secret_old, hlen,
364 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
365 NULL, 0, /* context */
366 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
367 tmp_secret, hlen);
368 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100369 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100371 }
372
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200373 ret = 0;
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200376 l_input = input;
377 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200379 l_input = all_zeroes_input;
380 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100381 }
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 status = psa_key_derivation_setup(&operation,
384 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (status != PSA_SUCCESS) {
387 goto cleanup;
388 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 status = psa_key_derivation_input_bytes(&operation,
391 PSA_KEY_DERIVATION_INPUT_SALT,
392 tmp_secret,
393 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (status != PSA_SUCCESS) {
396 goto cleanup;
397 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 status = psa_key_derivation_input_bytes(&operation,
400 PSA_KEY_DERIVATION_INPUT_SECRET,
401 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (status != PSA_SUCCESS) {
404 goto cleanup;
405 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 status = psa_key_derivation_output_bytes(&operation,
408 secret_new,
409 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (status != PSA_SUCCESS) {
412 goto cleanup;
413 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415cleanup:
416 abort_status = psa_key_derivation_abort(&operation);
417 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500418 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
420 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100421}
422
Xiaofei Bai746f9482021-11-12 08:53:56 +0000423int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 psa_algorithm_t hash_alg,
425 unsigned char const *early_secret,
426 unsigned char const *transcript, size_t transcript_len,
427 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100428{
429 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100431
432 /* We should never call this function with an unknown hash,
433 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (!PSA_ALG_IS_HASH(hash_alg)) {
435 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
436 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100437
438 /*
439 * 0
440 * |
441 * v
442 * PSK -> HKDF-Extract = Early Secret
443 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100444 * +-----> Derive-Secret(., "c e traffic", ClientHello)
445 * | = client_early_traffic_secret
446 * |
447 * +-----> Derive-Secret(., "e exp master", ClientHello)
448 * | = early_exporter_master_secret
449 * v
450 */
451
452 /* Create client_early_traffic_secret */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000453 ret = mbedtls_ssl_tls13_derive_secret(
454 hash_alg,
455 early_secret, hash_len,
456 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
457 transcript, transcript_len,
458 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
459 derived->client_early_traffic_secret,
460 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (ret != 0) {
462 return ret;
463 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100464
465 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000466 ret = mbedtls_ssl_tls13_derive_secret(
467 hash_alg,
468 early_secret, hash_len,
469 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
470 transcript, transcript_len,
471 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
472 derived->early_exporter_master_secret,
473 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (ret != 0) {
475 return ret;
476 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100479}
480
Xiaofei Bai746f9482021-11-12 08:53:56 +0000481int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 psa_algorithm_t hash_alg,
483 unsigned char const *handshake_secret,
484 unsigned char const *transcript, size_t transcript_len,
485 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100486{
487 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100489
490 /* We should never call this function with an unknown hash,
491 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (!PSA_ALG_IS_HASH(hash_alg)) {
493 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
494 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100495
496 /*
497 *
498 * Handshake Secret
499 * |
500 * +-----> Derive-Secret( ., "c hs traffic",
501 * | ClientHello...ServerHello )
502 * | = client_handshake_traffic_secret
503 * |
504 * +-----> Derive-Secret( ., "s hs traffic",
505 * | ClientHello...ServerHello )
506 * | = server_handshake_traffic_secret
507 *
508 */
509
510 /*
511 * Compute client_handshake_traffic_secret with
512 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
513 */
514
Xiaokang Qian123cde82023-03-29 06:54:51 +0000515 ret = mbedtls_ssl_tls13_derive_secret(
516 hash_alg,
517 handshake_secret, hash_len,
518 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
519 transcript, transcript_len,
520 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
521 derived->client_handshake_traffic_secret,
522 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (ret != 0) {
524 return ret;
525 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100526
527 /*
528 * Compute server_handshake_traffic_secret with
529 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
530 */
531
Xiaokang Qian123cde82023-03-29 06:54:51 +0000532 ret = mbedtls_ssl_tls13_derive_secret(
533 hash_alg,
534 handshake_secret, hash_len,
535 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
536 transcript, transcript_len,
537 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
538 derived->server_handshake_traffic_secret,
539 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (ret != 0) {
541 return ret;
542 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100545}
546
Xiaofei Bai746f9482021-11-12 08:53:56 +0000547int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 psa_algorithm_t hash_alg,
549 unsigned char const *application_secret,
550 unsigned char const *transcript, size_t transcript_len,
551 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100552{
553 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100555
556 /* We should never call this function with an unknown hash,
557 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (!PSA_ALG_IS_HASH(hash_alg)) {
559 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
560 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100561
562 /* Generate {client,server}_application_traffic_secret_0
563 *
564 * Master Secret
565 * |
566 * +-----> Derive-Secret( ., "c ap traffic",
567 * | ClientHello...server Finished )
568 * | = client_application_traffic_secret_0
569 * |
570 * +-----> Derive-Secret( ., "s ap traffic",
571 * | ClientHello...Server Finished )
572 * | = server_application_traffic_secret_0
573 * |
574 * +-----> Derive-Secret( ., "exp master",
575 * | ClientHello...server Finished)
576 * | = exporter_master_secret
577 *
578 */
579
Xiaokang Qian123cde82023-03-29 06:54:51 +0000580 ret = mbedtls_ssl_tls13_derive_secret(
581 hash_alg,
582 application_secret, hash_len,
583 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
584 transcript, transcript_len,
585 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
586 derived->client_application_traffic_secret_N,
587 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (ret != 0) {
589 return ret;
590 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100591
Xiaokang Qian123cde82023-03-29 06:54:51 +0000592 ret = mbedtls_ssl_tls13_derive_secret(
593 hash_alg,
594 application_secret, hash_len,
595 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
596 transcript, transcript_len,
597 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
598 derived->server_application_traffic_secret_N,
599 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (ret != 0) {
601 return ret;
602 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100603
Xiaokang Qian123cde82023-03-29 06:54:51 +0000604 ret = mbedtls_ssl_tls13_derive_secret(
605 hash_alg,
606 application_secret, hash_len,
607 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
608 transcript, transcript_len,
609 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
610 derived->exporter_master_secret,
611 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (ret != 0) {
613 return ret;
614 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100617}
618
619/* Generate resumption_master_secret for use with the ticket exchange.
620 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000621 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100622 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000623int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 psa_algorithm_t hash_alg,
625 unsigned char const *application_secret,
626 unsigned char const *transcript, size_t transcript_len,
627 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100628{
629 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100631
632 /* We should never call this function with an unknown hash,
633 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (!PSA_ALG_IS_HASH(hash_alg)) {
635 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
636 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100637
Xiaokang Qian123cde82023-03-29 06:54:51 +0000638 ret = mbedtls_ssl_tls13_derive_secret(
639 hash_alg,
640 application_secret, hash_len,
641 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
642 transcript, transcript_len,
643 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
644 derived->resumption_master_secret,
645 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (ret != 0) {
648 return ret;
649 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100652}
653
Yanray Wang05402112022-12-13 18:50:42 +0800654/**
655 * \brief Transition into application stage of TLS 1.3 key schedule.
656 *
657 * The TLS 1.3 key schedule can be viewed as a simple state machine
658 * with states Initial -> Early -> Handshake -> Application, and
659 * this function represents the Handshake -> Application transition.
660 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800661 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800662 * can be used to derive the handshake traffic keys.
663 *
664 * \param ssl The SSL context to operate on. This must be in key schedule
665 * stage \c Handshake.
666 *
667 * \returns \c 0 on success.
668 * \returns A negative error code on failure.
669 */
670MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800671static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000672{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000674 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200675 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100676 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000677
678 /*
679 * Compute MasterSecret
680 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000681 ret = mbedtls_ssl_tls13_evolve_secret(
682 hash_alg,
683 handshake->tls13_master_secrets.handshake,
684 NULL, 0,
685 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (ret != 0) {
687 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
688 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000689 }
690
Xiaokang Qian123cde82023-03-29 06:54:51 +0000691 MBEDTLS_SSL_DEBUG_BUF(
692 4, "Master secret",
693 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000696}
697
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200698MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100699static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
700 unsigned char const *base_key,
701 unsigned char const *transcript,
702 unsigned char *dst,
703 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100704{
Gabor Mezei07732f72022-03-26 17:04:19 +0100705 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
706 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
707 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100709 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100710 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100711 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100712
713 /* We should never call this function with an unknown hash,
714 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (!PSA_ALG_IS_HASH(hash_alg)) {
716 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
717 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100718
719 /* TLS 1.3 Finished message
720 *
721 * struct {
722 * opaque verify_data[Hash.length];
723 * } Finished;
724 *
725 * verify_data =
726 * HMAC( finished_key,
727 * Hash( Handshake Context +
728 * Certificate* +
729 * CertificateVerify* )
730 * )
731 *
732 * finished_key =
733 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
734 */
735
Xiaofei Bai746f9482021-11-12 08:53:56 +0000736 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 hash_alg, base_key, hash_len,
738 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
739 NULL, 0,
740 finished_key, hash_len);
741 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100742 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100743 }
744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 alg = PSA_ALG_HMAC(hash_alg);
746 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
747 psa_set_key_algorithm(&attributes, alg);
748 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
749
750 status = psa_import_key(&attributes, finished_key, hash_len, &key);
751 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500752 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 goto exit;
754 }
755
756 status = psa_mac_compute(key, alg, transcript, hash_len,
757 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500758 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100759
760exit:
761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 status = psa_destroy_key(key);
763 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500764 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100770}
771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
773 unsigned char *dst,
774 size_t dst_len,
775 size_t *actual_len,
776 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000777{
XiaokangQiana7634982021-10-22 06:32:32 +0000778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000779
XiaokangQianaaa0e192021-11-10 03:07:04 +0000780 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000781 size_t transcript_len;
782
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800783 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800784 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800785 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800787
Dave Rodgman2eab4622023-10-05 13:30:37 +0100788 mbedtls_md_type_t const md_type = (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100789
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200790 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100791 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800797 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
799 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800800 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800802 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800805 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
806 goto exit;
807 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
810 transcript, sizeof(transcript),
811 &transcript_len);
812 if (ret != 0) {
813 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000814 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000815 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000817
Xiaokang Qian123cde82023-03-29 06:54:51 +0000818 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
819 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000821 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
825 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000826
827exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800828 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 mbedtls_platform_zeroize(base_key, base_key_len);
830 mbedtls_platform_zeroize(transcript, sizeof(transcript));
831 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000832}
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
835 const psa_algorithm_t hash_alg,
836 unsigned char const *psk, size_t psk_len,
837 int psk_type,
838 unsigned char const *transcript,
839 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100840{
841 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100842 unsigned char binder_key[PSA_MAC_MAX_SIZE];
843 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100845 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100846
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100847#if !defined(MBEDTLS_DEBUG_C)
848 ssl = NULL; /* make sure we don't use it except for debug */
849 ((void) ssl);
850#endif
851
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100852 /* We should never call this function with an unknown hash,
853 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (!PSA_ALG_IS_HASH(hash_alg)) {
855 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
856 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100857
858 /*
859 * 0
860 * |
861 * v
862 * PSK -> HKDF-Extract = Early Secret
863 * |
864 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
865 * | = binder_key
866 * v
867 */
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
870 NULL, /* Old secret */
871 psk, psk_len, /* Input */
872 early_secret);
873 if (ret != 0) {
874 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100875 goto exit;
876 }
877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
879 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000882 ret = mbedtls_ssl_tls13_derive_secret(
883 hash_alg,
884 early_secret, hash_len,
885 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
886 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
887 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
889 } else {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000890 ret = mbedtls_ssl_tls13_derive_secret(
891 hash_alg,
892 early_secret, hash_len,
893 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
894 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
895 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100897 }
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (ret != 0) {
900 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100901 goto exit;
902 }
903
904 /*
905 * The binding_value is computed in the same way as the Finished message
906 * but with the BaseKey being the binder_key.
907 */
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
910 result, &actual_len);
911 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100912 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100916
917exit:
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
920 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
921 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100922}
923
Xiaokang Qian123cde82023-03-29 06:54:51 +0000924int mbedtls_ssl_tls13_populate_transform(
925 mbedtls_ssl_transform *transform,
926 int endpoint, int ciphersuite,
927 mbedtls_ssl_key_set const *traffic_keys,
928 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000929{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100930#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000931 int ret;
932 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200933#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000934 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
935 unsigned char const *key_enc;
936 unsigned char const *iv_enc;
937 unsigned char const *key_dec;
938 unsigned char const *iv_dec;
939
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100940#if defined(MBEDTLS_USE_PSA_CRYPTO)
941 psa_key_type_t key_type;
942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
943 psa_algorithm_t alg;
944 size_t key_bits;
945 psa_status_t status = PSA_SUCCESS;
946#endif
947
Hanno Beckerc94060c2021-03-22 07:50:44 +0000948#if !defined(MBEDTLS_DEBUG_C)
949 ssl = NULL; /* make sure we don't use it except for those cases */
950 (void) ssl;
951#endif
952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
954 if (ciphersuite_info == NULL) {
955 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
956 ciphersuite));
957 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100958 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000959
Neil Armstronga8093f52022-05-04 17:44:05 +0200960#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
962 if (cipher_info == NULL) {
963 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
964 ciphersuite_info->cipher));
965 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000966 }
967
968 /*
969 * Setup cipher contexts in target transform
970 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
972 cipher_info)) != 0) {
973 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
974 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000975 }
976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
978 cipher_info)) != 0) {
979 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
980 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000981 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100982#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000983
984#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000986 key_enc = traffic_keys->server_write_key;
987 key_dec = traffic_keys->client_write_key;
988 iv_enc = traffic_keys->server_write_iv;
989 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000991#endif /* MBEDTLS_SSL_SRV_C */
992#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000994 key_enc = traffic_keys->client_write_key;
995 key_dec = traffic_keys->server_write_key;
996 iv_enc = traffic_keys->client_write_iv;
997 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000999#endif /* MBEDTLS_SSL_CLI_C */
1000 {
1001 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001003 }
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
1006 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +00001007
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001008#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
Mehmet Cagri Aksoy56e90112023-10-11 15:28:06 +02001010 key_enc, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 MBEDTLS_ENCRYPT)) != 0) {
1012 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1013 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001014 }
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
Mehmet Cagri Aksoy56e90112023-10-11 15:28:06 +02001017 key_dec, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 MBEDTLS_DECRYPT)) != 0) {
1019 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1020 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001021 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001022#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001023
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001024 /*
1025 * Setup other fields in SSL transform
1026 */
1027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001029 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001031 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001033
1034 transform->ivlen = traffic_keys->iv_len;
1035 transform->maclen = 0;
1036 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001037 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001038
1039 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001040 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001041 * type-extended and padded plaintext is therefore the padding
1042 * granularity. */
1043 transform->minlen =
1044 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1045
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001046#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001047 /*
1048 * Setup psa keys and alg
1049 */
Dave Rodgman2eab4622023-10-05 13:30:37 +01001050 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 transform->taglen,
1052 &alg,
1053 &key_type,
1054 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001055 MBEDTLS_SSL_DEBUG_RET(
1056 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001057 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001058 }
1059
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001060 transform->psa_alg = alg;
1061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1063 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1064 psa_set_key_algorithm(&attributes, alg);
1065 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if ((status = psa_import_key(&attributes,
1068 key_enc,
1069 PSA_BITS_TO_BYTES(key_bits),
1070 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001071 MBEDTLS_SSL_DEBUG_RET(
1072 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001073 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001074 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if ((status = psa_import_key(&attributes,
1079 key_dec,
1080 PSA_BITS_TO_BYTES(key_bits),
1081 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001082 MBEDTLS_SSL_DEBUG_RET(
1083 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001084 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001085 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001086 }
1087#endif /* MBEDTLS_USE_PSA_CRYPTO */
1088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001090}
1091
Jerry Yu84a6eda2022-11-04 11:17:35 +08001092MBEDTLS_CHECK_RETURN_CRITICAL
1093static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1095 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001096{
1097 psa_key_type_t key_type;
1098 psa_algorithm_t alg;
1099 size_t taglen;
1100 size_t key_bits;
1101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001104 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001106 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001108
Dave Rodgman2eab4622023-10-05 13:30:37 +01001109 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, taglen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 &alg, &key_type, &key_bits);
1111 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001112 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001116
1117 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1118 *iv_len = 12;
1119
1120 return 0;
1121}
1122
Jerry Yu91b560f2022-11-04 14:10:34 +08001123#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001124/*
1125 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001126 * the early application data and handshake messages as described in section 7
1127 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001128 *
Jerry Yue31688b2022-11-22 21:55:56 +08001129 * NOTE: Only one key is generated, the key for the traffic from the client to
1130 * the server. The TLS 1.3 specification does not define a secret and thus
1131 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001132 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001133MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001134static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1135 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001136{
1137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001138 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001139 psa_algorithm_t hash_alg;
1140 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001141 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1142 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001143 size_t key_len = 0;
1144 size_t iv_len = 0;
Yanray Wang16c895d2022-12-15 15:14:35 +08001145 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001146
1147 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001148 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1149 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1154 if (ret != 0) {
1155 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001156 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001157 }
1158
Dave Rodgman2eab4622023-10-05 13:30:37 +01001159 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Jerry Yu91b560f2022-11-04 14:10:34 +08001160
Dave Rodgman2eab4622023-10-05 13:30:37 +01001161 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1165 transcript,
1166 sizeof(transcript),
1167 &transcript_len);
1168 if (ret != 0) {
1169 MBEDTLS_SSL_DEBUG_RET(1,
1170 "mbedtls_ssl_get_handshake_transcript",
1171 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001172 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001173 }
1174
Jerry Yub094e122022-11-21 13:03:47 +08001175 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001177 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001179 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001181 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001182 }
1183
1184 MBEDTLS_SSL_DEBUG_BUF(
1185 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001186 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001187
1188 /*
1189 * Export client handshake traffic secret
1190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001192 ssl->f_export_keys(
1193 ssl->p_export_keys,
1194 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001195 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001196 hash_len,
1197 handshake->randbytes,
1198 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001200 }
1201
Jerry Yua8771832022-11-21 23:16:54 +08001202 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001204 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 hash_len, traffic_keys->client_write_key, key_len,
1206 traffic_keys->client_write_iv, iv_len);
1207 if (ret != 0) {
1208 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001209 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001210 }
Jerry Yua8771832022-11-21 23:16:54 +08001211 traffic_keys->key_len = key_len;
1212 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1215 traffic_keys->client_write_key,
1216 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1219 traffic_keys->client_write_iv,
1220 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001223
Jerry Yu3d78e082022-11-23 18:26:20 +08001224cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001225 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001226 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001227 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1229 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001230}
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001233{
1234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1235 mbedtls_ssl_key_set traffic_keys;
1236 mbedtls_ssl_transform *transform_earlydata = NULL;
1237 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1238
1239 /* Next evolution in key schedule: Establish early_data secret and
1240 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1242 if (ret != 0) {
1243 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1244 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001245 goto cleanup;
1246 }
1247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1249 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001250 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1251 goto cleanup;
1252 }
1253
1254 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 transform_earlydata,
1256 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001257 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 &traffic_keys,
1259 ssl);
1260 if (ret != 0) {
1261 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001262 goto cleanup;
1263 }
1264 handshake->transform_earlydata = transform_earlydata;
1265
1266cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1268 if (ret != 0) {
1269 mbedtls_free(transform_earlydata);
1270 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001273}
1274#endif /* MBEDTLS_SSL_EARLY_DATA */
1275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001277{
Jerry Yue3131ef2021-09-16 13:14:15 +08001278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001279 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001280 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001281 unsigned char *psk = NULL;
1282 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (handshake->ciphersuite_info == NULL) {
1285 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1286 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001287 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001288
Dave Rodgman2eab4622023-10-05 13:30:37 +01001289 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001290#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1292 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1293 if (ret != 0) {
1294 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1295 ret);
1296 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001297 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001298 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001299#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1302 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001303#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001304 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001306#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (ret != 0) {
1308 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1309 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001310 }
1311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1313 handshake->tls13_master_secrets.early,
1314 PSA_HASH_LENGTH(hash_alg));
1315 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001316}
1317
Yanray Wang05402112022-12-13 18:50:42 +08001318/**
1319 * \brief Compute TLS 1.3 handshake traffic keys.
1320 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001321 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1322 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001323 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001324 *
1325 * \param ssl The SSL context to operate on. This must be in
1326 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001327 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001328 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001329 * keys. This must be writable but may be uninitialized.
1330 *
1331 * \returns \c 0 on success.
1332 * \returns A negative error code on failure.
1333 */
1334MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001335static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1336 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001337{
1338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001339 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001340 psa_algorithm_t hash_alg;
1341 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001342 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001343 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001344 size_t key_len = 0;
1345 size_t iv_len = 0;
Jerry Yu61e35e02021-09-16 18:59:08 +08001346
Jerry Yu435208a2021-10-13 11:22:16 +08001347 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001348 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1349 handshake->ciphersuite_info;
1350 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1351 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001352
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001353 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1356 if (ret != 0) {
1357 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001358 return ret;
1359 }
1360
Dave Rodgman2eab4622023-10-05 13:30:37 +01001361 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001362
Dave Rodgman2eab4622023-10-05 13:30:37 +01001363 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1367 transcript,
1368 sizeof(transcript),
1369 &transcript_len);
1370 if (ret != 0) {
1371 MBEDTLS_SSL_DEBUG_RET(1,
1372 "mbedtls_ssl_get_handshake_transcript",
1373 ret);
1374 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001375 }
1376
Xiaokang Qian123cde82023-03-29 06:54:51 +00001377 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1378 hash_alg, handshake->tls13_master_secrets.handshake,
1379 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if (ret != 0) {
1381 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1382 ret);
1383 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001384 }
1385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1387 tls13_hs_secrets->client_handshake_traffic_secret,
1388 hash_len);
1389 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1390 tls13_hs_secrets->server_handshake_traffic_secret,
1391 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001392
1393 /*
1394 * Export client handshake traffic secret
1395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001397 ssl->f_export_keys(
1398 ssl->p_export_keys,
1399 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1400 tls13_hs_secrets->client_handshake_traffic_secret,
1401 hash_len,
1402 handshake->randbytes,
1403 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1404 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001405
Xiaokang Qian123cde82023-03-29 06:54:51 +00001406 ssl->f_export_keys(
1407 ssl->p_export_keys,
1408 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1409 tls13_hs_secrets->server_handshake_traffic_secret,
1410 hash_len,
1411 handshake->randbytes,
1412 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1413 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001414 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001415
Xiaokang Qian123cde82023-03-29 06:54:51 +00001416 ret = mbedtls_ssl_tls13_make_traffic_keys(
1417 hash_alg,
1418 tls13_hs_secrets->client_handshake_traffic_secret,
1419 tls13_hs_secrets->server_handshake_traffic_secret,
1420 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if (ret != 0) {
1422 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001423 goto exit;
1424 }
1425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1427 traffic_keys->client_write_key,
1428 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1431 traffic_keys->server_write_key,
1432 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1435 traffic_keys->client_write_iv,
1436 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1439 traffic_keys->server_write_iv,
1440 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001441
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001442 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001443
1444exit:
1445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001447}
1448
Yanray Wang05402112022-12-13 18:50:42 +08001449/**
1450 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1451 *
1452 * The TLS 1.3 key schedule can be viewed as a simple state machine
1453 * with states Initial -> Early -> Handshake -> Application, and
1454 * this function represents the Early -> Handshake transition.
1455 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001456 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001457 * can be used to derive the handshake traffic keys.
1458 *
1459 * \param ssl The SSL context to operate on. This must be in key schedule
1460 * stage \c Early.
1461 *
1462 * \returns \c 0 on success.
1463 * \returns A negative error code on failure.
1464 */
1465MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001466static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001467{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001469 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001470 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001471 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001472 unsigned char *shared_secret = NULL;
1473 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001474
Ronald Crona2900bc2022-10-20 14:37:35 +02001475#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001476 /*
1477 * Compute ECDHE secret used to compute the handshake secret from which
1478 * client_handshake_traffic_secret and server_handshake_traffic_secret
1479 * are derived in the handshake secret derivation stage.
1480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001482 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001483 mbedtls_ssl_tls13_named_group_is_ffdh(handshake->offered_group_id)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001484#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001485 psa_algorithm_t alg =
1486 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1487 PSA_ALG_ECDH : PSA_ALG_FFDH;
1488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001490 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001491 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1492
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001493 status = psa_get_key_attributes(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 &key_attributes);
1495 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001496 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 }
Ronald Cron3b056202022-10-05 17:20:21 +02001498
1499 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 psa_get_key_bits(&key_attributes));
1501 shared_secret = mbedtls_calloc(1, shared_secret_len);
1502 if (shared_secret == NULL) {
1503 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1504 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001505
Ronald Cron4c7edb22022-10-05 15:37:11 +02001506 status = psa_raw_key_agreement(
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001507 alg, handshake->xxdh_psa_privkey,
1508 handshake->xxdh_psa_peerkey, handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 shared_secret, shared_secret_len, &shared_secret_len);
1510 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001511 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001513 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001514 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001515
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001516 status = psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001518 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001520 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001521 }
1522
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001523 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001524#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 } else {
1526 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1527 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001528 }
1529 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001530#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001531
1532 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001533 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001534 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001535 ret = mbedtls_ssl_tls13_evolve_secret(
1536 hash_alg, handshake->tls13_master_secrets.early,
1537 shared_secret, shared_secret_len,
1538 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (ret != 0) {
1540 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001541 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001542 }
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1545 handshake->tls13_master_secrets.handshake,
1546 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001547
Ronald Cron3b056202022-10-05 17:20:21 +02001548cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 if (shared_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001550 mbedtls_zeroize_and_free(shared_secret, shared_secret_len);
Ronald Cron3b056202022-10-05 17:20:21 +02001551 }
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001554}
1555
Yanray Wang05402112022-12-13 18:50:42 +08001556/**
1557 * \brief Compute TLS 1.3 application traffic keys.
1558 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001559 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001560 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001561 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001562 *
1563 * \param ssl The SSL context to operate on. This must be in
1564 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001565 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001566 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001567 * keys. This must be writable but may be uninitialized.
1568 *
1569 * \returns \c 0 on success.
1570 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001571 */
Yanray Wang05402112022-12-13 18:50:42 +08001572MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001573static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 mbedtls_ssl_context *ssl,
1575 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001576{
XiaokangQiana7634982021-10-22 06:32:32 +00001577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001578 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001579
1580 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001581 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001582 &ssl->session_negotiate->app_secrets;
1583
1584 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001585 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001586 size_t transcript_len;
1587
1588 /* Variables relating to the hash for the chosen ciphersuite. */
1589 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001590
1591 psa_algorithm_t hash_alg;
1592 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001593
1594 /* Variables relating to the cipher for the chosen ciphersuite. */
Paul Elliott88600212023-11-23 14:24:30 +00001595 size_t key_len = 0, iv_len = 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001598
1599 /* Extract basic information about hash and ciphersuite */
1600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1602 &key_len, &iv_len);
1603 if (ret != 0) {
1604 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001605 goto cleanup;
1606 }
1607
Dave Rodgman2eab4622023-10-05 13:30:37 +01001608 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001609
Dave Rodgman2eab4622023-10-05 13:30:37 +01001610 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001612
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001613 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001614 * to call this at the right time, that is, after the ServerFinished. */
1615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1617 transcript, sizeof(transcript),
1618 &transcript_len);
1619 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001620 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001622
1623 /* Compute application secrets from master secret and transcript hash. */
1624
Xiaokang Qian123cde82023-03-29 06:54:51 +00001625 ret = mbedtls_ssl_tls13_derive_application_secrets(
1626 hash_alg, handshake->tls13_master_secrets.app,
1627 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001629 MBEDTLS_SSL_DEBUG_RET(
1630 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001631 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001632 }
1633
1634 /* Derive first epoch of IV + Key for application traffic. */
1635
Xiaokang Qian123cde82023-03-29 06:54:51 +00001636 ret = mbedtls_ssl_tls13_make_traffic_keys(
1637 hash_alg,
1638 app_secrets->client_application_traffic_secret_N,
1639 app_secrets->server_application_traffic_secret_N,
1640 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if (ret != 0) {
1642 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001643 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001644 }
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1647 app_secrets->client_application_traffic_secret_N,
1648 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1651 app_secrets->server_application_traffic_secret_N,
1652 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001653
XiaokangQianac0385c2021-11-03 06:40:11 +00001654 /*
1655 * Export client/server application traffic secret 0
1656 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001658 ssl->f_export_keys(
1659 ssl->p_export_keys,
1660 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1661 app_secrets->client_application_traffic_secret_N, hash_len,
1662 handshake->randbytes,
1663 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1664 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1665 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001666
Xiaokang Qian123cde82023-03-29 06:54:51 +00001667 ssl->f_export_keys(
1668 ssl->p_export_keys,
1669 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1670 app_secrets->server_application_traffic_secret_N, hash_len,
1671 handshake->randbytes,
1672 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1673 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1674 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001675 }
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1678 traffic_keys->client_write_key, key_len);
1679 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1680 traffic_keys->server_write_key, key_len);
1681 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1682 traffic_keys->client_write_iv, iv_len);
1683 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1684 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001689 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1691 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1694 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001695}
1696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001698{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001700 mbedtls_ssl_key_set traffic_keys;
1701 mbedtls_ssl_transform *transform_handshake = NULL;
1702 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1703
1704 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001705 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (ret != 0) {
1707 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001708 goto cleanup;
1709 }
1710
1711 /* Next evolution in key schedule: Establish handshake secret and
1712 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001713 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001715 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001717 goto cleanup;
1718 }
1719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1721 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001722 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1723 goto cleanup;
1724 }
1725
Jerry Yuef2b98a2022-05-06 16:40:05 +08001726 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 transform_handshake,
1728 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001729 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 &traffic_keys,
1731 ssl);
1732 if (ret != 0) {
1733 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001734 goto cleanup;
1735 }
1736 handshake->transform_handshake = transform_handshake;
1737
1738cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1740 if (ret != 0) {
1741 mbedtls_free(transform_handshake);
1742 }
Jerry Yue110d252022-05-05 10:19:22 +08001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001745}
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001748{
Jerry Yu46bffe02022-09-13 11:25:28 +08001749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001750 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001751 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1752 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001753 size_t transcript_len;
1754
Xiaokang Qian123cde82023-03-29 06:54:51 +00001755 MBEDTLS_SSL_DEBUG_MSG(
1756 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001757
Dave Rodgman2eab4622023-10-05 13:30:37 +01001758 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1761 transcript, sizeof(transcript),
1762 &transcript_len);
1763 if (ret != 0) {
1764 return ret;
1765 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001766
1767 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001768 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 handshake->tls13_master_secrets.app,
1770 transcript, transcript_len,
1771 &ssl->session_negotiate->app_secrets);
1772 if (ret != 0) {
1773 return ret;
1774 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001775
Jerry Yuff226982022-04-16 16:52:57 +08001776 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1778 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001779
Xiaokang Qian123cde82023-03-29 06:54:51 +00001780 MBEDTLS_SSL_DEBUG_BUF(
1781 4, "Resumption master secret",
1782 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001783 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001784
Xiaokang Qian123cde82023-03-29 06:54:51 +00001785 MBEDTLS_SSL_DEBUG_MSG(
1786 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001788}
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001791{
1792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1793 mbedtls_ssl_key_set traffic_keys;
1794 mbedtls_ssl_transform *transform_application = NULL;
1795
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001796 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if (ret != 0) {
1798 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001799 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001800 goto cleanup;
1801 }
1802
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001803 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 if (ret != 0) {
1805 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001806 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001807 goto cleanup;
1808 }
1809
1810 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1812 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001813 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1814 goto cleanup;
1815 }
1816
1817 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 transform_application,
1819 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001820 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 &traffic_keys,
1822 ssl);
1823 if (ret != 0) {
1824 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001825 goto cleanup;
1826 }
1827
1828 ssl->transform_application = transform_application;
1829
1830cleanup:
1831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1833 if (ret != 0) {
1834 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001835 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001837}
1838
Ronald Cron41a443a2022-10-04 16:38:25 +02001839#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001840int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1841 unsigned char **psk,
1842 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001843{
Jerry Yu40f37712022-07-26 16:58:57 +08001844#if defined(MBEDTLS_USE_PSA_CRYPTO)
1845 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001846 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001847
1848 *psk_len = 0;
1849 *psk = NULL;
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1852 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001853 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001854
1855 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1856 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001857 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 }
1859
1860 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1861 *psk = mbedtls_calloc(1, *psk_len);
1862 if (*psk == NULL) {
1863 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1864 }
1865
1866 status = psa_export_key(ssl->handshake->psk_opaque,
1867 (uint8_t *) *psk, *psk_len, psk_len);
1868 if (status != PSA_SUCCESS) {
1869 mbedtls_free((void *) *psk);
1870 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001871 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 }
1873 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001874#else
1875 *psk = ssl->handshake->psk;
1876 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if (*psk == NULL) {
1878 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1879 }
1880 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001881#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001882}
Ronald Cron41a443a2022-10-04 16:38:25 +02001883#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001884
Max Fillinger1b0e2e92025-04-16 14:35:24 +02001885#if defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT)
Max Fillinger44042f02024-07-22 14:43:56 +02001886int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
1887 const unsigned char *secret, const size_t secret_len,
1888 const unsigned char *label, const size_t label_len,
1889 const unsigned char *context_value, const size_t context_len,
1890 unsigned char *out, const size_t out_len)
1891{
1892 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
1893 unsigned char hkdf_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Max Fillinger44042f02024-07-22 14:43:56 +02001894 int ret = 0;
Max Fillinger44042f02024-07-22 14:43:56 +02001895
1896 ret = mbedtls_ssl_tls13_derive_secret(hash_alg, secret, secret_len, label, label_len, NULL, 0,
Max Fillinger9359f4d2024-09-21 10:48:57 +02001897 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, hkdf_secret,
1898 hash_len);
Max Fillinger44042f02024-07-22 14:43:56 +02001899 if (ret != 0) {
1900 goto exit;
1901 }
Max Fillinger9359f4d2024-09-21 10:48:57 +02001902 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
1903 hkdf_secret,
1904 hash_len,
Max Fillinger404f7a32024-08-12 11:20:39 +02001905 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter),
Max Fillinger9359f4d2024-09-21 10:48:57 +02001906 context_value,
1907 context_len,
1908 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
1909 out,
1910 out_len);
Max Fillinger44042f02024-07-22 14:43:56 +02001911
1912exit:
1913 mbedtls_platform_zeroize(hkdf_secret, sizeof(hkdf_secret));
1914 return ret;
1915}
Max Fillinger1b0e2e92025-04-16 14:35:24 +02001916#endif /* defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) */
Max Fillinger44042f02024-07-22 14:43:56 +02001917
Ronald Cron6f135e12021-12-08 16:57:54 +01001918#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */