blob: dbc703a6c124961cfe38973ed4a0cae4867ba870 [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Hanno Beckerbe9d6642020-08-21 13:20:06 +01006 */
7
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "ssl_misc.h"
Hanno Beckerbe9d6642020-08-21 13:20:06 +01009
Ronald Cron6f135e12021-12-08 16:57:54 +010010#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010011
Hanno Beckerbe9d6642020-08-21 13:20:06 +010012#include <stdint.h>
13#include <string.h>
14
Valerio Settib4f50762024-01-17 10:24:52 +010015#include "debug_internal.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080016#include "mbedtls/error.h"
Jerry Yue110d252022-05-05 10:19:22 +080017#include "mbedtls/platform.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080018
Jerry Yue3131ef2021-09-16 13:14:15 +080019#include "ssl_tls13_keys.h"
Gabor Mezeia3eecd22022-02-09 16:57:26 +010020#include "ssl_tls13_invasive.h"
21
22#include "psa/crypto.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010023#include "mbedtls/psa_util.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080024
Andrzej Kurek00644842023-05-30 05:45:00 -040025/* Define a local translating function to save code size by not using too many
26 * arguments in each translating place. */
27static int local_err_translation(psa_status_t status)
28{
29 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040030 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040031 psa_generic_status_to_mbedtls);
32}
33#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050034
Gilles Peskine449bd832023-01-11 14:50:10 +010035#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010036 .name = string,
37
Xiaofei Bai746f9482021-11-12 08:53:56 +000038struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010039{
40 /* This seems to work in C, despite the string literal being one
41 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010042 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010043};
44
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010045#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010046
Hanno Beckerbe9d6642020-08-21 13:20:06 +010047/*
48 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
49 *
50 * The HkdfLabel is specified in RFC 8446 as follows:
51 *
52 * struct HkdfLabel {
53 * uint16 length; // Length of expanded key material
54 * opaque label<7..255>; // Always prefixed by "tls13 "
55 * opaque context<0..255>; // Usually a communication transcript hash
56 * };
57 *
58 * Parameters:
Max Fillingeree33b312024-12-02 19:26:13 +010059 * - desired_length: Length of expanded key material.
Max Fillingeraf2035f2024-12-02 19:34:40 +010060 * The length field can hold numbers up to 2**16, but HKDF
61 * can only generate outputs of up to 255 * HASH_LEN bytes.
62 * It is the caller's responsibility to ensure that this
63 * limit is not exceeded. In TLS 1.3, SHA256 is the hash
64 * function with the smallest block size, so a length
65 * <= 255 * 32 = 8160 is always safe.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000066 * - (label, label_len): label + label length, without "tls13 " prefix
67 * The label length MUST be less than or equal to
Max Fillinger58268832024-11-25 20:38:04 +010068 * MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000069 * It is the caller's responsibility to ensure this.
70 * All (label, label length) pairs used in TLS 1.3
71 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
72 * - (ctx, ctx_len): context + context length
73 * The context length MUST be less than or equal to
74 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
75 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010076 * - dst: Target buffer for HkdfLabel structure,
77 * This MUST be a writable buffer of size
78 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000079 * - dst_len: Pointer at which to store the actual length of
80 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010081 */
82
Xiaofei Baid25fab62021-12-02 06:36:27 +000083static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
86 (2 /* expansion length */ \
87 + 1 /* label length */ \
88 + label_len \
89 + 1 /* context length */ \
90 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010091
92#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
93 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +010094 sizeof(tls13_label_prefix) + \
Max Fillinger58268832024-11-25 20:38:04 +010095 MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN, \
Gilles Peskine449bd832023-01-11 14:50:10 +010096 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010097
Xiaofei Bai746f9482021-11-12 08:53:56 +000098static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +010099 size_t desired_length,
100 const unsigned char *label, size_t label_len,
101 const unsigned char *ctx, size_t ctx_len,
102 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100103{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100104 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000105 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100106 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100108
109 unsigned char *p = dst;
110
Max Fillinger28916ac2024-10-29 18:49:30 +0100111 /* Add the size of the expanded key material. */
112#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > UINT16_MAX
113#error "The desired key length must fit into an uint16 but \
114 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN is greater than UINT16_MAX"
Hanno Becker531fe302020-09-16 09:45:27 +0100115#endif
116
Max Fillinger28916ac2024-10-29 18:49:30 +0100117 *p++ = MBEDTLS_BYTE_1(desired_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100119
120 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 *p++ = MBEDTLS_BYTE_0(total_label_len);
122 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000123 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000125 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100126
127 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 *p++ = MBEDTLS_BYTE_0(ctx_len);
129 if (ctx_len != 0) {
130 memcpy(p, ctx, ctx_len);
131 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100132
133 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000134 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100135}
136
Xiaofei Bai746f9482021-11-12 08:53:56 +0000137int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 psa_algorithm_t hash_alg,
139 const unsigned char *secret, size_t secret_len,
140 const unsigned char *label, size_t label_len,
141 const unsigned char *ctx, size_t ctx_len,
142 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100143{
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200145 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200146 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
147 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200148 psa_key_derivation_operation_t operation =
149 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100150
Max Fillinger58268832024-11-25 20:38:04 +0100151 if (label_len > MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100152 /* Should never happen since this is an internal
153 * function, and we know statically which labels
154 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100156 }
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100159 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100161 }
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100164 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100166 }
167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if (!PSA_ALG_IS_HASH(hash_alg)) {
169 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
170 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 ssl_tls13_hkdf_encode_label(buf_len,
173 label, label_len,
174 ctx, ctx_len,
175 hkdf_label,
176 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (status != PSA_SUCCESS) {
181 goto cleanup;
182 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 status = psa_key_derivation_input_bytes(&operation,
185 PSA_KEY_DERIVATION_INPUT_SECRET,
186 secret,
187 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (status != PSA_SUCCESS) {
190 goto cleanup;
191 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 status = psa_key_derivation_input_bytes(&operation,
194 PSA_KEY_DERIVATION_INPUT_INFO,
195 hkdf_label,
196 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (status != PSA_SUCCESS) {
199 goto cleanup;
200 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 status = psa_key_derivation_output_bytes(&operation,
203 buf,
204 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if (status != PSA_SUCCESS) {
207 goto cleanup;
208 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200209
210cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 abort_status = psa_key_derivation_abort(&operation);
212 status = (status == PSA_SUCCESS ? abort_status : status);
213 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500214 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100215}
216
Jerry Yua5db6c02022-11-23 18:08:04 +0800217MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800218static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 psa_algorithm_t hash_alg,
220 const unsigned char *secret, size_t secret_len,
221 unsigned char *key, size_t key_len,
222 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800223{
224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
225
Jerry Yuaec08b32022-11-29 15:19:27 +0800226 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 hash_alg,
228 secret, secret_len,
229 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
230 NULL, 0,
231 key, key_len);
232 if (ret != 0) {
233 return ret;
234 }
Jerry Yua8771832022-11-21 23:16:54 +0800235
Jerry Yuaec08b32022-11-29 15:19:27 +0800236 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 hash_alg,
238 secret, secret_len,
239 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
240 NULL, 0,
241 iv, iv_len);
242 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800243}
244
Hanno Becker3385a4d2020-08-21 13:03:34 +0100245/*
246 * The traffic keying material is generated from the following inputs:
247 *
248 * - One secret value per sender.
249 * - A purpose value indicating the specific value being generated
250 * - The desired lengths of key and IV.
251 *
252 * The expansion itself is based on HKDF:
253 *
254 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
255 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
256 *
257 * [sender] denotes the sending side and the Secret value is provided
258 * by the function caller. Note that we generate server and client side
259 * keys in a single function call.
260 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000261int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 psa_algorithm_t hash_alg,
263 const unsigned char *client_secret,
264 const unsigned char *server_secret, size_t secret_len,
265 size_t key_len, size_t iv_len,
266 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100267{
268 int ret = 0;
269
Jerry Yua8771832022-11-21 23:16:54 +0800270 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 hash_alg, client_secret, secret_len,
272 keys->client_write_key, key_len,
273 keys->client_write_iv, iv_len);
274 if (ret != 0) {
275 return ret;
276 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100277
Jerry Yua8771832022-11-21 23:16:54 +0800278 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 hash_alg, server_secret, secret_len,
280 keys->server_write_key, key_len,
281 keys->server_write_iv, iv_len);
282 if (ret != 0) {
283 return ret;
284 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100285
Hanno Becker493ea7f2020-09-08 11:01:00 +0100286 keys->key_len = key_len;
287 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100290}
291
Xiaofei Bai746f9482021-11-12 08:53:56 +0000292int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 psa_algorithm_t hash_alg,
294 const unsigned char *secret, size_t secret_len,
295 const unsigned char *label, size_t label_len,
296 const unsigned char *ctx, size_t ctx_len,
297 int ctx_hashed,
298 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100299{
300 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
302 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100303 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
306 PSA_HASH_LENGTH(hash_alg), &ctx_len);
307 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500308 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100309 return ret;
310 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 } else {
312 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100313 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100314 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100315 * Let's double-check nonetheless to not run at the risk
316 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100318 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100321 }
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
324 secret, secret_len,
325 label, label_len,
326 hashed_context, ctx_len,
327 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100328
Hanno Beckerb35d5222020-08-21 13:27:44 +0100329}
330
Xiaofei Bai746f9482021-11-12 08:53:56 +0000331int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 psa_algorithm_t hash_alg,
333 const unsigned char *secret_old,
334 const unsigned char *input, size_t input_len,
335 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100336{
337 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
339 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200340 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
342 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200343 const unsigned char *l_input = NULL;
344 size_t l_input_len;
345
Przemek Stekield5ae3652022-05-13 12:10:08 +0200346 psa_key_derivation_operation_t operation =
347 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (!PSA_ALG_IS_HASH(hash_alg)) {
350 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
351 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100354
355 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100356 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000358 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 hash_alg,
360 secret_old, hlen,
361 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
362 NULL, 0, /* context */
363 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
364 tmp_secret, hlen);
365 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100366 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100368 }
369
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200370 ret = 0;
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200373 l_input = input;
374 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200376 l_input = all_zeroes_input;
377 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100378 }
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 status = psa_key_derivation_setup(&operation,
381 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (status != PSA_SUCCESS) {
384 goto cleanup;
385 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 status = psa_key_derivation_input_bytes(&operation,
388 PSA_KEY_DERIVATION_INPUT_SALT,
389 tmp_secret,
390 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (status != PSA_SUCCESS) {
393 goto cleanup;
394 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 status = psa_key_derivation_input_bytes(&operation,
397 PSA_KEY_DERIVATION_INPUT_SECRET,
398 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (status != PSA_SUCCESS) {
401 goto cleanup;
402 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 status = psa_key_derivation_output_bytes(&operation,
405 secret_new,
406 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (status != PSA_SUCCESS) {
409 goto cleanup;
410 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412cleanup:
413 abort_status = psa_key_derivation_abort(&operation);
414 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500415 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
417 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100418}
419
Xiaofei Bai746f9482021-11-12 08:53:56 +0000420int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 psa_algorithm_t hash_alg,
422 unsigned char const *early_secret,
423 unsigned char const *transcript, size_t transcript_len,
424 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100425{
426 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100428
429 /* We should never call this function with an unknown hash,
430 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (!PSA_ALG_IS_HASH(hash_alg)) {
432 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
433 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100434
435 /*
436 * 0
437 * |
438 * v
439 * PSK -> HKDF-Extract = Early Secret
440 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100441 * +-----> Derive-Secret(., "c e traffic", ClientHello)
442 * | = client_early_traffic_secret
443 * |
444 * +-----> Derive-Secret(., "e exp master", ClientHello)
445 * | = early_exporter_master_secret
446 * v
447 */
448
449 /* Create client_early_traffic_secret */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000450 ret = mbedtls_ssl_tls13_derive_secret(
451 hash_alg,
452 early_secret, hash_len,
453 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
454 transcript, transcript_len,
455 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
456 derived->client_early_traffic_secret,
457 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (ret != 0) {
459 return ret;
460 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100461
462 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000463 ret = mbedtls_ssl_tls13_derive_secret(
464 hash_alg,
465 early_secret, hash_len,
466 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
467 transcript, transcript_len,
468 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
469 derived->early_exporter_master_secret,
470 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (ret != 0) {
472 return ret;
473 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100476}
477
Xiaofei Bai746f9482021-11-12 08:53:56 +0000478int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 psa_algorithm_t hash_alg,
480 unsigned char const *handshake_secret,
481 unsigned char const *transcript, size_t transcript_len,
482 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100483{
484 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100486
487 /* We should never call this function with an unknown hash,
488 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (!PSA_ALG_IS_HASH(hash_alg)) {
490 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
491 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100492
493 /*
494 *
495 * Handshake Secret
496 * |
497 * +-----> Derive-Secret( ., "c hs traffic",
498 * | ClientHello...ServerHello )
499 * | = client_handshake_traffic_secret
500 * |
501 * +-----> Derive-Secret( ., "s hs traffic",
502 * | ClientHello...ServerHello )
503 * | = server_handshake_traffic_secret
504 *
505 */
506
507 /*
508 * Compute client_handshake_traffic_secret with
509 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
510 */
511
Xiaokang Qian123cde82023-03-29 06:54:51 +0000512 ret = mbedtls_ssl_tls13_derive_secret(
513 hash_alg,
514 handshake_secret, hash_len,
515 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
516 transcript, transcript_len,
517 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
518 derived->client_handshake_traffic_secret,
519 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (ret != 0) {
521 return ret;
522 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100523
524 /*
525 * Compute server_handshake_traffic_secret with
526 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
527 */
528
Xiaokang Qian123cde82023-03-29 06:54:51 +0000529 ret = mbedtls_ssl_tls13_derive_secret(
530 hash_alg,
531 handshake_secret, hash_len,
532 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
533 transcript, transcript_len,
534 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
535 derived->server_handshake_traffic_secret,
536 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (ret != 0) {
538 return ret;
539 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100542}
543
Xiaofei Bai746f9482021-11-12 08:53:56 +0000544int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 psa_algorithm_t hash_alg,
546 unsigned char const *application_secret,
547 unsigned char const *transcript, size_t transcript_len,
548 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100549{
550 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100552
553 /* We should never call this function with an unknown hash,
554 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (!PSA_ALG_IS_HASH(hash_alg)) {
556 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
557 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100558
559 /* Generate {client,server}_application_traffic_secret_0
560 *
561 * Master Secret
562 * |
563 * +-----> Derive-Secret( ., "c ap traffic",
564 * | ClientHello...server Finished )
565 * | = client_application_traffic_secret_0
566 * |
567 * +-----> Derive-Secret( ., "s ap traffic",
568 * | ClientHello...Server Finished )
569 * | = server_application_traffic_secret_0
570 * |
571 * +-----> Derive-Secret( ., "exp master",
572 * | ClientHello...server Finished)
573 * | = exporter_master_secret
574 *
575 */
576
Xiaokang Qian123cde82023-03-29 06:54:51 +0000577 ret = mbedtls_ssl_tls13_derive_secret(
578 hash_alg,
579 application_secret, hash_len,
580 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
581 transcript, transcript_len,
582 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
583 derived->client_application_traffic_secret_N,
584 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (ret != 0) {
586 return ret;
587 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100588
Xiaokang Qian123cde82023-03-29 06:54:51 +0000589 ret = mbedtls_ssl_tls13_derive_secret(
590 hash_alg,
591 application_secret, hash_len,
592 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
593 transcript, transcript_len,
594 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
595 derived->server_application_traffic_secret_N,
596 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (ret != 0) {
598 return ret;
599 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100600
Xiaokang Qian123cde82023-03-29 06:54:51 +0000601 ret = mbedtls_ssl_tls13_derive_secret(
602 hash_alg,
603 application_secret, hash_len,
604 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
605 transcript, transcript_len,
606 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
607 derived->exporter_master_secret,
608 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if (ret != 0) {
610 return ret;
611 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100614}
615
616/* Generate resumption_master_secret for use with the ticket exchange.
617 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000618 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100619 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000620int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 psa_algorithm_t hash_alg,
622 unsigned char const *application_secret,
623 unsigned char const *transcript, size_t transcript_len,
624 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100625{
626 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100628
629 /* We should never call this function with an unknown hash,
630 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (!PSA_ALG_IS_HASH(hash_alg)) {
632 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
633 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100634
Xiaokang Qian123cde82023-03-29 06:54:51 +0000635 ret = mbedtls_ssl_tls13_derive_secret(
636 hash_alg,
637 application_secret, hash_len,
638 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
639 transcript, transcript_len,
640 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
641 derived->resumption_master_secret,
642 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (ret != 0) {
645 return ret;
646 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100649}
650
Yanray Wang05402112022-12-13 18:50:42 +0800651/**
652 * \brief Transition into application stage of TLS 1.3 key schedule.
653 *
654 * The TLS 1.3 key schedule can be viewed as a simple state machine
655 * with states Initial -> Early -> Handshake -> Application, and
656 * this function represents the Handshake -> Application transition.
657 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800658 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800659 * can be used to derive the handshake traffic keys.
660 *
661 * \param ssl The SSL context to operate on. This must be in key schedule
662 * stage \c Handshake.
663 *
664 * \returns \c 0 on success.
665 * \returns A negative error code on failure.
666 */
667MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800668static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000669{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000671 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200672 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100673 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000674
675 /*
676 * Compute MasterSecret
677 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000678 ret = mbedtls_ssl_tls13_evolve_secret(
679 hash_alg,
680 handshake->tls13_master_secrets.handshake,
681 NULL, 0,
682 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (ret != 0) {
684 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
685 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000686 }
687
Xiaokang Qian123cde82023-03-29 06:54:51 +0000688 MBEDTLS_SSL_DEBUG_BUF(
689 4, "Master secret",
690 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000693}
694
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200695MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
697 unsigned char const *base_key,
698 unsigned char const *transcript,
699 unsigned char *dst,
700 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100701{
Gabor Mezei07732f72022-03-26 17:04:19 +0100702 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100706 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100707 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100708 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100709
710 /* We should never call this function with an unknown hash,
711 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (!PSA_ALG_IS_HASH(hash_alg)) {
713 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
714 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100715
716 /* TLS 1.3 Finished message
717 *
718 * struct {
719 * opaque verify_data[Hash.length];
720 * } Finished;
721 *
722 * verify_data =
723 * HMAC( finished_key,
724 * Hash( Handshake Context +
725 * Certificate* +
726 * CertificateVerify* )
727 * )
728 *
729 * finished_key =
730 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
731 */
732
Xiaofei Bai746f9482021-11-12 08:53:56 +0000733 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 hash_alg, base_key, hash_len,
735 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
736 NULL, 0,
737 finished_key, hash_len);
738 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100739 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100740 }
741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 alg = PSA_ALG_HMAC(hash_alg);
743 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
744 psa_set_key_algorithm(&attributes, alg);
745 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
746
747 status = psa_import_key(&attributes, finished_key, hash_len, &key);
748 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500749 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 goto exit;
751 }
752
753 status = psa_mac_compute(key, alg, transcript, hash_len,
754 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500755 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100756
757exit:
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 status = psa_destroy_key(key);
760 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500761 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100767}
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
770 unsigned char *dst,
771 size_t dst_len,
772 size_t *actual_len,
773 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000774{
XiaokangQiana7634982021-10-22 06:32:32 +0000775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000776
XiaokangQianaaa0e192021-11-10 03:07:04 +0000777 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000778 size_t transcript_len;
779
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800780 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800781 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800782 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800784
Dave Rodgman2eab4622023-10-05 13:30:37 +0100785 mbedtls_md_type_t const md_type = (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100786
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200787 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100788 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800794 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
796 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800797 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800799 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800802 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
803 goto exit;
804 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
807 transcript, sizeof(transcript),
808 &transcript_len);
809 if (ret != 0) {
810 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000811 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000812 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000814
Xiaokang Qian123cde82023-03-29 06:54:51 +0000815 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
816 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000818 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
822 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000823
824exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800825 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 mbedtls_platform_zeroize(base_key, base_key_len);
827 mbedtls_platform_zeroize(transcript, sizeof(transcript));
828 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000829}
830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
832 const psa_algorithm_t hash_alg,
833 unsigned char const *psk, size_t psk_len,
834 int psk_type,
835 unsigned char const *transcript,
836 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100837{
838 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100839 unsigned char binder_key[PSA_MAC_MAX_SIZE];
840 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100842 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100843
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100844#if !defined(MBEDTLS_DEBUG_C)
845 ssl = NULL; /* make sure we don't use it except for debug */
846 ((void) ssl);
847#endif
848
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100849 /* We should never call this function with an unknown hash,
850 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (!PSA_ALG_IS_HASH(hash_alg)) {
852 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
853 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100854
855 /*
856 * 0
857 * |
858 * v
859 * PSK -> HKDF-Extract = Early Secret
860 * |
861 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
862 * | = binder_key
863 * v
864 */
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
867 NULL, /* Old secret */
868 psk, psk_len, /* Input */
869 early_secret);
870 if (ret != 0) {
871 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100872 goto exit;
873 }
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
876 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000879 ret = mbedtls_ssl_tls13_derive_secret(
880 hash_alg,
881 early_secret, hash_len,
882 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
883 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
884 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
886 } else {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000887 ret = mbedtls_ssl_tls13_derive_secret(
888 hash_alg,
889 early_secret, hash_len,
890 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
891 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
892 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100894 }
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (ret != 0) {
897 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100898 goto exit;
899 }
900
901 /*
902 * The binding_value is computed in the same way as the Finished message
903 * but with the BaseKey being the binder_key.
904 */
905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
907 result, &actual_len);
908 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100909 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100913
914exit:
915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
917 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
918 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100919}
920
Xiaokang Qian123cde82023-03-29 06:54:51 +0000921int mbedtls_ssl_tls13_populate_transform(
922 mbedtls_ssl_transform *transform,
923 int endpoint, int ciphersuite,
924 mbedtls_ssl_key_set const *traffic_keys,
925 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000926{
Hanno Beckerc94060c2021-03-22 07:50:44 +0000927 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
928 unsigned char const *key_enc;
929 unsigned char const *iv_enc;
930 unsigned char const *key_dec;
931 unsigned char const *iv_dec;
932
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100933 psa_key_type_t key_type;
934 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
935 psa_algorithm_t alg;
936 size_t key_bits;
937 psa_status_t status = PSA_SUCCESS;
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100938
Hanno Beckerc94060c2021-03-22 07:50:44 +0000939#if !defined(MBEDTLS_DEBUG_C)
940 ssl = NULL; /* make sure we don't use it except for those cases */
941 (void) ssl;
942#endif
943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
945 if (ciphersuite_info == NULL) {
946 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
947 ciphersuite));
948 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100949 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000950
Hanno Beckerc94060c2021-03-22 07:50:44 +0000951
952#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000954 key_enc = traffic_keys->server_write_key;
955 key_dec = traffic_keys->client_write_key;
956 iv_enc = traffic_keys->server_write_iv;
957 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000959#endif /* MBEDTLS_SSL_SRV_C */
960#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000962 key_enc = traffic_keys->client_write_key;
963 key_dec = traffic_keys->server_write_key;
964 iv_enc = traffic_keys->client_write_iv;
965 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000967#endif /* MBEDTLS_SSL_CLI_C */
968 {
969 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000971 }
972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
974 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000975
Hanno Beckerc94060c2021-03-22 07:50:44 +0000976
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100977 /*
978 * Setup other fields in SSL transform
979 */
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100982 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100984 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100986
987 transform->ivlen = traffic_keys->iv_len;
988 transform->maclen = 0;
989 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -0400990 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100991
992 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800993 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100994 * type-extended and padded plaintext is therefore the padding
995 * granularity. */
996 transform->minlen =
997 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
998
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100999 /*
1000 * Setup psa keys and alg
1001 */
Dave Rodgman2eab4622023-10-05 13:30:37 +01001002 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 transform->taglen,
1004 &alg,
1005 &key_type,
1006 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001007 MBEDTLS_SSL_DEBUG_RET(
1008 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001009 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001010 }
1011
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001012 transform->psa_alg = alg;
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1015 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1016 psa_set_key_algorithm(&attributes, alg);
1017 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if ((status = psa_import_key(&attributes,
1020 key_enc,
1021 PSA_BITS_TO_BYTES(key_bits),
1022 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001023 MBEDTLS_SSL_DEBUG_RET(
1024 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001025 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001026 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if ((status = psa_import_key(&attributes,
1031 key_dec,
1032 PSA_BITS_TO_BYTES(key_bits),
1033 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001034 MBEDTLS_SSL_DEBUG_RET(
1035 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001036 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001037 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001038 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001041}
1042
Jerry Yu84a6eda2022-11-04 11:17:35 +08001043MBEDTLS_CHECK_RETURN_CRITICAL
1044static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1046 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001047{
1048 psa_key_type_t key_type;
1049 psa_algorithm_t alg;
1050 size_t taglen;
1051 size_t key_bits;
1052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001055 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001057 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001059
Dave Rodgman2eab4622023-10-05 13:30:37 +01001060 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, taglen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 &alg, &key_type, &key_bits);
1062 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001063 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001067
1068 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1069 *iv_len = 12;
1070
1071 return 0;
1072}
1073
Jerry Yu91b560f2022-11-04 14:10:34 +08001074#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001075/*
1076 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001077 * the early application data and handshake messages as described in section 7
1078 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001079 *
Jerry Yue31688b2022-11-22 21:55:56 +08001080 * NOTE: Only one key is generated, the key for the traffic from the client to
1081 * the server. The TLS 1.3 specification does not define a secret and thus
1082 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001083 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001084MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001085static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1086 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001087{
1088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001089 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001090 psa_algorithm_t hash_alg;
1091 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001092 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1093 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001094 size_t key_len = 0;
1095 size_t iv_len = 0;
Yanray Wang16c895d2022-12-15 15:14:35 +08001096 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001097
1098 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001099 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1100 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1105 if (ret != 0) {
1106 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001107 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001108 }
1109
Dave Rodgman2eab4622023-10-05 13:30:37 +01001110 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Jerry Yu91b560f2022-11-04 14:10:34 +08001111
Dave Rodgman2eab4622023-10-05 13:30:37 +01001112 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1116 transcript,
1117 sizeof(transcript),
1118 &transcript_len);
1119 if (ret != 0) {
1120 MBEDTLS_SSL_DEBUG_RET(1,
1121 "mbedtls_ssl_get_handshake_transcript",
1122 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001123 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001124 }
1125
Jerry Yub094e122022-11-21 13:03:47 +08001126 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001128 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001130 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001132 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001133 }
1134
1135 MBEDTLS_SSL_DEBUG_BUF(
1136 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001137 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001138
1139 /*
1140 * Export client handshake traffic secret
1141 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001143 ssl->f_export_keys(
1144 ssl->p_export_keys,
1145 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001146 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001147 hash_len,
1148 handshake->randbytes,
1149 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001151 }
1152
Jerry Yua8771832022-11-21 23:16:54 +08001153 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001155 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 hash_len, traffic_keys->client_write_key, key_len,
1157 traffic_keys->client_write_iv, iv_len);
1158 if (ret != 0) {
1159 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001160 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001161 }
Jerry Yua8771832022-11-21 23:16:54 +08001162 traffic_keys->key_len = key_len;
1163 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1166 traffic_keys->client_write_key,
1167 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1170 traffic_keys->client_write_iv,
1171 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001174
Jerry Yu3d78e082022-11-23 18:26:20 +08001175cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001176 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001177 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001178 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1180 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001181}
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001184{
1185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1186 mbedtls_ssl_key_set traffic_keys;
1187 mbedtls_ssl_transform *transform_earlydata = NULL;
1188 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1189
1190 /* Next evolution in key schedule: Establish early_data secret and
1191 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1193 if (ret != 0) {
1194 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1195 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001196 goto cleanup;
1197 }
1198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1200 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001201 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1202 goto cleanup;
1203 }
1204
1205 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 transform_earlydata,
1207 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001208 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 &traffic_keys,
1210 ssl);
1211 if (ret != 0) {
1212 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001213 goto cleanup;
1214 }
1215 handshake->transform_earlydata = transform_earlydata;
1216
1217cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1219 if (ret != 0) {
1220 mbedtls_free(transform_earlydata);
1221 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001224}
1225#endif /* MBEDTLS_SSL_EARLY_DATA */
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001228{
Jerry Yue3131ef2021-09-16 13:14:15 +08001229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001230 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001231 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001232 unsigned char *psk = NULL;
1233 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (handshake->ciphersuite_info == NULL) {
1236 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1237 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001238 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001239
Dave Rodgman2eab4622023-10-05 13:30:37 +01001240 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001241#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1243 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1244 if (ret != 0) {
1245 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1246 ret);
1247 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001248 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001249 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001250#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1253 handshake->tls13_master_secrets.early);
Manuel Pégourié-Gonnard0b44a812025-01-23 09:58:07 +01001254#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001256#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (ret != 0) {
1258 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1259 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001260 }
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1263 handshake->tls13_master_secrets.early,
1264 PSA_HASH_LENGTH(hash_alg));
1265 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001266}
1267
Yanray Wang05402112022-12-13 18:50:42 +08001268/**
1269 * \brief Compute TLS 1.3 handshake traffic keys.
1270 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001271 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1272 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001273 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001274 *
1275 * \param ssl The SSL context to operate on. This must be in
1276 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001277 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001278 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001279 * keys. This must be writable but may be uninitialized.
1280 *
1281 * \returns \c 0 on success.
1282 * \returns A negative error code on failure.
1283 */
1284MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001285static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1286 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001287{
1288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001289 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001290 psa_algorithm_t hash_alg;
1291 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001292 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001293 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001294 size_t key_len = 0;
1295 size_t iv_len = 0;
Jerry Yu61e35e02021-09-16 18:59:08 +08001296
Jerry Yu435208a2021-10-13 11:22:16 +08001297 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001298 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1299 handshake->ciphersuite_info;
1300 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1301 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001302
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001303 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1306 if (ret != 0) {
1307 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001308 return ret;
1309 }
1310
Dave Rodgman2eab4622023-10-05 13:30:37 +01001311 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001312
Dave Rodgman2eab4622023-10-05 13:30:37 +01001313 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1317 transcript,
1318 sizeof(transcript),
1319 &transcript_len);
1320 if (ret != 0) {
1321 MBEDTLS_SSL_DEBUG_RET(1,
1322 "mbedtls_ssl_get_handshake_transcript",
1323 ret);
1324 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001325 }
1326
Xiaokang Qian123cde82023-03-29 06:54:51 +00001327 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1328 hash_alg, handshake->tls13_master_secrets.handshake,
1329 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (ret != 0) {
1331 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1332 ret);
1333 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001334 }
1335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1337 tls13_hs_secrets->client_handshake_traffic_secret,
1338 hash_len);
1339 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1340 tls13_hs_secrets->server_handshake_traffic_secret,
1341 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001342
1343 /*
1344 * Export client handshake traffic secret
1345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001347 ssl->f_export_keys(
1348 ssl->p_export_keys,
1349 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1350 tls13_hs_secrets->client_handshake_traffic_secret,
1351 hash_len,
1352 handshake->randbytes,
1353 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1354 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001355
Xiaokang Qian123cde82023-03-29 06:54:51 +00001356 ssl->f_export_keys(
1357 ssl->p_export_keys,
1358 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1359 tls13_hs_secrets->server_handshake_traffic_secret,
1360 hash_len,
1361 handshake->randbytes,
1362 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1363 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001364 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001365
Xiaokang Qian123cde82023-03-29 06:54:51 +00001366 ret = mbedtls_ssl_tls13_make_traffic_keys(
1367 hash_alg,
1368 tls13_hs_secrets->client_handshake_traffic_secret,
1369 tls13_hs_secrets->server_handshake_traffic_secret,
1370 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if (ret != 0) {
1372 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001373 goto exit;
1374 }
1375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1377 traffic_keys->client_write_key,
1378 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1381 traffic_keys->server_write_key,
1382 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1385 traffic_keys->client_write_iv,
1386 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1389 traffic_keys->server_write_iv,
1390 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001391
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001392 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001393
1394exit:
1395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001397}
1398
Yanray Wang05402112022-12-13 18:50:42 +08001399/**
1400 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1401 *
1402 * The TLS 1.3 key schedule can be viewed as a simple state machine
1403 * with states Initial -> Early -> Handshake -> Application, and
1404 * this function represents the Early -> Handshake transition.
1405 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001406 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001407 * can be used to derive the handshake traffic keys.
1408 *
1409 * \param ssl The SSL context to operate on. This must be in key schedule
1410 * stage \c Early.
1411 *
1412 * \returns \c 0 on success.
1413 * \returns A negative error code on failure.
1414 */
1415MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001416static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001417{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001419 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001420 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001421 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001422 unsigned char *shared_secret = NULL;
1423 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001424
Ronald Crona2900bc2022-10-20 14:37:35 +02001425#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001426 /*
1427 * Compute ECDHE secret used to compute the handshake secret from which
1428 * client_handshake_traffic_secret and server_handshake_traffic_secret
1429 * are derived in the handshake secret derivation stage.
1430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001432 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001433 mbedtls_ssl_tls13_named_group_is_ffdh(handshake->offered_group_id)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001434#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001435 psa_algorithm_t alg =
1436 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1437 PSA_ALG_ECDH : PSA_ALG_FFDH;
1438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001440 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001441 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1442
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001443 status = psa_get_key_attributes(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 &key_attributes);
1445 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001446 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 }
Ronald Cron3b056202022-10-05 17:20:21 +02001448
1449 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 psa_get_key_bits(&key_attributes));
1451 shared_secret = mbedtls_calloc(1, shared_secret_len);
1452 if (shared_secret == NULL) {
1453 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1454 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001455
Ronald Cron4c7edb22022-10-05 15:37:11 +02001456 status = psa_raw_key_agreement(
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001457 alg, handshake->xxdh_psa_privkey,
1458 handshake->xxdh_psa_peerkey, handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 shared_secret, shared_secret_len, &shared_secret_len);
1460 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001461 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001463 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001464 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001465
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001466 status = psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001468 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001470 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001471 }
1472
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001473 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001474#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 } else {
1476 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1477 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001478 }
1479 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001480#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001481
1482 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001483 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001484 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001485 ret = mbedtls_ssl_tls13_evolve_secret(
1486 hash_alg, handshake->tls13_master_secrets.early,
1487 shared_secret, shared_secret_len,
1488 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (ret != 0) {
1490 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001491 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001492 }
1493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1495 handshake->tls13_master_secrets.handshake,
1496 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001497
Ronald Cron3b056202022-10-05 17:20:21 +02001498cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (shared_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001500 mbedtls_zeroize_and_free(shared_secret, shared_secret_len);
Ronald Cron3b056202022-10-05 17:20:21 +02001501 }
1502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001504}
1505
Yanray Wang05402112022-12-13 18:50:42 +08001506/**
1507 * \brief Compute TLS 1.3 application traffic keys.
1508 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001509 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001510 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001511 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001512 *
1513 * \param ssl The SSL context to operate on. This must be in
1514 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001515 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001516 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001517 * keys. This must be writable but may be uninitialized.
1518 *
1519 * \returns \c 0 on success.
1520 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001521 */
Yanray Wang05402112022-12-13 18:50:42 +08001522MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001523static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 mbedtls_ssl_context *ssl,
1525 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001526{
XiaokangQiana7634982021-10-22 06:32:32 +00001527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001528 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001529
1530 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001531 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001532 &ssl->session_negotiate->app_secrets;
1533
1534 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001535 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001536 size_t transcript_len;
1537
1538 /* Variables relating to the hash for the chosen ciphersuite. */
1539 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001540
1541 psa_algorithm_t hash_alg;
1542 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001543
1544 /* Variables relating to the cipher for the chosen ciphersuite. */
Paul Elliott88600212023-11-23 14:24:30 +00001545 size_t key_len = 0, iv_len = 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001548
1549 /* Extract basic information about hash and ciphersuite */
1550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1552 &key_len, &iv_len);
1553 if (ret != 0) {
1554 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001555 goto cleanup;
1556 }
1557
Dave Rodgman2eab4622023-10-05 13:30:37 +01001558 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001559
Dave Rodgman2eab4622023-10-05 13:30:37 +01001560 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001562
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001563 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001564 * to call this at the right time, that is, after the ServerFinished. */
1565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1567 transcript, sizeof(transcript),
1568 &transcript_len);
1569 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001570 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001572
1573 /* Compute application secrets from master secret and transcript hash. */
1574
Xiaokang Qian123cde82023-03-29 06:54:51 +00001575 ret = mbedtls_ssl_tls13_derive_application_secrets(
1576 hash_alg, handshake->tls13_master_secrets.app,
1577 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001579 MBEDTLS_SSL_DEBUG_RET(
1580 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001581 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001582 }
1583
1584 /* Derive first epoch of IV + Key for application traffic. */
1585
Xiaokang Qian123cde82023-03-29 06:54:51 +00001586 ret = mbedtls_ssl_tls13_make_traffic_keys(
1587 hash_alg,
1588 app_secrets->client_application_traffic_secret_N,
1589 app_secrets->server_application_traffic_secret_N,
1590 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (ret != 0) {
1592 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001593 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001594 }
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1597 app_secrets->client_application_traffic_secret_N,
1598 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1601 app_secrets->server_application_traffic_secret_N,
1602 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001603
XiaokangQianac0385c2021-11-03 06:40:11 +00001604 /*
1605 * Export client/server application traffic secret 0
1606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001608 ssl->f_export_keys(
1609 ssl->p_export_keys,
1610 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1611 app_secrets->client_application_traffic_secret_N, hash_len,
1612 handshake->randbytes,
1613 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1614 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1615 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001616
Xiaokang Qian123cde82023-03-29 06:54:51 +00001617 ssl->f_export_keys(
1618 ssl->p_export_keys,
1619 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1620 app_secrets->server_application_traffic_secret_N, hash_len,
1621 handshake->randbytes,
1622 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1623 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1624 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001625 }
1626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1628 traffic_keys->client_write_key, key_len);
1629 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1630 traffic_keys->server_write_key, key_len);
1631 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1632 traffic_keys->client_write_iv, iv_len);
1633 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1634 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001639 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1641 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1644 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001645}
1646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001648{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001650 mbedtls_ssl_key_set traffic_keys;
1651 mbedtls_ssl_transform *transform_handshake = NULL;
1652 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1653
1654 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001655 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if (ret != 0) {
1657 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001658 goto cleanup;
1659 }
1660
1661 /* Next evolution in key schedule: Establish handshake secret and
1662 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001663 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001665 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001667 goto cleanup;
1668 }
1669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1671 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001672 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1673 goto cleanup;
1674 }
1675
Jerry Yuef2b98a2022-05-06 16:40:05 +08001676 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 transform_handshake,
1678 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001679 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 &traffic_keys,
1681 ssl);
1682 if (ret != 0) {
1683 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001684 goto cleanup;
1685 }
1686 handshake->transform_handshake = transform_handshake;
1687
1688cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1690 if (ret != 0) {
1691 mbedtls_free(transform_handshake);
1692 }
Jerry Yue110d252022-05-05 10:19:22 +08001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001695}
1696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001698{
Jerry Yu46bffe02022-09-13 11:25:28 +08001699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001700 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001701 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1702 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001703 size_t transcript_len;
1704
Xiaokang Qian123cde82023-03-29 06:54:51 +00001705 MBEDTLS_SSL_DEBUG_MSG(
1706 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001707
Dave Rodgman2eab4622023-10-05 13:30:37 +01001708 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1711 transcript, sizeof(transcript),
1712 &transcript_len);
1713 if (ret != 0) {
1714 return ret;
1715 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001716
1717 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001718 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 handshake->tls13_master_secrets.app,
1720 transcript, transcript_len,
1721 &ssl->session_negotiate->app_secrets);
1722 if (ret != 0) {
1723 return ret;
1724 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001725
Jerry Yuff226982022-04-16 16:52:57 +08001726 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1728 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001729
Xiaokang Qian123cde82023-03-29 06:54:51 +00001730 MBEDTLS_SSL_DEBUG_BUF(
1731 4, "Resumption master secret",
1732 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001733 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001734
Xiaokang Qian123cde82023-03-29 06:54:51 +00001735 MBEDTLS_SSL_DEBUG_MSG(
1736 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001738}
1739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001741{
1742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1743 mbedtls_ssl_key_set traffic_keys;
1744 mbedtls_ssl_transform *transform_application = NULL;
1745
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001746 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (ret != 0) {
1748 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001749 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001750 goto cleanup;
1751 }
1752
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001753 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (ret != 0) {
1755 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001756 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001757 goto cleanup;
1758 }
1759
1760 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1762 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001763 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1764 goto cleanup;
1765 }
1766
1767 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 transform_application,
1769 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001770 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 &traffic_keys,
1772 ssl);
1773 if (ret != 0) {
1774 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001775 goto cleanup;
1776 }
1777
1778 ssl->transform_application = transform_application;
1779
1780cleanup:
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1783 if (ret != 0) {
1784 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001785 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001787}
1788
Ronald Cron41a443a2022-10-04 16:38:25 +02001789#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001790int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1791 unsigned char **psk,
1792 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001793{
Jerry Yu40f37712022-07-26 16:58:57 +08001794 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001795 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001796
1797 *psk_len = 0;
1798 *psk = NULL;
1799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1801 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001802 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001803
1804 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1805 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001806 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 }
1808
1809 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1810 *psk = mbedtls_calloc(1, *psk_len);
1811 if (*psk == NULL) {
1812 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1813 }
1814
1815 status = psa_export_key(ssl->handshake->psk_opaque,
1816 (uint8_t *) *psk, *psk_len, psk_len);
1817 if (status != PSA_SUCCESS) {
1818 mbedtls_free((void *) *psk);
1819 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001820 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 }
1822 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001823}
Ronald Cron41a443a2022-10-04 16:38:25 +02001824#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001825
Max Fillingerdba07e12025-04-16 14:35:24 +02001826#if defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT)
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001827int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
1828 const unsigned char *secret, const size_t secret_len,
1829 const unsigned char *label, const size_t label_len,
1830 const unsigned char *context_value, const size_t context_len,
1831 unsigned char *out, const size_t out_len)
1832{
1833 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
1834 unsigned char hkdf_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001835 int ret = 0;
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001836
1837 ret = mbedtls_ssl_tls13_derive_secret(hash_alg, secret, secret_len, label, label_len, NULL, 0,
Max Fillinger7b722202024-09-21 10:48:57 +02001838 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, hkdf_secret,
1839 hash_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001840 if (ret != 0) {
1841 goto exit;
1842 }
Max Fillinger7b722202024-09-21 10:48:57 +02001843 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
1844 hkdf_secret,
1845 hash_len,
Max Fillinger334c3672024-08-12 11:20:39 +02001846 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter),
Max Fillinger7b722202024-09-21 10:48:57 +02001847 context_value,
1848 context_len,
1849 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
1850 out,
1851 out_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001852
1853exit:
1854 mbedtls_platform_zeroize(hkdf_secret, sizeof(hkdf_secret));
1855 return ret;
1856}
Max Fillingerdba07e12025-04-16 14:35:24 +02001857#endif /* defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) */
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001858
Ronald Cron6f135e12021-12-08 16:57:54 +01001859#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */