blob: 9d2cc2cfed44437892e38246e2352c41200f70a1 [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 ( the "License" ); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Hanno Becker58c5cea2020-09-08 10:31:33 +010020#include "common.h"
Hanno Beckerbe9d6642020-08-21 13:20:06 +010021
Ronald Cron6f135e12021-12-08 16:57:54 +010022#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010023
Hanno Beckerbe9d6642020-08-21 13:20:06 +010024#include <stdint.h>
25#include <string.h>
26
Jerry Yue3131ef2021-09-16 13:14:15 +080027#include "mbedtls/hkdf.h"
28#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue110d252022-05-05 10:19:22 +080030#include "mbedtls/platform.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080031
32#include "ssl_misc.h"
33#include "ssl_tls13_keys.h"
Gabor Mezeia3eecd22022-02-09 16:57:26 +010034#include "ssl_tls13_invasive.h"
35
36#include "psa/crypto.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020037#include "md_psa.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080038
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050039#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
40 psa_to_ssl_errors, \
41 psa_generic_status_to_mbedtls)
42
Gilles Peskine449bd832023-01-11 14:50:10 +010043#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010044 .name = string,
45
Xiaofei Bai746f9482021-11-12 08:53:56 +000046struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010047{
48 /* This seems to work in C, despite the string literal being one
49 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010050 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010051};
52
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010053#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010054
Hanno Beckerbe9d6642020-08-21 13:20:06 +010055/*
56 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
57 *
58 * The HkdfLabel is specified in RFC 8446 as follows:
59 *
60 * struct HkdfLabel {
61 * uint16 length; // Length of expanded key material
62 * opaque label<7..255>; // Always prefixed by "tls13 "
63 * opaque context<0..255>; // Usually a communication transcript hash
64 * };
65 *
66 * Parameters:
67 * - desired_length: Length of expanded key material
68 * Even though the standard allows expansion to up to
69 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
70 * 255 Bytes, so we require `desired_length` to be at most
71 * 255. This allows us to save a few Bytes of code by
72 * hardcoding the writing of the high bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000073 * - (label, label_len): label + label length, without "tls13 " prefix
74 * The label length MUST be less than or equal to
75 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
76 * It is the caller's responsibility to ensure this.
77 * All (label, label length) pairs used in TLS 1.3
78 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
79 * - (ctx, ctx_len): context + context length
80 * The context length MUST be less than or equal to
81 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
82 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010083 * - dst: Target buffer for HkdfLabel structure,
84 * This MUST be a writable buffer of size
85 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000086 * - dst_len: Pointer at which to store the actual length of
87 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010088 */
89
Xiaofei Baid25fab62021-12-02 06:36:27 +000090static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
93 (2 /* expansion length */ \
94 + 1 /* label length */ \
95 + label_len \
96 + 1 /* context length */ \
97 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010098
99#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
100 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 sizeof(tls13_label_prefix) + \
102 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
103 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100104
Xiaofei Bai746f9482021-11-12 08:53:56 +0000105static void ssl_tls13_hkdf_encode_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 size_t desired_length,
107 const unsigned char *label, size_t label_len,
108 const unsigned char *ctx, size_t ctx_len,
109 unsigned char *dst, size_t *dst_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100110{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100111 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000112 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100113 size_t total_hkdf_lbl_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, ctx_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100115
116 unsigned char *p = dst;
117
Hanno Becker531fe302020-09-16 09:45:27 +0100118 /* Add the size of the expanded key material.
119 * We're hardcoding the high byte to 0 here assuming that we never use
120 * TLS 1.3 HKDF key expansion to more than 255 Bytes. */
121#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
Xiaofei Bai746f9482021-11-12 08:53:56 +0000122#error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
Hanno Becker531fe302020-09-16 09:45:27 +0100124#endif
125
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100126 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100128
129 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 *p++ = MBEDTLS_BYTE_0(total_label_len);
131 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000132 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000134 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100135
136 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 *p++ = MBEDTLS_BYTE_0(ctx_len);
138 if (ctx_len != 0) {
139 memcpy(p, ctx, ctx_len);
140 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100141
142 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000143 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100144}
145
Xiaofei Bai746f9482021-11-12 08:53:56 +0000146int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 psa_algorithm_t hash_alg,
148 const unsigned char *secret, size_t secret_len,
149 const unsigned char *label, size_t label_len,
150 const unsigned char *ctx, size_t ctx_len,
151 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100152{
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200154 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200155 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
156 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200157 psa_key_derivation_operation_t operation =
158 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 if (label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100161 /* Should never happen since this is an internal
162 * function, and we know statically which labels
163 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100165 }
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100168 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100170 }
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100173 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100175 }
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (!PSA_ALG_IS_HASH(hash_alg)) {
178 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
179 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 ssl_tls13_hkdf_encode_label(buf_len,
182 label, label_len,
183 ctx, ctx_len,
184 hkdf_label,
185 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (status != PSA_SUCCESS) {
190 goto cleanup;
191 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 status = psa_key_derivation_input_bytes(&operation,
194 PSA_KEY_DERIVATION_INPUT_SECRET,
195 secret,
196 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (status != PSA_SUCCESS) {
199 goto cleanup;
200 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 status = psa_key_derivation_input_bytes(&operation,
203 PSA_KEY_DERIVATION_INPUT_INFO,
204 hkdf_label,
205 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (status != PSA_SUCCESS) {
208 goto cleanup;
209 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 status = psa_key_derivation_output_bytes(&operation,
212 buf,
213 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (status != PSA_SUCCESS) {
216 goto cleanup;
217 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200218
219cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 abort_status = psa_key_derivation_abort(&operation);
221 status = (status == PSA_SUCCESS ? abort_status : status);
222 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500223 return PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100224}
225
Jerry Yua5db6c02022-11-23 18:08:04 +0800226MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800227static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 psa_algorithm_t hash_alg,
229 const unsigned char *secret, size_t secret_len,
230 unsigned char *key, size_t key_len,
231 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800232{
233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
234
Jerry Yuaec08b32022-11-29 15:19:27 +0800235 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 hash_alg,
237 secret, secret_len,
238 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
239 NULL, 0,
240 key, key_len);
241 if (ret != 0) {
242 return ret;
243 }
Jerry Yua8771832022-11-21 23:16:54 +0800244
Jerry Yuaec08b32022-11-29 15:19:27 +0800245 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 hash_alg,
247 secret, secret_len,
248 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
249 NULL, 0,
250 iv, iv_len);
251 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800252}
253
Hanno Becker3385a4d2020-08-21 13:03:34 +0100254/*
255 * The traffic keying material is generated from the following inputs:
256 *
257 * - One secret value per sender.
258 * - A purpose value indicating the specific value being generated
259 * - The desired lengths of key and IV.
260 *
261 * The expansion itself is based on HKDF:
262 *
263 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
264 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
265 *
266 * [sender] denotes the sending side and the Secret value is provided
267 * by the function caller. Note that we generate server and client side
268 * keys in a single function call.
269 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000270int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 psa_algorithm_t hash_alg,
272 const unsigned char *client_secret,
273 const unsigned char *server_secret, size_t secret_len,
274 size_t key_len, size_t iv_len,
275 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100276{
277 int ret = 0;
278
Jerry Yua8771832022-11-21 23:16:54 +0800279 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 hash_alg, client_secret, secret_len,
281 keys->client_write_key, key_len,
282 keys->client_write_iv, iv_len);
283 if (ret != 0) {
284 return ret;
285 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100286
Jerry Yua8771832022-11-21 23:16:54 +0800287 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 hash_alg, server_secret, secret_len,
289 keys->server_write_key, key_len,
290 keys->server_write_iv, iv_len);
291 if (ret != 0) {
292 return ret;
293 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100294
Hanno Becker493ea7f2020-09-08 11:01:00 +0100295 keys->key_len = key_len;
296 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100299}
300
Xiaofei Bai746f9482021-11-12 08:53:56 +0000301int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 psa_algorithm_t hash_alg,
303 const unsigned char *secret, size_t secret_len,
304 const unsigned char *label, size_t label_len,
305 const unsigned char *ctx, size_t ctx_len,
306 int ctx_hashed,
307 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100308{
309 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
311 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
315 PSA_HASH_LENGTH(hash_alg), &ctx_len);
316 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500317 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100318 return ret;
319 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 } else {
321 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100322 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100323 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100324 * Let's double-check nonetheless to not run at the risk
325 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100327 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100330 }
331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
333 secret, secret_len,
334 label, label_len,
335 hashed_context, ctx_len,
336 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100337
Hanno Beckerb35d5222020-08-21 13:27:44 +0100338}
339
Xiaofei Bai746f9482021-11-12 08:53:56 +0000340int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 psa_algorithm_t hash_alg,
342 const unsigned char *secret_old,
343 const unsigned char *input, size_t input_len,
344 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100345{
346 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
348 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200349 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
351 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200352 const unsigned char *l_input = NULL;
353 size_t l_input_len;
354
Przemek Stekield5ae3652022-05-13 12:10:08 +0200355 psa_key_derivation_operation_t operation =
356 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (!PSA_ALG_IS_HASH(hash_alg)) {
359 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
360 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100363
364 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100365 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000367 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 hash_alg,
369 secret_old, hlen,
370 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
371 NULL, 0, /* context */
372 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
373 tmp_secret, hlen);
374 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100375 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100377 }
378
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200379 ret = 0;
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200382 l_input = input;
383 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200385 l_input = all_zeroes_input;
386 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100387 }
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 status = psa_key_derivation_setup(&operation,
390 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (status != PSA_SUCCESS) {
393 goto cleanup;
394 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 status = psa_key_derivation_input_bytes(&operation,
397 PSA_KEY_DERIVATION_INPUT_SALT,
398 tmp_secret,
399 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (status != PSA_SUCCESS) {
402 goto cleanup;
403 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 status = psa_key_derivation_input_bytes(&operation,
406 PSA_KEY_DERIVATION_INPUT_SECRET,
407 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (status != PSA_SUCCESS) {
410 goto cleanup;
411 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 status = psa_key_derivation_output_bytes(&operation,
414 secret_new,
415 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (status != PSA_SUCCESS) {
418 goto cleanup;
419 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421cleanup:
422 abort_status = psa_key_derivation_abort(&operation);
423 status = (status == PSA_SUCCESS ? abort_status : status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500424 ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
426 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100427}
428
Xiaofei Bai746f9482021-11-12 08:53:56 +0000429int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 psa_algorithm_t hash_alg,
431 unsigned char const *early_secret,
432 unsigned char const *transcript, size_t transcript_len,
433 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100434{
435 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100437
438 /* We should never call this function with an unknown hash,
439 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (!PSA_ALG_IS_HASH(hash_alg)) {
441 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
442 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100443
444 /*
445 * 0
446 * |
447 * v
448 * PSK -> HKDF-Extract = Early Secret
449 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100450 * +-----> Derive-Secret(., "c e traffic", ClientHello)
451 * | = client_early_traffic_secret
452 * |
453 * +-----> Derive-Secret(., "e exp master", ClientHello)
454 * | = early_exporter_master_secret
455 * v
456 */
457
458 /* Create client_early_traffic_secret */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000459 ret = mbedtls_ssl_tls13_derive_secret(
460 hash_alg,
461 early_secret, hash_len,
462 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
463 transcript, transcript_len,
464 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
465 derived->client_early_traffic_secret,
466 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (ret != 0) {
468 return ret;
469 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100470
471 /* Create early exporter */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000472 ret = mbedtls_ssl_tls13_derive_secret(
473 hash_alg,
474 early_secret, hash_len,
475 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
476 transcript, transcript_len,
477 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
478 derived->early_exporter_master_secret,
479 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (ret != 0) {
481 return ret;
482 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100485}
486
Xiaofei Bai746f9482021-11-12 08:53:56 +0000487int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 psa_algorithm_t hash_alg,
489 unsigned char const *handshake_secret,
490 unsigned char const *transcript, size_t transcript_len,
491 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100492{
493 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100495
496 /* We should never call this function with an unknown hash,
497 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (!PSA_ALG_IS_HASH(hash_alg)) {
499 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
500 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100501
502 /*
503 *
504 * Handshake Secret
505 * |
506 * +-----> Derive-Secret( ., "c hs traffic",
507 * | ClientHello...ServerHello )
508 * | = client_handshake_traffic_secret
509 * |
510 * +-----> Derive-Secret( ., "s hs traffic",
511 * | ClientHello...ServerHello )
512 * | = server_handshake_traffic_secret
513 *
514 */
515
516 /*
517 * Compute client_handshake_traffic_secret with
518 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
519 */
520
Xiaokang Qian123cde82023-03-29 06:54:51 +0000521 ret = mbedtls_ssl_tls13_derive_secret(
522 hash_alg,
523 handshake_secret, hash_len,
524 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
525 transcript, transcript_len,
526 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
527 derived->client_handshake_traffic_secret,
528 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (ret != 0) {
530 return ret;
531 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100532
533 /*
534 * Compute server_handshake_traffic_secret with
535 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
536 */
537
Xiaokang Qian123cde82023-03-29 06:54:51 +0000538 ret = mbedtls_ssl_tls13_derive_secret(
539 hash_alg,
540 handshake_secret, hash_len,
541 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
542 transcript, transcript_len,
543 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
544 derived->server_handshake_traffic_secret,
545 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (ret != 0) {
547 return ret;
548 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100551}
552
Xiaofei Bai746f9482021-11-12 08:53:56 +0000553int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 psa_algorithm_t hash_alg,
555 unsigned char const *application_secret,
556 unsigned char const *transcript, size_t transcript_len,
557 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100558{
559 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100561
562 /* We should never call this function with an unknown hash,
563 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (!PSA_ALG_IS_HASH(hash_alg)) {
565 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
566 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100567
568 /* Generate {client,server}_application_traffic_secret_0
569 *
570 * Master Secret
571 * |
572 * +-----> Derive-Secret( ., "c ap traffic",
573 * | ClientHello...server Finished )
574 * | = client_application_traffic_secret_0
575 * |
576 * +-----> Derive-Secret( ., "s ap traffic",
577 * | ClientHello...Server Finished )
578 * | = server_application_traffic_secret_0
579 * |
580 * +-----> Derive-Secret( ., "exp master",
581 * | ClientHello...server Finished)
582 * | = exporter_master_secret
583 *
584 */
585
Xiaokang Qian123cde82023-03-29 06:54:51 +0000586 ret = mbedtls_ssl_tls13_derive_secret(
587 hash_alg,
588 application_secret, hash_len,
589 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
590 transcript, transcript_len,
591 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
592 derived->client_application_traffic_secret_N,
593 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (ret != 0) {
595 return ret;
596 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100597
Xiaokang Qian123cde82023-03-29 06:54:51 +0000598 ret = mbedtls_ssl_tls13_derive_secret(
599 hash_alg,
600 application_secret, hash_len,
601 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
602 transcript, transcript_len,
603 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
604 derived->server_application_traffic_secret_N,
605 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (ret != 0) {
607 return ret;
608 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100609
Xiaokang Qian123cde82023-03-29 06:54:51 +0000610 ret = mbedtls_ssl_tls13_derive_secret(
611 hash_alg,
612 application_secret, hash_len,
613 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
614 transcript, transcript_len,
615 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
616 derived->exporter_master_secret,
617 hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (ret != 0) {
619 return ret;
620 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100623}
624
625/* Generate resumption_master_secret for use with the ticket exchange.
626 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000627 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100628 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000629int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 psa_algorithm_t hash_alg,
631 unsigned char const *application_secret,
632 unsigned char const *transcript, size_t transcript_len,
633 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100634{
635 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100637
638 /* We should never call this function with an unknown hash,
639 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (!PSA_ALG_IS_HASH(hash_alg)) {
641 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
642 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100643
Xiaokang Qian123cde82023-03-29 06:54:51 +0000644 ret = mbedtls_ssl_tls13_derive_secret(
645 hash_alg,
646 application_secret, hash_len,
647 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
648 transcript, transcript_len,
649 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
650 derived->resumption_master_secret,
651 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (ret != 0) {
654 return ret;
655 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100658}
659
Yanray Wang05402112022-12-13 18:50:42 +0800660/**
661 * \brief Transition into application stage of TLS 1.3 key schedule.
662 *
663 * The TLS 1.3 key schedule can be viewed as a simple state machine
664 * with states Initial -> Early -> Handshake -> Application, and
665 * this function represents the Handshake -> Application transition.
666 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800667 * In the handshake stage, ssl_tls13_generate_application_keys()
Yanray Wang05402112022-12-13 18:50:42 +0800668 * can be used to derive the handshake traffic keys.
669 *
670 * \param ssl The SSL context to operate on. This must be in key schedule
671 * stage \c Handshake.
672 *
673 * \returns \c 0 on success.
674 * \returns A negative error code on failure.
675 */
676MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +0800677static int ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000678{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000680 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200681 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000683
684 /*
685 * Compute MasterSecret
686 */
Xiaokang Qian123cde82023-03-29 06:54:51 +0000687 ret = mbedtls_ssl_tls13_evolve_secret(
688 hash_alg,
689 handshake->tls13_master_secrets.handshake,
690 NULL, 0,
691 handshake->tls13_master_secrets.app);
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (ret != 0) {
693 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
694 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000695 }
696
Xiaokang Qian123cde82023-03-29 06:54:51 +0000697 MBEDTLS_SSL_DEBUG_BUF(
698 4, "Master secret",
699 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000702}
703
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200704MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100705static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
706 unsigned char const *base_key,
707 unsigned char const *transcript,
708 unsigned char *dst,
709 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100710{
Gabor Mezei07732f72022-03-26 17:04:19 +0100711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100715 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100716 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100717 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100718
719 /* We should never call this function with an unknown hash,
720 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (!PSA_ALG_IS_HASH(hash_alg)) {
722 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
723 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100724
725 /* TLS 1.3 Finished message
726 *
727 * struct {
728 * opaque verify_data[Hash.length];
729 * } Finished;
730 *
731 * verify_data =
732 * HMAC( finished_key,
733 * Hash( Handshake Context +
734 * Certificate* +
735 * CertificateVerify* )
736 * )
737 *
738 * finished_key =
739 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
740 */
741
Xiaofei Bai746f9482021-11-12 08:53:56 +0000742 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 hash_alg, base_key, hash_len,
744 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
745 NULL, 0,
746 finished_key, hash_len);
747 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100748 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100749 }
750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 alg = PSA_ALG_HMAC(hash_alg);
752 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
753 psa_set_key_algorithm(&attributes, alg);
754 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
755
756 status = psa_import_key(&attributes, finished_key, hash_len, &key);
757 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500758 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 goto exit;
760 }
761
762 status = psa_mac_compute(key, alg, transcript, hash_len,
763 dst, hash_len, dst_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500764 ret = PSA_TO_MBEDTLS_ERR(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100765
766exit:
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 status = psa_destroy_key(key);
769 if (ret == 0) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500770 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100776}
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
779 unsigned char *dst,
780 size_t dst_len,
781 size_t *actual_len,
782 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000783{
XiaokangQiana7634982021-10-22 06:32:32 +0000784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000785
XiaokangQianaaa0e192021-11-10 03:07:04 +0000786 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000787 size_t transcript_len;
788
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800789 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800790 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800791 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800793
794 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100795
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200796 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 ssl->handshake->ciphersuite_info->mac);
798 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800803 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
805 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800806 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800808 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800811 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
812 goto exit;
813 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
816 transcript, sizeof(transcript),
817 &transcript_len);
818 if (ret != 0) {
819 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000820 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000821 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000823
Xiaokang Qian123cde82023-03-29 06:54:51 +0000824 ret = ssl_tls13_calc_finished_core(hash_alg, base_key,
825 transcript, dst, actual_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000827 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
831 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000832
833exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800834 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 mbedtls_platform_zeroize(base_key, base_key_len);
836 mbedtls_platform_zeroize(transcript, sizeof(transcript));
837 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000838}
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
841 const psa_algorithm_t hash_alg,
842 unsigned char const *psk, size_t psk_len,
843 int psk_type,
844 unsigned char const *transcript,
845 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100846{
847 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100848 unsigned char binder_key[PSA_MAC_MAX_SIZE];
849 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100851 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100852
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100853#if !defined(MBEDTLS_DEBUG_C)
854 ssl = NULL; /* make sure we don't use it except for debug */
855 ((void) ssl);
856#endif
857
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100858 /* We should never call this function with an unknown hash,
859 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (!PSA_ALG_IS_HASH(hash_alg)) {
861 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
862 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100863
864 /*
865 * 0
866 * |
867 * v
868 * PSK -> HKDF-Extract = Early Secret
869 * |
870 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
871 * | = binder_key
872 * v
873 */
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
876 NULL, /* Old secret */
877 psk, psk_len, /* Input */
878 early_secret);
879 if (ret != 0) {
880 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100881 goto exit;
882 }
883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
885 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000888 ret = mbedtls_ssl_tls13_derive_secret(
889 hash_alg,
890 early_secret, hash_len,
891 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
892 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
893 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
895 } else {
Xiaokang Qian123cde82023-03-29 06:54:51 +0000896 ret = mbedtls_ssl_tls13_derive_secret(
897 hash_alg,
898 early_secret, hash_len,
899 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
900 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
901 binder_key, hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100903 }
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (ret != 0) {
906 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100907 goto exit;
908 }
909
910 /*
911 * The binding_value is computed in the same way as the Finished message
912 * but with the BaseKey being the binder_key.
913 */
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
916 result, &actual_len);
917 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100918 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100922
923exit:
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
926 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
927 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100928}
929
Xiaokang Qian123cde82023-03-29 06:54:51 +0000930int mbedtls_ssl_tls13_populate_transform(
931 mbedtls_ssl_transform *transform,
932 int endpoint, int ciphersuite,
933 mbedtls_ssl_key_set const *traffic_keys,
934 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000935{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100936#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000937 int ret;
938 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200939#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000940 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
941 unsigned char const *key_enc;
942 unsigned char const *iv_enc;
943 unsigned char const *key_dec;
944 unsigned char const *iv_dec;
945
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100946#if defined(MBEDTLS_USE_PSA_CRYPTO)
947 psa_key_type_t key_type;
948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
949 psa_algorithm_t alg;
950 size_t key_bits;
951 psa_status_t status = PSA_SUCCESS;
952#endif
953
Hanno Beckerc94060c2021-03-22 07:50:44 +0000954#if !defined(MBEDTLS_DEBUG_C)
955 ssl = NULL; /* make sure we don't use it except for those cases */
956 (void) ssl;
957#endif
958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
960 if (ciphersuite_info == NULL) {
961 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
962 ciphersuite));
963 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100964 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000965
Neil Armstronga8093f52022-05-04 17:44:05 +0200966#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
968 if (cipher_info == NULL) {
969 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
970 ciphersuite_info->cipher));
971 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000972 }
973
974 /*
975 * Setup cipher contexts in target transform
976 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
978 cipher_info)) != 0) {
979 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
980 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000981 }
982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
984 cipher_info)) != 0) {
985 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
986 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000987 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100988#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000989
990#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000992 key_enc = traffic_keys->server_write_key;
993 key_dec = traffic_keys->client_write_key;
994 iv_enc = traffic_keys->server_write_iv;
995 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000997#endif /* MBEDTLS_SSL_SRV_C */
998#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +00001000 key_enc = traffic_keys->client_write_key;
1001 key_dec = traffic_keys->server_write_key;
1002 iv_enc = traffic_keys->client_write_iv;
1003 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +00001005#endif /* MBEDTLS_SSL_CLI_C */
1006 {
1007 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001009 }
1010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
1012 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +00001013
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001014#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
Dave Rodgmanef2f3692023-06-24 17:31:08 +01001016 key_enc, mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 MBEDTLS_ENCRYPT)) != 0) {
1018 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1019 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001020 }
1021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
Dave Rodgmanef2f3692023-06-24 17:31:08 +01001023 key_dec, mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 MBEDTLS_DECRYPT)) != 0) {
1025 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
1026 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001027 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001028#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001029
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001030 /*
1031 * Setup other fields in SSL transform
1032 */
1033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001035 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001037 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001039
1040 transform->ivlen = traffic_keys->iv_len;
1041 transform->maclen = 0;
1042 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001043 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001044
1045 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001046 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001047 * type-extended and padded plaintext is therefore the padding
1048 * granularity. */
1049 transform->minlen =
1050 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1051
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001052#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001053 /*
1054 * Setup psa keys and alg
1055 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
1057 transform->taglen,
1058 &alg,
1059 &key_type,
1060 &key_bits)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001061 MBEDTLS_SSL_DEBUG_RET(
1062 1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001063 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001064 }
1065
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001066 transform->psa_alg = alg;
1067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1069 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1070 psa_set_key_algorithm(&attributes, alg);
1071 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if ((status = psa_import_key(&attributes,
1074 key_enc,
1075 PSA_BITS_TO_BYTES(key_bits),
1076 &transform->psa_key_enc)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001077 MBEDTLS_SSL_DEBUG_RET(
1078 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001079 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001080 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if ((status = psa_import_key(&attributes,
1085 key_dec,
1086 PSA_BITS_TO_BYTES(key_bits),
1087 &transform->psa_key_dec)) != PSA_SUCCESS) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001088 MBEDTLS_SSL_DEBUG_RET(
1089 1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001090 return PSA_TO_MBEDTLS_ERR(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001091 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001092 }
1093#endif /* MBEDTLS_USE_PSA_CRYPTO */
1094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001096}
1097
Jerry Yu84a6eda2022-11-04 11:17:35 +08001098MBEDTLS_CHECK_RETURN_CRITICAL
1099static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1101 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001102{
1103 psa_key_type_t key_type;
1104 psa_algorithm_t alg;
1105 size_t taglen;
1106 size_t key_bits;
1107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001110 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001112 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, taglen,
1116 &alg, &key_type, &key_bits);
1117 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001118 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001122
1123 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1124 *iv_len = 12;
1125
1126 return 0;
1127}
1128
Jerry Yu91b560f2022-11-04 14:10:34 +08001129#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001130/*
1131 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001132 * the early application data and handshake messages as described in section 7
1133 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001134 *
Jerry Yue31688b2022-11-22 21:55:56 +08001135 * NOTE: Only one key is generated, the key for the traffic from the client to
1136 * the server. The TLS 1.3 specification does not define a secret and thus
1137 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001138 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001139MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001140static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1141 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001142{
1143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001144 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001145 psa_algorithm_t hash_alg;
1146 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001147 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1148 size_t transcript_len;
Jerry Yuaec08b32022-11-29 15:19:27 +08001149 size_t key_len;
1150 size_t iv_len;
Yanray Wang16c895d2022-12-15 15:14:35 +08001151 mbedtls_ssl_tls13_early_secrets tls13_early_secrets;
Jerry Yu91b560f2022-11-04 14:10:34 +08001152
1153 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001154 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1155 handshake->ciphersuite_info;
Jerry Yu91b560f2022-11-04 14:10:34 +08001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1160 if (ret != 0) {
1161 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001162 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001163 }
1164
1165 md_type = ciphersuite_info->mac;
1166
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001167 hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1171 transcript,
1172 sizeof(transcript),
1173 &transcript_len);
1174 if (ret != 0) {
1175 MBEDTLS_SSL_DEBUG_RET(1,
1176 "mbedtls_ssl_get_handshake_transcript",
1177 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001178 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001179 }
1180
Jerry Yub094e122022-11-21 13:03:47 +08001181 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 hash_alg, handshake->tls13_master_secrets.early,
Yanray Wangbae9e742022-12-13 14:58:45 +08001183 transcript, transcript_len, &tls13_early_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001185 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001187 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001188 }
1189
1190 MBEDTLS_SSL_DEBUG_BUF(
1191 4, "Client early traffic secret",
Yanray Wangbae9e742022-12-13 14:58:45 +08001192 tls13_early_secrets.client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001193
1194 /*
1195 * Export client handshake traffic secret
1196 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001198 ssl->f_export_keys(
1199 ssl->p_export_keys,
1200 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
Yanray Wangbae9e742022-12-13 14:58:45 +08001201 tls13_early_secrets.client_early_traffic_secret,
Jerry Yub094e122022-11-21 13:03:47 +08001202 hash_len,
1203 handshake->randbytes,
1204 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001206 }
1207
Jerry Yua8771832022-11-21 23:16:54 +08001208 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 hash_alg,
Yanray Wangbae9e742022-12-13 14:58:45 +08001210 tls13_early_secrets.client_early_traffic_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 hash_len, traffic_keys->client_write_key, key_len,
1212 traffic_keys->client_write_iv, iv_len);
1213 if (ret != 0) {
1214 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001215 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001216 }
Jerry Yua8771832022-11-21 23:16:54 +08001217 traffic_keys->key_len = key_len;
1218 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1221 traffic_keys->client_write_key,
1222 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1225 traffic_keys->client_write_iv,
1226 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001229
Jerry Yu3d78e082022-11-23 18:26:20 +08001230cleanup:
Yanray Wang16c895d2022-12-15 15:14:35 +08001231 /* Erase early secrets and transcript */
Jerry Yu3d78e082022-11-23 18:26:20 +08001232 mbedtls_platform_zeroize(
Yanray Wangbae9e742022-12-13 14:58:45 +08001233 &tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1235 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001236}
1237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001239{
1240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1241 mbedtls_ssl_key_set traffic_keys;
1242 mbedtls_ssl_transform *transform_earlydata = NULL;
1243 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1244
1245 /* Next evolution in key schedule: Establish early_data secret and
1246 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1248 if (ret != 0) {
1249 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1250 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001251 goto cleanup;
1252 }
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1255 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001256 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1257 goto cleanup;
1258 }
1259
1260 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 transform_earlydata,
1262 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001263 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 &traffic_keys,
1265 ssl);
1266 if (ret != 0) {
1267 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001268 goto cleanup;
1269 }
1270 handshake->transform_earlydata = transform_earlydata;
1271
1272cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1274 if (ret != 0) {
1275 mbedtls_free(transform_earlydata);
1276 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001279}
1280#endif /* MBEDTLS_SSL_EARLY_DATA */
1281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001283{
Jerry Yue3131ef2021-09-16 13:14:15 +08001284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001285 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001286 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001287 unsigned char *psk = NULL;
1288 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (handshake->ciphersuite_info == NULL) {
1291 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1292 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001293 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001294
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001295 hash_alg = mbedtls_md_psa_alg_from_type(handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001296#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1298 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1299 if (ret != 0) {
1300 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1301 ret);
1302 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001303 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001304 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001305#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1308 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001309#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001310 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001312#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (ret != 0) {
1314 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1315 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001316 }
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1319 handshake->tls13_master_secrets.early,
1320 PSA_HASH_LENGTH(hash_alg));
1321 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001322}
1323
Yanray Wang05402112022-12-13 18:50:42 +08001324/**
1325 * \brief Compute TLS 1.3 handshake traffic keys.
1326 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001327 * ssl_tls13_generate_handshake_keys() generates keys necessary for
1328 * protecting the handshake messages, as described in Section 7 of
Yanray Wanga12cecb2023-02-01 14:29:47 +08001329 * RFC 8446.
Yanray Wang05402112022-12-13 18:50:42 +08001330 *
1331 * \param ssl The SSL context to operate on. This must be in
1332 * key schedule stage \c Handshake, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001333 * ssl_tls13_key_schedule_stage_handshake().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001334 * \param traffic_keys The address at which to store the handshake traffic
Yanray Wang05402112022-12-13 18:50:42 +08001335 * keys. This must be writable but may be uninitialized.
1336 *
1337 * \returns \c 0 on success.
1338 * \returns A negative error code on failure.
1339 */
1340MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001341static int ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1342 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001343{
1344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001345 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001346 psa_algorithm_t hash_alg;
1347 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001348 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001349 size_t transcript_len;
Jerry Yub094e122022-11-21 13:03:47 +08001350 size_t key_len;
1351 size_t iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001352
Jerry Yu435208a2021-10-13 11:22:16 +08001353 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaokang Qian123cde82023-03-29 06:54:51 +00001354 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1355 handshake->ciphersuite_info;
1356 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
1357 &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001358
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001359 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1362 if (ret != 0) {
1363 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001364 return ret;
1365 }
1366
Jerry Yu435208a2021-10-13 11:22:16 +08001367 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001368
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001369 hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1373 transcript,
1374 sizeof(transcript),
1375 &transcript_len);
1376 if (ret != 0) {
1377 MBEDTLS_SSL_DEBUG_RET(1,
1378 "mbedtls_ssl_get_handshake_transcript",
1379 ret);
1380 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001381 }
1382
Xiaokang Qian123cde82023-03-29 06:54:51 +00001383 ret = mbedtls_ssl_tls13_derive_handshake_secrets(
1384 hash_alg, handshake->tls13_master_secrets.handshake,
1385 transcript, transcript_len, tls13_hs_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 if (ret != 0) {
1387 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1388 ret);
1389 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001390 }
1391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1393 tls13_hs_secrets->client_handshake_traffic_secret,
1394 hash_len);
1395 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1396 tls13_hs_secrets->server_handshake_traffic_secret,
1397 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001398
1399 /*
1400 * Export client handshake traffic secret
1401 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001403 ssl->f_export_keys(
1404 ssl->p_export_keys,
1405 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1406 tls13_hs_secrets->client_handshake_traffic_secret,
1407 hash_len,
1408 handshake->randbytes,
1409 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1410 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001411
Xiaokang Qian123cde82023-03-29 06:54:51 +00001412 ssl->f_export_keys(
1413 ssl->p_export_keys,
1414 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1415 tls13_hs_secrets->server_handshake_traffic_secret,
1416 hash_len,
1417 handshake->randbytes,
1418 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1419 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001420 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001421
Xiaokang Qian123cde82023-03-29 06:54:51 +00001422 ret = mbedtls_ssl_tls13_make_traffic_keys(
1423 hash_alg,
1424 tls13_hs_secrets->client_handshake_traffic_secret,
1425 tls13_hs_secrets->server_handshake_traffic_secret,
1426 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (ret != 0) {
1428 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001429 goto exit;
1430 }
1431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1433 traffic_keys->client_write_key,
1434 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1437 traffic_keys->server_write_key,
1438 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1441 traffic_keys->client_write_iv,
1442 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1445 traffic_keys->server_write_iv,
1446 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001447
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001448 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001449
1450exit:
1451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001453}
1454
Yanray Wang05402112022-12-13 18:50:42 +08001455/**
1456 * \brief Transition into handshake stage of TLS 1.3 key schedule.
1457 *
1458 * The TLS 1.3 key schedule can be viewed as a simple state machine
1459 * with states Initial -> Early -> Handshake -> Application, and
1460 * this function represents the Early -> Handshake transition.
1461 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001462 * In the handshake stage, ssl_tls13_generate_handshake_keys()
Yanray Wang05402112022-12-13 18:50:42 +08001463 * can be used to derive the handshake traffic keys.
1464 *
1465 * \param ssl The SSL context to operate on. This must be in key schedule
1466 * stage \c Early.
1467 *
1468 * \returns \c 0 on success.
1469 * \returns A negative error code on failure.
1470 */
1471MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001472static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001473{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001475 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001476 psa_algorithm_t const hash_alg = mbedtls_md_psa_alg_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001478 unsigned char *shared_secret = NULL;
1479 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001480
Ronald Crona2900bc2022-10-20 14:37:35 +02001481#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001482 /*
1483 * Compute ECDHE secret used to compute the handshake secret from which
1484 * client_handshake_traffic_secret and server_handshake_traffic_secret
1485 * are derived in the handshake secret derivation stage.
1486 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
1488 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id)) {
Valerio Setti080a22b2023-03-20 15:22:47 +01001489#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001491 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001492 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 status = psa_get_key_attributes(handshake->ecdh_psa_privkey,
1495 &key_attributes);
1496 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001497 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 }
Ronald Cron3b056202022-10-05 17:20:21 +02001499
1500 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 psa_get_key_bits(&key_attributes));
1502 shared_secret = mbedtls_calloc(1, shared_secret_len);
1503 if (shared_secret == NULL) {
1504 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1505 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001506
Ronald Cron4c7edb22022-10-05 15:37:11 +02001507 status = psa_raw_key_agreement(
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1509 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1510 shared_secret, shared_secret_len, &shared_secret_len);
1511 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001512 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001514 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001515 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 status = psa_destroy_key(handshake->ecdh_psa_privkey);
1518 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001519 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001521 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001522 }
1523
1524 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti080a22b2023-03-20 15:22:47 +01001525#endif /* PSA_WANT_ALG_ECDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 } else {
1527 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1528 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001529 }
1530 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001531#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001532
1533 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001534 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001535 */
Xiaokang Qian123cde82023-03-29 06:54:51 +00001536 ret = mbedtls_ssl_tls13_evolve_secret(
1537 hash_alg, handshake->tls13_master_secrets.early,
1538 shared_secret, shared_secret_len,
1539 handshake->tls13_master_secrets.handshake);
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (ret != 0) {
1541 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001542 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001543 }
1544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1546 handshake->tls13_master_secrets.handshake,
1547 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001548
Ronald Cron3b056202022-10-05 17:20:21 +02001549cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (shared_secret != NULL) {
1551 mbedtls_platform_zeroize(shared_secret, shared_secret_len);
1552 mbedtls_free(shared_secret);
Ronald Cron3b056202022-10-05 17:20:21 +02001553 }
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001556}
1557
Yanray Wang05402112022-12-13 18:50:42 +08001558/**
1559 * \brief Compute TLS 1.3 application traffic keys.
1560 *
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001561 * ssl_tls13_generate_application_keys() generates application traffic
Yanray Wanga12cecb2023-02-01 14:29:47 +08001562 * keys, since any record following a 1-RTT Finished message MUST be
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001563 * encrypted under the application traffic key.
Yanray Wang05402112022-12-13 18:50:42 +08001564 *
1565 * \param ssl The SSL context to operate on. This must be in
1566 * key schedule stage \c Application, see
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001567 * ssl_tls13_key_schedule_stage_application().
Yanray Wanga12cecb2023-02-01 14:29:47 +08001568 * \param traffic_keys The address at which to store the application traffic
Yanray Wang05402112022-12-13 18:50:42 +08001569 * keys. This must be writable but may be uninitialized.
1570 *
1571 * \returns \c 0 on success.
1572 * \returns A negative error code on failure.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001573 */
Yanray Wang05402112022-12-13 18:50:42 +08001574MBEDTLS_CHECK_RETURN_CRITICAL
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001575static int ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 mbedtls_ssl_context *ssl,
1577 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001578{
XiaokangQiana7634982021-10-22 06:32:32 +00001579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001580 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001581
1582 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001583 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001584 &ssl->session_negotiate->app_secrets;
1585
1586 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001587 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001588 size_t transcript_len;
1589
1590 /* Variables relating to the hash for the chosen ciphersuite. */
1591 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001592
1593 psa_algorithm_t hash_alg;
1594 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001595
1596 /* Variables relating to the cipher for the chosen ciphersuite. */
Xiaofei Baib7972842021-11-18 07:29:56 +00001597 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001600
1601 /* Extract basic information about hash and ciphersuite */
1602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1604 &key_len, &iv_len);
1605 if (ret != 0) {
1606 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001607 goto cleanup;
1608 }
1609
XiaokangQian33062842021-11-11 03:37:45 +00001610 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001611
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001612 hash_alg = mbedtls_md_psa_alg_from_type(handshake->ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001614
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001615 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001616 * to call this at the right time, that is, after the ServerFinished. */
1617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1619 transcript, sizeof(transcript),
1620 &transcript_len);
1621 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001622 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001624
1625 /* Compute application secrets from master secret and transcript hash. */
1626
Xiaokang Qian123cde82023-03-29 06:54:51 +00001627 ret = mbedtls_ssl_tls13_derive_application_secrets(
1628 hash_alg, handshake->tls13_master_secrets.app,
1629 transcript, transcript_len, app_secrets);
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 if (ret != 0) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001631 MBEDTLS_SSL_DEBUG_RET(
1632 1, "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001633 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001634 }
1635
1636 /* Derive first epoch of IV + Key for application traffic. */
1637
Xiaokang Qian123cde82023-03-29 06:54:51 +00001638 ret = mbedtls_ssl_tls13_make_traffic_keys(
1639 hash_alg,
1640 app_secrets->client_application_traffic_secret_N,
1641 app_secrets->server_application_traffic_secret_N,
1642 hash_len, key_len, iv_len, traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 if (ret != 0) {
1644 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001645 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001646 }
1647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1649 app_secrets->client_application_traffic_secret_N,
1650 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1653 app_secrets->server_application_traffic_secret_N,
1654 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001655
XiaokangQianac0385c2021-11-03 06:40:11 +00001656 /*
1657 * Export client/server application traffic secret 0
1658 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (ssl->f_export_keys != NULL) {
Xiaokang Qian123cde82023-03-29 06:54:51 +00001660 ssl->f_export_keys(
1661 ssl->p_export_keys,
1662 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1663 app_secrets->client_application_traffic_secret_N, hash_len,
1664 handshake->randbytes,
1665 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1666 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1667 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001668
Xiaokang Qian123cde82023-03-29 06:54:51 +00001669 ssl->f_export_keys(
1670 ssl->p_export_keys,
1671 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1672 app_secrets->server_application_traffic_secret_N, hash_len,
1673 handshake->randbytes,
1674 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1675 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1676 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001677 }
1678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1680 traffic_keys->client_write_key, key_len);
1681 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1682 traffic_keys->server_write_key, key_len);
1683 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1684 traffic_keys->client_write_iv, iv_len);
1685 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1686 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001691 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1693 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1696 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001697}
1698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001700{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001702 mbedtls_ssl_key_set traffic_keys;
1703 mbedtls_ssl_transform *transform_handshake = NULL;
1704 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1705
1706 /* Compute handshake secret */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001707 ret = ssl_tls13_key_schedule_stage_handshake(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 if (ret != 0) {
1709 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001710 goto cleanup;
1711 }
1712
1713 /* Next evolution in key schedule: Establish handshake secret and
1714 * key material. */
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001715 ret = ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 if (ret != 0) {
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001717 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_handshake_keys",
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001719 goto cleanup;
1720 }
1721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1723 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001724 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1725 goto cleanup;
1726 }
1727
Jerry Yuef2b98a2022-05-06 16:40:05 +08001728 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 transform_handshake,
1730 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001731 handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 &traffic_keys,
1733 ssl);
1734 if (ret != 0) {
1735 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001736 goto cleanup;
1737 }
1738 handshake->transform_handshake = transform_handshake;
1739
1740cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1742 if (ret != 0) {
1743 mbedtls_free(transform_handshake);
1744 }
Jerry Yue110d252022-05-05 10:19:22 +08001745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001747}
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001750{
Jerry Yu46bffe02022-09-13 11:25:28 +08001751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001752 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001753 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1754 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001755 size_t transcript_len;
1756
Xiaokang Qian123cde82023-03-29 06:54:51 +00001757 MBEDTLS_SSL_DEBUG_MSG(
1758 2, ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001759
Jerry Yu46bffe02022-09-13 11:25:28 +08001760 md_type = handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1763 transcript, sizeof(transcript),
1764 &transcript_len);
1765 if (ret != 0) {
1766 return ret;
1767 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001768
1769 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001770 mbedtls_md_psa_alg_from_type(md_type),
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 handshake->tls13_master_secrets.app,
1772 transcript, transcript_len,
1773 &ssl->session_negotiate->app_secrets);
1774 if (ret != 0) {
1775 return ret;
1776 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001777
Jerry Yuff226982022-04-16 16:52:57 +08001778 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1780 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001781
Xiaokang Qian123cde82023-03-29 06:54:51 +00001782 MBEDTLS_SSL_DEBUG_BUF(
1783 4, "Resumption master secret",
1784 ssl->session_negotiate->app_secrets.resumption_master_secret,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02001785 PSA_HASH_LENGTH(mbedtls_md_psa_alg_from_type(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001786
Xiaokang Qian123cde82023-03-29 06:54:51 +00001787 MBEDTLS_SSL_DEBUG_MSG(
1788 2, ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001790}
1791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001793{
1794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1795 mbedtls_ssl_key_set traffic_keys;
1796 mbedtls_ssl_transform *transform_application = NULL;
1797
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001798 ret = ssl_tls13_key_schedule_stage_application(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 if (ret != 0) {
1800 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001801 "ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001802 goto cleanup;
1803 }
1804
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001805 ret = ssl_tls13_generate_application_keys(ssl, &traffic_keys);
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (ret != 0) {
1807 MBEDTLS_SSL_DEBUG_RET(1,
Yanray Wangef5ec8f2023-01-05 17:36:12 +08001808 "ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001809 goto cleanup;
1810 }
1811
1812 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1814 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001815 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1816 goto cleanup;
1817 }
1818
1819 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 transform_application,
1821 ssl->conf->endpoint,
Xiaokang Qian6b980012023-02-07 03:17:45 +00001822 ssl->handshake->ciphersuite_info->id,
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 &traffic_keys,
1824 ssl);
1825 if (ret != 0) {
1826 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001827 goto cleanup;
1828 }
1829
1830 ssl->transform_application = transform_application;
1831
1832cleanup:
1833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1835 if (ret != 0) {
1836 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001837 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001839}
1840
Ronald Cron41a443a2022-10-04 16:38:25 +02001841#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001842int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1843 unsigned char **psk,
1844 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001845{
Jerry Yu40f37712022-07-26 16:58:57 +08001846#if defined(MBEDTLS_USE_PSA_CRYPTO)
1847 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001849
1850 *psk_len = 0;
1851 *psk = NULL;
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1854 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001855 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001856
1857 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1858 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001859 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 }
1861
1862 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1863 *psk = mbedtls_calloc(1, *psk_len);
1864 if (*psk == NULL) {
1865 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1866 }
1867
1868 status = psa_export_key(ssl->handshake->psk_opaque,
1869 (uint8_t *) *psk, *psk_len, psk_len);
1870 if (status != PSA_SUCCESS) {
1871 mbedtls_free((void *) *psk);
1872 *psk = NULL;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001873 return PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 }
1875 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001876#else
1877 *psk = ssl->handshake->psk;
1878 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 if (*psk == NULL) {
1880 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1881 }
1882 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001883#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001884}
Ronald Cron41a443a2022-10-04 16:38:25 +02001885#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001886
Ronald Cron6f135e12021-12-08 16:57:54 +01001887#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */