blob: 43452b1a38b656bb4be9bfba168a74e3846d426b [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
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050038#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
39 psa_to_ssl_errors, \
40 psa_generic_status_to_mbedtls)
41
Gilles Peskine449bd832023-01-11 14:50:10 +010042#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010043 .name = string,
44
Xiaofei Bai746f9482021-11-12 08:53:56 +000045struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010046{
47 /* This seems to work in C, despite the string literal being one
48 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010049 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010050};
51
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010052#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010053
Hanno Beckerbe9d6642020-08-21 13:20:06 +010054/*
55 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
56 *
57 * The HkdfLabel is specified in RFC 8446 as follows:
58 *
59 * struct HkdfLabel {
60 * uint16 length; // Length of expanded key material
61 * opaque label<7..255>; // Always prefixed by "tls13 "
62 * opaque context<0..255>; // Usually a communication transcript hash
63 * };
64 *
65 * Parameters:
66 * - desired_length: Length of expanded key material
67 * Even though the standard allows expansion to up to
68 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
69 * 255 Bytes, so we require `desired_length` to be at most
70 * 255. This allows us to save a few Bytes of code by
71 * hardcoding the writing of the high bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000072 * - (label, label_len): label + label length, without "tls13 " prefix
73 * The label length MUST be less than or equal to
74 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
75 * It is the caller's responsibility to ensure this.
76 * All (label, label length) pairs used in TLS 1.3
77 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
78 * - (ctx, ctx_len): context + context length
79 * The context length MUST be less than or equal to
80 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
81 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010082 * - dst: Target buffer for HkdfLabel structure,
83 * This MUST be a writable buffer of size
84 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000085 * - dst_len: Pointer at which to store the actual length of
86 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010087 */
88
Xiaofei Baid25fab62021-12-02 06:36:27 +000089static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010090
Gilles Peskine449bd832023-01-11 14:50:10 +010091#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
92 (2 /* expansion length */ \
93 + 1 /* label length */ \
94 + label_len \
95 + 1 /* context length */ \
96 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010097
98#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
99 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 sizeof(tls13_label_prefix) + \
101 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
102 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100103
Xiaofei Bai746f9482021-11-12 08:53:56 +0000104static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 size_t desired_length,
106 const unsigned char *label, size_t label_len,
107 const unsigned char *ctx, size_t ctx_len,
108 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100109{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100110 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000111 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100112 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100114
115 unsigned char *p = dst;
116
Hanno Becker531fe302020-09-16 09:45:27 +0100117 /* Add the size of the expanded key material.
118 * We're hardcoding the high byte to 0 here assuming that we never use
119 * TLS 1.3 HKDF key expansion to more than 255 Bytes. */
120#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
Xiaofei Bai746f9482021-11-12 08:53:56 +0000121#error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
Hanno Becker531fe302020-09-16 09:45:27 +0100123#endif
124
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100125 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100127
128 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 *p++ = MBEDTLS_BYTE_0(total_label_len);
130 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000131 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000133 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100134
135 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 *p++ = MBEDTLS_BYTE_0(ctx_len);
137 if (ctx_len != 0) {
138 memcpy(p, ctx, ctx_len);
139 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100140
141 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000142 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100143}
144
Xiaofei Bai746f9482021-11-12 08:53:56 +0000145int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 psa_algorithm_t hash_alg,
147 const unsigned char *secret, size_t secret_len,
148 const unsigned char *label, size_t label_len,
149 const unsigned char *ctx, size_t ctx_len,
150 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100151{
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200153 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200154 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
155 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200156 psa_key_derivation_operation_t operation =
157 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if (label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100160 /* Should never happen since this is an internal
161 * function, and we know statically which labels
162 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100164 }
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100167 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100169 }
170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100172 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100174 }
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (!PSA_ALG_IS_HASH(hash_alg)) {
177 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
178 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 ssl_tls13_hkdf_encode_label(buf_len,
181 label, label_len,
182 ctx, ctx_len,
183 hkdf_label,
184 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (status != PSA_SUCCESS) {
189 goto cleanup;
190 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 status = psa_key_derivation_input_bytes(&operation,
193 PSA_KEY_DERIVATION_INPUT_SECRET,
194 secret,
195 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if (status != PSA_SUCCESS) {
198 goto cleanup;
199 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 status = psa_key_derivation_input_bytes(&operation,
202 PSA_KEY_DERIVATION_INPUT_INFO,
203 hkdf_label,
204 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if (status != PSA_SUCCESS) {
207 goto cleanup;
208 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 status = psa_key_derivation_output_bytes(&operation,
211 buf,
212 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (status != PSA_SUCCESS) {
215 goto cleanup;
216 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200217
218cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 abort_status = psa_key_derivation_abort(&operation);
220 status = (status == PSA_SUCCESS ? abort_status : status);
221 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500222 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100223}
224
Jerry Yua5db6c02022-11-23 18:08:04 +0800225MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800226static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 psa_algorithm_t hash_alg,
228 const unsigned char *secret, size_t secret_len,
229 unsigned char *key, size_t key_len,
230 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800231{
232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
233
Jerry Yuaec08b32022-11-29 15:19:27 +0800234 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 hash_alg,
236 secret, secret_len,
237 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
238 NULL, 0,
239 key, key_len);
240 if (ret != 0) {
241 return ret;
242 }
Jerry Yua8771832022-11-21 23:16:54 +0800243
Jerry Yuaec08b32022-11-29 15:19:27 +0800244 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 hash_alg,
246 secret, secret_len,
247 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
248 NULL, 0,
249 iv, iv_len);
250 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800251}
252
Hanno Becker3385a4d2020-08-21 13:03:34 +0100253/*
254 * The traffic keying material is generated from the following inputs:
255 *
256 * - One secret value per sender.
257 * - A purpose value indicating the specific value being generated
258 * - The desired lengths of key and IV.
259 *
260 * The expansion itself is based on HKDF:
261 *
262 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
263 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
264 *
265 * [sender] denotes the sending side and the Secret value is provided
266 * by the function caller. Note that we generate server and client side
267 * keys in a single function call.
268 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000269int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 psa_algorithm_t hash_alg,
271 const unsigned char *client_secret,
272 const unsigned char *server_secret, size_t secret_len,
273 size_t key_len, size_t iv_len,
274 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100275{
276 int ret = 0;
277
Jerry Yua8771832022-11-21 23:16:54 +0800278 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 hash_alg, client_secret, secret_len,
280 keys->client_write_key, key_len,
281 keys->client_write_iv, iv_len);
282 if (ret != 0) {
283 return ret;
284 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100285
Jerry Yua8771832022-11-21 23:16:54 +0800286 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 hash_alg, server_secret, secret_len,
288 keys->server_write_key, key_len,
289 keys->server_write_iv, iv_len);
290 if (ret != 0) {
291 return ret;
292 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100293
Hanno Becker493ea7f2020-09-08 11:01:00 +0100294 keys->key_len = key_len;
295 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100298}
299
Xiaofei Bai746f9482021-11-12 08:53:56 +0000300int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 psa_algorithm_t hash_alg,
302 const unsigned char *secret, size_t secret_len,
303 const unsigned char *label, size_t label_len,
304 const unsigned char *ctx, size_t ctx_len,
305 int ctx_hashed,
306 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100307{
308 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
310 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
314 PSA_HASH_LENGTH(hash_alg), &ctx_len);
315 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500316 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100317 return ret;
318 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 } else {
320 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100321 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100322 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100323 * Let's double-check nonetheless to not run at the risk
324 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100326 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100329 }
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
332 secret, secret_len,
333 label, label_len,
334 hashed_context, ctx_len,
335 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100336
Hanno Beckerb35d5222020-08-21 13:27:44 +0100337}
338
Xiaofei Bai746f9482021-11-12 08:53:56 +0000339int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 psa_algorithm_t hash_alg,
341 const unsigned char *secret_old,
342 const unsigned char *input, size_t input_len,
343 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100344{
345 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
347 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200348 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
350 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200351 const unsigned char *l_input = NULL;
352 size_t l_input_len;
353
Przemek Stekield5ae3652022-05-13 12:10:08 +0200354 psa_key_derivation_operation_t operation =
355 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (!PSA_ALG_IS_HASH(hash_alg)) {
358 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
359 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100362
363 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100364 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000366 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 hash_alg,
368 secret_old, hlen,
369 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
370 NULL, 0, /* context */
371 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
372 tmp_secret, hlen);
373 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100374 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100376 }
377
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200378 ret = 0;
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200381 l_input = input;
382 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200384 l_input = all_zeroes_input;
385 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100386 }
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 status = psa_key_derivation_setup(&operation,
389 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (status != PSA_SUCCESS) {
392 goto cleanup;
393 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 status = psa_key_derivation_input_bytes(&operation,
396 PSA_KEY_DERIVATION_INPUT_SALT,
397 tmp_secret,
398 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (status != PSA_SUCCESS) {
401 goto cleanup;
402 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 status = psa_key_derivation_input_bytes(&operation,
405 PSA_KEY_DERIVATION_INPUT_SECRET,
406 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (status != PSA_SUCCESS) {
409 goto cleanup;
410 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 status = psa_key_derivation_output_bytes(&operation,
413 secret_new,
414 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (status != PSA_SUCCESS) {
417 goto cleanup;
418 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420cleanup:
421 abort_status = psa_key_derivation_abort(&operation);
422 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500423 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
425 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100426}
427
Xiaofei Bai746f9482021-11-12 08:53:56 +0000428int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 psa_algorithm_t hash_alg,
430 unsigned char const *early_secret,
431 unsigned char const *transcript, size_t transcript_len,
432 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100433{
434 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100436
437 /* We should never call this function with an unknown hash,
438 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (!PSA_ALG_IS_HASH(hash_alg)) {
440 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
441 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100442
443 /*
444 * 0
445 * |
446 * v
447 * PSK -> HKDF-Extract = Early Secret
448 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100449 * +-----> Derive-Secret(., "c e traffic", ClientHello)
450 * | = client_early_traffic_secret
451 * |
452 * +-----> Derive-Secret(., "e exp master", ClientHello)
453 * | = early_exporter_master_secret
454 * v
455 */
456
457 /* Create client_early_traffic_secret */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000458 ret = mbedtls_ssl_tls13_derive_secret(
459 hash_alg,
460 early_secret, hash_len,
461 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
462 transcript, transcript_len,
463 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
464 derived->client_early_traffic_secret,
465 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (ret != 0) {
467 return ret;
468 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100469
470 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000471 ret = mbedtls_ssl_tls13_derive_secret(
472 hash_alg,
473 early_secret, hash_len,
474 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
475 transcript, transcript_len,
476 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
477 derived->early_exporter_master_secret,
478 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (ret != 0) {
480 return ret;
481 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100484}
485
Xiaofei Bai746f9482021-11-12 08:53:56 +0000486int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 psa_algorithm_t hash_alg,
488 unsigned char const *handshake_secret,
489 unsigned char const *transcript, size_t transcript_len,
490 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100491{
492 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100494
495 /* We should never call this function with an unknown hash,
496 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (!PSA_ALG_IS_HASH(hash_alg)) {
498 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
499 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100500
501 /*
502 *
503 * Handshake Secret
504 * |
505 * +-----> Derive-Secret( ., "c hs traffic",
506 * | ClientHello...ServerHello )
507 * | = client_handshake_traffic_secret
508 * |
509 * +-----> Derive-Secret( ., "s hs traffic",
510 * | ClientHello...ServerHello )
511 * | = server_handshake_traffic_secret
512 *
513 */
514
515 /*
516 * Compute client_handshake_traffic_secret with
517 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
518 */
519
Xiaokang Qian123cde82023-03-29 06:54:51 +0000520 ret = mbedtls_ssl_tls13_derive_secret(
521 hash_alg,
522 handshake_secret, hash_len,
523 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
524 transcript, transcript_len,
525 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
526 derived->client_handshake_traffic_secret,
527 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (ret != 0) {
529 return ret;
530 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100531
532 /*
533 * Compute server_handshake_traffic_secret with
534 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
535 */
536
Xiaokang Qian123cde82023-03-29 06:54:51 +0000537 ret = mbedtls_ssl_tls13_derive_secret(
538 hash_alg,
539 handshake_secret, hash_len,
540 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
541 transcript, transcript_len,
542 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
543 derived->server_handshake_traffic_secret,
544 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (ret != 0) {
546 return ret;
547 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100550}
551
Xiaofei Bai746f9482021-11-12 08:53:56 +0000552int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 psa_algorithm_t hash_alg,
554 unsigned char const *application_secret,
555 unsigned char const *transcript, size_t transcript_len,
556 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100557{
558 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100560
561 /* We should never call this function with an unknown hash,
562 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (!PSA_ALG_IS_HASH(hash_alg)) {
564 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
565 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100566
567 /* Generate {client,server}_application_traffic_secret_0
568 *
569 * Master Secret
570 * |
571 * +-----> Derive-Secret( ., "c ap traffic",
572 * | ClientHello...server Finished )
573 * | = client_application_traffic_secret_0
574 * |
575 * +-----> Derive-Secret( ., "s ap traffic",
576 * | ClientHello...Server Finished )
577 * | = server_application_traffic_secret_0
578 * |
579 * +-----> Derive-Secret( ., "exp master",
580 * | ClientHello...server Finished)
581 * | = exporter_master_secret
582 *
583 */
584
Xiaokang Qian123cde82023-03-29 06:54:51 +0000585 ret = mbedtls_ssl_tls13_derive_secret(
586 hash_alg,
587 application_secret, hash_len,
588 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
589 transcript, transcript_len,
590 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
591 derived->client_application_traffic_secret_N,
592 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (ret != 0) {
594 return ret;
595 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100596
Xiaokang Qian123cde82023-03-29 06:54:51 +0000597 ret = mbedtls_ssl_tls13_derive_secret(
598 hash_alg,
599 application_secret, hash_len,
600 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
601 transcript, transcript_len,
602 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
603 derived->server_application_traffic_secret_N,
604 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (ret != 0) {
606 return ret;
607 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100608
Xiaokang Qian123cde82023-03-29 06:54:51 +0000609 ret = mbedtls_ssl_tls13_derive_secret(
610 hash_alg,
611 application_secret, hash_len,
612 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
613 transcript, transcript_len,
614 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
615 derived->exporter_master_secret,
616 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (ret != 0) {
618 return ret;
619 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100622}
623
624/* Generate resumption_master_secret for use with the ticket exchange.
625 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000626 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100627 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000628int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 psa_algorithm_t hash_alg,
630 unsigned char const *application_secret,
631 unsigned char const *transcript, size_t transcript_len,
632 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100633{
634 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100636
637 /* We should never call this function with an unknown hash,
638 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (!PSA_ALG_IS_HASH(hash_alg)) {
640 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
641 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100642
Xiaokang Qian123cde82023-03-29 06:54:51 +0000643 ret = mbedtls_ssl_tls13_derive_secret(
644 hash_alg,
645 application_secret, hash_len,
646 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
647 transcript, transcript_len,
648 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
649 derived->resumption_master_secret,
650 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (ret != 0) {
653 return ret;
654 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100657}
658
Yanray Wang05402112022-12-13 18:50:42 +0800659/**
660 * \brief Transition into application stage of TLS 1.3 key schedule.
661 *
662 * The TLS 1.3 key schedule can be viewed as a simple state machine
663 * with states Initial -> Early -> Handshake -> Application, and
664 * this function represents the Handshake -> Application transition.
665 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800666 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800667 * can be used to derive the handshake traffic keys.
668 *
669 * \param ssl The SSL context to operate on. This must be in key schedule
670 * stage \c Handshake.
671 *
672 * \returns \c 0 on success.
673 * \returns A negative error code on failure.
674 */
675MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800676static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000677{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000679 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200680 psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000682
683 /*
684 * Compute MasterSecret
685 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000686 ret = mbedtls_ssl_tls13_evolve_secret(
687 hash_alg,
688 handshake->tls13_master_secrets.handshake,
689 NULL, 0,
690 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (ret != 0) {
692 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
693 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000694 }
695
Xiaokang Qian123cde82023-03-29 06:54:51 +0000696 MBEDTLS_SSL_DEBUG_BUF(
697 4, "Master secret",
698 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000701}
702
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200703MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100704static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
705 unsigned char const *base_key,
706 unsigned char const *transcript,
707 unsigned char *dst,
708 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100709{
Gabor Mezei07732f72022-03-26 17:04:19 +0100710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
712 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100714 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100715 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100716 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100717
718 /* We should never call this function with an unknown hash,
719 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (!PSA_ALG_IS_HASH(hash_alg)) {
721 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
722 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100723
724 /* TLS 1.3 Finished message
725 *
726 * struct {
727 * opaque verify_data[Hash.length];
728 * } Finished;
729 *
730 * verify_data =
731 * HMAC( finished_key,
732 * Hash( Handshake Context +
733 * Certificate* +
734 * CertificateVerify* )
735 * )
736 *
737 * finished_key =
738 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
739 */
740
Xiaofei Bai746f9482021-11-12 08:53:56 +0000741 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 hash_alg, base_key, hash_len,
743 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
744 NULL, 0,
745 finished_key, hash_len);
746 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100747 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100748 }
749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 alg = PSA_ALG_HMAC(hash_alg);
751 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
752 psa_set_key_algorithm(&attributes, alg);
753 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
754
755 status = psa_import_key(&attributes, finished_key, hash_len, &key);
756 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500757 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 goto exit;
759 }
760
761 status = psa_mac_compute(key, alg, transcript, hash_len,
762 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500763 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100764
765exit:
766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 status = psa_destroy_key(key);
768 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500769 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100775}
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
778 unsigned char *dst,
779 size_t dst_len,
780 size_t *actual_len,
781 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000782{
XiaokangQiana7634982021-10-22 06:32:32 +0000783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000784
XiaokangQianaaa0e192021-11-10 03:07:04 +0000785 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000786 size_t transcript_len;
787
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800788 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800789 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800790 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800792
793 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100794
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200795 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 ssl->handshake->ciphersuite_info->mac);
797 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800802 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
804 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800805 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800807 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800810 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
811 goto exit;
812 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
815 transcript, sizeof(transcript),
816 &transcript_len);
817 if (ret != 0) {
818 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000819 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000820 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000822
Xiaokang Qian123cde82023-03-29 06:54:51 +0000823 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
824 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000826 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
830 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000831
832exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800833 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_platform_zeroize(base_key, base_key_len);
835 mbedtls_platform_zeroize(transcript, sizeof(transcript));
836 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000837}
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
840 const psa_algorithm_t hash_alg,
841 unsigned char const *psk, size_t psk_len,
842 int psk_type,
843 unsigned char const *transcript,
844 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100845{
846 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100847 unsigned char binder_key[PSA_MAC_MAX_SIZE];
848 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100850 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100851
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100852#if !defined(MBEDTLS_DEBUG_C)
853 ssl = NULL; /* make sure we don't use it except for debug */
854 ((void) ssl);
855#endif
856
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100857 /* We should never call this function with an unknown hash,
858 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (!PSA_ALG_IS_HASH(hash_alg)) {
860 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
861 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100862
863 /*
864 * 0
865 * |
866 * v
867 * PSK -> HKDF-Extract = Early Secret
868 * |
869 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
870 * | = binder_key
871 * v
872 */
873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
875 NULL, /* Old secret */
876 psk, psk_len, /* Input */
877 early_secret);
878 if (ret != 0) {
879 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100880 goto exit;
881 }
882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
884 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000887 ret = mbedtls_ssl_tls13_derive_secret(
888 hash_alg,
889 early_secret, hash_len,
890 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
891 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
892 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
894 } else {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000895 ret = mbedtls_ssl_tls13_derive_secret(
896 hash_alg,
897 early_secret, hash_len,
898 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
899 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
900 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100902 }
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (ret != 0) {
905 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100906 goto exit;
907 }
908
909 /*
910 * The binding_value is computed in the same way as the Finished message
911 * but with the BaseKey being the binder_key.
912 */
913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
915 result, &actual_len);
916 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100917 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100921
922exit:
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
925 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
926 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100927}
928
Xiaokang Qian123cde82023-03-29 06:54:51 +0000929int mbedtls_ssl_tls13_populate_transform(
930 mbedtls_ssl_transform *transform,
931 int endpoint, int ciphersuite,
932 mbedtls_ssl_key_set const *traffic_keys,
933 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000934{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100935#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000936 int ret;
937 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200938#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000939 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
940 unsigned char const *key_enc;
941 unsigned char const *iv_enc;
942 unsigned char const *key_dec;
943 unsigned char const *iv_dec;
944
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100945#if defined(MBEDTLS_USE_PSA_CRYPTO)
946 psa_key_type_t key_type;
947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
948 psa_algorithm_t alg;
949 size_t key_bits;
950 psa_status_t status = PSA_SUCCESS;
951#endif
952
Hanno Beckerc94060c2021-03-22 07:50:44 +0000953#if !defined(MBEDTLS_DEBUG_C)
954 ssl = NULL; /* make sure we don't use it except for those cases */
955 (void) ssl;
956#endif
957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
959 if (ciphersuite_info == NULL) {
960 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
961 ciphersuite));
962 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100963 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000964
Neil Armstronga8093f52022-05-04 17:44:05 +0200965#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
967 if (cipher_info == NULL) {
968 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
969 ciphersuite_info->cipher));
970 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000971 }
972
973 /*
974 * Setup cipher contexts in target transform
975 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
977 cipher_info)) != 0) {
978 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
979 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000980 }
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
983 cipher_info)) != 0) {
984 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
985 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000986 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100987#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000988
989#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000991 key_enc = traffic_keys->server_write_key;
992 key_dec = traffic_keys->client_write_key;
993 iv_enc = traffic_keys->server_write_iv;
994 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000996#endif /* MBEDTLS_SSL_SRV_C */
997#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000999 key_enc = traffic_keys->client_write_key;
1000 key_dec = traffic_keys->server_write_key;
1001 iv_enc = traffic_keys->client_write_iv;
1002 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +00001004#endif /* MBEDTLS_SSL_CLI_C */
1005 {
1006 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001008 }
1009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
1011 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +00001012
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001013#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
1015 key_enc, cipher_info->key_bitlen,
1016 MBEDTLS_ENCRYPT)) != 0) {
1017 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1018 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001019 }
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
1022 key_dec, cipher_info->key_bitlen,
1023 MBEDTLS_DECRYPT)) != 0) {
1024 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1025 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001026 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001027#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001028
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001029 /*
1030 * Setup other fields in SSL transform
1031 */
1032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001034 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001036 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001038
1039 transform->ivlen = traffic_keys->iv_len;
1040 transform->maclen = 0;
1041 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001042 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001043
1044 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001045 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001046 * type-extended and padded plaintext is therefore the padding
1047 * granularity. */
1048 transform->minlen =
1049 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1050
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001052 /*
1053 * Setup psa keys and alg
1054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
1056 transform->taglen,
1057 &alg,
1058 &key_type,
1059 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001060 MBEDTLS_SSL_DEBUG_RET(
1061 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001062 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001063 }
1064
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001065 transform->psa_alg = alg;
1066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1068 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1069 psa_set_key_algorithm(&attributes, alg);
1070 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((status = psa_import_key(&attributes,
1073 key_enc,
1074 PSA_BITS_TO_BYTES(key_bits),
1075 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001076 MBEDTLS_SSL_DEBUG_RET(
1077 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001078 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001079 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if ((status = psa_import_key(&attributes,
1084 key_dec,
1085 PSA_BITS_TO_BYTES(key_bits),
1086 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001087 MBEDTLS_SSL_DEBUG_RET(
1088 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001089 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001090 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001091 }
1092#endif /* MBEDTLS_USE_PSA_CRYPTO */
1093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001095}
1096
Jerry Yu84a6eda2022-11-04 11:17:35 +08001097MBEDTLS_CHECK_RETURN_CRITICAL
1098static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1100 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001101{
1102 psa_key_type_t key_type;
1103 psa_algorithm_t alg;
1104 size_t taglen;
1105 size_t key_bits;
1106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001109 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001111 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, taglen,
1115 &alg, &key_type, &key_bits);
1116 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001117 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001121
1122 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1123 *iv_len = 12;
1124
1125 return 0;
1126}
1127
Jerry Yu91b560f2022-11-04 14:10:34 +08001128#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001129/*
1130 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001131 * the early application data and handshake messages as described in section 7
1132 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001133 *
Jerry Yue31688b2022-11-22 21:55:56 +08001134 * NOTE: Only one key is generated, the key for the traffic from the client to
1135 * the server. The TLS 1.3 specification does not define a secret and thus
1136 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001137 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001138MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001139static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1140 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001141{
1142 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001143 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001144 psa_algorithm_t hash_alg;
1145 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001146 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1147 size_t transcript_len;
Jerry Yuaec08b32022-11-29 15:19:27 +08001148 size_t key_len;
1149 size_t iv_len;
Yanray Wang16c895d2022-12-15 15:14:35 +08001150 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001151
1152 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001153 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1154 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1159 if (ret != 0) {
1160 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001161 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001162 }
1163
1164 md_type = ciphersuite_info->mac;
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
1167 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1170 transcript,
1171 sizeof(transcript),
1172 &transcript_len);
1173 if (ret != 0) {
1174 MBEDTLS_SSL_DEBUG_RET(1,
1175 "mbedtls_ssl_get_handshake_transcript",
1176 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001177 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001178 }
1179
Jerry Yub094e122022-11-21 13:03:47 +08001180 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001182 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001184 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001186 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001187 }
1188
1189 MBEDTLS_SSL_DEBUG_BUF(
1190 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001191 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001192
1193 /*
1194 * Export client handshake traffic secret
1195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001197 ssl->f_export_keys(
1198 ssl->p_export_keys,
1199 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001200 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001201 hash_len,
1202 handshake->randbytes,
1203 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001205 }
1206
Jerry Yua8771832022-11-21 23:16:54 +08001207 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001209 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 hash_len, traffic_keys->client_write_key, key_len,
1211 traffic_keys->client_write_iv, iv_len);
1212 if (ret != 0) {
1213 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001214 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001215 }
Jerry Yua8771832022-11-21 23:16:54 +08001216 traffic_keys->key_len = key_len;
1217 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1220 traffic_keys->client_write_key,
1221 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1224 traffic_keys->client_write_iv,
1225 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001228
Jerry Yu3d78e082022-11-23 18:26:20 +08001229cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001230 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001231 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001232 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1234 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001235}
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001238{
1239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1240 mbedtls_ssl_key_set traffic_keys;
1241 mbedtls_ssl_transform *transform_earlydata = NULL;
1242 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1243
1244 /* Next evolution in key schedule: Establish early_data secret and
1245 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1247 if (ret != 0) {
1248 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1249 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001250 goto cleanup;
1251 }
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1254 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001255 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1256 goto cleanup;
1257 }
1258
1259 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 transform_earlydata,
1261 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001262 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 &traffic_keys,
1264 ssl);
1265 if (ret != 0) {
1266 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001267 goto cleanup;
1268 }
1269 handshake->transform_earlydata = transform_earlydata;
1270
1271cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1273 if (ret != 0) {
1274 mbedtls_free(transform_earlydata);
1275 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001278}
1279#endif /* MBEDTLS_SSL_EARLY_DATA */
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001282{
Jerry Yue3131ef2021-09-16 13:14:15 +08001283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001284 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001285 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001286 unsigned char *psk = NULL;
1287 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 if (handshake->ciphersuite_info == NULL) {
1290 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1291 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001292 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001295#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1297 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1298 if (ret != 0) {
1299 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1300 ret);
1301 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001302 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001303 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001304#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1307 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001308#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001309 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001311#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (ret != 0) {
1313 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1314 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001315 }
1316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1318 handshake->tls13_master_secrets.early,
1319 PSA_HASH_LENGTH(hash_alg));
1320 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001321}
1322
Yanray Wang05402112022-12-13 18:50:42 +08001323/**
1324 * \brief Compute TLS 1.3 handshake traffic keys.
1325 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001326 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1327 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001328 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001329 *
1330 * \param ssl The SSL context to operate on. This must be in
1331 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001332 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001333 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001334 * keys. This must be writable but may be uninitialized.
1335 *
1336 * \returns \c 0 on success.
1337 * \returns A negative error code on failure.
1338 */
1339MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001340static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1341 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001342{
1343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001344 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001345 psa_algorithm_t hash_alg;
1346 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001347 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001348 size_t transcript_len;
Jerry Yub094e122022-11-21 13:03:47 +08001349 size_t key_len;
1350 size_t iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001351
Jerry Yu435208a2021-10-13 11:22:16 +08001352 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001353 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1354 handshake->ciphersuite_info;
1355 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1356 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001357
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001358 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1361 if (ret != 0) {
1362 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001363 return ret;
1364 }
1365
Jerry Yu435208a2021-10-13 11:22:16 +08001366 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
1369 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1372 transcript,
1373 sizeof(transcript),
1374 &transcript_len);
1375 if (ret != 0) {
1376 MBEDTLS_SSL_DEBUG_RET(1,
1377 "mbedtls_ssl_get_handshake_transcript",
1378 ret);
1379 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001380 }
1381
Xiaokang Qian123cde82023-03-29 06:54:51 +00001382 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1383 hash_alg, handshake->tls13_master_secrets.handshake,
1384 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (ret != 0) {
1386 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1387 ret);
1388 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001389 }
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1392 tls13_hs_secrets->client_handshake_traffic_secret,
1393 hash_len);
1394 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1395 tls13_hs_secrets->server_handshake_traffic_secret,
1396 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001397
1398 /*
1399 * Export client handshake traffic secret
1400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001402 ssl->f_export_keys(
1403 ssl->p_export_keys,
1404 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1405 tls13_hs_secrets->client_handshake_traffic_secret,
1406 hash_len,
1407 handshake->randbytes,
1408 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1409 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001410
Xiaokang Qian123cde82023-03-29 06:54:51 +00001411 ssl->f_export_keys(
1412 ssl->p_export_keys,
1413 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1414 tls13_hs_secrets->server_handshake_traffic_secret,
1415 hash_len,
1416 handshake->randbytes,
1417 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1418 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001419 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001420
Xiaokang Qian123cde82023-03-29 06:54:51 +00001421 ret = mbedtls_ssl_tls13_make_traffic_keys(
1422 hash_alg,
1423 tls13_hs_secrets->client_handshake_traffic_secret,
1424 tls13_hs_secrets->server_handshake_traffic_secret,
1425 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (ret != 0) {
1427 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001428 goto exit;
1429 }
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1432 traffic_keys->client_write_key,
1433 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1436 traffic_keys->server_write_key,
1437 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1440 traffic_keys->client_write_iv,
1441 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1444 traffic_keys->server_write_iv,
1445 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001446
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001447 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001448
1449exit:
1450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001452}
1453
Yanray Wang05402112022-12-13 18:50:42 +08001454/**
1455 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1456 *
1457 * The TLS 1.3 key schedule can be viewed as a simple state machine
1458 * with states Initial -> Early -> Handshake -> Application, and
1459 * this function represents the Early -> Handshake transition.
1460 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001461 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001462 * can be used to derive the handshake traffic keys.
1463 *
1464 * \param ssl The SSL context to operate on. This must be in key schedule
1465 * stage \c Early.
1466 *
1467 * \returns \c 0 on success.
1468 * \returns A negative error code on failure.
1469 */
1470MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001471static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001472{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001474 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001475 psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001477 unsigned char *shared_secret = NULL;
1478 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001479
Ronald Crona2900bc2022-10-20 14:37:35 +02001480#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001481 /*
1482 * Compute ECDHE secret used to compute the handshake secret from which
1483 * client_handshake_traffic_secret and server_handshake_traffic_secret
1484 * are derived in the handshake secret derivation stage.
1485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001487 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ||
1488 mbedtls_ssl_tls13_named_group_is_dhe(handshake->offered_group_id)) {
1489#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001490 psa_algorithm_t alg =
1491 mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id) ?
1492 PSA_ALG_ECDH : PSA_ALG_FFDH;
1493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001495 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001496 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 status = psa_get_key_attributes(handshake->ecdh_psa_privkey,
1499 &key_attributes);
1500 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001501 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 }
Ronald Cron3b056202022-10-05 17:20:21 +02001503
1504 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 psa_get_key_bits(&key_attributes));
1506 shared_secret = mbedtls_calloc(1, shared_secret_len);
1507 if (shared_secret == NULL) {
1508 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1509 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001510
Ronald Cron4c7edb22022-10-05 15:37:11 +02001511 status = psa_raw_key_agreement(
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001512 alg, handshake->ecdh_psa_privkey,
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1514 shared_secret, shared_secret_len, &shared_secret_len);
1515 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001516 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001518 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001519 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 status = psa_destroy_key(handshake->ecdh_psa_privkey);
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_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001525 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001526 }
1527
1528 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001529#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 } else {
1531 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1532 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001533 }
1534 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001535#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001536
1537 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001538 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001539 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001540 ret = mbedtls_ssl_tls13_evolve_secret(
1541 hash_alg, handshake->tls13_master_secrets.early,
1542 shared_secret, shared_secret_len,
1543 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (ret != 0) {
1545 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001546 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001547 }
1548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1550 handshake->tls13_master_secrets.handshake,
1551 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001552
Ronald Cron3b056202022-10-05 17:20:21 +02001553cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (shared_secret != NULL) {
1555 mbedtls_platform_zeroize(shared_secret, shared_secret_len);
1556 mbedtls_free(shared_secret);
Ronald Cron3b056202022-10-05 17:20:21 +02001557 }
1558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001560}
1561
Yanray Wang05402112022-12-13 18:50:42 +08001562/**
1563 * \brief Compute TLS 1.3 application traffic keys.
1564 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001565 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001566 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001567 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001568 *
1569 * \param ssl The SSL context to operate on. This must be in
1570 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001571 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001572 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001573 * keys. This must be writable but may be uninitialized.
1574 *
1575 * \returns \c 0 on success.
1576 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001577 */
Yanray Wang05402112022-12-13 18:50:42 +08001578MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001579static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 mbedtls_ssl_context *ssl,
1581 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001582{
XiaokangQiana7634982021-10-22 06:32:32 +00001583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001584 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001585
1586 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001587 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001588 &ssl->session_negotiate->app_secrets;
1589
1590 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001591 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001592 size_t transcript_len;
1593
1594 /* Variables relating to the hash for the chosen ciphersuite. */
1595 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001596
1597 psa_algorithm_t hash_alg;
1598 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001599
1600 /* Variables relating to the cipher for the chosen ciphersuite. */
Xiaofei Baib7972842021-11-18 07:29:56 +00001601 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001604
1605 /* Extract basic information about hash and ciphersuite */
1606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1608 &key_len, &iv_len);
1609 if (ret != 0) {
1610 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001611 goto cleanup;
1612 }
1613
XiaokangQian33062842021-11-11 03:37:45 +00001614 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
1617 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001618
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001619 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001620 * to call this at the right time, that is, after the ServerFinished. */
1621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1623 transcript, sizeof(transcript),
1624 &transcript_len);
1625 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001626 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001628
1629 /* Compute application secrets from master secret and transcript hash. */
1630
Xiaokang Qian123cde82023-03-29 06:54:51 +00001631 ret = mbedtls_ssl_tls13_derive_application_secrets(
1632 hash_alg, handshake->tls13_master_secrets.app,
1633 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001635 MBEDTLS_SSL_DEBUG_RET(
1636 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001637 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001638 }
1639
1640 /* Derive first epoch of IV + Key for application traffic. */
1641
Xiaokang Qian123cde82023-03-29 06:54:51 +00001642 ret = mbedtls_ssl_tls13_make_traffic_keys(
1643 hash_alg,
1644 app_secrets->client_application_traffic_secret_N,
1645 app_secrets->server_application_traffic_secret_N,
1646 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 if (ret != 0) {
1648 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001649 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001650 }
1651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1653 app_secrets->client_application_traffic_secret_N,
1654 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1657 app_secrets->server_application_traffic_secret_N,
1658 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001659
XiaokangQianac0385c2021-11-03 06:40:11 +00001660 /*
1661 * Export client/server application traffic secret 0
1662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001664 ssl->f_export_keys(
1665 ssl->p_export_keys,
1666 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1667 app_secrets->client_application_traffic_secret_N, hash_len,
1668 handshake->randbytes,
1669 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1670 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1671 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001672
Xiaokang Qian123cde82023-03-29 06:54:51 +00001673 ssl->f_export_keys(
1674 ssl->p_export_keys,
1675 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1676 app_secrets->server_application_traffic_secret_N, hash_len,
1677 handshake->randbytes,
1678 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1679 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1680 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001681 }
1682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1684 traffic_keys->client_write_key, key_len);
1685 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1686 traffic_keys->server_write_key, key_len);
1687 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1688 traffic_keys->client_write_iv, iv_len);
1689 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1690 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001695 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1697 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1700 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001701}
1702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001704{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001706 mbedtls_ssl_key_set traffic_keys;
1707 mbedtls_ssl_transform *transform_handshake = NULL;
1708 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1709
1710 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001711 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 if (ret != 0) {
1713 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001714 goto cleanup;
1715 }
1716
1717 /* Next evolution in key schedule: Establish handshake secret and
1718 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001719 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001721 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001723 goto cleanup;
1724 }
1725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1727 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001728 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1729 goto cleanup;
1730 }
1731
Jerry Yuef2b98a2022-05-06 16:40:05 +08001732 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 transform_handshake,
1734 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001735 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 &traffic_keys,
1737 ssl);
1738 if (ret != 0) {
1739 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001740 goto cleanup;
1741 }
1742 handshake->transform_handshake = transform_handshake;
1743
1744cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1746 if (ret != 0) {
1747 mbedtls_free(transform_handshake);
1748 }
Jerry Yue110d252022-05-05 10:19:22 +08001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001751}
1752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001754{
Jerry Yu46bffe02022-09-13 11:25:28 +08001755 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001756 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001757 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1758 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001759 size_t transcript_len;
1760
Xiaokang Qian123cde82023-03-29 06:54:51 +00001761 MBEDTLS_SSL_DEBUG_MSG(
1762 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001763
Jerry Yu46bffe02022-09-13 11:25:28 +08001764 md_type = handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1767 transcript, sizeof(transcript),
1768 &transcript_len);
1769 if (ret != 0) {
1770 return ret;
1771 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001772
1773 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 mbedtls_psa_translate_md(md_type),
1775 handshake->tls13_master_secrets.app,
1776 transcript, transcript_len,
1777 &ssl->session_negotiate->app_secrets);
1778 if (ret != 0) {
1779 return ret;
1780 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001781
Jerry Yuff226982022-04-16 16:52:57 +08001782 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1784 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001785
Xiaokang Qian123cde82023-03-29 06:54:51 +00001786 MBEDTLS_SSL_DEBUG_BUF(
1787 4, "Resumption master secret",
1788 ssl->session_negotiate->app_secrets.resumption_master_secret,
1789 PSA_HASH_LENGTH(mbedtls_psa_translate_md(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001790
Xiaokang Qian123cde82023-03-29 06:54:51 +00001791 MBEDTLS_SSL_DEBUG_MSG(
1792 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001794}
1795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001797{
1798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1799 mbedtls_ssl_key_set traffic_keys;
1800 mbedtls_ssl_transform *transform_application = NULL;
1801
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001802 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (ret != 0) {
1804 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001805 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001806 goto cleanup;
1807 }
1808
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001809 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
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_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001813 goto cleanup;
1814 }
1815
1816 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1818 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001819 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1820 goto cleanup;
1821 }
1822
1823 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 transform_application,
1825 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001826 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 &traffic_keys,
1828 ssl);
1829 if (ret != 0) {
1830 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001831 goto cleanup;
1832 }
1833
1834 ssl->transform_application = transform_application;
1835
1836cleanup:
1837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1839 if (ret != 0) {
1840 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001841 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001843}
1844
Ronald Cron41a443a2022-10-04 16:38:25 +02001845#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001846int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1847 unsigned char **psk,
1848 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001849{
Jerry Yu40f37712022-07-26 16:58:57 +08001850#if defined(MBEDTLS_USE_PSA_CRYPTO)
1851 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001852 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001853
1854 *psk_len = 0;
1855 *psk = NULL;
1856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1858 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001859 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001860
1861 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1862 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001863 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 }
1865
1866 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1867 *psk = mbedtls_calloc(1, *psk_len);
1868 if (*psk == NULL) {
1869 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1870 }
1871
1872 status = psa_export_key(ssl->handshake->psk_opaque,
1873 (uint8_t *) *psk, *psk_len, psk_len);
1874 if (status != PSA_SUCCESS) {
1875 mbedtls_free((void *) *psk);
1876 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001877 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 }
1879 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001880#else
1881 *psk = ssl->handshake->psk;
1882 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 if (*psk == NULL) {
1884 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1885 }
1886 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001887#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001888}
Ronald Cron41a443a2022-10-04 16:38:25 +02001889#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001890
Ronald Cron6f135e12021-12-08 16:57:54 +01001891#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */