blob: 00297af3b09e4c430e2b4600d94f56d2b664a068 [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
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "ssl_misc.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
Valerio Settib4f50762024-01-17 10:24:52 +010015#include "debug_internal.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080016#include "mbedtls/error.h"
Jerry Yue110d252022-05-05 10:19:22 +080017#include "mbedtls/platform.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080018
Jerry Yue3131ef2021-09-16 13:14:15 +080019#include "ssl_tls13_keys.h"
Gabor Mezeia3eecd22022-02-09 16:57:26 +010020#include "ssl_tls13_invasive.h"
21
22#include "psa/crypto.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010023#include "mbedtls/psa_util.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080024
Andrzej Kurek00644842023-05-30 05:45:00 -040025/* Define a local translating function to save code size by not using too many
26 * arguments in each translating place. */
27static int local_err_translation(psa_status_t status)
28{
29 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040030 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040031 psa_generic_status_to_mbedtls);
32}
33#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050034
Gilles Peskine449bd832023-01-11 14:50:10 +010035#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010036 .name = string,
37
Xiaofei Bai746f9482021-11-12 08:53:56 +000038struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010039{
40 /* This seems to work in C, despite the string literal being one
41 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010042 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010043};
44
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010045#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010046
Hanno Beckerbe9d6642020-08-21 13:20:06 +010047/*
48 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
49 *
50 * The HkdfLabel is specified in RFC 8446 as follows:
51 *
52 * struct HkdfLabel {
53 * uint16 length; // Length of expanded key material
54 * opaque label<7..255>; // Always prefixed by "tls13 "
55 * opaque context<0..255>; // Usually a communication transcript hash
56 * };
57 *
58 * Parameters:
Max Fillingeree33b312024-12-02 19:26:13 +010059 * - desired_length: Length of expanded key material.
60 * As the type implies, this must be less than 2**16 bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000061 * - (label, label_len): label + label length, without "tls13 " prefix
62 * The label length MUST be less than or equal to
Max Fillinger58268832024-11-25 20:38:04 +010063 * MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000064 * It is the caller's responsibility to ensure this.
65 * All (label, label length) pairs used in TLS 1.3
66 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
67 * - (ctx, ctx_len): context + context length
68 * The context length MUST be less than or equal to
69 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
70 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010071 * - dst: Target buffer for HkdfLabel structure,
72 * This MUST be a writable buffer of size
73 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000074 * - dst_len: Pointer at which to store the actual length of
75 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010076 */
77
Xiaofei Baid25fab62021-12-02 06:36:27 +000078static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010079
Gilles Peskine449bd832023-01-11 14:50:10 +010080#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
81 (2 /* expansion length */ \
82 + 1 /* label length */ \
83 + label_len \
84 + 1 /* context length */ \
85 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010086
87#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
88 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +010089 sizeof(tls13_label_prefix) + \
Max Fillinger58268832024-11-25 20:38:04 +010090 MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN, \
Gilles Peskine449bd832023-01-11 14:50:10 +010091 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010092
Xiaofei Bai746f9482021-11-12 08:53:56 +000093static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +010094 size_t desired_length,
95 const unsigned char *label, size_t label_len,
96 const unsigned char *ctx, size_t ctx_len,
97 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010098{
Hanno Becker2dfe1322020-09-10 09:23:12 +010099 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000100 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100101 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100103
104 unsigned char *p = dst;
105
Max Fillinger28916ac2024-10-29 18:49:30 +0100106 /* Add the size of the expanded key material. */
107#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > UINT16_MAX
108#error "The desired key length must fit into an uint16 but \
109 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN is greater than UINT16_MAX"
Hanno Becker531fe302020-09-16 09:45:27 +0100110#endif
111
Max Fillinger28916ac2024-10-29 18:49:30 +0100112 *p++ = MBEDTLS_BYTE_1(desired_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100114
115 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 *p++ = MBEDTLS_BYTE_0(total_label_len);
117 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000118 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000120 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100121
122 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 *p++ = MBEDTLS_BYTE_0(ctx_len);
124 if (ctx_len != 0) {
125 memcpy(p, ctx, ctx_len);
126 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100127
128 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000129 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100130}
131
Xiaofei Bai746f9482021-11-12 08:53:56 +0000132int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 psa_algorithm_t hash_alg,
134 const unsigned char *secret, size_t secret_len,
135 const unsigned char *label, size_t label_len,
136 const unsigned char *ctx, size_t ctx_len,
137 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100138{
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200140 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
142 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200143 psa_key_derivation_operation_t operation =
144 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100145
Max Fillinger58268832024-11-25 20:38:04 +0100146 if (label_len > MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100147 /* Should never happen since this is an internal
148 * function, and we know statically which labels
149 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100151 }
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100154 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100156 }
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100159 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100161 }
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (!PSA_ALG_IS_HASH(hash_alg)) {
164 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
165 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 ssl_tls13_hkdf_encode_label(buf_len,
168 label, label_len,
169 ctx, ctx_len,
170 hkdf_label,
171 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (status != PSA_SUCCESS) {
176 goto cleanup;
177 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 status = psa_key_derivation_input_bytes(&operation,
180 PSA_KEY_DERIVATION_INPUT_SECRET,
181 secret,
182 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (status != PSA_SUCCESS) {
185 goto cleanup;
186 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 status = psa_key_derivation_input_bytes(&operation,
189 PSA_KEY_DERIVATION_INPUT_INFO,
190 hkdf_label,
191 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (status != PSA_SUCCESS) {
194 goto cleanup;
195 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 status = psa_key_derivation_output_bytes(&operation,
198 buf,
199 buf_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
205cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 abort_status = psa_key_derivation_abort(&operation);
207 status = (status == PSA_SUCCESS ? abort_status : status);
208 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500209 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100210}
211
Jerry Yua5db6c02022-11-23 18:08:04 +0800212MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800213static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 psa_algorithm_t hash_alg,
215 const unsigned char *secret, size_t secret_len,
216 unsigned char *key, size_t key_len,
217 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800218{
219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
220
Jerry Yuaec08b32022-11-29 15:19:27 +0800221 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 hash_alg,
223 secret, secret_len,
224 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
225 NULL, 0,
226 key, key_len);
227 if (ret != 0) {
228 return ret;
229 }
Jerry Yua8771832022-11-21 23:16:54 +0800230
Jerry Yuaec08b32022-11-29 15:19:27 +0800231 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 hash_alg,
233 secret, secret_len,
234 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
235 NULL, 0,
236 iv, iv_len);
237 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800238}
239
Hanno Becker3385a4d2020-08-21 13:03:34 +0100240/*
241 * The traffic keying material is generated from the following inputs:
242 *
243 * - One secret value per sender.
244 * - A purpose value indicating the specific value being generated
245 * - The desired lengths of key and IV.
246 *
247 * The expansion itself is based on HKDF:
248 *
249 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
250 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
251 *
252 * [sender] denotes the sending side and the Secret value is provided
253 * by the function caller. Note that we generate server and client side
254 * keys in a single function call.
255 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000256int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 psa_algorithm_t hash_alg,
258 const unsigned char *client_secret,
259 const unsigned char *server_secret, size_t secret_len,
260 size_t key_len, size_t iv_len,
261 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100262{
263 int ret = 0;
264
Jerry Yua8771832022-11-21 23:16:54 +0800265 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 hash_alg, client_secret, secret_len,
267 keys->client_write_key, key_len,
268 keys->client_write_iv, iv_len);
269 if (ret != 0) {
270 return ret;
271 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100272
Jerry Yua8771832022-11-21 23:16:54 +0800273 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 hash_alg, server_secret, secret_len,
275 keys->server_write_key, key_len,
276 keys->server_write_iv, iv_len);
277 if (ret != 0) {
278 return ret;
279 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100280
Hanno Becker493ea7f2020-09-08 11:01:00 +0100281 keys->key_len = key_len;
282 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100285}
286
Xiaofei Bai746f9482021-11-12 08:53:56 +0000287int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 psa_algorithm_t hash_alg,
289 const unsigned char *secret, size_t secret_len,
290 const unsigned char *label, size_t label_len,
291 const unsigned char *ctx, size_t ctx_len,
292 int ctx_hashed,
293 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100294{
295 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
297 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100298 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
301 PSA_HASH_LENGTH(hash_alg), &ctx_len);
302 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500303 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100304 return ret;
305 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 } else {
307 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100308 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100309 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100310 * Let's double-check nonetheless to not run at the risk
311 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100313 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
319 secret, secret_len,
320 label, label_len,
321 hashed_context, ctx_len,
322 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100323
Hanno Beckerb35d5222020-08-21 13:27:44 +0100324}
325
Xiaofei Bai746f9482021-11-12 08:53:56 +0000326int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 psa_algorithm_t hash_alg,
328 const unsigned char *secret_old,
329 const unsigned char *input, size_t input_len,
330 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100331{
332 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
334 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200335 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
337 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200338 const unsigned char *l_input = NULL;
339 size_t l_input_len;
340
Przemek Stekield5ae3652022-05-13 12:10:08 +0200341 psa_key_derivation_operation_t operation =
342 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (!PSA_ALG_IS_HASH(hash_alg)) {
345 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
346 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100349
350 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100351 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000353 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 hash_alg,
355 secret_old, hlen,
356 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
357 NULL, 0, /* context */
358 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
359 tmp_secret, hlen);
360 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100361 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100363 }
364
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200365 ret = 0;
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200368 l_input = input;
369 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200371 l_input = all_zeroes_input;
372 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100373 }
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 status = psa_key_derivation_setup(&operation,
376 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (status != PSA_SUCCESS) {
379 goto cleanup;
380 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 status = psa_key_derivation_input_bytes(&operation,
383 PSA_KEY_DERIVATION_INPUT_SALT,
384 tmp_secret,
385 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (status != PSA_SUCCESS) {
388 goto cleanup;
389 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 status = psa_key_derivation_input_bytes(&operation,
392 PSA_KEY_DERIVATION_INPUT_SECRET,
393 l_input, l_input_len);
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_output_bytes(&operation,
400 secret_new,
401 PSA_HASH_LENGTH(hash_alg));
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 +0100407cleanup:
408 abort_status = psa_key_derivation_abort(&operation);
409 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500410 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
412 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100413}
414
Xiaofei Bai746f9482021-11-12 08:53:56 +0000415int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 psa_algorithm_t hash_alg,
417 unsigned char const *early_secret,
418 unsigned char const *transcript, size_t transcript_len,
419 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100420{
421 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100423
424 /* We should never call this function with an unknown hash,
425 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (!PSA_ALG_IS_HASH(hash_alg)) {
427 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
428 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100429
430 /*
431 * 0
432 * |
433 * v
434 * PSK -> HKDF-Extract = Early Secret
435 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100436 * +-----> Derive-Secret(., "c e traffic", ClientHello)
437 * | = client_early_traffic_secret
438 * |
439 * +-----> Derive-Secret(., "e exp master", ClientHello)
440 * | = early_exporter_master_secret
441 * v
442 */
443
444 /* Create client_early_traffic_secret */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000445 ret = mbedtls_ssl_tls13_derive_secret(
446 hash_alg,
447 early_secret, hash_len,
448 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
449 transcript, transcript_len,
450 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
451 derived->client_early_traffic_secret,
452 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (ret != 0) {
454 return ret;
455 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100456
457 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000458 ret = mbedtls_ssl_tls13_derive_secret(
459 hash_alg,
460 early_secret, hash_len,
461 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
462 transcript, transcript_len,
463 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
464 derived->early_exporter_master_secret,
465 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (ret != 0) {
467 return ret;
468 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100471}
472
Xiaofei Bai746f9482021-11-12 08:53:56 +0000473int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 psa_algorithm_t hash_alg,
475 unsigned char const *handshake_secret,
476 unsigned char const *transcript, size_t transcript_len,
477 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100478{
479 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100481
482 /* We should never call this function with an unknown hash,
483 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (!PSA_ALG_IS_HASH(hash_alg)) {
485 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
486 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100487
488 /*
489 *
490 * Handshake Secret
491 * |
492 * +-----> Derive-Secret( ., "c hs traffic",
493 * | ClientHello...ServerHello )
494 * | = client_handshake_traffic_secret
495 * |
496 * +-----> Derive-Secret( ., "s hs traffic",
497 * | ClientHello...ServerHello )
498 * | = server_handshake_traffic_secret
499 *
500 */
501
502 /*
503 * Compute client_handshake_traffic_secret with
504 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
505 */
506
Xiaokang Qian123cde82023-03-29 06:54:51 +0000507 ret = mbedtls_ssl_tls13_derive_secret(
508 hash_alg,
509 handshake_secret, hash_len,
510 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
511 transcript, transcript_len,
512 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
513 derived->client_handshake_traffic_secret,
514 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (ret != 0) {
516 return ret;
517 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100518
519 /*
520 * Compute server_handshake_traffic_secret with
521 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
522 */
523
Xiaokang Qian123cde82023-03-29 06:54:51 +0000524 ret = mbedtls_ssl_tls13_derive_secret(
525 hash_alg,
526 handshake_secret, hash_len,
527 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
528 transcript, transcript_len,
529 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
530 derived->server_handshake_traffic_secret,
531 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (ret != 0) {
533 return ret;
534 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100537}
538
Xiaofei Bai746f9482021-11-12 08:53:56 +0000539int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 psa_algorithm_t hash_alg,
541 unsigned char const *application_secret,
542 unsigned char const *transcript, size_t transcript_len,
543 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100544{
545 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100547
548 /* We should never call this function with an unknown hash,
549 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (!PSA_ALG_IS_HASH(hash_alg)) {
551 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
552 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100553
554 /* Generate {client,server}_application_traffic_secret_0
555 *
556 * Master Secret
557 * |
558 * +-----> Derive-Secret( ., "c ap traffic",
559 * | ClientHello...server Finished )
560 * | = client_application_traffic_secret_0
561 * |
562 * +-----> Derive-Secret( ., "s ap traffic",
563 * | ClientHello...Server Finished )
564 * | = server_application_traffic_secret_0
565 * |
566 * +-----> Derive-Secret( ., "exp master",
567 * | ClientHello...server Finished)
568 * | = exporter_master_secret
569 *
570 */
571
Xiaokang Qian123cde82023-03-29 06:54:51 +0000572 ret = mbedtls_ssl_tls13_derive_secret(
573 hash_alg,
574 application_secret, hash_len,
575 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
576 transcript, transcript_len,
577 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
578 derived->client_application_traffic_secret_N,
579 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (ret != 0) {
581 return ret;
582 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100583
Xiaokang Qian123cde82023-03-29 06:54:51 +0000584 ret = mbedtls_ssl_tls13_derive_secret(
585 hash_alg,
586 application_secret, hash_len,
587 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
588 transcript, transcript_len,
589 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
590 derived->server_application_traffic_secret_N,
591 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if (ret != 0) {
593 return ret;
594 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100595
Xiaokang Qian123cde82023-03-29 06:54:51 +0000596 ret = mbedtls_ssl_tls13_derive_secret(
597 hash_alg,
598 application_secret, hash_len,
599 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
600 transcript, transcript_len,
601 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
602 derived->exporter_master_secret,
603 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (ret != 0) {
605 return ret;
606 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100609}
610
611/* Generate resumption_master_secret for use with the ticket exchange.
612 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000613 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100614 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000615int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 psa_algorithm_t hash_alg,
617 unsigned char const *application_secret,
618 unsigned char const *transcript, size_t transcript_len,
619 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100620{
621 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100623
624 /* We should never call this function with an unknown hash,
625 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (!PSA_ALG_IS_HASH(hash_alg)) {
627 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
628 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100629
Xiaokang Qian123cde82023-03-29 06:54:51 +0000630 ret = mbedtls_ssl_tls13_derive_secret(
631 hash_alg,
632 application_secret, hash_len,
633 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
634 transcript, transcript_len,
635 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
636 derived->resumption_master_secret,
637 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (ret != 0) {
640 return ret;
641 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100644}
645
Yanray Wang05402112022-12-13 18:50:42 +0800646/**
647 * \brief Transition into application stage of TLS 1.3 key schedule.
648 *
649 * The TLS 1.3 key schedule can be viewed as a simple state machine
650 * with states Initial -> Early -> Handshake -> Application, and
651 * this function represents the Handshake -> Application transition.
652 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800653 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800654 * can be used to derive the handshake traffic keys.
655 *
656 * \param ssl The SSL context to operate on. This must be in key schedule
657 * stage \c Handshake.
658 *
659 * \returns \c 0 on success.
660 * \returns A negative error code on failure.
661 */
662MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800663static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000664{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000665 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000666 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200667 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100668 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000669
670 /*
671 * Compute MasterSecret
672 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000673 ret = mbedtls_ssl_tls13_evolve_secret(
674 hash_alg,
675 handshake->tls13_master_secrets.handshake,
676 NULL, 0,
677 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (ret != 0) {
679 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
680 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000681 }
682
Xiaokang Qian123cde82023-03-29 06:54:51 +0000683 MBEDTLS_SSL_DEBUG_BUF(
684 4, "Master secret",
685 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000688}
689
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200690MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100691static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
692 unsigned char const *base_key,
693 unsigned char const *transcript,
694 unsigned char *dst,
695 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100696{
Gabor Mezei07732f72022-03-26 17:04:19 +0100697 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
699 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100701 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100702 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100703 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100704
705 /* We should never call this function with an unknown hash,
706 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (!PSA_ALG_IS_HASH(hash_alg)) {
708 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
709 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100710
711 /* TLS 1.3 Finished message
712 *
713 * struct {
714 * opaque verify_data[Hash.length];
715 * } Finished;
716 *
717 * verify_data =
718 * HMAC( finished_key,
719 * Hash( Handshake Context +
720 * Certificate* +
721 * CertificateVerify* )
722 * )
723 *
724 * finished_key =
725 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
726 */
727
Xiaofei Bai746f9482021-11-12 08:53:56 +0000728 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 hash_alg, base_key, hash_len,
730 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
731 NULL, 0,
732 finished_key, hash_len);
733 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100734 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100735 }
736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 alg = PSA_ALG_HMAC(hash_alg);
738 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
739 psa_set_key_algorithm(&attributes, alg);
740 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
741
742 status = psa_import_key(&attributes, finished_key, hash_len, &key);
743 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500744 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 goto exit;
746 }
747
748 status = psa_mac_compute(key, alg, transcript, hash_len,
749 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500750 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100751
752exit:
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 status = psa_destroy_key(key);
755 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500756 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100762}
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
765 unsigned char *dst,
766 size_t dst_len,
767 size_t *actual_len,
768 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000769{
XiaokangQiana7634982021-10-22 06:32:32 +0000770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000771
XiaokangQianaaa0e192021-11-10 03:07:04 +0000772 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000773 size_t transcript_len;
774
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800775 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800776 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800777 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800779
Dave Rodgman2eab4622023-10-05 13:30:37 +0100780 mbedtls_md_type_t const md_type = (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100781
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200782 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100783 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800789 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
791 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800792 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800794 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800797 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
798 goto exit;
799 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
802 transcript, sizeof(transcript),
803 &transcript_len);
804 if (ret != 0) {
805 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000806 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000807 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000809
Xiaokang Qian123cde82023-03-29 06:54:51 +0000810 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
811 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000813 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
817 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000818
819exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800820 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 mbedtls_platform_zeroize(base_key, base_key_len);
822 mbedtls_platform_zeroize(transcript, sizeof(transcript));
823 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000824}
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
827 const psa_algorithm_t hash_alg,
828 unsigned char const *psk, size_t psk_len,
829 int psk_type,
830 unsigned char const *transcript,
831 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100832{
833 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100834 unsigned char binder_key[PSA_MAC_MAX_SIZE];
835 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100837 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100838
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100839#if !defined(MBEDTLS_DEBUG_C)
840 ssl = NULL; /* make sure we don't use it except for debug */
841 ((void) ssl);
842#endif
843
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100844 /* We should never call this function with an unknown hash,
845 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (!PSA_ALG_IS_HASH(hash_alg)) {
847 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
848 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100849
850 /*
851 * 0
852 * |
853 * v
854 * PSK -> HKDF-Extract = Early Secret
855 * |
856 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
857 * | = binder_key
858 * v
859 */
860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
862 NULL, /* Old secret */
863 psk, psk_len, /* Input */
864 early_secret);
865 if (ret != 0) {
866 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100867 goto exit;
868 }
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
871 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000874 ret = mbedtls_ssl_tls13_derive_secret(
875 hash_alg,
876 early_secret, hash_len,
877 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
878 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
879 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
881 } else {
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(ext_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 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100889 }
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (ret != 0) {
892 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100893 goto exit;
894 }
895
896 /*
897 * The binding_value is computed in the same way as the Finished message
898 * but with the BaseKey being the binder_key.
899 */
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
902 result, &actual_len);
903 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100904 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100908
909exit:
910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
912 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
913 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100914}
915
Xiaokang Qian123cde82023-03-29 06:54:51 +0000916int mbedtls_ssl_tls13_populate_transform(
917 mbedtls_ssl_transform *transform,
918 int endpoint, int ciphersuite,
919 mbedtls_ssl_key_set const *traffic_keys,
920 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000921{
Hanno Beckerc94060c2021-03-22 07:50:44 +0000922 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
923 unsigned char const *key_enc;
924 unsigned char const *iv_enc;
925 unsigned char const *key_dec;
926 unsigned char const *iv_dec;
927
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100928 psa_key_type_t key_type;
929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
930 psa_algorithm_t alg;
931 size_t key_bits;
932 psa_status_t status = PSA_SUCCESS;
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100933
Hanno Beckerc94060c2021-03-22 07:50:44 +0000934#if !defined(MBEDTLS_DEBUG_C)
935 ssl = NULL; /* make sure we don't use it except for those cases */
936 (void) ssl;
937#endif
938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
940 if (ciphersuite_info == NULL) {
941 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
942 ciphersuite));
943 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100944 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000945
Hanno Beckerc94060c2021-03-22 07:50:44 +0000946
947#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000949 key_enc = traffic_keys->server_write_key;
950 key_dec = traffic_keys->client_write_key;
951 iv_enc = traffic_keys->server_write_iv;
952 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000954#endif /* MBEDTLS_SSL_SRV_C */
955#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000957 key_enc = traffic_keys->client_write_key;
958 key_dec = traffic_keys->server_write_key;
959 iv_enc = traffic_keys->client_write_iv;
960 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000962#endif /* MBEDTLS_SSL_CLI_C */
963 {
964 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000966 }
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
969 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000970
Hanno Beckerc94060c2021-03-22 07:50:44 +0000971
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100972 /*
973 * Setup other fields in SSL transform
974 */
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100977 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100979 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100981
982 transform->ivlen = traffic_keys->iv_len;
983 transform->maclen = 0;
984 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -0400985 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100986
987 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800988 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100989 * type-extended and padded plaintext is therefore the padding
990 * granularity. */
991 transform->minlen =
992 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
993
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100994 /*
995 * Setup psa keys and alg
996 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100997 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 transform->taglen,
999 &alg,
1000 &key_type,
1001 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001002 MBEDTLS_SSL_DEBUG_RET(
1003 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001004 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001005 }
1006
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001007 transform->psa_alg = alg;
1008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1010 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1011 psa_set_key_algorithm(&attributes, alg);
1012 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if ((status = psa_import_key(&attributes,
1015 key_enc,
1016 PSA_BITS_TO_BYTES(key_bits),
1017 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001018 MBEDTLS_SSL_DEBUG_RET(
1019 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001020 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001021 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if ((status = psa_import_key(&attributes,
1026 key_dec,
1027 PSA_BITS_TO_BYTES(key_bits),
1028 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001029 MBEDTLS_SSL_DEBUG_RET(
1030 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001031 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001032 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001033 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001036}
1037
Jerry Yu84a6eda2022-11-04 11:17:35 +08001038MBEDTLS_CHECK_RETURN_CRITICAL
1039static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1041 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001042{
1043 psa_key_type_t key_type;
1044 psa_algorithm_t alg;
1045 size_t taglen;
1046 size_t key_bits;
1047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001050 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001052 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001054
Dave Rodgman2eab4622023-10-05 13:30:37 +01001055 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, taglen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 &alg, &key_type, &key_bits);
1057 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001058 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001062
1063 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1064 *iv_len = 12;
1065
1066 return 0;
1067}
1068
Jerry Yu91b560f2022-11-04 14:10:34 +08001069#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001070/*
1071 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001072 * the early application data and handshake messages as described in section 7
1073 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001074 *
Jerry Yue31688b2022-11-22 21:55:56 +08001075 * NOTE: Only one key is generated, the key for the traffic from the client to
1076 * the server. The TLS 1.3 specification does not define a secret and thus
1077 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001078 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001079MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001080static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1081 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001082{
1083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001084 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001085 psa_algorithm_t hash_alg;
1086 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001087 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1088 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001089 size_t key_len = 0;
1090 size_t iv_len = 0;
Yanray Wang16c895d2022-12-15 15:14:35 +08001091 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001092
1093 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001094 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1095 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1100 if (ret != 0) {
1101 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001102 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001103 }
1104
Dave Rodgman2eab4622023-10-05 13:30:37 +01001105 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Jerry Yu91b560f2022-11-04 14:10:34 +08001106
Dave Rodgman2eab4622023-10-05 13:30:37 +01001107 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1111 transcript,
1112 sizeof(transcript),
1113 &transcript_len);
1114 if (ret != 0) {
1115 MBEDTLS_SSL_DEBUG_RET(1,
1116 "mbedtls_ssl_get_handshake_transcript",
1117 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001118 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001119 }
1120
Jerry Yub094e122022-11-21 13:03:47 +08001121 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001123 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001125 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001127 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001128 }
1129
1130 MBEDTLS_SSL_DEBUG_BUF(
1131 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001132 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001133
1134 /*
1135 * Export client handshake traffic secret
1136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001138 ssl->f_export_keys(
1139 ssl->p_export_keys,
1140 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001141 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001142 hash_len,
1143 handshake->randbytes,
1144 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001146 }
1147
Jerry Yua8771832022-11-21 23:16:54 +08001148 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001150 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 hash_len, traffic_keys->client_write_key, key_len,
1152 traffic_keys->client_write_iv, iv_len);
1153 if (ret != 0) {
1154 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001155 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001156 }
Jerry Yua8771832022-11-21 23:16:54 +08001157 traffic_keys->key_len = key_len;
1158 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1161 traffic_keys->client_write_key,
1162 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1165 traffic_keys->client_write_iv,
1166 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001169
Jerry Yu3d78e082022-11-23 18:26:20 +08001170cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001171 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001172 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001173 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1175 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001176}
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001179{
1180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1181 mbedtls_ssl_key_set traffic_keys;
1182 mbedtls_ssl_transform *transform_earlydata = NULL;
1183 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1184
1185 /* Next evolution in key schedule: Establish early_data secret and
1186 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1188 if (ret != 0) {
1189 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1190 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001191 goto cleanup;
1192 }
1193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1195 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001196 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1197 goto cleanup;
1198 }
1199
1200 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 transform_earlydata,
1202 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001203 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 &traffic_keys,
1205 ssl);
1206 if (ret != 0) {
1207 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001208 goto cleanup;
1209 }
1210 handshake->transform_earlydata = transform_earlydata;
1211
1212cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1214 if (ret != 0) {
1215 mbedtls_free(transform_earlydata);
1216 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001219}
1220#endif /* MBEDTLS_SSL_EARLY_DATA */
1221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001223{
Jerry Yue3131ef2021-09-16 13:14:15 +08001224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001225 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001226 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001227 unsigned char *psk = NULL;
1228 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if (handshake->ciphersuite_info == NULL) {
1231 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1232 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001233 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001234
Dave Rodgman2eab4622023-10-05 13:30:37 +01001235 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001236#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1238 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1239 if (ret != 0) {
1240 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1241 ret);
1242 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001243 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001244 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001245#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1248 handshake->tls13_master_secrets.early);
Manuel Pégourié-Gonnard0b44a812025-01-23 09:58:07 +01001249#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001251#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (ret != 0) {
1253 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1254 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001255 }
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1258 handshake->tls13_master_secrets.early,
1259 PSA_HASH_LENGTH(hash_alg));
1260 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001261}
1262
Yanray Wang05402112022-12-13 18:50:42 +08001263/**
1264 * \brief Compute TLS 1.3 handshake traffic keys.
1265 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001266 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1267 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001268 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001269 *
1270 * \param ssl The SSL context to operate on. This must be in
1271 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001272 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001273 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001274 * keys. This must be writable but may be uninitialized.
1275 *
1276 * \returns \c 0 on success.
1277 * \returns A negative error code on failure.
1278 */
1279MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001280static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1281 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001282{
1283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001284 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001285 psa_algorithm_t hash_alg;
1286 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001287 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001288 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001289 size_t key_len = 0;
1290 size_t iv_len = 0;
Jerry Yu61e35e02021-09-16 18:59:08 +08001291
Jerry Yu435208a2021-10-13 11:22:16 +08001292 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001293 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1294 handshake->ciphersuite_info;
1295 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1296 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001297
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001298 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1301 if (ret != 0) {
1302 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001303 return ret;
1304 }
1305
Dave Rodgman2eab4622023-10-05 13:30:37 +01001306 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001307
Dave Rodgman2eab4622023-10-05 13:30:37 +01001308 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1312 transcript,
1313 sizeof(transcript),
1314 &transcript_len);
1315 if (ret != 0) {
1316 MBEDTLS_SSL_DEBUG_RET(1,
1317 "mbedtls_ssl_get_handshake_transcript",
1318 ret);
1319 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001320 }
1321
Xiaokang Qian123cde82023-03-29 06:54:51 +00001322 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1323 hash_alg, handshake->tls13_master_secrets.handshake,
1324 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (ret != 0) {
1326 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1327 ret);
1328 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001329 }
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1332 tls13_hs_secrets->client_handshake_traffic_secret,
1333 hash_len);
1334 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1335 tls13_hs_secrets->server_handshake_traffic_secret,
1336 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001337
1338 /*
1339 * Export client handshake traffic secret
1340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001342 ssl->f_export_keys(
1343 ssl->p_export_keys,
1344 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1345 tls13_hs_secrets->client_handshake_traffic_secret,
1346 hash_len,
1347 handshake->randbytes,
1348 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1349 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001350
Xiaokang Qian123cde82023-03-29 06:54:51 +00001351 ssl->f_export_keys(
1352 ssl->p_export_keys,
1353 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1354 tls13_hs_secrets->server_handshake_traffic_secret,
1355 hash_len,
1356 handshake->randbytes,
1357 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1358 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001359 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001360
Xiaokang Qian123cde82023-03-29 06:54:51 +00001361 ret = mbedtls_ssl_tls13_make_traffic_keys(
1362 hash_alg,
1363 tls13_hs_secrets->client_handshake_traffic_secret,
1364 tls13_hs_secrets->server_handshake_traffic_secret,
1365 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if (ret != 0) {
1367 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001368 goto exit;
1369 }
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1372 traffic_keys->client_write_key,
1373 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1376 traffic_keys->server_write_key,
1377 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1380 traffic_keys->client_write_iv,
1381 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1384 traffic_keys->server_write_iv,
1385 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001386
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001387 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001388
1389exit:
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001392}
1393
Yanray Wang05402112022-12-13 18:50:42 +08001394/**
1395 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1396 *
1397 * The TLS 1.3 key schedule can be viewed as a simple state machine
1398 * with states Initial -> Early -> Handshake -> Application, and
1399 * this function represents the Early -> Handshake transition.
1400 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001401 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001402 * can be used to derive the handshake traffic keys.
1403 *
1404 * \param ssl The SSL context to operate on. This must be in key schedule
1405 * stage \c Early.
1406 *
1407 * \returns \c 0 on success.
1408 * \returns A negative error code on failure.
1409 */
1410MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001411static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001412{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001414 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001415 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001416 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001417 unsigned char *shared_secret = NULL;
1418 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001419
Ronald Crona2900bc2022-10-20 14:37:35 +02001420#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001421 /*
1422 * Compute ECDHE secret used to compute the handshake secret from which
1423 * client_handshake_traffic_secret and server_handshake_traffic_secret
1424 * are derived in the handshake secret derivation stage.
1425 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001427 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001428 mbedtls_ssl_tls13_named_group_is_ffdh(handshake->offered_group_id)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001429#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001430 psa_algorithm_t alg =
1431 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1432 PSA_ALG_ECDH : PSA_ALG_FFDH;
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001435 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001436 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1437
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001438 status = psa_get_key_attributes(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 &key_attributes);
1440 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001441 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 }
Ronald Cron3b056202022-10-05 17:20:21 +02001443
1444 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 psa_get_key_bits(&key_attributes));
1446 shared_secret = mbedtls_calloc(1, shared_secret_len);
1447 if (shared_secret == NULL) {
1448 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1449 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001450
Ronald Cron4c7edb22022-10-05 15:37:11 +02001451 status = psa_raw_key_agreement(
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001452 alg, handshake->xxdh_psa_privkey,
1453 handshake->xxdh_psa_peerkey, handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 shared_secret, shared_secret_len, &shared_secret_len);
1455 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001456 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001458 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001459 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001460
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001461 status = psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001463 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001465 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001466 }
1467
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001468 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001469#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 } else {
1471 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1472 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001473 }
1474 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001475#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001476
1477 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001478 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001479 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001480 ret = mbedtls_ssl_tls13_evolve_secret(
1481 hash_alg, handshake->tls13_master_secrets.early,
1482 shared_secret, shared_secret_len,
1483 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (ret != 0) {
1485 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001486 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001487 }
1488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1490 handshake->tls13_master_secrets.handshake,
1491 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001492
Ronald Cron3b056202022-10-05 17:20:21 +02001493cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (shared_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001495 mbedtls_zeroize_and_free(shared_secret, shared_secret_len);
Ronald Cron3b056202022-10-05 17:20:21 +02001496 }
1497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001499}
1500
Yanray Wang05402112022-12-13 18:50:42 +08001501/**
1502 * \brief Compute TLS 1.3 application traffic keys.
1503 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001504 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001505 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001506 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001507 *
1508 * \param ssl The SSL context to operate on. This must be in
1509 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001510 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001511 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001512 * keys. This must be writable but may be uninitialized.
1513 *
1514 * \returns \c 0 on success.
1515 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001516 */
Yanray Wang05402112022-12-13 18:50:42 +08001517MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001518static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 mbedtls_ssl_context *ssl,
1520 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001521{
XiaokangQiana7634982021-10-22 06:32:32 +00001522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001523 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001524
1525 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001526 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001527 &ssl->session_negotiate->app_secrets;
1528
1529 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001530 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001531 size_t transcript_len;
1532
1533 /* Variables relating to the hash for the chosen ciphersuite. */
1534 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001535
1536 psa_algorithm_t hash_alg;
1537 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001538
1539 /* Variables relating to the cipher for the chosen ciphersuite. */
Paul Elliott88600212023-11-23 14:24:30 +00001540 size_t key_len = 0, iv_len = 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001543
1544 /* Extract basic information about hash and ciphersuite */
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1547 &key_len, &iv_len);
1548 if (ret != 0) {
1549 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001550 goto cleanup;
1551 }
1552
Dave Rodgman2eab4622023-10-05 13:30:37 +01001553 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001554
Dave Rodgman2eab4622023-10-05 13:30:37 +01001555 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001557
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001558 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001559 * to call this at the right time, that is, after the ServerFinished. */
1560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1562 transcript, sizeof(transcript),
1563 &transcript_len);
1564 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001565 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001567
1568 /* Compute application secrets from master secret and transcript hash. */
1569
Xiaokang Qian123cde82023-03-29 06:54:51 +00001570 ret = mbedtls_ssl_tls13_derive_application_secrets(
1571 hash_alg, handshake->tls13_master_secrets.app,
1572 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001574 MBEDTLS_SSL_DEBUG_RET(
1575 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001576 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001577 }
1578
1579 /* Derive first epoch of IV + Key for application traffic. */
1580
Xiaokang Qian123cde82023-03-29 06:54:51 +00001581 ret = mbedtls_ssl_tls13_make_traffic_keys(
1582 hash_alg,
1583 app_secrets->client_application_traffic_secret_N,
1584 app_secrets->server_application_traffic_secret_N,
1585 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (ret != 0) {
1587 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001588 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001589 }
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1592 app_secrets->client_application_traffic_secret_N,
1593 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1596 app_secrets->server_application_traffic_secret_N,
1597 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001598
XiaokangQianac0385c2021-11-03 06:40:11 +00001599 /*
1600 * Export client/server application traffic secret 0
1601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001603 ssl->f_export_keys(
1604 ssl->p_export_keys,
1605 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1606 app_secrets->client_application_traffic_secret_N, hash_len,
1607 handshake->randbytes,
1608 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1609 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1610 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001611
Xiaokang Qian123cde82023-03-29 06:54:51 +00001612 ssl->f_export_keys(
1613 ssl->p_export_keys,
1614 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1615 app_secrets->server_application_traffic_secret_N, hash_len,
1616 handshake->randbytes,
1617 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1618 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1619 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001620 }
1621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1623 traffic_keys->client_write_key, key_len);
1624 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1625 traffic_keys->server_write_key, key_len);
1626 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1627 traffic_keys->client_write_iv, iv_len);
1628 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1629 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001634 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1636 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1639 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001640}
1641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001643{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001645 mbedtls_ssl_key_set traffic_keys;
1646 mbedtls_ssl_transform *transform_handshake = NULL;
1647 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1648
1649 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001650 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (ret != 0) {
1652 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001653 goto cleanup;
1654 }
1655
1656 /* Next evolution in key schedule: Establish handshake secret and
1657 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001658 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001660 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001662 goto cleanup;
1663 }
1664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1666 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001667 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1668 goto cleanup;
1669 }
1670
Jerry Yuef2b98a2022-05-06 16:40:05 +08001671 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 transform_handshake,
1673 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001674 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 &traffic_keys,
1676 ssl);
1677 if (ret != 0) {
1678 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001679 goto cleanup;
1680 }
1681 handshake->transform_handshake = transform_handshake;
1682
1683cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1685 if (ret != 0) {
1686 mbedtls_free(transform_handshake);
1687 }
Jerry Yue110d252022-05-05 10:19:22 +08001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001690}
1691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001693{
Jerry Yu46bffe02022-09-13 11:25:28 +08001694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001695 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001696 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1697 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001698 size_t transcript_len;
1699
Xiaokang Qian123cde82023-03-29 06:54:51 +00001700 MBEDTLS_SSL_DEBUG_MSG(
1701 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001702
Dave Rodgman2eab4622023-10-05 13:30:37 +01001703 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1706 transcript, sizeof(transcript),
1707 &transcript_len);
1708 if (ret != 0) {
1709 return ret;
1710 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001711
1712 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001713 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 handshake->tls13_master_secrets.app,
1715 transcript, transcript_len,
1716 &ssl->session_negotiate->app_secrets);
1717 if (ret != 0) {
1718 return ret;
1719 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001720
Jerry Yuff226982022-04-16 16:52:57 +08001721 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1723 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001724
Xiaokang Qian123cde82023-03-29 06:54:51 +00001725 MBEDTLS_SSL_DEBUG_BUF(
1726 4, "Resumption master secret",
1727 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001728 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001729
Xiaokang Qian123cde82023-03-29 06:54:51 +00001730 MBEDTLS_SSL_DEBUG_MSG(
1731 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001733}
1734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001736{
1737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1738 mbedtls_ssl_key_set traffic_keys;
1739 mbedtls_ssl_transform *transform_application = NULL;
1740
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001741 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 if (ret != 0) {
1743 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001744 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001745 goto cleanup;
1746 }
1747
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001748 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (ret != 0) {
1750 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001751 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001752 goto cleanup;
1753 }
1754
1755 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1757 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001758 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1759 goto cleanup;
1760 }
1761
1762 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 transform_application,
1764 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001765 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 &traffic_keys,
1767 ssl);
1768 if (ret != 0) {
1769 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001770 goto cleanup;
1771 }
1772
1773 ssl->transform_application = transform_application;
1774
1775cleanup:
1776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1778 if (ret != 0) {
1779 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001780 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001782}
1783
Ronald Cron41a443a2022-10-04 16:38:25 +02001784#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001785int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1786 unsigned char **psk,
1787 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001788{
Jerry Yu40f37712022-07-26 16:58:57 +08001789 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001790 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001791
1792 *psk_len = 0;
1793 *psk = NULL;
1794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1796 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001797 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001798
1799 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1800 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001801 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 }
1803
1804 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1805 *psk = mbedtls_calloc(1, *psk_len);
1806 if (*psk == NULL) {
1807 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1808 }
1809
1810 status = psa_export_key(ssl->handshake->psk_opaque,
1811 (uint8_t *) *psk, *psk_len, psk_len);
1812 if (status != PSA_SUCCESS) {
1813 mbedtls_free((void *) *psk);
1814 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001815 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 }
1817 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001818}
Ronald Cron41a443a2022-10-04 16:38:25 +02001819#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001820
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001821int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
1822 const unsigned char *secret, const size_t secret_len,
1823 const unsigned char *label, const size_t label_len,
1824 const unsigned char *context_value, const size_t context_len,
1825 unsigned char *out, const size_t out_len)
1826{
1827 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
1828 unsigned char hkdf_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001829 int ret = 0;
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001830
1831 ret = mbedtls_ssl_tls13_derive_secret(hash_alg, secret, secret_len, label, label_len, NULL, 0,
Max Fillinger7b722202024-09-21 10:48:57 +02001832 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, hkdf_secret,
1833 hash_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001834 if (ret != 0) {
1835 goto exit;
1836 }
Max Fillinger7b722202024-09-21 10:48:57 +02001837 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
1838 hkdf_secret,
1839 hash_len,
Max Fillinger334c3672024-08-12 11:20:39 +02001840 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter),
Max Fillinger7b722202024-09-21 10:48:57 +02001841 context_value,
1842 context_len,
1843 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
1844 out,
1845 out_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001846
1847exit:
1848 mbedtls_platform_zeroize(hkdf_secret, sizeof(hkdf_secret));
1849 return ret;
1850}
1851
Ronald Cron6f135e12021-12-08 16:57:54 +01001852#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */