blob: 9e0ea7950e31578d51bbe297d64d832a8e1e328b [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
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
20#include "common.h"
21
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080023
Jerry Yu687101b2021-09-14 16:03:56 +080024#include "mbedtls/debug.h"
25
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080026#include "ssl_misc.h"
XiaokangQian7807f9f2022-02-15 10:04:37 +000027#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010028#include "ssl_debug_helpers.h"
XiaokangQian7807f9f2022-02-15 10:04:37 +000029#include <string.h>
30#if defined(MBEDTLS_ECP_C)
31#include "mbedtls/ecp.h"
XiaokangQian7807f9f2022-02-15 10:04:37 +000032#endif /* MBEDTLS_ECP_C */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080033
XiaokangQiana9c58412022-02-17 09:41:26 +000034#if defined(MBEDTLS_PLATFORM_C)
35#include "mbedtls/platform.h"
36#else
37#include <stdlib.h>
38#define mbedtls_calloc calloc
39#define mbedtls_free free
40#endif /* MBEDTLS_PLATFORM_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +000041
42/* From RFC 8446:
43 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +000044 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +000045 * } SupportedVersions;
46 */
47static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
48 const unsigned char *buf,
49 const unsigned char *end )
50{
XiaokangQian7807f9f2022-02-15 10:04:37 +000051 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +000052 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +000053 const unsigned char *versions_end;
XiaokangQian8f9dfe42022-04-15 02:52:39 +000054 int major_ver, minor_ver;
55 int tls13_supported = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +000056
57 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
XiaokangQian4080a7f2022-04-11 09:55:18 +000058 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +000059 p += 1;
60
XiaokangQian4080a7f2022-04-11 09:55:18 +000061 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, versions_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +000062 versions_end = p + versions_len;
63 while( p < versions_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +000064 {
XiaokangQiancfd925f2022-04-14 07:10:37 +000065 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, versions_end, 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +000066 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
XiaokangQianb67384d2022-04-19 00:02:38 +000067 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +000068
69 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
70 if( major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
71 minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
72 {
73 tls13_supported = 1;
74 break;
75 }
XiaokangQian7807f9f2022-02-15 10:04:37 +000076 }
77
XiaokangQianb67384d2022-04-19 00:02:38 +000078 if( !tls13_supported )
XiaokangQian7807f9f2022-02-15 10:04:37 +000079 {
XiaokangQian7807f9f2022-02-15 10:04:37 +000080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
81
82 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
83 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
84 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
85 }
86
87 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%d:%d]",
88 major_ver, minor_ver ) );
89
90 ssl->major_ver = major_ver;
91 ssl->minor_ver = minor_ver;
XiaokangQian7807f9f2022-02-15 10:04:37 +000092 return( 0 );
93}
94
XiaokangQian8f9dfe42022-04-15 02:52:39 +000095#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +000096/* This function parses the TLS 1.3 supported_groups extension and
97 * stores the received groups in ssl->handshake->curves.
98 *
99 * From RFC 8446:
100 * enum {
101 * ... (0xFFFF)
102 * } NamedGroup;
103 * struct {
104 * NamedGroup named_group_list<2..2^16-1>;
105 * } NamedGroupList;
106 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000107static int ssl_tls13_parse_supported_groups_ext(
XiaokangQian7807f9f2022-02-15 10:04:37 +0000108 mbedtls_ssl_context *ssl,
109 const unsigned char *buf, const unsigned char *end )
110{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000111 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000112 size_t named_group_list_len, curve_list_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000113 const mbedtls_ecp_curve_info *curve_info, **curves;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000114 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000115
116 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000117 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000118 named_group_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000119 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000120 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, named_group_list_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000121
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000122 /* At the moment, this can happen when receiving a second
123 * ClientHello after an HRR. We should properly reset the
124 * state upon receiving an HRR, in which case we should
125 * not observe handshake->curves already being allocated. */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000126 if( ssl->handshake->curves != NULL )
127 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000128 mbedtls_free( ssl->handshake->curves );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000129 ssl->handshake->curves = NULL;
130 }
131
132 /* Don't allow our peer to make us allocate too much memory,
XiaokangQianc5763b52022-04-02 03:34:37 +0000133 * and leave room for a final 0
134 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000135 curve_list_len = named_group_list_len / 2 + 1;
136 if( curve_list_len > MBEDTLS_ECP_DP_MAX )
137 curve_list_len = MBEDTLS_ECP_DP_MAX;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000138
XiaokangQian4080a7f2022-04-11 09:55:18 +0000139 if( ( curves = mbedtls_calloc( curve_list_len, sizeof( *curves ) ) ) == NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000140 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
141
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000142 named_group_list_end = p + named_group_list_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000143 ssl->handshake->curves = curves;
144
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000145 while ( p < named_group_list_end && curve_list_len > 1 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000146 {
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000147 uint16_t tls_grp_id;
148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, named_group_list_end, 2 );
149 tls_grp_id = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000150 curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_grp_id );
151
152 /* mbedtls_ecp_curve_info_from_tls_id() uses the mbedtls_ecp_curve_info
153 * data structure (defined in ecp.c), which only includes the list of
154 * curves implemented. Hence, we only add curves that are also supported
XiaokangQianc5763b52022-04-02 03:34:37 +0000155 * and implemented by the server.
156 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000157 if( curve_info != NULL )
158 {
159 *curves++ = curve_info;
160 MBEDTLS_SSL_DEBUG_MSG( 4, ( "supported curve: %s", curve_info->name ) );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000161 curve_list_len--;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000162 }
163
164 p += 2;
165 }
166
167 return( 0 );
168
169}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000170#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000171
XiaokangQian88408882022-04-02 10:15:03 +0000172#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000173/*
174 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
175 * extension is correct and stores the provided key shares. Whether this is an
176 * acceptable key share depends on the selected ciphersuite.
177 *
178 * Possible return values are:
179 * - 0: Successful processing of the client provided key share extension.
180 * - MBEDTLS_ERR_SSL_HRR_REQUIRED: The key share provided by the client
181 * does not match a group supported by the server. A HelloRetryRequest will
182 * be needed.
183 * - Another negative return value for fatal errors.
184*/
185
186static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
187 const unsigned char *buf,
188 const unsigned char *end )
189{
XiaokangQianb67384d2022-04-19 00:02:38 +0000190 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000191 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000192 unsigned char const *client_shares_end;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000193 size_t client_shares_len, key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000194 int match_found = 0;
195
196 /* From RFC 8446:
197 *
198 * struct {
199 * KeyShareEntry client_shares<0..2^16-1>;
200 * } KeyShareClientHello;
201 *
202 */
203
XiaokangQian7807f9f2022-02-15 10:04:37 +0000204 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000205 client_shares_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000206 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000207 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, client_shares_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000208
209 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000210 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000211
212 /* We try to find a suitable key share entry and copy it to the
213 * handshake context. Later, we have to find out whether we can do
214 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000215 * dismiss it and send a HelloRetryRequest message.
216 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000217
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000218 for( ; p < client_shares_end; p += key_exchange_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000219 {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000220 uint16_t group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000221
222 /*
223 * struct {
224 * NamedGroup group;
225 * opaque key_exchange<1..2^16-1>;
226 * } KeyShareEntry;
227 */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000228 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, 4 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000229 group = MBEDTLS_GET_UINT16_BE( p, 0 );
230 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000231 key_exchange_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000232 p += 2;
XiaokangQianb67384d2022-04-19 00:02:38 +0000233 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, key_exchange_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000234
235 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000236 * for input validation purposes.
237 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000238 if( match_found == 1 )
239 continue;
240
241 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000242 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000243 */
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000244 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
245 {
246 const mbedtls_ecp_curve_info *curve_info =
247 mbedtls_ecp_curve_info_from_tls_id( group );
248 if( curve_info == NULL )
249 {
250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000251 continue;
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000252 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000253
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000254 match_found = 1;
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
XiaokangQian3f84d5d2022-04-19 06:36:17 +0000256 ret = psa_crypto_init();
257 if( ret != PSA_SUCCESS )
258 {
259 MBEDTLS_SSL_DEBUG_RET( 1, "psa_crypto_init()", ret );
260 return( ret );
261 }
XiaokangQianb67384d2022-04-19 00:02:38 +0000262 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p - 2, end - p + 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000263 if( ret != 0 )
264 return( ret );
265 }
266 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000267 {
268 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000269 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000270 continue;
271 }
272
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000273 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000274 }
275
276 if( match_found == 0 )
277 {
278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
279 return( MBEDTLS_ERR_SSL_HRR_REQUIRED );
280 }
281 return( 0 );
282}
XiaokangQian88408882022-04-02 10:15:03 +0000283#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000284
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000285#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000286static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000287{
XiaokangQian3207a322022-02-23 03:15:27 +0000288 ((void) ssl);
289
XiaokangQian7807f9f2022-02-15 10:04:37 +0000290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
XiaokangQianb67384d2022-04-19 00:02:38 +0000291 MBEDTLS_SSL_DEBUG_MSG( 3,
292 ( "- KEY_SHARE_EXTENSION ( %s )",
293 ( ( ssl->handshake->extensions_present
294 & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ? "TRUE" : "FALSE" ) );
295 MBEDTLS_SSL_DEBUG_MSG( 3,
296 ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
297 ( ( ssl->handshake->extensions_present
298 & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
299 "TRUE" : "FALSE" ) );
300 MBEDTLS_SSL_DEBUG_MSG( 3,
301 ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
302 ( ( ssl->handshake->extensions_present
303 & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ? "TRUE" : "FALSE" ) );
304 MBEDTLS_SSL_DEBUG_MSG( 3,
305 ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
306 ( ( ssl->handshake->extensions_present
307 & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ? "TRUE" : "FALSE" ) );
308 MBEDTLS_SSL_DEBUG_MSG( 3,
309 ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
310 ( ( ssl->handshake->extensions_present
311 & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
312 "TRUE" : "FALSE" ) );
313 MBEDTLS_SSL_DEBUG_MSG( 3,
314 ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
315 ( ( ssl->handshake->extensions_present
316 & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
317 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000318#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
XiaokangQianb67384d2022-04-19 00:02:38 +0000319 MBEDTLS_SSL_DEBUG_MSG( 3,
320 ( "- SERVERNAME_EXTENSION ( %s )",
321 ( ( ssl->handshake->extensions_present
322 & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
323 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000324#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000325}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000326#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000327
XiaokangQian4080a7f2022-04-11 09:55:18 +0000328static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000329 int exts_mask )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000330{
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000331 int masked = ssl->handshake->extensions_present & exts_mask;
332 return( masked == exts_mask );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000333}
334
XiaokangQianb67384d2022-04-19 00:02:38 +0000335static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
336 mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000337{
XiaokangQian4080a7f2022-04-11 09:55:18 +0000338 return( ssl_tls13_client_hello_has_exts( ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000339 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
340 MBEDTLS_SSL_EXT_KEY_SHARE |
341 MBEDTLS_SSL_EXT_SIG_ALG ) );
342}
343
XiaokangQianb67384d2022-04-19 00:02:38 +0000344static int ssl_tls13_check_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000345{
346 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
347 return( 0 );
348
XiaokangQianb67384d2022-04-19 00:02:38 +0000349 if( !ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000350 return( 0 );
351
XiaokangQianb67384d2022-04-19 00:02:38 +0000352 ssl->handshake->tls13_kex_modes =
353 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000354 return( 1 );
355}
356
XiaokangQian4080a7f2022-04-11 09:55:18 +0000357/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000358 *
359 * STATE HANDLING: ClientHello
360 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000361 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000362 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000363 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000364 *
365 * In this case, the server progresses to sending its ServerHello.
366 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000367 * 2) The ClientHello was well-formed but didn't match the server's
368 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000369 *
370 * For example, the client might not have offered a key share which
371 * the server supports, or the server might require a cookie.
372 *
373 * In this case, the server sends a HelloRetryRequest.
374 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000375 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000376 *
377 * In this case, we abort the handshake.
378 *
379 */
380
381/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000382 * Structure of this message:
383 *
384 * uint16 ProtocolVersion;
385 * opaque Random[32];
XiaokangQian4080a7f2022-04-11 09:55:18 +0000386 * uint8 CipherSuite[2]; // Cryptographic suite selector
387 *
388 * struct {
389 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
390 * Random random;
391 * opaque legacy_session_id<0..32>;
392 * CipherSuite cipher_suites<2..2^16-2>;
393 * opaque legacy_compression_methods<1..2^8-1>;
394 * Extension extensions<8..2^16-1>;
395 * } ClientHello;
396 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000397
398#define SSL_CLIENT_HELLO_OK 0
399#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
400
XiaokangQian4080a7f2022-04-11 09:55:18 +0000401static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
402 const unsigned char *buf,
403 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000404{
XiaokangQianb67384d2022-04-19 00:02:38 +0000405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
406 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000407 size_t legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000408 size_t cipher_suites_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000409 const unsigned char *cipher_suites_start;
XiaokangQianb67384d2022-04-19 00:02:38 +0000410 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000411 const unsigned char *extensions_end;
412
XiaokangQianed582dd2022-04-13 08:21:05 +0000413 const int* cipher_suites;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000414 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000415 int hrr_required = 0;
416
417 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
418
419 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000420 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000421 * 0 . 1 protocol version
422 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
XiaokangQianc5763b52022-04-02 03:34:37 +0000423 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000424 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000425 * .. . .. ciphersuite list length ( 2 bytes )
426 * .. . .. ciphersuite list
427 * .. . .. compression alg. list length ( 1 byte )
428 * .. . .. compression alg. list
429 * .. . .. extensions length ( 2 bytes, optional )
430 * .. . .. extensions ( optional )
431 */
432
XiaokangQianb67384d2022-04-19 00:02:38 +0000433 /*
434 * Needs to be updated due to mandatory extensions
XiaokangQian7807f9f2022-02-15 10:04:37 +0000435 * Minimal length ( with everything empty and extensions ommitted ) is
436 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
437 * read at least up to session id length without worrying.
438 */
439 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
440
441 /* ...
442 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
443 * ...
444 * with ProtocolVersion defined as:
445 * uint16 ProtocolVersion;
446 */
447 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
448 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
449 {
450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
451 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
452 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000453 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000454 }
455 p += 2;
456
457 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000458 * Only support TLS 1.3 currently, temporarily set the version.
459 */
460 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
461 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4;
462
463 /*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000464 * Save client random
XiaokangQianb67384d2022-04-19 00:02:38 +0000465 *
466 * ---
467 * Random random;
468 * ---
469 * with Random defined as:
470 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000471 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000472 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
473 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000474
XiaokangQian4080a7f2022-04-11 09:55:18 +0000475 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000476 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000477
XiaokangQianb67384d2022-04-19 00:02:38 +0000478 /* ---
479 * opaque legacy_session_id<0..32>;
480 * ---
XiaokangQian7807f9f2022-02-15 10:04:37 +0000481 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000482 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000483 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000484
XiaokangQianb67384d2022-04-19 00:02:38 +0000485 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000486 {
487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
488 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
489 }
490
XiaokangQian4080a7f2022-04-11 09:55:18 +0000491 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000492 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
493 buf, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +0000494 /*
495 * Check we have enough data for the legacy session identifier
496 * and the ciphersuite list length.
497 */
498 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000499
XiaokangQianed582dd2022-04-13 08:21:05 +0000500 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000501 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000502
XiaokangQian7807f9f2022-02-15 10:04:37 +0000503 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
504 p += 2;
505
XiaokangQianb67384d2022-04-19 00:02:38 +0000506 /* Check we have enough data for the ciphersuite list, the legacy
507 * compression methods and the length of the extensions.
508 */
509 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000510
511 /* store pointer to ciphersuite list */
XiaokangQianed582dd2022-04-13 08:21:05 +0000512 cipher_suites_start = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000513
514 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
515 p, cipher_suites_len );
516
XiaokangQianed582dd2022-04-13 08:21:05 +0000517 /* skip cipher_suites for now */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000518 p += cipher_suites_len;
519
XiaokangQian4080a7f2022-04-11 09:55:18 +0000520 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000521 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000522 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000523 */
XiaokangQianb67384d2022-04-19 00:02:38 +0000524 if( p[0] != 1 || p[1] != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000525 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
527 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
528 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
529 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000530 }
XiaokangQianb67384d2022-04-19 00:02:38 +0000531 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000532
XiaokangQianb67384d2022-04-19 00:02:38 +0000533 /* ---
534 * Extension extensions<8..2^16-1>;
535 * ---
536 * with Extension defined as:
537 * struct {
538 * ExtensionType extension_type;
539 * opaque extension_data<0..2^16-1>;
540 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000541 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000542 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000543 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000544 extensions_end = p + extensions_len;
545 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000546
XiaokangQian4080a7f2022-04-11 09:55:18 +0000547 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000548
549 while( p < extensions_end )
550 {
551 unsigned int extension_type;
552 size_t extension_data_len;
553 const unsigned char *extension_data_end;
554
555 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
556 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
557 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
558 p += 4;
559
560 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
561 extension_data_end = p + extension_data_len;
562
563 switch( extension_type )
564 {
XiaokangQianb67384d2022-04-19 00:02:38 +0000565#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000566 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
568
569 /* Supported Groups Extension
570 *
571 * When sent by the client, the "supported_groups" extension
572 * indicates the named groups which the client supports,
573 * ordered from most preferred to least preferred.
574 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000575 ret = ssl_tls13_parse_supported_groups_ext( ssl, p,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000576 extension_data_end );
577 if( ret != 0 )
578 {
579 MBEDTLS_SSL_DEBUG_RET( 1,
580 "mbedtls_ssl_parse_supported_groups_ext", ret );
581 return( ret );
582 }
583
584 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
585 break;
XiaokangQianb67384d2022-04-19 00:02:38 +0000586#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000587
XiaokangQian88408882022-04-02 10:15:03 +0000588#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000589 case MBEDTLS_TLS_EXT_KEY_SHARE:
590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
591
592 /*
593 * Key Share Extension
594 *
595 * When sent by the client, the "key_share" extension
596 * contains the endpoint's cryptographic parameters for
597 * ECDHE/DHE key establishment methods.
598 */
599 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
600 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
601 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
603 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000604 }
605
606 if( ret != 0 )
607 return( ret );
608
609 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
610 break;
XiaokangQian88408882022-04-02 10:15:03 +0000611#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000612
613 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
615
616 ret = ssl_tls13_parse_supported_versions_ext(
XiaokangQianb67384d2022-04-19 00:02:38 +0000617 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000618 if( ret != 0 )
619 {
620 MBEDTLS_SSL_DEBUG_RET( 1,
621 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
622 return( ret );
623 }
624 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
625 break;
626
627#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
628 case MBEDTLS_TLS_EXT_SIG_ALG:
629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
630
631 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
632 extension_data_end );
633 if( ret != 0 )
634 {
635 MBEDTLS_SSL_DEBUG_MSG( 1,
636 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
637 ret ) );
638 return( ret );
639 }
640 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
641 break;
642#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
643
644 default:
645 MBEDTLS_SSL_DEBUG_MSG( 3,
646 ( "unknown extension found: %ud ( ignoring )",
647 extension_type ) );
648 }
649
650 p += extension_data_len;
651 }
652
653 /* Update checksum with either
654 * - The entire content of the CH message, if no PSK extension is present
655 * - The content up to but excluding the PSK extension, if present.
656 */
XiaokangQian3f84d5d2022-04-19 06:36:17 +0000657 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
XiaokangQianc4b8c992022-04-07 11:31:38 +0000658 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000659 /*
660 * Search for a matching ciphersuite
661 */
XiaokangQianb67384d2022-04-19 00:02:38 +0000662 size_t i, j;
XiaokangQianed582dd2022-04-13 08:21:05 +0000663 cipher_suites = ssl->conf->ciphersuite_list;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000664 ciphersuite_info = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +0000665 for ( j = 0, p = cipher_suites_start; j < cipher_suites_len; j += 2, p += 2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000666 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000667 for ( i = 0; cipher_suites[i] != 0; i++ )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000668 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000669 if( MBEDTLS_GET_UINT16_BE(p, 0) != cipher_suites[i] )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000670 continue;
671
672 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
XiaokangQianed582dd2022-04-13 08:21:05 +0000673 cipher_suites[i] );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000674
675 if( ciphersuite_info == NULL )
676 {
677 MBEDTLS_SSL_DEBUG_MSG(
678 1,
679 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
680 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
681 }
682
683 goto have_ciphersuite;
684
685 }
686 }
687
688 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
689
690have_ciphersuite:
691
692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
693 ciphersuite_info->name ) );
694
XiaokangQianed582dd2022-04-13 08:21:05 +0000695 ssl->session_negotiate->ciphersuite = cipher_suites[i];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000696 ssl->handshake->ciphersuite_info = ciphersuite_info;
697
698 /* List all the extensions we have received */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000699#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000700 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000701#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000702
703 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000704 * Here we only support the ephemeral or (EC)DHE key echange mode
XiaokangQian7807f9f2022-02-15 10:04:37 +0000705 */
706
XiaokangQianb67384d2022-04-19 00:02:38 +0000707 if( !ssl_tls13_check_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000708 {
709 MBEDTLS_SSL_DEBUG_MSG(
710 1,
711 ( "ClientHello message misses mandatory extensions." ) );
712 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
713 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
714 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
715 }
716
XiaokangQian7807f9f2022-02-15 10:04:37 +0000717 if( hrr_required == 1 )
718 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
719
720 return( 0 );
721}
722
XiaokangQianed582dd2022-04-13 08:21:05 +0000723/* Update the handshake state machine */
724
XiaokangQiancfd925f2022-04-14 07:10:37 +0000725static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000726{
727 int ret = 0;
728
XiaokangQian7807f9f2022-02-15 10:04:37 +0000729 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
730 if( ret != 0 )
731 {
732 MBEDTLS_SSL_DEBUG_RET( 1,
733 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
734 return( ret );
735 }
736
737 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
738 return( 0 );
739
740}
741
742/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000743 * Main entry point from the state machine; orchestrates the otherfunctions.
744 */
745
746static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
747{
748
749 int ret = 0;
XiaokangQianed582dd2022-04-13 08:21:05 +0000750 unsigned char* buf = NULL;
751 size_t buflen = 0;
752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
753
754 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
755 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
756 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
757 &buf, &buflen ) );
758
759 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
760 buf + buflen ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
XiaokangQiancfd925f2022-04-14 07:10:37 +0000762 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000763
764cleanup:
765
766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
767 return( ret );
768}
769
770/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000771 * TLS and DTLS 1.3 State Maschine -- server side
772 */
Jerry Yu27561932021-08-27 17:07:38 +0800773int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800774{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775 int ret = 0;
776
777 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
779
Jerry Yue3b34122021-09-28 17:53:35 +0800780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
781 mbedtls_ssl_states_str( ssl->state ),
782 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800783
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 switch( ssl->state )
785 {
786 /* start state */
787 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
789
790 break;
791
792 /* ----- READ CLIENT HELLO ----*/
793
794 case MBEDTLS_SSL_CLIENT_HELLO:
795
XiaokangQian4080a7f2022-04-11 09:55:18 +0000796 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000797 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +0000798 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799
800 break;
801
XiaokangQian3f84d5d2022-04-19 06:36:17 +0000802 case MBEDTLS_SSL_SERVER_HELLO:
803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSL - The requested feature is not available" ) );
804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
805
806 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807 default:
808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
809 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
810 }
811
812 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800813}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800814
Jerry Yufb4b6472022-01-27 15:03:26 +0800815#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */