blob: fd559a7d7fad83b6443695e69488ccabecbfeaba [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.
Max Fillinger5122dc62024-12-02 19:34:40 +010062 * The length field can hold numbers up to 2**16, but HKDF
63 * can only generate outputs of up to 255 * HASH_LEN bytes.
64 * It is the caller's responsibility to ensure that this
65 * limit is not exceeded. In TLS 1.3, SHA256 is the hash
66 * function with the smallest block size, so a length
67 * <= 255 * 32 = 8160 is always safe.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000068 * - (label, label_len): label + label length, without "tls13 " prefix
69 * The label length MUST be less than or equal to
Max Fillinger529931a2024-11-25 20:38:04 +010070 * MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000071 * It is the caller's responsibility to ensure this.
72 * All (label, label length) pairs used in TLS 1.3
73 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
74 * - (ctx, ctx_len): context + context length
75 * The context length MUST be less than or equal to
76 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
77 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010078 * - dst: Target buffer for HkdfLabel structure,
79 * This MUST be a writable buffer of size
80 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000081 * - dst_len: Pointer at which to store the actual length of
82 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010083 */
84
Xiaofei Baid25fab62021-12-02 06:36:27 +000085static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010086
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
88 (2 /* expansion length */ \
89 + 1 /* label length */ \
90 + label_len \
91 + 1 /* context length */ \
92 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010093
94#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
95 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +010096 sizeof(tls13_label_prefix) + \
Max Fillinger529931a2024-11-25 20:38:04 +010097 MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN, \
Gilles Peskine449bd832023-01-11 14:50:10 +010098 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010099
Xiaofei Bai746f9482021-11-12 08:53:56 +0000100static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 size_t desired_length,
102 const unsigned char *label, size_t label_len,
103 const unsigned char *ctx, size_t ctx_len,
104 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100105{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100106 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000107 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100108 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100110
111 unsigned char *p = dst;
112
Max Fillingerffc47e62024-10-29 18:49:30 +0100113 /* Add the size of the expanded key material. */
114#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > UINT16_MAX
115#error "The desired key length must fit into an uint16 but \
116 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN is greater than UINT16_MAX"
Hanno Becker531fe302020-09-16 09:45:27 +0100117#endif
118
Max Fillingerffc47e62024-10-29 18:49:30 +0100119 *p++ = MBEDTLS_BYTE_1(desired_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100121
122 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 *p++ = MBEDTLS_BYTE_0(total_label_len);
124 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000125 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000127 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100128
129 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 *p++ = MBEDTLS_BYTE_0(ctx_len);
131 if (ctx_len != 0) {
132 memcpy(p, ctx, ctx_len);
133 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100134
135 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000136 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100137}
138
Xiaofei Bai746f9482021-11-12 08:53:56 +0000139int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 psa_algorithm_t hash_alg,
141 const unsigned char *secret, size_t secret_len,
142 const unsigned char *label, size_t label_len,
143 const unsigned char *ctx, size_t ctx_len,
144 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100145{
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200147 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200148 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
149 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200150 psa_key_derivation_operation_t operation =
151 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100152
Max Fillinger529931a2024-11-25 20:38:04 +0100153 if (label_len > MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100154 /* Should never happen since this is an internal
155 * function, and we know statically which labels
156 * are allowed. */
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 (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_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 (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100166 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100168 }
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if (!PSA_ALG_IS_HASH(hash_alg)) {
171 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
172 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 ssl_tls13_hkdf_encode_label(buf_len,
175 label, label_len,
176 ctx, ctx_len,
177 hkdf_label,
178 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (status != PSA_SUCCESS) {
183 goto cleanup;
184 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 status = psa_key_derivation_input_bytes(&operation,
187 PSA_KEY_DERIVATION_INPUT_SECRET,
188 secret,
189 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (status != PSA_SUCCESS) {
192 goto cleanup;
193 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 status = psa_key_derivation_input_bytes(&operation,
196 PSA_KEY_DERIVATION_INPUT_INFO,
197 hkdf_label,
198 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (status != PSA_SUCCESS) {
201 goto cleanup;
202 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 status = psa_key_derivation_output_bytes(&operation,
205 buf,
206 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (status != PSA_SUCCESS) {
209 goto cleanup;
210 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200211
212cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 abort_status = psa_key_derivation_abort(&operation);
214 status = (status == PSA_SUCCESS ? abort_status : status);
215 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500216 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100217}
218
Jerry Yua5db6c02022-11-23 18:08:04 +0800219MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800220static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 psa_algorithm_t hash_alg,
222 const unsigned char *secret, size_t secret_len,
223 unsigned char *key, size_t key_len,
224 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800225{
226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
227
Jerry Yuaec08b32022-11-29 15:19:27 +0800228 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 hash_alg,
230 secret, secret_len,
231 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
232 NULL, 0,
233 key, key_len);
234 if (ret != 0) {
235 return ret;
236 }
Jerry Yua8771832022-11-21 23:16:54 +0800237
Jerry Yuaec08b32022-11-29 15:19:27 +0800238 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 hash_alg,
240 secret, secret_len,
241 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
242 NULL, 0,
243 iv, iv_len);
244 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800245}
246
Hanno Becker3385a4d2020-08-21 13:03:34 +0100247/*
248 * The traffic keying material is generated from the following inputs:
249 *
250 * - One secret value per sender.
251 * - A purpose value indicating the specific value being generated
252 * - The desired lengths of key and IV.
253 *
254 * The expansion itself is based on HKDF:
255 *
256 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
257 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
258 *
259 * [sender] denotes the sending side and the Secret value is provided
260 * by the function caller. Note that we generate server and client side
261 * keys in a single function call.
262 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000263int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 psa_algorithm_t hash_alg,
265 const unsigned char *client_secret,
266 const unsigned char *server_secret, size_t secret_len,
267 size_t key_len, size_t iv_len,
268 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100269{
270 int ret = 0;
271
Jerry Yua8771832022-11-21 23:16:54 +0800272 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 hash_alg, client_secret, secret_len,
274 keys->client_write_key, key_len,
275 keys->client_write_iv, iv_len);
276 if (ret != 0) {
277 return ret;
278 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100279
Jerry Yua8771832022-11-21 23:16:54 +0800280 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 hash_alg, server_secret, secret_len,
282 keys->server_write_key, key_len,
283 keys->server_write_iv, iv_len);
284 if (ret != 0) {
285 return ret;
286 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100287
Hanno Becker493ea7f2020-09-08 11:01:00 +0100288 keys->key_len = key_len;
289 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100292}
293
Xiaofei Bai746f9482021-11-12 08:53:56 +0000294int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 psa_algorithm_t hash_alg,
296 const unsigned char *secret, size_t secret_len,
297 const unsigned char *label, size_t label_len,
298 const unsigned char *ctx, size_t ctx_len,
299 int ctx_hashed,
300 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100301{
302 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
304 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100305 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
308 PSA_HASH_LENGTH(hash_alg), &ctx_len);
309 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500310 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100311 return ret;
312 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 } else {
314 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100315 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100316 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100317 * Let's double-check nonetheless to not run at the risk
318 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100320 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100323 }
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
326 secret, secret_len,
327 label, label_len,
328 hashed_context, ctx_len,
329 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100330
Hanno Beckerb35d5222020-08-21 13:27:44 +0100331}
332
Xiaofei Bai746f9482021-11-12 08:53:56 +0000333int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 psa_algorithm_t hash_alg,
335 const unsigned char *secret_old,
336 const unsigned char *input, size_t input_len,
337 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100338{
339 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
341 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200342 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
344 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200345 const unsigned char *l_input = NULL;
346 size_t l_input_len;
347
Przemek Stekield5ae3652022-05-13 12:10:08 +0200348 psa_key_derivation_operation_t operation =
349 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (!PSA_ALG_IS_HASH(hash_alg)) {
352 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
353 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100356
357 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100358 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000360 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 hash_alg,
362 secret_old, hlen,
363 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
364 NULL, 0, /* context */
365 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
366 tmp_secret, hlen);
367 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100368 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100370 }
371
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200372 ret = 0;
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200375 l_input = input;
376 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200378 l_input = all_zeroes_input;
379 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100380 }
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 status = psa_key_derivation_setup(&operation,
383 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (status != PSA_SUCCESS) {
386 goto cleanup;
387 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 status = psa_key_derivation_input_bytes(&operation,
390 PSA_KEY_DERIVATION_INPUT_SALT,
391 tmp_secret,
392 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if (status != PSA_SUCCESS) {
395 goto cleanup;
396 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 status = psa_key_derivation_input_bytes(&operation,
399 PSA_KEY_DERIVATION_INPUT_SECRET,
400 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (status != PSA_SUCCESS) {
403 goto cleanup;
404 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 status = psa_key_derivation_output_bytes(&operation,
407 secret_new,
408 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (status != PSA_SUCCESS) {
411 goto cleanup;
412 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414cleanup:
415 abort_status = psa_key_derivation_abort(&operation);
416 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500417 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
419 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100420}
421
Xiaofei Bai746f9482021-11-12 08:53:56 +0000422int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 psa_algorithm_t hash_alg,
424 unsigned char const *early_secret,
425 unsigned char const *transcript, size_t transcript_len,
426 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100427{
428 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100430
431 /* We should never call this function with an unknown hash,
432 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (!PSA_ALG_IS_HASH(hash_alg)) {
434 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
435 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100436
437 /*
438 * 0
439 * |
440 * v
441 * PSK -> HKDF-Extract = Early Secret
442 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100443 * +-----> Derive-Secret(., "c e traffic", ClientHello)
444 * | = client_early_traffic_secret
445 * |
446 * +-----> Derive-Secret(., "e exp master", ClientHello)
447 * | = early_exporter_master_secret
448 * v
449 */
450
451 /* Create client_early_traffic_secret */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000452 ret = mbedtls_ssl_tls13_derive_secret(
453 hash_alg,
454 early_secret, hash_len,
455 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
456 transcript, transcript_len,
457 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
458 derived->client_early_traffic_secret,
459 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (ret != 0) {
461 return ret;
462 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100463
464 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000465 ret = mbedtls_ssl_tls13_derive_secret(
466 hash_alg,
467 early_secret, hash_len,
468 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
469 transcript, transcript_len,
470 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
471 derived->early_exporter_master_secret,
472 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (ret != 0) {
474 return ret;
475 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100478}
479
Xiaofei Bai746f9482021-11-12 08:53:56 +0000480int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 psa_algorithm_t hash_alg,
482 unsigned char const *handshake_secret,
483 unsigned char const *transcript, size_t transcript_len,
484 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100485{
486 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100488
489 /* We should never call this function with an unknown hash,
490 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (!PSA_ALG_IS_HASH(hash_alg)) {
492 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
493 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100494
495 /*
496 *
497 * Handshake Secret
498 * |
499 * +-----> Derive-Secret( ., "c hs traffic",
500 * | ClientHello...ServerHello )
501 * | = client_handshake_traffic_secret
502 * |
503 * +-----> Derive-Secret( ., "s hs traffic",
504 * | ClientHello...ServerHello )
505 * | = server_handshake_traffic_secret
506 *
507 */
508
509 /*
510 * Compute client_handshake_traffic_secret with
511 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
512 */
513
Xiaokang Qian123cde82023-03-29 06:54:51 +0000514 ret = mbedtls_ssl_tls13_derive_secret(
515 hash_alg,
516 handshake_secret, hash_len,
517 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
518 transcript, transcript_len,
519 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
520 derived->client_handshake_traffic_secret,
521 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (ret != 0) {
523 return ret;
524 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100525
526 /*
527 * Compute server_handshake_traffic_secret with
528 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
529 */
530
Xiaokang Qian123cde82023-03-29 06:54:51 +0000531 ret = mbedtls_ssl_tls13_derive_secret(
532 hash_alg,
533 handshake_secret, hash_len,
534 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
535 transcript, transcript_len,
536 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
537 derived->server_handshake_traffic_secret,
538 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (ret != 0) {
540 return ret;
541 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100544}
545
Xiaofei Bai746f9482021-11-12 08:53:56 +0000546int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 psa_algorithm_t hash_alg,
548 unsigned char const *application_secret,
549 unsigned char const *transcript, size_t transcript_len,
550 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100551{
552 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100554
555 /* We should never call this function with an unknown hash,
556 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (!PSA_ALG_IS_HASH(hash_alg)) {
558 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
559 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100560
561 /* Generate {client,server}_application_traffic_secret_0
562 *
563 * Master Secret
564 * |
565 * +-----> Derive-Secret( ., "c ap traffic",
566 * | ClientHello...server Finished )
567 * | = client_application_traffic_secret_0
568 * |
569 * +-----> Derive-Secret( ., "s ap traffic",
570 * | ClientHello...Server Finished )
571 * | = server_application_traffic_secret_0
572 * |
573 * +-----> Derive-Secret( ., "exp master",
574 * | ClientHello...server Finished)
575 * | = exporter_master_secret
576 *
577 */
578
Xiaokang Qian123cde82023-03-29 06:54:51 +0000579 ret = mbedtls_ssl_tls13_derive_secret(
580 hash_alg,
581 application_secret, hash_len,
582 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
583 transcript, transcript_len,
584 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
585 derived->client_application_traffic_secret_N,
586 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (ret != 0) {
588 return ret;
589 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100590
Xiaokang Qian123cde82023-03-29 06:54:51 +0000591 ret = mbedtls_ssl_tls13_derive_secret(
592 hash_alg,
593 application_secret, hash_len,
594 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
595 transcript, transcript_len,
596 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
597 derived->server_application_traffic_secret_N,
598 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (ret != 0) {
600 return ret;
601 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100602
Xiaokang Qian123cde82023-03-29 06:54:51 +0000603 ret = mbedtls_ssl_tls13_derive_secret(
604 hash_alg,
605 application_secret, hash_len,
606 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
607 transcript, transcript_len,
608 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
609 derived->exporter_master_secret,
610 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (ret != 0) {
612 return ret;
613 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100616}
617
618/* Generate resumption_master_secret for use with the ticket exchange.
619 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000620 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100621 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000622int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 psa_algorithm_t hash_alg,
624 unsigned char const *application_secret,
625 unsigned char const *transcript, size_t transcript_len,
626 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100627{
628 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100630
631 /* We should never call this function with an unknown hash,
632 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (!PSA_ALG_IS_HASH(hash_alg)) {
634 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
635 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100636
Xiaokang Qian123cde82023-03-29 06:54:51 +0000637 ret = mbedtls_ssl_tls13_derive_secret(
638 hash_alg,
639 application_secret, hash_len,
640 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
641 transcript, transcript_len,
642 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
643 derived->resumption_master_secret,
644 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (ret != 0) {
647 return ret;
648 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100651}
652
Yanray Wang05402112022-12-13 18:50:42 +0800653/**
654 * \brief Transition into application stage of TLS 1.3 key schedule.
655 *
656 * The TLS 1.3 key schedule can be viewed as a simple state machine
657 * with states Initial -> Early -> Handshake -> Application, and
658 * this function represents the Handshake -> Application transition.
659 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800660 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800661 * can be used to derive the handshake traffic keys.
662 *
663 * \param ssl The SSL context to operate on. This must be in key schedule
664 * stage \c Handshake.
665 *
666 * \returns \c 0 on success.
667 * \returns A negative error code on failure.
668 */
669MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800670static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000671{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000673 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200674 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100675 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000676
677 /*
678 * Compute MasterSecret
679 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000680 ret = mbedtls_ssl_tls13_evolve_secret(
681 hash_alg,
682 handshake->tls13_master_secrets.handshake,
683 NULL, 0,
684 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (ret != 0) {
686 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
687 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000688 }
689
Xiaokang Qian123cde82023-03-29 06:54:51 +0000690 MBEDTLS_SSL_DEBUG_BUF(
691 4, "Master secret",
692 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000695}
696
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200697MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100698static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
699 unsigned char const *base_key,
700 unsigned char const *transcript,
701 unsigned char *dst,
702 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100703{
Gabor Mezei07732f72022-03-26 17:04:19 +0100704 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100708 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100709 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100710 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100711
712 /* We should never call this function with an unknown hash,
713 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (!PSA_ALG_IS_HASH(hash_alg)) {
715 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
716 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100717
718 /* TLS 1.3 Finished message
719 *
720 * struct {
721 * opaque verify_data[Hash.length];
722 * } Finished;
723 *
724 * verify_data =
725 * HMAC( finished_key,
726 * Hash( Handshake Context +
727 * Certificate* +
728 * CertificateVerify* )
729 * )
730 *
731 * finished_key =
732 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
733 */
734
Xiaofei Bai746f9482021-11-12 08:53:56 +0000735 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 hash_alg, base_key, hash_len,
737 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
738 NULL, 0,
739 finished_key, hash_len);
740 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100741 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100742 }
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 alg = PSA_ALG_HMAC(hash_alg);
745 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
746 psa_set_key_algorithm(&attributes, alg);
747 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
748
749 status = psa_import_key(&attributes, finished_key, hash_len, &key);
750 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500751 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 goto exit;
753 }
754
755 status = psa_mac_compute(key, alg, transcript, hash_len,
756 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500757 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100758
759exit:
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 status = psa_destroy_key(key);
762 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500763 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100769}
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
772 unsigned char *dst,
773 size_t dst_len,
774 size_t *actual_len,
775 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000776{
XiaokangQiana7634982021-10-22 06:32:32 +0000777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000778
XiaokangQianaaa0e192021-11-10 03:07:04 +0000779 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000780 size_t transcript_len;
781
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800782 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800783 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800784 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800786
Dave Rodgman2eab4622023-10-05 13:30:37 +0100787 mbedtls_md_type_t const md_type = (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100788
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200789 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100790 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800796 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
798 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800799 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800801 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800804 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
805 goto exit;
806 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
809 transcript, sizeof(transcript),
810 &transcript_len);
811 if (ret != 0) {
812 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000813 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000814 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000816
Xiaokang Qian123cde82023-03-29 06:54:51 +0000817 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
818 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000820 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
824 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000825
826exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800827 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 mbedtls_platform_zeroize(base_key, base_key_len);
829 mbedtls_platform_zeroize(transcript, sizeof(transcript));
830 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000831}
832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
834 const psa_algorithm_t hash_alg,
835 unsigned char const *psk, size_t psk_len,
836 int psk_type,
837 unsigned char const *transcript,
838 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100839{
840 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100841 unsigned char binder_key[PSA_MAC_MAX_SIZE];
842 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100844 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100845
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100846#if !defined(MBEDTLS_DEBUG_C)
847 ssl = NULL; /* make sure we don't use it except for debug */
848 ((void) ssl);
849#endif
850
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100851 /* We should never call this function with an unknown hash,
852 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (!PSA_ALG_IS_HASH(hash_alg)) {
854 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
855 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100856
857 /*
858 * 0
859 * |
860 * v
861 * PSK -> HKDF-Extract = Early Secret
862 * |
863 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
864 * | = binder_key
865 * v
866 */
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
869 NULL, /* Old secret */
870 psk, psk_len, /* Input */
871 early_secret);
872 if (ret != 0) {
873 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100874 goto exit;
875 }
876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
878 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000881 ret = mbedtls_ssl_tls13_derive_secret(
882 hash_alg,
883 early_secret, hash_len,
884 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
885 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
886 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
888 } else {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000889 ret = mbedtls_ssl_tls13_derive_secret(
890 hash_alg,
891 early_secret, hash_len,
892 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
893 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
894 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100896 }
897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (ret != 0) {
899 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100900 goto exit;
901 }
902
903 /*
904 * The binding_value is computed in the same way as the Finished message
905 * but with the BaseKey being the binder_key.
906 */
907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
909 result, &actual_len);
910 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100911 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100915
916exit:
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
919 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
920 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100921}
922
Xiaokang Qian123cde82023-03-29 06:54:51 +0000923int mbedtls_ssl_tls13_populate_transform(
924 mbedtls_ssl_transform *transform,
925 int endpoint, int ciphersuite,
926 mbedtls_ssl_key_set const *traffic_keys,
927 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000928{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100929#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000930 int ret;
931 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200932#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000933 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
934 unsigned char const *key_enc;
935 unsigned char const *iv_enc;
936 unsigned char const *key_dec;
937 unsigned char const *iv_dec;
938
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100939#if defined(MBEDTLS_USE_PSA_CRYPTO)
940 psa_key_type_t key_type;
941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
942 psa_algorithm_t alg;
943 size_t key_bits;
944 psa_status_t status = PSA_SUCCESS;
945#endif
946
Hanno Beckerc94060c2021-03-22 07:50:44 +0000947#if !defined(MBEDTLS_DEBUG_C)
948 ssl = NULL; /* make sure we don't use it except for those cases */
949 (void) ssl;
950#endif
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
953 if (ciphersuite_info == NULL) {
954 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
955 ciphersuite));
956 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100957 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000958
Neil Armstronga8093f52022-05-04 17:44:05 +0200959#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
961 if (cipher_info == NULL) {
962 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
963 ciphersuite_info->cipher));
964 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000965 }
966
967 /*
968 * Setup cipher contexts in target transform
969 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
971 cipher_info)) != 0) {
972 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
973 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000974 }
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
977 cipher_info)) != 0) {
978 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
979 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000980 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100981#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000982
983#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000985 key_enc = traffic_keys->server_write_key;
986 key_dec = traffic_keys->client_write_key;
987 iv_enc = traffic_keys->server_write_iv;
988 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000990#endif /* MBEDTLS_SSL_SRV_C */
991#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000993 key_enc = traffic_keys->client_write_key;
994 key_dec = traffic_keys->server_write_key;
995 iv_enc = traffic_keys->client_write_iv;
996 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000998#endif /* MBEDTLS_SSL_CLI_C */
999 {
1000 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001002 }
1003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
1005 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +00001006
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001007#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
Mehmet Cagri Aksoy56e90112023-10-11 15:28:06 +02001009 key_enc, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 MBEDTLS_ENCRYPT)) != 0) {
1011 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1012 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001013 }
1014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
Mehmet Cagri Aksoy56e90112023-10-11 15:28:06 +02001016 key_dec, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 MBEDTLS_DECRYPT)) != 0) {
1018 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1019 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001020 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001021#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001022
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001023 /*
1024 * Setup other fields in SSL transform
1025 */
1026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001028 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001030 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001032
1033 transform->ivlen = traffic_keys->iv_len;
1034 transform->maclen = 0;
1035 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001036 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001037
1038 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001039 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001040 * type-extended and padded plaintext is therefore the padding
1041 * granularity. */
1042 transform->minlen =
1043 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1044
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001045#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001046 /*
1047 * Setup psa keys and alg
1048 */
Dave Rodgman2eab4622023-10-05 13:30:37 +01001049 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 transform->taglen,
1051 &alg,
1052 &key_type,
1053 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001054 MBEDTLS_SSL_DEBUG_RET(
1055 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001056 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001057 }
1058
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001059 transform->psa_alg = alg;
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1062 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1063 psa_set_key_algorithm(&attributes, alg);
1064 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if ((status = psa_import_key(&attributes,
1067 key_enc,
1068 PSA_BITS_TO_BYTES(key_bits),
1069 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001070 MBEDTLS_SSL_DEBUG_RET(
1071 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001072 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001073 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if ((status = psa_import_key(&attributes,
1078 key_dec,
1079 PSA_BITS_TO_BYTES(key_bits),
1080 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001081 MBEDTLS_SSL_DEBUG_RET(
1082 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001083 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001084 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001085 }
1086#endif /* MBEDTLS_USE_PSA_CRYPTO */
1087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001089}
1090
Jerry Yu84a6eda2022-11-04 11:17:35 +08001091MBEDTLS_CHECK_RETURN_CRITICAL
1092static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1094 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001095{
1096 psa_key_type_t key_type;
1097 psa_algorithm_t alg;
1098 size_t taglen;
1099 size_t key_bits;
1100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001103 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001105 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001107
Dave Rodgman2eab4622023-10-05 13:30:37 +01001108 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, taglen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 &alg, &key_type, &key_bits);
1110 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001111 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001115
1116 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1117 *iv_len = 12;
1118
1119 return 0;
1120}
1121
Jerry Yu91b560f2022-11-04 14:10:34 +08001122#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001123/*
1124 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001125 * the early application data and handshake messages as described in section 7
1126 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001127 *
Jerry Yue31688b2022-11-22 21:55:56 +08001128 * NOTE: Only one key is generated, the key for the traffic from the client to
1129 * the server. The TLS 1.3 specification does not define a secret and thus
1130 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001131 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001132MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001133static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1134 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001135{
1136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001137 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001138 psa_algorithm_t hash_alg;
1139 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001140 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1141 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001142 size_t key_len = 0;
1143 size_t iv_len = 0;
Yanray Wang16c895d2022-12-15 15:14:35 +08001144 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001145
1146 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001147 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1148 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1153 if (ret != 0) {
1154 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001155 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001156 }
1157
Dave Rodgman2eab4622023-10-05 13:30:37 +01001158 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Jerry Yu91b560f2022-11-04 14:10:34 +08001159
Dave Rodgman2eab4622023-10-05 13:30:37 +01001160 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1164 transcript,
1165 sizeof(transcript),
1166 &transcript_len);
1167 if (ret != 0) {
1168 MBEDTLS_SSL_DEBUG_RET(1,
1169 "mbedtls_ssl_get_handshake_transcript",
1170 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001171 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001172 }
1173
Jerry Yub094e122022-11-21 13:03:47 +08001174 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001176 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001178 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001180 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001181 }
1182
1183 MBEDTLS_SSL_DEBUG_BUF(
1184 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001185 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001186
1187 /*
1188 * Export client handshake traffic secret
1189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001191 ssl->f_export_keys(
1192 ssl->p_export_keys,
1193 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001194 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001195 hash_len,
1196 handshake->randbytes,
1197 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001199 }
1200
Jerry Yua8771832022-11-21 23:16:54 +08001201 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001203 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 hash_len, traffic_keys->client_write_key, key_len,
1205 traffic_keys->client_write_iv, iv_len);
1206 if (ret != 0) {
1207 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001208 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001209 }
Jerry Yua8771832022-11-21 23:16:54 +08001210 traffic_keys->key_len = key_len;
1211 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1214 traffic_keys->client_write_key,
1215 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1218 traffic_keys->client_write_iv,
1219 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001222
Jerry Yu3d78e082022-11-23 18:26:20 +08001223cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001224 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001225 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001226 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1228 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001229}
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001232{
1233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1234 mbedtls_ssl_key_set traffic_keys;
1235 mbedtls_ssl_transform *transform_earlydata = NULL;
1236 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1237
1238 /* Next evolution in key schedule: Establish early_data secret and
1239 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1241 if (ret != 0) {
1242 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1243 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001244 goto cleanup;
1245 }
1246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1248 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001249 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1250 goto cleanup;
1251 }
1252
1253 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 transform_earlydata,
1255 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001256 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 &traffic_keys,
1258 ssl);
1259 if (ret != 0) {
1260 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001261 goto cleanup;
1262 }
1263 handshake->transform_earlydata = transform_earlydata;
1264
1265cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1267 if (ret != 0) {
1268 mbedtls_free(transform_earlydata);
1269 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001272}
1273#endif /* MBEDTLS_SSL_EARLY_DATA */
1274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001276{
Jerry Yue3131ef2021-09-16 13:14:15 +08001277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001278 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001279 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001280 unsigned char *psk = NULL;
1281 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (handshake->ciphersuite_info == NULL) {
1284 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1285 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001286 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001287
Dave Rodgman2eab4622023-10-05 13:30:37 +01001288 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001289#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1291 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1292 if (ret != 0) {
1293 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1294 ret);
1295 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001296 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001297 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001298#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1301 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001302#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001303 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001305#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (ret != 0) {
1307 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1308 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001309 }
1310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1312 handshake->tls13_master_secrets.early,
1313 PSA_HASH_LENGTH(hash_alg));
1314 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001315}
1316
Yanray Wang05402112022-12-13 18:50:42 +08001317/**
1318 * \brief Compute TLS 1.3 handshake traffic keys.
1319 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001320 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1321 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001322 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001323 *
1324 * \param ssl The SSL context to operate on. This must be in
1325 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001326 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001327 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001328 * keys. This must be writable but may be uninitialized.
1329 *
1330 * \returns \c 0 on success.
1331 * \returns A negative error code on failure.
1332 */
1333MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001334static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1335 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001336{
1337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001338 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001339 psa_algorithm_t hash_alg;
1340 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001341 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001342 size_t transcript_len;
Paul Elliott88600212023-11-23 14:24:30 +00001343 size_t key_len = 0;
1344 size_t iv_len = 0;
Jerry Yu61e35e02021-09-16 18:59:08 +08001345
Jerry Yu435208a2021-10-13 11:22:16 +08001346 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001347 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1348 handshake->ciphersuite_info;
1349 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1350 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001351
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001352 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1355 if (ret != 0) {
1356 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001357 return ret;
1358 }
1359
Dave Rodgman2eab4622023-10-05 13:30:37 +01001360 md_type = (mbedtls_md_type_t) ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001361
Dave Rodgman2eab4622023-10-05 13:30:37 +01001362 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1366 transcript,
1367 sizeof(transcript),
1368 &transcript_len);
1369 if (ret != 0) {
1370 MBEDTLS_SSL_DEBUG_RET(1,
1371 "mbedtls_ssl_get_handshake_transcript",
1372 ret);
1373 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001374 }
1375
Xiaokang Qian123cde82023-03-29 06:54:51 +00001376 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1377 hash_alg, handshake->tls13_master_secrets.handshake,
1378 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 if (ret != 0) {
1380 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1381 ret);
1382 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001383 }
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1386 tls13_hs_secrets->client_handshake_traffic_secret,
1387 hash_len);
1388 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1389 tls13_hs_secrets->server_handshake_traffic_secret,
1390 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001391
1392 /*
1393 * Export client handshake traffic secret
1394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001396 ssl->f_export_keys(
1397 ssl->p_export_keys,
1398 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1399 tls13_hs_secrets->client_handshake_traffic_secret,
1400 hash_len,
1401 handshake->randbytes,
1402 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1403 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001404
Xiaokang Qian123cde82023-03-29 06:54:51 +00001405 ssl->f_export_keys(
1406 ssl->p_export_keys,
1407 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1408 tls13_hs_secrets->server_handshake_traffic_secret,
1409 hash_len,
1410 handshake->randbytes,
1411 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1412 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001413 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001414
Xiaokang Qian123cde82023-03-29 06:54:51 +00001415 ret = mbedtls_ssl_tls13_make_traffic_keys(
1416 hash_alg,
1417 tls13_hs_secrets->client_handshake_traffic_secret,
1418 tls13_hs_secrets->server_handshake_traffic_secret,
1419 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 if (ret != 0) {
1421 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001422 goto exit;
1423 }
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1426 traffic_keys->client_write_key,
1427 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1430 traffic_keys->server_write_key,
1431 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1434 traffic_keys->client_write_iv,
1435 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1438 traffic_keys->server_write_iv,
1439 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001440
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001441 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001442
1443exit:
1444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001446}
1447
Yanray Wang05402112022-12-13 18:50:42 +08001448/**
1449 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1450 *
1451 * The TLS 1.3 key schedule can be viewed as a simple state machine
1452 * with states Initial -> Early -> Handshake -> Application, and
1453 * this function represents the Early -> Handshake transition.
1454 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001455 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001456 * can be used to derive the handshake traffic keys.
1457 *
1458 * \param ssl The SSL context to operate on. This must be in key schedule
1459 * stage \c Early.
1460 *
1461 * \returns \c 0 on success.
1462 * \returns A negative error code on failure.
1463 */
1464MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001465static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001466{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001468 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001469 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001470 (mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001471 unsigned char *shared_secret = NULL;
1472 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001473
Ronald Crona2900bc2022-10-20 14:37:35 +02001474#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001475 /*
1476 * Compute ECDHE secret used to compute the handshake secret from which
1477 * client_handshake_traffic_secret and server_handshake_traffic_secret
1478 * are derived in the handshake secret derivation stage.
1479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001481 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001482 mbedtls_ssl_tls13_named_group_is_ffdh(handshake->offered_group_id)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001483#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001484 psa_algorithm_t alg =
1485 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1486 PSA_ALG_ECDH : PSA_ALG_FFDH;
1487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001489 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001490 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1491
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001492 status = psa_get_key_attributes(handshake->xxdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 &key_attributes);
1494 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001495 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 }
Ronald Cron3b056202022-10-05 17:20:21 +02001497
1498 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 psa_get_key_bits(&key_attributes));
1500 shared_secret = mbedtls_calloc(1, shared_secret_len);
1501 if (shared_secret == NULL) {
1502 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1503 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001504
Ronald Cron4c7edb22022-10-05 15:37:11 +02001505 status = psa_raw_key_agreement(
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001506 alg, handshake->xxdh_psa_privkey,
1507 handshake->xxdh_psa_peerkey, handshake->xxdh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 shared_secret, shared_secret_len, &shared_secret_len);
1509 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001510 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001512 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001513 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001514
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001515 status = psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001517 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001519 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001520 }
1521
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001522 handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001523#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 } else {
1525 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1526 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001527 }
1528 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001529#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001530
1531 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001532 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001533 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001534 ret = mbedtls_ssl_tls13_evolve_secret(
1535 hash_alg, handshake->tls13_master_secrets.early,
1536 shared_secret, shared_secret_len,
1537 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (ret != 0) {
1539 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001540 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001541 }
1542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1544 handshake->tls13_master_secrets.handshake,
1545 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001546
Ronald Cron3b056202022-10-05 17:20:21 +02001547cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (shared_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001549 mbedtls_zeroize_and_free(shared_secret, shared_secret_len);
Ronald Cron3b056202022-10-05 17:20:21 +02001550 }
1551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001553}
1554
Yanray Wang05402112022-12-13 18:50:42 +08001555/**
1556 * \brief Compute TLS 1.3 application traffic keys.
1557 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001558 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001559 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001560 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001561 *
1562 * \param ssl The SSL context to operate on. This must be in
1563 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001564 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001565 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001566 * keys. This must be writable but may be uninitialized.
1567 *
1568 * \returns \c 0 on success.
1569 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001570 */
Yanray Wang05402112022-12-13 18:50:42 +08001571MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001572static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 mbedtls_ssl_context *ssl,
1574 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001575{
XiaokangQiana7634982021-10-22 06:32:32 +00001576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001577 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001578
1579 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001580 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001581 &ssl->session_negotiate->app_secrets;
1582
1583 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001584 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001585 size_t transcript_len;
1586
1587 /* Variables relating to the hash for the chosen ciphersuite. */
1588 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001589
1590 psa_algorithm_t hash_alg;
1591 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001592
1593 /* Variables relating to the cipher for the chosen ciphersuite. */
Paul Elliott88600212023-11-23 14:24:30 +00001594 size_t key_len = 0, iv_len = 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001597
1598 /* Extract basic information about hash and ciphersuite */
1599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1601 &key_len, &iv_len);
1602 if (ret != 0) {
1603 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001604 goto cleanup;
1605 }
1606
Dave Rodgman2eab4622023-10-05 13:30:37 +01001607 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001608
Dave Rodgman2eab4622023-10-05 13:30:37 +01001609 hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001611
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001612 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001613 * to call this at the right time, that is, after the ServerFinished. */
1614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1616 transcript, sizeof(transcript),
1617 &transcript_len);
1618 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001619 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001621
1622 /* Compute application secrets from master secret and transcript hash. */
1623
Xiaokang Qian123cde82023-03-29 06:54:51 +00001624 ret = mbedtls_ssl_tls13_derive_application_secrets(
1625 hash_alg, handshake->tls13_master_secrets.app,
1626 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001628 MBEDTLS_SSL_DEBUG_RET(
1629 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001630 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001631 }
1632
1633 /* Derive first epoch of IV + Key for application traffic. */
1634
Xiaokang Qian123cde82023-03-29 06:54:51 +00001635 ret = mbedtls_ssl_tls13_make_traffic_keys(
1636 hash_alg,
1637 app_secrets->client_application_traffic_secret_N,
1638 app_secrets->server_application_traffic_secret_N,
1639 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 if (ret != 0) {
1641 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001642 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001643 }
1644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1646 app_secrets->client_application_traffic_secret_N,
1647 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1650 app_secrets->server_application_traffic_secret_N,
1651 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001652
XiaokangQianac0385c2021-11-03 06:40:11 +00001653 /*
1654 * Export client/server application traffic secret 0
1655 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001657 ssl->f_export_keys(
1658 ssl->p_export_keys,
1659 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1660 app_secrets->client_application_traffic_secret_N, hash_len,
1661 handshake->randbytes,
1662 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1663 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1664 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001665
Xiaokang Qian123cde82023-03-29 06:54:51 +00001666 ssl->f_export_keys(
1667 ssl->p_export_keys,
1668 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1669 app_secrets->server_application_traffic_secret_N, hash_len,
1670 handshake->randbytes,
1671 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1672 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1673 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001674 }
1675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1677 traffic_keys->client_write_key, key_len);
1678 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1679 traffic_keys->server_write_key, key_len);
1680 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1681 traffic_keys->client_write_iv, iv_len);
1682 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1683 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001688 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1690 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1693 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001694}
1695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001697{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001699 mbedtls_ssl_key_set traffic_keys;
1700 mbedtls_ssl_transform *transform_handshake = NULL;
1701 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1702
1703 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001704 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 if (ret != 0) {
1706 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001707 goto cleanup;
1708 }
1709
1710 /* Next evolution in key schedule: Establish handshake secret and
1711 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001712 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001714 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001716 goto cleanup;
1717 }
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1720 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001721 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1722 goto cleanup;
1723 }
1724
Jerry Yuef2b98a2022-05-06 16:40:05 +08001725 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 transform_handshake,
1727 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001728 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 &traffic_keys,
1730 ssl);
1731 if (ret != 0) {
1732 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001733 goto cleanup;
1734 }
1735 handshake->transform_handshake = transform_handshake;
1736
1737cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1739 if (ret != 0) {
1740 mbedtls_free(transform_handshake);
1741 }
Jerry Yue110d252022-05-05 10:19:22 +08001742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001744}
1745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001747{
Jerry Yu46bffe02022-09-13 11:25:28 +08001748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001749 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001750 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1751 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001752 size_t transcript_len;
1753
Xiaokang Qian123cde82023-03-29 06:54:51 +00001754 MBEDTLS_SSL_DEBUG_MSG(
1755 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001756
Dave Rodgman2eab4622023-10-05 13:30:37 +01001757 md_type = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1760 transcript, sizeof(transcript),
1761 &transcript_len);
1762 if (ret != 0) {
1763 return ret;
1764 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001765
1766 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001767 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 handshake->tls13_master_secrets.app,
1769 transcript, transcript_len,
1770 &ssl->session_negotiate->app_secrets);
1771 if (ret != 0) {
1772 return ret;
1773 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001774
Jerry Yuff226982022-04-16 16:52:57 +08001775 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1777 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001778
Xiaokang Qian123cde82023-03-29 06:54:51 +00001779 MBEDTLS_SSL_DEBUG_BUF(
1780 4, "Resumption master secret",
1781 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001782 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001783
Xiaokang Qian123cde82023-03-29 06:54:51 +00001784 MBEDTLS_SSL_DEBUG_MSG(
1785 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001787}
1788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001790{
1791 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1792 mbedtls_ssl_key_set traffic_keys;
1793 mbedtls_ssl_transform *transform_application = NULL;
1794
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001795 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 if (ret != 0) {
1797 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001798 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001799 goto cleanup;
1800 }
1801
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001802 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (ret != 0) {
1804 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001805 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001806 goto cleanup;
1807 }
1808
1809 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1811 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001812 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1813 goto cleanup;
1814 }
1815
1816 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 transform_application,
1818 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001819 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 &traffic_keys,
1821 ssl);
1822 if (ret != 0) {
1823 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001824 goto cleanup;
1825 }
1826
1827 ssl->transform_application = transform_application;
1828
1829cleanup:
1830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1832 if (ret != 0) {
1833 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001834 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001836}
1837
Ronald Cron41a443a2022-10-04 16:38:25 +02001838#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001839int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1840 unsigned char **psk,
1841 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001842{
Jerry Yu40f37712022-07-26 16:58:57 +08001843#if defined(MBEDTLS_USE_PSA_CRYPTO)
1844 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001845 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001846
1847 *psk_len = 0;
1848 *psk = NULL;
1849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1851 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001852 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001853
1854 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1855 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001856 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 }
1858
1859 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1860 *psk = mbedtls_calloc(1, *psk_len);
1861 if (*psk == NULL) {
1862 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1863 }
1864
1865 status = psa_export_key(ssl->handshake->psk_opaque,
1866 (uint8_t *) *psk, *psk_len, psk_len);
1867 if (status != PSA_SUCCESS) {
1868 mbedtls_free((void *) *psk);
1869 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001870 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 }
1872 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001873#else
1874 *psk = ssl->handshake->psk;
1875 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (*psk == NULL) {
1877 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1878 }
1879 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001880#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001881}
Ronald Cron41a443a2022-10-04 16:38:25 +02001882#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001883
Max Fillinger1b0e2e92025-04-16 14:35:24 +02001884#if defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT)
Max Fillinger44042f02024-07-22 14:43:56 +02001885int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
1886 const unsigned char *secret, const size_t secret_len,
1887 const unsigned char *label, const size_t label_len,
1888 const unsigned char *context_value, const size_t context_len,
1889 unsigned char *out, const size_t out_len)
1890{
1891 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
1892 unsigned char hkdf_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Max Fillinger44042f02024-07-22 14:43:56 +02001893 int ret = 0;
Max Fillinger44042f02024-07-22 14:43:56 +02001894
1895 ret = mbedtls_ssl_tls13_derive_secret(hash_alg, secret, secret_len, label, label_len, NULL, 0,
Max Fillinger9359f4d2024-09-21 10:48:57 +02001896 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, hkdf_secret,
1897 hash_len);
Max Fillinger44042f02024-07-22 14:43:56 +02001898 if (ret != 0) {
1899 goto exit;
1900 }
Max Fillinger9359f4d2024-09-21 10:48:57 +02001901 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
1902 hkdf_secret,
1903 hash_len,
Max Fillinger404f7a32024-08-12 11:20:39 +02001904 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter),
Max Fillinger9359f4d2024-09-21 10:48:57 +02001905 context_value,
1906 context_len,
1907 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
1908 out,
1909 out_len);
Max Fillinger44042f02024-07-22 14:43:56 +02001910
1911exit:
1912 mbedtls_platform_zeroize(hkdf_secret, sizeof(hkdf_secret));
1913 return ret;
1914}
Max Fillinger1b0e2e92025-04-16 14:35:24 +02001915#endif /* defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) */
Max Fillinger44042f02024-07-22 14:43:56 +02001916
Ronald Cron6f135e12021-12-08 16:57:54 +01001917#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */