blob: 5aed7ffb713e24ed56a467a89730dfe27801e456 [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 );
67
68 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
69 if( major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
70 minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
71 {
72 tls13_supported = 1;
73 break;
74 }
75
76 p += 2;
77 }
78
79 if( tls13_supported == 0 )
80 {
XiaokangQian7807f9f2022-02-15 10:04:37 +000081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
82
83 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
84 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
85 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
86 }
87
88 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%d:%d]",
89 major_ver, minor_ver ) );
90
91 ssl->major_ver = major_ver;
92 ssl->minor_ver = minor_ver;
XiaokangQian7807f9f2022-02-15 10:04:37 +000093 return( 0 );
94}
95
XiaokangQian8f9dfe42022-04-15 02:52:39 +000096#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +000097/* This function parses the TLS 1.3 supported_groups extension and
98 * stores the received groups in ssl->handshake->curves.
99 *
100 * From RFC 8446:
101 * enum {
102 * ... (0xFFFF)
103 * } NamedGroup;
104 * struct {
105 * NamedGroup named_group_list<2..2^16-1>;
106 * } NamedGroupList;
107 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000108static int ssl_tls13_parse_supported_groups_ext(
XiaokangQian7807f9f2022-02-15 10:04:37 +0000109 mbedtls_ssl_context *ssl,
110 const unsigned char *buf, const unsigned char *end )
111{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000112 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000113 size_t named_group_list_len, curve_list_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000114 const mbedtls_ecp_curve_info *curve_info, **curves;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000115 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000116
117 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000118 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000119 named_group_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000120 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000121 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, named_group_list_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000122
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000123 /* At the moment, this can happen when receiving a second
124 * ClientHello after an HRR. We should properly reset the
125 * state upon receiving an HRR, in which case we should
126 * not observe handshake->curves already being allocated. */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000127 if( ssl->handshake->curves != NULL )
128 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000129 mbedtls_free( ssl->handshake->curves );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000130 ssl->handshake->curves = NULL;
131 }
132
133 /* Don't allow our peer to make us allocate too much memory,
XiaokangQianc5763b52022-04-02 03:34:37 +0000134 * and leave room for a final 0
135 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000136 curve_list_len = named_group_list_len / 2 + 1;
137 if( curve_list_len > MBEDTLS_ECP_DP_MAX )
138 curve_list_len = MBEDTLS_ECP_DP_MAX;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000139
XiaokangQian4080a7f2022-04-11 09:55:18 +0000140 if( ( curves = mbedtls_calloc( curve_list_len, sizeof( *curves ) ) ) == NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000141 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
142
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000143 named_group_list_end = p + named_group_list_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000144 ssl->handshake->curves = curves;
145
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000146 while ( p < named_group_list_end && curve_list_len > 1 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000147 {
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000148 uint16_t tls_grp_id;
149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, named_group_list_end, 2 );
150 tls_grp_id = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000151 curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_grp_id );
152
153 /* mbedtls_ecp_curve_info_from_tls_id() uses the mbedtls_ecp_curve_info
154 * data structure (defined in ecp.c), which only includes the list of
155 * curves implemented. Hence, we only add curves that are also supported
XiaokangQianc5763b52022-04-02 03:34:37 +0000156 * and implemented by the server.
157 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000158 if( curve_info != NULL )
159 {
160 *curves++ = curve_info;
161 MBEDTLS_SSL_DEBUG_MSG( 4, ( "supported curve: %s", curve_info->name ) );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000162 curve_list_len--;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000163 }
164
165 p += 2;
166 }
167
168 return( 0 );
169
170}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000171#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000172
XiaokangQian88408882022-04-02 10:15:03 +0000173#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000174/*
175 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
176 * extension is correct and stores the provided key shares. Whether this is an
177 * acceptable key share depends on the selected ciphersuite.
178 *
179 * Possible return values are:
180 * - 0: Successful processing of the client provided key share extension.
181 * - MBEDTLS_ERR_SSL_HRR_REQUIRED: The key share provided by the client
182 * does not match a group supported by the server. A HelloRetryRequest will
183 * be needed.
184 * - Another negative return value for fatal errors.
185*/
186
187static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
188 const unsigned char *buf,
189 const unsigned char *end )
190{
191 int ret = 0;
192 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000193 unsigned char const *client_shares_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000194
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000195 size_t client_shares_len, key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000196 int match_found = 0;
197
198 /* From RFC 8446:
199 *
200 * struct {
201 * KeyShareEntry client_shares<0..2^16-1>;
202 * } KeyShareClientHello;
203 *
204 */
205
XiaokangQian7807f9f2022-02-15 10:04:37 +0000206 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000207 client_shares_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000208 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000209 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, client_shares_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000210
211 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000212 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000213
214 /* We try to find a suitable key share entry and copy it to the
215 * handshake context. Later, we have to find out whether we can do
216 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000217 * dismiss it and send a HelloRetryRequest message.
218 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000219
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000220 for( ; p < client_shares_end; p += key_exchange_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000221 {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000222 uint16_t group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000223
224 /*
225 * struct {
226 * NamedGroup group;
227 * opaque key_exchange<1..2^16-1>;
228 * } KeyShareEntry;
229 */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000230 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, 4 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000231 group = MBEDTLS_GET_UINT16_BE( p, 0 );
232 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000233 key_exchange_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000234 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000235
236 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000237 * for input validation purposes.
238 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000239 if( match_found == 1 )
240 continue;
241
242 /*
243 * NamedGroup matching
244 *
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000245 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000246
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000247 * ECDHE shares
XiaokangQian7807f9f2022-02-15 10:04:37 +0000248 *
249 * - Check if we recognize the group
250 * - Check if it's supported
251 */
252
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000253 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
254 {
255 const mbedtls_ecp_curve_info *curve_info =
256 mbedtls_ecp_curve_info_from_tls_id( group );
257 if( curve_info == NULL )
258 {
259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000260 continue;
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000261 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000262
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000263 match_found = 1;
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000265 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
266 if( ret != 0 )
267 return( ret );
268 }
269 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000270 {
271 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000272 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000273 continue;
274 }
275
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000276 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000277 }
278
279 if( match_found == 0 )
280 {
281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
282 return( MBEDTLS_ERR_SSL_HRR_REQUIRED );
283 }
284 return( 0 );
285}
XiaokangQian88408882022-04-02 10:15:03 +0000286#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000287
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000288#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000289static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000290{
XiaokangQian3207a322022-02-23 03:15:27 +0000291 ((void) ssl);
292
XiaokangQian7807f9f2022-02-15 10:04:37 +0000293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- KEY_SHARE_EXTENSION ( %s )",
295 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ?
296 "TRUE" : "FALSE" ) );
297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
298 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
299 "TRUE" : "FALSE" ) );
300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
301 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ?
302 "TRUE" : "FALSE" ) );
303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
304 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ?
305 "TRUE" : "FALSE" ) );
306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
307 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
308 "TRUE" : "FALSE" ) );
309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
310 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
311 "TRUE" : "FALSE" ) );
312#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
313 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SERVERNAME_EXTENSION ( %s )",
314 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
315 "TRUE" : "FALSE" ) );
316#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000317}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000318#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000319
XiaokangQian4080a7f2022-04-11 09:55:18 +0000320static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000321 int exts_mask )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000322{
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000323 int masked = ssl->handshake->extensions_present & exts_mask;
324 return( masked == exts_mask );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000325}
326
XiaokangQian4080a7f2022-04-11 09:55:18 +0000327static int ssl_tls13_client_hello_has_cert_extensions( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000328{
XiaokangQian4080a7f2022-04-11 09:55:18 +0000329 return( ssl_tls13_client_hello_has_exts( ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000330 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
331 MBEDTLS_SSL_EXT_KEY_SHARE |
332 MBEDTLS_SSL_EXT_SIG_ALG ) );
333}
334
XiaokangQian4080a7f2022-04-11 09:55:18 +0000335static int ssl_tls13_check_certificate_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000336{
337 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
338 return( 0 );
339
XiaokangQian4080a7f2022-04-11 09:55:18 +0000340 if( !ssl_tls13_client_hello_has_cert_extensions( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000341 return( 0 );
342
343 ssl->handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
344 return( 1 );
345}
346
XiaokangQian4080a7f2022-04-11 09:55:18 +0000347/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000348 *
349 * STATE HANDLING: ClientHello
350 *
351 * There are three possible classes of outcomes when parsing the CH:
352 *
353 * 1) The CH was well-formed and matched the server's configuration.
354 *
355 * In this case, the server progresses to sending its ServerHello.
356 *
357 * 2) The CH was well-formed but didn't match the server's configuration.
358 *
359 * For example, the client might not have offered a key share which
360 * the server supports, or the server might require a cookie.
361 *
362 * In this case, the server sends a HelloRetryRequest.
363 *
364 * 3) The CH was ill-formed
365 *
366 * In this case, we abort the handshake.
367 *
368 */
369
370/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000371 * Structure of this message:
372 *
373 * uint16 ProtocolVersion;
374 * opaque Random[32];
375 *
376 * uint8 CipherSuite[2]; // Cryptographic suite selector
377 *
378 * struct {
379 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
380 * Random random;
381 * opaque legacy_session_id<0..32>;
382 * CipherSuite cipher_suites<2..2^16-2>;
383 * opaque legacy_compression_methods<1..2^8-1>;
384 * Extension extensions<8..2^16-1>;
385 * } ClientHello;
386 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000387
388#define SSL_CLIENT_HELLO_OK 0
389#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
390
XiaokangQian4080a7f2022-04-11 09:55:18 +0000391static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
392 const unsigned char *buf,
393 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000394{
395 int ret;
396 size_t i, j;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000397 size_t legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000398 size_t cipher_suites_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000399 size_t extensions_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000400 const unsigned char *cipher_suites_start;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000401 const unsigned char *p = buf;
402 const unsigned char *extensions_end;
403
XiaokangQianed582dd2022-04-13 08:21:05 +0000404 const int* cipher_suites;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000405 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
406
407 int hrr_required = 0;
408
409 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
410
411 /*
412 * ClientHello layer:
413 * 0 . 1 protocol version
414 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
XiaokangQianc5763b52022-04-02 03:34:37 +0000415 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000416 * 35 . 34+x session id
417 * 35+x . 35+x DTLS only: cookie length ( 1 byte )
418 * 36+x . .. DTLS only: cookie
419 * .. . .. ciphersuite list length ( 2 bytes )
420 * .. . .. ciphersuite list
421 * .. . .. compression alg. list length ( 1 byte )
422 * .. . .. compression alg. list
423 * .. . .. extensions length ( 2 bytes, optional )
424 * .. . .. extensions ( optional )
425 */
426
XiaokangQianc5763b52022-04-02 03:34:37 +0000427 /* Needs to be updated due to mandatory extensions
XiaokangQian7807f9f2022-02-15 10:04:37 +0000428 * Minimal length ( with everything empty and extensions ommitted ) is
429 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
430 * read at least up to session id length without worrying.
431 */
432 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
433
434 /* ...
435 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
436 * ...
437 * with ProtocolVersion defined as:
438 * uint16 ProtocolVersion;
439 */
440 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
441 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
442 {
443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
444 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
445 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
446 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
447 return ret;
448 }
449 p += 2;
450
451 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000452 * Only support TLS 1.3 currently, temporarily set the version.
453 */
454 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
455 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4;
456
457 /*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000458 * Save client random
459 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000460 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
461 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000462
XiaokangQian4080a7f2022-04-11 09:55:18 +0000463 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000464 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000465
466 /*
467 * Parse session ID
468 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000469 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000470 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000471
XiaokangQian4080a7f2022-04-11 09:55:18 +0000472 if( legacy_session_id_len > 32 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000473 {
474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
475 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
476 }
477
XiaokangQian4080a7f2022-04-11 09:55:18 +0000478 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000479
480 /* Note that this field is echoed even if
481 * the client's value corresponded to a cached pre-TLS 1.3 session
482 * which the server has chosen not to resume. A client which
483 * receives a legacy_session_id_echo field that does not match what
484 * it sent in the ClientHello MUST abort the handshake with an
485 * "illegal_parameter" alert.
486 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000487 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
488 buf, legacy_session_id_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000489
XiaokangQianed582dd2022-04-13 08:21:05 +0000490 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000491 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000492
493 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
494 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
495 p += 2;
496
497 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
498
499 /* store pointer to ciphersuite list */
XiaokangQianed582dd2022-04-13 08:21:05 +0000500 cipher_suites_start = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000501
502 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
503 p, cipher_suites_len );
504
XiaokangQianed582dd2022-04-13 08:21:05 +0000505 /* skip cipher_suites for now */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000506 p += cipher_suites_len;
507
XiaokangQian4080a7f2022-04-11 09:55:18 +0000508 /* ...
509 * uint8 legacy_compression_method = 0;
510 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000511 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000512 p += 1;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000513 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
514 if( p[0] != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000515 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
517 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
518 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
519 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000520 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000521 p++;
522
523 /*
XiaokangQianed582dd2022-04-13 08:21:05 +0000524 * Check the extensions length
XiaokangQian7807f9f2022-02-15 10:04:37 +0000525 */
526 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000527 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000528 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000529 extensions_end = p + extensions_len;
530 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000531
XiaokangQian4080a7f2022-04-11 09:55:18 +0000532 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000533
534 while( p < extensions_end )
535 {
536 unsigned int extension_type;
537 size_t extension_data_len;
538 const unsigned char *extension_data_end;
539
540 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
541 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
542 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
543 p += 4;
544
545 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
546 extension_data_end = p + extension_data_len;
547
548 switch( extension_type )
549 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000550#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
551 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
553
554 /* Supported Groups Extension
555 *
556 * When sent by the client, the "supported_groups" extension
557 * indicates the named groups which the client supports,
558 * ordered from most preferred to least preferred.
559 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000560 ret = ssl_tls13_parse_supported_groups_ext( ssl, p,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000561 extension_data_end );
562 if( ret != 0 )
563 {
564 MBEDTLS_SSL_DEBUG_RET( 1,
565 "mbedtls_ssl_parse_supported_groups_ext", ret );
566 return( ret );
567 }
568
569 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
570 break;
571#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
572
XiaokangQian88408882022-04-02 10:15:03 +0000573#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000574 case MBEDTLS_TLS_EXT_KEY_SHARE:
575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
576
577 /*
578 * Key Share Extension
579 *
580 * When sent by the client, the "key_share" extension
581 * contains the endpoint's cryptographic parameters for
582 * ECDHE/DHE key establishment methods.
583 */
584 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
585 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
586 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
588 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000589 }
590
591 if( ret != 0 )
592 return( ret );
593
594 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
595 break;
XiaokangQian88408882022-04-02 10:15:03 +0000596#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000597
598 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
600
601 ret = ssl_tls13_parse_supported_versions_ext(
602 ssl, p, extension_data_end );
603 if( ret != 0 )
604 {
605 MBEDTLS_SSL_DEBUG_RET( 1,
606 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
607 return( ret );
608 }
609 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
610 break;
611
612#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
613 case MBEDTLS_TLS_EXT_SIG_ALG:
614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
615
616 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
617 extension_data_end );
618 if( ret != 0 )
619 {
620 MBEDTLS_SSL_DEBUG_MSG( 1,
621 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
622 ret ) );
623 return( ret );
624 }
625 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
626 break;
627#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
628
629 default:
630 MBEDTLS_SSL_DEBUG_MSG( 3,
631 ( "unknown extension found: %ud ( ignoring )",
632 extension_type ) );
633 }
634
635 p += extension_data_len;
636 }
637
638 /* Update checksum with either
639 * - The entire content of the CH message, if no PSK extension is present
640 * - The content up to but excluding the PSK extension, if present.
641 */
XiaokangQianc4b8c992022-04-07 11:31:38 +0000642 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
643 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000644 /*
645 * Search for a matching ciphersuite
646 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000647 cipher_suites = ssl->conf->ciphersuite_list;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000648 ciphersuite_info = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +0000649 for ( j = 0, p = cipher_suites_start; j < cipher_suites_len; j += 2, p += 2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000650 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000651 for ( i = 0; cipher_suites[i] != 0; i++ )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000652 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000653 if( MBEDTLS_GET_UINT16_BE(p, 0) != cipher_suites[i] )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000654 continue;
655
656 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
XiaokangQianed582dd2022-04-13 08:21:05 +0000657 cipher_suites[i] );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000658
659 if( ciphersuite_info == NULL )
660 {
661 MBEDTLS_SSL_DEBUG_MSG(
662 1,
663 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
664 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
665 }
666
667 goto have_ciphersuite;
668
669 }
670 }
671
672 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
673
674have_ciphersuite:
675
676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
677 ciphersuite_info->name ) );
678
XiaokangQianed582dd2022-04-13 08:21:05 +0000679 ssl->session_negotiate->ciphersuite = cipher_suites[i];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000680 ssl->handshake->ciphersuite_info = ciphersuite_info;
681
682 /* List all the extensions we have received */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000683#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000684 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000685#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000686
687 /*
688 * Determine the key exchange algorithm to use.
689 * There are three types of key exchanges supported in TLS 1.3:
690 * - (EC)DH with ECDSA,
691 * - (EC)DH with PSK,
692 * - plain PSK.
693 *
694 * The PSK-based key exchanges may additionally be used with 0-RTT.
695 *
696 * Our built-in order of preference is
697 * 1 ) Plain PSK Mode
698 * 2 ) (EC)DHE-PSK Mode
699 * 3 ) Certificate Mode
700 */
701
XiaokangQian4080a7f2022-04-11 09:55:18 +0000702 if( !ssl_tls13_check_certificate_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000703 {
704 MBEDTLS_SSL_DEBUG_MSG(
705 1,
706 ( "ClientHello message misses mandatory extensions." ) );
707 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
708 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
709 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
710 }
711
XiaokangQian7807f9f2022-02-15 10:04:37 +0000712 if( hrr_required == 1 )
713 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
714
715 return( 0 );
716}
717
XiaokangQianed582dd2022-04-13 08:21:05 +0000718/* Update the handshake state machine */
719
XiaokangQiancfd925f2022-04-14 07:10:37 +0000720static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000721{
722 int ret = 0;
723
XiaokangQian7807f9f2022-02-15 10:04:37 +0000724 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
725 if( ret != 0 )
726 {
727 MBEDTLS_SSL_DEBUG_RET( 1,
728 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
729 return( ret );
730 }
731
732 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
733 return( 0 );
734
735}
736
737/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000738 * Main entry point from the state machine; orchestrates the otherfunctions.
739 */
740
741static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
742{
743
744 int ret = 0;
XiaokangQianed582dd2022-04-13 08:21:05 +0000745 unsigned char* buf = NULL;
746 size_t buflen = 0;
747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
748
749 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
750 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
751 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
752 &buf, &buflen ) );
753
754 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
755 buf + buflen ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
XiaokangQiancfd925f2022-04-14 07:10:37 +0000757 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000758
759cleanup:
760
761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
762 return( ret );
763}
764
765/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000766 * TLS and DTLS 1.3 State Maschine -- server side
767 */
Jerry Yu27561932021-08-27 17:07:38 +0800768int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800769{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000770 int ret = 0;
771
772 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
774
Jerry Yue3b34122021-09-28 17:53:35 +0800775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
776 mbedtls_ssl_states_str( ssl->state ),
777 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800778
XiaokangQian7807f9f2022-02-15 10:04:37 +0000779 switch( ssl->state )
780 {
781 /* start state */
782 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
784
785 break;
786
787 /* ----- READ CLIENT HELLO ----*/
788
789 case MBEDTLS_SSL_CLIENT_HELLO:
790
XiaokangQian4080a7f2022-04-11 09:55:18 +0000791 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +0000793 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000794
795 break;
796
XiaokangQian7807f9f2022-02-15 10:04:37 +0000797 default:
798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
800 }
801
802 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800803}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800804
Jerry Yufb4b6472022-01-27 15:03:26 +0800805#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */