blob: bbc85357906fa394b86bbd664425d09709a3231f [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"
Xiaofei Baicba64af2022-02-15 10:00:56 +000025#include "mbedtls/error.h"
26#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000027#include "mbedtls/constant_time.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080028
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000029#include "ssl_misc.h"
30#include "ssl_tls13_keys.h"
31#include "ssl_debug_helpers.h"
32
XiaokangQian7807f9f2022-02-15 10:04:37 +000033#if defined(MBEDTLS_ECP_C)
34#include "mbedtls/ecp.h"
XiaokangQian7807f9f2022-02-15 10:04:37 +000035#endif /* MBEDTLS_ECP_C */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080036
XiaokangQiana9c58412022-02-17 09:41:26 +000037#if defined(MBEDTLS_PLATFORM_C)
38#include "mbedtls/platform.h"
39#else
40#include <stdlib.h>
41#define mbedtls_calloc calloc
42#define mbedtls_free free
43#endif /* MBEDTLS_PLATFORM_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +000044
Jerry Yu4d3841a2022-04-16 12:37:19 +080045#include "ssl_misc.h"
46#include "ssl_tls13_keys.h"
47#include "ssl_debug_helpers.h"
48
Jerry Yue19e3b92022-07-08 12:04:51 +000049#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
50/* From RFC 8446:
51 *
52 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
53 * struct {
54 * PskKeyExchangeMode ke_modes<1..255>;
55 * } PskKeyExchangeModes;
56 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080057MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue19e3b92022-07-08 12:04:51 +000058static int ssl_tls13_parse_key_exchange_modes_ext( mbedtls_ssl_context *ssl,
59 const unsigned char *buf,
Jerry Yu299e31f2022-07-13 23:06:36 +080060 const unsigned char *end )
Jerry Yue19e3b92022-07-08 12:04:51 +000061{
Jerry Yu299e31f2022-07-13 23:06:36 +080062 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000063 size_t ke_modes_len;
64 int ke_modes = 0;
65
Jerry Yu854dd9e2022-07-15 14:28:27 +080066 /* Read ke_modes length (1 Byte) */
Jerry Yu299e31f2022-07-13 23:06:36 +080067 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
68 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000069 /* Currently, there are only two PSK modes, so even without looking
70 * at the content, something's wrong if the list has more than 2 items. */
71 if( ke_modes_len > 2 )
Jerry Yu299e31f2022-07-13 23:06:36 +080072 {
73 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
74 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue19e3b92022-07-08 12:04:51 +000075 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu299e31f2022-07-13 23:06:36 +080076 }
Jerry Yue19e3b92022-07-08 12:04:51 +000077
Jerry Yu299e31f2022-07-13 23:06:36 +080078 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ke_modes_len );
Jerry Yue19e3b92022-07-08 12:04:51 +000079
80 while( ke_modes_len-- != 0 )
81 {
Jerry Yu299e31f2022-07-13 23:06:36 +080082 switch( *p++ )
Jerry Yue19e3b92022-07-08 12:04:51 +000083 {
84 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
85 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Found PSK KEX MODE" ) );
87 break;
88 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
89 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Found PSK_EPHEMERAL KEX MODE" ) );
91 break;
92 default:
Jerry Yu299e31f2022-07-13 23:06:36 +080093 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
94 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue19e3b92022-07-08 12:04:51 +000095 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
96 }
97 }
98
99 ssl->handshake->tls13_kex_modes = ke_modes;
100 return( 0 );
101}
Jerry Yu1c105562022-07-10 06:32:38 +0000102
103#define SSL_TLS1_3_OFFERED_PSK_NOT_MATCH 0
104#define SSL_TLS1_3_OFFERED_PSK_MATCH 1
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800105MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000106static int ssl_tls13_offered_psks_check_identity_match(
107 mbedtls_ssl_context *ssl,
108 const unsigned char *identity,
Jerry Yu2f0abc92022-07-22 19:34:48 +0800109 size_t identity_len )
Jerry Yu1c105562022-07-10 06:32:38 +0000110{
111 /* Check identity with external configured function */
112 if( ssl->conf->f_psk != NULL )
113 {
114 if( ssl->conf->f_psk(
115 ssl->conf->p_psk, ssl, identity, identity_len ) == 0 )
116 {
117 return( SSL_TLS1_3_OFFERED_PSK_MATCH );
118 }
119 return( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH );
120 }
121
Jerry Yu96a2e362022-07-21 15:11:34 +0800122 MBEDTLS_SSL_DEBUG_BUF( 5, "identity", identity, identity_len );
Jerry Yu1c105562022-07-10 06:32:38 +0000123 /* Check identity with pre-configured psk */
Jerry Yu2f0abc92022-07-22 19:34:48 +0800124 if( ssl->conf->psk_identity != NULL &&
125 identity_len == ssl->conf->psk_identity_len &&
Jerry Yu1c105562022-07-10 06:32:38 +0000126 mbedtls_ct_memcmp( ssl->conf->psk_identity,
127 identity, identity_len ) == 0 )
128 {
129 mbedtls_ssl_set_hs_psk( ssl, ssl->conf->psk, ssl->conf->psk_len );
130 return( SSL_TLS1_3_OFFERED_PSK_MATCH );
131 }
132
Jerry Yu1c105562022-07-10 06:32:38 +0000133 return( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH );
134}
135
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800136MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu96a2e362022-07-21 15:11:34 +0800137static int ssl_tls13_get_psk( mbedtls_ssl_context *ssl,
Jerry Yu2f0abc92022-07-22 19:34:48 +0800138 unsigned char **psk,
Jerry Yu96a2e362022-07-21 15:11:34 +0800139 size_t *psk_len )
140{
141#if defined(MBEDTLS_USE_PSA_CRYPTO)
142 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
143 psa_status_t status;
144
145 *psk_len = 0;
146 *psk = NULL;
147
148 status = psa_get_key_attributes( ssl->handshake->psk_opaque, &key_attributes );
149 if( status != PSA_SUCCESS)
150 {
151 return( psa_ssl_status_to_mbedtls( status ) );
152 }
153
Jerry Yu2f0abc92022-07-22 19:34:48 +0800154 *psk_len = PSA_BITS_TO_BYTES( psa_get_key_bits( &key_attributes ) );
Jerry Yu96a2e362022-07-21 15:11:34 +0800155 *psk = mbedtls_calloc( 1, *psk_len );
156 if( *psk == NULL )
157 {
158 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
159 }
160
161 status = psa_export_key( ssl->handshake->psk_opaque,
162 (uint8_t *)*psk, *psk_len, psk_len );
163 if( status != PSA_SUCCESS)
164 {
165 mbedtls_free( (void *)*psk );
166 return( psa_ssl_status_to_mbedtls( status ) );
167 }
168#else
169 *psk = ssl->handshake->psk;
170 *psk_len = ssl->handshake->psk_len;
171#endif /* !MBEDTLS_USE_PSA_CRYPTO */
172 return( 0 );
173}
174
175MBEDTLS_CHECK_RETURN_CRITICAL
176static int ssl_tls13_offered_psks_check_binder_match( mbedtls_ssl_context *ssl,
177 const unsigned char *binder,
Jerry Yu2f0abc92022-07-22 19:34:48 +0800178 size_t binder_len )
Jerry Yu1c105562022-07-10 06:32:38 +0000179{
180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
181 int psk_type;
Jerry Yu96a2e362022-07-21 15:11:34 +0800182
Jerry Yu2f0abc92022-07-22 19:34:48 +0800183 mbedtls_md_type_t md_alg;
184 psa_algorithm_t psa_md_alg;
Jerry Yudaf375a2022-07-20 21:31:43 +0800185 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000186 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800187 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800188 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800189 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000190
Jerry Yudaf375a2022-07-20 21:31:43 +0800191 psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800192 switch( binder_len )
193 {
194 case 32:
195 md_alg = MBEDTLS_MD_SHA256;
196 break;
197 case 48:
198 md_alg = MBEDTLS_MD_SHA384;
199 break;
200 default:
201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
202 }
203 psa_md_alg = mbedtls_psa_translate_md( md_alg );
Jerry Yu1c105562022-07-10 06:32:38 +0000204 /* Get current state of handshake transcript. */
205 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_alg,
206 transcript, sizeof( transcript ),
207 &transcript_len );
208 if( ret != 0 )
209 return( ret );
210
Jerry Yu96a2e362022-07-21 15:11:34 +0800211 ret = ssl_tls13_get_psk( ssl, &psk, &psk_len );
212 if( ret != 0 )
213 return( ret );
214
Jerry Yu1c105562022-07-10 06:32:38 +0000215 ret = mbedtls_ssl_tls13_create_psk_binder( ssl, psa_md_alg,
216 psk, psk_len, psk_type,
217 transcript,
218 server_computed_binder );
Jerry Yu96a2e362022-07-21 15:11:34 +0800219#if defined(MBEDTLS_USE_PSA_CRYPTO)
220 mbedtls_free( (void*)psk );
221#endif
Jerry Yu1c105562022-07-10 06:32:38 +0000222 if( ret != 0 )
223 {
224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "PSK binder calculation failed." ) );
225 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
226 }
227
228 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder ( computed ): ",
Jerry Yu2f0abc92022-07-22 19:34:48 +0800229 server_computed_binder, transcript_len );
Jerry Yu1c105562022-07-10 06:32:38 +0000230 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder ( received ): ", binder, binder_len );
231
232 if( mbedtls_ct_memcmp( server_computed_binder, binder, binder_len ) == 0 )
233 {
234 return( SSL_TLS1_3_OFFERED_PSK_MATCH );
235 }
236
Jerry Yudaf375a2022-07-20 21:31:43 +0800237 mbedtls_platform_zeroize( server_computed_binder,
238 sizeof( server_computed_binder ) );
Jerry Yu1c105562022-07-10 06:32:38 +0000239 return( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH );
240}
Jerry Yu96a2e362022-07-21 15:11:34 +0800241
Jerry Yu1c105562022-07-10 06:32:38 +0000242/* Parser for pre_shared_key extension in client hello
243 * struct {
244 * opaque identity<1..2^16-1>;
245 * uint32 obfuscated_ticket_age;
246 * } PskIdentity;
247 *
248 * opaque PskBinderEntry<32..255>;
249 *
250 * struct {
251 * PskIdentity identities<7..2^16-1>;
252 * PskBinderEntry binders<33..2^16-1>;
253 * } OfferedPsks;
254 *
255 * struct {
256 * select (Handshake.msg_type) {
257 * case client_hello: OfferedPsks;
258 * ....
259 * };
260 * } PreSharedKeyExtension;
261 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800262MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yubb852022022-07-20 21:10:44 +0800263static int ssl_tls13_parse_pre_shared_key_ext( mbedtls_ssl_context *ssl,
264 const unsigned char *buf,
265 const unsigned char *end )
Jerry Yu1c105562022-07-10 06:32:38 +0000266{
Jerry Yu96a2e362022-07-21 15:11:34 +0800267 const unsigned char *next_identity = buf;
Jerry Yu1c105562022-07-10 06:32:38 +0000268 uint16_t identities_len;
269 const unsigned char *identities_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800270 const unsigned char *next_binder;
Jerry Yu1c105562022-07-10 06:32:38 +0000271 uint16_t binders_len;
272 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800273 int matched_identity = -1;
274 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000275
Jerry Yu2f0abc92022-07-22 19:34:48 +0800276 MBEDTLS_SSL_DEBUG_BUF( 3, "pre_shared_key extension", buf, end - buf );
Jerry Yu1c105562022-07-10 06:32:38 +0000277
Jerry Yu96a2e362022-07-21 15:11:34 +0800278 /* identities_len 2 bytes
279 * identities_data >= 7 bytes
280 */
281 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_identity, end, 7 + 2 );
282 identities_len = MBEDTLS_GET_UINT16_BE( next_identity, 0 );
283 next_identity += 2;
284 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_identity, end, identities_len );
285 identities_end = next_identity + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000286
Jerry Yu96a2e362022-07-21 15:11:34 +0800287 /* binders_len 2 bytes
288 * binders >= 33 bytes
289 */
290 next_binder = identities_end;
291 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_binder, end, 33 );
292 binders_len = MBEDTLS_GET_UINT16_BE( next_binder, 0 );
293 next_binder += 2;
294 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_binder, end, binders_len );
295 binders_end = next_binder + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000296
Jerry Yu96a2e362022-07-21 15:11:34 +0800297 ssl->handshake->update_checksum( ssl, buf, identities_end - buf );
298
299 while( next_identity < identities_end && next_binder < binders_end )
Jerry Yu1c105562022-07-10 06:32:38 +0000300 {
Jerry Yu1c105562022-07-10 06:32:38 +0000301 const unsigned char *identity;
Jerry Yu96a2e362022-07-21 15:11:34 +0800302 uint16_t identity_len;
303 const unsigned char *binder;
304 uint16_t binder_len;
305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yubb852022022-07-20 21:10:44 +0800306
Jerry Yu96a2e362022-07-21 15:11:34 +0800307 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_identity, identities_end, 2 );
308 identity_len = MBEDTLS_GET_UINT16_BE( next_identity, 0 );
309 next_identity += 2;
310 identity = next_identity;
311 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_identity,
Jerry Yu352cd7d2022-07-20 22:11:00 +0800312 identities_end,
313 identity_len + 4 );
Jerry Yu96a2e362022-07-21 15:11:34 +0800314 next_identity += identity_len + 4;
Jerry Yu1c105562022-07-10 06:32:38 +0000315
Jerry Yu96a2e362022-07-21 15:11:34 +0800316 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_binder, binders_end, 2 );
317
318 binder_len = *next_binder++;
319 binder = next_binder;
320 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_binder, binders_end, binder_len );
321 next_binder += binder_len;
322
323 identity_id++;
324 if( matched_identity != -1 )
Jerry Yu1c105562022-07-10 06:32:38 +0000325 continue;
326
Jerry Yu96a2e362022-07-21 15:11:34 +0800327 ret = ssl_tls13_offered_psks_check_identity_match(
328 ssl, identity, identity_len );
329 if( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH == ret )
330 continue;
331
332 ret = ssl_tls13_offered_psks_check_binder_match(
333 ssl, binder, binder_len );
334 if( ret < 0 )
335 {
336 MBEDTLS_SSL_DEBUG_RET( 1,
337 "ssl_tls13_offered_psks_check_binder_match" , ret );
338 MBEDTLS_SSL_PEND_FATAL_ALERT(
339 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
340 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
341 return( ret );
342 }
343 if( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH == ret )
344 continue;
345
346 matched_identity = identity_id;
Jerry Yu1c105562022-07-10 06:32:38 +0000347 }
348
Jerry Yu96a2e362022-07-21 15:11:34 +0800349 if( next_identity != identities_end || next_binder != binders_end )
Jerry Yu1c105562022-07-10 06:32:38 +0000350 {
351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key extesion decode error" ) );
352 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
353 MBEDTLS_ERR_SSL_DECODE_ERROR );
354 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
355 }
356
Jerry Yu96a2e362022-07-21 15:11:34 +0800357 if( matched_identity == -1 )
Jerry Yu1c105562022-07-10 06:32:38 +0000358 {
Jerry Yu96a2e362022-07-21 15:11:34 +0800359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "No matched pre shared key found" ) );
Jerry Yu1c105562022-07-10 06:32:38 +0000360 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu96a2e362022-07-21 15:11:34 +0800361 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY,
362 MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
363 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Jerry Yu1c105562022-07-10 06:32:38 +0000364 }
365
Jerry Yu96a2e362022-07-21 15:11:34 +0800366 ssl->handshake->selected_identity = (uint16_t)matched_identity;
367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Pre shared key found" ) );
Jerry Yu1c105562022-07-10 06:32:38 +0000368
369 /* Update the handshake transcript with the binder list. */
370 ssl->handshake->update_checksum( ssl,
371 identities_end,
Jerry Yu96a2e362022-07-21 15:11:34 +0800372 (size_t)( binders_end - identities_end ) );
373 return( 0 );
Jerry Yu1c105562022-07-10 06:32:38 +0000374}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000375
376/*
377 * struct {
378 * select ( Handshake.msg_type ) {
379 * ....
380 * case server_hello:
381 * uint16 selected_identity;
382 * }
383 * } PreSharedKeyExtension;
384 */
Jerry Yubb852022022-07-20 21:10:44 +0800385static int ssl_tls13_write_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
386 unsigned char *buf,
387 unsigned char *end,
388 size_t *olen )
Jerry Yu032b15ce2022-07-11 06:10:03 +0000389{
390 unsigned char *p = (unsigned char*)buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000391
392 *olen = 0;
393
394#if defined(MBEDTLS_USE_PSA_CRYPTO)
395 if( mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
396#else
397 if( ssl->handshake->psk == NULL )
398#endif
399 {
400 /* We shouldn't have called this extension writer unless we've
401 * chosen to use a PSK. */
402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
403 }
404
405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding pre_shared_key extension" ) );
406 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
407
Jerry Yu032b15ce2022-07-11 06:10:03 +0000408 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0 );
Jerry Yu032b15ce2022-07-11 06:10:03 +0000409 MBEDTLS_PUT_UINT16_BE( 2, p, 2 );
410
Jerry Yu96a2e362022-07-21 15:11:34 +0800411 MBEDTLS_PUT_UINT16_BE( ssl->handshake->selected_identity, p, 4 );
Jerry Yu032b15ce2022-07-11 06:10:03 +0000412
413 *olen = 6;
414
Jerry Yu96a2e362022-07-21 15:11:34 +0800415 MBEDTLS_SSL_DEBUG_MSG( 4, ( "sent selected_identity: %u",
416 ssl->handshake->selected_identity ) );
Jerry Yu032b15ce2022-07-11 06:10:03 +0000417
418 return( 0 );
419}
420
Jerry Yue19e3b92022-07-08 12:04:51 +0000421#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
422
XiaokangQian7807f9f2022-02-15 10:04:37 +0000423/* From RFC 8446:
424 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000425 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000426 * } SupportedVersions;
427 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200428MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian7807f9f2022-02-15 10:04:37 +0000429static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
430 const unsigned char *buf,
431 const unsigned char *end )
432{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000433 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000434 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000435 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000436 uint16_t tls_version;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000437 int tls13_supported = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000438
439 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000440 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000441 p += 1;
442
XiaokangQian4080a7f2022-04-11 09:55:18 +0000443 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, versions_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000444 versions_end = p + versions_len;
445 while( p < versions_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000446 {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000447 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, versions_end, 2 );
XiaokangQiande333912022-04-20 08:49:42 +0000448 tls_version = mbedtls_ssl_read_version( p, ssl->conf->transport );
XiaokangQianb67384d2022-04-19 00:02:38 +0000449 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000450
451 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
XiaokangQiande333912022-04-20 08:49:42 +0000452 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000453 {
454 tls13_supported = 1;
455 break;
456 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000457 }
458
XiaokangQianb67384d2022-04-19 00:02:38 +0000459 if( !tls13_supported )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000460 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
462
463 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
464 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
465 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
466 }
467
XiaokangQiande333912022-04-20 08:49:42 +0000468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%04x]",
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000469 (unsigned int)tls_version ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000470
XiaokangQian7807f9f2022-02-15 10:04:37 +0000471 return( 0 );
472}
473
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000474#if defined(MBEDTLS_ECDH_C)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000475/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000476 *
477 * From RFC 8446:
478 * enum {
479 * ... (0xFFFF)
480 * } NamedGroup;
481 * struct {
482 * NamedGroup named_group_list<2..2^16-1>;
483 * } NamedGroupList;
484 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200485MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc1be19f2022-04-23 16:11:39 +0800486static int ssl_tls13_parse_supported_groups_ext( mbedtls_ssl_context *ssl,
487 const unsigned char *buf,
488 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000489{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000490 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000491 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000492 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000493
494 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000495 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000496 named_group_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000497 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000498 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, named_group_list_len );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000499 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000500 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000501
XiaokangQian08037552022-04-20 07:16:41 +0000502 while( p < named_group_list_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000503 {
XiaokangQian08037552022-04-20 07:16:41 +0000504 uint16_t named_group;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000505 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, named_group_list_end, 2 );
XiaokangQian08037552022-04-20 07:16:41 +0000506 named_group = MBEDTLS_GET_UINT16_BE( p, 0 );
507 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000508
Jerry Yuc1be19f2022-04-23 16:11:39 +0800509 MBEDTLS_SSL_DEBUG_MSG( 2,
510 ( "got named group: %s(%04x)",
511 mbedtls_ssl_named_group_to_str( named_group ),
512 named_group ) );
XiaokangQian08037552022-04-20 07:16:41 +0000513
514 if( ! mbedtls_ssl_named_group_is_offered( ssl, named_group ) ||
515 ! mbedtls_ssl_named_group_is_supported( named_group ) ||
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000516 ssl->handshake->hrr_selected_group != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000517 {
XiaokangQian08037552022-04-20 07:16:41 +0000518 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000519 }
520
Jerry Yuc1be19f2022-04-23 16:11:39 +0800521 MBEDTLS_SSL_DEBUG_MSG( 2,
522 ( "add named group %s(%04x) into received list.",
523 mbedtls_ssl_named_group_to_str( named_group ),
524 named_group ) );
525
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000526 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000527 }
528
529 return( 0 );
530
531}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000532#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000533
XiaokangQian08037552022-04-20 07:16:41 +0000534#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
535
XiaokangQian88408882022-04-02 10:15:03 +0000536#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000537/*
538 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
XiaokangQiane8ff3502022-04-22 02:34:40 +0000539 * extension is correct and stores the first acceptable key share and its associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000540 *
541 * Possible return values are:
542 * - 0: Successful processing of the client provided key share extension.
XiaokangQian08037552022-04-20 07:16:41 +0000543 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by the client
XiaokangQian7807f9f2022-02-15 10:04:37 +0000544 * does not match a group supported by the server. A HelloRetryRequest will
545 * be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000546 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800547 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200548MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian7807f9f2022-02-15 10:04:37 +0000549static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
550 const unsigned char *buf,
551 const unsigned char *end )
552{
XiaokangQianb67384d2022-04-19 00:02:38 +0000553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000554 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000555 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800556 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000557
558 /* From RFC 8446:
559 *
560 * struct {
561 * KeyShareEntry client_shares<0..2^16-1>;
562 * } KeyShareClientHello;
563 *
564 */
565
XiaokangQian7807f9f2022-02-15 10:04:37 +0000566 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000567 client_shares_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000568 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000569 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, client_shares_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000570
571 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000572 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000573
574 /* We try to find a suitable key share entry and copy it to the
575 * handshake context. Later, we have to find out whether we can do
576 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000577 * dismiss it and send a HelloRetryRequest message.
578 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000579
Jerry Yuc1be19f2022-04-23 16:11:39 +0800580 while( p < client_shares_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000581 {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000582 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800583 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800584 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000585
586 /*
587 * struct {
588 * NamedGroup group;
589 * opaque key_exchange<1..2^16-1>;
590 * } KeyShareEntry;
591 */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000592 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, 4 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000593 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yuc1be19f2022-04-23 16:11:39 +0800594 key_exchange_len = MBEDTLS_GET_UINT16_BE( p, 2 );
595 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800596 key_exchange = p;
XiaokangQianb67384d2022-04-19 00:02:38 +0000597 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, key_exchange_len );
Jerry Yu086edc22022-05-05 10:50:38 +0800598 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000599
600 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000601 * for input validation purposes.
602 */
XiaokangQian060d8672022-04-21 09:24:56 +0000603 if( ! mbedtls_ssl_named_group_is_offered( ssl, group ) ||
Jerry Yuc1be19f2022-04-23 16:11:39 +0800604 ! mbedtls_ssl_named_group_is_supported( group ) ||
605 ssl->handshake->offered_group_id != 0 )
XiaokangQian060d8672022-04-21 09:24:56 +0000606 {
607 continue;
608 }
XiaokangQian060d8672022-04-21 09:24:56 +0000609
XiaokangQian7807f9f2022-02-15 10:04:37 +0000610 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000611 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000612 */
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000613 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
614 {
Jerry Yuc1be19f2022-04-23 16:11:39 +0800615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH group: %s (%04x)",
616 mbedtls_ssl_named_group_to_str( group ),
617 group ) );
XiaokangQian318dc762022-04-20 09:43:51 +0000618 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Jerry Yu086edc22022-05-05 10:50:38 +0800619 ssl, key_exchange - 2, key_exchange_len + 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000620 if( ret != 0 )
621 return( ret );
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000622
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000623 }
624 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000625 {
626 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000627 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000628 continue;
629 }
630
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000631 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000632 }
633
Jerry Yuc1be19f2022-04-23 16:11:39 +0800634
635 if( ssl->handshake->offered_group_id == 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000636 {
637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
XiaokangQian08037552022-04-20 07:16:41 +0000638 return( SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000639 }
640 return( 0 );
641}
XiaokangQian88408882022-04-02 10:15:03 +0000642#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000643
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000644#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000645static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000646{
XiaokangQian3207a322022-02-23 03:15:27 +0000647 ((void) ssl);
648
XiaokangQian7807f9f2022-02-15 10:04:37 +0000649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
XiaokangQianb67384d2022-04-19 00:02:38 +0000650 MBEDTLS_SSL_DEBUG_MSG( 3,
651 ( "- KEY_SHARE_EXTENSION ( %s )",
652 ( ( ssl->handshake->extensions_present
653 & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ? "TRUE" : "FALSE" ) );
654 MBEDTLS_SSL_DEBUG_MSG( 3,
655 ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
656 ( ( ssl->handshake->extensions_present
657 & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
658 "TRUE" : "FALSE" ) );
659 MBEDTLS_SSL_DEBUG_MSG( 3,
660 ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
661 ( ( ssl->handshake->extensions_present
662 & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ? "TRUE" : "FALSE" ) );
663 MBEDTLS_SSL_DEBUG_MSG( 3,
664 ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
665 ( ( ssl->handshake->extensions_present
666 & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ? "TRUE" : "FALSE" ) );
667 MBEDTLS_SSL_DEBUG_MSG( 3,
668 ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
669 ( ( ssl->handshake->extensions_present
670 & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
671 "TRUE" : "FALSE" ) );
672 MBEDTLS_SSL_DEBUG_MSG( 3,
673 ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
674 ( ( ssl->handshake->extensions_present
675 & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
676 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000677#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
XiaokangQianb67384d2022-04-19 00:02:38 +0000678 MBEDTLS_SSL_DEBUG_MSG( 3,
679 ( "- SERVERNAME_EXTENSION ( %s )",
680 ( ( ssl->handshake->extensions_present
681 & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
682 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000683#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianacb39922022-06-17 10:18:48 +0000684#if defined ( MBEDTLS_SSL_ALPN )
685 MBEDTLS_SSL_DEBUG_MSG( 3,
686 ( "- ALPN_EXTENSION ( %s )",
687 ( ( ssl->handshake->extensions_present
688 & MBEDTLS_SSL_EXT_ALPN ) > 0 ) ?
689 "TRUE" : "FALSE" ) );
690#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000691}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000692#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000693
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200694MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian4080a7f2022-04-11 09:55:18 +0000695static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000696 int exts_mask )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000697{
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000698 int masked = ssl->handshake->extensions_present & exts_mask;
699 return( masked == exts_mask );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000700}
701
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200702MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000703static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
704 mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000705{
Jerry Yu77f01482022-07-11 07:03:24 +0000706 return( ssl_tls13_client_hello_has_exts(
707 ssl,
708 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
709 MBEDTLS_SSL_EXT_KEY_SHARE |
710 MBEDTLS_SSL_EXT_SIG_ALG ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000711}
712
Jerry Yu77f01482022-07-11 07:03:24 +0000713#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
714MBEDTLS_CHECK_RETURN_CRITICAL
715static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
716 mbedtls_ssl_context *ssl )
717{
718 return( ssl_tls13_client_hello_has_exts(
719 ssl,
720 MBEDTLS_SSL_EXT_PRE_SHARED_KEY |
721 MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) );
722}
723
724MBEDTLS_CHECK_RETURN_CRITICAL
725static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
726 mbedtls_ssl_context *ssl )
727{
728 return( ssl_tls13_client_hello_has_exts(
729 ssl,
730 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
731 MBEDTLS_SSL_EXT_KEY_SHARE |
732 MBEDTLS_SSL_EXT_PRE_SHARED_KEY |
733 MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) );
734}
735#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
736
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200737MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000738static int ssl_tls13_check_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000739{
Jerry Yu77f01482022-07-11 07:03:24 +0000740 return( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) &&
741 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange( ssl ) );
742}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000743
Jerry Yu77f01482022-07-11 07:03:24 +0000744MBEDTLS_CHECK_RETURN_CRITICAL
745static int ssl_tls13_check_psk_key_exchange( mbedtls_ssl_context *ssl )
746{
747#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
748 return( mbedtls_ssl_conf_tls13_psk_enabled( ssl ) &&
749 mbedtls_ssl_tls13_psk_enabled( ssl ) &&
750 ssl_tls13_client_hello_has_exts_for_psk_key_exchange( ssl ) );
751#else
752 ((void) ssl);
753 return( 0 );
754#endif
755}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000756
Jerry Yu77f01482022-07-11 07:03:24 +0000757MBEDTLS_CHECK_RETURN_CRITICAL
758static int ssl_tls13_check_psk_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
759{
760#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
761 return( mbedtls_ssl_conf_tls13_psk_ephemeral_enabled( ssl ) &&
762 mbedtls_ssl_tls13_psk_ephemeral_enabled( ssl ) &&
763 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange( ssl ) );
764#else
765 ((void) ssl);
766 return( 0 );
767#endif
768}
769
770static int ssl_tls13_determine_key_exchange_mode( mbedtls_ssl_context *ssl )
771{
772 /*
773 * Determine the key exchange algorithm to use.
774 * There are three types of key exchanges supported in TLS 1.3:
775 * - (EC)DH with ECDSA,
776 * - (EC)DH with PSK,
777 * - plain PSK.
778 *
779 * The PSK-based key exchanges may additionally be used with 0-RTT.
780 *
781 * Our built-in order of preference is
782 * 1 ) Plain PSK Mode ( psk )
783 * 2 ) (EC)DHE-PSK Mode ( psk_ephemeral )
784 * 3 ) Certificate Mode ( ephemeral )
785 */
786
787 ssl->handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
788
789 if( ssl_tls13_check_psk_key_exchange( ssl ) )
790 {
791 ssl->handshake->key_exchange_mode =
792 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk" ) );
794 }
795 else
796 if( ssl_tls13_check_psk_ephemeral_key_exchange( ssl ) )
797 {
798 ssl->handshake->key_exchange_mode =
799 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk_ephemeral" ) );
801 }
802 else
803 if( ssl_tls13_check_ephemeral_key_exchange( ssl ) )
804 {
805 ssl->handshake->key_exchange_mode =
806 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: ephemeral" ) );
808 }
809 else
810 {
811 MBEDTLS_SSL_DEBUG_MSG(
812 1,
813 ( "ClientHello message misses mandatory extensions." ) );
814 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
815 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
816 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
817 }
818
819 return( 0 );
820
XiaokangQian7807f9f2022-02-15 10:04:37 +0000821}
822
XiaokangQian81802f42022-06-10 13:25:22 +0000823#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
824 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian23c5be62022-06-07 02:04:34 +0000825/*
XiaokangQianfb665a82022-06-15 03:57:21 +0000826 * Pick best ( private key, certificate chain ) pair based on the signature
827 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +0000828 */
Ronald Cronce7d76e2022-07-08 18:56:49 +0200829MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian07aad072022-06-14 05:35:09 +0000830static int ssl_tls13_pick_key_cert( mbedtls_ssl_context *ssl )
XiaokangQian23c5be62022-06-07 02:04:34 +0000831{
XiaokangQianfb665a82022-06-15 03:57:21 +0000832 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +0000833 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +0000834
835#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
836 if( ssl->handshake->sni_key_cert != NULL )
XiaokangQianfb665a82022-06-15 03:57:21 +0000837 key_cert_list = ssl->handshake->sni_key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000838 else
839#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianfb665a82022-06-15 03:57:21 +0000840 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000841
XiaokangQianfb665a82022-06-15 03:57:21 +0000842 if( key_cert_list == NULL )
XiaokangQian23c5be62022-06-07 02:04:34 +0000843 {
844 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
845 return( -1 );
846 }
847
XiaokangQian81802f42022-06-10 13:25:22 +0000848 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
XiaokangQian23c5be62022-06-07 02:04:34 +0000849 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000850 for( key_cert = key_cert_list; key_cert != NULL;
851 key_cert = key_cert->next )
XiaokangQian23c5be62022-06-07 02:04:34 +0000852 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000853 MBEDTLS_SSL_DEBUG_CRT( 3, "certificate (chain) candidate",
854 key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000855
856 /*
857 * This avoids sending the client a cert it'll reject based on
858 * keyUsage or other extensions.
859 */
860 if( mbedtls_x509_crt_check_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000861 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +0000862 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000863 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
864 MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ) ) != 0 )
XiaokangQian81802f42022-06-10 13:25:22 +0000865 {
866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
XiaokangQianfb665a82022-06-15 03:57:21 +0000867 "(extended) key usage extension" ) );
XiaokangQian81802f42022-06-10 13:25:22 +0000868 continue;
869 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000870
Jerry Yucc539102022-06-27 16:27:35 +0800871 MBEDTLS_SSL_DEBUG_MSG( 3,
872 ( "ssl_tls13_pick_key_cert:"
873 "check signature algorithm %s [%04x]",
874 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
875 *sig_alg ) );
Jerry Yufb526692022-06-19 11:22:49 +0800876 if( mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
Jerry Yud099cf02022-06-19 13:47:00 +0800877 *sig_alg, &key_cert->cert->pk ) )
XiaokangQian81802f42022-06-10 13:25:22 +0000878 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000879 ssl->handshake->key_cert = key_cert;
Jerry Yucc539102022-06-27 16:27:35 +0800880 MBEDTLS_SSL_DEBUG_MSG( 3,
881 ( "ssl_tls13_pick_key_cert:"
882 "selected signature algorithm"
883 " %s [%04x]",
884 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
885 *sig_alg ) );
XiaokangQianfb665a82022-06-15 03:57:21 +0000886 MBEDTLS_SSL_DEBUG_CRT(
XiaokangQian75fe8c72022-06-15 09:42:45 +0000887 3, "selected certificate (chain)",
XiaokangQianfb665a82022-06-15 03:57:21 +0000888 ssl->handshake->key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000889 return( 0 );
890 }
XiaokangQian81802f42022-06-10 13:25:22 +0000891 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000892 }
893
Jerry Yu9d3e2fa2022-06-27 22:14:01 +0800894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ssl_tls13_pick_key_cert:"
895 "no suitable certificate found" ) );
XiaokangQian23c5be62022-06-07 02:04:34 +0000896 return( -1 );
897}
XiaokangQian81802f42022-06-10 13:25:22 +0000898#endif /* MBEDTLS_X509_CRT_PARSE_C &&
899 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +0000900
XiaokangQian4080a7f2022-04-11 09:55:18 +0000901/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000902 *
903 * STATE HANDLING: ClientHello
904 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000905 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000906 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000907 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000908 *
909 * In this case, the server progresses to sending its ServerHello.
910 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000911 * 2) The ClientHello was well-formed but didn't match the server's
912 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000913 *
914 * For example, the client might not have offered a key share which
915 * the server supports, or the server might require a cookie.
916 *
917 * In this case, the server sends a HelloRetryRequest.
918 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000919 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000920 *
921 * In this case, we abort the handshake.
922 *
923 */
924
925/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000926 * Structure of this message:
927 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000928 * uint16 ProtocolVersion;
929 * opaque Random[32];
930 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +0000931 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000932 * struct {
933 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
934 * Random random;
935 * opaque legacy_session_id<0..32>;
936 * CipherSuite cipher_suites<2..2^16-2>;
937 * opaque legacy_compression_methods<1..2^8-1>;
938 * Extension extensions<8..2^16-1>;
939 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000940 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000941
942#define SSL_CLIENT_HELLO_OK 0
943#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
944
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200945MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian4080a7f2022-04-11 09:55:18 +0000946static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
947 const unsigned char *buf,
948 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000949{
XiaokangQianb67384d2022-04-19 00:02:38 +0000950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
951 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000952 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000953 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000954 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000955 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000956 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000957 const unsigned char *extensions_end;
Jerry Yu49ca9282022-05-05 11:05:22 +0800958 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000959
XiaokangQian7807f9f2022-02-15 10:04:37 +0000960 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
Jerry Yu1c105562022-07-10 06:32:38 +0000961#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
962 const unsigned char *pre_shared_key_ext_start = NULL;
963 const unsigned char *pre_shared_key_ext_end = NULL;
964#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000965
966 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
967
968 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000969 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000970 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +0000971 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +0000972 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000973 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000974 * .. . .. ciphersuite list length ( 2 bytes )
975 * .. . .. ciphersuite list
976 * .. . .. compression alg. list length ( 1 byte )
977 * .. . .. compression alg. list
978 * .. . .. extensions length ( 2 bytes, optional )
979 * .. . .. extensions ( optional )
980 */
981
XiaokangQianb67384d2022-04-19 00:02:38 +0000982 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400983 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +0000984 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
985 * read at least up to session id length without worrying.
986 */
987 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
988
989 /* ...
990 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
991 * ...
992 * with ProtocolVersion defined as:
993 * uint16 ProtocolVersion;
994 */
XiaokangQiande333912022-04-20 08:49:42 +0000995 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
996 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000997 {
998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
999 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1000 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +00001001 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001002 }
1003 p += 2;
1004
1005 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +00001006 * Only support TLS 1.3 currently, temporarily set the version.
1007 */
XiaokangQiande333912022-04-20 08:49:42 +00001008 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +00001009
Jerry Yuc1be19f2022-04-23 16:11:39 +08001010 /* ...
1011 * Random random;
1012 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001013 * with Random defined as:
1014 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001015 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001016 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +00001017 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001018
XiaokangQian08037552022-04-20 07:16:41 +00001019 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
1020 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001021
Jerry Yuc1be19f2022-04-23 16:11:39 +08001022 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001023 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001024 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001025 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001026 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +00001027 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001028
XiaokangQianb67384d2022-04-19 00:02:38 +00001029 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001030 {
1031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1032 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1033 }
1034
XiaokangQian4080a7f2022-04-11 09:55:18 +00001035 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +00001036 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
XiaokangQiane8ff3502022-04-22 02:34:40 +00001037 p, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +00001038 /*
1039 * Check we have enough data for the legacy session identifier
1040 * and the ciphersuite list length.
1041 */
1042 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001043
XiaokangQianed582dd2022-04-13 08:21:05 +00001044 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +00001045 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001046
XiaokangQian7807f9f2022-02-15 10:04:37 +00001047 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1048 p += 2;
1049
XiaokangQianb67384d2022-04-19 00:02:38 +00001050 /* Check we have enough data for the ciphersuite list, the legacy
1051 * compression methods and the length of the extensions.
1052 */
1053 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001054
Jerry Yuc1be19f2022-04-23 16:11:39 +08001055 /* ...
XiaokangQian08037552022-04-20 07:16:41 +00001056 * CipherSuite cipher_suites<2..2^16-2>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001057 * ...
XiaokangQian08037552022-04-20 07:16:41 +00001058 * with CipherSuite defined as:
1059 * uint8 CipherSuite[2];
1060 */
XiaokangQian060d8672022-04-21 09:24:56 +00001061 cipher_suites = p;
1062 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001063 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1064 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +00001065 /*
1066 * Search for a matching ciphersuite
1067 */
XiaokangQian318dc762022-04-20 09:43:51 +00001068 int ciphersuite_match = 0;
XiaokangQian060d8672022-04-21 09:24:56 +00001069 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +00001070 {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001071 uint16_t cipher_suite;
1072 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, cipher_suites_end, 2 );
1073 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1074 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
XiaokangQian08037552022-04-20 07:16:41 +00001075 /*
XiaokangQiane8ff3502022-04-22 02:34:40 +00001076 * Check whether this ciphersuite is valid and offered.
1077 */
XiaokangQian08037552022-04-20 07:16:41 +00001078 if( ( mbedtls_ssl_validate_ciphersuite(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001079 ssl, ciphersuite_info, ssl->tls_version,
1080 ssl->tls_version ) != 0 ) ||
1081 ! mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
1082 {
XiaokangQian08037552022-04-20 07:16:41 +00001083 continue;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001084 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001085
XiaokangQian08037552022-04-20 07:16:41 +00001086 ssl->session_negotiate->ciphersuite = cipher_suite;
1087 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +00001088 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +00001089
XiaokangQian08037552022-04-20 07:16:41 +00001090 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001091
XiaokangQian17f974c2022-04-19 09:57:41 +00001092 }
1093
Jerry Yuc1be19f2022-04-23 16:11:39 +08001094 if( ! ciphersuite_match )
XiaokangQian318dc762022-04-20 09:43:51 +00001095 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +00001096 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1097 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1098 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +00001099 }
XiaokangQian17f974c2022-04-19 09:57:41 +00001100
1101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
1102 ciphersuite_info->name ) );
1103
XiaokangQian060d8672022-04-21 09:24:56 +00001104 p = cipher_suites + cipher_suites_len;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001105
XiaokangQian4080a7f2022-04-11 09:55:18 +00001106 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001107 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001108 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001109 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +00001110 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001111 {
XiaokangQian4080a7f2022-04-11 09:55:18 +00001112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
1113 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1114 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1115 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001116 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001117 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001118
Jerry Yuc1be19f2022-04-23 16:11:39 +08001119 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001120 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001121 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001122 * with Extension defined as:
1123 * struct {
1124 * ExtensionType extension_type;
1125 * opaque extension_data<0..2^16-1>;
1126 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001127 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001128 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001129 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001130 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQiane8ff3502022-04-22 02:34:40 +00001131 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001132
XiaokangQian4080a7f2022-04-11 09:55:18 +00001133 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001134
1135 while( p < extensions_end )
1136 {
1137 unsigned int extension_type;
1138 size_t extension_data_len;
1139 const unsigned char *extension_data_end;
1140
Jerry Yu1c9247c2022-07-21 12:37:39 +08001141 /* RFC 8446, page 57
1142 *
1143 * The "pre_shared_key" extension MUST be the last extension in the
1144 * ClientHello (this facilitates implementation as described below).
1145 * Servers MUST check that it is the last extension and otherwise fail
1146 * the handshake with an "illegal_parameter" alert.
1147 */
1148 if( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY )
1149 {
1150 MBEDTLS_SSL_DEBUG_MSG(
1151 3, ( "pre_shared_key is not last extension." ) );
1152 MBEDTLS_SSL_PEND_FATAL_ALERT(
1153 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1154 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1155 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1156 }
1157
XiaokangQian318dc762022-04-20 09:43:51 +00001158 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001159 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1160 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1161 p += 4;
1162
1163 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1164 extension_data_end = p + extension_data_len;
1165
1166 switch( extension_type )
1167 {
XiaokangQian40a35232022-05-07 09:02:40 +00001168#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1169 case MBEDTLS_TLS_EXT_SERVERNAME:
1170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
XiaokangQian9b2b7712022-05-17 02:57:00 +00001171 ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
1172 extension_data_end );
XiaokangQian40a35232022-05-07 09:02:40 +00001173 if( ret != 0 )
1174 {
1175 MBEDTLS_SSL_DEBUG_RET(
1176 1, "mbedtls_ssl_parse_servername_ext", ret );
1177 return( ret );
1178 }
1179 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
1180 break;
1181#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1182
XiaokangQianb67384d2022-04-19 00:02:38 +00001183#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001184 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
1186
1187 /* Supported Groups Extension
1188 *
1189 * When sent by the client, the "supported_groups" extension
1190 * indicates the named groups which the client supports,
1191 * ordered from most preferred to least preferred.
1192 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001193 ret = ssl_tls13_parse_supported_groups_ext(
1194 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001195 if( ret != 0 )
1196 {
1197 MBEDTLS_SSL_DEBUG_RET( 1,
1198 "mbedtls_ssl_parse_supported_groups_ext", ret );
1199 return( ret );
1200 }
1201
1202 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
1203 break;
XiaokangQianb67384d2022-04-19 00:02:38 +00001204#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001205
XiaokangQian88408882022-04-02 10:15:03 +00001206#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001207 case MBEDTLS_TLS_EXT_KEY_SHARE:
1208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
1209
1210 /*
1211 * Key Share Extension
1212 *
1213 * When sent by the client, the "key_share" extension
1214 * contains the endpoint's cryptographic parameters for
1215 * ECDHE/DHE key establishment methods.
1216 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001217 ret = ssl_tls13_parse_key_shares_ext(
1218 ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +00001219 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001220 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
Jerry Yu49ca9282022-05-05 11:05:22 +08001222 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001223 }
1224
Jerry Yu582dd062022-04-22 21:59:01 +08001225 if( ret < 0 )
1226 {
1227 MBEDTLS_SSL_DEBUG_RET(
1228 1, "ssl_tls13_parse_key_shares_ext", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001229 return( ret );
Jerry Yu582dd062022-04-22 21:59:01 +08001230 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001231
1232 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
1233 break;
XiaokangQian88408882022-04-02 10:15:03 +00001234#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001235
1236 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
1238
1239 ret = ssl_tls13_parse_supported_versions_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001240 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001241 if( ret != 0 )
1242 {
1243 MBEDTLS_SSL_DEBUG_RET( 1,
1244 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
1245 return( ret );
1246 }
1247 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
1248 break;
1249
Jerry Yue19e3b92022-07-08 12:04:51 +00001250#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1251 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
1252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found psk key exchange modes extension" ) );
1253
1254 ret = ssl_tls13_parse_key_exchange_modes_ext(
1255 ssl, p, extension_data_end );
1256 if( ret != 0 )
1257 {
1258 MBEDTLS_SSL_DEBUG_RET(
1259 1, "ssl_tls13_parse_key_exchange_modes_ext", ret );
1260 return( ret );
1261 }
1262
1263 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES;
1264 break;
1265#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1266
Jerry Yu1c105562022-07-10 06:32:38 +00001267 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
Jerry Yu1c9247c2022-07-21 12:37:39 +08001269#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001270 /* Delay processing of the PSK identity once we have
1271 * found out which algorithms to use. We keep a pointer
1272 * to the buffer and the size for later processing.
1273 */
1274 pre_shared_key_ext_start = p;
1275 pre_shared_key_ext_end = extension_data_end;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001276#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001277 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1278 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001279
XiaokangQianacb39922022-06-17 10:18:48 +00001280#if defined(MBEDTLS_SSL_ALPN)
1281 case MBEDTLS_TLS_EXT_ALPN:
1282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1283
1284 ret = mbedtls_ssl_parse_alpn_ext( ssl, p, extension_data_end );
1285 if( ret != 0 )
1286 {
1287 MBEDTLS_SSL_DEBUG_RET(
1288 1, ( "mbedtls_ssl_parse_alpn_ext" ), ret );
1289 return( ret );
1290 }
1291 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_ALPN;
1292 break;
1293#endif /* MBEDTLS_SSL_ALPN */
1294
XiaokangQian7807f9f2022-02-15 10:04:37 +00001295#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1296 case MBEDTLS_TLS_EXT_SIG_ALG:
1297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1298
Gabor Mezei078e8032022-04-27 21:17:56 +02001299 ret = mbedtls_ssl_parse_sig_alg_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001300 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001301 if( ret != 0 )
1302 {
1303 MBEDTLS_SSL_DEBUG_MSG( 1,
1304 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
1305 ret ) );
1306 return( ret );
1307 }
1308 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
1309 break;
1310#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1311
1312 default:
1313 MBEDTLS_SSL_DEBUG_MSG( 3,
1314 ( "unknown extension found: %ud ( ignoring )",
1315 extension_type ) );
1316 }
1317
1318 p += extension_data_len;
1319 }
1320
Jerry Yu1c105562022-07-10 06:32:38 +00001321
1322#if defined(MBEDTLS_DEBUG_C)
1323 /* List all the extensions we have received */
1324 ssl_tls13_debug_print_client_hello_exts( ssl );
1325#endif /* MBEDTLS_DEBUG_C */
1326
Jerry Yu77f01482022-07-11 07:03:24 +00001327 ret = ssl_tls13_determine_key_exchange_mode( ssl );
1328 if( ret < 0 )
1329 return( ret );
Jerry Yu1c105562022-07-10 06:32:38 +00001330 mbedtls_ssl_add_hs_hdr_to_checksum( ssl,
1331 MBEDTLS_SSL_HS_CLIENT_HELLO,
1332 p - buf );
Jerry Yu032b15ce2022-07-11 06:10:03 +00001333
Jerry Yu1c105562022-07-10 06:32:38 +00001334#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001335 /* Update checksum with either
1336 * - The entire content of the CH message, if no PSK extension is present
1337 * - The content up to but excluding the PSK extension, if present.
1338 */
Jerry Yu1c105562022-07-10 06:32:38 +00001339 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Jerry Yu77f01482022-07-11 07:03:24 +00001340 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Jerry Yu1c105562022-07-10 06:32:38 +00001341 {
1342 ssl->handshake->update_checksum( ssl, buf,
1343 pre_shared_key_ext_start - buf );
Jerry Yubb852022022-07-20 21:10:44 +08001344 ret = ssl_tls13_parse_pre_shared_key_ext( ssl,
1345 pre_shared_key_ext_start,
1346 pre_shared_key_ext_end );
Jerry Yu1c105562022-07-10 06:32:38 +00001347 if( ret != 0 )
1348 {
Jerry Yubb852022022-07-20 21:10:44 +08001349 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_tls13_parse_pre_shared_key_ext" ),
Jerry Yu1c105562022-07-10 06:32:38 +00001350 ret );
1351 return( ret );
1352 }
Jerry Yu1c105562022-07-10 06:32:38 +00001353 }
1354 else
1355#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1356 {
1357 ssl->handshake->update_checksum( ssl, buf, p - buf );
1358 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001359
XiaokangQian75fe8c72022-06-15 09:42:45 +00001360 return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
1361}
1362
1363/* Update the handshake state machine */
1364
Ronald Cronce7d76e2022-07-08 18:56:49 +02001365MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian75fe8c72022-06-15 09:42:45 +00001366static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
1367{
1368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1369
XiaokangQian7807f9f2022-02-15 10:04:37 +00001370 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001371 * Server certificate selection
1372 */
1373 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
1374 {
1375 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
1376 return( ret );
1377 }
1378#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1379 ssl->handshake->sni_name = NULL;
1380 ssl->handshake->sni_name_len = 0;
1381#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001382
XiaokangQian7807f9f2022-02-15 10:04:37 +00001383 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
1384 if( ret != 0 )
1385 {
1386 MBEDTLS_SSL_DEBUG_RET( 1,
1387 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
1388 return( ret );
1389 }
1390
XiaokangQian7807f9f2022-02-15 10:04:37 +00001391 return( 0 );
1392
1393}
1394
1395/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001396 * Main entry point from the state machine; orchestrates the otherfunctions.
1397 */
1398
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001399MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianed582dd2022-04-13 08:21:05 +00001400static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
1401{
1402
XiaokangQian08037552022-04-20 07:16:41 +00001403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianed582dd2022-04-13 08:21:05 +00001404 unsigned char* buf = NULL;
1405 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001406 int parse_client_hello_ret;
1407
XiaokangQianed582dd2022-04-13 08:21:05 +00001408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1409
XiaokangQianed582dd2022-04-13 08:21:05 +00001410 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
1411 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1412 &buf, &buflen ) );
1413
1414 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
1415 buf + buflen ) );
Jerry Yuf41553b2022-05-09 22:20:30 +08001416 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
1417 * only SSL_CLIENT_HELLO_OK or
1418 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
1419 * stage as negative error codes are handled
1420 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001421
XiaokangQiancfd925f2022-04-14 07:10:37 +00001422 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
Jerry Yu582dd062022-04-22 21:59:01 +08001423
Jerry Yu4ca91402022-05-09 15:50:57 +08001424 if( parse_client_hello_ret == SSL_CLIENT_HELLO_OK )
1425 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1426 else
1427 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
XiaokangQianed582dd2022-04-13 08:21:05 +00001428
1429cleanup:
1430
1431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1432 return( ret );
1433}
1434
1435/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001436 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001437 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001438MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf4b27e42022-03-30 17:32:21 +08001439static int ssl_tls13_prepare_server_hello( mbedtls_ssl_context *ssl )
1440{
Jerry Yu637a3f12022-04-20 21:37:58 +08001441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1442 unsigned char *server_randbytes =
Jerry Yu3bf2c642022-03-30 22:02:12 +08001443 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001444 if( ssl->conf->f_rng == NULL )
1445 {
1446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
1447 return( MBEDTLS_ERR_SSL_NO_RNG );
1448 }
1449
Jerry Yu637a3f12022-04-20 21:37:58 +08001450 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001451 MBEDTLS_SERVER_HELLO_RANDOM_LEN ) ) != 0 )
1452 {
1453 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
1454 return( ret );
1455 }
1456
Jerry Yu637a3f12022-04-20 21:37:58 +08001457 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001458 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1459
1460#if defined(MBEDTLS_HAVE_TIME)
1461 ssl->session_negotiate->start = time( NULL );
1462#endif /* MBEDTLS_HAVE_TIME */
1463
1464 return( ret );
1465}
1466
Jerry Yu3bf2c642022-03-30 22:02:12 +08001467/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001468 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001469 *
1470 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001471 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001472 * } SupportedVersions;
1473 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001474MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001475static int ssl_tls13_write_server_hello_supported_versions_ext(
1476 mbedtls_ssl_context *ssl,
1477 unsigned char *buf,
1478 unsigned char *end,
1479 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +08001480{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001481 *out_len = 0;
1482
Jerry Yu955ddd72022-04-22 22:27:33 +08001483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, write selected version" ) );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001484
1485 /* Check if we have space to write the extension:
1486 * - extension_type (2 bytes)
1487 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001488 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001489 */
Jerry Yu349a6132022-04-14 20:52:56 +08001490 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001491
Jerry Yu349a6132022-04-14 20:52:56 +08001492 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001493
Jerry Yu349a6132022-04-14 20:52:56 +08001494 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001495
Jerry Yu349a6132022-04-14 20:52:56 +08001496 mbedtls_ssl_write_version( buf + 4,
1497 ssl->conf->transport,
1498 ssl->tls_version );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001499
1500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%04x]",
1501 ssl->tls_version ) );
1502
Jerry Yu349a6132022-04-14 20:52:56 +08001503 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001504
1505 return( 0 );
1506}
1507
Jerry Yud9436a12022-04-20 22:28:09 +08001508
Jerry Yu3bf2c642022-03-30 22:02:12 +08001509
1510/* Generate and export a single key share. For hybrid KEMs, this can
1511 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001512MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu955ddd72022-04-22 22:27:33 +08001513static int ssl_tls13_generate_and_write_key_share( mbedtls_ssl_context *ssl,
1514 uint16_t named_group,
1515 unsigned char *buf,
1516 unsigned char *end,
1517 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +08001518{
1519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001520
1521 *out_len = 0;
1522
Jerry Yu89e103c2022-03-30 22:43:29 +08001523#if defined(MBEDTLS_ECDH_C)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001524 if( mbedtls_ssl_tls13_named_group_is_ecdhe( named_group ) )
1525 {
Jerry Yu89e103c2022-03-30 22:43:29 +08001526 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1527 ssl, named_group, buf, end, out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001528 if( ret != 0 )
1529 {
Jerry Yu89e103c2022-03-30 22:43:29 +08001530 MBEDTLS_SSL_DEBUG_RET(
1531 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
1532 ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001533 return( ret );
1534 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001535 }
Jerry Yu89e103c2022-03-30 22:43:29 +08001536 else
1537#endif /* MBEDTLS_ECDH_C */
1538 if( 0 /* Other kinds of KEMs */ )
Jerry Yu3bf2c642022-03-30 22:02:12 +08001539 {
1540 }
1541 else
1542 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001543 ((void) ssl);
1544 ((void) named_group);
1545 ((void) buf);
1546 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001547 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1548 }
1549
1550 return( ret );
1551}
1552
1553/*
1554 * ssl_tls13_write_key_share_ext
1555 *
1556 * Structure of key_share extension in ServerHello:
1557 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001558 * struct {
1559 * NamedGroup group;
1560 * opaque key_exchange<1..2^16-1>;
1561 * } KeyShareEntry;
1562 * struct {
1563 * KeyShareEntry server_share;
1564 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001565 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001566MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3bf2c642022-03-30 22:02:12 +08001567static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
1568 unsigned char *buf,
1569 unsigned char *end,
1570 size_t *out_len )
1571{
Jerry Yu955ddd72022-04-22 22:27:33 +08001572 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001573 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001574 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001575 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001576 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001577
1578 *out_len = 0;
1579
1580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding key share extension" ) );
1581
1582 /* Check if we have space for header and length fields:
1583 * - extension_type (2 bytes)
1584 * - extension_data_length (2 bytes)
1585 * - group (2 bytes)
1586 * - key_exchange_length (2 bytes)
1587 */
1588 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
Jerry Yu57d48412022-04-20 21:50:42 +08001589 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, p, 0 );
1590 MBEDTLS_PUT_UINT16_BE( group, server_share, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001591 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001592
Jerry Yu3bf2c642022-03-30 22:02:12 +08001593 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1594 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001595 ret = ssl_tls13_generate_and_write_key_share(
Jerry Yue65d8012022-04-23 10:34:35 +08001596 ssl, group, server_share + 4, end, &key_exchange_length );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001597 if( ret != 0 )
1598 return( ret );
1599 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001600
Jerry Yu955ddd72022-04-22 22:27:33 +08001601 MBEDTLS_PUT_UINT16_BE( key_exchange_length, server_share + 2, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001602
Jerry Yu57d48412022-04-20 21:50:42 +08001603 MBEDTLS_PUT_UINT16_BE( p - server_share, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001604
Jerry Yu57d48412022-04-20 21:50:42 +08001605 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001606
Jerry Yu3bf2c642022-03-30 22:02:12 +08001607 return( 0 );
1608}
Jerry Yud9436a12022-04-20 22:28:09 +08001609
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001610MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001611static int ssl_tls13_write_hrr_key_share_ext( mbedtls_ssl_context *ssl,
1612 unsigned char *buf,
1613 unsigned char *end,
1614 size_t *out_len )
1615{
1616 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1617 /* key_share Extension
1618 *
1619 * struct {
1620 * select (Handshake.msg_type) {
1621 * ...
1622 * case hello_retry_request:
1623 * NamedGroup selected_group;
1624 * ...
1625 * };
1626 * } KeyShare;
1627 */
1628
1629 *out_len = 0;
1630
Jerry Yub0ac10b2022-05-05 11:10:08 +08001631 /*
1632 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1633 * of the HRR is then to transmit a cookie to force the client to demonstrate
1634 * reachability at their apparent network address (primarily useful for DTLS).
1635 */
Ronald Cron79907712022-07-20 17:05:29 +02001636 if( ! mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral( ssl ) )
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001637 return( 0 );
1638
1639 /* We should only send the key_share extension if the client's initial
1640 * key share was not acceptable. */
1641 if( ssl->handshake->offered_group_id != 0 )
1642 {
1643 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Skip key_share extension in HRR" ) );
1644 return( 0 );
1645 }
1646
1647 if( selected_group == 0 )
1648 {
1649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching named group found" ) );
1650 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1651 }
1652
Jerry Yub0ac10b2022-05-05 11:10:08 +08001653 /* Check if we have enough space:
1654 * - extension_type (2 bytes)
1655 * - extension_data_length (2 bytes)
1656 * - selected_group (2 bytes)
1657 */
Ronald Cron154d1b62022-06-01 15:33:26 +02001658 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001659
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001660 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001661 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001662 MBEDTLS_PUT_UINT16_BE( selected_group, buf, 4 );
1663
1664 MBEDTLS_SSL_DEBUG_MSG( 3,
1665 ( "HRR selected_group: %s (%x)",
1666 mbedtls_ssl_named_group_to_str( selected_group ),
1667 selected_group ) );
1668
1669 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001670
Jerry Yub0ac10b2022-05-05 11:10:08 +08001671 return( 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001672}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001673
1674/*
1675 * Structure of ServerHello message:
1676 *
1677 * struct {
1678 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1679 * Random random;
1680 * opaque legacy_session_id_echo<0..32>;
1681 * CipherSuite cipher_suite;
1682 * uint8 legacy_compression_method = 0;
1683 * Extension extensions<6..2^16-1>;
1684 * } ServerHello;
1685 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001686MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3bf2c642022-03-30 22:02:12 +08001687static int ssl_tls13_write_server_hello_body( mbedtls_ssl_context *ssl,
Jerry Yu56404d72022-03-30 17:36:13 +08001688 unsigned char *buf,
1689 unsigned char *end,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001690 size_t *out_len,
1691 int is_hrr )
Jerry Yu56404d72022-03-30 17:36:13 +08001692{
Jerry Yu955ddd72022-04-22 22:27:33 +08001693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001694 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08001695 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08001696 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001697
1698 *out_len = 0;
1699
Jerry Yucfc04b32022-04-21 09:31:58 +08001700 /* ...
1701 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1702 * ...
1703 * with ProtocolVersion defined as:
1704 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001705 */
1706 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1707 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
1708 p += 2;
1709
Jerry Yu1c3e6882022-04-20 21:23:40 +08001710 /* ...
1711 * Random random;
1712 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08001713 * with Random defined as:
1714 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08001715 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001716 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001717 if( is_hrr )
1718 {
1719 memcpy( p, mbedtls_ssl_tls13_hello_retry_request_magic,
Jerry Yufbe3e642022-04-25 19:31:51 +08001720 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001721 }
1722 else
1723 {
1724 memcpy( p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
Jerry Yufbe3e642022-04-25 19:31:51 +08001725 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001726 }
Jerry Yu955ddd72022-04-22 22:27:33 +08001727 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yu3bf2c642022-03-30 22:02:12 +08001728 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1729 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1730
Jerry Yucfc04b32022-04-21 09:31:58 +08001731 /* ...
1732 * opaque legacy_session_id_echo<0..32>;
1733 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08001734 */
1735 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + ssl->session_negotiate->id_len );
1736 *p++ = (unsigned char)ssl->session_negotiate->id_len;
1737 if( ssl->session_negotiate->id_len > 0 )
1738 {
1739 memcpy( p, &ssl->session_negotiate->id[0],
1740 ssl->session_negotiate->id_len );
1741 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08001742
Jerry Yu3bf2c642022-03-30 22:02:12 +08001743 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
1744 ssl->session_negotiate->id_len );
1745 }
1746
Jerry Yucfc04b32022-04-21 09:31:58 +08001747 /* ...
1748 * CipherSuite cipher_suite;
1749 * ...
1750 * with CipherSuite defined as:
1751 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08001752 */
1753 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1754 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
1755 p += 2;
1756 MBEDTLS_SSL_DEBUG_MSG( 3,
1757 ( "server hello, chosen ciphersuite: %s ( id=%d )",
1758 mbedtls_ssl_get_ciphersuite_name(
1759 ssl->session_negotiate->ciphersuite ),
1760 ssl->session_negotiate->ciphersuite ) );
1761
Jerry Yucfc04b32022-04-21 09:31:58 +08001762 /* ...
1763 * uint8 legacy_compression_method = 0;
1764 * ...
1765 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001766 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1767 *p++ = 0x0;
1768
Jerry Yucfc04b32022-04-21 09:31:58 +08001769 /* ...
1770 * Extension extensions<6..2^16-1>;
1771 * ...
1772 * struct {
1773 * ExtensionType extension_type; (2 bytes)
1774 * opaque extension_data<0..2^16-1>;
1775 * } Extension;
1776 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001777 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yud9436a12022-04-20 22:28:09 +08001778 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001779 p += 2;
1780
Jerry Yue74e04a2022-04-21 09:23:16 +08001781 if( ( ret = ssl_tls13_write_server_hello_supported_versions_ext(
Jerry Yu3bf2c642022-03-30 22:02:12 +08001782 ssl, p, end, &output_len ) ) != 0 )
1783 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001784 MBEDTLS_SSL_DEBUG_RET(
1785 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001786 return( ret );
1787 }
1788 p += output_len;
1789
Jerry Yu3bf2c642022-03-30 22:02:12 +08001790 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1791 {
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001792 if( is_hrr )
1793 ret = ssl_tls13_write_hrr_key_share_ext( ssl, p, end, &output_len );
1794 else
1795 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001796 if( ret != 0 )
1797 return( ret );
1798 p += output_len;
1799 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001800
Jerry Yu032b15ce2022-07-11 06:10:03 +00001801#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yu96a2e362022-07-21 15:11:34 +08001802 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Jerry Yu032b15ce2022-07-11 06:10:03 +00001803 {
Jerry Yubb852022022-07-20 21:10:44 +08001804 ret = ssl_tls13_write_server_pre_shared_key_ext( ssl, p, end, &output_len );
Jerry Yu032b15ce2022-07-11 06:10:03 +00001805 if( ret != 0 )
1806 {
Jerry Yubb852022022-07-20 21:10:44 +08001807 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_server_pre_shared_key_ext",
Jerry Yu032b15ce2022-07-11 06:10:03 +00001808 ret );
1809 return( ret );
1810 }
1811 p += output_len;
1812 }
1813#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1814
Jerry Yud9436a12022-04-20 22:28:09 +08001815 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001816
Jerry Yud9436a12022-04-20 22:28:09 +08001817 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello extensions",
1818 p_extensions_len, p - p_extensions_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001819
Jerry Yud9436a12022-04-20 22:28:09 +08001820 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001821
Jerry Yud9436a12022-04-20 22:28:09 +08001822 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello", buf, *out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001823
1824 return( ret );
Jerry Yu56404d72022-03-30 17:36:13 +08001825}
1826
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001827MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue110d252022-05-05 10:19:22 +08001828static int ssl_tls13_finalize_write_server_hello( mbedtls_ssl_context *ssl )
1829{
1830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf86eb752022-05-06 11:16:55 +08001831 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001832 if( ret != 0 )
1833 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001834 MBEDTLS_SSL_DEBUG_RET( 1,
1835 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yue110d252022-05-05 10:19:22 +08001836 ret );
1837 return( ret );
1838 }
1839
Jerry Yue110d252022-05-05 10:19:22 +08001840 return( ret );
1841}
1842
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001843MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu5b64ae92022-03-30 17:15:02 +08001844static int ssl_tls13_write_server_hello( mbedtls_ssl_context *ssl )
1845{
Jerry Yu637a3f12022-04-20 21:37:58 +08001846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001847 unsigned char *buf;
1848 size_t buf_len, msg_len;
1849
1850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1851
Jerry Yuf4b27e42022-03-30 17:32:21 +08001852 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_server_hello( ssl ) );
1853
Jerry Yu3bf2c642022-03-30 22:02:12 +08001854 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001855 MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len ) );
1856
Jerry Yu3bf2c642022-03-30 22:02:12 +08001857 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1858 buf + buf_len,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001859 &msg_len,
1860 0 ) );
Jerry Yuf4b27e42022-03-30 17:32:21 +08001861
Jerry Yu3bf2c642022-03-30 22:02:12 +08001862 mbedtls_ssl_add_hs_msg_to_checksum(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001863 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1864
Jerry Yu3bf2c642022-03-30 22:02:12 +08001865 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001866 ssl, buf_len, msg_len ) );
Jerry Yu637a3f12022-04-20 21:37:58 +08001867
Jerry Yue110d252022-05-05 10:19:22 +08001868 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_server_hello( ssl ) );
1869
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001870#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1871 /* The server sends a dummy change_cipher_spec record immediately
1872 * after its first handshake message. This may either be after
1873 * a ServerHello or a HelloRetryRequest.
1874 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001875 mbedtls_ssl_handshake_set_state(
1876 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001877#else
Jerry Yu637a3f12022-04-20 21:37:58 +08001878 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001879#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08001880
Jerry Yuf4b27e42022-03-30 17:32:21 +08001881cleanup:
1882
1883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1884 return( ret );
Jerry Yu5b64ae92022-03-30 17:15:02 +08001885}
1886
Jerry Yu23d1a252022-05-12 18:08:59 +08001887
1888/*
1889 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
1890 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001891MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b840462022-06-01 17:05:53 +02001892static int ssl_tls13_prepare_hello_retry_request( mbedtls_ssl_context *ssl )
Jerry Yu23d1a252022-05-12 18:08:59 +08001893{
1894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1895 if( ssl->handshake->hello_retry_request_count > 0 )
1896 {
1897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Too many HRRs" ) );
1898 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1899 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1900 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1901 }
1902
1903 /*
1904 * Create stateless transcript hash for HRR
1905 */
1906 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
1907 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
1908 if( ret != 0 )
1909 {
1910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr", ret );
1911 return( ret );
1912 }
1913 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
1914
1915 return( 0 );
1916}
1917
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001918MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu23d1a252022-05-12 18:08:59 +08001919static int ssl_tls13_write_hello_retry_request( mbedtls_ssl_context *ssl )
1920{
1921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1922 unsigned char *buf;
1923 size_t buf_len, msg_len;
1924
1925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello retry request" ) );
1926
Ronald Cron7b840462022-06-01 17:05:53 +02001927 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_hello_retry_request( ssl ) );
Jerry Yu23d1a252022-05-12 18:08:59 +08001928
1929 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
1930 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1931 &buf, &buf_len ) );
1932
1933 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1934 buf + buf_len,
1935 &msg_len,
1936 1 ) );
1937 mbedtls_ssl_add_hs_msg_to_checksum(
1938 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1939
1940
1941 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, buf_len,
1942 msg_len ) );
1943
1944 ssl->handshake->hello_retry_request_count++;
1945
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001946#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1947 /* The server sends a dummy change_cipher_spec record immediately
1948 * after its first handshake message. This may either be after
1949 * a ServerHello or a HelloRetryRequest.
1950 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001951 mbedtls_ssl_handshake_set_state(
Gabor Mezeif7044ea2022-06-28 16:01:49 +02001952 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001953#else
Jerry Yu23d1a252022-05-12 18:08:59 +08001954 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001955#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08001956
1957cleanup:
1958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello retry request" ) );
1959 return( ret );
1960}
1961
Jerry Yu5b64ae92022-03-30 17:15:02 +08001962/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08001963 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
1964 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001965
1966/*
1967 * struct {
1968 * Extension extensions<0..2 ^ 16 - 1>;
1969 * } EncryptedExtensions;
1970 *
1971 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001972MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d3841a2022-04-16 12:37:19 +08001973static int ssl_tls13_write_encrypted_extensions_body( mbedtls_ssl_context *ssl,
1974 unsigned char *buf,
1975 unsigned char *end,
1976 size_t *out_len )
1977{
XiaokangQianacb39922022-06-17 10:18:48 +00001978 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001979 unsigned char *p = buf;
1980 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08001981 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001982 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08001983
Jerry Yu4d3841a2022-04-16 12:37:19 +08001984 *out_len = 0;
1985
1986 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yu8937eb42022-05-03 12:12:14 +08001987 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001988 p += 2;
1989
Jerry Yu8937eb42022-05-03 12:12:14 +08001990 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00001991 ((void) ret);
1992 ((void) output_len);
1993
1994#if defined(MBEDTLS_SSL_ALPN)
1995 ret = mbedtls_ssl_write_alpn_ext( ssl, p, end, &output_len );
1996 if( ret != 0 )
1997 return( ret );
XiaokangQian95d5f542022-06-24 02:29:26 +00001998 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001999#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002000
Jerry Yu8937eb42022-05-03 12:12:14 +08002001 extensions_len = ( p - p_extensions_len ) - 2;
2002 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
2003
2004 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002005
2006 MBEDTLS_SSL_DEBUG_BUF( 4, "encrypted extensions", buf, *out_len );
2007
2008 return( 0 );
2009}
2010
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002011MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d3841a2022-04-16 12:37:19 +08002012static int ssl_tls13_write_encrypted_extensions( mbedtls_ssl_context *ssl )
2013{
2014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2015 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002016 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002017
Gabor Mezei54719122022-06-28 11:34:56 +02002018 mbedtls_ssl_set_outbound_transform( ssl,
2019 ssl->handshake->transform_handshake );
2020 MBEDTLS_SSL_DEBUG_MSG(
2021 3, ( "switching to handshake transform for outbound data" ) );
2022
Jerry Yuab452cc2022-04-28 15:27:08 +08002023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08002024
Jerry Yu4d3841a2022-04-16 12:37:19 +08002025 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
2026 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf, &buf_len ) );
2027
2028 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_encrypted_extensions_body(
2029 ssl, buf, buf + buf_len, &msg_len ) );
2030
2031 mbedtls_ssl_add_hs_msg_to_checksum(
2032 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len );
2033
Jerry Yu4d3841a2022-04-16 12:37:19 +08002034 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
2035 ssl, buf_len, msg_len ) );
2036
Jerry Yu8937eb42022-05-03 12:12:14 +08002037#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02002038 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Jerry Yu8937eb42022-05-03 12:12:14 +08002039 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2040 else
XiaokangQiana987e1d2022-05-07 01:25:58 +00002041 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yu8937eb42022-05-03 12:12:14 +08002042#else
2043 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2044#endif
2045
Jerry Yu4d3841a2022-04-16 12:37:19 +08002046cleanup:
2047
Jerry Yuab452cc2022-04-28 15:27:08 +08002048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08002049 return( ret );
2050}
2051
XiaokangQiancec9ae62022-05-06 07:28:50 +00002052#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002053#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2054#define SSL_CERTIFICATE_REQUEST_SKIP 1
2055/* Coordination:
2056 * Check whether a CertificateRequest message should be written.
2057 * Returns a negative code on failure, or
2058 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2059 * - SSL_CERTIFICATE_REQUEST_SKIP
2060 * indicating if the writing of the CertificateRequest
2061 * should be skipped or not.
2062 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002063MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiana987e1d2022-05-07 01:25:58 +00002064static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
2065{
2066 int authmode;
2067
XiaokangQian40a35232022-05-07 09:02:40 +00002068#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2069 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2070 authmode = ssl->handshake->sni_authmode;
2071 else
2072#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002073 authmode = ssl->conf->authmode;
2074
2075 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
2076 return( SSL_CERTIFICATE_REQUEST_SKIP );
2077
XiaokangQianc3017f62022-05-13 05:55:41 +00002078 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002079
XiaokangQiana987e1d2022-05-07 01:25:58 +00002080 return( SSL_CERTIFICATE_REQUEST_SEND_REQUEST );
2081}
2082
XiaokangQiancec9ae62022-05-06 07:28:50 +00002083/*
2084 * struct {
2085 * opaque certificate_request_context<0..2^8-1>;
2086 * Extension extensions<2..2^16-1>;
2087 * } CertificateRequest;
2088 *
2089 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002090MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiancec9ae62022-05-06 07:28:50 +00002091static int ssl_tls13_write_certificate_request_body( mbedtls_ssl_context *ssl,
2092 unsigned char *buf,
2093 const unsigned char *end,
2094 size_t *out_len )
2095{
2096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2097 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002098 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002099 unsigned char *p_extensions_len;
2100
2101 *out_len = 0;
2102
2103 /* Check if we have enough space:
2104 * - certificate_request_context (1 byte)
2105 * - extensions length (2 bytes)
2106 */
2107 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
2108
2109 /*
2110 * Write certificate_request_context
2111 */
2112 /*
2113 * We use a zero length context for the normal handshake
2114 * messages. For post-authentication handshake messages
2115 * this request context would be set to a non-zero value.
2116 */
2117 *p++ = 0x0;
2118
2119 /*
2120 * Write extensions
2121 */
2122 /* The extensions must contain the signature_algorithms. */
2123 p_extensions_len = p;
2124 p += 2;
XiaokangQianec6efb92022-05-06 09:53:10 +00002125 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
XiaokangQiancec9ae62022-05-06 07:28:50 +00002126 if( ret != 0 )
2127 return( ret );
2128
XiaokangQianec6efb92022-05-06 09:53:10 +00002129 p += output_len;
XiaokangQianaad9b0a2022-05-09 01:11:21 +00002130 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
XiaokangQiancec9ae62022-05-06 07:28:50 +00002131
2132 *out_len = p - buf;
2133
2134 return( 0 );
2135}
2136
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002137MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiancec9ae62022-05-06 07:28:50 +00002138static int ssl_tls13_write_certificate_request( mbedtls_ssl_context *ssl )
2139{
2140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2141
2142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2143
2144 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2145
2146 if( ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST )
2147 {
2148 unsigned char *buf;
2149 size_t buf_len, msg_len;
2150
2151 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
2152 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, &buf, &buf_len ) );
2153
2154 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_request_body(
2155 ssl, buf, buf + buf_len, &msg_len ) );
2156
2157 mbedtls_ssl_add_hs_msg_to_checksum(
2158 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len );
2159
2160 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
2161 ssl, buf_len, msg_len ) );
2162 }
2163 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
2164 {
2165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2166 ret = 0;
2167 }
2168 else
2169 {
2170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2171 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2172 goto cleanup;
2173 }
2174
2175 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2176cleanup:
2177
2178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2179 return( ret );
2180}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002181
Jerry Yu4d3841a2022-04-16 12:37:19 +08002182/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002183 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002184 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002185MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002186static int ssl_tls13_write_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08002187{
Jerry Yu5a26f302022-05-10 20:46:40 +08002188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002189
2190#if defined(MBEDTLS_X509_CRT_PARSE_C)
2191 if( ( ssl_tls13_pick_key_cert( ssl ) != 0 ) ||
2192 mbedtls_ssl_own_cert( ssl ) == NULL )
Jerry Yu5a26f302022-05-10 20:46:40 +08002193 {
Jerry Yub89125b2022-05-13 15:45:49 +08002194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate available." ) );
Jerry Yu5a26f302022-05-10 20:46:40 +08002195 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2196 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08002197 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu5a26f302022-05-10 20:46:40 +08002198 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002199#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002200
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08002201 ret = mbedtls_ssl_tls13_write_certificate( ssl );
2202 if( ret != 0 )
Jerry Yu1bff7112022-04-16 14:29:11 +08002203 return( ret );
Jerry Yu1bff7112022-04-16 14:29:11 +08002204 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2205 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08002206}
2207
2208/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002209 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002210 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002211MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002212static int ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08002213{
Jerry Yu4ff9e142022-04-16 14:57:49 +08002214 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08002215 if( ret != 0 )
Jerry Yu4ff9e142022-04-16 14:57:49 +08002216 return( ret );
Jerry Yu4ff9e142022-04-16 14:57:49 +08002217 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2218 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08002219}
Jerry Yu5a26f302022-05-10 20:46:40 +08002220#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002221
2222/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002223 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002224 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002225MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d8567f2022-04-17 10:57:57 +08002226static int ssl_tls13_write_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08002227{
Jerry Yud6e253d2022-05-18 13:59:24 +08002228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002229
2230 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2231 if( ret != 0 )
2232 return( ret );
2233
Jerry Yue3d67cb2022-05-19 15:33:10 +08002234 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2235 if( ret != 0 )
2236 {
2237 MBEDTLS_SSL_PEND_FATAL_ALERT(
2238 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2239 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2240 return( ret );
2241 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002242
Ronald Cron63dc4632022-05-31 14:41:53 +02002243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
2244 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
XiaokangQian189ded22022-05-10 08:12:17 +00002245
Ronald Cron63dc4632022-05-31 14:41:53 +02002246 if( ssl->handshake->certificate_request_sent )
2247 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
XiaokangQian189ded22022-05-10 08:12:17 +00002248 else
Ronald Cron19385882022-06-15 16:26:13 +02002249 {
2250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate" ) );
2251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
XiaokangQian189ded22022-05-10 08:12:17 +00002252 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002253 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002254
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002255 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08002256}
2257
2258/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002259 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002260 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002261MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d8567f2022-04-17 10:57:57 +08002262static int ssl_tls13_process_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08002263{
Jerry Yud6e253d2022-05-18 13:59:24 +08002264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2265
Jerry Yuff226982022-04-16 16:52:57 +08002266 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
2267 if( ret != 0 )
2268 return( ret );
2269
Jerry Yue3d67cb2022-05-19 15:33:10 +08002270 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
2271 if( ret != 0 )
2272 {
2273 MBEDTLS_SSL_DEBUG_RET( 1,
2274 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
2275 }
2276
Jerry Yu03ed50b2022-04-16 17:13:30 +08002277 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yuff226982022-04-16 16:52:57 +08002278 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08002279}
2280
2281/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002282 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002283 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002284MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d8567f2022-04-17 10:57:57 +08002285static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08002286{
Jerry Yu03ed50b2022-04-16 17:13:30 +08002287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2288
Jerry Yu03ed50b2022-04-16 17:13:30 +08002289 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2290 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2291 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08002292}
2293
2294/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00002295 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00002296 */
Jerry Yu27561932021-08-27 17:07:38 +08002297int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +08002298{
XiaokangQian08037552022-04-20 07:16:41 +00002299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002300
2301 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
2302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2303
Jerry Yue3b34122021-09-28 17:53:35 +08002304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
2305 mbedtls_ssl_states_str( ssl->state ),
2306 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +08002307
XiaokangQian7807f9f2022-02-15 10:04:37 +00002308 switch( ssl->state )
2309 {
2310 /* start state */
2311 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +00002312 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
XiaokangQian08037552022-04-20 07:16:41 +00002313 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002314 break;
2315
XiaokangQian7807f9f2022-02-15 10:04:37 +00002316 case MBEDTLS_SSL_CLIENT_HELLO:
XiaokangQian4080a7f2022-04-11 09:55:18 +00002317 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +00002318 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +00002319 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
Jerry Yuf41553b2022-05-09 22:20:30 +08002320 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002321
Jerry Yuf41553b2022-05-09 22:20:30 +08002322 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
2323 ret = ssl_tls13_write_hello_retry_request( ssl );
2324 if( ret != 0 )
2325 {
2326 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_hello_retry_request", ret );
2327 return( ret );
2328 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002329 break;
2330
Jerry Yu5b64ae92022-03-30 17:15:02 +08002331 case MBEDTLS_SSL_SERVER_HELLO:
2332 ret = ssl_tls13_write_server_hello( ssl );
2333 break;
2334
Xiaofei Baicba64af2022-02-15 10:00:56 +00002335 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Jerry Yu4d3841a2022-04-16 12:37:19 +08002336 ret = ssl_tls13_write_encrypted_extensions( ssl );
Xiaofei Baicba64af2022-02-15 10:00:56 +00002337 if( ret != 0 )
2338 {
Jerry Yu4d3841a2022-04-16 12:37:19 +08002339 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_encrypted_extensions", ret );
2340 return( ret );
Xiaofei Baicba64af2022-02-15 10:00:56 +00002341 }
Jerry Yu48330562022-05-06 21:35:44 +08002342 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08002343
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002344#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
2345 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Xiaofei Bai5ee73d82022-03-14 02:48:30 +00002346 ret = ssl_tls13_write_certificate_request( ssl );
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002347 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002348
Jerry Yu83da34e2022-04-16 13:59:52 +08002349 case MBEDTLS_SSL_SERVER_CERTIFICATE:
2350 ret = ssl_tls13_write_server_certificate( ssl );
2351 break;
2352
2353 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
2354 ret = ssl_tls13_write_certificate_verify( ssl );
2355 break;
Jerry Yu5a26f302022-05-10 20:46:40 +08002356#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002357
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002358 /*
2359 * Injection of dummy-CCS's for middlebox compatibility
2360 */
2361#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02002362 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002363 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2364 if( ret == 0 )
2365 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2366 break;
2367
2368 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
2369 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2370 if( ret == 0 )
2371 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
2372 break;
2373#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2374
Jerry Yu69dd8d42022-04-16 12:51:26 +08002375 case MBEDTLS_SSL_SERVER_FINISHED:
2376 ret = ssl_tls13_write_server_finished( ssl );
2377 break;
2378
2379 case MBEDTLS_SSL_CLIENT_FINISHED:
2380 ret = ssl_tls13_process_client_finished( ssl );
2381 break;
2382
Jerry Yu69dd8d42022-04-16 12:51:26 +08002383 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
2384 ret = ssl_tls13_handshake_wrapup( ssl );
2385 break;
2386
XiaokangQian6b916b12022-04-25 07:29:34 +00002387 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2388 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Ronald Cron209cae92022-06-07 10:30:19 +02002389 if( ret == 0 )
XiaokangQian6b916b12022-04-25 07:29:34 +00002390 {
Ronald Cron209cae92022-06-07 10:30:19 +02002391 if( ssl->session_negotiate->peer_cert != NULL )
2392 {
2393 mbedtls_ssl_handshake_set_state(
2394 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2395 }
2396 else
Ronald Cron19385882022-06-15 16:26:13 +02002397 {
2398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
Ronald Cron209cae92022-06-07 10:30:19 +02002399 mbedtls_ssl_handshake_set_state(
2400 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002401 }
XiaokangQian6b916b12022-04-25 07:29:34 +00002402 }
2403 break;
2404
2405 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2406 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2407 if( ret == 0 )
2408 {
2409 mbedtls_ssl_handshake_set_state(
2410 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2411 }
2412 break;
2413
XiaokangQian7807f9f2022-02-15 10:04:37 +00002414 default:
2415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
XiaokangQian060d8672022-04-21 09:24:56 +00002416 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian7807f9f2022-02-15 10:04:37 +00002417 }
2418
2419 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +08002420}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002421
Jerry Yufb4b6472022-01-27 15:03:26 +08002422#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */