blob: 5968501e644612a81515f037d113371f2fa4d8c8 [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
24#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
25
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"
Jerry Yue78ee992021-09-22 15:42:14 +080037#include "ssl_debug_helpers_generated.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/*
119 * Functions for writing supported_groups extension.
120 *
121 * Stucture of supported_groups:
122 * enum {
123 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
124 * x25519(0x001D), x448(0x001E),
125 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
126 * ffdhe6144(0x0103), ffdhe8192(0x0104),
127 * ffdhe_private_use(0x01FC..0x01FF),
128 * ecdhe_private_use(0xFE00..0xFEFF),
129 * (0xFFFF)
130 * } NamedGroup;
131 * struct {
132 * NamedGroup named_group_list<2..2^16-1>;
133 * } NamedGroupList;
134 */
Jerry Yu6b64fe32021-09-01 17:05:13 +0800135#if defined(MBEDTLS_ECDH_C)
136/*
137 * In versions of TLS prior to TLS 1.3, this extension was named
138 * 'elliptic_curves' and only contained elliptic curve groups.
139 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800140static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl,
Brett Warren14efd332021-10-06 09:32:11 +0100141 unsigned char *buf,
142 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000143 size_t *out_len )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800144{
145 unsigned char *p = buf;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800146
Xiaofei Baid25fab62021-12-02 06:36:27 +0000147 *out_len = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800148
Brett Warren14efd332021-10-06 09:32:11 +0100149 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
150
151 if( group_list == NULL )
Jerry Yu7c522d42021-09-08 17:55:09 +0800152 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
153
Brett Warren14efd332021-10-06 09:32:11 +0100154 for ( ; *group_list != 0; group_list++ )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800155 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000156 const mbedtls_ecp_curve_info *curve_info;
157 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
158 if( curve_info == NULL )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800159 continue;
Jerry Yu7c522d42021-09-08 17:55:09 +0800160
Brett Warren14efd332021-10-06 09:32:11 +0100161 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800162 continue;
163
164 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2);
Brett Warren14efd332021-10-06 09:32:11 +0100165 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800166 p += 2;
167
168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
Xiaofei Baieef15042021-11-18 07:29:56 +0000169 curve_info->name, *group_list ) );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800170 }
171
Xiaofei Baid25fab62021-12-02 06:36:27 +0000172 *out_len = p - buf;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800173
174 return( 0 );
175}
176#else
Jerry Yub60e3cf2021-09-08 16:41:02 +0800177static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl,
178 unsigned char *buf,
179 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000180 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800181{
182 ((void) ssl);
183 ((void) buf);
184 ((void) end);
Xiaofei Baid25fab62021-12-02 06:36:27 +0000185 *out_len = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800186 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
187}
188#endif /* MBEDTLS_ECDH_C */
189
Jerry Yub60e3cf2021-09-08 16:41:02 +0800190static int ssl_tls13_write_named_group_list_dhe( mbedtls_ssl_context *ssl,
191 unsigned char *buf,
192 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000193 size_t *out_len )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800194{
195 ((void) ssl);
196 ((void) buf);
197 ((void) end);
Xiaofei Baid25fab62021-12-02 06:36:27 +0000198 *out_len = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "write_named_group_dhe is not implemented" ) );
200 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
201}
202
Jerry Yu6b64fe32021-09-01 17:05:13 +0800203static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
204 unsigned char *buf,
205 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000206 size_t *out_len )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800207{
208 unsigned char *p = buf ;
Xiaofei Baieef15042021-11-18 07:29:56 +0000209 unsigned char *named_group_list; /* Start of named_group_list */
210 size_t named_group_list_len; /* Length of named_group_list */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800211 size_t output_len = 0;
212 int ret_ecdhe, ret_dhe;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800213
Xiaofei Baid25fab62021-12-02 06:36:27 +0000214 *out_len = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800215
216 if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
217 return( 0 );
218
219 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
220
Jerry Yub60e3cf2021-09-08 16:41:02 +0800221 /* Check if we have space for header and length fields:
Xiaofei Baieef15042021-11-18 07:29:56 +0000222 * - extension_type (2 bytes)
223 * - extension_data_length (2 bytes)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800224 * - named_group_list_length (2 bytes)
225 */
Jerry Yu6b64fe32021-09-01 17:05:13 +0800226 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
227 p += 6;
228
Xiaofei Baieef15042021-11-18 07:29:56 +0000229 named_group_list = p;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800230 ret_ecdhe = ssl_tls13_write_named_group_list_ecdhe( ssl, p, end, &output_len );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800231 if( ret_ecdhe != 0 )
232 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800233 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_ecdhe", ret_ecdhe );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800234 }
Jerry Yub60e3cf2021-09-08 16:41:02 +0800235 p += output_len;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800236
Jerry Yub60e3cf2021-09-08 16:41:02 +0800237 ret_dhe = ssl_tls13_write_named_group_list_dhe( ssl, p, end, &output_len );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800238 if( ret_dhe != 0 )
239 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800240 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_dhe", ret_dhe );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800241 }
Jerry Yub60e3cf2021-09-08 16:41:02 +0800242 p += output_len;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800243
Jerry Yu388bd0d2021-09-15 18:41:02 +0800244 /* Both ECDHE and DHE failed. */
Jerry Yu6b64fe32021-09-01 17:05:13 +0800245 if( ret_ecdhe != 0 && ret_dhe != 0 )
246 {
247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Both ECDHE and DHE groups are fail. " ) );
248 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
249 }
250
251 /* Length of named_group_list*/
Xiaofei Baieef15042021-11-18 07:29:56 +0000252 named_group_list_len = p - named_group_list;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800253 if( named_group_list_len == 0 )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800254 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
257 }
258
259 /* Write extension_type */
260 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
261 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800262 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800263 /* Write length of named_group_list */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800264 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800265
Jerry Yub60e3cf2021-09-08 16:41:02 +0800266 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", buf + 4, named_group_list_len + 2 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800267
Xiaofei Baid25fab62021-12-02 06:36:27 +0000268 *out_len = p - buf;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800269
270 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
271
Jerry Yub60e3cf2021-09-08 16:41:02 +0800272 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800273}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800274
Jerry Yu56fc07f2021-09-01 17:48:49 +0800275/*
276 * Functions for writing key_share extension.
277 */
278#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800279static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800280 mbedtls_ssl_context *ssl,
281 uint16_t named_group,
282 unsigned char *buf,
283 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000284 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800285{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800287 const mbedtls_ecp_curve_info *curve_info =
288 mbedtls_ecp_curve_info_from_tls_id( named_group );
289
290 if( curve_info == NULL )
291 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
292
293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
294
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800295 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
296 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800299 return( ret );
300 }
301
Xiaofei Baid25fab62021-12-02 06:36:27 +0000302 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
303 buf, end - buf,
304 ssl->conf->f_rng, ssl->conf->p_rng );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800305 if( ret != 0 )
306 {
307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
308 return( ret );
309 }
310
311 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
312 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800313 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800314}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800315#endif /* MBEDTLS_ECDH_C */
316
Jerry Yub60e3cf2021-09-08 16:41:02 +0800317static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
318 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319{
320 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
321
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322
Jerry Yu56fc07f2021-09-01 17:48:49 +0800323#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100324 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800325 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100326 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800327 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
328
Brett Warren14efd332021-10-06 09:32:11 +0100329 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000331 const mbedtls_ecp_curve_info *curve_info;
332 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
333 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100334 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800335 {
Brett Warren14efd332021-10-06 09:32:11 +0100336 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800337 return( 0 );
338 }
339 }
340#else
341 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800342 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800343#endif /* MBEDTLS_ECDH_C */
344
345 /*
346 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800347 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800348 */
349
350 return( ret );
351}
352
353/*
354 * ssl_tls13_write_key_share_ext
355 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800356 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800357 *
358 * struct {
359 * NamedGroup group;
360 * opaque key_exchange<1..2^16-1>;
361 * } KeyShareEntry;
362 * struct {
363 * KeyShareEntry client_shares<0..2^16-1>;
364 * } KeyShareClientHello;
365 */
366static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
367 unsigned char *buf,
368 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000369 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800370{
371 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000372 unsigned char *client_shares; /* Start of client_shares */
373 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800374 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800375 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
376
Xiaofei Baid25fab62021-12-02 06:36:27 +0000377 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800378
379 if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
380 return( 0 );
381
Jerry Yub60e3cf2021-09-08 16:41:02 +0800382 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800383 * - extension_type (2 bytes)
384 * - extension_data_length (2 bytes)
385 * - client_shares_length (2 bytes)
386 */
387 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
388 p += 6;
389
390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
391
392 /* HRR could already have requested something else. */
393 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800394 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
395 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800396 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800397 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800398 &group_id ) );
399 }
400
401 /*
402 * Dispatch to type-specific key generation function.
403 *
404 * So far, we're only supporting ECDHE. With the introduction
405 * of PQC KEMs, we'll want to have multiple branches, one per
406 * type of KEM, and dispatch to the corresponding crypto. And
407 * only one key share entry is allowed.
408 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000409 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800410#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800411 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800413 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000414 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800415 /* Length of key_exchange */
416 size_t key_exchange_len;
417
418 /* Check there is space for header of KeyShareEntry
419 * - group (2 bytes)
420 * - key_exchange_length (2 bytes)
421 */
422 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
423 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800424 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
425 p, end,
426 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800427 p += key_exchange_len;
428 if( ret != 0 )
429 return( ret );
430
431 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000432 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800433 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000434 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800435 }
436 else
437#endif /* MBEDTLS_ECDH_C */
438 if( 0 /* other KEMs? */ )
439 {
440 /* Do something */
441 }
442 else
443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
444
Jerry Yub60e3cf2021-09-08 16:41:02 +0800445 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000446 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800447 if( client_shares_len == 0)
448 {
449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800451 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800452 /* Write extension_type */
453 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
454 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800455 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800456 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800457 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800458
459 /* Update offered_group_id field */
460 ssl->handshake->offered_group_id = group_id;
461
462 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000463 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800464
Xiaofei Baid25fab62021-12-02 06:36:27 +0000465 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800466
467 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
468
469cleanup:
470
471 return( ret );
472}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800473
Jerry Yue1b9c292021-09-10 10:08:31 +0800474#if defined(MBEDTLS_ECDH_C)
475
Jerry Yuc068b662021-10-11 22:30:19 +0800476static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800477{
478 const mbedtls_ecp_curve_info *curve_info;
479 mbedtls_ecp_group_id grp_id;
480#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
481 grp_id = ssl->handshake->ecdh_ctx.grp.id;
482#else
483 grp_id = ssl->handshake->ecdh_ctx.grp_id;
484#endif
485
486 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
487 if( curve_info == NULL )
488 {
489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
490 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
491 }
492
493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
494
Jerry Yue1b9c292021-09-10 10:08:31 +0800495 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800496 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800497
498 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
499 MBEDTLS_DEBUG_ECDH_QP );
500
501 return( 0 );
502}
503
Jerry Yuc068b662021-10-11 22:30:19 +0800504static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
505 const unsigned char *buf,
506 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800507{
508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
509
Jerry Yuc068b662021-10-11 22:30:19 +0800510 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800511 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800512 if( ret != 0 )
513 {
514 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800515
516 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
517 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
518 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800519 }
520
Jerry Yuc068b662021-10-11 22:30:19 +0800521 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800522 {
Jerry Yuc068b662021-10-11 22:30:19 +0800523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800524
Jerry Yu4a173382021-10-11 21:45:31 +0800525 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
526 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
527 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800528 }
529
530 return( 0 );
531}
Jerry Yu4a173382021-10-11 21:45:31 +0800532#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800533
534/*
Jerry Yub85277e2021-10-13 13:36:05 +0800535 * ssl_tls13_parse_key_share_ext()
536 * Parse key_share extension in Server Hello
537 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800538 * struct {
539 * KeyShareEntry server_share;
540 * } KeyShareServerHello;
541 * struct {
542 * NamedGroup group;
543 * opaque key_exchange<1..2^16-1>;
544 * } KeyShareEntry;
545 */
Jerry Yu4a173382021-10-11 21:45:31 +0800546static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800547 const unsigned char *buf,
548 const unsigned char *end )
549{
Jerry Yub85277e2021-10-13 13:36:05 +0800550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800551 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800552 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800553
Jerry Yu4a173382021-10-11 21:45:31 +0800554 /* ...
555 * NamedGroup group; (2 bytes)
556 * ...
557 */
558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
559 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800560 p += 2;
561
Jerry Yu4a173382021-10-11 21:45:31 +0800562 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800563 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800564 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800565 {
566 MBEDTLS_SSL_DEBUG_MSG( 1,
567 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800568 (unsigned) offered_group, (unsigned) group ) );
569 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
570 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
571 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800572 }
573
574#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800575 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800576 {
577 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800578 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800579 if( ret != 0 )
580 return( ret );
581 }
Jerry Yub85277e2021-10-13 13:36:05 +0800582 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800583#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800584 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800585 {
586 /* Do something */
587 }
588 else
589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
590
591 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
592 return( ret );
593}
594
Jerry Yubc20bdd2021-08-24 15:59:48 +0800595#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
596
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800597/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800598 * CipherSuite cipher_suites<2..2^16-2>;
599 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800600static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800601 mbedtls_ssl_context *ssl,
602 unsigned char *buf,
603 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000604 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800605{
Jerry Yufec982e2021-09-07 17:26:06 +0800606 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800607 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000608 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800609 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800610
Xiaofei Baid25fab62021-12-02 06:36:27 +0000611 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800612
613 /*
614 * Ciphersuite list
615 *
616 * This is a list of the symmetric cipher options supported by
617 * the client, specifically the record protection algorithm
618 * ( including secret key length ) and a hash to be used with
619 * HKDF, in descending order of client preference.
620 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800621 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800622
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800623 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800624 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
625 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800626
Jerry Yu0c63af62021-09-02 12:59:12 +0800627 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000628 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800629 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800630 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800631 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800632 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800633
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800634 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800635 if( ciphersuite_info == NULL )
636 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800637 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
638 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800639 continue;
640
641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800642 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800643 ciphersuite_info->name ) );
644
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800645 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800646 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
647 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
648 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800649 }
650
Jerry Yu0c63af62021-09-02 12:59:12 +0800651 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000652 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800653 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800654 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800655 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
656 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800657
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800658 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000659 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800660
Jerry Yu6a643102021-08-31 14:40:36 +0800661 return( 0 );
662}
663
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800664/*
665 * Structure of ClientHello message:
666 *
667 * struct {
668 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
669 * Random random;
670 * opaque legacy_session_id<0..32>;
671 * CipherSuite cipher_suites<2..2^16-2>;
672 * opaque legacy_compression_methods<1..2^8-1>;
673 * Extension extensions<8..2^16-1>;
674 * } ClientHello;
675 */
Jerry Yu08906d02021-08-31 11:05:27 +0800676static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800677 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800678 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000679 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800680{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800681
Jerry Yubc20bdd2021-08-24 15:59:48 +0800682 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000683 unsigned char *p_extensions_len; /* Pointer to extensions length */
684 size_t output_len; /* Length of buffer used by function */
685 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800686
Jerry Yubc20bdd2021-08-24 15:59:48 +0800687 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800688 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800689
Xiaofei Baid25fab62021-12-02 06:36:27 +0000690 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800691
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800692 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800693 ssl->major_ver = ssl->conf->min_major_ver;
694 ssl->minor_ver = ssl->conf->min_minor_ver;
695
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800696 /*
697 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800698 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800699 *
700 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800701 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800702 */
Jerry Yufec982e2021-09-07 17:26:06 +0800703 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800704 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800705 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800706
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800707 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800708 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
709 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800710 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800711 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
712 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800713
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800714 /*
715 * Write legacy_session_id
716 *
717 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
718 * which has been merged with pre-shared keys in this version. A client
719 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
720 * this field to that value. In compatibility mode, this field MUST be
721 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
722 * a new 32-byte value. This value need not be random but SHOULD be
723 * unpredictable to avoid implementations fixating on a specific value
724 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
725 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800726 */
Jerry Yubbe09522021-09-06 21:17:54 +0800727 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
728 *p++ = 0; /* session id length set to zero */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800729
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800730 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800731 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800732 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800733 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800734 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800735
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800736 /* Write legacy_compression_methods
737 *
738 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800739 * one byte set to zero, which corresponds to the 'null' compression
740 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800741 */
Jerry Yubbe09522021-09-06 21:17:54 +0800742 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
743 *p++ = 1;
744 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800745
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800746 /* Write extensions */
747
748 /* Keeping track of the included extensions */
749 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800750
751 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800752 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000753 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800754 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800755
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800756 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800757 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800758 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800759 */
Jerry Yubbe09522021-09-06 21:17:54 +0800760 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800761 if( ret != 0 )
762 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800763 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800764
765#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800766 /* Write supported_groups extension
767 *
768 * It is REQUIRED for ECDHE cipher_suites.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800769 */
Jerry Yubbe09522021-09-06 21:17:54 +0800770 ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800771 if( ret != 0 )
772 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800773 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800774
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800775 /* Write key_share extension
776 *
777 * We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800778 * 1) A certificate-based ciphersuite is being offered. In this case
779 * supported_groups and supported_signature extensions have been
780 * successfully added.
781 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800782 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800783 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
784 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800785 */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800786 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800787 if( ret != 0 )
788 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800789 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800790
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800791 /* Write signature_algorithms extension
792 *
793 * It is REQUIRED for certificate authenticated cipher_suites.
794 */
Jerry Yubbe09522021-09-06 21:17:54 +0800795 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800796 if( ret != 0 )
797 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800798 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800799
Jerry Yubc20bdd2021-08-24 15:59:48 +0800800#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
801
Xiaofei Bai15a56812021-11-05 10:52:12 +0000802#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000803 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000804 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
805 if( ret != 0 )
806 return( ret );
807 p += output_len;
808#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
809
Jerry Yubc20bdd2021-08-24 15:59:48 +0800810 /* Add more extensions here */
811
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800812 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000813 extensions_len = p - p_extensions_len - 2;
814 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800816 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000817 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800818
Xiaofei Baid25fab62021-12-02 06:36:27 +0000819 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800820 return( 0 );
821}
822
Jerry Yu335aca92021-09-12 20:18:56 +0800823static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800824{
Jerry Yu92c6b402021-08-27 16:59:09 +0800825 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
826 return( 0 );
827}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800828
Jerry Yu92c6b402021-08-27 16:59:09 +0800829static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
830{
831 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800832
Jerry Yu92c6b402021-08-27 16:59:09 +0800833 if( ssl->conf->f_rng == NULL )
834 {
835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
836 return( MBEDTLS_ERR_SSL_NO_RNG );
837 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800838
Jerry Yu92c6b402021-08-27 16:59:09 +0800839 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
840 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800841 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800842 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800843 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800844 return( ret );
845 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800846
847 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800848}
849
Jerry Yu92c6b402021-08-27 16:59:09 +0800850/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800851 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800852 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800853 */
854static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800855{
Jerry Yu92c6b402021-08-27 16:59:09 +0800856 int ret = 0;
857 unsigned char *buf;
858 size_t buf_len, msg_len;
859
860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
861
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800862 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800863
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800864 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
865 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
866 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800867
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800868 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800869 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800870 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800871
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800872 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
873 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800874 msg_len );
875 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800876
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800877 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
878 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
879 buf_len,
880 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800881
882cleanup:
883
884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
885 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800886}
887
Jerry Yu687101b2021-09-14 16:03:56 +0800888/*
Jerry Yu4a173382021-10-11 21:45:31 +0800889 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800890 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800891/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800892 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
893 * - SSL_SERVER_HELLO_COORDINATE_HRR
894 * to indicate which message is expected and to be parsed next. */
895#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
896#define SSL_SERVER_HELLO_COORDINATE_HRR 1
897static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
898 const unsigned char *buf,
899 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800900{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800901 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800902 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
903 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
904 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
905 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
906
907 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
908 *
Jerry Yu4a173382021-10-11 21:45:31 +0800909 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800910 * special value of the SHA-256 of "HelloRetryRequest".
911 *
912 * struct {
913 * ProtocolVersion legacy_version = 0x0303;
914 * Random random;
915 * opaque legacy_session_id_echo<0..32>;
916 * CipherSuite cipher_suite;
917 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800918 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800919 * } ServerHello;
920 *
921 */
Jerry Yub85277e2021-10-13 13:36:05 +0800922 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800923
924 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800925 {
Jerry Yub85277e2021-10-13 13:36:05 +0800926 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800927 }
928
Jerry Yub85277e2021-10-13 13:36:05 +0800929 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800930}
931
Jerry Yu745bb612021-10-13 22:01:04 +0800932/* Fetch and preprocess
933 * Returns a negative value on failure, and otherwise
934 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
935 * - SSL_SERVER_HELLO_COORDINATE_HRR
936 */
Jerry Yub85277e2021-10-13 13:36:05 +0800937static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
938 unsigned char **buf,
939 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800940{
Jerry Yu4a173382021-10-11 21:45:31 +0800941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800942
943 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
944
Jerry Yue1b9c292021-09-10 10:08:31 +0800945 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
946 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
947 {
948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
949
950 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
951 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
952 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
953 }
954
955 *buf = ssl->in_msg + 4;
956 *buf_len = ssl->in_hslen - 4;
957
Jerry Yub85277e2021-10-13 13:36:05 +0800958 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
959 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800960 {
Jerry Yu745bb612021-10-13 22:01:04 +0800961 case SSL_SERVER_HELLO_COORDINATE_HELLO:
962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
963 break;
964 case SSL_SERVER_HELLO_COORDINATE_HRR:
965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
966 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800967 }
968
969cleanup:
970
971 return( ret );
972}
973
Jerry Yu4a173382021-10-11 21:45:31 +0800974static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
975 const unsigned char **buf,
976 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800977{
978 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800979 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800980
Jerry Yude4fb2c2021-09-19 18:05:08 +0800981 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800982 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800983
Jerry Yu4a173382021-10-11 21:45:31 +0800984 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800985
986 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800987 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
988 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800989 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800990 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
991 ssl->session_negotiate->id,
992 ssl->session_negotiate->id_len );
993 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800994 legacy_session_id_echo_len );
995
996 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
997 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
998
Jerry Yue1b9c292021-09-10 10:08:31 +0800999 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1000 }
1001
Jerry Yu4a173382021-10-11 21:45:31 +08001002 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001003 *buf = p;
1004
1005 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001006 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001007 return( 0 );
1008}
1009
Jerry Yu4a173382021-10-11 21:45:31 +08001010static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1011 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001012{
Jerry Yu4a173382021-10-11 21:45:31 +08001013 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1014
Jerry Yue1b9c292021-09-10 10:08:31 +08001015 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001016 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001017 {
Jerry Yu4a173382021-10-11 21:45:31 +08001018 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001019 {
1020 return( 1 );
1021 }
1022 }
1023 return( 0 );
1024}
1025
1026/* Parse ServerHello message and configure context
1027 *
1028 * struct {
1029 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1030 * Random random;
1031 * opaque legacy_session_id_echo<0..32>;
1032 * CipherSuite cipher_suite;
1033 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001034 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001035 * } ServerHello;
1036 */
Jerry Yuc068b662021-10-11 22:30:19 +08001037static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1038 const unsigned char *buf,
1039 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001040{
Jerry Yub85277e2021-10-13 13:36:05 +08001041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001042 const unsigned char *p = buf;
Jerry Yub85277e2021-10-13 13:36:05 +08001043 size_t extensions_len;
1044 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001045 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001046 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001047
1048 /*
1049 * Check there is space for minimal fields
1050 *
1051 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001052 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001053 * - legacy_session_id_echo ( 1 byte ), minimum size
1054 * - cipher_suite ( 2 bytes)
1055 * - legacy_compression_method ( 1 byte )
1056 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001057 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001058
1059 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001060 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1061
Jerry Yu4a173382021-10-11 21:45:31 +08001062 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001063 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001064 * ...
1065 * with ProtocolVersion defined as:
1066 * uint16 ProtocolVersion;
1067 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001068 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1069 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1070 {
1071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1072 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1073 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1074 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1075 }
1076 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001077
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001078 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001079 * Random random;
1080 * ...
1081 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001082 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001083 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001084 memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1085 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yue1b9c292021-09-10 10:08:31 +08001086 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001087 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1088 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001089
Jerry Yu4a173382021-10-11 21:45:31 +08001090 /* ...
1091 * opaque legacy_session_id_echo<0..32>;
1092 * ...
1093 */
1094 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001095 {
1096 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1097 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1098 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1099 }
1100
Jerry Yu4a173382021-10-11 21:45:31 +08001101 /* ...
1102 * CipherSuite cipher_suite;
1103 * ...
1104 * with CipherSuite defined as:
1105 * uint8 CipherSuite[2];
1106 */
1107 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001108 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1109 p += 2;
1110
Jerry Yu4a173382021-10-11 21:45:31 +08001111
1112 /*
1113 * Check whether this ciphersuite is supported and offered.
1114 * Via the force_ciphersuite version we may have instructed the client
1115 * to use a different ciphersuite.
1116 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001117 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001118 if( ciphersuite_info == NULL ||
1119 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001120 {
Jerry Yub85277e2021-10-13 13:36:05 +08001121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 cipher_suite ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001123
Jerry Yu4a173382021-10-11 21:45:31 +08001124 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1125 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1126 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +08001127 }
1128
Jerry Yue1b9c292021-09-10 10:08:31 +08001129
Jerry Yu4a173382021-10-11 21:45:31 +08001130 /* Configure ciphersuites */
1131 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1132
1133 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001134 ssl->session_negotiate->ciphersuite = cipher_suite;
1135
Jerry Yue1b9c292021-09-10 10:08:31 +08001136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1137 cipher_suite, ciphersuite_info->name ) );
1138
1139#if defined(MBEDTLS_HAVE_TIME)
1140 ssl->session_negotiate->start = time( NULL );
1141#endif /* MBEDTLS_HAVE_TIME */
1142
Jerry Yu4a173382021-10-11 21:45:31 +08001143 /* ...
1144 * uint8 legacy_compression_method = 0;
1145 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001146 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001147 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001148 if( p[0] != 0 )
1149 {
Jerry Yub85277e2021-10-13 13:36:05 +08001150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1152 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1153 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1154 }
1155 p++;
1156
Jerry Yub85277e2021-10-13 13:36:05 +08001157 /* ...
1158 * Extension extensions<6..2^16-1>;
1159 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001160 * struct {
1161 * ExtensionType extension_type; (2 bytes)
1162 * opaque extension_data<0..2^16-1>;
1163 * } Extension;
1164 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001165 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001166 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001167 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001168
Jerry Yu4a173382021-10-11 21:45:31 +08001169 /* Check extensions do not go beyond the buffer of data. */
1170 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1171 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001172
Jerry Yu4a173382021-10-11 21:45:31 +08001173 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001174
Jerry Yu4a173382021-10-11 21:45:31 +08001175 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001176 {
1177 unsigned int extension_type;
1178 size_t extension_data_len;
1179
Jerry Yu4a173382021-10-11 21:45:31 +08001180 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001181 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1182 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1183 p += 4;
1184
Jerry Yu4a173382021-10-11 21:45:31 +08001185 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001186
1187 switch( extension_type )
1188 {
1189 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1190 MBEDTLS_SSL_DEBUG_MSG( 3,
1191 ( "found supported_versions extension" ) );
1192
Jerry Yuc068b662021-10-11 22:30:19 +08001193 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001194 p,
1195 p + extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001196 if( ret != 0 )
1197 return( ret );
1198 break;
1199
1200 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001203
1204 MBEDTLS_SSL_PEND_FATAL_ALERT(
1205 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1206 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1207 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001208
1209#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1210 case MBEDTLS_TLS_EXT_KEY_SHARE:
1211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001212 if( ( ret = ssl_tls13_parse_key_share_ext( ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +08001213 p, p + extension_data_len ) ) != 0 )
1214 {
1215 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001216 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001217 ret );
1218 return( ret );
1219 }
1220 break;
1221#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1222
1223 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001224 MBEDTLS_SSL_DEBUG_MSG(
1225 3,
1226 ( "unknown extension found: %u ( ignoring )",
1227 extension_type ) );
1228
1229 MBEDTLS_SSL_PEND_FATAL_ALERT(
1230 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1231 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1232 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001233 }
1234
1235 p += extension_data_len;
1236 }
1237
1238 return( 0 );
1239}
1240
Jerry Yuc068b662021-10-11 22:30:19 +08001241static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001242{
Jerry Yub85277e2021-10-13 13:36:05 +08001243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001244 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001245 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001246 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001247
Jerry Yub85277e2021-10-13 13:36:05 +08001248 /* Determine the key exchange mode:
1249 * 1) If both the pre_shared_key and key_share extensions were received
1250 * then the key exchange mode is PSK with EPHEMERAL.
1251 * 2) If only the pre_shared_key extension was received then the key
1252 * exchange mode is PSK-only.
1253 * 3) If only the key_share extension was received then the key
1254 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001255 */
Jerry Yub85277e2021-10-13 13:36:05 +08001256 switch( handshake->extensions_present &
1257 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001258 {
Jerry Yu745bb612021-10-13 22:01:04 +08001259 /* Only the pre_shared_key extension was received */
1260 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001261 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001262 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001263
Jerry Yu745bb612021-10-13 22:01:04 +08001264 /* Only the key_share extension was received */
1265 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001266 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001267 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001268
Jerry Yu745bb612021-10-13 22:01:04 +08001269 /* Both the pre_shared_key and key_share extensions were received */
1270 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001271 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001272 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001273
Jerry Yu745bb612021-10-13 22:01:04 +08001274 /* Neither pre_shared_key nor key_share extension was received */
1275 default:
1276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1277 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1278 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001279 }
1280
1281 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1282 *
1283 * TODO: We don't have to do this in case we offered 0-RTT and the
1284 * server accepted it. In this case, we could skip generating
1285 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001286 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001287 if( ret != 0 )
1288 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001290 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001291 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001292 }
1293
1294 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001295 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001296 if( ret != 0 )
1297 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001299 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001300 }
1301
1302 /* Next evolution in key schedule: Establish handshake secret and
1303 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001304 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001305 if( ret != 0 )
1306 {
Jerry Yuc068b662021-10-11 22:30:19 +08001307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1308 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001309 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001310 }
1311
Jerry Yub85277e2021-10-13 13:36:05 +08001312 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001313 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001314 {
1315 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1316 goto cleanup;
1317 }
Jerry Yu0b177842021-09-05 19:41:30 +08001318
1319 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1320 ssl->conf->endpoint,
1321 ssl->session_negotiate->ciphersuite,
1322 &traffic_keys,
1323 ssl );
1324 if( ret != 0 )
1325 {
1326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001327 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001328 }
1329
Jerry Yub85277e2021-10-13 13:36:05 +08001330 handshake->transform_handshake = transform_handshake;
1331 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001332
1333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1334 ssl->session_in = ssl->session_negotiate;
1335
1336 /*
1337 * State machine update
1338 */
1339 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1340
Jerry Yu4a173382021-10-11 21:45:31 +08001341cleanup:
1342
Jerry Yu0b177842021-09-05 19:41:30 +08001343 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001344 if( ret != 0 )
1345 {
Jerry Yub85277e2021-10-13 13:36:05 +08001346 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001347
Jerry Yu4a173382021-10-11 21:45:31 +08001348 MBEDTLS_SSL_PEND_FATAL_ALERT(
1349 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1350 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1351 }
1352 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001353}
1354
1355/*
Jerry Yu4a173382021-10-11 21:45:31 +08001356 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001357 * Handler for MBEDTLS_SSL_SERVER_HELLO
1358 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001359static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001360{
Jerry Yu4a173382021-10-11 21:45:31 +08001361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001362 unsigned char *buf;
1363 size_t buf_len;
1364
1365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1366
1367 /* Coordination step
1368 * - Fetch record
1369 * - Make sure it's either a ServerHello or a HRR.
1370 * - Switch processing routine in case of HRR
1371 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001372 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1373 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1374
Jerry Yub85277e2021-10-13 13:36:05 +08001375 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001376 /* Parsing step
1377 * We know what message to expect by now and call
1378 * the respective parsing function.
1379 */
1380 if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
1381 {
Jerry Yuc068b662021-10-11 22:30:19 +08001382 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
1383 buf + buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001384
Xiaofei Bai746f9482021-11-12 08:53:56 +00001385 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1386 MBEDTLS_SSL_HS_SERVER_HELLO,
1387 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001388
Jerry Yuc068b662021-10-11 22:30:19 +08001389 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001390 }
1391 else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
1392 {
Jerry Yu4a173382021-10-11 21:45:31 +08001393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "HRR not supported" ) );
1394 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ,
1395 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1396 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yue1b9c292021-09-10 10:08:31 +08001397 }
1398
1399cleanup:
1400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
1401 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001402}
1403
1404/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001405 *
1406 * EncryptedExtensions message
1407 *
1408 * The EncryptedExtensions message contains any extensions which
1409 * should be protected, i.e., any which are not needed to establish
1410 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001411 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001412
1413/*
1414 * Overview
1415 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001416
1417/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001418static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001419
XiaokangQian97799ac2021-10-11 10:05:54 +00001420static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1421 const unsigned char *buf,
1422 const unsigned char *end );
1423static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001424
1425/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001426 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001427 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001428static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001429{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001430 int ret;
1431 unsigned char *buf;
1432 size_t buf_len;
1433
1434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1435
Xiaofei Bai746f9482021-11-12 08:53:56 +00001436 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001437 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001438 &buf, &buf_len ) );
1439
1440 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001441 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001442 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001443
Xiaofei Bai746f9482021-11-12 08:53:56 +00001444 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001445 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001446
XiaokangQian97799ac2021-10-11 10:05:54 +00001447 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001448
1449cleanup:
1450
1451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1452 return( ret );
1453
1454}
1455
XiaokangQian08da26c2021-10-09 10:12:11 +00001456/* Parse EncryptedExtensions message
1457 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001458 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001459 * } EncryptedExtensions;
1460 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001461static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1462 const unsigned char *buf,
1463 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001464{
1465 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001466 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001467 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001468 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001469
XiaokangQian08da26c2021-10-09 10:12:11 +00001470 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001471 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001472 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001473
XiaokangQian97799ac2021-10-11 10:05:54 +00001474 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001475 extensions_end = p + extensions_len;
1476 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001477
XiaokangQian8db25ff2021-10-13 05:56:18 +00001478 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001479 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001480 unsigned int extension_type;
1481 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001482
XiaokangQian08da26c2021-10-09 10:12:11 +00001483 /*
1484 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001485 * ExtensionType extension_type; (2 bytes)
1486 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001487 * } Extension;
1488 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001489 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001490 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1491 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1492 p += 4;
1493
XiaokangQian8db25ff2021-10-13 05:56:18 +00001494 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001495
XiaokangQian97799ac2021-10-11 10:05:54 +00001496 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001497 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001498 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001499 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001500 switch( extension_type )
1501 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001502
XiaokangQian08da26c2021-10-09 10:12:11 +00001503 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1505 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001506
XiaokangQian08da26c2021-10-09 10:12:11 +00001507 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001508 MBEDTLS_SSL_DEBUG_MSG(
1509 3, ( "unsupported extension found: %u ", extension_type) );
1510 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001511 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001512 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1513 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001514 }
1515
XiaokangQian08da26c2021-10-09 10:12:11 +00001516 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001517 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001518
XiaokangQian97799ac2021-10-11 10:05:54 +00001519 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001520 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001521 {
1522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001523 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001524 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001525 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001526 }
1527
1528 return( ret );
1529}
1530
XiaokangQian97799ac2021-10-11 10:05:54 +00001531static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001532{
Jerry Yua93ac112021-10-27 16:31:48 +08001533#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001534 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001535 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1536 else
Jerry Yud2674312021-10-29 10:08:19 +08001537 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001538#else
1539 ((void) ssl);
1540 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1541#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001542 return( 0 );
1543}
1544
Jerry Yua93ac112021-10-27 16:31:48 +08001545#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001546/*
Jerry Yud2674312021-10-29 10:08:19 +08001547 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1548 */
1549static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1550{
1551 int ret = mbedtls_ssl_read_record( ssl, 0 );
1552
1553 if( ret != 0 )
1554 {
1555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1556 return( ret );
1557 }
1558
1559 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1560 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1561 {
1562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1563 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1564 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1565 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1566 }
1567
1568 ssl->keep_current_message = 1;
1569 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1570
1571 return( 0 );
1572}
1573
1574/*
Jerry Yu687101b2021-09-14 16:03:56 +08001575 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1576 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001577static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001578{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001579 int ret;
1580
1581 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001582 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001583 return( ret );
1584
Jerry Yu687101b2021-09-14 16:03:56 +08001585 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1586 return( 0 );
1587}
1588
1589/*
1590 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1591 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001592static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001593{
Jerry Yu30b071c2021-09-12 20:16:03 +08001594 int ret;
1595
1596 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1597 if( ret != 0 )
1598 return( ret );
1599
Jerry Yu687101b2021-09-14 16:03:56 +08001600 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1601 return( 0 );
1602}
Jerry Yua93ac112021-10-27 16:31:48 +08001603#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001604/*
1605 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1606 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001607static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001608{
XiaokangQianac0385c2021-11-03 06:40:11 +00001609 int ret;
1610
XiaokangQianc5c39d52021-11-09 11:55:10 +00001611 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001612 if( ret != 0 )
1613 return( ret );
1614
XiaokangQian3ce4d512021-11-17 02:11:36 +00001615 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
XiaokangQianac0385c2021-11-03 06:40:11 +00001616 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1617 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001618}
1619
1620/*
Jerry Yu687101b2021-09-14 16:03:56 +08001621 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1622 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001623static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001624{
XiaokangQian0fa66432021-11-15 03:33:57 +00001625 int ret;
1626
1627 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1628 if( ret != 0 )
1629 return( ret );
1630
1631 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1632 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001633}
1634
1635/*
1636 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1637 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001638static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001639{
Jerry Yu378254d2021-10-30 21:44:47 +08001640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001641 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001642 return( 0 );
1643}
1644
1645/*
1646 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1647 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001648static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001649{
Jerry Yu378254d2021-10-30 21:44:47 +08001650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1651 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1652
1653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1654 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1655
1656 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1657
1658 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1659 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001660}
1661
Jerry Yu92c6b402021-08-27 16:59:09 +08001662int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001663{
Jerry Yu92c6b402021-08-27 16:59:09 +08001664 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001665
Xiaofei Baid25fab62021-12-02 06:36:27 +00001666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %d", ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001667
1668 switch( ssl->state )
1669 {
1670 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001671 * ssl->state is initialized as HELLO_REQUEST. It is the same
1672 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001673 */
1674 case MBEDTLS_SSL_HELLO_REQUEST:
1675 case MBEDTLS_SSL_CLIENT_HELLO:
1676 ret = ssl_tls13_write_client_hello( ssl );
1677 break;
1678
1679 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001680 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001681 break;
1682
1683 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001684 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001685 break;
1686
Jerry Yua93ac112021-10-27 16:31:48 +08001687#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001688 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1689 ret = ssl_tls13_process_certificate_request( ssl );
1690 break;
1691
Jerry Yu687101b2021-09-14 16:03:56 +08001692 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001693 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001694 break;
1695
1696 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001697 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001698 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001699#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001700
1701 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001702 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001703 break;
1704
Jerry Yu687101b2021-09-14 16:03:56 +08001705 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001706 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001707 break;
1708
1709 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001710 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001711 break;
1712
1713 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001714 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001715 break;
1716
1717 default:
1718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1719 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1720 }
1721
1722 return( ret );
1723}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001724
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001725#endif /* MBEDTLS_SSL_CLI_C */
1726
1727#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */