blob: 10e9bb7b0bde60a9af4ecc4e10a9a6c4e00108e0 [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 {
Jerry Yu568ec252022-07-22 21:27:34 +0800194 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_SSL_ALERT_MSG_DECRYPT_ERROR );
Jerry Yu2f0abc92022-07-22 19:34:48 +0800202 }
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 Yu568ec252022-07-22 21:27:34 +0800267 const unsigned char *identities = buf;
268 const unsigned char *p_identity_len;
269 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000270 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800271 const unsigned char *binders;
272 const unsigned char *p_binder_len;
273 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000274 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800275 int matched_identity = -1;
276 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000277
Jerry Yu2f0abc92022-07-22 19:34:48 +0800278 MBEDTLS_SSL_DEBUG_BUF( 3, "pre_shared_key extension", buf, end - buf );
Jerry Yu1c105562022-07-10 06:32:38 +0000279
Jerry Yu96a2e362022-07-21 15:11:34 +0800280 /* identities_len 2 bytes
281 * identities_data >= 7 bytes
282 */
Jerry Yu568ec252022-07-22 21:27:34 +0800283 MBEDTLS_SSL_CHK_BUF_READ_PTR( identities, end, 7 + 2 );
284 identities_len = MBEDTLS_GET_UINT16_BE( identities, 0 );
285 p_identity_len = identities + 2;
286 MBEDTLS_SSL_CHK_BUF_READ_PTR( p_identity_len, end, identities_len );
287 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000288
Jerry Yu96a2e362022-07-21 15:11:34 +0800289 /* binders_len 2 bytes
290 * binders >= 33 bytes
291 */
Jerry Yu568ec252022-07-22 21:27:34 +0800292 binders = identities_end;
293 MBEDTLS_SSL_CHK_BUF_READ_PTR( binders, end, 33 );
294 binders_len = MBEDTLS_GET_UINT16_BE( binders, 0 );
295 p_binder_len = binders + 2;
296 MBEDTLS_SSL_CHK_BUF_READ_PTR( p_binder_len, end, binders_len );
297 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000298
Jerry Yu96a2e362022-07-21 15:11:34 +0800299 ssl->handshake->update_checksum( ssl, buf, identities_end - buf );
300
Jerry Yu568ec252022-07-22 21:27:34 +0800301 while( p_identity_len < identities_end && p_binder_len < binders_end )
Jerry Yu1c105562022-07-10 06:32:38 +0000302 {
Jerry Yu1c105562022-07-10 06:32:38 +0000303 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800304 size_t identity_len;
Jerry Yu96a2e362022-07-21 15:11:34 +0800305 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800306 size_t binder_len;
Jerry Yu96a2e362022-07-21 15:11:34 +0800307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yubb852022022-07-20 21:10:44 +0800308
Jerry Yu568ec252022-07-22 21:27:34 +0800309 MBEDTLS_SSL_CHK_BUF_READ_PTR( p_identity_len, identities_end, 2 + 1 + 4 );
310 identity_len = MBEDTLS_GET_UINT16_BE( p_identity_len, 0 );
311 identity = p_identity_len + 2;
312 MBEDTLS_SSL_CHK_BUF_READ_PTR( identity, identities_end, identity_len + 4 );
313 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000314
Jerry Yu568ec252022-07-22 21:27:34 +0800315 MBEDTLS_SSL_CHK_BUF_READ_PTR( p_binder_len, binders_end, 1 + 32 );
316 binder_len = *p_binder_len;
317 binder = p_binder_len + 1;
318 MBEDTLS_SSL_CHK_BUF_READ_PTR( binder, binders_end, binder_len );
319 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800320
Jerry Yu96a2e362022-07-21 15:11:34 +0800321
322 identity_id++;
323 if( matched_identity != -1 )
Jerry Yu1c105562022-07-10 06:32:38 +0000324 continue;
325
Jerry Yu96a2e362022-07-21 15:11:34 +0800326 ret = ssl_tls13_offered_psks_check_identity_match(
327 ssl, identity, identity_len );
328 if( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH == ret )
329 continue;
330
331 ret = ssl_tls13_offered_psks_check_binder_match(
332 ssl, binder, binder_len );
Jerry Yu568ec252022-07-22 21:27:34 +0800333 if( ret != SSL_TLS1_3_OFFERED_PSK_MATCH )
Jerry Yu96a2e362022-07-21 15:11:34 +0800334 {
335 MBEDTLS_SSL_DEBUG_RET( 1,
336 "ssl_tls13_offered_psks_check_binder_match" , ret );
337 MBEDTLS_SSL_PEND_FATAL_ALERT(
338 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
339 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
340 return( ret );
341 }
342 if( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH == ret )
343 continue;
344
345 matched_identity = identity_id;
Jerry Yu1c105562022-07-10 06:32:38 +0000346 }
347
Jerry Yu568ec252022-07-22 21:27:34 +0800348 if( p_identity_len != identities_end || p_binder_len != binders_end )
Jerry Yu1c105562022-07-10 06:32:38 +0000349 {
350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key extesion decode error" ) );
351 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
352 MBEDTLS_ERR_SSL_DECODE_ERROR );
353 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
354 }
355
Jerry Yu96a2e362022-07-21 15:11:34 +0800356 if( matched_identity == -1 )
Jerry Yu1c105562022-07-10 06:32:38 +0000357 {
Jerry Yu96a2e362022-07-21 15:11:34 +0800358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "No matched pre shared key found" ) );
Jerry Yu96a2e362022-07-21 15:11:34 +0800359 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Jerry Yu1c105562022-07-10 06:32:38 +0000360 }
361
Jerry Yu96a2e362022-07-21 15:11:34 +0800362 ssl->handshake->selected_identity = (uint16_t)matched_identity;
363 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Pre shared key found" ) );
Jerry Yu1c105562022-07-10 06:32:38 +0000364
365 /* Update the handshake transcript with the binder list. */
366 ssl->handshake->update_checksum( ssl,
367 identities_end,
Jerry Yu96a2e362022-07-21 15:11:34 +0800368 (size_t)( binders_end - identities_end ) );
369 return( 0 );
Jerry Yu1c105562022-07-10 06:32:38 +0000370}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000371
372/*
373 * struct {
374 * select ( Handshake.msg_type ) {
375 * ....
376 * case server_hello:
377 * uint16 selected_identity;
378 * }
379 * } PreSharedKeyExtension;
380 */
Jerry Yubb852022022-07-20 21:10:44 +0800381static int ssl_tls13_write_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
382 unsigned char *buf,
383 unsigned char *end,
384 size_t *olen )
Jerry Yu032b15ce2022-07-11 06:10:03 +0000385{
386 unsigned char *p = (unsigned char*)buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000387
388 *olen = 0;
389
390#if defined(MBEDTLS_USE_PSA_CRYPTO)
391 if( mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
392#else
393 if( ssl->handshake->psk == NULL )
394#endif
395 {
396 /* We shouldn't have called this extension writer unless we've
397 * chosen to use a PSK. */
398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
399 }
400
401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding pre_shared_key extension" ) );
402 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
403
Jerry Yu032b15ce2022-07-11 06:10:03 +0000404 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0 );
Jerry Yu032b15ce2022-07-11 06:10:03 +0000405 MBEDTLS_PUT_UINT16_BE( 2, p, 2 );
406
Jerry Yu96a2e362022-07-21 15:11:34 +0800407 MBEDTLS_PUT_UINT16_BE( ssl->handshake->selected_identity, p, 4 );
Jerry Yu032b15ce2022-07-11 06:10:03 +0000408
409 *olen = 6;
410
Jerry Yu96a2e362022-07-21 15:11:34 +0800411 MBEDTLS_SSL_DEBUG_MSG( 4, ( "sent selected_identity: %u",
412 ssl->handshake->selected_identity ) );
Jerry Yu032b15ce2022-07-11 06:10:03 +0000413
414 return( 0 );
415}
416
Jerry Yue19e3b92022-07-08 12:04:51 +0000417#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
418
XiaokangQian7807f9f2022-02-15 10:04:37 +0000419/* From RFC 8446:
420 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000421 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000422 * } SupportedVersions;
423 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200424MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian7807f9f2022-02-15 10:04:37 +0000425static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
426 const unsigned char *buf,
427 const unsigned char *end )
428{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000429 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000430 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000431 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000432 uint16_t tls_version;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000433 int tls13_supported = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000434
435 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000436 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000437 p += 1;
438
XiaokangQian4080a7f2022-04-11 09:55:18 +0000439 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, versions_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000440 versions_end = p + versions_len;
441 while( p < versions_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000442 {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000443 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, versions_end, 2 );
XiaokangQiande333912022-04-20 08:49:42 +0000444 tls_version = mbedtls_ssl_read_version( p, ssl->conf->transport );
XiaokangQianb67384d2022-04-19 00:02:38 +0000445 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000446
447 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
XiaokangQiande333912022-04-20 08:49:42 +0000448 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000449 {
450 tls13_supported = 1;
451 break;
452 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000453 }
454
XiaokangQianb67384d2022-04-19 00:02:38 +0000455 if( !tls13_supported )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000456 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
458
459 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
460 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
461 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
462 }
463
XiaokangQiande333912022-04-20 08:49:42 +0000464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%04x]",
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000465 (unsigned int)tls_version ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000466
XiaokangQian7807f9f2022-02-15 10:04:37 +0000467 return( 0 );
468}
469
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000470#if defined(MBEDTLS_ECDH_C)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000471/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000472 *
473 * From RFC 8446:
474 * enum {
475 * ... (0xFFFF)
476 * } NamedGroup;
477 * struct {
478 * NamedGroup named_group_list<2..2^16-1>;
479 * } NamedGroupList;
480 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200481MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc1be19f2022-04-23 16:11:39 +0800482static int ssl_tls13_parse_supported_groups_ext( mbedtls_ssl_context *ssl,
483 const unsigned char *buf,
484 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000485{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000486 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000487 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000488 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000489
490 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000491 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000492 named_group_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000493 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000494 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, named_group_list_len );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000495 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000496 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000497
XiaokangQian08037552022-04-20 07:16:41 +0000498 while( p < named_group_list_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000499 {
XiaokangQian08037552022-04-20 07:16:41 +0000500 uint16_t named_group;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000501 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, named_group_list_end, 2 );
XiaokangQian08037552022-04-20 07:16:41 +0000502 named_group = MBEDTLS_GET_UINT16_BE( p, 0 );
503 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000504
Jerry Yuc1be19f2022-04-23 16:11:39 +0800505 MBEDTLS_SSL_DEBUG_MSG( 2,
506 ( "got named group: %s(%04x)",
507 mbedtls_ssl_named_group_to_str( named_group ),
508 named_group ) );
XiaokangQian08037552022-04-20 07:16:41 +0000509
510 if( ! mbedtls_ssl_named_group_is_offered( ssl, named_group ) ||
511 ! mbedtls_ssl_named_group_is_supported( named_group ) ||
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000512 ssl->handshake->hrr_selected_group != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000513 {
XiaokangQian08037552022-04-20 07:16:41 +0000514 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000515 }
516
Jerry Yuc1be19f2022-04-23 16:11:39 +0800517 MBEDTLS_SSL_DEBUG_MSG( 2,
518 ( "add named group %s(%04x) into received list.",
519 mbedtls_ssl_named_group_to_str( named_group ),
520 named_group ) );
521
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000522 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000523 }
524
525 return( 0 );
526
527}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000528#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000529
XiaokangQian08037552022-04-20 07:16:41 +0000530#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
531
XiaokangQian88408882022-04-02 10:15:03 +0000532#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000533/*
534 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
XiaokangQiane8ff3502022-04-22 02:34:40 +0000535 * extension is correct and stores the first acceptable key share and its associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000536 *
537 * Possible return values are:
538 * - 0: Successful processing of the client provided key share extension.
XiaokangQian08037552022-04-20 07:16:41 +0000539 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by the client
XiaokangQian7807f9f2022-02-15 10:04:37 +0000540 * does not match a group supported by the server. A HelloRetryRequest will
541 * be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000542 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800543 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200544MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian7807f9f2022-02-15 10:04:37 +0000545static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
546 const unsigned char *buf,
547 const unsigned char *end )
548{
XiaokangQianb67384d2022-04-19 00:02:38 +0000549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000550 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000551 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800552 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000553
554 /* From RFC 8446:
555 *
556 * struct {
557 * KeyShareEntry client_shares<0..2^16-1>;
558 * } KeyShareClientHello;
559 *
560 */
561
XiaokangQian7807f9f2022-02-15 10:04:37 +0000562 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000563 client_shares_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000564 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000565 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, client_shares_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000566
567 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000568 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000569
570 /* We try to find a suitable key share entry and copy it to the
571 * handshake context. Later, we have to find out whether we can do
572 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000573 * dismiss it and send a HelloRetryRequest message.
574 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000575
Jerry Yuc1be19f2022-04-23 16:11:39 +0800576 while( p < client_shares_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000577 {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000578 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800579 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800580 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000581
582 /*
583 * struct {
584 * NamedGroup group;
585 * opaque key_exchange<1..2^16-1>;
586 * } KeyShareEntry;
587 */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000588 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, 4 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000589 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yuc1be19f2022-04-23 16:11:39 +0800590 key_exchange_len = MBEDTLS_GET_UINT16_BE( p, 2 );
591 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800592 key_exchange = p;
XiaokangQianb67384d2022-04-19 00:02:38 +0000593 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, key_exchange_len );
Jerry Yu086edc22022-05-05 10:50:38 +0800594 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000595
596 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000597 * for input validation purposes.
598 */
XiaokangQian060d8672022-04-21 09:24:56 +0000599 if( ! mbedtls_ssl_named_group_is_offered( ssl, group ) ||
Jerry Yuc1be19f2022-04-23 16:11:39 +0800600 ! mbedtls_ssl_named_group_is_supported( group ) ||
601 ssl->handshake->offered_group_id != 0 )
XiaokangQian060d8672022-04-21 09:24:56 +0000602 {
603 continue;
604 }
XiaokangQian060d8672022-04-21 09:24:56 +0000605
XiaokangQian7807f9f2022-02-15 10:04:37 +0000606 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000607 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000608 */
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000609 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
610 {
Jerry Yuc1be19f2022-04-23 16:11:39 +0800611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH group: %s (%04x)",
612 mbedtls_ssl_named_group_to_str( group ),
613 group ) );
XiaokangQian318dc762022-04-20 09:43:51 +0000614 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Jerry Yu086edc22022-05-05 10:50:38 +0800615 ssl, key_exchange - 2, key_exchange_len + 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000616 if( ret != 0 )
617 return( ret );
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000618
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000619 }
620 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000621 {
622 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000623 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000624 continue;
625 }
626
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000627 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000628 }
629
Jerry Yuc1be19f2022-04-23 16:11:39 +0800630
631 if( ssl->handshake->offered_group_id == 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000632 {
633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
XiaokangQian08037552022-04-20 07:16:41 +0000634 return( SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000635 }
636 return( 0 );
637}
XiaokangQian88408882022-04-02 10:15:03 +0000638#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000639
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000640#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000641static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000642{
XiaokangQian3207a322022-02-23 03:15:27 +0000643 ((void) ssl);
644
XiaokangQian7807f9f2022-02-15 10:04:37 +0000645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
XiaokangQianb67384d2022-04-19 00:02:38 +0000646 MBEDTLS_SSL_DEBUG_MSG( 3,
647 ( "- KEY_SHARE_EXTENSION ( %s )",
648 ( ( ssl->handshake->extensions_present
649 & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ? "TRUE" : "FALSE" ) );
650 MBEDTLS_SSL_DEBUG_MSG( 3,
651 ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
652 ( ( ssl->handshake->extensions_present
653 & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
654 "TRUE" : "FALSE" ) );
655 MBEDTLS_SSL_DEBUG_MSG( 3,
656 ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
657 ( ( ssl->handshake->extensions_present
658 & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ? "TRUE" : "FALSE" ) );
659 MBEDTLS_SSL_DEBUG_MSG( 3,
660 ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
661 ( ( ssl->handshake->extensions_present
662 & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ? "TRUE" : "FALSE" ) );
663 MBEDTLS_SSL_DEBUG_MSG( 3,
664 ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
665 ( ( ssl->handshake->extensions_present
666 & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
667 "TRUE" : "FALSE" ) );
668 MBEDTLS_SSL_DEBUG_MSG( 3,
669 ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
670 ( ( ssl->handshake->extensions_present
671 & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
672 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000673#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
XiaokangQianb67384d2022-04-19 00:02:38 +0000674 MBEDTLS_SSL_DEBUG_MSG( 3,
675 ( "- SERVERNAME_EXTENSION ( %s )",
676 ( ( ssl->handshake->extensions_present
677 & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
678 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000679#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianacb39922022-06-17 10:18:48 +0000680#if defined ( MBEDTLS_SSL_ALPN )
681 MBEDTLS_SSL_DEBUG_MSG( 3,
682 ( "- ALPN_EXTENSION ( %s )",
683 ( ( ssl->handshake->extensions_present
684 & MBEDTLS_SSL_EXT_ALPN ) > 0 ) ?
685 "TRUE" : "FALSE" ) );
686#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000687}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000688#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000689
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200690MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian4080a7f2022-04-11 09:55:18 +0000691static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000692 int exts_mask )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000693{
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000694 int masked = ssl->handshake->extensions_present & exts_mask;
695 return( masked == exts_mask );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000696}
697
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200698MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000699static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
700 mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000701{
Jerry Yu77f01482022-07-11 07:03:24 +0000702 return( ssl_tls13_client_hello_has_exts(
703 ssl,
704 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
705 MBEDTLS_SSL_EXT_KEY_SHARE |
706 MBEDTLS_SSL_EXT_SIG_ALG ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000707}
708
Jerry Yu77f01482022-07-11 07:03:24 +0000709#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
710MBEDTLS_CHECK_RETURN_CRITICAL
711static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
712 mbedtls_ssl_context *ssl )
713{
714 return( ssl_tls13_client_hello_has_exts(
715 ssl,
716 MBEDTLS_SSL_EXT_PRE_SHARED_KEY |
717 MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) );
718}
719
720MBEDTLS_CHECK_RETURN_CRITICAL
721static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
722 mbedtls_ssl_context *ssl )
723{
724 return( ssl_tls13_client_hello_has_exts(
725 ssl,
726 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
727 MBEDTLS_SSL_EXT_KEY_SHARE |
728 MBEDTLS_SSL_EXT_PRE_SHARED_KEY |
729 MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) );
730}
731#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
732
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200733MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000734static int ssl_tls13_check_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000735{
Jerry Yu77f01482022-07-11 07:03:24 +0000736 return( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) &&
737 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange( ssl ) );
738}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000739
Jerry Yu77f01482022-07-11 07:03:24 +0000740MBEDTLS_CHECK_RETURN_CRITICAL
741static int ssl_tls13_check_psk_key_exchange( mbedtls_ssl_context *ssl )
742{
743#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
744 return( mbedtls_ssl_conf_tls13_psk_enabled( ssl ) &&
745 mbedtls_ssl_tls13_psk_enabled( ssl ) &&
746 ssl_tls13_client_hello_has_exts_for_psk_key_exchange( ssl ) );
747#else
748 ((void) ssl);
749 return( 0 );
750#endif
751}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000752
Jerry Yu77f01482022-07-11 07:03:24 +0000753MBEDTLS_CHECK_RETURN_CRITICAL
754static int ssl_tls13_check_psk_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
755{
756#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
757 return( mbedtls_ssl_conf_tls13_psk_ephemeral_enabled( ssl ) &&
758 mbedtls_ssl_tls13_psk_ephemeral_enabled( ssl ) &&
759 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange( ssl ) );
760#else
761 ((void) ssl);
762 return( 0 );
763#endif
764}
765
766static int ssl_tls13_determine_key_exchange_mode( mbedtls_ssl_context *ssl )
767{
768 /*
769 * Determine the key exchange algorithm to use.
770 * There are three types of key exchanges supported in TLS 1.3:
771 * - (EC)DH with ECDSA,
772 * - (EC)DH with PSK,
773 * - plain PSK.
774 *
775 * The PSK-based key exchanges may additionally be used with 0-RTT.
776 *
777 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +0800778 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
779 * 2 ) Certificate Mode ( ephemeral )
780 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +0000781 */
782
783 ssl->handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
784
Jerry Yu77f01482022-07-11 07:03:24 +0000785 if( ssl_tls13_check_psk_ephemeral_key_exchange( ssl ) )
786 {
787 ssl->handshake->key_exchange_mode =
788 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk_ephemeral" ) );
790 }
791 else
792 if( ssl_tls13_check_ephemeral_key_exchange( ssl ) )
793 {
794 ssl->handshake->key_exchange_mode =
795 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: ephemeral" ) );
797 }
798 else
Jerry Yuce6ed702022-07-22 21:49:53 +0800799 if( ssl_tls13_check_psk_key_exchange( ssl ) )
800 {
801 ssl->handshake->key_exchange_mode =
802 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk" ) );
804 }
805 else
Jerry Yu77f01482022-07-11 07:03:24 +0000806 {
807 MBEDTLS_SSL_DEBUG_MSG(
808 1,
809 ( "ClientHello message misses mandatory extensions." ) );
810 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
811 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
812 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
813 }
814
815 return( 0 );
816
XiaokangQian7807f9f2022-02-15 10:04:37 +0000817}
818
XiaokangQian81802f42022-06-10 13:25:22 +0000819#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
820 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian23c5be62022-06-07 02:04:34 +0000821/*
XiaokangQianfb665a82022-06-15 03:57:21 +0000822 * Pick best ( private key, certificate chain ) pair based on the signature
823 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +0000824 */
Ronald Cronce7d76e2022-07-08 18:56:49 +0200825MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian07aad072022-06-14 05:35:09 +0000826static int ssl_tls13_pick_key_cert( mbedtls_ssl_context *ssl )
XiaokangQian23c5be62022-06-07 02:04:34 +0000827{
XiaokangQianfb665a82022-06-15 03:57:21 +0000828 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +0000829 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +0000830
831#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
832 if( ssl->handshake->sni_key_cert != NULL )
XiaokangQianfb665a82022-06-15 03:57:21 +0000833 key_cert_list = ssl->handshake->sni_key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000834 else
835#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianfb665a82022-06-15 03:57:21 +0000836 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000837
XiaokangQianfb665a82022-06-15 03:57:21 +0000838 if( key_cert_list == NULL )
XiaokangQian23c5be62022-06-07 02:04:34 +0000839 {
840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
841 return( -1 );
842 }
843
XiaokangQian81802f42022-06-10 13:25:22 +0000844 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
XiaokangQian23c5be62022-06-07 02:04:34 +0000845 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000846 for( key_cert = key_cert_list; key_cert != NULL;
847 key_cert = key_cert->next )
XiaokangQian23c5be62022-06-07 02:04:34 +0000848 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000849 MBEDTLS_SSL_DEBUG_CRT( 3, "certificate (chain) candidate",
850 key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000851
852 /*
853 * This avoids sending the client a cert it'll reject based on
854 * keyUsage or other extensions.
855 */
856 if( mbedtls_x509_crt_check_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000857 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +0000858 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000859 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
860 MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ) ) != 0 )
XiaokangQian81802f42022-06-10 13:25:22 +0000861 {
862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
XiaokangQianfb665a82022-06-15 03:57:21 +0000863 "(extended) key usage extension" ) );
XiaokangQian81802f42022-06-10 13:25:22 +0000864 continue;
865 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000866
Jerry Yucc539102022-06-27 16:27:35 +0800867 MBEDTLS_SSL_DEBUG_MSG( 3,
868 ( "ssl_tls13_pick_key_cert:"
869 "check signature algorithm %s [%04x]",
870 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
871 *sig_alg ) );
Jerry Yufb526692022-06-19 11:22:49 +0800872 if( mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
Jerry Yud099cf02022-06-19 13:47:00 +0800873 *sig_alg, &key_cert->cert->pk ) )
XiaokangQian81802f42022-06-10 13:25:22 +0000874 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000875 ssl->handshake->key_cert = key_cert;
Jerry Yucc539102022-06-27 16:27:35 +0800876 MBEDTLS_SSL_DEBUG_MSG( 3,
877 ( "ssl_tls13_pick_key_cert:"
878 "selected signature algorithm"
879 " %s [%04x]",
880 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
881 *sig_alg ) );
XiaokangQianfb665a82022-06-15 03:57:21 +0000882 MBEDTLS_SSL_DEBUG_CRT(
XiaokangQian75fe8c72022-06-15 09:42:45 +0000883 3, "selected certificate (chain)",
XiaokangQianfb665a82022-06-15 03:57:21 +0000884 ssl->handshake->key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000885 return( 0 );
886 }
XiaokangQian81802f42022-06-10 13:25:22 +0000887 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000888 }
889
Jerry Yu9d3e2fa2022-06-27 22:14:01 +0800890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ssl_tls13_pick_key_cert:"
891 "no suitable certificate found" ) );
XiaokangQian23c5be62022-06-07 02:04:34 +0000892 return( -1 );
893}
XiaokangQian81802f42022-06-10 13:25:22 +0000894#endif /* MBEDTLS_X509_CRT_PARSE_C &&
895 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +0000896
XiaokangQian4080a7f2022-04-11 09:55:18 +0000897/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000898 *
899 * STATE HANDLING: ClientHello
900 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000901 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000902 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000903 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000904 *
905 * In this case, the server progresses to sending its ServerHello.
906 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000907 * 2) The ClientHello was well-formed but didn't match the server's
908 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000909 *
910 * For example, the client might not have offered a key share which
911 * the server supports, or the server might require a cookie.
912 *
913 * In this case, the server sends a HelloRetryRequest.
914 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000915 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000916 *
917 * In this case, we abort the handshake.
918 *
919 */
920
921/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000922 * Structure of this message:
923 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000924 * uint16 ProtocolVersion;
925 * opaque Random[32];
926 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +0000927 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000928 * struct {
929 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
930 * Random random;
931 * opaque legacy_session_id<0..32>;
932 * CipherSuite cipher_suites<2..2^16-2>;
933 * opaque legacy_compression_methods<1..2^8-1>;
934 * Extension extensions<8..2^16-1>;
935 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000936 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000937
938#define SSL_CLIENT_HELLO_OK 0
939#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
940
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200941MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian4080a7f2022-04-11 09:55:18 +0000942static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
943 const unsigned char *buf,
944 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000945{
XiaokangQianb67384d2022-04-19 00:02:38 +0000946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
947 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000948 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000949 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000950 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000951 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000952 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000953 const unsigned char *extensions_end;
Jerry Yu49ca9282022-05-05 11:05:22 +0800954 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955
XiaokangQian7807f9f2022-02-15 10:04:37 +0000956 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
Jerry Yu1c105562022-07-10 06:32:38 +0000957#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
958 const unsigned char *pre_shared_key_ext_start = NULL;
959 const unsigned char *pre_shared_key_ext_end = NULL;
960#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961
962 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
963
964 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000965 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000966 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +0000967 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +0000968 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000969 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000970 * .. . .. ciphersuite list length ( 2 bytes )
971 * .. . .. ciphersuite list
972 * .. . .. compression alg. list length ( 1 byte )
973 * .. . .. compression alg. list
974 * .. . .. extensions length ( 2 bytes, optional )
975 * .. . .. extensions ( optional )
976 */
977
XiaokangQianb67384d2022-04-19 00:02:38 +0000978 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400979 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +0000980 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
981 * read at least up to session id length without worrying.
982 */
983 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
984
985 /* ...
986 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
987 * ...
988 * with ProtocolVersion defined as:
989 * uint16 ProtocolVersion;
990 */
XiaokangQiande333912022-04-20 08:49:42 +0000991 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
992 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000993 {
994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
995 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
996 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000997 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000998 }
999 p += 2;
1000
1001 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +00001002 * Only support TLS 1.3 currently, temporarily set the version.
1003 */
XiaokangQiande333912022-04-20 08:49:42 +00001004 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +00001005
Jerry Yuc1be19f2022-04-23 16:11:39 +08001006 /* ...
1007 * Random random;
1008 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001009 * with Random defined as:
1010 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001011 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001012 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +00001013 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001014
XiaokangQian08037552022-04-20 07:16:41 +00001015 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
1016 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001017
Jerry Yuc1be19f2022-04-23 16:11:39 +08001018 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001019 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001020 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001021 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001022 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +00001023 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001024
XiaokangQianb67384d2022-04-19 00:02:38 +00001025 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001026 {
1027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1028 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1029 }
1030
XiaokangQian4080a7f2022-04-11 09:55:18 +00001031 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +00001032 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
XiaokangQiane8ff3502022-04-22 02:34:40 +00001033 p, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +00001034 /*
1035 * Check we have enough data for the legacy session identifier
1036 * and the ciphersuite list length.
1037 */
1038 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001039
XiaokangQianed582dd2022-04-13 08:21:05 +00001040 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +00001041 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001042
XiaokangQian7807f9f2022-02-15 10:04:37 +00001043 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1044 p += 2;
1045
XiaokangQianb67384d2022-04-19 00:02:38 +00001046 /* Check we have enough data for the ciphersuite list, the legacy
1047 * compression methods and the length of the extensions.
1048 */
1049 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001050
Jerry Yuc1be19f2022-04-23 16:11:39 +08001051 /* ...
XiaokangQian08037552022-04-20 07:16:41 +00001052 * CipherSuite cipher_suites<2..2^16-2>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001053 * ...
XiaokangQian08037552022-04-20 07:16:41 +00001054 * with CipherSuite defined as:
1055 * uint8 CipherSuite[2];
1056 */
XiaokangQian060d8672022-04-21 09:24:56 +00001057 cipher_suites = p;
1058 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001059 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1060 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +00001061 /*
1062 * Search for a matching ciphersuite
1063 */
XiaokangQian318dc762022-04-20 09:43:51 +00001064 int ciphersuite_match = 0;
XiaokangQian060d8672022-04-21 09:24:56 +00001065 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +00001066 {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001067 uint16_t cipher_suite;
1068 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, cipher_suites_end, 2 );
1069 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1070 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
XiaokangQian08037552022-04-20 07:16:41 +00001071 /*
XiaokangQiane8ff3502022-04-22 02:34:40 +00001072 * Check whether this ciphersuite is valid and offered.
1073 */
XiaokangQian08037552022-04-20 07:16:41 +00001074 if( ( mbedtls_ssl_validate_ciphersuite(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001075 ssl, ciphersuite_info, ssl->tls_version,
1076 ssl->tls_version ) != 0 ) ||
1077 ! mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
1078 {
XiaokangQian08037552022-04-20 07:16:41 +00001079 continue;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001080 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001081
XiaokangQian08037552022-04-20 07:16:41 +00001082 ssl->session_negotiate->ciphersuite = cipher_suite;
1083 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +00001084 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +00001085
XiaokangQian08037552022-04-20 07:16:41 +00001086 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001087
XiaokangQian17f974c2022-04-19 09:57:41 +00001088 }
1089
Jerry Yuc1be19f2022-04-23 16:11:39 +08001090 if( ! ciphersuite_match )
XiaokangQian318dc762022-04-20 09:43:51 +00001091 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +00001092 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1093 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1094 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +00001095 }
XiaokangQian17f974c2022-04-19 09:57:41 +00001096
1097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
1098 ciphersuite_info->name ) );
1099
XiaokangQian060d8672022-04-21 09:24:56 +00001100 p = cipher_suites + cipher_suites_len;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001101
XiaokangQian4080a7f2022-04-11 09:55:18 +00001102 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001103 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001104 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001105 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +00001106 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001107 {
XiaokangQian4080a7f2022-04-11 09:55:18 +00001108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
1109 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1110 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1111 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001112 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001113 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001114
Jerry Yuc1be19f2022-04-23 16:11:39 +08001115 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001116 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001117 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001118 * with Extension defined as:
1119 * struct {
1120 * ExtensionType extension_type;
1121 * opaque extension_data<0..2^16-1>;
1122 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001123 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001124 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001125 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001126 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQiane8ff3502022-04-22 02:34:40 +00001127 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001128
XiaokangQian4080a7f2022-04-11 09:55:18 +00001129 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001130
1131 while( p < extensions_end )
1132 {
1133 unsigned int extension_type;
1134 size_t extension_data_len;
1135 const unsigned char *extension_data_end;
1136
Jerry Yu1c9247c2022-07-21 12:37:39 +08001137 /* RFC 8446, page 57
1138 *
1139 * The "pre_shared_key" extension MUST be the last extension in the
1140 * ClientHello (this facilitates implementation as described below).
1141 * Servers MUST check that it is the last extension and otherwise fail
1142 * the handshake with an "illegal_parameter" alert.
1143 */
1144 if( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY )
1145 {
1146 MBEDTLS_SSL_DEBUG_MSG(
1147 3, ( "pre_shared_key is not last extension." ) );
1148 MBEDTLS_SSL_PEND_FATAL_ALERT(
1149 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1150 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1151 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1152 }
1153
XiaokangQian318dc762022-04-20 09:43:51 +00001154 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001155 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1156 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1157 p += 4;
1158
1159 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1160 extension_data_end = p + extension_data_len;
1161
1162 switch( extension_type )
1163 {
XiaokangQian40a35232022-05-07 09:02:40 +00001164#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1165 case MBEDTLS_TLS_EXT_SERVERNAME:
1166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
XiaokangQian9b2b7712022-05-17 02:57:00 +00001167 ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
1168 extension_data_end );
XiaokangQian40a35232022-05-07 09:02:40 +00001169 if( ret != 0 )
1170 {
1171 MBEDTLS_SSL_DEBUG_RET(
1172 1, "mbedtls_ssl_parse_servername_ext", ret );
1173 return( ret );
1174 }
1175 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
1176 break;
1177#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1178
XiaokangQianb67384d2022-04-19 00:02:38 +00001179#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001180 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
1182
1183 /* Supported Groups Extension
1184 *
1185 * When sent by the client, the "supported_groups" extension
1186 * indicates the named groups which the client supports,
1187 * ordered from most preferred to least preferred.
1188 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001189 ret = ssl_tls13_parse_supported_groups_ext(
1190 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001191 if( ret != 0 )
1192 {
1193 MBEDTLS_SSL_DEBUG_RET( 1,
1194 "mbedtls_ssl_parse_supported_groups_ext", ret );
1195 return( ret );
1196 }
1197
1198 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
1199 break;
XiaokangQianb67384d2022-04-19 00:02:38 +00001200#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001201
XiaokangQian88408882022-04-02 10:15:03 +00001202#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001203 case MBEDTLS_TLS_EXT_KEY_SHARE:
1204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
1205
1206 /*
1207 * Key Share Extension
1208 *
1209 * When sent by the client, the "key_share" extension
1210 * contains the endpoint's cryptographic parameters for
1211 * ECDHE/DHE key establishment methods.
1212 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001213 ret = ssl_tls13_parse_key_shares_ext(
1214 ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +00001215 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001216 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
Jerry Yu49ca9282022-05-05 11:05:22 +08001218 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001219 }
1220
Jerry Yu582dd062022-04-22 21:59:01 +08001221 if( ret < 0 )
1222 {
1223 MBEDTLS_SSL_DEBUG_RET(
1224 1, "ssl_tls13_parse_key_shares_ext", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001225 return( ret );
Jerry Yu582dd062022-04-22 21:59:01 +08001226 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001227
1228 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
1229 break;
XiaokangQian88408882022-04-02 10:15:03 +00001230#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001231
1232 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
1234
1235 ret = ssl_tls13_parse_supported_versions_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001236 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001237 if( ret != 0 )
1238 {
1239 MBEDTLS_SSL_DEBUG_RET( 1,
1240 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
1241 return( ret );
1242 }
1243 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
1244 break;
1245
Jerry Yue19e3b92022-07-08 12:04:51 +00001246#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1247 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
1248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found psk key exchange modes extension" ) );
1249
1250 ret = ssl_tls13_parse_key_exchange_modes_ext(
1251 ssl, p, extension_data_end );
1252 if( ret != 0 )
1253 {
1254 MBEDTLS_SSL_DEBUG_RET(
1255 1, "ssl_tls13_parse_key_exchange_modes_ext", ret );
1256 return( ret );
1257 }
1258
1259 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES;
1260 break;
1261#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1262
Jerry Yu1c105562022-07-10 06:32:38 +00001263 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
Jerry Yu1c9247c2022-07-21 12:37:39 +08001265#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001266 /* Delay processing of the PSK identity once we have
1267 * found out which algorithms to use. We keep a pointer
1268 * to the buffer and the size for later processing.
1269 */
1270 pre_shared_key_ext_start = p;
1271 pre_shared_key_ext_end = extension_data_end;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001272#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001273 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1274 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001275
XiaokangQianacb39922022-06-17 10:18:48 +00001276#if defined(MBEDTLS_SSL_ALPN)
1277 case MBEDTLS_TLS_EXT_ALPN:
1278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1279
1280 ret = mbedtls_ssl_parse_alpn_ext( ssl, p, extension_data_end );
1281 if( ret != 0 )
1282 {
1283 MBEDTLS_SSL_DEBUG_RET(
1284 1, ( "mbedtls_ssl_parse_alpn_ext" ), ret );
1285 return( ret );
1286 }
1287 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_ALPN;
1288 break;
1289#endif /* MBEDTLS_SSL_ALPN */
1290
XiaokangQian7807f9f2022-02-15 10:04:37 +00001291#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1292 case MBEDTLS_TLS_EXT_SIG_ALG:
1293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1294
Gabor Mezei078e8032022-04-27 21:17:56 +02001295 ret = mbedtls_ssl_parse_sig_alg_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001296 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001297 if( ret != 0 )
1298 {
1299 MBEDTLS_SSL_DEBUG_MSG( 1,
1300 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
1301 ret ) );
1302 return( ret );
1303 }
1304 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
1305 break;
1306#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1307
1308 default:
1309 MBEDTLS_SSL_DEBUG_MSG( 3,
1310 ( "unknown extension found: %ud ( ignoring )",
1311 extension_type ) );
1312 }
1313
1314 p += extension_data_len;
1315 }
1316
Jerry Yu1c105562022-07-10 06:32:38 +00001317#if defined(MBEDTLS_DEBUG_C)
1318 /* List all the extensions we have received */
1319 ssl_tls13_debug_print_client_hello_exts( ssl );
1320#endif /* MBEDTLS_DEBUG_C */
1321
1322 mbedtls_ssl_add_hs_hdr_to_checksum( ssl,
1323 MBEDTLS_SSL_HS_CLIENT_HELLO,
1324 p - buf );
Jerry Yu032b15ce2022-07-11 06:10:03 +00001325
Jerry Yu1c105562022-07-10 06:32:38 +00001326#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327 /* Update checksum with either
1328 * - The entire content of the CH message, if no PSK extension is present
1329 * - The content up to but excluding the PSK extension, if present.
1330 */
Jerry Yu1c105562022-07-10 06:32:38 +00001331 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Jerry Yuba9b6e92022-07-22 21:35:18 +08001332 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) &&
1333 ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) )
Jerry Yu1c105562022-07-10 06:32:38 +00001334 {
1335 ssl->handshake->update_checksum( ssl, buf,
1336 pre_shared_key_ext_start - buf );
Jerry Yubb852022022-07-20 21:10:44 +08001337 ret = ssl_tls13_parse_pre_shared_key_ext( ssl,
1338 pre_shared_key_ext_start,
1339 pre_shared_key_ext_end );
Jerry Yuba9b6e92022-07-22 21:35:18 +08001340 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY)
1341 {
1342 ssl->handshake->extensions_present &= ~MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1343 }else if( ret != 0 )
Jerry Yu1c105562022-07-10 06:32:38 +00001344 {
Jerry Yubb852022022-07-20 21:10:44 +08001345 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_tls13_parse_pre_shared_key_ext" ),
Jerry Yu1c105562022-07-10 06:32:38 +00001346 ret );
1347 return( ret );
1348 }
Jerry Yu1c105562022-07-10 06:32:38 +00001349 }
1350 else
1351#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1352 {
1353 ssl->handshake->update_checksum( ssl, buf, p - buf );
1354 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001355
Jerry Yuba9b6e92022-07-22 21:35:18 +08001356 ret = ssl_tls13_determine_key_exchange_mode( ssl );
1357 if( ret < 0 )
1358 return( ret );
1359
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 */