blob: b172ec9b549c39f85dae03ff1c888c945b7c000c [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
XiaokangQian4080a7f2022-04-11 09:55:18 +0000310static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000311{
XiaokangQian3207a322022-02-23 03:15:27 +0000312 ((void) ssl);
313
XiaokangQian7807f9f2022-02-15 10:04:37 +0000314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
315 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- KEY_SHARE_EXTENSION ( %s )",
316 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ?
317 "TRUE" : "FALSE" ) );
318 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
319 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
320 "TRUE" : "FALSE" ) );
321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
322 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ?
323 "TRUE" : "FALSE" ) );
324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
325 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ?
326 "TRUE" : "FALSE" ) );
327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
328 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
329 "TRUE" : "FALSE" ) );
330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
331 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
332 "TRUE" : "FALSE" ) );
333#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
334 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SERVERNAME_EXTENSION ( %s )",
335 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
336 "TRUE" : "FALSE" ) );
337#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000338}
339
XiaokangQian4080a7f2022-04-11 09:55:18 +0000340static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000341 int ext_id_mask )
342{
343 int masked = ssl->handshake->extensions_present & ext_id_mask;
344 return( masked == ext_id_mask );
345}
346
XiaokangQian4080a7f2022-04-11 09:55:18 +0000347static int ssl_tls13_client_hello_has_cert_extensions( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000348{
XiaokangQian4080a7f2022-04-11 09:55:18 +0000349 return( ssl_tls13_client_hello_has_exts( ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000350 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
351 MBEDTLS_SSL_EXT_KEY_SHARE |
352 MBEDTLS_SSL_EXT_SIG_ALG ) );
353}
354
XiaokangQian4080a7f2022-04-11 09:55:18 +0000355static int ssl_tls13_check_certificate_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000356{
357 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
358 return( 0 );
359
XiaokangQian4080a7f2022-04-11 09:55:18 +0000360 if( !ssl_tls13_client_hello_has_cert_extensions( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000361 return( 0 );
362
363 ssl->handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
364 return( 1 );
365}
366
XiaokangQian4080a7f2022-04-11 09:55:18 +0000367/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000368 *
369 * STATE HANDLING: ClientHello
370 *
371 * There are three possible classes of outcomes when parsing the CH:
372 *
373 * 1) The CH was well-formed and matched the server's configuration.
374 *
375 * In this case, the server progresses to sending its ServerHello.
376 *
377 * 2) The CH was well-formed but didn't match the server's configuration.
378 *
379 * For example, the client might not have offered a key share which
380 * the server supports, or the server might require a cookie.
381 *
382 * In this case, the server sends a HelloRetryRequest.
383 *
384 * 3) The CH was ill-formed
385 *
386 * In this case, we abort the handshake.
387 *
388 */
389
390/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000391 * Structure of this message:
392 *
393 * uint16 ProtocolVersion;
394 * opaque Random[32];
395 *
396 * uint8 CipherSuite[2]; // Cryptographic suite selector
397 *
398 * struct {
399 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
400 * Random random;
401 * opaque legacy_session_id<0..32>;
402 * CipherSuite cipher_suites<2..2^16-2>;
403 * opaque legacy_compression_methods<1..2^8-1>;
404 * Extension extensions<8..2^16-1>;
405 * } ClientHello;
406 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000407
408#define SSL_CLIENT_HELLO_OK 0
409#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
410
XiaokangQian4080a7f2022-04-11 09:55:18 +0000411static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
412 const unsigned char *buf,
413 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000414{
415 int ret;
416 size_t i, j;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000417 size_t legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000418 size_t cipher_suites_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000419 size_t extensions_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000420 const unsigned char *cipher_suites_start;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000421 const unsigned char *p = buf;
422 const unsigned char *extensions_end;
423
XiaokangQianed582dd2022-04-13 08:21:05 +0000424 const int* cipher_suites;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000425 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
426
427 int hrr_required = 0;
428
429 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
430
431 /*
432 * ClientHello layer:
433 * 0 . 1 protocol version
434 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
XiaokangQianc5763b52022-04-02 03:34:37 +0000435 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000436 * 35 . 34+x session id
437 * 35+x . 35+x DTLS only: cookie length ( 1 byte )
438 * 36+x . .. DTLS only: cookie
439 * .. . .. ciphersuite list length ( 2 bytes )
440 * .. . .. ciphersuite list
441 * .. . .. compression alg. list length ( 1 byte )
442 * .. . .. compression alg. list
443 * .. . .. extensions length ( 2 bytes, optional )
444 * .. . .. extensions ( optional )
445 */
446
XiaokangQianc5763b52022-04-02 03:34:37 +0000447 /* Needs to be updated due to mandatory extensions
XiaokangQian7807f9f2022-02-15 10:04:37 +0000448 * Minimal length ( with everything empty and extensions ommitted ) is
449 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
450 * read at least up to session id length without worrying.
451 */
452 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
453
454 /* ...
455 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
456 * ...
457 * with ProtocolVersion defined as:
458 * uint16 ProtocolVersion;
459 */
460 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
461 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
462 {
463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
464 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
465 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
466 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
467 return ret;
468 }
469 p += 2;
470
471 /*
472 * Save client random
473 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000474 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
475 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000476
XiaokangQian4080a7f2022-04-11 09:55:18 +0000477 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000478 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000479
480 /*
481 * Parse session ID
482 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000483 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000484 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000485
XiaokangQian4080a7f2022-04-11 09:55:18 +0000486 if( legacy_session_id_len > 32 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000487 {
488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
489 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
490 }
491
XiaokangQian4080a7f2022-04-11 09:55:18 +0000492 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000493
494 /* Note that this field is echoed even if
495 * the client's value corresponded to a cached pre-TLS 1.3 session
496 * which the server has chosen not to resume. A client which
497 * receives a legacy_session_id_echo field that does not match what
498 * it sent in the ClientHello MUST abort the handshake with an
499 * "illegal_parameter" alert.
500 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000501 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
502 buf, legacy_session_id_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000503
XiaokangQianed582dd2022-04-13 08:21:05 +0000504 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000505 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000506
507 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
508 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
509 p += 2;
510
511 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
512
513 /* store pointer to ciphersuite list */
XiaokangQianed582dd2022-04-13 08:21:05 +0000514 cipher_suites_start = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000515
516 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
517 p, cipher_suites_len );
518
XiaokangQianed582dd2022-04-13 08:21:05 +0000519 /* skip cipher_suites for now */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000520 p += cipher_suites_len;
521
XiaokangQian4080a7f2022-04-11 09:55:18 +0000522 /* ...
523 * uint8 legacy_compression_method = 0;
524 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000525 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000526 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
527 if( p[0] != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000528 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
530 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
531 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
532 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000533 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000534 p++;
535
536 /*
XiaokangQianed582dd2022-04-13 08:21:05 +0000537 * Check the extensions length
XiaokangQian7807f9f2022-02-15 10:04:37 +0000538 */
539 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000540 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000541 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000542 extensions_end = p + extensions_len;
543 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000544
XiaokangQian4080a7f2022-04-11 09:55:18 +0000545 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000546
547 while( p < extensions_end )
548 {
549 unsigned int extension_type;
550 size_t extension_data_len;
551 const unsigned char *extension_data_end;
552
553 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
554 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
555 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
556 p += 4;
557
558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
559 extension_data_end = p + extension_data_len;
560
561 switch( extension_type )
562 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000563#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
564 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
566
567 /* Supported Groups Extension
568 *
569 * When sent by the client, the "supported_groups" extension
570 * indicates the named groups which the client supports,
571 * ordered from most preferred to least preferred.
572 */
573 ret = mbedtls_ssl_tls13_parse_supported_groups_ext( ssl, p,
574 extension_data_end );
575 if( ret != 0 )
576 {
577 MBEDTLS_SSL_DEBUG_RET( 1,
578 "mbedtls_ssl_parse_supported_groups_ext", ret );
579 return( ret );
580 }
581
582 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
583 break;
584#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
585
XiaokangQian88408882022-04-02 10:15:03 +0000586#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000587 case MBEDTLS_TLS_EXT_KEY_SHARE:
588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
589
590 /*
591 * Key Share Extension
592 *
593 * When sent by the client, the "key_share" extension
594 * contains the endpoint's cryptographic parameters for
595 * ECDHE/DHE key establishment methods.
596 */
597 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
598 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
599 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
601 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000602 }
603
604 if( ret != 0 )
605 return( ret );
606
607 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
608 break;
XiaokangQian88408882022-04-02 10:15:03 +0000609#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000610
611 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
613
614 ret = ssl_tls13_parse_supported_versions_ext(
615 ssl, p, extension_data_end );
616 if( ret != 0 )
617 {
618 MBEDTLS_SSL_DEBUG_RET( 1,
619 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
620 return( ret );
621 }
622 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
623 break;
624
625#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
626 case MBEDTLS_TLS_EXT_SIG_ALG:
627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
628
629 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
630 extension_data_end );
631 if( ret != 0 )
632 {
633 MBEDTLS_SSL_DEBUG_MSG( 1,
634 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
635 ret ) );
636 return( ret );
637 }
638 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
639 break;
640#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
641
642 default:
643 MBEDTLS_SSL_DEBUG_MSG( 3,
644 ( "unknown extension found: %ud ( ignoring )",
645 extension_type ) );
646 }
647
648 p += extension_data_len;
649 }
650
651 /* Update checksum with either
652 * - The entire content of the CH message, if no PSK extension is present
653 * - The content up to but excluding the PSK extension, if present.
654 */
XiaokangQianc4b8c992022-04-07 11:31:38 +0000655 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
656 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000657 /*
658 * Search for a matching ciphersuite
659 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000660 cipher_suites = ssl->conf->ciphersuite_list;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000661 ciphersuite_info = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +0000662 for ( j = 0, p = cipher_suites_start; j < cipher_suites_len; j += 2, p += 2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000663 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000664 for ( i = 0; cipher_suites[i] != 0; i++ )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000665 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000666 if( MBEDTLS_GET_UINT16_BE(p, 0) != cipher_suites[i] )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000667 continue;
668
669 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
XiaokangQianed582dd2022-04-13 08:21:05 +0000670 cipher_suites[i] );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000671
672 if( ciphersuite_info == NULL )
673 {
674 MBEDTLS_SSL_DEBUG_MSG(
675 1,
676 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
678 }
679
680 goto have_ciphersuite;
681
682 }
683 }
684
685 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
686
687have_ciphersuite:
688
689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
690 ciphersuite_info->name ) );
691
XiaokangQianed582dd2022-04-13 08:21:05 +0000692 ssl->session_negotiate->ciphersuite = cipher_suites[i];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000693 ssl->handshake->ciphersuite_info = ciphersuite_info;
694
695 /* List all the extensions we have received */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000696 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000697
698 /*
699 * Determine the key exchange algorithm to use.
700 * There are three types of key exchanges supported in TLS 1.3:
701 * - (EC)DH with ECDSA,
702 * - (EC)DH with PSK,
703 * - plain PSK.
704 *
705 * The PSK-based key exchanges may additionally be used with 0-RTT.
706 *
707 * Our built-in order of preference is
708 * 1 ) Plain PSK Mode
709 * 2 ) (EC)DHE-PSK Mode
710 * 3 ) Certificate Mode
711 */
712
XiaokangQian4080a7f2022-04-11 09:55:18 +0000713 if( !ssl_tls13_check_certificate_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000714 {
715 MBEDTLS_SSL_DEBUG_MSG(
716 1,
717 ( "ClientHello message misses mandatory extensions." ) );
718 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
719 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
720 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
721 }
722
XiaokangQian7807f9f2022-02-15 10:04:37 +0000723 if( hrr_required == 1 )
724 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
725
726 return( 0 );
727}
728
XiaokangQianed582dd2022-04-13 08:21:05 +0000729/* Update the handshake state machine */
730
XiaokangQian4080a7f2022-04-11 09:55:18 +0000731static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl,
732 int hrr_required )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000733{
734 int ret = 0;
735
XiaokangQianed582dd2022-04-13 08:21:05 +0000736 if( ssl->handshake->hello_retry_requests_sent == 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000737 {
738 hrr_required = SSL_CLIENT_HELLO_HRR_REQUIRED;
739 }
740
741 if( hrr_required == SSL_CLIENT_HELLO_HRR_REQUIRED )
742 {
743 /*
744 * Create stateless transcript hash for HRR
745 */
746 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
747 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
748 if( ret != 0 )
749 {
750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr",
751 ret );
752 return( ret );
753 }
754 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
755
756 /* Transmit Hello Retry Request */
757 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
758 return( 0 );
759 }
760
761 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
762 if( ret != 0 )
763 {
764 MBEDTLS_SSL_DEBUG_RET( 1,
765 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
766 return( ret );
767 }
768
769 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
770 return( 0 );
771
772}
773
774/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000775 * Main entry point from the state machine; orchestrates the otherfunctions.
776 */
777
778static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
779{
780
781 int ret = 0;
782 int hrr_required = SSL_CLIENT_HELLO_OK;
783 unsigned char* buf = NULL;
784 size_t buflen = 0;
785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
786
787 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
788 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
789 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
790 &buf, &buflen ) );
791
792 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
793 buf + buflen ) );
794 hrr_required = ret;
795
796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
797 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl,
798 hrr_required ) );
799
800cleanup:
801
802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
803 return( ret );
804}
805
806/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807 * TLS and DTLS 1.3 State Maschine -- server side
808 */
Jerry Yu27561932021-08-27 17:07:38 +0800809int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800810{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811 int ret = 0;
812
813 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
815
Jerry Yue3b34122021-09-28 17:53:35 +0800816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
817 mbedtls_ssl_states_str( ssl->state ),
818 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800819
XiaokangQian7807f9f2022-02-15 10:04:37 +0000820 switch( ssl->state )
821 {
822 /* start state */
823 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000824 ssl->handshake->hello_retry_requests_sent = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000825 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
826
827 break;
828
829 /* ----- READ CLIENT HELLO ----*/
830
831 case MBEDTLS_SSL_CLIENT_HELLO:
832
XiaokangQian4080a7f2022-04-11 09:55:18 +0000833 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +0000835 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836
837 break;
838
XiaokangQian7807f9f2022-02-15 10:04:37 +0000839 default:
840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
841 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
842 }
843
844 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800845}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800846
Jerry Yufb4b6472022-01-27 15:03:26 +0800847#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */