blob: 3d31013c6cb84810fbe54f10e5d1d64e65e3639d [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:
Max Fillinger7833b182024-12-02 19:26:13 +010061 * - desired_length: Length of expanded key material.
62 * As the type implies, this must be less than 2**16 bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000063 * - (label, label_len): label + label length, without "tls13 " prefix
64 * The label length MUST be less than or equal to
Max Fillinger529931a2024-11-25 20:38:04 +010065 * MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000066 * It is the caller's responsibility to ensure this.
67 * All (label, label length) pairs used in TLS 1.3
68 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
69 * - (ctx, ctx_len): context + context length
70 * The context length MUST be less than or equal to
71 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
72 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010073 * - dst: Target buffer for HkdfLabel structure,
74 * This MUST be a writable buffer of size
75 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000076 * - dst_len: Pointer at which to store the actual length of
77 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010078 */
79
Xiaofei Baid25fab62021-12-02 06:36:27 +000080static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010081
Gilles Peskine449bd832023-01-11 14:50:10 +010082#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
83 (2 /* expansion length */ \
84 + 1 /* label length */ \
85 + label_len \
86 + 1 /* context length */ \
87 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010088
89#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
90 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +010091 sizeof(tls13_label_prefix) + \
Max Fillinger529931a2024-11-25 20:38:04 +010092 MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN, \
Gilles Peskine449bd832023-01-11 14:50:10 +010093 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010094
Xiaofei Bai746f9482021-11-12 08:53:56 +000095static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +010096 size_t desired_length,
97 const unsigned char *label, size_t label_len,
98 const unsigned char *ctx, size_t ctx_len,
99 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100100{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100101 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000102 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100103 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100105
106 unsigned char *p = dst;
107
Max Fillingerffc47e62024-10-29 18:49:30 +0100108 /* Add the size of the expanded key material. */
109#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > UINT16_MAX
110#error "The desired key length must fit into an uint16 but \
111 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN is greater than UINT16_MAX"
Hanno Becker531fe302020-09-16 09:45:27 +0100112#endif
113
Max Fillingerffc47e62024-10-29 18:49:30 +0100114 *p++ = MBEDTLS_BYTE_1(desired_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100116
117 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 *p++ = MBEDTLS_BYTE_0(total_label_len);
119 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000120 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000122 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100123
124 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 *p++ = MBEDTLS_BYTE_0(ctx_len);
126 if (ctx_len != 0) {
127 memcpy(p, ctx, ctx_len);
128 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100129
130 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000131 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100132}
133
Xiaofei Bai746f9482021-11-12 08:53:56 +0000134int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 psa_algorithm_t hash_alg,
136 const unsigned char *secret, size_t secret_len,
137 const unsigned char *label, size_t label_len,
138 const unsigned char *ctx, size_t ctx_len,
139 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100140{
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200142 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
144 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200145 psa_key_derivation_operation_t operation =
146 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100147
Max Fillinger529931a2024-11-25 20:38:04 +0100148 if (label_len > MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100149 /* Should never happen since this is an internal
150 * function, and we know statically which labels
151 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100153 }
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100156 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100158 }
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100161 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100163 }
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (!PSA_ALG_IS_HASH(hash_alg)) {
166 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
167 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 ssl_tls13_hkdf_encode_label(buf_len,
170 label, label_len,
171 ctx, ctx_len,
172 hkdf_label,
173 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (status != PSA_SUCCESS) {
178 goto cleanup;
179 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 status = psa_key_derivation_input_bytes(&operation,
182 PSA_KEY_DERIVATION_INPUT_SECRET,
183 secret,
184 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (status != PSA_SUCCESS) {
187 goto cleanup;
188 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 status = psa_key_derivation_input_bytes(&operation,
191 PSA_KEY_DERIVATION_INPUT_INFO,
192 hkdf_label,
193 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (status != PSA_SUCCESS) {
196 goto cleanup;
197 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 status = psa_key_derivation_output_bytes(&operation,
200 buf,
201 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (status != PSA_SUCCESS) {
204 goto cleanup;
205 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200206
207cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 abort_status = psa_key_derivation_abort(&operation);
209 status = (status == PSA_SUCCESS ? abort_status : status);
210 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500211 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100212}
213
Jerry Yua5db6c02022-11-23 18:08:04 +0800214MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800215static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 psa_algorithm_t hash_alg,
217 const unsigned char *secret, size_t secret_len,
218 unsigned char *key, size_t key_len,
219 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800220{
221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
222
Jerry Yuaec08b32022-11-29 15:19:27 +0800223 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 hash_alg,
225 secret, secret_len,
226 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
227 NULL, 0,
228 key, key_len);
229 if (ret != 0) {
230 return ret;
231 }
Jerry Yua8771832022-11-21 23:16:54 +0800232
Jerry Yuaec08b32022-11-29 15:19:27 +0800233 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 hash_alg,
235 secret, secret_len,
236 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
237 NULL, 0,
238 iv, iv_len);
239 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800240}
241
Hanno Becker3385a4d2020-08-21 13:03:34 +0100242/*
243 * The traffic keying material is generated from the following inputs:
244 *
245 * - One secret value per sender.
246 * - A purpose value indicating the specific value being generated
247 * - The desired lengths of key and IV.
248 *
249 * The expansion itself is based on HKDF:
250 *
251 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
252 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
253 *
254 * [sender] denotes the sending side and the Secret value is provided
255 * by the function caller. Note that we generate server and client side
256 * keys in a single function call.
257 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000258int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 psa_algorithm_t hash_alg,
260 const unsigned char *client_secret,
261 const unsigned char *server_secret, size_t secret_len,
262 size_t key_len, size_t iv_len,
263 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100264{
265 int ret = 0;
266
Jerry Yua8771832022-11-21 23:16:54 +0800267 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 hash_alg, client_secret, secret_len,
269 keys->client_write_key, key_len,
270 keys->client_write_iv, iv_len);
271 if (ret != 0) {
272 return ret;
273 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100274
Jerry Yua8771832022-11-21 23:16:54 +0800275 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 hash_alg, server_secret, secret_len,
277 keys->server_write_key, key_len,
278 keys->server_write_iv, iv_len);
279 if (ret != 0) {
280 return ret;
281 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100282
Hanno Becker493ea7f2020-09-08 11:01:00 +0100283 keys->key_len = key_len;
284 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100287}
288
Xiaofei Bai746f9482021-11-12 08:53:56 +0000289int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 psa_algorithm_t hash_alg,
291 const unsigned char *secret, size_t secret_len,
292 const unsigned char *label, size_t label_len,
293 const unsigned char *ctx, size_t ctx_len,
294 int ctx_hashed,
295 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100296{
297 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
299 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
303 PSA_HASH_LENGTH(hash_alg), &ctx_len);
304 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500305 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100306 return ret;
307 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 } else {
309 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100310 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100311 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100312 * Let's double-check nonetheless to not run at the risk
313 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100315 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100318 }
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
321 secret, secret_len,
322 label, label_len,
323 hashed_context, ctx_len,
324 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100325
Hanno Beckerb35d5222020-08-21 13:27:44 +0100326}
327
Xiaofei Bai746f9482021-11-12 08:53:56 +0000328int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 psa_algorithm_t hash_alg,
330 const unsigned char *secret_old,
331 const unsigned char *input, size_t input_len,
332 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100333{
334 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200335 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
336 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200337 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
339 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200340 const unsigned char *l_input = NULL;
341 size_t l_input_len;
342
Przemek Stekield5ae3652022-05-13 12:10:08 +0200343 psa_key_derivation_operation_t operation =
344 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (!PSA_ALG_IS_HASH(hash_alg)) {
347 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
348 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100351
352 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100353 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000355 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 hash_alg,
357 secret_old, hlen,
358 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
359 NULL, 0, /* context */
360 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
361 tmp_secret, hlen);
362 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100363 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100365 }
366
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200367 ret = 0;
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200370 l_input = input;
371 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200373 l_input = all_zeroes_input;
374 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100375 }
376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 status = psa_key_derivation_setup(&operation,
378 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (status != PSA_SUCCESS) {
381 goto cleanup;
382 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 status = psa_key_derivation_input_bytes(&operation,
385 PSA_KEY_DERIVATION_INPUT_SALT,
386 tmp_secret,
387 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (status != PSA_SUCCESS) {
390 goto cleanup;
391 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 status = psa_key_derivation_input_bytes(&operation,
394 PSA_KEY_DERIVATION_INPUT_SECRET,
395 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (status != PSA_SUCCESS) {
398 goto cleanup;
399 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 status = psa_key_derivation_output_bytes(&operation,
402 secret_new,
403 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (status != PSA_SUCCESS) {
406 goto cleanup;
407 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409cleanup:
410 abort_status = psa_key_derivation_abort(&operation);
411 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500412 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
414 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100415}
416
Xiaofei Bai746f9482021-11-12 08:53:56 +0000417int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 psa_algorithm_t hash_alg,
419 unsigned char const *early_secret,
420 unsigned char const *transcript, size_t transcript_len,
421 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100422{
423 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100425
426 /* We should never call this function with an unknown hash,
427 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (!PSA_ALG_IS_HASH(hash_alg)) {
429 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
430 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100431
432 /*
433 * 0
434 * |
435 * v
436 * PSK -> HKDF-Extract = Early Secret
437 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100438 * +-----> Derive-Secret(., "c e traffic", ClientHello)
439 * | = client_early_traffic_secret
440 * |
441 * +-----> Derive-Secret(., "e exp master", ClientHello)
442 * | = early_exporter_master_secret
443 * v
444 */
445
446 /* Create client_early_traffic_secret */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000447 ret = mbedtls_ssl_tls13_derive_secret(
448 hash_alg,
449 early_secret, hash_len,
450 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
451 transcript, transcript_len,
452 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
453 derived->client_early_traffic_secret,
454 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (ret != 0) {
456 return ret;
457 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100458
459 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000460 ret = mbedtls_ssl_tls13_derive_secret(
461 hash_alg,
462 early_secret, hash_len,
463 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
464 transcript, transcript_len,
465 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
466 derived->early_exporter_master_secret,
467 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (ret != 0) {
469 return ret;
470 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100473}
474
Xiaofei Bai746f9482021-11-12 08:53:56 +0000475int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 psa_algorithm_t hash_alg,
477 unsigned char const *handshake_secret,
478 unsigned char const *transcript, size_t transcript_len,
479 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100480{
481 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100483
484 /* We should never call this function with an unknown hash,
485 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if (!PSA_ALG_IS_HASH(hash_alg)) {
487 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
488 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100489
490 /*
491 *
492 * Handshake Secret
493 * |
494 * +-----> Derive-Secret( ., "c hs traffic",
495 * | ClientHello...ServerHello )
496 * | = client_handshake_traffic_secret
497 * |
498 * +-----> Derive-Secret( ., "s hs traffic",
499 * | ClientHello...ServerHello )
500 * | = server_handshake_traffic_secret
501 *
502 */
503
504 /*
505 * Compute client_handshake_traffic_secret with
506 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
507 */
508
Xiaokang Qian123cde82023-03-29 06:54:51 +0000509 ret = mbedtls_ssl_tls13_derive_secret(
510 hash_alg,
511 handshake_secret, hash_len,
512 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
513 transcript, transcript_len,
514 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
515 derived->client_handshake_traffic_secret,
516 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (ret != 0) {
518 return ret;
519 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100520
521 /*
522 * Compute server_handshake_traffic_secret with
523 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
524 */
525
Xiaokang Qian123cde82023-03-29 06:54:51 +0000526 ret = mbedtls_ssl_tls13_derive_secret(
527 hash_alg,
528 handshake_secret, hash_len,
529 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
530 transcript, transcript_len,
531 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
532 derived->server_handshake_traffic_secret,
533 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (ret != 0) {
535 return ret;
536 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100539}
540
Xiaofei Bai746f9482021-11-12 08:53:56 +0000541int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 psa_algorithm_t hash_alg,
543 unsigned char const *application_secret,
544 unsigned char const *transcript, size_t transcript_len,
545 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100546{
547 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100549
550 /* We should never call this function with an unknown hash,
551 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (!PSA_ALG_IS_HASH(hash_alg)) {
553 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
554 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100555
556 /* Generate {client,server}_application_traffic_secret_0
557 *
558 * Master Secret
559 * |
560 * +-----> Derive-Secret( ., "c ap traffic",
561 * | ClientHello...server Finished )
562 * | = client_application_traffic_secret_0
563 * |
564 * +-----> Derive-Secret( ., "s ap traffic",
565 * | ClientHello...Server Finished )
566 * | = server_application_traffic_secret_0
567 * |
568 * +-----> Derive-Secret( ., "exp master",
569 * | ClientHello...server Finished)
570 * | = exporter_master_secret
571 *
572 */
573
Xiaokang Qian123cde82023-03-29 06:54:51 +0000574 ret = mbedtls_ssl_tls13_derive_secret(
575 hash_alg,
576 application_secret, hash_len,
577 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
578 transcript, transcript_len,
579 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
580 derived->client_application_traffic_secret_N,
581 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (ret != 0) {
583 return ret;
584 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100585
Xiaokang Qian123cde82023-03-29 06:54:51 +0000586 ret = mbedtls_ssl_tls13_derive_secret(
587 hash_alg,
588 application_secret, hash_len,
589 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
590 transcript, transcript_len,
591 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
592 derived->server_application_traffic_secret_N,
593 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (ret != 0) {
595 return ret;
596 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100597
Xiaokang Qian123cde82023-03-29 06:54:51 +0000598 ret = mbedtls_ssl_tls13_derive_secret(
599 hash_alg,
600 application_secret, hash_len,
601 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
602 transcript, transcript_len,
603 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
604 derived->exporter_master_secret,
605 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (ret != 0) {
607 return ret;
608 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100611}
612
613/* Generate resumption_master_secret for use with the ticket exchange.
614 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000615 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100616 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000617int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 psa_algorithm_t hash_alg,
619 unsigned char const *application_secret,
620 unsigned char const *transcript, size_t transcript_len,
621 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100622{
623 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100625
626 /* We should never call this function with an unknown hash,
627 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (!PSA_ALG_IS_HASH(hash_alg)) {
629 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
630 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100631
Xiaokang Qian123cde82023-03-29 06:54:51 +0000632 ret = mbedtls_ssl_tls13_derive_secret(
633 hash_alg,
634 application_secret, hash_len,
635 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
636 transcript, transcript_len,
637 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
638 derived->resumption_master_secret,
639 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (ret != 0) {
642 return ret;
643 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100646}
647
Yanray Wang05402112022-12-13 18:50:42 +0800648/**
649 * \brief Transition into application stage of TLS 1.3 key schedule.
650 *
651 * The TLS 1.3 key schedule can be viewed as a simple state machine
652 * with states Initial -> Early -> Handshake -> Application, and
653 * this function represents the Handshake -> Application transition.
654 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800655 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800656 * can be used to derive the handshake traffic keys.
657 *
658 * \param ssl The SSL context to operate on. This must be in key schedule
659 * stage \c Handshake.
660 *
661 * \returns \c 0 on success.
662 * \returns A negative error code on failure.
663 */
664MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800665static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000666{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000668 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200669 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100670 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000671
672 /*
673 * Compute MasterSecret
674 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000675 ret = mbedtls_ssl_tls13_evolve_secret(
676 hash_alg,
677 handshake->tls13_master_secrets.handshake,
678 NULL, 0,
679 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (ret != 0) {
681 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
682 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000683 }
684
Xiaokang Qian123cde82023-03-29 06:54:51 +0000685 MBEDTLS_SSL_DEBUG_BUF(
686 4, "Master secret",
687 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000690}
691
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200692MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100693static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
694 unsigned char const *base_key,
695 unsigned char const *transcript,
696 unsigned char *dst,
697 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100698{
Gabor Mezei07732f72022-03-26 17:04:19 +0100699 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
701 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100703 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100704 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100705 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100706
707 /* We should never call this function with an unknown hash,
708 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (!PSA_ALG_IS_HASH(hash_alg)) {
710 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
711 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100712
713 /* TLS 1.3 Finished message
714 *
715 * struct {
716 * opaque verify_data[Hash.length];
717 * } Finished;
718 *
719 * verify_data =
720 * HMAC( finished_key,
721 * Hash( Handshake Context +
722 * Certificate* +
723 * CertificateVerify* )
724 * )
725 *
726 * finished_key =
727 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
728 */
729
Xiaofei Bai746f9482021-11-12 08:53:56 +0000730 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 hash_alg, base_key, hash_len,
732 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
733 NULL, 0,
734 finished_key, hash_len);
735 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100736 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100737 }
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 alg = PSA_ALG_HMAC(hash_alg);
740 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
741 psa_set_key_algorithm(&attributes, alg);
742 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
743
744 status = psa_import_key(&attributes, finished_key, hash_len, &key);
745 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500746 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 goto exit;
748 }
749
750 status = psa_mac_compute(key, alg, transcript, hash_len,
751 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500752 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100753
754exit:
755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 status = psa_destroy_key(key);
757 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500758 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100764}
765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
767 unsigned char *dst,
768 size_t dst_len,
769 size_t *actual_len,
770 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000771{
XiaokangQiana7634982021-10-22 06:32:32 +0000772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000773
XiaokangQianaaa0e192021-11-10 03:07:04 +0000774 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000775 size_t transcript_len;
776
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800777 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800778 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800779 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800781
Dave Rodgman2eab4622023-10-05 13:30:37 +0100782 mbedtls_md_type_t const md_type = (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100783
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200784 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100785 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800791 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
793 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800794 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800796 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800799 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
800 goto exit;
801 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
804 transcript, sizeof(transcript),
805 &transcript_len);
806 if (ret != 0) {
807 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000808 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000809 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000811
Xiaokang Qian123cde82023-03-29 06:54:51 +0000812 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
813 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000815 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
819 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000820
821exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800822 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 mbedtls_platform_zeroize(base_key, base_key_len);
824 mbedtls_platform_zeroize(transcript, sizeof(transcript));
825 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000826}
827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
829 const psa_algorithm_t hash_alg,
830 unsigned char const *psk, size_t psk_len,
831 int psk_type,
832 unsigned char const *transcript,
833 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100834{
835 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100836 unsigned char binder_key[PSA_MAC_MAX_SIZE];
837 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100839 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100840
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100841#if !defined(MBEDTLS_DEBUG_C)
842 ssl = NULL; /* make sure we don't use it except for debug */
843 ((void) ssl);
844#endif
845
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100846 /* We should never call this function with an unknown hash,
847 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (!PSA_ALG_IS_HASH(hash_alg)) {
849 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
850 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100851
852 /*
853 * 0
854 * |
855 * v
856 * PSK -> HKDF-Extract = Early Secret
857 * |
858 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
859 * | = binder_key
860 * v
861 */
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
864 NULL, /* Old secret */
865 psk, psk_len, /* Input */
866 early_secret);
867 if (ret != 0) {
868 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100869 goto exit;
870 }
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
873 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000876 ret = mbedtls_ssl_tls13_derive_secret(
877 hash_alg,
878 early_secret, hash_len,
879 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
880 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
881 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
883 } else {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000884 ret = mbedtls_ssl_tls13_derive_secret(
885 hash_alg,
886 early_secret, hash_len,
887 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
888 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
889 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100891 }
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (ret != 0) {
894 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100895 goto exit;
896 }
897
898 /*
899 * The binding_value is computed in the same way as the Finished message
900 * but with the BaseKey being the binder_key.
901 */
902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
904 result, &actual_len);
905 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100906 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100910
911exit:
912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
914 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
915 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100916}
917
Xiaokang Qian123cde82023-03-29 06:54:51 +0000918int mbedtls_ssl_tls13_populate_transform(
919 mbedtls_ssl_transform *transform,
920 int endpoint, int ciphersuite,
921 mbedtls_ssl_key_set const *traffic_keys,
922 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000923{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100924#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000925 int ret;
926 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200927#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000928 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
929 unsigned char const *key_enc;
930 unsigned char const *iv_enc;
931 unsigned char const *key_dec;
932 unsigned char const *iv_dec;
933
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100934#if defined(MBEDTLS_USE_PSA_CRYPTO)
935 psa_key_type_t key_type;
936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
937 psa_algorithm_t alg;
938 size_t key_bits;
939 psa_status_t status = PSA_SUCCESS;
940#endif
941
Hanno Beckerc94060c2021-03-22 07:50:44 +0000942#if !defined(MBEDTLS_DEBUG_C)
943 ssl = NULL; /* make sure we don't use it except for those cases */
944 (void) ssl;
945#endif
946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
948 if (ciphersuite_info == NULL) {
949 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
950 ciphersuite));
951 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100952 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000953
Neil Armstronga8093f52022-05-04 17:44:05 +0200954#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
956 if (cipher_info == NULL) {
957 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
958 ciphersuite_info->cipher));
959 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000960 }
961
962 /*
963 * Setup cipher contexts in target transform
964 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
966 cipher_info)) != 0) {
967 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
968 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000969 }
970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
972 cipher_info)) != 0) {
973 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
974 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000975 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100976#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000977
978#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000980 key_enc = traffic_keys->server_write_key;
981 key_dec = traffic_keys->client_write_key;
982 iv_enc = traffic_keys->server_write_iv;
983 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000985#endif /* MBEDTLS_SSL_SRV_C */
986#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000988 key_enc = traffic_keys->client_write_key;
989 key_dec = traffic_keys->server_write_key;
990 iv_enc = traffic_keys->client_write_iv;
991 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000993#endif /* MBEDTLS_SSL_CLI_C */
994 {
995 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000997 }
998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
1000 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +00001001
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001002#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
Mehmet Cagri Aksoy56e90112023-10-11 15:28:06 +02001004 key_enc, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 MBEDTLS_ENCRYPT)) != 0) {
1006 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1007 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001008 }
1009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
Mehmet Cagri Aksoy56e90112023-10-11 15:28:06 +02001011 key_dec, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 MBEDTLS_DECRYPT)) != 0) {
1013 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1014 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001015 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001016#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001017
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001018 /*
1019 * Setup other fields in SSL transform
1020 */
1021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001023 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001025 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001027
1028 transform->ivlen = traffic_keys->iv_len;
1029 transform->maclen = 0;
1030 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001031 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001032
1033 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001034 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001035 * type-extended and padded plaintext is therefore the padding
1036 * granularity. */
1037 transform->minlen =
1038 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1039
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001040#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001041 /*
1042 * Setup psa keys and alg
1043 */
Dave Rodgman2eab4622023-10-05 13:30:37 +01001044 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 transform->taglen,
1046 &alg,
1047 &key_type,
1048 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001049 MBEDTLS_SSL_DEBUG_RET(
1050 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001051 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001052 }
1053
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001054 transform->psa_alg = alg;
1055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1057 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1058 psa_set_key_algorithm(&attributes, alg);
1059 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if ((status = psa_import_key(&attributes,
1062 key_enc,
1063 PSA_BITS_TO_BYTES(key_bits),
1064 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001065 MBEDTLS_SSL_DEBUG_RET(
1066 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001067 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001068 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((status = psa_import_key(&attributes,
1073 key_dec,
1074 PSA_BITS_TO_BYTES(key_bits),
1075 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001076 MBEDTLS_SSL_DEBUG_RET(
1077 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001078 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001079 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001080 }
1081#endif /* MBEDTLS_USE_PSA_CRYPTO */
1082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001084}
1085
Jerry Yu84a6eda2022-11-04 11:17:35 +08001086MBEDTLS_CHECK_RETURN_CRITICAL
1087static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1089 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001090{
1091 psa_key_type_t key_type;
1092 psa_algorithm_t alg;
1093 size_t taglen;
1094 size_t key_bits;
1095 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001098 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001100 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001102
Dave Rodgman2eab4622023-10-05 13:30:37 +01001103 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, taglen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 &alg, &key_type, &key_bits);
1105 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001106 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001110
1111 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1112 *iv_len = 12;
1113
1114 return 0;
1115}
1116
Jerry Yu91b560f2022-11-04 14:10:34 +08001117#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001118/*
1119 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001120 * the early application data and handshake messages as described in section 7
1121 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001122 *
Jerry Yue31688b2022-11-22 21:55:56 +08001123 * NOTE: Only one key is generated, the key for the traffic from the client to
1124 * the server. The TLS 1.3 specification does not define a secret and thus
1125 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001126 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001127MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001128static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1129 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001130{
1131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001132 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001133 psa_algorithm_t hash_alg;
1134 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001135 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1136 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001137 size_t key_len = 0;
1138 size_t iv_len = 0;
Yanray Wang16c895d2022-12-15 15:14:35 +08001139 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001140
1141 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001142 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1143 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1148 if (ret != 0) {
1149 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001150 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001151 }
1152
Dave Rodgman2eab4622023-10-05 13:30:37 +01001153 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Jerry Yu91b560f2022-11-04 14:10:34 +08001154
Dave Rodgman2eab4622023-10-05 13:30:37 +01001155 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1159 transcript,
1160 sizeof(transcript),
1161 &transcript_len);
1162 if (ret != 0) {
1163 MBEDTLS_SSL_DEBUG_RET(1,
1164 "mbedtls_ssl_get_handshake_transcript",
1165 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001166 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001167 }
1168
Jerry Yub094e122022-11-21 13:03:47 +08001169 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001171 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001173 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001175 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001176 }
1177
1178 MBEDTLS_SSL_DEBUG_BUF(
1179 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001180 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001181
1182 /*
1183 * Export client handshake traffic secret
1184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001186 ssl->f_export_keys(
1187 ssl->p_export_keys,
1188 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001189 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001190 hash_len,
1191 handshake->randbytes,
1192 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001194 }
1195
Jerry Yua8771832022-11-21 23:16:54 +08001196 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001198 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 hash_len, traffic_keys->client_write_key, key_len,
1200 traffic_keys->client_write_iv, iv_len);
1201 if (ret != 0) {
1202 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001203 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001204 }
Jerry Yua8771832022-11-21 23:16:54 +08001205 traffic_keys->key_len = key_len;
1206 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1209 traffic_keys->client_write_key,
1210 traffic_keys->key_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_iv",
1213 traffic_keys->client_write_iv,
1214 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001217
Jerry Yu3d78e082022-11-23 18:26:20 +08001218cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001219 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001220 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001221 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1223 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001224}
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001227{
1228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1229 mbedtls_ssl_key_set traffic_keys;
1230 mbedtls_ssl_transform *transform_earlydata = NULL;
1231 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1232
1233 /* Next evolution in key schedule: Establish early_data secret and
1234 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1236 if (ret != 0) {
1237 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1238 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001239 goto cleanup;
1240 }
1241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1243 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001244 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1245 goto cleanup;
1246 }
1247
1248 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 transform_earlydata,
1250 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001251 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 &traffic_keys,
1253 ssl);
1254 if (ret != 0) {
1255 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001256 goto cleanup;
1257 }
1258 handshake->transform_earlydata = transform_earlydata;
1259
1260cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1262 if (ret != 0) {
1263 mbedtls_free(transform_earlydata);
1264 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001267}
1268#endif /* MBEDTLS_SSL_EARLY_DATA */
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001271{
Jerry Yue3131ef2021-09-16 13:14:15 +08001272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001273 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001274 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001275 unsigned char *psk = NULL;
1276 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 if (handshake->ciphersuite_info == NULL) {
1279 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1280 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001281 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001282
Dave Rodgman2eab4622023-10-05 13:30:37 +01001283 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001284#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1286 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1287 if (ret != 0) {
1288 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1289 ret);
1290 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001291 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001292 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001293#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1296 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001297#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001298 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001300#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (ret != 0) {
1302 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1303 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001304 }
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1307 handshake->tls13_master_secrets.early,
1308 PSA_HASH_LENGTH(hash_alg));
1309 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001310}
1311
Yanray Wang05402112022-12-13 18:50:42 +08001312/**
1313 * \brief Compute TLS 1.3 handshake traffic keys.
1314 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001315 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1316 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001317 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001318 *
1319 * \param ssl The SSL context to operate on. This must be in
1320 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001321 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001322 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001323 * keys. This must be writable but may be uninitialized.
1324 *
1325 * \returns \c 0 on success.
1326 * \returns A negative error code on failure.
1327 */
1328MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001329static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1330 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001331{
1332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001333 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001334 psa_algorithm_t hash_alg;
1335 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001336 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001337 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001338 size_t key_len = 0;
1339 size_t iv_len = 0;
Jerry Yu61e35e02021-09-16 18:59:08 +08001340
Jerry Yu435208a2021-10-13 11:22:16 +08001341 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001342 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1343 handshake->ciphersuite_info;
1344 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1345 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001346
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001347 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1350 if (ret != 0) {
1351 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001352 return ret;
1353 }
1354
Dave Rodgman2eab4622023-10-05 13:30:37 +01001355 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001356
Dave Rodgman2eab4622023-10-05 13:30:37 +01001357 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1361 transcript,
1362 sizeof(transcript),
1363 &transcript_len);
1364 if (ret != 0) {
1365 MBEDTLS_SSL_DEBUG_RET(1,
1366 "mbedtls_ssl_get_handshake_transcript",
1367 ret);
1368 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001369 }
1370
Xiaokang Qian123cde82023-03-29 06:54:51 +00001371 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1372 hash_alg, handshake->tls13_master_secrets.handshake,
1373 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if (ret != 0) {
1375 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1376 ret);
1377 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001378 }
1379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1381 tls13_hs_secrets->client_handshake_traffic_secret,
1382 hash_len);
1383 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1384 tls13_hs_secrets->server_handshake_traffic_secret,
1385 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001386
1387 /*
1388 * Export client handshake traffic secret
1389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001391 ssl->f_export_keys(
1392 ssl->p_export_keys,
1393 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1394 tls13_hs_secrets->client_handshake_traffic_secret,
1395 hash_len,
1396 handshake->randbytes,
1397 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1398 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001399
Xiaokang Qian123cde82023-03-29 06:54:51 +00001400 ssl->f_export_keys(
1401 ssl->p_export_keys,
1402 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1403 tls13_hs_secrets->server_handshake_traffic_secret,
1404 hash_len,
1405 handshake->randbytes,
1406 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1407 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001408 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001409
Xiaokang Qian123cde82023-03-29 06:54:51 +00001410 ret = mbedtls_ssl_tls13_make_traffic_keys(
1411 hash_alg,
1412 tls13_hs_secrets->client_handshake_traffic_secret,
1413 tls13_hs_secrets->server_handshake_traffic_secret,
1414 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (ret != 0) {
1416 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001417 goto exit;
1418 }
1419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1421 traffic_keys->client_write_key,
1422 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1425 traffic_keys->server_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, "client_handshake write_iv",
1429 traffic_keys->client_write_iv,
1430 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1433 traffic_keys->server_write_iv,
1434 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001435
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001436 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001437
1438exit:
1439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001441}
1442
Yanray Wang05402112022-12-13 18:50:42 +08001443/**
1444 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1445 *
1446 * The TLS 1.3 key schedule can be viewed as a simple state machine
1447 * with states Initial -> Early -> Handshake -> Application, and
1448 * this function represents the Early -> Handshake transition.
1449 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001450 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001451 * can be used to derive the handshake traffic keys.
1452 *
1453 * \param ssl The SSL context to operate on. This must be in key schedule
1454 * stage \c Early.
1455 *
1456 * \returns \c 0 on success.
1457 * \returns A negative error code on failure.
1458 */
1459MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001460static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001461{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001463 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001464 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001465 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001466 unsigned char *shared_secret = NULL;
1467 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001468
Ronald Crona2900bc2022-10-20 14:37:35 +02001469#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001470 /*
1471 * Compute ECDHE secret used to compute the handshake secret from which
1472 * client_handshake_traffic_secret and server_handshake_traffic_secret
1473 * are derived in the handshake secret derivation stage.
1474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001476 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001477 mbedtls_ssl_tls13_named_group_is_ffdh(handshake->offered_group_id)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001478#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001479 psa_algorithm_t alg =
1480 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1481 PSA_ALG_ECDH : PSA_ALG_FFDH;
1482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001484 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001485 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1486
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001487 status = psa_get_key_attributes(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 &key_attributes);
1489 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001490 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 }
Ronald Cron3b056202022-10-05 17:20:21 +02001492
1493 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 psa_get_key_bits(&key_attributes));
1495 shared_secret = mbedtls_calloc(1, shared_secret_len);
1496 if (shared_secret == NULL) {
1497 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1498 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001499
Ronald Cron4c7edb22022-10-05 15:37:11 +02001500 status = psa_raw_key_agreement(
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001501 alg, handshake->xxdh_psa_privkey,
1502 handshake->xxdh_psa_peerkey, handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 shared_secret, shared_secret_len, &shared_secret_len);
1504 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001505 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001507 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001508 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001509
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001510 status = psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001512 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001514 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001515 }
1516
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001517 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001518#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 } else {
1520 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1521 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001522 }
1523 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001524#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001525
1526 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001527 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001528 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001529 ret = mbedtls_ssl_tls13_evolve_secret(
1530 hash_alg, handshake->tls13_master_secrets.early,
1531 shared_secret, shared_secret_len,
1532 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if (ret != 0) {
1534 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001535 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001536 }
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1539 handshake->tls13_master_secrets.handshake,
1540 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001541
Ronald Cron3b056202022-10-05 17:20:21 +02001542cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 if (shared_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001544 mbedtls_zeroize_and_free(shared_secret, shared_secret_len);
Ronald Cron3b056202022-10-05 17:20:21 +02001545 }
1546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001548}
1549
Yanray Wang05402112022-12-13 18:50:42 +08001550/**
1551 * \brief Compute TLS 1.3 application traffic keys.
1552 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001553 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001554 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001555 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001556 *
1557 * \param ssl The SSL context to operate on. This must be in
1558 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001559 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001560 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001561 * keys. This must be writable but may be uninitialized.
1562 *
1563 * \returns \c 0 on success.
1564 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001565 */
Yanray Wang05402112022-12-13 18:50:42 +08001566MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001567static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 mbedtls_ssl_context *ssl,
1569 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001570{
XiaokangQiana7634982021-10-22 06:32:32 +00001571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001572 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001573
1574 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001575 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001576 &ssl->session_negotiate->app_secrets;
1577
1578 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001579 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001580 size_t transcript_len;
1581
1582 /* Variables relating to the hash for the chosen ciphersuite. */
1583 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001584
1585 psa_algorithm_t hash_alg;
1586 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001587
1588 /* Variables relating to the cipher for the chosen ciphersuite. */
Paul Elliott88600212023-11-23 14:24:30 +00001589 size_t key_len = 0, iv_len = 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001592
1593 /* Extract basic information about hash and ciphersuite */
1594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1596 &key_len, &iv_len);
1597 if (ret != 0) {
1598 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001599 goto cleanup;
1600 }
1601
Dave Rodgman2eab4622023-10-05 13:30:37 +01001602 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001603
Dave Rodgman2eab4622023-10-05 13:30:37 +01001604 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001606
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001607 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001608 * to call this at the right time, that is, after the ServerFinished. */
1609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1611 transcript, sizeof(transcript),
1612 &transcript_len);
1613 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001614 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001616
1617 /* Compute application secrets from master secret and transcript hash. */
1618
Xiaokang Qian123cde82023-03-29 06:54:51 +00001619 ret = mbedtls_ssl_tls13_derive_application_secrets(
1620 hash_alg, handshake->tls13_master_secrets.app,
1621 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001623 MBEDTLS_SSL_DEBUG_RET(
1624 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001625 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001626 }
1627
1628 /* Derive first epoch of IV + Key for application traffic. */
1629
Xiaokang Qian123cde82023-03-29 06:54:51 +00001630 ret = mbedtls_ssl_tls13_make_traffic_keys(
1631 hash_alg,
1632 app_secrets->client_application_traffic_secret_N,
1633 app_secrets->server_application_traffic_secret_N,
1634 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (ret != 0) {
1636 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001637 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001638 }
1639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1641 app_secrets->client_application_traffic_secret_N,
1642 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1645 app_secrets->server_application_traffic_secret_N,
1646 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001647
XiaokangQianac0385c2021-11-03 06:40:11 +00001648 /*
1649 * Export client/server application traffic secret 0
1650 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001652 ssl->f_export_keys(
1653 ssl->p_export_keys,
1654 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1655 app_secrets->client_application_traffic_secret_N, hash_len,
1656 handshake->randbytes,
1657 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1658 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1659 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001660
Xiaokang Qian123cde82023-03-29 06:54:51 +00001661 ssl->f_export_keys(
1662 ssl->p_export_keys,
1663 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1664 app_secrets->server_application_traffic_secret_N, hash_len,
1665 handshake->randbytes,
1666 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1667 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1668 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001669 }
1670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1672 traffic_keys->client_write_key, key_len);
1673 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1674 traffic_keys->server_write_key, key_len);
1675 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1676 traffic_keys->client_write_iv, iv_len);
1677 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1678 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001683 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1685 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1688 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001689}
1690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001692{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001694 mbedtls_ssl_key_set traffic_keys;
1695 mbedtls_ssl_transform *transform_handshake = NULL;
1696 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1697
1698 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001699 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 if (ret != 0) {
1701 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001702 goto cleanup;
1703 }
1704
1705 /* Next evolution in key schedule: Establish handshake secret and
1706 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001707 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001709 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001711 goto cleanup;
1712 }
1713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1715 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001716 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1717 goto cleanup;
1718 }
1719
Jerry Yuef2b98a2022-05-06 16:40:05 +08001720 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 transform_handshake,
1722 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001723 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 &traffic_keys,
1725 ssl);
1726 if (ret != 0) {
1727 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001728 goto cleanup;
1729 }
1730 handshake->transform_handshake = transform_handshake;
1731
1732cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1734 if (ret != 0) {
1735 mbedtls_free(transform_handshake);
1736 }
Jerry Yue110d252022-05-05 10:19:22 +08001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001739}
1740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001742{
Jerry Yu46bffe02022-09-13 11:25:28 +08001743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001744 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001745 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1746 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001747 size_t transcript_len;
1748
Xiaokang Qian123cde82023-03-29 06:54:51 +00001749 MBEDTLS_SSL_DEBUG_MSG(
1750 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001751
Dave Rodgman2eab4622023-10-05 13:30:37 +01001752 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1755 transcript, sizeof(transcript),
1756 &transcript_len);
1757 if (ret != 0) {
1758 return ret;
1759 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001760
1761 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001762 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 handshake->tls13_master_secrets.app,
1764 transcript, transcript_len,
1765 &ssl->session_negotiate->app_secrets);
1766 if (ret != 0) {
1767 return ret;
1768 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001769
Jerry Yuff226982022-04-16 16:52:57 +08001770 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1772 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001773
Xiaokang Qian123cde82023-03-29 06:54:51 +00001774 MBEDTLS_SSL_DEBUG_BUF(
1775 4, "Resumption master secret",
1776 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001777 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001778
Xiaokang Qian123cde82023-03-29 06:54:51 +00001779 MBEDTLS_SSL_DEBUG_MSG(
1780 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001782}
1783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001785{
1786 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1787 mbedtls_ssl_key_set traffic_keys;
1788 mbedtls_ssl_transform *transform_application = NULL;
1789
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001790 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (ret != 0) {
1792 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001793 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001794 goto cleanup;
1795 }
1796
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001797 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 if (ret != 0) {
1799 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001800 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001801 goto cleanup;
1802 }
1803
1804 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1806 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001807 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1808 goto cleanup;
1809 }
1810
1811 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 transform_application,
1813 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001814 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 &traffic_keys,
1816 ssl);
1817 if (ret != 0) {
1818 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001819 goto cleanup;
1820 }
1821
1822 ssl->transform_application = transform_application;
1823
1824cleanup:
1825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1827 if (ret != 0) {
1828 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001829 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001831}
1832
Ronald Cron41a443a2022-10-04 16:38:25 +02001833#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001834int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1835 unsigned char **psk,
1836 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001837{
Jerry Yu40f37712022-07-26 16:58:57 +08001838#if defined(MBEDTLS_USE_PSA_CRYPTO)
1839 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001840 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001841
1842 *psk_len = 0;
1843 *psk = NULL;
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1846 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001847 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001848
1849 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1850 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001851 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 }
1853
1854 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1855 *psk = mbedtls_calloc(1, *psk_len);
1856 if (*psk == NULL) {
1857 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1858 }
1859
1860 status = psa_export_key(ssl->handshake->psk_opaque,
1861 (uint8_t *) *psk, *psk_len, psk_len);
1862 if (status != PSA_SUCCESS) {
1863 mbedtls_free((void *) *psk);
1864 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001865 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 }
1867 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001868#else
1869 *psk = ssl->handshake->psk;
1870 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (*psk == NULL) {
1872 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1873 }
1874 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001875#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001876}
Ronald Cron41a443a2022-10-04 16:38:25 +02001877#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001878
Max Fillinger44042f02024-07-22 14:43:56 +02001879int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
1880 const unsigned char *secret, const size_t secret_len,
1881 const unsigned char *label, const size_t label_len,
1882 const unsigned char *context_value, const size_t context_len,
1883 unsigned char *out, const size_t out_len)
1884{
1885 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
1886 unsigned char hkdf_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Max Fillinger44042f02024-07-22 14:43:56 +02001887 int ret = 0;
Max Fillinger44042f02024-07-22 14:43:56 +02001888
1889 ret = mbedtls_ssl_tls13_derive_secret(hash_alg, secret, secret_len, label, label_len, NULL, 0,
Max Fillinger9359f4d2024-09-21 10:48:57 +02001890 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, hkdf_secret,
1891 hash_len);
Max Fillinger44042f02024-07-22 14:43:56 +02001892 if (ret != 0) {
1893 goto exit;
1894 }
Max Fillinger9359f4d2024-09-21 10:48:57 +02001895 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
1896 hkdf_secret,
1897 hash_len,
Max Fillinger404f7a32024-08-12 11:20:39 +02001898 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter),
Max Fillinger9359f4d2024-09-21 10:48:57 +02001899 context_value,
1900 context_len,
1901 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
1902 out,
1903 out_len);
Max Fillinger44042f02024-07-22 14:43:56 +02001904
1905exit:
1906 mbedtls_platform_zeroize(hkdf_secret, sizeof(hkdf_secret));
1907 return ret;
1908}
1909
Ronald Cron6f135e12021-12-08 16:57:54 +01001910#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */