blob: bf49af21b41fb6350cb04c134bd152dc1083b980 [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{
Jerry Yu77f01482022-07-11 07:03:24 +0000695 return( ssl_tls13_client_hello_has_exts(
696 ssl,
697 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
698 MBEDTLS_SSL_EXT_KEY_SHARE |
699 MBEDTLS_SSL_EXT_SIG_ALG ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000700}
701
Jerry Yu77f01482022-07-11 07:03:24 +0000702#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
703MBEDTLS_CHECK_RETURN_CRITICAL
704static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
705 mbedtls_ssl_context *ssl )
706{
707 return( ssl_tls13_client_hello_has_exts(
708 ssl,
709 MBEDTLS_SSL_EXT_PRE_SHARED_KEY |
710 MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) );
711}
712
713MBEDTLS_CHECK_RETURN_CRITICAL
714static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
715 mbedtls_ssl_context *ssl )
716{
717 return( ssl_tls13_client_hello_has_exts(
718 ssl,
719 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
720 MBEDTLS_SSL_EXT_KEY_SHARE |
721 MBEDTLS_SSL_EXT_PRE_SHARED_KEY |
722 MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) );
723}
724#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
725
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200726MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000727static int ssl_tls13_check_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000728{
Jerry Yu77f01482022-07-11 07:03:24 +0000729 return( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) &&
730 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange( ssl ) );
731}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000732
Jerry Yu77f01482022-07-11 07:03:24 +0000733MBEDTLS_CHECK_RETURN_CRITICAL
734static int ssl_tls13_check_psk_key_exchange( mbedtls_ssl_context *ssl )
735{
736#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
737 return( mbedtls_ssl_conf_tls13_psk_enabled( ssl ) &&
738 mbedtls_ssl_tls13_psk_enabled( ssl ) &&
739 ssl_tls13_client_hello_has_exts_for_psk_key_exchange( ssl ) );
740#else
741 ((void) ssl);
742 return( 0 );
743#endif
744}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000745
Jerry Yu77f01482022-07-11 07:03:24 +0000746MBEDTLS_CHECK_RETURN_CRITICAL
747static int ssl_tls13_check_psk_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
748{
749#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
750 return( mbedtls_ssl_conf_tls13_psk_ephemeral_enabled( ssl ) &&
751 mbedtls_ssl_tls13_psk_ephemeral_enabled( ssl ) &&
752 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange( ssl ) );
753#else
754 ((void) ssl);
755 return( 0 );
756#endif
757}
758
759static int ssl_tls13_determine_key_exchange_mode( mbedtls_ssl_context *ssl )
760{
761 /*
762 * Determine the key exchange algorithm to use.
763 * There are three types of key exchanges supported in TLS 1.3:
764 * - (EC)DH with ECDSA,
765 * - (EC)DH with PSK,
766 * - plain PSK.
767 *
768 * The PSK-based key exchanges may additionally be used with 0-RTT.
769 *
770 * Our built-in order of preference is
771 * 1 ) Plain PSK Mode ( psk )
772 * 2 ) (EC)DHE-PSK Mode ( psk_ephemeral )
773 * 3 ) Certificate Mode ( ephemeral )
774 */
775
776 ssl->handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
777
778 if( ssl_tls13_check_psk_key_exchange( ssl ) )
779 {
780 ssl->handshake->key_exchange_mode =
781 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk" ) );
783 }
784 else
785 if( ssl_tls13_check_psk_ephemeral_key_exchange( ssl ) )
786 {
787 ssl->handshake->key_exchange_mode =
788 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk_ephemeral" ) );
790 }
791 else
792 if( ssl_tls13_check_ephemeral_key_exchange( ssl ) )
793 {
794 ssl->handshake->key_exchange_mode =
795 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: ephemeral" ) );
797 }
798 else
799 {
800 MBEDTLS_SSL_DEBUG_MSG(
801 1,
802 ( "ClientHello message misses mandatory extensions." ) );
803 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
804 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
805 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
806 }
807
808 return( 0 );
809
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810}
811
XiaokangQian81802f42022-06-10 13:25:22 +0000812#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
813 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian23c5be62022-06-07 02:04:34 +0000814/*
XiaokangQianfb665a82022-06-15 03:57:21 +0000815 * Pick best ( private key, certificate chain ) pair based on the signature
816 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +0000817 */
Ronald Cronce7d76e2022-07-08 18:56:49 +0200818MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian07aad072022-06-14 05:35:09 +0000819static int ssl_tls13_pick_key_cert( mbedtls_ssl_context *ssl )
XiaokangQian23c5be62022-06-07 02:04:34 +0000820{
XiaokangQianfb665a82022-06-15 03:57:21 +0000821 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +0000822 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +0000823
824#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
825 if( ssl->handshake->sni_key_cert != NULL )
XiaokangQianfb665a82022-06-15 03:57:21 +0000826 key_cert_list = ssl->handshake->sni_key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000827 else
828#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianfb665a82022-06-15 03:57:21 +0000829 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000830
XiaokangQianfb665a82022-06-15 03:57:21 +0000831 if( key_cert_list == NULL )
XiaokangQian23c5be62022-06-07 02:04:34 +0000832 {
833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
834 return( -1 );
835 }
836
XiaokangQian81802f42022-06-10 13:25:22 +0000837 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
XiaokangQian23c5be62022-06-07 02:04:34 +0000838 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000839 for( key_cert = key_cert_list; key_cert != NULL;
840 key_cert = key_cert->next )
XiaokangQian23c5be62022-06-07 02:04:34 +0000841 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000842 MBEDTLS_SSL_DEBUG_CRT( 3, "certificate (chain) candidate",
843 key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000844
845 /*
846 * This avoids sending the client a cert it'll reject based on
847 * keyUsage or other extensions.
848 */
849 if( mbedtls_x509_crt_check_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000850 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +0000851 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000852 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
853 MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ) ) != 0 )
XiaokangQian81802f42022-06-10 13:25:22 +0000854 {
855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
XiaokangQianfb665a82022-06-15 03:57:21 +0000856 "(extended) key usage extension" ) );
XiaokangQian81802f42022-06-10 13:25:22 +0000857 continue;
858 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000859
Jerry Yucc539102022-06-27 16:27:35 +0800860 MBEDTLS_SSL_DEBUG_MSG( 3,
861 ( "ssl_tls13_pick_key_cert:"
862 "check signature algorithm %s [%04x]",
863 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
864 *sig_alg ) );
Jerry Yufb526692022-06-19 11:22:49 +0800865 if( mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
Jerry Yud099cf02022-06-19 13:47:00 +0800866 *sig_alg, &key_cert->cert->pk ) )
XiaokangQian81802f42022-06-10 13:25:22 +0000867 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000868 ssl->handshake->key_cert = key_cert;
Jerry Yucc539102022-06-27 16:27:35 +0800869 MBEDTLS_SSL_DEBUG_MSG( 3,
870 ( "ssl_tls13_pick_key_cert:"
871 "selected signature algorithm"
872 " %s [%04x]",
873 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
874 *sig_alg ) );
XiaokangQianfb665a82022-06-15 03:57:21 +0000875 MBEDTLS_SSL_DEBUG_CRT(
XiaokangQian75fe8c72022-06-15 09:42:45 +0000876 3, "selected certificate (chain)",
XiaokangQianfb665a82022-06-15 03:57:21 +0000877 ssl->handshake->key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000878 return( 0 );
879 }
XiaokangQian81802f42022-06-10 13:25:22 +0000880 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000881 }
882
Jerry Yu9d3e2fa2022-06-27 22:14:01 +0800883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ssl_tls13_pick_key_cert:"
884 "no suitable certificate found" ) );
XiaokangQian23c5be62022-06-07 02:04:34 +0000885 return( -1 );
886}
XiaokangQian81802f42022-06-10 13:25:22 +0000887#endif /* MBEDTLS_X509_CRT_PARSE_C &&
888 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +0000889
XiaokangQian4080a7f2022-04-11 09:55:18 +0000890/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000891 *
892 * STATE HANDLING: ClientHello
893 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000894 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000895 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000896 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000897 *
898 * In this case, the server progresses to sending its ServerHello.
899 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000900 * 2) The ClientHello was well-formed but didn't match the server's
901 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000902 *
903 * For example, the client might not have offered a key share which
904 * the server supports, or the server might require a cookie.
905 *
906 * In this case, the server sends a HelloRetryRequest.
907 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000908 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000909 *
910 * In this case, we abort the handshake.
911 *
912 */
913
914/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000915 * Structure of this message:
916 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000917 * uint16 ProtocolVersion;
918 * opaque Random[32];
919 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +0000920 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000921 * struct {
922 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
923 * Random random;
924 * opaque legacy_session_id<0..32>;
925 * CipherSuite cipher_suites<2..2^16-2>;
926 * opaque legacy_compression_methods<1..2^8-1>;
927 * Extension extensions<8..2^16-1>;
928 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000929 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000930
931#define SSL_CLIENT_HELLO_OK 0
932#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
933
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200934MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian4080a7f2022-04-11 09:55:18 +0000935static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
936 const unsigned char *buf,
937 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000938{
XiaokangQianb67384d2022-04-19 00:02:38 +0000939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
940 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000941 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000942 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000943 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000944 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000945 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946 const unsigned char *extensions_end;
Jerry Yu49ca9282022-05-05 11:05:22 +0800947 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948
XiaokangQian7807f9f2022-02-15 10:04:37 +0000949 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
Jerry Yu1c105562022-07-10 06:32:38 +0000950#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
951 const unsigned char *pre_shared_key_ext_start = NULL;
952 const unsigned char *pre_shared_key_ext_end = NULL;
953#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000954
955 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
956
957 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000958 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000959 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +0000960 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +0000961 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000963 * .. . .. ciphersuite list length ( 2 bytes )
964 * .. . .. ciphersuite list
965 * .. . .. compression alg. list length ( 1 byte )
966 * .. . .. compression alg. list
967 * .. . .. extensions length ( 2 bytes, optional )
968 * .. . .. extensions ( optional )
969 */
970
XiaokangQianb67384d2022-04-19 00:02:38 +0000971 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400972 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +0000973 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
974 * read at least up to session id length without worrying.
975 */
976 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
977
978 /* ...
979 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
980 * ...
981 * with ProtocolVersion defined as:
982 * uint16 ProtocolVersion;
983 */
XiaokangQiande333912022-04-20 08:49:42 +0000984 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
985 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000986 {
987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
988 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
989 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000990 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000991 }
992 p += 2;
993
994 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000995 * Only support TLS 1.3 currently, temporarily set the version.
996 */
XiaokangQiande333912022-04-20 08:49:42 +0000997 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +0000998
Jerry Yuc1be19f2022-04-23 16:11:39 +0800999 /* ...
1000 * Random random;
1001 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001002 * with Random defined as:
1003 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001004 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001005 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +00001006 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001007
XiaokangQian08037552022-04-20 07:16:41 +00001008 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
1009 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001010
Jerry Yuc1be19f2022-04-23 16:11:39 +08001011 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001012 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001013 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001014 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001015 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +00001016 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001017
XiaokangQianb67384d2022-04-19 00:02:38 +00001018 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001019 {
1020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1021 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1022 }
1023
XiaokangQian4080a7f2022-04-11 09:55:18 +00001024 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +00001025 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
XiaokangQiane8ff3502022-04-22 02:34:40 +00001026 p, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +00001027 /*
1028 * Check we have enough data for the legacy session identifier
1029 * and the ciphersuite list length.
1030 */
1031 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001032
XiaokangQianed582dd2022-04-13 08:21:05 +00001033 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +00001034 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001035
XiaokangQian7807f9f2022-02-15 10:04:37 +00001036 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1037 p += 2;
1038
XiaokangQianb67384d2022-04-19 00:02:38 +00001039 /* Check we have enough data for the ciphersuite list, the legacy
1040 * compression methods and the length of the extensions.
1041 */
1042 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001043
Jerry Yuc1be19f2022-04-23 16:11:39 +08001044 /* ...
XiaokangQian08037552022-04-20 07:16:41 +00001045 * CipherSuite cipher_suites<2..2^16-2>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001046 * ...
XiaokangQian08037552022-04-20 07:16:41 +00001047 * with CipherSuite defined as:
1048 * uint8 CipherSuite[2];
1049 */
XiaokangQian060d8672022-04-21 09:24:56 +00001050 cipher_suites = p;
1051 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001052 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1053 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +00001054 /*
1055 * Search for a matching ciphersuite
1056 */
XiaokangQian318dc762022-04-20 09:43:51 +00001057 int ciphersuite_match = 0;
XiaokangQian060d8672022-04-21 09:24:56 +00001058 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +00001059 {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001060 uint16_t cipher_suite;
1061 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, cipher_suites_end, 2 );
1062 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1063 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
XiaokangQian08037552022-04-20 07:16:41 +00001064 /*
XiaokangQiane8ff3502022-04-22 02:34:40 +00001065 * Check whether this ciphersuite is valid and offered.
1066 */
XiaokangQian08037552022-04-20 07:16:41 +00001067 if( ( mbedtls_ssl_validate_ciphersuite(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001068 ssl, ciphersuite_info, ssl->tls_version,
1069 ssl->tls_version ) != 0 ) ||
1070 ! mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
1071 {
XiaokangQian08037552022-04-20 07:16:41 +00001072 continue;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001073 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001074
XiaokangQian08037552022-04-20 07:16:41 +00001075 ssl->session_negotiate->ciphersuite = cipher_suite;
1076 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +00001077 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +00001078
XiaokangQian08037552022-04-20 07:16:41 +00001079 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001080
XiaokangQian17f974c2022-04-19 09:57:41 +00001081 }
1082
Jerry Yuc1be19f2022-04-23 16:11:39 +08001083 if( ! ciphersuite_match )
XiaokangQian318dc762022-04-20 09:43:51 +00001084 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +00001085 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1086 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1087 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +00001088 }
XiaokangQian17f974c2022-04-19 09:57:41 +00001089
1090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
1091 ciphersuite_info->name ) );
1092
XiaokangQian060d8672022-04-21 09:24:56 +00001093 p = cipher_suites + cipher_suites_len;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001094
XiaokangQian4080a7f2022-04-11 09:55:18 +00001095 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001096 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001097 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001098 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +00001099 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001100 {
XiaokangQian4080a7f2022-04-11 09:55:18 +00001101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
1102 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1103 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1104 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001105 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001106 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001107
Jerry Yuc1be19f2022-04-23 16:11:39 +08001108 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001109 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001110 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001111 * with Extension defined as:
1112 * struct {
1113 * ExtensionType extension_type;
1114 * opaque extension_data<0..2^16-1>;
1115 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001116 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001117 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001118 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001119 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQiane8ff3502022-04-22 02:34:40 +00001120 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001121
XiaokangQian4080a7f2022-04-11 09:55:18 +00001122 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001123
1124 while( p < extensions_end )
1125 {
1126 unsigned int extension_type;
1127 size_t extension_data_len;
1128 const unsigned char *extension_data_end;
1129
Jerry Yu1c9247c2022-07-21 12:37:39 +08001130 /* RFC 8446, page 57
1131 *
1132 * The "pre_shared_key" extension MUST be the last extension in the
1133 * ClientHello (this facilitates implementation as described below).
1134 * Servers MUST check that it is the last extension and otherwise fail
1135 * the handshake with an "illegal_parameter" alert.
1136 */
1137 if( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY )
1138 {
1139 MBEDTLS_SSL_DEBUG_MSG(
1140 3, ( "pre_shared_key is not last extension." ) );
1141 MBEDTLS_SSL_PEND_FATAL_ALERT(
1142 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1143 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1144 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1145 }
1146
XiaokangQian318dc762022-04-20 09:43:51 +00001147 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001148 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1149 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1150 p += 4;
1151
1152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1153 extension_data_end = p + extension_data_len;
1154
1155 switch( extension_type )
1156 {
XiaokangQian40a35232022-05-07 09:02:40 +00001157#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1158 case MBEDTLS_TLS_EXT_SERVERNAME:
1159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
XiaokangQian9b2b7712022-05-17 02:57:00 +00001160 ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
1161 extension_data_end );
XiaokangQian40a35232022-05-07 09:02:40 +00001162 if( ret != 0 )
1163 {
1164 MBEDTLS_SSL_DEBUG_RET(
1165 1, "mbedtls_ssl_parse_servername_ext", ret );
1166 return( ret );
1167 }
1168 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
1169 break;
1170#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1171
XiaokangQianb67384d2022-04-19 00:02:38 +00001172#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001173 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
1175
1176 /* Supported Groups Extension
1177 *
1178 * When sent by the client, the "supported_groups" extension
1179 * indicates the named groups which the client supports,
1180 * ordered from most preferred to least preferred.
1181 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001182 ret = ssl_tls13_parse_supported_groups_ext(
1183 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001184 if( ret != 0 )
1185 {
1186 MBEDTLS_SSL_DEBUG_RET( 1,
1187 "mbedtls_ssl_parse_supported_groups_ext", ret );
1188 return( ret );
1189 }
1190
1191 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
1192 break;
XiaokangQianb67384d2022-04-19 00:02:38 +00001193#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001194
XiaokangQian88408882022-04-02 10:15:03 +00001195#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001196 case MBEDTLS_TLS_EXT_KEY_SHARE:
1197 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
1198
1199 /*
1200 * Key Share Extension
1201 *
1202 * When sent by the client, the "key_share" extension
1203 * contains the endpoint's cryptographic parameters for
1204 * ECDHE/DHE key establishment methods.
1205 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001206 ret = ssl_tls13_parse_key_shares_ext(
1207 ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +00001208 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001209 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
Jerry Yu49ca9282022-05-05 11:05:22 +08001211 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001212 }
1213
Jerry Yu582dd062022-04-22 21:59:01 +08001214 if( ret < 0 )
1215 {
1216 MBEDTLS_SSL_DEBUG_RET(
1217 1, "ssl_tls13_parse_key_shares_ext", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001218 return( ret );
Jerry Yu582dd062022-04-22 21:59:01 +08001219 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001220
1221 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
1222 break;
XiaokangQian88408882022-04-02 10:15:03 +00001223#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001224
1225 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
1227
1228 ret = ssl_tls13_parse_supported_versions_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001229 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001230 if( ret != 0 )
1231 {
1232 MBEDTLS_SSL_DEBUG_RET( 1,
1233 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
1234 return( ret );
1235 }
1236 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
1237 break;
1238
Jerry Yue19e3b92022-07-08 12:04:51 +00001239#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1240 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
1241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found psk key exchange modes extension" ) );
1242
1243 ret = ssl_tls13_parse_key_exchange_modes_ext(
1244 ssl, p, extension_data_end );
1245 if( ret != 0 )
1246 {
1247 MBEDTLS_SSL_DEBUG_RET(
1248 1, "ssl_tls13_parse_key_exchange_modes_ext", ret );
1249 return( ret );
1250 }
1251
1252 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES;
1253 break;
1254#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1255
Jerry Yu1c105562022-07-10 06:32:38 +00001256 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
Jerry Yu1c9247c2022-07-21 12:37:39 +08001258#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001259 /* Delay processing of the PSK identity once we have
1260 * found out which algorithms to use. We keep a pointer
1261 * to the buffer and the size for later processing.
1262 */
1263 pre_shared_key_ext_start = p;
1264 pre_shared_key_ext_end = extension_data_end;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001265#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001266 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1267 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001268
XiaokangQianacb39922022-06-17 10:18:48 +00001269#if defined(MBEDTLS_SSL_ALPN)
1270 case MBEDTLS_TLS_EXT_ALPN:
1271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1272
1273 ret = mbedtls_ssl_parse_alpn_ext( ssl, p, extension_data_end );
1274 if( ret != 0 )
1275 {
1276 MBEDTLS_SSL_DEBUG_RET(
1277 1, ( "mbedtls_ssl_parse_alpn_ext" ), ret );
1278 return( ret );
1279 }
1280 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_ALPN;
1281 break;
1282#endif /* MBEDTLS_SSL_ALPN */
1283
XiaokangQian7807f9f2022-02-15 10:04:37 +00001284#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1285 case MBEDTLS_TLS_EXT_SIG_ALG:
1286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1287
Gabor Mezei078e8032022-04-27 21:17:56 +02001288 ret = mbedtls_ssl_parse_sig_alg_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +08001289 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001290 if( ret != 0 )
1291 {
1292 MBEDTLS_SSL_DEBUG_MSG( 1,
1293 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
1294 ret ) );
1295 return( ret );
1296 }
1297 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
1298 break;
1299#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1300
1301 default:
1302 MBEDTLS_SSL_DEBUG_MSG( 3,
1303 ( "unknown extension found: %ud ( ignoring )",
1304 extension_type ) );
1305 }
1306
1307 p += extension_data_len;
1308 }
1309
Jerry Yu1c105562022-07-10 06:32:38 +00001310
1311#if defined(MBEDTLS_DEBUG_C)
1312 /* List all the extensions we have received */
1313 ssl_tls13_debug_print_client_hello_exts( ssl );
1314#endif /* MBEDTLS_DEBUG_C */
1315
Jerry Yu77f01482022-07-11 07:03:24 +00001316 ret = ssl_tls13_determine_key_exchange_mode( ssl );
1317 if( ret < 0 )
1318 return( ret );
Jerry Yu1c105562022-07-10 06:32:38 +00001319 mbedtls_ssl_add_hs_hdr_to_checksum( ssl,
1320 MBEDTLS_SSL_HS_CLIENT_HELLO,
1321 p - buf );
Jerry Yu032b15ce2022-07-11 06:10:03 +00001322
Jerry Yu1c105562022-07-10 06:32:38 +00001323#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001324 /* Update checksum with either
1325 * - The entire content of the CH message, if no PSK extension is present
1326 * - The content up to but excluding the PSK extension, if present.
1327 */
Jerry Yu1c105562022-07-10 06:32:38 +00001328 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Jerry Yu77f01482022-07-11 07:03:24 +00001329 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Jerry Yu1c105562022-07-10 06:32:38 +00001330 {
1331 ssl->handshake->update_checksum( ssl, buf,
1332 pre_shared_key_ext_start - buf );
Jerry Yubb852022022-07-20 21:10:44 +08001333 ret = ssl_tls13_parse_pre_shared_key_ext( ssl,
1334 pre_shared_key_ext_start,
1335 pre_shared_key_ext_end );
Jerry Yu1c105562022-07-10 06:32:38 +00001336 if( ret != 0 )
1337 {
Jerry Yubb852022022-07-20 21:10:44 +08001338 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_tls13_parse_pre_shared_key_ext" ),
Jerry Yu1c105562022-07-10 06:32:38 +00001339 ret );
1340 return( ret );
1341 }
Jerry Yu1c105562022-07-10 06:32:38 +00001342 }
1343 else
1344#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1345 {
1346 ssl->handshake->update_checksum( ssl, buf, p - buf );
1347 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001348
XiaokangQian75fe8c72022-06-15 09:42:45 +00001349 return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
1350}
1351
1352/* Update the handshake state machine */
1353
Ronald Cronce7d76e2022-07-08 18:56:49 +02001354MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian75fe8c72022-06-15 09:42:45 +00001355static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
1356{
1357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1358
XiaokangQian7807f9f2022-02-15 10:04:37 +00001359 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001360 * Server certificate selection
1361 */
1362 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
1363 {
1364 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
1365 return( ret );
1366 }
1367#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1368 ssl->handshake->sni_name = NULL;
1369 ssl->handshake->sni_name_len = 0;
1370#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001371
XiaokangQian7807f9f2022-02-15 10:04:37 +00001372 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
1373 if( ret != 0 )
1374 {
1375 MBEDTLS_SSL_DEBUG_RET( 1,
1376 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
1377 return( ret );
1378 }
1379
XiaokangQian7807f9f2022-02-15 10:04:37 +00001380 return( 0 );
1381
1382}
1383
1384/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001385 * Main entry point from the state machine; orchestrates the otherfunctions.
1386 */
1387
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001388MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianed582dd2022-04-13 08:21:05 +00001389static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
1390{
1391
XiaokangQian08037552022-04-20 07:16:41 +00001392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianed582dd2022-04-13 08:21:05 +00001393 unsigned char* buf = NULL;
1394 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001395 int parse_client_hello_ret;
1396
XiaokangQianed582dd2022-04-13 08:21:05 +00001397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1398
XiaokangQianed582dd2022-04-13 08:21:05 +00001399 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
1400 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1401 &buf, &buflen ) );
1402
1403 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
1404 buf + buflen ) );
Jerry Yuf41553b2022-05-09 22:20:30 +08001405 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
1406 * only SSL_CLIENT_HELLO_OK or
1407 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
1408 * stage as negative error codes are handled
1409 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001410
XiaokangQiancfd925f2022-04-14 07:10:37 +00001411 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
Jerry Yu582dd062022-04-22 21:59:01 +08001412
Jerry Yu4ca91402022-05-09 15:50:57 +08001413 if( parse_client_hello_ret == SSL_CLIENT_HELLO_OK )
1414 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1415 else
1416 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
XiaokangQianed582dd2022-04-13 08:21:05 +00001417
1418cleanup:
1419
1420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1421 return( ret );
1422}
1423
1424/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001425 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001426 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001427MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf4b27e42022-03-30 17:32:21 +08001428static int ssl_tls13_prepare_server_hello( mbedtls_ssl_context *ssl )
1429{
Jerry Yu637a3f12022-04-20 21:37:58 +08001430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1431 unsigned char *server_randbytes =
Jerry Yu3bf2c642022-03-30 22:02:12 +08001432 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001433 if( ssl->conf->f_rng == NULL )
1434 {
1435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
1436 return( MBEDTLS_ERR_SSL_NO_RNG );
1437 }
1438
Jerry Yu637a3f12022-04-20 21:37:58 +08001439 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001440 MBEDTLS_SERVER_HELLO_RANDOM_LEN ) ) != 0 )
1441 {
1442 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
1443 return( ret );
1444 }
1445
Jerry Yu637a3f12022-04-20 21:37:58 +08001446 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001447 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1448
1449#if defined(MBEDTLS_HAVE_TIME)
1450 ssl->session_negotiate->start = time( NULL );
1451#endif /* MBEDTLS_HAVE_TIME */
1452
1453 return( ret );
1454}
1455
Jerry Yu3bf2c642022-03-30 22:02:12 +08001456/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001457 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001458 *
1459 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001460 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001461 * } SupportedVersions;
1462 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001463MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001464static int ssl_tls13_write_server_hello_supported_versions_ext(
1465 mbedtls_ssl_context *ssl,
1466 unsigned char *buf,
1467 unsigned char *end,
1468 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +08001469{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001470 *out_len = 0;
1471
Jerry Yu955ddd72022-04-22 22:27:33 +08001472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, write selected version" ) );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001473
1474 /* Check if we have space to write the extension:
1475 * - extension_type (2 bytes)
1476 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001477 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001478 */
Jerry Yu349a6132022-04-14 20:52:56 +08001479 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001480
Jerry Yu349a6132022-04-14 20:52:56 +08001481 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001482
Jerry Yu349a6132022-04-14 20:52:56 +08001483 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001484
Jerry Yu349a6132022-04-14 20:52:56 +08001485 mbedtls_ssl_write_version( buf + 4,
1486 ssl->conf->transport,
1487 ssl->tls_version );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001488
1489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%04x]",
1490 ssl->tls_version ) );
1491
Jerry Yu349a6132022-04-14 20:52:56 +08001492 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001493
1494 return( 0 );
1495}
1496
Jerry Yud9436a12022-04-20 22:28:09 +08001497
Jerry Yu3bf2c642022-03-30 22:02:12 +08001498
1499/* Generate and export a single key share. For hybrid KEMs, this can
1500 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001501MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu955ddd72022-04-22 22:27:33 +08001502static int ssl_tls13_generate_and_write_key_share( mbedtls_ssl_context *ssl,
1503 uint16_t named_group,
1504 unsigned char *buf,
1505 unsigned char *end,
1506 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +08001507{
1508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001509
1510 *out_len = 0;
1511
Jerry Yu89e103c2022-03-30 22:43:29 +08001512#if defined(MBEDTLS_ECDH_C)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001513 if( mbedtls_ssl_tls13_named_group_is_ecdhe( named_group ) )
1514 {
Jerry Yu89e103c2022-03-30 22:43:29 +08001515 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1516 ssl, named_group, buf, end, out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001517 if( ret != 0 )
1518 {
Jerry Yu89e103c2022-03-30 22:43:29 +08001519 MBEDTLS_SSL_DEBUG_RET(
1520 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
1521 ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001522 return( ret );
1523 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001524 }
Jerry Yu89e103c2022-03-30 22:43:29 +08001525 else
1526#endif /* MBEDTLS_ECDH_C */
1527 if( 0 /* Other kinds of KEMs */ )
Jerry Yu3bf2c642022-03-30 22:02:12 +08001528 {
1529 }
1530 else
1531 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001532 ((void) ssl);
1533 ((void) named_group);
1534 ((void) buf);
1535 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001536 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1537 }
1538
1539 return( ret );
1540}
1541
1542/*
1543 * ssl_tls13_write_key_share_ext
1544 *
1545 * Structure of key_share extension in ServerHello:
1546 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001547 * struct {
1548 * NamedGroup group;
1549 * opaque key_exchange<1..2^16-1>;
1550 * } KeyShareEntry;
1551 * struct {
1552 * KeyShareEntry server_share;
1553 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001554 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001555MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3bf2c642022-03-30 22:02:12 +08001556static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
1557 unsigned char *buf,
1558 unsigned char *end,
1559 size_t *out_len )
1560{
Jerry Yu955ddd72022-04-22 22:27:33 +08001561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001562 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001563 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001564 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001565 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001566
1567 *out_len = 0;
1568
1569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding key share extension" ) );
1570
1571 /* Check if we have space for header and length fields:
1572 * - extension_type (2 bytes)
1573 * - extension_data_length (2 bytes)
1574 * - group (2 bytes)
1575 * - key_exchange_length (2 bytes)
1576 */
1577 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
Jerry Yu57d48412022-04-20 21:50:42 +08001578 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, p, 0 );
1579 MBEDTLS_PUT_UINT16_BE( group, server_share, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001580 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001581
Jerry Yu3bf2c642022-03-30 22:02:12 +08001582 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1583 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001584 ret = ssl_tls13_generate_and_write_key_share(
Jerry Yue65d8012022-04-23 10:34:35 +08001585 ssl, group, server_share + 4, end, &key_exchange_length );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001586 if( ret != 0 )
1587 return( ret );
1588 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001589
Jerry Yu955ddd72022-04-22 22:27:33 +08001590 MBEDTLS_PUT_UINT16_BE( key_exchange_length, server_share + 2, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001591
Jerry Yu57d48412022-04-20 21:50:42 +08001592 MBEDTLS_PUT_UINT16_BE( p - server_share, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001593
Jerry Yu57d48412022-04-20 21:50:42 +08001594 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001595
Jerry Yu3bf2c642022-03-30 22:02:12 +08001596 return( 0 );
1597}
Jerry Yud9436a12022-04-20 22:28:09 +08001598
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001599MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001600static int ssl_tls13_write_hrr_key_share_ext( mbedtls_ssl_context *ssl,
1601 unsigned char *buf,
1602 unsigned char *end,
1603 size_t *out_len )
1604{
1605 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1606 /* key_share Extension
1607 *
1608 * struct {
1609 * select (Handshake.msg_type) {
1610 * ...
1611 * case hello_retry_request:
1612 * NamedGroup selected_group;
1613 * ...
1614 * };
1615 * } KeyShare;
1616 */
1617
1618 *out_len = 0;
1619
Jerry Yub0ac10b2022-05-05 11:10:08 +08001620 /*
1621 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1622 * of the HRR is then to transmit a cookie to force the client to demonstrate
1623 * reachability at their apparent network address (primarily useful for DTLS).
1624 */
Ronald Cron79907712022-07-20 17:05:29 +02001625 if( ! mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral( ssl ) )
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001626 return( 0 );
1627
1628 /* We should only send the key_share extension if the client's initial
1629 * key share was not acceptable. */
1630 if( ssl->handshake->offered_group_id != 0 )
1631 {
1632 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Skip key_share extension in HRR" ) );
1633 return( 0 );
1634 }
1635
1636 if( selected_group == 0 )
1637 {
1638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching named group found" ) );
1639 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1640 }
1641
Jerry Yub0ac10b2022-05-05 11:10:08 +08001642 /* Check if we have enough space:
1643 * - extension_type (2 bytes)
1644 * - extension_data_length (2 bytes)
1645 * - selected_group (2 bytes)
1646 */
Ronald Cron154d1b62022-06-01 15:33:26 +02001647 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001648
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001649 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001650 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001651 MBEDTLS_PUT_UINT16_BE( selected_group, buf, 4 );
1652
1653 MBEDTLS_SSL_DEBUG_MSG( 3,
1654 ( "HRR selected_group: %s (%x)",
1655 mbedtls_ssl_named_group_to_str( selected_group ),
1656 selected_group ) );
1657
1658 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001659
Jerry Yub0ac10b2022-05-05 11:10:08 +08001660 return( 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001661}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001662
1663/*
1664 * Structure of ServerHello message:
1665 *
1666 * struct {
1667 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1668 * Random random;
1669 * opaque legacy_session_id_echo<0..32>;
1670 * CipherSuite cipher_suite;
1671 * uint8 legacy_compression_method = 0;
1672 * Extension extensions<6..2^16-1>;
1673 * } ServerHello;
1674 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001675MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3bf2c642022-03-30 22:02:12 +08001676static int ssl_tls13_write_server_hello_body( mbedtls_ssl_context *ssl,
Jerry Yu56404d72022-03-30 17:36:13 +08001677 unsigned char *buf,
1678 unsigned char *end,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001679 size_t *out_len,
1680 int is_hrr )
Jerry Yu56404d72022-03-30 17:36:13 +08001681{
Jerry Yu955ddd72022-04-22 22:27:33 +08001682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001683 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08001684 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08001685 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001686
1687 *out_len = 0;
1688
Jerry Yucfc04b32022-04-21 09:31:58 +08001689 /* ...
1690 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1691 * ...
1692 * with ProtocolVersion defined as:
1693 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001694 */
1695 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1696 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
1697 p += 2;
1698
Jerry Yu1c3e6882022-04-20 21:23:40 +08001699 /* ...
1700 * Random random;
1701 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08001702 * with Random defined as:
1703 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08001704 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001705 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001706 if( is_hrr )
1707 {
1708 memcpy( p, mbedtls_ssl_tls13_hello_retry_request_magic,
Jerry Yufbe3e642022-04-25 19:31:51 +08001709 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001710 }
1711 else
1712 {
1713 memcpy( p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
Jerry Yufbe3e642022-04-25 19:31:51 +08001714 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001715 }
Jerry Yu955ddd72022-04-22 22:27:33 +08001716 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yu3bf2c642022-03-30 22:02:12 +08001717 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1718 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1719
Jerry Yucfc04b32022-04-21 09:31:58 +08001720 /* ...
1721 * opaque legacy_session_id_echo<0..32>;
1722 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08001723 */
1724 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + ssl->session_negotiate->id_len );
1725 *p++ = (unsigned char)ssl->session_negotiate->id_len;
1726 if( ssl->session_negotiate->id_len > 0 )
1727 {
1728 memcpy( p, &ssl->session_negotiate->id[0],
1729 ssl->session_negotiate->id_len );
1730 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08001731
Jerry Yu3bf2c642022-03-30 22:02:12 +08001732 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
1733 ssl->session_negotiate->id_len );
1734 }
1735
Jerry Yucfc04b32022-04-21 09:31:58 +08001736 /* ...
1737 * CipherSuite cipher_suite;
1738 * ...
1739 * with CipherSuite defined as:
1740 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08001741 */
1742 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1743 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
1744 p += 2;
1745 MBEDTLS_SSL_DEBUG_MSG( 3,
1746 ( "server hello, chosen ciphersuite: %s ( id=%d )",
1747 mbedtls_ssl_get_ciphersuite_name(
1748 ssl->session_negotiate->ciphersuite ),
1749 ssl->session_negotiate->ciphersuite ) );
1750
Jerry Yucfc04b32022-04-21 09:31:58 +08001751 /* ...
1752 * uint8 legacy_compression_method = 0;
1753 * ...
1754 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001755 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1756 *p++ = 0x0;
1757
Jerry Yucfc04b32022-04-21 09:31:58 +08001758 /* ...
1759 * Extension extensions<6..2^16-1>;
1760 * ...
1761 * struct {
1762 * ExtensionType extension_type; (2 bytes)
1763 * opaque extension_data<0..2^16-1>;
1764 * } Extension;
1765 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001766 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yud9436a12022-04-20 22:28:09 +08001767 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001768 p += 2;
1769
Jerry Yue74e04a2022-04-21 09:23:16 +08001770 if( ( ret = ssl_tls13_write_server_hello_supported_versions_ext(
Jerry Yu3bf2c642022-03-30 22:02:12 +08001771 ssl, p, end, &output_len ) ) != 0 )
1772 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001773 MBEDTLS_SSL_DEBUG_RET(
1774 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001775 return( ret );
1776 }
1777 p += output_len;
1778
Jerry Yu3bf2c642022-03-30 22:02:12 +08001779 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1780 {
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001781 if( is_hrr )
1782 ret = ssl_tls13_write_hrr_key_share_ext( ssl, p, end, &output_len );
1783 else
1784 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001785 if( ret != 0 )
1786 return( ret );
1787 p += output_len;
1788 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001789
Jerry Yu032b15ce2022-07-11 06:10:03 +00001790#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yu96a2e362022-07-21 15:11:34 +08001791 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Jerry Yu032b15ce2022-07-11 06:10:03 +00001792 {
Jerry Yubb852022022-07-20 21:10:44 +08001793 ret = ssl_tls13_write_server_pre_shared_key_ext( ssl, p, end, &output_len );
Jerry Yu032b15ce2022-07-11 06:10:03 +00001794 if( ret != 0 )
1795 {
Jerry Yubb852022022-07-20 21:10:44 +08001796 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_server_pre_shared_key_ext",
Jerry Yu032b15ce2022-07-11 06:10:03 +00001797 ret );
1798 return( ret );
1799 }
1800 p += output_len;
1801 }
1802#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1803
Jerry Yud9436a12022-04-20 22:28:09 +08001804 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001805
Jerry Yud9436a12022-04-20 22:28:09 +08001806 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello extensions",
1807 p_extensions_len, p - p_extensions_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001808
Jerry Yud9436a12022-04-20 22:28:09 +08001809 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001810
Jerry Yud9436a12022-04-20 22:28:09 +08001811 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello", buf, *out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001812
1813 return( ret );
Jerry Yu56404d72022-03-30 17:36:13 +08001814}
1815
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001816MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue110d252022-05-05 10:19:22 +08001817static int ssl_tls13_finalize_write_server_hello( mbedtls_ssl_context *ssl )
1818{
1819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf86eb752022-05-06 11:16:55 +08001820 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001821 if( ret != 0 )
1822 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001823 MBEDTLS_SSL_DEBUG_RET( 1,
1824 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yue110d252022-05-05 10:19:22 +08001825 ret );
1826 return( ret );
1827 }
1828
Jerry Yue110d252022-05-05 10:19:22 +08001829 return( ret );
1830}
1831
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001832MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu5b64ae92022-03-30 17:15:02 +08001833static int ssl_tls13_write_server_hello( mbedtls_ssl_context *ssl )
1834{
Jerry Yu637a3f12022-04-20 21:37:58 +08001835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001836 unsigned char *buf;
1837 size_t buf_len, msg_len;
1838
1839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1840
Jerry Yuf4b27e42022-03-30 17:32:21 +08001841 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_server_hello( ssl ) );
1842
Jerry Yu3bf2c642022-03-30 22:02:12 +08001843 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001844 MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len ) );
1845
Jerry Yu3bf2c642022-03-30 22:02:12 +08001846 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1847 buf + buf_len,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001848 &msg_len,
1849 0 ) );
Jerry Yuf4b27e42022-03-30 17:32:21 +08001850
Jerry Yu3bf2c642022-03-30 22:02:12 +08001851 mbedtls_ssl_add_hs_msg_to_checksum(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001852 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1853
Jerry Yu3bf2c642022-03-30 22:02:12 +08001854 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001855 ssl, buf_len, msg_len ) );
Jerry Yu637a3f12022-04-20 21:37:58 +08001856
Jerry Yue110d252022-05-05 10:19:22 +08001857 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_server_hello( ssl ) );
1858
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001859#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1860 /* The server sends a dummy change_cipher_spec record immediately
1861 * after its first handshake message. This may either be after
1862 * a ServerHello or a HelloRetryRequest.
1863 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001864 mbedtls_ssl_handshake_set_state(
1865 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001866#else
Jerry Yu637a3f12022-04-20 21:37:58 +08001867 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001868#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08001869
Jerry Yuf4b27e42022-03-30 17:32:21 +08001870cleanup:
1871
1872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1873 return( ret );
Jerry Yu5b64ae92022-03-30 17:15:02 +08001874}
1875
Jerry Yu23d1a252022-05-12 18:08:59 +08001876
1877/*
1878 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
1879 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001880MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b840462022-06-01 17:05:53 +02001881static int ssl_tls13_prepare_hello_retry_request( mbedtls_ssl_context *ssl )
Jerry Yu23d1a252022-05-12 18:08:59 +08001882{
1883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1884 if( ssl->handshake->hello_retry_request_count > 0 )
1885 {
1886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Too many HRRs" ) );
1887 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1888 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1889 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1890 }
1891
1892 /*
1893 * Create stateless transcript hash for HRR
1894 */
1895 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
1896 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
1897 if( ret != 0 )
1898 {
1899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr", ret );
1900 return( ret );
1901 }
1902 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
1903
1904 return( 0 );
1905}
1906
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001907MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu23d1a252022-05-12 18:08:59 +08001908static int ssl_tls13_write_hello_retry_request( mbedtls_ssl_context *ssl )
1909{
1910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1911 unsigned char *buf;
1912 size_t buf_len, msg_len;
1913
1914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello retry request" ) );
1915
Ronald Cron7b840462022-06-01 17:05:53 +02001916 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_hello_retry_request( ssl ) );
Jerry Yu23d1a252022-05-12 18:08:59 +08001917
1918 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
1919 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1920 &buf, &buf_len ) );
1921
1922 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1923 buf + buf_len,
1924 &msg_len,
1925 1 ) );
1926 mbedtls_ssl_add_hs_msg_to_checksum(
1927 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1928
1929
1930 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, buf_len,
1931 msg_len ) );
1932
1933 ssl->handshake->hello_retry_request_count++;
1934
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001935#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1936 /* The server sends a dummy change_cipher_spec record immediately
1937 * after its first handshake message. This may either be after
1938 * a ServerHello or a HelloRetryRequest.
1939 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001940 mbedtls_ssl_handshake_set_state(
Gabor Mezeif7044ea2022-06-28 16:01:49 +02001941 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001942#else
Jerry Yu23d1a252022-05-12 18:08:59 +08001943 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001944#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08001945
1946cleanup:
1947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello retry request" ) );
1948 return( ret );
1949}
1950
Jerry Yu5b64ae92022-03-30 17:15:02 +08001951/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08001952 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
1953 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001954
1955/*
1956 * struct {
1957 * Extension extensions<0..2 ^ 16 - 1>;
1958 * } EncryptedExtensions;
1959 *
1960 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001961MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d3841a2022-04-16 12:37:19 +08001962static int ssl_tls13_write_encrypted_extensions_body( mbedtls_ssl_context *ssl,
1963 unsigned char *buf,
1964 unsigned char *end,
1965 size_t *out_len )
1966{
XiaokangQianacb39922022-06-17 10:18:48 +00001967 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001968 unsigned char *p = buf;
1969 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08001970 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001971 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08001972
Jerry Yu4d3841a2022-04-16 12:37:19 +08001973 *out_len = 0;
1974
1975 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yu8937eb42022-05-03 12:12:14 +08001976 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001977 p += 2;
1978
Jerry Yu8937eb42022-05-03 12:12:14 +08001979 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00001980 ((void) ret);
1981 ((void) output_len);
1982
1983#if defined(MBEDTLS_SSL_ALPN)
1984 ret = mbedtls_ssl_write_alpn_ext( ssl, p, end, &output_len );
1985 if( ret != 0 )
1986 return( ret );
XiaokangQian95d5f542022-06-24 02:29:26 +00001987 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001988#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001989
Jerry Yu8937eb42022-05-03 12:12:14 +08001990 extensions_len = ( p - p_extensions_len ) - 2;
1991 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
1992
1993 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001994
1995 MBEDTLS_SSL_DEBUG_BUF( 4, "encrypted extensions", buf, *out_len );
1996
1997 return( 0 );
1998}
1999
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002000MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d3841a2022-04-16 12:37:19 +08002001static int ssl_tls13_write_encrypted_extensions( mbedtls_ssl_context *ssl )
2002{
2003 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2004 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002005 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002006
Gabor Mezei54719122022-06-28 11:34:56 +02002007 mbedtls_ssl_set_outbound_transform( ssl,
2008 ssl->handshake->transform_handshake );
2009 MBEDTLS_SSL_DEBUG_MSG(
2010 3, ( "switching to handshake transform for outbound data" ) );
2011
Jerry Yuab452cc2022-04-28 15:27:08 +08002012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08002013
Jerry Yu4d3841a2022-04-16 12:37:19 +08002014 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
2015 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf, &buf_len ) );
2016
2017 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_encrypted_extensions_body(
2018 ssl, buf, buf + buf_len, &msg_len ) );
2019
2020 mbedtls_ssl_add_hs_msg_to_checksum(
2021 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len );
2022
Jerry Yu4d3841a2022-04-16 12:37:19 +08002023 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
2024 ssl, buf_len, msg_len ) );
2025
Jerry Yu8937eb42022-05-03 12:12:14 +08002026#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02002027 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Jerry Yu8937eb42022-05-03 12:12:14 +08002028 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2029 else
XiaokangQiana987e1d2022-05-07 01:25:58 +00002030 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yu8937eb42022-05-03 12:12:14 +08002031#else
2032 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2033#endif
2034
Jerry Yu4d3841a2022-04-16 12:37:19 +08002035cleanup:
2036
Jerry Yuab452cc2022-04-28 15:27:08 +08002037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08002038 return( ret );
2039}
2040
XiaokangQiancec9ae62022-05-06 07:28:50 +00002041#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002042#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2043#define SSL_CERTIFICATE_REQUEST_SKIP 1
2044/* Coordination:
2045 * Check whether a CertificateRequest message should be written.
2046 * Returns a negative code on failure, or
2047 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2048 * - SSL_CERTIFICATE_REQUEST_SKIP
2049 * indicating if the writing of the CertificateRequest
2050 * should be skipped or not.
2051 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002052MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiana987e1d2022-05-07 01:25:58 +00002053static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
2054{
2055 int authmode;
2056
XiaokangQian40a35232022-05-07 09:02:40 +00002057#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2058 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2059 authmode = ssl->handshake->sni_authmode;
2060 else
2061#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002062 authmode = ssl->conf->authmode;
2063
2064 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
2065 return( SSL_CERTIFICATE_REQUEST_SKIP );
2066
XiaokangQianc3017f62022-05-13 05:55:41 +00002067 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002068
XiaokangQiana987e1d2022-05-07 01:25:58 +00002069 return( SSL_CERTIFICATE_REQUEST_SEND_REQUEST );
2070}
2071
XiaokangQiancec9ae62022-05-06 07:28:50 +00002072/*
2073 * struct {
2074 * opaque certificate_request_context<0..2^8-1>;
2075 * Extension extensions<2..2^16-1>;
2076 * } CertificateRequest;
2077 *
2078 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002079MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiancec9ae62022-05-06 07:28:50 +00002080static int ssl_tls13_write_certificate_request_body( mbedtls_ssl_context *ssl,
2081 unsigned char *buf,
2082 const unsigned char *end,
2083 size_t *out_len )
2084{
2085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2086 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002087 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002088 unsigned char *p_extensions_len;
2089
2090 *out_len = 0;
2091
2092 /* Check if we have enough space:
2093 * - certificate_request_context (1 byte)
2094 * - extensions length (2 bytes)
2095 */
2096 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
2097
2098 /*
2099 * Write certificate_request_context
2100 */
2101 /*
2102 * We use a zero length context for the normal handshake
2103 * messages. For post-authentication handshake messages
2104 * this request context would be set to a non-zero value.
2105 */
2106 *p++ = 0x0;
2107
2108 /*
2109 * Write extensions
2110 */
2111 /* The extensions must contain the signature_algorithms. */
2112 p_extensions_len = p;
2113 p += 2;
XiaokangQianec6efb92022-05-06 09:53:10 +00002114 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
XiaokangQiancec9ae62022-05-06 07:28:50 +00002115 if( ret != 0 )
2116 return( ret );
2117
XiaokangQianec6efb92022-05-06 09:53:10 +00002118 p += output_len;
XiaokangQianaad9b0a2022-05-09 01:11:21 +00002119 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
XiaokangQiancec9ae62022-05-06 07:28:50 +00002120
2121 *out_len = p - buf;
2122
2123 return( 0 );
2124}
2125
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002126MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiancec9ae62022-05-06 07:28:50 +00002127static int ssl_tls13_write_certificate_request( mbedtls_ssl_context *ssl )
2128{
2129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2130
2131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2132
2133 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2134
2135 if( ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST )
2136 {
2137 unsigned char *buf;
2138 size_t buf_len, msg_len;
2139
2140 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
2141 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, &buf, &buf_len ) );
2142
2143 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_request_body(
2144 ssl, buf, buf + buf_len, &msg_len ) );
2145
2146 mbedtls_ssl_add_hs_msg_to_checksum(
2147 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len );
2148
2149 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
2150 ssl, buf_len, msg_len ) );
2151 }
2152 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
2153 {
2154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2155 ret = 0;
2156 }
2157 else
2158 {
2159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2160 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2161 goto cleanup;
2162 }
2163
2164 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2165cleanup:
2166
2167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2168 return( ret );
2169}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002170
Jerry Yu4d3841a2022-04-16 12:37:19 +08002171/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002172 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002173 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002174MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002175static int ssl_tls13_write_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08002176{
Jerry Yu5a26f302022-05-10 20:46:40 +08002177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002178
2179#if defined(MBEDTLS_X509_CRT_PARSE_C)
2180 if( ( ssl_tls13_pick_key_cert( ssl ) != 0 ) ||
2181 mbedtls_ssl_own_cert( ssl ) == NULL )
Jerry Yu5a26f302022-05-10 20:46:40 +08002182 {
Jerry Yub89125b2022-05-13 15:45:49 +08002183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate available." ) );
Jerry Yu5a26f302022-05-10 20:46:40 +08002184 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2185 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08002186 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu5a26f302022-05-10 20:46:40 +08002187 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002188#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002189
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08002190 ret = mbedtls_ssl_tls13_write_certificate( ssl );
2191 if( ret != 0 )
Jerry Yu1bff7112022-04-16 14:29:11 +08002192 return( ret );
Jerry Yu1bff7112022-04-16 14:29:11 +08002193 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2194 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08002195}
2196
2197/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002198 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002199 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002200MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002201static int ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08002202{
Jerry Yu4ff9e142022-04-16 14:57:49 +08002203 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08002204 if( ret != 0 )
Jerry Yu4ff9e142022-04-16 14:57:49 +08002205 return( ret );
Jerry Yu4ff9e142022-04-16 14:57:49 +08002206 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2207 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08002208}
Jerry Yu5a26f302022-05-10 20:46:40 +08002209#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002210
2211/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002212 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002213 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002214MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d8567f2022-04-17 10:57:57 +08002215static int ssl_tls13_write_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08002216{
Jerry Yud6e253d2022-05-18 13:59:24 +08002217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002218
2219 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2220 if( ret != 0 )
2221 return( ret );
2222
Jerry Yue3d67cb2022-05-19 15:33:10 +08002223 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2224 if( ret != 0 )
2225 {
2226 MBEDTLS_SSL_PEND_FATAL_ALERT(
2227 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2228 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2229 return( ret );
2230 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002231
Ronald Cron63dc4632022-05-31 14:41:53 +02002232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
2233 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
XiaokangQian189ded22022-05-10 08:12:17 +00002234
Ronald Cron63dc4632022-05-31 14:41:53 +02002235 if( ssl->handshake->certificate_request_sent )
2236 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
XiaokangQian189ded22022-05-10 08:12:17 +00002237 else
Ronald Cron19385882022-06-15 16:26:13 +02002238 {
2239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate" ) );
2240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
XiaokangQian189ded22022-05-10 08:12:17 +00002241 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002242 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002243
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002244 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08002245}
2246
2247/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002248 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002249 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002250MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d8567f2022-04-17 10:57:57 +08002251static int ssl_tls13_process_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08002252{
Jerry Yud6e253d2022-05-18 13:59:24 +08002253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2254
Jerry Yuff226982022-04-16 16:52:57 +08002255 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
2256 if( ret != 0 )
2257 return( ret );
2258
Jerry Yue3d67cb2022-05-19 15:33:10 +08002259 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
2260 if( ret != 0 )
2261 {
2262 MBEDTLS_SSL_DEBUG_RET( 1,
2263 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
2264 }
2265
Jerry Yu03ed50b2022-04-16 17:13:30 +08002266 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yuff226982022-04-16 16:52:57 +08002267 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08002268}
2269
2270/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002271 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002272 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002273MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4d8567f2022-04-17 10:57:57 +08002274static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08002275{
Jerry Yu03ed50b2022-04-16 17:13:30 +08002276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2277
Jerry Yu03ed50b2022-04-16 17:13:30 +08002278 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2279 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2280 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08002281}
2282
2283/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00002284 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00002285 */
Jerry Yu27561932021-08-27 17:07:38 +08002286int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +08002287{
XiaokangQian08037552022-04-20 07:16:41 +00002288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002289
2290 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
2291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2292
Jerry Yue3b34122021-09-28 17:53:35 +08002293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
2294 mbedtls_ssl_states_str( ssl->state ),
2295 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +08002296
XiaokangQian7807f9f2022-02-15 10:04:37 +00002297 switch( ssl->state )
2298 {
2299 /* start state */
2300 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +00002301 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
XiaokangQian08037552022-04-20 07:16:41 +00002302 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002303 break;
2304
XiaokangQian7807f9f2022-02-15 10:04:37 +00002305 case MBEDTLS_SSL_CLIENT_HELLO:
XiaokangQian4080a7f2022-04-11 09:55:18 +00002306 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +00002307 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +00002308 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
Jerry Yuf41553b2022-05-09 22:20:30 +08002309 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002310
Jerry Yuf41553b2022-05-09 22:20:30 +08002311 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
2312 ret = ssl_tls13_write_hello_retry_request( ssl );
2313 if( ret != 0 )
2314 {
2315 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_hello_retry_request", ret );
2316 return( ret );
2317 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002318 break;
2319
Jerry Yu5b64ae92022-03-30 17:15:02 +08002320 case MBEDTLS_SSL_SERVER_HELLO:
2321 ret = ssl_tls13_write_server_hello( ssl );
2322 break;
2323
Xiaofei Baicba64af2022-02-15 10:00:56 +00002324 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Jerry Yu4d3841a2022-04-16 12:37:19 +08002325 ret = ssl_tls13_write_encrypted_extensions( ssl );
Xiaofei Baicba64af2022-02-15 10:00:56 +00002326 if( ret != 0 )
2327 {
Jerry Yu4d3841a2022-04-16 12:37:19 +08002328 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_encrypted_extensions", ret );
2329 return( ret );
Xiaofei Baicba64af2022-02-15 10:00:56 +00002330 }
Jerry Yu48330562022-05-06 21:35:44 +08002331 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08002332
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002333#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
2334 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Xiaofei Bai5ee73d82022-03-14 02:48:30 +00002335 ret = ssl_tls13_write_certificate_request( ssl );
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002336 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002337
Jerry Yu83da34e2022-04-16 13:59:52 +08002338 case MBEDTLS_SSL_SERVER_CERTIFICATE:
2339 ret = ssl_tls13_write_server_certificate( ssl );
2340 break;
2341
2342 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
2343 ret = ssl_tls13_write_certificate_verify( ssl );
2344 break;
Jerry Yu5a26f302022-05-10 20:46:40 +08002345#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002346
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002347 /*
2348 * Injection of dummy-CCS's for middlebox compatibility
2349 */
2350#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02002351 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002352 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2353 if( ret == 0 )
2354 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2355 break;
2356
2357 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
2358 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2359 if( ret == 0 )
2360 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
2361 break;
2362#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2363
Jerry Yu69dd8d42022-04-16 12:51:26 +08002364 case MBEDTLS_SSL_SERVER_FINISHED:
2365 ret = ssl_tls13_write_server_finished( ssl );
2366 break;
2367
2368 case MBEDTLS_SSL_CLIENT_FINISHED:
2369 ret = ssl_tls13_process_client_finished( ssl );
2370 break;
2371
Jerry Yu69dd8d42022-04-16 12:51:26 +08002372 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
2373 ret = ssl_tls13_handshake_wrapup( ssl );
2374 break;
2375
XiaokangQian6b916b12022-04-25 07:29:34 +00002376 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2377 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Ronald Cron209cae92022-06-07 10:30:19 +02002378 if( ret == 0 )
XiaokangQian6b916b12022-04-25 07:29:34 +00002379 {
Ronald Cron209cae92022-06-07 10:30:19 +02002380 if( ssl->session_negotiate->peer_cert != NULL )
2381 {
2382 mbedtls_ssl_handshake_set_state(
2383 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2384 }
2385 else
Ronald Cron19385882022-06-15 16:26:13 +02002386 {
2387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
Ronald Cron209cae92022-06-07 10:30:19 +02002388 mbedtls_ssl_handshake_set_state(
2389 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002390 }
XiaokangQian6b916b12022-04-25 07:29:34 +00002391 }
2392 break;
2393
2394 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2395 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2396 if( ret == 0 )
2397 {
2398 mbedtls_ssl_handshake_set_state(
2399 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2400 }
2401 break;
2402
XiaokangQian7807f9f2022-02-15 10:04:37 +00002403 default:
2404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
XiaokangQian060d8672022-04-21 09:24:56 +00002405 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian7807f9f2022-02-15 10:04:37 +00002406 }
2407
2408 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +08002409}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002410
Jerry Yufb4b6472022-01-27 15:03:26 +08002411#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */