blob: b04bf0986dc811832826ab822612c1d634f9c83d [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
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 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Ronald Cron6f135e12021-12-08 16:57:54 +010024#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
26#if defined(MBEDTLS_SSL_CLI_C)
27
Jerry Yubc20bdd2021-08-24 15:59:48 +080028#include <string.h>
29
Jerry Yu56fc07f2021-09-01 17:48:49 +080030#include "mbedtls/debug.h"
31#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080032#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080033
Jerry Yubdc71882021-09-14 19:30:36 +080034#include "ssl_misc.h"
35#include "ecdh_misc.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080036#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010037#include "ssl_debug_helpers.h"
Jerry Yubdc71882021-09-14 19:30:36 +080038
Jerry Yubc20bdd2021-08-24 15:59:48 +080039/* Write extensions */
40
Jerry Yu92c6b402021-08-27 16:59:09 +080041/*
42 * ssl_tls13_write_supported_versions_ext():
43 *
44 * struct {
45 * ProtocolVersion versions<2..254>;
46 * } SupportedVersions;
47 */
Jerry Yuf4436812021-08-26 22:59:56 +080048static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080049 unsigned char *buf,
50 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000051 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080052{
53 unsigned char *p = buf;
54
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
63 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Jerry Yu92c6b402021-08-27 16:59:09 +080065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
66
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080067 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080068 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080069
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080070 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080071 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080072 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080074 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080075 *p++ = 0x2;
76
Jerry Yu0c63af62021-09-02 12:59:12 +080077 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080078 *
Jerry Yu0c63af62021-09-02 12:59:12 +080079 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080080 *
Jerry Yu0c63af62021-09-02 12:59:12 +080081 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080082 */
Jerry Yueecfbf02021-08-30 18:32:07 +080083 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
84 ssl->conf->max_minor_ver,
85 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080086
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080088 ssl->conf->max_major_ver,
89 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080090
Xiaofei Baid25fab62021-12-02 06:36:27 +000091 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080092
93 return( 0 );
94}
Jerry Yubc20bdd2021-08-24 15:59:48 +080095
Jerry Yuc068b662021-10-11 22:30:19 +080096static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
97 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080098 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080099{
Jerry Yue1b9c292021-09-10 10:08:31 +0800100 ((void) ssl);
101
Jerry Yub85277e2021-10-13 13:36:05 +0800102 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
103 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800104 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
105 {
106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800107
108 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
109 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800111 }
112
113 return( 0 );
114}
115
Jerry Yubc20bdd2021-08-24 15:59:48 +0800116#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
117
Jerry Yu6b64fe32021-09-01 17:05:13 +0800118/*
XiaokangQian647719a2021-12-07 09:16:29 +0000119 * Key Shares Extension
120 *
121 * enum {
122 * ... (0xFFFF)
123 * } NamedGroup;
124 *
125 * struct {
126 * NamedGroup group;
127 * opaque key_exchange<1..2^16-1>;
128 * } KeyShareEntry;
129 *
130 * struct {
131 * select(role) {
132 * case client:
133 * KeyShareEntry client_shares<0..2^16-1>;
134 * }
135 * } KeyShare;
136 */
137
XiaokangQian0b56a8f2021-12-22 02:39:32 +0000138#if defined(MBEDTLS_ECDH_C)
XiaokangQian16acd4b2022-01-14 07:35:47 +0000139static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000140{
141 uint16_t group_id = ssl->handshake->offered_group_id;
142 if( group_id == 0 )
143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
144
145 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000146 {
147 mbedtls_ecdh_free( &ssl->handshake->ecdh_ctx );
148 return( 0 );
149 }
XiaokangQian647719a2021-12-07 09:16:29 +0000150 else if( 0 /* other KEMs? */ )
151 {
152 /* Do something */
153 }
154
155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
156}
XiaokangQian0b56a8f2021-12-22 02:39:32 +0000157#else
XiaokangQian16acd4b2022-01-14 07:35:47 +0000158static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian0b56a8f2021-12-22 02:39:32 +0000159{
160 ((void) ssl);
161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
162}
163#endif /* MBEDTLS_ECDH_C */
XiaokangQian647719a2021-12-07 09:16:29 +0000164
165/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800166 * Functions for writing key_share extension.
167 */
168#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800169static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800170 mbedtls_ssl_context *ssl,
171 uint16_t named_group,
172 unsigned char *buf,
173 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000174 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800175{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800177 const mbedtls_ecp_curve_info *curve_info =
178 mbedtls_ecp_curve_info_from_tls_id( named_group );
179
180 if( curve_info == NULL )
181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
182
183 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
184
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800185 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
186 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800187 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800189 return( ret );
190 }
191
Xiaofei Baid25fab62021-12-02 06:36:27 +0000192 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
193 buf, end - buf,
194 ssl->conf->f_rng, ssl->conf->p_rng );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800195 if( ret != 0 )
196 {
197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
198 return( ret );
199 }
200
201 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
202 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800203 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800204}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800205#endif /* MBEDTLS_ECDH_C */
206
Jerry Yub60e3cf2021-09-08 16:41:02 +0800207static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
208 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800209{
210 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
211
Jerry Yu56fc07f2021-09-01 17:48:49 +0800212
Jerry Yu56fc07f2021-09-01 17:48:49 +0800213#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100214 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800215 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100216 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800217 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
218
Brett Warren14efd332021-10-06 09:32:11 +0100219 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000221 const mbedtls_ecp_curve_info *curve_info;
222 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
223 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100224 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800225 {
Brett Warren14efd332021-10-06 09:32:11 +0100226 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227 return( 0 );
228 }
229 }
230#else
231 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800232 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233#endif /* MBEDTLS_ECDH_C */
234
235 /*
236 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800237 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 */
239
240 return( ret );
241}
242
243/*
244 * ssl_tls13_write_key_share_ext
245 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800246 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 *
248 * struct {
249 * NamedGroup group;
250 * opaque key_exchange<1..2^16-1>;
251 * } KeyShareEntry;
252 * struct {
253 * KeyShareEntry client_shares<0..2^16-1>;
254 * } KeyShareClientHello;
255 */
256static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
257 unsigned char *buf,
258 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000259 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800260{
261 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000262 unsigned char *client_shares; /* Start of client_shares */
263 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
266
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268
Jerry Yub60e3cf2021-09-08 16:41:02 +0800269 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270 * - extension_type (2 bytes)
271 * - extension_data_length (2 bytes)
272 * - client_shares_length (2 bytes)
273 */
274 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
275 p += 6;
276
277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
278
279 /* HRR could already have requested something else. */
280 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800281 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
282 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800283 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800284 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 &group_id ) );
286 }
287
288 /*
289 * Dispatch to type-specific key generation function.
290 *
291 * So far, we're only supporting ECDHE. With the introduction
292 * of PQC KEMs, we'll want to have multiple branches, one per
293 * type of KEM, and dispatch to the corresponding crypto. And
294 * only one key share entry is allowed.
295 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000296 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800298 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800299 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800300 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000301 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800302 /* Length of key_exchange */
303 size_t key_exchange_len;
304
305 /* Check there is space for header of KeyShareEntry
306 * - group (2 bytes)
307 * - key_exchange_length (2 bytes)
308 */
309 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
310 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800311 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
312 p, end,
313 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800314 p += key_exchange_len;
315 if( ret != 0 )
316 return( ret );
317
318 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000319 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800320 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000321 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322 }
323 else
324#endif /* MBEDTLS_ECDH_C */
325 if( 0 /* other KEMs? */ )
326 {
327 /* Do something */
328 }
329 else
330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
331
Jerry Yub60e3cf2021-09-08 16:41:02 +0800332 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000333 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800334 if( client_shares_len == 0)
335 {
336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800338 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800339 /* Write extension_type */
340 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
341 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800342 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800343 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800344 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800345
346 /* Update offered_group_id field */
347 ssl->handshake->offered_group_id = group_id;
348
349 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000350 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800351
Xiaofei Baid25fab62021-12-02 06:36:27 +0000352 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800353
354 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
355
356cleanup:
357
358 return( ret );
359}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800360
Jerry Yue1b9c292021-09-10 10:08:31 +0800361#if defined(MBEDTLS_ECDH_C)
362
Jerry Yuc068b662021-10-11 22:30:19 +0800363static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800364{
365 const mbedtls_ecp_curve_info *curve_info;
366 mbedtls_ecp_group_id grp_id;
367#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
368 grp_id = ssl->handshake->ecdh_ctx.grp.id;
369#else
370 grp_id = ssl->handshake->ecdh_ctx.grp_id;
371#endif
372
373 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
374 if( curve_info == NULL )
375 {
376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
377 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
378 }
379
380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
381
Jerry Yue1b9c292021-09-10 10:08:31 +0800382 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800383 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800384
385 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
386 MBEDTLS_DEBUG_ECDH_QP );
387
388 return( 0 );
389}
390
Jerry Yuc068b662021-10-11 22:30:19 +0800391static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
392 const unsigned char *buf,
393 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800394{
395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
396
Jerry Yuc068b662021-10-11 22:30:19 +0800397 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800398 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800399 if( ret != 0 )
400 {
401 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800402
403 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
404 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
405 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800406 }
407
Jerry Yuc068b662021-10-11 22:30:19 +0800408 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800409 {
Jerry Yuc068b662021-10-11 22:30:19 +0800410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800411
Jerry Yu4a173382021-10-11 21:45:31 +0800412 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
413 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
414 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800415 }
416
417 return( 0 );
418}
Jerry Yu4a173382021-10-11 21:45:31 +0800419#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800420
XiaokangQiand9e068e2022-01-18 06:23:32 +0000421static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000422 const unsigned char *buf,
423 const unsigned char *end )
424{
XiaokangQianb851da82022-01-14 04:03:11 +0000425 const mbedtls_ecp_curve_info *curve_info = NULL;
426 const unsigned char *p = buf;
427 int tls_id;
428 int found = 0;
429
430 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
431 if( group_list == NULL )
432 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
433
434 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
435
436 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000437 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQianb851da82022-01-14 04:03:11 +0000438 tls_id = MBEDTLS_GET_UINT16_BE( p, 0 );
439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", tls_id ) );
440
441 /* Upon receipt of this extension in a HelloRetryRequest, the client
442 * MUST first verify that the selected_group field corresponds to a
443 * group which was provided in the "supported_groups" extension in the
444 * original ClientHello.
445 * The supported_group was based on the info in ssl->conf->group_list.
446 *
447 * If the server provided a key share that was not sent in the ClientHello
448 * then the client MUST abort the handshake with an "illegal_parameter" alert.
449 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000450 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000451 {
452 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
453 if( curve_info == NULL || curve_info->tls_id != tls_id )
454 continue;
455
456 /* We found a match */
457 found = 1;
458 break;
459 }
460
461 /* Client MUST verify that the selected_group field does not
462 * correspond to a group which was provided in the "key_share"
463 * extension in the original ClientHello. If the server sent an
464 * HRR message with a key share already provided in the
465 * ClientHello then the client MUST abort the handshake with
466 * an "illegal_parameter" alert.
467 */
468 if( found == 0 || tls_id == ssl->handshake->offered_group_id )
469 {
470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
471 MBEDTLS_SSL_PEND_FATAL_ALERT(
472 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
473 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
474 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
475 }
476
477 /* Remember server's preference for next ClientHello */
478 ssl->handshake->offered_group_id= tls_id;
479
480 return( 0 );
481}
482
Jerry Yue1b9c292021-09-10 10:08:31 +0800483/*
Jerry Yub85277e2021-10-13 13:36:05 +0800484 * ssl_tls13_parse_key_share_ext()
485 * Parse key_share extension in Server Hello
486 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800487 * struct {
488 * KeyShareEntry server_share;
489 * } KeyShareServerHello;
490 * struct {
491 * NamedGroup group;
492 * opaque key_exchange<1..2^16-1>;
493 * } KeyShareEntry;
494 */
Jerry Yu4a173382021-10-11 21:45:31 +0800495static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800496 const unsigned char *buf,
497 const unsigned char *end )
498{
Jerry Yub85277e2021-10-13 13:36:05 +0800499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800500 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800501 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800502
Jerry Yu4a173382021-10-11 21:45:31 +0800503 /* ...
504 * NamedGroup group; (2 bytes)
505 * ...
506 */
507 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
508 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800509 p += 2;
510
Jerry Yu4a173382021-10-11 21:45:31 +0800511 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800512 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800513 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800514 {
515 MBEDTLS_SSL_DEBUG_MSG( 1,
516 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800517 (unsigned) offered_group, (unsigned) group ) );
518 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
519 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
520 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800521 }
522
523#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800524 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800525 {
526 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800527 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800528 if( ret != 0 )
529 return( ret );
530 }
Jerry Yub85277e2021-10-13 13:36:05 +0800531 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800532#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800533 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800534 {
535 /* Do something */
536 }
537 else
538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
539
540 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
541 return( ret );
542}
543
Jerry Yubc20bdd2021-08-24 15:59:48 +0800544#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
545
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800546/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800547 * CipherSuite cipher_suites<2..2^16-2>;
548 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800549static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800550 mbedtls_ssl_context *ssl,
551 unsigned char *buf,
552 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000553 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800554{
Jerry Yufec982e2021-09-07 17:26:06 +0800555 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800556 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000557 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800558 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800559
Xiaofei Baid25fab62021-12-02 06:36:27 +0000560 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800561
562 /*
563 * Ciphersuite list
564 *
565 * This is a list of the symmetric cipher options supported by
566 * the client, specifically the record protection algorithm
567 * ( including secret key length ) and a hash to be used with
568 * HKDF, in descending order of client preference.
569 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800570 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800571
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800572 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800573 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
574 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800575
Jerry Yu0c63af62021-09-02 12:59:12 +0800576 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000577 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800578 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800579 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800580 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800581 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800582
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800583 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800584 if( ciphersuite_info == NULL )
585 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800586 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
587 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800588 continue;
589
590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800591 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800592 ciphersuite_info->name ) );
593
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800594 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800595 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
596 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
597 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800598 }
599
Jerry Yu0c63af62021-09-02 12:59:12 +0800600 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000601 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800602 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800603 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800604 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
605 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800606
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800607 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000608 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800609
Jerry Yu6a643102021-08-31 14:40:36 +0800610 return( 0 );
611}
612
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800613/*
614 * Structure of ClientHello message:
615 *
616 * struct {
617 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
618 * Random random;
619 * opaque legacy_session_id<0..32>;
620 * CipherSuite cipher_suites<2..2^16-2>;
621 * opaque legacy_compression_methods<1..2^8-1>;
622 * Extension extensions<8..2^16-1>;
623 * } ClientHello;
624 */
Jerry Yu08906d02021-08-31 11:05:27 +0800625static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800626 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800627 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000628 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800629{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800630
Jerry Yubc20bdd2021-08-24 15:59:48 +0800631 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000632 unsigned char *p_extensions_len; /* Pointer to extensions length */
633 size_t output_len; /* Length of buffer used by function */
634 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800635
Jerry Yubc20bdd2021-08-24 15:59:48 +0800636 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800637 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800638
Xiaofei Baid25fab62021-12-02 06:36:27 +0000639 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800640
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800641 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800642 ssl->major_ver = ssl->conf->min_major_ver;
643 ssl->minor_ver = ssl->conf->min_minor_ver;
644
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800645 /*
646 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800647 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800648 *
649 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800650 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800651 */
Jerry Yufec982e2021-09-07 17:26:06 +0800652 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800653 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800654 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800655
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800656 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800657 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
658 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800659 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800660 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
661 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800662
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800663 /*
664 * Write legacy_session_id
665 *
666 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
667 * which has been merged with pre-shared keys in this version. A client
668 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
669 * this field to that value. In compatibility mode, this field MUST be
670 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
671 * a new 32-byte value. This value need not be random but SHOULD be
672 * unpredictable to avoid implementations fixating on a specific value
673 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
674 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800675 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100676#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
677 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
678 *p++ = (unsigned char)ssl->session_negotiate->id_len;
679 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
680 p += ssl->session_negotiate->id_len;
681
682 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
683 ssl->session_negotiate->id_len );
684#else
Jerry Yubbe09522021-09-06 21:17:54 +0800685 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
686 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100687#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800688
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800689 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800690 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800691 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800692 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800693 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800694
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800695 /* Write legacy_compression_methods
696 *
697 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800698 * one byte set to zero, which corresponds to the 'null' compression
699 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800700 */
Jerry Yubbe09522021-09-06 21:17:54 +0800701 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
702 *p++ = 1;
703 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800704
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800705 /* Write extensions */
706
707 /* Keeping track of the included extensions */
708 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800709
710 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800711 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000712 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800713 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800714
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800715 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800716 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800717 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800718 */
Jerry Yubbe09522021-09-06 21:17:54 +0800719 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800720 if( ret != 0 )
721 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800722 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800723
724#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800725
Jerry Yub925f212022-01-12 11:17:02 +0800726 /*
727 * Add the extensions related to (EC)DHE ephemeral key establishment only if
728 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800729 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800730 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
731 {
732 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
733 if( ret != 0 )
734 return( ret );
735 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800736
Jerry Yuf46b0162022-01-11 16:28:00 +0800737 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
738 if( ret != 0 )
739 return( ret );
740 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800741
Jerry Yuf017ee42022-01-12 15:49:48 +0800742 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800743 if( ret != 0 )
744 return( ret );
745 p += output_len;
746 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800747#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
748
Xiaofei Bai15a56812021-11-05 10:52:12 +0000749#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000750 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000751 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
752 if( ret != 0 )
753 return( ret );
754 p += output_len;
755#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
756
Jerry Yubc20bdd2021-08-24 15:59:48 +0800757 /* Add more extensions here */
758
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800759 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000760 extensions_len = p - p_extensions_len - 2;
761 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800762 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800763 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000764 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800765
Xiaofei Baid25fab62021-12-02 06:36:27 +0000766 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800767 return( 0 );
768}
769
Jerry Yu335aca92021-09-12 20:18:56 +0800770static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800771{
Jerry Yu92c6b402021-08-27 16:59:09 +0800772 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
773 return( 0 );
774}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800775
Jerry Yu92c6b402021-08-27 16:59:09 +0800776static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
777{
778 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800779
Jerry Yu92c6b402021-08-27 16:59:09 +0800780 if( ssl->conf->f_rng == NULL )
781 {
782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
783 return( MBEDTLS_ERR_SSL_NO_RNG );
784 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800785
Jerry Yu92c6b402021-08-27 16:59:09 +0800786 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
787 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800788 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800789 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800790 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800791 return( ret );
792 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800793
Ronald Cron49ad6192021-11-24 16:25:31 +0100794#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
795 /*
796 * Create a session identifier for the purpose of middlebox compatibility
797 * only if one has not been created already.
798 */
799 if( ssl->session_negotiate->id_len == 0 )
800 {
801 /* Creating a session id with 32 byte length */
802 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
803 ssl->session_negotiate->id, 32 ) ) != 0 )
804 {
805 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
806 return( ret );
807 }
808 ssl->session_negotiate->id_len = 32;
809 }
810#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
811
Jerry Yu6f13f642021-08-26 17:18:15 +0800812 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800813}
814
Jerry Yu92c6b402021-08-27 16:59:09 +0800815/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800816 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800817 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800818 */
819static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800820{
Jerry Yu92c6b402021-08-27 16:59:09 +0800821 int ret = 0;
822 unsigned char *buf;
823 size_t buf_len, msg_len;
824
825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
826
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800827 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800828
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800829 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
830 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
831 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800832
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800833 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800834 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800835 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800836
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800837 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
838 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800839 msg_len );
840 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800841
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800842 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
843 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
844 buf_len,
845 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800846
847cleanup:
848
849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
850 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800851}
852
Jerry Yu687101b2021-09-14 16:03:56 +0800853/*
Jerry Yu4a173382021-10-11 21:45:31 +0800854 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800855 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800856/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800857 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
858 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000859 * to indicate which message is expected and to be parsed next.
860 */
Jerry Yub85277e2021-10-13 13:36:05 +0800861#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
862#define SSL_SERVER_HELLO_COORDINATE_HRR 1
863static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
864 const unsigned char *buf,
865 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800866{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800867 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800868 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
869 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
870 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
871 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
872
873 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
874 *
Jerry Yu4a173382021-10-11 21:45:31 +0800875 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800876 * special value of the SHA-256 of "HelloRetryRequest".
877 *
878 * struct {
879 * ProtocolVersion legacy_version = 0x0303;
880 * Random random;
881 * opaque legacy_session_id_echo<0..32>;
882 * CipherSuite cipher_suite;
883 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800884 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800885 * } ServerHello;
886 *
887 */
Jerry Yub85277e2021-10-13 13:36:05 +0800888 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800889
890 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800891 {
Jerry Yub85277e2021-10-13 13:36:05 +0800892 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800893 }
894
Jerry Yub85277e2021-10-13 13:36:05 +0800895 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800896}
897
Jerry Yu745bb612021-10-13 22:01:04 +0800898/* Fetch and preprocess
899 * Returns a negative value on failure, and otherwise
900 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
901 * - SSL_SERVER_HELLO_COORDINATE_HRR
902 */
Jerry Yub85277e2021-10-13 13:36:05 +0800903static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
904 unsigned char **buf,
905 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800906{
Jerry Yu4a173382021-10-11 21:45:31 +0800907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800908
909 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
910
Jerry Yue1b9c292021-09-10 10:08:31 +0800911 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
912 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
913 {
914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
915
916 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
917 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
918 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
919 }
920
921 *buf = ssl->in_msg + 4;
922 *buf_len = ssl->in_hslen - 4;
923
Jerry Yub85277e2021-10-13 13:36:05 +0800924 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
925 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800926 {
Jerry Yu745bb612021-10-13 22:01:04 +0800927 case SSL_SERVER_HELLO_COORDINATE_HELLO:
928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
929 break;
930 case SSL_SERVER_HELLO_COORDINATE_HRR:
931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000932 /* If a client receives a second
933 * HelloRetryRequest in the same connection (i.e., where the ClientHello
934 * was itself in response to a HelloRetryRequest), it MUST abort the
935 * handshake with an "unexpected_message" alert.
936 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000937 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000938 {
939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
940 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
941 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
942 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
943 }
944
XiaokangQiand9e068e2022-01-18 06:23:32 +0000945 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000946
Jerry Yu745bb612021-10-13 22:01:04 +0800947 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800948 }
949
950cleanup:
951
952 return( ret );
953}
954
Jerry Yu4a173382021-10-11 21:45:31 +0800955static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
956 const unsigned char **buf,
957 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800958{
959 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800960 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800961
Jerry Yude4fb2c2021-09-19 18:05:08 +0800962 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800963 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800964
Jerry Yu4a173382021-10-11 21:45:31 +0800965 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800966
967 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800968 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
969 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800970 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800971 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
972 ssl->session_negotiate->id,
973 ssl->session_negotiate->id_len );
974 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800975 legacy_session_id_echo_len );
976
977 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
978 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
979
Jerry Yue1b9c292021-09-10 10:08:31 +0800980 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
981 }
982
Jerry Yu4a173382021-10-11 21:45:31 +0800983 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800984 *buf = p;
985
986 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800987 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800988 return( 0 );
989}
990
Jerry Yu4a173382021-10-11 21:45:31 +0800991static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
992 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800993{
Jerry Yu4a173382021-10-11 21:45:31 +0800994 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
995
Jerry Yue1b9c292021-09-10 10:08:31 +0800996 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800997 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800998 {
Jerry Yu4a173382021-10-11 21:45:31 +0800999 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001000 {
1001 return( 1 );
1002 }
1003 }
1004 return( 0 );
1005}
1006
1007/* Parse ServerHello message and configure context
1008 *
1009 * struct {
1010 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1011 * Random random;
1012 * opaque legacy_session_id_echo<0..32>;
1013 * CipherSuite cipher_suite;
1014 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001015 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001016 * } ServerHello;
1017 */
Jerry Yuc068b662021-10-11 22:30:19 +08001018static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1019 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001020 const unsigned char *end,
1021 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001022{
Jerry Yub85277e2021-10-13 13:36:05 +08001023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001024 const unsigned char *p = buf;
Jerry Yub85277e2021-10-13 13:36:05 +08001025 size_t extensions_len;
1026 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001027 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001028 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001029 int supported_versions_ext_found = 0;
XiaokangQianb851da82022-01-14 04:03:11 +00001030#if defined(MBEDTLS_SSL_COOKIE_C)
1031 size_t cookie_len;
1032 unsigned char *cookie;
1033#endif /* MBEDTLS_SSL_COOKIE_C */
Jerry Yue1b9c292021-09-10 10:08:31 +08001034
1035 /*
1036 * Check there is space for minimal fields
1037 *
1038 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001039 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001040 * - legacy_session_id_echo ( 1 byte ), minimum size
1041 * - cipher_suite ( 2 bytes)
1042 * - legacy_compression_method ( 1 byte )
1043 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001044 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001045
1046 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001047 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1048
Jerry Yu4a173382021-10-11 21:45:31 +08001049 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001050 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001051 * ...
1052 * with ProtocolVersion defined as:
1053 * uint16 ProtocolVersion;
1054 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001055 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1056 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1057 {
1058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1059 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1060 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1061 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1062 }
1063 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001064
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001065 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001066 * Random random;
1067 * ...
1068 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001069 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001070 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001071 if( !is_hrr )
1072 {
1073 memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1074 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1075 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1076 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1077 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001078 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001079
Jerry Yu4a173382021-10-11 21:45:31 +08001080 /* ...
1081 * opaque legacy_session_id_echo<0..32>;
1082 * ...
1083 */
1084 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001085 {
1086 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1087 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1088 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1089 }
1090
Jerry Yu4a173382021-10-11 21:45:31 +08001091 /* ...
1092 * CipherSuite cipher_suite;
1093 * ...
1094 * with CipherSuite defined as:
1095 * uint8 CipherSuite[2];
1096 */
1097 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001098 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1099 p += 2;
1100
Jerry Yu4a173382021-10-11 21:45:31 +08001101
1102 /*
1103 * Check whether this ciphersuite is supported and offered.
1104 * Via the force_ciphersuite version we may have instructed the client
1105 * to use a different ciphersuite.
1106 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001107 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001108 if( ciphersuite_info == NULL ||
1109 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001110 {
Jerry Yub85277e2021-10-13 13:36:05 +08001111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
Jerry Yue1b9c292021-09-10 10:08:31 +08001112 cipher_suite ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001113
Jerry Yu4a173382021-10-11 21:45:31 +08001114 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1115 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1116 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +08001117 }
1118
XiaokangQian53f20b72022-01-18 10:47:33 +00001119 /*
1120 * Check whether this ciphersuite is the same with what we received in HRR.
1121 */
1122 if( ( !is_hrr ) && ( ssl->handshake->hello_retry_request_count > 0 ) &&
1123 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
1124 {
1125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not the one from HRR",
1126 cipher_suite ) );
1127
1128 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1129 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1130 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1131 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001132
Jerry Yu4a173382021-10-11 21:45:31 +08001133 /* Configure ciphersuites */
1134 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1135
1136 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001137 ssl->session_negotiate->ciphersuite = cipher_suite;
1138
Jerry Yue1b9c292021-09-10 10:08:31 +08001139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1140 cipher_suite, ciphersuite_info->name ) );
1141
1142#if defined(MBEDTLS_HAVE_TIME)
1143 ssl->session_negotiate->start = time( NULL );
1144#endif /* MBEDTLS_HAVE_TIME */
1145
Jerry Yu4a173382021-10-11 21:45:31 +08001146 /* ...
1147 * uint8 legacy_compression_method = 0;
1148 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001150 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 if( p[0] != 0 )
1152 {
Jerry Yub85277e2021-10-13 13:36:05 +08001153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001154 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1155 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1156 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1157 }
1158 p++;
1159
Jerry Yub85277e2021-10-13 13:36:05 +08001160 /* ...
1161 * Extension extensions<6..2^16-1>;
1162 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001163 * struct {
1164 * ExtensionType extension_type; (2 bytes)
1165 * opaque extension_data<0..2^16-1>;
1166 * } Extension;
1167 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001168 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001169 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001170 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001171
Jerry Yu4a173382021-10-11 21:45:31 +08001172 /* Check extensions do not go beyond the buffer of data. */
1173 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1174 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001175
Jerry Yu4a173382021-10-11 21:45:31 +08001176 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001177
Jerry Yu4a173382021-10-11 21:45:31 +08001178 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001179 {
1180 unsigned int extension_type;
1181 size_t extension_data_len;
1182
Jerry Yu4a173382021-10-11 21:45:31 +08001183 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001184 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1185 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1186 p += 4;
1187
Jerry Yu4a173382021-10-11 21:45:31 +08001188 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001189
1190 switch( extension_type )
1191 {
XiaokangQianb851da82022-01-14 04:03:11 +00001192#if defined(MBEDTLS_SSL_COOKIE_C)
1193 case MBEDTLS_TLS_EXT_COOKIE:
1194
XiaokangQiand9e068e2022-01-18 06:23:32 +00001195 if( !is_hrr )
1196 {
1197 MBEDTLS_SSL_PEND_FATAL_ALERT(
1198 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1199 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1200 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1201 }
1202
XiaokangQianb851da82022-01-14 04:03:11 +00001203 /* Retrieve length field of cookie */
1204 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 2 );
1205 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1206 cookie = (unsigned char *) ( p + 2 );
1207
1208 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, cookie_len + 2 );
1209 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", cookie, cookie_len );
1210
1211 mbedtls_free( ssl->handshake->verify_cookie );
1212 ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
1213 if( ssl->handshake->verify_cookie == NULL )
1214 {
1215 MBEDTLS_SSL_DEBUG_MSG( 1,
1216 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
1217 cookie_len ) );
1218 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1219 }
1220
1221 memcpy( ssl->handshake->verify_cookie, cookie, cookie_len );
1222 ssl->handshake->verify_cookie_len = (unsigned char) cookie_len;
1223 break;
1224#endif /* MBEDTLS_SSL_COOKIE_C */
1225
Jerry Yue1b9c292021-09-10 10:08:31 +08001226 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001227 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001228 MBEDTLS_SSL_DEBUG_MSG( 3,
1229 ( "found supported_versions extension" ) );
1230
Jerry Yuc068b662021-10-11 22:30:19 +08001231 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001232 p,
1233 p + extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001234 if( ret != 0 )
1235 return( ret );
1236 break;
1237
1238 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001241
1242 MBEDTLS_SSL_PEND_FATAL_ALERT(
1243 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1244 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1245 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001246
1247#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1248 case MBEDTLS_TLS_EXT_KEY_SHARE:
1249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQian53f20b72022-01-18 10:47:33 +00001250 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001251 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQianb851da82022-01-14 04:03:11 +00001252 p, p + extension_data_len );
XiaokangQian53f20b72022-01-18 10:47:33 +00001253 else
XiaokangQianb851da82022-01-14 04:03:11 +00001254 ret = ssl_tls13_parse_key_share_ext( ssl,
1255 p, p + extension_data_len );
1256 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001257 {
1258 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001259 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001260 ret );
1261 return( ret );
1262 }
1263 break;
1264#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1265
1266 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001267 MBEDTLS_SSL_DEBUG_MSG(
1268 3,
1269 ( "unknown extension found: %u ( ignoring )",
1270 extension_type ) );
1271
1272 MBEDTLS_SSL_PEND_FATAL_ALERT(
1273 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1274 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1275 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001276 }
1277
1278 p += extension_data_len;
1279 }
1280
XiaokangQian78b1fa72022-01-19 06:56:30 +00001281 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001282 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian53f20b72022-01-18 10:47:33 +00001284 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1285 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1286 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1287 }
1288
Jerry Yue1b9c292021-09-10 10:08:31 +08001289 return( 0 );
1290}
1291
Jerry Yuc068b662021-10-11 22:30:19 +08001292static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001293{
Jerry Yub85277e2021-10-13 13:36:05 +08001294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001295 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001296 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001297 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001298
Jerry Yub85277e2021-10-13 13:36:05 +08001299 /* Determine the key exchange mode:
1300 * 1) If both the pre_shared_key and key_share extensions were received
1301 * then the key exchange mode is PSK with EPHEMERAL.
1302 * 2) If only the pre_shared_key extension was received then the key
1303 * exchange mode is PSK-only.
1304 * 3) If only the key_share extension was received then the key
1305 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001306 */
Jerry Yub85277e2021-10-13 13:36:05 +08001307 switch( handshake->extensions_present &
1308 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001309 {
Jerry Yu745bb612021-10-13 22:01:04 +08001310 /* Only the pre_shared_key extension was received */
1311 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001312 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001313 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001314
Jerry Yu745bb612021-10-13 22:01:04 +08001315 /* Only the key_share extension was received */
1316 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001317 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001318 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001319
Jerry Yu745bb612021-10-13 22:01:04 +08001320 /* Both the pre_shared_key and key_share extensions were received */
1321 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001322 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001323 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001324
Jerry Yu745bb612021-10-13 22:01:04 +08001325 /* Neither pre_shared_key nor key_share extension was received */
1326 default:
1327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1328 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1329 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001330 }
1331
1332 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1333 *
1334 * TODO: We don't have to do this in case we offered 0-RTT and the
1335 * server accepted it. In this case, we could skip generating
1336 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001337 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001338 if( ret != 0 )
1339 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001341 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001342 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001343 }
1344
1345 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001346 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001347 if( ret != 0 )
1348 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001350 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001351 }
1352
1353 /* Next evolution in key schedule: Establish handshake secret and
1354 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001355 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001356 if( ret != 0 )
1357 {
Jerry Yuc068b662021-10-11 22:30:19 +08001358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1359 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001360 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001361 }
1362
Jerry Yub85277e2021-10-13 13:36:05 +08001363 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001364 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001365 {
1366 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1367 goto cleanup;
1368 }
Jerry Yu0b177842021-09-05 19:41:30 +08001369
1370 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1371 ssl->conf->endpoint,
1372 ssl->session_negotiate->ciphersuite,
1373 &traffic_keys,
1374 ssl );
1375 if( ret != 0 )
1376 {
1377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001378 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001379 }
1380
Jerry Yub85277e2021-10-13 13:36:05 +08001381 handshake->transform_handshake = transform_handshake;
1382 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001383
1384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1385 ssl->session_in = ssl->session_negotiate;
1386
1387 /*
1388 * State machine update
1389 */
1390 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1391
Jerry Yu4a173382021-10-11 21:45:31 +08001392cleanup:
1393
Jerry Yu0b177842021-09-05 19:41:30 +08001394 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001395 if( ret != 0 )
1396 {
Jerry Yub85277e2021-10-13 13:36:05 +08001397 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001398
Jerry Yu4a173382021-10-11 21:45:31 +08001399 MBEDTLS_SSL_PEND_FATAL_ALERT(
1400 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1401 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1402 }
1403 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001404}
1405
XiaokangQian16acd4b2022-01-14 07:35:47 +00001406static int ssl_tls13_finalize_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001407{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001408#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001410#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001411
XiaokangQian647719a2021-12-07 09:16:29 +00001412#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1413 /* If not offering early data, the client sends a dummy CCS record
1414 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001415 * its second ClientHello or before its encrypted handshake flight.
1416 */
XiaokangQian647719a2021-12-07 09:16:29 +00001417 mbedtls_ssl_handshake_set_state( ssl,
1418 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1419#else
1420 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1421#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1422
XiaokangQian78b1fa72022-01-19 06:56:30 +00001423 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001424
XiaokangQian78b1fa72022-01-19 06:56:30 +00001425 /*
1426 * We are going to re-generate a shared secret corresponding to the group selected by the server,
1427 * which is different from the group for which we generated a shared secret in the first client
1428 * hello. Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001429 */
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001430#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian16acd4b2022-01-14 07:35:47 +00001431 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001432 if( ret != 0 )
1433 return( ret );
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001434#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001435
1436 return( 0 );
1437}
1438
Jerry Yue1b9c292021-09-10 10:08:31 +08001439/*
Jerry Yu4a173382021-10-11 21:45:31 +08001440 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001441 * Handler for MBEDTLS_SSL_SERVER_HELLO
1442 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001443static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001444{
Jerry Yu4a173382021-10-11 21:45:31 +08001445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001446 unsigned char *buf = NULL;
1447 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001448 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001449
1450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1451
1452 /* Coordination step
1453 * - Fetch record
1454 * - Make sure it's either a ServerHello or a HRR.
1455 * - Switch processing routine in case of HRR
1456 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001457 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1458 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1459
XiaokangQian16acd4b2022-01-14 07:35:47 +00001460 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001461 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001462 goto cleanup;
1463 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001464 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001465 /* Parsing step
1466 * We know what message to expect by now and call
1467 * the respective parsing function.
1468 */
XiaokangQianb851da82022-01-14 04:03:11 +00001469 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001470 buf + buf_len,
1471 is_hrr ) );
1472 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001473 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1474
XiaokangQianb851da82022-01-14 04:03:11 +00001475 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1476 MBEDTLS_SSL_HS_SERVER_HELLO,
1477 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001478
XiaokangQiand9e068e2022-01-18 06:23:32 +00001479 if( is_hrr )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001480 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001481 else
1482 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001483
1484cleanup:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s:is_hrr = %d", __func__, is_hrr ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001486 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001487}
1488
1489/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001490 *
1491 * EncryptedExtensions message
1492 *
1493 * The EncryptedExtensions message contains any extensions which
1494 * should be protected, i.e., any which are not needed to establish
1495 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001496 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001497
1498/*
1499 * Overview
1500 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001501
1502/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001503static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001504
XiaokangQian97799ac2021-10-11 10:05:54 +00001505static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1506 const unsigned char *buf,
1507 const unsigned char *end );
1508static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001509
1510/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001511 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001512 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001513static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001514{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001515 int ret;
1516 unsigned char *buf;
1517 size_t buf_len;
1518
1519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1520
Xiaofei Bai746f9482021-11-12 08:53:56 +00001521 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001522 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001523 &buf, &buf_len ) );
1524
1525 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001526 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001527 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001528
Xiaofei Bai746f9482021-11-12 08:53:56 +00001529 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001530 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001531
XiaokangQian97799ac2021-10-11 10:05:54 +00001532 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001533
1534cleanup:
1535
1536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1537 return( ret );
1538
1539}
1540
XiaokangQian08da26c2021-10-09 10:12:11 +00001541/* Parse EncryptedExtensions message
1542 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001543 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001544 * } EncryptedExtensions;
1545 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001546static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1547 const unsigned char *buf,
1548 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001549{
1550 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001551 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001552 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001553 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001554
XiaokangQian08da26c2021-10-09 10:12:11 +00001555 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001556 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001557 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001558
XiaokangQian97799ac2021-10-11 10:05:54 +00001559 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001560 extensions_end = p + extensions_len;
1561 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001562
XiaokangQian8db25ff2021-10-13 05:56:18 +00001563 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001564 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001565 unsigned int extension_type;
1566 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001567
XiaokangQian08da26c2021-10-09 10:12:11 +00001568 /*
1569 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001570 * ExtensionType extension_type; (2 bytes)
1571 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001572 * } Extension;
1573 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001574 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001575 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1576 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1577 p += 4;
1578
XiaokangQian8db25ff2021-10-13 05:56:18 +00001579 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001580
XiaokangQian97799ac2021-10-11 10:05:54 +00001581 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001582 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001583 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001584 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001585 switch( extension_type )
1586 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001587
XiaokangQian08da26c2021-10-09 10:12:11 +00001588 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1590 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001591
XiaokangQian08da26c2021-10-09 10:12:11 +00001592 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001593 MBEDTLS_SSL_DEBUG_MSG(
1594 3, ( "unsupported extension found: %u ", extension_type) );
1595 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001596 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001597 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1598 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001599 }
1600
XiaokangQian08da26c2021-10-09 10:12:11 +00001601 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001602 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001603
XiaokangQian97799ac2021-10-11 10:05:54 +00001604 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001605 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001606 {
1607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001608 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001609 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001610 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001611 }
1612
1613 return( ret );
1614}
1615
XiaokangQian97799ac2021-10-11 10:05:54 +00001616static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001617{
Jerry Yua93ac112021-10-27 16:31:48 +08001618#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001619 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001620 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1621 else
Jerry Yud2674312021-10-29 10:08:19 +08001622 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001623#else
1624 ((void) ssl);
1625 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1626#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001627 return( 0 );
1628}
1629
Jerry Yua93ac112021-10-27 16:31:48 +08001630#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001631/*
Jerry Yud2674312021-10-29 10:08:19 +08001632 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1633 */
1634static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1635{
1636 int ret = mbedtls_ssl_read_record( ssl, 0 );
1637
1638 if( ret != 0 )
1639 {
1640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1641 return( ret );
1642 }
1643
1644 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1645 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1646 {
1647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1648 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1649 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1650 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1651 }
1652
1653 ssl->keep_current_message = 1;
1654 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1655
1656 return( 0 );
1657}
1658
1659/*
Jerry Yu687101b2021-09-14 16:03:56 +08001660 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1661 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001662static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001663{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001664 int ret;
1665
1666 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001667 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001668 return( ret );
1669
Jerry Yu687101b2021-09-14 16:03:56 +08001670 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1671 return( 0 );
1672}
1673
1674/*
1675 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1676 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001677static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001678{
Jerry Yu30b071c2021-09-12 20:16:03 +08001679 int ret;
1680
1681 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1682 if( ret != 0 )
1683 return( ret );
1684
Jerry Yu687101b2021-09-14 16:03:56 +08001685 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1686 return( 0 );
1687}
Jerry Yua93ac112021-10-27 16:31:48 +08001688#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001689
Jerry Yu687101b2021-09-14 16:03:56 +08001690/*
1691 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1692 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001693static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001694{
XiaokangQianac0385c2021-11-03 06:40:11 +00001695 int ret;
1696
XiaokangQianc5c39d52021-11-09 11:55:10 +00001697 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001698 if( ret != 0 )
1699 return( ret );
1700
Ronald Cron49ad6192021-11-24 16:25:31 +01001701#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1702 mbedtls_ssl_handshake_set_state(
1703 ssl,
1704 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1705#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001706 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001707#endif
1708
XiaokangQianac0385c2021-11-03 06:40:11 +00001709 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001710}
1711
1712/*
Ronald Crond4c64022021-12-06 09:06:46 +01001713 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1714 */
1715#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1716static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1717{
1718 int ret;
1719
1720 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1721 if( ret != 0 )
1722 return( ret );
1723
Ronald Crond4c64022021-12-06 09:06:46 +01001724 return( 0 );
1725}
1726#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1727
1728/*
Jerry Yu687101b2021-09-14 16:03:56 +08001729 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1730 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001731static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001732{
XiaokangQian0fa66432021-11-15 03:33:57 +00001733 int ret;
1734
Ronald Cron49ad6192021-11-24 16:25:31 +01001735 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1736
XiaokangQian0fa66432021-11-15 03:33:57 +00001737 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1738 if( ret != 0 )
1739 return( ret );
1740
1741 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1742 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001743}
1744
1745/*
1746 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1747 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001748static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001749{
Jerry Yu378254d2021-10-30 21:44:47 +08001750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001751 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001752 return( 0 );
1753}
1754
1755/*
1756 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1757 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001758static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001759{
Jerry Yu378254d2021-10-30 21:44:47 +08001760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1761 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1762
1763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1764 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1765
1766 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1767
1768 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1769 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001770}
1771
Jerry Yu92c6b402021-08-27 16:59:09 +08001772int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001773{
Jerry Yu92c6b402021-08-27 16:59:09 +08001774 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001775
Jerry Yue3b34122021-09-28 17:53:35 +08001776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1777 mbedtls_ssl_states_str( ssl->state ),
1778 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001779
1780 switch( ssl->state )
1781 {
1782 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001783 * ssl->state is initialized as HELLO_REQUEST. It is the same
1784 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001785 */
1786 case MBEDTLS_SSL_HELLO_REQUEST:
1787 case MBEDTLS_SSL_CLIENT_HELLO:
1788 ret = ssl_tls13_write_client_hello( ssl );
1789 break;
1790
1791 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001792 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001793 break;
1794
1795 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001796 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001797 break;
1798
Jerry Yua93ac112021-10-27 16:31:48 +08001799#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001800 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1801 ret = ssl_tls13_process_certificate_request( ssl );
1802 break;
1803
Jerry Yu687101b2021-09-14 16:03:56 +08001804 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001805 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001806 break;
1807
1808 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001809 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001810 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001811#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001812
1813 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001814 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001815 break;
1816
Jerry Yu687101b2021-09-14 16:03:56 +08001817 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001818 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001819 break;
1820
1821 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001822 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001823 break;
1824
1825 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001826 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001827 break;
1828
Ronald Cron49ad6192021-11-24 16:25:31 +01001829 /*
1830 * Injection of dummy-CCS's for middlebox compatibility
1831 */
1832#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1833 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001834 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01001835 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001836 break;
1837#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1838
Jerry Yu92c6b402021-08-27 16:59:09 +08001839 default:
1840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1841 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1842 }
1843
1844 return( ret );
1845}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001846
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001847#endif /* MBEDTLS_SSL_CLI_C */
1848
Ronald Cron6f135e12021-12-08 16:57:54 +01001849#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */