blob: f3126d27a5ed51aa698bc13f2e3bd3ea225a9a90 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
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 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Ronald Cron6f135e12021-12-08 16:57:54 +010024#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
26#if defined(MBEDTLS_SSL_CLI_C)
27
Jerry Yubc20bdd2021-08-24 15:59:48 +080028#include <string.h>
29
Jerry Yu56fc07f2021-09-01 17:48:49 +080030#include "mbedtls/debug.h"
31#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080032#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080033
Jerry Yubdc71882021-09-14 19:30:36 +080034#include "ssl_misc.h"
35#include "ecdh_misc.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080036#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010037#include "ssl_debug_helpers.h"
Jerry Yubdc71882021-09-14 19:30:36 +080038
Jerry Yubc20bdd2021-08-24 15:59:48 +080039/* Write extensions */
40
Jerry Yu92c6b402021-08-27 16:59:09 +080041/*
42 * ssl_tls13_write_supported_versions_ext():
43 *
44 * struct {
45 * ProtocolVersion versions<2..254>;
46 * } SupportedVersions;
47 */
Jerry Yuf4436812021-08-26 22:59:56 +080048static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080049 unsigned char *buf,
50 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000051 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080052{
53 unsigned char *p = buf;
54
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
63 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Jerry Yu92c6b402021-08-27 16:59:09 +080065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
66
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080067 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080068 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080069
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080070 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080071 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080072 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080074 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080075 *p++ = 0x2;
76
Jerry Yu0c63af62021-09-02 12:59:12 +080077 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080078 *
Jerry Yu0c63af62021-09-02 12:59:12 +080079 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080080 *
Jerry Yu0c63af62021-09-02 12:59:12 +080081 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080082 */
Jerry Yueecfbf02021-08-30 18:32:07 +080083 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
84 ssl->conf->max_minor_ver,
85 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080086
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080088 ssl->conf->max_major_ver,
89 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080090
Xiaofei Baid25fab62021-12-02 06:36:27 +000091 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080092
93 return( 0 );
94}
Jerry Yubc20bdd2021-08-24 15:59:48 +080095
Jerry Yuc068b662021-10-11 22:30:19 +080096static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
97 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080098 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080099{
Jerry Yue1b9c292021-09-10 10:08:31 +0800100 ((void) ssl);
101
Jerry Yub85277e2021-10-13 13:36:05 +0800102 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
103 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800104 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
105 {
106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800107
108 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
109 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800111 }
112
113 return( 0 );
114}
115
Jerry Yubc20bdd2021-08-24 15:59:48 +0800116#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
117
Jerry Yu6b64fe32021-09-01 17:05:13 +0800118/*
XiaokangQian647719a2021-12-07 09:16:29 +0000119 * Key Shares Extension
120 *
121 * enum {
122 * ... (0xFFFF)
123 * } NamedGroup;
124 *
125 * struct {
126 * NamedGroup group;
127 * opaque key_exchange<1..2^16-1>;
128 * } KeyShareEntry;
129 *
130 * struct {
131 * select(role) {
132 * case client:
133 * KeyShareEntry client_shares<0..2^16-1>;
134 * }
135 * } KeyShare;
136 */
137
XiaokangQian0b56a8f2021-12-22 02:39:32 +0000138#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000139static int ssl_reset_ecdhe_share( mbedtls_ssl_context *ssl )
140{
141 mbedtls_ecdh_free( &ssl->handshake->ecdh_ctx );
142 return( 0 );
143}
144
145static int ssl_reset_key_share( mbedtls_ssl_context *ssl )
146{
147 uint16_t group_id = ssl->handshake->offered_group_id;
148 if( group_id == 0 )
149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
150
151 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
152 return( ssl_reset_ecdhe_share( ssl ) );
153 else if( 0 /* other KEMs? */ )
154 {
155 /* Do something */
156 }
157
158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
159}
XiaokangQian0b56a8f2021-12-22 02:39:32 +0000160#else
161static int ssl_reset_key_share( mbedtls_ssl_context *ssl )
162{
163 ((void) ssl);
164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
165}
166#endif /* MBEDTLS_ECDH_C */
XiaokangQian647719a2021-12-07 09:16:29 +0000167
168/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800169 * Functions for writing key_share extension.
170 */
171#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800172static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800173 mbedtls_ssl_context *ssl,
174 uint16_t named_group,
175 unsigned char *buf,
176 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000177 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800178{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800180 const mbedtls_ecp_curve_info *curve_info =
181 mbedtls_ecp_curve_info_from_tls_id( named_group );
182
183 if( curve_info == NULL )
184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
185
186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
187
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800188 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
189 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800190 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800192 return( ret );
193 }
194
Xiaofei Baid25fab62021-12-02 06:36:27 +0000195 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
196 buf, end - buf,
197 ssl->conf->f_rng, ssl->conf->p_rng );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800198 if( ret != 0 )
199 {
200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
201 return( ret );
202 }
203
204 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
205 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800206 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800207}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800208#endif /* MBEDTLS_ECDH_C */
209
Jerry Yub60e3cf2021-09-08 16:41:02 +0800210static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
211 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800212{
213 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
214
Jerry Yu56fc07f2021-09-01 17:48:49 +0800215
Jerry Yu56fc07f2021-09-01 17:48:49 +0800216#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100217 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800218 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100219 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800220 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
221
Brett Warren14efd332021-10-06 09:32:11 +0100222 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800223 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000224 const mbedtls_ecp_curve_info *curve_info;
225 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
226 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100227 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800228 {
Brett Warren14efd332021-10-06 09:32:11 +0100229 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800230 return( 0 );
231 }
232 }
233#else
234 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800235 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800236#endif /* MBEDTLS_ECDH_C */
237
238 /*
239 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800240 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800241 */
242
243 return( ret );
244}
245
246/*
247 * ssl_tls13_write_key_share_ext
248 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800249 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800250 *
251 * struct {
252 * NamedGroup group;
253 * opaque key_exchange<1..2^16-1>;
254 * } KeyShareEntry;
255 * struct {
256 * KeyShareEntry client_shares<0..2^16-1>;
257 * } KeyShareClientHello;
258 */
259static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
260 unsigned char *buf,
261 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000262 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800263{
264 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000265 unsigned char *client_shares; /* Start of client_shares */
266 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800267 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
269
Xiaofei Baid25fab62021-12-02 06:36:27 +0000270 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271
Jerry Yub60e3cf2021-09-08 16:41:02 +0800272 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273 * - extension_type (2 bytes)
274 * - extension_data_length (2 bytes)
275 * - client_shares_length (2 bytes)
276 */
277 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
278 p += 6;
279
280 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
281
282 /* HRR could already have requested something else. */
283 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800284 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
285 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800286 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800287 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800288 &group_id ) );
289 }
290
291 /*
292 * Dispatch to type-specific key generation function.
293 *
294 * So far, we're only supporting ECDHE. With the introduction
295 * of PQC KEMs, we'll want to have multiple branches, one per
296 * type of KEM, and dispatch to the corresponding crypto. And
297 * only one key share entry is allowed.
298 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000299 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800300#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800301 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800302 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800303 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000304 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800305 /* Length of key_exchange */
306 size_t key_exchange_len;
307
308 /* Check there is space for header of KeyShareEntry
309 * - group (2 bytes)
310 * - key_exchange_length (2 bytes)
311 */
312 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
313 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800314 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
315 p, end,
316 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800317 p += key_exchange_len;
318 if( ret != 0 )
319 return( ret );
320
321 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000322 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800323 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000324 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800325 }
326 else
327#endif /* MBEDTLS_ECDH_C */
328 if( 0 /* other KEMs? */ )
329 {
330 /* Do something */
331 }
332 else
333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
334
Jerry Yub60e3cf2021-09-08 16:41:02 +0800335 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000336 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800337 if( client_shares_len == 0)
338 {
339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800341 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 /* Write extension_type */
343 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
344 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800345 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800346 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800347 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800348
349 /* Update offered_group_id field */
350 ssl->handshake->offered_group_id = group_id;
351
352 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000353 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800354
Xiaofei Baid25fab62021-12-02 06:36:27 +0000355 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800356
357 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
358
359cleanup:
360
361 return( ret );
362}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800363
Jerry Yue1b9c292021-09-10 10:08:31 +0800364#if defined(MBEDTLS_ECDH_C)
365
Jerry Yuc068b662021-10-11 22:30:19 +0800366static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800367{
368 const mbedtls_ecp_curve_info *curve_info;
369 mbedtls_ecp_group_id grp_id;
370#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
371 grp_id = ssl->handshake->ecdh_ctx.grp.id;
372#else
373 grp_id = ssl->handshake->ecdh_ctx.grp_id;
374#endif
375
376 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
377 if( curve_info == NULL )
378 {
379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
381 }
382
383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
384
Jerry Yue1b9c292021-09-10 10:08:31 +0800385 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800386 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800387
388 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
389 MBEDTLS_DEBUG_ECDH_QP );
390
391 return( 0 );
392}
393
Jerry Yuc068b662021-10-11 22:30:19 +0800394static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
395 const unsigned char *buf,
396 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800397{
398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
399
Jerry Yuc068b662021-10-11 22:30:19 +0800400 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800401 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800402 if( ret != 0 )
403 {
404 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800405
406 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
407 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
408 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800409 }
410
Jerry Yuc068b662021-10-11 22:30:19 +0800411 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800412 {
Jerry Yuc068b662021-10-11 22:30:19 +0800413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800414
Jerry Yu4a173382021-10-11 21:45:31 +0800415 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
416 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
417 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800418 }
419
420 return( 0 );
421}
Jerry Yu4a173382021-10-11 21:45:31 +0800422#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800423
XiaokangQianb851da82022-01-14 04:03:11 +0000424static int ssl_tls13_hrr_check_key_share_ext( mbedtls_ssl_context *ssl,
425 const unsigned char *buf,
426 const unsigned char *end )
427{
428 /* Variables for parsing the key_share */
429 const uint16_t* grp_id;
430 const mbedtls_ecp_curve_info *curve_info = NULL;
431 const unsigned char *p = buf;
432 int tls_id;
433 int found = 0;
434
435 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
436 if( group_list == NULL )
437 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
438
439 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
440
441 /* Read selected_group */
442 tls_id = MBEDTLS_GET_UINT16_BE( p, 0 );
443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", tls_id ) );
444
445 /* Upon receipt of this extension in a HelloRetryRequest, the client
446 * MUST first verify that the selected_group field corresponds to a
447 * group which was provided in the "supported_groups" extension in the
448 * original ClientHello.
449 * The supported_group was based on the info in ssl->conf->group_list.
450 *
451 * If the server provided a key share that was not sent in the ClientHello
452 * then the client MUST abort the handshake with an "illegal_parameter" alert.
453 */
454 for ( ; *group_list != 0; group_list++ )
455 {
456 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
457 if( curve_info == NULL || curve_info->tls_id != tls_id )
458 continue;
459
460 /* We found a match */
461 found = 1;
462 break;
463 }
464
465 /* Client MUST verify that the selected_group field does not
466 * correspond to a group which was provided in the "key_share"
467 * extension in the original ClientHello. If the server sent an
468 * HRR message with a key share already provided in the
469 * ClientHello then the client MUST abort the handshake with
470 * an "illegal_parameter" alert.
471 */
472 if( found == 0 || tls_id == ssl->handshake->offered_group_id )
473 {
474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
475 MBEDTLS_SSL_PEND_FATAL_ALERT(
476 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
477 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
478 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
479 }
480
481 /* Remember server's preference for next ClientHello */
482 ssl->handshake->offered_group_id= tls_id;
483
484 return( 0 );
485}
486
Jerry Yue1b9c292021-09-10 10:08:31 +0800487/*
Jerry Yub85277e2021-10-13 13:36:05 +0800488 * ssl_tls13_parse_key_share_ext()
489 * Parse key_share extension in Server Hello
490 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800491 * struct {
492 * KeyShareEntry server_share;
493 * } KeyShareServerHello;
494 * struct {
495 * NamedGroup group;
496 * opaque key_exchange<1..2^16-1>;
497 * } KeyShareEntry;
498 */
Jerry Yu4a173382021-10-11 21:45:31 +0800499static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800500 const unsigned char *buf,
501 const unsigned char *end )
502{
Jerry Yub85277e2021-10-13 13:36:05 +0800503 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800504 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800505 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800506
Jerry Yu4a173382021-10-11 21:45:31 +0800507 /* ...
508 * NamedGroup group; (2 bytes)
509 * ...
510 */
511 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
512 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800513 p += 2;
514
Jerry Yu4a173382021-10-11 21:45:31 +0800515 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800516 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800517 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800518 {
519 MBEDTLS_SSL_DEBUG_MSG( 1,
520 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800521 (unsigned) offered_group, (unsigned) group ) );
522 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
523 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
524 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800525 }
526
527#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800528 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800529 {
530 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800531 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800532 if( ret != 0 )
533 return( ret );
534 }
Jerry Yub85277e2021-10-13 13:36:05 +0800535 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800536#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800537 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800538 {
539 /* Do something */
540 }
541 else
542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
543
544 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
545 return( ret );
546}
547
Jerry Yubc20bdd2021-08-24 15:59:48 +0800548#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
549
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800550/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800551 * CipherSuite cipher_suites<2..2^16-2>;
552 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800553static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800554 mbedtls_ssl_context *ssl,
555 unsigned char *buf,
556 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000557 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800558{
Jerry Yufec982e2021-09-07 17:26:06 +0800559 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800560 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000561 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800562 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800563
Xiaofei Baid25fab62021-12-02 06:36:27 +0000564 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800565
566 /*
567 * Ciphersuite list
568 *
569 * This is a list of the symmetric cipher options supported by
570 * the client, specifically the record protection algorithm
571 * ( including secret key length ) and a hash to be used with
572 * HKDF, in descending order of client preference.
573 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800574 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800575
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800576 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800577 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
578 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800579
Jerry Yu0c63af62021-09-02 12:59:12 +0800580 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000581 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800582 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800583 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800584 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800585 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800586
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800587 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800588 if( ciphersuite_info == NULL )
589 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800590 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
591 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800592 continue;
593
594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800595 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800596 ciphersuite_info->name ) );
597
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800598 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800599 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
600 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
601 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800602 }
603
Jerry Yu0c63af62021-09-02 12:59:12 +0800604 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000605 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800606 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800607 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800608 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
609 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800610
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800611 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000612 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800613
Jerry Yu6a643102021-08-31 14:40:36 +0800614 return( 0 );
615}
616
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800617/*
618 * Structure of ClientHello message:
619 *
620 * struct {
621 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
622 * Random random;
623 * opaque legacy_session_id<0..32>;
624 * CipherSuite cipher_suites<2..2^16-2>;
625 * opaque legacy_compression_methods<1..2^8-1>;
626 * Extension extensions<8..2^16-1>;
627 * } ClientHello;
628 */
Jerry Yu08906d02021-08-31 11:05:27 +0800629static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800630 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800631 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000632 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800633{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800634
Jerry Yubc20bdd2021-08-24 15:59:48 +0800635 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000636 unsigned char *p_extensions_len; /* Pointer to extensions length */
637 size_t output_len; /* Length of buffer used by function */
638 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800639
Jerry Yubc20bdd2021-08-24 15:59:48 +0800640 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800641 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800642
Xiaofei Baid25fab62021-12-02 06:36:27 +0000643 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800644
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800645 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800646 ssl->major_ver = ssl->conf->min_major_ver;
647 ssl->minor_ver = ssl->conf->min_minor_ver;
648
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800649 /*
650 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800651 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800652 *
653 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800654 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800655 */
Jerry Yufec982e2021-09-07 17:26:06 +0800656 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800657 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800658 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800659
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800660 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800661 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
662 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800663 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800664 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
665 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800666
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800667 /*
668 * Write legacy_session_id
669 *
670 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
671 * which has been merged with pre-shared keys in this version. A client
672 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
673 * this field to that value. In compatibility mode, this field MUST be
674 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
675 * a new 32-byte value. This value need not be random but SHOULD be
676 * unpredictable to avoid implementations fixating on a specific value
677 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
678 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800679 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100680#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
681 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
682 *p++ = (unsigned char)ssl->session_negotiate->id_len;
683 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
684 p += ssl->session_negotiate->id_len;
685
686 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
687 ssl->session_negotiate->id_len );
688#else
Jerry Yubbe09522021-09-06 21:17:54 +0800689 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
690 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100691#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800692
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800693 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800694 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800695 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800696 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800697 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800698
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800699 /* Write legacy_compression_methods
700 *
701 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800702 * one byte set to zero, which corresponds to the 'null' compression
703 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800704 */
Jerry Yubbe09522021-09-06 21:17:54 +0800705 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
706 *p++ = 1;
707 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800708
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800709 /* Write extensions */
710
711 /* Keeping track of the included extensions */
712 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800713
714 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800715 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000716 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800717 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800718
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800719 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800720 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800721 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800722 */
Jerry Yubbe09522021-09-06 21:17:54 +0800723 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800724 if( ret != 0 )
725 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800726 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800727
728#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800729
Jerry Yub925f212022-01-12 11:17:02 +0800730 /*
731 * Add the extensions related to (EC)DHE ephemeral key establishment only if
732 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800733 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800734 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
735 {
736 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
737 if( ret != 0 )
738 return( ret );
739 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800740
Jerry Yuf46b0162022-01-11 16:28:00 +0800741 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
742 if( ret != 0 )
743 return( ret );
744 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800745
Jerry Yuf017ee42022-01-12 15:49:48 +0800746 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800747 if( ret != 0 )
748 return( ret );
749 p += output_len;
750 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800751#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
752
Xiaofei Bai15a56812021-11-05 10:52:12 +0000753#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000754 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000755 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
756 if( ret != 0 )
757 return( ret );
758 p += output_len;
759#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
760
Jerry Yubc20bdd2021-08-24 15:59:48 +0800761 /* Add more extensions here */
762
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800763 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000764 extensions_len = p - p_extensions_len - 2;
765 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800767 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000768 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800769
Xiaofei Baid25fab62021-12-02 06:36:27 +0000770 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800771 return( 0 );
772}
773
Jerry Yu335aca92021-09-12 20:18:56 +0800774static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800775{
Jerry Yu92c6b402021-08-27 16:59:09 +0800776 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
777 return( 0 );
778}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800779
Jerry Yu92c6b402021-08-27 16:59:09 +0800780static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
781{
782 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800783
Jerry Yu92c6b402021-08-27 16:59:09 +0800784 if( ssl->conf->f_rng == NULL )
785 {
786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
787 return( MBEDTLS_ERR_SSL_NO_RNG );
788 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800789
Jerry Yu92c6b402021-08-27 16:59:09 +0800790 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
791 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800792 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800793 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800794 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800795 return( ret );
796 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800797
Ronald Cron49ad6192021-11-24 16:25:31 +0100798#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
799 /*
800 * Create a session identifier for the purpose of middlebox compatibility
801 * only if one has not been created already.
802 */
803 if( ssl->session_negotiate->id_len == 0 )
804 {
805 /* Creating a session id with 32 byte length */
806 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
807 ssl->session_negotiate->id, 32 ) ) != 0 )
808 {
809 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
810 return( ret );
811 }
812 ssl->session_negotiate->id_len = 32;
813 }
814#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
815
Jerry Yu6f13f642021-08-26 17:18:15 +0800816 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800817}
818
Jerry Yu92c6b402021-08-27 16:59:09 +0800819/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800820 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800821 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800822 */
823static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800824{
Jerry Yu92c6b402021-08-27 16:59:09 +0800825 int ret = 0;
826 unsigned char *buf;
827 size_t buf_len, msg_len;
828
829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
830
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800831 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800832
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800833 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
834 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
835 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800836
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800837 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800838 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800839 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800840
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800841 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
842 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800843 msg_len );
844 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800845
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800846 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
847 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
848 buf_len,
849 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800850
851cleanup:
852
853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
854 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800855}
856
Jerry Yu687101b2021-09-14 16:03:56 +0800857/*
Jerry Yu4a173382021-10-11 21:45:31 +0800858 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800859 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800860/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800861 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
862 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000863 * to indicate which message is expected and to be parsed next.
864 */
Jerry Yub85277e2021-10-13 13:36:05 +0800865#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
866#define SSL_SERVER_HELLO_COORDINATE_HRR 1
867static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
868 const unsigned char *buf,
869 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800870{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800871 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800872 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
873 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
874 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
875 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
876
877 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
878 *
Jerry Yu4a173382021-10-11 21:45:31 +0800879 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800880 * special value of the SHA-256 of "HelloRetryRequest".
881 *
882 * struct {
883 * ProtocolVersion legacy_version = 0x0303;
884 * Random random;
885 * opaque legacy_session_id_echo<0..32>;
886 * CipherSuite cipher_suite;
887 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800888 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800889 * } ServerHello;
890 *
891 */
Jerry Yub85277e2021-10-13 13:36:05 +0800892 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800893
894 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800895 {
Jerry Yub85277e2021-10-13 13:36:05 +0800896 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800897 }
898
Jerry Yub85277e2021-10-13 13:36:05 +0800899 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800900}
901
Jerry Yu745bb612021-10-13 22:01:04 +0800902/* Fetch and preprocess
903 * Returns a negative value on failure, and otherwise
904 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
905 * - SSL_SERVER_HELLO_COORDINATE_HRR
906 */
Jerry Yub85277e2021-10-13 13:36:05 +0800907static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
908 unsigned char **buf,
909 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800910{
Jerry Yu4a173382021-10-11 21:45:31 +0800911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800912
913 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
914
Jerry Yue1b9c292021-09-10 10:08:31 +0800915 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
916 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
917 {
918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
919
920 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
921 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
922 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
923 }
924
925 *buf = ssl->in_msg + 4;
926 *buf_len = ssl->in_hslen - 4;
927
Jerry Yub85277e2021-10-13 13:36:05 +0800928 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
929 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800930 {
Jerry Yu745bb612021-10-13 22:01:04 +0800931 case SSL_SERVER_HELLO_COORDINATE_HELLO:
932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
933 break;
934 case SSL_SERVER_HELLO_COORDINATE_HRR:
935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
936 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800937 }
938
939cleanup:
940
941 return( ret );
942}
943
Jerry Yu4a173382021-10-11 21:45:31 +0800944static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
945 const unsigned char **buf,
946 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800947{
948 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800949 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800950
Jerry Yude4fb2c2021-09-19 18:05:08 +0800951 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800952 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800953
Jerry Yu4a173382021-10-11 21:45:31 +0800954 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800955
956 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800957 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
958 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800959 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800960 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
961 ssl->session_negotiate->id,
962 ssl->session_negotiate->id_len );
963 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800964 legacy_session_id_echo_len );
965
966 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
967 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
968
Jerry Yue1b9c292021-09-10 10:08:31 +0800969 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
970 }
971
Jerry Yu4a173382021-10-11 21:45:31 +0800972 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800973 *buf = p;
974
975 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800976 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800977 return( 0 );
978}
979
Jerry Yu4a173382021-10-11 21:45:31 +0800980static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
981 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800982{
Jerry Yu4a173382021-10-11 21:45:31 +0800983 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
984
Jerry Yue1b9c292021-09-10 10:08:31 +0800985 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800986 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800987 {
Jerry Yu4a173382021-10-11 21:45:31 +0800988 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800989 {
990 return( 1 );
991 }
992 }
993 return( 0 );
994}
995
996/* Parse ServerHello message and configure context
997 *
998 * struct {
999 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1000 * Random random;
1001 * opaque legacy_session_id_echo<0..32>;
1002 * CipherSuite cipher_suite;
1003 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001004 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001005 * } ServerHello;
1006 */
Jerry Yuc068b662021-10-11 22:30:19 +08001007static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1008 const unsigned char *buf,
XiaokangQianb851da82022-01-14 04:03:11 +00001009 const unsigned char *end,
1010 int hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001011{
Jerry Yub85277e2021-10-13 13:36:05 +08001012 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001013 const unsigned char *p = buf;
Jerry Yub85277e2021-10-13 13:36:05 +08001014 size_t extensions_len;
1015 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001016 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001017 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb851da82022-01-14 04:03:11 +00001018#if defined(MBEDTLS_SSL_COOKIE_C)
1019 size_t cookie_len;
1020 unsigned char *cookie;
1021#endif /* MBEDTLS_SSL_COOKIE_C */
Jerry Yue1b9c292021-09-10 10:08:31 +08001022
1023 /*
1024 * Check there is space for minimal fields
1025 *
1026 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001027 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001028 * - legacy_session_id_echo ( 1 byte ), minimum size
1029 * - cipher_suite ( 2 bytes)
1030 * - legacy_compression_method ( 1 byte )
1031 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001032 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001033
1034 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001035 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1036
Jerry Yu4a173382021-10-11 21:45:31 +08001037 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001038 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001039 * ...
1040 * with ProtocolVersion defined as:
1041 * uint16 ProtocolVersion;
1042 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001043 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1044 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1045 {
1046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1047 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1048 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1049 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1050 }
1051 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001052
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001053 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001054 * Random random;
1055 * ...
1056 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001057 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001058 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001059 memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1060 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yue1b9c292021-09-10 10:08:31 +08001061 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001062 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1063 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001064
Jerry Yu4a173382021-10-11 21:45:31 +08001065 /* ...
1066 * opaque legacy_session_id_echo<0..32>;
1067 * ...
1068 */
1069 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001070 {
1071 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1072 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1073 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1074 }
1075
Jerry Yu4a173382021-10-11 21:45:31 +08001076 /* ...
1077 * CipherSuite cipher_suite;
1078 * ...
1079 * with CipherSuite defined as:
1080 * uint8 CipherSuite[2];
1081 */
1082 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001083 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1084 p += 2;
1085
Jerry Yu4a173382021-10-11 21:45:31 +08001086
1087 /*
1088 * Check whether this ciphersuite is supported and offered.
1089 * Via the force_ciphersuite version we may have instructed the client
1090 * to use a different ciphersuite.
1091 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001092 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001093 if( ciphersuite_info == NULL ||
1094 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001095 {
Jerry Yub85277e2021-10-13 13:36:05 +08001096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
Jerry Yue1b9c292021-09-10 10:08:31 +08001097 cipher_suite ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001098
Jerry Yu4a173382021-10-11 21:45:31 +08001099 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1100 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1101 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +08001102 }
1103
Jerry Yue1b9c292021-09-10 10:08:31 +08001104
Jerry Yu4a173382021-10-11 21:45:31 +08001105 /* Configure ciphersuites */
1106 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1107
1108 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001109 ssl->session_negotiate->ciphersuite = cipher_suite;
1110
Jerry Yue1b9c292021-09-10 10:08:31 +08001111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1112 cipher_suite, ciphersuite_info->name ) );
1113
1114#if defined(MBEDTLS_HAVE_TIME)
1115 ssl->session_negotiate->start = time( NULL );
1116#endif /* MBEDTLS_HAVE_TIME */
1117
Jerry Yu4a173382021-10-11 21:45:31 +08001118 /* ...
1119 * uint8 legacy_compression_method = 0;
1120 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001121 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001122 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001123 if( p[0] != 0 )
1124 {
Jerry Yub85277e2021-10-13 13:36:05 +08001125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001126 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1127 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1128 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1129 }
1130 p++;
1131
Jerry Yub85277e2021-10-13 13:36:05 +08001132 /* ...
1133 * Extension extensions<6..2^16-1>;
1134 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001135 * struct {
1136 * ExtensionType extension_type; (2 bytes)
1137 * opaque extension_data<0..2^16-1>;
1138 * } Extension;
1139 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001140 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001141 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001142 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001143
Jerry Yu4a173382021-10-11 21:45:31 +08001144 /* Check extensions do not go beyond the buffer of data. */
1145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1146 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001147
Jerry Yu4a173382021-10-11 21:45:31 +08001148 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001149
Jerry Yu4a173382021-10-11 21:45:31 +08001150 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 {
1152 unsigned int extension_type;
1153 size_t extension_data_len;
1154
Jerry Yu4a173382021-10-11 21:45:31 +08001155 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001156 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1157 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1158 p += 4;
1159
Jerry Yu4a173382021-10-11 21:45:31 +08001160 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001161
1162 switch( extension_type )
1163 {
XiaokangQianb851da82022-01-14 04:03:11 +00001164#if defined(MBEDTLS_SSL_COOKIE_C)
1165 case MBEDTLS_TLS_EXT_COOKIE:
1166
1167 /* Retrieve length field of cookie */
1168 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 2 );
1169 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1170 cookie = (unsigned char *) ( p + 2 );
1171
1172 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, cookie_len + 2 );
1173 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", cookie, cookie_len );
1174
1175 mbedtls_free( ssl->handshake->verify_cookie );
1176 ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
1177 if( ssl->handshake->verify_cookie == NULL )
1178 {
1179 MBEDTLS_SSL_DEBUG_MSG( 1,
1180 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
1181 cookie_len ) );
1182 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1183 }
1184
1185 memcpy( ssl->handshake->verify_cookie, cookie, cookie_len );
1186 ssl->handshake->verify_cookie_len = (unsigned char) cookie_len;
1187 break;
1188#endif /* MBEDTLS_SSL_COOKIE_C */
1189
Jerry Yue1b9c292021-09-10 10:08:31 +08001190 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1191 MBEDTLS_SSL_DEBUG_MSG( 3,
1192 ( "found supported_versions extension" ) );
1193
Jerry Yuc068b662021-10-11 22:30:19 +08001194 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001195 p,
1196 p + extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001197 if( ret != 0 )
1198 return( ret );
1199 break;
1200
1201 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001204
1205 MBEDTLS_SSL_PEND_FATAL_ALERT(
1206 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1207 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1208 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001209
1210#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1211 case MBEDTLS_TLS_EXT_KEY_SHARE:
1212 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQianb851da82022-01-14 04:03:11 +00001213 if( hrr )
1214 ret = ssl_tls13_hrr_check_key_share_ext( ssl,
1215 p, p + extension_data_len );
1216 else
1217 ret = ssl_tls13_parse_key_share_ext( ssl,
1218 p, p + extension_data_len );
1219 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001220 {
1221 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001222 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001223 ret );
1224 return( ret );
1225 }
1226 break;
1227#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1228
1229 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001230 MBEDTLS_SSL_DEBUG_MSG(
1231 3,
1232 ( "unknown extension found: %u ( ignoring )",
1233 extension_type ) );
1234
1235 MBEDTLS_SSL_PEND_FATAL_ALERT(
1236 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1237 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1238 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001239 }
1240
1241 p += extension_data_len;
1242 }
1243
1244 return( 0 );
1245}
1246
Jerry Yuc068b662021-10-11 22:30:19 +08001247static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001248{
Jerry Yub85277e2021-10-13 13:36:05 +08001249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001250 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001251 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001252 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001253
Jerry Yub85277e2021-10-13 13:36:05 +08001254 /* Determine the key exchange mode:
1255 * 1) If both the pre_shared_key and key_share extensions were received
1256 * then the key exchange mode is PSK with EPHEMERAL.
1257 * 2) If only the pre_shared_key extension was received then the key
1258 * exchange mode is PSK-only.
1259 * 3) If only the key_share extension was received then the key
1260 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001261 */
Jerry Yub85277e2021-10-13 13:36:05 +08001262 switch( handshake->extensions_present &
1263 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001264 {
Jerry Yu745bb612021-10-13 22:01:04 +08001265 /* Only the pre_shared_key extension was received */
1266 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001267 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001268 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001269
Jerry Yu745bb612021-10-13 22:01:04 +08001270 /* Only the key_share extension was received */
1271 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001272 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001273 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001274
Jerry Yu745bb612021-10-13 22:01:04 +08001275 /* Both the pre_shared_key and key_share extensions were received */
1276 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001277 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001278 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001279
Jerry Yu745bb612021-10-13 22:01:04 +08001280 /* Neither pre_shared_key nor key_share extension was received */
1281 default:
1282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1283 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1284 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001285 }
1286
1287 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1288 *
1289 * TODO: We don't have to do this in case we offered 0-RTT and the
1290 * server accepted it. In this case, we could skip generating
1291 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001292 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001293 if( ret != 0 )
1294 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001296 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001297 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001298 }
1299
1300 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001301 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001302 if( ret != 0 )
1303 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001305 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001306 }
1307
1308 /* Next evolution in key schedule: Establish handshake secret and
1309 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001310 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001311 if( ret != 0 )
1312 {
Jerry Yuc068b662021-10-11 22:30:19 +08001313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1314 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001315 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001316 }
1317
Jerry Yub85277e2021-10-13 13:36:05 +08001318 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001319 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001320 {
1321 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1322 goto cleanup;
1323 }
Jerry Yu0b177842021-09-05 19:41:30 +08001324
1325 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1326 ssl->conf->endpoint,
1327 ssl->session_negotiate->ciphersuite,
1328 &traffic_keys,
1329 ssl );
1330 if( ret != 0 )
1331 {
1332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001333 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001334 }
1335
Jerry Yub85277e2021-10-13 13:36:05 +08001336 handshake->transform_handshake = transform_handshake;
1337 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001338
1339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1340 ssl->session_in = ssl->session_negotiate;
1341
1342 /*
1343 * State machine update
1344 */
1345 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1346
Jerry Yu4a173382021-10-11 21:45:31 +08001347cleanup:
1348
Jerry Yu0b177842021-09-05 19:41:30 +08001349 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001350 if( ret != 0 )
1351 {
Jerry Yub85277e2021-10-13 13:36:05 +08001352 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001353
Jerry Yu4a173382021-10-11 21:45:31 +08001354 MBEDTLS_SSL_PEND_FATAL_ALERT(
1355 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1356 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1357 }
1358 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001359}
1360
XiaokangQian647719a2021-12-07 09:16:29 +00001361static int ssl_hrr_postprocess( mbedtls_ssl_context *ssl )
1362{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001363#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001365#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001366
1367 if( ssl->handshake->hello_retry_requests_received > 0 )
1368 {
1369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1370 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1371 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1372 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1373 }
1374
1375 ssl->handshake->hello_retry_requests_received++;
1376
1377#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1378 /* If not offering early data, the client sends a dummy CCS record
1379 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001380 * its second ClientHello or before its encrypted handshake flight.
1381 */
XiaokangQian647719a2021-12-07 09:16:29 +00001382 mbedtls_ssl_handshake_set_state( ssl,
1383 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1384#else
1385 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1386#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1387
1388 mbedtls_ssl_tls13_session_reset_msg_layer( ssl, 0 );
1389
1390 /* Reset everything that's going to be re-generated in the new ClientHello.
1391 *
1392 * Currently, we're always resetting the key share, even if the server
1393 * was fine with it. Once we have separated key share generation from
1394 * key share writing, we can confine this to the case where the server
XiaokangQian51eff222021-12-10 10:33:56 +00001395 * requested a different share.
1396 */
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001397#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001398 ret = ssl_reset_key_share( ssl );
1399 if( ret != 0 )
1400 return( ret );
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001401#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001402
1403 return( 0 );
1404}
1405
Jerry Yue1b9c292021-09-10 10:08:31 +08001406/*
Jerry Yu4a173382021-10-11 21:45:31 +08001407 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001408 * Handler for MBEDTLS_SSL_SERVER_HELLO
1409 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001410static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001411{
Jerry Yu4a173382021-10-11 21:45:31 +08001412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001413 unsigned char *buf = NULL;
1414 size_t buf_len = 0;
XiaokangQianb851da82022-01-14 04:03:11 +00001415 int hrr = -1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001416
1417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1418
1419 /* Coordination step
1420 * - Fetch record
1421 * - Make sure it's either a ServerHello or a HRR.
1422 * - Switch processing routine in case of HRR
1423 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001424 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1425 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1426
XiaokangQianb851da82022-01-14 04:03:11 +00001427 hrr = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001428 /* Parsing step
1429 * We know what message to expect by now and call
1430 * the respective parsing function.
1431 */
XiaokangQianb851da82022-01-14 04:03:11 +00001432 MBEDTLS_SSL_DEBUG_MSG( 2, ( " hrr = %d ", hrr ) );
1433 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
1434 buf + buf_len,
1435 hrr ) );
1436 if( hrr == SSL_SERVER_HELLO_COORDINATE_HRR )
XiaokangQian647719a2021-12-07 09:16:29 +00001437 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1438
XiaokangQianb851da82022-01-14 04:03:11 +00001439 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1440 MBEDTLS_SSL_HS_SERVER_HELLO,
1441 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001442
XiaokangQianb851da82022-01-14 04:03:11 +00001443 if( hrr == SSL_SERVER_HELLO_COORDINATE_HELLO )
1444 {
1445 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
1446 }
1447 else if( hrr == SSL_SERVER_HELLO_COORDINATE_HRR )
1448 {
XiaokangQian647719a2021-12-07 09:16:29 +00001449 MBEDTLS_SSL_PROC_CHK( ssl_hrr_postprocess( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001450 }
1451
1452cleanup:
1453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
1454 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001455}
1456
1457/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001458 *
1459 * EncryptedExtensions message
1460 *
1461 * The EncryptedExtensions message contains any extensions which
1462 * should be protected, i.e., any which are not needed to establish
1463 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001464 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001465
1466/*
1467 * Overview
1468 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001469
1470/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001471static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001472
XiaokangQian97799ac2021-10-11 10:05:54 +00001473static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1474 const unsigned char *buf,
1475 const unsigned char *end );
1476static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001477
1478/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001479 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001480 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001481static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001482{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001483 int ret;
1484 unsigned char *buf;
1485 size_t buf_len;
1486
1487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1488
Xiaofei Bai746f9482021-11-12 08:53:56 +00001489 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001490 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001491 &buf, &buf_len ) );
1492
1493 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001494 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001495 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001496
Xiaofei Bai746f9482021-11-12 08:53:56 +00001497 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001498 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001499
XiaokangQian97799ac2021-10-11 10:05:54 +00001500 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001501
1502cleanup:
1503
1504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1505 return( ret );
1506
1507}
1508
XiaokangQian08da26c2021-10-09 10:12:11 +00001509/* Parse EncryptedExtensions message
1510 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001511 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001512 * } EncryptedExtensions;
1513 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001514static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1515 const unsigned char *buf,
1516 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001517{
1518 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001519 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001520 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001521 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001522
XiaokangQian08da26c2021-10-09 10:12:11 +00001523 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001524 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001525 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001526
XiaokangQian97799ac2021-10-11 10:05:54 +00001527 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001528 extensions_end = p + extensions_len;
1529 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001530
XiaokangQian8db25ff2021-10-13 05:56:18 +00001531 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001532 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001533 unsigned int extension_type;
1534 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001535
XiaokangQian08da26c2021-10-09 10:12:11 +00001536 /*
1537 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001538 * ExtensionType extension_type; (2 bytes)
1539 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001540 * } Extension;
1541 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001542 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001543 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1544 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1545 p += 4;
1546
XiaokangQian8db25ff2021-10-13 05:56:18 +00001547 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001548
XiaokangQian97799ac2021-10-11 10:05:54 +00001549 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001550 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001551 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001552 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001553 switch( extension_type )
1554 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001555
XiaokangQian08da26c2021-10-09 10:12:11 +00001556 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1558 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001559
XiaokangQian08da26c2021-10-09 10:12:11 +00001560 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001561 MBEDTLS_SSL_DEBUG_MSG(
1562 3, ( "unsupported extension found: %u ", extension_type) );
1563 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001564 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001565 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1566 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001567 }
1568
XiaokangQian08da26c2021-10-09 10:12:11 +00001569 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001570 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001571
XiaokangQian97799ac2021-10-11 10:05:54 +00001572 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001573 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001574 {
1575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001576 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001577 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001578 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001579 }
1580
1581 return( ret );
1582}
1583
XiaokangQian97799ac2021-10-11 10:05:54 +00001584static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001585{
Jerry Yua93ac112021-10-27 16:31:48 +08001586#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001587 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001588 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1589 else
Jerry Yud2674312021-10-29 10:08:19 +08001590 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001591#else
1592 ((void) ssl);
1593 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1594#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001595 return( 0 );
1596}
1597
Jerry Yua93ac112021-10-27 16:31:48 +08001598#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001599/*
Jerry Yud2674312021-10-29 10:08:19 +08001600 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1601 */
1602static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1603{
1604 int ret = mbedtls_ssl_read_record( ssl, 0 );
1605
1606 if( ret != 0 )
1607 {
1608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1609 return( ret );
1610 }
1611
1612 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1613 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1614 {
1615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1616 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1617 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1618 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1619 }
1620
1621 ssl->keep_current_message = 1;
1622 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1623
1624 return( 0 );
1625}
1626
1627/*
Jerry Yu687101b2021-09-14 16:03:56 +08001628 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1629 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001630static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001631{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001632 int ret;
1633
1634 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001635 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001636 return( ret );
1637
Jerry Yu687101b2021-09-14 16:03:56 +08001638 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1639 return( 0 );
1640}
1641
1642/*
1643 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1644 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001645static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001646{
Jerry Yu30b071c2021-09-12 20:16:03 +08001647 int ret;
1648
1649 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1650 if( ret != 0 )
1651 return( ret );
1652
Jerry Yu687101b2021-09-14 16:03:56 +08001653 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1654 return( 0 );
1655}
Jerry Yua93ac112021-10-27 16:31:48 +08001656#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001657
Jerry Yu687101b2021-09-14 16:03:56 +08001658/*
1659 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1660 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001661static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001662{
XiaokangQianac0385c2021-11-03 06:40:11 +00001663 int ret;
1664
XiaokangQianc5c39d52021-11-09 11:55:10 +00001665 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001666 if( ret != 0 )
1667 return( ret );
1668
Ronald Cron49ad6192021-11-24 16:25:31 +01001669#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1670 mbedtls_ssl_handshake_set_state(
1671 ssl,
1672 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1673#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001674 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001675#endif
1676
XiaokangQianac0385c2021-11-03 06:40:11 +00001677 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001678}
1679
1680/*
Ronald Crond4c64022021-12-06 09:06:46 +01001681 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1682 */
1683#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1684static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1685{
1686 int ret;
1687
1688 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1689 if( ret != 0 )
1690 return( ret );
1691
Ronald Crond4c64022021-12-06 09:06:46 +01001692 return( 0 );
1693}
1694#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1695
1696/*
Jerry Yu687101b2021-09-14 16:03:56 +08001697 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1698 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001699static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001700{
XiaokangQian0fa66432021-11-15 03:33:57 +00001701 int ret;
1702
Ronald Cron49ad6192021-11-24 16:25:31 +01001703 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1704
XiaokangQian0fa66432021-11-15 03:33:57 +00001705 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1706 if( ret != 0 )
1707 return( ret );
1708
1709 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1710 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001711}
1712
1713/*
1714 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1715 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001716static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001717{
Jerry Yu378254d2021-10-30 21:44:47 +08001718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001719 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001720 return( 0 );
1721}
1722
1723/*
1724 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1725 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001726static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001727{
Jerry Yu378254d2021-10-30 21:44:47 +08001728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1729 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1730
1731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1732 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1733
1734 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1735
1736 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1737 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001738}
1739
Jerry Yu92c6b402021-08-27 16:59:09 +08001740int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001741{
Jerry Yu92c6b402021-08-27 16:59:09 +08001742 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001743
Jerry Yue3b34122021-09-28 17:53:35 +08001744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1745 mbedtls_ssl_states_str( ssl->state ),
1746 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001747
1748 switch( ssl->state )
1749 {
1750 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001751 * ssl->state is initialized as HELLO_REQUEST. It is the same
1752 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001753 */
1754 case MBEDTLS_SSL_HELLO_REQUEST:
1755 case MBEDTLS_SSL_CLIENT_HELLO:
1756 ret = ssl_tls13_write_client_hello( ssl );
1757 break;
1758
1759 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001760 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001761 break;
1762
1763 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001764 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001765 break;
1766
Jerry Yua93ac112021-10-27 16:31:48 +08001767#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001768 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1769 ret = ssl_tls13_process_certificate_request( ssl );
1770 break;
1771
Jerry Yu687101b2021-09-14 16:03:56 +08001772 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001773 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001774 break;
1775
1776 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001777 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001778 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001779#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001780
1781 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001782 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001783 break;
1784
Jerry Yu687101b2021-09-14 16:03:56 +08001785 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001786 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001787 break;
1788
1789 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001790 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001791 break;
1792
1793 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001794 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001795 break;
1796
Ronald Cron49ad6192021-11-24 16:25:31 +01001797 /*
1798 * Injection of dummy-CCS's for middlebox compatibility
1799 */
1800#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1801 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001802 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01001803 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001804 break;
1805#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1806
Jerry Yu92c6b402021-08-27 16:59:09 +08001807 default:
1808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1809 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1810 }
1811
1812 return( ret );
1813}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001814
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001815#endif /* MBEDTLS_SSL_CLI_C */
1816
Ronald Cron6f135e12021-12-08 16:57:54 +01001817#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */