blob: 6edce50bc8cfb01b234e9e124d5c073db3afcefb [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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
459 early_secret, hash_len,
460 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
461 transcript, transcript_len,
462 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
463 derived->client_early_traffic_secret,
464 hash_len);
465 if (ret != 0) {
466 return ret;
467 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100468
469 /* Create early exporter */
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
471 early_secret, hash_len,
472 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
473 transcript, transcript_len,
474 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
475 derived->early_exporter_master_secret,
476 hash_len);
477 if (ret != 0) {
478 return ret;
479 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100482}
483
Xiaofei Bai746f9482021-11-12 08:53:56 +0000484int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 psa_algorithm_t hash_alg,
486 unsigned char const *handshake_secret,
487 unsigned char const *transcript, size_t transcript_len,
488 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100489{
490 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100492
493 /* We should never call this function with an unknown hash,
494 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (!PSA_ALG_IS_HASH(hash_alg)) {
496 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
497 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100498
499 /*
500 *
501 * Handshake Secret
502 * |
503 * +-----> Derive-Secret( ., "c hs traffic",
504 * | ClientHello...ServerHello )
505 * | = client_handshake_traffic_secret
506 * |
507 * +-----> Derive-Secret( ., "s hs traffic",
508 * | ClientHello...ServerHello )
509 * | = server_handshake_traffic_secret
510 *
511 */
512
513 /*
514 * Compute client_handshake_traffic_secret with
515 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
516 */
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
519 handshake_secret, hash_len,
520 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
521 transcript, transcript_len,
522 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
523 derived->client_handshake_traffic_secret,
524 hash_len);
525 if (ret != 0) {
526 return ret;
527 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100528
529 /*
530 * Compute server_handshake_traffic_secret with
531 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
532 */
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
535 handshake_secret, hash_len,
536 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
537 transcript, transcript_len,
538 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
539 derived->server_handshake_traffic_secret,
540 hash_len);
541 if (ret != 0) {
542 return ret;
543 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100546}
547
Xiaofei Bai746f9482021-11-12 08:53:56 +0000548int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 psa_algorithm_t hash_alg,
550 unsigned char const *application_secret,
551 unsigned char const *transcript, size_t transcript_len,
552 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100553{
554 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100556
557 /* We should never call this function with an unknown hash,
558 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (!PSA_ALG_IS_HASH(hash_alg)) {
560 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
561 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100562
563 /* Generate {client,server}_application_traffic_secret_0
564 *
565 * Master Secret
566 * |
567 * +-----> Derive-Secret( ., "c ap traffic",
568 * | ClientHello...server Finished )
569 * | = client_application_traffic_secret_0
570 * |
571 * +-----> Derive-Secret( ., "s ap traffic",
572 * | ClientHello...Server Finished )
573 * | = server_application_traffic_secret_0
574 * |
575 * +-----> Derive-Secret( ., "exp master",
576 * | ClientHello...server Finished)
577 * | = exporter_master_secret
578 *
579 */
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
582 application_secret, hash_len,
583 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
584 transcript, transcript_len,
585 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
586 derived->client_application_traffic_secret_N,
587 hash_len);
588 if (ret != 0) {
589 return ret;
590 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
593 application_secret, hash_len,
594 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
595 transcript, transcript_len,
596 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
597 derived->server_application_traffic_secret_N,
598 hash_len);
599 if (ret != 0) {
600 return ret;
601 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
604 application_secret, hash_len,
605 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
606 transcript, transcript_len,
607 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
608 derived->exporter_master_secret,
609 hash_len);
610 if (ret != 0) {
611 return ret;
612 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100615}
616
617/* Generate resumption_master_secret for use with the ticket exchange.
618 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000619 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100620 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000621int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 psa_algorithm_t hash_alg,
623 unsigned char const *application_secret,
624 unsigned char const *transcript, size_t transcript_len,
625 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100626{
627 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100629
630 /* We should never call this function with an unknown hash,
631 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (!PSA_ALG_IS_HASH(hash_alg)) {
633 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
634 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
637 application_secret, hash_len,
638 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
639 transcript, transcript_len,
640 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
641 derived->resumption_master_secret,
642 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (ret != 0) {
645 return ret;
646 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100649}
650
Yanray Wang05402112022-12-13 18:50:42 +0800651/**
652 * \brief Transition into application stage of TLS 1.3 key schedule.
653 *
654 * The TLS 1.3 key schedule can be viewed as a simple state machine
655 * with states Initial -> Early -> Handshake -> Application, and
656 * this function represents the Handshake -> Application transition.
657 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800658 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800659 * can be used to derive the handshake traffic keys.
660 *
661 * \param ssl The SSL context to operate on. This must be in key schedule
662 * stage \c Handshake.
663 *
664 * \returns \c 0 on success.
665 * \returns A negative error code on failure.
666 */
667MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800668static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000669{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000671 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200672 psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000674
675 /*
676 * Compute MasterSecret
677 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
679 handshake->tls13_master_secrets.handshake,
680 NULL, 0,
681 handshake->tls13_master_secrets.app);
682 if (ret != 0) {
683 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
684 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000685 }
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 MBEDTLS_SSL_DEBUG_BUF(4, "Master secret",
688 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000691}
692
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200693MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100694static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
695 unsigned char const *base_key,
696 unsigned char const *transcript,
697 unsigned char *dst,
698 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100699{
Gabor Mezei07732f72022-03-26 17:04:19 +0100700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100704 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100705 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100706 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100707
708 /* We should never call this function with an unknown hash,
709 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (!PSA_ALG_IS_HASH(hash_alg)) {
711 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
712 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100713
714 /* TLS 1.3 Finished message
715 *
716 * struct {
717 * opaque verify_data[Hash.length];
718 * } Finished;
719 *
720 * verify_data =
721 * HMAC( finished_key,
722 * Hash( Handshake Context +
723 * Certificate* +
724 * CertificateVerify* )
725 * )
726 *
727 * finished_key =
728 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
729 */
730
Xiaofei Bai746f9482021-11-12 08:53:56 +0000731 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 hash_alg, base_key, hash_len,
733 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
734 NULL, 0,
735 finished_key, hash_len);
736 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100737 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100738 }
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 alg = PSA_ALG_HMAC(hash_alg);
741 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
742 psa_set_key_algorithm(&attributes, alg);
743 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
744
745 status = psa_import_key(&attributes, finished_key, hash_len, &key);
746 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500747 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 goto exit;
749 }
750
751 status = psa_mac_compute(key, alg, transcript, hash_len,
752 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500753 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100754
755exit:
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 status = psa_destroy_key(key);
758 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500759 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100765}
766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
768 unsigned char *dst,
769 size_t dst_len,
770 size_t *actual_len,
771 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000772{
XiaokangQiana7634982021-10-22 06:32:32 +0000773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000774
XiaokangQianaaa0e192021-11-10 03:07:04 +0000775 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000776 size_t transcript_len;
777
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800778 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800779 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800780 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800782
783 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100784
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200785 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 ssl->handshake->ciphersuite_info->mac);
787 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800792 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
794 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800795 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800797 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800800 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
801 goto exit;
802 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
805 transcript, sizeof(transcript),
806 &transcript_len);
807 if (ret != 0) {
808 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000809 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000810 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 ret = ssl_tls13_calc_finished_core(hash_alg, base_key, transcript, dst, actual_len);
814 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000815 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
819 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000820
821exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800822 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 mbedtls_platform_zeroize(base_key, base_key_len);
824 mbedtls_platform_zeroize(transcript, sizeof(transcript));
825 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000826}
827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
829 const psa_algorithm_t hash_alg,
830 unsigned char const *psk, size_t psk_len,
831 int psk_type,
832 unsigned char const *transcript,
833 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100834{
835 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100836 unsigned char binder_key[PSA_MAC_MAX_SIZE];
837 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100839 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100840
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100841#if !defined(MBEDTLS_DEBUG_C)
842 ssl = NULL; /* make sure we don't use it except for debug */
843 ((void) ssl);
844#endif
845
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100846 /* We should never call this function with an unknown hash,
847 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (!PSA_ALG_IS_HASH(hash_alg)) {
849 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
850 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100851
852 /*
853 * 0
854 * |
855 * v
856 * PSK -> HKDF-Extract = Early Secret
857 * |
858 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
859 * | = binder_key
860 * v
861 */
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
864 NULL, /* Old secret */
865 psk, psk_len, /* Input */
866 early_secret);
867 if (ret != 0) {
868 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100869 goto exit;
870 }
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
873 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
876 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
877 early_secret, hash_len,
878 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
879 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
880 binder_key, hash_len);
881 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
882 } else {
883 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
884 early_secret, hash_len,
885 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
886 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
887 binder_key, hash_len);
888 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100889 }
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (ret != 0) {
892 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100893 goto exit;
894 }
895
896 /*
897 * The binding_value is computed in the same way as the Finished message
898 * but with the BaseKey being the binder_key.
899 */
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
902 result, &actual_len);
903 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100904 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100908
909exit:
910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
912 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
913 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100914}
915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
917 int endpoint,
918 int ciphersuite,
919 mbedtls_ssl_key_set const *traffic_keys,
920 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000921{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100922#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000923 int ret;
924 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200925#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000926 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
927 unsigned char const *key_enc;
928 unsigned char const *iv_enc;
929 unsigned char const *key_dec;
930 unsigned char const *iv_dec;
931
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100932#if defined(MBEDTLS_USE_PSA_CRYPTO)
933 psa_key_type_t key_type;
934 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
935 psa_algorithm_t alg;
936 size_t key_bits;
937 psa_status_t status = PSA_SUCCESS;
938#endif
939
Hanno Beckerc94060c2021-03-22 07:50:44 +0000940#if !defined(MBEDTLS_DEBUG_C)
941 ssl = NULL; /* make sure we don't use it except for those cases */
942 (void) ssl;
943#endif
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
946 if (ciphersuite_info == NULL) {
947 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
948 ciphersuite));
949 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100950 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000951
Neil Armstronga8093f52022-05-04 17:44:05 +0200952#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
954 if (cipher_info == NULL) {
955 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
956 ciphersuite_info->cipher));
957 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000958 }
959
960 /*
961 * Setup cipher contexts in target transform
962 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
964 cipher_info)) != 0) {
965 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
966 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
970 cipher_info)) != 0) {
971 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
972 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000973 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100974#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000975
976#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000978 key_enc = traffic_keys->server_write_key;
979 key_dec = traffic_keys->client_write_key;
980 iv_enc = traffic_keys->server_write_iv;
981 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000983#endif /* MBEDTLS_SSL_SRV_C */
984#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000986 key_enc = traffic_keys->client_write_key;
987 key_dec = traffic_keys->server_write_key;
988 iv_enc = traffic_keys->client_write_iv;
989 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000991#endif /* MBEDTLS_SSL_CLI_C */
992 {
993 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000995 }
996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
998 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000999
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001000#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
1002 key_enc, cipher_info->key_bitlen,
1003 MBEDTLS_ENCRYPT)) != 0) {
1004 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1005 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001006 }
1007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
1009 key_dec, cipher_info->key_bitlen,
1010 MBEDTLS_DECRYPT)) != 0) {
1011 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1012 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001013 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001014#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001015
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001016 /*
1017 * Setup other fields in SSL transform
1018 */
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001021 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001023 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001025
1026 transform->ivlen = traffic_keys->iv_len;
1027 transform->maclen = 0;
1028 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001029 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001030
1031 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001032 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001033 * type-extended and padded plaintext is therefore the padding
1034 * granularity. */
1035 transform->minlen =
1036 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1037
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001038#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001039 /*
1040 * Setup psa keys and alg
1041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
1043 transform->taglen,
1044 &alg,
1045 &key_type,
1046 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001047 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
1048 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001049 }
1050
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001051 transform->psa_alg = alg;
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1054 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1055 psa_set_key_algorithm(&attributes, alg);
1056 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if ((status = psa_import_key(&attributes,
1059 key_enc,
1060 PSA_BITS_TO_BYTES(key_bits),
1061 &transform->psa_key_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001062 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
1063 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001064 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if ((status = psa_import_key(&attributes,
1069 key_dec,
1070 PSA_BITS_TO_BYTES(key_bits),
1071 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001072 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
1073 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001074 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001075 }
1076#endif /* MBEDTLS_USE_PSA_CRYPTO */
1077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001079}
1080
Jerry Yu84a6eda2022-11-04 11:17:35 +08001081MBEDTLS_CHECK_RETURN_CRITICAL
1082static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1084 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001085{
1086 psa_key_type_t key_type;
1087 psa_algorithm_t alg;
1088 size_t taglen;
1089 size_t key_bits;
1090 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001093 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001095 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, taglen,
1099 &alg, &key_type, &key_bits);
1100 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001101 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001105
1106 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1107 *iv_len = 12;
1108
1109 return 0;
1110}
1111
Jerry Yu91b560f2022-11-04 14:10:34 +08001112#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001113/*
1114 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001115 * the early application data and handshake messages as described in section 7
1116 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001117 *
Jerry Yue31688b2022-11-22 21:55:56 +08001118 * NOTE: Only one key is generated, the key for the traffic from the client to
1119 * the server. The TLS 1.3 specification does not define a secret and thus
1120 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001121 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001122MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001123static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1124 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001125{
1126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001127 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001128 psa_algorithm_t hash_alg;
1129 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001130 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1131 size_t transcript_len;
Jerry Yuaec08b32022-11-29 15:19:27 +08001132 size_t key_len;
1133 size_t iv_len;
Yanray Wang16c895d2022-12-15 15:14:35 +08001134 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001135
1136 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1137 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1142 if (ret != 0) {
1143 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001144 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001145 }
1146
1147 md_type = ciphersuite_info->mac;
1148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
1150 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1153 transcript,
1154 sizeof(transcript),
1155 &transcript_len);
1156 if (ret != 0) {
1157 MBEDTLS_SSL_DEBUG_RET(1,
1158 "mbedtls_ssl_get_handshake_transcript",
1159 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001160 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001161 }
1162
Jerry Yub094e122022-11-21 13:03:47 +08001163 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001165 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001167 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001169 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001170 }
1171
1172 MBEDTLS_SSL_DEBUG_BUF(
1173 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001174 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001175
1176 /*
1177 * Export client handshake traffic secret
1178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001180 ssl->f_export_keys(
1181 ssl->p_export_keys,
1182 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001183 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001184 hash_len,
1185 handshake->randbytes,
1186 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001188 }
1189
Jerry Yua8771832022-11-21 23:16:54 +08001190 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001192 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 hash_len, traffic_keys->client_write_key, key_len,
1194 traffic_keys->client_write_iv, iv_len);
1195 if (ret != 0) {
1196 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001197 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001198 }
Jerry Yua8771832022-11-21 23:16:54 +08001199 traffic_keys->key_len = key_len;
1200 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1203 traffic_keys->client_write_key,
1204 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1207 traffic_keys->client_write_iv,
1208 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001211
Jerry Yu3d78e082022-11-23 18:26:20 +08001212cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001213 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001214 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001215 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1217 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001218}
1219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001221{
1222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1223 mbedtls_ssl_key_set traffic_keys;
1224 mbedtls_ssl_transform *transform_earlydata = NULL;
1225 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1226
1227 /* Next evolution in key schedule: Establish early_data secret and
1228 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1230 if (ret != 0) {
1231 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1232 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001233 goto cleanup;
1234 }
1235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1237 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001238 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1239 goto cleanup;
1240 }
1241
1242 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 transform_earlydata,
1244 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001245 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 &traffic_keys,
1247 ssl);
1248 if (ret != 0) {
1249 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001250 goto cleanup;
1251 }
1252 handshake->transform_earlydata = transform_earlydata;
1253
1254cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1256 if (ret != 0) {
1257 mbedtls_free(transform_earlydata);
1258 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001261}
1262#endif /* MBEDTLS_SSL_EARLY_DATA */
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001265{
Jerry Yue3131ef2021-09-16 13:14:15 +08001266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001267 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001268 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001269 unsigned char *psk = NULL;
1270 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (handshake->ciphersuite_info == NULL) {
1273 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1274 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001275 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001278#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1280 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1281 if (ret != 0) {
1282 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1283 ret);
1284 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001285 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001286 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001287#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1290 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001291#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001292 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001294#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (ret != 0) {
1296 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1297 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001298 }
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1301 handshake->tls13_master_secrets.early,
1302 PSA_HASH_LENGTH(hash_alg));
1303 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001304}
1305
Yanray Wang05402112022-12-13 18:50:42 +08001306/**
1307 * \brief Compute TLS 1.3 handshake traffic keys.
1308 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001309 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1310 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001311 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001312 *
1313 * \param ssl The SSL context to operate on. This must be in
1314 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001315 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001316 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001317 * keys. This must be writable but may be uninitialized.
1318 *
1319 * \returns \c 0 on success.
1320 * \returns A negative error code on failure.
1321 */
1322MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001323static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1324 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001325{
1326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001327 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001328 psa_algorithm_t hash_alg;
1329 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001330 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001331 size_t transcript_len;
Jerry Yub094e122022-11-21 13:03:47 +08001332 size_t key_len;
1333 size_t iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001334
Jerry Yu435208a2021-10-13 11:22:16 +08001335 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1336 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001337 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001338
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001339 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1342 if (ret != 0) {
1343 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001344 return ret;
1345 }
1346
Jerry Yu435208a2021-10-13 11:22:16 +08001347 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
1350 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1353 transcript,
1354 sizeof(transcript),
1355 &transcript_len);
1356 if (ret != 0) {
1357 MBEDTLS_SSL_DEBUG_RET(1,
1358 "mbedtls_ssl_get_handshake_transcript",
1359 ret);
1360 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001361 }
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 ret = mbedtls_ssl_tls13_derive_handshake_secrets(hash_alg,
1364 handshake->tls13_master_secrets.handshake,
1365 transcript, transcript_len, tls13_hs_secrets);
1366 if (ret != 0) {
1367 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1368 ret);
1369 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001370 }
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1373 tls13_hs_secrets->client_handshake_traffic_secret,
1374 hash_len);
1375 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1376 tls13_hs_secrets->server_handshake_traffic_secret,
1377 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001378
1379 /*
1380 * Export client handshake traffic secret
1381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (ssl->f_export_keys != NULL) {
1383 ssl->f_export_keys(ssl->p_export_keys,
1384 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1385 tls13_hs_secrets->client_handshake_traffic_secret,
1386 hash_len,
1387 handshake->randbytes,
1388 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1389 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 ssl->f_export_keys(ssl->p_export_keys,
1392 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1393 tls13_hs_secrets->server_handshake_traffic_secret,
1394 hash_len,
1395 handshake->randbytes,
1396 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1397 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001398 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 ret = mbedtls_ssl_tls13_make_traffic_keys(hash_alg,
1401 tls13_hs_secrets->client_handshake_traffic_secret,
1402 tls13_hs_secrets->server_handshake_traffic_secret,
1403 hash_len, key_len, iv_len, traffic_keys);
1404 if (ret != 0) {
1405 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001406 goto exit;
1407 }
1408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1410 traffic_keys->client_write_key,
1411 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1414 traffic_keys->server_write_key,
1415 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1418 traffic_keys->client_write_iv,
1419 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1422 traffic_keys->server_write_iv,
1423 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001424
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001425 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001426
1427exit:
1428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001430}
1431
Yanray Wang05402112022-12-13 18:50:42 +08001432/**
1433 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1434 *
1435 * The TLS 1.3 key schedule can be viewed as a simple state machine
1436 * with states Initial -> Early -> Handshake -> Application, and
1437 * this function represents the Early -> Handshake transition.
1438 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001439 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001440 * can be used to derive the handshake traffic keys.
1441 *
1442 * \param ssl The SSL context to operate on. This must be in key schedule
1443 * stage \c Early.
1444 *
1445 * \returns \c 0 on success.
1446 * \returns A negative error code on failure.
1447 */
1448MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001449static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001450{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001452 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001453 psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001455 unsigned char *shared_secret = NULL;
1456 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001457
Ronald Crona2900bc2022-10-20 14:37:35 +02001458#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001459 /*
1460 * Compute ECDHE secret used to compute the handshake secret from which
1461 * client_handshake_traffic_secret and server_handshake_traffic_secret
1462 * are derived in the handshake secret derivation stage.
1463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
1465 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id)) {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001466#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001468 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001469 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 status = psa_get_key_attributes(handshake->ecdh_psa_privkey,
1472 &key_attributes);
1473 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001474 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 }
Ronald Cron3b056202022-10-05 17:20:21 +02001476
1477 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 psa_get_key_bits(&key_attributes));
1479 shared_secret = mbedtls_calloc(1, shared_secret_len);
1480 if (shared_secret == NULL) {
1481 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1482 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001483
Ronald Cron4c7edb22022-10-05 15:37:11 +02001484 status = psa_raw_key_agreement(
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1486 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1487 shared_secret, shared_secret_len, &shared_secret_len);
1488 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001489 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001491 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001492 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 status = psa_destroy_key(handshake->ecdh_psa_privkey);
1495 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001496 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001498 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001499 }
1500
1501 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001502#endif /* MBEDTLS_ECDH_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 } else {
1504 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1505 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001506 }
1507 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001508#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001509
1510 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001511 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
1514 handshake->tls13_master_secrets.early,
1515 shared_secret, shared_secret_len,
1516 handshake->tls13_master_secrets.handshake);
1517 if (ret != 0) {
1518 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001519 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001520 }
1521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1523 handshake->tls13_master_secrets.handshake,
1524 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001525
Ronald Cron3b056202022-10-05 17:20:21 +02001526cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (shared_secret != NULL) {
1528 mbedtls_platform_zeroize(shared_secret, shared_secret_len);
1529 mbedtls_free(shared_secret);
Ronald Cron3b056202022-10-05 17:20:21 +02001530 }
1531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001533}
1534
Yanray Wang05402112022-12-13 18:50:42 +08001535/**
1536 * \brief Compute TLS 1.3 application traffic keys.
1537 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001538 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001539 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001540 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001541 *
1542 * \param ssl The SSL context to operate on. This must be in
1543 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001544 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001545 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001546 * keys. This must be writable but may be uninitialized.
1547 *
1548 * \returns \c 0 on success.
1549 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001550 */
Yanray Wang05402112022-12-13 18:50:42 +08001551MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001552static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 mbedtls_ssl_context *ssl,
1554 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001555{
XiaokangQiana7634982021-10-22 06:32:32 +00001556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001557 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001558
1559 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001560 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001561 &ssl->session_negotiate->app_secrets;
1562
1563 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001564 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001565 size_t transcript_len;
1566
1567 /* Variables relating to the hash for the chosen ciphersuite. */
1568 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001569
1570 psa_algorithm_t hash_alg;
1571 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001572
1573 /* Variables relating to the cipher for the chosen ciphersuite. */
Xiaofei Baib7972842021-11-18 07:29:56 +00001574 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001577
1578 /* Extract basic information about hash and ciphersuite */
1579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1581 &key_len, &iv_len);
1582 if (ret != 0) {
1583 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001584 goto cleanup;
1585 }
1586
XiaokangQian33062842021-11-11 03:37:45 +00001587 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
1590 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001591
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001592 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001593 * to call this at the right time, that is, after the ServerFinished. */
1594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1596 transcript, sizeof(transcript),
1597 &transcript_len);
1598 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001599 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001601
1602 /* Compute application secrets from master secret and transcript hash. */
1603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 ret = mbedtls_ssl_tls13_derive_application_secrets(hash_alg,
1605 handshake->tls13_master_secrets.app,
1606 transcript, transcript_len,
1607 app_secrets);
1608 if (ret != 0) {
1609 MBEDTLS_SSL_DEBUG_RET(1,
1610 "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001611 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001612 }
1613
1614 /* Derive first epoch of IV + Key for application traffic. */
1615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 ret = mbedtls_ssl_tls13_make_traffic_keys(hash_alg,
1617 app_secrets->client_application_traffic_secret_N,
1618 app_secrets->server_application_traffic_secret_N,
1619 hash_len, key_len, iv_len, traffic_keys);
1620 if (ret != 0) {
1621 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001622 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001623 }
1624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1626 app_secrets->client_application_traffic_secret_N,
1627 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1630 app_secrets->server_application_traffic_secret_N,
1631 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001632
XiaokangQianac0385c2021-11-03 06:40:11 +00001633 /*
1634 * Export client/server application traffic secret 0
1635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if (ssl->f_export_keys != NULL) {
1637 ssl->f_export_keys(ssl->p_export_keys,
1638 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1639 app_secrets->client_application_traffic_secret_N, hash_len,
1640 handshake->randbytes,
1641 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1642 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1643 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 ssl->f_export_keys(ssl->p_export_keys,
1646 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1647 app_secrets->server_application_traffic_secret_N, hash_len,
1648 handshake->randbytes,
1649 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1650 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1651 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001652 }
1653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1655 traffic_keys->client_write_key, key_len);
1656 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1657 traffic_keys->server_write_key, key_len);
1658 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1659 traffic_keys->client_write_iv, iv_len);
1660 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1661 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001666 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1668 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1671 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001672}
1673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001675{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001677 mbedtls_ssl_key_set traffic_keys;
1678 mbedtls_ssl_transform *transform_handshake = NULL;
1679 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1680
1681 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001682 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 if (ret != 0) {
1684 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001685 goto cleanup;
1686 }
1687
1688 /* Next evolution in key schedule: Establish handshake secret and
1689 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001690 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001692 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001694 goto cleanup;
1695 }
1696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1698 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001699 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1700 goto cleanup;
1701 }
1702
Jerry Yuef2b98a2022-05-06 16:40:05 +08001703 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 transform_handshake,
1705 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001706 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 &traffic_keys,
1708 ssl);
1709 if (ret != 0) {
1710 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001711 goto cleanup;
1712 }
1713 handshake->transform_handshake = transform_handshake;
1714
1715cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1717 if (ret != 0) {
1718 mbedtls_free(transform_handshake);
1719 }
Jerry Yue110d252022-05-05 10:19:22 +08001720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001722}
1723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001725{
Jerry Yu46bffe02022-09-13 11:25:28 +08001726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001727 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001728 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1729 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001730 size_t transcript_len;
1731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 MBEDTLS_SSL_DEBUG_MSG(2,
1733 ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001734
Jerry Yu46bffe02022-09-13 11:25:28 +08001735 md_type = handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1738 transcript, sizeof(transcript),
1739 &transcript_len);
1740 if (ret != 0) {
1741 return ret;
1742 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001743
1744 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 mbedtls_psa_translate_md(md_type),
1746 handshake->tls13_master_secrets.app,
1747 transcript, transcript_len,
1748 &ssl->session_negotiate->app_secrets);
1749 if (ret != 0) {
1750 return ret;
1751 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001752
Jerry Yuff226982022-04-16 16:52:57 +08001753 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1755 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 MBEDTLS_SSL_DEBUG_BUF(4, "Resumption master secret",
1758 ssl->session_negotiate->app_secrets.resumption_master_secret,
1759 PSA_HASH_LENGTH(mbedtls_psa_translate_md(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 MBEDTLS_SSL_DEBUG_MSG(2,
1762 ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
1763 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001764}
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001767{
1768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1769 mbedtls_ssl_key_set traffic_keys;
1770 mbedtls_ssl_transform *transform_application = NULL;
1771
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001772 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (ret != 0) {
1774 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001775 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001776 goto cleanup;
1777 }
1778
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001779 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 if (ret != 0) {
1781 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001782 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001783 goto cleanup;
1784 }
1785
1786 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1788 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001789 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1790 goto cleanup;
1791 }
1792
1793 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 transform_application,
1795 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001796 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 &traffic_keys,
1798 ssl);
1799 if (ret != 0) {
1800 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001801 goto cleanup;
1802 }
1803
1804 ssl->transform_application = transform_application;
1805
1806cleanup:
1807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1809 if (ret != 0) {
1810 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001811 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001813}
1814
Ronald Cron41a443a2022-10-04 16:38:25 +02001815#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001816int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1817 unsigned char **psk,
1818 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001819{
Jerry Yu40f37712022-07-26 16:58:57 +08001820#if defined(MBEDTLS_USE_PSA_CRYPTO)
1821 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001822 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001823
1824 *psk_len = 0;
1825 *psk = NULL;
1826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1828 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001829 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001830
1831 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1832 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001833 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 }
1835
1836 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1837 *psk = mbedtls_calloc(1, *psk_len);
1838 if (*psk == NULL) {
1839 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1840 }
1841
1842 status = psa_export_key(ssl->handshake->psk_opaque,
1843 (uint8_t *) *psk, *psk_len, psk_len);
1844 if (status != PSA_SUCCESS) {
1845 mbedtls_free((void *) *psk);
1846 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001847 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 }
1849 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001850#else
1851 *psk = ssl->handshake->psk;
1852 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (*psk == NULL) {
1854 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1855 }
1856 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001857#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001858}
Ronald Cron41a443a2022-10-04 16:38:25 +02001859#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001860
Ronald Cron6f135e12021-12-08 16:57:54 +01001861#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */