blob: b92f12e6dfab884f05be537fdfd18375816c1f8f [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"
Jerry Yue3131ef2021-09-16 13:14:15 +080037
Gilles Peskine449bd832023-01-11 14:50:10 +010038#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010039 .name = string,
40
Xiaofei Bai746f9482021-11-12 08:53:56 +000041struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010042{
43 /* This seems to work in C, despite the string literal being one
44 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010045 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010046};
47
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010048#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010049
Hanno Beckerbe9d6642020-08-21 13:20:06 +010050/*
51 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
52 *
53 * The HkdfLabel is specified in RFC 8446 as follows:
54 *
55 * struct HkdfLabel {
56 * uint16 length; // Length of expanded key material
57 * opaque label<7..255>; // Always prefixed by "tls13 "
58 * opaque context<0..255>; // Usually a communication transcript hash
59 * };
60 *
61 * Parameters:
62 * - desired_length: Length of expanded key material
63 * Even though the standard allows expansion to up to
64 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
65 * 255 Bytes, so we require `desired_length` to be at most
66 * 255. This allows us to save a few Bytes of code by
67 * hardcoding the writing of the high bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000068 * - (label, label_len): label + label length, without "tls13 " prefix
69 * The label length MUST be less than or equal to
70 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
71 * It is the caller's responsibility to ensure this.
72 * All (label, label length) pairs used in TLS 1.3
73 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
74 * - (ctx, ctx_len): context + context length
75 * The context length MUST be less than or equal to
76 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
77 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010078 * - dst: Target buffer for HkdfLabel structure,
79 * This MUST be a writable buffer of size
80 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000081 * - dst_len: Pointer at which to store the actual length of
82 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010083 */
84
Xiaofei Baid25fab62021-12-02 06:36:27 +000085static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010086
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
88 (2 /* expansion length */ \
89 + 1 /* label length */ \
90 + label_len \
91 + 1 /* context length */ \
92 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010093
94#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
95 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +010096 sizeof(tls13_label_prefix) + \
97 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
98 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010099
Xiaofei Bai746f9482021-11-12 08:53:56 +0000100static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 size_t desired_length,
102 const unsigned char *label, size_t label_len,
103 const unsigned char *ctx, size_t ctx_len,
104 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100105{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100106 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000107 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100108 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100110
111 unsigned char *p = dst;
112
Hanno Becker531fe302020-09-16 09:45:27 +0100113 /* Add the size of the expanded key material.
114 * We're hardcoding the high byte to 0 here assuming that we never use
115 * TLS 1.3 HKDF key expansion to more than 255 Bytes. */
116#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
Xiaofei Bai746f9482021-11-12 08:53:56 +0000117#error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
Hanno Becker531fe302020-09-16 09:45:27 +0100119#endif
120
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100121 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100123
124 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 *p++ = MBEDTLS_BYTE_0(total_label_len);
126 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000127 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000129 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100130
131 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 *p++ = MBEDTLS_BYTE_0(ctx_len);
133 if (ctx_len != 0) {
134 memcpy(p, ctx, ctx_len);
135 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100136
137 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000138 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100139}
140
Xiaofei Bai746f9482021-11-12 08:53:56 +0000141int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 psa_algorithm_t hash_alg,
143 const unsigned char *secret, size_t secret_len,
144 const unsigned char *label, size_t label_len,
145 const unsigned char *ctx, size_t ctx_len,
146 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100147{
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200149 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
151 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200152 psa_key_derivation_operation_t operation =
153 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100156 /* Should never happen since this is an internal
157 * function, and we know statically which labels
158 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100160 }
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100163 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100165 }
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100168 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100170 }
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (!PSA_ALG_IS_HASH(hash_alg)) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
174 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 ssl_tls13_hkdf_encode_label(buf_len,
177 label, label_len,
178 ctx, ctx_len,
179 hkdf_label,
180 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (status != PSA_SUCCESS) {
185 goto cleanup;
186 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 status = psa_key_derivation_input_bytes(&operation,
189 PSA_KEY_DERIVATION_INPUT_SECRET,
190 secret,
191 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (status != PSA_SUCCESS) {
194 goto cleanup;
195 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 status = psa_key_derivation_input_bytes(&operation,
198 PSA_KEY_DERIVATION_INPUT_INFO,
199 hkdf_label,
200 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (status != PSA_SUCCESS) {
203 goto cleanup;
204 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 status = psa_key_derivation_output_bytes(&operation,
207 buf,
208 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (status != PSA_SUCCESS) {
211 goto cleanup;
212 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200213
214cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 abort_status = psa_key_derivation_abort(&operation);
216 status = (status == PSA_SUCCESS ? abort_status : status);
217 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
218 return psa_ssl_status_to_mbedtls(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100219}
220
Jerry Yua5db6c02022-11-23 18:08:04 +0800221MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800222static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 psa_algorithm_t hash_alg,
224 const unsigned char *secret, size_t secret_len,
225 unsigned char *key, size_t key_len,
226 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800227{
228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
229
Jerry Yuaec08b32022-11-29 15:19:27 +0800230 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 hash_alg,
232 secret, secret_len,
233 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
234 NULL, 0,
235 key, key_len);
236 if (ret != 0) {
237 return ret;
238 }
Jerry Yua8771832022-11-21 23:16:54 +0800239
Jerry Yuaec08b32022-11-29 15:19:27 +0800240 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 hash_alg,
242 secret, secret_len,
243 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
244 NULL, 0,
245 iv, iv_len);
246 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800247}
248
Hanno Becker3385a4d2020-08-21 13:03:34 +0100249/*
250 * The traffic keying material is generated from the following inputs:
251 *
252 * - One secret value per sender.
253 * - A purpose value indicating the specific value being generated
254 * - The desired lengths of key and IV.
255 *
256 * The expansion itself is based on HKDF:
257 *
258 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
259 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
260 *
261 * [sender] denotes the sending side and the Secret value is provided
262 * by the function caller. Note that we generate server and client side
263 * keys in a single function call.
264 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000265int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 psa_algorithm_t hash_alg,
267 const unsigned char *client_secret,
268 const unsigned char *server_secret, size_t secret_len,
269 size_t key_len, size_t iv_len,
270 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100271{
272 int ret = 0;
273
Jerry Yua8771832022-11-21 23:16:54 +0800274 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 hash_alg, client_secret, secret_len,
276 keys->client_write_key, key_len,
277 keys->client_write_iv, iv_len);
278 if (ret != 0) {
279 return ret;
280 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100281
Jerry Yua8771832022-11-21 23:16:54 +0800282 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 hash_alg, server_secret, secret_len,
284 keys->server_write_key, key_len,
285 keys->server_write_iv, iv_len);
286 if (ret != 0) {
287 return ret;
288 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100289
Hanno Becker493ea7f2020-09-08 11:01:00 +0100290 keys->key_len = key_len;
291 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100294}
295
Xiaofei Bai746f9482021-11-12 08:53:56 +0000296int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 psa_algorithm_t hash_alg,
298 const unsigned char *secret, size_t secret_len,
299 const unsigned char *label, size_t label_len,
300 const unsigned char *ctx, size_t ctx_len,
301 int ctx_hashed,
302 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100303{
304 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
306 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100307 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
310 PSA_HASH_LENGTH(hash_alg), &ctx_len);
311 if (status != PSA_SUCCESS) {
312 ret = psa_ssl_status_to_mbedtls(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100313 return ret;
314 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 } else {
316 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100317 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100318 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100319 * Let's double-check nonetheless to not run at the risk
320 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100322 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100325 }
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
328 secret, secret_len,
329 label, label_len,
330 hashed_context, ctx_len,
331 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100332
Hanno Beckerb35d5222020-08-21 13:27:44 +0100333}
334
Xiaofei Bai746f9482021-11-12 08:53:56 +0000335int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 psa_algorithm_t hash_alg,
337 const unsigned char *secret_old,
338 const unsigned char *input, size_t input_len,
339 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100340{
341 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
343 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200344 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
346 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200347 const unsigned char *l_input = NULL;
348 size_t l_input_len;
349
Przemek Stekield5ae3652022-05-13 12:10:08 +0200350 psa_key_derivation_operation_t operation =
351 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (!PSA_ALG_IS_HASH(hash_alg)) {
354 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
355 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100358
359 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100360 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000362 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 hash_alg,
364 secret_old, hlen,
365 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
366 NULL, 0, /* context */
367 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
368 tmp_secret, hlen);
369 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100370 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100372 }
373
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200374 ret = 0;
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200377 l_input = input;
378 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200380 l_input = all_zeroes_input;
381 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100382 }
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 status = psa_key_derivation_setup(&operation,
385 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (status != PSA_SUCCESS) {
388 goto cleanup;
389 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 status = psa_key_derivation_input_bytes(&operation,
392 PSA_KEY_DERIVATION_INPUT_SALT,
393 tmp_secret,
394 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (status != PSA_SUCCESS) {
397 goto cleanup;
398 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 status = psa_key_derivation_input_bytes(&operation,
401 PSA_KEY_DERIVATION_INPUT_SECRET,
402 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (status != PSA_SUCCESS) {
405 goto cleanup;
406 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 status = psa_key_derivation_output_bytes(&operation,
409 secret_new,
410 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (status != PSA_SUCCESS) {
413 goto cleanup;
414 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416cleanup:
417 abort_status = psa_key_derivation_abort(&operation);
418 status = (status == PSA_SUCCESS ? abort_status : status);
419 ret = (ret == 0 ? psa_ssl_status_to_mbedtls(status) : ret);
420 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
421 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100422}
423
Xiaofei Bai746f9482021-11-12 08:53:56 +0000424int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 psa_algorithm_t hash_alg,
426 unsigned char const *early_secret,
427 unsigned char const *transcript, size_t transcript_len,
428 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100429{
430 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100432
433 /* We should never call this function with an unknown hash,
434 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (!PSA_ALG_IS_HASH(hash_alg)) {
436 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
437 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100438
439 /*
440 * 0
441 * |
442 * v
443 * PSK -> HKDF-Extract = Early Secret
444 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100445 * +-----> Derive-Secret(., "c e traffic", ClientHello)
446 * | = client_early_traffic_secret
447 * |
448 * +-----> Derive-Secret(., "e exp master", ClientHello)
449 * | = early_exporter_master_secret
450 * v
451 */
452
453 /* Create client_early_traffic_secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
455 early_secret, hash_len,
456 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
457 transcript, transcript_len,
458 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
459 derived->client_early_traffic_secret,
460 hash_len);
461 if (ret != 0) {
462 return ret;
463 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100464
465 /* Create early exporter */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
467 early_secret, hash_len,
468 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
469 transcript, transcript_len,
470 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
471 derived->early_exporter_master_secret,
472 hash_len);
473 if (ret != 0) {
474 return ret;
475 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100478}
479
Xiaofei Bai746f9482021-11-12 08:53:56 +0000480int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 psa_algorithm_t hash_alg,
482 unsigned char const *handshake_secret,
483 unsigned char const *transcript, size_t transcript_len,
484 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100485{
486 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100488
489 /* We should never call this function with an unknown hash,
490 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (!PSA_ALG_IS_HASH(hash_alg)) {
492 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
493 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100494
495 /*
496 *
497 * Handshake Secret
498 * |
499 * +-----> Derive-Secret( ., "c hs traffic",
500 * | ClientHello...ServerHello )
501 * | = client_handshake_traffic_secret
502 * |
503 * +-----> Derive-Secret( ., "s hs traffic",
504 * | ClientHello...ServerHello )
505 * | = server_handshake_traffic_secret
506 *
507 */
508
509 /*
510 * Compute client_handshake_traffic_secret with
511 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
512 */
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
515 handshake_secret, hash_len,
516 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
517 transcript, transcript_len,
518 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
519 derived->client_handshake_traffic_secret,
520 hash_len);
521 if (ret != 0) {
522 return ret;
523 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100524
525 /*
526 * Compute server_handshake_traffic_secret with
527 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
528 */
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
531 handshake_secret, hash_len,
532 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
533 transcript, transcript_len,
534 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
535 derived->server_handshake_traffic_secret,
536 hash_len);
537 if (ret != 0) {
538 return ret;
539 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100542}
543
Xiaofei Bai746f9482021-11-12 08:53:56 +0000544int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 psa_algorithm_t hash_alg,
546 unsigned char const *application_secret,
547 unsigned char const *transcript, size_t transcript_len,
548 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100549{
550 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100552
553 /* We should never call this function with an unknown hash,
554 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (!PSA_ALG_IS_HASH(hash_alg)) {
556 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
557 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100558
559 /* Generate {client,server}_application_traffic_secret_0
560 *
561 * Master Secret
562 * |
563 * +-----> Derive-Secret( ., "c ap traffic",
564 * | ClientHello...server Finished )
565 * | = client_application_traffic_secret_0
566 * |
567 * +-----> Derive-Secret( ., "s ap traffic",
568 * | ClientHello...Server Finished )
569 * | = server_application_traffic_secret_0
570 * |
571 * +-----> Derive-Secret( ., "exp master",
572 * | ClientHello...server Finished)
573 * | = exporter_master_secret
574 *
575 */
576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
578 application_secret, hash_len,
579 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
580 transcript, transcript_len,
581 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
582 derived->client_application_traffic_secret_N,
583 hash_len);
584 if (ret != 0) {
585 return ret;
586 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
589 application_secret, hash_len,
590 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
591 transcript, transcript_len,
592 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
593 derived->server_application_traffic_secret_N,
594 hash_len);
595 if (ret != 0) {
596 return ret;
597 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
600 application_secret, hash_len,
601 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
602 transcript, transcript_len,
603 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
604 derived->exporter_master_secret,
605 hash_len);
606 if (ret != 0) {
607 return ret;
608 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100611}
612
613/* Generate resumption_master_secret for use with the ticket exchange.
614 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000615 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100616 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000617int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 psa_algorithm_t hash_alg,
619 unsigned char const *application_secret,
620 unsigned char const *transcript, size_t transcript_len,
621 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100622{
623 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100625
626 /* We should never call this function with an unknown hash,
627 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (!PSA_ALG_IS_HASH(hash_alg)) {
629 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
630 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
633 application_secret, hash_len,
634 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
635 transcript, transcript_len,
636 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
637 derived->resumption_master_secret,
638 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (ret != 0) {
641 return ret;
642 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100645}
646
Yanray Wang05402112022-12-13 18:50:42 +0800647/**
648 * \brief Transition into application stage of TLS 1.3 key schedule.
649 *
650 * The TLS 1.3 key schedule can be viewed as a simple state machine
651 * with states Initial -> Early -> Handshake -> Application, and
652 * this function represents the Handshake -> Application transition.
653 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800654 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800655 * can be used to derive the handshake traffic keys.
656 *
657 * \param ssl The SSL context to operate on. This must be in key schedule
658 * stage \c Handshake.
659 *
660 * \returns \c 0 on success.
661 * \returns A negative error code on failure.
662 */
663MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800664static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000665{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000667 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200668 psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000670
671 /*
672 * Compute MasterSecret
673 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
675 handshake->tls13_master_secrets.handshake,
676 NULL, 0,
677 handshake->tls13_master_secrets.app);
678 if (ret != 0) {
679 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
680 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000681 }
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 MBEDTLS_SSL_DEBUG_BUF(4, "Master secret",
684 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000687}
688
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200689MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100690static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
691 unsigned char const *base_key,
692 unsigned char const *transcript,
693 unsigned char *dst,
694 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100695{
Gabor Mezei07732f72022-03-26 17:04:19 +0100696 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
697 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
698 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100700 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100701 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100702 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100703
704 /* We should never call this function with an unknown hash,
705 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (!PSA_ALG_IS_HASH(hash_alg)) {
707 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
708 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100709
710 /* TLS 1.3 Finished message
711 *
712 * struct {
713 * opaque verify_data[Hash.length];
714 * } Finished;
715 *
716 * verify_data =
717 * HMAC( finished_key,
718 * Hash( Handshake Context +
719 * Certificate* +
720 * CertificateVerify* )
721 * )
722 *
723 * finished_key =
724 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
725 */
726
Xiaofei Bai746f9482021-11-12 08:53:56 +0000727 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 hash_alg, base_key, hash_len,
729 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
730 NULL, 0,
731 finished_key, hash_len);
732 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100733 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100734 }
735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 alg = PSA_ALG_HMAC(hash_alg);
737 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
738 psa_set_key_algorithm(&attributes, alg);
739 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
740
741 status = psa_import_key(&attributes, finished_key, hash_len, &key);
742 if (status != PSA_SUCCESS) {
743 ret = psa_ssl_status_to_mbedtls(status);
744 goto exit;
745 }
746
747 status = psa_mac_compute(key, alg, transcript, hash_len,
748 dst, hash_len, dst_len);
749 ret = psa_ssl_status_to_mbedtls(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100750
751exit:
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 status = psa_destroy_key(key);
754 if (ret == 0) {
755 ret = psa_ssl_status_to_mbedtls(status);
756 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100761}
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
764 unsigned char *dst,
765 size_t dst_len,
766 size_t *actual_len,
767 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000768{
XiaokangQiana7634982021-10-22 06:32:32 +0000769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000770
XiaokangQianaaa0e192021-11-10 03:07:04 +0000771 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000772 size_t transcript_len;
773
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800774 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800775 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800776 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800778
779 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100780
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200781 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 ssl->handshake->ciphersuite_info->mac);
783 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800788 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
790 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800791 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800793 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800796 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
797 goto exit;
798 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
801 transcript, sizeof(transcript),
802 &transcript_len);
803 if (ret != 0) {
804 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000805 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000806 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 ret = ssl_tls13_calc_finished_core(hash_alg, base_key, transcript, dst, actual_len);
810 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000811 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
815 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000816
817exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800818 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 mbedtls_platform_zeroize(base_key, base_key_len);
820 mbedtls_platform_zeroize(transcript, sizeof(transcript));
821 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000822}
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
825 const psa_algorithm_t hash_alg,
826 unsigned char const *psk, size_t psk_len,
827 int psk_type,
828 unsigned char const *transcript,
829 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100830{
831 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100832 unsigned char binder_key[PSA_MAC_MAX_SIZE];
833 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100835 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100836
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100837#if !defined(MBEDTLS_DEBUG_C)
838 ssl = NULL; /* make sure we don't use it except for debug */
839 ((void) ssl);
840#endif
841
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100842 /* We should never call this function with an unknown hash,
843 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (!PSA_ALG_IS_HASH(hash_alg)) {
845 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
846 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100847
848 /*
849 * 0
850 * |
851 * v
852 * PSK -> HKDF-Extract = Early Secret
853 * |
854 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
855 * | = binder_key
856 * v
857 */
858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
860 NULL, /* Old secret */
861 psk, psk_len, /* Input */
862 early_secret);
863 if (ret != 0) {
864 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100865 goto exit;
866 }
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
869 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
872 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
873 early_secret, hash_len,
874 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
875 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
876 binder_key, hash_len);
877 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
878 } else {
879 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
880 early_secret, hash_len,
881 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
882 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
883 binder_key, hash_len);
884 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100885 }
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (ret != 0) {
888 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100889 goto exit;
890 }
891
892 /*
893 * The binding_value is computed in the same way as the Finished message
894 * but with the BaseKey being the binder_key.
895 */
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
898 result, &actual_len);
899 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100900 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100904
905exit:
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
908 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
909 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100910}
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
913 int endpoint,
914 int ciphersuite,
915 mbedtls_ssl_key_set const *traffic_keys,
916 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000917{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100918#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000919 int ret;
920 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200921#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000922 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
923 unsigned char const *key_enc;
924 unsigned char const *iv_enc;
925 unsigned char const *key_dec;
926 unsigned char const *iv_dec;
927
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100928#if defined(MBEDTLS_USE_PSA_CRYPTO)
929 psa_key_type_t key_type;
930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
931 psa_algorithm_t alg;
932 size_t key_bits;
933 psa_status_t status = PSA_SUCCESS;
934#endif
935
Hanno Beckerc94060c2021-03-22 07:50:44 +0000936#if !defined(MBEDTLS_DEBUG_C)
937 ssl = NULL; /* make sure we don't use it except for those cases */
938 (void) ssl;
939#endif
940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
942 if (ciphersuite_info == NULL) {
943 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
944 ciphersuite));
945 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100946 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000947
Neil Armstronga8093f52022-05-04 17:44:05 +0200948#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
950 if (cipher_info == NULL) {
951 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
952 ciphersuite_info->cipher));
953 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000954 }
955
956 /*
957 * Setup cipher contexts in target transform
958 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
960 cipher_info)) != 0) {
961 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
962 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000963 }
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
966 cipher_info)) != 0) {
967 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
968 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000969 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100970#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000971
972#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000974 key_enc = traffic_keys->server_write_key;
975 key_dec = traffic_keys->client_write_key;
976 iv_enc = traffic_keys->server_write_iv;
977 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000979#endif /* MBEDTLS_SSL_SRV_C */
980#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000982 key_enc = traffic_keys->client_write_key;
983 key_dec = traffic_keys->server_write_key;
984 iv_enc = traffic_keys->client_write_iv;
985 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000987#endif /* MBEDTLS_SSL_CLI_C */
988 {
989 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000991 }
992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
994 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000995
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100996#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
998 key_enc, cipher_info->key_bitlen,
999 MBEDTLS_ENCRYPT)) != 0) {
1000 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1001 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001002 }
1003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
1005 key_dec, cipher_info->key_bitlen,
1006 MBEDTLS_DECRYPT)) != 0) {
1007 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1008 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001009 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001010#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001011
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001012 /*
1013 * Setup other fields in SSL transform
1014 */
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001017 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001019 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001021
1022 transform->ivlen = traffic_keys->iv_len;
1023 transform->maclen = 0;
1024 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001025 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001026
1027 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001028 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001029 * type-extended and padded plaintext is therefore the padding
1030 * granularity. */
1031 transform->minlen =
1032 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1033
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001034#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001035 /*
1036 * Setup psa keys and alg
1037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
1039 transform->taglen,
1040 &alg,
1041 &key_type,
1042 &key_bits)) != PSA_SUCCESS) {
1043 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls(status));
1044 return psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001045 }
1046
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001047 transform->psa_alg = alg;
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1050 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1051 psa_set_key_algorithm(&attributes, alg);
1052 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((status = psa_import_key(&attributes,
1055 key_enc,
1056 PSA_BITS_TO_BYTES(key_bits),
1057 &transform->psa_key_enc)) != PSA_SUCCESS) {
1058 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", psa_ssl_status_to_mbedtls(status));
1059 return psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001060 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if ((status = psa_import_key(&attributes,
1065 key_dec,
1066 PSA_BITS_TO_BYTES(key_bits),
1067 &transform->psa_key_dec)) != PSA_SUCCESS) {
1068 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", psa_ssl_status_to_mbedtls(status));
1069 return psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001070 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001071 }
1072#endif /* MBEDTLS_USE_PSA_CRYPTO */
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001075}
1076
Jerry Yu84a6eda2022-11-04 11:17:35 +08001077MBEDTLS_CHECK_RETURN_CRITICAL
1078static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1080 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001081{
1082 psa_key_type_t key_type;
1083 psa_algorithm_t alg;
1084 size_t taglen;
1085 size_t key_bits;
1086 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001089 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001091 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, taglen,
1095 &alg, &key_type, &key_bits);
1096 if (status != PSA_SUCCESS) {
1097 return psa_ssl_status_to_mbedtls(status);
1098 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001101
1102 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1103 *iv_len = 12;
1104
1105 return 0;
1106}
1107
Jerry Yu91b560f2022-11-04 14:10:34 +08001108#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001109/*
1110 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001111 * the early application data and handshake messages as described in section 7
1112 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001113 *
Jerry Yue31688b2022-11-22 21:55:56 +08001114 * NOTE: Only one key is generated, the key for the traffic from the client to
1115 * the server. The TLS 1.3 specification does not define a secret and thus
1116 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001117 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001118MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001119static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1120 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001121{
1122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001123 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001124 psa_algorithm_t hash_alg;
1125 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001126 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1127 size_t transcript_len;
Jerry Yuaec08b32022-11-29 15:19:27 +08001128 size_t key_len;
1129 size_t iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001130
1131 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1132 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
1133 mbedtls_ssl_tls13_early_secrets *tls13_early_secrets = &handshake->tls13_early_secrets;
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1138 if (ret != 0) {
1139 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001140 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001141 }
1142
1143 md_type = ciphersuite_info->mac;
1144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
1146 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1149 transcript,
1150 sizeof(transcript),
1151 &transcript_len);
1152 if (ret != 0) {
1153 MBEDTLS_SSL_DEBUG_RET(1,
1154 "mbedtls_ssl_get_handshake_transcript",
1155 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001156 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001157 }
1158
Jerry Yub094e122022-11-21 13:03:47 +08001159 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 hash_alg, handshake->tls13_master_secrets.early,
1161 transcript, transcript_len, tls13_early_secrets);
1162 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001163 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001165 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001166 }
1167
1168 MBEDTLS_SSL_DEBUG_BUF(
1169 4, "Client early traffic secret",
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 tls13_early_secrets->client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001171
1172 /*
1173 * Export client handshake traffic secret
1174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001176 ssl->f_export_keys(
1177 ssl->p_export_keys,
1178 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
1179 tls13_early_secrets->client_early_traffic_secret,
1180 hash_len,
1181 handshake->randbytes,
1182 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001184 }
1185
Jerry Yua8771832022-11-21 23:16:54 +08001186 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 hash_alg,
1188 tls13_early_secrets->client_early_traffic_secret,
1189 hash_len, traffic_keys->client_write_key, key_len,
1190 traffic_keys->client_write_iv, iv_len);
1191 if (ret != 0) {
1192 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001193 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001194 }
Jerry Yua8771832022-11-21 23:16:54 +08001195 traffic_keys->key_len = key_len;
1196 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1199 traffic_keys->client_write_key,
1200 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1203 traffic_keys->client_write_iv,
1204 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001207
Jerry Yu3d78e082022-11-23 18:26:20 +08001208cleanup:
1209 /* Erase secret and transcript */
1210 mbedtls_platform_zeroize(
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
1212 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1213 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001214}
1215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001217{
1218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1219 mbedtls_ssl_key_set traffic_keys;
1220 mbedtls_ssl_transform *transform_earlydata = NULL;
1221 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1222
1223 /* Next evolution in key schedule: Establish early_data secret and
1224 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1226 if (ret != 0) {
1227 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1228 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001229 goto cleanup;
1230 }
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1233 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001234 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1235 goto cleanup;
1236 }
1237
1238 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 transform_earlydata,
1240 ssl->conf->endpoint,
1241 ssl->session_negotiate->ciphersuite,
1242 &traffic_keys,
1243 ssl);
1244 if (ret != 0) {
1245 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001246 goto cleanup;
1247 }
1248 handshake->transform_earlydata = transform_earlydata;
1249
1250cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1252 if (ret != 0) {
1253 mbedtls_free(transform_earlydata);
1254 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001257}
1258#endif /* MBEDTLS_SSL_EARLY_DATA */
1259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001261{
Jerry Yue3131ef2021-09-16 13:14:15 +08001262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001263 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001264 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001265 unsigned char *psk = NULL;
1266 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (handshake->ciphersuite_info == NULL) {
1269 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1270 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001271 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001274#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1276 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1277 if (ret != 0) {
1278 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1279 ret);
1280 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001281 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001282 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001283#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1286 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001287#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001288 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001290#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if (ret != 0) {
1292 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1293 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001294 }
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1297 handshake->tls13_master_secrets.early,
1298 PSA_HASH_LENGTH(hash_alg));
1299 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001300}
1301
Yanray Wang05402112022-12-13 18:50:42 +08001302/**
1303 * \brief Compute TLS 1.3 handshake traffic keys.
1304 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001305 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1306 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001307 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001308 *
1309 * \param ssl The SSL context to operate on. This must be in
1310 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001311 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001312 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001313 * keys. This must be writable but may be uninitialized.
1314 *
1315 * \returns \c 0 on success.
1316 * \returns A negative error code on failure.
1317 */
1318MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001319static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1320 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001321{
1322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001323 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001324 psa_algorithm_t hash_alg;
1325 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001326 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001327 size_t transcript_len;
Jerry Yub094e122022-11-21 13:03:47 +08001328 size_t key_len;
1329 size_t iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001330
Jerry Yu435208a2021-10-13 11:22:16 +08001331 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1332 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001333 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001334
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001335 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1338 if (ret != 0) {
1339 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001340 return ret;
1341 }
1342
Jerry Yu435208a2021-10-13 11:22:16 +08001343 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
1346 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1349 transcript,
1350 sizeof(transcript),
1351 &transcript_len);
1352 if (ret != 0) {
1353 MBEDTLS_SSL_DEBUG_RET(1,
1354 "mbedtls_ssl_get_handshake_transcript",
1355 ret);
1356 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001357 }
1358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 ret = mbedtls_ssl_tls13_derive_handshake_secrets(hash_alg,
1360 handshake->tls13_master_secrets.handshake,
1361 transcript, transcript_len, tls13_hs_secrets);
1362 if (ret != 0) {
1363 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1364 ret);
1365 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001366 }
1367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1369 tls13_hs_secrets->client_handshake_traffic_secret,
1370 hash_len);
1371 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1372 tls13_hs_secrets->server_handshake_traffic_secret,
1373 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001374
1375 /*
1376 * Export client handshake traffic secret
1377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (ssl->f_export_keys != NULL) {
1379 ssl->f_export_keys(ssl->p_export_keys,
1380 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1381 tls13_hs_secrets->client_handshake_traffic_secret,
1382 hash_len,
1383 handshake->randbytes,
1384 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1385 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 ssl->f_export_keys(ssl->p_export_keys,
1388 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1389 tls13_hs_secrets->server_handshake_traffic_secret,
1390 hash_len,
1391 handshake->randbytes,
1392 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1393 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001394 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 ret = mbedtls_ssl_tls13_make_traffic_keys(hash_alg,
1397 tls13_hs_secrets->client_handshake_traffic_secret,
1398 tls13_hs_secrets->server_handshake_traffic_secret,
1399 hash_len, key_len, iv_len, traffic_keys);
1400 if (ret != 0) {
1401 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001402 goto exit;
1403 }
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1406 traffic_keys->client_write_key,
1407 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1410 traffic_keys->server_write_key,
1411 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1414 traffic_keys->client_write_iv,
1415 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1418 traffic_keys->server_write_iv,
1419 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001420
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001421 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001422
1423exit:
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001426}
1427
Yanray Wang05402112022-12-13 18:50:42 +08001428/**
1429 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1430 *
1431 * The TLS 1.3 key schedule can be viewed as a simple state machine
1432 * with states Initial -> Early -> Handshake -> Application, and
1433 * this function represents the Early -> Handshake transition.
1434 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001435 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001436 * can be used to derive the handshake traffic keys.
1437 *
1438 * \param ssl The SSL context to operate on. This must be in key schedule
1439 * stage \c Early.
1440 *
1441 * \returns \c 0 on success.
1442 * \returns A negative error code on failure.
1443 */
1444MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001445static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001446{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001448 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001449 psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001451 unsigned char *shared_secret = NULL;
1452 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001453
Ronald Crona2900bc2022-10-20 14:37:35 +02001454#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001455 /*
1456 * Compute ECDHE secret used to compute the handshake secret from which
1457 * client_handshake_traffic_secret and server_handshake_traffic_secret
1458 * are derived in the handshake secret derivation stage.
1459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
1461 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id)) {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001462#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001464 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001465 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 status = psa_get_key_attributes(handshake->ecdh_psa_privkey,
1468 &key_attributes);
1469 if (status != PSA_SUCCESS) {
1470 ret = psa_ssl_status_to_mbedtls(status);
1471 }
Ronald Cron3b056202022-10-05 17:20:21 +02001472
1473 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 psa_get_key_bits(&key_attributes));
1475 shared_secret = mbedtls_calloc(1, shared_secret_len);
1476 if (shared_secret == NULL) {
1477 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1478 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001479
Ronald Cron4c7edb22022-10-05 15:37:11 +02001480 status = psa_raw_key_agreement(
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1482 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1483 shared_secret, shared_secret_len, &shared_secret_len);
1484 if (status != PSA_SUCCESS) {
1485 ret = psa_ssl_status_to_mbedtls(status);
1486 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001487 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001488 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 status = psa_destroy_key(handshake->ecdh_psa_privkey);
1491 if (status != PSA_SUCCESS) {
1492 ret = psa_ssl_status_to_mbedtls(status);
1493 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001494 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001495 }
1496
1497 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001498#endif /* MBEDTLS_ECDH_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 } else {
1500 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1501 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001502 }
1503 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001504#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001505
1506 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001507 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
1510 handshake->tls13_master_secrets.early,
1511 shared_secret, shared_secret_len,
1512 handshake->tls13_master_secrets.handshake);
1513 if (ret != 0) {
1514 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001515 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001516 }
1517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1519 handshake->tls13_master_secrets.handshake,
1520 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001521
Ronald Cron3b056202022-10-05 17:20:21 +02001522cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (shared_secret != NULL) {
1524 mbedtls_platform_zeroize(shared_secret, shared_secret_len);
1525 mbedtls_free(shared_secret);
Ronald Cron3b056202022-10-05 17:20:21 +02001526 }
1527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001529}
1530
Yanray Wang05402112022-12-13 18:50:42 +08001531/**
1532 * \brief Compute TLS 1.3 application traffic keys.
1533 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001534 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001535 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001536 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001537 *
1538 * \param ssl The SSL context to operate on. This must be in
1539 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001540 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001541 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001542 * keys. This must be writable but may be uninitialized.
1543 *
1544 * \returns \c 0 on success.
1545 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001546 */
Yanray Wang05402112022-12-13 18:50:42 +08001547MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001548static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 mbedtls_ssl_context *ssl,
1550 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001551{
XiaokangQiana7634982021-10-22 06:32:32 +00001552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001553 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001554
1555 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001556 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001557 &ssl->session_negotiate->app_secrets;
1558
1559 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001560 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001561 size_t transcript_len;
1562
1563 /* Variables relating to the hash for the chosen ciphersuite. */
1564 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001565
1566 psa_algorithm_t hash_alg;
1567 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001568
1569 /* Variables relating to the cipher for the chosen ciphersuite. */
Xiaofei Baib7972842021-11-18 07:29:56 +00001570 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001573
1574 /* Extract basic information about hash and ciphersuite */
1575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1577 &key_len, &iv_len);
1578 if (ret != 0) {
1579 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001580 goto cleanup;
1581 }
1582
XiaokangQian33062842021-11-11 03:37:45 +00001583 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
1586 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001587
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001588 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001589 * to call this at the right time, that is, after the ServerFinished. */
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1592 transcript, sizeof(transcript),
1593 &transcript_len);
1594 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001595 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001597
1598 /* Compute application secrets from master secret and transcript hash. */
1599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 ret = mbedtls_ssl_tls13_derive_application_secrets(hash_alg,
1601 handshake->tls13_master_secrets.app,
1602 transcript, transcript_len,
1603 app_secrets);
1604 if (ret != 0) {
1605 MBEDTLS_SSL_DEBUG_RET(1,
1606 "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001607 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001608 }
1609
1610 /* Derive first epoch of IV + Key for application traffic. */
1611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 ret = mbedtls_ssl_tls13_make_traffic_keys(hash_alg,
1613 app_secrets->client_application_traffic_secret_N,
1614 app_secrets->server_application_traffic_secret_N,
1615 hash_len, key_len, iv_len, traffic_keys);
1616 if (ret != 0) {
1617 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001618 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001619 }
1620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1622 app_secrets->client_application_traffic_secret_N,
1623 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1626 app_secrets->server_application_traffic_secret_N,
1627 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001628
XiaokangQianac0385c2021-11-03 06:40:11 +00001629 /*
1630 * Export client/server application traffic secret 0
1631 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (ssl->f_export_keys != NULL) {
1633 ssl->f_export_keys(ssl->p_export_keys,
1634 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1635 app_secrets->client_application_traffic_secret_N, hash_len,
1636 handshake->randbytes,
1637 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1638 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1639 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 ssl->f_export_keys(ssl->p_export_keys,
1642 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1643 app_secrets->server_application_traffic_secret_N, hash_len,
1644 handshake->randbytes,
1645 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1646 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1647 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001648 }
1649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1651 traffic_keys->client_write_key, key_len);
1652 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1653 traffic_keys->server_write_key, key_len);
1654 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1655 traffic_keys->client_write_iv, iv_len);
1656 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1657 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001662 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1664 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1667 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001668}
1669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001671{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001673 mbedtls_ssl_key_set traffic_keys;
1674 mbedtls_ssl_transform *transform_handshake = NULL;
1675 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1676
1677 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001678 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 if (ret != 0) {
1680 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001681 goto cleanup;
1682 }
1683
1684 /* Next evolution in key schedule: Establish handshake secret and
1685 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001686 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001688 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001690 goto cleanup;
1691 }
1692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1694 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001695 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1696 goto cleanup;
1697 }
1698
Jerry Yuef2b98a2022-05-06 16:40:05 +08001699 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 transform_handshake,
1701 ssl->conf->endpoint,
1702 ssl->session_negotiate->ciphersuite,
1703 &traffic_keys,
1704 ssl);
1705 if (ret != 0) {
1706 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001707 goto cleanup;
1708 }
1709 handshake->transform_handshake = transform_handshake;
1710
1711cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1713 if (ret != 0) {
1714 mbedtls_free(transform_handshake);
1715 }
Jerry Yue110d252022-05-05 10:19:22 +08001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001718}
1719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001721{
Jerry Yu46bffe02022-09-13 11:25:28 +08001722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001723 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001724 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1725 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001726 size_t transcript_len;
1727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 MBEDTLS_SSL_DEBUG_MSG(2,
1729 ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001730
Jerry Yu46bffe02022-09-13 11:25:28 +08001731 md_type = handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1734 transcript, sizeof(transcript),
1735 &transcript_len);
1736 if (ret != 0) {
1737 return ret;
1738 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001739
1740 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 mbedtls_psa_translate_md(md_type),
1742 handshake->tls13_master_secrets.app,
1743 transcript, transcript_len,
1744 &ssl->session_negotiate->app_secrets);
1745 if (ret != 0) {
1746 return ret;
1747 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001748
Jerry Yuff226982022-04-16 16:52:57 +08001749 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1751 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 MBEDTLS_SSL_DEBUG_BUF(4, "Resumption master secret",
1754 ssl->session_negotiate->app_secrets.resumption_master_secret,
1755 PSA_HASH_LENGTH(mbedtls_psa_translate_md(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 MBEDTLS_SSL_DEBUG_MSG(2,
1758 ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
1759 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001760}
1761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001763{
1764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1765 mbedtls_ssl_key_set traffic_keys;
1766 mbedtls_ssl_transform *transform_application = NULL;
1767
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001768 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (ret != 0) {
1770 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001771 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001772 goto cleanup;
1773 }
1774
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001775 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (ret != 0) {
1777 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001778 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001779 goto cleanup;
1780 }
1781
1782 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1784 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001785 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1786 goto cleanup;
1787 }
1788
1789 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 transform_application,
1791 ssl->conf->endpoint,
1792 ssl->session_negotiate->ciphersuite,
1793 &traffic_keys,
1794 ssl);
1795 if (ret != 0) {
1796 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001797 goto cleanup;
1798 }
1799
1800 ssl->transform_application = transform_application;
1801
1802cleanup:
1803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1805 if (ret != 0) {
1806 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001807 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001809}
1810
Ronald Cron41a443a2022-10-04 16:38:25 +02001811#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001812int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1813 unsigned char **psk,
1814 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001815{
Jerry Yu40f37712022-07-26 16:58:57 +08001816#if defined(MBEDTLS_USE_PSA_CRYPTO)
1817 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001818 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001819
1820 *psk_len = 0;
1821 *psk = NULL;
1822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1824 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001825 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001826
1827 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1828 if (status != PSA_SUCCESS) {
1829 return psa_ssl_status_to_mbedtls(status);
1830 }
1831
1832 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1833 *psk = mbedtls_calloc(1, *psk_len);
1834 if (*psk == NULL) {
1835 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1836 }
1837
1838 status = psa_export_key(ssl->handshake->psk_opaque,
1839 (uint8_t *) *psk, *psk_len, psk_len);
1840 if (status != PSA_SUCCESS) {
1841 mbedtls_free((void *) *psk);
1842 *psk = NULL;
1843 return psa_ssl_status_to_mbedtls(status);
1844 }
1845 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001846#else
1847 *psk = ssl->handshake->psk;
1848 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (*psk == NULL) {
1850 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1851 }
1852 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001853#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001854}
Ronald Cron41a443a2022-10-04 16:38:25 +02001855#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001856
Ronald Cron6f135e12021-12-08 16:57:54 +01001857#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */