blob: 865e02c2dc89938c37272d18815836cc33317c0b [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
Felix Conway79b51382025-06-11 16:04:06 +010083/* We need to tell the compiler that we meant to leave out the null character. */
Felix Conway5b84ae12025-06-12 11:28:56 +010084static const char tls13_label_prefix[6] MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
87 (2 /* expansion length */ \
88 + 1 /* label length */ \
89 + label_len \
90 + 1 /* context length */ \
91 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010092
93#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
94 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +010095 sizeof(tls13_label_prefix) + \
Max Fillinger58268832024-11-25 20:38:04 +010096 MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN, \
Gilles Peskine449bd832023-01-11 14:50:10 +010097 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010098
Xiaofei Bai746f9482021-11-12 08:53:56 +000099static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 size_t desired_length,
101 const unsigned char *label, size_t label_len,
102 const unsigned char *ctx, size_t ctx_len,
103 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100104{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100105 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000106 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100107 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100109
110 unsigned char *p = dst;
111
Max Fillinger28916ac2024-10-29 18:49:30 +0100112 /* Add the size of the expanded key material. */
113#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > UINT16_MAX
114#error "The desired key length must fit into an uint16 but \
115 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN is greater than UINT16_MAX"
Hanno Becker531fe302020-09-16 09:45:27 +0100116#endif
117
Max Fillinger28916ac2024-10-29 18:49:30 +0100118 *p++ = MBEDTLS_BYTE_1(desired_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100120
121 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 *p++ = MBEDTLS_BYTE_0(total_label_len);
123 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000124 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000126 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100127
128 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 *p++ = MBEDTLS_BYTE_0(ctx_len);
130 if (ctx_len != 0) {
131 memcpy(p, ctx, ctx_len);
132 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100133
134 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000135 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100136}
137
Xiaofei Bai746f9482021-11-12 08:53:56 +0000138int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 psa_algorithm_t hash_alg,
140 const unsigned char *secret, size_t secret_len,
141 const unsigned char *label, size_t label_len,
142 const unsigned char *ctx, size_t ctx_len,
143 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100144{
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200146 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200147 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
148 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200149 psa_key_derivation_operation_t operation =
150 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100151
Max Fillinger58268832024-11-25 20:38:04 +0100152 if (label_len > MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100153 /* Should never happen since this is an internal
154 * function, and we know statically which labels
155 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100157 }
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100160 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100162 }
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100165 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100167 }
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (!PSA_ALG_IS_HASH(hash_alg)) {
170 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
171 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 ssl_tls13_hkdf_encode_label(buf_len,
174 label, label_len,
175 ctx, ctx_len,
176 hkdf_label,
177 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (status != PSA_SUCCESS) {
182 goto cleanup;
183 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 status = psa_key_derivation_input_bytes(&operation,
186 PSA_KEY_DERIVATION_INPUT_SECRET,
187 secret,
188 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (status != PSA_SUCCESS) {
191 goto cleanup;
192 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 status = psa_key_derivation_input_bytes(&operation,
195 PSA_KEY_DERIVATION_INPUT_INFO,
196 hkdf_label,
197 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (status != PSA_SUCCESS) {
200 goto cleanup;
201 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 status = psa_key_derivation_output_bytes(&operation,
204 buf,
205 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (status != PSA_SUCCESS) {
208 goto cleanup;
209 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200210
211cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 abort_status = psa_key_derivation_abort(&operation);
213 status = (status == PSA_SUCCESS ? abort_status : status);
214 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500215 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100216}
217
Jerry Yua5db6c02022-11-23 18:08:04 +0800218MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800219static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 psa_algorithm_t hash_alg,
221 const unsigned char *secret, size_t secret_len,
222 unsigned char *key, size_t key_len,
223 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800224{
225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
226
Jerry Yuaec08b32022-11-29 15:19:27 +0800227 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 hash_alg,
229 secret, secret_len,
230 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
231 NULL, 0,
232 key, key_len);
233 if (ret != 0) {
234 return ret;
235 }
Jerry Yua8771832022-11-21 23:16:54 +0800236
Jerry Yuaec08b32022-11-29 15:19:27 +0800237 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 hash_alg,
239 secret, secret_len,
240 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
241 NULL, 0,
242 iv, iv_len);
243 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800244}
245
Hanno Becker3385a4d2020-08-21 13:03:34 +0100246/*
247 * The traffic keying material is generated from the following inputs:
248 *
249 * - One secret value per sender.
250 * - A purpose value indicating the specific value being generated
251 * - The desired lengths of key and IV.
252 *
253 * The expansion itself is based on HKDF:
254 *
255 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
256 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
257 *
258 * [sender] denotes the sending side and the Secret value is provided
259 * by the function caller. Note that we generate server and client side
260 * keys in a single function call.
261 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000262int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 psa_algorithm_t hash_alg,
264 const unsigned char *client_secret,
265 const unsigned char *server_secret, size_t secret_len,
266 size_t key_len, size_t iv_len,
267 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100268{
269 int ret = 0;
270
Jerry Yua8771832022-11-21 23:16:54 +0800271 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 hash_alg, client_secret, secret_len,
273 keys->client_write_key, key_len,
274 keys->client_write_iv, iv_len);
275 if (ret != 0) {
276 return ret;
277 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100278
Jerry Yua8771832022-11-21 23:16:54 +0800279 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 hash_alg, server_secret, secret_len,
281 keys->server_write_key, key_len,
282 keys->server_write_iv, iv_len);
283 if (ret != 0) {
284 return ret;
285 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100286
Hanno Becker493ea7f2020-09-08 11:01:00 +0100287 keys->key_len = key_len;
288 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100291}
292
Xiaofei Bai746f9482021-11-12 08:53:56 +0000293int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 psa_algorithm_t hash_alg,
295 const unsigned char *secret, size_t secret_len,
296 const unsigned char *label, size_t label_len,
297 const unsigned char *ctx, size_t ctx_len,
298 int ctx_hashed,
299 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100300{
301 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
303 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
307 PSA_HASH_LENGTH(hash_alg), &ctx_len);
308 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500309 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100310 return ret;
311 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 } else {
313 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100314 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100315 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100316 * Let's double-check nonetheless to not run at the risk
317 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100319 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100322 }
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
325 secret, secret_len,
326 label, label_len,
327 hashed_context, ctx_len,
328 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100329
Hanno Beckerb35d5222020-08-21 13:27:44 +0100330}
331
Xiaofei Bai746f9482021-11-12 08:53:56 +0000332int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 psa_algorithm_t hash_alg,
334 const unsigned char *secret_old,
335 const unsigned char *input, size_t input_len,
336 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100337{
338 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
340 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200341 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
343 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200344 const unsigned char *l_input = NULL;
345 size_t l_input_len;
346
Przemek Stekield5ae3652022-05-13 12:10:08 +0200347 psa_key_derivation_operation_t operation =
348 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (!PSA_ALG_IS_HASH(hash_alg)) {
351 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
352 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100355
356 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100357 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000359 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 hash_alg,
361 secret_old, hlen,
362 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
363 NULL, 0, /* context */
364 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
365 tmp_secret, hlen);
366 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100367 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100369 }
370
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200371 ret = 0;
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200374 l_input = input;
375 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200377 l_input = all_zeroes_input;
378 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100379 }
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 status = psa_key_derivation_setup(&operation,
382 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (status != PSA_SUCCESS) {
385 goto cleanup;
386 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 status = psa_key_derivation_input_bytes(&operation,
389 PSA_KEY_DERIVATION_INPUT_SALT,
390 tmp_secret,
391 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (status != PSA_SUCCESS) {
394 goto cleanup;
395 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 status = psa_key_derivation_input_bytes(&operation,
398 PSA_KEY_DERIVATION_INPUT_SECRET,
399 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (status != PSA_SUCCESS) {
402 goto cleanup;
403 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 status = psa_key_derivation_output_bytes(&operation,
406 secret_new,
407 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (status != PSA_SUCCESS) {
410 goto cleanup;
411 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413cleanup:
414 abort_status = psa_key_derivation_abort(&operation);
415 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500416 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
418 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100419}
420
Xiaofei Bai746f9482021-11-12 08:53:56 +0000421int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 psa_algorithm_t hash_alg,
423 unsigned char const *early_secret,
424 unsigned char const *transcript, size_t transcript_len,
425 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100426{
427 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100429
430 /* We should never call this function with an unknown hash,
431 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (!PSA_ALG_IS_HASH(hash_alg)) {
433 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
434 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100435
436 /*
437 * 0
438 * |
439 * v
440 * PSK -> HKDF-Extract = Early Secret
441 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100442 * +-----> Derive-Secret(., "c e traffic", ClientHello)
443 * | = client_early_traffic_secret
444 * |
445 * +-----> Derive-Secret(., "e exp master", ClientHello)
446 * | = early_exporter_master_secret
447 * v
448 */
449
450 /* Create client_early_traffic_secret */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000451 ret = mbedtls_ssl_tls13_derive_secret(
452 hash_alg,
453 early_secret, hash_len,
454 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
455 transcript, transcript_len,
456 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
457 derived->client_early_traffic_secret,
458 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (ret != 0) {
460 return ret;
461 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100462
463 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000464 ret = mbedtls_ssl_tls13_derive_secret(
465 hash_alg,
466 early_secret, hash_len,
467 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
468 transcript, transcript_len,
469 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
470 derived->early_exporter_master_secret,
471 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (ret != 0) {
473 return ret;
474 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100477}
478
Xiaofei Bai746f9482021-11-12 08:53:56 +0000479int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 psa_algorithm_t hash_alg,
481 unsigned char const *handshake_secret,
482 unsigned char const *transcript, size_t transcript_len,
483 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100484{
485 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100487
488 /* We should never call this function with an unknown hash,
489 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (!PSA_ALG_IS_HASH(hash_alg)) {
491 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
492 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100493
494 /*
495 *
496 * Handshake Secret
497 * |
498 * +-----> Derive-Secret( ., "c hs traffic",
499 * | ClientHello...ServerHello )
500 * | = client_handshake_traffic_secret
501 * |
502 * +-----> Derive-Secret( ., "s hs traffic",
503 * | ClientHello...ServerHello )
504 * | = server_handshake_traffic_secret
505 *
506 */
507
508 /*
509 * Compute client_handshake_traffic_secret with
510 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
511 */
512
Xiaokang Qian123cde82023-03-29 06:54:51 +0000513 ret = mbedtls_ssl_tls13_derive_secret(
514 hash_alg,
515 handshake_secret, hash_len,
516 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
517 transcript, transcript_len,
518 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
519 derived->client_handshake_traffic_secret,
520 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (ret != 0) {
522 return ret;
523 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100524
525 /*
526 * Compute server_handshake_traffic_secret with
527 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
528 */
529
Xiaokang Qian123cde82023-03-29 06:54:51 +0000530 ret = mbedtls_ssl_tls13_derive_secret(
531 hash_alg,
532 handshake_secret, hash_len,
533 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
534 transcript, transcript_len,
535 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
536 derived->server_handshake_traffic_secret,
537 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (ret != 0) {
539 return ret;
540 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100543}
544
Xiaofei Bai746f9482021-11-12 08:53:56 +0000545int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 psa_algorithm_t hash_alg,
547 unsigned char const *application_secret,
548 unsigned char const *transcript, size_t transcript_len,
549 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100550{
551 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100553
554 /* We should never call this function with an unknown hash,
555 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (!PSA_ALG_IS_HASH(hash_alg)) {
557 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
558 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100559
560 /* Generate {client,server}_application_traffic_secret_0
561 *
562 * Master Secret
563 * |
564 * +-----> Derive-Secret( ., "c ap traffic",
565 * | ClientHello...server Finished )
566 * | = client_application_traffic_secret_0
567 * |
568 * +-----> Derive-Secret( ., "s ap traffic",
569 * | ClientHello...Server Finished )
570 * | = server_application_traffic_secret_0
571 * |
572 * +-----> Derive-Secret( ., "exp master",
573 * | ClientHello...server Finished)
574 * | = exporter_master_secret
575 *
576 */
577
Xiaokang Qian123cde82023-03-29 06:54:51 +0000578 ret = mbedtls_ssl_tls13_derive_secret(
579 hash_alg,
580 application_secret, hash_len,
581 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
582 transcript, transcript_len,
583 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
584 derived->client_application_traffic_secret_N,
585 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ret != 0) {
587 return ret;
588 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100589
Xiaokang Qian123cde82023-03-29 06:54:51 +0000590 ret = mbedtls_ssl_tls13_derive_secret(
591 hash_alg,
592 application_secret, hash_len,
593 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
594 transcript, transcript_len,
595 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
596 derived->server_application_traffic_secret_N,
597 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if (ret != 0) {
599 return ret;
600 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100601
Xiaokang Qian123cde82023-03-29 06:54:51 +0000602 ret = mbedtls_ssl_tls13_derive_secret(
603 hash_alg,
604 application_secret, hash_len,
605 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
606 transcript, transcript_len,
607 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
608 derived->exporter_master_secret,
609 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (ret != 0) {
611 return ret;
612 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100615}
616
617/* Generate resumption_master_secret for use with the ticket exchange.
618 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000619 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100620 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000621int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 psa_algorithm_t hash_alg,
623 unsigned char const *application_secret,
624 unsigned char const *transcript, size_t transcript_len,
625 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100626{
627 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100629
630 /* We should never call this function with an unknown hash,
631 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (!PSA_ALG_IS_HASH(hash_alg)) {
633 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
634 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100635
Xiaokang Qian123cde82023-03-29 06:54:51 +0000636 ret = mbedtls_ssl_tls13_derive_secret(
637 hash_alg,
638 application_secret, hash_len,
639 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
640 transcript, transcript_len,
641 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
642 derived->resumption_master_secret,
643 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (ret != 0) {
646 return ret;
647 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100650}
651
Yanray Wang05402112022-12-13 18:50:42 +0800652/**
653 * \brief Transition into application stage of TLS 1.3 key schedule.
654 *
655 * The TLS 1.3 key schedule can be viewed as a simple state machine
656 * with states Initial -> Early -> Handshake -> Application, and
657 * this function represents the Handshake -> Application transition.
658 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800659 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800660 * can be used to derive the handshake traffic keys.
661 *
662 * \param ssl The SSL context to operate on. This must be in key schedule
663 * stage \c Handshake.
664 *
665 * \returns \c 0 on success.
666 * \returns A negative error code on failure.
667 */
668MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800669static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000670{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000672 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200673 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100674 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000675
676 /*
677 * Compute MasterSecret
678 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000679 ret = mbedtls_ssl_tls13_evolve_secret(
680 hash_alg,
681 handshake->tls13_master_secrets.handshake,
682 NULL, 0,
683 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (ret != 0) {
685 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
686 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000687 }
688
Xiaokang Qian123cde82023-03-29 06:54:51 +0000689 MBEDTLS_SSL_DEBUG_BUF(
690 4, "Master secret",
691 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000694}
695
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200696MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100697static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
698 unsigned char const *base_key,
699 unsigned char const *transcript,
700 unsigned char *dst,
701 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100702{
Gabor Mezei07732f72022-03-26 17:04:19 +0100703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
705 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100707 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100708 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100709 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100710
711 /* We should never call this function with an unknown hash,
712 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (!PSA_ALG_IS_HASH(hash_alg)) {
714 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
715 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100716
717 /* TLS 1.3 Finished message
718 *
719 * struct {
720 * opaque verify_data[Hash.length];
721 * } Finished;
722 *
723 * verify_data =
724 * HMAC( finished_key,
725 * Hash( Handshake Context +
726 * Certificate* +
727 * CertificateVerify* )
728 * )
729 *
730 * finished_key =
731 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
732 */
733
Xiaofei Bai746f9482021-11-12 08:53:56 +0000734 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 hash_alg, base_key, hash_len,
736 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
737 NULL, 0,
738 finished_key, hash_len);
739 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100740 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100741 }
742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 alg = PSA_ALG_HMAC(hash_alg);
744 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
745 psa_set_key_algorithm(&attributes, alg);
746 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
747
748 status = psa_import_key(&attributes, finished_key, hash_len, &key);
749 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500750 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 goto exit;
752 }
753
754 status = psa_mac_compute(key, alg, transcript, hash_len,
755 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500756 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100757
758exit:
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 status = psa_destroy_key(key);
761 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500762 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100768}
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
771 unsigned char *dst,
772 size_t dst_len,
773 size_t *actual_len,
774 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000775{
XiaokangQiana7634982021-10-22 06:32:32 +0000776 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000777
XiaokangQianaaa0e192021-11-10 03:07:04 +0000778 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000779 size_t transcript_len;
780
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800781 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800782 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800783 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800785
Dave Rodgman2eab4622023-10-05 13:30:37 +0100786 mbedtls_md_type_t const md_type = (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100787
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200788 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100789 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800795 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
797 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800798 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800800 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800803 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
804 goto exit;
805 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
808 transcript, sizeof(transcript),
809 &transcript_len);
810 if (ret != 0) {
811 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000812 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000813 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000815
Xiaokang Qian123cde82023-03-29 06:54:51 +0000816 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
817 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000819 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
823 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000824
825exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800826 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 mbedtls_platform_zeroize(base_key, base_key_len);
828 mbedtls_platform_zeroize(transcript, sizeof(transcript));
829 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000830}
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
833 const psa_algorithm_t hash_alg,
834 unsigned char const *psk, size_t psk_len,
835 int psk_type,
836 unsigned char const *transcript,
837 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100838{
839 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100840 unsigned char binder_key[PSA_MAC_MAX_SIZE];
841 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100843 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100844
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100845#if !defined(MBEDTLS_DEBUG_C)
846 ssl = NULL; /* make sure we don't use it except for debug */
847 ((void) ssl);
848#endif
849
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100850 /* We should never call this function with an unknown hash,
851 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (!PSA_ALG_IS_HASH(hash_alg)) {
853 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
854 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100855
856 /*
857 * 0
858 * |
859 * v
860 * PSK -> HKDF-Extract = Early Secret
861 * |
862 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
863 * | = binder_key
864 * v
865 */
866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
868 NULL, /* Old secret */
869 psk, psk_len, /* Input */
870 early_secret);
871 if (ret != 0) {
872 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100873 goto exit;
874 }
875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
877 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000880 ret = mbedtls_ssl_tls13_derive_secret(
881 hash_alg,
882 early_secret, hash_len,
883 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
884 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
885 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
887 } else {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000888 ret = mbedtls_ssl_tls13_derive_secret(
889 hash_alg,
890 early_secret, hash_len,
891 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
892 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
893 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100895 }
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (ret != 0) {
898 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100899 goto exit;
900 }
901
902 /*
903 * The binding_value is computed in the same way as the Finished message
904 * but with the BaseKey being the binder_key.
905 */
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
908 result, &actual_len);
909 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100910 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100914
915exit:
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
918 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
919 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100920}
921
Xiaokang Qian123cde82023-03-29 06:54:51 +0000922int mbedtls_ssl_tls13_populate_transform(
923 mbedtls_ssl_transform *transform,
924 int endpoint, int ciphersuite,
925 mbedtls_ssl_key_set const *traffic_keys,
926 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000927{
Hanno Beckerc94060c2021-03-22 07:50:44 +0000928 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
929 unsigned char const *key_enc;
930 unsigned char const *iv_enc;
931 unsigned char const *key_dec;
932 unsigned char const *iv_dec;
933
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100934 psa_key_type_t key_type;
935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
936 psa_algorithm_t alg;
937 size_t key_bits;
938 psa_status_t status = PSA_SUCCESS;
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100939
Hanno Beckerc94060c2021-03-22 07:50:44 +0000940#if !defined(MBEDTLS_DEBUG_C)
941 ssl = NULL; /* make sure we don't use it except for those cases */
942 (void) ssl;
943#endif
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
946 if (ciphersuite_info == NULL) {
947 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
948 ciphersuite));
949 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100950 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000951
Hanno Beckerc94060c2021-03-22 07:50:44 +0000952
953#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000955 key_enc = traffic_keys->server_write_key;
956 key_dec = traffic_keys->client_write_key;
957 iv_enc = traffic_keys->server_write_iv;
958 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000960#endif /* MBEDTLS_SSL_SRV_C */
961#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000963 key_enc = traffic_keys->client_write_key;
964 key_dec = traffic_keys->server_write_key;
965 iv_enc = traffic_keys->client_write_iv;
966 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000968#endif /* MBEDTLS_SSL_CLI_C */
969 {
970 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000972 }
973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
975 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000976
Hanno Beckerc94060c2021-03-22 07:50:44 +0000977
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100978 /*
979 * Setup other fields in SSL transform
980 */
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100983 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100985 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100987
988 transform->ivlen = traffic_keys->iv_len;
989 transform->maclen = 0;
990 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -0400991 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100992
993 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800994 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100995 * type-extended and padded plaintext is therefore the padding
996 * granularity. */
997 transform->minlen =
998 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
999
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001000 /*
1001 * Setup psa keys and alg
1002 */
Dave Rodgman2eab4622023-10-05 13:30:37 +01001003 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 transform->taglen,
1005 &alg,
1006 &key_type,
1007 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001008 MBEDTLS_SSL_DEBUG_RET(
1009 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001010 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001011 }
1012
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001013 transform->psa_alg = alg;
1014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1016 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1017 psa_set_key_algorithm(&attributes, alg);
1018 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if ((status = psa_import_key(&attributes,
1021 key_enc,
1022 PSA_BITS_TO_BYTES(key_bits),
1023 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001024 MBEDTLS_SSL_DEBUG_RET(
1025 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001026 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001027 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if ((status = psa_import_key(&attributes,
1032 key_dec,
1033 PSA_BITS_TO_BYTES(key_bits),
1034 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001035 MBEDTLS_SSL_DEBUG_RET(
1036 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001037 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001038 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001039 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001042}
1043
Jerry Yu84a6eda2022-11-04 11:17:35 +08001044MBEDTLS_CHECK_RETURN_CRITICAL
1045static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1047 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001048{
1049 psa_key_type_t key_type;
1050 psa_algorithm_t alg;
1051 size_t taglen;
1052 size_t key_bits;
1053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001056 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001058 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001060
Dave Rodgman2eab4622023-10-05 13:30:37 +01001061 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, taglen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 &alg, &key_type, &key_bits);
1063 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001064 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001068
1069 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1070 *iv_len = 12;
1071
1072 return 0;
1073}
1074
Jerry Yu91b560f2022-11-04 14:10:34 +08001075#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001076/*
1077 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001078 * the early application data and handshake messages as described in section 7
1079 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001080 *
Jerry Yue31688b2022-11-22 21:55:56 +08001081 * NOTE: Only one key is generated, the key for the traffic from the client to
1082 * the server. The TLS 1.3 specification does not define a secret and thus
1083 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001084 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001085MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001086static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1087 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001088{
1089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001090 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001091 psa_algorithm_t hash_alg;
1092 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001093 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1094 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001095 size_t key_len = 0;
1096 size_t iv_len = 0;
Yanray Wang16c895d2022-12-15 15:14:35 +08001097 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001098
1099 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001100 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1101 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1106 if (ret != 0) {
1107 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001108 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001109 }
1110
Dave Rodgman2eab4622023-10-05 13:30:37 +01001111 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Jerry Yu91b560f2022-11-04 14:10:34 +08001112
Dave Rodgman2eab4622023-10-05 13:30:37 +01001113 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1117 transcript,
1118 sizeof(transcript),
1119 &transcript_len);
1120 if (ret != 0) {
1121 MBEDTLS_SSL_DEBUG_RET(1,
1122 "mbedtls_ssl_get_handshake_transcript",
1123 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001124 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001125 }
1126
Jerry Yub094e122022-11-21 13:03:47 +08001127 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001129 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001131 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001133 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001134 }
1135
1136 MBEDTLS_SSL_DEBUG_BUF(
1137 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001138 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001139
1140 /*
1141 * Export client handshake traffic secret
1142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001144 ssl->f_export_keys(
1145 ssl->p_export_keys,
1146 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001147 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001148 hash_len,
1149 handshake->randbytes,
1150 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001152 }
1153
Jerry Yua8771832022-11-21 23:16:54 +08001154 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001156 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 hash_len, traffic_keys->client_write_key, key_len,
1158 traffic_keys->client_write_iv, iv_len);
1159 if (ret != 0) {
1160 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001161 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001162 }
Jerry Yua8771832022-11-21 23:16:54 +08001163 traffic_keys->key_len = key_len;
1164 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1167 traffic_keys->client_write_key,
1168 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1171 traffic_keys->client_write_iv,
1172 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001175
Jerry Yu3d78e082022-11-23 18:26:20 +08001176cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001177 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001178 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001179 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1181 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001182}
1183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001185{
1186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1187 mbedtls_ssl_key_set traffic_keys;
1188 mbedtls_ssl_transform *transform_earlydata = NULL;
1189 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1190
1191 /* Next evolution in key schedule: Establish early_data secret and
1192 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1194 if (ret != 0) {
1195 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1196 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001197 goto cleanup;
1198 }
1199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1201 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001202 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1203 goto cleanup;
1204 }
1205
1206 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 transform_earlydata,
1208 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001209 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 &traffic_keys,
1211 ssl);
1212 if (ret != 0) {
1213 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001214 goto cleanup;
1215 }
1216 handshake->transform_earlydata = transform_earlydata;
1217
1218cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1220 if (ret != 0) {
1221 mbedtls_free(transform_earlydata);
1222 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001225}
1226#endif /* MBEDTLS_SSL_EARLY_DATA */
1227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001229{
Jerry Yue3131ef2021-09-16 13:14:15 +08001230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001231 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001232 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001233 unsigned char *psk = NULL;
1234 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (handshake->ciphersuite_info == NULL) {
1237 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1238 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001239 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001240
Dave Rodgman2eab4622023-10-05 13:30:37 +01001241 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001242#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1244 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1245 if (ret != 0) {
1246 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1247 ret);
1248 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001249 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001250 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001251#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1254 handshake->tls13_master_secrets.early);
Manuel Pégourié-Gonnard0b44a812025-01-23 09:58:07 +01001255#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001257#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (ret != 0) {
1259 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1260 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001261 }
1262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1264 handshake->tls13_master_secrets.early,
1265 PSA_HASH_LENGTH(hash_alg));
1266 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001267}
1268
Yanray Wang05402112022-12-13 18:50:42 +08001269/**
1270 * \brief Compute TLS 1.3 handshake traffic keys.
1271 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001272 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1273 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001274 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001275 *
1276 * \param ssl The SSL context to operate on. This must be in
1277 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001278 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001279 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001280 * keys. This must be writable but may be uninitialized.
1281 *
1282 * \returns \c 0 on success.
1283 * \returns A negative error code on failure.
1284 */
1285MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001286static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1287 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001288{
1289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001290 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001291 psa_algorithm_t hash_alg;
1292 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001293 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001294 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001295 size_t key_len = 0;
1296 size_t iv_len = 0;
Jerry Yu61e35e02021-09-16 18:59:08 +08001297
Jerry Yu435208a2021-10-13 11:22:16 +08001298 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001299 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1300 handshake->ciphersuite_info;
1301 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1302 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001303
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001304 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1307 if (ret != 0) {
1308 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001309 return ret;
1310 }
1311
Dave Rodgman2eab4622023-10-05 13:30:37 +01001312 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001313
Dave Rodgman2eab4622023-10-05 13:30:37 +01001314 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1318 transcript,
1319 sizeof(transcript),
1320 &transcript_len);
1321 if (ret != 0) {
1322 MBEDTLS_SSL_DEBUG_RET(1,
1323 "mbedtls_ssl_get_handshake_transcript",
1324 ret);
1325 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001326 }
1327
Xiaokang Qian123cde82023-03-29 06:54:51 +00001328 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1329 hash_alg, handshake->tls13_master_secrets.handshake,
1330 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (ret != 0) {
1332 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1333 ret);
1334 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001335 }
1336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1338 tls13_hs_secrets->client_handshake_traffic_secret,
1339 hash_len);
1340 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1341 tls13_hs_secrets->server_handshake_traffic_secret,
1342 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001343
1344 /*
1345 * Export client handshake traffic secret
1346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001348 ssl->f_export_keys(
1349 ssl->p_export_keys,
1350 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1351 tls13_hs_secrets->client_handshake_traffic_secret,
1352 hash_len,
1353 handshake->randbytes,
1354 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1355 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001356
Xiaokang Qian123cde82023-03-29 06:54:51 +00001357 ssl->f_export_keys(
1358 ssl->p_export_keys,
1359 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1360 tls13_hs_secrets->server_handshake_traffic_secret,
1361 hash_len,
1362 handshake->randbytes,
1363 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1364 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001365 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001366
Xiaokang Qian123cde82023-03-29 06:54:51 +00001367 ret = mbedtls_ssl_tls13_make_traffic_keys(
1368 hash_alg,
1369 tls13_hs_secrets->client_handshake_traffic_secret,
1370 tls13_hs_secrets->server_handshake_traffic_secret,
1371 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (ret != 0) {
1373 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001374 goto exit;
1375 }
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1378 traffic_keys->client_write_key,
1379 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1382 traffic_keys->server_write_key,
1383 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1386 traffic_keys->client_write_iv,
1387 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1390 traffic_keys->server_write_iv,
1391 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001392
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001393 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001394
1395exit:
1396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001398}
1399
Yanray Wang05402112022-12-13 18:50:42 +08001400/**
1401 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1402 *
1403 * The TLS 1.3 key schedule can be viewed as a simple state machine
1404 * with states Initial -> Early -> Handshake -> Application, and
1405 * this function represents the Early -> Handshake transition.
1406 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001407 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001408 * can be used to derive the handshake traffic keys.
1409 *
1410 * \param ssl The SSL context to operate on. This must be in key schedule
1411 * stage \c Early.
1412 *
1413 * \returns \c 0 on success.
1414 * \returns A negative error code on failure.
1415 */
1416MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001417static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001418{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001420 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001421 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001422 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001423 unsigned char *shared_secret = NULL;
1424 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001425
Ronald Crona2900bc2022-10-20 14:37:35 +02001426#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001427 /*
1428 * Compute ECDHE secret used to compute the handshake secret from which
1429 * client_handshake_traffic_secret and server_handshake_traffic_secret
1430 * are derived in the handshake secret derivation stage.
1431 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001433 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001434 mbedtls_ssl_tls13_named_group_is_ffdh(handshake->offered_group_id)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001435#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001436 psa_algorithm_t alg =
1437 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1438 PSA_ALG_ECDH : PSA_ALG_FFDH;
1439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001441 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001442 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1443
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001444 status = psa_get_key_attributes(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 &key_attributes);
1446 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001447 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 }
Ronald Cron3b056202022-10-05 17:20:21 +02001449
1450 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 psa_get_key_bits(&key_attributes));
1452 shared_secret = mbedtls_calloc(1, shared_secret_len);
1453 if (shared_secret == NULL) {
1454 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1455 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001456
Ronald Cron4c7edb22022-10-05 15:37:11 +02001457 status = psa_raw_key_agreement(
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001458 alg, handshake->xxdh_psa_privkey,
1459 handshake->xxdh_psa_peerkey, handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 shared_secret, shared_secret_len, &shared_secret_len);
1461 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001462 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001464 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001465 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001466
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001467 status = psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001469 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001471 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001472 }
1473
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001474 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001475#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 } else {
1477 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1478 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001479 }
1480 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001481#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001482
1483 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001484 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001485 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001486 ret = mbedtls_ssl_tls13_evolve_secret(
1487 hash_alg, handshake->tls13_master_secrets.early,
1488 shared_secret, shared_secret_len,
1489 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (ret != 0) {
1491 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001492 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001493 }
1494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1496 handshake->tls13_master_secrets.handshake,
1497 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001498
Ronald Cron3b056202022-10-05 17:20:21 +02001499cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (shared_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001501 mbedtls_zeroize_and_free(shared_secret, shared_secret_len);
Ronald Cron3b056202022-10-05 17:20:21 +02001502 }
1503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001505}
1506
Yanray Wang05402112022-12-13 18:50:42 +08001507/**
1508 * \brief Compute TLS 1.3 application traffic keys.
1509 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001510 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001511 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001512 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001513 *
1514 * \param ssl The SSL context to operate on. This must be in
1515 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001516 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001517 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001518 * keys. This must be writable but may be uninitialized.
1519 *
1520 * \returns \c 0 on success.
1521 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001522 */
Yanray Wang05402112022-12-13 18:50:42 +08001523MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001524static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 mbedtls_ssl_context *ssl,
1526 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001527{
XiaokangQiana7634982021-10-22 06:32:32 +00001528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001529 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001530
1531 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001532 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001533 &ssl->session_negotiate->app_secrets;
1534
1535 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001536 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001537 size_t transcript_len;
1538
1539 /* Variables relating to the hash for the chosen ciphersuite. */
1540 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001541
1542 psa_algorithm_t hash_alg;
1543 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001544
1545 /* Variables relating to the cipher for the chosen ciphersuite. */
Paul Elliott88600212023-11-23 14:24:30 +00001546 size_t key_len = 0, iv_len = 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001549
1550 /* Extract basic information about hash and ciphersuite */
1551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1553 &key_len, &iv_len);
1554 if (ret != 0) {
1555 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001556 goto cleanup;
1557 }
1558
Dave Rodgman2eab4622023-10-05 13:30:37 +01001559 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001560
Dave Rodgman2eab4622023-10-05 13:30:37 +01001561 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001563
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001564 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001565 * to call this at the right time, that is, after the ServerFinished. */
1566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1568 transcript, sizeof(transcript),
1569 &transcript_len);
1570 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001571 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001573
1574 /* Compute application secrets from master secret and transcript hash. */
1575
Xiaokang Qian123cde82023-03-29 06:54:51 +00001576 ret = mbedtls_ssl_tls13_derive_application_secrets(
1577 hash_alg, handshake->tls13_master_secrets.app,
1578 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001580 MBEDTLS_SSL_DEBUG_RET(
1581 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001582 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001583 }
1584
1585 /* Derive first epoch of IV + Key for application traffic. */
1586
Xiaokang Qian123cde82023-03-29 06:54:51 +00001587 ret = mbedtls_ssl_tls13_make_traffic_keys(
1588 hash_alg,
1589 app_secrets->client_application_traffic_secret_N,
1590 app_secrets->server_application_traffic_secret_N,
1591 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (ret != 0) {
1593 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001594 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001595 }
1596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1598 app_secrets->client_application_traffic_secret_N,
1599 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1602 app_secrets->server_application_traffic_secret_N,
1603 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001604
XiaokangQianac0385c2021-11-03 06:40:11 +00001605 /*
1606 * Export client/server application traffic secret 0
1607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001609 ssl->f_export_keys(
1610 ssl->p_export_keys,
1611 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1612 app_secrets->client_application_traffic_secret_N, hash_len,
1613 handshake->randbytes,
1614 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1615 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1616 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001617
Xiaokang Qian123cde82023-03-29 06:54:51 +00001618 ssl->f_export_keys(
1619 ssl->p_export_keys,
1620 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1621 app_secrets->server_application_traffic_secret_N, hash_len,
1622 handshake->randbytes,
1623 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1624 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1625 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001626 }
1627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1629 traffic_keys->client_write_key, key_len);
1630 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1631 traffic_keys->server_write_key, key_len);
1632 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1633 traffic_keys->client_write_iv, iv_len);
1634 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1635 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001640 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1642 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1645 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001646}
1647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001649{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001651 mbedtls_ssl_key_set traffic_keys;
1652 mbedtls_ssl_transform *transform_handshake = NULL;
1653 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1654
1655 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001656 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 if (ret != 0) {
1658 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001659 goto cleanup;
1660 }
1661
1662 /* Next evolution in key schedule: Establish handshake secret and
1663 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001664 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001666 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001668 goto cleanup;
1669 }
1670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1672 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001673 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1674 goto cleanup;
1675 }
1676
Jerry Yuef2b98a2022-05-06 16:40:05 +08001677 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 transform_handshake,
1679 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001680 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 &traffic_keys,
1682 ssl);
1683 if (ret != 0) {
1684 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001685 goto cleanup;
1686 }
1687 handshake->transform_handshake = transform_handshake;
1688
1689cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1691 if (ret != 0) {
1692 mbedtls_free(transform_handshake);
1693 }
Jerry Yue110d252022-05-05 10:19:22 +08001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001696}
1697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001699{
Jerry Yu46bffe02022-09-13 11:25:28 +08001700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001701 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001702 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1703 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001704 size_t transcript_len;
1705
Xiaokang Qian123cde82023-03-29 06:54:51 +00001706 MBEDTLS_SSL_DEBUG_MSG(
1707 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001708
Dave Rodgman2eab4622023-10-05 13:30:37 +01001709 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1712 transcript, sizeof(transcript),
1713 &transcript_len);
1714 if (ret != 0) {
1715 return ret;
1716 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001717
1718 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001719 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 handshake->tls13_master_secrets.app,
1721 transcript, transcript_len,
1722 &ssl->session_negotiate->app_secrets);
1723 if (ret != 0) {
1724 return ret;
1725 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001726
Jerry Yuff226982022-04-16 16:52:57 +08001727 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1729 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001730
Xiaokang Qian123cde82023-03-29 06:54:51 +00001731 MBEDTLS_SSL_DEBUG_BUF(
1732 4, "Resumption master secret",
1733 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001734 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001735
Xiaokang Qian123cde82023-03-29 06:54:51 +00001736 MBEDTLS_SSL_DEBUG_MSG(
1737 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001739}
1740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001742{
1743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1744 mbedtls_ssl_key_set traffic_keys;
1745 mbedtls_ssl_transform *transform_application = NULL;
1746
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001747 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (ret != 0) {
1749 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001750 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001751 goto cleanup;
1752 }
1753
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001754 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (ret != 0) {
1756 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001757 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001758 goto cleanup;
1759 }
1760
1761 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1763 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001764 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1765 goto cleanup;
1766 }
1767
1768 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 transform_application,
1770 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001771 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 &traffic_keys,
1773 ssl);
1774 if (ret != 0) {
1775 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001776 goto cleanup;
1777 }
1778
1779 ssl->transform_application = transform_application;
1780
1781cleanup:
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1784 if (ret != 0) {
1785 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001786 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001788}
1789
Ronald Cron41a443a2022-10-04 16:38:25 +02001790#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001791int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1792 unsigned char **psk,
1793 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001794{
Jerry Yu40f37712022-07-26 16:58:57 +08001795 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001796 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001797
1798 *psk_len = 0;
1799 *psk = NULL;
1800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1802 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001803 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001804
1805 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1806 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001807 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 }
1809
1810 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1811 *psk = mbedtls_calloc(1, *psk_len);
1812 if (*psk == NULL) {
1813 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1814 }
1815
1816 status = psa_export_key(ssl->handshake->psk_opaque,
1817 (uint8_t *) *psk, *psk_len, psk_len);
1818 if (status != PSA_SUCCESS) {
1819 mbedtls_free((void *) *psk);
1820 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001821 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 }
1823 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001824}
Ronald Cron41a443a2022-10-04 16:38:25 +02001825#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001826
Max Fillingerdba07e12025-04-16 14:35:24 +02001827#if defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT)
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001828int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
1829 const unsigned char *secret, const size_t secret_len,
1830 const unsigned char *label, const size_t label_len,
1831 const unsigned char *context_value, const size_t context_len,
1832 unsigned char *out, const size_t out_len)
1833{
1834 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
1835 unsigned char hkdf_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001836 int ret = 0;
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001837
1838 ret = mbedtls_ssl_tls13_derive_secret(hash_alg, secret, secret_len, label, label_len, NULL, 0,
Max Fillinger7b722202024-09-21 10:48:57 +02001839 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, hkdf_secret,
1840 hash_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001841 if (ret != 0) {
1842 goto exit;
1843 }
Max Fillinger7b722202024-09-21 10:48:57 +02001844 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
1845 hkdf_secret,
1846 hash_len,
Max Fillinger334c3672024-08-12 11:20:39 +02001847 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter),
Max Fillinger7b722202024-09-21 10:48:57 +02001848 context_value,
1849 context_len,
1850 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
1851 out,
1852 out_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001853
1854exit:
1855 mbedtls_platform_zeroize(hkdf_secret, sizeof(hkdf_secret));
1856 return ret;
1857}
Max Fillingerdba07e12025-04-16 14:35:24 +02001858#endif /* defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) */
Max Fillingerbd81c9d2024-07-22 14:43:56 +02001859
Ronald Cron6f135e12021-12-08 16:57:54 +01001860#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */