blob: 007b9fa7728a1c9869a362fe24c6047b02e4fcdd [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
95 * a graceful fallback to TLS 1.2 in this case. */
96
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;
109 ssl->handshake->max_major_ver = ssl->major_ver;
110 ssl->handshake->max_minor_ver = ssl->minor_ver;
111 return( 0 );
112}
113
114#if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
115/* This function parses the TLS 1.3 supported_groups extension and
116 * stores the received groups in ssl->handshake->curves.
117 *
118 * From RFC 8446:
119 * enum {
120 * ... (0xFFFF)
121 * } NamedGroup;
122 * struct {
123 * NamedGroup named_group_list<2..2^16-1>;
124 * } NamedGroupList;
125 */
126static int mbedtls_ssl_tls13_parse_supported_groups_ext(
127 mbedtls_ssl_context *ssl,
128 const unsigned char *buf, const unsigned char *end )
129{
130
131 size_t list_size, our_size;
132 const unsigned char *p = buf;
133 const mbedtls_ecp_curve_info *curve_info, **curves;
134 const unsigned char *extentions_end;
135
136 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
137 list_size = MBEDTLS_GET_UINT16_BE( p, 0 );
138 p += 2;
139 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_size );
140 if( list_size % 2 != 0 )
141 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
142
143 /* TODO: At the moment, this can happen when receiving a second
144 * ClientHello after an HRR. We should properly reset the
145 * state upon receiving an HRR, in which case we should
146 * not observe handshake->curves already being allocated. */
147 if( ssl->handshake->curves != NULL )
148 {
149 mbedtls_free( ssl->handshake->curves );
150 ssl->handshake->curves = NULL;
151 }
152
153 /* Don't allow our peer to make us allocate too much memory,
154 * and leave room for a final 0 */
155 our_size = list_size / 2 + 1;
156 if( our_size > MBEDTLS_ECP_DP_MAX )
157 our_size = MBEDTLS_ECP_DP_MAX;
158
159 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
160 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
161
162 extentions_end = p + list_size;
163 ssl->handshake->curves = curves;
164
165 while ( p < extentions_end && our_size > 1 )
166 {
167 uint16_t tls_grp_id = MBEDTLS_GET_UINT16_BE( p, 0 );
168 curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_grp_id );
169
170 /* mbedtls_ecp_curve_info_from_tls_id() uses the mbedtls_ecp_curve_info
171 * data structure (defined in ecp.c), which only includes the list of
172 * curves implemented. Hence, we only add curves that are also supported
173 * and implemented by the server. */
174 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
XiaokangQiana9c58412022-02-17 09:41:26 +0000189#if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000190/* TODO: Code for MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED missing */
191/*
192 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
193 * extension is correct and stores the provided key shares. Whether this is an
194 * acceptable key share depends on the selected ciphersuite.
195 *
196 * Possible return values are:
197 * - 0: Successful processing of the client provided key share extension.
198 * - MBEDTLS_ERR_SSL_HRR_REQUIRED: The key share provided by the client
199 * does not match a group supported by the server. A HelloRetryRequest will
200 * be needed.
201 * - Another negative return value for fatal errors.
202*/
203
204static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
205 const unsigned char *buf,
206 const unsigned char *end )
207{
208 int ret = 0;
209 unsigned char const *p = buf;
210 unsigned char const *extentions_end;
211
212 size_t total_ext_len, cur_share_len;
213 int match_found = 0;
214
215 /* From RFC 8446:
216 *
217 * struct {
218 * KeyShareEntry client_shares<0..2^16-1>;
219 * } KeyShareClientHello;
220 *
221 */
222
223 /* Read total legnth of KeyShareClientHello */
224 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
225
226 total_ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
227 p += 2;
228 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, total_ext_len );
229
230 ssl->handshake->offered_group_id = 0;
231 extentions_end = p + total_ext_len;
232
233 /* We try to find a suitable key share entry and copy it to the
234 * handshake context. Later, we have to find out whether we can do
235 * something with the provided key share or whether we have to
236 * dismiss it and send a HelloRetryRequest message. */
237
238 for( ; p < extentions_end; p += cur_share_len )
239 {
240 uint16_t their_group;
241 mbedtls_ecp_group_id their_curve;
242 mbedtls_ecp_curve_info const *their_curve_info;
243 unsigned char const *end_of_share;
244
245 /*
246 * struct {
247 * NamedGroup group;
248 * opaque key_exchange<1..2^16-1>;
249 * } KeyShareEntry;
250 */
251 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extentions_end, 4 );
252
253 their_group = MBEDTLS_GET_UINT16_BE( p, 0 );
254 p += 2;
255
256 cur_share_len = MBEDTLS_GET_UINT16_BE( p, 0 );
257 p += 2;
258
259 end_of_share = p + cur_share_len;
260
261 /* Continue parsing even if we have already found a match,
262 * for input validation purposes. */
263 if( match_found == 1 )
264 continue;
265
266 /*
267 * NamedGroup matching
268 *
269 * For now, we only support ECDHE groups, but e.g.
270 * PQC KEMs will need to be added at a later stage.
271 */
272
273 /* Type 1: ECDHE shares
274 *
275 * - Check if we recognize the group
276 * - Check if it's supported
277 */
278
XiaokangQian3207a322022-02-23 03:15:27 +0000279 their_curve = mbedtls_ecp_named_group_to_id( their_group );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000280 if( mbedtls_ssl_check_curve( ssl, their_curve ) != 0 )
281 continue;
282
283 /* Type 2..X: Other kinds of shares */
284 /* TO BE ADDED */
285
286 /* 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
299 * over the (type of) the named curve */
300
301 /* Type 1: ECDHE shares
302 *
303 * - Setup ECDHE context
304 * - Import client's public key
305 * - Apply further curve checks
306 */
307
308 their_curve_info = mbedtls_ecp_curve_info_from_grp_id( their_curve );
309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", their_curve_info->name ) );
310
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}
336#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
337
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;
346 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
347
348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse cookie extension" ) );
349
350 if( ssl->conf->f_cookie_check != NULL )
351 {
352 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
353 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
354 p += 2;
355
356 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
357
358 MBEDTLS_SSL_DEBUG_BUF( 3, "Received cookie", p, cookie_len );
359
360 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
361 p, cookie_len, ssl->cli_id,
362 ssl->cli_id_len ) != 0 )
363 {
364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
365 handshake->verify_cookie_len = 1;
366 ret = MBEDTLS_ERR_SSL_HRR_REQUIRED;
367 }
368 else
369 {
370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
371 handshake->verify_cookie_len = 0;
372 }
373 }
374 else {
375 /* TBD: Check under what cases this is appropriate */
376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
377 }
378
379 return( ret );
380}
381#endif /* MBEDTLS_SSL_COOKIE_C */
382
383/*
384 *
385 * STATE HANDLING: ClientHello
386 *
387 * There are three possible classes of outcomes when parsing the CH:
388 *
389 * 1) The CH was well-formed and matched the server's configuration.
390 *
391 * In this case, the server progresses to sending its ServerHello.
392 *
393 * 2) The CH was well-formed but didn't match the server's configuration.
394 *
395 * For example, the client might not have offered a key share which
396 * the server supports, or the server might require a cookie.
397 *
398 * In this case, the server sends a HelloRetryRequest.
399 *
400 * 3) The CH was ill-formed
401 *
402 * In this case, we abort the handshake.
403 *
404 */
405
406/*
407 * Overview
408 */
409
410/* Main entry point from the state machine; orchestrates the otherfunctions. */
411static int ssl_client_hello_process( mbedtls_ssl_context *ssl );
412
413static int ssl_client_hello_parse( mbedtls_ssl_context *ssl,
414 const unsigned char *buf,
415 const unsigned char *end );
416
417/* Update the handshake state machine */
418/* TODO: At the moment, this doesn't update the state machine - why? */
419static int ssl_client_hello_postprocess( mbedtls_ssl_context *ssl,
420 int hrr_required );
421
422/*
423 * Implementation
424 */
425
426#define SSL_CLIENT_HELLO_OK 0
427#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
428
429static int ssl_client_hello_process( mbedtls_ssl_context *ssl )
430{
431
432 int ret = 0;
433 int hrr_required = SSL_CLIENT_HELLO_OK;
434 unsigned char* buf = NULL;
435 size_t buflen = 0;
436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
437
438 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
439 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
440 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
441 &buf, &buflen ) );
442
443 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
444 MBEDTLS_SSL_HS_CLIENT_HELLO,
445 buflen );
446
447 MBEDTLS_SSL_PROC_CHK_NEG( ssl_client_hello_parse( ssl, buf, buf + buflen ) );
448 hrr_required = ret;
449
450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
451 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess( ssl, hrr_required ) );
452
453cleanup:
454
455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
456 return( ret );
457}
458
459static void ssl_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
460{
XiaokangQian3207a322022-02-23 03:15:27 +0000461 ((void) ssl);
462
XiaokangQian7807f9f2022-02-15 10:04:37 +0000463 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- KEY_SHARE_EXTENSION ( %s )",
465 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ?
466 "TRUE" : "FALSE" ) );
467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
468 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
469 "TRUE" : "FALSE" ) );
470 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
471 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ?
472 "TRUE" : "FALSE" ) );
473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
474 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ?
475 "TRUE" : "FALSE" ) );
476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
477 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
478 "TRUE" : "FALSE" ) );
479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
480 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
481 "TRUE" : "FALSE" ) );
482#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SERVERNAME_EXTENSION ( %s )",
484 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
485 "TRUE" : "FALSE" ) );
486#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
487#if defined ( MBEDTLS_SSL_COOKIE_C )
488 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- COOKIE_EXTENSION ( %s )",
489 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) >0 ) ?
490 "TRUE" : "FALSE" ) );
491#endif /* MBEDTLS_SSL_COOKIE_C */
492}
493
494static int ssl_client_hello_has_exts( mbedtls_ssl_context *ssl,
495 int ext_id_mask )
496{
497 int masked = ssl->handshake->extensions_present & ext_id_mask;
498 return( masked == ext_id_mask );
499}
500
501static int ssl_client_hello_has_cert_extensions( mbedtls_ssl_context *ssl )
502{
503 return( ssl_client_hello_has_exts( ssl,
504 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
505 MBEDTLS_SSL_EXT_KEY_SHARE |
506 MBEDTLS_SSL_EXT_SIG_ALG ) );
507}
508
509static int ssl_check_certificate_key_exchange( mbedtls_ssl_context *ssl )
510{
511 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
512 return( 0 );
513
514 if( !ssl_client_hello_has_cert_extensions( ssl ) )
515 return( 0 );
516
517 ssl->handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
518 return( 1 );
519}
520
521static int ssl_client_hello_parse( mbedtls_ssl_context *ssl,
522 const unsigned char *buf,
523 const unsigned char *end )
524{
525 int ret;
526 size_t i, j;
527 size_t comp_len, sess_len;
528 size_t cipher_suites_len;
529 size_t ext_len;
530 const unsigned char *ciph_offset;
531 const unsigned char *p = buf;
532 const unsigned char *extensions_end;
533
534 const int* ciphersuites;
535 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
536
537 int hrr_required = 0;
538
539 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
540
541 /*
542 * ClientHello layer:
543 * 0 . 1 protocol version
544 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
545 * 34 . 35 session id length ( 1 byte )
546 * 35 . 34+x session id
547 * 35+x . 35+x DTLS only: cookie length ( 1 byte )
548 * 36+x . .. DTLS only: cookie
549 * .. . .. ciphersuite list length ( 2 bytes )
550 * .. . .. ciphersuite list
551 * .. . .. compression alg. list length ( 1 byte )
552 * .. . .. compression alg. list
553 * .. . .. extensions length ( 2 bytes, optional )
554 * .. . .. extensions ( optional )
555 */
556
557 /* TBD: Needs to be updated due to mandatory extensions
558 * Minimal length ( with everything empty and extensions ommitted ) is
559 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
560 * read at least up to session id length without worrying.
561 */
562 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
563
564 /* ...
565 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
566 * ...
567 * with ProtocolVersion defined as:
568 * uint16 ProtocolVersion;
569 */
570 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
571 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
572 {
573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
574 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
575 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
576 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
577 return ret;
578 }
579 p += 2;
580
581 /*
582 * Save client random
583 */
584 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
585
586 memcpy( &ssl->handshake->randbytes[0], p, 32 );
587 p += 32; /* skip random bytes */
588
589 /*
590 * Parse session ID
591 */
592 sess_len = p[0];
593 p++; /* skip session id length */
594
595 if( sess_len > 32 )
596 {
597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
598 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
599 }
600
601 ssl->session_negotiate->id_len = sess_len;
602
603 /* Note that this field is echoed even if
604 * the client's value corresponded to a cached pre-TLS 1.3 session
605 * which the server has chosen not to resume. A client which
606 * receives a legacy_session_id_echo field that does not match what
607 * it sent in the ClientHello MUST abort the handshake with an
608 * "illegal_parameter" alert.
609 */
610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id length ( %" MBEDTLS_PRINTF_SIZET " )", sess_len ) );
611 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf, sess_len );
612
613 memcpy( &ssl->session_negotiate->id[0], p, sess_len ); /* write session id */
614 p += sess_len;
615
616 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
617 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
618 p += 2;
619
620 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
621
622 /* store pointer to ciphersuite list */
623 ciph_offset = p;
624
625 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
626 p, cipher_suites_len );
627
628 /* skip ciphersuites for now */
629 p += cipher_suites_len;
630
631 /*
632 * For TLS 1.3 we are not using compression.
633 */
XiaokangQiana9c58412022-02-17 09:41:26 +0000634 comp_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000635 p++;
636 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, comp_len );
637
638 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
639 p, comp_len );
640
641 /* Determine whether we are indeed using null compression */
XiaokangQiana9c58412022-02-17 09:41:26 +0000642 if( ( comp_len != 1 ) && ( p[0] == 0 ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000643 {
644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
645 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
646 }
647
648 /* skip compression */
649 p++;
650
651 /*
652 * Check the extension length
653 */
654 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
655
656 ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
657 p += 2;
658 extensions_end = p + ext_len;
659 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ext_len );
660
661 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, ext_len );
662
663 while( p < extensions_end )
664 {
665 unsigned int extension_type;
666 size_t extension_data_len;
667 const unsigned char *extension_data_end;
668
669 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
670 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
671 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
672 p += 4;
673
674 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
675 extension_data_end = p + extension_data_len;
676
677 switch( extension_type )
678 {
679#if defined(MBEDTLS_SSL_COOKIE_C)
680 case MBEDTLS_TLS_EXT_COOKIE:
681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found cookie extension" ) );
682
683 ret = ssl_tls13_parse_cookie_ext( ssl, p,
684 extension_data_end );
685
686 /* if cookie verification failed then we return a hello retry
687 * message, or return success and set cookie extension present
688 */
689 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
690 {
691 hrr_required = 1;
692 }
693 else if( ret == 0 )
694 {
695 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_COOKIE;
696 }
697 break;
698#endif /* MBEDTLS_SSL_COOKIE_C */
699
700#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
701 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
703
704 /* Supported Groups Extension
705 *
706 * When sent by the client, the "supported_groups" extension
707 * indicates the named groups which the client supports,
708 * ordered from most preferred to least preferred.
709 */
710 ret = mbedtls_ssl_tls13_parse_supported_groups_ext( ssl, p,
711 extension_data_end );
712 if( ret != 0 )
713 {
714 MBEDTLS_SSL_DEBUG_RET( 1,
715 "mbedtls_ssl_parse_supported_groups_ext", ret );
716 return( ret );
717 }
718
719 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
720 break;
721#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
722
723#if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
724 case MBEDTLS_TLS_EXT_KEY_SHARE:
725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
726
727 /*
728 * Key Share Extension
729 *
730 * When sent by the client, the "key_share" extension
731 * contains the endpoint's cryptographic parameters for
732 * ECDHE/DHE key establishment methods.
733 */
734 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
735 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
736 {
737 hrr_required = 1;
738 ret = 0;
739 }
740
741 if( ret != 0 )
742 return( ret );
743
744 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
745 break;
746#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
747
748 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
749 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
750
751 ret = ssl_tls13_parse_supported_versions_ext(
752 ssl, p, extension_data_end );
753 if( ret != 0 )
754 {
755 MBEDTLS_SSL_DEBUG_RET( 1,
756 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
757 return( ret );
758 }
759 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
760 break;
761
762#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
763 case MBEDTLS_TLS_EXT_SIG_ALG:
764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
765
766 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
767 extension_data_end );
768 if( ret != 0 )
769 {
770 MBEDTLS_SSL_DEBUG_MSG( 1,
771 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
772 ret ) );
773 return( ret );
774 }
775 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
776 break;
777#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
778
779 default:
780 MBEDTLS_SSL_DEBUG_MSG( 3,
781 ( "unknown extension found: %ud ( ignoring )",
782 extension_type ) );
783 }
784
785 p += extension_data_len;
786 }
787
788 /* Update checksum with either
789 * - The entire content of the CH message, if no PSK extension is present
790 * - The content up to but excluding the PSK extension, if present.
791 */
792 ssl->handshake->update_checksum( ssl, buf, p - buf );
793 /*
794 * Search for a matching ciphersuite
795 */
796 ciphersuites = ssl->conf->ciphersuite_list;
797 ciphersuite_info = NULL;
798 for ( j = 0, p = ciph_offset; j < cipher_suites_len; j += 2, p += 2 )
799 {
800 for ( i = 0; ciphersuites[i] != 0; i++ )
801 {
802 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
803 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
804 continue;
805
806 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
807 ciphersuites[i] );
808
809 if( ciphersuite_info == NULL )
810 {
811 MBEDTLS_SSL_DEBUG_MSG(
812 1,
813 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
815 }
816
817 goto have_ciphersuite;
818
819 }
820 }
821
822 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
823
824have_ciphersuite:
825
826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
827 ciphersuite_info->name ) );
828
829 ssl->session_negotiate->ciphersuite = ciphersuites[i];
830 ssl->handshake->ciphersuite_info = ciphersuite_info;
831
832 /* List all the extensions we have received */
833 ssl_debug_print_client_hello_exts( ssl );
834
835 /*
836 * Determine the key exchange algorithm to use.
837 * There are three types of key exchanges supported in TLS 1.3:
838 * - (EC)DH with ECDSA,
839 * - (EC)DH with PSK,
840 * - plain PSK.
841 *
842 * The PSK-based key exchanges may additionally be used with 0-RTT.
843 *
844 * Our built-in order of preference is
845 * 1 ) Plain PSK Mode
846 * 2 ) (EC)DHE-PSK Mode
847 * 3 ) Certificate Mode
848 */
849
850 if( !ssl_check_certificate_key_exchange( ssl ) )
851 {
852 MBEDTLS_SSL_DEBUG_MSG(
853 1,
854 ( "ClientHello message misses mandatory extensions." ) );
855 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
856 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
857 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
858 }
859
860#if defined(MBEDTLS_SSL_COOKIE_C)
861 /* If we failed to see a cookie extension, and we required it through the
862 * configuration settings ( rr_config ), then we need to send a HRR msg.
863 * Conceptually, this is similiar to having received a cookie that failed
864 * the verification check.
865 */
866 if( ( ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON ) &&
867 !( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) )
868 {
869 MBEDTLS_SSL_DEBUG_MSG(
870 2,
871 ( "Cookie extension missing. Need to send a HRR." ) );
872 hrr_required = 1;
873 }
874#endif /* MBEDTLS_SSL_COOKIE_C */
875
876 if( hrr_required == 1 )
877 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
878
879 return( 0 );
880}
881
882static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl,
883 int hrr_required )
884{
885 int ret = 0;
886
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000887 if( ssl->handshake->hello_retry_requests_sent == 0 &&
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888 ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON )
889 {
890 hrr_required = SSL_CLIENT_HELLO_HRR_REQUIRED;
891 }
892
893 if( hrr_required == SSL_CLIENT_HELLO_HRR_REQUIRED )
894 {
895 /*
896 * Create stateless transcript hash for HRR
897 */
898 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
899 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
900 if( ret != 0 )
901 {
902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr",
903 ret );
904 return( ret );
905 }
906 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
907
908 /* Transmit Hello Retry Request */
909 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
910 return( 0 );
911 }
912
913 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
914 if( ret != 0 )
915 {
916 MBEDTLS_SSL_DEBUG_RET( 1,
917 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
918 return( ret );
919 }
920
921 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
922 return( 0 );
923
924}
925
926/*
927 * TLS and DTLS 1.3 State Maschine -- server side
928 */
Jerry Yu27561932021-08-27 17:07:38 +0800929int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800930{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000931 int ret = 0;
932
933 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
935
Jerry Yue3b34122021-09-28 17:53:35 +0800936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
937 mbedtls_ssl_states_str( ssl->state ),
938 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800939
XiaokangQian7807f9f2022-02-15 10:04:37 +0000940 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
941 return( ret );
942
943 switch( ssl->state )
944 {
945 /* start state */
946 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000947 ssl->handshake->hello_retry_requests_sent = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
949
950 break;
951
952 /* ----- READ CLIENT HELLO ----*/
953
954 case MBEDTLS_SSL_CLIENT_HELLO:
955
956 ret = ssl_client_hello_process( ssl );
957 if( ret != 0 )
958 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_client_hello_process", ret );
959
960 break;
961
962 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
964
965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for all traffic" ) );
966
967 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
968 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
969
970 mbedtls_ssl_tls13_handshake_wrapup( ssl );
971 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET );
972
973 break;
974
975 default:
976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
978 }
979
980 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800981}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800982
Jerry Yufb4b6472022-01-27 15:03:26 +0800983#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */