blob: 5f9b5d016585784b459b42643ba3c40efacae528 [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;
XiaokangQian0a1b54e2022-04-21 03:01:38 +000054 uint16_t tls_version;
XiaokangQian8f9dfe42022-04-15 02:52:39 +000055 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 );
XiaokangQiande333912022-04-20 08:49:42 +000066 tls_version = mbedtls_ssl_read_version( p, ssl->conf->transport );
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. */
XiaokangQiande333912022-04-20 08:49:42 +000070 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
XiaokangQian7807f9f2022-02-15 10:04:37 +000071 {
72 tls13_supported = 1;
73 break;
74 }
XiaokangQian7807f9f2022-02-15 10:04:37 +000075 }
76
XiaokangQianb67384d2022-04-19 00:02:38 +000077 if( !tls13_supported )
XiaokangQian7807f9f2022-02-15 10:04:37 +000078 {
XiaokangQian7807f9f2022-02-15 10:04:37 +000079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
80
81 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
82 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
83 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
84 }
85
XiaokangQiande333912022-04-20 08:49:42 +000086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%04x]",
XiaokangQian0a1b54e2022-04-21 03:01:38 +000087 (unsigned int)tls_version ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +000088
XiaokangQian7807f9f2022-02-15 10:04:37 +000089 return( 0 );
90}
91
XiaokangQian8f9dfe42022-04-15 02:52:39 +000092#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +000093/* This function parses the TLS 1.3 supported_groups extension and
94 * stores the received groups in ssl->handshake->curves.
95 *
96 * From RFC 8446:
97 * enum {
98 * ... (0xFFFF)
99 * } NamedGroup;
100 * struct {
101 * NamedGroup named_group_list<2..2^16-1>;
102 * } NamedGroupList;
103 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000104static int ssl_tls13_parse_supported_groups_ext(
XiaokangQian7807f9f2022-02-15 10:04:37 +0000105 mbedtls_ssl_context *ssl,
106 const unsigned char *buf, const unsigned char *end )
107{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000108 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000109 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000110 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000111
112 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000113 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000114 named_group_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000115 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000116 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, named_group_list_len );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000117 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000118 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000119
XiaokangQian08037552022-04-20 07:16:41 +0000120 while( p < named_group_list_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000121 {
XiaokangQian08037552022-04-20 07:16:41 +0000122 uint16_t named_group;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000123 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, named_group_list_end, 2 );
XiaokangQian08037552022-04-20 07:16:41 +0000124 named_group = MBEDTLS_GET_UINT16_BE( p, 0 );
125 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000126
XiaokangQian318dc762022-04-20 09:43:51 +0000127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got named group: %d", named_group ) );
XiaokangQian08037552022-04-20 07:16:41 +0000128
129 if( ! mbedtls_ssl_named_group_is_offered( ssl, named_group ) ||
130 ! mbedtls_ssl_named_group_is_supported( named_group ) ||
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000131 ssl->handshake->hrr_selected_group != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000132 {
XiaokangQian08037552022-04-20 07:16:41 +0000133 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000134 }
135
XiaokangQian08037552022-04-20 07:16:41 +0000136 MBEDTLS_SSL_DEBUG_MSG(
137 2, ( "add named group (%04x) into received list.",
138 named_group ) );
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000139 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000140 }
141
142 return( 0 );
143
144}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000145#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000146
XiaokangQian08037552022-04-20 07:16:41 +0000147#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
148
XiaokangQian88408882022-04-02 10:15:03 +0000149#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000150/*
151 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
152 * extension is correct and stores the provided key shares. Whether this is an
153 * acceptable key share depends on the selected ciphersuite.
154 *
155 * Possible return values are:
156 * - 0: Successful processing of the client provided key share extension.
XiaokangQian08037552022-04-20 07:16:41 +0000157 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by the client
XiaokangQian7807f9f2022-02-15 10:04:37 +0000158 * does not match a group supported by the server. A HelloRetryRequest will
159 * be needed.
160 * - Another negative return value for fatal errors.
161*/
162
163static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
164 const unsigned char *buf,
165 const unsigned char *end )
166{
XiaokangQianb67384d2022-04-19 00:02:38 +0000167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000168 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000169 unsigned char const *client_shares_end;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000170 size_t client_shares_len, key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000171 int match_found = 0;
172
173 /* From RFC 8446:
174 *
175 * struct {
176 * KeyShareEntry client_shares<0..2^16-1>;
177 * } KeyShareClientHello;
178 *
179 */
180
XiaokangQian7807f9f2022-02-15 10:04:37 +0000181 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000182 client_shares_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000183 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000184 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, client_shares_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000185
186 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000187 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000188
189 /* We try to find a suitable key share entry and copy it to the
190 * handshake context. Later, we have to find out whether we can do
191 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000192 * dismiss it and send a HelloRetryRequest message.
193 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000194
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000195 for( ; p < client_shares_end; p += key_exchange_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000196 {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000197 uint16_t group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000198
199 /*
200 * struct {
201 * NamedGroup group;
202 * opaque key_exchange<1..2^16-1>;
203 * } KeyShareEntry;
204 */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000205 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, 4 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000206 group = MBEDTLS_GET_UINT16_BE( p, 0 );
207 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000208 key_exchange_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000209 p += 2;
XiaokangQianb67384d2022-04-19 00:02:38 +0000210 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, key_exchange_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000211
212 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000213 * for input validation purposes.
214 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000215 if( match_found == 1 )
216 continue;
217
XiaokangQian060d8672022-04-21 09:24:56 +0000218 if( ! mbedtls_ssl_named_group_is_offered( ssl, group ) ||
219 ! mbedtls_ssl_named_group_is_supported( group ) )
220 {
221 continue;
222 }
XiaokangQian060d8672022-04-21 09:24:56 +0000223
XiaokangQian7807f9f2022-02-15 10:04:37 +0000224 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000225 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000226 */
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000227 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
228 {
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000229 const mbedtls_ecp_curve_info *curve_info =
230 mbedtls_ecp_curve_info_from_tls_id( group );
231 ((void) curve_info);
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
XiaokangQian318dc762022-04-20 09:43:51 +0000233 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
234 ssl, p - 2, key_exchange_len + 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000235 if( ret != 0 )
236 return( ret );
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000237
238 match_found = 1;
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000239 }
240 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000241 {
242 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000243 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000244 continue;
245 }
246
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000247 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000248 }
249
250 if( match_found == 0 )
251 {
252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
XiaokangQian08037552022-04-20 07:16:41 +0000253 return( SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000254 }
255 return( 0 );
256}
XiaokangQian88408882022-04-02 10:15:03 +0000257#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000258
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000259#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000260static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000261{
XiaokangQian3207a322022-02-23 03:15:27 +0000262 ((void) ssl);
263
XiaokangQian7807f9f2022-02-15 10:04:37 +0000264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
XiaokangQianb67384d2022-04-19 00:02:38 +0000265 MBEDTLS_SSL_DEBUG_MSG( 3,
266 ( "- KEY_SHARE_EXTENSION ( %s )",
267 ( ( ssl->handshake->extensions_present
268 & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ? "TRUE" : "FALSE" ) );
269 MBEDTLS_SSL_DEBUG_MSG( 3,
270 ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
271 ( ( ssl->handshake->extensions_present
272 & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
273 "TRUE" : "FALSE" ) );
274 MBEDTLS_SSL_DEBUG_MSG( 3,
275 ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
276 ( ( ssl->handshake->extensions_present
277 & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ? "TRUE" : "FALSE" ) );
278 MBEDTLS_SSL_DEBUG_MSG( 3,
279 ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
280 ( ( ssl->handshake->extensions_present
281 & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ? "TRUE" : "FALSE" ) );
282 MBEDTLS_SSL_DEBUG_MSG( 3,
283 ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
284 ( ( ssl->handshake->extensions_present
285 & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
286 "TRUE" : "FALSE" ) );
287 MBEDTLS_SSL_DEBUG_MSG( 3,
288 ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
289 ( ( ssl->handshake->extensions_present
290 & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
291 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000292#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
XiaokangQianb67384d2022-04-19 00:02:38 +0000293 MBEDTLS_SSL_DEBUG_MSG( 3,
294 ( "- SERVERNAME_EXTENSION ( %s )",
295 ( ( ssl->handshake->extensions_present
296 & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
297 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000298#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000299}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000300#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000301
XiaokangQian4080a7f2022-04-11 09:55:18 +0000302static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000303 int exts_mask )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000304{
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000305 int masked = ssl->handshake->extensions_present & exts_mask;
306 return( masked == exts_mask );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000307}
308
XiaokangQianb67384d2022-04-19 00:02:38 +0000309static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
310 mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000311{
XiaokangQian4080a7f2022-04-11 09:55:18 +0000312 return( ssl_tls13_client_hello_has_exts( ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000313 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
314 MBEDTLS_SSL_EXT_KEY_SHARE |
315 MBEDTLS_SSL_EXT_SIG_ALG ) );
316}
317
XiaokangQianb67384d2022-04-19 00:02:38 +0000318static int ssl_tls13_check_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000319{
320 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
321 return( 0 );
322
XiaokangQianb67384d2022-04-19 00:02:38 +0000323 if( !ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000324 return( 0 );
325
XiaokangQianb67384d2022-04-19 00:02:38 +0000326 ssl->handshake->tls13_kex_modes =
327 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000328 return( 1 );
329}
330
XiaokangQian4080a7f2022-04-11 09:55:18 +0000331/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000332 *
333 * STATE HANDLING: ClientHello
334 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000335 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000336 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000337 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000338 *
339 * In this case, the server progresses to sending its ServerHello.
340 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000341 * 2) The ClientHello was well-formed but didn't match the server's
342 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000343 *
344 * For example, the client might not have offered a key share which
345 * the server supports, or the server might require a cookie.
346 *
347 * In this case, the server sends a HelloRetryRequest.
348 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000349 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000350 *
351 * In this case, we abort the handshake.
352 *
353 */
354
355/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000356 * Structure of this message:
357 *
358 * uint16 ProtocolVersion;
359 * opaque Random[32];
XiaokangQian4080a7f2022-04-11 09:55:18 +0000360 * uint8 CipherSuite[2]; // Cryptographic suite selector
361 *
362 * struct {
363 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
364 * Random random;
365 * opaque legacy_session_id<0..32>;
366 * CipherSuite cipher_suites<2..2^16-2>;
367 * opaque legacy_compression_methods<1..2^8-1>;
368 * Extension extensions<8..2^16-1>;
369 * } ClientHello;
370 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000371
372#define SSL_CLIENT_HELLO_OK 0
373#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
374
XiaokangQian4080a7f2022-04-11 09:55:18 +0000375static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
376 const unsigned char *buf,
377 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000378{
XiaokangQianb67384d2022-04-19 00:02:38 +0000379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
380 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000381 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000382 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000383 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000384 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000385 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000386 const unsigned char *extensions_end;
387
XiaokangQian7807f9f2022-02-15 10:04:37 +0000388 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000389
390 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
391
392 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000393 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000394 * 0 . 1 protocol version
395 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
XiaokangQianc5763b52022-04-02 03:34:37 +0000396 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000397 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000398 * .. . .. ciphersuite list length ( 2 bytes )
399 * .. . .. ciphersuite list
400 * .. . .. compression alg. list length ( 1 byte )
401 * .. . .. compression alg. list
402 * .. . .. extensions length ( 2 bytes, optional )
403 * .. . .. extensions ( optional )
404 */
405
XiaokangQianb67384d2022-04-19 00:02:38 +0000406 /*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000407 * Minimal length ( with everything empty and extensions ommitted ) is
408 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
409 * read at least up to session id length without worrying.
410 */
411 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
412
413 /* ...
414 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
415 * ...
416 * with ProtocolVersion defined as:
417 * uint16 ProtocolVersion;
418 */
XiaokangQiande333912022-04-20 08:49:42 +0000419 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
420 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000421 {
422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
423 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
424 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000425 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000426 }
427 p += 2;
428
429 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000430 * Only support TLS 1.3 currently, temporarily set the version.
431 */
XiaokangQiande333912022-04-20 08:49:42 +0000432 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +0000433
XiaokangQian08037552022-04-20 07:16:41 +0000434 /* ---
XiaokangQianb67384d2022-04-19 00:02:38 +0000435 * Random random;
436 * ---
437 * with Random defined as:
438 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000439 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000440 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +0000441 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000442
XiaokangQian08037552022-04-20 07:16:41 +0000443 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
444 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000445
XiaokangQianb67384d2022-04-19 00:02:38 +0000446 /* ---
447 * opaque legacy_session_id<0..32>;
448 * ---
XiaokangQian7807f9f2022-02-15 10:04:37 +0000449 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000450 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000451 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000452
XiaokangQianb67384d2022-04-19 00:02:38 +0000453 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000454 {
455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
456 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
457 }
458
XiaokangQian4080a7f2022-04-11 09:55:18 +0000459 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000460 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
461 buf, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +0000462 /*
463 * Check we have enough data for the legacy session identifier
464 * and the ciphersuite list length.
465 */
466 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000467
XiaokangQianed582dd2022-04-13 08:21:05 +0000468 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000469 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000470
XiaokangQian7807f9f2022-02-15 10:04:37 +0000471 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
472 p += 2;
473
XiaokangQianb67384d2022-04-19 00:02:38 +0000474 /* Check we have enough data for the ciphersuite list, the legacy
475 * compression methods and the length of the extensions.
476 */
477 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000478
XiaokangQian08037552022-04-20 07:16:41 +0000479 /* ---
480 * CipherSuite cipher_suites<2..2^16-2>;
481 * ---
482 * with CipherSuite defined as:
483 * uint8 CipherSuite[2];
484 */
XiaokangQian060d8672022-04-21 09:24:56 +0000485 cipher_suites = p;
486 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000487 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
488 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +0000489 /*
490 * Search for a matching ciphersuite
491 */
XiaokangQian318dc762022-04-20 09:43:51 +0000492 int ciphersuite_match = 0;
XiaokangQian17f974c2022-04-19 09:57:41 +0000493 ciphersuite_info = NULL;
XiaokangQian060d8672022-04-21 09:24:56 +0000494 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +0000495 {
XiaokangQian318dc762022-04-20 09:43:51 +0000496 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08037552022-04-20 07:16:41 +0000497 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
498 cipher_suite );
499 /*
500 * Check whether this ciphersuite is valid and offered.
501 */
502 if( ( mbedtls_ssl_validate_ciphersuite(
XiaokangQiande333912022-04-20 08:49:42 +0000503 ssl, ciphersuite_info, ssl->tls_version,
504 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +0000505 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
506 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000507
XiaokangQian08037552022-04-20 07:16:41 +0000508 ssl->session_negotiate->ciphersuite = cipher_suite;
509 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +0000510 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +0000511
XiaokangQian08037552022-04-20 07:16:41 +0000512 break;
XiaokangQian17f974c2022-04-19 09:57:41 +0000513
XiaokangQian17f974c2022-04-19 09:57:41 +0000514 }
515
XiaokangQian318dc762022-04-20 09:43:51 +0000516 if( !ciphersuite_match )
517 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000518 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
519 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
520 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +0000521 }
XiaokangQian17f974c2022-04-19 09:57:41 +0000522
523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
524 ciphersuite_info->name ) );
525
XiaokangQian060d8672022-04-21 09:24:56 +0000526 p = cipher_suites + cipher_suites_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000527 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000528 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000529 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000530 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000531 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000532 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
534 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
535 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
536 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000537 }
XiaokangQianb67384d2022-04-19 00:02:38 +0000538 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000539
XiaokangQianb67384d2022-04-19 00:02:38 +0000540 /* ---
541 * Extension extensions<8..2^16-1>;
542 * ---
543 * with Extension defined as:
544 * struct {
545 * ExtensionType extension_type;
546 * opaque extension_data<0..2^16-1>;
547 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000548 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000549 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000550 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000551 extensions_end = p + extensions_len;
552 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000553
XiaokangQian4080a7f2022-04-11 09:55:18 +0000554 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000555
556 while( p < extensions_end )
557 {
558 unsigned int extension_type;
559 size_t extension_data_len;
560 const unsigned char *extension_data_end;
561
XiaokangQian318dc762022-04-20 09:43:51 +0000562 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000563 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
564 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
565 p += 4;
566
567 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
568 extension_data_end = p + extension_data_len;
569
570 switch( extension_type )
571 {
XiaokangQianb67384d2022-04-19 00:02:38 +0000572#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000573 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
575
576 /* Supported Groups Extension
577 *
578 * When sent by the client, the "supported_groups" extension
579 * indicates the named groups which the client supports,
580 * ordered from most preferred to least preferred.
581 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000582 ret = ssl_tls13_parse_supported_groups_ext( ssl, p,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000583 extension_data_end );
584 if( ret != 0 )
585 {
586 MBEDTLS_SSL_DEBUG_RET( 1,
587 "mbedtls_ssl_parse_supported_groups_ext", ret );
588 return( ret );
589 }
590
591 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
592 break;
XiaokangQianb67384d2022-04-19 00:02:38 +0000593#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000594
XiaokangQian88408882022-04-02 10:15:03 +0000595#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000596 case MBEDTLS_TLS_EXT_KEY_SHARE:
597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
598
599 /*
600 * Key Share Extension
601 *
602 * When sent by the client, the "key_share" extension
603 * contains the endpoint's cryptographic parameters for
604 * ECDHE/DHE key establishment methods.
605 */
606 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +0000607 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000608 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
610 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000611 }
612
613 if( ret != 0 )
614 return( ret );
615
616 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
617 break;
XiaokangQian88408882022-04-02 10:15:03 +0000618#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000619
620 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
622
623 ret = ssl_tls13_parse_supported_versions_ext(
XiaokangQianb67384d2022-04-19 00:02:38 +0000624 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000625 if( ret != 0 )
626 {
627 MBEDTLS_SSL_DEBUG_RET( 1,
628 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
629 return( ret );
630 }
631 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
632 break;
633
634#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
635 case MBEDTLS_TLS_EXT_SIG_ALG:
636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
637
638 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
639 extension_data_end );
640 if( ret != 0 )
641 {
642 MBEDTLS_SSL_DEBUG_MSG( 1,
643 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
644 ret ) );
645 return( ret );
646 }
647 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
648 break;
649#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
650
651 default:
652 MBEDTLS_SSL_DEBUG_MSG( 3,
653 ( "unknown extension found: %ud ( ignoring )",
654 extension_type ) );
655 }
656
657 p += extension_data_len;
658 }
659
660 /* Update checksum with either
661 * - The entire content of the CH message, if no PSK extension is present
662 * - The content up to but excluding the PSK extension, if present.
663 */
XiaokangQian3f84d5d2022-04-19 06:36:17 +0000664 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
XiaokangQianc4b8c992022-04-07 11:31:38 +0000665 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000666
667 /* List all the extensions we have received */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000668#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000669 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000670#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000671
672 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000673 * Here we only support the ephemeral or (EC)DHE key echange mode
XiaokangQian7807f9f2022-02-15 10:04:37 +0000674 */
675
XiaokangQianb67384d2022-04-19 00:02:38 +0000676 if( !ssl_tls13_check_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000677 {
678 MBEDTLS_SSL_DEBUG_MSG(
679 1,
680 ( "ClientHello message misses mandatory extensions." ) );
681 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
682 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
683 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
684 }
685
XiaokangQian7807f9f2022-02-15 10:04:37 +0000686 return( 0 );
687}
688
XiaokangQianed582dd2022-04-13 08:21:05 +0000689/* Update the handshake state machine */
690
XiaokangQiancfd925f2022-04-14 07:10:37 +0000691static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000692{
XiaokangQian08037552022-04-20 07:16:41 +0000693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000694
XiaokangQian7807f9f2022-02-15 10:04:37 +0000695 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
696 if( ret != 0 )
697 {
698 MBEDTLS_SSL_DEBUG_RET( 1,
699 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
700 return( ret );
701 }
702
703 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
704 return( 0 );
705
706}
707
708/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000709 * Main entry point from the state machine; orchestrates the otherfunctions.
710 */
711
712static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
713{
714
XiaokangQian08037552022-04-20 07:16:41 +0000715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianed582dd2022-04-13 08:21:05 +0000716 unsigned char* buf = NULL;
717 size_t buflen = 0;
718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
719
XiaokangQianed582dd2022-04-13 08:21:05 +0000720 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
721 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
722 &buf, &buflen ) );
723
724 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
725 buf + buflen ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
XiaokangQiancfd925f2022-04-14 07:10:37 +0000727 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000728
729cleanup:
730
731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
732 return( ret );
733}
734
735/*
XiaokangQian060d8672022-04-21 09:24:56 +0000736 * TLS 1.3 State Maschine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +0000737 */
Jerry Yu27561932021-08-27 17:07:38 +0800738int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800739{
XiaokangQian08037552022-04-20 07:16:41 +0000740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000741
742 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
744
Jerry Yue3b34122021-09-28 17:53:35 +0800745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
746 mbedtls_ssl_states_str( ssl->state ),
747 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800748
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749 switch( ssl->state )
750 {
751 /* start state */
752 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000753 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
754
XiaokangQian08037552022-04-20 07:16:41 +0000755 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000756 break;
757
XiaokangQian7807f9f2022-02-15 10:04:37 +0000758 case MBEDTLS_SSL_CLIENT_HELLO:
759
XiaokangQian4080a7f2022-04-11 09:55:18 +0000760 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000761 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +0000762 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000763
764 break;
765
XiaokangQian7807f9f2022-02-15 10:04:37 +0000766 default:
767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
XiaokangQian060d8672022-04-21 09:24:56 +0000768 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000769 }
770
771 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800772}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800773
Jerry Yufb4b6472022-01-27 15:03:26 +0800774#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */