blob: d67f4534a70b381b4e06116890359b60a37390ac [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,
109 uint16_t identity_len )
110{
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 */
124 if( identity_len == ssl->conf->psk_identity_len &&
125 mbedtls_ct_memcmp( ssl->conf->psk_identity,
126 identity, identity_len ) == 0 )
127 {
128 mbedtls_ssl_set_hs_psk( ssl, ssl->conf->psk, ssl->conf->psk_len );
129 return( SSL_TLS1_3_OFFERED_PSK_MATCH );
130 }
131
Jerry Yu1c105562022-07-10 06:32:38 +0000132 return( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH );
133}
134
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800135MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu96a2e362022-07-21 15:11:34 +0800136static int ssl_tls13_get_psk( mbedtls_ssl_context *ssl,
137 const unsigned char **psk,
138 size_t *psk_len )
139{
140#if defined(MBEDTLS_USE_PSA_CRYPTO)
141 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
142 psa_status_t status;
143
144 *psk_len = 0;
145 *psk = NULL;
146
147 status = psa_get_key_attributes( ssl->handshake->psk_opaque, &key_attributes );
148 if( status != PSA_SUCCESS)
149 {
150 return( psa_ssl_status_to_mbedtls( status ) );
151 }
152
153 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits( &key_attributes ) );
154 *psk = mbedtls_calloc( 1, *psk_len );
155 if( *psk == NULL )
156 {
157 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
158 }
159
160 status = psa_export_key( ssl->handshake->psk_opaque,
161 (uint8_t *)*psk, *psk_len, psk_len );
162 if( status != PSA_SUCCESS)
163 {
164 mbedtls_free( (void *)*psk );
165 return( psa_ssl_status_to_mbedtls( status ) );
166 }
167#else
168 *psk = ssl->handshake->psk;
169 *psk_len = ssl->handshake->psk_len;
170#endif /* !MBEDTLS_USE_PSA_CRYPTO */
171 return( 0 );
172}
173
174MBEDTLS_CHECK_RETURN_CRITICAL
175static int ssl_tls13_offered_psks_check_binder_match( mbedtls_ssl_context *ssl,
176 const unsigned char *binder,
177 uint16_t binder_len )
Jerry Yu1c105562022-07-10 06:32:38 +0000178{
179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
180 int psk_type;
Jerry Yu96a2e362022-07-21 15:11:34 +0800181
Jerry Yu1c105562022-07-10 06:32:38 +0000182 mbedtls_md_type_t md_alg =
183 binder_len == 32 ? MBEDTLS_MD_SHA256 : MBEDTLS_MD_SHA384 ;
184 psa_algorithm_t psa_md_alg = mbedtls_psa_translate_md( 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 Yu96a2e362022-07-21 15:11:34 +0800187 const unsigned char *psk;
188 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 Yu1c105562022-07-10 06:32:38 +0000192
193 /* Get current state of handshake transcript. */
194 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_alg,
195 transcript, sizeof( transcript ),
196 &transcript_len );
197 if( ret != 0 )
198 return( ret );
199
Jerry Yu96a2e362022-07-21 15:11:34 +0800200 ret = ssl_tls13_get_psk( ssl, &psk, &psk_len );
201 if( ret != 0 )
202 return( ret );
203
Jerry Yu1c105562022-07-10 06:32:38 +0000204 ret = mbedtls_ssl_tls13_create_psk_binder( ssl, psa_md_alg,
205 psk, psk_len, psk_type,
206 transcript,
207 server_computed_binder );
Jerry Yu96a2e362022-07-21 15:11:34 +0800208#if defined(MBEDTLS_USE_PSA_CRYPTO)
209 mbedtls_free( (void*)psk );
210#endif
Jerry Yu1c105562022-07-10 06:32:38 +0000211 if( ret != 0 )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "PSK binder calculation failed." ) );
214 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
215 }
216
217 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder ( computed ): ",
218 server_computed_binder, binder_len );
219 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder ( received ): ", binder, binder_len );
220
221 if( mbedtls_ct_memcmp( server_computed_binder, binder, binder_len ) == 0 )
222 {
223 return( SSL_TLS1_3_OFFERED_PSK_MATCH );
224 }
225
Jerry Yudaf375a2022-07-20 21:31:43 +0800226 mbedtls_platform_zeroize( server_computed_binder,
227 sizeof( server_computed_binder ) );
Jerry Yu1c105562022-07-10 06:32:38 +0000228 return( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH );
229}
Jerry Yu96a2e362022-07-21 15:11:34 +0800230
Jerry Yu1c105562022-07-10 06:32:38 +0000231/* Parser for pre_shared_key extension in client hello
232 * struct {
233 * opaque identity<1..2^16-1>;
234 * uint32 obfuscated_ticket_age;
235 * } PskIdentity;
236 *
237 * opaque PskBinderEntry<32..255>;
238 *
239 * struct {
240 * PskIdentity identities<7..2^16-1>;
241 * PskBinderEntry binders<33..2^16-1>;
242 * } OfferedPsks;
243 *
244 * struct {
245 * select (Handshake.msg_type) {
246 * case client_hello: OfferedPsks;
247 * ....
248 * };
249 * } PreSharedKeyExtension;
250 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800251MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yubb852022022-07-20 21:10:44 +0800252static int ssl_tls13_parse_pre_shared_key_ext( mbedtls_ssl_context *ssl,
253 const unsigned char *buf,
254 const unsigned char *end )
Jerry Yu1c105562022-07-10 06:32:38 +0000255{
Jerry Yu96a2e362022-07-21 15:11:34 +0800256 const unsigned char *next_identity = buf;
Jerry Yu1c105562022-07-10 06:32:38 +0000257 uint16_t identities_len;
258 const unsigned char *identities_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800259 const unsigned char *next_binder;
Jerry Yu1c105562022-07-10 06:32:38 +0000260 uint16_t binders_len;
261 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800262 int matched_identity = -1;
263 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000264
265 MBEDTLS_SSL_DEBUG_BUF( 3, "pre_shared_key extesion", buf, end - buf );
266
Jerry Yu96a2e362022-07-21 15:11:34 +0800267 /* identities_len 2 bytes
268 * identities_data >= 7 bytes
269 */
270 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_identity, end, 7 + 2 );
271 identities_len = MBEDTLS_GET_UINT16_BE( next_identity, 0 );
272 next_identity += 2;
273 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_identity, end, identities_len );
274 identities_end = next_identity + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000275
Jerry Yu96a2e362022-07-21 15:11:34 +0800276 /* binders_len 2 bytes
277 * binders >= 33 bytes
278 */
279 next_binder = identities_end;
280 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_binder, end, 33 );
281 binders_len = MBEDTLS_GET_UINT16_BE( next_binder, 0 );
282 next_binder += 2;
283 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_binder, end, binders_len );
284 binders_end = next_binder + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000285
Jerry Yu96a2e362022-07-21 15:11:34 +0800286 ssl->handshake->update_checksum( ssl, buf, identities_end - buf );
287
288 while( next_identity < identities_end && next_binder < binders_end )
Jerry Yu1c105562022-07-10 06:32:38 +0000289 {
Jerry Yu1c105562022-07-10 06:32:38 +0000290 const unsigned char *identity;
Jerry Yu96a2e362022-07-21 15:11:34 +0800291 uint16_t identity_len;
292 const unsigned char *binder;
293 uint16_t binder_len;
294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yubb852022022-07-20 21:10:44 +0800295
Jerry Yu96a2e362022-07-21 15:11:34 +0800296 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_identity, identities_end, 2 );
297 identity_len = MBEDTLS_GET_UINT16_BE( next_identity, 0 );
298 next_identity += 2;
299 identity = next_identity;
300 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_identity,
Jerry Yu352cd7d2022-07-20 22:11:00 +0800301 identities_end,
302 identity_len + 4 );
Jerry Yu96a2e362022-07-21 15:11:34 +0800303 next_identity += identity_len + 4;
Jerry Yu1c105562022-07-10 06:32:38 +0000304
Jerry Yu96a2e362022-07-21 15:11:34 +0800305 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_binder, binders_end, 2 );
306
307 binder_len = *next_binder++;
308 binder = next_binder;
309 MBEDTLS_SSL_CHK_BUF_READ_PTR( next_binder, binders_end, binder_len );
310 next_binder += binder_len;
311
312 identity_id++;
313 if( matched_identity != -1 )
Jerry Yu1c105562022-07-10 06:32:38 +0000314 continue;
315
Jerry Yu96a2e362022-07-21 15:11:34 +0800316 ret = ssl_tls13_offered_psks_check_identity_match(
317 ssl, identity, identity_len );
318 if( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH == ret )
319 continue;
320
321 ret = ssl_tls13_offered_psks_check_binder_match(
322 ssl, binder, binder_len );
323 if( ret < 0 )
324 {
325 MBEDTLS_SSL_DEBUG_RET( 1,
326 "ssl_tls13_offered_psks_check_binder_match" , ret );
327 MBEDTLS_SSL_PEND_FATAL_ALERT(
328 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
329 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
330 return( ret );
331 }
332 if( SSL_TLS1_3_OFFERED_PSK_NOT_MATCH == ret )
333 continue;
334
335 matched_identity = identity_id;
Jerry Yu1c105562022-07-10 06:32:38 +0000336 }
337
Jerry Yu96a2e362022-07-21 15:11:34 +0800338 if( next_identity != identities_end || next_binder != binders_end )
Jerry Yu1c105562022-07-10 06:32:38 +0000339 {
340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key extesion decode error" ) );
341 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
342 MBEDTLS_ERR_SSL_DECODE_ERROR );
343 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
344 }
345
Jerry Yu96a2e362022-07-21 15:11:34 +0800346 if( matched_identity == -1 )
Jerry Yu1c105562022-07-10 06:32:38 +0000347 {
Jerry Yu96a2e362022-07-21 15:11:34 +0800348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "No matched pre shared key found" ) );
Jerry Yu1c105562022-07-10 06:32:38 +0000349 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu96a2e362022-07-21 15:11:34 +0800350 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY,
351 MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
352 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Jerry Yu1c105562022-07-10 06:32:38 +0000353 }
354
Jerry Yu96a2e362022-07-21 15:11:34 +0800355 ssl->handshake->selected_identity = (uint16_t)matched_identity;
356 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Pre shared key found" ) );
Jerry Yu1c105562022-07-10 06:32:38 +0000357
358 /* Update the handshake transcript with the binder list. */
359 ssl->handshake->update_checksum( ssl,
360 identities_end,
Jerry Yu96a2e362022-07-21 15:11:34 +0800361 (size_t)( binders_end - identities_end ) );
362 return( 0 );
Jerry Yu1c105562022-07-10 06:32:38 +0000363}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000364
365/*
366 * struct {
367 * select ( Handshake.msg_type ) {
368 * ....
369 * case server_hello:
370 * uint16 selected_identity;
371 * }
372 * } PreSharedKeyExtension;
373 */
Jerry Yubb852022022-07-20 21:10:44 +0800374static int ssl_tls13_write_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
375 unsigned char *buf,
376 unsigned char *end,
377 size_t *olen )
Jerry Yu032b15ce2022-07-11 06:10:03 +0000378{
379 unsigned char *p = (unsigned char*)buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000380
381 *olen = 0;
382
383#if defined(MBEDTLS_USE_PSA_CRYPTO)
384 if( mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
385#else
386 if( ssl->handshake->psk == NULL )
387#endif
388 {
389 /* We shouldn't have called this extension writer unless we've
390 * chosen to use a PSK. */
391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
392 }
393
394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding pre_shared_key extension" ) );
395 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
396
Jerry Yu032b15ce2022-07-11 06:10:03 +0000397 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0 );
Jerry Yu032b15ce2022-07-11 06:10:03 +0000398 MBEDTLS_PUT_UINT16_BE( 2, p, 2 );
399
Jerry Yu96a2e362022-07-21 15:11:34 +0800400 MBEDTLS_PUT_UINT16_BE( ssl->handshake->selected_identity, p, 4 );
Jerry Yu032b15ce2022-07-11 06:10:03 +0000401
402 *olen = 6;
403
Jerry Yu96a2e362022-07-21 15:11:34 +0800404 MBEDTLS_SSL_DEBUG_MSG( 4, ( "sent selected_identity: %u",
405 ssl->handshake->selected_identity ) );
Jerry Yu032b15ce2022-07-11 06:10:03 +0000406
407 return( 0 );
408}
409
Jerry Yue19e3b92022-07-08 12:04:51 +0000410#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
411
XiaokangQian7807f9f2022-02-15 10:04:37 +0000412/* From RFC 8446:
413 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000414 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000415 * } SupportedVersions;
416 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian7807f9f2022-02-15 10:04:37 +0000418static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
419 const unsigned char *buf,
420 const unsigned char *end )
421{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000422 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000423 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000424 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000425 uint16_t tls_version;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000426 int tls13_supported = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000427
428 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000429 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000430 p += 1;
431
XiaokangQian4080a7f2022-04-11 09:55:18 +0000432 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, versions_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000433 versions_end = p + versions_len;
434 while( p < versions_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000435 {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000436 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, versions_end, 2 );
XiaokangQiande333912022-04-20 08:49:42 +0000437 tls_version = mbedtls_ssl_read_version( p, ssl->conf->transport );
XiaokangQianb67384d2022-04-19 00:02:38 +0000438 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000439
440 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
XiaokangQiande333912022-04-20 08:49:42 +0000441 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000442 {
443 tls13_supported = 1;
444 break;
445 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000446 }
447
XiaokangQianb67384d2022-04-19 00:02:38 +0000448 if( !tls13_supported )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000449 {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
451
452 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
453 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
454 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
455 }
456
XiaokangQiande333912022-04-20 08:49:42 +0000457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%04x]",
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000458 (unsigned int)tls_version ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000459
XiaokangQian7807f9f2022-02-15 10:04:37 +0000460 return( 0 );
461}
462
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000463#if defined(MBEDTLS_ECDH_C)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000464/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000465 *
466 * From RFC 8446:
467 * enum {
468 * ... (0xFFFF)
469 * } NamedGroup;
470 * struct {
471 * NamedGroup named_group_list<2..2^16-1>;
472 * } NamedGroupList;
473 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200474MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc1be19f2022-04-23 16:11:39 +0800475static int ssl_tls13_parse_supported_groups_ext( mbedtls_ssl_context *ssl,
476 const unsigned char *buf,
477 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000478{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000479 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000480 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000481 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000482
483 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000484 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000485 named_group_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000486 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000487 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, named_group_list_len );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000488 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000489 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000490
XiaokangQian08037552022-04-20 07:16:41 +0000491 while( p < named_group_list_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000492 {
XiaokangQian08037552022-04-20 07:16:41 +0000493 uint16_t named_group;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000494 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, named_group_list_end, 2 );
XiaokangQian08037552022-04-20 07:16:41 +0000495 named_group = MBEDTLS_GET_UINT16_BE( p, 0 );
496 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000497
Jerry Yuc1be19f2022-04-23 16:11:39 +0800498 MBEDTLS_SSL_DEBUG_MSG( 2,
499 ( "got named group: %s(%04x)",
500 mbedtls_ssl_named_group_to_str( named_group ),
501 named_group ) );
XiaokangQian08037552022-04-20 07:16:41 +0000502
503 if( ! mbedtls_ssl_named_group_is_offered( ssl, named_group ) ||
504 ! mbedtls_ssl_named_group_is_supported( named_group ) ||
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000505 ssl->handshake->hrr_selected_group != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000506 {
XiaokangQian08037552022-04-20 07:16:41 +0000507 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000508 }
509
Jerry Yuc1be19f2022-04-23 16:11:39 +0800510 MBEDTLS_SSL_DEBUG_MSG( 2,
511 ( "add named group %s(%04x) into received list.",
512 mbedtls_ssl_named_group_to_str( named_group ),
513 named_group ) );
514
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000515 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000516 }
517
518 return( 0 );
519
520}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000521#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000522
XiaokangQian08037552022-04-20 07:16:41 +0000523#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
524
XiaokangQian88408882022-04-02 10:15:03 +0000525#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000526/*
527 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
XiaokangQiane8ff3502022-04-22 02:34:40 +0000528 * extension is correct and stores the first acceptable key share and its associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000529 *
530 * Possible return values are:
531 * - 0: Successful processing of the client provided key share extension.
XiaokangQian08037552022-04-20 07:16:41 +0000532 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by the client
XiaokangQian7807f9f2022-02-15 10:04:37 +0000533 * does not match a group supported by the server. A HelloRetryRequest will
534 * be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000535 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800536 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200537MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian7807f9f2022-02-15 10:04:37 +0000538static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
539 const unsigned char *buf,
540 const unsigned char *end )
541{
XiaokangQianb67384d2022-04-19 00:02:38 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000543 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000544 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800545 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000546
547 /* From RFC 8446:
548 *
549 * struct {
550 * KeyShareEntry client_shares<0..2^16-1>;
551 * } KeyShareClientHello;
552 *
553 */
554
XiaokangQian7807f9f2022-02-15 10:04:37 +0000555 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000556 client_shares_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000557 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, client_shares_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000559
560 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000561 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000562
563 /* We try to find a suitable key share entry and copy it to the
564 * handshake context. Later, we have to find out whether we can do
565 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000566 * dismiss it and send a HelloRetryRequest message.
567 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000568
Jerry Yuc1be19f2022-04-23 16:11:39 +0800569 while( p < client_shares_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000570 {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000571 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800572 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800573 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000574
575 /*
576 * struct {
577 * NamedGroup group;
578 * opaque key_exchange<1..2^16-1>;
579 * } KeyShareEntry;
580 */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000581 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, 4 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000582 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yuc1be19f2022-04-23 16:11:39 +0800583 key_exchange_len = MBEDTLS_GET_UINT16_BE( p, 2 );
584 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800585 key_exchange = p;
XiaokangQianb67384d2022-04-19 00:02:38 +0000586 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, key_exchange_len );
Jerry Yu086edc22022-05-05 10:50:38 +0800587 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000588
589 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000590 * for input validation purposes.
591 */
XiaokangQian060d8672022-04-21 09:24:56 +0000592 if( ! mbedtls_ssl_named_group_is_offered( ssl, group ) ||
Jerry Yuc1be19f2022-04-23 16:11:39 +0800593 ! mbedtls_ssl_named_group_is_supported( group ) ||
594 ssl->handshake->offered_group_id != 0 )
XiaokangQian060d8672022-04-21 09:24:56 +0000595 {
596 continue;
597 }
XiaokangQian060d8672022-04-21 09:24:56 +0000598
XiaokangQian7807f9f2022-02-15 10:04:37 +0000599 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000600 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000601 */
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000602 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
603 {
Jerry Yuc1be19f2022-04-23 16:11:39 +0800604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH group: %s (%04x)",
605 mbedtls_ssl_named_group_to_str( group ),
606 group ) );
XiaokangQian318dc762022-04-20 09:43:51 +0000607 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Jerry Yu086edc22022-05-05 10:50:38 +0800608 ssl, key_exchange - 2, key_exchange_len + 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000609 if( ret != 0 )
610 return( ret );
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000611
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000612 }
613 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000614 {
615 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000616 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000617 continue;
618 }
619
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000620 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000621 }
622
Jerry Yuc1be19f2022-04-23 16:11:39 +0800623
624 if( ssl->handshake->offered_group_id == 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000625 {
626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
XiaokangQian08037552022-04-20 07:16:41 +0000627 return( SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000628 }
629 return( 0 );
630}
XiaokangQian88408882022-04-02 10:15:03 +0000631#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000632
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000633#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000634static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000635{
XiaokangQian3207a322022-02-23 03:15:27 +0000636 ((void) ssl);
637
XiaokangQian7807f9f2022-02-15 10:04:37 +0000638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
XiaokangQianb67384d2022-04-19 00:02:38 +0000639 MBEDTLS_SSL_DEBUG_MSG( 3,
640 ( "- KEY_SHARE_EXTENSION ( %s )",
641 ( ( ssl->handshake->extensions_present
642 & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ? "TRUE" : "FALSE" ) );
643 MBEDTLS_SSL_DEBUG_MSG( 3,
644 ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
645 ( ( ssl->handshake->extensions_present
646 & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
647 "TRUE" : "FALSE" ) );
648 MBEDTLS_SSL_DEBUG_MSG( 3,
649 ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
650 ( ( ssl->handshake->extensions_present
651 & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ? "TRUE" : "FALSE" ) );
652 MBEDTLS_SSL_DEBUG_MSG( 3,
653 ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
654 ( ( ssl->handshake->extensions_present
655 & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ? "TRUE" : "FALSE" ) );
656 MBEDTLS_SSL_DEBUG_MSG( 3,
657 ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
658 ( ( ssl->handshake->extensions_present
659 & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
660 "TRUE" : "FALSE" ) );
661 MBEDTLS_SSL_DEBUG_MSG( 3,
662 ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
663 ( ( ssl->handshake->extensions_present
664 & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
665 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000666#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
XiaokangQianb67384d2022-04-19 00:02:38 +0000667 MBEDTLS_SSL_DEBUG_MSG( 3,
668 ( "- SERVERNAME_EXTENSION ( %s )",
669 ( ( ssl->handshake->extensions_present
670 & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
671 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000672#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianacb39922022-06-17 10:18:48 +0000673#if defined ( MBEDTLS_SSL_ALPN )
674 MBEDTLS_SSL_DEBUG_MSG( 3,
675 ( "- ALPN_EXTENSION ( %s )",
676 ( ( ssl->handshake->extensions_present
677 & MBEDTLS_SSL_EXT_ALPN ) > 0 ) ?
678 "TRUE" : "FALSE" ) );
679#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000680}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000681#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000682
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200683MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian4080a7f2022-04-11 09:55:18 +0000684static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000685 int exts_mask )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000686{
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000687 int masked = ssl->handshake->extensions_present & exts_mask;
688 return( masked == exts_mask );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000689}
690
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200691MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000692static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
693 mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000694{
XiaokangQian4080a7f2022-04-11 09:55:18 +0000695 return( ssl_tls13_client_hello_has_exts( ssl,
Jerry Yu99754932022-07-15 15:01:08 +0800696 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
697 MBEDTLS_SSL_EXT_KEY_SHARE |
698 MBEDTLS_SSL_EXT_SIG_ALG ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000699}
700
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200701MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000702static int ssl_tls13_check_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000703{
704 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
705 return( 0 );
706
XiaokangQianb67384d2022-04-19 00:02:38 +0000707 if( !ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000708 return( 0 );
709
Ronald Cron79907712022-07-20 17:05:29 +0200710 ssl->handshake->key_exchange_mode =
XiaokangQianb67384d2022-04-19 00:02:38 +0000711 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000712 return( 1 );
713}
714
XiaokangQian81802f42022-06-10 13:25:22 +0000715#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
716 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian23c5be62022-06-07 02:04:34 +0000717/*
XiaokangQianfb665a82022-06-15 03:57:21 +0000718 * Pick best ( private key, certificate chain ) pair based on the signature
719 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +0000720 */
Ronald Cronce7d76e2022-07-08 18:56:49 +0200721MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian07aad072022-06-14 05:35:09 +0000722static int ssl_tls13_pick_key_cert( mbedtls_ssl_context *ssl )
XiaokangQian23c5be62022-06-07 02:04:34 +0000723{
XiaokangQianfb665a82022-06-15 03:57:21 +0000724 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +0000725 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +0000726
727#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
728 if( ssl->handshake->sni_key_cert != NULL )
XiaokangQianfb665a82022-06-15 03:57:21 +0000729 key_cert_list = ssl->handshake->sni_key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000730 else
731#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianfb665a82022-06-15 03:57:21 +0000732 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000733
XiaokangQianfb665a82022-06-15 03:57:21 +0000734 if( key_cert_list == NULL )
XiaokangQian23c5be62022-06-07 02:04:34 +0000735 {
736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
737 return( -1 );
738 }
739
XiaokangQian81802f42022-06-10 13:25:22 +0000740 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
XiaokangQian23c5be62022-06-07 02:04:34 +0000741 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000742 for( key_cert = key_cert_list; key_cert != NULL;
743 key_cert = key_cert->next )
XiaokangQian23c5be62022-06-07 02:04:34 +0000744 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000745 MBEDTLS_SSL_DEBUG_CRT( 3, "certificate (chain) candidate",
746 key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000747
748 /*
749 * This avoids sending the client a cert it'll reject based on
750 * keyUsage or other extensions.
751 */
752 if( mbedtls_x509_crt_check_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000753 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +0000754 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000755 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
756 MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ) ) != 0 )
XiaokangQian81802f42022-06-10 13:25:22 +0000757 {
758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
XiaokangQianfb665a82022-06-15 03:57:21 +0000759 "(extended) key usage extension" ) );
XiaokangQian81802f42022-06-10 13:25:22 +0000760 continue;
761 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000762
Jerry Yucc539102022-06-27 16:27:35 +0800763 MBEDTLS_SSL_DEBUG_MSG( 3,
764 ( "ssl_tls13_pick_key_cert:"
765 "check signature algorithm %s [%04x]",
766 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
767 *sig_alg ) );
Jerry Yufb526692022-06-19 11:22:49 +0800768 if( mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
Jerry Yud099cf02022-06-19 13:47:00 +0800769 *sig_alg, &key_cert->cert->pk ) )
XiaokangQian81802f42022-06-10 13:25:22 +0000770 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000771 ssl->handshake->key_cert = key_cert;
Jerry Yucc539102022-06-27 16:27:35 +0800772 MBEDTLS_SSL_DEBUG_MSG( 3,
773 ( "ssl_tls13_pick_key_cert:"
774 "selected signature algorithm"
775 " %s [%04x]",
776 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
777 *sig_alg ) );
XiaokangQianfb665a82022-06-15 03:57:21 +0000778 MBEDTLS_SSL_DEBUG_CRT(
XiaokangQian75fe8c72022-06-15 09:42:45 +0000779 3, "selected certificate (chain)",
XiaokangQianfb665a82022-06-15 03:57:21 +0000780 ssl->handshake->key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000781 return( 0 );
782 }
XiaokangQian81802f42022-06-10 13:25:22 +0000783 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000784 }
785
Jerry Yu9d3e2fa2022-06-27 22:14:01 +0800786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ssl_tls13_pick_key_cert:"
787 "no suitable certificate found" ) );
XiaokangQian23c5be62022-06-07 02:04:34 +0000788 return( -1 );
789}
XiaokangQian81802f42022-06-10 13:25:22 +0000790#endif /* MBEDTLS_X509_CRT_PARSE_C &&
791 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +0000792
XiaokangQian4080a7f2022-04-11 09:55:18 +0000793/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000794 *
795 * STATE HANDLING: ClientHello
796 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000797 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000798 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000799 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000800 *
801 * In this case, the server progresses to sending its ServerHello.
802 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000803 * 2) The ClientHello was well-formed but didn't match the server's
804 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000805 *
806 * For example, the client might not have offered a key share which
807 * the server supports, or the server might require a cookie.
808 *
809 * In this case, the server sends a HelloRetryRequest.
810 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000811 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000812 *
813 * In this case, we abort the handshake.
814 *
815 */
816
817/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000818 * Structure of this message:
819 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000820 * uint16 ProtocolVersion;
821 * opaque Random[32];
822 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +0000823 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000824 * struct {
825 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
826 * Random random;
827 * opaque legacy_session_id<0..32>;
828 * CipherSuite cipher_suites<2..2^16-2>;
829 * opaque legacy_compression_methods<1..2^8-1>;
830 * Extension extensions<8..2^16-1>;
831 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000832 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000833
834#define SSL_CLIENT_HELLO_OK 0
835#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
836
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200837MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian4080a7f2022-04-11 09:55:18 +0000838static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
839 const unsigned char *buf,
840 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841{
XiaokangQianb67384d2022-04-19 00:02:38 +0000842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
843 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000844 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000845 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000846 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000847 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000848 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000849 const unsigned char *extensions_end;
Jerry Yu49ca9282022-05-05 11:05:22 +0800850 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000851
XiaokangQian7807f9f2022-02-15 10:04:37 +0000852 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
Jerry Yu1c105562022-07-10 06:32:38 +0000853#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
854 const unsigned char *pre_shared_key_ext_start = NULL;
855 const unsigned char *pre_shared_key_ext_end = NULL;
856#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000857
858 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
859
860 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000861 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000862 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +0000863 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +0000864 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000865 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000866 * .. . .. ciphersuite list length ( 2 bytes )
867 * .. . .. ciphersuite list
868 * .. . .. compression alg. list length ( 1 byte )
869 * .. . .. compression alg. list
870 * .. . .. extensions length ( 2 bytes, optional )
871 * .. . .. extensions ( optional )
872 */
873
XiaokangQianb67384d2022-04-19 00:02:38 +0000874 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400875 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +0000876 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
877 * read at least up to session id length without worrying.
878 */
879 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
880
881 /* ...
882 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
883 * ...
884 * with ProtocolVersion defined as:
885 * uint16 ProtocolVersion;
886 */
XiaokangQiande333912022-04-20 08:49:42 +0000887 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
888 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000889 {
890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
891 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
892 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000893 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000894 }
895 p += 2;
896
897 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000898 * Only support TLS 1.3 currently, temporarily set the version.
899 */
XiaokangQiande333912022-04-20 08:49:42 +0000900 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +0000901
Jerry Yuc1be19f2022-04-23 16:11:39 +0800902 /* ...
903 * Random random;
904 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000905 * with Random defined as:
906 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000907 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000908 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +0000909 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000910
XiaokangQian08037552022-04-20 07:16:41 +0000911 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
912 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000913
Jerry Yuc1be19f2022-04-23 16:11:39 +0800914 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000915 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800916 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000917 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000918 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000919 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000920
XiaokangQianb67384d2022-04-19 00:02:38 +0000921 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000922 {
923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
924 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
925 }
926
XiaokangQian4080a7f2022-04-11 09:55:18 +0000927 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000928 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
XiaokangQiane8ff3502022-04-22 02:34:40 +0000929 p, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +0000930 /*
931 * Check we have enough data for the legacy session identifier
932 * and the ciphersuite list length.
933 */
934 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000935
XiaokangQianed582dd2022-04-13 08:21:05 +0000936 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000937 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000938
XiaokangQian7807f9f2022-02-15 10:04:37 +0000939 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
940 p += 2;
941
XiaokangQianb67384d2022-04-19 00:02:38 +0000942 /* Check we have enough data for the ciphersuite list, the legacy
943 * compression methods and the length of the extensions.
944 */
945 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946
Jerry Yuc1be19f2022-04-23 16:11:39 +0800947 /* ...
XiaokangQian08037552022-04-20 07:16:41 +0000948 * CipherSuite cipher_suites<2..2^16-2>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800949 * ...
XiaokangQian08037552022-04-20 07:16:41 +0000950 * with CipherSuite defined as:
951 * uint8 CipherSuite[2];
952 */
XiaokangQian060d8672022-04-21 09:24:56 +0000953 cipher_suites = p;
954 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
956 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +0000957 /*
958 * Search for a matching ciphersuite
959 */
XiaokangQian318dc762022-04-20 09:43:51 +0000960 int ciphersuite_match = 0;
XiaokangQian060d8672022-04-21 09:24:56 +0000961 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +0000962 {
XiaokangQiane8ff3502022-04-22 02:34:40 +0000963 uint16_t cipher_suite;
964 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, cipher_suites_end, 2 );
965 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
966 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
XiaokangQian08037552022-04-20 07:16:41 +0000967 /*
XiaokangQiane8ff3502022-04-22 02:34:40 +0000968 * Check whether this ciphersuite is valid and offered.
969 */
XiaokangQian08037552022-04-20 07:16:41 +0000970 if( ( mbedtls_ssl_validate_ciphersuite(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800971 ssl, ciphersuite_info, ssl->tls_version,
972 ssl->tls_version ) != 0 ) ||
973 ! mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
974 {
XiaokangQian08037552022-04-20 07:16:41 +0000975 continue;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800976 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977
XiaokangQian08037552022-04-20 07:16:41 +0000978 ssl->session_negotiate->ciphersuite = cipher_suite;
979 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +0000980 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +0000981
XiaokangQian08037552022-04-20 07:16:41 +0000982 break;
XiaokangQian17f974c2022-04-19 09:57:41 +0000983
XiaokangQian17f974c2022-04-19 09:57:41 +0000984 }
985
Jerry Yuc1be19f2022-04-23 16:11:39 +0800986 if( ! ciphersuite_match )
XiaokangQian318dc762022-04-20 09:43:51 +0000987 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000988 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
989 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
990 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +0000991 }
XiaokangQian17f974c2022-04-19 09:57:41 +0000992
993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
994 ciphersuite_info->name ) );
995
XiaokangQian060d8672022-04-21 09:24:56 +0000996 p = cipher_suites + cipher_suites_len;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800997
XiaokangQian4080a7f2022-04-11 09:55:18 +0000998 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000999 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001000 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001001 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +00001002 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001003 {
XiaokangQian4080a7f2022-04-11 09:55:18 +00001004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
1005 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1006 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1007 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001008 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001009 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001010
Jerry Yuc1be19f2022-04-23 16:11:39 +08001011 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001012 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001013 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001014 * with Extension defined as:
1015 * struct {
1016 * ExtensionType extension_type;
1017 * opaque extension_data<0..2^16-1>;
1018 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001019 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001020 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001021 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001022 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQiane8ff3502022-04-22 02:34:40 +00001023 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001024
XiaokangQian4080a7f2022-04-11 09:55:18 +00001025 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001026
1027 while( p < extensions_end )
1028 {
1029 unsigned int extension_type;
1030 size_t extension_data_len;
1031 const unsigned char *extension_data_end;
1032
Jerry Yu1c9247c2022-07-21 12:37:39 +08001033 /* RFC 8446, page 57
1034 *
1035 * The "pre_shared_key" extension MUST be the last extension in the
1036 * ClientHello (this facilitates implementation as described below).
1037 * Servers MUST check that it is the last extension and otherwise fail
1038 * the handshake with an "illegal_parameter" alert.
1039 */
1040 if( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY )
1041 {
1042 MBEDTLS_SSL_DEBUG_MSG(
1043 3, ( "pre_shared_key is not last extension." ) );
1044 MBEDTLS_SSL_PEND_FATAL_ALERT(
1045 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1046 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1047 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1048 }
1049
XiaokangQian318dc762022-04-20 09:43:51 +00001050 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001051 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1052 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1053 p += 4;
1054
1055 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1056 extension_data_end = p + extension_data_len;
1057
1058 switch( extension_type )
1059 {
XiaokangQian40a35232022-05-07 09:02:40 +00001060#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1061 case MBEDTLS_TLS_EXT_SERVERNAME:
1062 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
XiaokangQian9b2b7712022-05-17 02:57:00 +00001063 ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
1064 extension_data_end );
XiaokangQian40a35232022-05-07 09:02:40 +00001065 if( ret != 0 )
1066 {
1067 MBEDTLS_SSL_DEBUG_RET(
1068 1, "mbedtls_ssl_parse_servername_ext", ret );
1069 return( ret );
1070 }
1071 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
1072 break;
1073#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1074
XiaokangQianb67384d2022-04-19 00:02:38 +00001075#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001076 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
1078
1079 /* Supported Groups Extension
1080 *
1081 * When sent by the client, the "supported_groups" extension
1082 * indicates the named groups which the client supports,
1083 * ordered from most preferred to least preferred.
1084 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001085 ret = ssl_tls13_parse_supported_groups_ext(
1086 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001087 if( ret != 0 )
1088 {
1089 MBEDTLS_SSL_DEBUG_RET( 1,
1090 "mbedtls_ssl_parse_supported_groups_ext", ret );
1091 return( ret );
1092 }
1093
1094 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
1095 break;
XiaokangQianb67384d2022-04-19 00:02:38 +00001096#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001097
XiaokangQian88408882022-04-02 10:15:03 +00001098#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001099 case MBEDTLS_TLS_EXT_KEY_SHARE:
1100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
1101
1102 /*
1103 * Key Share Extension
1104 *
1105 * When sent by the client, the "key_share" extension
1106 * contains the endpoint's cryptographic parameters for
1107 * ECDHE/DHE key establishment methods.
1108 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001109 ret = ssl_tls13_parse_key_shares_ext(
1110 ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +00001111 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001112 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
Jerry Yu49ca9282022-05-05 11:05:22 +08001114 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001115 }
1116
Jerry Yu582dd062022-04-22 21:59:01 +08001117 if( ret < 0 )
1118 {
1119 MBEDTLS_SSL_DEBUG_RET(
1120 1, "ssl_tls13_parse_key_shares_ext", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001121 return( ret );
Jerry Yu582dd062022-04-22 21:59:01 +08001122 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001123
1124 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
1125 break;
XiaokangQian88408882022-04-02 10:15:03 +00001126#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001127
1128 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
1130
1131 ret = ssl_tls13_parse_supported_versions_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001132 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001133 if( ret != 0 )
1134 {
1135 MBEDTLS_SSL_DEBUG_RET( 1,
1136 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
1137 return( ret );
1138 }
1139 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
1140 break;
1141
Jerry Yue19e3b92022-07-08 12:04:51 +00001142#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1143 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
1144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found psk key exchange modes extension" ) );
1145
1146 ret = ssl_tls13_parse_key_exchange_modes_ext(
1147 ssl, p, extension_data_end );
1148 if( ret != 0 )
1149 {
1150 MBEDTLS_SSL_DEBUG_RET(
1151 1, "ssl_tls13_parse_key_exchange_modes_ext", ret );
1152 return( ret );
1153 }
1154
1155 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES;
1156 break;
1157#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1158
Jerry Yu1c105562022-07-10 06:32:38 +00001159 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
Jerry Yu1c9247c2022-07-21 12:37:39 +08001161#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001162 /* Delay processing of the PSK identity once we have
1163 * found out which algorithms to use. We keep a pointer
1164 * to the buffer and the size for later processing.
1165 */
1166 pre_shared_key_ext_start = p;
1167 pre_shared_key_ext_end = extension_data_end;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001168#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001169 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1170 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001171
XiaokangQianacb39922022-06-17 10:18:48 +00001172#if defined(MBEDTLS_SSL_ALPN)
1173 case MBEDTLS_TLS_EXT_ALPN:
1174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1175
1176 ret = mbedtls_ssl_parse_alpn_ext( ssl, p, extension_data_end );
1177 if( ret != 0 )
1178 {
1179 MBEDTLS_SSL_DEBUG_RET(
1180 1, ( "mbedtls_ssl_parse_alpn_ext" ), ret );
1181 return( ret );
1182 }
1183 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_ALPN;
1184 break;
1185#endif /* MBEDTLS_SSL_ALPN */
1186
XiaokangQian7807f9f2022-02-15 10:04:37 +00001187#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1188 case MBEDTLS_TLS_EXT_SIG_ALG:
1189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1190
Gabor Mezei078e8032022-04-27 21:17:56 +02001191 ret = mbedtls_ssl_parse_sig_alg_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001192 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001193 if( ret != 0 )
1194 {
1195 MBEDTLS_SSL_DEBUG_MSG( 1,
1196 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
1197 ret ) );
1198 return( ret );
1199 }
1200 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
1201 break;
1202#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1203
1204 default:
1205 MBEDTLS_SSL_DEBUG_MSG( 3,
1206 ( "unknown extension found: %ud ( ignoring )",
1207 extension_type ) );
1208 }
1209
1210 p += extension_data_len;
1211 }
1212
Jerry Yu1c105562022-07-10 06:32:38 +00001213
1214#if defined(MBEDTLS_DEBUG_C)
1215 /* List all the extensions we have received */
1216 ssl_tls13_debug_print_client_hello_exts( ssl );
1217#endif /* MBEDTLS_DEBUG_C */
1218
1219 mbedtls_ssl_add_hs_hdr_to_checksum( ssl,
1220 MBEDTLS_SSL_HS_CLIENT_HELLO,
1221 p - buf );
Jerry Yu032b15ce2022-07-11 06:10:03 +00001222
Jerry Yu1c105562022-07-10 06:32:38 +00001223#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001224 /* Update checksum with either
1225 * - The entire content of the CH message, if no PSK extension is present
1226 * - The content up to but excluding the PSK extension, if present.
1227 */
Jerry Yu1c105562022-07-10 06:32:38 +00001228 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
1229 if( mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) &&
1230 ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) )
1231 {
1232 ssl->handshake->update_checksum( ssl, buf,
1233 pre_shared_key_ext_start - buf );
Jerry Yubb852022022-07-20 21:10:44 +08001234 ret = ssl_tls13_parse_pre_shared_key_ext( ssl,
1235 pre_shared_key_ext_start,
1236 pre_shared_key_ext_end );
Jerry Yu1c105562022-07-10 06:32:38 +00001237 if( ret != 0 )
1238 {
Jerry Yubb852022022-07-20 21:10:44 +08001239 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_tls13_parse_pre_shared_key_ext" ),
Jerry Yu1c105562022-07-10 06:32:38 +00001240 ret );
1241 return( ret );
1242 }
Jerry Yu1c105562022-07-10 06:32:38 +00001243 }
1244 else
1245#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1246 {
1247 ssl->handshake->update_checksum( ssl, buf, p - buf );
1248 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001249
XiaokangQian75fe8c72022-06-15 09:42:45 +00001250 return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
1251}
1252
1253/* Update the handshake state machine */
1254
Ronald Cronce7d76e2022-07-08 18:56:49 +02001255MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian75fe8c72022-06-15 09:42:45 +00001256static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
1257{
1258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1259
XiaokangQian7807f9f2022-02-15 10:04:37 +00001260 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001261 * Here we only support the ephemeral or (EC)DHE key echange mode
XiaokangQian7807f9f2022-02-15 10:04:37 +00001262 */
XiaokangQianb67384d2022-04-19 00:02:38 +00001263 if( !ssl_tls13_check_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001264 {
1265 MBEDTLS_SSL_DEBUG_MSG(
1266 1,
1267 ( "ClientHello message misses mandatory extensions." ) );
1268 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
1269 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1270 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1271 }
1272
XiaokangQianfb665a82022-06-15 03:57:21 +00001273 /*
1274 * Server certificate selection
1275 */
1276 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
1277 {
1278 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
1279 return( ret );
1280 }
1281#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1282 ssl->handshake->sni_name = NULL;
1283 ssl->handshake->sni_name_len = 0;
1284#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001285
XiaokangQian7807f9f2022-02-15 10:04:37 +00001286 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
1287 if( ret != 0 )
1288 {
1289 MBEDTLS_SSL_DEBUG_RET( 1,
1290 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
1291 return( ret );
1292 }
1293
XiaokangQian7807f9f2022-02-15 10:04:37 +00001294 return( 0 );
1295
1296}
1297
1298/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001299 * Main entry point from the state machine; orchestrates the otherfunctions.
1300 */
1301
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001302MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianed582dd2022-04-13 08:21:05 +00001303static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
1304{
1305
XiaokangQian08037552022-04-20 07:16:41 +00001306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianed582dd2022-04-13 08:21:05 +00001307 unsigned char* buf = NULL;
1308 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001309 int parse_client_hello_ret;
1310
XiaokangQianed582dd2022-04-13 08:21:05 +00001311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1312
XiaokangQianed582dd2022-04-13 08:21:05 +00001313 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
1314 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1315 &buf, &buflen ) );
1316
1317 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
1318 buf + buflen ) );
Jerry Yuf41553b2022-05-09 22:20:30 +08001319 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
1320 * only SSL_CLIENT_HELLO_OK or
1321 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
1322 * stage as negative error codes are handled
1323 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001324
XiaokangQiancfd925f2022-04-14 07:10:37 +00001325 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
Jerry Yu582dd062022-04-22 21:59:01 +08001326
Jerry Yu4ca91402022-05-09 15:50:57 +08001327 if( parse_client_hello_ret == SSL_CLIENT_HELLO_OK )
1328 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1329 else
1330 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
XiaokangQianed582dd2022-04-13 08:21:05 +00001331
1332cleanup:
1333
1334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1335 return( ret );
1336}
1337
1338/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001339 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001340 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001341MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf4b27e42022-03-30 17:32:21 +08001342static int ssl_tls13_prepare_server_hello( mbedtls_ssl_context *ssl )
1343{
Jerry Yu637a3f12022-04-20 21:37:58 +08001344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1345 unsigned char *server_randbytes =
Jerry Yu3bf2c642022-03-30 22:02:12 +08001346 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001347 if( ssl->conf->f_rng == NULL )
1348 {
1349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
1350 return( MBEDTLS_ERR_SSL_NO_RNG );
1351 }
1352
Jerry Yu637a3f12022-04-20 21:37:58 +08001353 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001354 MBEDTLS_SERVER_HELLO_RANDOM_LEN ) ) != 0 )
1355 {
1356 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
1357 return( ret );
1358 }
1359
Jerry Yu637a3f12022-04-20 21:37:58 +08001360 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001361 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1362
1363#if defined(MBEDTLS_HAVE_TIME)
1364 ssl->session_negotiate->start = time( NULL );
1365#endif /* MBEDTLS_HAVE_TIME */
1366
1367 return( ret );
1368}
1369
Jerry Yu3bf2c642022-03-30 22:02:12 +08001370/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001371 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001372 *
1373 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001374 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001375 * } SupportedVersions;
1376 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001377MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001378static int ssl_tls13_write_server_hello_supported_versions_ext(
1379 mbedtls_ssl_context *ssl,
1380 unsigned char *buf,
1381 unsigned char *end,
1382 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +08001383{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001384 *out_len = 0;
1385
Jerry Yu955ddd72022-04-22 22:27:33 +08001386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, write selected version" ) );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001387
1388 /* Check if we have space to write the extension:
1389 * - extension_type (2 bytes)
1390 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001391 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001392 */
Jerry Yu349a6132022-04-14 20:52:56 +08001393 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001394
Jerry Yu349a6132022-04-14 20:52:56 +08001395 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001396
Jerry Yu349a6132022-04-14 20:52:56 +08001397 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001398
Jerry Yu349a6132022-04-14 20:52:56 +08001399 mbedtls_ssl_write_version( buf + 4,
1400 ssl->conf->transport,
1401 ssl->tls_version );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001402
1403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%04x]",
1404 ssl->tls_version ) );
1405
Jerry Yu349a6132022-04-14 20:52:56 +08001406 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001407
1408 return( 0 );
1409}
1410
Jerry Yud9436a12022-04-20 22:28:09 +08001411
Jerry Yu3bf2c642022-03-30 22:02:12 +08001412
1413/* Generate and export a single key share. For hybrid KEMs, this can
1414 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001415MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu955ddd72022-04-22 22:27:33 +08001416static int ssl_tls13_generate_and_write_key_share( mbedtls_ssl_context *ssl,
1417 uint16_t named_group,
1418 unsigned char *buf,
1419 unsigned char *end,
1420 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +08001421{
1422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001423
1424 *out_len = 0;
1425
Jerry Yu89e103c2022-03-30 22:43:29 +08001426#if defined(MBEDTLS_ECDH_C)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001427 if( mbedtls_ssl_tls13_named_group_is_ecdhe( named_group ) )
1428 {
Jerry Yu89e103c2022-03-30 22:43:29 +08001429 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1430 ssl, named_group, buf, end, out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001431 if( ret != 0 )
1432 {
Jerry Yu89e103c2022-03-30 22:43:29 +08001433 MBEDTLS_SSL_DEBUG_RET(
1434 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
1435 ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001436 return( ret );
1437 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001438 }
Jerry Yu89e103c2022-03-30 22:43:29 +08001439 else
1440#endif /* MBEDTLS_ECDH_C */
1441 if( 0 /* Other kinds of KEMs */ )
Jerry Yu3bf2c642022-03-30 22:02:12 +08001442 {
1443 }
1444 else
1445 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001446 ((void) ssl);
1447 ((void) named_group);
1448 ((void) buf);
1449 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001450 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1451 }
1452
1453 return( ret );
1454}
1455
1456/*
1457 * ssl_tls13_write_key_share_ext
1458 *
1459 * Structure of key_share extension in ServerHello:
1460 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001461 * struct {
1462 * NamedGroup group;
1463 * opaque key_exchange<1..2^16-1>;
1464 * } KeyShareEntry;
1465 * struct {
1466 * KeyShareEntry server_share;
1467 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001468 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001469MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3bf2c642022-03-30 22:02:12 +08001470static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
1471 unsigned char *buf,
1472 unsigned char *end,
1473 size_t *out_len )
1474{
Jerry Yu955ddd72022-04-22 22:27:33 +08001475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001476 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001477 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001478 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001479 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001480
1481 *out_len = 0;
1482
1483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding key share extension" ) );
1484
1485 /* Check if we have space for header and length fields:
1486 * - extension_type (2 bytes)
1487 * - extension_data_length (2 bytes)
1488 * - group (2 bytes)
1489 * - key_exchange_length (2 bytes)
1490 */
1491 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
Jerry Yu57d48412022-04-20 21:50:42 +08001492 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, p, 0 );
1493 MBEDTLS_PUT_UINT16_BE( group, server_share, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001494 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001495
Jerry Yu3bf2c642022-03-30 22:02:12 +08001496 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1497 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001498 ret = ssl_tls13_generate_and_write_key_share(
Jerry Yue65d8012022-04-23 10:34:35 +08001499 ssl, group, server_share + 4, end, &key_exchange_length );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001500 if( ret != 0 )
1501 return( ret );
1502 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001503
Jerry Yu955ddd72022-04-22 22:27:33 +08001504 MBEDTLS_PUT_UINT16_BE( key_exchange_length, server_share + 2, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001505
Jerry Yu57d48412022-04-20 21:50:42 +08001506 MBEDTLS_PUT_UINT16_BE( p - server_share, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001507
Jerry Yu57d48412022-04-20 21:50:42 +08001508 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001509
Jerry Yu3bf2c642022-03-30 22:02:12 +08001510 return( 0 );
1511}
Jerry Yud9436a12022-04-20 22:28:09 +08001512
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001513MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001514static int ssl_tls13_write_hrr_key_share_ext( mbedtls_ssl_context *ssl,
1515 unsigned char *buf,
1516 unsigned char *end,
1517 size_t *out_len )
1518{
1519 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1520 /* key_share Extension
1521 *
1522 * struct {
1523 * select (Handshake.msg_type) {
1524 * ...
1525 * case hello_retry_request:
1526 * NamedGroup selected_group;
1527 * ...
1528 * };
1529 * } KeyShare;
1530 */
1531
1532 *out_len = 0;
1533
Jerry Yub0ac10b2022-05-05 11:10:08 +08001534 /*
1535 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1536 * of the HRR is then to transmit a cookie to force the client to demonstrate
1537 * reachability at their apparent network address (primarily useful for DTLS).
1538 */
Ronald Cron79907712022-07-20 17:05:29 +02001539 if( ! mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral( ssl ) )
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001540 return( 0 );
1541
1542 /* We should only send the key_share extension if the client's initial
1543 * key share was not acceptable. */
1544 if( ssl->handshake->offered_group_id != 0 )
1545 {
1546 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Skip key_share extension in HRR" ) );
1547 return( 0 );
1548 }
1549
1550 if( selected_group == 0 )
1551 {
1552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching named group found" ) );
1553 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1554 }
1555
Jerry Yub0ac10b2022-05-05 11:10:08 +08001556 /* Check if we have enough space:
1557 * - extension_type (2 bytes)
1558 * - extension_data_length (2 bytes)
1559 * - selected_group (2 bytes)
1560 */
Ronald Cron154d1b62022-06-01 15:33:26 +02001561 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001562
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001563 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001564 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001565 MBEDTLS_PUT_UINT16_BE( selected_group, buf, 4 );
1566
1567 MBEDTLS_SSL_DEBUG_MSG( 3,
1568 ( "HRR selected_group: %s (%x)",
1569 mbedtls_ssl_named_group_to_str( selected_group ),
1570 selected_group ) );
1571
1572 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001573
Jerry Yub0ac10b2022-05-05 11:10:08 +08001574 return( 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001575}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001576
1577/*
1578 * Structure of ServerHello message:
1579 *
1580 * struct {
1581 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1582 * Random random;
1583 * opaque legacy_session_id_echo<0..32>;
1584 * CipherSuite cipher_suite;
1585 * uint8 legacy_compression_method = 0;
1586 * Extension extensions<6..2^16-1>;
1587 * } ServerHello;
1588 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001589MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3bf2c642022-03-30 22:02:12 +08001590static int ssl_tls13_write_server_hello_body( mbedtls_ssl_context *ssl,
Jerry Yu56404d72022-03-30 17:36:13 +08001591 unsigned char *buf,
1592 unsigned char *end,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001593 size_t *out_len,
1594 int is_hrr )
Jerry Yu56404d72022-03-30 17:36:13 +08001595{
Jerry Yu955ddd72022-04-22 22:27:33 +08001596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001597 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08001598 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08001599 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001600
1601 *out_len = 0;
1602
Jerry Yucfc04b32022-04-21 09:31:58 +08001603 /* ...
1604 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1605 * ...
1606 * with ProtocolVersion defined as:
1607 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001608 */
1609 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1610 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
1611 p += 2;
1612
Jerry Yu1c3e6882022-04-20 21:23:40 +08001613 /* ...
1614 * Random random;
1615 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08001616 * with Random defined as:
1617 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08001618 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001619 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001620 if( is_hrr )
1621 {
1622 memcpy( p, mbedtls_ssl_tls13_hello_retry_request_magic,
Jerry Yufbe3e642022-04-25 19:31:51 +08001623 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001624 }
1625 else
1626 {
1627 memcpy( p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
Jerry Yufbe3e642022-04-25 19:31:51 +08001628 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001629 }
Jerry Yu955ddd72022-04-22 22:27:33 +08001630 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yu3bf2c642022-03-30 22:02:12 +08001631 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1632 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1633
Jerry Yucfc04b32022-04-21 09:31:58 +08001634 /* ...
1635 * opaque legacy_session_id_echo<0..32>;
1636 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08001637 */
1638 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + ssl->session_negotiate->id_len );
1639 *p++ = (unsigned char)ssl->session_negotiate->id_len;
1640 if( ssl->session_negotiate->id_len > 0 )
1641 {
1642 memcpy( p, &ssl->session_negotiate->id[0],
1643 ssl->session_negotiate->id_len );
1644 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08001645
Jerry Yu3bf2c642022-03-30 22:02:12 +08001646 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
1647 ssl->session_negotiate->id_len );
1648 }
1649
Jerry Yucfc04b32022-04-21 09:31:58 +08001650 /* ...
1651 * CipherSuite cipher_suite;
1652 * ...
1653 * with CipherSuite defined as:
1654 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08001655 */
1656 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1657 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
1658 p += 2;
1659 MBEDTLS_SSL_DEBUG_MSG( 3,
1660 ( "server hello, chosen ciphersuite: %s ( id=%d )",
1661 mbedtls_ssl_get_ciphersuite_name(
1662 ssl->session_negotiate->ciphersuite ),
1663 ssl->session_negotiate->ciphersuite ) );
1664
Jerry Yucfc04b32022-04-21 09:31:58 +08001665 /* ...
1666 * uint8 legacy_compression_method = 0;
1667 * ...
1668 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001669 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1670 *p++ = 0x0;
1671
Jerry Yucfc04b32022-04-21 09:31:58 +08001672 /* ...
1673 * Extension extensions<6..2^16-1>;
1674 * ...
1675 * struct {
1676 * ExtensionType extension_type; (2 bytes)
1677 * opaque extension_data<0..2^16-1>;
1678 * } Extension;
1679 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001680 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yud9436a12022-04-20 22:28:09 +08001681 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001682 p += 2;
1683
Jerry Yue74e04a2022-04-21 09:23:16 +08001684 if( ( ret = ssl_tls13_write_server_hello_supported_versions_ext(
Jerry Yu3bf2c642022-03-30 22:02:12 +08001685 ssl, p, end, &output_len ) ) != 0 )
1686 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001687 MBEDTLS_SSL_DEBUG_RET(
1688 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001689 return( ret );
1690 }
1691 p += output_len;
1692
Jerry Yu3bf2c642022-03-30 22:02:12 +08001693 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1694 {
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001695 if( is_hrr )
1696 ret = ssl_tls13_write_hrr_key_share_ext( ssl, p, end, &output_len );
1697 else
1698 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001699 if( ret != 0 )
1700 return( ret );
1701 p += output_len;
1702 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001703
Jerry Yu032b15ce2022-07-11 06:10:03 +00001704#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yu96a2e362022-07-21 15:11:34 +08001705 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Jerry Yu032b15ce2022-07-11 06:10:03 +00001706 {
Jerry Yubb852022022-07-20 21:10:44 +08001707 ret = ssl_tls13_write_server_pre_shared_key_ext( ssl, p, end, &output_len );
Jerry Yu032b15ce2022-07-11 06:10:03 +00001708 if( ret != 0 )
1709 {
Jerry Yubb852022022-07-20 21:10:44 +08001710 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_server_pre_shared_key_ext",
Jerry Yu032b15ce2022-07-11 06:10:03 +00001711 ret );
1712 return( ret );
1713 }
1714 p += output_len;
1715 }
1716#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1717
Jerry Yud9436a12022-04-20 22:28:09 +08001718 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001719
Jerry Yud9436a12022-04-20 22:28:09 +08001720 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello extensions",
1721 p_extensions_len, p - p_extensions_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001722
Jerry Yud9436a12022-04-20 22:28:09 +08001723 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001724
Jerry Yud9436a12022-04-20 22:28:09 +08001725 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello", buf, *out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001726
1727 return( ret );
Jerry Yu56404d72022-03-30 17:36:13 +08001728}
1729
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001730MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue110d252022-05-05 10:19:22 +08001731static int ssl_tls13_finalize_write_server_hello( mbedtls_ssl_context *ssl )
1732{
1733 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf86eb752022-05-06 11:16:55 +08001734 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001735 if( ret != 0 )
1736 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001737 MBEDTLS_SSL_DEBUG_RET( 1,
1738 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yue110d252022-05-05 10:19:22 +08001739 ret );
1740 return( ret );
1741 }
1742
Jerry Yue110d252022-05-05 10:19:22 +08001743 return( ret );
1744}
1745
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001746MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu5b64ae92022-03-30 17:15:02 +08001747static int ssl_tls13_write_server_hello( mbedtls_ssl_context *ssl )
1748{
Jerry Yu637a3f12022-04-20 21:37:58 +08001749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001750 unsigned char *buf;
1751 size_t buf_len, msg_len;
1752
1753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1754
Jerry Yuf4b27e42022-03-30 17:32:21 +08001755 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_server_hello( ssl ) );
1756
Jerry Yu3bf2c642022-03-30 22:02:12 +08001757 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001758 MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len ) );
1759
Jerry Yu3bf2c642022-03-30 22:02:12 +08001760 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1761 buf + buf_len,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001762 &msg_len,
1763 0 ) );
Jerry Yuf4b27e42022-03-30 17:32:21 +08001764
Jerry Yu3bf2c642022-03-30 22:02:12 +08001765 mbedtls_ssl_add_hs_msg_to_checksum(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001766 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1767
Jerry Yu3bf2c642022-03-30 22:02:12 +08001768 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001769 ssl, buf_len, msg_len ) );
Jerry Yu637a3f12022-04-20 21:37:58 +08001770
Jerry Yue110d252022-05-05 10:19:22 +08001771 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_server_hello( ssl ) );
1772
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001773#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1774 /* The server sends a dummy change_cipher_spec record immediately
1775 * after its first handshake message. This may either be after
1776 * a ServerHello or a HelloRetryRequest.
1777 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001778 mbedtls_ssl_handshake_set_state(
1779 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001780#else
Jerry Yu637a3f12022-04-20 21:37:58 +08001781 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001782#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08001783
Jerry Yuf4b27e42022-03-30 17:32:21 +08001784cleanup:
1785
1786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1787 return( ret );
Jerry Yu5b64ae92022-03-30 17:15:02 +08001788}
1789
Jerry Yu23d1a252022-05-12 18:08:59 +08001790
1791/*
1792 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
1793 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001794MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b840462022-06-01 17:05:53 +02001795static int ssl_tls13_prepare_hello_retry_request( mbedtls_ssl_context *ssl )
Jerry Yu23d1a252022-05-12 18:08:59 +08001796{
1797 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1798 if( ssl->handshake->hello_retry_request_count > 0 )
1799 {
1800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Too many HRRs" ) );
1801 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1802 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1803 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1804 }
1805
1806 /*
1807 * Create stateless transcript hash for HRR
1808 */
1809 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
1810 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
1811 if( ret != 0 )
1812 {
1813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr", ret );
1814 return( ret );
1815 }
1816 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
1817
1818 return( 0 );
1819}
1820
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001821MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu23d1a252022-05-12 18:08:59 +08001822static int ssl_tls13_write_hello_retry_request( mbedtls_ssl_context *ssl )
1823{
1824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1825 unsigned char *buf;
1826 size_t buf_len, msg_len;
1827
1828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello retry request" ) );
1829
Ronald Cron7b840462022-06-01 17:05:53 +02001830 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_hello_retry_request( ssl ) );
Jerry Yu23d1a252022-05-12 18:08:59 +08001831
1832 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
1833 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1834 &buf, &buf_len ) );
1835
1836 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1837 buf + buf_len,
1838 &msg_len,
1839 1 ) );
1840 mbedtls_ssl_add_hs_msg_to_checksum(
1841 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1842
1843
1844 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, buf_len,
1845 msg_len ) );
1846
1847 ssl->handshake->hello_retry_request_count++;
1848
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001849#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1850 /* The server sends a dummy change_cipher_spec record immediately
1851 * after its first handshake message. This may either be after
1852 * a ServerHello or a HelloRetryRequest.
1853 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001854 mbedtls_ssl_handshake_set_state(
Gabor Mezeif7044ea2022-06-28 16:01:49 +02001855 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001856#else
Jerry Yu23d1a252022-05-12 18:08:59 +08001857 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001858#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08001859
1860cleanup:
1861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello retry request" ) );
1862 return( ret );
1863}
1864
Jerry Yu5b64ae92022-03-30 17:15:02 +08001865/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08001866 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
1867 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001868
1869/*
1870 * struct {
1871 * Extension extensions<0..2 ^ 16 - 1>;
1872 * } EncryptedExtensions;
1873 *
1874 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001875MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d3841a2022-04-16 12:37:19 +08001876static int ssl_tls13_write_encrypted_extensions_body( mbedtls_ssl_context *ssl,
1877 unsigned char *buf,
1878 unsigned char *end,
1879 size_t *out_len )
1880{
XiaokangQianacb39922022-06-17 10:18:48 +00001881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001882 unsigned char *p = buf;
1883 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08001884 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001885 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08001886
Jerry Yu4d3841a2022-04-16 12:37:19 +08001887 *out_len = 0;
1888
1889 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yu8937eb42022-05-03 12:12:14 +08001890 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001891 p += 2;
1892
Jerry Yu8937eb42022-05-03 12:12:14 +08001893 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00001894 ((void) ret);
1895 ((void) output_len);
1896
1897#if defined(MBEDTLS_SSL_ALPN)
1898 ret = mbedtls_ssl_write_alpn_ext( ssl, p, end, &output_len );
1899 if( ret != 0 )
1900 return( ret );
XiaokangQian95d5f542022-06-24 02:29:26 +00001901 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001902#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001903
Jerry Yu8937eb42022-05-03 12:12:14 +08001904 extensions_len = ( p - p_extensions_len ) - 2;
1905 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
1906
1907 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001908
1909 MBEDTLS_SSL_DEBUG_BUF( 4, "encrypted extensions", buf, *out_len );
1910
1911 return( 0 );
1912}
1913
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001914MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d3841a2022-04-16 12:37:19 +08001915static int ssl_tls13_write_encrypted_extensions( mbedtls_ssl_context *ssl )
1916{
1917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1918 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08001919 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001920
Gabor Mezei54719122022-06-28 11:34:56 +02001921 mbedtls_ssl_set_outbound_transform( ssl,
1922 ssl->handshake->transform_handshake );
1923 MBEDTLS_SSL_DEBUG_MSG(
1924 3, ( "switching to handshake transform for outbound data" ) );
1925
Jerry Yuab452cc2022-04-28 15:27:08 +08001926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001927
Jerry Yu4d3841a2022-04-16 12:37:19 +08001928 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1929 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf, &buf_len ) );
1930
1931 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_encrypted_extensions_body(
1932 ssl, buf, buf + buf_len, &msg_len ) );
1933
1934 mbedtls_ssl_add_hs_msg_to_checksum(
1935 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len );
1936
Jerry Yu4d3841a2022-04-16 12:37:19 +08001937 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1938 ssl, buf_len, msg_len ) );
1939
Jerry Yu8937eb42022-05-03 12:12:14 +08001940#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02001941 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Jerry Yu8937eb42022-05-03 12:12:14 +08001942 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1943 else
XiaokangQiana987e1d2022-05-07 01:25:58 +00001944 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yu8937eb42022-05-03 12:12:14 +08001945#else
1946 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1947#endif
1948
Jerry Yu4d3841a2022-04-16 12:37:19 +08001949cleanup:
1950
Jerry Yuab452cc2022-04-28 15:27:08 +08001951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001952 return( ret );
1953}
1954
XiaokangQiancec9ae62022-05-06 07:28:50 +00001955#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00001956#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
1957#define SSL_CERTIFICATE_REQUEST_SKIP 1
1958/* Coordination:
1959 * Check whether a CertificateRequest message should be written.
1960 * Returns a negative code on failure, or
1961 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
1962 * - SSL_CERTIFICATE_REQUEST_SKIP
1963 * indicating if the writing of the CertificateRequest
1964 * should be skipped or not.
1965 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001966MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiana987e1d2022-05-07 01:25:58 +00001967static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
1968{
1969 int authmode;
1970
XiaokangQian40a35232022-05-07 09:02:40 +00001971#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1972 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
1973 authmode = ssl->handshake->sni_authmode;
1974 else
1975#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00001976 authmode = ssl->conf->authmode;
1977
1978 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
1979 return( SSL_CERTIFICATE_REQUEST_SKIP );
1980
XiaokangQianc3017f62022-05-13 05:55:41 +00001981 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00001982
XiaokangQiana987e1d2022-05-07 01:25:58 +00001983 return( SSL_CERTIFICATE_REQUEST_SEND_REQUEST );
1984}
1985
XiaokangQiancec9ae62022-05-06 07:28:50 +00001986/*
1987 * struct {
1988 * opaque certificate_request_context<0..2^8-1>;
1989 * Extension extensions<2..2^16-1>;
1990 * } CertificateRequest;
1991 *
1992 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001993MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiancec9ae62022-05-06 07:28:50 +00001994static int ssl_tls13_write_certificate_request_body( mbedtls_ssl_context *ssl,
1995 unsigned char *buf,
1996 const unsigned char *end,
1997 size_t *out_len )
1998{
1999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2000 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002001 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002002 unsigned char *p_extensions_len;
2003
2004 *out_len = 0;
2005
2006 /* Check if we have enough space:
2007 * - certificate_request_context (1 byte)
2008 * - extensions length (2 bytes)
2009 */
2010 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
2011
2012 /*
2013 * Write certificate_request_context
2014 */
2015 /*
2016 * We use a zero length context for the normal handshake
2017 * messages. For post-authentication handshake messages
2018 * this request context would be set to a non-zero value.
2019 */
2020 *p++ = 0x0;
2021
2022 /*
2023 * Write extensions
2024 */
2025 /* The extensions must contain the signature_algorithms. */
2026 p_extensions_len = p;
2027 p += 2;
XiaokangQianec6efb92022-05-06 09:53:10 +00002028 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
XiaokangQiancec9ae62022-05-06 07:28:50 +00002029 if( ret != 0 )
2030 return( ret );
2031
XiaokangQianec6efb92022-05-06 09:53:10 +00002032 p += output_len;
XiaokangQianaad9b0a2022-05-09 01:11:21 +00002033 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
XiaokangQiancec9ae62022-05-06 07:28:50 +00002034
2035 *out_len = p - buf;
2036
2037 return( 0 );
2038}
2039
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002040MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiancec9ae62022-05-06 07:28:50 +00002041static int ssl_tls13_write_certificate_request( mbedtls_ssl_context *ssl )
2042{
2043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2044
2045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2046
2047 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2048
2049 if( ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST )
2050 {
2051 unsigned char *buf;
2052 size_t buf_len, msg_len;
2053
2054 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
2055 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, &buf, &buf_len ) );
2056
2057 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_request_body(
2058 ssl, buf, buf + buf_len, &msg_len ) );
2059
2060 mbedtls_ssl_add_hs_msg_to_checksum(
2061 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len );
2062
2063 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
2064 ssl, buf_len, msg_len ) );
2065 }
2066 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
2067 {
2068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2069 ret = 0;
2070 }
2071 else
2072 {
2073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2074 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2075 goto cleanup;
2076 }
2077
2078 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2079cleanup:
2080
2081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2082 return( ret );
2083}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002084
Jerry Yu4d3841a2022-04-16 12:37:19 +08002085/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002086 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002087 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002088MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002089static int ssl_tls13_write_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08002090{
Jerry Yu5a26f302022-05-10 20:46:40 +08002091 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002092
2093#if defined(MBEDTLS_X509_CRT_PARSE_C)
2094 if( ( ssl_tls13_pick_key_cert( ssl ) != 0 ) ||
2095 mbedtls_ssl_own_cert( ssl ) == NULL )
Jerry Yu5a26f302022-05-10 20:46:40 +08002096 {
Jerry Yub89125b2022-05-13 15:45:49 +08002097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate available." ) );
Jerry Yu5a26f302022-05-10 20:46:40 +08002098 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2099 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08002100 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu5a26f302022-05-10 20:46:40 +08002101 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002102#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002103
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08002104 ret = mbedtls_ssl_tls13_write_certificate( ssl );
2105 if( ret != 0 )
Jerry Yu1bff7112022-04-16 14:29:11 +08002106 return( ret );
Jerry Yu1bff7112022-04-16 14:29:11 +08002107 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2108 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08002109}
2110
2111/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002112 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002113 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002114MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002115static int ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08002116{
Jerry Yu4ff9e142022-04-16 14:57:49 +08002117 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08002118 if( ret != 0 )
Jerry Yu4ff9e142022-04-16 14:57:49 +08002119 return( ret );
Jerry Yu4ff9e142022-04-16 14:57:49 +08002120 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2121 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08002122}
Jerry Yu5a26f302022-05-10 20:46:40 +08002123#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002124
2125/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002126 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002127 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002128MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d8567f2022-04-17 10:57:57 +08002129static int ssl_tls13_write_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08002130{
Jerry Yud6e253d2022-05-18 13:59:24 +08002131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002132
2133 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2134 if( ret != 0 )
2135 return( ret );
2136
Jerry Yue3d67cb2022-05-19 15:33:10 +08002137 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2138 if( ret != 0 )
2139 {
2140 MBEDTLS_SSL_PEND_FATAL_ALERT(
2141 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2142 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2143 return( ret );
2144 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002145
Ronald Cron63dc4632022-05-31 14:41:53 +02002146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
2147 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
XiaokangQian189ded22022-05-10 08:12:17 +00002148
Ronald Cron63dc4632022-05-31 14:41:53 +02002149 if( ssl->handshake->certificate_request_sent )
2150 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
XiaokangQian189ded22022-05-10 08:12:17 +00002151 else
Ronald Cron19385882022-06-15 16:26:13 +02002152 {
2153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate" ) );
2154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
XiaokangQian189ded22022-05-10 08:12:17 +00002155 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002156 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002157
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002158 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08002159}
2160
2161/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002162 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002163 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002164MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d8567f2022-04-17 10:57:57 +08002165static int ssl_tls13_process_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08002166{
Jerry Yud6e253d2022-05-18 13:59:24 +08002167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2168
Jerry Yuff226982022-04-16 16:52:57 +08002169 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
2170 if( ret != 0 )
2171 return( ret );
2172
Jerry Yue3d67cb2022-05-19 15:33:10 +08002173 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
2174 if( ret != 0 )
2175 {
2176 MBEDTLS_SSL_DEBUG_RET( 1,
2177 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
2178 }
2179
Jerry Yu03ed50b2022-04-16 17:13:30 +08002180 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yuff226982022-04-16 16:52:57 +08002181 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08002182}
2183
2184/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002185 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002186 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002187MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d8567f2022-04-17 10:57:57 +08002188static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08002189{
Jerry Yu03ed50b2022-04-16 17:13:30 +08002190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2191
Jerry Yu03ed50b2022-04-16 17:13:30 +08002192 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2193 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2194 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08002195}
2196
2197/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00002198 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00002199 */
Jerry Yu27561932021-08-27 17:07:38 +08002200int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +08002201{
XiaokangQian08037552022-04-20 07:16:41 +00002202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002203
2204 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
2205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2206
Jerry Yue3b34122021-09-28 17:53:35 +08002207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
2208 mbedtls_ssl_states_str( ssl->state ),
2209 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +08002210
XiaokangQian7807f9f2022-02-15 10:04:37 +00002211 switch( ssl->state )
2212 {
2213 /* start state */
2214 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +00002215 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
XiaokangQian08037552022-04-20 07:16:41 +00002216 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002217 break;
2218
XiaokangQian7807f9f2022-02-15 10:04:37 +00002219 case MBEDTLS_SSL_CLIENT_HELLO:
XiaokangQian4080a7f2022-04-11 09:55:18 +00002220 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +00002221 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +00002222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
Jerry Yuf41553b2022-05-09 22:20:30 +08002223 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002224
Jerry Yuf41553b2022-05-09 22:20:30 +08002225 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
2226 ret = ssl_tls13_write_hello_retry_request( ssl );
2227 if( ret != 0 )
2228 {
2229 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_hello_retry_request", ret );
2230 return( ret );
2231 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002232 break;
2233
Jerry Yu5b64ae92022-03-30 17:15:02 +08002234 case MBEDTLS_SSL_SERVER_HELLO:
2235 ret = ssl_tls13_write_server_hello( ssl );
2236 break;
2237
Xiaofei Baicba64af2022-02-15 10:00:56 +00002238 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Jerry Yu4d3841a2022-04-16 12:37:19 +08002239 ret = ssl_tls13_write_encrypted_extensions( ssl );
Xiaofei Baicba64af2022-02-15 10:00:56 +00002240 if( ret != 0 )
2241 {
Jerry Yu4d3841a2022-04-16 12:37:19 +08002242 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_encrypted_extensions", ret );
2243 return( ret );
Xiaofei Baicba64af2022-02-15 10:00:56 +00002244 }
Jerry Yu48330562022-05-06 21:35:44 +08002245 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08002246
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002247#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
2248 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Xiaofei Bai5ee73d82022-03-14 02:48:30 +00002249 ret = ssl_tls13_write_certificate_request( ssl );
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002250 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002251
Jerry Yu83da34e2022-04-16 13:59:52 +08002252 case MBEDTLS_SSL_SERVER_CERTIFICATE:
2253 ret = ssl_tls13_write_server_certificate( ssl );
2254 break;
2255
2256 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
2257 ret = ssl_tls13_write_certificate_verify( ssl );
2258 break;
Jerry Yu5a26f302022-05-10 20:46:40 +08002259#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002260
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002261 /*
2262 * Injection of dummy-CCS's for middlebox compatibility
2263 */
2264#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02002265 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002266 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2267 if( ret == 0 )
2268 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2269 break;
2270
2271 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
2272 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2273 if( ret == 0 )
2274 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
2275 break;
2276#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2277
Jerry Yu69dd8d42022-04-16 12:51:26 +08002278 case MBEDTLS_SSL_SERVER_FINISHED:
2279 ret = ssl_tls13_write_server_finished( ssl );
2280 break;
2281
2282 case MBEDTLS_SSL_CLIENT_FINISHED:
2283 ret = ssl_tls13_process_client_finished( ssl );
2284 break;
2285
Jerry Yu69dd8d42022-04-16 12:51:26 +08002286 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
2287 ret = ssl_tls13_handshake_wrapup( ssl );
2288 break;
2289
XiaokangQian6b916b12022-04-25 07:29:34 +00002290 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2291 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Ronald Cron209cae92022-06-07 10:30:19 +02002292 if( ret == 0 )
XiaokangQian6b916b12022-04-25 07:29:34 +00002293 {
Ronald Cron209cae92022-06-07 10:30:19 +02002294 if( ssl->session_negotiate->peer_cert != NULL )
2295 {
2296 mbedtls_ssl_handshake_set_state(
2297 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2298 }
2299 else
Ronald Cron19385882022-06-15 16:26:13 +02002300 {
2301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
Ronald Cron209cae92022-06-07 10:30:19 +02002302 mbedtls_ssl_handshake_set_state(
2303 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002304 }
XiaokangQian6b916b12022-04-25 07:29:34 +00002305 }
2306 break;
2307
2308 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2309 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2310 if( ret == 0 )
2311 {
2312 mbedtls_ssl_handshake_set_state(
2313 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2314 }
2315 break;
2316
XiaokangQian7807f9f2022-02-15 10:04:37 +00002317 default:
2318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
XiaokangQian060d8672022-04-21 09:24:56 +00002319 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian7807f9f2022-02-15 10:04:37 +00002320 }
2321
2322 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +08002323}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002324
Jerry Yufb4b6472022-01-27 15:03:26 +08002325#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */