blob: 52d19f0baf00ed8260aa23d2f27ce1ad1a1cb3ed [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Hanno Beckerbe9d6642020-08-21 13:20:06 +01006 */
7
Hanno Becker58c5cea2020-09-08 10:31:33 +01008#include "common.h"
Hanno Beckerbe9d6642020-08-21 13:20:06 +01009
Ronald Cron6f135e12021-12-08 16:57:54 +010010#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010011
Hanno Beckerbe9d6642020-08-21 13:20:06 +010012#include <stdint.h>
13#include <string.h>
14
Jerry Yue3131ef2021-09-16 13:14:15 +080015#include "mbedtls/hkdf.h"
Valerio Settib4f50762024-01-17 10:24:52 +010016#include "debug_internal.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080017#include "mbedtls/error.h"
Jerry Yue110d252022-05-05 10:19:22 +080018#include "mbedtls/platform.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080019
20#include "ssl_misc.h"
21#include "ssl_tls13_keys.h"
Gabor Mezeia3eecd22022-02-09 16:57:26 +010022#include "ssl_tls13_invasive.h"
23
24#include "psa/crypto.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010025#include "mbedtls/psa_util.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080026
Andrzej Kurek00644842023-05-30 05:45:00 -040027/* Define a local translating function to save code size by not using too many
28 * arguments in each translating place. */
29static int local_err_translation(psa_status_t status)
30{
31 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040032 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040033 psa_generic_status_to_mbedtls);
34}
35#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050036
Gilles Peskine449bd832023-01-11 14:50:10 +010037#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010038 .name = string,
39
Xiaofei Bai746f9482021-11-12 08:53:56 +000040struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010041{
42 /* This seems to work in C, despite the string literal being one
43 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010044 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010045};
46
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010047#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010048
Hanno Beckerbe9d6642020-08-21 13:20:06 +010049/*
50 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
51 *
52 * The HkdfLabel is specified in RFC 8446 as follows:
53 *
54 * struct HkdfLabel {
55 * uint16 length; // Length of expanded key material
56 * opaque label<7..255>; // Always prefixed by "tls13 "
57 * opaque context<0..255>; // Usually a communication transcript hash
58 * };
59 *
60 * Parameters:
61 * - desired_length: Length of expanded key material
62 * Even though the standard allows expansion to up to
63 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
64 * 255 Bytes, so we require `desired_length` to be at most
65 * 255. This allows us to save a few Bytes of code by
66 * hardcoding the writing of the high bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000067 * - (label, label_len): label + label length, without "tls13 " prefix
68 * The label length MUST be less than or equal to
Max Fillinger529931a2024-11-25 20:38:04 +010069 * MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000070 * It is the caller's responsibility to ensure this.
71 * All (label, label length) pairs used in TLS 1.3
72 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
73 * - (ctx, ctx_len): context + context length
74 * The context length MUST be less than or equal to
75 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
76 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010077 * - dst: Target buffer for HkdfLabel structure,
78 * This MUST be a writable buffer of size
79 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000080 * - dst_len: Pointer at which to store the actual length of
81 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010082 */
83
Xiaofei Baid25fab62021-12-02 06:36:27 +000084static const char tls13_label_prefix[6] = "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 Fillinger529931a2024-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 Fillingerffc47e62024-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 Fillingerffc47e62024-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 Fillinger529931a2024-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{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100928#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000929 int ret;
930 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200931#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000932 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
933 unsigned char const *key_enc;
934 unsigned char const *iv_enc;
935 unsigned char const *key_dec;
936 unsigned char const *iv_dec;
937
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100938#if defined(MBEDTLS_USE_PSA_CRYPTO)
939 psa_key_type_t key_type;
940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
941 psa_algorithm_t alg;
942 size_t key_bits;
943 psa_status_t status = PSA_SUCCESS;
944#endif
945
Hanno Beckerc94060c2021-03-22 07:50:44 +0000946#if !defined(MBEDTLS_DEBUG_C)
947 ssl = NULL; /* make sure we don't use it except for those cases */
948 (void) ssl;
949#endif
950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
952 if (ciphersuite_info == NULL) {
953 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
954 ciphersuite));
955 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100956 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000957
Neil Armstronga8093f52022-05-04 17:44:05 +0200958#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
960 if (cipher_info == NULL) {
961 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
962 ciphersuite_info->cipher));
963 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000964 }
965
966 /*
967 * Setup cipher contexts in target transform
968 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
970 cipher_info)) != 0) {
971 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
972 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000973 }
974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
976 cipher_info)) != 0) {
977 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
978 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000979 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100980#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000981
982#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000984 key_enc = traffic_keys->server_write_key;
985 key_dec = traffic_keys->client_write_key;
986 iv_enc = traffic_keys->server_write_iv;
987 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000989#endif /* MBEDTLS_SSL_SRV_C */
990#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000992 key_enc = traffic_keys->client_write_key;
993 key_dec = traffic_keys->server_write_key;
994 iv_enc = traffic_keys->client_write_iv;
995 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000997#endif /* MBEDTLS_SSL_CLI_C */
998 {
999 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001001 }
1002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
1004 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +00001005
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001006#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
Mehmet Cagri Aksoy56e90112023-10-11 15:28:06 +02001008 key_enc, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 MBEDTLS_ENCRYPT)) != 0) {
1010 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1011 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001012 }
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
Mehmet Cagri Aksoy56e90112023-10-11 15:28:06 +02001015 key_dec, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 MBEDTLS_DECRYPT)) != 0) {
1017 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1018 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001019 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001020#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001021
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001022 /*
1023 * Setup other fields in SSL transform
1024 */
1025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001027 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001029 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001031
1032 transform->ivlen = traffic_keys->iv_len;
1033 transform->maclen = 0;
1034 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001035 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001036
1037 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001038 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001039 * type-extended and padded plaintext is therefore the padding
1040 * granularity. */
1041 transform->minlen =
1042 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1043
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001044#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001045 /*
1046 * Setup psa keys and alg
1047 */
Dave Rodgman2eab4622023-10-05 13:30:37 +01001048 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 transform->taglen,
1050 &alg,
1051 &key_type,
1052 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001053 MBEDTLS_SSL_DEBUG_RET(
1054 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001055 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001056 }
1057
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001058 transform->psa_alg = alg;
1059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1061 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1062 psa_set_key_algorithm(&attributes, alg);
1063 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if ((status = psa_import_key(&attributes,
1066 key_enc,
1067 PSA_BITS_TO_BYTES(key_bits),
1068 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001069 MBEDTLS_SSL_DEBUG_RET(
1070 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001071 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001072 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if ((status = psa_import_key(&attributes,
1077 key_dec,
1078 PSA_BITS_TO_BYTES(key_bits),
1079 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001080 MBEDTLS_SSL_DEBUG_RET(
1081 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001082 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001083 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001084 }
1085#endif /* MBEDTLS_USE_PSA_CRYPTO */
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001088}
1089
Jerry Yu84a6eda2022-11-04 11:17:35 +08001090MBEDTLS_CHECK_RETURN_CRITICAL
1091static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1093 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001094{
1095 psa_key_type_t key_type;
1096 psa_algorithm_t alg;
1097 size_t taglen;
1098 size_t key_bits;
1099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001102 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001104 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001106
Dave Rodgman2eab4622023-10-05 13:30:37 +01001107 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, taglen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 &alg, &key_type, &key_bits);
1109 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001110 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001114
1115 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1116 *iv_len = 12;
1117
1118 return 0;
1119}
1120
Jerry Yu91b560f2022-11-04 14:10:34 +08001121#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001122/*
1123 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001124 * the early application data and handshake messages as described in section 7
1125 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001126 *
Jerry Yue31688b2022-11-22 21:55:56 +08001127 * NOTE: Only one key is generated, the key for the traffic from the client to
1128 * the server. The TLS 1.3 specification does not define a secret and thus
1129 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001130 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001131MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001132static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1133 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001134{
1135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001136 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001137 psa_algorithm_t hash_alg;
1138 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001139 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1140 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001141 size_t key_len = 0;
1142 size_t iv_len = 0;
Yanray Wang16c895d2022-12-15 15:14:35 +08001143 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001144
1145 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001146 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1147 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1152 if (ret != 0) {
1153 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001154 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001155 }
1156
Dave Rodgman2eab4622023-10-05 13:30:37 +01001157 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Jerry Yu91b560f2022-11-04 14:10:34 +08001158
Dave Rodgman2eab4622023-10-05 13:30:37 +01001159 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1163 transcript,
1164 sizeof(transcript),
1165 &transcript_len);
1166 if (ret != 0) {
1167 MBEDTLS_SSL_DEBUG_RET(1,
1168 "mbedtls_ssl_get_handshake_transcript",
1169 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001170 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001171 }
1172
Jerry Yub094e122022-11-21 13:03:47 +08001173 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001175 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001177 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001179 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001180 }
1181
1182 MBEDTLS_SSL_DEBUG_BUF(
1183 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001184 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001185
1186 /*
1187 * Export client handshake traffic secret
1188 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001190 ssl->f_export_keys(
1191 ssl->p_export_keys,
1192 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001193 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001194 hash_len,
1195 handshake->randbytes,
1196 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001198 }
1199
Jerry Yua8771832022-11-21 23:16:54 +08001200 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001202 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 hash_len, traffic_keys->client_write_key, key_len,
1204 traffic_keys->client_write_iv, iv_len);
1205 if (ret != 0) {
1206 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001207 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001208 }
Jerry Yua8771832022-11-21 23:16:54 +08001209 traffic_keys->key_len = key_len;
1210 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1213 traffic_keys->client_write_key,
1214 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1217 traffic_keys->client_write_iv,
1218 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001221
Jerry Yu3d78e082022-11-23 18:26:20 +08001222cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001223 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001224 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001225 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1227 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001228}
1229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001231{
1232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1233 mbedtls_ssl_key_set traffic_keys;
1234 mbedtls_ssl_transform *transform_earlydata = NULL;
1235 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1236
1237 /* Next evolution in key schedule: Establish early_data secret and
1238 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1240 if (ret != 0) {
1241 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1242 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001243 goto cleanup;
1244 }
1245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1247 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001248 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1249 goto cleanup;
1250 }
1251
1252 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 transform_earlydata,
1254 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001255 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 &traffic_keys,
1257 ssl);
1258 if (ret != 0) {
1259 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001260 goto cleanup;
1261 }
1262 handshake->transform_earlydata = transform_earlydata;
1263
1264cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1266 if (ret != 0) {
1267 mbedtls_free(transform_earlydata);
1268 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001271}
1272#endif /* MBEDTLS_SSL_EARLY_DATA */
1273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001275{
Jerry Yue3131ef2021-09-16 13:14:15 +08001276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001277 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001278 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001279 unsigned char *psk = NULL;
1280 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (handshake->ciphersuite_info == NULL) {
1283 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1284 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001285 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001286
Dave Rodgman2eab4622023-10-05 13:30:37 +01001287 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001288#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1290 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1291 if (ret != 0) {
1292 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1293 ret);
1294 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001295 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001296 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001297#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1300 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001301#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001302 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001304#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (ret != 0) {
1306 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1307 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001308 }
1309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1311 handshake->tls13_master_secrets.early,
1312 PSA_HASH_LENGTH(hash_alg));
1313 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001314}
1315
Yanray Wang05402112022-12-13 18:50:42 +08001316/**
1317 * \brief Compute TLS 1.3 handshake traffic keys.
1318 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001319 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1320 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001321 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001322 *
1323 * \param ssl The SSL context to operate on. This must be in
1324 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001325 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001326 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001327 * keys. This must be writable but may be uninitialized.
1328 *
1329 * \returns \c 0 on success.
1330 * \returns A negative error code on failure.
1331 */
1332MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001333static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1334 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001335{
1336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001337 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001338 psa_algorithm_t hash_alg;
1339 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001340 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001341 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001342 size_t key_len = 0;
1343 size_t iv_len = 0;
Jerry Yu61e35e02021-09-16 18:59:08 +08001344
Jerry Yu435208a2021-10-13 11:22:16 +08001345 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001346 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1347 handshake->ciphersuite_info;
1348 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1349 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001350
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001351 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1354 if (ret != 0) {
1355 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001356 return ret;
1357 }
1358
Dave Rodgman2eab4622023-10-05 13:30:37 +01001359 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001360
Dave Rodgman2eab4622023-10-05 13:30:37 +01001361 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1365 transcript,
1366 sizeof(transcript),
1367 &transcript_len);
1368 if (ret != 0) {
1369 MBEDTLS_SSL_DEBUG_RET(1,
1370 "mbedtls_ssl_get_handshake_transcript",
1371 ret);
1372 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001373 }
1374
Xiaokang Qian123cde82023-03-29 06:54:51 +00001375 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1376 hash_alg, handshake->tls13_master_secrets.handshake,
1377 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (ret != 0) {
1379 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1380 ret);
1381 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1385 tls13_hs_secrets->client_handshake_traffic_secret,
1386 hash_len);
1387 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1388 tls13_hs_secrets->server_handshake_traffic_secret,
1389 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001390
1391 /*
1392 * Export client handshake traffic secret
1393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001395 ssl->f_export_keys(
1396 ssl->p_export_keys,
1397 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1398 tls13_hs_secrets->client_handshake_traffic_secret,
1399 hash_len,
1400 handshake->randbytes,
1401 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1402 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001403
Xiaokang Qian123cde82023-03-29 06:54:51 +00001404 ssl->f_export_keys(
1405 ssl->p_export_keys,
1406 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1407 tls13_hs_secrets->server_handshake_traffic_secret,
1408 hash_len,
1409 handshake->randbytes,
1410 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1411 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001412 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001413
Xiaokang Qian123cde82023-03-29 06:54:51 +00001414 ret = mbedtls_ssl_tls13_make_traffic_keys(
1415 hash_alg,
1416 tls13_hs_secrets->client_handshake_traffic_secret,
1417 tls13_hs_secrets->server_handshake_traffic_secret,
1418 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (ret != 0) {
1420 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001421 goto exit;
1422 }
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1425 traffic_keys->client_write_key,
1426 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1429 traffic_keys->server_write_key,
1430 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1433 traffic_keys->client_write_iv,
1434 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1437 traffic_keys->server_write_iv,
1438 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001439
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001440 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001441
1442exit:
1443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001445}
1446
Yanray Wang05402112022-12-13 18:50:42 +08001447/**
1448 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1449 *
1450 * The TLS 1.3 key schedule can be viewed as a simple state machine
1451 * with states Initial -> Early -> Handshake -> Application, and
1452 * this function represents the Early -> Handshake transition.
1453 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001454 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001455 * can be used to derive the handshake traffic keys.
1456 *
1457 * \param ssl The SSL context to operate on. This must be in key schedule
1458 * stage \c Early.
1459 *
1460 * \returns \c 0 on success.
1461 * \returns A negative error code on failure.
1462 */
1463MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001464static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001465{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001467 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001468 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001469 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001470 unsigned char *shared_secret = NULL;
1471 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001472
Ronald Crona2900bc2022-10-20 14:37:35 +02001473#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001474 /*
1475 * Compute ECDHE secret used to compute the handshake secret from which
1476 * client_handshake_traffic_secret and server_handshake_traffic_secret
1477 * are derived in the handshake secret derivation stage.
1478 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001480 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001481 mbedtls_ssl_tls13_named_group_is_ffdh(handshake->offered_group_id)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001482#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001483 psa_algorithm_t alg =
1484 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1485 PSA_ALG_ECDH : PSA_ALG_FFDH;
1486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001488 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001489 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1490
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001491 status = psa_get_key_attributes(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 &key_attributes);
1493 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001494 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 }
Ronald Cron3b056202022-10-05 17:20:21 +02001496
1497 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 psa_get_key_bits(&key_attributes));
1499 shared_secret = mbedtls_calloc(1, shared_secret_len);
1500 if (shared_secret == NULL) {
1501 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1502 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001503
Ronald Cron4c7edb22022-10-05 15:37:11 +02001504 status = psa_raw_key_agreement(
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001505 alg, handshake->xxdh_psa_privkey,
1506 handshake->xxdh_psa_peerkey, handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 shared_secret, shared_secret_len, &shared_secret_len);
1508 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001509 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001511 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001512 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001513
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001514 status = psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001516 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001518 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001519 }
1520
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001521 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001522#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 } else {
1524 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1525 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001526 }
1527 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001528#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001529
1530 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001531 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001532 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001533 ret = mbedtls_ssl_tls13_evolve_secret(
1534 hash_alg, handshake->tls13_master_secrets.early,
1535 shared_secret, shared_secret_len,
1536 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (ret != 0) {
1538 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001539 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001540 }
1541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1543 handshake->tls13_master_secrets.handshake,
1544 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001545
Ronald Cron3b056202022-10-05 17:20:21 +02001546cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (shared_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001548 mbedtls_zeroize_and_free(shared_secret, shared_secret_len);
Ronald Cron3b056202022-10-05 17:20:21 +02001549 }
1550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001552}
1553
Yanray Wang05402112022-12-13 18:50:42 +08001554/**
1555 * \brief Compute TLS 1.3 application traffic keys.
1556 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001557 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001558 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001559 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001560 *
1561 * \param ssl The SSL context to operate on. This must be in
1562 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001563 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001564 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001565 * keys. This must be writable but may be uninitialized.
1566 *
1567 * \returns \c 0 on success.
1568 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001569 */
Yanray Wang05402112022-12-13 18:50:42 +08001570MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001571static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 mbedtls_ssl_context *ssl,
1573 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001574{
XiaokangQiana7634982021-10-22 06:32:32 +00001575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001576 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001577
1578 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001579 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001580 &ssl->session_negotiate->app_secrets;
1581
1582 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001583 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001584 size_t transcript_len;
1585
1586 /* Variables relating to the hash for the chosen ciphersuite. */
1587 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001588
1589 psa_algorithm_t hash_alg;
1590 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001591
1592 /* Variables relating to the cipher for the chosen ciphersuite. */
Paul Elliott88600212023-11-23 14:24:30 +00001593 size_t key_len = 0, iv_len = 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001596
1597 /* Extract basic information about hash and ciphersuite */
1598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1600 &key_len, &iv_len);
1601 if (ret != 0) {
1602 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001603 goto cleanup;
1604 }
1605
Dave Rodgman2eab4622023-10-05 13:30:37 +01001606 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001607
Dave Rodgman2eab4622023-10-05 13:30:37 +01001608 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001610
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001611 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001612 * to call this at the right time, that is, after the ServerFinished. */
1613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1615 transcript, sizeof(transcript),
1616 &transcript_len);
1617 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001618 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001620
1621 /* Compute application secrets from master secret and transcript hash. */
1622
Xiaokang Qian123cde82023-03-29 06:54:51 +00001623 ret = mbedtls_ssl_tls13_derive_application_secrets(
1624 hash_alg, handshake->tls13_master_secrets.app,
1625 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001627 MBEDTLS_SSL_DEBUG_RET(
1628 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001629 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001630 }
1631
1632 /* Derive first epoch of IV + Key for application traffic. */
1633
Xiaokang Qian123cde82023-03-29 06:54:51 +00001634 ret = mbedtls_ssl_tls13_make_traffic_keys(
1635 hash_alg,
1636 app_secrets->client_application_traffic_secret_N,
1637 app_secrets->server_application_traffic_secret_N,
1638 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 if (ret != 0) {
1640 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001641 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001642 }
1643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1645 app_secrets->client_application_traffic_secret_N,
1646 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1649 app_secrets->server_application_traffic_secret_N,
1650 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001651
XiaokangQianac0385c2021-11-03 06:40:11 +00001652 /*
1653 * Export client/server application traffic secret 0
1654 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001656 ssl->f_export_keys(
1657 ssl->p_export_keys,
1658 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1659 app_secrets->client_application_traffic_secret_N, hash_len,
1660 handshake->randbytes,
1661 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1662 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1663 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001664
Xiaokang Qian123cde82023-03-29 06:54:51 +00001665 ssl->f_export_keys(
1666 ssl->p_export_keys,
1667 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1668 app_secrets->server_application_traffic_secret_N, hash_len,
1669 handshake->randbytes,
1670 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1671 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1672 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001673 }
1674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1676 traffic_keys->client_write_key, key_len);
1677 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1678 traffic_keys->server_write_key, key_len);
1679 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1680 traffic_keys->client_write_iv, iv_len);
1681 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1682 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001687 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1689 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1692 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001693}
1694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001696{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001698 mbedtls_ssl_key_set traffic_keys;
1699 mbedtls_ssl_transform *transform_handshake = NULL;
1700 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1701
1702 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001703 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 if (ret != 0) {
1705 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001706 goto cleanup;
1707 }
1708
1709 /* Next evolution in key schedule: Establish handshake secret and
1710 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001711 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001713 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001715 goto cleanup;
1716 }
1717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1719 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001720 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1721 goto cleanup;
1722 }
1723
Jerry Yuef2b98a2022-05-06 16:40:05 +08001724 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 transform_handshake,
1726 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001727 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 &traffic_keys,
1729 ssl);
1730 if (ret != 0) {
1731 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001732 goto cleanup;
1733 }
1734 handshake->transform_handshake = transform_handshake;
1735
1736cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1738 if (ret != 0) {
1739 mbedtls_free(transform_handshake);
1740 }
Jerry Yue110d252022-05-05 10:19:22 +08001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001743}
1744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001746{
Jerry Yu46bffe02022-09-13 11:25:28 +08001747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001748 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001749 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1750 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001751 size_t transcript_len;
1752
Xiaokang Qian123cde82023-03-29 06:54:51 +00001753 MBEDTLS_SSL_DEBUG_MSG(
1754 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001755
Dave Rodgman2eab4622023-10-05 13:30:37 +01001756 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1759 transcript, sizeof(transcript),
1760 &transcript_len);
1761 if (ret != 0) {
1762 return ret;
1763 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001764
1765 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001766 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 handshake->tls13_master_secrets.app,
1768 transcript, transcript_len,
1769 &ssl->session_negotiate->app_secrets);
1770 if (ret != 0) {
1771 return ret;
1772 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001773
Jerry Yuff226982022-04-16 16:52:57 +08001774 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1776 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001777
Xiaokang Qian123cde82023-03-29 06:54:51 +00001778 MBEDTLS_SSL_DEBUG_BUF(
1779 4, "Resumption master secret",
1780 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001781 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001782
Xiaokang Qian123cde82023-03-29 06:54:51 +00001783 MBEDTLS_SSL_DEBUG_MSG(
1784 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001786}
1787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001789{
1790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1791 mbedtls_ssl_key_set traffic_keys;
1792 mbedtls_ssl_transform *transform_application = NULL;
1793
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001794 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (ret != 0) {
1796 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001797 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001798 goto cleanup;
1799 }
1800
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001801 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (ret != 0) {
1803 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001804 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001805 goto cleanup;
1806 }
1807
1808 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1810 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001811 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1812 goto cleanup;
1813 }
1814
1815 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 transform_application,
1817 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001818 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 &traffic_keys,
1820 ssl);
1821 if (ret != 0) {
1822 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001823 goto cleanup;
1824 }
1825
1826 ssl->transform_application = transform_application;
1827
1828cleanup:
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1831 if (ret != 0) {
1832 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001835}
1836
Ronald Cron41a443a2022-10-04 16:38:25 +02001837#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001838int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1839 unsigned char **psk,
1840 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001841{
Jerry Yu40f37712022-07-26 16:58:57 +08001842#if defined(MBEDTLS_USE_PSA_CRYPTO)
1843 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001845
1846 *psk_len = 0;
1847 *psk = NULL;
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1850 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001851 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001852
1853 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1854 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001855 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 }
1857
1858 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1859 *psk = mbedtls_calloc(1, *psk_len);
1860 if (*psk == NULL) {
1861 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1862 }
1863
1864 status = psa_export_key(ssl->handshake->psk_opaque,
1865 (uint8_t *) *psk, *psk_len, psk_len);
1866 if (status != PSA_SUCCESS) {
1867 mbedtls_free((void *) *psk);
1868 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001869 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 }
1871 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001872#else
1873 *psk = ssl->handshake->psk;
1874 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (*psk == NULL) {
1876 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1877 }
1878 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001879#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001880}
Ronald Cron41a443a2022-10-04 16:38:25 +02001881#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001882
Max Fillinger44042f02024-07-22 14:43:56 +02001883int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
1884 const unsigned char *secret, const size_t secret_len,
1885 const unsigned char *label, const size_t label_len,
1886 const unsigned char *context_value, const size_t context_len,
1887 unsigned char *out, const size_t out_len)
1888{
1889 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
1890 unsigned char hkdf_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Max Fillinger44042f02024-07-22 14:43:56 +02001891 int ret = 0;
Max Fillinger44042f02024-07-22 14:43:56 +02001892
1893 ret = mbedtls_ssl_tls13_derive_secret(hash_alg, secret, secret_len, label, label_len, NULL, 0,
Max Fillinger9359f4d2024-09-21 10:48:57 +02001894 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, hkdf_secret,
1895 hash_len);
Max Fillinger44042f02024-07-22 14:43:56 +02001896 if (ret != 0) {
1897 goto exit;
1898 }
Max Fillinger9359f4d2024-09-21 10:48:57 +02001899 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
1900 hkdf_secret,
1901 hash_len,
Max Fillinger404f7a32024-08-12 11:20:39 +02001902 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter),
Max Fillinger9359f4d2024-09-21 10:48:57 +02001903 context_value,
1904 context_len,
1905 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
1906 out,
1907 out_len);
Max Fillinger44042f02024-07-22 14:43:56 +02001908
1909exit:
1910 mbedtls_platform_zeroize(hkdf_secret, sizeof(hkdf_secret));
1911 return ret;
1912}
1913
Ronald Cron6f135e12021-12-08 16:57:54 +01001914#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */