blob: 895176d0c642d51022bd8674b9132d3250d2d375 [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:
59 * - desired_length: Length of expanded key material
60 * Even though the standard allows expansion to up to
61 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
62 * 255 Bytes, so we require `desired_length` to be at most
63 * 255. This allows us to save a few Bytes of code by
64 * hardcoding the writing of the high bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000065 * - (label, label_len): label + label length, without "tls13 " prefix
66 * The label length MUST be less than or equal to
67 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
68 * It is the caller's responsibility to ensure this.
69 * All (label, label length) pairs used in TLS 1.3
70 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
71 * - (ctx, ctx_len): context + context length
72 * The context length MUST be less than or equal to
73 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
74 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010075 * - dst: Target buffer for HkdfLabel structure,
76 * This MUST be a writable buffer of size
77 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000078 * - dst_len: Pointer at which to store the actual length of
79 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010080 */
81
Xiaofei Baid25fab62021-12-02 06:36:27 +000082static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010083
Gilles Peskine449bd832023-01-11 14:50:10 +010084#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
85 (2 /* expansion length */ \
86 + 1 /* label length */ \
87 + label_len \
88 + 1 /* context length */ \
89 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010090
91#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
92 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +010093 sizeof(tls13_label_prefix) + \
94 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
95 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010096
Xiaofei Bai746f9482021-11-12 08:53:56 +000097static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +010098 size_t desired_length,
99 const unsigned char *label, size_t label_len,
100 const unsigned char *ctx, size_t ctx_len,
101 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100102{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100103 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000104 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100105 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100107
108 unsigned char *p = dst;
109
Max Fillinger28916ac2024-10-29 18:49:30 +0100110 /* Add the size of the expanded key material. */
111#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > UINT16_MAX
112#error "The desired key length must fit into an uint16 but \
113 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN is greater than UINT16_MAX"
Hanno Becker531fe302020-09-16 09:45:27 +0100114#endif
115
Max Fillinger28916ac2024-10-29 18:49:30 +0100116 *p++ = MBEDTLS_BYTE_1(desired_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100118
119 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 *p++ = MBEDTLS_BYTE_0(total_label_len);
121 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000122 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000124 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100125
126 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 *p++ = MBEDTLS_BYTE_0(ctx_len);
128 if (ctx_len != 0) {
129 memcpy(p, ctx, ctx_len);
130 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100131
132 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000133 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100134}
135
Xiaofei Bai746f9482021-11-12 08:53:56 +0000136int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 psa_algorithm_t hash_alg,
138 const unsigned char *secret, size_t secret_len,
139 const unsigned char *label, size_t label_len,
140 const unsigned char *ctx, size_t ctx_len,
141 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100142{
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200144 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
146 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200147 psa_key_derivation_operation_t operation =
148 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100151 /* Should never happen since this is an internal
152 * function, and we know statically which labels
153 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100155 }
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100158 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100160 }
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100163 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100165 }
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (!PSA_ALG_IS_HASH(hash_alg)) {
168 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
169 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 ssl_tls13_hkdf_encode_label(buf_len,
172 label, label_len,
173 ctx, ctx_len,
174 hkdf_label,
175 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (status != PSA_SUCCESS) {
180 goto cleanup;
181 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 status = psa_key_derivation_input_bytes(&operation,
184 PSA_KEY_DERIVATION_INPUT_SECRET,
185 secret,
186 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (status != PSA_SUCCESS) {
189 goto cleanup;
190 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 status = psa_key_derivation_input_bytes(&operation,
193 PSA_KEY_DERIVATION_INPUT_INFO,
194 hkdf_label,
195 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if (status != PSA_SUCCESS) {
198 goto cleanup;
199 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 status = psa_key_derivation_output_bytes(&operation,
202 buf,
203 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (status != PSA_SUCCESS) {
206 goto cleanup;
207 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200208
209cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 abort_status = psa_key_derivation_abort(&operation);
211 status = (status == PSA_SUCCESS ? abort_status : status);
212 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500213 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100214}
215
Jerry Yua5db6c02022-11-23 18:08:04 +0800216MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800217static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 psa_algorithm_t hash_alg,
219 const unsigned char *secret, size_t secret_len,
220 unsigned char *key, size_t key_len,
221 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800222{
223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
224
Jerry Yuaec08b32022-11-29 15:19:27 +0800225 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 hash_alg,
227 secret, secret_len,
228 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
229 NULL, 0,
230 key, key_len);
231 if (ret != 0) {
232 return ret;
233 }
Jerry Yua8771832022-11-21 23:16:54 +0800234
Jerry Yuaec08b32022-11-29 15:19:27 +0800235 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 hash_alg,
237 secret, secret_len,
238 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
239 NULL, 0,
240 iv, iv_len);
241 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800242}
243
Hanno Becker3385a4d2020-08-21 13:03:34 +0100244/*
245 * The traffic keying material is generated from the following inputs:
246 *
247 * - One secret value per sender.
248 * - A purpose value indicating the specific value being generated
249 * - The desired lengths of key and IV.
250 *
251 * The expansion itself is based on HKDF:
252 *
253 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
254 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
255 *
256 * [sender] denotes the sending side and the Secret value is provided
257 * by the function caller. Note that we generate server and client side
258 * keys in a single function call.
259 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000260int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 psa_algorithm_t hash_alg,
262 const unsigned char *client_secret,
263 const unsigned char *server_secret, size_t secret_len,
264 size_t key_len, size_t iv_len,
265 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100266{
267 int ret = 0;
268
Jerry Yua8771832022-11-21 23:16:54 +0800269 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 hash_alg, client_secret, secret_len,
271 keys->client_write_key, key_len,
272 keys->client_write_iv, iv_len);
273 if (ret != 0) {
274 return ret;
275 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100276
Jerry Yua8771832022-11-21 23:16:54 +0800277 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 hash_alg, server_secret, secret_len,
279 keys->server_write_key, key_len,
280 keys->server_write_iv, iv_len);
281 if (ret != 0) {
282 return ret;
283 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100284
Hanno Becker493ea7f2020-09-08 11:01:00 +0100285 keys->key_len = key_len;
286 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100289}
290
Xiaofei Bai746f9482021-11-12 08:53:56 +0000291int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 psa_algorithm_t hash_alg,
293 const unsigned char *secret, size_t secret_len,
294 const unsigned char *label, size_t label_len,
295 const unsigned char *ctx, size_t ctx_len,
296 int ctx_hashed,
297 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100298{
299 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
301 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100302 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
305 PSA_HASH_LENGTH(hash_alg), &ctx_len);
306 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500307 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100308 return ret;
309 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 } else {
311 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100312 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100313 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100314 * Let's double-check nonetheless to not run at the risk
315 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100317 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100320 }
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
323 secret, secret_len,
324 label, label_len,
325 hashed_context, ctx_len,
326 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100327
Hanno Beckerb35d5222020-08-21 13:27:44 +0100328}
329
Xiaofei Bai746f9482021-11-12 08:53:56 +0000330int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 psa_algorithm_t hash_alg,
332 const unsigned char *secret_old,
333 const unsigned char *input, size_t input_len,
334 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100335{
336 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
338 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200339 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
341 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200342 const unsigned char *l_input = NULL;
343 size_t l_input_len;
344
Przemek Stekield5ae3652022-05-13 12:10:08 +0200345 psa_key_derivation_operation_t operation =
346 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (!PSA_ALG_IS_HASH(hash_alg)) {
349 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
350 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100353
354 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100355 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000357 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 hash_alg,
359 secret_old, hlen,
360 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
361 NULL, 0, /* context */
362 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
363 tmp_secret, hlen);
364 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100365 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100367 }
368
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200369 ret = 0;
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200372 l_input = input;
373 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200375 l_input = all_zeroes_input;
376 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100377 }
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 status = psa_key_derivation_setup(&operation,
380 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (status != PSA_SUCCESS) {
383 goto cleanup;
384 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 status = psa_key_derivation_input_bytes(&operation,
387 PSA_KEY_DERIVATION_INPUT_SALT,
388 tmp_secret,
389 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (status != PSA_SUCCESS) {
392 goto cleanup;
393 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 status = psa_key_derivation_input_bytes(&operation,
396 PSA_KEY_DERIVATION_INPUT_SECRET,
397 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (status != PSA_SUCCESS) {
400 goto cleanup;
401 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 status = psa_key_derivation_output_bytes(&operation,
404 secret_new,
405 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (status != PSA_SUCCESS) {
408 goto cleanup;
409 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411cleanup:
412 abort_status = psa_key_derivation_abort(&operation);
413 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500414 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
416 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100417}
418
Xiaofei Bai746f9482021-11-12 08:53:56 +0000419int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 psa_algorithm_t hash_alg,
421 unsigned char const *early_secret,
422 unsigned char const *transcript, size_t transcript_len,
423 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100424{
425 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100427
428 /* We should never call this function with an unknown hash,
429 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (!PSA_ALG_IS_HASH(hash_alg)) {
431 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
432 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100433
434 /*
435 * 0
436 * |
437 * v
438 * PSK -> HKDF-Extract = Early Secret
439 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100440 * +-----> Derive-Secret(., "c e traffic", ClientHello)
441 * | = client_early_traffic_secret
442 * |
443 * +-----> Derive-Secret(., "e exp master", ClientHello)
444 * | = early_exporter_master_secret
445 * v
446 */
447
448 /* Create client_early_traffic_secret */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000449 ret = mbedtls_ssl_tls13_derive_secret(
450 hash_alg,
451 early_secret, hash_len,
452 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
453 transcript, transcript_len,
454 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
455 derived->client_early_traffic_secret,
456 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (ret != 0) {
458 return ret;
459 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100460
461 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000462 ret = mbedtls_ssl_tls13_derive_secret(
463 hash_alg,
464 early_secret, hash_len,
465 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
466 transcript, transcript_len,
467 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
468 derived->early_exporter_master_secret,
469 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (ret != 0) {
471 return ret;
472 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100475}
476
Xiaofei Bai746f9482021-11-12 08:53:56 +0000477int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 psa_algorithm_t hash_alg,
479 unsigned char const *handshake_secret,
480 unsigned char const *transcript, size_t transcript_len,
481 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100482{
483 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100485
486 /* We should never call this function with an unknown hash,
487 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (!PSA_ALG_IS_HASH(hash_alg)) {
489 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
490 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100491
492 /*
493 *
494 * Handshake Secret
495 * |
496 * +-----> Derive-Secret( ., "c hs traffic",
497 * | ClientHello...ServerHello )
498 * | = client_handshake_traffic_secret
499 * |
500 * +-----> Derive-Secret( ., "s hs traffic",
501 * | ClientHello...ServerHello )
502 * | = server_handshake_traffic_secret
503 *
504 */
505
506 /*
507 * Compute client_handshake_traffic_secret with
508 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
509 */
510
Xiaokang Qian123cde82023-03-29 06:54:51 +0000511 ret = mbedtls_ssl_tls13_derive_secret(
512 hash_alg,
513 handshake_secret, hash_len,
514 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
515 transcript, transcript_len,
516 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
517 derived->client_handshake_traffic_secret,
518 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (ret != 0) {
520 return ret;
521 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100522
523 /*
524 * Compute server_handshake_traffic_secret with
525 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
526 */
527
Xiaokang Qian123cde82023-03-29 06:54:51 +0000528 ret = mbedtls_ssl_tls13_derive_secret(
529 hash_alg,
530 handshake_secret, hash_len,
531 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
532 transcript, transcript_len,
533 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
534 derived->server_handshake_traffic_secret,
535 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (ret != 0) {
537 return ret;
538 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100541}
542
Xiaofei Bai746f9482021-11-12 08:53:56 +0000543int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 psa_algorithm_t hash_alg,
545 unsigned char const *application_secret,
546 unsigned char const *transcript, size_t transcript_len,
547 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100548{
549 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100551
552 /* We should never call this function with an unknown hash,
553 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (!PSA_ALG_IS_HASH(hash_alg)) {
555 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
556 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100557
558 /* Generate {client,server}_application_traffic_secret_0
559 *
560 * Master Secret
561 * |
562 * +-----> Derive-Secret( ., "c ap traffic",
563 * | ClientHello...server Finished )
564 * | = client_application_traffic_secret_0
565 * |
566 * +-----> Derive-Secret( ., "s ap traffic",
567 * | ClientHello...Server Finished )
568 * | = server_application_traffic_secret_0
569 * |
570 * +-----> Derive-Secret( ., "exp master",
571 * | ClientHello...server Finished)
572 * | = exporter_master_secret
573 *
574 */
575
Xiaokang Qian123cde82023-03-29 06:54:51 +0000576 ret = mbedtls_ssl_tls13_derive_secret(
577 hash_alg,
578 application_secret, hash_len,
579 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
580 transcript, transcript_len,
581 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
582 derived->client_application_traffic_secret_N,
583 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (ret != 0) {
585 return ret;
586 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100587
Xiaokang Qian123cde82023-03-29 06:54:51 +0000588 ret = mbedtls_ssl_tls13_derive_secret(
589 hash_alg,
590 application_secret, hash_len,
591 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
592 transcript, transcript_len,
593 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
594 derived->server_application_traffic_secret_N,
595 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (ret != 0) {
597 return ret;
598 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100599
Xiaokang Qian123cde82023-03-29 06:54:51 +0000600 ret = mbedtls_ssl_tls13_derive_secret(
601 hash_alg,
602 application_secret, hash_len,
603 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
604 transcript, transcript_len,
605 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
606 derived->exporter_master_secret,
607 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (ret != 0) {
609 return ret;
610 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100613}
614
615/* Generate resumption_master_secret for use with the ticket exchange.
616 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000617 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100618 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000619int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 psa_algorithm_t hash_alg,
621 unsigned char const *application_secret,
622 unsigned char const *transcript, size_t transcript_len,
623 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100624{
625 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100627
628 /* We should never call this function with an unknown hash,
629 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (!PSA_ALG_IS_HASH(hash_alg)) {
631 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
632 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100633
Xiaokang Qian123cde82023-03-29 06:54:51 +0000634 ret = mbedtls_ssl_tls13_derive_secret(
635 hash_alg,
636 application_secret, hash_len,
637 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
638 transcript, transcript_len,
639 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
640 derived->resumption_master_secret,
641 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (ret != 0) {
644 return ret;
645 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100648}
649
Yanray Wang05402112022-12-13 18:50:42 +0800650/**
651 * \brief Transition into application stage of TLS 1.3 key schedule.
652 *
653 * The TLS 1.3 key schedule can be viewed as a simple state machine
654 * with states Initial -> Early -> Handshake -> Application, and
655 * this function represents the Handshake -> Application transition.
656 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800657 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800658 * can be used to derive the handshake traffic keys.
659 *
660 * \param ssl The SSL context to operate on. This must be in key schedule
661 * stage \c Handshake.
662 *
663 * \returns \c 0 on success.
664 * \returns A negative error code on failure.
665 */
666MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800667static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000668{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000670 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200671 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100672 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000673
674 /*
675 * Compute MasterSecret
676 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000677 ret = mbedtls_ssl_tls13_evolve_secret(
678 hash_alg,
679 handshake->tls13_master_secrets.handshake,
680 NULL, 0,
681 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (ret != 0) {
683 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
684 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000685 }
686
Xiaokang Qian123cde82023-03-29 06:54:51 +0000687 MBEDTLS_SSL_DEBUG_BUF(
688 4, "Master secret",
689 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000692}
693
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200694MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100695static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
696 unsigned char const *base_key,
697 unsigned char const *transcript,
698 unsigned char *dst,
699 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100700{
Gabor Mezei07732f72022-03-26 17:04:19 +0100701 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
703 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100705 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100706 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100707 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100708
709 /* We should never call this function with an unknown hash,
710 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (!PSA_ALG_IS_HASH(hash_alg)) {
712 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
713 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100714
715 /* TLS 1.3 Finished message
716 *
717 * struct {
718 * opaque verify_data[Hash.length];
719 * } Finished;
720 *
721 * verify_data =
722 * HMAC( finished_key,
723 * Hash( Handshake Context +
724 * Certificate* +
725 * CertificateVerify* )
726 * )
727 *
728 * finished_key =
729 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
730 */
731
Xiaofei Bai746f9482021-11-12 08:53:56 +0000732 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 hash_alg, base_key, hash_len,
734 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
735 NULL, 0,
736 finished_key, hash_len);
737 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100738 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100739 }
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 alg = PSA_ALG_HMAC(hash_alg);
742 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
743 psa_set_key_algorithm(&attributes, alg);
744 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
745
746 status = psa_import_key(&attributes, finished_key, hash_len, &key);
747 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500748 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 goto exit;
750 }
751
752 status = psa_mac_compute(key, alg, transcript, hash_len,
753 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500754 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100755
756exit:
757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 status = psa_destroy_key(key);
759 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500760 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100766}
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
769 unsigned char *dst,
770 size_t dst_len,
771 size_t *actual_len,
772 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000773{
XiaokangQiana7634982021-10-22 06:32:32 +0000774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000775
XiaokangQianaaa0e192021-11-10 03:07:04 +0000776 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000777 size_t transcript_len;
778
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800779 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800780 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800781 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800783
Dave Rodgman2eab4622023-10-05 13:30:37 +0100784 mbedtls_md_type_t const md_type = (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100785
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200786 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100787 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800793 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
795 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800796 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800798 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800801 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
802 goto exit;
803 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
806 transcript, sizeof(transcript),
807 &transcript_len);
808 if (ret != 0) {
809 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000810 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000811 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000813
Xiaokang Qian123cde82023-03-29 06:54:51 +0000814 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
815 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000817 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
821 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000822
823exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800824 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 mbedtls_platform_zeroize(base_key, base_key_len);
826 mbedtls_platform_zeroize(transcript, sizeof(transcript));
827 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000828}
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
831 const psa_algorithm_t hash_alg,
832 unsigned char const *psk, size_t psk_len,
833 int psk_type,
834 unsigned char const *transcript,
835 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100836{
837 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100838 unsigned char binder_key[PSA_MAC_MAX_SIZE];
839 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100841 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100842
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100843#if !defined(MBEDTLS_DEBUG_C)
844 ssl = NULL; /* make sure we don't use it except for debug */
845 ((void) ssl);
846#endif
847
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100848 /* We should never call this function with an unknown hash,
849 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (!PSA_ALG_IS_HASH(hash_alg)) {
851 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
852 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100853
854 /*
855 * 0
856 * |
857 * v
858 * PSK -> HKDF-Extract = Early Secret
859 * |
860 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
861 * | = binder_key
862 * v
863 */
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
866 NULL, /* Old secret */
867 psk, psk_len, /* Input */
868 early_secret);
869 if (ret != 0) {
870 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100871 goto exit;
872 }
873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
875 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000878 ret = mbedtls_ssl_tls13_derive_secret(
879 hash_alg,
880 early_secret, hash_len,
881 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
882 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
883 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
885 } else {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000886 ret = mbedtls_ssl_tls13_derive_secret(
887 hash_alg,
888 early_secret, hash_len,
889 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
890 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
891 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100893 }
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (ret != 0) {
896 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100897 goto exit;
898 }
899
900 /*
901 * The binding_value is computed in the same way as the Finished message
902 * but with the BaseKey being the binder_key.
903 */
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
906 result, &actual_len);
907 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100908 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100912
913exit:
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
916 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
917 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100918}
919
Xiaokang Qian123cde82023-03-29 06:54:51 +0000920int mbedtls_ssl_tls13_populate_transform(
921 mbedtls_ssl_transform *transform,
922 int endpoint, int ciphersuite,
923 mbedtls_ssl_key_set const *traffic_keys,
924 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000925{
Hanno Beckerc94060c2021-03-22 07:50:44 +0000926 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
927 unsigned char const *key_enc;
928 unsigned char const *iv_enc;
929 unsigned char const *key_dec;
930 unsigned char const *iv_dec;
931
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100932 psa_key_type_t key_type;
933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
934 psa_algorithm_t alg;
935 size_t key_bits;
936 psa_status_t status = PSA_SUCCESS;
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100937
Hanno Beckerc94060c2021-03-22 07:50:44 +0000938#if !defined(MBEDTLS_DEBUG_C)
939 ssl = NULL; /* make sure we don't use it except for those cases */
940 (void) ssl;
941#endif
942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
944 if (ciphersuite_info == NULL) {
945 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
946 ciphersuite));
947 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100948 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000949
Hanno Beckerc94060c2021-03-22 07:50:44 +0000950
951#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000953 key_enc = traffic_keys->server_write_key;
954 key_dec = traffic_keys->client_write_key;
955 iv_enc = traffic_keys->server_write_iv;
956 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000958#endif /* MBEDTLS_SSL_SRV_C */
959#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000961 key_enc = traffic_keys->client_write_key;
962 key_dec = traffic_keys->server_write_key;
963 iv_enc = traffic_keys->client_write_iv;
964 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000966#endif /* MBEDTLS_SSL_CLI_C */
967 {
968 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000970 }
971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
973 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000974
Hanno Beckerc94060c2021-03-22 07:50:44 +0000975
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100976 /*
977 * Setup other fields in SSL transform
978 */
979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100981 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100983 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100985
986 transform->ivlen = traffic_keys->iv_len;
987 transform->maclen = 0;
988 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -0400989 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100990
991 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800992 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100993 * type-extended and padded plaintext is therefore the padding
994 * granularity. */
995 transform->minlen =
996 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
997
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100998 /*
999 * Setup psa keys and alg
1000 */
Dave Rodgman2eab4622023-10-05 13:30:37 +01001001 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 transform->taglen,
1003 &alg,
1004 &key_type,
1005 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001006 MBEDTLS_SSL_DEBUG_RET(
1007 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001008 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001009 }
1010
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001011 transform->psa_alg = alg;
1012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1014 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1015 psa_set_key_algorithm(&attributes, alg);
1016 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 if ((status = psa_import_key(&attributes,
1019 key_enc,
1020 PSA_BITS_TO_BYTES(key_bits),
1021 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001022 MBEDTLS_SSL_DEBUG_RET(
1023 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001024 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001025 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if ((status = psa_import_key(&attributes,
1030 key_dec,
1031 PSA_BITS_TO_BYTES(key_bits),
1032 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001033 MBEDTLS_SSL_DEBUG_RET(
1034 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001035 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001036 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001037 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001040}
1041
Jerry Yu84a6eda2022-11-04 11:17:35 +08001042MBEDTLS_CHECK_RETURN_CRITICAL
1043static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1045 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001046{
1047 psa_key_type_t key_type;
1048 psa_algorithm_t alg;
1049 size_t taglen;
1050 size_t key_bits;
1051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001054 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001056 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001058
Dave Rodgman2eab4622023-10-05 13:30:37 +01001059 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, taglen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 &alg, &key_type, &key_bits);
1061 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001062 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001066
1067 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1068 *iv_len = 12;
1069
1070 return 0;
1071}
1072
Jerry Yu91b560f2022-11-04 14:10:34 +08001073#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001074/*
1075 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001076 * the early application data and handshake messages as described in section 7
1077 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001078 *
Jerry Yue31688b2022-11-22 21:55:56 +08001079 * NOTE: Only one key is generated, the key for the traffic from the client to
1080 * the server. The TLS 1.3 specification does not define a secret and thus
1081 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001082 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001083MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001084static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1085 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001086{
1087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001088 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001089 psa_algorithm_t hash_alg;
1090 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001091 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1092 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001093 size_t key_len = 0;
1094 size_t iv_len = 0;
Yanray Wang16c895d2022-12-15 15:14:35 +08001095 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001096
1097 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001098 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1099 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1104 if (ret != 0) {
1105 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001106 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001107 }
1108
Dave Rodgman2eab4622023-10-05 13:30:37 +01001109 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Jerry Yu91b560f2022-11-04 14:10:34 +08001110
Dave Rodgman2eab4622023-10-05 13:30:37 +01001111 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1115 transcript,
1116 sizeof(transcript),
1117 &transcript_len);
1118 if (ret != 0) {
1119 MBEDTLS_SSL_DEBUG_RET(1,
1120 "mbedtls_ssl_get_handshake_transcript",
1121 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001122 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001123 }
1124
Jerry Yub094e122022-11-21 13:03:47 +08001125 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001127 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001129 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001131 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001132 }
1133
1134 MBEDTLS_SSL_DEBUG_BUF(
1135 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001136 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001137
1138 /*
1139 * Export client handshake traffic secret
1140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001142 ssl->f_export_keys(
1143 ssl->p_export_keys,
1144 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001145 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001146 hash_len,
1147 handshake->randbytes,
1148 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001150 }
1151
Jerry Yua8771832022-11-21 23:16:54 +08001152 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001154 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 hash_len, traffic_keys->client_write_key, key_len,
1156 traffic_keys->client_write_iv, iv_len);
1157 if (ret != 0) {
1158 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001159 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001160 }
Jerry Yua8771832022-11-21 23:16:54 +08001161 traffic_keys->key_len = key_len;
1162 traffic_keys->iv_len = iv_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_key",
1165 traffic_keys->client_write_key,
1166 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1169 traffic_keys->client_write_iv,
1170 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001173
Jerry Yu3d78e082022-11-23 18:26:20 +08001174cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001175 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001176 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001177 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1179 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001180}
1181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001183{
1184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1185 mbedtls_ssl_key_set traffic_keys;
1186 mbedtls_ssl_transform *transform_earlydata = NULL;
1187 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1188
1189 /* Next evolution in key schedule: Establish early_data secret and
1190 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1192 if (ret != 0) {
1193 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1194 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001195 goto cleanup;
1196 }
1197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1199 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001200 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1201 goto cleanup;
1202 }
1203
1204 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 transform_earlydata,
1206 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001207 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 &traffic_keys,
1209 ssl);
1210 if (ret != 0) {
1211 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001212 goto cleanup;
1213 }
1214 handshake->transform_earlydata = transform_earlydata;
1215
1216cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1218 if (ret != 0) {
1219 mbedtls_free(transform_earlydata);
1220 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001223}
1224#endif /* MBEDTLS_SSL_EARLY_DATA */
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001227{
Jerry Yue3131ef2021-09-16 13:14:15 +08001228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001229 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001230 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001231 unsigned char *psk = NULL;
1232 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (handshake->ciphersuite_info == NULL) {
1235 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1236 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001237 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001238
Dave Rodgman2eab4622023-10-05 13:30:37 +01001239 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001240#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1242 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1243 if (ret != 0) {
1244 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1245 ret);
1246 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001247 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001248 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001249#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1252 handshake->tls13_master_secrets.early);
Manuel Pégourié-Gonnard0b44a812025-01-23 09:58:07 +01001253#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001255#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (ret != 0) {
1257 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1258 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001259 }
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1262 handshake->tls13_master_secrets.early,
1263 PSA_HASH_LENGTH(hash_alg));
1264 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001265}
1266
Yanray Wang05402112022-12-13 18:50:42 +08001267/**
1268 * \brief Compute TLS 1.3 handshake traffic keys.
1269 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001270 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1271 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001272 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001273 *
1274 * \param ssl The SSL context to operate on. This must be in
1275 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001276 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001277 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001278 * keys. This must be writable but may be uninitialized.
1279 *
1280 * \returns \c 0 on success.
1281 * \returns A negative error code on failure.
1282 */
1283MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001284static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1285 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001286{
1287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001288 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001289 psa_algorithm_t hash_alg;
1290 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001291 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001292 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001293 size_t key_len = 0;
1294 size_t iv_len = 0;
Jerry Yu61e35e02021-09-16 18:59:08 +08001295
Jerry Yu435208a2021-10-13 11:22:16 +08001296 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001297 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1298 handshake->ciphersuite_info;
1299 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1300 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001301
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001302 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1305 if (ret != 0) {
1306 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001307 return ret;
1308 }
1309
Dave Rodgman2eab4622023-10-05 13:30:37 +01001310 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001311
Dave Rodgman2eab4622023-10-05 13:30:37 +01001312 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1316 transcript,
1317 sizeof(transcript),
1318 &transcript_len);
1319 if (ret != 0) {
1320 MBEDTLS_SSL_DEBUG_RET(1,
1321 "mbedtls_ssl_get_handshake_transcript",
1322 ret);
1323 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001324 }
1325
Xiaokang Qian123cde82023-03-29 06:54:51 +00001326 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1327 hash_alg, handshake->tls13_master_secrets.handshake,
1328 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (ret != 0) {
1330 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1331 ret);
1332 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001333 }
1334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1336 tls13_hs_secrets->client_handshake_traffic_secret,
1337 hash_len);
1338 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1339 tls13_hs_secrets->server_handshake_traffic_secret,
1340 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001341
1342 /*
1343 * Export client handshake traffic secret
1344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001346 ssl->f_export_keys(
1347 ssl->p_export_keys,
1348 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1349 tls13_hs_secrets->client_handshake_traffic_secret,
1350 hash_len,
1351 handshake->randbytes,
1352 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1353 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001354
Xiaokang Qian123cde82023-03-29 06:54:51 +00001355 ssl->f_export_keys(
1356 ssl->p_export_keys,
1357 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1358 tls13_hs_secrets->server_handshake_traffic_secret,
1359 hash_len,
1360 handshake->randbytes,
1361 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1362 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001363 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001364
Xiaokang Qian123cde82023-03-29 06:54:51 +00001365 ret = mbedtls_ssl_tls13_make_traffic_keys(
1366 hash_alg,
1367 tls13_hs_secrets->client_handshake_traffic_secret,
1368 tls13_hs_secrets->server_handshake_traffic_secret,
1369 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 if (ret != 0) {
1371 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001372 goto exit;
1373 }
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1376 traffic_keys->client_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, "server_handshake write_key",
1380 traffic_keys->server_write_key,
1381 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1384 traffic_keys->client_write_iv,
1385 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1388 traffic_keys->server_write_iv,
1389 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001390
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001391 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001392
1393exit:
1394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001396}
1397
Yanray Wang05402112022-12-13 18:50:42 +08001398/**
1399 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1400 *
1401 * The TLS 1.3 key schedule can be viewed as a simple state machine
1402 * with states Initial -> Early -> Handshake -> Application, and
1403 * this function represents the Early -> Handshake transition.
1404 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001405 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001406 * can be used to derive the handshake traffic keys.
1407 *
1408 * \param ssl The SSL context to operate on. This must be in key schedule
1409 * stage \c Early.
1410 *
1411 * \returns \c 0 on success.
1412 * \returns A negative error code on failure.
1413 */
1414MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001415static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001416{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001418 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001419 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001420 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001421 unsigned char *shared_secret = NULL;
1422 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001423
Ronald Crona2900bc2022-10-20 14:37:35 +02001424#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001425 /*
1426 * Compute ECDHE secret used to compute the handshake secret from which
1427 * client_handshake_traffic_secret and server_handshake_traffic_secret
1428 * are derived in the handshake secret derivation stage.
1429 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001431 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001432 mbedtls_ssl_tls13_named_group_is_ffdh(handshake->offered_group_id)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001433#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001434 psa_algorithm_t alg =
1435 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1436 PSA_ALG_ECDH : PSA_ALG_FFDH;
1437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001439 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001440 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1441
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001442 status = psa_get_key_attributes(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 &key_attributes);
1444 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001445 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 }
Ronald Cron3b056202022-10-05 17:20:21 +02001447
1448 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 psa_get_key_bits(&key_attributes));
1450 shared_secret = mbedtls_calloc(1, shared_secret_len);
1451 if (shared_secret == NULL) {
1452 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1453 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001454
Ronald Cron4c7edb22022-10-05 15:37:11 +02001455 status = psa_raw_key_agreement(
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001456 alg, handshake->xxdh_psa_privkey,
1457 handshake->xxdh_psa_peerkey, handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 shared_secret, shared_secret_len, &shared_secret_len);
1459 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001460 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001462 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001463 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001464
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001465 status = psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001467 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001469 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001470 }
1471
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001472 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001473#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 } else {
1475 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1476 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001477 }
1478 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001479#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001480
1481 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001482 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001483 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001484 ret = mbedtls_ssl_tls13_evolve_secret(
1485 hash_alg, handshake->tls13_master_secrets.early,
1486 shared_secret, shared_secret_len,
1487 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (ret != 0) {
1489 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001490 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001491 }
1492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1494 handshake->tls13_master_secrets.handshake,
1495 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001496
Ronald Cron3b056202022-10-05 17:20:21 +02001497cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (shared_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001499 mbedtls_zeroize_and_free(shared_secret, shared_secret_len);
Ronald Cron3b056202022-10-05 17:20:21 +02001500 }
1501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001503}
1504
Yanray Wang05402112022-12-13 18:50:42 +08001505/**
1506 * \brief Compute TLS 1.3 application traffic keys.
1507 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001508 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001509 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001510 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001511 *
1512 * \param ssl The SSL context to operate on. This must be in
1513 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001514 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001515 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001516 * keys. This must be writable but may be uninitialized.
1517 *
1518 * \returns \c 0 on success.
1519 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001520 */
Yanray Wang05402112022-12-13 18:50:42 +08001521MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001522static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 mbedtls_ssl_context *ssl,
1524 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001525{
XiaokangQiana7634982021-10-22 06:32:32 +00001526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001527 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001528
1529 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001530 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001531 &ssl->session_negotiate->app_secrets;
1532
1533 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001534 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001535 size_t transcript_len;
1536
1537 /* Variables relating to the hash for the chosen ciphersuite. */
1538 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001539
1540 psa_algorithm_t hash_alg;
1541 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001542
1543 /* Variables relating to the cipher for the chosen ciphersuite. */
Paul Elliott88600212023-11-23 14:24:30 +00001544 size_t key_len = 0, iv_len = 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001547
1548 /* Extract basic information about hash and ciphersuite */
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1551 &key_len, &iv_len);
1552 if (ret != 0) {
1553 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001554 goto cleanup;
1555 }
1556
Dave Rodgman2eab4622023-10-05 13:30:37 +01001557 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001558
Dave Rodgman2eab4622023-10-05 13:30:37 +01001559 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001561
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001562 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001563 * to call this at the right time, that is, after the ServerFinished. */
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1566 transcript, sizeof(transcript),
1567 &transcript_len);
1568 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001569 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001571
1572 /* Compute application secrets from master secret and transcript hash. */
1573
Xiaokang Qian123cde82023-03-29 06:54:51 +00001574 ret = mbedtls_ssl_tls13_derive_application_secrets(
1575 hash_alg, handshake->tls13_master_secrets.app,
1576 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001578 MBEDTLS_SSL_DEBUG_RET(
1579 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001580 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001581 }
1582
1583 /* Derive first epoch of IV + Key for application traffic. */
1584
Xiaokang Qian123cde82023-03-29 06:54:51 +00001585 ret = mbedtls_ssl_tls13_make_traffic_keys(
1586 hash_alg,
1587 app_secrets->client_application_traffic_secret_N,
1588 app_secrets->server_application_traffic_secret_N,
1589 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (ret != 0) {
1591 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001592 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001593 }
1594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1596 app_secrets->client_application_traffic_secret_N,
1597 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1600 app_secrets->server_application_traffic_secret_N,
1601 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001602
XiaokangQianac0385c2021-11-03 06:40:11 +00001603 /*
1604 * Export client/server application traffic secret 0
1605 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001607 ssl->f_export_keys(
1608 ssl->p_export_keys,
1609 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1610 app_secrets->client_application_traffic_secret_N, hash_len,
1611 handshake->randbytes,
1612 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1613 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1614 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001615
Xiaokang Qian123cde82023-03-29 06:54:51 +00001616 ssl->f_export_keys(
1617 ssl->p_export_keys,
1618 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1619 app_secrets->server_application_traffic_secret_N, hash_len,
1620 handshake->randbytes,
1621 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1622 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1623 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001624 }
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1627 traffic_keys->client_write_key, key_len);
1628 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1629 traffic_keys->server_write_key, key_len);
1630 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1631 traffic_keys->client_write_iv, iv_len);
1632 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1633 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001638 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1640 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1643 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001644}
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001647{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001649 mbedtls_ssl_key_set traffic_keys;
1650 mbedtls_ssl_transform *transform_handshake = NULL;
1651 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1652
1653 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001654 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if (ret != 0) {
1656 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001657 goto cleanup;
1658 }
1659
1660 /* Next evolution in key schedule: Establish handshake secret and
1661 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001662 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001664 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001666 goto cleanup;
1667 }
1668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1670 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001671 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1672 goto cleanup;
1673 }
1674
Jerry Yuef2b98a2022-05-06 16:40:05 +08001675 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 transform_handshake,
1677 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001678 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 &traffic_keys,
1680 ssl);
1681 if (ret != 0) {
1682 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001683 goto cleanup;
1684 }
1685 handshake->transform_handshake = transform_handshake;
1686
1687cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1689 if (ret != 0) {
1690 mbedtls_free(transform_handshake);
1691 }
Jerry Yue110d252022-05-05 10:19:22 +08001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001694}
1695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001697{
Jerry Yu46bffe02022-09-13 11:25:28 +08001698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001699 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001700 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1701 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001702 size_t transcript_len;
1703
Xiaokang Qian123cde82023-03-29 06:54:51 +00001704 MBEDTLS_SSL_DEBUG_MSG(
1705 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001706
Dave Rodgman2eab4622023-10-05 13:30:37 +01001707 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1710 transcript, sizeof(transcript),
1711 &transcript_len);
1712 if (ret != 0) {
1713 return ret;
1714 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001715
1716 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001717 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 handshake->tls13_master_secrets.app,
1719 transcript, transcript_len,
1720 &ssl->session_negotiate->app_secrets);
1721 if (ret != 0) {
1722 return ret;
1723 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001724
Jerry Yuff226982022-04-16 16:52:57 +08001725 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1727 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001728
Xiaokang Qian123cde82023-03-29 06:54:51 +00001729 MBEDTLS_SSL_DEBUG_BUF(
1730 4, "Resumption master secret",
1731 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001732 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001733
Xiaokang Qian123cde82023-03-29 06:54:51 +00001734 MBEDTLS_SSL_DEBUG_MSG(
1735 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001737}
1738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001740{
1741 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1742 mbedtls_ssl_key_set traffic_keys;
1743 mbedtls_ssl_transform *transform_application = NULL;
1744
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001745 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (ret != 0) {
1747 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001748 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001749 goto cleanup;
1750 }
1751
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001752 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (ret != 0) {
1754 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001755 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001756 goto cleanup;
1757 }
1758
1759 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1761 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001762 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1763 goto cleanup;
1764 }
1765
1766 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 transform_application,
1768 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001769 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 &traffic_keys,
1771 ssl);
1772 if (ret != 0) {
1773 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001774 goto cleanup;
1775 }
1776
1777 ssl->transform_application = transform_application;
1778
1779cleanup:
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1782 if (ret != 0) {
1783 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001784 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001786}
1787
Ronald Cron41a443a2022-10-04 16:38:25 +02001788#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001789int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1790 unsigned char **psk,
1791 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001792{
Jerry Yu40f37712022-07-26 16:58:57 +08001793 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001795
1796 *psk_len = 0;
1797 *psk = NULL;
1798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1800 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001801 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001802
1803 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1804 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001805 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 }
1807
1808 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1809 *psk = mbedtls_calloc(1, *psk_len);
1810 if (*psk == NULL) {
1811 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1812 }
1813
1814 status = psa_export_key(ssl->handshake->psk_opaque,
1815 (uint8_t *) *psk, *psk_len, psk_len);
1816 if (status != PSA_SUCCESS) {
1817 mbedtls_free((void *) *psk);
1818 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001819 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 }
1821 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001822}
Ronald Cron41a443a2022-10-04 16:38:25 +02001823#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001824
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001825int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
1826 const unsigned char *secret, const size_t secret_len,
1827 const unsigned char *label, const size_t label_len,
1828 const unsigned char *context_value, const size_t context_len,
1829 unsigned char *out, const size_t out_len)
1830{
1831 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
1832 unsigned char hkdf_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001833 int ret = 0;
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001834
1835 ret = mbedtls_ssl_tls13_derive_secret(hash_alg, secret, secret_len, label, label_len, NULL, 0,
Max Fillinger7b722202024-09-21 10:48:57 +02001836 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, hkdf_secret,
1837 hash_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001838 if (ret != 0) {
1839 goto exit;
1840 }
Max Fillinger7b722202024-09-21 10:48:57 +02001841 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
1842 hkdf_secret,
1843 hash_len,
Max Fillinger334c3672024-08-12 11:20:39 +02001844 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter),
Max Fillinger7b722202024-09-21 10:48:57 +02001845 context_value,
1846 context_len,
1847 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
1848 out,
1849 out_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001850
1851exit:
1852 mbedtls_platform_zeroize(hkdf_secret, sizeof(hkdf_secret));
1853 return ret;
1854}
1855
Ronald Cron6f135e12021-12-08 16:57:54 +01001856#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */