blob: f002595264998d2daac661ed63c05e3733254415 [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{
56 size_t list_len;
57 int tls13_supported = 0;
58 int major_ver, minor_ver;
59 const unsigned char *p = buf;
60 const unsigned char *version_end;
61
62 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
63
64 list_len = p[0];
65 p += 1;
66
67 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
68 if( list_len % 2 != 0 )
69 {
70 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid supported version list length %" MBEDTLS_PRINTF_SIZET,
71 list_len ) );
72 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
73 }
74
75 version_end = p + list_len;
76 while( p < version_end )
77 {
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 {
93 /* When we support runtime negotiation of TLS 1.2 and TLS 1.3, we need
XiaokangQianc5763b52022-04-02 03:34:37 +000094 * a graceful fallback to TLS 1.2 in this case.
95 */
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
129 size_t list_size, our_size;
130 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 );
135 list_size = MBEDTLS_GET_UINT16_BE( p, 0 );
136 p += 2;
137 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_size );
138 if( list_size % 2 != 0 )
139 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 {
XiaokangQian88408882022-04-02 10:15:03 +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 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000154 our_size = list_size / 2 + 1;
155 if( our_size > MBEDTLS_ECP_DP_MAX )
156 our_size = MBEDTLS_ECP_DP_MAX;
157
158 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
159 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
160
161 extentions_end = p + list_size;
162 ssl->handshake->curves = curves;
163
164 while ( p < extentions_end && our_size > 1 )
165 {
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 ) );
178 our_size--;
179 }
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
211 size_t total_ext_len, cur_share_len;
212 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
225 total_ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
226 p += 2;
227 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, total_ext_len );
228
229 ssl->handshake->offered_group_id = 0;
230 extentions_end = p + total_ext_len;
231
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
238 for( ; p < extentions_end; p += cur_share_len )
239 {
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
253 cur_share_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000254 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000255
256 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000257 * for input validation purposes.
258 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000259 if( match_found == 1 )
260 continue;
261
262 /*
263 * NamedGroup matching
264 *
265 * For now, we only support ECDHE groups, but e.g.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000266
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000267 * Type 1: ECDHE shares
XiaokangQian7807f9f2022-02-15 10:04:37 +0000268 *
269 * - Check if we recognize the group
270 * - Check if it's supported
271 */
272
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000273 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
274 {
275 const mbedtls_ecp_curve_info *curve_info =
276 mbedtls_ecp_curve_info_from_tls_id( group );
277 if( curve_info == NULL )
278 {
279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
280 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
281 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000282
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000283 match_found = 1;
284
285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
286
287 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
288 if( ret != 0 )
289 return( ret );
290 }
291 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000292 {
293 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000294 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000295 continue;
296 }
297
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000298 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000299 }
300
301 if( match_found == 0 )
302 {
303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
304 return( MBEDTLS_ERR_SSL_HRR_REQUIRED );
305 }
306 return( 0 );
307}
XiaokangQian88408882022-04-02 10:15:03 +0000308#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000309
310#if defined(MBEDTLS_SSL_COOKIE_C)
311static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
312 const unsigned char *buf,
313 const unsigned char *end )
314{
315 int ret = 0;
316 size_t cookie_len;
317 unsigned char const *p = buf;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000318
319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse cookie extension" ) );
320
321 if( ssl->conf->f_cookie_check != NULL )
322 {
323 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
324 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
325 p += 2;
326
327 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
328
329 MBEDTLS_SSL_DEBUG_BUF( 3, "Received cookie", p, cookie_len );
330
331 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
332 p, cookie_len, ssl->cli_id,
333 ssl->cli_id_len ) != 0 )
334 {
335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000336 ret = MBEDTLS_ERR_SSL_HRR_REQUIRED;
337 }
338 else
339 {
340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000341 }
342 }
XiaokangQianc5763b52022-04-02 03:34:37 +0000343 else
344 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
346 }
347
348 return( ret );
349}
350#endif /* MBEDTLS_SSL_COOKIE_C */
351
352/*
353 *
354 * STATE HANDLING: ClientHello
355 *
356 * There are three possible classes of outcomes when parsing the CH:
357 *
358 * 1) The CH was well-formed and matched the server's configuration.
359 *
360 * In this case, the server progresses to sending its ServerHello.
361 *
362 * 2) The CH was well-formed but didn't match the server's configuration.
363 *
364 * For example, the client might not have offered a key share which
365 * the server supports, or the server might require a cookie.
366 *
367 * In this case, the server sends a HelloRetryRequest.
368 *
369 * 3) The CH was ill-formed
370 *
371 * In this case, we abort the handshake.
372 *
373 */
374
375/*
376 * Overview
377 */
378
379/* Main entry point from the state machine; orchestrates the otherfunctions. */
380static int ssl_client_hello_process( mbedtls_ssl_context *ssl );
381
382static int ssl_client_hello_parse( mbedtls_ssl_context *ssl,
383 const unsigned char *buf,
384 const unsigned char *end );
385
386/* Update the handshake state machine */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000387static int ssl_client_hello_postprocess( mbedtls_ssl_context *ssl,
388 int hrr_required );
389
390/*
391 * Implementation
392 */
393
394#define SSL_CLIENT_HELLO_OK 0
395#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
396
397static int ssl_client_hello_process( mbedtls_ssl_context *ssl )
398{
399
400 int ret = 0;
401 int hrr_required = SSL_CLIENT_HELLO_OK;
402 unsigned char* buf = NULL;
403 size_t buflen = 0;
404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
405
406 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
407 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
408 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
409 &buf, &buflen ) );
410
XiaokangQian7807f9f2022-02-15 10:04:37 +0000411 MBEDTLS_SSL_PROC_CHK_NEG( ssl_client_hello_parse( ssl, buf, buf + buflen ) );
412 hrr_required = ret;
413
414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
415 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess( ssl, hrr_required ) );
416
417cleanup:
418
419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
420 return( ret );
421}
422
423static void ssl_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
424{
XiaokangQian3207a322022-02-23 03:15:27 +0000425 ((void) ssl);
426
XiaokangQian7807f9f2022-02-15 10:04:37 +0000427 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
428 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- KEY_SHARE_EXTENSION ( %s )",
429 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ?
430 "TRUE" : "FALSE" ) );
431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
432 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
433 "TRUE" : "FALSE" ) );
434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
435 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ?
436 "TRUE" : "FALSE" ) );
437 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
438 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ?
439 "TRUE" : "FALSE" ) );
440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
441 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
442 "TRUE" : "FALSE" ) );
443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
444 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
445 "TRUE" : "FALSE" ) );
446#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
447 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SERVERNAME_EXTENSION ( %s )",
448 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
449 "TRUE" : "FALSE" ) );
450#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
451#if defined ( MBEDTLS_SSL_COOKIE_C )
452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- COOKIE_EXTENSION ( %s )",
453 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) >0 ) ?
454 "TRUE" : "FALSE" ) );
455#endif /* MBEDTLS_SSL_COOKIE_C */
456}
457
458static int ssl_client_hello_has_exts( mbedtls_ssl_context *ssl,
459 int ext_id_mask )
460{
461 int masked = ssl->handshake->extensions_present & ext_id_mask;
462 return( masked == ext_id_mask );
463}
464
465static int ssl_client_hello_has_cert_extensions( mbedtls_ssl_context *ssl )
466{
467 return( ssl_client_hello_has_exts( ssl,
468 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
469 MBEDTLS_SSL_EXT_KEY_SHARE |
470 MBEDTLS_SSL_EXT_SIG_ALG ) );
471}
472
473static int ssl_check_certificate_key_exchange( mbedtls_ssl_context *ssl )
474{
475 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
476 return( 0 );
477
478 if( !ssl_client_hello_has_cert_extensions( ssl ) )
479 return( 0 );
480
481 ssl->handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
482 return( 1 );
483}
484
485static int ssl_client_hello_parse( mbedtls_ssl_context *ssl,
486 const unsigned char *buf,
487 const unsigned char *end )
488{
489 int ret;
490 size_t i, j;
491 size_t comp_len, sess_len;
492 size_t cipher_suites_len;
493 size_t ext_len;
494 const unsigned char *ciph_offset;
495 const unsigned char *p = buf;
496 const unsigned char *extensions_end;
497
498 const int* ciphersuites;
499 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
500
501 int hrr_required = 0;
502
503 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
504
505 /*
506 * ClientHello layer:
507 * 0 . 1 protocol version
508 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
XiaokangQianc5763b52022-04-02 03:34:37 +0000509 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000510 * 35 . 34+x session id
511 * 35+x . 35+x DTLS only: cookie length ( 1 byte )
512 * 36+x . .. DTLS only: cookie
513 * .. . .. ciphersuite list length ( 2 bytes )
514 * .. . .. ciphersuite list
515 * .. . .. compression alg. list length ( 1 byte )
516 * .. . .. compression alg. list
517 * .. . .. extensions length ( 2 bytes, optional )
518 * .. . .. extensions ( optional )
519 */
520
XiaokangQianc5763b52022-04-02 03:34:37 +0000521 /* Needs to be updated due to mandatory extensions
XiaokangQian7807f9f2022-02-15 10:04:37 +0000522 * Minimal length ( with everything empty and extensions ommitted ) is
523 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
524 * read at least up to session id length without worrying.
525 */
526 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
527
528 /* ...
529 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
530 * ...
531 * with ProtocolVersion defined as:
532 * uint16 ProtocolVersion;
533 */
534 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
535 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
536 {
537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
538 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
539 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
540 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
541 return ret;
542 }
543 p += 2;
544
545 /*
546 * Save client random
547 */
548 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
549
550 memcpy( &ssl->handshake->randbytes[0], p, 32 );
XiaokangQianc5763b52022-04-02 03:34:37 +0000551 /* skip random bytes */
552 p += 32;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000553
554 /*
555 * Parse session ID
556 */
557 sess_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000558 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000559
560 if( sess_len > 32 )
561 {
562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
563 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
564 }
565
566 ssl->session_negotiate->id_len = sess_len;
567
568 /* Note that this field is echoed even if
569 * the client's value corresponded to a cached pre-TLS 1.3 session
570 * which the server has chosen not to resume. A client which
571 * receives a legacy_session_id_echo field that does not match what
572 * it sent in the ClientHello MUST abort the handshake with an
573 * "illegal_parameter" alert.
574 */
575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id length ( %" MBEDTLS_PRINTF_SIZET " )", sess_len ) );
576 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf, sess_len );
577
578 memcpy( &ssl->session_negotiate->id[0], p, sess_len ); /* write session id */
579 p += sess_len;
580
581 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
582 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
583 p += 2;
584
585 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
586
587 /* store pointer to ciphersuite list */
588 ciph_offset = p;
589
590 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
591 p, cipher_suites_len );
592
593 /* skip ciphersuites for now */
594 p += cipher_suites_len;
595
596 /*
597 * For TLS 1.3 we are not using compression.
598 */
XiaokangQiana9c58412022-02-17 09:41:26 +0000599 comp_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000600 p++;
601 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, comp_len );
602
603 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
604 p, comp_len );
605
606 /* Determine whether we are indeed using null compression */
XiaokangQiana9c58412022-02-17 09:41:26 +0000607 if( ( comp_len != 1 ) && ( p[0] == 0 ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000608 {
609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
610 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
611 }
612
613 /* skip compression */
614 p++;
615
616 /*
617 * Check the extension length
618 */
619 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
620
621 ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
622 p += 2;
623 extensions_end = p + ext_len;
624 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ext_len );
625
626 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, ext_len );
627
628 while( p < extensions_end )
629 {
630 unsigned int extension_type;
631 size_t extension_data_len;
632 const unsigned char *extension_data_end;
633
634 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
635 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
636 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
637 p += 4;
638
639 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
640 extension_data_end = p + extension_data_len;
641
642 switch( extension_type )
643 {
644#if defined(MBEDTLS_SSL_COOKIE_C)
645 case MBEDTLS_TLS_EXT_COOKIE:
646 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found cookie extension" ) );
647
648 ret = ssl_tls13_parse_cookie_ext( ssl, p,
649 extension_data_end );
650
651 /* if cookie verification failed then we return a hello retry
652 * message, or return success and set cookie extension present
653 */
654 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
655 {
656 hrr_required = 1;
657 }
658 else if( ret == 0 )
659 {
660 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_COOKIE;
661 }
662 break;
663#endif /* MBEDTLS_SSL_COOKIE_C */
664
665#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
666 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
668
669 /* Supported Groups Extension
670 *
671 * When sent by the client, the "supported_groups" extension
672 * indicates the named groups which the client supports,
673 * ordered from most preferred to least preferred.
674 */
675 ret = mbedtls_ssl_tls13_parse_supported_groups_ext( ssl, p,
676 extension_data_end );
677 if( ret != 0 )
678 {
679 MBEDTLS_SSL_DEBUG_RET( 1,
680 "mbedtls_ssl_parse_supported_groups_ext", ret );
681 return( ret );
682 }
683
684 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
685 break;
686#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
687
XiaokangQian88408882022-04-02 10:15:03 +0000688#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000689 case MBEDTLS_TLS_EXT_KEY_SHARE:
690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
691
692 /*
693 * Key Share Extension
694 *
695 * When sent by the client, the "key_share" extension
696 * contains the endpoint's cryptographic parameters for
697 * ECDHE/DHE key establishment methods.
698 */
699 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
700 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
701 {
702 hrr_required = 1;
703 ret = 0;
704 }
705
706 if( ret != 0 )
707 return( ret );
708
709 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
710 break;
XiaokangQian88408882022-04-02 10:15:03 +0000711#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000712
713 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
715
716 ret = ssl_tls13_parse_supported_versions_ext(
717 ssl, p, extension_data_end );
718 if( ret != 0 )
719 {
720 MBEDTLS_SSL_DEBUG_RET( 1,
721 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
722 return( ret );
723 }
724 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
725 break;
726
727#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
728 case MBEDTLS_TLS_EXT_SIG_ALG:
729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
730
731 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
732 extension_data_end );
733 if( ret != 0 )
734 {
735 MBEDTLS_SSL_DEBUG_MSG( 1,
736 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
737 ret ) );
738 return( ret );
739 }
740 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
741 break;
742#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
743
744 default:
745 MBEDTLS_SSL_DEBUG_MSG( 3,
746 ( "unknown extension found: %ud ( ignoring )",
747 extension_type ) );
748 }
749
750 p += extension_data_len;
751 }
752
753 /* Update checksum with either
754 * - The entire content of the CH message, if no PSK extension is present
755 * - The content up to but excluding the PSK extension, if present.
756 */
XiaokangQianc4b8c992022-04-07 11:31:38 +0000757 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
758 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 /*
760 * Search for a matching ciphersuite
761 */
762 ciphersuites = ssl->conf->ciphersuite_list;
763 ciphersuite_info = NULL;
764 for ( j = 0, p = ciph_offset; j < cipher_suites_len; j += 2, p += 2 )
765 {
766 for ( i = 0; ciphersuites[i] != 0; i++ )
767 {
768 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
769 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
770 continue;
771
772 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
773 ciphersuites[i] );
774
775 if( ciphersuite_info == NULL )
776 {
777 MBEDTLS_SSL_DEBUG_MSG(
778 1,
779 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
781 }
782
783 goto have_ciphersuite;
784
785 }
786 }
787
788 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
789
790have_ciphersuite:
791
792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
793 ciphersuite_info->name ) );
794
795 ssl->session_negotiate->ciphersuite = ciphersuites[i];
796 ssl->handshake->ciphersuite_info = ciphersuite_info;
797
798 /* List all the extensions we have received */
799 ssl_debug_print_client_hello_exts( ssl );
800
801 /*
802 * Determine the key exchange algorithm to use.
803 * There are three types of key exchanges supported in TLS 1.3:
804 * - (EC)DH with ECDSA,
805 * - (EC)DH with PSK,
806 * - plain PSK.
807 *
808 * The PSK-based key exchanges may additionally be used with 0-RTT.
809 *
810 * Our built-in order of preference is
811 * 1 ) Plain PSK Mode
812 * 2 ) (EC)DHE-PSK Mode
813 * 3 ) Certificate Mode
814 */
815
816 if( !ssl_check_certificate_key_exchange( ssl ) )
817 {
818 MBEDTLS_SSL_DEBUG_MSG(
819 1,
820 ( "ClientHello message misses mandatory extensions." ) );
821 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
822 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
823 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
824 }
825
826#if defined(MBEDTLS_SSL_COOKIE_C)
827 /* If we failed to see a cookie extension, and we required it through the
828 * configuration settings ( rr_config ), then we need to send a HRR msg.
829 * Conceptually, this is similiar to having received a cookie that failed
830 * the verification check.
831 */
832 if( ( ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON ) &&
833 !( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) )
834 {
835 MBEDTLS_SSL_DEBUG_MSG(
836 2,
837 ( "Cookie extension missing. Need to send a HRR." ) );
838 hrr_required = 1;
839 }
840#endif /* MBEDTLS_SSL_COOKIE_C */
841
842 if( hrr_required == 1 )
843 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
844
845 return( 0 );
846}
847
848static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl,
849 int hrr_required )
850{
851 int ret = 0;
852
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000853 if( ssl->handshake->hello_retry_requests_sent == 0 &&
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854 ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON )
855 {
856 hrr_required = SSL_CLIENT_HELLO_HRR_REQUIRED;
857 }
858
859 if( hrr_required == SSL_CLIENT_HELLO_HRR_REQUIRED )
860 {
861 /*
862 * Create stateless transcript hash for HRR
863 */
864 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
865 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
866 if( ret != 0 )
867 {
868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr",
869 ret );
870 return( ret );
871 }
872 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
873
874 /* Transmit Hello Retry Request */
875 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
876 return( 0 );
877 }
878
879 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
880 if( ret != 0 )
881 {
882 MBEDTLS_SSL_DEBUG_RET( 1,
883 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
884 return( ret );
885 }
886
887 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
888 return( 0 );
889
890}
891
892/*
893 * TLS and DTLS 1.3 State Maschine -- server side
894 */
Jerry Yu27561932021-08-27 17:07:38 +0800895int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800896{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897 int ret = 0;
898
899 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
901
Jerry Yue3b34122021-09-28 17:53:35 +0800902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
903 mbedtls_ssl_states_str( ssl->state ),
904 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800905
XiaokangQian7807f9f2022-02-15 10:04:37 +0000906 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
907 return( ret );
908
909 switch( ssl->state )
910 {
911 /* start state */
912 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000913 ssl->handshake->hello_retry_requests_sent = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000914 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
915
916 break;
917
918 /* ----- READ CLIENT HELLO ----*/
919
920 case MBEDTLS_SSL_CLIENT_HELLO:
921
922 ret = ssl_client_hello_process( ssl );
923 if( ret != 0 )
924 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_client_hello_process", ret );
925
926 break;
927
928 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
930
931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for all traffic" ) );
932
933 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
934 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
935
936 mbedtls_ssl_tls13_handshake_wrapup( ssl );
937 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET );
938
939 break;
940
941 default:
942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
944 }
945
946 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800947}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800948
Jerry Yufb4b6472022-01-27 15:03:26 +0800949#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */