blob: 9f55fe76ea5eed8f8ba02ffef7975bb85acec629 [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;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000110 return( 0 );
111}
112
113#if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
114/* This function parses the TLS 1.3 supported_groups extension and
115 * stores the received groups in ssl->handshake->curves.
116 *
117 * From RFC 8446:
118 * enum {
119 * ... (0xFFFF)
120 * } NamedGroup;
121 * struct {
122 * NamedGroup named_group_list<2..2^16-1>;
123 * } NamedGroupList;
124 */
125static int mbedtls_ssl_tls13_parse_supported_groups_ext(
126 mbedtls_ssl_context *ssl,
127 const unsigned char *buf, const unsigned char *end )
128{
129
130 size_t list_size, our_size;
131 const unsigned char *p = buf;
132 const mbedtls_ecp_curve_info *curve_info, **curves;
133 const unsigned char *extentions_end;
134
135 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
136 list_size = MBEDTLS_GET_UINT16_BE( p, 0 );
137 p += 2;
138 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_size );
139 if( list_size % 2 != 0 )
140 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
141
XiaokangQianc5763b52022-04-02 03:34:37 +0000142 /* At the moment, this can happen when receiving a second
XiaokangQian7807f9f2022-02-15 10:04:37 +0000143 * ClientHello after an HRR. We should properly reset the
144 * state upon receiving an HRR, in which case we should
145 * not observe handshake->curves already being allocated. */
146 if( ssl->handshake->curves != NULL )
147 {
XiaokangQian88408882022-04-02 10:15:03 +0000148 //mbedtls_free( ssl->handshake->curves );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000149 ssl->handshake->curves = NULL;
150 }
151
152 /* Don't allow our peer to make us allocate too much memory,
XiaokangQianc5763b52022-04-02 03:34:37 +0000153 * and leave room for a final 0
154 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000155 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
XiaokangQianc5763b52022-04-02 03:34:37 +0000173 * and implemented by the server.
174 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000175 if( curve_info != NULL )
176 {
177 *curves++ = curve_info;
178 MBEDTLS_SSL_DEBUG_MSG( 4, ( "supported curve: %s", curve_info->name ) );
179 our_size--;
180 }
181
182 p += 2;
183 }
184
185 return( 0 );
186
187}
188#endif /* MBEDTLS_ECDH_C || ( MBEDTLS_ECDSA_C */
189
XiaokangQian88408882022-04-02 10:15:03 +0000190#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000191/*
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
XiaokangQianc5763b52022-04-02 03:34:37 +0000236 * dismiss it and send a HelloRetryRequest message.
237 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000238
239 for( ; p < extentions_end; p += cur_share_len )
240 {
241 uint16_t their_group;
242 mbedtls_ecp_group_id their_curve;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000243 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,
XiaokangQianc5763b52022-04-02 03:34:37 +0000262 * for input validation purposes.
263 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000264 if( match_found == 1 )
265 continue;
266
267 /*
268 * NamedGroup matching
269 *
270 * For now, we only support ECDHE groups, but e.g.
271 * PQC KEMs will need to be added at a later stage.
272 */
273
274 /* Type 1: ECDHE shares
275 *
276 * - Check if we recognize the group
277 * - Check if it's supported
278 */
279
XiaokangQian3207a322022-02-23 03:15:27 +0000280 their_curve = mbedtls_ecp_named_group_to_id( their_group );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000281 if( mbedtls_ssl_check_curve( ssl, their_curve ) != 0 )
282 continue;
283
XiaokangQian7807f9f2022-02-15 10:04:37 +0000284 /* 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
XiaokangQianc5763b52022-04-02 03:34:37 +0000297 * over the (type of) the named curve
298 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000299
300 /* Type 1: ECDHE shares
301 *
302 * - Setup ECDHE context
303 * - Import client's public key
304 * - Apply further curve checks
305 */
306
XiaokangQian88408882022-04-02 10:15:03 +0000307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %ud", their_curve ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000308
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}
XiaokangQian88408882022-04-02 10:15:03 +0000334#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000335
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;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000344
345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse cookie extension" ) );
346
347 if( ssl->conf->f_cookie_check != NULL )
348 {
349 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
350 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
351 p += 2;
352
353 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
354
355 MBEDTLS_SSL_DEBUG_BUF( 3, "Received cookie", p, cookie_len );
356
357 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
358 p, cookie_len, ssl->cli_id,
359 ssl->cli_id_len ) != 0 )
360 {
361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000362 ret = MBEDTLS_ERR_SSL_HRR_REQUIRED;
363 }
364 else
365 {
366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000367 }
368 }
XiaokangQianc5763b52022-04-02 03:34:37 +0000369 else
370 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
372 }
373
374 return( ret );
375}
376#endif /* MBEDTLS_SSL_COOKIE_C */
377
378/*
379 *
380 * STATE HANDLING: ClientHello
381 *
382 * There are three possible classes of outcomes when parsing the CH:
383 *
384 * 1) The CH was well-formed and matched the server's configuration.
385 *
386 * In this case, the server progresses to sending its ServerHello.
387 *
388 * 2) The CH was well-formed but didn't match the server's configuration.
389 *
390 * For example, the client might not have offered a key share which
391 * the server supports, or the server might require a cookie.
392 *
393 * In this case, the server sends a HelloRetryRequest.
394 *
395 * 3) The CH was ill-formed
396 *
397 * In this case, we abort the handshake.
398 *
399 */
400
401/*
402 * Overview
403 */
404
405/* Main entry point from the state machine; orchestrates the otherfunctions. */
406static int ssl_client_hello_process( mbedtls_ssl_context *ssl );
407
408static int ssl_client_hello_parse( mbedtls_ssl_context *ssl,
409 const unsigned char *buf,
410 const unsigned char *end );
411
412/* Update the handshake state machine */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000413static int ssl_client_hello_postprocess( mbedtls_ssl_context *ssl,
414 int hrr_required );
415
416/*
417 * Implementation
418 */
419
420#define SSL_CLIENT_HELLO_OK 0
421#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
422
423static int ssl_client_hello_process( mbedtls_ssl_context *ssl )
424{
425
426 int ret = 0;
427 int hrr_required = SSL_CLIENT_HELLO_OK;
428 unsigned char* buf = NULL;
429 size_t buflen = 0;
430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
431
432 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
433 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
434 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
435 &buf, &buflen ) );
436
XiaokangQian7807f9f2022-02-15 10:04:37 +0000437 MBEDTLS_SSL_PROC_CHK_NEG( ssl_client_hello_parse( ssl, buf, buf + buflen ) );
438 hrr_required = ret;
439
440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) );
441 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess( ssl, hrr_required ) );
442
443cleanup:
444
445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
446 return( ret );
447}
448
449static void ssl_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
450{
XiaokangQian3207a322022-02-23 03:15:27 +0000451 ((void) ssl);
452
XiaokangQian7807f9f2022-02-15 10:04:37 +0000453 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
454 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- KEY_SHARE_EXTENSION ( %s )",
455 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ?
456 "TRUE" : "FALSE" ) );
457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
458 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
459 "TRUE" : "FALSE" ) );
460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
461 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ?
462 "TRUE" : "FALSE" ) );
463 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
464 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ?
465 "TRUE" : "FALSE" ) );
466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
467 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
468 "TRUE" : "FALSE" ) );
469 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
470 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
471 "TRUE" : "FALSE" ) );
472#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SERVERNAME_EXTENSION ( %s )",
474 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
475 "TRUE" : "FALSE" ) );
476#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
477#if defined ( MBEDTLS_SSL_COOKIE_C )
478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "- COOKIE_EXTENSION ( %s )",
479 ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) >0 ) ?
480 "TRUE" : "FALSE" ) );
481#endif /* MBEDTLS_SSL_COOKIE_C */
482}
483
484static int ssl_client_hello_has_exts( mbedtls_ssl_context *ssl,
485 int ext_id_mask )
486{
487 int masked = ssl->handshake->extensions_present & ext_id_mask;
488 return( masked == ext_id_mask );
489}
490
491static int ssl_client_hello_has_cert_extensions( mbedtls_ssl_context *ssl )
492{
493 return( ssl_client_hello_has_exts( ssl,
494 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
495 MBEDTLS_SSL_EXT_KEY_SHARE |
496 MBEDTLS_SSL_EXT_SIG_ALG ) );
497}
498
499static int ssl_check_certificate_key_exchange( mbedtls_ssl_context *ssl )
500{
501 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
502 return( 0 );
503
504 if( !ssl_client_hello_has_cert_extensions( ssl ) )
505 return( 0 );
506
507 ssl->handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
508 return( 1 );
509}
510
511static int ssl_client_hello_parse( mbedtls_ssl_context *ssl,
512 const unsigned char *buf,
513 const unsigned char *end )
514{
515 int ret;
516 size_t i, j;
517 size_t comp_len, sess_len;
518 size_t cipher_suites_len;
519 size_t ext_len;
520 const unsigned char *ciph_offset;
521 const unsigned char *p = buf;
522 const unsigned char *extensions_end;
523
524 const int* ciphersuites;
525 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
526
527 int hrr_required = 0;
528
529 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
530
531 /*
532 * ClientHello layer:
533 * 0 . 1 protocol version
534 * 2 . 33 random bytes ( starting with 4 bytes of Unix time )
XiaokangQianc5763b52022-04-02 03:34:37 +0000535 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000536 * 35 . 34+x session id
537 * 35+x . 35+x DTLS only: cookie length ( 1 byte )
538 * 36+x . .. DTLS only: cookie
539 * .. . .. ciphersuite list length ( 2 bytes )
540 * .. . .. ciphersuite list
541 * .. . .. compression alg. list length ( 1 byte )
542 * .. . .. compression alg. list
543 * .. . .. extensions length ( 2 bytes, optional )
544 * .. . .. extensions ( optional )
545 */
546
XiaokangQianc5763b52022-04-02 03:34:37 +0000547 /* Needs to be updated due to mandatory extensions
XiaokangQian7807f9f2022-02-15 10:04:37 +0000548 * Minimal length ( with everything empty and extensions ommitted ) is
549 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
550 * read at least up to session id length without worrying.
551 */
552 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
553
554 /* ...
555 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
556 * ...
557 * with ProtocolVersion defined as:
558 * uint16 ProtocolVersion;
559 */
560 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
561 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
562 {
563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
564 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
565 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
566 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
567 return ret;
568 }
569 p += 2;
570
571 /*
572 * Save client random
573 */
574 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
575
576 memcpy( &ssl->handshake->randbytes[0], p, 32 );
XiaokangQianc5763b52022-04-02 03:34:37 +0000577 /* skip random bytes */
578 p += 32;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000579
580 /*
581 * Parse session ID
582 */
583 sess_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000584 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000585
586 if( sess_len > 32 )
587 {
588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
589 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
590 }
591
592 ssl->session_negotiate->id_len = sess_len;
593
594 /* Note that this field is echoed even if
595 * the client's value corresponded to a cached pre-TLS 1.3 session
596 * which the server has chosen not to resume. A client which
597 * receives a legacy_session_id_echo field that does not match what
598 * it sent in the ClientHello MUST abort the handshake with an
599 * "illegal_parameter" alert.
600 */
601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id length ( %" MBEDTLS_PRINTF_SIZET " )", sess_len ) );
602 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf, sess_len );
603
604 memcpy( &ssl->session_negotiate->id[0], p, sess_len ); /* write session id */
605 p += sess_len;
606
607 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
608 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
609 p += 2;
610
611 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len );
612
613 /* store pointer to ciphersuite list */
614 ciph_offset = p;
615
616 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
617 p, cipher_suites_len );
618
619 /* skip ciphersuites for now */
620 p += cipher_suites_len;
621
622 /*
623 * For TLS 1.3 we are not using compression.
624 */
XiaokangQiana9c58412022-02-17 09:41:26 +0000625 comp_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000626 p++;
627 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, comp_len );
628
629 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
630 p, comp_len );
631
632 /* Determine whether we are indeed using null compression */
XiaokangQiana9c58412022-02-17 09:41:26 +0000633 if( ( comp_len != 1 ) && ( p[0] == 0 ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000634 {
635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
636 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
637 }
638
639 /* skip compression */
640 p++;
641
642 /*
643 * Check the extension length
644 */
645 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
646
647 ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
648 p += 2;
649 extensions_end = p + ext_len;
650 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ext_len );
651
652 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, ext_len );
653
654 while( p < extensions_end )
655 {
656 unsigned int extension_type;
657 size_t extension_data_len;
658 const unsigned char *extension_data_end;
659
660 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
661 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
662 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
663 p += 4;
664
665 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
666 extension_data_end = p + extension_data_len;
667
668 switch( extension_type )
669 {
670#if defined(MBEDTLS_SSL_COOKIE_C)
671 case MBEDTLS_TLS_EXT_COOKIE:
672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found cookie extension" ) );
673
674 ret = ssl_tls13_parse_cookie_ext( ssl, p,
675 extension_data_end );
676
677 /* if cookie verification failed then we return a hello retry
678 * message, or return success and set cookie extension present
679 */
680 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
681 {
682 hrr_required = 1;
683 }
684 else if( ret == 0 )
685 {
686 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_COOKIE;
687 }
688 break;
689#endif /* MBEDTLS_SSL_COOKIE_C */
690
691#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
692 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
694
695 /* Supported Groups Extension
696 *
697 * When sent by the client, the "supported_groups" extension
698 * indicates the named groups which the client supports,
699 * ordered from most preferred to least preferred.
700 */
701 ret = mbedtls_ssl_tls13_parse_supported_groups_ext( ssl, p,
702 extension_data_end );
703 if( ret != 0 )
704 {
705 MBEDTLS_SSL_DEBUG_RET( 1,
706 "mbedtls_ssl_parse_supported_groups_ext", ret );
707 return( ret );
708 }
709
710 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
711 break;
712#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
713
XiaokangQian88408882022-04-02 10:15:03 +0000714#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000715 case MBEDTLS_TLS_EXT_KEY_SHARE:
716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
717
718 /*
719 * Key Share Extension
720 *
721 * When sent by the client, the "key_share" extension
722 * contains the endpoint's cryptographic parameters for
723 * ECDHE/DHE key establishment methods.
724 */
725 ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end );
726 if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED )
727 {
728 hrr_required = 1;
729 ret = 0;
730 }
731
732 if( ret != 0 )
733 return( ret );
734
735 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
736 break;
XiaokangQian88408882022-04-02 10:15:03 +0000737#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000738
739 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
741
742 ret = ssl_tls13_parse_supported_versions_ext(
743 ssl, p, extension_data_end );
744 if( ret != 0 )
745 {
746 MBEDTLS_SSL_DEBUG_RET( 1,
747 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
748 return( ret );
749 }
750 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
751 break;
752
753#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
754 case MBEDTLS_TLS_EXT_SIG_ALG:
755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
756
757 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
758 extension_data_end );
759 if( ret != 0 )
760 {
761 MBEDTLS_SSL_DEBUG_MSG( 1,
762 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
763 ret ) );
764 return( ret );
765 }
766 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
767 break;
768#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
769
770 default:
771 MBEDTLS_SSL_DEBUG_MSG( 3,
772 ( "unknown extension found: %ud ( ignoring )",
773 extension_type ) );
774 }
775
776 p += extension_data_len;
777 }
778
779 /* Update checksum with either
780 * - The entire content of the CH message, if no PSK extension is present
781 * - The content up to but excluding the PSK extension, if present.
782 */
XiaokangQianc4b8c992022-04-07 11:31:38 +0000783 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
784 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785 /*
786 * Search for a matching ciphersuite
787 */
788 ciphersuites = ssl->conf->ciphersuite_list;
789 ciphersuite_info = NULL;
790 for ( j = 0, p = ciph_offset; j < cipher_suites_len; j += 2, p += 2 )
791 {
792 for ( i = 0; ciphersuites[i] != 0; i++ )
793 {
794 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
795 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
796 continue;
797
798 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
799 ciphersuites[i] );
800
801 if( ciphersuite_info == NULL )
802 {
803 MBEDTLS_SSL_DEBUG_MSG(
804 1,
805 ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) );
806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
807 }
808
809 goto have_ciphersuite;
810
811 }
812 }
813
814 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
815
816have_ciphersuite:
817
818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
819 ciphersuite_info->name ) );
820
821 ssl->session_negotiate->ciphersuite = ciphersuites[i];
822 ssl->handshake->ciphersuite_info = ciphersuite_info;
823
824 /* List all the extensions we have received */
825 ssl_debug_print_client_hello_exts( ssl );
826
827 /*
828 * Determine the key exchange algorithm to use.
829 * There are three types of key exchanges supported in TLS 1.3:
830 * - (EC)DH with ECDSA,
831 * - (EC)DH with PSK,
832 * - plain PSK.
833 *
834 * The PSK-based key exchanges may additionally be used with 0-RTT.
835 *
836 * Our built-in order of preference is
837 * 1 ) Plain PSK Mode
838 * 2 ) (EC)DHE-PSK Mode
839 * 3 ) Certificate Mode
840 */
841
842 if( !ssl_check_certificate_key_exchange( ssl ) )
843 {
844 MBEDTLS_SSL_DEBUG_MSG(
845 1,
846 ( "ClientHello message misses mandatory extensions." ) );
847 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
848 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
849 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
850 }
851
852#if defined(MBEDTLS_SSL_COOKIE_C)
853 /* If we failed to see a cookie extension, and we required it through the
854 * configuration settings ( rr_config ), then we need to send a HRR msg.
855 * Conceptually, this is similiar to having received a cookie that failed
856 * the verification check.
857 */
858 if( ( ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON ) &&
859 !( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) )
860 {
861 MBEDTLS_SSL_DEBUG_MSG(
862 2,
863 ( "Cookie extension missing. Need to send a HRR." ) );
864 hrr_required = 1;
865 }
866#endif /* MBEDTLS_SSL_COOKIE_C */
867
868 if( hrr_required == 1 )
869 return( SSL_CLIENT_HELLO_HRR_REQUIRED );
870
871 return( 0 );
872}
873
874static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl,
875 int hrr_required )
876{
877 int ret = 0;
878
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000879 if( ssl->handshake->hello_retry_requests_sent == 0 &&
XiaokangQian7807f9f2022-02-15 10:04:37 +0000880 ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON )
881 {
882 hrr_required = SSL_CLIENT_HELLO_HRR_REQUIRED;
883 }
884
885 if( hrr_required == SSL_CLIENT_HELLO_HRR_REQUIRED )
886 {
887 /*
888 * Create stateless transcript hash for HRR
889 */
890 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
891 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
892 if( ret != 0 )
893 {
894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr",
895 ret );
896 return( ret );
897 }
898 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
899
900 /* Transmit Hello Retry Request */
901 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
902 return( 0 );
903 }
904
905 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
906 if( ret != 0 )
907 {
908 MBEDTLS_SSL_DEBUG_RET( 1,
909 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
910 return( ret );
911 }
912
913 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
914 return( 0 );
915
916}
917
918/*
919 * TLS and DTLS 1.3 State Maschine -- server side
920 */
Jerry Yu27561932021-08-27 17:07:38 +0800921int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +0800922{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000923 int ret = 0;
924
925 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
926 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
927
Jerry Yue3b34122021-09-28 17:53:35 +0800928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
929 mbedtls_ssl_states_str( ssl->state ),
930 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +0800931
XiaokangQian7807f9f2022-02-15 10:04:37 +0000932 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
933 return( ret );
934
935 switch( ssl->state )
936 {
937 /* start state */
938 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7ac3ab32022-02-22 04:03:26 +0000939 ssl->handshake->hello_retry_requests_sent = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000940 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
941
942 break;
943
944 /* ----- READ CLIENT HELLO ----*/
945
946 case MBEDTLS_SSL_CLIENT_HELLO:
947
948 ret = ssl_client_hello_process( ssl );
949 if( ret != 0 )
950 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_client_hello_process", ret );
951
952 break;
953
954 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
956
957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for all traffic" ) );
958
959 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
960 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
961
962 mbedtls_ssl_tls13_handshake_wrapup( ssl );
963 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET );
964
965 break;
966
967 default:
968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
969 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
970 }
971
972 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +0800973}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800974
Jerry Yufb4b6472022-01-27 15:03:26 +0800975#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */