blob: 9ebab81c41355a1b486cdff45a0d59ed7152c5d7 [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 /*
452 * Save client random
453 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000454 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
455 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000456
XiaokangQian4080a7f2022-04-11 09:55:18 +0000457 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000458 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000459
460 /*
461 * Parse session ID
462 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000463 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000464 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000465
XiaokangQian4080a7f2022-04-11 09:55:18 +0000466 if( legacy_session_id_len > 32 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000467 {
468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
469 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
470 }
471
XiaokangQian4080a7f2022-04-11 09:55:18 +0000472 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000473
474 /* Note that this field is echoed even if
475 * the client's value corresponded to a cached pre-TLS 1.3 session
476 * which the server has chosen not to resume. A client which
477 * receives a legacy_session_id_echo field that does not match what
478 * it sent in the ClientHello MUST abort the handshake with an
479 * "illegal_parameter" alert.
480 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000481 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
482 buf, legacy_session_id_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000483
XiaokangQianed582dd2022-04-13 08:21:05 +0000484 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000485 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000486
487 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
488 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
489 p += 2;
490
491 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
492
493 /* store pointer to ciphersuite list */
XiaokangQianed582dd2022-04-13 08:21:05 +0000494 cipher_suites_start = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000495
496 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
497 p, cipher_suites_len );
498
XiaokangQianed582dd2022-04-13 08:21:05 +0000499 /* skip cipher_suites for now */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000500 p += cipher_suites_len;
501
XiaokangQian4080a7f2022-04-11 09:55:18 +0000502 /* ...
503 * uint8 legacy_compression_method = 0;
504 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000505 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000506 p += 1;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000507 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
508 if( p[0] != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000509 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
511 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
512 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
513 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000514 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000515 p++;
516
517 /*
XiaokangQianed582dd2022-04-13 08:21:05 +0000518 * Check the extensions length
XiaokangQian7807f9f2022-02-15 10:04:37 +0000519 */
520 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000521 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000522 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000523 extensions_end = p + extensions_len;
524 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000525
XiaokangQian4080a7f2022-04-11 09:55:18 +0000526 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000527
528 while( p < extensions_end )
529 {
530 unsigned int extension_type;
531 size_t extension_data_len;
532 const unsigned char *extension_data_end;
533
534 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
535 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
536 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
537 p += 4;
538
539 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
540 extension_data_end = p + extension_data_len;
541
542 switch( extension_type )
543 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000544#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
545 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
547
548 /* Supported Groups Extension
549 *
550 * When sent by the client, the "supported_groups" extension
551 * indicates the named groups which the client supports,
552 * ordered from most preferred to least preferred.
553 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000554 ret = ssl_tls13_parse_supported_groups_ext( ssl, p,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000555 extension_data_end );
556 if( ret != 0 )
557 {
558 MBEDTLS_SSL_DEBUG_RET( 1,
559 "mbedtls_ssl_parse_supported_groups_ext", ret );
560 return( ret );
561 }
562
563 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
564 break;
565#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
566
XiaokangQian88408882022-04-02 10:15:03 +0000567#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000568 case MBEDTLS_TLS_EXT_KEY_SHARE:
569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
570
571 /*
572 * Key Share Extension
573 *
574 * When sent by the client, the "key_share" extension
575 * contains the endpoint's cryptographic parameters for
576 * ECDHE/DHE key establishment methods.
577 */
578 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
579 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
580 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
582 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000583 }
584
585 if( ret != 0 )
586 return( ret );
587
588 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
589 break;
XiaokangQian88408882022-04-02 10:15:03 +0000590#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000591
592 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
594
595 ret = ssl_tls13_parse_supported_versions_ext(
596 ssl, p, extension_data_end );
597 if( ret != 0 )
598 {
599 MBEDTLS_SSL_DEBUG_RET( 1,
600 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
601 return( ret );
602 }
603 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
604 break;
605
606#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
607 case MBEDTLS_TLS_EXT_SIG_ALG:
608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
609
610 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
611 extension_data_end );
612 if( ret != 0 )
613 {
614 MBEDTLS_SSL_DEBUG_MSG( 1,
615 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
616 ret ) );
617 return( ret );
618 }
619 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
620 break;
621#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
622
623 default:
624 MBEDTLS_SSL_DEBUG_MSG( 3,
625 ( "unknown extension found: %ud ( ignoring )",
626 extension_type ) );
627 }
628
629 p += extension_data_len;
630 }
631
632 /* Update checksum with either
633 * - The entire content of the CH message, if no PSK extension is present
634 * - The content up to but excluding the PSK extension, if present.
635 */
XiaokangQianc4b8c992022-04-07 11:31:38 +0000636 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
637 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000638 /*
639 * Search for a matching ciphersuite
640 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000641 cipher_suites = ssl->conf->ciphersuite_list;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000642 ciphersuite_info = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +0000643 for ( j = 0, p = cipher_suites_start; j < cipher_suites_len; j += 2, p += 2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000644 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000645 for ( i = 0; cipher_suites[i] != 0; i++ )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000646 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000647 if( MBEDTLS_GET_UINT16_BE(p, 0) != cipher_suites[i] )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000648 continue;
649
650 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
XiaokangQianed582dd2022-04-13 08:21:05 +0000651 cipher_suites[i] );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000652
653 if( ciphersuite_info == NULL )
654 {
655 MBEDTLS_SSL_DEBUG_MSG(
656 1,
657 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
659 }
660
661 goto have_ciphersuite;
662
663 }
664 }
665
666 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
667
668have_ciphersuite:
669
670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
671 ciphersuite_info->name ) );
672
XiaokangQianed582dd2022-04-13 08:21:05 +0000673 ssl->session_negotiate->ciphersuite = cipher_suites[i];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000674 ssl->handshake->ciphersuite_info = ciphersuite_info;
675
676 /* List all the extensions we have received */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000677#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000678 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000679#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000680
681 /*
682 * Determine the key exchange algorithm to use.
683 * There are three types of key exchanges supported in TLS 1.3:
684 * - (EC)DH with ECDSA,
685 * - (EC)DH with PSK,
686 * - plain PSK.
687 *
688 * The PSK-based key exchanges may additionally be used with 0-RTT.
689 *
690 * Our built-in order of preference is
691 * 1 ) Plain PSK Mode
692 * 2 ) (EC)DHE-PSK Mode
693 * 3 ) Certificate Mode
694 */
695
XiaokangQian4080a7f2022-04-11 09:55:18 +0000696 if( !ssl_tls13_check_certificate_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000697 {
698 MBEDTLS_SSL_DEBUG_MSG(
699 1,
700 ( "ClientHello message misses mandatory extensions." ) );
701 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
702 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
703 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
704 }
705
XiaokangQian7807f9f2022-02-15 10:04:37 +0000706 if( hrr_required == 1 )
707 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
708
709 return( 0 );
710}
711
XiaokangQianed582dd2022-04-13 08:21:05 +0000712/* Update the handshake state machine */
713
XiaokangQiancfd925f2022-04-14 07:10:37 +0000714static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000715{
716 int ret = 0;
717
XiaokangQian7807f9f2022-02-15 10:04:37 +0000718 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
719 if( ret != 0 )
720 {
721 MBEDTLS_SSL_DEBUG_RET( 1,
722 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
723 return( ret );
724 }
725
726 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
727 return( 0 );
728
729}
730
731/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000732 * Main entry point from the state machine; orchestrates the otherfunctions.
733 */
734
735static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
736{
737
738 int ret = 0;
XiaokangQianed582dd2022-04-13 08:21:05 +0000739 unsigned char* buf = NULL;
740 size_t buflen = 0;
741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
742
743 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
744 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
745 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
746 &buf, &buflen ) );
747
748 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
749 buf + buflen ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
XiaokangQiancfd925f2022-04-14 07:10:37 +0000751 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000752
753cleanup:
754
755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
756 return( ret );
757}
758
759/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000760 * TLS and DTLS 1.3 State Maschine -- server side
761 */
Jerry Yu27561932021-08-27 17:07:38 +0800762int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800763{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000764 int ret = 0;
765
766 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
768
Jerry Yue3b34122021-09-28 17:53:35 +0800769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
770 mbedtls_ssl_states_str( ssl->state ),
771 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800772
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773 switch( ssl->state )
774 {
775 /* start state */
776 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
778
779 break;
780
781 /* ----- READ CLIENT HELLO ----*/
782
783 case MBEDTLS_SSL_CLIENT_HELLO:
784
XiaokangQian4080a7f2022-04-11 09:55:18 +0000785 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000786 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +0000787 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788
789 break;
790
XiaokangQian7807f9f2022-02-15 10:04:37 +0000791 default:
792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
794 }
795
796 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800797}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800798
Jerry Yufb4b6472022-01-27 15:03:26 +0800799#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */