blob: 8a2f18ad1dbe7c835185c37aeb57f7f6d3c17f49 [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 {
44 * select (Handshake.msg_type) {
45 * case client_hello:
46 * ProtocolVersion versions<2..254>;
47 * case server_hello: // and HelloRetryRequest
48 * ProtocolVersion selected_version;
49 * };
50 * } SupportedVersions;
51 */
52static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
53 const unsigned char *buf,
54 const unsigned char *end )
55{
XiaokangQian4080a7f2022-04-11 09:55:18 +000056 size_t versions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +000057 int tls13_supported = 0;
58 int major_ver, minor_ver;
59 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +000060 const unsigned char *versions_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +000061
62 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
63
XiaokangQian4080a7f2022-04-11 09:55:18 +000064 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +000065 p += 1;
66
XiaokangQian4080a7f2022-04-11 09:55:18 +000067 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, versions_len );
68 if( versions_len % 2 != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +000069 {
70 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid supported version list length %" MBEDTLS_PRINTF_SIZET,
XiaokangQian4080a7f2022-04-11 09:55:18 +000071 versions_len ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +000072 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
73 }
74
XiaokangQian4080a7f2022-04-11 09:55:18 +000075 versions_end = p + versions_len;
76 while( p < versions_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +000077 {
78 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
79
80 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
81 if( major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
82 minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
83 {
84 tls13_supported = 1;
85 break;
86 }
87
88 p += 2;
89 }
90
91 if( tls13_supported == 0 )
92 {
XiaokangQian4080a7f2022-04-11 09:55:18 +000093 /* Here we only support TLS 1.3, we need report "bad protocol" if it
94 * doesn't support TLS 1.2.
XiaokangQianc5763b52022-04-02 03:34:37 +000095 */
XiaokangQian7807f9f2022-02-15 10:04:37 +000096
97 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
98
99 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
100 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
101 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
102 }
103
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%d:%d]",
105 major_ver, minor_ver ) );
106
107 ssl->major_ver = major_ver;
108 ssl->minor_ver = minor_ver;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000109 return( 0 );
110}
111
112#if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
113/* This function parses the TLS 1.3 supported_groups extension and
114 * stores the received groups in ssl->handshake->curves.
115 *
116 * From RFC 8446:
117 * enum {
118 * ... (0xFFFF)
119 * } NamedGroup;
120 * struct {
121 * NamedGroup named_group_list<2..2^16-1>;
122 * } NamedGroupList;
123 */
124static int mbedtls_ssl_tls13_parse_supported_groups_ext(
125 mbedtls_ssl_context *ssl,
126 const unsigned char *buf, const unsigned char *end )
127{
128
XiaokangQian4080a7f2022-04-11 09:55:18 +0000129 size_t named_group_list_len, curve_list_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000130 const unsigned char *p = buf;
131 const mbedtls_ecp_curve_info *curve_info, **curves;
132 const unsigned char *extentions_end;
133
134 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000135 named_group_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000136 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000137 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, named_group_list_len );
138 if( named_group_list_len % 2 != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000139 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
140
XiaokangQianc5763b52022-04-02 03:34:37 +0000141 /* At the moment, this can happen when receiving a second
XiaokangQian7807f9f2022-02-15 10:04:37 +0000142 * ClientHello after an HRR. We should properly reset the
143 * state upon receiving an HRR, in which case we should
144 * not observe handshake->curves already being allocated. */
145 if( ssl->handshake->curves != NULL )
146 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000147 mbedtls_free( ssl->handshake->curves );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000148 ssl->handshake->curves = NULL;
149 }
150
151 /* Don't allow our peer to make us allocate too much memory,
XiaokangQianc5763b52022-04-02 03:34:37 +0000152 * and leave room for a final 0
153 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000154 curve_list_len = named_group_list_len / 2 + 1;
155 if( curve_list_len > MBEDTLS_ECP_DP_MAX )
156 curve_list_len = MBEDTLS_ECP_DP_MAX;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000157
XiaokangQian4080a7f2022-04-11 09:55:18 +0000158 if( ( curves = mbedtls_calloc( curve_list_len, sizeof( *curves ) ) ) == NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000159 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
160
XiaokangQian4080a7f2022-04-11 09:55:18 +0000161 extentions_end = p + named_group_list_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000162 ssl->handshake->curves = curves;
163
XiaokangQian4080a7f2022-04-11 09:55:18 +0000164 while ( p < extentions_end && curve_list_len > 1 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000165 {
166 uint16_t tls_grp_id = MBEDTLS_GET_UINT16_BE( p, 0 );
167 curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_grp_id );
168
169 /* mbedtls_ecp_curve_info_from_tls_id() uses the mbedtls_ecp_curve_info
170 * data structure (defined in ecp.c), which only includes the list of
171 * curves implemented. Hence, we only add curves that are also supported
XiaokangQianc5763b52022-04-02 03:34:37 +0000172 * and implemented by the server.
173 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000174 if( curve_info != NULL )
175 {
176 *curves++ = curve_info;
177 MBEDTLS_SSL_DEBUG_MSG( 4, ( "supported curve: %s", curve_info->name ) );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000178 curve_list_len--;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000179 }
180
181 p += 2;
182 }
183
184 return( 0 );
185
186}
187#endif /* MBEDTLS_ECDH_C || ( MBEDTLS_ECDSA_C */
188
XiaokangQian88408882022-04-02 10:15:03 +0000189#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000190/*
191 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
192 * extension is correct and stores the provided key shares. Whether this is an
193 * acceptable key share depends on the selected ciphersuite.
194 *
195 * Possible return values are:
196 * - 0: Successful processing of the client provided key share extension.
197 * - MBEDTLS_ERR_SSL_HRR_REQUIRED: The key share provided by the client
198 * does not match a group supported by the server. A HelloRetryRequest will
199 * be needed.
200 * - Another negative return value for fatal errors.
201*/
202
203static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
204 const unsigned char *buf,
205 const unsigned char *end )
206{
207 int ret = 0;
208 unsigned char const *p = buf;
209 unsigned char const *extentions_end;
210
XiaokangQian4080a7f2022-04-11 09:55:18 +0000211 size_t total_extensions_len, key_share_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000212 int match_found = 0;
213
214 /* From RFC 8446:
215 *
216 * struct {
217 * KeyShareEntry client_shares<0..2^16-1>;
218 * } KeyShareClientHello;
219 *
220 */
221
222 /* Read total legnth of KeyShareClientHello */
223 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
224
XiaokangQian4080a7f2022-04-11 09:55:18 +0000225 total_extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000226 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000227 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, total_extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000228
229 ssl->handshake->offered_group_id = 0;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000230 extentions_end = p + total_extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000231
232 /* We try to find a suitable key share entry and copy it to the
233 * handshake context. Later, we have to find out whether we can do
234 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000235 * dismiss it and send a HelloRetryRequest message.
236 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000237
XiaokangQian4080a7f2022-04-11 09:55:18 +0000238 for( ; p < extentions_end; p += key_share_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000239 {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000240 uint16_t group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000241
242 /*
243 * struct {
244 * NamedGroup group;
245 * opaque key_exchange<1..2^16-1>;
246 * } KeyShareEntry;
247 */
248 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extentions_end, 4 );
249
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000250 group = MBEDTLS_GET_UINT16_BE( p, 0 );
251 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000252
XiaokangQian4080a7f2022-04-11 09:55:18 +0000253 key_share_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000254 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000255
256 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000257 * for input validation purposes.
258 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000259 if( match_found == 1 )
260 continue;
261
262 /*
263 * NamedGroup matching
264 *
265 * For now, we only support ECDHE groups, but e.g.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000266
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000267 * Type 1: ECDHE shares
XiaokangQian7807f9f2022-02-15 10:04:37 +0000268 *
269 * - Check if we recognize the group
270 * - Check if it's supported
271 */
272
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000273 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
274 {
275 const mbedtls_ecp_curve_info *curve_info =
276 mbedtls_ecp_curve_info_from_tls_id( group );
277 if( curve_info == NULL )
278 {
279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
280 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
281 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000282
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000283 match_found = 1;
284
285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
286
287 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
288 if( ret != 0 )
289 return( ret );
290 }
291 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000292 {
293 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000294 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000295 continue;
296 }
297
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000298 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000299 }
300
301 if( match_found == 0 )
302 {
303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
304 return( MBEDTLS_ERR_SSL_HRR_REQUIRED );
305 }
306 return( 0 );
307}
XiaokangQian88408882022-04-02 10:15:03 +0000308#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000309
310#if defined(MBEDTLS_SSL_COOKIE_C)
311static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
312 const unsigned char *buf,
313 const unsigned char *end )
314{
315 int ret = 0;
316 size_t cookie_len;
317 unsigned char const *p = buf;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000318
319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse cookie extension" ) );
320
321 if( ssl->conf->f_cookie_check != NULL )
322 {
323 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
324 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
325 p += 2;
326
327 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
328
329 MBEDTLS_SSL_DEBUG_BUF( 3, "Received cookie", p, cookie_len );
330
331 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
332 p, cookie_len, ssl->cli_id,
333 ssl->cli_id_len ) != 0 )
334 {
335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000336 ret = MBEDTLS_ERR_SSL_HRR_REQUIRED;
337 }
338 else
339 {
340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000341 }
342 }
XiaokangQianc5763b52022-04-02 03:34:37 +0000343 else
344 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
346 }
347
348 return( ret );
349}
350#endif /* MBEDTLS_SSL_COOKIE_C */
351
352/*
353 *
354 * STATE HANDLING: ClientHello
355 *
356 * There are three possible classes of outcomes when parsing the CH:
357 *
358 * 1) The CH was well-formed and matched the server's configuration.
359 *
360 * In this case, the server progresses to sending its ServerHello.
361 *
362 * 2) The CH was well-formed but didn't match the server's configuration.
363 *
364 * For example, the client might not have offered a key share which
365 * the server supports, or the server might require a cookie.
366 *
367 * In this case, the server sends a HelloRetryRequest.
368 *
369 * 3) The CH was ill-formed
370 *
371 * In this case, we abort the handshake.
372 *
373 */
374
375/*
376 * Overview
377 */
378
379/* Main entry point from the state machine; orchestrates the otherfunctions. */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000380static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000381
XiaokangQian4080a7f2022-04-11 09:55:18 +0000382static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
383 const unsigned char *buf,
384 const unsigned char *end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000385
386/* Update the handshake state machine */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000387static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context *ssl,
388 int hrr_required );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000389
390/*
391 * Implementation
392 */
393
394#define SSL_CLIENT_HELLO_OK 0
395#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
396
XiaokangQian4080a7f2022-04-11 09:55:18 +0000397static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000398{
399
400 int ret = 0;
401 int hrr_required = SSL_CLIENT_HELLO_OK;
402 unsigned char* buf = NULL;
403 size_t buflen = 0;
404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
405
406 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
407 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
408 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
409 &buf, &buflen ) );
410
XiaokangQian4080a7f2022-04-11 09:55:18 +0000411 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
412 buf + buflen ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000413 hrr_required = ret;
414
415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000416 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl,
417 hrr_required ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000418
419cleanup:
420
421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
422 return( ret );
423}
424
XiaokangQian4080a7f2022-04-11 09:55:18 +0000425static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000426{
XiaokangQian3207a322022-02-23 03:15:27 +0000427 ((void) ssl);
428
XiaokangQian7807f9f2022-02-15 10:04:37 +0000429 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- KEY_SHARE_EXTENSION ( %s )",
431 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ?
432 "TRUE" : "FALSE" ) );
433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
434 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
435 "TRUE" : "FALSE" ) );
436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
437 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ?
438 "TRUE" : "FALSE" ) );
439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
440 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ?
441 "TRUE" : "FALSE" ) );
442 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
443 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
444 "TRUE" : "FALSE" ) );
445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
446 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
447 "TRUE" : "FALSE" ) );
448#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SERVERNAME_EXTENSION ( %s )",
450 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
451 "TRUE" : "FALSE" ) );
452#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
453#if defined ( MBEDTLS_SSL_COOKIE_C )
454 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- COOKIE_EXTENSION ( %s )",
455 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) >0 ) ?
456 "TRUE" : "FALSE" ) );
457#endif /* MBEDTLS_SSL_COOKIE_C */
458}
459
XiaokangQian4080a7f2022-04-11 09:55:18 +0000460static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000461 int ext_id_mask )
462{
463 int masked = ssl->handshake->extensions_present & ext_id_mask;
464 return( masked == ext_id_mask );
465}
466
XiaokangQian4080a7f2022-04-11 09:55:18 +0000467static int ssl_tls13_client_hello_has_cert_extensions( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000468{
XiaokangQian4080a7f2022-04-11 09:55:18 +0000469 return( ssl_tls13_client_hello_has_exts( ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000470 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
471 MBEDTLS_SSL_EXT_KEY_SHARE |
472 MBEDTLS_SSL_EXT_SIG_ALG ) );
473}
474
XiaokangQian4080a7f2022-04-11 09:55:18 +0000475static int ssl_tls13_check_certificate_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000476{
477 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
478 return( 0 );
479
XiaokangQian4080a7f2022-04-11 09:55:18 +0000480 if( !ssl_tls13_client_hello_has_cert_extensions( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000481 return( 0 );
482
483 ssl->handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
484 return( 1 );
485}
486
XiaokangQian4080a7f2022-04-11 09:55:18 +0000487/*
488 * Structure of this message:
489 *
490 * uint16 ProtocolVersion;
491 * opaque Random[32];
492 *
493 * uint8 CipherSuite[2]; // Cryptographic suite selector
494 *
495 * struct {
496 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
497 * Random random;
498 * opaque legacy_session_id<0..32>;
499 * CipherSuite cipher_suites<2..2^16-2>;
500 * opaque legacy_compression_methods<1..2^8-1>;
501 * Extension extensions<8..2^16-1>;
502 * } ClientHello;
503 */
504static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
505 const unsigned char *buf,
506 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000507{
508 int ret;
509 size_t i, j;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000510 size_t legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000511 size_t cipher_suites_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000512 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000513 const unsigned char *ciph_offset;
514 const unsigned char *p = buf;
515 const unsigned char *extensions_end;
516
517 const int* ciphersuites;
518 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
519
520 int hrr_required = 0;
521
522 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
523
524 /*
525 * ClientHello layer:
526 * 0 . 1 protocol version
527 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
XiaokangQianc5763b52022-04-02 03:34:37 +0000528 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000529 * 35 . 34+x session id
530 * 35+x . 35+x DTLS only: cookie length ( 1 byte )
531 * 36+x . .. DTLS only: cookie
532 * .. . .. ciphersuite list length ( 2 bytes )
533 * .. . .. ciphersuite list
534 * .. . .. compression alg. list length ( 1 byte )
535 * .. . .. compression alg. list
536 * .. . .. extensions length ( 2 bytes, optional )
537 * .. . .. extensions ( optional )
538 */
539
XiaokangQianc5763b52022-04-02 03:34:37 +0000540 /* Needs to be updated due to mandatory extensions
XiaokangQian7807f9f2022-02-15 10:04:37 +0000541 * Minimal length ( with everything empty and extensions ommitted ) is
542 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
543 * read at least up to session id length without worrying.
544 */
545 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
546
547 /* ...
548 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
549 * ...
550 * with ProtocolVersion defined as:
551 * uint16 ProtocolVersion;
552 */
553 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
554 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
555 {
556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
557 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
558 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
559 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
560 return ret;
561 }
562 p += 2;
563
564 /*
565 * Save client random
566 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000567 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
568 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000569
XiaokangQian4080a7f2022-04-11 09:55:18 +0000570 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQianc5763b52022-04-02 03:34:37 +0000571 /* skip random bytes */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000572 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000573
574 /*
575 * Parse session ID
576 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000577 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000578 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000579
XiaokangQian4080a7f2022-04-11 09:55:18 +0000580 if( legacy_session_id_len > 32 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000581 {
582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
583 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
584 }
585
XiaokangQian4080a7f2022-04-11 09:55:18 +0000586 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000587
588 /* Note that this field is echoed even if
589 * the client's value corresponded to a cached pre-TLS 1.3 session
590 * which the server has chosen not to resume. A client which
591 * receives a legacy_session_id_echo field that does not match what
592 * it sent in the ClientHello MUST abort the handshake with an
593 * "illegal_parameter" alert.
594 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id length ( %" MBEDTLS_PRINTF_SIZET " )", legacy_session_id_len ) );
596 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf, legacy_session_id_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000597
XiaokangQian4080a7f2022-04-11 09:55:18 +0000598 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len ); /* write session id */
599 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000600
601 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
602 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
603 p += 2;
604
605 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
606
607 /* store pointer to ciphersuite list */
608 ciph_offset = p;
609
610 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
611 p, cipher_suites_len );
612
613 /* skip ciphersuites for now */
614 p += cipher_suites_len;
615
XiaokangQian4080a7f2022-04-11 09:55:18 +0000616 /* ...
617 * uint8 legacy_compression_method = 0;
618 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000619 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000620 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
621 if( p[0] != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000622 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
624 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
625 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
626 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000627 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000628 p++;
629
630 /*
631 * Check the extension length
632 */
633 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
634
XiaokangQian4080a7f2022-04-11 09:55:18 +0000635 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000636 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000637 extensions_end = p + extensions_len;
638 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000639
XiaokangQian4080a7f2022-04-11 09:55:18 +0000640 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000641
642 while( p < extensions_end )
643 {
644 unsigned int extension_type;
645 size_t extension_data_len;
646 const unsigned char *extension_data_end;
647
648 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
649 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
650 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
651 p += 4;
652
653 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
654 extension_data_end = p + extension_data_len;
655
656 switch( extension_type )
657 {
658#if defined(MBEDTLS_SSL_COOKIE_C)
659 case MBEDTLS_TLS_EXT_COOKIE:
660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found cookie extension" ) );
661
662 ret = ssl_tls13_parse_cookie_ext( ssl, p,
663 extension_data_end );
664
665 /* if cookie verification failed then we return a hello retry
666 * message, or return success and set cookie extension present
667 */
668 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
669 {
670 hrr_required = 1;
671 }
672 else if( ret == 0 )
673 {
674 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_COOKIE;
675 }
676 break;
677#endif /* MBEDTLS_SSL_COOKIE_C */
678
679#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
680 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
682
683 /* Supported Groups Extension
684 *
685 * When sent by the client, the "supported_groups" extension
686 * indicates the named groups which the client supports,
687 * ordered from most preferred to least preferred.
688 */
689 ret = mbedtls_ssl_tls13_parse_supported_groups_ext( ssl, p,
690 extension_data_end );
691 if( ret != 0 )
692 {
693 MBEDTLS_SSL_DEBUG_RET( 1,
694 "mbedtls_ssl_parse_supported_groups_ext", ret );
695 return( ret );
696 }
697
698 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
699 break;
700#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
701
XiaokangQian88408882022-04-02 10:15:03 +0000702#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000703 case MBEDTLS_TLS_EXT_KEY_SHARE:
704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
705
706 /*
707 * Key Share Extension
708 *
709 * When sent by the client, the "key_share" extension
710 * contains the endpoint's cryptographic parameters for
711 * ECDHE/DHE key establishment methods.
712 */
713 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
714 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
715 {
716 hrr_required = 1;
717 ret = 0;
718 }
719
720 if( ret != 0 )
721 return( ret );
722
723 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
724 break;
XiaokangQian88408882022-04-02 10:15:03 +0000725#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000726
727 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
729
730 ret = ssl_tls13_parse_supported_versions_ext(
731 ssl, p, extension_data_end );
732 if( ret != 0 )
733 {
734 MBEDTLS_SSL_DEBUG_RET( 1,
735 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
736 return( ret );
737 }
738 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
739 break;
740
741#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
742 case MBEDTLS_TLS_EXT_SIG_ALG:
743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
744
745 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
746 extension_data_end );
747 if( ret != 0 )
748 {
749 MBEDTLS_SSL_DEBUG_MSG( 1,
750 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
751 ret ) );
752 return( ret );
753 }
754 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
755 break;
756#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
757
758 default:
759 MBEDTLS_SSL_DEBUG_MSG( 3,
760 ( "unknown extension found: %ud ( ignoring )",
761 extension_type ) );
762 }
763
764 p += extension_data_len;
765 }
766
767 /* Update checksum with either
768 * - The entire content of the CH message, if no PSK extension is present
769 * - The content up to but excluding the PSK extension, if present.
770 */
XiaokangQianc4b8c992022-04-07 11:31:38 +0000771 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
772 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773 /*
774 * Search for a matching ciphersuite
775 */
776 ciphersuites = ssl->conf->ciphersuite_list;
777 ciphersuite_info = NULL;
778 for ( j = 0, p = ciph_offset; j < cipher_suites_len; j += 2, p += 2 )
779 {
780 for ( i = 0; ciphersuites[i] != 0; i++ )
781 {
782 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
783 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
784 continue;
785
786 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
787 ciphersuites[i] );
788
789 if( ciphersuite_info == NULL )
790 {
791 MBEDTLS_SSL_DEBUG_MSG(
792 1,
793 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
795 }
796
797 goto have_ciphersuite;
798
799 }
800 }
801
802 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
803
804have_ciphersuite:
805
806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
807 ciphersuite_info->name ) );
808
809 ssl->session_negotiate->ciphersuite = ciphersuites[i];
810 ssl->handshake->ciphersuite_info = ciphersuite_info;
811
812 /* List all the extensions we have received */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000813 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000814
815 /*
816 * Determine the key exchange algorithm to use.
817 * There are three types of key exchanges supported in TLS 1.3:
818 * - (EC)DH with ECDSA,
819 * - (EC)DH with PSK,
820 * - plain PSK.
821 *
822 * The PSK-based key exchanges may additionally be used with 0-RTT.
823 *
824 * Our built-in order of preference is
825 * 1 ) Plain PSK Mode
826 * 2 ) (EC)DHE-PSK Mode
827 * 3 ) Certificate Mode
828 */
829
XiaokangQian4080a7f2022-04-11 09:55:18 +0000830 if( !ssl_tls13_check_certificate_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000831 {
832 MBEDTLS_SSL_DEBUG_MSG(
833 1,
834 ( "ClientHello message misses mandatory extensions." ) );
835 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
836 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
837 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
838 }
839
840#if defined(MBEDTLS_SSL_COOKIE_C)
841 /* If we failed to see a cookie extension, and we required it through the
842 * configuration settings ( rr_config ), then we need to send a HRR msg.
843 * Conceptually, this is similiar to having received a cookie that failed
844 * the verification check.
845 */
846 if( ( ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON ) &&
847 !( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) )
848 {
849 MBEDTLS_SSL_DEBUG_MSG(
850 2,
851 ( "Cookie extension missing. Need to send a HRR." ) );
852 hrr_required = 1;
853 }
854#endif /* MBEDTLS_SSL_COOKIE_C */
855
856 if( hrr_required == 1 )
857 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
858
859 return( 0 );
860}
861
XiaokangQian4080a7f2022-04-11 09:55:18 +0000862static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl,
863 int hrr_required )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000864{
865 int ret = 0;
866
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000867 if( ssl->handshake->hello_retry_requests_sent == 0 &&
XiaokangQian7807f9f2022-02-15 10:04:37 +0000868 ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON )
869 {
870 hrr_required = SSL_CLIENT_HELLO_HRR_REQUIRED;
871 }
872
873 if( hrr_required == SSL_CLIENT_HELLO_HRR_REQUIRED )
874 {
875 /*
876 * Create stateless transcript hash for HRR
877 */
878 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
879 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
880 if( ret != 0 )
881 {
882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr",
883 ret );
884 return( ret );
885 }
886 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
887
888 /* Transmit Hello Retry Request */
889 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
890 return( 0 );
891 }
892
893 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
894 if( ret != 0 )
895 {
896 MBEDTLS_SSL_DEBUG_RET( 1,
897 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
898 return( ret );
899 }
900
901 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
902 return( 0 );
903
904}
905
906/*
907 * TLS and DTLS 1.3 State Maschine -- server side
908 */
Jerry Yu27561932021-08-27 17:07:38 +0800909int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800910{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000911 int ret = 0;
912
913 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
915
Jerry Yue3b34122021-09-28 17:53:35 +0800916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
917 mbedtls_ssl_states_str( ssl->state ),
918 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800919
XiaokangQian7807f9f2022-02-15 10:04:37 +0000920 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
921 return( ret );
922
923 switch( ssl->state )
924 {
925 /* start state */
926 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000927 ssl->handshake->hello_retry_requests_sent = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000928 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
929
930 break;
931
932 /* ----- READ CLIENT HELLO ----*/
933
934 case MBEDTLS_SSL_CLIENT_HELLO:
935
XiaokangQian4080a7f2022-04-11 09:55:18 +0000936 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000937 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +0000938 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000939
940 break;
941
942 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
944
945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for all traffic" ) );
946
947 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
948 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
949
950 mbedtls_ssl_tls13_handshake_wrapup( ssl );
951 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET );
952
953 break;
954
955 default:
956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
958 }
959
960 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800961}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800962
Jerry Yufb4b6472022-01-27 15:03:26 +0800963#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */