blob: 4e455832068574c49331fbbd18439f035770bdde [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"
XiaokangQian7807f9f2022-02-15 10:04:37 +000025#include "mbedtls/platform.h"
Jerry Yu687101b2021-09-14 16:03:56 +080026
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080027#include "ssl_misc.h"
XiaokangQian7807f9f2022-02-15 10:04:37 +000028#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010029#include "ssl_debug_helpers.h"
XiaokangQian7807f9f2022-02-15 10:04:37 +000030#include <string.h>
31#if defined(MBEDTLS_ECP_C)
32#include "mbedtls/ecp.h"
33#include "ecp_internal.h"
34#endif /* MBEDTLS_ECP_C */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080035
XiaokangQian7807f9f2022-02-15 10:04:37 +000036#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
37
38/* From RFC 8446:
39 * struct {
40 * select (Handshake.msg_type) {
41 * case client_hello:
42 * ProtocolVersion versions<2..254>;
43 * case server_hello: // and HelloRetryRequest
44 * ProtocolVersion selected_version;
45 * };
46 * } SupportedVersions;
47 */
48static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
49 const unsigned char *buf,
50 const unsigned char *end )
51{
52 size_t list_len;
53 int tls13_supported = 0;
54 int major_ver, minor_ver;
55 const unsigned char *p = buf;
56 const unsigned char *version_end;
57
58 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
59
60 list_len = p[0];
61 p += 1;
62
63 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
64 if( list_len % 2 != 0 )
65 {
66 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid supported version list length %" MBEDTLS_PRINTF_SIZET,
67 list_len ) );
68 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
69 }
70
71 version_end = p + list_len;
72 while( p < version_end )
73 {
74 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
75
76 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
77 if( major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
78 minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
79 {
80 tls13_supported = 1;
81 break;
82 }
83
84 p += 2;
85 }
86
87 if( tls13_supported == 0 )
88 {
89 /* When we support runtime negotiation of TLS 1.2 and TLS 1.3, we need
90 * a graceful fallback to TLS 1.2 in this case. */
91
92 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
93
94 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
95 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
96 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
97 }
98
99 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%d:%d]",
100 major_ver, minor_ver ) );
101
102 ssl->major_ver = major_ver;
103 ssl->minor_ver = minor_ver;
104 ssl->handshake->max_major_ver = ssl->major_ver;
105 ssl->handshake->max_minor_ver = ssl->minor_ver;
106 return( 0 );
107}
108
109#if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
110/* This function parses the TLS 1.3 supported_groups extension and
111 * stores the received groups in ssl->handshake->curves.
112 *
113 * From RFC 8446:
114 * enum {
115 * ... (0xFFFF)
116 * } NamedGroup;
117 * struct {
118 * NamedGroup named_group_list<2..2^16-1>;
119 * } NamedGroupList;
120 */
121static int mbedtls_ssl_tls13_parse_supported_groups_ext(
122 mbedtls_ssl_context *ssl,
123 const unsigned char *buf, const unsigned char *end )
124{
125
126 size_t list_size, our_size;
127 const unsigned char *p = buf;
128 const mbedtls_ecp_curve_info *curve_info, **curves;
129 const unsigned char *extentions_end;
130
131 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
132 list_size = MBEDTLS_GET_UINT16_BE( p, 0 );
133 p += 2;
134 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_size );
135 if( list_size % 2 != 0 )
136 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
137
138 /* TODO: At the moment, this can happen when receiving a second
139 * ClientHello after an HRR. We should properly reset the
140 * state upon receiving an HRR, in which case we should
141 * not observe handshake->curves already being allocated. */
142 if( ssl->handshake->curves != NULL )
143 {
144 mbedtls_free( ssl->handshake->curves );
145 ssl->handshake->curves = NULL;
146 }
147
148 /* Don't allow our peer to make us allocate too much memory,
149 * and leave room for a final 0 */
150 our_size = list_size / 2 + 1;
151 if( our_size > MBEDTLS_ECP_DP_MAX )
152 our_size = MBEDTLS_ECP_DP_MAX;
153
154 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
155 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
156
157 extentions_end = p + list_size;
158 ssl->handshake->curves = curves;
159
160 while ( p < extentions_end && our_size > 1 )
161 {
162 uint16_t tls_grp_id = MBEDTLS_GET_UINT16_BE( p, 0 );
163 curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_grp_id );
164
165 /* mbedtls_ecp_curve_info_from_tls_id() uses the mbedtls_ecp_curve_info
166 * data structure (defined in ecp.c), which only includes the list of
167 * curves implemented. Hence, we only add curves that are also supported
168 * and implemented by the server. */
169 if( curve_info != NULL )
170 {
171 *curves++ = curve_info;
172 MBEDTLS_SSL_DEBUG_MSG( 4, ( "supported curve: %s", curve_info->name ) );
173 our_size--;
174 }
175
176 p += 2;
177 }
178
179 return( 0 );
180
181}
182#endif /* MBEDTLS_ECDH_C || ( MBEDTLS_ECDSA_C */
183
184/* TODO: Code for MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED missing */
185/*
186 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
187 * extension is correct and stores the provided key shares. Whether this is an
188 * acceptable key share depends on the selected ciphersuite.
189 *
190 * Possible return values are:
191 * - 0: Successful processing of the client provided key share extension.
192 * - MBEDTLS_ERR_SSL_HRR_REQUIRED: The key share provided by the client
193 * does not match a group supported by the server. A HelloRetryRequest will
194 * be needed.
195 * - Another negative return value for fatal errors.
196*/
197
198static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
199 const unsigned char *buf,
200 const unsigned char *end )
201{
202 int ret = 0;
203 unsigned char const *p = buf;
204 unsigned char const *extentions_end;
205
206 size_t total_ext_len, cur_share_len;
207 int match_found = 0;
208
209 /* From RFC 8446:
210 *
211 * struct {
212 * KeyShareEntry client_shares<0..2^16-1>;
213 * } KeyShareClientHello;
214 *
215 */
216
217 /* Read total legnth of KeyShareClientHello */
218 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
219
220 total_ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
221 p += 2;
222 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, total_ext_len );
223
224 ssl->handshake->offered_group_id = 0;
225 extentions_end = p + total_ext_len;
226
227 /* We try to find a suitable key share entry and copy it to the
228 * handshake context. Later, we have to find out whether we can do
229 * something with the provided key share or whether we have to
230 * dismiss it and send a HelloRetryRequest message. */
231
232 for( ; p < extentions_end; p += cur_share_len )
233 {
234 uint16_t their_group;
235 mbedtls_ecp_group_id their_curve;
236 mbedtls_ecp_curve_info const *their_curve_info;
237 unsigned char const *end_of_share;
238
239 /*
240 * struct {
241 * NamedGroup group;
242 * opaque key_exchange<1..2^16-1>;
243 * } KeyShareEntry;
244 */
245 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extentions_end, 4 );
246
247 their_group = MBEDTLS_GET_UINT16_BE( p, 0 );
248 p += 2;
249
250 cur_share_len = MBEDTLS_GET_UINT16_BE( p, 0 );
251 p += 2;
252
253 end_of_share = p + cur_share_len;
254
255 /* Continue parsing even if we have already found a match,
256 * for input validation purposes. */
257 if( match_found == 1 )
258 continue;
259
260 /*
261 * NamedGroup matching
262 *
263 * For now, we only support ECDHE groups, but e.g.
264 * PQC KEMs will need to be added at a later stage.
265 */
266
267 /* Type 1: ECDHE shares
268 *
269 * - Check if we recognize the group
270 * - Check if it's supported
271 */
272
273 const mbedtls_ecp_curve_info *curve_info;
274 curve_info = mbedtls_ecp_curve_info_from_tls_id( their_group );
275 if( curve_info == NULL )
276 return( MBEDTLS_ECP_DP_NONE );
277 their_curve = curve_info->grp_id;
278 if( mbedtls_ssl_check_curve( ssl, their_curve ) != 0 )
279 continue;
280
281 /* Type 2..X: Other kinds of shares */
282 /* TO BE ADDED */
283
284 /* Skip if we no match succeeded. */
285 if( their_curve == MBEDTLS_ECP_DP_NONE )
286 {
287 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
288 (unsigned) their_group ) );
289 continue;
290 }
291
292 match_found = 1;
293
294 /* KeyShare parsing
295 *
296 * Once we add more key share types, this needs to be a switch
297 * over the (type of) the named curve */
298
299 /* Type 1: ECDHE shares
300 *
301 * - Setup ECDHE context
302 * - Import client's public key
303 * - Apply further curve checks
304 */
305
306 their_curve_info = mbedtls_ecp_curve_info_from_grp_id( their_curve );
307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", their_curve_info->name ) );
308
309 ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx, their_curve );
310 if( ret != 0 )
311 {
312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup()", ret );
313 return( ret );
314 }
315
316 ret = mbedtls_ecdh_import_public_raw( &ssl->handshake->ecdh_ctx,
317 p, end_of_share );
318 if( ret != 0 )
319 {
320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_import_public_raw()", ret );
321 return( ret );
322 }
323
324 ssl->handshake->offered_group_id = their_group;
325 }
326
327 if( match_found == 0 )
328 {
329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
330 return( MBEDTLS_ERR_SSL_HRR_REQUIRED );
331 }
332 return( 0 );
333}
334#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
335
336#if defined(MBEDTLS_SSL_COOKIE_C)
337static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
338 const unsigned char *buf,
339 const unsigned char *end )
340{
341 int ret = 0;
342 size_t cookie_len;
343 unsigned char const *p = buf;
344 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
345
346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse cookie extension" ) );
347
348 if( ssl->conf->f_cookie_check != NULL )
349 {
350 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
351 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
352 p += 2;
353
354 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
355
356 MBEDTLS_SSL_DEBUG_BUF( 3, "Received cookie", p, cookie_len );
357
358 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
359 p, cookie_len, ssl->cli_id,
360 ssl->cli_id_len ) != 0 )
361 {
362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
363 handshake->verify_cookie_len = 1;
364 ret = MBEDTLS_ERR_SSL_HRR_REQUIRED;
365 }
366 else
367 {
368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
369 handshake->verify_cookie_len = 0;
370 }
371 }
372 else {
373 /* TBD: Check under what cases this is appropriate */
374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
375 }
376
377 return( ret );
378}
379#endif /* MBEDTLS_SSL_COOKIE_C */
380
381/*
382 *
383 * STATE HANDLING: ClientHello
384 *
385 * There are three possible classes of outcomes when parsing the CH:
386 *
387 * 1) The CH was well-formed and matched the server's configuration.
388 *
389 * In this case, the server progresses to sending its ServerHello.
390 *
391 * 2) The CH was well-formed but didn't match the server's configuration.
392 *
393 * For example, the client might not have offered a key share which
394 * the server supports, or the server might require a cookie.
395 *
396 * In this case, the server sends a HelloRetryRequest.
397 *
398 * 3) The CH was ill-formed
399 *
400 * In this case, we abort the handshake.
401 *
402 */
403
404/*
405 * Overview
406 */
407
408/* Main entry point from the state machine; orchestrates the otherfunctions. */
409static int ssl_client_hello_process( mbedtls_ssl_context *ssl );
410
411static int ssl_client_hello_parse( mbedtls_ssl_context *ssl,
412 const unsigned char *buf,
413 const unsigned char *end );
414
415/* Update the handshake state machine */
416/* TODO: At the moment, this doesn't update the state machine - why? */
417static int ssl_client_hello_postprocess( mbedtls_ssl_context *ssl,
418 int hrr_required );
419
420/*
421 * Implementation
422 */
423
424#define SSL_CLIENT_HELLO_OK 0
425#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
426
427static int ssl_client_hello_process( mbedtls_ssl_context *ssl )
428{
429
430 int ret = 0;
431 int hrr_required = SSL_CLIENT_HELLO_OK;
432 unsigned char* buf = NULL;
433 size_t buflen = 0;
434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
435
436 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
437 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
438 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
439 &buf, &buflen ) );
440
441 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
442 MBEDTLS_SSL_HS_CLIENT_HELLO,
443 buflen );
444
445 MBEDTLS_SSL_PROC_CHK_NEG( ssl_client_hello_parse( ssl, buf, buf + buflen ) );
446 hrr_required = ret;
447
448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
449 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess( ssl, hrr_required ) );
450
451cleanup:
452
453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
454 return( ret );
455}
456
457static void ssl_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
458{
459 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 )
541 * 34 . 35 session id length ( 1 byte )
542 * 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
553 /* TBD: Needs to be updated due to mandatory extensions
554 * 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 );
583 p += 32; /* skip random bytes */
584
585 /*
586 * Parse session ID
587 */
588 sess_len = p[0];
589 p++; /* skip session id length */
590
591 if( sess_len > 32 )
592 {
593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
594 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
595 }
596
597 ssl->session_negotiate->id_len = sess_len;
598
599 /* Note that this field is echoed even if
600 * the client's value corresponded to a cached pre-TLS 1.3 session
601 * which the server has chosen not to resume. A client which
602 * receives a legacy_session_id_echo field that does not match what
603 * it sent in the ClientHello MUST abort the handshake with an
604 * "illegal_parameter" alert.
605 */
606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id length ( %" MBEDTLS_PRINTF_SIZET " )", sess_len ) );
607 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf, sess_len );
608
609 memcpy( &ssl->session_negotiate->id[0], p, sess_len ); /* write session id */
610 p += sess_len;
611
612 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
613 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
614 p += 2;
615
616 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
617
618 /* store pointer to ciphersuite list */
619 ciph_offset = p;
620
621 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
622 p, cipher_suites_len );
623
624 /* skip ciphersuites for now */
625 p += cipher_suites_len;
626
627 /*
628 * For TLS 1.3 we are not using compression.
629 */
630 comp_len = buf[0];
631 p++;
632 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, comp_len );
633
634 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
635 p, comp_len );
636
637 /* Determine whether we are indeed using null compression */
638 if( ( comp_len != 1 ) && ( p[1] == 0 ) )
639 {
640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
641 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
642 }
643
644 /* skip compression */
645 p++;
646
647 /*
648 * Check the extension length
649 */
650 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
651
652 ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
653 p += 2;
654 extensions_end = p + ext_len;
655 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ext_len );
656
657 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, ext_len );
658
659 while( p < extensions_end )
660 {
661 unsigned int extension_type;
662 size_t extension_data_len;
663 const unsigned char *extension_data_end;
664
665 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
666 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
667 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
668 p += 4;
669
670 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
671 extension_data_end = p + extension_data_len;
672
673 switch( extension_type )
674 {
675#if defined(MBEDTLS_SSL_COOKIE_C)
676 case MBEDTLS_TLS_EXT_COOKIE:
677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found cookie extension" ) );
678
679 ret = ssl_tls13_parse_cookie_ext( ssl, p,
680 extension_data_end );
681
682 /* if cookie verification failed then we return a hello retry
683 * message, or return success and set cookie extension present
684 */
685 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
686 {
687 hrr_required = 1;
688 }
689 else if( ret == 0 )
690 {
691 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_COOKIE;
692 }
693 break;
694#endif /* MBEDTLS_SSL_COOKIE_C */
695
696#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
697 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
699
700 /* Supported Groups Extension
701 *
702 * When sent by the client, the "supported_groups" extension
703 * indicates the named groups which the client supports,
704 * ordered from most preferred to least preferred.
705 */
706 ret = mbedtls_ssl_tls13_parse_supported_groups_ext( ssl, p,
707 extension_data_end );
708 if( ret != 0 )
709 {
710 MBEDTLS_SSL_DEBUG_RET( 1,
711 "mbedtls_ssl_parse_supported_groups_ext", ret );
712 return( ret );
713 }
714
715 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
716 break;
717#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
718
719#if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
720 case MBEDTLS_TLS_EXT_KEY_SHARE:
721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
722
723 /*
724 * Key Share Extension
725 *
726 * When sent by the client, the "key_share" extension
727 * contains the endpoint's cryptographic parameters for
728 * ECDHE/DHE key establishment methods.
729 */
730 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
731 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
732 {
733 hrr_required = 1;
734 ret = 0;
735 }
736
737 if( ret != 0 )
738 return( ret );
739
740 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
741 break;
742#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
743
744 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
746
747 ret = ssl_tls13_parse_supported_versions_ext(
748 ssl, p, extension_data_end );
749 if( ret != 0 )
750 {
751 MBEDTLS_SSL_DEBUG_RET( 1,
752 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
753 return( ret );
754 }
755 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
756 break;
757
758#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
759 case MBEDTLS_TLS_EXT_SIG_ALG:
760 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
761
762 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
763 extension_data_end );
764 if( ret != 0 )
765 {
766 MBEDTLS_SSL_DEBUG_MSG( 1,
767 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
768 ret ) );
769 return( ret );
770 }
771 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
772 break;
773#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
774
775 default:
776 MBEDTLS_SSL_DEBUG_MSG( 3,
777 ( "unknown extension found: %ud ( ignoring )",
778 extension_type ) );
779 }
780
781 p += extension_data_len;
782 }
783
784 /* Update checksum with either
785 * - The entire content of the CH message, if no PSK extension is present
786 * - The content up to but excluding the PSK extension, if present.
787 */
788 ssl->handshake->update_checksum( ssl, buf, p - buf );
789 /*
790 * Search for a matching ciphersuite
791 */
792 ciphersuites = ssl->conf->ciphersuite_list;
793 ciphersuite_info = NULL;
794 for ( j = 0, p = ciph_offset; j < cipher_suites_len; j += 2, p += 2 )
795 {
796 for ( i = 0; ciphersuites[i] != 0; i++ )
797 {
798 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
799 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
800 continue;
801
802 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
803 ciphersuites[i] );
804
805 if( ciphersuite_info == NULL )
806 {
807 MBEDTLS_SSL_DEBUG_MSG(
808 1,
809 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
811 }
812
813 goto have_ciphersuite;
814
815 }
816 }
817
818 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
819
820have_ciphersuite:
821
822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
823 ciphersuite_info->name ) );
824
825 ssl->session_negotiate->ciphersuite = ciphersuites[i];
826 ssl->handshake->ciphersuite_info = ciphersuite_info;
827
828 /* List all the extensions we have received */
829 ssl_debug_print_client_hello_exts( ssl );
830
831 /*
832 * Determine the key exchange algorithm to use.
833 * There are three types of key exchanges supported in TLS 1.3:
834 * - (EC)DH with ECDSA,
835 * - (EC)DH with PSK,
836 * - plain PSK.
837 *
838 * The PSK-based key exchanges may additionally be used with 0-RTT.
839 *
840 * Our built-in order of preference is
841 * 1 ) Plain PSK Mode
842 * 2 ) (EC)DHE-PSK Mode
843 * 3 ) Certificate Mode
844 */
845
846 if( !ssl_check_certificate_key_exchange( ssl ) )
847 {
848 MBEDTLS_SSL_DEBUG_MSG(
849 1,
850 ( "ClientHello message misses mandatory extensions." ) );
851 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
852 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
853 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
854 }
855
856#if defined(MBEDTLS_SSL_COOKIE_C)
857 /* If we failed to see a cookie extension, and we required it through the
858 * configuration settings ( rr_config ), then we need to send a HRR msg.
859 * Conceptually, this is similiar to having received a cookie that failed
860 * the verification check.
861 */
862 if( ( ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON ) &&
863 !( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) )
864 {
865 MBEDTLS_SSL_DEBUG_MSG(
866 2,
867 ( "Cookie extension missing. Need to send a HRR." ) );
868 hrr_required = 1;
869 }
870#endif /* MBEDTLS_SSL_COOKIE_C */
871
872 if( hrr_required == 1 )
873 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
874
875 return( 0 );
876}
877
878static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl,
879 int hrr_required )
880{
881 int ret = 0;
882
883 if( ssl->handshake->hello_retry_request_count == 0 &&
884 ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON )
885 {
886 hrr_required = SSL_CLIENT_HELLO_HRR_REQUIRED;
887 }
888
889 if( hrr_required == SSL_CLIENT_HELLO_HRR_REQUIRED )
890 {
891 /*
892 * Create stateless transcript hash for HRR
893 */
894 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
895 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
896 if( ret != 0 )
897 {
898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr",
899 ret );
900 return( ret );
901 }
902 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
903
904 /* Transmit Hello Retry Request */
905 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
906 return( 0 );
907 }
908
909 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
910 if( ret != 0 )
911 {
912 MBEDTLS_SSL_DEBUG_RET( 1,
913 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
914 return( ret );
915 }
916
917 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
918 return( 0 );
919
920}
921
922/*
923 * TLS and DTLS 1.3 State Maschine -- server side
924 */
Jerry Yu27561932021-08-27 17:07:38 +0800925int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800926{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000927 int ret = 0;
928
929 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
931
Jerry Yue3b34122021-09-28 17:53:35 +0800932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
933 mbedtls_ssl_states_str( ssl->state ),
934 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800935
XiaokangQian7807f9f2022-02-15 10:04:37 +0000936 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
937 return( ret );
938
939 switch( ssl->state )
940 {
941 /* start state */
942 case MBEDTLS_SSL_HELLO_REQUEST:
943 ssl->handshake->hello_retry_request_count = 0;
944 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
945
946 break;
947
948 /* ----- READ CLIENT HELLO ----*/
949
950 case MBEDTLS_SSL_CLIENT_HELLO:
951
952 ret = ssl_client_hello_process( ssl );
953 if( ret != 0 )
954 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_client_hello_process", ret );
955
956 break;
957
958 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
960
961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for all traffic" ) );
962
963 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
964 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
965
966 mbedtls_ssl_tls13_handshake_wrapup( ssl );
967 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET );
968
969 break;
970
971 default:
972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
974 }
975
976 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800977}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800978
Jerry Yufb4b6472022-01-27 15:03:26 +0800979#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */