blob: 934737a85334b341af4bcbc9fc547b2df9b9ddb6 [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"
32#include "ecp_internal.h"
33#endif /* MBEDTLS_ECP_C */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080034
XiaokangQiana9c58412022-02-17 09:41:26 +000035#if defined(MBEDTLS_PLATFORM_C)
36#include "mbedtls/platform.h"
37#else
38#include <stdlib.h>
39#define mbedtls_calloc calloc
40#define mbedtls_free free
41#endif /* MBEDTLS_PLATFORM_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +000042
43/* From RFC 8446:
44 * struct {
45 * select (Handshake.msg_type) {
46 * case client_hello:
47 * ProtocolVersion versions<2..254>;
48 * case server_hello: // and HelloRetryRequest
49 * ProtocolVersion selected_version;
50 * };
51 * } SupportedVersions;
52 */
53static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
54 const unsigned char *buf,
55 const unsigned char *end )
56{
57 size_t list_len;
58 int tls13_supported = 0;
59 int major_ver, minor_ver;
60 const unsigned char *p = buf;
61 const unsigned char *version_end;
62
63 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
64
65 list_len = p[0];
66 p += 1;
67
68 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
69 if( list_len % 2 != 0 )
70 {
71 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid supported version list length %" MBEDTLS_PRINTF_SIZET,
72 list_len ) );
73 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
74 }
75
76 version_end = p + list_len;
77 while( p < version_end )
78 {
79 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
80
81 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
82 if( major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
83 minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
84 {
85 tls13_supported = 1;
86 break;
87 }
88
89 p += 2;
90 }
91
92 if( tls13_supported == 0 )
93 {
94 /* When we support runtime negotiation of TLS 1.2 and TLS 1.3, we need
XiaokangQianc5763b52022-04-02 03:34:37 +000095 * a graceful fallback to TLS 1.2 in this case.
96 */
XiaokangQian7807f9f2022-02-15 10:04:37 +000097
98 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
99
100 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
101 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
102 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
103 }
104
105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%d:%d]",
106 major_ver, minor_ver ) );
107
108 ssl->major_ver = major_ver;
109 ssl->minor_ver = minor_ver;
110 ssl->handshake->max_major_ver = ssl->major_ver;
111 ssl->handshake->max_minor_ver = ssl->minor_ver;
112 return( 0 );
113}
114
115#if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
116/* This function parses the TLS 1.3 supported_groups extension and
117 * stores the received groups in ssl->handshake->curves.
118 *
119 * From RFC 8446:
120 * enum {
121 * ... (0xFFFF)
122 * } NamedGroup;
123 * struct {
124 * NamedGroup named_group_list<2..2^16-1>;
125 * } NamedGroupList;
126 */
127static int mbedtls_ssl_tls13_parse_supported_groups_ext(
128 mbedtls_ssl_context *ssl,
129 const unsigned char *buf, const unsigned char *end )
130{
131
132 size_t list_size, our_size;
133 const unsigned char *p = buf;
134 const mbedtls_ecp_curve_info *curve_info, **curves;
135 const unsigned char *extentions_end;
136
137 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
138 list_size = MBEDTLS_GET_UINT16_BE( p, 0 );
139 p += 2;
140 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_size );
141 if( list_size % 2 != 0 )
142 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
143
XiaokangQianc5763b52022-04-02 03:34:37 +0000144 /* At the moment, this can happen when receiving a second
XiaokangQian7807f9f2022-02-15 10:04:37 +0000145 * ClientHello after an HRR. We should properly reset the
146 * state upon receiving an HRR, in which case we should
147 * not observe handshake->curves already being allocated. */
148 if( ssl->handshake->curves != NULL )
149 {
XiaokangQian88408882022-04-02 10:15:03 +0000150 //mbedtls_free( ssl->handshake->curves );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000151 ssl->handshake->curves = NULL;
152 }
153
154 /* Don't allow our peer to make us allocate too much memory,
XiaokangQianc5763b52022-04-02 03:34:37 +0000155 * and leave room for a final 0
156 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000157 our_size = list_size / 2 + 1;
158 if( our_size > MBEDTLS_ECP_DP_MAX )
159 our_size = MBEDTLS_ECP_DP_MAX;
160
161 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
162 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
163
164 extentions_end = p + list_size;
165 ssl->handshake->curves = curves;
166
167 while ( p < extentions_end && our_size > 1 )
168 {
169 uint16_t tls_grp_id = MBEDTLS_GET_UINT16_BE( p, 0 );
170 curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_grp_id );
171
172 /* mbedtls_ecp_curve_info_from_tls_id() uses the mbedtls_ecp_curve_info
173 * data structure (defined in ecp.c), which only includes the list of
174 * curves implemented. Hence, we only add curves that are also supported
XiaokangQianc5763b52022-04-02 03:34:37 +0000175 * and implemented by the server.
176 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000177 if( curve_info != NULL )
178 {
179 *curves++ = curve_info;
180 MBEDTLS_SSL_DEBUG_MSG( 4, ( "supported curve: %s", curve_info->name ) );
181 our_size--;
182 }
183
184 p += 2;
185 }
186
187 return( 0 );
188
189}
190#endif /* MBEDTLS_ECDH_C || ( MBEDTLS_ECDSA_C */
191
XiaokangQian88408882022-04-02 10:15:03 +0000192#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000193/*
194 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
195 * extension is correct and stores the provided key shares. Whether this is an
196 * acceptable key share depends on the selected ciphersuite.
197 *
198 * Possible return values are:
199 * - 0: Successful processing of the client provided key share extension.
200 * - MBEDTLS_ERR_SSL_HRR_REQUIRED: The key share provided by the client
201 * does not match a group supported by the server. A HelloRetryRequest will
202 * be needed.
203 * - Another negative return value for fatal errors.
204*/
205
206static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
207 const unsigned char *buf,
208 const unsigned char *end )
209{
210 int ret = 0;
211 unsigned char const *p = buf;
212 unsigned char const *extentions_end;
213
214 size_t total_ext_len, cur_share_len;
215 int match_found = 0;
216
217 /* From RFC 8446:
218 *
219 * struct {
220 * KeyShareEntry client_shares<0..2^16-1>;
221 * } KeyShareClientHello;
222 *
223 */
224
225 /* Read total legnth of KeyShareClientHello */
226 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
227
228 total_ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
229 p += 2;
230 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, total_ext_len );
231
232 ssl->handshake->offered_group_id = 0;
233 extentions_end = p + total_ext_len;
234
235 /* We try to find a suitable key share entry and copy it to the
236 * handshake context. Later, we have to find out whether we can do
237 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000238 * dismiss it and send a HelloRetryRequest message.
239 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000240
241 for( ; p < extentions_end; p += cur_share_len )
242 {
243 uint16_t their_group;
244 mbedtls_ecp_group_id their_curve;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000245 unsigned char const *end_of_share;
246
247 /*
248 * struct {
249 * NamedGroup group;
250 * opaque key_exchange<1..2^16-1>;
251 * } KeyShareEntry;
252 */
253 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extentions_end, 4 );
254
255 their_group = MBEDTLS_GET_UINT16_BE( p, 0 );
256 p += 2;
257
258 cur_share_len = MBEDTLS_GET_UINT16_BE( p, 0 );
259 p += 2;
260
261 end_of_share = p + cur_share_len;
262
263 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000264 * for input validation purposes.
265 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000266 if( match_found == 1 )
267 continue;
268
269 /*
270 * NamedGroup matching
271 *
272 * For now, we only support ECDHE groups, but e.g.
273 * PQC KEMs will need to be added at a later stage.
274 */
275
276 /* Type 1: ECDHE shares
277 *
278 * - Check if we recognize the group
279 * - Check if it's supported
280 */
281
XiaokangQian3207a322022-02-23 03:15:27 +0000282 their_curve = mbedtls_ecp_named_group_to_id( their_group );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000283 if( mbedtls_ssl_check_curve( ssl, their_curve ) != 0 )
284 continue;
285
XiaokangQian7807f9f2022-02-15 10:04:37 +0000286 /* Skip if we no match succeeded. */
287 if( their_curve == MBEDTLS_ECP_DP_NONE )
288 {
289 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
290 (unsigned) their_group ) );
291 continue;
292 }
293
294 match_found = 1;
295
296 /* KeyShare parsing
297 *
298 * Once we add more key share types, this needs to be a switch
XiaokangQianc5763b52022-04-02 03:34:37 +0000299 * over the (type of) the named curve
300 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000301
302 /* Type 1: ECDHE shares
303 *
304 * - Setup ECDHE context
305 * - Import client's public key
306 * - Apply further curve checks
307 */
308
XiaokangQian88408882022-04-02 10:15:03 +0000309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %ud", their_curve ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000310
311 ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx, their_curve );
312 if( ret != 0 )
313 {
314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup()", ret );
315 return( ret );
316 }
317
318 ret = mbedtls_ecdh_import_public_raw( &ssl->handshake->ecdh_ctx,
319 p, end_of_share );
320 if( ret != 0 )
321 {
322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_import_public_raw()", ret );
323 return( ret );
324 }
325
326 ssl->handshake->offered_group_id = their_group;
327 }
328
329 if( match_found == 0 )
330 {
331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
332 return( MBEDTLS_ERR_SSL_HRR_REQUIRED );
333 }
334 return( 0 );
335}
XiaokangQian88408882022-04-02 10:15:03 +0000336#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000337
338#if defined(MBEDTLS_SSL_COOKIE_C)
339static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
340 const unsigned char *buf,
341 const unsigned char *end )
342{
343 int ret = 0;
344 size_t cookie_len;
345 unsigned char const *p = buf;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000346
347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse cookie extension" ) );
348
349 if( ssl->conf->f_cookie_check != NULL )
350 {
351 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
352 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
353 p += 2;
354
355 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
356
357 MBEDTLS_SSL_DEBUG_BUF( 3, "Received cookie", p, cookie_len );
358
359 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
360 p, cookie_len, ssl->cli_id,
361 ssl->cli_id_len ) != 0 )
362 {
363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000364 ret = MBEDTLS_ERR_SSL_HRR_REQUIRED;
365 }
366 else
367 {
368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000369 }
370 }
XiaokangQianc5763b52022-04-02 03:34:37 +0000371 else
372 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
374 }
375
376 return( ret );
377}
378#endif /* MBEDTLS_SSL_COOKIE_C */
379
380/*
381 *
382 * STATE HANDLING: ClientHello
383 *
384 * There are three possible classes of outcomes when parsing the CH:
385 *
386 * 1) The CH was well-formed and matched the server's configuration.
387 *
388 * In this case, the server progresses to sending its ServerHello.
389 *
390 * 2) The CH was well-formed but didn't match the server's configuration.
391 *
392 * For example, the client might not have offered a key share which
393 * the server supports, or the server might require a cookie.
394 *
395 * In this case, the server sends a HelloRetryRequest.
396 *
397 * 3) The CH was ill-formed
398 *
399 * In this case, we abort the handshake.
400 *
401 */
402
403/*
404 * Overview
405 */
406
407/* Main entry point from the state machine; orchestrates the otherfunctions. */
408static int ssl_client_hello_process( mbedtls_ssl_context *ssl );
409
410static int ssl_client_hello_parse( mbedtls_ssl_context *ssl,
411 const unsigned char *buf,
412 const unsigned char *end );
413
414/* Update the handshake state machine */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000415static int ssl_client_hello_postprocess( mbedtls_ssl_context *ssl,
416 int hrr_required );
417
418/*
419 * Implementation
420 */
421
422#define SSL_CLIENT_HELLO_OK 0
423#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
424
425static int ssl_client_hello_process( mbedtls_ssl_context *ssl )
426{
427
428 int ret = 0;
429 int hrr_required = SSL_CLIENT_HELLO_OK;
430 unsigned char* buf = NULL;
431 size_t buflen = 0;
432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
433
434 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
435 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
436 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
437 &buf, &buflen ) );
438
439 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
440 MBEDTLS_SSL_HS_CLIENT_HELLO,
441 buflen );
442
443 MBEDTLS_SSL_PROC_CHK_NEG( ssl_client_hello_parse( ssl, buf, buf + buflen ) );
444 hrr_required = ret;
445
446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
447 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess( ssl, hrr_required ) );
448
449cleanup:
450
451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
452 return( ret );
453}
454
455static void ssl_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
456{
XiaokangQian3207a322022-02-23 03:15:27 +0000457 ((void) ssl);
458
XiaokangQian7807f9f2022-02-15 10:04:37 +0000459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- KEY_SHARE_EXTENSION ( %s )",
461 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ?
462 "TRUE" : "FALSE" ) );
463 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
464 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
465 "TRUE" : "FALSE" ) );
466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
467 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ?
468 "TRUE" : "FALSE" ) );
469 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
470 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ?
471 "TRUE" : "FALSE" ) );
472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
473 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
474 "TRUE" : "FALSE" ) );
475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
476 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
477 "TRUE" : "FALSE" ) );
478#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SERVERNAME_EXTENSION ( %s )",
480 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
481 "TRUE" : "FALSE" ) );
482#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
483#if defined ( MBEDTLS_SSL_COOKIE_C )
484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- COOKIE_EXTENSION ( %s )",
485 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) >0 ) ?
486 "TRUE" : "FALSE" ) );
487#endif /* MBEDTLS_SSL_COOKIE_C */
488}
489
490static int ssl_client_hello_has_exts( mbedtls_ssl_context *ssl,
491 int ext_id_mask )
492{
493 int masked = ssl->handshake->extensions_present & ext_id_mask;
494 return( masked == ext_id_mask );
495}
496
497static int ssl_client_hello_has_cert_extensions( mbedtls_ssl_context *ssl )
498{
499 return( ssl_client_hello_has_exts( ssl,
500 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
501 MBEDTLS_SSL_EXT_KEY_SHARE |
502 MBEDTLS_SSL_EXT_SIG_ALG ) );
503}
504
505static int ssl_check_certificate_key_exchange( mbedtls_ssl_context *ssl )
506{
507 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
508 return( 0 );
509
510 if( !ssl_client_hello_has_cert_extensions( ssl ) )
511 return( 0 );
512
513 ssl->handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
514 return( 1 );
515}
516
517static int ssl_client_hello_parse( mbedtls_ssl_context *ssl,
518 const unsigned char *buf,
519 const unsigned char *end )
520{
521 int ret;
522 size_t i, j;
523 size_t comp_len, sess_len;
524 size_t cipher_suites_len;
525 size_t ext_len;
526 const unsigned char *ciph_offset;
527 const unsigned char *p = buf;
528 const unsigned char *extensions_end;
529
530 const int* ciphersuites;
531 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
532
533 int hrr_required = 0;
534
535 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
536
537 /*
538 * ClientHello layer:
539 * 0 . 1 protocol version
540 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
XiaokangQianc5763b52022-04-02 03:34:37 +0000541 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000542 * 35 . 34+x session id
543 * 35+x . 35+x DTLS only: cookie length ( 1 byte )
544 * 36+x . .. DTLS only: cookie
545 * .. . .. ciphersuite list length ( 2 bytes )
546 * .. . .. ciphersuite list
547 * .. . .. compression alg. list length ( 1 byte )
548 * .. . .. compression alg. list
549 * .. . .. extensions length ( 2 bytes, optional )
550 * .. . .. extensions ( optional )
551 */
552
XiaokangQianc5763b52022-04-02 03:34:37 +0000553 /* Needs to be updated due to mandatory extensions
XiaokangQian7807f9f2022-02-15 10:04:37 +0000554 * Minimal length ( with everything empty and extensions ommitted ) is
555 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
556 * read at least up to session id length without worrying.
557 */
558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
559
560 /* ...
561 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
562 * ...
563 * with ProtocolVersion defined as:
564 * uint16 ProtocolVersion;
565 */
566 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
567 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
568 {
569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
570 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
571 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
572 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
573 return ret;
574 }
575 p += 2;
576
577 /*
578 * Save client random
579 */
580 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
581
582 memcpy( &ssl->handshake->randbytes[0], p, 32 );
XiaokangQianc5763b52022-04-02 03:34:37 +0000583 /* skip random bytes */
584 p += 32;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000585
586 /*
587 * Parse session ID
588 */
589 sess_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000590 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000591
592 if( sess_len > 32 )
593 {
594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
595 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
596 }
597
598 ssl->session_negotiate->id_len = sess_len;
599
600 /* Note that this field is echoed even if
601 * the client's value corresponded to a cached pre-TLS 1.3 session
602 * which the server has chosen not to resume. A client which
603 * receives a legacy_session_id_echo field that does not match what
604 * it sent in the ClientHello MUST abort the handshake with an
605 * "illegal_parameter" alert.
606 */
607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id length ( %" MBEDTLS_PRINTF_SIZET " )", sess_len ) );
608 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf, sess_len );
609
610 memcpy( &ssl->session_negotiate->id[0], p, sess_len ); /* write session id */
611 p += sess_len;
612
613 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
614 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
615 p += 2;
616
617 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
618
619 /* store pointer to ciphersuite list */
620 ciph_offset = p;
621
622 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
623 p, cipher_suites_len );
624
625 /* skip ciphersuites for now */
626 p += cipher_suites_len;
627
628 /*
629 * For TLS 1.3 we are not using compression.
630 */
XiaokangQiana9c58412022-02-17 09:41:26 +0000631 comp_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000632 p++;
633 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, comp_len );
634
635 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
636 p, comp_len );
637
638 /* Determine whether we are indeed using null compression */
XiaokangQiana9c58412022-02-17 09:41:26 +0000639 if( ( comp_len != 1 ) && ( p[0] == 0 ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000640 {
641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
642 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
643 }
644
645 /* skip compression */
646 p++;
647
648 /*
649 * Check the extension length
650 */
651 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
652
653 ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
654 p += 2;
655 extensions_end = p + ext_len;
656 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ext_len );
657
658 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, ext_len );
659
660 while( p < extensions_end )
661 {
662 unsigned int extension_type;
663 size_t extension_data_len;
664 const unsigned char *extension_data_end;
665
666 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
667 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
668 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
669 p += 4;
670
671 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
672 extension_data_end = p + extension_data_len;
673
674 switch( extension_type )
675 {
676#if defined(MBEDTLS_SSL_COOKIE_C)
677 case MBEDTLS_TLS_EXT_COOKIE:
678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found cookie extension" ) );
679
680 ret = ssl_tls13_parse_cookie_ext( ssl, p,
681 extension_data_end );
682
683 /* if cookie verification failed then we return a hello retry
684 * message, or return success and set cookie extension present
685 */
686 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
687 {
688 hrr_required = 1;
689 }
690 else if( ret == 0 )
691 {
692 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_COOKIE;
693 }
694 break;
695#endif /* MBEDTLS_SSL_COOKIE_C */
696
697#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
698 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
700
701 /* Supported Groups Extension
702 *
703 * When sent by the client, the "supported_groups" extension
704 * indicates the named groups which the client supports,
705 * ordered from most preferred to least preferred.
706 */
707 ret = mbedtls_ssl_tls13_parse_supported_groups_ext( ssl, p,
708 extension_data_end );
709 if( ret != 0 )
710 {
711 MBEDTLS_SSL_DEBUG_RET( 1,
712 "mbedtls_ssl_parse_supported_groups_ext", ret );
713 return( ret );
714 }
715
716 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
717 break;
718#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
719
XiaokangQian88408882022-04-02 10:15:03 +0000720#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000721 case MBEDTLS_TLS_EXT_KEY_SHARE:
722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
723
724 /*
725 * Key Share Extension
726 *
727 * When sent by the client, the "key_share" extension
728 * contains the endpoint's cryptographic parameters for
729 * ECDHE/DHE key establishment methods.
730 */
731 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
732 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
733 {
734 hrr_required = 1;
735 ret = 0;
736 }
737
738 if( ret != 0 )
739 return( ret );
740
741 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
742 break;
XiaokangQian88408882022-04-02 10:15:03 +0000743#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000744
745 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
747
748 ret = ssl_tls13_parse_supported_versions_ext(
749 ssl, p, extension_data_end );
750 if( ret != 0 )
751 {
752 MBEDTLS_SSL_DEBUG_RET( 1,
753 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
754 return( ret );
755 }
756 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
757 break;
758
759#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
760 case MBEDTLS_TLS_EXT_SIG_ALG:
761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
762
763 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
764 extension_data_end );
765 if( ret != 0 )
766 {
767 MBEDTLS_SSL_DEBUG_MSG( 1,
768 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
769 ret ) );
770 return( ret );
771 }
772 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
773 break;
774#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
775
776 default:
777 MBEDTLS_SSL_DEBUG_MSG( 3,
778 ( "unknown extension found: %ud ( ignoring )",
779 extension_type ) );
780 }
781
782 p += extension_data_len;
783 }
784
785 /* Update checksum with either
786 * - The entire content of the CH message, if no PSK extension is present
787 * - The content up to but excluding the PSK extension, if present.
788 */
789 ssl->handshake->update_checksum( ssl, buf, p - buf );
790 /*
791 * Search for a matching ciphersuite
792 */
793 ciphersuites = ssl->conf->ciphersuite_list;
794 ciphersuite_info = NULL;
795 for ( j = 0, p = ciph_offset; j < cipher_suites_len; j += 2, p += 2 )
796 {
797 for ( i = 0; ciphersuites[i] != 0; i++ )
798 {
799 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
800 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
801 continue;
802
803 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
804 ciphersuites[i] );
805
806 if( ciphersuite_info == NULL )
807 {
808 MBEDTLS_SSL_DEBUG_MSG(
809 1,
810 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
811 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
812 }
813
814 goto have_ciphersuite;
815
816 }
817 }
818
819 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
820
821have_ciphersuite:
822
823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
824 ciphersuite_info->name ) );
825
826 ssl->session_negotiate->ciphersuite = ciphersuites[i];
827 ssl->handshake->ciphersuite_info = ciphersuite_info;
828
829 /* List all the extensions we have received */
830 ssl_debug_print_client_hello_exts( ssl );
831
832 /*
833 * Determine the key exchange algorithm to use.
834 * There are three types of key exchanges supported in TLS 1.3:
835 * - (EC)DH with ECDSA,
836 * - (EC)DH with PSK,
837 * - plain PSK.
838 *
839 * The PSK-based key exchanges may additionally be used with 0-RTT.
840 *
841 * Our built-in order of preference is
842 * 1 ) Plain PSK Mode
843 * 2 ) (EC)DHE-PSK Mode
844 * 3 ) Certificate Mode
845 */
846
847 if( !ssl_check_certificate_key_exchange( ssl ) )
848 {
849 MBEDTLS_SSL_DEBUG_MSG(
850 1,
851 ( "ClientHello message misses mandatory extensions." ) );
852 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
853 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
854 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
855 }
856
857#if defined(MBEDTLS_SSL_COOKIE_C)
858 /* If we failed to see a cookie extension, and we required it through the
859 * configuration settings ( rr_config ), then we need to send a HRR msg.
860 * Conceptually, this is similiar to having received a cookie that failed
861 * the verification check.
862 */
863 if( ( ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON ) &&
864 !( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) )
865 {
866 MBEDTLS_SSL_DEBUG_MSG(
867 2,
868 ( "Cookie extension missing. Need to send a HRR." ) );
869 hrr_required = 1;
870 }
871#endif /* MBEDTLS_SSL_COOKIE_C */
872
873 if( hrr_required == 1 )
874 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
875
876 return( 0 );
877}
878
879static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl,
880 int hrr_required )
881{
882 int ret = 0;
883
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000884 if( ssl->handshake->hello_retry_requests_sent == 0 &&
XiaokangQian7807f9f2022-02-15 10:04:37 +0000885 ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON )
886 {
887 hrr_required = SSL_CLIENT_HELLO_HRR_REQUIRED;
888 }
889
890 if( hrr_required == SSL_CLIENT_HELLO_HRR_REQUIRED )
891 {
892 /*
893 * Create stateless transcript hash for HRR
894 */
895 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
896 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
897 if( ret != 0 )
898 {
899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr",
900 ret );
901 return( ret );
902 }
903 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
904
905 /* Transmit Hello Retry Request */
906 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
907 return( 0 );
908 }
909
910 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
911 if( ret != 0 )
912 {
913 MBEDTLS_SSL_DEBUG_RET( 1,
914 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
915 return( ret );
916 }
917
918 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
919 return( 0 );
920
921}
922
923/*
924 * TLS and DTLS 1.3 State Maschine -- server side
925 */
Jerry Yu27561932021-08-27 17:07:38 +0800926int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800927{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000928 int ret = 0;
929
930 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
932
Jerry Yue3b34122021-09-28 17:53:35 +0800933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
934 mbedtls_ssl_states_str( ssl->state ),
935 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800936
XiaokangQian7807f9f2022-02-15 10:04:37 +0000937 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
938 return( ret );
939
940 switch( ssl->state )
941 {
942 /* start state */
943 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000944 ssl->handshake->hello_retry_requests_sent = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000945 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
946
947 break;
948
949 /* ----- READ CLIENT HELLO ----*/
950
951 case MBEDTLS_SSL_CLIENT_HELLO:
952
953 ret = ssl_client_hello_process( ssl );
954 if( ret != 0 )
955 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_client_hello_process", ret );
956
957 break;
958
959 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
961
962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for all traffic" ) );
963
964 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
965 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
966
967 mbedtls_ssl_tls13_handshake_wrapup( ssl );
968 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET );
969
970 break;
971
972 default:
973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
975 }
976
977 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800978}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800979
Jerry Yufb4b6472022-01-27 15:03:26 +0800980#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */