blob: 80cb4bf7b53ffc6ef4485295ec1c6a7d21dd679e [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 Yubdc71882021-09-14 19:30:36 +080037
Jerry Yubc20bdd2021-08-24 15:59:48 +080038/* Write extensions */
39
Jerry Yu92c6b402021-08-27 16:59:09 +080040/*
41 * ssl_tls13_write_supported_versions_ext():
42 *
43 * struct {
44 * ProtocolVersion versions<2..254>;
45 * } SupportedVersions;
46 */
Jerry Yuf4436812021-08-26 22:59:56 +080047static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080048 unsigned char *buf,
49 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000050 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080051{
52 unsigned char *p = buf;
53
Xiaofei Baid25fab62021-12-02 06:36:27 +000054 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Jerry Yu159c5a02021-08-31 12:51:25 +080056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080057
Jerry Yu388bd0d2021-09-15 18:41:02 +080058 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080059 * - extension_type (2 bytes)
60 * - extension_data_length (2 bytes)
61 * - versions_length (1 byte )
62 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080063 */
Jerry Yu92c6b402021-08-27 16:59:09 +080064 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
65
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080066 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080068
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080069 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080070 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080071 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080072
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080073 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080074 *p++ = 0x2;
75
Jerry Yu0c63af62021-09-02 12:59:12 +080076 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080077 *
Jerry Yu0c63af62021-09-02 12:59:12 +080078 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080079 *
Jerry Yu0c63af62021-09-02 12:59:12 +080080 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080081 */
Jerry Yueecfbf02021-08-30 18:32:07 +080082 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
83 ssl->conf->max_minor_ver,
84 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080085
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080087 ssl->conf->max_major_ver,
88 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080089
Xiaofei Baid25fab62021-12-02 06:36:27 +000090 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080091
92 return( 0 );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
Jerry Yuc068b662021-10-11 22:30:19 +080095static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
96 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080097 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080098{
Jerry Yue1b9c292021-09-10 10:08:31 +080099 ((void) ssl);
100
Jerry Yub85277e2021-10-13 13:36:05 +0800101 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
102 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800103 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
104 {
105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800106
107 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
108 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
109 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800110 }
111
112 return( 0 );
113}
114
Jerry Yubc20bdd2021-08-24 15:59:48 +0800115#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
116
Jerry Yu6b64fe32021-09-01 17:05:13 +0800117/*
118 * Functions for writing supported_groups extension.
119 *
120 * Stucture of supported_groups:
121 * enum {
122 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
123 * x25519(0x001D), x448(0x001E),
124 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
125 * ffdhe6144(0x0103), ffdhe8192(0x0104),
126 * ffdhe_private_use(0x01FC..0x01FF),
127 * ecdhe_private_use(0xFE00..0xFEFF),
128 * (0xFFFF)
129 * } NamedGroup;
130 * struct {
131 * NamedGroup named_group_list<2..2^16-1>;
132 * } NamedGroupList;
133 */
Jerry Yu6b64fe32021-09-01 17:05:13 +0800134#if defined(MBEDTLS_ECDH_C)
135/*
136 * In versions of TLS prior to TLS 1.3, this extension was named
137 * 'elliptic_curves' and only contained elliptic curve groups.
138 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800139static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl,
Brett Warren14efd332021-10-06 09:32:11 +0100140 unsigned char *buf,
141 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000142 size_t *out_len )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800143{
144 unsigned char *p = buf;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800145
Xiaofei Baid25fab62021-12-02 06:36:27 +0000146 *out_len = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800147
Brett Warren14efd332021-10-06 09:32:11 +0100148 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
149
150 if( group_list == NULL )
Jerry Yu7c522d42021-09-08 17:55:09 +0800151 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
152
Brett Warren14efd332021-10-06 09:32:11 +0100153 for ( ; *group_list != 0; group_list++ )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800154 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000155 const mbedtls_ecp_curve_info *curve_info;
156 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
157 if( curve_info == NULL )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800158 continue;
Jerry Yu7c522d42021-09-08 17:55:09 +0800159
Brett Warren14efd332021-10-06 09:32:11 +0100160 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800161 continue;
162
163 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2);
Brett Warren14efd332021-10-06 09:32:11 +0100164 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800165 p += 2;
166
167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
Xiaofei Baieef15042021-11-18 07:29:56 +0000168 curve_info->name, *group_list ) );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800169 }
170
Xiaofei Baid25fab62021-12-02 06:36:27 +0000171 *out_len = p - buf;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800172
173 return( 0 );
174}
175#else
Jerry Yub60e3cf2021-09-08 16:41:02 +0800176static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl,
177 unsigned char *buf,
178 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000179 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800180{
181 ((void) ssl);
182 ((void) buf);
183 ((void) end);
Xiaofei Baid25fab62021-12-02 06:36:27 +0000184 *out_len = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800185 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
186}
187#endif /* MBEDTLS_ECDH_C */
188
Jerry Yub60e3cf2021-09-08 16:41:02 +0800189static int ssl_tls13_write_named_group_list_dhe( mbedtls_ssl_context *ssl,
190 unsigned char *buf,
191 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000192 size_t *out_len )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800193{
194 ((void) ssl);
195 ((void) buf);
196 ((void) end);
Xiaofei Baid25fab62021-12-02 06:36:27 +0000197 *out_len = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "write_named_group_dhe is not implemented" ) );
199 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
200}
201
Jerry Yu6b64fe32021-09-01 17:05:13 +0800202static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
203 unsigned char *buf,
204 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000205 size_t *out_len )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800206{
207 unsigned char *p = buf ;
Xiaofei Baieef15042021-11-18 07:29:56 +0000208 unsigned char *named_group_list; /* Start of named_group_list */
209 size_t named_group_list_len; /* Length of named_group_list */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800210 size_t output_len = 0;
211 int ret_ecdhe, ret_dhe;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800212
Xiaofei Baid25fab62021-12-02 06:36:27 +0000213 *out_len = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800214
215 if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
216 return( 0 );
217
218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
219
Jerry Yub60e3cf2021-09-08 16:41:02 +0800220 /* Check if we have space for header and length fields:
Xiaofei Baieef15042021-11-18 07:29:56 +0000221 * - extension_type (2 bytes)
222 * - extension_data_length (2 bytes)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800223 * - named_group_list_length (2 bytes)
224 */
Jerry Yu6b64fe32021-09-01 17:05:13 +0800225 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
226 p += 6;
227
Xiaofei Baieef15042021-11-18 07:29:56 +0000228 named_group_list = p;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800229 ret_ecdhe = ssl_tls13_write_named_group_list_ecdhe( ssl, p, end, &output_len );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800230 if( ret_ecdhe != 0 )
231 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800232 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_ecdhe", ret_ecdhe );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800233 }
Jerry Yub60e3cf2021-09-08 16:41:02 +0800234 p += output_len;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800235
Jerry Yub60e3cf2021-09-08 16:41:02 +0800236 ret_dhe = ssl_tls13_write_named_group_list_dhe( ssl, p, end, &output_len );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800237 if( ret_dhe != 0 )
238 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800239 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_dhe", ret_dhe );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800240 }
Jerry Yub60e3cf2021-09-08 16:41:02 +0800241 p += output_len;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800242
Jerry Yu388bd0d2021-09-15 18:41:02 +0800243 /* Both ECDHE and DHE failed. */
Jerry Yu6b64fe32021-09-01 17:05:13 +0800244 if( ret_ecdhe != 0 && ret_dhe != 0 )
245 {
246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Both ECDHE and DHE groups are fail. " ) );
247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
248 }
249
250 /* Length of named_group_list*/
Xiaofei Baieef15042021-11-18 07:29:56 +0000251 named_group_list_len = p - named_group_list;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800252 if( named_group_list_len == 0 )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800253 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
256 }
257
258 /* Write extension_type */
259 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
260 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800261 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800262 /* Write length of named_group_list */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800263 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800264
Jerry Yub60e3cf2021-09-08 16:41:02 +0800265 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", buf + 4, named_group_list_len + 2 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800266
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 *out_len = p - buf;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800268
269 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
270
Jerry Yub60e3cf2021-09-08 16:41:02 +0800271 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800272}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800273
Jerry Yu56fc07f2021-09-01 17:48:49 +0800274/*
275 * Functions for writing key_share extension.
276 */
277#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800278static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800279 mbedtls_ssl_context *ssl,
280 uint16_t named_group,
281 unsigned char *buf,
282 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000283 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800284{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800286 const mbedtls_ecp_curve_info *curve_info =
287 mbedtls_ecp_curve_info_from_tls_id( named_group );
288
289 if( curve_info == NULL )
290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
291
292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
293
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800294 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
295 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800296 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800298 return( ret );
299 }
300
Xiaofei Baid25fab62021-12-02 06:36:27 +0000301 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
302 buf, end - buf,
303 ssl->conf->f_rng, ssl->conf->p_rng );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800304 if( ret != 0 )
305 {
306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
307 return( ret );
308 }
309
310 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
311 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800312 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800313}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800314#endif /* MBEDTLS_ECDH_C */
315
Jerry Yub60e3cf2021-09-08 16:41:02 +0800316static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
317 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800318{
319 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
320
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100323 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800324 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100325 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800326 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
327
Brett Warren14efd332021-10-06 09:32:11 +0100328 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800329 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000330 const mbedtls_ecp_curve_info *curve_info;
331 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
332 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100333 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800334 {
Brett Warren14efd332021-10-06 09:32:11 +0100335 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800336 return( 0 );
337 }
338 }
339#else
340 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342#endif /* MBEDTLS_ECDH_C */
343
344 /*
345 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800346 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800347 */
348
349 return( ret );
350}
351
352/*
353 * ssl_tls13_write_key_share_ext
354 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800355 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800356 *
357 * struct {
358 * NamedGroup group;
359 * opaque key_exchange<1..2^16-1>;
360 * } KeyShareEntry;
361 * struct {
362 * KeyShareEntry client_shares<0..2^16-1>;
363 * } KeyShareClientHello;
364 */
365static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
366 unsigned char *buf,
367 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000368 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800369{
370 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000371 unsigned char *client_shares; /* Start of client_shares */
372 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800373 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800374 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
375
Xiaofei Baid25fab62021-12-02 06:36:27 +0000376 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800377
378 if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
379 return( 0 );
380
Jerry Yub60e3cf2021-09-08 16:41:02 +0800381 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800382 * - extension_type (2 bytes)
383 * - extension_data_length (2 bytes)
384 * - client_shares_length (2 bytes)
385 */
386 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
387 p += 6;
388
389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
390
391 /* HRR could already have requested something else. */
392 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800393 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
394 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800395 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800396 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800397 &group_id ) );
398 }
399
400 /*
401 * Dispatch to type-specific key generation function.
402 *
403 * So far, we're only supporting ECDHE. With the introduction
404 * of PQC KEMs, we'll want to have multiple branches, one per
405 * type of KEM, and dispatch to the corresponding crypto. And
406 * only one key share entry is allowed.
407 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000408 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800409#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800410 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800411 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800412 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000413 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800414 /* Length of key_exchange */
415 size_t key_exchange_len;
416
417 /* Check there is space for header of KeyShareEntry
418 * - group (2 bytes)
419 * - key_exchange_length (2 bytes)
420 */
421 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
422 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800423 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
424 p, end,
425 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800426 p += key_exchange_len;
427 if( ret != 0 )
428 return( ret );
429
430 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000431 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800432 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000433 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800434 }
435 else
436#endif /* MBEDTLS_ECDH_C */
437 if( 0 /* other KEMs? */ )
438 {
439 /* Do something */
440 }
441 else
442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
443
Jerry Yub60e3cf2021-09-08 16:41:02 +0800444 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000445 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800446 if( client_shares_len == 0)
447 {
448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800450 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800451 /* Write extension_type */
452 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
453 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800454 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800455 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800456 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800457
458 /* Update offered_group_id field */
459 ssl->handshake->offered_group_id = group_id;
460
461 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000462 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800463
Xiaofei Baid25fab62021-12-02 06:36:27 +0000464 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800465
466 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
467
468cleanup:
469
470 return( ret );
471}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800472
Jerry Yue1b9c292021-09-10 10:08:31 +0800473#if defined(MBEDTLS_ECDH_C)
474
Jerry Yuc068b662021-10-11 22:30:19 +0800475static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800476{
477 const mbedtls_ecp_curve_info *curve_info;
478 mbedtls_ecp_group_id grp_id;
479#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
480 grp_id = ssl->handshake->ecdh_ctx.grp.id;
481#else
482 grp_id = ssl->handshake->ecdh_ctx.grp_id;
483#endif
484
485 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
486 if( curve_info == NULL )
487 {
488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
490 }
491
492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
493
Jerry Yue1b9c292021-09-10 10:08:31 +0800494 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800495 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800496
497 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
498 MBEDTLS_DEBUG_ECDH_QP );
499
500 return( 0 );
501}
502
Jerry Yuc068b662021-10-11 22:30:19 +0800503static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
504 const unsigned char *buf,
505 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800506{
507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
508
Jerry Yuc068b662021-10-11 22:30:19 +0800509 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800510 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800511 if( ret != 0 )
512 {
513 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800514
515 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
516 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
517 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800518 }
519
Jerry Yuc068b662021-10-11 22:30:19 +0800520 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800521 {
Jerry Yuc068b662021-10-11 22:30:19 +0800522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800523
Jerry Yu4a173382021-10-11 21:45:31 +0800524 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
525 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
526 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800527 }
528
529 return( 0 );
530}
Jerry Yu4a173382021-10-11 21:45:31 +0800531#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800532
533/*
Jerry Yub85277e2021-10-13 13:36:05 +0800534 * ssl_tls13_parse_key_share_ext()
535 * Parse key_share extension in Server Hello
536 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800537 * struct {
538 * KeyShareEntry server_share;
539 * } KeyShareServerHello;
540 * struct {
541 * NamedGroup group;
542 * opaque key_exchange<1..2^16-1>;
543 * } KeyShareEntry;
544 */
Jerry Yu4a173382021-10-11 21:45:31 +0800545static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800546 const unsigned char *buf,
547 const unsigned char *end )
548{
Jerry Yub85277e2021-10-13 13:36:05 +0800549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800550 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800551 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800552
Jerry Yu4a173382021-10-11 21:45:31 +0800553 /* ...
554 * NamedGroup group; (2 bytes)
555 * ...
556 */
557 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
558 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800559 p += 2;
560
Jerry Yu4a173382021-10-11 21:45:31 +0800561 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800562 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800563 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800564 {
565 MBEDTLS_SSL_DEBUG_MSG( 1,
566 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800567 (unsigned) offered_group, (unsigned) group ) );
568 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
569 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
570 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800571 }
572
573#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800574 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800575 {
576 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800577 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800578 if( ret != 0 )
579 return( ret );
580 }
Jerry Yub85277e2021-10-13 13:36:05 +0800581 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800582#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800583 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800584 {
585 /* Do something */
586 }
587 else
588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
589
590 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
591 return( ret );
592}
593
Jerry Yubc20bdd2021-08-24 15:59:48 +0800594#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
595
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800596/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800597 * CipherSuite cipher_suites<2..2^16-2>;
598 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800599static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800600 mbedtls_ssl_context *ssl,
601 unsigned char *buf,
602 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000603 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800604{
Jerry Yufec982e2021-09-07 17:26:06 +0800605 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800606 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000607 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800608 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800609
Xiaofei Baid25fab62021-12-02 06:36:27 +0000610 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800611
612 /*
613 * Ciphersuite list
614 *
615 * This is a list of the symmetric cipher options supported by
616 * the client, specifically the record protection algorithm
617 * ( including secret key length ) and a hash to be used with
618 * HKDF, in descending order of client preference.
619 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800620 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800621
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800622 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800623 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
624 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800625
Jerry Yu0c63af62021-09-02 12:59:12 +0800626 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000627 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800628 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800629 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800630 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800631 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800632
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800633 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800634 if( ciphersuite_info == NULL )
635 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800636 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
637 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800638 continue;
639
640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800641 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800642 ciphersuite_info->name ) );
643
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800644 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800645 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
646 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
647 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800648 }
649
Jerry Yu0c63af62021-09-02 12:59:12 +0800650 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000651 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800652 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800653 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800654 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
655 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800656
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800657 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000658 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800659
Jerry Yu6a643102021-08-31 14:40:36 +0800660 return( 0 );
661}
662
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800663/*
664 * Structure of ClientHello message:
665 *
666 * struct {
667 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
668 * Random random;
669 * opaque legacy_session_id<0..32>;
670 * CipherSuite cipher_suites<2..2^16-2>;
671 * opaque legacy_compression_methods<1..2^8-1>;
672 * Extension extensions<8..2^16-1>;
673 * } ClientHello;
674 */
Jerry Yu08906d02021-08-31 11:05:27 +0800675static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800676 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800677 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000678 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800679{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800680
Jerry Yubc20bdd2021-08-24 15:59:48 +0800681 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000682 unsigned char *p_extensions_len; /* Pointer to extensions length */
683 size_t output_len; /* Length of buffer used by function */
684 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800685
Jerry Yubc20bdd2021-08-24 15:59:48 +0800686 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800687 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800688
Xiaofei Baid25fab62021-12-02 06:36:27 +0000689 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800690
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800691 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800692 ssl->major_ver = ssl->conf->min_major_ver;
693 ssl->minor_ver = ssl->conf->min_minor_ver;
694
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800695 /*
696 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800697 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800698 *
699 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800700 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800701 */
Jerry Yufec982e2021-09-07 17:26:06 +0800702 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800703 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800704 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800705
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800706 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800707 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
708 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800709 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800710 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
711 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800712
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800713 /*
714 * Write legacy_session_id
715 *
716 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
717 * which has been merged with pre-shared keys in this version. A client
718 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
719 * this field to that value. In compatibility mode, this field MUST be
720 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
721 * a new 32-byte value. This value need not be random but SHOULD be
722 * unpredictable to avoid implementations fixating on a specific value
723 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
724 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800725 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100726#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
727 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
728 *p++ = (unsigned char)ssl->session_negotiate->id_len;
729 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
730 p += ssl->session_negotiate->id_len;
731
732 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
733 ssl->session_negotiate->id_len );
734#else
Jerry Yubbe09522021-09-06 21:17:54 +0800735 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
736 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100737#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800738
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800739 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800740 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800741 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800742 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800743 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800744
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800745 /* Write legacy_compression_methods
746 *
747 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800748 * one byte set to zero, which corresponds to the 'null' compression
749 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800750 */
Jerry Yubbe09522021-09-06 21:17:54 +0800751 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
752 *p++ = 1;
753 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800754
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800755 /* Write extensions */
756
757 /* Keeping track of the included extensions */
758 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800759
760 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800761 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000762 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800763 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800764
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800765 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800766 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800767 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800768 */
Jerry Yubbe09522021-09-06 21:17:54 +0800769 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800770 if( ret != 0 )
771 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800772 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800773
774#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800775 /* Write supported_groups extension
776 *
777 * It is REQUIRED for ECDHE cipher_suites.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800778 */
Jerry Yubbe09522021-09-06 21:17:54 +0800779 ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800780 if( ret != 0 )
781 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800782 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800783
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800784 /* Write key_share extension
785 *
786 * We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800787 * 1) A certificate-based ciphersuite is being offered. In this case
788 * supported_groups and supported_signature extensions have been
789 * successfully added.
790 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800791 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800792 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
793 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800794 */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800795 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800796 if( ret != 0 )
797 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800798 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800799
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800800 /* Write signature_algorithms extension
801 *
802 * It is REQUIRED for certificate authenticated cipher_suites.
803 */
Jerry Yubbe09522021-09-06 21:17:54 +0800804 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800805 if( ret != 0 )
806 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800807 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800808
Jerry Yubc20bdd2021-08-24 15:59:48 +0800809#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
810
Xiaofei Bai15a56812021-11-05 10:52:12 +0000811#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000812 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000813 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
814 if( ret != 0 )
815 return( ret );
816 p += output_len;
817#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
818
Jerry Yubc20bdd2021-08-24 15:59:48 +0800819 /* Add more extensions here */
820
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800821 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000822 extensions_len = p - p_extensions_len - 2;
823 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800824 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800825 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000826 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800827
Xiaofei Baid25fab62021-12-02 06:36:27 +0000828 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800829 return( 0 );
830}
831
Jerry Yu335aca92021-09-12 20:18:56 +0800832static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800833{
Jerry Yu92c6b402021-08-27 16:59:09 +0800834 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
835 return( 0 );
836}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800837
Jerry Yu92c6b402021-08-27 16:59:09 +0800838static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
839{
840 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800841
Jerry Yu92c6b402021-08-27 16:59:09 +0800842 if( ssl->conf->f_rng == NULL )
843 {
844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
845 return( MBEDTLS_ERR_SSL_NO_RNG );
846 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800847
Jerry Yu92c6b402021-08-27 16:59:09 +0800848 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
849 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800850 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800851 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800852 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800853 return( ret );
854 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800855
Ronald Cron49ad6192021-11-24 16:25:31 +0100856#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
857 /*
858 * Create a session identifier for the purpose of middlebox compatibility
859 * only if one has not been created already.
860 */
861 if( ssl->session_negotiate->id_len == 0 )
862 {
863 /* Creating a session id with 32 byte length */
864 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
865 ssl->session_negotiate->id, 32 ) ) != 0 )
866 {
867 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
868 return( ret );
869 }
870 ssl->session_negotiate->id_len = 32;
871 }
872#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
873
Jerry Yu6f13f642021-08-26 17:18:15 +0800874 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800875}
876
Jerry Yu92c6b402021-08-27 16:59:09 +0800877/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800878 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800879 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800880 */
881static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800882{
Jerry Yu92c6b402021-08-27 16:59:09 +0800883 int ret = 0;
884 unsigned char *buf;
885 size_t buf_len, msg_len;
886
887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
888
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800889 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800890
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800891 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
892 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
893 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800894
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800895 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800896 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800897 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800898
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800899 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
900 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800901 msg_len );
902 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800903
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800904 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
905 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
906 buf_len,
907 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800908
909cleanup:
910
911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
912 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800913}
914
Jerry Yu687101b2021-09-14 16:03:56 +0800915/*
Jerry Yu4a173382021-10-11 21:45:31 +0800916 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800917 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800918/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800919 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
920 * - SSL_SERVER_HELLO_COORDINATE_HRR
921 * to indicate which message is expected and to be parsed next. */
922#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
923#define SSL_SERVER_HELLO_COORDINATE_HRR 1
924static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
925 const unsigned char *buf,
926 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800927{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800928 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800929 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
930 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
931 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
932 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
933
934 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
935 *
Jerry Yu4a173382021-10-11 21:45:31 +0800936 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800937 * special value of the SHA-256 of "HelloRetryRequest".
938 *
939 * struct {
940 * ProtocolVersion legacy_version = 0x0303;
941 * Random random;
942 * opaque legacy_session_id_echo<0..32>;
943 * CipherSuite cipher_suite;
944 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800945 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800946 * } ServerHello;
947 *
948 */
Jerry Yub85277e2021-10-13 13:36:05 +0800949 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800950
951 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800952 {
Jerry Yub85277e2021-10-13 13:36:05 +0800953 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800954 }
955
Jerry Yub85277e2021-10-13 13:36:05 +0800956 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800957}
958
Jerry Yu745bb612021-10-13 22:01:04 +0800959/* Fetch and preprocess
960 * Returns a negative value on failure, and otherwise
961 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
962 * - SSL_SERVER_HELLO_COORDINATE_HRR
963 */
Jerry Yub85277e2021-10-13 13:36:05 +0800964static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
965 unsigned char **buf,
966 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800967{
Jerry Yu4a173382021-10-11 21:45:31 +0800968 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800969
970 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
971
Jerry Yue1b9c292021-09-10 10:08:31 +0800972 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
973 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
974 {
975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
976
977 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
978 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
979 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
980 }
981
982 *buf = ssl->in_msg + 4;
983 *buf_len = ssl->in_hslen - 4;
984
Jerry Yub85277e2021-10-13 13:36:05 +0800985 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
986 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800987 {
Jerry Yu745bb612021-10-13 22:01:04 +0800988 case SSL_SERVER_HELLO_COORDINATE_HELLO:
989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
990 break;
991 case SSL_SERVER_HELLO_COORDINATE_HRR:
992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
993 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800994 }
995
996cleanup:
997
998 return( ret );
999}
1000
Jerry Yu4a173382021-10-11 21:45:31 +08001001static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1002 const unsigned char **buf,
1003 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001004{
1005 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001006 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001007
Jerry Yude4fb2c2021-09-19 18:05:08 +08001008 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001009 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001010
Jerry Yu4a173382021-10-11 21:45:31 +08001011 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001012
1013 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001014 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1015 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001016 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001017 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1018 ssl->session_negotiate->id,
1019 ssl->session_negotiate->id_len );
1020 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001021 legacy_session_id_echo_len );
1022
1023 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1024 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1025
Jerry Yue1b9c292021-09-10 10:08:31 +08001026 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1027 }
1028
Jerry Yu4a173382021-10-11 21:45:31 +08001029 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001030 *buf = p;
1031
1032 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001033 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001034 return( 0 );
1035}
1036
Jerry Yu4a173382021-10-11 21:45:31 +08001037static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1038 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001039{
Jerry Yu4a173382021-10-11 21:45:31 +08001040 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1041
Jerry Yue1b9c292021-09-10 10:08:31 +08001042 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001043 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001044 {
Jerry Yu4a173382021-10-11 21:45:31 +08001045 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001046 {
1047 return( 1 );
1048 }
1049 }
1050 return( 0 );
1051}
1052
1053/* Parse ServerHello message and configure context
1054 *
1055 * struct {
1056 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1057 * Random random;
1058 * opaque legacy_session_id_echo<0..32>;
1059 * CipherSuite cipher_suite;
1060 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001061 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001062 * } ServerHello;
1063 */
Jerry Yuc068b662021-10-11 22:30:19 +08001064static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1065 const unsigned char *buf,
1066 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001067{
Jerry Yub85277e2021-10-13 13:36:05 +08001068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001069 const unsigned char *p = buf;
Jerry Yub85277e2021-10-13 13:36:05 +08001070 size_t extensions_len;
1071 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001072 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001073 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001074
1075 /*
1076 * Check there is space for minimal fields
1077 *
1078 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001079 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001080 * - legacy_session_id_echo ( 1 byte ), minimum size
1081 * - cipher_suite ( 2 bytes)
1082 * - legacy_compression_method ( 1 byte )
1083 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001084 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001085
1086 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001087 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1088
Jerry Yu4a173382021-10-11 21:45:31 +08001089 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001090 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001091 * ...
1092 * with ProtocolVersion defined as:
1093 * uint16 ProtocolVersion;
1094 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001095 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1096 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1097 {
1098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1099 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1100 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1101 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1102 }
1103 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001104
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001105 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001106 * Random random;
1107 * ...
1108 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001109 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001110 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001111 memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1112 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yue1b9c292021-09-10 10:08:31 +08001113 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001114 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1115 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001116
Jerry Yu4a173382021-10-11 21:45:31 +08001117 /* ...
1118 * opaque legacy_session_id_echo<0..32>;
1119 * ...
1120 */
1121 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 {
1123 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1124 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1125 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1126 }
1127
Jerry Yu4a173382021-10-11 21:45:31 +08001128 /* ...
1129 * CipherSuite cipher_suite;
1130 * ...
1131 * with CipherSuite defined as:
1132 * uint8 CipherSuite[2];
1133 */
1134 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001135 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1136 p += 2;
1137
Jerry Yu4a173382021-10-11 21:45:31 +08001138
1139 /*
1140 * Check whether this ciphersuite is supported and offered.
1141 * Via the force_ciphersuite version we may have instructed the client
1142 * to use a different ciphersuite.
1143 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001144 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001145 if( ciphersuite_info == NULL ||
1146 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001147 {
Jerry Yub85277e2021-10-13 13:36:05 +08001148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 cipher_suite ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001150
Jerry Yu4a173382021-10-11 21:45: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 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001154 }
1155
Jerry Yue1b9c292021-09-10 10:08:31 +08001156
Jerry Yu4a173382021-10-11 21:45:31 +08001157 /* Configure ciphersuites */
1158 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1159
1160 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001161 ssl->session_negotiate->ciphersuite = cipher_suite;
1162
Jerry Yue1b9c292021-09-10 10:08:31 +08001163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1164 cipher_suite, ciphersuite_info->name ) );
1165
1166#if defined(MBEDTLS_HAVE_TIME)
1167 ssl->session_negotiate->start = time( NULL );
1168#endif /* MBEDTLS_HAVE_TIME */
1169
Jerry Yu4a173382021-10-11 21:45:31 +08001170 /* ...
1171 * uint8 legacy_compression_method = 0;
1172 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001173 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001174 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001175 if( p[0] != 0 )
1176 {
Jerry Yub85277e2021-10-13 13:36:05 +08001177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001178 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1179 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1180 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1181 }
1182 p++;
1183
Jerry Yub85277e2021-10-13 13:36:05 +08001184 /* ...
1185 * Extension extensions<6..2^16-1>;
1186 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001187 * struct {
1188 * ExtensionType extension_type; (2 bytes)
1189 * opaque extension_data<0..2^16-1>;
1190 * } Extension;
1191 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001192 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001193 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001194 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001195
Jerry Yu4a173382021-10-11 21:45:31 +08001196 /* Check extensions do not go beyond the buffer of data. */
1197 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1198 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001199
Jerry Yu4a173382021-10-11 21:45:31 +08001200 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001201
Jerry Yu4a173382021-10-11 21:45:31 +08001202 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001203 {
1204 unsigned int extension_type;
1205 size_t extension_data_len;
1206
Jerry Yu4a173382021-10-11 21:45:31 +08001207 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001208 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1209 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1210 p += 4;
1211
Jerry Yu4a173382021-10-11 21:45:31 +08001212 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001213
1214 switch( extension_type )
1215 {
1216 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1217 MBEDTLS_SSL_DEBUG_MSG( 3,
1218 ( "found supported_versions extension" ) );
1219
Jerry Yuc068b662021-10-11 22:30:19 +08001220 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001221 p,
1222 p + extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001223 if( ret != 0 )
1224 return( ret );
1225 break;
1226
1227 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1228 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001230
1231 MBEDTLS_SSL_PEND_FATAL_ALERT(
1232 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1233 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1234 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001235
1236#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1237 case MBEDTLS_TLS_EXT_KEY_SHARE:
1238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001239 if( ( ret = ssl_tls13_parse_key_share_ext( ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +08001240 p, p + extension_data_len ) ) != 0 )
1241 {
1242 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001243 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001244 ret );
1245 return( ret );
1246 }
1247 break;
1248#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1249
1250 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001251 MBEDTLS_SSL_DEBUG_MSG(
1252 3,
1253 ( "unknown extension found: %u ( ignoring )",
1254 extension_type ) );
1255
1256 MBEDTLS_SSL_PEND_FATAL_ALERT(
1257 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1258 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1259 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001260 }
1261
1262 p += extension_data_len;
1263 }
1264
1265 return( 0 );
1266}
1267
Jerry Yuc068b662021-10-11 22:30:19 +08001268static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001269{
Jerry Yub85277e2021-10-13 13:36:05 +08001270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001271 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001272 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001273 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001274
Jerry Yub85277e2021-10-13 13:36:05 +08001275 /* Determine the key exchange mode:
1276 * 1) If both the pre_shared_key and key_share extensions were received
1277 * then the key exchange mode is PSK with EPHEMERAL.
1278 * 2) If only the pre_shared_key extension was received then the key
1279 * exchange mode is PSK-only.
1280 * 3) If only the key_share extension was received then the key
1281 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001282 */
Jerry Yub85277e2021-10-13 13:36:05 +08001283 switch( handshake->extensions_present &
1284 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001285 {
Jerry Yu745bb612021-10-13 22:01:04 +08001286 /* Only the pre_shared_key extension was received */
1287 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001288 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001289 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001290
Jerry Yu745bb612021-10-13 22:01:04 +08001291 /* Only the key_share extension was received */
1292 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001293 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001294 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001295
Jerry Yu745bb612021-10-13 22:01:04 +08001296 /* Both the pre_shared_key and key_share extensions were received */
1297 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001298 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001299 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001300
Jerry Yu745bb612021-10-13 22:01:04 +08001301 /* Neither pre_shared_key nor key_share extension was received */
1302 default:
1303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1304 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1305 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001306 }
1307
1308 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1309 *
1310 * TODO: We don't have to do this in case we offered 0-RTT and the
1311 * server accepted it. In this case, we could skip generating
1312 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001313 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001314 if( ret != 0 )
1315 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001317 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001318 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001319 }
1320
1321 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001322 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001323 if( ret != 0 )
1324 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001326 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001327 }
1328
1329 /* Next evolution in key schedule: Establish handshake secret and
1330 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001331 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001332 if( ret != 0 )
1333 {
Jerry Yuc068b662021-10-11 22:30:19 +08001334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1335 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001336 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001337 }
1338
Jerry Yub85277e2021-10-13 13:36:05 +08001339 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001340 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001341 {
1342 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1343 goto cleanup;
1344 }
Jerry Yu0b177842021-09-05 19:41:30 +08001345
1346 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1347 ssl->conf->endpoint,
1348 ssl->session_negotiate->ciphersuite,
1349 &traffic_keys,
1350 ssl );
1351 if( ret != 0 )
1352 {
1353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001354 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001355 }
1356
Jerry Yub85277e2021-10-13 13:36:05 +08001357 handshake->transform_handshake = transform_handshake;
1358 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001359
1360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1361 ssl->session_in = ssl->session_negotiate;
1362
1363 /*
1364 * State machine update
1365 */
1366 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1367
Jerry Yu4a173382021-10-11 21:45:31 +08001368cleanup:
1369
Jerry Yu0b177842021-09-05 19:41:30 +08001370 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001371 if( ret != 0 )
1372 {
Jerry Yub85277e2021-10-13 13:36:05 +08001373 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001374
Jerry Yu4a173382021-10-11 21:45:31 +08001375 MBEDTLS_SSL_PEND_FATAL_ALERT(
1376 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1377 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1378 }
1379 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001380}
1381
1382/*
Jerry Yu4a173382021-10-11 21:45:31 +08001383 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001384 * Handler for MBEDTLS_SSL_SERVER_HELLO
1385 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001386static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001387{
Jerry Yu4a173382021-10-11 21:45:31 +08001388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001389 unsigned char *buf;
1390 size_t buf_len;
1391
1392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1393
1394 /* Coordination step
1395 * - Fetch record
1396 * - Make sure it's either a ServerHello or a HRR.
1397 * - Switch processing routine in case of HRR
1398 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001399 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1400 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1401
Jerry Yub85277e2021-10-13 13:36:05 +08001402 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001403 /* Parsing step
1404 * We know what message to expect by now and call
1405 * the respective parsing function.
1406 */
1407 if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
1408 {
Jerry Yuc068b662021-10-11 22:30:19 +08001409 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
1410 buf + buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001411
Xiaofei Bai746f9482021-11-12 08:53:56 +00001412 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1413 MBEDTLS_SSL_HS_SERVER_HELLO,
1414 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001415
Jerry Yuc068b662021-10-11 22:30:19 +08001416 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001417 }
1418 else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
1419 {
Jerry Yu4a173382021-10-11 21:45:31 +08001420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "HRR not supported" ) );
1421 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ,
1422 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1423 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yue1b9c292021-09-10 10:08:31 +08001424 }
1425
1426cleanup:
1427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
1428 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001429}
1430
1431/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001432 *
1433 * EncryptedExtensions message
1434 *
1435 * The EncryptedExtensions message contains any extensions which
1436 * should be protected, i.e., any which are not needed to establish
1437 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001438 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001439
1440/*
1441 * Overview
1442 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001443
1444/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001445static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001446
XiaokangQian97799ac2021-10-11 10:05:54 +00001447static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1448 const unsigned char *buf,
1449 const unsigned char *end );
1450static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001451
1452/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001453 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001454 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001455static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001456{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001457 int ret;
1458 unsigned char *buf;
1459 size_t buf_len;
1460
1461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1462
Xiaofei Bai746f9482021-11-12 08:53:56 +00001463 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001464 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001465 &buf, &buf_len ) );
1466
1467 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001468 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001469 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001470
Xiaofei Bai746f9482021-11-12 08:53:56 +00001471 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001472 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001473
XiaokangQian97799ac2021-10-11 10:05:54 +00001474 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001475
1476cleanup:
1477
1478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1479 return( ret );
1480
1481}
1482
XiaokangQian08da26c2021-10-09 10:12:11 +00001483/* Parse EncryptedExtensions message
1484 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001485 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001486 * } EncryptedExtensions;
1487 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001488static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1489 const unsigned char *buf,
1490 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001491{
1492 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001493 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001494 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001495 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001496
XiaokangQian08da26c2021-10-09 10:12:11 +00001497 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001498 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001499 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001500
XiaokangQian97799ac2021-10-11 10:05:54 +00001501 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001502 extensions_end = p + extensions_len;
1503 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001504
XiaokangQian8db25ff2021-10-13 05:56:18 +00001505 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001506 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001507 unsigned int extension_type;
1508 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001509
XiaokangQian08da26c2021-10-09 10:12:11 +00001510 /*
1511 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001512 * ExtensionType extension_type; (2 bytes)
1513 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001514 * } Extension;
1515 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001516 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001517 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1518 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1519 p += 4;
1520
XiaokangQian8db25ff2021-10-13 05:56:18 +00001521 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001522
XiaokangQian97799ac2021-10-11 10:05:54 +00001523 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001524 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001525 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001526 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001527 switch( extension_type )
1528 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001529
XiaokangQian08da26c2021-10-09 10:12:11 +00001530 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1532 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001533
XiaokangQian08da26c2021-10-09 10:12:11 +00001534 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001535 MBEDTLS_SSL_DEBUG_MSG(
1536 3, ( "unsupported extension found: %u ", extension_type) );
1537 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001538 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001539 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1540 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001541 }
1542
XiaokangQian08da26c2021-10-09 10:12:11 +00001543 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001544 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001545
XiaokangQian97799ac2021-10-11 10:05:54 +00001546 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001547 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001548 {
1549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001550 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001551 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001552 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001553 }
1554
1555 return( ret );
1556}
1557
XiaokangQian97799ac2021-10-11 10:05:54 +00001558static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001559{
Jerry Yua93ac112021-10-27 16:31:48 +08001560#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001561 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001562 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1563 else
Jerry Yud2674312021-10-29 10:08:19 +08001564 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001565#else
1566 ((void) ssl);
1567 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1568#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001569 return( 0 );
1570}
1571
Jerry Yua93ac112021-10-27 16:31:48 +08001572#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001573/*
Jerry Yud2674312021-10-29 10:08:19 +08001574 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1575 */
1576static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1577{
1578 int ret = mbedtls_ssl_read_record( ssl, 0 );
1579
1580 if( ret != 0 )
1581 {
1582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1583 return( ret );
1584 }
1585
1586 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1587 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1588 {
1589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1590 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1591 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1592 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1593 }
1594
1595 ssl->keep_current_message = 1;
1596 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1597
1598 return( 0 );
1599}
1600
1601/*
Jerry Yu687101b2021-09-14 16:03:56 +08001602 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1603 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001604static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001605{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001606 int ret;
1607
1608 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001609 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001610 return( ret );
1611
Jerry Yu687101b2021-09-14 16:03:56 +08001612 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1613 return( 0 );
1614}
1615
1616/*
1617 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1618 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001619static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001620{
Jerry Yu30b071c2021-09-12 20:16:03 +08001621 int ret;
1622
1623 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1624 if( ret != 0 )
1625 return( ret );
1626
Jerry Yu687101b2021-09-14 16:03:56 +08001627 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1628 return( 0 );
1629}
Jerry Yua93ac112021-10-27 16:31:48 +08001630#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001631/*
1632 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1633 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001634static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001635{
XiaokangQianac0385c2021-11-03 06:40:11 +00001636 int ret;
1637
XiaokangQianc5c39d52021-11-09 11:55:10 +00001638 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001639 if( ret != 0 )
1640 return( ret );
1641
Ronald Cron49ad6192021-11-24 16:25:31 +01001642#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1643 mbedtls_ssl_handshake_set_state(
1644 ssl,
1645 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1646#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001647 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001648#endif
1649
XiaokangQianac0385c2021-11-03 06:40:11 +00001650 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001651}
1652
1653/*
Jerry Yu687101b2021-09-14 16:03:56 +08001654 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1655 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001656static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001657{
XiaokangQian0fa66432021-11-15 03:33:57 +00001658 int ret;
1659
Ronald Cron49ad6192021-11-24 16:25:31 +01001660 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1661
XiaokangQian0fa66432021-11-15 03:33:57 +00001662 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1663 if( ret != 0 )
1664 return( ret );
1665
1666 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1667 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001668}
1669
1670/*
1671 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1672 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001673static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001674{
Jerry Yu378254d2021-10-30 21:44:47 +08001675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001676 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001677 return( 0 );
1678}
1679
1680/*
1681 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1682 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001683static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001684{
Jerry Yu378254d2021-10-30 21:44:47 +08001685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1686 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1687
1688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1689 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1690
1691 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1692
1693 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1694 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001695}
1696
Jerry Yu92c6b402021-08-27 16:59:09 +08001697int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001698{
Jerry Yu92c6b402021-08-27 16:59:09 +08001699 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001700
Xiaofei Baid25fab62021-12-02 06:36:27 +00001701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %d", ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001702
1703 switch( ssl->state )
1704 {
1705 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001706 * ssl->state is initialized as HELLO_REQUEST. It is the same
1707 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001708 */
1709 case MBEDTLS_SSL_HELLO_REQUEST:
1710 case MBEDTLS_SSL_CLIENT_HELLO:
1711 ret = ssl_tls13_write_client_hello( ssl );
1712 break;
1713
1714 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001715 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001716 break;
1717
1718 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001719 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001720 break;
1721
Jerry Yua93ac112021-10-27 16:31:48 +08001722#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001723 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1724 ret = ssl_tls13_process_certificate_request( ssl );
1725 break;
1726
Jerry Yu687101b2021-09-14 16:03:56 +08001727 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001728 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001729 break;
1730
1731 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001732 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001733 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001734#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001735
1736 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001737 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001738 break;
1739
Jerry Yu687101b2021-09-14 16:03:56 +08001740 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001741 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001742 break;
1743
1744 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001745 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001746 break;
1747
1748 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001749 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001750 break;
1751
Ronald Cron49ad6192021-11-24 16:25:31 +01001752 /*
1753 * Injection of dummy-CCS's for middlebox compatibility
1754 */
1755#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1756 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1757 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1758 break;
1759#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1760
Jerry Yu92c6b402021-08-27 16:59:09 +08001761 default:
1762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1764 }
1765
1766 return( ret );
1767}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001768
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001769#endif /* MBEDTLS_SSL_CLI_C */
1770
1771#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */