blob: 31d7dafdb95d0e5b2e29dde942e08f268060a849 [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"
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 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100727#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
728 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
729 *p++ = (unsigned char)ssl->session_negotiate->id_len;
730 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
731 p += ssl->session_negotiate->id_len;
732
733 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
734 ssl->session_negotiate->id_len );
735#else
Jerry Yubbe09522021-09-06 21:17:54 +0800736 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
737 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100738#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800739
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800740 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800741 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800742 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800743 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800744 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800745
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800746 /* Write legacy_compression_methods
747 *
748 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800749 * one byte set to zero, which corresponds to the 'null' compression
750 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800751 */
Jerry Yubbe09522021-09-06 21:17:54 +0800752 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
753 *p++ = 1;
754 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800755
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800756 /* Write extensions */
757
758 /* Keeping track of the included extensions */
759 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800760
761 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800762 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000763 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800764 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800765
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800766 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800767 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800768 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800769 */
Jerry Yubbe09522021-09-06 21:17:54 +0800770 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +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
775#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800776 /* Write supported_groups extension
777 *
778 * It is REQUIRED for ECDHE cipher_suites.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800779 */
Jerry Yubbe09522021-09-06 21:17:54 +0800780 ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800781 if( ret != 0 )
782 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800783 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800784
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800785 /* Write key_share extension
786 *
787 * We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800788 * 1) A certificate-based ciphersuite is being offered. In this case
789 * supported_groups and supported_signature extensions have been
790 * successfully added.
791 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800792 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800793 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
794 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800795 */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800796 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800797 if( ret != 0 )
798 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800799 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800800
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800801 /* Write signature_algorithms extension
802 *
803 * It is REQUIRED for certificate authenticated cipher_suites.
804 */
Jerry Yubbe09522021-09-06 21:17:54 +0800805 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800806 if( ret != 0 )
807 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800808 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800809
Jerry Yubc20bdd2021-08-24 15:59:48 +0800810#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
811
Xiaofei Bai15a56812021-11-05 10:52:12 +0000812#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000813 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000814 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
815 if( ret != 0 )
816 return( ret );
817 p += output_len;
818#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
819
Jerry Yubc20bdd2021-08-24 15:59:48 +0800820 /* Add more extensions here */
821
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800822 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000823 extensions_len = p - p_extensions_len - 2;
824 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800826 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000827 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800828
Xiaofei Baid25fab62021-12-02 06:36:27 +0000829 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800830 return( 0 );
831}
832
Jerry Yu335aca92021-09-12 20:18:56 +0800833static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800834{
Jerry Yu92c6b402021-08-27 16:59:09 +0800835 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
836 return( 0 );
837}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800838
Jerry Yu92c6b402021-08-27 16:59:09 +0800839static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
840{
841 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800842
Jerry Yu92c6b402021-08-27 16:59:09 +0800843 if( ssl->conf->f_rng == NULL )
844 {
845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
846 return( MBEDTLS_ERR_SSL_NO_RNG );
847 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800848
Jerry Yu92c6b402021-08-27 16:59:09 +0800849 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
850 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800851 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800852 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800853 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800854 return( ret );
855 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800856
Ronald Cron49ad6192021-11-24 16:25:31 +0100857#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
858 /*
859 * Create a session identifier for the purpose of middlebox compatibility
860 * only if one has not been created already.
861 */
862 if( ssl->session_negotiate->id_len == 0 )
863 {
864 /* Creating a session id with 32 byte length */
865 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
866 ssl->session_negotiate->id, 32 ) ) != 0 )
867 {
868 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
869 return( ret );
870 }
871 ssl->session_negotiate->id_len = 32;
872 }
873#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
874
Jerry Yu6f13f642021-08-26 17:18:15 +0800875 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800876}
877
Jerry Yu92c6b402021-08-27 16:59:09 +0800878/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800879 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800880 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800881 */
882static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800883{
Jerry Yu92c6b402021-08-27 16:59:09 +0800884 int ret = 0;
885 unsigned char *buf;
886 size_t buf_len, msg_len;
887
888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
889
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800890 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800891
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800892 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
893 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
894 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800895
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800896 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800897 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800898 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800899
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800900 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
901 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800902 msg_len );
903 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800904
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800905 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
906 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
907 buf_len,
908 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800909
910cleanup:
911
912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
913 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800914}
915
Jerry Yu687101b2021-09-14 16:03:56 +0800916/*
Jerry Yu4a173382021-10-11 21:45:31 +0800917 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800918 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800919/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800920 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
921 * - SSL_SERVER_HELLO_COORDINATE_HRR
922 * to indicate which message is expected and to be parsed next. */
923#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
924#define SSL_SERVER_HELLO_COORDINATE_HRR 1
925static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
926 const unsigned char *buf,
927 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800928{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800929 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800930 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
931 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
932 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
933 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
934
935 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
936 *
Jerry Yu4a173382021-10-11 21:45:31 +0800937 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800938 * special value of the SHA-256 of "HelloRetryRequest".
939 *
940 * struct {
941 * ProtocolVersion legacy_version = 0x0303;
942 * Random random;
943 * opaque legacy_session_id_echo<0..32>;
944 * CipherSuite cipher_suite;
945 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800946 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800947 * } ServerHello;
948 *
949 */
Jerry Yub85277e2021-10-13 13:36:05 +0800950 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800951
952 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800953 {
Jerry Yub85277e2021-10-13 13:36:05 +0800954 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800955 }
956
Jerry Yub85277e2021-10-13 13:36:05 +0800957 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800958}
959
Jerry Yu745bb612021-10-13 22:01:04 +0800960/* Fetch and preprocess
961 * Returns a negative value on failure, and otherwise
962 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
963 * - SSL_SERVER_HELLO_COORDINATE_HRR
964 */
Jerry Yub85277e2021-10-13 13:36:05 +0800965static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
966 unsigned char **buf,
967 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800968{
Jerry Yu4a173382021-10-11 21:45:31 +0800969 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800970
971 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
972
Jerry Yue1b9c292021-09-10 10:08:31 +0800973 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
974 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
975 {
976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
977
978 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
979 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
980 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
981 }
982
983 *buf = ssl->in_msg + 4;
984 *buf_len = ssl->in_hslen - 4;
985
Jerry Yub85277e2021-10-13 13:36:05 +0800986 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
987 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800988 {
Jerry Yu745bb612021-10-13 22:01:04 +0800989 case SSL_SERVER_HELLO_COORDINATE_HELLO:
990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
991 break;
992 case SSL_SERVER_HELLO_COORDINATE_HRR:
993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
994 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800995 }
996
997cleanup:
998
999 return( ret );
1000}
1001
Jerry Yu4a173382021-10-11 21:45:31 +08001002static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1003 const unsigned char **buf,
1004 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001005{
1006 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001007 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001008
Jerry Yude4fb2c2021-09-19 18:05:08 +08001009 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001010 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001011
Jerry Yu4a173382021-10-11 21:45:31 +08001012 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001013
1014 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001015 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1016 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001017 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001018 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1019 ssl->session_negotiate->id,
1020 ssl->session_negotiate->id_len );
1021 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001022 legacy_session_id_echo_len );
1023
1024 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1025 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1026
Jerry Yue1b9c292021-09-10 10:08:31 +08001027 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1028 }
1029
Jerry Yu4a173382021-10-11 21:45:31 +08001030 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001031 *buf = p;
1032
1033 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001034 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001035 return( 0 );
1036}
1037
Jerry Yu4a173382021-10-11 21:45:31 +08001038static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1039 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001040{
Jerry Yu4a173382021-10-11 21:45:31 +08001041 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1042
Jerry Yue1b9c292021-09-10 10:08:31 +08001043 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001044 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001045 {
Jerry Yu4a173382021-10-11 21:45:31 +08001046 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001047 {
1048 return( 1 );
1049 }
1050 }
1051 return( 0 );
1052}
1053
1054/* Parse ServerHello message and configure context
1055 *
1056 * struct {
1057 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1058 * Random random;
1059 * opaque legacy_session_id_echo<0..32>;
1060 * CipherSuite cipher_suite;
1061 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001062 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001063 * } ServerHello;
1064 */
Jerry Yuc068b662021-10-11 22:30:19 +08001065static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1066 const unsigned char *buf,
1067 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001068{
Jerry Yub85277e2021-10-13 13:36:05 +08001069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001070 const unsigned char *p = buf;
Jerry Yub85277e2021-10-13 13:36:05 +08001071 size_t extensions_len;
1072 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001073 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001074 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001075
1076 /*
1077 * Check there is space for minimal fields
1078 *
1079 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001080 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001081 * - legacy_session_id_echo ( 1 byte ), minimum size
1082 * - cipher_suite ( 2 bytes)
1083 * - legacy_compression_method ( 1 byte )
1084 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001085 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001086
1087 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001088 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1089
Jerry Yu4a173382021-10-11 21:45:31 +08001090 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001091 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001092 * ...
1093 * with ProtocolVersion defined as:
1094 * uint16 ProtocolVersion;
1095 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001096 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1097 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1098 {
1099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1100 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1101 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1102 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1103 }
1104 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001105
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001106 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001107 * Random random;
1108 * ...
1109 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001110 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001111 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001112 memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1113 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yue1b9c292021-09-10 10:08:31 +08001114 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001115 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1116 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001117
Jerry Yu4a173382021-10-11 21:45:31 +08001118 /* ...
1119 * opaque legacy_session_id_echo<0..32>;
1120 * ...
1121 */
1122 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001123 {
1124 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1125 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1126 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1127 }
1128
Jerry Yu4a173382021-10-11 21:45:31 +08001129 /* ...
1130 * CipherSuite cipher_suite;
1131 * ...
1132 * with CipherSuite defined as:
1133 * uint8 CipherSuite[2];
1134 */
1135 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001136 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1137 p += 2;
1138
Jerry Yu4a173382021-10-11 21:45:31 +08001139
1140 /*
1141 * Check whether this ciphersuite is supported and offered.
1142 * Via the force_ciphersuite version we may have instructed the client
1143 * to use a different ciphersuite.
1144 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001145 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001146 if( ciphersuite_info == NULL ||
1147 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001148 {
Jerry Yub85277e2021-10-13 13:36:05 +08001149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
Jerry Yue1b9c292021-09-10 10:08:31 +08001150 cipher_suite ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001151
Jerry Yu4a173382021-10-11 21:45:31 +08001152 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1153 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1154 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +08001155 }
1156
Jerry Yue1b9c292021-09-10 10:08:31 +08001157
Jerry Yu4a173382021-10-11 21:45:31 +08001158 /* Configure ciphersuites */
1159 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1160
1161 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001162 ssl->session_negotiate->ciphersuite = cipher_suite;
1163
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1165 cipher_suite, ciphersuite_info->name ) );
1166
1167#if defined(MBEDTLS_HAVE_TIME)
1168 ssl->session_negotiate->start = time( NULL );
1169#endif /* MBEDTLS_HAVE_TIME */
1170
Jerry Yu4a173382021-10-11 21:45:31 +08001171 /* ...
1172 * uint8 legacy_compression_method = 0;
1173 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001174 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001175 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001176 if( p[0] != 0 )
1177 {
Jerry Yub85277e2021-10-13 13:36:05 +08001178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001179 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1180 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1181 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1182 }
1183 p++;
1184
Jerry Yub85277e2021-10-13 13:36:05 +08001185 /* ...
1186 * Extension extensions<6..2^16-1>;
1187 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001188 * struct {
1189 * ExtensionType extension_type; (2 bytes)
1190 * opaque extension_data<0..2^16-1>;
1191 * } Extension;
1192 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001193 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001194 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001195 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001196
Jerry Yu4a173382021-10-11 21:45:31 +08001197 /* Check extensions do not go beyond the buffer of data. */
1198 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1199 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001200
Jerry Yu4a173382021-10-11 21:45:31 +08001201 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001202
Jerry Yu4a173382021-10-11 21:45:31 +08001203 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001204 {
1205 unsigned int extension_type;
1206 size_t extension_data_len;
1207
Jerry Yu4a173382021-10-11 21:45:31 +08001208 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001209 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1210 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1211 p += 4;
1212
Jerry Yu4a173382021-10-11 21:45:31 +08001213 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001214
1215 switch( extension_type )
1216 {
1217 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1218 MBEDTLS_SSL_DEBUG_MSG( 3,
1219 ( "found supported_versions extension" ) );
1220
Jerry Yuc068b662021-10-11 22:30:19 +08001221 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001222 p,
1223 p + extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001224 if( ret != 0 )
1225 return( ret );
1226 break;
1227
1228 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001231
1232 MBEDTLS_SSL_PEND_FATAL_ALERT(
1233 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1234 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1235 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001236
1237#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1238 case MBEDTLS_TLS_EXT_KEY_SHARE:
1239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001240 if( ( ret = ssl_tls13_parse_key_share_ext( ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +08001241 p, p + extension_data_len ) ) != 0 )
1242 {
1243 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001244 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001245 ret );
1246 return( ret );
1247 }
1248 break;
1249#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1250
1251 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001252 MBEDTLS_SSL_DEBUG_MSG(
1253 3,
1254 ( "unknown extension found: %u ( ignoring )",
1255 extension_type ) );
1256
1257 MBEDTLS_SSL_PEND_FATAL_ALERT(
1258 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1259 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1260 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001261 }
1262
1263 p += extension_data_len;
1264 }
1265
1266 return( 0 );
1267}
1268
Jerry Yuc068b662021-10-11 22:30:19 +08001269static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001270{
Jerry Yub85277e2021-10-13 13:36:05 +08001271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001272 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001273 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001274 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001275
Jerry Yub85277e2021-10-13 13:36:05 +08001276 /* Determine the key exchange mode:
1277 * 1) If both the pre_shared_key and key_share extensions were received
1278 * then the key exchange mode is PSK with EPHEMERAL.
1279 * 2) If only the pre_shared_key extension was received then the key
1280 * exchange mode is PSK-only.
1281 * 3) If only the key_share extension was received then the key
1282 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001283 */
Jerry Yub85277e2021-10-13 13:36:05 +08001284 switch( handshake->extensions_present &
1285 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001286 {
Jerry Yu745bb612021-10-13 22:01:04 +08001287 /* Only the pre_shared_key extension was received */
1288 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001289 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001290 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001291
Jerry Yu745bb612021-10-13 22:01:04 +08001292 /* Only the key_share extension was received */
1293 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001294 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001295 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001296
Jerry Yu745bb612021-10-13 22:01:04 +08001297 /* Both the pre_shared_key and key_share extensions were received */
1298 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001299 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001300 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001301
Jerry Yu745bb612021-10-13 22:01:04 +08001302 /* Neither pre_shared_key nor key_share extension was received */
1303 default:
1304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1305 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1306 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001307 }
1308
1309 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1310 *
1311 * TODO: We don't have to do this in case we offered 0-RTT and the
1312 * server accepted it. In this case, we could skip generating
1313 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001314 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001315 if( ret != 0 )
1316 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001318 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001319 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001320 }
1321
1322 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001323 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001324 if( ret != 0 )
1325 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001327 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001328 }
1329
1330 /* Next evolution in key schedule: Establish handshake secret and
1331 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001332 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001333 if( ret != 0 )
1334 {
Jerry Yuc068b662021-10-11 22:30:19 +08001335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1336 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001337 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001338 }
1339
Jerry Yub85277e2021-10-13 13:36:05 +08001340 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001341 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001342 {
1343 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1344 goto cleanup;
1345 }
Jerry Yu0b177842021-09-05 19:41:30 +08001346
1347 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1348 ssl->conf->endpoint,
1349 ssl->session_negotiate->ciphersuite,
1350 &traffic_keys,
1351 ssl );
1352 if( ret != 0 )
1353 {
1354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001355 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001356 }
1357
Jerry Yub85277e2021-10-13 13:36:05 +08001358 handshake->transform_handshake = transform_handshake;
1359 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001360
1361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1362 ssl->session_in = ssl->session_negotiate;
1363
1364 /*
1365 * State machine update
1366 */
1367 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1368
Jerry Yu4a173382021-10-11 21:45:31 +08001369cleanup:
1370
Jerry Yu0b177842021-09-05 19:41:30 +08001371 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001372 if( ret != 0 )
1373 {
Jerry Yub85277e2021-10-13 13:36:05 +08001374 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001375
Jerry Yu4a173382021-10-11 21:45:31 +08001376 MBEDTLS_SSL_PEND_FATAL_ALERT(
1377 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1378 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1379 }
1380 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001381}
1382
1383/*
Jerry Yu4a173382021-10-11 21:45:31 +08001384 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001385 * Handler for MBEDTLS_SSL_SERVER_HELLO
1386 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001387static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001388{
Jerry Yu4a173382021-10-11 21:45:31 +08001389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001390 unsigned char *buf;
1391 size_t buf_len;
1392
1393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1394
1395 /* Coordination step
1396 * - Fetch record
1397 * - Make sure it's either a ServerHello or a HRR.
1398 * - Switch processing routine in case of HRR
1399 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001400 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1401 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1402
Jerry Yub85277e2021-10-13 13:36:05 +08001403 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001404 /* Parsing step
1405 * We know what message to expect by now and call
1406 * the respective parsing function.
1407 */
1408 if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
1409 {
Jerry Yuc068b662021-10-11 22:30:19 +08001410 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
1411 buf + buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001412
Xiaofei Bai746f9482021-11-12 08:53:56 +00001413 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1414 MBEDTLS_SSL_HS_SERVER_HELLO,
1415 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001416
Jerry Yuc068b662021-10-11 22:30:19 +08001417 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001418 }
1419 else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
1420 {
Jerry Yu4a173382021-10-11 21:45:31 +08001421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "HRR not supported" ) );
1422 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ,
1423 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1424 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yue1b9c292021-09-10 10:08:31 +08001425 }
1426
1427cleanup:
1428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
1429 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001430}
1431
1432/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001433 *
1434 * EncryptedExtensions message
1435 *
1436 * The EncryptedExtensions message contains any extensions which
1437 * should be protected, i.e., any which are not needed to establish
1438 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001439 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001440
1441/*
1442 * Overview
1443 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001444
1445/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001446static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001447
XiaokangQian97799ac2021-10-11 10:05:54 +00001448static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1449 const unsigned char *buf,
1450 const unsigned char *end );
1451static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001452
1453/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001454 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001455 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001456static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001457{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001458 int ret;
1459 unsigned char *buf;
1460 size_t buf_len;
1461
1462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1463
Xiaofei Bai746f9482021-11-12 08:53:56 +00001464 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001465 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001466 &buf, &buf_len ) );
1467
1468 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001469 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001470 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001471
Xiaofei Bai746f9482021-11-12 08:53:56 +00001472 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001473 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001474
XiaokangQian97799ac2021-10-11 10:05:54 +00001475 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001476
1477cleanup:
1478
1479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1480 return( ret );
1481
1482}
1483
XiaokangQian08da26c2021-10-09 10:12:11 +00001484/* Parse EncryptedExtensions message
1485 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001486 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001487 * } EncryptedExtensions;
1488 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001489static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1490 const unsigned char *buf,
1491 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001492{
1493 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001494 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001495 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001496 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001497
XiaokangQian08da26c2021-10-09 10:12:11 +00001498 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001499 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001500 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001501
XiaokangQian97799ac2021-10-11 10:05:54 +00001502 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001503 extensions_end = p + extensions_len;
1504 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001505
XiaokangQian8db25ff2021-10-13 05:56:18 +00001506 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001507 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001508 unsigned int extension_type;
1509 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001510
XiaokangQian08da26c2021-10-09 10:12:11 +00001511 /*
1512 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001513 * ExtensionType extension_type; (2 bytes)
1514 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001515 * } Extension;
1516 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001517 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001518 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1519 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1520 p += 4;
1521
XiaokangQian8db25ff2021-10-13 05:56:18 +00001522 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001523
XiaokangQian97799ac2021-10-11 10:05:54 +00001524 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001525 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001526 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001527 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001528 switch( extension_type )
1529 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001530
XiaokangQian08da26c2021-10-09 10:12:11 +00001531 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1533 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001534
XiaokangQian08da26c2021-10-09 10:12:11 +00001535 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001536 MBEDTLS_SSL_DEBUG_MSG(
1537 3, ( "unsupported extension found: %u ", extension_type) );
1538 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001539 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001540 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1541 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001542 }
1543
XiaokangQian08da26c2021-10-09 10:12:11 +00001544 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001545 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001546
XiaokangQian97799ac2021-10-11 10:05:54 +00001547 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001548 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001549 {
1550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001551 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001552 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001553 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001554 }
1555
1556 return( ret );
1557}
1558
XiaokangQian97799ac2021-10-11 10:05:54 +00001559static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001560{
Jerry Yua93ac112021-10-27 16:31:48 +08001561#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001562 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001563 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1564 else
Jerry Yud2674312021-10-29 10:08:19 +08001565 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001566#else
1567 ((void) ssl);
1568 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1569#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001570 return( 0 );
1571}
1572
Jerry Yua93ac112021-10-27 16:31:48 +08001573#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001574/*
Jerry Yud2674312021-10-29 10:08:19 +08001575 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1576 */
1577static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1578{
1579 int ret = mbedtls_ssl_read_record( ssl, 0 );
1580
1581 if( ret != 0 )
1582 {
1583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1584 return( ret );
1585 }
1586
1587 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1588 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1589 {
1590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1591 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1592 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1593 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1594 }
1595
1596 ssl->keep_current_message = 1;
1597 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1598
1599 return( 0 );
1600}
1601
1602/*
Jerry Yu687101b2021-09-14 16:03:56 +08001603 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1604 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001605static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001606{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001607 int ret;
1608
1609 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001610 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001611 return( ret );
1612
Jerry Yu687101b2021-09-14 16:03:56 +08001613 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1614 return( 0 );
1615}
1616
1617/*
1618 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1619 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001620static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001621{
Jerry Yu30b071c2021-09-12 20:16:03 +08001622 int ret;
1623
1624 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1625 if( ret != 0 )
1626 return( ret );
1627
Jerry Yu687101b2021-09-14 16:03:56 +08001628 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1629 return( 0 );
1630}
Jerry Yua93ac112021-10-27 16:31:48 +08001631#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001632
Jerry Yu687101b2021-09-14 16:03:56 +08001633/*
1634 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1635 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001636static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001637{
XiaokangQianac0385c2021-11-03 06:40:11 +00001638 int ret;
1639
XiaokangQianc5c39d52021-11-09 11:55:10 +00001640 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001641 if( ret != 0 )
1642 return( ret );
1643
Ronald Cron49ad6192021-11-24 16:25:31 +01001644#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1645 mbedtls_ssl_handshake_set_state(
1646 ssl,
1647 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1648#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001649 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001650#endif
1651
XiaokangQianac0385c2021-11-03 06:40:11 +00001652 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001653}
1654
1655/*
Ronald Crond4c64022021-12-06 09:06:46 +01001656 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1657 */
1658#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1659static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1660{
1661 int ret;
1662
1663 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1664 if( ret != 0 )
1665 return( ret );
1666
1667 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1668
1669 return( 0 );
1670}
1671#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1672
1673/*
Jerry Yu687101b2021-09-14 16:03:56 +08001674 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1675 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001676static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001677{
XiaokangQian0fa66432021-11-15 03:33:57 +00001678 int ret;
1679
Ronald Cron49ad6192021-11-24 16:25:31 +01001680 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1681
XiaokangQian0fa66432021-11-15 03:33:57 +00001682 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1683 if( ret != 0 )
1684 return( ret );
1685
1686 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1687 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001688}
1689
1690/*
1691 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1692 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001693static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001694{
Jerry Yu378254d2021-10-30 21:44:47 +08001695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001696 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001697 return( 0 );
1698}
1699
1700/*
1701 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1702 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001703static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001704{
Jerry Yu378254d2021-10-30 21:44:47 +08001705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1706 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1707
1708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1709 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1710
1711 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1712
1713 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1714 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001715}
1716
Jerry Yu92c6b402021-08-27 16:59:09 +08001717int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001718{
Jerry Yu92c6b402021-08-27 16:59:09 +08001719 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001720
Jerry Yue3b34122021-09-28 17:53:35 +08001721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1722 mbedtls_ssl_states_str( ssl->state ),
1723 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001724
1725 switch( ssl->state )
1726 {
1727 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001728 * ssl->state is initialized as HELLO_REQUEST. It is the same
1729 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001730 */
1731 case MBEDTLS_SSL_HELLO_REQUEST:
1732 case MBEDTLS_SSL_CLIENT_HELLO:
1733 ret = ssl_tls13_write_client_hello( ssl );
1734 break;
1735
1736 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001737 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001738 break;
1739
1740 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001741 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001742 break;
1743
Jerry Yua93ac112021-10-27 16:31:48 +08001744#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001745 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1746 ret = ssl_tls13_process_certificate_request( ssl );
1747 break;
1748
Jerry Yu687101b2021-09-14 16:03:56 +08001749 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001750 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001751 break;
1752
1753 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001754 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001755 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001756#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001757
1758 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001759 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001760 break;
1761
Jerry Yu687101b2021-09-14 16:03:56 +08001762 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001763 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001764 break;
1765
1766 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001767 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001768 break;
1769
1770 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001771 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001772 break;
1773
Ronald Cron49ad6192021-11-24 16:25:31 +01001774 /*
1775 * Injection of dummy-CCS's for middlebox compatibility
1776 */
1777#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1778 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
Ronald Crond4c64022021-12-06 09:06:46 +01001779 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001780 break;
1781#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1782
Jerry Yu92c6b402021-08-27 16:59:09 +08001783 default:
1784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1786 }
1787
1788 return( ret );
1789}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001790
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001791#endif /* MBEDTLS_SSL_CLI_C */
1792
Ronald Cron6f135e12021-12-08 16:57:54 +01001793#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */