blob: d181cd294b75a3b37914dbe2fd1bea605c38ecf2 [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{
XiaokangQian4080a7f2022-04-11 09:55:18 +000051 size_t versions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +000052 int tls13_supported = 0;
53 int major_ver, minor_ver;
54 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +000055 const unsigned char *versions_end;
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 );
XiaokangQian7807f9f2022-02-15 10:04:37 +000062
XiaokangQian4080a7f2022-04-11 09:55:18 +000063 versions_end = p + versions_len;
64 while( p < versions_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +000065 {
XiaokangQiancfd925f2022-04-14 07:10:37 +000066 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, versions_end, 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +000067 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
68
69 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
70 if( major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
71 minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
72 {
73 tls13_supported = 1;
74 break;
75 }
76
77 p += 2;
78 }
79
80 if( tls13_supported == 0 )
81 {
XiaokangQian4080a7f2022-04-11 09:55:18 +000082 /* Here we only support TLS 1.3, we need report "bad protocol" if it
83 * doesn't support TLS 1.2.
XiaokangQianc5763b52022-04-02 03:34:37 +000084 */
XiaokangQian7807f9f2022-02-15 10:04:37 +000085
86 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
87
88 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
89 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
90 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
91 }
92
93 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%d:%d]",
94 major_ver, minor_ver ) );
95
96 ssl->major_ver = major_ver;
97 ssl->minor_ver = minor_ver;
XiaokangQian7807f9f2022-02-15 10:04:37 +000098 return( 0 );
99}
100
101#if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
102/* This function parses the TLS 1.3 supported_groups extension and
103 * stores the received groups in ssl->handshake->curves.
104 *
105 * From RFC 8446:
106 * enum {
107 * ... (0xFFFF)
108 * } NamedGroup;
109 * struct {
110 * NamedGroup named_group_list<2..2^16-1>;
111 * } NamedGroupList;
112 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000113static int ssl_tls13_parse_supported_groups_ext(
XiaokangQian7807f9f2022-02-15 10:04:37 +0000114 mbedtls_ssl_context *ssl,
115 const unsigned char *buf, const unsigned char *end )
116{
117
XiaokangQian4080a7f2022-04-11 09:55:18 +0000118 size_t named_group_list_len, curve_list_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000119 const unsigned char *p = buf;
120 const mbedtls_ecp_curve_info *curve_info, **curves;
121 const unsigned char *extentions_end;
122
123 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000124 named_group_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000125 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000126 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, named_group_list_len );
127 if( named_group_list_len % 2 != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000128 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
129
XiaokangQianc5763b52022-04-02 03:34:37 +0000130 /* At the moment, this can happen when receiving a second
XiaokangQian7807f9f2022-02-15 10:04:37 +0000131 * ClientHello after an HRR. We should properly reset the
132 * state upon receiving an HRR, in which case we should
133 * not observe handshake->curves already being allocated. */
134 if( ssl->handshake->curves != NULL )
135 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000136 mbedtls_free( ssl->handshake->curves );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000137 ssl->handshake->curves = NULL;
138 }
139
140 /* Don't allow our peer to make us allocate too much memory,
XiaokangQianc5763b52022-04-02 03:34:37 +0000141 * and leave room for a final 0
142 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000143 curve_list_len = named_group_list_len / 2 + 1;
144 if( curve_list_len > MBEDTLS_ECP_DP_MAX )
145 curve_list_len = MBEDTLS_ECP_DP_MAX;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000146
XiaokangQian4080a7f2022-04-11 09:55:18 +0000147 if( ( curves = mbedtls_calloc( curve_list_len, sizeof( *curves ) ) ) == NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000148 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
149
XiaokangQian4080a7f2022-04-11 09:55:18 +0000150 extentions_end = p + named_group_list_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000151 ssl->handshake->curves = curves;
152
XiaokangQian4080a7f2022-04-11 09:55:18 +0000153 while ( p < extentions_end && curve_list_len > 1 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000154 {
155 uint16_t tls_grp_id = MBEDTLS_GET_UINT16_BE( p, 0 );
156 curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_grp_id );
157
158 /* mbedtls_ecp_curve_info_from_tls_id() uses the mbedtls_ecp_curve_info
159 * data structure (defined in ecp.c), which only includes the list of
160 * curves implemented. Hence, we only add curves that are also supported
XiaokangQianc5763b52022-04-02 03:34:37 +0000161 * and implemented by the server.
162 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000163 if( curve_info != NULL )
164 {
165 *curves++ = curve_info;
166 MBEDTLS_SSL_DEBUG_MSG( 4, ( "supported curve: %s", curve_info->name ) );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000167 curve_list_len--;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000168 }
169
170 p += 2;
171 }
172
173 return( 0 );
174
175}
176#endif /* MBEDTLS_ECDH_C || ( MBEDTLS_ECDSA_C */
177
XiaokangQian88408882022-04-02 10:15:03 +0000178#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000179/*
180 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
181 * extension is correct and stores the provided key shares. Whether this is an
182 * acceptable key share depends on the selected ciphersuite.
183 *
184 * Possible return values are:
185 * - 0: Successful processing of the client provided key share extension.
186 * - MBEDTLS_ERR_SSL_HRR_REQUIRED: The key share provided by the client
187 * does not match a group supported by the server. A HelloRetryRequest will
188 * be needed.
189 * - Another negative return value for fatal errors.
190*/
191
192static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
193 const unsigned char *buf,
194 const unsigned char *end )
195{
196 int ret = 0;
197 unsigned char const *p = buf;
198 unsigned char const *extentions_end;
199
XiaokangQian4080a7f2022-04-11 09:55:18 +0000200 size_t total_extensions_len, key_share_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000201 int match_found = 0;
202
203 /* From RFC 8446:
204 *
205 * struct {
206 * KeyShareEntry client_shares<0..2^16-1>;
207 * } KeyShareClientHello;
208 *
209 */
210
211 /* Read total legnth of KeyShareClientHello */
212 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
213
XiaokangQian4080a7f2022-04-11 09:55:18 +0000214 total_extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000215 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000216 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, total_extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000217
218 ssl->handshake->offered_group_id = 0;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000219 extentions_end = p + total_extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000220
221 /* We try to find a suitable key share entry and copy it to the
222 * handshake context. Later, we have to find out whether we can do
223 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000224 * dismiss it and send a HelloRetryRequest message.
225 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000226
XiaokangQian4080a7f2022-04-11 09:55:18 +0000227 for( ; p < extentions_end; p += key_share_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000228 {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000229 uint16_t group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000230
231 /*
232 * struct {
233 * NamedGroup group;
234 * opaque key_exchange<1..2^16-1>;
235 * } KeyShareEntry;
236 */
237 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extentions_end, 4 );
238
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000239 group = MBEDTLS_GET_UINT16_BE( p, 0 );
240 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000241
XiaokangQian4080a7f2022-04-11 09:55:18 +0000242 key_share_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000243 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000244
245 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000246 * for input validation purposes.
247 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000248 if( match_found == 1 )
249 continue;
250
251 /*
252 * NamedGroup matching
253 *
254 * For now, we only support ECDHE groups, but e.g.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000255
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000256 * Type 1: ECDHE shares
XiaokangQian7807f9f2022-02-15 10:04:37 +0000257 *
258 * - Check if we recognize the group
259 * - Check if it's supported
260 */
261
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000262 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
263 {
264 const mbedtls_ecp_curve_info *curve_info =
265 mbedtls_ecp_curve_info_from_tls_id( group );
266 if( curve_info == NULL )
267 {
268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
270 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000271
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000272 match_found = 1;
273
274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
275
276 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
277 if( ret != 0 )
278 return( ret );
279 }
280 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000281 {
282 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000283 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000284 continue;
285 }
286
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000287 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000288 }
289
290 if( match_found == 0 )
291 {
292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
293 return( MBEDTLS_ERR_SSL_HRR_REQUIRED );
294 }
295 return( 0 );
296}
XiaokangQian88408882022-04-02 10:15:03 +0000297#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000298
XiaokangQiancfd925f2022-04-14 07:10:37 +0000299#if defined(MBEDTLS_SSL_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000300static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000301{
XiaokangQian3207a322022-02-23 03:15:27 +0000302 ((void) ssl);
303
XiaokangQian7807f9f2022-02-15 10:04:37 +0000304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- KEY_SHARE_EXTENSION ( %s )",
306 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ?
307 "TRUE" : "FALSE" ) );
308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
309 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
310 "TRUE" : "FALSE" ) );
311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
312 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ?
313 "TRUE" : "FALSE" ) );
314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
315 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ?
316 "TRUE" : "FALSE" ) );
317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
318 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
319 "TRUE" : "FALSE" ) );
320 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
321 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
322 "TRUE" : "FALSE" ) );
323#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SERVERNAME_EXTENSION ( %s )",
325 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
326 "TRUE" : "FALSE" ) );
327#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000328}
XiaokangQiancfd925f2022-04-14 07:10:37 +0000329#endif /* MBEDTLS_SSL_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000330
XiaokangQian4080a7f2022-04-11 09:55:18 +0000331static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000332 int ext_id_mask )
333{
334 int masked = ssl->handshake->extensions_present & ext_id_mask;
335 return( masked == ext_id_mask );
336}
337
XiaokangQian4080a7f2022-04-11 09:55:18 +0000338static int ssl_tls13_client_hello_has_cert_extensions( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000339{
XiaokangQian4080a7f2022-04-11 09:55:18 +0000340 return( ssl_tls13_client_hello_has_exts( ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000341 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
342 MBEDTLS_SSL_EXT_KEY_SHARE |
343 MBEDTLS_SSL_EXT_SIG_ALG ) );
344}
345
XiaokangQian4080a7f2022-04-11 09:55:18 +0000346static int ssl_tls13_check_certificate_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000347{
348 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
349 return( 0 );
350
XiaokangQian4080a7f2022-04-11 09:55:18 +0000351 if( !ssl_tls13_client_hello_has_cert_extensions( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000352 return( 0 );
353
354 ssl->handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
355 return( 1 );
356}
357
XiaokangQian4080a7f2022-04-11 09:55:18 +0000358/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000359 *
360 * STATE HANDLING: ClientHello
361 *
362 * There are three possible classes of outcomes when parsing the CH:
363 *
364 * 1) The CH was well-formed and matched the server's configuration.
365 *
366 * In this case, the server progresses to sending its ServerHello.
367 *
368 * 2) The CH was well-formed but didn't match the server's configuration.
369 *
370 * For example, the client might not have offered a key share which
371 * the server supports, or the server might require a cookie.
372 *
373 * In this case, the server sends a HelloRetryRequest.
374 *
375 * 3) The CH was ill-formed
376 *
377 * In this case, we abort the handshake.
378 *
379 */
380
381/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000382 * Structure of this message:
383 *
384 * uint16 ProtocolVersion;
385 * opaque Random[32];
386 *
387 * uint8 CipherSuite[2]; // Cryptographic suite selector
388 *
389 * struct {
390 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
391 * Random random;
392 * opaque legacy_session_id<0..32>;
393 * CipherSuite cipher_suites<2..2^16-2>;
394 * opaque legacy_compression_methods<1..2^8-1>;
395 * Extension extensions<8..2^16-1>;
396 * } ClientHello;
397 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000398
399#define SSL_CLIENT_HELLO_OK 0
400#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
401
XiaokangQian4080a7f2022-04-11 09:55:18 +0000402static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
403 const unsigned char *buf,
404 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000405{
406 int ret;
407 size_t i, j;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000408 size_t legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000409 size_t cipher_suites_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000410 size_t extensions_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000411 const unsigned char *cipher_suites_start;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000412 const unsigned char *p = buf;
413 const unsigned char *extensions_end;
414
XiaokangQianed582dd2022-04-13 08:21:05 +0000415 const int* cipher_suites;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000416 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
417
418 int hrr_required = 0;
419
420 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
421
422 /*
423 * ClientHello layer:
424 * 0 . 1 protocol version
425 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
XiaokangQianc5763b52022-04-02 03:34:37 +0000426 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000427 * 35 . 34+x session id
428 * 35+x . 35+x DTLS only: cookie length ( 1 byte )
429 * 36+x . .. DTLS only: cookie
430 * .. . .. ciphersuite list length ( 2 bytes )
431 * .. . .. ciphersuite list
432 * .. . .. compression alg. list length ( 1 byte )
433 * .. . .. compression alg. list
434 * .. . .. extensions length ( 2 bytes, optional )
435 * .. . .. extensions ( optional )
436 */
437
XiaokangQianc5763b52022-04-02 03:34:37 +0000438 /* Needs to be updated due to mandatory extensions
XiaokangQian7807f9f2022-02-15 10:04:37 +0000439 * Minimal length ( with everything empty and extensions ommitted ) is
440 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
441 * read at least up to session id length without worrying.
442 */
443 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
444
445 /* ...
446 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
447 * ...
448 * with ProtocolVersion defined as:
449 * uint16 ProtocolVersion;
450 */
451 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
452 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
453 {
454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
455 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
456 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
457 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
458 return ret;
459 }
460 p += 2;
461
462 /*
463 * Save client random
464 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000465 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
466 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000467
XiaokangQian4080a7f2022-04-11 09:55:18 +0000468 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000469 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000470
471 /*
472 * Parse session ID
473 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000474 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000475 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000476
XiaokangQian4080a7f2022-04-11 09:55:18 +0000477 if( legacy_session_id_len > 32 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000478 {
479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
480 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
481 }
482
XiaokangQian4080a7f2022-04-11 09:55:18 +0000483 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000484
485 /* Note that this field is echoed even if
486 * the client's value corresponded to a cached pre-TLS 1.3 session
487 * which the server has chosen not to resume. A client which
488 * receives a legacy_session_id_echo field that does not match what
489 * it sent in the ClientHello MUST abort the handshake with an
490 * "illegal_parameter" alert.
491 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000492 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
493 buf, legacy_session_id_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000494
XiaokangQianed582dd2022-04-13 08:21:05 +0000495 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000496 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000497
498 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
499 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
500 p += 2;
501
502 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
503
504 /* store pointer to ciphersuite list */
XiaokangQianed582dd2022-04-13 08:21:05 +0000505 cipher_suites_start = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000506
507 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
508 p, cipher_suites_len );
509
XiaokangQianed582dd2022-04-13 08:21:05 +0000510 /* skip cipher_suites for now */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000511 p += cipher_suites_len;
512
XiaokangQian4080a7f2022-04-11 09:55:18 +0000513 /* ...
514 * uint8 legacy_compression_method = 0;
515 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000516 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000517 p += 1;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000518 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
519 if( p[0] != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000520 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
522 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
523 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
524 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000525 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000526 p++;
527
528 /*
XiaokangQianed582dd2022-04-13 08:21:05 +0000529 * Check the extensions length
XiaokangQian7807f9f2022-02-15 10:04:37 +0000530 */
531 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000532 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000533 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000534 extensions_end = p + extensions_len;
535 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000536
XiaokangQian4080a7f2022-04-11 09:55:18 +0000537 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000538
539 while( p < extensions_end )
540 {
541 unsigned int extension_type;
542 size_t extension_data_len;
543 const unsigned char *extension_data_end;
544
545 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
546 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
547 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
548 p += 4;
549
550 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
551 extension_data_end = p + extension_data_len;
552
553 switch( extension_type )
554 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000555#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
556 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
558
559 /* Supported Groups Extension
560 *
561 * When sent by the client, the "supported_groups" extension
562 * indicates the named groups which the client supports,
563 * ordered from most preferred to least preferred.
564 */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000565 ret = ssl_tls13_parse_supported_groups_ext( ssl, p,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000566 extension_data_end );
567 if( ret != 0 )
568 {
569 MBEDTLS_SSL_DEBUG_RET( 1,
570 "mbedtls_ssl_parse_supported_groups_ext", ret );
571 return( ret );
572 }
573
574 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
575 break;
576#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
577
XiaokangQian88408882022-04-02 10:15:03 +0000578#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000579 case MBEDTLS_TLS_EXT_KEY_SHARE:
580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
581
582 /*
583 * Key Share Extension
584 *
585 * When sent by the client, the "key_share" extension
586 * contains the endpoint's cryptographic parameters for
587 * ECDHE/DHE key establishment methods.
588 */
589 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
590 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
591 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
593 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000594 }
595
596 if( ret != 0 )
597 return( ret );
598
599 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
600 break;
XiaokangQian88408882022-04-02 10:15:03 +0000601#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000602
603 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
605
606 ret = ssl_tls13_parse_supported_versions_ext(
607 ssl, p, extension_data_end );
608 if( ret != 0 )
609 {
610 MBEDTLS_SSL_DEBUG_RET( 1,
611 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
612 return( ret );
613 }
614 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
615 break;
616
617#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
618 case MBEDTLS_TLS_EXT_SIG_ALG:
619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
620
621 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
622 extension_data_end );
623 if( ret != 0 )
624 {
625 MBEDTLS_SSL_DEBUG_MSG( 1,
626 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
627 ret ) );
628 return( ret );
629 }
630 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
631 break;
632#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
633
634 default:
635 MBEDTLS_SSL_DEBUG_MSG( 3,
636 ( "unknown extension found: %ud ( ignoring )",
637 extension_type ) );
638 }
639
640 p += extension_data_len;
641 }
642
643 /* Update checksum with either
644 * - The entire content of the CH message, if no PSK extension is present
645 * - The content up to but excluding the PSK extension, if present.
646 */
XiaokangQianc4b8c992022-04-07 11:31:38 +0000647 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
648 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000649 /*
650 * Search for a matching ciphersuite
651 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000652 cipher_suites = ssl->conf->ciphersuite_list;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000653 ciphersuite_info = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +0000654 for ( j = 0, p = cipher_suites_start; j < cipher_suites_len; j += 2, p += 2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000655 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000656 for ( i = 0; cipher_suites[i] != 0; i++ )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000657 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000658 if( MBEDTLS_GET_UINT16_BE(p, 0) != cipher_suites[i] )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000659 continue;
660
661 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
XiaokangQianed582dd2022-04-13 08:21:05 +0000662 cipher_suites[i] );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000663
664 if( ciphersuite_info == NULL )
665 {
666 MBEDTLS_SSL_DEBUG_MSG(
667 1,
668 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
670 }
671
672 goto have_ciphersuite;
673
674 }
675 }
676
677 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
678
679have_ciphersuite:
680
681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
682 ciphersuite_info->name ) );
683
XiaokangQianed582dd2022-04-13 08:21:05 +0000684 ssl->session_negotiate->ciphersuite = cipher_suites[i];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000685 ssl->handshake->ciphersuite_info = ciphersuite_info;
686
687 /* List all the extensions we have received */
XiaokangQiancfd925f2022-04-14 07:10:37 +0000688#if defined(MBEDTLS_SSL_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000689 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQiancfd925f2022-04-14 07:10:37 +0000690#endif /* MBEDTLS_SSL_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000691
692 /*
693 * Determine the key exchange algorithm to use.
694 * There are three types of key exchanges supported in TLS 1.3:
695 * - (EC)DH with ECDSA,
696 * - (EC)DH with PSK,
697 * - plain PSK.
698 *
699 * The PSK-based key exchanges may additionally be used with 0-RTT.
700 *
701 * Our built-in order of preference is
702 * 1 ) Plain PSK Mode
703 * 2 ) (EC)DHE-PSK Mode
704 * 3 ) Certificate Mode
705 */
706
XiaokangQian4080a7f2022-04-11 09:55:18 +0000707 if( !ssl_tls13_check_certificate_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000708 {
709 MBEDTLS_SSL_DEBUG_MSG(
710 1,
711 ( "ClientHello message misses mandatory extensions." ) );
712 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
713 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
714 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
715 }
716
XiaokangQian7807f9f2022-02-15 10:04:37 +0000717 if( hrr_required == 1 )
718 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
719
720 return( 0 );
721}
722
XiaokangQianed582dd2022-04-13 08:21:05 +0000723/* Update the handshake state machine */
724
XiaokangQiancfd925f2022-04-14 07:10:37 +0000725static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000726{
727 int ret = 0;
728
XiaokangQian7807f9f2022-02-15 10:04:37 +0000729 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
730 if( ret != 0 )
731 {
732 MBEDTLS_SSL_DEBUG_RET( 1,
733 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
734 return( ret );
735 }
736
737 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
738 return( 0 );
739
740}
741
742/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000743 * Main entry point from the state machine; orchestrates the otherfunctions.
744 */
745
746static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
747{
748
749 int ret = 0;
XiaokangQianed582dd2022-04-13 08:21:05 +0000750 unsigned char* buf = NULL;
751 size_t buflen = 0;
752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
753
754 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
755 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
756 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
757 &buf, &buflen ) );
758
759 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
760 buf + buflen ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
XiaokangQiancfd925f2022-04-14 07:10:37 +0000762 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
XiaokangQianed582dd2022-04-13 08:21:05 +0000763
764cleanup:
765
766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
767 return( ret );
768}
769
770/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000771 * TLS and DTLS 1.3 State Maschine -- server side
772 */
Jerry Yu27561932021-08-27 17:07:38 +0800773int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800774{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775 int ret = 0;
776
777 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
779
Jerry Yue3b34122021-09-28 17:53:35 +0800780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
781 mbedtls_ssl_states_str( ssl->state ),
782 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800783
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 switch( ssl->state )
785 {
786 /* start state */
787 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
789
790 break;
791
792 /* ----- READ CLIENT HELLO ----*/
793
794 case MBEDTLS_SSL_CLIENT_HELLO:
795
XiaokangQian4080a7f2022-04-11 09:55:18 +0000796 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000797 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +0000798 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799
800 break;
801
XiaokangQian7807f9f2022-02-15 10:04:37 +0000802 default:
803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
805 }
806
807 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800808}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800809
Jerry Yufb4b6472022-01-27 15:03:26 +0800810#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */