blob: e8b009f48bfaa3f098a12139725c7fcd11ca10ac [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 ( the "License" ); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Hanno Becker58c5cea2020-09-08 10:31:33 +010020#include "common.h"
Hanno Beckerbe9d6642020-08-21 13:20:06 +010021
Ronald Cron6f135e12021-12-08 16:57:54 +010022#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010023
Hanno Beckerbe9d6642020-08-21 13:20:06 +010024#include <stdint.h>
25#include <string.h>
26
Jerry Yue3131ef2021-09-16 13:14:15 +080027#include "mbedtls/hkdf.h"
28#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue110d252022-05-05 10:19:22 +080030#include "mbedtls/platform.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080031
32#include "ssl_misc.h"
33#include "ssl_tls13_keys.h"
Gabor Mezeia3eecd22022-02-09 16:57:26 +010034#include "ssl_tls13_invasive.h"
35
36#include "psa/crypto.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020037#include "md_psa.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080038
Andrzej Kurek00644842023-05-30 05:45:00 -040039/* Define a local translating function to save code size by not using too many
40 * arguments in each translating place. */
41static int local_err_translation(psa_status_t status)
42{
43 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040044 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040045 psa_generic_status_to_mbedtls);
46}
47#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050048
Gilles Peskine449bd832023-01-11 14:50:10 +010049#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010050 .name = string,
51
Xiaofei Bai746f9482021-11-12 08:53:56 +000052struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010053{
54 /* This seems to work in C, despite the string literal being one
55 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010056 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010057};
58
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010059#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010060
Hanno Beckerbe9d6642020-08-21 13:20:06 +010061/*
62 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
63 *
64 * The HkdfLabel is specified in RFC 8446 as follows:
65 *
66 * struct HkdfLabel {
67 * uint16 length; // Length of expanded key material
68 * opaque label<7..255>; // Always prefixed by "tls13 "
69 * opaque context<0..255>; // Usually a communication transcript hash
70 * };
71 *
72 * Parameters:
73 * - desired_length: Length of expanded key material
74 * Even though the standard allows expansion to up to
75 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
76 * 255 Bytes, so we require `desired_length` to be at most
77 * 255. This allows us to save a few Bytes of code by
78 * hardcoding the writing of the high bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000079 * - (label, label_len): label + label length, without "tls13 " prefix
80 * The label length MUST be less than or equal to
81 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
82 * It is the caller's responsibility to ensure this.
83 * All (label, label length) pairs used in TLS 1.3
84 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
85 * - (ctx, ctx_len): context + context length
86 * The context length MUST be less than or equal to
87 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
88 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010089 * - dst: Target buffer for HkdfLabel structure,
90 * This MUST be a writable buffer of size
91 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000092 * - dst_len: Pointer at which to store the actual length of
93 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010094 */
95
Xiaofei Baid25fab62021-12-02 06:36:27 +000096static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010097
Gilles Peskine449bd832023-01-11 14:50:10 +010098#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
99 (2 /* expansion length */ \
100 + 1 /* label length */ \
101 + label_len \
102 + 1 /* context length */ \
103 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +0100104
105#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
106 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 sizeof(tls13_label_prefix) + \
108 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
109 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100110
Xiaofei Bai746f9482021-11-12 08:53:56 +0000111static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 size_t desired_length,
113 const unsigned char *label, size_t label_len,
114 const unsigned char *ctx, size_t ctx_len,
115 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100116{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100117 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000118 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100119 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100121
122 unsigned char *p = dst;
123
Hanno Becker531fe302020-09-16 09:45:27 +0100124 /* Add the size of the expanded key material.
125 * We're hardcoding the high byte to 0 here assuming that we never use
126 * TLS 1.3 HKDF key expansion to more than 255 Bytes. */
127#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
Xiaofei Bai746f9482021-11-12 08:53:56 +0000128#error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
Hanno Becker531fe302020-09-16 09:45:27 +0100130#endif
131
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100132 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100134
135 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 *p++ = MBEDTLS_BYTE_0(total_label_len);
137 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000138 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000140 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100141
142 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 *p++ = MBEDTLS_BYTE_0(ctx_len);
144 if (ctx_len != 0) {
145 memcpy(p, ctx, ctx_len);
146 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100147
148 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000149 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100150}
151
Xiaofei Bai746f9482021-11-12 08:53:56 +0000152int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 psa_algorithm_t hash_alg,
154 const unsigned char *secret, size_t secret_len,
155 const unsigned char *label, size_t label_len,
156 const unsigned char *ctx, size_t ctx_len,
157 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100158{
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200160 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200161 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
162 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200163 psa_key_derivation_operation_t operation =
164 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100167 /* Should never happen since this is an internal
168 * function, and we know statically which labels
169 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100171 }
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100174 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100176 }
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100179 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100181 }
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (!PSA_ALG_IS_HASH(hash_alg)) {
184 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
185 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 ssl_tls13_hkdf_encode_label(buf_len,
188 label, label_len,
189 ctx, ctx_len,
190 hkdf_label,
191 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (status != PSA_SUCCESS) {
196 goto cleanup;
197 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 status = psa_key_derivation_input_bytes(&operation,
200 PSA_KEY_DERIVATION_INPUT_SECRET,
201 secret,
202 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (status != PSA_SUCCESS) {
205 goto cleanup;
206 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 status = psa_key_derivation_input_bytes(&operation,
209 PSA_KEY_DERIVATION_INPUT_INFO,
210 hkdf_label,
211 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if (status != PSA_SUCCESS) {
214 goto cleanup;
215 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 status = psa_key_derivation_output_bytes(&operation,
218 buf,
219 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (status != PSA_SUCCESS) {
222 goto cleanup;
223 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200224
225cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 abort_status = psa_key_derivation_abort(&operation);
227 status = (status == PSA_SUCCESS ? abort_status : status);
228 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500229 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100230}
231
Jerry Yua5db6c02022-11-23 18:08:04 +0800232MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800233static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 psa_algorithm_t hash_alg,
235 const unsigned char *secret, size_t secret_len,
236 unsigned char *key, size_t key_len,
237 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800238{
239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
240
Jerry Yuaec08b32022-11-29 15:19:27 +0800241 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 hash_alg,
243 secret, secret_len,
244 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
245 NULL, 0,
246 key, key_len);
247 if (ret != 0) {
248 return ret;
249 }
Jerry Yua8771832022-11-21 23:16:54 +0800250
Jerry Yuaec08b32022-11-29 15:19:27 +0800251 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 hash_alg,
253 secret, secret_len,
254 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
255 NULL, 0,
256 iv, iv_len);
257 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800258}
259
Hanno Becker3385a4d2020-08-21 13:03:34 +0100260/*
261 * The traffic keying material is generated from the following inputs:
262 *
263 * - One secret value per sender.
264 * - A purpose value indicating the specific value being generated
265 * - The desired lengths of key and IV.
266 *
267 * The expansion itself is based on HKDF:
268 *
269 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
270 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
271 *
272 * [sender] denotes the sending side and the Secret value is provided
273 * by the function caller. Note that we generate server and client side
274 * keys in a single function call.
275 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000276int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 psa_algorithm_t hash_alg,
278 const unsigned char *client_secret,
279 const unsigned char *server_secret, size_t secret_len,
280 size_t key_len, size_t iv_len,
281 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100282{
283 int ret = 0;
284
Jerry Yua8771832022-11-21 23:16:54 +0800285 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 hash_alg, client_secret, secret_len,
287 keys->client_write_key, key_len,
288 keys->client_write_iv, iv_len);
289 if (ret != 0) {
290 return ret;
291 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100292
Jerry Yua8771832022-11-21 23:16:54 +0800293 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 hash_alg, server_secret, secret_len,
295 keys->server_write_key, key_len,
296 keys->server_write_iv, iv_len);
297 if (ret != 0) {
298 return ret;
299 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100300
Hanno Becker493ea7f2020-09-08 11:01:00 +0100301 keys->key_len = key_len;
302 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100305}
306
Xiaofei Bai746f9482021-11-12 08:53:56 +0000307int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 psa_algorithm_t hash_alg,
309 const unsigned char *secret, size_t secret_len,
310 const unsigned char *label, size_t label_len,
311 const unsigned char *ctx, size_t ctx_len,
312 int ctx_hashed,
313 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100314{
315 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
317 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100318 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
321 PSA_HASH_LENGTH(hash_alg), &ctx_len);
322 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500323 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100324 return ret;
325 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 } else {
327 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100328 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100329 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100330 * Let's double-check nonetheless to not run at the risk
331 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100333 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100336 }
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
339 secret, secret_len,
340 label, label_len,
341 hashed_context, ctx_len,
342 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100343
Hanno Beckerb35d5222020-08-21 13:27:44 +0100344}
345
Xiaofei Bai746f9482021-11-12 08:53:56 +0000346int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 psa_algorithm_t hash_alg,
348 const unsigned char *secret_old,
349 const unsigned char *input, size_t input_len,
350 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100351{
352 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
354 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200355 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
357 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200358 const unsigned char *l_input = NULL;
359 size_t l_input_len;
360
Przemek Stekield5ae3652022-05-13 12:10:08 +0200361 psa_key_derivation_operation_t operation =
362 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (!PSA_ALG_IS_HASH(hash_alg)) {
365 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
366 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100369
370 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100371 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000373 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 hash_alg,
375 secret_old, hlen,
376 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
377 NULL, 0, /* context */
378 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
379 tmp_secret, hlen);
380 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100381 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100383 }
384
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200385 ret = 0;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200388 l_input = input;
389 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200391 l_input = all_zeroes_input;
392 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 status = psa_key_derivation_setup(&operation,
396 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (status != PSA_SUCCESS) {
399 goto cleanup;
400 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 status = psa_key_derivation_input_bytes(&operation,
403 PSA_KEY_DERIVATION_INPUT_SALT,
404 tmp_secret,
405 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (status != PSA_SUCCESS) {
408 goto cleanup;
409 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 status = psa_key_derivation_input_bytes(&operation,
412 PSA_KEY_DERIVATION_INPUT_SECRET,
413 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (status != PSA_SUCCESS) {
416 goto cleanup;
417 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 status = psa_key_derivation_output_bytes(&operation,
420 secret_new,
421 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (status != PSA_SUCCESS) {
424 goto cleanup;
425 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427cleanup:
428 abort_status = psa_key_derivation_abort(&operation);
429 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500430 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
432 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100433}
434
Xiaofei Bai746f9482021-11-12 08:53:56 +0000435int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 psa_algorithm_t hash_alg,
437 unsigned char const *early_secret,
438 unsigned char const *transcript, size_t transcript_len,
439 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100440{
441 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100443
444 /* We should never call this function with an unknown hash,
445 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (!PSA_ALG_IS_HASH(hash_alg)) {
447 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
448 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100449
450 /*
451 * 0
452 * |
453 * v
454 * PSK -> HKDF-Extract = Early Secret
455 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100456 * +-----> Derive-Secret(., "c e traffic", ClientHello)
457 * | = client_early_traffic_secret
458 * |
459 * +-----> Derive-Secret(., "e exp master", ClientHello)
460 * | = early_exporter_master_secret
461 * v
462 */
463
464 /* Create client_early_traffic_secret */
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(c_e_traffic),
469 transcript, transcript_len,
470 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
471 derived->client_early_traffic_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
477 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000478 ret = mbedtls_ssl_tls13_derive_secret(
479 hash_alg,
480 early_secret, hash_len,
481 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
482 transcript, transcript_len,
483 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
484 derived->early_exporter_master_secret,
485 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if (ret != 0) {
487 return ret;
488 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100491}
492
Xiaofei Bai746f9482021-11-12 08:53:56 +0000493int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 psa_algorithm_t hash_alg,
495 unsigned char const *handshake_secret,
496 unsigned char const *transcript, size_t transcript_len,
497 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100498{
499 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100501
502 /* We should never call this function with an unknown hash,
503 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (!PSA_ALG_IS_HASH(hash_alg)) {
505 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
506 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100507
508 /*
509 *
510 * Handshake Secret
511 * |
512 * +-----> Derive-Secret( ., "c hs traffic",
513 * | ClientHello...ServerHello )
514 * | = client_handshake_traffic_secret
515 * |
516 * +-----> Derive-Secret( ., "s hs traffic",
517 * | ClientHello...ServerHello )
518 * | = server_handshake_traffic_secret
519 *
520 */
521
522 /*
523 * Compute client_handshake_traffic_secret with
524 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
525 */
526
Xiaokang Qian123cde82023-03-29 06:54:51 +0000527 ret = mbedtls_ssl_tls13_derive_secret(
528 hash_alg,
529 handshake_secret, hash_len,
530 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
531 transcript, transcript_len,
532 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
533 derived->client_handshake_traffic_secret,
534 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (ret != 0) {
536 return ret;
537 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100538
539 /*
540 * Compute server_handshake_traffic_secret with
541 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
542 */
543
Xiaokang Qian123cde82023-03-29 06:54:51 +0000544 ret = mbedtls_ssl_tls13_derive_secret(
545 hash_alg,
546 handshake_secret, hash_len,
547 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
548 transcript, transcript_len,
549 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
550 derived->server_handshake_traffic_secret,
551 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (ret != 0) {
553 return ret;
554 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100557}
558
Xiaofei Bai746f9482021-11-12 08:53:56 +0000559int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 psa_algorithm_t hash_alg,
561 unsigned char const *application_secret,
562 unsigned char const *transcript, size_t transcript_len,
563 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100564{
565 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100567
568 /* We should never call this function with an unknown hash,
569 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (!PSA_ALG_IS_HASH(hash_alg)) {
571 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
572 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100573
574 /* Generate {client,server}_application_traffic_secret_0
575 *
576 * Master Secret
577 * |
578 * +-----> Derive-Secret( ., "c ap traffic",
579 * | ClientHello...server Finished )
580 * | = client_application_traffic_secret_0
581 * |
582 * +-----> Derive-Secret( ., "s ap traffic",
583 * | ClientHello...Server Finished )
584 * | = server_application_traffic_secret_0
585 * |
586 * +-----> Derive-Secret( ., "exp master",
587 * | ClientHello...server Finished)
588 * | = exporter_master_secret
589 *
590 */
591
Xiaokang Qian123cde82023-03-29 06:54:51 +0000592 ret = mbedtls_ssl_tls13_derive_secret(
593 hash_alg,
594 application_secret, hash_len,
595 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
596 transcript, transcript_len,
597 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
598 derived->client_application_traffic_secret_N,
599 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (ret != 0) {
601 return ret;
602 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100603
Xiaokang Qian123cde82023-03-29 06:54:51 +0000604 ret = mbedtls_ssl_tls13_derive_secret(
605 hash_alg,
606 application_secret, hash_len,
607 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
608 transcript, transcript_len,
609 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
610 derived->server_application_traffic_secret_N,
611 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (ret != 0) {
613 return ret;
614 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100615
Xiaokang Qian123cde82023-03-29 06:54:51 +0000616 ret = mbedtls_ssl_tls13_derive_secret(
617 hash_alg,
618 application_secret, hash_len,
619 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
620 transcript, transcript_len,
621 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
622 derived->exporter_master_secret,
623 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ret != 0) {
625 return ret;
626 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100629}
630
631/* Generate resumption_master_secret for use with the ticket exchange.
632 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000633 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100634 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000635int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 psa_algorithm_t hash_alg,
637 unsigned char const *application_secret,
638 unsigned char const *transcript, size_t transcript_len,
639 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100640{
641 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100643
644 /* We should never call this function with an unknown hash,
645 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (!PSA_ALG_IS_HASH(hash_alg)) {
647 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
648 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100649
Xiaokang Qian123cde82023-03-29 06:54:51 +0000650 ret = mbedtls_ssl_tls13_derive_secret(
651 hash_alg,
652 application_secret, hash_len,
653 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
654 transcript, transcript_len,
655 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
656 derived->resumption_master_secret,
657 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (ret != 0) {
660 return ret;
661 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100664}
665
Yanray Wang05402112022-12-13 18:50:42 +0800666/**
667 * \brief Transition into application stage of TLS 1.3 key schedule.
668 *
669 * The TLS 1.3 key schedule can be viewed as a simple state machine
670 * with states Initial -> Early -> Handshake -> Application, and
671 * this function represents the Handshake -> Application transition.
672 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800673 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800674 * can be used to derive the handshake traffic keys.
675 *
676 * \param ssl The SSL context to operate on. This must be in key schedule
677 * stage \c Handshake.
678 *
679 * \returns \c 0 on success.
680 * \returns A negative error code on failure.
681 */
682MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800683static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000684{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000686 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200687 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000689
690 /*
691 * Compute MasterSecret
692 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000693 ret = mbedtls_ssl_tls13_evolve_secret(
694 hash_alg,
695 handshake->tls13_master_secrets.handshake,
696 NULL, 0,
697 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (ret != 0) {
699 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
700 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000701 }
702
Xiaokang Qian123cde82023-03-29 06:54:51 +0000703 MBEDTLS_SSL_DEBUG_BUF(
704 4, "Master secret",
705 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000708}
709
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200710MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100711static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
712 unsigned char const *base_key,
713 unsigned char const *transcript,
714 unsigned char *dst,
715 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100716{
Gabor Mezei07732f72022-03-26 17:04:19 +0100717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100721 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100722 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100723 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100724
725 /* We should never call this function with an unknown hash,
726 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (!PSA_ALG_IS_HASH(hash_alg)) {
728 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
729 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100730
731 /* TLS 1.3 Finished message
732 *
733 * struct {
734 * opaque verify_data[Hash.length];
735 * } Finished;
736 *
737 * verify_data =
738 * HMAC( finished_key,
739 * Hash( Handshake Context +
740 * Certificate* +
741 * CertificateVerify* )
742 * )
743 *
744 * finished_key =
745 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
746 */
747
Xiaofei Bai746f9482021-11-12 08:53:56 +0000748 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 hash_alg, base_key, hash_len,
750 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
751 NULL, 0,
752 finished_key, hash_len);
753 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100754 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100755 }
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 alg = PSA_ALG_HMAC(hash_alg);
758 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
759 psa_set_key_algorithm(&attributes, alg);
760 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
761
762 status = psa_import_key(&attributes, finished_key, hash_len, &key);
763 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500764 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 goto exit;
766 }
767
768 status = psa_mac_compute(key, alg, transcript, hash_len,
769 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500770 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100771
772exit:
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 status = psa_destroy_key(key);
775 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500776 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100782}
783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
785 unsigned char *dst,
786 size_t dst_len,
787 size_t *actual_len,
788 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000789{
XiaokangQiana7634982021-10-22 06:32:32 +0000790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000791
XiaokangQianaaa0e192021-11-10 03:07:04 +0000792 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000793 size_t transcript_len;
794
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800795 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800796 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800797 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800799
800 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100801
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200802 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 ssl->handshake->ciphersuite_info->mac);
804 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800809 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
811 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800812 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800814 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800817 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
818 goto exit;
819 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
822 transcript, sizeof(transcript),
823 &transcript_len);
824 if (ret != 0) {
825 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000826 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000827 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000829
Xiaokang Qian123cde82023-03-29 06:54:51 +0000830 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
831 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000833 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
837 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000838
839exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800840 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 mbedtls_platform_zeroize(base_key, base_key_len);
842 mbedtls_platform_zeroize(transcript, sizeof(transcript));
843 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000844}
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
847 const psa_algorithm_t hash_alg,
848 unsigned char const *psk, size_t psk_len,
849 int psk_type,
850 unsigned char const *transcript,
851 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100852{
853 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100854 unsigned char binder_key[PSA_MAC_MAX_SIZE];
855 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100857 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100858
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100859#if !defined(MBEDTLS_DEBUG_C)
860 ssl = NULL; /* make sure we don't use it except for debug */
861 ((void) ssl);
862#endif
863
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100864 /* We should never call this function with an unknown hash,
865 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (!PSA_ALG_IS_HASH(hash_alg)) {
867 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
868 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100869
870 /*
871 * 0
872 * |
873 * v
874 * PSK -> HKDF-Extract = Early Secret
875 * |
876 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
877 * | = binder_key
878 * v
879 */
880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
882 NULL, /* Old secret */
883 psk, psk_len, /* Input */
884 early_secret);
885 if (ret != 0) {
886 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100887 goto exit;
888 }
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
891 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000894 ret = mbedtls_ssl_tls13_derive_secret(
895 hash_alg,
896 early_secret, hash_len,
897 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
898 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
899 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
901 } else {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000902 ret = mbedtls_ssl_tls13_derive_secret(
903 hash_alg,
904 early_secret, hash_len,
905 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
906 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
907 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100909 }
910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (ret != 0) {
912 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100913 goto exit;
914 }
915
916 /*
917 * The binding_value is computed in the same way as the Finished message
918 * but with the BaseKey being the binder_key.
919 */
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
922 result, &actual_len);
923 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100924 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100928
929exit:
930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
932 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
933 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100934}
935
Xiaokang Qian123cde82023-03-29 06:54:51 +0000936int mbedtls_ssl_tls13_populate_transform(
937 mbedtls_ssl_transform *transform,
938 int endpoint, int ciphersuite,
939 mbedtls_ssl_key_set const *traffic_keys,
940 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000941{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100942#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000943 int ret;
944 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200945#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000946 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
947 unsigned char const *key_enc;
948 unsigned char const *iv_enc;
949 unsigned char const *key_dec;
950 unsigned char const *iv_dec;
951
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100952#if defined(MBEDTLS_USE_PSA_CRYPTO)
953 psa_key_type_t key_type;
954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
955 psa_algorithm_t alg;
956 size_t key_bits;
957 psa_status_t status = PSA_SUCCESS;
958#endif
959
Hanno Beckerc94060c2021-03-22 07:50:44 +0000960#if !defined(MBEDTLS_DEBUG_C)
961 ssl = NULL; /* make sure we don't use it except for those cases */
962 (void) ssl;
963#endif
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
966 if (ciphersuite_info == NULL) {
967 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
968 ciphersuite));
969 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100970 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000971
Neil Armstronga8093f52022-05-04 17:44:05 +0200972#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
974 if (cipher_info == NULL) {
975 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
976 ciphersuite_info->cipher));
977 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000978 }
979
980 /*
981 * Setup cipher contexts in target transform
982 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
984 cipher_info)) != 0) {
985 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
986 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000987 }
988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
990 cipher_info)) != 0) {
991 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
992 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000993 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100994#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000995
996#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000998 key_enc = traffic_keys->server_write_key;
999 key_dec = traffic_keys->client_write_key;
1000 iv_enc = traffic_keys->server_write_iv;
1001 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +00001003#endif /* MBEDTLS_SSL_SRV_C */
1004#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +00001006 key_enc = traffic_keys->client_write_key;
1007 key_dec = traffic_keys->server_write_key;
1008 iv_enc = traffic_keys->client_write_iv;
1009 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +00001011#endif /* MBEDTLS_SSL_CLI_C */
1012 {
1013 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001015 }
1016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
1018 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +00001019
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001020#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
1022 key_enc, cipher_info->key_bitlen,
1023 MBEDTLS_ENCRYPT)) != 0) {
1024 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1025 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001026 }
1027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
1029 key_dec, cipher_info->key_bitlen,
1030 MBEDTLS_DECRYPT)) != 0) {
1031 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1032 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001033 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001034#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001035
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001036 /*
1037 * Setup other fields in SSL transform
1038 */
1039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001041 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001043 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001045
1046 transform->ivlen = traffic_keys->iv_len;
1047 transform->maclen = 0;
1048 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001049 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001050
1051 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001052 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001053 * type-extended and padded plaintext is therefore the padding
1054 * granularity. */
1055 transform->minlen =
1056 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1057
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001058#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001059 /*
1060 * Setup psa keys and alg
1061 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
1063 transform->taglen,
1064 &alg,
1065 &key_type,
1066 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001067 MBEDTLS_SSL_DEBUG_RET(
1068 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001069 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001070 }
1071
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001072 transform->psa_alg = alg;
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1075 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1076 psa_set_key_algorithm(&attributes, alg);
1077 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if ((status = psa_import_key(&attributes,
1080 key_enc,
1081 PSA_BITS_TO_BYTES(key_bits),
1082 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001083 MBEDTLS_SSL_DEBUG_RET(
1084 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001085 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001086 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if ((status = psa_import_key(&attributes,
1091 key_dec,
1092 PSA_BITS_TO_BYTES(key_bits),
1093 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001094 MBEDTLS_SSL_DEBUG_RET(
1095 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001096 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001097 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001098 }
1099#endif /* MBEDTLS_USE_PSA_CRYPTO */
1100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001102}
1103
Jerry Yu84a6eda2022-11-04 11:17:35 +08001104MBEDTLS_CHECK_RETURN_CRITICAL
1105static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1107 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001108{
1109 psa_key_type_t key_type;
1110 psa_algorithm_t alg;
1111 size_t taglen;
1112 size_t key_bits;
1113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001116 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001118 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, taglen,
1122 &alg, &key_type, &key_bits);
1123 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001124 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001128
1129 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1130 *iv_len = 12;
1131
1132 return 0;
1133}
1134
Jerry Yu91b560f2022-11-04 14:10:34 +08001135#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001136/*
1137 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001138 * the early application data and handshake messages as described in section 7
1139 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001140 *
Jerry Yue31688b2022-11-22 21:55:56 +08001141 * NOTE: Only one key is generated, the key for the traffic from the client to
1142 * the server. The TLS 1.3 specification does not define a secret and thus
1143 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001144 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001145MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001146static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1147 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001148{
1149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001150 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001151 psa_algorithm_t hash_alg;
1152 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001153 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1154 size_t transcript_len;
Jerry Yuaec08b32022-11-29 15:19:27 +08001155 size_t key_len;
1156 size_t iv_len;
Yanray Wang16c895d2022-12-15 15:14:35 +08001157 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001158
1159 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001160 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1161 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1166 if (ret != 0) {
1167 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001168 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001169 }
1170
1171 md_type = ciphersuite_info->mac;
1172
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001173 hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1177 transcript,
1178 sizeof(transcript),
1179 &transcript_len);
1180 if (ret != 0) {
1181 MBEDTLS_SSL_DEBUG_RET(1,
1182 "mbedtls_ssl_get_handshake_transcript",
1183 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001184 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001185 }
1186
Jerry Yub094e122022-11-21 13:03:47 +08001187 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001189 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001191 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001193 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001194 }
1195
1196 MBEDTLS_SSL_DEBUG_BUF(
1197 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001198 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001199
1200 /*
1201 * Export client handshake traffic secret
1202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001204 ssl->f_export_keys(
1205 ssl->p_export_keys,
1206 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001207 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001208 hash_len,
1209 handshake->randbytes,
1210 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001212 }
1213
Jerry Yua8771832022-11-21 23:16:54 +08001214 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001216 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 hash_len, traffic_keys->client_write_key, key_len,
1218 traffic_keys->client_write_iv, iv_len);
1219 if (ret != 0) {
1220 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001221 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001222 }
Jerry Yua8771832022-11-21 23:16:54 +08001223 traffic_keys->key_len = key_len;
1224 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1227 traffic_keys->client_write_key,
1228 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1231 traffic_keys->client_write_iv,
1232 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001235
Jerry Yu3d78e082022-11-23 18:26:20 +08001236cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001237 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001238 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001239 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1241 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001242}
1243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001245{
1246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1247 mbedtls_ssl_key_set traffic_keys;
1248 mbedtls_ssl_transform *transform_earlydata = NULL;
1249 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1250
1251 /* Next evolution in key schedule: Establish early_data secret and
1252 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1254 if (ret != 0) {
1255 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1256 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001257 goto cleanup;
1258 }
1259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1261 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001262 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1263 goto cleanup;
1264 }
1265
1266 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 transform_earlydata,
1268 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001269 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 &traffic_keys,
1271 ssl);
1272 if (ret != 0) {
1273 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001274 goto cleanup;
1275 }
1276 handshake->transform_earlydata = transform_earlydata;
1277
1278cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1280 if (ret != 0) {
1281 mbedtls_free(transform_earlydata);
1282 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001285}
1286#endif /* MBEDTLS_SSL_EARLY_DATA */
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001289{
Jerry Yue3131ef2021-09-16 13:14:15 +08001290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001291 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001292 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001293 unsigned char *psk = NULL;
1294 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (handshake->ciphersuite_info == NULL) {
1297 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1298 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001299 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001300
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001301 hash_alg = mbedtls_md_psa_alg_from_type(handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001302#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1304 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1305 if (ret != 0) {
1306 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1307 ret);
1308 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001309 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001310 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001311#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1314 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001315#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001316 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001318#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (ret != 0) {
1320 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1321 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001322 }
1323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1325 handshake->tls13_master_secrets.early,
1326 PSA_HASH_LENGTH(hash_alg));
1327 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001328}
1329
Yanray Wang05402112022-12-13 18:50:42 +08001330/**
1331 * \brief Compute TLS 1.3 handshake traffic keys.
1332 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001333 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1334 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001335 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001336 *
1337 * \param ssl The SSL context to operate on. This must be in
1338 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001339 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001340 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001341 * keys. This must be writable but may be uninitialized.
1342 *
1343 * \returns \c 0 on success.
1344 * \returns A negative error code on failure.
1345 */
1346MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001347static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1348 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001349{
1350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001351 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001352 psa_algorithm_t hash_alg;
1353 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001354 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001355 size_t transcript_len;
Jerry Yub094e122022-11-21 13:03:47 +08001356 size_t key_len;
1357 size_t iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001358
Jerry Yu435208a2021-10-13 11:22:16 +08001359 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001360 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1361 handshake->ciphersuite_info;
1362 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1363 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001364
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001365 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1368 if (ret != 0) {
1369 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001370 return ret;
1371 }
1372
Jerry Yu435208a2021-10-13 11:22:16 +08001373 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001374
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001375 hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1379 transcript,
1380 sizeof(transcript),
1381 &transcript_len);
1382 if (ret != 0) {
1383 MBEDTLS_SSL_DEBUG_RET(1,
1384 "mbedtls_ssl_get_handshake_transcript",
1385 ret);
1386 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001387 }
1388
Xiaokang Qian123cde82023-03-29 06:54:51 +00001389 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1390 hash_alg, handshake->tls13_master_secrets.handshake,
1391 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if (ret != 0) {
1393 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1394 ret);
1395 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001396 }
1397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1399 tls13_hs_secrets->client_handshake_traffic_secret,
1400 hash_len);
1401 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1402 tls13_hs_secrets->server_handshake_traffic_secret,
1403 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001404
1405 /*
1406 * Export client handshake traffic secret
1407 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001409 ssl->f_export_keys(
1410 ssl->p_export_keys,
1411 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1412 tls13_hs_secrets->client_handshake_traffic_secret,
1413 hash_len,
1414 handshake->randbytes,
1415 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1416 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001417
Xiaokang Qian123cde82023-03-29 06:54:51 +00001418 ssl->f_export_keys(
1419 ssl->p_export_keys,
1420 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1421 tls13_hs_secrets->server_handshake_traffic_secret,
1422 hash_len,
1423 handshake->randbytes,
1424 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1425 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001426 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001427
Xiaokang Qian123cde82023-03-29 06:54:51 +00001428 ret = mbedtls_ssl_tls13_make_traffic_keys(
1429 hash_alg,
1430 tls13_hs_secrets->client_handshake_traffic_secret,
1431 tls13_hs_secrets->server_handshake_traffic_secret,
1432 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (ret != 0) {
1434 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001435 goto exit;
1436 }
1437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1439 traffic_keys->client_write_key,
1440 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1443 traffic_keys->server_write_key,
1444 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1447 traffic_keys->client_write_iv,
1448 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1451 traffic_keys->server_write_iv,
1452 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001453
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001454 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001455
1456exit:
1457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001459}
1460
Yanray Wang05402112022-12-13 18:50:42 +08001461/**
1462 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1463 *
1464 * The TLS 1.3 key schedule can be viewed as a simple state machine
1465 * with states Initial -> Early -> Handshake -> Application, and
1466 * this function represents the Early -> Handshake transition.
1467 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001468 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001469 * can be used to derive the handshake traffic keys.
1470 *
1471 * \param ssl The SSL context to operate on. This must be in key schedule
1472 * stage \c Early.
1473 *
1474 * \returns \c 0 on success.
1475 * \returns A negative error code on failure.
1476 */
1477MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001478static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001479{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001481 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001482 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001484 unsigned char *shared_secret = NULL;
1485 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001486
Ronald Crona2900bc2022-10-20 14:37:35 +02001487#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001488 /*
1489 * Compute ECDHE secret used to compute the handshake secret from which
1490 * client_handshake_traffic_secret and server_handshake_traffic_secret
1491 * are derived in the handshake secret derivation stage.
1492 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001494 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
1495 mbedtls_ssl_tls13_named_group_is_dhe(handshake->offered_group_id)) {
1496#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001497 psa_algorithm_t alg =
1498 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1499 PSA_ALG_ECDH : PSA_ALG_FFDH;
1500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001502 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001503 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1504
Przemek Stekiel6f199852023-06-29 08:59:26 +02001505 status = psa_get_key_attributes(handshake->dh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 &key_attributes);
1507 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001508 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 }
Ronald Cron3b056202022-10-05 17:20:21 +02001510
1511 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 psa_get_key_bits(&key_attributes));
1513 shared_secret = mbedtls_calloc(1, shared_secret_len);
1514 if (shared_secret == NULL) {
1515 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1516 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001517
Ronald Cron4c7edb22022-10-05 15:37:11 +02001518 status = psa_raw_key_agreement(
Przemek Stekiel6f199852023-06-29 08:59:26 +02001519 alg, handshake->dh_psa_privkey,
1520 handshake->dh_psa_peerkey, handshake->dh_psa_peerkey_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 shared_secret, shared_secret_len, &shared_secret_len);
1522 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001523 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001525 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001526 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001527
Przemek Stekiel6f199852023-06-29 08:59:26 +02001528 status = psa_destroy_key(handshake->dh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001530 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001532 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001533 }
1534
Przemek Stekiel6f199852023-06-29 08:59:26 +02001535 handshake->dh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001536#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 } else {
1538 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1539 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001540 }
1541 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001542#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001543
1544 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001545 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001546 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001547 ret = mbedtls_ssl_tls13_evolve_secret(
1548 hash_alg, handshake->tls13_master_secrets.early,
1549 shared_secret, shared_secret_len,
1550 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (ret != 0) {
1552 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001553 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001554 }
1555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1557 handshake->tls13_master_secrets.handshake,
1558 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001559
Ronald Cron3b056202022-10-05 17:20:21 +02001560cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if (shared_secret != NULL) {
1562 mbedtls_platform_zeroize(shared_secret, shared_secret_len);
1563 mbedtls_free(shared_secret);
Ronald Cron3b056202022-10-05 17:20:21 +02001564 }
1565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001567}
1568
Yanray Wang05402112022-12-13 18:50:42 +08001569/**
1570 * \brief Compute TLS 1.3 application traffic keys.
1571 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001572 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001573 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001574 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001575 *
1576 * \param ssl The SSL context to operate on. This must be in
1577 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001578 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001579 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001580 * keys. This must be writable but may be uninitialized.
1581 *
1582 * \returns \c 0 on success.
1583 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001584 */
Yanray Wang05402112022-12-13 18:50:42 +08001585MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001586static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 mbedtls_ssl_context *ssl,
1588 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001589{
XiaokangQiana7634982021-10-22 06:32:32 +00001590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001591 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001592
1593 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001594 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001595 &ssl->session_negotiate->app_secrets;
1596
1597 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001598 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001599 size_t transcript_len;
1600
1601 /* Variables relating to the hash for the chosen ciphersuite. */
1602 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001603
1604 psa_algorithm_t hash_alg;
1605 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001606
1607 /* Variables relating to the cipher for the chosen ciphersuite. */
Xiaofei Baib7972842021-11-18 07:29:56 +00001608 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001611
1612 /* Extract basic information about hash and ciphersuite */
1613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1615 &key_len, &iv_len);
1616 if (ret != 0) {
1617 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001618 goto cleanup;
1619 }
1620
XiaokangQian33062842021-11-11 03:37:45 +00001621 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001622
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001623 hash_alg = mbedtls_md_psa_alg_from_type(handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001625
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001626 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001627 * to call this at the right time, that is, after the ServerFinished. */
1628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1630 transcript, sizeof(transcript),
1631 &transcript_len);
1632 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001633 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001635
1636 /* Compute application secrets from master secret and transcript hash. */
1637
Xiaokang Qian123cde82023-03-29 06:54:51 +00001638 ret = mbedtls_ssl_tls13_derive_application_secrets(
1639 hash_alg, handshake->tls13_master_secrets.app,
1640 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001642 MBEDTLS_SSL_DEBUG_RET(
1643 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001644 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001645 }
1646
1647 /* Derive first epoch of IV + Key for application traffic. */
1648
Xiaokang Qian123cde82023-03-29 06:54:51 +00001649 ret = mbedtls_ssl_tls13_make_traffic_keys(
1650 hash_alg,
1651 app_secrets->client_application_traffic_secret_N,
1652 app_secrets->server_application_traffic_secret_N,
1653 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if (ret != 0) {
1655 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001656 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001657 }
1658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1660 app_secrets->client_application_traffic_secret_N,
1661 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1664 app_secrets->server_application_traffic_secret_N,
1665 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001666
XiaokangQianac0385c2021-11-03 06:40:11 +00001667 /*
1668 * Export client/server application traffic secret 0
1669 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001671 ssl->f_export_keys(
1672 ssl->p_export_keys,
1673 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1674 app_secrets->client_application_traffic_secret_N, hash_len,
1675 handshake->randbytes,
1676 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1677 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1678 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001679
Xiaokang Qian123cde82023-03-29 06:54:51 +00001680 ssl->f_export_keys(
1681 ssl->p_export_keys,
1682 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1683 app_secrets->server_application_traffic_secret_N, hash_len,
1684 handshake->randbytes,
1685 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1686 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1687 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001688 }
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1691 traffic_keys->client_write_key, key_len);
1692 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1693 traffic_keys->server_write_key, key_len);
1694 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1695 traffic_keys->client_write_iv, iv_len);
1696 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1697 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001702 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1704 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1707 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001708}
1709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001711{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001713 mbedtls_ssl_key_set traffic_keys;
1714 mbedtls_ssl_transform *transform_handshake = NULL;
1715 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1716
1717 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001718 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if (ret != 0) {
1720 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001721 goto cleanup;
1722 }
1723
1724 /* Next evolution in key schedule: Establish handshake secret and
1725 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001726 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001728 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001730 goto cleanup;
1731 }
1732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1734 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001735 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1736 goto cleanup;
1737 }
1738
Jerry Yuef2b98a2022-05-06 16:40:05 +08001739 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 transform_handshake,
1741 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001742 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 &traffic_keys,
1744 ssl);
1745 if (ret != 0) {
1746 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001747 goto cleanup;
1748 }
1749 handshake->transform_handshake = transform_handshake;
1750
1751cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1753 if (ret != 0) {
1754 mbedtls_free(transform_handshake);
1755 }
Jerry Yue110d252022-05-05 10:19:22 +08001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001758}
1759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001761{
Jerry Yu46bffe02022-09-13 11:25:28 +08001762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001763 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001764 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1765 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001766 size_t transcript_len;
1767
Xiaokang Qian123cde82023-03-29 06:54:51 +00001768 MBEDTLS_SSL_DEBUG_MSG(
1769 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001770
Jerry Yu46bffe02022-09-13 11:25:28 +08001771 md_type = handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1774 transcript, sizeof(transcript),
1775 &transcript_len);
1776 if (ret != 0) {
1777 return ret;
1778 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001779
1780 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001781 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 handshake->tls13_master_secrets.app,
1783 transcript, transcript_len,
1784 &ssl->session_negotiate->app_secrets);
1785 if (ret != 0) {
1786 return ret;
1787 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001788
Jerry Yuff226982022-04-16 16:52:57 +08001789 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1791 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001792
Xiaokang Qian123cde82023-03-29 06:54:51 +00001793 MBEDTLS_SSL_DEBUG_BUF(
1794 4, "Resumption master secret",
1795 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001796 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001797
Xiaokang Qian123cde82023-03-29 06:54:51 +00001798 MBEDTLS_SSL_DEBUG_MSG(
1799 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001801}
1802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001804{
1805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1806 mbedtls_ssl_key_set traffic_keys;
1807 mbedtls_ssl_transform *transform_application = NULL;
1808
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001809 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if (ret != 0) {
1811 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001812 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001813 goto cleanup;
1814 }
1815
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001816 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 if (ret != 0) {
1818 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001819 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001820 goto cleanup;
1821 }
1822
1823 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1825 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001826 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1827 goto cleanup;
1828 }
1829
1830 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 transform_application,
1832 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001833 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 &traffic_keys,
1835 ssl);
1836 if (ret != 0) {
1837 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001838 goto cleanup;
1839 }
1840
1841 ssl->transform_application = transform_application;
1842
1843cleanup:
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1846 if (ret != 0) {
1847 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001848 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001850}
1851
Ronald Cron41a443a2022-10-04 16:38:25 +02001852#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001853int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1854 unsigned char **psk,
1855 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001856{
Jerry Yu40f37712022-07-26 16:58:57 +08001857#if defined(MBEDTLS_USE_PSA_CRYPTO)
1858 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001859 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001860
1861 *psk_len = 0;
1862 *psk = NULL;
1863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1865 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001866 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001867
1868 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1869 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001870 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 }
1872
1873 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1874 *psk = mbedtls_calloc(1, *psk_len);
1875 if (*psk == NULL) {
1876 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1877 }
1878
1879 status = psa_export_key(ssl->handshake->psk_opaque,
1880 (uint8_t *) *psk, *psk_len, psk_len);
1881 if (status != PSA_SUCCESS) {
1882 mbedtls_free((void *) *psk);
1883 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001884 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 }
1886 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001887#else
1888 *psk = ssl->handshake->psk;
1889 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (*psk == NULL) {
1891 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1892 }
1893 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001894#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001895}
Ronald Cron41a443a2022-10-04 16:38:25 +02001896#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001897
Ronald Cron6f135e12021-12-08 16:57:54 +01001898#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */