blob: 396c2e98dbd30efa5bcf742a72a99d78e99d2f68 [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 ( the "License" ); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Hanno Becker58c5cea2020-09-08 10:31:33 +010020#include "common.h"
Hanno Beckerbe9d6642020-08-21 13:20:06 +010021
Ronald Cron6f135e12021-12-08 16:57:54 +010022#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010023
Hanno Beckerbe9d6642020-08-21 13:20:06 +010024#include <stdint.h>
25#include <string.h>
26
Jerry Yue3131ef2021-09-16 13:14:15 +080027#include "mbedtls/hkdf.h"
28#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue110d252022-05-05 10:19:22 +080030#include "mbedtls/platform.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080031
32#include "ssl_misc.h"
33#include "ssl_tls13_keys.h"
Gabor Mezeia3eecd22022-02-09 16:57:26 +010034#include "ssl_tls13_invasive.h"
35
36#include "psa/crypto.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080037
Hanno Becker1413bd82020-09-09 12:46:09 +010038#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010039 .name = string,
40
Xiaofei Bai746f9482021-11-12 08:53:56 +000041struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010042{
43 /* This seems to work in C, despite the string literal being one
44 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010045 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010046};
47
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010048#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010049
Hanno Beckerbe9d6642020-08-21 13:20:06 +010050/*
51 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
52 *
53 * The HkdfLabel is specified in RFC 8446 as follows:
54 *
55 * struct HkdfLabel {
56 * uint16 length; // Length of expanded key material
57 * opaque label<7..255>; // Always prefixed by "tls13 "
58 * opaque context<0..255>; // Usually a communication transcript hash
59 * };
60 *
61 * Parameters:
62 * - desired_length: Length of expanded key material
63 * Even though the standard allows expansion to up to
64 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
65 * 255 Bytes, so we require `desired_length` to be at most
66 * 255. This allows us to save a few Bytes of code by
67 * hardcoding the writing of the high bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000068 * - (label, label_len): label + label length, without "tls13 " prefix
69 * The label length MUST be less than or equal to
70 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
71 * It is the caller's responsibility to ensure this.
72 * All (label, label length) pairs used in TLS 1.3
73 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
74 * - (ctx, ctx_len): context + context length
75 * The context length MUST be less than or equal to
76 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
77 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010078 * - dst: Target buffer for HkdfLabel structure,
79 * This MUST be a writable buffer of size
80 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000081 * - dst_len: Pointer at which to store the actual length of
82 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010083 */
84
Xiaofei Baid25fab62021-12-02 06:36:27 +000085static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010086
Hanno Becker9cb0a142020-09-08 10:48:14 +010087#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( label_len, context_len ) \
Hanno Beckerbe9d6642020-08-21 13:20:06 +010088 ( 2 /* expansion length */ \
89 + 1 /* label length */ \
Hanno Becker9cb0a142020-09-08 10:48:14 +010090 + label_len \
Hanno Beckerbe9d6642020-08-21 13:20:06 +010091 + 1 /* context length */ \
Hanno Becker9cb0a142020-09-08 10:48:14 +010092 + context_len )
93
94#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
95 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Xiaofei Baid25fab62021-12-02 06:36:27 +000096 sizeof(tls13_label_prefix) + \
Hanno Becker9cb0a142020-09-08 10:48:14 +010097 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
98 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +010099
Xiaofei Bai746f9482021-11-12 08:53:56 +0000100static void ssl_tls13_hkdf_encode_label(
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100101 size_t desired_length,
Xiaofei Baib7972842021-11-18 07:29:56 +0000102 const unsigned char *label, size_t label_len,
103 const unsigned char *ctx, size_t ctx_len,
104 unsigned char *dst, size_t *dst_len )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100105{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100106 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000107 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100108 size_t total_hkdf_lbl_len =
Xiaofei Baib7972842021-11-18 07:29:56 +0000109 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( total_label_len, ctx_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100110
111 unsigned char *p = dst;
112
Hanno Becker531fe302020-09-16 09:45:27 +0100113 /* Add the size of the expanded key material.
114 * We're hardcoding the high byte to 0 here assuming that we never use
115 * TLS 1.3 HKDF key expansion to more than 255 Bytes. */
116#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
Xiaofei Bai746f9482021-11-12 08:53:56 +0000117#error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \
Hanno Becker531fe302020-09-16 09:45:27 +0100118 value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
119#endif
120
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100121 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100122 *p++ = MBEDTLS_BYTE_0( desired_length );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100123
124 /* Add label incl. prefix */
Joe Subbiani2194dc42021-07-14 12:31:31 +0100125 *p++ = MBEDTLS_BYTE_0( total_label_len );
Xiaofei Baid25fab62021-12-02 06:36:27 +0000126 memcpy( p, tls13_label_prefix, sizeof(tls13_label_prefix) );
127 p += sizeof(tls13_label_prefix);
Xiaofei Baib7972842021-11-18 07:29:56 +0000128 memcpy( p, label, label_len );
129 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100130
131 /* Add context value */
Xiaofei Baib7972842021-11-18 07:29:56 +0000132 *p++ = MBEDTLS_BYTE_0( ctx_len );
133 if( ctx_len != 0 )
134 memcpy( p, ctx, ctx_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100135
136 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000137 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100138}
139
Xiaofei Bai746f9482021-11-12 08:53:56 +0000140int mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100141 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000142 const unsigned char *secret, size_t secret_len,
143 const unsigned char *label, size_t label_len,
144 const unsigned char *ctx, size_t ctx_len,
145 unsigned char *buf, size_t buf_len )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100146{
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100147 unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ];
148 size_t hkdf_label_len;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200149 psa_status_t status = PSA_SUCCESS;
150 psa_key_derivation_operation_t operation =
151 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100152
Xiaofei Baib7972842021-11-18 07:29:56 +0000153 if( label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100154 {
155 /* Should never happen since this is an internal
156 * function, and we know statically which labels
157 * are allowed. */
158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
159 }
160
Xiaofei Baib7972842021-11-18 07:29:56 +0000161 if( ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100162 {
163 /* Should not happen, as above. */
164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
165 }
166
Xiaofei Baib7972842021-11-18 07:29:56 +0000167 if( buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100168 {
169 /* Should not happen, as above. */
170 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
171 }
172
Gabor Mezei07732f72022-03-26 17:04:19 +0100173 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100174 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
175
Xiaofei Baib7972842021-11-18 07:29:56 +0000176 ssl_tls13_hkdf_encode_label( buf_len,
177 label, label_len,
178 ctx, ctx_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000179 hkdf_label,
180 &hkdf_label_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100181
Przemek Stekield5ae3652022-05-13 12:10:08 +0200182 status = psa_key_derivation_setup( &operation, PSA_ALG_HKDF_EXPAND( hash_alg ) );
183 if (status == PSA_SUCCESS)
184 status |= psa_key_derivation_input_bytes( &operation,
185 PSA_KEY_DERIVATION_INPUT_SECRET,
186 secret,
187 secret_len );
188 if (status == PSA_SUCCESS)
189 status |= psa_key_derivation_input_bytes( &operation,
190 PSA_KEY_DERIVATION_INPUT_INFO,
191 hkdf_label,
192 hkdf_label_len );
193 if (status == PSA_SUCCESS)
194 status |= psa_key_derivation_output_bytes( &operation,
195 buf,
196 buf_len );
197 if (status == PSA_SUCCESS)
198 status |= psa_key_derivation_abort( &operation );
199
200 return( psa_ssl_status_to_mbedtls ( status ) );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100201}
202
Hanno Becker3385a4d2020-08-21 13:03:34 +0100203/*
204 * The traffic keying material is generated from the following inputs:
205 *
206 * - One secret value per sender.
207 * - A purpose value indicating the specific value being generated
208 * - The desired lengths of key and IV.
209 *
210 * The expansion itself is based on HKDF:
211 *
212 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
213 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
214 *
215 * [sender] denotes the sending side and the Secret value is provided
216 * by the function caller. Note that we generate server and client side
217 * keys in a single function call.
218 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000219int mbedtls_ssl_tls13_make_traffic_keys(
Gabor Mezei07732f72022-03-26 17:04:19 +0100220 psa_algorithm_t hash_alg,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100221 const unsigned char *client_secret,
Xiaofei Baib7972842021-11-18 07:29:56 +0000222 const unsigned char *server_secret, size_t secret_len,
223 size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100224 mbedtls_ssl_key_set *keys )
225{
226 int ret = 0;
227
Xiaofei Bai746f9482021-11-12 08:53:56 +0000228 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000229 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100230 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
231 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100232 keys->client_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100233 if( ret != 0 )
234 return( ret );
235
Xiaofei Bai746f9482021-11-12 08:53:56 +0000236 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000237 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100238 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
239 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100240 keys->server_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100241 if( ret != 0 )
242 return( ret );
243
Xiaofei Bai746f9482021-11-12 08:53:56 +0000244 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000245 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100246 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
247 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100248 keys->client_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100249 if( ret != 0 )
250 return( ret );
251
Xiaofei Bai746f9482021-11-12 08:53:56 +0000252 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000253 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100254 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
255 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100256 keys->server_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100257 if( ret != 0 )
258 return( ret );
259
Hanno Becker493ea7f2020-09-08 11:01:00 +0100260 keys->key_len = key_len;
261 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100262
263 return( 0 );
264}
265
Xiaofei Bai746f9482021-11-12 08:53:56 +0000266int mbedtls_ssl_tls13_derive_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100267 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000268 const unsigned char *secret, size_t secret_len,
269 const unsigned char *label, size_t label_len,
270 const unsigned char *ctx, size_t ctx_len,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100271 int ctx_hashed,
Xiaofei Baib7972842021-11-18 07:29:56 +0000272 unsigned char *dstbuf, size_t dstbuf_len )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100273{
274 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100275 unsigned char hashed_context[ PSA_HASH_MAX_SIZE ];
Hanno Becker0c42fd92020-09-09 12:58:29 +0100276 if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100277 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
279
280 status = psa_hash_compute( hash_alg, ctx, ctx_len, hashed_context,
281 PSA_HASH_LENGTH( hash_alg ), &ctx_len );
282 if( status != PSA_SUCCESS )
283 {
284 ret = psa_ssl_status_to_mbedtls( status );
285 return ret;
286 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100287 }
288 else
289 {
Xiaofei Baib7972842021-11-18 07:29:56 +0000290 if( ctx_len > sizeof(hashed_context) )
Hanno Becker97a21562020-09-09 12:57:16 +0100291 {
292 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100293 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100294 * Let's double-check nonetheless to not run at the risk
295 * of getting a stack overflow. */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Becker97a21562020-09-09 12:57:16 +0100297 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100298
Xiaofei Baib7972842021-11-18 07:29:56 +0000299 memcpy( hashed_context, ctx, ctx_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100300 }
301
Xiaofei Bai746f9482021-11-12 08:53:56 +0000302 return( mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000303 secret, secret_len,
304 label, label_len,
305 hashed_context, ctx_len,
306 dstbuf, dstbuf_len ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100307
Hanno Beckerb35d5222020-08-21 13:27:44 +0100308}
309
Xiaofei Bai746f9482021-11-12 08:53:56 +0000310int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100311 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100312 const unsigned char *secret_old,
313 const unsigned char *input, size_t input_len,
314 unsigned char *secret_new )
315{
316 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200317 psa_status_t status = PSA_SUCCESS;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100318 size_t hlen, ilen;
Gabor Mezei07732f72022-03-26 17:04:19 +0100319 unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 };
Jerry Yu6eaa41c2021-11-22 18:16:45 +0800320 unsigned char tmp_input [ MBEDTLS_ECP_MAX_BYTES ] = { 0 };
Przemek Stekield5ae3652022-05-13 12:10:08 +0200321 psa_key_derivation_operation_t operation =
322 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100323
324 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
326
Gabor Mezei07732f72022-03-26 17:04:19 +0100327 hlen = PSA_HASH_LENGTH( hash_alg );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100328
329 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100330 * on the old secret. */
Hanno Beckere9cccb42020-08-20 13:42:46 +0100331 if( secret_old != NULL )
332 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000333 ret = mbedtls_ssl_tls13_derive_secret(
Hanno Beckere9cccb42020-08-20 13:42:46 +0100334 hash_alg,
335 secret_old, hlen,
336 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ),
337 NULL, 0, /* context */
338 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Hanno Becker59b50a12020-09-09 10:56:56 +0100339 tmp_secret, hlen );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100340 if( ret != 0 )
341 goto cleanup;
342 }
343
344 if( input != NULL )
345 {
Hanno Becker59b50a12020-09-09 10:56:56 +0100346 memcpy( tmp_input, input, input_len );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100347 ilen = input_len;
348 }
349 else
350 {
351 ilen = hlen;
352 }
353
Przemek Stekield5ae3652022-05-13 12:10:08 +0200354 status = psa_key_derivation_setup( &operation,
355 PSA_ALG_HKDF_EXTRACT( hash_alg ) );
356 if (status == PSA_SUCCESS)
357 status |= psa_key_derivation_input_bytes( &operation,
358 PSA_KEY_DERIVATION_INPUT_SALT,
359 tmp_secret,
360 hlen );
361 if (status == PSA_SUCCESS)
362 status |= psa_key_derivation_input_bytes( &operation,
363 PSA_KEY_DERIVATION_INPUT_SECRET,
364 tmp_input,
365 ilen );
366 if (status == PSA_SUCCESS)
367 status |= psa_key_derivation_output_bytes( &operation,
368 secret_new,
369 PSA_HASH_LENGTH( hash_alg ) );
370 if (status == PSA_SUCCESS)
371 status |= psa_key_derivation_abort( &operation );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100372
Przemek Stekield5ae3652022-05-13 12:10:08 +0200373 ret = psa_ssl_status_to_mbedtls ( status );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100374 cleanup:
375
Hanno Becker59b50a12020-09-09 10:56:56 +0100376 mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) );
377 mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100378 return( ret );
379}
380
Xiaofei Bai746f9482021-11-12 08:53:56 +0000381int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100382 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100383 unsigned char const *early_secret,
384 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000385 mbedtls_ssl_tls13_early_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100386{
387 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100388 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100389
390 /* We should never call this function with an unknown hash,
391 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100392 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
394
395 /*
396 * 0
397 * |
398 * v
399 * PSK -> HKDF-Extract = Early Secret
400 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100401 * +-----> Derive-Secret(., "c e traffic", ClientHello)
402 * | = client_early_traffic_secret
403 * |
404 * +-----> Derive-Secret(., "e exp master", ClientHello)
405 * | = early_exporter_master_secret
406 * v
407 */
408
409 /* Create client_early_traffic_secret */
Gabor Mezei07732f72022-03-26 17:04:19 +0100410 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
411 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100412 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ),
413 transcript, transcript_len,
414 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
415 derived->client_early_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100416 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100417 if( ret != 0 )
418 return( ret );
419
420 /* Create early exporter */
Gabor Mezei07732f72022-03-26 17:04:19 +0100421 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
422 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100423 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ),
424 transcript, transcript_len,
425 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
426 derived->early_exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100427 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100428 if( ret != 0 )
429 return( ret );
430
431 return( 0 );
432}
433
Xiaofei Bai746f9482021-11-12 08:53:56 +0000434int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100435 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100436 unsigned char const *handshake_secret,
437 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000438 mbedtls_ssl_tls13_handshake_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100439{
440 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100441 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100442
443 /* We should never call this function with an unknown hash,
444 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100445 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100446 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
447
448 /*
449 *
450 * Handshake Secret
451 * |
452 * +-----> Derive-Secret( ., "c hs traffic",
453 * | ClientHello...ServerHello )
454 * | = client_handshake_traffic_secret
455 * |
456 * +-----> Derive-Secret( ., "s hs traffic",
457 * | ClientHello...ServerHello )
458 * | = server_handshake_traffic_secret
459 *
460 */
461
462 /*
463 * Compute client_handshake_traffic_secret with
464 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
465 */
466
Gabor Mezei07732f72022-03-26 17:04:19 +0100467 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
468 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100469 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ),
470 transcript, transcript_len,
471 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
472 derived->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100473 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100474 if( ret != 0 )
475 return( ret );
476
477 /*
478 * Compute server_handshake_traffic_secret with
479 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
480 */
481
Gabor Mezei07732f72022-03-26 17:04:19 +0100482 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
483 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100484 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ),
485 transcript, transcript_len,
486 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
487 derived->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100488 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100489 if( ret != 0 )
490 return( ret );
491
492 return( 0 );
493}
494
Xiaofei Bai746f9482021-11-12 08:53:56 +0000495int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100496 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100497 unsigned char const *application_secret,
498 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000499 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100500{
501 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100502 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100503
504 /* We should never call this function with an unknown hash,
505 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100506 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100507 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
508
509 /* Generate {client,server}_application_traffic_secret_0
510 *
511 * Master Secret
512 * |
513 * +-----> Derive-Secret( ., "c ap traffic",
514 * | ClientHello...server Finished )
515 * | = client_application_traffic_secret_0
516 * |
517 * +-----> Derive-Secret( ., "s ap traffic",
518 * | ClientHello...Server Finished )
519 * | = server_application_traffic_secret_0
520 * |
521 * +-----> Derive-Secret( ., "exp master",
522 * | ClientHello...server Finished)
523 * | = exporter_master_secret
524 *
525 */
526
Gabor Mezei07732f72022-03-26 17:04:19 +0100527 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
528 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100529 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ),
530 transcript, transcript_len,
531 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
532 derived->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100533 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100534 if( ret != 0 )
535 return( ret );
536
Gabor Mezei07732f72022-03-26 17:04:19 +0100537 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
538 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100539 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ),
540 transcript, transcript_len,
541 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
542 derived->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100543 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100544 if( ret != 0 )
545 return( ret );
546
Gabor Mezei07732f72022-03-26 17:04:19 +0100547 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
548 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100549 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ),
550 transcript, transcript_len,
551 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
552 derived->exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100553 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100554 if( ret != 0 )
555 return( ret );
556
557 return( 0 );
558}
559
560/* Generate resumption_master_secret for use with the ticket exchange.
561 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000562 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100563 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000564int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100565 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100566 unsigned char const *application_secret,
567 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000568 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100569{
570 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100571 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100572
573 /* We should never call this function with an unknown hash,
574 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100575 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100576 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
577
Gabor Mezei07732f72022-03-26 17:04:19 +0100578 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
579 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100580 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ),
581 transcript, transcript_len,
582 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
583 derived->resumption_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100584 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100585
586 if( ret != 0 )
587 return( ret );
588
589 return( 0 );
590}
591
XiaokangQiana4c99f22021-11-11 06:46:35 +0000592int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000593{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000595 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +0100596 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
597 handshake->ciphersuite_info->mac );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000598
599 /*
600 * Compute MasterSecret
601 */
Gabor Mezei07732f72022-03-26 17:04:19 +0100602 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000603 handshake->tls13_master_secrets.handshake,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000604 NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000605 handshake->tls13_master_secrets.app );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000606 if( ret != 0 )
607 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000609 return( ret );
610 }
611
612 MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret",
Gabor Mezei07732f72022-03-26 17:04:19 +0100613 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH( hash_alg ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000614
615 return( 0 );
616}
617
Gabor Mezei07732f72022-03-26 17:04:19 +0100618static int ssl_tls13_calc_finished_core( psa_algorithm_t hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000619 unsigned char const *base_key,
620 unsigned char const *transcript,
Gabor Mezei07732f72022-03-26 17:04:19 +0100621 unsigned char *dst,
622 size_t *dst_len )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100623{
Gabor Mezei07732f72022-03-26 17:04:19 +0100624 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
627 size_t hash_len = PSA_HASH_LENGTH( hash_alg );
628 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100629 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100630 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100631
632 /* We should never call this function with an unknown hash,
633 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100634 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
636
637 /* TLS 1.3 Finished message
638 *
639 * struct {
640 * opaque verify_data[Hash.length];
641 * } Finished;
642 *
643 * verify_data =
644 * HMAC( finished_key,
645 * Hash( Handshake Context +
646 * Certificate* +
647 * CertificateVerify* )
648 * )
649 *
650 * finished_key =
651 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
652 */
653
Xiaofei Bai746f9482021-11-12 08:53:56 +0000654 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100655 hash_alg, base_key, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100656 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ),
657 NULL, 0,
Gabor Mezei07732f72022-03-26 17:04:19 +0100658 finished_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100659 if( ret != 0 )
660 goto exit;
661
Gabor Mezei07732f72022-03-26 17:04:19 +0100662 alg = PSA_ALG_HMAC( hash_alg );
663 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
664 psa_set_key_algorithm( &attributes, alg );
665 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
666
667 status = psa_import_key( &attributes, finished_key, hash_len, &key );
668 if( status != PSA_SUCCESS )
669 {
670 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100671 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100672 }
673
674 status = psa_mac_compute( key, alg, transcript, hash_len,
675 dst, hash_len, dst_len );
676 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100677
678exit:
679
Gabor Mezei07732f72022-03-26 17:04:19 +0100680 status = psa_destroy_key( key );
681 if( ret == 0 )
682 ret = psa_ssl_status_to_mbedtls( status );
683
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100684 mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100685
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100686 return( ret );
687}
688
XiaokangQianc5c39d52021-11-09 11:55:10 +0000689int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000690 unsigned char* dst,
691 size_t dst_len,
692 size_t *actual_len,
693 int from )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000694{
XiaokangQiana7634982021-10-22 06:32:32 +0000695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000696
XiaokangQianaaa0e192021-11-10 03:07:04 +0000697 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000698 size_t transcript_len;
699
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800700 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800701 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800702 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
703 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800704
705 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100706
707 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(
708 ssl->handshake->ciphersuite_info->mac );
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200709 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yua5563f62021-12-10 18:14:36 +0800710
711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) );
712
Jerry Yub737f6a2021-12-10 17:55:23 +0800713 if( from == MBEDTLS_SSL_IS_CLIENT )
714 {
715 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
716 base_key_len = sizeof( tls13_hs_secrets->client_handshake_traffic_secret );
717 }
718 else
719 {
720 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
721 base_key_len = sizeof( tls13_hs_secrets->server_handshake_traffic_secret );
722 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000723
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200724 if( dst_len < hash_len )
Jerry Yu9c074732021-12-10 17:12:43 +0800725 {
726 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
727 goto exit;
728 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000729
730 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
731 transcript, sizeof( transcript ),
732 &transcript_len );
733 if( ret != 0 )
734 {
735 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000736 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000737 }
738 MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len );
739
Gabor Mezei07732f72022-03-26 17:04:19 +0100740 ret = ssl_tls13_calc_finished_core( hash_alg, base_key, transcript, dst, actual_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000741 if( ret != 0 )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000742 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000743
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200744 MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, hash_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000746
747exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800748 /* Erase handshake secrets */
Jerry Yub737f6a2021-12-10 17:55:23 +0800749 mbedtls_platform_zeroize( base_key, base_key_len );
XiaokangQian33062842021-11-11 03:37:45 +0000750 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000751 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000752}
753
Xiaofei Bai746f9482021-11-12 08:53:56 +0000754int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100755 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100756 unsigned char const *psk, size_t psk_len,
757 int psk_type,
758 unsigned char const *transcript,
759 unsigned char *result )
760{
761 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100762 unsigned char binder_key[PSA_MAC_MAX_SIZE];
763 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gabor Mezei07732f72022-03-26 17:04:19 +0100764 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
765 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100766
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100767#if !defined(MBEDTLS_DEBUG_C)
768 ssl = NULL; /* make sure we don't use it except for debug */
769 ((void) ssl);
770#endif
771
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100772 /* We should never call this function with an unknown hash,
773 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100774 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100775 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
776
777 /*
778 * 0
779 * |
780 * v
781 * PSK -> HKDF-Extract = Early Secret
782 * |
783 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
784 * | = binder_key
785 * v
786 */
787
Gabor Mezei07732f72022-03-26 17:04:19 +0100788 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000789 NULL, /* Old secret */
790 psk, psk_len, /* Input */
791 early_secret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100792 if( ret != 0 )
793 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100795 goto exit;
796 }
797
798 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
799 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100800 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
801 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100802 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ),
803 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100804 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100805 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) );
806 }
807 else
808 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100809 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
810 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100811 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ),
812 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100813 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100814 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) );
815 }
816
817 if( ret != 0 )
818 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100820 goto exit;
821 }
822
823 /*
824 * The binding_value is computed in the same way as the Finished message
825 * but with the BaseKey being the binder_key.
826 */
827
Gabor Mezei07732f72022-03-26 17:04:19 +0100828 ret = ssl_tls13_calc_finished_core( hash_alg, binder_key, transcript,
829 result, &actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100830 if( ret != 0 )
831 goto exit;
832
Gabor Mezei07732f72022-03-26 17:04:19 +0100833 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100834
835exit:
836
837 mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) );
838 mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) );
839 return( ret );
840}
841
Hanno Beckerc94060c2021-03-22 07:50:44 +0000842int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
843 int endpoint,
844 int ciphersuite,
845 mbedtls_ssl_key_set const *traffic_keys,
846 mbedtls_ssl_context *ssl /* DEBUG ONLY */ )
847{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100848#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000849 int ret;
850 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200851#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000852 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
853 unsigned char const *key_enc;
854 unsigned char const *iv_enc;
855 unsigned char const *key_dec;
856 unsigned char const *iv_dec;
857
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100858#if defined(MBEDTLS_USE_PSA_CRYPTO)
859 psa_key_type_t key_type;
860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
861 psa_algorithm_t alg;
862 size_t key_bits;
863 psa_status_t status = PSA_SUCCESS;
864#endif
865
Hanno Beckerc94060c2021-03-22 07:50:44 +0000866#if !defined(MBEDTLS_DEBUG_C)
867 ssl = NULL; /* make sure we don't use it except for those cases */
868 (void) ssl;
869#endif
870
871 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker7887a772021-04-20 05:27:57 +0100872 if( ciphersuite_info == NULL )
873 {
874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
875 ciphersuite ) );
876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
877 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000878
Neil Armstronga8093f52022-05-04 17:44:05 +0200879#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000880 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
881 if( cipher_info == NULL )
882 {
Hanno Becker7887a772021-04-20 05:27:57 +0100883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
884 ciphersuite_info->cipher ) );
Hanno Beckerf62a7302021-04-21 05:21:28 +0100885 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerc94060c2021-03-22 07:50:44 +0000886 }
887
888 /*
889 * Setup cipher contexts in target transform
890 */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000891 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
892 cipher_info ) ) != 0 )
893 {
894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
895 return( ret );
896 }
897
898 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
899 cipher_info ) ) != 0 )
900 {
901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
902 return( ret );
903 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100904#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000905
906#if defined(MBEDTLS_SSL_SRV_C)
907 if( endpoint == MBEDTLS_SSL_IS_SERVER )
908 {
909 key_enc = traffic_keys->server_write_key;
910 key_dec = traffic_keys->client_write_key;
911 iv_enc = traffic_keys->server_write_iv;
912 iv_dec = traffic_keys->client_write_iv;
913 }
914 else
915#endif /* MBEDTLS_SSL_SRV_C */
916#if defined(MBEDTLS_SSL_CLI_C)
917 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
918 {
919 key_enc = traffic_keys->client_write_key;
920 key_dec = traffic_keys->server_write_key;
921 iv_enc = traffic_keys->client_write_iv;
922 iv_dec = traffic_keys->server_write_iv;
923 }
924 else
925#endif /* MBEDTLS_SSL_CLI_C */
926 {
927 /* should not happen */
928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
929 }
930
931 memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len );
932 memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len );
933
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100934#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000935 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc,
936 key_enc, cipher_info->key_bitlen,
937 MBEDTLS_ENCRYPT ) ) != 0 )
938 {
939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
940 return( ret );
941 }
942
943 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec,
944 key_dec, cipher_info->key_bitlen,
945 MBEDTLS_DECRYPT ) ) != 0 )
946 {
947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
948 return( ret );
949 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100950#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000951
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100952 /*
953 * Setup other fields in SSL transform
954 */
955
956 if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 )
957 transform->taglen = 8;
958 else
959 transform->taglen = 16;
960
961 transform->ivlen = traffic_keys->iv_len;
962 transform->maclen = 0;
963 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -0400964 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100965
966 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800967 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100968 * type-extended and padded plaintext is therefore the padding
969 * granularity. */
970 transform->minlen =
971 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
972
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100973#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100974 /*
975 * Setup psa keys and alg
976 */
Neil Armstronga8093f52022-05-04 17:44:05 +0200977 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100978 transform->taglen,
979 &alg,
980 &key_type,
981 &key_bits ) ) != PSA_SUCCESS )
982 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +0100983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls( status ) );
984 return( psa_ssl_status_to_mbedtls( status ) );
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100985 }
986
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100987 transform->psa_alg = alg;
988
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +0100989 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100990 {
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +0100991 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
992 psa_set_key_algorithm( &attributes, alg );
993 psa_set_key_type( &attributes, key_type );
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +0100994
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +0100995 if( ( status = psa_import_key( &attributes,
996 key_enc,
997 PSA_BITS_TO_BYTES( key_bits ),
998 &transform->psa_key_enc ) ) != PSA_SUCCESS )
999 {
1000 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1001 return( psa_ssl_status_to_mbedtls( status ) );
1002 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001003
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001004 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
1005
1006 if( ( status = psa_import_key( &attributes,
1007 key_dec,
1008 PSA_BITS_TO_BYTES( key_bits ),
1009 &transform->psa_key_dec ) ) != PSA_SUCCESS )
1010 {
1011 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1012 return( psa_ssl_status_to_mbedtls( status ) );
1013 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001014 }
1015#endif /* MBEDTLS_USE_PSA_CRYPTO */
1016
Hanno Beckerc94060c2021-03-22 07:50:44 +00001017 return( 0 );
1018}
1019
Xiaofei Bai746f9482021-11-12 08:53:56 +00001020int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl )
Jerry Yu89ea3212021-09-09 14:31:24 +08001021{
Jerry Yue3131ef2021-09-16 13:14:15 +08001022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001023 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001024 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001025
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001026 if( handshake->ciphersuite_info == NULL )
Jerry Yu89ea3212021-09-09 14:31:24 +08001027 {
1028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) );
1029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1030 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001031
Gabor Mezei07732f72022-03-26 17:04:19 +01001032 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
Jerry Yue06f4532021-09-23 18:35:07 +08001033
Gabor Mezei07732f72022-03-26 17:04:19 +01001034 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, NULL, NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001035 handshake->tls13_master_secrets.early );
Jerry Yu89ea3212021-09-09 14:31:24 +08001036 if( ret != 0 )
1037 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yu89ea3212021-09-09 14:31:24 +08001039 return( ret );
1040 }
1041
1042 return( 0 );
1043}
1044
Neil Armstrongb818e162022-05-17 09:24:52 +02001045static int mbedtls_ssl_tls13_get_cipher_key_info(
1046 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1047 size_t *key_len, size_t *iv_len )
1048{
1049 psa_key_type_t key_type;
1050 psa_algorithm_t alg;
1051 size_t taglen;
1052 size_t key_bits;
1053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1054
1055 if( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG )
1056 taglen = 8;
1057 else
1058 taglen = 16;
1059
1060 status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, taglen,
1061 &alg, &key_type, &key_bits );
1062 if( status != PSA_SUCCESS )
1063 return psa_ssl_status_to_mbedtls( status );
1064
1065 *key_len = PSA_BITS_TO_BYTES( key_bits );
1066
Neil Armstrong0fa8ce32022-05-17 14:42:57 +02001067 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1068 *iv_len = 12;
Neil Armstrongb818e162022-05-17 09:24:52 +02001069
1070 return 0;
1071}
1072
Jerry Yuc068b662021-10-11 22:30:19 +08001073/* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for
Jerry Yu61e35e02021-09-16 18:59:08 +08001074 * protecting the handshake messages, as described in Section 7 of TLS 1.3. */
Jerry Yuc068b662021-10-11 22:30:19 +08001075int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
1076 mbedtls_ssl_key_set *traffic_keys )
Jerry Yu61e35e02021-09-16 18:59:08 +08001077{
1078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1079
1080 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001081
1082 psa_algorithm_t hash_alg;
1083 size_t hash_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001084
Jerry Yu435208a2021-10-13 11:22:16 +08001085 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001086 size_t transcript_len;
1087
Xiaofei Baib7972842021-11-18 07:29:56 +00001088 size_t key_len, iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001089
Jerry Yu435208a2021-10-13 11:22:16 +08001090 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1091 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001092 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001093
Jerry Yuc068b662021-10-11 22:30:19 +08001094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001095
Neil Armstrongb818e162022-05-17 09:24:52 +02001096 ret = mbedtls_ssl_tls13_get_cipher_key_info( ciphersuite_info,
1097 &key_len, &iv_len );
1098 if( ret != 0 )
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001099 {
Neil Armstrongb818e162022-05-17 09:24:52 +02001100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_get_cipher_key_info", ret );
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001101 return ret;
1102 }
1103
Jerry Yu435208a2021-10-13 11:22:16 +08001104 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001105
1106 hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
1107 hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yu61e35e02021-09-16 18:59:08 +08001108
1109 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1110 transcript,
1111 sizeof( transcript ),
1112 &transcript_len );
1113 if( ret != 0 )
1114 {
1115 MBEDTLS_SSL_DEBUG_RET( 1,
1116 "mbedtls_ssl_get_handshake_transcript",
1117 ret );
1118 return( ret );
1119 }
1120
Gabor Mezei07732f72022-03-26 17:04:19 +01001121 ret = mbedtls_ssl_tls13_derive_handshake_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001122 handshake->tls13_master_secrets.handshake,
Jerry Yu435208a2021-10-13 11:22:16 +08001123 transcript, transcript_len, tls13_hs_secrets );
Jerry Yu61e35e02021-09-16 18:59:08 +08001124 if( ret != 0 )
1125 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_handshake_secrets",
Jerry Yu61e35e02021-09-16 18:59:08 +08001127 ret );
1128 return( ret );
1129 }
1130
1131 MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001132 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001133 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001134 MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001135 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001136 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001137
1138 /*
1139 * Export client handshake traffic secret
1140 */
Jerry Yu61e35e02021-09-16 18:59:08 +08001141 if( ssl->f_export_keys != NULL )
1142 {
1143 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001144 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001145 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001146 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001147 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001148 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001149 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1150
1151 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001152 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001153 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001154 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001155 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001156 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001157 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1158 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001159
Gabor Mezei07732f72022-03-26 17:04:19 +01001160 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
Jerry Yu435208a2021-10-13 11:22:16 +08001161 tls13_hs_secrets->client_handshake_traffic_secret,
1162 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001163 hash_len, key_len, iv_len, traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +08001164 if( ret != 0 )
1165 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
Jerry Yu61e35e02021-09-16 18:59:08 +08001167 goto exit;
1168 }
1169
1170 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key",
1171 traffic_keys->client_write_key,
1172 traffic_keys->key_len);
1173
1174 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key",
1175 traffic_keys->server_write_key,
1176 traffic_keys->key_len);
1177
1178 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv",
1179 traffic_keys->client_write_iv,
1180 traffic_keys->iv_len);
1181
1182 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv",
1183 traffic_keys->server_write_iv,
1184 traffic_keys->iv_len);
1185
Jerry Yuc068b662021-10-11 22:30:19 +08001186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001187
1188exit:
1189
1190 return( ret );
1191}
1192
Jerry Yuf0ac2352021-10-11 17:47:07 +08001193int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl )
Jerry Yua0650eb2021-09-09 17:14:45 +08001194{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001196#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) && defined(MBEDTLS_ECDH_C)
1197 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1198#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED && MBEDTLS_ECDH_C */
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001199 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +01001200 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
1201 handshake->ciphersuite_info->mac );
Jerry Yua0650eb2021-09-09 17:14:45 +08001202
Jerry Yuf0ac2352021-10-11 17:47:07 +08001203#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
1204 /*
1205 * Compute ECDHE secret used to compute the handshake secret from which
1206 * client_handshake_traffic_secret and server_handshake_traffic_secret
1207 * are derived in the handshake secret derivation stage.
1208 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001209 if( mbedtls_ssl_tls13_ephemeral_enabled( ssl ) )
Jerry Yuf0ac2352021-10-11 17:47:07 +08001210 {
1211 if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) )
1212 {
1213#if defined(MBEDTLS_ECDH_C)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001214 /* Compute ECDH shared secret. */
1215 status = psa_raw_key_agreement(
1216 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1217 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1218 handshake->premaster, sizeof( handshake->premaster ),
1219 &handshake->pmslen );
1220 if( status != PSA_SUCCESS )
1221 {
1222 ret = psa_ssl_status_to_mbedtls( status );
1223 MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret );
1224 return( ret );
1225 }
1226
1227 status = psa_destroy_key( handshake->ecdh_psa_privkey );
1228 if( status != PSA_SUCCESS )
1229 {
1230 ret = psa_ssl_status_to_mbedtls( status );
1231 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
1232 return( ret );
1233 }
1234
1235 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001236#endif /* MBEDTLS_ECDH_C */
1237 }
1238 else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) )
1239 {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) );
1241 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1242 }
1243 }
1244#else
1245 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1246#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001247
1248 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001249 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001250 */
Gabor Mezei07732f72022-03-26 17:04:19 +01001251 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001252 handshake->tls13_master_secrets.early,
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001253 handshake->premaster, handshake->pmslen,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001254 handshake->tls13_master_secrets.handshake );
Jerry Yua0650eb2021-09-09 17:14:45 +08001255 if( ret != 0 )
1256 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yua0650eb2021-09-09 17:14:45 +08001258 return( ret );
1259 }
1260
1261 MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret",
Gabor Mezei07732f72022-03-26 17:04:19 +01001262 handshake->tls13_master_secrets.handshake,
1263 PSA_HASH_LENGTH( hash_alg ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001264
1265#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001266 mbedtls_platform_zeroize( handshake->premaster, sizeof( handshake->premaster ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001267#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
1268 return( 0 );
1269}
1270
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001271/* Generate application traffic keys since any records following a 1-RTT Finished message
1272 * MUST be encrypted under the application traffic key.
1273 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001274int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001275 mbedtls_ssl_context *ssl,
1276 mbedtls_ssl_key_set *traffic_keys )
1277{
XiaokangQiana7634982021-10-22 06:32:32 +00001278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001279 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001280
1281 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001282 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001283 &ssl->session_negotiate->app_secrets;
1284
1285 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001286 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001287 size_t transcript_len;
1288
1289 /* Variables relating to the hash for the chosen ciphersuite. */
1290 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001291
1292 psa_algorithm_t hash_alg;
1293 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001294
1295 /* Variables relating to the cipher for the chosen ciphersuite. */
Xiaofei Baib7972842021-11-18 07:29:56 +00001296 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001297
1298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) );
1299
1300 /* Extract basic information about hash and ciphersuite */
1301
Neil Armstrongb818e162022-05-17 09:24:52 +02001302 ret = mbedtls_ssl_tls13_get_cipher_key_info( handshake->ciphersuite_info,
1303 &key_len, &iv_len );
1304 if( ret != 0 )
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001305 {
Neil Armstrongb818e162022-05-17 09:24:52 +02001306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_get_cipher_key_info", ret );
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001307 goto cleanup;
1308 }
1309
XiaokangQian33062842021-11-11 03:37:45 +00001310 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001311
1312 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
1313 hash_len = PSA_HASH_LENGTH( hash_alg );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001314
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001315 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001316 * to call this at the right time, that is, after the ServerFinished. */
1317
1318 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1319 transcript, sizeof( transcript ),
1320 &transcript_len );
1321 if( ret != 0 )
XiaokangQian4cab0242021-10-12 08:43:37 +00001322 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001323
1324 /* Compute application secrets from master secret and transcript hash. */
1325
Gabor Mezei07732f72022-03-26 17:04:19 +01001326 ret = mbedtls_ssl_tls13_derive_application_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001327 handshake->tls13_master_secrets.app,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001328 transcript, transcript_len,
1329 app_secrets );
1330 if( ret != 0 )
1331 {
1332 MBEDTLS_SSL_DEBUG_RET( 1,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001333 "mbedtls_ssl_tls13_derive_application_secrets", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001334 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001335 }
1336
1337 /* Derive first epoch of IV + Key for application traffic. */
1338
Gabor Mezei07732f72022-03-26 17:04:19 +01001339 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001340 app_secrets->client_application_traffic_secret_N,
1341 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001342 hash_len, key_len, iv_len, traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001343 if( ret != 0 )
1344 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001346 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001347 }
1348
1349 MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret",
1350 app_secrets->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001351 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001352
1353 MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret",
1354 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001355 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001356
XiaokangQianac0385c2021-11-03 06:40:11 +00001357 /*
1358 * Export client/server application traffic secret 0
1359 */
1360 if( ssl->f_export_keys != NULL )
1361 {
1362 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001363 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001364 app_secrets->client_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001365 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001366 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001367 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1368 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001369
1370 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001371 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001372 app_secrets->server_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001373 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001374 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001375 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1376 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001377 }
1378
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001379 MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:",
Xiaofei Baib7972842021-11-18 07:29:56 +00001380 traffic_keys->client_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001381 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key",
Xiaofei Baib7972842021-11-18 07:29:56 +00001382 traffic_keys->server_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001383 MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001384 traffic_keys->client_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001385 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001386 traffic_keys->server_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001387
1388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001389
1390 cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001391 /* randbytes is not used again */
1392 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1393 sizeof( ssl->handshake->randbytes ) );
Jerry Yuef2b98a2022-05-06 16:40:05 +08001394
XiaokangQian33062842021-11-11 03:37:45 +00001395 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001396 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001397}
1398
Jerry Yuf86eb752022-05-06 11:16:55 +08001399int mbedtls_ssl_tls13_compute_handshake_transform( mbedtls_ssl_context *ssl )
Jerry Yue110d252022-05-05 10:19:22 +08001400{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001402 mbedtls_ssl_key_set traffic_keys;
1403 mbedtls_ssl_transform *transform_handshake = NULL;
1404 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1405
1406 /* Compute handshake secret */
1407 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
1408 if( ret != 0 )
1409 {
1410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
1411 goto cleanup;
1412 }
1413
1414 /* Next evolution in key schedule: Establish handshake secret and
1415 * key material. */
1416 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
1417 if( ret != 0 )
1418 {
1419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1420 ret );
1421 goto cleanup;
1422 }
1423
1424 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1425 if( transform_handshake == NULL )
1426 {
1427 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1428 goto cleanup;
1429 }
1430
Jerry Yuef2b98a2022-05-06 16:40:05 +08001431 ret = mbedtls_ssl_tls13_populate_transform(
1432 transform_handshake,
1433 ssl->conf->endpoint,
1434 ssl->session_negotiate->ciphersuite,
1435 &traffic_keys,
1436 ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001437 if( ret != 0 )
1438 {
1439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
1440 goto cleanup;
1441 }
1442 handshake->transform_handshake = transform_handshake;
1443
1444cleanup:
1445 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
1446 if( ret != 0 )
Jerry Yue110d252022-05-05 10:19:22 +08001447 mbedtls_free( transform_handshake );
Jerry Yue110d252022-05-05 10:19:22 +08001448
1449 return( ret );
1450}
1451
Jerry Yuff226982022-04-16 16:52:57 +08001452int mbedtls_ssl_tls13_generate_resumption_master_secret(
1453 mbedtls_ssl_context *ssl )
1454{
1455 /* Erase master secrets */
1456 mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets,
1457 sizeof( ssl->handshake->tls13_master_secrets ) );
1458 return( 0 );
1459}
1460
Jerry Yufd5ea042022-05-19 14:29:48 +08001461int mbedtls_ssl_tls13_compute_application_transform( mbedtls_ssl_context *ssl )
1462{
1463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1464 mbedtls_ssl_key_set traffic_keys;
1465 mbedtls_ssl_transform *transform_application = NULL;
1466
1467 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
1468 if( ret != 0 )
1469 {
1470 MBEDTLS_SSL_DEBUG_RET( 1,
1471 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
1472 goto cleanup;
1473 }
1474
1475 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
1476 if( ret != 0 )
1477 {
1478 MBEDTLS_SSL_DEBUG_RET( 1,
1479 "mbedtls_ssl_tls13_generate_application_keys", ret );
1480 goto cleanup;
1481 }
1482
1483 transform_application =
1484 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1485 if( transform_application == NULL )
1486 {
1487 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1488 goto cleanup;
1489 }
1490
1491 ret = mbedtls_ssl_tls13_populate_transform(
1492 transform_application,
1493 ssl->conf->endpoint,
1494 ssl->session_negotiate->ciphersuite,
1495 &traffic_keys,
1496 ssl );
1497 if( ret != 0 )
1498 {
1499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
1500 goto cleanup;
1501 }
1502
1503 ssl->transform_application = transform_application;
1504
1505cleanup:
1506
1507 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
1508 if( ret != 0 )
1509 {
1510 mbedtls_free( transform_application );
1511 }
1512 return( ret );
1513}
1514
Ronald Cron6f135e12021-12-08 16:57:54 +01001515#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */